branch: externals/denote commit 0e75e46b32ca8f31251f170317037ade6fd9a9f7 Author: Protesilaos Stavrou <i...@protesilaos.com> Commit: Protesilaos Stavrou <i...@protesilaos.com>
Improve denote-dired-rename-file It now preserves the identifier, if it exists. So if you want to change the title/keywords in a note's front matter, this command can then be called to also update the file name (maybe there is some way to automate this, but let's leave it for now). --- README.org | 22 +++++++++++++++++----- denote-dired.el | 38 ++++++++++++++++++++++++++++++-------- denote.el | 9 +++++---- 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/README.org b/README.org index 6007a5e22f..4765d39402 100644 --- a/README.org +++ b/README.org @@ -626,7 +626,7 @@ Have more ideas? Something does not work quite right? Areas you wish were more abstract in the code? Please participate in the development process. -* Renaming non-notes +* Renaming files :PROPERTIES: :CUSTOM_ID: h:532e8e2a-9b7d-41c0-8f4b-3c5cbb7d4dca :END: @@ -638,10 +638,22 @@ Denote does not manage such files, it already has all the mechanisms to facilitate the task of renaming them. #+findex: denote-dired-rename-file -To this end, invoke ~denote-dired-rename-file~ when point is over a file -in Dired to rename it. The commaand prompts for a =TITLE= and -=KEYWORDS= the same way the ~denote~ command does it ([[#h:17896c8c-d97a-4faa-abf6-31df99746ca6][Points of entry]]). -It finally asks for confirmation before renaming the file at point. +To this end, we provide the ~denote-dired-rename-file~ command. It has +a two-fold purpose: (i) to change the file name of an existing note +while retaining its identifier and (ii) to write a Denote-style file +name for an item that was not created by ~denote~ or related commands +(such as an image or PDF). + +The ~denote-dired-rename-file~ command will target the file at point if +it finds one in the current Dired buffer. Otherwise it prompts with +minibuffer completion for a file name. It then uses the familiar +prompts for a =TITLE= and =KEYWORDS= the same way the ~denote~ command +does it ([[#h:17896c8c-d97a-4faa-abf6-31df99746ca6][Points of entry]]). As a final step it asks for confirmation +before renaming the file at point, showing a message like: + +#+begin_example +Rename denote-link.el to 20220612T052900--what-the-thing__testing.el? (y or n) +#+end_example The file type extension (e.g. =.pdf=) is read from the underlying file and is preserved in the renaming process. Files that have no extension diff --git a/denote-dired.el b/denote-dired.el index 6e36bfddd7..2598ecae9b 100644 --- a/denote-dired.el +++ b/denote-dired.el @@ -80,22 +80,43 @@ ;;;; Commands +(defun denote-dired--file-name-id (file) + "Return FILE identifier, else provide one." + (if (string-match denote--id-regexp file) + (substring file (match-beginning 0) (match-end 0)) + (format-time-string denote--id))) + ;;;###autoload -(defun denote-dired-rename-file (title keywords) - "Rename file at point to new file with TITLE and KEYWORDS. -This command is intended to complement note-taking, such as by -renaming attachments that the user adds to their notes." +(defun denote-dired-rename-file (file title keywords) + "Rename FILE to include TITLE and KEYWORDS. +If in Dired consider FILE the one at point, else prompt with +completion. + +If FILE has a Denote-style identifier, retain it while updating +the TITLE and KEYWORDS fields. Else create an identifier, +replacing the whole name. + +The file type extension (e.g. .pdf) is read from the underlying +file and is preserved in the renaming process. Files that have +no extension are simply left without one. + +Renaming only occurs relative to the current directory. Files +are not moved between directories. + +This command is intended to (i) rename existing Denote +notes, (ii) complement note-taking, such as by renaming +attachments that the user adds to their notes." (interactive (list + (or (dired-get-filename nil t) (read-file-name "Rename file Denote-style: ")) (denote--title-prompt) (denote--keywords-prompt))) - (let* ((file (dired-get-filename)) - (dir (file-name-directory file)) + (let* ((dir (file-name-directory file)) (old-name (file-name-nondirectory file)) (extension (file-name-extension file t)) (new-name (denote--format-file dir - (format-time-string denote--id) + (denote-dired--file-name-id file) keywords (denote--sluggify title) extension))) @@ -104,7 +125,8 @@ renaming attachments that the user adds to their notes." (propertize old-name 'face 'error) (propertize (file-name-nondirectory new-name) 'face 'success))) (rename-file old-name new-name nil) - (revert-buffer)))) + (when (eq major-mode 'dired-mode) + (revert-buffer))))) ;;;; Extra fontification diff --git a/denote.el b/denote.el index a5d5822bd0..f1bb86434f 100644 --- a/denote.el +++ b/denote.el @@ -436,13 +436,14 @@ TITLE, DATE, KEYWORDS, FILENAME, ID are all strings which are ('text (format denote-text-front-matter title date kw-space id denote-text-front-matter-delimiter)) (_ (format denote-org-front-matter title date kw-space id))))) -(defun denote--path (title keywords) +(defun denote--path (title keywords &optional dir id) "Return path to new file with TITLE and KEYWORDS. -Format current time, else use optional ID." +With optional DIR, use it instead of variable `denote-directory'. +With optional ID, use it else format the current time." (setq denote-last-path (denote--format-file - (file-name-as-directory denote-directory) - (format-time-string denote--id) + (or dir (file-name-as-directory denote-directory)) + (or id (format-time-string denote--id)) (denote--sluggify-keywords keywords) (denote--sluggify title) (denote--file-extension))))