Adjust generated header behavior for TOC, etc.

This commit is contained in:
Steven Stallion 2019-03-21 14:31:04 -05:00 committed by Steven Stallion
parent 8f5d38365b
commit 6c2f9f999d
5 changed files with 170 additions and 28 deletions

View File

@ -9,6 +9,9 @@ endif
let g:loaded_vimwiki_auto = 1 let g:loaded_vimwiki_auto = 1
let g:vimwiki_max_scan_for_caption = 5
function! s:safesubstitute(text, search, replace, mode) function! s:safesubstitute(text, search, replace, mode)
" Substitute regexp but do not interpret replace " Substitute regexp but do not interpret replace
let escaped = escape(a:replace, '\&') let escaped = escape(a:replace, '\&')
@ -351,23 +354,34 @@ function! vimwiki#base#generate_links()
let bullet = repeat(' ', vimwiki#lst#get_list_margin()) . vimwiki#lst#default_symbol().' ' let bullet = repeat(' ', vimwiki#lst#get_list_margin()) . vimwiki#lst#default_symbol().' '
for link in links for link in links
let abs_filepath = vimwiki#path#abs_path_of_link(link) let link_infos = vimwiki#base#resolve_link(link)
if !s:is_diary_file(abs_filepath) if !s:is_diary_file(link_infos.filename)
if vimwiki#vars#get_wikilocal('syntax') == 'markdown' if vimwiki#vars#get_wikilocal('syntax') == 'markdown'
let link_tpl = vimwiki#vars#get_syntaxlocal('Weblink1Template') let link_tpl = vimwiki#vars#get_syntaxlocal('Weblink1Template')
else else
let link_tpl = vimwiki#vars#get_global('WikiLinkTemplate1') let link_tpl = vimwiki#vars#get_global('WikiLinkTemplate1')
endif endif
let link_caption = vimwiki#base#read_caption(link_infos.filename)
if link_caption == '' " default to link if caption not found
let link_caption = link
endif
let entry = s:safesubstitute(link_tpl, '__LinkUrl__', link, '') let entry = s:safesubstitute(link_tpl, '__LinkUrl__', link, '')
let entry = s:safesubstitute(entry, '__LinkDescription__', link, '') let entry = s:safesubstitute(entry, '__LinkDescription__', link_caption, '')
call add(lines, bullet. entry) call add(lines, bullet. entry)
endif endif
endfor endfor
let links_rx = '\m^\s*'.vimwiki#u#escape(vimwiki#lst#default_symbol()).' ' let links_rx = '\%(^\s*$\)\|\%('.vimwiki#vars#get_syntaxlocal('rxListBullet').'\)'
call vimwiki#base#update_listing_in_buffer(lines, 'Generated Links', links_rx, line('$')+1, 1, 1) call vimwiki#base#update_listing_in_buffer(
\ lines,
\ vimwiki#vars#get_global('links_header'),
\ links_rx,
\ line('$')+1,
\ vimwiki#vars#get_global('links_header_level'),
\ 1)
endfunction endfunction
@ -1092,6 +1106,11 @@ function! vimwiki#base#update_listing_in_buffer(strings, start_header,
let start_lnum = a:default_lnum let start_lnum = a:default_lnum
let is_cursor_after_listing = ( cursor_line > a:default_lnum ) let is_cursor_after_listing = ( cursor_line > a:default_lnum )
let whitespaces_in_first_line = '' let whitespaces_in_first_line = ''
" append newline if not replacing first line
if start_lnum > 1
keepjumps call append(start_lnum -1, '')
let start_lnum += 1
endif
endif endif
let start_of_listing = start_lnum let start_of_listing = start_lnum
@ -1102,13 +1121,24 @@ function! vimwiki#base#update_listing_in_buffer(strings, start_header,
\ '__Header__', a:start_header, '') \ '__Header__', a:start_header, '')
keepjumps call append(start_lnum - 1, new_header) keepjumps call append(start_lnum - 1, new_header)
let start_lnum += 1 let start_lnum += 1
let lines_diff += 1 + len(a:strings) let lines_diff += 1
if vimwiki#vars#get_wikilocal('syntax') == 'markdown'
for _ in range(vimwiki#vars#get_global('markdown_header_style'))
keepjumps call append(start_lnum - 1, '')
let start_lnum += 1
let lines_diff += 1
endfor
endif
for string in a:strings for string in a:strings
keepjumps call append(start_lnum - 1, string) keepjumps call append(start_lnum - 1, string)
let start_lnum += 1 let start_lnum += 1
let lines_diff += 1
endfor endfor
" append an empty line if there is not one
if start_lnum <= line('$') && getline(start_lnum) !~# '\m^\s*$' " remove empty line if end of file, otherwise append if needed
if start_lnum == line('$')
silent exe 'keepjumps ' . start_lnum.'delete _'
elseif start_lnum < line('$') && getline(start_lnum) !~# '\m^\s*$'
keepjumps call append(start_lnum - 1, '') keepjumps call append(start_lnum - 1, '')
let lines_diff += 1 let lines_diff += 1
endif endif
@ -1894,7 +1924,7 @@ function! vimwiki#base#table_of_contents(create)
call add(lines, startindent.repeat(indentstring, lvl-1).bullet.link) call add(lines, startindent.repeat(indentstring, lvl-1).bullet.link)
endfor endfor
let links_rx = '\m^\s*'.vimwiki#u#escape(vimwiki#lst#default_symbol()).' ' let links_rx = '\%(^\s*$\)\|\%('.vimwiki#vars#get_syntaxlocal('rxListBullet').'\)'
call vimwiki#base#update_listing_in_buffer( call vimwiki#base#update_listing_in_buffer(
\ lines, \ lines,
@ -2114,6 +2144,21 @@ function! vimwiki#base#complete_links_escaped(ArgLead, CmdLine, CursorPos) abort
endfunction endfunction
function! vimwiki#base#read_caption(file)
let rx_header = vimwiki#vars#get_syntaxlocal('rxHeader')
if filereadable(a:file)
for line in readfile(a:file, '', g:vimwiki_max_scan_for_caption)
if line =~# rx_header
return vimwiki#u#trim(matchstr(line, rx_header))
endif
endfor
endif
return ''
endfunction
" ------------------------------------------------------------------------- " -------------------------------------------------------------------------
" Load syntax-specific Wiki functionality " Load syntax-specific Wiki functionality
for s:syn in s:vimwiki_get_known_syntaxes() for s:syn in s:vimwiki_get_known_syntaxes()

View File

@ -10,9 +10,6 @@ endif
let g:loaded_vimwiki_diary_auto = 1 let g:loaded_vimwiki_diary_auto = 1
let s:vimwiki_max_scan_for_caption = 5
function! s:prefix_zero(num) function! s:prefix_zero(num)
if a:num < 10 if a:num < 10
return '0'.a:num return '0'.a:num
@ -67,7 +64,7 @@ function! s:get_first_header(fl)
" Get the first header in the file within the first s:vimwiki_max_scan_for_caption lines. " Get the first header in the file within the first s:vimwiki_max_scan_for_caption lines.
let header_rx = vimwiki#vars#get_syntaxlocal('rxHeader') let header_rx = vimwiki#vars#get_syntaxlocal('rxHeader')
for line in readfile(a:fl, '', s:vimwiki_max_scan_for_caption) for line in readfile(a:fl, '', g:vimwiki_max_scan_for_caption)
if line =~# header_rx if line =~# header_rx
return vimwiki#u#trim(matchstr(line, header_rx)) return vimwiki#u#trim(matchstr(line, header_rx))
endif endif
@ -210,9 +207,13 @@ function! s:format_diary()
let links_with_captions = s:read_captions(s:get_diary_files()) let links_with_captions = s:read_captions(s:get_diary_files())
let g_files = s:group_links(links_with_captions) let g_files = s:group_links(links_with_captions)
let g_keys = s:sort(keys(g_files))
for year in s:sort(keys(g_files)) for year in g_keys
if len(result) > 0
call add(result, '') call add(result, '')
endif
call add(result, call add(result,
\ substitute(vimwiki#vars#get_syntaxlocal('rxH2_Template'), '__Header__', year , '')) \ substitute(vimwiki#vars#get_syntaxlocal('rxH2_Template'), '__Header__', year , ''))
@ -221,6 +222,12 @@ function! s:format_diary()
call add(result, substitute(vimwiki#vars#get_syntaxlocal('rxH3_Template'), call add(result, substitute(vimwiki#vars#get_syntaxlocal('rxH3_Template'),
\ '__Header__', s:get_month_name(month), '')) \ '__Header__', s:get_month_name(month), ''))
if vimwiki#vars#get_wikilocal('syntax') == 'markdown'
for _ in range(vimwiki#vars#get_global('markdown_header_style'))
call add(result, '')
endfor
endif
for [fl, captions] in s:sort(items(g_files[year][month])) for [fl, captions] in s:sort(items(g_files[year][month]))
let topcap = captions['top'] let topcap = captions['top']
let link_tpl = vimwiki#vars#get_global('WikiLinkTemplate2') let link_tpl = vimwiki#vars#get_global('WikiLinkTemplate2')
@ -239,9 +246,10 @@ function! s:format_diary()
let top_link_tpl = link_tpl let top_link_tpl = link_tpl
endif endif
let bullet = vimwiki#lst#default_symbol().' '
let entry = substitute(top_link_tpl, '__LinkUrl__', fl, '') let entry = substitute(top_link_tpl, '__LinkUrl__', fl, '')
let entry = substitute(entry, '__LinkDescription__', topcap, '') let entry = substitute(entry, '__LinkDescription__', topcap, '')
call add(result, repeat(' ', vimwiki#lst#get_list_margin()).'* '.entry) call add(result, repeat(' ', vimwiki#lst#get_list_margin()).bullet.entry)
for [depth, subcap] in captions['rest'] for [depth, subcap] in captions['rest']
if empty(subcap) if empty(subcap)
@ -371,9 +379,10 @@ function! vimwiki#diary#generate_diary_section()
let current_file = vimwiki#path#path_norm(expand("%:p")) let current_file = vimwiki#path#path_norm(expand("%:p"))
let diary_file = vimwiki#path#path_norm(s:diary_index()) let diary_file = vimwiki#path#path_norm(s:diary_index())
if vimwiki#path#is_equal(current_file, diary_file) if vimwiki#path#is_equal(current_file, diary_file)
let content_rx = '^\%(\s*[*-] \)\|\%(^\s*$\)\|\%('.vimwiki#vars#get_syntaxlocal('rxHeader').'\)' let content_rx = '^\%('.vimwiki#vars#get_syntaxlocal('rxHeader').'\)\|'.
\ '\%(^\s*$\)\|\%('.vimwiki#vars#get_syntaxlocal('rxListBullet').'\)'
call vimwiki#base#update_listing_in_buffer(s:format_diary(), call vimwiki#base#update_listing_in_buffer(s:format_diary(),
\ vimwiki#vars#get_wikilocal('diary_header'), content_rx, line('$')+1, 1, 1) \ vimwiki#vars#get_wikilocal('diary_header'), content_rx, 1, 1, 1)
else else
echomsg 'Vimwiki Error: You can generate diary links only in a diary index page!' echomsg 'Vimwiki Error: You can generate diary links only in a diary index page!'
endif endif

View File

@ -296,6 +296,7 @@ function! vimwiki#tags#generate_tags(...) abort
let need_all_tags = (a:0 == 0) let need_all_tags = (a:0 == 0)
let specific_tags = a:000 let specific_tags = a:000
let header_level = vimwiki#vars#get_global('tags_header_level')
let metadata = s:load_tags_metadata() let metadata = s:load_tags_metadata()
" make a dictionary { tag_name: [tag_links, ...] } " make a dictionary { tag_name: [tag_links, ...] }
@ -310,26 +311,55 @@ function! vimwiki#tags#generate_tags(...) abort
endfor endfor
endfor endfor
let tag_match = printf('rxH%d', header_level + 1)
let tag_tpl = printf('rxH%d_Template', header_level + 1)
let lines = [] let lines = []
let bullet = repeat(' ', vimwiki#lst#get_list_margin()).vimwiki#lst#default_symbol().' ' let bullet = repeat(' ', vimwiki#lst#get_list_margin()).vimwiki#lst#default_symbol().' '
for tagname in sort(keys(tags_entries)) for tagname in sort(keys(tags_entries))
if need_all_tags || index(specific_tags, tagname) != -1 if need_all_tags || index(specific_tags, tagname) != -1
call extend(lines, [ if len(lines) > 0
\ '', call add(lines, '')
\ substitute(vimwiki#vars#get_syntaxlocal('rxH2_Template'), '__Header__', tagname, ''), endif
\ '' ])
call add(lines,
\ substitute(vimwiki#vars#get_syntaxlocal(tag_tpl), '__Header__', tagname, ''))
if vimwiki#vars#get_wikilocal('syntax') == 'markdown'
for _ in range(vimwiki#vars#get_global('markdown_header_style'))
call add(lines, '')
endfor
endif
for taglink in sort(tags_entries[tagname]) for taglink in sort(tags_entries[tagname])
call add(lines, bullet . substitute(vimwiki#vars#get_global('WikiLinkTemplate1'), if vimwiki#vars#get_wikilocal('syntax') == 'markdown'
\ '__LinkUrl__', taglink, '')) let link_tpl = vimwiki#vars#get_syntaxlocal('Weblink3Template')
let link_infos = vimwiki#base#resolve_link(taglink)
let link_caption = vimwiki#base#read_caption(link_infos.filename)
let link_text = split(taglink, '#', 1)[0]
let entry = substitute(link_tpl, '__LinkUrl__', link_text, '')
let entry = substitute(entry, '__LinkAnchor__', link_infos.anchor, '')
let entry = substitute(entry, '__LinkDescription__', link_caption, '')
call add(lines, bullet.entry)
else
let link_tpl = vimwiki#vars#get_global('WikiLinkTemplate1')
call add(lines, bullet . substitute(link_tpl, '__LinkUrl__', taglink, ''))
endif
endfor endfor
endif endif
endfor endfor
let links_rx = '\m\%(^\s*$\)\|\%('.vimwiki#vars#get_syntaxlocal('rxH2').'\)\|\%(^\s*' let links_rx = '^\%('.vimwiki#vars#get_syntaxlocal(tag_match).'\)\|'.
\ .vimwiki#u#escape(vimwiki#lst#default_symbol()).' ' \ '\%(^\s*$\)\|\%('.vimwiki#vars#get_syntaxlocal('rxListBullet').'\)'
\ .vimwiki#vars#get_syntaxlocal('rxWikiLink').'$\)'
call vimwiki#base#update_listing_in_buffer(lines, 'Generated Tags', links_rx, line('$')+1, 1, 1) call vimwiki#base#update_listing_in_buffer(
\ lines,
\ vimwiki#vars#get_global('tags_header'),
\ links_rx,
\ line('$')+1,
\ header_level,
\ 1)
endfunction endfunction

View File

@ -165,13 +165,18 @@ function! s:read_global_settings_from_user()
\ 'html_header_numbering_sym': {'type': type(''), 'default': ''}, \ 'html_header_numbering_sym': {'type': type(''), 'default': ''},
\ 'list_ignore_newline': {'type': type(0), 'default': 1, 'min': 0, 'max': 1}, \ 'list_ignore_newline': {'type': type(0), 'default': 1, 'min': 0, 'max': 1},
\ 'text_ignore_newline': {'type': type(0), 'default': 1, 'min': 0, 'max': 1}, \ 'text_ignore_newline': {'type': type(0), 'default': 1, 'min': 0, 'max': 1},
\ 'links_header': {'type': type(''), 'default': 'Generated Links', 'min_length': 1},
\ 'links_header_level': {'type': type(0), 'default': 1, 'min': 1, 'max': 6},
\ 'listsyms': {'type': type(''), 'default': ' .oOX', 'min_length': 2}, \ 'listsyms': {'type': type(''), 'default': ' .oOX', 'min_length': 2},
\ 'listsym_rejected': {'type': type(''), 'default': '-', 'length': 1}, \ 'listsym_rejected': {'type': type(''), 'default': '-', 'length': 1},
\ 'map_prefix': {'type': type(''), 'default': '<Leader>w'}, \ 'map_prefix': {'type': type(''), 'default': '<Leader>w'},
\ 'markdown_header_style': {'type': type(0), 'default': 1, 'min':0, 'max': 2},
\ 'markdown_link_ext': {'type': type(0), 'default': 0, 'min': 0, 'max': 1}, \ 'markdown_link_ext': {'type': type(0), 'default': 0, 'min': 0, 'max': 1},
\ 'menu': {'type': type(''), 'default': 'Vimwiki'}, \ 'menu': {'type': type(''), 'default': 'Vimwiki'},
\ 'table_auto_fmt': {'type': type(0), 'default': 1, 'min': 0, 'max': 1}, \ 'table_auto_fmt': {'type': type(0), 'default': 1, 'min': 0, 'max': 1},
\ 'table_mappings': {'type': type(0), 'default': 1, 'min': 0, 'max': 1}, \ 'table_mappings': {'type': type(0), 'default': 1, 'min': 0, 'max': 1},
\ 'tags_header': {'type': type(''), 'default': 'Generated Tags', 'min_length': 1},
\ 'tags_header_level': {'type': type(0), 'default': 1, 'min': 1, 'max': 5},
\ 'toc_header': {'type': type(''), 'default': 'Contents', 'min_length': 1}, \ 'toc_header': {'type': type(''), 'default': 'Contents', 'min_length': 1},
\ 'toc_header_level': {'type': type(0), 'default': 1, 'min': 1, 'max': 6}, \ 'toc_header_level': {'type': type(0), 'default': 1, 'min': 1, 'max': 6},
\ 'url_maxsave': {'type': type(0), 'default': 15, 'min': 0}, \ 'url_maxsave': {'type': type(0), 'default': 15, 'min': 0},
@ -707,6 +712,10 @@ function! s:populate_extra_markdown_vars()
" [DESCRIPTION](ANCHOR) " [DESCRIPTION](ANCHOR)
let mkd_syntax.Weblink2Template = mkd_syntax.rxWeblink1Prefix . '__LinkDescription__'. let mkd_syntax.Weblink2Template = mkd_syntax.rxWeblink1Prefix . '__LinkDescription__'.
\ mkd_syntax.rxWeblink1Separator. '__LinkUrl__'. mkd_syntax.rxWeblink1Suffix \ mkd_syntax.rxWeblink1Separator. '__LinkUrl__'. mkd_syntax.rxWeblink1Suffix
" [DESCRIPTION](FILE#ANCHOR)
let mkd_syntax.Weblink3Template = mkd_syntax.rxWeblink1Prefix . '__LinkDescription__'.
\ mkd_syntax.rxWeblink1Separator. '__LinkUrl__'. mkd_syntax.rxWeblink1Ext.
\ '#__LinkAnchor__'. mkd_syntax.rxWeblink1Suffix
let valid_chars = '[^\\]' let valid_chars = '[^\\]'

View File

@ -2981,6 +2981,54 @@ Value Description~
Default: 0 Default: 0
------------------------------------------------------------------------------
*g:vimwiki_links_header*
A string with the magic header that tells Vimwiki where the generated links
are located in a file. You can change it to the appropriate word in your
mother tongue like this: >
let g:vimwiki_links_header = 'Generierte Links'
The default is 'Generated Links'.
------------------------------------------------------------------------------
*g:vimwiki_links_header_level*
The header level of generated links. Valid values are from 1 to 6.
The default is 1.
------------------------------------------------------------------------------
*g:vimwiki_tags_header*
A string with the magic header that tells Vimwiki where the generated tags
are located in a file. You can change it to the appropriate word in your
mother tongue like this: >
let g:vimwiki_tags_header = 'Generierte Stichworte'
The default is 'Generated Tags'.
------------------------------------------------------------------------------
*g:vimwiki_tags_header_level*
The header level of generated tags. Valid values are from 1 to 5.
The default is 1.
------------------------------------------------------------------------------
*g:vimwiki_markdown_header_style*
The number of newlines to be inserted after a header is generated. Valid
values are from 0 to 2.
The default is 1.
============================================================================== ==============================================================================
13. Getting help *vimwiki-help* 13. Getting help *vimwiki-help*
@ -3058,6 +3106,7 @@ Contributors and their Github usernames in roughly chronological order:
- Zhuang Ma (@mzlogin) - Zhuang Ma (@mzlogin)
- Huy Le (@huynle) - Huy Le (@huynle)
- Nick Borden (@hcwndbyw) - Nick Borden (@hcwndbyw)
- Steven Stallion (@sstallion)
============================================================================== ==============================================================================