59 Commits

Author SHA1 Message Date
4ec359716a Bump version 1.3.4 2019-01-15 04:18:06 +08:00
92e5ece6a3 Merge pull request #227 from Felixoid/master
Fix breaking of keymap behavior
2019-01-14 23:07:33 +08:00
cf921dadce Fix breaking of keymap behavior 2019-01-12 16:09:52 +01:00
9086ce897a Merge pull request #221 from zoresvit/patch-1
Spell fix
2018-09-24 00:02:14 +08:00
3c290d9308 Spell fix
Remove duplicated word.
2018-09-18 17:42:41 -07:00
f0019fc642 Merge pull request #185 from d10n/fix-filename
Fix filename
2017-07-03 12:02:43 +08:00
c00cc24c8e Fix filename 2017-07-02 20:23:39 -04:00
6afc850e24 New feature: Move character under the cursor to the pair. 2017-06-17 01:23:20 +08:00
20ec5b043f Merge pull request #174 from grodzik/master
Added .gitignore with doc/tags entry
2017-03-22 04:16:39 -05:00
3bd07a4eeb Added .gitignore with doc/tags entry 2017-03-21 20:04:05 +01:00
ff9160e6d8 Merge pull request #173 from zoresvit/master
Add Vim help file
2017-03-12 04:03:52 -05:00
23f1c89508 Add Vim help file. 2017-03-11 13:16:45 -08:00
e915d857fe Merge pull request #166 from bombela/patch-1
Use `normal!`
2017-01-26 03:23:42 -06:00
69bfaf0fdd Use normal!
Use `normal!` to avoid collision with user defined mappings.
2017-01-23 14:51:25 -08:00
8451816810 Merge pull request #162 from vbauerster/trailingws
remove trailing white spaces
2016-11-21 08:15:07 -06:00
edf7ab9a9e remove trailing white spaces 2016-11-21 11:29:39 +05:00
b0c9fce101 Merge pull request #161 from smiechowy/master
Fix checking for patch849
2016-11-20 02:40:26 -06:00
ea8ee08f44 Merge pull request #157 from davepagurek/fix-overwriting-register
Fix overwriting copied text when recentering
2016-11-20 02:40:01 -06:00
2d4071038a Fix checking for patch849 2016-11-14 11:52:47 +01:00
33ecfd759d Recenter before adding line, fix overwriting copy 2016-10-29 11:47:04 -07:00
1b3a1efb07 Merge pull request #147 from davepagurek/master
Fix double-escape being needed
2016-07-17 09:49:46 +08:00
63b0c1b249 Fix double-escape being needed 2016-07-16 01:23:20 -04:00
5979999a17 won't break repeat if jump by the closed pair in the same line 2016-03-28 13:12:41 +08:00
15256d0e94 Update README 2016-03-28 03:14:12 +08:00
65a7237b2b Fixes #119, allow disabling <C-h> mapping. 2016-03-28 03:11:14 +08:00
0c30ba85f0 Fixes #3, breaks '.'. 2016-03-28 03:06:51 +08:00
7f548cbb18 Don't break undo with left/right cursor movement. 2016-02-17 13:42:20 -05:00
b647ef54cd Merge pull request #106 from tienslebien/patch-1
Update README.md
2015-04-07 22:46:38 +08:00
26289c6430 Update README.md 2015-04-06 23:39:43 +02:00
7390859102 Merge pull request #89 from docwhat/close-across-lines
option to only search current line for closing pair
2015-02-11 02:44:28 +08:00
9828061855 option to only search current line for closing pair
This adds the `g:AutoPairsMultilineClose` option.
2014-05-24 00:03:51 -04:00
c2f60ca2e5 Fixes #63, skip checking patch if the version is greater than 703. 2013-10-20 10:11:05 +08:00
062a5de551 Fixes #65, Incorrect option name 2013-10-15 00:20:01 +08:00
0e27b0a9b1 Fixes #63, skip checking patch if the version is greater than 703. 2013-10-15 00:19:22 +08:00
8e07c8de31 Compatible with xmleditor. 2013-09-03 16:39:44 +08:00
8f4598b883 Bump version to 1.3.2 2013-07-13 12:00:27 +08:00
4390981ccf Merge pull request #49 from scottmcginness/master
Try to use space mapping without breaking abbreviations
2013-05-21 05:54:28 -07:00
1484f6501d Try to use abbreviation before using space mapping 2013-05-20 19:56:12 +01:00
275b47353f Merge pull request #48 from halftan/master
Added key map for <C-H>, #47
2013-05-07 10:53:19 -07:00
6a705983d1 <c-h> now behaves the same as <BS>
mean that it will delete the closing parentheses as well
2013-05-07 21:24:36 +08:00
9617a648c5 Bump version to 1.3.1 2013-04-01 04:05:03 +08:00
a5f9ac7f43 Fix #46, Incompatible CR mapping with supertab. 2013-03-31 12:17:42 +08:00
37076cb15f Correct version check for maparg ext, introducetd from Vim 7.3.32, #44. 2013-03-28 03:47:53 +08:00
b7ae7d0ba8 Fix incorrect is_expr. 2013-03-27 17:24:17 +08:00
5d713be821 Update map expr checking. 2013-03-27 16:39:52 +08:00
24ffa783f0 Fix #44, compatible with old vim before 7.3. 2013-03-27 15:52:29 +08:00
0a79f50acb Wrap old CR in buffer level. 2013-03-23 18:02:09 +08:00
85a9a7daac Improve <CR> mapping compatible. #43 2013-03-23 16:56:21 +08:00
d23864fdd6 Merge pull request #42 from halftan/master
Update README, correct the installation destination by pathogen.
2013-03-19 07:28:18 -07:00
4f1643fbc9 amend last commit
Signed-off-by: HalFtaN <half.a.tan@gmail.com>
2013-03-19 22:16:39 +08:00
64a10a60eb Merge pull request #41 from halftan/master
Added installation instruction for users who use pathogen plugin
2013-03-19 07:06:33 -07:00
cc03f6e915 installation instruction for pathogen users
Signed-off-by: HalFtaN <half.a.tan@gmail.com>
2013-03-19 21:22:57 +08:00
b3729e8779 Add License MIT #40 2013-03-17 19:32:25 +08:00
f13a60913f Fixes #39, Backspace removes additional " (quote) on line below. 2013-03-17 11:58:28 +08:00
a86ea24a0f Update README. 2013-03-05 13:28:18 +08:00
e5f17cf9e2 Improve #37, ignore comment line for vim file. 2013-03-04 22:04:15 +08:00
fc5b845f6b Improve #36, Keep quotes number is odd in one line. 2013-03-04 14:19:13 +08:00
2982312bae fixes #35: Fast Wrap incorrect.
(|)p[i] to (p[)i]
should be (p[i])
2013-03-02 19:06:01 +08:00
f6a18ca775 fixes #33: character '|' map failed. 2013-02-18 11:23:05 +08:00
4 changed files with 605 additions and 94 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
doc/tags

