-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgrugru.el
1682 lines (1419 loc) · 62.4 KB
/
grugru.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
;;; grugru.el --- Rotate text at point -*- lexical-binding: t; -*-
;; Copyright (C) 2019-2023 ROCKTAKEY
;; Author: ROCKTAKEY <[email protected]>
;; Keywords: convenience, abbrev, tools
;; Version: 1.22.5
;; Package-Requires: ((emacs "24.4"))
;; URL: https://github.com/ROCKTAKEY/grugru
;; 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:
;; Table of Contents
;; _________________
;; 1. Grugru: Rotate text at point.
;; 2. How to Use?
;; .. 1. Examples
;; 3. Interactive Functions
;; .. 1. `grugru'
;; .. 2. `grugru-select'
;; .. 3. `grugru-edit'
;; 4. Functions Defining grugru
;; .. 1. `(grugru-define-global GETTER STRINGS-OR-FUNCTION)'
;; .. 2. `(grugru-define-on-major-mode MAJOR GETTER STRINGS-OR-FUNCTION)'
;; .. 3. `(grugru-define-local GETTER STRINGS-OR-FUNCTION)'
;; .. 4. `(grugru-define-multiple &rest CLAUSES)'
;; .. 5. `(grugru-define-function NAME () &optional DOCSTRING &rest BODY)'
;; 5. Utilities to define grugru
;; .. 1. `(grugru-utils-lisp-exchange-args LIST-STRING PERMUTATION)'
;; ..... 1. Usage
;; 6. Custom Variables
;; .. 1. `grugru-getter-alist'
;; .. 2. `grugru-edit-save-file'
;; .. 3. `grugru-completing-function'
;; .. 4. `grugru-select-function-generate-number'
;; .. 5. `grugru-local-interactively-default-getter'
;; .. 6. `grugru-point-after-rotate'
;; .. 7. `grugru-indent-after-rotate'
;; .. 8. `grugru-strings-metagenerator'
;; 7. leaf-keyword `:grugru'
;; 8. License
;; <https://raw.githubusercontent.com/ROCKTAKEY/images/4524403fbcdd9abe6d88197eddb1c4d241046e72/grugru.png>
;; [https://img.shields.io/github/tag/ROCKTAKEY/grugru.svg?style=flat-square]
;; [https://img.shields.io/github/license/ROCKTAKEY/grugru.svg?style=flat-square]
;; [https://img.shields.io/github/actions/workflow/status/ROCKTAKEY/grugru/CI.yml.svg?style=flat-square]
;; [https://img.shields.io/codecov/c/github/ROCKTAKEY/grugru/master.svg?style=flat-square]
;; [file:https://melpa.org/packages/grugru-badge.svg]
;; [https://img.shields.io/github/tag/ROCKTAKEY/grugru.svg?style=flat-square]
;; <https://github.com/ROCKTAKEY/grugru>
;; [https://img.shields.io/github/license/ROCKTAKEY/grugru.svg?style=flat-square]
;; <file:LICENSE>
;; [https://img.shields.io/github/actions/workflow/status/ROCKTAKEY/grugru/CI.yml.svg?style=flat-square]
;; <https://github.com/ROCKTAKEY/grugru/actions>
;; [https://img.shields.io/codecov/c/github/ROCKTAKEY/grugru/master.svg?style=flat-square]
;; <https://codecov.io/gh/ROCKTAKEY/grugru?branch=master>
;; [file:https://melpa.org/packages/grugru-badge.svg]
;; <https://melpa.org/#/grugru>
;; 1 Grugru: Rotate text at point.
;; ===============================
;; With this package, you can rotate things at point.
;; <https://raw.githubusercontent.com/ROCKTAKEY/images/7baf9507a8fb9c20eda7395be1c9d91d0ae61c51/emacs-lisp-mode.gif>
;; Fig. 1 demo on `emacs-lisp-mode'
;; <https://raw.githubusercontent.com/ROCKTAKEY/images/35e323db33f4da1545c289f2741782c4ac04968b/c++-mode.gif>
;; Fig. 2 demo on `c++-mode'
;; <https://raw.githubusercontent.com/ROCKTAKEY/images/698f33489645a6e7b0c29d879771dbb15fa3fcd9/grugru-define-local.gif>
;; Fig. 3 Use `grugru-define-local' interactively
;; 2 How to Use?
;; =============
;; You can interactively use the function `grugru'. This function rotate
;; the thing at point if assigned. You can assign rotated things with
;; `grugru-define-on-major-mode', `grugru-define-on-local-major-mode',
;; and `grugru-define-local'. If you use `grugru', you should assign
;; `grugru' to 1 stroke key like `C-;', or `M-g'.
;; ,----
;; | (global-set-key (kbd "C-;") #'grugru) ; Or other key.
;; `----
;; If you want use default grugru, eval `grugru-default-setup'. In the
;; other words, add to your init.el:
;; ,----
;; | (grugru-default-setup)
;; `----
;; If you want `grugru' to highlight gurgruable thing, add to your
;; init.el:
;; ,----
;; | (grugru-highlight-mode)
;; `----
;; If you want to change default action at point, you can use
;; `grugru-edit', with which you can edit grugrus at point
;; interactively. The change edited by this function is saved in
;; `grugru-edit-save-file', and loaded by run `grugru-edit-load'. So to
;; load the change, you can write on init.el after
;; `(grugru-default-setup)':
;; ,----
;; | (grugru-edit-load)
;; `----
;; If you want to use ivy or ido as completing-read, set
;; `grugru-edit-completing-function'. Or, you can use
;; `grugru-redefine-\*' or `grugru-remove-\*' for non-interactive editing
;; of default setup.
;; 2.1 Examples
;; ~~~~~~~~~~~~
;; ,----
;; | 1 ;; Define grugru on major-mode.
;; | 2 (grugru-define-on-major-mode 'c-mode 'symbol '("unsigned" "signed"))
;; | 3 (grugru-define-on-major-mode 'c-mode 'word '("get" "set"))
;; | 4 ;; Now, you can toggle unsigned <=> signed and get <=> set
;; | 5 ;; by running the command grugru in c-mode.
;; | 6
;; | 7 ;; You can pass a list of symbol major-mode instead of one.
;; | 8 (grugru-define-on-major-mode '(java-mode c++-mode) 'word '("get" "set"))
;; | 9
;; | 10 ;; Define grugru on current major-mode.
;; | 11 ;; Same as (grugru-define-on-major-mode major-mode 'symbol '("red" "green" "yellow"))
;; | 12 ;; This should be run in some hook or function,
;; | 13 ;; because major-mode is not confirmed if in init.el.
;; | 14 (add-hook 'c-mode-common-hook
;; | 15 (lambda ()
;; | 16 (grugru-define-on-local-major-mode 'symbol '("red" "green" "yellow"))))
;; | 17
;; | 18 ;; Define grugru on local. Should be defined in some hook or function,
;; | 19 ;; because it is saved buffer local.
;; | 20 (add-hook 'text-mode-hook
;; | 21 (lambda ()
;; | 22 (grugru-define-local 'word '("is" "was"))
;; | 23 (grugru-define-local 'word '("I" "my" "me" "mine"))))
;; | 24
;; | 25 ;; Define grugru globally. This is applied in all buffers.
;; | 26 (grugru-define-global 'symbol '("yes" "no"))
;; | 27
;; | 28 ;; Define grugru keeping case:
;; | 29 (grugru-define-global 'symbol (grugru-metagenerator-keep-case '("yes" "no")))
;; | 30
;; | 31 ;; If you want grugru to define grugru defaultly keeping case:
;; | 32 (customize-set-variable 'grugru-strings-metagenerator #'grugru-metagenerator-simple)
;; | 33
;; | 34 ;; You can use function instead of list of strings.
;; | 35 (grugru-define-on-major-mode
;; | 36 'c-mode 'symbol
;; | 37 ;; Optional argument `rev' is flag for backward rotation.
;; | 38 ;; If the second argument `rev' is ignoreable (for example, rotate two strings),
;; | 39 ;; you can just use the function receiving only 1 argument.
;; | 40 (lambda (arg &optional rev)
;; | 41 (if rev
;; | 42 ;; backward
;; | 43 (cond
;; | 44 ((string-match "a\\(.*\\)b" arg)
;; | 45 ;; Rotate axyzb to cxyzd
;; | 46 (concat "c" (match-string 1 arg) "d"))
;; | 47 ((string-match "b\\(.*\\)c" arg)
;; | 48 ;; Rotate bxyzc to axyzb
;; | 49 (concat "a" (match-string 1 arg) "b"))
;; | 50 ((string-match "c\\(.*\\)d" arg)
;; | 51 ;; Rotate cxyzd to bxyzc
;; | 52 (concat "b" (match-string 1 arg) "c")))
;; | 53 ;; forward
;; | 54 (cond
;; | 55 ((string-match "a\\(.*\\)b" arg)
;; | 56 ;; Rotate axyzb to bxyzc
;; | 57 (concat "b" (match-string 1 arg) "c"))
;; | 58 ((string-match "b\\(.*\\)c" arg)
;; | 59 ;; Rotate bxyzc to cxyzd
;; | 60 (concat "c" (match-string 1 arg) "d"))
;; | 61 ((string-match "c\\(.*\\)d" arg)
;; | 62 ;; Rotate cxyzd to axyzb
;; | 63 (concat "a" (match-string 1 arg) "b"))))))
;; | 64
;; | 65 ;; You can indicate which position is valid to grugru in strings.
;; | 66 ;; The function can return not only string but also cons cell (BOUNDS . STRING).
;; | 67 ;; BOUNDS is a list of cons cell (BEG . END), which is pair of numbers indicating
;; | 68 ;; range valid to rotate.
;; | 69 (defun grugru-default@emacs-lisp+nth!aref (str)
;; | 70 "Return STR exchanged `nth' and `aref' with argument permutation."
;; | 71 (cond
;; | 72 ((string-match "^(\\_<\\(nth\\)\\_>" str)
;; | 73 (cons
;; | 74 (cons (match-beginning 1) (match-end 1))
;; | 75 ;; This function permutate arguments on Lisp.
;; | 76 (grugru-utils-lisp-exchange-args
;; | 77 (replace-match "aref" nil nil str 1)
;; | 78 '(2 1))))
;; | 79 ((string-match "^(\\_<\\(aref\\)\\_>" str)
;; | 80 (cons
;; | 81 (cons (match-beginning 1) (match-end 1))
;; | 82 (grugru-utils-lisp-exchange-args
;; | 83 (replace-match "nth" nil nil str 1)
;; | 84 '(2 1))))))
;; | 85
;; | 86 ;; You can also write like:
;; | 87 (grugru-define-multiple
;; | 88 (fundamental-mode
;; | 89 . ((word . ("aaa" "bbb" "ccc"))
;; | 90 ;; (symbol "xxx" "yyy" "zzz") is same as below.
;; | 91 ;; You can use both.
;; | 92 (symbol . ("xxx" "yyy" "zzz"))
;; | 93 (word . ("abc" "def" "ghi"))))
;; | 94 (word . ("aaaa" "bbbb" "cccc"))
;; | 95 (symbol . ("xxxx" "yyyyy" "zzzzz"))
;; | 96 (word . ("abcd" "defd" "ghid")))
;; | 97 ;; or
;; | 98 (grugru-define-multiple
;; | 99 (fundamental-mode
;; | 100 (word "aaa" "bbb" "ccc")
;; | 101 (symbol "xxx" "yyy" "zzz")
;; | 102 (word "abc" "def" "ghi"))
;; | 103 (word "aaaa" "bbbb" "cccc")
;; | 104 (symbol "xxxx" "yyyyy" "zzzzz")
;; | 105 (word "abcd" "defd" "ghid"))
;; | 106
;; | 107 ;; Above two examples are both expanded to:
;; | 108 (progn
;; | 109 (progn
;; | 110 (grugru-define-on-major-mode 'fundamental-mode 'word '("aaa" "bbb" "ccc"))
;; | 111 (grugru-define-on-major-mode 'fundamental-mode 'symbol '("xxx" "yyy" "zzz"))
;; | 112 (grugru-define-on-major-mode 'fundamental-mode 'word '("abc" "def" "ghi")))
;; | 113 (grugru-define-global 'word '("aaaa" "bbbb" "cccc"))
;; | 114 (grugru-define-global 'symbol '("xxxx" "yyyyy" "zzzzz"))
;; | 115 (grugru-define-global 'word '("abcd" "defd" "ghid")))
;; | 116
;; | 117
;; | 118 ;; You can define function which rotate pre-specified texts.
;; | 119 ;; For example, three-state can rotate only 2 tuples,
;; | 120 ;; ("water" "ice" "vapor") and ("solid" "liquid" "gas"),
;; | 121 ;; not any other tuples defined by grugru-define-global and so on.
;; | 122 (grugru-define-function three-state ()
;; | 123 "Docstring. This is optional."
;; | 124 (symbol . ("water" "ice" "vapor"))
;; | 125 (symbol . ("solid" "liquid" "gas")))
;; | 126 ;; If you want to find the functions defined by `grugru-define-function'
;; | 127 ;; with `describe-function', execute this:
;; | 128 (grugru-find-function-integration-mode +1)
;; `----
;; 3 Interactive Functions
;; =======================
;; 3.1 `grugru'
;; ~~~~~~~~~~~~
;; This function rotates text at point. Rotated text is defined by
;; `grugru-define-*' functions. If prefix argument is passed, repeatedly
;; executed. Negative prefix arguments means backward rotation. Also,
;; `grugru-backward' can be used for backward rotation.
;; 3.2 `grugru-select'
;; ~~~~~~~~~~~~~~~~~~~
;; This function replace text at point. You can select grugru and string
;; replaced to.
;; You can assign completing function to `grugru-completing-function'.
;; 3.3 `grugru-edit'
;; ~~~~~~~~~~~~~~~~~
;; This function edits grugru at point defined by default.
;; First, select grugru from grugrus available at point. Then, edit the
;; list in minibuffer.
;; The change is saved to file `grugru-edit-save-file' if it is not
;; `local' one. You can assign completing function to
;; `grugru-completing-function'.
;; 4 Functions Defining grugru
;; ===========================
;; 4.1 `(grugru-define-global GETTER STRINGS-OR-FUNCTION)'
;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;; Define global grugru with GETTER and STRINGS-OR-FUNCTION.
;; GETTER is a function, or a symbol which is alias defined in
;; `grugru-getter-alist'. GETTER also can be positive or negative
;; number, which means the number of characters after point. By default,
;; symbol, word, char is available. If it is a function, it should
;; return cons cell `(begin . end)' which express things at point, and
;; with no argument.
;; STRINGS-OR-FUNCTION is list of string or function.
;; List of string: If it includes string gotten by GETTER, the things
;; gotten by GETTER is replaced to next string.
;; Function: It is passed things gotten by GETTER, and should return
;; string to replace the things to.
;; You can use like:
;; ,----
;; | ;; Replace "yes" at point, to "no".
;; | ;; Or replace "no" at point, to "yes".
;; | (grugru-define-global 'symbol '("yes" "no"))
;; `----
;; 4.2 `(grugru-define-on-major-mode MAJOR GETTER STRINGS-OR-FUNCTION)'
;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;; Define major-mode local grugru with GETTER and STRINGS-OR-FUNCTION.
;; Same as `grugru-define-global', but grugru defined with this is
;; applied only in buffer on MAJOR major-mode. MAJOR can be list of
;; major-modes.
;; ,----
;; | ;; Replace "yes" at point, to "no", or replace "no" at point, to "yes",
;; | ;; only in lisp-interaction-mode.
;; | (grugru-define-on-major-mode lisp-interaction-mode 'symbol '("yes" "no"))
;; `----
;; 4.3 `(grugru-define-local GETTER STRINGS-OR-FUNCTION)'
;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;; Define buffer-local grugru with GETTER and STRINGS-OR-FUNCTION.
;; Same as `grugru-define-global', but grugru defined with this is
;; applied only in buffer where eval this expression.
;; ,----
;; | ;; This should be used in hook or others.
;; | ;; Because this definition is buffer-local.
;; | (add-hook 'text-mode-hook
;; | (lambda ()
;; | (grugru-define-local 'word '("is" "was"))
;; | (grugru-define-local 'word '("I" "my" "me" "mine"))))
;; `----
;; Also, you can run it interactively (though cannot set
;; STRINGS-OR-FUNCTION to a function). On interactive usage, by default,
;; GETTER is the length of car of STRINGS-OR-FUNCTION, and
;; STRINGS-OR-FUNCTION is a list which has 2 elements, constructed
;; interactively. With prefix argument, you can select GETTER and length
;; of STRINGS-OR-FUNCTION. Default GETTER is set by
;; `grugru-local-interactively-default-getter'.
;; 4.4 `(grugru-define-multiple &rest CLAUSES)'
;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;; This function define multiple grugru.
;; Each `CLAUSE' is:
;; - `(GETTER . STRINGS-OR-FUNCTION)': means `(grugru-define-global
;; GETTER STRINGS-OR-FUNCTION)'.
;; - `(MAJOR (GETTER . STRINGS-OR-FUNCTION)...)': means
;; `(grugru-define-on-major-mode MAJOR GETTER STRINGS-OR-FUNCTION)...'.
;; - List of above.
;; ,----
;; | (grugru-define-multiple
;; | (fundamental-mode
;; | . ((word . ("aaa" "bbb" "ccc"))
;; | ;; (symbol "xxx" "yyy" "zzz") is same as below.
;; | ;; You can use both.
;; | (symbol . ("xxx" "yyy" "zzz"))
;; | (word . ("abc" "def" "ghi"))))
;; | (word . ("aaaa" "bbbb" "cccc"))
;; | (symbol . ("xxxx" "yyyyy" "zzzzz"))
;; | (word . ("abcd" "defd" "ghid")))
;; | ;; or
;; | (grugru-define-multiple
;; | (fundamental-mode
;; | (word "aaa" "bbb" "ccc")
;; | (symbol "xxx" "yyy" "zzz")
;; | (word "abc" "def" "ghi"))
;; | (word "aaaa" "bbbb" "cccc")
;; | (symbol "xxxx" "yyyyy" "zzzzz")
;; | (word "abcd" "defd" "ghid"))
;; |
;; | ;; Above two examples are both expanded to:
;; | (progn
;; | (progn
;; | (grugru-define-on-major-mode 'fundamental-mode 'word '("aaa" "bbb" "ccc"))
;; | (grugru-define-on-major-mode 'fundamental-mode 'symbol '("xxx" "yyy" "zzz"))
;; | (grugru-define-on-major-mode 'fundamental-mode 'word '("abc" "def" "ghi")))
;; | (grugru-define-global 'word '("aaaa" "bbbb" "cccc"))
;; | (grugru-define-global 'symbol '("xxxx" "yyyyy" "zzzzz"))
;; | (grugru-define-global 'word '("abcd" "defd" "ghid")))
;; `----
;; 4.5 `(grugru-define-function NAME () &optional DOCSTRING &rest BODY)'
;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;; Define function which can roate only grugru defined by BODY. Each
;; element of BODY is `(GETTER . STRINGS-OR-FUNCTION)', which meaning is
;; same as `grugru-define-*' functions.
;; ,----
;; | ;; The function `three-state' rotate like "water"=>"ice"=>"vapor"=>"water",
;; | ;; or "solid"=>"liquid"=>"gas"=>"solid".
;; | (grugru-define-function three-state ()
;; | "Docstring. This is optional."
;; | (symbol . ("water" "ice" "vapor"))
;; | (symbol . ("solid" "liquid" "gas")))
;; |
;; | ;; This sentense do NOT affect to the function `three-state'.
;; | (grugru-define-global 'symbol '("yes" "no"))
;; `----
;; 5 Utilities to define grugru
;; ============================
;; 5.1 `(grugru-utils-lisp-exchange-args LIST-STRING PERMUTATION)'
;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;; Permute argument of sexp read from `LIST-STRING' according to
;; `PERMUTATION'.
;; For example, `(grugru-utils-lisp-exchange-args \"(nth 1 '(x y z))\"
;; '(2 1))' returns `(nth '(x y z) 1)'. Newlines and whitespaces are
;; also kept.
;; This function is defined for user to define the function for grugru
;; which rotate not only fuction's name but also arguments' order.
;; 5.1.1 Usage
;; -----------
;; ,----
;; | (defun grugru-rotate-nth-aref (str)
;; | (cond
;; | ((string-match "^(\\(\\_<nth\\_>\\)" str) ;match to "(nth"
;; | (grugru-utils-lisp-exchange-args
;; | (replace-match "aref" nil nil str 1) ;replace function's name to "aref"
;; | '(2 1))) ;exchange arguments' order
;; | ((string-match "^(\\(\\_<aref\\_>\\)" str) ;match to "(aref"
;; | (grugru-utils-lisp-exchange-args
;; | (replace-match "nth" nil nil str 1) ;replace function's name to "nth"
;; | '(2 1))))) ;exchange arguments' order
;; | (grugru-define-on-major-mode
;; | 'emacs-lisp-mode
;; | 'list
;; | #'grugru-rotate-nth-aref)
;; |
;; | ;; Then,
;; | (nth 3 '(foo bar))
;; | ;; is rotated to:
;; | (aref '(foo bar) 3)
;; `----
;; 6 Custom Variables
;; ==================
;; 6.1 `grugru-getter-alist'
;; ~~~~~~~~~~~~~~~~~~~~~~~~~
;; Alist of getter.
;; Each key (car) of element is a symbol, which is regarded as `GETTER'.
;; Each value (cdr) of element is a function or sexp. It should return
;; things at point.
;; 6.2 `grugru-edit-save-file'
;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~
;; The name of file saved the information by `grugru-edit'. Default
;; value is "~/.emacs.d/.grugru".
;; 6.3 `grugru-completing-function'
;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;; Completing function. Default value is `completing-read'. If you would
;; like to use ivy or ido, write:
;; ,----
;; | ;; For ivy:
;; | (setq grugru-completing-function #'ivy-completing-read)
;; | ;; For ido:
;; | (setq grugru-completing-function #'ido-completing-read)
;; `----
;; 6.4 `grugru-select-function-generate-number'
;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;; This variable have how many strings are generated from function in
;; `STRINGS-OR-FUNCTION', on `grugru-select'.
;; 6.5 `grugru-local-interactively-default-getter'
;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;; Indicate default getter on interactive usage of `grugru-define-local'.
;; 0 means If 0, gets number from first string, otherwise it should be
;; symbol in `grugru-getter-alist' or a function which gets things at
;; point.
;; 6.6 `grugru-point-after-rotate'
;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;; Where the point is after rotation by `grugru'.
;; - `as-is' means keeping first position.
;; - `beginning' means beginning of rotated things.
;; - `end' means end of rotated things.
;; 6.7 `grugru-indent-after-rotate'
;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;; Indent rotated text after `grugru' or not. Indent happens only if
;; text after rotation has a newline.
;; ,----
;; | (grugru-define-local 'list '("(abc def)" "(ghi\njkl)"))
;; | ;; If `grugru-indent-after-rotate' is nil,
;; | (abc def)
;; | ;; is rotated to:
;; | (ghi
;; | jkl)
;; |
;; | ;; If `grugru-indent-after-rotate' is t,
;; | (abc def)
;; | ;; is rotated to:
;; | (ghi
;; | jkl)
;; `----
;; 6.8 `grugru-strings-metagenerator'
;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;; Function which generates default generator from strings on
;; `grugru-define-*'. The function should recieve `STRINGS', list of
;; string, as one argument, and return function. Returned function
;; should recieve one or two argument(s), string `STRING' as first one,
;; boolean `REVERSE' as second one.
;; STRING means current string. Returned function (generator) returns
;; string next to STRING. If REVERSE is non-nil, it returns previous one
;; instead.
;; 7 leaf-keyword `:grugru'
;; ========================
;; You can use `:grugru' keyword on [leaf.el], if you use
;; [leaf-keywords.el].
;; By default, `leaf--name' is used as major-mode. Or you can write
;; major-mode obviously.
;; ,----
;; | (leaf lisp-mode
;; | :grugru
;; | (symbol "nil" "t")
;; | (emacs-lisp-mode
;; | (word "add" "remove"))
;; | ...)
;; | ;; The section of `:grugru' means:
;; | (grugru-define-multiple
;; | (lisp-mode
;; | (symbol "nil" "t"))
;; | (emacs-lisp-mode
;; | (word "add" "remove")))
;; `----
;; [leaf.el] <https://github.com/conao3/leaf.el>
;; [leaf-keywords.el] <https://github.com/conao3/leaf-keywords.el>
;; 8 License
;; =========
;; This package is licensed by GPLv3. See [LICENSE].
;; [LICENSE] <file:LICENSE>
;;; Code:
(require 'cl-lib)
(require 'thingatpt)
(require 'subword)
(defgroup grugru ()
"Group for grugru."
:group 'convenience
:group 'abbrev
:group 'tools
:prefix "grugru")
(defcustom grugru-getter-alist
'((defun . (bounds-of-thing-at-point 'defun))
(symbol . (bounds-of-thing-at-point 'symbol))
(word . grugru--getter-word)
(char . (unless (= (point) (point-max)) (cons (point) (1+ (point)))))
(list . (bounds-of-thing-at-point 'list))
(line . (bounds-of-thing-at-point 'line))
(non-alphabet . grugru--getter-non-alphabet)
(tex-command . grugru--getter-tex-command))
"An alist of getter of current thing.
Each element should be (SYMBOL . FUNCTION-OR-SEXP). SYMBOL is used to access to
SEXP by `grugru'. FUNCTION-OR-SEXP should be sexp or getter function,
which return cons cell whose car/cdr is beginning/end point of current thing."
:group 'grugru
:risky t
:type '(&rest (symbolp . [functionp sexp])))
(defcustom grugru-edit-save-file
(expand-file-name ".grugru" user-emacs-directory)
"File which has saved data, provided by `grugru-edit'.
If it is nil, `grugru-edit' never saves data."
:group 'grugru
:type '(choice string
(const nil)))
(define-obsolete-variable-alias 'grugru-edit-completing-function
'grugru-completing-function
"1.10.0")
(defcustom grugru-completing-function #'completing-read
"Completing read function used `grugru-edit'.
You can also use `ivy-completing-read' or `ido-completing-read'."
:group 'grugru
:risky t
:type 'function)
(defcustom grugru-select-function-generate-number 30
"Max number of strings which are generated by generator on `grugru-select'."
:group 'grugru
:type 'number)
(defcustom grugru-local-interactively-default-getter 0
"Default getter of `grugru-define-local' on interactive usage.
If 0, use the length of the first string."
:group 'grugru
:risky t
:type '(choice (const 0) function symbol))
(defcustom grugru-point-after-rotate 'as-is
"Where the point is after rotation by `grugru'.
`as-is' means keeping first position.
`beginning' means beginning of rotated things.
`end' means end of rotated things."
:group 'grugru
:type '(choice
(const as-is)
(const beginning)
(const end)))
(defcustom grugru-indent-after-rotate t
"Indent rotated text after `grugru' or not.
Indent happens only if text after rotation has a newline."
:group 'grugru
:type 'boolean)
(defcustom grugru-strings-metagenerator #'grugru-metagenerator-simple
"Function which generates default generator from strings on `grugru-define-*'.
The function should recieve STRINGS, list of string, as one argument,
and return function. Returned function should recieve one or two argument(s),
string STRING as first one, boolean REVERSE as second one.
STRING means current string. Returned function returns string next to STRING.
If REVERSE is non-nil, it returns previous one instead."
:group 'grugru
:type '(choice (const grugru-metagenerator-simple)
(const grugru-metagenerator-keep-case)
function)
:risky t)
(defcustom grugru-before-hook nil
"Hooks run before rotation by `grugru'."
:group 'grugru
:type 'hook)
(defcustom grugru-after-hook nil
"Hooks run after rotation by `grugru'."
:group 'grugru
:type 'hook)
(defcustom grugru-after-no-rotate-hook nil
"Hooks run after `grugru' tries to rotate text but cannot rotate."
:group 'grugru
:type 'hook)
(defvar grugru--major-modes-grugru-alist '()
"An alist of rotated text on each `major-mode'.
Each element should be (MAJOR-MODE . ALIST).
ALIST is compounded from (GETTER . STRINGS-OR-GENERATOR).
GETTER is symbol in `grugru-getter-alist', sexp or getter function.
See also `grugru-getter-alist'.
STRINGS-OR-GENERATOR can be a list of strings or generator,
function which recieves current thing as an argument and returns next text.
You can add element to this with `grugru-define-on-major-mode',
or `grugru-define-on-major-mode'.")
(defvar grugru--global-grugru-alist '()
"This variable keeps global list of (GETTER . STRINGS-OR-GENERATOR).
GETTER is symbol in `grugru-getter-alist', sexp or getter function.
See also `grugru-getter-alist'.
STRINGS-OR-GENERATOR can be a list of strings or generator,
function which recieves current thing as an argument and returns next text.
You can add element to this with `grugru-define-global'.")
(defvar grugru--local-interactively-history nil)
(defvar-local grugru--buffer-local-grugru-alist '()
"This variable keeps buffer-local list of (GETTER . STRINGS-OR-GENERATOR).
GETTER is symbol in `grugru-getter-alist', sexp or getter function.
See also `grugru-getter-alist'.
STRINGS-OR-GENERATOR can be a list of strings or generator,
function which recieves current thing as an argument and returns next text.
You can add element to this with `grugru-define-local'.")
(defvar-local grugru--buffer-local-major-mode-grugru-alist '()
"This variable keeps major-mode-specific list of (GETTER . STRINGS-OR-GENERATOR).
GETTER is symbol in `grugru-getter-alist', sexp or getter function.
See also `grugru-getter-alist'.
STRINGS-OR-GENERATOR can be a list of strings or generator,
function which recieves current thing as an argument and returns next text.
This variable is automatically loaded from `grugru--major-mode-grugru-alist'
by `grugru--major-mode-load'.")
(defvar-local grugru--loaded-local nil
"Whether the buffer load grugru list or not, on the buffer.
Global grugru is not observed, because `grugru' is remake rotated sets of list.")
(defvar grugru--point-cache nil
"Cache for keep position on sequentially executed `grugru'.")
;;; Metagetter
(defconst grugru--non-alphabet-regexp
(mapconcat
(lambda (arg)
(regexp-quote (string arg)))
"-^\\@;:,\\./=~|`+*<>?_!\"#$%&'"
"\\|")
"Regexp which match non alphabet character.
Used in `grugru--getter-non-alphabet'.")
(defun grugru--metagetter-from-regexp (regexp)
"Get beginning/end of string at point matched to REGEXP repeatedly."
(let ((beg
(save-excursion
(let ((bef (point)))
(while (and (<= (point-min) (point))
(re-search-backward regexp nil t)
(eq (match-end 0) bef))
(setq bef (point)))
bef)))
(end
(save-excursion
(let ((bef (point)))
(while (and (<= (point) (point-max))
(re-search-forward regexp nil t)
(eq (match-beginning 0) bef))
(setq bef (point)))
bef))))
(unless (= beg end)
(cons beg end))))
(defun grugru--metagetter-with-integer (number)
"Get beginning/end of string at point by NUMBER characters.
NUMBER can be negative."
(let ((p (+ (point)))
(q (+ (point) number)))
(when (and (<= (point-min) p) (<= (point-min) q)
(<= p (point-max)) (<= q (point-max)))
(cons (min p q) (max p q)))))
;;; Getter
(defun grugru--getter-word ()
"Get beginning/end of word at point."
(if (or (eq (point) (point-at-eol))
(string-match "[-\\[\\]_:;&+^~|#$!?%'()<>=*{}.,/\\\\\n\t]\\| "
(buffer-substring (point) (1+ (point)))))
(save-excursion (cons (subword-left) (subword-right)))
(save-excursion
(let ((x (subword-right))
(y (subword-left)))
(cons y x)))))
(defun grugru--getter-non-alphabet ()
"Get beginning/end non-alphabet string sequence at point."
(grugru--metagetter-from-regexp grugru--non-alphabet-regexp))
(defun grugru--getter-tex-command ()
"Get beginning/end of TeX command string sequence at point.
For example, \"\\alpha\", \"\\mathrm\" is valid sequence."
(save-excursion
(when (and
(not (eq (point) (point-max)))
(string= (buffer-substring-no-properties (point) (1+ (point))) "\\"))
(goto-char (1+ (point))))
(let ((cons (grugru--metagetter-from-regexp "[a-zA-Z]")))
(when (and cons
(not (eq (point-min) (car cons)))
(string=
(buffer-substring-no-properties (1- (car cons)) (car cons))
"\\"))
(cons (1- (car cons)) (cdr cons))))))
;;; Load major-mode-local `grugru'
(defun grugru--major-mode-load ()
"Load major mode local grugru in current buffer."
(setq grugru--buffer-local-major-mode-grugru-alist
(cdr (assq major-mode grugru--major-modes-grugru-alist)))
(setq grugru--loaded-local t))
(add-hook 'change-major-mode-after-body-hook #'grugru--major-mode-load)
(defun grugru--major-mode-set-as-unloaded (major)
"Mark buffers on MAJOR `major-mode' as unloaded."
(mapcar (lambda (arg)
(with-current-buffer arg
(when (or (eq major major-mode)
(and (listp major) (memq major-mode major)))
(setq grugru--loaded-local nil))))
(buffer-list)))
;;; Metagenerator
(defun grugru--metagenerator-simple (strings string &optional reverse)
"Return string next to STRING in STRINGS.
If REVERSE is non-nil, return previous STRING."
(when reverse
(setq strings (reverse strings)))
(let ((match-list (member string strings)))
(when match-list
(or (cadr match-list)
(car strings)))))
(defun grugru-metagenerator-simple (strings)
"Generate generator from STRINGS."
(apply-partially #'grugru--metagenerator-simple strings))
(defun grugru--metagenerator-keep-case (strings string &optional reverse)
"Return string next to STRING in STRINGS.
If REVERSE is non-nil, return previous STRING.
This function is not case-sensitive and keeps case."
(when reverse
(setq strings (nreverse strings)))
(let ((match-list
(member-ignore-case string strings)))
(when match-list
(let ((case-insensitive-result (or (cadr match-list)
(car strings))))
(cond
((string= string (capitalize string))
(capitalize case-insensitive-result))
((string= string (upcase string))
(upcase case-insensitive-result))
((string= string (downcase string))
(downcase case-insensitive-result))
(t
(grugru--metagenerator-simple strings string reverse)))))))
(defun grugru-metagenerator-keep-case (strings)
"Generate generator from STRINGS.
Generated generator is not case-sensitive and keeps case."
(apply-partially #'grugru--metagenerator-keep-case strings))
;;; Miscs
(defmacro grugru--set-cons (car cdr value)
"Set CAR and CDR to car and cdr of VALUE."
(let ((temp (cl-gensym)))
`(let ((,temp ,value))
(setf (cons ,car ,cdr) ,temp))))
(defun grugru--get-valid-bound (point valid-bounds)
"Return bound if POINT is among VALID-BOUNDS.
VALID-BOUNDS is list of cons cell (BEG . END), which is pair of numbers
indicating range valid to rotate."
(cl-some
(lambda (bound)
(let ((begin (car bound))
(end (cdr bound)))
(when (and (<= begin point) (<= point end))
bound)))
valid-bounds))
(defun grugru--get-generator (strings-or-generator)
"Return generator from STRINGS-OR-GENERATOR."
(if (functionp strings-or-generator) strings-or-generator
(funcall grugru-strings-metagenerator strings-or-generator)))
(defun grugru--call-generator (generator string reverse)
"Call GENERATOR with STRING and REVERSE.
When REVERSE is non-nil, ignore the `wrong-number-of-arguments' error."
(if reverse
(condition-case nil
(funcall generator string t)
('wrong-number-of-arguments nil))
(funcall generator string)))
(defun grugru--get-next-string (string strings-or-generator &optional point reverse)
"Get next string of STRING with STRINGS-OR-GENERATOR.
POINT is relative from beggining of STRING. POINT is used when valid-bounds are
detected.
This function returns cons cell (valid-bounds . next-string).
If REVERSE is non-nil, get previous string instead."
(let* ((generator (grugru--get-generator strings-or-generator))
(result (grugru--call-generator generator string reverse))
(valid-bounds (car-safe result))
(next-string (or (cdr-safe result) result))
valid-bound)
(when (and (not (null valid-bounds))
(listp valid-bounds)
(not (consp (car valid-bounds))))
(setq valid-bounds (list valid-bounds)))
(setq valid-bound
(if valid-bounds
(grugru--get-valid-bound point valid-bounds)
(cons 0 (length string))))
(when (and next-string valid-bound)
(cons valid-bound next-string))))
(defun grugru--get-getter-function (getter)
"Get getter function from GETTER."
(setq getter (or (cdr (assq getter grugru-getter-alist)) getter))
(pcase getter
((pred functionp)
getter)
((pred integerp)
(apply-partially #'grugru--metagetter-with-integer getter))
(_ `(lambda () ,getter))))
(defun grugru--get-plist (alist &optional only-one reverse)
"Return plist list constructed with ALIST.
If ONLY-ONE is non-nil, returned value is 1 plist, which matches first.
Each element of ALIST is (SYMBOL . GRUGRU-ALIST).
Each element of GRUGRU-ALIST is (GETTER . STRINGS-OR-GENERATOR), which is same