blob: 91f5fab8857bed27fef41296b370bc92919abb6f [file] [log] [blame]
Bram Moolenaar4770d092006-01-12 23:22:24 +00001" Vim completion script
2" Language: All languages, uses existing syntax highlighting rules
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003" Maintainer: David Fishburn <dfishburn.vim@gmail.com>
4" Version: 4.0
5" Last Change: Fri 26 Oct 2007 05:27:03 PM Eastern Daylight Time
Bram Moolenaar97e8f352006-05-06 21:40:45 +00006" Usage: For detailed help, ":help ft-syntax-omni"
Bram Moolenaar4770d092006-01-12 23:22:24 +00007
8" Set completion with CTRL-X CTRL-O to autoloaded function.
Bram Moolenaarc15ef302006-03-19 22:11:16 +00009" This check is in place in case this script is
10" sourced directly instead of using the autoload feature.
Bram Moolenaarc06ac342006-03-02 22:43:39 +000011if exists('+omnifunc')
Bram Moolenaarc15ef302006-03-19 22:11:16 +000012 " Do not set the option if already set since this
13 " results in an E117 warning.
14 if &omnifunc == ""
15 setlocal omnifunc=syntaxcomplete#Complete
16 endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000017endif
18
19if exists('g:loaded_syntax_completion')
20 finish
21endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000022let g:loaded_syntax_completion = 40
Bram Moolenaar97e8f352006-05-06 21:40:45 +000023
24" Set ignorecase to the ftplugin standard
Bram Moolenaar9964e462007-05-05 17:54:07 +000025" This is the default setting, but if you define a buffer local
26" variable you can override this on a per filetype.
Bram Moolenaar97e8f352006-05-06 21:40:45 +000027if !exists('g:omni_syntax_ignorecase')
28 let g:omni_syntax_ignorecase = &ignorecase
29endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000030
Bram Moolenaar9964e462007-05-05 17:54:07 +000031" Indicates whether we should use the iskeyword option to determine
32" how to split words.
33" This is the default setting, but if you define a buffer local
34" variable you can override this on a per filetype.
35if !exists('g:omni_syntax_use_iskeyword')
36 let g:omni_syntax_use_iskeyword = 1
37endif
38
39" Only display items in the completion window that are at least
40" this many characters in length.
41" This is the default setting, but if you define a buffer local
42" variable you can override this on a per filetype.
43if !exists('g:omni_syntax_minimum_length')
44 let g:omni_syntax_minimum_length = 0
45endif
46
Bram Moolenaar4770d092006-01-12 23:22:24 +000047" This script will build a completion list based on the syntax
48" elements defined by the files in $VIMRUNTIME/syntax.
Bram Moolenaar4770d092006-01-12 23:22:24 +000049let s:syn_remove_words = 'match,matchgroup=,contains,'.
50 \ 'links to,start=,end=,nextgroup='
51
52let s:cache_name = []
53let s:cache_list = []
Bram Moolenaarc15ef302006-03-19 22:11:16 +000054let s:prepended = ''
Bram Moolenaar4770d092006-01-12 23:22:24 +000055
56" This function is used for the 'omnifunc' option.
57function! syntaxcomplete#Complete(findstart, base)
58
Bram Moolenaar9964e462007-05-05 17:54:07 +000059 " Only display items in the completion window that are at least
60 " this many characters in length
61 if !exists('b:omni_syntax_ignorecase')
62 if exists('g:omni_syntax_ignorecase')
63 let b:omni_syntax_ignorecase = g:omni_syntax_ignorecase
64 else
65 let b:omni_syntax_ignorecase = &ignorecase
66 endif
67 endif
68
Bram Moolenaar4770d092006-01-12 23:22:24 +000069 if a:findstart
70 " Locate the start of the item, including "."
71 let line = getline('.')
72 let start = col('.') - 1
73 let lastword = -1
74 while start > 0
Bram Moolenaar9964e462007-05-05 17:54:07 +000075 " if line[start - 1] =~ '\S'
76 " let start -= 1
77 " elseif line[start - 1] =~ '\.'
78 if line[start - 1] =~ '\k'
Bram Moolenaar4770d092006-01-12 23:22:24 +000079 let start -= 1
Bram Moolenaar9964e462007-05-05 17:54:07 +000080 let lastword = a:findstart
Bram Moolenaar4770d092006-01-12 23:22:24 +000081 else
82 break
83 endif
84 endwhile
85
86 " Return the column of the last word, which is going to be changed.
87 " Remember the text that comes before it in s:prepended.
88 if lastword == -1
89 let s:prepended = ''
90 return start
91 endif
Bram Moolenaar9964e462007-05-05 17:54:07 +000092 let s:prepended = strpart(line, start, (col('.') - 1) - start)
93 return start
Bram Moolenaar4770d092006-01-12 23:22:24 +000094 endif
95
Bram Moolenaar9964e462007-05-05 17:54:07 +000096 " let base = s:prepended . a:base
97 let base = s:prepended
Bram Moolenaar4770d092006-01-12 23:22:24 +000098
Bram Moolenaar97e8f352006-05-06 21:40:45 +000099 let filetype = substitute(&filetype, '\.', '_', 'g')
100 let list_idx = index(s:cache_name, filetype, 0, &ignorecase)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000101 if list_idx > -1
102 let compl_list = s:cache_list[list_idx]
103 else
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000104 let compl_list = OmniSyntaxList()
105 let s:cache_name = add( s:cache_name, filetype )
106 let s:cache_list = add( s:cache_list, compl_list )
Bram Moolenaar4770d092006-01-12 23:22:24 +0000107 endif
108
109 " Return list of matches.
110
Bram Moolenaar9964e462007-05-05 17:54:07 +0000111 if base != ''
112 " let compstr = join(compl_list, ' ')
113 " let expr = (b:omni_syntax_ignorecase==0?'\C':'').'\<\%('.base.'\)\@!\w\+\s*'
114 " let compstr = substitute(compstr, expr, '', 'g')
115 " let compl_list = split(compstr, '\s\+')
116
117 " Filter the list based on the first few characters the user
118 " entered
119 let expr = 'v:val '.(g:omni_syntax_ignorecase==1?'=~?':'=~#')." '^".escape(base, '\\/.*$^~[]').".*'"
120 let compl_list = filter(deepcopy(compl_list), expr)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000121 endif
122
123 return compl_list
124endfunc
125
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000126function! OmniSyntaxList()
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000127 " Default to returning a dictionary, if use_dictionary is set to 0
128 " a list will be returned.
129 " let use_dictionary = 1
130 " if a:0 > 0 && a:1 != ''
131 " let use_dictionary = a:1
132 " endif
133
Bram Moolenaar9964e462007-05-05 17:54:07 +0000134 " Only display items in the completion window that are at least
135 " this many characters in length
136 if !exists('b:omni_syntax_use_iskeyword')
137 if exists('g:omni_syntax_use_iskeyword')
138 let b:omni_syntax_use_iskeyword = g:omni_syntax_use_iskeyword
139 else
140 let b:omni_syntax_use_iskeyword = 1
141 endif
142 endif
143
144 " Only display items in the completion window that are at least
145 " this many characters in length
146 if !exists('b:omni_syntax_minimum_length')
147 if exists('g:omni_syntax_minimum_length')
148 let b:omni_syntax_minimum_length = g:omni_syntax_minimum_length
149 else
150 let b:omni_syntax_minimum_length = 0
151 endif
152 endif
153
Bram Moolenaar4770d092006-01-12 23:22:24 +0000154 let saveL = @l
155
156 " Loop through all the syntax groupnames, and build a
157 " syntax file which contains these names. This can
158 " work generically for any filetype that does not already
159 " have a plugin defined.
160 " This ASSUMES the syntax groupname BEGINS with the name
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000161 " of the filetype. From my casual viewing of the vim7\syntax
Bram Moolenaar4770d092006-01-12 23:22:24 +0000162 " directory.
163 redir @l
164 silent! exec 'syntax list '
165 redir END
166
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000167 let syntax_full = "\n".@l
Bram Moolenaar4770d092006-01-12 23:22:24 +0000168 let @l = saveL
169
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000170 if syntax_full =~ 'E28'
171 \ || syntax_full =~ 'E411'
172 \ || syntax_full =~ 'E415'
173 \ || syntax_full =~ 'No Syntax items'
174 return []
Bram Moolenaar4770d092006-01-12 23:22:24 +0000175 endif
176
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000177 let filetype = substitute(&filetype, '\.', '_', 'g')
178
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000179 " Default the include group to include the requested syntax group
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000180 let syntax_group_include_{filetype} = ''
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000181 " Check if there are any overrides specified for this filetype
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000182 if exists('g:omni_syntax_group_include_'.filetype)
183 let syntax_group_include_{filetype} =
184 \ substitute( g:omni_syntax_group_include_{filetype},'\s\+','','g')
185 if syntax_group_include_{filetype} =~ '\w'
186 let syntax_group_include_{filetype} =
187 \ substitute( syntax_group_include_{filetype},
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000188 \ '\s*,\s*', '\\|', 'g'
189 \ )
190 endif
191 endif
192
193 " Default the exclude group to nothing
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000194 let syntax_group_exclude_{filetype} = ''
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000195 " Check if there are any overrides specified for this filetype
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000196 if exists('g:omni_syntax_group_exclude_'.filetype)
197 let syntax_group_exclude_{filetype} =
198 \ substitute( g:omni_syntax_group_exclude_{filetype},'\s\+','','g')
199 if syntax_group_exclude_{filetype} =~ '\w'
200 let syntax_group_exclude_{filetype} =
201 \ substitute( syntax_group_exclude_{filetype},
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000202 \ '\s*,\s*', '\\|', 'g'
203 \ )
204 endif
205 endif
206
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000207 " Sometimes filetypes can be composite names, like c.doxygen
208 " Loop through each individual part looking for the syntax
209 " items specific to each individual filetype.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000210 let syn_list = ''
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000211 let ftindex = 0
212 let ftindex = match(&filetype, '\w\+', ftindex)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000213
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000214 while ftindex > -1
215 let ft_part_name = matchstr( &filetype, '\w\+', ftindex )
Bram Moolenaar4770d092006-01-12 23:22:24 +0000216
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000217 " Syntax rules can contain items for more than just the current
218 " filetype. They can contain additional items added by the user
219 " via autocmds or their vimrc.
220 " Some syntax files can be combined (html, php, jsp).
221 " We want only items that begin with the filetype we are interested in.
222 let next_group_regex = '\n' .
223 \ '\zs'.ft_part_name.'\w\+\ze'.
224 \ '\s\+xxx\s\+'
225 let index = 0
226 let index = match(syntax_full, next_group_regex, index)
227
228 while index > -1
229 let group_name = matchstr( syntax_full, '\w\+', index )
230
231 let get_syn_list = 1
232 " if syntax_group_include_{&filetype} == ''
233 " if syntax_group_exclude_{&filetype} != ''
234 " if '\<'.syntax_group_exclude_{&filetype}.'\>' =~ '\<'.group_name.'\>'
235 " let get_syn_list = 0
236 " endif
237 " endif
238 " else
239 " if '\<'.syntax_group_include_{&filetype}.'\>' !~ '\<'.group_name.'\>'
240 " let get_syn_list = 0
241 " endif
242 " endif
243 if syntax_group_exclude_{filetype} != ''
244 if '\<'.syntax_group_exclude_{filetype}.'\>' =~ '\<'.group_name.'\>'
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000245 let get_syn_list = 0
246 endif
247 endif
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000248
249 if get_syn_list == 1
250 if syntax_group_include_{filetype} != ''
251 if '\<'.syntax_group_include_{filetype}.'\>' !~ '\<'.group_name.'\>'
252 let get_syn_list = 0
253 endif
254 endif
255 endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000256
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000257 if get_syn_list == 1
258 " Pass in the full syntax listing, plus the group name we
259 " are interested in.
260 let extra_syn_list = s:SyntaxCSyntaxGroupItems(group_name, syntax_full)
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000261
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000262 " if !empty(extra_syn_list)
263 " for elem in extra_syn_list
264 " let item = {'word':elem, 'kind':'t', 'info':group_name}
265 " let compl_list += [item]
266 " endfor
267 " endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000268
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000269 let syn_list = syn_list . extra_syn_list . "\n"
270 endif
271
272 let index = index + strlen(group_name)
273 let index = match(syntax_full, next_group_regex, index)
274 endwhile
275
276 let ftindex = ftindex + len(ft_part_name)
277 let ftindex = match( &filetype, '\w\+', ftindex )
Bram Moolenaar4770d092006-01-12 23:22:24 +0000278 endwhile
279
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000280 " Convert the string to a List and sort it.
281 let compl_list = sort(split(syn_list))
282
283 if &filetype == 'vim'
284 let short_compl_list = []
285 for i in range(len(compl_list))
286 if i == len(compl_list)-1
287 let next = i
288 else
289 let next = i + 1
290 endif
291 if compl_list[next] !~ '^'.compl_list[i].'.$'
292 let short_compl_list += [compl_list[i]]
293 endif
294 endfor
295
296 return short_compl_list
297 else
298 return compl_list
299 endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000300endfunction
301
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000302function! s:SyntaxCSyntaxGroupItems( group_name, syntax_full )
Bram Moolenaar4770d092006-01-12 23:22:24 +0000303
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000304 let syn_list = ""
305
306 " From the full syntax listing, strip out the portion for the
307 " request group.
308 " Query:
309 " \n - must begin with a newline
310 " a:group_name - the group name we are interested in
311 " \s\+xxx\s\+ - group names are always followed by xxx
312 " \zs - start the match
313 " .\{-} - everything ...
314 " \ze - end the match
315 " \n\w - at the first newline starting with a character
316 let syntax_group = matchstr(a:syntax_full,
317 \ "\n".a:group_name.'\s\+xxx\s\+\zs.\{-}\ze'."\n".'\w'
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000318 \ )
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000319
320 if syntax_group != ""
Bram Moolenaar4770d092006-01-12 23:22:24 +0000321 " let syn_list = substitute( @l, '^.*xxx\s*\%(contained\s*\)\?', "", '' )
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000322 " let syn_list = substitute( @l, '^.*xxx\s*', "", '' )
Bram Moolenaar4770d092006-01-12 23:22:24 +0000323
324 " We only want the words for the lines begining with
325 " containedin, but there could be other items.
326
327 " Tried to remove all lines that do not begin with contained
328 " but this does not work in all cases since you can have
329 " contained nextgroup=...
330 " So this will strip off the ending of lines with known
331 " keywords.
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000332 let syn_list = substitute(
333 \ syntax_group, '\<\('.
334 \ substitute(
335 \ escape(s:syn_remove_words, '\\/.*$^~[]')
336 \ , ',', '\\|', 'g'
337 \ ).
338 \ '\).\{-}\%($\|'."\n".'\)'
339 \ , "\n", 'g'
340 \ )
Bram Moolenaar4770d092006-01-12 23:22:24 +0000341
342 " Now strip off the newline + blank space + contained
Bram Moolenaar97e8f352006-05-06 21:40:45 +0000343 let syn_list = substitute(
344 \ syn_list, '\%(^\|\n\)\@<=\s*\<\(contained\)'
345 \ , "", 'g'
346 \ )
Bram Moolenaar4770d092006-01-12 23:22:24 +0000347
Bram Moolenaar9964e462007-05-05 17:54:07 +0000348 if b:omni_syntax_use_iskeyword == 0
349 " There are a number of items which have non-word characters in
350 " them, *'T_F1'*. vim.vim is one such file.
351 " This will replace non-word characters with spaces.
352 let syn_list = substitute( syn_list, '[^0-9A-Za-z_ ]', ' ', 'g' )
353 else
354 let accept_chars = ','.&iskeyword.','
355 " Remove all character ranges
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000356 " let accept_chars = substitute(accept_chars, ',[^,]\+-[^,]\+,', ',', 'g')
357 let accept_chars = substitute(accept_chars, ',\@<=[^,]\+-[^,]\+,', '', 'g')
Bram Moolenaar9964e462007-05-05 17:54:07 +0000358 " Remove all numeric specifications
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000359 " let accept_chars = substitute(accept_chars, ',\d\{-},', ',', 'g')
360 let accept_chars = substitute(accept_chars, ',\@<=\d\{-},', '', 'g')
Bram Moolenaar9964e462007-05-05 17:54:07 +0000361 " Remove all commas
362 let accept_chars = substitute(accept_chars, ',', '', 'g')
363 " Escape special regex characters
364 let accept_chars = escape(accept_chars, '\\/.*$^~[]' )
365 " Remove all characters that are not acceptable
366 let syn_list = substitute( syn_list, '[^0-9A-Za-z_ '.accept_chars.']', ' ', 'g' )
367 endif
368
369 if b:omni_syntax_minimum_length > 0
370 " If the user specified a minimum length, enforce it
371 let syn_list = substitute(' '.syn_list.' ', ' \S\{,'.b:omni_syntax_minimum_length.'}\ze ', ' ', 'g')
372 endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000373 else
374 let syn_list = ''
375 endif
376
Bram Moolenaar4770d092006-01-12 23:22:24 +0000377 return syn_list
378endfunction