|
| 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 |
0 commit comments