Improved normalize links in diary (fixes #66)

This commit is contained in:
Karl Yngve Lervåg 2014-09-15 21:44:32 +02:00
parent 21b3f63a47
commit e8ad164fb0

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,22 @@ 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) " {{{
" Check if link already exists or is a date
if filereadable(a:lnk . '.wiki') || a:lnk =~# '\d\d\d\d-\d\d-\d\d'
let sub = vimwiki#base#normalize_link_helper(a:lnk,
\ g:vimwiki_rxWord, '',
\ g:vimwiki_WikiLinkTemplate1)
else
let depth = len(split(VimwikiGet('diary_rel_path'), '/'))
let sub = '[[' . repeat('../', depth) . a:lnk . '|' . a:lnk . ']]'
endif
return sub
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 +1769,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 +1793,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