-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathonboard.el
1450 lines (1137 loc) · 51.8 KB
/
onboard.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
;;; onboard.el --- Emacs Onboard Starter Kit -*- lexical-binding: t; -*-
;;
;; ▒░▒░▒░ ▒░ ▒░ ▒░▒░▒░▒░ ▒░▒░▒░ ▒░ ▒░▒░▒░▒░ ▒░▒░▒░▒░
;; ▒░ ▒░ ▒░▒░ ▒░ ▒░ ▒░ ▒░ ▒░ ▒░▒░ ▒░ ▒░ ▒░ ▒░
;; ▒░ ▒░ ▒░ ▒░ ▒░ ▒░ ▒░ ▒░ ▒░ ▒░ ▒░ ▒░ ▒░ ▒░ ▒░
;; ▒░ ▒░ ▒░ ▒░ ▒░ ▒░▒░▒░▒░ ▒░ ▒░ ▒░ ▒░ ▒░▒░▒░▒░ ▒░ ▒░
;; ▒░ ▒░ ▒░ ▒░▒░ ▒░ ▒░ ▒░ ▒░ ▒░▒░▒░▒░ ▒░ ▒░ ▒░ ▒░
;; ▒░ ▒░ ▒░ ▒░ ▒░ ▒░ ▒░ ▒░ ▒░ ▒░ ▒░ ▒░ ▒░ ▒░
;; ▒░▒░▒░ ▒░ ▒░ ▒░▒░▒░▒░ ▒░▒░▒░ ▒░ ▒░ ▒░ ▒░ ▒░▒░▒░▒░
;;
;;; Commentary:
;; Emacs ONBOARD offers a clean slate to build your personal Emacs config.
;; It stays as close as possible to vanilla Emacs, but offers some convenience
;; and a better user experience, while only relying on built-in packages.
;;
;; Copyright (C) 2021–2025 Dan Dee
;; Author: Dan Dee <monkeyjunglejuice@pm.me>
;; URL: https://github.com/monkeyjunglejuice/emacs.onboard
;; Version: 1.3.5
;; Package-Requires: ((EMACS "28.2"))
;; Keywords: convenience
;; SPDX-License-Identifier: MIT
;; This file is not part of GNU Emacs.
;;
;;; Keybindings:
;;
;; "M-x" Show all commands
;; – hold down the "Meta key" and press <x>
;; – the "Meta key" is usually <Alt> on Linux/Windows and <Option> on Mac
;;
;; "C-g" Get out! Press <Ctrl>+<g> to cancel whatever happens – or hit 3x <ESC>
;;
;; "F12" Toggle between dark and light theme
;;
;;; Examples:
;;
;; "M-x eon-" Show all commands defined by Emacs ONBOARD
;; "M-x eon-visit-user-init-file" Visit main config file: .emacs or init.el
;; "M-x check-parens" Check if all parens match in Emacs Lisp code
;; "M-x help" Reach the ultimate help menu
;;
;; "C-h o" Place the cursor behind a keyword, function, variable or other symbol
;; to issue the command `describe-symbol' via keybinding
;; and view the symbol's documentation
;;
;; "M-;" Comment/uncomment a selected piece of text or code
;;
;;; Code:
;; ____________________________________________________________________________
;;; GARBAGE COLLECTION
;; <https://www.gnu.org/software/emacs/manual/html_mono/elisp.html#Garbage-Collection>
;; Set a high value of 1 GB to prevent frequent garbage collections
;; during initialization.
(setq gc-cons-threshold #x40000000) ; default threshold is 800 KB
;; Prevent longer GC pauses and experience less mini-interruptions.
;; When idle for 15 sec, run the GC no matter what.
;; This hack was stolen from <https://akrl.sdf.org/>
(defmacro eon-time (&rest body)
"Measure and return the time it takes evaluating BODY."
`(let ((time (current-time)))
,@body
(float-time (time-since time))))
(defvar eon-gc-timer nil
"Timer object for garbage collection monitoring.
The timer can be canceled with `eon-cancel-gc-timer'.")
(defun eon-start-gc-timer ()
"Start the garbage collection timer."
(interactive)
(setq eon-gc-timer
(run-with-idle-timer 15 t (lambda () (eon-time (garbage-collect))))))
(defun eon-cancel-gc-timer ()
"Cancel the garbage collection timer."
(interactive)
(when (timerp eon-gc-timer)
(cancel-timer eon-gc-timer)
(setq eon-gc-timer nil)))
;; Start the GC Timer
(eon-start-gc-timer)
;; Show a message when garbage collection happens? Useful while tuning the GC
(setq garbage-collection-messages nil)
;; Diagnostics
(add-hook 'emacs-startup-hook
(lambda ()
(message "Emacs started in %s with %d garbage collections."
(format "%.3f seconds"
(float-time
(time-subtract after-init-time before-init-time)))
gcs-done)))
;; ____________________________________________________________________________
;;; PACKAGE MANAGEMENT
;; <https://www.gnu.org/software/emacs/manual/html_mono/emacs.html#Packages>
;; ... or do "M-x info-emacs-manual s packages RET" to read it within Emacs
;; Browse, select and install 3rd-party packages with "M-x list-packages RET"
(require 'package)
;; 1st priority
(add-to-list 'package-archives
'("melpa" . "https://melpa.org/packages/") t)
;; 2nd priority
;; Install form melpa-stable' only when the package from 'melpa' is broken
;; (add-to-list 'package-archives
;; '("melpa-stable" . "https://stable.melpa.org/packages/") t)
;; 3rd priority
;; There are also Gnu Elpa and Non-Gnu Elpa, which are enabled by default
;; Natively compile packages at first use or immediately after installation?
(setq package-native-compile t)
;; Highlight current line in the package manager
(add-hook 'package-menu-mode-hook
(lambda ()
(hl-line-mode 1)))
;; Install packages declaratively within an Emacs Lisp file
;; Better use `use-package' instead (Emacs >= 29)
(defun eon-package (action package-list)
"Helper function to install 3rd-party packages declaratively.
PACKAGE-LIST will be installed if \='install is passed as an argument to ACTION.
When ACTION receives \='ignore, then nothing will happen."
(when (eq action 'install)
(mapc #'(lambda (package)
(unless (package-installed-p package)
(package-refresh-contents)
(package-install package nil)))
package-list)))
;; Example: You can install suggested 3rd-party packages from within this file
;; with single function calls like so:
;;
;; (eon-package 'install '(the-matrix-theme)) ; installs the package
;; (eon-package 'ignore '(the-matrix-theme)) ; does nothing (default)
;;
;; The installation will be performed when you restart Emacs or
;; when you evaluate a function manually – eg. via pressing "C-M-x"
;; while the cursor is placed somewhere within a function application form.
;; ____________________________________________________________________________
;; HELPERS
;; Simplify writing of operating-system-specific Elisp code
(defun eon-linp ()
"True if `system-type' is Linux or something compatible.
For finer granularity, use the variables `system-type'
or `system-configuration' directly."
(string= system-type (or "gnu/linux" "berkeley-unix" "gnu" "gnu/kfreebsd")))
(defun eon-winp ()
"True if `system-type' is Windows or something compatible.
For finer granularity, use the variables `system-type'
or `system-configuration' directly."
(string= system-type (or "windows-nt" "cygwin" "ms-dos")))
(defun eon-macp ()
"True if `system-type' is MacOS or something compatible.
For finer granularity, use the variables `system-type'
or `system-configuration' directly."
(string= system-type "darwin"))
;; Open the '~/.emacs.d' directory in the Dired file manager
(defun eon-visit-user-emacs-directory ()
"Open the Emacs directory in Dired, which is ~/.emacs.d usually."
(interactive)
(dired user-emacs-directory))
;; Emacs knows where your init file is (open and edit '.emacs' or 'init.el')
(defun eon-visit-user-init-file ()
"Visit the init file."
(interactive)
(find-file user-init-file))
;; ____________________________________________________________________________
;;; KEYMAPS
;; Make "C-z" available as a prefix key in the same manner as "C-x" and "C-c".
;; To avoid clashes, new keybindings introduced by Emacs Onboard will usually
;; begin with the prefix "C-z" (with only a few exceptions).
(global-unset-key (kbd "C-z"))
(define-prefix-command 'ctl-z-map nil "Additional prefix key C-z")
(global-set-key (kbd "C-z") 'ctl-z-map)
(define-prefix-command 'ctl-z-c-map nil "Commonly used commands")
(define-key ctl-z-map (kbd "c") 'ctl-z-c-map)
(define-prefix-command 'ctl-z-e-map nil "Emacs built-ins")
(define-key ctl-z-map (kbd "e") 'ctl-z-e-map)
(define-prefix-command 'ctl-z-o-map nil "Org-mode")
(define-key ctl-z-map (kbd "o") 'ctl-z-o-map)
(define-prefix-command 'ctl-z-s-map nil "Scratch buffers")
(define-key ctl-z-map (kbd "s") 'ctl-z-s-map)
(define-prefix-command 'ctl-z-w-map nil "Web-related")
(define-key ctl-z-map (kbd "w") 'ctl-z-w-map)
(define-prefix-command 'ctl-z-x-map nil "Global REPL bindings")
(define-key ctl-z-map (kbd "x") 'ctl-z-x-map)
;; ____________________________________________________________________________
;;; KEYBINDINGS
;; Make the <Command> key on MacOS act as <Ctrl> key: "C- ..."
(setq mac-command-modifier 'control)
;; Make the <Option> key on MacOS act as <Meta> key for "M- ..."
(setq mac-option-modifier 'meta)
;; Don't bypass "C-h ..." keybindings
(setq mac-pass-command-to-system nil)
;; ____________________________________________________________________________
;;; SYSTEM
;; Prevent stale elisp bytecode from shadowing more up-to-date source files
(setq load-prefer-newer t)
;; Increase warning threshold
(setq large-file-warning-threshold (* 64 1000000))
;; Set undo limit to 64 MB
(setq undo-outer-limit (* 64 1000000))
;; Increase the amount of data which Emacs reads from subprocesses
(setq read-process-output-max (* 1024 1024)) ; 1 MB
;; ____________________________________________________________________________
;;; SERVER
;; <https://www.gnu.org/software/emacs/manual/html_mono/emacs.html#Emacs-Server>
;; ... or do "M-x info-emacs-manual s server RET" to read it within Emacs
(require 'server)
;; Display the name of the Emacs server process in the frame title
;; to see easily to which server process a client is connected to
;; Further information:
;; <https://monkeyjunglejuice.github.io/blog/emacs-server-name-frame-title.howto.html>
(defun eon-frame-title ()
"Set a custom frame title."
(setq frame-title-format
(concat "%b (%f)"
(when (server-running-p)
(concat " " server-name)))))
(add-hook 'emacs-startup-hook
(lambda ()
"Run functions after loading init files"
(eon-frame-title)))
(add-hook 'server-mode-hook
(lambda ()
"Run functions after entering or leaving server-mode."
(eon-frame-title)))
;; Shutdown the Emacs server process
(defun eon-server-stop ()
"Save buffers, quit and shutdown (kill) server."
(interactive)
(save-some-buffers)
(kill-emacs))
;; Start the server?
;; (server-start)
;; ____________________________________________________________________________
;;; FONTS
;;; <https://www.gnu.org/software/emacs/manual/html_mono/emacs.html#Fonts>
;; You can use this function definition as a template to define your own font
;; set, then call your personal function via `eon-load-after-light-theme-hook'
;; and `eon-load-after-light-theme-hook' (under section 'THEME CONFIG').
(defun eon-fonts-default ()
"The height value is in 1/10 pt, so 140 will give 14 pt."
(interactive)
;; Set the default monospaced font
(set-face-attribute 'default nil
;; :family "Iosevka Curly"
:slant 'normal
:weight 'normal
:width 'normal
:height 140)
;; Set an alternative monospaced font. Can be the same as above.
;; It should have the same character width as the default font
(set-face-attribute 'fixed-pitch nil
;; :family "Iosevka Curly"
:slant 'normal
:weight 'normal
:width 'normal
:height 1.0)
;; Set an alternative monospaced font, preferably with serifs (optional)
;; It should have the same character width as the other two fonts above
(set-face-attribute 'fixed-pitch-serif nil
;; :family "Iosevka Slab"
:slant 'normal
:weight 'normal
:width 'normal
:height 1.0)
;; Set the proportional font (toggle by "M-x variable-pitch-mode")
(set-face-attribute 'variable-pitch nil
;; :family "Iosevka Etoile"
:slant 'normal
:weight 'normal
:width 'normal
:height 1.0)
;; Set the fonts for the active mode line
(set-face-attribute 'mode-line nil
;; :family "Iosevka Curly"
:slant 'normal
:weight 'normal
:width 'normal
:height 0.8)
;; Set the fonts for the inactive mode line
(set-face-attribute 'mode-line-inactive nil
;; :family "Iosevka Curly"
:slant 'normal
:weight 'normal
:width 'normal
:height 0.8))
;; ____________________________________________________________________________
;;; TOGGLE THEME
;; Default/fallback definitions – don't change them here,
;; but scroll further down to 'THEME CONFIG'
(defgroup eon-toggle-theme nil
"Toggle between light and dark theme with a single key press."
:group 'convenience)
(defcustom eon-light-theme-name
(setq eon-light-theme-name 'modus-operandi)
"Name of the light theme."
:group 'toggle-theme
:type 'symbol)
(defcustom eon-dark-theme-name
(setq eon-dark-theme-name 'modus-vivendi)
"Name of the dark theme."
:group 'toggle-theme
:type 'symbol)
(defcustom eon-default-theme-variant 'light
"Load either the light or the dark theme at startup?"
:group 'toggle-theme
:type 'symbol)
(defvar eon-active-theme-variant nil
"Holds the information about the currently active theme variant.")
(defcustom eon-load-before-light-theme-hook nil
"Run before loading the light theme."
:group 'toggle-theme
:type 'hook)
(defcustom eon-load-after-light-theme-hook nil
"Run after loading the light theme."
:group 'toggle-theme
:type 'hook)
(defcustom eon-load-before-dark-theme-hook nil
"Run before loading the dark theme."
:group 'toggle-theme
:type 'hook)
(defcustom eon-load-after-dark-theme-hook nil
"Run after loading the dark theme."
:group 'toggle-theme
:type 'hook)
(defun eon-load-theme-light ()
"Load the light theme and apply some modifications.
Some themes may come as functions -- wrap these ones in lambdas."
(interactive)
(mapc #'disable-theme custom-enabled-themes)
(run-hooks 'eon-load-before-light-theme-hook)
(cond ((symbolp eon-light-theme-name)
(load-theme eon-light-theme-name t))
((functionp eon-light-theme-name)
(funcall eon-light-theme-name)))
(setq eon-active-theme-variant 'light)
(run-hooks 'eon-load-after-light-theme-hook))
(defun eon-load-theme-dark ()
"Load the dark theme and apply some modifications.
Some themes may come as functions -- wrap these ones in lambdas."
(interactive)
(mapc #'disable-theme custom-enabled-themes)
(run-hooks 'eon-load-before-dark-theme-hook)
(cond ((symbolp eon-dark-theme-name)
(load-theme eon-dark-theme-name t))
((functionp eon-dark-theme-name)
(funcall eon-dark-theme-name)))
(setq eon-active-theme-variant 'dark)
(run-hooks 'eon-load-after-dark-theme-hook))
(defun eon-toggle-theme ()
"Toggle between light and dark theme."
(interactive)
(cond
((equal eon-active-theme-variant 'light) (eon-load-theme-dark))
((equal eon-active-theme-variant 'dark) (eon-load-theme-light))
(t (mapc #'disable-theme custom-enabled-themes))))
(defun eon-load-theme-default ()
"Load the default theme."
(cond
((equal eon-default-theme-variant 'light) (eon-load-theme-light))
((equal eon-default-theme-variant 'dark) (eon-load-theme-dark))
(t (message
"Toggle theme: DEFAULT-THEME-VARIANT must be either 'light or 'dark"))))
;; . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
;;; THEME CONFIG
;; Either configure the themes here, or "M-x customize-group RET toggle-theme"
;; Set the light theme:
;; (setq eon-light-theme-name 'modus-operandi)
;; Set the dark theme:
;; (setq eon-dark-theme-name 'modus-vivendi)
;; Set the default variant here:
;; (setq eon-default-theme-variant 'light)
;; Set the keybinding to toggle between light and dark:
(global-set-key (kbd "<f12>") #'eon-toggle-theme)
;; The hooks below can be used to run additional functions before or after
;; loading the selected light and dark theme. That's useful for setting
;; variables that otherwise would get overwritten by the themes.
;; Restart Emacs to take effect after changing the hooks.
(add-hook 'eon-load-after-light-theme-hook
(lambda ()
(eon-fonts-default)
))
(add-hook 'eon-load-after-dark-theme-hook
(lambda ()
(eon-fonts-default)
))
;; . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
;; Load the theme eventually
(eon-load-theme-default)
;; ____________________________________________________________________________
;;; DEFAULT AND INITIAL FRAME
;; In Emacs terminology, a "frame" means the ordinary "desktop window";
;; while "window" refers to tiled panels within an Emacs frame. Why?
;; Because Emacs had it first, and today's convention what "window" means
;; appeared later.
;; In order to define properties generally, add them to `default-frame-alist';
;; to affect only the first frame created, add them to `initial-frame-alist'.
;; Either start Emacs maximized …
;; (add-to-list 'default-frame-alist '(fullscreen . maximized))
;; … or set the default width of the Emacs frame in characters
(add-to-list 'default-frame-alist '(width . 80))
;; … and set the default height of the Emacs frame in lines
(add-to-list 'default-frame-alist '(height . 32))
;; Horizontal position: set the distance from the left screen edge in pixels
;; That way, only the first frame created will get a fixed position:
;; (add-to-list 'initial-frame-alist '(left . 0))
;; Vertical position: set the distance from the top screen edge in pixels
;; That way, only the first frame created will get a fixed position:
;; (add-to-list 'initial-frame-alist '(top . 0))
;; Fringe: choose on which sides (not) to show it
;; <https://www.gnu.org/software/emacs/manual/html_mono/emacs.html#Fringes>
;; (add-to-list 'default-frame-alist '(right-fringe . 0))
;; ____________________________________________________________________________
;;; CURSOR
;; <https://www.gnu.org/software/emacs/manual/html_mono/emacs.html#Cursor-Display>
;; To learn about available cursors, place your cursor behind 'cursor-type'
;; in the code below or do "M-x describe-symbol RET cursor-type RET"
;; Set the cursor type
;; Comment out the following expression to change the curser into to a bar
;; (add-to-list 'default-frame-alist '(cursor-type . bar))
;; Turn on/off cursor blinking by default?
(blink-cursor-mode -1) ; 1 means 'on' / -1 means 'off'
;; Cursor blinking interval in seconds
(setq blink-cursor-interval 0.4)
;; Emphasize the cursor when running Emacs in a text terminal?
(setq visible-cursor nil)
;; Make sure to highlight the current line only in the active window.
(setq hl-line-sticky-flag nil)
;; ____________________________________________________________________________
;;; USER INTERFACE
;; Show a help window with possible keys?
(when (>= emacs-major-version 30)
(setq which-key-lighter ""
which-key-idle-delay 0.4
which-key-sort-uppercase-first nil)
(which-key-mode 1))
;; Menu bar: on/off by default?
(menu-bar-mode 1)
;; Scroll bar: on/off by default?
(scroll-bar-mode -1)
;; Tool bar: on/off by default?
(tool-bar-mode -1)
;; Tooltips: enable/disable?
(tooltip-mode -1)
;; Startup screen: on/off by default?
(setq inhibit-startup-screen t)
;; Alarms: turn off?
(setq ring-bell-function 'ignore)
;; Visually indicate unused lines at the end of the buffer?
(setq-default indicate-empty-lines t)
;; Redraw the display – useful when running Emacs in a Windows terminal emulator
(define-key ctl-z-map (kbd "C-r") #'redraw-display)
;; ____________________________________________________________________________
;;; SMOOTH SCROLLING
;; <https://www.gnu.org/software/emacs/manual/html_mono/emacs.html#Scrolling>
(setq-default mouse-wheel-scroll-amount '(1 ((shift) . 1))
mouse-wheel-progressive-speed t
mouse-wheel-follow-mouse t
scroll-preserve-screen-position t
;; scroll-conservatively 10000
;; scroll-step 1 ; may lock up display in some cases
;; scroll-margin 1 ; leave n lines on both screen ends
scroll-up-aggressively 0.01
scroll-down-aggressively 0.01
auto-window-vscroll nil)
;; Enable pixel-based scrolling
(if (fboundp 'pixel-scroll-precision-mode)
(pixel-scroll-precision-mode 1))
;; ____________________________________________________________________________
;;; MODELINE
;; <https://www.gnu.org/software/emacs/manual/html_mono/emacs.html#Mode-Line>
;; Compress the mode line? If non-nil, repeating spaces are compressed into
;; a single space. If 'long', this is only done when the mode line is longer
;; than the current window width (in columns).
(setq mode-line-compact 'nil)
;; Show the buffer size in the modeline
(size-indication-mode 1)
;; Show column number along with line number in modeline
(column-number-mode 1)
;; ____________________________________________________________________________
;;; MINIBUFFER
;; <https://www.gnu.org/software/emacs/manual/html_mono/emacs.html#Minibuffer>
;; Recursive minibuffers
;; <https://www.gnu.org/software/emacs/manual/html_mono/emacs.html#Recursive-Edit>
;; Allow minibuffer commands while in the minibuffer
;; There are two commands to get out of recursive minibuffers:
;; "C-z-c" exit-recursive-edit and "C-]" abort-recursive-edit
(setq enable-recursive-minibuffers t)
(minibuffer-depth-indicate-mode 1)
;; Use the minibuffer instead of dialog boxes
(setq use-dialog-box nil)
;; Grow and shrink the minibuffer according to its content
(setq resize-mini-windows 'grow-only)
;; Save minibuffer history between Emacs sessions
(savehist-mode 1)
;; Delete duplicates from the command history
(setq history-delete-duplicates t)
;; Change all yes/no style questions to y/n style
(fset 'yes-or-no-p 'y-or-n-p)
;; ____________________________________________________________________________
;;; MINIBUFFER COMPLETION
;; <https://www.gnu.org/software/emacs/manual/html_mono/emacs.html#Icomplete>
;; There are many matching styles available, see `completion-styles-alist'
;; <https://www.gnu.org/software/emacs/manual/html_node/emacs/Completion-Styles.html>
;; Below is the standard combo from Emacs 29 plus `substring'
(require 'minibuffer
(setq completion-styles '(basic partial-completion emacs22 substring)))
;; Tweaking Icomplete
(require 'icomplete)
(setq icomplete-in-buffer t
icomplete-compute-delay 0.01
icomplete-delay-completions-threshold 10000
icomplete-show-matches-on-no-input t
icomplete-hide-common-prefix nil)
;; Emacs version 28 and later: vertical completion with fido-vertical
(when (>= emacs-major-version 28)
(fido-vertical-mode 1))
;; ____________________________________________________________________________
;;; ELDOC
;; <https://www.gnu.org/software/emacs/manual/html_mono/emacs.html#Lisp-Doc>
;; <https://www.masteringemacs.org/article/seamlessly-merge-multiple-documentation-sources-eldoc>
(setq eldoc-minor-mode-string nil
eldoc-documentation-strategy 'eldoc-documentation-compose-eagerly
eldoc-echo-area-display-truncation-message nil
eldoc-echo-area-prefer-doc-buffer nil
eldoc-echo-area-use-multiline-p 'truncate-sym-name-if-fit)
;; ____________________________________________________________________________
;;; PINENTRY
(require 'epg-config)
(setq epg-pinentry-mode 'loopback)
;; ____________________________________________________________________________
;;; WINDOW MANAGEMENT
;; <https://www.gnu.org/software/emacs/manual/html_mono/emacs.html#Windows>
;; <https://www.gnu.org/software/emacs/manual/html_mono/emacs.html#Window-Convenience>
;; Display-buffer: avoid resizing
(setq even-window-sizes nil)
;; Focus follows mouse?
(setq mouse-autoselect-window nil
focus-follows-mouse nil)
;; Default window navigation – simply switch to the next window in order.
;; Added for convenience; the default keybinding is "C-x o"
(global-set-key (kbd "M-o") #'other-window)
;; Navigate windows by direction
;; (require 'windmove)
;; (setq windmove-wrap-around nil)
;; (global-set-key (kbd "s-j") #'windmove-down)
;; (global-set-key (kbd "s-k") #'windmove-up)
;; (global-set-key (kbd "s-h") #'windmove-left)
;; (global-set-key (kbd "s-l") #'windmove-right)
;; Undo/redo window layouts
(require 'winner)
(winner-mode 1)
(define-key winner-mode-map (kbd "C-x 4 u") #'winner-undo)
(define-key winner-mode-map (kbd "C-x 4 r") #'winner-redo)
;; ____________________________________________________________________________
;;; BUFFERS
;; <https://www.gnu.org/software/emacs/manual/html_mono/emacs.html#Buffers>
(setq switch-to-buffer-obey-display-actions nil)
;; Uniquify buffer names for buffers that would have identical names
(setq uniquify-buffer-name-style 'forward)
;; Fast buffer switching
(global-set-key (kbd "M-[") #'previous-buffer)
(global-set-key (kbd "M-]") #'next-buffer)
;; Kill the current buffer immediately instead of presenting a selection
;; It's the equivalent to "close tab" in a web browser or other editors
(global-set-key (kbd "C-x k") #'kill-current-buffer)
;; Get the buffer out of the way, but let it alive
(global-set-key (kbd "C-x K") #'bury-buffer)
(global-set-key (kbd "C-x M-k") #'unbury-buffer)
;; Kill all buffers at once – equivalent to "close all tabs"
(defun eon-kill-all-buffers ()
"Close all buffers at once."
(interactive)
(save-some-buffers)
(let ((kill-buffer-query-functions '()))
(mapc #'kill-buffer (buffer-list))))
;; Define boring buffers globally, so they can be hidden.
;; These buffers remain accessible via Ibuffer "C-x C-b".
(defvar eon-boring-buffers '("\\` "
"\\`\\*Echo Area"
"\\`\\*Minibuf"
"\\`\\*Completions"
"\\`\\*Flymake log"
"\\`\\*Semantic SymRef"
"\\`\\*Backtrace"
"\\`\\*tramp"
"\\`\\*EGLOT"
;; And some hidden buffers can be visited by ...
"\\`\\*scratch" ; "C-z s s"
"\\`\\*Messages" ; "C-h e"
"\\`\\*Bookmark List" ; "C-x r l"
"\\`\\*Ibuffer" ; "C-x C-b"
)
"List of buffer names of buffers to hide on several occasions.
The elements of the list are regular expressions.")
;; ____________________________________________________________________________
;;; IBUFFER – the buffer manager
;; <https://protesilaos.com/codelog/2020-04-02-emacs-intro-ibuffer/>
(require 'ibuf-ext)
(add-hook 'ibuffer-mode-hook
(lambda ()
(ibuffer-auto-mode 1)))
(setq ibuffer-marked-face 'dired-marked)
;; Hide the boring buffers from Ibuffer too?
;; (setq ibuffer-never-show-predicates eon-boring-buffers)
(global-set-key (kbd "C-x C-b") 'ibuffer)
;; ____________________________________________________________________________
;;; SCRATCH BUFFER
;; Set an initial major mode for the *scratch* buffer:
;; Lisp-interaction-mode is the default
;; <https://www.gnu.org/software/emacs/manual/html_mono/emacs.html#Lisp-Interaction>
;; (setq initial-major-mode #'lisp-interaction-mode)
;; We're setting the scratch buffer to Org-mode which is more useful
;; for quick notes, writing and literate programming
(setq initial-major-mode #'org-mode)
;; Should the *scratch* buffer contain some initial content?
(setq initial-scratch-message "")
;; Quickly jump to the *scratch* buffer
(defun eon-scratch ()
"Jump to the *scratch* buffer. If it does not exist, create it."
(interactive)
(switch-to-buffer "*scratch*"))
(define-key ctl-z-s-map (kbd "s") #'eon-scratch)
;; ____________________________________________________________________________
;;; VISITING FILES AT POINT
;; "C-x C-v" – Visit any resource under the cursor
;; "M-x ffap-menu" – Display a list of all ressources mentioned in this buffer
(define-key ctl-z-map (kbd "C-.") #'find-file-at-point)
;; ____________________________________________________________________________
;;; CLIPBOARD, COPY & PASTE
;; <https://www.gnu.org/software/emacs/manual/html_mono/emacs.html#Killing>
(require 'select)
(setq
;; Use clipboard
select-enable-clipboard t
;; Use primary selection: mark = copy / middle-click = paste
select-enable-primary t
;; When one selects something in another program to pastes it into Emacs,
;; but kills something in Emacs before actually pasting it,
;; this selection is gone unless this variable is non-nil
save-interprogram-paste-before-kill t
;; Mouse yank commands yank at point instead of at click.
mouse-yank-at-point t)
;; Allow Emacs to copy to and paste from the GUI clipboard
;; when running in a text terminal
;; --> recommended 3rd-party package 'xclip'
;; If you would like to install this 3rd-party package, change 'ignore
;; into 'install and evaluate the expression – either via "C-z-x",
;; or simply restart Emacs:
(eon-package 'ignore '(xclip))
(when (fboundp #'xclip-mode) (xclip-mode 1))
;; Copy the full path of the current file
(defun eon-copy-file-name ()
"Copy the full path of the current buffer's file to the clipboard."
(interactive)
(let ((filename (if (equal major-mode 'dired-mode)
default-directory
(buffer-file-name))))
(when filename
(kill-new filename)
(message "Copied buffer file name '%s' to the clipboard."
filename))))
;; Simple alternative for 'yank-pop' – present a selection of the kill ring
(defun eon-insert-kill-ring-item ()
"Select and insert an item from the 'kill-ring'."
(interactive)
(insert (completing-read "Yank: " kill-ring nil t)))
(global-set-key (kbd "M-y") #'eon-insert-kill-ring-item)
;; Copy & paste between Windows and Emacs running within WSL
;; (Windows Subsysten for Linux) — which is technically a Linux, not Windows
;; Copy "kill" text from an Emacs buffer for pasting it into a Windows app
(when (eon-linp)
(defun eon-wsl-copy (start end)
"Copy selected text into the Windows clipboard."
(interactive "r")
(let ((default-directory "/mnt/c/"))
(shell-command-on-region start end "clip.exe")))
(define-key ctl-z-map (kbd "C-w") 'eon-wsl-copy))
;; Paste "yank" text into Emacs buffer that has been copied from a Windows app
(when (eon-linp)
(defun eon-wsl-paste ()
"Paste contents from the Windows clipboard into the Emacs buffer."
(interactive)
(let ((coding-system-for-read 'dos)
(default-directory "/mnt/c/"))
(insert
(substring
(shell-command-to-string "powershell.exe -command 'Get-Clipboard'")
0 -1))))
(define-key ctl-z-map (kbd "C-y") 'eon-wsl-paste))
;; ____________________________________________________________________________
;;; BACKUP
;; <https://www.gnu.org/software/emacs/manual/html_mono/emacs.html#Backup>
;; Make backup before editing
(setq backup-by-copying t
kept-new-versions 10
kept-old-versions 5
delete-old-versions t
version-control t)
;; Where to save the backups?
;; Specify file name/path patterns and directories ("REGEXP" . "DIRECTORY")
(setq backup-directory-alist
`(("." . ,(concat user-emacs-directory "backup/"))))
;; ____________________________________________________________________________
;;; LOCKFILES
;; <https://www.gnu.org/software/emacs/manual/html_mono/emacs.html#Interlocking>
;; Let Emacs keep track of files currently visited?
(setq create-lockfiles nil)
;; ____________________________________________________________________________
;;; AUTO-SAVE
;; <https://www.gnu.org/software/emacs/manual/html_mono/emacs.html#Auto-Save>
(setq auto-save-default nil
auto-save-interval 0)
;; ____________________________________________________________________________
;;; HELP
;; <https://www.gnu.org/software/emacs/manual/html_mono/emacs.html#Help>
;; Show all options when running 'apropos' "C-h a" (fulltext search)
(require 'apropos)
(setq apropos-do-all t)
;; ____________________________________________________________________________
;;; SEARCH
;; <https://www.gnu.org/software/emacs/manual/html_mono/emacs.html#Search>
;; Switch search functions to make regex-search the default
(global-set-key (kbd "C-s") #'isearch-forward-regexp)
(global-set-key (kbd "C-r") #'isearch-backward-regexp)
(global-set-key (kbd "C-S-s") #'isearch-forward)
(global-set-key (kbd "C-S-r") #'isearch-backward)
;; Search and replace
;; The 'query-' variant asks for each string. Confirm with "SPC",
;; or jump to the next via "n"
(global-set-key (kbd "M-%") #'query-replace-regexp)
(global-set-key (kbd "C-M-%") #'replace-regexp)
;; ____________________________________________________________________________
;;; RECENT FILES
(require 'recentf)
;; Turn on recent file mode to visit recently edited files
(recentf-mode 1)
(setq recentf-max-menu-items 10
recentf-max-saved-items 100)
;; Ignore some recently visited files, eg. to prevent them from showing up
;; amongst recent files after package upgrades
(add-to-list 'recentf-exclude
(expand-file-name (concat user-emacs-directory "elpa/")))
;; Use 'completing-read' to choose between recent files
(defun eon-find-recentf ()
"Find recent file via completion in the minibuffer."
(interactive)
(find-file (completing-read "Find recent file: " recentf-list nil t) nil))
(global-set-key (kbd "C-x f") #'eon-find-recentf)
;; ____________________________________________________________________________
;;; DIRED
;; <https://www.gnu.org/software/emacs/manual/html_mono/emacs.html#Dired>
(require 'dired)
;; The `dired' keybinding is "C-x d". This new keybinding is in accordance
;; with "C-x C-f" for visiting files
(global-set-key (kbd "C-x C-d") #'dired)
;; Switch to wdired-mode and edit directory content like a text buffer
(define-key dired-mode-map (kbd "e") #'dired-toggle-read-only)
;; Don't accumulate useless Dired buffers
(defun eon-dired-single-buffer (s)
"When S is non-nil, prevent superfluous Dired buffers from accumulating.
Kills the current Dired buffer when entering a new directory"
(when (not (null s))
(cond
((version< emacs-version "28.1")
(progn (put 'dired-find-alternate-file 'disabled nil)
(define-key dired-mode-map (kbd "RET") #'dired-find-alternate-file)
(define-key dired-mode-map (kbd "^") (lambda ()
(interactive)
(find-alternate-file "..")))))
(t (setq dired-kill-when-opening-new-dired-buffer t)))))
(eon-dired-single-buffer t) ; set the default
;; Use the system trash when deleting files
(defun eon-trash-on ()
"Delete files by moving to the system trash."
(interactive)
(setq delete-by-moving-to-trash t)
(setq dired-recursive-deletes 'always) ; don't ask when directory not empty
(message "Trash on: Deleted files will go to system trash."))
(defun eon-trash-off ()
"Delete files immediately."
(interactive)
(setq delete-by-moving-to-trash nil)
(setq dired-recursive-deletes 'top) ; ask when directory not empty
(message "Trash off: Files will be deleted immediately!"))
(eon-trash-on) ; set the default
;; Auto refresh dired when contents of a directory change
(require 'autorevert)
(setq auto-revert-verbose nil)
(add-hook 'dired-mode-hook #'auto-revert-mode)
;; Directory listings
(add-hook 'dired-mode-hook
(lambda ()
;; Hide details in file listings? Toggle via "S-("
(dired-hide-details-mode 1)
;; Highlight current line?
(hl-line-mode 1)))
;; Listing columns; Switch arguments with "C-u s" e.g. hide backups with -B
(setq-default dired-listing-switches "-lhFA -v --group-directories-first")
;; Copying files/directories
(setq dired-recursive-copies 'always)
;; Create directories if they don't exist
(setq dired-create-destination-dirs 'ask)
;; Mimic dual-pane file managers?
(setq dired-dwim-target t)
;; Images
(require 'image-dired)
(setq image-dired-thumb-margin 1
image-dired-thumb-relief 0