1 " Vim completion script
2 " Language: All languages, uses existing syntax highlighting rules
3 " Maintainer: David Fishburn <dfishburn dot vim at gmail dot com>
5 " Last Change: 2010 Jan 31
6 " Usage: For detailed help, ":help ft-syntax-omni"
10 " When processing a list of syntax groups, the final group
11 " was missed in function SyntaxCSyntaxGroupItems.
13 " Set completion with CTRL-X CTRL-O to autoloaded function.
14 " This check is in place in case this script is
15 " sourced directly instead of using the autoload feature.
16 if exists('+omnifunc')
17 " Do not set the option if already set since this
18 " results in an E117 warning.
20 setlocal omnifunc=syntaxcomplete#Complete
24 if exists('g:loaded_syntax_completion')
27 let g:loaded_syntax_completion = 40
29 " Set ignorecase to the ftplugin standard
30 " This is the default setting, but if you define a buffer local
31 " variable you can override this on a per filetype.
32 if !exists('g:omni_syntax_ignorecase')
33 let g:omni_syntax_ignorecase = &ignorecase
36 " Indicates whether we should use the iskeyword option to determine
38 " This is the default setting, but if you define a buffer local
39 " variable you can override this on a per filetype.
40 if !exists('g:omni_syntax_use_iskeyword')
41 let g:omni_syntax_use_iskeyword = 1
44 " Only display items in the completion window that are at least
45 " this many characters in length.
46 " This is the default setting, but if you define a buffer local
47 " variable you can override this on a per filetype.
48 if !exists('g:omni_syntax_minimum_length')
49 let g:omni_syntax_minimum_length = 0
52 " This script will build a completion list based on the syntax
53 " elements defined by the files in $VIMRUNTIME/syntax.
54 let s:syn_remove_words = 'match,matchgroup=,contains,'.
55 \ 'links to,start=,end=,nextgroup='
61 " This function is used for the 'omnifunc' option.
62 function! syntaxcomplete#Complete(findstart, base)
64 " Only display items in the completion window that are at least
65 " this many characters in length
66 if !exists('b:omni_syntax_ignorecase')
67 if exists('g:omni_syntax_ignorecase')
68 let b:omni_syntax_ignorecase = g:omni_syntax_ignorecase
70 let b:omni_syntax_ignorecase = &ignorecase
75 " Locate the start of the item, including "."
76 let line = getline('.')
77 let start = col('.') - 1
80 " if line[start - 1] =~ '\S'
82 " elseif line[start - 1] =~ '\.'
83 if line[start - 1] =~ '\k'
85 let lastword = a:findstart
91 " Return the column of the last word, which is going to be changed.
92 " Remember the text that comes before it in s:prepended.
97 let s:prepended = strpart(line, start, (col('.') - 1) - start)
101 " let base = s:prepended . a:base
102 let base = s:prepended
104 let filetype = substitute(&filetype, '\.', '_', 'g')
105 let list_idx = index(s:cache_name, filetype, 0, &ignorecase)
107 let compl_list = s:cache_list[list_idx]
109 let compl_list = OmniSyntaxList()
110 let s:cache_name = add( s:cache_name, filetype )
111 let s:cache_list = add( s:cache_list, compl_list )
114 " Return list of matches.
117 " let compstr = join(compl_list, ' ')
118 " let expr = (b:omni_syntax_ignorecase==0?'\C':'').'\<\%('.base.'\)\@!\w\+\s*'
119 " let compstr = substitute(compstr, expr, '', 'g')
120 " let compl_list = split(compstr, '\s\+')
122 " Filter the list based on the first few characters the user
124 let expr = 'v:val '.(g:omni_syntax_ignorecase==1?'=~?':'=~#')." '^".escape(base, '\\/.*$^~[]').".*'"
125 let compl_list = filter(deepcopy(compl_list), expr)
131 function! OmniSyntaxList()
132 " Default to returning a dictionary, if use_dictionary is set to 0
133 " a list will be returned.
134 " let use_dictionary = 1
135 " if a:0 > 0 && a:1 != ''
136 " let use_dictionary = a:1
139 " Only display items in the completion window that are at least
140 " this many characters in length
141 if !exists('b:omni_syntax_use_iskeyword')
142 if exists('g:omni_syntax_use_iskeyword')
143 let b:omni_syntax_use_iskeyword = g:omni_syntax_use_iskeyword
145 let b:omni_syntax_use_iskeyword = 1
149 " Only display items in the completion window that are at least
150 " this many characters in length
151 if !exists('b:omni_syntax_minimum_length')
152 if exists('g:omni_syntax_minimum_length')
153 let b:omni_syntax_minimum_length = g:omni_syntax_minimum_length
155 let b:omni_syntax_minimum_length = 0
161 " Loop through all the syntax groupnames, and build a
162 " syntax file which contains these names. This can
163 " work generically for any filetype that does not already
164 " have a plugin defined.
165 " This ASSUMES the syntax groupname BEGINS with the name
166 " of the filetype. From my casual viewing of the vim7\syntax
169 silent! exec 'syntax list '
172 let syntax_full = "\n".@l
175 if syntax_full =~ 'E28'
176 \ || syntax_full =~ 'E411'
177 \ || syntax_full =~ 'E415'
178 \ || syntax_full =~ 'No Syntax items'
182 let filetype = substitute(&filetype, '\.', '_', 'g')
184 " Default the include group to include the requested syntax group
185 let syntax_group_include_{filetype} = ''
186 " Check if there are any overrides specified for this filetype
187 if exists('g:omni_syntax_group_include_'.filetype)
188 let syntax_group_include_{filetype} =
189 \ substitute( g:omni_syntax_group_include_{filetype},'\s\+','','g')
190 if syntax_group_include_{filetype} =~ '\w'
191 let syntax_group_include_{filetype} =
192 \ substitute( syntax_group_include_{filetype},
193 \ '\s*,\s*', '\\|', 'g'
198 " Default the exclude group to nothing
199 let syntax_group_exclude_{filetype} = ''
200 " Check if there are any overrides specified for this filetype
201 if exists('g:omni_syntax_group_exclude_'.filetype)
202 let syntax_group_exclude_{filetype} =
203 \ substitute( g:omni_syntax_group_exclude_{filetype},'\s\+','','g')
204 if syntax_group_exclude_{filetype} =~ '\w'
205 let syntax_group_exclude_{filetype} =
206 \ substitute( syntax_group_exclude_{filetype},
207 \ '\s*,\s*', '\\|', 'g'
212 " Sometimes filetypes can be composite names, like c.doxygen
213 " Loop through each individual part looking for the syntax
214 " items specific to each individual filetype.
217 let ftindex = match(&filetype, '\w\+', ftindex)
220 let ft_part_name = matchstr( &filetype, '\w\+', ftindex )
222 " Syntax rules can contain items for more than just the current
223 " filetype. They can contain additional items added by the user
224 " via autocmds or their vimrc.
225 " Some syntax files can be combined (html, php, jsp).
226 " We want only items that begin with the filetype we are interested in.
227 let next_group_regex = '\n' .
228 \ '\zs'.ft_part_name.'\w\+\ze'.
231 let index = match(syntax_full, next_group_regex, index)
234 let group_name = matchstr( syntax_full, '\w\+', index )
237 " if syntax_group_include_{&filetype} == ''
238 " if syntax_group_exclude_{&filetype} != ''
239 " if '\<'.syntax_group_exclude_{&filetype}.'\>' =~ '\<'.group_name.'\>'
240 " let get_syn_list = 0
244 " if '\<'.syntax_group_include_{&filetype}.'\>' !~ '\<'.group_name.'\>'
245 " let get_syn_list = 0
248 if syntax_group_exclude_{filetype} != ''
249 if '\<'.syntax_group_exclude_{filetype}.'\>' =~ '\<'.group_name.'\>'
255 if syntax_group_include_{filetype} != ''
256 if '\<'.syntax_group_include_{filetype}.'\>' !~ '\<'.group_name.'\>'
263 " Pass in the full syntax listing, plus the group name we
265 let extra_syn_list = s:SyntaxCSyntaxGroupItems(group_name, syntax_full)
267 " if !empty(extra_syn_list)
268 " for elem in extra_syn_list
269 " let item = {'word':elem, 'kind':'t', 'info':group_name}
270 " let compl_list += [item]
274 let syn_list = syn_list . extra_syn_list . "\n"
277 let index = index + strlen(group_name)
278 let index = match(syntax_full, next_group_regex, index)
281 let ftindex = ftindex + len(ft_part_name)
282 let ftindex = match( &filetype, '\w\+', ftindex )
285 " Convert the string to a List and sort it.
286 let compl_list = sort(split(syn_list))
288 if &filetype == 'vim'
289 let short_compl_list = []
290 for i in range(len(compl_list))
291 if i == len(compl_list)-1
296 if compl_list[next] !~ '^'.compl_list[i].'.$'
297 let short_compl_list += [compl_list[i]]
301 return short_compl_list
307 function! s:SyntaxCSyntaxGroupItems( group_name, syntax_full )
311 " From the full syntax listing, strip out the portion for the
314 " \n - must begin with a newline
315 " a:group_name - the group name we are interested in
316 " \s\+xxx\s\+ - group names are always followed by xxx
317 " \zs - start the match
318 " .\{-} - everything ...
319 " \ze - end the match
320 " \( - start a group or 2 potential matches
321 " \n\w - at the first newline starting with a character
322 " \| - 2nd potential match
323 " \%$ - matches end of the file or string
325 let syntax_group = matchstr(a:syntax_full,
326 \ "\n".a:group_name.'\s\+xxx\s\+\zs.\{-}\ze\(\n\w\|\%$\)'
329 if syntax_group != ""
330 " let syn_list = substitute( @l, '^.*xxx\s*\%(contained\s*\)\?', "", '' )
331 " let syn_list = substitute( @l, '^.*xxx\s*', "", '' )
333 " We only want the words for the lines begining with
334 " containedin, but there could be other items.
336 " Tried to remove all lines that do not begin with contained
337 " but this does not work in all cases since you can have
338 " contained nextgroup=...
339 " So this will strip off the ending of lines with known
341 let syn_list = substitute(
342 \ syntax_group, '\<\('.
344 \ escape(s:syn_remove_words, '\\/.*$^~[]')
347 \ '\).\{-}\%($\|'."\n".'\)'
351 " Now strip off the newline + blank space + contained
352 let syn_list = substitute(
353 \ syn_list, '\%(^\|\n\)\@<=\s*\<\(contained\)'
357 if b:omni_syntax_use_iskeyword == 0
358 " There are a number of items which have non-word characters in
359 " them, *'T_F1'*. vim.vim is one such file.
360 " This will replace non-word characters with spaces.
361 let syn_list = substitute( syn_list, '[^0-9A-Za-z_ ]', ' ', 'g' )
363 let accept_chars = ','.&iskeyword.','
364 " Remove all character ranges
365 " let accept_chars = substitute(accept_chars, ',[^,]\+-[^,]\+,', ',', 'g')
366 let accept_chars = substitute(accept_chars, ',\@<=[^,]\+-[^,]\+,', '', 'g')
367 " Remove all numeric specifications
368 " let accept_chars = substitute(accept_chars, ',\d\{-},', ',', 'g')
369 let accept_chars = substitute(accept_chars, ',\@<=\d\{-},', '', 'g')
371 let accept_chars = substitute(accept_chars, ',', '', 'g')
372 " Escape special regex characters
373 let accept_chars = escape(accept_chars, '\\/.*$^~[]' )
374 " Remove all characters that are not acceptable
375 let syn_list = substitute( syn_list, '[^0-9A-Za-z_ '.accept_chars.']', ' ', 'g' )
378 if b:omni_syntax_minimum_length > 0
379 " If the user specified a minimum length, enforce it
380 let syn_list = substitute(' '.syn_list.' ', ' \S\{,'.b:omni_syntax_minimum_length.'}\ze ', ' ', 'g')