LunarWatcher / auto-pairs

Vim plugin, insert or delete brackets, parentheses, and quotes in pairs
MIT License
173 stars 6 forks source link

Multibyte pair \( and \) causing errors. #71

Closed HawkinsT closed 1 year ago

HawkinsT commented 1 year ago

OS Arch Linux What vim? (+ version) Neovim 0.8 and 0.9.0-dev

Autopairs config (if applicable):

let g:AutoPairsCompatibleMaps = 0
let g:AutoPairsMapBS = 1    " This is required for some reason otherwise <BS> is not set; not sure why as it's apparently the default value and this and packer are the only plugins I'm loading
let g:AutoPairs = autopairs#AutoPairsDefine([
    \ {'open': '\\left(', 'close': '\right)', 'filetype': 'tex'},
    \ {'open': '\\(', 'close': '\)', 'filetype': 'tex'},
    \ {'open': '\\[', 'close': '\]', 'filetype': 'tex'}])

Describe the bug I'm using the above multibyte pairs for latex. \[ and \left( work fine and are just here for comparison. \( also works fine and creates the associated \) pair, however if I then hit backspace I get the warning:

Error detected while processing function autopairs#AutoPairsDelete:
line 16:
E55: Unmatched \)

then

Error detected while processing function autopairs#AutoPairsDelete[41] .. autopairs#Strings#matchend:
line 1:
E55: Unmatched \)

However the deletion works (it just deletes one byte so I end up with \|\) where | is the cursor position). Deleting the other three bytes now also results in these same two errors being repeated three more times.

As another scenario, after creating the matched pairs \(|\) if I hit <CR> I get the error:

Error detected while processing function autopairs#Keybinds#IgnoreInsertEnter[3] .. autopairs#AutoPairsReturn:
line 18:
E55: Unmatched \)

and then the CR happens so I end up with:

\(
|\)

instead of

\(
|
\)

as happens with, e.g. ( and ).

Since this doesn't happen with the matched pairs \[|\] or \left(|\right) my assumption is it's some screwy regex related to #53. I've tested this on all three branches and an otherwise blank config (except for bootstrapping packer and loading auto-pairs with the above settings) and all have this issue.

LunarWatcher commented 1 year ago

It is indeed screwy regex. AutoPairsBackspace doesn't properly escape close:

https://github.com/LunarWatcher/auto-pairs/blob/26dca000968d1c9dc133b1dec5dc8b23cc350ceb/autoload/autopairs.vim#L261

For comparison, this is from AutoPairsInsert, which is fine:

https://github.com/LunarWatcher/auto-pairs/blob/26dca000968d1c9dc133b1dec5dc8b23cc350ceb/autoload/autopairs.vim#L162

The problem is that I use \V to "disable" regex constructs, so to use a group, you'd have to use \(\). This is where the bad escape comes in. The escape method transforms \) to \\), which is interpreted as the literal \) rather than as a regex group.

As for the enter problem, same cause, in theory. Missing escape. Weird thing though, it doesn't error out for me (admittedly in Vim). I don't understand why, but I mean, if I can fix it anyway, it doesn't really matter.

I really need to revisit a large part of the pattern matching. I'll make a hotfix for the errors (or try anyway), but if that doesn't fix return, that's a wontfix until #53 is fixed (which I've been putting off because of Other Stuff:tm:, but no time like the present I suppose)

HawkinsT commented 1 year ago

Many thanks for your quick response (and continued work on auto-pairs)! I'll await your hotfix and report back; let me know if you'd like me to test anything else related to this.

LunarWatcher commented 1 year ago

Hotfix pushed; confirmation it also works in neovim would be great!

HawkinsT commented 1 year ago

I can confirm that BS and CR now work as expected in Neovim also. Thanks again!