Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ObsidianLink and ObsidianLinkNew misbehave when called from a keybind #372

Closed
kohotli opened this issue Feb 3, 2024 · 6 comments
Closed
Labels
bug Something isn't working

Comments

@kohotli
Copy link

kohotli commented Feb 3, 2024

🐛 Describe the bug

I have ObsidianLink and ObsidianLinkNew bound to keys, and when I try to use them while having a keybind, the first time I use them they throw an error:

E5108: Error executing lua: vim/_editor.lua:0: nvim_exec2(): Vim:ObsidianLink must be called with visual selection                                                     
stack traceback:                                                                                                                                                       
        [C]: in function 'nvim_exec2'                                                                                                                                  
        vim/_editor.lua: in function 'cmd'                                                                                                                             
        /Users/amelia/.config/nvim/lua/plugins/obsidian.lua:143: in function </Users/amelia/.config/nvim/lua/plugins/obsidian.lua:142>

Even while I have text selected. If I clear my visual selection, select a different block of text, then use either command, they will perform the expected operation on the previously selected block.

Here's how I have these commands mapped:

local map = vim.keymap.set
map("v", "<leader>Ole", function()
	vim.cmd('ObsidianLink')
end)

map("v", "<leader>Oln", function()
	vim.cmd('ObsidianLinkNew')
end)

However, if I just call the commands by hitting colon and typing the commands in, they work as expected.

I'm using lazy.nvim as my plugin manager and have the version set to "*". Neovim version is 0.9.5 release, though this behavior remains the same as of the latest dev build of neovim.
Thanks in advance!

Versions

NVIM v0.9.5
Build type: Release
LuaJIT 2.1.1703358377

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/opt/homebrew/Cellar/neovim/0.9.5/share/nvim"

Run :checkhealth for more info
[obsidian.nvim (v2.9.0)] Commit SHA: 8b10f0c377ee6f8f45aaeb59a6eb8a8ce28c5a43
[plenary.nvim] Commit SHA: 4f71c0c4a196ceb656c824a70792f3df3ce6bb6d
[nvim-cmp] Commit SHA: 04e0ca376d6abdbfc8b52180f8ea236cbfddf782
[telescope.nvim] Commit SHA: 7b5c5f56a21e82fdcfe5b250278b8dfc4b1cbab4
ripgrep 14.1.0%
@kohotli kohotli added the bug Something isn't working label Feb 3, 2024
@epwalsh
Copy link
Owner

epwalsh commented Feb 5, 2024

Hey @kohotli, have you tried this:

 local map = vim.keymap.set
 map("v", "<leader>Ole", function()
-	vim.cmd("ObsidianLink")
+	vim.cmd("'<,'>ObsidianLink")
 end)

 map("v", "<leader>Oln", function()
-	vim.cmd("ObsidianLinkNew")
+	vim.cmd("'<,'>ObsidianLinkNew")
 end)

@kohotli
Copy link
Author

kohotli commented Feb 5, 2024

Just tried it, I now get a different error when I highlight something for the first time:

E5108: Error executing lua: vim/_editor.lua:0: nvim_exec2(): Vim:E20: Mark not set                                                                                     
stack traceback:                                                                                                                                                       
        [C]: in function 'nvim_exec2'                                                                                                                                  
        vim/_editor.lua: in function 'cmd'                                                                                                                             
        /Users/amelia/.config/nvim/lua/plugins/obsidian.lua:145: in function </Users/amelia/.config/nvim/lua/plugins/obsidian.lua:143>

And then it behaves the same as previously indicated in this issue (i.e. it makes a link of the previously highlighted text, not what's currently highlighted)

@sotte
Copy link
Contributor

sotte commented Feb 12, 2024

@kohotli, I stumbled upon something similar and have some notes here:
#329 (comment)

The marks that '<,'> are referring to and which are used here, are on set when you leave the visual mode. That is why calling the user command :ObsidianLink<cr> works (as you leave visual mode and implicitly setting the marks) but doing it via a keymap does not.

The whole visual selection, keymap, user command situation is a bit messy IMO.

A workaround from my obsidian configuration (with lazy):

return {
  "epwalsh/obsidian.nvim",
  keys = {
      {
        "<cr>",
        -- leave visual mode to set marks and make ObsidianLink command work
        ":ObsidianLink<cr>",
        desc = "Search link",
        ft = "markdown",
        mode = "v",
      },
  },
}

Some related links that might be helpful if we want to improve this in obsidian.nvim:

Here is an implementation of a user command in a different plugin which seems to work for this scenario:
https://github.com/linrongbin16/gitlinker.nvim/blob/cafacd0bbc9c6f5e89d4e5366d64a7ee9f239c98/lua/gitlinker.lua#L411-L428

And fzf-lua has this helper to get the visual selection:
https://github.com/ibhagwan/fzf-lua/blob/6ee73fdf2a79bbd74ec56d980262e29993b46f2b/lua/fzf-lua/utils.lua#L434-L466

epwalsh added a commit that referenced this issue Feb 13, 2024
@epwalsh
Copy link
Owner

epwalsh commented Feb 13, 2024

@sotte thanks for that. I adapted that helper function from fzf-lua in #396. Hopefully that gets it working for this use case.

@kohotli
Copy link
Author

kohotli commented Feb 13, 2024

@epwalsh This fixes my problem! Thanks much for taking care of that :D

@kohotli kohotli closed this as completed Feb 13, 2024
@epwalsh
Copy link
Owner

epwalsh commented Feb 13, 2024

Yay!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants