-
Notifications
You must be signed in to change notification settings - Fork 83
/
xah-fly-keys.el
4163 lines (3613 loc) · 140 KB
/
xah-fly-keys.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
;;; xah-fly-keys.el --- ergonomic modal keybinding minor mode. -*- coding: utf-8; lexical-binding: t; -*-
;; Copyright © 2013, 2024 by Xah Lee
;; Author: Xah Lee ( http://xahlee.info/ )
;; Maintainer: Xah Lee <[email protected]>
;; Version: 26.9.20241225173150
;; Created: 2013-09-10
;; Package-Requires: ((emacs "27"))
;; Keywords: convenience, vi, vim, ergoemacs, keybinding
;; License: GPL v3.
;; Homepage: http://xahlee.info/emacs/misc/xah-fly-keys.html
;; This file is not part of GNU Emacs.
;;; Commentary:
;; xah-fly-keys is a efficient keybinding for emacs. It is modal like
;; vi, but key choices are based on statistics of command call
;; frequency.
;;; Usage:
;; M-x xah-fly-keys to toggle the mode on/off.
;; Important command/insert mode switch keys:
;; M-x `xah-fly-command-mode-activate'
;; or press F8 or Alt+Space or Ctrl+Space or <escape>.
;; Note: if using emacs 28 or before, escape key only works when in emacs is running in graphical user interface mode.
;; M-x `xah-fly-insert-mode-activate'
;; when in command mode, press qwerty letter key f.
;; When in command mode:
;; "f" calls `xah-fly-insert-mode-activate'.
;; Space is a leader key. For example, "SPC r" calls `query-replace'.
;; Press "SPC C-h" to see the full list.
;; "SPC SPC" also activates insertion mode.
;; "SPC RET" calls `execute-extended-command'.
;; "a" calls `execute-extended-command'.
;; The leader key sequence basically supplant ALL emacs commands that
;; starts with C-x key.
;; When using xah-fly-keys, you don't need to press Control or Meta,
;; with the following exceptions:
;; "C-c" for major mode commands.
;; "C-g" for cancel.
;; "C-q" for quoted-insert.
;; "C-h" for getting a list of keys following a prefix/leader key.
;; Leader key
;; You NEVER need to press "C-x"
;; Any emacs command that has a keybinding starting with C-x, has also
;; a key sequence binding in xah-fly-keys. For example,
;; "C-x b" for `switch-to-buffer' is "SPC f"
;; "C-x C-f" for `find-file' is "SPC i e"
;; "C-x n n" for `narrow-to-region' is "SPC l l"
;; The first key we call it leader key. In the above examples, the SPC
;; is the leader key.
;; When in command mode, the "SPC" is a leader key.
;; the following standard keys with Control are supported:
;; "C-TAB" `xah-next-user-buffer'
;; "C-S-TAB" `xah-previous-user-buffer'
;; "C-v" paste
;; "C-w" close
;; "C-z" undo
;; "C-n" new
;; "C-o" open
;; "C-s" save
;; "C-S-s" save as
;; "C-S-t" open last closed
;; "C-+" `text-scale-increase'
;; "C--" `text-scale-decrease'
;; To disable both Control and Meta shortcut keys, add the following
;; lines to you init.el BEFORE loading xah-fly-keys:
;; (setq xah-fly-use-control-key nil)
;; (setq xah-fly-use-meta-key nil)
;; If you have a bug, post on github.
;; For detail about design and other info, see home page at
;; http://xahlee.info/emacs/misc/xah-fly-keys.html
;; If you like this project, paypal me $30 to [email protected]
;;; Installation:
;; here's how to manual install
;;
;; put the file xah-fly-keys.el in ~/.emacs.d/lisp/
;; create the dir if doesn't exist.
;;
;; put the following in your emacs init file:
;; (add-to-list 'load-path "~/.emacs.d/lisp/")
;; (require 'xah-fly-keys)
;; (xah-fly-keys-set-layout "qwerty") ; optional
;; (xah-fly-keys 1)
;;
;; possible layout values:
;; adnw (German)
;; azerty
;; azerty-be
;; bepo (French)
;; colemak
;; colemak-dh
;; dvorak
;; engrammer
;; halmak
;; koy (German)
;; minimak
;; neo2 (German)
;; norman
;; programer-dvorak
;; pt-nativo (Brazil)
;; qfmlwy
;; qgmlwb
;; qwerty
;; qwerty-abnt (Brazil)
;; qwerty-no (Norwegian)
;; qwerty-se (Swedish)
;; qwertz
;; qwpr
;; russian
;; workman
;; supported layouts are stored in the variable xah-fly-layout-diagrams
;; HHHH---------------------------------------------------
;;; Code:
(require 'dired)
(require 'dired-x)
(require 'seq)
;; HHHH---------------------------------------------------
(defgroup xah-fly-keys nil
"Ergonomic modal keybinding minor mode."
:group 'keyboard)
(defvar xah-fly-command-mode-activate-hook nil "Hook for `xah-fly-command-mode-activate'")
(defvar xah-fly-insert-mode-activate-hook nil "Hook for `xah-fly-insert-mode-activate'")
(defvar xah-fly-command-mode-indicator "c"
"Character in mode line indicating command mode is active.")
(defvar xah-fly-insert-mode-indicator "i"
"Character in mode line indicating insert mode is active.")
(defcustom xah-fly-use-control-key nil
"If true, standard keys for open, close, copy, paste etc, are bound, and others.
If nil, do not modify any default emacs control key keybinding."
:type 'boolean)
(defcustom xah-fly-use-meta-key t
"If nil, do not bind any meta key."
:type 'boolean)
(defcustom xah-fly-use-isearch-arrows t
"If nil, no change to any key in isearch (`isearch-forward'). Otherwise, arrow keys are for moving between occurrences, and C-v is paste."
:type 'boolean)
(defun xah-fly-get-pos-block ()
"Return the begin end positions of current text block.
Return value is a `vector'.
Text block is group of lines separated by blank lines.
URL `http://xahlee.info/emacs/emacs/elisp_get_text_block.html'
Created: 2024-03-23
Version: 2024-10-07"
(let (xbeg xend (xp (point)))
(save-excursion
(setq xbeg (if (re-search-backward "\n[ \t]*\n" nil 1) (match-end 0) (point)))
(goto-char xp)
(setq xend (if (re-search-forward "\n[ \t]*\n" nil 1) (match-beginning 0) (point))))
(vector xbeg xend)))
(defun xah-fly-get-pos-block-or ()
"If region is active, return its [begin end] positions, else same as `xah-fly-get-pos-block'.
Return value is a `vector'.
Version: 2024-03-23"
(if (region-active-p)
(vector (region-beginning) (region-end))
(xah-fly-get-pos-block)))
;; HHHH---------------------------------------------------
;; cursor movement
(defun xah-pop-local-mark-ring ()
"Move cursor to last mark position of current buffer.
Repeat call cycles all positions in `mark-ring'.
URL `http://xahlee.info/emacs/emacs/emacs_cycle_local_mark_ring.html'
Created: 2016-04-04
Version: 2023-09-03"
(interactive)
(set-mark-command t))
(defun xah-beginning-of-line-or-block ()
"Move cursor to beginning of indent or line, end of previous block, in that order.
If `visual-line-mode' is on, beginning of line means visual line.
URL `http://xahlee.info/emacs/emacs/emacs_move_by_paragraph.html'
Created: 2018-06-04
Version: 2024-10-30"
(interactive)
(let ((xp (point)))
(if (or (eq (point) (line-beginning-position))
(eq last-command this-command))
(when (re-search-backward "\n[\t\n ]*\n+" nil :move)
(skip-chars-backward "\n\t ")
;; (forward-char)
)
(if visual-line-mode
(beginning-of-visual-line)
(if (eq major-mode 'eshell-mode)
(progn
(declare-function eshell-bol "esh-mode.el" ())
(eshell-bol))
(back-to-indentation)
(when (eq xp (point))
(beginning-of-line)))))))
(defun xah-end-of-line-or-block ()
"Move cursor to end of line or next block.
• When called first time, move cursor to end of line.
• When called again, move cursor forward by jumping over any sequence of whitespaces containing 2 blank lines.
• if `visual-line-mode' is on, end of line means visual line.
URL `http://xahlee.info/emacs/emacs/emacs_move_by_paragraph.html'
Created: 2018-06-04
Version: 2024-10-30"
(interactive)
(if (or (eq (point) (line-end-position))
(eq last-command this-command))
(re-search-forward "\n[\t\n ]*\n+" nil :move)
(if visual-line-mode
(end-of-visual-line)
(end-of-line))))
(defun xah-page-up ()
"Call `scroll-down-command'. (page up key.)
Created: 2024-10-09
Version: 2024-10-09"
(interactive)
(progn
(scroll-down-command)
(set-transient-map
(let ((xkmap (make-sparse-keymap)))
(define-key xkmap (kbd "<up>") #'xah-page-up)
(define-key xkmap (kbd "<down>") #'xah-page-down)
xkmap))))
(defun xah-page-down ()
"Call `scroll-up-command'. (page down key.)
Created: 2024-10-09
Version: 2024-10-09"
(interactive)
(progn
(scroll-up-command)
(set-transient-map
(let ((xkmap (make-sparse-keymap)))
(define-key xkmap (kbd "<up>") #'xah-page-up)
(define-key xkmap (kbd "<down>") #'xah-page-down)
xkmap))))
(defvar xah-brackets '( "“”" "()" "[]" "{}" "<>" "<>" "()" "[]" "{}" "⦅⦆" "〚〛" "⦃⦄" "‹›" "«»" "「」" "〈〉" "《》" "【】" "〔〕" "⦗⦘" "『』" "〖〗" "〘〙" "「」" "⟦⟧" "⟨⟩" "⟪⟫" "⟮⟯" "⟬⟭" "⌈⌉" "⌊⌋" "⦇⦈" "⦉⦊" "❛❜" "❝❞" "❨❩" "❪❫" "❴❵" "❬❭" "❮❯" "❰❱" "❲❳" "〈〉" "⦑⦒" "⧼⧽" "﹙﹚" "﹛﹜" "﹝﹞" "⁽⁾" "₍₎" "⦋⦌" "⦍⦎" "⦏⦐" "⁅⁆" "⸢⸣" "⸤⸥" "⟅⟆" "⦓⦔" "⦕⦖" "⸦⸧" "⸨⸩" "⦅⦆")
"A list of strings, each element is a string of 2 chars, the left bracket and a matching right bracket.
Used by `xah-select-text-in-quote' and others.")
(defconst xah-left-brackets
(mapcar (lambda (x) (substring x 0 1)) xah-brackets)
"List of left bracket chars. Each element is a string.")
(defconst xah-right-brackets
(mapcar (lambda (x) (substring x 1 2)) xah-brackets)
"List of right bracket chars. Each element is a string.")
(defun xah-backward-left-bracket ()
"Move cursor to the previous occurrence of left bracket.
The list of brackets to jump to is defined by `xah-left-brackets'.
URL `http://xahlee.info/emacs/emacs/emacs_navigating_keys_for_brackets.html'
Version: 2015-10-01"
(interactive)
(re-search-backward (regexp-opt xah-left-brackets) nil t))
(defun xah-forward-right-bracket ()
"Move cursor to the next occurrence of right bracket.
The list of brackets to jump to is defined by `xah-right-brackets'.
URL `http://xahlee.info/emacs/emacs/emacs_navigating_keys_for_brackets.html'
Version: 2015-10-01"
(interactive)
(re-search-forward (regexp-opt xah-right-brackets) nil t))
(defun xah-goto-matching-bracket ()
"Move cursor to the matching bracket.
If cursor is not on a bracket, call `backward-up-list'.
The list of brackets to jump to is defined by `xah-left-brackets' and `xah-right-brackets'.
URL `http://xahlee.info/emacs/emacs/emacs_navigating_keys_for_brackets.html'
Created: 2016-11-22
Version: 2024-06-15"
(interactive)
(if (nth 3 (syntax-ppss))
(backward-up-list 1 'ESCAPE-STRINGS 'NO-SYNTAX-CROSSING)
(cond
((eq (char-after) ?\") (forward-sexp))
((eq (char-before) ?\") (backward-sexp))
((looking-at (regexp-opt xah-left-brackets))
(forward-sexp))
((if (eq (point-min) (point))
nil
(prog2
(backward-char)
(looking-at (regexp-opt xah-right-brackets))
(forward-char)))
(backward-sexp)
(while (looking-at "\\s'") (forward-char)))
(t (backward-up-list 1 'ESCAPE-STRINGS 'NO-SYNTAX-CROSSING)))))
(defvar xah-punctuation-regex nil "A regex string for the purpose of moving cursor to a punctuation.")
(setq xah-punctuation-regex "[\"]")
(defun xah-forward-punct ()
"Move cursor to the next occurrence of punctuation.
Punctuations is defined by `xah-punctuation-regex'
URL `http://xahlee.info/emacs/emacs/emacs_jump_to_punctuations.html'
Created: 2017-06-26
Version: 2024-01-20"
(interactive)
(re-search-forward xah-punctuation-regex nil t))
(defun xah-backward-punct ()
"Move cursor to the previous occurrence of punctuation.
See `xah-forward-punct'
URL `http://xahlee.info/emacs/emacs/emacs_jump_to_punctuations.html'
Created: 2017-06-26
Version: 2024-01-20"
(interactive)
(re-search-backward xah-punctuation-regex nil t))
(defun xah-sort-lines ()
"Like `sort-lines' but if no region, do the current block.
Created: 2022-01-22
Version: 2024-03-19"
(interactive)
(let (xbeg xend)
(seq-setq (xbeg xend) (xah-fly-get-pos-block-or))
(sort-lines current-prefix-arg xbeg xend)))
(defun xah-narrow-to-region ()
"Same as `narrow-to-region', but if no selection, narrow to the current block.
Created: 2022-01-22
Version: 2024-03-19"
(interactive)
(let (xbeg xend)
(seq-setq (xbeg xend) (xah-fly-get-pos-block-or))
(narrow-to-region xbeg xend)))
;; HHHH---------------------------------------------------
;; editing commands
(defun xah-copy-line-or-region ()
"Copy current line or selection.
Copy current line. When called repeatedly, append copy subsequent lines.
Except:
If `universal-argument' is called first, copy whole buffer (respects `narrow-to-region').
If `rectangle-mark-mode' is on, copy the rectangle.
If `region-active-p', copy the region.
URL `http://xahlee.info/emacs/emacs/emacs_copy_cut_current_line.html'
Created: 2010-05-21
Version: 2024-06-19"
(interactive)
(cond
(current-prefix-arg (copy-region-as-kill (point-min) (point-max)))
((and (boundp 'rectangle-mark-mode) rectangle-mark-mode)
(copy-region-as-kill (region-beginning) (region-end) t))
((region-active-p) (copy-region-as-kill (region-beginning) (region-end)))
((eq last-command this-command)
(if (eobp)
nil
(progn
(kill-append "\n" nil)
(kill-append (buffer-substring (line-beginning-position) (line-end-position)) nil)
(end-of-line)
(forward-char))))
((eobp)
(if (eq (char-before) 10)
(progn)
(progn
(copy-region-as-kill (line-beginning-position) (line-end-position))
(end-of-line))))
(t
(copy-region-as-kill (line-beginning-position) (line-end-position))
(end-of-line)
(forward-char))))
(defun xah-cut-line-or-region ()
"Cut current line or selection.
If `universal-argument' is called first, cut whole buffer (respects `narrow-to-region').
URL `http://xahlee.info/emacs/emacs/emacs_copy_cut_current_line.html'
Created: 2010-05-21
Version: 2015-06-10"
(interactive)
(if current-prefix-arg
(progn ; not using kill-region because we don't want to include previous kill
(kill-new (buffer-string))
(delete-region (point-min) (point-max)))
(progn (if (region-active-p)
(kill-region (region-beginning) (region-end) t)
(kill-region (line-beginning-position) (line-beginning-position 2))))))
(defun xah-copy-all-or-region ()
"Copy buffer or selection content to `kill-ring'.
Respects `narrow-to-region'.
URL `http://xahlee.info/emacs/emacs/emacs_copy_cut_all_or_region.html'
Version: 2015-08-22"
(interactive)
(if (region-active-p)
(progn
(kill-new (buffer-substring (region-beginning) (region-end)))
(message "Text selection copied."))
(progn
(kill-new (buffer-string))
(message "Buffer content copied."))))
(defun xah-cut-all-or-region ()
"Cut buffer or selection content to `kill-ring'.
Respects `narrow-to-region'.
URL `http://xahlee.info/emacs/emacs/emacs_copy_cut_all_or_region.html'
Version: 2015-08-22"
(interactive)
(if (region-active-p)
(progn
(kill-new (buffer-substring (region-beginning) (region-end)))
(delete-region (region-beginning) (region-end)))
(progn
(kill-new (buffer-string))
(delete-region (point-min) (point-max)))))
(defun xah-copy-all ()
"Put the whole buffer content into the `kill-ring'.
(respects `narrow-to-region')
Version: 2016-10-06"
(interactive)
(kill-new (buffer-string))
(message "Buffer content copied."))
(defun xah-cut-all ()
"Cut the whole buffer content into the `kill-ring'.
Respects `narrow-to-region'.
Version: 2017-01-03"
(interactive)
(kill-new (buffer-string))
(delete-region (point-min) (point-max)))
(defun xah-paste-or-paste-previous ()
"Paste. When called repeatedly, paste previous.
This command calls `yank', and if repeated, call `yank-pop'.
If `universal-argument' is called first with a number arg, paste that many times.
URL `http://xahlee.info/emacs/emacs/emacs_paste_or_paste_previous.html'
Created: 2017-07-25
Version: 2020-09-08"
(interactive)
(progn
(when (and delete-selection-mode (region-active-p))
(delete-region (region-beginning) (region-end)))
(if current-prefix-arg
(progn
(dotimes (_ (prefix-numeric-value current-prefix-arg))
(yank)))
(if (eq real-last-command this-command)
(yank-pop 1)
(yank)))))
(defun xah-show-kill-ring ()
"Insert all `kill-ring' content in a new buffer named *copy history*.
URL `http://xahlee.info/emacs/emacs/emacs_show_kill_ring.html'
Created: 2019-12-02
Version: 2024-05-07"
(interactive)
(let ((xbuf (generate-new-buffer "*copy history*"))
(inhibit-read-only t))
(progn
(switch-to-buffer xbuf)
(funcall 'fundamental-mode)
(mapc
(lambda (x)
(insert x "\n\nsss97707------------------------------------------------\n\n" ))
kill-ring))
(goto-char (point-min))))
(defun xah-move-block-up ()
"Swap the current text block with the previous.
After this command is called, press <up> or <down> to move. Any other key to exit.
Version: 2022-03-04"
(interactive)
(let ((xp0 (point))
xc1 ; current block begin
xc2 ; current Block End
xbeg ; prev Block Begin
xend ; prev Block end
)
(if (re-search-forward "\n[ \t]*\n+" nil "move")
(setq xc2 (match-beginning 0))
(setq xc2 (point)))
(goto-char xp0)
(if (re-search-backward "\n[ \t]*\n+" nil "move")
(progn
(skip-chars-backward "\n \t")
(setq xend (point))
(skip-chars-forward "\n \t")
(setq xc1 (point)))
(error "No previous block."))
(goto-char xend)
(if (re-search-backward "\n[ \t]*\n+" nil "move")
(progn
(setq xbeg (match-end 0)))
(setq xbeg (point)))
(transpose-regions xbeg xend xc1 xc2)
(goto-char xbeg)
(set-transient-map
(let ((xkmap (make-sparse-keymap)))
(define-key xkmap (kbd "<up>") #'xah-move-block-up)
(define-key xkmap (kbd "<down>") #'xah-move-block-down)
xkmap))))
(defun xah-move-block-down ()
"Swap the current text block with the next.
After this command is called, press <up> or <down> to move. Any other key to exit.
Version: 2022-03-04"
(interactive)
(let ((xp0 (point))
xc1 ; current block begin
xc2 ; current Block End
xn1 ; next Block Begin
xn2 ; next Block end
)
(if (eq (point-min) (point))
(setq xc1 (point))
(if (re-search-backward "\n\n+" nil "move")
(progn
(setq xc1 (match-end 0)))
(setq xc1 (point))))
(goto-char xp0)
(if (re-search-forward "\n[ \t]*\n+" nil "move")
(progn
(setq xc2 (match-beginning 0))
(setq xn1 (match-end 0)))
(error "No next block."))
(if (re-search-forward "\n[ \t]*\n+" nil "move")
(progn
(setq xn2 (match-beginning 0)))
(setq xn2 (point)))
(transpose-regions xc1 xc2 xn1 xn2)
(goto-char xn2))
(set-transient-map
(let ((xkmap (make-sparse-keymap)))
(define-key xkmap (kbd "<up>") #'xah-move-block-up)
(define-key xkmap (kbd "<down>") #'xah-move-block-down)
xkmap)))
(defun xah-shrink-whitespaces ()
"Remove whitespaces around cursor .
Shrink neighboring spaces, then newlines, then spaces again, leaving one space or newline at each step, till no more white space.
URL `http://xahlee.info/emacs/emacs/emacs_shrink_whitespace.html'
Created: 2014-10-21
Version: 2023-07-12"
(interactive)
(let ((xeol-count 0)
(xp0 (point))
xbeg ; whitespace begin
xend ; whitespace end
(xcharBefore (char-before))
(xcharAfter (char-after))
xspace-neighbor-p)
(setq xspace-neighbor-p (or (eq xcharBefore 32) (eq xcharBefore 9) (eq xcharAfter 32) (eq xcharAfter 9)))
(skip-chars-backward " \n\t ")
(setq xbeg (point))
(goto-char xp0)
(skip-chars-forward " \n\t ")
(setq xend (point))
(goto-char xbeg)
(while (search-forward "\n" xend t)
(setq xeol-count (1+ xeol-count)))
(goto-char xp0)
(cond
((eq xeol-count 0)
(if (> (- xend xbeg) 1)
(progn
(delete-horizontal-space) (insert " "))
(progn (delete-horizontal-space))))
((eq xeol-count 1)
(if xspace-neighbor-p
(delete-horizontal-space)
(progn (delete-space--internal "\n" nil) (insert " "))))
((eq xeol-count 2)
(if xspace-neighbor-p
(delete-horizontal-space)
(progn
(delete-space--internal "\n" nil)
(insert "\n"))))
((> xeol-count 2)
(if xspace-neighbor-p
(delete-horizontal-space)
(progn
(goto-char xend)
(search-backward "\n")
(delete-region xbeg (point))
(insert "\n"))))
(t (progn
(message "nothing done. logic error 40873. shouldn't reach here"))))))
(defun xah-delete-string-backward (&optional DeleteJustQuote)
"Delete string to the left of cursor.
Cursor must be on the right of a string delimiter.
e.g. \"▮some\" or \"some\"▮
Else, do nothing.
String delimiter is determined by current syntax table. (see `describe-syntax')
If DeleteJustQuote is true, delete only the quotation marks.
Created: 2023-11-12
Version: 2024-06-06"
(when (prog2 (backward-char) (looking-at "\\s\"") (forward-char))
(let ((xp0 (point)) xbeg xend)
;; xbeg xend are the begin and end pos of the string
(if (nth 3 (syntax-ppss))
(setq xbeg (1- xp0)
xend
(progn
(backward-char)
(forward-sexp)
(point)))
(setq xend (point)
xbeg
(progn (forward-sexp -1) (point))))
(if DeleteJustQuote
(progn (goto-char xend)
(delete-char -1)
(goto-char xbeg)
(delete-char 1))
(if (eq real-this-command real-last-command)
(kill-append (delete-and-extract-region xbeg xend) t)
(kill-region xbeg xend))))))
(defvar xah-smart-delete-dispatch
nil
"Used by `xah-smart-delete'.
This makes that function behavior dependent on current major-mode.
Value is Alist of pairs, each is of the form
(‹major-mode-name› . ‹function-name›)
If ‹major-mode-name› match current var `major-mode', the paired function is called.
If no major mode matches, `xah-smart-delete' default behavior is used.
Version: 2024-06-05")
(setq xah-smart-delete-dispatch
'((xah-wolfram-mode . xah-wolfram-smart-delete-backward)
(xah-html-mode . xah-html-smart-delete-backward)))
(defun xah-smart-delete (&optional BracketOnly SkipDispatch)
"Smart backward delete.
Typically, delete to the left 1 char or entire bracketed text.
Behavior depends on what's left char, and current `major-mode'.
If `xah-smart-delete-dispatch' match, call the matched function instead.
If region active, delete region.
If cursor left is space tab newline, delete them.
If cursor left is bracket, delete the whole bracket block.
If cursor left is string quote, delete the string.
Else just delete one char to the left.
If `universal-argument' is called first, do not delete bracket's innertext.
In elisp code, arg BracketOnly if true, do not delete innertext. SkipDispatch if true, skip checking `xah-smart-delete-dispatch'.
Created: 2023-07-22
Version: 2024-06-05"
(interactive (list current-prefix-arg nil))
(let (xfun)
(cond
((and (not SkipDispatch) (setq xfun (assq major-mode xah-smart-delete-dispatch)))
(message "calling cdr of %s" xfun)
(funcall (cdr xfun)))
((region-active-p) (delete-region (region-beginning) (region-end)))
((or
;; 32 is space, 9 is tab, 10 is newline
(eq (char-before) 32)
(eq (char-before) 10)
(eq (char-before) 9))
(let ((xp0 (point)) xbeg xend)
(skip-chars-backward " \t\n")
(setq xbeg (point) xend xp0)
(if (eq real-this-command real-last-command)
(kill-append (delete-and-extract-region xbeg xend) t)
(kill-region xbeg xend))))
((prog2 (backward-char) (looking-at "\\s)") (forward-char))
;; (message "cursor left is closing bracket")
(cond
;; unmatched bracket, just delete it
((not (condition-case nil (scan-sexps (point) -1) (scan-error nil)))
(warn "There was unmatched bracket: no paired opening bracket on left of cursor")
(delete-char -1))
;; delete just the brackets
(BracketOnly
(let ((xp0 (point)) xbeg)
(forward-sexp -1)
(while (looking-at "\\s'") (forward-char))
(setq xbeg (point))
(goto-char xp0)
(delete-char -1)
(goto-char xbeg)
(delete-char 1)
(goto-char (- xp0 2))))
;; delete the bracket block
(t
(let ((xp0 (point)) xbeg xend)
(forward-sexp -1)
(while (looking-at "\\s'") (forward-char))
(setq xbeg (point) xend xp0)
(if (eq real-this-command real-last-command)
(kill-append (delete-and-extract-region xbeg xend) t)
(kill-region xbeg xend))))))
((prog2 (backward-char) (looking-at "\\s(") (forward-char))
;; (message "cursor left is opening bracket")
(cond
;; unmatched bracket, just delete it
((save-excursion
(backward-char)
(not (condition-case nil (scan-sexps (point) 1) (scan-error nil))))
(warn "There was unmatched bracket: no paired closing bracket on right of cursor")
(delete-char -1))
;; delete just the brackets
(BracketOnly
(let (xbeg)
(backward-char)
(setq xbeg (point))
(forward-sexp 1)
(delete-char -1)
(goto-char xbeg)
(delete-char 1)))
;; delete the bracket block
(t
(let (xbeg xend)
(backward-char)
(setq xbeg (point))
(forward-sexp 1)
(setq xend (point))
(if (eq real-this-command real-last-command)
(kill-append (delete-and-extract-region xbeg xend) t)
(kill-region xbeg xend))))))
((prog2 (backward-char) (looking-at "\\s\"") (forward-char))
(message "calling xah-delete-string-backward")
(xah-delete-string-backward BracketOnly))
(t (delete-char -1)))))
(defun xah-change-bracket-pairs (FromChars ToChars)
"Change bracket pairs to another type or none.
For example, change all parenthesis () to square brackets [].
Works on current block or selection.
In lisp code, FromChars is a string with at least 2 spaces.
e.g.
paren ( )
french angle ‹ ›
double bracket [[ ]]
etc.
It is split by space, and last 2 items are taken as left and right brackets.
ToChars is similar, with a special value of
none
followed by 2 spaces.
,it means replace by empty string.
URL `http://xahlee.info/emacs/emacs/elisp_change_brackets.html'
Created: 2020-11-01
Version: 2024-08-07"
(interactive
(let ((xbrackets
'(
"square [ ]"
"brace { }"
"paren ( )"
"greater < >"
"double quote \" \""
"single quote ' '"
"emacs ` '"
"markdown grave accent ` `"
"double square [[ ]]"
"tilde ~ ~"
"equal = ="
"double curly quote “ ”"
"single curly quote ‘ ’"
"french angle ‹ ›"
"french double angle « »"
"corner 「 」"
"white corner 『 』"
"lenticular 【 】"
"white lenticular 〖 〗"
"title angle 〈 〉"
"double angle 《 》"
"tortoise 〔 〕"
"white tortoise 〘 〙"
"white square 〚 〛"
"white paren ⦅ ⦆"
"white curly bracket ⦃ ⦄"
"pointing angle 〈 〉"
"angle with dot ⦑ ⦒"
"curved angle ⧼ ⧽"
"math square ⟦ ⟧"
"math angle ⟨ ⟩"
"math double angle ⟪ ⟫"
"math flattened parenthesis ⟮ ⟯"
"math white tortoise shell ⟬ ⟭"
"heavy single quotation mark ornament ❛ ❜"
"heavy double turned comma quotation mark ornament ❝ ❞"
"medium parenthesis ornament ❨ ❩"
"medium flattened parenthesis ornament ❪ ❫"
"medium curly ornament ❴ ❵"
"medium pointing angle ornament ❬ ❭"
"heavy pointing angle quotation mark ornament ❮ ❯"
"heavy pointing angle ornament ❰ ❱"
"none "
)))
(let ((completion-ignore-case t))
(list
(completing-read "Replace this:" xbrackets nil t nil nil (car xbrackets))
(completing-read "To:" xbrackets nil t nil nil (car (last xbrackets)))))))
(let (xbeg xend xleft xright xtoL xtoR)
(seq-setq (xbeg xend) (xah-fly-get-pos-block-or))
(let ((xsFrom (last (split-string FromChars " ") 2))
(xsTo (last (split-string ToChars " ") 2)))
;; (when (< (length xsFrom) 3)
;; (error "cannot find input brackets %s" xsFrom))
;; (when (< (length xsTo) 3)
;; (message "replace blacket is empty string")
;; (setq xsTo (list "" "" "")))
(setq xleft (car xsFrom) xright (car (cdr xsFrom))
xtoL (car xsTo) xtoR (car (cdr xsTo)))
(save-excursion
(save-restriction
(narrow-to-region xbeg xend)
(let ((case-fold-search nil))
(if (string-equal xleft xright)
(let ((xx (regexp-quote xleft)))
(goto-char (point-min))
(while
(re-search-forward
(format "%s\\([^%s]+?\\)%s" xx xx xx)
nil t)
(overlay-put (make-overlay (match-beginning 0) (match-end 0)) 'face 'highlight)
(replace-match (concat xtoL "\\1" xtoR) t)))
(progn
(progn
(goto-char (point-min))
(while (search-forward xleft nil t)
(overlay-put (make-overlay (match-beginning 0) (match-end 0)) 'face 'highlight)
(replace-match xtoL t t)))
(progn
(goto-char (point-min))
(while (search-forward xright nil t)
(overlay-put (make-overlay (match-beginning 0) (match-end 0)) 'face 'highlight)
(replace-match xtoR t t)))))))))))
(defun xah-toggle-letter-case ()
"Toggle the letter case of current word or selection.
Always cycle in this order: Init Caps, ALL CAPS, all lower.
URL `http://xahlee.info/emacs/emacs/emacs_toggle_letter_case.html'
Created: 2020-06-26
Version: 2024-06-17"
(interactive)
(let ((deactivate-mark nil) xbeg xend)
(if (region-active-p)
(setq xbeg (region-beginning) xend (region-end))
(save-excursion
(skip-chars-backward "[:alnum:]")
(setq xbeg (point))
(skip-chars-forward "[:alnum:]")
(setq xend (point))))
(when (not (eq last-command this-command))
(put this-command 'state 0))
(cond
((equal 0 (get this-command 'state))
(upcase-initials-region xbeg xend)
(put this-command 'state 1))
((equal 1 (get this-command 'state))
(upcase-region xbeg xend)
(put this-command 'state 2))
((equal 2 (get this-command 'state))
(downcase-region xbeg xend)
(put this-command 'state 0)))))
;; test case
;; test_case some
;; test-case
;; tes▮t-case
(defun xah-toggle-previous-letter-case ()
"Toggle the letter case of the letter to the left of cursor.
URL `http://xahlee.info/emacs/emacs/emacs_toggle_letter_case.html'
Created: 2015-12-22
Version: 2023-11-14"
(interactive)
(let ((case-fold-search nil))
(left-char 1)
(cond
((looking-at "[[:lower:]]") (upcase-region (point) (1+ (point))))
((looking-at "[[:upper:]]") (downcase-region (point) (1+ (point)))))
(right-char)))
(defun xah-upcase-sentence ()
"Upcase first letters of sentences of current block or selection.
URL `http://xahlee.info/emacs/emacs/emacs_upcase_sentence.html'
Created: 2020-12-08
Version: 2024-03-19"
(interactive)
(let (xbeg xend)
(seq-setq (xbeg xend) (xah-fly-get-pos-block-or))
(save-restriction
(narrow-to-region xbeg xend)
(let ((case-fold-search nil))
;; after period or question mark or exclamation
(goto-char (point-min))
(while (re-search-forward "\\(\\.\\|\\?\\|!\\)[ \n]+ *\\([a-z]\\)" nil :move)
(upcase-region (match-beginning 2) (match-end 2))
(overlay-put (make-overlay (match-beginning 2) (match-end 2)) 'face 'highlight))
;; after a blank line, after a bullet, or beginning of buffer
(goto-char (point-min))
(while (re-search-forward "\\(\\`\\|• \\|\n\n\\)\\([a-z]\\)" nil :move)
(upcase-region (match-beginning 2) (match-end 2))
(overlay-put (make-overlay (match-beginning 2) (match-end 2)) 'face 'highlight))
;; for HTML. first letter after tag
(when
(or
(eq major-mode 'xah-html-mode)
(eq major-mode 'html-mode)
(eq major-mode 'sgml-mode)
(eq major-mode 'nxml-mode)
(eq major-mode 'xml-mode)
(eq major-mode 'mhtml-mode))
(goto-char (point-min))
(while
(re-search-forward "\\(<title>[ \n]?\\|<h[1-6]>[ \n]?\\|<p>[ \n]?\\|<li>[ \n]?\\|<dd>[ \n]?\\|<td>[ \n]?\\|<br ?/?>[ \n]?\\|<figcaption>[ \n]?\\)\\([a-z]\\)" nil :move)
(upcase-region (match-beginning 2) (match-end 2))
(overlay-put (make-overlay (match-beginning 2) (match-end 2)) 'face 'highlight))))
(goto-char (point-max)))
(skip-chars-forward " \n\t")))
(defun xah-title-case-region-or-line (&optional Begin End)
"Title case text between nearest brackets, or current line or selection.
Capitalize first letter of each word, except words like {to, of, the, a, in, or, and}. If a word already contains cap letters such as HTTP, URL, they are left as is.
When called in a elisp program, Begin End are region boundaries.
URL `http://xahlee.info/emacs/emacs/elisp_title_case_text.html'
Created: 2017-01-11
Version: 2021-09-19"