From 14735733443f1f5406c6c2c1a480bcd8c985a8e3 Mon Sep 17 00:00:00 2001 From: saccarosium Date: Sat, 18 Mar 2023 19:44:31 +0100 Subject: [PATCH 1/5] feat: adding support for neovim vim.ui.select --- autoload/wiki/buffer.vim | 1 + lua/wiki/init.lua | 50 ++++++++++++++++++++++++++++++++++++++++ plugin/wiki.vim | 4 ++++ 3 files changed, 55 insertions(+) create mode 100644 lua/wiki/init.lua diff --git a/autoload/wiki/buffer.vim b/autoload/wiki/buffer.vim index 05c372d..90f2797 100644 --- a/autoload/wiki/buffer.vim +++ b/autoload/wiki/buffer.vim @@ -74,6 +74,7 @@ function! s:init_buffer_commands() abort " {{{1 command! -buffer -nargs=+ -complete=custom,wiki#tags#get_tag_names \ WikiTagRename call wiki#tags#rename_ask() + command! -buffer WikiToc call luaeval("require('wiki').toc()") command! -buffer WikiFzfToc call wiki#fzf#toc() command! -buffer -nargs=1 WikiClearCache call wiki#cache#clear() diff --git a/lua/wiki/init.lua b/lua/wiki/init.lua new file mode 100644 index 0000000..6037f44 --- /dev/null +++ b/lua/wiki/init.lua @@ -0,0 +1,50 @@ +local M = {} + +function M.get_pages() + local res = {} + local pages = vim.fn["wiki#page#get_all"]() + for _, p in pairs(pages) do + table.insert(res, p[1]) + end + vim.ui.select(res, + { prompt = "WikiPages> " }, + function(f) + vim.cmd("edit " .. f) + end) +end + +function M.get_tags() + local res = {} + local tags = vim.fn["wiki#tags#get_all"]() + for key, val in pairs(tags) do + for _, file in pairs(val) do + local str = string.format("%s: %s:%s", key, file[1], file[2]) + table.insert(res, str) + end + end + vim.ui.select(res, + { prompt = "WikiTags> " }, + function(t) + t = vim.split(t, ':')[2] + vim.cmd("edit " .. t) + end) + return res +end + +function M.toc() + local res = {} + local toc = vim.fn["wiki#toc#gather_entries"]() + for _, hd in pairs(toc) do + local indent = vim.fn["repeat"]('.', hd.level - 1) + local line = hd.lnum .. ': ' .. indent .. hd.header + table.insert(res, line) + end + vim.ui.select(res, + { prompt = "WikiToc> ", }, + function(t) + t = vim.split(t, ':')[1] + vim.cmd("execute " .. t) + end) +end + +return M diff --git a/plugin/wiki.vim b/plugin/wiki.vim index d3c8d8f..5e049dc 100644 --- a/plugin/wiki.vim +++ b/plugin/wiki.vim @@ -92,6 +92,8 @@ command! WikiReload call wiki#reload() command! WikiJournal call wiki#journal#open() command! WikiFzfPages call wiki#fzf#pages() command! WikiFzfTags call wiki#fzf#tags() +command! WikiPages call luaeval("require('wiki').get_pages()") +command! WikiTags call luaeval("require('wiki').get_tags()") " Initialize mappings nnoremap (wiki-index) :WikiIndex @@ -100,6 +102,8 @@ nnoremap (wiki-journal) :WikiJournal nnoremap (wiki-reload) :WikiReload nnoremap (wiki-fzf-pages) :WikiFzfPages nnoremap (wiki-fzf-tags) :WikiFzfTags +nnoremap (wiki-pages) :WikiPages +nnoremap (wiki-tags) :WikiTags " Apply default mappings let s:mappings = index(['all', 'global'], g:wiki_mappings_use_defaults) >= 0 From 38fb4b8abb1c13716e5cdad88454941a5b163c46 Mon Sep 17 00:00:00 2001 From: saccarosium Date: Sat, 18 Mar 2023 21:27:13 +0100 Subject: [PATCH 2/5] feat!: unifying search commands with Wiki{Pages,Tags,Toc} --- autoload/wiki/buffer.vim | 19 +++++++++++-------- doc/wiki.txt | 36 ++++++++++++++++++------------------ plugin/wiki.vim | 13 +++++++------ 3 files changed, 36 insertions(+), 32 deletions(-) diff --git a/autoload/wiki/buffer.vim b/autoload/wiki/buffer.vim index 90f2797..38de5c4 100644 --- a/autoload/wiki/buffer.vim +++ b/autoload/wiki/buffer.vim @@ -63,8 +63,8 @@ function! s:init_buffer_commands() abort " {{{1 command! -buffer WikiPageDelete call wiki#page#delete() command! -buffer WikiPageRename call wiki#page#rename() command! -buffer WikiPageRenameSection call wiki#page#rename_section() - command! -buffer WikiPageToc call wiki#toc#create(0) - command! -buffer WikiPageTocLocal call wiki#toc#create(1) + command! -buffer WikiGenerateToc call wiki#toc#create(0) + command! -buffer WikiGenerateToc call wiki#toc#create(1) command! -buffer -range=% -nargs=* WikiExport \ call wiki#page#export(, , ) @@ -74,8 +74,11 @@ function! s:init_buffer_commands() abort " {{{1 command! -buffer -nargs=+ -complete=custom,wiki#tags#get_tag_names \ WikiTagRename call wiki#tags#rename_ask() - command! -buffer WikiToc call luaeval("require('wiki').toc()") - command! -buffer WikiFzfToc call wiki#fzf#toc() + if has('nvim') + command! -buffer WikiToc call luaeval("require('wiki').toc()") + else + command! -buffer WikiToc call wiki#fzf#toc() + endif command! -buffer -nargs=1 WikiClearCache call wiki#cache#clear() if b:wiki.in_journal @@ -118,8 +121,8 @@ function! s:init_buffer_mappings() abort " {{{1 nnoremap (wiki-tag-search) :WikiTagSearch nnoremap (wiki-tag-rename) :WikiTagRename - nnoremap (wiki-fzf-toc) :WikiFzfToc - inoremap (wiki-fzf-toc) :WikiFzfToc + nnoremap (wiki-toc) :WikiToc + inoremap (wiki-toc) :WikiToc xnoremap (wiki-link-toggle-visual) :call wiki#link#toggle_visual() nnoremap (wiki-link-toggle-operator) :set opfunc=wiki#link#toggle_operatorg@ @@ -165,8 +168,8 @@ function! s:init_buffer_mappings() abort " {{{1 \ '(wiki-page-delete)': 'wd', \ '(wiki-page-rename)': 'wr', \ '(wiki-page-rename-section)': '', - \ '(wiki-page-toc)': 'wt', - \ '(wiki-page-toc-local)': 'wT', + \ '(wiki-generate-toc)': 'wt', + \ '(wiki-genrate-toc-local)': 'wT', \ '(wiki-export)': 'wp', \ 'x_(wiki-export)': 'wp', \ '(wiki-tag-list)': 'wsl', diff --git a/doc/wiki.txt b/doc/wiki.txt index 16cb8a5..ff45796 100644 --- a/doc/wiki.txt +++ b/doc/wiki.txt @@ -158,7 +158,7 @@ FEATURES *wiki-intro-features* * Utility functionality * |WikiExport| command for exporting to e.g. `pdf` with `pandoc` * Third-party support - * |fzf|: |WikiFzfPages|, |WikiFzfToc|, and |WikiFzfTags| commands + * |fzf|: |WikiPages|, |WikiToc|, and |WikiTags| commands (https://github.com/junegunn/fzf.vim) ------------------------------------------------------------------------------ @@ -184,7 +184,7 @@ Executive summary ~ * Speed is key - make sure you have a fast workflow: * Use a keyboard shortcut in your desktop/window manager to open Vim/neovim with the wiki index (e.g. ). - * Use something like |:WikiFzfPages| and `ripgrep` (with e.g. + * Use something like |:WikiPages| and `ripgrep` (with e.g. `ctrlsf.vim` [2]) for searching. * Write short pages and use links between related pages. * Don't obsess about the wiki structure. @@ -211,7 +211,7 @@ previously also used Dropbox. Needless to say, it is important to be able to find relevant notes fast. I have a global Vim mapping `ow` to open a page search interface. -I previously used |:WikiFzfPages|, which works very well! Today, though, I use +I previously used |:WikiPages|, which works very well! Today, though, I use Telescope [4] with a personal function to list wiki files [5]. It usually takes no more than a couple of seconds from the moment I think of something until I have opened the relevant note. A key here is to use simple names for @@ -523,7 +523,7 @@ OPTIONS *wiki-config-options* endfunction *g:wiki_fzf_pages_force_create_key* - Key combination that, when pressed while searching with |WikiFzfPages|, will + Key combination that, when pressed while searching with |WikiPages|, will create a new page with the name of the query. The value must be a string recognized by fzf's `--expect` argument; see fzf's manual page for a list of available keys. @@ -531,7 +531,7 @@ OPTIONS *wiki-config-options* Default: `'alt-enter'` *g:wiki_fzf_pages_opts* - A string with additional user options for |WikiFzfPages|. This can be used + A string with additional user options for |WikiPages|. This can be used e.g. to add a previewer. Users should be aware that the page candidates are "prettified" with the `--with-nth=1` and `-d` options for fzf, so to obtain the page path in a previewer option one must use the field index expression `1`. @@ -542,7 +542,7 @@ OPTIONS *wiki-config-options* Default: `''` *g:wiki_fzf_tags_opts* - A string with additional user options for |WikiFzfTags|. This can be used + A string with additional user options for |WikiTags|. This can be used e.g. to add a previewer. Users should be aware that the page candidates are "prettified" with the `-d` option for fzf, so to obtain the page path in a previewer option one must use the field index expression `2..`. @@ -1318,12 +1318,12 @@ the commands are also available as mappings of the form `(wiki-[name])`. *WikiPageRename* Rename wiki page (will update all links to the page). -*(wiki-page-toc)* -*WikiPageToc* +*(wiki-generate-toc)* +*WikiGenerateToc* Create/Update table of contents. -*(wiki-page-toc-local)* -*WikiPageTocLocal* +*(wiki-generate-toc-local)* +*WikiGenerateTocLocal* Create/Update table of contents (section local variant). *(wiki-journal-index)* @@ -1419,7 +1419,7 @@ the commands are also available as mappings of the form `(wiki-[name])`. *WikiTagSearch* [options] [tag] List wiki pages that contain the desired `tag`. If the argument `tag` is not supplied, the command asks for input. See also |wiki-tags| for more - information on the tags feature and |WikiFzfTags| for an alternative search + information on the tags feature and |WikiTags| for an alternative search mechanism. Available options are (use |g:wiki_tag_search| to set default values): @@ -1438,8 +1438,8 @@ the commands are also available as mappings of the form `(wiki-[name])`. supplied, the command asks for input. This command uses the `make` function of |g:wiki_tag_parsers|. -*(wiki-fzf-pages)* -*WikiFzfPages* +*(wiki-pages)* +*WikiPages* Open |fzf| in find file mode for wiki files in current wiki or in the main wiki defined by |g:wiki_root|. @@ -1465,16 +1465,16 @@ the commands are also available as mappings of the form `(wiki-[name])`. Page", and this term does not exist, then it will instead open a new page "My Fzf Page.wiki" (or similar, depending on the value of other options). -*(wiki-fzf-tags)* -*WikiFzfTags* +*(wiki-tags)* +*WikiTags* Open |fzf| and search for tags. One may pass additional options to fzf with the |g:wiki_fzf_tags_opts| option. This allows more fine grained control of fzf, e.g. to add a previewer. -*(wiki-fzf-toc)* -*WikiFzfToc* +*(wiki-toc)* +*WikiToc* Open |fzf| and list table of contents entries for the current wiki page. ============================================================================== @@ -1847,7 +1847,7 @@ Related commands: - |WikiTagReload| - |WikiTagSearch| - |WikiTagRename| -- |WikiFzfTags| +- |WikiTags| Related settings: - |g:wiki_tag_list| diff --git a/plugin/wiki.vim b/plugin/wiki.vim index 5e049dc..294aa3b 100644 --- a/plugin/wiki.vim +++ b/plugin/wiki.vim @@ -90,18 +90,19 @@ command! WikiIndex call wiki#goto_index() command! WikiOpen call wiki#page#open() command! WikiReload call wiki#reload() command! WikiJournal call wiki#journal#open() -command! WikiFzfPages call wiki#fzf#pages() -command! WikiFzfTags call wiki#fzf#tags() -command! WikiPages call luaeval("require('wiki').get_pages()") -command! WikiTags call luaeval("require('wiki').get_tags()") +if has('nvim') + command! WikiPages call luaeval("require('wiki').get_pages()") + command! WikiTags call luaeval("require('wiki').get_tags()") +else + command! WikiPages call wiki#fzf#pages() + command! WikiTags call wiki#fzf#tags() +endif " Initialize mappings nnoremap (wiki-index) :WikiIndex nnoremap (wiki-open) :WikiOpen nnoremap (wiki-journal) :WikiJournal nnoremap (wiki-reload) :WikiReload -nnoremap (wiki-fzf-pages) :WikiFzfPages -nnoremap (wiki-fzf-tags) :WikiFzfTags nnoremap (wiki-pages) :WikiPages nnoremap (wiki-tags) :WikiTags From 8a05bd4c68f076ba9e666ecfee9a0e5758680b8c Mon Sep 17 00:00:00 2001 From: saccarosium Date: Wed, 22 Mar 2023 11:38:53 +0100 Subject: [PATCH 3/5] chore: polishing naming and spacing --- autoload/wiki/buffer.vim | 12 ++++++------ doc/wiki.txt | 8 ++++---- plugin/wiki.vim | 8 ++++---- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/autoload/wiki/buffer.vim b/autoload/wiki/buffer.vim index 38de5c4..17cd73b 100644 --- a/autoload/wiki/buffer.vim +++ b/autoload/wiki/buffer.vim @@ -63,8 +63,8 @@ function! s:init_buffer_commands() abort " {{{1 command! -buffer WikiPageDelete call wiki#page#delete() command! -buffer WikiPageRename call wiki#page#rename() command! -buffer WikiPageRenameSection call wiki#page#rename_section() - command! -buffer WikiGenerateToc call wiki#toc#create(0) - command! -buffer WikiGenerateToc call wiki#toc#create(1) + command! -buffer WikiTocGenerate call wiki#toc#create(0) + command! -buffer WikiTocGenerateLocal call wiki#toc#create(1) command! -buffer -range=% -nargs=* WikiExport \ call wiki#page#export(, , ) @@ -75,9 +75,9 @@ function! s:init_buffer_commands() abort " {{{1 \ WikiTagRename call wiki#tags#rename_ask() if has('nvim') - command! -buffer WikiToc call luaeval("require('wiki').toc()") + command! -buffer WikiToc lua require('wiki').toc() else - command! -buffer WikiToc call wiki#fzf#toc() + command! -buffer WikiToc call wiki#fzf#toc() endif command! -buffer -nargs=1 WikiClearCache call wiki#cache#clear() @@ -168,8 +168,8 @@ function! s:init_buffer_mappings() abort " {{{1 \ '(wiki-page-delete)': 'wd', \ '(wiki-page-rename)': 'wr', \ '(wiki-page-rename-section)': '', - \ '(wiki-generate-toc)': 'wt', - \ '(wiki-genrate-toc-local)': 'wT', + \ '(wiki-toc-generate)': 'wt', + \ '(wiki-toc-generate-local)': 'wT', \ '(wiki-export)': 'wp', \ 'x_(wiki-export)': 'wp', \ '(wiki-tag-list)': 'wsl', diff --git a/doc/wiki.txt b/doc/wiki.txt index ff45796..ecc13bb 100644 --- a/doc/wiki.txt +++ b/doc/wiki.txt @@ -1318,12 +1318,12 @@ the commands are also available as mappings of the form `(wiki-[name])`. *WikiPageRename* Rename wiki page (will update all links to the page). -*(wiki-generate-toc)* -*WikiGenerateToc* +*(wiki-toc-generate)* +*WikiTocGenerate* Create/Update table of contents. -*(wiki-generate-toc-local)* -*WikiGenerateTocLocal* +*(wiki-toc-generate-local)* +*WikiTocGenerateLocal* Create/Update table of contents (section local variant). *(wiki-journal-index)* diff --git a/plugin/wiki.vim b/plugin/wiki.vim index 294aa3b..3f48fe7 100644 --- a/plugin/wiki.vim +++ b/plugin/wiki.vim @@ -91,11 +91,11 @@ command! WikiOpen call wiki#page#open() command! WikiReload call wiki#reload() command! WikiJournal call wiki#journal#open() if has('nvim') - command! WikiPages call luaeval("require('wiki').get_pages()") - command! WikiTags call luaeval("require('wiki').get_tags()") + command! WikiPages lua require('wiki').get_pages() + command! WikiTags lua require('wiki').get_tags() else - command! WikiPages call wiki#fzf#pages() - command! WikiTags call wiki#fzf#tags() + command! WikiPages call wiki#fzf#pages() + command! WikiTags call wiki#fzf#tags() endif " Initialize mappings From e5fc41b5eebb4d5d704ff61d38d5ed7a2e809985 Mon Sep 17 00:00:00 2001 From: saccarosium Date: Wed, 22 Mar 2023 12:53:39 +0100 Subject: [PATCH 4/5] fix: path relative to g:wiki_path --- lua/wiki/init.lua | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/lua/wiki/init.lua b/lua/wiki/init.lua index 6037f44..aa1adbd 100644 --- a/lua/wiki/init.lua +++ b/lua/wiki/init.lua @@ -1,14 +1,28 @@ local M = {} +-- Returns shorten path +local function filter(path) + local root = vim.g.wiki_root .. "/" + return path:gsub(root, "") +end + +-- Returns full path +local function unfilter(path) + local root = vim.g.wiki_root .. "/" + return root .. path +end + function M.get_pages() local res = {} local pages = vim.fn["wiki#page#get_all"]() for _, p in pairs(pages) do - table.insert(res, p[1]) + local page = filter(p[1]) + table.insert(res, page) end vim.ui.select(res, { prompt = "WikiPages> " }, function(f) + f = unfilter(f) vim.cmd("edit " .. f) end) end @@ -18,14 +32,14 @@ function M.get_tags() local tags = vim.fn["wiki#tags#get_all"]() for key, val in pairs(tags) do for _, file in pairs(val) do - local str = string.format("%s: %s:%s", key, file[1], file[2]) + local str = string.format("%s:%s:%s", key, file[2], filter(file[1])) table.insert(res, str) end end vim.ui.select(res, { prompt = "WikiTags> " }, function(t) - t = vim.split(t, ':')[2] + t = unfilter(vim.split(t, ':')[3]) vim.cmd("edit " .. t) end) return res @@ -36,13 +50,13 @@ function M.toc() local toc = vim.fn["wiki#toc#gather_entries"]() for _, hd in pairs(toc) do local indent = vim.fn["repeat"]('.', hd.level - 1) - local line = hd.lnum .. ': ' .. indent .. hd.header + local line = hd.lnum .. '|' .. indent .. hd.header table.insert(res, line) end vim.ui.select(res, { prompt = "WikiToc> ", }, function(t) - t = vim.split(t, ':')[1] + t = vim.split(t, '|')[1] vim.cmd("execute " .. t) end) end From 087d60fe0a55d43842bedc8ddcc8d4d7c8d2f726 Mon Sep 17 00:00:00 2001 From: saccarosium Date: Wed, 22 Mar 2023 13:09:17 +0100 Subject: [PATCH 5/5] feat: add configuration option g:wiki_selet_method --- plugin/wiki.vim | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/plugin/wiki.vim b/plugin/wiki.vim index 3f48fe7..f6a33f5 100644 --- a/plugin/wiki.vim +++ b/plugin/wiki.vim @@ -65,6 +65,11 @@ call wiki#init#option('wiki_month_names', [ \]) call wiki#init#option('wiki_resolver', 'wiki#url#wiki#resolver') call wiki#init#option('wiki_root', '') +if has('nvim') + call wiki#init#option('wiki_select_method', 'ui_select') +else + call wiki#init#option('wiki_select_method', 'fzf') +endif call wiki#init#option('wiki_tag_list', { 'output' : 'loclist' }) call wiki#init#option('wiki_tag_search', { 'output' : 'loclist' }) call wiki#init#option('wiki_tag_parsers', [g:wiki#tags#default_parser]) @@ -90,7 +95,7 @@ command! WikiIndex call wiki#goto_index() command! WikiOpen call wiki#page#open() command! WikiReload call wiki#reload() command! WikiJournal call wiki#journal#open() -if has('nvim') +if has('nvim') && g:wiki_select_method == 'ui_select' command! WikiPages lua require('wiki').get_pages() command! WikiTags lua require('wiki').get_tags() else