blob: c887bd5a11fdc0253ff6e45e583600beebc2f231 [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>
4" Version: 5.0
5" Last Change: 2010 Jan 31
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
9" Version 5.0
10" When processing a list of syntax groups, the final group
11" was missed in function SyntaxCSyntaxGroupItems.
12"
Bram Moolenaar4770d092006-01-12 23:22:24 +000013" Set completion with CTRL-X CTRL-O to autoloaded function.
Bram Moolenaarc15ef302006-03-19 22:11:16 +000014" This check is in place in case this script is
15" sourced directly instead of using the autoload feature.
Bram Moolenaarc06ac342006-03-02 22:43:39 +000016if exists('+omnifunc')
Bram Moolenaarc15ef302006-03-19 22:11:16 +000017 " Do not set the option if already set since this
18 " results in an E117 warning.
19 if &omnifunc == ""
20 setlocal omnifunc=syntaxcomplete#Complete
21 endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000022endif
23
24if exists('g:loaded_syntax_completion')
25 finish
26endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000027let g:loaded_syntax_completion = 40
Bram Moolenaar97e8f352006-05-06 21:40:45 +000028
29" Set ignorecase to the ftplugin standard
Bram Moolenaar9964e462007-05-05 17:54:07 +000030" This is the default setting, but if you define a buffer local
31" variable you can override this on a per filetype.
Bram Moolenaar97e8f352006-05-06 21:40:45 +000032if !exists('g:omni_syntax_ignorecase')
33 let g:omni_syntax_ignorecase = &ignorecase
34endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000035
Bram Moolenaar9964e462007-05-05 17:54:07 +000036" Indicates whether we should use the iskeyword option to determine
37" how to split words.
38" This is the default setting, but if you define a buffer local
39" variable you can override this on a per filetype.
40if !exists('g:omni_syntax_use_iskeyword')
41 let g:omni_syntax_use_iskeyword = 1
42endif
43
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.
48if !exists('g:omni_syntax_minimum_length')
49 let g:omni_syntax_minimum_length = 0
50endif
51
Bram Moolenaar4770d092006-01-12 23:22:24 +000052" This script will build a completion list based on the syntax
53" elements defined by the files in $VIMRUNTIME/syntax.
Bram Moolenaar4770d092006-01-12 23:22:24 +000054let s:syn_remove_words = 'match,matchgroup=,contains,'.
55 \ 'links to,start=,end=,nextgroup='
56
57let s:cache_name = []
58let s:cache_list = []
Bram Moolenaarc15ef302006-03-19 22:11:16 +000059let s:prepended = ''
Bram Moolenaar4770d092006-01-12 23:22:24 +000060
61" This function is used for the 'omnifunc' option.
62function! syntaxcomplete#Complete(findstart, base)
63
Bram Moolenaar9964e462007-05-05 17:54:07 +000064 " 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
69 else
70 let b:omni_syntax_ignorecase = &ignorecase
71 endif
72 endif
73
Bram Moolenaar4770d092006-01-12 23:22:24 +000074 if a:findstart
75 " Locate the start of the item, including "."
76 let line = getline('.')
77 let start = col('.') - 1
78 let lastword = -1
79 while start > 0
Bram Moolenaar9964e462007-05-05 17:54:07 +000080 " if line[start - 1] =~ '\S'
81 " let start -= 1
82 " elseif line[start - 1] =~ '\.'
83 if line[start - 1] =~ '\k'
Bram Moolenaar4770d092006-01-12 23:22:24 +000084 let start -= 1
Bram Moolenaar9964e462007-05-05 17:54:07 +000085 let lastword = a:findstart
Bram Moolenaar4770d092006-01-12 23:22:24 +000086 else
87 break
88 endif
89 endwhile
90
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.
93 if lastword == -1
94 let s:prepended = ''
95 return start
96 endif
Bram Moolenaar9964e462007-05-05 17:54:07 +000097 let s:prepended = strpart(line, start, (col('.') - 1) - start)
98 return start
Bram Moolenaar4770d092006-01-12 23:22:24 +000099 endif
100
Bram Moolenaar9964e462007-05-05 17:54:07 +0000101 " let base = s:prepended . a:base
102 let base = s:prepended
Bram Moolenaar4770d092006-01-12 23:22:24 +0000103
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000104 let filetype = substitute(&filetype, '\.', '_', 'g')
105 let list_idx = index(s:cache_name, filetype, 0, &ignorecase)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000106 if list_idx > -1
107 let compl_list = s:cache_list[list_idx]
108 else
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000109 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 )
Bram Moolenaar4770d092006-01-12 23:22:24 +0000112 endif
113
114 " Return list of matches.
115
Bram Moolenaar9964e462007-05-05 17:54:07 +0000116 if base != ''
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\+')
121
122 " Filter the list based on the first few characters the user
123 " entered
124 let expr = 'v:val '.(g:omni_syntax_ignorecase==1?'=~?':'=~#')." '^".escape(base, '\\/.*$^~[]').".*'"
125 let compl_list = filter(deepcopy(compl_list), expr)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000126 endif
127
128 return compl_list
129endfunc
130
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000131function! OmniSyntaxList()
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000132 " 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
137 " endif
138
Bram Moolenaar9964e462007-05-05 17:54:07 +0000139 " 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
144 else
145 let b:omni_syntax_use_iskeyword = 1
146 endif
147 endif
148
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
154 else
155 let b:omni_syntax_minimum_length = 0
156 endif
157 endif
158
Bram Moolenaar4770d092006-01-12 23:22:24 +0000159 let saveL = @l
160
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
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000166 " of the filetype. From my casual viewing of the vim7\syntax
Bram Moolenaar4770d092006-01-12 23:22:24 +0000167 " directory.
168 redir @l
169 silent! exec 'syntax list '
170 redir END
171
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000172 let syntax_full = "\n".@l
Bram Moolenaar4770d092006-01-12 23:22:24 +0000173 let @l = saveL
174
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000175 if syntax_full =~ 'E28'
176 \ || syntax_full =~ 'E411'
177 \ || syntax_full =~ 'E415'
178 \ || syntax_full =~ 'No Syntax items'
179 return []
Bram Moolenaar4770d092006-01-12 23:22:24 +0000180 endif
181
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000182 let filetype = substitute(&filetype, '\.', '_', 'g')
183
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000184 " Default the include group to include the requested syntax group
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000185 let syntax_group_include_{filetype} = ''
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000186 " Check if there are any overrides specified for this filetype
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000187 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},
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000193 \ '\s*,\s*', '\\|', 'g'
194 \ )
195 endif
196 endif
197
198 " Default the exclude group to nothing
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000199 let syntax_group_exclude_{filetype} = ''
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000200 " Check if there are any overrides specified for this filetype
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000201 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},
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000207 \ '\s*,\s*', '\\|', 'g'
208 \ )
209 endif
210 endif
211
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000212 " 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.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000215 let syn_list = ''
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000216 let ftindex = 0
217 let ftindex = match(&filetype, '\w\+', ftindex)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000218
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000219 while ftindex > -1
220 let ft_part_name = matchstr( &filetype, '\w\+', ftindex )
Bram Moolenaar4770d092006-01-12 23:22:24 +0000221
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000222 " 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'.
229 \ '\s\+xxx\s\+'
230 let index = 0
231 let index = match(syntax_full, next_group_regex, index)
232
233 while index > -1
234 let group_name = matchstr( syntax_full, '\w\+', index )
235
236 let get_syn_list = 1
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
241 " endif
242 " endif
243 " else
244 " if '\<'.syntax_group_include_{&filetype}.'\>' !~ '\<'.group_name.'\>'
245 " let get_syn_list = 0
246 " endif
247 " endif
248 if syntax_group_exclude_{filetype} != ''
249 if '\<'.syntax_group_exclude_{filetype}.'\>' =~ '\<'.group_name.'\>'
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000250 let get_syn_list = 0
251 endif
252 endif
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000253
254 if get_syn_list == 1
255 if syntax_group_include_{filetype} != ''
256 if '\<'.syntax_group_include_{filetype}.'\>' !~ '\<'.group_name.'\>'
257 let get_syn_list = 0
258 endif
259 endif
260 endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000261
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000262 if get_syn_list == 1
263 " Pass in the full syntax listing, plus the group name we
264 " are interested in.
265 let extra_syn_list = s:SyntaxCSyntaxGroupItems(group_name, syntax_full)
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000266
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000267 " 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]
271 " endfor
272 " endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000273
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000274 let syn_list = syn_list . extra_syn_list . "\n"
275 endif
276
277 let index = index + strlen(group_name)
278 let index = match(syntax_full, next_group_regex, index)
279 endwhile
280
281 let ftindex = ftindex + len(ft_part_name)
282 let ftindex = match( &filetype, '\w\+', ftindex )
Bram Moolenaar4770d092006-01-12 23:22:24 +0000283 endwhile
284
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000285 " Convert the string to a List and sort it.
286 let compl_list = sort(split(syn_list))
287
288 if &filetype == 'vim'
289 let short_compl_list = []
290 for i in range(len(compl_list))
291 if i == len(compl_list)-1
292 let next = i
293 else
294 let next = i + 1
295 endif
296 if compl_list[next] !~ '^'.compl_list[i].'.$'
297 let short_compl_list += [compl_list[i]]
298 endif
299 endfor
300
301 return short_compl_list
302 else
303 return compl_list
304 endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000305endfunction
306
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000307function! s:SyntaxCSyntaxGroupItems( group_name, syntax_full )
Bram Moolenaar4770d092006-01-12 23:22:24 +0000308
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000309 let syn_list = ""
310
311 " From the full syntax listing, strip out the portion for the
312 " request group.
313 " Query:
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
Bram Moolenaar6dfc28b2010-02-11 14:19:15 +0100320 " \( - start a group or 2 potential matches
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000321 " \n\w - at the first newline starting with a character
Bram Moolenaar6dfc28b2010-02-11 14:19:15 +0100322 " \| - 2nd potential match
323 " \%$ - matches end of the file or string
324 " \) - end a group
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000325 let syntax_group = matchstr(a:syntax_full,
Bram Moolenaar6dfc28b2010-02-11 14:19:15 +0100326 \ "\n".a:group_name.'\s\+xxx\s\+\zs.\{-}\ze\(\n\w\|\%$\)'
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000327 \ )
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000328
329 if syntax_group != ""
Bram Moolenaar4770d092006-01-12 23:22:24 +0000330 " let syn_list = substitute( @l, '^.*xxx\s*\%(contained\s*\)\?', "", '' )
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000331 " let syn_list = substitute( @l, '^.*xxx\s*', "", '' )
Bram Moolenaar4770d092006-01-12 23:22:24 +0000332
333 " We only want the words for the lines begining with
334 " containedin, but there could be other items.
335
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
340 " keywords.
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000341 let syn_list = substitute(
342 \ syntax_group, '\<\('.
343 \ substitute(
344 \ escape(s:syn_remove_words, '\\/.*$^~[]')
345 \ , ',', '\\|', 'g'
346 \ ).
347 \ '\).\{-}\%($\|'."\n".'\)'
348 \ , "\n", 'g'
349 \ )
Bram Moolenaar4770d092006-01-12 23:22:24 +0000350
351 " Now strip off the newline + blank space + contained
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000352 let syn_list = substitute(
353 \ syn_list, '\%(^\|\n\)\@<=\s*\<\(contained\)'
354 \ , "", 'g'
355 \ )
Bram Moolenaar4770d092006-01-12 23:22:24 +0000356
Bram Moolenaar9964e462007-05-05 17:54:07 +0000357 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' )
362 else
363 let accept_chars = ','.&iskeyword.','
364 " Remove all character ranges
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000365 " let accept_chars = substitute(accept_chars, ',[^,]\+-[^,]\+,', ',', 'g')
366 let accept_chars = substitute(accept_chars, ',\@<=[^,]\+-[^,]\+,', '', 'g')
Bram Moolenaar9964e462007-05-05 17:54:07 +0000367 " Remove all numeric specifications
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000368 " let accept_chars = substitute(accept_chars, ',\d\{-},', ',', 'g')
369 let accept_chars = substitute(accept_chars, ',\@<=\d\{-},', '', 'g')
Bram Moolenaar9964e462007-05-05 17:54:07 +0000370 " Remove all commas
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' )
376 endif
377
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')
381 endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000382 else
383 let syn_list = ''
384 endif
385
Bram Moolenaar4770d092006-01-12 23:22:24 +0000386 return syn_list
387endfunction