Skip to content

Commit 0ef840a

Browse files
feat(links): Add support for visual mode for insert link
Visual selection is used as a default description for the link
1 parent 50d7238 commit 0ef840a

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

lua/orgmode/config/mappings/init.lua

+7-4
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,13 @@ return {
319319
args = { true },
320320
opts = { desc = 'org timestamp (inactive)', help_desc = 'Insert/Update inactive date under cursor' },
321321
}),
322-
org_insert_link = m.action(
323-
'org_mappings.insert_link',
324-
{ opts = { desc = 'org insert link', help_desc = 'Insert a hyperlink' } }
325-
),
322+
org_insert_link = m.action('org_mappings.insert_link', {
323+
modes = { 'n', 'x' },
324+
opts = {
325+
desc = 'org insert link',
326+
help_desc = 'Insert or Update a hyperlink under cursor. Visual selection used as description',
327+
},
328+
}),
326329
org_store_link = m.action(
327330
'org_mappings.store_link',
328331
{ opts = { desc = 'org store link', help_desc = 'Store link to current headline' } }

lua/orgmode/org/links/init.lua

+10
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ function OrgLinks:insert_link(link_location, desc)
108108
link_location = ('id:%s'):format(selected_link.url:get_path())
109109
end
110110

111+
if not desc and vim.fn.mode() == 'v' then
112+
desc = utils.get_visual_selection()
113+
end
114+
111115
return Input.open('Description: ', desc or ''):next(function(link_description)
112116
if not link_description then
113117
return false
@@ -128,6 +132,12 @@ function OrgLinks:insert_link(link_location, desc)
128132
insert_from = position.from - 1
129133
insert_to = position.to + 1
130134
target_col = target_col + position.from
135+
elseif vim.fn.mode() == 'v' then
136+
local start_pos = vim.fn.getpos('v')
137+
local end_pos = vim.fn.getpos('.')
138+
insert_from = start_pos[3] - 1
139+
insert_to = end_pos[3] + 1
140+
target_col = target_col + start_pos[3]
131141
else
132142
local colnr = vim.fn.col('.')
133143
insert_from = colnr

lua/orgmode/utils/init.lua

+5
Original file line numberDiff line numberDiff line change
@@ -628,4 +628,9 @@ function utils.goto_headline(headline)
628628
vim.cmd([[normal! zv]])
629629
end
630630

631+
---@return string
632+
function utils.get_visual_selection()
633+
return table.concat(vim.fn.getregion(vim.fn.getpos('v'), vim.fn.getpos('.')), '\n')
634+
end
635+
631636
return utils

0 commit comments

Comments
 (0)