emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/denote db7df4a948 1/3: Add denote-kill-buffers


From: ELPA Syncer
Subject: [elpa] externals/denote db7df4a948 1/3: Add denote-kill-buffers
Date: Tue, 3 Sep 2024 03:58:02 -0400 (EDT)

branch: externals/denote
commit db7df4a94831044cd5b1447df8ed1704c5d6ee9e
Author: Jean-Philippe Gagné Guay <jeanphilippe150@gmail.com>
Commit: Jean-Philippe Gagné Guay <jeanphilippe150@gmail.com>

    Add denote-kill-buffers
---
 denote.el | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 83 insertions(+), 20 deletions(-)

diff --git a/denote.el b/denote.el
index a09e800a69..543f6ac76c 100644
--- a/denote.el
+++ b/denote.el
@@ -157,11 +157,39 @@ commands (`denote-rename-file' and related).
 If this user option is set to a non-nil value, such buffers are
 saved automatically.  The assumption is that the user who opts in
 to this feature is familiar with the `denote-rename-file'
-operation (or related) and knows it is reliable."
+operation (or related) and knows it is reliable.  Data loss may
+occur if the file is modified externally."
   :group 'denote
   :package-version '(denote . "3.0.0")
   :type 'boolean)
 
+(defcustom denote-kill-buffers nil
+  "Control whether creation or renaming commands kill their buffer.
+
+The default behaviour of creation or renaming commands such as
+`denote' or `denote-rename-file' is to not kill the buffer they
+create or modify at the end of their operation.
+
+If this user option is nil (the default), buffers affected by a
+creation or renaming command are not automatically killed.  If
+set to `on-creation', new notes are automatically kill.  If set
+to `on-rename', renamed notes are automatically killed.  If set
+to t, new and renamed notes are killed.
+
+If a buffer is killed, it is also saved, as if
+`denote-save-buffers' were t. See its documentation.
+
+In all cases, if a note was already visited at the beginning of
+an operation (such as a renaming command), it is NOT
+automatically killed."
+  :group 'denote
+  :package-version '(denote . "3.1.0")
+  :type '(choice
+          (const :tag "Do not kill buffers" nil)
+          (const :tag "Kill after creation" on-creation)
+          (const :tag "Kill after rename" on-rename)
+          (const :tag "Kill after creation and rename" t)))
+
 ;;;###autoload (put 'denote-known-keywords 'safe-local-variable #'listp)
 (defcustom denote-known-keywords
   '("emacs" "philosophy" "politics" "economics")
@@ -2192,12 +2220,36 @@ The path of the newly created file is returned."
         (path))
     (if in-background
         (save-window-excursion
-          (call-interactively command)
-          (setq path (buffer-file-name)))
-      (call-interactively command)
-      (setq path (buffer-file-name)))
+          (setq path (call-interactively command)))
+      (setq path (call-interactively command)))
     path))
 
+(defun denote--handle-save-and-kill-buffer (mode file initial-state)
+  "Save and kill buffer of FILE according to MODE and INITIAL-STATE.
+
+The values of `denote-save-buffers' and `denote-kill-buffers' are
+used to decide whether to save and/or kill the buffer visiting
+FILE.
+
+MODE is one of the symbols `creation' or `rename'.
+
+INITIAL-STATE is nil or one of the following symbols:
+`not-visited', `visited'.  If a buffer was already visited at the
+beginning of a rename operation, it is NOT killed automatically.
+
+If a buffer needs to be killed, it is also automatically saved,
+no matter the value of `denote-save-buffers'."
+  (let* ((do-kill-buffer (and (not (eq initial-state 'visited))
+                              (or (eq denote-kill-buffers t)
+                                  (and (eq mode 'creation)
+                                       (eq denote-kill-buffers 'on-creation))
+                                  (and (eq mode 'rename)
+                                       (eq denote-kill-buffers 'on-rename)))))
+         (do-save-buffer (or do-kill-buffer denote-save-buffers)))
+    (when-let ((buffer (find-buffer-visiting file)))
+      (when do-save-buffer (with-current-buffer buffer (save-buffer)))
+      (when do-kill-buffer (kill-buffer buffer)))))
+
 (defvar denote-use-title nil
   "The title to be used in a note creation command.
 See the documentation of `denote' for acceptable values.  This variable
@@ -2358,7 +2410,7 @@ When called from Lisp, all arguments are optional.
                 (denote--creation-prepare-note-data title keywords file-type 
directory date template signature))
                (id (denote--find-first-unused-id (denote-get-identifier date)))
                (note-path (denote--prepare-note title keywords date id 
directory file-type template signature)))
-    (when denote-save-buffers (save-buffer))
+    (denote--handle-save-and-kill-buffer 'creation note-path nil)
     (denote--keywords-add-to-history keywords)
     (run-hooks 'denote-after-new-note-hook)
     note-path))
