local agenda = require 'agenda' local getopts = require 'agenda.util'.getopts -- If neovim was started by `agenda` the `AGENDA_FILE` env var will be set -- with the file name. if vim.env.AGENDA_FILE == vim.api.nvim_buf_get_name(0) then agenda.on_attach(0) end -- Callbacks in the middle of a function signature are PAIN. local function create_user_command(name, opts) local command = opts.command opts.command = nil vim.api.nvim_create_user_command(name, command, opts) end -- TODO: Handle subcommands (kinda like git-fugitive)? Could be useful if I'll -- add a remove subcommand. -- TODO: Handle -E option (why not?). create_user_command('Agenda', { desc = 'Open a note file/buffer for the given day or the current day', nargs = '*', bang = true, bar = true, command = function (command) local opts = { create = command.bang, } local i = 1 for optind, opt, optval in getopts(command.fargs, 'bce:t:') do if opt == 'b' then opts.backlog = true elseif opt == 'c' then opts.create = true elseif opt == 'e' then opts.extension = optval elseif opt == 't' then opts.name = optval else local err = string.format("agenda: invalid option: '%s'", opt) vim.notify(err, vim.log.levels.WARN) return end i = optind end local args = {} while command.fargs[i] do args[#args + 1] = command.fargs[i] i = i + 1 end local date if #args > 0 then date = table.concat(args, ' ') else date = opts.backlog and 'backlog' or 'today' end agenda.open(date, opts) end, -- TODO: Handle supported agenda edit options. complete = function (arg_lead, cmd_line, cursor_pos) local modifiers = { next = true, last = true, } local completions = {} local final = {} if cmd_line:match '^Agenda!? [%w-]*$' then completions = { 'next ', 'last ', 'yesterday', 'tomorrow', 'today', 'day', 'week', 'month', 'year', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday', } else local first = cmd_line:match '^Agenda (%w+)%s+%w*$' if modifiers[first] then completions = { 'day', 'week', 'month', 'year', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday', } end end for _, can in pairs(completions) do if vim.startswith(can, arg_lead) then final[#final + 1] = can end end return final end, })