~gpanders/dotfiles

f163b9ef43a11683e1e969d5e2063ee0062f4344 — Greg Anders 2 years ago ffeb5c4
Further tweaks to Markdown ftplugin

Use 'formatexpr' to "pre-filter" the file before running it through
'formatprg'. This escapes spaces in links in list items, to prevent
this:

    -   [A link to a really cool article](https://reallylongurltoacoolarticle.biz/foo/bar/baz/omg/wow)

from becoming this:

    -   [A link to a really
        cool article](https://reallylongurltoacoolarticle.biz/foo/bar/baz/omg/wow)

Also switch back to using `markdown` format instead of `gfm` in pandoc
since it does work just a little bit better.

Also add a new buffer-local command `:Reflinks` that toggles using the
`--reference-links` option.
2 files changed, 38 insertions(+), 3 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 +5 -3
@@ 19,9 19,11 @@ endif

if executable('pandoc')
  compiler pandoc
  let &l:formatprg = 'pandoc -f gfm -t gfm --standalone --columns=' . &textwidth
  command! -buffer Toc exe '%!' . &l:formatprg . ' --toc'
  let b:undo_ftplugin .= '|setl fp<|delc Toc'
  let &l:formatprg = 'pandoc -f markdown -t markdown --atx-headers --standalone --columns=' . &textwidth
  setlocal formatexpr=ft#markdown#format()
  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'
endif

" Use [[ and ]] to navigate between sections

M vim/.vim/autoload/ft/markdown.vim => vim/.vim/autoload/ft/markdown.vim +33 -0
@@ 1,3 1,36 @@
function! ft#markdown#format()
    " 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
endfunction

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

function! ft#markdown#reflinks(bang)
    if a:bang
        if &l:formatprg =~# '--reference-links'
            let &l:formatprg = substitute(&l:formatprg, ' --reference-links', '', '')
        endif
    else
        if &l:formatprg !~# '--reference-links'
            let &l:formatprg .= ' --reference-links'
        endif
    endif
    let view = winsaveview()
    normal! gggqG
    call winrestview(view)
endfunction

function! ft#markdown#section(back)
    for _ in range(v:count1)
        call search('\%(^#\+ \|^\S.*\n^-\+$\|^\S.*\n^=\+$\)',