~gpanders/dotfiles

774f90f0bbda152886d6a052efd0e7dac2d97637 — Greg Anders 1 year, 11 months ago dcc7861
Refactor Markdown formatexpr function

- Rename to ft#markdown#formatexpr
- Use `:g` instead of `:s` to unwrap wrapped links
- Replace *italics* with _italics_
- Replace ``` {.lang} with ```lang for fenced code blocks
2 files changed, 13 insertions(+), 9 deletions(-)

M vim/.vim/after/ftplugin/markdown.vim
M vim/.vim/autoload/ft/markdown.vim
M vim/.vim/after/ftplugin/markdown.vim => vim/.vim/after/ftplugin/markdown.vim +1 -1
@@ 20,7 20,7 @@ endif
if executable('pandoc')
  compiler pandoc
  let &l:formatprg = 'pandoc -f markdown -t markdown --atx-headers --standalone --columns=' . &textwidth
  setlocal formatexpr=ft#markdown#format()
  setlocal formatexpr=ft#markdown#formatexpr()
  command! -buffer Toc call ft#markdown#toc()
  command! -bang -buffer Reflinks call ft#markdown#reflinks(<bang>0)
  let b:undo_ftplugin .= '|setl fp< fex<|delc Toc|delc Reflinks'

M vim/.vim/autoload/ft/markdown.vim => vim/.vim/autoload/ft/markdown.vim +12 -8
@@ 1,21 1,25 @@
function! ft#markdown#format()
function! ft#markdown#formatexpr()
    " Only use this in normal mode to prevent automatic formatting being
    " messed up in insert mode
    if mode() !=# 'n'
        return 1
    endif

    " Escape whitespace characters in link text in lists and then run
    " 'formatprg' as usual
    let start = v:lnum
    let end = start + v:count - 1
    exe start . ',' . end . 's/\%(^\s*\%([-*]\|[0-9]\+\.\)\s\+\[.*\)\@<=\s\ze.*]/\\&/g'
    exe start . ',' . end . '!' . &l:formatprg
    let lastsearch = @/
    let range = v:lnum . ',' . (v:lnum + v:count - 1)
    exe range . '!' . &l:formatprg
    " Join wrapped links
    silent exe range . 'g/\[[^]]*$/.,/\%(\[.*\)\@<!]/j'
    " Convert *italics* (used by pandoc) to _italics_ (which I prefer)
    exe range . 's/\*\([^*]\+\)\*/_\1_/ge'
    " Convert {.language} to just language
    exe range . 's/[~`]\{3,}\zs\s*{\.\(\w\+\)}/\1/g'
    let @/ = lastsearch
endfunction

function! ft#markdown#toc()
    let fp = &l:formatprg
    let &l:formatprg = fp . ' --toc'
    let &l:formatprg .= ' --toc'
    let view = winsaveview()
    normal! gggqG
    call winrestview(view)