I have a syntax file for CSV files. To be able setting the separator, there is a function ChangeChar()
and the syn match ...
terms are variable.
To initialize it ;
is used. But to call the SetMatches()
function, it must be declared first. Is it possible to declare functions at the top of the syntax file to let it clearly arranged?
In some programming languages this is possible, in others not needed.
Calling my file there comes
error executing "/home/.../csv.vim"
line 21:
E117: unknown function: SetMatches
and it doesn't work. Calling set syntax=csv
again, it works, because the function now is defined, I think.
My syntax file (extract)
let g:seperator = ';'
call SetMatches() " not defined yet
function! ChangeChar()
echo "Enter separator: "
let char = getchar()
let char = nr2char(char)
let g:seperator = char
call SetMatches()
endfunction
function! SetMatches()
let g:pattern = '/[^' . g:seperator . ']*' . g:seperator . '\?/'
execute 'syn match csvField1 ' . g:pattern . ' display nextgroup=csvField2'
execute 'syn match csvField2 ' . g:pattern . ' display contained nextgroup=csvField3'
execute 'syn match csvField3 ' . g:pattern . ' display contained'
endfunction
hi def link csvField1 Normal
hi def link csvField2 Constant
hi def link csvField3 Special
How can I make SetMatches()
be found before declared in file?
Syntax file (complete)
" Vim syntax file
" Language: csv
if exists("b:current_syntax")
finish
endif
let s:cpo_save = &cpo
set cpo&vim
syn clear
" Syntax-Mapping
let b:syn_exec = []
let b:syn_exec = add(b:syn_exec, '')
let b:syn_exec = add(b:syn_exec, '')
let b:syn_exec = add(b:syn_exec, 'call ChangeChar()')
let g:seperator = ';'
call SetMatches()
function! ChangeChar()
echo "Enter separator: "
let char = getchar()
let char = nr2char(char)
let g:seperator = char
call SetMatches()
" TODO Bestätigung weg
endfunction
function! SetMatches()
let g:pattern = '/[^' . g:seperator . ']*' . g:seperator . '\?/'
" TODO Prüfen, ob pattern korrekt ist
execute 'syn match csvField1 ' . g:pattern . ' display nextgroup=csvField2'
execute 'syn match csvField2 ' . g:pattern . ' display contained nextgroup=csvField3'
execute 'syn match csvField3 ' . g:pattern . ' display contained nextgroup=csvField4'
execute 'syn match csvField4 ' . g:pattern . ' display contained nextgroup=csvField5'
execute 'syn match csvField5 ' . g:pattern . ' display contained nextgroup=csvField6'
execute 'syn match csvField6 ' . g:pattern . ' display contained nextgroup=csvField7'
execute 'syn match csvField7 ' . g:pattern . ' display contained'
endfunction
hi def link csvField1 Normal
hi def link csvField2 Constant
hi def link csvField3 Special
hi def link csvField4 Identifier
hi def link csvField5 Statement
hi def link csvField6 PreProc
hi def link csvField7 Type
let b:current_syntax = "csv"
let &cpo = s:cpo_save
unlet s:cpo_save