Skip to content

Commit

Permalink
feat: Added a command to open link under cursor
Browse files Browse the repository at this point in the history
Closes #173
  • Loading branch information
OXY2DEV committed Oct 9, 2024
1 parent 34330b4 commit 4ef3e11
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 42 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<p align="center">
Expand Down
86 changes: 44 additions & 42 deletions lua/markview/keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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("<cfile>"))
--- 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("<cfile>"), 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("<cfile>"))

if def_err then
vim.notify("[ Markview.nvim ] : Failed to open: " .. vim.fn.expand("<cfile>"), 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", "<CMD>MarkOpen", {

This comment has been minimized.

Copy link
@mawkler

mawkler Oct 9, 2024

Contributor

Note that <cmd> has to end with <cr>:

<cmd>MarkOpen<cr>
desc = "Opens the link under cursor"
})
end

Expand All @@ -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
Expand Down

0 comments on commit 4ef3e11

Please sign in to comment.