Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1 | " SQL filetype plugin file |
| 2 | " Language: SQL (Common for Oracle, Microsoft SQL Server, Sybase) |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 3 | " Version: 12.0 |
Bram Moolenaar | adc2182 | 2011-04-01 18:03:16 +0200 | [diff] [blame] | 4 | " Maintainer: David Fishburn <dfishburn dot vim at gmail dot com> |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 5 | " Last Change: 2017 Mar 07 |
Doug Kearns | 93197fd | 2024-01-14 20:59:02 +0100 | [diff] [blame] | 6 | " 2024 Jan 14 by Vim Project (browsefilter) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7 | " Download: http://vim.sourceforge.net/script.php?script_id=454 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8 | |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 9 | " For more details please use: |
| 10 | " :h sql.txt |
| 11 | " |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 12 | " 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 Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 15 | " vimfiles/after/ftplugin/sql.vim (Windows) |
| 16 | " .vim/after/ftplugin/sql.vim (Unix) |
| 17 | " to override and add any of your own settings. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 18 | |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 19 | |
| 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 Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 33 | " |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 34 | " This file also creates a command, SQLGetType, which allows you to |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 35 | " determine what the current dialect is in use. |
| 36 | " :SQLGetType |
| 37 | " |
| 38 | " History |
| 39 | " |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 40 | " 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 Moolenaar | ad3b366 | 2013-05-17 18:14:19 +0200 | [diff] [blame] | 48 | " Version 11.0 (May 2013) |
| 49 | " |
| 50 | " NF: Updated to use SyntaxComplete's new regex support for syntax groups. |
| 51 | " |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 52 | " 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 Moolenaar | adc2182 | 2011-04-01 18:03:16 +0200 | [diff] [blame] | 66 | " Version 8.0 |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 67 | " |
Bram Moolenaar | adc2182 | 2011-04-01 18:03:16 +0200 | [diff] [blame] | 68 | " NF: Improved the matchit plugin regex (Talek) |
| 69 | " |
Bram Moolenaar | f9d5ca1 | 2010-08-01 16:13:51 +0200 | [diff] [blame] | 70 | " Version 7.0 |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 71 | " |
Bram Moolenaar | f9d5ca1 | 2010-08-01 16:13:51 +0200 | [diff] [blame] | 72 | " NF: Calls the sqlcomplete#ResetCacheSyntax() function when calling |
| 73 | " SQLSetType. |
| 74 | " |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 75 | " Version 6.0 |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 76 | " |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 77 | " NF: Adds the command SQLGetType |
| 78 | " |
| 79 | " Version 5.0 |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 80 | " |
| 81 | " NF: Adds the ability to choose the keys to control SQL completion, just add |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 82 | " 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 Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 87 | " BF: format-options - Auto-wrap comments using textwidth was turned off |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 88 | " by mistake. |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 89 | |
| 90 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 91 | " Only do this when not done yet for this buffer |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 92 | " This ftplugin can be used with other ftplugins. So ensure loading |
| 93 | " happens if all elements of this plugin have not yet loaded. |
| 94 | if exists("b:did_ftplugin") && exists("b:current_ftplugin") && b:current_ftplugin == 'sql' |
| 95 | finish |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 96 | endif |
| 97 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 98 | let s:save_cpo = &cpo |
Bram Moolenaar | 8e52a59 | 2012-05-18 21:49:28 +0200 | [diff] [blame] | 99 | set cpo&vim |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 100 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 101 | " Disable autowrapping for code, but enable for comments |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 102 | " t Auto-wrap text using textwidth |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 103 | " c Auto-wrap comments using textwidth, inserting the current comment |
| 104 | " leader automatically. |
| 105 | setlocal formatoptions-=t |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 106 | setlocal formatoptions+=c |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 107 | |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 108 | " 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 | |
| 112 | if !exists("*SQL_SetType") |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 113 | " NOTE: You cannot use function! since this file can be |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 114 | " 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 Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 137 | let sqls = substitute( sqls, |
| 138 | \ '[\n]\%(.\{-}\)\(\w\+\.\w\+\)\n\@=', |
| 139 | \ '\1\n', |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 140 | \ 'g' |
| 141 | \ ) |
| 142 | |
| 143 | " Remove duplicates, since sqlanywhere.vim can exist in the |
Viktor Szépe | 3fc7a7e | 2023-08-23 21:20:00 +0200 | [diff] [blame] | 144 | " syntax, indent and ftplugin directory, yet we only want |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 145 | " 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 Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 174 | " specify a vendor specific version |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 175 | " (ie Oracle, Informix, SQL Anywhere, ...) |
| 176 | " So check for an remove any settings that prevent the |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 177 | " scripts from being executed, and then source the |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 178 | " 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 Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 185 | if exists("b:current_syntax") |
| 186 | unlet b:current_syntax |
| 187 | endif |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 188 | 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 Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 198 | let new_sql_type = substitute(a:name, |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 199 | \ '\s*\([^\.]\+\)\(\.\w\+\)\?', '\L\1', '') |
| 200 | |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 201 | " Do not specify a buffer local variable if it is |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 202 | " the default value |
| 203 | if new_sql_type == 'sql' |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 204 | let new_sql_type = 'sqloracle' |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 205 | endif |
| 206 | let b:sql_type_override = new_sql_type |
| 207 | |
Viktor Szépe | 3fc7a7e | 2023-08-23 21:20:00 +0200 | [diff] [blame] | 208 | " Remove any cached SQL since a new syntax will have different |
Bram Moolenaar | f9d5ca1 | 2010-08-01 16:13:51 +0200 | [diff] [blame] | 209 | " items and groups |
Bram Moolenaar | ad3b366 | 2013-05-17 18:14:19 +0200 | [diff] [blame] | 210 | if !exists('g:loaded_sql_completion') || g:loaded_sql_completion >= 100 |
Bram Moolenaar | f9d5ca1 | 2010-08-01 16:13:51 +0200 | [diff] [blame] | 211 | call sqlcomplete#ResetCacheSyntax() |
| 212 | endif |
| 213 | |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 214 | " 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 Moolenaar | f9d5ca1 | 2010-08-01 16:13:51 +0200 | [diff] [blame] | 220 | |
| 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 Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 228 | endfunction |
| 229 | command! -nargs=* -complete=custom,SQL_GetList SQLSetType :call SQL_SetType(<q-args>) |
| 230 | |
| 231 | endif |
| 232 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 233 | " Functions/Commands to allow the user determine current SQL syntax dialect |
| 234 | " This works with both Vim 6 and 7. |
| 235 | |
| 236 | if !exists("*SQL_GetType") |
| 237 | function SQL_GetType() |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 238 | if exists('b:sql_type_override') |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 239 | echomsg "Current SQL dialect in use:".b:sql_type_override |
| 240 | else |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 241 | echomsg "Current SQL dialect in use:".g:sql_type_default |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 242 | endif |
| 243 | endfunction |
| 244 | command! -nargs=0 SQLGetType :call SQL_GetType() |
| 245 | endif |
| 246 | |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 247 | if 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 Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 251 | " else |
| 252 | " echomsg 'ftplugin/'.b:sql_type_override.' not exist, using default' |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 253 | endif |
| 254 | elseif 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 Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 258 | " else |
| 259 | " echomsg 'ftplugin/'.g:sql_type_default.'.vim not exist, using default' |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 260 | endif |
| 261 | endif |
| 262 | |
| 263 | " If the above runtime command succeeded, do not load the default settings |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 264 | " as they should have already been loaded from a previous run. |
| 265 | if exists("b:did_ftplugin") && exists("b:current_ftplugin") && b:current_ftplugin == 'sql' |
| 266 | finish |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 267 | endif |
| 268 | |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 269 | let b:undo_ftplugin = "setl comments< formatoptions< define< omnifunc<" . |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 270 | \ " | unlet! b:browsefilter b:match_words" |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 271 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 272 | " Don't load another plugin for this buffer |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 273 | let b:did_ftplugin = 1 |
| 274 | let b:current_ftplugin = 'sql' |
| 275 | |
Doug Kearns | 93197fd | 2024-01-14 20:59:02 +0100 | [diff] [blame] | 276 | " Win32 and GTK can filter files in the browse dialog |
| 277 | if (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 Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 284 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 285 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 286 | " Some standard expressions for use with the matchit strings |
| 287 | let s:notend = '\%(\<end\s\+\)\@<!' |
| 288 | let s:when_no_matched_or_others = '\%(\<when\>\%(\s\+\%(\%(\<not\>\s\+\)\?<matched\>\)\|\<others\>\)\@!\)' |
| 289 | let s:or_replace = '\%(or\s\+replace\s\+\)\?' |
| 290 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 291 | " Define patterns for the matchit macro |
| 292 | if !exists("b:match_words") |
| 293 | " SQL is generally case insensitive |
| 294 | let b:match_ignorecase = 1 |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 295 | |
| 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 Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 321 | " when |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 322 | " 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 Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 334 | " begin try |
| 335 | " end try |
| 336 | " begin catch |
| 337 | " end catch |
| 338 | " |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 339 | " create[ or replace] procedure|function|event |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 340 | " \ '^\s*\<\%(do\|for\|while\|loop\)\>.*:'. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 341 | |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 342 | " For ColdFusion support |
| 343 | setlocal matchpairs+=<:> |
| 344 | let b:match_words = &matchpairs . |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 345 | \ ',\%(\<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 Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 352 | \ s:notend . '\<if\>:'. |
| 353 | \ '\<elsif\>\|\<elseif\>\|\<else\>:'. |
| 354 | \ '\<end\s\+if\>,'. |
| 355 | \ |
Bram Moolenaar | adc2182 | 2011-04-01 18:03:16 +0200 | [diff] [blame] | 356 | \ '\(^\s*\)\@<=\(\<\%(do\|for\|while\|loop\)\>.*\):'. |
| 357 | \ '\%(\<exit\>\|\<leave\>\|\<break\>\|\<continue\>\):'. |
| 358 | \ '\%(\<doend\>\|\%(\<end\s\+\%(for\|while\|loop\>\)\)\),'. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 359 | \ |
| 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 Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 371 | " \ '\<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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 379 | endif |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 380 | |
| 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 Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 385 | let &l:define = '\c\<\(VARIABLE\|DECLARE\|IN\|OUT\|INOUT\)\>' |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 386 | |
| 387 | |
| 388 | " Mappings to move to the next BEGIN ... END block |
| 389 | " \W - no characters or digits |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 390 | nnoremap <buffer> <silent> ]] :call search('\c^\s*begin\>', 'W' )<CR> |
| 391 | nnoremap <buffer> <silent> [[ :call search('\c^\s*begin\>', 'bW' )<CR> |
| 392 | nnoremap <buffer> <silent> ][ :call search('\c^\s*end\W*$', 'W' )<CR> |
| 393 | nnoremap <buffer> <silent> [] :call search('\c^\s*end\W*$', 'bW' )<CR> |
| 394 | xnoremap <buffer> <silent> ]] :<C-U>exec "normal! gv"<Bar>call search('\c^\s*begin\>', 'W' )<CR> |
| 395 | xnoremap <buffer> <silent> [[ :<C-U>exec "normal! gv"<Bar>call search('\c^\s*begin\>', 'bW' )<CR> |
| 396 | xnoremap <buffer> <silent> ][ :<C-U>exec "normal! gv"<Bar>call search('\c^\s*end\W*$', 'W' )<CR> |
| 397 | xnoremap <buffer> <silent> [] :<C-U>exec "normal! gv"<Bar>call search('\c^\s*end\W*$', 'bW' )<CR> |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 398 | |
| 399 | |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 400 | " By default only look for CREATE statements, but allow |
| 401 | " the user to override |
| 402 | if !exists('g:ftplugin_sql_statements') |
| 403 | let g:ftplugin_sql_statements = 'create' |
| 404 | endif |
| 405 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 406 | " Predefined SQL objects what are used by the below mappings using |
| 407 | " the ]} style maps. |
Bram Moolenaar | a6c27c4 | 2019-05-09 19:16:22 +0200 | [diff] [blame] | 408 | " This global variable allows the users to override its value |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 409 | " from within their vimrc. |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 410 | " Note, you cannot use \?, since these patterns can be used to search |
| 411 | " backwards, you must use \{,1} |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 412 | if !exists('g:ftplugin_sql_objects') |
| 413 | let g:ftplugin_sql_objects = 'function,procedure,event,' . |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 414 | \ '\(existing\\|global\s\+temporary\s\+\)\{,1}' . |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 415 | \ 'table,trigger' . |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 416 | \ ',schema,service,publication,database,datatype,domain' . |
| 417 | \ ',index,subscription,synchronization,view,variable' |
| 418 | endif |
| 419 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 420 | " Key to trigger SQL completion |
| 421 | if !exists('g:ftplugin_sql_omni_key') |
| 422 | let g:ftplugin_sql_omni_key = '<C-C>' |
| 423 | endif |
| 424 | " Key to trigger drill into column list |
| 425 | if !exists('g:ftplugin_sql_omni_key_right') |
| 426 | let g:ftplugin_sql_omni_key_right = '<Right>' |
| 427 | endif |
| 428 | " Key to trigger drill out of column list |
| 429 | if !exists('g:ftplugin_sql_omni_key_left') |
| 430 | let g:ftplugin_sql_omni_key_left = '<Left>' |
| 431 | endif |
| 432 | |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 433 | " Replace all ,'s with bars, except ones with numbers after them. |
| 434 | " This will most likely be a \{,1} string. |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 435 | let 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 Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 443 | |
| 444 | " Mappings to move to the next CREATE ... block |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 445 | exec "nnoremap <buffer> <silent> ]} :call search('".s:ftplugin_sql_objects."', 'W')<CR>" |
| 446 | exec "nnoremap <buffer> <silent> [{ :call search('".s:ftplugin_sql_objects."', 'bW')<CR>" |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 447 | " Could not figure out how to use a :call search() string in visual mode |
| 448 | " without it ending visual mode |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 449 | " Unfortunately, this will add a entry to the search history |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 450 | exec 'xnoremap <buffer> <silent> ]} /'.s:ftplugin_sql_objects.'<CR>' |
| 451 | exec 'xnoremap <buffer> <silent> [{ ?'.s:ftplugin_sql_objects.'<CR>' |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 452 | |
| 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 Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 457 | let b:comment_leader = '\(--\\|\/\/\\|\*\\|\/\*\\|\*\/\)' |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 458 | " Find the start of the next comment |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 459 | let b:comment_start = '^\(\s*'.b:comment_leader.'.*\n\)\@<!'. |
| 460 | \ '\(\s*'.b:comment_leader.'\)' |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 461 | " Find the end of the previous comment |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 462 | let b:comment_end = '\(^\s*'.b:comment_leader.'.*\n\)'. |
| 463 | \ '\(^\s*'.b:comment_leader.'\)\@!' |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 464 | " Skip over the comment |
| 465 | let b:comment_jump_over = "call search('". |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 466 | \ '^\(\s*'.b:comment_leader.'.*\n\)\@<!'. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 467 | \ "', 'W')" |
| 468 | let b:comment_skip_back = "call search('". |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 469 | \ '^\(\s*'.b:comment_leader.'.*\n\)\@<!'. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 470 | \ "', 'bW')" |
| 471 | " Move to the start and end of comments |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 472 | exec 'nnoremap <silent><buffer> ]" :call search('."'".b:comment_start."'".', "W" )<CR>' |
| 473 | exec 'nnoremap <silent><buffer> [" :call search('."'".b:comment_end."'".', "W" )<CR>' |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 474 | exec 'xnoremap <silent><buffer> ]" :<C-U>exec "normal! gv"<Bar>call search('."'".b:comment_start."'".', "W" )<CR>' |
| 475 | exec 'xnoremap <silent><buffer> [" :<C-U>exec "normal! gv"<Bar>call search('."'".b:comment_end."'".', "W" )<CR>' |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 476 | |
| 477 | " Comments can be of the form: |
| 478 | " /* |
| 479 | " * |
| 480 | " */ |
| 481 | " or |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 482 | " -- |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 483 | " or |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 484 | " // |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 485 | setlocal comments=s1:/*,mb:*,ex:*/,:--,:// |
| 486 | |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 487 | " Set completion with CTRL-X CTRL-O to autoloaded function. |
| 488 | if exists('&omnifunc') |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 489 | " 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 Moolenaar | a6c27c4 | 2019-05-09 19:16:22 +0200 | [diff] [blame] | 494 | " Source it to determine its version |
Bram Moolenaar | ad3b366 | 2013-05-17 18:14:19 +0200 | [diff] [blame] | 495 | runtime autoload/sqlcomplete.vim |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 496 | " This is used by the sqlcomplete.vim plugin |
Bram Moolenaar | a6c27c4 | 2019-05-09 19:16:22 +0200 | [diff] [blame] | 497 | " Source it for its global functions |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 498 | runtime autoload/syntaxcomplete.vim |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 499 | |
| 500 | setlocal omnifunc=sqlcomplete#Complete |
| 501 | " Prevent the intellisense plugin from loading |
| 502 | let b:sql_vis = 1 |
Bram Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame] | 503 | if !exists('g:omni_sql_no_default_maps') |
Bram Moolenaar | ad3b366 | 2013-05-17 18:14:19 +0200 | [diff] [blame] | 504 | 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 Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame] | 510 | " Static maps which use populate the completion list |
| 511 | " using Vim's syntax highlighting rules |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 512 | exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'a <C-\><C-O>:call sqlcomplete#Map("syntax")<CR><C-X><C-O>' |
Bram Moolenaar | ad3b366 | 2013-05-17 18:14:19 +0200 | [diff] [blame] | 513 | 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 Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame] | 518 | " Dynamic maps which use populate the completion list |
| 519 | " using the dbext.vim plugin |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 520 | 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 Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame] | 525 | " 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 Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 527 | 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 Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 528 | " <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 Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 532 | " if has('win32') |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 533 | 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 Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 535 | " endif |
Bram Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame] | 536 | " Remove any cached items useful for schema changes |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 537 | exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'R <C-\><C-O>:call sqlcomplete#Map("resetCache")<CR><C-X><C-O>' |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 538 | 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 Moolenaar | f9d5ca1 | 2010-08-01 16:13:51 +0200 | [diff] [blame] | 545 | call sqlcomplete#ResetCacheSyntax() |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 546 | call sqlcomplete#PreCacheSyntax() |
Bram Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame] | 547 | endif |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 548 | endif |
| 549 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 550 | let &cpo = s:save_cpo |
Bram Moolenaar | 84f7235 | 2012-03-11 15:57:40 +0100 | [diff] [blame] | 551 | unlet s:save_cpo |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 552 | |
Bram Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame] | 553 | " vim:sw=4: |