~gpanders/dotfiles

999096c51ef541cb5ad029c45c3495e69c0bb3ab — Greg Anders 1 year, 9 months ago 25e4292
Add mapping to open Markdown links in a split
2 files changed, 14 insertions(+), 11 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 -2
@@ 33,5 33,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>'
nnoremap <buffer> <silent> <CR> :<C-U>call ft#markdown#open('edit')<CR>
nnoremap <buffer> <silent> <C-W><CR> :<C-U>call ft#markdown#open('split')<CR>
let b:undo_ftplugin .= '|nun <buffer> <CR>|nun <buffer> <S-CR>'

M vim/.vim/autoload/ft/markdown.vim => vim/.vim/autoload/ft/markdown.vim +11 -9
@@ 1,24 1,26 @@
function! s:openlink(link) abort
function! s:openlink(link, action) 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'))
    elseif a:link =~# '^/' " Absolute path
        exec a:action . ' ' . a:link
    elseif !empty(a:link) " Relative path
        exec a:action . ' ' . simplify(expand('%:h') . '/' . a:link)
    endif
endfunction

function! s:openreflink(name) abort
function! s:openreflink(name, action) 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)
        call s:openlink(link, a:action)
    endif
endfunction

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


@@ 29,14 31,14 @@ function! ft#markdown#open() abort
    if matches[2] =~# '([^)]\+)'
        " Normal link
        let link = matchstr(matches[2], '(\zs\([^)]\+\)\ze)')
        call s:openlink(link)
        call s:openlink(link, a:action)
    elseif matches[2] =~# '\[[^]]\+]'
        " Reference link
        let ref = matchstr(matches[2], '\[\zs\([^]]\+\)\ze]')
        call s:openreflink(ref)
        call s:openreflink(ref, a:action)
    else
        let ref = matchstr(matches[1], '\[\zs\([^]]\+\)\ze]')
        call s:openreflink(ref)
        call s:openreflink(ref, a:action)
    endif
endfunction