-
-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
respect and use existing front matter when adding front matter #493
Comments
Do you mean that you do not want to be prompted for the title? Or that you want it to read the title from the front matter instead of from the file name? |
I guess it would be a solution, if I wasn't asked and the title was leaved unchanged. But I think it gives me more optons if the first proposal for a title would be the title from the front matter. |
Thanks to juh for asking about this in issue 493: <#493>.
I just made the change to About having the same via Dired, let me take a look and work on it accordingly. |
If I updated correctly with straight (was using the release before) it inserts the filename part, not the title. |
Maybe straight did something unexpected. Please evaluate the following and then try again: (defun denote-add-front-matter (file title keywords signature)
"Insert front matter at the top of FILE.
When called interactively, FILE is the return value of the
function `buffer-file-name'. FILE is checked to determine
whether it is a note for Denote's purposes.
TITLE is a string. Interactively, it is the user input at the
minibuffer prompt.
KEYWORDS is a list of strings. Interactively, it is the user
input at the minibuffer prompt. This one supports completion for
multiple entries, each separated by the `crm-separator' (normally
a comma).
SIGNATURE is a string. Interactively, it is the user input at the
minibuffer prompt.
The purpose of this command is to help the user generate new
front matter for an existing note (perhaps because the user
deleted the previous one and could not undo the change).
This command does not rename the file (e.g. to update the
keywords). To rename a file by reading its front matter as
input, use `denote-rename-file-using-front-matter'.
Note that this command is useful only for existing Denote notes.
If the user needs to convert a generic text file to a Denote
note, they can use one of the command which first rename the file
to make it comply with our file-naming scheme and then add the
relevant front matter.
[ NOTE: Please check with your minibuffer user interface how to
provide an empty input. The Emacs default setup accepts the
empty minibuffer contents as they are, though popular packages
like `vertico' use the first available completion candidate
instead. For `vertico', the user must either move one up to
select the prompt and then type RET there with empty contents,
or use the command `vertico-exit-input' with empty contents.
That Vertico command is bound to M-RET as of this writing on
2024-02-29 09:24 +0200. ]"
(interactive
(let* ((file buffer-file-name)
(type (denote-filetype-heuristics file))
(default-title (or (denote-retrieve-title-or-filename file type) ""))
(default-keywords (string-join (denote-retrieve-filename-keywords-as-list file) ","))
(default-signature (or (denote-retrieve-filename-signature file) "")))
(list
file
(denote-title-prompt default-title "Add TITLE (empty to ignore)")
(denote-keywords-sort (denote-keywords-prompt "Add KEYWORDS (empty to ignore)" default-keywords))
(denote-signature-prompt default-signature "Add SIGNATURE (empty to ignore)"))))
(when-let* ((denote-file-is-writable-and-supported-p file)
(id (or (denote-retrieve-filename-identifier file) ""))
(date (if (string-empty-p id) nil (date-to-time id)))
(file-type (denote-filetype-heuristics file)))
(denote--add-front-matter file title keywords signature date id file-type))) |
By the way, I am rewriting this command to make it more useful and efficient overall. |
With the code above it works as expected. BTW: denote-rename-file-keywords makes what I want. Missing meta data like identifier, date and keywords is added without touching the title. |
From: juh ***@***.***>
Date: Fri, 13 Dec 2024 01:28:40 -0800
With the code above it works as expected. BTW:
denote-rename-file-keywords makes what I want. Missing meta data like
identifier, date and keywords is added without touching the title.
Fine. Can you please try the latest version of 'denote-add-front-matter'?
I paste it below, just to be sure:
(defun denote-prepend-front-matter (file title keywords signature date id file-type)
"Prepend front matter to FILE.
The TITLE, KEYWORDS, DATE, ID, SIGNATURE, and FILE-TYPE are passed from
the renaming command and are used to construct a new front matter block
if appropriate."
(when-let* ((new-front-matter (denote--format-front-matter title date keywords id signature file-type)))
(with-current-buffer (find-file-noselect file)
(goto-char (point-min))
(insert new-front-matter))))
(defun denote-add-front-matter ()
"Insert front matter at the top of the current file if it is a Denote note.
If front matter exists, fully or in part, rewrite it. Else prepend a
new block to the current file.
To prepare the new front matter, prompt for title, keywords, and
signature. Do it depending on if those are part of the current file.
Skip the prompt for any missing file name component.
At each prompt, use the current value of the given file name component
as the default text in the minibuffer. For the title in particular,
read from the existing front matter to get the human-readable version.
Otherwise, read from the file name.
Do not write the value of the given file name component if the
minibuffer input is empty.
Whatever the case, do not rename the file upon completing the operation.
This is the task of `denote-rename-file' or, more probably for this
case, `denote-rename-file-using-front-matter', among others.
[ NOTE: Please check with your minibuffer user interface how to
provide an empty input. The Emacs default setup accepts the
empty minibuffer contents as they are, though popular packages
like `vertico' use the first available completion candidate
instead. For `vertico', the user must either move one up to
select the prompt and then type RET there with empty contents,
or use the command `vertico-exit-input' with empty contents.
That Vertico command is bound to M-RET as of this writing on
2024-02-29 09:24 +0200. ]"
(declare (advertised-calling-convention nil "3.1.0")
(interactive-only t))
(interactive nil text-mode)
(let* ((file buffer-file-name)
(titlep (denote-retrieve-filename-title file))
(keywordsp (denote-retrieve-filename-keywords file))
(signaturep (denote-retrieve-filename-signature file))
(file-type (denote-filetype-heuristics file))
(default-title (when titlep (or (denote-retrieve-title-value file file-type) titlep "")))
(default-keywords (when keywordsp (string-join (denote-retrieve-filename-keywords-as-list file) ",")))
(default-signature (or signaturep ""))
(title (if titlep
(denote-title-prompt default-title "Add TITLE (empty to ignore)")
""))
(keywords (if keywordsp
(denote-keywords-sort (denote-keywords-prompt "Add KEYWORDS (empty to ignore)" default-keywords))
""))
(signature (if signaturep
(denote-signature-prompt default-signature "Add SIGNATURE (empty to ignore)")
"")))
(when-let* ((denote-file-is-writable-and-supported-p file)
(id (denote-retrieve-filename-identifier file))
(date (date-to-time id)))
(if (denote--file-has-front-matter-p file file-type)
(denote-rewrite-front-matter file title keywords signature date id file-type)
(denote-prepend-front-matter file title keywords signature date id file-type)))))
…--
Protesilaos Stavrou
https://protesilaos.com
|
I am currently working on a silo where front matter is not complete, but the files have eg. titles. denote-add-front-matter does not evaluate the old title to make it the default. So I have to manually reuse the old title after adding front matter. Do I miss something? And is there a way to have this in denote-dired as kind of bulk add frontmatter?
The text was updated successfully, but these errors were encountered: