From ccccf48d6ed58dcf19b2f10db644e24c78cf5508 Mon Sep 17 00:00:00 2001 From: "Andy K. Massimino" Date: Mon, 27 Nov 2017 20:10:54 -0500 Subject: [PATCH] Allow forcing for % and g% --- autoload/matchup.vim | 6 ++-- autoload/matchup/delim.vim | 8 +++++- autoload/matchup/motion.vim | 57 +++++++++++++++++++------------------ 3 files changed, 41 insertions(+), 30 deletions(-) diff --git a/autoload/matchup.vim b/autoload/matchup.vim index e6a95ba..b3498df 100644 --- a/autoload/matchup.vim +++ b/autoload/matchup.vim @@ -90,8 +90,6 @@ function! s:init_default_mappings() call s:map('x', '%', '(matchup-%)' ) call s:map('x', 'g%', '(matchup-g%)') - call s:map('o', '%', '(matchup-%)' ) - call s:map('o', 'g%', '(matchup-g%)') call s:map('n', ']%', '(matchup-]%)') call s:map('n', '[%', '(matchup-[%)') @@ -103,6 +101,10 @@ function! s:init_default_mappings() call s:map('x', 'z%', '(matchup-z%)') for l:opforce in ['', 'v', 'V', ''] + call s:map('o', l:opforce.'%', + \ '(matchup-o_'.l:opforce.')(matchup-%)') + call s:map('o', l:opforce.'g%', + \ '(matchup-o_'.l:opforce.')(matchup-g%)') call s:map('o', l:opforce.']%', \ '(matchup-o_'.l:opforce.')(matchup-]%)') call s:map('o', l:opforce.'[%', diff --git a/autoload/matchup/delim.vim b/autoload/matchup/delim.vim index 9746083..6a03511 100644 --- a/autoload/matchup/delim.vim +++ b/autoload/matchup/delim.vim @@ -233,7 +233,8 @@ function! matchup#delim#get_surrounding(type, ...) " {{{1 endfunction " }}}1 -function! matchup#delim#jump_target(delim) "{{{1 + +function! matchup#delim#jump_target(delim) " {{{1 let l:save_pos = matchup#pos#get_cursor() " unicode note: technically wrong, but works in practice @@ -260,6 +261,11 @@ function! matchup#delim#jump_target(delim) "{{{1 return l:column endfunction +" }}}1 +function! matchup#delim#end_offset(delim) " {{{1 + return max([0, match(a:delim.match, '.$')]) +endfunction + " }}}1 function! s:get_delim(opts) " {{{1 diff --git a/autoload/matchup/motion.vim b/autoload/matchup/motion.vim index 6144122..ee5a2ef 100644 --- a/autoload/matchup/motion.vim +++ b/autoload/matchup/motion.vim @@ -35,14 +35,14 @@ function! matchup#motion#init_module() " {{{1 \ :call matchup#motion#find_matching_pair(1, 1) xmap (matchup-%) (matchup-%) onoremap (matchup-%) - \ :call oper("normal \(v)" + \ :call oper("normal \(wise)" \ . (v:count > 0 ? v:count : '') . "\(matchup-%)") xnoremap (matchup-g%) \ :call matchup#motion#find_matching_pair(1, 0) xmap (matchup-g%) (matchup-g%) onoremap (matchup-g%) - \ :call oper("normal \(v)" + \ :call oper("normal \(wise)" \ . (v:count > 0 ? v:count : '') . "\(matchup-g%)") " ]% and [% @@ -64,7 +64,7 @@ function! matchup#motion#init_module() " {{{1 \ :call oper("normal \(wise)" \ . v:count1 . "\(matchup-[%)") - " jump inside z% + " jump inside z% nnoremap (matchup-z%) \ :call matchup#motion#jump_inside(0) xnoremap (matchup-z%) @@ -95,8 +95,6 @@ function! matchup#motion#find_matching_pair(visual, down) " {{{1 exe 'normal!' l:count.'%' return endif - - let [l:start_lnum, l:start_cnum] = matchup#pos#get_cursor()[1:2] " disable the timeout call matchup#perf#timeout_start(0) @@ -117,32 +115,37 @@ function! matchup#motion#find_matching_pair(visual, down) " {{{1 if empty(l:delim) | return | endif endfor + let l:is_oper = !empty(get(s:, 'v_operator', '')) + let l:exclusive = l:is_oper && (g:v_motion_force ==# 'v') + let l:forward = ((a:down && l:delim.side !=# 'open') + \ || l:delim.side ==# 'close') + " go to the end of the delimiter, if necessary let l:column = l:delim.cnum - if g:matchup_motion_cursor_end - \ && ((a:down && l:delim.side !=# 'open') - \ || l:delim.side ==# 'close') - - " XXX spin this off into delim object - " let l:column += strdisplaywidth(l:delim.match) - 1 - - " XXX + if g:matchup_motion_cursor_end && !l:is_oper && l:forward let l:column = matchup#delim#jump_target(l:delim) - - " TODO - " let l:test = matchup#delim#get_current('all', 'both_all') - " echo l:test.rematch l:delim.rematch - " if !empty(l:test) && l:test.rematch !=# l:delim.rematch - " let l:column -= 1 - " endif endif + let l:start_pos = matchup#pos#get_cursor() + normal! m` - - " XXX spin off - let l:eom = l:delim.cnum + strlen(l:delim.match) - 1 + + " column position of last character in match + let l:eom = l:delim.cnum + matchup#delim#end_offset(l:delim) + + if l:is_oper && l:forward + let l:column = l:exclusive ? (l:column - 1) : l:eom + endif + + if l:is_oper && l:exclusive + \ && matchup#pos#smaller(l:delim, l:start_pos) + normal! o + call matchup#pos#set_cursor(matchup#pos#prev(l:start_pos)) + normal! o + endif " special handling for d% + let [l:start_lnum, l:start_cnum] = l:start_pos[1:2] if get(s:, 'v_operator', '') ==# 'd' && l:start_lnum != l:delim.lnum let l:tl = [l:start_lnum, l:start_cnum] let l:br = [l:delim.lnum, l:eom] @@ -167,7 +170,6 @@ function! matchup#motion#find_matching_pair(visual, down) " {{{1 endif call matchup#pos#set_cursor(l:delim.lnum, l:column) - endfunction " }}}1 @@ -198,7 +200,7 @@ function! matchup#motion#find_unmatched(visual, down) " {{{1 let l:save_pos = matchup#pos#get_cursor() let l:new_pos = [l:delim.lnum, l:delim.cnum] - + " this is an exclusive motion, ]% if l:delim.side ==# 'close' if l:exclusive @@ -207,7 +209,7 @@ function! matchup#motion#find_unmatched(visual, down) " {{{1 "XXX spin this off let l:new_pos[1] += strdisplaywidth(l:delim.match) - 1 endif - endif + endif " if the cursor didn't move, increment count if matchup#pos#equal(l:save_pos, l:new_pos) @@ -259,6 +261,7 @@ function! matchup#motion#jump_inside(visual) " {{{1 let l:new_pos = [l:delim.lnum, l:delim.cnum] " XXX spin this off + " XXX very wrong for unicode let l:new_pos[1] += strdisplaywidth(l:delim.match) - 1 call matchup#pos#set_cursor(matchup#pos#next(l:new_pos)) @@ -276,7 +279,7 @@ function! matchup#motion#jump_inside(visual) " {{{1 let l:new_pos = matchup#pos#next(l:new_pos) endwhile let l:new_pos = matchup#pos#prev(l:new_pos) - endif + endif normal! m` call matchup#pos#set_cursor(l:new_pos)