Skip to content

Commit 362408f

Browse files
jiangmiaovim-scripts
authored andcommitted
Version 1.3.1
Fix #33: character '|' map failed. Fix #35: Fast Wrap incorrect. Improve #36, Keep quotes number is odd in one line. Improve #37, ignore comment line for vim file. Improve #43, <CR> mapping compatible.
1 parent 23d7184 commit 362408f

File tree

3 files changed

+129
-105
lines changed

3 files changed

+129
-105
lines changed

README

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
This is a mirror of http://www.vim.org/scripts/script.php?script_id=3599
22

3-
Responsitory: https://github.com/jiangmiao/auto-pairs
3+
Repository: https://github.com/jiangmiao/auto-pairs
44

55
Auto Pairs
66
==========
@@ -10,6 +10,10 @@ Installation
1010
------------
1111
copy plugin/auto-pairs.vim to ~/.vim/plugin
1212

13+
or if you are using `pathogen`:
14+
15+
```git clone git://github.com/jiangmiao/auto-pairs.git ~/.vim/bundle/auto-pairs```
16+
1317
Features
1418
--------
1519
* Insert in pair
@@ -87,7 +91,18 @@ Features
8791
'''
8892

8993
output:
90-
'''
94+
'''|'''
95+
96+
* Delete Repeated Pairs in one time
97+
98+
input: """|""" (press <BS> at |)
99+
output: |
100+
101+
input: {{|}} (press <BS> at |)
102+
output: |
103+
104+
input: [[[[[[|]]]]]] (press <BS> at |)
105+
output: |
91106

92107
* Fly Mode
93108

@@ -122,7 +137,7 @@ Fly Mode
122137
--------
123138
Fly Mode will always force closed-pair jumping instead of inserting. only for ")", "}", "]"
124139

125-
If jumps in mistake, could use AutoPairsBackInsert(Default Key: <M-b>) to jump back and insert closed pair.
140+
If jumps in mistake, could use AutoPairsBackInsert(Default Key: `<M-b>`) to jump back and insert closed pair.
126141

127142
the most situation maybe want to insert single closed pair in the string, eg ")"
128143

@@ -144,13 +159,13 @@ Shortcuts
144159
<M-p> : Toggle Autopairs (g:AutoPairsShortcutToggle)
145160
<M-e> : Fast Wrap (g:AutoPairsShortcutFastWrap)
146161
<M-n> : Jump to next closed pair (g:AutoPairsShortcutJump)
147-
<M-b> : BackInsert
162+
<M-b> : BackInsert (g:AutoPairsShortcutBackInsert)
148163

149164
If <M-p> <M-e> or <M-n> conflict with another keys or want to bind to another keys, add
150165

151166
let g:AutoPairShortcutToggle = '<another key>'
152167

153-
to .vimrc, it the key is empty string '', then the shortcut will be disabled.
168+
to .vimrc, if the key is empty string '', then the shortcut will be disabled.
154169

155170
Options
156171
-------
@@ -258,40 +273,6 @@ Known Issues
258273
-----------------------
259274
There are the issues I cannot fix.
260275

