Sunday, March 24, 2013

define complex group for highlighting

Let's consider defining group for more complex text instead of strait forward word list.

Suppose you want to define group of any word consisting lower cases and want group name as Identifier. The following command makes group xidentifire which is lowercase.   
                                 :syntax match xIdentifier /\<\l\+\>/
Defining match for a comment. Let's assume that in x programming language anything after # to the end of the line is considered as comment.
                                :syntax match xComment /#.*/
Since you can use any search pattern, you can highlight very complex things with match items.

In x programming language strings are enclosed in double quotation marks. So we have to write a logic that match starts with quotation and end with first quotation after that. Command is written as follows.
                             :syntax region xString start=/"/ end=/"/
The "start" and "end" directives indicates start and end of the sting region. But as we know that sting can also be written as
                           "A string with a double quote (\") in it"
This string will create a problem as string will terminated at second  quotation in the middle of the string according to out definition of string group in above. For skipping \" we have to add information about skipping as following.
                      :syntax region xString start=/"/ skip=/\\"/ end=/"/ 

Nesting Items

Suppose you want to highlight TODO in big yellow latter even though it is highlighted blue in comment. To inform gvim/vim you should write the following command in command line of gvim/vim.
                    :syntax keyword xTodo TODO contained
                    :syntax match xComment /%.*/ contains=xTodo
The first gvim command line indicates that keyword can only exist in another syntax item. The next line   contains=xTodo indicates that xTodo syntax element is inside it. 

Recursive nesting   
Suppose x language defines code blocks in curly brackets. This code may contains another code block as follows. One example of the code is given below.
                          while(a>b) {
                                      while(x>y) {
                                                           printf("inside block");
                                                         }
                                             }
gvim/vim command for this type of recursive blocs is as follows.
                :syntax region xBlock start=/{/ end=/}/ contains=xBlock
In the above code first block starts with { in first line and second block starts in second line with {. Since we are inside a xBlock and the xBlock contains itself so the recursive block starts again. now in third line } is detected so nested block ends and then in fourth line top  block ends.


PREV PAGE                                             INDEX                                         NEXT PAGE

No comments:

Post a Comment