Skip to content

Instantly share code, notes, and snippets.

@plane

plane/rparen.vim Secret

Created January 12, 2022 10:44
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plane/8c872ed174ba4f026b95ea8eb934cead to your computer and use it in GitHub Desktop.
Save plane/8c872ed174ba4f026b95ea8eb934cead to your computer and use it in GitHub Desktop.
Automatically insert ) ] or } when you type ] in vim
" searchpairpos() timeout in milliseconds
let s:paren_search_timeout = 50
" set to line("w0") to search to top of screen
" set to 1 to search to top of file
let s:paren_search_top = 1
function FindMatchingParenType()
" taken from Bram Moolenaar's `matchparen.vim`
if !has("syntax") || !exists("g:syntax_on")
let skip = "0"
else
let skip = '!empty(filter(map(synstack(line("."), col(".")), ''synIDattr(v:val, "name")''), ' .
\ '''v:val =~? "string\\|character\\|singlequote\\|escape\\|symbol\\|comment"''))'
try
exec 'if ' . skip . ' | let skip = "0" | endif'
catch /^Vim\%((\a\+)\)\=:E363/
" pattern uses more memory than 'maxmempattern'
return
endtry
endif
let s:parenOfs = {} " using s: here so we can access it in the lambda below
let parens = []
for delims in split(&matchpairs, ',')
let [left, right] = split(delims, ':')
let parens += [right]
let [line, col] = searchpairpos('\M'.left, '', '\M'.right, 'nbW', skip,
\ s:paren_search_top, s:paren_search_timeout)
let ofs = line2byte(line) + col
let s:parenOfs[right] = ofs
endfor
eval parens->sort({lhs, rhs -> s:parenOfs[rhs] - s:parenOfs[lhs]})
return parens[0]
endfunction
inoremap <expr> ] FindMatchingParenType()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment