1 " File: plugin/delimitMate.vim
4 " Description: This plugin provides auto-completion for quotes, parens, etc.
5 " Maintainer: Israel Chauca F. <israelchauca@gmail.com>
6 " Manual: Read ":help delimitMate".
7 " ============================================================================
11 if exists("g:loaded_delimitMate") || &cp
12 " User doesn't want this plugin or compatible is set, let's get out!
15 let g:loaded_delimitMate = 1
17 if exists("s:loaded_delimitMate") && !exists("g:delimitMate_testing")
18 " Don't define the functions if they already exist: just do the work
19 " (unless we are testing):
20 call s:DelimitMateDo()
25 echoerr "delimitMate: this plugin requires vim >= 7!"
29 let s:loaded_delimitMate = 1
30 let delimitMate_version = "2.6"
32 function! s:option_init(name, default) "{{{
33 let b = exists("b:delimitMate_" . a:name)
34 let g = exists("g:delimitMate_" . a:name)
35 let prefix = "_l_delimitMate_"
40 exec "let sufix = g:delimitMate_" . a:name
42 exec "let sufix = b:delimitMate_" . a:name
44 if exists("b:" . prefix . a:name)
45 exec "unlockvar! b:" . prefix . a:name
47 exec "let b:" . prefix . a:name . " = " . string(sufix)
48 exec "lockvar! b:" . prefix . a:name
51 function! s:init() "{{{
52 " Initialize variables:
55 call s:option_init("autoclose", 1)
58 call s:option_init("matchpairs", string(&matchpairs)[1:-2])
59 call s:option_init("matchpairs_list", split(b:_l_delimitMate_matchpairs, ','))
60 call s:option_init("left_delims", split(b:_l_delimitMate_matchpairs, ':.,\='))
61 call s:option_init("right_delims", split(b:_l_delimitMate_matchpairs, ',\=.:'))
64 call s:option_init("quotes", "\" ' `")
65 call s:option_init("quotes_list", split(b:_l_delimitMate_quotes))
68 call s:option_init("nesting_quotes", [])
71 call s:option_init("excluded_regions", "Comment")
72 call s:option_init("excluded_regions_list", split(b:_l_delimitMate_excluded_regions, ',\s*'))
73 let enabled = len(b:_l_delimitMate_excluded_regions_list) > 0
74 call s:option_init("excluded_regions_enabled", enabled)
77 call s:option_init("excluded_ft", "")
80 if exists("b:delimitMate_expand_space") && type(b:delimitMate_expand_space) == type("")
81 echom "b:delimitMate_expand_space is '".b:delimitMate_expand_space."' but it must be either 1 or 0!"
82 echom "Read :help 'delimitMate_expand_space' for more details."
83 unlet b:delimitMate_expand_space
84 let b:delimitMate_expand_space = 1
86 if exists("g:delimitMate_expand_space") && type(g:delimitMate_expand_space) == type("")
87 echom "delimitMate_expand_space is '".g:delimitMate_expand_space."' but it must be either 1 or 0!"
88 echom "Read :help 'delimitMate_expand_space' for more details."
89 unlet g:delimitMate_expand_space
90 let g:delimitMate_expand_space = 1
92 call s:option_init("expand_space", 0)
95 if exists("b:delimitMate_expand_cr") && type(b:delimitMate_expand_cr) == type("")
96 echom "b:delimitMate_expand_cr is '".b:delimitMate_expand_cr."' but it must be either 1 or 0!"
97 echom "Read :help 'delimitMate_expand_cr' for more details."
98 unlet b:delimitMate_expand_cr
99 let b:delimitMate_expand_cr = 1
101 if exists("g:delimitMate_expand_cr") && type(g:delimitMate_expand_cr) == type("")
102 echom "delimitMate_expand_cr is '".g:delimitMate_expand_cr."' but it must be either 1 or 0!"
103 echom "Read :help 'delimitMate_expand_cr' for more details."
104 unlet g:delimitMate_expand_cr
105 let g:delimitMate_expand_cr = 1
107 if ((&backspace !~ 'eol' || &backspace !~ 'start') && &backspace != 2) &&
108 \ ((exists('b:delimitMate_expand_cr') && b:delimitMate_expand_cr == 1) ||
109 \ (exists('g:delimitMate_expand_cr') && g:delimitMate_expand_cr == 1))
110 echom "delimitMate: There seems to be some incompatibility with your settings that may interfer with the expansion of <CR>. See :help 'delimitMate_expand_cr' for details."
112 call s:option_init("expand_cr", 0)
115 call s:option_init("smart_matchpairs", '^\%(\w\|\!\|£\|\$\|_\|["'']\s*\S\)')
118 call s:option_init("smart_quotes", 1)
121 call s:option_init("apostrophes", "")
122 call s:option_init("apostrophes_list", split(b:_l_delimitMate_apostrophes, ":\s*"))
125 call s:option_init("tab2exit", 1)
128 call s:option_init("balance_matchpairs", 0)
130 let b:_l_delimitMate_buffer = []
132 endfunction "}}} Init()
138 function! s:Map() "{{{
142 let save_keymap = &keymap
143 let save_iminsert = &iminsert
144 let save_imsearch = &imsearch
147 if b:_l_delimitMate_autoclose
152 call s:ExtraMappings()
155 let &keymap = save_keymap
156 let &iminsert = save_iminsert
157 let &imsearch = save_imsearch
160 let b:delimitMate_enabled = 1
162 endfunction "}}} Map()
164 function! s:Unmap() " {{{
166 \ b:_l_delimitMate_right_delims +
167 \ b:_l_delimitMate_left_delims +
168 \ b:_l_delimitMate_quotes_list +
169 \ b:_l_delimitMate_apostrophes_list +
170 \ ['<BS>', '<S-BS>', '<Del>', '<CR>', '<Space>', '<S-Tab>', '<Esc>'] +
171 \ ['<Up>', '<Down>', '<Left>', '<Right>', '<LeftMouse>', '<RightMouse>'] +
172 \ ['<Home>', '<End>', '<PageUp>', '<PageDown>', '<S-Down>', '<S-Up>', '<C-G>g']
175 if maparg(map, "i") =~? 'delimitMate'
179 exec 'silent! iunmap <buffer> ' . map
183 if !has('gui_running')
184 silent! iunmap <C-[>OC
187 let b:delimitMate_enabled = 0
188 endfunction " }}} s:Unmap()
190 function! s:TestMappingsDo() "{{{
192 if !exists("g:delimitMate_testing")
193 silent call delimitMate#TestMappings()
195 let temp_varsDM = [b:_l_delimitMate_expand_space, b:_l_delimitMate_expand_cr, b:_l_delimitMate_autoclose]
197 let b:delimitMate_expand_space = i
198 let b:delimitMate_expand_cr = i
200 let b:delimitMate_autoclose = a
204 call delimitMate#TestMappings()
205 call append(line('$'),'')
208 let b:delimitMate_expand_space = temp_varsDM[0]
209 let b:delimitMate_expand_cr = temp_varsDM[1]
210 let b:delimitMate_autoclose = temp_varsDM[2]
217 function! s:DelimitMateDo(...) "{{{
219 " First, remove all magic, if needed:
220 if exists("b:delimitMate_enabled") && b:delimitMate_enabled == 1
224 " Check if this file type is excluded:
225 if exists("g:delimitMate_excluded_ft") &&
226 \ index(split(g:delimitMate_excluded_ft, ','), &filetype, 0, 1) >= 0
232 " Check if user tried to disable using b:loaded_delimitMate
233 if exists("b:loaded_delimitMate")
237 " Initialize settings:
244 echo "delimitMate has been reset."
248 function! s:DelimitMateSwitch() "{{{
249 if exists("b:delimitMate_enabled") && b:delimitMate_enabled
251 echo "delimitMate has been disabled."
256 echo "delimitMate has been enabled."
260 function! s:Finish() " {{{
261 if exists('g:delimitMate_loaded')
262 return delimitMate#Finish(1)
267 function! s:FlushBuffer() " {{{
268 if exists('g:delimitMate_loaded')
269 return delimitMate#FlushBuffer()
277 function! s:NoAutoClose() "{{{
278 " inoremap <buffer> ) <C-R>=delimitMate#SkipDelim('\)')<CR>
279 for delim in b:_l_delimitMate_right_delims + b:_l_delimitMate_quotes_list
283 exec 'inoremap <silent> <Plug>delimitMate' . delim . ' <C-R>=delimitMate#SkipDelim("' . escape(delim,'"') . '")<CR>'
284 exec 'silent! imap <unique> <buffer> '.delim.' <Plug>delimitMate'.delim
288 function! s:AutoClose() "{{{
289 " Add matching pair and jump to the midle:
290 " inoremap <silent> <buffer> ( ()<Left>
292 while i < len(b:_l_delimitMate_matchpairs_list)
293 let ld = b:_l_delimitMate_left_delims[i] == '|' ? '<bar>' : b:_l_delimitMate_left_delims[i]
294 let rd = b:_l_delimitMate_right_delims[i] == '|' ? '<bar>' : b:_l_delimitMate_right_delims[i]
295 exec 'inoremap <silent> <Plug>delimitMate' . ld . ' ' . ld . '<C-R>=delimitMate#ParenDelim("' . escape(rd, '|') . '")<CR>'
296 exec 'silent! imap <unique> <buffer> '.ld.' <Plug>delimitMate'.ld
300 " Exit from inside the matching pair:
301 for delim in b:_l_delimitMate_right_delims
302 exec 'inoremap <silent> <Plug>delimitMate' . delim . ' <C-R>=delimitMate#JumpOut("\' . delim . '")<CR>'
303 exec 'silent! imap <unique> <buffer> ' . delim . ' <Plug>delimitMate'. delim
306 " Add matching quote and jump to the midle, or exit if inside a pair of matching quotes:
307 " inoremap <silent> <buffer> " <C-R>=delimitMate#QuoteDelim("\"")<CR>
308 for delim in b:_l_delimitMate_quotes_list
312 exec 'inoremap <silent> <Plug>delimitMate' . delim . ' <C-R>=delimitMate#QuoteDelim("\' . delim . '")<CR>'
313 exec 'silent! imap <unique> <buffer> ' . delim . ' <Plug>delimitMate' . delim
316 " Try to fix the use of apostrophes (kept for backward compatibility):
317 " inoremap <silent> <buffer> n't n't
318 for map in b:_l_delimitMate_apostrophes_list
319 exec "inoremap <silent> " . map . " " . map
320 exec 'silent! imap <unique> <buffer> ' . map . ' <Plug>delimitMate' . map
324 function! s:ExtraMappings() "{{{
325 " If pair is empty, delete both delimiters:
326 inoremap <silent> <Plug>delimitMateBS <C-R>=delimitMate#BS()<CR>
327 if !hasmapto('<Plug>delimitMateBS','i')
328 silent! imap <unique> <buffer> <BS> <Plug>delimitMateBS
330 " If pair is empty, delete closing delimiter:
331 inoremap <silent> <expr> <Plug>delimitMateS-BS delimitMate#WithinEmptyPair() ? "\<C-R>=delimitMate#Del()\<CR>" : "\<S-BS>"
332 if !hasmapto('<Plug>delimitMateS-BS','i')
333 silent! imap <unique> <buffer> <S-BS> <Plug>delimitMateS-BS
335 " Expand return if inside an empty pair:
336 inoremap <silent> <Plug>delimitMateCR <C-R>=delimitMate#ExpandReturn()<CR>
337 if b:_l_delimitMate_expand_cr != 0 && !hasmapto('<Plug>delimitMateCR', 'i')
338 silent! imap <unique> <buffer> <CR> <Plug>delimitMateCR
340 " Expand space if inside an empty pair:
341 inoremap <silent> <Plug>delimitMateSpace <C-R>=delimitMate#ExpandSpace()<CR>
342 if b:_l_delimitMate_expand_space != 0 && !hasmapto('<Plug>delimitMateSpace', 'i')
343 silent! imap <unique> <buffer> <Space> <Plug>delimitMateSpace
345 " Jump over any delimiter:
346 inoremap <silent> <Plug>delimitMateS-Tab <C-R>=delimitMate#JumpAny("\<S-Tab>")<CR>
347 if b:_l_delimitMate_tab2exit && !hasmapto('<Plug>delimitMateS-Tab', 'i')
348 silent! imap <unique> <buffer> <S-Tab> <Plug>delimitMateS-Tab
350 " Change char buffer on Del:
351 inoremap <silent> <Plug>delimitMateDel <C-R>=delimitMate#Del()<CR>
352 if !hasmapto('<Plug>delimitMateDel', 'i')
353 silent! imap <unique> <buffer> <Del> <Plug>delimitMateDel
355 " Flush the char buffer on movement keystrokes or when leaving insert mode:
356 for map in ['Esc', 'Left', 'Right', 'Home', 'End']
357 exec 'inoremap <silent> <Plug>delimitMate'.map.' <C-R>=<SID>Finish()<CR><'.map.'>'
358 if !hasmapto('<Plug>delimitMate'.map, 'i')
359 exec 'silent! imap <unique> <buffer> <'.map.'> <Plug>delimitMate'.map
362 " Except when pop-up menu is active:
363 for map in ['Up', 'Down', 'PageUp', 'PageDown', 'S-Down', 'S-Up']
364 exec 'inoremap <silent> <expr> <Plug>delimitMate'.map.' pumvisible() ? "\<'.map.'>" : "\<C-R>=\<SID>Finish()\<CR>\<'.map.'>"'
365 if !hasmapto('<Plug>delimitMate'.map, 'i')
366 exec 'silent! imap <unique> <buffer> <'.map.'> <Plug>delimitMate'.map
369 " Avoid ambiguous mappings:
370 for map in ['LeftMouse', 'RightMouse']
371 exec 'inoremap <silent> <Plug>delimitMateM'.map.' <C-R>=delimitMate#Finish(1)<CR><'.map.'>'
372 if !hasmapto('<Plug>delimitMate'.map, 'i')
373 exec 'silent! imap <unique> <buffer> <'.map.'> <Plug>delimitMateM'.map
377 " Jump over next delimiters
378 inoremap <buffer> <Plug>delimitMateJumpMany <C-R>=len(b:_l_delimitMate_buffer) ? delimitMate#Finish(0) : delimitMate#JumpMany()<CR>
379 if !hasmapto('<Plug>delimitMateJumpMany')
380 imap <silent> <buffer> <C-G>g <Plug>delimitMateJumpMany
383 " The following simply creates an ambiguous mapping so vim fully processes
384 " the escape sequence for terminal keys, see 'ttimeout' for a rough
385 " explanation, this just forces it to work
386 if !has('gui_running')
387 imap <silent> <C-[>OC <RIGHT>
395 call s:DelimitMateDo()
397 " Let me refresh without re-loading the buffer:
398 command! -bar DelimitMateReload call s:DelimitMateDo(1)
401 command! -bar DelimitMateTest silent call s:TestMappingsDo()
404 command! -bar DelimitMateSwitch call s:DelimitMateSwitch()
411 " Run on file type change.
412 "autocmd VimEnter * autocmd FileType * call <SID>DelimitMateDo()
413 autocmd FileType * call <SID>DelimitMateDo()
415 " Run on new buffers.
416 autocmd BufNewFile,BufRead,BufEnter *
417 \ if !exists('b:delimitMate_was_here') |
418 \ call <SID>DelimitMateDo() |
419 \ let b:delimitMate_was_here = 1 |
422 " Flush the char buffer:
423 autocmd InsertEnter * call <SID>FlushBuffer()
426 \ call <SID>FlushBuffer() |
433 " GetLatestVimScripts: 2754 1 :AutoInstall: delimitMate.vim
434 " vim:foldmethod=marker:foldcolumn=4