forked from positron-solutions/transient-showcase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransient-showcase.el
1315 lines (1077 loc) · 46.8 KB
/
transient-showcase.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-showcase.el --- transient features & behavior showcase -*- lexical-binding: t; -*-
;; Copyright (C) 2022 Positron Solutions
;; Author: Psionik K <[email protected]>
;; Keywords: convenience
;; Version: 0.1.0
;; Package-Requires: ((emacs "28.1"))
;; Homepage: http://github.com/positron-solutions/transient-showcase
;;; License notice:
;; This program 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.
;; This program 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:
;; This package is created from the README and serves as a fast way to load
;; all of the examples without tangling the org document. This is appropriate
;; if you just want to quickly browse through the examples and see their
;; source code.
;;
;; M-x tsc-showcase contains most of the prefixes and can be bound for
;; use as a quick reference. Just use transient's help for each
;; command to see the source. C-h <suffix key>.
;;
;;; Code:
(require 'transient)
(require 'org-id)
(defun tsc-suffix-wave ()
"Wave at the user."
(interactive)
(message "Waves at the user at: %s." (current-time-string)))
(defvar tsc-busy nil "Are we busy?")
(defun tsc--busy-p () "Are we busy?" tsc-busy)
(transient-define-suffix tsc--toggle-busy ()
"Toggle busy."
(interactive)
(setf tsc-busy (not tsc-busy))
(message (propertize (format "busy: %s" tsc-busy)
'face 'success)))
(transient-define-suffix tsc-suffix-show-level ()
"Show the current transient's level."
:transient t
(interactive)
(message "Current level: %s" (oref transient-current-prefix level)))
;; Because command names are used to store and lookup child levels, we have
;; define a macro to generate unqiquely named wavers. See #153 at
;; https://github.com/magit/transient/issues/153
(defmacro tsc--define-waver (name)
"Define a new suffix with NAME tsc--wave-NAME."
`(transient-define-suffix ,(intern (format "tsc--wave-%s" name)) ()
,(format "Wave at the user %s" name)
:transient t
(interactive)
(message (format "Waves at %s" (current-time-string)))))
;; Each form results in a unique suffix definition.
(tsc--define-waver "surely")
(tsc--define-waver "normally")
(tsc--define-waver "non-essentially")
(tsc--define-waver "definitely")
(tsc--define-waver "eventually")
(tsc--define-waver "hidden")
(transient-define-suffix tsc-suffix-print-args (the-prefix-arg)
"Report the PREFIX-ARG, prefix's scope, and infix values."
:transient 'transient--do-call
(interactive "P")
(let ((args (transient-args (oref transient-current-prefix command)))
(scope (oref transient-current-prefix scope)))
(message "prefix-arg: %s \nprefix's scope value: %s \ntransient-args: %s"
the-prefix-arg scope args)))
;; tsc-suffix-print-args command is incidentally created
(transient-define-prefix tsc-hello ()
"Prefix that is minimal and uses an anonymous command suffix."
[("s" "call suffix"
(lambda ()
(interactive)
(message "Called a suffix")))])
;; First, use M-x org-babel-execute-src-blk to cause `tsc-hello' to be
;; defined
;; Second, M-x `eval-last-sexp' with your point at the end of the line below
;; (tsc-hello)
(transient-define-suffix tsc-suffix-wave-macroed ()
"Prefix that waves with macro-defined suffix."
:transient t
:key "T"
:description "wave from macro definition"
(interactive)
(message "Waves from a macro definition at: %s" (current-time-string)))
;; Suffix definition creates a command
;; (tsc-suffix-wave-macroed)
;; Because that's where the suffix object is stored
;; (get 'tsc-suffix-wave-macroed 'transient--suffix)
;; tsc-suffix-wave-suffix defined above
(transient-define-prefix tsc-wave-macro-defined ()
"Prefix to wave using a macro-defined suffix."
[(tsc-suffix-wave-macroed)])
;; note, information moved from prefix to the suffix.
;; (tsc-wave-macro-defined)
(defun tsc--wave-override ()
"Vanilla command used to override suffix's commands."
(interactive)
(message "This suffix was overridden. I am what remains."))
(transient-define-prefix tsc-wave-overridden ()
"Prefix that waves with overridden suffix behavior."
[(tsc-suffix-wave-macroed
:transient nil
:key "O"
:description "wave overridingly"
:command tsc--wave-override)]) ; we overrode what the suffix even does
;; (tsc-wave-overridden)
(transient-define-prefix tsc-layout-descriptions ()
"Prefix with descriptions specified with slots."
["Let's Give This Transient a Title\n" ; yes the newline works
["Group One"
("wo" "wave once" tsc-suffix-wave)
("wa" "wave again" tsc-suffix-wave)]
["Group Two"
("ws" "wave some" tsc-suffix-wave)
("wb" "wave better" tsc-suffix-wave)]]
["Bad title" :description "Group of Groups"
["Group Three"
("k" "bad desc" tsc-suffix-wave :description "key-value wins")
("n" tsc-suffix-wave :description "no desc necessary")]
[:description "Key Only Def"
("wt" "wave too much" tsc-suffix-wave)
("we" "wave excessively" tsc-suffix-wave)]])
;; (tsc-layout-descriptions)
(transient-define-prefix tsc-layout-dynamic-descriptions ()
"Prefix that generate descriptions dynamically when transient is shown."
;; group using function-name to generate description
[:description current-time-string
("-s" "--switch" "switch=") ; switch just to cause updates
;; single suffix with dynamic description
("wa" tsc-suffix-wave
:description (lambda ()
(format "Wave at %s" (current-time-string))))]
;; group with anonymoous function generating description
[:description (lambda ()
(format "Group %s" (org-id-new)))
("wu" "wave uniquely" tsc-suffix-wave)])
;; (tsc-layout-dynamic-descriptions)
(defun tsc--random-info ()
(format "Temperature outside: %d" (random 100)))
(transient-define-prefix tsc-information ()
"Prefix that displays some information."
["Group Header"
(:info "Basic info")
(:info #'tsc--random-info)
(:info "Use :format to remove whitespace" :format "%d")
("k" :info "Keys will be greyed out")
() ; empty line
("wg" "wave greenishly" tsc-suffix-wave)])
(transient-define-prefix tsc-layout-stacked ()
"Prefix with layout that stacks groups on top of each other."
["Top Group" ("wt" "wave top" tsc-suffix-wave)]
["Bottom Group" ("wb" "wave bottom" tsc-suffix-wave)])
;; (tsc-layout-stacked)
(transient-define-prefix tsc-layout-columns ()
"Prefix with side-by-side layout."
[["Left Group" ("wl" "wave left" tsc-suffix-wave)]
["Right Group" ("wr" "wave right" tsc-suffix-wave)]])
;; (tsc-layout-columns)
(transient-define-prefix tsc-layout-stacked-columns ()
"Prefix with stacked columns layout."
["Top Group"
("wt" "wave top" tsc-suffix-wave)]
[["Left Group"
("wl" "wave left" tsc-suffix-wave)]
["Right Group"
("wr" "wave right" tsc-suffix-wave)]])
;; (tsc-layout-stacked-columns)
(transient-define-prefix tsc-layout-spaced-out ()
"Prefix lots of spacing for users to space out at."
["" ; cannot add another empty string because it will mix suffixes with groups
["Left Group"
""
("wl" "wave left" tsc-suffix-wave)
("L" "wave lefter" tsc-suffix-wave)
""
("bl" "wave bottom-left" tsc-suffix-wave)
("z" "zone\n" zone)] ; the newline does pad
[[]] ; empty vector will do nothing
[""] ; vector with just empty line has no effect
;; empty group will be ignored
;; (useful for hiding in dynamic layouts)
["Empty Group\n"]
["Right Group"
""
("wr" "wave right" tsc-suffix-wave)
("R" "wave righter" tsc-suffix-wave)
""
("br" "wave bottom-right" tsc-suffix-wave)]])
;; (tsc-layout-spaced-out)
(transient-define-prefix tsc-layout-the-grid ()
"Prefix with groups in a grid-like arrangement."
[:description
"The Grid\n" ; must use slot or macro is confused
["Left Column" ; note, no newline
("ltt" "left top top" tsc-suffix-wave)
("ltb" "left top bottom" tsc-suffix-wave)
""
("lbt" "left bottom top" tsc-suffix-wave)
("lbb" "left bottom bottom" tsc-suffix-wave)] ; note, no newline
["Right Column\n"
("rtt" "right top top" tsc-suffix-wave)
("rtb" "right top bottom" tsc-suffix-wave)
""
("rbt" "right bottom top" tsc-suffix-wave)
("rbb" "right bottom bottom\n" tsc-suffix-wave)]])
;; (tsc-layout-the-grid)
(transient-define-prefix tsc-layout-explicit-classes ()
"Prefix with group class used to explicitly specify layout."
[:class transient-row "Row"
("l" "wave left" tsc-suffix-wave)
("r" "wave right" tsc-suffix-wave)]
[:class transient-column "Column"
("t" "wave top" tsc-suffix-wave)
("b" "wave bottom" tsc-suffix-wave)])
;; (tsc-layout-explicit-classes)
(transient-define-prefix tsc-stay-transient ()
"Prefix where some suffixes do not exit."
["Exit or Not?"
;; this suffix will not exit after calling sub-prefix
("we" "wave & exit" tsc-wave-overridden)
("ws" "wave & stay" tsc-suffix-wave :transient t)])
;; (tsc-stay-transient)
(transient-define-prefix tsc--simple-child ()
["Simple Child"
("wc" "wave childishly" tsc-suffix-wave)])
(transient-define-prefix tsc-simple-parent ()
"Prefix that calls a child prefix."
["Simple Parent"
("w" "wave parentally" tsc-suffix-wave)
("b" "become child" tsc--simple-child)])
;; (tsc--simple-child)
;; (tsc-simple-parent)
(transient-define-prefix tsc-simple-parent-with-return ()
"Prefix with a child prefix that returns."
["Parent With Return"
("w" "wave parentally" tsc-suffix-wave)
("b" "become child with return" tsc--simple-child :transient t)])
;; Child does not "return" when called independently
;; (tsc--simple-child)
;; (tsc-simple-parent-with-return)
(transient-define-suffix tsc-suffix-setup-child ()
"A suffix that uses `transient-setup' to manually load another transient."
(interactive)
;; note that it's usually during the post-command side of calling the
;; command that the actual work to set up the transient will occur.
;; This is an implementation detail because it depends if we are calling
;; `transient-setup' while already transient or not.
(transient-setup 'tsc--simple-child))
(transient-define-prefix tsc-parent-with-setup-suffix ()
"Prefix with a suffix that calls `transient-setup'."
["Simple Parent"
("wp" "wave parentally" tsc-suffix-wave :transient t) ; remain transient
;; You may need to specify a different pre-command (the :transient) key
;; because we need to clean up this transient or create some conditions
;; to trigger the following transient correctly. This example will
;; work with `transient--do-replace' or no custom pre-command
("bc" "become child" tsc-suffix-setup-child
:transient transient--do-replace)])
;; (tsc-parent-with-setup-suffix)
(transient-define-suffix tsc--suffix-interactive-string (user-input)
"An interactive suffix that obtains string input from the user."
(interactive "sPlease just tell me what you want!: ")
(message "I think you want: %s" user-input))
(transient-define-suffix tsc--suffix-interactive-buffer-name (buffer-name)
"An interactive suffix that obtains a buffer name from the user."
(interactive "b")
(message "You selected: %s" buffer-name))
(transient-define-prefix tsc-interactive-basic ()
"Prefix with interactive user input."
["Interactive Command Suffixes"
("s" "enter string" tsc--suffix-interactive-string)
("b" "select buffer" tsc--suffix-interactive-buffer-name)])
;; (tsc-interactive-basic)
(defvar tsc--complex nil "Show complex menu or not.")
(transient-define-suffix tsc--toggle-complex ()
"Toggle `tsc--complex'."
:transient t
:description (lambda () (format "toggle complex: %s" tsc--complex))
(interactive)
(setf tsc--complex (not tsc--complex))
(message (propertize (concat "Complexity set to: "
(if tsc--complex "true" "false"))
'face 'success)))
(transient-define-prefix tsc-complex-messager ()
"Prefix that sends complex messages, unles `tsc--complex' is nil."
["Send Complex Messages"
("s" "snow people"
(lambda () (interactive)
(message (propertize "snow people! ☃" 'face 'success))))
("k" "kitty cats"
(lambda () (interactive)
(message (propertize "🐈 kitty cats! 🐈" 'face 'success))))
("r" "radiations"
(lambda () (interactive)
(message (propertize "Oh no! radiation! ☢" 'face 'success)))
;; radiation is dangerous!
:transient transient--do-exit)]
(interactive)
;; The command body either sets up the transient or simply returns
;; This is the "early return" we're talking about.
(if tsc--complex
(transient-setup 'tsc-complex-messager)
(message "Simple and boring!")))
(transient-define-prefix tsc-simple-messager ()
"Prefix that toggles child behavior!"
[["Send Message"
;; using `transient--do-recurse' causes suffixes in tsc-child to perform
;; `transient--do-return' so that we come back to this transient.
("m" "message" tsc-complex-messager :transient transient--do-recurse)]
["Toggle Complexity"
("t" tsc--toggle-complex)]])
;; (tsc-simple-messager)
;; does not "return" when called independently
;; (tsc-complex-messager)
;; infix defined with a macro
(transient-define-argument tsc--exclusive-switches ()
"This is a specialized infix for only selecting one of several values."
:class 'transient-switches
:argument-format "--%s-snowcone"
:argument-regexp "\\(--\\(grape\\|orange\\|cherry\\|lime\\)-snowcone\\)"
:choices '("grape" "orange" "cherry" "lime"))
(transient-define-prefix tsc-basic-infixes ()
"Prefix that just shows off many typical infix types."
["Infixes"
;; from macro
("-e" "exclusive switches" tsc--exclusive-switches)
;; shorthand definitions
("-b" "switch with shortarg" ("-w" "--switch-short"))
;; note :short-arg != :key
("-s" "switch" "--switch")
( "n" "no dash switch" "still works")
("-a" "argument" "--argument=" :prompt "Let's argue because: ")
;; a bit of inline EIEIO in our shorthand
("-n" "never empty" "--non-null=" :always-read t :allow-empty nil
:init-value (lambda (obj) (oset obj value "better-than-nothing")))
("-c" "choices" "--choice=" :choices (foo bar baz))]
["Show Args"
("s" "show arguments" tsc-suffix-print-args)])
;; (tsc-basic-infixes)
(transient-define-suffix tsc--read-prefix-scope ()
"Read the scope of the prefix."
:transient 'transient--do-call
(interactive)
(let ((scope (oref transient-current-prefix scope)))
(message "scope: %s" scope)))
(transient-define-suffix tsc--double-scope-re-enter ()
"Re-enter the current prefix with double the scope."
;; :transient 'transient--do-replace ; builds up the stack
:transient 'transient--do-exit
(interactive)
(let ((scope (oref transient-current-prefix scope)))
(if (numberp scope)
(transient-setup transient-current-command
nil nil :scope (* scope 2))
(message
(propertize
(format "scope was non-numeric! %s" scope) 'face 'warning))
(transient-setup transient-current-command))))
(transient-define-suffix tsc--update-scope-with-prefix-re-enter (new-scope)
"Re-enter the prefix with double the scope."
;; :transient 'transient--do-replace ; builds up the stack
:transient 'transient--do-exit ; do not build up the stack
(interactive "P")
(message "universal arg: %s" new-scope)
(transient-setup transient-current-command nil nil :scope new-scope))
(transient-define-prefix tsc-scope (scope)
"Prefix demonstrating use of scope."
;; note! this is a location where we definitely had to use
;; `transient--prefix' or get the transient object from the tsc-scope
;; symbol. `transient-current-prefix' would not be correct here!
[:description
(lambda () (format "Scope: %s"
(oref transient--prefix scope)))
[("r" "read scope" tsc--read-prefix-scope)
("d" "double scope" tsc--double-scope-re-enter)
("o" "update scope (use prefix argument)"
tsc--update-scope-with-prefix-re-enter)]]
(interactive "P")
(transient-setup 'tsc-scope nil nil :scope scope))
;; Setting an interactive argument for `eval-last-sexp' is a
;; little different
;; (let ((current-prefix-arg 4)) (call-interactively 'tsc-scope))
;; (tsc-scope)
;; Then press "C-u 4 o" to update the scope
;; Then d to double
;; Then r to read
;; ... and so on
;; C-g to exit
(transient-define-suffix tsc-suffix-eat-snowcone (args)
"Eat the snowcone!
This command can be called from it's parent, `tsc-snowcone-eater' or independently."
:transient t
;; you can use the interactive form of a command to obtain a default value
;; from the user etc if the one obtained from the parent is invalid.
(interactive (list (transient-args 'tsc-snowcone-eater)))
;; `transient-arg-value' can (with varying success) pick out individual
;; values from the results of `transient-args'.
(let ((topping (transient-arg-value "--topping=" args))
(flavor (transient-arg-value "--flavor=" args)))
(message "I ate a %s flavored snowcone with %s on top!" flavor topping)))
(transient-define-prefix tsc-snowcone-eater ()
"Prefix demonstrating set & save infix persistence."
;; This prefix has a default value that tsc-suffix-eat-snowcone can see
;; even before the prefix has been called.
:value '("--topping=fruit" "--flavor=cherry")
;; always-read is used below so that you don't save nil values to history
["Arguments"
("-t" "topping" "--topping="
:choices ("ice cream" "fruit" "whipped cream" "mochi")
:always-read t)
("-f" "flavor" "--flavor="
:choices ("grape" "orange" "cherry" "lime")
:always-read t)]
;; Definitely check out the =C-x= menu
["C-x Menu Behaviors"
("S" "save snowcone settings"
(lambda () (interactive) (message "saved!") (transient-save))
:transient t)
("R" "reset snowcone settings"
(lambda () (interactive) (message "reset!") (transient-reset))
:transient t)]
["Actions"
("m" "message arguments" tsc-suffix-print-args)
("e" "eat snowcone" tsc-suffix-eat-snowcone)])
;; First call will use the transient's default value
;; M-x tsc-suffix-eat-snowcone or `eval-last-sexp' below
;; (call-interactively 'tsc-suffix-eat-snowcone)
;; (tsc-snowcone-eater)
;; Eat some snowcones with different flavors
;; ...
;; ...
;; ...
;; Now save the value and exit the transient.
;; When you call the suffix independently, it can still read the saved values!
;; M-x tsc-suffix-eat-snowcone or `eval-last-sexp' below
;; (call-interactively 'tsc-suffix-eat-snowcone)
(transient-define-prefix tsc-ping ()
"Prefix demonstrating history sharing."
:history-key 'non-unique-name
["Ping"
("-g" "game" "--game=")
("p" "ping the pong" tsc-pong)
("a" "print args" tsc-suffix-print-args :transient nil)])
(transient-define-prefix tsc-pong ()
"Prefix demonstrating history sharing."
:history-key 'non-unique-name
["Pong"
("-g" "game" "--game=")
("p" "pong the ping" tsc-ping)
("a" "print args" tsc-suffix-print-args :transient nil)])
;; (tsc-ping)
;; Okay here's where it gets weird
;; 1. Set the value of game to something and remember it
;; 2. Press a to print the args
;; 3. Re-open tsc-ping.
;; 4. C-x p to load the previous history, see the old value?
;; 5. p to switch to the tsc-pong transient
;; 6. C-x p to load the previous history, see the old value from tsc-ping???
;; 7. Note that tsc-pong uses the same history as tsc-ping!
(transient-define-prefix tsc-goldfish ()
"A prefix that cannot remember anything."
["Goldfish"
("-r" "rememeber" "--i-remember="
:unsavable t ; infix isn't saved
:always-read t ; infix always asks for new value
;; overriding the method to provide a starting value
:init-value (lambda (obj) (oset obj value "nothing")))
("a" "print args" tsc-suffix-print-args :transient nil)])
;; (tsc-goldfish)
(transient-define-suffix tsc-suffix-remember-and-wave ()
"Wave, and force the prefix to set it's saveable infix values."
(interactive)
;; (transient-reset) ; forget
(transient-set) ; save for this session
;; If you combine reset with set, you get a reset for future sessions only.
;; (transient-save) ; save for this and future sessions
;; (transient-reset-value some-other-prefix-object)
(message "Waves at user at: %s. You will never be forgotten." (current-time-string)))
(transient-define-prefix tsc-elephant ()
"A prefix that always remembers its infixes."
["Elephant"
("-r" "rememeber" "--i-remember="
:always-read t)
("w" "remember and wave" tsc-suffix-remember-and-wave)
("a" "print args (skips remembering)" tsc-suffix-print-args
:transient nil)])
;; (tsc-elephant)
(transient-define-prefix tsc-default-values ()
"A prefix with a default value."
:value '("--toggle" "--value=5")
["Arguments"
("t" "toggle" "--toggle")
("v" "value" "--value=" :prompt "an integer: ")]
["Show Args"
("s" "show arguments" tsc-suffix-print-args)])
;; (tsc-default-values)
(transient-define-prefix tsc-enforcing-inputs ()
"A prefix with enforced input type."
["Arguments"
("v" "value" "--value=" :prompt "an integer: " :reader transient-read-number-N+)]
["Show Args"
("s" "show arguments" tsc-suffix-print-args)])
;; (tsc-enforcing-inputs)
(defvar tsc--position '(0 0) "A transient prefix location.")
(transient-define-infix tsc--pos-infix ()
"A location, key, or command symbol."
:class 'transient-lisp-variable
:transient t
:prompt "An expression such as (0 0), \"p\", nil, 'tsc--msg-pos: "
:variable 'tsc--position)
(transient-define-suffix tsc--msg-pos ()
"Message the element at location."
:transient 'transient--do-call
(interactive)
;; lisp variables are not sent in the usual (transient-args) list.
;; Just read `tsc--position' directly.
(let ((suffix (transient-get-suffix
transient-current-command tsc--position)))
(message "%s" (oref suffix description))))
(transient-define-prefix tsc-lisp-variable ()
"A prefix that updates and uses a lisp variable."
["Location Printing"
[("p" "position" tsc--pos-infix)]
[("m" "message" tsc--msg-pos)]])
;; (tsc-lisp-variable)
(transient-define-prefix tsc-switches-and-arguments (arg)
"A prefix with switch and argument examples."
[["Arguments"
("-s" "switch" "--switch")
("-a" "argument" "--argument=")
("t" "toggle" "--toggle")
("v" "value" "--value=")]
["More Arguments"
("-f" "argument with forced class" "--forced-class "
:class transient-option)
("I" "argument with inline" ("-i" "--inline-shortarg="))
("S" "inline shortarg switch" ("-n" "--inline-shortarg-switch"))]]
["Commands"
("w" "wave some" tsc-suffix-wave)
("s" "show arguments" tsc-suffix-print-args)])
;; use to `tsc-suffix-print-args' to analyze the switch values
;; (tsc-switches-and-arguments)
(transient-define-infix tsc--random-init-infix ()
"Switch on and off."
:argument "--switch"
:shortarg "-s" ; will be used for :key when key is not set
:description "switch"
:init-value (lambda (obj)
(oset obj value
(eq 0 (random 2))))) ; write t with 50% probability
(transient-define-prefix tsc-maybe-on ()
"A prefix with a randomly intializing switch."
["Arguments"
(tsc--random-init-infix)]
["Show Args"
("s" "show arguments" tsc-suffix-print-args)])
;; (tsc-maybe-on)
;; (tsc-maybe-on)
;; ...
;; Run the command a few times to see the random initialization of
;; `tsc--random-init-infix'
;; It will only take more than ten tries for one in a thousand users.
;; Good luck.
(transient-define-argument tsc--animals-argument ()
"Animal picker."
:argument "--animals="
;; :multi-value t
;; :multi-value t means multiple options can be selected at once, such as:
;; --animals=fox,otter,kitten etc
:class 'transient-option
:choices '("fox" "kitten" "peregrine" "otter"))
(transient-define-prefix tsc-animal-choices ()
"Prefix demonstrating selecting animals from choices."
["Arguments"
("-a" "--animals=" tsc--animals-argument)]
["Show Args"
("s" "show arguments" tsc-suffix-print-args)])
;; (tsc-animal-choices)
(transient-define-argument tsc--snowcone-flavor ()
:description "Flavor of snowcone."
:class 'transient-switches
:key "f"
:argument-format "--%s-snowcone"
:argument-regexp "\\(--\\(grape\\|orange\\|cherry\\|lime\\)-snowcone\\)"
:choices '("grape" "orange" "cherry" "lime"))
(transient-define-prefix tsc-exclusive-switches ()
"Prefix demonstrating exclusive switches."
:value '("--orange-snowcone")
["Arguments"
(tsc--snowcone-flavor)]
["Show Args"
("s" "show arguments" tsc-suffix-print-args)])
;; (tsc-exclusive-switches)
(transient-define-prefix tsc-incompatible ()
"Prefix demonstrating incompatible switches."
;; update your transient version if you experience #129 / #155
:incompatible '(("--switch" "--value=")
("--switch" "--toggle" "--flip")
("--argument=" "--value=" "--special-arg="))
["Arguments"
("-s" "switch" "--switch")
("-t" "toggle" "--toggle")
("-f" "flip" "--flip")
("-a" "argument" "--argument=")
("v" "value" "--value=")
("C-a" "special arg" "--special-arg=")]
["Show Args"
("s" "show arguments" tsc-suffix-print-args)])
;; (tsc-incompatible)
(defun tsc--animal-choices (_complete-me _predicate flag)
"Programmed completion for animal choice.
_COMPLETE-ME: whatever the user has typed so far
_PREDICATE: function you should use to filter candidates (only nil seen so far)
FLAG: request for metadata (which can be disrespected)"
;; if you want to respect metadata requests, here's what the form might
;; look like, but no behavior was observed.
(if (eq flag 'metadata)
'(metadata . '((annotation-function . (lambda (c) "an annotation"))))
;; when not handling a metadata request from completions, use some
;; logic to generate the choices, possibly based on input or some time
;; / context sensitive process. FLAG will be `t' when these are
;; reqeusted.
(if (eq 0 (random 2))
'("fox" "kitten" "otter")
'("ant" "peregrine" "zebra"))))
(transient-define-prefix tsc-choices-with-completions ()
"Prefix with completions for choices."
["Arguments"
("-a" "Animal" "--animal="
:always-read t ; don't allow unsetting, just read a new value
:choices tsc--animal-choices)]
["Show Args"
("s" "show arguments" tsc-suffix-print-args)])
;; (tsc-choices-with-completions)
(defun tsc--quit-cowsay ()
"Kill the cowsay buffer and exit."
(interactive)
(kill-buffer "*cowsay*"))
(defun tsc--cowsay-buffer-exists-p ()
"Visibility predicate."
(not (equal (get-buffer "*cowsay*") nil)))
(transient-define-suffix tsc--cowsay-clear-buffer (&optional buffer)
"Delete the *cowsay* buffer. Optional BUFFER name."
:transient 'transient--do-call
:if 'tsc--cowsay-buffer-exists-p
(interactive) ; todo look at "b" interactive code
(save-excursion
(let ((buffer (or buffer "*cowsay*")))
(set-buffer buffer)
(delete-region 1 (+ 1 (buffer-size))))))
(transient-define-suffix tsc--cowsay (&optional args)
"Run cowsay."
(interactive (list (transient-args transient-current-command)))
(let* ((buffer "*cowsay*")
;; TODO ugly
(cowmsg (if args (transient-arg-value "--message=" args) nil))
(cowmsg (if cowmsg (list cowmsg) nil))
(args (if args
(seq-filter
(lambda (s) (not (string-prefix-p "--message=" s))) args)
nil))
(args (if args
(if cowmsg
(append args cowmsg)
args)
cowmsg)))
(when (tsc--cowsay-buffer-exists-p)
(tsc--cowsay-clear-buffer))
(apply #'call-process "cowsay" nil buffer nil args)
(switch-to-buffer buffer)))
(transient-define-prefix tsc-cowsay ()
"Say things with animals!"
;; only one kind of eyes is meaningful at a time
:incompatible '(("-b" "-g" "-p" "-s" "-t" "-w" "-y"))
["Message"
("m" "message" "--message=" :always-read t)]
;; always-read, so clear by entering empty string
[["Built-in Eyes"
("b" "borg" "-b")
("g" "greedy" "-g")
("p" "paranoid" "-p")
("s" "stoned" "-s")
("t" "tired" "-t")
("w" "wired" "-w")
("y" "youthful" "-y")]
["Actions"
("c" "cowsay" tsc--cowsay :transient transient--do-call)
""
("d" "delete buffer" tsc--cowsay-clear-buffer)
("q" "quit" tsc--quit-cowsay)]])
;; (tsc-cowsay)
(transient-define-prefix tsc-visibility-predicates ()
"Prefix with visibility predicates.
Try opening this prefix in buffers with modes deriving from different
abstract major modes."
["Empty Groups Not Displayed"
;; in org mode for example, this group doesn't appear.
("we" "wave elisp" tsc-suffix-wave :if-mode emacs-lisp-mode)
("wc" "wave in C" tsc-suffix-wave :if-mode cc-mode)]
["Lists of Modes"
("wm" "wave multiply" tsc-suffix-wave :if-mode (dired-mode gnus-mode))]
[["Function Predicates"
;; note, after toggling, the transient needs to be re-displayed for the
;; predicate to take effect
("tb" "toggle busy" tsc--toggle-busy :transient t)
("bw" "wave busily" tsc-suffix-wave :if tsc--busy-p)]
["Programming Actions"
:if-derived prog-mode
("pw" "wave programishly" tsc-suffix-wave)
("pe" "wave in elisp" tsc-suffix-wave :if emacs-lisp-mode)]
["Special Mode Actions"
:if-derived special-mode
("sw" "wave specially" tsc-suffix-wave)
("sd" "wave dired" tsc-suffix-wave :if-mode dired-mode)]
["Text Mode Actions"
:if-derived text-mode
("tw" "wave textually" tsc-suffix-wave)
("to" "wave org-modeishly" tsc-suffix-wave :if-mode org-mode)]])
;; (tsc-visibility-predicates)
(defun tsc--child-scope-p ()
"Return the scope of the current transient.
When this is called in layouts, it's the transient being layed out"
(let ((scope (oref transient--prefix scope)))
(message "The scope is: %s" scope)
scope))
;; the wave suffixes were :transient t as defined, so we need to manually
;; override them to the `transient--do-return' value for :transient slot so
;; that they return back to the parent.
(transient-define-prefix tsc--inapt-children ()
"Prefix with children using inapt predicates."
["Inapt Predicates Child"
("s" "switched" tsc--wave-surely
:transient transient--do-return
:inapt-if tsc--child-scope-p)
("u" "unswitched" tsc--wave-normally
:transient transient--do-return
:inapt-if-not tsc--child-scope-p)]
;; in the body, we read the value of the parent and set our scope to
;; non-nil if the switch is set
(interactive)
(let ((scope (transient-arg-value "--switch"
(transient-args 'tsc-inapt-parent))))
(message "scope: %s" scope)
(message "type: %s" (type-of scope))
(transient-setup 'tsc--inapt-children nil nil :scope (if scope t nil))))
(transient-define-prefix tsc-inapt-parent ()
"Prefix that configures child with inapt predicates."
[("-s" "switch" "--switch")
("a" "show arguments" tsc-suffix-print-args)
("c" "launch child prefix" tsc--inapt-children
:transient transient--do-recurse)])
;; (tsc-inapt-parent)
(transient-define-prefix tsc-levels-and-visibility ()
"Prefix with visibility levels for hiding rarely used commands."
[["Setting the Current Level"
;; this binding is normally not displayed. The value of
;; `transient-show-common-commands' controls this by default.
("C-x l" "set level" transient-set-level)
("s" "show level" tsc-suffix-show-level)]
[2 "Per Group" ;; 1 is the default default-child-level
("ws" "wave surely" tsc--wave-surely)
(3"wn" "wave normally" tsc--wave-normally)
(5"wb" "wave non-essentially" tsc--wave-non-essentially)]
[3 "Per Group Somewhat Useful"
("wd" "wave definitely" tsc--wave-definitely)]
[6 "Groups hide visible children"
(1 "wh" "wave hidden" tsc--wave-hidden)]
[5 "Per Group Rarely Useful"
("we" "wave eventually" tsc--wave-eventually)]])
;; (tsc-levels-and-visibility)
(transient-define-prefix tsc-generated-child ()
"Prefix that uses `setup-children' to generate single child."
["Replace this child"
;; Let's override the group's method
:setup-children
(lambda (_) ; we don't care about the stupid suffix
;; remember to return a list
(list (transient-parse-suffix
transient--prefix
'("r" "replacement" (lambda ()
(interactive)
(message "okay!"))))))
("s" "haha stupid suffix" (lambda ()
(interactive)
(message "You should replace me!")))])
;; (tsc-generated-child)
(transient-define-prefix tsc-generated-group ()
"Prefix that uses `setup-children' to generate a group."
["Replace this child"
;; Let's override the group's method
:setup-children
(lambda (_) ; we don't care about the stupid suffix
;; the result of parsing here will be a group
(transient-parse-suffixes
transient--prefix
["Group Name" ("r" "replacement" (lambda ()
(interactive)
(message "okay!")))]))