Skip to content

Commit

Permalink
Support using a function as the value of an infix's choices slot
Browse files Browse the repository at this point in the history
  • Loading branch information
tarsius committed Oct 19, 2023
1 parent ffa6c1c commit 0f0b1f6
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lisp/transient.el
Original file line number Diff line number Diff line change
Expand Up @@ -2936,7 +2936,7 @@ limited number of possible values should you replace this with a
simple method that does not handle history. (E.g., for a command
line switch the only possible values are \"use it\" and \"don't use
it\", in which case it is pointless to preserve history.)"
(with-slots (value multi-value always-read allow-empty choices) obj
(with-slots (value multi-value always-read allow-empty) obj
(if (and value
(not multi-value)
(not always-read)
Expand All @@ -2958,6 +2958,7 @@ it\", in which case it is pointless to preserve history.)"
(history (if initial-input
(cons 'transient--history 1)
'transient--history))
(choices (transient-infix-choices obj))
(value
(cond
(reader (funcall reader prompt initial-input history))
Expand Down Expand Up @@ -2988,7 +2989,7 @@ it\", in which case it is pointless to preserve history.)"
"Cycle through the mutually exclusive switches.
The last value is \"don't use any of these switches\"."
(let ((choices (mapcar (apply-partially #'format (oref obj argument-format))
(oref obj choices))))
(transient-infix-choices obj))))
(if-let ((value (oref obj value)))
(cadr (member value choices))
(car choices))))
Expand Down Expand Up @@ -3245,6 +3246,12 @@ value of the variable. I.e., this is a side-effect and does
not contribute to the value of the transient."
nil)

(cl-defmethod transient-infix-choices ((obj transient-infix))
(let ((choices (oref obj choices)))
(if (functionp choices)
(oset obj choices (funcall choices obj))
choices)))

;;;; Utilities

(defun transient-arg-value (arg args)
Expand Down Expand Up @@ -3686,7 +3693,7 @@ If the OBJ's `key' is currently unreachable, then apply the face
(propertize argument 'face 'transient-inactive-value))))

(cl-defmethod transient-format-value ((obj transient-switches))
(with-slots (value argument-format choices) obj
(with-slots (value argument-format) obj
(format (propertize argument-format
'face (if value
'transient-value
Expand All @@ -3699,7 +3706,7 @@ If the OBJ's `key' is currently unreachable, then apply the face
(if (equal (format argument-format choice) value)
'transient-value
'transient-inactive-value)))
choices
(transient-infix-choices obj)
(propertize "|" 'face 'transient-inactive-value))
(propertize "]" 'face 'transient-inactive-value)))))

Expand Down

0 comments on commit 0f0b1f6

Please sign in to comment.