-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.el
1895 lines (1485 loc) · 64.8 KB
/
init.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
;;; ~thblt/.emacs.d/init.el -*- lexical-binding: t; -*-
(setq gc-cons-percentage 100
gc-cons-threshold most-positive-fixnum
garbage-collection-messages t)
(message "┌───────────────────────────┬─────────────────────┬───────────────────────────────────────────────────┒")
(message "│ oooooo oooo o8o │ M I C R O S O F T │ oooo oooooooooooo ┃")
(message "│ ‘888. .8’ ‘\"’ └─────────────────────┘ ‘888 ‘888’ ‘8 ┃")
(message "│ ‘888. .8’ oooo .oooo.o oooo oooo .oooo. 888 888 88 88 ┃")
(message "│ ‘888. .8’ ‘888 d88( \"8 ‘888 ‘888 ‘P )88b 888 888oooo8 88 88 ┃")
(message "│ ‘888.8’ 888 ‘\"Y88b. 888 888 .oP\"888 888 888 \" 8888888888 888888888 ┃")
(message "│ ‘888’ 888 o. )88b 888 888 d8( 888 888 888 o 88 88 ┃")
(message "│ ‘8’ o888o 8\"\"888P’ ‘V88V\"V8P’ ‘Y888\"\"8o o888o o888ooooood8 88 88 ┃")
(message "┕━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛")
(message "Version %s" emacs-version)
;;; Introduction
;; This chapter deals with the general use of Emacs, and is limited to
;; general settings and sane defaults. It's a bit messy, since it's
;; mostly made up of all the bits that don't fit anywhere else.
;;;; Package managers
;;;;; package.el
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/"))
(package-initialize 'no-activate)
;;;;; Borg
;; Borg comes second, because it comes first. The second initialized
;; manager will be the first in load-path.
(eval-and-compile
(add-to-list 'load-path (expand-file-name "lib/borg" user-emacs-directory))
(require 'borg)
(borg-initialize))
;; So I can play with .el files in lib/ without creating a full
;; package before I start.
(add-to-list 'load-path borg-drones-directory)
;;;;; Paths
(require 'no-littering)
;;;; Settings
(setq user-full-name "Thibault Polge"
user-mail-address "[email protected]"
;; LSP wants this.
read-process-output-max (* 1024 1024)
;; Less noise
native-comp-async-report-warnings-errors nil
make-backup-files nil
;; Lockfiles are so 1990
create-lockfiles nil
;; Don't lose the contents of system clipboard when killing from Emacs:
save-interprogram-paste-before-kill t
custom-file (make-temp-file "emacs-custom")
inhibit-compacting-font-caches (eq system-type 'windows-nt) ; This prevents slowdown when using strange characters.
;; Use default browser from the system. Using =setsid xdg-open=
;; prevents Emacs from killing xdg-open before it actually opened
;; anything. See
;; [[https://askubuntu.com/questions/646631/emacs-doesnot-work-with-xdg-open][here]].
browse-url-browser-function (if (eq system-type 'windows-nt) 'browse-url-default-browser 'browse-url-generic)
;; browse-url-generic-program "setsid"
browse-url-generic-program "firefox")
(load custom-file t)
(setq-default major-mode 'text-mode)
(defun thblt/disable-key-translations (&optional frame)
"Disable key translations used for terminal compatibility, selecting FRAME.
Notice the variables this sets are terminal-local, not frame
local."
(with-selected-frame (or frame (selected-frame))
(when (display-graphic-p)
(define-key input-decode-map [?\C-m] [C-m])
(define-key input-decode-map [?\C-i] [C-i]))))
;; Don't lose usage of C-* in X
(add-to-list 'after-make-frame-functions 'thblt/disable-key-translations)
(unless (daemonp)
(thblt/disable-key-translations))
;; I do that by accident, so let's not
(define-key global-map [remap suspend-frame] 'ignore) ; nil doesn't unset.
(define-key global-map [remap suspend-emacs] 'ignore)
;;; User interface
;;;; Settings and general configuration
(setq-default
enable-recursive-minibuffers t
max-mini-window-height .6
inhibit-startup-screen t
use-dialog-box nil
vc-follow-symlinks t
truncate-lines t
disabled-command-function nil)
(minibuffer-depth-indicate-mode)
(pixel-scroll-mode)
;; Don't warn when opening large files. The warning *could* be
;; useful, but it almost only happens with PDF files, which don't
;; cause the kind of trouble this warning is meant to avoid.
(setq large-file-warning-threshold nil)
;; Line numbers
(setq-default display-line-numbers-type 'relative
display-line-numbers-major-tick 10
display-line-numbers-minor-tick 5
display-line-numbers-current-absolute t)
;; Line numbers in mode-line
(column-number-mode)
;; Cursor configuration
(setq-default cursor-type 'box)
(blink-cursor-mode)
;; Never use the "safe" ~yes-or-no~ function:
(fset 'yes-or-no-p 'y-or-n-p)
;; Don't show the menu bar, unless this is MacOS. Never show toolbar
;; or scrollbars.
(unless (string= 'system-type 'darwin) (menu-bar-mode -1))
(when (fboundp 'tool-bar-mode)
(tool-bar-mode -1)
(scroll-bar-mode -1))
;; Mouse wheel scrolling makes big jumps by default, let's make it smoother.
(setq mouse-wheel-scroll-amount '(1 ((shift) . 1)) ;; one line at a time
mouse-wheel-progressive-speed nil ;; don't accelerate scrolling
mouse-wheel-follow-mouse 't ;; scroll window under mouse
scroll-step 1)
;; Rebind =C-x k= to kill the /current/ buffer.
(global-set-key (kbd "C-x k") (lambda () (interactive) (kill-buffer (current-buffer))))
;; And force leave the minibuffer with =C-M-]=
(global-set-key (kbd "C-M-à") 'abort-recursive-edit)
;; Rebind =C-x C-b= to =ibuffer= instead of =list-buffers=:
(global-set-key (kbd "C-x C-b") 'ibuffer)
(global-set-key (kbd "C-c b") 'switch-to-minibuffer)
;; No right fringe
(when (fboundp 'fringe-mode) (fringe-mode '(nil . 0)))
;; Better truncation indicator
(unless standard-display-table
(setq standard-display-table (make-display-table)))
(set-display-table-slot standard-display-table 'truncation ?▶)
;; A small function to identify the face at point. Nice to have when
;; writing themes, and faster than C-u C-x =
(defun what-face (pos)
"Show face at POS, defaulting to point."
(interactive "d")
(let ((face (or (get-char-property (point) 'read-face-name)
(get-char-property (point) 'face))))
(if face (message "Face: %s" face) (message "No face at %d" pos))))
;;;; Fonts and themes
;; Configure the default font:
(add-to-list 'default-frame-alist '(font . "Iosevka"))
(set-face-attribute 'default nil
:height (pcase (system-name)
("dru" 130)
("maladict" 100)
("margolotta" 110)))
(set-face-attribute 'fixed-pitch nil
:family "Iosevka")
(when (and (eq system-type 'gnu/linux)
(string= (system-name) "margolotta"))
;; Fixes flickering on Wayland/NVidia
(modify-all-frames-parameters '((inhibit-double-buffering . t))))
(add-to-list 'custom-theme-load-path borg-drones-directory)
(setq x-underline-at-descent-line t)
(defun thblt/disable-all-themes ()
"Disable all themes."
(interactive)
(mapc 'disable-theme custom-enabled-themes))
(defun thblt/replace-theme (theme)
"Like `load-theme', but also disable all other themes."
(interactive (list (intern (completing-read "Theme: " (custom-available-themes)))))
(thblt/disable-all-themes)
(load-theme theme t))
(setq ;; Leuven
leuven-dark-scale-org-agenda-structure nil
leuven-scale-org-agenda-structure nil
leuven-scale-outline-headlines nil
leuven-dark-scale-outline-headlines nil
;; Modus
modus-themes-fringes 'subtle
modus-themes-headings '((t . (raindow background)))
;; Doom
doom-one-brighter-comments t)
(defun thblt/doom-one-theme () "Activate doom-one theme." (interactive) (thblt/replace-theme 'doom-one))
(defun thblt/doom-one-light-theme () "Activate doom-one-light theme." (interactive) (thblt/replace-theme 'doom-one-light))
(defun thblt/doom-zenburn-theme () "Activate Doom Zenburn theme." (interactive) (thblt/replace-theme 'doom-zenburn))
(defun thblt/modus-operandi-theme () "Activate modus-operandi theme." (interactive) (thblt/replace-theme 'modus-operandi))
(defun thblt/modus-vivendi-theme () "Activate modus-vivendi theme." (interactive) (thblt/replace-theme 'modus-vivendi))
;; Theme is loaded at the very end of this file.
;;;; UI Utilities
;;;;; General launcher
(defvar thblt/launcher-map (make-sparse-keymap)
"Keymap for the launchers.")
(define-key global-map (kbd "C-è") thblt/launcher-map)
;;;;; Hydra
(require 'hydra)
(setq hydra-hint-display-type 'lv)
;;;;; Vertico + Orderless + Embark
(require 'vertico)
(vertico-mode)
(marginalia-mode)
(require 'orderless)
(setq completion-styles '(orderless)
completion-category-defaults nil
completion-category-overrides '((file (styles basic partial-completion))))
(require 'embark-consult)
(define-key global-map (kbd "M-é") 'embark-act)
(define-key vertico-map (kbd "C-<return>") 'embark-act)
;; This ↓ from Vertico doc. Make Vertico work in tab-completion as
;; well (ERC, M-:…)
(setq completion-in-region-function
(lambda (&rest args)
(apply (if vertico-mode
#'consult-completion-in-region
#'completion--in-region)
args)))
;;;;;; Embark + magit
(defun thblt/embark-magit-status (file)
"Run `magit-status` on repo containing the embark target."
(interactive "GFile: ")
(magit-status (locate-dominating-file file ".git")))
(define-key embark-file-map (kbd "v v") 'thblt/embark-magit-status)
;;;;; Shackle
(require 'magit)
(require 'shackle)
(require 'sway)
(setq shackle-rules
`(("*Help*" :align t :select t)
("*Error*" :align t :poput t :select t :frame nil)
("*Backtrace*" :align t :poput t :select t :frame nil)
;; ** Magit **
(magit-status-mode :same t)
((:custom ;; Magit diffs
,(lambda (buffer)
(with-current-buffer buffer
(and
(eq major-mode 'magit-diff-mode)
magit-display-buffer-noselect))))
:select nil :frame t :dedicate t)
(magit-status-mode :same t)
((:custom ;; Magit log select
,(lambda (buffer)
(with-current-buffer buffer
(eq major-mode 'magit-log-select-mode)
magit-display-buffer-noselect)))
:select t :frame t :dedicate t)
((:custom ;; Support noselect
,(lambda (buffer)
(with-current-buffer buffer
(bound-and-true-p magit-display-buffer-noselect))))
:select nil :frame nil :dedicate t)
(magit-log-mode :same t)
(magit-submodule-list-mode :same t)
(magit-revision-mode :same t)
(magit-process-mode :frame nil)
("COMMIT_EDITMSG" :popup t :select t)
("^magit.*$'" :regexp t :frame nil)
(" *transient*" :frame nil :popup t :select nil :align below)
;; ** Forge **.
(forge-post-mode :dedicate t :frame t)
;; ** Org **
("*Org PDF LaTeX Output*" :select nil)
("*Org Preview LaTeX Output*" :select nil)
("*Org Export Dispatcher*" :frame nil)
("*Org Select*" :frame nil)
("*Org Links*" :frame nil :select nil)
((:custom (lambda (buffer) (with-current-buffer buffer
(bound-and-true-p
org-src-mode))) :frame t :dedicate t))
;; ** Common Emacs UI elements **
("*Completions*" :frame nil :popup t :select t) ; Magit helper popups
;; ** GPG prompts (for transparently editing GPG files)
("*Keys*" :frame nil :popup t :select t)
;; ** Dired **
(" *Deletions*" :frame nil :popup t :select t) ; Dired deletion info
(" *Marked Files*" :frame nil :popup t :select t)
;; ** Embark **
;; Warning: `:frame t' on the rule below breaks embark-act.
(" *Embark Actions*" :frame nil :select nil)
;; ** Sunrise commander **
(sunrise-mode :custom (lambda (&rest _)))
;; ** Proced **
("*Proced*" :same t)
(" *Marked Processes*" :frame nil :popup t :select t)
;; ** Byte-compiler
("*Compile-Log*" :frame nil :popup t :select t)
;; ** Local variables warning **
("*Local Variables*" :same t :frame nil :popup t :select t)
;; ** Errors **
("*Backtrace*" :frame t :select t :dedicate t)
;; ** M-x report-emacs-bug
("*Bug Help*" :frame nil :align below)
;; ** Misc **
(" *undo-tree*" :frame nil)
("*Register Preview*" :frame nil :noselect t)
(flycheck-error-list-mode :select t)
((compilation-mode "\\`\\*firestarter\\*\\'"
"\\`\\*magit-diff: .*?\\'") :regexp t :noselect t :frame t)
((inferior-scheme-mode "*shell*" "*eshell*") :popup t))
shackle-default-rule '(:frame t)
shackle-default-size 0.4
shackle-inhibit-window-quit-on-same-windows t
shackle-display-buffer-frame-function 'sway-shackle-display-buffer-frame)
(setq frame-title-format '("%b — GNU Emacs [" (:eval (frame-parameter (selected-frame) 'window-id)) "]"))
(shackle-mode)
(sway-socket-tracker-mode)
(sway-undertaker-mode)
(sway-x-focus-through-sway-mode)
;;;;; Consult
(define-key global-map [remap switch-to-buffer] 'consult-buffer)
(define-key global-map [remap goto-line] 'consult-goto-line)
;;;;; project.el
(require 'project)
(setq project-vc-merge-submodules nil)
(require 'rg) ; Needed. This evals without it, but doesn't work.
(rg-define-search thblt/project-rg :query ask :format regexp :files "everything" :case-fold-search smart :dir
(if (project-current) (project-root (project-current))
(project-prompt-project-dir))
:confirm prefix :flags
("--hidden -g !.git"))
(defun thblt/project-magit (directory)
(interactive (list (if (project-current) (project-root (project-current))
(project-prompt-project-dir))))
(magit-status directory))
(defun thblt/project-vterm (default-directory)
(interactive (list (if (project-current) (project-root (project-current))
(project-prompt-project-dir))))
(vterm))
;; Add our commands to the project picker
(add-to-list 'project-switch-commands '(thblt/project-rg "Ripgrep"))
(add-to-list 'project-switch-commands '(thblt/project-magit "Magit"))
;; (add-to-list 'project-switch-commands '(thblt/project-vterm "Vterm"))
;; Delete commands we don't use.
(cl-delete-if
(lambda (x) (member (car x)
'(project-find-regexp project-vc-dir project-eshell)))
project-switch-commands)
(define-key project-prefix-map (kbd "g") 'thblt/project-rg)
(define-key project-prefix-map (kbd "v") 'thblt/project-magit)
;; (define-key project-prefix-map (kbd "T") 'thblt/project-vterm)
(defun thblt/project-discover-projects ()
(interactive)
(mapc
(lambda (p)
(let ((root (car p))
(recursive (cdr p)))
(message "Looking for projects into %s%s…"
root
(if recursive ", recursively" ""))
(project-remember-projects-under root recursive)))
'(("~/.emacs.d/" . t)
("~/Documents/" . t)
("~/.dotfiles/" . nil)
("/etc/nixos/" . nil)
("~/.dotfiles.private/" . nil))))
(defun thblt/magit-repos-from-project (&rest _)
"Overwrite `magit-repository-directories' with `project-known-projects'."
(setq magit-repository-directories
(mapcar (lambda (p) (cons p 0))
(project-known-project-roots))))
(advice-add 'magit-list-repositories :before 'thblt/magit-repos-from-project)
;;;; BÉPO adjustments
;; Unshifted digit argument
(defmacro thblt/digit-argument-with-value (char)
"Simulate `digit-argument' as if it was called by pressing CHAR.
This can be used to update the digit argument from arbitrary keys."
`(lambda () (interactive)
(prefix-command-preserve-state)
(let ((last-command-event ,char))
(call-interactively 'digit-argument))))
(define-key universal-argument-map (kbd "\"") (thblt/digit-argument-with-value ?1))
(define-key universal-argument-map (kbd "«") (thblt/digit-argument-with-value ?2))
(define-key universal-argument-map (kbd "»") (thblt/digit-argument-with-value ?3))
(define-key universal-argument-map (kbd "(") (thblt/digit-argument-with-value ?4))
(define-key universal-argument-map (kbd ")") (thblt/digit-argument-with-value ?5))
(define-key universal-argument-map (kbd "@") (thblt/digit-argument-with-value ?6))
(define-key universal-argument-map (kbd "+") (thblt/digit-argument-with-value ?7))
(define-key universal-argument-map (kbd "-") (thblt/digit-argument-with-value ?8))
(define-key universal-argument-map (kbd "/") (thblt/digit-argument-with-value ?9))
(define-key universal-argument-map (kbd "*") (thblt/digit-argument-with-value ?0))
;; Some key translations
;; Swap é and w
(define-key key-translation-map (kbd "M-é") (kbd "M-w"))
(define-key key-translation-map (kbd "M-w") (kbd "M-é"))
(define-key key-translation-map (kbd "C-é") (kbd "C-w"))
(define-key key-translation-map (kbd "C-w") (kbd "C-é"))
(define-key key-translation-map (kbd "C-M-é") (kbd "C-M-w"))
(define-key key-translation-map (kbd "C-M-w") (kbd "C-M-é"))
;; Read êÊ as <> (key right of left shift)
(define-key key-translation-map (kbd "M-ê") (kbd "M-<"))
(define-key key-translation-map (kbd "C-ê") (kbd "C-<"))
(define-key key-translation-map (kbd "C-M-ê") (kbd "C-M-<"))
(define-key key-translation-map (kbd "M-Ê") (kbd "M->"))
(define-key key-translation-map (kbd "C-Ê") (kbd "C->"))
(define-key key-translation-map (kbd "C-M-Ê") (kbd "C-M->"))
(defmacro thblt/self-insert-this (char)
"Run `self-insert' as if it was called by pressing CHAR."
`(lambda () (interactive)
(let ((last-command-event ,char))
(call-interactively 'self-insert-command))))
(defvar thblt/normalize-spaces-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd " ") (thblt/self-insert-this ? ))
(define-key map (kbd " ") (thblt/self-insert-this ?_))
map))
;; Fuck BÉPO's shift-space in prog-mode
(define-minor-mode thblt/normalize-spaces-mode
"Map various non-breaking spaces to a regular space."
:lighter " spaces"
:keymap thblt/normalize-spaces-mode-map)
(add-hook 'prog-mode-hook 'thblt/normalize-spaces-mode)
(diminish 'thblt/normalize-spaces-mode)
;;;; The editor appearance hydra
(require 'hydra)
(require 'visual-fill-column)
(eval-and-compile
(defmacro thblt/hydra-indicator (desc active)
"Return DESC with a state indicator determined by ACTIVE.))
If at runtime ACTIVE is an unbound symbol it is interpreted as
nil; otherwise it's evaluated normally."
`(format "[%s] %s" (if ,(if (symbolp active)
`(bound-and-true-p ,active)
active)
(propertize "█" 'face 'bold)
(propertize " " 'face 'shadow))
,desc)))
(defhydra hydra-editor-appearance ()
("b" text-scale-decrease "Size -" :column "Font and theme")
("é" thblt/text-scale-reset (thblt/hydra-indicator "Default size"
(not (bound-and-true-p text-scale-mode))))
("É" thblt/font-size-set "Set default")
("p" text-scale-increase "Size +")
("td" thblt/doom-one-theme (thblt/hydra-indicator "Doom one"
(member 'doom-one custom-enabled-themes)))
("tl" thblt/doom-one-light-theme (thblt/hydra-indicator "D.one light"
(member 'doom-one-light custom-enabled-themes)))
("tz" thblt/doom-zenburn-theme (thblt/hydra-indicator "Doom Zenburn"
(member 'doom-zenburn custom-enabled-themes)))
;; ("V" variable-pitch-mode (thblt/hydra-indicator "Var. pitch" buffer-face-mode))
;; ("tl" thblt/light-theme "Light theme")
;; ("tL" thblt/light-hc-theme "Light (hc) theme")
;; ("td" thblt/dark-theme "Dark theme")
;; ("tD" thblt/dark-hc-theme "Dark (hc) theme")
;; ("to" thblt/modus-operandi-theme "Modus Operandi")
;; ("tv" thblt/modus-vivendi-theme "Modus Vivendi")
("f" thblt/visual-fill-column-toggle-mode (thblt/hydra-indicator "Visual fill" visual-fill-column-mode) :column "Appearance")
;; @FIXME This breaks if `visual-fill-column' hasn't been loaded yet.
("c" thblt/visual-fill-column-toggle-centering (thblt/hydra-indicator "Centering" visual-fill-column-center-text))
("g" thblt/visual-fill-column-width-decrease "Width -")
("h" thblt/visual-fill-column-width-increase "Width +")
("l" visual-line-mode (thblt/hydra-indicator "Line wrap" visual-line-mode))
("-" toggle-word-wrap (thblt/hydra-indicator "Word wrap" word-wrap))
("L" display-line-numbers-mode (thblt/hydra-indicator "Line numbers" display-line-numbers-mode))
("a" auto-fill-mode (thblt/hydra-indicator "Auto fill" auto-fill-function) :column "Magic/more magic")
("A" refill-mode (thblt/hydra-indicator "Auto refill" refill-mode))
("I" aggressive-indent-mode (thblt/hydra-indicator "Aggressive indent" aggressive-indent-mode))
("de" toggle-debug-on-error (thblt/hydra-indicator "Debug on error" debug-on-error))
("dq" toggle-debug-on-quit (thblt/hydra-indicator "Debug on quit" debug-on-quit))
("r" rainbow-mode (thblt/hydra-indicator "Rainbow" rainbow-mode))
("W" superword-mode (thblt/hydra-indicator "super-word" superword-mode))
("w" subword-mode (thblt/hydra-indicator "SubWord" subword-mode))
("!" flycheck-mode (thblt/hydra-indicator "Code" flycheck-mode) :column "Utility")
("?" flyspell-mode (thblt/hydra-indicator "Spell" flyspell-mode))
("F" thblt/ispell-use-french (thblt/hydra-indicator "Français" (string= (bound-and-true-p ispell-local-dictionary) "french")))
("E" thblt/ispell-use-english (thblt/hydra-indicator "English" (string= (bound-and-true-p ispell-local-dictionary) "english")))
("R" auto-revert-mode (thblt/hydra-indicator "Auto-revert" auto-revert-mode)))
(define-key global-map (kbd "C-c l") 'hydra-editor-appearance/body)
(defun thblt/visual-fill-column-reset (&optional activate)
"Turn visual-fill-column off and on again.
If visual-fill-column isn't enabled, activate it if ACTIVATE,
otherwise do nothing.
For use by `hydra-editor-appearance/body'."
(interactive)
(when (or
activate
(bound-and-true-p visual-fill-column-mode))
(visual-fill-column-mode -1)
(visual-fill-column-mode t)))
(defun thblt/ispell-use-french ()
"For use by `hydra-editor-appearance/body'."
(interactive)
(ispell-change-dictionary "french")
(when flyspell-mode (flyspell-buffer)))
(defun thblt/ispell-use-english ()
"For use by `hydra-editor-appearance/body'."
(interactive)
(ispell-change-dictionary "english")
(when flyspell-mode (flyspell-buffer)))
(defun thblt/text-scale-reset ()
"For use by `hydra-editor-appearance/body'."
(interactive)
(text-scale-set 0))
(defun thblt/font-size-set (size)
"For use by `hydra-editor-appearance/body'."
(interactive
(list
(string-to-number (completing-read "Base-size" '("80" "85" "90" "95" "100" "105" "110" "115" "120" "130" "140") nil nil))))
(set-face-attribute 'default nil
:height size)
(thblt/text-scale-reset))
(defun thblt/visual-fill-column-toggle-mode ()
"For use by `hydra-editor-appearance/body'."
(interactive)
(setq visual-fill-column-center-text nil)
(call-interactively 'visual-fill-column-mode))
(defun thblt/visual-fill-column-toggle-centering ()
"For use by `hydra-editor-appearance/body'."
(interactive)
(if
(setq visual-fill-column-center-text (not (bound-and-true-p visual-fill-column-center-text)))
(thblt/visual-fill-column-reset visual-fill-column-center-text)
(visual-fill-column-mode 0)))
(defun thblt/visual-fill-column-width-adjust (delta)
"Adjust visual fill column by DELTA."
(interactive)
(setq visual-fill-column-width
(+ delta (or visual-fill-column-width fill-column)))
(thblt/visual-fill-column-reset))
(defun thblt/visual-fill-column-width-increase ()
"For use by `hydra-editor-appearance/body'."
(interactive)
(thblt/visual-fill-column-width-adjust 5))
(defun thblt/visual-fill-column-width-decrease ()
"For use by `hydra-editor-appearance/body'."
(interactive)
(thblt/visual-fill-column-width-adjust -5))
;;; Editing text
;; This chapter deals with /general/ text editing. The next two configure
;; prose and code editing, respectively.
(setq shift-select-mode nil)
(editorconfig-mode)
(diminish 'editorconfig-mode "")
(define-key global-map (kbd "M-<SPC>") 'cycle-spacing)
(defun thblt/imenu-or-outline (arg)
"With no arg, execute `consult-imenu'. With an argument,
`consult-outline'. If the function to execute isn't defined,
execute `imenu' instead."
(interactive "P")
(cond ((and (not arg) (fboundp 'consult-imenu))
(consult-imenu))
((fboundp 'consult-outline)
(consult-outline))
(t (call-interactively 'imenu))))
(define-key global-map (kbd "M-i") 'thblt/imenu-or-outline)
;;;; Spell checking
(require 'flyspell)
(require 'ispell)
(setq ispell-program-name "aspell"
ispell-silently-savep t)
;; Enable Flyspell:
(add-hook 'text-mode-hook (lambda () (flyspell-mode t)))
(diminish 'flyspell-mode)
;; Correct words using Ivy instead of default method:
(with-eval-after-load 'flyspell
(require 'flyspell-correct)
(define-key flyspell-mode-map (kbd "M-$") 'flyspell-auto-correct-previous-word)
(define-key flyspell-mode-map (kbd "C-,") 'flyspell-correct-previous))
;; Pulse
(advice-add 'flyspell-auto-correct-previous-word
:after (defun thblt/flyspell-auto-correct-pulse (&rest _)
(pulse-momentary-highlight-region
(car flyspell-auto-correct-region)
(+ (car flyspell-auto-correct-region)
(cdr flyspell-auto-correct-region)))))
;;;; Moving around
(define-key global-map (kbd "M-«") 'backward-paragraph)
(define-key global-map (kbd "M-»") 'forward-paragraph)
(define-key global-map (kbd "C-M-«") 'beginning-of-defun)
(define-key global-map (kbd "C-M-»") 'end-of-defun)
;;;;; beginend
(require 'beginend)
(beginend-global-mode)
(mapc (lambda (m) (diminish (cdr m)))
beginend-modes)
(diminish 'beginend-global-mode)
;;;;; mwim
(require 'haskell-interactive-mode)
(define-key global-map [remap move-beginning-of-line] 'mwim-beginning)
(define-key global-map [remap move-end-of-line] 'mwim-end)
(define-key visual-line-mode-map [remap move-beginning-of-line] 'mwim-beginning)
(define-key visual-line-mode-map [remap move-end-of-line] 'mwim-end)
;; but…
(with-eval-after-load 'haskell-interactive-mode
(define-key haskell-interactive-mode-map (kbd "C-a") 'haskell-interactive-mode-bol)
(diminish 'interactive-haskell-mode " λ"))
;;;;; pulse (don't get lost)
(advice-add 'recenter-top-bottom :after (lambda (_) (pulse-momentary-highlight-one-line (point))))
(advice-add 'scroll-down-command :after (lambda (_) (pulse-momentary-highlight-one-line (point))))
(advice-add 'scroll-up-command :after (lambda (_) (pulse-momentary-highlight-one-line (point))))
(advice-add 'yank :after (lambda (_) (pulse-momentary-highlight-region (point) (mark))))
(advice-add 'yank-pop :after (lambda (_) (pulse-momentary-highlight-region (point) (mark))))
;;;; isearch, replace, query-replace
(define-key global-map [remap query-replace] 'vr/query-replace)
(define-key global-map (kbd "C-c r") 'vr/replace)
;; Leave the cursor at the beginning of an isearch match
;; From: https://endlessparentheses.com/leave-the-cursor-at-start-of-match-after-isearch.html
(add-hook 'isearch-mode-end-hook
#'thblt/goto-match-beginning)
(defun thblt/goto-match-beginning ()
"Go to the start of current isearch match.
Use in `isearch-mode-end-hook'."
(when (and isearch-forward
(number-or-marker-p isearch-other-end)
(not mark-active)
(not isearch-mode-end-hook-quit))
(goto-char isearch-other-end)))
;;;; Minor modes
;;;;; Auto-revert-mode
(setq auto-revert-avoid-polling t
auto-revert-interval 1)
(with-eval-after-load 'autorevert
(diminish 'auto-revert-mode " 🔃"))
;;;;; Recentf
(recentf-mode)
;;;;; Smartparens
(require 'smartparens-config) ;; Load default config
(smartparens-global-mode)
(show-smartparens-global-mode)
(define-key smartparens-mode-map (kbd "C-M-f") 'sp-forward-sexp)
(define-key smartparens-mode-map (kbd "C-M-b") 'sp-backward-sexp)
(define-key smartparens-mode-map (kbd "C-M-d") 'sp-down-sexp)
(define-key smartparens-mode-map (kbd "C-M-a") 'sp-backward-down-sexp)
(define-key smartparens-mode-map (kbd "C-S-d") 'sp-beginning-of-sexp)
(define-key smartparens-mode-map (kbd "C-S-a") 'sp-end-of-sexp)
(define-key smartparens-mode-map (kbd "C-M-e") 'sp-up-sexp)
(define-key smartparens-mode-map (kbd "C-M-u") 'sp-backward-up-sexp)
(define-key smartparens-mode-map (kbd "C-M-t") 'sp-transpose-sexp)
(define-key smartparens-mode-map (kbd "C-M-n") 'sp-next-sexp)
(define-key smartparens-mode-map (kbd "C-M-p") 'sp-previous-sexp)
(define-key smartparens-mode-map (kbd "C-M-k") 'sp-kill-sexp)
(define-key smartparens-mode-map (kbd "C-M-w") 'sp-copy-sexp)
(define-key smartparens-mode-map (kbd "M-<delete>") 'sp-unwrap-sexp)
(define-key smartparens-mode-map (kbd "M-<backspace>") 'sp-backward-unwrap-sexp)
(define-key smartparens-mode-map (kbd "C-<right>") 'sp-forward-slurp-sexp)
(define-key smartparens-mode-map (kbd "C-<left>") 'sp-forward-barf-sexp)
(define-key smartparens-mode-map (kbd "C-M-<left>") 'sp-backward-slurp-sexp)
(define-key smartparens-mode-map (kbd "C-M-<right>") 'sp-backward-barf-sexp)
(define-key smartparens-mode-map (kbd "M-D") 'sp-splice-sexp)
(define-key smartparens-mode-map (kbd "C-M-<delete>") 'sp-splice-sexp-killing-forward)
(define-key smartparens-mode-map (kbd "C-M-<backspace>") 'sp-splice-sexp-killing-backward)
(define-key smartparens-mode-map (kbd "C-S-<backspace>") 'sp-splice-sexp-killing-around)
(define-key smartparens-mode-map (kbd "C-]") 'sp-select-next-thing-exchange)
(define-key smartparens-mode-map (kbd "C-<left_bracket>") 'sp-select-previous-thing)
(define-key smartparens-mode-map (kbd "C-M-]") 'sp-select-next-thing)
(define-key smartparens-mode-map (kbd "M-F") 'sp-forward-symbol)
(define-key smartparens-mode-map (kbd "M-B") 'sp-backward-symbol)
(diminish 'smartparens-mode)
;;;;; Vundo
(require 'vundo)
(define-key global-map (kbd "C-x u") 'vundo)
(with-eval-after-load 'vundo
(setq vundo-glyph-alist vundo-unicode-symbols))
;;;;; Yasnippet
(setq yas-snippet-dirs
(list (no-littering-expand-etc-file-name "snippets")
(borg-worktree "yasnippet-snippets/snippets")))
;; @FIXME Takes almost a second!
(yas-global-mode)
(diminish 'yas-minor-mode)
(defun thblt/split-path (path)
(let ((base t)
(list))
(while (< 1 (length path))
(setq base (file-name-nondirectory path)
path (substring (file-name-directory path) 0 -1))
(push base list))
list))
;;;;;; Snippet helpers
(defun thblt/guess-module-name-from-path (path &optional keep-extension)
"Guess a Haskell-ish module name.
This works by concatenating PATH components that don't start with
a lowercase letter and dropping the extension, unless KEEP-EXTENSION."
(let ((module)
(case-fold-search nil))
(mapc (lambda (item)
(if (string-match-p (rx bos lower) item)
(setq module nil)
(push item module)))
(thblt/split-path path))
(unless keep-extension
(setf (car module) (file-name-base (car module))))
(mapconcat 'identity (reverse module) ".")))
(defun thblt/guess-module-name (&optional buffer)
(with-current-buffer (or buffer (current-buffer))
(when buffer-file-name
(thblt/guess-module-name-from-path buffer-file-name))))
;;;; Misc
(defmacro with-maybe-region (name region-fun default-fun)
"Create a command NAME wrapping REGION-FUN and DEFAULT-FUN.
If the region is active and usable, call REGION-FUN
interactively, DEFAULT-FUN otherwise ."
`(defun ,name ()
,(format "Run `%s' if region is active, `%s' otherwise." region-fun default-fun)
(interactive)
(call-interactively
(if (use-region-p) ',region-fun ',default-fun))))
(define-key global-map [remap upcase-region] 'upcase-dwim)
(define-key global-map [remap downcase-region] 'downcase-dwim)
(define-key global-map [remap capitalize-word] 'capitalize-dwim)
;; (keymap-unset global-map "M-c")
(keymap-unset global-map "M-l")
(keymap-unset global-map "M-u")
(define-key global-map [remap kill-ring-save]
(with-maybe-region thblt/kill-ring-save kill-ring-save thblt/line-to-kill-ring))
(define-key global-map [remap kill-region]
(with-maybe-region thblt/kill-region kill-region kill-whole-line))
(defun thblt/line-to-kill-ring ()
"Copy the active line to kill-ring"
(interactive)
(let ((beg))
(save-excursion
(beginning-of-line)
(setq beg (point))
(end-of-line)
(kill-ring-save beg (point)))))
;;;;; Bindings
(global-set-key (kbd "C-S-k") 'kill-whole-line)
(global-set-key (kbd "C-S-y") 'copy-line)
(global-set-key (kbd "C-h") 'delete-backward-char)
(define-key global-map (kbd "M-Q") 'unfill-paragraph)
;;;;; Autosave when losing focus
(super-save-mode +1)
(add-to-list 'super-save-predicates
(lambda () (not (member major-mode '(notmuch-message-mode message-mode)))))
(diminish 'super-save-mode)
;;;;; What to do when saving
;; Delete trailing whitespace
(add-hook 'before-save-hook 'delete-trailing-whitespace)
;; Make scripts executable
(add-hook 'after-save-hook 'executable-make-buffer-file-executable-if-script-p)
;;; Writing prose
;;;; Common settings and minor modes
;;;; abbrev
(setq save-abbrevs 'silently)
(add-hook 'text-mode-hook (lambda () (abbrev-mode t)))
(diminish 'abbrev-mode)
;;;; Wordwrap/visual line/visual-fill-column
(diminish 'visual-line-mode)
;;;; Major modes
;;;; AucTex
(require 'latex)
(require 'tex-site)
(load "auctex.el" nil t t)
(load "preview-latex.el" nil t t)
(add-hook 'LaTeX-mode-hook
(lambda ()
(visual-line-mode t)
(TeX-fold-mode t)
(aggressive-indent-mode)))
(setq-default TeX-save-query nil ; Autosave
TeX-parse-self t
TeX-engine 'xetex
TeX-source-correlate-mode t)
(sp-with-modes 'latex-mode
(sp-local-pair "«" "»")
(sp-local-pair "“" "”"))
(with-eval-after-load 'latex
(define-key LaTeX-mode-map (kbd "M-RET") 'latex-insert-item)
(define-key LaTeX-mode-map [remap compile] 'TeX-command-run-all))
;;;; Org-Mode
(require 'org)
(require 'org-num)
(require 'ox-latex)
(setq org-fold-catch-invisible-edits t
org-hide-leading-stars t
org-hide-emphasis-markers nil
org-html-htmlize-output-type 'css
org-imenu-depth 8
org-src-fontify-natively t
org-use-speed-commands nil
;;org-ellipsis " ▼"
org-special-ctrl-a/e t
org-special-ctrl-k t)
(add-hook 'org-mode-hook (lambda ()
(org-indent-mode t)
(visual-line-mode t)))
;; Use shift-arrow for window navigation when not on an heading
(add-hook 'org-shiftup-final-hook 'windmove-up)
(add-hook 'org-shiftleft-final-hook 'windmove-left)
(add-hook 'org-shiftdown-final-hook 'windmove-down)
(add-hook 'org-shiftright-final-hook 'windmove-right)
(with-eval-after-load 'org-indent
(diminish 'org-indent-mode))
;;;;; Pairs
(defun sp--org-skip-asterisk (_ mb me)
"I assume: skip opening * in matched string _ between MB and ME."
(or (and (= (line-beginning-position) mb)
(eq 32 (char-after (1+ mb))))
(and (= (1+ (line-beginning-position)) me)
(eq 32 (char-after me)))))
(sp-with-modes 'org-mode