vimwiki-backup / vimwiki

Automatically exported from code.google.com/p/vimwiki
1 stars 1 forks source link

feature request: add tags support. #345

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
When I write more and more note on vimwiki. I found that it is hard to search 
through vimwiki. It is slow, use too much time.

So I think vimwiki support "tags" search will be more fast. Just like blog 
"tags".
So if vimwiki add an option to only do "tags" search instead of whole content 
search will be faster. Of course whole content is still useful when user can 
not find what he want with "tags" search.

Those "tags" can be append at the first line or the last line. (as a part of 
vimwiki)

Original issue reported on code.google.com by numbch...@gmail.com on 13 Jun 2012 at 2:41

GoogleCodeExporter commented 9 years ago
Something nice would be to have a simple
%tag foo
placeholder (like %title)
which could be included on individual generated html files, based on templates.

The important addition would be to enable to *also* automatically reference the 
%tag in some kind of tag index file when the html is generated via <leader>wh. 
This index file (tags.vw or whatever) would be later interpreted via its own 
template.

With some extra work from the user (on the templates), this would allow the 
generation of output that enables simple navigation via tags (like in wordpress 
and such) on the generated html site, while keeping vimwiki working as now, 
which means: really well.

Original comment by dkhorn...@gmail.com on 16 Jun 2012 at 8:15

GoogleCodeExporter commented 9 years ago
My headers typically contain pairs, in [yaml](http://www.yaml.org/) format:

NAME: VALUE

e.g.

Tags: tag1, tag2, ...

I have previously searched for tags using the same code as VimwikiSearch code, 
but as I recall, this expended unnecessary effort searching the entire contents 
of the files in your wiki, and not just the headers.

In principle, this could be sped-up by tweaking the arguments to "lvimgrep" ; 
e.g. using ":1lvimgrep" to restrict the "maximum number of matches" that are 
returned.  (see :help vimgrep).    

Here is the code I had previously used.  It hasn't been tested recently.  Is 
this helpful?
- S.

``` vim
  " *:VimwikiTagsearch*
  " *:VWT* /pattern/
  "     Search for /^Tags: *\(pattern\)$/ in all files of current wiki.
  "     This is useful for maintaining "Tags" metadata on across
  "     all wiki pages.  Navigate matches using same commands
  "     as for VimwikiSearch. Example: "VWT xxx\|yyy"

  exe 'au FileType vimwiki command! -buffer -nargs=* VimwikiTagsearch lvimgrep "^Tags: .*\(<args>\)" '.
        \ escape(VimwikiGet('path').'**/*'.VimwikiGet('ext'), ' ')
  exe 'au FileType vimwiki command! -buffer -nargs=* VWT lvimgrep "^Tags: .*\(<args>\)" '.
        \ escape(VimwikiGet('path').'**/*'.VimwikiGet('ext'), ' ')

  " *:VimwikiUntagged*
  " *:VWU*
  "     Search for /^Tags: *$/ in all files of current wiki.
  "     This is useful for maintaining "Tags" metadata on across
  "     all wiki pages.  Navigate matches using same commands
  "     as for VimwikiSearch.

  exe 'au FileType vimwiki command! -buffer VimwikiUntagged lvimgrep "^Tags: *$" '.
        \ escape(VimwikiGet('path').'**/*'.VimwikiGet('ext'), ' ')
  exe 'au FileType vimwiki command! -buffer VWU lvimgrep "^Tags: *$" '.
        \ escape(VimwikiGet('path').'**/*'.VimwikiGet('ext'), ' ')

Original comment by `stu.andrews` on 17 Jun 2012 at 6:06
GoogleCodeExporter commented 9 years ago
@stu.andrews: this seems very helpful. I'll test it as soon as I have some time 
off (i.e. not in the next couple of days).

Original comment by dkhorn...@gmail.com on 17 Jun 2012 at 3:25

GoogleCodeExporter commented 9 years ago
Here's what I did in order to implement tags for now:
1) looked for references to %title everywhere in the vimwiki package and 
duplicated the behaviour for %tag :
1.1) in syntax/vimwiki.vim : 
  syntax match VimwikiPlaceholder /^\s*%tag\%(\s.*\)\?$/ contains=VimwikiPlaceholderParam
" highlights %tag lines in the same way as %title lines
1.2) in autoload/vimwiki/html.vim :
" get tag.
 function! s:process_tag(placeholders, default_tag) "{{{
 if !empty(a:placeholders)
 for [placeholder, row, idx] in a:placeholders
   let [type, param] = placeholder
   if type == 'tag' && !empty(param)
     return param
   endif
 endfor
 endif
 return a:default_tag
 endfunction "}}}
 " tag -- placeholder                                                      
 if !processed
 if line =~ '^\s*%tag'
   let processed = 1
   let param = matchstr(line, '^\s*%tag\s\zs.*')
   let state.placeholder = ['tag', param]
 endif
 endif
" (...)
 let tag = s:process_tag(placeholders, fnamemodify(a:wikifile, ":t:r"))
 call map(html_lines, 'substitute(v:val, "%tag%", "'. tag .'", "g")')
" %tag won't appear in html contents and first %tag can be used in html template

2) Implemented stu.andrews' suggestion with the %tag syntax, in 
ftplugin/vimwiki.vim :
  exe 'au FileType vimwiki command! -buffer -nargs=* VimwikiTagsearch lvimgrep "^%tag .*\(<args>\)" '.
        \ escape(VimwikiGet('path').'**/*'.VimwikiGet('ext'), ' ')
  exe 'au FileType vimwiki command! -buffer -nargs=* VWT lvimgrep "^%tag .*\(<args>\)" '.
        \ escape(VimwikiGet('path').'**/*'.VimwikiGet('ext'), ' ')
  exe 'au FileType vimwiki command! -buffer VimwikiUntagged lvimgrep "^%tag *$" '.
        \ escape(VimwikiGet('path').'**/*'.VimwikiGet('ext'), ' ')
  exe 'au FileType vimwiki command! -buffer VWU lvimgrep "^%tag *$" '.
        \ escape(VimwikiGet('path').'**/*'.VimwikiGet('ext'), ' ')

The final result of all this is that I can "header" my vimwiki files as:
%title foo
%tag var
and then the tag line is nicely highlighted, it's easy to search by tag from 
within vimwiki (stu.andrews' code), and html exports can place the first (main) 
tag in evidence depending on the template.

Still to do : write a script to go through all files and build a tag index 
(list of tags with direct links to files that contain them, very useful for 
html output, as it will allow tag navigation from within resulting website)

Original comment by dkhorn...@gmail.com on 19 Jun 2012 at 3:40

GoogleCodeExporter commented 9 years ago
I have found this very helpful.  However the search commands (VWT, etc) only 
work for me when I delete the autocommand filetype specification (au FileType 
vimwiki); when I leave them in VWT returns "Not an editor command: VWT".  Might 
someone have an idea how to fix this?

Original comment by reynolds...@gmail.com on 3 Jul 2015 at 12:08