-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo.lua
More file actions
396 lines (341 loc) · 12.6 KB
/
todo.lua
File metadata and controls
396 lines (341 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
local state = {
floating = {
buf = -1,
win = -1,
},
help_win = -1,
}
local todo_file = vim.fn.stdpath("state") .. "/todo.md"
local lock_file = vim.fn.stdpath("state") .. "/todo.lock"
local backup_dir = vim.fn.stdpath("state") .. "/todo_backups/"
local load_todo_file -- forward declaration
local backup_file = backup_dir .. "todo_" .. os.date("%Y-%m-%d") .. ".md"
local function backup_todo()
if vim.fn.filereadable(todo_file) == 0 then return end
vim.fn.mkdir(backup_dir, "p")
if vim.fn.filereadable(backup_file) == 1 then return end
vim.fn.system({ "cp", todo_file, backup_file })
local files = vim.fn.glob(backup_dir .. "todo_[0-9][0-9][0-9][0-9]-*.md", false, true)
table.sort(files)
for i = 1, #files - 30 do
vim.fn.delete(files[i])
end
end
local function restore_todo()
local daily = vim.fn.glob(backup_dir .. "todo_[0-9][0-9][0-9][0-9]-*.md", false, true)
local pre = vim.fn.glob(backup_dir .. "todo_pre-restore_*.md", false, true)
local files = vim.list_extend(daily, pre)
if #files == 0 then
vim.notify("No todo backups found", vim.log.levels.WARN)
return
end
table.sort(files, function(a, b) return a > b end)
local labels = vim.tbl_map(function(f)
local date = f:match("todo_(%d%d%d%d%-%d%d%-%d%d)%.md")
if date then return date end
local ts = f:match("todo_pre%-restore_(%d%d%d%d%-%d%d%-%d%dT%d%d%d%d%d%d)%.md")
if ts then return "pre-restore " .. ts end
return f
end, files)
vim.ui.select(labels, { prompt = "Restore todo from backup:" }, function(choice, idx)
if not choice then return end
-- delete old pre-restore backups (except the one being restored)
for _, f in ipairs(vim.fn.glob(backup_dir .. "todo_pre-restore_*.md", false, true)) do
if f ~= files[idx] then vim.fn.delete(f) end
end
local pre_restore = backup_dir .. "todo_pre-restore_" .. os.date("%Y-%m-%dT%H%M%S") .. ".md"
if vim.fn.filereadable(todo_file) == 1 then
vim.fn.system({ "cp", todo_file, pre_restore })
end
vim.fn.system({ "cp", files[idx], todo_file })
if files[idx]:find("pre%-restore") then vim.fn.delete(files[idx]) end
vim.notify("Restored todo from " .. choice, vim.log.levels.INFO)
local buf = state.floating.buf
if buf and vim.api.nvim_buf_is_valid(buf) then
load_todo_file(buf)
end
end)
end
local function pid_alive(pid)
vim.fn.system("kill -0 " .. pid .. " 2>/dev/null")
return vim.v.shell_error == 0
end
local function acquire_lock()
if vim.fn.filereadable(lock_file) == 1 then
local pid = tonumber(vim.fn.readfile(lock_file)[1])
if pid and pid_alive(pid) then
return false
end
-- Stale lock — process is gone, clean it up
end
vim.fn.writefile({ tostring(vim.fn.getpid()) }, lock_file)
return true
end
local function release_lock()
if vim.fn.filereadable(lock_file) == 1 then
local pid = tonumber(vim.fn.readfile(lock_file)[1])
if pid == vim.fn.getpid() then
vim.fn.delete(lock_file)
end
end
end
local function create_floating_window(opts)
opts = opts or {}
local width = opts.width or math.floor(vim.o.columns * 0.7)
local height = opts.height or math.floor(vim.o.lines * 0.7)
local col = math.floor((vim.o.columns - width) / 2)
local row = math.floor((vim.o.lines - height) / 2)
local buf = state.floating.buf
if not vim.api.nvim_buf_is_valid(buf) then
buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_option(buf, "filetype", "markdown")
vim.api.nvim_buf_set_option(buf, "buflisted", false)
vim.api.nvim_buf_set_option(buf, "modifiable", true)
end
vim.api.nvim_set_hl(0, "TodoBorder", { fg = "#89b4fa", bg = "NONE" })
vim.api.nvim_set_hl(0, "TodoTitle", { fg = "#a6e3a1", bg = "NONE", bold = true })
local win = vim.api.nvim_open_win(buf, true, {
relative = "editor",
width = width,
height = height,
col = col,
row = row,
style = "minimal",
border = { "╭", "─", "╮", "│", "╯", "─", "╰", "│" },
title = " 📝 Todo List ",
title_pos = "center",
})
vim.api.nvim_win_set_option(win, "winhl", "FloatBorder:TodoBorder,FloatTitle:TodoTitle")
vim.api.nvim_win_set_option(win, "conceallevel", 2)
vim.api.nvim_win_set_option(win, "concealcursor", "niv")
vim.api.nvim_win_set_option(win, "number", true)
vim.api.nvim_win_set_option(win, "relativenumber", true)
vim.api.nvim_win_set_option(win, "wrap", true)
vim.api.nvim_buf_set_option(buf, "shiftwidth", 2)
vim.api.nvim_buf_set_option(buf, "tabstop", 2)
vim.api.nvim_buf_set_option(buf, "softtabstop", 2)
vim.api.nvim_buf_set_option(buf, "expandtab", true)
return { buf = buf, win = win }
end
load_todo_file = function(buf)
vim.api.nvim_buf_set_option(buf, "modifiable", true)
vim.fn.mkdir(vim.fn.stdpath("state"), "p")
if vim.fn.filereadable(todo_file) == 1 then
local lines = vim.fn.readfile(todo_file)
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
else
local initial_content = {
"# 📋 My Todo List",
"",
"## 🎯 Today's Tasks",
"- [ ] Add your important tasks here",
"- [ ] Use `<space>+Enter` to toggle completion",
"- [x] ✨ Example completed task",
"",
"## 💡 Ideas & Notes",
"- [ ] Brainstorm new features",
"- [ ] Review code improvements",
"",
"## 📅 This Week",
"- [ ] Plan upcoming projects",
"- [ ] Schedule team meetings",
"",
"---",
"*Tip: Use markdown formatting for better organization!*",
}
vim.api.nvim_buf_set_lines(buf, 0, -1, false, initial_content)
end
vim.api.nvim_buf_set_option(buf, "modified", false)
end
local function save_todo_file(buf)
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
vim.fn.writefile(lines, todo_file)
vim.api.nvim_buf_set_option(buf, "modified", false)
end
local function toggle_checkbox()
local line = vim.api.nvim_get_current_line()
local row = vim.api.nvim_win_get_cursor(0)[1]
if line:match("^%s*-%s%[%s%]") then
local new_line = line:gsub("^(%s*-%s)%[%s%](.*)$", "%1[x]%2")
vim.api.nvim_buf_set_lines(0, row - 1, row, false, { new_line })
elseif line:match("^%s*-%s%[x%]") then
local new_line = line:gsub("^(%s*-%s)%[x%](.*)$", "%1[ ]%2")
vim.api.nvim_buf_set_lines(0, row - 1, row, false, { new_line })
end
end
local function toggle_checkbox_range()
local start_row, end_row
local mode = vim.api.nvim_get_mode().mode
if mode == "V" or mode == "v" then
local start_pos = vim.fn.getpos("v")
local end_pos = vim.fn.getpos(".")
start_row = math.min(start_pos[2], end_pos[2])
end_row = math.max(start_pos[2], end_pos[2])
else
start_row = vim.api.nvim_win_get_cursor(0)[1]
end_row = start_row
end
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc>", true, false, true), "n", false)
for row = start_row, end_row do
local line = vim.api.nvim_buf_get_lines(0, row - 1, row, false)[1]
local modified_line
if line:match("^%s*-%s%[%s%]") then
modified_line = line:gsub("^(%s*-%s)%[%s%](.*)$", "%1[x]%2")
elseif line:match("^%s*-%s%[x%]") then
modified_line = line:gsub("^(%s*-%s)%[x%](.*)$", "%1[ ]%2")
else
modified_line = line
end
if modified_line ~= line then
vim.api.nvim_buf_set_lines(0, row - 1, row, false, { modified_line })
end
end
end
local function close_todo(buf)
save_todo_file(buf)
release_lock()
if vim.api.nvim_win_is_valid(state.help_win) then
vim.api.nvim_win_close(state.help_win, true)
state.help_win = -1
end
if vim.api.nvim_win_is_valid(state.floating.win) then
vim.api.nvim_win_hide(state.floating.win)
end
end
local function setup_todo_keymaps(buf)
local opts = { buffer = buf, silent = true }
vim.keymap.set("n", "<C-q>", function()
close_todo(buf)
end, vim.tbl_extend("force", opts, { desc = "Close todo window" }))
vim.keymap.set("n", "<cr>", toggle_checkbox, vim.tbl_extend("force", opts, { desc = "Toggle todo checkbox" }))
vim.keymap.set(
"v",
"<cr>",
toggle_checkbox_range,
vim.tbl_extend("force", opts, { desc = "Toggle multiple todo checkboxes" })
)
local function go_to_file(split_cmd)
local line = vim.api.nvim_get_current_line()
local col = vim.api.nvim_win_get_cursor(0)[2]
local start_pos = col
local end_pos = col
while start_pos > 0 and line:sub(start_pos, start_pos):match("[%S]") do
start_pos = start_pos - 1
end
start_pos = start_pos + 1
while end_pos < #line and line:sub(end_pos + 1, end_pos + 1):match("[%S]") do
end_pos = end_pos + 1
end
local filename = line:sub(start_pos, end_pos)
if filename == "" then
vim.notify("No file name under cursor", vim.log.levels.WARN)
return
end
local file_part, line_part, col_part = filename:match("^(.+):(%d+):?(%d*)$")
if not file_part then
file_part = filename
line_part = nil
col_part = nil
end
close_todo(buf)
vim.cmd(split_cmd .. " " .. vim.fn.fnameescape(file_part))
if line_part then
local line_num = tonumber(line_part)
local col_num = col_part and tonumber(col_part) or 1
vim.api.nvim_win_set_cursor(0, { line_num, col_num - 1 })
end
end
vim.keymap.set("n", "gf", function()
go_to_file("edit")
end, vim.tbl_extend("force", opts, { desc = "Close todo and go to file" }))
vim.keymap.set("n", "gF", function()
go_to_file("tabedit")
end, vim.tbl_extend("force", opts, { desc = "Close todo and go to file in new tab" }))
vim.keymap.set("n", "R", restore_todo, vim.tbl_extend("force", opts, { desc = "Restore todo from backup" }))
vim.keymap.set("n", "?", function()
if vim.api.nvim_win_is_valid(state.help_win) then
vim.api.nvim_win_close(state.help_win, true)
state.help_win = -1
else
local help_buf = vim.api.nvim_create_buf(false, true)
local help_content = {
"Keymaps:",
"<Enter> - Toggle checkbox",
"V + <Enter> - Toggle multiple todos",
"<Ctrl-q> - Close todo",
"gf - Close todo and go to file",
"gF - Close todo and go to file in new tab",
"R - Restore from backup",
"? - Toggle this help",
}
vim.api.nvim_buf_set_lines(help_buf, 0, -1, false, help_content)
local help_width = 38
local help_height = 8
local help_row = math.floor((vim.o.lines - help_height) / 2)
local help_col = math.floor((vim.o.columns - help_width) / 2)
state.help_win = vim.api.nvim_open_win(help_buf, true, {
relative = "editor",
width = help_width,
height = help_height,
row = help_row,
col = help_col,
style = "minimal",
border = "single",
zindex = 200,
})
vim.api.nvim_win_set_option(state.help_win, "winhl", "FloatBorder:TodoBorder")
vim.keymap.set("n", "?", function()
if vim.api.nvim_win_is_valid(state.help_win) then
vim.api.nvim_win_close(state.help_win, true)
state.help_win = -1
end
end, { buffer = help_buf, silent = true })
end
end, vim.tbl_extend("force", opts, { desc = "Toggle help" }))
end
local function toggle_todo()
if not vim.api.nvim_win_is_valid(state.floating.win) then
if not acquire_lock() then
vim.notify("Todo is already open in another instance", vim.log.levels.WARN)
return
end
state.floating = create_floating_window()
load_todo_file(state.floating.buf)
setup_todo_keymaps(state.floating.buf)
vim.api.nvim_create_autocmd({ "BufWriteCmd", "BufLeave", "WinClosed" }, {
buffer = state.floating.buf,
callback = function()
save_todo_file(state.floating.buf)
release_lock()
if vim.api.nvim_win_is_valid(state.help_win) then
vim.api.nvim_win_close(state.help_win, true)
state.help_win = -1
end
end,
})
else
close_todo(state.floating.buf)
end
end
vim.api.nvim_create_autocmd("VimLeavePre", {
callback = release_lock,
})
vim.api.nvim_create_user_command("Todo", toggle_todo, {})
vim.api.nvim_create_user_command("TodoRestore", restore_todo, {})
vim.keymap.set("n", "<space>td", toggle_todo, { desc = "Toggle todo window" })
backup_todo()
-- Yank current file path with line number
vim.keymap.set("n", "<leader>yf", function()
local file_path = vim.fn.expand("%")
local line_num = vim.api.nvim_win_get_cursor(0)[1]
local git_root = vim.fn.systemlist("git rev-parse --show-toplevel")[1]
local result
if git_root and git_root ~= "" then
local relative_path = vim.fn.fnamemodify(file_path, ":p"):gsub("^" .. git_root .. "/", "")
result = relative_path .. ":" .. line_num
else
result = vim.fn.fnamemodify(file_path, ":p") .. ":" .. line_num
end
vim.fn.setreg("+", result)
vim.notify("Yanked: " .. result)
end, { desc = "Yank file path with line number" })