Push surround feature (initially disabled)

let g:matchup_surround_enabled = 1
    cs% / ds%
This commit is contained in:
Andy K. Massimino
2018-08-18 13:46:26 -04:00
parent 10c5daa32f
commit 3262eecc3c
2 changed files with 102 additions and 0 deletions

View File

@@ -52,6 +52,8 @@ function! s:init_options()
call s:init_option('matchup_mouse_enabled', 1)
call s:init_option('matchup_surround_enabled', 0)
call s:init_option('matchup_matchpref', {})
endfunction
@@ -75,6 +77,7 @@ function! s:init_modules()
call s:motion_init_module()
call s:text_obj_init_module()
call s:misc_init_module()
call s:surround_init_module()
endfunction
let g:v_motion_force = ''
@@ -146,6 +149,11 @@ function! s:init_default_mappings()
if get(g:, 'matchup_mouse_enabled', 1)
call s:map('n', '<2-LeftMouse>', '<plug>(matchup-double-click)')
endif
if get(g:, 'matchup_surround_enabled', 0)
call s:map('n', 'ds%', '<plug>(matchup-ds%)')
call s:map('n', 'cs%', '<plug>(matchup-cs%)')
endif
endfunction
" module initialization
@@ -253,6 +261,25 @@ function! s:misc_init_module() " {{{1
call matchup#perf#toc('loading_module', 'misc')
endfunction
" }}}1
function! s:surround_init_module() " {{{1
if !g:matchup_surround_enabled | return | endif
call matchup#perf#tic('loading_module')
for [l:map, l:name, l:opt] in [
\ ['%', 'delimited', 'delim_all'],
\]
let l:p1 = 'noremap <silent> <plug>(matchup-'
let l:p2 = l:map . ') :<c-u>call matchup#surround#' . l:name
let l:p3 = empty(l:opt) ? ')<cr>' : ', ''' . l:opt . ''')<cr>'
execute 'n' . l:p1 . 'ds' . l:p2 . '(0, "d"' . l:p3
execute 'n' . l:p1 . 'cs' . l:p2 . '(0, "c"' . l:p3
endfor
call matchup#perf#toc('loading_module', 'surround')
endfunction
" }}}1
let &cpo = s:save_cpo

View File

@@ -0,0 +1,75 @@
" vim match-up - matchit replacement and more
"
" Maintainer: Andy Massimino
" Email: a@normed.space
"
let s:save_cpo = &cpo
set cpo&vim
function! matchup#surround#delimited(is_cap, op, type) " {{{1
call matchup#perf#timeout_start(1000)
let [l:open, l:close] = matchup#delim#get_surrounding(
\ a:type, v:count, { 'local': 0 })
if empty(l:open) || empty(l:close)
return
endif
if a:op ==# 'c'
let l:char = nr2char(getchar())
if index(["\<esc>","\<c-c>"], l:char) >= 0
return
endif
endif
let l:tpope = !empty(maparg('<plug>VSurround', 'x'))
let [l:l1, l:c11, l:c12] = [l:open.lnum, l:open.cnum,
\ l:open.cnum + strlen(l:open.match) - 1]
let [l:l2, l:c21, l:c22] = [l:close.lnum, l:close.cnum,
\ l:close.cnum + strlen(l:close.match) - 1]
if a:op ==# 'd' || a:op ==# 'c'
call matchup#pos#set_cursor(l1, c12+1)
let l:line = getline(l:l2)
call setline(l:l2, strpart(l:line, 0, l:c21-1)
\ . strpart(l:line, l:c22))
let l:regtext = strpart(l:line, l:c21-1, l:c22-l:c21+1)
let l:ins = a:op ==# 'c' && !l:tpope ? l:char : ''
let l:line = getline(l:l1)
call setline(l:l1, strpart(l:line, 0, l:c11-1)
\ . l:ins . strpart(l:line, l:c12))
call setreg(v:register, strpart(l:line, l:c11-1, l:c12-l:c11+1)
\ . ' ' . l:regtext)
let l:epos = l:c21-1 - (l:l1 == l:l2
\ ? (l:c12-l:c11+1-strlen(l:ins)) : 0)
call setpos("']", [0, l:l2, l:epos, 0])
call setpos("'[", [0, l:l1, l:c11, 0])
endif
if a:op ==# 'd' || a:op ==# 'c' && empty(l:char)
silent! call repeat#set("\<plug>(matchup-ds%)", v:count)
elseif a:op ==# 'c' && l:tpope
normal! `[v`]
undojoin
execute "normal \<plug>VSurround".l:char
silent! call repeat#set("\<plug>(matchup-cs%)"
\ . matchstr(g:repeat_sequence, 'SSurroundRepeat\zs.\+'),
\ v:count)
elseif a:op ==# 'c'
call feedkeys('`]a', 'tn')
endif
call matchup#pos#set_cursor(l1, c11)
endfunction
" }}}1
let &cpo = s:save_cpo
" vim: fdm=marker sw=2