zk-org / zk-nvim

Neovim extension for zk
https://github.com/zk-org/zk
GNU General Public License v3.0
539 stars 43 forks source link

Error when using `ZkNew` from within a zk-buffer. #192

Open blastmaster opened 2 months ago

blastmaster commented 2 months ago

Check if applicable

Describe the bug

When executing ZKNew commands from inside an zk-buffer in Neovim I'll get an error. E488: Trailing characters: The command works fine from any other buffer or a fresh neovim session without any file open.

How to reproduce?

  1. Open any Zk note in Neovim
  2. Try to run :ZkNew with some args. No matter if I try to create a new Note using a keymap like that from your Readme: map("n", "<leader>zn", "<Cmd>ZkNew { dir = vim.fn.expand('%:p:h'), title = vim.fn.input('Title: ') }<CR>", opts) or doing things by hand like :ZkNew { dir = "journal", date = "today" }.

I'll get the error: E488: Trailing characters: { dir = "journal", date = "today" }

The same commands and or keymaps work when using them from an non-zk buffer.

zk configuration

# zk configuration file
[note]

template = "default.md"

[extra]

[group.daily]
paths = ["journal/daily"]

[group.daily.note]

filename = "{{format-date now '%Y-%m-%d'}}"
extension = "md"
template = "daily.md"

# MARKDOWN SETTINGS
[format.markdown]

# Enable support for #hashtags.
hashtags = false
# Enable support for :colon:separated:tags:.
colon-tags = true
# Enable support for Bear's #multi-word tags#
# Hashtags must be enabled for multi-word tags to work.
multiword-tags = false

[tool]

editor = "nvim"
fzf-preview = "batcat -p --color always {-1}"

[lsp]

[lsp.diagnostics]
dead-link = "error"

[lsp.completion]

[filter]

[alias]

# Shortcut to a command.
ls = "zk list $@"

# Create a new journal note
daily = 'zk new --no-input "$ZK_NOTEBOOK_DIR/journal/daily"'

conf = '$EDITOR "$ZK_NOTEBOOK_DIR/.zk/config.toml"'

Neovim configuration

-- zk.lua loaded by lazy
return {
  "zk-org/zk-nvim",
  config = function()
    require("zk").setup({
        -- can be "telescope", "fzf", "fzf_lua", "minipick", or "select" (`vim.ui.select`)
        -- it's recommended to use "telescope", "fzf", "fzf_lua", or "minipick"
        picker = "telescope",

        lsp = {
            -- `config` is passed to `vim.lsp.start_client(config)`
            config = {
                cmd = { "zk", "lsp" },
                name = "zk",
                -- on_attach = ...
                -- etc, see `:h vim.lsp.start_client()`
            },

            -- automatically attach buffers in a zk notebook that match the given filetypes
            auto_attach = {
                enabled = true,
                filetypes = { "markdown" },
            },
        },
    })
  end
}

-- top level init.lua
-- define local options for key-mappigns
local opts = { noremap = true, silent = false }

-- Zettelkasten stuff Keymaps
-- create a new note after asking for the new Title.
vim.api.nvim_set_keymap("n", "<leader>zn", "<Cmd>ZkNew { title = vim.fn.input('Title: ') }<CR>", opts)

-- Open notes.
vim.api.nvim_set_keymap("n", "<leader>zo", "<Cmd>ZkNotes { sort = { 'modified' } }<CR>", opts)
-- Open notes associated with the selected tags.
vim.api.nvim_set_keymap("n", "<leader>zt", "<Cmd>ZkTags<CR>", opts)
-- Search for the notes matching a given query.
-- vim.api.nvim_set_keymap("n", "<leader>zf", "<Cmd>ZkNotes { sort = { 'modified' }, match = { vim.fn.input('Search: ') } }<CR>", opts)
vim.api.nvim_set_keymap("n", "<leader>zf", "<Cmd>ZkNotes { sort = { 'modified' } }<CR>", opts)
-- Search for the notes matching the current visual selection.
vim.api.nvim_set_keymap("v", "<leader>zf", ":'<,'>ZkMatch<CR>", opts)

Environment

zk 0.14.1-11-g4a51e39
system: Linux 6.10.9-amd64 x86_64 GNU/Linux
NVIM v0.9.5
Build type: Release
LuaJIT 2.1.1723681758

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/share/nvim"

Run :checkhealth for more info
tjex commented 2 months ago

I just ran ZkNew { dir = "journal", date = "today" } (replacing dir with a directry I have) without issue.

If they're working from a non-zk buffer then ... I'm confused. Would've expected it to at least be the other way around.

I would ask you to fully close all running instances of neovim, navigate to the root dir of your notebook, and try again.

What happens if you just run :ZkNew?

tjex commented 2 months ago

Also, there have been a few changes since your zk version. Maybe rebuild just for the sake of it. Latest is zk 0.14.1-23-gbe5bd76

blastmaster commented 1 month ago

Running ZkNew from an empty buffer works as expected. Just from inside a Zk buffer I got the error. I will try updating zk version.

blastmaster commented 1 month ago

Ok, I've updated the zk to version zk 0.14.1-28-g73092a6 Just running :ZkNew without any args as neovim command works in every kind of buffer. Using the shortcut <leader>zn as above just works in non-zk buffers but not in zk-buffers. To be more concrete, as soon as I pass parameters to the ZKNew I've got the error also :ZkNew { title = "blub" } results in the E488 Trailing characters: { title = "blub" } but works perfectly well when doing from an non zk-buffer.

tjex commented 1 month ago

Have you tried in a completely fresh notebook with a completely fresh terminal session?

It works for me: https://asciinema.org/a/GNLkLwdTYVDWVRiCqNFh4sJjW

If I run the command in a non-zk buffer the note gets created in my main global zk (as I have $ZK_NOTEBOOK_DIR set ).

tjex commented 1 month ago

Hmmm. It's occurred to me now. You're on nvim 0.9. The neovim api for getting the active clients changed between 0.9 and 0.10.

We created a conditional check for that though: https://github.com/zk-org/zk-nvim/pull/167

But maybe it's not playing nice.

ngocdotnt commented 1 month ago

I also encountered the same error as @blastmaster.
My environment: zk 0.14.1-28-g73092a6 NVIM v0.10.2.

I temporarily fixed it by using a Lua function to assign a keymap as follows: vim.keymap.set('n', '<leader>zn', '<cmd>lua require'zk.commands'.get('ZkNew')({title = vim.fn.input('Title: ')})<cr>, {desc='New note'})

Sorry for my poor English.

tjex commented 1 month ago

@ngocdotnt So your command only works when replacing ZkNew with lua require...? and are you on Linux of OSX?

ngocdotnt commented 1 month ago

@ngocdotnt So your command only works when replacing ZkNew with lua require...? and are you on Linux of OSX?

  • Yes, it works fine if use zk.commands.
  • No, i'm on windows 10.
tjex commented 1 week ago

Ok, I've updated the zk to version zk 0.14.1-28-g73092a6 Just running :ZkNew without any args as neovim command works in every kind of buffer. Using the shortcut <leader>zn as above just works in non-zk buffers but not in zk-buffers.

Reading this again fresh. It sounds like the culprit could be how you're loading your keybinds. I see that they're not being loaded via on_attach. This could be why the commands aren't working in zk buffers.

The way to test would be to create a singular keybind in on attach that just creates a new note. Then iterate from there.

on_attach = function()
        vim.keymap.set('n', '<leader>zn', '<cmd>ZkNew<cr>')
end,