diff options
| author | Jonas Bernoulli <jonas@bernoul.li> | 2025-05-26 19:28:35 +0200 |
|---|---|---|
| committer | Jonas Bernoulli <jonas@bernoul.li> | 2025-05-26 19:28:35 +0200 |
| commit | 81727bacfce91a119c840d08ac6da92cb6af91c4 (patch) | |
| tree | c3cfa5c7d42016a8ae38d66e7bf8826beea7b617 /lisp | |
| parent | 56f9bb96b9e376ed1c80676c9898f15b70e0d1f0 (diff) | |
transient-cons-option: New suffix class for non-cmdline options
The existing `transient-option' class is suitable for options passed to
commands. A suffix that uses that class may represent an argument such
as "--option". In the list returned by `transient-args', that argument
and its value, "value", is represented as one element "--option=value".
For "options" that are not passed to a command that is not appropriate.
A caller that wants to extract the value of such an option would have
to parse that string. `transient-arg-value' can help with that, but it
always returns a string (or nil).
The new `transient-cons-option' class provides a more direct way to deal
with "non-command-line options", by representing the key value pair
using a cons-cell (ARGUMENT . VALUE), not just internally, but in
particular when handing it of to a caller. VALUE can have any printable
type and ARGUMENT can have any printable type, expect string.
We cannot use the term "key" because that is already used to the key
binding for the command. Also avoid using "property" because that
implies a property list, while the value returned by `transient-args'
is an alist when all infixes use the `transient-cons-options' class.
Sticking to "argument" also has the advantage, that we don't have to
implement or generalize a dozen methods.
Diffstat (limited to 'lisp')
| -rw-r--r-- | lisp/transient.el | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/lisp/transient.el b/lisp/transient.el index b7359c5..4852a34 100644 --- a/lisp/transient.el +++ b/lisp/transient.el @@ -2540,7 +2540,9 @@ value. Otherwise return CHILDREN as is.") (if (transient-switches--eieio-childp obj) (cl-call-next-method obj) (when-let* (((not (slot-boundp obj 'shortarg))) - (shortarg (transient--derive-shortarg (oref obj argument)))) + (argument (oref obj argument)) + ((stringp argument)) + (shortarg (transient--derive-shortarg argument))) (oset obj shortarg shortarg)) (unless (slot-boundp obj 'key) (if (slot-boundp obj 'shortarg) @@ -4682,7 +4684,7 @@ apply the face `transient-unreachable' to the complete string." 'transient-inactive-argument))) (cl-defmethod transient-format-value ((obj transient-option)) - (let ((argument (oref obj argument))) + (let ((argument (prin1-to-string (oref obj argument) t))) (if-let ((value (oref obj value))) (pcase-exhaustive (oref obj multi-value) ('nil @@ -5312,6 +5314,29 @@ as stand-in for elements of exhausted lists." (defun transient-lisp-variable--reader (prompt initial-input _history) (read--expression prompt initial-input)) +;;;; `transient-cons-option' + +(defclass transient-cons-option (transient-option) + ((format :initform " %k %d: %v")) + "[Experimental] Class used for unencoded key-value pairs.") + +(cl-defmethod transient-infix-value ((obj transient-cons-option)) + "Return ARGUMENT and VALUE as a cons-cell or nil if the latter is nil." + (and-let* ((value (oref obj value))) + (cons (oref obj argument) value))) + +(cl-defmethod transient-format-description ((obj transient-cons-option)) + (or (oref obj description) + (let ((description (prin1-to-string (oref obj argument) t))) + (if (string-prefix-p ":" description) + (substring description 1) + description)))) + +(cl-defmethod transient-format-value ((obj transient-cons-option)) + (let ((value (oref obj value))) + (propertize (prin1-to-string value t) 'face + (if value 'transient-value 'transient-inactive-value)))) + ;;; _ (provide 'transient) ;; Local Variables: |
