Skip to content

Commit 933cc0f

Browse files
committed
feat: allow configuration with telescope options
- add telescope compatible setup function - attach mappings from configuration within picker creation
1 parent a847849 commit 933cc0f

File tree

7 files changed

+58
-21
lines changed

7 files changed

+58
-21
lines changed

lua/telescope-orgmode/config.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
local M = {}
2+
3+
M.opts = {
4+
max_depth = nil,
5+
}
6+
7+
function M.setup(ext_opts)
8+
M.opts = vim.tbl_extend('force', M.opts, ext_opts or {})
9+
end
10+
11+
return M

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 {
2+
setup = require('telescope-orgmode.config').setup,
23
refile_heading = require('telescope-orgmode.refile_heading'),
34
search_headings = require('telescope-orgmode.search_headings'),
45
insert_link = require('telescope-orgmode.insert_link'),

lua/telescope-orgmode/insert_link.lua

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ local action_state = require('telescope.actions.state')
99
local OrgApi = require('orgmode.api')
1010

1111
local utils = require('telescope-orgmode.utils')
12+
local config = require('telescope-orgmode.config')
1213

1314
---@class MatchEntry
1415
---@field value OrgEntry
@@ -34,13 +35,12 @@ local function insert(prompt_bufnr)
3435
destination = 'file:' .. entry.value.file.filename .. '::*' .. entry.value.headline.title
3536
end
3637

37-
--print(vim.inspect(destination))
3838
OrgApi.insert_link(destination)
3939
return true
4040
end
4141

4242
return function(opts)
43-
opts = opts or {}
43+
opts = vim.tbl_extend('force', config.opts, opts or {})
4444

4545
pickers
4646
.new(opts, {
@@ -51,9 +51,14 @@ return function(opts)
5151
}),
5252
sorter = conf.generic_sorter(opts),
5353
previewer = conf.grep_previewer(opts),
54-
attach_mappings = function(prompt_bufnr, map)
54+
attach_mappings = function(_, map)
55+
map('i', '<C-Space>', utils.gen_depth_toggle(opts), { desc = 'Toggle headline/file jump' })
56+
for mode, mappings in pairs(opts.mappings or {}) do
57+
for key, action in pairs(mappings) do
58+
map(mode, key, action)
59+
end
60+
end
5561
action_set.select:replace(insert)
56-
map('i', '<c-space>', utils.gen_depth_toggle(opts, prompt_bufnr))
5762
return true
5863
end,
5964
})

lua/telescope-orgmode/refile_heading.lua

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local actions = require('telescope.actions')
66
local action_state = require('telescope.actions.state')
77

88
local utils = require('telescope-orgmode.utils')
9+
local config = require('telescope-orgmode.config')
910

1011
local OrgApi = require('orgmode.api')
1112

@@ -32,7 +33,7 @@ end
3233
M.closest_headline = nil
3334

3435
return function(opts)
35-
opts = opts or {}
36+
opts = vim.tbl_extend('force', config.opts, opts or {})
3637

3738
M.closest_headline = OrgApi.current():get_closest_headline()
3839

@@ -47,9 +48,14 @@ return function(opts)
4748
}),
4849
sorter = conf.generic_sorter(opts),
4950
previewer = conf.grep_previewer(opts),
50-
attach_mappings = function(prompt_bufnr, map)
51+
attach_mappings = function(_, map)
52+
map('i', '<C-Space>', utils.gen_depth_toggle(opts), { desc = 'Toggle headline/orgfile' })
53+
for mode, mappings in pairs(opts.mappings or {}) do
54+
for key, action in pairs(mappings) do
55+
map(mode, key, action)
56+
end
57+
end
5158
action_set.select:replace(M.refile)
52-
map('i', '<c-space>', utils.gen_depth_toggle(opts, prompt_bufnr))
5359
return true
5460
end,
5561
})

lua/telescope-orgmode/search_headings.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ local finders = require('telescope.finders')
33
local conf = require('telescope.config').values
44

55
local utils = require('telescope-orgmode.utils')
6+
local config = require('telescope-orgmode.config')
67

78
return function(opts)
8-
opts = opts or {}
9+
opts = vim.tbl_extend('force', config.opts, opts or {})
910

1011
pickers
1112
.new(opts, {
@@ -16,6 +17,15 @@ return function(opts)
1617
}),
1718
sorter = conf.generic_sorter(opts),
1819
previewer = conf.grep_previewer(opts),
20+
attach_mappings = function(_, map)
21+
map('i', '<C-Space>', utils.gen_depth_toggle(opts), { desc = 'Toggle headline/orgfile jump' })
22+
for mode, mappings in pairs(opts.mappings or {}) do
23+
for key, action in pairs(mappings) do
24+
map(mode, key, action)
25+
end
26+
end
27+
return true
28+
end,
1929
})
2030
:find()
2131
end

lua/telescope-orgmode/utils.lua

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,24 @@ utils.make_entry = function(opts)
9898
end
9999
end
100100

101-
utils.gen_depth_toggle = function(opts, prompt_bufnr)
102-
local status = state.get_status(prompt_bufnr)
103-
status._ot_current_depth = opts.max_depth
104-
status._ot_next_depth = nil
105-
if status._ot_current_depth ~= 0 then
106-
status._ot_next_depth = 0
107-
end
108-
109-
return function()
101+
utils.gen_depth_toggle = function(opts)
102+
return function(prompt_bufnr)
110103
local current_picker = action_state.get_current_picker(prompt_bufnr)
104+
local status = state.get_status(prompt_bufnr)
105+
106+
if status._ot_current_depth == nil and status._ot_next_depth == nil then
107+
-- uninitialized state - initialize with "show files only"
108+
-- Because when this function is called the first time, it is triggered
109+
-- by the users and we search over headings by default, we set the state
110+
-- for the first toggle already here.
111+
status._ot_current_depth = 0
112+
status._ot_next_depth = opts.max_depth
113+
else
114+
-- initalized state - swap to next state
115+
status._ot_current_depth, status._ot_next_depth = status._ot_next_depth, status._ot_current_depth
116+
end
111117

112-
local aux = status._ot_current_depth
113-
status._ot_current_depth = status._ot_next_depth
114-
status._ot_next_depth = aux
115-
118+
-- opts is used as a channel to communicate the depth state to the get_entries function
116119
opts.max_depth = status._ot_current_depth
117120
local new_finder = finders.new_table({
118121
results = utils.get_entries(opts),

lua/telescope/_extensions/orgmode/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
-- TODO: add highlight groups
44

55
return require('telescope').register_extension({
6+
setup = require('telescope-orgmode').setup,
67
exports = {
78
search_headings = require('telescope-orgmode').search_headings,
89
refile_heading = require('telescope-orgmode').refile_heading,

0 commit comments

Comments
 (0)