Skip to content

Commit 3232e42

Browse files
author
Sebastian Flügge
committed
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.
1 parent a5c56c7 commit 3232e42

File tree

3 files changed

+50
-50
lines changed

3 files changed

+50
-50
lines changed

lua/orgmode/api/init.lua

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ local orgmode = require('orgmode')
1010
---@field destination OrgApiFile | OrgApiHeadline
1111

1212
---@class OrgApi
13-
---@field load fun(name?: string|string[]): OrgApiFile|OrgApiFile[]
14-
---@field current fun(): OrgApiFile
15-
---@field refile fun(opts: OrgApiRefileOpts)
16-
---@field insert_link fun(link_location: string): boolean
1713
local OrgApi = {}
1814

1915
---@param name? string|string[] specific file names to return (absolute path). If ommitted, returns all loaded files
@@ -109,50 +105,7 @@ end
109105
--- @param link_location string
110106
--- @return boolean
111107
function OrgApi.insert_link(link_location)
112-
local selected_link = Link:new(link_location)
113-
local desc = selected_link.url:get_target_value()
114-
if selected_link.url:is_id() then
115-
local id_link = ('id:%s'):format(selected_link.url:get_id())
116-
desc = link_location:gsub('^' .. vim.pesc(id_link) .. '%s+', '')
117-
link_location = id_link
118-
end
119-
120-
local link_description = vim.trim(vim.fn.OrgmodeInput('Description: ', desc or ''))
121-
122-
link_location = '[' .. vim.trim(link_location) .. ']'
123-
124-
if link_description ~= '' then
125-
link_description = '[' .. link_description .. ']'
126-
end
127-
128-
local insert_from
129-
local insert_to
130-
local target_col = #link_location + #link_description + 2
131-
132-
-- check if currently on link
133-
local link, position = Hyperlinks.get_link_under_cursor()
134-
if link and position then
135-
insert_from = position.from - 1
136-
insert_to = position.to + 1
137-
target_col = target_col + position.from
138-
else
139-
local colnr = vim.fn.col('.')
140-
insert_from = colnr
141-
insert_to = colnr + 1
142-
target_col = target_col + colnr
143-
end
144-
145-
local linenr = vim.fn.line('.') or 0
146-
local curr_line = vim.fn.getline(linenr)
147-
local new_line = string.sub(curr_line, 0, insert_from)
148-
.. '['
149-
.. link_location
150-
.. link_description
151-
.. ']'
152-
.. string.sub(curr_line, insert_to, #curr_line)
153-
154-
vim.fn.setline(linenr, new_line)
155-
vim.fn.cursor(linenr, target_col)
108+
Hyperlinks.insert_link(link_location)
156109
end
157110

158111
return OrgApi

lua/orgmode/org/hyperlinks/init.lua

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,59 @@ function Hyperlinks.autocomplete_links(arg_lead)
205205

206206
return vim.tbl_keys(Hyperlinks.stored_links)
207207
end
208-
--
208+
209209
---@return OrgLink|nil, table | nil
210210
function Hyperlinks.get_link_under_cursor()
211211
local line = vim.fn.getline('.')
212212
local col = vim.fn.col('.') or 0
213213
return Link.at_pos(line, col)
214214
end
215215

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+
216263
return Hyperlinks

lua/orgmode/org/mappings.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ function OrgMappings:insert_link()
737737
return
738738
end
739739

740-
OrgApi.insert_link(link_location)
740+
Hyperlinks.insert_link(link_location)
741741
end
742742

743743
function OrgMappings:store_link()

0 commit comments

Comments
 (0)