From 710a7b0a78d558594434aa78a6c8c46e6660f836 Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Tue, 30 Aug 2022 20:49:24 -0600 Subject: joiner.vim: add joiner.vim; verbs to join text. --- autoload/joiner.vim | 38 +++++++++++++++++++++++++ plugin/joiner.vim | 82 ++++++++++++++++------------------------------------- 2 files changed, 62 insertions(+), 58 deletions(-) create mode 100644 autoload/joiner.vim diff --git a/autoload/joiner.vim b/autoload/joiner.vim new file mode 100644 index 0000000..6f59282 --- /dev/null +++ b/autoload/joiner.vim @@ -0,0 +1,38 @@ + +" Function for the operatorfunc with joiner. +function! joiner#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 changed = [g:JoinerJoinText(yanked, yankedtype)] + + call setreg('"', changed, yankedtype) + + norm gvp + + " Reset the yanked text to what it was originally. + call setreg('"', yanked, yankedtype) +endfunction + +function! joiner#join_with_string(yanked, yanktype) abort + let join_string = input('> ') + return join(map(a:yanked, "substitute(v:val, '^\\s\\+', '', '')"), join_string) +endfunction + +function! joiner#join_with_string_literal(yanked, yanktype) abort + let join_string = input('> ') + return join(a:yanked, join_string) +endfunction + +function! joiner#default_join(yanked, yanktype) abort + return join(map(a:yanked, "substitute(v:val, '^\\s\\+', '', '')"), ' ') +endfunction diff --git a/plugin/joiner.vim b/plugin/joiner.vim index 929a326..cef2fda 100644 --- a/plugin/joiner.vim +++ b/plugin/joiner.vim @@ -1,70 +1,36 @@ " Joiner. Ability to join lines in a text object. +" +" Joiner provides several bindings, with the leader being configurable. This +" documentation assums as the leader. +" +" s: join the lines in with . +" The lines are stripped of whitesapce before being +" joined +" +" S: join the lines in with . +" The lines are joined verbatim. +" +" : join the lines in . The lines are stripped +" before being joined. +" +" There are Visual counterparts to these. if ! exists('g:joiner_include_bindings') let g:joiner_include_bindings = 1 endif if ! exists('g:joiner_leader') - let g:joiner_leader = '+' + let g:joiner_leader = '' endif -function! s:join_text_literal(yanked, yankedtype) abort - if !exists('g:joiner_join_text') - let g:joiner_join_text = ' ' - endif - return join(a:yanked, g:joiner_join_text) -endfunction +noremap (joiner)s :let g:JoinerJoinText=function('joiner#join_with_string'):set operatorfunc=joiner#dog@ +noremap (joiner)S :let g:JoinerJoinText=function('joiner#join_with_string_literal'):set operatorfunc=joiner#dog@ +noremap (joiner) :let g:JoinerJoinText=function('joiner#default_join'):set operatorfunc=joiner#dog@ -function! s:join_text_collapse(yanked, yankedtype) abort - let changed = [] - if !exists('g:joiner_join_text') - let g:joiner_join_text = ' ' - endif - for y in a:yanked - call add(changed, substitute(y, '^\s\+', '', 'g')) - endfor - return join(changed, g:joiner_join_text) -endfunction +vnoremap (joiner)s :let g:JoinerJoinText=function('joiner#join_with_string')gv:call joiner#do(visualmode(), 1) +vnoremap (joiner)S :let g:JoinerJoinText=function('joiner#join_with_string_literal')gv:call joiner#do(visualmode(), 1) +vnoremap (joiner) :let g:JoinerJoinText=function('joiner#default_join')gv:call joiner#do(visualmode(), 1) -function! s:joiner_do(type, ...) abort - if a:0 - psilent 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('"') - - if !exists('g:joiner_join_text') - let g:joiner_join_text = ' ' - endif - - let changed = [g:JoinerJoinText(yanked, yankedtype)] - - call setreg('"', changed, yankedtype) - - norm gvp - - " Reset the yanked text to what it was originally. - call setreg('"', yanked, yankedtype) -endfunction - -noremap (joiner-leader) - \ :let g:JoinerJoinText=function("join_text_collapse") - \:let g:joiner_join_text=' ' - \:set operatorfunc=joiner_do - \g@ - -vnoremap (joiner-leader) - \ :let g:JoinerJoinText=function("join_text_collapse") - \:let g:joiner_join_text=' ' - \gv:call joiner_do(visualmode(), 1) - -if g:joiner_include_bindings - exec printf('nmap %s (joiner-leader)', g:joiner_leader) - exec printf('vmap %s (joiner-leader)', g:joiner_leader) +if g:joiner_include_bindings exec printf("vnoremap %s (joiner)", g:joiner_leader) + exec printf("noremap %s (joiner)", g:joiner_leader) endif -- cgit