Skip to content

Commit

Permalink
Add support for :inapt* predicates for groups
Browse files Browse the repository at this point in the history
  • Loading branch information
tarsius committed May 9, 2024
1 parent dc967e0 commit 3d395d6
Showing 1 changed file with 41 additions and 30 deletions.
71 changes: 41 additions & 30 deletions lisp/transient.el
Original file line number Diff line number Diff line change
Expand Up @@ -724,24 +724,12 @@ the prototype is stored in the clone's `prototype' slot.")
(if-not-derived
:initarg :if-not-derived
:initform nil
:documentation "Enable if major-mode does not derive from value."))
"Abstract superclass for group and suffix classes.
It is undefined what happens if more than one `if*' predicate
slot is non-nil."
:abstract t)

(defclass transient-suffix (transient-child)
((definition :allocation :class :initform nil)
(key :initarg :key)
(command :initarg :command)
(transient :initarg :transient)
(format :initarg :format :initform " %k %d")
(description :initarg :description :initform nil)
(face :initarg :face :initform nil)
(show-help :initarg :show-help :initform nil)
(inapt-face :initarg :inapt-face :initform 'transient-inapt-suffix)
(inapt :initform nil)
:documentation "Enable if major-mode does not derive from value.")
(inapt
:initform nil)
(inapt-face
:initarg :inapt-face
:initform 'transient-inapt-suffix)
(inapt-if
:initarg :inapt-if
:initform nil
Expand Down Expand Up @@ -774,6 +762,21 @@ slot is non-nil."
:initarg :inapt-if-not-derived
:initform nil
:documentation "Inapt if major-mode does not derive from value."))
"Abstract superclass for group and suffix classes.
It is undefined what happens if more than one `if*' predicate
slot is non-nil."
:abstract t)

(defclass transient-suffix (transient-child)
((definition :allocation :class :initform nil)
(key :initarg :key)
(command :initarg :command)
(transient :initarg :transient)
(format :initarg :format :initform " %k %d")
(description :initarg :description :initform nil)
(face :initarg :face :initform nil)
(show-help :initarg :show-help :initform nil))
"Superclass for suffix command.")

(defclass transient-information (transient-suffix)
Expand Down Expand Up @@ -2071,7 +2074,7 @@ value. Otherwise return CHILDREN as is."

(defun transient--init-suffixes (name)
(let ((levels (alist-get name transient-levels)))
(cl-mapcan (lambda (c) (transient--init-child levels c))
(cl-mapcan (lambda (c) (transient--init-child levels c nil))
(append (get name 'transient--layout)
(and (not transient--editp)
(get 'transient-common-commands
Expand All @@ -2089,24 +2092,29 @@ value. Otherwise return CHILDREN as is."
(list def)))))
(cl-mapcan #'s layout)))

(defun transient--init-child (levels spec)
(defun transient--init-child (levels spec parent)
(cl-etypecase spec
(vector (transient--init-group levels spec))
(list (transient--init-suffix levels spec))
(vector (transient--init-group levels spec parent))
(list (transient--init-suffix levels spec parent))
(string (list spec))))

(defun transient--init-group (levels spec)
(defun transient--init-group (levels spec parent)
(pcase-let ((`(,level ,class ,args ,children) (append spec nil)))
(and-let* (((transient--use-level-p level))
(obj (apply class :level level args))
((transient--use-suffix-p obj))
(suffixes (cl-mapcan (lambda (c) (transient--init-child levels c))
(transient-setup-children obj children))))
((prog1 t
(when (or (and parent (oref parent inapt))
(transient--inapt-suffix-p obj))
(oset obj inapt t))))
(suffixes (cl-mapcan
(lambda (c) (transient--init-child levels c obj))
(transient-setup-children obj children))))
(progn ; work around debbugs#31840
(oset obj suffixes suffixes)
(list obj)))))

(defun transient--init-suffix (levels spec)
(defun transient--init-suffix (levels spec parent)
(pcase-let* ((`(,level ,class ,args) spec)
(cmd (plist-get args :command))
(key (transient--kbd (plist-get args :key)))
Expand Down Expand Up @@ -2139,7 +2147,8 @@ value. Otherwise return CHILDREN as is."
(unless (cl-typep obj 'transient-information)
(transient--init-suffix-key obj))
(when (transient--use-suffix-p obj)
(if (transient--inapt-suffix-p obj)
(if (or (and parent (oref parent inapt))
(transient--inapt-suffix-p obj))
(oset obj inapt t)
(transient-init-scope obj)
(transient-init-value obj))
Expand Down Expand Up @@ -3945,9 +3954,11 @@ and its value is returned to the caller."
doesn't use the `face' property at all, then apply the face
`transient-heading' to the complete string."
(and-let* ((desc (cl-call-next-method obj)))
(if (text-property-not-all 0 (length desc) 'face nil desc)
desc
(propertize desc 'face 'transient-heading))))
(cond ((oref obj inapt)
(propertize desc 'face 'transient-inapt-suffix))
((text-property-not-all 0 (length desc) 'face nil desc)
desc)
((propertize desc 'face 'transient-heading)))))

(cl-defmethod transient-format-description :around ((obj transient-suffix))
"Format the description by calling the next method. If the result
Expand Down

0 comments on commit 3d395d6

Please sign in to comment.