branch: externals/transient
commit 07a104aa73b5d5e3168ba6e202ebfb60188af6ed
Author: Jonas Bernoulli <jo...@bernoul.li>
Commit: Jonas Bernoulli <jo...@bernoul.li>

    Support automatically remembering the prefix value
    
    Closes #287.
---
 docs/transient.org  | 30 ++++++++++++++++++++++++++++++
 docs/transient.texi | 39 +++++++++++++++++++++++++++++++++++++++
 lisp/transient.el   | 23 ++++++++++++++++++++++-
 3 files changed, 91 insertions(+), 1 deletion(-)

diff --git a/docs/transient.org b/docs/transient.org
index 9d1d6834bb..e1dd7261c6 100644
--- a/docs/transient.org
+++ b/docs/transient.org
@@ -2374,6 +2374,36 @@ functions use ~describe-function~.
   then this slot has to be set to the same value for all of them.  You
   probably don't want that.
 
+- ~remember-value~ When a suffix command is invoked, which can consume
+  the prefix's value (which depends on the suffix slot ~transient~ and
+  the prefix slots ~transient-suffix~ and ~transient-non-suffix~), then
+  the value is automatically pushed to the prefix's value history.
+
+  This slot allows additionally setting or even saving the value, so
+  that it becomes the initial value when the menu is invoked again.
+
+  Beside ~nil~, the value can be one of these symbols:
+
+  - ~export~ Set the value when it is exported.  That is the time when
+    the value would ordinarily just be pushed to the history stack.
+
+  - ~exit~ Set the value when the menu is exited, except when that is
+    done using a command whose sole purpose is to quit the menu.
+
+  - ~quit~ Set the value when the menu is quit, using a command whose
+    sole purpose is to do so.
+
+  The value can also be a list of one or more of these symbols and
+  optionally also the symbol ~save~.
+
+  - ~save~ Instead of merely setting the value, save it, so that it will
+    be used in future Emacs sessions.  At least one other symbol has
+    to be used together with this.
+
+  The value can also be a (quoted) variable, whose value is a list of
+  symbols as described above.  Ideally an option should be used, since
+  not all users will find the automatic saving of the value desirable.
+
 - ~incompatible~ A list of lists.  Each sub-list specifies a set of
   mutually exclusive arguments.  Enabling one of these arguments
   causes the others to be disabled.  An argument may appear in
diff --git a/docs/transient.texi b/docs/transient.texi
index 280872fbb3..f74f438981 100644
--- a/docs/transient.texi
+++ b/docs/transient.texi
@@ -2633,6 +2633,45 @@ primary @code{transient-init-value} method is called 
instead.
 then this slot has to be set to the same value for all of them.  You
 probably don't want that.
 
+@item
+@code{remember-value} When a suffix command is invoked, which can consume
+the prefix's value (which depends on the suffix slot @code{transient} and
+the prefix slots @code{transient-suffix} and @code{transient-non-suffix}), then
+the value is automatically pushed to the prefix's value history.
+
+This slot allows additionally setting or even saving the value, so
+that it becomes the initial value when the menu is invoked again.
+
+Beside @code{nil}, the value can be one of these symbols:
+
+@itemize
+@item
+@code{export} Set the value when it is exported.  That is the time when
+the value would ordinarily just be pushed to the history stack.
+
+@item
+@code{exit} Set the value when the menu is exited, except when that is
+done using a command whose sole purpose is to quit the menu.
+
+@item
+@code{quit} Set the value when the menu is quit, using a command whose
+sole purpose is to do so.
+@end itemize
+
+The value can also be a list of one or more of these symbols and
+optionally also the symbol @code{save}.
+
+@itemize
+@item
+@code{save} Instead of merely setting the value, save it, so that it will
+be used in future Emacs sessions.  At least one other symbol has
+to be used together with this.
+@end itemize
+
+The value can also be a (quoted) variable, whose value is a list of
+symbols as described above.  Ideally an option should be used, since
+not all users will find the automatic saving of the value desirable.
+
 @item
 @code{incompatible} A list of lists.  Each sub-list specifies a set of
 mutually exclusive arguments.  Enabling one of these arguments
diff --git a/lisp/transient.el b/lisp/transient.el
index e5539d04cf..7265b4a0ee 100644
--- a/lisp/transient.el
+++ b/lisp/transient.el
@@ -808,6 +808,7 @@ If `transient-save-history' is nil, then do nothing."
    (transient-non-suffix :initarg :transient-non-suffix :initform nil)
    (transient-switch-frame :initarg :transient-switch-frame)
    (refresh-suffixes     :initarg :refresh-suffixes     :initform nil)
+   (remember-value       :initarg :remember-value       :initform nil)
    (environment          :initarg :environment          :initform nil)
    (incompatible         :initarg :incompatible         :initform nil)
    (suffix-description   :initarg :suffix-description)
@@ -2738,6 +2739,7 @@ value.  Otherwise return CHILDREN as is.")
       (let ((exitp (eq (transient--call-pre-command) transient--exit)))
         (transient--wrap-command)
         (when exitp
+          (transient--maybe-set-value 'exit)
           (transient--pre-exit)))))))
 
 (defun transient--pre-exit ()
@@ -2768,7 +2770,8 @@ value.  Otherwise return CHILDREN as is.")
   (setq transient-current-prefix transient--prefix)
   (setq transient-current-command (oref transient--prefix command))
   (setq transient-current-suffixes transient--suffixes)
-  (transient--history-push transient--prefix))
+  (unless (transient--maybe-set-value 'export)
+    (transient--history-push transient--prefix)))
 
 (defun transient--suspend-override (&optional nohide)
   (transient--debug 'suspend-override)
@@ -3994,6 +3997,24 @@ See also `transient-prefix-set'.")
     (oset (oref obj prototype) value value)
     (transient--history-push obj value)))
 
+(defun transient--maybe-set-value (event)
+  "Maybe set the value, subject to EVENT and the `remember-value' slot."
+  (let* ((event (if (and (eq event 'exit)
+                         (memq this-command transient--quit-commands))
+                    'quit
+                  event))
+         (spec (oref transient--prefix remember-value))
+         (spec (cond ((listp spec) spec)
+                     ((memq spec '(export exit quit))
+                      (list spec))
+                     ((boundp spec)
+                      (symbol-value spec)))))
+    (and (memq event spec)
+         (prog1 t
+           (if (memq 'save spec)
+               (transient-save-value transient--prefix)
+             (transient-set-value transient--prefix))))))
+
 ;;;; Save
 
 (cl-defgeneric transient-save-value (obj)

Reply via email to