diff --git a/README.md b/README.md index 45e2953..5f79872 100644 --- a/README.md +++ b/README.md @@ -227,6 +227,10 @@ Available subcommands, > Subcommands that end with `{n}` can also take a buffer id. If a buffer id isn't provided then the current buffer's id is used. > Completion for buffer id is also provided by the plugin. +Additional command(s), + +- `MarkOpen`, Opens the link under cursor, falls back to **vim.ui.open()**. + ## 🎨 Highlight groups

diff --git a/lua/markview/keymaps.lua b/lua/markview/keymaps.lua index cfc4d6d..5c3764d 100644 --- a/lua/markview/keymaps.lua +++ b/lua/markview/keymaps.lua @@ -4,51 +4,54 @@ local keymaps = {}; keymaps.views = {}; keymaps.on_bufs = {}; -keymaps.createKeymap = function (buffer) - vim.api.nvim_buf_set_keymap(buffer, "n", "gx", "", { - desc = "gx implamentation for Markview.nvim", - callback = function () - local buf_links = keymaps.views[buffer] or {}; - local cursor = vim.api.nvim_win_get_cursor(0); - - --- Iterate over all the available links - for _, link in ipairs(buf_links) do - --- Cursor isn't on the line of the link - if link.row_start + 1 ~= cursor[1] then - goto continue; - end - - --- Cursor isn't on the column range of the link - if cursor[2] < link.col_start or cursor[2] > link.col_end then - goto continue; - end - - --- Modify the address and open it in `vim.ui.open()` - local cmd, err = vim.ui.open(vim.fn.fnamemodify(link.address, ":~")) - - if err then - vim.notify("[ Markview.nvim ] : Failed to open: " .. link.address, vim.diagnostic.severity.WARN) - break; - end - - if cmd then - cmd:wait(); - break; - end - - ::continue:: +keymaps.create_command = function (buffer) + vim.api.nvim_buf_create_user_command(buffer, "MarkOpen", function () + local buf_links = keymaps.views[buffer] or {}; + local cursor = vim.api.nvim_win_get_cursor(0); + + --- Iterate over all the available links + for _, link in ipairs(buf_links) do + --- Cursor isn't on the line of the link + if link.row_start + 1 ~= cursor[1] then + goto continue; + end + + --- Cursor isn't on the column range of the link + if cursor[2] < link.col_start or cursor[2] > link.col_end then + goto continue; end - local def_cmd, def_err = vim.ui.open(vim.fn.expand("")) + --- Modify the address and open it in `vim.ui.open()` + local cmd, err = vim.ui.open(vim.fn.fnamemodify(link.address, ":~")) - if def_err then - vim.notify("[ Markview.nvim ] : Failed to open: " .. vim.fn.expand(""), vim.diagnostic.severity.WARN) + if err then + vim.notify("[ Markview.nvim ] : Failed to open: " .. link.address, vim.diagnostic.severity.WARN) + break; end - if def_cmd then - def_cmd:wait(); + if cmd then + cmd:wait(); + break; end - end, + + ::continue:: + end + + local def_cmd, def_err = vim.ui.open(vim.fn.expand("")) + + if def_err then + vim.notify("[ Markview.nvim ] : Failed to open: " .. vim.fn.expand(""), vim.diagnostic.severity.WARN) + end + + if def_cmd then + def_cmd:wait(); + end + end, {}) +end + +keymaps.createKeymap = function (buffer) + vim.api.nvim_buf_set_keymap(buffer, "n", "gx", "MarkOpen", { + desc = "Opens the link under cursor" }) end @@ -62,15 +65,14 @@ keymaps.init = function (buffer, parsed_content, config_table) end for _, content in ipairs(parsed_content --[[@as table]]) do - if content.type == "link" then - table.insert(keymaps.views[buffer], content); - elseif content.type == "image" then + if content.type:match("^link_") then table.insert(keymaps.views[buffer], content); end end if not vim.list_contains(keymaps.on_bufs, buffer) then keymaps.createKeymap(buffer); + keymaps.create_command(buffer); table.insert(keymaps.on_bufs, buffer) end