From 6971bcc756e5bacc3fed9a3675aba5c09cbe8718 Mon Sep 17 00:00:00 2001 From: Olivia Date: Thu, 12 Sep 2024 04:28:18 +0200 Subject: [PATCH 1/3] Add support for arbitrary keystrokes to fall back to for deletions (#90) --- CHANGELOG.md | 3 +++ autoload/autopairs.vim | 11 ++++++----- doc/AutoPairs.txt | 2 +- test/ComplexTests.vimspec | 4 ++-- test/DeleteTest.vimspec | 37 +++++++++++++++++++++++++++++++------ 5 files changed, 43 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3157bb6..c9150ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # 4.0.3 `g:AutoPairsVersion = 40003` +## Added +* Support for arbitrary underlying deletion keybinds ([#90](https://github.com/LunarWatcher/auto-pairs/issues/90)) + ## Fixed * Made `g:AutoPairsExperimentalAutocmd` actually reflect the new default-enabled state diff --git a/autoload/autopairs.vim b/autoload/autopairs.vim index 2f72001..ae4210a 100644 --- a/autoload/autopairs.vim +++ b/autoload/autopairs.vim @@ -247,10 +247,11 @@ func! autopairs#AutoPairsInsert(key, ...) return a:key endf -func! autopairs#AutoPairsDelete() +func! autopairs#AutoPairsDelete(...) + let fallbackEvent = get(a:, 1, "\") if !b:autopairs_enabled || b:AutoPairsIgnoreSingle let b:AutoPairsIgnoreSingle = 0 - return "\" + return fallbackEvent end let [before, after, ig] = autopairs#Strings#getline(b:AutoPairsMultilineBackspace) @@ -270,7 +271,7 @@ func! autopairs#AutoPairsDelete() if a[0] == ' ' return "\\" else - return "\" + return fallbackEvent end end return autopairs#Strings#backspace(b) .. autopairs#Strings#delete(a) @@ -302,7 +303,7 @@ func! autopairs#AutoPairsDelete() let b ..= getline(line('.') - offset) .. ' ' let offset += 1 if (line('.') - offset <= 0) - return "\" + return fallbackEvent endif endwhile let a = matchstr(getline(line('.') - offset), autopairs#Utils#escape(open, opt) .. '\v\s*$') .. ' ' @@ -313,7 +314,7 @@ func! autopairs#AutoPairsDelete() end endfor endif - return "\" + return fallbackEvent endf " Fast wrap the word in brackets diff --git a/doc/AutoPairs.txt b/doc/AutoPairs.txt index a435d7f..72129c9 100644 --- a/doc/AutoPairs.txt +++ b/doc/AutoPairs.txt @@ -774,7 +774,7 @@ Default: 0 Map to delete brackets and quotes in pair, executes: - inoremap =AutoPairsDelete() + inoremap =autopairs#AutoPairsDelete() ------------------------------------------------------------------------------ *g:AutoPairsMultilineBackspace* diff --git a/test/ComplexTests.vimspec b/test/ComplexTests.vimspec index 61b2c79..c384d9d 100644 --- a/test/ComplexTests.vimspec +++ b/test/ComplexTests.vimspec @@ -16,8 +16,8 @@ Describe Complex use of maps " sends some random signal crap call Expect("<\]").ToMatch("<]") %d - exec "normal i<\" - exec "normal ^\i>\" + exec "normal i<" + exec "normal ^\i>" call Expect('').CheckBuff("<>") End End diff --git a/test/DeleteTest.vimspec b/test/DeleteTest.vimspec index dea7bcb..bca3c2c 100644 --- a/test/DeleteTest.vimspec +++ b/test/DeleteTest.vimspec @@ -21,17 +21,42 @@ Describe tests call autopairs#AutoPairsInit() call Expect("()\").ToMatch("") - let b:AutoPairsBSAfter = 0 - - call Expect("()\").ToMatch("(") + let b:AutoPairsBSIn = 0 + call Expect("(\").ToMatch(")") End - It should delete in when instructed + It should allow mapping other default events new | only! call autopairs#AutoPairsInit() + inoremap autopairs#AutoPairsDelete("\") + inoremap autopairs#AutoPairsDelete("\") + + call Expect("(\").ToMatch("") + " This is sort of dumb, but eh, it's an edge-case + call Expect("()\").ToMatch("") + + + call Expect("(\").ToMatch("") + call Expect("()\").ToMatch("") + call Expect("Word Two\").ToMatch("Word ") + + call Expect("(Hello\\").ToMatch("()") + call Expect("(Hello\\\\").ToMatch("") + " Just for good measure + call Expect("[(Hello\\\\").ToMatch("[]") + + iunmap + iunmap + End + " Not sure if this is strictly speaking necessary or not + It should not break with other maps + inoremap autopairs#AutoPairsDelete("\") + inoremap autopairs#AutoPairsDelete("\") call Expect("(\").ToMatch("") - let b:AutoPairsBSIn = 0 - call Expect("(\").ToMatch(")") + call Expect("[\").ToMatch("") + + iunmap + iunmap End End From 30661c5ecd04cb943d1817fed306ce9dcc3474fc Mon Sep 17 00:00:00 2001 From: Olivia Date: Thu, 12 Sep 2024 04:28:52 +0200 Subject: [PATCH 2/3] Fix dumb typo How the fuck did that happen? --- doc/AutoPairs.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/AutoPairs.txt b/doc/AutoPairs.txt index 72129c9..9988be4 100644 --- a/doc/AutoPairs.txt +++ b/doc/AutoPairs.txt @@ -774,7 +774,7 @@ Default: 0 Map to delete brackets and quotes in pair, executes: - inoremap =autopairs#AutoPairsDelete() + inoremap =autopairs#AutoPairsReturn() ------------------------------------------------------------------------------ *g:AutoPairsMultilineBackspace* From 3700c5088b691d09f6f0dc48ec76dff8e95e83a0 Mon Sep 17 00:00:00 2001 From: Olivia Date: Thu, 12 Sep 2024 04:56:49 +0200 Subject: [PATCH 3/3] Update docs with stuff for special pair deletion --- CHANGELOG.md | 5 ++- doc/AutoPairsHowTo.txt | 95 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 93 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c9150ca..4b7cf28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,9 @@ -# 4.0.3 +# 4.1.0 `g:AutoPairsVersion = 40003` ## Added -* Support for arbitrary underlying deletion keybinds ([#90](https://github.com/LunarWatcher/auto-pairs/issues/90)) +* Support for arbitrary underlying deletion keybinds when deleting pairs ([#90](https://github.com/LunarWatcher/auto-pairs/issues/90)) + - This does not affect the default deletion keybinds, which still default to ``. This is only relevant if you want to delete pairs with other forms of deletion. ## Fixed * Made `g:AutoPairsExperimentalAutocmd` actually reflect the new default-enabled state diff --git a/doc/AutoPairsHowTo.txt b/doc/AutoPairsHowTo.txt index 50df3e1..a0615d5 100644 --- a/doc/AutoPairsHowTo.txt +++ b/doc/AutoPairsHowTo.txt @@ -11,11 +11,14 @@ Table of help documents~ ============================================================================== CONTENTS *autopairs-howto-contents* - About this document .......................... |autopairs-howto-about| - Requiring whitespace when inserting .......... |autopairs-howto-whitespace-only| - Contextual pair insertion .................... |autopairs-howto-contextual| - Regex-based pairs ............................ |autopairs-adding-regex-pairs| - Using for the autocomplete popup ........ |autopairs-autocomplete-cr| + About this document ................................ |autopairs-howto-about| + Requiring whitespace when inserting ................ |autopairs-howto-whitespace-only| + Configuring contextual insertion ................... |autopairs-howto-contextual| + Adding regex pairs ................................. |autopairs-adding-regex-pairs| + Using for autocomplete ........................ |autopairs-autocomplete-cr| + Using non- keys for deleting pairs ............. |autopairs-non-bs-deletion| + 1. Alternative keys ............................ |autopairs-delete-other-key| + 2. Using , , or similar ........... |autopairs-delete-other-action| ============================================================================== About this document *autopairs-howto-about* @@ -127,5 +130,87 @@ documentation for the source template to copy-pasta. I also recommend checking coc.nvim's documentation for a template to copy-pasta, as the one kept here will not be kept up-to-date. +================================================================================ +Using non- keys for deleting pairs *autopairs-non-bs-deletion* + +This section describes two scenarios: + +1. Using alternative keys instead of +2. Using other strokes (such as or ) to trigger pair deletion + +-------------------------------------------------------------------------------- +1. Alternative keys *autopairs-delete-other-key* + +For using an alternate key that acts identically to , you can recursively +remap that key to : > + imap +< + +Note that, for pair deletion to work, |g:AutoPairsMapBS| MUST be set to 1. At +the time of writing, if you want to remap that other key to be the only trigger, +you need to do so with a bit more involved map: > + inoremap =autopairs#AutoPairsDelete() +< + +-------------------------------------------------------------------------------- +2. Using , , or similar *autopairs-delete-other-action* + + +In some cases, you may want pair deletion to work with keys other than . +For convenience, auto-pairs lets you define ✨ special maps ✨ to work with +these. autopairs#AutoPairsDelete() accepts a single argument with the default +action to take if not deleting pairs. Using this does not affect how the pairs +are deleted, but it changes what default action to take for that map if pair +deletions aren't applicable. + +If you were to use method 1 for , for example, it would delete pairs, but +it would also convert into a glorified that no longer deletes entire +words like one might expect. + +If, for example, you want to map `` to delete pairs if possible, but +without affecting your ability to use it to delete words, you'd use[^1]: > + inoremap autopairs#AutoPairsDelete("\") +< + +If you have > + (|) +< +and now press , the pair will be deleted. But if you have: > + (Hello|) +< + +and press , you get: > + (|) +< +Again, as expected from . + +Note that the keybind needs to be present in both the string and in the keymap +itself. The action actually taken by auto-pairs is dictated by the string, and +not the keybind the function is called in. + +WARNING: Using this with will result in unintuitive behaviour around +deletion. If you have the following scenario: > + ()|[] +< +And use as a special pair delete key, it will NOT delete the pair in +front of the cursor. After pressing , you'll have: > + |[] +< +... or exactly the same thing would've done. This is the case for all +alternate actions. + +While you can remap , this unintuitive behaviour means I don't recommend +doing so. Forward-looking deletions may come at some point in the future, but +are not currently planned. If you want it, consider opening a PR. + +Associated FR:~ + https://github.com/LunarWatcher/auto-pairs/issues/90 + +Section footnotes~ +[^1]: Why here, but not in the previous section, you ask? + + I have no fucking clue. = refused to cooperate with the string + argument, and incorrectly claims autopairs#AutoPairsDelete does not exist. + does not have this issue, but requires dropping . vim:ft=help