blob: c85232f51ba3644682fe314aa063ab602edc5ef6 [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)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007" Download: http://vim.sourceforge.net/script.php?script_id=454
Bram Moolenaar071d4272004-06-13 20:20:40 +00008
Bram Moolenaar0fd92892006-03-09 22:27:48 +00009" For more details please use:
10" :h sql.txt
11"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012" This file should only contain values that are common to all SQL languages
13" Oracle, Microsoft SQL Server, Sybase ASA/ASE, MySQL, and so on
14" If additional features are required create:
Bram Moolenaardf177f62005-02-22 08:39:57 +000015" vimfiles/after/ftplugin/sql.vim (Windows)
16" .vim/after/ftplugin/sql.vim (Unix)
17" to override and add any of your own settings.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018
Bram Moolenaar0fd92892006-03-09 22:27:48 +000019
20" This file also creates a command, SQLSetType, which allows you to change
21" SQL dialects on the fly. For example, if I open an Oracle SQL file, it
22" is color highlighted appropriately. If I open an Informix SQL file, it
23" will still be highlighted according to Oracles settings. By running:
24" :SQLSetType sqlinformix
25"
26" All files called sqlinformix.vim will be loaded from the indent and syntax
27" directories. This allows you to easily flip SQL dialects on a per file
28" basis. NOTE: you can also use completion:
29" :SQLSetType <tab>
30"
31" To change the default dialect, add the following to your vimrc:
32" let g:sql_type_default = 'sqlanywhere'
Bram Moolenaar5c736222010-01-06 20:54:52 +010033"
Bram Moolenaar34feacb2012-12-05 19:01:43 +010034" This file also creates a command, SQLGetType, which allows you to
Bram Moolenaar5c736222010-01-06 20:54:52 +010035" determine what the current dialect is in use.
36" :SQLGetType
37"
38" History
39"
Bram Moolenaar036986f2017-03-16 17:41:02 +010040" Version 12.0 (April 2013)
41"
42" NF: Added support for "BEGIN TRY ... END TRY ... BEGIN CATCH ... END CATCH
43" BF: This plugin is designed to be used with other plugins to enable the
44" SQL completion with Perl, Python, Java, ... The loading mechanism
45" was not checking if the SQL objects were created, which can lead to
46" the plugin not loading the SQL support.
47"
Bram Moolenaarad3b3662013-05-17 18:14:19 +020048" Version 11.0 (May 2013)
49"
50" NF: Updated to use SyntaxComplete's new regex support for syntax groups.
51"
Bram Moolenaar34feacb2012-12-05 19:01:43 +010052" Version 10.0 (Dec 2012)
53"
54" NF: Changed all maps to use noremap instead of must map
55" NF: Changed all visual maps to use xnoremap instead of vnoremap as they
56" should only be used in visual mode and not select mode.
57" BF: Most of the maps were using doubled up backslashes before they were
58" changed to using the search() function, which meant they no longer
59" worked.
60"
61" Version 9.0
62"
63" NF: Completes 'b:undo_ftplugin'
64" BF: Correctly set cpoptions when creating script
65"
Bram Moolenaaradc21822011-04-01 18:03:16 +020066" Version 8.0
Bram Moolenaar34feacb2012-12-05 19:01:43 +010067"
Bram Moolenaaradc21822011-04-01 18:03:16 +020068" NF: Improved the matchit plugin regex (Talek)
69"
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +020070" Version 7.0
Bram Moolenaar34feacb2012-12-05 19:01:43 +010071"
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +020072" NF: Calls the sqlcomplete#ResetCacheSyntax() function when calling
73" SQLSetType.
74"
Bram Moolenaar5c736222010-01-06 20:54:52 +010075" Version 6.0
Bram Moolenaar34feacb2012-12-05 19:01:43 +010076"
Bram Moolenaar5c736222010-01-06 20:54:52 +010077" NF: Adds the command SQLGetType
78"
79" Version 5.0
Bram Moolenaar34feacb2012-12-05 19:01:43 +010080"
81" NF: Adds the ability to choose the keys to control SQL completion, just add
Bram Moolenaar5c736222010-01-06 20:54:52 +010082" the following to your .vimrc:
83" let g:ftplugin_sql_omni_key = '<C-C>'
84" let g:ftplugin_sql_omni_key_right = '<Right>'
85" let g:ftplugin_sql_omni_key_left = '<Left>'
86"
Bram Moolenaar34feacb2012-12-05 19:01:43 +010087" BF: format-options - Auto-wrap comments using textwidth was turned off
Bram Moolenaar5c736222010-01-06 20:54:52 +010088" by mistake.
Bram Moolenaar0fd92892006-03-09 22:27:48 +000089
90
Bram Moolenaar071d4272004-06-13 20:20:40 +000091" Only do this when not done yet for this buffer
Bram Moolenaar036986f2017-03-16 17:41:02 +010092" This ftplugin can be used with other ftplugins. So ensure loading
93" happens if all elements of this plugin have not yet loaded.
94if exists("b:did_ftplugin") && exists("b:current_ftplugin") && b:current_ftplugin == 'sql'
95 finish
Bram Moolenaar071d4272004-06-13 20:20:40 +000096endif
97
Bram Moolenaardf177f62005-02-22 08:39:57 +000098let s:save_cpo = &cpo
Bram Moolenaar8e52a592012-05-18 21:49:28 +020099set cpo&vim
Bram Moolenaardf177f62005-02-22 08:39:57 +0000100
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000101" Disable autowrapping for code, but enable for comments
Bram Moolenaar036986f2017-03-16 17:41:02 +0100102" t Auto-wrap text using textwidth
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000103" c Auto-wrap comments using textwidth, inserting the current comment
104" leader automatically.
105setlocal formatoptions-=t
Bram Moolenaar5c736222010-01-06 20:54:52 +0100106setlocal formatoptions+=c
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000107
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000108" Functions/Commands to allow the user to change SQL syntax dialects
109" through the use of :SQLSetType <tab> for completion.
110" This works with both Vim 6 and 7.
111
112if !exists("*SQL_SetType")
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100113 " NOTE: You cannot use function! since this file can be
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000114 " sourced from within this function. That will result in
115 " an error reported by Vim.
116 function SQL_GetList(ArgLead, CmdLine, CursorPos)
117
118 if !exists('s:sql_list')
119 " Grab a list of files that contain "sql" in their names
120 let list_indent = globpath(&runtimepath, 'indent/*sql*')
121 let list_syntax = globpath(&runtimepath, 'syntax/*sql*')
122 let list_ftplugin = globpath(&runtimepath, 'ftplugin/*sql*')
123
124 let sqls = "\n".list_indent."\n".list_syntax."\n".list_ftplugin."\n"
125
126 " Strip out everything (path info) but the filename
127 " Regex
128 " From between two newline characters
129 " Non-greedily grab all characters
130 " Followed by a valid filename \w\+\.\w\+ (sql.vim)
131 " Followed by a newline, but do not include the newline
132 "
133 " Replace it with just the filename (get rid of PATH)
134 "
135 " Recursively, since there are many filenames that contain
136 " the word SQL in the indent, syntax and ftplugin directory
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100137 let sqls = substitute( sqls,
138 \ '[\n]\%(.\{-}\)\(\w\+\.\w\+\)\n\@=',
139 \ '\1\n',
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000140 \ 'g'
141 \ )
142
143 " Remove duplicates, since sqlanywhere.vim can exist in the
Viktor Szépe3fc7a7e2023-08-23 21:20:00 +0200144 " syntax, indent and ftplugin directory, yet we only want
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000145 " to display the option once
146 let index = match(sqls, '.\{-}\ze\n')
147 while index > -1
148 " Get the first filename
149 let file = matchstr(sqls, '.\{-}\ze\n', index)
150 " Recursively replace any *other* occurrence of that
151 " filename with nothing (ie remove it)
152 let sqls = substitute(sqls, '\%>'.(index+strlen(file)).'c\<'.file.'\>\n', '', 'g')
153 " Move on to the next filename
154 let index = match(sqls, '.\{-}\ze\n', (index+strlen(file)+1))
155 endwhile
156
157 " Sort the list if using version 7
158 if v:version >= 700
159 let mylist = split(sqls, "\n")
160 let mylist = sort(mylist)
161 let sqls = join(mylist, "\n")
162 endif
163
164 let s:sql_list = sqls
165 endif
166
167 return s:sql_list
168
169 endfunction
170
171 function SQL_SetType(name)
172
173 " User has decided to override default SQL scripts and
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100174 " specify a vendor specific version
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000175 " (ie Oracle, Informix, SQL Anywhere, ...)
176 " So check for an remove any settings that prevent the
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100177 " scripts from being executed, and then source the
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000178 " appropriate Vim scripts.
179 if exists("b:did_ftplugin")
180 unlet b:did_ftplugin
181 endif
182 if exists("b:current_syntax")
183 " echomsg 'SQLSetType - clearing syntax'
184 syntax clear
Bram Moolenaar036986f2017-03-16 17:41:02 +0100185 if exists("b:current_syntax")
186 unlet b:current_syntax
187 endif
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000188 endif
189 if exists("b:did_indent")
190 " echomsg 'SQLSetType - clearing indent'
191 unlet b:did_indent
192 " Set these values to their defaults
193 setlocal indentkeys&
194 setlocal indentexpr&
195 endif
196
197 " Ensure the name is in the correct format
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100198 let new_sql_type = substitute(a:name,
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000199 \ '\s*\([^\.]\+\)\(\.\w\+\)\?', '\L\1', '')
200
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100201 " Do not specify a buffer local variable if it is
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000202 " the default value
203 if new_sql_type == 'sql'
Bram Moolenaar036986f2017-03-16 17:41:02 +0100204 let new_sql_type = 'sqloracle'
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000205 endif
206 let b:sql_type_override = new_sql_type
207
Viktor Szépe3fc7a7e2023-08-23 21:20:00 +0200208 " Remove any cached SQL since a new syntax will have different
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +0200209 " items and groups
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200210 if !exists('g:loaded_sql_completion') || g:loaded_sql_completion >= 100
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +0200211 call sqlcomplete#ResetCacheSyntax()
212 endif
213
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000214 " Vim will automatically source the correct files if we
215 " change the filetype. You cannot do this with setfiletype
216 " since that command will only execute if a filetype has
217 " not already been set. In this case we want to override
218 " the existing filetype.
219 let &filetype = 'sql'
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +0200220
221 if b:sql_compl_savefunc != ""
222 " We are changing the filetype to SQL from some other filetype
223 " which had OMNI completion defined. We need to activate the
224 " SQL completion plugin in order to cache some of the syntax items
225 " while the syntax rules for SQL are active.
226 call sqlcomplete#PreCacheSyntax()
227 endif
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000228 endfunction
229 command! -nargs=* -complete=custom,SQL_GetList SQLSetType :call SQL_SetType(<q-args>)
230
231endif
232
Bram Moolenaar5c736222010-01-06 20:54:52 +0100233" Functions/Commands to allow the user determine current SQL syntax dialect
234" This works with both Vim 6 and 7.
235
236if !exists("*SQL_GetType")
237 function SQL_GetType()
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100238 if exists('b:sql_type_override')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100239 echomsg "Current SQL dialect in use:".b:sql_type_override
240 else
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100241 echomsg "Current SQL dialect in use:".g:sql_type_default
Bram Moolenaar5c736222010-01-06 20:54:52 +0100242 endif
243 endfunction
244 command! -nargs=0 SQLGetType :call SQL_GetType()
245endif
246
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000247if exists("b:sql_type_override")
248 " echo 'sourcing buffer ftplugin/'.b:sql_type_override.'.vim'
249 if globpath(&runtimepath, 'ftplugin/'.b:sql_type_override.'.vim') != ''
250 exec 'runtime ftplugin/'.b:sql_type_override.'.vim'
Bram Moolenaar036986f2017-03-16 17:41:02 +0100251 " else
252 " echomsg 'ftplugin/'.b:sql_type_override.' not exist, using default'
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000253 endif
254elseif exists("g:sql_type_default")
255 " echo 'sourcing global ftplugin/'.g:sql_type_default.'.vim'
256 if globpath(&runtimepath, 'ftplugin/'.g:sql_type_default.'.vim') != ''
257 exec 'runtime ftplugin/'.g:sql_type_default.'.vim'
Bram Moolenaar036986f2017-03-16 17:41:02 +0100258 " else
259 " echomsg 'ftplugin/'.g:sql_type_default.'.vim not exist, using default'
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000260 endif
261endif
262
263" If the above runtime command succeeded, do not load the default settings
Bram Moolenaar036986f2017-03-16 17:41:02 +0100264" as they should have already been loaded from a previous run.
265if exists("b:did_ftplugin") && exists("b:current_ftplugin") && b:current_ftplugin == 'sql'
266 finish
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000267endif
268
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100269let b:undo_ftplugin = "setl comments< formatoptions< define< omnifunc<" .
Bram Moolenaar036986f2017-03-16 17:41:02 +0100270 \ " | unlet! b:browsefilter b:match_words"
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000271
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272" Don't load another plugin for this buffer
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000273let b:did_ftplugin = 1
274let b:current_ftplugin = 'sql'
275
Doug Kearns93197fd2024-01-14 20:59:02 +0100276" Win32 and GTK can filter files in the browse dialog
277if (has("gui_win32") || has("gui_gtk")) && !exists("b:browsefilter")
278 let b:browsefilter = "SQL Files (*.sql)\t*.sql\n"
279 if has("win32")
280 let b:browsefilter .= "All Files (*.*)\t*\n"
281 else
282 let b:browsefilter .= "All Files (*)\t*\n"
283 endif
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000284endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285
Bram Moolenaardf177f62005-02-22 08:39:57 +0000286" Some standard expressions for use with the matchit strings
287let s:notend = '\%(\<end\s\+\)\@<!'
288let s:when_no_matched_or_others = '\%(\<when\>\%(\s\+\%(\%(\<not\>\s\+\)\?<matched\>\)\|\<others\>\)\@!\)'
289let s:or_replace = '\%(or\s\+replace\s\+\)\?'
290
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291" Define patterns for the matchit macro
292if !exists("b:match_words")
293 " SQL is generally case insensitive
294 let b:match_ignorecase = 1
Bram Moolenaardf177f62005-02-22 08:39:57 +0000295
296 " Handle the following:
297 " if
298 " elseif | elsif
299 " else [if]
300 " end if
301 "
302 " [while condition] loop
303 " leave
304 " break
305 " continue
306 " exit
307 " end loop
308 "
309 " for
310 " leave
311 " break
312 " continue
313 " exit
314 " end loop
315 "
316 " do
317 " statements
318 " doend
319 "
320 " case
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100321 " when
Bram Moolenaardf177f62005-02-22 08:39:57 +0000322 " when
323 " default
324 " end case
325 "
326 " merge
327 " when not matched
328 " when matched
329 "
330 " EXCEPTION
331 " WHEN column_not_found THEN
332 " WHEN OTHERS THEN
333 "
Bram Moolenaar036986f2017-03-16 17:41:02 +0100334 " begin try
335 " end try
336 " begin catch
337 " end catch
338 "
Bram Moolenaardf177f62005-02-22 08:39:57 +0000339 " create[ or replace] procedure|function|event
Bram Moolenaar036986f2017-03-16 17:41:02 +0100340 " \ '^\s*\<\%(do\|for\|while\|loop\)\>.*:'.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000341
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100342 " For ColdFusion support
343 setlocal matchpairs+=<:>
344 let b:match_words = &matchpairs .
Bram Moolenaar036986f2017-03-16 17:41:02 +0100345 \ ',\%(\<begin\)\%(\s\+\%(try\|catch\)\>\)\@!:\<end\>\W*$,'.
346 \
347 \ '\<begin\s\+try\>:'.
348 \ '\<end\s\+try\>:'.
349 \ '\<begin\s\+catch\>:'.
350 \ '\<end\s\+catch\>,'.
351 \
Bram Moolenaardf177f62005-02-22 08:39:57 +0000352 \ s:notend . '\<if\>:'.
353 \ '\<elsif\>\|\<elseif\>\|\<else\>:'.
354 \ '\<end\s\+if\>,'.
355 \
Bram Moolenaaradc21822011-04-01 18:03:16 +0200356 \ '\(^\s*\)\@<=\(\<\%(do\|for\|while\|loop\)\>.*\):'.
357 \ '\%(\<exit\>\|\<leave\>\|\<break\>\|\<continue\>\):'.
358 \ '\%(\<doend\>\|\%(\<end\s\+\%(for\|while\|loop\>\)\)\),'.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000359 \
360 \ '\%('. s:notend . '\<case\>\):'.
361 \ '\%('.s:when_no_matched_or_others.'\):'.
362 \ '\%(\<when\s\+others\>\|\<end\s\+case\>\),' .
363 \
364 \ '\<merge\>:' .
365 \ '\<when\s\+not\s\+matched\>:' .
366 \ '\<when\s\+matched\>,' .
367 \
368 \ '\%(\<create\s\+' . s:or_replace . '\)\?'.
369 \ '\%(function\|procedure\|event\):'.
370 \ '\<returns\?\>'
Bram Moolenaar036986f2017-03-16 17:41:02 +0100371 " \ '\<begin\>\|\<returns\?\>:'.
372 " \ '\<end\>\(;\)\?\s*$'
373 " \ '\<exception\>:'.s:when_no_matched_or_others.
374 " \ ':\<when\s\+others\>,'.
375 "
376 " \ '\%(\<exception\>\|\%('. s:notend . '\<case\>\)\):'.
377 " \ '\%(\<default\>\|'.s:when_no_matched_or_others.'\):'.
378 " \ '\%(\%(\<when\s\+others\>\)\|\<end\s\+case\>\),' .
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379endif
Bram Moolenaardf177f62005-02-22 08:39:57 +0000380
381" Define how to find the macro definition of a variable using the various
382" [d, [D, [_CTRL_D and so on features
383" Match these values ignoring case
384" ie DECLARE varname INTEGER
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000385let &l:define = '\c\<\(VARIABLE\|DECLARE\|IN\|OUT\|INOUT\)\>'
Bram Moolenaardf177f62005-02-22 08:39:57 +0000386
387
388" Mappings to move to the next BEGIN ... END block
389" \W - no characters or digits
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100390nnoremap <buffer> <silent> ]] :call search('\c^\s*begin\>', 'W' )<CR>
391nnoremap <buffer> <silent> [[ :call search('\c^\s*begin\>', 'bW' )<CR>
392nnoremap <buffer> <silent> ][ :call search('\c^\s*end\W*$', 'W' )<CR>
393nnoremap <buffer> <silent> [] :call search('\c^\s*end\W*$', 'bW' )<CR>
394xnoremap <buffer> <silent> ]] :<C-U>exec "normal! gv"<Bar>call search('\c^\s*begin\>', 'W' )<CR>
395xnoremap <buffer> <silent> [[ :<C-U>exec "normal! gv"<Bar>call search('\c^\s*begin\>', 'bW' )<CR>
396xnoremap <buffer> <silent> ][ :<C-U>exec "normal! gv"<Bar>call search('\c^\s*end\W*$', 'W' )<CR>
397xnoremap <buffer> <silent> [] :<C-U>exec "normal! gv"<Bar>call search('\c^\s*end\W*$', 'bW' )<CR>
Bram Moolenaardf177f62005-02-22 08:39:57 +0000398
399
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000400" By default only look for CREATE statements, but allow
401" the user to override
402if !exists('g:ftplugin_sql_statements')
403 let g:ftplugin_sql_statements = 'create'
404endif
405
Bram Moolenaardf177f62005-02-22 08:39:57 +0000406" Predefined SQL objects what are used by the below mappings using
407" the ]} style maps.
Bram Moolenaara6c27c42019-05-09 19:16:22 +0200408" This global variable allows the users to override its value
Bram Moolenaardf177f62005-02-22 08:39:57 +0000409" from within their vimrc.
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000410" Note, you cannot use \?, since these patterns can be used to search
411" backwards, you must use \{,1}
Bram Moolenaardf177f62005-02-22 08:39:57 +0000412if !exists('g:ftplugin_sql_objects')
413 let g:ftplugin_sql_objects = 'function,procedure,event,' .
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100414 \ '\(existing\\|global\s\+temporary\s\+\)\{,1}' .
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000415 \ 'table,trigger' .
Bram Moolenaardf177f62005-02-22 08:39:57 +0000416 \ ',schema,service,publication,database,datatype,domain' .
417 \ ',index,subscription,synchronization,view,variable'
418endif
419
Bram Moolenaar5c736222010-01-06 20:54:52 +0100420" Key to trigger SQL completion
421if !exists('g:ftplugin_sql_omni_key')
422 let g:ftplugin_sql_omni_key = '<C-C>'
423endif
424" Key to trigger drill into column list
425if !exists('g:ftplugin_sql_omni_key_right')
426 let g:ftplugin_sql_omni_key_right = '<Right>'
427endif
428" Key to trigger drill out of column list
429if !exists('g:ftplugin_sql_omni_key_left')
430 let g:ftplugin_sql_omni_key_left = '<Left>'
431endif
432
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000433" Replace all ,'s with bars, except ones with numbers after them.
434" This will most likely be a \{,1} string.
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100435let s:ftplugin_sql_objects =
436 \ '\c^\s*' .
437 \ '\(\(' .
438 \ substitute(g:ftplugin_sql_statements, ',\d\@!', '\\\\|', 'g') .
439 \ '\)\s\+\(or\s\+replace\s\+\)\{,1}\)\{,1}' .
440 \ '\<\(' .
441 \ substitute(g:ftplugin_sql_objects, ',\d\@!', '\\\\|', 'g') .
442 \ '\)\>'
Bram Moolenaardf177f62005-02-22 08:39:57 +0000443
444" Mappings to move to the next CREATE ... block
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100445exec "nnoremap <buffer> <silent> ]} :call search('".s:ftplugin_sql_objects."', 'W')<CR>"
446exec "nnoremap <buffer> <silent> [{ :call search('".s:ftplugin_sql_objects."', 'bW')<CR>"
Bram Moolenaardf177f62005-02-22 08:39:57 +0000447" Could not figure out how to use a :call search() string in visual mode
448" without it ending visual mode
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000449" Unfortunately, this will add a entry to the search history
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100450exec 'xnoremap <buffer> <silent> ]} /'.s:ftplugin_sql_objects.'<CR>'
451exec 'xnoremap <buffer> <silent> [{ ?'.s:ftplugin_sql_objects.'<CR>'
Bram Moolenaardf177f62005-02-22 08:39:57 +0000452
453" Mappings to move to the next COMMENT
454"
455" Had to double the \ for the \| separator since this has a special
456" meaning on maps
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100457let b:comment_leader = '\(--\\|\/\/\\|\*\\|\/\*\\|\*\/\)'
Bram Moolenaardf177f62005-02-22 08:39:57 +0000458" Find the start of the next comment
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100459let b:comment_start = '^\(\s*'.b:comment_leader.'.*\n\)\@<!'.
460 \ '\(\s*'.b:comment_leader.'\)'
Bram Moolenaardf177f62005-02-22 08:39:57 +0000461" Find the end of the previous comment
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100462let b:comment_end = '\(^\s*'.b:comment_leader.'.*\n\)'.
463 \ '\(^\s*'.b:comment_leader.'\)\@!'
Bram Moolenaardf177f62005-02-22 08:39:57 +0000464" Skip over the comment
465let b:comment_jump_over = "call search('".
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100466 \ '^\(\s*'.b:comment_leader.'.*\n\)\@<!'.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000467 \ "', 'W')"
468let b:comment_skip_back = "call search('".
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100469 \ '^\(\s*'.b:comment_leader.'.*\n\)\@<!'.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000470 \ "', 'bW')"
471" Move to the start and end of comments
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000472exec 'nnoremap <silent><buffer> ]" :call search('."'".b:comment_start."'".', "W" )<CR>'
473exec 'nnoremap <silent><buffer> [" :call search('."'".b:comment_end."'".', "W" )<CR>'
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100474exec 'xnoremap <silent><buffer> ]" :<C-U>exec "normal! gv"<Bar>call search('."'".b:comment_start."'".', "W" )<CR>'
475exec 'xnoremap <silent><buffer> [" :<C-U>exec "normal! gv"<Bar>call search('."'".b:comment_end."'".', "W" )<CR>'
Bram Moolenaardf177f62005-02-22 08:39:57 +0000476
477" Comments can be of the form:
478" /*
479" *
480" */
481" or
Bram Moolenaardf177f62005-02-22 08:39:57 +0000482" --
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000483" or
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100484" //
Bram Moolenaardf177f62005-02-22 08:39:57 +0000485setlocal comments=s1:/*,mb:*,ex:*/,:--,://
486
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000487" Set completion with CTRL-X CTRL-O to autoloaded function.
488if exists('&omnifunc')
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000489 " Since the SQL completion plugin can be used in conjunction
490 " with other completion filetypes it must record the previous
491 " OMNI function prior to setting up the SQL OMNI function
492 let b:sql_compl_savefunc = &omnifunc
493
Bram Moolenaara6c27c42019-05-09 19:16:22 +0200494 " Source it to determine its version
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200495 runtime autoload/sqlcomplete.vim
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000496 " This is used by the sqlcomplete.vim plugin
Bram Moolenaara6c27c42019-05-09 19:16:22 +0200497 " Source it for its global functions
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100498 runtime autoload/syntaxcomplete.vim
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000499
500 setlocal omnifunc=sqlcomplete#Complete
501 " Prevent the intellisense plugin from loading
502 let b:sql_vis = 1
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000503 if !exists('g:omni_sql_no_default_maps')
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200504 let regex_extra = ''
505 if exists('g:loaded_syntax_completion') && exists('g:loaded_sql_completion')
506 if g:loaded_syntax_completion > 120 && g:loaded_sql_completion > 140
507 let regex_extra = '\\w*'
508 endif
509 endif
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000510 " Static maps which use populate the completion list
511 " using Vim's syntax highlighting rules
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100512 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 +0200513 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'k <C-\><C-O>:call sqlcomplete#Map("sqlKeyword'.regex_extra.'")<CR><C-X><C-O>'
514 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'f <C-\><C-O>:call sqlcomplete#Map("sqlFunction'.regex_extra.'")<CR><C-X><C-O>'
515 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'o <C-\><C-O>:call sqlcomplete#Map("sqlOption'.regex_extra.'")<CR><C-X><C-O>'
516 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'T <C-\><C-O>:call sqlcomplete#Map("sqlType'.regex_extra.'")<CR><C-X><C-O>'
517 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 +0000518 " Dynamic maps which use populate the completion list
519 " using the dbext.vim plugin
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100520 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'t <C-\><C-O>:call sqlcomplete#Map("table")<CR><C-X><C-O>'
521 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'p <C-\><C-O>:call sqlcomplete#Map("procedure")<CR><C-X><C-O>'
522 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'v <C-\><C-O>:call sqlcomplete#Map("view")<CR><C-X><C-O>'
523 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'c <C-\><C-O>:call sqlcomplete#Map("column")<CR><C-X><C-O>'
524 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 +0000525 " The next 3 maps are only to be used while the completion window is
526 " active due to the <CR> at the beginning of the map
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100527 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 +0000528 " <C-Right> is not recognized on most Unix systems, so only create
529 " these additional maps on the Windows platform.
530 " If you would like to use these maps, choose a different key and make
531 " the same map in your vimrc.
Bram Moolenaar5c736222010-01-06 20:54:52 +0100532 " if has('win32')
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100533 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key_right.' <C-R>=sqlcomplete#DrillIntoTable()<CR>'
534 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key_left.' <C-R>=sqlcomplete#DrillOutOfColumns()<CR>'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100535 " endif
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000536 " Remove any cached items useful for schema changes
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100537 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 +0000538 endif
539
540 if b:sql_compl_savefunc != ""
541 " We are changing the filetype to SQL from some other filetype
542 " which had OMNI completion defined. We need to activate the
543 " SQL completion plugin in order to cache some of the syntax items
544 " while the syntax rules for SQL are active.
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +0200545 call sqlcomplete#ResetCacheSyntax()
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000546 call sqlcomplete#PreCacheSyntax()
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000547 endif
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000548endif
549
Bram Moolenaardf177f62005-02-22 08:39:57 +0000550let &cpo = s:save_cpo
Bram Moolenaar84f72352012-03-11 15:57:40 +0100551unlet s:save_cpo
Bram Moolenaardf177f62005-02-22 08:39:57 +0000552
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000553" vim:sw=4: