diff options
-rw-r--r-- | autoload/joiner.vim | 38 | ||||
-rw-r--r-- | plugin/joiner.vim | 82 |
2 files changed, 62 insertions, 58 deletions
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 <C-j> as the leader. +" +" <C-j>s<text_object><string>: join the lines in <text_object> with <string>. +" The lines are stripped of whitesapce before being +" joined +" +" <C-j>S<text_object><string>: join the lines in <text_object> with <string>. +" The lines are joined verbatim. +" +" <C-j><text_object>: join the lines in <text_object>. 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 = '<leader>+' + let g:joiner_leader = '<C-j>' 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 <silent> <Plug>(joiner)s :<c-u>let g:JoinerJoinText=function('joiner#join_with_string')<cr>:<c-u>set operatorfunc=joiner#do<cr>g@ +noremap <silent> <Plug>(joiner)S :<c-u>let g:JoinerJoinText=function('joiner#join_with_string_literal')<cr>:<c-u>set operatorfunc=joiner#do<cr>g@ +noremap <silent> <Plug>(joiner) :<c-u>let g:JoinerJoinText=function('joiner#default_join')<cr>:<c-u>set operatorfunc=joiner#do<cr>g@ -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 <silent> <Plug>(joiner)s :<c-u>let g:JoinerJoinText=function('joiner#join_with_string')<cr>gv:<c-u>call joiner#do(visualmode(), 1)<cr> +vnoremap <silent> <Plug>(joiner)S :<c-u>let g:JoinerJoinText=function('joiner#join_with_string_literal')<cr>gv:<c-u>call joiner#do(visualmode(), 1)<cr> +vnoremap <silent> <Plug>(joiner)<space> :<c-u>let g:JoinerJoinText=function('joiner#default_join')<cr>gv:<c-u>call joiner#do(visualmode(), 1)<cr> -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 <Plug>(joiner-leader) - \ :<C-u>let g:JoinerJoinText=function("<SID>join_text_collapse")<cr> - \:<C-u>let g:joiner_join_text=' '<cr> - \:<C-u>set operatorfunc=<SID>joiner_do<cr> - \g@ - -vnoremap <Plug>(joiner-leader) - \ :<C-u>let g:JoinerJoinText=function("<SID>join_text_collapse")<cr> - \:<C-u>let g:joiner_join_text=' '<cr> - \gv:<C-u>call joiner_do(visualmode(), 1)<cr> - -if g:joiner_include_bindings - exec printf('nmap %s <Plug>(joiner-leader)', g:joiner_leader) - exec printf('vmap %s <Plug>(joiner-leader)', g:joiner_leader) +if g:joiner_include_bindings exec printf("vnoremap <silent> %s <Plug>(joiner)", g:joiner_leader) + exec printf("noremap <silent> %s <Plug>(joiner)", g:joiner_leader) endif |