Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3198 lines
123 KiB

  1. ;;; transient.el --- Transient commands -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2018-2020 Jonas Bernoulli
  3. ;; Author: Jonas Bernoulli <jonas@bernoul.li>
  4. ;; Homepage: https://github.com/magit/transient
  5. ;; Package-Requires: ((emacs "25.1"))
  6. ;; Keywords: bindings
  7. ;; This file is not part of GNU Emacs.
  8. ;; This file is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published
  10. ;; by the Free Software Foundation; either version 3 of the License,
  11. ;; or (at your option) any later version.
  12. ;; This file is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; For a full copy of the GNU GPL see http://www.gnu.org/licenses.
  17. ;;; Commentary:
  18. ;; Taking inspiration from prefix keys and prefix arguments, Transient
  19. ;; implements a similar abstraction involving a prefix command, infix
  20. ;; arguments and suffix commands. We could call this abstraction a
  21. ;; "transient command", but because it always involves at least two
  22. ;; commands (a prefix and a suffix) we prefer to call it just a
  23. ;; "transient".
  24. ;; When the user calls a transient prefix command, then a transient
  25. ;; (temporary) keymap is activated, which binds the transient's infix
  26. ;; and suffix commands, and functions that control the transient state
  27. ;; are added to `pre-command-hook' and `post-command-hook'. The
  28. ;; available suffix and infix commands and their state are shown in
  29. ;; the echo area until the transient is exited by invoking a suffix
  30. ;; command.
  31. ;; Calling an infix command causes its value to be changed, possibly
  32. ;; by reading a new value in the minibuffer.
  33. ;; Calling a suffix command usually causes the transient to be exited
  34. ;; but suffix commands can also be configured to not exit the
  35. ;; transient state.
  36. ;;; Code:
  37. (require 'cl-lib)
  38. (require 'eieio)
  39. (require 'format-spec)
  40. (require 'seq)
  41. (eval-when-compile
  42. (require 'subr-x))
  43. (declare-function info 'info)
  44. (declare-function Man-find-section 'man)
  45. (declare-function Man-next-section 'man)
  46. (declare-function Man-getpage-in-background 'man)
  47. (defvar Man-notify-method)
  48. ;;; Options
  49. (defgroup transient nil
  50. "Transient commands."
  51. :group 'extensions)
  52. (defcustom transient-show-popup t
  53. "Whether to show the current transient in a popup buffer.
  54. - If t, then show the popup as soon as a transient prefix command
  55. is invoked.
  56. - If nil, then do not show the popup unless the user explicitly
  57. requests it, by pressing an incomplete prefix key sequence.
  58. - If a number, then delay displaying the popup and instead show
  59. a brief one-line summary. If zero or negative, then suppress
  60. even showing that summary and display the pressed key only.
  61. Show the popup when the user explicitly requests it by pressing
  62. an incomplete prefix key sequence. Unless zero, then also show
  63. the popup after that many seconds of inactivity (using the
  64. absolute value)."
  65. :package-version '(transient . "0.1.0")
  66. :group 'transient
  67. :type '(choice (const :tag "instantly" t)
  68. (const :tag "on demand" nil)
  69. (const :tag "on demand (no summary)" 0)
  70. (number :tag "after delay" 1)))
  71. (defcustom transient-enable-popup-navigation nil
  72. "Whether navigation commands are enabled in the transient popup.
  73. While a transient is active the transient popup buffer is not the
  74. current buffer, making it necessary to use dedicated commands to
  75. act on that buffer itself. If this non-nil, then the following
  76. features are available:
  77. - \"<up>\" moves the cursor to the previous suffix.
  78. \"<down>\" moves the cursor to the next suffix.
  79. \"RET\" invokes the suffix the cursor is on.
  80. - \"<mouse-1>\" invokes the clicked on suffix.
  81. - \"C-s\" and \"C-r\" start isearch in the popup buffer."
  82. :package-version '(transient . "0.2.0")
  83. :group 'transient
  84. :type 'boolean)
  85. (defcustom transient-display-buffer-action
  86. '(display-buffer-in-side-window (side . bottom))
  87. "The action used to display the transient popup buffer.
  88. The transient popup buffer is displayed in a window using
  89. \(display-buffer buf transient-display-buffer-action)
  90. The value of this option has the form (FUNCTION . ALIST),
  91. where FUNCTION is a function or a list of functions. Each such
  92. function should accept two arguments: a buffer to display and
  93. an alist of the same form as ALIST. See `display-buffer' for
  94. details.
  95. The default is (display-buffer-in-side-window (side . bottom)).
  96. This displays the window at the bottom of the selected frame.
  97. Another useful value is (display-buffer-below-selected). This
  98. is what `magit-popup' used by default. For more alternatives
  99. see info node `(elisp)Display Action Functions'.
  100. It may be possible to display the window in another frame, but
  101. whether that works in practice depends on the window-manager.
  102. If the window manager selects the new window (Emacs frame),
  103. then it doesn't work.
  104. If you change the value of this option, then you might also
  105. want to change the value of `transient-mode-line-format'."
  106. :package-version '(transient . "0.2.0")
  107. :group 'transient
  108. :type '(cons (choice function (repeat :tag "Functions" function))
  109. alist))
  110. (defcustom transient-mode-line-format 'line
  111. "The mode-line format for the transient popup buffer.
  112. If nil, then the buffer has no mode-line. If the buffer is not
  113. displayed right above the echo area, then this probably is not
  114. a good value.
  115. If `line' (the default), then the buffer also has no mode-line,
  116. but a thin line is drawn instead, using the background color of
  117. the face `transient-separator'. Termcap frames cannot display
  118. thin lines and therefore fallback to treating `line' like nil.
  119. Otherwise this can be any mode-line format.
  120. See `mode-line-format' for details."
  121. :package-version '(transient . "0.2.0")
  122. :group 'transient
  123. :type '(choice (const :tag "hide mode-line" nil)
  124. (const :tag "substitute thin line" line)
  125. (const :tag "name of prefix command"
  126. ("%e" mode-line-front-space
  127. mode-line-buffer-identification))
  128. (sexp :tag "custom mode-line format")))
  129. (defcustom transient-show-common-commands nil
  130. "Whether to show common transient suffixes in the popup buffer.
  131. These commands are always shown after typing the prefix key
  132. \"C-x\" when a transient command is active. To toggle the value
  133. of this variable use \"C-x t\" when a transient is active."
  134. :package-version '(transient . "0.1.0")
  135. :group 'transient
  136. :type 'boolean)
  137. (defcustom transient-read-with-initial-input nil
  138. "Whether to use the last history element as initial minibuffer input."
  139. :package-version '(transient . "0.2.0")
  140. :group 'transient
  141. :type 'boolean)
  142. (defcustom transient-highlight-mismatched-keys nil
  143. "Whether to highlight keys that do not match their argument.
  144. This only affects infix arguments that represent command-line
  145. arguments. When this option is non-nil, then the key binding
  146. for infix argument are highlighted when only a long argument
  147. \(e.g. \"--verbose\") is specified but no shor-thand (e.g \"-v\").
  148. In the rare case that a short-hand is specified but does not
  149. match the key binding, then it is highlighed differently.
  150. The highlighting is done using using `transient-mismatched-key'
  151. and `transient-nonstandard-key'."
  152. :package-version '(transient . "0.1.0")
  153. :group 'transient
  154. :type 'boolean)
  155. (defcustom transient-substitute-key-function nil
  156. "Function used to modify key bindings.
  157. This function is called with one argument, the prefix object,
  158. and must return a key binding description, either the existing
  159. key description it finds in the `key' slot, or a substitution.
  160. This is intended to let users replace certain prefix keys. It
  161. could also be used to make other substitutions, but that is
  162. discouraged.
  163. For example, \"=\" is hard to reach using my custom keyboard
  164. layout, so I substitute \"(\" for that, which is easy to reach
  165. using a layout optimized for lisp.
  166. (setq transient-substitute-key-function
  167. (lambda (obj)
  168. (let ((key (oref obj key)))
  169. (if (string-match \"\\\\`\\\\(=\\\\)[a-zA-Z]\" key)
  170. (replace-match \"(\" t t key 1)
  171. key)))))"
  172. :package-version '(transient . "0.1.0")
  173. :group 'transient
  174. :type '(choice (const :tag "Transform no keys (nil)" nil) function))
  175. (defcustom transient-detect-key-conflicts nil
  176. "Whether to detect key binding conflicts.
  177. Conflicts are detected when a transient prefix command is invoked
  178. and results in an error, which prevents the transient from being
  179. used."
  180. :package-version '(transient . "0.1.0")
  181. :group 'transient
  182. :type 'boolean)
  183. (defcustom transient-force-fixed-pitch nil
  184. "Whether to force used of monospaced font in popup buffer.
  185. Even if you use a proportional font for the `default' face,
  186. you might still want to use a monospaced font in transient's
  187. popup buffer. Setting this option to t causes `default' to
  188. be remapped to `fixed-pitch' in that buffer."
  189. :package-version '(transient . "0.2.0")
  190. :group 'transient
  191. :type 'boolean)
  192. (defcustom transient-default-level 4
  193. "Control what suffix levels are made available by default.
  194. Each suffix command is placed on a level and each prefix command
  195. has a level, which controls which suffix commands are available.
  196. Integers between 1 and 7 (inclusive) are valid levels.
  197. The levels of individual transients and/or their individual
  198. suffixes can be changed individually, by invoking the prefix and
  199. then pressing \"C-x l\".
  200. The default level for both transients and their suffixes is 4.
  201. This option only controls the default for transients. The default
  202. suffix level is always 4. The author of a transient should place
  203. certain suffixes on a higher level if they expect that it won't be
  204. of use to most users, and they should place very important suffixes
  205. on a lower level so that they remain available even if the user
  206. lowers the transient level.
  207. \(Magit currently places nearly all suffixes on level 4 and lower
  208. levels are not used at all yet. So for the time being you should
  209. not set a lower level here and using a higher level might not
  210. give you as many additional suffixes as you hoped.)"
  211. :package-version '(transient . "0.1.0")
  212. :group 'transient
  213. :type '(choice (const :tag "1 - fewest suffixes" 1)
  214. (const 2)
  215. (const 3)
  216. (const :tag "4 - default" 4)
  217. (const 5)
  218. (const 6)
  219. (const :tag "7 - most suffixes" 7)))
  220. (defcustom transient-levels-file
  221. (locate-user-emacs-file (convert-standard-filename "transient/levels.el"))
  222. "File used to save levels of transients and their suffixes."
  223. :package-version '(transient . "0.1.0")
  224. :group 'transient
  225. :type 'file)
  226. (defcustom transient-values-file
  227. (locate-user-emacs-file (convert-standard-filename "transient/values.el"))
  228. "File used to save values of transients."
  229. :package-version '(transient . "0.1.0")
  230. :group 'transient
  231. :type 'file)
  232. (defcustom transient-history-file
  233. (locate-user-emacs-file (convert-standard-filename "transient/history.el"))
  234. "File used to save history of transients and their infixes."
  235. :package-version '(transient . "0.1.0")
  236. :group 'transient
  237. :type 'file)
  238. (defcustom transient-history-limit 10
  239. "Number of history elements to keep when saving to file."
  240. :package-version '(transient . "0.1.0")
  241. :group 'transient
  242. :type 'integer)
  243. (defcustom transient-save-history t
  244. "Whether to save history of transient commands when exiting Emacs."
  245. :package-version '(transient . "0.1.0")
  246. :group 'transient
  247. :type 'boolean)
  248. ;;; Faces
  249. (defgroup transient-faces nil
  250. "Faces used by Transient."
  251. :group 'transient)
  252. (defface transient-heading '((t :inherit font-lock-keyword-face))
  253. "Face used for headings."
  254. :group 'transient-faces)
  255. (defface transient-key '((t :inherit font-lock-builtin-face))
  256. "Face used for keys."
  257. :group 'transient-faces)
  258. (defface transient-argument '((t :inherit font-lock-warning-face))
  259. "Face used for enabled arguments."
  260. :group 'transient-faces)
  261. (defface transient-value '((t :inherit font-lock-string-face))
  262. "Face used for values."
  263. :group 'transient-faces)
  264. (defface transient-inactive-argument '((t :inherit shadow))
  265. "Face used for inactive arguments."
  266. :group 'transient-faces)
  267. (defface transient-inactive-value '((t :inherit shadow))
  268. "Face used for inactive values."
  269. :group 'transient-faces)
  270. (defface transient-unreachable '((t :inherit shadow))
  271. "Face used for suffixes unreachable from the current prefix sequence."
  272. :group 'transient-faces)
  273. (defface transient-active-infix '((t :inherit secondary-selection))
  274. "Face used for the infix for which the value is being read."
  275. :group 'transient-faces)
  276. (defface transient-unreachable-key '((t :inherit shadow))
  277. "Face used for keys unreachable from the current prefix sequence."
  278. :group 'transient-faces)
  279. (defface transient-nonstandard-key '((t :underline t))
  280. "Face optionally used to highlight keys conflicting with short-argument.
  281. Also see option `transient-highlight-mismatched-keys'."
  282. :group 'transient-faces)
  283. (defface transient-mismatched-key '((t :underline t))
  284. "Face optionally used to highlight keys without a short-argument.
  285. Also see option `transient-highlight-mismatched-keys'."
  286. :group 'transient-faces)
  287. (defface transient-enabled-suffix
  288. '((t :background "green" :foreground "black" :weight bold))
  289. "Face used for enabled levels while editing suffix levels.
  290. See info node `(transient)Enabling and Disabling Suffixes'."
  291. :group 'transient-faces)
  292. (defface transient-disabled-suffix
  293. '((t :background "red" :foreground "black" :weight bold))
  294. "Face used for disabled levels while editing suffix levels.
  295. See info node `(transient)Enabling and Disabling Suffixes'."
  296. :group 'transient-faces)
  297. (defface transient-separator
  298. `((((class color) (background light))
  299. ,@(and (>= emacs-major-version 27) '(:extend t))
  300. :background "grey80")
  301. (((class color) (background dark))
  302. ,@(and (>= emacs-major-version 27) '(:extend t))
  303. :background "grey30"))
  304. "Face used to draw line below transient popup window.
  305. This is only used if `transient-mode-line-format' is `line'.
  306. Only the background color is significant."
  307. :group 'transient-faces)
  308. ;;; Persistence
  309. (defun transient--read-file-contents (file)
  310. (with-demoted-errors "Transient error: %S"
  311. (and (file-exists-p file)
  312. (with-temp-buffer
  313. (insert-file-contents file)
  314. (read (current-buffer))))))
  315. (defun transient--pp-to-file (object file)
  316. (make-directory (file-name-directory file) t)
  317. (setq object (cl-sort object #'string< :key #'car))
  318. (with-temp-file file
  319. (let ((print-level nil)
  320. (print-length nil))
  321. (pp object (current-buffer)))))
  322. (defvar transient-values
  323. (transient--read-file-contents transient-values-file)
  324. "Values of transient commands.
  325. The value of this variable persists between Emacs sessions
  326. and you usually should not change it manually.")
  327. (defun transient-save-values ()
  328. (transient--pp-to-file transient-values transient-values-file))
  329. (defvar transient-levels
  330. (transient--read-file-contents transient-levels-file)
  331. "Levels of transient commands.
  332. The value of this variable persists between Emacs sessions
  333. and you usually should not change it manually.")
  334. (defun transient-save-levels ()
  335. (transient--pp-to-file transient-levels transient-levels-file))
  336. (defvar transient-history
  337. (transient--read-file-contents transient-history-file)
  338. "History of transient commands and infix arguments.
  339. The value of this variable persists between Emacs sessions
  340. \(unless `transient-save-history' is nil) and you usually
  341. should not change it manually.")
  342. (defun transient-save-history ()
  343. (setq transient-history
  344. (cl-sort (mapcar (pcase-lambda (`(,key . ,val))
  345. (cons key (seq-take (delete-dups val)
  346. transient-history-limit)))
  347. transient-history)
  348. #'string< :key #'car))
  349. (transient--pp-to-file transient-history transient-history-file))
  350. (defun transient-maybe-save-history ()
  351. "Save the value of `transient-history'.
  352. If `transient-save-history' is nil, then do nothing."
  353. (when transient-save-history
  354. (transient-save-history)))
  355. (unless noninteractive
  356. (add-hook 'kill-emacs-hook 'transient-maybe-save-history))
  357. ;;; Classes
  358. ;;;; Prefix
  359. (defclass transient-prefix ()
  360. ((prototype :initarg :prototype)
  361. (command :initarg :command)
  362. (level :initarg :level)
  363. (variable :initarg :variable :initform nil)
  364. (value) (default-value :initarg :value)
  365. (scope :initarg :scope :initform nil)
  366. (history :initarg :history :initform nil)
  367. (history-pos :initarg :history-pos :initform 0)
  368. (history-key :initarg :history-key :initform nil)
  369. (man-page :initarg :man-page :initform nil)
  370. (info-manual :initarg :info-manual :initform nil)
  371. (transient-suffix :initarg :transient-suffix :initform nil)
  372. (transient-non-suffix :initarg :transient-non-suffix :initform nil)
  373. (incompatible :initarg :incompatible :initform nil))
  374. "Transient prefix command.
  375. Each transient prefix command consists of a command, which is
  376. stored in a symbol's function slot and an object, which is
  377. stored in the `transient--prefix' property of the same symbol.
  378. When a transient prefix command is invoked, then a clone of that
  379. object is stored in the global variable `transient--prefix' and
  380. the prototype is stored in the clone's `prototype' slot.")
  381. ;;;; Suffix
  382. (defclass transient-child ()
  383. ((level
  384. :initarg :level
  385. :initform 1
  386. :documentation "Enable if level of prefix is equal or greater.")
  387. (if
  388. :initarg :if
  389. :initform nil
  390. :documentation "Enable if predicate returns non-nil.")
  391. (if-not
  392. :initarg :if-not
  393. :initform nil
  394. :documentation "Enable if predicate returns nil.")
  395. (if-non-nil
  396. :initarg :if-non-nil
  397. :initform nil
  398. :documentation "Enable if variable's value is non-nil.")
  399. (if-nil
  400. :initarg :if-nil
  401. :initform nil
  402. :documentation "Enable if variable's value is nil.")
  403. (if-mode
  404. :initarg :if-mode
  405. :initform nil
  406. :documentation "Enable if major-mode matches value.")
  407. (if-not-mode
  408. :initarg :if-not-mode
  409. :initform nil
  410. :documentation "Enable if major-mode does not match value.")
  411. (if-derived
  412. :initarg :if-derived
  413. :initform nil
  414. :documentation "Enable if major-mode derives from value.")
  415. (if-not-derived
  416. :initarg :if-not-derived
  417. :initform nil
  418. :documentation "Enable if major-mode does not derive from value."))
  419. "Abstract superclass for group and and suffix classes.
  420. It is undefined what happens if more than one `if*' predicate
  421. slot is non-nil."
  422. :abstract t)
  423. (defclass transient-suffix (transient-child)
  424. ((key :initarg :key)
  425. (command :initarg :command)
  426. (transient :initarg :transient)
  427. (format :initarg :format :initform " %k %d")
  428. (description :initarg :description :initform nil))
  429. "Superclass for suffix command.")
  430. (defclass transient-infix (transient-suffix)
  431. ((transient :initform t)
  432. (argument :initarg :argument)
  433. (shortarg :initarg :shortarg)
  434. (value :initform nil)
  435. (multi-value :initarg :multi-value :initform nil)
  436. (allow-empty :initarg :allow-empty :initform nil)
  437. (history-key :initarg :history-key :initform nil)
  438. (reader :initarg :reader :initform nil)
  439. (prompt :initarg :prompt :initform nil)
  440. (choices :initarg :choices :initform nil)
  441. (format :initform " %k %d (%v)"))
  442. "Transient infix command."
  443. :abstract t)
  444. (defclass transient-argument (transient-infix) ()
  445. "Abstract superclass for infix arguments."
  446. :abstract t)
  447. (defclass transient-switch (transient-argument) ()
  448. "Class used for command-line argument that can be turned on and off.")
  449. (defclass transient-option (transient-argument) ()
  450. "Class used for command-line argument that can take a value.")
  451. (defclass transient-variable (transient-infix)
  452. ((variable :initarg :variable)
  453. (format :initform " %k %d %v"))
  454. "Abstract superclass for infix commands that set a variable."
  455. :abstract t)
  456. (defclass transient-switches (transient-argument)
  457. ((argument-format :initarg :argument-format)
  458. (argument-regexp :initarg :argument-regexp))
  459. "Class used for sets of mutually exclusive command-line switches.")
  460. (defclass transient-files (transient-infix) ()
  461. "Class used for the \"--\" argument.
  462. All remaining arguments are treated as files.
  463. They become the value of this this argument.")
  464. ;;;; Group
  465. (defclass transient-group (transient-child)
  466. ((suffixes :initarg :suffixes :initform nil)
  467. (hide :initarg :hide :initform nil)
  468. (description :initarg :description :initform nil))
  469. "Abstract superclass of all group classes."
  470. :abstract t)
  471. (defclass transient-column (transient-group) ()
  472. "Group class that displays each element on a separate line.")
  473. (defclass transient-row (transient-group) ()
  474. "Group class that displays all elements on a single line.")
  475. (defclass transient-columns (transient-group) ()
  476. "Group class that displays elements organized in columns.
  477. Direct elements have to be groups whose elements have to be
  478. commands or string. Each subgroup represents a column. This
  479. class takes care of inserting the subgroups' elements.")
  480. (defclass transient-subgroups (transient-group) ()
  481. "Group class that wraps other groups.
  482. Direct elements have to be groups whose elements have to be
  483. commands or strings. This group inserts an empty line between
  484. subgroups. The subgroups are responsible for displaying their
  485. elements themselves.")
  486. ;;; Define
  487. (defmacro define-transient-command (name arglist &rest args)
  488. "Define NAME as a transient prefix command.
  489. ARGLIST are the arguments that command takes.
  490. DOCSTRING is the documentation string and is optional.
  491. These arguments can optionally be followed by key-value pairs.
  492. Each key has to be a keyword symbol, either `:class' or a keyword
  493. argument supported by the constructor of that class. The
  494. `transient-prefix' class is used if the class is not specified
  495. explicitly.
  496. GROUPs add key bindings for infix and suffix commands and specify
  497. how these bindings are presented in the popup buffer. At least
  498. one GROUP has to be specified. See info node `(transient)Binding
  499. Suffix and Infix Commands'.
  500. The BODY is optional. If it is omitted, then ARGLIST is also
  501. ignored and the function definition becomes:
  502. (lambda ()
  503. (interactive)
  504. (transient-setup \\='NAME))
  505. If BODY is specified, then it must begin with an `interactive'
  506. form that matches ARGLIST, and it must call `transient-setup'.
  507. It may however call that function only when some condition is
  508. satisfied; that is one of the reason why you might want to use
  509. an explicit BODY.
  510. All transients have a (possibly nil) value, which is exported
  511. when suffix commands are called, so that they can consume that
  512. value. For some transients it might be necessary to have a sort
  513. of secondary value, called a scope. Such a scope would usually
  514. be set in the commands `interactive' form and has to be passed
  515. to the setup function:
  516. (transient-setup \\='NAME nil nil :scope SCOPE)
  517. \(fn NAME ARGLIST [DOCSTRING] [KEYWORD VALUE]... GROUP... [BODY...])"
  518. (declare (debug (&define name lambda-list
  519. [&optional lambda-doc]
  520. [&rest keywordp sexp]
  521. [&rest vectorp]
  522. [&optional ("interactive" interactive) def-body]))
  523. (doc-string 3))
  524. (pcase-let ((`(,class ,slots ,suffixes ,docstr ,body)
  525. (transient--expand-define-args args)))
  526. `(progn
  527. (defalias ',name
  528. ,(if body
  529. `(lambda ,arglist ,@body)
  530. `(lambda ()
  531. (interactive)
  532. (transient-setup ',name))))
  533. (put ',name 'interactive-only t)
  534. (put ',name 'function-documentation ,docstr)
  535. (put ',name 'transient--prefix
  536. (,(or class 'transient-prefix) :command ',name ,@slots))
  537. (put ',name 'transient--layout
  538. ',(cl-mapcan (lambda (s) (transient--parse-child name s))
  539. suffixes)))))
  540. (defmacro define-suffix-command (name arglist &rest args)
  541. "Define NAME as a transient suffix command.
  542. ARGLIST are the arguments that the command takes.
  543. DOCSTRING is the documentation string and is optional.
  544. These arguments can optionally be followed by key-value pairs.
  545. Each key has to be a keyword symbol, either `:class' or a
  546. keyword argument supported by the constructor of that class.
  547. The `transient-suffix' class is used if the class is not
  548. specified explicitly.
  549. The BODY must begin with an `interactive' form that matches
  550. ARGLIST. The infix arguments are usually accessed by using
  551. `transient-args' inside `interactive'.
  552. \(fn NAME ARGLIST [DOCSTRING] [KEYWORD VALUE]... BODY...)"
  553. (declare (debug (&define name lambda-list
  554. [&optional lambda-doc]
  555. [&rest keywordp sexp]
  556. ("interactive" interactive)
  557. def-body))
  558. (doc-string 3))
  559. (pcase-let ((`(,class ,slots ,_ ,docstr ,body)
  560. (transient--expand-define-args args)))
  561. `(progn
  562. (defalias ',name (lambda ,arglist ,@body))
  563. (put ',name 'interactive-only t)
  564. (put ',name 'function-documentation ,docstr)
  565. (put ',name 'transient--suffix
  566. (,(or class 'transient-suffix) :command ',name ,@slots)))))
  567. (defmacro define-infix-command (name _arglist &rest args)
  568. "Define NAME as a transient infix command.
  569. ARGLIST is always ignored and reserved for future use.
  570. DOCSTRING is the documentation string and is optional.
  571. The key-value pairs are mandatory. All transient infix commands
  572. are equal to each other (but not eq), so it is meaningless to
  573. define an infix command without also setting at least `:class'
  574. and one other keyword (which it is depends on the used class,
  575. usually `:argument' or `:variable').
  576. Each key has to be a keyword symbol, either `:class' or a keyword
  577. argument supported by the constructor of that class. The
  578. `transient-switch' class is used if the class is not specified
  579. explicitly.
  580. The function definitions is always:
  581. (lambda ()
  582. (interactive)
  583. (let ((obj (transient-suffix-object)))
  584. (transient-infix-set obj (transient-infix-read obj)))
  585. (transient--show))
  586. `transient-infix-read' and `transient-infix-set' are generic
  587. functions. Different infix commands behave differently because
  588. the concrete methods are different for different infix command
  589. classes. In rare case the above command function might not be
  590. suitable, even if you define your own infix command class. In
  591. that case you have to use `transient-suffix-command' to define
  592. the infix command and use t as the value of the `:transient'
  593. keyword.
  594. \(fn NAME ARGLIST [DOCSTRING] [KEYWORD VALUE]...)"
  595. (declare (debug (&define name lambda-list
  596. [&optional lambda-doc]
  597. [&rest keywordp sexp]))
  598. (doc-string 3))
  599. (pcase-let ((`(,class ,slots ,_ ,docstr ,_)
  600. (transient--expand-define-args args)))
  601. `(progn
  602. (defalias ',name ,(transient--default-infix-command))
  603. (put ',name 'interactive-only t)
  604. (put ',name 'function-documentation ,docstr)
  605. (put ',name 'transient--suffix
  606. (,(or class 'transient-switch) :command ',name ,@slots)))))
  607. (defalias 'define-infix-argument 'define-infix-command
  608. "Define NAME as a transient infix command.
  609. Only use this alias to define an infix command that actually
  610. sets an infix argument. To define a infix command that, for
  611. example, sets a variable use `define-infix-command' instead.
  612. \(fn NAME ARGLIST [DOCSTRING] [KEYWORD VALUE]...)")
  613. (defun transient--expand-define-args (args)
  614. (let (class keys suffixes docstr)
  615. (when (stringp (car args))
  616. (setq docstr (pop args)))
  617. (while (keywordp (car args))
  618. (let ((k (pop args))
  619. (v (pop args)))
  620. (if (eq k :class)
  621. (setq class v)
  622. (push k keys)
  623. (push v keys))))
  624. (while (vectorp (car args))
  625. (push (pop args) suffixes))
  626. (list (if (eq (car-safe class) 'quote)
  627. (cadr class)
  628. class)
  629. (nreverse keys)
  630. (nreverse suffixes)
  631. docstr
  632. args)))
  633. (defun transient--parse-child (prefix spec)
  634. (cl-etypecase spec
  635. (vector (when-let ((c (transient--parse-group prefix spec))) (list c)))
  636. (list (when-let ((c (transient--parse-suffix prefix spec))) (list c)))
  637. (string (list spec))))
  638. (defun transient--parse-group (prefix spec)
  639. (setq spec (append spec nil))
  640. (cl-symbol-macrolet
  641. ((car (car spec))
  642. (pop (pop spec)))
  643. (let (level class args)
  644. (when (integerp car)
  645. (setq level pop))
  646. (when (stringp car)
  647. (setq args (plist-put args :description pop)))
  648. (while (keywordp car)
  649. (let ((k pop))
  650. (if (eq k :class)
  651. (setq class pop)
  652. (setq args (plist-put args k pop)))))
  653. (vector (or level (oref-default 'transient-child level))
  654. (or class
  655. (if (vectorp car)
  656. 'transient-columns
  657. 'transient-column))
  658. args
  659. (cl-mapcan (lambda (s) (transient--parse-child prefix s)) spec)))))
  660. (defun transient--parse-suffix (prefix spec)
  661. (let (level class args)
  662. (cl-symbol-macrolet
  663. ((car (car spec))
  664. (pop (pop spec)))
  665. (when (integerp car)
  666. (setq level pop))
  667. (when (or (stringp car)
  668. (vectorp car))
  669. (setq args (plist-put args :key pop)))
  670. (when (or (stringp car)
  671. (eq (car-safe car) 'lambda)
  672. (and (symbolp car)
  673. (not (commandp car))
  674. (commandp (cadr spec))))
  675. (setq args (plist-put args :description pop)))
  676. (cond
  677. ((keywordp car)
  678. (error "Need command, got %S" car))
  679. ((symbolp car)
  680. (setq args (plist-put args :command pop)))
  681. ((or (stringp car)
  682. (and car (listp car)))
  683. (let ((arg pop))
  684. (cl-typecase arg
  685. (list
  686. (setq args (plist-put args :shortarg (car arg)))
  687. (setq args (plist-put args :argument (cadr arg)))
  688. (setq arg (cadr arg)))
  689. (string
  690. (when-let ((shortarg (transient--derive-shortarg arg)))
  691. (setq args (plist-put args :shortarg shortarg)))
  692. (setq args (plist-put args :argument arg))))
  693. (setq args (plist-put args :command
  694. (intern (format "transient:%s:%s"
  695. prefix arg))))
  696. (cond ((and car (not (keywordp car)))
  697. (setq class 'transient-option)
  698. (setq args (plist-put args :reader pop)))
  699. ((not (string-suffix-p "=" arg))
  700. (setq class 'transient-switch))
  701. (t
  702. (setq class 'transient-option)
  703. (setq args (plist-put args :reader 'read-string))))))
  704. (t
  705. (error "Needed command or argument, got %S" car)))
  706. (while (keywordp car)
  707. (let ((k pop))
  708. (cl-case k
  709. (:class (setq class pop))
  710. (:level (setq level pop))
  711. (t (setq args (plist-put args k pop)))))))
  712. (unless (plist-get args :key)
  713. (when-let ((shortarg (plist-get args :shortarg)))
  714. (setq args (plist-put args :key shortarg))))
  715. (list (or level (oref-default 'transient-child level))
  716. (or class 'transient-suffix)
  717. args)))
  718. (defun transient--default-infix-command ()
  719. (cons 'lambda
  720. '(()
  721. (interactive)
  722. (let ((obj (transient-suffix-object)))
  723. (transient-infix-set obj (transient-infix-read obj)))
  724. (transient--show))))
  725. (defun transient--ensure-infix-command (obj)
  726. (let ((cmd (oref obj command)))
  727. (unless (or (commandp cmd)
  728. (get cmd 'transient--infix-command))
  729. (if (or (cl-typep obj 'transient-switch)
  730. (cl-typep obj 'transient-option))
  731. (put cmd 'transient--infix-command
  732. (transient--default-infix-command))
  733. ;; This is not an anonymous infix argument.
  734. (error "Suffix %s is not defined or autoloaded as a command" cmd)))))
  735. (defun transient--derive-shortarg (arg)
  736. (save-match-data
  737. (and (string-match "\\`\\(-[a-zA-Z]\\)\\(\\'\\|=\\)" arg)
  738. (match-string 1 arg))))
  739. ;;; Edit
  740. (defun transient--insert-suffix (prefix loc suffix action)
  741. (let* ((suf (cl-etypecase suffix
  742. (vector (transient--parse-group prefix suffix))
  743. (list (transient--parse-suffix prefix suffix))
  744. (string suffix)))
  745. (mem (transient--layout-member loc prefix))
  746. (elt (car mem)))
  747. (cond
  748. ((not mem)
  749. (message "Cannot insert %S into %s; %s not found"
  750. suffix prefix loc))
  751. ((or (and (vectorp suffix) (not (vectorp elt)))
  752. (and (listp suffix) (vectorp elt))
  753. (and (stringp suffix) (vectorp elt)))
  754. (message "Cannot place %S into %s at %s; %s"
  755. suffix prefix loc
  756. "suffixes and groups cannot be siblings"))
  757. (t
  758. (when (and (listp suffix)
  759. (listp elt))
  760. (let ((key (transient--spec-key suf)))
  761. (if (equal (transient--kbd key)
  762. (transient--kbd (transient--spec-key elt)))
  763. (setq action 'replace)
  764. (transient-remove-suffix prefix key))))
  765. (cl-ecase action
  766. (insert (setcdr mem (cons elt (cdr mem)))
  767. (setcar mem suf))
  768. (append (setcdr mem (cons suf (cdr mem))))
  769. (replace (setcar mem suf)))))))
  770. (defun transient-insert-suffix (prefix loc suffix)
  771. "Insert a SUFFIX into PREFIX before LOC.
  772. PREFIX is a prefix command, a symbol.
  773. SUFFIX is a suffix command or a group specification (of
  774. the same forms as expected by `define-transient-command').
  775. LOC is a command, a key vector, a key description (a string
  776. as returned by `key-description'), or a coordination list
  777. (whose last element may also be a command or key).
  778. See info node `(transient)Modifying Existing Transients'."
  779. (declare (indent defun))
  780. (transient--insert-suffix prefix loc suffix 'insert))
  781. (defun transient-append-suffix (prefix loc suffix)
  782. "Insert a SUFFIX into PREFIX after LOC.
  783. PREFIX is a prefix command, a symbol.
  784. SUFFIX is a suffix command or a group specification (of
  785. the same forms as expected by `define-transient-command').
  786. LOC is a command, a key vector, a key description (a string
  787. as returned by `key-description'), or a coordination list
  788. (whose last element may also be a command or key).
  789. See info node `(transient)Modifying Existing Transients'."
  790. (declare (indent defun))
  791. (transient--insert-suffix prefix loc suffix 'append))
  792. (defun transient-replace-suffix (prefix loc suffix)
  793. "Replace the suffix at LOC in PREFIX with SUFFIX.
  794. PREFIX is a prefix command, a symbol.
  795. SUFFIX is a suffix command or a group specification (of
  796. the same forms as expected by `define-transient-command').
  797. LOC is a command, a key vector, a key description (a string
  798. as returned by `key-description'), or a coordination list
  799. (whose last element may also be a command or key).
  800. See info node `(transient)Modifying Existing Transients'."
  801. (declare (indent defun))
  802. (transient--insert-suffix prefix loc suffix 'replace))
  803. (defun transient-remove-suffix (prefix loc)
  804. "Remove the suffix or group at LOC in PREFIX.
  805. PREFIX is a prefix command, a symbol.
  806. LOC is a command, a key vector, a key description (a string
  807. as returned by `key-description'), or a coordination list
  808. (whose last element may also be a command or key).
  809. See info node `(transient)Modifying Existing Transients'."
  810. (declare (indent defun))
  811. (transient--layout-member loc prefix 'remove))
  812. (defun transient-get-suffix (prefix loc)
  813. "Return the suffix or group at LOC in PREFIX.
  814. PREFIX is a prefix command, a symbol.
  815. LOC is a command, a key vector, a key description (a string
  816. as returned by `key-description'), or a coordination list
  817. (whose last element may also be a command or key).
  818. See info node `(transient)Modifying Existing Transients'."
  819. (if-let ((mem (transient--layout-member loc prefix)))
  820. (car mem)
  821. (error "%s not found in %s" loc prefix)))
  822. (defun transient-suffix-put (prefix loc prop value)
  823. "Edit the suffix at LOC in PREFIX, setting PROP to VALUE.
  824. PREFIX is a prefix command, a symbol.
  825. SUFFIX is a suffix command or a group specification (of
  826. the same forms as expected by `define-transient-command').
  827. LOC is a command, a key vector, a key description (a string
  828. as returned by `key-description'), or a coordination list
  829. (whose last element may also be a command or key).
  830. See info node `(transient)Modifying Existing Transients'."
  831. (let ((suf (transient-get-suffix prefix loc)))
  832. (setf (elt suf 2)
  833. (plist-put (elt suf 2) prop value))))
  834. (defun transient--layout-member (loc prefix &optional remove)
  835. (let ((val (or (get prefix 'transient--layout)
  836. (error "%s is not a transient command" prefix))))
  837. (when (listp loc)
  838. (while (integerp (car loc))
  839. (let* ((children (if (vectorp val) (aref val 3) val))
  840. (mem (transient--nthcdr (pop loc) children)))
  841. (if (and remove (not loc))
  842. (let ((rest (delq (car mem) children)))
  843. (if (vectorp val)
  844. (aset val 3 rest)
  845. (put prefix 'transient--layout rest))
  846. (setq val nil))
  847. (setq val (if loc (car mem) mem)))))
  848. (setq loc (car loc)))
  849. (if loc
  850. (transient--layout-member-1 (transient--kbd loc) val remove)
  851. val)))
  852. (defun transient--layout-member-1 (loc layout remove)
  853. (cond ((listp layout)
  854. (seq-some (lambda (elt) (transient--layout-member-1 loc elt remove))
  855. layout))
  856. ((vectorp (car (aref layout 3)))
  857. (seq-some (lambda (elt) (transient--layout-member-1 loc elt remove))
  858. (aref layout 3)))
  859. (remove
  860. (aset layout 3
  861. (delq (car (transient--group-member loc layout))
  862. (aref layout 3)))
  863. nil)
  864. (t (transient--group-member loc layout))))
  865. (defun transient--group-member (loc group)
  866. (cl-member-if (lambda (suffix)
  867. (and (listp suffix)
  868. (let* ((def (nth 2 suffix))
  869. (cmd (plist-get def :command)))
  870. (if (symbolp loc)
  871. (eq cmd loc)
  872. (equal (transient--kbd
  873. (or (plist-get def :key)
  874. (transient--command-key cmd)))
  875. loc)))))
  876. (aref group 3)))
  877. (defun transient--kbd (keys)
  878. (when (vectorp keys)
  879. (setq keys (key-description keys)))
  880. (when (stringp keys)
  881. (setq keys (kbd keys)))
  882. keys)
  883. (defun transient--spec-key (spec)
  884. (let ((plist (nth 2 spec)))
  885. (or (plist-get plist :key)
  886. (transient--command-key
  887. (plist-get plist :command)))))
  888. (defun transient--command-key (cmd)
  889. (when-let ((obj (get cmd 'transient--suffix)))
  890. (cond ((slot-boundp obj 'key)
  891. (oref obj key))
  892. ((slot-exists-p obj 'shortarg)
  893. (if (slot-boundp obj 'shortarg)
  894. (oref obj shortarg)
  895. (transient--derive-shortarg (oref obj argument)))))))
  896. (defun transient--nthcdr (n list)
  897. (nthcdr (if (< n 0) (- (length list) (abs n)) n) list))
  898. ;;; Variables
  899. (defvar current-transient-prefix nil
  900. "The transient from which this suffix command was invoked.
  901. This is an object representing that transient, use
  902. `current-transient-command' to get the respective command.")
  903. (defvar current-transient-command nil
  904. "The transient from which this suffix command was invoked.
  905. This is a symbol representing that transient, use
  906. `current-transient-object' to get the respective object.")
  907. (defvar current-transient-suffixes nil
  908. "The suffixes of the transient from which this suffix command was invoked.
  909. This is a list of objects. Usually it is sufficient to instead
  910. use the function `transient-args', which returns a list of
  911. values. In complex cases it might be necessary to use this
  912. variable instead.")
  913. (defvar post-transient-hook nil
  914. "Hook run after exiting a transient.")
  915. (defvar transient--prefix nil)
  916. (defvar transient--layout nil)
  917. (defvar transient--suffixes nil)
  918. (defconst transient--stay t "Do not exit the transient.")
  919. (defconst transient--exit nil "Do exit the transient.")
  920. (defvar transient--exitp nil "Whether to exit the transient.")
  921. (defvar transient--showp nil "Whether the transient is show in a popup buffer.")
  922. (defvar transient--helpp nil "Whether help-mode is active.")
  923. (defvar transient--editp nil "Whether edit-mode is active.")
  924. (defvar transient--active-infix nil "The active infix awaiting user input.")
  925. (defvar transient--timer nil)
  926. (defvar transient--stack nil)
  927. (defvar transient--buffer-name " *transient*"
  928. "Name of the transient buffer.")
  929. (defvar transient--window nil
  930. "The window used to display the transient popup.")
  931. (defvar transient--original-window nil
  932. "The window that was selected before the transient was invoked.
  933. Usually it remains selected while the transient is active.")
  934. (define-obsolete-variable-alias 'transient--source-buffer
  935. 'transient--original-buffer "Transient 0.2.0")
  936. (defvar transient--original-buffer nil
  937. "The buffer that was current before the transient was invoked.
  938. Usually it remains current while the transient is active.")
  939. (defvar transient--debug nil "Whether put debug information into *Messages*.")
  940. (defvar transient--history nil)
  941. ;;; Identities
  942. (defun transient-suffix-object (&optional command)
  943. "Return the object associated with the current suffix command.
  944. Each suffix commands is associated with an object, which holds
  945. additional information about the suffix, such as its value (in
  946. the case of an infix command, which is a kind of suffix command).
  947. This function is intended to be called by infix commands, whose
  948. command definition usually (at least when defined using
  949. `define-infix-command') is this:
  950. (lambda ()
  951. (interactive)
  952. (let ((obj (transient-suffix-object)))
  953. (transient-infix-set obj (transient-infix-read obj)))
  954. (transient--show))
  955. \(User input is read outside of `interactive' to prevent the
  956. command from being added to `command-history'. See #23.)
  957. Such commands need to be able to access their associated object
  958. to guide how `transient-infix-read' reads the new value and to
  959. store the read value. Other suffix commands (including non-infix
  960. commands) may also need the object to guide their behavior.
  961. This function attempts to return the object associated with the
  962. current suffix command even if the suffix command was not invoked
  963. from a transient. (For some suffix command that is a valid thing
  964. to do, for others it is not.) In that case nil may be returned
  965. if the command was not defined using one of the macros intended
  966. to define such commands.
  967. The optional argument COMMAND is intended for internal use. If
  968. you are contemplating using it in your own code, then you should
  969. probably use this instead:
  970. (get COMMAND 'transient--suffix)"
  971. (if transient--prefix
  972. (cl-find-if (lambda (obj)
  973. (eq (transient--suffix-command obj)
  974. (or command this-original-command)))
  975. transient--suffixes)
  976. (when-let ((obj (get (or command this-command) 'transient--suffix))
  977. (obj (clone obj)))
  978. (transient-init-scope obj)
  979. (transient-init-value obj)
  980. obj)))
  981. (defun transient--suffix-command (arg)
  982. "Return the command specified by ARG.
  983. Given a suffix specified by ARG, this function returns the
  984. respective command or a symbol that represents it. It could
  985. therefore be considered the inverse of `transient-suffix-object'.
  986. Unlike that function it is only intended for internal use though,
  987. and it is more complicated to describe because of some internal
  988. tricks it has to account for. You do not actually have to know
  989. any of this.
  990. ARG can be a `transient-suffix' object, a symbol representing a
  991. command, or a command (which can be either a fbound symbol or a
  992. lambda expression).
  993. If it is an object, then the value of its `command' slot is used
  994. as follows. If ARG satisfies `commandp', then that is returned.
  995. Otherwise it is assumed to be a symbol that merely represents the
  996. command. In that case the lambda expression that is stored in
  997. the symbols `transient--infix-command' property is returned.
  998. Therefore, if ARG is an object, then this function always returns
  999. something that is callable as a command.
  1000. ARG can also be something that is callable as a function. If it
  1001. is a symbol, then that is returned. Otherwise it is a lambda
  1002. expression and a symbol that merely representing that command is
  1003. returned.
  1004. Therefore, if ARG is something that is callable as a command,
  1005. then this function always returns a symbol that is, or merely
  1006. represents that command.
  1007. The reason that there are \"symbols that merely represent a
  1008. command\" is that by avoiding binding a symbol as a command we
  1009. can prevent it from being offered as a completion candidate for
  1010. `execute-extended-command'. That is useful for infix arguments,
  1011. which usually do not work correctly unless called from a
  1012. transient. Unfortunately this only works for infix arguments
  1013. that are defined inline in the definition of a transient prefix
  1014. command; explicitly defined infix arguments continue to pollute
  1015. the command namespace. It would be better if all this were made
  1016. unnecessary by a `execute-extended-command-ignore' symbol property
  1017. but unfortunately that does not exist (yet?)."
  1018. (if (transient-suffix--eieio-childp arg)
  1019. (let ((sym (oref arg command)))
  1020. (if (commandp sym)
  1021. sym
  1022. (get sym 'transient--infix-command)))
  1023. (if (symbolp arg)
  1024. arg
  1025. ;; ARG is an interactive lambda. The symbol returned by this
  1026. ;; is not actually a command, just a symbol representing it
  1027. ;; for purposes other than invoking it as a command.
  1028. (oref (transient-suffix-object) command))))
  1029. ;;; Keymaps
  1030. (defvar transient-base-map
  1031. (let ((map (make-sparse-keymap)))
  1032. (define-key map (kbd "ESC ESC ESC") 'transient-quit-all)
  1033. (define-key map (kbd "C-g") 'transient-quit-one)
  1034. (define-key map (kbd "C-q") 'transient-quit-all)
  1035. (define-key map (kbd "C-z") 'transient-suspend)
  1036. (define-key map (kbd "C-v") 'transient-scroll-up)
  1037. (define-key map (kbd "M-v") 'transient-scroll-down)
  1038. (define-key map [next] 'transient-scroll-up)
  1039. (define-key map [prior] 'transient-scroll-down)
  1040. map)
  1041. "Parent of other keymaps used by Transient.
  1042. This is the parent keymap of all the keymaps that are used in
  1043. all transients: `transient-map' (which in turn is the parent
  1044. of the transient-specific keymaps), `transient-edit-map' and
  1045. `transient-sticky-map'.
  1046. If you change a binding here, then you might also have to edit
  1047. `transient-sticky-map' and `transient-common-commands'. While
  1048. the latter isn't a proper transient prefix command, it can be
  1049. edited using the same functions as used for transients.")
  1050. (defvar transient-map
  1051. (let ((map (make-sparse-keymap)))
  1052. (set-keymap-parent map transient-base-map)
  1053. (define-key map (kbd "C-p") 'universal-argument)
  1054. (define-key map (kbd "C--") 'negative-argument)
  1055. (define-key map (kbd "C-t") 'transient-show)
  1056. (define-key map (kbd "?") 'transient-help)
  1057. (define-key map (kbd "C-h") 'transient-help)
  1058. (define-key map (kbd "M-p") 'transient-history-prev)
  1059. (define-key map (kbd "M-n") 'transient-history-next)
  1060. map)
  1061. "Top-level keymap used by all transients.")
  1062. (defvar transient-edit-map
  1063. (let ((map (make-sparse-keymap)))
  1064. (set-keymap-parent map transient-base-map)
  1065. (define-key map (kbd "?") 'transient-help)
  1066. (define-key map (kbd "C-h") 'transient-help)
  1067. (define-key map (kbd "C-x l") 'transient-set-level)
  1068. map)
  1069. "Keymap that is active while a transient in is in \"edit mode\".")
  1070. (defvar transient-sticky-map
  1071. (let ((map (make-sparse-keymap)))
  1072. (set-keymap-parent map transient-base-map)
  1073. (define-key map (kbd "C-g") 'transient-quit-seq)
  1074. map)
  1075. "Keymap that is active while an incomplete key sequence is active.")
  1076. (defvar transient--common-command-prefixes '(?\C-x))
  1077. (put 'transient-common-commands
  1078. 'transient--layout
  1079. (cl-mapcan
  1080. (lambda (s) (transient--parse-child 'transient-common-commands s))
  1081. '([:hide (lambda ()
  1082. (and (not (memq (car transient--redisplay-key)
  1083. transient--common-command-prefixes))
  1084. (not transient-show-common-commands)))
  1085. ["Value commands"
  1086. ("C-x s " "Set" transient-set)
  1087. ("C-x C-s" "Save" transient-save)
  1088. ("M-p " "Previous value" transient-history-prev)
  1089. ("M-n " "Next value" transient-history-next)]
  1090. ["Sticky commands"
  1091. ;; Like `transient-sticky-map' except that
  1092. ;; "C-g" has to be bound to a different command.
  1093. ("C-g" "Quit prefix or transient" transient-quit-one)
  1094. ("C-q" "Quit transient stack" transient-quit-all)
  1095. ("C-z" "Suspend transient stack" transient-suspend)]
  1096. ["Customize"
  1097. ("C-x t" transient-toggle-common
  1098. :description (lambda ()
  1099. (if transient-show-common-commands
  1100. "Hide common commands"
  1101. "Show common permanently")))
  1102. ("C-x l" "Show/hide suffixes" transient-set-level)]])))
  1103. (defvar transient-predicate-map
  1104. (let ((map (make-sparse-keymap)))
  1105. (define-key map [handle-switch-frame] 'transient--do-suspend)
  1106. (define-key map [transient-suspend] 'transient--do-suspend)
  1107. (define-key map [transient-help] 'transient--do-stay)
  1108. (define-key map [transient-set-level] 'transient--do-stay)
  1109. (define-key map [transient-history-prev] 'transient--do-stay)
  1110. (define-key map [transient-history-next] 'transient--do-stay)
  1111. (define-key map [universal-argument] 'transient--do-stay)
  1112. (define-key map [negative-argument] 'transient--do-stay)
  1113. (define-key map [digit-argument] 'transient--do-stay)
  1114. (define-key map [transient-quit-all] 'transient--do-quit-all)
  1115. (define-key map [transient-quit-one] 'transient--do-quit-one)
  1116. (define-key map [transient-quit-seq] 'transient--do-stay)
  1117. (define-key map [transient-show] 'transient--do-stay)
  1118. (define-key map [transient-update] 'transient--do-stay)
  1119. (define-key map [transient-toggle-common] 'transient--do-stay)
  1120. (define-key map [transient-set] 'transient--do-call)
  1121. (define-key map [transient-save] 'transient--do-call)
  1122. (define-key map [describe-key-briefly] 'transient--do-stay)
  1123. (define-key map [describe-key] 'transient--do-stay)
  1124. (define-key map [transient-scroll-up] 'transient--do-stay)
  1125. (define-key map [transient-scroll-down] 'transient--do-stay)
  1126. (define-key map [mwheel-scroll] 'transient--do-stay)
  1127. (define-key map [transient-noop] 'transient--do-noop)
  1128. (define-key map [transient-mouse-push-button] 'transient--do-move)
  1129. (define-key map [transient-push-button] 'transient--do-move)
  1130. (define-key map [transient-backward-button] 'transient--do-move)
  1131. (define-key map [transient-forward-button] 'transient--do-move)
  1132. (define-key map [transient-isearch-backward] 'transient--do-move)
  1133. (define-key map [transient-isearch-forward] 'transient--do-move)
  1134. map)
  1135. "Base keymap used to map common commands to their transient behavior.
  1136. The \"transient behavior\" of a command controls, among other
  1137. things, whether invoking the command causes the transient to be
  1138. exited or not and whether infix arguments are exported before
  1139. doing so.
  1140. Each \"key\" is a command that is common to all transients and
  1141. that is bound in `transient-map', `transient-edit-map',
  1142. `transient-sticky-map' and/or `transient-common-command'.
  1143. Each binding is a \"pre-command\", a function that controls the
  1144. transient behavior of the respective command.
  1145. For transient commands that are bound in individual transients,
  1146. the transient behavior is specified using the `:transient' slot
  1147. of the corresponding object.")
  1148. (defvar transient-popup-navigation-map)
  1149. (defvar transient--transient-map nil)
  1150. (defvar transient--predicate-map nil)
  1151. (defvar transient--redisplay-map nil)
  1152. (defvar transient--redisplay-key nil)
  1153. (defun transient--push-keymap (map)
  1154. (transient--debug " push %s%s" map (if (symbol-value map) "" " VOID"))
  1155. (with-demoted-errors "transient--push-keymap: %S"
  1156. (internal-push-keymap (symbol-value map) 'overriding-terminal-local-map)))
  1157. (defun transient--pop-keymap (map)
  1158. (transient--debug " pop %s%s" map (if (symbol-value map) "" " VOID"))
  1159. (with-demoted-errors "transient--pop-keymap: %S"
  1160. (internal-pop-keymap (symbol-value map) 'overriding-terminal-local-map)))
  1161. (defun transient--make-transient-map ()
  1162. (let ((map (make-sparse-keymap)))
  1163. (set-keymap-parent map (if transient--editp
  1164. transient-edit-map
  1165. transient-map))
  1166. (dolist (obj transient--suffixes)
  1167. (let ((key (oref obj key)))
  1168. (when (vectorp key)
  1169. (setq key (key-description key))
  1170. (oset obj key key))
  1171. (when transient-substitute-key-function
  1172. (setq key (save-match-data
  1173. (funcall transient-substitute-key-function obj)))
  1174. (oset obj key key))
  1175. (let ((kbd (kbd key))
  1176. (cmd (transient--suffix-command obj)))
  1177. (when-let ((conflict (and transient-detect-key-conflicts
  1178. (transient--lookup-key map kbd))))
  1179. (unless (eq cmd conflict)
  1180. (transient--emergency-exit)
  1181. (error "Cannot bind %S to %s and also %s"
  1182. (string-trim key)
  1183. cmd conflict)))
  1184. (define-key map kbd cmd))))
  1185. (when transient-enable-popup-navigation
  1186. (setq map
  1187. (make-composed-keymap (list map transient-popup-navigation-map))))
  1188. map))
  1189. (defun transient--make-predicate-map ()
  1190. (let ((map (make-sparse-keymap)))
  1191. (set-keymap-parent map transient-predicate-map)
  1192. (dolist (obj transient--suffixes)
  1193. (let* ((cmd (transient--suffix-command obj))
  1194. (sub-prefix (and (symbolp cmd) (get cmd 'transient--prefix))))
  1195. (if (slot-boundp obj 'transient)
  1196. (define-key map (vector cmd)
  1197. (let ((do (oref obj transient)))
  1198. (pcase do
  1199. (`t (if sub-prefix
  1200. 'transient--do-replace
  1201. 'transient--do-stay))
  1202. (`nil 'transient--do-exit)
  1203. (_ do))))
  1204. (unless (lookup-key transient-predicate-map (vector cmd))
  1205. (define-key map (vector cmd)
  1206. (if sub-prefix
  1207. 'transient--do-replace
  1208. (or (oref transient--prefix transient-suffix)
  1209. 'transient--do-exit)))))))
  1210. map))
  1211. (defun transient--make-redisplay-map ()
  1212. (setq transient--redisplay-key
  1213. (cl-case this-command
  1214. (transient-update
  1215. (setq transient--showp t)
  1216. (setq unread-command-events
  1217. (listify-key-sequence (this-single-command-raw-keys))))
  1218. (transient-quit-seq
  1219. (setq unread-command-events
  1220. (butlast (listify-key-sequence
  1221. (this-single-command-raw-keys))
  1222. 2))
  1223. (butlast transient--redisplay-key))
  1224. (t nil)))
  1225. (let ((topmap (make-sparse-keymap))
  1226. (submap (make-sparse-keymap)))
  1227. (when transient--redisplay-key
  1228. (define-key topmap (vconcat transient--redisplay-key) submap)
  1229. (set-keymap-parent submap transient-sticky-map))
  1230. (map-keymap-internal
  1231. (lambda (key def)
  1232. (when (and (not (eq key ?\e))
  1233. (listp def)
  1234. (keymapp def))
  1235. (define-key topmap (vconcat transient--redisplay-key (list key))
  1236. 'transient-update)))
  1237. (if transient--redisplay-key
  1238. (lookup-key transient--transient-map (vconcat transient--redisplay-key))
  1239. transient--transient-map))
  1240. topmap))
  1241. ;;; Setup
  1242. (defun transient-setup (&optional name layout edit &rest params)
  1243. "Setup the transient specified by NAME.
  1244. This function is called by transient prefix commands to setup the
  1245. transient. In that case NAME is mandatory, LAYOUT and EDIT must
  1246. be nil and PARAMS may be (but usually is not) used to set e.g. the
  1247. \"scope\" of the transient (see `transient-define-prefix').
  1248. This function is also called internally in which case LAYOUT and
  1249. EDIT may be non-nil."
  1250. (transient--debug 'setup)
  1251. (cond
  1252. ((not name)
  1253. ;; Switching between regular and edit mode.
  1254. (transient--pop-keymap 'transient--transient-map)
  1255. (transient--pop-keymap 'transient--redisplay-map)
  1256. (setq name (oref transient--prefix command))
  1257. (setq params (list :scope (oref transient--prefix scope))))
  1258. ((not (or layout ; resuming parent/suspended prefix
  1259. current-transient-command)) ; entering child prefix
  1260. (transient--stack-zap)) ; replace suspended prefix, if any
  1261. (edit
  1262. ;; Returning from help to edit.
  1263. (setq transient--editp t)))
  1264. (transient--init-objects name layout params)
  1265. (transient--history-init transient--prefix)
  1266. (setq transient--predicate-map (transient--make-predicate-map))
  1267. (setq transient--transient-map (transient--make-transient-map))
  1268. (setq transient--redisplay-map (transient--make-redisplay-map))
  1269. (setq transient--original-window (selected-window))
  1270. (setq transient--original-buffer (current-buffer))
  1271. (transient--redisplay)
  1272. (transient--init-transient)
  1273. (transient--suspend-which-key-mode))
  1274. (defun transient--init-objects (name layout params)
  1275. (setq transient--prefix
  1276. (let ((proto (get name 'transient--prefix)))
  1277. (apply #'clone proto
  1278. :prototype proto
  1279. :level (or (alist-get
  1280. t (alist-get name transient-levels))
  1281. transient-default-level)
  1282. params)))
  1283. (transient-init-value transient--prefix)
  1284. (setq transient--layout
  1285. (or layout
  1286. (let ((levels (alist-get name transient-levels)))
  1287. (cl-mapcan (lambda (c) (transient--init-child levels c))
  1288. (append (get name 'transient--layout)
  1289. (and (not transient--editp)
  1290. (get 'transient-common-commands
  1291. 'transient--layout)))))))
  1292. (setq transient--suffixes
  1293. (cl-labels ((s (def)
  1294. (cond
  1295. ((stringp def) nil)
  1296. ((listp def) (cl-mapcan #'s def))
  1297. ((transient-group--eieio-childp def)
  1298. (cl-mapcan #'s (oref def suffixes)))
  1299. ((transient-suffix--eieio-childp def)
  1300. (list def)))))
  1301. (cl-mapcan #'s transient--layout))))
  1302. (defun transient--init-child (levels spec)
  1303. (cl-etypecase spec
  1304. (vector (transient--init-group levels spec))
  1305. (list (transient--init-suffix levels spec))
  1306. (string (list spec))))
  1307. (defun transient--init-group (levels spec)
  1308. (pcase-let ((`(,level ,class ,args ,children) (append spec nil)))
  1309. (when (transient--use-level-p level)
  1310. (let ((obj (apply class :level level args)))
  1311. (when (transient--use-suffix-p obj)
  1312. (when-let ((suffixes
  1313. (cl-mapcan (lambda (c) (transient--init-child levels c))
  1314. children)))
  1315. (oset obj suffixes suffixes)
  1316. (list obj)))))))
  1317. (defun transient--init-suffix (levels spec)
  1318. (pcase-let* ((`(,level ,class ,args) spec)
  1319. (cmd (plist-get args :command))
  1320. (level (or (alist-get (transient--suffix-command cmd) levels)
  1321. level)))
  1322. (let ((fn (and (symbolp cmd)
  1323. (symbol-function cmd))))
  1324. (when (autoloadp fn)
  1325. (transient--debug " autoload %s" cmd)
  1326. (autoload-do-load fn)))
  1327. (when (transient--use-level-p level)
  1328. (let ((obj (if-let ((proto (and cmd
  1329. (symbolp cmd)
  1330. (get cmd 'transient--suffix))))
  1331. (apply #'clone proto :level level args)
  1332. (apply class :level level args))))
  1333. (transient--init-suffix-key obj)
  1334. (transient--ensure-infix-command obj)
  1335. (when (transient--use-suffix-p obj)
  1336. (transient-init-scope obj)
  1337. (transient-init-value obj)
  1338. (list obj))))))
  1339. (cl-defmethod transient--init-suffix-key ((obj transient-suffix))
  1340. (unless (slot-boundp obj 'key)
  1341. (error "No key for %s" (oref obj command))))
  1342. (cl-defmethod transient--init-suffix-key ((obj transient-argument))
  1343. (if (transient-switches--eieio-childp obj)
  1344. (cl-call-next-method obj)
  1345. (unless (slot-boundp obj 'shortarg)
  1346. (when-let ((shortarg (transient--derive-shortarg (oref obj argument))))
  1347. (oset obj shortarg shortarg)))
  1348. (unless (slot-boundp obj 'key)
  1349. (if (slot-boundp obj 'shortarg)
  1350. (oset obj key (oref obj shortarg))
  1351. (error "No key for %s" (oref obj command))))))
  1352. (defun transient--use-level-p (level &optional edit)
  1353. (or (and transient--editp (not edit))
  1354. (and (>= level 1)
  1355. (<= level (oref transient--prefix level)))))
  1356. (defun transient--use-suffix-p (obj)
  1357. (with-slots
  1358. (if if-not if-nil if-non-nil if-mode if-not-mode if-derived if-not-derived)
  1359. obj
  1360. (cond
  1361. (if (funcall if))
  1362. (if-not (not (funcall if-not)))
  1363. (if-non-nil (symbol-value if-non-nil))
  1364. (if-nil (not (symbol-value if-nil)))
  1365. (if-mode (if (atom if-mode)
  1366. (eq major-mode if-mode)
  1367. (memq major-mode if-mode)))
  1368. (if-not-mode (not (if (atom if-not-mode)
  1369. (eq major-mode if-not-mode)
  1370. (memq major-mode if-not-mode))))
  1371. (if-derived (if (atom if-derived)
  1372. (derived-mode-p if-derived)
  1373. (apply #'derived-mode-p if-derived)))
  1374. (if-not-derived (not (if (atom if-not-derived)
  1375. (derived-mode-p if-not-derived)
  1376. (apply #'derived-mode-p if-not-derived))))
  1377. (t))))
  1378. ;;; Flow-Control
  1379. (defun transient--init-transient ()
  1380. (transient--debug 'init-transient)
  1381. (transient--push-keymap 'transient--transient-map)
  1382. (transient--push-keymap 'transient--redisplay-map)
  1383. (add-hook 'pre-command-hook #'transient--pre-command)
  1384. (add-hook 'minibuffer-setup-hook #'transient--minibuffer-setup)
  1385. (add-hook 'minibuffer-exit-hook #'transient--minibuffer-exit)
  1386. (add-hook 'post-command-hook #'transient--post-command)
  1387. (advice-add 'abort-recursive-edit :after #'transient--minibuffer-exit)
  1388. (when transient--exitp
  1389. ;; This prefix command was invoked as the suffix of another.
  1390. ;; Prevent `transient--post-command' from removing the hooks
  1391. ;; that we just added.
  1392. (setq transient--exitp 'replace)))
  1393. (defun transient--pre-command ()
  1394. (transient--debug 'pre-command)
  1395. (cond
  1396. ((memq this-command '(transient-update transient-quit-seq))
  1397. (transient--pop-keymap 'transient--redisplay-map))
  1398. ((and transient--helpp
  1399. (not (memq this-command '(transient-quit-one
  1400. transient-quit-all))))
  1401. (cond
  1402. ((transient-help)
  1403. (transient--do-suspend)
  1404. (setq this-command 'transient-suspend)
  1405. (transient--pre-exit))
  1406. ((not (transient--edebug-command-p))
  1407. (setq this-command 'transient-undefined))))
  1408. ((and transient--editp
  1409. (transient-suffix-object)
  1410. (not (memq this-command '(transient-quit-one
  1411. transient-quit-all
  1412. transient-help))))
  1413. (setq this-command 'transient-set-level))
  1414. (t
  1415. (setq transient--exitp nil)
  1416. (when (eq (if-let ((fn (or (lookup-key transient--predicate-map
  1417. (vector this-original-command))
  1418. (oref transient--prefix transient-non-suffix))))
  1419. (let ((action (funcall fn)))
  1420. (when (eq action transient--exit)
  1421. (setq transient--exitp (or transient--exitp t)))
  1422. action)
  1423. (if (let ((keys (this-command-keys-vector)))
  1424. (eq (aref keys (1- (length keys))) ?\C-g))
  1425. (setq this-command 'transient-noop)
  1426. (unless (transient--edebug-command-p)
  1427. (setq this-command 'transient-undefined)))
  1428. transient--stay)
  1429. transient--exit)
  1430. (transient--pre-exit)))))
  1431. (defun transient--pre-exit ()
  1432. (transient--debug 'pre-exit)
  1433. (transient--delete-window)
  1434. (transient--timer-cancel)
  1435. (transient--pop-keymap 'transient--transient-map)
  1436. (transient--pop-keymap 'transient--redisplay-map)
  1437. (remove-hook 'pre-command-hook #'transient--pre-command)
  1438. (unless transient--showp
  1439. (message ""))
  1440. (setq transient--transient-map nil)
  1441. (setq transient--predicate-map nil)
  1442. (setq transient--redisplay-map nil)
  1443. (setq transient--redisplay-key nil)
  1444. (setq transient--showp nil)
  1445. (setq transient--helpp nil)
  1446. (setq transient--editp nil)
  1447. (setq transient--prefix nil)
  1448. (setq transient--layout nil)
  1449. (setq transient--suffixes nil)
  1450. (setq transient--original-window nil)
  1451. (setq transient--original-buffer nil)
  1452. (setq transient--window nil))
  1453. (defun transient--delete-window ()
  1454. (when (window-live-p transient--window)
  1455. (let ((buf (window-buffer transient--window)))
  1456. (with-demoted-errors "Error while exiting transient: %S"
  1457. (delete-window transient--window))
  1458. (kill-buffer buf))))
  1459. (defun transient--export ()
  1460. (setq current-transient-prefix transient--prefix)
  1461. (setq current-transient-command (oref transient--prefix command))
  1462. (setq current-transient-suffixes transient--suffixes)
  1463. (transient--history-push transient--prefix))
  1464. (defun transient--minibuffer-setup ()
  1465. (transient--debug 'minibuffer-setup)
  1466. (unless (> (minibuffer-depth) 1)
  1467. (unless transient--exitp
  1468. (transient--pop-keymap 'transient--transient-map)
  1469. (transient--pop-keymap 'transient--redisplay-map)
  1470. (remove-hook 'pre-command-hook #'transient--pre-command))
  1471. (remove-hook 'post-command-hook #'transient--post-command)))
  1472. (defun transient--minibuffer-exit ()
  1473. (transient--debug 'minibuffer-exit)
  1474. (unless (> (minibuffer-depth) 1)
  1475. (unless transient--exitp
  1476. (transient--push-keymap 'transient--transient-map)
  1477. (transient--push-keymap 'transient--redisplay-map)
  1478. (add-hook 'pre-command-hook #'transient--pre-command))
  1479. (add-hook 'post-command-hook #'transient--post-command)))
  1480. (defun transient--suspend-override (&optional minibuffer-hooks)
  1481. (transient--debug 'suspend-override)
  1482. (transient--pop-keymap 'transient--transient-map)
  1483. (transient--pop-keymap 'transient--redisplay-map)
  1484. (remove-hook 'pre-command-hook #'transient--pre-command)
  1485. (remove-hook 'post-command-hook #'transient--post-command)
  1486. (when minibuffer-hooks
  1487. (remove-hook 'minibuffer-setup-hook #'transient--minibuffer-setup)
  1488. (remove-hook 'minibuffer-exit-hook #'transient--minibuffer-exit)
  1489. (advice-remove 'abort-recursive-edit #'transient--minibuffer-exit)))
  1490. (defun transient--resume-override (&optional minibuffer-hooks)
  1491. (transient--debug 'resume-override)
  1492. (transient--push-keymap 'transient--transient-map)
  1493. (transient--push-keymap 'transient--redisplay-map)
  1494. (add-hook 'pre-command-hook #'transient--pre-command)
  1495. (add-hook 'post-command-hook #'transient--post-command)
  1496. (when minibuffer-hooks
  1497. (add-hook 'minibuffer-setup-hook #'transient--minibuffer-setup)
  1498. (add-hook 'minibuffer-exit-hook #'transient--minibuffer-exit)
  1499. (advice-add 'abort-recursive-edit :after #'transient--minibuffer-exit)))
  1500. (defun transient--post-command ()
  1501. (transient--debug 'post-command)
  1502. (if transient--exitp
  1503. (progn
  1504. (unless (and (eq transient--exitp 'replace)
  1505. (or transient--prefix
  1506. ;; The current command could act as a prefix,
  1507. ;; but decided not to call `transient-setup'.
  1508. (prog1 nil (transient--stack-zap))))
  1509. (remove-hook 'minibuffer-setup-hook #'transient--minibuffer-setup)
  1510. (remove-hook 'minibuffer-exit-hook #'transient--minibuffer-exit)
  1511. (advice-remove 'abort-recursive-edit #'transient--minibuffer-exit)
  1512. (remove-hook 'post-command-hook #'transient--post-command))
  1513. (setq current-transient-prefix nil)
  1514. (setq current-transient-command nil)
  1515. (setq current-transient-suffixes nil)
  1516. (let ((resume (and transient--stack
  1517. (not (memq transient--exitp '(replace suspend))))))
  1518. (setq transient--exitp nil)
  1519. (setq transient--helpp nil)
  1520. (setq transient--editp nil)
  1521. (run-hooks 'post-transient-hook)
  1522. (when resume
  1523. (transient--stack-pop))))
  1524. (transient--pop-keymap 'transient--redisplay-map)
  1525. (setq transient--redisplay-map (transient--make-redisplay-map))
  1526. (transient--push-keymap 'transient--redisplay-map)
  1527. (unless (eq this-command (oref transient--prefix command))
  1528. (transient--redisplay))))
  1529. (defun transient--stack-push ()
  1530. (transient--debug 'stack-push)
  1531. (push (list (oref transient--prefix command)
  1532. transient--layout
  1533. transient--editp
  1534. :scope (oref transient--prefix scope))
  1535. transient--stack))
  1536. (defun transient--stack-pop ()
  1537. (transient--debug 'stack-pop)
  1538. (and transient--stack
  1539. (prog1 t (apply #'transient-setup (pop transient--stack)))))
  1540. (defun transient--stack-zap ()
  1541. (transient--debug 'stack-zap)
  1542. (setq transient--stack nil))
  1543. (defun transient--redisplay ()
  1544. (if (or (eq transient-show-popup t)
  1545. transient--showp)
  1546. (unless (memq this-command '(transient-scroll-up
  1547. transient-scroll-down
  1548. mwheel-scroll))
  1549. (transient--show))
  1550. (when (and (numberp transient-show-popup)
  1551. (not (zerop transient-show-popup))
  1552. (not transient--timer))
  1553. (transient--timer-start))
  1554. (transient--show-brief)))
  1555. (defun transient--timer-start ()
  1556. (setq transient--timer
  1557. (run-at-time (abs transient-show-popup) nil
  1558. (lambda ()
  1559. (transient--timer-cancel)
  1560. (transient--show)
  1561. (let ((message-log-max nil))
  1562. (message ""))))))
  1563. (defun transient--timer-cancel ()
  1564. (when transient--timer
  1565. (cancel-timer transient--timer)
  1566. (setq transient--timer nil)))
  1567. (defun transient--debug (arg &rest args)
  1568. (when transient--debug
  1569. (if (symbolp arg)
  1570. (message "-- %-16s (cmd: %s, exit: %s)"
  1571. arg this-command transient--exitp)
  1572. (apply #'message arg args))))
  1573. (defun transient--emergency-exit ()
  1574. "Exit the current transient command after an error occurred.
  1575. Beside being used with `condition-case', this function also has
  1576. to be a member of `debugger-mode-hook', else the debugger would
  1577. be unusable and exiting it by pressing \"q\" would fail because
  1578. the transient command would still be active and that key would
  1579. either be unbound or do something else.
  1580. When no transient is active (i.e. when `transient--prefix') is
  1581. nil, then do nothing."
  1582. (when transient--prefix
  1583. (setq transient--stack nil)
  1584. (setq transient--exitp t)
  1585. (transient--pre-exit)
  1586. (transient--post-command)))
  1587. (add-hook 'debugger-mode-hook 'transient--emergency-exit)
  1588. (defmacro transient--with-emergency-exit (&rest body)
  1589. (declare (indent defun))
  1590. `(condition-case nil
  1591. ,(macroexp-progn body)
  1592. (error (transient--emergency-exit))))
  1593. ;;; Pre-Commands
  1594. (defun transient--do-stay ()
  1595. "Call the command without exporting variables and stay transient."
  1596. transient--stay)
  1597. (defun transient--do-noop ()
  1598. "Call `transient-noop' and stay transient."
  1599. (setq this-command 'transient-noop)
  1600. transient--stay)
  1601. (defun transient--do-warn ()
  1602. "Call `transient-undefined' and stay transient."
  1603. (setq this-command 'transient-undefined)
  1604. transient--stay)
  1605. (defun transient--do-call ()
  1606. "Call the command after exporting variables and stay transient."
  1607. (transient--export)
  1608. transient--stay)
  1609. (defun transient--do-exit ()
  1610. "Call the command after exporting variables and exit the transient."
  1611. (transient--export)
  1612. (transient--stack-zap)
  1613. transient--exit)
  1614. (defun transient--do-replace ()
  1615. "Call the transient prefix command, replacing the active transient."
  1616. (transient--export)
  1617. (transient--stack-push)
  1618. (setq transient--exitp 'replace)
  1619. transient--exit)
  1620. (defun transient--do-suspend ()
  1621. "Suspend the active transient, saving the transient stack."
  1622. (transient--stack-push)
  1623. (setq transient--exitp 'suspend)
  1624. transient--exit)
  1625. (defun transient--do-quit-one ()
  1626. "If active, quit help or edit mode, else exit the active transient."
  1627. (cond (transient--helpp
  1628. (setq transient--helpp nil)
  1629. transient--stay)
  1630. (transient--editp
  1631. (setq transient--editp nil)
  1632. (transient-setup)
  1633. transient--stay)
  1634. (t transient--exit)))
  1635. (defun transient--do-quit-all ()
  1636. "Exit all transients without saving the transient stack."
  1637. (transient--stack-zap)
  1638. transient--exit)
  1639. (defun transient--do-move ()
  1640. "Call the command if `transient-enable-popup-navigation' is non-nil.
  1641. In that case behave like `transient--do-stay', otherwise similar
  1642. to `transient--do-warn'."
  1643. (unless transient-enable-popup-navigation
  1644. (setq this-command 'transient-popup-navigation-help))
  1645. transient--stay)
  1646. ;;; Commands
  1647. (defun transient-noop ()
  1648. "Do nothing at all."
  1649. (interactive))
  1650. (defun transient-undefined ()
  1651. "Warn the user that the pressed key is not bound to any suffix."
  1652. (interactive)
  1653. (ding)
  1654. (message "Unbound suffix: `%s' (Use `%s' to abort, `%s' for help) [%s]"
  1655. (propertize (key-description (this-single-command-keys))
  1656. 'face 'font-lock-warning-face)
  1657. (propertize "C-g" 'face 'transient-key)
  1658. (propertize "?" 'face 'transient-key)
  1659. this-original-command))
  1660. (defun transient-toggle-common ()
  1661. "Toggle whether common commands are always shown."
  1662. (interactive)
  1663. (setq transient-show-common-commands (not transient-show-common-commands)))
  1664. (defun transient-suspend ()
  1665. "Suspend the current transient.
  1666. It can later be resumed using `transient-resume' while no other
  1667. transient is active."
  1668. (interactive))
  1669. (defun transient-quit-all ()
  1670. "Exit all transients without saving the transient stack."
  1671. (interactive))
  1672. (defun transient-quit-one ()
  1673. "Exit the current transients, possibly returning to the previous."
  1674. (interactive))
  1675. (defun transient-quit-seq ()
  1676. "Abort the current incomplete key sequence."
  1677. (interactive))
  1678. (defun transient-update ()
  1679. "Redraw the transient's state in the popup buffer."
  1680. (interactive))
  1681. (defun transient-show ()
  1682. "Show the transient's state in the popup buffer."
  1683. (interactive)
  1684. (setq transient--showp t))
  1685. (defvar-local transient--restore-winconf nil)
  1686. (defvar transient-resume-mode)
  1687. (defun transient-help ()
  1688. "Show help for the active transient or one of its suffixes."
  1689. (interactive)
  1690. (if (called-interactively-p 'any)
  1691. (setq transient--helpp t)
  1692. (with-demoted-errors "transient-help: %S"
  1693. (when (lookup-key transient--transient-map
  1694. (this-single-command-raw-keys))
  1695. (setq transient--helpp nil)
  1696. (let ((winconf (current-window-configuration)))
  1697. (transient-show-help
  1698. (if (eq this-original-command 'transient-help)
  1699. transient--prefix
  1700. (or (transient-suffix-object)
  1701. this-original-command)))
  1702. (setq transient--restore-winconf winconf))
  1703. (fit-window-to-buffer nil (frame-height) (window-height))
  1704. (transient-resume-mode)
  1705. (message "Type \"q\" to resume transient command.")
  1706. t))))
  1707. (defun transient-set-level (&optional command level)
  1708. "Set the level of the transient or one of its suffix commands."
  1709. (interactive
  1710. (let ((command this-original-command)
  1711. (prefix (oref transient--prefix command)))
  1712. (and (or (not (eq command 'transient-set-level))
  1713. (and transient--editp
  1714. (setq command prefix)))
  1715. (list command
  1716. (let ((keys (this-single-command-raw-keys)))
  1717. (and (lookup-key transient--transient-map keys)
  1718. (string-to-number
  1719. (let ((transient--active-infix
  1720. (transient-suffix-object)))
  1721. (transient--show)
  1722. (transient--read-number-N
  1723. (format "Set level for `%s': "
  1724. (transient--suffix-command command))
  1725. nil nil (not (eq command prefix)))))))))))
  1726. (cond
  1727. ((not command)
  1728. (setq transient--editp t)
  1729. (transient-setup))
  1730. (level
  1731. (let* ((prefix (oref transient--prefix command))
  1732. (alist (alist-get prefix transient-levels))
  1733. (key (transient--suffix-command command)))
  1734. (if (eq command prefix)
  1735. (progn (oset transient--prefix level level)
  1736. (setq key t))
  1737. (oset (transient-suffix-object command) level level))
  1738. (setf (alist-get key alist) level)
  1739. (setf (alist-get prefix transient-levels) alist))
  1740. (transient-save-levels))
  1741. (t
  1742. (transient-undefined))))
  1743. (defun transient-set ()
  1744. "Save the value of the active transient for this Emacs session."
  1745. (interactive)
  1746. (transient-set-value (or transient--prefix current-transient-prefix)))
  1747. (defun transient-save ()
  1748. "Save the value of the active transient persistenly across Emacs sessions."
  1749. (interactive)
  1750. (transient-save-value (or transient--prefix current-transient-prefix)))
  1751. (defun transient-history-next ()
  1752. "Switch to the next value used for the active transient."
  1753. (interactive)
  1754. (let* ((obj transient--prefix)
  1755. (pos (1- (oref obj history-pos)))
  1756. (hst (oref obj history)))
  1757. (if (< pos 0)
  1758. (user-error "End of history")
  1759. (oset obj history-pos pos)
  1760. (oset obj value (nth pos hst))
  1761. (mapc #'transient-init-value transient--suffixes))))
  1762. (defun transient-history-prev ()
  1763. "Switch to the previous value used for the active transient."
  1764. (interactive)
  1765. (let* ((obj transient--prefix)
  1766. (pos (1+ (oref obj history-pos)))
  1767. (hst (oref obj history))
  1768. (len (length hst)))
  1769. (if (> pos (1- len))
  1770. (user-error "End of history")
  1771. (oset obj history-pos pos)
  1772. (oset obj value (nth pos hst))
  1773. (mapc #'transient-init-value transient--suffixes))))
  1774. (defun transient-scroll-up (&optional arg)
  1775. "Scroll text of transient popup window upward ARG lines.
  1776. If ARG is nil scroll near full screen. This is a wrapper
  1777. around `scroll-up-command' (which see)."
  1778. (interactive "^P")
  1779. (with-selected-window transient--window
  1780. (scroll-up-command arg)))
  1781. (defun transient-scroll-down (&optional arg)
  1782. "Scroll text of transient popup window down ARG lines.
  1783. If ARG is nil scroll near full screen. This is a wrapper
  1784. around `scroll-down-command' (which see)."
  1785. (interactive "^P")
  1786. (with-selected-window transient--window
  1787. (scroll-down-command arg)))
  1788. (defun transient-resume ()
  1789. "Resume a previously suspended stack of transients."
  1790. (interactive)
  1791. (cond (transient--stack
  1792. (let ((winconf transient--restore-winconf))
  1793. (kill-local-variable 'transient--restore-winconf)
  1794. (when transient-resume-mode
  1795. (transient-resume-mode -1)
  1796. (quit-window))
  1797. (when winconf
  1798. (set-window-configuration winconf)))
  1799. (transient--stack-pop))
  1800. (transient-resume-mode
  1801. (kill-local-variable 'transient--restore-winconf)
  1802. (transient-resume-mode -1)
  1803. (quit-window))
  1804. (t
  1805. (message "No suspended transient command"))))
  1806. ;;; Value
  1807. ;;;; Init
  1808. (cl-defgeneric transient-init-scope (obj)
  1809. "Set the scope of the suffix object OBJ.
  1810. The scope is actually a property of the transient prefix, not of
  1811. individual suffixes. However it is possible to invoke a suffix
  1812. command directly instead of from a transient. In that case, if
  1813. the suffix expects a scope, then it has to determine that itself
  1814. and store it in its `scope' slot.
  1815. This function is called for all suffix commands, but unless a
  1816. concrete method is implemented this falls through to the default
  1817. implementation, which is a noop.")
  1818. (cl-defmethod transient-init-scope ((_ transient-suffix))
  1819. "Noop." nil)
  1820. (cl-defgeneric transient-init-value (_)
  1821. "Set the initial value of the object OBJ.
  1822. This function is called for all prefix and suffix commands.
  1823. For suffix commands (including infix argument commands) the
  1824. default implementation is a noop. Classes derived from the
  1825. abstract `transient-infix' class must implement this function.
  1826. Non-infix suffix commands usually don't have a value."
  1827. nil)
  1828. (cl-defmethod transient-init-value ((obj transient-prefix))
  1829. (if (slot-boundp obj 'value)
  1830. (oref obj value)
  1831. (oset obj value
  1832. (if-let ((saved (assq (oref obj command) transient-values)))
  1833. (cdr saved)
  1834. (if-let ((default (and (slot-boundp obj 'default-value)
  1835. (oref obj default-value))))
  1836. (if (functionp default)
  1837. (funcall default)
  1838. default)
  1839. nil)))))
  1840. (cl-defmethod transient-init-value ((obj transient-switch))
  1841. (oset obj value
  1842. (car (member (oref obj argument)
  1843. (oref transient--prefix value)))))
  1844. (cl-defmethod transient-init-value ((obj transient-option))
  1845. (oset obj value
  1846. (transient--value-match (format "\\`%s\\(.*\\)" (oref obj argument)))))
  1847. (cl-defmethod transient-init-value ((obj transient-switches))
  1848. (oset obj value
  1849. (transient--value-match (oref obj argument-regexp))))
  1850. (defun transient--value-match (re)
  1851. (when-let ((match (cl-find-if (lambda (v)
  1852. (and (stringp v)
  1853. (string-match re v)))
  1854. (oref transient--prefix value))))
  1855. (match-string 1 match)))
  1856. (cl-defmethod transient-init-value ((obj transient-files))
  1857. (oset obj value
  1858. (cdr (assoc "--" (oref transient--prefix value)))))
  1859. ;;;; Read
  1860. (cl-defgeneric transient-infix-read (obj)
  1861. "Determine the new value of the infix object OBJ.
  1862. This function merely determines the value; `transient-infix-set'
  1863. is used to actually store the new value in the object.
  1864. For most infix classes this is done by reading a value from the
  1865. user using the reader specified by the `reader' slot (using the
  1866. `transient-infix' method described below).
  1867. For some infix classes the value is changed without reading
  1868. anything in the minibuffer, i.e. the mere act of invoking the
  1869. infix command determines what the new value should be, based
  1870. on the previous value.")
  1871. (cl-defmethod transient-infix-read :around ((obj transient-infix))
  1872. "Highlight the infix in the popup buffer.
  1873. Also arrange for the transient to be exited in case of an error
  1874. because otherwise Emacs would get stuck in an inconsistent state,
  1875. which might make it necessary to kill it from the outside."
  1876. (let ((transient--active-infix obj))
  1877. (transient--show))
  1878. (transient--with-emergency-exit
  1879. (cl-call-next-method obj)))
  1880. (cl-defmethod transient-infix-read ((obj transient-infix))
  1881. "Read a value while taking care of history.
  1882. This method is suitable for a wide variety of infix commands,
  1883. including but not limitted to inline arguments and variables.
  1884. If you do not use this method for your own infix class, then
  1885. you should likely replicate a lot of the behavior of this
  1886. method. If you fail to do so, then users might not appreciate
  1887. the lack of history, for example.
  1888. Only for very simple classes that toggle or cycle through a very
  1889. limited number of possible values should you replace this with a
  1890. simple method that does not handle history. (E.g. for a command
  1891. line switch the only possible values are \"use it\" and \"don't use
  1892. it\", in which case it is pointless to preserve history.)"
  1893. (with-slots (value multi-value allow-empty choices) obj
  1894. (if (and value
  1895. (not multi-value)
  1896. transient--prefix)
  1897. (oset obj value nil)
  1898. (let* ((overriding-terminal-local-map nil)
  1899. (reader (oref obj reader))
  1900. (prompt (transient-prompt obj))
  1901. (value (if multi-value (mapconcat #'identity value ",") value))
  1902. (history-key (or (oref obj history-key)
  1903. (oref obj command)))
  1904. (transient--history (alist-get history-key transient-history))
  1905. (transient--history (if (or (null value)
  1906. (eq value (car transient--history)))
  1907. transient--history
  1908. (cons value transient--history)))
  1909. (initial-input (and transient-read-with-initial-input
  1910. (car transient--history)))
  1911. (history (if initial-input
  1912. (cons 'transient--history 1)
  1913. 'transient--history))
  1914. (value
  1915. (cond
  1916. (reader (funcall reader prompt initial-input history))
  1917. (multi-value
  1918. (completing-read-multiple prompt choices nil nil
  1919. initial-input history))
  1920. (choices
  1921. (completing-read prompt choices nil t initial-input history))
  1922. (t (read-string prompt initial-input history)))))
  1923. (cond ((and (equal value "") (not allow-empty))
  1924. (setq value nil))
  1925. ((and (equal value "\"\"") allow-empty)
  1926. (setq value "")))
  1927. (when value
  1928. (when (bound-and-true-p ivy-mode)
  1929. (set-text-properties 0 (length (car transient--history)) nil
  1930. (car transient--history)))
  1931. (setf (alist-get history-key transient-history)
  1932. (delete-dups transient--history)))
  1933. value))))
  1934. (cl-defmethod transient-infix-read ((obj transient-switch))
  1935. "Toggle the switch on or off."
  1936. (if (oref obj value) nil (oref obj argument)))
  1937. (cl-defmethod transient-infix-read ((obj transient-switches))
  1938. "Cycle through the mutually exclusive switches.
  1939. The last value is \"don't use any of these switches\"."
  1940. (let ((choices (mapcar (apply-partially #'format (oref obj argument-format))
  1941. (oref obj choices))))
  1942. (if-let ((value (oref obj value)))
  1943. (cadr (member value choices))
  1944. (car choices))))
  1945. ;;;; Readers
  1946. (defun transient-read-directory (prompt _initial-input _history)
  1947. "Read a directory."
  1948. (expand-file-name (read-directory-name prompt)))
  1949. (defun transient-read-existing-directory (prompt _initial-input _history)
  1950. "Read an existing directory."
  1951. (expand-file-name (read-directory-name prompt nil nil t)))
  1952. (defun transient-read-number-N0 (prompt initial-input history)
  1953. "Read a natural number (including zero) and return it as a string."
  1954. (transient--read-number-N prompt initial-input history t))
  1955. (defun transient-read-number-N+ (prompt initial-input history)
  1956. "Read a natural number (excluding zero) and return it as a string."
  1957. (transient--read-number-N prompt initial-input history nil))
  1958. (defun transient--read-number-N (prompt initial-input history include-zero)
  1959. (save-match-data
  1960. (cl-block nil
  1961. (while t
  1962. (let ((str (read-from-minibuffer prompt initial-input nil nil history)))
  1963. (cond ((string-equal str "")
  1964. (cl-return nil))
  1965. ((string-match-p (if include-zero
  1966. "\\`\\(0\\|[1-9][0-9]*\\)\\'"
  1967. "\\`[1-9][0-9]*\\'")
  1968. str)
  1969. (cl-return str))))
  1970. (message "Please enter a natural number (%s zero)."
  1971. (if include-zero "including" "excluding"))
  1972. (sit-for 1)))))
  1973. (defun transient-read-date (prompt default-time _history)
  1974. "Read a date using `org-read-date' (which see)."
  1975. (require 'org)
  1976. (when (fboundp 'org-read-date)
  1977. (org-read-date 'with-time nil nil prompt default-time)))
  1978. ;;;; Prompt
  1979. (cl-defgeneric transient-prompt (obj)
  1980. "Return the prompt to be used to read infix object OBJ's value.")
  1981. (cl-defmethod transient-prompt ((obj transient-infix))
  1982. "Return the prompt to be used to read infix object OBJ's value.
  1983. This implementation should be suitable for almost all infix
  1984. commands.
  1985. If the value of OBJ's `prompt' slot is non-nil, then it must be
  1986. a string or a function. If it is a string, then use that. If
  1987. it is a function, then call that with OBJ as the only argument.
  1988. That function must return a string, which is then used as the
  1989. prompt.
  1990. Otherwise, if the value of either the `argument' or `variable'
  1991. slot of OBJ is a string, then base the prompt on that (preferring
  1992. the former), appending either \"=\" (if it appears to be a
  1993. command-line option) or \": \".
  1994. Finally fall through to using \"(BUG: no prompt): \" as the
  1995. prompt."
  1996. (if-let ((prompt (oref obj prompt)))
  1997. (let ((prompt (if (functionp prompt)
  1998. (funcall prompt obj)
  1999. prompt)))
  2000. (if (stringp prompt)
  2001. prompt
  2002. "(BUG: no prompt): "))
  2003. (or (when-let ((arg (and (slot-boundp obj 'argument) (oref obj argument))))
  2004. (if (and (stringp arg) (string-suffix-p "=" arg))
  2005. arg
  2006. (concat arg ": ")))
  2007. (when-let ((var (and (slot-boundp obj 'variable) (oref obj variable))))
  2008. (and (stringp var)
  2009. (concat var ": ")))
  2010. "(BUG: no prompt): ")))
  2011. ;;;; Set
  2012. (defvar transient--unset-incompatible t)
  2013. (cl-defgeneric transient-infix-set (obj value)
  2014. "Set the value of infix object OBJ to value.")
  2015. (cl-defmethod transient-infix-set ((obj transient-infix) value)
  2016. "Set the value of infix object OBJ to value.
  2017. This implementation should be suitable for almost all infix
  2018. commands."
  2019. (oset obj value value))
  2020. (cl-defmethod transient-infix-set :around ((obj transient-argument) value)
  2021. "Unset incompatible infix arguments."
  2022. (let ((arg (if (slot-boundp obj 'argument)
  2023. (oref obj argument)
  2024. (oref obj argument-regexp))))
  2025. (if-let ((sic (and value arg transient--unset-incompatible))
  2026. (spec (oref transient--prefix incompatible))
  2027. (incomp (remove arg (cl-find-if (lambda (elt) (member arg elt)) spec))))
  2028. (progn
  2029. (cl-call-next-method obj value)
  2030. (dolist (arg incomp)
  2031. (when-let ((obj (cl-find-if (lambda (obj)
  2032. (and (slot-boundp obj 'argument)
  2033. (equal (oref obj argument) arg)))
  2034. transient--suffixes)))
  2035. (let ((transient--unset-incompatible nil))
  2036. (transient-infix-set obj nil)))))
  2037. (cl-call-next-method obj value))))
  2038. (cl-defmethod transient-set-value ((obj transient-prefix))
  2039. (oset (oref obj prototype) value (transient-get-value))
  2040. (transient--history-push obj))
  2041. ;;;; Save
  2042. (cl-defmethod transient-save-value ((obj transient-prefix))
  2043. (let ((value (transient-get-value)))
  2044. (oset (oref obj prototype) value value)
  2045. (setf (alist-get (oref obj command) transient-values) value)
  2046. (transient-save-values))
  2047. (transient--history-push obj))
  2048. ;;;; Get
  2049. (defun transient-args (prefix)
  2050. "Return the value of the transient prefix command PREFIX.
  2051. If the current command was invoked from the transient prefix
  2052. command PREFIX, then return the active infix arguments. If
  2053. the current command was not invoked from PREFIX, then return
  2054. the set, saved or default value for PREFIX."
  2055. (if (eq current-transient-command prefix)
  2056. (delq nil (mapcar 'transient-infix-value current-transient-suffixes))
  2057. (let ((transient--prefix nil)
  2058. (transient--layout nil)
  2059. (transient--suffixes nil))
  2060. (transient--init-objects prefix nil nil)
  2061. (delq nil (mapcar 'transient-infix-value transient--suffixes)))))
  2062. (defun transient-get-value ()
  2063. (delq nil (mapcar 'transient-infix-value current-transient-suffixes)))
  2064. (cl-defgeneric transient-infix-value (obj)
  2065. "Return the value of the suffix object OBJ.
  2066. This function is called by `transient-args' (which see), meaning
  2067. this function is how the value of a transient is determined so
  2068. that the invoked suffix command can use it.
  2069. Currently most values are strings, but that is not set in stone.
  2070. Nil is not a value, it means \"no value\".
  2071. Usually only infixes have a value, but see the method for
  2072. `transient-suffix'.")
  2073. (cl-defmethod transient-infix-value ((_ transient-suffix))
  2074. "Return nil, which means \"no value\".
  2075. Infix arguments contribute the the transient's value while suffix
  2076. commands consume it. This function is called for suffixes anyway
  2077. because a command that both contributes to the transient's value
  2078. and also consumes it is not completely unconceivable.
  2079. If you define such a command, then you must define a derived
  2080. class and implement this function because this default method
  2081. does nothing." nil)
  2082. (cl-defmethod transient-infix-value ((obj transient-infix))
  2083. "Return the value of OBJ's `value' slot."
  2084. (oref obj value))
  2085. (cl-defmethod transient-infix-value ((obj transient-option))
  2086. "Return (concat ARGUMENT VALUE) or nil.
  2087. ARGUMENT and VALUE are the values of the respective slots of OBJ.
  2088. If VALUE is nil, then return nil. VALUE may be the empty string,
  2089. which is not the same as nil."
  2090. (when-let ((value (oref obj value)))
  2091. (concat (oref obj argument) value)))
  2092. (cl-defmethod transient-infix-value ((_ transient-variable))
  2093. "Return nil, which means \"no value\".
  2094. Setting the value of a variable is done by, well, setting the
  2095. value of the variable. I.e. this is a side-effect and does not
  2096. contribute to the value of the transient."
  2097. nil)
  2098. (cl-defmethod transient-infix-value ((obj transient-files))
  2099. "Return (concat ARGUMENT VALUE) or nil.
  2100. ARGUMENT and VALUE are the values of the respective slots of OBJ.
  2101. If VALUE is nil, then return nil. VALUE may be the empty string,
  2102. which is not the same as nil."
  2103. (when-let ((value (oref obj value)))
  2104. (cons (oref obj argument) value)))
  2105. ;;; History
  2106. (cl-defgeneric transient--history-key (obj)
  2107. "Return OBJ's history key.
  2108. If the value of the `history-key' slot is non-nil, then return
  2109. that. Otherwise return the value of the `command' slot."
  2110. (or (oref obj history-key)
  2111. (oref obj command)))
  2112. (cl-defgeneric transient--history-push (obj)
  2113. "Push the current value of OBJ to its entry in `transient-history'."
  2114. (let ((key (transient--history-key obj)))
  2115. (setf (alist-get key transient-history)
  2116. (let ((args (transient-get-value)))
  2117. (cons args (delete args (alist-get key transient-history)))))))
  2118. (cl-defgeneric transient--history-init (obj)
  2119. "Initialize OBJ's `history' slot.
  2120. This is the transient-wide history; many individual infixes also
  2121. have a history of their own.")
  2122. (cl-defmethod transient--history-init ((obj transient-prefix))
  2123. "Initialize OBJ's `history' slot from the variable `transient-history'."
  2124. (let ((val (oref obj value)))
  2125. (oset obj history
  2126. (cons val (delete val (alist-get (transient--history-key obj)
  2127. transient-history))))))
  2128. ;;; Draw
  2129. (defun transient--show-brief ()
  2130. (let ((message-log-max nil))
  2131. (if (and transient-show-popup (<= transient-show-popup 0))
  2132. (message "%s-" (key-description (this-command-keys)))
  2133. (message
  2134. "%s- [%s] %s"
  2135. (key-description (this-command-keys))
  2136. (oref transient--prefix command)
  2137. (mapconcat
  2138. #'identity
  2139. (sort
  2140. (cl-mapcan
  2141. (lambda (suffix)
  2142. (let ((key (kbd (oref suffix key))))
  2143. ;; Don't list any common commands.
  2144. (and (not (memq (oref suffix command)
  2145. `(,(lookup-key transient-map key)
  2146. ,(lookup-key transient-sticky-map key)
  2147. ;; From transient-common-commands:
  2148. transient-set
  2149. transient-save
  2150. transient-history-prev
  2151. transient-history-next
  2152. transient-quit-one
  2153. transient-toggle-common
  2154. transient-set-level)))
  2155. (list (propertize (oref suffix key) 'face 'transient-key)))))
  2156. transient--suffixes)
  2157. #'string<)
  2158. (propertize "|" 'face 'transient-unreachable-key))))))
  2159. (defun transient--show ()
  2160. (transient--timer-cancel)
  2161. (setq transient--showp t)
  2162. (let ((buf (get-buffer-create transient--buffer-name))
  2163. (focus nil))
  2164. (unless (window-live-p transient--window)
  2165. (setq transient--window
  2166. (display-buffer buf transient-display-buffer-action)))
  2167. (with-selected-window transient--window
  2168. (when transient-enable-popup-navigation
  2169. (setq focus (button-get (point) 'command)))
  2170. (erase-buffer)
  2171. (set-window-hscroll transient--window 0)
  2172. (set-window-dedicated-p transient--window t)
  2173. (set-window-parameter transient--window 'no-other-window t)
  2174. (setq window-size-fixed t)
  2175. (when (bound-and-true-p tab-line-format)
  2176. (setq tab-line-format nil))
  2177. (setq mode-line-format (if (eq transient-mode-line-format 'line)
  2178. nil
  2179. transient-mode-line-format))
  2180. (setq mode-line-buffer-identification
  2181. (symbol-name (oref transient--prefix command)))
  2182. (if transient-enable-popup-navigation
  2183. (setq-local cursor-in-non-selected-windows 'box)
  2184. (setq cursor-type nil))
  2185. (setq display-line-numbers nil)
  2186. (setq show-trailing-whitespace nil)
  2187. (transient--insert-groups)
  2188. (when (or transient--helpp transient--editp)
  2189. (transient--insert-help))
  2190. (when (and (eq transient-mode-line-format 'line)
  2191. window-system)
  2192. (insert (propertize "__" 'face 'transient-separator
  2193. 'display '(space :height (1))))
  2194. (insert (propertize "\n" 'face 'transient-separator 'line-height t)))
  2195. (let ((window-resize-pixelwise t)
  2196. (window-size-fixed nil))
  2197. (fit-window-to-buffer nil nil 1))
  2198. (goto-char (point-min))
  2199. (when transient-force-fixed-pitch
  2200. (transient--force-fixed-pitch))
  2201. (when transient-enable-popup-navigation
  2202. (transient--goto-button focus)))))
  2203. (defun transient--insert-groups ()
  2204. (let ((groups (cl-mapcan (lambda (group)
  2205. (let ((hide (oref group hide)))
  2206. (and (not (and (functionp hide)
  2207. (funcall hide)))
  2208. (list group))))
  2209. transient--layout))
  2210. group)
  2211. (while (setq group (pop groups))
  2212. (transient--insert-group group)
  2213. (when groups
  2214. (insert ?\n)))))
  2215. (cl-defgeneric transient--insert-group (group)
  2216. "Format GROUP and its elements and insert the result.")
  2217. (cl-defmethod transient--insert-group :before ((group transient-group))
  2218. "Insert GROUP's description, if any."
  2219. (when-let ((desc (transient-format-description group)))
  2220. (insert desc ?\n)))
  2221. (cl-defmethod transient--insert-group ((group transient-row))
  2222. (dolist (suffix (oref group suffixes))
  2223. (insert (transient-format suffix))
  2224. (insert " "))
  2225. (insert ?\n))
  2226. (cl-defmethod transient--insert-group ((group transient-column))
  2227. (dolist (suffix (oref group suffixes))
  2228. (let ((str (transient-format suffix)))
  2229. (insert str)
  2230. (unless (string-match-p ".\n\\'" str)
  2231. (insert ?\n)))))
  2232. (cl-defmethod transient--insert-group ((group transient-columns))
  2233. (let* ((columns
  2234. (mapcar
  2235. (lambda (column)
  2236. (let ((rows (mapcar 'transient-format (oref column suffixes))))
  2237. (when-let ((desc (transient-format-description column)))
  2238. (push desc rows))
  2239. rows))
  2240. (oref group suffixes)))
  2241. (rs (apply #'max (mapcar #'length columns)))
  2242. (cs (length columns))
  2243. (cw (mapcar (lambda (col) (apply #'max (mapcar #'length col)))
  2244. columns))
  2245. (cc (transient--seq-reductions-from (apply-partially #'+ 3) cw 0)))
  2246. (dotimes (r rs)
  2247. (dotimes (c cs)
  2248. (insert (make-string (- (nth c cc) (current-column)) ?\s))
  2249. (when-let ((cell (nth r (nth c columns))))
  2250. (insert cell))
  2251. (when (= c (1- cs))
  2252. (insert ?\n))))))
  2253. (cl-defmethod transient--insert-group ((group transient-subgroups))
  2254. (let* ((subgroups (oref group suffixes))
  2255. (n (length subgroups)))
  2256. (dotimes (s n)
  2257. (transient--insert-group (nth s subgroups))
  2258. (when (< s (1- n))
  2259. (insert ?\n)))))
  2260. (cl-defgeneric transient-format (obj)
  2261. "Format and return OBJ for display.
  2262. When this function is called, then the current buffer is some
  2263. temporary buffer. If you need the buffer from which the prefix
  2264. command was invoked to be current, then do so by temporarily
  2265. making `transient--original-buffer' current.")
  2266. (cl-defmethod transient-format ((arg string))
  2267. "Return the string ARG after applying the `transient-heading' face."
  2268. (propertize arg 'face 'transient-heading))
  2269. (cl-defmethod transient-format ((_ null))
  2270. "Return a string containing just the newline character."
  2271. "\n")
  2272. (cl-defmethod transient-format ((arg integer))
  2273. "Return a string containing just the ARG character."
  2274. (char-to-string arg))
  2275. (cl-defmethod transient-format :around ((obj transient-infix))
  2276. "When reading user input for this infix, then highlight it."
  2277. (let ((str (cl-call-next-method obj)))
  2278. (when (eq obj transient--active-infix)
  2279. (setq str (concat str "\n"))
  2280. (add-face-text-property
  2281. (if (eq this-command 'transient-set-level) 3 0)
  2282. (length str)
  2283. 'transient-active-infix nil str))
  2284. str))
  2285. (cl-defmethod transient-format :around ((obj transient-suffix))
  2286. "When edit-mode is enabled, then prepend the level information.
  2287. Optional support for popup buttons is also implemented here."
  2288. (let ((str (concat
  2289. (and transient--editp
  2290. (let ((level (oref obj level)))
  2291. (propertize (format " %s " level)
  2292. 'face (if (transient--use-level-p level t)
  2293. 'transient-enabled-suffix
  2294. 'transient-disabled-suffix))))
  2295. (cl-call-next-method obj))))
  2296. (if transient-enable-popup-navigation
  2297. (make-text-button str nil
  2298. 'type 'transient-button
  2299. 'command (transient--suffix-command obj))
  2300. str)))
  2301. (cl-defmethod transient-format ((obj transient-infix))
  2302. "Return a string generated using OBJ's `format'.
  2303. %k is formatted using `transient-format-key'.
  2304. %d is formatted using `transient-format-description'.
  2305. %f is formatted using `transient-format-value'."
  2306. (format-spec (oref obj format)
  2307. `((?k . ,(transient-format-key obj))
  2308. (?d . ,(transient-format-description obj))
  2309. (?v . ,(transient-format-value obj)))))
  2310. (cl-defmethod transient-format ((obj transient-suffix))
  2311. "Return a string generated using OBJ's `format'.
  2312. %k is formatted using `transient-format-key'.
  2313. %d is formatted using `transient-format-description'."
  2314. (format-spec (oref obj format)
  2315. `((?k . ,(transient-format-key obj))
  2316. (?d . ,(transient-format-description obj)))))
  2317. (cl-defgeneric transient-format-key (obj)
  2318. "Format OBJ's `key' for display and return the result.")
  2319. (cl-defmethod transient-format-key ((obj transient-suffix))
  2320. "Format OBJ's `key' for display and return the result."
  2321. (let ((key (oref obj key)))
  2322. (if transient--redisplay-key
  2323. (let ((len (length transient--redisplay-key))
  2324. (seq (cl-coerce (edmacro-parse-keys key t) 'list)))
  2325. (cond
  2326. ((equal (seq-take seq len) transient--redisplay-key)
  2327. (let ((pre (key-description (vconcat (seq-take seq len))))
  2328. (suf (key-description (vconcat (seq-drop seq len)))))
  2329. (setq pre (replace-regexp-in-string "RET" "C-m" pre t))
  2330. (setq pre (replace-regexp-in-string "TAB" "C-i" pre t))
  2331. (setq suf (replace-regexp-in-string "RET" "C-m" suf t))
  2332. (setq suf (replace-regexp-in-string "TAB" "C-i" suf t))
  2333. ;; We use e.g. "-k" instead of the more correct "- k",
  2334. ;; because the former is prettier. If we did that in
  2335. ;; the definition, then we want to drop the space that
  2336. ;; is reinserted above. False-positives are possible
  2337. ;; for silly bindings like "-C-c C-c".
  2338. (unless (string-match-p " " key)
  2339. (setq pre (replace-regexp-in-string " " "" pre))
  2340. (setq suf (replace-regexp-in-string " " "" suf)))
  2341. (concat (propertize pre 'face 'default)
  2342. (and (string-prefix-p (concat pre " ") key) " ")
  2343. (propertize suf 'face 'transient-key)
  2344. (save-excursion
  2345. (when (string-match " +\\'" key)
  2346. (match-string 0 key))))))
  2347. ((transient--lookup-key transient-sticky-map (kbd key))
  2348. (propertize key 'face 'transient-key))
  2349. (t
  2350. (propertize key 'face 'transient-unreachable-key))))
  2351. (propertize key 'face 'transient-key))))
  2352. (cl-defmethod transient-format-key :around ((obj transient-argument))
  2353. (let ((key (cl-call-next-method obj)))
  2354. (cond ((not transient-highlight-mismatched-keys))
  2355. ((not (slot-boundp obj 'shortarg))
  2356. (add-face-text-property
  2357. 0 (length key) 'transient-nonstandard-key nil key))
  2358. ((not (string-equal key (oref obj shortarg)))
  2359. (add-face-text-property
  2360. 0 (length key) 'transient-mismatched-key nil key)))
  2361. key))
  2362. (cl-defgeneric transient-format-description (obj)
  2363. "Format OBJ's `description' for display and return the result.")
  2364. (cl-defmethod transient-format-description ((obj transient-child))
  2365. "The `description' slot may be a function, in which case that is
  2366. called inside the correct buffer (see `transient-insert-group')
  2367. and its value is returned to the caller."
  2368. (when-let ((desc (oref obj description)))
  2369. (if (functionp desc)
  2370. (with-current-buffer transient--original-buffer
  2371. (funcall desc))
  2372. desc)))
  2373. (cl-defmethod transient-format-description ((obj transient-group))
  2374. "Format the description by calling the next method. If the result
  2375. doesn't use the `face' property at all, then apply the face
  2376. `transient-heading' to the complete string."
  2377. (when-let ((desc (cl-call-next-method obj)))
  2378. (if (text-property-not-all 0 (length desc) 'face nil desc)
  2379. desc
  2380. (propertize desc 'face 'transient-heading))))
  2381. (cl-defmethod transient-format-description :around ((obj transient-suffix))
  2382. "Format the description by calling the next method. If the result
  2383. is nil, then use \"(BUG: no description)\" as the description.
  2384. If the OBJ's `key' is currently unreachable, then apply the face
  2385. `transient-unreachable' to the complete string."
  2386. (let ((desc (or (cl-call-next-method obj)
  2387. (propertize "(BUG: no description)" 'face 'error))))
  2388. (if (transient--key-unreachable-p obj)
  2389. (propertize desc 'face 'transient-unreachable)
  2390. desc)))
  2391. (cl-defgeneric transient-format-value (obj)
  2392. "Format OBJ's value for display and return the result.")
  2393. (cl-defmethod transient-format-value ((obj transient-suffix))
  2394. (propertize (oref obj argument)
  2395. 'face (if (oref obj value)
  2396. 'transient-argument
  2397. 'transient-inactive-argument)))
  2398. (cl-defmethod transient-format-value ((obj transient-option))
  2399. (let ((value (oref obj value)))
  2400. (propertize (concat (oref obj argument)
  2401. (if (listp value)
  2402. (mapconcat #'identity value ",")
  2403. value))
  2404. 'face (if value
  2405. 'transient-value
  2406. 'transient-inactive-value))))
  2407. (cl-defmethod transient-format-value ((obj transient-switches))
  2408. (with-slots (value argument-format choices) obj
  2409. (format (propertize argument-format
  2410. 'face (if value
  2411. 'transient-value
  2412. 'transient-inactive-value))
  2413. (concat
  2414. (propertize "[" 'face 'transient-inactive-value)
  2415. (mapconcat
  2416. (lambda (choice)
  2417. (propertize choice 'face
  2418. (if (equal (format argument-format choice) value)
  2419. 'transient-value
  2420. 'transient-inactive-value)))
  2421. choices
  2422. (propertize "|" 'face 'transient-inactive-value))
  2423. (propertize "]" 'face 'transient-inactive-value)))))
  2424. (cl-defmethod transient-format-value ((obj transient-files))
  2425. (let ((argument (oref obj argument)))
  2426. (if-let ((value (oref obj value)))
  2427. (propertize (concat argument " "
  2428. (mapconcat (lambda (f) (format "%S" f))
  2429. (oref obj value) " "))
  2430. 'face 'transient-argument)
  2431. (propertize argument 'face 'transient-inactive-argument))))
  2432. (defun transient--key-unreachable-p (obj)
  2433. (and transient--redisplay-key
  2434. (let ((key (oref obj key)))
  2435. (not (or (equal (seq-take (cl-coerce (edmacro-parse-keys key t) 'list)
  2436. (length transient--redisplay-key))
  2437. transient--redisplay-key)
  2438. (transient--lookup-key transient-sticky-map (kbd key)))))))
  2439. (defun transient--lookup-key (keymap key)
  2440. (let ((val (lookup-key keymap key)))
  2441. (and val (not (integerp val)) val)))
  2442. ;;; Help
  2443. (cl-defgeneric transient-show-help (obj)
  2444. "Show help for OBJ's command.")
  2445. (cl-defmethod transient-show-help ((obj transient-prefix))
  2446. "Show the info manual, manpage or command doc-string.
  2447. Show the first one that is specified."
  2448. (if-let ((manual (oref obj info-manual)))
  2449. (info manual)
  2450. (if-let ((manpage (oref obj man-page)))
  2451. (transient--show-manpage manpage)
  2452. (transient--describe-function (oref obj command)))))
  2453. (cl-defmethod transient-show-help ((_ transient-suffix))
  2454. "Show the command doc-string."
  2455. (if (eq this-original-command 'transient-help)
  2456. (if-let ((manpage (oref transient--prefix man-page)))
  2457. (transient--show-manpage manpage)
  2458. (transient--describe-function (oref transient--prefix command)))
  2459. (transient--describe-function this-original-command)))
  2460. (cl-defmethod transient-show-help ((obj transient-infix))
  2461. "Show the manpage if defined or the command doc-string.
  2462. If the manpage is specified, then try to jump to the correct
  2463. location."
  2464. (if-let ((manpage (oref transient--prefix man-page)))
  2465. (transient--show-manpage manpage (ignore-errors (oref obj argument)))
  2466. (transient--describe-function this-original-command)))
  2467. ;; `cl-generic-generalizers' doesn't support `command' et al.
  2468. (cl-defmethod transient-show-help (cmd)
  2469. "Show the command doc-string."
  2470. (transient--describe-function cmd))
  2471. (defun transient--show-manpage (manpage &optional argument)
  2472. (require 'man)
  2473. (let* ((Man-notify-method 'meek)
  2474. (buf (Man-getpage-in-background manpage))
  2475. (proc (get-buffer-process buf)))
  2476. (while (and proc (eq (process-status proc) 'run))
  2477. (accept-process-output proc))
  2478. (switch-to-buffer buf)
  2479. (when argument
  2480. (transient--goto-argument-description argument))))
  2481. (defun transient--describe-function (fn)
  2482. (describe-function fn)
  2483. (select-window (get-buffer-window (help-buffer))))
  2484. (defun transient--goto-argument-description (arg)
  2485. (goto-char (point-min))
  2486. (let ((case-fold-search nil)
  2487. ;; This matches preceding/proceeding options. Options
  2488. ;; such as "-a", "-S[<keyid>]", and "--grep=<pattern>"
  2489. ;; are matched by this regex without the shy group.
  2490. ;; The ". " in the shy group is for options such as
  2491. ;; "-m parent-number", and the "-[^[:space:]]+ " is
  2492. ;; for options such as "--mainline parent-number"
  2493. (others "-\\(?:. \\|-[^[:space:]]+ \\)?[^[:space:]]+"))
  2494. (when (re-search-forward
  2495. (if (equal arg "--")
  2496. ;; Special case.
  2497. "^[\t\s]+\\(--\\(?: \\|$\\)\\|\\[--\\]\\)"
  2498. ;; Should start with whitespace and may have
  2499. ;; any number of options before and/or after.
  2500. (format
  2501. "^[\t\s]+\\(?:%s, \\)*?\\(?1:%s\\)%s\\(?:, %s\\)*$"
  2502. others
  2503. ;; Options don't necessarily end in an "="
  2504. ;; (e.g., "--gpg-sign[=<keyid>]")
  2505. (string-remove-suffix "=" arg)
  2506. ;; Simple options don't end in an "=". Splitting this
  2507. ;; into 2 cases should make getting false positives
  2508. ;; less likely.
  2509. (if (string-suffix-p "=" arg)
  2510. ;; "[^[:space:]]*[^.[:space:]]" matches the option
  2511. ;; value, which is usually after the option name
  2512. ;; and either '=' or '[='. The value can't end in
  2513. ;; a period, as that means it's being used at the
  2514. ;; end of a sentence. The space is for options
  2515. ;; such as '--mainline parent-number'.
  2516. "\\(?: \\|\\[?=\\)[^[:space:]]*[^.[:space:]]"
  2517. ;; Either this doesn't match anything (e.g., "-a"),
  2518. ;; or the option is followed by a value delimited
  2519. ;; by a "[", "<", or ":". A space might appear
  2520. ;; before this value, as in "-f <file>". The
  2521. ;; space alternative is for options such as
  2522. ;; "-m parent-number".
  2523. "\\(?:\\(?: \\| ?[\\[<:]\\)[^[:space:]]*[^.[:space:]]\\)?")
  2524. others))
  2525. nil t)
  2526. (goto-char (match-beginning 1)))))
  2527. (defun transient--insert-help ()
  2528. (unless (looking-back "\n\n" 2)
  2529. (insert "\n"))
  2530. (when transient--helpp
  2531. (insert
  2532. (format (propertize "\
  2533. Type a %s to show help for that suffix command, or %s to show manual.
  2534. Type %s to exit help.\n"
  2535. 'face 'transient-heading)
  2536. (propertize "<KEY>" 'face 'transient-key)
  2537. (propertize "?" 'face 'transient-key)
  2538. (propertize "C-g" 'face 'transient-key))))
  2539. (when transient--editp
  2540. (unless transient--helpp
  2541. (insert
  2542. (format (propertize "\
  2543. Type a %s to set level for that suffix command.
  2544. Type %s to set what levels are available for this prefix command.\n"
  2545. 'face 'transient-heading)
  2546. (propertize "<KEY>" 'face 'transient-key)
  2547. (propertize "C-x l" 'face 'transient-key))))
  2548. (with-slots (level) transient--prefix
  2549. (insert
  2550. (format (propertize "
  2551. Suffixes on levels %s are available.
  2552. Suffixes on levels %s and %s are unavailable.\n"
  2553. 'face 'transient-heading)
  2554. (propertize (format "1-%s" level)
  2555. 'face 'transient-enabled-suffix)
  2556. (propertize " 0 "
  2557. 'face 'transient-disabled-suffix)
  2558. (propertize (format ">=%s" (1+ level))
  2559. 'face 'transient-disabled-suffix))))))
  2560. (defvar transient-resume-mode-map
  2561. (let ((map (make-sparse-keymap)))
  2562. (define-key map [remap Man-quit] 'transient-resume)
  2563. (define-key map [remap Info-exit] 'transient-resume)
  2564. (define-key map [remap quit-window] 'transient-resume)
  2565. map)
  2566. "Keymap for `transient-resume-mode'.
  2567. This keymap remaps every command that would usually just quit the
  2568. documentation buffer to `transient-resume', which additionally
  2569. resumes the suspended transient.")
  2570. (define-minor-mode transient-resume-mode
  2571. "Auxiliary minor-mode used to resume a transient after viewing help.")
  2572. ;;; Compatibility
  2573. ;;;; Popup Navigation
  2574. (defun transient-popup-navigation-help ()
  2575. "Inform the user how to enable popup navigation commands."
  2576. (interactive)
  2577. (message "This command is only available if `%s' is non-nil"
  2578. 'transient-enable-popup-navigation))
  2579. (define-button-type 'transient-button
  2580. 'face nil
  2581. 'action (lambda (button)
  2582. (let ((command (button-get button 'command)))
  2583. ;; Yes, I know that this is wrong(tm).
  2584. ;; Unfortunately it is also necessary.
  2585. (setq this-original-command command)
  2586. (call-interactively command))))
  2587. (defvar transient-popup-navigation-map
  2588. (let ((map (make-sparse-keymap)))
  2589. (define-key map (kbd "<down-mouse-1>") 'transient-noop)
  2590. (define-key map (kbd "<mouse-1>") 'transient-mouse-push-button)
  2591. (define-key map (kbd "RET") 'transient-push-button)
  2592. (define-key map (kbd "<up>") 'transient-backward-button)
  2593. (define-key map (kbd "C-p") 'transient-backward-button)
  2594. (define-key map (kbd "<down>") 'transient-forward-button)
  2595. (define-key map (kbd "C-n") 'transient-forward-button)
  2596. (define-key map (kbd "C-r") 'transient-isearch-backward)
  2597. (define-key map (kbd "C-s") 'transient-isearch-forward)
  2598. map))
  2599. (defun transient-mouse-push-button (&optional pos)
  2600. "Invoke the suffix the user clicks on."
  2601. (interactive (list last-command-event))
  2602. (push-button pos))
  2603. (defun transient-push-button ()
  2604. "Invoke the selected suffix command."
  2605. (interactive)
  2606. (with-selected-window transient--window
  2607. (push-button)))
  2608. (defun transient-backward-button (n)
  2609. "Move to the previous button in the transient popup buffer.
  2610. See `backward-button' for information about N."
  2611. (interactive "p")
  2612. (with-selected-window transient--window
  2613. (backward-button n t)))
  2614. (defun transient-forward-button (n)
  2615. "Move to the next button in the transient popup buffer.
  2616. See `forward-button' for information about N."
  2617. (interactive "p")
  2618. (with-selected-window transient--window
  2619. (forward-button n t)))
  2620. (defun transient--goto-button (command)
  2621. (if (not command)
  2622. (forward-button 1)
  2623. (while (and (ignore-errors (forward-button 1))
  2624. (not (eq (button-get (button-at (point)) 'command) command))))
  2625. (unless (eq (button-get (button-at (point)) 'command) command)
  2626. (goto-char (point-min))
  2627. (forward-button 1))))
  2628. ;;;; Popup Isearch
  2629. (defvar transient--isearch-mode-map
  2630. (let ((map (make-sparse-keymap)))
  2631. (set-keymap-parent map isearch-mode-map)
  2632. (define-key map [remap isearch-exit] 'transient-isearch-exit)
  2633. (define-key map [remap isearch-cancel] 'transient-isearch-cancel)
  2634. (define-key map [remap isearch-abort] 'transient-isearch-abort)
  2635. map))
  2636. (defun transient-isearch-backward (&optional regexp-p)
  2637. "Do incremental search backward.
  2638. With a prefix argument, do an incremental regular expression
  2639. search instead."
  2640. (interactive "P")
  2641. (transient--isearch-setup)
  2642. (let ((isearch-mode-map transient--isearch-mode-map))
  2643. (isearch-mode nil regexp-p)))
  2644. (defun transient-isearch-forward (&optional regexp-p)
  2645. "Do incremental search forward.
  2646. With a prefix argument, do an incremental regular expression
  2647. search instead."
  2648. (interactive "P")
  2649. (transient--isearch-setup)
  2650. (let ((isearch-mode-map transient--isearch-mode-map))
  2651. (isearch-mode t regexp-p)))
  2652. (defun transient-isearch-exit ()
  2653. "Like `isearch-exit' but adapted for `transient'."
  2654. (interactive)
  2655. (isearch-exit)
  2656. (transient--isearch-exit))
  2657. (defun transient-isearch-cancel ()
  2658. "Like `isearch-cancel' but adapted for `transient'."
  2659. (interactive)
  2660. (condition-case nil (isearch-cancel) (quit))
  2661. (transient--isearch-exit))
  2662. (defun transient-isearch-abort ()
  2663. "Like `isearch-abort' but adapted for `transient'."
  2664. (interactive)
  2665. (condition-case nil (isearch-abort) (quit))
  2666. (transient--isearch-exit))
  2667. (defun transient--isearch-setup ()
  2668. (select-window transient--window)
  2669. (transient--suspend-override))
  2670. (defun transient--isearch-exit ()
  2671. (select-window transient--original-window)
  2672. (transient--resume-override))
  2673. ;;;; Edebug
  2674. (defun transient--edebug--recursive-edit (fn arg-mode)
  2675. (transient--debug 'edebug--recursive-edit)
  2676. (if (not transient--prefix)
  2677. (funcall fn arg-mode)
  2678. (transient--suspend-override t)
  2679. (funcall fn arg-mode)
  2680. (transient--resume-override t)))
  2681. (advice-add 'edebug--recursive-edit :around 'transient--edebug--recursive-edit)
  2682. (defun transient--abort-edebug ()
  2683. (when (bound-and-true-p edebug-active)
  2684. (transient--emergency-exit)))
  2685. (advice-add 'abort-recursive-edit :before 'transient--abort-edebug)
  2686. (advice-add 'top-level :before 'transient--abort-edebug)
  2687. (defun transient--edebug-command-p ()
  2688. (and (bound-and-true-p edebug-active)
  2689. (or (memq this-command '(top-level abort-recursive-edit))
  2690. (string-prefix-p "edebug" (symbol-name this-command)))))
  2691. ;;;; Miscellaneous
  2692. (declare-function which-key-mode "which-key" (&optional arg))
  2693. (defun transient--suspend-which-key-mode ()
  2694. (when (bound-and-true-p which-key-mode)
  2695. (which-key-mode -1)
  2696. (add-hook 'post-transient-hook 'transient--resume-which-key-mode)))
  2697. (defun transient--resume-which-key-mode ()
  2698. (unless transient--prefix
  2699. (which-key-mode 1)
  2700. (remove-hook 'post-transient-hook 'transient--resume-which-key-mode)))
  2701. (defun transient-bind-q-to-quit ()
  2702. "Modify some keymaps to bind \"q\" to the appropriate quit command.
  2703. \"C-g\" is the default binding for such commands now, but Transient's
  2704. predecessor Magit-Popup used \"q\" instead. If you would like to get
  2705. that binding back, then call this function in your init file like so:
  2706. (with-eval-after-load \\='transient
  2707. (transient-bind-q-to-quit))
  2708. Individual transients may already bind \"q\" to something else
  2709. and such a binding would shadow the quit binding. If that is the
  2710. case then \"Q\" is bound to whatever \"q\" would have been bound
  2711. to by setting `transient-substitute-key-function' to a function
  2712. that does that. Of course \"Q\" may already be bound to something
  2713. else, so that function binds \"M-q\" to that command instead.
  2714. Of course \"M-q\" may already be bound to something else, but
  2715. we stop there."
  2716. (define-key transient-base-map "q" 'transient-quit-one)
  2717. (define-key transient-sticky-map "q" 'transient-quit-seq)
  2718. (setq transient-substitute-key-function
  2719. 'transient-rebind-quit-commands))
  2720. (defun transient-rebind-quit-commands (obj)
  2721. "See `transient-bind-q-to-quit'."
  2722. (let ((key (oref obj key)))
  2723. (cond ((string-equal key "q") "Q")
  2724. ((string-equal key "Q") "M-q")
  2725. (t key))))
  2726. (defun transient--force-fixed-pitch ()
  2727. (require 'face-remap)
  2728. (face-remap-reset-base 'default)
  2729. (face-remap-add-relative 'default 'fixed-pitch))
  2730. ;;;; Missing from Emacs
  2731. (defun transient--seq-reductions-from (function sequence initial-value)
  2732. (let ((acc (list initial-value)))
  2733. (seq-doseq (elt sequence)
  2734. (push (funcall function (car acc) elt) acc))
  2735. (nreverse acc)))
  2736. ;;; Font-Lock
  2737. (defconst transient-font-lock-keywords
  2738. (eval-when-compile
  2739. `((,(concat "("
  2740. (regexp-opt (list "define-transient-command"
  2741. "define-infix-command"
  2742. "define-infix-argument"
  2743. "define-suffix-command")
  2744. t)
  2745. "\\_>[ \t'\(]*"
  2746. "\\(\\(?:\\sw\\|\\s_\\)+\\)?")
  2747. (1 'font-lock-keyword-face)
  2748. (2 'font-lock-function-name-face nil t)))))
  2749. (font-lock-add-keywords 'emacs-lisp-mode transient-font-lock-keywords)
  2750. ;;; _
  2751. (provide 'transient)
  2752. ;; Local Variables:
  2753. ;; indent-tabs-mode: nil
  2754. ;; End:
  2755. ;;; transient.el ends here