261-
Compatible with Vimwiki - [issue #19](https://github.com/jiangmiao/auto-pairs/issues/19)
262-
263-
Description: When works with vimwiki `<CR>` will output `<SNR>xx_CR()`
264-
Reason: vimwiki uses `<expr>` on mapping `<CR>` that auto-pairs cannot expanding.
265-
Solution A: Add
266-
267-
" Copy from vimwiki.vim s:CR function for CR remapping
268-
function! VimwikiCR()
269-
let res = vimwiki#lst#kbd_cr()
270-
if res == "\<CR>" && g:vimwiki_table_mappings
271-
let res = vimwiki#tbl#kbd_cr()
272-
endif
273-
return res
274-
endfunction
275-
autocmd filetype vimwiki inoremap <buffer> <silent> <CR> <C-R>=VimwikiCR()<CR><C-R>=AutoPairsReturn()<CR>
276-
277-
to .vimrc, it will make vimwiki and auto-pairs 'Return' feature works together.
278-
279-
Solution B: add `let g:AutoPairsMapCR = 0` to .vimrc to disable `<CR>` mapping.
280-
281-
Compatible with viki - [issue #25](https://github.com/jiangmiao/auto-pairs/issues/25)
282-
283-
Description: When works with viki `<CR>` will output viki#ExprMarkInexistentInElement('ParagraphVisible','<CR>')
284-
Reason: viki uses `<expr>` on mapping `<CR>` that auto-pairs cannot expanding.
285-
Solution A: Add
286-
287-
autocmd filetype viki inoremap <buffer> <silent> <CR> <C-R>=viki#ExprMarkInexistentInElement('ParagraphVisible',"\n")<CR><C-R>=AutoPairsReturn()<CR>`
288-
289-
to .vimrc, it will make viki and auto-pairs works together.
290-
291-
Solution B: add `let g:AutoPairsMapCR = 0` to .vimrc to disable `<CR>` mapping.
292-
293-
Remarks: Solution A need NOT add `let g:AutoPairsMapCR = 0` to .vimrc, if Solution A still cannot work, then have to use Solution B to disable auto-pairs `<CR>`.
294-
295276
Breaks '.' - [issue #3](https://github.com/jiangmiao/auto-pairs/issues/3)
296277

297278
Description: After entering insert mode and inputing `[hello` then leave insert
@@ -302,4 +283,3 @@ Breaks '.' - [issue #3](https://github.com/jiangmiao/auto-pairs/issues/3)
302283
Contributors
303284
------------
304285
* [camthompson](https://github.com/camthompson)
305-

README.md

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Installation
66
------------
77
copy plugin/auto-pairs.vim to ~/.vim/plugin
88

9+
or if you are using `pathogen`:
10+
11+
```git clone git://github.com/jiangmiao/auto-pairs.git ~/.vim/bundle/auto-pairs```
12+
913
Features
1014
--------
1115
* Insert in pair
@@ -129,7 +133,7 @@ Fly Mode
129133
--------
130134
Fly Mode will always force closed-pair jumping instead of inserting. only for ")", "}", "]"
131135

132-
If jumps in mistake, could use AutoPairsBackInsert(Default Key: <M-b>) to jump back and insert closed pair.
136+
If jumps in mistake, could use AutoPairsBackInsert(Default Key: `<M-b>`) to jump back and insert closed pair.
133137

134138
the most situation maybe want to insert single closed pair in the string, eg ")"
135139

@@ -151,13 +155,13 @@ Shortcuts
151155
<M-p> : Toggle Autopairs (g:AutoPairsShortcutToggle)
152156
<M-e> : Fast Wrap (g:AutoPairsShortcutFastWrap)
153157
<M-n> : Jump to next closed pair (g:AutoPairsShortcutJump)
154-
<M-b> : BackInsert
158+
<M-b> : BackInsert (g:AutoPairsShortcutBackInsert)
155159

156160
If <M-p> <M-e> or <M-n> conflict with another keys or want to bind to another keys, add
157161

158162
let g:AutoPairShortcutToggle = '<another key>'
159163

160-
to .vimrc, it the key is empty string '', then the shortcut will be disabled.
164+
to .vimrc, if the key is empty string '', then the shortcut will be disabled.
161165

162166
Options
163167
-------
@@ -265,40 +269,6 @@ Known Issues
265269
-----------------------
266270
There are the issues I cannot fix.
267271

268-
Compatible with Vimwiki - [issue #19](https://github.com/jiangmiao/auto-pairs/issues/19)
269-
270-
Description: When works with vimwiki `<CR>` will output `<SNR>xx_CR()`
271-
Reason: vimwiki uses `<expr>` on mapping `<CR>` that auto-pairs cannot expanding.
272-
Solution A: Add
273-
274-
" Copy from vimwiki.vim s:CR function for CR remapping
275-
function! VimwikiCR()
276-
let res = vimwiki#lst#kbd_cr()
277-
if res == "\<CR>" && g:vimwiki_table_mappings
278-
let res = vimwiki#tbl#kbd_cr()
279-
endif
280-
return res
281-
endfunction
282-
autocmd filetype vimwiki inoremap <buffer> <silent> <CR> <C-R>=VimwikiCR()<CR><C-R>=AutoPairsReturn()<CR>
283-
284-
to .vimrc, it will make vimwiki and auto-pairs 'Return' feature works together.
285-
286-
Solution B: add `let g:AutoPairsMapCR = 0` to .vimrc to disable `<CR>` mapping.
287-
288-
Compatible with viki - [issue #25](https://github.com/jiangmiao/auto-pairs/issues/25)
289-
290-
Description: When works with viki `<CR>` will output viki#ExprMarkInexistentInElement('ParagraphVisible','<CR>')
291-
Reason: viki uses `<expr>` on mapping `<CR>` that auto-pairs cannot expanding.
292-
Solution A: Add
293-
294-
autocmd filetype viki inoremap <buffer> <silent> <CR> <C-R>=viki#ExprMarkInexistentInElement('ParagraphVisible',"\n")<CR><C-R>=AutoPairsReturn()<CR>`
295-
296-
to .vimrc, it will make viki and auto-pairs works together.
297-
298-
Solution B: add `let g:AutoPairsMapCR = 0` to .vimrc to disable `<CR>` mapping.
299-
300-
Remarks: Solution A need NOT add `let g:AutoPairsMapCR = 0` to .vimrc, if Solution A still cannot work, then have to use Solution B to disable auto-pairs `<CR>`.
301-
302272
Breaks '.' - [issue #3](https://github.com/jiangmiao/auto-pairs/issues/3)
303273

304274
Description: After entering insert mode and inputing `[hello` then leave insert
@@ -310,3 +280,14 @@ Contributors
310280
------------
311281
* [camthompson](https://github.com/camthompson)
312282

283+
284+
License
285+
-------
286+
287+
Copyright (C) 2011-2013 Miao Jiang
288+
289+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
290+
291+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
292+
293+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

plugin/auto-pairs.vim

Lines changed: 91 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
" Insert or delete brackets, parens, quotes in pairs.
22
" Maintainer: JiangMiao <jiangfriend@gmail.com>
33
" Contributor: camthompson
4-
" Last Change: 2013-02-16
5-
" Version: 1.3.0
4+
" Last Change: 2013-04-01
5+
" Version: 1.3.1
66
" Homepage: http://www.vim.org/scripts/script.php?script_id=3599
77
" Repository: https://github.com/jiangmiao/auto-pairs
8+
" License: MIT
89

910
if exists('g:AutoPairsLoaded') || &cp
1011
finish
@@ -58,6 +59,10 @@ if !exists('g:AutoPairsShortcutBackInsert')
5859
let g:AutoPairsShortcutBackInsert = '<M-b>'
5960
endif
6061

62+
if !exists('g:AutoPairsSmartQuotes')
63+
let g:AutoPairsSmartQuotes = 1
64+
endif
65+
6166

6267
" Will auto generated {']' => '[', ..., '}' => '{'}in initialize.
6368
let g:AutoPairsClosedPairs = {}
@@ -70,10 +75,12 @@ function! AutoPairsInsert(key)
7075

7176
let line = getline('.')
7277
let pos = col('.') - 1
73-
let next_chars = split(strpart(line, pos), '\zs')
78+
let before = strpart(line, 0, pos)
79+
let after = strpart(line, pos)
80+
let next_chars = split(after, '\zs')
7481
let current_char = get(next_chars, 0, '')
7582
let next_char = get(next_chars, 1, '')
76-
let prev_chars = split(strpart(line, 0, pos), '\zs')
83+
let prev_chars = split(before, '\zs')
7784
let prev_char = get(prev_chars, -1, '')
7885

7986
let eol = 0
@@ -119,7 +126,7 @@ function! AutoPairsInsert(key)
119126
endif
120127
endif
121128

122-
" Input directly if the key is not an open key
129+
" Insert directly if the key is not an open key
123130
return a:key
124131
end
125132

@@ -146,6 +153,37 @@ function! AutoPairsInsert(key)
146153
end
147154
end
148155

156+
let quotes_num = 0
157+
" Ignore comment line for vim file
158+
if &filetype == 'vim' && a:key == '"'
159+
if before =~ '^\s*$'
160+
return a:key
161+
end
162+
if before =~ '^\s*"'
163+
let quotes_num = -1
164+
end
165+
end
166+
167+
" Keep quote number is odd.
168+
" Because quotes should be matched in the same line in most of situation
169+
if g:AutoPairsSmartQuotes && open == close
170+
" Remove \\ \" \'
171+
let cleaned_line = substitute(line, '\v(\\.)', '', 'g')
172+
let n = quotes_num
173+
let pos = 0
174+
while 1
175+
let pos = stridx(cleaned_line, open, pos)
176+
if pos == -1
177+
break
178+
end
179+
let n = n + 1
180+
let pos = pos + 1
181+
endwhile
182+
if n % 2 == 1
183+
return a:key
184+
endif
185+
endif
186+
149187
return open.close."\<Left>"
150188
endfunction
151189

@@ -201,6 +239,11 @@ function! AutoPairsDelete()
201239
" Delete (|__\n___)
202240
let nline = getline(line('.')+1)
203241
if nline =~ '^\s*'.close
242+
if &filetype == 'vim' && prev_char == '"'
243+
" Keep next line's comment
244+
return "\<BS>"
245+
end
246+
204247
let space = matchstr(nline, '^\s*')
205248
return "\<BS>\<DEL>". repeat("\<DEL>", len(space)+1)
206249
end
@@ -267,15 +310,20 @@ function! AutoPairsFastWrap()
267310
end
268311
return "\<RIGHT>".inputed_close_pair."\<LEFT>"
269312
else
270-
normal e
313+
normal he
271314
return "\<RIGHT>".current_char."\<LEFT>"
272315
end
273316
endfunction
274317

275318
function! AutoPairsMap(key)
276-
let escaped_key = substitute(a:key, "'", "''", 'g')
319+
" | is special key which separate map command from text
320+
let key = a:key
321+
if key == '|'
322+
let key = '<BAR>'
323+
end
324+
let escaped_key = substitute(key, "'", "''", 'g')
277325
" use expr will cause search() doesn't work
278-
execute 'inoremap <buffer> <silent> '.a:key." <C-R>=AutoPairsInsert('".escaped_key."')<CR>"
326+
execute 'inoremap <buffer> <silent> '.key." <C-R>=AutoPairsInsert('".escaped_key."')<CR>"
279327
endfunction
280328

281329
function! AutoPairsToggle()
@@ -396,9 +444,7 @@ endfunction
396444

397445
function! s:ExpandMap(map)
398446
let map = a:map
399-
if map =~ '<Plug>'
400-
let map = substitute(map, '\(<Plug>\w\+\)', '\=maparg(submatch(1), "i")', 'g')
401-
endif
447+
let map = substitute(map, '\(<Plug>\w\+\)', '\=maparg(submatch(1), "i")', 'g')
402448
return map
403449
endfunction
404450

@@ -421,26 +467,43 @@ function! AutoPairsTryInit()
421467
" Buffer level keys mapping
422468
" comptible with other plugin
423469
if g:AutoPairsMapCR
424-
let old_cr = maparg('<CR>', 'i')
425-
if old_cr == ''
426-
let old_cr = '<CR>'
470+
if v:version >= 703 && has('patch32')
471+
" VIM 7.3 supports advancer maparg which could get <expr> info
472+
" then auto-pairs could remap <CR> in any case.
473+
let info = maparg('<CR>', 'i', 0, 1)
474+
if empty(info)
475+
let old_cr = '<CR>'
476+
let is_expr = 0
477+
else
478+
let old_cr = info['rhs']
479+
let old_cr = s:ExpandMap(old_cr)
480+
let old_cr = substitute(old_cr, '<SID>', '<SNR>' . info['sid'] . '_', 'g')
481+
let is_expr = info['expr']
482+
let wrapper_name = '<SID>AutoPairsOldCRWrapper73'
483+
endif
427484
else
428-
let old_cr = s:ExpandMap(old_cr)
429-
endif
430-
431-
" compatible with clang_complete
432-
" https://github.com/jiangmiao/auto-pairs/issues/18
433-
let pattern = '<SNR>\d\+_HandlePossibleSelectionEnter()'
434-
if old_cr =~ pattern
435-
execute 'imap <expr> <script> <SID>AutoPairsClangCompleteCR ' . matchstr(old_cr, pattern)
436-
let old_cr = substitute(old_cr, pattern , '<SID>AutoPairsClangCompleteCR', '')
437-
endif
485+
" VIM version less than 7.3
486+
" the mapping's <expr> info is lost, so guess it is expr or not, it's
487+
" not accurate.
488+
let old_cr = maparg('<CR>', 'i')
489+
if old_cr == ''
490+
let old_cr = '<CR>'
491+
let is_expr = 0
492+
else
493+
let old_cr = s:ExpandMap(old_cr)
494+
" old_cr contain (, I guess the old cr is in expr mode
495+
let is_expr = old_cr =~ '\V(' && toupper(old_cr) !~ '\V<C-R>'
496+
let wrapper_name = '<SID>AutoPairsOldCRWrapper'
497+
end
498+
end
438499

439500
if old_cr !~ 'AutoPairsReturn'
440-
" generally speaking, <silent> should not be here because every plugin
441-
" has there own silent solution. but for some plugin which wasn't double silent
442-
" mapping, when maparg expand the map will lose the silent info, so <silent> always.
443-
" use inoremap for neocomplcache
501+
if is_expr
502+
" remap <expr> to `name` to avoid mix expr and non-expr mode
503+
execute 'inoremap <buffer> <expr> <script> '. wrapper_name . ' ' . old_cr
504+
let old_cr = wrapper_name
505+
end
506+
" Alawys slient mapping
444507
execute 'inoremap <script> <buffer> <silent> <CR> '.old_cr.'<SID>AutoPairsReturn'
445508
end
446509
endif

0 commit comments

Comments
 (0)