2010-05-12 02:00:00 +02:00
|
|
|
" vim:tabstop=2:shiftwidth=2:expandtab:foldmethod=marker:textwidth=79
|
2010-01-20 01:00:00 +01:00
|
|
|
" Vimwiki autoload plugin file
|
2015-02-23 12:10:42 +01:00
|
|
|
" Desc: Basic functionality
|
|
|
|
" Home: https://github.com/vimwiki/vimwiki/
|
2010-01-20 01:00:00 +01:00
|
|
|
|
|
|
|
if exists("g:loaded_vimwiki_auto") || &cp
|
|
|
|
finish
|
|
|
|
endif
|
|
|
|
let g:loaded_vimwiki_auto = 1
|
|
|
|
|
2015-01-23 20:41:36 +01:00
|
|
|
" s:vimwiki_get_known_syntaxes
|
|
|
|
function! s:vimwiki_get_known_syntaxes() " {{{
|
2012-07-07 02:00:00 +02:00
|
|
|
" Getting all syntaxes that different wikis could have
|
|
|
|
let syntaxes = {}
|
|
|
|
let syntaxes['default'] = 1
|
|
|
|
for wiki in g:vimwiki_list
|
|
|
|
if has_key(wiki, 'syntax')
|
|
|
|
let syntaxes[wiki.syntax] = 1
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
" append map g:vimwiki_ext2syntax
|
|
|
|
for syn in values(g:vimwiki_ext2syntax)
|
|
|
|
let syntaxes[syn] = 1
|
|
|
|
endfor
|
|
|
|
return keys(syntaxes)
|
|
|
|
endfunction " }}}
|
2012-06-07 02:00:00 +02:00
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#apply_wiki_options
|
2012-06-07 02:00:00 +02:00
|
|
|
function! vimwiki#base#apply_wiki_options(options) " {{{ Update the current
|
|
|
|
" wiki using the options dictionary
|
|
|
|
for kk in keys(a:options)
|
|
|
|
let g:vimwiki_list[g:vimwiki_current_idx][kk] = a:options[kk]
|
|
|
|
endfor
|
2014-11-26 14:35:38 +01:00
|
|
|
call Validate_wiki_options(g:vimwiki_current_idx)
|
2012-06-07 02:00:00 +02:00
|
|
|
call vimwiki#base#setup_buffer_state(g:vimwiki_current_idx)
|
|
|
|
endfunction " }}}
|
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#read_wiki_options
|
2012-06-07 02:00:00 +02:00
|
|
|
function! vimwiki#base#read_wiki_options(check) " {{{ Attempt to read wiki
|
|
|
|
" options from the current page's directory, or its ancesters. If a file
|
|
|
|
" named vimwiki.vimrc is found, which declares a wiki-options dictionary
|
|
|
|
" named g:local_wiki, a message alerts the user that an update has been
|
|
|
|
" found and may be applied. If the argument check=1, the user is queried
|
|
|
|
" before applying the update to the current wiki's option.
|
|
|
|
|
|
|
|
" Save global vimwiki options ... after all, the global list is often
|
|
|
|
" initialized for the first time in vimrc files, and we don't want to
|
|
|
|
" overwrite !! (not to mention all the other globals ...)
|
|
|
|
let l:vimwiki_list = deepcopy(g:vimwiki_list, 1)
|
|
|
|
"
|
2012-07-07 02:00:00 +02:00
|
|
|
if a:check > 1
|
2012-06-07 02:00:00 +02:00
|
|
|
call vimwiki#base#print_wiki_state()
|
|
|
|
echo " \n"
|
|
|
|
endif
|
|
|
|
"
|
|
|
|
let g:local_wiki = {}
|
|
|
|
let done = 0
|
|
|
|
" ... start the wild-goose chase!
|
|
|
|
for invsubdir in ['.', '..', '../..', '../../..']
|
|
|
|
" other names are possible, but most vimrc files will cause grief!
|
|
|
|
for nm in ['vimwiki.vimrc']
|
|
|
|
" TODO: use an alternate strategy, instead of source, to read options
|
2012-07-07 02:00:00 +02:00
|
|
|
if done
|
2012-06-07 02:00:00 +02:00
|
|
|
continue
|
|
|
|
endif
|
|
|
|
"
|
|
|
|
let local_wiki_options_filename = expand('%:p:h').'/'.invsubdir.'/'.nm
|
|
|
|
if !filereadable(local_wiki_options_filename)
|
|
|
|
continue
|
|
|
|
endif
|
|
|
|
"
|
|
|
|
echo "\nFound file : ".local_wiki_options_filename
|
|
|
|
let query = "Vimwiki: Check for options in this file [Y]es/[n]o? "
|
2015-02-07 22:01:20 +01:00
|
|
|
if a:check > 0 && input(query) =~? '^n')
|
2012-06-07 02:00:00 +02:00
|
|
|
continue
|
|
|
|
endif
|
|
|
|
"
|
|
|
|
try
|
|
|
|
execute 'source '.local_wiki_options_filename
|
|
|
|
catch
|
|
|
|
endtry
|
|
|
|
if empty(g:local_wiki)
|
|
|
|
continue
|
|
|
|
endif
|
|
|
|
"
|
2012-07-07 02:00:00 +02:00
|
|
|
if a:check > 0
|
2012-06-07 02:00:00 +02:00
|
|
|
echo "\n\nFound wiki options\n g:local_wiki = ".string(g:local_wiki)
|
|
|
|
let query = "Vimwiki: Apply these options [Y]es/[n]o? "
|
2015-02-07 22:01:20 +01:00
|
|
|
if input(query) =~? '^n'
|
2012-06-07 02:00:00 +02:00
|
|
|
let g:local_wiki = {}
|
|
|
|
continue
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
"
|
|
|
|
" restore global list
|
|
|
|
" - this prevents corruption by g:vimwiki_list in options_file
|
|
|
|
let g:vimwiki_list = deepcopy(l:vimwiki_list, 1)
|
|
|
|
"
|
|
|
|
call vimwiki#base#apply_wiki_options(g:local_wiki)
|
|
|
|
let done = 1
|
|
|
|
endfor
|
|
|
|
endfor
|
|
|
|
if !done
|
|
|
|
"
|
|
|
|
" restore global list, if no local options were found
|
|
|
|
" - this prevents corruption by g:vimwiki_list in options_file
|
|
|
|
let g:vimwiki_list = deepcopy(l:vimwiki_list, 1)
|
|
|
|
"
|
|
|
|
endif
|
2012-07-07 02:00:00 +02:00
|
|
|
if a:check > 1
|
2012-06-07 02:00:00 +02:00
|
|
|
echo " \n "
|
|
|
|
if done
|
|
|
|
call vimwiki#base#print_wiki_state()
|
|
|
|
else
|
|
|
|
echo "Vimwiki: No options were applied."
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endfunction " }}}
|
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#setup_buffer_state
|
2012-06-07 02:00:00 +02:00
|
|
|
function! vimwiki#base#setup_buffer_state(idx) " {{{ Init page-specific variables
|
|
|
|
" Only call this function *after* opening a wiki page.
|
|
|
|
if a:idx < 0
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let g:vimwiki_current_idx = a:idx
|
|
|
|
|
|
|
|
" The following state depends on the current active wiki page
|
|
|
|
let subdir = vimwiki#base#current_subdir(a:idx)
|
|
|
|
call VimwikiSet('subdir', subdir, a:idx)
|
|
|
|
call VimwikiSet('invsubdir', vimwiki#base#invsubdir(subdir), a:idx)
|
|
|
|
|
2015-01-24 08:49:20 +01:00
|
|
|
if g:vimwiki_auto_chdir == 1
|
|
|
|
exe 'lcd' VimwikiGet('path')
|
|
|
|
endif
|
|
|
|
|
2012-06-07 02:00:00 +02:00
|
|
|
" update cache
|
|
|
|
call vimwiki#base#cache_buffer_state()
|
|
|
|
endfunction " }}}
|
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#cache_buffer_state
|
2012-06-07 02:00:00 +02:00
|
|
|
function! vimwiki#base#cache_buffer_state() "{{{
|
|
|
|
if !exists('g:vimwiki_current_idx') && g:vimwiki_debug
|
|
|
|
echo "[Vimwiki Internal Error]: Missing global state variable: 'g:vimwiki_current_idx'"
|
|
|
|
endif
|
|
|
|
let b:vimwiki_idx = g:vimwiki_current_idx
|
2011-06-11 02:00:00 +02:00
|
|
|
endfunction "}}}
|
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#recall_buffer_state
|
2012-06-07 02:00:00 +02:00
|
|
|
function! vimwiki#base#recall_buffer_state() "{{{
|
|
|
|
if !exists('b:vimwiki_idx')
|
|
|
|
if g:vimwiki_debug
|
|
|
|
echo "[Vimwiki Internal Error]: Missing buffer state variable: 'b:vimwiki_idx'"
|
|
|
|
endif
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
let g:vimwiki_current_idx = b:vimwiki_idx
|
|
|
|
return 1
|
|
|
|
endif
|
|
|
|
endfunction " }}}
|
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#print_wiki_state
|
2012-06-07 02:00:00 +02:00
|
|
|
function! vimwiki#base#print_wiki_state() "{{{ print wiki options
|
|
|
|
" and buffer state variables
|
|
|
|
let g_width = 18
|
|
|
|
let b_width = 18
|
|
|
|
echo "- Wiki Options (idx=".g:vimwiki_current_idx.") -"
|
|
|
|
for kk in VimwikiGetOptionNames()
|
|
|
|
echo " '".kk."': ".repeat(' ', g_width-len(kk)).string(VimwikiGet(kk))
|
|
|
|
endfor
|
|
|
|
if !exists('b:vimwiki_list')
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
echo "- Cached Variables -"
|
|
|
|
for kk in keys(b:vimwiki_list)
|
|
|
|
echo " '".kk."': ".repeat(' ', b_width-len(kk)).string(b:vimwiki_list[kk])
|
|
|
|
endfor
|
|
|
|
endfunction "}}}
|
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#file_pattern
|
2012-06-07 02:00:00 +02:00
|
|
|
function! vimwiki#base#file_pattern(files) "{{{ Get search regex from glob()
|
|
|
|
" string. Aim to support *all* special characters, forcing the user to choose
|
|
|
|
" names that are compatible with any external restrictions that they
|
|
|
|
" encounter (e.g. filesystem, wiki conventions, other syntaxes, ...).
|
|
|
|
" See: http://code.google.com/p/vimwiki/issues/detail?id=316
|
2013-04-19 05:46:58 +02:00
|
|
|
" Change / to [/\\] to allow "Windows paths"
|
2014-12-04 21:27:02 +01:00
|
|
|
return '\V\%('.join(a:files, '\|').'\)\m'
|
2012-07-07 02:00:00 +02:00
|
|
|
endfunction "}}}
|
2010-01-20 01:00:00 +01:00
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#subdir
|
2012-06-07 02:00:00 +02:00
|
|
|
"FIXME TODO slow and faulty
|
2014-01-06 13:37:33 +01:00
|
|
|
function! vimwiki#base#subdir(path, filename) "{{{
|
2012-06-07 02:00:00 +02:00
|
|
|
let g:VimwikiLog.subdir += 1 "XXX
|
|
|
|
let path = a:path
|
|
|
|
" ensure that we are not fooled by a symbolic link
|
|
|
|
"FIXME if we are not "fooled", we end up in a completely different wiki?
|
2013-11-06 13:34:45 +01:00
|
|
|
if a:filename !~# '^scp:'
|
|
|
|
let filename = resolve(a:filename)
|
|
|
|
else
|
|
|
|
let filename = a:filename
|
|
|
|
endif
|
2010-01-20 01:00:00 +01:00
|
|
|
let idx = 0
|
2012-06-07 02:00:00 +02:00
|
|
|
"FIXME this can terminate in the middle of a path component!
|
2010-05-12 02:00:00 +02:00
|
|
|
while path[idx] ==? filename[idx]
|
2010-01-20 01:00:00 +01:00
|
|
|
let idx = idx + 1
|
|
|
|
endwhile
|
|
|
|
|
|
|
|
let p = split(strpart(filename, idx), '[/\\]')
|
2012-06-07 02:00:00 +02:00
|
|
|
let res = join(p[:-2], '/')
|
2010-01-20 01:00:00 +01:00
|
|
|
if len(res) > 0
|
2012-06-07 02:00:00 +02:00
|
|
|
let res = res.'/'
|
2010-01-20 01:00:00 +01:00
|
|
|
endif
|
|
|
|
return res
|
2012-06-07 02:00:00 +02:00
|
|
|
endfunction "}}}
|
2010-01-20 01:00:00 +01:00
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#current_subdir
|
2012-06-07 02:00:00 +02:00
|
|
|
function! vimwiki#base#current_subdir(idx)"{{{
|
|
|
|
return vimwiki#base#subdir(VimwikiGet('path', a:idx), expand('%:p'))
|
2010-01-20 01:00:00 +01:00
|
|
|
endfunction"}}}
|
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#invsubdir
|
2012-06-07 02:00:00 +02:00
|
|
|
function! vimwiki#base#invsubdir(subdir) " {{{
|
|
|
|
return substitute(a:subdir, '[^/\.]\+/', '../', 'g')
|
|
|
|
endfunction " }}}
|
|
|
|
|
2015-03-13 16:31:15 +01:00
|
|
|
|
|
|
|
" Returns: the number of the wiki a file belongs to
|
|
|
|
function! vimwiki#base#find_wiki(path) "{{{
|
|
|
|
let path = vimwiki#path#path_norm(vimwiki#path#chomp_slash(a:path))
|
|
|
|
let idx = 0
|
|
|
|
while idx < len(g:vimwiki_list)
|
|
|
|
let idx_path = expand(VimwikiGet('path', idx))
|
|
|
|
let idx_path = vimwiki#path#path_norm(vimwiki#path#chomp_slash(idx_path))
|
|
|
|
if vimwiki#path#is_equal(
|
|
|
|
\ vimwiki#path#path_common_pfx(idx_path, path), idx_path)
|
|
|
|
return idx
|
|
|
|
endif
|
|
|
|
let idx += 1
|
|
|
|
endwhile
|
|
|
|
|
|
|
|
" an orphan page has been detected
|
|
|
|
return -1
|
|
|
|
endfunction "}}}
|
|
|
|
|
|
|
|
|
|
|
|
" THE central function of Vimwiki. Extract infos about the target from a link.
|
|
|
|
" If the second parameter is present, which should be an absolute file path, it
|
|
|
|
" is assumed that the link appears in that file. Without it, the current file
|
|
|
|
" is used.
|
|
|
|
function! vimwiki#base#resolve_link(link_text, ...) "{{{
|
|
|
|
if a:0
|
|
|
|
let source_wiki = vimwiki#base#find_wiki(a:1)
|
|
|
|
let source_file = a:1
|
|
|
|
else
|
|
|
|
let source_wiki = g:vimwiki_current_idx
|
|
|
|
let source_file = expand('%:p')
|
|
|
|
endif
|
|
|
|
|
|
|
|
let link_text = a:link_text
|
|
|
|
|
|
|
|
" if link is schemeless add wikiN: scheme
|
|
|
|
if link_text !~# g:vimwiki_rxSchemeUrl
|
|
|
|
let link_text = 'wiki'.source_wiki.':'.link_text
|
|
|
|
endif
|
|
|
|
|
|
|
|
|
|
|
|
let link_infos = {
|
|
|
|
\ 'index': -1,
|
|
|
|
\ 'scheme': '',
|
|
|
|
\ 'filename': '',
|
|
|
|
\ 'anchor': '',
|
|
|
|
\ }
|
|
|
|
|
|
|
|
|
|
|
|
" extract scheme
|
|
|
|
let link_infos.scheme = matchstr(link_text, g:vimwiki_rxSchemeUrlMatchScheme)
|
|
|
|
if !(link_infos.scheme =~# '\mwiki\d\+' || link_infos.scheme ==# 'diary' ||
|
|
|
|
\ link_infos.scheme ==# 'local' || link_infos.scheme ==# 'file')
|
|
|
|
let link_infos.filename = '' " malformed link
|
|
|
|
return link_infos
|
|
|
|
endif
|
|
|
|
let link_text = matchstr(link_text, g:vimwiki_rxSchemeUrlMatchUrl)
|
|
|
|
|
|
|
|
let is_wiki_link = link_infos.scheme =~# '\mwiki\d\+' ||
|
|
|
|
\ link_infos.scheme ==# 'diary'
|
|
|
|
|
|
|
|
" extract anchor
|
|
|
|
if is_wiki_link
|
|
|
|
let split_lnk = split(link_text, '#', 1)
|
|
|
|
let link_text = split_lnk[0]
|
|
|
|
if len(split_lnk) > 1 && split_lnk[-1] != ''
|
|
|
|
let link_infos.anchor = join(split_lnk[1:], '#')
|
|
|
|
endif
|
|
|
|
if link_text == '' " because the link was of the form '#anchor'
|
|
|
|
let link_text = fnamemodify(source_file, ':p:t:r')
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
|
|
|
" check if absolute or relative path
|
|
|
|
if is_wiki_link && link_text[0] == '/'
|
|
|
|
let link_text = link_text[1:]
|
|
|
|
let is_relative = 0
|
|
|
|
elseif !is_wiki_link && (link_text[0] == '/' ||
|
|
|
|
\ (link_text =~? '\m^\a:' && vimwiki#u#is_windows()))
|
|
|
|
let is_relative = 0
|
|
|
|
else
|
|
|
|
let is_relative = 1
|
|
|
|
let root_dir = fnamemodify(source_file, ':p:h') . '/'
|
|
|
|
endif
|
|
|
|
|
|
|
|
|
|
|
|
" extract the other items depending on the scheme
|
|
|
|
if link_infos.scheme =~# '\mwiki\d\+'
|
|
|
|
let link_infos.index = eval(matchstr(link_infos.scheme, '\D\+\zs\d\+\ze'))
|
|
|
|
if link_infos.index < 0 || link_infos.index >= len(g:vimwiki_list)
|
|
|
|
let link_infos.filename = ''
|
|
|
|
return link_infos
|
|
|
|
endif
|
|
|
|
|
2015-03-19 09:05:19 +01:00
|
|
|
if !is_relative || link_infos.index != source_wiki
|
2015-03-13 16:31:15 +01:00
|
|
|
let root_dir = VimwikiGet('path', link_infos.index)
|
|
|
|
endif
|
|
|
|
|
|
|
|
let link_infos.filename = root_dir . link_text
|
|
|
|
|
|
|
|
if vimwiki#path#is_link_to_dir(link_text)
|
|
|
|
if g:vimwiki_dir_link != ''
|
|
|
|
let link_infos.filename .= g:vimwiki_dir_link .
|
|
|
|
\ VimwikiGet('ext', link_infos.index)
|
|
|
|
endif
|
|
|
|
else
|
|
|
|
let link_infos.filename .= VimwikiGet('ext', link_infos.index)
|
|
|
|
endif
|
|
|
|
|
|
|
|
elseif link_infos.scheme ==# 'diary'
|
|
|
|
let link_infos.index = source_wiki
|
|
|
|
|
|
|
|
let link_infos.filename =
|
|
|
|
\ VimwikiGet('path', link_infos.index) .
|
|
|
|
\ VimwikiGet('diary_rel_path', link_infos.index) .
|
|
|
|
\ link_text .
|
|
|
|
\ VimwikiGet('ext', link_infos.index)
|
|
|
|
elseif (link_infos.scheme ==# 'file' && is_relative) ||
|
|
|
|
\ link_infos.scheme ==# 'local'
|
|
|
|
let link_infos.filename = root_dir . link_text
|
|
|
|
else " absolute file link
|
|
|
|
" collapse repeated leading "/"'s within a link
|
|
|
|
let link_text = substitute(link_text, '\m^/*', '/', '')
|
|
|
|
" convert "/~..." into "~..." for fnamemodify
|
|
|
|
let link_text = substitute(link_text, '\m^/\~', '\~', '')
|
|
|
|
" convert /C: to C: (or fnamemodify(...":p:h") interpret it as C:\C:)
|
|
|
|
if vimwiki#u#is_windows()
|
|
|
|
let link_text = substitute(link_text, '\m^/\ze[[:alpha:]]:', '', '')
|
|
|
|
endif
|
|
|
|
let link_infos.filename = link_text
|
|
|
|
endif
|
|
|
|
|
|
|
|
let link_infos.filename = vimwiki#path#normalize(link_infos.filename)
|
|
|
|
return link_infos
|
|
|
|
endfunction "}}}
|
|
|
|
|
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#resolve_scheme
|
2014-12-14 14:49:06 +01:00
|
|
|
function! vimwiki#base#resolve_scheme(lnk, as_html, ...) " {{{ Resolve scheme
|
|
|
|
let quiet = a:0 && a:1 ? 1 : 0
|
2012-06-07 02:00:00 +02:00
|
|
|
let lnk = a:lnk
|
2014-02-13 12:42:24 +01:00
|
|
|
|
|
|
|
" if link is schemeless add wikiN: scheme
|
2015-02-09 20:47:11 +01:00
|
|
|
let is_schemeless = lnk !~# g:vimwiki_rxSchemeUrl
|
2012-06-07 02:00:00 +02:00
|
|
|
let lnk = (is_schemeless ? 'wiki'.g:vimwiki_current_idx.':'.lnk : lnk)
|
2013-04-19 05:46:58 +02:00
|
|
|
|
2012-06-07 02:00:00 +02:00
|
|
|
" Get scheme
|
|
|
|
let scheme = matchstr(lnk, g:vimwiki_rxSchemeUrlMatchScheme)
|
|
|
|
" Get link (without scheme)
|
|
|
|
let lnk = matchstr(lnk, g:vimwiki_rxSchemeUrlMatchUrl)
|
|
|
|
let path = ''
|
|
|
|
let subdir = ''
|
|
|
|
let ext = ''
|
|
|
|
let idx = -1
|
2014-02-13 12:42:24 +01:00
|
|
|
let anchor = ''
|
|
|
|
|
|
|
|
"extract anchor
|
2015-02-09 20:47:11 +01:00
|
|
|
if scheme =~# 'wiki' || scheme =~# 'diary'
|
2014-02-13 12:42:24 +01:00
|
|
|
let split_lnk = split(lnk, '#', 1)
|
|
|
|
let lnk = split_lnk[0]
|
|
|
|
if len(split_lnk) <= 1 || split_lnk[-1] == ''
|
|
|
|
let anchor = ''
|
|
|
|
else
|
|
|
|
let anchor = join(split_lnk[1:], '#')
|
|
|
|
endif
|
|
|
|
endif
|
2012-06-07 02:00:00 +02:00
|
|
|
|
|
|
|
" do nothing if scheme is unknown to vimwiki
|
2015-02-09 20:47:11 +01:00
|
|
|
if !(scheme =~# 'wiki.*' || scheme =~# 'diary' || scheme =~# 'local'
|
|
|
|
\ || scheme =~# 'file')
|
2014-02-13 12:42:24 +01:00
|
|
|
return [idx, scheme, path, subdir, lnk, ext, scheme.':'.lnk, anchor]
|
2012-06-07 02:00:00 +02:00
|
|
|
endif
|
|
|
|
|
|
|
|
" scheme behaviors
|
2015-02-09 20:47:11 +01:00
|
|
|
if scheme =~# 'wiki\d\+'
|
2012-06-07 02:00:00 +02:00
|
|
|
let idx = eval(matchstr(scheme, '\D\+\zs\d\+\ze'))
|
|
|
|
if idx < 0 || idx >= len(g:vimwiki_list)
|
2014-12-14 14:49:06 +01:00
|
|
|
if !quiet
|
|
|
|
echom 'Vimwiki Error: Numbered scheme refers to a non-existent wiki!'
|
|
|
|
endif
|
2014-02-13 12:42:24 +01:00
|
|
|
return [idx,'','','','','','', '']
|
2010-02-23 01:00:00 +01:00
|
|
|
endif
|
|
|
|
|
2012-06-07 02:00:00 +02:00
|
|
|
if a:as_html
|
|
|
|
if idx == g:vimwiki_current_idx
|
|
|
|
let path = VimwikiGet('path_html')
|
2010-05-12 02:00:00 +02:00
|
|
|
else
|
2012-06-07 02:00:00 +02:00
|
|
|
let path = VimwikiGet('path_html', idx)
|
2010-05-12 02:00:00 +02:00
|
|
|
endif
|
|
|
|
else
|
2012-06-07 02:00:00 +02:00
|
|
|
if idx == g:vimwiki_current_idx
|
|
|
|
let path = VimwikiGet('path')
|
|
|
|
else
|
|
|
|
let path = VimwikiGet('path', idx)
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
|
|
|
" For Issue 310. Otherwise current subdir is used for another wiki.
|
|
|
|
if idx == g:vimwiki_current_idx
|
|
|
|
let subdir = VimwikiGet('subdir')
|
|
|
|
else
|
|
|
|
let subdir = ""
|
|
|
|
endif
|
|
|
|
|
|
|
|
if a:as_html
|
|
|
|
let ext = '.html'
|
|
|
|
else
|
|
|
|
if idx == g:vimwiki_current_idx
|
|
|
|
let ext = VimwikiGet('ext')
|
|
|
|
else
|
|
|
|
let ext = VimwikiGet('ext', idx)
|
|
|
|
endif
|
2010-05-12 02:00:00 +02:00
|
|
|
endif
|
2010-02-23 01:00:00 +01:00
|
|
|
|
2012-06-07 02:00:00 +02:00
|
|
|
" default link for directories
|
2014-12-04 21:12:04 +01:00
|
|
|
if vimwiki#path#is_link_to_dir(lnk)
|
2015-02-09 20:24:18 +01:00
|
|
|
let ext = (g:vimwiki_dir_link != '' ? g:vimwiki_dir_link . ext : '')
|
2012-06-07 02:00:00 +02:00
|
|
|
endif
|
2015-02-09 20:47:11 +01:00
|
|
|
elseif scheme =~# 'diary'
|
2012-06-07 02:00:00 +02:00
|
|
|
if a:as_html
|
|
|
|
" use cached value (save time when converting diary index!)
|
|
|
|
let path = VimwikiGet('invsubdir')
|
|
|
|
let ext = '.html'
|
|
|
|
else
|
|
|
|
let path = VimwikiGet('path')
|
|
|
|
let ext = VimwikiGet('ext')
|
2010-02-23 01:00:00 +01:00
|
|
|
endif
|
2014-08-14 13:08:24 +02:00
|
|
|
let idx = g:vimwiki_current_idx
|
2012-06-07 02:00:00 +02:00
|
|
|
let subdir = VimwikiGet('diary_rel_path')
|
2015-02-09 20:47:11 +01:00
|
|
|
elseif scheme =~# 'local'
|
2012-06-07 02:00:00 +02:00
|
|
|
" revisiting the 'lcd'-bug ...
|
|
|
|
let path = VimwikiGet('path')
|
|
|
|
let subdir = VimwikiGet('subdir')
|
|
|
|
if a:as_html
|
|
|
|
" prepend browser-specific file: scheme
|
|
|
|
let path = 'file://'.fnamemodify(path, ":p")
|
|
|
|
endif
|
2015-02-09 20:47:11 +01:00
|
|
|
elseif scheme =~# 'file'
|
2012-06-07 02:00:00 +02:00
|
|
|
" RM repeated leading "/"'s within a link
|
|
|
|
let lnk = substitute(lnk, '^/*', '/', '')
|
|
|
|
" convert "/~..." into "~..." for fnamemodify
|
|
|
|
let lnk = substitute(lnk, '^/\~', '\~', '')
|
|
|
|
" convert /C: to C: (or fnamemodify(...":p:h") interpret it as C:\C:
|
|
|
|
if vimwiki#u#is_windows()
|
|
|
|
let lnk = substitute(lnk, '^/\ze[[:alpha:]]:', '', '')
|
|
|
|
endif
|
|
|
|
if a:as_html
|
|
|
|
" prepend browser-specific file: scheme
|
|
|
|
let path = 'file://'.fnamemodify(lnk, ":p:h").'/'
|
|
|
|
else
|
|
|
|
let path = fnamemodify(lnk, ":p:h").'/'
|
|
|
|
endif
|
|
|
|
let lnk = fnamemodify(lnk, ":p:t")
|
|
|
|
let subdir = ''
|
2010-02-23 01:00:00 +01:00
|
|
|
endif
|
|
|
|
|
2012-06-07 02:00:00 +02:00
|
|
|
|
|
|
|
" construct url from parts
|
|
|
|
if is_schemeless && a:as_html
|
|
|
|
let scheme = ''
|
|
|
|
let url = lnk.ext
|
|
|
|
else
|
|
|
|
let url = path.subdir.lnk.ext
|
2010-05-12 02:00:00 +02:00
|
|
|
endif
|
2012-06-07 02:00:00 +02:00
|
|
|
|
2014-08-18 09:16:41 +02:00
|
|
|
" lnk and url should be '' if the given wiki link has the form [[#anchor]].
|
|
|
|
" We cannot do lnk = expand('%:t:r') or so, because this function is called
|
|
|
|
" from vimwiki#html#WikiAll2HTML() for many files, so expand('%:t:r')
|
|
|
|
" doesn't give the currently processed file
|
|
|
|
if lnk == ''
|
|
|
|
let url = ''
|
|
|
|
endif
|
|
|
|
|
2012-06-07 02:00:00 +02:00
|
|
|
" result
|
2014-02-13 12:42:24 +01:00
|
|
|
return [idx, scheme, path, subdir, lnk, ext, url, anchor]
|
2012-06-07 02:00:00 +02:00
|
|
|
endfunction "}}}
|
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#system_open_link
|
2012-06-07 02:00:00 +02:00
|
|
|
function! vimwiki#base#system_open_link(url) "{{{
|
|
|
|
" handlers
|
|
|
|
function! s:win32_handler(url)
|
|
|
|
"http://vim.wikia.com/wiki/Opening_current_Vim_file_in_your_Windows_browser
|
2014-07-04 11:23:28 +02:00
|
|
|
"disable 'shellslash', otherwise the url will be enclosed in single quotes,
|
|
|
|
"which is problematic
|
|
|
|
"see https://github.com/vimwiki/vimwiki/issues/54#issuecomment-48011289
|
|
|
|
if exists('+shellslash')
|
|
|
|
let old_ssl = &shellslash
|
|
|
|
set noshellslash
|
|
|
|
let url = shellescape(a:url, 1)
|
|
|
|
let &shellslash = old_ssl
|
|
|
|
else
|
|
|
|
let url = shellescape(a:url, 1)
|
|
|
|
endif
|
|
|
|
execute 'silent ! start "Title" /B ' . url
|
2012-06-07 02:00:00 +02:00
|
|
|
endfunction
|
|
|
|
function! s:macunix_handler(url)
|
|
|
|
execute '!open ' . shellescape(a:url, 1)
|
|
|
|
endfunction
|
|
|
|
function! s:linux_handler(url)
|
2014-03-03 09:19:59 +01:00
|
|
|
call system('xdg-open ' . shellescape(a:url).' &')
|
2012-06-07 02:00:00 +02:00
|
|
|
endfunction
|
2014-02-24 12:16:23 +01:00
|
|
|
try
|
2012-06-07 02:00:00 +02:00
|
|
|
if vimwiki#u#is_windows()
|
|
|
|
call s:win32_handler(a:url)
|
|
|
|
return
|
|
|
|
elseif has("macunix")
|
|
|
|
call s:macunix_handler(a:url)
|
|
|
|
return
|
|
|
|
else
|
|
|
|
call s:linux_handler(a:url)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
endtry
|
|
|
|
echomsg 'Default Vimwiki link handler was unable to open the HTML file!'
|
|
|
|
endfunction "}}}
|
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#open_link
|
2012-06-07 02:00:00 +02:00
|
|
|
function! vimwiki#base#open_link(cmd, link, ...) "{{{
|
2015-03-13 16:31:15 +01:00
|
|
|
let link_infos = vimwiki#base#resolve_link(a:link)
|
2012-06-07 02:00:00 +02:00
|
|
|
|
2015-03-13 16:31:15 +01:00
|
|
|
if g:vimwiki_debug
|
|
|
|
echom 'open_link:' string(link_infos)
|
2014-09-11 10:36:44 +02:00
|
|
|
endif
|
|
|
|
|
2015-03-13 16:31:15 +01:00
|
|
|
if link_infos.filename == ''
|
2012-06-07 02:00:00 +02:00
|
|
|
echom 'Vimwiki Error: Unable to resolve link!'
|
|
|
|
return
|
2010-05-12 02:00:00 +02:00
|
|
|
endif
|
|
|
|
|
2015-03-13 16:31:15 +01:00
|
|
|
let is_wiki_link = link_infos.scheme =~# '\mwiki\d\+'
|
|
|
|
\ || link_infos.scheme =~# 'diary'
|
2010-05-12 02:00:00 +02:00
|
|
|
|
2015-03-13 16:31:15 +01:00
|
|
|
let update_prev_link = is_wiki_link &&
|
|
|
|
\ !vimwiki#path#is_equal(link_infos.filename, expand('%:p'))
|
2010-05-12 02:00:00 +02:00
|
|
|
|
2012-06-07 02:00:00 +02:00
|
|
|
let vimwiki_prev_link = []
|
|
|
|
" update previous link for wiki pages
|
|
|
|
if update_prev_link
|
|
|
|
if a:0
|
|
|
|
let vimwiki_prev_link = [a:1, []]
|
2015-02-09 20:05:25 +01:00
|
|
|
elseif &ft ==# 'vimwiki'
|
2012-06-07 02:00:00 +02:00
|
|
|
let vimwiki_prev_link = [expand('%:p'), getpos('.')]
|
|
|
|
endif
|
2010-05-12 02:00:00 +02:00
|
|
|
endif
|
|
|
|
|
2012-06-07 02:00:00 +02:00
|
|
|
" open/edit
|
2015-03-13 16:31:15 +01:00
|
|
|
if is_wiki_link
|
|
|
|
call vimwiki#base#edit_file(a:cmd, link_infos.filename, link_infos.anchor,
|
2012-06-07 02:00:00 +02:00
|
|
|
\ vimwiki_prev_link, update_prev_link)
|
2015-03-13 16:31:15 +01:00
|
|
|
if link_infos.index != g:vimwiki_current_idx
|
2012-06-07 02:00:00 +02:00
|
|
|
" this call to setup_buffer_state may not be necessary
|
2015-03-13 16:31:15 +01:00
|
|
|
call vimwiki#base#setup_buffer_state(link_infos.index)
|
2012-06-07 02:00:00 +02:00
|
|
|
endif
|
2015-03-13 16:31:15 +01:00
|
|
|
else
|
|
|
|
call vimwiki#base#system_open_link(link_infos.filename)
|
2012-06-07 02:00:00 +02:00
|
|
|
endif
|
2012-07-07 02:00:00 +02:00
|
|
|
endfunction " }}}
|
2012-06-07 02:00:00 +02:00
|
|
|
|
2014-11-11 21:12:03 +01:00
|
|
|
" vimwiki#base#get_globlinks_escaped
|
|
|
|
function! vimwiki#base#get_globlinks_escaped() abort "{{{only get links from the current dir
|
2012-06-07 02:00:00 +02:00
|
|
|
" change to the directory of the current file
|
|
|
|
let orig_pwd = getcwd()
|
|
|
|
lcd! %:h
|
2013-04-19 05:46:58 +02:00
|
|
|
" all path are relative to the current file's location
|
2012-06-07 02:00:00 +02:00
|
|
|
let globlinks = glob('*'.VimwikiGet('ext'),1)."\n"
|
|
|
|
" remove extensions
|
|
|
|
let globlinks = substitute(globlinks, '\'.VimwikiGet('ext').'\ze\n', '', 'g')
|
|
|
|
" restore the original working directory
|
|
|
|
exe 'lcd! '.orig_pwd
|
2014-11-11 21:12:03 +01:00
|
|
|
" convert to a List
|
2014-11-10 21:04:06 +01:00
|
|
|
let lst = split(globlinks, '\n')
|
2014-11-11 21:12:03 +01:00
|
|
|
" Apply fnameescape() to each item
|
2014-11-10 21:04:06 +01:00
|
|
|
call map(lst, 'fnameescape(v:val)')
|
2014-11-11 21:12:03 +01:00
|
|
|
" Convert back to newline-separated list
|
2014-11-10 21:04:06 +01:00
|
|
|
let globlinks = join(lst, "\n")
|
2014-11-11 21:12:03 +01:00
|
|
|
" return all escaped links as a single newline-separated string
|
2014-11-10 21:04:06 +01:00
|
|
|
return globlinks
|
|
|
|
endfunction " }}}
|
|
|
|
|
2014-11-06 21:52:26 +01:00
|
|
|
" vimwiki#base#generate_links
|
2014-12-04 21:27:02 +01:00
|
|
|
function! vimwiki#base#generate_links() "{{{
|
2015-03-17 10:33:43 +01:00
|
|
|
let links = vimwiki#base#get_wikilinks(g:vimwiki_current_idx, 0)
|
2012-06-07 02:00:00 +02:00
|
|
|
|
|
|
|
call append(line('$'), substitute(g:vimwiki_rxH1_Template, '__Header__', 'Generated Links', ''))
|
|
|
|
|
2010-05-12 02:00:00 +02:00
|
|
|
call sort(links)
|
|
|
|
|
2012-06-07 02:00:00 +02:00
|
|
|
let bullet = repeat(' ', vimwiki#lst#get_list_margin()).
|
|
|
|
\ vimwiki#lst#default_symbol().' '
|
2010-05-12 02:00:00 +02:00
|
|
|
for link in links
|
2014-12-04 21:27:02 +01:00
|
|
|
let abs_filepath = vimwiki#path#abs_path_of_link(link)
|
|
|
|
if !s:is_diary_file(abs_filepath)
|
|
|
|
call append(line('$'), bullet.
|
|
|
|
\ substitute(g:vimwiki_WikiLinkTemplate1, '__LinkUrl__', '\='."'".link."'", ''))
|
|
|
|
endif
|
2010-05-12 02:00:00 +02:00
|
|
|
endfor
|
|
|
|
endfunction " }}}
|
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#goto
|
2014-02-13 12:42:24 +01:00
|
|
|
function! vimwiki#base#goto(...) "{{{
|
|
|
|
let key = a:1
|
|
|
|
let anchor = a:0 > 1 ? a:2 : ''
|
|
|
|
|
|
|
|
call vimwiki#base#edit_file(':e',
|
2014-12-04 21:27:02 +01:00
|
|
|
\ VimwikiGet('path') . key . VimwikiGet('ext'),
|
2014-02-13 12:42:24 +01:00
|
|
|
\ anchor)
|
2010-08-24 02:00:00 +02:00
|
|
|
endfunction "}}}
|
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#backlinks
|
2012-06-07 02:00:00 +02:00
|
|
|
function! vimwiki#base#backlinks() "{{{
|
2014-12-07 11:59:56 +01:00
|
|
|
let current_filename = expand("%:p")
|
|
|
|
let locations = []
|
|
|
|
for idx in range(len(g:vimwiki_list))
|
|
|
|
let syntax = VimwikiGet('syntax', idx)
|
|
|
|
let wikifiles = s:find_files(idx, 0)
|
|
|
|
for source_file in wikifiles
|
|
|
|
let links = s:get_links(source_file, idx)
|
|
|
|
for [target_file, _, lnum, col] in links
|
|
|
|
" don't include links from the current file to itself
|
2015-02-09 20:05:25 +01:00
|
|
|
if vimwiki#path#is_equal(target_file, current_filename) &&
|
|
|
|
\ !vimwiki#path#is_equal(target_file, source_file)
|
2014-12-07 11:59:56 +01:00
|
|
|
call add(locations, {'filename':source_file, 'lnum':lnum, 'col':col})
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
endfor
|
|
|
|
endfor
|
|
|
|
|
|
|
|
if empty(locations)
|
|
|
|
echom 'vimwiki: no other file links to this file'
|
|
|
|
else
|
|
|
|
call setloclist(0, locations, 'r')
|
|
|
|
lopen
|
|
|
|
endif
|
2010-05-12 02:00:00 +02:00
|
|
|
endfunction "}}}
|
|
|
|
|
2014-12-04 21:18:58 +01:00
|
|
|
" Returns: a list containing all files of the given wiki as absolute file path.
|
|
|
|
" If the given wiki number is negative, the diary of the current wiki is used
|
|
|
|
" If the second argument is not zero, only directories are found
|
|
|
|
function! s:find_files(wiki_nr, directories_only)
|
|
|
|
let wiki_nr = a:wiki_nr
|
|
|
|
if wiki_nr >= 0
|
|
|
|
let root_directory = VimwikiGet('path', wiki_nr)
|
|
|
|
else
|
|
|
|
let root_directory = VimwikiGet('path').VimwikiGet('diary_rel_path')
|
|
|
|
let wiki_nr = g:vimwiki_current_idx
|
|
|
|
endif
|
|
|
|
if a:directories_only
|
|
|
|
let ext = '/'
|
|
|
|
else
|
|
|
|
let ext = VimwikiGet('ext', wiki_nr)
|
|
|
|
endif
|
2010-08-24 02:00:00 +02:00
|
|
|
" if current wiki is temporary -- was added by an arbitrary wiki file then do
|
|
|
|
" not search wiki files in subdirectories. Or it would hang the system if
|
|
|
|
" wiki file was created in $HOME or C:/ dirs.
|
2014-12-04 21:18:58 +01:00
|
|
|
if VimwikiGet('temp', wiki_nr)
|
|
|
|
let pattern = '*'.ext
|
2010-08-24 02:00:00 +02:00
|
|
|
else
|
2014-12-04 21:18:58 +01:00
|
|
|
let pattern = '**/*'.ext
|
2010-08-24 02:00:00 +02:00
|
|
|
endif
|
2014-12-04 21:18:58 +01:00
|
|
|
return split(globpath(root_directory, pattern), '\n')
|
|
|
|
endfunction
|
|
|
|
|
2015-03-17 10:33:43 +01:00
|
|
|
" Returns: a list containing the links to get from the current file to all wiki
|
|
|
|
" files in the given wiki.
|
|
|
|
" If the given wiki number is negative, the diary of the current wiki is used.
|
|
|
|
" If also_absolute_links is nonzero, also return links of the form /file
|
|
|
|
function! vimwiki#base#get_wikilinks(wiki_nr, also_absolute_links)
|
2014-12-04 21:18:58 +01:00
|
|
|
let files = s:find_files(a:wiki_nr, 0)
|
|
|
|
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')
|
2012-06-07 02:00:00 +02:00
|
|
|
else
|
2014-12-04 21:18:58 +01:00
|
|
|
let cwd = VimwikiGet('path', a:wiki_nr)
|
2012-06-07 02:00:00 +02:00
|
|
|
endif
|
2014-12-04 21:18:58 +01:00
|
|
|
let result = []
|
|
|
|
for wikifile in files
|
|
|
|
let wikifile = fnamemodify(wikifile, ':r') " strip extension
|
|
|
|
let wikifile = vimwiki#path#relpath(cwd, wikifile)
|
|
|
|
call add(result, wikifile)
|
2012-06-07 02:00:00 +02:00
|
|
|
endfor
|
2015-03-17 10:33:43 +01:00
|
|
|
if a:also_absolute_links
|
|
|
|
for wikifile in files
|
|
|
|
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')
|
|
|
|
endif
|
|
|
|
let wikifile = fnamemodify(wikifile, ':r') " strip extension
|
|
|
|
let wikifile = '/'.vimwiki#path#relpath(cwd, wikifile)
|
|
|
|
call add(result, wikifile)
|
|
|
|
endfor
|
|
|
|
endif
|
2014-12-04 21:18:58 +01:00
|
|
|
return result
|
|
|
|
endfunction
|
|
|
|
|
2015-03-17 10:33:43 +01:00
|
|
|
" Returns: a list containing the links to all directories from the current file
|
2014-12-04 21:18:58 +01:00
|
|
|
function! vimwiki#base#get_wiki_directories(wiki_nr)
|
|
|
|
let dirs = s:find_files(a:wiki_nr, 1)
|
|
|
|
if a:wiki_nr == g:vimwiki_current_idx
|
|
|
|
let cwd = vimwiki#path#wikify_path(expand('%:p:h'))
|
2015-03-17 10:33:43 +01:00
|
|
|
let root_dir = VimwikiGet('path')
|
2014-12-04 21:18:58 +01:00
|
|
|
else
|
|
|
|
let cwd = VimwikiGet('path', a:wiki_nr)
|
2012-06-07 02:00:00 +02:00
|
|
|
endif
|
2014-12-04 21:18:58 +01:00
|
|
|
let result = ['./']
|
|
|
|
for wikidir in dirs
|
2015-03-17 10:33:43 +01:00
|
|
|
let wikidir_relative = vimwiki#path#relpath(cwd, wikidir).'/'
|
|
|
|
call add(result, wikidir_relative)
|
|
|
|
if a:wiki_nr == g:vimwiki_current_idx
|
|
|
|
let wikidir_absolute = '/'.vimwiki#path#relpath(root_dir, wikidir).'/'
|
|
|
|
call add(result, wikidir_absolute)
|
|
|
|
endif
|
2014-12-04 21:18:58 +01:00
|
|
|
endfor
|
|
|
|
return result
|
|
|
|
endfunction
|
2010-05-12 02:00:00 +02:00
|
|
|
|
2014-08-14 13:08:24 +02:00
|
|
|
function! vimwiki#base#get_anchors(filename, syntax) "{{{
|
|
|
|
if !filereadable(a:filename)
|
|
|
|
return []
|
|
|
|
endif
|
|
|
|
|
|
|
|
let rxheader = g:vimwiki_{a:syntax}_header_search
|
|
|
|
let rxbold = g:vimwiki_{a:syntax}_bold_search
|
|
|
|
|
|
|
|
let anchor_level = ['', '', '', '', '', '', '']
|
|
|
|
let anchors = []
|
2014-11-11 13:43:44 +01:00
|
|
|
let current_complete_anchor = ''
|
2014-08-14 13:08:24 +02:00
|
|
|
for line in readfile(a:filename)
|
|
|
|
|
|
|
|
" collect headers
|
|
|
|
let h_match = matchlist(line, rxheader)
|
|
|
|
if !empty(h_match)
|
|
|
|
let header = vimwiki#u#trim(h_match[2])
|
|
|
|
let level = len(h_match[1])
|
2014-11-11 13:43:44 +01:00
|
|
|
call add(anchors, header)
|
2014-08-14 13:08:24 +02:00
|
|
|
let anchor_level[level-1] = header
|
|
|
|
for l in range(level, 6)
|
|
|
|
let anchor_level[l] = ''
|
|
|
|
endfor
|
2014-11-11 13:43:44 +01:00
|
|
|
if level == 1
|
|
|
|
let current_complete_anchor = header
|
|
|
|
else
|
|
|
|
let current_complete_anchor = ''
|
|
|
|
for l in range(level-1)
|
|
|
|
if anchor_level[l] != ''
|
|
|
|
let current_complete_anchor .= anchor_level[l].'#'
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
let current_complete_anchor .= header
|
|
|
|
call add(anchors, current_complete_anchor)
|
|
|
|
endif
|
2014-08-14 13:08:24 +02:00
|
|
|
endif
|
|
|
|
|
|
|
|
" collect bold text (there can be several in one line)
|
2015-01-10 14:12:40 +01:00
|
|
|
let bold_count = 1
|
2014-08-14 13:08:24 +02:00
|
|
|
while 1
|
2014-11-11 13:43:44 +01:00
|
|
|
let bold_text = matchstr(line, rxbold, 0, bold_count)
|
|
|
|
if bold_text == ''
|
2014-08-14 13:08:24 +02:00
|
|
|
break
|
|
|
|
endif
|
|
|
|
call add(anchors, bold_text)
|
2014-11-11 13:43:44 +01:00
|
|
|
if current_complete_anchor != ''
|
|
|
|
call add(anchors, current_complete_anchor.'#'.bold_text)
|
|
|
|
endif
|
2014-08-14 13:08:24 +02:00
|
|
|
let bold_count += 1
|
|
|
|
endwhile
|
|
|
|
|
|
|
|
endfor
|
|
|
|
|
|
|
|
return anchors
|
|
|
|
endfunction "}}}
|
|
|
|
|
2014-02-18 14:47:42 +01:00
|
|
|
" s:jump_to_anchor
|
|
|
|
function! s:jump_to_anchor(anchor) "{{{
|
2014-02-13 12:42:24 +01:00
|
|
|
let oldpos = getpos('.')
|
|
|
|
call cursor(1, 1)
|
|
|
|
|
|
|
|
let anchor = vimwiki#u#escape(a:anchor)
|
|
|
|
|
|
|
|
let segments = split(anchor, '#', 0)
|
|
|
|
for segment in segments
|
|
|
|
|
|
|
|
let anchor_header = substitute(
|
|
|
|
\ g:vimwiki_{VimwikiGet('syntax')}_header_match,
|
|
|
|
\ '__Header__', "\\='".segment."'", '')
|
|
|
|
let anchor_bold = substitute(g:vimwiki_{VimwikiGet('syntax')}_bold_match,
|
|
|
|
\ '__Text__', "\\='".segment."'", '')
|
|
|
|
|
2014-08-18 09:38:45 +02:00
|
|
|
if !search(anchor_header, 'Wc') && !search(anchor_bold, 'Wc')
|
2014-02-13 12:42:24 +01:00
|
|
|
call setpos('.', oldpos)
|
|
|
|
break
|
|
|
|
endif
|
|
|
|
let oldpos = getpos('.')
|
|
|
|
endfor
|
2014-02-18 14:47:42 +01:00
|
|
|
endfunction "}}}
|
2014-02-13 12:42:24 +01:00
|
|
|
|
2014-12-07 11:58:08 +01:00
|
|
|
" Params: full path to a wiki file and its wiki number
|
|
|
|
" Returns: a list of all links inside the wiki file
|
|
|
|
" Every list item has the form
|
|
|
|
" [target file, anchor, line number of the link in source file, column number]
|
|
|
|
function! s:get_links(wikifile, idx) "{{{
|
2014-12-04 21:18:58 +01:00
|
|
|
if !filereadable(a:wikifile)
|
|
|
|
return []
|
|
|
|
endif
|
|
|
|
|
|
|
|
let syntax = VimwikiGet('syntax', a:idx)
|
2014-12-07 11:58:08 +01:00
|
|
|
let rx_link = g:vimwiki_{syntax}_wikilink
|
2014-12-04 21:18:58 +01:00
|
|
|
let links = []
|
|
|
|
let lnum = 0
|
|
|
|
|
|
|
|
for line in readfile(a:wikifile)
|
|
|
|
let lnum += 1
|
|
|
|
|
|
|
|
let link_count = 1
|
|
|
|
while 1
|
2014-12-07 11:58:08 +01:00
|
|
|
let col = match(line, rx_link, 0, link_count)+1
|
|
|
|
let link_text = matchstr(line, rx_link, 0, link_count)
|
2014-12-04 21:18:58 +01:00
|
|
|
if link_text == ''
|
|
|
|
break
|
|
|
|
endif
|
|
|
|
let link_count += 1
|
2015-03-13 16:31:15 +01:00
|
|
|
let target = vimwiki#base#resolve_link(link_text, a:wikifile)
|
|
|
|
if target.filename != ''
|
|
|
|
call add(links, [target.filename, target.anchor, lnum, col])
|
2014-12-04 21:18:58 +01:00
|
|
|
endif
|
|
|
|
endwhile
|
|
|
|
endfor
|
|
|
|
|
|
|
|
return links
|
2014-12-07 11:58:08 +01:00
|
|
|
endfunction "}}}
|
2014-12-04 21:18:58 +01:00
|
|
|
|
2014-12-07 11:58:08 +01:00
|
|
|
function! vimwiki#base#check_links() "{{{
|
2014-12-04 21:18:58 +01:00
|
|
|
let anchors_of_files = {}
|
|
|
|
let links_of_files = {}
|
|
|
|
let errors = []
|
|
|
|
for idx in range(len(g:vimwiki_list))
|
|
|
|
let syntax = VimwikiGet('syntax', idx)
|
|
|
|
let wikifiles = s:find_files(idx, 0)
|
|
|
|
for wikifile in wikifiles
|
2014-12-07 11:58:08 +01:00
|
|
|
let links_of_files[wikifile] = s:get_links(wikifile, idx)
|
2014-12-04 21:18:58 +01:00
|
|
|
let anchors_of_files[wikifile] = vimwiki#base#get_anchors(wikifile, syntax)
|
|
|
|
endfor
|
|
|
|
endfor
|
|
|
|
|
|
|
|
for wikifile in keys(links_of_files)
|
|
|
|
for [target_file, target_anchor, lnum, col] in links_of_files[wikifile]
|
|
|
|
if target_file == '' && target_anchor == ''
|
|
|
|
call add(errors, {'filename':wikifile, 'lnum':lnum, 'col':col,
|
|
|
|
\ 'text': "numbered scheme refers to a non-existent wiki"})
|
|
|
|
elseif has_key(anchors_of_files, target_file)
|
|
|
|
if target_anchor != '' && index(anchors_of_files[target_file], target_anchor) < 0
|
|
|
|
call add(errors, {'filename':wikifile, 'lnum':lnum, 'col':col,
|
|
|
|
\'text': "there is no such anchor: ".target_anchor})
|
|
|
|
endif
|
|
|
|
else
|
2015-03-19 13:22:28 +01:00
|
|
|
if target_file =~ '/$' " maybe it's a link to a directory
|
|
|
|
if !isdirectory(target_file)
|
|
|
|
call add(errors, {'filename':wikifile, 'lnum':lnum, 'col':col,
|
|
|
|
\'text': "there is no such directory: ".target_file})
|
|
|
|
endif
|
2014-12-04 21:18:58 +01:00
|
|
|
else
|
2015-03-19 13:22:28 +01:00
|
|
|
if filereadable(target_file) " maybe it's a non-wiki file
|
|
|
|
let anchors_of_files[target_file] = []
|
|
|
|
else
|
|
|
|
call add(errors, {'filename':wikifile, 'lnum':lnum, 'col':col,
|
|
|
|
\'text': "there is no such file: ".target_file})
|
|
|
|
endif
|
2014-12-04 21:18:58 +01:00
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
endfor
|
|
|
|
|
|
|
|
let reachable_wikifiles = {}
|
|
|
|
for wikifile in keys(links_of_files)
|
|
|
|
let reachable_wikifiles[wikifile] = 0
|
|
|
|
endfor
|
|
|
|
for idx in range(len(g:vimwiki_list))
|
|
|
|
let index_file = VimwikiGet('path', idx) . VimwikiGet('index', idx) .
|
|
|
|
\ VimwikiGet('ext', idx)
|
|
|
|
let reachable_wikifiles[index_file] = 1
|
|
|
|
endfor
|
|
|
|
while 1
|
|
|
|
let visit_wikifile = ''
|
|
|
|
for wf in keys(reachable_wikifiles)
|
|
|
|
if reachable_wikifiles[wf] == 1
|
|
|
|
let visit_wikifile = wf
|
|
|
|
let reachable_wikifiles[wf] = 2
|
|
|
|
break
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
if visit_wikifile == ''
|
|
|
|
break
|
|
|
|
endif
|
|
|
|
for [target_file, target_anchor, lnum, col] in links_of_files[visit_wikifile]
|
|
|
|
if has_key(reachable_wikifiles, target_file) && reachable_wikifiles[target_file] == 0
|
|
|
|
let reachable_wikifiles[target_file] = 1
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
endwhile
|
|
|
|
for wf in keys(reachable_wikifiles)
|
|
|
|
if reachable_wikifiles[wf] == 0
|
|
|
|
call add(errors, {'text':wf." is not reachable from the index file"})
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
|
|
|
if empty(errors)
|
|
|
|
echom 'vimwiki: all links are OK'
|
|
|
|
else
|
|
|
|
call setqflist(errors, 'r')
|
|
|
|
copen
|
|
|
|
endif
|
2014-12-07 11:58:08 +01:00
|
|
|
endfunction "}}}
|
2014-12-04 21:18:58 +01:00
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#edit_file
|
2014-02-13 12:42:24 +01:00
|
|
|
function! vimwiki#base#edit_file(command, filename, anchor, ...) "{{{
|
2012-06-07 02:00:00 +02:00
|
|
|
" XXX: Should we allow * in filenames!?
|
|
|
|
" Maxim: It is allowed, escaping here is for vim to be able to open files
|
|
|
|
" which have that symbols.
|
|
|
|
" Try to remove * from escaping and open&save :
|
|
|
|
" [[testBLAfile]]...
|
|
|
|
" then
|
|
|
|
" [[test*file]]...
|
|
|
|
" you'll have E77: Too many file names
|
2013-04-19 05:46:58 +02:00
|
|
|
let fname = escape(a:filename, '% *|#')
|
2012-06-07 02:00:00 +02:00
|
|
|
let dir = fnamemodify(a:filename, ":p:h")
|
2014-12-04 21:12:04 +01:00
|
|
|
|
|
|
|
let ok = vimwiki#path#mkdir(dir, 1)
|
|
|
|
|
|
|
|
if !ok
|
2012-06-07 02:00:00 +02:00
|
|
|
echom ' '
|
|
|
|
echom 'Vimwiki: Unable to edit file in non-existent directory: '.dir
|
2014-12-04 21:12:04 +01:00
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
" check if the file we want to open is already the current file
|
|
|
|
" which happens if we jump to an achor in the current file.
|
|
|
|
" This hack is necessary because apparently Vim messes up the result of
|
|
|
|
" getpos() directly after this command. Strange.
|
2015-02-09 20:05:25 +01:00
|
|
|
if !(a:command ==# ':e ' && vimwiki#path#is_equal(a:filename, expand('%:p')))
|
2014-12-04 21:12:04 +01:00
|
|
|
execute a:command.' '.fname
|
|
|
|
endif
|
|
|
|
if a:anchor != ''
|
|
|
|
call s:jump_to_anchor(a:anchor)
|
2010-01-20 01:00:00 +01:00
|
|
|
endif
|
|
|
|
|
2012-06-07 02:00:00 +02:00
|
|
|
" save previous link
|
|
|
|
" a:1 -- previous vimwiki link to save
|
|
|
|
" a:2 -- should we update previous link
|
|
|
|
if a:0 && a:2 && len(a:1) > 0
|
|
|
|
let b:vimwiki_prev_link = a:1
|
2010-01-20 01:00:00 +01:00
|
|
|
endif
|
2012-07-07 02:00:00 +02:00
|
|
|
endfunction " }}}
|
2010-01-20 01:00:00 +01:00
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#search_word
|
2012-06-07 02:00:00 +02:00
|
|
|
function! vimwiki#base#search_word(wikiRx, cmd) "{{{
|
2010-01-20 01:00:00 +01:00
|
|
|
let match_line = search(a:wikiRx, 's'.a:cmd)
|
|
|
|
if match_line == 0
|
2012-06-07 02:00:00 +02:00
|
|
|
echomsg 'vimwiki: Wiki link not found.'
|
2010-01-20 01:00:00 +01:00
|
|
|
endif
|
2012-07-07 02:00:00 +02:00
|
|
|
endfunction " }}}
|
2010-01-20 01:00:00 +01:00
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#matchstr_at_cursor
|
2012-06-07 02:00:00 +02:00
|
|
|
" Returns part of the line that matches wikiRX at cursor
|
|
|
|
function! vimwiki#base#matchstr_at_cursor(wikiRX) "{{{
|
2010-01-20 01:00:00 +01:00
|
|
|
let col = col('.') - 1
|
|
|
|
let line = getline('.')
|
|
|
|
let ebeg = -1
|
|
|
|
let cont = match(line, a:wikiRX, 0)
|
|
|
|
while (ebeg >= 0 || (0 <= cont) && (cont <= col))
|
|
|
|
let contn = matchend(line, a:wikiRX, cont)
|
|
|
|
if (cont <= col) && (col < contn)
|
|
|
|
let ebeg = match(line, a:wikiRX, cont)
|
|
|
|
let elen = contn - ebeg
|
|
|
|
break
|
|
|
|
else
|
|
|
|
let cont = match(line, a:wikiRX, contn)
|
|
|
|
endif
|
|
|
|
endwh
|
|
|
|
if ebeg >= 0
|
|
|
|
return strpart(line, ebeg, elen)
|
|
|
|
else
|
|
|
|
return ""
|
|
|
|
endif
|
|
|
|
endf "}}}
|
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#replacestr_at_cursor
|
2012-06-07 02:00:00 +02:00
|
|
|
function! vimwiki#base#replacestr_at_cursor(wikiRX, sub) "{{{
|
|
|
|
let col = col('.') - 1
|
|
|
|
let line = getline('.')
|
|
|
|
let ebeg = -1
|
|
|
|
let cont = match(line, a:wikiRX, 0)
|
|
|
|
while (ebeg >= 0 || (0 <= cont) && (cont <= col))
|
|
|
|
let contn = matchend(line, a:wikiRX, cont)
|
|
|
|
if (cont <= col) && (col < contn)
|
|
|
|
let ebeg = match(line, a:wikiRX, cont)
|
|
|
|
let elen = contn - ebeg
|
|
|
|
break
|
|
|
|
else
|
|
|
|
let cont = match(line, a:wikiRX, contn)
|
2010-01-20 01:00:00 +01:00
|
|
|
endif
|
2012-06-07 02:00:00 +02:00
|
|
|
endwh
|
|
|
|
if ebeg >= 0
|
|
|
|
" TODO: There might be problems with Unicode chars...
|
|
|
|
let newline = strpart(line, 0, ebeg).a:sub.strpart(line, ebeg+elen)
|
|
|
|
call setline(line('.'), newline)
|
2010-01-20 01:00:00 +01:00
|
|
|
endif
|
2012-06-07 02:00:00 +02:00
|
|
|
endf "}}}
|
2010-01-20 01:00:00 +01:00
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" s:print_wiki_list
|
2010-01-20 01:00:00 +01:00
|
|
|
function! s:print_wiki_list() "{{{
|
|
|
|
let idx = 0
|
|
|
|
while idx < len(g:vimwiki_list)
|
|
|
|
if idx == g:vimwiki_current_idx
|
|
|
|
let sep = ' * '
|
|
|
|
echohl PmenuSel
|
|
|
|
else
|
|
|
|
let sep = ' '
|
|
|
|
echohl None
|
|
|
|
endif
|
|
|
|
echo (idx + 1).sep.VimwikiGet('path', idx)
|
|
|
|
let idx += 1
|
|
|
|
endwhile
|
|
|
|
echohl None
|
2012-07-07 02:00:00 +02:00
|
|
|
endfunction " }}}
|
2010-01-20 01:00:00 +01:00
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" s:update_wiki_link
|
2010-01-20 01:00:00 +01:00
|
|
|
function! s:update_wiki_link(fname, old, new) " {{{
|
|
|
|
echo "Updating links in ".a:fname
|
|
|
|
let has_updates = 0
|
|
|
|
let dest = []
|
|
|
|
for line in readfile(a:fname)
|
|
|
|
if !has_updates && match(line, a:old) != -1
|
|
|
|
let has_updates = 1
|
|
|
|
endif
|
2012-06-07 02:00:00 +02:00
|
|
|
" XXX: any other characters to escape!?
|
2010-01-20 01:00:00 +01:00
|
|
|
call add(dest, substitute(line, a:old, escape(a:new, "&"), "g"))
|
|
|
|
endfor
|
|
|
|
" add exception handling...
|
|
|
|
if has_updates
|
|
|
|
call rename(a:fname, a:fname.'#vimwiki_upd#')
|
|
|
|
call writefile(dest, a:fname)
|
|
|
|
call delete(a:fname.'#vimwiki_upd#')
|
|
|
|
endif
|
2012-07-07 02:00:00 +02:00
|
|
|
endfunction " }}}
|
2010-01-20 01:00:00 +01:00
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" s:update_wiki_links_dir
|
2010-01-20 01:00:00 +01:00
|
|
|
function! s:update_wiki_links_dir(dir, old_fname, new_fname) " {{{
|
|
|
|
let old_fname = substitute(a:old_fname, '[/\\]', '[/\\\\]', 'g')
|
|
|
|
let new_fname = a:new_fname
|
|
|
|
|
2014-06-19 15:36:11 +02:00
|
|
|
let old_fname_r = vimwiki#base#apply_template(
|
|
|
|
\ g:vimwiki_WikiLinkMatchUrlTemplate, old_fname, '', '')
|
2010-08-24 02:00:00 +02:00
|
|
|
|
2010-01-20 01:00:00 +01:00
|
|
|
let files = split(glob(VimwikiGet('path').a:dir.'*'.VimwikiGet('ext')), '\n')
|
|
|
|
for fname in files
|
2014-06-19 15:36:11 +02:00
|
|
|
call s:update_wiki_link(fname, old_fname_r, new_fname)
|
2010-01-20 01:00:00 +01:00
|
|
|
endfor
|
2012-07-07 02:00:00 +02:00
|
|
|
endfunction " }}}
|
2010-01-20 01:00:00 +01:00
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" s:tail_name
|
2010-01-20 01:00:00 +01:00
|
|
|
function! s:tail_name(fname) "{{{
|
|
|
|
let result = substitute(a:fname, ":", "__colon__", "g")
|
|
|
|
let result = fnamemodify(result, ":t:r")
|
|
|
|
let result = substitute(result, "__colon__", ":", "g")
|
|
|
|
return result
|
|
|
|
endfunction "}}}
|
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" s:update_wiki_links
|
2010-01-20 01:00:00 +01:00
|
|
|
function! s:update_wiki_links(old_fname, new_fname) " {{{
|
2013-08-17 18:10:00 +02:00
|
|
|
let old_fname = a:old_fname
|
|
|
|
let new_fname = a:new_fname
|
2010-01-20 01:00:00 +01:00
|
|
|
|
|
|
|
let subdirs = split(a:old_fname, '[/\\]')[: -2]
|
|
|
|
|
|
|
|
" TODO: Use Dictionary here...
|
|
|
|
let dirs_keys = ['']
|
|
|
|
let dirs_vals = ['']
|
|
|
|
if len(subdirs) > 0
|
|
|
|
let dirs_keys = ['']
|
|
|
|
let dirs_vals = [join(subdirs, '/').'/']
|
|
|
|
let idx = 0
|
|
|
|
while idx < len(subdirs) - 1
|
|
|
|
call add(dirs_keys, join(subdirs[: idx], '/').'/')
|
|
|
|
call add(dirs_vals, join(subdirs[idx+1 :], '/').'/')
|
|
|
|
let idx = idx + 1
|
|
|
|
endwhile
|
|
|
|
call add(dirs_keys,join(subdirs, '/').'/')
|
|
|
|
call add(dirs_vals, '')
|
|
|
|
endif
|
|
|
|
|
|
|
|
let idx = 0
|
|
|
|
while idx < len(dirs_keys)
|
|
|
|
let dir = dirs_keys[idx]
|
|
|
|
let new_dir = dirs_vals[idx]
|
2013-04-19 05:46:58 +02:00
|
|
|
call s:update_wiki_links_dir(dir,
|
2010-01-20 01:00:00 +01:00
|
|
|
\ new_dir.old_fname, new_dir.new_fname)
|
|
|
|
let idx = idx + 1
|
|
|
|
endwhile
|
2010-08-24 02:00:00 +02:00
|
|
|
endfunction " }}}
|
2010-01-20 01:00:00 +01:00
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" s:get_wiki_buffers
|
2010-01-20 01:00:00 +01:00
|
|
|
function! s:get_wiki_buffers() "{{{
|
|
|
|
let blist = []
|
|
|
|
let bcount = 1
|
|
|
|
while bcount<=bufnr("$")
|
|
|
|
if bufexists(bcount)
|
|
|
|
let bname = fnamemodify(bufname(bcount), ":p")
|
2015-02-09 20:47:11 +01:00
|
|
|
if bname =~# VimwikiGet('ext')."$"
|
2010-02-23 01:00:00 +01:00
|
|
|
let bitem = [bname, getbufvar(bname, "vimwiki_prev_link")]
|
2010-01-20 01:00:00 +01:00
|
|
|
call add(blist, bitem)
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
let bcount = bcount + 1
|
|
|
|
endwhile
|
|
|
|
return blist
|
2010-08-24 02:00:00 +02:00
|
|
|
endfunction " }}}
|
2010-01-20 01:00:00 +01:00
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" s:open_wiki_buffer
|
2010-01-20 01:00:00 +01:00
|
|
|
function! s:open_wiki_buffer(item) "{{{
|
2014-02-13 12:42:24 +01:00
|
|
|
call vimwiki#base#edit_file(':e', a:item[0], '')
|
2010-01-20 01:00:00 +01:00
|
|
|
if !empty(a:item[1])
|
2010-02-23 01:00:00 +01:00
|
|
|
call setbufvar(a:item[0], "vimwiki_prev_link", a:item[1])
|
2010-01-20 01:00:00 +01:00
|
|
|
endif
|
2010-08-24 02:00:00 +02:00
|
|
|
endfunction " }}}
|
2010-01-20 01:00:00 +01:00
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#nested_syntax
|
2011-06-11 02:00:00 +02:00
|
|
|
function! vimwiki#base#nested_syntax(filetype, start, end, textSnipHl) abort "{{{
|
2010-01-20 01:00:00 +01:00
|
|
|
" From http://vim.wikia.com/wiki/VimTip857
|
|
|
|
let ft=toupper(a:filetype)
|
|
|
|
let group='textGroup'.ft
|
|
|
|
if exists('b:current_syntax')
|
|
|
|
let s:current_syntax=b:current_syntax
|
|
|
|
" Remove current syntax definition, as some syntax files (e.g. cpp.vim)
|
|
|
|
" do nothing if b:current_syntax is defined.
|
|
|
|
unlet b:current_syntax
|
|
|
|
endif
|
|
|
|
|
|
|
|
" Some syntax files set up iskeyword which might scratch vimwiki a bit.
|
|
|
|
" Let us save and restore it later.
|
|
|
|
" let b:skip_set_iskeyword = 1
|
|
|
|
let is_keyword = &iskeyword
|
|
|
|
|
|
|
|
try
|
2012-07-07 02:00:00 +02:00
|
|
|
" keep going even if syntax file is not found
|
|
|
|
execute 'syntax include @'.group.' syntax/'.a:filetype.'.vim'
|
2010-01-20 01:00:00 +01:00
|
|
|
execute 'syntax include @'.group.' after/syntax/'.a:filetype.'.vim'
|
|
|
|
catch
|
|
|
|
endtry
|
|
|
|
|
|
|
|
let &iskeyword = is_keyword
|
|
|
|
|
|
|
|
if exists('s:current_syntax')
|
|
|
|
let b:current_syntax=s:current_syntax
|
|
|
|
else
|
|
|
|
unlet b:current_syntax
|
|
|
|
endif
|
2010-08-24 02:00:00 +02:00
|
|
|
execute 'syntax region textSnip'.ft.
|
|
|
|
\ ' matchgroup='.a:textSnipHl.
|
|
|
|
\ ' start="'.a:start.'" end="'.a:end.'"'.
|
|
|
|
\ ' contains=@'.group.' keepend'
|
|
|
|
|
|
|
|
" A workaround to Issue 115: Nested Perl syntax highlighting differs from
|
|
|
|
" regular one.
|
|
|
|
" Perl syntax file has perlFunctionName which is usually has no effect due to
|
|
|
|
" 'contained' flag. Now we have 'syntax include' that makes all the groups
|
2013-04-19 05:46:58 +02:00
|
|
|
" included as 'contained' into specific group.
|
2010-08-24 02:00:00 +02:00
|
|
|
" Here perlFunctionName (with quite an angry regexp "\h\w*[^:]") clashes with
|
|
|
|
" the rest syntax rules as now it has effect being really 'contained'.
|
|
|
|
" Clear it!
|
2015-02-09 20:47:11 +01:00
|
|
|
if ft =~? 'perl'
|
2013-04-19 05:46:58 +02:00
|
|
|
syntax clear perlFunctionName
|
2010-08-24 02:00:00 +02:00
|
|
|
endif
|
2010-01-20 01:00:00 +01:00
|
|
|
endfunction "}}}
|
|
|
|
|
2012-06-07 02:00:00 +02:00
|
|
|
" WIKI link following functions {{{
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#find_next_link
|
2011-06-11 02:00:00 +02:00
|
|
|
function! vimwiki#base#find_next_link() "{{{
|
2012-06-07 02:00:00 +02:00
|
|
|
call vimwiki#base#search_word(g:vimwiki_rxAnyLink, '')
|
2012-07-07 02:00:00 +02:00
|
|
|
endfunction " }}}
|
2010-01-20 01:00:00 +01:00
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#find_prev_link
|
2011-06-11 02:00:00 +02:00
|
|
|
function! vimwiki#base#find_prev_link() "{{{
|
2014-01-06 13:37:33 +01:00
|
|
|
"Jump 2 times if the cursor is in the middle of a link
|
2015-02-09 20:47:11 +01:00
|
|
|
if synIDattr(synID(line('.'), col('.'), 0), "name") =~# "VimwikiLink.*" &&
|
|
|
|
\ synIDattr(synID(line('.'), col('.')-1, 0), "name") =~# "VimwikiLink.*"
|
2014-01-06 13:37:33 +01:00
|
|
|
call vimwiki#base#search_word(g:vimwiki_rxAnyLink, 'b')
|
|
|
|
endif
|
2012-06-07 02:00:00 +02:00
|
|
|
call vimwiki#base#search_word(g:vimwiki_rxAnyLink, 'b')
|
2012-07-07 02:00:00 +02:00
|
|
|
endfunction " }}}
|
2010-01-20 01:00:00 +01:00
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#follow_link
|
2013-04-19 05:46:58 +02:00
|
|
|
function! vimwiki#base#follow_link(split, ...) "{{{ Parse link at cursor and pass
|
2012-06-07 02:00:00 +02:00
|
|
|
" to VimwikiLinkHandler, or failing that, the default open_link handler
|
2012-07-07 02:00:00 +02:00
|
|
|
if exists('*vimwiki#'.VimwikiGet('syntax').'_base#follow_link')
|
2012-06-07 02:00:00 +02:00
|
|
|
" Syntax-specific links
|
|
|
|
" XXX: @Stuart: do we still need it?
|
|
|
|
" XXX: @Maxim: most likely! I am still working on a seemless way to
|
|
|
|
" integrate regexp's without complicating syntax/vimwiki.vim
|
|
|
|
if a:0
|
2012-07-07 02:00:00 +02:00
|
|
|
call vimwiki#{VimwikiGet('syntax')}_base#follow_link(a:split, a:1)
|
2012-06-07 02:00:00 +02:00
|
|
|
else
|
2012-07-07 02:00:00 +02:00
|
|
|
call vimwiki#{VimwikiGet('syntax')}_base#follow_link(a:split)
|
2012-06-07 02:00:00 +02:00
|
|
|
endif
|
2010-01-20 01:00:00 +01:00
|
|
|
else
|
2015-02-09 20:05:25 +01:00
|
|
|
if a:split ==# "split"
|
2012-06-07 02:00:00 +02:00
|
|
|
let cmd = ":split "
|
2015-02-09 20:05:25 +01:00
|
|
|
elseif a:split ==# "vsplit"
|
2012-06-07 02:00:00 +02:00
|
|
|
let cmd = ":vsplit "
|
2015-02-09 20:05:25 +01:00
|
|
|
elseif a:split ==# "tabnew"
|
2012-06-07 02:00:00 +02:00
|
|
|
let cmd = ":tabnew "
|
|
|
|
else
|
|
|
|
let cmd = ":e "
|
|
|
|
endif
|
|
|
|
|
|
|
|
" try WikiLink
|
|
|
|
let lnk = matchstr(vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWikiLink),
|
|
|
|
\ g:vimwiki_rxWikiLinkMatchUrl)
|
|
|
|
" try WikiIncl
|
|
|
|
if lnk == ""
|
|
|
|
let lnk = matchstr(vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWikiIncl),
|
|
|
|
\ g:vimwiki_rxWikiInclMatchUrl)
|
|
|
|
endif
|
|
|
|
" try Weblink
|
|
|
|
if lnk == ""
|
|
|
|
let lnk = matchstr(vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWeblink),
|
|
|
|
\ g:vimwiki_rxWeblinkMatchUrl)
|
|
|
|
endif
|
2010-01-20 01:00:00 +01:00
|
|
|
|
2012-06-07 02:00:00 +02:00
|
|
|
if lnk != ""
|
|
|
|
if !VimwikiLinkHandler(lnk)
|
|
|
|
call vimwiki#base#open_link(cmd, lnk)
|
|
|
|
endif
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
if a:0 > 0
|
|
|
|
execute "normal! ".a:1
|
2013-04-19 05:46:58 +02:00
|
|
|
else
|
2012-06-07 02:00:00 +02:00
|
|
|
call vimwiki#base#normalize_link(0)
|
2010-01-20 01:00:00 +01:00
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
2010-08-24 02:00:00 +02:00
|
|
|
endfunction " }}}
|
2010-01-20 01:00:00 +01:00
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#go_back_link
|
2011-06-11 02:00:00 +02:00
|
|
|
function! vimwiki#base#go_back_link() "{{{
|
2010-02-23 01:00:00 +01:00
|
|
|
if exists("b:vimwiki_prev_link")
|
2012-06-07 02:00:00 +02:00
|
|
|
" go back to saved wiki link
|
2010-02-23 01:00:00 +01:00
|
|
|
let prev_word = b:vimwiki_prev_link
|
2010-01-20 01:00:00 +01:00
|
|
|
execute ":e ".substitute(prev_word[0], '\s', '\\\0', 'g')
|
|
|
|
call setpos('.', prev_word[1])
|
|
|
|
endif
|
2010-08-24 02:00:00 +02:00
|
|
|
endfunction " }}}
|
2010-01-20 01:00:00 +01:00
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#goto_index
|
2012-06-07 02:00:00 +02:00
|
|
|
function! vimwiki#base#goto_index(wnum, ...) "{{{
|
|
|
|
if a:wnum > len(g:vimwiki_list)
|
|
|
|
echom "vimwiki: Wiki ".a:wnum." is not registered in g:vimwiki_list!"
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
" usually a:wnum is greater then 0 but with the following command it is == 0:
|
|
|
|
" vim -n -c "exe 'VimwikiIndex' | echo g:vimwiki_current_idx"
|
|
|
|
if a:wnum > 0
|
|
|
|
let idx = a:wnum - 1
|
|
|
|
else
|
|
|
|
let idx = 0
|
|
|
|
endif
|
|
|
|
|
|
|
|
if a:0
|
|
|
|
let cmd = 'tabedit'
|
|
|
|
else
|
|
|
|
let cmd = 'edit'
|
|
|
|
endif
|
|
|
|
|
|
|
|
if g:vimwiki_debug == 3
|
|
|
|
echom "--- Goto_index g:curr_idx=".g:vimwiki_current_idx." ww_idx=".idx.""
|
|
|
|
endif
|
|
|
|
|
|
|
|
call vimwiki#base#edit_file(cmd,
|
|
|
|
\ VimwikiGet('path', idx).VimwikiGet('index', idx).
|
2014-02-13 12:42:24 +01:00
|
|
|
\ VimwikiGet('ext', idx),
|
|
|
|
\ '')
|
2012-06-07 02:00:00 +02:00
|
|
|
call vimwiki#base#setup_buffer_state(idx)
|
2010-08-24 02:00:00 +02:00
|
|
|
endfunction "}}}
|
2010-01-20 01:00:00 +01:00
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#delete_link
|
2011-06-11 02:00:00 +02:00
|
|
|
function! vimwiki#base#delete_link() "{{{
|
2010-01-20 01:00:00 +01:00
|
|
|
"" file system funcs
|
2012-06-07 02:00:00 +02:00
|
|
|
"" Delete wiki link you are in from filesystem
|
2015-02-07 22:01:20 +01:00
|
|
|
let val = input('Delete "'.expand('%').'" [y]es/[N]o? ')
|
|
|
|
if val !~? '^y'
|
2010-01-20 01:00:00 +01:00
|
|
|
return
|
|
|
|
endif
|
|
|
|
let fname = expand('%:p')
|
|
|
|
try
|
|
|
|
call delete(fname)
|
|
|
|
catch /.*/
|
|
|
|
echomsg 'vimwiki: Cannot delete "'.expand('%:t:r').'"!'
|
|
|
|
return
|
|
|
|
endtry
|
2012-06-07 02:00:00 +02:00
|
|
|
|
|
|
|
call vimwiki#base#go_back_link()
|
2010-01-20 01:00:00 +01:00
|
|
|
execute "bdelete! ".escape(fname, " ")
|
|
|
|
|
2012-06-07 02:00:00 +02:00
|
|
|
" reread buffer => deleted wiki link should appear as non-existent
|
2010-01-20 01:00:00 +01:00
|
|
|
if expand('%:p') != ""
|
|
|
|
execute "e"
|
|
|
|
endif
|
2010-08-24 02:00:00 +02:00
|
|
|
endfunction "}}}
|
2010-01-20 01:00:00 +01:00
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#rename_link
|
2014-12-07 11:58:08 +01:00
|
|
|
" Rename current file, update all links to it
|
2011-06-11 02:00:00 +02:00
|
|
|
function! vimwiki#base#rename_link() "{{{
|
2012-06-07 02:00:00 +02:00
|
|
|
let subdir = VimwikiGet('subdir')
|
2010-01-20 01:00:00 +01:00
|
|
|
let old_fname = subdir.expand('%:t')
|
|
|
|
|
|
|
|
" there is no file (new one maybe)
|
|
|
|
if glob(expand('%:p')) == ''
|
|
|
|
echomsg 'vimwiki: Cannot rename "'.expand('%:p').
|
|
|
|
\'". It does not exist! (New file? Save it before renaming.)'
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2015-02-07 22:01:20 +01:00
|
|
|
let val = input('Rename "'.expand('%:t:r').'" [y]es/[N]o? ')
|
|
|
|
if val !~? '^y'
|
2010-01-20 01:00:00 +01:00
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2015-02-07 22:01:20 +01:00
|
|
|
let new_link = input('Enter new name: ')
|
2010-01-20 01:00:00 +01:00
|
|
|
|
2015-02-09 20:47:11 +01:00
|
|
|
if new_link =~# '[/\\]'
|
2010-01-20 01:00:00 +01:00
|
|
|
" It is actually doable but I do not have free time to do it.
|
|
|
|
echomsg 'vimwiki: Cannot rename to a filename with path!'
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
" check new_fname - it should be 'good', not empty
|
|
|
|
if substitute(new_link, '\s', '', 'g') == ''
|
|
|
|
echomsg 'vimwiki: Cannot rename to an empty filename!'
|
|
|
|
return
|
|
|
|
endif
|
2012-06-07 02:00:00 +02:00
|
|
|
|
|
|
|
let url = matchstr(new_link, g:vimwiki_rxWikiLinkMatchUrl)
|
|
|
|
if url != ''
|
|
|
|
let new_link = url
|
2010-01-20 01:00:00 +01:00
|
|
|
endif
|
2013-04-19 05:46:58 +02:00
|
|
|
|
2010-08-24 02:00:00 +02:00
|
|
|
let new_link = subdir.new_link
|
2012-06-07 02:00:00 +02:00
|
|
|
let new_fname = VimwikiGet('path').new_link.VimwikiGet('ext')
|
2010-01-20 01:00:00 +01:00
|
|
|
|
2012-06-07 02:00:00 +02:00
|
|
|
" do not rename if file with such name exists
|
2010-01-20 01:00:00 +01:00
|
|
|
let fname = glob(new_fname)
|
|
|
|
if fname != ''
|
|
|
|
echomsg 'vimwiki: Cannot rename to "'.new_fname.
|
|
|
|
\ '". File with that name exist!'
|
|
|
|
return
|
|
|
|
endif
|
2012-06-07 02:00:00 +02:00
|
|
|
" rename wiki link file
|
2010-01-20 01:00:00 +01:00
|
|
|
try
|
|
|
|
echomsg "Renaming ".VimwikiGet('path').old_fname." to ".new_fname
|
|
|
|
let res = rename(expand('%:p'), expand(new_fname))
|
|
|
|
if res != 0
|
|
|
|
throw "Cannot rename!"
|
|
|
|
end
|
|
|
|
catch /.*/
|
|
|
|
echomsg 'vimwiki: Cannot rename "'.expand('%:t:r').'" to "'.new_fname.'"'
|
|
|
|
return
|
|
|
|
endtry
|
|
|
|
|
|
|
|
let &buftype="nofile"
|
|
|
|
|
|
|
|
let cur_buffer = [expand('%:p'),
|
2010-02-23 01:00:00 +01:00
|
|
|
\getbufvar(expand('%:p'), "vimwiki_prev_link")]
|
2010-01-20 01:00:00 +01:00
|
|
|
|
|
|
|
let blist = s:get_wiki_buffers()
|
|
|
|
|
|
|
|
" save wiki buffers
|
|
|
|
for bitem in blist
|
|
|
|
execute ':b '.escape(bitem[0], ' ')
|
|
|
|
execute ':update'
|
|
|
|
endfor
|
|
|
|
|
|
|
|
execute ':b '.escape(cur_buffer[0], ' ')
|
|
|
|
|
|
|
|
" remove wiki buffers
|
|
|
|
for bitem in blist
|
|
|
|
execute 'bwipeout '.escape(bitem[0], ' ')
|
|
|
|
endfor
|
|
|
|
|
|
|
|
let setting_more = &more
|
|
|
|
setlocal nomore
|
|
|
|
|
|
|
|
" update links
|
2013-08-17 18:10:00 +02:00
|
|
|
call s:update_wiki_links(s:tail_name(old_fname), new_link)
|
2010-01-20 01:00:00 +01:00
|
|
|
|
|
|
|
" restore wiki buffers
|
|
|
|
for bitem in blist
|
2015-02-09 20:24:18 +01:00
|
|
|
if !vimwiki#path#is_equal(bitem[0], cur_buffer[0])
|
2010-01-20 01:00:00 +01:00
|
|
|
call s:open_wiki_buffer(bitem)
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
|
|
|
call s:open_wiki_buffer([new_fname,
|
|
|
|
\ cur_buffer[1]])
|
|
|
|
" execute 'bwipeout '.escape(cur_buffer[0], ' ')
|
|
|
|
|
|
|
|
echomsg old_fname." is renamed to ".new_fname
|
|
|
|
|
|
|
|
let &more = setting_more
|
2010-08-24 02:00:00 +02:00
|
|
|
endfunction " }}}
|
2010-01-20 01:00:00 +01:00
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#ui_select
|
2012-06-07 02:00:00 +02:00
|
|
|
function! vimwiki#base#ui_select() "{{{
|
2010-01-20 01:00:00 +01:00
|
|
|
call s:print_wiki_list()
|
|
|
|
let idx = input("Select Wiki (specify number): ")
|
|
|
|
if idx == ""
|
|
|
|
return
|
|
|
|
endif
|
2011-06-11 02:00:00 +02:00
|
|
|
call vimwiki#base#goto_index(idx)
|
2012-07-07 02:00:00 +02:00
|
|
|
endfunction "}}}
|
2010-01-20 01:00:00 +01:00
|
|
|
" }}}
|
|
|
|
|
|
|
|
" TEXT OBJECTS functions {{{
|
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#TO_header
|
2011-06-11 02:00:00 +02:00
|
|
|
function! vimwiki#base#TO_header(inner, visual) "{{{
|
2010-05-12 02:00:00 +02:00
|
|
|
if !search('^\(=\+\).\+\1\s*$', 'bcW')
|
2010-01-20 01:00:00 +01:00
|
|
|
return
|
|
|
|
endif
|
2013-04-19 05:46:58 +02:00
|
|
|
|
2010-01-20 01:00:00 +01:00
|
|
|
let sel_start = line("'<")
|
|
|
|
let sel_end = line("'>")
|
|
|
|
let block_start = line(".")
|
|
|
|
let advance = 0
|
|
|
|
|
2012-06-07 02:00:00 +02:00
|
|
|
let level = vimwiki#u#count_first_sym(getline('.'))
|
2010-01-20 01:00:00 +01:00
|
|
|
|
2013-04-19 05:46:58 +02:00
|
|
|
let is_header_selected = sel_start == block_start
|
2010-01-20 01:00:00 +01:00
|
|
|
\ && sel_start != sel_end
|
|
|
|
|
|
|
|
if a:visual && is_header_selected
|
|
|
|
if level > 1
|
|
|
|
let level -= 1
|
2010-05-12 02:00:00 +02:00
|
|
|
call search('^\(=\{'.level.'\}\).\+\1\s*$', 'bcW')
|
2010-01-20 01:00:00 +01:00
|
|
|
else
|
|
|
|
let advance = 1
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
|
|
|
normal! V
|
|
|
|
|
|
|
|
if a:visual && is_header_selected
|
|
|
|
call cursor(sel_end + advance, 0)
|
|
|
|
endif
|
|
|
|
|
2010-05-12 02:00:00 +02:00
|
|
|
if search('^\(=\{1,'.level.'}\).\+\1\s*$', 'W')
|
2010-01-20 01:00:00 +01:00
|
|
|
call cursor(line('.') - 1, 0)
|
|
|
|
else
|
|
|
|
call cursor(line('$'), 0)
|
|
|
|
endif
|
|
|
|
|
2015-02-09 20:47:11 +01:00
|
|
|
if a:inner && getline(line('.')) =~# '^\s*$'
|
2010-01-20 01:00:00 +01:00
|
|
|
let lnum = prevnonblank(line('.') - 1)
|
|
|
|
call cursor(lnum, 0)
|
|
|
|
endif
|
2012-07-07 02:00:00 +02:00
|
|
|
endfunction "}}}
|
2010-01-20 01:00:00 +01:00
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#TO_table_cell
|
2011-06-11 02:00:00 +02:00
|
|
|
function! vimwiki#base#TO_table_cell(inner, visual) "{{{
|
2010-05-12 02:00:00 +02:00
|
|
|
if col('.') == col('$')-1
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
if a:visual
|
|
|
|
normal! `>
|
|
|
|
let sel_end = getpos('.')
|
|
|
|
normal! `<
|
|
|
|
let sel_start = getpos('.')
|
|
|
|
|
|
|
|
let firsttime = sel_start == sel_end
|
|
|
|
|
|
|
|
if firsttime
|
|
|
|
if !search('|\|\(-+-\)', 'cb', line('.'))
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
if getline('.')[virtcol('.')] == '+'
|
|
|
|
normal! l
|
|
|
|
endif
|
|
|
|
if a:inner
|
|
|
|
normal! 2l
|
|
|
|
endif
|
|
|
|
let sel_start = getpos('.')
|
|
|
|
endif
|
|
|
|
|
|
|
|
normal! `>
|
|
|
|
call search('|\|\(-+-\)', '', line('.'))
|
|
|
|
if getline('.')[virtcol('.')] == '+'
|
|
|
|
normal! l
|
|
|
|
endif
|
|
|
|
if a:inner
|
|
|
|
if firsttime || abs(sel_end[2] - getpos('.')[2]) != 2
|
|
|
|
normal! 2h
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
let sel_end = getpos('.')
|
|
|
|
|
|
|
|
call setpos('.', sel_start)
|
|
|
|
exe "normal! \<C-v>"
|
|
|
|
call setpos('.', sel_end)
|
|
|
|
|
|
|
|
" XXX: WORKAROUND.
|
|
|
|
" if blockwise selection is ended at | character then pressing j to extend
|
|
|
|
" selection furhter fails. But if we shake the cursor left and right then
|
|
|
|
" it works.
|
|
|
|
normal! hl
|
|
|
|
else
|
|
|
|
if !search('|\|\(-+-\)', 'cb', line('.'))
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
if a:inner
|
|
|
|
normal! 2l
|
|
|
|
endif
|
|
|
|
normal! v
|
|
|
|
call search('|\|\(-+-\)', '', line('.'))
|
|
|
|
if !a:inner && getline('.')[virtcol('.')-1] == '|'
|
|
|
|
normal! h
|
|
|
|
elseif a:inner
|
|
|
|
normal! 2h
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endfunction "}}}
|
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#TO_table_col
|
2011-06-11 02:00:00 +02:00
|
|
|
function! vimwiki#base#TO_table_col(inner, visual) "{{{
|
2012-06-07 02:00:00 +02:00
|
|
|
let t_rows = vimwiki#tbl#get_rows(line('.'))
|
2010-05-12 02:00:00 +02:00
|
|
|
if empty(t_rows)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
" TODO: refactor it!
|
|
|
|
if a:visual
|
|
|
|
normal! `>
|
|
|
|
let sel_end = getpos('.')
|
|
|
|
normal! `<
|
|
|
|
let sel_start = getpos('.')
|
|
|
|
|
|
|
|
let firsttime = sel_start == sel_end
|
|
|
|
|
|
|
|
if firsttime
|
|
|
|
" place cursor to the top row of the table
|
2012-06-07 02:00:00 +02:00
|
|
|
call vimwiki#u#cursor(t_rows[0][0], virtcol('.'))
|
2010-05-12 02:00:00 +02:00
|
|
|
" do not accept the match at cursor position if cursor is next to column
|
|
|
|
" separator of the table separator (^ is a cursor):
|
|
|
|
" |-----^-+-------|
|
|
|
|
" | bla | bla |
|
|
|
|
" |-------+-------|
|
|
|
|
" or it will select wrong column.
|
2015-02-09 20:47:11 +01:00
|
|
|
if strpart(getline('.'), virtcol('.')-1) =~# '^-+'
|
2010-05-12 02:00:00 +02:00
|
|
|
let s_flag = 'b'
|
|
|
|
else
|
|
|
|
let s_flag = 'cb'
|
|
|
|
endif
|
|
|
|
" search the column separator backwards
|
|
|
|
if !search('|\|\(-+-\)', s_flag, line('.'))
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
" -+- column separator is matched --> move cursor to the + sign
|
|
|
|
if getline('.')[virtcol('.')] == '+'
|
|
|
|
normal! l
|
|
|
|
endif
|
|
|
|
" inner selection --> reduce selection
|
|
|
|
if a:inner
|
|
|
|
normal! 2l
|
|
|
|
endif
|
|
|
|
let sel_start = getpos('.')
|
|
|
|
endif
|
|
|
|
|
|
|
|
normal! `>
|
|
|
|
if !firsttime && getline('.')[virtcol('.')] == '|'
|
|
|
|
normal! l
|
2015-02-09 20:47:11 +01:00
|
|
|
elseif a:inner && getline('.')[virtcol('.')+1] =~# '[|+]'
|
2010-05-12 02:00:00 +02:00
|
|
|
normal! 2l
|
|
|
|
endif
|
|
|
|
" search for the next column separator
|
|
|
|
call search('|\|\(-+-\)', '', line('.'))
|
|
|
|
" Outer selection selects a column without border on the right. So we move
|
|
|
|
" our cursor left if the previous search finds | border, not -+-.
|
|
|
|
if getline('.')[virtcol('.')] != '+'
|
|
|
|
normal! h
|
|
|
|
endif
|
|
|
|
if a:inner
|
|
|
|
" reduce selection a bit more if inner.
|
|
|
|
normal! h
|
|
|
|
endif
|
|
|
|
" expand selection to the bottom line of the table
|
2012-06-07 02:00:00 +02:00
|
|
|
call vimwiki#u#cursor(t_rows[-1][0], virtcol('.'))
|
2010-05-12 02:00:00 +02:00
|
|
|
let sel_end = getpos('.')
|
|
|
|
|
|
|
|
call setpos('.', sel_start)
|
|
|
|
exe "normal! \<C-v>"
|
|
|
|
call setpos('.', sel_end)
|
|
|
|
|
|
|
|
else
|
|
|
|
" place cursor to the top row of the table
|
2012-06-07 02:00:00 +02:00
|
|
|
call vimwiki#u#cursor(t_rows[0][0], virtcol('.'))
|
2010-05-12 02:00:00 +02:00
|
|
|
" do not accept the match at cursor position if cursor is next to column
|
|
|
|
" separator of the table separator (^ is a cursor):
|
|
|
|
" |-----^-+-------|
|
|
|
|
" | bla | bla |
|
|
|
|
" |-------+-------|
|
|
|
|
" or it will select wrong column.
|
2015-02-09 20:47:11 +01:00
|
|
|
if strpart(getline('.'), virtcol('.')-1) =~# '^-+'
|
2010-05-12 02:00:00 +02:00
|
|
|
let s_flag = 'b'
|
|
|
|
else
|
|
|
|
let s_flag = 'cb'
|
|
|
|
endif
|
|
|
|
" search the column separator backwards
|
|
|
|
if !search('|\|\(-+-\)', s_flag, line('.'))
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
" -+- column separator is matched --> move cursor to the + sign
|
|
|
|
if getline('.')[virtcol('.')] == '+'
|
|
|
|
normal! l
|
|
|
|
endif
|
|
|
|
" inner selection --> reduce selection
|
|
|
|
if a:inner
|
|
|
|
normal! 2l
|
|
|
|
endif
|
|
|
|
|
|
|
|
exe "normal! \<C-V>"
|
|
|
|
|
|
|
|
" search for the next column separator
|
|
|
|
call search('|\|\(-+-\)', '', line('.'))
|
|
|
|
" Outer selection selects a column without border on the right. So we move
|
|
|
|
" our cursor left if the previous search finds | border, not -+-.
|
|
|
|
if getline('.')[virtcol('.')] != '+'
|
|
|
|
normal! h
|
|
|
|
endif
|
|
|
|
" reduce selection a bit more if inner.
|
|
|
|
if a:inner
|
|
|
|
normal! h
|
|
|
|
endif
|
|
|
|
" expand selection to the bottom line of the table
|
2012-06-07 02:00:00 +02:00
|
|
|
call vimwiki#u#cursor(t_rows[-1][0], virtcol('.'))
|
2010-05-12 02:00:00 +02:00
|
|
|
endif
|
|
|
|
endfunction "}}}
|
2012-06-07 02:00:00 +02:00
|
|
|
" }}}
|
2010-05-12 02:00:00 +02:00
|
|
|
|
2012-06-07 02:00:00 +02:00
|
|
|
" HEADER functions {{{
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#AddHeaderLevel
|
2011-06-11 02:00:00 +02:00
|
|
|
function! vimwiki#base#AddHeaderLevel() "{{{
|
2010-01-20 01:00:00 +01:00
|
|
|
let lnum = line('.')
|
|
|
|
let line = getline(lnum)
|
2012-06-07 02:00:00 +02:00
|
|
|
let rxHdr = g:vimwiki_rxH
|
2015-02-09 20:47:11 +01:00
|
|
|
if line =~# '^\s*$'
|
2010-01-20 01:00:00 +01:00
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2015-02-09 20:47:11 +01:00
|
|
|
if line =~# g:vimwiki_rxHeader
|
2012-06-07 02:00:00 +02:00
|
|
|
let level = vimwiki#u#count_first_sym(line)
|
2010-01-20 01:00:00 +01:00
|
|
|
if level < 6
|
2012-06-07 02:00:00 +02:00
|
|
|
if g:vimwiki_symH
|
|
|
|
let line = substitute(line, '\('.rxHdr.'\+\).\+\1', rxHdr.'&'.rxHdr, '')
|
|
|
|
else
|
|
|
|
let line = substitute(line, '\('.rxHdr.'\+\).\+', rxHdr.'&', '')
|
|
|
|
endif
|
2010-01-20 01:00:00 +01:00
|
|
|
call setline(lnum, line)
|
|
|
|
endif
|
|
|
|
else
|
2013-04-19 05:46:58 +02:00
|
|
|
let line = substitute(line, '^\s*', '&'.rxHdr.' ', '')
|
2012-06-07 02:00:00 +02:00
|
|
|
if g:vimwiki_symH
|
|
|
|
let line = substitute(line, '\s*$', ' '.rxHdr.'&', '')
|
|
|
|
endif
|
|
|
|
call setline(lnum, line)
|
2010-01-20 01:00:00 +01:00
|
|
|
endif
|
2012-07-07 02:00:00 +02:00
|
|
|
endfunction "}}}
|
2010-01-20 01:00:00 +01:00
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#RemoveHeaderLevel
|
2011-06-11 02:00:00 +02:00
|
|
|
function! vimwiki#base#RemoveHeaderLevel() "{{{
|
2010-01-20 01:00:00 +01:00
|
|
|
let lnum = line('.')
|
|
|
|
let line = getline(lnum)
|
2012-06-07 02:00:00 +02:00
|
|
|
let rxHdr = g:vimwiki_rxH
|
2015-02-09 20:47:11 +01:00
|
|
|
if line =~# '^\s*$'
|
2010-01-20 01:00:00 +01:00
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2015-02-09 20:47:11 +01:00
|
|
|
if line =~# g:vimwiki_rxHeader
|
2012-06-07 02:00:00 +02:00
|
|
|
let level = vimwiki#u#count_first_sym(line)
|
|
|
|
let old = repeat(rxHdr, level)
|
|
|
|
let new = repeat(rxHdr, level - 1)
|
2010-01-20 01:00:00 +01:00
|
|
|
|
2015-02-09 20:47:11 +01:00
|
|
|
let chomp = line =~# rxHdr.'\s'
|
2010-01-20 01:00:00 +01:00
|
|
|
|
2012-06-07 02:00:00 +02:00
|
|
|
if g:vimwiki_symH
|
|
|
|
let line = substitute(line, old, new, 'g')
|
|
|
|
else
|
|
|
|
let line = substitute(line, old, new, '')
|
|
|
|
endif
|
2010-01-20 01:00:00 +01:00
|
|
|
|
|
|
|
if level == 1 && chomp
|
|
|
|
let line = substitute(line, '^\s', '', 'g')
|
|
|
|
let line = substitute(line, '\s$', '', 'g')
|
|
|
|
endif
|
2012-06-07 02:00:00 +02:00
|
|
|
|
|
|
|
let line = substitute(line, '\s*$', '', '')
|
|
|
|
|
2010-01-20 01:00:00 +01:00
|
|
|
call setline(lnum, line)
|
|
|
|
endif
|
2012-07-07 02:00:00 +02:00
|
|
|
endfunction " }}}
|
2014-02-13 12:42:24 +01:00
|
|
|
|
2014-02-18 14:47:42 +01:00
|
|
|
" a:create == 1: creates or updates TOC in current file
|
|
|
|
" a:create == 0: update if TOC exists
|
|
|
|
function! vimwiki#base#table_of_contents(create)
|
2014-11-12 11:13:14 +01:00
|
|
|
" apparently, Vim behaves strange when files change while in diff mode
|
|
|
|
if &diff
|
|
|
|
return
|
|
|
|
endif
|
2014-02-13 12:42:24 +01:00
|
|
|
|
2014-02-18 14:47:42 +01:00
|
|
|
" look for existing TOC
|
2014-02-13 12:42:24 +01:00
|
|
|
let toc_header = '^\s*'.substitute(g:vimwiki_rxH1_Template, '__Header__',
|
2014-02-18 14:47:42 +01:00
|
|
|
\ '\='."'".g:vimwiki_toc_header."'", '').'\s*$'
|
|
|
|
let toc_line = 0
|
2014-02-13 12:42:24 +01:00
|
|
|
let lnum = 1
|
|
|
|
while lnum <= &modelines + 2 && lnum <= line('$')
|
2014-02-18 14:47:42 +01:00
|
|
|
if getline(lnum) =~# toc_header
|
|
|
|
let toc_line = lnum
|
2014-02-13 12:42:24 +01:00
|
|
|
break
|
|
|
|
endif
|
|
|
|
let lnum += 1
|
|
|
|
endwhile
|
|
|
|
|
2014-02-18 14:47:42 +01:00
|
|
|
if !a:create && toc_line <= 0
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let old_cursor_pos = getpos('.')
|
|
|
|
let bullet = vimwiki#lst#default_symbol().' '
|
|
|
|
let rx_bullet = vimwiki#u#escape(bullet)
|
|
|
|
let whitespaces = matchstr(getline(toc_line), '^\s*')
|
|
|
|
|
|
|
|
" delete old TOC
|
|
|
|
if toc_line > 0
|
|
|
|
let endoftoc = toc_line+1
|
2015-02-09 20:47:11 +01:00
|
|
|
while endoftoc <= line('$') && getline(endoftoc) =~# '^\s*'.rx_bullet.g:vimwiki_rxWikiLink.'\s*$'
|
2014-02-18 14:47:42 +01:00
|
|
|
let endoftoc += 1
|
|
|
|
endwhile
|
|
|
|
silent exe toc_line.','.string(endoftoc-1).'delete _'
|
|
|
|
else
|
|
|
|
let toc_line = 1
|
|
|
|
endif
|
|
|
|
|
2014-02-13 12:42:24 +01:00
|
|
|
" collect new headers
|
2014-02-18 14:47:42 +01:00
|
|
|
let headers = []
|
2014-02-13 12:42:24 +01:00
|
|
|
let headers_levels = [['', 0], ['', 0], ['', 0], ['', 0], ['', 0], ['', 0]]
|
|
|
|
for lnum in range(1, line('$'))
|
|
|
|
let line_content = getline(lnum)
|
2015-02-09 20:47:11 +01:00
|
|
|
if line_content !~# g:vimwiki_rxHeader
|
2014-02-13 12:42:24 +01:00
|
|
|
continue
|
|
|
|
endif
|
|
|
|
let h_level = vimwiki#u#count_first_sym(line_content)
|
|
|
|
let h_text = vimwiki#u#trim(matchstr(line_content, g:vimwiki_rxHeader))
|
|
|
|
let headers_levels[h_level-1] = [h_text, headers_levels[h_level-1][1]+1]
|
|
|
|
for idx in range(h_level, 5) | let headers_levels[idx] = ['', 0] | endfor
|
|
|
|
|
|
|
|
let h_complete_id = ''
|
|
|
|
for l in range(h_level-1)
|
|
|
|
if headers_levels[l][0] != ''
|
|
|
|
let h_complete_id .= headers_levels[l][0].'#'
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
let h_complete_id .= headers_levels[h_level-1][0]
|
|
|
|
|
|
|
|
if g:vimwiki_html_header_numbering > 0
|
|
|
|
\ && g:vimwiki_html_header_numbering <= h_level
|
|
|
|
let h_number = join(map(copy(headers_levels[
|
|
|
|
\ g:vimwiki_html_header_numbering-1 : h_level-1]), 'v:val[1]'), '.')
|
|
|
|
let h_number .= g:vimwiki_html_header_numbering_sym
|
|
|
|
let h_text = h_number.' '.h_text
|
|
|
|
endif
|
|
|
|
|
2014-02-18 14:47:42 +01:00
|
|
|
call add(headers, [h_level, h_complete_id, h_text])
|
2014-02-13 12:42:24 +01:00
|
|
|
endfor
|
|
|
|
|
|
|
|
" write new TOC
|
2014-02-18 14:47:42 +01:00
|
|
|
call append(toc_line-1, whitespaces . substitute(g:vimwiki_rxH1_Template,
|
|
|
|
\ '__Header__', '\='."'".g:vimwiki_toc_header."'", ''))
|
2014-02-13 12:42:24 +01:00
|
|
|
|
|
|
|
let startindent = repeat(' ', vimwiki#lst#get_list_margin())
|
2015-01-08 17:14:16 +01:00
|
|
|
let indentstring = repeat(' ', vimwiki#u#sw())
|
2014-02-18 14:47:42 +01:00
|
|
|
for [lvl, link, desc] in headers
|
2014-02-13 12:42:24 +01:00
|
|
|
let esc_link = substitute(link, "'", "''", 'g')
|
|
|
|
let esc_desc = substitute(desc, "'", "''", 'g')
|
|
|
|
let link = substitute(g:vimwiki_WikiLinkTemplate2, '__LinkUrl__',
|
|
|
|
\ '\='."'".'#'.esc_link."'", '')
|
|
|
|
let link = substitute(link, '__LinkDescription__', '\='."'".esc_desc."'", '')
|
|
|
|
call append(toc_line, startindent.repeat(indentstring, lvl-1).bullet.link)
|
|
|
|
let toc_line += 1
|
|
|
|
endfor
|
2015-02-09 20:47:11 +01:00
|
|
|
if getline(toc_line+1) !~# '^\s*$'
|
2014-02-13 12:42:24 +01:00
|
|
|
call append(toc_line, '')
|
|
|
|
endif
|
|
|
|
call setpos('.', old_cursor_pos)
|
|
|
|
endfunction
|
2012-06-07 02:00:00 +02:00
|
|
|
"}}}
|
|
|
|
|
|
|
|
" LINK functions {{{
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#apply_template
|
|
|
|
" Construct a regular expression matching from template (with special
|
2012-06-07 02:00:00 +02:00
|
|
|
" characters properly escaped), by substituting rxUrl for __LinkUrl__, rxDesc
|
|
|
|
" for __LinkDescription__, and rxStyle for __LinkStyle__. The three
|
|
|
|
" arguments rxUrl, rxDesc, and rxStyle are copied verbatim, without any
|
|
|
|
" special character escapes or substitutions.
|
|
|
|
function! vimwiki#base#apply_template(template, rxUrl, rxDesc, rxStyle) "{{{
|
2014-06-19 15:36:11 +02:00
|
|
|
let lnk = a:template
|
2012-06-07 02:00:00 +02:00
|
|
|
if a:rxUrl != ""
|
2014-06-19 15:36:11 +02:00
|
|
|
let lnk = substitute(lnk, '__LinkUrl__', '\='."'".a:rxUrl."'", 'g')
|
2012-06-07 02:00:00 +02:00
|
|
|
endif
|
|
|
|
if a:rxDesc != ""
|
2014-06-19 15:36:11 +02:00
|
|
|
let lnk = substitute(lnk, '__LinkDescription__', '\='."'".a:rxDesc."'", 'g')
|
2012-06-07 02:00:00 +02:00
|
|
|
endif
|
|
|
|
if a:rxStyle != ""
|
2014-06-19 15:36:11 +02:00
|
|
|
let lnk = substitute(lnk, '__LinkStyle__', '\='."'".a:rxStyle."'", 'g')
|
2012-06-07 02:00:00 +02:00
|
|
|
endif
|
|
|
|
return lnk
|
2012-07-07 02:00:00 +02:00
|
|
|
endfunction " }}}
|
2012-06-07 02:00:00 +02:00
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" s:clean_url
|
2012-06-07 02:00:00 +02:00
|
|
|
function! s:clean_url(url) " {{{
|
|
|
|
let url = split(a:url, '/\|=\|-\|&\|?\|\.')
|
2015-02-09 20:24:18 +01:00
|
|
|
let url = filter(url, 'v:val !=# ""')
|
|
|
|
let url = filter(url, 'v:val !=# "www"')
|
|
|
|
let url = filter(url, 'v:val !=# "com"')
|
|
|
|
let url = filter(url, 'v:val !=# "org"')
|
|
|
|
let url = filter(url, 'v:val !=# "net"')
|
|
|
|
let url = filter(url, 'v:val !=# "edu"')
|
|
|
|
let url = filter(url, 'v:val !=# "http\:"')
|
|
|
|
let url = filter(url, 'v:val !=# "https\:"')
|
|
|
|
let url = filter(url, 'v:val !=# "file\:"')
|
|
|
|
let url = filter(url, 'v:val !=# "xml\:"')
|
2012-06-07 02:00:00 +02:00
|
|
|
return join(url, " ")
|
|
|
|
endfunction " }}}
|
|
|
|
|
2014-12-04 21:27:02 +01:00
|
|
|
" s:is_diary_file
|
|
|
|
function! s:is_diary_file(filename) " {{{
|
|
|
|
let file_path = vimwiki#path#path_norm(a:filename)
|
2014-09-15 21:44:32 +02:00
|
|
|
let rel_path = VimwikiGet('diary_rel_path')
|
2014-12-04 21:27:02 +01:00
|
|
|
let diary_path = vimwiki#path#path_norm(VimwikiGet('path') . rel_path)
|
2014-09-15 21:44:32 +02:00
|
|
|
return rel_path != ''
|
|
|
|
\ && file_path =~# '^'.vimwiki#u#escape(diary_path)
|
|
|
|
endfunction " }}}
|
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#normalize_link_helper
|
2012-06-07 02:00:00 +02:00
|
|
|
function! vimwiki#base#normalize_link_helper(str, rxUrl, rxDesc, template) " {{{
|
|
|
|
let str = a:str
|
|
|
|
let url = matchstr(str, a:rxUrl)
|
|
|
|
let descr = matchstr(str, a:rxDesc)
|
|
|
|
let template = a:template
|
|
|
|
if descr == ""
|
|
|
|
let descr = s:clean_url(url)
|
|
|
|
endif
|
|
|
|
let lnk = substitute(template, '__LinkDescription__', '\="'.descr.'"', '')
|
|
|
|
let lnk = substitute(lnk, '__LinkUrl__', '\="'.url.'"', '')
|
|
|
|
return lnk
|
|
|
|
endfunction " }}}
|
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#normalize_imagelink_helper
|
2012-06-07 02:00:00 +02:00
|
|
|
function! vimwiki#base#normalize_imagelink_helper(str, rxUrl, rxDesc, rxStyle, template) "{{{
|
|
|
|
let lnk = vimwiki#base#normalize_link_helper(a:str, a:rxUrl, a:rxDesc, a:template)
|
2014-02-24 12:16:23 +01:00
|
|
|
let style = matchstr(a:str, a:rxStyle)
|
2012-06-07 02:00:00 +02:00
|
|
|
let lnk = substitute(lnk, '__LinkStyle__', '\="'.style.'"', '')
|
|
|
|
return lnk
|
|
|
|
endfunction " }}}
|
|
|
|
|
2014-09-15 21:44:32 +02:00
|
|
|
" s:normalize_link_in_diary
|
|
|
|
function! s:normalize_link_in_diary(lnk) " {{{
|
2014-09-17 23:26:16 +02:00
|
|
|
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
|
2014-09-17 23:10:49 +02:00
|
|
|
let rxUrl = g:vimwiki_rxWord
|
|
|
|
let rxDesc = ''
|
|
|
|
let template = g:vimwiki_WikiLinkTemplate1
|
2014-09-15 21:44:32 +02:00
|
|
|
else
|
|
|
|
let depth = len(split(VimwikiGet('diary_rel_path'), '/'))
|
2014-11-06 20:01:16 +01:00
|
|
|
let str = repeat('../', depth) . a:lnk . '|' . a:lnk
|
|
|
|
let rxUrl = '^.*\ze|'
|
|
|
|
let rxDesc = '|\zs.*$'
|
2014-09-17 23:10:49 +02:00
|
|
|
let template = g:vimwiki_WikiLinkTemplate2
|
2014-09-15 21:44:32 +02:00
|
|
|
endif
|
|
|
|
|
2014-09-17 23:10:49 +02:00
|
|
|
return vimwiki#base#normalize_link_helper(str, rxUrl, rxDesc, template)
|
2014-09-15 21:44:32 +02:00
|
|
|
endfunction " }}}
|
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" s:normalize_link_syntax_n
|
2012-06-07 02:00:00 +02:00
|
|
|
function! s:normalize_link_syntax_n() " {{{
|
|
|
|
|
|
|
|
" try WikiLink
|
|
|
|
let lnk = vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWikiLink)
|
|
|
|
if !empty(lnk)
|
|
|
|
let sub = vimwiki#base#normalize_link_helper(lnk,
|
|
|
|
\ g:vimwiki_rxWikiLinkMatchUrl, g:vimwiki_rxWikiLinkMatchDescr,
|
|
|
|
\ g:vimwiki_WikiLinkTemplate2)
|
|
|
|
call vimwiki#base#replacestr_at_cursor(g:vimwiki_rxWikiLink, sub)
|
|
|
|
if g:vimwiki_debug > 1
|
|
|
|
echomsg "WikiLink: ".lnk." Sub: ".sub
|
|
|
|
endif
|
|
|
|
return
|
|
|
|
endif
|
2013-04-19 05:46:58 +02:00
|
|
|
|
2012-06-07 02:00:00 +02:00
|
|
|
" try WikiIncl
|
|
|
|
let lnk = vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWikiIncl)
|
|
|
|
if !empty(lnk)
|
|
|
|
" NO-OP !!
|
|
|
|
if g:vimwiki_debug > 1
|
|
|
|
echomsg "WikiIncl: ".lnk." Sub: ".lnk
|
|
|
|
endif
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
" try Word (any characters except separators)
|
|
|
|
" rxWord is less permissive than rxWikiLinkUrl which is used in
|
|
|
|
" normalize_link_syntax_v
|
|
|
|
let lnk = vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWord)
|
|
|
|
if !empty(lnk)
|
2014-12-04 21:27:02 +01:00
|
|
|
if s:is_diary_file(expand("%:p"))
|
2014-09-15 21:44:32 +02:00
|
|
|
let sub = s:normalize_link_in_diary(lnk)
|
|
|
|
else
|
|
|
|
let sub = vimwiki#base#normalize_link_helper(lnk,
|
|
|
|
\ g:vimwiki_rxWord, '',
|
|
|
|
\ g:vimwiki_WikiLinkTemplate1)
|
|
|
|
endif
|
2012-06-07 02:00:00 +02:00
|
|
|
call vimwiki#base#replacestr_at_cursor('\V'.lnk, sub)
|
|
|
|
if g:vimwiki_debug > 1
|
|
|
|
echomsg "Word: ".lnk." Sub: ".sub
|
|
|
|
endif
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
endfunction " }}}
|
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" s:normalize_link_syntax_v
|
2012-06-07 02:00:00 +02:00
|
|
|
function! s:normalize_link_syntax_v() " {{{
|
|
|
|
let sel_save = &selection
|
|
|
|
let &selection = "old"
|
|
|
|
let rv = @"
|
|
|
|
let rt = getregtype('"')
|
|
|
|
|
|
|
|
try
|
2014-09-15 21:44:32 +02:00
|
|
|
" Save selected text to register "
|
|
|
|
normal! gv""y
|
2012-06-07 02:00:00 +02:00
|
|
|
|
2014-09-15 21:44:32 +02:00
|
|
|
" Set substitution
|
2014-12-04 21:27:02 +01:00
|
|
|
if s:is_diary_file(expand("%:p"))
|
2014-09-15 21:44:32 +02:00
|
|
|
let sub = s:normalize_link_in_diary(@")
|
|
|
|
else
|
|
|
|
let sub = substitute(g:vimwiki_WikiLinkTemplate1,
|
|
|
|
\ '__LinkUrl__', '\=' . "'" . @" . "'", '')
|
|
|
|
endif
|
2012-06-07 02:00:00 +02:00
|
|
|
|
2014-09-15 21:44:32 +02:00
|
|
|
" Put substitution in register " and change text
|
|
|
|
call setreg('"', sub, 'v')
|
|
|
|
normal! `>""pgvd
|
2012-06-07 02:00:00 +02:00
|
|
|
finally
|
|
|
|
call setreg('"', rv, rt)
|
|
|
|
let &selection = sel_save
|
|
|
|
endtry
|
|
|
|
endfunction " }}}
|
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" vimwiki#base#normalize_link
|
2012-06-07 02:00:00 +02:00
|
|
|
function! vimwiki#base#normalize_link(is_visual_mode) "{{{
|
|
|
|
if exists('*vimwiki#'.VimwikiGet('syntax').'_base#normalize_link')
|
|
|
|
" Syntax-specific links
|
|
|
|
call vimwiki#{VimwikiGet('syntax')}_base#normalize_link(a:is_visual_mode)
|
|
|
|
else
|
|
|
|
if !a:is_visual_mode
|
|
|
|
call s:normalize_link_syntax_n()
|
|
|
|
elseif visualmode() ==# 'v' && line("'<") == line("'>")
|
|
|
|
" action undefined for 'line-wise' or 'multi-line' visual mode selections
|
|
|
|
call s:normalize_link_syntax_v()
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endfunction "}}}
|
2010-01-20 01:00:00 +01:00
|
|
|
|
|
|
|
" }}}
|
2012-07-07 02:00:00 +02:00
|
|
|
|
2014-11-06 22:02:37 +01:00
|
|
|
" Command completion functions {{{
|
|
|
|
|
2014-11-10 21:04:06 +01:00
|
|
|
" vimwiki#base#complete_links_escaped
|
|
|
|
function! vimwiki#base#complete_links_escaped(ArgLead, CmdLine, CursorPos) abort " {{{
|
|
|
|
" We can safely ignore args if we use -custom=complete option, Vim engine
|
|
|
|
" will do the job of filtering.
|
|
|
|
return vimwiki#base#get_globlinks_escaped()
|
|
|
|
endfunction " }}}
|
|
|
|
|
2014-11-06 22:02:37 +01:00
|
|
|
"}}}
|
|
|
|
|
2012-07-07 02:00:00 +02:00
|
|
|
" -------------------------------------------------------------------------
|
|
|
|
" Load syntax-specific Wiki functionality
|
2015-01-23 20:41:36 +01:00
|
|
|
for s:syn in s:vimwiki_get_known_syntaxes()
|
2014-02-24 12:16:23 +01:00
|
|
|
execute 'runtime! autoload/vimwiki/'.s:syn.'_base.vim'
|
2013-04-19 05:46:58 +02:00
|
|
|
endfor
|
2012-07-07 02:00:00 +02:00
|
|
|
" -------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|