aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2022-09-12 21:56:35 -0600
committerJosh Rahm <joshuarahm@gmail.com>2022-09-12 21:56:35 -0600
commit0514c88053e83ff5d1bf3c6ba3cfa80565df368e (patch)
tree9392e402da08ee2f0916c2d206aa69e9868a703f
parent2f99407cd2ce0d0ffbe3f62bc31407033c06d7f7 (diff)
downloadfieldmarshal.vim-0514c88053e83ff5d1bf3c6ba3cfa80565df368e.tar.gz
fieldmarshal.vim-0514c88053e83ff5d1bf3c6ba3cfa80565df368e.tar.bz2
fieldmarshal.vim-0514c88053e83ff5d1bf3c6ba3cfa80565df368e.zip
fieldmarshal.vim: Add utility function for modifying motions
-rw-r--r--autoload/fieldmarshal.vim39
-rw-r--r--plugin/casefmt.vim30
-rw-r--r--plugin/charadd.vim43
-rw-r--r--plugin/commenter.vim64
-rw-r--r--plugin/move.vim2
5 files changed, 86 insertions, 92 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
diff --git a/plugin/casefmt.vim b/plugin/casefmt.vim
index 4c66a76..752aecd 100644
--- a/plugin/casefmt.vim
+++ b/plugin/casefmt.vim
@@ -52,13 +52,9 @@ function! CaseFmt_AddFormat(key, funcname) abort
\ . "<cmd>let g:CaseFmtFunction=function(\"\<SID>%s\")<cr>"
\ . "<cmd>set operatorfunc=\<SID>casefmt_do<cr>g@", a:key, a:funcname)
- exec printf("vnoremap <silent> <Plug>(casefmt-leader-no-set)%s "
- \ . "<cmd>let g:CaseFmtFunction=function(\"\<SID>%s\")<cr>"
- \ . "<cmd>call \<SID>casefmt_do(visualmode(), 1)<cr>", a:key, a:funcname)
endfunction
-nmap <silent> <Plug>(casefmt-leader) <cmd>let g:CaseFmtProcessor=function("<SID>casefmt_default_processor")<cr><Plug>(casefmt-leader-no-set)
-vmap <silent> <Plug>(casefmt-leader) <cmd>let g:CaseFmtProcessor=function("<SID>casefmt_default_processor")<cr>gv<Plug>(casefmt-leader-no-set)
+noremap <silent> <Plug>(casefmt-leader) <cmd>let g:CaseFmtProcessor=function("<SID>casefmt_default_processor")<cr><Plug>(casefmt-leader-no-set)
for [k, v] in items(s:case_fmts)
call CaseFmt_AddFormat(k, v)
@@ -69,34 +65,20 @@ if g:casefmt_include_bindings
exec printf("vmap <silent> %s <Plug>(casefmt-leader)", g:casefmt_leader)
endif
-nmap <silent> <Plug>(casefmt-leader)j <cmd>let g:CaseFmtProcessor=function("<SID>casefmt_joiner")<cr><Plug>(casefmt-leader-no-set)
-vmap <silent> <Plug>(casefmt-leader)j <cmd>let g:CaseFmtProcessor=function("<SID>casefmt_joiner")<cr>gv<Plug>(casefmt-leader-no-set)
+noremap <silent> <Plug>(casefmt-leader)j <cmd>let g:CaseFmtProcessor=function("<SID>casefmt_joiner")<cr><Plug>(casefmt-leader-no-set)
function! s:casefmt_do(type, ...) abort
- if a:0
- silent exe "norm! gvy"
- elseif a:type == 'line'
- " yank the text described by the motion
- silent exe "norm! '[V']y"
- else
- silent exe "norm! `[v`]y"
- endif
- let yanked = getreg('"', 1, v:true)
- let yankedtype = getregtype('"')
+ let cb = {}
if !exists('g:CaseFmtProcessor')
let g:CaseFmtProcessor = function("\<SID>casefmt_default_processor")
endif
+ let cb.operate = g:CaseFmtProcessor
- let changed = g:CaseFmtProcessor(yanked, yankedtype)
-
- call setreg('"', changed, yankedtype)
-
- norm gvp
+ call fieldmarshal#modifytext(a:type, cb)
- " Reset the yanked text to what it was originally.
- call setreg('"', yanked, yankedtype)
+ normal! ``
endfunction
" Default processor. Calls change case fmt on each word.
diff --git a/plugin/charadd.vim b/plugin/charadd.vim
index b1d0912..02bd626 100644
--- a/plugin/charadd.vim
+++ b/plugin/charadd.vim
@@ -46,35 +46,20 @@ function! s:set_dir(d, i) abort
endfunction
function! s:charadd(arg, ...) abort
- let save_a = getreg('a')
- let save_a_type = getregtype('a')
+ let cb = {'as_chars': 1}
- if a:0
- echo a:1
- let vis = '`<' . a:arg . '`>'
- elseif a:arg == 'line'
- let vis = "'[V']"
- elseif a:arg == 'block'
- let vis = "`[\<C-v>`]"
- else
- let vis = "`[v`]"
- endif
+ function cb.operate(l, ...)
+ let nl = ""
+ for c in a:l
+ if s:incl || ! (c =~ '\_s')
+ let n = char2nr(c)
+ let nl .= nr2char(n + s:dir)
+ else
+ let nl .= c
+ endif
+ endfor
+ return nl
+ endfunction
- silent! exec printf("norm! %s\"ay", vis)
- let r = getreg('a', 1)
- let rtyp = getregtype('a')
-
- let nl = ""
- for c in r
- if s:incl || ! (c =~ '\_s')
- let n = char2nr(c)
- let nl .= nr2char(n + s:dir)
- else
- let nl .= c
- endif
- endfor
-
- call setreg('a', nl, rtyp)
- silent! exec printf("norm! gv\"ap`[")
- call setreg('a', save_a, save_a_type)
+ call fieldmarshal#modifytext(a:arg, cb)
endfunction
diff --git a/plugin/commenter.vim b/plugin/commenter.vim
index 351e2dc..0681378 100644
--- a/plugin/commenter.vim
+++ b/plugin/commenter.vim
@@ -1,10 +1,11 @@
" Function for commenting out blocks of text.
noremap cd <cmd>set operatorfunc=<sid>comment<cr>g@
-noremap cdd <cmd>set operatorfunc=<sid>comment<cr>g@_
+nnoremap cdd <cmd>set operatorfunc=<sid>comment<cr>g@_
+nnoremap cD <cmd>set operatorfunc=<sid>comment<cr>g@$
-noremap cD <cmd>set operatorfunc=<sid>uncomment<cr>g@
-noremap cDD <cmd>set operatorfunc=<sid>uncomment<cr>g@_
+noremap czd <cmd>set operatorfunc=<sid>uncomment<cr>g@
+nnoremap czdd <cmd>set operatorfunc=<sid>uncomment<cr>g@_
" filetype, linecomment, block start, block continue, blockend, blockpad,
" inline_support
@@ -15,37 +16,17 @@ let s:comment_style = {
\ 'vim': ['"', '"', '"', '"', 0, 0]
\ }
-function! s:modifyreg(arg, cb) abort
- let save_a = getreg('a')
- let save_a_type = getregtype('a')
-
- if a:arg == 'line'
- let vis = "'[V']"
- elseif a:arg == 'block'
- let vis = "`[\<C-v>`]"
- else
- let vis = "`[v`]"
- endif
- silent! exec printf("norm! %s\"ay", vis)
-
- let ls = getreg('a', 1, 1)
- let regtype = getregtype('a')
-
- let ls = a:cb.operate(ls)
-
- call setreg('a', ls, regtype)
- silent! exec "norm! gv\"ap"
- call setreg('a', save_a, save_a_type)
-endfunction
function! s:uncomment(arg, ...) abort
- let [line_com, bstart, bcont, bend, bpad, inl] = get(s:comment_style, &ft, ['#', '#', '#', '#', 0, 0])
+ let [line_com, bstart, bcont, bend, bpad, inl] =
+ \ get(s:comment_style, &ft, ['#', '#', '#', '#', 0, 0])
let l1 = line("'[")
let l2 = line("']")
if inl
- silent exec printf('silent! %s,%s s/\V\zs%s\s\?\ze//', l1, l2, escape(bstart, '/'))
+ silent exec printf(
+ \ 'silent! %s,%s s/\V\zs%s\s\?\ze//', l1, l2, escape(bstart, '/'))
silent exec printf('silent! %s,%s s/\V\zs\s\?%s\ze//', l1, l2, escape(bend, '/'))
endif
@@ -55,16 +36,13 @@ endfunction
function! s:comment(arg, ...) abort
let cb = { 'arg': a:arg }
+ normal! m`
- function! cb.operate(ls) dict abort
+ function! cb.operate(ls, t) dict abort
let [line_com, bstart, bcont, bend, bpad, inl] = get(s:comment_style, &ft, ['#', '#', '#', '#', 0, 0])
let smallest = 100
let ls = a:ls
- if self.arg != 'line' && !inl
- throw "Inline comments not allowed"
- endif
-
if self.arg == 'line' || len(ls) > 1
for l in ls
if ! (l =~ '^\s*$')
@@ -86,20 +64,28 @@ function! s:comment(arg, ...) abort
let i += 1
endwhile
else
+ if self.arg != 'line' && !inl
+ throw "Inline comments not allowed"
+ endif
+
let ls[0] = substitute(ls[0], '^', bstart . ' ', '')
let ls[-1] = substitute(ls[-1], '$', ' ' . bend, '')
endif
-
else
- let i = 0
- while i < len(ls)
- let ls[i] = printf("%s %s %s", bstart, ls[i], bend)
- let i += 1
- endwhile
+ let l = getline('.')
+ if l[len(l) - len(ls[0]):] ==# ls[0]
+ let ls[0] = printf("%s %s", line_com, ls[0])
+ else
+ if !inl
+ throw "Inline comments not allowed"
+ endif
+ let ls[0] = printf("%s %s %s", bstart, ls[0], bend)
+ endif
endif
return ls
endfunction
- call s:modifyreg(a:arg, cb)
+ call fieldmarshal#modifytext(a:arg, cb)
+ norm ``
endfunction
diff --git a/plugin/move.vim b/plugin/move.vim
index 0ed2e7f..8d11d22 100644
--- a/plugin/move.vim
+++ b/plugin/move.vim
@@ -3,3 +3,5 @@ onoremap <silent> i% <cmd>call search('{') <bar> normal! v%<bs>o<space><cr>
vnoremap <silent> a% <esc>V/{<cr>%
vnoremap <silent> i% <esc>/{<cr>v%<bs>o<space>o
+
+onoremap <silent> gv gv