Klimi's new dotfiles with stow.
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

3110 wiersze
120 KiB

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