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 |
Yinzuo Jiang | fc762df | 2024-08-14 21:49:00 +0200 | [diff] [blame] | 6 | " 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 Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 9 | " Download: http://vim.sourceforge.net/script.php?script_id=454 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10 | |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 11 | " For more details please use: |
| 12 | " :h sql.txt |
| 13 | " |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 14 | " 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 Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 17 | " vimfiles/after/ftplugin/sql.vim (Windows) |
| 18 | " .vim/after/ftplugin/sql.vim (Unix) |
| 19 | " to override and add any of your own settings. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 20 | |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 21 | |
| 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 Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 35 | " |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 36 | " This file also creates a command, SQLGetType, which allows you to |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 37 | " determine what the current dialect is in use. |
| 38 | " :SQLGetType |
| 39 | " |
| 40 | " History |
| 41 | " |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 42 | " 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 Moolenaar | ad3b366 | 2013-05-17 18:14:19 +0200 | [diff] [blame] | 50 | " Version 11.0 (May 2013) |
| 51 | " |
| 52 | " NF: Updated to use SyntaxComplete's new regex support for syntax groups. |
| 53 | " |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 54 | " 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 Moolenaar | adc2182 | 2011-04-01 18:03:16 +0200 | [diff] [blame] | 68 | " Version 8.0 |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 69 | " |
Bram Moolenaar | adc2182 | 2011-04-01 18:03:16 +0200 | [diff] [blame] | 70 | " NF: Improved the matchit plugin regex (Talek) |
| 71 | " |
Bram Moolenaar | f9d5ca1 | 2010-08-01 16:13:51 +0200 | [diff] [blame] | 72 | " Version 7.0 |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 73 | " |
Bram Moolenaar | f9d5ca1 | 2010-08-01 16:13:51 +0200 | [diff] [blame] | 74 | " NF: Calls the sqlcomplete#ResetCacheSyntax() function when calling |
| 75 | " SQLSetType. |
| 76 | " |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 77 | " Version 6.0 |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 78 | " |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 79 | " NF: Adds the command SQLGetType |
| 80 | " |
| 81 | " Version 5.0 |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 82 | " |
| 83 | " 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] | 84 | " 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 Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 89 | " BF: format-options - Auto-wrap comments using textwidth was turned off |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 90 | " by mistake. |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 91 | |
| 92 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 93 | " Only do this when not done yet for this buffer |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 94 | " This ftplugin can be used with other ftplugins. So ensure loading |
| 95 | " happens if all elements of this plugin have not yet loaded. |
Yinzuo Jiang | fc762df | 2024-08-14 21:49:00 +0200 | [diff] [blame] | 96 | if exists("b:did_ftplugin") |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 97 | finish |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 98 | endif |
| 99 | |
Yinzuo Jiang | fc762df | 2024-08-14 21:49:00 +0200 | [diff] [blame] | 100 | " Don't load another plugin for this buffer |
| 101 | let b:did_ftplugin = 1 |
| 102 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 103 | let s:save_cpo = &cpo |
Bram Moolenaar | 8e52a59 | 2012-05-18 21:49:28 +0200 | [diff] [blame] | 104 | set cpo&vim |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 105 | |
Yinzuo Jiang | fc762df | 2024-08-14 21:49:00 +0200 | [diff] [blame] | 106 | let b:undo_ftplugin = "setl comments< commentstring< formatoptions< define< omnifunc<" . |
| 107 | \ " | unlet! b:browsefilter b:match_words" |
| 108 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 109 | " Disable autowrapping for code, but enable for comments |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 110 | " t Auto-wrap text using textwidth |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 111 | " c Auto-wrap comments using textwidth, inserting the current comment |
| 112 | " leader automatically. |
| 113 | setlocal formatoptions-=t |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 114 | setlocal formatoptions+=c |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 115 | |
Riley Bruins | df859a3 | 2024-05-19 09:23:10 +0200 | [diff] [blame] | 116 | setlocal comments=:-- commentstring=--\ %s |
| 117 | |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 118 | " 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 | |
| 122 | if !exists("*SQL_SetType") |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 123 | " NOTE: You cannot use function! since this file can be |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 124 | " 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 Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 147 | let sqls = substitute( sqls, |
| 148 | \ '[\n]\%(.\{-}\)\(\w\+\.\w\+\)\n\@=', |
| 149 | \ '\1\n', |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 150 | \ 'g' |
| 151 | \ ) |
| 152 | |
| 153 | " Remove duplicates, since sqlanywhere.vim can exist in the |
Viktor Szépe | 3fc7a7e | 2023-08-23 21:20:00 +0200 | [diff] [blame] | 154 | " syntax, indent and ftplugin directory, yet we only want |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 155 | " 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 Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 184 | " specify a vendor specific version |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 185 | " (ie Oracle, Informix, SQL Anywhere, ...) |
| 186 | " So check for an remove any settings that prevent the |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 187 | " scripts from being executed, and then source the |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 188 | " 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 Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 195 | if exists("b:current_syntax") |
| 196 | unlet b:current_syntax |
| 197 | endif |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 198 | 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 Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 208 | let new_sql_type = substitute(a:name, |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 209 | \ '\s*\([^\.]\+\)\(\.\w\+\)\?', '\L\1', '') |
| 210 | |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 211 | " Do not specify a buffer local variable if it is |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 212 | " the default value |
| 213 | if new_sql_type == 'sql' |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 214 | let new_sql_type = 'sqloracle' |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 215 | endif |
| 216 | let b:sql_type_override = new_sql_type |
| 217 | |
Viktor Szépe | 3fc7a7e | 2023-08-23 21:20:00 +0200 | [diff] [blame] | 218 | " Remove any cached SQL since a new syntax will have different |
Bram Moolenaar | f9d5ca1 | 2010-08-01 16:13:51 +0200 | [diff] [blame] | 219 | " items and groups |
Bram Moolenaar | ad3b366 | 2013-05-17 18:14:19 +0200 | [diff] [blame] | 220 | if !exists('g:loaded_sql_completion') || g:loaded_sql_completion >= 100 |
Bram Moolenaar | f9d5ca1 | 2010-08-01 16:13:51 +0200 | [diff] [blame] | 221 | call sqlcomplete#ResetCacheSyntax() |
| 222 | endif |
| 223 | |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 224 | " 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 Moolenaar | f9d5ca1 | 2010-08-01 16:13:51 +0200 | [diff] [blame] | 230 | |
| 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 Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 238 | endfunction |
| 239 | command! -nargs=* -complete=custom,SQL_GetList SQLSetType :call SQL_SetType(<q-args>) |
| 240 | |
| 241 | endif |
| 242 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 243 | " Functions/Commands to allow the user determine current SQL syntax dialect |
| 244 | " This works with both Vim 6 and 7. |
| 245 | |
| 246 | if !exists("*SQL_GetType") |
| 247 | function SQL_GetType() |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 248 | if exists('b:sql_type_override') |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 249 | echomsg "Current SQL dialect in use:".b:sql_type_override |
| 250 | else |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 251 | echomsg "Current SQL dialect in use:".g:sql_type_default |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 252 | endif |
| 253 | endfunction |
| 254 | command! -nargs=0 SQLGetType :call SQL_GetType() |
| 255 | endif |
| 256 | |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 257 | if 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 Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 261 | " else |
| 262 | " echomsg 'ftplugin/'.b:sql_type_override.' not exist, using default' |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 263 | endif |
| 264 | elseif 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 Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 268 | " else |
| 269 | " echomsg 'ftplugin/'.g:sql_type_default.'.vim not exist, using default' |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 270 | endif |
| 271 | endif |
| 272 | |
Doug Kearns | 93197fd | 2024-01-14 20:59:02 +0100 | [diff] [blame] | 273 | " Win32 and GTK can filter files in the browse dialog |
| 274 | if (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 Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 281 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 282 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 283 | " Some standard expressions for use with the matchit strings |
| 284 | let s:notend = '\%(\<end\s\+\)\@<!' |
| 285 | let s:when_no_matched_or_others = '\%(\<when\>\%(\s\+\%(\%(\<not\>\s\+\)\?<matched\>\)\|\<others\>\)\@!\)' |
| 286 | let s:or_replace = '\%(or\s\+replace\s\+\)\?' |
| 287 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 288 | " Define patterns for the matchit macro |
| 289 | if !exists("b:match_words") |
| 290 | " SQL is generally case insensitive |
| 291 | let b:match_ignorecase = 1 |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 292 | |
| 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 Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 318 | " when |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 319 | " 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 Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 331 | " begin try |
| 332 | " end try |
| 333 | " begin catch |
| 334 | " end catch |
| 335 | " |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 336 | " create[ or replace] procedure|function|event |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 337 | " \ '^\s*\<\%(do\|for\|while\|loop\)\>.*:'. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 338 | |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 339 | " For ColdFusion support |
| 340 | setlocal matchpairs+=<:> |
| 341 | let b:match_words = &matchpairs . |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 342 | \ ',\%(\<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 Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 349 | \ s:notend . '\<if\>:'. |
| 350 | \ '\<elsif\>\|\<elseif\>\|\<else\>:'. |
| 351 | \ '\<end\s\+if\>,'. |
| 352 | \ |
Bram Moolenaar | adc2182 | 2011-04-01 18:03:16 +0200 | [diff] [blame] | 353 | \ '\(^\s*\)\@<=\(\<\%(do\|for\|while\|loop\)\>.*\):'. |
| 354 | \ '\%(\<exit\>\|\<leave\>\|\<break\>\|\<continue\>\):'. |
| 355 | \ '\%(\<doend\>\|\%(\<end\s\+\%(for\|while\|loop\>\)\)\),'. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 356 | \ |
| 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 Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 368 | " \ '\<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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 376 | endif |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 377 | |
| 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 Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 382 | let &l:define = '\c\<\(VARIABLE\|DECLARE\|IN\|OUT\|INOUT\)\>' |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 383 | |
| 384 | |
| 385 | " Mappings to move to the next BEGIN ... END block |
| 386 | " \W - no characters or digits |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 387 | nnoremap <buffer> <silent> ]] :call search('\c^\s*begin\>', 'W' )<CR> |
| 388 | nnoremap <buffer> <silent> [[ :call search('\c^\s*begin\>', 'bW' )<CR> |
| 389 | nnoremap <buffer> <silent> ][ :call search('\c^\s*end\W*$', 'W' )<CR> |
| 390 | nnoremap <buffer> <silent> [] :call search('\c^\s*end\W*$', 'bW' )<CR> |
| 391 | xnoremap <buffer> <silent> ]] :<C-U>exec "normal! gv"<Bar>call search('\c^\s*begin\>', 'W' )<CR> |
| 392 | xnoremap <buffer> <silent> [[ :<C-U>exec "normal! gv"<Bar>call search('\c^\s*begin\>', 'bW' )<CR> |
| 393 | xnoremap <buffer> <silent> ][ :<C-U>exec "normal! gv"<Bar>call search('\c^\s*end\W*$', 'W' )<CR> |
| 394 | 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] | 395 | |
| 396 | |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 397 | " By default only look for CREATE statements, but allow |
| 398 | " the user to override |
| 399 | if !exists('g:ftplugin_sql_statements') |
| 400 | let g:ftplugin_sql_statements = 'create' |
| 401 | endif |
| 402 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 403 | " Predefined SQL objects what are used by the below mappings using |
| 404 | " the ]} style maps. |
Bram Moolenaar | a6c27c4 | 2019-05-09 19:16:22 +0200 | [diff] [blame] | 405 | " This global variable allows the users to override its value |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 406 | " from within their vimrc. |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 407 | " Note, you cannot use \?, since these patterns can be used to search |
| 408 | " backwards, you must use \{,1} |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 409 | if !exists('g:ftplugin_sql_objects') |
| 410 | let g:ftplugin_sql_objects = 'function,procedure,event,' . |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 411 | \ '\(existing\\|global\s\+temporary\s\+\)\{,1}' . |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 412 | \ 'table,trigger' . |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 413 | \ ',schema,service,publication,database,datatype,domain' . |
| 414 | \ ',index,subscription,synchronization,view,variable' |
| 415 | endif |
| 416 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 417 | " Key to trigger SQL completion |
| 418 | if !exists('g:ftplugin_sql_omni_key') |
| 419 | let g:ftplugin_sql_omni_key = '<C-C>' |
| 420 | endif |
| 421 | " Key to trigger drill into column list |
| 422 | if !exists('g:ftplugin_sql_omni_key_right') |
| 423 | let g:ftplugin_sql_omni_key_right = '<Right>' |
| 424 | endif |
| 425 | " Key to trigger drill out of column list |
| 426 | if !exists('g:ftplugin_sql_omni_key_left') |
| 427 | let g:ftplugin_sql_omni_key_left = '<Left>' |
| 428 | endif |
| 429 | |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 430 | " Replace all ,'s with bars, except ones with numbers after them. |
| 431 | " This will most likely be a \{,1} string. |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 432 | let 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 Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 440 | |
| 441 | " Mappings to move to the next CREATE ... block |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 442 | exec "nnoremap <buffer> <silent> ]} :call search('".s:ftplugin_sql_objects."', 'W')<CR>" |
| 443 | exec "nnoremap <buffer> <silent> [{ :call search('".s:ftplugin_sql_objects."', 'bW')<CR>" |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 444 | " Could not figure out how to use a :call search() string in visual mode |
| 445 | " without it ending visual mode |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 446 | " Unfortunately, this will add a entry to the search history |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 447 | exec 'xnoremap <buffer> <silent> ]} /'.s:ftplugin_sql_objects.'<CR>' |
| 448 | exec 'xnoremap <buffer> <silent> [{ ?'.s:ftplugin_sql_objects.'<CR>' |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 449 | |
| 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 Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 454 | let b:comment_leader = '\(--\\|\/\/\\|\*\\|\/\*\\|\*\/\)' |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 455 | " Find the start of the next comment |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 456 | let b:comment_start = '^\(\s*'.b:comment_leader.'.*\n\)\@<!'. |
| 457 | \ '\(\s*'.b:comment_leader.'\)' |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 458 | " Find the end of the previous comment |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 459 | let b:comment_end = '\(^\s*'.b:comment_leader.'.*\n\)'. |
| 460 | \ '\(^\s*'.b:comment_leader.'\)\@!' |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 461 | " Skip over the comment |
| 462 | let b:comment_jump_over = "call search('". |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 463 | \ '^\(\s*'.b:comment_leader.'.*\n\)\@<!'. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 464 | \ "', 'W')" |
| 465 | let b:comment_skip_back = "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 | \ "', 'bW')" |
| 468 | " Move to the start and end of comments |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 469 | exec 'nnoremap <silent><buffer> ]" :call search('."'".b:comment_start."'".', "W" )<CR>' |
| 470 | exec 'nnoremap <silent><buffer> [" :call search('."'".b:comment_end."'".', "W" )<CR>' |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 471 | exec 'xnoremap <silent><buffer> ]" :<C-U>exec "normal! gv"<Bar>call search('."'".b:comment_start."'".', "W" )<CR>' |
| 472 | 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] | 473 | |
| 474 | " Comments can be of the form: |
| 475 | " /* |
| 476 | " * |
| 477 | " */ |
| 478 | " or |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 479 | " -- |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 480 | " or |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 481 | " // |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 482 | setlocal comments=s1:/*,mb:*,ex:*/,:--,:// |
| 483 | |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 484 | " Set completion with CTRL-X CTRL-O to autoloaded function. |
| 485 | if exists('&omnifunc') |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 486 | " 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 Moolenaar | a6c27c4 | 2019-05-09 19:16:22 +0200 | [diff] [blame] | 491 | " Source it to determine its version |
Bram Moolenaar | ad3b366 | 2013-05-17 18:14:19 +0200 | [diff] [blame] | 492 | runtime autoload/sqlcomplete.vim |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 493 | " This is used by the sqlcomplete.vim plugin |
Bram Moolenaar | a6c27c4 | 2019-05-09 19:16:22 +0200 | [diff] [blame] | 494 | " Source it for its global functions |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 495 | runtime autoload/syntaxcomplete.vim |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 496 | |
| 497 | setlocal omnifunc=sqlcomplete#Complete |
| 498 | " Prevent the intellisense plugin from loading |
| 499 | let b:sql_vis = 1 |
Bram Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame] | 500 | if !exists('g:omni_sql_no_default_maps') |
Bram Moolenaar | ad3b366 | 2013-05-17 18:14:19 +0200 | [diff] [blame] | 501 | 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 Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame] | 507 | " Static maps which use populate the completion list |
| 508 | " using Vim's syntax highlighting rules |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 509 | 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] | 510 | 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 Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame] | 515 | " Dynamic maps which use populate the completion list |
| 516 | " using the dbext.vim plugin |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 517 | 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 Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame] | 522 | " 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 Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 524 | 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] | 525 | " <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 Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 529 | " if has('win32') |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 530 | 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 Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 532 | " endif |
Bram Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame] | 533 | " Remove any cached items useful for schema changes |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 534 | 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] | 535 | 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 Moolenaar | f9d5ca1 | 2010-08-01 16:13:51 +0200 | [diff] [blame] | 542 | call sqlcomplete#ResetCacheSyntax() |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 543 | call sqlcomplete#PreCacheSyntax() |
Bram Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame] | 544 | endif |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 545 | endif |
| 546 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 547 | let &cpo = s:save_cpo |
Bram Moolenaar | 84f7235 | 2012-03-11 15:57:40 +0100 | [diff] [blame] | 548 | unlet s:save_cpo |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 549 | |
Bram Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame] | 550 | " vim:sw=4: |