From 6a1810f4dfe4ac22dfbaf2b98fb7618fd76eb761 Mon Sep 17 00:00:00 2001 From: Robert Günzler Date: Wed, 30 Apr 2025 13:14:22 +0200 Subject: vim: misc updates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Robert Günzler --- .vim/lua/internal/zk.lua | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 .vim/lua/internal/zk.lua (limited to '.vim/lua/internal/zk.lua') diff --git a/.vim/lua/internal/zk.lua b/.vim/lua/internal/zk.lua new file mode 100644 index 0000000..4631e13 --- /dev/null +++ b/.vim/lua/internal/zk.lua @@ -0,0 +1,73 @@ +if not vim.env.ZK_NOTEBOOK_DIR then + vim.notify('Missing ZK_NOTEBOOK_DIR!', vim.log.levels.WARN) + return +end + +local api = require('zk.api') +local util = require('zk.util') + +local function insert_new_note() + -- TODO: catch error an delete note + api.new(nil, nil, function(err1, res1) + if not res1 then + error(err1) + end + + -- update index to include the new note + api.index(nil, nil, nil) + + local path = res1.path + local filename = vim.fs.basename(path) + local location = util.get_lsp_location_from_caret() + + api.link(filename, location, nil, {}, function(err2, res2) + if not res2 then + error(err2) + end + + -- open split + local buf = vim.api.nvim_create_buf(true, false) + vim.api.nvim_buf_set_name(buf, filename) + vim.api.nvim_buf_call(buf, function() + vim.cmd.edit(path) + end) + local win = vim.api.nvim_open_win(buf, false, { + split = 'below', + win = 0, + }) + -- move to split + vim.api.nvim_set_current_win(win) + vim.api.nvim_win_set_cursor(win, { -1, 0 }) + end) + end) +end + +local function on_attach() + if not vim.env.ZK_NOTEBOOK_DIR then + vim.notify('Missing ZK_NOTEBOOK_DIR!', vim.log.levels.WARN) + return + end + + -- enable virtual_text diagnostics to show link titles + vim.diagnostic.config({ virtual_text = true }) + + local opts = { remap = true, silent = false, buffer = true } + + -- -- list zettelkasten + -- vim.keymap.set('n', '-', ':call feedkeys(";zz")', opts) + + -- follow links + -- TODO: should this also work on outgoing links? + vim.keymap.set('n', '', 'lua vim.lsp.buf.definition()', opts) + + -- insert ref to new note and open note in split + vim.keymap.set({ 'n', 'i' }, ';zi', insert_new_note, opts) +end + +-- setup binds to run when entering zettel buffers +vim.api.nvim_create_autocmd({ 'BufEnter' }, { + pattern = vim.env.ZK_NOTEBOOK_DIR .. '/*.md', + callback = on_attach, + group = vim.api.nvim_create_augroup('zk-on-attach', { clear = true }), + once = true, +}) -- cgit 1.4.1