Add wikilocal option to assign a name to each wiki.

Names can be used for interwiki links in the format wn.name:link.
Additionally, :VimwikiUISelect and the menu created in GVim utilize the
wiki name. Fixes #612, closes #477.
This commit is contained in:
Rane Brown 2019-04-21 13:05:01 -06:00
parent 37f020d21a
commit 510c149512
4 changed files with 116 additions and 18 deletions

View File

@ -133,6 +133,7 @@ function! vimwiki#base#resolve_link(link_text, ...)
let scheme = matchstr(link_text, '^\zs'.vimwiki#vars#get_global('rxSchemes').'\ze:') let scheme = matchstr(link_text, '^\zs'.vimwiki#vars#get_global('rxSchemes').'\ze:')
if scheme == '' if scheme == ''
" interwiki link scheme is default
let link_infos.scheme = 'wiki'.source_wiki let link_infos.scheme = 'wiki'.source_wiki
else else
let link_infos.scheme = scheme let link_infos.scheme = scheme
@ -181,12 +182,36 @@ function! vimwiki#base#resolve_link(link_text, ...)
" extract the other items depending on the scheme " extract the other items depending on the scheme
if link_infos.scheme =~# '\mwiki\d\+' if link_infos.scheme =~# '\mwiki\d\+'
" interwiki link named wiki 'wn.name:link' format
let wnmatch = matchlist(link_text, '\m^wn\.\([a-zA-Z0-9\-_ ]\+\):\(.*\)')
if len(wnmatch) >= 2 && wnmatch[1] !=? '' && wnmatch[2] !=? ''
let wname = wnmatch[1]
for idx in range(vimwiki#vars#number_of_wikis())
if vimwiki#vars#get_wikilocal('name', idx) ==# wname
" name matches!
let link_infos.index = idx
let link_text = wnmatch[2]
break
endif
endfor
if link_text !=# wnmatch[2]
" error: invalid wiki name
let link_infos.index = -2
let link_infos.filename = ''
" use scheme field to return invalid wiki name
let link_infos.scheme = wname
return link_infos
endif
else
" interwiki link numbered wiki format
let link_infos.index = eval(matchstr(link_infos.scheme, '\D\+\zs\d\+\ze')) let link_infos.index = eval(matchstr(link_infos.scheme, '\D\+\zs\d\+\ze'))
if link_infos.index < 0 || link_infos.index >= vimwiki#vars#number_of_wikis() if link_infos.index < 0 || link_infos.index >= vimwiki#vars#number_of_wikis()
let link_infos.index = -1 let link_infos.index = -1
let link_infos.filename = '' let link_infos.filename = ''
return link_infos return link_infos
endif endif
endif
if !is_relative || link_infos.index != source_wiki if !is_relative || link_infos.index != source_wiki
let root_dir = vimwiki#vars#get_wikilocal('path', link_infos.index) let root_dir = vimwiki#vars#get_wikilocal('path', link_infos.index)
@ -293,6 +318,9 @@ function! vimwiki#base#open_link(cmd, link, ...)
if link_infos.filename == '' if link_infos.filename == ''
if link_infos.index == -1 if link_infos.index == -1
echomsg 'Vimwiki Error: No registered wiki ''' . link_infos.scheme . '''.' echomsg 'Vimwiki Error: No registered wiki ''' . link_infos.scheme . '''.'
elseif link_infos.index == -2
" scheme field stores wiki name for this error case
echom 'Vimwiki Error: No wiki found with name "' . link_infos.scheme . '"'
else else
echomsg 'Vimwiki Error: Unable to resolve link!' echomsg 'Vimwiki Error: Unable to resolve link!'
endif endif
@ -892,18 +920,35 @@ endfunction
function! s:print_wiki_list() function! s:print_wiki_list()
let idx = 0 " find the max name length for prettier formatting
while idx < vimwiki#vars#number_of_wikis() let max_len = 0
for idx in range(vimwiki#vars#number_of_wikis())
let wname = vimwiki#vars#get_wikilocal('name', idx)
if len(wname) > max_len
let max_len = len(wname)
endif
endfor
" print each wiki, active wiki highlighted and marked with '*'
for idx in range(vimwiki#vars#number_of_wikis())
if idx == vimwiki#vars#get_bufferlocal('wiki_nr') if idx == vimwiki#vars#get_bufferlocal('wiki_nr')
let sep = ' * ' let sep = '*'
echohl PmenuSel echohl PmenuSel
else else
let sep = ' ' let sep = ' '
echohl None echohl None
endif endif
echo (idx + 1) . sep . vimwiki#vars#get_wikilocal('path', idx) let wname = vimwiki#vars#get_wikilocal('name', idx)
let idx += 1 let wpath = vimwiki#vars#get_wikilocal('path', idx)
endwhile if wname ==? ''
let wname = '----'
if max_len < 4
let max_len = 4
endif
endif
let wname = '"' . wname . '"'
echo printf('%2d %s %-*s %s', idx+1, sep, max_len+2, wname, wpath)
endfor
echohl None echohl None
endfunction endfunction
@ -1459,8 +1504,12 @@ endfunction
function! vimwiki#base#ui_select() function! vimwiki#base#ui_select()
call s:print_wiki_list() call s:print_wiki_list()
let idx = input("Select Wiki (specify number): ") let idx = input('Select Wiki by number and press <Enter> (empty cancels): ')
if idx == "" if idx ==# ''
return
elseif idx !~# '\m[0-9]\+'
echo "\n"
echom 'Invalid wiki selection.'
return return
endif endif
call vimwiki#base#goto_index(idx) call vimwiki#base#goto_index(idx)

View File

@ -274,6 +274,7 @@ function! s:populate_wikilocal_options()
\ 'links_space_char': {'type': type(''), 'default': ' ', 'min_length': 1}, \ 'links_space_char': {'type': type(''), 'default': ' ', 'min_length': 1},
\ 'list_margin': {'type': type(0), 'default': -1, 'min': -1}, \ 'list_margin': {'type': type(0), 'default': -1, 'min': -1},
\ 'maxhi': {'type': type(0), 'default': 0, 'min': 0, 'max': 1}, \ 'maxhi': {'type': type(0), 'default': 0, 'min': 0, 'max': 1},
\ 'name': {'type': type(''), 'default': ''},
\ 'nested_syntaxes': {'type': type({}), 'default': {}}, \ 'nested_syntaxes': {'type': type({}), 'default': {}},
\ 'path': {'type': type(''), 'default': $HOME . '/vimwiki/', 'min_length': 1}, \ 'path': {'type': type(''), 'default': $HOME . '/vimwiki/', 'min_length': 1},
\ 'path_html': {'type': type(''), 'default': ''}, \ 'path_html': {'type': type(''), 'default': ''},

View File

@ -966,6 +966,13 @@ or: >
The number behind "wiki" is in the range 0..N-1 and identifies the destination The number behind "wiki" is in the range 0..N-1 and identifies the destination
wiki in |g:vimwiki_list|. wiki in |g:vimwiki_list|.
Named interwiki links are also supported in the format "wn.name:link" >
[[wn.My Name:This is a link]]
or: >
[[wn.MyWiki:This is a link source|Description of the link]]
See |vimwiki-option-name| to set a per wiki name.
Diary~ Diary~
The "diary:" scheme is used to link to diary entries: > The "diary:" scheme is used to link to diary entries: >
@ -2005,6 +2012,24 @@ If path_html is an empty string, the location is derived from
path_html will be set to '~/okidoki_html/'. path_html will be set to '~/okidoki_html/'.
*vimwiki-option-name*
------------------------------------------------------------------------------
Key Default value~
name ''
Description~
A name that can be used to create interwiki links: >
let g:vimwiki_list = [{'path': '~/my_site/',
\ 'name': 'My Wiki'}]
Valid names can contain letters, numbers, spaces, underscores, and dashes.
If duplicate names are used the interwiki link will jump to the first wiki
with a matching name in |g:vimwiki_list|.
The assigned wiki name will also be shown in the menu entries in GVim.
See the option |g:vimwiki_menu|.
*vimwiki-option-auto_export* *vimwiki-option-auto_export*
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
Key Default value Values~ Key Default value Values~
@ -2543,6 +2568,10 @@ Default: {}
*g:vimwiki_menu* *g:vimwiki_menu*
Create a menu in the menu bar of GVim, where you can open the available wikis. Create a menu in the menu bar of GVim, where you can open the available wikis.
If the wiki has an assigned name (see |vimwiki-option-name|) the menu entry
will match the name. Otherwise the final folder of |vimwiki-option-path| will
be used for the name. If there are duplicate entries the index number from
|g:vimwiki_list| will be appended to the name.
Value Description~ Value Description~
'' No menu '' No menu
@ -3297,6 +3326,7 @@ https://github.com/vimwiki-backup/vimwiki/issues.
2.5 (in progress)~ 2.5 (in progress)~
New:~ New:~
* PR #675: Add option |vimwiki-option-name| to assign a per wiki name.
* PR #661: Add option |g:vimwiki_auto_header| to automatically generate * PR #661: Add option |g:vimwiki_auto_header| to automatically generate
a level 1 header for new wiki pages. a level 1 header for new wiki pages.
* PR #665: Integration with vimwiki_markdown gem * PR #665: Integration with vimwiki_markdown gem
@ -3347,6 +3377,7 @@ Removed:~
* *
Fixed:~ Fixed:~
* Issue #612: GVim menu displayed duplicate names.
* Issue #456: Omnicompletion of wikilinks under Windows. Note: this should * Issue #456: Omnicompletion of wikilinks under Windows. Note: this should
be considered a temporary fix until #478 is closed. be considered a temporary fix until #478 is closed.
* Issue #654: Fix `:VimwikiShowVersion` command. * Issue #654: Fix `:VimwikiShowVersion` command.

View File

@ -417,12 +417,29 @@ nnoremap <unique><script> <Plug>VimwikiMakeTomorrowDiaryNote
function! s:build_menu(topmenu) function! s:build_menu(topmenu)
let wnamelist = []
for idx in range(vimwiki#vars#number_of_wikis()) for idx in range(vimwiki#vars#number_of_wikis())
let norm_path = fnamemodify(vimwiki#vars#get_wikilocal('path', idx), ':h:t') let wname = vimwiki#vars#get_wikilocal('name', idx)
let norm_path = escape(norm_path, '\ \.') if wname ==? ''
execute 'menu '.a:topmenu.'.Open\ index.'.norm_path. " fall back to the path if wiki isn't named
let wname = fnamemodify(vimwiki#vars#get_wikilocal('path', idx), ':h:t')
endif
if index(wnamelist, wname) != -1
" append wiki index number to duplicate entries
let wname = wname . ' ' . string(idx + 1)
endif
" add entry to the list of names for duplicate checks
call add(wnamelist, wname)
" escape spaces and periods
let wname = escape(wname, '\ \.')
" build the menu
execute 'menu '.a:topmenu.'.Open\ index.'.wname.
\ ' :call vimwiki#base#goto_index('.(idx+1).')<CR>' \ ' :call vimwiki#base#goto_index('.(idx+1).')<CR>'
execute 'menu '.a:topmenu.'.Open/Create\ diary\ note.'.norm_path. execute 'menu '.a:topmenu.'.Open/Create\ diary\ note.'.wname.
\ ' :call vimwiki#diary#make_note('.(idx+1).')<CR>' \ ' :call vimwiki#diary#make_note('.(idx+1).')<CR>'
endfor endfor
endfunction endfunction