Config: Permit tag configuration for syntax, search and match (Issue #922 #928)

Affects: (via user configurated regexp)

- VimwikiGenerateTagLinks
- follow_link
- completion
- Syntax Highlighting
This commit is contained in:
Tinmarino
2020-08-08 04:02:15 -04:00
parent dede5a1eea
commit 188ead55db
11 changed files with 459 additions and 232 deletions

View File

@@ -453,7 +453,7 @@ function! vimwiki#base#generate_links(create, ...) abort
endfunction
" Update buffer with generator super power
let links_rx = '\%(^\s*$\)\|\%('.vimwiki#vars#get_syntaxlocal('rxListBullet').'\)'
let links_rx = '\%(^\s*$\)\|^\s*\%('.vimwiki#vars#get_syntaxlocal('rxListBullet').'\)'
call vimwiki#base#update_listing_in_buffer(
\ GeneratorLinks,
\ vimwiki#vars#get_global('links_header'),
@@ -617,6 +617,7 @@ endfunction
" Parse file. Returns list of all anchors
" Called: vimwiki#base#check_links() for all wiki files
function! vimwiki#base#get_anchors(filename, syntax) abort
" Clause: if not readable
if !filereadable(a:filename)
@@ -637,7 +638,7 @@ function! vimwiki#base#get_anchors(filename, syntax) abort
" Collect: headers
let h_match = matchlist(line, rxheader)
if !empty(h_match)
let header = vimwiki#u#trim(h_match[2])
let header = vimwiki#base#normalize_anchor(h_match[2])
" Mesure: header level
let level = len(h_match[1])
call add(anchors, header)
@@ -654,8 +655,8 @@ function! vimwiki#base#get_anchors(filename, syntax) abort
let current_complete_anchor .= anchor_level[l].'#'
endif
endfor
" TODO: should not that be out of the if branch ?
let current_complete_anchor .= header
" TODO: should not that be out of the if branch ?
call add(anchors, current_complete_anchor)
endif
endif
@@ -681,7 +682,8 @@ function! vimwiki#base#get_anchors(filename, syntax) abort
if tag_group_text ==? ''
break
endif
for tag_text in split(tag_group_text, ':')
let sep = vimwiki#vars#get_syntaxlocal('tag_format', a:syntax).sep
for tag_text in split(tag_group_text, sep)
call add(anchors, tag_text)
if current_complete_anchor !=? ''
call add(anchors, current_complete_anchor.'#'.tag_text)
@@ -712,7 +714,8 @@ endfunction
" :param: (1) previous_anchors <dic[IN/OUT]> of previous normalized anchor
" -- to know if must append -2, updated on the fly
" Return: anchor <string> => link in TOC
function! s:normalize_anchor(anchor, ...) abort
" Called: vimwiki#base#table_of_contents
function! vimwiki#base#normalize_anchor(anchor, ...) abort
" Note: See unormalize
" 0 Get in
let anchor = a:anchor
@@ -756,7 +759,7 @@ endfunction
" -- with or without suffix
" -- Ex: ['toto", 2] => search for the second occurrence of toto
" Called: jump_to_anchor
function! s:unnormalize_anchor(anchor) abort
function! vimwiki#base#unnormalize_anchor(anchor) abort
" Note:
" -- Pandoc keep the '_' in anchor
" -- Done after: Add spaces leading and trailing => Later with the template
@@ -832,84 +835,85 @@ function! s:jump_to_anchor(anchor) abort
for segment in segments
" Craft segment pattern so that it is case insensitive and also matches dashes
" in anchor link with spaces in heading
let [segment_re, segment_nb, segment_suffix] = s:unnormalize_anchor(segment)
let [segment_norm_re, segment_nb, segment_suffix] = vimwiki#base#unnormalize_anchor(segment)
" Try once with suffix (If header ends with number)
let res = s:jump_to_segment(segment_re . segment_suffix, 1)
let res = s:jump_to_segment(segment, segment_norm_re . segment_suffix, 1)
" Try segment_nb times otherwise
if res != 0
let res = s:jump_to_segment(segment_re, segment_nb)
let res = s:jump_to_segment(segment, segment_norm_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)
function! s:jump_to_segment(segment, segment_norm_re, segment_nb) abort
" Save cursor %% Initialize at top of line
let oldpos = getpos('.')
call cursor(1, 1)
let anchor_header = s:safesubstitute(
\ vimwiki#vars#get_syntaxlocal('header_match'),
\ '__Header__', a:segment_re, 'g')
let anchor_bold = s:safesubstitute(
\ vimwiki#vars#get_syntaxlocal('bold_match'),
\ '__Text__', a:segment_re, 'g')
let anchor_tag = s:safesubstitute(
\ vimwiki#vars#get_syntaxlocal('tag_match'),
\ '__Tag__', a:segment_re, 'g')
" Get anchor regex
let anchor_header = s:safesubstitute(
\ vimwiki#vars#get_syntaxlocal('header_match'),
\ '__Header__', a:segment_norm_re, 'g')
let anchor_bold = s:safesubstitute(
\ vimwiki#vars#get_syntaxlocal('bold_match'),
\ '__Text__', a:segment, 'g')
let anchor_tag = s:safesubstitute(
\ vimwiki#vars#get_syntaxlocal('tag_match'),
\ '__Tag__', a:segment, 'g')
" Go: Move cursor: maybe more than onces (see markdown suffix)
let success_nb = 0
let is_last_segment = 0
for i in range(a:segment_nb)
" Search
let pos = 0
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_bold, 'Wc')
" Go: Move cursor: maybe more than onces (see markdown suffix)
let success_nb = 0
let is_last_segment = 0
for i in range(a:segment_nb)
" Search
let pos = 0
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_bold, 'Wc')
" Succed: Get the result and reloop or leave
if pos != 0
" Avance, one line more to not rematch the same pattern if not last segment_nb
if success_nb < a:segment_nb-1
let pos += 1
let is_last_segment = -1
endif
call cursor(pos, 1)
let success_nb += 1
" Break if last line (avoid infinite loop)
" Anyway leave the loop: (Imagine heading # 7271212 at last line)
if pos >= line('$')
return 0
endif
" Fail:
" Do not move
" But maybe suffix -2 is not the segment number but the real header suffix
else
" If fail at first: do not move
if i == 0
call setpos('.', oldpos)
endif
" Anyway leave the loop: (Imagine heading # 7271212, you do not want to loop all that)
" Go one line back: if I advanced too much
if is_last_segment == -1 | call cursor(line('.')-1, 1) | endif
return 1
" Succeed: Get the result and reloop or leave
if pos != 0
" Avance, one line more to not rematch the same pattern if not last segment_nb
if success_nb < a:segment_nb-1
let pos += 1
let is_last_segment = -1
endif
endfor
call cursor(pos, 1)
let success_nb += 1
" Check if happy
if success_nb == a:segment_nb
return 0
" Break if last line (avoid infinite loop)
" Anyway leave the loop: (Imagine heading # 7271212 at last line)
if pos >= line('$')
return 0
endif
" Fail:
" Do not move
" But maybe suffix -2 is not the segment number but the real header suffix
else
" If fail at first: do not move
if i == 0
call setpos('.', oldpos)
endif
" Anyway leave the loop: (Imagine heading # 7271212, you do not want to loop all that)
" Go one line back: if I advanced too much
if is_last_segment == -1 | call cursor(line('.')-1, 1) | endif
return 1
endif
endfor
" Or keep on (i.e more than once segment)
let oldpos = getpos('.')
" Check if happy
if success_nb == a:segment_nb
return 0
endif
" Said 'fail' to caller
return 1
" Or keep on (i.e more than once segment)
let oldpos = getpos('.')
" Said 'fail' to caller
return 1
endfunction
@@ -2482,7 +2486,7 @@ function! vimwiki#base#table_of_contents(create) abort
endif
" Normalize anchor
let anchor = s:normalize_anchor(anchor, previous_anchors)
let anchor = vimwiki#base#normalize_anchor(anchor, previous_anchors)
" Insert link in template
let link = s:safesubstitute(link_tpl, '__LinkUrl__',
@@ -2494,7 +2498,7 @@ function! vimwiki#base#table_of_contents(create) abort
return lines
endfunction
let links_rx = '\%(^\s*$\)\|\%(^\s*\%('.vimwiki#vars#get_syntaxlocal('rxListBullet').'\)\)'
let links_rx = '\%(^\s*$\)\|^\s*\%(\%('.vimwiki#vars#get_syntaxlocal('rxListBullet').'\)\)'
call vimwiki#base#update_listing_in_buffer(
\ GeneratorTOC,
\ toc_header_text,

View File

@@ -70,14 +70,15 @@ endfunction
" Scan the list of text lines (argument) and produces tags metadata as a list of tag entries.
function! s:scan_tags(lines, page_name) abort
let entries = []
" Code wireframe to scan for headers -- borrowed from
" vimwiki#base#get_anchors(), with minor modifications.
let entries = []
" Get syntax wide regex
let rxheader = vimwiki#vars#get_syntaxlocal('header_search')
let rxtag = vimwiki#vars#get_syntaxlocal('tag_search')
let tag_search_rx = vimwiki#vars#get_syntaxlocal('tag_search')
let tag_format = vimwiki#vars#get_syntaxlocal('tag_format')
let anchor_level = ['', '', '', '', '', '', '']
let current_complete_anchor = ''
@@ -97,7 +98,7 @@ function! s:scan_tags(lines, page_name) abort
let h_match = matchlist(line, rxheader)
if !empty(h_match) " got a header
let header_line_nr = line_nr
let header = vimwiki#u#trim(h_match[2])
let header = vimwiki#base#normalize_anchor(h_match[2])
let level = len(h_match[1])
let anchor_level[level-1] = header
for l in range(level, 6)
@@ -120,28 +121,32 @@ function! s:scan_tags(lines, page_name) abort
" Scan line for tags. There can be many of them.
let str = line
while 1
let tag_group = matchstr(str, rxtag)
if tag_group ==? ''
" Get all matches
let tag_groups = []
call substitute(str, tag_search_rx, '\=add(tag_groups, submatch(0))', 'g')
if tag_groups == []
break
endif
let tagend = matchend(str, rxtag)
let tagend = matchend(str, tag_search_rx)
let str = str[(tagend):]
for tag in split(tag_group, ':')
" Create metadata entry
let entry = {}
let entry.tagname = tag
let entry.lineno = line_nr
if line_nr <= PROXIMITY_LINES_NR && header_line_nr < 0
" Tag appeared at the top of the file
let entry.link = a:page_name
elseif line_nr <= (header_line_nr + PROXIMITY_LINES_NR)
" Tag appeared right below a header
let entry.link = a:page_name . '#' . current_complete_anchor
else
" Tag stands on its own
let entry.link = a:page_name . '#' . tag
endif
call add(entries, entry)
for tag_group in tag_groups
for tag in split(tag_group, tag_format.sep)
" Create metadata entry
let entry = {}
let entry.tagname = tag
let entry.lineno = line_nr
if line_nr <= PROXIMITY_LINES_NR && header_line_nr < 0
" Tag appeared at the top of the file
let entry.link = a:page_name
elseif line_nr <= (header_line_nr + PROXIMITY_LINES_NR)
" Tag appeared right below a header
let entry.link = a:page_name . '#' . current_complete_anchor
else
" Tag stands on its own
let entry.link = a:page_name . '#' . tag
endif
call add(entries, entry)
endfor
endfor
endwhile
@@ -391,7 +396,7 @@ function! vimwiki#tags#generate_tags(create, ...) abort
let tag_match = printf('rxH%d', header_level + 1)
let links_rx = '^\%('.vimwiki#vars#get_syntaxlocal(tag_match).'\)\|'.
\ '\%(^\s*$\)\|\%('.vimwiki#vars#get_syntaxlocal('rxListBullet').'\)'
\ '\%(^\s*$\)\|^\s*\%('.vimwiki#vars#get_syntaxlocal('rxListBullet').'\)'
call vimwiki#base#update_listing_in_buffer(
\ GeneratorTags,

View File

@@ -38,7 +38,7 @@ endfunction
" ----------------------------------------------------------
" 1. Global
" 1. Global {{{1
" ----------------------------------------------------------
" Populate global variable <- user & default
@@ -337,12 +337,15 @@ endfunction
" ----------------------------------------------------------
" 2. Buffer local
" 2. Buffer local {{{1
" ----------------------------------------------------------
" Populate local variable <- user & default
" Called: s:vimwiki#vars#init
function! s:populate_wikilocal_options() abort
" Warning Dev: if type is dict,
" -- the default dict gets extended and not replaced: keys are not deleted
" Init local variable container
let g:vimwiki_wikilocal_vars = []
@@ -393,6 +396,14 @@ function! s:populate_wikilocal_options() abort
\ 'template_ext': {'type': type(''), 'default': '.tpl'},
\ 'template_path': {'type': type(''), 'default': $HOME . '/vimwiki/templates/'},
\ 'text_ignore_newline': {'type': type(0), 'default': 1, 'min': 0, 'max': 1},
\ 'tag_format': {'type': type({}), 'default': {
\ 'pre': '^\|\s',
\ 'pre_mark': ':',
\ 'in': '[^:''[:space:]]\+',
\ 'sep': ':',
\ 'post_mark': ':',
\ 'post': '\s\|$',
\ 'conceal': 0, 'cchar':''}},
\ 'toc_header': {'type': type(''), 'default': 'Contents', 'min_length': 1},
\ 'toc_header_level': {'type': type(0), 'default': 1, 'min': 1, 'max': 6},
\ 'toc_link_format': {'type': type(0), 'default': 0, 'min': 0, 'max': 1},
@@ -402,8 +413,16 @@ function! s:populate_wikilocal_options() abort
let default_wiki_settings = {}
for key in keys(default_values)
if exists('g:vimwiki_'.key)
" Check type
call s:check_users_value(key, g:vimwiki_{key}, default_values[key], 1)
let default_wiki_settings[key] = g:vimwiki_{key}
" Update if dict
if default_values[key]['type'] == type({})
let default_wiki_settings[key] = default_values[key].default
call extend(default_wiki_settings[key], g:vimwiki_{key})
" Set if other var
else
let default_wiki_settings[key] = g:vimwiki_{key}
endif
else
let default_wiki_settings[key] = default_values[key].default
endif
@@ -419,7 +438,14 @@ function! s:populate_wikilocal_options() abort
if key ==# 'list_margin'
let s:margin_set_by_user = 1
endif
let new_wiki_settings[key] = users_wiki_settings[key]
" Update if dict
if default_values[key]['type'] == type({})
let new_wiki_settings[key] = extend({}, default_values[key].default)
let new_wiki_settings[key] = extend(new_wiki_settings.key, users_wiki_settings[key])
" Set if other var
else
let new_wiki_settings[key] = users_wiki_settings[key]
endif
else
let new_wiki_settings[key] = default_wiki_settings[key]
endif
@@ -578,7 +604,7 @@ endfunction
" ----------------------------------------------------------
" 3. Syntax specific
" 3. Syntax specific {{{1
" ----------------------------------------------------------
" Populate syntax variable
@@ -615,6 +641,42 @@ function! vimwiki#vars#populate_syntax_vars(syntax) abort
let syntax_dic['cycle_bullets'] =
\ vimwiki#vars#get_wikilocal('cycle_bullets')
" Tag: get var
let syntax_dic.tag_format = {}
let tf = syntax_dic.tag_format
call extend(tf, vimwiki#vars#get_wikilocal('tag_format'))
" Tag: Close regex
for key in ['pre', 'pre_mark', 'in', 'sep', 'post_mark', 'post']
let tf[key] = '\%(' . tf[key] . '\)'
endfor
" Match \s<tag[:tag:tag:tag...]>\s
" Tag: highlighting
" Used: syntax/vimwiki.vim
let syntax_dic.rxTags =
\ tf.pre . '\@<=' . tf.pre_mark . tf.in
\ . '\%(' . tf.sep . tf.in . '\)*'
\ . tf.post_mark . tf.post . '\@='
" Tag: searching for all
" Used: vimwiki#base#get_anchors <- GenerateTagLinks
let syntax_dic.tag_search =
\ tf.pre . tf.pre_mark . '\zs'
\ . tf.in . '\%(' . tf.sep . tf.in . '\)*'
\ . '\ze' . tf.post_mark . tf.post
" Tag: matching a specific: when goto tag
" Used: tags.vim->s:scan_tags
" Match <[tag:tag:...tag:]__TAG__[:tag...:tag]>
let syntax_dic.tag_match =
\ tf.pre . tf.pre_mark
\ . '\%(' . tf.in . tf.sep . '\)*'
\ . '__Tag__'
\ . '\%(' . tf.sep . tf.in . '\)*'
\ . tf.post_mark . tf.post
" Populate generic stuff
let header_symbol = syntax_dic.rxH
if syntax_dic.symH
@@ -771,7 +833,7 @@ function! s:populate_list_vars(wiki) abort
let a:wiki.multiple_bullet_chars =
\ recurring_bullets
\ ? a:wiki.bullet_types : []
" Create regexp for bulleted list items
if !empty(a:wiki.bullet_types)
let rxListBullet =
@@ -1007,7 +1069,7 @@ endfunction
" ----------------------------------------------------------
" 4. Getter, Setter (exported)
" 4. Getter, Setter (exported) {{{1
" ----------------------------------------------------------
" Get syntax variable