Improve denote--file-with-temp-buffer macro to also check unsaved buffers

This gives us the opportunity to insert the contents of a Denote
buffer that has not been saved yet, such as those that are generated
right after we invoke the 'denote' command.
This commit is contained in:
Protesilaos Stavrou 2023-10-14 09:06:53 +03:00
parent b954ade554
commit c36550139c
No known key found for this signature in database
GPG key ID: 99BD6459CD5CA3EA

View file

@ -1370,12 +1370,20 @@ Run `denote-desluggify' on title if the extraction is sucessful."
(denote-desluggify title)
(file-name-base file)))
(defun denote--file-with-temp-buffer-1 (file)
"Return path to FILE or its buffer together with the appropriate function.
Subroutine of `denote--file-with-temp-buffer'."
(when file
(if (file-exists-p file)
(cons #'insert-file-contents buffer-file-name)
(cons #'insert-buffer (get-file-buffer file)))))
(defmacro denote--file-with-temp-buffer (file &rest body)
"If FILE exists, insert its contents in a temp buffer and call BODY."
(declare (indent 1))
`(when (file-exists-p ,file)
`(when-let ((file-and-function (denote--file-with-temp-buffer-1 ,file)))
(with-temp-buffer
(insert-file-contents ,file)
(funcall (car file-and-function) (cdr file-and-function))
(goto-char (point-min))
,@body)))