aboutsummaryrefslogtreecommitdiffstats
path: root/plugin/50-notes.lua
blob: 418f49b53d7d7fc0bd619160c8660652f66b720d (plain)
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
--
-- 50-notes.lua
--
-- * Turns the directory in `$NOTES_DIR` into a wiki of markdown notes linked with `[[wiki-links]]`.
-- * Generates ctags for the notes and regenerates them whenever a note is written.
-- * Provides 'omnifunc' wiki-link completion: note names inside `[[`, headings after `#`.
-- * Provides fzf-lua pickers to find notes by tag/name/section, by file name, or by content.
-- * Provides rename commands that move the note (or rewrite a heading) and update every
--   `[[wiki-link]]` pointing to it across all notes, editing loaded buffers in place and the rest
--   on disk.
--
-- User commands:
--   `NotesRename`: rename the current note and update wiki-links
--   `NotesRenameSection`: rename the heading at the cursor and update wiki-links
--   `NotesFindTags`: fuzzy-find notes (tag, name, section)
--   `NotesFindFile`: fuzzy-find notes (file name)
--   `NotesFindGrep`: fuzzy-find notes (content)
--   `NotesOpen`: open the notes directory and start finding
--
-- Keymaps:
--   `<Leader>nn`: fuzzy-find notes
--   `<Leader>nf`: fuzzy-find notes by file name
--   `<Leader>ng`: fuzzy-find notes by content
--

if vim.env.NOTES_DIR == nil then
	vim.notify("NOTES_DIR is not set", vim.log.levels.ERROR)
	return
end
local notes_dir = vim.fs.normalize(vim.env.NOTES_DIR)
local tagfile = vim.fs.joinpath(vim.fn.stdpath("state"), "notes-tags")

-- Generates ctags for (markdown) files in `notes_dir`
local function generate_tags()
	vim.system({
		"ctags",
		"--languages=Markdown",
		"--tag-relative=no",
		"--recurse",
		"-f",
		tagfile,
		notes_dir,
	})
end

