diff options
author | Josh Rahm <joshuarahm@gmail.com> | 2022-09-12 21:56:35 -0600 |
---|---|---|
committer | Josh Rahm <joshuarahm@gmail.com> | 2022-09-12 21:56:35 -0600 |
commit | 0514c88053e83ff5d1bf3c6ba3cfa80565df368e (patch) | |
tree | 9392e402da08ee2f0916c2d206aa69e9868a703f /autoload | |
parent | 2f99407cd2ce0d0ffbe3f62bc31407033c06d7f7 (diff) | |
download | fieldmarshal.vim-0514c88053e83ff5d1bf3c6ba3cfa80565df368e.tar.gz fieldmarshal.vim-0514c88053e83ff5d1bf3c6ba3cfa80565df368e.tar.bz2 fieldmarshal.vim-0514c88053e83ff5d1bf3c6ba3cfa80565df368e.zip |
fieldmarshal.vim: Add utility function for modifying motions
Diffstat (limited to 'autoload')
-rw-r--r-- | autoload/fieldmarshal.vim | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/autoload/fieldmarshal.vim b/autoload/fieldmarshal.vim new file mode 100644 index 0000000..cba0fed --- /dev/null +++ b/autoload/fieldmarshal.vim @@ -0,0 +1,39 @@ + +" Modify the text described by the '[ and '] marks. +" +" @param cb the callback used to modify the text should have the following +" function: +" +" {operate} - takes a list and returns the modified list. +" +" +" This function does make temporary use of the @a register, and thus the operate +" function should not expect changes to that register to persist. +function! fieldmarshal#modifytext(type, cb) abort + let save_a = getreg('a') + let save_a_type = getregtype('a') + + " Yank the contents of the described text object into the "a register. + if a:type == 'line' + let vis = "'[V']" + elseif a:type == 'block' + let vis = "`[\<C-v>`]" + else + let vis = "`[v`]" + endif + silent! exec printf("norm! %s\"ay", vis) + + " Get the a register, + let ls = getreg('a', 1, !get(a:cb, 'as_chars', 0)) + let regtype = getregtype('a') + + let ls = a:cb.operate(ls, regtype) + + " Set the register list to whatever was returned from operate(). Then + " re-VIsualize the text object and paste. + call setreg('a', ls, regtype) + silent! exec "norm! gv\"ap" + + " Restore the register "a to what it was before. + call setreg('a', save_a, save_a_type) +endfunction |