From 81400f9c237814a313fc475c051c4fe969820faf Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 11 Dec 2024 13:13:58 +0800 Subject: [PATCH] feat(zettelkasten): add zk tags tree --- .../vim-zettelkasten/ftplugin/zkbrowser.lua | 8 +++ .../vim-zettelkasten/ftplugin/zktagstree.lua | 38 ++++++++++++++ .../lua/zettelkasten/browser.lua | 51 ++++++++++++++----- lua/spacevim/plugin/statusline.lua | 3 ++ 4 files changed, 86 insertions(+), 14 deletions(-) create mode 100644 bundle/vim-zettelkasten/ftplugin/zktagstree.lua diff --git a/bundle/vim-zettelkasten/ftplugin/zkbrowser.lua b/bundle/vim-zettelkasten/ftplugin/zkbrowser.lua index 225f8deb9..ccb67f1a1 100644 --- a/bundle/vim-zettelkasten/ftplugin/zkbrowser.lua +++ b/bundle/vim-zettelkasten/ftplugin/zkbrowser.lua @@ -65,6 +65,14 @@ 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() + require('zettelkasten.browser').open_tag_tree() + end, + }) end local config = require('zettelkasten.config') diff --git a/bundle/vim-zettelkasten/ftplugin/zktagstree.lua b/bundle/vim-zettelkasten/ftplugin/zktagstree.lua new file mode 100644 index 000000000..2f36b1d11 --- /dev/null +++ b/bundle/vim-zettelkasten/ftplugin/zktagstree.lua @@ -0,0 +1,38 @@ +if vim.b.did_ftp == true then + return +end + +vim.opt_local.cursorline = true +vim.opt_local.modifiable = false +vim.opt_local.buflisted = false +vim.opt_local.number = false +vim.opt_local.relativenumber = false +vim.opt_local.bufhidden = 'wipe' +vim.opt_local.syntax = 'zktagstree' +vim.opt_local.buftype = 'nofile' +vim.opt_local.swapfile = false +vim.api.nvim_buf_set_keymap(0, 'n', '', '', { + noremap = true, + silent = true, + nowait = true, + callback = function() end, +}) +vim.api.nvim_buf_set_keymap(0, 'n', '', '', { + noremap = true, + silent = true, + nowait = true, + callback = function() + local bufnr = vim.fn.bufnr('zk://browser') + if vim.api.nvim_buf_is_valid(bufnr) then + vim.api.nvim_set_option_value('modifiable', true, { buf = bufnr }) + vim.api.nvim_buf_set_lines( + bufnr, + 0, + -1, + false, + require('zettelkasten').get_note_browser_content({ tags = { vim.fn.getline('.') } }) + ) + end + vim.api.nvim_set_option_value('modifiable', false, { buf = bufnr }) + end, +}) diff --git a/bundle/vim-zettelkasten/lua/zettelkasten/browser.lua b/bundle/vim-zettelkasten/lua/zettelkasten/browser.lua index 85f4e59e3..1ffd6b4e2 100644 --- a/bundle/vim-zettelkasten/lua/zettelkasten/browser.lua +++ b/bundle/vim-zettelkasten/lua/zettelkasten/browser.lua @@ -17,7 +17,6 @@ local ZK_FILE_NAME_PATTERN = '%d+-%d+-%d+-%d+-%d+-%d+.md' local s_note_cache_with_file_path = {} local s_note_cache_with_id = {} - -- list all zettelkasten notes in specific folder local function get_files(folder) local files = fn.split(fn.globpath(folder, '*.md'), '\\n') @@ -225,21 +224,45 @@ 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 + 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 +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 +function M.open_tag_tree() + vim.cmd('30vsplit zk://tags_tree') + vim.opt_local.filetype = 'zktagstree' + vim.opt_local.modifiable = true + local lines = {} + local result = M.get_tags() + + for _, tag in ipairs(result) do + table.insert(lines, tag.name) + end + vim.api.nvim_buf_set_lines(0, 0, -1, false, unique_string_table(lines)) + vim.opt_local.buflisted = false + vim.opt_local.modifiable = false +end return M diff --git a/lua/spacevim/plugin/statusline.lua b/lua/spacevim/plugin/statusline.lua index c88600844..a56171216 100644 --- a/lua/spacevim/plugin/statusline.lua +++ b/lua/spacevim/plugin/statusline.lua @@ -585,6 +585,9 @@ local special_statusline = { zkbrowser = function() return simple_name('Zettelkasten Browser') end, + zktagstree = function() + return simple_name('ZkTags Tree') + end, ['vader-result'] = function() return simple_name('Vader result') end,