Highlighting #defined value in VIM

2019-08-23 10:29发布

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.

4条回答
萌系小妹纸
2楼-- · 2019-08-23 10:40

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

Vim and Ctags tips and tricks Vim auto-generate ctags

and plenty of others.

查看更多
三岁会撩人
3楼-- · 2019-08-23 10:45

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.

查看更多
干净又极端
4楼-- · 2019-08-23 10:56

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.

查看更多
Deceive 欺骗
5楼-- · 2019-08-23 10:58

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).

查看更多
登录 后发表回答