summaryrefslogtreecommitdiff
path: root/.config/emacs/init.el
blob: 99e99de424ae011b1a17d234bdcfcabb164a36f1 (plain) (blame)
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
;;; init.el --- Main Emacs configuration file  -*- lexical-binding: t; -*-

;;; Preamble

;; To inhibit this message you must do this in init.el (not
;; early-init.el!), you must use ‘setq’ (not ‘setopt’!), and you must
;; write your login name as a string (you shan’t use ‘user-login-name’!).
;; Lord knows why this needs to be so complicated…
;;
;; The ‘eval’ is required in the case that this file is byte-compiled.
(if mm-darwin-p
    (eval '(setq inhibit-startup-echo-area-message "thomasvoss"))
  (eval '(setq inhibit-startup-echo-area-message "thomas")))

;; Add all my custom lisp code into the load path
(dolist (directory
         (list mm-config-directory
               (expand-file-name "modules" mm-config-directory)
               (expand-file-name "site-lisp" mm-config-directory)))
  (add-to-list 'load-path directory))

^L
;;; Disable or Enable LSP?

;; I’m not decided on LSP… so make it a variable

(defvar mm-lsp-p nil
  "Enable LSP support if non-nil.")

^L
;;; Convenience Macros and -Functions

(defun mm-mode-to-hook (mode)
  "Get the hook corresponding to MODE."
  (declare (ftype (function (symbol) symbol))
           (pure t) (side-effect-free t))
  (intern (concat (symbol-name mode) "-hook")))

(defun mm-mode-to-ts-mode (mode)
  "Get the Tree-Sitter mode corresponding to MODE."
  (declare (ftype (function (symbol) symbol))
           (pure t) (side-effect-free t))
  (intern (concat
           (string-remove-suffix "-mode" (symbol-name mode))
           "-ts-mode")))

(defun mm-ts-mode-to-mode (ts-mode)
  "Get the non-Tree-Sitter mode corresponding to TS-MODE."
  (declare (ftype (function (symbol) symbol))
           (pure t) (side-effect-free t))
  (intern (concat
           (string-remove-suffix "-ts-mode" (symbol-name ts-mode))
           "-mode")))

(defsubst mm-string-split (separators string)
  "Split STRING on SEPARATORS.
Wrapper around `string-split' that puts separators first.  This makes it
convenient to use in `thread-last'."
  (declare (ftype (function (string string) (list string)))
           (pure t) (side-effect-free t))
  (string-split string separators))

(defun mm-as-number (string-or-number)
  "Ensure STRING-OR-NUMBER is a number.
If given a number return STRING-OR-NUMBER as-is, otherwise convert it to
a number and then return it.

This function is meant to be used in conjuction with `read-string' and
`format-prompt'."
  (declare (ftype (function (or string number) number))
           (pure t) (side-effect-free t))
  (if (stringp string-or-number)
      (string-to-number string-or-number)
    string-or-number))

(defun mm-do-and-center (function &rest arguments)
  "Call FUNCTION with ARGUMENTS and then center the screen."
  (apply function arguments)
  (when (called-interactively-p)
    (recenter)))

(defmacro mm-comment (&rest _body)
  "Comment out BODY.  A cleaner alternative to line-commenting a region."
  (declare (indent 0))
  nil)

(defun mm-nil (&rest _)
  "Return nil."
  nil)

(defmacro mm-with-suppressed-output (&rest body)
  "Execute BODY while suppressing output.
Execute BODY as given with all output to the echo area or the *Messages*
buffer suppressed."
  (declare (indent 0))
  `(let ((inhibit-message t)
         (message-log-max nil))
     ,@body))

(defun mm-rotate-left (n list)
  "Rotate the elements of LIST N places to the left."
  (declare (ftype (function (number (list t)) (list t)))
           (pure t) (side-effect-free t))
  (append (nthcdr n list) (butlast list (- (length list) n))))

(defun mm-rotate-right (n list)
  "Rotate the elements of LIST N places to the right."
  (declare (ftype (function (number (list t)) (list t)))
           (pure t) (side-effect-free t))
  (mm-rotate-left (- (length list) n) list))

^L
;;; Silent Native Compilation

(when (native-comp-available-p)
  (setopt
   native-comp-async-report-warnings-errors nil
   native-compile-prune-cache t))

^L
;;; Package Management

(setopt
 package-vc-register-as-project nil
 package-gnupghome-dir (getenv "GNUPGHOME")
 package-user-dir (expand-file-name "pkg" mm-data-directory)
 package-archives (cl-loop with proto = (if (gnutls-available-p) "https" "http")
                           for (name . url) in
                             '(("gnu"    . "elpa.gnu.org/packages/")
                               ("melpa"  . "melpa.org/packages/")
                               ("nongnu" . "elpa.nongnu.org/nongnu/"))
                           collect (cons name (concat proto "://" url)))
 package-archive-priorities '(("gnu"    . 3)
                              ("nongnu" . 2)
                              ("melpa"  . 1)))
(setopt use-package-always-defer t)

(package-initialize)

(defun mm-package-sync ()
  "Remove unused packages and install missing ones."
  (interactive)
  (let ((window-configuration (current-window-configuration)))
    (package-autoremove)
    (package-install-selected-packages)
    (package-upgrade-all)
    (package-vc-install-selected-packages)
    (package-vc-upgrade-all)
    (set-window-configuration window-configuration))
  (message "Done syncing packages."))

^L
;;; Generic Emacs Configuration

(defvar mm-initial-scratch-message
  (format
   ";; This is `%s'.  Use `%s' to evaluate and print results.\n\n"
   initial-major-mode
   (propertize
    (substitute-command-keys
     "\\<lisp-interaction-mode-map>\\[eval-print-last-sexp]")))
  "The initial message to display in the scratch buffer.")

(use-package emacs
  :demand t
  :custom
  (ad-redefinition-action 'accept)
  (case-fold-search nil)
  (create-lockfiles nil)
  (custom-file (expand-file-name "custom.el" mm-config-directory))
  (custom-safe-themes t)
  (delete-pair-blink-delay 0)
  (disabled-command-function nil)
  (duplicate-line-final-position -1)
  (duplicate-region-final-position -1)
  (echo-keystrokes 0.01)                ; 0 disables echoing
  (echo-keystrokes-help nil)
  (extended-command-suggest-shorter nil)
  (help-window-select t)
  (initial-buffer-choice t)
  (initial-scratch-message mm-initial-scratch-message)
  (kill-do-not-save-duplicates t)
  (large-file-warning-threshold nil)
  (make-backup-files nil)
  (mode-require-final-newline nil)
  (next-error-recenter '(4)) ; ‘center of window’
  (read-extended-command-predicate #'command-completion-default-include-p)
  (remote-file-name-inhibit-auto-save t)
  (remote-file-name-inhibit-delete-by-moving-to-trash t)
  (require-final-newline nil)
  (save-interprogram-paste-before-kill t)
  (scroll-conservatively 101) ; (info "(Emacs)Auto Scrolling")
  (scroll-error-top-bottom t)
  (scroll-margin 10)
  (user-full-name "Thomas Voss")
  (user-mail-address "mail@thomasvoss.com")
  (vc-follow-symlinks t)
  (vc-handled-backends '(Git))
  :config
  (load custom-file :noerror)
  (setq-default
   fill-column 80
   truncate-partial-width-windows nil)
  (dolist (mode '(text-mode emacs-lisp-mode lisp-mode))
    (add-hook (mm-mode-to-hook mode)
              (defun mm-set-fill-column ()
                (setq-local fill-column 73))))
  (add-hook 'text-mode-hook #'auto-fill-mode)
  (add-hook 'before-save-hook
            (defun mm-delete-final-newline ()
              (let ((end (point-max)))
                (unless (or require-final-newline
                            mode-require-final-newline
                            (not (= (char-before end) ?\n)))
                  (delete-region (1- end) end)))))
  (add-hook 'before-save-hook #'delete-trailing-whitespace)
  (prefer-coding-system 'utf-8)

  ;; Show trailing whitespace but only in relevant buffers
  (dolist (mode '(conf-mode prog-mode text-mode))
    (add-hook (mm-mode-to-hook mode)
              (defun mm-show-trailing-whitespace ()
                (setopt show-trailing-whitespace t))))

  ;; Disabled modes
  (blink-cursor-mode -1)
  (line-number-mode -1)
  (tooltip-mode -1))

^L
;;; Instantly highlight matching parens

(use-package paren
  :custom
  (show-paren-delay 0))

^L
;;; Display Line Numbers

(use-package display-line-numbers
  :hook prog-mode
  :custom
  (display-line-numbers-type 'relative)
  (display-line-numbers-width-start 99)
  (display-line-numbers-grow-only t))

^L
;;; Auto Revert Buffers

(use-package autorevert
  :custom
  (global-auto-revert-non-file-buffers t)
  :init
  (add-hook
   'after-change-major-mode-hook
   (defun mm-enable-autorevert ()
     (unless (derived-mode-p 'Buffer-menu-mode)
       (auto-revert-mode)))))

^L
;;; Bookmarks

(use-package bookmark
  :custom
  (bookmark-save-flag 1))

^L
;;; Smoother Scrolling

(use-package pixel-scroll
  :init
  (pixel-scroll-precision-mode)
  :config
  ;; Make it easier to use custom scroll functions
  (dolist (binding '("<next>" "<prior>"))
    (keymap-unset pixel-scroll-precision-mode-map binding :remove)))

^L
;;; Automatically Create- and Delete Directories

(defun mm-auto-create-directories (function filename &rest arguments)
  "Automatically create and delete parent directories of files.
This is an `:override' advice for `find-file' and friends.  It
automatically creates the parent directories of the file being visited
if necessary.  It also sets a buffer-local variable so that the user
will be prompted to delete the newly created directories if they kill
the buffer without saving it."
  (let (dirs-to-delete)
    (let* ((dir-to-create (file-name-directory filename))
           (current-dir dir-to-create))
      ;; Add each directory component to ‘dirs-to-delete’
      (while (not (file-exists-p current-dir))
        (push current-dir dirs-to-delete)
        (setq current-dir (file-name-directory
                           (directory-file-name current-dir))))
      (unless (file-exists-p dir-to-create)
        (make-directory dir-to-create :parents)))
    (prog1
        (apply function filename arguments)
      (when dirs-to-delete
        (setq-local mm-find-file--dirs-to-delete (reverse dirs-to-delete))
        (add-hook 'kill-buffer-hook #'mm-find-file--maybe-delete-directories
                  :depth :local)
        (add-hook 'after-save-hook  #'mm-find-file--remove-hooks
                  :depth :local)))))

(defun mm-find-file--maybe-delete-directories ()
  (unless (file-exists-p buffer-file-name)
    (dolist (directory mm-find-file--dirs-to-delete)
      (when (and (stringp directory)
                 (file-exists-p directory)
                 (thread-last
                   (directory-file-name directory)
                   (format "Also delete directory `%s'?")
                   (substitute-quotes)
                   (y-or-n-p)))
        (delete-directory directory)))))

(defun mm-find-file--remove-hooks ()
  (remove-hook 'kill-buffer-hook
               #'mm-find-file--maybe-delete-directories
               :local)
  (remove-hook 'after-save-hook
               #'mm-find-file--remove-hooks
               :local))

(dolist (command #'(find-file find-alternate-file write-file))
  (advice-add command :around #'mm-auto-create-directories))

^L
;;; Load Modules

(require 'mm-abbrev)                    ; Text Expansion
(require 'mm-calc)                      ; Emacs Calc
(require 'mm-completion)                ; Completions
(require 'mm-dired)                     ; Dired
(require 'mm-documentation)             ; Documentation
(require 'mm-editing)                   ; Text Editing
(require 'mm-keybindings)               ; Keybindings
(require 'mm-modeline)                  ; Modeline
(require 'mm-org)                       ; Org-Mode
(require 'mm-projects)                  ; Project Management
(require 'mm-search)                    ; Text Searching
(require 'mm-spellcheck)                ; Spell Checking
(require 'mm-tetris)                    ; Emacs Tetris
(require 'mm-theme)                     ; Themeing
(require 'mm-treesit)                   ; Tree-Sitter
(when mm-darwin-p
  (require 'mm-darwin))                 ; MacOS
(when mm-lsp-p
  (require 'mm-lsp))                    ; Language Server Protocol

^L
;;; Postamble

(add-hook 'after-init-hook
          (defun mm-echo-init-time ()
            (message (emacs-init-time "Emacs initialized in %.2f seconds")))
          100)