I have an operator function that yanks double quotes or single quotes, ciq works fine because I want to be inside the quotes but when I do yaq (yank a quote) I don't want to move to the place where the quote is at. Seems like an easy task to just get back to the previous cursor position after the yank? I have tried the following things:
- Tried setting the cursor position with an TextYankPost.
- Tried setting marks in the actual mapping, probably not allowed cause it's expecting a motion.
- Tried restoring cursor position from the function with setpos(). Probably don't work cause it's inside of the function.
This does not work maybe cause it's inside the function.
let saveCur=getcurpos()
execute "normal! mz"
call setpos('.',saveCur)
exe "normal! `z"
if a:operator == 'y' | call setpos('.',saveCur) | endif
Not too sure if a call back function would work. Jumps wont work cause exe "normal 12lv"
is not a jump. This is the function. I know it's probably overkill when I could have just done exe normal! ?\%
but I learned a lot of vimscript from it. Any suggestions are welcome. Thanks
function! QuoteTextObject(whole) abort
let char=a:whole ? 'a' : 'i'
let regex= '\v("|' . "').{-}".'("|' . "')"
let res=matchstrpos(strcharpart(getline('.'),col('.')-1),regex)
let backRes=matchstrpos(strcharpart(getline('.'),0),regex)
let [start,stop]=copy(res)->remove(1,2)
let [bstart,bstop]=copy(backRes)->remove(1,2)
let offset=stop-start-1
let boffset=bstop-bstart-1
"if nothing was found forward or backward return
if res == ['',-1,-1] && backRes == ['',-1,-1] |call feedkeys("\e")| return 0 | endif
"find match backwards
if res == ['',-1,-1]
echom 'no match found forward'
execute "normal! 0"
let start=bstart | let offset=boffset
endif
if char == 'a' | execute 'normal! '.start.'lv'.offset.'l' | return 0 |endif
let offset= eval(offset-2) > 0 ? eval(offset-2).'l':''
let icmd='normal! '.eval(start+1).'lv'. offset
if char == 'i' | execute icmd | return 0 |endif
endfunction
onoremap <silent> <Plug>QuoteOperatorWhole :<C-U> call QuoteTextObject(1)<CR>
onoremap <silent> <Plug>QuoteOperatorInner :<C-U>call QuoteTextObject(0)<CR>
omap iq <Plug>QuoteOperatorInner
omap aq <Plug>QuoteOperatorWhole
winsaveview()
andwinrestview()
for state save/restore.