blob: deaf7ed53e994e1e9697f8cb4f1523905a226259 [file] [log] [blame]
Bram Moolenaar4770d092006-01-12 23:22:24 +00001" Vim completion script
2" Language: All languages, uses existing syntax highlighting rules
3" Maintainer: David Fishburn <fishburn@ianywhere.com>
Bram Moolenaarc06ac342006-03-02 22:43:39 +00004" Version: 1.1
5" Last Change: Wed Mar 01 2006 9:58:14 PM
Bram Moolenaar4770d092006-01-12 23:22:24 +00006
7" Set completion with CTRL-X CTRL-O to autoloaded function.
Bram Moolenaarc06ac342006-03-02 22:43:39 +00008if exists('+omnifunc')
9 setlocal omnifunc=syntaxcomplete#Complete
Bram Moolenaar4770d092006-01-12 23:22:24 +000010endif
11
12if exists('g:loaded_syntax_completion')
13 finish
14endif
15let g:loaded_syntax_completion = 1
16
17" This script will build a completion list based on the syntax
18" elements defined by the files in $VIMRUNTIME/syntax.
19
20let s:syn_remove_words = 'match,matchgroup=,contains,'.
21 \ 'links to,start=,end=,nextgroup='
22
23let s:cache_name = []
24let s:cache_list = []
25
26" This function is used for the 'omnifunc' option.
27function! syntaxcomplete#Complete(findstart, base)
28
29 if a:findstart
30 " Locate the start of the item, including "."
31 let line = getline('.')
32 let start = col('.') - 1
33 let lastword = -1
34 while start > 0
35 if line[start - 1] =~ '\w'
36 let start -= 1
37 elseif line[start - 1] =~ '\.'
38 " The user must be specifying a column name
39 if lastword == -1
40 let lastword = start
41 endif
42 let start -= 1
43 let b:sql_compl_type = 'column'
44 else
45 break
46 endif
47 endwhile
48
49 " Return the column of the last word, which is going to be changed.
50 " Remember the text that comes before it in s:prepended.
51 if lastword == -1
52 let s:prepended = ''
53 return start
54 endif
55 let s:prepended = strpart(line, start, lastword - start)
56 return lastword
57 endif
58
59 let base = s:prepended . a:base
60
61 let list_idx = index(s:cache_name, &filetype, 0, &ignorecase)
62 if list_idx > -1
63 let compl_list = s:cache_list[list_idx]
64 else
Bram Moolenaarc06ac342006-03-02 22:43:39 +000065 let compl_list = OmniSyntaxList()
Bram Moolenaar4770d092006-01-12 23:22:24 +000066 let s:cache_name = add( s:cache_name, &filetype )
67 let s:cache_list = add( s:cache_list, compl_list )
68 endif
69
70 " Return list of matches.
71
72 if base =~ '\w'
73 let compstr = join(compl_list, ' ')
74 let compstr = substitute(compstr, '\<\%('.base.'\)\@!\w\+\s*', '', 'g')
75 let compl_list = split(compstr, '\s\+')
76 endif
77
78 return compl_list
79endfunc
80
Bram Moolenaarc06ac342006-03-02 22:43:39 +000081function! OmniSyntaxList()
Bram Moolenaar4770d092006-01-12 23:22:24 +000082 let saveL = @l
83
84 " Loop through all the syntax groupnames, and build a
85 " syntax file which contains these names. This can
86 " work generically for any filetype that does not already
87 " have a plugin defined.
88 " This ASSUMES the syntax groupname BEGINS with the name
Bram Moolenaarc06ac342006-03-02 22:43:39 +000089 " of the filetype. From my casual viewing of the vim7\syntax
Bram Moolenaar4770d092006-01-12 23:22:24 +000090 " directory.
91 redir @l
92 silent! exec 'syntax list '
93 redir END
94
Bram Moolenaarc06ac342006-03-02 22:43:39 +000095 let syntax_full = "\n".@l
Bram Moolenaar4770d092006-01-12 23:22:24 +000096 let @l = saveL
97
Bram Moolenaarc06ac342006-03-02 22:43:39 +000098 if syntax_full =~ 'E28'
99 \ || syntax_full =~ 'E411'
100 \ || syntax_full =~ 'E415'
101 \ || syntax_full =~ 'No Syntax items'
102 return []
Bram Moolenaar4770d092006-01-12 23:22:24 +0000103 endif
104
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000105 " Default the include group to include the requested syntax group
106 let syntax_group_include_{&filetype} = ''
107 " Check if there are any overrides specified for this filetype
108 if exists('g:omni_syntax_group_include_'.&filetype)
109 let syntax_group_include_{&filetype} =
110 \ substitute( g:omni_syntax_group_include_{&filetype},'\s\+','','g')
111 if syntax_group_include_{&filetype} =~ '\w'
112 let syntax_group_include_{&filetype} =
113 \ substitute( syntax_group_include_{&filetype},
114 \ '\s*,\s*', '\\|', 'g'
115 \ )
116 endif
117 endif
118
119 " Default the exclude group to nothing
120 let syntax_group_exclude_{&filetype} = ''
121 " Check if there are any overrides specified for this filetype
122 if exists('g:omni_syntax_group_exclude_'.&filetype)
123 let syntax_group_exclude_{&filetype} =
124 \ substitute( g:omni_syntax_group_exclude_{&filetype},'\s\+','','g')
125 if syntax_group_exclude_{&filetype} =~ '\w'
126 let syntax_group_exclude_{&filetype} =
127 \ substitute( syntax_group_exclude_{&filetype},
128 \ '\s*,\s*', '\\|', 'g'
129 \ )
130 endif
131 endif
132
133 " Syntax rules can contain items for more than just the current
134 " filetype. They can contain additional items added by the user
135 " via autocmds or their vimrc.
136 " Some syntax files can be combined (html, php, jsp).
137 " We want only items that begin with the filetype we are interested in.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000138 let next_group_regex = '\n' .
139 \ '\zs'.&filetype.'\w\+\ze'.
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000140 \ '\s\+xxx\s\+'
Bram Moolenaar4770d092006-01-12 23:22:24 +0000141 let syn_list = ''
142 let index = 0
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000143 let index = match(syntax_full, next_group_regex, index)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000144
145 while index > 0
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000146 let group_name = matchstr( syntax_full, '\w\+', index )
Bram Moolenaar4770d092006-01-12 23:22:24 +0000147
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000148 let get_syn_list = 1
149 " if syntax_group_include_{&filetype} == ''
150 " if syntax_group_exclude_{&filetype} != ''
151 " if '\<'.syntax_group_exclude_{&filetype}.'\>' =~ '\<'.group_name.'\>'
152 " let get_syn_list = 0
153 " endif
154 " endif
155 " else
156 " if '\<'.syntax_group_include_{&filetype}.'\>' !~ '\<'.group_name.'\>'
157 " let get_syn_list = 0
158 " endif
159 " endif
160 if syntax_group_exclude_{&filetype} != ''
161 if '\<'.syntax_group_exclude_{&filetype}.'\>' =~ '\<'.group_name.'\>'
162 let get_syn_list = 0
163 endif
164 endif
165
166 if get_syn_list == 1
167 if syntax_group_include_{&filetype} != ''
168 if '\<'.syntax_group_include_{&filetype}.'\>' !~ '\<'.group_name.'\>'
169 let get_syn_list = 0
170 endif
171 endif
172 endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000173
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000174 if get_syn_list == 1
175 " Pass in the full syntax listing, plus the group name we
176 " are interested in.
177 let extra_syn_list = s:SyntaxGroupItems(group_name, syntax_full)
178
179 let syn_list = syn_list . extra_syn_list . "\n"
180 endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000181
182 let index = index + strlen(group_name)
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000183 let index = match(syntax_full, next_group_regex, index)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000184 endwhile
185
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000186 " Convert the string to a List and sort it.
187 let compl_list = sort(split(syn_list))
188
189 if &filetype == 'vim'
190 let short_compl_list = []
191 for i in range(len(compl_list))
192 if i == len(compl_list)-1
193 let next = i
194 else
195 let next = i + 1
196 endif
197 if compl_list[next] !~ '^'.compl_list[i].'.$'
198 let short_compl_list += [compl_list[i]]
199 endif
200 endfor
201
202 return short_compl_list
203 else
204 return compl_list
205 endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000206endfunction
207
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000208function! s:SyntaxGroupItems( group_name, syntax_full )
Bram Moolenaar4770d092006-01-12 23:22:24 +0000209
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000210 let syn_list = ""
211
212 " From the full syntax listing, strip out the portion for the
213 " request group.
214 " Query:
215 " \n - must begin with a newline
216 " a:group_name - the group name we are interested in
217 " \s\+xxx\s\+ - group names are always followed by xxx
218 " \zs - start the match
219 " .\{-} - everything ...
220 " \ze - end the match
221 " \n\w - at the first newline starting with a character
222 let syntax_group = matchstr(a:syntax_full,
223 \ "\n".a:group_name.'\s\+xxx\s\+\zs.\{-}\ze'."\n".'\w'
224 \)
225
226 if syntax_group != ""
Bram Moolenaar4770d092006-01-12 23:22:24 +0000227 " let syn_list = substitute( @l, '^.*xxx\s*\%(contained\s*\)\?', "", '' )
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000228 " let syn_list = substitute( @l, '^.*xxx\s*', "", '' )
Bram Moolenaar4770d092006-01-12 23:22:24 +0000229
230 " We only want the words for the lines begining with
231 " containedin, but there could be other items.
232
233 " Tried to remove all lines that do not begin with contained
234 " but this does not work in all cases since you can have
235 " contained nextgroup=...
236 " So this will strip off the ending of lines with known
237 " keywords.
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000238 let syn_list = substitute( syntax_group, '\<\('.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000239 \ substitute(
240 \ escape( s:syn_remove_words, '\\/.*$^~[]')
241 \ , ',', '\\|', 'g').
242 \ '\).\{-}\%($\|'."\n".'\)'
243 \ , "\n", 'g' )
244
245 " Now strip off the newline + blank space + contained
246 let syn_list = substitute( syn_list, '\%(^\|\n\)\@<=\s*\<\('.
247 \ 'contained\)'
248 \ , "", 'g' )
249
250 " There are a number of items which have non-word characters in
251 " them, *'T_F1'*. vim.vim is one such file.
252 " This will replace non-word characters with spaces.
253 let syn_list = substitute( syn_list, '[^0-9A-Za-z_ ]', ' ', 'g' )
254 else
255 let syn_list = ''
256 endif
257
Bram Moolenaar4770d092006-01-12 23:22:24 +0000258 return syn_list
259endfunction
260