61 Commits

Author SHA1 Message Date
23e77decb1 Bump version to 1.3.0 2013-02-16 14:02:32 +08:00
a9f53155e8 Update README. 2013-02-16 14:02:13 +08:00
c0593a61c7 Delete duplicated pairs in one time.
eg ```|``` {{|}} [[|]]
2013-02-12 14:02:25 +08:00
f11f3e5ee3 updated action on triple quote to add second line
changed the action on triple quote to add a closing quote and
start editing on the newly opened line inside the quotes.  the
only way to jump beyond the quotes is to escape and scroll down.
this is because a <CR> should give a new line, not jump to closing
quotes.
2013-02-11 09:56:54 -05:00
dd1ea6b1f7 Bump to v1.2.9 2013-01-15 15:27:23 +08:00
36ebfb6f29 improve #29 Buffer level pairs controlling. 2012-12-24 00:53:22 +08:00
0cabb1be77 bump to v1.2.8 2012-12-18 22:27:43 +08:00
ba81f659e7 fixes #27 <BS> cannot work when AutoPairs is turn off. 2012-12-01 13:06:15 +08:00
d59bc39f75 fixes incorrect AutoPairsDelete
[|{}
    ]

    press <BS> at | will become ]
2012-11-29 17:13:04 +08:00
713a33318a bump to v1.2.7 2012-11-02 09:27:26 +08:00
f8fa522790 Update vimwiki compatible hint. 2012-10-29 14:50:45 +08:00
7287467e64 update viki compatible hints in README 2012-10-25 01:18:19 +08:00
0ef881effd update viki compatible hints in README 2012-10-25 01:13:24 +08:00
8e9bee94c8 Add viki compatible hints to README 2012-10-25 00:59:21 +08:00
3c18515955 fixes #24 when equalprg is setting AutoPairsReturn cannot work well 2012-10-19 00:05:10 +08:00
652175babc bump to v1.2.6 2012-10-17 09:40:12 +08:00
9758a447cf fixes #23: Support typographer's quote pairs, such as ‘, ’, “, ” 2012-10-09 15:00:34 +08:00
1c5046f8c3 bump to v1.2.5 2012-09-15 12:08:47 +08:00
4a02e2c488 Fixes indentation incorrect when returning below 1/3 bottom of window. 2012-08-30 14:59:44 +08:00
28dc48fd9d bump to v1.2.4 2012-08-17 00:22:13 +08:00
27123dda7c update README 2012-08-11 22:30:11 +08:00
30c9108f80 fixes #20 Errors in pair creation that is split by ENTER 2012-08-11 22:29:33 +08:00
f24af6af48 bump to v1.2.3 2012-07-15 22:52:54 +08:00
0b39e635de add known issues 2012-07-15 22:50:09 +08:00
a9365b216d fixes #16 compatible with neocomplcache 2012-07-06 17:44:57 +08:00
559113fa2a fixes #18 compatible with clang_complete 2012-07-05 14:40:41 +08:00
d0d077a57d Improve Fast Wrap
(|)"foo"     TO ("foo")
    (|)"\\f\"oo" TO ("\\f\"oo")
    (|)func()    TO (func())
