aboutsummaryrefslogtreecommitdiff
path: root/vim/config/format.vim
blob: b0de7621297e24862a69f4ca05bfb0da84945284 (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
" If we can, add j to the format options to get rid of comment leaders when
" joining lines
if v:version > 703 || v:version ==# 703 && has('patch541')
  set formatoptions+=j
endif

"
" Quick way to toggle flags in 'formatoptions' that I often want to change;
" specifically:
"
" a - Automatically format paragraphs, reapplying the wrap on every text
"     insertion or deletion; sometimes I want this and sometimes I
"     don't, it particularly varies when typing prose in Markdown that
"     includes headings and code
" c - Automatically wrap comments at 'textwidth' (which I allow the filetypes
"     to set for me)
" t - Automatically wrap text at 'textwidth' (as above)
"
" So I just have to type e.g. \a to toggle the auto-format flag on and off;
" very handy
"
if has('eval')

  " Declare function
  function! s:ToggleFormatFlag(flag)

    " Decide on whether we're adding or removing the flag
    if &l:formatoptions =~# a:flag
      let l:command = 'setlocal formatoptions-=' . a:flag
    else
      let l:command = 'setlocal formatoptions+=' . a:flag
    endif

    " Execute the command we determined and show the result
    silent! execute l:command
    setlocal formatoptions?

  endfunction

  " Map leader-letters to corresponding format option flags
  nnoremap <silent> <Leader>a :<C-U>call <SID>ToggleFormatFlag('a')<CR>
  nnoremap <silent> <Leader>c :<C-U>call <SID>ToggleFormatFlag('c')<CR>
  nnoremap <silent> <Leader>t :<C-U>call <SID>ToggleFormatFlag('t')<CR>

endif