local checked_pattern = '^(%s*)%- %[x%]'
local checked_important_pattern = '^(%s*)%* %[x%]'
+local function is_open_task(line)
+ return line:match(unchecked_pattern) or line:match(unchecked_important_pattern)
+end
+
+local function is_completed_task(line)
+ return line:match(checked_pattern) or line:match(checked_important_pattern)
+end
+
+local function is_task(line)
+ return is_open_task(line) or is_completed_task(line)
+end
+
local function open(path)
local parent = Util.directory_name(path)
Util.ensure_directory_exists(parent)
local path = Configuration.path_for(Configuration.configuration.tasks.inbox)
open(path)
end
+
local function open_someday()
local path = Configuration.path_for(Configuration.configuration.tasks.someday)
open(path)
end
+
+local function parse_tags(tag_string)
+ local tags = {}
+ for tag in tag_string:gmatch('([^,]+)') do
+ table.insert(tags, tag)
+ end
+ return tags
+end
+
+local function table_contains_value(target_table, value)
+ for _, table_value in ipairs(target_table) do
+ if table_value == value then
+ return true
+ end
+ end
+ return false
+end
+
+local function remove_value(target_table, value)
+ for i, table_value in ipairs(target_table) do
+ if table_value == value then
+ table.remove(target_table, i)
+ end
+ end
+end
+
-------------------------------------------------------------------------------
-- Public Interface
-------------------------------------------------------------------------------
line = line:gsub(checked_pattern, '%1- [ ]', 1)
elseif line:match(checked_important_pattern) then
line = line:gsub(checked_important_pattern, '%1* [ ]', 1)
+ else
+ api.nvim_err_writeln('Toggle completion only works on tasks')
end
api.nvim_buf_set_lines(0, line_number - 1, line_number, false, {line})
line = line:gsub(checked_pattern, '%1* [x]', 1)
elseif line:match(checked_important_pattern) then
line = line:gsub(checked_important_pattern, '%1- [x]', 1)
+ else
+ api.nvim_err_writeln('Toggle importance only works on tasks')
end
api.nvim_buf_set_lines(0, line_number - 1, line_number, false, {line})
end
--- Tag a task
-function Tasks.tag()
- error('Not yet implemented')
+function Tasks.tag(tag)
+ local line_number = api.nvim_win_get_cursor(0)[1]
+ local line = api.nvim_get_current_line()
+ if is_task(line) then
+ local pattern = ':(.*):$'
+ local tag_string = line:match(pattern) or ''
+ if not tag or tag == '' then
+ tag = vim.fn.input('Add new tag for task (' .. tag_string .. '): ')
+ if not tag or tag == '' then
+ return
+ end
+ end
+ local tags = parse_tags(tag_string)
+ if not table_contains_value(tags, tag) then
+ table.insert(tags, tag)
+ local new_tags = table.concat(tags, ',')
+ if line:match(pattern) then
+ line = line:gsub(pattern, ':' .. new_tags .. ':', 1)
+ else
+ line = line .. ' :' .. new_tags .. ':'
+ end
+ api.nvim_buf_set_lines(0, line_number - 1, line_number, false, {line})
+ end
+ else
+ api.nvim_err_writeln('Tagging only works on tasks')
+ end
+end
+
+--- Remove tag on a task
+function Tasks.remove_tag(tag)
+ local line_number = api.nvim_win_get_cursor(0)[1]
+ local line = api.nvim_get_current_line()
+ if is_task(line) then
+ local pattern = ':(.*):$'
+ local tag_string = line:match(pattern)
+ if not tag_string then
+ api.nvim_err_writeln('No tags to remove')
+ return
+ end
+
+ if not tag or tag == '' then
+ tag = vim.fn.input('Pick tag to remove (' .. tag_string .. '): ')
+ end
+
+ local tags = parse_tags(tag_string)
+ remove_value(tags, tag)
+ local new_tags = ''
+
+ if #tags > 0 then
+ new_tags = ':' .. table.concat(tags, ',') .. ':'
+ end
+
+ line = line:gsub(pattern, new_tags, 1):gsub('%s+$', '')
+ api.nvim_buf_set_lines(0, line_number - 1, line_number, false, {line})
+ else
+ api.nvim_err_writeln('Tagging only works on tasks')
+ end
end
--- Reschedule a task for today
local line = api.nvim_get_current_line()
local filename = vim.fn.expand('%:t:r')
- if line:match(unchecked_pattern) or line:match(unchecked_important_pattern) then
+ if is_open_task(line) then
if Util.is_before_today(filename) and line:match(unchecked_pattern) then
local rescheduled_line = line:gsub(unchecked_pattern, '%1- [>' .. new_date .. ']', 1)
api.nvim_buf_set_lines(0, line_number - 1, line_number, false, {rescheduled_line})