blob: 61b7e6725515479f057566b16aa7351cf2f8156a [file] [log] [blame]
Bram Moolenaardf177f62005-02-22 08:39:57 +00001" SQL filetype plugin file
2" Language: SQL (Common for Oracle, Microsoft SQL Server, Sybase)
Bram Moolenaar036986f2017-03-16 17:41:02 +01003" Version: 12.0
Bram Moolenaaradc21822011-04-01 18:03:16 +02004" Maintainer: David Fishburn <dfishburn dot vim at gmail dot com>
Bram Moolenaar036986f2017-03-16 17:41:02 +01005" Last Change: 2017 Mar 07
Doug Kearns93197fd2024-01-14 20:59:02 +01006" 2024 Jan 14 by Vim Project (browsefilter)
Riley Bruinsdf859a32024-05-19 09:23:10 +02007" 2024 May 18 by Vim Project (set comment options)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008" Download: http://vim.sourceforge.net/script.php?script_id=454
Bram Moolenaar071d4272004-06-13 20:20:40 +00009
Bram Moolenaar0fd92892006-03-09 22:27:48 +000010" For more details please use:
11" :h sql.txt
12"
Bram Moolenaar071d4272004-06-13 20:20:40 +000013" This file should only contain values that are common to all SQL languages
14" Oracle, Microsoft SQL Server, Sybase ASA/ASE, MySQL, and so on
15" If additional features are required create:
Bram Moolenaardf177f62005-02-22 08:39:57 +000016" vimfiles/after/ftplugin/sql.vim (Windows)
17" .vim/after/ftplugin/sql.vim (Unix)
18" to override and add any of your own settings.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019
Bram Moolenaar0fd92892006-03-09 22:27:48 +000020
21" This file also creates a command, SQLSetType, which allows you to change
22" SQL dialects on the fly. For example, if I open an Oracle SQL file, it
23" is color highlighted appropriately. If I open an Informix SQL file, it
24" will still be highlighted according to Oracles settings. By running:
25" :SQLSetType sqlinformix
26"
27" All files called sqlinformix.vim will be loaded from the indent and syntax
28" directories. This allows you to easily flip SQL dialects on a per file
29" basis. NOTE: you can also use completion:
30" :SQLSetType <tab>
31"
32" To change the default dialect, add the following to your vimrc:
33" let g:sql_type_default = 'sqlanywhere'
Bram Moolenaar5c736222010-01-06 20:54:52 +010034"
Bram Moolenaar34feacb2012-12-05 19:01:43 +010035" This file also creates a command, SQLGetType, which allows you to
Bram Moolenaar5c736222010-01-06 20:54:52 +010036" determine what the current dialect is in use.
37" :SQLGetType
38"
39" History
40"
Bram Moolenaar036986f2017-03-16 17:41:02 +010041" Version 12.0 (April 2013)
42"
43" NF: Added support for "BEGIN TRY ... END TRY ... BEGIN CATCH ... END CATCH
44" BF: This plugin is designed to be used with other plugins to enable the
45" SQL completion with Perl, Python, Java, ... The loading mechanism
46" was not checking if the SQL objects were created, which can lead to
47" the plugin not loading the SQL support.
48"
Bram Moolenaarad3b3662013-05-17 18:14:19 +020049" Version 11.0 (May 2013)
50"
51" NF: Updated to use SyntaxComplete's new regex support for syntax groups.
52"
Bram Moolenaar34feacb2012-12-05 19:01:43 +010053" Version 10.0 (Dec 2012)
54"
55" NF: Changed all maps to use noremap instead of must map
56" NF: Changed all visual maps to use xnoremap instead of vnoremap as they
57" should only be used in visual mode and not select mode.
58" BF: Most of the maps were using doubled up backslashes before they were
59" changed to using the search() function, which meant they no longer
60" worked.
61"
62" Version 9.0
63"
64" NF: Completes 'b:undo_ftplugin'
65" BF: Correctly set cpoptions when creating script
66"
Bram Moolenaaradc21822011-04-01 18:03:16 +020067" Version 8.0
Bram Moolenaar34feacb2012-12-05 19:01:43 +010068"
Bram Moolenaaradc21822011-04-01 18:03:16 +020069" NF: Improved the matchit plugin regex (Talek)
70"
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +020071" Version 7.0
Bram Moolenaar34feacb2012-12-05 19:01:43 +010072"
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +020073" NF: Calls the sqlcomplete#ResetCacheSyntax() function when calling
74" SQLSetType.
75"
Bram Moolenaar5c736222010-01-06 20:54:52 +010076" Version 6.0
Bram Moolenaar34feacb2012-12-05 19:01:43 +010077"
Bram Moolenaar5c736222010-01-06 20:54:52 +010078" NF: Adds the command SQLGetType
79"
80" Version 5.0
Bram Moolenaar34feacb2012-12-05 19:01:43 +010081"
82" NF: Adds the ability to choose the keys to control SQL completion, just add
Bram Moolenaar5c736222010-01-06 20:54:52 +010083" the following to your .vimrc:
84" let g:ftplugin_sql_omni_key = '<C-C>'
85" let g:ftplugin_sql_omni_key_right = '<Right>'
86" let g:ftplugin_sql_omni_key_left = '<Left>'
87"
Bram Moolenaar34feacb2012-12-05 19:01:43 +010088" BF: format-options - Auto-wrap comments using textwidth was turned off
Bram Moolenaar5c736222010-01-06 20:54:52 +010089" by mistake.
Bram Moolenaar0fd92892006-03-09 22:27:48 +000090
91
Bram Moolenaar071d4272004-06-13 20:20:40 +000092" Only do this when not done yet for this buffer
Bram Moolenaar036986f2017-03-16 17:41:02 +010093" This ftplugin can be used with other ftplugins. So ensure loading
94" happens if all elements of this plugin have not yet loaded.
95if exists("b:did_ftplugin") && exists("b:current_ftplugin") && b:current_ftplugin == 'sql'
96 finish
Bram Moolenaar071d4272004-06-13 20:20:40 +000097endif
98
Bram Moolenaardf177f62005-02-22 08:39:57 +000099let s:save_cpo = &cpo
Bram Moolenaar8e52a592012-05-18 21:49:28 +0200100set cpo&vim
Bram Moolenaardf177f62005-02-22 08:39:57 +0000101
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000102" Disable autowrapping for code, but enable for comments
Bram Moolenaar036986f2017-03-16 17:41:02 +0100103" t Auto-wrap text using textwidth
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000104" c Auto-wrap comments using textwidth, inserting the current comment
105" leader automatically.
106setlocal formatoptions-=t
Bram Moolenaar5c736222010-01-06 20:54:52 +0100107setlocal formatoptions+=c
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000108
Riley Bruinsdf859a32024-05-19 09:23:10 +0200109setlocal comments=:-- commentstring=--\ %s
110
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000111" Functions/Commands to allow the user to change SQL syntax dialects
112" through the use of :SQLSetType <tab> for completion.
113" This works with both Vim 6 and 7.
114
115if !exists("*SQL_SetType")
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100116 " NOTE: You cannot use function! since this file can be
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000117 " sourced from within this function. That will result in
118 " an error reported by Vim.
119 function SQL_GetList(ArgLead, CmdLine, CursorPos)
120
121 if !exists('s:sql_list')
122 " Grab a list of files that contain "sql" in their names
123 let list_indent = globpath(&runtimepath, 'indent/*sql*')
124 let list_syntax = globpath(&runtimepath, 'syntax/*sql*')
125 let list_ftplugin = globpath(&runtimepath, 'ftplugin/*sql*')
126
127 let sqls = "\n".list_indent."\n".list_syntax."\n".list_ftplugin."\n"
128
129 " Strip out everything (path info) but the filename
130 " Regex
131 " From between two newline characters
132 " Non-greedily grab all characters
133 " Followed by a valid filename \w\+\.\w\+ (sql.vim)
134 " Followed by a newline, but do not include the newline
135 "
136 " Replace it with just the filename (get rid of PATH)
137 "
138 " Recursively, since there are many filenames that contain
139 " the word SQL in the indent, syntax and ftplugin directory
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100140 let sqls = substitute( sqls,
141 \ '[\n]\%(.\{-}\)\(\w\+\.\w\+\)\n\@=',
142 \ '\1\n',
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000143 \ 'g'
144 \ )
145
146 " Remove duplicates, since sqlanywhere.vim can exist in the
Viktor Szépe3fc7a7e2023-08-23 21:20:00 +0200147 " syntax, indent and ftplugin directory, yet we only want
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000148 " to display the option once
149 let index = match(sqls, '.\{-}\ze\n')
150 while index > -1
151 " Get the first filename
152 let file = matchstr(sqls, '.\{-}\ze\n', index)
153 " Recursively replace any *other* occurrence of that
154 " filename with nothing (ie remove it)
155 let sqls = substitute(sqls, '\%>'.(index+strlen(file)).'c\<'.file.'\>\n', '', 'g')
156 " Move on to the next filename
157 let index = match(sqls, '.\{-}\ze\n', (index+strlen(file)+1))
158 endwhile
159
160 " Sort the list if using version 7
161 if v:version >= 700
162 let mylist = split(sqls, "\n")
163 let mylist = sort(mylist)
164 let sqls = join(mylist, "\n")
165 endif
166
167 let s:sql_list = sqls
168 endif
169
170 return s:sql_list
171
172 endfunction
173
174 function SQL_SetType(name)
175
176 " User has decided to override default SQL scripts and
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100177 " specify a vendor specific version
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000178 " (ie Oracle, Informix, SQL Anywhere, ...)
179 " So check for an remove any settings that prevent the
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100180 " scripts from being executed, and then source the
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000181 " appropriate Vim scripts.
182 if exists("b:did_ftplugin")
183 unlet b:did_ftplugin
184 endif
185 if exists("b:current_syntax")
186 " echomsg 'SQLSetType - clearing syntax'
187 syntax clear
Bram Moolenaar036986f2017-03-16 17:41:02 +0100188 if exists("b:current_syntax")
189 unlet b:current_syntax
190 endif
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000191 endif
192 if exists("b:did_indent")
193 " echomsg 'SQLSetType - clearing indent'
194 unlet b:did_indent
195 " Set these values to their defaults
196 setlocal indentkeys&
197 setlocal indentexpr&
198 endif
199
200 " Ensure the name is in the correct format
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100201 let new_sql_type = substitute(a:name,
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000202 \ '\s*\([^\.]\+\)\(\.\w\+\)\?', '\L\1', '')
203
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100204 " Do not specify a buffer local variable if it is
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000205 " the default value
206 if new_sql_type == 'sql'
Bram Moolenaar036986f2017-03-16 17:41:02 +0100207 let new_sql_type = 'sqloracle'
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000208 endif
209 let b:sql_type_override = new_sql_type
210
Viktor Szépe3fc7a7e2023-08-23 21:20:00 +0200211 " Remove any cached SQL since a new syntax will have different
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +0200212 " items and groups
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200213 if !exists('g:loaded_sql_completion') || g:loaded_sql_completion >= 100
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +0200214 call sqlcomplete#ResetCacheSyntax()
215 endif
216
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000217 " Vim will automatically source the correct files if we
218 " change the filetype. You cannot do this with setfiletype
219 " since that command will only execute if a filetype has
220 " not already been set. In this case we want to override
221 " the existing filetype.
222 let &filetype = 'sql'
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +0200223
224 if b:sql_compl_savefunc != ""
225 " We are changing the filetype to SQL from some other filetype
226 " which had OMNI completion defined. We need to activate the
227 " SQL completion plugin in order to cache some of the syntax items
228 " while the syntax rules for SQL are active.
229 call sqlcomplete#PreCacheSyntax()
230 endif
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000231 endfunction
232 command! -nargs=* -complete=custom,SQL_GetList SQLSetType :call SQL_SetType(<q-args>)
233
234endif
235
Bram Moolenaar5c736222010-01-06 20:54:52 +0100236" Functions/Commands to allow the user determine current SQL syntax dialect
237" This works with both Vim 6 and 7.
238
239if !exists("*SQL_GetType")
240 function SQL_GetType()
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100241 if exists('b:sql_type_override')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100242 echomsg "Current SQL dialect in use:".b:sql_type_override
243 else
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100244 echomsg "Current SQL dialect in use:".g:sql_type_default
Bram Moolenaar5c736222010-01-06 20:54:52 +0100245 endif
246 endfunction
247 command! -nargs=0 SQLGetType :call SQL_GetType()
248endif
249
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000250if exists("b:sql_type_override")
251 " echo 'sourcing buffer ftplugin/'.b:sql_type_override.'.vim'
252 if globpath(&runtimepath, 'ftplugin/'.b:sql_type_override.'.vim') != ''
253 exec 'runtime ftplugin/'.b:sql_type_override.'.vim'
Bram Moolenaar036986f2017-03-16 17:41:02 +0100254 " else
255 " echomsg 'ftplugin/'.b:sql_type_override.' not exist, using default'
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000256 endif
257elseif exists("g:sql_type_default")
258 " echo 'sourcing global ftplugin/'.g:sql_type_default.'.vim'
259 if globpath(&runtimepath, 'ftplugin/'.g:sql_type_default.'.vim') != ''
260 exec 'runtime ftplugin/'.g:sql_type_default.'.vim'
Bram Moolenaar036986f2017-03-16 17:41:02 +0100261 " else
262 " echomsg 'ftplugin/'.g:sql_type_default.'.vim not exist, using default'
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000263 endif
264endif
265
266" If the above runtime command succeeded, do not load the default settings
Bram Moolenaar036986f2017-03-16 17:41:02 +0100267" as they should have already been loaded from a previous run.
268if exists("b:did_ftplugin") && exists("b:current_ftplugin") && b:current_ftplugin == 'sql'
269 finish
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000270endif
271
Riley Bruinsdf859a32024-05-19 09:23:10 +0200272let b:undo_ftplugin = "setl comments< commentstring< formatoptions< define< omnifunc<" .
Bram Moolenaar036986f2017-03-16 17:41:02 +0100273 \ " | unlet! b:browsefilter b:match_words"
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000274
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275" Don't load another plugin for this buffer
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000276let b:did_ftplugin = 1
277let b:current_ftplugin = 'sql'
278
Doug Kearns93197fd2024-01-14 20:59:02 +0100279" Win32 and GTK can filter files in the browse dialog
280if (has("gui_win32") || has("gui_gtk")) && !exists("b:browsefilter")
281 let b:browsefilter = "SQL Files (*.sql)\t*.sql\n"
282 if has("win32")
283 let b:browsefilter .= "All Files (*.*)\t*\n"
284 else
285 let b:browsefilter .= "All Files (*)\t*\n"
286 endif
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000287endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288
Bram Moolenaardf177f62005-02-22 08:39:57 +0000289" Some standard expressions for use with the matchit strings
290let s:notend = '\%(\<end\s\+\)\@<!'
291let s:when_no_matched_or_others = '\%(\<when\>\%(\s\+\%(\%(\<not\>\s\+\)\?<matched\>\)\|\<others\>\)\@!\)'
292let s:or_replace = '\%(or\s\+replace\s\+\)\?'
293
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294" Define patterns for the matchit macro
295if !exists("b:match_words")
296 " SQL is generally case insensitive
297 let b:match_ignorecase = 1
Bram Moolenaardf177f62005-02-22 08:39:57 +0000298
299 " Handle the following:
300 " if
301 " elseif | elsif
302 " else [if]
303 " end if
304 "
305 " [while condition] loop
306 " leave
307 " break
308 " continue
309 " exit
310 " end loop
311 "
312 " for
313 " leave
314 " break
315 " continue
316 " exit
317 " end loop
318 "
319 " do
320 " statements
321 " doend
322 "
323 " case
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100324 " when
Bram Moolenaardf177f62005-02-22 08:39:57 +0000325 " when
326 " default
327 " end case
328 "
329 " merge
330 " when not matched
331 " when matched
332 "
333 " EXCEPTION
334 " WHEN column_not_found THEN
335 " WHEN OTHERS THEN
336 "
Bram Moolenaar036986f2017-03-16 17:41:02 +0100337 " begin try
338 " end try
339 " begin catch
340 " end catch
341 "
Bram Moolenaardf177f62005-02-22 08:39:57 +0000342 " create[ or replace] procedure|function|event
Bram Moolenaar036986f2017-03-16 17:41:02 +0100343 " \ '^\s*\<\%(do\|for\|while\|loop\)\>.*:'.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000344
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100345 " For ColdFusion support
346 setlocal matchpairs+=<:>
347 let b:match_words = &matchpairs .
Bram Moolenaar036986f2017-03-16 17:41:02 +0100348 \ ',\%(\<begin\)\%(\s\+\%(try\|catch\)\>\)\@!:\<end\>\W*$,'.
349 \
350 \ '\<begin\s\+try\>:'.
351 \ '\<end\s\+try\>:'.
352 \ '\<begin\s\+catch\>:'.
353 \ '\<end\s\+catch\>,'.
354 \
Bram Moolenaardf177f62005-02-22 08:39:57 +0000355 \ s:notend . '\<if\>:'.
356 \ '\<elsif\>\|\<elseif\>\|\<else\>:'.
357 \ '\<end\s\+if\>,'.
358 \
Bram Moolenaaradc21822011-04-01 18:03:16 +0200359 \ '\(^\s*\)\@<=\(\<\%(do\|for\|while\|loop\)\>.*\):'.
360 \ '\%(\<exit\>\|\<leave\>\|\<break\>\|\<continue\>\):'.
361 \ '\%(\<doend\>\|\%(\<end\s\+\%(for\|while\|loop\>\)\)\),'.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000362 \
363 \ '\%('. s:notend . '\<case\>\):'.
364 \ '\%('.s:when_no_matched_or_others.'\):'.
365 \ '\%(\<when\s\+others\>\|\<end\s\+case\>\),' .
366 \
367 \ '\<merge\>:' .
368 \ '\<when\s\+not\s\+matched\>:' .
369 \ '\<when\s\+matched\>,' .
370 \
371 \ '\%(\<create\s\+' . s:or_replace . '\)\?'.
372 \ '\%(function\|procedure\|event\):'.
373 \ '\<returns\?\>'
Bram Moolenaar036986f2017-03-16 17:41:02 +0100374 " \ '\<begin\>\|\<returns\?\>:'.
375 " \ '\<end\>\(;\)\?\s*$'
376 " \ '\<exception\>:'.s:when_no_matched_or_others.
377 " \ ':\<when\s\+others\>,'.
378 "
379 " \ '\%(\<exception\>\|\%('. s:notend . '\<case\>\)\):'.
380 " \ '\%(\<default\>\|'.s:when_no_matched_or_others.'\):'.
381 " \ '\%(\%(\<when\s\+others\>\)\|\<end\s\+case\>\),' .
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382endif
Bram Moolenaardf177f62005-02-22 08:39:57 +0000383
384" Define how to find the macro definition of a variable using the various
385" [d, [D, [_CTRL_D and so on features
386" Match these values ignoring case
387" ie DECLARE varname INTEGER
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000388let &l:define = '\c\<\(VARIABLE\|DECLARE\|IN\|OUT\|INOUT\)\>'
Bram Moolenaardf177f62005-02-22 08:39:57 +0000389
390
391" Mappings to move to the next BEGIN ... END block
392" \W - no characters or digits
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100393nnoremap <buffer> <silent> ]] :call search('\c^\s*begin\>', 'W' )<CR>
394nnoremap <buffer> <silent> [[ :call search('\c^\s*begin\>', 'bW' )<CR>
395nnoremap <buffer> <silent> ][ :call search('\c^\s*end\W*$', 'W' )<CR>
396nnoremap <buffer> <silent> [] :call search('\c^\s*end\W*$', 'bW' )<CR>
397xnoremap <buffer> <silent> ]] :<C-U>exec "normal! gv"<Bar>call search('\c^\s*begin\>', 'W' )<CR>
398xnoremap <buffer> <silent> [[ :<C-U>exec "normal! gv"<Bar>call search('\c^\s*begin\>', 'bW' )<CR>
399xnoremap <buffer> <silent> ][ :<C-U>exec "normal! gv"<Bar>call search('\c^\s*end\W*$', 'W' )<CR>
400xnoremap <buffer> <silent> [] :<C-U>exec "normal! gv"<Bar>call search('\c^\s*end\W*$', 'bW' )<CR>
Bram Moolenaardf177f62005-02-22 08:39:57 +0000401
402
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000403" By default only look for CREATE statements, but allow
404" the user to override
405if !exists('g:ftplugin_sql_statements')
406 let g:ftplugin_sql_statements = 'create'
407endif
408
Bram Moolenaardf177f62005-02-22 08:39:57 +0000409" Predefined SQL objects what are used by the below mappings using
410" the ]} style maps.
Bram Moolenaara6c27c42019-05-09 19:16:22 +0200411" This global variable allows the users to override its value
Bram Moolenaardf177f62005-02-22 08:39:57 +0000412" from within their vimrc.
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000413" Note, you cannot use \?, since these patterns can be used to search
414" backwards, you must use \{,1}
Bram Moolenaardf177f62005-02-22 08:39:57 +0000415if !exists('g:ftplugin_sql_objects')
416 let g:ftplugin_sql_objects = 'function,procedure,event,' .
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100417 \ '\(existing\\|global\s\+temporary\s\+\)\{,1}' .
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000418 \ 'table,trigger' .
Bram Moolenaardf177f62005-02-22 08:39:57 +0000419 \ ',schema,service,publication,database,datatype,domain' .
420 \ ',index,subscription,synchronization,view,variable'
421endif
422
Bram Moolenaar5c736222010-01-06 20:54:52 +0100423" Key to trigger SQL completion
424if !exists('g:ftplugin_sql_omni_key')
425 let g:ftplugin_sql_omni_key = '<C-C>'
426endif
427" Key to trigger drill into column list
428if !exists('g:ftplugin_sql_omni_key_right')
429 let g:ftplugin_sql_omni_key_right = '<Right>'
430endif
431" Key to trigger drill out of column list
432if !exists('g:ftplugin_sql_omni_key_left')
433 let g:ftplugin_sql_omni_key_left = '<Left>'
434endif
435
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000436" Replace all ,'s with bars, except ones with numbers after them.
437" This will most likely be a \{,1} string.
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100438let s:ftplugin_sql_objects =
439 \ '\c^\s*' .
440 \ '\(\(' .
441 \ substitute(g:ftplugin_sql_statements, ',\d\@!', '\\\\|', 'g') .
442 \ '\)\s\+\(or\s\+replace\s\+\)\{,1}\)\{,1}' .
443 \ '\<\(' .
444 \ substitute(g:ftplugin_sql_objects, ',\d\@!', '\\\\|', 'g') .
445 \ '\)\>'
Bram Moolenaardf177f62005-02-22 08:39:57 +0000446
447" Mappings to move to the next CREATE ... block
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100448exec "nnoremap <buffer> <silent> ]} :call search('".s:ftplugin_sql_objects."', 'W')<CR>"
449exec "nnoremap <buffer> <silent> [{ :call search('".s:ftplugin_sql_objects."', 'bW')<CR>"
Bram Moolenaardf177f62005-02-22 08:39:57 +0000450" Could not figure out how to use a :call search() string in visual mode
451" without it ending visual mode
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000452" Unfortunately, this will add a entry to the search history
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100453exec 'xnoremap <buffer> <silent> ]} /'.s:ftplugin_sql_objects.'<CR>'
454exec 'xnoremap <buffer> <silent> [{ ?'.s:ftplugin_sql_objects.'<CR>'
Bram Moolenaardf177f62005-02-22 08:39:57 +0000455
456" Mappings to move to the next COMMENT
457"
458" Had to double the \ for the \| separator since this has a special
459" meaning on maps
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100460let b:comment_leader = '\(--\\|\/\/\\|\*\\|\/\*\\|\*\/\)'
Bram Moolenaardf177f62005-02-22 08:39:57 +0000461" Find the start of the next comment
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100462let b:comment_start = '^\(\s*'.b:comment_leader.'.*\n\)\@<!'.
463 \ '\(\s*'.b:comment_leader.'\)'
Bram Moolenaardf177f62005-02-22 08:39:57 +0000464" Find the end of the previous comment
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100465let b:comment_end = '\(^\s*'.b:comment_leader.'.*\n\)'.
466 \ '\(^\s*'.b:comment_leader.'\)\@!'
Bram Moolenaardf177f62005-02-22 08:39:57 +0000467" Skip over the comment
468let b:comment_jump_over = "call search('".
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100469 \ '^\(\s*'.b:comment_leader.'.*\n\)\@<!'.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000470 \ "', 'W')"
471let b:comment_skip_back = "call search('".
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100472 \ '^\(\s*'.b:comment_leader.'.*\n\)\@<!'.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000473 \ "', 'bW')"
474" Move to the start and end of comments
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000475exec 'nnoremap <silent><buffer> ]" :call search('."'".b:comment_start."'".', "W" )<CR>'
476exec 'nnoremap <silent><buffer> [" :call search('."'".b:comment_end."'".', "W" )<CR>'
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100477exec 'xnoremap <silent><buffer> ]" :<C-U>exec "normal! gv"<Bar>call search('."'".b:comment_start."'".', "W" )<CR>'
478exec 'xnoremap <silent><buffer> [" :<C-U>exec "normal! gv"<Bar>call search('."'".b:comment_end."'".', "W" )<CR>'
Bram Moolenaardf177f62005-02-22 08:39:57 +0000479
480" Comments can be of the form:
481" /*
482" *
483" */
484" or
Bram Moolenaardf177f62005-02-22 08:39:57 +0000485" --
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000486" or
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100487" //
Bram Moolenaardf177f62005-02-22 08:39:57 +0000488setlocal comments=s1:/*,mb:*,ex:*/,:--,://
489
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000490" Set completion with CTRL-X CTRL-O to autoloaded function.
491if exists('&omnifunc')
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000492 " Since the SQL completion plugin can be used in conjunction
493 " with other completion filetypes it must record the previous
494 " OMNI function prior to setting up the SQL OMNI function
495 let b:sql_compl_savefunc = &omnifunc
496
Bram Moolenaara6c27c42019-05-09 19:16:22 +0200497 " Source it to determine its version
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200498 runtime autoload/sqlcomplete.vim
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000499 " This is used by the sqlcomplete.vim plugin
Bram Moolenaara6c27c42019-05-09 19:16:22 +0200500 " Source it for its global functions
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100501 runtime autoload/syntaxcomplete.vim
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000502
503 setlocal omnifunc=sqlcomplete#Complete
504 " Prevent the intellisense plugin from loading
505 let b:sql_vis = 1
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000506 if !exists('g:omni_sql_no_default_maps')
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200507 let regex_extra = ''
508 if exists('g:loaded_syntax_completion') && exists('g:loaded_sql_completion')
509 if g:loaded_syntax_completion > 120 && g:loaded_sql_completion > 140
510 let regex_extra = '\\w*'
511 endif
512 endif
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000513 " Static maps which use populate the completion list
514 " using Vim's syntax highlighting rules
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100515 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'a <C-\><C-O>:call sqlcomplete#Map("syntax")<CR><C-X><C-O>'
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200516 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'k <C-\><C-O>:call sqlcomplete#Map("sqlKeyword'.regex_extra.'")<CR><C-X><C-O>'
517 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'f <C-\><C-O>:call sqlcomplete#Map("sqlFunction'.regex_extra.'")<CR><C-X><C-O>'
518 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'o <C-\><C-O>:call sqlcomplete#Map("sqlOption'.regex_extra.'")<CR><C-X><C-O>'
519 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'T <C-\><C-O>:call sqlcomplete#Map("sqlType'.regex_extra.'")<CR><C-X><C-O>'
520 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'s <C-\><C-O>:call sqlcomplete#Map("sqlStatement'.regex_extra.'")<CR><C-X><C-O>'
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000521 " Dynamic maps which use populate the completion list
522 " using the dbext.vim plugin
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100523 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'t <C-\><C-O>:call sqlcomplete#Map("table")<CR><C-X><C-O>'
524 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'p <C-\><C-O>:call sqlcomplete#Map("procedure")<CR><C-X><C-O>'
525 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'v <C-\><C-O>:call sqlcomplete#Map("view")<CR><C-X><C-O>'
526 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'c <C-\><C-O>:call sqlcomplete#Map("column")<CR><C-X><C-O>'
527 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'l <C-\><C-O>:call sqlcomplete#Map("column_csv")<CR><C-X><C-O>'
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000528 " The next 3 maps are only to be used while the completion window is
529 " active due to the <CR> at the beginning of the map
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100530 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'L <C-Y><C-\><C-O>:call sqlcomplete#Map("column_csv")<CR><C-X><C-O>'
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000531 " <C-Right> is not recognized on most Unix systems, so only create
532 " these additional maps on the Windows platform.
533 " If you would like to use these maps, choose a different key and make
534 " the same map in your vimrc.
Bram Moolenaar5c736222010-01-06 20:54:52 +0100535 " if has('win32')
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100536 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key_right.' <C-R>=sqlcomplete#DrillIntoTable()<CR>'
537 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key_left.' <C-R>=sqlcomplete#DrillOutOfColumns()<CR>'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100538 " endif
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000539 " Remove any cached items useful for schema changes
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100540 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'R <C-\><C-O>:call sqlcomplete#Map("resetCache")<CR><C-X><C-O>'
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000541 endif
542
543 if b:sql_compl_savefunc != ""
544 " We are changing the filetype to SQL from some other filetype
545 " which had OMNI completion defined. We need to activate the
546 " SQL completion plugin in order to cache some of the syntax items
547 " while the syntax rules for SQL are active.
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +0200548 call sqlcomplete#ResetCacheSyntax()
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000549 call sqlcomplete#PreCacheSyntax()
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000550 endif
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000551endif
552
Bram Moolenaardf177f62005-02-22 08:39:57 +0000553let &cpo = s:save_cpo
Bram Moolenaar84f72352012-03-11 15:57:40 +0100554unlet s:save_cpo
Bram Moolenaardf177f62005-02-22 08:39:57 +0000555
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000556" vim:sw=4: