Skip to content

Commit

Permalink
transient-scope: Add CLASSES argument
Browse files Browse the repository at this point in the history
Closes #334.
  • Loading branch information
tarsius committed Dec 5, 2024
1 parent eff2245 commit 0974bf5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
12 changes: 8 additions & 4 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@

- Added documentation for ~inapt-if*~ slots to manual. 179545a6

- ~transient-args~ and ~transient-scope~ now both take a prefix command or
a list of prefix commands as argument. ~transient-scope~ can still be
called without an argument, but that should only be done in functions
that take part in setting up a menu, not in a suffix command.
- ~transient-args~ now takes a prefix command or a list of prefix
commands as argument.

- ~transient-scope~ now takes a prefix command or a list of prefix
commands and/or a prefix class or list of prefix classes as
arguments. It can still be called without any argument, but that
should only be done in functions that take part in setting up a
menu, not in a suffix command.

- Added new generic function ~transient-prefix-value~, giving finer
control over how the value returned by ~transient-args~ is determined.
Expand Down
49 changes: 30 additions & 19 deletions lisp/transient.el
Original file line number Diff line number Diff line change
Expand Up @@ -3787,37 +3787,48 @@ a default implementation, which is a noop.")

;;;; Get

(defun transient-scope (&optional prefixes)
(defun transient-scope (&optional prefixes classes)
"Return the scope of the active or current transient prefix command.
If optional PREFIXES is nil, return the scope of the prefix currently
being setup, making this variant useful, e.g., in `:if*' predicates.
If no prefix is being setup, but the current command was invoked from
some prefix, then return the scope of that.
When this function is called from the body or `interactive' form of a
suffix command, PREFIXES should be non-nil.
If optional PREFIXES and CLASSES are both nil, return the scope of
the prefix currently being setup, making this variation useful, e.g.,
in `:if*' predicates. If no prefix is being setup, but the current
command was invoked from some prefix, then return the scope of that.
If PREFIXES is non-nil, it must be a prefix command or a list of such
commands. In this case try the following in order:
commands. If CLASSES is non-nil, it must be a prefix class or a list
of such classes. When this function is called from the body or the
`interactive' form of a suffix command, PREFIXES and/or CLASSES should
be non-nil. If either is non-nil, try the following in order:
- If the current suffix command was invoked from a prefix, which
appears in PREFIXES, then return the scope of that prefix.
appears in PREFIXES, return the scope of that prefix.
- If the current suffix command was invoked from a prefix, and its
class derives from one of the CLASSES, return the scope of that
prefix.
- If a prefix is being setup and it appears in PREFIXES, return its
scope.
- If a prefix is being setup and it appears in PREFIXES, then return
its scope.
- If a prefix is being setup and its class derives from one of the
CLASSES, return its scope.
- Finally try to return the default scope of the first prefix in
- Finally try to return the default scope of the first command in
PREFIXES. This only works if that slot is set in the respective
class definition or using its `transient-init-scope' method.
If no prefix matches, return nil."
(if prefixes
(let ((prefixes (ensure-list prefixes)))
(if-let* ((obj (or (and-let* ((obj transient-current-prefix))
(and (memq (oref obj command) prefixes) obj))
(and-let* ((obj transient--prefix))
(and (memq (oref obj command) prefixes) obj)))))
(if (or prefixes classes)
(let* ((prefixes (ensure-list prefixes))
(type (if (symbolp classes) classes (cons 'or classes))))
(if-let* ((obj (cl-flet ((match (obj)
(and obj
(or (memq (oref obj command) prefixes)
(cl-typep obj type))
obj)))
(or (match transient-current-prefix)
(match transient--prefix)))))
(oref obj scope)
(and (get (car prefixes) 'transient--prefix)
(oref (transient--init-prefix (car prefixes)) scope))))
Expand Down

0 comments on commit 0974bf5

Please sign in to comment.