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, " ") return join(url, " ")
endfunction " }}} 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 " vimwiki#base#normalize_link_helper
function! vimwiki#base#normalize_link_helper(str, rxUrl, rxDesc, template) " {{{ function! vimwiki#base#normalize_link_helper(str, rxUrl, rxDesc, template) " {{{
let str = a:str let str = a:str
@ -1713,6 +1722,32 @@ function! vimwiki#base#normalize_imagelink_helper(str, rxUrl, rxDesc, rxStyle, t
return lnk return lnk
endfunction " }}} 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 " s:normalize_link_syntax_n
function! 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 " normalize_link_syntax_v
let lnk = vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWord) let lnk = vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWord)
if !empty(lnk) if !empty(lnk)
if s:in_diary()
let sub = s:normalize_link_in_diary(lnk)
else
let sub = vimwiki#base#normalize_link_helper(lnk, let sub = vimwiki#base#normalize_link_helper(lnk,
\ g:vimwiki_rxWord, '', \ g:vimwiki_rxWord, '',
\ g:vimwiki_WikiLinkTemplate1) \ g:vimwiki_WikiLinkTemplate1)
endif
call vimwiki#base#replacestr_at_cursor('\V'.lnk, sub) call vimwiki#base#replacestr_at_cursor('\V'.lnk, sub)
if g:vimwiki_debug > 1 if g:vimwiki_debug > 1
echomsg "Word: ".lnk." Sub: ".sub echomsg "Word: ".lnk." Sub: ".sub
@ -1764,20 +1803,24 @@ function! s:normalize_link_syntax_v() " {{{
let rt = getregtype('"') let rt = getregtype('"')
try try
norm! gv""y " Save selected text to register "
let visual_selection = @" normal! gv""y
let visual_selection = substitute(g:vimwiki_WikiLinkTemplate1, '__LinkUrl__', '\='."'".visual_selection."'", '')
call setreg('"', visual_selection, 'v') " Set substitution
if s:in_diary()
" paste result let sub = s:normalize_link_in_diary(@")
norm! `>""pgvd else
let sub = substitute(g:vimwiki_WikiLinkTemplate1,
\ '__LinkUrl__', '\=' . "'" . @" . "'", '')
endif
" Put substitution in register " and change text
call setreg('"', sub, 'v')
normal! `>""pgvd
finally finally
call setreg('"', rv, rt) call setreg('"', rv, rt)
let &selection = sel_save let &selection = sel_save
endtry endtry
endfunction " }}} endfunction " }}}
" vimwiki#base#normalize_link " vimwiki#base#normalize_link