Skip to content

Commit

Permalink
Merge develop into master - closes #90
Browse files Browse the repository at this point in the history
  • Loading branch information
LunarWatcher committed Sep 12, 2024
2 parents 2c18fa6 + 3700c50 commit 9f7b3f2
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 20 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# 4.0.3
# 4.1.0
`g:AutoPairsVersion = 40003`

## Added
* 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 `<BS>`. 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

Expand Down
11 changes: 6 additions & 5 deletions autoload/autopairs.vim
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,11 @@ func! autopairs#AutoPairsInsert(key, ...)
return a:key
endf

func! autopairs#AutoPairsDelete()
func! autopairs#AutoPairsDelete(...)
let fallbackEvent = get(a:, 1, "\<BS>")
if !b:autopairs_enabled || b:AutoPairsIgnoreSingle
let b:AutoPairsIgnoreSingle = 0
return "\<BS>"
return fallbackEvent
end

let [before, after, ig] = autopairs#Strings#getline(b:AutoPairsMultilineBackspace)
Expand All @@ -270,7 +271,7 @@ func! autopairs#AutoPairsDelete()
if a[0] == ' '
return "\<BS>\<DELETE>"
else
return "\<BS>"
return fallbackEvent
end
end
return autopairs#Strings#backspace(b) .. autopairs#Strings#delete(a)
Expand Down Expand Up @@ -302,7 +303,7 @@ func! autopairs#AutoPairsDelete()
let b ..= getline(line('.') - offset) .. ' '
let offset += 1
if (line('.') - offset <= 0)
return "\<BS>"
return fallbackEvent
endif
endwhile
let a = matchstr(getline(line('.') - offset), autopairs#Utils#escape(open, opt) .. '\v\s*$') .. ' '
Expand All @@ -313,7 +314,7 @@ func! autopairs#AutoPairsDelete()
end
endfor
endif
return "\<BS>"
return fallbackEvent
endf

" Fast wrap the word in brackets
Expand Down
2 changes: 1 addition & 1 deletion doc/AutoPairs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ Default: 0

Map <BS> to delete brackets and quotes in pair, executes:

inoremap <buffer> <silent> <BS> <C-R>=AutoPairsDelete()<CR>
inoremap <buffer> <silent> <BS> <C-R>=autopairs#AutoPairsReturn()<CR>

------------------------------------------------------------------------------
*g:AutoPairsMultilineBackspace*
Expand Down
95 changes: 90 additions & 5 deletions doc/AutoPairsHowTo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <CR> 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 <CR> for autocomplete ........................ |autopairs-autocomplete-cr|
Using non-<BS> keys for deleting pairs ............. |autopairs-non-bs-deletion|
1. Alternative keys ............................ |autopairs-delete-other-key|
2. Using <C-w>, <Delete>, or similar ........... |autopairs-delete-other-action|

==============================================================================
About this document *autopairs-howto-about*
Expand Down Expand Up @@ -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-<BS> keys for deleting pairs *autopairs-non-bs-deletion*

This section describes two scenarios:

1. Using alternative keys instead of <BS>
2. Using other strokes (such as <C-w> or <Delete>) to trigger pair deletion

--------------------------------------------------------------------------------
1. Alternative keys *autopairs-delete-other-key*

For using an alternate key that acts identically to <BS>, you can recursively
remap that key to <BS>: >
imap <somekey> <BS>
<

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 <silent> <somekey> <C-R>=autopairs#AutoPairsDelete()<CR>
<

--------------------------------------------------------------------------------
2. Using <C-w>, <Delete>, or similar *autopairs-delete-other-action*


In some cases, you may want pair deletion to work with keys other than <BS>.
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 <C-w>, for example, it would delete pairs, but
it would also convert <C-w> into a glorified <BS> that no longer deletes entire
words like one might expect.

If, for example, you want to map `<C-w>` to delete pairs if possible, but
without affecting your ability to use it to delete words, you'd use[^1]: >
inoremap <silent><expr> <C-w> autopairs#AutoPairsDelete("\<C-w>")
<

If you have >
(|)
<
and now press <C-w>, the pair will be deleted. But if you have: >
(Hello|)
<

and press <C-w>, you get: >
(|)
<
Again, as expected from <C-w>.

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 <Delete> will result in unintuitive behaviour around
deletion. If you have the following scenario: >
()|[]
<
And use <Delete> as a special pair delete key, it will NOT delete the pair in
front of the cursor. After pressing <Delete>, you'll have: >
|[]
<
... or exactly the same thing <BS> would've done. This is the case for all
alternate actions.

While you can remap <Delete>, 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 <expr> here, but not in the previous section, you ask?

I have no fucking clue. <C-R>= refused to cooperate with the string
argument, and incorrectly claims autopairs#AutoPairsDelete does not exist.
<expr> does not have this issue, but requires dropping <C-R>.

vim:ft=help
4 changes: 2 additions & 2 deletions test/ComplexTests.vimspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Describe Complex use of maps
" sends some random signal crap
call Expect("<\<Delete>]").ToMatch("<]")
%d
exec "normal i<\<ESC>"
exec "normal ^\<Right>i>\<ESC>"
exec "normal i<"
exec "normal ^\<Right>i>"
call Expect('').CheckBuff("<>")
End
End
37 changes: 31 additions & 6 deletions test/DeleteTest.vimspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,42 @@ Describe <BS> tests
call autopairs#AutoPairsInit()

call Expect("()\<BS>").ToMatch("")
let b:AutoPairsBSAfter = 0

call Expect("()\<BS>").ToMatch("(")
let b:AutoPairsBSIn = 0
call Expect("(\<BS>").ToMatch(")")
End
It should delete in when instructed
It should allow mapping other default events
new | only!
call autopairs#AutoPairsInit()
inoremap <silent><expr> <Delete> autopairs#AutoPairsDelete("\<Delete>")
inoremap <silent><expr> <C-w> autopairs#AutoPairsDelete("\<C-w>")

call Expect("(\<Delete>").ToMatch("")
" This is sort of dumb, but eh, it's an edge-case
call Expect("()\<Delete>").ToMatch("")


call Expect("(\<C-w>").ToMatch("")
call Expect("()\<C-w>").ToMatch("")
call Expect("Word Two\<C-w>").ToMatch("Word ")

call Expect("(Hello\<Ignore>\<C-w>").ToMatch("()")
call Expect("(Hello\<Ignore>\<C-w>\<Ignore>\<C-w>").ToMatch("")
" Just for good measure
call Expect("[(Hello\<Ignore>\<C-w>\<Ignore>\<C-w>").ToMatch("[]")

iunmap <C-w>
iunmap <Delete>
End
" Not sure if this is strictly speaking necessary or not
It should not break <BS> with other maps
inoremap <silent><expr> <Delete> autopairs#AutoPairsDelete("\<Delete>")
inoremap <silent><expr> <C-w> autopairs#AutoPairsDelete("\<C-w>")

call Expect("(\<BS>").ToMatch("")
let b:AutoPairsBSIn = 0
call Expect("(\<BS>").ToMatch(")")
call Expect("[\<BS>").ToMatch("")

iunmap <C-w>
iunmap <Delete>
End

End

0 comments on commit 9f7b3f2

Please sign in to comment.