From: Ruben Beltran del Rio Date: Fri, 1 Mar 2024 16:16:58 +0000 (+0100) Subject: Brain X-Git-Url: https://git.r.bdr.sh/rbdr/dotfiles/commitdiff_plain/fc82060a6f1fbdffb4e394c812ee5fd71ad4deb4 Brain --- diff --git a/config/nvim/init.lua b/config/nvim/init.lua index ac926b3..d414215 100644 --- a/config/nvim/init.lua +++ b/config/nvim/init.lua @@ -54,6 +54,12 @@ vim.keymap.set('i', '', ':m .-2==gi') vim.keymap.set('v', '', ':m \'>+1gv=gv') vim.keymap.set('v', '', ':m \'<-2gv=gv') +-- Navigate the location list +vim.keymap.set('n', '', ':lnext') +vim.keymap.set('n', '', ':lprev') +vim.keymap.set('n', '', ':cnext') +vim.keymap.set('n', '', ':cprev') + ------------------------------------------------------------------------------- -- File Specific Behavior ------------------------------------------------------------------------------- diff --git a/config/nvim/lua/brain.lua b/config/nvim/lua/brain.lua index 23556c6..1d04df0 100644 --- a/config/nvim/lua/brain.lua +++ b/config/nvim/lua/brain.lua @@ -1,8 +1,14 @@ local brain_home = os.getenv('HOME') .. '/brain' local templates_path = brain_home .. '/1 periodic/99 templates' +------------------------------------------------------------------------------- +-- Private functions for Periodic Tasks +------------------------------------------------------------------------------- + local function load_template(template_name) - local template_path = brain_home .. templates_path .. '/' .. template_name + local template_path = templates_path .. '/' .. template_name + + print(template_path) local template_file = io.open(template_path, 'r') if not template_file then @@ -11,6 +17,7 @@ local function load_template(template_name) end local content = template_file:read('*a') template_file:close() + return content end local function open_or_create_from_template(template_name, file) @@ -25,6 +32,35 @@ local function open_or_create_from_template(template_name, file) vim.cmd('edit ' .. file) end +local function find_tasks(completed) + local journal_path = brain_home .. '/1 periodic/01 journal/' + + local pattern = '^\\s*\\- \\[ \\]' + if completed == 1 then + pattern = '^\\s*\\- \\[x\\]' + end + + local command = string.format('rg --vimgrep \'%s\' \'%s\'', pattern, journal_path) + local results = vim.fn.systemlist(command) + + if vim.v.shell_error == 0 then + local items = {} + for _, line in ipairs(results) do + local filename, lnum, col, text = line:match("([^:]+):(%d+):(%d+):(.*)") + table.insert(items, { + filename = filename, + lnum = tonumber(lnum), + col = tonumber(col), + text = text, + }) + end + + -- Set location list for the current window and open it + vim.fn.setloclist(0, items) + vim.cmd('lopen') + end +end + local function get_this_weeks_files() local today = os.time() local day_of_week = os.date('*t', today).wday @@ -39,6 +75,39 @@ local function get_this_weeks_files() return filenames end +local function populate_quicklist_with_files(filenames) + local uv = vim.loop + local items = {} + local pattern = '^%s*%- %[[ ]?x?%]' + + for _, filename in ipairs(filenames) do + local daily_note = brain_home .. '/1 periodic/01 journal/' .. filename + local stat = uv.fs_stat(daily_note) + + if stat then -- File exists + local file, err = io.open(daily_note, 'r') + if file then + local set_header = 0 + local line_number = 0 + for line in file:lines() do + line_number = line_number + 1 + if line:match(pattern) then + if set_header == 0 then + local header = string.sub(filename:match('([^/\\]+)$'), 1, -4) + table.insert(items, {filename = '', lnum = 0, text = header}) + set_header = 1 + end + table.insert(items, {filename = daily_note, text = line, lnum = line_number}) + end + end + file:close() + end + end + end + vim.fn.setloclist(0, {}, ' ', {title = 'Weekly Tasks', items = items}) + vim.cmd('lopen') +end + ------------------------------------------------------------------------------- -- Periodic Notes ------------------------------------------------------------------------------- @@ -80,48 +149,40 @@ function toggle_task() end ------------------------------------------------------------------------------- --- Agenda View +-- Task Quicklist Views ------------------------------------------------------------------------------- function open_agenda() - local uv = vim.loop local week_filenames = get_this_weeks_files() - local items = {} - local pattern = '^%s*%- %[[ ]?x?%]' - print(week_filenames[1]) - - for _, filename in ipairs(week_filenames) do - local daily_note = brain_home .. '/1 periodic/01 journal/' .. filename - local stat = uv.fs_stat(daily_note) + populate_quicklist_with_files(week_filenames) +end - if stat then -- File exists - local file, err = io.open(daily_note, 'r') - if file then - local set_header = 0 - local line_number = 0 - for line in file:lines() do - line_number = line_number + 1 - if line:match(pattern) then - if set_header == 0 then - local header = filename:match('([^/\\]+)$') - table.insert(items, {filename = '', lnum = 0, text = header}) - set_header = 1 - end - table.insert(items, {filename = daily_note, text = line, lnum = line_number}) - end - end - file:close() - end - end - end - vim.fn.setloclist(0, {}, ' ', {title = 'Weekly Tasks', items = items}) - vim.cmd('lopen') +function open_open() + find_tasks(0) end ------------------------------------------------------------------------------- -- Key Bindings ------------------------------------------------------------------------------- +-- Periodic Notes vim.api.nvim_set_keymap('n', 'od', 'lua open_daily_note()', { noremap = true, silent = true }) vim.api.nvim_set_keymap('n', 'ow', 'lua open_weekly_note()', { noremap = true, silent = true }) +-- Planned: \om to open monthly, \os to open seasonly, and \oy to open yearly +-- Agenda Views vim.api.nvim_set_keymap('n', 'oa', 'lua open_agenda()', { noremap = true, silent = true }) +vim.api.nvim_set_keymap('n', 'oo', 'lua open_open()', { noremap = true, silent = true }) +-- Planned: \oj to open journal +-- Task Helpers vim.api.nvim_set_keymap('n', 't', 'lua toggle_task()', { noremap = true, silent = true }) +-- Planned: \it to insert tasks +-- Planned: \ct to capture task to an inbox +-- Planned: \cl to capture a learning +-- Planned: \cp to capture .plan +-- Planned: \rt reschedule task to today +-- Planned: \rT reschedule task for tomorrow +-- Planned But even later: \rr reschedule task for an arbitrary date. +-- Planned: \rs reschedule task for someday +------------------------------------------------------------------------------- +-- Ideas: +-- Navigate through the weeks like in org mode? +-- Labels?