1" Vim completion script 2" Language: All languages, uses existing syntax highlighting rules 3" Maintainer: David Fishburn <[email protected]> 4" Version: 1.0 5" Last Change: Sun Jan 08 2006 10:17:51 PM 6 7" Set completion with CTRL-X CTRL-O to autoloaded function. 8if exists('&ofu') 9 setlocal ofu=syntaxcomplete#Complete 10endif 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 65 let compl_list = s:SyntaxList() 66 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 81function! s:SyntaxList() 82 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 89 " of the filetype. From my casual viewing of the vim7\sytax 90 " directory. 91 redir @l 92 silent! exec 'syntax list ' 93 redir END 94 95 let syntax_groups = @l 96 let @l = saveL 97 98 if syntax_groups =~ 'E28' 99 \ || syntax_groups =~ 'E411' 100 \ || syntax_groups =~ 'E415' 101 \ || syntax_groups =~ 'No sytax items' 102 return -1 103 endif 104 105 " Abort names - match, links to, matchgroup=, start=, contains=, contained, 106 " cluster=, nextgroup=, end= 107 let next_group_regex = '\n' . 108 \ '\zs'.&filetype.'\w\+\ze'. 109 \ '\s\+xxx\s\+'. 110 \ '\<\('. 111 \ substitute(s:syn_remove_words, ',', '\\|', 'g'). 112 \ '\)\@!' 113 let syn_list = '' 114 let index = 0 115 let index = match(syntax_groups, next_group_regex, index) 116 117 118 while index > 0 119 let group_name = matchstr( syntax_groups, '\w\+', index ) 120 121 let extra_syn_list = s:SyntaxGroupItems(group_name) 122 123 let syn_list = syn_list . extra_syn_list . "\n" 124 125 let index = index + strlen(group_name) 126 let index = match(syntax_groups, next_group_regex, index) 127 endwhile 128 129 return sort(split(syn_list)) 130endfunction 131 132function! s:SyntaxGroupItems( group_name ) 133 let saveL = @l 134 135 " Generate (based on the syntax highlight rules) a list of 136 " the Statements, functions, keywords and so on available 137 " If this needs updating, the syntax\sql.vim file should be 138 " updated 139 redir @l 140 silent! exec 'syntax list ' . a:group_name 141 redir END 142 143 if @l !~ 'E28' 144 " let syn_list = substitute( @l, '^.*xxx\s*\%(contained\s*\)\?', "", '' ) 145 let syn_list = substitute( @l, '^.*xxx\s*', "", '' ) 146 147 " We only want the words for the lines begining with 148 " containedin, but there could be other items. 149 150 " Tried to remove all lines that do not begin with contained 151 " but this does not work in all cases since you can have 152 " contained nextgroup=... 153 " So this will strip off the ending of lines with known 154 " keywords. 155 let syn_list = substitute( syn_list, '\<\('. 156 \ substitute( 157 \ escape( s:syn_remove_words, '\\/.*$^~[]') 158 \ , ',', '\\|', 'g'). 159 \ '\).\{-}\%($\|'."\n".'\)' 160 \ , "\n", 'g' ) 161 162 " Now strip off the newline + blank space + contained 163 let syn_list = substitute( syn_list, '\%(^\|\n\)\@<=\s*\<\('. 164 \ 'contained\)' 165 \ , "", 'g' ) 166 167 " There are a number of items which have non-word characters in 168 " them, *'T_F1'*. vim.vim is one such file. 169 " This will replace non-word characters with spaces. 170 let syn_list = substitute( syn_list, '[^0-9A-Za-z_ ]', ' ', 'g' ) 171 else 172 let syn_list = '' 173 endif 174 175 let @l = saveL 176 177 return syn_list 178endfunction 179 180