aboutsummaryrefslogtreecommitdiff
path: root/config/emacs/config.org
blob: 495f37c7b96e2bf5cd4b493ee3adff72d0bbb63a (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
#+title: Emacs Configuration
* Package Setup
Set up package archives including =melpa=, =org=, and =elpa=.
#+begin_src emacs-lisp
  (require 'package)

  (setq package-archives '(("melpa" . "https://melpa.org/packages/")
    ("org" . "https://orgmode.org/elpa/")
    ("elpa" . "https://elpa.gnu.org/packages/")))

  (package-initialize)
  (unless package-archive-contents
    (package-refresh-contents))
#+end_src

Install =use-package= for declarative package installation. Make =use-package= default to =ensure t= so that packages are enabled if they are declared.
#+begin_src emacs-lisp
  (unless (package-installed-p 'use-package)
    (package-install 'use-package))

  (require 'use-package)
  (setq use-package-always-ensure t)
#+end_src

* User Interface
** Theming
Set the default font to the Source Code Pro nerd font variant. I use size 14 font.
#+begin_src emacs-lisp
  (set-face-attribute 'default t :font "Sauce Code Pro Nerd Font-14")
#+end_src

Use the Doom Nord light theme.
#+begin_src emacs-lisp
  (use-package doom-themes
    :config
    (setq doom-themes-enable-bold t
          doom-themes-enable-italic t)
    (load-theme 'doom-nord-light t)
    (doom-themes-org-config))
#+end_src

Use =doom-modeline= for a nicer modeline.
#+begin_src emacs-lisp
  (use-package doom-modeline
    :init (doom-modeline-mode 1))
#+end_src

** Clean UI
Disable the Emacs start screen and make the =scratch= buffer default to empty.
#+begin_src emacs-lisp
  (setq inhibit-startup-screen t)
  (setq initial-scratch-message nil)
#+end_src

Disable scroll bar, tool bar, and menu bar.
#+begin_src emacs-lisp
  (scroll-bar-mode -1)
  (tool-bar-mode -1)
  (menu-bar-mode -1)
#+end_src

** Fancy Stuff
Use line numbers by default.
#+begin_src emacs-lisp
  (global-display-line-numbers-mode 1)
#+end_src

Highlight changes for an operation with =evil-goggles=.
#+begin_src emacs-lisp
  (use-package evil-goggles
    :after evil
    :config
    (evil-goggles-mode)
    (evil-goggles-use-diff-faces))
#+end_src

Install nerd font icons.
#+begin_src emacs-lisp
  (use-package nerd-icons)
#+end_src

Scroll one line at a time.
#+begin_src emacs-lisp
  (setq scroll-conservatively most-positive-fixnum)
#+end_src

Create parent directories when they don't yet exist.
#+begin_src emacs-lisp
  (defun jj/create-non-existent-directory ()
  (let ((parent-directory (file-name-directory buffer-file-name)))
    (when (and (not (file-exists-p parent-directory))
               (y-or-n-p (format "Directory `%s' does not exist! Create it?" parent-directory)))
      (make-directory parent-directory t))))
  (add-to-list 'find-file-not-found-functions #'jj/create-non-existent-directory)
#+end_src


** Whitespace Management
Use spaces over tabs and set tab width to 4.
#+begin_src emacs-lisp
  (setq-default indent-tabs-mode nil)
  (setq tab-width 4
    c-basic-offset tab-width)
#+end_src

Delete trailing whitespace on save.
#+begin_src emacs-lisp
  (defun jj/before-save-hook ()
    (unless (eql (with-current-buffer (current-buffer) major-mode)
                  'markdown-mode)
      (delete-trailing-whitespace)))
  (add-hook 'before-save-hook #'jj/before-save-hook)
#+end_src

** Backup Management
Don't create backup files.
#+begin_src emacs-lisp
  (setq make-backup-files nil)
#+end_src

** Customize =dired=
Use nerd font icons in =dired=.
#+begin_src emacs-lisp
  (use-package nerd-icons-dired
    :hook dired-mode)
#+end_src

Use colours in =dired= with =diredfl=.
#+begin_src emacs-lisp
  (use-package diredfl
    :init (diredfl-global-mode 1))
#+end_src

* Tools
** Vi Keybindings
Use =vi= keybindings with =evil=. Set the undo system to =undo-fu=. Wrapped lines can be moved between with =j= and =k=.
#+begin_src emacs-lisp
  (use-package evil
    :init
    (setq evil-want-keybinding nil)
    :config
    (evil-mode)
    (evil-global-set-key 'motion "j" 'evil-next-visual-line)
    (evil-global-set-key 'motion "k" 'evil-previous-visual-line)
    :custom
    (evil-undo-system 'undo-fu))
#+end_src

Use =evil-collection= to include =vi= keybindings in extra modes.
#+begin_src emacs-lisp
  (use-package evil-collection
    :after evil
    :config
    (evil-collection-init))
#+end_src

** Lisp Editing
Better Lisp editing with =lispy= and =lispyville=.
#+begin_src emacs-lisp
  (use-package lispy
    :hook emacs-lisp-mode)
  (use-package lispyville
    :after lispy
    :hook lispy-mode)
#+end_src

Better parentheses handling in lisp with =parinfer-rust-mode=.
#+begin_src emacs-lisp
  (use-package parinfer-rust-mode
    :hook emacs-lisp-mode
    :init
    (setq parinfer-rust-auto-download t))
#+end_src

** Undo
Better undo with =undo-fu=.
#+begin_src emacs-lisp
  (use-package undo-fu)
#+end_src

Make undo persistent when closing Emacs with =undo-fu-session=.
#+begin_src emacs-lisp
  (use-package undo-fu-session
    :init (undo-fu-session-global-mode 1))
#+end_src

** Lookup
Better lookup with =dumb-jump=.
#+begin_src emacs-lisp
  (use-package dumb-jump
    :init (add-hook 'xref-backend-functions #'dumb-jump-xref-activate))
#+end_src

** Version Control
Install Magit for Git integration.
#+begin_src emacs-lisp
  (use-package magit)
#+end_src

** Document Viewing
Replace =DocView= with a better document viewer from =pdf-tools=.
#+begin_src emacs-lisp
  (use-package pdf-tools
    :config
    (pdf-tools-install)
    :init
    (add-hook 'pdf-view-mode-hook #'(lambda () (display-line-numbers-mode -1)))
    (add-hook 'TeX-after-compilation-finished-functions
              #'TeX-revert-document-buffer)
    :config
    (setq TeX-view-program-selection '((output-pdf "PDF Tools"))
          TeX-view-program-list '(("PDF Tools" TeX-pdf-tools-sync-view))
          TeX-source-correlate-start-server t))
#+end_src

Save place in PDFs with =saveplace-pdf-view=.
#+begin_src emacs-lisp
  (use-package saveplace-pdf-view
    :config (save-place-mode 1))
#+end_src

** Org-Mode
Set my =org-mode= directory.
#+begin_src emacs-lisp
(setq org-directory "~/org")
#+end_src

Hide emphasis markers because I can see if something is *bold*, /italic/, or =monospace= without needing to see the markers.
#+begin_src emacs-lisp
(setq org-hide-emphasis-markers t)
#+end_src

Set up nicer looking bullet points.
- they look like circles
- instead of hyphens
#+begin_src emacs-lisp
(font-lock-add-keywords 'org-mode
                        '(("^ *\\([-]\\) "
                           (0 (prog1 () (compose-region (match-beginning 1) (match-end 1) "•"))))))
#+end_src

Set up fonts. Don't use =monospace= by default. Do use it where necessary though. Also, make different heading levels different sizes.
#+begin_src emacs-lisp
(add-hook 'org-mode-hook 'variable-pitch-mode)
(custom-set-faces
 '(variable-pitch ((t (:family "CMU Serif" :height 130 :weight thin))))
 '(fixed-pitch ((t (:family "SauceCodePro Nerd Font" :height 110 :weight regular))))
 '(org-block ((t (:inherit fixed-pitch))))
 '(org-code ((t (:inherit (shadow fixed-pitch)))))
 '(org-document-info-keyword ((t (:inherit (shadow fixed-pitch)))))
 '(org-meta-line ((t (:inherit (font-lock-comment-face fixed-pitch)))))
 '(org-verbatim ((t (:inherit (shadow fixed-pitch)))))
 '(org-table ((t (:inherit (shadow fixed-pitch)))))
 '(org-document-title ((t (:inherit title :height 2.0 :underline nil))))
 '(org-level-1 ((t (:inherit outline-1 :weight: bold :height 1.75))))
 '(org-level-2 ((t (:inherit outline-2 :weight: bold :height 1.5))))
 '(org-level-2 ((t (:inherit outline-3 :weight: bold :height 1.25))))
 '(org-level-2 ((t (:inherit outline-4 :weight: bold :height 1.1))))
 '(org-level-4 ((t (:inherit outline-4 :height 1.1))))
 '(org-level-5 ((t (:inherit outline-5 :height 1.0))))
 )
#+end_src

Wrap lines and centre the view to make for a nicer reading experience.
#+begin_src emacs-lisp
(use-package visual-fill-column)
(add-hook 'org-mode-hook 'visual-line-mode)
(add-hook 'org-mode-hook #'(lambda () (display-line-numbers-mode -1)))
(defun jj/org-mode-visual-fill ()
  (setq visual-fill-column-width 100
        visual-fill-column-center-text t)
  (visual-fill-column-mode 1))
(add-hook 'org-mode-hook #'jj/org-mode-visual-fill)
#+end_src

Increase the size of LaTeX previews.
#+begin_src emacs-lisp
(setq org-format-latex-options (plist-put org-format-latex-options :scale 2.0))
#+end_src

Follow links with the return key.
#+begin_src emacs-lisp
(setq org-return-follows-link t)
#+end_src

Tangle on save.
#+begin_src emacs-lisp
(add-hook 'org-mode-hook
  (lambda ()
    (add-hook 'after-save-hook #'org-babel-tangle)))
#+end_src


** Shell
Use =eshell= as an integrated shell.
#+begin_src emacs-lisp
  (use-package eshell)
#+end_src

** Language Servers
Add =eglot= keybindings.
#+begin_src emacs-lisp
  (global-set-key (kbd "C-c r") 'eglot-rename)
  (global-set-key (kbd "C-c a") 'eglot-code-actions)
#+end_src

Install =tree-sitter=.
#+begin_src emacs-lisp
  (use-package tree-sitter)
  (use-package tree-sitter-langs)
#+end_src

Define function to set up =eglot= automatically.
#+begin_src emacs-lisp
  (defun jj/eglot-setup ()
    (eglot-ensure)
    (tree-sitter-mode 1)
    (tree-sitter-hl-mode 1))
#+end_src

** Completions
Use company for completions with no delay, starting immediately after first character is typed.
#+begin_src emacs-lisp
  (use-package company
    :config
    (add-hook 'after-init-hook 'global-company-mode)
    (setq company-idle-delay 0
          company-minimum-prefix-length 1
          company-selection-wrap-around t))
#+end_src

Use =vertico= as a completion user interface.
#+begin_src emacs-lisp
  (use-package vertico
    :custom
    (vertico-cycle t)
    :init
    (vertico-mode))
#+end_src

Use =orderless= to allow typing any portion of a word that you want to search for.
#+begin_src emacs-lisp
  (use-package orderless
    :ensure t
    :custom
    (completion-styles '(orderless basic))
    (completion-category-overrides '((file (styles basic partial-completion)))))
#+end_src

Get descriptions of items in =vertico= with =marginalia=.
#+begin_src emacs-lisp
  (use-package marginalia
    :bind (:map minibuffer-local-map
           ("M-A" . marginalia-cycle))
    :init
    (marginalia-mode))
#+end_src

Get nerd font icons in completions.
#+begin_src emacs-lisp
  (use-package nerd-icons-completion
    :config
    (nerd-icons-completion-mode))
#+end_src

Use consult with =vertico= for extra functionality to various functions.
#+begin_src emacs-lisp
  (use-package consult
    :bind (
           ("C-c M-x" . consult-mode-command)
           ("C-c h" . consult-history)
           ("C-c k" . consult-kmacro)
           ("C-c m" . consult-man)
           ("C-c i" . consult-info)
           ([remap Info-search] . consult-info)
           ("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)
           ("C-x t b" . consult-buffer-other-tab)
           ("C-x r b" . consult-bookmark)
           ("C-x p b" . consult-project-buffer)
           ("M-#" . consult-register-load)
           ("M-'" . consult-register-store)
           ("C-M-#" . consult-register)
           ("M-y" . consult-yank-pop)
           ("M-g e" . consult-compile-error)
           ("M-g f" . consult-flycheck)
           ("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-imenu-multi)
           ("M-s d" . consult-fd)
           ("M-s c" . consult-locate)
           ("M-s g" . consult-grep)
           ("M-s G" . consult-git-grep)
           ("M-s r" . consult-ripgrep)
           ("M-s l" . consult-line)
           ("M-s L" . consult-line-multi)
           ("M-s k" . consult-keep-lines)
           ("M-s u" . consult-focus-lines)
           ("M-s e" . consult-isearch-history)
           :map isearch-mode-map
           ("M-e" . consult-isearch-history)
           ("M-s e" . consult-isearch-history)
           ("M-s l" . consult-line)
           ("M-s L" . consult-line-multi)
           :map minibuffer-local-map
           ("M-s" . consult-history)
           ("M-r" . consult-history))
    :hook (completion-list-mode . consult-preview-at-point-mode)
    :init
    (setq register-preview-delay 0.5
          register-preview-function #'consult-register-format)
    (advice-add #'register-preview :override #'consult-register-window)
    (setq xref-show-xrefs-function #'consult-xref
          xref-show-definitions-function #'consult-xref)
    :config
    (consult-customize
     consult-theme :preview-key '(:debounce 0.2 any)
     consult-ripgrep consult-git-grep consult-grep
     consult-bookmark consult-recent-file consult-xref
     consult--source-bookmark consult--source-file-register
     consult--source-recent-file consult--source-project-recent-file
     :preview-key '(:debounce 0.4 any))
    (setq consult-narrow-key "<"))
#+end_src

Use Flycheck for syntax checking.
#+begin_src emacs-lisp
  (use-package flycheck
    :config
    (add-hook 'after-init-hook #'global-flycheck-mode))
#+end_src

Use Flyspell for spell checking.
#+begin_src emacs-lisp
  (dolist (hook '(text-mode-hook))
    (add-hook hook (lambda () (flyspell-mode 1))))
  (use-package flyspell-correct
    :after flyspell
    :bind (:map flyspell-mode-map ("C-;" . flyspell-correct-wrapper)))
#+end_src

** Snippets
Use =yasnippet= for snippets so I don't need to type as much.
#+begin_src emacs-lisp
  (use-package yasnippet
    :init
    (yas-global-mode 1)
    :config
    (global-set-key (kbd "C-c s") 'yas-insert-snippet))
#+end_src

Install snippet collection for =yasnippet=.
#+begin_src emacs-lisp
  (use-package yasnippet-snippets)
#+end_src

** Formatting
Automatically format with Apheleia and =clang-format=.
#+begin_src emacs-lisp
  (use-package apheleia
    :init (apheleia-global-mode +1))
  (use-package clang-format)
#+end_src

** RSS
Use Emacs as an RSS feed with =elfeed=.
#+begin_src emacs-lisp
  (use-package elfeed
    :config
    (global-set-key (kbd "C-c w") 'elfeed)
    (global-set-key (kbd "C-c C-W") 'elfeed-update))
#+end_src

Make =elfeed= more powerful with =elfeed-goodies=.
#+begin_src emacs-lisp
  (use-package elfeed-goodies
    :after elfeed
    :config
    (elfeed-goodies/setup))
#+end_src

Store my feed in Org-mode [[./feed.org][here]].
#+begin_src emacs-lisp
  (use-package elfeed-org
    :config
    (elfeed-org)
    (setq rmh-elfeed-org-files (list "~/.config/emacs/feed.org")))
#+end_src

** Deft
Use the Deft package to manage notes.
#+begin_src emacs-lisp
  (use-package deft
    :config
    (global-set-key (kbd "C-c d") 'deft)
    (setq deft-directory "~/notes/"
          deft-default-extension "org"))
#+end_src

* Languages
** Shell Script
Run =eglot= on shell script files.
#+begin_src emacs-lisp
  (add-hook 'sh-mode-hook 'jj/eglot-setup)
#+end_src

** C
Run =eglot= on C and C++ files.
#+begin_src emacs-lisp
  (add-hook 'c-mode-hook 'jj/eglot-setup)
  (add-hook 'c++-mode-hook 'jj/eglot-setup)
  (add-hook 'cc-mode-hook 'jj/eglot-setup)
#+end_src

** Web
Run =eglot= on HTML files.
#+begin_src emacs-lisp
  (add-hook 'html-mode-hook 'jj/eglot-setup)
#+end_src

Run =eglot= on CSS files.
#+begin_src emacs-lisp
  (add-hook 'css-mode-hook 'jj/eglot-setup)
#+end_src

Run =eglot= on JavaScript/Typescript files.
#+begin_src emacs-lisp
  (add-hook 'js-json-mode-hook 'jj/eglot-setup)
  (add-hook 'js-mode-hook 'jj/eglot-setup)
  (use-package typescript-mode
    :init
    (add-hook 'typescript-mode-hook 'jj/eglot-setup))
#+end_src

** Python
Run =eglot= on Python files.
#+begin_src emacs-lisp
  (add-hook 'python-mode-hook 'jj/eglot-setup)
#+end_src

** TODO Rust
Run =eglot= on Rust files. (This does not work at all)
#+begin_src emacs-lisp
  (use-package rust-mode
    :init
    (add-hook 'rust-mode-hook 'jj/eglot-setup))
#+end_src

** Go
Run =eglot= on Go files.
#+begin_src emacs-lisp
  (use-package go-mode
    :init
    (add-hook 'go-mode-hook 'jj/eglot-setup))
#+end_src

Get documentation for Go variables, functions, and arguments.
#+begin_src emacs-lisp
  (use-package go-eldoc
    :init
    (add-hook 'go-mode-hook 'go-eldoc-setup))
#+end_src

Automatically generate tests.
#+begin_src emacs-lisp
  (use-package go-gen-test)
#+end_src

Refactoring tools from =go-guru=.
#+begin_src emacs-lisp
  (use-package go-guru
    :hook (go-mode . go-guru-hl-identifier-mode))
#+end_src

** Lua
Run =eglot= on Lua files.
#+begin_src emacs-lisp
  (use-package lua-mode
    :init
    (add-hook 'lua-mode-hook 'jj/eglot-setup))
#+end_src

** Markdown
Run =eglot= on Markdown files.
#+begin_src emacs-lisp
  (use-package markdown-mode
    :init
    (add-hook 'markdown-mode-hook 'jj/eglot-setup))
#+end_src

** LaTeX
Run =eglot= on TeX files.
#+begin_src emacs-lisp
  (add-hook 'tex-mode-hook 'jj/eglot-setup)
#+end_src

Use AUCTeX for extra LaTeX integrations.
#+begin_src emacs-lisp
  (use-package auctex
    :config
    (add-hook 'LaTeX-mode-hook 'jj/eglot-setup)
    (add-hook 'LaTeX-mode-hook
            (lambda ()
              (put 'LaTeX-mode 'eglot-language-id "latex"))))
#+end_src

Use CDLaTeX for environment and macro insertion.
#+begin_src emacs-lisp
  (use-package cdlatex
    :config
    (add-hook 'LaTeX-mode-hook #'turn-on-cdlatex))
#+end_src

** YAML
Run =eglot= on YAML files.
#+begin_src emacs-lisp
  (use-package yaml-mode
    :init
    (add-hook 'yaml-mode-hook 'jj/eglot-setup))
#+end_src