diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2023-03-15 00:11:39 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2023-03-15 00:13:45 +1300 |
commit | be813f200aa9ec1ce92c5bd996c23e2d79fa580d (patch) | |
tree | 0ac9b4d95d9f878275507159c276e9569c6e75aa | |
parent | Turn off line/column highlighting options too (diff) | |
download | vim-copy-linebreak-be813f200aa9ec1ce92c5bd996c23e2d79fa580d.tar.gz vim-copy-linebreak-be813f200aa9ec1ce92c5bd996c23e2d79fa580d.zip |
Complete rewrite of implementation
* Set a broader set of copy-friendly options
* Use filter() to clear out non-existent options
* Switch on value for 'showbreak' per Vim patch 8.1.2281
* Clean up comments
-rw-r--r-- | autoload/copy_linebreak.vim | 93 |
1 files changed, 59 insertions, 34 deletions
diff --git a/autoload/copy_linebreak.vim b/autoload/copy_linebreak.vim index 8c81133..9a2e9d8 100644 --- a/autoload/copy_linebreak.vim +++ b/autoload/copy_linebreak.vim @@ -1,45 +1,70 @@ -" Enable copy-friendly linebreak options +" 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 - setlocal nolinebreak linebreak? - let s:colorcolumn_save = &colorcolumn - setlocal colorcolumn= - let s:cursorcolumn_save = &cursorcolumn - setlocal nocursorcolumn - let s:cursorline_save = &cursorline - setlocal nocursorline - let s:showbreak_save = &showbreak - set showbreak= - if exists('+breakindent') - setlocal nobreakindent - endif + + " 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 +" Disable copy-friendly 'linebreak' options function! copy_linebreak#Disable() abort - setlocal linebreak linebreak? - if exists('s:colorcolumn_save') - let &colorcolumn = s:colorcolumn_save - unlet s:colorcolumn_save - endif - if exists('s:cursorcolumn_save') - let &cursorcolumn = s:cursorcolumn_save - unlet s:cursorcolumn_save - endif - if exists('s:cursorline_save') - let &cursorline = s:cursorline_save - unlet s:cursorline_save - endif - if exists('s:showbreak_save') - let &showbreak = s:showbreak_save - unlet s:showbreak_save - endif - if exists('+breakindent') - setlocal breakindent< + + " 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 +" 'linebreak' option as the pivot. +" function! copy_linebreak#Toggle() abort if &linebreak call copy_linebreak#Enable() |