-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathergoemacs-functions.el
2848 lines (2485 loc) · 106 KB
/
ergoemacs-functions.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
;;; ergoemacs-functions.el --- miscellaneous functions for ErgoEmacs -*- lexical-binding: t -*-
;; Copyright © 2013-2023 Free Software Foundation, Inc.
;; Maintainer: Matthew L. Fidler
;; Authors: Xah Lee, Matthew Fidler, Drew Adams, Ting-Yu Lin, David
;; Capello, Nikolaj Schumacher, Andy Stewart
;; Keywords: convenience
;; ErgoEmacs 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.
;; ErgoEmacs 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 ErgoEmacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Todo:
;;
;;; Code:
(require 'cl-lib)
(eval-when-compile
(require 'ergoemacs-macros))
(defvar ergoemacs-set-ignore-customize)
(defvar apropos-do-all)
(defvar cua--last-killed-rectangle)
(defvar dirtrack-list)
(defvar ergoemacs-dir)
(defvar ergoemacs-keyboard-layout)
(defvar ergoemacs-mode)
(defvar ergoemacs-single-command-keys)
(defvar helm-buffer)
(defvar helm-ff-default-directory)
(defvar helm-ff-last-expanded)
(defvar org-table-any-line-regexp)
(defvar server-kill-new-buffers)
(defvar server-existing-buffer)
(defvar explicit-shell-file-name)
(declare-function browse-url-default-windows-browser "browse-url")
(declare-function cua-copy-rectangle "cua-base")
(declare-function cua-copy-region "cua-base")
(declare-function cua-cut-rectangle-as-text "cua-rect")
(declare-function cua-cut-region "cua-base")
(declare-function cua-paste "cua-base")
(declare-function cua-set-rectangle-mark "cua-rect")
(declare-function dired-get-marked-files "dired")
(declare-function ergoemacs-mode "ergoemacs-mode")
(declare-function ergoemacs-map-properties--original "ergoemacs-map-properties")
(declare-function ergoemacs-theme-describe "ergoemacs-theme-engine")
(declare-function ergoemacs-key-description "ergoemacs-key-description")
(declare-function helm-attrset "helm")
(declare-function helm-basename "helm-utils")
(declare-function helm-execute-persistent-action "helm")
(declare-function helm-exit-minibuffer "helm")
(declare-function minibuffer-keyboard-quit "delsel")
(declare-function org-at-heading-p "org")
(declare-function org-at-item-p "org-list")
(declare-function org-at-table-p "org")
(declare-function org-edit-src-save "org-src")
(declare-function org-emphasize "org")
(declare-function org-insert-heading-respect-content "org")
(declare-function org-region-active-p "org-compat")
(declare-function org-with-limited-levels "org-macs")
(declare-function org-yank "org")
(declare-function server-edit "server")
(declare-function shell-dirtrack-mode "shell")
(declare-function undo-tree-mode "undo-tree")
(declare-function flyspell-auto-correct-previous-word "fylspell")
(declare-function w32-shell-execute "compat")
(declare-function w32-long-file-name "compat")
(declare-function term-paste "term")
(declare-function calc-yank "calc-yank")
(defvar ergoemacs-temporary-disable)
(defcustom ergoemacs-isearch-backward-char-to-edit nil
"Backward char will edit isearch."
:type 'boolean
:group 'ergoemacs-mode)
(defun ergoemacs--send-emacs-key (key &optional key2 key3)
"This replays the events from the intial key press.
KEY is the first key in the sequence.
KEY2 is the optional second key in the sequence.
KEY3 is the optional third key in the sequence."
;; Don't record this command
(setq ergoemacs--temporary-disable t
this-command last-command)
;; Restore the prefix arg
(ergoemacs-prefix-command-preserve-state)
;; Push the key back on the event queue
(when key3
(if (version< emacs-version "26.2")
(setq unread-command-events (cons key3
unread-command-events))
(setq unread-command-events (cons (cons 'no-record key3)
unread-command-events))))
(when key2
(if (version< emacs-version "26.2")
(setq unread-command-events (cons key2
unread-command-events))
(setq unread-command-events (cons (cons 'no-record key2)
unread-command-events))))
(if (version< emacs-version "26.2")
(setq unread-command-events (cons key
unread-command-events))
(setq unread-command-events (cons (cons 'no-record key)
unread-command-events))))
(defun ergoemacs-kill-line ()
"Ergoemacs replacement for `kill-line' using `ergoemacs--send-emacs-key'."
(interactive)
(ergoemacs--send-emacs-key ?\C-k))
(defun ergoemacs-mark-whole-buffer ()
"Ergoemacs replacement for `mark-whole-buffer' using `ergoemacs--send-emacs-key'."
(interactive)
(ergoemacs--send-emacs-key ?\C-x ?h))
(defun ergoemacs-find-file ()
"Ergoemacs replacement for `find-file' using `ergoemacs--send-emacs-key'."
(interactive)
(ergoemacs--send-emacs-key ?\C-x ?\C-f))
(defun ergoemacs-save-buffer ()
"Ergoemacs replacement for `save-buffer' using `ergoemacs--send-emacs-key'."
(interactive)
(ergoemacs--send-emacs-key ?\C-x ?\C-s))
(defun ergoemacs-write-file ()
"Ergoemacs replacement for `write-file' using `ergoemacs--send-emacs-key'."
(interactive)
(ergoemacs--send-emacs-key ?\C-x ?\C-w))
(defun ergoemacs-goto-line ()
"Ergoemacs replacement for `goto-line' using `ergoemacs--send-emacs-key'."
(interactive)
(ergoemacs--send-emacs-key ?\M-g ?\M-g))
(defun ergoemacs-move-beginning-of-line ()
"Ergoemacs replacement for `move-beginning-of-line' using `ergoemacs--send-emacs-key'."
(interactive)
(if this-command-keys-shift-translated
(ergoemacs--send-emacs-key ?\C-\S-a)
(ergoemacs--send-emacs-key ?\C-a)))
(defun ergoemacs-move-end-of-line ()
"Ergoemacs replacement for `move-end-of-line' using `ergoemacs--send-emacs-key'."
(interactive)
(if this-command-keys-shift-translated
(ergoemacs--send-emacs-key ?\C-\S-e)
(ergoemacs--send-emacs-key ?\C-e)))
(defun ergoemacs-set-mark-command ()
"Ergoemacs replacement for `set-mark-command' using `ergoemacs--send-emacs-key'."
(interactive)
(ergoemacs--send-emacs-key ?\C-\ ))
(defun ergoemacs-delete-backward-char ()
"Ergoemacs replacement for `delete-backward-char' using `ergoemacs--send-emacs-key'."
(interactive)
;; 127 = DEL
(ergoemacs--send-emacs-key 127))
(defun ergoemacs-delete-char ()
"Ergoemacs replacement for `delete-char' using `ergoemacs--send-emacs-key'."
(interactive)
(ergoemacs--send-emacs-key ?\C-d))
(defun ergoemacs-kill-word ()
"Ergoemacs replacement for `kill-word' using `ergoemacs--send-emacs-key'."
(interactive)
(ergoemacs--send-emacs-key ?\M-d))
(defun ergoemacs-backward-word ()
"Ergoemacs replacement for `backward-word' using `ergoemacs--send-emacs-key'."
(interactive "^")
(if this-command-keys-shift-translated
(ergoemacs--send-emacs-key ?\M-\S-b)
(ergoemacs--send-emacs-key ?\M-b)))
(defun ergoemacs-backward-kill-word ()
"Ergoemacs replacement for `backward-kill-word' using `ergoemacs--send-emacs-key'."
(interactive)
(ergoemacs--send-emacs-key 'C-backspace))
(defun ergoemacs-forward-word ()
"Ergoemacs replacement for `forward-word' using `ergoemacs--send-emacs-key'."
(interactive "^")
(if this-command-keys-shift-translated
(ergoemacs--send-emacs-key ?\M-\S-f)
(ergoemacs--send-emacs-key ?\M-f)))
(defun ergoemacs-backward-paragraph ()
"Ergoemacs replacement for `backward-paragraph' using `ergoemacs--send-emacs-key'."
(interactive "^")
(if this-command-keys-shift-translated
(ergoemacs--send-emacs-key 'C-S-up)
(ergoemacs--send-emacs-key ?\M-\{)))
(defun ergoemacs-forward-paragraph ()
"Ergoemacs replacement for `forward-paragraph' using `ergoemacs--send-emacs-key'."
(interactive "^")
(if this-command-keys-shift-translated
(ergoemacs--send-emacs-key 'C-S-down)
(ergoemacs--send-emacs-key ?\M-\})))
(defun ergoemacs-scroll-down-command ()
"Ergoemacs replacement for `scroll-down-command' using `ergoemacs--send-emacs-key'."
(interactive "^")
(if this-command-keys-shift-translated
(ergoemacs--send-emacs-key ?\M-\S-v)
(ergoemacs--send-emacs-key ?\M-v)))
(defun ergoemacs-scroll-up-command ()
"Ergoemacs replacement for `scroll-up-command' using `ergoemacs--send-emacs-key'."
(interactive "^")
(if this-command-keys-shift-translated
(ergoemacs--send-emacs-key ?\C-\S-v)
(ergoemacs--send-emacs-key ?\C-v)))
(defun ergoemacs-beginning-of-buffer ()
"Ergoemacs replacement for `beginning-of-buffer' using `ergoemacs--send-emacs-key'."
(interactive "^")
(if this-command-keys-shift-translated
(ergoemacs--send-emacs-key 'C-S-home)
(ergoemacs--send-emacs-key ?\M-\<)))
(defun ergoemacs-end-of-buffer ()
"Ergoemacs replacement for `end-of-buffer' using `ergoemacs--send-emacs-key'."
(interactive "^")
(if this-command-keys-shift-translated
(ergoemacs--send-emacs-key 'C-S-end)
(ergoemacs--send-emacs-key ?\M-\>)))
(defun ergoemacs-query-replace ()
"Ergoemacs replacement for `query-replace' using `ergoemacs--send-emacs-key'."
(interactive)
(ergoemacs--send-emacs-key ?\M-\%))
(defun ergoemacs-query-replace-regexp ()
"Ergoemacs replacement for `query-replace-regexp' using `ergoemacs--send-emacs-key'."
(interactive)
(ergoemacs--send-emacs-key ?\C-\M-\%))
(defun ergoemacs-other-window ()
"Ergoemacs replacement for `other-window' using `ergoemacs--send-emacs-key'."
(interactive)
(ergoemacs--send-emacs-key ?\C-x ?o))
(defun ergoemacs-delete-other-windows ()
"Ergoemacs replacement for `delete-other-windows' using `ergoemacs--send-emacs-key'."
(interactive)
(ergoemacs--send-emacs-key ?\C-x ?1))
(defun ergoemacs-delete-window ()
"Ergoemacs replacement for `delete-window' using `ergoemacs--send-emacs-key'."
(interactive)
(ergoemacs--send-emacs-key ?\C-x ?0))
(defun ergoemacs-split-window-below ()
"Ergoemacs replacement for `split-window-below' using `ergoemacs--send-emacs-key'."
(interactive)
(ergoemacs--send-emacs-key ?\C-x ?2))
(defun ergoemacs-split-window-right ()
"Ergoemacs replacement for `split-window-right' using `ergoemacs--send-emacs-key'."
(interactive)
(ergoemacs--send-emacs-key ?\C-x ?3))
(defun ergoemacs-switch-to-buffer ()
"Ergoemacs replacement for `switch-to-buffer' using `ergoemacs--send-emacs-key'."
(interactive)
(ergoemacs--send-emacs-key ?\C-x ?b))
(defun ergoemacs-shell-command ()
"Ergoemacs replacement for `shell-command' using `ergoemacs--send-emacs-key'."
(interactive)
(ergoemacs--send-emacs-key ?\M-\!))
(defun ergoemacs-recenter-top-bottom ()
"Ergoemacs replacement for `recenter-top-bottom' using `ergoemacs--send-emacs-key'."
(interactive)
(ergoemacs--send-emacs-key ?\C-l))
(defun ergoemacs-comment-dwim ()
"Ergoemacs replacement for `comment-dwim' using `ergoemacs--send-emacs-key'."
(interactive)
(ergoemacs--send-emacs-key ?\M-\;))
(defun ergoemacs-delete-horizontal-space ()
"Ergoemacs replacement for `delete-horizontal-space' using `ergoemacs--send-emacs-key'."
(interactive)
(ergoemacs--send-emacs-key ?\M-\\))
(defun ergoemacs-mark-paragraph ()
"Ergoemacs replacement for `mark-paragraph' using `ergoemacs--send-emacs-key'."
(interactive)
(ergoemacs--send-emacs-key ?\M-\S-\ ))
(defvar ergoemacs-delete-functions
'(delete-backward-char delete-char kill-word backward-kill-word)
"Defines deletion functions that ergoemacs is aware of.")
;;;###autoload
(defun ergoemacs-undo ()
"Run `undo'. Prefer running undo-fo if present"
(interactive)
(cond
((eq major-mode 'calc-mode)
(calc-undo 1))
((fboundp 'undo-fu-only-undo)
(call-interactively 'undo-fu-only-undo))
(t (undo))))
(defun ergoemacs-redo()
"Run `redo' when present."
(interactive)
(cond
((fboundp 'undo-fu-only-redo)
(call-interactively 'undo-fu-only-redo))
((fboundp 'undo-redo) ; should be in emacs 28
(call-interactively 'undo-redo))
(t (message "Redo support not present. Try `undo-fu'"))))
(defvar ergoemacs-revert-buffer 0)
(defun ergoemacs-revert-buffer ()
"Ergoemacs replacement of `revert-buffer'.
Does one of the following:
- When buffer is modified, call `revert-buffer' (or major/minor
mode replacement)
- When \"g\" is bound to a non-self-insert function, call that
function.
- When buffer is unmodified, revert to the last backup.
The backup is determined by `find-backup-file-name'"
(interactive)
(let ((backup-buffer (and (or (eq last-command 'ergoemacs-revert-buffer)
(not (buffer-modified-p (current-buffer))))
(buffer-file-name)
(find-backup-file-name (buffer-file-name))))
(opt (point))
bind)
(cond
((and (setq bind (key-binding [?g]))
(not (string-match-p "self-insert" (symbol-name bind)))
(not (memq bind '(ergoemacs-revert-buffer revert-buffer))))
(call-interactively bind))
((and (not (eq last-command 'ergoemacs-revert-buffer))
(buffer-modified-p (current-buffer)))
(revert-buffer)
(goto-char opt))
((and backup-buffer
(or (and (not (eq last-command 'ergoemacs-revert-buffer))
(setq ergoemacs-revert-buffer 0)
(file-readable-p (nth ergoemacs-revert-buffer backup-buffer))
(if (and (= (point-min) (point-max)) (not (eq last-command 'ergoemacs-revert-buffer))) nil
(yes-or-no-p (format "Revert buffer to backup saved on disk (%s)?" (nth 0 backup-buffer)))))
(and (eq last-command 'ergoemacs-revert-buffer)
(setq ergoemacs-revert-buffer (+ ergoemacs-revert-buffer 1)))))
(if (= ergoemacs-revert-buffer (length backup-buffer))
(progn
(revert-buffer t t)
(setq ergoemacs-revert-buffer -1)
(message "Reverted to saved version"))
(delete-region (point-min) (point-max))
(insert-file-contents (nth ergoemacs-revert-buffer backup-buffer))
(message "Reverted to autosave: %s" (nth ergoemacs-revert-buffer backup-buffer)))
(goto-char opt))
(t
(call-interactively 'revert-buffer)))))
(defun ergoemacs-major-mode-p (value)
"Return t if VALUE is a major mode function."
;; Taken from http://bazaar.launchpad.net/~nxhtml/nxhtml/main/view/head:/util/ourcomments-util.el
(let ((sym-name (symbol-name value)))
;; Do some reasonable test to find out if it is a major mode.
;; Load autoloaded mode functions.
;;
;; Fix-me: Maybe test for minor modes? How was that done?
(when (and (fboundp value)
(commandp value)
(not (memq value '(flyspell-mode isearch-mode savehist-mode)))
(< 5 (length sym-name))
(string= "-mode" (substring sym-name (- (length sym-name) 5)))
(if (and (listp (symbol-function value))
(eq 'autoload (car (symbol-function value))))
(progn
(message "loading ")
(load (cadr (symbol-function value)) t t))
t)
(or (memq value
;; Fix-me: Complement this table of known major modes:
'(fundamental-mode
xml-mode
nxml-mode
nxhtml-mode
css-mode
javascript-mode
espresso-mode
php-mode
))
(and (intern-soft (concat sym-name "-hook"))
;; This fits `define-derived-mode'
(get (intern-soft (concat sym-name "-hook")) 'variable-documentation))
(progn (message "Not a major mode: %s" value)
;;(sit-for 4)
nil)))
t)))
(defun ergoemacs-exit-customize-save-customized ()
"Call `customize-save-customized' on exit Emacs."
(let (set save val revert save-p)
(dolist (elt ergoemacs-set-ignore-customize)
;; Changed -- (Adatped from cus-edit+)
(when (ergoemacs :custom-p elt)
(setq set (get elt :ergoemacs-set-value)
save (get elt :ergoemacs-save-value)
val (ergoemacs-sv elt))
(if (not (equal set val))
(unless (or (eq elt 'echo-keystrokes)
(string-match-p "-\\(hook\\|mode\\)$" (symbol-name elt)))
(setq save-p t)
(message "%s was changed outside of ergoemacs-mode\n\tPrior: %s\n\tErgoemacs: %s\n\tFinal: %s" elt
save
set
val)
(customize-mark-to-save elt))
;; Consider this value unchanged (even though it was...)
(set-default elt save)
(set elt save)
(push elt revert))))
(when save-p
(ignore-errors (unless noninteractive (customize-save-customized))))
(dolist (elt revert)
(set-default elt (get elt :ergoemacs-set-value))
(set elt (get elt :ergoemacs-set-value)))))
(defvar ergoemacs-terminal
"Local variable to determine if `ergoemacs-clean' is running a terminal `ergoemacs-mode'")
(defvar ergoemacs-batch-file
"Ergoemacs batch file to run `ergoemacs-mode' in a terminal")
(defun ergoemacs-clean-recompile-then-run (&optional terminal)
"Recompile `ergoemacs-mode' for a bootstrap environment.
When TERMINAL is non-nil, run in a terminal instead of GUI."
(interactive)
(let ((buf (get-buffer "*ergoemacs-clean*")))
(when buf
(kill-buffer buf)))
(switch-to-buffer-other-window (get-buffer-create "*ergoemacs-clean*"))
(let ((inhibit-read-only t))
(with-silent-modifications
(set (make-local-variable 'ergoemacs-terminal) terminal))
(setq default-directory (expand-file-name (file-name-directory (locate-library "ergoemacs-mode"))))
(delete-region (point-min) (point-max))
(when (or (equal current-prefix-arg '(4))
(equal current-prefix-arg '(16)))
(insert "Delete Byte Compiled Files:\n")
(dolist (file (directory-files default-directory t "\\(ergoemacs-test-global-.*elc?\\(\\.gz\\)?$\\|ergoemacs-global-.*elc?\\(\\.gz\\)?$\\|[.]elc\\(\\.gz\\)?$\\\)"))
(insert "\tDelete " file)
(delete-file file)
(insert "\n"))
(insert "\n"))
(if (equal current-prefix-arg '(16))
(let* ((emacs-exe (ergoemacs-emacs-exe))
(default-directory (expand-file-name (file-name-directory (locate-library "ergoemacs-mode"))))
(process (start-process-shell-command "ergoemacs-byte-compile"
"*ergoemacs-clean*"
(format "%s -L %s -Q --batch -f batch-byte-compile ergoemacs-*.el" emacs-exe default-directory))))
(set-process-sentinel process #'ergoemacs-run-clean))
(ergoemacs-run-clean nil nil))))
(defvar ergoemacs-run-clean nil
"Library to load and run instead of `ergoemacs-mode'.")
(defun ergoemacs-run-clean (process _change)
"Run the clean environment.
The PROCESS is the process where the clean environment is run."
(message "Run ergoemacs-clean (%s)" process)
(let ((emacs-exe (ergoemacs-emacs-exe))
(inhibit-read-only t)
(ergoemacs-load (or ergoemacs-run-clean
" --load=\"ergoemacs-mode\" --load=\"ergoemacs-test\" --eval \"(progn (require 'elp) (setq debug-on-error t) (elp-instrument-package (symbol-name 'ergoemacs-)) (ergoemacs-mode 1) (run-with-idle-timer 0.1 nil 'elp-results))\""))
cmd process)
(cond
((with-current-buffer (get-buffer-create "*ergoemacs-clean*")
(not ergoemacs-terminal))
(setq cmd (format "%s --debug-init -Q -L \"%s\" %s" emacs-exe
(expand-file-name (file-name-directory (locate-library "ergoemacs-mode")))
ergoemacs-load)))
((and (eq system-type 'windows-nt) (executable-find "cmd"))
; Needs some work....
(setq cmd (format "%s -nw --debug-init -Q -L \"%s\" %s"
emacs-exe
(expand-file-name (file-name-directory (locate-library "ergoemacs-mode")))
ergoemacs-load))
(with-silent-modifications
(set (make-local-variable 'ergoemacs-batch-file)
(make-temp-file "ergoemacs-clean" nil ".bat")))
(with-temp-file ergoemacs-batch-file
(insert cmd))
(setq default-directory (file-name-directory ergoemacs-batch-file)))
((executable-find "xterm")
(setq cmd (format "%s -e %s -nw --debug-init -Q -L \"%s\" %s"
(executable-find "xterm") emacs-exe
(expand-file-name (file-name-directory (locate-library "ergoemacs-mode")))
ergoemacs-load))))
(with-current-buffer (get-buffer-create "*ergoemacs-clean*")
(goto-char (point-max))
(insert "Command\n" cmd "\n\n"))
(with-current-buffer (get-buffer-create "*ergoemacs-clean*")
(unless (derived-mode-p 'compilation-mode)
(compilation-mode)))
(start-process-shell-command "ergoemacs-run-clean"
"*ergoemacs-clean*"
cmd)))
(defun ergoemacs-run-clean-rm-batch ()
"Remove temporary batch file."
(when ergoemacs-batch-file
(delete-file ergoemacs-batch-file)))
(defun ergoemacs-clean-library (library &optional terminal)
"Run a clean LIBRARY with an `ergoemacs-mode' autoload.
If TERMINAL is non-nil, run the terminal version"
(interactive "aLibrary: \n")
(let* ((lib-file (expand-file-name (find-lisp-object-file-name library (symbol-function library))))
(lib-dir (file-name-directory lib-file))
(req-file (file-name-sans-extension
(file-name-nondirectory lib-file)))
(ergoemacs-run-clean (format " -L \"%s\" --load \"%s\" --eval \"(progn (setq debug-on-error t) (%s) (autoload 'ergoemacs-mode (symbol-name 'ergoemacs-mode) nil t))\"" lib-dir req-file library)))
(if terminal
(ergoemacs-clean-nw)
(ergoemacs-clean))))
(defun ergoemacs-clean ()
"Run ergoemacs in a bootstrap environment.
\\[universal-argument] deletes old byte compiled `ergoemacs-mode' files, and the recompiles.
\\[universal-argument] \\[universal-argument] deletes old byte compilde `ergoemacs-mode' files."
(interactive)
(ergoemacs-clean-recompile-then-run))
(defun ergoemacs-clean-nw ()
"Run ergoemacs in bootstrap environment in terminal.
\\[universal-argument] deletes old byte compiled `ergoemacs-mode' files, and the recompiles.
\\[universal-argument] \\[universal-argument] deletes old byte compilde `ergoemacs-mode' files."
(interactive)
(ergoemacs-clean-recompile-then-run t))
(defun ergoemacs-emacs-exe ()
"Get the Emacs executable for testing purposes."
;; FIXME: The quotes can't be quite right; better use something like
;; `shell-quote-argument'.
(let* ((emacs-exe (if (fboundp 'invocation-name) (invocation-name)
(if (boundp 'invocation-name) invocation-name)))
(emacs-dir (if (fboundp 'invocation-directory) (invocation-directory)
(if (boundp 'invocation-directory) invocation-directory)))
(full-exe (concat "\"" (expand-file-name emacs-exe emacs-dir)
"\"")))
full-exe))
(defun ergoemacs-open-line ()
"Insert and move to an indented newline after the current line."
(interactive "P")
(end-of-line)
(newline-and-indent))
(defun ergoemacs-call-keyword-completion ()
"Call the command that has keyboard shortcut M-TAB."
(interactive)
(call-interactively
(key-binding (kbd "M-TAB"))))
(defun ergoemacs-copy-all ()
"Put the whole buffer content into the `kill-ring'.
If `narrow-to-region' is in effect, then copy that region only."
(interactive)
(kill-new (buffer-string))
(message "Buffer content copied."))
(defun ergoemacs-cut-all ()
"Cut the whole buffer content into the `kill-ring'.
If `narrow-to-region' is in effect, then cut that region only."
(interactive)
(kill-new (buffer-string))
(delete-region (point-min) (point-max)))
(defcustom ergoemacs-keep-region-after-copy nil
"Keep region after copy."
:type 'boolean
:group 'ergoemacs-mode)
(defun ergoemacs--keep-mark-active ()
(when (mark t)
(setq mark-active t
deactivate-mark nil)))
(defun ergoemacs-copy-line-or-region (&optional arg)
"Copy current line, or current text selection.
Pass prefix ARG to the respective copy functions."
(interactive "P")
(setq unread-command-events nil)
(cond
;;; cua-copy-rectangle
((and (boundp 'cua--rectangle) cua--rectangle cua-mode)
(cua-copy-rectangle arg))
((and (region-active-p) cua-mode)
(cua-copy-region arg))
((region-active-p)
(kill-ring-save (region-beginning) (region-end)))
(t
;; Hack away to support `org-mode' folded reg
(kill-ring-save
(save-excursion
(let ((pt (point)))
(call-interactively 'move-beginning-of-line)
(when (= pt (point))
(call-interactively 'move-beginning-of-line)))
(when (not (bolp))
(beginning-of-line))
(point))
(save-excursion
(let ((pt (point)))
(call-interactively 'move-end-of-line)
(when (= pt (point))
(call-interactively 'move-end-of-line)))
(re-search-forward "\\=\n" nil t) ;; Include newline
(point)))))
(if ergoemacs-keep-region-after-copy
(ergoemacs--keep-mark-active)
(deactivate-mark)))
(defun ergoemacs-cut-line-or-region (&optional arg)
"Cut the current line, or current text selection.
Use `cua-cut-rectangle' or `cua-cut-region' when the variable
`cua-mode' is non-nil.
Otherwise, when a region is active, use
`ergoemacs-shortcut-remap' to remap any mode that changes
Emacs' default cut key, Ctrl+w (`kill-region').
When region is not active, move to the beginning of the line and
use `kill-line'. If looking at the end of the line, run
`kill-line' again. The prefix arguments will be preserved for
the first `kill-line', but not the second.
Note that `ergoemacs-shortcut-remap' will remap mode-specific
changes to `kill-line' to allow it to work as expected in
major-modes like `org-mode'.
The ARG is passed to the respective function for any prefixes."
(interactive "P")
(setq unread-command-events nil)
(cond
((and (boundp 'cua--rectangle) cua--rectangle)
(cua-cut-rectangle-as-text arg))
((and (region-active-p) (boundp 'cua-mode) cua-mode)
(cua-cut-region arg)
(deactivate-mark))
((region-active-p) ;; In case something else is bound to C-w.
(call-interactively 'kill-region)
(deactivate-mark))
(t
(ignore-errors
(let ((pt (point)))
(call-interactively 'move-beginning-of-line)
(when (= pt (point))
(call-interactively 'move-beginning-of-line))))
(when (not (bolp))
(beginning-of-line))
;; Keep prefix args.
(let ((kill-whole-line t))
(kill-line)))))
;; When editing a search in isearch, it uses the
;; minibuffer-local-isearch-map keymap, which get overridden by the
;; global emulation keymap. So we make our own version of
;; isearch-forward and isearch-backward to handle that.
;;;###autoload
(defun ergoemacs-isearch-forward ()
(interactive)
(if (eq (current-local-map) minibuffer-local-isearch-map)
(isearch-forward-exit-minibuffer)
(isearch-forward)
)
)
;;;###autoload
(defun ergoemacs-isearch-backward ()
(interactive)
(if (eq (current-local-map) minibuffer-local-isearch-map)
(isearch-reverse-exit-minibuffer)
(isearch-backward)
)
)
;;; CURSOR MOVEMENT
(defun ergoemacs-forward-open-bracket (&optional number)
"Move cursor to the next occurrence of left bracket/ quotation mark.
With prefix NUMBER, move forward to the next NUMBER left bracket
or quotation mark.
With a negative prefix NUMBER, move backward to the previous
NUMBER left bracket or quotation mark."
(interactive "p")
(if (and number
(> 0 number))
(ergoemacs-backward-open-bracket (- 0 number))
(forward-char 1)
(search-forward-regexp
(eval-when-compile
(regexp-opt
'("\"" "(" "{" "[" "<" "〔" "【" "〖" "〈" "《"
"「" "『" "“" "‘" "‹" "«"))) nil t number)
(backward-char 1)))
(defun ergoemacs-backward-open-bracket (&optional number)
"Move cursor to the previous occurrence of left bracket or quotation mark.
With prefix argument NUMBER, move backward NUMBER open brackets.
With a negative prefix NUMBER, move forward NUMBER open brackets."
(interactive "p")
(if (and number
(> 0 number))
(ergoemacs-forward-open-bracket (- 0 number))
(search-backward-regexp
(eval-when-compile
(regexp-opt
'("\"" "(" "{" "[" "<" "〔" "【" "〖" "〈" "《" "「"
"『" "“" "‘" "‹" "«"))) nil t number)))
(defun ergoemacs-forward-close-bracket (&optional number)
"Move cursor to the next occurrence of right bracket or quotation mark.
With a prefix argument NUMBER, move forward NUMBER closed bracket.
With a negative prefix argument NUMBER, move backward NUMBER closed brackets."
(interactive "p")
(if (and number
(> 0 number))
(ergoemacs-backward-close-bracket (- 0 number))
(search-forward-regexp
(eval-when-compile
(regexp-opt '("\"" ")" "]" "}" ">" "〕" "】" "〗" "〉" "》" "」" "』" "”" "’" "›" "»"))) nil t number)))
(defun ergoemacs-backward-close-bracket (&optional number)
"Move cursor to the previous occurrence of right bracket or quotation mark.
With a prefix argument NUMBER, move backward NUMBER closed brackets.
With a negative prefix argument NUMBER, move forward NUMBER closed brackets."
(interactive "p")
(if (and number
(> 0 number))
(ergoemacs-forward-close-bracket (- 0 number))
(backward-char 1)
(search-backward-regexp
(eval-when-compile
(regexp-opt '("\"" ")" "]" "}" ">" "〕" "】" "〗" "〉" "》" "」" "』" "”" "’" "›" "»"))) nil t number)
(forward-char 1)))
(defun ergoemacs-forward-block (&optional number)
"Move cursor forward to the beginning of next text block.
A text block is separated by 2 empty lines (or line with just
whitespace).
In most major modes, this is similar to `forward-paragraph', but
this command's behavior is the same regardless of syntax table.
With a prefix argument NUMBER, move forward NUMBER blocks.
With a negative prefix argument NUMBER, move backward NUMBER blocks."
(interactive "p")
(if (and number
(> 0 number))
(ergoemacs-backward-block (- 0 number))
(if (search-forward-regexp "\n[[:blank:]\n]*\n+" nil "NOERROR" number)
(progn (backward-char))
(progn (goto-char (point-max))))))
(defun ergoemacs-backward-block (&optional number)
"Move cursor backward to previous text block.
With a prefix argument NUMBER, move backward NUMBER blocks.
With a negative prefix argument NUMBER, move forward NUMBER blocks.
See: `ergoemacs-forward-block'"
(interactive "p")
(if (and number
(> 0 number))
(ergoemacs-forward-block (- 0 number))
(if (search-backward-regexp "\n[\t\n ]*\n+" nil "NOERROR" number)
(progn
(skip-chars-backward "\n\t ")
(forward-char 1))
(progn (goto-char (point-min))))))
(defcustom ergoemacs-back-to-indentation t
"Allow `ergoemacs-beginning-of-line-or-what' to move cursor back to the beginning of the indentation. Otherwise, it is always beginning of line."
:type 'boolean
:group 'ergoemacs-mode) ;
(defcustom ergoemacs-end-of-comment-line t
"When non-nil, treat comments different for beginning/end of line.
When non-nil `ergoemacs-end-of-line-or-what', the end of the
line is the end of the code line first, then the end of the code
+ comment.
When non-nil `ergoemacs-beginning-of-line-or-what' to move the
cursor to the beginning of the comment, then end of code,
followed by the beginning of indentation (if
`ergoemacs-back-to-indentation' is true) and beginning of line."
:type 'boolean
:group 'ergoemacs-mode)
(defcustom ergoemacs-use-beginning-or-end-of-line-only 'on-repeat
"Allow `ergoemacs-beginning-of-line-or-what' and `ergoemacs-end-of-line-or-what' to only go to the beginning/end of a line."
:type '(choice
(const :tag "Only go to the beginning or end of a line" t)
(const :tag "Goto beginning/end of block whenever at beginning/end of line" nil)
(const :tag "Goto beginning/end of block when at beginining/end of line and have already pressed the key." on-repeat))
:group 'ergoemacs-mode)
(defcustom ergoemacs-beginning-or-end-of-line-and-what 'block
"Change repeatable behavior of beginning/end of line.
When 'buffer use `beginning-of-buffer' or `end-of-buffer'
When 'page use `scroll-down-command' or `scroll-up-command'
When 'block use `ergoemacs-backward-block' or `ergoemacs-forward-block'
When 'nil don't use a repeatable command."
:type '(choice
(const :tag "Goto beginning/end of buffer" buffer)
(const :tag "Page Up" page)
(const :tag "Goto beginning/end of block" block)
(const :tag "Do nothing on repeat at beginning/end of line" nil))
:group 'ergoemacs-mode)
(defcustom ergoemacs-beginning-or-end-of-line-prefix-scrolls-other-window t
"Turn on scrolling the other window.
With a single prefix argument (called with \\[universal-argument]),
`ergoemacs-end-of-line-or-what' and
`ergoemacs-beginning-of-line-or-what' do a page up/down in the
other window."
:type 'boolean
:group 'ergoemacs-mode)
(defcustom ergoemacs-repeatable-beginning-or-end-of-buffer t
"Makes the beginning and end of buffer command repeatable.
Calling it more than once changes the point from the beginning to
the end of the buffer."
:type 'boolean
:group 'ergoemacs-mode)
(defun ergoemacs-beginning-or-end-of-buffer ()
"Goto end or beginning of buffer.
See `ergoemacs-end-or-beginning-of-buffer'.
This behavior can be turned off with
`ergoemacs-repeatable-beginning-or-end-of-buffer'."
(interactive)
(let ((ma (region-active-p)))
(if current-prefix-arg
(call-interactively 'end-of-buffer)
(cond
((and ergoemacs-repeatable-beginning-or-end-of-buffer (bobp))
(call-interactively 'end-of-buffer))
(t
(call-interactively 'beginning-of-buffer))))
(when (and (not ma) (region-active-p))
(deactivate-mark))))
(defun ergoemacs-end-or-beginning-of-buffer ()
"Go to beginning or end of buffer.
This calls `end-of-buffer', unless there is no prefix and the
point is already at the beginning of the buffer.
On repeating, this function will call `beginning-of-buffer'.
This function tries to be smart and if the major mode redefines
the keys, use those keys instead.
The repatable behavior can be turned off
with`ergoemacs-repeatable-beginning-or-end-of-buffer'
This will not honor `shift-select-mode'."
(interactive)
(let ((ma (region-active-p)))
(if current-prefix-arg
(call-interactively 'end-of-buffer)
(cond
((and ergoemacs-repeatable-beginning-or-end-of-buffer (eobp))
(call-interactively 'beginning-of-buffer))
(t
(call-interactively 'end-of-buffer))))
(when (and (not ma) (region-active-p))
(deactivate-mark))))
;; Extends behavior of
;; http://emacsredux.com/blog/2013/05/22/smarter-navigation-to-the-beginning-of-a-line/
(defvar ergoemacs-beginning-of-line-or-what-last-command nil)
(defun ergoemacs-beginning-of-line-or-what (&optional N)
"Move cursor to beginning of line or something else.
This could be the indentation, line, or text block, or beginning
of buffer, depending on context, options, and the number of times
this is repeated.
(a text block is separated by empty lines).
This command moves the cursor as follows:
1. Move cursor to the beginning of a comment
(if `ergoemacs-end-of-comment-line') is true.
From:
(progn
(ergoemacs-mode 1)) ; Turn on ergoemacs-mode|
To:
(progn
(ergoemacs-mode 1)) ; |Turn on ergoemacs-mode
2. Move to the end of the line, ignoring comments
(if `ergoemacs-end-of-comment-line') is true.
From:
(progn
(ergoemacs-mode 1)) ; |Turn on ergoemacs-mode
To:
(progn
(ergoemacs-mode 1))| ; Turn on ergoemacs-mode
3. Move cursor to the first non-whitespace character of a line,
if `ergoemacs-back-to-indentation' is true (otherwise skip).
From:
(progn
(ergoemacs-mode 1))| ; Turn on ergoemacs-mode
To:
(progn
|(ergoemacs-mode 1)) ; Turn on ergoemacs-mode
4. Move to the beginning of line
From:
(progn
|(ergoemacs-mode 1)) ; Turn on ergoemacs-mode
To:
(progn
| (ergoemacs-mode 1)) ; Turn on ergoemacs-mode
5. After #4, move to (based on
`ergoemacs-beginning-or-end-of-line-and-what'):
a. Beginning of text-block when selected ('block),
b. Beginning of buffer ('buffer), or
c. A PgUp ('page)