Skip to content

Commit ce4e92d

Browse files
bi0ha2ardSebastian Flügge
authored and
Sebastian Flügge
committed
fix: make initial state (headlines vs orgfiles) deterministic
Also allows directly opening as one or the other via :Telescope orgmode search_headings mode=orgfiles
1 parent 3cc876a commit ce4e92d

File tree

8 files changed

+30
-11
lines changed

8 files changed

+30
-11
lines changed

lua/telescope-orgmode/config.lua

+9-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,21 @@ function M.setup(ext_opts)
88
M.opts = vim.tbl_extend('force', M.opts, ext_opts or {})
99
end
1010

11-
function M.init_opts(opts, prompt_titles)
11+
function M.init_opts(opts, prompt_titles, default_state)
1212
opts = vim.tbl_extend('force', M.opts, opts or {})
13+
opts.mode = opts.mode or default_state
14+
if not prompt_titles[opts.mode] then
15+
error("Invalid mode '" .. opts.mode .. "'. Valid modes are: " .. table.concat(vim.tbl_keys(prompt_titles), ", "))
16+
end
1317
opts.prompt_titles = prompt_titles
1418
opts.states = {}
19+
opts.state = { current = opts.mode, next = opts.mode }
1520
for state, _ in pairs(prompt_titles) do
21+
if state ~= opts.mode then
22+
opts.state.next = state
23+
end
1624
table.insert(opts.states, state)
1725
end
18-
opts.state = { current = opts.states[1], next = opts.states[2] }
1926
return opts
2027
end
2128

lua/telescope-orgmode/entry_maker/headlines.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ local function index_headlines(file_results, opts)
2525
end
2626

2727
local M = {}
28-
---Fetches entrys from OrgApi and extracts the relevant information
28+
---Fetches entries from OrgApi and extracts the relevant information
2929
---@param opts any
3030
---@return OrgHeadlineEntry[]
3131
M.get_entries = function(opts)

lua/telescope-orgmode/entry_maker/orgfiles.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ local function index_orgfiles(file_results)
2222
return results
2323
end
2424

25-
---Fetches entrys from OrgApi and extracts the relevant information
25+
---Fetches entries from OrgApi and extracts the relevant information
2626
---@param opts any
2727
---@return OrgFileEntry[]
2828
M.get_entries = function(opts)

lua/telescope-orgmode/finders.lua

+12
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,16 @@ function M.orgfiles(opts)
1919
})
2020
end
2121

22+
-- return the correct finder for the current state
23+
function M.from_options(opts)
24+
if opts.state.current == 'headlines' then
25+
return M.headlines(opts)
26+
elseif opts.state.current == 'orgfiles' then
27+
return M.orgfiles(opts)
28+
else
29+
-- this should not happen
30+
error(string.format('Invalid state %s', opts.state.current))
31+
end
32+
end
33+
2234
return M

lua/telescope-orgmode/mappings.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local to_actions = require('telescope-orgmode.actions')
33
local M = {}
44

55
function M.attach_mappings(map, opts)
6-
map('i', '<C-Space>', to_actions.toggle_headlines_orgfiles(opts), { desc = 'Toggle headline/orgfile' })
6+
map('i', '<c-space>', to_actions.toggle_headlines_orgfiles(opts), { desc = 'Toggle headline/orgfile' })
77
M.attach_custom(map, opts)
88
end
99

lua/telescope-orgmode/picker/insert_link.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ return function(opts)
1111
opts = config.init_opts(opts, {
1212
headlines = 'Insert link to headline',
1313
orgfiles = 'Insert link to org file',
14-
})
14+
}, "headlines")
1515

1616
pickers
1717
.new(opts, {
1818
prompt_title = opts.prompt_titles[opts.state.current],
19-
finder = finders.headlines(opts),
19+
finder = finders.from_options(opts),
2020
sorter = conf.generic_sorter(opts),
2121
previewer = conf.grep_previewer(opts),
2222
attach_mappings = function(_, map)

lua/telescope-orgmode/picker/refile_heading.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ return function(opts)
1212
opts = config.init_opts(opts, {
1313
headlines = 'Refile to headline',
1414
orgfiles = 'Refile to orgfile',
15-
})
15+
}, "headlines")
1616

1717
local closest_headline = org.get_closest_headline()
1818

1919
pickers
2020
.new(opts, {
2121
prompt_title = opts.prompt_titles[opts.state.current],
22-
finder = finders.headlines(opts),
22+
finder = finders.from_options(opts),
2323
sorter = conf.generic_sorter(opts),
2424
previewer = conf.grep_previewer(opts),
2525
attach_mappings = function(_, map)

lua/telescope-orgmode/picker/search_headings.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ return function(opts)
99
opts = config.init_opts(opts, {
1010
headlines = 'Search headlines',
1111
orgfiles = 'Search org files',
12-
})
12+
}, "headlines")
1313

1414
pickers
1515
.new(opts, {
1616
prompt_title = opts.prompt_titles[opts.state.current],
17-
finder = finders.headlines(opts),
17+
finder = finders.from_options(opts),
1818
sorter = conf.generic_sorter(opts),
1919
previewer = conf.grep_previewer(opts),
2020
attach_mappings = function(_, map)

0 commit comments

Comments
 (0)