Fix: follow_link (suffix) and VimwikiGenerateTagLinks (__FileExtension__) (Issue #914)
Problem: - follow_link failed with header with number at the end - VimwikiGenerateTagLinks did not replace the __FileExtension__ Solution: - follow_link try first 1 time with suffix number n and then n times without - VimwikiGenerateTagLinks replace the __FileExtension__
This commit is contained in:
parent
69ead3bf3c
commit
8a50d022b1
@ -729,7 +729,6 @@ function! s:normalize_anchor(anchor, ...) abort
|
|||||||
let anchor = tolower(anchor)
|
let anchor = tolower(anchor)
|
||||||
|
|
||||||
" 2 Remove anything that is not a letter, number, CJK character, hyphen or space
|
" 2 Remove anything that is not a letter, number, CJK character, hyphen or space
|
||||||
" TODO mutualize punctuation_rx with above
|
|
||||||
let punctuation_rx = s:get_punctuaction_regex()
|
let punctuation_rx = s:get_punctuaction_regex()
|
||||||
let anchor = substitute(anchor, punctuation_rx, '', 'g')
|
let anchor = substitute(anchor, punctuation_rx, '', 'g')
|
||||||
|
|
||||||
@ -753,8 +752,10 @@ endfunction
|
|||||||
|
|
||||||
|
|
||||||
" :param: anchor <string> <= link
|
" :param: anchor <string> <= link
|
||||||
" Return: [anchor_re <regex>, anchor_nb <number>] to look for
|
" Return: [anchor_re <regex>, anchor_nb <number>, suffix_re <regex>] to look for
|
||||||
|
" -- with or without suffix
|
||||||
" -- Ex: ['toto", 2] => search for the second occurrence of toto
|
" -- Ex: ['toto", 2] => search for the second occurrence of toto
|
||||||
|
" Called: jump_to_anchor
|
||||||
function! s:unnormalize_anchor(anchor) abort
|
function! s:unnormalize_anchor(anchor) abort
|
||||||
" Note:
|
" Note:
|
||||||
" -- Pandoc keep the '_' in anchor
|
" -- Pandoc keep the '_' in anchor
|
||||||
@ -773,12 +774,15 @@ function! s:unnormalize_anchor(anchor) abort
|
|||||||
" -- Save the trailing -12
|
" -- Save the trailing -12
|
||||||
let anchor_nb = substitute(anchor, '^.*-\(\d\+\)$', '\1', '')
|
let anchor_nb = substitute(anchor, '^.*-\(\d\+\)$', '\1', '')
|
||||||
if anchor_nb ==# '' || anchor_nb == 0
|
if anchor_nb ==# '' || anchor_nb == 0
|
||||||
" No Sufix: number = 1
|
" No Suffix: number = 1
|
||||||
let sufix = ''
|
let suffix = ''
|
||||||
let anchor_nb = 1
|
let anchor_nb = 1
|
||||||
else
|
else
|
||||||
" Yes Sufix: number <- read suffix
|
" Yes suffix: number <- read suffix
|
||||||
let sufix = invisible_rx.'*' . anchor_nb . invisible_rx.'*'
|
let suffix = invisible_rx.'*'
|
||||||
|
for char in split(anchor_nb, '\zs')
|
||||||
|
let suffix .= char . invisible_rx.'*'
|
||||||
|
endfor
|
||||||
let anchor_nb = str2nr(anchor_nb)
|
let anchor_nb = str2nr(anchor_nb)
|
||||||
endif
|
endif
|
||||||
" -- Remove it
|
" -- Remove it
|
||||||
@ -813,21 +817,13 @@ function! s:unnormalize_anchor(anchor) abort
|
|||||||
" 1 Downcase the string
|
" 1 Downcase the string
|
||||||
let anchor = '\c' . anchor
|
let anchor = '\c' . anchor
|
||||||
|
|
||||||
" 4.bis Add the optional suffix
|
return [anchor, anchor_nb, suffix]
|
||||||
let anchor = anchor . '\%(' . sufix . '\)\?'
|
|
||||||
|
|
||||||
return [anchor, anchor_nb]
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
" Jump to anchor, doing the oposite of normalize_anchor
|
" Jump to anchor, doing the oposite of normalize_anchor
|
||||||
" Called: edit_file
|
" Called: edit_file
|
||||||
" TODO treat the sufix: -2 -> Go to second anchor
|
|
||||||
function! s:jump_to_anchor(anchor) abort
|
function! s:jump_to_anchor(anchor) abort
|
||||||
" Save cursor %% Initialize at top of line
|
|
||||||
let oldpos = getpos('.')
|
|
||||||
call cursor(1, 1)
|
|
||||||
|
|
||||||
" Get segments <= anchor
|
" Get segments <= anchor
|
||||||
let anchor = vimwiki#u#escape(a:anchor)
|
let anchor = vimwiki#u#escape(a:anchor)
|
||||||
let segments = split(anchor, '#', 0)
|
let segments = split(anchor, '#', 0)
|
||||||
@ -836,34 +832,48 @@ function! s:jump_to_anchor(anchor) abort
|
|||||||
for segment in segments
|
for segment in segments
|
||||||
" Craft segment pattern so that it is case insensitive and also matches dashes
|
" Craft segment pattern so that it is case insensitive and also matches dashes
|
||||||
" in anchor link with spaces in heading
|
" in anchor link with spaces in heading
|
||||||
let [segment_re, segment_nb] = s:unnormalize_anchor(segment)
|
let [segment_re, segment_nb, segment_suffix] = s:unnormalize_anchor(segment)
|
||||||
|
|
||||||
|
" Try once with suffix (If header ends with number)
|
||||||
|
let res = s:jump_to_segment(segment_re . segment_suffix, 1)
|
||||||
|
" Try segment_nb times otherwise
|
||||||
|
if res != 0
|
||||||
|
let res = s:jump_to_segment(segment_re, segment_nb)
|
||||||
|
endif
|
||||||
|
endfor
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
" Called: jump_to_anchor with suffix and withtou suffix
|
||||||
|
function! s:jump_to_segment(segment_re, segment_nb) abort
|
||||||
|
" Save cursor %% Initialize at top of line
|
||||||
|
let oldpos = getpos('.')
|
||||||
|
call cursor(1, 1)
|
||||||
|
|
||||||
let anchor_header = s:safesubstitute(
|
let anchor_header = s:safesubstitute(
|
||||||
\ vimwiki#vars#get_syntaxlocal('header_match'),
|
\ vimwiki#vars#get_syntaxlocal('header_match'),
|
||||||
\ '__Header__', segment_re, 'g')
|
\ '__Header__', a:segment_re, 'g')
|
||||||
let anchor_bold = s:safesubstitute(
|
let anchor_bold = s:safesubstitute(
|
||||||
\ vimwiki#vars#get_syntaxlocal('bold_match'),
|
\ vimwiki#vars#get_syntaxlocal('bold_match'),
|
||||||
\ '__Text__', segment_re, 'g')
|
\ '__Text__', a:segment_re, 'g')
|
||||||
let anchor_tag = s:safesubstitute(
|
let anchor_tag = s:safesubstitute(
|
||||||
\ vimwiki#vars#get_syntaxlocal('tag_match'),
|
\ vimwiki#vars#get_syntaxlocal('tag_match'),
|
||||||
\ '__Tag__', segment_re, 'g')
|
\ '__Tag__', a:segment_re, 'g')
|
||||||
|
|
||||||
" Go: Move cursor: maybe more than onces (see markdown suffix)
|
" Go: Move cursor: maybe more than onces (see markdown suffix)
|
||||||
let success_nb = 0
|
let success_nb = 0
|
||||||
let is_last_segment = 0
|
let is_last_segment = 0
|
||||||
for i in range(segment_nb)
|
for i in range(a:segment_nb)
|
||||||
" Search
|
" Search
|
||||||
let pos = 0
|
let pos = 0
|
||||||
let pos = pos != 0 ? pos : search(anchor_tag, 'Wc')
|
let pos = pos != 0 ? pos : search(anchor_tag, 'Wc')
|
||||||
let pos = pos != 0 ? pos : search(anchor_header, 'Wc')
|
let pos = pos != 0 ? pos : search(anchor_header, 'Wc')
|
||||||
let pos = pos != 0 ? pos : search(anchor_bold, 'Wc')
|
let pos = pos != 0 ? pos : search(anchor_bold, 'Wc')
|
||||||
|
|
||||||
echom 'Tin pos: ' . pos
|
" Succed: Get the result and reloop or leave
|
||||||
|
|
||||||
" Get the result and reloop or leave
|
|
||||||
if pos != 0
|
if pos != 0
|
||||||
" Avance, one line more to not rematch the same pattern if not last segment_nb
|
" Avance, one line more to not rematch the same pattern if not last segment_nb
|
||||||
if success_nb < segment_nb-1
|
if success_nb < a:segment_nb-1
|
||||||
let pos += 1
|
let pos += 1
|
||||||
let is_last_segment = -1
|
let is_last_segment = -1
|
||||||
endif
|
endif
|
||||||
@ -873,12 +883,11 @@ function! s:jump_to_anchor(anchor) abort
|
|||||||
" Break if last line (avoid infinite loop)
|
" Break if last line (avoid infinite loop)
|
||||||
" Anyway leave the loop: (Imagine heading # 7271212 at last line)
|
" Anyway leave the loop: (Imagine heading # 7271212 at last line)
|
||||||
if pos >= line('$')
|
if pos >= line('$')
|
||||||
let is_last_segment = 1
|
return 0
|
||||||
break
|
|
||||||
endif
|
endif
|
||||||
|
" Fail:
|
||||||
" Do not move
|
" Do not move
|
||||||
" But maybe suffix -2 is not the segment number but the real header suffix
|
" But maybe suffix -2 is not the segment number but the real header suffix
|
||||||
" TODO make this more robust
|
|
||||||
else
|
else
|
||||||
" If fail at first: do not move
|
" If fail at first: do not move
|
||||||
if i == 0
|
if i == 0
|
||||||
@ -887,19 +896,20 @@ function! s:jump_to_anchor(anchor) abort
|
|||||||
" Anyway leave the loop: (Imagine heading # 7271212, you do not want to loop all that)
|
" Anyway leave the loop: (Imagine heading # 7271212, you do not want to loop all that)
|
||||||
" Go one line back: if I advanced too much
|
" Go one line back: if I advanced too much
|
||||||
if is_last_segment == -1 | call cursor(line('.')-1, 1) | endif
|
if is_last_segment == -1 | call cursor(line('.')-1, 1) | endif
|
||||||
let is_last_segment = 1
|
return 1
|
||||||
break
|
|
||||||
endif
|
endif
|
||||||
endfor
|
endfor
|
||||||
|
|
||||||
" Check if happy
|
" Check if happy
|
||||||
if success_nb == segment_nb || is_last_segment == 1
|
if success_nb == a:segment_nb
|
||||||
break
|
return 0
|
||||||
endif
|
endif
|
||||||
|
|
||||||
" Or keep on (i.e more than once segment)
|
" Or keep on (i.e more than once segment)
|
||||||
let oldpos = getpos('.')
|
let oldpos = getpos('.')
|
||||||
endfor
|
|
||||||
|
" Said 'fail' to caller
|
||||||
|
return 1
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
@ -363,17 +363,23 @@ function! vimwiki#tags#generate_tags(create, ...) abort
|
|||||||
let link_tpl = vimwiki#vars#get_syntaxlocal('Link1')
|
let link_tpl = vimwiki#vars#get_syntaxlocal('Link1')
|
||||||
let entry = s:safesubstitute(link_tpl, '__LinkUrl__', taglink, '')
|
let entry = s:safesubstitute(link_tpl, '__LinkUrl__', taglink, '')
|
||||||
let entry = s:safesubstitute(entry, '__LinkDescription__', taglink, '')
|
let entry = s:safesubstitute(entry, '__LinkDescription__', taglink, '')
|
||||||
|
let file_extension = vimwiki#vars#get_wikilocal('ext', vimwiki#vars#get_bufferlocal('wiki_nr'))
|
||||||
|
let entry = s:safesubstitute(entry, '__FileExtension__', file_extension , '')
|
||||||
else
|
else
|
||||||
let link_caption = split(link_infos.anchor, '#', 0)[-1]
|
let link_caption = split(link_infos.anchor, '#', 0)[-1]
|
||||||
let link_text = split(taglink, '#', 1)[0]
|
let link_text = split(taglink, '#', 1)[0]
|
||||||
let entry = s:safesubstitute(link_tpl, '__LinkUrl__', link_text, '')
|
let entry = s:safesubstitute(link_tpl, '__LinkUrl__', link_text, '')
|
||||||
let entry = s:safesubstitute(entry, '__LinkAnchor__', link_infos.anchor, '')
|
let entry = s:safesubstitute(entry, '__LinkAnchor__', link_infos.anchor, '')
|
||||||
let entry = s:safesubstitute(entry, '__LinkDescription__', link_caption, '')
|
let entry = s:safesubstitute(entry, '__LinkDescription__', link_caption, '')
|
||||||
|
let file_extension = vimwiki#vars#get_wikilocal('ext', vimwiki#vars#get_bufferlocal('wiki_nr'))
|
||||||
|
let entry = s:safesubstitute(entry, '__FileExtension__', file_extension , '')
|
||||||
endif
|
endif
|
||||||
|
|
||||||
call add(lines, bullet . entry)
|
call add(lines, bullet . entry)
|
||||||
else
|
else
|
||||||
let link_tpl = vimwiki#vars#get_global('WikiLinkTemplate1')
|
let link_tpl = vimwiki#vars#get_global('WikiLinkTemplate1')
|
||||||
|
let file_extension = vimwiki#vars#get_wikilocal('ext', vimwiki#vars#get_bufferlocal('wiki_nr'))
|
||||||
|
let link_tpl = s:safesubstitute(link_tpl, '__FileExtension__', file_extension , '')
|
||||||
call add(lines, bullet . substitute(link_tpl, '__LinkUrl__', taglink, ''))
|
call add(lines, bullet . substitute(link_tpl, '__LinkUrl__', taglink, ''))
|
||||||
endif
|
endif
|
||||||
endfor
|
endfor
|
||||||
|
@ -19,6 +19,31 @@ Expect (a):
|
|||||||
a
|
a
|
||||||
|
|
||||||
|
|
||||||
|
Given vimwiki (VimwikiTOC with link and number {{{1):
|
||||||
|
[link1](#i-v-p-741528)
|
||||||
|
[link2](#i-v-p-741528-2)
|
||||||
|
|
||||||
|
# I [V p](h) (7.415.28)
|
||||||
|
|
||||||
|
# I [V p](h) 741.528
|
||||||
|
|
||||||
|
Execute (Set syntax markdown):
|
||||||
|
call SetSyntax('markdown')
|
||||||
|
|
||||||
|
Do (Enter link):
|
||||||
|
gg\<Cr>
|
||||||
|
A__HERE1__\<Esc>
|
||||||
|
ggj\<Cr>
|
||||||
|
A__HERE2__\<Esc>
|
||||||
|
|
||||||
|
Expect():
|
||||||
|
[link1](#i-v-p-741528)
|
||||||
|
[link2](#i-v-p-741528-2)
|
||||||
|
|
||||||
|
# I [V p](h) (7.415.28)__HERE1__
|
||||||
|
|
||||||
|
# I [V p](h) 741.528__HERE2__
|
||||||
|
|
||||||
Given vimwiki (VimwikiTOC is broken against headers with link #182 {{{1):
|
Given vimwiki (VimwikiTOC is broken against headers with link #182 {{{1):
|
||||||
[A link B](#a-link-b)
|
[A link B](#a-link-b)
|
||||||
[tlink](#tlink)
|
[tlink](#tlink)
|
||||||
@ -34,7 +59,7 @@ Given vimwiki (VimwikiTOC is broken against headers with link #182 {{{1):
|
|||||||
#### [link]() (333)
|
#### [link]() (333)
|
||||||
|
|
||||||
|
|
||||||
Execute (VimwikiTOC: Set syntax markdown && Set sw=8):
|
Execute (Set syntax markdown):
|
||||||
call SetSyntax('markdown')
|
call SetSyntax('markdown')
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user