View File

@ -6,6 +6,10 @@ Installation
------------
copy plugin/auto-pairs.vim to ~/.vim/plugin
or if you are using `pathogen`:
```git clone git://github.com/jiangmiao/auto-pairs.git ~/.vim/bundle/auto-pairs```
Features
--------
* Insert in pair
@ -15,7 +19,7 @@ Features
* Delete in pair
input: foo[<BS>
input: foo[<BS>]
output: foo
* Insert new indented line after Return
@ -63,6 +67,14 @@ Features
input: |[foo, bar()] (press (<M-e> at |)
output: ([foo, bar()])
* Quick move char to closed pair
input: (|){["foo"]} (press <M-}> at |)
output: ({["foo"]}|)
input: |[foo, bar()] (press (<M-]> at |)
output: ([foo, bar()]|)
* Quick jump to closed pair.
input:
@ -129,7 +141,7 @@ 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.
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 ")"
@ -151,13 +163,13 @@ Shortcuts
<M-p> : Toggle Autopairs (g:AutoPairsShortcutToggle)
<M-e> : Fast Wrap (g:AutoPairsShortcutFastWrap)
<M-n> : Jump to next closed pair (g:AutoPairsShortcutJump)
<M-b> : BackInsert
<M-b> : BackInsert (g:AutoPairsShortcutBackInsert)
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>'
let g:AutoPairsShortcutToggle = '<another key>'
to .vimrc, it the key is empty string '', then the shortcut will be disabled.
to .vimrc, if the key is empty string '', then the shortcut will be disabled.
Options
-------
@ -198,6 +210,12 @@ Options
Map <BS> to delete brackets, quotes in pair
execute 'inoremap <buffer> <silent> <BS> <C-R>=AutoPairsDelete()<CR>'
* g:AutoPairsMapCh
Default : 1
Map <C-h> to delete brackets, quotes in pair
* g:AutoPairsMapCR
Default : 1
@ -225,12 +243,27 @@ Options
set it to 1 to enable FlyMode.
see FlyMode section for details.
* g:AutoPairsMultilineClose
Default : 1
When you press the key for the closing pair (e.g. `)`) it jumps past it.
If set to 1, then it'll jump to the next line, if there is only whitespace.
If set to 0, then it'll only jump to a closing pair on the same line.
* g:AutoPairsShortcutBackInsert
Default : <M-b>
Work with FlyMode, insert the key at the Fly Mode jumped postion
* g:AutoPairsMoveCharacter
Default: "()[]{}\"'"
Map <M-(> <M-)> <M-[> <M-]> <M-{> <M-}> <M-"> <M-'> to
move character under the cursor to the pair.
Buffer Level Pairs Setting
--------------------------
@ -260,53 +293,33 @@ TroubleShooting
3. use DEL or <C-O>x to delete the character insert by plugin.
* Swedish Character Conflict
Because AutoPairs uses Meta(Alt) key as shortcut, it is conflict with some Swedish character such as å.
To fix the issue, you need remap or disable the related shortcut.
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
Reason: `[` actually equals `[]\<LEFT>` and \<LEFT> will break '.'.
After version 7.4.849, Vim implements new keyword <C-G>U to avoid the break
Solution: Update Vim to 7.4.849+
Contributors
------------
* [camthompson](https://github.com/camthompson)
License
-------
Copyright (C) 2011-2013 Miao Jiang
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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.

356
doc/AutoPairs.txt Normal file
View File

@ -0,0 +1,356 @@
*AutoPairs.txt* Insert or delete brackets, parens, quotes in pair
Author: jiangmiao
License: MIT
URL: https://github.com/jiangmiao/auto-pairs
==============================================================================
CONTENTS *autopairs-contents*
1. Installation ............................. |autopairs-installation|
2. Features ..................................... |autopairs-features|
3. Fly Mode ..................................... |autopairs-fly-mode|
4. Shortcuts ................................... |autopairs-shortcuts|
5. Options ....................................... |autopairs-options|
6. Troubleshooting ...................... |autopairs-troubleshooting|
==============================================================================
1. Introduction *autopairs-installation*
Copy `plugin/auto-pairs.vim` to `~/.vim/plugin`.
Or if you are using `pathogen`: >
git clone git://github.com/jiangmiao/auto-pairs.git ~/.vim/bundle/auto-pairs
==============================================================================
2. Features *autopairs-features*
Insert in pair: >
input: [
output: [|]
Delete in pair: >
input: foo[<BS>]
output: foo
Insert new indented line after Return: >
input: {|} (press <CR> at |)
output: {
|
}
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: []
output: []
Ignore auto pair when previous character is '\': >
input: "\'
output: "\'"
Fast Wrap: >
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 (|autopairs-flymode|): >
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
==============================================================================
3. Fly Mode *autopairs-flymode*
Fly Mode will always force closed-pair jumping instead of inserting. Only for
")", "}", "]". If jumps in mistake, you can use |g:AutoPairsBackInsert| (default
Key: <M-b>) to jump back and insert closed pair.
The most situation maybe you want to insert single closed pair in the string,
eg: >
")"
Fly Mode is DISABLED by default. To enable Fly Mode add following to your
'.vimrc': >
let g:AutoPairsFlyMode = 1
Default Options: >
let g:AutoPairsFlyMode = 0
let g:AutoPairsShortcutBackInsert = '<M-b>'
==============================================================================
4. Shortcuts *autopairs-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 (|g:AutoPairsShortcutToggle|)
<M-e>: Fast Wrap (|g:AutoPairsShortcutFastWrap|)
<M-n>: Jump to next closed pair (|g:AutoPairsShortcutJump|)
<M-b>: BackInsert (|g:AutoPairsShortcutBackInsert|)
To rebind keys <M-p>, <M-e> or <M-n> or in case of conflicts with
another keys:
let g:AutoPairsShortcutToggle = '<another key>'
If the key is empty string '', then the shortcut will be disabled.
==============================================================================
5. Options *autopairs-options*
*g:AutoPairs*
|g:AutoPairs| dict
Default: >
{'(':')', '[':']', '{':'}',"'":"'",'"':'"', '`':'`'}
Specifies which symbols should be automatically paired.
To append new pairs without overwriting defaults, add values in your `.vimrc`.:
let g:AutoPairs['<']='>'
This example will enable matching of `<` with `>`.
*b:AutoPairs*
|b:AutoPairs| dict
Default: |g:AutoPairs|
Buffer level pairs set.
You can set |b:AutoPairs| before |BufEnter|: >
au Filetype FILETYPE let b:AutoPairs = {"(": ")"}
This sets |AutoPairs| to only match for parenthesis for 'FILETYPE'.
*g:AutoPairsShortcutToggle*
|g:AutoPairsShortcutToggle| string
Default: <M-p>
The shortcut to toggle autopairs.
*g:AutoPairsShortcutFastWrap*
|g:AutoPairsShortcutFastWrap| string
Default: <M-e>
Fast wrap the word. All pairs will be considered as a block (including <>).
(|)'hello' after fast wrap at |, the word will be ('hello')
(|)<hello> after fast wrap at |, the word will be (<hello>)
*g:AutoPairsShortcutJump*
|g:AutoPairsShortcutJump| string
Default: <M-n>
Jump to the next closed pair.
*g:AutoPairsShortcutBackInsert*
|g:AutoPairsShortcutBackInsert| string
Default: <M-b>
Work with |autopairs-flymode|, insert the key at the Fly Mode jumped position.
*g:AutoPairsMapBS*
|g:AutoPairsMapBS| int
Default: 1
Map <BS> to delete brackets and quotes in pair, executes:
inoremap <buffer> <silent> <BS> <C-R>=AutoPairsDelete()<CR>
*g:AutoPairsMapCh*
|g:AutoPairsMapCh| int
Default: 1
Map <C-h> to delete brackets and quotes in pair.
*g:AutoPairsMapCR*
|g:AutoPairsMapCR| int
Default: 1
Map <CR> to insert a new indented line if cursor in (|), {|} [|], '|', "|".
Executes:
inoremap <buffer> <silent> <CR> <C-R>=AutoPairsReturn()<CR>
*g:AutoPairsCenterLine*
|g:AutoPairsCenterLine| int
Default: 1
When |g:AutoPairsMapCR| is on, center current line after return if the line
is at the bottom 1/3 of the window.
*g:AutoPairsMapSpace*
|g:AutoPairsMapSpace| int
Default: 1
Map <space> to insert a space after the opening character and before the
closing one.
Executes:
inoremap <buffer> <silent> <CR> <C-R>=AutoPairsSpace()<CR>
*g:AutoPairsFlyMode*
|g:AutoPairsFlyMode| int
Default: 0
Set it to 1 to enable |autopairs-flymode|.
*g:AutoPairsMultilineClose*
|g:AutoPairsMultilineClose| int
Default: 1
When you press the key for the closing pair (e.g. `)`) it jumps past it.
If set to 1, then it'll jump to the next line, if there is only 'whitespace'.
If set to 0, then it'll only jump to a closing pair on the same line.
==============================================================================
6. Troubleshooting *autopairs-troubleshooting*
This plugin remaps 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 conflicts 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.
--- Swedish Character Conflict ---
Because AutoPairs uses Meta(Alt) key as a shortcut, it conflicts with some
Swedish character such as å. To fix the issue, you need remap or disable the
related shortcut.

View File

@ -1,10 +1,11 @@
" Insert or delete brackets, parens, quotes in pairs.
" Maintainer: JiangMiao <jiangfriend@gmail.com>
" Contributor: camthompson
" Last Change: 2013-02-16
" Version: 1.3.0
" Last Change: 2019-01-15
" Version: 1.3.4
" Homepage: http://www.vim.org/scripts/script.php?script_id=3599
" Repository: https://github.com/jiangmiao/auto-pairs
" License: MIT
if exists('g:AutoPairsLoaded') || &cp
finish
@ -23,6 +24,11 @@ if !exists('g:AutoPairsMapBS')
let g:AutoPairsMapBS = 1
end
" Map <C-h> as the same BS
if !exists('g:AutoPairsMapCh')
let g:AutoPairsMapCh = 1
end
if !exists('g:AutoPairsMapCR')
let g:AutoPairsMapCR = 1
end
@ -43,6 +49,10 @@ if !exists('g:AutoPairsShortcutFastWrap')
let g:AutoPairsShortcutFastWrap = '<M-e>'
end
if !exists('g:AutoPairsMoveCharacter')
let g:AutoPairsMoveCharacter = "()[]{}\"'"
end
if !exists('g:AutoPairsShortcutJump')
let g:AutoPairsShortcutJump = '<M-n>'
endif
@ -53,11 +63,33 @@ if !exists('g:AutoPairsFlyMode')
let g:AutoPairsFlyMode = 0
endif
" When skipping the closed pair, look at the current and
" next line as well.
if !exists('g:AutoPairsMultilineClose')
let g:AutoPairsMultilineClose = 1
endif
" Work with Fly Mode, insert pair where jumped
if !exists('g:AutoPairsShortcutBackInsert')
let g:AutoPairsShortcutBackInsert = '<M-b>'
endif
if !exists('g:AutoPairsSmartQuotes')
let g:AutoPairsSmartQuotes = 1
endif
" 7.4.849 support <C-G>U to avoid breaking '.'
" Issue talk: https://github.com/jiangmiao/auto-pairs/issues/3
" Vim note: https://github.com/vim/vim/releases/tag/v7.4.849
if v:version > 704 || v:version == 704 && has("patch849")
let s:Go = "\<C-G>U"
else
let s:Go = ""
endif
let s:Left = s:Go."\<LEFT>"
let s:Right = s:Go."\<RIGHT>"
" Will auto generated {']' => '[', ..., '}' => '{'}in initialize.
let g:AutoPairsClosedPairs = {}
@ -70,10 +102,12 @@ function! AutoPairsInsert(key)
let line = getline('.')
let pos = col('.') - 1
let next_chars = split(strpart(line, pos), '\zs')
let before = strpart(line, 0, pos)
let after = strpart(line, pos)
let next_chars = split(after, '\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_chars = split(before, '\zs')
let prev_char = get(prev_chars, -1, '')
let eol = 0
@ -92,20 +126,24 @@ function! AutoPairsInsert(key)
" Skip the character if current character is the same as input
if current_char == a:key
return "\<Right>"
return s:Right
end
if !g:AutoPairsFlyMode
" Skip the character if next character is space
if current_char == ' ' && next_char == a:key
return "\<Right>\<Right>"
return s:Right.s: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 g:AutoPairsMultilineClose
let next_lineno = line('.')+1
let next_line = getline(nextnonblank(next_lineno))
let next_char = matchstr(next_line, '\s*\zs.')
else
let next_char = matchstr(line, '\s*\zs.')
end
if next_char == a:key
return "\<ESC>e^a"
endif
@ -114,12 +152,17 @@ function! AutoPairsInsert(key)
" Fly Mode, and the key is closed-pairs, search closed-pair and jump
if g:AutoPairsFlyMode && has_key(b:AutoPairsClosedPairs, a:key)
let n = stridx(after, a:key)
if n != -1
return repeat(s:Right, n+1)
end
if search(a:key, 'W')
" force break the '.' when jump to different line
return "\<Right>"
endif
endif
" Input directly if the key is not an open key
" Insert directly if the key is not an open key
return a:key
end
@ -127,7 +170,7 @@ function! AutoPairsInsert(key)
let close = b:AutoPairs[open]
if current_char == close && open == close
return "\<Right>"
return s:Right
end
" Ignore auto close ' if follows a word
@ -142,11 +185,42 @@ function! AutoPairsInsert(key)
let pprev_char = line[col('.')-3]
if pprev_char == open && prev_char == open
" Double pair found
return repeat(a:key, 4) . repeat("\<LEFT>", 3)
return repeat(a:key, 4) . repeat(s:Left, 3)
end
end
return open.close."\<Left>"
let quotes_num = 0
" Ignore comment line for vim file
if &filetype == 'vim' && a:key == '"'
if before =~ '^\s*$'
return a:key
end
if before =~ '^\s*"'
let quotes_num = -1
end
end
" Keep quote number is odd.
" Because quotes should be matched in the same line in most of situation
if g:AutoPairsSmartQuotes && open == close
" Remove \\ \" \'
let cleaned_line = substitute(line, '\v(\\.)', '', 'g')
let n = quotes_num
let pos = 0
while 1
let pos = stridx(cleaned_line, open, pos)
if pos == -1
break
end
let n = n + 1
let pos = pos + 1
endwhile
if n % 2 == 1
return a:key
endif
endif
return open.close.s:Left
endfunction
function! AutoPairsDelete()
@ -191,7 +265,7 @@ function! AutoPairsDelete()
end
if has_key(b:AutoPairs, prev_char)
if has_key(b:AutoPairs, prev_char)
let close = b:AutoPairs[prev_char]
if match(line,'^\s*'.close, col('.')-1) != -1
" Delete (|___)
@ -201,6 +275,11 @@ function! AutoPairsDelete()
" Delete (|__\n___)
let nline = getline(line('.')+1)
if nline =~ '^\s*'.close
if &filetype == 'vim' && prev_char == '"'
" Keep next line's comment
return "\<BS>"
end
let space = matchstr(nline, '^\s*')
return "\<BS>\<DEL>". repeat("\<DEL>", len(space)+1)
end
@ -246,7 +325,7 @@ function! AutoPairsFastWrap()
let next_char = line[col('.')]
let open_pair_pattern = '\v[({\[''"]'
let at_end = col('.') >= col('$') - 1
normal x
normal! x
" Skip blank
if next_char =~ '\v\s' || at_end
call search('\v\S', 'W')
@ -265,17 +344,23 @@ function! AutoPairsFastWrap()
else
call search(s:FormatChunk(followed_open_pair, followed_close_pair), 'We')
end
return "\<RIGHT>".inputed_close_pair."\<LEFT>"
return s:Right.inputed_close_pair.s:Left
else
normal e
return "\<RIGHT>".current_char."\<LEFT>"
normal! he
return s:Right.current_char.s:Left
end
endfunction
function! AutoPairsMap(key)
let escaped_key = substitute(a:key, "'", "''", 'g')
" | is special key which separate map command from text
let key = a:key
if key == '|'
let key = '<BAR>'
end
let escaped_key = substitute(key, "'", "''", 'g')
" use expr will cause search() doesn't work
execute 'inoremap <buffer> <silent> '.a:key." <C-R>=AutoPairsInsert('".escaped_key."')<CR>"
execute 'inoremap <buffer> <silent> '.key." <C-R>=AutoPairsInsert('".escaped_key."')<CR>"
endfunction
function! AutoPairsToggle()
@ -289,6 +374,12 @@ function! AutoPairsToggle()
return ''
endfunction
function! AutoPairsMoveCharacter(key)
let c = getline(".")[col(".")-1]
let escaped_key = substitute(a:key, "'", "''", 'g')
return "\<DEL>\<ESC>:call search("."'".escaped_key."'".")\<CR>a".c."\<LEFT>"
endfunction
function! AutoPairsReturn()
if b:autopairs_enabled == 0
return ''
@ -300,25 +391,23 @@ function! AutoPairsReturn()
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"
" Recenter before adding new line to avoid replacing line content
let cmd = "zz"
end
" If equalprg has been set, then avoid call =
" https://github.com/jiangmiao/auto-pairs/issues/24
if &equalprg != ''
return "\<ESC>O".cmd
return "\<ESC>".cmd."O"
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
return "\<ESC>".cmd."k==o"
else
return "\<ESC>=ko".cmd
return "\<ESC>".cmd."=ko"
endif
end
return ''
@ -330,7 +419,7 @@ function! AutoPairsSpace()
let cmd = ''
let cur_char =line[col('.')-1]
if has_key(g:AutoPairsParens, prev_char) && g:AutoPairsParens[prev_char] == cur_char
let cmd = "\<SPACE>\<LEFT>"
let cmd = "\<SPACE>".s:Left
endif
return "\<SPACE>".cmd
endfunction
@ -347,13 +436,19 @@ endfunction
function! AutoPairsInit()
let b:autopairs_loaded = 1
let b:autopairs_enabled = 1
if !exists('b:autopairs_enabled')
let b:autopairs_enabled = 1
end
let b:AutoPairsClosedPairs = {}
if !exists('b:AutoPairs')
let b:AutoPairs = g:AutoPairs
end
if !exists('b:AutoPairsMoveCharacter')
let b:AutoPairsMoveCharacter = g:AutoPairsMoveCharacter
end
" buffer level map pairs keys
for [open, close] in items(b:AutoPairs)
call AutoPairsMap(open)
@ -363,14 +458,28 @@ function! AutoPairsInit()
let b:AutoPairsClosedPairs[close] = open
endfor
for key in split(b:AutoPairsMoveCharacter, '\s*')
let escaped_key = substitute(key, "'", "''", 'g')
execute 'inoremap <silent> <buffer> <M-'.key."> <C-R>=AutoPairsMoveCharacter('".escaped_key."')<CR>"
endfor
" Still use <buffer> level mapping for <BS> <SPACE>
if g:AutoPairsMapBS
" 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:AutoPairsMapCh
execute 'inoremap <buffer> <silent> <C-h> <C-R>=AutoPairsDelete()<CR>'
endif
if g:AutoPairsMapSpace
execute 'inoremap <buffer> <silent> <SPACE> <C-R>=AutoPairsSpace()<CR>'
" Try to respect abbreviations on a <SPACE>
let do_abbrev = ""
if v:version == 703 && has("patch489") || v:version > 703
let do_abbrev = "<C-]>"
endif
execute 'inoremap <buffer> <silent> <SPACE> '.do_abbrev.'<C-R>=AutoPairsSpace()<CR>'
end
if g:AutoPairsShortcutFastWrap != ''
@ -392,13 +501,25 @@ function! AutoPairsInit()
execute 'noremap <buffer> <silent> ' . g:AutoPairsShortcutJump. ' :call AutoPairsJump()<CR>'
end
if &keymap != ''
let l:imsearch = &imsearch
let l:iminsert = &iminsert
let l:imdisable = &imdisable
execute 'setlocal keymap=' . &keymap
execute 'setlocal imsearch=' . l:imsearch
execute 'setlocal iminsert=' . l:iminsert
if l:imdisable
execute 'setlocal imdisable'
else
execute 'setlocal noimdisable'
end
end
endfunction
function! s:ExpandMap(map)
let map = a:map
if map =~ '<Plug>'
let map = substitute(map, '\(<Plug>\w\+\)', '\=maparg(submatch(1), "i")', 'g')
endif
let map = substitute(map, '\(<Plug>\w\+\)', '\=maparg(submatch(1), "i")', 'g')
return map
endfunction
@ -415,32 +536,52 @@ function! AutoPairsTryInit()
" 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.
" 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>'
if v:version == 703 && has('patch32') || v:version > 703
" VIM 7.3 supports advancer maparg which could get <expr> info
" then auto-pairs could remap <CR> in any case.
let info = maparg('<CR>', 'i', 0, 1)
if empty(info)
let old_cr = '<CR>'
let is_expr = 0
else
let old_cr = info['rhs']
let old_cr = s:ExpandMap(old_cr)
let old_cr = substitute(old_cr, '<SID>', '<SNR>' . info['sid'] . '_', 'g')
let is_expr = info['expr']
let wrapper_name = '<SID>AutoPairsOldCRWrapper73'
endif
else
let old_cr = s:ExpandMap(old_cr)
endif
" VIM version less than 7.3
" the mapping's <expr> info is lost, so guess it is expr or not, it's
" not accurate.
let old_cr = maparg('<CR>', 'i')
if old_cr == ''
let old_cr = '<CR>'
let is_expr = 0
else
let old_cr = s:ExpandMap(old_cr)
" old_cr contain (, I guess the old cr is in expr mode
let is_expr = old_cr =~ '\V(' && toupper(old_cr) !~ '\V<C-R>'
" 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
" The old_cr start with " it must be in expr mode
let is_expr = is_expr || old_cr =~ '\v^"'
let wrapper_name = '<SID>AutoPairsOldCRWrapper'
end
end
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
if is_expr
" remap <expr> to `name` to avoid mix expr and non-expr mode
execute 'inoremap <buffer> <expr> <script> '. wrapper_name . ' ' . old_cr
let old_cr = wrapper_name
end
" Always silent mapping
execute 'inoremap <script> <buffer> <silent> <CR> '.old_cr.'<SID>AutoPairsReturn'
end
endif