aboutsummaryrefslogtreecommitdiff
path: root/autoload/copy_linebreak.vim
blob: 9a2e9d8e2b2bd2eadd6825063af468699270a44c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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