From de1cadd40f8f969fa7f518a633b899a07d63a4f7 Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 20 Sep 2019 19:13:35 +0800 Subject: [PATCH] Add a new choice, 3, for linewise option #81 --- autoload/operator/sandwich/act.vim | 65 +++++++++++++++++-------- autoload/operator/sandwich/operator.vim | 6 ++- autoload/sandwich/highlight.vim | 2 +- doc/operator-sandwich.jax | 11 ++++- doc/operator-sandwich.txt | 12 ++++- 5 files changed, 71 insertions(+), 25 deletions(-) diff --git a/autoload/operator/sandwich/act.vim b/autoload/operator/sandwich/act.vim index 50e8678..165e33b 100644 --- a/autoload/operator/sandwich/act.vim +++ b/autoload/operator/sandwich/act.vim @@ -284,7 +284,8 @@ function! s:Indent(pos, opt) abort "{{{ let indent.linehead = s:TRUE endif - if a:opt.of('linewise') && a:opt.of('autoindent') == 4 + let opt_linewise = a:opt.of('linewise') + if opt_linewise > 0 && a:opt.of('autoindent') == 4 let indent.savedstr = indent.str endif return indent @@ -383,32 +384,54 @@ endfunction "}}} function! s:add_former(buns, pos, opt, ...) abort "{{{ let undojoin_cmd = get(a:000, 0, 0) ? 'undojoin | ' : '' - let indent = s:Indent(a:pos, a:opt) - let opt_linewise = a:opt.of('linewise') - if opt_linewise + let pos = copy(a:pos) + let linewise = a:opt.of('linewise') + if linewise == 1 || linewise == 2 || (linewise == 3 && pos[1] == 1) let startinsert = a:opt.of('noremap') ? 'normal! O' : "normal \(sandwich-O)" - let insertion = indent.savedstr . a:buns[0] + let linewise = 1 + elseif linewise == 3 + let prevline = pos[1] - 1 + let pos = [0, prevline, col([prevline, '$']), 0] + let startinsert = a:opt.of('noremap') ? 'normal! i' : "normal \(sandwich-i)" + let linewise = 0 else let startinsert = a:opt.of('noremap') ? 'normal! i' : "normal \(sandwich-i)" + let linewise = 0 + endif + let indent = s:Indent(pos, a:opt) + if linewise + let insertion = indent.savedstr . a:buns[0] + else let insertion = a:buns[0] endif - call s:add_portion(insertion, a:pos, undojoin_cmd, startinsert) - return [opt_linewise, indent.diff(a:buns[0]), getpos("'["), getpos("']")] + call s:add_portion(insertion, pos, undojoin_cmd, startinsert) + return [linewise, indent.diff(a:buns[0]), getpos("'["), getpos("']")] endfunction "}}} function! s:add_latter(buns, pos, opt) abort "{{{ let undojoin_cmd = '' - let indent = s:Indent(a:pos, a:opt) - let opt_linewise = a:opt.of('linewise') - if opt_linewise + let pos = copy(a:pos) + let linewise = a:opt.of('linewise') + if linewise == 1 || linewise == 2 || (linewise == 3 && pos[1] == line('$')) let startinsert = a:opt.of('noremap') ? 'normal! o' : "normal \(sandwich-o)" - let insertion = indent.savedstr . a:buns[1] + let linewise = 1 + elseif linewise == 3 + let nextline = pos[1] + 1 + let pos = [0, nextline, indent(nextline) + 1, 0] + let startinsert = a:opt.of('noremap') ? 'normal! i' : "normal \(sandwich-i)" + let linewise = 0 else let startinsert = a:opt.of('noremap') ? 'normal! i' : "normal \(sandwich-i)" + let linewise = 0 + endif + let indent = s:Indent(pos, a:opt) + if linewise + let insertion = indent.savedstr . a:buns[1] + else let insertion = a:buns[1] endif - call s:add_portion(insertion, a:pos, undojoin_cmd, startinsert) - return [opt_linewise, indent.diff(a:buns[1]), getpos("'["), getpos("']")] + call s:add_portion(insertion, pos, undojoin_cmd, startinsert) + return [linewise, indent.diff(a:buns[1]), getpos("'["), getpos("']")] endfunction "}}} function! s:add_portion(bun, pos, undojoin_cmd, startinsert) abort "{{{ @@ -633,19 +656,19 @@ function! s:shift_for_replace(shifted_pos, target, addition, deletion, indent, i endif else call s:pull2(a:shifted_pos, a:target, a:deletion, a:is_linewise) - if a:is_linewise[1] + if a:is_linewise[1] == 1 || a:is_linewise[1] == 2 let a:target.head2[1] -= 1 endif call s:push2(a:shifted_pos, a:target, a:addition, a:indent, a:is_linewise) - if a:is_linewise[1] + if a:is_linewise[1] == 1 || a:is_linewise[1] == 2 let a:target.head2[1] += 1 endif call s:pull1(a:shifted_pos, a:target, a:deletion, a:is_linewise) - if a:is_linewise[0] + if a:is_linewise[0] == 1 || a:is_linewise[0] == 2 let a:target.head1[1] -= 1 endif call s:push1(a:shifted_pos, a:target, a:addition, a:indent, a:is_linewise) - if a:is_linewise[0] + if a:is_linewise[0] == 1 || a:is_linewise[0] == 2 let a:target.head1[1] += 1 endif endif @@ -669,7 +692,9 @@ function! s:push1(shifted_pos, target, addition, indent, is_linewise) abort "{{ let shift[1] += 1 endif - if s:is_equal_or_ahead(a:shifted_pos, head) || (a:is_linewise[0] && a:shifted_pos[1] == head[1]) + if s:is_equal_or_ahead(a:shifted_pos, head) + call s:push(shift, a:shifted_pos, head, a:addition[0], a:indent[0], a:is_linewise[0]) + elseif a:is_linewise[0] && a:shifted_pos[1] == head[1] call s:push(shift, a:shifted_pos, head, a:addition[0], a:indent[0], a:is_linewise[0]) endif let a:shifted_pos[1:2] += shift[1:2] @@ -737,7 +762,7 @@ function! s:pull1(shifted_pos, target, deletion, is_linewise) abort "{{{ let a:shifted_pos[1] += shift[1] " the case for linewise action - if a:is_linewise[0] + if a:is_linewise[0] == 1 || a:is_linewise[0] == 2 if a:shifted_pos[1] == head[1] " col let a:shifted_pos[2] = 0 @@ -787,7 +812,7 @@ function! s:pull2(shifted_pos, target, deletion, is_linewise) abort "{{{ let a:shifted_pos[1:2] += shift[1:2] " the case for linewise action - if a:is_linewise[1] + if a:is_linewise[1] == 1 || a:is_linewise[1] == 2 if a:shifted_pos[1] == head[1] " col let a:shifted_pos[2] = s:constants('colmax') diff --git a/autoload/operator/sandwich/operator.vim b/autoload/operator/sandwich/operator.vim index a60e468..10d5222 100644 --- a/autoload/operator/sandwich/operator.vim +++ b/autoload/operator/sandwich/operator.vim @@ -249,7 +249,8 @@ function! s:operator.add_once(i, recipe) dict abort "{{{ let buns = s:get_buns(a:recipe, self.opt.of('expr'), self.opt.of('listexpr')) let undojoin = a:i == 0 || self.state == 0 ? 0 : 1 let modified = 0 - if buns[0] !=# '' || buns[1] !=# '' || self.opt.of('linewise') + let opt_linewise = self.opt.of('linewise') + if buns[0] !=# '' || buns[1] !=# '' || opt_linewise == 1 || opt_linewise == 2 for j in range(self.n) let stuff = self.basket[j] if stuff.active @@ -398,7 +399,8 @@ function! s:operator.skip_space(i) dict abort "{{{ endfor " for cursor positions - if !opt.of('linewise') + let opt_linewise = opt.of('linewise') + if !(opt_linewise == 1 || opt_linewise == 2) let top_stuff = self.basket[self.n-1] let bot_stuff = self.basket[0] let self.cursor.inner_head = deepcopy(top_stuff.edges.head) diff --git a/autoload/sandwich/highlight.vim b/autoload/sandwich/highlight.vim index f8a2b1f..8e522b8 100644 --- a/autoload/sandwich/highlight.vim +++ b/autoload/sandwich/highlight.vim @@ -62,7 +62,7 @@ function! s:highlight.order(target, linewise) dict abort "{{{ let order_list = [] for [head, tail, linewise] in [[a:target.head1, a:target.tail1, a:linewise[0]], \ [a:target.head2, a:target.tail2, a:linewise[1]]] - if linewise + if linewise == 1 || linewise == 2 call s:highlight_order_linewise(order_list, order, head, tail) else call s:highlight_order_charwise(order_list, order, head, tail) diff --git a/doc/operator-sandwich.jax b/doc/operator-sandwich.jax index 2a5d2cd..9c79d99 100644 --- a/doc/operator-sandwich.jax +++ b/doc/operator-sandwich.jax @@ -1,5 +1,5 @@ *operator-sandwich.jax* 『挟まれた』テキストを編集する。 - Last change:02-Mar-2019. + Last change:18-Sep-2019. 書いた人 : machakann ライセンス : NYSL license @@ -1068,6 +1068,15 @@ linewise " foo ---> ( foo ) +< + このオプションを 3 に設定した場合、括弧を前の行の最後尾と次の行の最初 + にそれぞれ追加します。行指向の場合に設定するのが有用でしょう。 +> + call operator#sandwich#set('add', 'line', 'linewise', 3) + " Press jVsa( + " foo ---> foo( + bar bar + baz )baz < 文字列を削除する場合は、文字列削除後にその行に空白文字以外に残っていな ければ行ごと削除します。 diff --git a/doc/operator-sandwich.txt b/doc/operator-sandwich.txt index a881b78..af1d949 100644 --- a/doc/operator-sandwich.txt +++ b/doc/operator-sandwich.txt @@ -1,5 +1,5 @@ *operator-sandwich.txt* The operator plugin to edit sandwiched textobjects. - Last change:02-Mar-2019. + Last change:18-Sep-2019. Author : machakann License : NYSL license @@ -1082,6 +1082,16 @@ linewise " foo ---> ( foo ) +< + If this option is 3, add parentheses at the end of the previous line + and at the beginning of the next line. Typically, setting the option + to 3 in line-wise operation would be useful. +> + call operator#sandwich#set('add', 'line', 'linewise', 3) + " Press jVsa( + " foo ---> foo( + bar bar + baz )baz < For the case of deleting, if there are nothing other than white spaces and breaking after deleting, delete the lines.