feat(init): multi-level priority with <C-a>/<C-x>

Problem: priority was binary (0 or 1), toggled with !, with no way
to express finer gradations or use Vim's native increment idiom.

Solution: replace toggle_priority with change_priority(delta) which
clamps to floor 0. Display format changes from '! ' to '[N] ' so any
integer level is representable. Parser updated to extract numeric
level from the [N] prefix. Visual g<C-a>/g<C-x> apply the delta to
all tasks in the selection. <Plug>(pending-priority) replaced with
<Plug>(pending-priority-up) and <Plug>(pending-priority-down).
This commit is contained in:
Barrett Ruth 2026-02-24 22:01:21 -05:00 committed by Barrett Ruth
parent aae6989a19
commit fc4a47a1ec
5 changed files with 60 additions and 13 deletions

View file

@ -57,7 +57,7 @@ local function setup_syntax(bufnr)
syntax clear
syntax match taskId /^\/\d\+\// conceal
syntax match taskHeader /^\S.*$/ contains=taskId
syntax match taskPriority /! / contained containedin=taskLine
syntax match taskPriority /\[\d\+\] / contained containedin=taskLine
syntax match taskLine /^\/\d\+\/ .*$/ contains=taskId,taskPriority
]])
end)

View file

@ -34,10 +34,11 @@ function M.parse_buffer(lines)
table.insert(result, { type = 'blank', lnum = i })
elseif id or body then
local stripped = body:match('^ (.+)$') or body
local prio_str = stripped:match('^%[(%d+)%] ')
local priority = 0
if stripped:match('^! ') then
priority = 1
stripped = stripped:sub(3)
if prio_str then
priority = tonumber(prio_str)
stripped = stripped:sub(#prio_str + 4)
end
local description, metadata = parse.body(stripped)
if description and description ~= '' then

View file

@ -52,8 +52,17 @@ function M._setup_buf_mappings(bufnr)
vim.keymap.set('n', 'g?', function()
M.show_help()
end, opts)
vim.keymap.set('n', '!', function()
M.toggle_priority()
vim.keymap.set('n', '<C-a>', function()
M.change_priority(1)
end, opts)
vim.keymap.set('n', '<C-x>', function()
M.change_priority(-1)
end, opts)
vim.keymap.set('v', 'g<C-a>', function()
M.change_priority_visual(1)
end, opts)
vim.keymap.set('v', 'g<C-x>', function()
M.change_priority_visual(-1)
end, opts)
vim.keymap.set('n', 'D', function()
M.prompt_date()
@ -119,7 +128,8 @@ function M.toggle_complete()
buffer.render(bufnr)
end
function M.toggle_priority()
---@param delta integer
function M.change_priority(delta)
local bufnr = buffer.bufnr()
if not bufnr then
return
@ -137,7 +147,7 @@ function M.toggle_priority()
if not task then
return
end
local new_priority = task.priority == 1 and 0 or 1
local new_priority = math.max(0, task.priority + delta)
store.update(id, { priority = new_priority })
store.save()
buffer.render(bufnr)
@ -149,6 +159,33 @@ function M.toggle_priority()
end
end
---@param delta integer
function M.change_priority_visual(delta)
local bufnr = buffer.bufnr()
if not bufnr then
return
end
local start_row = vim.fn.line("'<")
local end_row = vim.fn.line("'>")
local meta = buffer.meta()
local changed = false
for row = start_row, end_row do
local m = meta[row]
if m and m.type == 'task' and m.id then
local task = store.get(m.id)
if task then
local new_priority = math.max(0, task.priority + delta)
store.update(m.id, { priority = new_priority })
changed = true
end
end
end
if changed then
store.save()
buffer.render(bufnr)
end
end
function M.prompt_date()
local bufnr = buffer.bufnr()
if not bufnr then
@ -305,7 +342,10 @@ function M.show_help()
'',
'<CR> Toggle complete/uncomplete',
'<Tab> Switch category/priority view',
'! Toggle priority',
'<C-a> Raise priority level',
'<C-x> Lower priority level',
'g<C-a> Raise priority for visual selection',
'g<C-x> Lower priority for visual selection',
'D Set due date',
'U Undo last write',
'o / O Add new task line',
@ -330,7 +370,8 @@ function M.show_help()
' Empty input clears due date',
'',
'Highlights:',
' PendingOverdue overdue tasks (red)',
' PendingOverdue overdue tasks (red)',
' PendingPriority [N] priority prefix',
'',
'Press q or <Esc> to close',
}

View file

@ -9,6 +9,7 @@ local config = require('pending.config')
---@field category? string
---@field overdue? boolean
---@field show_category? boolean
---@field priority? integer
---@class pending.views
local M = {}
@ -138,7 +139,7 @@ function M.category_view(tasks)
for _, task in ipairs(all) do
local prefix = '/' .. task.id .. '/'
local indent = ' '
local prio = task.priority == 1 and '! ' or ''
local prio = task.priority > 0 and ('[' .. task.priority .. '] ') or ''
local line = prefix .. indent .. prio .. task.description
table.insert(lines, line)
table.insert(meta, {

View file

@ -30,8 +30,12 @@ vim.keymap.set('n', '<Plug>(pending-view)', function()
require('pending.buffer').toggle_view()
end)
vim.keymap.set('n', '<Plug>(pending-priority)', function()
require('pending').toggle_priority()
vim.keymap.set('n', '<Plug>(pending-priority-up)', function()
require('pending').change_priority(1)
end)
vim.keymap.set('n', '<Plug>(pending-priority-down)', function()
require('pending').change_priority(-1)
end)
vim.keymap.set('n', '<Plug>(pending-date)', function()