From 83e22c7e3b936e423855d9d5635aa1c3db8fa4e2 Mon Sep 17 00:00:00 2001 From: xluffy Date: Thu, 16 Jun 2016 08:29:07 +0700 Subject: [PATCH 01/45] Update install auto-pair - Manual install with copy plugin file - Install with Pathogen - Install with Vundle plugin manager --- README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 72bde89..4625589 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,13 @@ Insert or delete brackets, parens, quotes in pair. 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``` +* Manual + * Copy `plugin/auto-pairs.vim` to `~/.vim/plugin` +* [Pathogen](https://github.com/tpope/vim-pathogen) + * `git clone git://github.com/jiangmiao/auto-pairs.git ~/.vim/bundle/auto-pairs` +* [Vundle](https://github.com/VundleVim/Vundle.vim) + * `Plugin 'jiangmiao/auto-pairs'` Features -------- From 33ecfd759d32bc8835a6d76a8e24e17ecf5c56f8 Mon Sep 17 00:00:00 2001 From: Dave Pagurek Date: Sat, 29 Oct 2016 11:46:39 -0700 Subject: [PATCH 02/45] Recenter before adding line, fix overwriting copy --- plugin/auto-pairs.vim | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/plugin/auto-pairs.vim b/plugin/auto-pairs.vim index 200b2dd..0a263cb 100644 --- a/plugin/auto-pairs.vim +++ b/plugin/auto-pairs.vim @@ -380,27 +380,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 \ instead of \cl will cause the placeholder deleted - " incorrect. because zz won't leave Normal mode. - " Use \ is a bit wierd. the character before cursor need to be deleted. - " Adding a space, recentering, and deleting it interferes with default - " whitespace-removing behavior when exiting insert mode. - let cmd = "\zzcc" + " 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 "\O".cmd + return "\".cmd."O" endif " conflict with javascript and coffee " javascript need indent new line " coffeescript forbid indent new line if &filetype == 'coffeescript' || &filetype == 'coffee' - return "\k==o".cmd + return "\".cmd."k==o" else - return "\=ko".cmd + return "\".cmd."=ko" endif end return '' From 2d4071038a7c1164956883b0788453df00047101 Mon Sep 17 00:00:00 2001 From: smiechowy Date: Mon, 14 Nov 2016 11:52:47 +0100 Subject: [PATCH 03/45] Fix checking for patch849 --- plugin/auto-pairs.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/auto-pairs.vim b/plugin/auto-pairs.vim index 200b2dd..18bea74 100644 --- a/plugin/auto-pairs.vim +++ b/plugin/auto-pairs.vim @@ -77,7 +77,7 @@ endif " 7.4.849 support 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 && has("patch849") +if v:version > 704 || v:version == 704 && has("patch849") let s:Go = "\U" else let s:Go = "" From edf7ab9a9e688f15c193d4d9bc53d0cdf5486134 Mon Sep 17 00:00:00 2001 From: Vladimir Bauer Date: Mon, 21 Nov 2016 11:29:39 +0500 Subject: [PATCH 04/45] remove trailing white spaces --- plugin/auto-pairs.vim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugin/auto-pairs.vim b/plugin/auto-pairs.vim index 16b0987..b4b070b 100644 --- a/plugin/auto-pairs.vim +++ b/plugin/auto-pairs.vim @@ -261,7 +261,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 (|___) @@ -500,9 +500,9 @@ function! AutoPairsTryInit() " supertab doesn't support AutoPairsReturn " when use AutoPairsReturn will cause Duplicated " - " 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 From 69bfaf0fdd935d679a11aa00f581b7481487c509 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois-Xavier=20Bourlet?= Date: Mon, 23 Jan 2017 14:51:25 -0800 Subject: [PATCH 05/45] Use `normal!` Use `normal!` to avoid collision with user defined mappings. --- plugin/auto-pairs.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/auto-pairs.vim b/plugin/auto-pairs.vim index b4b070b..b4beb21 100644 --- a/plugin/auto-pairs.vim +++ b/plugin/auto-pairs.vim @@ -321,7 +321,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') @@ -342,7 +342,7 @@ function! AutoPairsFastWrap() end return s:Right.inputed_close_pair.s:Left else - normal he + normal! he return s:Right.current_char.s:Left end endfunction From 23f1c89508c2d4ed584afc12eaafc544cccf8f63 Mon Sep 17 00:00:00 2001 From: Ruslan Kiianchuk Date: Sat, 11 Mar 2017 13:01:38 -0800 Subject: [PATCH 06/45] Add Vim help file. --- doc/AudoPairs.txt | 356 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 356 insertions(+) create mode 100644 doc/AudoPairs.txt diff --git a/doc/AudoPairs.txt b/doc/AudoPairs.txt new file mode 100644 index 0000000..49ff549 --- /dev/null +++ b/doc/AudoPairs.txt @@ -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[] + output: foo + +Insert new indented line after Return: > + + input: {|} (press at |) + output: { + | + } + +Insert spaces before closing characters, only for [], (), {}: > + + input: {|} (press at |) + output: { | } + + input: {|} (press foo} at |) + output: { foo }| + + input: '|' (press 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 ( at |) + output: ('hello') + + Wrap string, only support c style string. + input: |'h\\el\'lo' (press ( at |) + output ('h\\ello\'') + + input: |[foo, bar()] (press ( 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 at |) + output: | + + input: {{|}} (press at |) + output: | + + input: [[[[[[|]]]]]] (press 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 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: ) 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 = '' + +============================================================================== +4. Shortcuts *autopairs-shortcuts* + +System Shortcuts: + : Insert new indented line after return if cursor in blank brackets + or quotes. + : Delete brackets in pair + : Toggle Autopairs (|g:AutoPairsShortcutToggle|) + : Fast Wrap (|g:AutoPairsShortcutFastWrap|) + : Jump to next closed pair (|g:AutoPairsShortcutJump|) + : BackInsert (|g:AutoPairsShortcutBackInsert|) + + + To rebind keys , or or in case of conflicts conflicts with + another keys: + + let g:AutoPairsShortcutToggle = '' + + 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: + +The shortcut to toggle autopairs. + + + + *g:AutoPairsShortcutFastWrap* +|g:AutoPairsShortcutFastWrap| string + +Default: + +Fast wrap the word. All pairs will be considered as a block (including <>). + + (|)'hello' after fast wrap at |, the word will be ('hello') + (|) after fast wrap at |, the word will be () + + + + *g:AutoPairsShortcutJump* +|g:AutoPairsShortcutJump| string + +Default: + +Jump to the next closed pair. + + + *g:AutoPairsShortcutBackInsert* +|g:AutoPairsShortcutBackInsert| string + +Default: + +Work with |autopairs-flymode|, insert the key at the Fly Mode jumped position. + + + + *g:AutoPairsMapBS* +|g:AutoPairsMapBS| int + +Default: 1 + +Map to delete brackets and quotes in pair, executes: + + inoremap =AutoPairsDelete() + + + *g:AutoPairsMapCh* +|g:AutoPairsMapCh| int + +Default: 1 + +Map to delete brackets and quotes in pair. + + + *g:AutoPairsMapCR* +|g:AutoPairsMapCR| int + +Default: 1 + +Map to insert a new indented line if cursor in (|), {|} [|], '|', "|". +Executes: + + inoremap =AutoPairsReturn() + + + *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 to insert a space after the opening character and before the +closing one. + +Executes: + + inoremap =AutoPairsSpace() + + + *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 `([{'"}]) ` + +If auto pairs cannot work, use |:imap| to check if the map is corrected. + +The correct map should be: > + + =AutoPairsInsert("\(") + +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 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. From 3bd07a4eebe6ec95d985e91420f2680b9bfab508 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Tomak?= Date: Tue, 21 Mar 2017 20:04:05 +0100 Subject: [PATCH 07/45] Added .gitignore with doc/tags entry --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..926ccaa --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +doc/tags From 6afc850e2429e6832a1b093e32a31e0b5eff477d Mon Sep 17 00:00:00 2001 From: Miao Jiang Date: Sat, 17 Jun 2017 01:23:20 +0800 Subject: [PATCH 08/45] New feature: Move character under the cursor to the pair. --- README.md | 15 +++++++++++++++ plugin/auto-pairs.vim | 28 +++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 72bde89..7cdcbf9 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,14 @@ Features input: |[foo, bar()] (press ( at |) output: ([foo, bar()]) +* Quick move char to closed pair + + input: (|){["foo"]} (press at |) + output: ({["foo"]}|) + + input: |[foo, bar()] (press ( at |) + output: ([foo, bar()]|) + * Quick jump to closed pair. input: @@ -249,6 +257,13 @@ Options Work with FlyMode, insert the key at the Fly Mode jumped postion +* g:AutoPairsMoveCharacter + + Default: "()[]{}\"'" + + Map to + move character under the cursor to the pair. + Buffer Level Pairs Setting -------------------------- diff --git a/plugin/auto-pairs.vim b/plugin/auto-pairs.vim index b4beb21..b2a9af7 100644 --- a/plugin/auto-pairs.vim +++ b/plugin/auto-pairs.vim @@ -1,8 +1,8 @@ " Insert or delete brackets, parens, quotes in pairs. " Maintainer: JiangMiao " Contributor: camthompson -" Last Change: 2013-07-13 -" Version: 1.3.2 +" Last Change: 2017-06-17 +" Version: 1.3.3 " Homepage: http://www.vim.org/scripts/script.php?script_id=3599 " Repository: https://github.com/jiangmiao/auto-pairs " License: MIT @@ -49,6 +49,10 @@ if !exists('g:AutoPairsShortcutFastWrap') let g:AutoPairsShortcutFastWrap = '' end +if !exists('g:AutoPairsMoveCharacter') + let g:AutoPairsMoveCharacter = "()[]{}\"'" +end + if !exists('g:AutoPairsShortcutJump') let g:AutoPairsShortcutJump = '' endif @@ -356,6 +360,7 @@ function! AutoPairsMap(key) let escaped_key = substitute(key, "'", "''", 'g') " use expr will cause search() doesn't work execute 'inoremap '.key." =AutoPairsInsert('".escaped_key."')" + endfunction function! AutoPairsToggle() @@ -369,6 +374,12 @@ function! AutoPairsToggle() return '' endfunction +function! AutoPairsMoveCharacter(key) + let c = getline(".")[col(".")-1] + let escaped_key = substitute(a:key, "'", "''", 'g') + return "\\:call search("."'".escaped_key."'".")\a".c."\" +endfunction + function! AutoPairsReturn() if b:autopairs_enabled == 0 return '' @@ -425,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) @@ -441,6 +458,11 @@ function! AutoPairsInit() let b:AutoPairsClosedPairs[close] = open endfor + for key in split(b:AutoPairsMoveCharacter, '\s*') + let escaped_key = substitute(key, "'", "''", 'g') + execute 'inoremap =AutoPairsMoveCharacter('".escaped_key."')" + endfor + " Still use level mapping for if g:AutoPairsMapBS " Use instead of for issue #14 sometimes press BS output strange words From c00cc24c8e2f6aa8264a96d966729ad9676ae7fc Mon Sep 17 00:00:00 2001 From: d10n Date: Sun, 2 Jul 2017 20:23:39 -0400 Subject: [PATCH 09/45] Fix filename --- doc/{AudoPairs.txt => AutoPairs.txt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename doc/{AudoPairs.txt => AutoPairs.txt} (100%) diff --git a/doc/AudoPairs.txt b/doc/AutoPairs.txt similarity index 100% rename from doc/AudoPairs.txt rename to doc/AutoPairs.txt From 404a121b04d0141f882564f2060c02f578ac00ba Mon Sep 17 00:00:00 2001 From: Hana Shiro Date: Tue, 21 Feb 2017 23:53:29 +0800 Subject: [PATCH 10/45] Close empty pairs smartly --- plugin/auto-pairs.vim | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/plugin/auto-pairs.vim b/plugin/auto-pairs.vim index b2a9af7..a1693bb 100644 --- a/plugin/auto-pairs.vim +++ b/plugin/auto-pairs.vim @@ -129,12 +129,23 @@ function! AutoPairsInsert(key) return s:Right end - if !g:AutoPairsFlyMode - " Skip the character if next character is space - if current_char == ' ' && next_char == a:key - return s:Right.s:Right - end + " Skip the character if closed pair is next character + if current_char == ' ' && next_char == a:key + " Remove the space we added if the pair is empty + if has_key(b:AutoPairsClosedPairs, a:key) + let end_of_prevchar_index = matchend(before, '\S\ze\s*$') + if end_of_prevchar_index > -1 + let end_of_prevchar = get(prev_chars, end_of_prevchar_index-1, '') + if end_of_prevchar == b:AutoPairsClosedPairs[a:key] + return "\".s:Right + endif + endif + endif + return s:Right.s:Right + endif + + if !g:AutoPairsFlyMode " Skip the character if closed pair is next character if current_char == '' if g:AutoPairsMultilineClose From 3c290d9308c1b651c588865c3741a12654ae640e Mon Sep 17 00:00:00 2001 From: Ruslan Kiyanchuk Date: Tue, 18 Sep 2018 17:42:41 -0700 Subject: [PATCH 11/45] Spell fix Remove duplicated word. --- doc/AutoPairs.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/AutoPairs.txt b/doc/AutoPairs.txt index 49ff549..afe589e 100644 --- a/doc/AutoPairs.txt +++ b/doc/AutoPairs.txt @@ -178,7 +178,7 @@ System Shortcuts: : BackInsert (|g:AutoPairsShortcutBackInsert|) - To rebind keys , or or in case of conflicts conflicts with + To rebind keys , or or in case of conflicts with another keys: let g:AutoPairsShortcutToggle = '' From cf921dadceae27d05ae05543342af23e3b6955e5 Mon Sep 17 00:00:00 2001 From: "Mikhail f. Shiryaev" Date: Sat, 12 Jan 2019 15:58:06 +0100 Subject: [PATCH 12/45] Fix breaking of keymap behavior --- plugin/auto-pairs.vim | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/plugin/auto-pairs.vim b/plugin/auto-pairs.vim index b2a9af7..b062ba8 100644 --- a/plugin/auto-pairs.vim +++ b/plugin/auto-pairs.vim @@ -501,6 +501,20 @@ function! AutoPairsInit() execute 'noremap ' . g:AutoPairsShortcutJump. ' :call AutoPairsJump()' 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) From 4ec359716a48fe0953f38208e45063bdc5e551d6 Mon Sep 17 00:00:00 2001 From: Miao Jiang Date: Tue, 15 Jan 2019 04:18:06 +0800 Subject: [PATCH 13/45] Bump version 1.3.4 --- plugin/auto-pairs.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/auto-pairs.vim b/plugin/auto-pairs.vim index b062ba8..93a4ed3 100644 --- a/plugin/auto-pairs.vim +++ b/plugin/auto-pairs.vim @@ -1,8 +1,8 @@ " Insert or delete brackets, parens, quotes in pairs. " Maintainer: JiangMiao " Contributor: camthompson -" Last Change: 2017-06-17 -" Version: 1.3.3 +" 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 From 738d1f18a8cc6b3f8e0597304e0b2bce7c107dce Mon Sep 17 00:00:00 2001 From: Miao Jiang Date: Tue, 15 Jan 2019 04:21:48 +0800 Subject: [PATCH 14/45] Support multibytes pairs --- plugin/auto-pairs.vim | 514 +++++++++++++++++------------------------- 1 file changed, 211 insertions(+), 303 deletions(-) diff --git a/plugin/auto-pairs.vim b/plugin/auto-pairs.vim index 93a4ed3..bfa072c 100644 --- a/plugin/auto-pairs.vim +++ b/plugin/auto-pairs.vim @@ -16,10 +16,6 @@ if !exists('g:AutoPairs') let g:AutoPairs = {'(':')', '[':']', '{':'}',"'":"'",'"':'"', '`':'`'} end -if !exists('g:AutoPairsParens') - let g:AutoPairsParens = {'(':')', '[':']', '{':'}'} -end - if !exists('g:AutoPairsMapBS') let g:AutoPairsMapBS = 1 end @@ -95,263 +91,226 @@ let s:Right = s:Go."\" let g:AutoPairsClosedPairs = {} -function! AutoPairsInsert(key) - if !b:autopairs_enabled - return a:key - end +" unicode len +func! s:ulen(s) + return len(split(a:s, '\zs')) +endf +func! s:left(s) + return repeat(s:Left, s:ulen(a:s)) +endf + +func! s:right(s) + return repeat(s:Right, s:ulen(a:s)) +endf + +func! s:delete(s) + return repeat("\", s:ulen(a:s)) +endf + +func! s:backspace(s) + return repeat("\", s:ulen(a:s)) +endf + +func! s:getline() let line = getline('.') let pos = col('.') - 1 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(before, '\zs') - let prev_char = get(prev_chars, -1, '') - - let eol = 0 - if col('$') - col('.') <= 1 - let eol = 1 - end - - " Ignore auto close if prev character is \ - if prev_char == '\' - return a:key - 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('.')] - - " Skip the character if current character is the same as input - if current_char == a:key - return s:Right - end - - if !g:AutoPairsFlyMode - " Skip the character if next character is space - if current_char == ' ' && next_char == a:key - return s:Right.s:Right - end - - " Skip the character if closed pair is next character - if current_char == '' - 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 "\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) - 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 "\" - endif - endif - - " Insert directly if the key is not an open key - return a:key - end - - let open = a:key - let close = b:AutoPairs[open] - - if current_char == close && open == close - return s: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(s:Left, 3) - end - end - - 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 + if g:AutoPairsMultilineClose + let n = line('$') + let i = line('.')+1 + while i <= n + let line = getline(i) + let after = after.' '.line + if !(line =~ '\v^\s*$') break end - let n = n + 1 - let pos = pos + 1 + let i = i+1 endwhile - if n % 2 == 1 - return a:key + end + return [before, after] +endf + +" add or delete pairs base on g:AutoPairs +" AutoPairsDefine(addPairs:dict[, removeOpenPairList:list]) +" +" eg: +" au FileType html let b:AutoPairs = AutoPairsDefine({''}, ['{']) +" add pair and remove '{' for html file +func! AutoPairsDefine(pairs, ...) + let r = copy(g:AutoPairs) + for [open, close] in items(a:pairs) + let r[open] = close + endfor + if a:0 > 1 + for open in a:1 + unlet r[open] + endfor + end + return r +endf + +func! AutoPairsInsert(key) + if !b:autopairs_enabled + return a:key + end + + let b:autopairs_saved_pair = [a:key, getpos('.')] + + let [before, after] = s:getline() + + " Ignore auto close if prev character is \ + if before[-1:-1] == '\' + return a:key + end + + " check close pairs + for [open, close] in b:AutoPairsList + if close[0] == a:key + let m = matchlist(after, '\v^\s*(\V'.close.'\v)') + if len(m) > 0 + " skip close pair + call search(m[1], 'We') + return "\" + end + end + endfor + + " check open pairs + let text=before.a:key + for [open, close] in b:AutoPairsList + let m = matchstr(text, '\V'.open.'\v$') + if m != '' + " process the open pair + + " remove inserted pair + " if the pairs include < > and + " when + " eg: if the pairs include < > and " when " when , <% %>, """ """ + See multibyte pairs section for details * Fly Mode @@ -298,6 +284,88 @@ TroubleShooting 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. +Multibyte Pairs +--------------- + + The default pairs is {'(':')', '[':']', '{':'}',"'":"'",'"':'"', '`':'`'} + You could also define multibyte pairs such as , <% %> and so on + + Here are some examples + +* General usage + + au FileType php let b:AutoPairs = AutoPairsDefine({'', ''}) + + the first key of closed pair ? will be mapped + + pairs: '', '' + input: + + input: + + input: he (press at|) + output: he| + + input: (press ? at|) + output: | + + pair: '[[':']]' + input: [[|]] (press
) + output: | ([[ and ]] will be deleted the [['s priority is higher than [ for it's longer) + +* Modifier + + The text after // in close pair is modifiers + + n - do not map the first charactor of closed pair + + for 'begin' 'end' pair, e is a charactor, if map e to jump will be annoy, so use modifier 'n' to skip key map + + au FileType ruby let b:AutoPairs = AutoPairsDefine({'begin': 'end//n]'}) + + + input: begin + output: begin|end + + input: begin|end (press
on |) + output: | + + input: begin|end (press e on |) + output: begineend (will not jump for e is not mapped) + +* Advanced usage + + au FileType rust let b:AutoPairs = AutoPairsDefine({'\w\zs<': '>'}) + + if press < after a word will generate the pair + + when use regexp MUST use \zs to prevent catching + if use '\w<' without \zs, for text hello<|> press on | will output 'hell', the 'o' has been deleted + + pair: '\w\zs<': '>' + input: h < + output: h < + + input: h< + output: h<|> + + input: h<|> press
+ output: h| + + pair: '\ws<': '>' (WRONG pair which missed \zs) + input: h<|> press
+ output: | (charactor 'h' is deleted) + + + the 'begin' 'end' pair write in + + au FileType ruby let b:AutoPairs = AutoPairsDefine({'\v(^|[^\w])\zsbegin': 'end//n'}) + + will be better, only auto pair when at start of line or follow non-word text + + Known Issues ----------------------- Breaks '.' - [issue #3](https://github.com/jiangmiao/auto-pairs/issues/3) From 4d5060c80c1e78e6670e2092c44cee4dcb04cb04 Mon Sep 17 00:00:00 2001 From: Miao Jiang Date: Tue, 15 Jan 2019 17:17:06 +0800 Subject: [PATCH 18/45] Add wild closed pair --- README.md | 22 ++++++++++++++-------- plugin/auto-pairs.vim | 6 +++--- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index fda583f..667e9ed 100644 --- a/README.md +++ b/README.md @@ -57,13 +57,6 @@ Features * Fast Wrap - input: |'hello' (press ( at |) - output: ('hello') - - wrap string, only support c style string - input: |'h\\el\'lo' (press ( at |) - output ('h\\ello\'') - input: |[foo, bar()] (press ( at |) output: ([foo, bar()]) @@ -250,6 +243,13 @@ Options Map to move character under the cursor to the pair. +* g:AutoPairsWildClosedPair + + Default: ']' + + Jump over following closed pair + for pair {'begin': 'end//n]'}, e is not mapped, use wild closed pair ] to jump over 'end' + Buffer Level Pairs Setting -------------------------- @@ -290,7 +290,13 @@ Multibyte Pairs The default pairs is {'(':')', '[':']', '{':'}',"'":"'",'"':'"', '`':'`'} You could also define multibyte pairs such as , <% %> and so on - Here are some examples +* Function AutoPairsDefine(addPairs:dict[, removeOpenPairList:list]) + + add or delete pairs base on g:AutoPairs + + eg: + au FileType html let b:AutoPairs = AutoPairsDefine({''}, ['{']) + add pair and remove '{' for html file * General usage diff --git a/plugin/auto-pairs.vim b/plugin/auto-pairs.vim index 2cb6340..ae79b32 100644 --- a/plugin/auto-pairs.vim +++ b/plugin/auto-pairs.vim @@ -2,7 +2,7 @@ " Maintainer: JiangMiao " Contributor: camthompson " Last Change: 2019-01-15 -" Version: 1.3.4 +" Version: 2.0.0 " Homepage: http://www.vim.org/scripts/script.php?script_id=3599 " Repository: https://github.com/jiangmiao/auto-pairs " License: MIT @@ -185,8 +185,8 @@ func! AutoPairsInsert(key) " check close pairs for [open, close] in b:AutoPairsList - if close[0] == a:key - let m = s:matchbegin(after, '\v\s*\V'.close) + if a:key == g:AutoPairsWildClosedPair || close[0] == a:key + let m = s:matchbegin(after, '\v\s*\zs\V'.close) if len(m) > 0 " skip close pair call search(m[1], 'We') From 177664ab1aa9b12e50134a4b004ddb5e694cd115 Mon Sep 17 00:00:00 2001 From: Miao Jiang Date: Tue, 15 Jan 2019 18:40:51 +0800 Subject: [PATCH 19/45] Update README --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 667e9ed..944201a 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ Insert or delete brackets, parens, quotes in pair. Installation ------------ + copy plugin/auto-pairs.vim to ~/.vim/plugin or if you are using `pathogen`: @@ -249,6 +250,7 @@ Options Jump over following closed pair for pair {'begin': 'end//n]'}, e is not mapped, use wild closed pair ] to jump over 'end' + use to back insert ] after jumping Buffer Level Pairs Setting -------------------------- @@ -259,6 +261,7 @@ eg: " When the filetype is FILETYPE then make AutoPairs only match for parenthesis au Filetype FILETYPE let b:AutoPairs = {"(": ")"} + au FileType php let b:AutoPairs = AutoPairsDefine({'', ''}) TroubleShooting --------------- @@ -298,6 +301,8 @@ Multibyte Pairs au FileType html let b:AutoPairs = AutoPairsDefine({''}, ['{']) add pair and remove '{' for html file + the pair implict start with \V, so if want to match start of line ^ should be write in \^ vim comment {'\^"': ''} + * General usage au FileType php let b:AutoPairs = AutoPairsDefine({'', ''}) From bec90a4076cd559e290b61abda8073800f06c720 Mon Sep 17 00:00:00 2001 From: Miao Jiang Date: Tue, 15 Jan 2019 18:52:47 +0800 Subject: [PATCH 20/45] Update README --- README.md | 92 +++++++++++++++++++++++++++---------------------------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index e1fe0bb..41b351b 100644 --- a/README.md +++ b/README.md @@ -296,86 +296,86 @@ Multibyte Pairs * Function AutoPairsDefine(addPairs:dict[, removeOpenPairList:list]) - add or delete pairs base on g:AutoPairs + add or delete pairs base on g:AutoPairs - eg: - au FileType html let b:AutoPairs = AutoPairsDefine({''}, ['{']) - add pair and remove '{' for html file + eg: + au FileType html let b:AutoPairs = AutoPairsDefine({''}, ['{']) + add pair and remove '{' for html file - the pair implict start with \V, so if want to match start of line ^ should be write in \^ vim comment {'\^"': ''} + the pair implict start with \V, so if want to match start of line ^ should be write in \^ vim comment {'\^"': ''} * General usage - au FileType php let b:AutoPairs = AutoPairsDefine({'', ''}) + au FileType php let b:AutoPairs = AutoPairsDefine({'', ''}) - the first key of closed pair ? will be mapped + the first key of closed pair ? will be mapped - pairs: '', '' - input: + pairs: '', '' + input: - input: + input: - input: he (press at|) - output: he| + input: he (press at|) + output: he| - input: (press ? at|) - output: | + input: (press ? at|) + output: | - pair: '[[':']]' - input: [[|]] (press
) - output: | ([[ and ]] will be deleted the [['s priority is higher than [ for it's longer) + pair: '[[':']]' + input: [[|]] (press
) + output: | ([[ and ]] will be deleted the [['s priority is higher than [ for it's longer) * Modifier - The text after // in close pair is modifiers + The text after // in close pair is modifiers - n - do not map the first charactor of closed pair + n - do not map the first charactor of closed pair - for 'begin' 'end' pair, e is a charactor, if map e to jump will be annoy, so use modifier 'n' to skip key map + for 'begin' 'end' pair, e is a charactor, if map e to jump will be annoy, so use modifier 'n' to skip key map - au FileType ruby let b:AutoPairs = AutoPairsDefine({'begin': 'end//n]'}) + au FileType ruby let b:AutoPairs = AutoPairsDefine({'begin': 'end//n]'}) - input: begin - output: begin|end + input: begin + output: begin|end - input: begin|end (press
on |) - output: | + input: begin|end (press
on |) + output: | - input: begin|end (press e on |) - output: begineend (will not jump for e is not mapped) + input: begin|end (press e on |) + output: begineend (will not jump for e is not mapped) * Advanced usage - au FileType rust let b:AutoPairs = AutoPairsDefine({'\w\zs<': '>'}) + au FileType rust let b:AutoPairs = AutoPairsDefine({'\w\zs<': '>'}) - if press < after a word will generate the pair + if press < after a word will generate the pair - when use regexp MUST use \zs to prevent catching - if use '\w<' without \zs, for text hello<|> press on | will output 'hell', the 'o' has been deleted + when use regexp MUST use \zs to prevent catching + if use '\w<' without \zs, for text hello<|> press on | will output 'hell', the 'o' has been deleted - pair: '\w\zs<': '>' - input: h < - output: h < + pair: '\w\zs<': '>' + input: h < + output: h < - input: h< - output: h<|> + input: h< + output: h<|> - input: h<|> press
- output: h| + input: h<|> press
+ output: h| - pair: '\ws<': '>' (WRONG pair which missed \zs) - input: h<|> press
- output: | (charactor 'h' is deleted) + pair: '\ws<': '>' (WRONG pair which missed \zs) + input: h<|> press
+ output: | (charactor 'h' is deleted) - the 'begin' 'end' pair write in + the 'begin' 'end' pair write in - au FileType ruby let b:AutoPairs = AutoPairsDefine({'\v(^|[^\w])\zsbegin': 'end//n'}) + au FileType ruby let b:AutoPairs = AutoPairsDefine({'\v(^|[^\w])\zsbegin': 'end//n'}) - will be better, only auto pair when at start of line or follow non-word text + will be better, only auto pair when at start of line or follow non-word text Known Issues From 3c7fde2643f1c611c15cb9c30e124d49dca1a4ef Mon Sep 17 00:00:00 2001 From: Miao Jiang Date: Tue, 15 Jan 2019 19:08:18 +0800 Subject: [PATCH 21/45] Update README --- README.md | 57 +++++++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 41b351b..3aa8cb8 100644 --- a/README.md +++ b/README.md @@ -84,11 +84,6 @@ Features }| -* Multibyte Pairs - - Support any multibyte pairs such as , <% %>, """ """ - See multibyte pairs section for details - * Fly Mode input: if(a[3) @@ -118,6 +113,11 @@ Features See Fly Mode section for details +* Multibyte Pairs + + Support any multibyte pairs such as , <% %>, """ """ + See multibyte pairs section for details + Fly Mode -------- Fly Mode will always force closed-pair jumping instead of inserting. only for ")", "}", "]" @@ -264,30 +264,6 @@ eg: au Filetype FILETYPE let b:AutoPairs = {"(": ")"} au FileType php let b:AutoPairs = AutoPairsDefine({'', ''}) -TroubleShooting ---------------- - The script will remap keys ([{'"}]) , - If auto pairs cannot work, use :imap ( to check if the map is corrected. - The correct map should be =AutoPairsInsert("\(") - 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 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. - Multibyte Pairs --------------- @@ -377,6 +353,29 @@ Multibyte Pairs will be better, only auto pair when at start of line or follow non-word text +TroubleShooting +--------------- + The script will remap keys ([{'"}]) , + If auto pairs cannot work, use :imap ( to check if the map is corrected. + The correct map should be =AutoPairsInsert("\(") + 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 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 ----------------------- From ea2fd8a2deb77a33dd8849c7fa7383b84317b684 Mon Sep 17 00:00:00 2001 From: Miao Jiang Date: Tue, 15 Jan 2019 19:10:52 +0800 Subject: [PATCH 22/45] Update README --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3aa8cb8..c80af60 100644 --- a/README.md +++ b/README.md @@ -300,7 +300,7 @@ Multibyte Pairs output: | pair: '[[':']]' - input: [[|]] (press
) + input: [[|]] (press ) output: | ([[ and ]] will be deleted the [['s priority is higher than [ for it's longer) * Modifier @@ -317,7 +317,7 @@ Multibyte Pairs input: begin output: begin|end - input: begin|end (press
on |) + input: begin|end (press on |) output: | input: begin|end (press e on |) @@ -339,11 +339,11 @@ Multibyte Pairs input: h< output: h<|> - input: h<|> press
+ input: h<|> press output: h| pair: '\ws<': '>' (WRONG pair which missed \zs) - input: h<|> press
+ input: h<|> press output: | (charactor 'h' is deleted) From 423ee192c727bc27e272a28fbb3f951a9548a765 Mon Sep 17 00:00:00 2001 From: Miao Jiang Date: Tue, 15 Jan 2019 19:38:21 +0800 Subject: [PATCH 23/45] Prevent jumping multi line if open pair equals close pair --- README.md | 2 +- plugin/auto-pairs.vim | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c80af60..862c9b9 100644 --- a/README.md +++ b/README.md @@ -342,7 +342,7 @@ Multibyte Pairs input: h<|> press output: h| - pair: '\ws<': '>' (WRONG pair which missed \zs) + pair: '\w<': '>' (WRONG pair which missed \zs) input: h<|> press output: | (charactor 'h' is deleted) diff --git a/plugin/auto-pairs.vim b/plugin/auto-pairs.vim index 6509c31..a79dd4e 100644 --- a/plugin/auto-pairs.vim +++ b/plugin/auto-pairs.vim @@ -114,12 +114,12 @@ func! s:backspace(s) return repeat("\", s:ulen(a:s)) endf -func! s:getline() +func! s:getline(...) let line = getline('.') let pos = col('.') - 1 let before = strpart(line, 0, pos) let after = strpart(line, pos) - if g:AutoPairsMultilineClose + if a:0 == 0 && g:AutoPairsMultilineClose let n = line('$') let i = line('.')+1 while i <= n @@ -186,6 +186,9 @@ func! AutoPairsInsert(key) " check close pairs for [open, close] in b:AutoPairsList if a:key == g:AutoPairsWildClosedPair || close[0] == a:key + if open == close + let [before, after] = s:getline(0) + end let m = s:matchbegin(after, '\v\s*\zs\V'.close) if len(m) > 0 " skip close pair From 7d721d261f18127ae6fb1220ab226d2593db2e7a Mon Sep 17 00:00:00 2001 From: Miao Jiang Date: Tue, 15 Jan 2019 21:33:19 +0800 Subject: [PATCH 24/45] Indent line when return --- plugin/auto-pairs.vim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugin/auto-pairs.vim b/plugin/auto-pairs.vim index a79dd4e..e791c42 100644 --- a/plugin/auto-pairs.vim +++ b/plugin/auto-pairs.vim @@ -336,7 +336,8 @@ func! AutoPairsReturn() let after = getline('.') let cmd = '' for [open, close] in b:AutoPairsList - if before =~ '\V'.open.'\v\s*$' && after =~ '^\s*\V'.close + " before =~ '\V'.open.'\v\s*$' && + if after =~ '^\s*\V'.close if g:AutoPairsCenterLine && winline() * 3 >= winheight(0) * 2 " Recenter before adding new line to avoid replacing line content let cmd = "zz" From 38d53d4df05ddec3a75fa801c4ba8600b8fcd4a9 Mon Sep 17 00:00:00 2001 From: Miao Jiang Date: Tue, 15 Jan 2019 22:26:37 +0800 Subject: [PATCH 25/45] Close empty pairs smartly --- plugin/auto-pairs.vim | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/plugin/auto-pairs.vim b/plugin/auto-pairs.vim index e791c42..d329bb5 100644 --- a/plugin/auto-pairs.vim +++ b/plugin/auto-pairs.vim @@ -114,11 +114,12 @@ func! s:backspace(s) return repeat("\", s:ulen(a:s)) endf -func! s:getline(...) +func! s:getline() let line = getline('.') let pos = col('.') - 1 let before = strpart(line, 0, pos) let after = strpart(line, pos) + let afterline = after if a:0 == 0 && g:AutoPairsMultilineClose let n = line('$') let i = line('.')+1 @@ -131,7 +132,7 @@ func! s:getline(...) let i = i+1 endwhile end - return [before, after] + return [before, after, afterline] endf " split text to two part @@ -176,7 +177,7 @@ func! AutoPairsInsert(key) let b:autopairs_saved_pair = [a:key, getpos('.')] - let [before, after] = s:getline() + let [before, after, afterline] = s:getline() " Ignore auto close if prev character is \ if before[-1:-1] == '\' @@ -186,16 +187,20 @@ func! AutoPairsInsert(key) " check close pairs for [open, close] in b:AutoPairsList if a:key == g:AutoPairsWildClosedPair || close[0] == a:key - if open == close - let [before, after] = s:getline(0) - end - let m = s:matchbegin(after, '\v\s*\zs\V'.close) - if len(m) > 0 - " skip close pair - let c = matchstr(after, '^\V'.close) - if c != "" - return s:right(c) + " the close pair is in the same line + let m = matchstr(afterline, '^\v\s*\V'.close) + if m != '' + if before =~ '\V'.open.'\v\s*$' && m[0] =~ '\v\s' + " remove the space we inserted if the text in pairs is blank + return "\".s:right(m[1:]) else + return s:right(m) + end + end + if open != close + let m = s:matchend(after, '^\v\s*\zs\V'.close) + if len(m) > 0 + " skip close pair greedy call search(m[1], 'We') return "\" end @@ -265,7 +270,7 @@ func! AutoPairsDelete() return "\" end - let [before, after] = s:getline() + let [before, after, ig] = s:getline() for [open, close] in b:AutoPairsList let b = matchstr(before, '\V'.open.'\v\s?$') let a = matchstr(after, '^\v\s*\V'.close) @@ -288,7 +293,7 @@ endf func! AutoPairsFastWrap() let c = @" normal! x - let [before, after] = s:getline() + let [before, after, ig] = s:getline() if after[0] =~ '\v[\{\[\(\<]' normal! % normal! p @@ -301,8 +306,12 @@ func! AutoPairsFastWrap() return "" end endfor - normal! e - normal! p + if after[1:1] =~ '\v[a-zA-Z0-9_]' + normal! e + normal! p + else + normal! p + end end let @" = c return "" @@ -367,7 +376,7 @@ func! AutoPairsSpace() return "\" end - let [before, after] = s:getline() + let [before, after, ig] = s:getline() for [open, close] in b:AutoPairsList if before =~ '\V'.open.'\v$' && after =~ '^\V'.close From 8d2f8382051202c7f54a9dfe0805928ad47c3751 Mon Sep 17 00:00:00 2001 From: Miao Jiang Date: Tue, 15 Jan 2019 22:56:58 +0800 Subject: [PATCH 26/45] Fix jump incorrect --- plugin/auto-pairs.vim | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/plugin/auto-pairs.vim b/plugin/auto-pairs.vim index d329bb5..ad2e8f5 100644 --- a/plugin/auto-pairs.vim +++ b/plugin/auto-pairs.vim @@ -198,10 +198,10 @@ func! AutoPairsInsert(key) end end if open != close - let m = s:matchend(after, '^\v\s*\zs\V'.close) - if len(m) > 0 + let m = matchstr(after, '^\v\s*\zs\V'.close) + if m != '' " skip close pair greedy - call search(m[1], 'We') + call search(m, 'We') return "\" end end @@ -285,6 +285,15 @@ func! AutoPairsDelete() return repeat("\", s:ulen(b)).repeat("\", s:ulen(a)) end endfor + + return "\" + " delete the pair foo[]| to foo + for [open, close] in b:AutoPairsList + let m = s:matchend(before, '\V'.open.'\v\s*'.'\V'.close.'\v$') + if len(m) > 0 + return s:backspace(m[2]) + end + endfor return "\" endf From 43db4c177641b0c51ec614b2350bf0a87e580591 Mon Sep 17 00:00:00 2001 From: Miao Jiang Date: Wed, 16 Jan 2019 10:07:05 +0800 Subject: [PATCH 27/45] Fixes #228 ' is duplicated inside a word --- plugin/auto-pairs.vim | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/plugin/auto-pairs.vim b/plugin/auto-pairs.vim index ad2e8f5..3f0a911 100644 --- a/plugin/auto-pairs.vim +++ b/plugin/auto-pairs.vim @@ -197,7 +197,7 @@ func! AutoPairsInsert(key) return s:right(m) end end - if open != close + if a:key == g:AutoPairsWildClosedPair || open != close let m = matchstr(after, '^\v\s*\zs\V'.close) if m != '' " skip close pair greedy @@ -315,7 +315,7 @@ func! AutoPairsFastWrap() return "" end endfor - if after[1:1] =~ '\v[a-zA-Z0-9_]' + if after[1:1] =~ '\v\w' normal! e normal! p else @@ -441,6 +441,9 @@ func! AutoPairsInit() " buffer level map pairs keys for [open, close] in items(b:AutoPairs) + if open == close && open == "'" + let open = '\v(^|\W)\zs''' + end let o = open[len(open)-1] let m = matchlist(close, '\v(.*)//(.*)$') let mapclose = 1 From 179520e6907c5c0e5bf723236fccf250243316bb Mon Sep 17 00:00:00 2001 From: Miao Jiang Date: Thu, 17 Jan 2019 15:51:19 +0800 Subject: [PATCH 28/45] Fixes #229 triple quote stopped working --- plugin/auto-pairs.vim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugin/auto-pairs.vim b/plugin/auto-pairs.vim index 3f0a911..7cd400f 100644 --- a/plugin/auto-pairs.vim +++ b/plugin/auto-pairs.vim @@ -211,6 +211,9 @@ func! AutoPairsInsert(key) " check open pairs let text=before.a:key for [open, close] in b:AutoPairsList + if open == close && open == "'" + let open = '\v(^|\W)\zs''' + end let m = s:matchend(text, open) if len(m) > 0 " process the open pair @@ -441,9 +444,6 @@ func! AutoPairsInit() " buffer level map pairs keys for [open, close] in items(b:AutoPairs) - if open == close && open == "'" - let open = '\v(^|\W)\zs''' - end let o = open[len(open)-1] let m = matchlist(close, '\v(.*)//(.*)$') let mapclose = 1 From 4af571e18bb48a2244fe89880a69c995a38d6446 Mon Sep 17 00:00:00 2001 From: Miao Jiang Date: Thu, 17 Jan 2019 19:05:58 +0800 Subject: [PATCH 29/45] New modifier s --- README.md | 25 +++++++++++++++-- plugin/auto-pairs.vim | 64 +++++++++++++++++++++++++------------------ 2 files changed, 61 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 862c9b9..4c03984 100644 --- a/README.md +++ b/README.md @@ -307,7 +307,28 @@ Multibyte Pairs The text after // in close pair is modifiers - n - do not map the first charactor of closed pair + n - do not map the first charactor of closed pair to close key + s - do not jump through multi line + + pair: "'''":"'''" + input: + ''' + | + ''' (press ') + output: + ''' + + '''| + + pair: "'''":"'''//s" + input: + ''' + | + ''' (press ') + output: + ''' + '|' + ''' for 'begin' 'end' pair, e is a charactor, if map e to jump will be annoy, so use modifier 'n' to skip key map @@ -349,7 +370,7 @@ Multibyte Pairs the 'begin' 'end' pair write in - au FileType ruby let b:AutoPairs = AutoPairsDefine({'\v(^|[^\w])\zsbegin': 'end//n'}) + au FileType ruby let b:AutoPairs = AutoPairsDefine({'\v(^|\W)\zsbegin': 'end//n'}) will be better, only auto pair when at start of line or follow non-word text diff --git a/plugin/auto-pairs.vim b/plugin/auto-pairs.vim index 7cd400f..88a2264 100644 --- a/plugin/auto-pairs.vim +++ b/plugin/auto-pairs.vim @@ -159,14 +159,14 @@ endf " add pair and remove '{' for html file func! AutoPairsDefine(pairs, ...) let r = copy(g:AutoPairs) - for [open, close] in items(a:pairs) - let r[open] = close - endfor if a:0 > 1 for open in a:1 unlet r[open] endfor end + for [open, close] in items(a:pairs) + let r[open] = close + endfor return r endf @@ -185,8 +185,8 @@ func! AutoPairsInsert(key) end " check close pairs - for [open, close] in b:AutoPairsList - if a:key == g:AutoPairsWildClosedPair || close[0] == a:key + for [open, close, opt] in b:AutoPairsList + if a:key == g:AutoPairsWildClosedPair || opt['mapclose'] && close[0] == a:key " the close pair is in the same line let m = matchstr(afterline, '^\v\s*\V'.close) if m != '' @@ -197,12 +197,14 @@ func! AutoPairsInsert(key) return s:right(m) end end - if a:key == g:AutoPairsWildClosedPair || open != close - let m = matchstr(after, '^\v\s*\zs\V'.close) - if m != '' + let m = matchstr(after, '^\v\s*\zs\V'.close) + if m != '' + if a:key == g:AutoPairsWildClosedPair || opt['multiline'] " skip close pair greedy call search(m, 'We') return "\" + else + break end end end @@ -210,10 +212,7 @@ func! AutoPairsInsert(key) " check open pairs let text=before.a:key - for [open, close] in b:AutoPairsList - if open == close && open == "'" - let open = '\v(^|\W)\zs''' - end + for [open, close, opt] in b:AutoPairsList let m = s:matchend(text, open) if len(m) > 0 " process the open pair @@ -229,7 +228,7 @@ func! AutoPairsInsert(key) while len(text) >= len(target) && target != text let found = 0 " delete pair - for [o, c] in b:AutoPairsList + for [o, c, opt] in b:AutoPairsList let m = s:matchend(text, o) if len(m) > 0 let found = 1 @@ -274,7 +273,7 @@ func! AutoPairsDelete() end let [before, after, ig] = s:getline() - for [open, close] in b:AutoPairsList + for [open, close, opt] in b:AutoPairsList let b = matchstr(before, '\V'.open.'\v\s?$') let a = matchstr(after, '^\v\s*\V'.close) if b != '' && a != '' @@ -291,7 +290,7 @@ func! AutoPairsDelete() return "\" " delete the pair foo[]| to foo - for [open, close] in b:AutoPairsList + for [open, close, opt] in b:AutoPairsList let m = s:matchend(before, '\V'.open.'\v\s*'.'\V'.close.'\v$') if len(m) > 0 return s:backspace(m[2]) @@ -310,7 +309,7 @@ func! AutoPairsFastWrap() normal! % normal! p else - for [open, close] in b:AutoPairsList + for [open, close, opt] in b:AutoPairsList if after =~ '^\s*\V'.open call search(close, 'We') normal! p @@ -353,10 +352,13 @@ func! AutoPairsReturn() if b:autopairs_enabled == 0 return '' end - let before = getline(line('.')-1) - let after = getline('.') + " let before = getline(line('.')-1) + let [ig, ig, after] = s:getline() let cmd = '' - for [open, close] in b:AutoPairsList + for [open, close, opt] in b:AutoPairsList + if close == '' + continue + end " before =~ '\V'.open.'\v\s*$' && if after =~ '^\s*\V'.close if g:AutoPairsCenterLine && winline() * 3 >= winheight(0) * 2 @@ -390,7 +392,7 @@ func! AutoPairsSpace() let [before, after, ig] = s:getline() - for [open, close] in b:AutoPairsList + for [open, close, opt] in b:AutoPairsList if before =~ '\V'.open.'\v$' && after =~ '^\V'.close return "\\".s:Left end @@ -407,7 +409,6 @@ func! AutoPairsMap(key) let escaped_key = substitute(key, "'", "''", 'g') " use expr will cause search() doesn't work execute 'inoremap '.key." =AutoPairsInsert('".escaped_key."')" - endf func! AutoPairsToggle() @@ -446,24 +447,35 @@ func! AutoPairsInit() for [open, close] in items(b:AutoPairs) let o = open[len(open)-1] let m = matchlist(close, '\v(.*)//(.*)$') - let mapclose = 1 + let opt = {'mapclose': 1, 'multiline':1} if len(m) > 0 - if m[1] =~ 'n' - let mapclose = 0 + if m[2] =~ 'n' + let opt['mapclose'] = 0 + end + if m[2] =~ 's' + let opt['multiline'] = 0 end let close = m[1] end let c = close[0] call AutoPairsMap(o) - if o != c && c != '' && mapclose + if o != c && c != '' && opt['mapclose'] call AutoPairsMap(c) end - let b:AutoPairsList += [[open, close]] + let b:AutoPairsList += [[open, close, opt]] endfor " sort pairs by length, longer pair should have higher priority let b:AutoPairsList = sort(b:AutoPairsList, "s:sortByLength") + for item in b:AutoPairsList + let [open, close, opt] = item + if open == "'" && open == close + let item[0] = '\v(^|\W)\zs''' + end + endfor + + for key in split(b:AutoPairsMoveCharacter, '\s*') let escaped_key = substitute(key, "'", "''", 'g') execute 'inoremap =AutoPairsMoveCharacter('".escaped_key."')" From c2f0eef628ac1bc3c8ebb42a927c65d96699cf2c Mon Sep 17 00:00:00 2001 From: Miao Jiang Date: Sat, 19 Jan 2019 11:37:41 +0800 Subject: [PATCH 30/45] Prevent jumping if open key equals close key, fixes #231 --- README.md | 27 ++++++++------------------- plugin/auto-pairs.vim | 18 +++++++++++++++--- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 4c03984..68643ed 100644 --- a/README.md +++ b/README.md @@ -308,27 +308,16 @@ Multibyte Pairs The text after // in close pair is modifiers n - do not map the first charactor of closed pair to close key - s - do not jump through multi line + m - close key jumps through multi line + s - close key jumps only in the same line - pair: "'''":"'''" - input: - ''' - | - ''' (press ') - output: - ''' - - '''| + by default if open key equals close key the multi line is turn off - pair: "'''":"'''//s" - input: - ''' - | - ''' (press ') - output: - ''' - '|' - ''' + "" ? jumps only in the same line + "//m" force ? jumping through multi line + "" ? will jump through multi line + "//s" force ? only jumping in the same line + "//n" do not jump totally for 'begin' 'end' pair, e is a charactor, if map e to jump will be annoy, so use modifier 'n' to skip key map diff --git a/plugin/auto-pairs.vim b/plugin/auto-pairs.vim index 88a2264..ee63471 100644 --- a/plugin/auto-pairs.vim +++ b/plugin/auto-pairs.vim @@ -393,6 +393,9 @@ func! AutoPairsSpace() let [before, after, ig] = s:getline() for [open, close, opt] in b:AutoPairsList + if close == '' + continue + end if before =~ '\V'.open.'\v$' && after =~ '^\V'.close return "\\".s:Left end @@ -444,20 +447,29 @@ func! AutoPairsInit() let b:AutoPairsList = [] " buffer level map pairs keys + " n - do not map the first charactor of closed pair to close key + " m - close key jumps through multi line + " s - close key jumps only in the same line for [open, close] in items(b:AutoPairs) - let o = open[len(open)-1] - let m = matchlist(close, '\v(.*)//(.*)$') + let o = open[-1:-1] + let c = close[0] let opt = {'mapclose': 1, 'multiline':1} + if o == c + let opt['multiline'] = 0 + end + let m = matchlist(close, '\v(.*)//(.*)$') if len(m) > 0 if m[2] =~ 'n' let opt['mapclose'] = 0 end + if m[2] =~ 'm' + let opt['multiline'] = 1 + end if m[2] =~ 's' let opt['multiline'] = 0 end let close = m[1] end - let c = close[0] call AutoPairsMap(o) if o != c && c != '' && opt['mapclose'] call AutoPairsMap(c) From 81a4a9fe5b99d357462798610e295d0a314bf380 Mon Sep 17 00:00:00 2001 From: Phillip Vo Date: Fri, 18 Jan 2019 23:40:08 -0500 Subject: [PATCH 31/45] Allow removeOpenPairList's with a single element in AutoPairsDefine function --- plugin/auto-pairs.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/auto-pairs.vim b/plugin/auto-pairs.vim index ee63471..d06796c 100644 --- a/plugin/auto-pairs.vim +++ b/plugin/auto-pairs.vim @@ -159,7 +159,7 @@ endf " add pair and remove '{' for html file func! AutoPairsDefine(pairs, ...) let r = copy(g:AutoPairs) - if a:0 > 1 + if a:0 > 0 for open in a:1 unlet r[open] endfor From 453d4889876cb89fd1381f68645673250405228d Mon Sep 17 00:00:00 2001 From: Miao Jiang Date: Sun, 20 Jan 2019 12:02:09 +0800 Subject: [PATCH 32/45] Compatible with vim 7.3, fixes #233 --- plugin/auto-pairs.vim | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/plugin/auto-pairs.vim b/plugin/auto-pairs.vim index d06796c..72a1424 100644 --- a/plugin/auto-pairs.vim +++ b/plugin/auto-pairs.vim @@ -143,6 +143,7 @@ func! s:matchend(text, open) end return [a:text, strpart(a:text, 0, len(a:text)-len(m)), m] endf + func! s:matchbegin(text, close) let m = matchstr(a:text, '^\V'.a:close) if m == "" @@ -186,6 +187,9 @@ func! AutoPairsInsert(key) " check close pairs for [open, close, opt] in b:AutoPairsList + if close == '' + continue + end if a:key == g:AutoPairsWildClosedPair || opt['mapclose'] && close[0] == a:key " the close pair is in the same line let m = matchstr(afterline, '^\v\s*\V'.close) @@ -213,38 +217,38 @@ func! AutoPairsInsert(key) " check open pairs let text=before.a:key for [open, close, opt] in b:AutoPairsList - let m = s:matchend(text, open) - if len(m) > 0 + let ms = s:matchend(text, open) + if len(ms) > 0 " process the open pair " remove inserted pair " eg: if the pairs include < > and " when " when + " when - " when '}, ['{']) " add pair and remove '{' for html file func! AutoPairsDefine(pairs, ...) - let r = copy(g:AutoPairs) + let r = AutoPairsDefaultPairs() if a:0 > 0 for open in a:1 unlet r[open] @@ -453,10 +475,9 @@ func! AutoPairsInit() end if !exists('b:AutoPairs') - let b:AutoPairs = g:AutoPairs + let b:AutoPairs = AutoPairsDefaultPairs() end - if !exists('b:AutoPairsMoveCharacter') let b:AutoPairsMoveCharacter = g:AutoPairsMoveCharacter end From 65a9f452f2238ca6c89576a7cdd305d78fda28e5 Mon Sep 17 00:00:00 2001 From: Miao Jiang Date: Wed, 27 Feb 2019 16:34:50 +0800 Subject: [PATCH 44/45] Obsolete wild close key by default, add custom close key, improve #242 --- README.md | 10 ++-------- plugin/auto-pairs.vim | 12 +++++++++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index d467cf9..8d079ba 100644 --- a/README.md +++ b/README.md @@ -248,14 +248,6 @@ Options Map to move character under the cursor to the pair. -* g:AutoPairsWildClosedPair - - Default: ']' - - Jump over following closed pair - for pair {'begin': 'end//n]'}, e is not mapped, use wild closed pair ] to jump over 'end' - use to back insert ] after jumping - Buffer Level Pairs Setting -------------------------- @@ -313,6 +305,7 @@ Multibyte Pairs n - do not map the first charactor of closed pair to close key m - close key jumps through multi line s - close key jumps only in the same line + k[KEY] - map the close key to [KEY] by default if open key equals close key the multi line is turn off @@ -321,6 +314,7 @@ Multibyte Pairs "" ? will jump through multi line "//s" force ? only jumping in the same line "//n" do not jump totally + "//k]" use key ] to jump through ?> for 'begin' 'end' pair, e is a charactor, if map e to jump will be annoy, so use modifier 'n' to skip key map diff --git a/plugin/auto-pairs.vim b/plugin/auto-pairs.vim index e630454..66a0c6c 100644 --- a/plugin/auto-pairs.vim +++ b/plugin/auto-pairs.vim @@ -25,7 +25,7 @@ func! AutoPairsDefaultPairs() let allPairs = { \ 'vim': {'\v^\s*\zs"': ''}, \ 'rust': {'\w\zs<': '>', '&\zs''': ''}, - \ 'php': {'//n', '//n'} + \ 'php': {'//k]', '//k]'} \ } for [filetype, pairs] in items(allPairs) if &filetype == filetype @@ -52,7 +52,7 @@ if !exists('g:AutoPairsMapCR') end if !exists('g:AutoPairsWildClosedPair') - let g:AutoPairsWildClosedPair = ']' + let g:AutoPairsWildClosedPair = '' end if !exists('g:AutoPairsMapSpace') @@ -263,7 +263,7 @@ func! AutoPairsInsert(key) if close == '' continue end - if a:key == g:AutoPairsWildClosedPair || opt['mapclose'] && close[0] == a:key + if a:key == g:AutoPairsWildClosedPair || opt['mapclose'] && opt['key'] == a:key " the close pair is in the same line let m = matchstr(afterline, '^\v\s*\V'.close) if m != '' @@ -494,6 +494,7 @@ func! AutoPairsInit() let o = open[-1:-1] let c = close[0] let opt = {'mapclose': 1, 'multiline':1} + let opt['key'] = c if o == c let opt['multiline'] = 0 end @@ -508,6 +509,11 @@ func! AutoPairsInit() if m[2] =~ 's' let opt['multiline'] = 0 end + let ks = matchlist(m[2], '\vk(.)') + if len(ks) > 0 + let opt['key'] = ks[1] + let c = opt['key'] + end let close = m[1] end call AutoPairsMap(o) From 39f06b873a8449af8ff6a3eee716d3da14d63a76 Mon Sep 17 00:00:00 2001 From: Miao Jiang Date: Wed, 27 Feb 2019 17:11:39 +0800 Subject: [PATCH 45/45] Comptabile with () mapping, fix #245 --- plugin/auto-pairs.vim | 1 + 1 file changed, 1 insertion(+) diff --git a/plugin/auto-pairs.vim b/plugin/auto-pairs.vim index 66a0c6c..af5eb32 100644 --- a/plugin/auto-pairs.vim +++ b/plugin/auto-pairs.vim @@ -596,6 +596,7 @@ endf func! s:ExpandMap(map) let map = a:map let map = substitute(map, '\(\w\+\)', '\=maparg(submatch(1), "i")', 'g') + let map = substitute(map, '\(([^)]*)\)', '\=maparg(submatch(1), "i")', 'g') return map endf