branch: externals/denote
commit b945fed0278e41ec3728c1c580dfb4810769e452
Author: Protesilaos Stavrou <[email protected]>
Commit: Protesilaos Stavrou <[email protected]>

    Add commands denote-rename-{add,remove}-signature
---
 README.org | 28 ++++++++++++++++++++++++
 denote.el  | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 101 insertions(+)

diff --git a/README.org b/README.org
index cfd9ef4b4c..45de15e847 100644
--- a/README.org
+++ b/README.org
@@ -1538,6 +1538,34 @@ a final step after carrying out their task.
 Aliases for these commands are: ~denote-rename-add-keywords~ and
 ~denote-rename-remove-keywords~.
 
+** Rename a file by adding or removing a signature interactively
+:PROPERTIES:
+:CUSTOM_ID: h:b08a350f-b269-47ed-8c2a-b8ecf1b63c7f
+:END:
+
+[ Part of {{{development-version}}}. ]
+
+#+findex: denote-rename-add-signature
+#+findex: denote-rename-remove-signature
+The commands ~denote-rename-add-signature~ and
+~denote-rename-remove-signature~ streamline the process of
+interactively adding or removing a signature from a given file 
([[#h:4e9c7512-84dc-4dfb-9fa9-e15d51178e5d][The file-naming scheme]]).
+
+The ~denote-rename-add-signature~ prompts for a file and a signature. The
+default value for the file prompt is the file of the currently open
+buffer or the file-at-point in a Dired buffer. The signature is an
+ordinary string, defaulting to the selected file's signature, if any.
+
+The ~denote-rename-remove-signature~ uses the same file prompt as
+above. It performs its action only if the selected file has a
+signature. Otherwise, it does nothing.
+
+Both commands ask for confirmation before carrying out their action.
+They do so unless the user option ~denote-rename-no-confirm~ is set to
+a non-nil value ([[#h:a2ae9090-c49e-4b32-bcf5-eb8944241fd7][The 
~denote-rename-no-confirm~ option]]). They also
+both take care to reload any Dired buffers and run the
+~denote-after-rename-file-hook~ as a final step.
+
 ** Faces used by rename commands
 :PROPERTIES:
 :CUSTOM_ID: h:ab3f355a-f763-43ae-a4c9-179d2d9265a5
diff --git a/denote.el b/denote.el
index 874d9296c2..e7651e5352 100644
--- a/denote.el
+++ b/denote.el
@@ -3137,6 +3137,79 @@ Run `denote-after-rename-file-hook' as a final step."
 (defalias 'denote-rename-remove-keywords 'denote-keywords-remove
   "Alias for `denote-keywords-remove'.")
 
+;;;;;; Interactively add or remove file name signature
+
+;;;###autoload
+(defun denote-rename-add-signature (file signature)
+  "Add to FILE name the SIGNATURE.
+In interactive use, prompt for FILE, defaulting either to the current
+buffer's file or the one at point in a Dired buffer.  Also prompt for
+SIGNATURE, using the existing one, if any, as the initial value.
+
+When called from Lisp, FILE is a string pointing to a file system path
+and SIGNATURE is a string.
+
+Ask for confirmation before renaming the file to include the new
+signature.  Do it unless the user option `denote-rename-no-confirm' is
+set to a non-nil value.
+
+Once the operation is done, reload any Dired buffers and run the
+`denote-after-rename-file-hook'.
+
+Also see `denote-rename-remove-signature'."
+  (interactive
+   (let* ((file (denote--rename-dired-file-or-prompt))
+          (file-in-prompt (propertize (file-relative-name file) 'face 
'denote-faces-prompt-current-name)))
+     (list
+      file
+      (denote-signature-prompt
+       (or (denote-retrieve-filename-signature file) "")
+       (format "Rename `%s' with signature (empty to remove)" 
file-in-prompt)))))
+  (let* ((type (denote-filetype-heuristics file))
+         (title (denote--retrieve-title-or-filename file type))
+         (keywords-string (denote-retrieve-filename-keywords file))
+         (keywords (when keywords-string (split-string keywords-string "_" 
:omit-nulls "_")))
+         (dir (file-name-directory file))
+         (id (or (denote-retrieve-filename-identifier file)
+                 (denote-create-unique-file-identifier file 
(denote--get-all-used-ids))))
+         (extension (denote-get-file-extension file))
+         (new-name (denote-format-file-name dir id keywords title extension 
signature)))
+    (when (or denote-rename-no-confirm (denote-rename-file-prompt file 
new-name))
+      (denote-rename-file-and-buffer file new-name)
+      (denote-update-dired-buffers)
+      (run-hooks 'denote-after-rename-file-hook))))
+
+;;;###autoload
+(defun denote-rename-remove-signature (file)
+  "Remove the signature of FILE.
+In interactive use, prompt for FILE, defaulting either to the current
+buffer's file or the one at point in a Dired buffer.  When called from
+Lisp, FILE is a string pointing to a file system path.
+
+Ask for confirmation before renaming the file to remove its signature.
+Do it unless the user option `denote-rename-no-confirm' is set to a
+non-nil value.
+
+Once the operation is done, reload any Dired buffers and run the
+`denote-after-rename-file-hook'.
+
+Also see `denote-rename-add-signature'."
+  (interactive (list (denote--rename-dired-file-or-prompt)))
+  (when (denote-retrieve-filename-signature file)
+    (let* ((type (denote-filetype-heuristics file))
+           (title (denote--retrieve-title-or-filename file type))
+           (keywords-string (denote-retrieve-filename-keywords file))
+           (keywords (when keywords-string (split-string keywords-string "_" 
:omit-nulls "_")))
+           (dir (file-name-directory file))
+           (id (or (denote-retrieve-filename-identifier file)
+                   (denote-create-unique-file-identifier file 
(denote--get-all-used-ids))))
+           (extension (denote-get-file-extension file))
+           (new-name (denote-format-file-name dir id keywords title extension 
nil)))
+      (when (or denote-rename-no-confirm (denote-rename-file-prompt file 
new-name))
+        (denote-rename-file-and-buffer file new-name)
+        (denote-update-dired-buffers)
+        (run-hooks 'denote-after-rename-file-hook)))))
+
 ;;;;; Creation of front matter
 
 ;;;###autoload

Reply via email to