Vim script: check if the current word is/isn't a C/C++ keyword

vim, vim-plugin

Solution

You can do that depending on current syntax highlight script. Say, this function returns if your cursor is on some access modifier (`public`, `protected`, etc), or some C++ type (say, `bool`, etc) :

function! IsCppAccessOrType()
   return match(synIDattr(synID(line("."), col("."), 1), "name"), '\v\CcppAccess|cppType') >= 0
endfunction

But, again, this will completely depend on current syntax script. Example above depends on standard `syntax/cpp.vim` provided with Vim 7.3 .

By the way, there is very useful trick to deal with syntax highlight stuff:

" Show syntax highlighting groups for word under cursor 
nnoremap <silent> <F10> :call <SID>SynStack()<CR>
function! <SID>SynStack()
   if !exists("*synstack")
      return
   endif
   echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')
endfunc

Add this to your `vimrc`, and when you press F10, stack of syntax items will be echoed ( see `:help synstack` for more details ).

I have found this trick somewhere on http://vimbits.com .

Problem

I'm working on a small vim plugin which should highlight current word occurrences after cursor idle. I have a highlight part, and what I need is to check if the current word is not a C/C++ keyword.

Original source