Skip to content

Commit

Permalink
Set up repeat with the last-inserted text
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewRadev committed Apr 24, 2021
1 parent aea74df commit 2a86162
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions autoload/sideways/new_item.vim
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ function! sideways#new_item#Add(mode)

if a:mode == 'i'
call s:InsertBefore(current, delimiter_string, new_line)
call s:SetupRepeat("\<Plug>SidewaysArgumentInsertBefore")
call s:SetRepeatAutocommand('SidewaysArgumentInsertBefore')
elseif a:mode == 'a'
call s:InsertAfter(current, delimiter_string, new_line)
call s:SetupRepeat("\<Plug>SidewaysArgumentAppendAfter")
call s:SetRepeatAutocommand('SidewaysArgumentAppendAfter')
endif
endfunction

Expand All @@ -31,7 +31,7 @@ function! sideways#new_item#AddFirst()

let first_item = items[0]
call s:InsertBefore(first_item, delimiter_string, new_line)
call s:SetupRepeat("\<Plug>SidewaysArgumentInsertFirst")
call s:SetRepeatAutocommand('SidewaysArgumentInsertFirst')
endfunction

function! sideways#new_item#AddLast()
Expand All @@ -45,13 +45,20 @@ function! sideways#new_item#AddLast()

let last_item = items[len(items) - 1]
call s:InsertAfter(last_item, delimiter_string, new_line)
call s:SetupRepeat("\<Plug>SidewaysArgumentAppendLast")
call s:SetRepeatAutocommand('SidewaysArgumentAppendLast')
endfunction

function! sideways#new_item#Repeat(action, text)
let b:sideways_repeat_new_item = a:text
exe "normal \<Plug>".a:action
unlet b:sideways_repeat_new_item
endfunction

function! s:InsertBefore(item, delimiter_string, new_line)
let item = a:item
let delimiter_string = a:delimiter_string
let new_line = a:new_line
let repeat_text = get(b:, 'sideways_repeat_new_item', '')

if g:sideways_add_item_cursor_restore && has('textprop')
call s:StorePosition()
Expand Down Expand Up @@ -81,13 +88,14 @@ function! s:InsertBefore(item, delimiter_string, new_line)
call s:SetupRestorePosition()
endif

call feedkeys('i'.whitespace, 'n')
call feedkeys('i' . whitespace . repeat_text, 'n')
endfunction

function! s:InsertAfter(item, delimiter_string, new_line)
let item = a:item
let delimiter_string = a:delimiter_string
let new_line = a:new_line
let repeat_text = get(b:, 'sideways_repeat_new_item', '')

if g:sideways_add_item_cursor_restore && has('textprop')
call s:StorePosition()
Expand All @@ -99,13 +107,13 @@ function! s:InsertAfter(item, delimiter_string, new_line)
exe "normal! a".sideways#util#Trim(delimiter_string)."\<cr>\<esc>"

if getline('.') == ''
call feedkeys('cc', 'n')
call feedkeys('cc' . repeat_text, 'n')
else
call feedkeys('I', 'n')
call feedkeys('I' . repeat_text, 'n')
endif
else
exe 'normal! a'.delimiter_string
call feedkeys('a', 'n')
call feedkeys('a' . repeat_text, 'n')
endif

if g:sideways_add_item_cursor_restore && has('textprop')
Expand Down Expand Up @@ -192,7 +200,7 @@ function! s:ClearSavedPosition()
call prop_remove({'type': 'sideways_saved_position', 'all': 1})
endfunction

function! s:SetupRepeat(mapping)
function! s:SetRepeatAutocommand(action)
if !g:sideways_add_item_repeat
return
endif
Expand All @@ -202,5 +210,12 @@ function! s:SetupRepeat(mapping)
return
endif

exe 'autocmd InsertLeavePre <buffer> ++once silent! call repeat#set("' . a:mapping . '")'
exe 'autocmd InsertLeavePre <buffer> ++once call s:SetRepeatInvocation("'.a:action.'")'
endfunction

function! s:SetRepeatInvocation(action)
let last_insert = @.
let repeat_invocation = ":call sideways#new_item#Repeat(\"" . a:action . "\", \"". escape(last_insert, '"\') . "\")\<cr>"

silent! call repeat#set(repeat_invocation)
endfunction

0 comments on commit 2a86162

Please sign in to comment.