|
| 1 | +local current_file_path = string.sub(debug.getinfo(1, 'S').source, 2) |
| 2 | +local docs_dir = vim.fn.fnamemodify(current_file_path, ':p:h:h:h:h') .. '/docs' |
| 3 | + |
| 4 | +---@param orgmode Org |
| 5 | +local build = function(orgmode) |
| 6 | + local Open = setmetatable({}, { |
| 7 | + __call = function(t, ...) |
| 8 | + t.a(...) |
| 9 | + end, |
| 10 | + __index = function(t, k) |
| 11 | + local existing = rawget(t, k) |
| 12 | + if existing then |
| 13 | + return existing |
| 14 | + end |
| 15 | + |
| 16 | + ---@diagnostic disable-next-line: invisible |
| 17 | + local keys = orgmode.agenda:_build_menu():get_valid_keys() |
| 18 | + |
| 19 | + for key, item in pairs(keys) do |
| 20 | + t[key] = item.action |
| 21 | + end |
| 22 | + |
| 23 | + return rawget(t, k) |
| 24 | + end, |
| 25 | + }) |
| 26 | + |
| 27 | + for _, shortcut in ipairs({ 'a', 't', 'm', 'M', 's' }) do |
| 28 | + Open[shortcut] = function() |
| 29 | + return orgmode.agenda:open_by_key(shortcut) |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + local OrgGlobal = { |
| 34 | + help = function() |
| 35 | + vim.cmd(('tabnew %s'):format(('%s/%s'):format(docs_dir, 'index.org'))) |
| 36 | + vim.cmd(('tcd %s'):format(docs_dir)) |
| 37 | + end, |
| 38 | + |
| 39 | + helpgrep = function() |
| 40 | + orgmode.agenda:open_view('search', { |
| 41 | + agenda_files = ('%s/**/*'):format(docs_dir), |
| 42 | + }) |
| 43 | + end, |
| 44 | + |
| 45 | + open = Open, |
| 46 | + } |
| 47 | + |
| 48 | + _G.Org = OrgGlobal |
| 49 | +end |
| 50 | + |
| 51 | +---@param opts string[] |
| 52 | +---@return table |
| 53 | +local function resolve_item(opts) |
| 54 | + ---@type table |
| 55 | + local obj = _G.Org |
| 56 | + for _, opt in ipairs(opts) do |
| 57 | + if type(obj) ~= 'table' then |
| 58 | + return obj |
| 59 | + end |
| 60 | + if obj[opt] then |
| 61 | + obj = obj[opt] |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + return obj |
| 66 | +end |
| 67 | + |
| 68 | +vim.api.nvim_create_user_command('Org', function(opts) |
| 69 | + local item = resolve_item(opts.fargs) |
| 70 | + if item and type(item) == 'function' then |
| 71 | + return item() |
| 72 | + end |
| 73 | + require('orgmode.utils').echo_error(('Invalid command "Org %s"'):format(opts.args)) |
| 74 | +end, { |
| 75 | + nargs = '*', |
| 76 | + complete = function(arg_lead, cmd_line) |
| 77 | + local opts = vim.split(cmd_line:sub(5), '%s+') |
| 78 | + local item = resolve_item(opts) |
| 79 | + if type(item) ~= 'table' then |
| 80 | + return {} |
| 81 | + end |
| 82 | + local list = vim.tbl_keys(item) |
| 83 | + |
| 84 | + if arg_lead == '' then |
| 85 | + return list |
| 86 | + end |
| 87 | + return vim.fn.matchfuzzy(list, arg_lead) |
| 88 | + end, |
| 89 | +}) |
| 90 | + |
| 91 | +return build |
0 commit comments