|
| 1 | +local finders = require('telescope-orgmode.finders') |
| 2 | +local org = require('telescope-orgmode.org') |
| 3 | + |
| 4 | +local actions = require('telescope.actions') |
| 5 | +local action_state = require('telescope.actions.state') |
| 6 | +local state = require('telescope.state') |
| 7 | + |
| 8 | +local M = {} |
| 9 | + |
| 10 | +function M.toggle_headlines_orgfiles(opts) |
| 11 | + return function(prompt_bufnr) |
| 12 | + local status = state.get_status(prompt_bufnr) |
| 13 | + |
| 14 | + -- _ot_ is used as our plugin-specific namespace in the action status |
| 15 | + -- (ot - orgmode telescope) |
| 16 | + -- |
| 17 | + -- FIXME: the state get's sometimes nil when the initalization has already been run |
| 18 | + -- In this case, a toggle is "dropped" (keypress does not change the state). |
| 19 | + -- Can we avoid that by initializing the state in the higher order function? |
| 20 | + -- Idea: We can try to do it as before, but pass the prompt_bufnr with the opts. |
| 21 | + if status._ot_state == nil then |
| 22 | + -- uninitialized state - initialize with orgfiles |
| 23 | + -- Because when this function is called the first time, it is triggered |
| 24 | + -- by the users and we search over headlines by default, we set the state |
| 25 | + -- for the first toggle already here. |
| 26 | + status._ot_state = { current = opts.states[2], next = opts.states[1] } |
| 27 | + else |
| 28 | + status._ot_state.current, status._ot_state.next = status._ot_state.next, status._ot_state.current |
| 29 | + end |
| 30 | + |
| 31 | + if status._ot_state.current == 'headlines' then |
| 32 | + M._find_headlines(opts, prompt_bufnr) |
| 33 | + elseif status._ot_state.current == 'orgfiles' then |
| 34 | + M._find_orgfiles(opts, prompt_bufnr) |
| 35 | + else |
| 36 | + -- this should not happen |
| 37 | + error(string.format('Invalid state %s', status._ot_state.current)) |
| 38 | + end |
| 39 | + end |
| 40 | +end |
| 41 | + |
| 42 | +function M.search_headlines(opts) |
| 43 | + return function(prompt_bufnr) |
| 44 | + M._find_headlines(opts, prompt_bufnr) |
| 45 | + end |
| 46 | +end |
| 47 | + |
| 48 | +function M.search_orgfiles(opts) |
| 49 | + return function(prompt_bufnr) |
| 50 | + M._find_orgfiles(opts, prompt_bufnr) |
| 51 | + end |
| 52 | +end |
| 53 | + |
| 54 | +function M.refile(closest_headline) |
| 55 | + return function(prompt_bufnr) |
| 56 | + local entry = action_state.get_selected_entry() |
| 57 | + actions.close(prompt_bufnr) |
| 58 | + |
| 59 | + -- Refile to the file by default |
| 60 | + local destination = entry.value.file |
| 61 | + |
| 62 | + -- Refile to a specific heading if is set |
| 63 | + if entry.value.headline then |
| 64 | + destination = entry.value.headline |
| 65 | + end |
| 66 | + |
| 67 | + return org.refile({ |
| 68 | + source = closest_headline, |
| 69 | + destination = destination, |
| 70 | + }) |
| 71 | + end |
| 72 | +end |
| 73 | + |
| 74 | +function M.insert(_) |
| 75 | + return function(prompt_bufnr) |
| 76 | + actions.close(prompt_bufnr) |
| 77 | + |
| 78 | + ---@type MatchEntry |
| 79 | + local entry = action_state.get_selected_entry() |
| 80 | + |
| 81 | + -- Link to the filename by default |
| 82 | + local destination = entry.value.file.filename |
| 83 | + |
| 84 | + -- Link to a specific heading if is set |
| 85 | + if entry.value.headline then |
| 86 | + destination = 'file:' .. entry.value.file.filename .. '::*' .. entry.value.headline.title |
| 87 | + end |
| 88 | + |
| 89 | + org.insert_link(destination) |
| 90 | + return true |
| 91 | + end |
| 92 | +end |
| 93 | + |
| 94 | +function M._find_headlines(opts, prompt_bufnr) |
| 95 | + local headlines = finders.headlines(opts) |
| 96 | + M._update_picker(headlines, opts.prompt_titles.headlines, prompt_bufnr) |
| 97 | +end |
| 98 | + |
| 99 | +function M._find_orgfiles(opts, prompt_bufnr) |
| 100 | + local orgfiles = finders.orgfiles(opts) |
| 101 | + M._update_picker(orgfiles, opts.prompt_titles.orgfiles, prompt_bufnr) |
| 102 | +end |
| 103 | + |
| 104 | +function M._update_picker(results, title, prompt_bufnr) |
| 105 | + local current_picker = action_state.get_current_picker(prompt_bufnr) |
| 106 | + |
| 107 | + current_picker.layout.prompt.border:change_title(title) |
| 108 | + current_picker:refresh(results) |
| 109 | +end |
| 110 | + |
| 111 | +return M |
0 commit comments