Skip to content

Commit

Permalink
transient-preset: New command
Browse files Browse the repository at this point in the history
Example usage:

  (transient-define-prefix demo ()
    :refresh-suffixes t
    ["Presets"
     ("1" transient-preset := ("--option=111" "--flag"))
     ("2" transient-preset := ("--option=222"))]
    ["Settings"
     ("i" "Set option" "--option=")
     ("f" "Set flag" "--flag")]
    ["Actions"
     (transient-echo-arguments)])

Closes #183.
  • Loading branch information
tarsius committed Jun 22, 2024
1 parent 8db5f0f commit 06a87bd
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lisp/transient.el
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,11 @@ Technically a suffix object with no associated command.")
All remaining arguments are treated as files.
They become the value of this argument.")

(defclass transient-value-preset (transient-suffix)
((transient :initform t)
(set :initarg := :initform nil))
"Class used by the `transient-preset' suffix command.")

;;;; Group

(defclass transient-group (transient-child)
Expand Down Expand Up @@ -3093,6 +3098,12 @@ transient is active."
(oset obj value (nth pos hst))
(mapc #'transient-init-value transient--suffixes))))

(transient-define-suffix transient-preset ()
"Put this preset into action."
:class transient-value-preset
(interactive)
(transient-prefix-set (oref (transient-suffix-object) set)))

;;;; Auxiliary

(defun transient-toggle-common ()
Expand Down Expand Up @@ -3974,6 +3985,24 @@ called inside the correct buffer (see `transient--insert-group')
and its value is returned to the caller."
(transient--get-description obj))

(cl-defmethod transient-format-description ((obj transient-value-preset))
(pcase-let* (((eieio description key set) obj)
((eieio value) transient--prefix)
(active (seq-set-equal-p set value)))
(format
"%s %s"
(propertize (or description (format "Preset %s" key))
'face (and active 'transient-argument))
(format (propertize "(%s)" 'face 'transient-delimiter)
(mapconcat (lambda (arg)
(propertize
arg 'face (cond (active 'transient-argument)
((member arg value)
'((:weight demibold)
transient-inactive-argument))
('transient-inactive-argument))))
set " ")))))

(cl-defmethod transient-format-description ((obj transient-group))
"Format the description by calling the next method. If the result
doesn't use the `face' property at all, then apply the face
Expand Down

0 comments on commit 06a87bd

Please sign in to comment.