-
Notifications
You must be signed in to change notification settings - Fork 3
/
init.el
2431 lines (1999 loc) · 82.1 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
;;; Emacs Configurations -*- no-byte-compile: t -*-
(let ((min-version "28.1"))
(if (version< emacs-version min-version)
(error "Emacs v. %s+ is required for this configuration!" min-version)))
;; Constants.
(defconst --emacs-start-time (current-time))
(defconst --lisp-dir (concat user-emacs-directory "lisp/"))
(defconst --misc-dir (concat user-emacs-directory "misc/"))
(defconst --yas-dir (concat user-emacs-directory "snippets/"))
(defconst --themes-dir (concat user-emacs-directory "themes/"))
(defconst --user-cache-dir (concat user-emacs-directory "cache/"))
(defconst --auto-save-dir (concat user-emacs-directory "auto-save/"))
;; Set to `t' to enable config loading benchmarking and showing results when finished.
(defconst --do-init-benchmark nil)
(setq custom-theme-directory --themes-dir)
;; Custom.
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(custom-enabled-themes '(mustang-netrom))
'(custom-safe-themes
'("7fbf91c674f9628401c56c88729d1d5037568cb8537b2d45d81b34f4836e450e" default)))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)
;; Set the random number seed from the system's entropy pool.
(random t)
;; Prefer newest version of a file. This is especially useful for compiled files.
(setq load-prefer-newer t)
;; Load general stuff that other init.el things might use. Loading without extension to
;; automatically load the newest version of a file: .el or .elc.
(mapc (lambda (file)
(load (file-name-sans-extension file)))
(directory-files --lisp-dir t "\\.el$"))
;; Create necessary directories if missing.
(mkdir --user-cache-dir)
(mkdir --auto-save-dir)
(setq --initial-done-time (current-time))
;; Speed up loading by removing handlers until finished. It contains a lot of regexps for matching
;; handlers to file names but it is not necessary while loading.
(setq file-name-handler-alist-old file-name-handler-alist
file-name-handler-alist nil)
;; Speed up loading by disabling garbage collection until finished.
(setq gc-cons-threshold most-positive-fixnum)
;;;;; Setup and Bootstrap straight.el ;;;;;
(defvar --straight-bootstrap-version)
(let ((bootstrap-file
(expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
(--straight-bootstrap-version 5))
(unless (file-exists-p bootstrap-file)
(with-current-buffer
(url-retrieve-synchronously
"https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
'silent 'inhibit-cookies)
(goto-char (point-max))
(eval-print-last-sexp)))
(load bootstrap-file nil 'nomessage))
(setq straight-repository-branch "master"
straight-cache-autoloads t
;; Make `use-package` use `straight.el` by default so that `:straight t` is not necessary to
;; write for every `use-package` invocation.
straight-use-package-by-default t
straight-hosts '((github "github.com" ".git")
(gitlab "gitlab.com" ".git")
(bitbucket "bitbucket.com" ".git")
(codeberg "codeberg.org" ".git"))
straight-profiles '((nil . "default.el")
;; Packages which are pinned to a specific commit.
(pinned . "pinned.el")))
;; Load straight-x.el to make pinning of packages possible.
;; NOTE: Use `straight-x-pull-all' instead of `straight-pull-all', and use
;; `straight-x-freeze-versions' instead of `straight-freeze-versions'!
(load-library "straight-x")
;;;;; Pinned packages ;;;;;
(add-to-list 'straight-x-pinned-packages
'("doom-modeline" . "156b02445c3360added80009ab3c1a33dd88c5d9")) ;; v3.3.1
(add-to-list 'straight-x-pinned-packages
'("lsp-mode" . "5e0524cc9a4e21c4fe5b35153ad33e7b8a4f9117")) ;; v8.0.0
(add-to-list 'straight-x-pinned-packages
'("lsp-ui" . "9a8983d95d823ae62e5f842a4bd433c860131398")) ;; v8.0.1
;;;;; Timing ;;;;;
(setq --straight-init-done-time (current-time))
(defun show-elapsed-time (msg start end)
(let ((elapsed (float-time (time-subtract end start))))
(message "%s %.3fs" msg elapsed)))
(defun show-loading-info ()
(let ((cur (current-time)))
(message "============================")
(show-elapsed-time "Initial setup: " --emacs-start-time --initial-done-time)
(show-elapsed-time "straight.el setup: " --initial-done-time --straight-init-done-time)
(show-elapsed-time "Loaded packages: " --straight-init-done-time cur)
(show-elapsed-time "Total: " --emacs-start-time cur)
(message "============================")
;; Show message 2s later about total time so it's visible in the mini buffer as the last thing.
(run-at-time "2 sec" nil
#'show-elapsed-time "Loaded config in" --emacs-start-time cur)))
;; Executed when loading is done.
(defun loading-done ()
;; Stop benchmarking if enabled and show tabulated findings.
(when --do-init-benchmark
(benchmark-init/deactivate)
(run-at-time "2 sec" nil
#'benchmark-init/show-durations-tabulated))
(show-loading-info)
;; Restore the file name handlers.
(setq file-name-handler-alist file-name-handler-alist-old)
;; Garbage collect at every 20 MB allocated instead of the default 8 MB. This speeds up various
;; things.
(setq gc-cons-threshold 20000000)
;; Start daemon server if not already running.
(require 'server)
(unless (server-running-p)
(server-start))
(byte-compile-confs))
;;;;; Package Configuration Macro ;;;;;
;; The `use-package` configuration macro is used throughout this configuration to isolate package
;; configurations in a tidy fashion.
;; Install `use-package` which `straight.el` will automatically modify to use itself instead of
;; `package.el` since `straight-use-package-by-default` is set above.
(straight-use-package 'use-package)
;; Verbosity when starting emacs with `--debug-init'.
(if init-file-debug
(setq use-package-verbose t
use-package-expand-minimally nil
use-package-compute-statistics t
debug-on-error t)
(setq use-package-verbose nil
use-package-expand-minimally t))
;;;;; Benchmarking ;;;;;
;; Load and activate config loading benchmarking. It is deactivated in `loading-done'.
(when --do-init-benchmark
(use-package benchmark-init)
(benchmark-init/activate))
;;;;; Mac Setup ;;;;;
(when (eq system-type 'darwin)
(progn
(if (or (eq window-system 'ns)
(eq window-system 'mac))
(progn
;; avoid, e.g., hiding with M-h etc. (Carbon Emacs specific)
;;(setq mac-pass-command-to-system nil)
;; Let command be meta and alt be alt.
(setq mac-option-key-is-meta nil)
(setq mac-command-key-is-meta t)
(setq mac-command-modifier 'meta)
(setq mac-option-modifier nil)
;; The font is located in the ./font/ folder. Install it on the system to be used.
(set-frame-font "JetBrains Mono-12")))
(defun remove-dos-eol ()
"Do not show ^M in files containing mixed UNIX and DOS line endings."
(interactive)
(setq buffer-display-table (make-display-table))
(aset buffer-display-table ?\^M []))
;; ..and run it on all files.
(add-hook 'find-file-hook 'remove-dos-eol)
(defun open-with-finder ()
"Show current buffer-file, or directory if in Dired-mode, in Finder."
(interactive)
(if (eq 'dired-mode major-mode)
(shell-command "open .")
(shell-command (concat "open -R '" (concat buffer-file-name "'")))))
(defun toggle-fullscreen ()
"Toggle full screen."
(interactive)
(set-frame-parameter
nil 'fullscreen
(when (not (frame-parameter nil 'fullscreen)) 'fullboth)))))
;;;;; Linux / X11 Setup ;;;;;
(if (eq window-system 'x)
(set-default-font "-misc-fixed-medium-r-normal--13-120-75-75-c-70-iso8859-15"))
;;;;; Shell ;;;;;
(use-package exec-path-from-shell
:config
(when (memq window-system '(mac ns x))
(exec-path-from-shell-initialize)))
;;;;; Bindings ;;;;;
(use-package beacon
:config
(setq beacon-blink-delay 0.1
beacon-blink-duration 0.3
beacon-color "#b1d631")
(defun backward-paragraph-blink ()
(interactive)
(backward-paragraph)
(beacon-blink))
(defun forward-paragraph-blink ()
(interactive)
(forward-paragraph)
(beacon-blink))
(global-set-key (kbd "M-p") 'backward-paragraph-blink)
(global-set-key (kbd "M-n") 'forward-paragraph-blink)
(beacon-mode 1))
;; General bindings.
(global-set-key (kbd "C-c o")
#'(lambda () (interactive) (ff-find-other-file nil t)))
(global-set-key (kbd "C-.") 'repeat)
;; Move to first whitespace or begninning of line if none. Pressing again goes to the beginning if
;; there was whitespace.
(global-set-key [remap move-beginning-of-line] 'smarter-move-beginning-of-line)
;; Cycle through "just one space", "no spaces" and original number of spaces,
;; instead of just "just one space". It does not delete newlines, too.
(global-set-key (kbd "M-SPC")
#'(lambda () (interactive) (cycle-spacing +1 t)))
;; Make font bigger/smaller.
(global-set-key (kbd "C-+") 'text-scale-increase)
(global-set-key (kbd "C--") 'text-scale-decrease)
(global-set-key (kbd "C-0") 'text-scale-adjust)
;; Indent, untabify and clean whitespace of region or buffer.
(global-set-key (kbd "C-c c") 'cleanup-region-or-buffer)
;; Intelligent line opening that also places cursor on new line.
(global-set-key (kbd "C-o") 'prelude-smart-open-line)
(global-set-key (kbd "M-o") 'open-line) ;; Default way.
(use-package key-chord
:config
(setq key-chord-two-keys-delay 0.2)
(key-chord-define-global "qq" 'kill-this-buffer)
(key-chord-define-global "''" "`'\C-b")
(key-chord-define-global ",," 'indent-for-comment-and-indent)
(key-chord-define-global "((" 'kmacro-start-macro)
(key-chord-mode 1))
;;;;; Window Movement ;;;;;
(use-package golden-ratio-scroll-screen
:config
(global-set-key [remap scroll-down-command] 'golden-ratio-scroll-screen-down)
(global-set-key [remap scroll-up-command] 'golden-ratio-scroll-screen-up))
;;;;; Magit ;;;;;
(use-package magit
:config
;; Bindings.
(global-set-key (kbd "C-x g") 'magit-status)
;; Show word-granularity differences within current diff hunk.
(setq magit-diff-refine-hunk t)
;; Show status full screen.
(defadvice magit-status (around magit-fullscreen activate)
(window-configuration-to-register :magit-fullscreen)
ad-do-it
(delete-other-windows))
;; Cycle between how much info is shown in the margin: author + date, date, none.
(defun magit-cycle-margin ()
"Cycle visibility of the Magit margin.
,-> show with details --> show no details -- hide -.
`--------------------------------------------------'"
(interactive)
(if (not (magit-margin-option))
(user-error "Magit margin isn't supported in this buffer")
(pcase (list (nth 0 magit-buffer-margin)
(and (nth 3 magit-buffer-margin) t))
(`(t t)
(setf (nth 3 magit-buffer-margin) nil)
(magit-set-buffer-margin nil t))
(`(t nil)
(setf (nth 0 magit-buffer-margin) nil)
(magit-set-buffer-margin))
(`(nil ,_)
(setf (nth 0 magit-buffer-margin) t)
(setf (nth 3 magit-buffer-margin) t)
(magit-set-buffer-margin nil t)))))
;; Call `magit-staging' to show mode with only unstaged and staged changes diff where one can
;; stage/unstage chunks like normal.
(define-derived-mode magit-staging-mode magit-status-mode "Magit staging"
"Mode for showing staged and unstaged changes."
:group 'magit-status)
(defun magit-staging-refresh-buffer ()
(magit-insert-section (status)
(magit-insert-unstaged-changes)
(magit-insert-staged-changes)))
(defun magit-staging ()
(interactive)
(window-configuration-to-register :magit-fullscreen)
(magit-mode-setup #'magit-staging-mode)
(delete-other-windows))
(defun magit-log-origin ()
"Show log of origin of current branch."
(interactive)
(magit-log-other `(,(concat "origin/" (magit-get-current-branch))))))
;;;;; Selectrum ;;;;;
(use-package orderless
:config
(defun flex-if-twiddle (pattern _index _total)
(when (string-suffix-p "~" pattern)
`(orderless-flex . ,(substring pattern 0 -1))))
(defun without-if-bang (pattern _index _total)
(cond
((equal "!" pattern)
'(orderless-literal . ""))
((string-prefix-p "!" pattern)
`(orderless-without-literal . ,(substring pattern 1)))))
;; Use orderless for all completion, defaulting to regexp style.
;; If pattern ends with `~' then use flex style.
;; If pattern starts with `!' then exclude results using literal style.
(setq completion-styles '(orderless)
orderless-matching-styles '(orderless-regexp)
orderless-style-dispatchers '(flex-if-twiddle without-if-bang)))
(use-package prescient
:config
(setq prescient-save-file (concat user-emacs-directory "prescient-save"))
(prescient-persist-mode +1))
(use-package marginalia
:config
(marginalia-mode +1))
(use-package selectrum
:requires orderless
:config
(setq selectrum-refine-candidates-function #'orderless-filter
selectrum-highlight-candidates-function #'orderless-highlight-matches
selectrum-count-style 'current/matches
selectrum-max-window-height 15)
(selectrum-mode +1))
(use-package selectrum-prescient
:requires selectrum prescient
:config
;; Use filtring from only `completion-styles' and not selectrum.
(setq selectrum-prescient-enable-filtering nil)
;; But enable frequency and recency ordering from selectrum.
(selectrum-prescient-mode +1))
;;;;; Hydra ;;;;;
(use-package hydra
:config
;; Easier cycling of yanking.
(defhydra yank-pop-hydra ()
"yank"
("C-y" yank nil)
("M-y" yank-pop nil)
("y" (yank-pop 1) "next")
("Y" (yank-pop -1) "prev"))
(global-set-key (kbd "M-y") #'yank-pop-hydra/yank-pop)
(global-set-key (kbd "C-y") #'yank-pop-hydra/yank)
(defhydra compilation-hydra (:columns 4)
"
Command: %(netrom/compilation-command-string)
%(netrom/compilation-scroll-output-string) + %(netrom/compilation-skip-threshold-string)
"
("c" compile "Compile")
("C" compile-from-buffer-folder "Compile from buffer folder")
("r" recompile "Recompile")
("k" netrom/kill-compilation "Stop")
("n" next-error "Next error")
("N" next-error-skip-warnings "Next error, skip warnings")
("p" previous-error "Previous error")
("f" first-error "First error")
("l" netrom/compilation-last-error "Last error")
("s" netrom/compilation-toggle-scroll "Toggle scroll")
("t" netrom/compilation-toggle-threshold "Toggle threshold")
("q" nil "Cancel" :color blue))
(global-set-key [(f5)] 'compilation-hydra/body)
;; Define hydra for programming modes.
(add-hook 'prog-mode-hook
(lambda ()
;; Using local-set-key because defining the bindings in prog-mode-map will get
;; overridden by c++-mode bindings, for instance. This shadows them instead.
(when (member major-mode '(c++-mode c-mode))
(local-set-key (kbd "C-c C-c") 'compilation-hydra/body)))))
;;;;; Help & Documentation ;;;;;
;; Helpful is an alternative to the built-in Emacs help that provides much more contextual
;; information.
(use-package helpful
:config
;; Note that the built-in `describe-function' includes both functions and
;; macros. `helpful-function' is functions only, so we provide `helpful-callable' as a drop-in
;; replacement.
(global-set-key (kbd "C-h f") #'helpful-callable)
(global-set-key (kbd "C-h v") #'helpful-variable)
(global-set-key (kbd "C-h k") #'helpful-key)
;; Lookup the current symbol at point. C-c C-d is a common keybinding
;; for this in lisp modes.
(global-set-key (kbd "C-c C-d") #'helpful-at-point)
;; Look up *F*unctions (excludes macros).
;;
;; By default, C-h F is bound to `Info-goto-emacs-command-node'. Helpful
;; already links to the manual, if a function is referenced there.
(global-set-key (kbd "C-h F") #'helpful-function)
;; Look up *C*ommands.
;;
;; By default, C-h C is bound to describe `describe-coding-system'. I
;; don't find this very useful, but it's frequently useful to only
;; look at interactive functions.
(global-set-key (kbd "C-h C") #'helpful-command))
;;;;; Completion ;;;;;
;; Using hippie-expand instead of dabbrev-expand.
(global-set-key (kbd "M-/") 'hippie-expand)
;; Put dabbrev expansions first because it's most often what's expected.
(setq hippie-expand-try-functions-list
'(try-expand-dabbrev
try-expand-dabbrev-all-buffers
try-expand-dabbrev-from-kill
try-expand-all-abbrevs
try-expand-whole-kill
try-complete-file-name-partially
try-complete-file-name
try-expand-list
try-expand-line))
;; Enable the lisp expansion functions for lisp modes.
(add-to-multiple-hooks
(lambda ()
(setq-local hippie-expand-try-functions-list
'(try-expand-dabbrev
try-expand-dabbrev-all-buffers
try-expand-dabbrev-from-kill
try-expand-all-abbrevs
try-expand-whole-kill
try-complete-lisp-symbol-partially
try-complete-lisp-symbol
try-complete-file-name-partially
try-complete-file-name
try-expand-list
try-expand-line)))
'(emacs-lisp-mode-hook
lisp-interaction-mode-hook))
(use-package flx-ido
:requires flx
:config
(ido-mode 1)
(ido-everywhere 1)
(flx-ido-mode 1)
;; Disable ido faces to see flx highlights.
(setq ido-enable-flex-matching t)
(setq ido-use-faces nil))
(use-package company
:config
(setq company-idle-delay 0.3)
(global-company-mode 1)
(global-set-key (kbd "C-<tab>") 'company-complete))
;; Show icons in company completion UI.
(use-package company-box
:config
(if (display-graphic-p)
;; Show font icons in windowed mode.
(setq company-box-color-icon t)
;; Show compatible icons in terminal.
(setq company-box-icons-alist 'company-box-icons-icons-in-terminal))
:hook (company-mode . company-box-mode))
(use-package company-flx
:requires company
:config
(company-flx-mode +1))
;;;;; Development ;;;;;
(use-package cmake-font-lock
:hook (cmake-mode . cmake-font-lock-activate))
(use-package cmake-mode
:mode (("CMakeLists\\.txt\\'" . cmake-mode)
("\\.cmake\\'" . cmake-mode)))
;; General compilation settings.
(setq compilation-window-height 30
compilation-scroll-output 'first-error ; Scroll but stop at first error.
compilation-skip-threshold 2 ; Skip anything less than errors.
compilation-always-kill t) ; Don't ask, just start new compilation.
;; Turn off adaptive process buffering when using compilation mode because it speeds up immensely
;; when there is a lot of output in the buffer. And tweak split-height-threshold so that it shows
;; the compilation buffer in the bottom because that looks best, otherwise it will show to the side
;; and replace any buffer already at that place whereas this solution keeps the buffers but shows it
;; at the bottom.
(setq old-split-height-threshold nil)
(add-hook 'compilation-mode-hook
(lambda ()
(setq old-split-height-threshold split-height-threshold
split-height-threshold --global-fill-column)
process-adaptive-read-buffering nil))
;; Turn it back on again when finished.
(add-hook 'compilation-finish-functions
(lambda (buffer string)
(setq split-height-threshold old-split-height-threshold
old-split-height-threshold nil
process-adaptive-read-buffering t)))
(defun next-error-skip-warnings ()
(interactive)
(let ((compilation-skip-threshold 2))
(next-error)))
(defun compile-from-buffer-folder (cmd)
(interactive
(list
(read-shell-command "Compile command (pwd): ")))
(compile
(format "cd `dirname '%s'` && %s" (buffer-file-name) cmd)))
(defun netrom/compilation-toggle-scroll ()
"Toggle between not scrolling and scrolling until first error
in compilation mode."
(interactive)
(if (not compilation-scroll-output)
(setq compilation-scroll-output 'first-error)
(setq compilation-scroll-output nil)))
(defun netrom/compilation-scroll-output-string ()
(interactive)
(if (not compilation-scroll-output)
"No scroll"
"Scroll until match"))
(defun netrom/compilation-skip-threshold-string ()
(interactive)
(cond
((= compilation-skip-threshold 2)
"Skip anything less than error")
((= compilation-skip-threshold 1)
"Skip anything less than warning")
((<= compilation-skip-threshold 0)
"Don't skip anything")))
(defun netrom/compilation-toggle-threshold ()
(interactive)
(progn
(setq compilation-skip-threshold (- compilation-skip-threshold 1))
(when (< compilation-skip-threshold 0)
(setq compilation-skip-threshold 2))))
(defun netrom/compilation-command-string ()
(interactive)
(if (not compile-command)
"None"
compile-command))
(defun netrom/compilation-last-error ()
(interactive)
(condition-case err
(while t
(next-error))
(user-error nil)))
(defun netrom/kill-compilation ()
"Sometimes `kill-compilation' doesn't work because it finds the
wrong buffer. Here `compilation-find-buffer' uses non-nil
`AVOID-CURRENT' to only use current as a last resort."
(interactive)
(interrupt-process (get-buffer-process (compilation-find-buffer t))))
;; Closes *compilation* buffer after successful compilation, and otherwise when the failure was
;; fixed to compile, it restores the original window configuration.
(use-package bury-successful-compilation
:config
(add-hook 'prog-mode-hook 'bury-successful-compilation))
(use-package dash-at-point
:config
(global-set-key "\C-cd" 'dash-at-point)
(add-to-list 'dash-at-point-mode-alist '(c-mode . "c,manpages"))
(add-to-list 'dash-at-point-mode-alist '(c++-mode . "cpp,qt,c,manpages,lux"))
(add-to-list 'dash-at-point-mode-alist '(python-mode . "py,flask"))
(add-to-list 'dash-at-point-mode-alist '(cmake-mode . "cmake"))
(add-to-list 'dash-at-point-mode-alist '(js-mode . "js")))
;; Highlights hexcolors, like #aabbcc and Red.
(use-package rainbow-mode
:config
(add-hook 'prog-mode-hook 'rainbow-mode))
;; For programming modes, show delimiters with variying colors to easily
;; distinguish between them.
(use-package rainbow-delimiters
:config
(add-hook 'prog-mode-hook 'rainbow-delimiters-mode))
;; Highlights numbers with another color so they are easier to spot.
(use-package highlight-numbers
:config
(add-hook 'prog-mode-hook 'highlight-numbers-mode))
;; Highlights thing at point.
(use-package highlight-thing
:config
(setq highlight-thing-delay-seconds 1.5)
(setq highlight-thing-limit-to-defun t) ;; Limit to current function.
(setq highlight-thing-what-thing 'symbol)
(setq highlight-thing-case-sensitive-p t)
;; Enable highlight-thing-mode in prog modes except for modes using LSP because it has it's own
;; highlight feature.
(add-hook 'prog-mode-hook
#'(lambda ()
(when (not (member major-mode '(c++-mode c-mode python-mode rust-mode)))
(highlight-thing-mode)))))
;; Better commenting DWIM that cycles. Use "C-u M-;" to align comments at end of line with those
;; around it.
(use-package comment-dwim-2
:config
(global-set-key (kbd "M-;") 'comment-dwim-2))
;; Avoid escape nightmares by editing string in separate buffer.
(use-package string-edit
:bind ("C-c e" . string-edit-at-point))
;; Visualize certain like space at end of line and trailing characters after
;; fill column.
(setq whitespace-style '(face tabs lines-tail trailing tab-mark))
(setq whitespace-line-column --global-fill-column)
(add-hook 'prog-mode-hook 'whitespace-mode)
;; Formatting code via clang-format-region.
(use-package clang-format)
;; Highlights escape sequences like \t, \n etc. in strings in programming modes.
(use-package highlight-escape-sequences
:config
;; Has its own `hes-mode-alist' that specifies which modes it supports.
(hes-mode))
;; ;; Annotate depth when it gets too deep.
;; (use-package annotate-depth
;; :config
;; (add-hook 'prog-mode-hook 'annotate-depth-mode)
;; (add-hook 'annotate-depth-mode-hook
;; (lambda ()
;; (if (equal major-mode 'emacs-lisp-mode)
;; (setq-local annotate-depth-threshold 10)
;; (when (equal major-mode 'c++-mode)
;; (setq-local annotate-depth-threshold 5))))))
;; C/C++
(use-package modern-cpp-font-lock
:config
(add-hook 'c++-mode-hook #'modern-c++-font-lock-mode))
(setq use-qt-tab-width nil)
(defun turn-on-qt-tab-width ()
"Use Qt tab width (4 spaces)."
(interactive)
(setq use-qt-tab-width t)
(revert-buffer :ignore-auto :noconfirm))
(defun turn-off-qt-tab-width ()
"Use general tab width."
(interactive)
(setq use-qt-tab-width nil)
(revert-buffer :ignore-auto :noconfirm))
(add-hook 'c-mode-common-hook
(lambda ()
;; If in Qt code then use 4 spaces as tabs, otherwise the general tab width.
(if (bound-and-true-p use-qt-tab-width)
(setq tab-width 4)
(setq tab-width --general-tab-width))
(setq c-basic-offset tab-width)
(setq indent-tabs-mode nil)
;; Run clang-format on region or buffer.
(local-set-key (kbd "C-c f") 'clang-format-region-or-buffer)))
(use-package cc-mode
:requires key-chord
:config
(defun my-colon-at-the-end ()
(interactive)
(progn
(move-end-of-line nil)
(cycle-spacing 0) ; Remove all spaces.
(insert ";")))
(key-chord-define c-mode-base-map ";;" 'my-colon-at-the-end))
(setq auto-mode-alist
(append '(("\\.c$" . c-mode)
("\\.C$" . c++-mode)
("\\.h$" . c++-mode)
("\\.hpp$" . c++-mode)
("\\.cpp$" . c++-mode)
("\\.hh$" . c++-mode)
("\\.cc$" . c++-mode))
auto-mode-alist))
;; ;; Swift
;; (use-package swift-mode
;; :config
;; (setq swift-indent-offset --general-tab-width))
;; Elisp
(defhydra netrom-elisp-hydra (:color blue :hint nil)
;; Xref
("d" xref-find-definitions "Definitions" :column "Xref")
("D" xref-find-definitions-other-window "-> other win")
("r" xref-find-references "References")
("s" xref-find-apropos "Search")
("f" netrom/consult-imenu "Filter funcs/classes")
("F" netrom/consult-imenu-multi "-> in all buffers")
;; Buffer
("b" eval-buffer "Eval buffer" :column "Buffer")
("r" eval-region "Eval region")
;; Misc
("q" nil "Cancel" :column "Misc")
("b" pop-tag-mark "Back"))
(add-hook 'emacs-lisp-mode-hook
(lambda ()
(eldoc-mode)
(local-set-key (kbd "C-c b") 'eval-buffer)
(local-set-key (kbd "C-c r") 'eval-region)
(local-set-key (kbd "C-c C-c") 'netrom-elisp-hydra/body)))
;; Auto-compile .el files when loading and saving them.
(use-package auto-compile
:config
(auto-compile-on-load-mode)
(auto-compile-on-save-mode))
;; Shell script
(require 'sh-script)
(setq-default sh-basic-offset --general-tab-width)
(setq auto-mode-alist
(append '(("\\.[z]?sh$" . sh-mode))
auto-mode-alist))
;; JavaScript
(require 'js)
(setq-default js-indent-level --general-tab-width)
;; It can take quite some time to load dense, minified JS files, so revert to plain old
;; fundamental-mode!
(add-to-list 'auto-mode-alist '("\\.min.js\\'" . fundamental-mode))
(use-package json-mode
:mode ("\\.json$" . json-mode))
;; Objective-C
(setq auto-mode-alist
(append '(("\\.mm$" . objc-mode))
auto-mode-alist))
;; PHP
(use-package php-mode)
;; CSS
(require 'css-mode)
(setq-default css-indent-offset --general-tab-width)
(setq auto-mode-alist
(append '(("\\.min.css$" . fundamental-mode) ;; Faster to load.
("\\.css$" . css-mode)
("\\.style$" . css-mode))
auto-mode-alist))
;; TWIG with SGML/HTML integration.
(use-package twig-mode)
;; Nginx config mode.
(use-package nginx-mode)
;; ;; C#
;; (use-package csharp-mode
;; :mode (("\\.cs$" . csharp-mode))
;; :config
;; (add-hook 'csharp-mode-hook
;; (lambda ()
;; (setq c-basic-offset --general-tab-width))))
;; Markdown
(use-package markdown-mode
:mode (("\\.markdown\\'" . markdown-mode)
("\\.md\\'" . markdown-mode))
:config
;; Turn off auto-fill-mode beacuse markdown is sensitive about newlines.
(add-hook 'markdown-mode-hook
(lambda ()
(auto-fill-mode 0)
(visual-line-mode t))))
;; Python
(require 'python)
(setq python-indent-offset 2)
;; Marks TODO, FIXME etc. clearly.
(use-package fic-mode
:config
(add-hook 'prog-mode-hook 'fic-mode))
(use-package yasnippet
:config
;; Add local snippets to override some of the defaults in elpa folder.
(add-to-list 'yas-snippet-dirs --yas-dir)
;; (setq yas-prompt-functions
;; '(yas-ido-prompt yas-dropdown-prompt yas-completing-prompt yas-x-prompt yas-no-prompt))
(yas-global-mode 1)
;; Disable normal tab expansion because it often interferes with
;; indentation.
(define-key yas-minor-mode-map (kbd "<tab>") nil)
(define-key yas-minor-mode-map (kbd "TAB") nil)
(define-key yas-minor-mode-map (kbd "C-M-y") 'yas-expand)
;; Test if thing at cursor can expand with yas, if it can then change the color of the cursor.
(setq default-cursor-color (face-background 'cursor))
(setq yasnippet-can-fire-cursor-color "#4CB5F5")
(defun yasnippet-can-fire-p (&optional field)
(interactive)
(setq yas--condition-cache-timestamp (current-time))
(let (templates-and-pos)
(unless (and yas-expand-only-for-last-commands
(not (member last-command yas-expand-only-for-last-commands)))
(setq templates-and-pos (if field
(save-restriction
(narrow-to-region (yas--field-start field)
(yas--field-end field))
(yas--templates-for-key-at-point))
(yas--templates-for-key-at-point))))
(and templates-and-pos (first templates-and-pos))))
(defun my/change-cursor-color-when-can-expand (&optional field)
(interactive)
(when (eq last-command 'self-insert-command)
(if (my/can-expand)
(progn
(set-cursor-color yasnippet-can-fire-cursor-color)
;; Change back 5 seconds afterwards to avoid border cases where it stays in the "can
;; expand" color when actually it can't.
(run-at-time "5 sec" nil
(lambda ()
(set-cursor-color default-cursor-color))))
(set-cursor-color default-cursor-color))))
(defun my/can-expand ()
"Return true if right after an expandable thing."
(or (abbrev--before-point) (yasnippet-can-fire-p)))
(add-hook 'post-command-hook 'my/change-cursor-color-when-can-expand))
(use-package smartparens
:config
(require 'smartparens-config)
(smartparens-global-mode t)
(show-smartparens-global-mode t)
;; Bindings
(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 "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-M-S-k") 'sp-kill-hybrid-sexp)
(define-key smartparens-mode-map (kbd "C-M-S-t") 'sp-transpose-hybrid-sexp)
(define-key smartparens-mode-map (kbd "C-M-S-s") 'sp-slurp-hybrid-sexp)
(define-key smartparens-mode-map (kbd "C-M-S-p") 'sp-push-hybrid-sexp)
;; General prog mode handling of "{}" to indent after hitting RET.
(sp-with-modes '(c-mode c++-mode php-mode java-mode js-mode rust-mode sh-mode)
(sp-local-pair "{" nil :post-handlers '(("||\n[i]" "RET"))))
;; Same thing but with "[]" and match "|" with "|".
(sp-with-modes '(rust-mode)
(sp-local-pair "[" nil :post-handlers '(("||\n[i]" "RET")))
(sp-local-pair "|" "|"))
;; Markdown modes
(sp-with-modes '(markdown-mode gfm-mode rst-mode)
(sp-local-pair "**" "**")
(sp-local-pair "_" "_"))
;; Lisp modes
(sp-with-modes sp--lisp-modes
(sp-local-pair "(" nil :wrap "C-(")
(sp-local-pair "`" "'" :when '(sp-in-string-p)))
(bind-key ";" 'sp-comment emacs-lisp-mode-map)