summary refs log tree commit diff
path: root/.vim/lua/internal/zk.lua
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2025-04-30 13:14:22 +0200
committerRobert Günzler <r@gnzler.io>2025-04-30 13:14:22 +0200
commit6a1810f4dfe4ac22dfbaf2b98fb7618fd76eb761 (patch)
treed9cc1046e3305476e2eea284fbdafde049cf3e10 /.vim/lua/internal/zk.lua
parent59bdb9bc3f7b06b886a24def842f62f350f1411b (diff)
vim: misc updates
Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to '.vim/lua/internal/zk.lua')
-rw-r--r--.vim/lua/internal/zk.lua73
1 files changed, 73 insertions, 0 deletions
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")<cr>', opts)
+
+  -- follow links
+  -- TODO: should this also work on outgoing links?
+  vim.keymap.set('n', '<cr>', '<cmd>lua vim.lsp.buf.definition()<cr>', 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,
+})