~gpanders/dotfiles

99d0a9434740b5fc8889f9ef8c22367da3540a68 — Greg Anders 1 year, 9 months ago 2e26520
Open links in Markdown files with Return
2 files changed, 45 insertions(+), 0 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 +3 -0
@@ 32,3 32,6 @@ let b:undo_ftplugin .= '|nun <buffer> [[|nun <buffer> ]]'

nnoremap <buffer> <silent> Z! :<C-U>call ft#markdown#eval()<CR>
let b:undo_ftplugin .= '|nun <buffer> Z!'

nnoremap <buffer> <silent> <CR> :<C-U>call ft#markdown#open()<CR>
let b:undo_ftplugin .= '|nun <buffer> <CR>'

M vim/.vim/autoload/ft/markdown.vim => vim/.vim/autoload/ft/markdown.vim +42 -0
@@ 1,3 1,45 @@
function! s:openlink(link) abort
    if a:link =~# '^https\?://'
        if executable('xdg-open')
            call system('xdg-open ' . a:link)
        elseif executable('open')
            call system('open ' . a:link)
        endif
    elseif !empty(a:link)
        exec 'edit ' . simplify(fnamemodify(a:link, ':p'))
    endif
endfunction

function! s:openreflink(name) abort
    let l = search('^\s*\[' . a:name . '\]\s*:\s\+', 'nw')
    if l
        let link = matchlist(getline(l), '^\s*\[' . a:name . '\]\s*:\s\+\(\S\+\)')[1]
        call s:openlink(link)
    endif
endfunction

function! ft#markdown#open() abort
    let line = getline('.')
    let [_, col] = searchpos('\[[^]]\+\]\(([^)]\+)\|\[[^]]\+]\|\[]\|[^[(]\=\)', 'bcn', line('.'))
    if col == 0
        return
    endif

    let matches = matchlist(line, '\(\[[^]]\+\]\)\(([^)]\+)\|\[[^]]\+]\|\[]\|[^[(]\=\)', col-1)
    if matches[2] =~# '([^)]\+)'
        " Normal link
        let link = matchstr(matches[2], '(\zs\([^)]\+\)\ze)')
        call s:openlink(link)
    elseif matches[2] =~# '\[[^]]\+]'
        " Reference link
        let ref = matchstr(matches[2], '\[\zs\([^]]\+\)\ze]')
        call s:openreflink(ref)
    else
        let ref = matchstr(matches[1], '\[\zs\([^]]\+\)\ze]')
        call s:openreflink(ref)
    endif
endfunction

function! ft#markdown#formatexpr()
    " Only use this in normal mode to prevent automatic formatting being
    " messed up in insert mode