Skip to content

Commit f53a8e5

Browse files
author
Sebastian Flügge
committed
feat: support inserting links via Telescope-orgmode
This feature provides a convenient way to create links to existing org-files and headlines via telescope. It leverages the already existing search for headlines. It relies on an extension of the API of nvim-orgmode, which exposes its existing feature to insert links. The Telescope feature acts as a drop-in-replacement to create/select the Link target.
1 parent 0ece91e commit f53a8e5

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

lua/telescope-orgmode/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
return {
22
refile_heading = require('telescope-orgmode.refile_heading'),
33
search_headings = require('telescope-orgmode.search_headings'),
4+
insert_link = require('telescope-orgmode.insert_link'),
45
}

lua/telescope-orgmode/insert_link.lua

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
local pickers = require('telescope.pickers')
2+
local finders = require('telescope.finders')
3+
local conf = require('telescope.config').values
4+
local action_set = require('telescope.actions.set')
5+
local actions = require('telescope.actions')
6+
local action_state = require('telescope.actions.state')
7+
8+
---@type OrgApi
9+
local OrgApi = require('orgmode.api')
10+
11+
local utils = require('telescope-orgmode.utils')
12+
13+
---@class MatchEntry
14+
---@field value OrgEntry
15+
---@field ordinal string
16+
---@field filename string
17+
---@field lnum number
18+
---@field display function
19+
---@field location string,
20+
---@field line string,
21+
22+
local function insert(prompt_bufnr)
23+
actions.close(prompt_bufnr)
24+
25+
---@type MatchEntry
26+
local entry = action_state.get_selected_entry()
27+
28+
-- Link to the filename by default
29+
local destination = entry.value.file.filename
30+
31+
-- Link to a specific heading if is set
32+
if entry.value.headline then
33+
destination = 'file:' .. entry.value.file.filename .. '::*' .. entry.value.headline.title
34+
end
35+
36+
--print(vim.inspect(destination))
37+
OrgApi.insert_link(destination)
38+
return true
39+
end
40+
41+
return function(opts)
42+
opts = opts or {}
43+
44+
pickers
45+
.new(opts, {
46+
prompt_title = 'Link Target',
47+
finder = finders.new_table({
48+
results = utils.get_entries(opts),
49+
entry_maker = opts.entry_maker or utils.make_entry(opts),
50+
}),
51+
sorter = conf.generic_sorter(opts),
52+
previewer = conf.grep_previewer(opts),
53+
attach_mappings = function(prompt_bufnr, map)
54+
action_set.select:replace(insert)
55+
map('i', '<c-space>', utils.gen_depth_toggle(opts, prompt_bufnr))
56+
return true
57+
end,
58+
})
59+
:find()
60+
end

lua/telescope/_extensions/orgmode/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ return require('telescope').register_extension({
66
exports = {
77
search_headings = require('telescope-orgmode').search_headings,
88
refile_heading = require('telescope-orgmode').refile_heading,
9+
insert_link = require('telescope-orgmode').insert_link,
910
},
1011
})

0 commit comments

Comments
 (0)