Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
LunarWatcher committed Jun 19, 2021
1 parent 84a93e2 commit 6dcc81f
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 31 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# 3.0.0-beta12
`let g:AutoPairsVersion = 30057`

## Fixed
* Better escape handing ([jiangmiao/#325](https://github.com/jiangmiao/auto-pairs/issues/325))
* Typos in the help document
* Update variable list in the help document

## Added
* `g:AutoPairsSearchEscape`

# 3.0.0-beta11
`let g:AutoPairsVersion = 30056`

Expand Down
65 changes: 50 additions & 15 deletions autoload/autopairs.vim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ scriptencoding utf-8
" Current version; not representative of tags or real versions, but purely
" meant as a number associated with the version. Semantic meaning on the first
" digit will take place. See the documentation for more details.
let g:AutoPairsVersion = 30056
let g:AutoPairsVersion = 30057

let s:save_cpo = &cpoptions
set cpoptions&vim
Expand Down Expand Up @@ -145,19 +145,55 @@ func! autopairs#AutoPairsInsert(key, ...)
let b:autopairs_saved_pair = [a:key, getpos('.')]

let [before, after, afterline] = autopairs#Strings#getline(l:multiline)

" Ignore auto close if prev character is \
" And skip if it's double-escaped
if before[-1:-1] == '\' && before[-2:-1] != "\\\\"
return a:key
end

" check open pairs
" Check open pairs {{{
" TODO: maybe move this to another file?
for [open, close, opt] in b:AutoPairsList
let ms = autopairs#Strings#matchend(before . a:key, open)
let m = matchstr(afterline, '^\v\s*\zs\V'.close)

if len(ms) > 0
let target = ms[1]
let openPair = ms[2]


" To compensate for multibyte pairs,
" we need to search for escaping after we find a match.
" Since b:AutoPairsList is sorted by pair size, we can assume that
" if we find \[ and it's matched, it's because there's a pair that
" matches \[, and not that we have [ escaped.
if b:AutoPairsSearchEscape
" Intermediate length
let pLen = len(openPair)
" First check the character prior to the character of the
" current pair. Take \[ in LaTeX:
" 0 0 \ [
" -1 0 1 2
" ^ pair match found starting here -- that's why we're
" referencing the length of the found pair.
" Because of potentially varying width, we need to figure
" out how long the match we wanna search is.
" ^ Look for backslash here. If one is found,
" ^ Look for one here. Essentially, this is to make sure we
" don't trigger a false positive on, among other things,
" '\\|', type ' at |. Essentially, if the backslash is
" escaped, we assume that the pair character isn't.
" This doesn't take into account '\\\|', ' at |,
" because it only checks the last two backslashes.
" There's no good way to check for correct escaping without
" doing an obnoxious amount of checks, which is overkill
" for a case like this.
" (0 is a reference to null, meaning there's no string at that
" position)
"
" TL;DR: if we find a backslash in front of the pair, we then know
" it's escaped, and we don't want to insert the close.
" If there's another backslash in front of the backslash in
" front of the pair, we assume the backslash is escaped and
" insert the pair anyway.
if before[-pLen:-pLen] == '\' && before[-pLen - 1:-pLen - 1] != '\'
return a:key
endif
endif

" Krasjet: only insert the closing pair if the next character is a space
" or a non-quote closing pair, or a whitelisted character (string)
Expand All @@ -175,8 +211,6 @@ func! autopairs#AutoPairsInsert(key, ...)
" eg: if the pairs include < > and <!-- -->
" when <!-- is detected the inserted pair < > should be clean up
"
let target = ms[1]
let openPair = ms[2]

if (len(openPair) == 1 && m == openPair) || (close == '')
break
Expand All @@ -197,8 +231,8 @@ func! autopairs#AutoPairsInsert(key, ...)
let found = 1
let before = os[1]
let afterline = cs[2]
let bs = bs.autopairs#Strings#backspace(os[2])
let del = del.autopairs#Strings#delete(cs[1])
let bs = bs . autopairs#Strings#backspace(os[2])
let del = del . autopairs#Strings#delete(cs[1])
break
end
endfor
Expand All @@ -211,14 +245,15 @@ func! autopairs#AutoPairsInsert(key, ...)
end
end
endwhile

return bs.del.openPair.close.autopairs#Strings#left(close)
return bs . del . openPair
\ . close . autopairs#Strings#left(close)
\ . (index(b:AutoPairsAutoLineBreak, open) != -1 ?
\ "\<cr>".autopairs#AutoPairsDetermineCRMovement()
\ : '')

end
endfor
" }}}

let checkClose = autopairs#Insert#checkClose(a:key, before, after, afterline)
if checkClose != ""
Expand Down
2 changes: 2 additions & 0 deletions autoload/autopairs/Variables.vim
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ fun! autopairs#Variables#_InitVariables()

call s:define('g:AutoPairsMultilineClose', 0)
call s:define('g:AutoPairsShortcutToggleMultilineClose', '<C-p><C-m>')
call s:define('g:AutoPairsSearchEscape', 1)

if exists('g:AutoPairsEnableMove')
echom "g:AutoPairsEnableMove has been deprecated. If you set it to 1, you may remove it."
Expand Down Expand Up @@ -151,6 +152,7 @@ fun! autopairs#Variables#_InitBufferVariables()
call s:define('b:AutoPairsMultilineBackspace', g:AutoPairsMultilineBackspace)

call s:define('b:AutoPairsMultilineClose', g:AutoPairsMultilineClose)
call s:define('b:AutoPairsSearchEscape', g:AutoPairsSearchEscape)

" Keybinds
call s:define('b:AutoPairsMapCR', g:AutoPairsMapCR)
Expand Down
60 changes: 44 additions & 16 deletions doc/AutoPairs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -352,28 +352,28 @@ autopairs#AutoPairsInit()'s source if in doubt.
*autopairs-options-contents*

Note that this list isn't entirely in order at the time of writing, but tries
to link associateed entries as close as possible.
to link associated entries as close as possible.

- |g:AutoPairs| (has buffer variant) (default: see the entry)
Defines what pairs AutoPairs will complete
- |g:AutoPairsCompleteOnlyOnSpace| (has a buffer variant) (default: 0)
Defines whether or not there has to be a space (or EOL) to insert a pair
- |g:AutoPairsShortcutToggle| (default: see the entry)
- |g:AutoPairsShortcutToggle| (has a buffer variant) (default: see the entry)
Defines the shortcut for toggling auto-pairs
- |g:AutoPairsShortcutFastWrap| (default: see the entry)
- |g:AutoPairsShortcutFastWrap| (has a buffer variant) (default: see the entry)
Defines the shortcut for fast wrap
- |g:AutoPairsShortcutJump| (default: see the entry)
- |g:AutoPairsShortcutJump| (has a buffer variant) (default: see the entry)
Defines thte shortcut for jumping to the first closed pair, regardless of
what that pair is
- |g:AutoPairsShortcutBackInsert| (default: see the entry)
Defines the shortcut for undoing a flymode jump.
- |g:AutoPairsMapBS| (default: 0)
- |g:AutoPairsMapBS| (has a buffer variant) (default: 0)
Whether or not to map backspace
- |g:AutoPairsMultilineBackspace| (has a buffer variant) (default: 0)
Whether or not to use multiline deletion
- |g:AutoPairsMapCR| (default: 1)
Whether or not to map enter (/return)
- |g:AutoPairsCRKey| (default: '<CR>')
- |g:AutoPairsMapCR| (has a buffer variant) (default: 1)
Whether or not to map enter
- |g:AutoPairsCRKey| (has a buffer variant) (default: '<CR>')
Which key to use for the return action if |g:AutoPairsMapCR| is 1
- |g:AutoPairsJumpBlacklist| (has buffer variant) (default: [])
Defines a set of close keybinds that don't trigger close skipping.
Expand Down Expand Up @@ -424,7 +424,7 @@ to link associateed entries as close as possible.
- |g:AutoPairsOpenBalanceBlacklist| (has a buffer variant) (default: [])
Contains a list of characters that don't trigger opening balance checks
- |g:AutoPairsMultilineFastWrap| (default: 0)
Whether or not to let fast wraop work across lines intentionally
Whether or not to let fast wrap work across lines intentionally
- |g:AutoPairsBackwardsCompat| (default: 0)
Exposes a function for other plugins that haven't been made compatible
with this fork, but that actively access it
Expand All @@ -445,15 +445,23 @@ to link associateed entries as close as possible.
Whether or not to use legacy keybinds (from jiangmiao/auto-pairs), or to
use new ones designed not to conflict with other maps, or with characters
in various languages.
- |g:AutoPairsReturnOnEmptyOnly| (default: 1)
- |g:AutoPairsReturnOnEmptyOnly| (has a buffer variant) (default: 1)
Whether or not the pair has to be empty to trigger the return action
- |g:AutoPairsExperimentalAutocmd| (default: 0)
Whether or not to use BufWinEnter instead of BufEnter
- |g:AutoPairsStringHandlingMode| (default: 0)
- |g:AutoPairsStringHandlingMode| (has a buffer variant) (default: 0)
How auto-pairs handles strings. HIGHLY EXPERIMENTAL!
- |g:AutoPairsMoveExpression| (default: <C-p>%key)
- |g:AutoPairsMoveExpression| (has a buffer variant) (default: <C-p>%key)
Defines an expression that triggers the move feature. Please read the
entry before using.
- |g:AutoPairsMultilineClose| (has a buffer variant) (default: 0)
Whether or not to search across lines when looking for close pairs.
- |g:AutoPairsShortcutToggleMultilineClose| (has a buffer variant) (default: <C-p><C-m>)
What keybind to use for toggling |g:AutoPairsMultilineClose|
- |g:AutoPairsPreferClose| (default: 1)
Whether to prefer closing over jumping.
- |g:AutoPairsSearchEscape| (has a buffer variant) (default: 1)
Whether or not to take backslashes into account when inserting pairs

------------------------------------------------------------------------------
*g:AutoPairs* *b:AutoPairs*
Expand Down Expand Up @@ -556,19 +564,22 @@ There shouldn't be a limit the length (though, for obvious reasons, things
might go wrong if you decide to input an obnoxiously long mapping), so you
should be able to map all pairs used by your favorite language or framework.

|autopairs-weird-behavior|
*autopairs-regex-escaping*

Open is used in a regex search, and because auto-pairs applies stuff directly,
there's a minor input escape issue. Most people aren't going to experience it,
but if you i.e. wanna add `'\left(': '\right)'`, there is actually a "right
way" to add it. `\left` may cause `\l` to be interpreted as a group; |\l|
way" to add it. `\left` may cause `\l` to be interpreted as regex; |\l|
to be specific.

Consequentially, the pair has to be declared as `'\\left(': '\right)'` . The
close pair is also used as a regex in some places, but shouldn't cause any
problems. Additional escaping will be added soon to mitigate problems, but
it's fairly niche. TL;DR: if you use backslash in an open pair, you _need_ to
escape it with `\\` for it to work properly
escape it with `\\` for it to work properly.

Note: due to how strings work, you'll need \\\\ if you use a double quote
string, or \\ is interpreted as \ in the string.

This is a bit of a double-sided blade, though; forcing escape of the close
pair in certain places means regex cannot be used. Regex groups for replace
Expand Down Expand Up @@ -1488,6 +1499,23 @@ jump instead of insert, which again, may or may not be desirable. If the
preceeding line contains an opening (, the closure would be correct.
Otherwise, it's not.

------------------------------------------------------------------------------
*g:AutoPairsSearchEscape*

Type: int
Default: 1

Whether or not to check if there's backslashes indicating that the
character(s) may have been escaped. The internal check is for \<pair> and
not \\<pair>: this means \[ doesn't insert the close pair, but \\[ does insert
].

Modified multibyte functionality in 3.0.0-beta12 means that if '\\[': '\]' is
defined as a pair, \\[ counts as escaping and \\\[ counts as the backslash
being escaped. The backslash before the [ is considered a part of the pair.
See |autopairs-regex-escaping| for an explanation on the double escaping in
the pair itself.

*autopairs-end-of-options* (this tag exists purely for doc editing purposes and
has no meaning for the plugin)
==============================================================================
Expand Down Expand Up @@ -1975,6 +2003,6 @@ new one.
To aid with backwards compat in the future, |g:AutoPairsVersion| has been
added to identify versions to give an easy way to detect API changes for
plugin compatibility. Unfortunately, it's introduced so late it's not really
useful for a few years.
useful for at least a few years.

vim:ft=help

0 comments on commit 6dcc81f

Please sign in to comment.