Files
agenda-nvim/lua/agenda.lua
Jonas Kattendick 16a71cadc2 feat: add agenda plugin
Import the package from my dotfiles.
2023-11-17 02:04:30 +01:00

133 lines
3.0 KiB
Lua

local api = vim.api
local fn = vim.fn
---Wrapper for using my `agenda` script from within neovim.
local M = {}
function M.on_attach(buf)
vim.bo[buf].bufhidden = 'delete'
end
---@return string
function M.dir()
if vim.env.AGENDA_DIR then
return vim.env.AGENDA_DIR
end
if vim.env.XDG_DATA_HOME then
return string.format('%/agenda', vim.env.XDG_DATA_HOME)
end
return string.format('%/.local/share/agenda', vim.env.HOME)
end
---Gets the first window with an agenda note buffer in a tabpage.
---@param tabpage integer Tabpage handle, or 0 for the current tabpage.
---@param opts {dir: string?}?
---@return integer? win Window handle
function M.tabpage_get_win(tabpage, opts)
local agenda_dir = opts and opts.dir or M.dir()
for _, win in ipairs(api.nvim_tabpage_list_wins(tabpage)) do
local buf = api.nvim_win_get_buf(win)
-- Better to look at the name (file path), since that state is always there
-- (e.g. after restoring a session).
local name = api.nvim_buf_get_name(buf)
if vim.startswith(name, agenda_dir) then
return win
end
end
end
---@class agenda.Opts
---@field backlog string? Set the -b flag.
---@field create boolean? Set the -c flag.
---@field dir string? Set this as AGENDA_DIR value.
---@field extension string? Set this as -e option value.
---@field name string? Set this as -t option value.
---Open an agenda note.
---@param date string Either the date or backlog name.
---@param opts agenda.Opts?
function M.open(date, opts)
local filepath, err = M.agenda(date, opts)
if not filepath or fn.filereadable(filepath) < 1 then
if not err then
err = string.format("agenda: file '%s' is not readable", filepath)
end
vim.notify(err, vim.log.levels.WARN)
return
end
local win = M.tabpage_get_win(0)
if win then
vim.api.nvim_set_current_win(win)
vim.cmd.edit(filepath)
else
vim.cmd.split(filepath)
end
M.on_attach(0)
end
---Query an agenda note's file path.
---@param date string Either the date or backlog name.
---@param opts agenda.Opts?
---@return string? filepath
---@return string? err
function M.agenda(date, opts)
local env = {
-- An empty environment table is invalid.
AGENDA_DIR = vim.env.AGENDA_DIR
}
local cmd = { 'agenda', '-E' }
if opts then
if opts.dir then
env.AGENDA_DIR = opts.dir
end
if opts.backlog then
cmd[#cmd + 1] = '-b'
end
if opts.create then
cmd[#cmd + 1] = '-c'
end
if opts.extension then
cmd[#cmd + 1] = '-e'
cmd[#cmd + 1] = opts.extension
end
if opts.name then
cmd[#cmd + 1] = '-t'
cmd[#cmd + 1] = opts.name
end
end
cmd[#cmd + 1] = date
local filepath = ''
local err = ''
local pid = fn.jobstart(cmd, {
env = env,
on_stdout = function(_, data)
filepath = filepath .. data[1]
end,
on_stderr = function(_, data)
err = err .. data[1]
end,
})
local code = unpack(fn.jobwait { pid })
if code > 0 then
return nil, err
end
return filepath
end
return M