Skip to content

Commit

Permalink
Add a new choice, 3, for linewise option
Browse files Browse the repository at this point in the history
  • Loading branch information
machakann committed Sep 20, 2019
1 parent 4640063 commit de1cadd
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 25 deletions.
65 changes: 45 additions & 20 deletions autoload/operator/sandwich/act.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 \<Plug>(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 \<Plug>(sandwich-i)"
let linewise = 0
else
let startinsert = a:opt.of('noremap') ? 'normal! i' : "normal \<Plug>(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 \<Plug>(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 \<Plug>(sandwich-i)"
let linewise = 0
else
let startinsert = a:opt.of('noremap') ? 'normal! i' : "normal \<Plug>(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 "{{{
Expand Down Expand Up @@ -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
Expand All @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down
6 changes: 4 additions & 2 deletions autoload/operator/sandwich/operator.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion autoload/sandwich/highlight.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion doc/operator-sandwich.jax
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
*operator-sandwich.jax* 『挟まれた』テキストを編集する。
Last change:02-Mar-2019.
Last change:18-Sep-2019.

書いた人 : machakann <mckn{at}outlook.jp>
ライセンス : NYSL license
Expand Down Expand Up @@ -1068,6 +1068,15 @@ linewise
" foo ---> (
foo
)
<
このオプションを 3 に設定した場合、括弧を前の行の最後尾と次の行の最初
にそれぞれ追加します。行指向の場合に設定するのが有用でしょう。
>
call operator#sandwich#set('add', 'line', 'linewise', 3)
" Press jVsa(
" foo ---> foo(
bar bar
baz )baz
<
文字列を削除する場合は、文字列削除後にその行に空白文字以外に残っていな
ければ行ごと削除します。
Expand Down
12 changes: 11 additions & 1 deletion doc/operator-sandwich.txt
Original file line number Diff line number Diff line change
@@ -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 <mckn{at}outlook.jp>
License : NYSL license
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit de1cadd

Please sign in to comment.