On Jan 6, 9:49 pm, Gusman <[email protected]> wrote:
>
> Your idea to parse tags file is interesting. My plan is putting all defined
> (selected by user using hotkey) #ifdef into a file, and make a plugin to
> parse it.
>
> Actually I still don't know when VIM start to parsing and generate the
> highlight. Does vim generate the highlight only when we open the file?
>
Vim parses the file to determine the syntax highlighting when loading
a file, or applying a new file type, or when turning syntax on. I
believe you might also be able to trigger it with a :doautocmd Syntax
{syntax, e.g. c} or a :doautocmd FileType {filetype, e.g. c}.
Regardless, you don't really need to know under what conditions it was
triggered, if you just place any additional rules you desire in the
appropriate directory (like $HOME/vimfiles/syntax or $HOME/vimfiles/
after/syntax), or use an autocmd Syntax event.
However, I would suggest a different approach, based on my own usage.
I have folding defined for #ifdef, etc. by placing the following in
$HOME/vimfiles/after/syntax/c.vim (based loosely on extra folding for
the Vim scripting language found at
http://vim.wikia.com/wiki/Syntax_folding_of_Vim_scripts):
" fold #if...#else...#endif constructs
syn region IfFoldContainer
\ start="^\s*#\s*if\(n\?def\)\?\>"
\ end="#\s*endif\>"
\ skip=+"\%(\\"\|[^"]\)\{-}\\\@<!"\|'[^']\{-}'\|'\\''\|//.*+
\ transparent
\ keepend extend
\ containedin=NONE
\ contains=SynFoldIf,SynFoldElif,SynFoldElse
syn region SynFoldIf
\ start="^\s*#\s*if\(n\?def\)\?\>"
\ end="^\s*#\s*el\(se\|if\)\>"ms=s-1,me=s-1
\ skip=+"\%(\\"\|[^"]\)\{-}\\\@<!"\|'[^']\{-}'\|'\\''\|//.*+
\ fold transparent
\ keepend
\ contained
\ nextgroup=SynFoldElif,SynFoldElse
\ contains=TOP
syn region SynFoldElif
\ start="^\s*#\s*elif\>"
\ end="^\s*#\s*el\(se\|if\)\>"ms=s-1,me=s-1
\ skip=+"\%(\\"\|[^"]\)\{-}\\\@<!"\|'[^']\{-}'\|'\\''\|//.*+
\ fold transparent
\ keepend
\ contained
\ nextgroup=SynFoldElse
\ contains=TOP
syn region SynFoldElse
\ start="^\s*#\s*else\>"
\ end="^\s*#\s*endif\>"
\ skip=+"\%(\\"\|[^"]\)\{-}\\\@<!"\|'[^']\{-}'\|'\\''\|//.*+
\ fold transparent
\ keepend
\ contained
\ contains=TOP
Now, when I encounter a macro that I know is not defined, I can press
zc to fold away all the code that will not be compiled in.
You could extend this by creating a mapping to add the macro name
under the cursor to a file/list containing all defined macros, and
another mapping to execute zc if the current section of code is not in
your list.
You could extend this further by automatically running your close-a-
fold command on every #if, #else, or #elif in your code using a Syntax
autocmd event and the :g command (assuming you like to have all folds
open when you load the file). You could also set up a mapping to do
this at will instead.
The close-a-fold command would do something like this, on the current
line:
1. Look for #ifdef {something not in your list} and close it
2. Look for #ifndef {something in your list} and close it
3. Look for #if defined({something not in your list}) or #elif defined
({something not in your list}) (with potential ||, && logic that you
will need to parse) and close it
4. Look for #if !defined({something in your list}) or #elif !defined
({something in your list}) (with potential ||, && logic that you will
need to parse) and close it
5. Look for #else and use the matchit plugin to look for the above
cases, and close it if required
--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php