Skip to content

Commit

Permalink
transient-describe: New command
Browse files Browse the repository at this point in the history
Two examples:

 ("d" "show magit-status' docstring" transient-describe := magit-status)
 ("m" "show transient's manual" transient-describe := "transient"
   :helper transient--show-manual)
  • Loading branch information
tarsius committed Dec 19, 2024
1 parent 52679f9 commit 5a18a79
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
3 changes: 3 additions & 0 deletions docs/transient.org
Original file line number Diff line number Diff line change
Expand Up @@ -1902,6 +1902,9 @@ object should not affect later invocations.
it is somewhat likely that future improvements won't be fully
backward compatible.

- The ~transient-describe-target~ class is used by the command
~transient-describe~.

- The ~transient-value-preset~ class is used to implement the command
~transient-preset~, which activates a value preset.

Expand Down
4 changes: 4 additions & 0 deletions docs/transient.texi
Original file line number Diff line number Diff line change
Expand Up @@ -2157,6 +2157,10 @@ value of lisp variables. This class is not fully featured yet and
it is somewhat likely that future improvements won't be fully
backward compatible.

@item
The @code{transient-describe-target} class is used by the command
@code{transient-describe}.

@item
The @code{transient-value-preset} class is used to implement the command
@code{transient-preset}, which activates a value preset.
Expand Down
62 changes: 50 additions & 12 deletions lisp/transient.el
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,12 @@ They become the value of this argument.")
(set :initarg := :initform nil))
"Class used by the `transient-preset' suffix command.")

(defclass transient-describe-target (transient-suffix)
((transient :initform #'transient--do-suspend)
(helper :initarg :helper :initform nil)
(target :initarg := :initform nil))
"Class used by the `transient-describe' suffix command.")

;;;; Group

(defclass transient-group (transient-child)
Expand Down Expand Up @@ -2953,6 +2959,9 @@ Do not push the active transient to the transient stack."

(defun transient--do-suspend ()
"Suspend the active transient, saving the transient stack."
;; Export so that `transient-describe' instances can use
;; `transient-suffix-object' to get their respective object.
(transient--export)
(transient--stack-push)
(setq transient--exitp 'suspend)
transient--exit)
Expand Down Expand Up @@ -3128,18 +3137,32 @@ transient is active."
(when (lookup-key transient--transient-map
(this-single-command-raw-keys))
(setq transient--helpp nil)
(let ((winconf (current-window-configuration)))
(transient-show-help
(if (eq this-original-command 'transient-help)
transient--prefix
(or (transient-suffix-object)
this-original-command)))
(setq-local transient--restore-winconf winconf))
(fit-window-to-buffer nil (frame-height) (window-height))
(transient-resume-mode)
(message (substitute-command-keys
"Type \\`q' to resume transient command."))
t))))
(transient--display-help #'transient-show-help
(if (eq this-original-command 'transient-help)
transient--prefix
(or (transient-suffix-object)
this-original-command)))))))

(transient-define-suffix transient-describe ()
"From a transient menu, describe something in another buffer.
This command can be bound multiple times to describe different targets.
Each binding must specify the thing it describes, be setting the value
of its `target' slot, using the keyword argument `:='.
The `helper' slot specifies the low-level function used to describe the
target, and can be omitted, in which case `transient--describe-function'
is used for a symbol, `transient--show-manual' is used for a string
beginning with a parenthesis, and `transient--show-manpage' is used for
any other string.
For example:
[(\"e\" \"about emacs\" transient-describe := \"(emacs)\")
(\"g\" \"about git\" transient-describe := \"git\")]"
:class 'transient-describe-target
(interactive)
(with-slots (helper target) (transient-suffix-object)
(transient--display-help helper target)))

;;;; Level

Expand Down Expand Up @@ -4538,6 +4561,21 @@ Select the help window, and make the help buffer current and return it."
(setq buffer (current-buffer)))
(set-buffer buffer)))

(defun transient--display-help (helper target)
(let ((winconf (current-window-configuration)))
(funcall (cond (helper)
((symbolp target) #'transient--describe-function)
((stringp target)
(if (string-prefix-p "(" target)
#'transient--show-manual
#'transient--show-manpage))
((error "Unknown how to show help for %S" target)))
target)
(setq-local transient--restore-winconf winconf))
(fit-window-to-buffer nil (frame-height) (window-height))
(transient-resume-mode)
(message (substitute-command-keys "Type \\`q' to resume transient command.")))

(defun transient--describe-function (fn)
(let* ((buffer nil)
(help-window-select t)
Expand Down

0 comments on commit 5a18a79

Please sign in to comment.