Skip to content

Commit d975c9e

Browse files
authored
Add basic org-insert-link functionality (#512)
1 parent 4a7b926 commit d975c9e

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

DOCS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,9 @@ Toggle current line checkbox state
821821
#### **org_toggle_heading**
822822
*mapped to*: `<Leader>o*`<br />
823823
Toggle current line to headline and vice versa. Checkboxes will turn into TODO headlines.
824+
#### **org_insert_link**
825+
*mapped to*: `<Leader>oil`<br />
826+
Insert a hyperlink at cursor position. When the cursor is on a hyperlink, edit that hyperlink.<br />
824827
#### **org_open_at_point**
825828
*mapped to*: `<Leader>oo`<br />
826829
Open hyperlink or date under cursor. When date is under the cursor, open the agenda for that day.<br />

lua/orgmode/config/defaults.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ local DefaultConfig = {
143143
org_schedule = '<prefix>is',
144144
org_time_stamp = '<prefix>i.',
145145
org_time_stamp_inactive = '<prefix>i!',
146+
org_insert_link = '<prefix>il',
146147
org_clock_in = '<prefix>xi',
147148
org_clock_out = '<prefix>xo',
148149
org_clock_cancel = '<prefix>xq',

lua/orgmode/config/mappings/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ return {
131131
'org_mappings.org_time_stamp',
132132
{ args = { true }, opts = { desc = 'org timestamp (inactive)' } }
133133
),
134+
org_insert_link = m.action('org_mappings.insert_link', { opts = { desc = 'org insert link' } }),
134135
org_clock_in = m.action('clock.org_clock_in', { opts = { desc = 'org clock in' } }),
135136
org_clock_out = m.action('clock.org_clock_out', { opts = { desc = 'org clock out' } }),
136137
org_clock_cancel = m.action('clock.org_clock_cancel', { opts = { desc = 'org clock cancel' } }),

lua/orgmode/org/mappings.lua

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,51 @@ function OrgMappings:_insert_heading_from_plain_line(suffix)
707707
end
708708
end
709709

710+
-- Inserts a new link at the cursor position or modifies the link the cursor is
711+
-- currently on
712+
function OrgMappings:insert_link()
713+
local link_location = vim.fn.OrgmodeInput('Links: ', '')
714+
if vim.trim(link_location) ~= '' then
715+
link_location = '[' .. link_location .. ']'
716+
else
717+
utils.echo_warning('No Link selected')
718+
return
719+
end
720+
local link_description = vim.trim(vim.fn.OrgmodeInput('Description: ', ''))
721+
if link_description ~= '' then
722+
link_description = '[' .. link_description .. ']'
723+
end
724+
725+
local insert_from
726+
local insert_to
727+
local target_col = #link_location + #link_description + 2
728+
729+
-- check if currently on link
730+
local link = self:_get_link_under_cursor()
731+
if link then
732+
insert_from = link.from - 1
733+
insert_to = link.to + 1
734+
target_col = target_col + link.from
735+
else
736+
local colnr = vim.fn.col('.')
737+
insert_from = colnr - 1
738+
insert_to = colnr
739+
target_col = target_col + colnr
740+
end
741+
742+
local linenr = vim.fn.line('.')
743+
local curr_line = vim.fn.getline(linenr)
744+
local new_line = string.sub(curr_line, 0, insert_from)
745+
.. '['
746+
.. link_location
747+
.. link_description
748+
.. ']'
749+
.. string.sub(curr_line, insert_to, #curr_line)
750+
751+
vim.fn.setline(linenr, new_line)
752+
vim.fn.cursor(linenr, target_col)
753+
end
754+
710755
function OrgMappings:move_subtree_up()
711756
local item = Files.get_closest_headline()
712757
local prev_headline = item:get_prev_headline_same_level()
@@ -751,7 +796,7 @@ function OrgMappings:open_at_point()
751796
return
752797
end
753798

754-
local parts = vim.split(link, '][', true)
799+
local parts = vim.split(link.content, '][', true)
755800
local url = parts[1]
756801
local link_ctx = { base = url, skip_add_prefix = true }
757802
if url:find('^file:') then
@@ -991,6 +1036,7 @@ function OrgMappings:_get_date_under_cursor(col_offset)
9911036
return nil
9921037
end
9931038

1039+
-- TODO: this will result in a bug, when more than one date is in the line
9941040
return dates[1]
9951041
end
9961042

@@ -1025,7 +1071,7 @@ function OrgMappings:_adjust_date(amount, span, fallback)
10251071
return vim.api.nvim_feedkeys(utils.esc(fallback), 'n', true)
10261072
end
10271073

1028-
---@return string|nil
1074+
---@return table|nil
10291075
function OrgMappings:_get_link_under_cursor()
10301076
local found_link = nil
10311077
local links = {}
@@ -1035,10 +1081,10 @@ function OrgMappings:_get_link_under_cursor()
10351081
local start_from = #links > 0 and links[#links].to or nil
10361082
local from, to = line:find('%[%[(.-)%]%]', start_from)
10371083
if col >= from and col <= to then
1038-
found_link = link
1084+
found_link = { content = link, from = from, to = to }
10391085
break
10401086
end
1041-
table.insert(links, { link = link, from = from, to = to })
1087+
table.insert(links, { content = link, from = from, to = to })
10421088
end
10431089
return found_link
10441090
end

0 commit comments

Comments
 (0)