Highlighting #defined value in VIM

I have XYZ highlighted in the header file where I defined XYZ. However, in the place where it is used, XYZ is not highlighted. How can i fix this?

I added two screenshots (see TH_SYN in the code) to clarify my question -

link text

Any pointers are welcome.

0
macros vim c-preprocessor syntax-highlighting
03 Oct '09 at 2:16
source share
4 answers

I made a very crude way to do this for Java constants (static finals), based on the fact that all constants are all caps with stands. Almost no other identifier meets these criteria.

So, very simple and very fast, but not 100% accurate, is to match all caps with the same syntax group as yours.

Change Adding a Sample

In your language syntax file, just add something like:

syn match defined "[AZ][A-Z0-9_]*" HiLink defined Type 

You can make HiLink up to Constant or any of the selected highlight groups that you like.

+3
04 Oct '09 at 4:01
source share

There is no built-in way to highlight elements without using a tag shortcut. If you want to simply highlight specific names (instead of having a relatively slow response to the full tag label), you can change the tag label only to highlight specific names.

If you use my tag shortcut , you can change mktypes.py (if you are not using an executable version of Windows, in which case, write to me at the address on the website and I will compile it for you) by changing this:

 UsedTypes = [ 'ctags_c', 'ctags_d', 'ctags_e', 'ctags_f', 'ctags_g', 'ctags_k', 'ctags_m', 'ctags_p', 'ctags_s', 'ctags_t', 'ctags_u', 'ctags_v' ] 

:

 UsedTypes = ['ctags_d'] 

This will lead to the creation of a type highlight file that includes only certain names, and therefore it should start much faster. If you have too many specific names in your project, it will slow down Vim anyway.

To highlight only specific names defined in the current file, add an autorun that calls the Vim function after reading the file. The function should be something like this:

 function! HighlightDefinedNames() " Clear any existing defined names syn clear DefinedName " Run through the whole file for l in getline('1','$') " Look for #define if l =~ '^\s*#\s*define\s\+' " Find the name part of the #define let name = substitute(l, '^\s*#\s*define\s\+\(\k\+\).*$', '\1', '') " Highlight it as DefinedName exe 'syn keyword DefinedName ' . name endif endfor endfunction 

You need to make sure that you highlight DefinedName in your color scheme, for example.

 hi DefinedName guifg=#ee82ee 

(assuming you are using a GUI).

+4
Oct 03 '09 at 9:32
source share

I think he bases the selection on the first on the fact that it starts with "#define". In the second, there is no marker that vim could use to decide that it should be highlighted. Vim does not do deep parsing like Eclipse does, it's just plain lexing.

0
Oct 03 '09 at 2:27
source share

It looks like you want to customize the selection based on specific constant names, etc. You can accomplish this using ctags or similar to generate tags based on your constants, and then get vim to highlight the result.

For more information, there are many posts about ctags + vim. See for example

Tips and Tricks Vim and Ctags Vim auto-generate ctags

and many others.

0
03 Oct '09 at 3:45
source share



All Articles