diff --git a/lua/orgmode/config/mappings/init.lua b/lua/orgmode/config/mappings/init.lua index 0a1bb8abc..a760b79ec 100644 --- a/lua/orgmode/config/mappings/init.lua +++ b/lua/orgmode/config/mappings/init.lua @@ -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' } } diff --git a/lua/orgmode/org/links/init.lua b/lua/orgmode/org/links/init.lua index e25c2fc27..0f1bf8255 100644 --- a/lua/orgmode/org/links/init.lua +++ b/lua/orgmode/org/links/init.lua @@ -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 @@ -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 diff --git a/lua/orgmode/utils/init.lua b/lua/orgmode/utils/init.lua index 727eceacd..5b60928eb 100644 --- a/lua/orgmode/utils/init.lua +++ b/lua/orgmode/utils/init.lua @@ -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