telescope-orgmode.nvim/lua/telescope/_extensions/orgmode/refile_heading.lua

80 lines
2.2 KiB
Lua
Raw Permalink Normal View History

2022-10-31 05:03:36 -05:00
local pickers = require("telescope.pickers")
local finders = require("telescope.finders")
local conf = require("telescope.config").values
local action_set = require("telescope.actions.set")
local actions = require("telescope.actions")
local action_state = require("telescope.actions.state")
2022-11-01 18:27:56 -05:00
local state = require("telescope.state")
2022-10-31 05:03:36 -05:00
local utils = require("telescope-orgmode.utils")
2022-10-31 05:03:36 -05:00
local api = require("orgmode.api")
2022-10-31 05:03:36 -05:00
return function(opts)
opts = opts or {}
2022-10-31 05:03:36 -05:00
local closest_headline = api.current():get_closest_headline()
2022-10-31 05:03:36 -05:00
local function refile(prompt_bufnr)
local entry = action_state.get_selected_entry()
actions.close(prompt_bufnr)
2022-10-31 05:03:36 -05:00
-- Refile to the file by default
local destination = entry.value.file
2022-10-31 18:31:43 -05:00
-- Refile to a specific heading if is set
if entry.value.headline then
destination = entry.value.headline
end
return api.refile({
source = closest_headline,
destination = destination,
})
end
2022-10-31 05:03:36 -05:00
local function gen_depth_toggle(opts, prompt_bufnr)
local status = state.get_status(prompt_bufnr)
status._ot_current_depth = opts.max_depth
status._ot_next_depth = nil
if status._ot_current_depth ~= 0 then
status._ot_next_depth = 0
end
2022-10-31 05:03:36 -05:00
return function()
local current_picker = action_state.get_current_picker(prompt_bufnr)
2022-10-31 05:03:36 -05:00
local aux = status._ot_current_depth
status._ot_current_depth = status._ot_next_depth
status._ot_next_depth = aux
2022-10-31 05:03:36 -05:00
opts.max_depth = status._ot_current_depth
local new_finder = finders.new_table({
results = utils.get_entries(opts),
entry_maker = opts.entry_maker or utils.make_entry(opts),
})
2022-11-01 18:27:56 -05:00
current_picker:refresh(new_finder, opts)
end
end
2022-10-31 05:03:36 -05:00
pickers
.new(opts, {
-- TODO: alter prompt title when depth is 0: Refile under file, Refile
-- under Headline
prompt_title = "Refile Destination",
finder = finders.new_table({
results = utils.get_entries(opts),
entry_maker = opts.entry_maker or utils.make_entry(opts),
}),
sorter = conf.generic_sorter(opts),
previewer = conf.grep_previewer(opts),
attach_mappings = function(prompt_bufnr, map)
action_set.select:replace(refile)
map("i", "<c-space>", gen_depth_toggle(opts, prompt_bufnr))
return true
end,
})
:find()
2022-10-31 05:03:36 -05:00
end