xref: /vim-8.2.3635/runtime/ftplugin/sql.vim (revision df177f67)
1" SQL filetype plugin file
2" Language:    SQL (Common for Oracle, Microsoft SQL Server, Sybase)
3" Version:     0.08
4" Maintainer:  David Fishburn <fishburn at ianywhere dot com>
5" Last Change: Mon Feb 21 2005 7:27:36 AM
6" Download:    http://vim.sourceforge.net/script.php?script_id=454
7
8" This file should only contain values that are common to all SQL languages
9" Oracle, Microsoft SQL Server, Sybase ASA/ASE, MySQL, and so on
10" If additional features are required create:
11"        vimfiles/after/ftplugin/sql.vim (Windows)
12"        .vim/after/ftplugin/sql.vim     (Unix)
13" to override and add any of your own settings.
14
15" Only do this when not done yet for this buffer
16if exists("b:did_ftplugin")
17  finish
18endif
19
20let s:save_cpo = &cpo
21set cpo=
22
23" Don't load another plugin for this buffer
24let b:did_ftplugin = 1
25
26" Some standard expressions for use with the matchit strings
27let s:notend = '\%(\<end\s\+\)\@<!'
28let s:when_no_matched_or_others = '\%(\<when\>\%(\s\+\%(\%(\<not\>\s\+\)\?<matched\>\)\|\<others\>\)\@!\)'
29let s:or_replace = '\%(or\s\+replace\s\+\)\?'
30
31" Define patterns for the matchit macro
32if !exists("b:match_words")
33    " SQL is generally case insensitive
34    let b:match_ignorecase = 1
35
36    " Handle the following:
37    " if
38    " elseif | elsif
39    " else [if]
40    " end if
41    "
42    " [while condition] loop
43    "     leave
44    "     break
45    "     continue
46    "     exit
47    " end loop
48    "
49    " for
50    "     leave
51    "     break
52    "     continue
53    "     exit
54    " end loop
55    "
56    " do
57    "     statements
58    " doend
59    "
60    " case
61    " when
62    " when
63    " default
64    " end case
65    "
66    " merge
67    " when not matched
68    " when matched
69    "
70    " EXCEPTION
71    " WHEN column_not_found THEN
72    " WHEN OTHERS THEN
73    "
74    " create[ or replace] procedure|function|event
75
76    let b:match_words =
77		\ '\<begin\>:\<end\>\W*$,'.
78		\
79                \ s:notend . '\<if\>:'.
80                \ '\<elsif\>\|\<elseif\>\|\<else\>:'.
81                \ '\<end\s\+if\>,'.
82                \
83                \ '\<do\>\|'.
84                \ '\<while\>\|'.
85                \ '\%(' . s:notend . '\<loop\>\)\|'.
86                \ '\%(' . s:notend . '\<for\>\):'.
87                \ '\<exit\>\|\<leave\>\|\<break\>\|\<continue\>:'.
88                \ '\%(\<end\s\+\%(for\|loop\>\)\)\|\<doend\>,'.
89                \
90                \ '\%('. s:notend . '\<case\>\):'.
91                \ '\%('.s:when_no_matched_or_others.'\):'.
92                \ '\%(\<when\s\+others\>\|\<end\s\+case\>\),' .
93                \
94                \ '\<merge\>:' .
95                \ '\<when\s\+not\s\+matched\>:' .
96                \ '\<when\s\+matched\>,' .
97                \
98                \ '\%(\<create\s\+' . s:or_replace . '\)\?'.
99                \ '\%(function\|procedure\|event\):'.
100                \ '\<returns\?\>'
101                " \ '\<begin\>\|\<returns\?\>:'.
102                " \ '\<end\>\(;\)\?\s*$'
103                " \ '\<exception\>:'.s:when_no_matched_or_others.
104                " \ ':\<when\s\+others\>,'.
105		"
106                " \ '\%(\<exception\>\|\%('. s:notend . '\<case\>\)\):'.
107                " \ '\%(\<default\>\|'.s:when_no_matched_or_others.'\):'.
108                " \ '\%(\%(\<when\s\+others\>\)\|\<end\s\+case\>\),' .
109endif
110
111" Define how to find the macro definition of a variable using the various
112" [d, [D, [_CTRL_D and so on features
113" Match these values ignoring case
114" ie  DECLARE varname INTEGER
115let &l:define = '\c\(DECLARE\|IN\|OUT\|INOUT\)\s*'
116
117
118" Mappings to move to the next BEGIN ... END block
119" \W - no characters or digits
120nmap <buffer> <silent> ]] :call search('\c^\s*begin\>', 'W' )<CR>
121nmap <buffer> <silent> [[ :call search('\c^\s*begin\>', 'bW' )<CR>
122nmap <buffer> <silent> ][ :call search('\c^\s*end\W*$', 'W' )<CR>
123nmap <buffer> <silent> [] :call search('\c^\s*end\W*$', 'bW' )<CR>
124vmap <buffer> <silent> ]] /\c^\s*begin\><CR>
125vmap <buffer> <silent> [[ ?\c^\s*begin<CR>
126vmap <buffer> <silent> ][ /\c^\s*end\W*$<CR>
127vmap <buffer> <silent> [] ?\c^\s*end\W*$<CR>
128
129
130" Predefined SQL objects what are used by the below mappings using
131" the ]} style maps.
132" This global variable allows the users to override it's value
133" from within their vimrc.
134if !exists('g:ftplugin_sql_objects')
135    let g:ftplugin_sql_objects = 'function,procedure,event,' .
136                \ '\(existing\\|global\s\+temporary\s\+\)\?table,trigger' .
137                \ ',schema,service,publication,database,datatype,domain' .
138                \ ',index,subscription,synchronization,view,variable'
139endif
140
141let s:ftplugin_sql_objects =
142            \ '\c^\s*' .
143            \ '\(create\s\+\(or\s\+replace\s\+\)\?\)\?' .
144            \ '\<\(' .
145            \ substitute(g:ftplugin_sql_objects, ',', '\\\\|', 'g') .
146            \ '\)\>'
147
148" Mappings to move to the next CREATE ... block
149" map <buffer> <silent> ]} :call search(g:ftplugin_sql_objects, 'W' )<CR>
150" nmap <buffer> <silent> [{ :call search('\c^\s*\(create\s\+\(or\s\+replace\s\+\)\?\)\?\<\(function\\|procedure\\|event\\|table\\|trigger\\|schema\)\>', 'bW' )<CR>
151" exec 'nmap <buffer> <silent> ]} /'.s:ftplugin_sql_objects.'<CR>'
152exec "nmap <buffer> <silent> ]} :call search('".s:ftplugin_sql_objects."', 'W')<CR>"
153exec "nmap <buffer> <silent> [{ :call search('".s:ftplugin_sql_objects."', 'bW')<CR>"
154" Could not figure out how to use a :call search() string in visual mode
155" without it ending visual mode
156exec 'vmap <buffer> <silent> ]} /'.s:ftplugin_sql_objects.'<CR>'
157exec 'vmap <buffer> <silent> [{ ?'.s:ftplugin_sql_objects.'<CR>'
158" vmap <buffer> <silent> ]} /\c^\s*\(create\s\+\(or\s\+replace\s\+\)\?\)\?\<\(function\\|procedure\\|event\\|table\\|trigger\\|schema\)\><CR>
159" vmap <buffer> <silent> [{ ?\c^\s*\(create\s\+\(or\s\+replace\s\+\)\?\)\?\<\(function\\|procedure\\|event\\|table\\|trigger\\|schema\)\><CR>
160
161" Mappings to move to the next COMMENT
162"
163" Had to double the \ for the \| separator since this has a special
164" meaning on maps
165let b:comment_leader = '\(--\\|\/\/\\|\*\\|\/\*\\|\*\/\)'
166" Find the start of the next comment
167let b:comment_start  = '^\(\s*'.b:comment_leader.'.*\n\)\@<!'.
168            \ '\(\s*'.b:comment_leader.'\)'
169" Find the end of the previous comment
170let b:comment_end = '\(^\s*'.b:comment_leader.'.*\n\)'.
171            \ '\(^\s*'.b:comment_leader.'\)\@!'
172" Skip over the comment
173let b:comment_jump_over  = "call search('".
174            \ '^\(\s*'.b:comment_leader.'.*\n\)\@<!'.
175            \ "', 'W')"
176let b:comment_skip_back  = "call search('".
177            \ '^\(\s*'.b:comment_leader.'.*\n\)\@<!'.
178            \ "', 'bW')"
179" Move to the start and end of comments
180exec 'nnoremap <silent><buffer> ]" /'.b:comment_start.'<CR>'
181exec 'nnoremap <silent><buffer> [" /'.b:comment_end.'<CR>'
182exec 'vnoremap <silent><buffer> ]" /'.b:comment_start.'<CR>'
183exec 'vnoremap <silent><buffer> [" /'.b:comment_end.'<CR>'
184
185" Comments can be of the form:
186"   /*
187"    *
188"    */
189" or
190"   //
191" or
192"   --
193setlocal comments=s1:/*,mb:*,ex:*/,:--,://
194
195let &cpo = s:save_cpo
196
197" vim:sw=4:ff=unix:
198
199