From 4b61259a804fac7744ed1139f5c903ad432e4dce Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 10 Dec 2024 22:45:40 +0800 Subject: [PATCH] feat(zettelkasten): filter zk tags --- autoload/SpaceVim/layers/zettelkasten.vim | 3 +- .../vim-zettelkasten/ftplugin/zkbrowser.lua | 22 +++---- .../_extensions/zettelkasten_tags.lua | 60 +++++++++++++++++++ bundle/vim-zettelkasten/lua/zettelkasten.lua | 35 ++++++++--- .../lua/zettelkasten/browser.lua | 17 ++++++ .../vim-zettelkasten/plugin/zettelkasten.lua | 28 ++++----- config/plugins/telescope.nvim-0.1.5.vim | 1 + 7 files changed, 126 insertions(+), 40 deletions(-) create mode 100644 bundle/vim-zettelkasten/lua/telescope/_extensions/zettelkasten_tags.lua diff --git a/autoload/SpaceVim/layers/zettelkasten.vim b/autoload/SpaceVim/layers/zettelkasten.vim index 106aa30c1..8ea71d84c 100644 --- a/autoload/SpaceVim/layers/zettelkasten.vim +++ b/autoload/SpaceVim/layers/zettelkasten.vim @@ -10,7 +10,7 @@ function! SpaceVim#layers#zettelkasten#plugins() abort let plugins = [] call add(plugins, [g:_spacevim_root_dir . 'bundle/vim-zettelkasten', \ { - \ 'merged' : 0, 'on_cmd' : ['ZkNew', 'ZkBrowse', 'ZkListTemplete'], 'loadconf' : 1, + \ 'merged' : 0, 'on_cmd' : ['ZkNew', 'ZkBrowse', 'ZkListTemplete', 'ZkListTags'], 'loadconf' : 1, \ }]) return plugins endfunction @@ -30,6 +30,7 @@ function! SpaceVim#layers#zettelkasten#config() abort let g:_spacevim_mappings_space.m.z = {'name' : '+zettelkasten'} call SpaceVim#mapping#space#def('nnoremap', ['m', 'z', 'n'], 'ZkNew', 'create-new-zettel-note', 1) call SpaceVim#mapping#space#def('nnoremap', ['m', 'z', 't'], 'ZkListTemplete', 'zettel-template', 1) + call SpaceVim#mapping#space#def('nnoremap', ['m', 'z', 'g'], 'ZkListTags', 'zettel-tags', 1) call SpaceVim#mapping#space#def('nnoremap', ['m', 'z', 'b'], 'ZkBrowse', 'open-zettelkasten-browse', 1) endfunction diff --git a/bundle/vim-zettelkasten/ftplugin/zkbrowser.lua b/bundle/vim-zettelkasten/ftplugin/zkbrowser.lua index 11ef5e8ca..cc0eb7f82 100644 --- a/bundle/vim-zettelkasten/ftplugin/zkbrowser.lua +++ b/bundle/vim-zettelkasten/ftplugin/zkbrowser.lua @@ -8,7 +8,6 @@ vim.opt_local.buflisted = false vim.opt_local.syntax = 'zkbrowser' vim.opt_local.buftype = 'nofile' vim.opt_local.swapfile = false -vim.opt_local.buflisted = false vim.opt_local.iskeyword:append(':') vim.opt_local.iskeyword:append('-') vim.opt_local.suffixesadd:append('.md') @@ -46,22 +45,17 @@ if vim.fn.mapcheck('[I', 'n') == '' then end end, }) + vim.api.nvim_buf_set_keymap(0, 'n', '', '', { + noremap = true, + silent = true, + nowait = true, + callback = function() + vim.cmd('ZkBrowse') + end, + }) end local config = require('zettelkasten.config') if config.zettel_dir ~= '' then vim.cmd('lcd ' .. config.zettel_dir) end - -vim.api.nvim_create_autocmd({ 'BufEnter' }, { - group = vim.api.nvim_create_augroup('zettelkasten_browser_events', { clear = true }), - buffer = vim.api.nvim_get_current_buf(), - callback = function(opts) - vim.opt_local.syntax = '' - vim.opt_local.modifiable = true - vim.api.nvim_buf_set_lines(0, 0, -1, false, require('zettelkasten').get_note_browser_content()) - vim.opt_local.syntax = 'zkbrowser' - vim.opt_local.buflisted = false - vim.opt_local.modifiable = false - end, -}) diff --git a/bundle/vim-zettelkasten/lua/telescope/_extensions/zettelkasten_tags.lua b/bundle/vim-zettelkasten/lua/telescope/_extensions/zettelkasten_tags.lua new file mode 100644 index 000000000..e997152cc --- /dev/null +++ b/bundle/vim-zettelkasten/lua/telescope/_extensions/zettelkasten_tags.lua @@ -0,0 +1,60 @@ +local action_state = require("telescope.actions.state") +local actions = require("telescope.actions") +local conf = require("telescope.config").values +local finders = require("telescope.finders") +local pickers = require("telescope.pickers") +local browser = require('zettelkasten.browser') + +local function unique_string_table(t) + local temp = {} + for _, k in ipairs(t) do + temp[k] = true + end + local rst = {} + for m, _ in pairs(temp) do + table.insert(rst, m) + end + return rst +end + +local function prepare_output_table() + local lines = {} + local result = browser.get_tags() + + for _, tag in ipairs(result) do + table.insert(lines, tag.name) + end + return unique_string_table(lines) +end + +local function show_script_names(opts) + opts = opts or {} + pickers.new(opts, { + prompt_title = "ZettelKasten Tags", + finder = finders.new_table { + results = prepare_output_table() + }, + sorter = conf.generic_sorter(opts), + attach_mappings = function(prompt_bufnr) + actions.select_default:replace(function() + local entry = action_state.get_selected_entry() + actions.close(prompt_bufnr) + vim.cmd('ZkBrowse --tags ' .. entry.value) + vim.cmd('stopinsert') + end) + return true + end, + }):find() +end + +local function run() + show_script_names() +end + +return require("telescope").register_extension({ + exports = { + -- Default when to argument is given, i.e. :Telescope scriptnames + zettelkasten_tags = run, + }, +}) + diff --git a/bundle/vim-zettelkasten/lua/zettelkasten.lua b/bundle/vim-zettelkasten/lua/zettelkasten.lua index aa93aa022..9038e5e74 100644 --- a/bundle/vim-zettelkasten/lua/zettelkasten.lua +++ b/bundle/vim-zettelkasten/lua/zettelkasten.lua @@ -257,23 +257,40 @@ function M.get_toc(note_id, format) return formatter.format(lines, format) end -function M.get_note_browser_content() +function M.get_note_browser_content(opt) if config.zettel_dir == '' then log.notify("'notes_path' option is required for note browsing.", log_levels.WARN, {}) return {} end + local filter_tags = {} + for _, tag in ipairs(opt.tags) do + filter_tags[tag] = true + end local all_notes = browser.get_notes() local lines = {} for _, note in ipairs(all_notes) do - table.insert(lines, { - file_name = note.file_name, - id = note.id, - references = note.references, - back_references = note.back_references, - tags = note.tags, - title = note.title, - }) + local has_tag + if #opt.tags == 0 then + has_tag = true + else + for _, tag in ipairs(note.tags) do + if filter_tags[tag.name] then + has_tag = true + break + end + end + end + if has_tag then + table.insert(lines, { + file_name = note.file_name, + id = note.id, + references = note.references, + back_references = note.back_references, + tags = note.tags, + title = note.title, + }) + end end return formatter.format(lines, config.browseformat) diff --git a/bundle/vim-zettelkasten/lua/zettelkasten/browser.lua b/bundle/vim-zettelkasten/lua/zettelkasten/browser.lua index 1ec7f39f0..85f4e59e3 100644 --- a/bundle/vim-zettelkasten/lua/zettelkasten/browser.lua +++ b/bundle/vim-zettelkasten/lua/zettelkasten/browser.lua @@ -225,4 +225,21 @@ function M.get_tags() return tags end + +function M.browse(opt) + vim.cmd('edit zk://browser') + vim.opt_local.syntax = '' + vim.opt_local.modifiable = true + vim.api.nvim_buf_set_lines( + 0, + 0, + -1, + false, + require('zettelkasten').get_note_browser_content({ tags = opt }) + ) + vim.opt_local.syntax = 'zkbrowser' + vim.opt_local.buflisted = false + vim.opt_local.modifiable = false +end + return M diff --git a/bundle/vim-zettelkasten/plugin/zettelkasten.lua b/bundle/vim-zettelkasten/plugin/zettelkasten.lua index 922f61de0..06e513ca7 100644 --- a/bundle/vim-zettelkasten/plugin/zettelkasten.lua +++ b/bundle/vim-zettelkasten/plugin/zettelkasten.lua @@ -5,23 +5,19 @@ -- URL: https://spacevim.org -- License: GPLv3 --============================================================================= -if vim.fn.exists(":ZkNew") == 0 then - vim.cmd([[command ZkNew :lua require('zettelkasten').zknew({})]]) -end +vim.cmd([[command ZkNew :lua require('zettelkasten').zknew({})]]) -if vim.fn.exists(":ZkBrowse") == 0 then - vim.cmd([[command ZkBrowse :lua _G.zettelkasten.zkbrowse()]]) -end - -if vim.fn.exists(":ZkListTemplete") == 0 then - vim.cmd([[command ZkListTemplete :Telescope zettelkasten_template]]) -end +vim.cmd([[command ZkListTemplete :Telescope zettelkasten_template]]) +vim.cmd([[command ZkListTags :Telescope zettelkasten_tags]]) +vim.api.nvim_create_user_command('ZkBrowse', function(opt) + require('zettelkasten.browser').browse(opt.fargs) +end, { nargs = '*' }) _G.zettelkasten = { - tagfunc = require("zettelkasten").tagfunc, - completefunc = require("zettelkasten").completefunc, - zknew = require("zettelkasten").zknew, - zkbrowse = function() - vim.cmd("edit zk://browser") - end, + tagfunc = require('zettelkasten').tagfunc, + completefunc = require('zettelkasten').completefunc, + zknew = require('zettelkasten').zknew, + zkbrowse = function() + vim.cmd('edit zk://browser') + end, } diff --git a/config/plugins/telescope.nvim-0.1.5.vim b/config/plugins/telescope.nvim-0.1.5.vim index 24f8ae540..8e602598c 100644 --- a/config/plugins/telescope.nvim-0.1.5.vim +++ b/config/plugins/telescope.nvim-0.1.5.vim @@ -7,6 +7,7 @@ lua require('telescope').load_extension('task') lua require('telescope').load_extension('neomru') if SpaceVim#layers#isLoaded('zettelkasten') lua require('telescope').load_extension('zettelkasten_template') + lua require('telescope').load_extension('zettelkasten_tags') endif if SpaceVim#layers#isLoaded('tools') lua require('telescope').load_extension('bookmarks')