Fix front-matter retrieving when buffer is unsaved or file missing

This addresses a regression where
'denote-rename-file-using-front-matter' would fail to recognize changes
in an unsaved buffer, or fail entirely if the file had not yet been
created on disk (as described in
https://github.com/protesilaos/denote/pull/670).

The logic now consistently prioritizes the content of an open buffer
over the file on disk. Previously, the code would revert to reading the
file if the buffer was marked as modified; however, since we cannot
reliably detect if a modification affects the front-matter, it is safe
to always use the live buffer state.

* (denote--file-with-temp-buffer-subr): Remove.
(denote--file-with-temp-buffer): Refactor to prioritize buffer content
and remove the dependency on the internal subr.

Fixes: https://github.com/protesilaos/denote/pull/670
This commit is contained in:
duli 2026-01-17 19:28:58 +08:00
parent 3563d2b3fe
commit 0f71f126cc
No known key found for this signature in database
GPG key ID: 0C378F00219F31FD

View file

@ -2624,30 +2624,17 @@ Also see `denote-extract-keywords-from-path' (alias
(when (string-match denote-title-regexp filename)
(match-string 1 filename))))
(defun denote--file-with-temp-buffer-subr (file)
"Return path to FILE or its buffer together with the appropriate function.
Subroutine of `denote--file-with-temp-buffer'."
(let* ((buffer (get-file-buffer file))
(file-exists (file-exists-p file))
(buffer-modified (buffer-modified-p buffer)))
(cond
((and file-exists
buffer
(not buffer-modified))
(cons #'insert-buffer buffer))
((and file-exists
(or (null buffer) buffer-modified))
(cons #'insert-file-contents file))
;; (t
;; (error "Cannot find anything about file `%s'" file))
)))
(defmacro denote--file-with-temp-buffer (file &rest body)
"If FILE exists, insert its contents in a temp buffer and call BODY."
"If a buffer is visiting FILE, insert its contents into a temporary
buffer. Otherwise, insert the contents of FILE. Then call BODY."
(declare (indent 1))
`(when-let* ((file-and-function (denote--file-with-temp-buffer-subr ,file)))
`(let* ((buffer (get-file-buffer ,file))
(file-exist (file-exists-p ,file)))
(with-temp-buffer
(funcall (car file-and-function) (cdr file-and-function))
(cond
(buffer (insert-buffer buffer))
(file-exist (insert-file-contents ,file))
(t (error "Cannot find anything about file `%s'" ,file)))
(goto-char (point-min))
,@body)))