Skip to content

Commit

Permalink
feat(links): Add support for visual mode for insert link
Browse files Browse the repository at this point in the history
Visual selection is used as a default description for the link
  • Loading branch information
kristijanhusak committed Jan 26, 2025
1 parent 50d7238 commit 0ef840a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lua/orgmode/config/mappings/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,13 @@ return {
args = { true },
opts = { desc = 'org timestamp (inactive)', help_desc = 'Insert/Update inactive date under cursor' },
}),
org_insert_link = m.action(
'org_mappings.insert_link',
{ opts = { desc = 'org insert link', help_desc = 'Insert a hyperlink' } }
),
org_insert_link = m.action('org_mappings.insert_link', {
modes = { 'n', 'x' },
opts = {
desc = 'org insert link',
help_desc = 'Insert or Update a hyperlink under cursor. Visual selection used as description',
},
}),
org_store_link = m.action(
'org_mappings.store_link',
{ opts = { desc = 'org store link', help_desc = 'Store link to current headline' } }
Expand Down
10 changes: 10 additions & 0 deletions lua/orgmode/org/links/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ function OrgLinks:insert_link(link_location, desc)
link_location = ('id:%s'):format(selected_link.url:get_path())
end

if not desc and vim.fn.mode() == 'v' then
desc = utils.get_visual_selection()
end

return Input.open('Description: ', desc or ''):next(function(link_description)
if not link_description then
return false
Expand All @@ -128,6 +132,12 @@ function OrgLinks:insert_link(link_location, desc)
insert_from = position.from - 1
insert_to = position.to + 1
target_col = target_col + position.from
elseif vim.fn.mode() == 'v' then
local start_pos = vim.fn.getpos('v')
local end_pos = vim.fn.getpos('.')
insert_from = start_pos[3] - 1
insert_to = end_pos[3] + 1
target_col = target_col + start_pos[3]
else
local colnr = vim.fn.col('.')
insert_from = colnr
Expand Down
5 changes: 5 additions & 0 deletions lua/orgmode/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -628,4 +628,9 @@ function utils.goto_headline(headline)
vim.cmd([[normal! zv]])
end

---@return string
function utils.get_visual_selection()
return table.concat(vim.fn.getregion(vim.fn.getpos('v'), vim.fn.getpos('.')), '\n')
end

return utils

0 comments on commit 0ef840a

Please sign in to comment.