blob: f36c6683a61b9f0faa4c4292a1b8dccfccc494e8 [file] [log] [blame]
Bram Moolenaar4770d092006-01-12 23:22:24 +00001" Vim completion script
2" Language: All languages, uses existing syntax highlighting rules
Bram Moolenaar6dfc28b2010-02-11 14:19:15 +01003" Maintainer: David Fishburn <dfishburn dot vim at gmail dot com>
Bram Moolenaarf1568ec2011-12-14 21:17:39 +01004" Version: 8.0
5" Last Change: 2011 Nov 02
Bram Moolenaar97e8f352006-05-06 21:40:45 +00006" Usage: For detailed help, ":help ft-syntax-omni"
Bram Moolenaar4770d092006-01-12 23:22:24 +00007
Bram Moolenaar6dfc28b2010-02-11 14:19:15 +01008" History
Bram Moolenaar40af4e32010-07-29 22:33:18 +02009"
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010010" Version 8.0
11" Updated SyntaxCSyntaxGroupItems()
12" - Some additional syntax items were also allowed
13" on nextgroup= lines which were ignored by default.
14" Now these lines are processed independently.
15"
Bram Moolenaar40af4e32010-07-29 22:33:18 +020016" Version 7.0
17" Updated syntaxcomplete#OmniSyntaxList()
18" - Looking up the syntax groups defined from a syntax file
19" looked for only 1 format of {filetype}GroupName, but some
20" syntax writers use this format as well:
21" {b:current_syntax}GroupName
22" OmniSyntaxList() will now check for both if the first
23" method does not find a match.
24"
25" Version 6.0
26" Added syntaxcomplete#OmniSyntaxList()
27" - Allows other plugins to use this for their own
28" purposes.
29" - It will return a List of all syntax items for the
30" syntax group name passed in.
31" - XPTemplate for SQL will use this function via the
32" sqlcomplete plugin to populate a Choose box.
33"
Bram Moolenaar6dfc28b2010-02-11 14:19:15 +010034" Version 5.0
Bram Moolenaar40af4e32010-07-29 22:33:18 +020035" Updated SyntaxCSyntaxGroupItems()
36" - When processing a list of syntax groups, the final group
37" was missed in function SyntaxCSyntaxGroupItems.
Bram Moolenaar6dfc28b2010-02-11 14:19:15 +010038"
Bram Moolenaar4770d092006-01-12 23:22:24 +000039" Set completion with CTRL-X CTRL-O to autoloaded function.
Bram Moolenaarc15ef302006-03-19 22:11:16 +000040" This check is in place in case this script is
41" sourced directly instead of using the autoload feature.
Bram Moolenaarc06ac342006-03-02 22:43:39 +000042if exists('+omnifunc')
Bram Moolenaarc15ef302006-03-19 22:11:16 +000043 " Do not set the option if already set since this
44 " results in an E117 warning.
45 if &omnifunc == ""
46 setlocal omnifunc=syntaxcomplete#Complete
47 endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000048endif
49
50if exists('g:loaded_syntax_completion')
51 finish
52endif
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010053let g:loaded_syntax_completion = 80
Bram Moolenaar97e8f352006-05-06 21:40:45 +000054
55" Set ignorecase to the ftplugin standard
Bram Moolenaar9964e462007-05-05 17:54:07 +000056" This is the default setting, but if you define a buffer local
57" variable you can override this on a per filetype.
Bram Moolenaar97e8f352006-05-06 21:40:45 +000058if !exists('g:omni_syntax_ignorecase')
59 let g:omni_syntax_ignorecase = &ignorecase
60endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000061
Bram Moolenaar9964e462007-05-05 17:54:07 +000062" Indicates whether we should use the iskeyword option to determine
63" how to split words.
64" This is the default setting, but if you define a buffer local
65" variable you can override this on a per filetype.
66if !exists('g:omni_syntax_use_iskeyword')
67 let g:omni_syntax_use_iskeyword = 1
68endif
69
70" Only display items in the completion window that are at least
71" this many characters in length.
72" This is the default setting, but if you define a buffer local
73" variable you can override this on a per filetype.
74if !exists('g:omni_syntax_minimum_length')
75 let g:omni_syntax_minimum_length = 0
76endif
77
Bram Moolenaar4770d092006-01-12 23:22:24 +000078" This script will build a completion list based on the syntax
79" elements defined by the files in $VIMRUNTIME/syntax.
Bram Moolenaar4770d092006-01-12 23:22:24 +000080let s:syn_remove_words = 'match,matchgroup=,contains,'.
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010081 \ 'links to,start=,end='
82 " \ 'links to,start=,end=,nextgroup='
Bram Moolenaar4770d092006-01-12 23:22:24 +000083
84let s:cache_name = []
85let s:cache_list = []
Bram Moolenaarc15ef302006-03-19 22:11:16 +000086let s:prepended = ''
Bram Moolenaar4770d092006-01-12 23:22:24 +000087
88" This function is used for the 'omnifunc' option.
89function! syntaxcomplete#Complete(findstart, base)
90
Bram Moolenaar9964e462007-05-05 17:54:07 +000091 " Only display items in the completion window that are at least
92 " this many characters in length
93 if !exists('b:omni_syntax_ignorecase')
94 if exists('g:omni_syntax_ignorecase')
95 let b:omni_syntax_ignorecase = g:omni_syntax_ignorecase
96 else
97 let b:omni_syntax_ignorecase = &ignorecase
98 endif
99 endif
100
Bram Moolenaar4770d092006-01-12 23:22:24 +0000101 if a:findstart
102 " Locate the start of the item, including "."
103 let line = getline('.')
104 let start = col('.') - 1
105 let lastword = -1
106 while start > 0
Bram Moolenaar9964e462007-05-05 17:54:07 +0000107 " if line[start - 1] =~ '\S'
108 " let start -= 1
109 " elseif line[start - 1] =~ '\.'
110 if line[start - 1] =~ '\k'
Bram Moolenaar4770d092006-01-12 23:22:24 +0000111 let start -= 1
Bram Moolenaar9964e462007-05-05 17:54:07 +0000112 let lastword = a:findstart
Bram Moolenaar4770d092006-01-12 23:22:24 +0000113 else
114 break
115 endif
116 endwhile
117
118 " Return the column of the last word, which is going to be changed.
119 " Remember the text that comes before it in s:prepended.
120 if lastword == -1
121 let s:prepended = ''
122 return start
123 endif
Bram Moolenaar9964e462007-05-05 17:54:07 +0000124 let s:prepended = strpart(line, start, (col('.') - 1) - start)
125 return start
Bram Moolenaar4770d092006-01-12 23:22:24 +0000126 endif
127
Bram Moolenaar9964e462007-05-05 17:54:07 +0000128 " let base = s:prepended . a:base
129 let base = s:prepended
Bram Moolenaar4770d092006-01-12 23:22:24 +0000130
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000131 let filetype = substitute(&filetype, '\.', '_', 'g')
132 let list_idx = index(s:cache_name, filetype, 0, &ignorecase)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000133 if list_idx > -1
134 let compl_list = s:cache_list[list_idx]
135 else
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000136 let compl_list = OmniSyntaxList()
137 let s:cache_name = add( s:cache_name, filetype )
138 let s:cache_list = add( s:cache_list, compl_list )
Bram Moolenaar4770d092006-01-12 23:22:24 +0000139 endif
140
141 " Return list of matches.
142
Bram Moolenaar9964e462007-05-05 17:54:07 +0000143 if base != ''
144 " let compstr = join(compl_list, ' ')
145 " let expr = (b:omni_syntax_ignorecase==0?'\C':'').'\<\%('.base.'\)\@!\w\+\s*'
146 " let compstr = substitute(compstr, expr, '', 'g')
147 " let compl_list = split(compstr, '\s\+')
148
149 " Filter the list based on the first few characters the user
150 " entered
151 let expr = 'v:val '.(g:omni_syntax_ignorecase==1?'=~?':'=~#')." '^".escape(base, '\\/.*$^~[]').".*'"
152 let compl_list = filter(deepcopy(compl_list), expr)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000153 endif
154
155 return compl_list
156endfunc
157
Bram Moolenaar40af4e32010-07-29 22:33:18 +0200158function! syntaxcomplete#OmniSyntaxList(...)
159 if a:0 > 0
160 let parms = []
161 if 3 == type(a:1)
162 let parms = a:1
163 elseif 1 == type(a:1)
164 let parms = split(a:1, ',')
165 endif
166 return OmniSyntaxList( parms )
167 else
168 return OmniSyntaxList()
169 endif
170endfunc
171
172function! OmniSyntaxList(...)
173 let list_parms = []
174 if a:0 > 0
175 if 3 == type(a:1)
176 let list_parms = a:1
177 elseif 1 == type(a:1)
178 let list_parms = split(a:1, ',')
179 endif
180 endif
181
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000182 " Default to returning a dictionary, if use_dictionary is set to 0
183 " a list will be returned.
184 " let use_dictionary = 1
185 " if a:0 > 0 && a:1 != ''
186 " let use_dictionary = a:1
187 " endif
188
Bram Moolenaar9964e462007-05-05 17:54:07 +0000189 " Only display items in the completion window that are at least
190 " this many characters in length
191 if !exists('b:omni_syntax_use_iskeyword')
192 if exists('g:omni_syntax_use_iskeyword')
193 let b:omni_syntax_use_iskeyword = g:omni_syntax_use_iskeyword
194 else
195 let b:omni_syntax_use_iskeyword = 1
196 endif
197 endif
198
199 " Only display items in the completion window that are at least
200 " this many characters in length
201 if !exists('b:omni_syntax_minimum_length')
202 if exists('g:omni_syntax_minimum_length')
203 let b:omni_syntax_minimum_length = g:omni_syntax_minimum_length
204 else
205 let b:omni_syntax_minimum_length = 0
206 endif
207 endif
208
Bram Moolenaar4770d092006-01-12 23:22:24 +0000209 let saveL = @l
Bram Moolenaar40af4e32010-07-29 22:33:18 +0200210 let filetype = substitute(&filetype, '\.', '_', 'g')
Bram Moolenaar4770d092006-01-12 23:22:24 +0000211
Bram Moolenaar40af4e32010-07-29 22:33:18 +0200212 if empty(list_parms)
213 " Default the include group to include the requested syntax group
214 let syntax_group_include_{filetype} = ''
215 " Check if there are any overrides specified for this filetype
216 if exists('g:omni_syntax_group_include_'.filetype)
217 let syntax_group_include_{filetype} =
218 \ substitute( g:omni_syntax_group_include_{filetype},'\s\+','','g')
219 let list_parms = split(g:omni_syntax_group_include_{filetype}, ',')
220 if syntax_group_include_{filetype} =~ '\w'
221 let syntax_group_include_{filetype} =
222 \ substitute( syntax_group_include_{filetype},
223 \ '\s*,\s*', '\\|', 'g'
224 \ )
225 endif
226 endif
227 else
228 " A specific list was provided, use it
229 endif
230
Bram Moolenaar4770d092006-01-12 23:22:24 +0000231 " Loop through all the syntax groupnames, and build a
232 " syntax file which contains these names. This can
233 " work generically for any filetype that does not already
234 " have a plugin defined.
235 " This ASSUMES the syntax groupname BEGINS with the name
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000236 " of the filetype. From my casual viewing of the vim7\syntax
Bram Moolenaar40af4e32010-07-29 22:33:18 +0200237 " directory this is true for almost all syntax definitions.
238 " As an example, the SQL syntax groups have this pattern:
239 " sqlType
240 " sqlOperators
241 " sqlKeyword ...
Bram Moolenaar4770d092006-01-12 23:22:24 +0000242 redir @l
Bram Moolenaar40af4e32010-07-29 22:33:18 +0200243 silent! exec 'syntax list '.join(list_parms)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000244 redir END
245
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000246 let syntax_full = "\n".@l
Bram Moolenaar4770d092006-01-12 23:22:24 +0000247 let @l = saveL
248
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000249 if syntax_full =~ 'E28'
250 \ || syntax_full =~ 'E411'
251 \ || syntax_full =~ 'E415'
252 \ || syntax_full =~ 'No Syntax items'
253 return []
Bram Moolenaar4770d092006-01-12 23:22:24 +0000254 endif
255
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000256 let filetype = substitute(&filetype, '\.', '_', 'g')
257
Bram Moolenaar40af4e32010-07-29 22:33:18 +0200258 let list_exclude_groups = []
259 if a:0 > 0
260 " Do nothing since we have specific a specific list of groups
261 else
262 " Default the exclude group to nothing
263 let syntax_group_exclude_{filetype} = ''
264 " Check if there are any overrides specified for this filetype
265 if exists('g:omni_syntax_group_exclude_'.filetype)
266 let syntax_group_exclude_{filetype} =
267 \ substitute( g:omni_syntax_group_exclude_{filetype},'\s\+','','g')
268 let list_exclude_groups = split(g:omni_syntax_group_exclude_{filetype}, ',')
269 if syntax_group_exclude_{filetype} =~ '\w'
270 let syntax_group_exclude_{filetype} =
271 \ substitute( syntax_group_exclude_{filetype},
272 \ '\s*,\s*', '\\|', 'g'
273 \ )
274 endif
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000275 endif
276 endif
277
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000278 " Sometimes filetypes can be composite names, like c.doxygen
279 " Loop through each individual part looking for the syntax
280 " items specific to each individual filetype.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000281 let syn_list = ''
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000282 let ftindex = 0
283 let ftindex = match(&filetype, '\w\+', ftindex)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000284
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000285 while ftindex > -1
286 let ft_part_name = matchstr( &filetype, '\w\+', ftindex )
Bram Moolenaar4770d092006-01-12 23:22:24 +0000287
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000288 " Syntax rules can contain items for more than just the current
289 " filetype. They can contain additional items added by the user
290 " via autocmds or their vimrc.
291 " Some syntax files can be combined (html, php, jsp).
292 " We want only items that begin with the filetype we are interested in.
293 let next_group_regex = '\n' .
294 \ '\zs'.ft_part_name.'\w\+\ze'.
295 \ '\s\+xxx\s\+'
296 let index = 0
297 let index = match(syntax_full, next_group_regex, index)
298
Bram Moolenaar40af4e32010-07-29 22:33:18 +0200299 if index == -1 && exists('b:current_syntax') && ft_part_name != b:current_syntax
300 " There appears to be two standards when writing syntax files.
301 " Either items begin as:
302 " syn keyword {filetype}Keyword values ...
303 " let b:current_syntax = "sql"
304 " let b:current_syntax = "sqlanywhere"
305 " Or
306 " syn keyword {syntax_filename}Keyword values ...
307 " let b:current_syntax = "mysql"
308 " So, we will make the format of finding the syntax group names
309 " a bit more flexible and look for both if the first fails to
310 " find a match.
311 let next_group_regex = '\n' .
312 \ '\zs'.b:current_syntax.'\w\+\ze'.
313 \ '\s\+xxx\s\+'
314 let index = 0
315 let index = match(syntax_full, next_group_regex, index)
316 endif
317
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000318 while index > -1
319 let group_name = matchstr( syntax_full, '\w\+', index )
320
321 let get_syn_list = 1
Bram Moolenaar40af4e32010-07-29 22:33:18 +0200322 for exclude_group_name in list_exclude_groups
323 if '\<'.exclude_group_name.'\>' =~ '\<'.group_name.'\>'
324 let get_syn_list = 0
325 endif
326 endfor
327
328 " This code is no longer needed in version 6.0 since we have
329 " augmented the syntax list command to only retrieve the syntax
330 " groups we are interested in.
331 "
332 " if get_syn_list == 1
333 " if syntax_group_include_{filetype} != ''
334 " if '\<'.syntax_group_include_{filetype}.'\>' !~ '\<'.group_name.'\>'
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000335 " let get_syn_list = 0
336 " endif
337 " endif
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000338 " endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000339
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000340 if get_syn_list == 1
341 " Pass in the full syntax listing, plus the group name we
342 " are interested in.
343 let extra_syn_list = s:SyntaxCSyntaxGroupItems(group_name, syntax_full)
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000344 let syn_list = syn_list . extra_syn_list . "\n"
345 endif
346
347 let index = index + strlen(group_name)
348 let index = match(syntax_full, next_group_regex, index)
349 endwhile
350
351 let ftindex = ftindex + len(ft_part_name)
352 let ftindex = match( &filetype, '\w\+', ftindex )
Bram Moolenaar4770d092006-01-12 23:22:24 +0000353 endwhile
354
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000355 " Convert the string to a List and sort it.
356 let compl_list = sort(split(syn_list))
357
358 if &filetype == 'vim'
359 let short_compl_list = []
360 for i in range(len(compl_list))
361 if i == len(compl_list)-1
362 let next = i
363 else
364 let next = i + 1
365 endif
366 if compl_list[next] !~ '^'.compl_list[i].'.$'
367 let short_compl_list += [compl_list[i]]
368 endif
369 endfor
370
371 return short_compl_list
372 else
373 return compl_list
374 endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000375endfunction
376
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000377function! s:SyntaxCSyntaxGroupItems( group_name, syntax_full )
Bram Moolenaar4770d092006-01-12 23:22:24 +0000378
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000379 let syn_list = ""
380
381 " From the full syntax listing, strip out the portion for the
382 " request group.
383 " Query:
384 " \n - must begin with a newline
385 " a:group_name - the group name we are interested in
386 " \s\+xxx\s\+ - group names are always followed by xxx
387 " \zs - start the match
388 " .\{-} - everything ...
389 " \ze - end the match
Bram Moolenaar6dfc28b2010-02-11 14:19:15 +0100390 " \( - start a group or 2 potential matches
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000391 " \n\w - at the first newline starting with a character
Bram Moolenaar6dfc28b2010-02-11 14:19:15 +0100392 " \| - 2nd potential match
393 " \%$ - matches end of the file or string
394 " \) - end a group
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000395 let syntax_group = matchstr(a:syntax_full,
Bram Moolenaar6dfc28b2010-02-11 14:19:15 +0100396 \ "\n".a:group_name.'\s\+xxx\s\+\zs.\{-}\ze\(\n\w\|\%$\)'
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000397 \ )
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000398
399 if syntax_group != ""
Bram Moolenaar4770d092006-01-12 23:22:24 +0000400 " let syn_list = substitute( @l, '^.*xxx\s*\%(contained\s*\)\?', "", '' )
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000401 " let syn_list = substitute( @l, '^.*xxx\s*', "", '' )
Bram Moolenaar4770d092006-01-12 23:22:24 +0000402
403 " We only want the words for the lines begining with
404 " containedin, but there could be other items.
405
406 " Tried to remove all lines that do not begin with contained
407 " but this does not work in all cases since you can have
408 " contained nextgroup=...
409 " So this will strip off the ending of lines with known
410 " keywords.
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000411 let syn_list = substitute(
412 \ syntax_group, '\<\('.
413 \ substitute(
414 \ escape(s:syn_remove_words, '\\/.*$^~[]')
415 \ , ',', '\\|', 'g'
416 \ ).
417 \ '\).\{-}\%($\|'."\n".'\)'
418 \ , "\n", 'g'
419 \ )
Bram Moolenaar4770d092006-01-12 23:22:24 +0000420
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100421 " Now strip off the newline + blank space + contained.
422 " Also include lines with nextgroup=@someName skip_key_words syntax_element
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000423 let syn_list = substitute(
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100424 \ syn_list, '\%(^\|\n\)\@<=\s*\<\(contained\|nextgroup=\)'
425 \ , "", 'g'
426 \ )
427
428 " This can leave lines like this
429 " =@vimMenuList skipwhite onoremenu
430 " Strip the special option keywords first
431 " :h :syn-skipwhite*
432 let syn_list = substitute(
433 \ syn_list, '\<\(skipwhite\|skipnl\|skipempty\)\>'
434 \ , "", 'g'
435 \ )
436
437 " Now remove the remainder of the nextgroup=@someName lines
438 let syn_list = substitute(
439 \ syn_list, '\%(^\|\n\)\@<=\s*\(@\w\+\)'
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000440 \ , "", 'g'
441 \ )
Bram Moolenaar4770d092006-01-12 23:22:24 +0000442
Bram Moolenaar9964e462007-05-05 17:54:07 +0000443 if b:omni_syntax_use_iskeyword == 0
444 " There are a number of items which have non-word characters in
445 " them, *'T_F1'*. vim.vim is one such file.
446 " This will replace non-word characters with spaces.
447 let syn_list = substitute( syn_list, '[^0-9A-Za-z_ ]', ' ', 'g' )
448 else
449 let accept_chars = ','.&iskeyword.','
450 " Remove all character ranges
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000451 " let accept_chars = substitute(accept_chars, ',[^,]\+-[^,]\+,', ',', 'g')
452 let accept_chars = substitute(accept_chars, ',\@<=[^,]\+-[^,]\+,', '', 'g')
Bram Moolenaar9964e462007-05-05 17:54:07 +0000453 " Remove all numeric specifications
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000454 " let accept_chars = substitute(accept_chars, ',\d\{-},', ',', 'g')
455 let accept_chars = substitute(accept_chars, ',\@<=\d\{-},', '', 'g')
Bram Moolenaar9964e462007-05-05 17:54:07 +0000456 " Remove all commas
457 let accept_chars = substitute(accept_chars, ',', '', 'g')
458 " Escape special regex characters
459 let accept_chars = escape(accept_chars, '\\/.*$^~[]' )
460 " Remove all characters that are not acceptable
461 let syn_list = substitute( syn_list, '[^0-9A-Za-z_ '.accept_chars.']', ' ', 'g' )
462 endif
463
464 if b:omni_syntax_minimum_length > 0
465 " If the user specified a minimum length, enforce it
466 let syn_list = substitute(' '.syn_list.' ', ' \S\{,'.b:omni_syntax_minimum_length.'}\ze ', ' ', 'g')
467 endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000468 else
469 let syn_list = ''
470 endif
471
Bram Moolenaar4770d092006-01-12 23:22:24 +0000472 return syn_list
473endfunction