[TxMt] language mode is not per tab/file?

Charilaos Skiadas cskiadas at uchicago.edu
Mon Nov 7 02:15:23 UTC 2005


On Nov 6, 2005, at 11:29 AM, Greg Humphreys wrote:

> Along those lines, I have a feature request which I would use  
> frequently, which is that a single file could be multi-mode based  
> on some very simple regular expression matching.
>
> For example, I have lots of files that look like this:
>
<example snipped>
> and so on.  I'd like the sections to be syntax highlighted (hilit?)  
> according to their respective styles, and when the cursor is  
> between the <<...>>= and @, I'd like to edit in C++ mode with all  
> the associated macros / tab expanders, and in LaTeX otherwise.
>
> I'm mainly just a user of TextMate; I haven't really extended it in  
> any meaningful way; is this something that an experienced end-user  
> could do, or would it require a fundamental change to the core?
>
> Allan?  Thoughts about this?  I imagine it would be implemented as  
> a "meta-mode", which is just a mode that tells TM under what  
> circumstances it should interpret blocks as other modes.

Yes, this is already possible. This is a portion of the LaTeX syntax  
file:

         {    name = 'source.python.embedded.latex';
             comment = 'Put the lstlisting match before the more  
general environment listing. Someday it would be nice to make this  
rule general enough to figure out which language is inside the  
lstlisting environment rather than my own personal use for python. -- 
Brad';
             begin = '(\\begin\{)(lstlisting)(\})(\[(.*)\])?';
             end = '(\\end\{)(lstlisting)(\})';
             captures =
             {    1 = { name = 'support.function.latex'; };
                 2 = { name = 'variable.parameter.latex'; };
                 3 = { name = 'support.function.latex'; };
                 4 = { name = 'support.function.latex'; };
             };
             patterns = ( { include = 'source.python'; } );
         },

As you can see, the part saying include = 'source.python' will make  
it so that when in this mode, you are really editing in python, with  
the full power of the python bundle. So it is just a question of  
adding your own thing in there, by the appropriate begin,end regex's.  
This is actually not as hard as it looks, if you read the "syntax  
highlight" part of the help file, or just look at some example  
language files.
I would guess something like this would work:
         {    name = 'source.c++.embedded.latex';
             begin = '^(<<).+(>>)=';
             end = '^(@)$';
             patterns = ( { include = 'source.c++'; } );
         },

You'll want to add some capture patterns of course, to color things  
properly, and maybe finetune the regexs. But the inside should  
already be colored properly.

Haris






More information about the textmate mailing list