@@ -2629,7 +2681,11 @@ has the `signature' prompt appended to its existing 
prompts."
 
 ;;;###autoload
 (defun denote-region ()
-  "Call `denote' and insert therein the text of the active region."
+  "Call `denote' and insert therein the text of the active region.
+
+Note that, currently, `denote-save-buffers' and
+`denote-kill-buffers' are NOT respected.  The buffer is not
+saved or killed at the end of `denote-region'."
   (declare (interactive-only t))
   (interactive)
   (if-let (((region-active-p))
@@ -2637,7 +2693,11 @@ has the `signature' prompt appended to its existing 
prompts."
            ;; the moment `insert' is called.
            (text (buffer-substring-no-properties (region-beginning) 
(region-end))))
       (progn
-        (let ((denote-ignore-region-in-denote-command t))
+        (let ((denote-ignore-region-in-denote-command t)
+              ;; FIXME: Find a way to insert the region before the buffer is
+              ;; saved/killed by the creation command.
+              (denote-save-buffers nil)
+              (denote-kill-buffers nil))
           (call-interactively 'denote))
         (push-mark (point))
         (insert text)
@@ -2899,8 +2959,10 @@ for anything.  It is meant to be combined with
 `denote--rename-get-file-info-from-prompts-or-existing' to create
 a renaming command.
 
-Respect `denote-rename-confirmations' and `denote-save-buffers'."
-  (let* ((file-type (denote-filetype-heuristics file))
+Respect `denote-rename-confirmations', `denote-save-buffers' and
+`denote-kill-buffers'."
+  (let* ((initial-state (if (find-buffer-visiting file) 'visited 'not-visited))
+         (file-type (denote-filetype-heuristics file))
          (current-title (or (denote-retrieve-front-matter-title-value file 
file-type) ""))
          (current-keywords (denote-extract-keywords-from-path file))
          (keywords (denote-keywords-sort keywords))
@@ -2930,9 +2992,7 @@ Respect `denote-rename-confirmations' and 
`denote-save-buffers'."
       ;; files on rename. This could be a distinct command.
       (when denote--used-ids
         (puthash id t denote--used-ids))
-      (when denote-save-buffers
-        (with-current-buffer (find-file-noselect new-name)
-          (save-buffer)))
+      (denote--handle-save-and-kill-buffer 'rename new-name initial-state)
       (run-hooks 'denote-after-rename-file-hook))
     new-name))
 
@@ -3062,6 +3122,9 @@ file types (per `denote-file-type'), add front matter to 
the top
 of it and leave the buffer unsaved for further inspection.  Save
 the buffer if `denote-save-buffers' is non-nil.
 
+When `denote-kill-buffers' is t or `on-rename', kill the buffer
+if it was not already being visited before the rename operation.
+
 For the front matter of each file type, refer to the variables:
 
 - `denote-org-front-matter'
@@ -3271,9 +3334,10 @@ the Dired file at point, which is subsequently inspected 
for the
 requisite front matter.  It is thus implied that the FILE has a file
 type that is supported by Denote, per `denote-file-type'.
 
-The values of `denote-rename-confirmations' and `denote-save-buffers'
-are respected.  Though there is no prompt to confirm the rewrite of the
-front matter, since this is already done by the user.
+The values of `denote-rename-confirmations',
+`denote-save-buffers' and `denote-kill-buffers' are respected.
+Though there is no prompt to confirm the rewrite of the front
+matter, since this is already done by the user.
 
 The identifier of the file, if any, is never modified even if it
 is edited in the front matter: Denote considers the file name to
@@ -3398,7 +3462,8 @@ Construct the file name in accordance with the user option
    (list
     (denote--rename-dired-file-or-current-file-or-prompt)
     (denote--valid-file-type (or (denote-file-type-prompt) denote-file-type))))
-  (let* ((dir (file-name-directory file))
+  (let* ((initial-state (if (find-buffer-visiting file) 'visited 'not-visited))
+         (dir (file-name-directory file))
          (old-file-type (denote-filetype-heuristics file))
          (id (or (denote-retrieve-filename-identifier file) ""))
          (title (or (denote-retrieve-title-or-filename file old-file-type) ""))
@@ -3413,9 +3478,7 @@ Construct the file name in accordance with the user option
       (when (and (denote-file-is-writable-and-supported-p new-name)
                  (denote-add-front-matter-prompt new-name))
         (denote--add-front-matter new-name title keywords id new-file-type)
-        (when denote-save-buffers
-          (with-current-buffer (find-file-noselect new-name)
-            (save-buffer)))))))
+        (denote--handle-save-and-kill-buffer 'rename new-name 
initial-state)))))
 
 ;;;; The Denote faces
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]