-- Completion of all `*.md` note names (relative to the notes dir, extension stripped).
local function note_candidates()
	local out = {}
	local files = vim.fs.find(function(name)
		return name:match("%.md$")
	end, { path = notes_dir, type = "file", limit = math.huge })
	for _, f in ipairs(files) do
		out[#out + 1] = f:sub(#notes_dir + 2):gsub("%.md$", "")
	end
	return out
end

-- Headings of a note, read from the ctags tagfile so `#` comments inside fenced code blocks aren't
-- mistaken for headings (hashtag `h` / footnote `n` kinds excluded).
local function heading_candidates(note)
	local out = {}
	local target = notes_dir .. "/" .. note .. ".md"
	local fd = io.open(tagfile, "r")
	if fd == nil then
		return out
	end
	for line in fd:lines() do
		if line:sub(1, 1) ~= "!" then
			local name, file, _, kind = line:match('^(.-)\t(.-)\t(.-);"\t(%a)')
			if file == target and kind ~= "h" and kind ~= "n" then
				out[#out + 1] = name
			end
		end
	end
	fd:close()
	return out
end

-- 'omnifunc' for wiki-links: inside `[[`, complete note names; after `#`, complete the target
-- note's headings.
function _G.dotfiles_wikilink_source(findstart, base)
	local line = vim.api.nvim_get_current_line()
	local col = vim.api.nvim_win_get_cursor(0)[2]
	local before = line:sub(1, col)

	-- Locate the innermost still-open `[[` to the left of the cursor.
	local open, i = nil, 1
	while true do
		local s = before:find("%[%[", i)
		if s == nil then
			break
		end
		open, i = s, s + 2
	end
	local inner = open and before:sub(open + 2)
	if inner == nil or inner:find("%]") then -- not inside an open `[[ ... ]]`
		return findstart == 1 and -3 or {}
	end

	local hash = inner:find("#")
	if findstart == 1 then
		-- 0-based byte column where the completed text starts (after `[[` or after `#`).
		return hash and (open + 1 + hash) or (open + 1)
	end

	local candidates = hash and heading_candidates(inner:sub(1, hash - 1)) or note_candidates()
	if base ~= "" then
		candidates = vim.fn.matchfuzzy(candidates, base)
	end
	return candidates
end

-- Initializes a notes buffer
-- * Include generated notes tags in 'tags' option
-- * Enable wikilink completion
local function init_notes_buffer()
	if not vim.tbl_contains(vim.opt_local.tags:get(), tagfile) then
		vim.opt_local.tags:append(tagfile)
	end
	vim.bo.omnifunc = "v:lua.dotfiles_wikilink_source"
end

-- Rewrite `[[oldname]]` / `[[oldname#heading]]` targets to `newname`, matching the target exactly
-- so `[[oldname_extra]]` is left alone. The gsub replacement is a function, so its return value is
-- used literally (no `%` escaping needed).
-- Returns the new text and the number of links rewritten.
local function rewrite_links(text, oldname, newname)
	local count = 0
	local out = text:gsub("%[%[(.-)%]%]", function(inner)
		local target, rest = inner:match("^([^#]*)(.*)$")
		if target == oldname then
			count = count + 1
			return "[[" .. newname .. rest .. "]]"
		end
	end)
	return out, count
end

-- Rewrite loaded buffer `buf` with `rewriter`
-- Returns the number of elements rewritten
local function rewrite_loaded_buf(buf, rewriter)
	local was_modified = vim.bo[buf].modified
	local text = table.concat(vim.api.nvim_buf_get_lines(buf, 0, -1, false), "\n")
	local new_text, n
	new_text, n = rewriter(text)
	if n == 0 then
		return n
	end
	vim.api.nvim_buf_set_lines(buf, 0, -1, false, vim.split(new_text, "\n", { plain = true }))
	if not was_modified then -- buffer matched disk; keep it that way
		vim.api.nvim_buf_call(buf, function()
			vim.cmd("silent! write!")
		end)
	end
	return n
end

local function rewrite_unloaded_file(file, rewriter)
	local fd = io.open(file, "r")
	if not fd then
		vim.notify("Could not open file " .. file, vim.log.levels.ERROR)
		return 0
	end
	local content = fd:read("*a")
	fd:close()
	local new_content, n
	new_content, n = rewriter(content)
	if n == 0 then
		return n
	end
	local wf = io.open(file, "w")
	if wf then
		wf:write(new_content)
		wf:close()
	end
	return n
end

-- Apply `rewriter(text) -> new_text, n` to every note, editing loaded buffers in place (preserving
-- unsaved changes) and others on disk. Returns total edited links and files.
local function rewrite_all_notes(rewriter)
	local loaded = {}
	for _, buf in ipairs(vim.api.nvim_list_bufs()) do
		if vim.api.nvim_buf_is_loaded(buf) then
			loaded[vim.fs.normalize(vim.api.nvim_buf_get_name(buf))] = buf
		end
	end
	local files = vim.fs.find(function(name)
		return name:match("%.md$")
	end, { path = notes_dir, type = "file", limit = math.huge })

	local link_count, file_count = 0, 0
	for _, file in ipairs(files) do
		local buf = loaded[vim.fs.normalize(file)]
		local n = 0
		n = buf and rewrite_loaded_buf(buf, rewriter) or rewrite_unloaded_file(file, rewriter)
		if n > 0 then
			link_count = link_count + n
			file_count = file_count + 1
		end
	end
	return link_count, file_count
end

-- Returns a note's path relative to the notes dir, without `.md`, or nil if not a note.
local function note_rel_path(path)
	local prefix = vim.fs.normalize(notes_dir) .. "/"
	path = vim.fs.normalize(path)
	if not vim.startswith(path, prefix) then
		return nil
	end
	return (path:sub(#prefix + 1):gsub("%.md$", ""))
end

-- Move a file on disk. Returns `true` on success, or `false, err` on failure.
local function move_file(old_path, new_path)
	vim.fn.mkdir(vim.fs.dirname(new_path), "p")
	return vim.uv.fs_rename(old_path, new_path)
end

-- Renames buffer `buf` and delete old stale buffers with the original name
local function rename_buffer(buf, new_path)
	local old_path = vim.api.nvim_buf_get_name(buf)
	vim.api.nvim_buf_set_name(buf, new_path)
	vim.cmd("keepalt write!")
	for _, b in ipairs(vim.api.nvim_list_bufs()) do
		if b ~= buf and vim.api.nvim_buf_get_name(b) == old_path then
			pcall(vim.api.nvim_buf_delete, b, { force = true })
		end
	end
end

-- Rename note `old_path` → `new_path` and update every wiki-link pointing to it across all notes.
-- When `buf` is given (the note is open), it is repointed at `new_path` *before* links are rewritten,
-- so the rewrite reaches the buffer (incl. its own self-links) instead of being clobbered by a later
-- write. Regenerates the notes tags after renaming.
local function rename_note(old_path, new_path, buf)
	if not vim.startswith(old_path, notes_dir) or not vim.startswith(new_path, notes_dir) then
		vim.notify("old_path and new_path must both be inside " .. notes_dir, vim.log.levels.ERROR)
		return
	end
	if not vim.uv.fs_stat(old_path) then
		vim.notify("file to rename " .. old_path .. " does not exists", vim.log.levels.ERROR)
		return
	end
	if vim.uv.fs_stat(new_path) then
		vim.notify("target path " .. new_path .. " already exists", vim.log.levels.ERROR)
		return
	end
	local ok, err = move_file(old_path, new_path)
	if not ok then
		vim.notify("rename failed: " .. tostring(err), vim.log.levels.ERROR)
		return
	end
	if buf then
		rename_buffer(buf, new_path)
	end
	-- Wiki-links hold the note's rel path (no `.md`), so rewrite against those, not the full paths.
	local oldname, newname = note_rel_path(old_path), note_rel_path(new_path)
	local link_count, file_count = rewrite_all_notes(function(text)
		return rewrite_links(text, oldname, newname)
	end)
	generate_tags()

	local notification = string.format(
		"Renamed to %s — updated %d link(s) in %d file(s)",
		newname,
		link_count,
		file_count
	)
	vim.notify(notification)
end

-- Rename the current note to `new_path` (repointing this buffer at it).
local function rename_current_note(new_path)
	local buf = vim.api.nvim_get_current_buf()
	local old_path = vim.api.nvim_buf_get_name(buf)
	new_path = vim.fs.joinpath(notes_dir, new_path)
	rename_note(old_path, new_path, buf)
end

-- Rewrites `[[note#oldhead]]` heading anchors to `newhead` in `text`.
-- Returns the edited text and the number of substitutions performed.
local function rewrite_heading_links(text, note, oldhead, newhead)
	local count = 0
	local out = text:gsub("%[%[(.-)%]%]", function(inner)
		local target, head = inner:match("^([^#]*)#(.*)$")
		if target == note and head == oldhead then
			count = count + 1
			return "[[" .. target .. "#" .. newhead .. "]]"
		end
	end)
	return out, count
end

-- Returns the markdown heading at or above the cursor as `{ row, level, text }` (row is 1-based),
-- or nil if not found.
local function heading_at_or_above_cursor(buf)
	local row = vim.api.nvim_win_get_cursor(0)[1]
	local lines = vim.api.nvim_buf_get_lines(buf, 0, row, false)
	for r = #lines, 1, -1 do
		local hashes, text = lines[r]:match("^(#+)%s+(.-)%s*$")
		if hashes then
			return { row = r, level = #hashes, text = text }
		end
	end
	return nil
end

-- Rename section `old_head` → `new_head` in note `note` (rel path) and update every wiki-link
-- pointing to it across all notes. Regenerates the notes tags after renaming.
local function rename_section(note, old_head, new_head)
	local link_count, file_count = rewrite_all_notes(function(text)
		return rewrite_heading_links(text, note, old_head, new_head)
	end)
	generate_tags()

	local notification = string.format(
		"Renamed section to %s — updated %d link(s) in %d file(s)",
		new_head,
		link_count,
		file_count
	)
	vim.notify(notification)
end

-- Rename the heading at the cursor to `new_head` and update wiki-links to it.
local function rename_current_section(new_head)
	local buf = vim.api.nvim_get_current_buf()
	local note = note_rel_path(vim.api.nvim_buf_get_name(buf))
	if note == nil then
		vim.notify("Current buffer is not a note", vim.log.levels.ERROR)
		return
	end
	local heading = heading_at_or_above_cursor(buf)
	if heading == nil then
		vim.notify("No heading at or above the cursor", vim.log.levels.ERROR)
		return
	end
	new_head = vim.trim(new_head or "")
	if new_head == "" or new_head == heading.text then
		return
	end
	-- Rewrite the heading line itself, then persist before touching links.
	local line = string.rep("#", heading.level) .. " " .. new_head
	vim.api.nvim_buf_set_lines(buf, heading.row - 1, heading.row, false, { line })
	vim.cmd("write")

	rename_section(note, heading.text, new_head)
end

-- `:NotesRename` implementation: take the new note name from the command argument, or prompt for it
-- (defaulting to the current note's name), then rename via `rename_current_note`.
local function rename_note_command(opts)
	if opts.args ~= "" then
		rename_current_note(opts.args)
	else
		vim.ui.input(
			{ prompt = "Rename note to: ", default = note_rel_path(vim.api.nvim_buf_get_name(0)) },
			function(input)
				if input and input ~= "" then
					rename_current_note(input)
				end
			end
		)
	end
end

-- `:NotesRenameSection` implementation: take the new heading from the command argument, or prompt
-- for it (defaulting to the heading at the cursor), then rename via `rename_current_section`.
local function rename_section_command(opts)
	if opts.args ~= "" then
		rename_current_section(opts.args)
	else
		local heading = heading_at_or_above_cursor(vim.api.nvim_get_current_buf())
		vim.ui.input(
			{ prompt = "Rename section to: ", default = heading and heading.text },
			function(input)
				if input and input ~= "" then
					rename_current_section(input)
				end
			end
		)
	end
end

local function find_notes_by_tag()
	require("fzf-lua").tags({
		ctags_file = tagfile,
		cwd = notes_dir,
		winopts = { preview = { layout = "vertical" } },
		fzf_opts = { ["--delimiter"] = "[\t]", ["--with-nth"] = "{2}\t{1}", ["--tabstop"] = "48" },
	})
end

local function find_notes_by_name()
	require("fzf-lua").files({ cwd = notes_dir })
end

local function find_notes_by_grep()
	require("fzf-lua").live_grep({ cwd = notes_dir })
end

local function open_notes()
	vim.cmd.cd(notes_dir)
	vim.cmd.NotesFindTags()
end

----------------------------------------------------------------------------------------------------

-- Initializes note buffer
vim.api.nvim_create_autocmd({ "BufReadPost", "BufNewFile" }, {
	desc = "Set up notes buffer (tagfile, wiki-link completion)",
	group = vim.g.dotfiles.augroup,
	pattern = notes_dir .. "/*",
	callback = init_notes_buffer,
})

-- Regenerate tags everytime a note (i.e. file inside `notes_dir`) is written
vim.api.nvim_create_autocmd("BufWritePost", {
	desc = "Regenerate notes ctags",
	group = vim.g.dotfiles.augroup,
	pattern = notes_dir .. "/*",
	callback = generate_tags,
})

vim.api.nvim_create_user_command(
	"NotesRename",
	rename_note_command,
	{ nargs = "?", desc = "Rename current note and update wiki-links" }
)
vim.api.nvim_create_user_command(
	"NotesRenameSection",
	rename_section_command,
	{ nargs = "*", desc = "Rename the heading at the cursor and update wiki-links" }
)
vim.api.nvim_create_user_command(
	"NotesFindTags",
	find_notes_by_tag,
	{ desc = "Fuzzy-find notes (tag,name,section)" }
)
vim.api.nvim_create_user_command(
	"NotesFindFile",
	find_notes_by_name,
	{ desc = "Fuzzy-find notes (name)" }
)
vim.api.nvim_create_user_command(
	"NotesFindGrep",
	find_notes_by_grep,
	{ desc = "Fuzzy-find notes (grep content)" }
)
vim.api.nvim_create_user_command("NotesOpen", open_notes, { desc = "Open notes" })

vim.keymap.set("n", "<Leader>nn", vim.cmd.NotesFindTags, { desc = "Fuzzy-find notes" })
vim.keymap.set("n", "<Leader>nf", vim.cmd.NotesFindFile, { desc = "Fuzzy-find notes file name" })
vim.keymap.set("n", "<Leader>ng", vim.cmd.NotesFindGrep, { desc = "Fuzzy-find notes content" })

generate_tags()