2012-05-23 12:46:42 +08:00
df761f3f28 Disable Fly mode by default. 2012-05-17 00:16:58 +08:00
1f45d88a95 bump to v1.2.1 2012-05-15 15:09:14 +08:00
49a24fc671 add some comments 2012-05-15 11:26:53 +08:00
ff597df8e8 silent the <CR> mapping forcely 2012-05-15 11:10:40 +08:00
ab206d7a5a fixes #14: sometimes press BS output strange words 2012-05-15 11:03:05 +08:00
106578d69e support back insert for any situation 2012-05-14 20:04:46 +08:00
eea7b44270 expand plug in map 2012-05-14 16:06:43 +08:00
96f779b075 update comment 2012-05-14 15:26:40 +08:00
82c649c522 compatible with vim-endwise, supertab. 2012-05-14 15:22:43 +08:00
a01f22e5ad fixes #12 : comptible with supertab.vim 2012-05-14 14:23:22 +08:00
80c6af14a2 expandtab 2012-05-14 14:23:08 +08:00
dceeb70a93 Update README 2012-05-14 10:19:58 +08:00
331060ff4b New Feature: Fly Mode 2012-05-14 09:54:25 +08:00
b6a478f891 bump to 1.1.6 2012-03-22 15:20:11 +08:00
48232b5e3d fixes issue #9 Lowercase 'a' acute accent via opt-e not working
REMOVE g:AutoPairsShortcuts
ADD g:AutoPairsShortcutJump
2012-03-20 13:13:39 +08:00
8026a0db4b Fixes #8 having errors with the double quotes when cpoptions is $ 2012-03-13 21:33:00 +08:00
a452553602 avoid overriding imap silent flag 2012-03-06 23:24:50 +08:00
1d5914910a always silent mapping 2012-03-06 23:12:55 +08:00
fac14bec63 support deleting empty multi-line pair 2012-03-06 15:52:38 +08:00
207107a684 Fixes #7: compatible with other plugin for key <CR> 2012-01-24 19:34:25 +08:00
10f56aad0e NEW support ```, ''', """ 2012-01-17 13:17:21 +08:00
6c3bcbca50 support ` 2012-01-17 13:14:56 +08:00
9d554d08be fix close single quote incorrect 2011-12-30 16:05:37 +08:00
7e4614d6aa add contributor list 2011-12-29 21:42:43 +08:00
e62479a542 Don't close single quotes when inside a word 2011-12-22 09:23:48 -05:00
583d0a4196 update README 2011-12-22 12:30:04 +08:00
d0fb9fe72b quick jump to the closed pair
{
      |
    }
    (press } at |)
    {

    }|
2011-12-21 18:29:04 +08:00
6a4b2ca820 Improve AutoPairsMapSpace Feature
MapSpace now only works with [], {}, ()

Delete last two space with one backspace,

    [ | ], press <BS> at |, output: [|]

Smarter paren insertion

    [ foo| ], press ] at |, ouput: [ foo ]|
2011-12-14 17:08:33 +08:00
75b6bfb804 Merge branch 'master' of https://github.com/camthompson/auto-pairs into map_space 2011-12-14 13:19:39 +08:00
283e12bded Fixes auto indent incorrect in JavaScript 2011-12-14 13:17:27 +08:00
21b4b1062c Add documentation for AutoPairsMapSpace 2011-12-13 17:10:23 -05:00
1522bff476 Add AutoPairsMapSpace option
When g:AutoPairsMapSpace == 1, a space key after a character in
AutoPairs will expand to a space after the previous character and a
space before the next character.

With ^ being the cursor position, pressing space will turn
[^] into [ ^ ].
2011-12-13 16:56:46 -05:00
e0d5dab300 Fixes auto indent incorrect in CoffeeScript 2011-12-07 19:56:14 +08:00
2d6a50c4b5 indent previous line after entering <CR>
https://github.com/jiangmiao/simple-javascript-indenter/issues/8
2011-11-14 17:10:18 +08:00
2 changed files with 532 additions and 91 deletions

235
README.md
View File

