comparison runtime/ftplugin/sql.vim @ 718:7b21554be7a1

updated for version 7.0219
author vimboss
date Thu, 09 Mar 2006 22:27:48 +0000
parents c93c9cad9618
children 1f929f3ca806
comparison
equal deleted inserted replaced
717:2fa8cb05b861 718:7b21554be7a1
1 " SQL filetype plugin file 1 " SQL filetype plugin file
2 " Language: SQL (Common for Oracle, Microsoft SQL Server, Sybase) 2 " Language: SQL (Common for Oracle, Microsoft SQL Server, Sybase)
3 " Version: 0.08 3 " Version: 1.0
4 " Maintainer: David Fishburn <fishburn at ianywhere dot com> 4 " Maintainer: David Fishburn <fishburn at ianywhere dot com>
5 " Last Change: Mon Feb 21 2005 7:27:36 AM 5 " Last Change: Wed Jan 11 2006 10:04:55 AM
6 " Download: http://vim.sourceforge.net/script.php?script_id=454 6 " Download: http://vim.sourceforge.net/script.php?script_id=454
7 7
8 " For more details please use:
9 " :h sql.txt
10 "
8 " This file should only contain values that are common to all SQL languages 11 " This file should only contain values that are common to all SQL languages
9 " Oracle, Microsoft SQL Server, Sybase ASA/ASE, MySQL, and so on 12 " Oracle, Microsoft SQL Server, Sybase ASA/ASE, MySQL, and so on
10 " If additional features are required create: 13 " If additional features are required create:
11 " vimfiles/after/ftplugin/sql.vim (Windows) 14 " vimfiles/after/ftplugin/sql.vim (Windows)
12 " .vim/after/ftplugin/sql.vim (Unix) 15 " .vim/after/ftplugin/sql.vim (Unix)
13 " to override and add any of your own settings. 16 " to override and add any of your own settings.
14 17
18
19 " This file also creates a command, SQLSetType, which allows you to change
20 " SQL dialects on the fly. For example, if I open an Oracle SQL file, it
21 " is color highlighted appropriately. If I open an Informix SQL file, it
22 " will still be highlighted according to Oracles settings. By running:
23 " :SQLSetType sqlinformix
24 "
25 " All files called sqlinformix.vim will be loaded from the indent and syntax
26 " directories. This allows you to easily flip SQL dialects on a per file
27 " basis. NOTE: you can also use completion:
28 " :SQLSetType <tab>
29 "
30 " To change the default dialect, add the following to your vimrc:
31 " let g:sql_type_default = 'sqlanywhere'
32
33
15 " Only do this when not done yet for this buffer 34 " Only do this when not done yet for this buffer
16 if exists("b:did_ftplugin") 35 if exists("b:did_ftplugin")
17 finish 36 finish
18 endif 37 endif
19 38
20 let s:save_cpo = &cpo 39 let s:save_cpo = &cpo
21 set cpo= 40 set cpo=
22 41
42 " Functions/Commands to allow the user to change SQL syntax dialects
43 " through the use of :SQLSetType <tab> for completion.
44 " This works with both Vim 6 and 7.
45
46 if !exists("*SQL_SetType")
47 " NOTE: You cannot use function! since this file can be
48 " sourced from within this function. That will result in
49 " an error reported by Vim.
50 function SQL_GetList(ArgLead, CmdLine, CursorPos)
51
52 if !exists('s:sql_list')
53 " Grab a list of files that contain "sql" in their names
54 let list_indent = globpath(&runtimepath, 'indent/*sql*')
55 let list_syntax = globpath(&runtimepath, 'syntax/*sql*')
56 let list_ftplugin = globpath(&runtimepath, 'ftplugin/*sql*')
57
58 let sqls = "\n".list_indent."\n".list_syntax."\n".list_ftplugin."\n"
59
60 " Strip out everything (path info) but the filename
61 " Regex
62 " From between two newline characters
63 " Non-greedily grab all characters
64 " Followed by a valid filename \w\+\.\w\+ (sql.vim)
65 " Followed by a newline, but do not include the newline
66 "
67 " Replace it with just the filename (get rid of PATH)
68 "
69 " Recursively, since there are many filenames that contain
70 " the word SQL in the indent, syntax and ftplugin directory
71 let sqls = substitute( sqls,
72 \ '[\n]\%(.\{-}\)\(\w\+\.\w\+\)\n\@=',
73 \ '\1\n',
74 \ 'g'
75 \ )
76
77 " Remove duplicates, since sqlanywhere.vim can exist in the
78 " sytax, indent and ftplugin directory, yet we only want
79 " to display the option once
80 let index = match(sqls, '.\{-}\ze\n')
81 while index > -1
82 " Get the first filename
83 let file = matchstr(sqls, '.\{-}\ze\n', index)
84 " Recursively replace any *other* occurrence of that
85 " filename with nothing (ie remove it)
86 let sqls = substitute(sqls, '\%>'.(index+strlen(file)).'c\<'.file.'\>\n', '', 'g')
87 " Move on to the next filename
88 let index = match(sqls, '.\{-}\ze\n', (index+strlen(file)+1))
89 endwhile
90
91 " Sort the list if using version 7
92 if v:version >= 700
93 let mylist = split(sqls, "\n")
94 let mylist = sort(mylist)
95 let sqls = join(mylist, "\n")
96 endif
97
98 let s:sql_list = sqls
99 endif
100
101 return s:sql_list
102
103 endfunction
104
105 function SQL_SetType(name)
106
107 " User has decided to override default SQL scripts and
108 " specify a vendor specific version
109 " (ie Oracle, Informix, SQL Anywhere, ...)
110 " So check for an remove any settings that prevent the
111 " scripts from being executed, and then source the
112 " appropriate Vim scripts.
113 if exists("b:did_ftplugin")
114 unlet b:did_ftplugin
115 endif
116 if exists("b:current_syntax")
117 " echomsg 'SQLSetType - clearing syntax'
118 syntax clear
119 endif
120 if exists("b:did_indent")
121 " echomsg 'SQLSetType - clearing indent'
122 unlet b:did_indent
123 " Set these values to their defaults
124 setlocal indentkeys&
125 setlocal indentexpr&
126 endif
127
128 " Ensure the name is in the correct format
129 let new_sql_type = substitute(a:name,
130 \ '\s*\([^\.]\+\)\(\.\w\+\)\?', '\L\1', '')
131
132 " Do not specify a buffer local variable if it is
133 " the default value
134 if new_sql_type == 'sql'
135 let new_sql_type = 'sqloracle'
136 endif
137 let b:sql_type_override = new_sql_type
138
139 " Vim will automatically source the correct files if we
140 " change the filetype. You cannot do this with setfiletype
141 " since that command will only execute if a filetype has
142 " not already been set. In this case we want to override
143 " the existing filetype.
144 let &filetype = 'sql'
145 endfunction
146 command! -nargs=* -complete=custom,SQL_GetList SQLSetType :call SQL_SetType(<q-args>)
147
148 endif
149
150 if exists("b:sql_type_override")
151 " echo 'sourcing buffer ftplugin/'.b:sql_type_override.'.vim'
152 if globpath(&runtimepath, 'ftplugin/'.b:sql_type_override.'.vim') != ''
153 exec 'runtime ftplugin/'.b:sql_type_override.'.vim'
154 " else
155 " echomsg 'ftplugin/'.b:sql_type_override.' not exist, using default'
156 endif
157 elseif exists("g:sql_type_default")
158 " echo 'sourcing global ftplugin/'.g:sql_type_default.'.vim'
159 if globpath(&runtimepath, 'ftplugin/'.g:sql_type_default.'.vim') != ''
160 exec 'runtime ftplugin/'.g:sql_type_default.'.vim'
161 " else
162 " echomsg 'ftplugin/'.g:sql_type_default.'.vim not exist, using default'
163 endif
164 endif
165
166 " If the above runtime command succeeded, do not load the default settings
167 if exists("b:did_ftplugin")
168 finish
169 endif
170
171 let b:undo_ftplugin = "setl comments<"
172
23 " Don't load another plugin for this buffer 173 " Don't load another plugin for this buffer
24 let b:did_ftplugin = 1 174 let b:did_ftplugin = 1
175 let b:current_ftplugin = 'sql'
176
177 " Win32 can filter files in the browse dialog
178 if has("gui_win32") && !exists("b:browsefilter")
179 let b:browsefilter = "SQL Files (*.sql)\t*.sql\n" .
180 \ "All Files (*.*)\t*.*\n"
181 endif
25 182
26 " Some standard expressions for use with the matchit strings 183 " Some standard expressions for use with the matchit strings
27 let s:notend = '\%(\<end\s\+\)\@<!' 184 let s:notend = '\%(\<end\s\+\)\@<!'
28 let s:when_no_matched_or_others = '\%(\<when\>\%(\s\+\%(\%(\<not\>\s\+\)\?<matched\>\)\|\<others\>\)\@!\)' 185 let s:when_no_matched_or_others = '\%(\<when\>\%(\s\+\%(\%(\<not\>\s\+\)\?<matched\>\)\|\<others\>\)\@!\)'
29 let s:or_replace = '\%(or\s\+replace\s\+\)\?' 186 let s:or_replace = '\%(or\s\+replace\s\+\)\?'
110 267
111 " Define how to find the macro definition of a variable using the various 268 " Define how to find the macro definition of a variable using the various
112 " [d, [D, [_CTRL_D and so on features 269 " [d, [D, [_CTRL_D and so on features
113 " Match these values ignoring case 270 " Match these values ignoring case
114 " ie DECLARE varname INTEGER 271 " ie DECLARE varname INTEGER
115 let &l:define = '\c\(DECLARE\|IN\|OUT\|INOUT\)\s*' 272 let &l:define = '\c\<\(VARIABLE\|DECLARE\|IN\|OUT\|INOUT\)\>'
116 273
117 274
118 " Mappings to move to the next BEGIN ... END block 275 " Mappings to move to the next BEGIN ... END block
119 " \W - no characters or digits 276 " \W - no characters or digits
120 nmap <buffer> <silent> ]] :call search('\c^\s*begin\>', 'W' )<CR> 277 nmap <buffer> <silent> ]] :call search('\\c^\\s*begin\\>', 'W' )<CR>
121 nmap <buffer> <silent> [[ :call search('\c^\s*begin\>', 'bW' )<CR> 278 nmap <buffer> <silent> [[ :call search('\\c^\\s*begin\\>', 'bW' )<CR>
122 nmap <buffer> <silent> ][ :call search('\c^\s*end\W*$', 'W' )<CR> 279 nmap <buffer> <silent> ][ :call search('\\c^\\s*end\\W*$', 'W' )<CR>
123 nmap <buffer> <silent> [] :call search('\c^\s*end\W*$', 'bW' )<CR> 280 nmap <buffer> <silent> [] :call search('\\c^\\s*end\\W*$', 'bW' )<CR>
124 vmap <buffer> <silent> ]] /\c^\s*begin\><CR> 281 vmap <buffer> <silent> ]] /\\c^\\s*begin\\><CR>
125 vmap <buffer> <silent> [[ ?\c^\s*begin<CR> 282 vmap <buffer> <silent> [[ ?\\c^\\s*begin\\><CR>
126 vmap <buffer> <silent> ][ /\c^\s*end\W*$<CR> 283 vmap <buffer> <silent> ][ /\\c^\\s*end\\W*$<CR>
127 vmap <buffer> <silent> [] ?\c^\s*end\W*$<CR> 284 vmap <buffer> <silent> [] ?\\c^\\s*end\\W*$<CR>
128 285
286
287 " By default only look for CREATE statements, but allow
288 " the user to override
289 if !exists('g:ftplugin_sql_statements')
290 let g:ftplugin_sql_statements = 'create'
291 endif
129 292
130 " Predefined SQL objects what are used by the below mappings using 293 " Predefined SQL objects what are used by the below mappings using
131 " the ]} style maps. 294 " the ]} style maps.
132 " This global variable allows the users to override it's value 295 " This global variable allows the users to override it's value
133 " from within their vimrc. 296 " from within their vimrc.
297 " Note, you cannot use \?, since these patterns can be used to search
298 " backwards, you must use \{,1}
134 if !exists('g:ftplugin_sql_objects') 299 if !exists('g:ftplugin_sql_objects')
135 let g:ftplugin_sql_objects = 'function,procedure,event,' . 300 let g:ftplugin_sql_objects = 'function,procedure,event,' .
136 \ '\(existing\\|global\s\+temporary\s\+\)\?table,trigger' . 301 \ '\\(existing\\\\|global\\s\\+temporary\\s\\+\\)\\\{,1}' .
302 \ 'table,trigger' .
137 \ ',schema,service,publication,database,datatype,domain' . 303 \ ',schema,service,publication,database,datatype,domain' .
138 \ ',index,subscription,synchronization,view,variable' 304 \ ',index,subscription,synchronization,view,variable'
139 endif 305 endif
140 306
307 " Replace all ,'s with bars, except ones with numbers after them.
308 " This will most likely be a \{,1} string.
141 let s:ftplugin_sql_objects = 309 let s:ftplugin_sql_objects =
142 \ '\c^\s*' . 310 \ '\\c^\\s*' .
143 \ '\(create\s\+\(or\s\+replace\s\+\)\?\)\?' . 311 \ '\\(\\(' .
144 \ '\<\(' . 312 \ substitute(g:ftplugin_sql_statements, ',\d\@!', '\\\\\\|', 'g') .
145 \ substitute(g:ftplugin_sql_objects, ',', '\\\\|', 'g') . 313 \ '\\)\\s\\+\\(or\\s\\+replace\\\s\+\\)\\{,1}\\)\\{,1}' .
146 \ '\)\>' 314 \ '\\<\\(' .
315 \ substitute(g:ftplugin_sql_objects, ',\d\@!', '\\\\\\|', 'g') .
316 \ '\\)\\>'
147 317
148 " Mappings to move to the next CREATE ... block 318 " Mappings to move to the next CREATE ... block
149 " map <buffer> <silent> ]} :call search(g:ftplugin_sql_objects, 'W' )<CR>
150 " nmap <buffer> <silent> [{ :call search('\c^\s*\(create\s\+\(or\s\+replace\s\+\)\?\)\?\<\(function\\|procedure\\|event\\|table\\|trigger\\|schema\)\>', 'bW' )<CR>
151 " exec 'nmap <buffer> <silent> ]} /'.s:ftplugin_sql_objects.'<CR>'
152 exec "nmap <buffer> <silent> ]} :call search('".s:ftplugin_sql_objects."', 'W')<CR>" 319 exec "nmap <buffer> <silent> ]} :call search('".s:ftplugin_sql_objects."', 'W')<CR>"
153 exec "nmap <buffer> <silent> [{ :call search('".s:ftplugin_sql_objects."', 'bW')<CR>" 320 exec "nmap <buffer> <silent> [{ :call search('".s:ftplugin_sql_objects."', 'bW')<CR>"
154 " Could not figure out how to use a :call search() string in visual mode 321 " Could not figure out how to use a :call search() string in visual mode
155 " without it ending visual mode 322 " without it ending visual mode
323 " Unfortunately, this will add a entry to the search history
156 exec 'vmap <buffer> <silent> ]} /'.s:ftplugin_sql_objects.'<CR>' 324 exec 'vmap <buffer> <silent> ]} /'.s:ftplugin_sql_objects.'<CR>'
157 exec 'vmap <buffer> <silent> [{ ?'.s:ftplugin_sql_objects.'<CR>' 325 exec 'vmap <buffer> <silent> [{ ?'.s:ftplugin_sql_objects.'<CR>'
158 " vmap <buffer> <silent> ]} /\c^\s*\(create\s\+\(or\s\+replace\s\+\)\?\)\?\<\(function\\|procedure\\|event\\|table\\|trigger\\|schema\)\><CR>
159 " vmap <buffer> <silent> [{ ?\c^\s*\(create\s\+\(or\s\+replace\s\+\)\?\)\?\<\(function\\|procedure\\|event\\|table\\|trigger\\|schema\)\><CR>
160 326
161 " Mappings to move to the next COMMENT 327 " Mappings to move to the next COMMENT
162 " 328 "
163 " Had to double the \ for the \| separator since this has a special 329 " Had to double the \ for the \| separator since this has a special
164 " meaning on maps 330 " meaning on maps
165 let b:comment_leader = '\(--\\|\/\/\\|\*\\|\/\*\\|\*\/\)' 331 let b:comment_leader = '\\(--\\\|\\/\\/\\\|\\*\\\|\\/\\*\\\|\\*\\/\\)'
166 " Find the start of the next comment 332 " Find the start of the next comment
167 let b:comment_start = '^\(\s*'.b:comment_leader.'.*\n\)\@<!'. 333 let b:comment_start = '^\\(\\s*'.b:comment_leader.'.*\\n\\)\\@<!'.
168 \ '\(\s*'.b:comment_leader.'\)' 334 \ '\\(\\s*'.b:comment_leader.'\\)'
169 " Find the end of the previous comment 335 " Find the end of the previous comment
170 let b:comment_end = '\(^\s*'.b:comment_leader.'.*\n\)'. 336 let b:comment_end = '\\(^\\s*'.b:comment_leader.'.*\\n\\)'.
171 \ '\(^\s*'.b:comment_leader.'\)\@!' 337 \ '\\(^\\s*'.b:comment_leader.'\\)\\@!'
172 " Skip over the comment 338 " Skip over the comment
173 let b:comment_jump_over = "call search('". 339 let b:comment_jump_over = "call search('".
174 \ '^\(\s*'.b:comment_leader.'.*\n\)\@<!'. 340 \ '^\\(\\s*'.b:comment_leader.'.*\\n\\)\\@<!'.
175 \ "', 'W')" 341 \ "', 'W')"
176 let b:comment_skip_back = "call search('". 342 let b:comment_skip_back = "call search('".
177 \ '^\(\s*'.b:comment_leader.'.*\n\)\@<!'. 343 \ '^\\(\\s*'.b:comment_leader.'.*\\n\\)\\@<!'.
178 \ "', 'bW')" 344 \ "', 'bW')"
179 " Move to the start and end of comments 345 " Move to the start and end of comments
180 exec 'nnoremap <silent><buffer> ]" /'.b:comment_start.'<CR>' 346 exec 'nnoremap <silent><buffer> ]" /'.b:comment_start.'<CR>'
181 exec 'nnoremap <silent><buffer> [" /'.b:comment_end.'<CR>' 347 exec 'nnoremap <silent><buffer> [" /'.b:comment_end.'<CR>'
182 exec 'vnoremap <silent><buffer> ]" /'.b:comment_start.'<CR>' 348 exec 'vnoremap <silent><buffer> ]" /'.b:comment_start.'<CR>'
185 " Comments can be of the form: 351 " Comments can be of the form:
186 " /* 352 " /*
187 " * 353 " *
188 " */ 354 " */
189 " or 355 " or
356 " --
357 " or
190 " // 358 " //
191 " or
192 " --
193 setlocal comments=s1:/*,mb:*,ex:*/,:--,:// 359 setlocal comments=s1:/*,mb:*,ex:*/,:--,://
194 360
361 " Set completion with CTRL-X CTRL-O to autoloaded function.
362 if exists('&omnifunc')
363 " This is used by the sqlcomplete.vim plugin
364 " Source it for it's global functions
365 runtime autoload/syntaxcomplete.vim
366
367 setlocal omnifunc=sqlcomplete#Complete
368 " Prevent the intellisense plugin from loading
369 let b:sql_vis = 1
370 imap <buffer> <c-space>t <C-O>:let b:sql_compl_type='table'<CR><C-X><C-O>
371 imap <buffer> <c-space>p <C-O>:let b:sql_compl_type='procedure'<CR><C-X><C-O>
372 imap <buffer> <c-space>v <C-O>:let b:sql_compl_type='view'<CR><C-X><C-O>
373 imap <buffer> <c-space>c <C-O>:let b:sql_compl_type='column'<CR><C-X><C-O>
374 imap <buffer> <c-space>f <C-O>:let b:sql_compl_type='function'<CR><C-X><C-O>
375 imap <buffer> <c-space>o <C-O>:let b:sql_compl_type='option'<CR><C-X><C-O>
376 imap <buffer> <c-right> <C-O>:let b:sql_compl_type='column'<CR><C-X><C-O>
377 endif
378
195 let &cpo = s:save_cpo 379 let &cpo = s:save_cpo
196 380
197 " vim:sw=4:ff=unix: 381 " vim:sw=4:ff=unix:
198 382