2012-06-07 02:00:00 +02:00
|
|
|
" vim:tabstop=2:shiftwidth=2:expandtab:foldmethod=marker:textwidth=79
|
|
|
|
" Vimwiki autoload plugin file
|
|
|
|
" Desc: Link functions for markdown syntax
|
2015-02-23 12:10:42 +01:00
|
|
|
" Home: https://github.com/vimwiki/vimwiki/
|
2012-06-07 02:00:00 +02:00
|
|
|
|
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" MISC helper functions {{{
|
|
|
|
|
|
|
|
" vimwiki#markdown_base#reset_mkd_refs
|
|
|
|
function! vimwiki#markdown_base#reset_mkd_refs() "{{{
|
|
|
|
call VimwikiClear('markdown_refs')
|
|
|
|
endfunction "}}}
|
|
|
|
|
|
|
|
" vimwiki#markdown_base#scan_reflinks
|
|
|
|
function! vimwiki#markdown_base#scan_reflinks() " {{{
|
|
|
|
let mkd_refs = {}
|
|
|
|
" construct list of references using vimgrep
|
|
|
|
try
|
2015-03-23 13:25:39 +01:00
|
|
|
" Why noautocmd? Because https://github.com/vimwiki/vimwiki/issues/121
|
|
|
|
noautocmd execute 'vimgrep #'.g:vimwiki_rxMkdRef.'#j %'
|
2012-07-07 02:00:00 +02:00
|
|
|
catch /^Vim\%((\a\+)\)\=:E480/ " No Match
|
|
|
|
"Ignore it, and move on to the next file
|
|
|
|
endtry
|
2013-04-19 05:46:58 +02:00
|
|
|
"
|
2012-07-07 02:00:00 +02:00
|
|
|
for d in getqflist()
|
|
|
|
let matchline = join(getline(d.lnum, min([d.lnum+1, line('$')])), ' ')
|
|
|
|
let descr = matchstr(matchline, g:vimwiki_rxMkdRefMatchDescr)
|
|
|
|
let url = matchstr(matchline, g:vimwiki_rxMkdRefMatchUrl)
|
|
|
|
if descr != '' && url != ''
|
|
|
|
let mkd_refs[descr] = url
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
call VimwikiSet('markdown_refs', mkd_refs)
|
|
|
|
return mkd_refs
|
|
|
|
endfunction "}}}
|
|
|
|
|
|
|
|
|
|
|
|
" vimwiki#markdown_base#get_reflinks
|
|
|
|
function! vimwiki#markdown_base#get_reflinks() " {{{
|
|
|
|
let done = 1
|
|
|
|
try
|
|
|
|
let mkd_refs = VimwikiGet('markdown_refs')
|
|
|
|
catch
|
|
|
|
" work-around hack
|
|
|
|
let done = 0
|
|
|
|
" ... the following command does not work inside catch block !?
|
|
|
|
" > let mkd_refs = vimwiki#markdown_base#scan_reflinks()
|
|
|
|
endtry
|
|
|
|
if !done
|
|
|
|
let mkd_refs = vimwiki#markdown_base#scan_reflinks()
|
|
|
|
endif
|
|
|
|
return mkd_refs
|
|
|
|
endfunction "}}}
|
|
|
|
|
|
|
|
" vimwiki#markdown_base#open_reflink
|
|
|
|
" try markdown reference links
|
|
|
|
function! vimwiki#markdown_base#open_reflink(link) " {{{
|
|
|
|
" echom "vimwiki#markdown_base#open_reflink"
|
|
|
|
let link = a:link
|
|
|
|
let mkd_refs = vimwiki#markdown_base#get_reflinks()
|
|
|
|
if has_key(mkd_refs, link)
|
|
|
|
let url = mkd_refs[link]
|
|
|
|
call vimwiki#base#system_open_link(url)
|
|
|
|
return 1
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
endif
|
|
|
|
endfunction " }}}
|
|
|
|
" }}}
|
|
|
|
|
|
|
|
" WIKI link following functions {{{
|
|
|
|
|
2014-12-04 21:03:49 +01:00
|
|
|
" vimwiki#markdown_base#follow_link
|
2013-04-19 05:46:58 +02:00
|
|
|
function! vimwiki#markdown_base#follow_link(split, ...) "{{{ Parse link at cursor and pass
|
2012-07-07 02:00:00 +02:00
|
|
|
" to VimwikiLinkHandler, or failing that, the default open_link handler
|
|
|
|
" echom "markdown_base#follow_link"
|
|
|
|
|
|
|
|
if 0
|
|
|
|
" Syntax-specific links
|
|
|
|
" XXX: @Stuart: do we still need it?
|
|
|
|
" XXX: @Maxim: most likely! I am still working on a seemless way to
|
|
|
|
" integrate regexp's without complicating syntax/vimwiki.vim
|
|
|
|
else
|
2015-02-09 20:05:25 +01:00
|
|
|
if a:split ==# "split"
|
2012-07-07 02:00:00 +02:00
|
|
|
let cmd = ":split "
|
2015-02-09 20:05:25 +01:00
|
|
|
elseif a:split ==# "vsplit"
|
2012-07-07 02:00:00 +02:00
|
|
|
let cmd = ":vsplit "
|
2015-02-09 20:05:25 +01:00
|
|
|
elseif a:split ==# "tabnew"
|
2012-07-07 02:00:00 +02:00
|
|
|
let cmd = ":tabnew "
|
|
|
|
else
|
|
|
|
let cmd = ":e "
|
|
|
|
endif
|
|
|
|
|
|
|
|
" try WikiLink
|
|
|
|
let lnk = matchstr(vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWikiLink),
|
|
|
|
\ g:vimwiki_rxWikiLinkMatchUrl)
|
|
|
|
" try WikiIncl
|
|
|
|
if lnk == ""
|
|
|
|
let lnk = matchstr(vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWikiIncl),
|
|
|
|
\ g:vimwiki_rxWikiInclMatchUrl)
|
|
|
|
endif
|
|
|
|
" try Weblink
|
|
|
|
if lnk == ""
|
|
|
|
let lnk = matchstr(vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWeblink),
|
|
|
|
\ g:vimwiki_rxWeblinkMatchUrl)
|
|
|
|
endif
|
|
|
|
|
|
|
|
if lnk != ""
|
|
|
|
if !VimwikiLinkHandler(lnk)
|
|
|
|
if !vimwiki#markdown_base#open_reflink(lnk)
|
2016-01-18 09:51:08 +01:00
|
|
|
" remove the extension from the filename if exists
|
|
|
|
let lnk = substitute(lnk, VimwikiGet('ext').'$', '', '')
|
2012-07-07 02:00:00 +02:00
|
|
|
call vimwiki#base#open_link(cmd, lnk)
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
if a:0 > 0
|
|
|
|
execute "normal! ".a:1
|
2013-04-19 05:46:58 +02:00
|
|
|
else
|
2012-07-07 02:00:00 +02:00
|
|
|
call vimwiki#base#normalize_link(0)
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
|
|
|
endfunction " }}}
|
|
|
|
|
|
|
|
" LINK functions {{{
|
|
|
|
|
|
|
|
" s:normalize_link_syntax_n
|
2012-06-07 02:00:00 +02:00
|
|
|
function! s:normalize_link_syntax_n() " {{{
|
|
|
|
let lnum = line('.')
|
|
|
|
|
|
|
|
" try WikiIncl
|
|
|
|
let lnk = vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWikiIncl)
|
|
|
|
if !empty(lnk)
|
|
|
|
" NO-OP !!
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
" try WikiLink0: replace with WikiLink1
|
|
|
|
let lnk = vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWikiLink0)
|
|
|
|
if !empty(lnk)
|
|
|
|
let sub = vimwiki#base#normalize_link_helper(lnk,
|
|
|
|
\ g:vimwiki_rxWikiLinkMatchUrl, g:vimwiki_rxWikiLinkMatchDescr,
|
|
|
|
\ g:vimwiki_WikiLink1Template2)
|
|
|
|
call vimwiki#base#replacestr_at_cursor(g:vimwiki_rxWikiLink0, sub)
|
|
|
|
return
|
|
|
|
endif
|
2013-04-19 05:46:58 +02:00
|
|
|
|
2012-06-07 02:00:00 +02:00
|
|
|
" try WikiLink1: replace with WikiLink0
|
|
|
|
let lnk = vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWikiLink1)
|
|
|
|
if !empty(lnk)
|
|
|
|
let sub = vimwiki#base#normalize_link_helper(lnk,
|
|
|
|
\ g:vimwiki_rxWikiLinkMatchUrl, g:vimwiki_rxWikiLinkMatchDescr,
|
|
|
|
\ g:vimwiki_WikiLinkTemplate2)
|
|
|
|
call vimwiki#base#replacestr_at_cursor(g:vimwiki_rxWikiLink1, sub)
|
|
|
|
return
|
|
|
|
endif
|
2013-04-19 05:46:58 +02:00
|
|
|
|
2012-06-07 02:00:00 +02:00
|
|
|
" try Weblink
|
|
|
|
let lnk = vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWeblink)
|
|
|
|
if !empty(lnk)
|
|
|
|
let sub = vimwiki#base#normalize_link_helper(lnk,
|
|
|
|
\ g:vimwiki_rxWeblinkMatchUrl, g:vimwiki_rxWeblinkMatchDescr,
|
|
|
|
\ g:vimwiki_Weblink1Template)
|
|
|
|
call vimwiki#base#replacestr_at_cursor(g:vimwiki_rxWeblink, sub)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
" try Word (any characters except separators)
|
|
|
|
" rxWord is less permissive than rxWikiLinkUrl which is used in
|
|
|
|
" normalize_link_syntax_v
|
|
|
|
let lnk = vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWord)
|
|
|
|
if !empty(lnk)
|
|
|
|
let sub = vimwiki#base#normalize_link_helper(lnk,
|
|
|
|
\ g:vimwiki_rxWord, '',
|
2016-01-18 09:51:08 +01:00
|
|
|
\ g:vimwiki_Weblink1Template)
|
2012-06-07 02:00:00 +02:00
|
|
|
call vimwiki#base#replacestr_at_cursor('\V'.lnk, sub)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
endfunction " }}}
|
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" s:normalize_link_syntax_v
|
2012-06-07 02:00:00 +02:00
|
|
|
function! s:normalize_link_syntax_v() " {{{
|
|
|
|
let lnum = line('.')
|
|
|
|
let sel_save = &selection
|
|
|
|
let &selection = "old"
|
|
|
|
let rv = @"
|
|
|
|
let rt = getregtype('"')
|
|
|
|
let done = 0
|
|
|
|
|
|
|
|
try
|
|
|
|
norm! gvy
|
|
|
|
let visual_selection = @"
|
2016-01-18 09:51:08 +01:00
|
|
|
let link = substitute(g:vimwiki_Weblink1Template, '__LinkUrl__', '\='."'".visual_selection."'", '')
|
2016-02-22 09:00:37 +01:00
|
|
|
let link = substitute(link, '__LinkDescription__', '\='."'".visual_selection."'", '')
|
2012-06-07 02:00:00 +02:00
|
|
|
|
2016-01-18 09:51:08 +01:00
|
|
|
call setreg('"', link, 'v')
|
2012-06-07 02:00:00 +02:00
|
|
|
|
|
|
|
" paste result
|
|
|
|
norm! `>pgvd
|
|
|
|
|
|
|
|
finally
|
|
|
|
call setreg('"', rv, rt)
|
|
|
|
let &selection = sel_save
|
|
|
|
endtry
|
|
|
|
|
|
|
|
endfunction " }}}
|
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#normalize_link
|
2012-06-07 02:00:00 +02:00
|
|
|
function! vimwiki#markdown_base#normalize_link(is_visual_mode) "{{{
|
2012-07-07 02:00:00 +02:00
|
|
|
if 0
|
|
|
|
" Syntax-specific links
|
|
|
|
else
|
|
|
|
if !a:is_visual_mode
|
|
|
|
call s:normalize_link_syntax_n()
|
|
|
|
elseif visualmode() ==# 'v' && line("'<") == line("'>")
|
|
|
|
" action undefined for 'line-wise' or 'multi-line' visual mode selections
|
|
|
|
call s:normalize_link_syntax_v()
|
|
|
|
endif
|
2012-06-07 02:00:00 +02:00
|
|
|
endif
|
|
|
|
endfunction "}}}
|
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" }}}
|
|
|
|
|
|
|
|
" -------------------------------------------------------------------------
|
|
|
|
" Load syntax-specific Wiki functionality
|
|
|
|
" -------------------------------------------------------------------------
|
|
|
|
|