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, })