Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 36c76c2

Browse files
seflueSebastian Flügge
and
Sebastian Flügge
authored
feat(api): Allow to insert links through API (#731)
* feat(api): Allow to insert links through API To give plugins like Telescope-orgmode the ability to insert links, the API needs an extension. The refactoring moves some methods from org.mappings into modules, where they should belong anyway, so they can be used from the new api method. The new API method api.insert_link is mostly existing code moved from org.mappings.insert_link, which now just calls api.insert_link. * refactor: move link insertion into Hyperlinks To have clean module dependencies, insert_link is now a method of Hyperlinks and is exposed over the api but also called from mappings. --------- Co-authored-by: Sebastian Flügge <sebastian.fluegge@dnv.com>
1 parent 2f96359 commit 36c76c2

File tree

3 files changed

+67
-52
lines changed

3 files changed

+67
-52
lines changed

lua/orgmode/api/init.lua

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
---@diagnostic disable: invisible
22
local OrgFile = require('orgmode.api.file')
33
local OrgHeadline = require('orgmode.api.headline')
4+
local Hyperlinks = require('orgmode.org.hyperlinks')
5+
local Link = require('orgmode.org.hyperlinks.link')
46
local orgmode = require('orgmode')
57

68
---@class OrgApiRefileOpts
@@ -99,4 +101,11 @@ function OrgApi.refile(opts)
99101
return true
100102
end
101103

104+
--- Insert a link to a given location at the current cursor position
105+
--- @param link_location string
106+
--- @return boolean
107+
function OrgApi.insert_link(link_location)
108+
Hyperlinks.insert_link(link_location)
109+
end
110+
102111
return OrgApi

lua/orgmode/org/hyperlinks/init.lua

+55
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local org = require('orgmode')
22
local utils = require('orgmode.utils')
33
local fs = require('orgmode.utils.fs')
44
local Url = require('orgmode.org.hyperlinks.url')
5+
local Link = require('orgmode.org.hyperlinks.link')
56
local config = require('orgmode.config')
67
local Hyperlinks = {
78
stored_links = {},
@@ -205,4 +206,58 @@ function Hyperlinks.autocomplete_links(arg_lead)
205206
return vim.tbl_keys(Hyperlinks.stored_links)
206207
end
207208

209+
---@return OrgLink|nil, table | nil
210+
function Hyperlinks.get_link_under_cursor()
211+
local line = vim.fn.getline('.')
212+
local col = vim.fn.col('.') or 0
213+
return Link.at_pos(line, col)
214+
end
215+
216+
function Hyperlinks.insert_link(link_location)
217+
local selected_link = Link:new(link_location)
218+
local desc = selected_link.url:get_target_value()
219+
if selected_link.url:is_id() then
220+
local id_link = ('id:%s'):format(selected_link.url:get_id())
221+
desc = link_location:gsub('^' .. vim.pesc(id_link) .. '%s+', '')
222+
link_location = id_link
223+
end
224+
225+
local link_description = vim.trim(vim.fn.OrgmodeInput('Description: ', desc or ''))
226+
227+
link_location = '[' .. vim.trim(link_location) .. ']'
228+
229+
if link_description ~= '' then
230+
link_description = '[' .. link_description .. ']'
231+
end
232+
233+
local insert_from
234+
local insert_to
235+
local target_col = #link_location + #link_description + 2
236+
237+
-- check if currently on link
238+
local link, position = Hyperlinks.get_link_under_cursor()
239+
if link and position then
240+
insert_from = position.from - 1
241+
insert_to = position.to + 1
242+
target_col = target_col + position.from
243+
else
244+
local colnr = vim.fn.col('.')
245+
insert_from = colnr
246+
insert_to = colnr + 1
247+
target_col = target_col + colnr
248+
end
249+
250+
local linenr = vim.fn.line('.') or 0
251+
local curr_line = vim.fn.getline(linenr)
252+
local new_line = string.sub(curr_line, 0, insert_from)
253+
.. '['
254+
.. link_location
255+
.. link_description
256+
.. ']'
257+
.. string.sub(curr_line, insert_to, #curr_line)
258+
259+
vim.fn.setline(linenr, new_line)
260+
vim.fn.cursor(linenr, target_col)
261+
end
262+
208263
return Hyperlinks

lua/orgmode/org/mappings.lua

+3-52
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ local Promise = require('orgmode.utils.promise')
1616
local events = EventManager.event
1717
local Link = require('orgmode.org.hyperlinks.link')
1818
local Babel = require('orgmode.babel')
19+
local OrgApi = require('orgmode.api')
1920

2021
---@class OrgMappings
2122
---@field capture OrgCapture
@@ -736,50 +737,7 @@ function OrgMappings:insert_link()
736737
return
737738
end
738739

739-
local selected_link = Link:new(link_location)
740-
local desc = selected_link.url:get_target_value()
741-
if selected_link.url:is_id() then
742-
local id_link = ('id:%s'):format(selected_link.url:get_id())
743-
desc = link_location:gsub('^' .. vim.pesc(id_link) .. '%s+', '')
744-
link_location = id_link
745-
end
746-
747-
local link_description = vim.trim(vim.fn.OrgmodeInput('Description: ', desc or ''))
748-
749-
link_location = '[' .. vim.trim(link_location) .. ']'
750-
751-
if link_description ~= '' then
752-
link_description = '[' .. link_description .. ']'
753-
end
754-
755-
local insert_from
756-
local insert_to
757-
local target_col = #link_location + #link_description + 2
758-
759-
-- check if currently on link
760-
local link, position = self:_get_link_under_cursor()
761-
if link and position then
762-
insert_from = position.from - 1
763-
insert_to = position.to + 1
764-
target_col = target_col + position.from
765-
else
766-
local colnr = vim.fn.col('.')
767-
insert_from = colnr
768-
insert_to = colnr + 1
769-
target_col = target_col + colnr
770-
end
771-
772-
local linenr = vim.fn.line('.') or 0
773-
local curr_line = vim.fn.getline(linenr)
774-
local new_line = string.sub(curr_line, 0, insert_from)
775-
.. '['
776-
.. link_location
777-
.. link_description
778-
.. ']'
779-
.. string.sub(curr_line, insert_to, #curr_line)
780-
781-
vim.fn.setline(linenr, new_line)
782-
vim.fn.cursor(linenr, target_col)
740+
Hyperlinks.insert_link(link_location)
783741
end
784742

785743
function OrgMappings:store_link()
@@ -823,7 +781,7 @@ function OrgMappings:_edit_special_callback()
823781
end
824782

825783
function OrgMappings:open_at_point()
826-
local link = self:_get_link_under_cursor()
784+
local link = Hyperlinks.get_link_under_cursor()
827785
if not link then
828786
local date = self:_get_date_under_cursor()
829787
if date then
@@ -1138,13 +1096,6 @@ function OrgMappings:_adjust_date(amount, span, fallback)
11381096
return vim.api.nvim_feedkeys(utils.esc(fallback), 'n', true)
11391097
end
11401098

1141-
---@return OrgLink|nil, table | nil
1142-
function OrgMappings:_get_link_under_cursor()
1143-
local line = vim.fn.getline('.')
1144-
local col = vim.fn.col('.') or 0
1145-
return Link.at_pos(line, col)
1146-
end
1147-
11481099
---@param headline OrgHeadline
11491100
function OrgMappings:_goto_headline(headline)
11501101
local current_file_path = utils.current_file_path()

0 commit comments

Comments
 (0)