1 -------------------------------------------------------------------------------
2 -- Tools to deal with task views
3 -------------------------------------------------------------------------------
5 local Util = require('util')
6 local Configuration = require('configuration')
8 -------------------------------------------------------------------------------
10 -------------------------------------------------------------------------------
11 local function get_this_weeks_files()
12 local today = os.time()
13 local day_of_week = os.date('*t', today).wday
14 local week_start = today - (day_of_week - 2) * 86400
18 local date = os.date('*t', week_start + i * 86400)
19 table.insert(filenames, string.format('%04d-%02d-%02d.md', date.year, date.month, date.day))
25 local function find_tasks(completed, important)
26 local file_directory_path = Configuration.path_for()
28 local completed_fragment = '(\\s|x)'
29 if completed == 1 then
30 completed_fragment = 'x'
31 elseif completed == 0 then
32 completed_fragment = '\\s'
35 local important_fragment = '(\\-|\\*)'
36 if important == 1 then
37 important_fragment = '\\*'
38 elseif important == 0 then
39 important_fragment = '\\-'
43 local pattern = '^\\s*' .. important_fragment .. '\\s\\[' .. completed_fragment .. ']'
45 local command = string.format('rg --vimgrep \'%s\' \'%s\'', pattern, file_directory_path)
46 local results = vim.fn.systemlist(command)
48 if vim.v.shell_error == 0 then
50 for _, line in ipairs(results) do
51 local filename, lnum, col, text = line:match('([^:]+):(%d+):(%d+):(.*)')
54 lnum = tonumber(lnum),
60 -- Set location list for the current window and open it
61 vim.fn.setloclist(0, items)
66 local function populate_quicklist_with_files(filenames)
69 local task_pattern = '^%s*%- %[[ ]?x?%]'
70 local important_task_pattern = '^%s*%* %[[ ]?x?%]'
72 local file_directory_path = Configuration.path_for(Configuration.configuration.periodic_locations.daily)
73 Util.ensure_directory_exists(file_directory_path)
75 for _, filename in ipairs(filenames) do
76 local daily_note = Util.join(file_directory_path, filename)
77 local stat = uv.fs_stat(daily_note)
79 if stat then -- File exists
80 local file, err = io.open(daily_note, 'r')
84 for line in file:lines() do
85 line_number = line_number + 1
86 if line:match(task_pattern) or line:match(important_task_pattern) then
87 if set_header == 0 then
88 local header = string.sub(filename:match('([^/\\]+)$'), 1, -4)
89 table.insert(items, {filename = '', lnum = 0, text = header})
92 table.insert(items, {filename = daily_note, text = line, lnum = line_number})
99 vim.fn.setloclist(0, {}, ' ', {title = 'Weekly Tasks', items = items})
103 -------------------------------------------------------------------------------
105 -------------------------------------------------------------------------------
107 --- Opens the agenda view to show tasks
108 function TaskViews.open_agenda()
109 local week_filenames = get_this_weeks_files()
110 populate_quicklist_with_files(week_filenames)
113 --- Opens the view to show open tasks
114 function TaskViews.open_open()
118 --- Opens the view to show open important tasks
119 function TaskViews.open_open_important()
123 --- Opens the view to search the journal
124 function TaskViews.open_journal()
125 local pattern = '^\\s*(\\*|\\-)\\s\\[x]'
126 -- local pattern = 'hell'
127 local success, module = pcall(require, 'fzf-lua')
129 local notes_path = Configuration.path_for()
130 Util.ensure_directory_exists(notes_path)
131 module.files({ cwd = notes_path, cmd = 'rg --line-number --no-heading -- \'' .. pattern ..'\''})
133 api.nvim_err_writeln('This feature requires optional dependency fzf-lua')