about summary refs log tree commit diff
path: root/config/emacs/config.org
blob: d0a2ce3a9342fdaf57e4dd14521b532ce79df33e (plain)
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
#+TITLE: My Emacs Config
#+AUTHOR: Venomade

This is my Emacs config that is theoretically going to be simpler than my previous configs. I have been trying out neovim more recently and I liked how simple it can be, giving it very quick startup times. I've tried the Emacs Daemon in the past but have never really jived with it so I would rather just have a fast launch time instead. The target is that `emacs-init-time` should be <2 seconds.

* Non-Package Configuration
This section is the main non-package configuration of my Emacs config, everything here should be run before everything else in order to set up the basic environment for using packages.

** Internal
#+begin_src emacs-lisp
  ;; Setup Native Compilation if available
  (if (native-comp-available-p)
      (setq package-native-compile t))

  ;; Disable the command key on MacOS
  (setq mac-command-modifier nil)

  ;; Use a custom.el file instead of init.el to store uncommitable changes
  (setq custom-file (expand-file-name "custom.el" user-emacs-directory))
  (when (file-exists-p custom-file)
    (load custom-file))

  ;; Set Backups, Autosaves and Lockfiles to all be stored in the .emacs.d directory
  (let* ((backup-dir (expand-file-name "backups/" user-emacs-directory))
         (autosave-dir (expand-file-name "autosaves/" user-emacs-directory))
         (lock-dir (expand-file-name "lock/" user-emacs-directory)))
    (make-directory backup-dir t)
    (make-directory autosave-dir t)
    (make-directory lock-dir t)

    (setq backup-directory-alist `((".*" . ,(directory-file-name backup-dir)))
          auto-save-file-name-transforms `((".*" ,(directory-file-name autosave-dir) t))
          lock-file-name-transforms `((".*" ,(directory-file-name lock-dir) t))))

  ;; Don't show Warning and Compile-Log buffers in a window
  (add-to-list 'display-buffer-alist
               '("\\`\\*\\(Warnings\\|Compile-Log\\)\\*\\'"
                 (display-buffer-no-window)
                 (allow-no-window . t)))

  ;; Use 'trash' to trash system files instead of permanently rm-ing
  (setq trash-directory "~/.Trash")
  (defun system-move-file-to-trash (file)
    "Use \"trash\" to move FILE to the system trash."
    (cl-assert (executable-find "trash") nil "'trash' executable not found.")
    (call-process "trash" nil 0 nil "-F"  file))
#+end_src

