From 622186195795e19566f5041cece6880a55265545 Mon Sep 17 00:00:00 2001 From: EinfachToll Date: Thu, 22 Dec 2016 18:16:05 +0100 Subject: [PATCH] Use new access functions for wiki-local variables -- part 1 Ref #256 --- autoload/vimwiki/base.vim | 14 +++++++------- autoload/vimwiki/diary.vim | 37 ++++++++++++++++++------------------- autoload/vimwiki/html.vim | 4 ++-- autoload/vimwiki/lst.vim | 5 +++-- autoload/vimwiki/vars.vim | 13 +++++++++---- ftplugin/vimwiki.vim | 2 +- plugin/vimwiki.vim | 23 +++-------------------- 7 files changed, 43 insertions(+), 55 deletions(-) diff --git a/autoload/vimwiki/base.vim b/autoload/vimwiki/base.vim index 8875e0b..513a0b1 100644 --- a/autoload/vimwiki/base.vim +++ b/autoload/vimwiki/base.vim @@ -335,7 +335,7 @@ function! vimwiki#base#resolve_link(link_text, ...) "{{{ let link_infos.filename = \ VimwikiGet('path', link_infos.index) . - \ VimwikiGet('diary_rel_path', link_infos.index) . + \ vimwiki#vars#get_wikilocal('diary_rel_path', link_infos.index) . \ link_text . \ VimwikiGet('ext', link_infos.index) elseif (link_infos.scheme ==# 'file' || link_infos.scheme ==# 'local') @@ -520,7 +520,7 @@ function! vimwiki#base#find_files(wiki_nr, directories_only) if wiki_nr >= 0 let root_directory = VimwikiGet('path', wiki_nr) else - let root_directory = VimwikiGet('path').VimwikiGet('diary_rel_path') + let root_directory = VimwikiGet('path').vimwiki#vars#get_wikilocal('diary_rel_path') let wiki_nr = g:vimwiki_current_idx endif if a:directories_only @@ -548,7 +548,7 @@ function! vimwiki#base#get_wikilinks(wiki_nr, also_absolute_links) if a:wiki_nr == g:vimwiki_current_idx let cwd = vimwiki#path#wikify_path(expand('%:p:h')) elseif a:wiki_nr < 0 - let cwd = VimwikiGet('path').VimwikiGet('diary_rel_path') + let cwd = VimwikiGet('path').vimwiki#vars#get_wikilocal('diary_rel_path') else let cwd = VimwikiGet('path', a:wiki_nr) endif @@ -563,7 +563,7 @@ function! vimwiki#base#get_wikilinks(wiki_nr, also_absolute_links) if a:wiki_nr == g:vimwiki_current_idx let cwd = VimwikiGet('path') elseif a:wiki_nr < 0 - let cwd = VimwikiGet('path').VimwikiGet('diary_rel_path') + let cwd = VimwikiGet('path').vimwiki#vars#get_wikilocal('diary_rel_path') endif let wikifile = fnamemodify(wikifile, ':r') " strip extension let wikifile = '/'.vimwiki#path#relpath(cwd, wikifile) @@ -1856,7 +1856,7 @@ endfunction " }}} " s:is_diary_file function! s:is_diary_file(filename) " {{{ let file_path = vimwiki#path#path_norm(a:filename) - let rel_path = VimwikiGet('diary_rel_path') + let rel_path = vimwiki#vars#get_wikilocal('diary_rel_path') let diary_path = vimwiki#path#path_norm(VimwikiGet('path') . rel_path) return rel_path != '' \ && file_path =~# '^'.vimwiki#u#escape(diary_path) @@ -1889,7 +1889,7 @@ 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 + \ . vimwiki#vars#get_wikilocal('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' @@ -1900,7 +1900,7 @@ function! s:normalize_link_in_diary(lnk) " {{{ let rxDesc = '' let template = g:vimwiki_WikiLinkTemplate1 else - let depth = len(split(VimwikiGet('diary_rel_path'), '/')) + let depth = len(split(vimwiki#vars#get_wikilocal('diary_rel_path'), '/')) let str = repeat('../', depth) . a:lnk . '|' . a:lnk let rxUrl = '^.*\ze|' let rxDesc = '|\zs.*$' diff --git a/autoload/vimwiki/diary.vim b/autoload/vimwiki/diary.vim index 80b1fa7..0fecbe1 100644 --- a/autoload/vimwiki/diary.vim +++ b/autoload/vimwiki/diary.vim @@ -20,23 +20,22 @@ function! s:prefix_zero(num) "{{{ return a:num endfunction "}}} -function! s:get_date_link(fmt) "{{{ - return strftime(a:fmt) -endfunction "}}} - function! s:diary_path(...) "{{{ let idx = a:0 == 0 ? g:vimwiki_current_idx : a:1 - return VimwikiGet('path', idx).VimwikiGet('diary_rel_path', idx) + return VimwikiGet('path', idx).vimwiki#vars#get_wikilocal('diary_rel_path', idx) endfunction "}}} function! s:diary_index(...) "{{{ let idx = a:0 == 0 ? g:vimwiki_current_idx : a:1 - return s:diary_path(idx).VimwikiGet('diary_index', idx).VimwikiGet('ext', idx) + return s:diary_path(idx).vimwiki#vars#get_wikilocal('diary_index', idx).VimwikiGet('ext', idx) endfunction "}}} -function! s:diary_date_link(...) "{{{ - let idx = a:0 == 0 ? g:vimwiki_current_idx : a:1 - return s:get_date_link(VimwikiGet('diary_link_fmt', idx)) +function! vimwiki#diary#diary_date_link(...) "{{{ + if a:0 + return strftime('%Y-%m-%d', a:1) + else + return strftime('%Y-%m-%d') + endif endfunction "}}} function! s:get_position_links(link) "{{{ @@ -45,8 +44,8 @@ function! s:get_position_links(link) "{{{ if a:link =~# '^\d\{4}-\d\d-\d\d' let links = keys(s:get_diary_links()) " include 'today' into links - if index(links, s:diary_date_link()) == -1 - call add(links, s:diary_date_link()) + if index(links, vimwiki#diary#diary_date_link()) == -1 + call add(links, vimwiki#diary#diary_date_link()) endif call sort(links) let idx = index(links, a:link) @@ -85,7 +84,7 @@ endfun "}}} fun! s:get_diary_links() "{{{ let rx = '^\d\{4}-\d\d-\d\d' - let s_files = glob(VimwikiGet('path').VimwikiGet('diary_rel_path').'*'.VimwikiGet('ext')) + let s_files = glob(VimwikiGet('path').vimwiki#vars#get_wikilocal('diary_rel_path').'*'.VimwikiGet('ext')) let files = split(s_files, '\n') call filter(files, 'fnamemodify(v:val, ":t") =~# "'.escape(rx, '\').'"') @@ -119,7 +118,7 @@ fun! s:group_links(links) "{{{ endfun "}}} function! s:sort(lst) "{{{ - if VimwikiGet("diary_sort") ==? 'desc' + if vimwiki#vars#get_wikilocal('diary_sort') ==? 'desc' return reverse(sort(a:lst)) else return sort(a:lst) @@ -172,7 +171,7 @@ function! vimwiki#diary#make_note(wnum, ...) "{{{ let idx = 0 endif - call vimwiki#path#mkdir(VimwikiGet('path', idx).VimwikiGet('diary_rel_path', idx)) + call vimwiki#path#mkdir(VimwikiGet('path', idx).vimwiki#vars#get_wikilocal('diary_rel_path', idx)) if a:0 && a:1 == 1 let cmd = 'tabedit' @@ -182,7 +181,7 @@ function! vimwiki#diary#make_note(wnum, ...) "{{{ if a:0>1 let link = 'diary:'.a:2 else - let link = 'diary:'.s:diary_date_link(idx) + let link = 'diary:'.vimwiki#diary#diary_date_link() endif call vimwiki#base#open_link(cmd, link, s:diary_index(idx)) @@ -218,7 +217,7 @@ function! vimwiki#diary#goto_next_day() "{{{ let link = 'diary:'.links[idx+1] else " goto today - let link = 'diary:'.s:diary_date_link() + let link = 'diary:'.vimwiki#diary#diary_date_link() endif if len(link) @@ -238,7 +237,7 @@ function! vimwiki#diary#goto_prev_day() "{{{ let link = 'diary:'.links[idx-1] else " goto today - let link = 'diary:'.s:diary_date_link() + let link = 'diary:'.vimwiki#diary#diary_date_link() endif if len(link) @@ -252,7 +251,7 @@ function! vimwiki#diary#generate_diary_section() "{{{ if vimwiki#path#is_equal(current_file, diary_file) let content_rx = '^\%(\s*\* \)\|\%(^\s*$\)\|\%('.g:vimwiki_rxHeader.'\)' call vimwiki#base#update_listing_in_buffer(s:format_diary(), - \ VimwikiGet('diary_header'), content_rx, line('$')+1, 1) + \ vimwiki#vars#get_wikilocal('diary_header'), content_rx, line('$')+1, 1) else echomsg 'Vimwiki Error: You can generate diary links only in a diary index page!' endif @@ -286,7 +285,7 @@ endfunction "}}} function vimwiki#diary#calendar_sign(day, month, year) "{{{ let day = s:prefix_zero(a:day) let month = s:prefix_zero(a:month) - let sfile = VimwikiGet('path').VimwikiGet('diary_rel_path'). + let sfile = VimwikiGet('path').vimwiki#vars#get_wikilocal('diary_rel_path'). \ a:year.'-'.month.'-'.day.VimwikiGet('ext') return filereadable(expand(sfile)) endfunction "}}} diff --git a/autoload/vimwiki/html.vim b/autoload/vimwiki/html.vim index 10081b5..7da33cd 100644 --- a/autoload/vimwiki/html.vim +++ b/autoload/vimwiki/html.vim @@ -1348,13 +1348,13 @@ function! s:parse_line(line, state) " {{{ endfunction " }}} function! s:use_custom_wiki2html() "{{{ - let custom_wiki2html = VimwikiGet('custom_wiki2html') + let custom_wiki2html = vimwiki#vars#get_wikilocal('custom_wiki2html') return !empty(custom_wiki2html) && (s:file_exists(custom_wiki2html) || s:binary_exists(custom_wiki2html)) endfunction " }}} function! vimwiki#html#CustomWiki2HTML(path, wikifile, force) "{{{ call vimwiki#path#mkdir(a:path) - echomsg system(VimwikiGet('custom_wiki2html'). ' '. + echomsg system(vimwiki#vars#get_wikilocal('custom_wiki2html'). ' '. \ a:force. ' '. \ VimwikiGet('syntax'). ' '. \ strpart(VimwikiGet('ext'), 1). ' '. diff --git a/autoload/vimwiki/lst.vim b/autoload/vimwiki/lst.vim index 8e295b6..7ae8d43 100644 --- a/autoload/vimwiki/lst.vim +++ b/autoload/vimwiki/lst.vim @@ -113,10 +113,11 @@ function! vimwiki#lst#default_symbol() "{{{ endfunction "}}} function! vimwiki#lst#get_list_margin() "{{{ - if VimwikiGet('list_margin') < 0 + let list_margin = vimwiki#vars#get_wikilocal('list_margin') + if list_margin < 0 return &sw else - return VimwikiGet('list_margin') + return list_margin endif endfunction "}}} diff --git a/autoload/vimwiki/vars.vim b/autoload/vimwiki/vars.vim index fd04ca2..ca7fd5d 100644 --- a/autoload/vimwiki/vars.vim +++ b/autoload/vimwiki/vars.vim @@ -105,7 +105,6 @@ function! s:populate_wikilocal_options() \ 'custom_wiki2html': '', \ 'diary_header': 'Diary', \ 'diary_index': 'diary', - \ 'diary_link_fmt': '%Y-%m-%d', \ 'diary_rel_path': 'diary/', \ 'diary_sort': 'desc', \ 'ext': '.wiki', @@ -217,12 +216,18 @@ function! vimwiki#vars#get_global(key) endfunction -function! vimwiki#vars#get_wikilocal(wiki_nr, key) - return g:vimwiki_wikilocal_vars[a:wiki_nr][a:key] +" the second argument can be a wiki number. When absent, the wiki of the currently active buffer is +" used +function! vimwiki#vars#get_wikilocal(key, ...) + if a:0 + return g:vimwiki_wikilocal_vars[a:1][a:key] + else + return g:vimwiki_wikilocal_vars[vimwiki#vars#get_bufferlocal('wiki_nr')][a:key] + endif endfunction -function! vimwiki#vars#set_wikilocal(wiki_nr, key, value) +function! vimwiki#vars#set_wikilocal(key, value, wiki_nr) if a:wiki_nr == len(g:vimwiki_wikilocal_vars) - 1 call insert(g:vimwiki_wikilocal_vars, {}, -1) endif diff --git a/ftplugin/vimwiki.vim b/ftplugin/vimwiki.vim index 813118a..3c1bd03 100644 --- a/ftplugin/vimwiki.vim +++ b/ftplugin/vimwiki.vim @@ -655,7 +655,7 @@ if VimwikiGet('auto_toc') augroup END endif -if VimwikiGet('auto_tags') +if vimwiki#vars#get_wikilocal('auto_tags') " Automatically update tags metadata on page write. augroup vimwiki au BufWritePost call vimwiki#tags#update_tags(0, '') diff --git a/plugin/vimwiki.vim b/plugin/vimwiki.vim index e12b5fd..066fe3c 100644 --- a/plugin/vimwiki.vim +++ b/plugin/vimwiki.vim @@ -44,8 +44,8 @@ function! Validate_wiki_options(idx) " {{{ call VimwikiSet('path_html', s:normalize_path(s:path_html(a:idx)), a:idx) call VimwikiSet('template_path', \ s:normalize_path(VimwikiGet('template_path', a:idx)), a:idx) - call VimwikiSet('diary_rel_path', - \ s:normalize_path(VimwikiGet('diary_rel_path', a:idx)), a:idx) + call vimwiki#vars#set_wikilocal('diary_rel_path', + \ s:normalize_path(vimwiki#vars#get_wikilocal('diary_rel_path', a:idx)), a:idx) endfunction " }}} function! s:vimwiki_idx() " {{{ @@ -339,23 +339,6 @@ let s:vimwiki_defaults.auto_toc = 0 " is wiki temporary -- was added to g:vimwiki_list by opening arbitrary wiki " file. let s:vimwiki_defaults.temp = 0 - -" diary -let s:vimwiki_defaults.diary_rel_path = 'diary/' -let s:vimwiki_defaults.diary_index = 'diary' -let s:vimwiki_defaults.diary_header = 'Diary' -let s:vimwiki_defaults.diary_sort = 'desc' - -" Do not change this! Will wait till vim become more datetime awareable. -let s:vimwiki_defaults.diary_link_fmt = '%Y-%m-%d' - -" NEW! in v2.0 -" custom_wiki2html -let s:vimwiki_defaults.custom_wiki2html = '' -" -let s:vimwiki_defaults.list_margin = -1 - -let s:vimwiki_defaults.auto_tags = 0 "}}} " DEFAULT options {{{ @@ -409,7 +392,7 @@ command! -count=1 VimwikiMakeDiaryNote command! -count=1 VimwikiTabMakeDiaryNote \ call vimwiki#diary#make_note(v:count1, 1) command! -count=1 VimwikiMakeYesterdayDiaryNote - \ call vimwiki#diary#make_note(v:count1, 0, strftime(VimwikiGet('diary_link_fmt', v:count1 - 1), localtime() - 60*60*24)) + \ call vimwiki#diary#make_note(v:count1, 0, vimwiki#diary#diary_date_link(localtime() - 60*60*24)) command! VimwikiDiaryGenerateLinks \ call vimwiki#diary#generate_diary_section()