@ -9,12 +9,12 @@ copy plugin/auto-pairs.vim to ~/.vim/plugin
Features
--------
* Insert in pair
input: [
output: [|]
* Delete in pair
input: foo[<BS>
output: foo
@ -25,6 +25,22 @@ Features
|
}
* Insert spaces before closing characters, only for [], (), {}
input: {|} (press <SPACE> at |)
output: { | }
input: {|} (press <SPACE>foo} at |)
output: { foo }|
input: '|' (press <SPACE> at |)
output: ' |'
* Skip ' when inside a word
input: foo| (press ' at |)
output: foo'
* Skip closed bracket.
input: []
@ -37,9 +53,94 @@ Features
* Fast Wrap
input: |'hello' (press (<M-e> at|)
input: |'hello' (press (<M-e> at |)
output: ('hello')
wrap string, only support c style string
input: |'h\\el\'lo' (press (<M-e> at |)
output ('h\\ello\'')
input: |[foo, bar()] (press (<M-e> at |)
output: ([foo, bar()])
* Quick jump to closed pair.
input:
{
something;|
}
(press } at |)
output:
{
}|
* Support ``` ''' and """
input:
'''
output:
'''|'''
* Delete Repeated Pairs in one time
input: """|""" (press <BS> at |)
output: |
input: {{|}} (press <BS> at |)
output: |
input: [[[[[[|]]]]]] (press <BS> at |)
output: |
* Fly Mode
input: if(a[3)
output: if(a[3])| (In Fly Mode)
output: if(a[3)]) (Without Fly Mode)
input:
{
hello();|
world();
}
(press } at |)
output:
{
hello();
world();
}|
(then press <M-b> at | to do backinsert)
output:
{
hello();}|
world();
}
See Fly Mode section for details
Fly Mode
--------
Fly Mode will always force closed-pair jumping instead of inserting. only for ")", "}", "]"
If jumps in mistake, could use AutoPairsBackInsert(Default Key: <M-b>) to jump back and insert closed pair.
the most situation maybe want to insert single closed pair in the string, eg ")"
Fly Mode is DISABLED by default.
add **let g:AutoPairsFlyMode = 1** .vimrc to turn it on
Default Options:
let g:AutoPairsFlyMode = 0
let g:AutoPairsShortcutBackInsert = '<M-b>'
Shortcuts
---------
@ -47,20 +148,28 @@ Shortcuts
System Shortcuts:
<CR> : Insert new indented line after return if cursor in blank brackets or quotes.
<BS> : Delete brackets in pair
<M-p> : Toggle Autopairs
<M-e> : Fast Wrap
<M-p> : Toggle Autopairs (g:AutoPairsShortcutToggle)
<M-e> : Fast Wrap (g:AutoPairsShortcutFastWrap)
<M-n> : Jump to next closed pair (g:AutoPairsShortcutJump)
<M-b> : BackInsert
Optional Shortcuts:
could be turn off by let g:AutoPairsShortcuts = 0
<M-n> jump to next closed bracket.
<M-a> jump to end of line.
<M-o> jump to newline with indented.
If <M-p> <M-e> or <M-n> conflict with another keys or want to bind to another keys, add
let g:AutoPairShortcutToggle = '<another key>'
to .vimrc, it the key is empty string '', then the shortcut will be disabled.
Options
-------
* g:AutoPairs
Default: {'(':')', '[':']', '{':'}',"'":"'",'"':'"'}
Default: {'(':')', '[':']', '{':'}',"'":"'",'"':'"', '`':'`'}
* b:AutoPairs
Default: g:AutoPairs
Buffer level pairs set.
* g:AutoPairsShortcutToggle
@ -69,21 +178,18 @@ Options
The shortcut to toggle autopairs.
* g:AutoPairsShortcutFastWrap
Default: '<M-e>'
Fast wrap the word. all pairs will be consider as a block (include <>).
(|)'hello' after fast wrap at |, the word will be ('hello')
(|)<hello> after fast wrap at |, the word will be (<hello>)
* g:AutoPairsShortcuts
* g:AutoPairsShortcutJump
Default: 1
Default: '<M-n>'
imap 3 shortcuts
<M-n> jump to next closed bracket.
<M-a> jump to end of line.
<M-o> jump to newline with indented.
Jump to the next closed pair
* g:AutoPairsMapBS
@ -105,11 +211,102 @@ Options
When g:AutoPairsMapCR is on, center current line after return if the line is at the bottom 1/3 of the window.
* g:AutoPairsMapSpace
Default : 1
Map <space> to insert a space after the opening character and before the closing one.
execute 'inoremap <buffer> <silent> <CR> <C-R>=AutoPairsSpace()<CR>'
* g:AutoPairsFlyMode
Default : 0
set it to 1 to enable FlyMode.
see FlyMode section for details.
* g:AutoPairsShortcutBackInsert
Default : <M-b>
Work with FlyMode, insert the key at the Fly Mode jumped postion
Buffer Level Pairs Setting
--------------------------
Set b:AutoPairs before BufEnter
eg:
" When the filetype is FILETYPE then make AutoPairs only match for parenthesis
au Filetype FILETYPE let b:AutoPairs = {"(": ")"}
TroubleShooting
---------------
The script will remap keys ([{'"}]) <BS>,
The script will remap keys ([{'"}]) <BS>,
If auto pairs cannot work, use :imap ( to check if the map is corrected.
The correct map should be <C-R>=AutoPairsInsert("\(")<CR>
Or the plugin conflict with some other plugins.
use command :call AutoPairsInit() to remap the keys.
* How to insert parens purely
There are 3 ways
1. use Ctrl-V ) to insert paren without trigger the plugin.
2. use Alt-P to turn off the plugin.
3. use DEL or <C-O>x to delete the character insert by plugin.
Known Issues
-----------------------
There are the issues I cannot fix.
Compatible with Vimwiki - [issue #19](https://github.com/jiangmiao/auto-pairs/issues/19)
Description: When works with vimwiki `<CR>` will output `<SNR>xx_CR()`
Reason: vimwiki uses `<expr>` on mapping `<CR>` that auto-pairs cannot expanding.
Solution A: Add
" Copy from vimwiki.vim s:CR function for CR remapping
function! VimwikiCR()
let res = vimwiki#lst#kbd_cr()
if res == "\<CR>" && g:vimwiki_table_mappings
let res = vimwiki#tbl#kbd_cr()
endif
return res
endfunction
autocmd filetype vimwiki inoremap <buffer> <silent> <CR> <C-R>=VimwikiCR()<CR><C-R>=AutoPairsReturn()<CR>
to .vimrc, it will make vimwiki and auto-pairs 'Return' feature works together.
Solution B: add `let g:AutoPairsMapCR = 0` to .vimrc to disable `<CR>` mapping.
Compatible with viki - [issue #25](https://github.com/jiangmiao/auto-pairs/issues/25)
Description: When works with viki `<CR>` will output viki#ExprMarkInexistentInElement('ParagraphVisible','<CR>')
Reason: viki uses `<expr>` on mapping `<CR>` that auto-pairs cannot expanding.
Solution A: Add
autocmd filetype viki inoremap <buffer> <silent> <CR> <C-R>=viki#ExprMarkInexistentInElement('ParagraphVisible',"\n")<CR><C-R>=AutoPairsReturn()<CR>`
to .vimrc, it will make viki and auto-pairs works together.
Solution B: add `let g:AutoPairsMapCR = 0` to .vimrc to disable `<CR>` mapping.
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>`.
Breaks '.' - [issue #3](https://github.com/jiangmiao/auto-pairs/issues/3)
Description: After entering insert mode and inputing `[hello` then leave insert
mode by `<ESC>`. press '.' will insert 'hello' instead of '[hello]'.
Reason: `[` actually equals `[]\<LEFT>` and \<LEFT> will break '.'
Solution: none
Contributors
------------
* [camthompson](https://github.com/camthompson)

View File

@ -1,7 +1,8 @@
" Insert or delete brackets, parens, quotes in pairs.
" Maintainer: JiangMiao <jiangfriend@gmail.com>
" Last Change: 2011-09-06
" Version: 1.1.1
" Contributor: camthompson
" Last Change: 2013-02-16
" Version: 1.3.0
" Homepage: http://www.vim.org/scripts/script.php?script_id=3599
" Repository: https://github.com/jiangmiao/auto-pairs
@ -10,19 +11,13 @@ if exists('g:AutoPairsLoaded') || &cp
end
let g:AutoPairsLoaded = 1
" Shortcurs for
" <M-o> newline with indetation
" <M-a> jump to of line
" <M-n> jmup to next pairs
if !exists('g:AutoPairsShortcuts')
let g:AutoPairsShortcuts = 1
if !exists('g:AutoPairs')
let g:AutoPairs = {'(':')', '[':']', '{':'}',"'":"'",'"':'"', '`':'`'}
end
if !exists('g:AutoPairs')
let g:AutoPairs = {'(':')', '[':']', '{':'}',"'":"'",'"':'"'}
if !exists('g:AutoPairsParens')
let g:AutoPairsParens = {'(':')', '[':']', '{':'}'}
end
let g:AutoExtraPairs = copy(g:AutoPairs)
let g:AutoExtraPairs['<'] = '>'
if !exists('g:AutoPairsMapBS')
let g:AutoPairsMapBS = 1
@ -32,6 +27,10 @@ if !exists('g:AutoPairsMapCR')
let g:AutoPairsMapCR = 1
end
if !exists('g:AutoPairsMapSpace')
let g:AutoPairsMapSpace = 1
end
if !exists('g:AutoPairsCenterLine')
let g:AutoPairsCenterLine = 1
end
@ -44,8 +43,24 @@ if !exists('g:AutoPairsShortcutFastWrap')
let g:AutoPairsShortcutFastWrap = '<M-e>'
end
let g:AutoPairsClosedPairs = {}
if !exists('g:AutoPairsShortcutJump')
let g:AutoPairsShortcutJump = '<M-n>'
endif
" Fly mode will for closed pair to jump to closed pair instead of insert.
" also support AutoPairsBackInsert to insert pairs where jumped.
if !exists('g:AutoPairsFlyMode')
let g:AutoPairsFlyMode = 0
endif
" Work with Fly Mode, insert pair where jumped
if !exists('g:AutoPairsShortcutBackInsert')
let g:AutoPairsShortcutBackInsert = '<M-b>'
endif
" Will auto generated {']' => '[', ..., '}' => '{'}in initialize.
let g:AutoPairsClosedPairs = {}
function! AutoPairsInsert(key)
@ -54,8 +69,12 @@ function! AutoPairsInsert(key)
end
let line = getline('.')
let prev_char = line[col('.')-2]
let current_char = line[col('.')-1]
let pos = col('.') - 1
let next_chars = split(strpart(line, pos), '\zs')
let current_char = get(next_chars, 0, '')
let next_char = get(next_chars, 1, '')
let prev_chars = split(strpart(line, 0, pos), '\zs')
let prev_char = get(prev_chars, -1, '')
let eol = 0
if col('$') - col('.') <= 1
@ -67,40 +86,124 @@ function! AutoPairsInsert(key)
return a:key
end
" Skip the character if current character is the same as input
if current_char == a:key && !has_key(g:AutoPairs, a:key)
return "\<Right>"
end
" The key is difference open-pair, then it means only for ) ] } by default
if !has_key(b:AutoPairs, a:key)
let b:autopairs_saved_pair = [a:key, getpos('.')]
" Input directly if the key is not an open key
if !has_key(g:AutoPairs, a:key)
" Skip the character if current character is the same as input
if current_char == a:key
return "\<Right>"
end
if !g:AutoPairsFlyMode
" Skip the character if next character is space
if current_char == ' ' && next_char == a:key
return "\<Right>\<Right>"
end
" Skip the character if closed pair is next character
if current_char == ''
let next_lineno = line('.')+1
let next_line = getline(nextnonblank(next_lineno))
let next_char = matchstr(next_line, '\s*\zs.')
if next_char == a:key
return "\<ESC>e^a"
endif
endif
endif
" Fly Mode, and the key is closed-pairs, search closed-pair and jump
if g:AutoPairsFlyMode && has_key(b:AutoPairsClosedPairs, a:key)
if search(a:key, 'W')
return "\<Right>"
endif
endif
" Input directly if the key is not an open key
return a:key
end
let open = a:key
let close = g:AutoPairs[open]
let close = b:AutoPairs[open]
if current_char == close && open == close
return "\<Right>"
end
" Ignore auto close ' if follows a word
" MUST after closed check. 'hello|'
if a:key == "'" && prev_char =~ '\v\w'
return a:key
end
" support for ''' ``` and """
if open == close
" The key must be ' " `
let pprev_char = line[col('.')-3]
if pprev_char == open && prev_char == open
" Double pair found
return repeat(a:key, 4) . repeat("\<LEFT>", 3)
end
end
return open.close."\<Left>"
endfunction
function! AutoPairsDelete()
if !b:autopairs_enabled
return "\<BS>"
end
let line = getline('.')
let prev_char = line[col('.')-2]
let pprev_char = line[col('.')-3]
let pos = col('.') - 1
let current_char = get(split(strpart(line, pos), '\zs'), 0, '')
let prev_chars = split(strpart(line, 0, pos), '\zs')
let prev_char = get(prev_chars, -1, '')
let pprev_char = get(prev_chars, -2, '')
if pprev_char == '\'
return "\<BS>"
end
if has_key(g:AutoPairs, prev_char)
let close = g:AutoPairs[prev_char]
" Delete last two spaces in parens, work with MapSpace
if has_key(b:AutoPairs, pprev_char) && prev_char == ' ' && current_char == ' '
return "\<BS>\<DEL>"
endif
" Delete Repeated Pair eg: '''|''' [[|]] {{|}}
if has_key(b:AutoPairs, prev_char)
let times = 0
let p = -1
while get(prev_chars, p, '') == prev_char
let p = p - 1
let times = times + 1
endwhile
let close = b:AutoPairs[prev_char]
let left = repeat(prev_char, times)
let right = repeat(close, times)
let before = strpart(line, pos-times, times)
let after = strpart(line, pos, times)
if left == before && right == after
return repeat("\<BS>\<DEL>", times)
end
end
if has_key(b:AutoPairs, prev_char)
let close = b:AutoPairs[prev_char]
if match(line,'^\s*'.close, col('.')-1) != -1
" Delete (|___)
let space = matchstr(line, '^\s*', col('.')-1)
return "\<BS>". repeat("\<DEL>", len(space)+1)
elseif match(line, '^\s*$', col('.')-1) != -1
" Delete (|__\n___)
let nline = getline(line('.')+1)
if nline =~ '^\s*'.close
let space = matchstr(nline, '^\s*')
return "\<BS>\<DEL>". repeat("\<DEL>", len(space)+1)
end
end
end
@ -108,41 +211,71 @@ function! AutoPairsDelete()
endfunction
function! AutoPairsJump()
call search('[{("\[\]'')}]','W')
call search('["\]'')}]','W')
endfunction
" string_chunk cannot use standalone
let s:string_chunk = '\v%(\\\_.|[^\1]|[\r\n]){-}'
let s:ss_pattern = '\v''' . s:string_chunk . ''''
let s:ds_pattern = '\v"' . s:string_chunk . '"'
func! s:RegexpQuote(str)
return substitute(a:str, '\v[\[\{\(\<\>\)\}\]]', '\\&', 'g')
endf
func! s:RegexpQuoteInSquare(str)
return substitute(a:str, '\v[\[\]]', '\\&', 'g')
endf
" Search next open or close pair
func! s:FormatChunk(open, close)
let open = s:RegexpQuote(a:open)
let close = s:RegexpQuote(a:close)
let open2 = s:RegexpQuoteInSquare(a:open)
let close2 = s:RegexpQuoteInSquare(a:close)
if open == close
return '\v'.open.s:string_chunk.close
else
return '\v%(' . s:ss_pattern . '|' . s:ds_pattern . '|' . '[^'.open2.close2.']|[\r\n]' . '){-}(['.open2.close2.'])'
end
endf
" Fast wrap the word in brackets
function! AutoPairsFastWrap()
let line = getline('.')
let current_char = line[col('.')-1]
let next_char = line[col('.')]
" Ignore EOL
if col('.') == col('$')
return ''
end
normal! x
if match(next_char, '\s') != -1
call search('\S', 'W')
let next_char = getline('.')[col('.')-1]
let open_pair_pattern = '\v[({\[''"]'
let at_end = col('.') >= col('$') - 1
normal x
" Skip blank
if next_char =~ '\v\s' || at_end
call search('\v\S', 'W')
let line = getline('.')
let next_char = line[col('.')-1]
end
if has_key(g:AutoExtraPairs, next_char)
let close = g:AutoExtraPairs[next_char]
call search(close, 'W')
return "\<RIGHT>".current_char."\<LEFT>"
else
if match(next_char, '\w') != -1
execute "normal! he"
if has_key(b:AutoPairs, next_char)
let followed_open_pair = next_char
let inputed_close_pair = current_char
let followed_close_pair = b:AutoPairs[next_char]
if followed_close_pair != followed_open_pair
" TODO replace system searchpair to skip string and nested pair.
" eg: (|){"hello}world"} will transform to ({"hello})world"}
call searchpair('\V'.followed_open_pair, '', '\V'.followed_close_pair, 'W')
else
call search(s:FormatChunk(followed_open_pair, followed_close_pair), 'We')
end
execute "normal! a".current_char
return ""
return "\<RIGHT>".inputed_close_pair."\<LEFT>"
else
normal e
return "\<RIGHT>".current_char."\<LEFT>"
end
endfunction
function! AutoPairsMap(key)
execute 'inoremap <buffer> <silent> '.a:key.' <C-R>=AutoPairsInsert("\'.a:key.'")<CR>'
let escaped_key = substitute(a:key, "'", "''", 'g')
" use expr will cause search() doesn't work
execute 'inoremap <buffer> <silent> '.a:key." <C-R>=AutoPairsInsert('".escaped_key."')<CR>"
endfunction
function! AutoPairsToggle()
@ -157,55 +290,166 @@ function! AutoPairsToggle()
endfunction
function! AutoPairsReturn()
if b:autopairs_enabled == 0
return ''
end
let line = getline('.')
let pline = getline(line('.')-1)
let prev_char = pline[strlen(pline)-1]
let cmd = ''
let cur_char = line[col('.')-1]
if has_key(b:AutoPairs, prev_char) && b:AutoPairs[prev_char] == cur_char
if g:AutoPairsCenterLine && winline() * 3 >= winheight(0) * 2
" Use \<BS> instead of \<ESC>cl will cause the placeholder deleted
" incorrect. because <C-O>zz won't leave Normal mode.
" Use \<DEL> is a bit wierd. the character before cursor need to be deleted.
let cmd = " \<C-O>zz\<ESC>cl"
end
" If equalprg has been set, then avoid call =
" https://github.com/jiangmiao/auto-pairs/issues/24
if &equalprg != ''
return "\<ESC>O".cmd
endif
" conflict with javascript and coffee
" javascript need indent new line
" coffeescript forbid indent new line
if &filetype == 'coffeescript' || &filetype == 'coffee'
return "\<ESC>k==o".cmd
else
return "\<ESC>=ko".cmd
endif
end
return ''
endfunction
function! AutoPairsSpace()
let line = getline('.')
let prev_char = line[col('.')-2]
let cmd = ''
let cur_char = line[col('.')-1]
if has_key(g:AutoPairs, prev_char) && g:AutoPairs[prev_char] == cur_char
if g:AutoPairsCenterLine && winline() * 1.5 >= winheight(0)
let cmd = " \<C-O>zz\<ESC>cl"
end
return "\<DEL>\<CR>".cur_char."\<C-O>O".cmd
end
return "\<CR>"
let cur_char =line[col('.')-1]
if has_key(g:AutoPairsParens, prev_char) && g:AutoPairsParens[prev_char] == cur_char
let cmd = "\<SPACE>\<LEFT>"
endif
return "\<SPACE>".cmd
endfunction
function! AutoPairsBackInsert()
if exists('b:autopairs_saved_pair')
let pair = b:autopairs_saved_pair[0]
let pos = b:autopairs_saved_pair[1]
call setpos('.', pos)
return pair
endif
return ''
endfunction
function! AutoPairsInit()
let b:autopairs_loaded = 1
let b:autopairs_enabled = 1
for [open, close] in items(g:AutoPairs)
let b:AutoPairsClosedPairs = {}
if !exists('b:AutoPairs')
let b:AutoPairs = g:AutoPairs
end
" buffer level map pairs keys
for [open, close] in items(b:AutoPairs)
call AutoPairsMap(open)
if open != close
call AutoPairsMap(close)
end
let g:AutoPairsClosedPairs[close] = 1
let b:AutoPairsClosedPairs[close] = open
endfor
" Still use <buffer> level mapping for <BS> <SPACE>
if g:AutoPairsMapBS
execute 'inoremap <buffer> <silent> <expr> <BS> AutoPairsDelete()'
" Use <C-R> instead of <expr> for issue #14 sometimes press BS output strange words
execute 'inoremap <buffer> <silent> <BS> <C-R>=AutoPairsDelete()<CR>'
end
if g:AutoPairsMapCR
execute 'inoremap <buffer> <silent> <expr> <CR> AutoPairsReturn()'
if g:AutoPairsMapSpace
execute 'inoremap <buffer> <silent> <SPACE> <C-R>=AutoPairsSpace()<CR>'
end
execute 'inoremap <buffer> <silent> '.g:AutoPairsShortcutFastWrap.' <C-R>=AutoPairsFastWrap()<CR>'
execute 'inoremap <buffer> <silent> <expr> '.g:AutoPairsShortcutToggle.' AutoPairsToggle()'
execute 'noremap <buffer> <silent> '.g:AutoPairsShortcutToggle.' :call AutoPairsToggle()<CR>'
" If the keys map conflict with your own settings, delete or change them
if g:AutoPairsShortcuts
execute 'inoremap <buffer> <silent> <M-n> <ESC>:call AutoPairsJump()<CR>a'
execute 'inoremap <buffer> <silent> <M-a> <END>'
execute 'inoremap <buffer> <silent> <M-o> <END><CR>'
if g:AutoPairsShortcutFastWrap != ''
execute 'inoremap <buffer> <silent> '.g:AutoPairsShortcutFastWrap.' <C-R>=AutoPairsFastWrap()<CR>'
end
if g:AutoPairsShortcutBackInsert != ''
execute 'inoremap <buffer> <silent> '.g:AutoPairsShortcutBackInsert.' <C-R>=AutoPairsBackInsert()<CR>'
end
if g:AutoPairsShortcutToggle != ''
" use <expr> to ensure showing the status when toggle
execute 'inoremap <buffer> <silent> <expr> '.g:AutoPairsShortcutToggle.' AutoPairsToggle()'
execute 'noremap <buffer> <silent> '.g:AutoPairsShortcutToggle.' :call AutoPairsToggle()<CR>'
end
if g:AutoPairsShortcutJump != ''
execute 'inoremap <buffer> <silent> ' . g:AutoPairsShortcutJump. ' <ESC>:call AutoPairsJump()<CR>a'
execute 'noremap <buffer> <silent> ' . g:AutoPairsShortcutJump. ' :call AutoPairsJump()<CR>'
end
endfunction
function! AutoPairsForceInit()
function! s:ExpandMap(map)
let map = a:map
if map =~ '<Plug>'
let map = substitute(map, '\(<Plug>\w\+\)', '\=maparg(submatch(1), "i")', 'g')
endif
return map
endfunction
function! AutoPairsTryInit()
if exists('b:autopairs_loaded')
return
else
call AutoPairsInit()
end
" for auto-pairs starts with 'a', so the priority is higher than supertab and vim-endwise
"
" vim-endwise doesn't support <Plug>AutoPairsReturn
" when use <Plug>AutoPairsReturn will cause <Plug> isn't expanded
"
" supertab doesn't support <SID>AutoPairsReturn
" when use <SID>AutoPairsReturn will cause Duplicated <CR>
"
" and when load after vim-endwise will cause unexpected endwise inserted.
" so always load AutoPairs at last
" Buffer level keys mapping
" comptible with other plugin
if g:AutoPairsMapCR
let old_cr = maparg('<CR>', 'i')
if old_cr == ''
let old_cr = '<CR>'
else
let old_cr = s:ExpandMap(old_cr)
endif
" compatible with clang_complete
" https://github.com/jiangmiao/auto-pairs/issues/18
let pattern = '<SNR>\d\+_HandlePossibleSelectionEnter()'
if old_cr =~ pattern
execute 'imap <expr> <script> <SID>AutoPairsClangCompleteCR ' . matchstr(old_cr, pattern)
let old_cr = substitute(old_cr, pattern , '<SID>AutoPairsClangCompleteCR', '')
endif
if old_cr !~ 'AutoPairsReturn'
" generally speaking, <silent> should not be here because every plugin
" has there own silent solution. but for some plugin which wasn't double silent
" mapping, when maparg expand the map will lose the silent info, so <silent> always.
" use inoremap for neocomplcache
execute 'inoremap <script> <buffer> <silent> <CR> '.old_cr.'<SID>AutoPairsReturn'
end
endif
call AutoPairsInit()
endfunction
au BufEnter * :call AutoPairsForceInit()
" Always silent the command
inoremap <silent> <SID>AutoPairsReturn <C-R>=AutoPairsReturn()<CR>
imap <script> <Plug>AutoPairsReturn <SID>AutoPairsReturn
au BufEnter * :call AutoPairsTryInit()