mirror of
https://github.com/jiangmiao/auto-pairs.git
synced 2025-07-13 21:05:31 +08:00
Compare commits
59 Commits
Author | SHA1 | Date | |
---|---|---|---|
4ec359716a | |||
92e5ece6a3 | |||
cf921dadce | |||
9086ce897a | |||
3c290d9308 | |||
f0019fc642 | |||
c00cc24c8e | |||
6afc850e24 | |||
20ec5b043f | |||
3bd07a4eeb | |||
ff9160e6d8 | |||
23f1c89508 | |||
e915d857fe | |||
69bfaf0fdd | |||
8451816810 | |||
edf7ab9a9e | |||
b0c9fce101 | |||
ea8ee08f44 | |||
2d4071038a | |||
33ecfd759d | |||
1b3a1efb07 | |||
63b0c1b249 | |||
5979999a17 | |||
15256d0e94 | |||
65a7237b2b | |||
0c30ba85f0 | |||
7f548cbb18 | |||
b647ef54cd | |||
26289c6430 | |||
7390859102 | |||
9828061855 | |||
c2f60ca2e5 | |||
062a5de551 | |||
0e27b0a9b1 | |||
8e07c8de31 | |||
8f4598b883 | |||
4390981ccf | |||
1484f6501d | |||
275b47353f | |||
6a705983d1 | |||
9617a648c5 | |||
a5f9ac7f43 | |||
37076cb15f | |||
b7ae7d0ba8 | |||
5d713be821 | |||
24ffa783f0 | |||
0a79f50acb | |||
85a9a7daac | |||
d23864fdd6 | |||
4f1643fbc9 | |||
64a10a60eb | |||
cc03f6e915 | |||
b3729e8779 | |||
f13a60913f | |||
a86ea24a0f | |||
e5f17cf9e2 | |||
fc5b845f6b | |||
2982312bae | |||
f6a18ca775 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
doc/tags
|
99
README.md
99
README.md
@ -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
356
doc/AutoPairs.txt
Normal 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.
|
@ -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
|
||||
|
Reference in New Issue
Block a user