Skip to content

Commit

Permalink
Add support for arbitrary keystrokes to fall back to for deletions (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
LunarWatcher committed Sep 12, 2024
1 parent 2c18fa6 commit 6971bcc
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

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#AutoPairsDelete()<CR>

------------------------------------------------------------------------------
*g:AutoPairsMultilineBackspace*
Expand Down
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 6971bcc

Please sign in to comment.