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 |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 6 | " Download: http://vim.sourceforge.net/script.php?script_id=454 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7 | |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 8 | " For more details please use: |
| 9 | " :h sql.txt |
| 10 | " |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11 | " This file should only contain values that are common to all SQL languages |
| 12 | " Oracle, Microsoft SQL Server, Sybase ASA/ASE, MySQL, and so on |
| 13 | " If additional features are required create: |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 14 | " vimfiles/after/ftplugin/sql.vim (Windows) |
| 15 | " .vim/after/ftplugin/sql.vim (Unix) |
| 16 | " to override and add any of your own settings. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 17 | |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 18 | |
| 19 | " This file also creates a command, SQLSetType, which allows you to change |
| 20 | " SQL dialects on the fly. For example, if I open an Oracle SQL file, it |
| 21 | " is color highlighted appropriately. If I open an Informix SQL file, it |
| 22 | " will still be highlighted according to Oracles settings. By running: |
| 23 | " :SQLSetType sqlinformix |
| 24 | " |
| 25 | " All files called sqlinformix.vim will be loaded from the indent and syntax |
| 26 | " directories. This allows you to easily flip SQL dialects on a per file |
| 27 | " basis. NOTE: you can also use completion: |
| 28 | " :SQLSetType <tab> |
| 29 | " |
| 30 | " To change the default dialect, add the following to your vimrc: |
| 31 | " let g:sql_type_default = 'sqlanywhere' |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 32 | " |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 33 | " This file also creates a command, SQLGetType, which allows you to |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 34 | " determine what the current dialect is in use. |
| 35 | " :SQLGetType |
| 36 | " |
| 37 | " History |
| 38 | " |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 39 | " Version 12.0 (April 2013) |
| 40 | " |
| 41 | " NF: Added support for "BEGIN TRY ... END TRY ... BEGIN CATCH ... END CATCH |
| 42 | " BF: This plugin is designed to be used with other plugins to enable the |
| 43 | " SQL completion with Perl, Python, Java, ... The loading mechanism |
| 44 | " was not checking if the SQL objects were created, which can lead to |
| 45 | " the plugin not loading the SQL support. |
| 46 | " |
Bram Moolenaar | ad3b366 | 2013-05-17 18:14:19 +0200 | [diff] [blame] | 47 | " Version 11.0 (May 2013) |
| 48 | " |
| 49 | " NF: Updated to use SyntaxComplete's new regex support for syntax groups. |
| 50 | " |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 51 | " Version 10.0 (Dec 2012) |
| 52 | " |
| 53 | " NF: Changed all maps to use noremap instead of must map |
| 54 | " NF: Changed all visual maps to use xnoremap instead of vnoremap as they |
| 55 | " should only be used in visual mode and not select mode. |
| 56 | " BF: Most of the maps were using doubled up backslashes before they were |
| 57 | " changed to using the search() function, which meant they no longer |
| 58 | " worked. |
| 59 | " |
| 60 | " Version 9.0 |
| 61 | " |
| 62 | " NF: Completes 'b:undo_ftplugin' |
| 63 | " BF: Correctly set cpoptions when creating script |
| 64 | " |
Bram Moolenaar | adc2182 | 2011-04-01 18:03:16 +0200 | [diff] [blame] | 65 | " Version 8.0 |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 66 | " |
Bram Moolenaar | adc2182 | 2011-04-01 18:03:16 +0200 | [diff] [blame] | 67 | " NF: Improved the matchit plugin regex (Talek) |
| 68 | " |
Bram Moolenaar | f9d5ca1 | 2010-08-01 16:13:51 +0200 | [diff] [blame] | 69 | " Version 7.0 |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 70 | " |
Bram Moolenaar | f9d5ca1 | 2010-08-01 16:13:51 +0200 | [diff] [blame] | 71 | " NF: Calls the sqlcomplete#ResetCacheSyntax() function when calling |
| 72 | " SQLSetType. |
| 73 | " |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 74 | " Version 6.0 |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 75 | " |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 76 | " NF: Adds the command SQLGetType |
| 77 | " |
| 78 | " Version 5.0 |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 79 | " |
| 80 | " 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] | 81 | " the following to your .vimrc: |
| 82 | " let g:ftplugin_sql_omni_key = '<C-C>' |
| 83 | " let g:ftplugin_sql_omni_key_right = '<Right>' |
| 84 | " let g:ftplugin_sql_omni_key_left = '<Left>' |
| 85 | " |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 86 | " BF: format-options - Auto-wrap comments using textwidth was turned off |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 87 | " by mistake. |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 88 | |
| 89 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 90 | " Only do this when not done yet for this buffer |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 91 | " This ftplugin can be used with other ftplugins. So ensure loading |
| 92 | " happens if all elements of this plugin have not yet loaded. |
| 93 | if exists("b:did_ftplugin") && exists("b:current_ftplugin") && b:current_ftplugin == 'sql' |
| 94 | finish |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 95 | endif |
| 96 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 97 | let s:save_cpo = &cpo |
Bram Moolenaar | 8e52a59 | 2012-05-18 21:49:28 +0200 | [diff] [blame] | 98 | set cpo&vim |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 99 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 100 | " Disable autowrapping for code, but enable for comments |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 101 | " t Auto-wrap text using textwidth |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 102 | " c Auto-wrap comments using textwidth, inserting the current comment |
| 103 | " leader automatically. |
| 104 | setlocal formatoptions-=t |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 105 | setlocal formatoptions+=c |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 106 | |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 107 | " Functions/Commands to allow the user to change SQL syntax dialects |
| 108 | " through the use of :SQLSetType <tab> for completion. |
| 109 | " This works with both Vim 6 and 7. |
| 110 | |
| 111 | if !exists("*SQL_SetType") |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 112 | " NOTE: You cannot use function! since this file can be |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 113 | " sourced from within this function. That will result in |
| 114 | " an error reported by Vim. |
| 115 | function SQL_GetList(ArgLead, CmdLine, CursorPos) |
| 116 | |
| 117 | if !exists('s:sql_list') |
| 118 | " Grab a list of files that contain "sql" in their names |
| 119 | let list_indent = globpath(&runtimepath, 'indent/*sql*') |
| 120 | let list_syntax = globpath(&runtimepath, 'syntax/*sql*') |
| 121 | let list_ftplugin = globpath(&runtimepath, 'ftplugin/*sql*') |
| 122 | |
| 123 | let sqls = "\n".list_indent."\n".list_syntax."\n".list_ftplugin."\n" |
| 124 | |
| 125 | " Strip out everything (path info) but the filename |
| 126 | " Regex |
| 127 | " From between two newline characters |
| 128 | " Non-greedily grab all characters |
| 129 | " Followed by a valid filename \w\+\.\w\+ (sql.vim) |
| 130 | " Followed by a newline, but do not include the newline |
| 131 | " |
| 132 | " Replace it with just the filename (get rid of PATH) |
| 133 | " |
| 134 | " Recursively, since there are many filenames that contain |
| 135 | " the word SQL in the indent, syntax and ftplugin directory |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 136 | let sqls = substitute( sqls, |
| 137 | \ '[\n]\%(.\{-}\)\(\w\+\.\w\+\)\n\@=', |
| 138 | \ '\1\n', |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 139 | \ 'g' |
| 140 | \ ) |
| 141 | |
| 142 | " Remove duplicates, since sqlanywhere.vim can exist in the |
| 143 | " sytax, indent and ftplugin directory, yet we only want |
| 144 | " to display the option once |
| 145 | let index = match(sqls, '.\{-}\ze\n') |
| 146 | while index > -1 |
| 147 | " Get the first filename |
| 148 | let file = matchstr(sqls, '.\{-}\ze\n', index) |
| 149 | " Recursively replace any *other* occurrence of that |
| 150 | " filename with nothing (ie remove it) |
| 151 | let sqls = substitute(sqls, '\%>'.(index+strlen(file)).'c\<'.file.'\>\n', '', 'g') |
| 152 | " Move on to the next filename |
| 153 | let index = match(sqls, '.\{-}\ze\n', (index+strlen(file)+1)) |
| 154 | endwhile |
| 155 | |
| 156 | " Sort the list if using version 7 |
| 157 | if v:version >= 700 |
| 158 | let mylist = split(sqls, "\n") |
| 159 | let mylist = sort(mylist) |
| 160 | let sqls = join(mylist, "\n") |
| 161 | endif |
| 162 | |
| 163 | let s:sql_list = sqls |
| 164 | endif |
| 165 | |
| 166 | return s:sql_list |
| 167 | |
| 168 | endfunction |
| 169 | |
| 170 | function SQL_SetType(name) |
| 171 | |
| 172 | " User has decided to override default SQL scripts and |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 173 | " specify a vendor specific version |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 174 | " (ie Oracle, Informix, SQL Anywhere, ...) |
| 175 | " So check for an remove any settings that prevent the |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 176 | " scripts from being executed, and then source the |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 177 | " appropriate Vim scripts. |
| 178 | if exists("b:did_ftplugin") |
| 179 | unlet b:did_ftplugin |
| 180 | endif |
| 181 | if exists("b:current_syntax") |
| 182 | " echomsg 'SQLSetType - clearing syntax' |
| 183 | syntax clear |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 184 | if exists("b:current_syntax") |
| 185 | unlet b:current_syntax |
| 186 | endif |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 187 | endif |
| 188 | if exists("b:did_indent") |
| 189 | " echomsg 'SQLSetType - clearing indent' |
| 190 | unlet b:did_indent |
| 191 | " Set these values to their defaults |
| 192 | setlocal indentkeys& |
| 193 | setlocal indentexpr& |
| 194 | endif |
| 195 | |
| 196 | " Ensure the name is in the correct format |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 197 | let new_sql_type = substitute(a:name, |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 198 | \ '\s*\([^\.]\+\)\(\.\w\+\)\?', '\L\1', '') |
| 199 | |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 200 | " Do not specify a buffer local variable if it is |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 201 | " the default value |
| 202 | if new_sql_type == 'sql' |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 203 | let new_sql_type = 'sqloracle' |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 204 | endif |
| 205 | let b:sql_type_override = new_sql_type |
| 206 | |
Bram Moolenaar | f9d5ca1 | 2010-08-01 16:13:51 +0200 | [diff] [blame] | 207 | " Remove any cached SQL since a new sytax will have different |
| 208 | " items and groups |
Bram Moolenaar | ad3b366 | 2013-05-17 18:14:19 +0200 | [diff] [blame] | 209 | if !exists('g:loaded_sql_completion') || g:loaded_sql_completion >= 100 |
Bram Moolenaar | f9d5ca1 | 2010-08-01 16:13:51 +0200 | [diff] [blame] | 210 | call sqlcomplete#ResetCacheSyntax() |
| 211 | endif |
| 212 | |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 213 | " Vim will automatically source the correct files if we |
| 214 | " change the filetype. You cannot do this with setfiletype |
| 215 | " since that command will only execute if a filetype has |
| 216 | " not already been set. In this case we want to override |
| 217 | " the existing filetype. |
| 218 | let &filetype = 'sql' |
Bram Moolenaar | f9d5ca1 | 2010-08-01 16:13:51 +0200 | [diff] [blame] | 219 | |
| 220 | if b:sql_compl_savefunc != "" |
| 221 | " We are changing the filetype to SQL from some other filetype |
| 222 | " which had OMNI completion defined. We need to activate the |
| 223 | " SQL completion plugin in order to cache some of the syntax items |
| 224 | " while the syntax rules for SQL are active. |
| 225 | call sqlcomplete#PreCacheSyntax() |
| 226 | endif |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 227 | endfunction |
| 228 | command! -nargs=* -complete=custom,SQL_GetList SQLSetType :call SQL_SetType(<q-args>) |
| 229 | |
| 230 | endif |
| 231 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 232 | " Functions/Commands to allow the user determine current SQL syntax dialect |
| 233 | " This works with both Vim 6 and 7. |
| 234 | |
| 235 | if !exists("*SQL_GetType") |
| 236 | function SQL_GetType() |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 237 | if exists('b:sql_type_override') |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 238 | echomsg "Current SQL dialect in use:".b:sql_type_override |
| 239 | else |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 240 | echomsg "Current SQL dialect in use:".g:sql_type_default |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 241 | endif |
| 242 | endfunction |
| 243 | command! -nargs=0 SQLGetType :call SQL_GetType() |
| 244 | endif |
| 245 | |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 246 | if exists("b:sql_type_override") |
| 247 | " echo 'sourcing buffer ftplugin/'.b:sql_type_override.'.vim' |
| 248 | if globpath(&runtimepath, 'ftplugin/'.b:sql_type_override.'.vim') != '' |
| 249 | exec 'runtime ftplugin/'.b:sql_type_override.'.vim' |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 250 | " else |
| 251 | " echomsg 'ftplugin/'.b:sql_type_override.' not exist, using default' |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 252 | endif |
| 253 | elseif exists("g:sql_type_default") |
| 254 | " echo 'sourcing global ftplugin/'.g:sql_type_default.'.vim' |
| 255 | if globpath(&runtimepath, 'ftplugin/'.g:sql_type_default.'.vim') != '' |
| 256 | exec 'runtime ftplugin/'.g:sql_type_default.'.vim' |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 257 | " else |
| 258 | " echomsg 'ftplugin/'.g:sql_type_default.'.vim not exist, using default' |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 259 | endif |
| 260 | endif |
| 261 | |
| 262 | " If the above runtime command succeeded, do not load the default settings |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 263 | " as they should have already been loaded from a previous run. |
| 264 | if exists("b:did_ftplugin") && exists("b:current_ftplugin") && b:current_ftplugin == 'sql' |
| 265 | finish |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 266 | endif |
| 267 | |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 268 | let b:undo_ftplugin = "setl comments< formatoptions< define< omnifunc<" . |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 269 | \ " | unlet! b:browsefilter b:match_words" |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 270 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 271 | " Don't load another plugin for this buffer |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 272 | let b:did_ftplugin = 1 |
| 273 | let b:current_ftplugin = 'sql' |
| 274 | |
| 275 | " Win32 can filter files in the browse dialog |
| 276 | if has("gui_win32") && !exists("b:browsefilter") |
| 277 | let b:browsefilter = "SQL Files (*.sql)\t*.sql\n" . |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 278 | \ "All Files (*.*)\t*.*\n" |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 279 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 280 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 281 | " Some standard expressions for use with the matchit strings |
| 282 | let s:notend = '\%(\<end\s\+\)\@<!' |
| 283 | let s:when_no_matched_or_others = '\%(\<when\>\%(\s\+\%(\%(\<not\>\s\+\)\?<matched\>\)\|\<others\>\)\@!\)' |
| 284 | let s:or_replace = '\%(or\s\+replace\s\+\)\?' |
| 285 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 286 | " Define patterns for the matchit macro |
| 287 | if !exists("b:match_words") |
| 288 | " SQL is generally case insensitive |
| 289 | let b:match_ignorecase = 1 |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 290 | |
| 291 | " Handle the following: |
| 292 | " if |
| 293 | " elseif | elsif |
| 294 | " else [if] |
| 295 | " end if |
| 296 | " |
| 297 | " [while condition] loop |
| 298 | " leave |
| 299 | " break |
| 300 | " continue |
| 301 | " exit |
| 302 | " end loop |
| 303 | " |
| 304 | " for |
| 305 | " leave |
| 306 | " break |
| 307 | " continue |
| 308 | " exit |
| 309 | " end loop |
| 310 | " |
| 311 | " do |
| 312 | " statements |
| 313 | " doend |
| 314 | " |
| 315 | " case |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 316 | " when |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 317 | " when |
| 318 | " default |
| 319 | " end case |
| 320 | " |
| 321 | " merge |
| 322 | " when not matched |
| 323 | " when matched |
| 324 | " |
| 325 | " EXCEPTION |
| 326 | " WHEN column_not_found THEN |
| 327 | " WHEN OTHERS THEN |
| 328 | " |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 329 | " begin try |
| 330 | " end try |
| 331 | " begin catch |
| 332 | " end catch |
| 333 | " |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 334 | " create[ or replace] procedure|function|event |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 335 | " \ '^\s*\<\%(do\|for\|while\|loop\)\>.*:'. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 336 | |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 337 | " For ColdFusion support |
| 338 | setlocal matchpairs+=<:> |
| 339 | let b:match_words = &matchpairs . |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 340 | \ ',\%(\<begin\)\%(\s\+\%(try\|catch\)\>\)\@!:\<end\>\W*$,'. |
| 341 | \ |
| 342 | \ '\<begin\s\+try\>:'. |
| 343 | \ '\<end\s\+try\>:'. |
| 344 | \ '\<begin\s\+catch\>:'. |
| 345 | \ '\<end\s\+catch\>,'. |
| 346 | \ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 347 | \ s:notend . '\<if\>:'. |
| 348 | \ '\<elsif\>\|\<elseif\>\|\<else\>:'. |
| 349 | \ '\<end\s\+if\>,'. |
| 350 | \ |
Bram Moolenaar | adc2182 | 2011-04-01 18:03:16 +0200 | [diff] [blame] | 351 | \ '\(^\s*\)\@<=\(\<\%(do\|for\|while\|loop\)\>.*\):'. |
| 352 | \ '\%(\<exit\>\|\<leave\>\|\<break\>\|\<continue\>\):'. |
| 353 | \ '\%(\<doend\>\|\%(\<end\s\+\%(for\|while\|loop\>\)\)\),'. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 354 | \ |
| 355 | \ '\%('. s:notend . '\<case\>\):'. |
| 356 | \ '\%('.s:when_no_matched_or_others.'\):'. |
| 357 | \ '\%(\<when\s\+others\>\|\<end\s\+case\>\),' . |
| 358 | \ |
| 359 | \ '\<merge\>:' . |
| 360 | \ '\<when\s\+not\s\+matched\>:' . |
| 361 | \ '\<when\s\+matched\>,' . |
| 362 | \ |
| 363 | \ '\%(\<create\s\+' . s:or_replace . '\)\?'. |
| 364 | \ '\%(function\|procedure\|event\):'. |
| 365 | \ '\<returns\?\>' |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 366 | " \ '\<begin\>\|\<returns\?\>:'. |
| 367 | " \ '\<end\>\(;\)\?\s*$' |
| 368 | " \ '\<exception\>:'.s:when_no_matched_or_others. |
| 369 | " \ ':\<when\s\+others\>,'. |
| 370 | " |
| 371 | " \ '\%(\<exception\>\|\%('. s:notend . '\<case\>\)\):'. |
| 372 | " \ '\%(\<default\>\|'.s:when_no_matched_or_others.'\):'. |
| 373 | " \ '\%(\%(\<when\s\+others\>\)\|\<end\s\+case\>\),' . |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 374 | endif |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 375 | |
| 376 | " Define how to find the macro definition of a variable using the various |
| 377 | " [d, [D, [_CTRL_D and so on features |
| 378 | " Match these values ignoring case |
| 379 | " ie DECLARE varname INTEGER |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 380 | let &l:define = '\c\<\(VARIABLE\|DECLARE\|IN\|OUT\|INOUT\)\>' |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 381 | |
| 382 | |
| 383 | " Mappings to move to the next BEGIN ... END block |
| 384 | " \W - no characters or digits |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 385 | nnoremap <buffer> <silent> ]] :call search('\c^\s*begin\>', 'W' )<CR> |
| 386 | nnoremap <buffer> <silent> [[ :call search('\c^\s*begin\>', 'bW' )<CR> |
| 387 | nnoremap <buffer> <silent> ][ :call search('\c^\s*end\W*$', 'W' )<CR> |
| 388 | nnoremap <buffer> <silent> [] :call search('\c^\s*end\W*$', 'bW' )<CR> |
| 389 | xnoremap <buffer> <silent> ]] :<C-U>exec "normal! gv"<Bar>call search('\c^\s*begin\>', 'W' )<CR> |
| 390 | xnoremap <buffer> <silent> [[ :<C-U>exec "normal! gv"<Bar>call search('\c^\s*begin\>', 'bW' )<CR> |
| 391 | xnoremap <buffer> <silent> ][ :<C-U>exec "normal! gv"<Bar>call search('\c^\s*end\W*$', 'W' )<CR> |
| 392 | 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] | 393 | |
| 394 | |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 395 | " By default only look for CREATE statements, but allow |
| 396 | " the user to override |
| 397 | if !exists('g:ftplugin_sql_statements') |
| 398 | let g:ftplugin_sql_statements = 'create' |
| 399 | endif |
| 400 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 401 | " Predefined SQL objects what are used by the below mappings using |
| 402 | " the ]} style maps. |
| 403 | " This global variable allows the users to override it's value |
| 404 | " from within their vimrc. |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 405 | " Note, you cannot use \?, since these patterns can be used to search |
| 406 | " backwards, you must use \{,1} |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 407 | if !exists('g:ftplugin_sql_objects') |
| 408 | let g:ftplugin_sql_objects = 'function,procedure,event,' . |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 409 | \ '\(existing\\|global\s\+temporary\s\+\)\{,1}' . |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 410 | \ 'table,trigger' . |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 411 | \ ',schema,service,publication,database,datatype,domain' . |
| 412 | \ ',index,subscription,synchronization,view,variable' |
| 413 | endif |
| 414 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 415 | " Key to trigger SQL completion |
| 416 | if !exists('g:ftplugin_sql_omni_key') |
| 417 | let g:ftplugin_sql_omni_key = '<C-C>' |
| 418 | endif |
| 419 | " Key to trigger drill into column list |
| 420 | if !exists('g:ftplugin_sql_omni_key_right') |
| 421 | let g:ftplugin_sql_omni_key_right = '<Right>' |
| 422 | endif |
| 423 | " Key to trigger drill out of column list |
| 424 | if !exists('g:ftplugin_sql_omni_key_left') |
| 425 | let g:ftplugin_sql_omni_key_left = '<Left>' |
| 426 | endif |
| 427 | |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 428 | " Replace all ,'s with bars, except ones with numbers after them. |
| 429 | " This will most likely be a \{,1} string. |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 430 | let s:ftplugin_sql_objects = |
| 431 | \ '\c^\s*' . |
| 432 | \ '\(\(' . |
| 433 | \ substitute(g:ftplugin_sql_statements, ',\d\@!', '\\\\|', 'g') . |
| 434 | \ '\)\s\+\(or\s\+replace\s\+\)\{,1}\)\{,1}' . |
| 435 | \ '\<\(' . |
| 436 | \ substitute(g:ftplugin_sql_objects, ',\d\@!', '\\\\|', 'g') . |
| 437 | \ '\)\>' |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 438 | |
| 439 | " Mappings to move to the next CREATE ... block |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 440 | exec "nnoremap <buffer> <silent> ]} :call search('".s:ftplugin_sql_objects."', 'W')<CR>" |
| 441 | exec "nnoremap <buffer> <silent> [{ :call search('".s:ftplugin_sql_objects."', 'bW')<CR>" |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 442 | " Could not figure out how to use a :call search() string in visual mode |
| 443 | " without it ending visual mode |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 444 | " Unfortunately, this will add a entry to the search history |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 445 | exec 'xnoremap <buffer> <silent> ]} /'.s:ftplugin_sql_objects.'<CR>' |
| 446 | exec 'xnoremap <buffer> <silent> [{ ?'.s:ftplugin_sql_objects.'<CR>' |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 447 | |
| 448 | " Mappings to move to the next COMMENT |
| 449 | " |
| 450 | " Had to double the \ for the \| separator since this has a special |
| 451 | " meaning on maps |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 452 | let b:comment_leader = '\(--\\|\/\/\\|\*\\|\/\*\\|\*\/\)' |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 453 | " Find the start of the next comment |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 454 | let b:comment_start = '^\(\s*'.b:comment_leader.'.*\n\)\@<!'. |
| 455 | \ '\(\s*'.b:comment_leader.'\)' |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 456 | " Find the end of the previous comment |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 457 | let b:comment_end = '\(^\s*'.b:comment_leader.'.*\n\)'. |
| 458 | \ '\(^\s*'.b:comment_leader.'\)\@!' |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 459 | " Skip over the comment |
| 460 | let b:comment_jump_over = "call search('". |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 461 | \ '^\(\s*'.b:comment_leader.'.*\n\)\@<!'. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 462 | \ "', 'W')" |
| 463 | let b:comment_skip_back = "call search('". |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 464 | \ '^\(\s*'.b:comment_leader.'.*\n\)\@<!'. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 465 | \ "', 'bW')" |
| 466 | " Move to the start and end of comments |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 467 | exec 'nnoremap <silent><buffer> ]" :call search('."'".b:comment_start."'".', "W" )<CR>' |
| 468 | exec 'nnoremap <silent><buffer> [" :call search('."'".b:comment_end."'".', "W" )<CR>' |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 469 | exec 'xnoremap <silent><buffer> ]" :<C-U>exec "normal! gv"<Bar>call search('."'".b:comment_start."'".', "W" )<CR>' |
| 470 | 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] | 471 | |
| 472 | " Comments can be of the form: |
| 473 | " /* |
| 474 | " * |
| 475 | " */ |
| 476 | " or |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 477 | " -- |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 478 | " or |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 479 | " // |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 480 | setlocal comments=s1:/*,mb:*,ex:*/,:--,:// |
| 481 | |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 482 | " Set completion with CTRL-X CTRL-O to autoloaded function. |
| 483 | if exists('&omnifunc') |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 484 | " Since the SQL completion plugin can be used in conjunction |
| 485 | " with other completion filetypes it must record the previous |
| 486 | " OMNI function prior to setting up the SQL OMNI function |
| 487 | let b:sql_compl_savefunc = &omnifunc |
| 488 | |
Bram Moolenaar | ad3b366 | 2013-05-17 18:14:19 +0200 | [diff] [blame] | 489 | " Source it to determine it's version |
| 490 | runtime autoload/sqlcomplete.vim |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 491 | " This is used by the sqlcomplete.vim plugin |
| 492 | " Source it for it's global functions |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 493 | runtime autoload/syntaxcomplete.vim |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 494 | |
| 495 | setlocal omnifunc=sqlcomplete#Complete |
| 496 | " Prevent the intellisense plugin from loading |
| 497 | let b:sql_vis = 1 |
Bram Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame] | 498 | if !exists('g:omni_sql_no_default_maps') |
Bram Moolenaar | ad3b366 | 2013-05-17 18:14:19 +0200 | [diff] [blame] | 499 | let regex_extra = '' |
| 500 | if exists('g:loaded_syntax_completion') && exists('g:loaded_sql_completion') |
| 501 | if g:loaded_syntax_completion > 120 && g:loaded_sql_completion > 140 |
| 502 | let regex_extra = '\\w*' |
| 503 | endif |
| 504 | endif |
Bram Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame] | 505 | " Static maps which use populate the completion list |
| 506 | " using Vim's syntax highlighting rules |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 507 | 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] | 508 | exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'k <C-\><C-O>:call sqlcomplete#Map("sqlKeyword'.regex_extra.'")<CR><C-X><C-O>' |
| 509 | exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'f <C-\><C-O>:call sqlcomplete#Map("sqlFunction'.regex_extra.'")<CR><C-X><C-O>' |
| 510 | exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'o <C-\><C-O>:call sqlcomplete#Map("sqlOption'.regex_extra.'")<CR><C-X><C-O>' |
| 511 | exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'T <C-\><C-O>:call sqlcomplete#Map("sqlType'.regex_extra.'")<CR><C-X><C-O>' |
| 512 | 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] | 513 | " Dynamic maps which use populate the completion list |
| 514 | " using the dbext.vim plugin |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 515 | exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'t <C-\><C-O>:call sqlcomplete#Map("table")<CR><C-X><C-O>' |
| 516 | exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'p <C-\><C-O>:call sqlcomplete#Map("procedure")<CR><C-X><C-O>' |
| 517 | exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'v <C-\><C-O>:call sqlcomplete#Map("view")<CR><C-X><C-O>' |
| 518 | exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key.'c <C-\><C-O>:call sqlcomplete#Map("column")<CR><C-X><C-O>' |
| 519 | 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] | 520 | " The next 3 maps are only to be used while the completion window is |
| 521 | " active due to the <CR> at the beginning of the map |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 522 | 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] | 523 | " <C-Right> is not recognized on most Unix systems, so only create |
| 524 | " these additional maps on the Windows platform. |
| 525 | " If you would like to use these maps, choose a different key and make |
| 526 | " the same map in your vimrc. |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 527 | " if has('win32') |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 528 | exec 'inoremap <buffer> '.g:ftplugin_sql_omni_key_right.' <C-R>=sqlcomplete#DrillIntoTable()<CR>' |
| 529 | 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] | 530 | " endif |
Bram Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame] | 531 | " Remove any cached items useful for schema changes |
Bram Moolenaar | 34feacb | 2012-12-05 19:01:43 +0100 | [diff] [blame] | 532 | 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] | 533 | endif |
| 534 | |
| 535 | if b:sql_compl_savefunc != "" |
| 536 | " We are changing the filetype to SQL from some other filetype |
| 537 | " which had OMNI completion defined. We need to activate the |
| 538 | " SQL completion plugin in order to cache some of the syntax items |
| 539 | " while the syntax rules for SQL are active. |
Bram Moolenaar | f9d5ca1 | 2010-08-01 16:13:51 +0200 | [diff] [blame] | 540 | call sqlcomplete#ResetCacheSyntax() |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 541 | call sqlcomplete#PreCacheSyntax() |
Bram Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame] | 542 | endif |
Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 543 | endif |
| 544 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 545 | let &cpo = s:save_cpo |
Bram Moolenaar | 84f7235 | 2012-03-11 15:57:40 +0100 | [diff] [blame] | 546 | unlet s:save_cpo |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 547 | |
Bram Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame] | 548 | " vim:sw=4: |