summaryrefslogtreecommitdiffstats
path: root/plugin/50-notes.lua
diff options
context:
space:
mode:
authorThomas Vanbesien <tvanbesi@proton.me>2026-06-24 13:34:15 +0200
committerThomas Vanbesien <tvanbesi@proton.me>2026-06-24 17:02:51 +0200
commit2c95e80bdf74b489cb62ac15027fff8c1eb1aa3e (patch)
treefe9bb4581cd686b028208362378e46735ebc1553 /plugin/50-notes.lua
parentc901526977bae4efc478dd9084349c29898fc9ab (diff)
downloadnvim-config-2c95e80bdf74b489cb62ac15027fff8c1eb1aa3e.tar.gz
nvim-config-2c95e80bdf74b489cb62ac15027fff8c1eb1aa3e.zip
misc(nvim): notes rename robustness and <Leader>no mapping
- rename_note discovers the open buffer itself (drop buf param) - keep the .md extension on rename even if the user omits it - add <Leader>no to open notes; use lcd instead of cd
Diffstat (limited to 'plugin/50-notes.lua')
-rw-r--r--plugin/50-notes.lua44
1 files changed, 31 insertions, 13 deletions
diff --git a/plugin/50-notes.lua b/plugin/50-notes.lua
index 3172a02..30c655c 100644
--- a/plugin/50-notes.lua
+++ b/plugin/50-notes.lua
@@ -16,10 +16,11 @@
-- `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
+-- `NotesOpen`: move to the notes directory and open index
--
-- Keymaps:
--- `<Leader>nn`: fuzzy-find notes
+-- `<Leader>no`: open notes in current window
+-- `<Leader>nn`: fuzzy-find notes by tags
-- `<Leader>nf`: fuzzy-find notes by file name
-- `<Leader>ng`: fuzzy-find notes by content
-- `<Tab>` / `<S-Tab>` (in notes buffers): go to next/previous URL or wiki-link
@@ -229,6 +230,20 @@ local function move_file(old_path, new_path)
return vim.uv.fs_rename(old_path, new_path)
end
+-- Returns the loaded buffer editing `path` (normalized match), or nil if none is open.
+local function buf_for_path(path)
+ path = vim.fs.normalize(path)
+ for _, b in ipairs(vim.api.nvim_list_bufs()) do
+ if
+ vim.api.nvim_buf_is_loaded(b)
+ and vim.fs.normalize(vim.api.nvim_buf_get_name(b)) == path
+ then
+ return b
+ end
+ end
+ return nil
+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)
@@ -242,10 +257,10 @@ local function rename_buffer(buf, new_path)
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 the note is open in a buffer, that buffer 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)
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
@@ -263,6 +278,8 @@ local function rename_note(old_path, new_path, buf)
vim.notify("rename failed: " .. tostring(err), vim.log.levels.ERROR)
return
end
+ -- Moving on disk doesn't rename the buffer, so it's still found under `old_path` here.
+ local buf = buf_for_path(old_path)
if buf then
rename_buffer(buf, new_path)
end
@@ -282,12 +299,12 @@ local function rename_note(old_path, new_path, buf)
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)
+-- Rename the current note to `new_rel` (relative to the notes dir, `.md` optional).
+local function rename_current_note(new_rel)
+ local old_path = vim.api.nvim_buf_get_name(0)
+ -- Tolerate a missing (or already-present) `.md` so the note never loses its extension.
+ new_rel = (new_rel:gsub("%.md$", "")) .. ".md"
+ rename_note(old_path, vim.fs.joinpath(notes_dir, new_rel))
end
-- `:NotesRename` implementation: take the new note name from the command argument, or prompt for it
@@ -451,7 +468,7 @@ end
-- Enters the notes dir and opens its index, creating it with a `# Index` header on first use.
local function open_notes()
- vim.cmd.cd(notes_dir)
+ vim.cmd.lcd(notes_dir)
local index = notes_dir .. "/index.md"
if not vim.uv.fs_stat(index) then
local f = assert(io.open(index, "w"))
@@ -528,6 +545,7 @@ vim.api.nvim_create_user_command(
)
vim.api.nvim_create_user_command("NotesOpen", open_notes, { desc = "Open notes" })
+vim.keymap.set("n", "<Leader>no", vim.cmd.NotesOpen, { desc = "Open notes in current window" })
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" })