** User Interface
#+begin_src emacs-lisp
        ;; Allow any size of the Emacs GUI
  (setq frame-resize-pixelwise t
        ;; Configure line numbers display
        display-line-numbers-width-start t
        display-line-numbers-width 4
        display-line-numbers-grow-only t
        ;; Disable bell
        ring-bell-function 'ignore
        ;; Confirm on C-x C-c
        ns-confirm-quit t
        ;; Don't open files opened from an external source in a new frame
        ns-pop-up-frames nil
        ;; Add a scroll margin and improve scrolling
        scroll-margin 10 ; Doesn't work with smooth-scrolling
        scroll-conservatively 100
        scroll-step 1)

  ;; Default to no wrapping on buffers
  (setq-default truncate-lines t)

  ;; Replace the $ signs for truncation with spaces
  (set-display-table-slot standard-display-table 'truncation 32)

  ;; Disable extra GUI interfaces
  (tool-bar-mode -1)
  (scroll-bar-mode -1)
  (menu-bar-mode -1)

  ;; Display line numbers from startup
  (add-hook 'emacs-startup-hook #'global-display-line-numbers-mode)

  ;; Focus new frames on creation
  (add-hook 'after-make-frame-functions
            (lambda (f)
              (select-frame-set-input-focus f)))

  ;; Custom quit messages from DOOM and DOOM Emacs
  (setq quit-messages
          `(; from DOOM 1
            ,(format "I wouldn't leave if I were you. %s is much worse."
                     (if (featurep 'windows-nt) "DOS" "UNIX"))
            "Ya know, next time you come in here I'm gonna toast ya."
            "Go ahead and leave. See if I care."
            "Are you sure you want to quit this great editor?"
            ; from DOOM Emacs
            "(setq nothing t everything 'permitted)"
            "Emacs will remember that."
            "Emacs, Emacs never changes."
            "Hey! Hey, M-x listen!"
            "Wake up, Mr. Stallman. Wake up and smell the ashes."
            "You are *not* prepared!"
            "Please don't go. The drones need you. They look up to you."))

  (defun random-quit-message ()
    "Return a randomly chosen quit message from `quit-messages'."
    (nth (random (length quit-messages)) quit-messages))

  (defun message-confirm-kill-emacs (&rest _)
    "Prompt the user with a random message before quitting.
  Returns t to allow kill if the user answers yes; nil otherwise."
    (let* ((msg (random-quit-message))
           (prompt (format "%s  Really quit Emacs? " msg)))
      (yes-or-no-p (propertize prompt 'face '(italic default)))))

  (setq confirm-kill-emacs #'message-confirm-kill-emacs)

  ;; Enable which-key on startup
  (add-hook 'emacs-startup-hook #'which-key-mode)

  ;; Disable line numbers in fundamental mode
  (let ((no-numbers-modes '(fundamental-mode-hook)))
    (dolist (hk no-numbers-modes)
      (add-hook hk (lambda () (display-line-numbers-mode 0)))))

  ;; Colorise the compile-mode buffer
  (require 'ansi-color)
  (defun colorize-compilation-buffer ()
    (ansi-color-apply-on-region compilation-filter-start (point)))
  (add-hook 'compilation-filter-hook 'colorize-compilation-buffer)

  ;; Shorten symbols into their single character equivalent
  (global-prettify-symbols-mode 1)

  ;; Font Setup
  (let* ((fontconf '((fixed-font    . "AporeticSansMonoNerdFont")
                     (variable-font . "Google Sans")
                     (size          . 16)))
         (fixed-font    (cdr (assoc 'fixed-font fontconf)))
         (variable-font (cdr (assoc 'variable-font fontconf)))
         (pt-size       (cdr (assoc 'size fontconf)))
         (height        (* pt-size 10))
         (frame-font    (concat fixed-font "-" (number-to-string pt-size))))
    ; Fixed-width faces
    (dolist (face '(default fixed-pitch))
      (set-face-attribute face nil
                          :font fixed-font
                          :height height
                          :weight 'regular))
    ; Variable-pitch face
    (set-face-attribute 'variable-pitch nil
                        :font variable-font
                        :height height
                        :weight 'regular)

    ; Syntax styling
    (set-face-attribute 'font-lock-comment-face nil :slant 'italic)
    (set-face-attribute 'font-lock-keyword-face nil :slant 'italic)

    ; Frame font
    (add-to-list 'default-frame-alist `(font . ,frame-font)))

  ;; Spacious user interface elements
  (use-package spacious-padding
    :ensure t
    :hook (after-init . spacious-padding-mode)
    :config
    (setq spacious-padding-widths
          '( :internal-border-width 8
             :header-line-width 4
             :mode-line-width 6
             :custom-button-width 3
             :tab-width 4
             :right-divider-width 30
             :scroll-bar-width 8
             :fringe-width 0))
    (setq spacious-padding-subtle-frame-lines
        '( :mode-line-active spacious-padding-line-active
           :mode-line-inactive spacious-padding-line-inactive
           :header-line-active spacious-padding-line-active
           :header-line-inactive spacious-padding-line-inactive)))

  ;; Enable line wrapping for text documents
  (add-hook 'text-mode-hook 'visual-line-mode)
  ;; Disable line numbers in text mode
  (add-hook 'text-mode-hook (lambda () (display-line-numbers-mode -1)))
#+end_src

** Functionality
#+begin_src emacs-lisp
                ;; Indent with spaces
  (setq-default indent-tabs-mode nil
  	      ;; Use 2 spaces
                tab-width 2
                tab-stop-list (number-sequence 2 100 2)
                c-basic-offset 2)

  ;; Regex find and replace shortcut
  (keymap-global-set "C-c r" 'replace-regexp)

  ;; Functions to match vi's 'o' and 'O'
  (cl-labels
      ((vi-open-line-above ()
         "Open line above the current line."
         (interactive)
         (unless (bolp) (beginning-of-line))
         (newline)
         (forward-line -1)
         (indent-according-to-mode))
       (vi-open-line-below ()
         "Open line below the current line."
         (interactive)
         (unless (eolp) (end-of-line))
         (newline-and-indent)))
    (define-key (current-global-map) (kbd "C-c o") #'vi-open-line-above)
    (define-key (current-global-map) (kbd "C-c O") #'vi-open-line-below)
    (which-key-add-key-based-replacements "C-c o" "open-line-above")
    (which-key-add-key-based-replacements "C-c O" "open-line-below"))

  ;; Move to the beginning of the line with respect to context
  (progn
    (defun smart-beginning-of-line ()
      "Move the cursor to the beginning of text on the line."
      (interactive)
      (let ((initial-point (point)))
        (back-to-indentation)
        (when (eq initial-point (point))
          (move-beginning-of-line 1))))
    (defun setup-prog-mode-c-a ()
      (local-set-key (kbd "C-a") 'smart-beginning-of-line))
    (add-hook 'prog-mode-hook 'setup-prog-mode-c-a)
    (add-hook 'conf-mode-hook 'setup-prog-mode-c-a))

  ;; Clean up trailing whitespace
  (progn
    (defun prog-nuke-trailing-whitespace ()
      "Removes trailing whitespace at the end of the line."
      (when (or (derived-mode-p 'prog-mode) (derived-mode-p 'conf-mode))
        (delete-trailing-whitespace)))
    (add-hook 'before-save-hook 'prog-nuke-trailing-whitespace))

  ;; Enable overwriting selection with the built-in delsel package
  (use-package delsel
      :hook (after-init . delete-selection-mode))

  ;; Use y/n and other similar shortenings for dialogue response
  (setopt use-short-answers t)

  ;; Configure the built-in file-manager dired
  (use-package dired
    :commands (dired)
    :hook
    ((dired-mode . dired-hide-details-mode)
     (dired-mode . hl-line-mode))
    :config
    (setq dired-recursive-copies 'always
          dired-recursive-deletes 'always
          ; delete-by-moving-to-trash t ; FIXME: Currently Broken, need to find why
          dired-dwim-target t
          insert-directory-program "~/.nix-profile/bin/ls"
          dired-listing-switches "-lah"))

  ;; Return to last position after leaving a file
  (add-hook 'emacs-startup-hook #'save-place-mode)

  ;; Setup a pretty and useful eshell prompt
  (progn
    (dolist (spec
             '((eshell-prompt-user-face        font-lock-keyword-face   "Face for the username in the eshell prompt.")
               (eshell-prompt-venv-face        font-lock-string-face    "Face for the virtualenv name in the eshell prompt.")
               (eshell-prompt-dir-face         dired-directory          "Face for the directory path in the eshell prompt.")
               (eshell-prompt-git-face         magit-tag                "Face for the git branch in the eshell prompt.")
               (eshell-prompt-git-dirty-face   eshell-prompt-git-face   "Face for the git dirty status in the eshell prompt.")
               (eshell-prompt-symbol-face      font-lock-builtin-face   "Face for the prompt symbol in the eshell prompt.")))
      (cl-destructuring-bind (name inherit doc) spec
        (defface name
          `((t (:inherit ,inherit)))
          doc
          :group 'eshell-prompt)))

    (defun eshell-abbreviate-dir (dir)
      "Abbreviate directory to show only the first and the last two components."
      (let* ((abbrev (abbreviate-file-name dir))
             (components (split-string abbrev "/" t))
             (num-components (length components)))
        (if (>= num-components 4)
            (concat (car components) "/.../"
                    (mapconcat #'identity (last components 2) "/"))
          abbrev)))

    (defun eshell-prompt-git-branch ()
      "Return the current git branch or short commit hash, if available."
      (when (and (executable-find "git")
                 (locate-dominating-file default-directory ".git"))
        (with-temp-buffer
          (let ((ret (call-process "git" nil t nil "symbolic-ref" "--short" "HEAD")))
            (if (zerop ret)
                (string-trim (buffer-string))
              (when (zerop (call-process "git" nil t nil "rev-parse" "--short" "HEAD"))
                (string-trim (buffer-string))))))))

    (defun eshell-prompt-git-dirty ()
      "Return a dirty flag (✗ if dirty, ✓ if clean) for the git repository."
      (when (eshell-prompt-git-branch)
        (with-temp-buffer
          (call-process "git" nil t nil "status" "--porcelain")
          (if (> (buffer-size) 0) "✗" "✓"))))

    (defun eshell-prompt ()
      "Custom eshell prompt."
      (let* ((user (propertize (user-login-name) 'face 'eshell-prompt-user-face))
             (venv (when-let ((venv (getenv "VIRTUAL_ENV")))
                     (concat (propertize "(" 'face 'eshell-prompt-venv-face)
                             (propertize (file-name-nondirectory venv) 'face 'eshell-prompt-venv-face)
                             (propertize ")" 'face 'eshell-prompt-venv-face))))
             (path (propertize (eshell-abbreviate-dir (eshell/pwd)) 'face 'eshell-prompt-dir-face))
             (git-branch (eshell-prompt-git-branch))
             (git-info (when git-branch
                         (concat " " (propertize "on" 'face 'font-lock-number-face) " "
                                 (propertize git-branch 'face 'eshell-prompt-git-face)
                                 (propertize (eshell-prompt-git-dirty) 'face 'eshell-prompt-git-dirty-face))))
             (prompt (concat user " " (propertize "in" 'face 'font-lock-number-face) " "
                             (if venv (concat venv " ") "")
                             path git-info "\n"
                             (propertize "λ" 'face 'eshell-prompt-symbol-face) " ")))
        prompt))

    (setq eshell-prompt-function 'eshell-prompt
          eshell-prompt-regexp "^[^λ\n]*λ "))

  ;; Bind recent files to C-x C-r
  (add-hook 'emacs-startup-hook #'recentf-mode)
  (keymap-global-set "C-x C-r" 'recentf-open)
  (which-key-add-key-based-replacements "C-x C-r" "Recent Files")
  (put 'compile-command 'safe-local-variable #'stringp)

  ;; Persistently save minibuffer history between sessions
  (use-package savehist
    :hook (after-init . savehist-mode))

  ;; Local info files directory
  (add-to-list 'Info-directory-list "/Users/venomade/Documents/Info/")

  ;; Scroll on compilation mode output
  (setq compilation-scroll-output t)

  ;; Add keybinds for compiling
  (keymap-global-set "C-c m c" 'compile)
  (keymap-global-set "C-c m r" 'recompile)
  (which-key-add-key-based-replacements "C-c m" "make")
#+end_src

* Basic Package Configuration
This is the configuration for the necessary packages required for my base Emacs experience for all cases.

** Setup
#+begin_src emacs-lisp
  ;; Setup Melpa
  (require 'package)
  (add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
  (package-initialize)
#+end_src

** Internal Packages
#+begin_src emacs-lisp
  ;; Improved Garbage Collection
  (use-package gcmh
    :ensure t
    :hook (after-init . gcmh-mode)
    :config
    (setq gcmh-high-cons-threshold (* 128 1024 1024)  ; 128MB
          gcmh-idle-delay 10
          gcmh-verbose nil))

  ;; Find executables that are usable from the specified shell
  (use-package exec-path-from-shell
    :ensure t
    :config
    (setq exec-path-from-shell-shell-name "/bin/zsh"
          exec-path-from-shell-arguments '("-l" "-i" "-c"))
    (when (memq window-system '(mac ns x))
      (exec-path-from-shell-initialize)))
#+end_src

** User Interface Packages
#+begin_src emacs-lisp
  ;; Catppuccin Color Theme
  (use-package catppuccin-theme
    :ensure t
    :config
    (setq catppuccin-flavor 'mocha)
    (catppuccin-set-color 'base     "#000000")
    (catppuccin-set-color 'mantle   "#181818")
    (catppuccin-set-color 'crust    "#111111")
    (catppuccin-set-color 'overlay2 "#9B9B9B")
    (catppuccin-set-color 'overlay1 "#858585")
    (catppuccin-set-color 'overlay0 "#707070")
    (catppuccin-set-color 'surface2 "#5C5C5C")
    (catppuccin-set-color 'surface1 "#474747")
    (catppuccin-set-color 'surface0 "#333333")
    (catppuccin-reload)
    (load-theme 'catppuccin t))

  ;; Automatically match the titlebar to the theme
  (use-package ns-auto-titlebar
    :ensure t
    :config
    (when (eq system-type 'darwin) (ns-auto-titlebar-mode)))

  ;; More readable modeline from DOOM Emacs
  (use-package doom-modeline
    :ensure t
    :hook (after-init . doom-modeline-mode)
    :config
    (setq doom-modeline-height 0 ; minimum
          doom-modeline-bar-width 0 ; disabled
          doom-modeline-project-detection 'project ; use project.el
          doom-modeline-buffer-state-icon nil
          doom-modeline-highlight-modified-buffer-name nil
          doom-modeline-percent-position nil
          line-number-mode nil
          doom-modeline-buffer-encoding nil
          doom-modeline-time-live-icon nil
          doom-modeline-time-icon nil
          display-time-default-load-average nil
          display-time-format "%a %d %b | %R"
          helm-ag-show-status-function nil)) ;HACK: Doom Modeline asks for this but I don't use helm

  ;; Icons for use with a nerd-font
  (use-package nerd-icons
    :ensure t
    :if (display-graphic-p))

  ;; Nerd Icons in the file manager
  (use-package nerd-icons-dired
    :ensure t
    :after nerd-icons
    :hook
    (dired-mode . nerd-icons-dired-mode))

  ;; Nerd Icons in the buffer menu
  (use-package nerd-icons-ibuffer
    :ensure t
    :after nerd-icons
    :hook (ibuffer-mode . nerd-icons-ibuffer-mode))

  ;; Color brackets for visual grepping
  (use-package rainbow-delimiters
    :ensure t
    :hook (prog-mode . rainbow-delimiters-mode))

  ;; Highlight todo comments
  (use-package hl-todo
    :ensure t
    :hook ((org-mode . hl-todo-mode)
           (prog-mode . hl-todo-mode))
    :config
    (add-to-list 'hl-todo-keyword-faces '("ERROR" . "#8c5353")))

  ;; Highlight large changes
  (use-package goggles
    :ensure t
    :hook ((prog-mode text-mode) . goggles-mode)
    :config
    (setq-default goggles-pulse t))

  ;; Nicer view to manager project.el projects
  (use-package disproject
    :ensure t
    :after project
    :bind (:map ctl-x-map
                ("p" . disproject-dispatch)))

  ;; Sort buffers by project in the buffer manager
  (use-package ibuffer-project
    :ensure t
    :after project
    :config
    (add-hook
     'ibuffer-hook
     (lambda ()
       (setq ibuffer-filter-groups (ibuffer-project-generate-filter-groups))
       (unless (eq ibuffer-sorting-mode 'project-file-relative)
         (ibuffer-do-sort-by-project-file-relative))))
    (keymap-global-set "C-x C-b" 'ibuffer))
#+end_src

** Functionality Packages
#+begin_src emacs-lisp
  ;; Move text up and down with a single keybind
  (use-package move-text
    :ensure t
    :config
    (keymap-global-set "M-p" 'move-text-up)
    (keymap-global-set "M-n" 'move-text-down)
    (defun indent-region-advice (&rest ignored)
      (let ((deactivate deactivate-mark))
        (if (region-active-p)
            (indent-region (region-beginning) (region-end))
          (indent-region (line-beginning-position) (line-end-position)))
        (setq deactivate-mark deactivate)))

    (advice-add 'move-text-up :after 'indent-region-advice)
    (advice-add 'move-text-down :after 'indent-region-advice))

  ;; Automatically insert matching delimiters and jump between them
  (use-package smartparens
    :ensure t
    :hook (prog-mode text-mode markdown-mode)
    :config
    (require 'smartparens-config)
    (sp-use-paredit-bindings))

  ;; Undo across history and across revisions
  (use-package undo-tree
    :ensure t
    :init
    (global-undo-tree-mode 1)
    :config
    (setq undo-tree-auto-save-history t
          undo-tree-history-directory-alist `(("." . ,(expand-file-name ".cache" user-emacs-directory)))))

  ;; Insert accents with a menu
  (use-package accent
    :ensure t
    :bind* (("C-'" . accent-menu))
    :config
    (setq accent-diacritics '((a (á ã â à)) ; só para Português
                              (e (é ê))
                              (i (í))
                              (o (ó õ ô))
                              (u (ú))
                              (c (ç))
                              (A (Á Ã Â À))
                              (E (É Ê))
                              (I (Í))
                              (O (Ó Õ Ô))
                              (U (Ú)))))

  ;; Allow recursive expansion in the dired file-manager
  (use-package dired-subtree
    :ensure t
    :after dired
    :bind
    (:map dired-mode-map
          ("TAB" . (lambda () (interactive) (dired-subtree-toggle) (revert-buffer)))
          ("<backtab>" . dired-subtree-remove))
    :config
    (setq dired-subtree-use-backgrounds nil))
#+end_src

* Advanced Package Configuration
This configuration is for specific filetypes/languages and more advanced packages. It is sorted by module for easy enabling and disabling, and ordered by relevance to most modes.

** Corfu Completion
#+begin_src emacs-lisp
  ;; Make completion matching much more powerful
  (use-package orderless
    :ensure t
    :config
    (setq completion-styles '(orderless basic)
          completion-category-defaults nil
          completion-category-overrides '((file (styles . (partial-completion)))
                                          (eglot (styles . (orderless))))))

  ;; Snippet expansion allows for powerful multi-stage completion
  (use-package yasnippet
    :ensure t
    :hook (prog-mode . yas-minor-mode)
    :bind
    (("C-c y n" . yas-new-snippet)
     ("C-c y v" . yas-visit-snippet-file)
     ("C-c y i" . yas-insert-snippet))
    :config
    (yas-reload-all))

  ;; Add some base snippets
  (use-package yasnippet-snippets
    :after yasnippet
    :ensure t)

  ;; Combiner for multiple completion backends
  (use-package cape
    :ensure t
    :config
    ;; TODO find out what the wrap buster actually does (caching or whatever)
    (advice-add 'eglot-completion-at-point :around #'cape-wrap-buster))

  (use-package yasnippet-capf
    :ensure t
    :after cape)

  (defun eglot-capf-with-yasnippet ()
    (setq-local completion-at-point-functions
                (list
                 (cape-capf-super
                  #'eglot-completion-at-point
                  #'yasnippet-capf))))
  (with-eval-after-load 'eglot
    (add-hook 'eglot-managed-mode-hook #'eglot-capf-with-yasnippet))

  ;; The fast completion menu
  (use-package corfu
    :ensure t
    :hook ((prog-mode . corfu-mode)
           (eshell-mode . corfu-mode))
    :bind (:map corfu-map
                ("TAB" . corfu-insert)
                ("<tab>" . corfu-insert)
                ("RET" . nil)
                ("C-n" . corfu-next)
                ("C-p" . corfu-previous)
                ("C-g" . corfu-quit))
    :config
    (setq corfu-cycle t
          corfu-auto t
          corfu-auto-delay 0.2
          corfu-preselect 'first
          corfu-quit-at-boundary t
          corfu-quit-no-match t
          corfu-scroll-margin 5
          corfu-tab-always-indent 'complete
          corfu-preview-current nil
          corfu-min-width 20
          corfu-popupinfo-delay '(1.25 . 0.5)
          corfu-on-exact-match nil)
    (corfu-popupinfo-mode 1)
    (with-eval-after-load 'savehist
      (corfu-history-mode 1)
      (add-to-list 'savehist-additional-variables 'corfu-history)))

  (use-package nerd-icons-corfu
    :ensure t
    :after nerd-icons corfu
    :config
    (add-to-list 'corfu-margin-formatters #'nerd-icons-corfu-formatter))
#+end_src

** Miscellaneous Completion
#+begin_src emacs-lisp
  ;; Powerful minibuffer completion
  (use-package vertico
    :ensure t
    :hook (after-init . vertico-mode)
    :bind (:map vertico-map
                ("DEL" . vertico-directory-delete-char)))

  ;; Powerful completion replacements for various other emacs functions
  (use-package consult
    :ensure t
    :bind (("M-g g" . consult-goto-line)
           ("M-g M-g" . consult-goto-line)
           ("C-c s" . consult-line))

    :config
    (which-key-add-key-based-replacements "C-c s" "search-lines"))

  ;; Powerful completion replacements for find-style emacs functions
  (use-package affe
    :ensure t
    :after consult
    :bind (("C-c f f" . affe-find)
           ("C-c f g" . affe-grep))
    :init
    (which-key-add-key-based-replacements "C-c f" "find-file")
    (which-key-add-key-based-replacements "C-c f f" "find-fuzzy")
    (which-key-add-key-based-replacements "C-c f g" "find-greppy"))

  ;; Add descriptive annotations to minibuffer completions
  (use-package marginalia
    :ensure t
    :after vertico
    :config (marginalia-mode 1))

  ;; Add nerd-font icons to completions in minibuffer
  (use-package nerd-icons-completion
    :ensure t
    :after vertico marginalia
    :hook
    (marginalia-mode . nerd-icons-completion-marginalia-setup)
    (after-init . nerd-icons-completion-mode))
#+end_src

** Eglot
#+begin_src emacs-lisp
  (use-package eglot
    :ensure t
    :after cape
    :config
    ;; Disable progress indicator in the modeline
    (setq eglot-report-progress nil
          eglot-extend-to-xref t))

  ;; Use a rust binary to do lsp communication and then send results to emacs
  ; Booster at https://github.com/blahgeek/emacs-lsp-booster
  (use-package eglot-booster
    :vc (:url "https://github.com/jdtsmith/eglot-booster.git")
    :ensure t
  	:after eglot
  	:config
    ;; Use emacs-30's built in JSON handler
    (setq eglot-booster-io-only t)
    (eglot-booster-mode))

  ;; Jump to workspace symbols from the LSP with consult
  (use-package consult-eglot
    :ensure t
    :after eglot)

  ;; Install markdown support for eldoc
  (use-package markdown-mode
    :ensure t)

  ;; Popup lsp documentation information in a floating box
  (use-package eldoc-box
    :ensure t)
  ;; Follow markdown links inside eldoc boxes on selection
  (defun markdown-follow-help-or-link-at-point-advice (orig-fun &rest args)
    "Prefer to use the help-echo property as `browse-url' target."
    (let* ((event-win (posn-window (event-start last-input-event)))
           (help-echo (with-selected-frame (window-frame event-win)
                        (with-current-buffer (window-buffer event-win)
                          (get-text-property (point) 'help-echo))))
           (help-is-url (url-type (url-generic-parse-url help-echo))))
      (message "if %s (browse-url %S)" help-is-url help-echo)
      (if help-is-url
          (browse-url help-echo)
        (apply orig-fun args))))
  (advice-add 'markdown-follow-link-at-point :around #'markdown-follow-help-or-link-at-point-advice)

  ;; Menu for eglot and related functions
  (require 'transient)
  (transient-define-prefix eglot-transient ()
    "Transient menu for Eglot."
    [["Server"
      ("c" "Reconnect" eglot-reconnect)
      ("q" "Shutdown" eglot-shutdown)
      ("I" "Server Info" eglot-show-workspace-configuration)]
     ["Navigation"
      ("g" "Go to Definition" xref-find-definitions)
      ("d" "Describe Thing at Point" eldoc-box-help-at-point)
      ("D" "Go to Declaration" eglot-find-declaration)
      ("i" "Go to Implementation" eglot-find-implementation)
      ("f" "Find References" xref-find-references)
      ("t" "Type Definition" eglot-find-typeDefinition)
      ("s" "Search Symbols"
       (lambda ()
         (interactive)
         (if (eq major-mode 'java-mode)
             (call-interactively 'consult-eglot-jdt-symbols)
           (call-interactively 'consult-eglot-symbols))))]
     ["Code Actions"
      ("a" "Code Actions" eglot-code-actions)
      ("q" "Quick Fix" eglot-code-action-quickfix)
      ("=" "Format Buffer" eglot-format-buffer)
      ("o" "Organize Imports" eglot-code-action-organize-imports)
      ("r" "Rename Symbol" eglot-rename)]
     ["Diagnostics"
      ("l" "List Diagnostics" flycheck-list-errors)
      ("L" "Project Diagnostics" flymake-show-project-diagnostics)]]
    (interactive)
     (transient-setup 'eglot-transient))

  (define-key eglot-mode-map (kbd "C-c l") #'eglot-transient)

  ;; Display error and warning highlighting from eglot
  (use-package flycheck
    :ensure t
    :config
    (global-flycheck-mode 1))

  (use-package flycheck-eglot
    :ensure t
    :after (flycheck eglot)
    :custom (flycheck-eglot-exclusive nil)
    :config
    (global-flycheck-eglot-mode 1))
#+end_src

** Org Mode
#+begin_src emacs-lisp
        ;; Open source blocks in the current window
  (setq org-src-window-setup 'current-window
        ;; Don't confirm when evaluating a code block
        org-confirm-babel-evaluate nil
        ;; Make org documents more seamless by hiding markup
        org-hide-emphasis-markers t)

  ;; A nicer org-bullets for headers
  (use-package org-superstar
    :ensure t
    :hook (org-mode . org-superstar-mode)
    :custom
    (org-superstar-remove-leading-stars t)
    (org-superstar-headline-bullets-list
     '("•" "•" "•" "•" "•")))

  ;; Specify the variable font for org-mode text
  (let* ((variable-tuple
          (cond ((x-list-fonts "Google Sans")     '(:font "Google Sans"))
                ((x-family-fonts "Sans Serif")    '(:family "Sans Serif"))
                (nil (warn "Cannot find a Sans Serif Font.  Install Google Sans."))))
         (base-font-color     (face-foreground 'default nil 'default))
         (headline           `(:inherit default :weight bold :foreground ,base-font-color)))

    (custom-theme-set-faces
     'user
     `(org-level-8 ((t (,@headline ,@variable-tuple))))
     `(org-level-7 ((t (,@headline ,@variable-tuple))))
     `(org-level-6 ((t (,@headline ,@variable-tuple))))
     `(org-level-5 ((t (,@headline ,@variable-tuple))))
     `(org-level-4 ((t (,@headline ,@variable-tuple :height 1.1))))
     `(org-level-3 ((t (,@headline ,@variable-tuple :height 1.2))))
     `(org-level-2 ((t (,@headline ,@variable-tuple :height 1.3))))
     `(org-level-1 ((t (,@headline ,@variable-tuple :height 1.4))))
     `(org-document-title ((t (,@headline ,@variable-tuple :height 1.6 :underline nil))))))

  ;; Enable a non-monospace font for org documents
  (add-hook 'org-mode-hook 'variable-pitch-mode)
  ;; Enable line wrapping for org documents
  (add-hook 'org-mode-hook 'visual-line-mode)
  ;; Disable line numbers in org mode
  (add-hook 'org-mode-hook (lambda () (display-line-numbers-mode -1)))

  ;; Theme the fonts and styles for various org elements
  (custom-theme-set-faces
   'user
   '(org-block ((t (:inherit fixed-pitch))))
   '(org-code ((t (:inherit (shadow fixed-pitch)))))
   '(org-document-info-keyword ((t (:inherit (shadow fixed-pitch)))))
   '(org-indent ((t (:inherit (org-hide fixed-pitch)))))
   '(org-link ((t (:foreground "#89b4fa" :underline t))))
   '(org-meta-line ((t (:inherit (font-lock-comment-face fixed-pitch)))))
   '(org-property-value ((t (:inherit fixed-pitch))) t)
   '(org-special-keyword ((t (:inherit (font-lock-comment-face fixed-pitch)))))
   '(org-table ((t (:inherit fixed-pitch))))
   '(org-tag ((t (:inherit (shadow fixed-pitch) :weight bold :height 0.8))))
   '(org-verbatim ((t (:inherit (shadow fixed-pitch))))))

  ;; Provides quick snippets for common org templates
                                          ;(add-to-list 'org-modules 'org-tempo)
  (require 'org-tempo)
                                          ; FIXME: Won't enable for some reason,
                                          ; figure out why, and make it lazy

  ;; Open org mode links in current window
  (setq org-link-frame-setup
        '((file . find-file)
          (wl . wl-other-frame)
          (vm . vm-visit-folder-other-frame)
          (gnus . gnus)))

  (add-hook 'org-mode-hook (lambda () (electric-indent-local-mode -1)))
#+end_src

* Language Specific Configuration
These are the configurations for specific programming languages that should be enabled or disabled based on what is needed at any time.

** Lua
#+begin_src emacs-lisp
  (use-package lua-mode
    :ensure t
    :hook (lua-mode . eglot-ensure)
    :config
          ;; Use a 2 space indent level for formatting
    (setq lua-indent-level 2
          ;; Use lua 5.4 for documentation
          lua-documentation-url "https://www.lua.org/manual/5.4/manual.html"))

  ;; Settings for lua_ls
  (setq-default eglot-workspace-configuration
                `(:Lua
                  (:completion (:callSnippet "Replace")
                   :runtime (:version "Lua 5.4"
                             :path ["?.lua"
                                    "?/init.lua"
                                    "./stubs/?.lua"
                                    "./stubs/?/init.lua"])
                   :workspace (:library [,(concat (getenv "HOME") "/.luarocks/share/lua/5.4")
                                         "./stubs"])
                   :diagnostics (:enable t))))

  ; TODO Try fix this only for lua-mode as that is where the bug is
  (setq eglot-ignored-server-capabilities
        '(:documentOnTypeFormattingProvider))
#+end_src

** Nix
#+begin_src emacs-lisp
  (use-package nix-mode
    :ensure t
    :mode "\\.nix\\'"
    :config
    (add-hook 'nix-mode-hook #'eglot-ensure t)
    (add-to-list 'eglot-server-programs '(nix-mode . ("nil"))))
#+end_src

** COMMENT ZK
#+begin_src emacs-lisp
  (use-package tomlparse
    :ensure t)

  (use-package zk4e
    :ensure t
    :after tomlparse
    :vc ( :url "https://codeberg.org/mcookly/zk4e"
          :branch "main"
          :rev :newest)
    :config
    (setq zk4e-notebook-directories '(("Notes" . "~/Documents/Notes")))
    (zk4e-select-notebook "Notes"))
#+end_src

** Denote
#+begin_src emacs-lisp
  (use-package denote
    :ensure t
    :hook (dired-mode . denote-dired-mode)
    :bind
    (("C-c n n" . denote)
     ("C-c n N" . denote-type)
     ("C-c n r" . denote-rename-file)
     ("C-c n l f" . denote-link)
     ("C-c n b" . denote-backlinks)
     ("C-c n g" . denote-grep))
    :config
    (setq denote-directory (expand-file-name "~/Documents/Denotes/"))
    (denote-rename-buffer-mode 1))

  (use-package consult-denote
    :ensure t
    :bind
    (("C-c n f" . consult-denote-find)
     ("C-c n g" . consult-denote-grep))
    :config
    (consult-denote-mode 1))

  ; TODO: Denote Explore https://lucidmanager.org/productivity/denote-explore/

  (use-package denote-menu
    :ensure t
    :bind
    (("C-c n d" . list-denotes))
    :config
    (add-hook 'denote-menu-mode-hook (lambda () (display-line-numbers-mode -1))))

  (use-package denote-org
    :ensure t
    :bind
    (("C-c n l h" . denote-org-link-to-heading)))
#+end_src