1" Vim completion script 2" Language: All languages, uses existing syntax highlighting rules 3" Maintainer: David Fishburn <dfishburn dot vim at gmail dot com> 4" Version: 5.0 5" Last Change: 2010 Jan 31 6" Usage: For detailed help, ":help ft-syntax-omni" 7 8" History 9" Version 5.0 10" When processing a list of syntax groups, the final group 11" was missed in function SyntaxCSyntaxGroupItems. 12" 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. 16if exists('+omnifunc') 17 " 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 22endif 23 24if exists('g:loaded_syntax_completion') 25 finish 26endif 27let g:loaded_syntax_completion = 40 28 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. 32if !exists('g:omni_syntax_ignorecase') 33 let g:omni_syntax_ignorecase = &ignorecase 34endif 35 36" 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 52" This script will build a completion list based on the syntax 53" elements defined by the files in $VIMRUNTIME/syntax. 54let s:syn_remove_words = 'match,matchgroup=,contains,'. 55 \ 'links to,start=,end=,nextgroup=' 56 57let s:cache_name = [] 58let s:cache_list = [] 59let s:prepended = '' 60 61" This function is used for the 'omnifunc' option. 62function! syntaxcomplete#Complete(findstart, base) 63 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 69 else 70 let b:omni_syntax_ignorecase = &ignorecase 71 endif 72 endif 73 74 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 80 " if line[start - 1] =~ '\S' 81 " let start -= 1 82 " elseif line[start - 1] =~ '\.' 83 if line[start - 1] =~ '\k' 84 let start -= 1 85 let lastword = a:findstart 86 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 97 let s:prepended = strpart(line, start, (col('.') - 1) - start) 98 return start 99 endif 100 101 " let base = s:prepended . a:base 102 let base = s:prepended 103 104 let filetype = substitute(&filetype, '\.', '_', 'g') 105 let list_idx = index(s:cache_name, filetype, 0, &ignorecase) 106 if list_idx > -1 107 let compl_list = s:cache_list[list_idx] 108 else 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 ) 112 endif 113 114 " Return list of matches. 115 116 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) 126 endif 127 128 return compl_list 129endfunc 130 131function! 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 137 " endif 138 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 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 159 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 166 " of the filetype. From my casual viewing of the vim7\syntax 167 " directory. 168 redir @l 169 silent! exec 'syntax list ' 170 redir END 171 172 let syntax_full = "\n".@l 173 let @l = saveL 174 175 if syntax_full =~ 'E28' 176 \ || syntax_full =~ 'E411' 177 \ || syntax_full =~ 'E415' 178 \ || syntax_full =~ 'No Syntax items' 179 return [] 180 endif 181 182 let filetype = substitute(&filetype, '\.', '_', 'g') 183 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' 194 \ ) 195 endif 196 endif 197 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' 208 \ ) 209 endif 210 endif 211 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. 215 let syn_list = '' 216 let ftindex = 0 217 let ftindex = match(&filetype, '\w\+', ftindex) 218 219 while ftindex > -1 220 let ft_part_name = matchstr( &filetype, '\w\+', ftindex ) 221 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'. 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.'\>' 250 let get_syn_list = 0 251 endif 252 endif 253 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 261 262 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) 266 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] 271 " endfor 272 " endif 273 274 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 ) 283 endwhile 284 285 " 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 305endfunction 306 307function! s:SyntaxCSyntaxGroupItems( group_name, syntax_full ) 308 309 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 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 324 " \) - end a group 325 let syntax_group = matchstr(a:syntax_full, 326 \ "\n".a:group_name.'\s\+xxx\s\+\zs.\{-}\ze\(\n\w\|\%$\)' 327 \ ) 328 329 if syntax_group != "" 330 " let syn_list = substitute( @l, '^.*xxx\s*\%(contained\s*\)\?', "", '' ) 331 " let syn_list = substitute( @l, '^.*xxx\s*', "", '' ) 332 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. 341 let syn_list = substitute( 342 \ syntax_group, '\<\('. 343 \ substitute( 344 \ escape(s:syn_remove_words, '\\/.*$^~[]') 345 \ , ',', '\\|', 'g' 346 \ ). 347 \ '\).\{-}\%($\|'."\n".'\)' 348 \ , "\n", 'g' 349 \ ) 350 351 " Now strip off the newline + blank space + contained 352 let syn_list = substitute( 353 \ syn_list, '\%(^\|\n\)\@<=\s*\<\(contained\)' 354 \ , "", 'g' 355 \ ) 356 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' ) 362 else 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') 370 " 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 382 else 383 let syn_list = '' 384 endif 385 386 return syn_list 387endfunction 388