From 716f835da833e055e9bef473366b22245ffcad6c Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 23 Dec 2024 23:14:50 +0800 Subject: [PATCH] feat(zettelkasten): filter zk title via telescope --- autoload/SpaceVim/layers/zettelkasten.vim | 3 +- bundle/vim-zettelkasten/README.md | 1 + .../telescope/_extensions/zettelkasten.lua | 87 +++++++++++++++++++ .../vim-zettelkasten/plugin/zettelkasten.lua | 1 + config/plugins/telescope.nvim-0.1.5.vim | 1 + docs/layers/zettelkasten.md | 13 +-- 6 files changed, 99 insertions(+), 7 deletions(-) create mode 100644 bundle/vim-zettelkasten/lua/telescope/_extensions/zettelkasten.lua diff --git a/autoload/SpaceVim/layers/zettelkasten.vim b/autoload/SpaceVim/layers/zettelkasten.vim index 8ea71d84c..71f2ac533 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', 'ZkListTags'], 'loadconf' : 1, + \ 'merged' : 0, 'on_cmd' : ['ZkNew', 'ZkBrowse', 'ZkListTemplete', 'ZkListTags', 'ZkListNotes'], 'loadconf' : 1, \ }]) return plugins endfunction @@ -32,6 +32,7 @@ function! SpaceVim#layers#zettelkasten#config() abort 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) + call SpaceVim#mapping#space#def('nnoremap', ['m', 'z', 'f'], 'ZkListNotes', 'fuzzy-find-zettels', 1) endfunction function! SpaceVim#layers#zettelkasten#set_variable(var) abort diff --git a/bundle/vim-zettelkasten/README.md b/bundle/vim-zettelkasten/README.md index 5728b1fcf..e4ef0e8bb 100644 --- a/bundle/vim-zettelkasten/README.md +++ b/bundle/vim-zettelkasten/README.md @@ -43,6 +43,7 @@ let g:zettelkasten_template_directory = 'D:\me\zettelkasten_template' | `:ZkBrowse` | list note in browser window | | `:ZkListTags` | filter tags in telescope | | `:ZkListTemplete` | filte note templates in telescope | +| `:ZkListNotes` | filte note title in telescope | **Key bindings in browser window:** diff --git a/bundle/vim-zettelkasten/lua/telescope/_extensions/zettelkasten.lua b/bundle/vim-zettelkasten/lua/telescope/_extensions/zettelkasten.lua new file mode 100644 index 000000000..889e3f4db --- /dev/null +++ b/bundle/vim-zettelkasten/lua/telescope/_extensions/zettelkasten.lua @@ -0,0 +1,87 @@ +local action_state = require('telescope.actions.state') +local actions = require('telescope.actions') +local conf = require('telescope.config').values +local entry_display = require('telescope.pickers.entry_display') +local finders = require('telescope.finders') +local pickers = require('telescope.pickers') +local browser = require('zettelkasten.browser') +local file = require('spacevim.api.file') + +local function get_all_zettelkasten_notes() + local p = {} + local notes = browser.get_notes() + for _, k in pairs(notes) do + table.insert(p, k) + end + return p +end + +local function concat_tags(t) + + local tags = {} + for _, tag in ipairs(t) do + if vim.tbl_contains(tags, tag.name) == false then + table.insert(tags, tag.name) + end + end + + return table.concat(tags, ' ') +end + +local function show_changes(opts) + opts = opts or {} + local displayer = entry_display.create({ + separator = ' ', + items = { + { width = 23 }, + { width = vim.o.columns - 100 }, + { remaining = true }, + }, + }) + local function make_display(entry) + -- print(vim.inspect(entry)) + return displayer({ + { vim.fn.fnamemodify(entry.value.file_name, ':t'), 'String' }, + { entry.value.title, 'Normal' }, + { + concat_tags(entry.value.tags), + 'Tag', + }, + }) + end + pickers + .new(opts, { + prompt_title = 'ZettelKasten Notes', + finder = finders.new_table({ + results = get_all_zettelkasten_notes(), + entry_maker = function(entry) + return { + value = entry, + display = make_display, + ordinal = entry.title, + } + end, + }), + 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('e ' .. entry.value.file_name) + end) + return true + end, + }) + :find() +end + +local function run() + show_changes() +end + +return require('telescope').register_extension({ + exports = { + -- Default when to argument is given, i.e. :Telescope project + zettelkasten = run, + }, +}) diff --git a/bundle/vim-zettelkasten/plugin/zettelkasten.lua b/bundle/vim-zettelkasten/plugin/zettelkasten.lua index 06e513ca7..860082ec3 100644 --- a/bundle/vim-zettelkasten/plugin/zettelkasten.lua +++ b/bundle/vim-zettelkasten/plugin/zettelkasten.lua @@ -9,6 +9,7 @@ vim.cmd([[command ZkNew :lua require('zettelkasten').zknew({})]]) vim.cmd([[command ZkListTemplete :Telescope zettelkasten_template]]) vim.cmd([[command ZkListTags :Telescope zettelkasten_tags]]) +vim.cmd([[command ZkListNotes :Telescope zettelkasten]]) vim.api.nvim_create_user_command('ZkBrowse', function(opt) require('zettelkasten.browser').browse(opt.fargs) diff --git a/config/plugins/telescope.nvim-0.1.5.vim b/config/plugins/telescope.nvim-0.1.5.vim index 8e602598c..e6dfc53ce 100644 --- a/config/plugins/telescope.nvim-0.1.5.vim +++ b/config/plugins/telescope.nvim-0.1.5.vim @@ -8,6 +8,7 @@ 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') + lua require('telescope').load_extension('zettelkasten') endif if SpaceVim#layers#isLoaded('tools') lua require('telescope').load_extension('bookmarks') diff --git a/docs/layers/zettelkasten.md b/docs/layers/zettelkasten.md index fc818fd23..c04ec8f49 100644 --- a/docs/layers/zettelkasten.md +++ b/docs/layers/zettelkasten.md @@ -39,12 +39,13 @@ update your custom configuration file with: ## Key bindings -| Key bindings | description | -| ------------ | -------------------------------------- | -| `SPC m z n` | create new note | -| `SPC m z t` | create new note with template | -| `SPC m z b` | open zettelkasten browse | -| `SPC m z g` | filter zettelkasten tags via telescope | +| Key bindings | description | +| ------------ | --------------------------------------- | +| `SPC m z n` | create new note | +| `SPC m z t` | create new note with template | +| `SPC m z b` | open zettelkasten browse | +| `SPC m z g` | filter zettelkasten tags via telescope | +| `SPC m z f` | filter zettelkasten title via telescope | In the zettelkasten browse buffer: