Merge pull request #77 from lervag/issue66

Improved normalize links in diary (fixes #66)
This commit is contained in:
EinfachToll 2014-11-10 09:14:23 +01:00
commit a579e258a1

View File

@ -1691,6 +1691,15 @@ function! s:clean_url(url) " {{{
return join(url, " ")
endfunction " }}}
" s:in_diary
function! s:in_diary() " {{{
let file_path = vimwiki#u#path_norm(expand("%:p"))
let rel_path = VimwikiGet('diary_rel_path')
let diary_path = vimwiki#u#path_norm(VimwikiGet('path') . rel_path)
return rel_path != ''
\ && file_path =~# '^'.vimwiki#u#escape(diary_path)
endfunction " }}}
" vimwiki#base#normalize_link_helper
function! vimwiki#base#normalize_link_helper(str, rxUrl, rxDesc, template) " {{{
let str = a:str
@ -1713,6 +1722,32 @@ function! vimwiki#base#normalize_imagelink_helper(str, rxUrl, rxDesc, rxStyle, t
return lnk
endfunction " }}}
" s:normalize_link_in_diary
function! s:normalize_link_in_diary(lnk) " {{{
let link = a:lnk . VimwikiGet('ext')
let link_wiki = VimwikiGet('path') . '/' . link
let link_diary = VimwikiGet('path') . '/'
\ . VimwikiGet('diary_rel_path') . '/' . link
let link_exists_in_diary = filereadable(link_diary)
let link_exists_in_wiki = filereadable(link_wiki)
let link_is_date = a:lnk =~# '\d\d\d\d-\d\d-\d\d'
if ! link_exists_in_wiki || link_exists_in_diary || link_is_date
let str = a:lnk
let rxUrl = g:vimwiki_rxWord
let rxDesc = ''
let template = g:vimwiki_WikiLinkTemplate1
else
let depth = len(split(VimwikiGet('diary_rel_path'), '/'))
let str = repeat('../', depth) . a:lnk . '|' . a:lnk
let rxUrl = '^.*\ze|'
let rxDesc = '|\zs.*$'
let template = g:vimwiki_WikiLinkTemplate2
endif
return vimwiki#base#normalize_link_helper(str, rxUrl, rxDesc, template)
endfunction " }}}
" s:normalize_link_syntax_n
function! s:normalize_link_syntax_n() " {{{
@ -1744,9 +1779,13 @@ function! s:normalize_link_syntax_n() " {{{
" 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, '',
\ g:vimwiki_WikiLinkTemplate1)
if s:in_diary()
let sub = s:normalize_link_in_diary(lnk)
else
let sub = vimwiki#base#normalize_link_helper(lnk,
\ g:vimwiki_rxWord, '',
\ g:vimwiki_WikiLinkTemplate1)
endif
call vimwiki#base#replacestr_at_cursor('\V'.lnk, sub)
if g:vimwiki_debug > 1
echomsg "Word: ".lnk." Sub: ".sub
@ -1764,20 +1803,24 @@ function! s:normalize_link_syntax_v() " {{{
let rt = getregtype('"')
try
norm! gv""y
let visual_selection = @"
let visual_selection = substitute(g:vimwiki_WikiLinkTemplate1, '__LinkUrl__', '\='."'".visual_selection."'", '')
" Save selected text to register "
normal! gv""y
call setreg('"', visual_selection, 'v')
" paste result
norm! `>""pgvd
" Set substitution
if s:in_diary()
let sub = s:normalize_link_in_diary(@")
else
let sub = substitute(g:vimwiki_WikiLinkTemplate1,
\ '__LinkUrl__', '\=' . "'" . @" . "'", '')
endif
" Put substitution in register " and change text
call setreg('"', sub, 'v')
normal! `>""pgvd
finally
call setreg('"', rv, rt)
let &selection = sel_save
endtry
endfunction " }}}
" vimwiki#base#normalize_link