diff options
-rw-r--r-- | VERSION | 2 | ||||
-rw-r--r-- | autoload/copy_linebreak.vim | 74 | ||||
-rw-r--r-- | doc/copy_linebreak.txt | 2 | ||||
-rw-r--r-- | plugin/copy_linebreak.vim | 49 |
4 files changed, 83 insertions, 44 deletions
@@ -1 +1 @@ -0.7.0 +2.0.0 diff --git a/autoload/copy_linebreak.vim b/autoload/copy_linebreak.vim new file mode 100644 index 0000000..9a2e9d8 --- /dev/null +++ b/autoload/copy_linebreak.vim @@ -0,0 +1,74 @@ +" Window-local options to set back to Vim defaults while copy-friendly +" line-breaking options are in effect; note we have to switch on the presence +" of patch 8.1.2281 to find a workable value for 'showbreak'. +" +" <https://github.com/vim/vim/releases/tag/v8.1.2281> +" +let s:options = { + \ 'breakindent': 0, + \ 'colorcolumn': '', + \ 'cursorcolumn': 0, + \ 'cursorline': 0, + \ 'linebreak': 0, + \ 'showbreak': + \ has('patch-8.1.2281') ? 'NONE' : '', + \} + +" Filter that list for only the options that actually exist in this version of +" Vim; we use & rather than +, because it's OK if the option doesn't work, we +" just don't want to throw errors when we try to get/set it. +" +call filter(s:options, 'exists(''&''.v:key)') + +" Helper function to set a local option with :execute +function! s:SetOption(option, value) abort + execute 'let &l:'.a:option.' = a:value' +endfunction + +" Enable copy-friendly 'linebreak' options +function! copy_linebreak#Enable() abort + + " Set appropriate window-local options to their Vim defaults, saving their + " values into a window-scoped dictionary first, so that #Disable() can put + " them back when we're done. + " + let w:copy_linebreak_save = {} + for option in keys(s:options) + let w:copy_linebreak_save[option] = eval('&l:'.option) + call s:SetOption(option, s:options[option]) + endfor + + " Report the new value of 'linebreak' to the user + setlocal linebreak? + +endfunction + +" Disable copy-friendly 'linebreak' options +function! copy_linebreak#Disable() abort + + " Set appropriate window-local options back to the values to which they were + " set before #Enable() messed with them, if a window-local dictionary to do + " such exists. + " + if exists('w:copy_linebreak_save') + for option in keys(w:copy_linebreak_save) + call s:SetOption(option, w:copy_linebreak_save[option]) + endfor + unlet w:copy_linebreak_save + endif + + " Report the new value of 'linebreak' to the user + setlocal linebreak? + +endfunction + +" Toggle copy-friendly linebreak options, using the current setting for the +" 'linebreak' option as the pivot. +" +function! copy_linebreak#Toggle() abort + if &linebreak + call copy_linebreak#Enable() + else + call copy_linebreak#Disable() + endif +endfunction diff --git a/doc/copy_linebreak.txt b/doc/copy_linebreak.txt index 754cccb..3748558 100644 --- a/doc/copy_linebreak.txt +++ b/doc/copy_linebreak.txt @@ -1,4 +1,4 @@ -*copy_linebreak.txt* For Vim version 6.0 Last change: 2018 June 27 +*copy_linebreak.txt* For Vim version 7.0 Last change: 2023 March 15 DESCRIPTION *copy_linebreak* diff --git a/plugin/copy_linebreak.vim b/plugin/copy_linebreak.vim index 8b93c24..76d9228 100644 --- a/plugin/copy_linebreak.vim +++ b/plugin/copy_linebreak.vim @@ -5,53 +5,18 @@ " Author: Tom Ryder <tom@sanctum.geek.nz> " License: Same as Vim itself " -if exists('loaded_copy_linebreak') || &compatible - finish -endif -if !has('linebreak') || v:version < 600 +if exists('loaded_copy_linebreak') || &compatible || v:version < 700 finish endif let loaded_copy_linebreak = 1 -" Enable copy-friendly linebreak options -function! s:CopyLinebreakEnable() - setlocal nolinebreak linebreak? - let s:showbreak_save = &showbreak - set showbreak= - if exists('+breakindent') - setlocal nobreakindent - endif -endfunction - -" Disable copy-friendly linebreak options -function! s:CopyLinebreakDisable() - setlocal linebreak linebreak? - if exists('s:showbreak_save') - let &showbreak = s:showbreak_save - unlet s:showbreak_save - endif - if exists('+breakindent') - setlocal breakindent< - endif -endfunction - -" Toggle copy-friendly linebreak options, using the current setting for the -" 'linebreak' option as the pivot -function! s:CopyLinebreakToggle() - if &linebreak - call s:CopyLinebreakEnable() - else - call s:CopyLinebreakDisable() - endif -endfunction - " Provide mappings to the functions just defined -noremap <silent> +nnoremap <silent> <unique> \ <Plug>(CopyLinebreakEnable) - \ :<C-U>call <SID>CopyLinebreakEnable()<CR> -noremap <silent> + \ :<C-U>call copy_linebreak#Enable()<CR> +nnoremap <silent> <unique> \ <Plug>(CopyLinebreakDisable) - \ :<C-U>call <SID>CopyLinebreakDisable()<CR> -noremap <silent> + \ :<C-U>call copy_linebreak#Disable()<CR> +nnoremap <silent> <unique> \ <Plug>(CopyLinebreakToggle) - \ :<C-U>call <SID>CopyLinebreakToggle()<CR> + \ :<C-U>call copy_linebreak#Toggle()<CR> |