diff --git a/bundle/vim-zettelkasten/ftplugin/zkbrowser.lua b/bundle/vim-zettelkasten/ftplugin/zkbrowser.lua index cc0eb7f82..225f8deb9 100644 --- a/bundle/vim-zettelkasten/ftplugin/zkbrowser.lua +++ b/bundle/vim-zettelkasten/ftplugin/zkbrowser.lua @@ -16,6 +16,7 @@ vim.opt_local.keywordprg = ':ZkHover -preview' vim.opt_local.tagfunc = 'v:lua.zettelkasten.tagfunc' local win = require('spacevim.api.vim.window') +local hi = require('spacevim.api.vim.highlight') require('zettelkasten').add_hover_command() @@ -53,6 +54,17 @@ if vim.fn.mapcheck('[I', 'n') == '' then vim.cmd('ZkBrowse') end, }) + vim.api.nvim_buf_set_keymap(0, 'n', '', '', { + noremap = true, + silent = true, + nowait = true, + callback = function() + if hi.syntax_at() == 'ZettelKastenTags' then + vim.cmd('ZkBrowse #' .. vim.fn.expand('') ) + + end + end, + }) end local config = require('zettelkasten.config') diff --git a/lua/spacevim/api/vim/highlight.lua b/lua/spacevim/api/vim/highlight.lua index 46a5b8cc7..89b9e9531 100644 --- a/lua/spacevim/api/vim/highlight.lua +++ b/lua/spacevim/api/vim/highlight.lua @@ -101,4 +101,47 @@ function M.hi_separator(a, b) M.hi(hi_b_a) end +local function get_color(name) + local c = vim.api.nvim_get_hl(0, { name = name }) + + if c.link then + return get_color(c.link) + else + return c + end +end + + +function M.syntax_at(...) + local lnum = select(1, ...) or vim.fn.line('.') + local col = select(2, ...) or vim.fn.col('.') + local inspect = vim.inspect_pos(0, lnum - 1, col - 1) + local name, hl + if #inspect.semantic_tokens > 0 then + local token, priority = {}, 0 + for _, semantic_token in ipairs(inspect.semantic_tokens) do + if semantic_token.opts.priority > priority then + priority = semantic_token.opts.priority + token = semantic_token + end + end + if token then + name = token.opts.hl_group_link + hl = vim.api.nvim_get_hl(0, { name = token.opts.hl_group_link }) + end + elseif #inspect.treesitter > 0 then + for i = #inspect.treesitter, 1, -1 do + name = inspect.treesitter[i].hl_group_link + hl = vim.api.nvim_get_hl(0, { name = name }) + if hl.fg then + break + end + end + else + name = vim.fn.synIDattr(vim.fn.synID(vim.fn.line('.'), vim.fn.col('.'), 1), 'name', 'gui') + hl = get_color(name) + end + return name, hl +end + return M