-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
transient.el
4681 lines (4103 loc) · 184 KB
/
transient.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; transient.el --- Transient commands -*- lexical-binding:t -*-
;; Copyright (C) 2018-2024 Free Software Foundation, Inc.
;; Author: Jonas Bernoulli <[email protected]>
;; Homepage: https://github.com/magit/transient
;; Keywords: extensions
;; Package-Version: 0.7.9
;; Package-Requires: ((emacs "26.1") (compat "30.0.0.0") (seq "2.24"))
;; SPDX-License-Identifier: GPL-3.0-or-later
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published
;; by the Free Software Foundation, either version 3 of the License,
;; or (at your option) any later version.
;;
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Transient is the library used to implement the keyboard-driven menus
;; in Magit. It is distributed as a separate package, so that it can be
;; used to implement similar menus in other packages.
;;; Code:
(require 'cl-lib)
(require 'compat)
(require 'eieio)
(require 'edmacro)
(require 'format-spec)
(require 'pcase)
(eval-and-compile
(when (and (featurep 'seq)
(not (fboundp 'seq-keep)))
(unload-feature 'seq 'force)))
(require 'seq)
(unless (fboundp 'seq-keep)
(display-warning 'transient (substitute-command-keys "\
Transient requires `seq' >= 2.24,
but due to bad defaults, Emacs's package manager, refuses to
upgrade this and other built-in packages to higher releases
from GNU Elpa, when a package specifies that this is needed.
To fix this, you have to add this to your init file:
(setq package-install-upgrade-built-in t)
Then evaluate that expression by placing the cursor after it
and typing \\[eval-last-sexp].
Once you have done that, you have to explicitly upgrade `seq':
\\[package-upgrade] seq \\`RET'
Then you also must make sure the updated version is loaded,
by evaluating this form:
(progn (unload-feature 'seq t) (require 'seq))
Until you do this, you will get random errors about `seq-keep'
being undefined while using Transient.
If you don't use the `package' package manager but still get
this warning, then your chosen package manager likely has a
similar defect.") :emergency))
(eval-when-compile (require 'subr-x))
(declare-function info "info" (&optional file-or-node buffer))
(declare-function Man-find-section "man" (section))
(declare-function Man-next-section "man" (n))
(declare-function Man-getpage-in-background "man" (topic))
(defvar Man-notify-method)
(defvar pp-default-function) ; since Emacs 29.1
(eval-and-compile
(when (< emacs-major-version 28)
(pcase-defmacro cl-type (type)
"Pcase pattern that matches objects of TYPE.
TYPE is a type descriptor as accepted by `cl-typep', which see."
(static-if (< emacs-major-version 30)
`(pred (pcase--flip cl-typep ',type))
`(pred (cl-typep _ ',type))))))
(defmacro transient--with-emergency-exit (id &rest body)
(declare (indent defun))
(unless (keywordp id)
(setq body (cons id body))
(setq id nil))
`(condition-case err
(let ((debugger #'transient--exit-and-debug))
,(macroexp-progn body))
((debug error)
(transient--emergency-exit ,id)
(signal (car err) (cdr err)))))
(defun transient--exit-and-debug (&rest args)
(transient--emergency-exit :debugger)
(apply #'debug args))
;;; Options
(defgroup transient nil
"Transient commands."
:group 'extensions)
(defcustom transient-show-popup t
"Whether to show the current transient in a popup buffer.
\\<transient-map>
- If t, then show the popup as soon as a transient prefix command
is invoked.
- If nil, then do not show the popup unless the user explicitly
requests it, by pressing \\[transient-show] or a prefix key.
- If a number, then delay displaying the popup and instead show
a brief one-line summary. If zero or negative, then suppress
even showing that summary and display the pressed key only.
Show the popup when the user explicitly requests it by pressing
\\[transient-show] or a prefix key. Unless zero, then also show the popup
after that many seconds of inactivity (using the absolute value)."
:package-version '(transient . "0.1.0")
:group 'transient
:type '(choice (const :tag "instantly" t)
(const :tag "on demand" nil)
(const :tag "on demand (no summary)" 0)
(number :tag "after delay" 1)))
(defcustom transient-enable-popup-navigation 'verbose
"Whether navigation commands are enabled in the transient popup.
If the value is `verbose', additionally show brief documentation
about the command under point in the echo area.
While a transient is active the transient popup buffer is not the
current buffer, making it necessary to use dedicated commands to
act on that buffer itself. If this is non-nil, then the following
bindings are available:
\\<transient-popup-navigation-map>\
- \\[transient-backward-button] moves the cursor to the previous suffix.
- \\[transient-forward-button] moves the cursor to the next suffix.
- \\[transient-push-button] invokes the suffix the cursor is on.
\\<transient-button-map>\
- \\`<mouse-1>' and \\`<mouse-2>' invoke the clicked on suffix.
\\<transient-popup-navigation-map>\
- \\[transient-isearch-backward]\
and \\[transient-isearch-forward] start isearch in the popup buffer.
\\`<mouse-1>' and \\`<mouse-2>' are bound in `transient-push-button'.
All other bindings are in `transient-popup-navigation-map'.
By default \\`M-RET' is bound to `transient-push-button', instead of
\\`RET', because if a transient allows the invocation of non-suffixes,
then it is likely, that you would want \\`RET' to do what it would do
if no transient were active."
:package-version '(transient . "0.7.8")
:group 'transient
:type '(choice (const :tag "enable navigation and echo summary" verbose)
(const :tag "enable navigation commands" t)
(const :tag "disable navigation commands" nil)))
(defcustom transient-display-buffer-action
'(display-buffer-in-side-window
(side . bottom)
(dedicated . t)
(inhibit-same-window . t))
"The action used to display the transient popup buffer.
The transient popup buffer is displayed in a window using
(display-buffer BUFFER transient-display-buffer-action)
The value of this option has the form (FUNCTION . ALIST),
where FUNCTION is a function or a list of functions. Each such
function should accept two arguments: a buffer to display and an
alist of the same form as ALIST. See info node `(elisp)Choosing
Window' for details.
The default is:
(display-buffer-in-side-window
(side . bottom)
(dedicated . t)
(inhibit-same-window . t))
This displays the window at the bottom of the selected frame.
Another useful FUNCTION is `display-buffer-below-selected', which
is what `magit-popup' used by default. For more alternatives see
info node `(elisp)Display Action Functions' and info node
`(elisp)Buffer Display Action Alists'.
Note that the buffer that was current before the transient buffer
is shown should remain the current buffer. Many suffix commands
act on the thing at point, if appropriate, and if the transient
buffer became the current buffer, then that would change what is
at point. To that effect `inhibit-same-window' ensures that the
selected window is not used to show the transient buffer.
It may be possible to display the window in another frame, but
whether that works in practice depends on the window-manager.
If the window manager selects the new window (Emacs frame),
then that unfortunately changes which buffer is current.
If you change the value of this option, then you might also
want to change the value of `transient-mode-line-format'."
:package-version '(transient . "0.7.5")
:group 'transient
:type '(cons (choice function (repeat :tag "Functions" function))
alist))
(defcustom transient-mode-line-format 'line
"The mode-line format for the transient popup buffer.
If nil, then the buffer has no mode-line. If the buffer is not
displayed right above the echo area, then this probably is not
a good value.
If `line' (the default) or a natural number, then the buffer has no
mode-line, but a line is drawn in its place. If a number is used,
that specifies the thickness of the line. On termcap frames we
cannot draw lines, so there `line' and numbers are synonyms for nil.
The color of the line is used to indicate if non-suffixes are
allowed and whether they exit the transient. The foreground
color of `transient-key-noop' (if non-suffixes are disallowed),
`transient-key-stay' (if allowed and transient stays active), or
`transient-key-exit' (if allowed and they exit the transient) is
used to draw the line.
Otherwise this can be any mode-line format.
See `mode-line-format' for details."
:package-version '(transient . "0.2.0")
:group 'transient
:type '(choice (const :tag "hide mode-line" nil)
(const :tag "substitute thin line" line)
(number :tag "substitute line with thickness")
(const :tag "name of prefix command"
("%e" mode-line-front-space
mode-line-buffer-identification))
(sexp :tag "custom mode-line format")))
(defcustom transient-show-common-commands nil
"Whether to show common transient suffixes in the popup buffer.
These commands are always shown after typing the prefix key
\"C-x\" when a transient command is active. To toggle the value
of this variable use \"C-x t\" when a transient is active."
:package-version '(transient . "0.1.0")
:group 'transient
:type 'boolean)
(defcustom transient-read-with-initial-input nil
"Whether to use the last history element as initial minibuffer input."
:package-version '(transient . "0.2.0")
:group 'transient
:type 'boolean)
(defcustom transient-highlight-mismatched-keys nil
"Whether to highlight keys that do not match their argument.
This only affects infix arguments that represent command-line
arguments. When this option is non-nil, then the key binding
for infix argument are highlighted when only a long argument
\(e.g., \"--verbose\") is specified but no shorthand (e.g., \"-v\").
In the rare case that a short-hand is specified but does not
match the key binding, then it is highlighted differently.
The highlighting is done using `transient-mismatched-key'
and `transient-nonstandard-key'."
:package-version '(transient . "0.1.0")
:group 'transient
:type 'boolean)
(defcustom transient-highlight-higher-levels nil
"Whether to highlight suffixes on higher levels.
This is primarily intended for package authors.
When non-nil then highlight the description of suffixes whose
level is above 4, the default of `transient-default-level'.
Assuming you have set that variable to 7, this highlights all
suffixes that won't be available to users without them making
the same customization."
:package-version '(transient . "0.3.6")
:group 'transient
:type 'boolean)
(defcustom transient-substitute-key-function nil
"Function used to modify key bindings.
This function is called with one argument, the prefix object,
and must return a key binding description, either the existing
key description it finds in the `key' slot, or a substitution.
This is intended to let users replace certain prefix keys. It
could also be used to make other substitutions, but that is
discouraged.
For example, \"=\" is hard to reach using my custom keyboard
layout, so I substitute \"(\" for that, which is easy to reach
using a layout optimized for Lisp.
(setq transient-substitute-key-function
(lambda (obj)
(let ((key (oref obj key)))
(if (string-match \"\\\\`\\\\(=\\\\)[a-zA-Z]\" key)
(replace-match \"(\" t t key 1)
key)))))"
:package-version '(transient . "0.1.0")
:group 'transient
:type '(choice (const :tag "Transform no keys (nil)" nil) function))
(defcustom transient-semantic-coloring t
"Whether to use colors to indicate transient behavior.
If non-nil, then the key binding of each suffix is colorized to
indicate whether it exits the transient state or not, and the
line that is drawn below the transient popup buffer is used to
indicate the behavior of non-suffix commands."
:package-version '(transient . "0.5.0")
:group 'transient
:type 'boolean)
(defcustom transient-detect-key-conflicts nil
"Whether to detect key binding conflicts.
Conflicts are detected when a transient prefix command is invoked
and results in an error, which prevents the transient from being
used."
:package-version '(transient . "0.1.0")
:group 'transient
:type 'boolean)
(defcustom transient-align-variable-pitch nil
"Whether to align columns pixel-wise in the popup buffer.
If this is non-nil, then columns are aligned pixel-wise to
support variable-pitch fonts. Keys are not aligned, so you
should use a fixed-pitch font for the `transient-key' face.
Other key faces inherit from that face unless a theme is
used that breaks that relationship.
This option is intended for users who use a variable-pitch
font for the `default' face.
Also see `transient-force-fixed-pitch'."
:package-version '(transient . "0.4.0")
:group 'transient
:type 'boolean)
(defcustom transient-force-fixed-pitch nil
"Whether to force use of monospaced font in the popup buffer.
Even if you use a proportional font for the `default' face,
you might still want to use a monospaced font in transient's
popup buffer. Setting this option to t causes `default' to
be remapped to `fixed-pitch' in that buffer.
Also see `transient-align-variable-pitch'."
:package-version '(transient . "0.2.0")
:group 'transient
:type 'boolean)
(defcustom transient-force-single-column nil
"Whether to force use of a single column to display suffixes.
This might be useful for users with low vision who use large
text and might otherwise have to scroll in two dimensions."
:package-version '(transient . "0.3.6")
:group 'transient
:type 'boolean)
(defcustom transient-hide-during-minibuffer-read nil
"Whether to hide the transient buffer while reading in the minibuffer."
:package-version '(transient . "0.4.0")
:group 'transient
:type 'boolean)
(defconst transient--max-level 7)
(defconst transient--default-child-level 1)
(defconst transient--default-prefix-level 4)
(defcustom transient-default-level transient--default-prefix-level
"Control what suffix levels are made available by default.
Each suffix command is placed on a level and each prefix command
has a level, which controls which suffix commands are available.
Integers between 1 and 7 (inclusive) are valid levels.
The levels of individual transients and/or their individual
suffixes can be changed individually, by invoking the prefix and
then pressing \"C-x l\".
The default level for both transients and their suffixes is 4.
This option only controls the default for transients. The default
suffix level is always 4. The author of a transient should place
certain suffixes on a higher level if they expect that it won't be
of use to most users, and they should place very important suffixes
on a lower level so that they remain available even if the user
lowers the transient level.
\(Magit currently places nearly all suffixes on level 4 and lower
levels are not used at all yet. So for the time being you should
not set a lower level here and using a higher level might not
give you as many additional suffixes as you hoped.)"
:package-version '(transient . "0.1.0")
:group 'transient
:type '(choice (const :tag "1 - fewest suffixes" 1)
(const 2)
(const 3)
(const :tag "4 - default" 4)
(const 5)
(const 6)
(const :tag "7 - most suffixes" 7)))
(defcustom transient-levels-file
(locate-user-emacs-file "transient/levels.el")
"File used to save levels of transients and their suffixes."
:package-version '(transient . "0.1.0")
:group 'transient
:type 'file)
(defcustom transient-values-file
(locate-user-emacs-file "transient/values.el")
"File used to save values of transients."
:package-version '(transient . "0.1.0")
:group 'transient
:type 'file)
(defcustom transient-history-file
(locate-user-emacs-file "transient/history.el")
"File used to save history of transients and their infixes."
:package-version '(transient . "0.1.0")
:group 'transient
:type 'file)
(defcustom transient-history-limit 10
"Number of history elements to keep when saving to file."
:package-version '(transient . "0.1.0")
:group 'transient
:type 'integer)
(defcustom transient-save-history t
"Whether to save history of transient commands when exiting Emacs."
:package-version '(transient . "0.1.0")
:group 'transient
:type 'boolean)
;;; Faces
(defgroup transient-faces nil
"Faces used by Transient."
:group 'transient)
(defface transient-heading '((t :inherit font-lock-keyword-face))
"Face used for headings."
:group 'transient-faces)
(defface transient-argument '((t :inherit font-lock-string-face :weight bold))
"Face used for enabled arguments."
:group 'transient-faces)
(defface transient-inactive-argument '((t :inherit shadow))
"Face used for inactive arguments."
:group 'transient-faces)
(defface transient-value '((t :inherit font-lock-string-face :weight bold))
"Face used for values."
:group 'transient-faces)
(defface transient-inactive-value '((t :inherit shadow))
"Face used for inactive values."
:group 'transient-faces)
(defface transient-unreachable '((t :inherit shadow))
"Face used for suffixes unreachable from the current prefix sequence."
:group 'transient-faces)
(defface transient-inapt-suffix '((t :inherit shadow :italic t))
"Face used for suffixes that are inapt at this time."
:group 'transient-faces)
(defface transient-active-infix '((t :inherit highlight))
"Face used for the infix for which the value is being read."
:group 'transient-faces)
(defface transient-enabled-suffix
'((t :background "green" :foreground "black" :weight bold))
"Face used for enabled levels while editing suffix levels.
See info node `(transient)Enabling and Disabling Suffixes'."
:group 'transient-faces)
(defface transient-disabled-suffix
'((t :background "red" :foreground "black" :weight bold))
"Face used for disabled levels while editing suffix levels.
See info node `(transient)Enabling and Disabling Suffixes'."
:group 'transient-faces)
(defface transient-higher-level
`((t :box ( :line-width ,(if (>= emacs-major-version 28) (cons -1 -1) -1)
:color ,(let ((color (face-attribute 'shadow :foreground nil t)))
(or (and (not (eq color 'unspecified)) color)
"grey60")))))
"Face optionally used to highlight suffixes on higher levels.
Also see option `transient-highlight-higher-levels'."
:group 'transient-faces)
(defface transient-delimiter '((t :inherit shadow))
"Face used for delimiters and separators.
This includes the parentheses around values and the pipe
character used to separate possible values from each other."
:group 'transient-faces)
(defface transient-key '((t :inherit font-lock-builtin-face))
"Face used for keys."
:group 'transient-faces)
(defface transient-key-stay
`((((class color) (background light))
:inherit transient-key
:foreground "#22aa22")
(((class color) (background dark))
:inherit transient-key
:foreground "#ddffdd"))
"Face used for keys of suffixes that don't exit transient state."
:group 'transient-faces)
(defface transient-key-noop
`((((class color) (background light))
:inherit transient-key
:foreground "grey80")
(((class color) (background dark))
:inherit transient-key
:foreground "grey30"))
"Face used for keys of suffixes that currently cannot be invoked."
:group 'transient-faces)
(defface transient-key-return
`((((class color) (background light))
:inherit transient-key
:foreground "#aaaa11")
(((class color) (background dark))
:inherit transient-key
:foreground "#ffffcc"))
"Face used for keys of suffixes that return to the parent transient."
:group 'transient-faces)
(defface transient-key-exit
`((((class color) (background light))
:inherit transient-key
:foreground "#aa2222")
(((class color) (background dark))
:inherit transient-key
:foreground "#ffdddd"))
"Face used for keys of suffixes that exit transient state."
:group 'transient-faces)
(defface transient-unreachable-key
'((t :inherit (shadow transient-key) :weight normal))
"Face used for keys unreachable from the current prefix sequence."
:group 'transient-faces)
(defface transient-nonstandard-key
`((t :box ( :line-width ,(if (>= emacs-major-version 28) (cons -1 -1) -1)
:color "cyan")))
"Face optionally used to highlight keys conflicting with short-argument.
Also see option `transient-highlight-mismatched-keys'."
:group 'transient-faces)
(defface transient-mismatched-key
`((t :box ( :line-width ,(if (>= emacs-major-version 28) (cons -1 -1) -1)
:color "magenta")))
"Face optionally used to highlight keys without a short-argument.
Also see option `transient-highlight-mismatched-keys'."
:group 'transient-faces)
;;; Persistence
(defun transient--read-file-contents (file)
(with-demoted-errors "Transient error: %S"
(and (file-exists-p file)
(with-temp-buffer
(insert-file-contents file)
(read (current-buffer))))))
(defun transient--pp-to-file (list file)
(make-directory (file-name-directory file) t)
(setq list (cl-sort (copy-sequence list) #'string< :key #'car))
(with-temp-file file
(let ((print-level nil)
(print-length nil)
(pp-default-function 'pp-28)
(fill-column 999))
(pp list (current-buffer)))))
(defvar transient-values
(transient--read-file-contents transient-values-file)
"Values of transient commands.
The value of this variable persists between Emacs sessions
and you usually should not change it manually.")
(defun transient-save-values ()
(transient--pp-to-file transient-values transient-values-file))
(defvar transient-levels
(transient--read-file-contents transient-levels-file)
"Levels of transient commands.
The value of this variable persists between Emacs sessions
and you usually should not change it manually.")
(defun transient-save-levels ()
(transient--pp-to-file transient-levels transient-levels-file))
(defvar transient-history
(transient--read-file-contents transient-history-file)
"History of transient commands and infix arguments.
The value of this variable persists between Emacs sessions
\(unless `transient-save-history' is nil) and you usually
should not change it manually.")
(defun transient-save-history ()
(setq transient-history
(cl-sort (mapcar (pcase-lambda (`(,key . ,val))
(cons key (seq-take (delete-dups val)
transient-history-limit)))
transient-history)
#'string< :key #'car))
(transient--pp-to-file transient-history transient-history-file))
(defun transient-maybe-save-history ()
"Save the value of `transient-history'.
If `transient-save-history' is nil, then do nothing."
(when transient-save-history
(transient-save-history)))
(unless noninteractive
(add-hook 'kill-emacs-hook #'transient-maybe-save-history))
;;; Classes
;;;; Prefix
(defclass transient-prefix ()
((prototype :initarg :prototype)
(command :initarg :command)
(level :initarg :level)
(variable :initarg :variable :initform nil)
(init-value :initarg :init-value)
(value) (default-value :initarg :value)
(scope :initarg :scope :initform nil)
(history :initarg :history :initform nil)
(history-pos :initarg :history-pos :initform 0)
(history-key :initarg :history-key :initform nil)
(show-help :initarg :show-help :initform nil)
(info-manual :initarg :info-manual :initform nil)
(man-page :initarg :man-page :initform nil)
(transient-suffix :initarg :transient-suffix :initform nil)
(transient-non-suffix :initarg :transient-non-suffix :initform nil)
(transient-switch-frame :initarg :transient-switch-frame)
(refresh-suffixes :initarg :refresh-suffixes :initform nil)
(environment :initarg :environment :initform nil)
(incompatible :initarg :incompatible :initform nil)
(suffix-description :initarg :suffix-description)
(variable-pitch :initarg :variable-pitch :initform nil)
(column-widths :initarg :column-widths :initform nil)
(unwind-suffix :documentation "Internal use." :initform nil))
"Transient prefix command.
Each transient prefix command consists of a command, which is
stored in a symbol's function slot and an object, which is
stored in the `transient--prefix' property of the same symbol.
When a transient prefix command is invoked, then a clone of that
object is stored in the global variable `transient--prefix' and
the prototype is stored in the clone's `prototype' slot.")
;;;; Suffix
(defclass transient-child ()
((level
:initarg :level
:initform (symbol-value 'transient--default-child-level)
:documentation "Enable if level of prefix is equal or greater.")
(if
:initarg :if
:initform nil
:documentation "Enable if predicate returns non-nil.")
(if-not
:initarg :if-not
:initform nil
:documentation "Enable if predicate returns nil.")
(if-non-nil
:initarg :if-non-nil
:initform nil
:documentation "Enable if variable's value is non-nil.")
(if-nil
:initarg :if-nil
:initform nil
:documentation "Enable if variable's value is nil.")
(if-mode
:initarg :if-mode
:initform nil
:documentation "Enable if major-mode matches value.")
(if-not-mode
:initarg :if-not-mode
:initform nil
:documentation "Enable if major-mode does not match value.")
(if-derived
:initarg :if-derived
:initform nil
:documentation "Enable if major-mode derives from value.")
(if-not-derived
:initarg :if-not-derived
:initform nil
:documentation "Enable if major-mode does not derive from value.")
(inapt
:initform nil)
(inapt-face
:initarg :inapt-face
:initform 'transient-inapt-suffix)
(inapt-if
:initarg :inapt-if
:initform nil
:documentation "Inapt if predicate returns non-nil.")
(inapt-if-not
:initarg :inapt-if-not
:initform nil
:documentation "Inapt if predicate returns nil.")
(inapt-if-non-nil
:initarg :inapt-if-non-nil
:initform nil
:documentation "Inapt if variable's value is non-nil.")
(inapt-if-nil
:initarg :inapt-if-nil
:initform nil
:documentation "Inapt if variable's value is nil.")
(inapt-if-mode
:initarg :inapt-if-mode
:initform nil
:documentation "Inapt if major-mode matches value.")
(inapt-if-not-mode
:initarg :inapt-if-not-mode
:initform nil
:documentation "Inapt if major-mode does not match value.")
(inapt-if-derived
:initarg :inapt-if-derived
:initform nil
:documentation "Inapt if major-mode derives from value.")
(inapt-if-not-derived
:initarg :inapt-if-not-derived
:initform nil
:documentation "Inapt if major-mode does not derive from value."))
"Abstract superclass for group and suffix classes.
It is undefined which predicates are used if more than one `if*'
predicate slots or more than one `inapt-if*' slots are non-nil."
:abstract t)
(defclass transient-suffix (transient-child)
((definition :allocation :class :initform nil)
(key :initarg :key)
(command :initarg :command)
(transient :initarg :transient)
(format :initarg :format :initform " %k %d")
(description :initarg :description :initform nil)
(face :initarg :face :initform nil)
(show-help :initarg :show-help :initform nil)
(summary :initarg :summary :initform nil))
"Superclass for suffix command.")
(defclass transient-information (transient-suffix)
((format :initform " %k %d")
(key :initform " "))
"Display-only information, aligned with suffix keys.
Technically a suffix object with no associated command.")
(defclass transient-information* (transient-information)
((format :initform " %d"))
"Display-only information, aligned with suffix descriptions.
Technically a suffix object with no associated command.")
(defclass transient-infix (transient-suffix)
((transient :initform t)
(argument :initarg :argument)
(shortarg :initarg :shortarg)
(value :initform nil)
(init-value :initarg :init-value)
(unsavable :initarg :unsavable :initform nil)
(multi-value :initarg :multi-value :initform nil)
(always-read :initarg :always-read :initform nil)
(allow-empty :initarg :allow-empty :initform nil)
(history-key :initarg :history-key :initform nil)
(reader :initarg :reader :initform nil)
(prompt :initarg :prompt :initform nil)
(choices :initarg :choices :initform nil)
(format :initform " %k %d (%v)"))
"Transient infix command."
:abstract t)
(defclass transient-argument (transient-infix) ()
"Abstract superclass for infix arguments."
:abstract t)
(defclass transient-switch (transient-argument) ()
"Class used for command-line argument that can be turned on and off.")
(defclass transient-option (transient-argument) ()
"Class used for command-line argument that can take a value.")
(defclass transient-variable (transient-infix)
((variable :initarg :variable)
(format :initform " %k %d %v"))
"Abstract superclass for infix commands that set a variable."
:abstract t)
(defclass transient-switches (transient-argument)
((argument-format :initarg :argument-format)
(argument-regexp :initarg :argument-regexp))
"Class used for sets of mutually exclusive command-line switches.")
(defclass transient-files (transient-option) ()
((key :initform "--")
(argument :initform "--")
(multi-value :initform rest)
(reader :initform transient-read-files))
"Class used for the \"--\" argument or similar.
All remaining arguments are treated as files.
They become the value of this argument.")
(defclass transient-value-preset (transient-suffix)
((transient :initform t)
(set :initarg := :initform nil))
"Class used by the `transient-preset' suffix command.")
;;;; Group
(defclass transient-group (transient-child)
((suffixes :initarg :suffixes :initform nil)
(hide :initarg :hide :initform nil)
(description :initarg :description :initform nil)
(pad-keys :initarg :pad-keys :initform nil)
(info-format :initarg :info-format :initform nil)
(setup-children :initarg :setup-children))
"Abstract superclass of all group classes."
:abstract t)
(defclass transient-column (transient-group) ()
"Group class that displays each element on a separate line.")
(defclass transient-row (transient-group) ()
"Group class that displays all elements on a single line.")
(defclass transient-columns (transient-group) ()
"Group class that displays elements organized in columns.
Direct elements have to be groups whose elements have to be
commands or strings. Each subgroup represents a column.
This class takes care of inserting the subgroups' elements.")
(defclass transient-subgroups (transient-group) ()
"Group class that wraps other groups.
Direct elements have to be groups whose elements have to be
commands or strings. This group inserts an empty line between
subgroups. The subgroups are responsible for displaying their
elements themselves.")
;;; Define
(defmacro transient-define-prefix (name arglist &rest args)
"Define NAME as a transient prefix command.
ARGLIST are the arguments that command takes.
DOCSTRING is the documentation string and is optional.
These arguments can optionally be followed by key-value pairs.
Each key has to be a keyword symbol, either `:class' or a keyword
argument supported by the constructor of that class. The
`transient-prefix' class is used if the class is not specified
explicitly.
GROUPs add key bindings for infix and suffix commands and specify
how these bindings are presented in the popup buffer. At least
one GROUP has to be specified. See info node `(transient)Binding
Suffix and Infix Commands'.
The BODY is optional. If it is omitted, then ARGLIST is also
ignored and the function definition becomes:
(lambda ()
(interactive)
(transient-setup \\='NAME))
If BODY is specified, then it must begin with an `interactive'
form that matches ARGLIST, and it must call `transient-setup'.
It may however call that function only when some condition is
satisfied; that is one of the reason why you might want to use
an explicit BODY.
All transients have a (possibly nil) value, which is exported
when suffix commands are called, so that they can consume that
value. For some transients it might be necessary to have a sort
of secondary value, called a scope. Such a scope would usually
be set in the commands `interactive' form and has to be passed
to the setup function:
(transient-setup \\='NAME nil nil :scope SCOPE)
\(fn NAME ARGLIST [DOCSTRING] [KEYWORD VALUE]... GROUP... [BODY...])"
(declare (debug ( &define name lambda-list
[&optional lambda-doc]
[&rest keywordp sexp]
[&rest vectorp]
[&optional ("interactive" interactive) def-body]))
(indent defun)
(doc-string 3))
(pcase-let
((`(,class ,slots ,suffixes ,docstr ,body ,interactive-only)
(transient--expand-define-args args arglist 'transient-define-prefix)))
`(progn
(defalias ',name
,(if body
`(lambda ,arglist ,@body)
`(lambda ()
(interactive)
(transient-setup ',name))))
(put ',name 'interactive-only ,interactive-only)
(put ',name 'function-documentation ,docstr)
(put ',name 'transient--prefix
(,(or class 'transient-prefix) :command ',name ,@slots))
(put ',name 'transient--layout
(list ,@(cl-mapcan (lambda (s) (transient--parse-child name s))
suffixes))))))
(defmacro transient-define-suffix (name arglist &rest args)
"Define NAME as a transient suffix command.
ARGLIST are the arguments that the command takes.
DOCSTRING is the documentation string and is optional.
These arguments can optionally be followed by key-value pairs.
Each key has to be a keyword symbol, either `:class' or a
keyword argument supported by the constructor of that class.
The `transient-suffix' class is used if the class is not
specified explicitly.
The BODY must begin with an `interactive' form that matches
ARGLIST. The infix arguments are usually accessed by using
`transient-args' inside `interactive'.
\(fn NAME ARGLIST [DOCSTRING] [KEYWORD VALUE]... [BODY...])"
(declare (debug ( &define name lambda-list
[&optional lambda-doc]
[&rest keywordp sexp]
[&optional ("interactive" interactive) def-body]))
(indent defun)
(doc-string 3))
(pcase-let
((`(,class ,slots ,_ ,docstr ,body ,interactive-only)
(transient--expand-define-args args arglist 'transient-define-suffix)))
`(progn
(defalias ',name
,(if (and (not body) class (oref-default class definition))
`(oref-default ',class definition)
`(lambda ,arglist ,@body)))
(put ',name 'interactive-only ,interactive-only)
(put ',name 'function-documentation ,docstr)
(put ',name 'transient--suffix
(,(or class 'transient-suffix) :command ',name ,@slots)))))
(defmacro transient-augment-suffix (name &rest args)
"Augment existing command NAME with a new transient suffix object.
Similar to `transient-define-suffix' but define a suffix object only.
\n\(fn NAME [KEYWORD VALUE]...)"
(declare (debug (&define name [&rest keywordp sexp]))
(indent defun))
(pcase-let
((`(,class ,slots)
(transient--expand-define-args args nil 'transient-augment-suffix t)))
`(put ',name 'transient--suffix
(,(or class 'transient-suffix) :command ',name ,@slots))))
(defmacro transient-define-infix (name arglist &rest args)
"Define NAME as a transient infix command.
ARGLIST is always ignored and reserved for future use.
DOCSTRING is the documentation string and is optional.