[TxMt] C comments && doxygen

Allan Odgaard throw-away-1 at macromates.com
Sun Apr 9 14:22:54 UTC 2006

On 9/4/2006, at 11:13, Enrico Franchi wrote:

> Once I found in the Wiki a tip to let TextMate auto add * in C  
> comments. That is to say when I press return [...]

Create a new snippet (Window => Show Bundle Editor), set the key  
equivalent to ‘return’ and the scope selector to ‘comment.block’ --  
now that snippet is inserted when you press return inside block  
comments. You could refine the scope selector, e.g. ‘source.c  
comment.block’ for only block comments in C.

The contents of this snippet should be a newline plus ‘ *’ to have  
that inserted after the newline.

But since we only want the leading space after a line with ‘/*’, we  
need conditional insertion. That is, insert ‘ *’ when the current  
line is the starting line (containing ‘/*’) and otherwise insert ‘*’.

We can do this by doing a regexp replacement on the current line, to  
detect if it’s the first line, this could be:

    ${TM_CURRENT_LINE/.*?(\/\*(?!.*\*\/)).*|.*/(?1: )* /}

It looks a little complex, but it’s rather simple, it does:

    1) Try:
       a) match anything (non-greedy)
       b) match /* not followed by */ (put in capture 1)
       c) match the rest of the line
    2) If that failed, instead try:
       a) match the entire line

The replacement then does:

    1) If there was a capture 1
       a) insert ‘ ’
    2) Insert ‘* ’

Effectively inserting ‘ * ’ if the line matches the ‘is a block start  
comment’ otherwise only insert ‘* ’.

As discussed not so long ago, a problem is that when the caret is  
exactly after ‘*/’ the scope is still block.comment, since the scope  
of the caret is that of the character to the left of it [1]. We could  
catch this case, and not insert anything, when in that situation,  
this would make the snippet:

    ${TM_CURRENT_LINE/(.*\*\/$)|.*?(\/\*(?!.*\*\/)).*|.*/(?1::(?2: ) 
* )/}

I leave it as a reader exercise to dissect it.

Remember the actual newline before this regexp substitution.

> A more advanced question is: could a "Doxygen" mode be done?
> Is someone doing it?

There is a Doxygen bundle at the svn repository.

> I suppose it is possible, since it should behave quite like the css  
> mode inside the HTML mode or something like that?

Yes. Currently it would require a (simple) modification to the C  
language grammar.

I don’t know which keywords Doxygen use, but we could probably ship  
with markup of these by default.

Long-term there will be a system for keeping such extensions isolated  
in their own bundle, and only have them used, when that bundle is  

[1] I am currently experimenting with changing it so that the scope  
of the caret is that of the character to the right of it. Both  
solutions have disadvantages, but currently it looks like using the  
character to the right has the fewest.

More information about the textmate mailing list