views:

257

answers:

4

Hi all,

I have XYZ highlighted in the header file where I have defined XYZ. However at the point of where it is used, XYZ is not highlighted. How would I fix this ?

I have attached two screen shots (see TH_SYN in the code) to clarify my question- link text

Any pointers are welcome.

Many thanks.

A: 

I think it's basing the highlight on the first one on the fact that it starts with "#define". There isn't any sort of marker on the second one that vim could use to decide it needs to be highlighted. Vim doesn't do deep syntax analysis like Eclipse can do, it's just simple lexing.

Paul Tomblin
thanks..i now better understand it how VIM highlights
A: 

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

For more information, there are plenty of posts on ctags + vim. See, for example

http://stackoverflow.com/questions/563616/vimctags-tips-and-tricks http://stackoverflow.com/questions/155449/vim-auto-generate-ctags

and plenty of others.

Peter
Thanks for the note. I have tried ctags based highlighters, but they tend to make VIM slow since my tags file is huge. I'm looking for some kind of script like the one mentioned here for function names-http://stackoverflow.com/questions/736701/class-function-names-highlighting-in-vim/773392
+4  A: 

There's no built in way to highlight defines without using a tag highlighter. If you want to just highlight defined names (rather than having the comparatively slow response of a full tag highlighter), you could modify the tag highlighter to only highlight defined names.

If you use my tag highlighter, you could modify mktypes.py (unless you're using the Windows executable version, in which case email me on the address on the website and I'll 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'
      ]

to this:

UsedTypes = ['ctags_d']

This will generate a type highlighting file that only includes defined names and it should therefore run a lot quicker. If you have too many defined names in your project then it'll still slow Vim down a bit.

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

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'll need to make sure you've highlighted DefinedName in your colourscheme, e.g.

hi DefinedName guifg=#ee82ee

(assuming you're using the GUI).

Al
thanks Al..I'll give this a shot sometime
awesome man...i will try it out too
Yogesh Arora
+2  A: 

I have done a very crude way of doing this for Java constants (static finals), based on the fact that all constants, are all caps with underbars. Almost no other identifiers match that criteria.

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

Edit. Adding sample

In your language syntax file just add something like:

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

You can do HiLink to Constant, or any of the defined highlight groups you like.

Ayman
great..can your please tell me the lines I've to put in my .vimrc to do the same?Thanks.
`HiLink` doesn't exist for me. Should that be `hi def link` ?Also, the match expression should be word delimited. (I wonder if you have settings enabled that I don't.) I also added a requirement for 3 characters or more: `syn match cConstant "\<[A-Z][A-Z0-9_]\{3,\}\>"` `hi def link cConstant Constant`
pydave