forked from xahlee/xah-fly-keys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxah-fly-keys.el
4176 lines (3731 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; byte-compile-dynamic: t; -*-
;; Copyright © 2013-2020, by Xah Lee
;; Author: Xah Lee ( http://xahlee.info/ )
;; Version: 11.5.20200625041453
;; Created: 10 Sep 2013
;; Package-Requires: ((emacs "24.1"))
;; Keywords: convenience, emulations, vim, ergoemacs
;; License: GPL v3
;; Homepage: http://ergoemacs.org/misc/ergoemacs_vi_mode.html
;; This file is not part of GNU Emacs.
;;; Commentary:
;; xah-fly-keys is a efficient keybinding for emacs. (more efficient than vim)
;; It is a modal mode like vi, but key choices are based on statistics of command call frequency.
;; --------------------------------------------------
;; 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) ; required
;; possible layout values:
;; 'azerty
;; 'azerty-be
;; 'colemak
;; 'colemak-mod-dh
;; 'dvorak
;; 'programer-dvorak
;; 'qwerty
;; 'qwerty-abnt
;; 'qwerty-no (qwerty Norwegian)
;; 'qwertz
;; 'workman
;; 'norman
;; For backward compatibility, strings with the above names are also accepted.
;; (xah-fly-keys 1)
;; --------------------------------------------------
;; HOW TO USE
;; M-x xah-fly-keys to toggle the mode on/off.
;; Important command/insert mode switch keys:
;; xah-fly-command-mode-activate (press 【<home>】 or 【F8】 or 【Alt+Space】 or 【menu】)
;; xah-fly-insert-mode-activate (when in command mode, press qwerty letter key f. (Dvorak key u))
;; When in command mode:
;; 【f】 (or Dvorak 【u】) activates insertion mode.
;; 【Space】 is a leader key. For example, 【SPACE r】 (Dvorak 【SPACE p】) calls query-replace. Press 【SPACE C-h】 to see the full list.
;; 【Space Space】 also activates insertion mode.
;; 【Space Enter】 calls execute-extended-command.
;; 【a】 calls execute-extended-command or smex or helm or counsel (if they are installed).
;; The leader key sequence basically replace 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 Ctrl+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】 switch-to-buffer is 【SPACE f】 (Dvorak 【SPACE u】)
;; 【C-x C-f】 find-file is 【SPACE i e】 (Dvorak 【SPACE c .】)
;; 【C-x n n】 narrow-to-region is 【SPACE l l】 (Dvorak 【SPACE n n】)
;; The first key we call it leader key. In the above examples, the SPACE is the leader key.
;; When in command mode, the 【SPACE】 is a leader key.
;; globally, the leader key is the 【f9】 key. 【f9】 is leader key regardless it's in command mode or insert mode.
;; the following standard keys with Control are supported:
;; 【Ctrl+tab】 'xah-next-user-buffer
;; 【Ctrl+shift+tab】 'xah-previous-user-buffer
;; 【Ctrl+v】 paste
;; 【Ctrl+w】 close
;; 【Ctrl+z】 undo
;; 【Ctrl+n】 new
;; 【Ctrl+o】 open
;; 【Ctrl+s】 save
;; 【Ctrl+shift+s】 save as
;; 【Ctrl+shift+t】 open last closed
;; 【Ctrl++】 'text-scale-increase
;; 【Ctrl+-】 'text-scale-decrease
;; To disable both Control and Meta shortcut keys, add the following lines to you init.el before (require 'xah-fly-keys):
;; (setq xah-fly-use-control-key nil)
;; (setq xah-fly-use-meta-key nil)
;; I highly recommend setting 【capslock】 to send 【Home】. So that it acts as `xah-fly-command-mode-activate'.
;; see
;; How to Make the CapsLock Key do Home Key
;; http://ergoemacs.org/misc/capslock_do_home_key.html
;; If you have a bug, post on github.
;; For detail about design and other info, see home page at
;; http://ergoemacs.org/misc/ergoemacs_vi_mode.html
;; If you like this project, Buy Xah Emacs Tutorial http://ergoemacs.org/emacs/buy_xah_emacs_tutorial.html or make a donation. Thanks.
;;; Code:
(require 'dired) ; in emacs
(require 'dired-x) ; in emacs
(require 'ido) ; in emacs
(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'")
(defcustom xah-fly-use-control-key t
"If nil, do not bind any control key. When t, standard keys for open, close, paste, are bound."
:type 'boolean
:group 'xah-fly-keys)
(defcustom xah-fly-use-meta-key t
"If nil, do not bind any meta key."
:type 'boolean
:group 'xah-fly-keys)
(defcustom xah-fly-use-isearch-arrows t
"If nil, do not bind the arrow keys to move between matches in Isearch."
:type 'boolean
:group 'xah-fly-keys)
;; cursor movement
(defun xah-pop-local-mark-ring ()
"Move cursor to last mark position of current buffer.
Call this repeatedly will cycle all positions in `mark-ring'.
URL `http://ergoemacs.org/emacs/emacs_jump_to_previous_position.html'
Version 2016-04-04"
(interactive)
(set-mark-command t))
(defun xah-beginning-of-line-or-block ()
"Move cursor to beginning of line or previous paragraph.
• When called first time, move cursor to beginning of char in current line. (if already, move to beginning of line.)
• When called again, move cursor backward by jumping over any sequence of whitespaces containing 2 blank lines.
URL `http://ergoemacs.org/emacs/emacs_keybinding_design_beginning-of-line-or-block.html'
Version 2018-06-04"
(interactive)
(let (($p (point)))
(if (or (equal (point) (line-beginning-position))
(eq last-command this-command))
(if (re-search-backward "\n[\t\n ]*\n+" nil "move")
(progn
(skip-chars-backward "\n\t ")
;; (forward-char )
)
(goto-char (point-min)))
(progn
(back-to-indentation)
(when (eq $p (point))
(beginning-of-line))))))
(defun xah-end-of-line-or-block ()
"Move cursor to end of line or next paragraph.
• 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.
URL `http://ergoemacs.org/emacs/emacs_keybinding_design_beginning-of-line-or-block.html'
Version 2018-06-04"
(interactive)
(if (or (equal (point) (line-end-position))
(eq last-command this-command))
(progn
(re-search-forward "\n[\t\n ]*\n+" nil "move" ))
(end-of-line)))
(defvar xah-brackets nil "string of left/right brackets pairs.")
(setq xah-brackets "()[]{}<><>()[]{}⦅⦆〚〛⦃⦄“”‘’‹›«»「」〈〉《》【】〔〕⦗⦘『』〖〗〘〙「」⟦⟧⟨⟩⟪⟫⟮⟯⟬⟭⌈⌉⌊⌋⦇⦈⦉⦊❛❜❝❞❨❩❪❫❴❵❬❭❮❯❰❱❲❳〈〉⦑⦒⧼⧽﹙﹚﹛﹜﹝﹞⁽⁾₍₎⦋⦌⦍⦎⦏⦐⁅⁆⸢⸣⸤⸥⟅⟆⦓⦔⦕⦖⸦⸧⸨⸩⦅⦆⧘⧙⧚⧛⸜⸝⸌⸍⸂⸃⸄⸅⸉⸊᚛᚜༺༻༼༽⏜⏝⎴⎵⏞⏟⏠⏡﹁﹂﹃﹄︹︺︻︼︗︘︿﹀︽︾﹇﹈︷︸")
(defvar xah-left-brackets '("(" "{" "[" "<" "〔" "【" "〖" "〈" "《" "「" "『" "“" "‘" "‹" "«" )
"List of left bracket chars.")
(progn
;; make xah-left-brackets based on xah-brackets
(setq xah-left-brackets '())
(dotimes ($x (- (length xah-brackets) 1))
(when (= (% $x 2) 0)
(push (char-to-string (elt xah-brackets $x))
xah-left-brackets)))
(setq xah-left-brackets (reverse xah-left-brackets)))
(defvar xah-right-brackets '(")" "]" "}" ">" "〕" "】" "〗" "〉" "》" "」" "』" "”" "’" "›" "»")
"list of right bracket chars.")
(progn
(setq xah-right-brackets '())
(dotimes ($x (- (length xah-brackets) 1))
(when (= (% $x 2) 1)
(push (char-to-string (elt xah-brackets $x))
xah-right-brackets)))
(setq xah-right-brackets (reverse xah-right-brackets)))
(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 (&optional n)
"Move cursor to the next occurrence of punctuation.
The list of punctuations to jump to is defined by `xah-punctuation-regex'
URL `http://ergoemacs.org/emacs/emacs_jump_to_punctuations.html'
Version 2017-06-26"
(interactive "p")
(re-search-forward xah-punctuation-regex nil t n))
(defun xah-backward-punct (&optional n)
"Move cursor to the previous occurrence of punctuation.
See `xah-forward-punct'
URL `http://ergoemacs.org/emacs/emacs_jump_to_punctuations.html'
Version 2017-06-26"
(interactive "p")
(re-search-backward xah-punctuation-regex nil t n))
(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://ergoemacs.org/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://ergoemacs.org/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://ergoemacs.org/emacs/emacs_navigating_keys_for_brackets.html'
Version 2016-11-22"
(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))
((looking-back (regexp-opt xah-right-brackets) (max (- (point) 1) 1))
(backward-sexp))
(t (backward-up-list 1 'ESCAPE-STRINGS 'NO-SYNTAX-CROSSING)))))
(defun xah-forward-equal-quote ()
"Move cursor to the next occurrence of 「='」 or 「=\"」, with or without space.
URL `http://ergoemacs.org/emacs/emacs_navigating_keys_for_brackets.html'
Version 2015-05-05"
(interactive)
(re-search-forward "=[ \n]*\\('+\\|\\\"+\\)" nil t))
(defun xah-forward-equal-sign ()
"Move cursor to the next occurrence of equal sign 「=」.
URL `http://ergoemacs.org/emacs/emacs_jump_to_punctuations.html'
Version 2015-06-15"
(interactive)
(re-search-forward "=+" nil t))
(defun xah-backward-equal-sign ()
"Move cursor to previous occurrence of equal sign 「=」.
URL `http://ergoemacs.org/emacs/emacs_jump_to_punctuations.html'
Version 2015-06-15"
(interactive)
(when (re-search-backward "=+" nil t)
(while (search-backward "=" (- (point) 1) t)
(left-char))))
(defun xah-forward-comma-sign ()
"Move cursor to the next occurrence of comma 「,」.
Version 2016-01-19"
(interactive)
(re-search-forward ",+" nil t))
(defun xah-backward-comma-sign ()
"Move cursor to previous occurrence of comma sign 「,」.
Version 2016-01-19"
(interactive)
(when (re-search-backward ",+" nil t)
(while (search-backward "," (- (point) 1) t)
(left-char))))
(defun xah-forward-quote ()
"Move cursor to the next occurrence of \".
If there are consecutive quotes of the same char, keep moving until none.
Returns `t' if found, else `nil'.
URL `http://ergoemacs.org/emacs/emacs_navigating_keys_for_brackets.html'
Version 2016-07-23"
(interactive)
(if (re-search-forward "\\\"+" nil t)
t
(progn
(message "No more quotes after cursor..")
nil)))
(defun xah-forward-quote-twice ()
"Call `xah-forward-quote' twice.
Returns `t' if found, else `nil'.
URL `http://ergoemacs.org/emacs/emacs_navigating_keys_for_brackets.html'
Version 2016-07-23"
(interactive)
(when (xah-forward-quote)
(xah-forward-quote)))
(defun xah-forward-quote-smart ()
"Move cursor to the current or next string quote.
Place cursor at the position after the left quote.
Repeated call will find the next string.
URL `http://ergoemacs.org/emacs/emacs_navigating_keys_for_brackets.html'
Version 2016-11-22"
(interactive)
(let (($pos (point)))
(if (nth 3 (syntax-ppss))
(progn
(backward-up-list 1 'ESCAPE-STRINGS 'NO-SYNTAX-CROSSING)
(forward-sexp)
(re-search-forward "\\\"" nil t))
(progn (re-search-forward "\\\"" nil t)))
(when (<= (point) $pos)
(progn (re-search-forward "\\\"" nil t)))))
(defun xah-backward-quote ()
"Move cursor to the previous occurrence of \".
If there are consecutive quotes of the same char, keep moving until none.
Returns `t' if found, else `nil'.
URL `http://ergoemacs.org/emacs/emacs_navigating_keys_for_brackets.html'
Version 2016-07-23"
(interactive)
(if (re-search-backward "\\\"+" nil t)
(when (char-before) ; isn't nil, at beginning of buffer
(while (char-equal (char-before) (char-after))
(left-char)
t))
(progn
(message "No more quotes before cursor.")
nil)))
(defun xah-forward-dot-comma ()
"Move cursor to the next occurrence of 「.」 「,」 「;」.
URL `http://ergoemacs.org/emacs/emacs_jump_to_punctuations.html'
Version 2015-03-24"
(interactive)
(re-search-forward "\\.+\\|,+\\|;+" nil t))
(defun xah-backward-dot-comma ()
"Move cursor to the previous occurrence of 「.」 「,」 「;」
URL `http://ergoemacs.org/emacs/emacs_jump_to_punctuations.html'
Version 2015-03-24"
(interactive)
(re-search-backward "\\.+\\|,+\\|;+" nil t))
;; (defun goto-point-min ()
;; "Goto the beginning of buffer.
;; This is different from `beginning-of-buffer'
;; because that marks the previous position."
;; (interactive)
;; (goto-char (point-min))
;; )
;; (defun goto-point-max ()
;; "Goto the end of buffer.
;; This is different from `end-of-buffer'
;; because that marks the previous position."
;; (interactive)
;; (goto-char (point-max))
;; )
;; (defun xah-forward-space ()
;; "Move cursor to the next occurrence of white space."
;; (interactive)
;; (re-search-forward "[ \t\n]+" nil t))
;; (defun xah-backward-space ()
;; "Move cursor to the next occurrence of white space."
;; (interactive)
;; ;; (skip-chars-backward "^ \t\n")
;; ;; (re-search-backward "[ \t\n]+" nil t)
;; (posix-search-backward "[ \t\n]+" nil t)
;; )
;; editing commands
(defun xah-copy-line-or-region ()
"Copy current line, or text selection.
When called repeatedly, append copy subsequent lines.
When `universal-argument' is called first, copy whole buffer (respects `narrow-to-region').
URL `http://ergoemacs.org/emacs/emacs_copy_cut_current_line.html'
Version 2019-10-30"
(interactive)
(let ((inhibit-field-text-motion nil))
(if current-prefix-arg
(progn
(copy-region-as-kill (point-min) (point-max)))
(if (use-region-p)
(progn
(copy-region-as-kill (region-beginning) (region-end)))
(if (eq last-command this-command)
(if (eobp)
(progn )
(progn
(kill-append "\n" nil)
(kill-append
(buffer-substring-no-properties (line-beginning-position) (line-end-position))
nil)
(progn
(end-of-line)
(forward-char))))
(if (eobp)
(if (eq (char-before) 10 )
(progn )
(progn
(copy-region-as-kill (line-beginning-position) (line-end-position))
(end-of-line)))
(progn
(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 text selection.
When `universal-argument' is called first, cut whole buffer (respects `narrow-to-region').
URL `http://ergoemacs.org/emacs/emacs_copy_cut_current_line.html'
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 (use-region-p)
(kill-region (region-beginning) (region-end) t)
(kill-region (line-beginning-position) (line-beginning-position 2))))))
(defun xah-copy-all-or-region ()
"Put the whole buffer content to `kill-ring', or text selection if there's one.
Respects `narrow-to-region'.
URL `http://ergoemacs.org/emacs/emacs_copy_cut_all_or_region.html'
Version 2015-08-22"
(interactive)
(if (use-region-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 the whole buffer content to `kill-ring', or text selection if there's one.
Respects `narrow-to-region'.
URL `http://ergoemacs.org/emacs/emacs_copy_cut_all_or_region.html'
Version 2015-08-22"
(interactive)
(if (use-region-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'.
When `universal-argument' is called first with a number arg, paste that many times.
URL `http://ergoemacs.org/emacs/emacs_paste_or_paste_previous.html'
Version 2017-07-25"
(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://ergoemacs.org/emacs/emacs_show_kill_ring.html'
Version 2019-12-02"
(interactive)
(let (($buf (generate-new-buffer "*copy history*")))
(progn
(switch-to-buffer $buf)
(funcall 'fundamental-mode)
(dolist (x kill-ring )
(insert x "\n\nhh=============================================================================\n\n"))
(goto-char (point-min)))))
(defun xah-kill-word ()
"Like `kill-word', but delete selection first if there's one.
Version 2018-08-31"
(interactive)
(when (use-region-p)
(delete-region (region-beginning) (region-end)))
(kill-word 1))
(defun xah-backward-kill-word ()
"Like `backward-kill-word', but delete selection first if there's one.
Version 2018-08-31"
(interactive)
(when (use-region-p)
(delete-region (region-beginning) (region-end)))
(backward-kill-word 1))
(defun xah-delete-backward-char-or-bracket-text ()
"Delete backward 1 character, but if it's a \"quote\" or bracket ()[]{}【】「」 etc, delete bracket and the inner text, push the deleted text to `kill-ring'.
What char is considered bracket or quote is determined by current syntax table.
If `universal-argument' is called first, do not delete inner text.
URL `http://ergoemacs.org/emacs/emacs_delete_backward_char_or_bracket_text.html'
Version 2017-07-02"
(interactive)
(if (and delete-selection-mode (region-active-p))
(delete-region (region-beginning) (region-end))
(cond
((looking-back "\\s)" 1)
(if current-prefix-arg
(xah-delete-backward-bracket-pair)
(xah-delete-backward-bracket-text)))
((looking-back "\\s(" 1)
(progn
(backward-char)
(forward-sexp)
(if current-prefix-arg
(xah-delete-backward-bracket-pair)
(xah-delete-backward-bracket-text))))
((looking-back "\\s\"" 1)
(if (nth 3 (syntax-ppss))
(progn
(backward-char )
(xah-delete-forward-bracket-pairs (not current-prefix-arg)))
(if current-prefix-arg
(xah-delete-backward-bracket-pair)
(xah-delete-backward-bracket-text))))
(t
(delete-char -1)))))
(defun xah-delete-backward-bracket-text ()
"Delete the matching brackets/quotes to the left of cursor, including the inner text.
This command assumes the left of cursor is a right bracket, and there's a matching one before it.
What char is considered bracket or quote is determined by current syntax table.
URL `http://ergoemacs.org/emacs/emacs_delete_backward_char_or_bracket_text.html'
Version 2017-09-21"
(interactive)
(progn
(forward-sexp -1)
(mark-sexp)
(kill-region (region-beginning) (region-end))))
(defun xah-delete-backward-bracket-pair ()
"Delete the matching brackets/quotes to the left of cursor.
After the command, mark is set at the left matching bracket position, so you can `exchange-point-and-mark' to select it.
This command assumes the left of point is a right bracket, and there's a matching one before it.
What char is considered bracket or quote is determined by current syntax table.
URL `http://ergoemacs.org/emacs/emacs_delete_backward_char_or_bracket_text.html'
Version 2017-07-02"
(interactive)
(let (( $p0 (point)) $p1)
(forward-sexp -1)
(setq $p1 (point))
(goto-char $p0)
(delete-char -1)
(goto-char $p1)
(delete-char 1)
(push-mark (point) t)
(goto-char (- $p0 2))))
(defun xah-delete-forward-bracket-pairs ( &optional @delete-inner-text-p)
"Delete the matching brackets/quotes to the right of cursor.
If @delete-inner-text-p is true, also delete the inner text.
After the command, mark is set at the left matching bracket position, so you can `exchange-point-and-mark' to select it.
This command assumes the char to the right of point is a left bracket or quote, and have a matching one after.
What char is considered bracket or quote is determined by current syntax table.
URL `http://ergoemacs.org/emacs/emacs_delete_backward_char_or_bracket_text.html'
Version 2017-07-02"
(interactive)
(if @delete-inner-text-p
(progn
(mark-sexp)
(kill-region (region-beginning) (region-end)))
(let (($pt (point)))
(forward-sexp)
(delete-char -1)
(push-mark (point) t)
(goto-char $pt)
(delete-char 1))))
(defun xah-change-bracket-pairs ( @from-chars @to-chars)
"Change bracket pairs from one type to another.
For example, change all parenthesis () to square brackets [].
Works on selected text, or current text block.
When called in lisp program, @from-chars or @to-chars is a string of bracket pair. eg \"(paren)\", \"[bracket]\", etc.
The first and last characters are used. (the middle is for convenience in ido selection.)
If the string contains “,2”, then the first 2 chars and last 2 chars are used, for example \"[[bracket,2]]\".
If @to-chars is equal to string “none”, the brackets are deleted.
URL `http://ergoemacs.org/emacs/elisp_change_brackets.html'
Version 2019-02-12"
(interactive
(let (($bracketsList
'("(paren)"
"{brace}"
"[square]"
"<greater>"
"`emacs'"
"`markdown`"
"~tilde~"
"=equal="
"\"ascii quote\""
"[[double square,2]]"
"“curly quote”"
"‘single quote’"
"‹angle quote›"
"«double angle quote»"
"「corner」"
"『white corner』"
"【LENTICULAR】"
"〖white LENTICULAR〗"
"〈angle〉"
"《double angle》"
"〔TORTOISE〕"
"〘WHITE TORTOISE SHELL〙"
"⦅white paren⦆"
"〚white square〛"
"⦃white curly⦄"
"〈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 LEFT PARENTHESIS ORNAMENT❩"
"❪MEDIUM FLATTENED LEFT PARENTHESIS ORNAMENT❫"
"❴MEDIUM LEFT CURLY ORNAMENT❵"
"❬MEDIUM LEFT-POINTING ANGLE ORNAMENT❭"
"❮HEAVY LEFT-POINTING ANGLE QUOTATION MARK ORNAMENT❯"
"❰HEAVY LEFT-POINTING ANGLE ORNAMENT❱"
"none"
)))
(list
(ido-completing-read "Replace this:" $bracketsList )
(ido-completing-read "To:" $bracketsList ))))
(let ( $p1 $p2 )
(if (use-region-p)
(setq $p1 (region-beginning) $p2 (region-end))
(save-excursion
(if (re-search-backward "\n[ \t]*\n" nil "move")
(progn (re-search-forward "\n[ \t]*\n")
(setq $p1 (point)))
(setq $p1 (point)))
(if (re-search-forward "\n[ \t]*\n" nil "move")
(progn (re-search-backward "\n[ \t]*\n")
(setq $p2 (point)))
(setq $p2 (point)))))
(save-excursion
(save-restriction
(narrow-to-region $p1 $p2)
(let ( (case-fold-search nil)
$fromLeft
$fromRight
$toLeft
$toRight)
(cond
((string-match ",2" @from-chars )
(progn
(setq $fromLeft (substring @from-chars 0 2))
(setq $fromRight (substring @from-chars -2))))
(t
(progn
(setq $fromLeft (substring @from-chars 0 1))
(setq $fromRight (substring @from-chars -1)))))
(cond
((string-match ",2" @to-chars)
(progn
(setq $toLeft (substring @to-chars 0 2))
(setq $toRight (substring @to-chars -2))))
((string-match "none" @to-chars)
(progn
(setq $toLeft "")
(setq $toRight "")))
(t
(progn
(setq $toLeft (substring @to-chars 0 1))
(setq $toRight (substring @to-chars -1)))))
(cond
((string-match "markdown" @from-chars)
(progn
(goto-char (point-min))
(while
(re-search-forward "`\\([^`]+?\\)`" nil t)
(overlay-put (make-overlay (match-beginning 0) (match-end 0)) 'face 'highlight)
(replace-match (concat $toLeft "\\1" $toRight ) "FIXEDCASE" ))))
((string-match "tilde" @from-chars)
(progn
(goto-char (point-min))
(while
(re-search-forward "~\\([^~]+?\\)~" nil t)
(overlay-put (make-overlay (match-beginning 0) (match-end 0)) 'face 'highlight)
(replace-match (concat $toLeft "\\1" $toRight ) "FIXEDCASE" ))))
((string-match "ascii quote" @from-chars)
(progn
(goto-char (point-min))
(while
(re-search-forward "\"\\([^\"]+?\\)\"" nil t)
(overlay-put (make-overlay (match-beginning 0) (match-end 0)) 'face 'highlight)
(replace-match (concat $toLeft "\\1" $toRight ) "FIXEDCASE" ))))
((string-match "equal" @from-chars)
(progn
(goto-char (point-min))
(while
(re-search-forward "=\\([^=]+?\\)=" nil t)
(overlay-put (make-overlay (match-beginning 0) (match-end 0)) 'face 'highlight)
(replace-match (concat $toLeft "\\1" $toRight ) "FIXEDCASE" ))))
(t (progn
(progn
(goto-char (point-min))
(while (search-forward $fromLeft nil t)
(overlay-put (make-overlay (match-beginning 0) (match-end 0)) 'face 'highlight)
(replace-match $toLeft "FIXEDCASE" "LITERAL")))
(progn
(goto-char (point-min))
(while (search-forward $fromRight nil t)
(overlay-put (make-overlay (match-beginning 0) (match-end 0)) 'face 'highlight)
(replace-match $toRight "FIXEDCASE" "LITERAL")))))))))))
(defun xah-toggle-letter-case ()
"Toggle the letter case of current word or text selection.
Always cycle in this order: Init Caps, ALL CAPS, all lower.
URL `http://ergoemacs.org/emacs/modernization_upcase-word.html'
Version 2019-11-24"
(interactive)
(let (
(deactivate-mark nil)
$p1 $p2)
(if (use-region-p)
(setq $p1 (region-beginning) $p2 (region-end))
(save-excursion
(skip-chars-backward "0-9A-Za-z")
(setq $p1 (point))
(skip-chars-forward "0-9A-Za-z")
(setq $p2 (point))))
(when (not (eq last-command this-command))
(put this-command 'state 0))
(cond
((equal 0 (get this-command 'state))
(upcase-initials-region $p1 $p2)
(put this-command 'state 1))
((equal 1 (get this-command 'state))
(upcase-region $p1 $p2)
(put this-command 'state 2))
((equal 2 (get this-command 'state))
(downcase-region $p1 $p2)
(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://ergoemacs.org/emacs/modernization_upcase-word.html'
Version 2015-12-22"
(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 text block or selection.
URL `http://ergoemacs.org/emacs/emacs_upcase_sentence.html'
Version 2020-03-29"
(interactive)
(let ($p1 $p2)
(if (region-active-p)
(setq $p1 (region-beginning) $p2 (region-end))
(save-excursion
(if (re-search-backward "\n[ \t]*\n" nil "move")
(progn
(setq $p1 (point))
(re-search-forward "\n[ \t]*\n"))
(setq $p1 (point)))
(progn
(re-search-forward "\n[ \t]*\n" nil "move")
(setq $p2 (point)))))
(save-excursion
(save-restriction
(narrow-to-region $p1 $p2)
(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
(goto-char (point-min))
(while (re-search-forward "\\(<p>\n?\\|<li>\\|<td>\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-min)))))))
(defun xah-title-case-region-or-line (@begin @end)
"Title case text between nearest brackets, or current line, or text 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://ergoemacs.org/emacs/elisp_title_case_text.html'
Version 2017-01-11"
(interactive
(if (use-region-p)
(list (region-beginning) (region-end))
(let (
$p1
$p2
($skipChars "^\"<>(){}[]“”‘’‹›«»「」『』【】〖〗《》〈〉〔〕"))
(progn
(skip-chars-backward $skipChars (line-beginning-position))
(setq $p1 (point))
(skip-chars-forward $skipChars (line-end-position))
(setq $p2 (point)))
(list $p1 $p2))))
(let* (
($strPairs [
[" A " " a "]
[" And " " and "]
[" At " " at "]
[" As " " as "]
[" By " " by "]
[" Be " " be "]
[" Into " " into "]
[" In " " in "]
[" Is " " is "]
[" It " " it "]
[" For " " for "]
[" Of " " of "]
[" Or " " or "]
[" On " " on "]
[" Via " " via "]
[" The " " the "]
[" That " " that "]
[" To " " to "]
[" Vs " " vs "]
[" With " " with "]
[" From " " from "]
["'S " "'s "]
["'T " "'t "]
]))
(save-excursion
(save-restriction
(narrow-to-region @begin @end)
(upcase-initials-region (point-min) (point-max))
(let ((case-fold-search nil))
(mapc
(lambda ($x)
(goto-char (point-min))
(while
(search-forward (aref $x 0) nil t)
(replace-match (aref $x 1) "FIXEDCASE" "LITERAL")))
$strPairs))))))
(defun xah-delete-blank-lines ()
"Delete all newline around cursor.
URL `http://ergoemacs.org/emacs/emacs_shrink_whitespace.html'
Version 2018-04-02"
(interactive)
(let ($p3 $p4)
(skip-chars-backward "\n")
(setq $p3 (point))
(skip-chars-forward "\n")
(setq $p4 (point))
(delete-region $p3 $p4)))
(defun xah-fly-delete-spaces ()
"Delete space, tab, IDEOGRAPHIC SPACE (U+3000) around cursor.
Version 2019-06-13"
(interactive)
(let (p1 p2)
(skip-chars-forward " \t ")
(setq p2 (point))
(skip-chars-backward " \t ")
(setq p1 (point))
(delete-region p1 p2)))
(defun xah-shrink-whitespaces ()
"Remove whitespaces around cursor to just one, or none.
Shrink any neighboring space tab newline characters to 1 or none.
If cursor neighbor has space/tab, toggle between 1 or 0 space.
If cursor neighbor are newline, shrink them to just 1.
If already has just 1 whitespace, delete it.
URL `http://ergoemacs.org/emacs/emacs_shrink_whitespace.html'
Version 2019-06-13"
(interactive)
(let* (
($eol-count 0)
($p0 (point))
$p1 ; whitespace begin
$p2 ; whitespace end
($charBefore (char-before))
($charAfter (char-after ))
($space-neighbor-p (or (eq $charBefore 32) (eq $charBefore 9) (eq $charAfter 32) (eq $charAfter 9)))
$just-1-space-p
)
(skip-chars-backward " \n\t ")