blob: 3b56acd6749feeb6c6fc7e614c8b47e7eb860531 [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
Yinzuo Jiangfc762df2024-08-14 21:49:00 +02006" 2024 Jan 14 by Vim Project: browsefilter
7" 2024 May 18 by Vim Project: set comment options
8" 2024 Aug 14 by Vim Project: remove redundant code
Bram Moolenaardf177f62005-02-22 08:39:57 +00009" Download: http://vim.sourceforge.net/script.php?script_id=454
Bram Moolenaar071d4272004-06-13 20:20:40 +000010
Bram Moolenaar0fd92892006-03-09 22:27:48 +000011" For more details please use:
12" :h sql.txt
13"
Bram Moolenaar071d4272004-06-13 20:20:40 +000014" This file should only contain values that are common to all SQL languages
15" Oracle, Microsoft SQL Server, Sybase ASA/ASE, MySQL, and so on
16" If additional features are required create:
Bram Moolenaardf177f62005-02-22 08:39:57 +000017" vimfiles/after/ftplugin/sql.vim (Windows)
18" .vim/after/ftplugin/sql.vim (Unix)
19" to override and add any of your own settings.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020
Bram Moolenaar0fd92892006-03-09 22:27:48 +000021
22" This file also creates a command, SQLSetType, which allows you to change
23" SQL dialects on the fly. For example, if I open an Oracle SQL file, it
24" is color highlighted appropriately. If I open an Informix SQL file, it
25" will still be highlighted according to Oracles settings. By running:
26" :SQLSetType sqlinformix
27"
28" All files called sqlinformix.vim will be loaded from the indent and syntax
29" directories. This allows you to easily flip SQL dialects on a per file
30" basis. NOTE: you can also use completion:
31" :SQLSetType <tab>
32"
33" To change the default dialect, add the following to your vimrc:
34" let g:sql_type_default = 'sqlanywhere'
Bram Moolenaar5c736222010-01-06 20:54:52 +010035"
Bram Moolenaar34feacb2012-12-05 19:01:43 +010036" This file also creates a command, SQLGetType, which allows you to
Bram Moolenaar5c736222010-01-06 20:54:52 +010037" determine what the current dialect is in use.
38" :SQLGetType
39"
40" History
41"
Bram Moolenaar036986f2017-03-16 17:41:02 +010042" Version 12.0 (April 2013)
43"
44" NF: Added support for "BEGIN TRY ... END TRY ... BEGIN CATCH ... END CATCH
45" BF: This plugin is designed to be used with other plugins to enable the
46" SQL completion with Perl, Python, Java, ... The loading mechanism
47" was not checking if the SQL objects were created, which can lead to
48" the plugin not loading the SQL support.
49"
Bram Moolenaarad3b3662013-05-17 18:14:19 +020050" Version 11.0 (May 2013)
51"
52" NF: Updated to use SyntaxComplete's new regex support for syntax groups.
53"
Bram Moolenaar34feacb2012-12-05 19:01:43 +010054" Version 10.0 (Dec 2012)
55"
56" NF: Changed all maps to use noremap instead of must map
57" NF: Changed all visual maps to use xnoremap instead of vnoremap as they
58" should only be used in visual mode and not select mode.
59" BF: Most of the maps were using doubled up backslashes before they were
60" changed to using the search() function, which meant they no longer
61" worked.
62"
63" Version 9.0
64"
65" NF: Completes 'b:undo_ftplugin'
66" BF: Correctly set cpoptions when creating script
67"
Bram Moolenaaradc21822011-04-01 18:03:16 +020068" Version 8.0
Bram Moolenaar34feacb2012-12-05 19:01:43 +010069"
Bram Moolenaaradc21822011-04-01 18:03:16 +020070" NF: Improved the matchit plugin regex (Talek)
71"
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +020072" Version 7.0
Bram Moolenaar34feacb2012-12-05 19:01:43 +010073"
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +020074" NF: Calls the sqlcomplete#ResetCacheSyntax() function when calling
75" SQLSetType.
76"
Bram Moolenaar5c736222010-01-06 20:54:52 +010077" Version 6.0
Bram Moolenaar34feacb2012-12-05 19:01:43 +010078"
Bram Moolenaar5c736222010-01-06 20:54:52 +010079" NF: Adds the command SQLGetType
80"
81" Version 5.0
Bram Moolenaar34feacb2012-12-05 19:01:43 +010082"
83" NF: Adds the ability to choose the keys to control SQL completion, just add
Bram Moolenaar5c736222010-01-06 20:54:52 +010084" the following to your .vimrc:
85" let g:ftplugin_sql_omni_key = '<C-C>'
86" let g:ftplugin_sql_omni_key_right = '<Right>'
87" let g:ftplugin_sql_omni_key_left = '<Left>'
88"
Bram Moolenaar34feacb2012-12-05 19:01:43 +010089" BF: format-options - Auto-wrap comments using textwidth was turned off
Bram Moolenaar5c736222010-01-06 20:54:52 +010090" by mistake.
Bram Moolenaar0fd92892006-03-09 22:27:48 +000091
92
Bram Moolenaar071d4272004-06-13 20:20:40 +000093" Only do this when not done yet for this buffer
Bram Moolenaar036986f2017-03-16 17:41:02 +010094" This ftplugin can be used with other ftplugins. So ensure loading
95" happens if all elements of this plugin have not yet loaded.
Yinzuo Jiangfc762df2024-08-14 21:49:00 +020096if exists("b:did_ftplugin")
Bram Moolenaar036986f2017-03-16 17:41:02 +010097 finish
Bram Moolenaar071d4272004-06-13 20:20:40 +000098endif
99
Yinzuo Jiangfc762df2024-08-14 21:49:00 +0200100" Don't load another plugin for this buffer
101let b:did_ftplugin = 1
102
Bram Moolenaardf177f62005-02-22 08:39:57 +0000103let s:save_cpo = &cpo
Bram Moolenaar8e52a592012-05-18 21:49:28 +0200104set cpo&vim
Bram Moolenaardf177f62005-02-22 08:39:57 +0000105
Yinzuo Jiangfc762df2024-08-14 21:49:00 +0200106let b:undo_ftplugin = "setl comments< commentstring< formatoptions< define< omnifunc<" .
107 \ " | unlet! b:browsefilter b:match_words"
108
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000109" Disable autowrapping for code, but enable for comments
Bram Moolenaar036986f2017-03-16 17:41:02 +0100110" t Auto-wrap text using textwidth
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000111" c Auto-wrap comments using textwidth, inserting the current comment
112" leader automatically.
113setlocal formatoptions-=t
Bram Moolenaar5c736222010-01-06 20:54:52 +0100114setlocal formatoptions+=c
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000115
Riley Bruinsdf859a32024-05-19 09:23:10 +0200116setlocal comments=:-- commentstring=--\ %s
117
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000118" Functions/Commands to allow the user to change SQL syntax dialects
119" through the use of :SQLSetType <tab> for completion.
120" This works with both Vim 6 and 7.
121
122if !exists("*SQL_SetType")
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100123 " NOTE: You cannot use function! since this file can be
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000124 " sourced from within this function. That will result in
125 " an error reported by Vim.
126 function SQL_GetList(ArgLead, CmdLine, CursorPos)
127
128 if !exists('s:sql_list')
129 " Grab a list of files that contain "sql" in their names
130 let list_indent = globpath(&runtimepath, 'indent/*sql*')
131 let list_syntax = globpath(&runtimepath, 'syntax/*sql*')
132 let list_ftplugin = globpath(&runtimepath, 'ftplugin/*sql*')
133
134 let sqls = "\n".list_indent."\n".list_syntax."\n".list_ftplugin."\n"
135
136 " Strip out everything (path info) but the filename
137 " Regex
138 " From between two newline characters
139 " Non-greedily grab all characters
140 " Followed by a valid filename \w\+\.\w\+ (sql.vim)
141 " Followed by a newline, but do not include the newline
142 "
143 " Replace it with just the filename (get rid of PATH)
144 "
145 " Recursively, since there are many filenames that contain
146 " the word SQL in the indent, syntax and ftplugin directory
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100147 let sqls = substitute( sqls,
148 \ '[\n]\%(.\{-}\)\(\w\+\.\w\+\)\n\@=',
149 \ '\1\n',
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000150 \ 'g'
151 \ )
152
153 " Remove duplicates, since sqlanywhere.vim can exist in the
Viktor Szépe3fc7a7e2023-08-23 21:20:00 +0200154 " syntax, indent and ftplugin directory, yet we only want
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000155 " to display the option once
156 let index = match(sqls, '.\{-}\ze\n')
157 while index > -1
158 " Get the first filename
159 let file = matchstr(sqls, '.\{-}\ze\n', index)
160 " Recursively replace any *other* occurrence of that
161 " filename with nothing (ie remove it)
162 let sqls = substitute(sqls, '\%>'.(index+strlen(file)).'c\<'.file.'\>\n', '', 'g')
163 " Move on to the next filename
164 let index = match(sqls, '.\{-}\ze\n', (index+strlen(file)+1))
165 endwhile
166
167 " Sort the list if using version 7
168 if v:version >= 700
169 let mylist = split(sqls, "\n")
170 let mylist = sort(mylist)
171 let sqls = join(mylist, "\n")
172 endif
173
174 let s:sql_list = sqls
175 endif
176
177 return s:sql_list
178
179 endfunction
180
181 function SQL_SetType(name)
182
183 " User has decided to override default SQL scripts and
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100184 " specify a vendor specific version
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000185 " (ie Oracle, Informix, SQL Anywhere, ...)
186 " So check for an remove any settings that prevent the
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100187 " scripts from being executed, and then source the
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000188 " appropriate Vim scripts.
189 if exists("b:did_ftplugin")
190 unlet b:did_ftplugin
191 endif
192 if exists("b:current_syntax")
193 " echomsg 'SQLSetType - clearing syntax'
194 syntax clear
Bram Moolenaar036986f2017-03-16 17:41:02 +0100195 if exists("b:current_syntax")
196 unlet b:current_syntax
197 endif
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000198 endif
199 if exists("b:did_indent")
200 " echomsg 'SQLSetType - clearing indent'
201 unlet b:did_indent
202 " Set these values to their defaults
203 setlocal indentkeys&
204 setlocal indentexpr&
205 endif
206
207 " Ensure the name is in the correct format
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100208 let new_sql_type = substitute(a:name,
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000209 \ '\s*\([^\.]\+\)\(\.\w\+\)\?', '\L\1', '')
210
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100211 " Do not specify a buffer local variable if it is
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000212 " the default value
213 if new_sql_type == 'sql'
Bram Moolenaar036986f2017-03-16 17:41:02 +0100214 let new_sql_type = 'sqloracle'
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000215 endif
216 let b:sql_type_override = new_sql_type
217
Viktor Szépe3fc7a7e2023-08-23 21:20:00 +0200218 " Remove any cached SQL since a new syntax will have different
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +0200219 " items and groups
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200220 if !exists('g:loaded_sql_completion') || g:loaded_sql_completion >= 100
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +0200221 call sqlcomplete#ResetCacheSyntax()
222 endif
223
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000224 " Vim will automatically source the correct files if we
225 " change the filetype. You cannot do this with setfiletype
226 " since that command will only execute if a filetype has
227 " not already been set. In this case we want to override
228 " the existing filetype.
229 let &filetype = 'sql'
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +0200230
231 if b:sql_compl_savefunc != ""
232 " We are changing the filetype to SQL from some other filetype
233 " which had OMNI completion defined. We need to activate the
234 " SQL completion plugin in order to cache some of the syntax items
235 " while the syntax rules for SQL are active.
236 call sqlcomplete#PreCacheSyntax()
237 endif
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000238 endfunction
239 command! -nargs=* -complete=custom,SQL_GetList SQLSetType :call SQL_SetType(<q-args>)
240
241endif
242
Bram Moolenaar5c736222010-01-06 20:54:52 +0100243" Functions/Commands to allow the user determine current SQL syntax dialect
244" This works with both Vim 6 and 7.
245
246if !exists("*SQL_GetType")
247 function SQL_GetType()
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100248 if exists('b:sql_type_override')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100249 echomsg "Current SQL dialect in use:".b:sql_type_override
250 else
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100251 echomsg "Current SQL dialect in use:".g:sql_type_default
Bram Moolenaar5c736222010-01-06 20:54:52 +0100252 endif
253 endfunction
254 command! -nargs=0 SQLGetType :call SQL_GetType()
255endif
256
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000257if exists("b:sql_type_override")
258 " echo 'sourcing buffer ftplugin/'.b:sql_type_override.'.vim'
259 if globpath(&runtimepath, 'ftplugin/'.b:sql_type_override.'.vim') != ''
260 exec 'runtime ftplugin/'.b:sql_type_override.'.vim'
Bram Moolenaar036986f2017-03-16 17:41:02 +0100261 " else
262 " echomsg 'ftplugin/'.b:sql_type_override.' not exist, using default'
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000263 endif
264elseif exists("g:sql_type_default")
265 " echo 'sourcing global ftplugin/'.g:sql_type_default.'.vim'
266 if globpath(&runtimepath, 'ftplugin/'.g:sql_type_default.'.vim') != ''
267 exec 'runtime ftplugin/'.g:sql_type_default.'.vim'
Bram Moolenaar036986f2017-03-16 17:41:02 +0100268 " else
269 " echomsg 'ftplugin/'.g:sql_type_default.'.vim not exist, using default'
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000270 endif
271endif
272
Doug Kearns93197fd2024-01-14 20:59:02 +0100273" Win32 and GTK can filter files in the browse dialog
274if (has("gui_win32") || has("gui_gtk")) && !exists("b:browsefilter")
275 let b:browsefilter = "SQL Files (*.sql)\t*.sql\n"
276 if has("win32")
277 let b:browsefilter .= "All Files (*.*)\t*\n"
278 else
279 let b:browsefilter .= "All Files (*)\t*\n"
280 endif
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000281endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282
Bram Moolenaardf177f62005-02-22 08:39:57 +0000283" Some standard expressions for use with the matchit strings
284let s:notend = '\%(\<end\s\+\)\@<!'
285let s:when_no_matched_or_others = '\%(\<when\>\%(\s\+\%(\%(\<not\>\s\+\)\?<matched\>\)\|\<others\>\)\@!\)'
286let s:or_replace = '\%(or\s\+replace\s\+\)\?'
287
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288" Define patterns for the matchit macro
289if !exists("b:match_words")
290 " SQL is generally case insensitive
291 let b:match_ignorecase = 1
Bram Moolenaardf177f62005-02-22 08:39:57 +0000292
293 " Handle the following:
294 " if
295 " elseif | elsif
296 " else [if]
297 " end if
298 "
299 " [while condition] loop
300 " leave
301 " break
302 " continue
303 " exit
304 " end loop
305 "
306 " for
307 " leave
308 " break
309 " continue
310 " exit
311 " end loop
312 "
313 " do
314 " statements
315 " doend
316 "
317 " case
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100318 " when
Bram Moolenaardf177f62005-02-22 08:39:57 +0000319 " when
320 " default
321 " end case
322 "
323 " merge
324 " when not matched
325 " when matched
326 "
327 " EXCEPTION
328 " WHEN column_not_found THEN
329 " WHEN OTHERS THEN
330 "
Bram Moolenaar036986f2017-03-16 17:41:02 +0100331 " begin try
332 " end try
333 " begin catch
334 " end catch
335 "
Bram Moolenaardf177f62005-02-22 08:39:57 +0000336 " create[ or replace] procedure|function|event
Bram Moolenaar036986f2017-03-16 17:41:02 +0100337 " \ '^\s*\<\%(do\|for\|while\|loop\)\>.*:'.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000338
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100339 " For ColdFusion support
340 setlocal matchpairs+=<:>
341 let b:match_words = &matchpairs .
Bram Moolenaar036986f2017-03-16 17:41:02 +0100342 \ ',\%(\<begin\)\%(\s\+\%(try\|catch\)\>\)\@!:\<end\>\W*$,'.
343 \
344 \ '\<begin\s\+try\>:'.
345 \ '\<end\s\+try\>:'.
346 \ '\<begin\s\+catch\>:'.
347 \ '\<end\s\+catch\>,'.
348 \
Bram Moolenaardf177f62005-02-22 08:39:57 +0000349 \ s:notend . '\<if\>:'.
350 \ '\<elsif\>\|\<elseif\>\|\<else\>:'.
351 \ '\<end\s\+if\>,'.
352 \
Bram Moolenaaradc21822011-04-01 18:03:16 +0200353 \ '\(^\s*\)\@<=\(\<\%(do\|for\|while\|loop\)\>.*\):'.
354 \ '\%(\<exit\>\|\<leave\>\|\<break\>\|\<continue\>\):'.
355 \ '\%(\<doend\>\|\%(\<end\s\+\%(for\|while\|loop\>\)\)\),'.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000356 \
357 \ '\%('. s:notend . '\<case\>\):'.
358 \ '\%('.s:when_no_matched_or_others.'\):'.
359 \ '\%(\<when\s\+others\>\|\<end\s\+case\>\),' .
360 \
361 \ '\<merge\>:' .
362 \ '\<when\s\+not\s\+matched\>:' .
363 \ '\<when\s\+matched\>,' .
364 \
365 \ '\%(\<create\s\+' . s:or_replace . '\)\?'.
366 \ '\%(function\|procedure\|event\):'.
367 \ '\<returns\?\>'
Bram Moolenaar036986f2017-03-16 17:41:02 +0100368 " \ '\<begin\>\|\<returns\?\>:'.
369 " \ '\<end\>\(;\)\?\s*$'
370 " \ '\<exception\>:'.s:when_no_matched_or_others.
371 " \ ':\<when\s\+others\>,'.
372 "
373 " \ '\%(\<exception\>\|\%('. s:notend . '\<case\>\)\):'.
374 " \ '\%(\<default\>\|'.s:when_no_matched_or_others.'\):'.
375 " \ '\%(\%(\<when\s\+others\>\)\|\<end\s\+case\>\),' .
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376endif
Bram Moolenaardf177f62005-02-22 08:39:57 +0000377
378" Define how to find the macro definition of a variable using the various
379" [d, [D, [_CTRL_D and so on features
380" Match these values ignoring case
381" ie DECLARE varname INTEGER
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000382let &l:define = '\c\<\(VARIABLE\|DECLARE\|IN\|OUT\|INOUT\)\>'
Bram Moolenaardf177f62005-02-22 08:39:57 +0000383
384
385" Mappings to move to the next BEGIN ... END block
386" \W - no characters or digits
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100387nnoremap <buffer> <silent> ]] :call search('\c^\s*begin\>', 'W' )<CR>
388nnoremap <buffer> <silent> [[ :call search('\c^\s*begin\>', 'bW' )<CR>
389nnoremap <buffer> <silent> ][ :call search('\c^\s*end\W*$', 'W' )<CR>
390nnoremap <buffer> <silent> [] :call search('\c^\s*end\W*$', 'bW' )<CR>
391xnoremap <buffer> <silent> ]] :<C-U>exec "normal! gv"<Bar>call search('\c^\s*begin\>', 'W' )<CR>
392xnoremap <buffer> <silent> [[ :<C-U>exec "normal! gv"<Bar>call search('\c^\s*begin\>', 'bW' )<CR>
393xnoremap <buffer> <silent> ][ :<C-U>exec "normal! gv"<Bar>call search('\c^\s*end\W*$', 'W' )<CR>
394xnoremap <buffer> <silent> [] :<C-U>exec "normal! gv"<Bar>call search('\c^\s*end\W*$', 'bW' )<CR>
Bram Moolenaardf177f62005-02-22 08:39:57 +0000395
396
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000397" By default only look for CREATE statements, but allow
398" the user to override
399if !exists('g:ftplugin_sql_statements')
400 let g:ftplugin_sql_statements = 'create'
401endif
402
Bram Moolenaardf177f62005-02-22 08:39:57 +0000403" Predefined SQL objects what are used by the below mappings using
404" the ]} style maps.
Bram Moolenaara6c27c42019-05-09 19:16:22 +0200405" This global variable allows the users to override its value
Bram Moolenaardf177f62005-02-22 08:39:57 +0000406" from within their vimrc.
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000407" Note, you cannot use \?, since these patterns can be used to search
408" backwards, you must use \{,1}
Bram Moolenaardf177f62005-02-22 08:39:57 +0000409if !exists('g:ftplugin_sql_objects')
410 let g:ftplugin_sql_objects = 'function,procedure,event,' .
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100411 \ '\(existing\\|global\s\+temporary\s\+\)\{,1}' .
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000412 \ 'table,trigger' .
Bram Moolenaardf177f62005-02-22 08:39:57 +0000413 \ ',schema,service,publication,database,datatype,domain' .
414 \ ',index,subscription,synchronization,view,variable'
415endif
416
Bram Moolenaar5c736222010-01-06 20:54:52 +0100417" Key to trigger SQL completion
418if !exists('g:ftplugin_sql_omni_key')
419 let g:ftplugin_sql_omni_key = '<C-C>'
420endif
421" Key to trigger drill into column list
422if !exists('g:ftplugin_sql_omni_key_right')
423 let g:ftplugin_sql_omni_key_right = '<Right>'
424endif
425" Key to trigger drill out of column list
426if !exists('g:ftplugin_sql_omni_key_left')
427 let g:ftplugin_sql_omni_key_left = '<Left>'
428endif
429
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000430" Replace all ,'s with bars, except ones with numbers after them.
431" This will most likely be a \{,1} string.
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100432let s:ftplugin_sql_objects =
433 \ '\c^\s*' .
434 \ '\(\(' .
435 \ substitute(g:ftplugin_sql_statements, ',\d\@!', '\\\\|', 'g') .
436 \ '\)\s\+\(or\s\+replace\s\+\)\{,1}\)\{,1}' .
437 \ '\<\(' .
438 \ substitute(g:ftplugin_sql_objects, ',\d\@!', '\\\\|', 'g') .
439 \ '\)\>'
Bram Moolenaardf177f62005-02-22 08:39:57 +0000440
441" Mappings to move to the next CREATE ... block
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100442exec "nnoremap <buffer> <silent> ]} :call search('".s:ftplugin_sql_objects."', 'W')<CR>"
443exec "nnoremap <buffer> <silent> [{ :call search('".s:ftplugin_sql_objects."', 'bW')<CR>"
Bram Moolenaardf177f62005-02-22 08:39:57 +0000444" Could not figure out how to use a :call search() string in visual mode
445" without it ending visual mode
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000446" Unfortunately, this will add a entry to the search history
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100447exec 'xnoremap <buffer> <silent> ]} /'.s:ftplugin_sql_objects.'<CR>'
448exec 'xnoremap <buffer> <silent> [{ ?'.s:ftplugin_sql_objects.'<CR>'
Bram Moolenaardf177f62005-02-22 08:39:57 +0000449
450" Mappings to move to the next COMMENT
451"
452" Had to double the \ for the \| separator since this has a special
453" meaning on maps
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100454let b:comment_leader = '\(--\\|\/\/\\|\*\\|\/\*\\|\*\/\)'
Bram Moolenaardf177f62005-02-22 08:39:57 +0000455" Find the start of the next comment
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100456let b:comment_start = '^\(\s*'.b:comment_leader.'.*\n\)\@<!'.
457 \ '\(\s*'.b:comment_leader.'\)'
Bram Moolenaardf177f62005-02-22 08:39:57 +0000458" Find the end of the previous comment
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100459let b:comment_end = '\(^\s*'.b:comment_leader.'.*\n\)'.
460 \ '\(^\s*'.b:comment_leader.'\)\@!'
Bram Moolenaardf177f62005-02-22 08:39:57 +0000461" Skip over the comment
462let b:comment_jump_over = "call search('".
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100463 \ '^\(\s*'.b:comment_leader.'.*\n\)\@<!'.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000464 \ "', 'W')"
465let b:comment_skip_back = "call search('".
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100466 \ '^\(\s*'.b:comment_leader.'.*\n\)\@<!'.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000467 \ "', 'bW')"
468" Move to the start and end of comments
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000469exec 'nnoremap <silent><buffer> ]" :call search('."'".b:comment_start."'".', "W" )<CR>'
470exec 'nnoremap <silent><buffer> [" :call search('."'".b:comment_end."'".', "W" )<CR>'
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100471exec 'xnoremap <silent><buffer> ]" :<C-U>exec "normal! gv"<Bar>call search('."'".b:comment_start."'".', "W" )<CR>'
472exec 'xnoremap <silent><buffer> [" :<C-U>exec "normal! gv"<Bar>call search('."'".b:comment_end."'".', "W" )<CR>'
Bram Moolenaardf177f62005-02-22 08:39:57 +0000473
474" Comments can be of the form:
475" /*
476" *
477" */
478" or
Bram Moolenaardf177f62005-02-22 08:39:57 +0000479" --
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000480" or
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100481" //
Bram Moolenaardf177f62005-02-22 08:39:57 +0000482setlocal comments=s1:/*,mb:*,ex:*/,:--,://
483
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000484" Set completion with CTRL-X CTRL-O to autoloaded function.
485if exists('&omnifunc')
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000486 " Since the SQL completion plugin can be used in conjunction
487 " with other completion filetypes it must record the previous
488 " OMNI function prior to setting up the SQL OMNI function
489 let b:sql_compl_savefunc = &omnifunc
490
Bram Moolenaara6c27c42019-05-09 19:16:22 +0200491 " Source it to determine its version
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200492 runtime autoload/sqlcomplete.vim
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000493 " This is used by the sqlcomplete.vim plugin
Bram Moolenaara6c27c42019-05-09 19:16:22 +0200494 " Source it for its global functions
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100495 runtime autoload/syntaxcomplete.vim
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000496
497 setlocal omnifunc=sqlcomplete#Complete
498 " Prevent the intellisense plugin from loading
499 let b:sql_vis = 1
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000500 if !exists('g:omni_sql_no_default_maps')
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200501 let regex_extra = ''
502 if exists('g:loaded_syntax_completion') && exists('g:loaded_sql_completion')
503 if g:loaded_syntax_completion > 120 && g:loaded_sql_completion > 140
504 let regex_extra = '\\w*'
505 endif
506 endif
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000507 " Static maps which use populate the completion list
508 " using Vim's syntax highlighting rules
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100509 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 +0200510 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'k <C-\><C-O>:call sqlcomplete#Map("sqlKeyword'.regex_extra.'")<CR><C-X><C-O>'
511 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'f <C-\><C-O>:call sqlcomplete#Map("sqlFunction'.regex_extra.'")<CR><C-X><C-O>'
512 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'o <C-\><C-O>:call sqlcomplete#Map("sqlOption'.regex_extra.'")<CR><C-X><C-O>'
513 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'T <C-\><C-O>:call sqlcomplete#Map("sqlType'.regex_extra.'")<CR><C-X><C-O>'
514 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 +0000515 " Dynamic maps which use populate the completion list
516 " using the dbext.vim plugin
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100517 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'t <C-\><C-O>:call sqlcomplete#Map("table")<CR><C-X><C-O>'
518 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'p <C-\><C-O>:call sqlcomplete#Map("procedure")<CR><C-X><C-O>'
519 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'v <C-\><C-O>:call sqlcomplete#Map("view")<CR><C-X><C-O>'
520 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'c <C-\><C-O>:call sqlcomplete#Map("column")<CR><C-X><C-O>'
521 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 +0000522 " The next 3 maps are only to be used while the completion window is
523 " active due to the <CR> at the beginning of the map
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100524 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 +0000525 " <C-Right> is not recognized on most Unix systems, so only create
526 " these additional maps on the Windows platform.
527 " If you would like to use these maps, choose a different key and make
528 " the same map in your vimrc.
Bram Moolenaar5c736222010-01-06 20:54:52 +0100529 " if has('win32')
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100530 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key_right.' <C-R>=sqlcomplete#DrillIntoTable()<CR>'
531 exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key_left.' <C-R>=sqlcomplete#DrillOutOfColumns()<CR>'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100532 " endif
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000533 " Remove any cached items useful for schema changes
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100534 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 +0000535 endif
536
537 if b:sql_compl_savefunc != ""
538 " We are changing the filetype to SQL from some other filetype
539 " which had OMNI completion defined. We need to activate the
540 " SQL completion plugin in order to cache some of the syntax items
541 " while the syntax rules for SQL are active.
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +0200542 call sqlcomplete#ResetCacheSyntax()
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000543 call sqlcomplete#PreCacheSyntax()
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000544 endif
Bram Moolenaar0fd92892006-03-09 22:27:48 +0000545endif
546
Bram Moolenaardf177f62005-02-22 08:39:57 +0000547let &cpo = s:save_cpo
Bram Moolenaar84f72352012-03-11 15:57:40 +0100548unlet s:save_cpo
Bram Moolenaardf177f62005-02-22 08:39:57 +0000549
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000550" vim:sw=4: