On Wed, Jun 14, 2006 at 02:29:59PM +0400, Agibov Dmitry wrote:
> 
> Hi, All!
> 
> In my source code I have some c-like macroses
> How can I highlight this macroses? It it possible?
> 
> What I want is
> 
> file1 header_file.h
> ...
> #define MYVAR 1
> ...
> 
> file2 my_source_code.c
> 
> #include "header_file.h"
> ...
> void somefunc()
> {
>   int x = MYVAR;
>           ^------- highlight this
>           
> }
> ...

     Of course, it is possible.  It just depends on how hard you want to
work.  First, define a highlight group for macros.  For example,

:hi link cMacro Macro

Then, to see how this will be highlighted, just try

:hi cMacro

     Next, consider taking the easy way out:

:syn match cMacro /\<\u\+\>/

That should highlight any word made of upper-case characters.  Even if
you do not want to take the easy way out, you may as well try this to
make sure that we do not have to worry about syntax regions and
containedin specifications, etc.  After testing this, get rid of it:

:syn clear cMacro

     Assuming you really want to highlight only macros that have been
defined, write a function that will search through a file (e.g.,
header_file.h) and build up a list of words:


function! PatternList(pat)
  let matches = []
  execute "g/" . a:pat . "/let matches += [matchstr(getline('.'), a:pat)]"
  return matches
endfunction

Next, use this function to build up a list of included files and, for
each one, find the macros that it defines.

function MacroList()
  let mpat = '^\s*#define\s\+\zs\S\+'
  let macros = PatternList(mpat)
  let files = PatternList('^\s*#include\s*"\zs\f\+\ze"')
  for file in files
    execute "silent tabedit" file
    let macros += PatternList(mpat)
    silent tabclose
  endfor
  return macros
endfunction

Finally, a few lines to define the syntax matches:

let macros = MacroList()
  for macro in macros
  execute "syn keyword cMacro" macro
endfor

(It seems that "for macro in MacroList()" does not work.)

     If this suits your needs, you can put it in your ftplugin file, or
perhaps your syntax file, for C.  We can also refine it, trying to avoid
screen flickers when doing the :tabedit and :tabclose , or by
recursively searching through included files.

HTH                                     --Benji Fisher

Reply via email to