feat: add agenda plugin

Import the package from my dotfiles.
This commit is contained in:
2023-11-17 02:04:30 +01:00
parent 4d1c1d1535
commit 16a71cadc2
5 changed files with 357 additions and 1 deletions

121
plugin/agenda.lua Normal file
View File

@@ -0,0 +1,121 @@
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,
})