-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.el
1141 lines (941 loc) · 40 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
;;; Code:
;;;; Package installation and management
(require 'package)
(require 'use-package)
(setopt package-enable-at-startup t
use-package-always-ensure t
byte-compile-warnings nil
native-comp-async-report-warnings-errors nil) ; silence noisy warnings
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/"))
;; The :vc keyword is not enabled in use-package yet, so using this additional package.
(unless (package-installed-p 'vc-use-package)
(package-vc-install "https://github.com/slotThe/vc-use-package"))
;; Make system path variables accessible in Emacs
(use-package exec-path-from-shell
:custom
(exec-path-from-shell-check-startup-files nil)
:init
(exec-path-from-shell-initialize))
;;;; System local configuration
;; This section handles configurations that are local to the system and thus should not be under
;; version control, for instance credentials and system specific feature flags.
;; System local variables which can be set in local-settings.el
(defvar iensu-org-dir "~/org"
"Directory containing Org files.")
(defvar iensu-denote-dir "~/notes"
"Directory containing Denote notes.")
(defvar iensu-email-directory "~/Mail"
"Path to email directory.")
(defvar iensu-age-encrypted-key nil
"Path to encrypted key.")
(defvar iensu-age-session-duration "15 minutes")
(defvar iensu-org-refile-targets nil
"Org files which can be used as refiling targets.")
(defvar iensu-org-capture-templates nil
"Capture templates to be used by Org mode.")
(defvar iensu-enabled-features-alist '("elpher"
"pdf"
"web-dev"
"lang-bash"
"lang-docker"
"lang-fish"
"lang-go"
"lang-graphviz"
"lang-javascript"
"lang-json"
"lang-markdown"
"lang-nix"
"lang-terraform"
"lang-toml"
"lang-typescript"
"lang-rust"
"lang-wasm"
"lang-yaml")
"Locally enabled features. Available features are stored in the `features/' directory.")
;; Load settings
(let ((local-settings-file (expand-file-name "local-settings.el" user-emacs-directory)))
(when (file-exists-p local-settings-file)
(load-file (expand-file-name "local-settings.el"
user-emacs-directory))))
;; (setopt load-path (cons (concat user-emacs-directory "features") load-path))
;;;; Helper functions
(defun iensu-add-to-list (list &rest items)
"Add multiple items to a list."
(dolist (item items)
(add-to-list list item)))
;;;; Basic setup
;; This contains some basic default configuration which does not depend on any external packages.
;; Cleanup the UI by removing the menu tool and scroll bars.
(menu-bar-mode -1)
(tool-bar-mode -1)
(scroll-bar-mode -1)
;; Remove the warning bell.
(setopt ring-bell-function 'ignore)
;; Remove the GNU/Emacs startup screen to boot direct to the Scratch buffer.
(setopt inhibit-startup-screen t)
;; The week starts on Monday.
(setopt calendar-week-start-day 1)
(setopt calendar-intermonth-text
'(propertize
(format "%2d"
(car
(calendar-iso-from-absolute
(calendar-absolute-from-gregorian (list month day year)))))
'font-lock-face 'font-lock-warning-face))
(setopt calendar-intermonth-header
(propertize "Wk" 'font-lock-face 'font-lock-keyword-face))
;; Make sure that buffer names become unique when opening multiple files of the same name.
(setq frame-title-format "%b (%f)")
(setopt uniquify-buffer-name-style 'post-forward
uniquify-separator ":")
;; Ask before killing Emacs.
(setopt confirm-kill-emacs 'y-or-n-p)
;; Always just ask y/n instead of yes/no
(fset #'yes-or-no-p #'y-or-n-p)
;; Put any Emacs generated customizations into ./custom.el instead of ./init.el.
(setopt custom-file (expand-file-name "custom.el"
user-emacs-directory))
;; Prefer to recompile newer files instead of using already compiled code
(setopt load-prefer-newer t)
;; Improve performance by increasing the garbage collector threshold and max LISP evaluation depth.
(setopt gc-cons-threshold 100000000
max-lisp-eval-depth 2000)
(setq read-process-output-max (* 1024 1024))
;; Move backups and auto-saves
(setopt create-lockfiles nil
backup-directory-alist `(("." . ,(expand-file-name ".local/backups/" user-emacs-directory)))
backup-by-copying t
delete-old-versions t
kept-new-versions 6
frame-inhibit-implied-resize 1
delete-by-moving-to-trash t
undo-limit 8000000)
(setq auto-save-list-file-name (expand-file-name ".local/auto-saves-list" user-emacs-directory))
;; Don't allow eldoc to display more than one line in the echo area
(setopt eldoc-echo-area-use-multiline-p nil)
(pixel-scroll-mode 1)
;; Enable autosaves
(auto-save-mode 1)
;; Remember recent files
(use-package recentf
:custom
(recentf-max-menu-items 50)
:config
(recentf-load-list)
:init
(recentf-mode 1)
(setopt recentf-save-file (expand-file-name ".local/recentf" user-emacs-directory)))
;; Keep some files in ~/.emacs.d/.local to avoid cluttering the configuration root directory.
(setopt url-configuration-directory (expand-file-name ".local/uri/" user-emacs-directory)
bookmark-default-file (expand-file-name ".local/bookmarks" user-emacs-directory)
tramp-auto-save-directory (expand-file-name ".local/tramp-autosaves/" user-emacs-directory))
(setq image-dired-dir (expand-file-name ".local/image-dired-thumbnails/" user-emacs-directory))
;; Setup authentication file.
(setopt auth-sources '("~/.authinfo.gpg"
"~/.netrc"))
;; File encryption
(use-package age
:ensure t
:demand t
:config
(age-file-enable)
(setopt age-default-identity "/Users/iensu/.identities/main.txt"
age-default-recipient "/Users/iensu/.recipients"
age-pinentry-mode 'ask
age-debug t
;; age doesn't work with pinentry, so using rage instead
age-program (executable-find "rage"))
(setq iensu--age-session-timer nil)
(defun iensu/age-session-start ()
"Starts an age session by decrypting the age key.
The decrypted key will be deleted either after `iensu-age-session-duration' or when Emacs is exited."
(interactive)
(if iensu--age-session-timer
(message "Age session is already running!")
;; rage is needed for pinentry support!
(if (not (executable-find "rage"))
(message "'rage' executable not found!")
(let ((decrypted-key (string-replace ".age" ".txt" iensu-age-encrypted-key)))
(when (file-exists-p decrypted-key)
(shell-command (format "rm %s" decrypted-key)))
(shell-command (format "rage -d %s -o %s" iensu-age-encrypted-key decrypted-key))
(setq iensu--age-session-timer
(run-at-time iensu-age-session-duration nil #'iensu/age-session-end))))))
(defun iensu/age-session-end ()
"Ends an age session by deleting the decrypted file and cancelling the age session timer."
(interactive)
(let ((decrypted-key (string-replace ".age" ".txt" iensu-age-encrypted-key)))
(when (file-exists-p decrypted-key)
(shell-command (format "rm %s" decrypted-key)))
(when iensu--age-session-timer
(cancel-timer iensu--age-session-timer)
(setq iensu--age-session-timer nil))))
(add-hook 'kill-emacs-hook #'iensu/age-session-end))
;; Auto scroll through output in compilation buffers.
(setopt compilation-scroll-output t)
;; Save bookmarks when changed
(setopt bookmark-save-flag 1)
;;;; Editor default settings
;; This section changes the default text editing behavior of Emacs so it is more inline with my
;; expectations.
;; Use spaces instead of tabs.
(setopt indent-tabs-mode nil
tab-width 2)
(setopt ns-right-command-modifier 'super)
;; Read-only buffers are visited in `view-mode'.
(setopt view-read-only t)
(setopt require-final-newline t) ; Files should always have a final newline.
(setopt sentence-end-double-space nil) ; Sentence end does not require two spaces.
;; Fix buffer scrolling behavior.
(setopt scroll-conservatively 0
scroll-step 4
next-screen-context-lines 20)
(add-hook 'before-save-hook 'delete-trailing-whitespace) ; Delete trailing whitespace on save.
(delete-selection-mode 1) ; Replace selected text with typed text.
;; Enable narrowing to region and page. Pages are delimited by ^L.
(put 'narrow-to-page 'disabled nil)
(put 'narrow-to-region 'disabled nil)
;; Disable suspend key binding
(global-unset-key (kbd "C-x C-z"))
;; Use the editorconfig package to conform to project formatting rules if present.
(use-package editorconfig
:hook
(prog-mode . editorconfig-mode)
(text-mode . editorconfig-mode))
;; Make parentheses pretty
(use-package rainbow-delimiters
:hook
(scheme-mode . rainbow-delimiters-mode)
(emacs-lisp-mode . rainbow-delimiters-mode)
(lisp-mode . rainbow-delimiters-mode)
(lisp-interaction-mode . rainbow-delimiters-mode)
(geiser-repl-mode . rainbow-delimiters-mode))
;; Enable multiple cursors for convenient editing. Use `iedit' for quick and dirty multi-cursor
;; functionality.
(use-package iedit)
(use-package multiple-cursors
:bind
(("M-=" . mc/edit-lines)
("C-S-<right>" . mc/mark-next-like-this)
("C-S-<left>" . mc/mark-previous-like-this)
("C-S-<mouse-1>" . mc/add-cursor-on-click))
:custom
(mc/list-file (expand-file-name ".local/.mc-lists.el" user-emacs-directory)))
;; Expand region from current region or point.
(use-package expand-region
:bind
(("C-=" . er/expand-region)
("C-M-=" . er/contract-region)))
;;;;; Programming mode related settings
(defun iensu--prog-mode-hook ()
"Defaults for programming modes"
(subword-mode 1) ; delimit words at camelCase boundries
(eldoc-mode 1) ; display documentation in minibuffer
(display-line-numbers-mode 1) ;; display line numbers
(show-paren-mode 1) ; highlight matching parentheses
(setopt show-paren-when-point-in-periphery t
show-paren-when-point-inside-paren t)
(hs-minor-mode 1) ; hide-show code and comment blocks
(outline-minor-mode 1)) ; Navigate by outlines
(add-hook 'prog-mode-hook #'iensu--prog-mode-hook)
;; Manipulate parentheses and other code structures.
;; Some of these commands might be intercepted by MacOS Mission Control shortcuts!
(use-package smartparens
:init
(require 'smartparens-config)
:bind (:map smartparens-mode-map
("M-s" . sp-unwrap-sexp)
("C-<down>" . sp-down-sexp)
("C-<up>" . sp-up-sexp)
("M-<down>" . sp-backward-down-sexp)
("M-<up>" . sp-backward-up-sexp)
("C-<right>" . sp-forward-slurp-sexp)
("M-<right>" . sp-forward-barf-sexp)
("C-<left>" . sp-backward-slurp-sexp)
("M-<left>" . sp-backward-barf-sexp))
:hook
(prog-mode . smartparens-mode) ; non-strict by default, but keep strict in LISPs
(repl-mode . smartparens-strict-mode)
(lisp-mode . smartparens-strict-mode)
(ielm-mode . smartparens-strict-mode)
(emacs-lisp-mode . smartparens-strict-mode))
;; Prettify compilation-mode buffers
(use-package xterm-color
:init
(defun iensu--advice-compilation-filter (f proc string)
;; Apply `xterm-color' only to real compilation buffers, and not buffers which rely on the
;; color codes for parsing (ag.el, rg.el)
;; More info: https://github.com/atomontage/xterm-color/issues/37
(funcall f proc (if (string-prefix-p "*compilation" (buffer-name (process-buffer proc)))
(xterm-color-filter string) string)))
(advice-add 'compilation-filter :around #'iensu--advice-compilation-filter)
(add-hook 'compilation-mode-hook
(lambda () (setopt compilation-environment '("TERM=xterm-256color")))))
;; Install vterm for better terminal support
(use-package vterm
:config
(setopt vterm-shell (executable-find "fish"))
(defun iensu/project-vterm ()
"Open a vterm terminal at the current project root."
(interactive)
(let* ((default-directory (project-root (project-current t)))
(vterm-buffer-name (project-prefixed-buffer-name "vterm"))
(vterm-buffer (get-buffer vterm-buffer-name)))
(if (and vterm-buffer (not current-prefix-arg))
(pop-to-buffer vterm-buffer t)
(vterm current-prefix-arg)))))
(use-package multi-vterm)
;;;;; Text editing tools
;; Spellcheck using flyspell
(use-package flyspell
:bind (:map flyspell-mode-map ("C-:" . flyspell-popup-correct))
:custom
(ispell-program-name "aspell")
(ispell-extra-args '("--sug-mode=ultra"))
(ispell-list-command "--list")
(ispell-dictionary "en_US")
:config
(unbind-key (kbd "C-.") 'flyspell-mode-map) ;; Using this binding for other stuff
(defvar iensu--language-ring nil
"Ispell language ring used to toggle current selected ispell dictionary")
(let ((languages '("swedish" "en_US")))
(setq iensu--language-ring (make-ring (length languages)))
(dolist (elem languages) (ring-insert iensu--language-ring elem)))
(defun iensu/cycle-ispell-dictionary ()
"Cycle through the languages defined in `iensu--language-ring'."
(interactive)
(let ((language (ring-ref iensu--language-ring -1)))
(ring-insert iensu--language-ring language)
(ispell-change-dictionary language)
(message (format "Switched to dictionary: %s" language))))
(defalias 'sd #'iensu/cycle-ispell-dictionary
"Switch spellchecking dictionary."))
(use-package flyspell-popup :after (flyspell))
;; Use synosaurus to look up synonyms
(use-package synosaurus
:custom
(synosaurus-backend 'synosaurus-backend-wordnet)
(synosaurus-choose-method 'popup))
;; Emoji support because reasons...
(use-package emojify
:custom
(emojify-emojis-dir (expand-file-name ".local/emojis" user-emacs-directory)))
;; `visual-fill-column' makes it possible to visually wrap and center text which is good for
;; document-like editing.
(use-package visual-fill-column
:config
(setopt visual-fill-column-center-text t)
(let ((column-width 130))
(setopt visual-fill-column-width column-width)
(setopt fill-column column-width)))
(defun iensu/text-editing-mode-hook ()
"Enables text editing tools such as spell checking and thesaurus support"
(interactive)
(flyspell-mode 1)
(synosaurus-mode 1)
(emojify-mode 1)
(visual-line-mode 1)
(visual-fill-column-mode 1))
(add-hook 'text-mode-hook #'iensu/text-editing-mode-hook)
;;;; Utility packages
(use-package rfc-mode)
(use-package marginalia
:init
(marginalia-mode)
:config
(setq marginalia-annotators '(marginalia-annotators-heavy marginalia-annotators-light nil)))
(use-package consult
:bind
(("C-c h" . consult-history)
("C-x M-:" . consult-complex-command)
("C-x b" . consult-buffer)
("C-x 4 b" . consult-buffer-other-window)
("C-x 5 b" . consult-buffer-other-frame)
("M-y" . consult-yank-pop)
("<help> a" . consult-apropos)
("M-g e" . consult-compile-error)
("M-g g" . consult-goto-line)
("M-g M-g" . consult-goto-line)
("M-g o" . consult-outline)
("M-g m" . consult-mark)
("M-g k" . consult-global-mark)
("M-g i" . consult-imenu)
("M-g I" . consult-project-imenu)
("H-s f" . consult-find)
("H-s L" . consult-locate)
("H-s g" . consult-grep)
("H-s G" . consult-git-grep)
("H-s r" . consult-ripgrep)
("H-s l" . consult-line)
("H-s m" . consult-multi-occur)
("H-s k" . consult-keep-lines)
("H-s u" . consult-focus-lines)
("H-s e" . consult-isearch)
:map isearch-mode-map
("M-e" . consult-isearch)
("M-s e" . consult-isearch)
("M-s l" . consult-line))
:config
(setopt consult-narrow-key "<")
;; Disable previews
(setopt consult-preview-key nil))
(use-package embark
:bind
(("C-." . embark-act)
("H-a" . embark-act)
("H-e" . embark-export)
("C-h B" . embark-bindings)))
(use-package embark-consult)
;; Install `hydra' with `pretty-hydra' which simplifies hydra definitions
(use-package hydra)
(use-package pretty-hydra :after (hydra))
;; Armor exported PGP-keys
(setq epa-armor t)
;; Password entry in minibuffer
(setopt epa-pinentry-mode 'loopback)
(defun iensu/fix-gpg ()
"Solving issue with Emacs 29.1 and GnuPG 2.4.1+."
(interactive)
(fset 'epg-wait-for-status 'ignore))
;; (iensu/fix-gpg)
(setopt dired-listing-switches "-alGh --group-directories-first"
dired-dwim-target t)
(when (executable-find "gls") ;; native OSX ls works differently then GNU ls
(setq insert-directory-program "/usr/local/bin/gls"))
;;;; Navigation
;; This section adds packages which enables quick navigation and search.
(use-package dired-sidebar
:bind (("C-x C-n" . dired-sidebar-toggle-sidebar))
:ensure t
:commands (dired-sidebar-toggle-sidebar)
:init
(add-hook 'dired-sidebar-mode-hook
(lambda ()
(unless (file-remote-p default-directory)
(auto-revert-mode))))
:config
(push 'toggle-window-split dired-sidebar-toggle-hidden-commands)
(push 'rotate-windows dired-sidebar-toggle-hidden-commands)
(setopt dired-sidebar-theme 'none)
(setopt dired-sidebar-use-term-integration t)
(setopt dired-sidebar-use-custom-font t))
;; Mark-ring tweaks
(setopt mark-ring-max 6
global-mark-ring-max 8)
;; Simplify jumping between local marks (C-u C-<space>, C-<space> * n)
(setopt set-mark-command-repeat-pop t)
(use-package deadgrep
:config
(add-to-list 'deadgrep-extra-arguments "--follow") ; follow symlinks
(add-to-list 'deadgrep-extra-arguments "--hidden") ; search hidden files
)
(use-package wgrep
:load-path (lambda () (expand-file-name "packages/wgrep" user-emacs-directory))
:config
(setopt wgrep-auto-save-buffer t)
(require 'wgrep-deadgrep))
;; Snippet expansion for less repetitive text editing
(use-package yasnippet
:delight yas-minor-mode
:init
(yas-global-mode 1)
(setopt yas-snippet-dirs (add-to-list 'yas-snippet-dirs (expand-file-name "snippets" user-emacs-directory)))
:config
(add-hook 'snippet-mode-hook (lambda ()
(setopt mode-require-final-newline nil
require-final-newline nil))))
;; When all else fails
(use-package dumb-jump
:config
(add-hook 'xref-backend-functions #'dumb-jump-xref-activate))
;; Speedbar for file navigation
(require 'speedbar)
(defun iensu/speedbar-reset-layout ()
(setf (alist-get 'width speedbar-frame-parameters) 60)
(setf (alist-get 'height speedbar-frame-parameters) 45)
(setf (alist-get 'left speedbar-frame-parameters) 0)
(setf (alist-get 'top speedbar-frame-parameters) 0))
(add-hook 'speedbar-after-create-hook #'iensu/speedbar-reset-layout)
(define-key speedbar-file-key-map (kbd "<tab>") #'speedbar-toggle-line-expansion)
(global-set-key (kbd "C-ä") #'speedbar)
;;;; Custom commands
(defun iensu/toggle-scratch-buffer ()
"Based on a great idea from Eric Skoglund (https://github.com/EricIO/emacs-configuration/)."
(interactive)
(if (string-equal (buffer-name (current-buffer))
"*scratch*")
(switch-to-buffer (other-buffer))
(switch-to-buffer "*scratch*")))
(defun iensu/toggle-profiler ()
"Starts or stops the profiler, displaying the report when stopped."
(interactive)
(if (profiler-running-p)
(progn
(profiler-stop)
(profiler-report))
(progn
(profiler-reset)
(profiler-start 'cpu+mem))))
;;;; Global key-bindings
(global-set-key (kbd "C-<backspace>") 'delete-indentation)
(global-set-key (kbd "C-h C-s") 'iensu/toggle-scratch-buffer)
(global-set-key (kbd "C-x C-b") 'ibuffer)
(global-set-key (kbd "M-<backspace>") 'fixup-whitespace)
(global-set-key (kbd "M-i") 'imenu)
(global-set-key (kbd "M-o") 'occur)
(global-set-key (kbd "M-/") 'hippie-expand)
(global-unset-key (kbd "C-z")) ;; Disable the suspend Emacs ikey-binding
(global-set-key (kbd "C-ö") 'window-toggle-side-windows)
(global-set-key (kbd "H-g") 'goto-line)
;; Trying out some of Steve Yegge's re-bindings
(global-set-key "\C-w" 'backward-kill-word)
(global-set-key "\C-x\C-k" 'kill-region)
(global-set-key "\M-p" 'previous-line)
(global-set-key "\M-n" 'next-line)
;; `windmove' enables navigation using `<shift>-<direction>'. These bindings conflict `org-mode' so
;; we make `windmove' take precedence.
(windmove-default-keybindings)
(setopt org-replace-disputed-keys t) ; This line needs to occur before `org-mode' is loaded.
(global-set-key (kbd "s-a") 'windmove-swap-states-left)
(global-set-key (kbd "s-w") 'windmove-swap-states-up)
(global-set-key (kbd "s-s") 'windmove-swap-states-down)
(global-set-key (kbd "s-d") 'windmove-swap-states-right)
;;;;; Global hydra
;; Setup a global hydra with keybindings I use very often.
(pretty-hydra-define iensu-hydra
(:color teal :quit-key "q" :title "Global commands")
("Utilities"
(("d" duplicate-dwim "duplicate DWIM" :exit nil)
("s" deadgrep "search")
("t" toggle-truncate-lines "truncate lines")
("u" revert-buffer "reload buffer")
("D" iensu/cycle-ispell-dictionary "change dictionary")
("+" (lambda () (interactive) (enlarge-window-horizontally 10)) "enlarge horizontally" :exit nil)
("?" (lambda () (interactive) (enlarge-window 5)) "enlarge vertically" :exit nil)
("-" (lambda () (interactive) (shrink-window-horizontally 10)) "shrink horizontally" :exit nil)
("_" (lambda () (interactive) (shrink-window 5)) "shrink vertically" :exit nil))
"Bookmarks"
(("l" list-bookmarks "list bookmarks")
("b" bookmark-set "set bookmark"))
"Misc"
(("P" iensu/project-todo-list "project todo list")
("p" iensu/open-project-org-file "open project notes file")
("ä" iensu/promote-side-window "promote side window"))
"Hide/show"
(("h h" hs-toggle-hiding "toggle block visibility")
("h l" hs-hide-level "hide all blocks at same level")
("h a" hs-hide-all "hide all")
("h s" hs-show-all "show all"))))
(global-set-key (kbd "C-å") #'iensu-hydra/body)
;; Enhance explorability with by listing possible completions while doing key chords.
(use-package which-key :config (which-key-mode))
;;;;; macOS specific keybindings
;; Set command to act as `meta' (`M-') and disable the `option' key since that button is needed to
;; type various characters on a Swedish keyboard. Also make the right `option' key act as `hyper'
;; (`H-') to give us more keybindings to work with.
(setopt mac-command-modifier 'meta
mac-option-modifier 'none
mac-right-option-modifier 'hyper)
;; An unfortunate workaround required when switching to an external keyboard.
(defun iensu/switch-left-and-right-option-keys ()
"Switch left and right option keys.
On some external keyboards the left and right Mac `option' keys are swapped,
this command switches the keys so that they work as expected."
(interactive)
(let ((current-left mac-option-modifier)
(current-right mac-right-option-modifier))
(setopt mac-option-modifier current-right
mac-right-option-modifier current-left)))
;;;; Make Emacs prettier
(setopt cursor-type 'box)
(global-prettify-symbols-mode 1)
(global-font-lock-mode 1)
;; Use dark mode on macOS.
(add-to-list 'default-frame-alist '(ns-transparent-titlebar . t))
(add-to-list 'default-frame-alist '(ns-appearence . dark))
(setq frame-title-format nil)
;; Use icons where applicable.
(use-package all-the-icons)
(use-package modus-themes
:config
(load-theme 'modus-vivendi-tinted t))
;; Pimp tab-bar-mode
(use-package emacs
:config
(add-to-list 'tab-bar-format #'tab-bar-format-menu-bar)
(set-face-attribute 'tab-bar nil
:family "Monospace"
:background nil)
(set-face-attribute 'tab-bar-tab nil
:box `(:line-width (4 . 8) :color ,(face-attribute 'default :background)))
(set-face-attribute 'tab-bar-tab-inactive nil
:box `(:line-width (4 . 8) :color ,(face-attribute 'default :background))
:background nil
:foreground "#969696")
(setopt tab-bar-close-button-show nil
tab-bar-new-button-show nil)
(setq tab-bar-separator " ")
(require 'desktop)
(add-to-list 'desktop-path "~/.emacs.d/.local/"))
;;;; Version control
;; Make `magit' and other version control tools follow symlinks.
(setopt vc-follow-symlinks t)
;; Use `magit' for a great `git' experience.
(use-package magit
:bind (("C-x g" . magit-status))
:custom
(magit-bury-buffer-function 'quit-window)
:config
(when (executable-find "~/.nix-profile/bin/git") ; Speeds up git operations on macOS
(setopt magit-git-executable "~/.nix-profile/bin/git"))
(setopt magit-display-buffer-function #'magit-display-buffer-same-window-except-diff-v1)
(setopt magit-refresh-status-buffer nil))
;; `smerge-mode' is a merge conflict resolution tool which is great but unfortunately has awful
;; default keybindings. Here I define a hydra to make `smerge' easier to work with.
(use-package smerge-mode
:bind (:map smerge-mode-map (("C-c ö" . smerge-mode-hydra/body)))
:pretty-hydra
((:color teal :quit-key "q" :title "Smerge - Git conflicts")
("Resolving"
(("RET" smerge-keep-current "Keep current" :exit nil)
("l" smerge-keep-lower "Keep lower" :exit nil)
("u" smerge-keep-upper "Keep upper" :exit nil)
("b" smerge-keep-base "Keep base" :exit nil)
("C" smerge-combine-with-next "Combine with next")
("a" smerge-keep-all "Keep all" :exit nil)
("r" smerge-resolve "Resolve"))
"Navigation"
(("n" smerge-next "Next conflict" :exit nil)
("p" smerge-prev "Previous conflict" :exit nil)
("R" smerge-refine "Highlight differences" :exit nil))
"Misc"
(("E" smerge-ediff "Open in Ediff")))))
;;;; Project management
;; Settings which help with handling and navigating projects.
(defun iensu/project-save ()
"Save current project to `project-list-file'."
(interactive)
(project-remember-project (project-current)))
(defun iensu/project-remove ()
"Remove current project from `project-list-file'."
(interactive)
(project-remove-known-project (project-root (project-current))))
(defun iensu/project-ripgrep ()
"Run ripgrep in the current project."
(interactive)
(consult-ripgrep (project-root (project-current))))
(use-package project
:bind
(("C-c p" . project-hydra/body))
:pretty-hydra
((:color teal :quit-key "q" :title "Project management")
("Project"
(("p" project-switch-project "open project")
("k" project-kill-buffers "close project")
("a" iensu/project-save "remember project")
("A" iensu/project-remove "forget project")
("v" iensu/project-vterm "vterm"))
"Files & Buffers"
(("f" project-find-file "open project file")
("o" iensu/open-project-org-file "open project org file")
("T" iensu/project-todo-list "open project TODO list"))
"Search"
(("s" iensu/project-ripgrep "search")
("r" project-query-replace-regexp "query replace"))))
:config
(setopt project-list-file (expand-file-name "projects"
(concat user-emacs-directory ".local/")))
(setopt project-switch-commands '((project-find-file "Find file")
(project-find-regexp "Find regexp")
(project-find-dir "Find directory")
(iensu/project-vterm "Vterm" ?v)
(magit-project-status "Magit" ?m)))
;; Handle projects which are not version controlled
(defun iensu--locate-non-vc-project (dir)
"Locate project root based on the existence of .project file.
Falls back to looking for .projectile for compatibility reasons."
(let ((root (or (locate-dominating-file dir ".project")
(locate-dominating-file dir ".projectile"))))
(and root (cons 'non-vc root))))
(add-to-list 'project-find-functions #'iensu--locate-non-vc-project)
(cl-defmethod project-root ((project (head non-vc)))
"Handle `non-vc' projects, i.e. projects which are not version controlled."
(cdr project)))
;; Force all ediff windows to be in the same frame
(setopt ediff-window-setup-function 'ediff-setup-windows-plain)
;;;; IDE features
;; Highlight TODOs in programming buffers
(use-package hl-todo :hook ((prog-mode . hl-todo-mode)))
;;;;; Autocompletion and intellisense
;; Corfu for completions
(use-package corfu
:custom
(corfu-cycle t)
(corfu-auto t)
(corfu-auto-prefix 2)
(corfu-auto-delay 0.5)
(corfu-quit-at-boundary 'separator)
(corfu-echo-documentation 0.25)
(corfu-preview-current 'insert)
(corfu-preselect-first t)
:init
(global-corfu-mode 1)
(corfu-echo-mode 1)
(corfu-history-mode 1)
:config
(add-hook 'emacs-lisp-mode-hook (lambda ()
(setopt corfu-auto nil))))
(use-package cape
:bind (("H-c p" . completion-at-point)
("H-c t" . complete-tag)
("H-c d" . cape-dabbrev)
("H-c h" . cape-history)
("H-c f" . cape-file)
("H-c k" . cape-keyword)
("H-c s" . cape-symbol)
("H-c a" . cape-abbrev)
("H-c i" . cape-ispell)
("H-c l" . cape-line)
("H-c w" . cape-dict)
("H-c \\" . cape-tex)
("H-c _" . cape-tex)
("H-c ^" . cape-tex)
("H-c &" . cape-sgml)
("H-c r" . cape-rfc1345))
:init
(add-to-list 'completion-at-point-functions #'cape-dabbrev)
(add-to-list 'completion-at-point-functions #'cape-file))
(use-package vertico
:init
(vertico-mode)
(setopt vertico-cycle t))
(use-package savehist
:init
(savehist-mode 1))
(use-package orderless
:init
(setopt completion-styles '(orderless basic)
completion-category-overrides '((file (styles partial-completion))))
(setq completion-category-defaults nil))
;; https://github.com/minad/vertico#configuration
(use-package emacs
:init
(defun crm-indicator (args)
(cons (format "[CRM%s] %s"
(replace-regexp-in-string
"\\`\\[.*?]\\*\\|\\[.*?]\\*\\'" ""
crm-separator)
(car args))
(cdr args)))
(advice-add #'completing-read-multiple :filter-args #'crm-indicator)
(setopt minibuffer-prompt-properties
'(read-only t cursor-intangible t face minibuffer-prompt))
(add-hook 'minibuffer-setup-hook #'cursor-intangible-mode)
(setopt read-extended-command-predicate #'command-completion-default-include-p)
;; Enable recursive minibuffers
(setopt enable-recursive-minibuffers t))
(pretty-hydra-define prog-mode-hydra
(:color teal :quit-key "q" :title "Programming")
("Exploration"
(("l" xref-find-references "list references")
("d" eldoc-doc-buffer "describe symbol")
("e" flymake-show-buffer-diagnostics "list buffer errors")
("å" flymake-goto-previous-error "goto previous error in buffer")
("ä" flymake-goto-next-error "goto next error in buffer ")
("E" flymake-show-project-diagnostics "list workspace errors"))))
(define-key prog-mode-map (kbd "C-c l") 'prog-mode-hydra/body)
(define-key prog-mode-map (kbd "M-<RET>") 'default-indent-new-line)
(use-package eglot
:bind (:map eglot-mode-map
("C-c l" . eglot-hydra/body))
:pretty-hydra
((:title "Eglot" :quit-key "q" :color teal)
("Exploration"
(("l" xref-find-references "list references")
("d" eldoc-doc-buffer "describe symbol")
("e" flymake-show-buffer-diagnostics "list buffer errors")
("å" flymake-goto-previous-error "goto previous error in buffer")
("ä" flymake-goto-next-error "goto next error in buffer ")
("E" flymake-show-project-diagnostics "list workspace errors"))
"Refactoring"
(("a" eglot-code-actions "execute code action")
("n" eglot-rename "rename symbol")
("i" eglot-code-actions-organize-imports "organize imports")
("f" eglot-format-buffer "format buffer"))
"Misc"
(("w" eglot-reconnect "Reconnect to LSP server"))))
:config
(setopt eglot-confirm-server-initiated-edits nil))
(use-package lsp-mode
:bind (:map lsp-mode-map
("C-c l" . lsp-mode-hydra/body))
:pretty-hydra
((:title "LSP" :quit-key "q" :color teal)
("Exploration"
(("l" xref-find-references "list references")
("d" eldoc-doc-buffer "describe symbol")
("e" flymake-show-buffer-diagnostics "list buffer errors")
("å" flymake-goto-previous-error "goto previous error in buffer")
("ä" flymake-goto-next-error "goto next error in buffer ")
("E" flymake-show-project-diagnostics "list workspace errors"))
"Refactoring"
(("a" lsp-execute-code-action "execute code action")
("n" lsp-rename "rename symbol")
("i" lso-organize-imports "organize imports")
("f" lsp-format-buffer "format buffer"))
"Misc"
(("w" lsp-workspace-restart "Reconnect to LSP server")))))
(use-package lsp-ui :commands lsp-ui-mode
:bind
(:map lsp-mode-map
("C-c C-ä" . lsp-ui-doc-focus-frame))
(:map lsp-ui-doc-frame-mode-map
("q" . lsp-ui-doc-unfocus-frame))
:config
(setopt lsp-ui-sideline-show-hover nil
lsp-ui-sideline-show-symbol nil
lsp-ui-sideline-show-diagnostics t
lsp-ui-doc-show-with-cursor t
lsp-ui-doc-delay 1
lsp-ui-doc-max-height 40
lsp-ui-doc-max-width 100)
(defun iensu--maybe-lsp-format-buffer()
(when (and lsp-mode (not prettier-js-mode))
(lsp-format-buffer)))
(add-hook 'before-save-hook #'iensu--maybe-lsp-format-buffer))
;; Autoformatting
(use-package prettier-js)
;; HTTP requests
(use-package restclient
:mode (("\\.rest$" . restclient-mode)
("\\.restclient$" . restclient-mode)
("\\.http$" . restclient-mode))
:hook (restclient-mode . outline-minor-mode)
:config
(setq outline-regexp "[#]+"))
(use-package hurl-mode
:vc (hurl-mode :url "https://github.com/JasZhe/hurl-mode")
:mode (("\\.hurl$" . hurl-mode)))
(use-package direnv
:config
(direnv-mode)
;; Handle .direnv as shell file
(add-to-list 'auto-mode-alist '("\\.envrc\\'" . sh-mode))