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
|
;; -*- lexical-binding: t -*-
;; Packages and Custom initialization
;; Letting Custom run *before* initializing packages seems to result
;; in packages resetting some of their variables, eg page-break-lines
;; resets global-page-break-lines-mode to nil. Cue Custom shrugging,
;; "changed outside Customize".
;; NB: starting from Emacs 27, package-initialize is automatically
;; called before loading the user's init file, unless
;; package-enable-at-startup is set to nil in the early init file.
(when (< emacs-major-version 27)
(package-initialize))
(setq custom-file "~/.emacs-custom.el")
(load custom-file)
;; Key bindings
;; C-h is a special snowflake in many situations; this is the most
;; reliable way I found to consistently get C-h to do what DEL does.
;;
;; Likewise, C-M-h is re-bound by some major modes (CC, Python, Perl),
;; so this is the simplest way I know of to make sure C-M-h sticks as
;; "backward-kill-word".
;;
;; Same story with M-h (mark-paragraph) which gets re-bound by eg
;; markdown-mode and nxml-mode.
;;
;; NB: help and mark-defun are still accessible using H instead of h,
;; except in a terminal.
(define-key input-decode-map (kbd "C-h") (kbd "DEL"))
(define-key input-decode-map (kbd "C-M-h") (kbd "M-DEL"))
(global-set-key (kbd "C-x C-b") 'ibuffer)
(defun my/other-window (count &optional all-frames)
(interactive "p")
(let ((repeat-map (make-sparse-keymap)))
(define-key repeat-map [?o] #'other-window)
(set-transient-map repeat-map t)
(other-window count all-frames)))
(global-set-key (kbd "C-x o") #'my/other-window)
;; Hopefully these will be easier to remember than TeX commands:
(quail-define-package
"my/symbols" "UTF-8" "π°" t
"Input arbitrary Unicode symbols with other arbitrary symbols.")
(pcase-dolist
(`(,key ,translation)
'(;; Punctuation
("..." ?β¦)
;; Math symbols
("~~" ?β) ("~~_" ?β) ("~=" ?β
) ("~_" ?β)
("=_" ?β‘) ("^=" ?β) (":=" ?β)
("<=" ?β€) (">=" ?β₯)
("-->" ?β) ("-/>" ?β) ("==>" ?β) ("=/>" ?β)
("<--" ?β) ("</-" ?β) ("<==" ?β) ("</=" ?β)
("<->" ?β) ("<=>" ?β)
;; Emojis
("\\o/" ?π) ("\\m/" ?π€)
;; Pictograms
("/!\\" ?β )))
(quail-defrule key translation "my/symbols"))
(defmacro my/make-input-toggle (input-method)
(let ((fsym (intern (format "my/toggle-input-%s" input-method)))
;; Unfortunately, by default `help-make-xrefs' does not try to
;; cross-reference input methods, as `help-xref-mule-regexp'
;; is nil. This can be worked around by setting this variable
;; to `help-xref-mule-regexp-template'.
(doc (format "Toggle `%s' input method." input-method)))
`(defun ,fsym ()
,doc
(interactive)
;; `current-input-method' is a string; if INPUT-METHOD is a
;; symbol, neither eq, eql nor equal would return t.
(if (string= current-input-method ',input-method)
(deactivate-input-method)
(set-input-method ',input-method t)))))
(defun my/set-tab-width (&optional arg)
(interactive "P")
(let ((new-width (cond (arg (prefix-numeric-value arg))
((= tab-width 4) 8)
(4)))
(old-width tab-width))
;; TODO: for some reason, set-variable takes effect immediately,
;; but setq(-local)? do not: I need to move the cursor before tabs
;; are re-drawn.
(set-variable 'tab-width new-width)
(message "changed from %s to %s" old-width new-width)))
(defun my/kill (stuff)
(kill-new stuff)
(message stuff))
(defun my/kill-ring-filename ()
(interactive)
(my/kill (or (buffer-file-name) default-directory)))
(defun my/kill-ring-pipe-region (command)
(interactive (list (read-shell-command "Shell command on region: ")))
(let ((input (funcall region-extract-function nil)))
(with-temp-buffer
(insert input)
(call-process-region (point-min) (point-max) shell-file-name
t t nil shell-command-switch command)
(my/kill (buffer-string)))))
(defun my/kill-ring-shell (command)
(interactive (list (read-shell-command "Shell command: ")))
(with-temp-buffer
(call-process-shell-command command nil t)
(my/kill (buffer-string))))
(defun my/make-project-wide (f)
"Make a function which will run F from the project's root directory."
(lambda ()
(:documentation (format "Run `%s' from the project's root directory." f))
(interactive)
(let ((default-directory (my/project-root)))
(call-interactively f))))
(defun my/magit-project ()
(interactive)
(require 'project)
(magit-status (project-prompt-project-dir)))
(defmacro my/define-prefix-command (name doc bindings)
(declare (indent defun))
`(defvar ,name
(let ((map (define-prefix-command ',name)))
(pcase-dolist (`(,key ,fun) ,bindings)
(define-key map key fun))
map)
,doc))
(my/define-prefix-command my/buffer-map
"Keymap for buffer manipulation commands."
'(("b" bury-buffer)
("g" revert-buffer)
("r" rename-buffer)))
(my/define-prefix-command my/display-map
"Keymap for display-related commands."
'(("t" toggle-truncate-lines)
("v" visual-line-mode)))
(my/define-prefix-command my/editing-map
"Keymap for toggling editing features."
'(("f" auto-fill-mode)))
(my/define-prefix-command my/magit-map
"Keymap for Magit commands."
'(("c" magit-file-dispatch)
("f" magit-find-file)
("g" magit-status)
("x" magit-dispatch)))
(my/define-prefix-command my/input-map
"Keymap for input methods shortcuts."
`(("t" ,(my/make-input-toggle TeX))
("u" ,(my/make-input-toggle my/symbols))))
(my/define-prefix-command my/kill-map
"Keymap for adding things to the kill ring."
'(("f" my/kill-ring-filename)
("|" my/kill-ring-pipe-region)
("!" my/kill-ring-shell)))
(my/define-prefix-command my/manual-map
"Keymap for reading manuals."
'(("i" info-display-manual)
("m" man)))
(my/define-prefix-command my/project-map
"Keymap for project-related commands."
'(("g" my/magit-project)))
(my/define-prefix-command my/whitespace-map
"Keymap for whitespace-related commands."
'(("c" whitespace-cleanup)
("f" page-break-lines-mode)
("m" whitespace-mode)
("t" my/set-tab-width)))
;; C-c [[:alpha:]] is reserved for users - let's make good use of it.
(global-set-key (kbd "C-c b") 'my/buffer-map)
(global-set-key (kbd "C-c c") 'compile)
(global-set-key (kbd "C-c d") 'my/display-map)
(global-set-key (kbd "C-c e") 'my/editing-map)
(global-set-key (kbd "C-c g") 'my/magit-map)
(global-set-key (kbd "C-c i") 'my/input-map)
(global-set-key (kbd "C-c k") 'my/kill-map)
(global-set-key (kbd "C-c m") 'my/manual-map)
(global-set-key (kbd "C-c p") 'my/project-map)
(global-set-key (kbd "C-c w") 'my/whitespace-map)
(unless (>= emacs-major-version 28)
(define-key my/project-map "c" (my/make-project-wide 'compile))
(define-key my/project-map "f" 'project-find-file)
(define-key my/project-map "!" (my/make-project-wide 'shell-command))
(define-key my/project-map "&" (my/make-project-wide 'async-shell-command)))
(rg-enable-default-bindings) ; Uses the C-c s prefix.
;; What's life without a little risk?
(setq disabled-command-function nil)
;; Window management
(when window-system
(load-theme 'eighters t)
;; Bindings ala Terminator
(global-set-key (kbd "C-S-o") 'split-window-below)
(global-set-key (kbd "C-S-e") 'split-window-right)
(global-set-key (kbd "C-+") 'text-scale-adjust)
(global-set-key (kbd "C--") 'text-scale-adjust)
(global-set-key (kbd "C-0") 'text-scale-adjust)
(global-set-key (kbd "C-S-<up>") 'enlarge-window)
(global-set-key (kbd "C-S-<down>") 'shrink-window)
(global-set-key (kbd "C-S-<right>") 'enlarge-window-horizontally)
(global-set-key (kbd "C-S-<left>") 'shrink-window-horizontally))
;; Online packages configuration
;; So long, Will Mengarini.
(delight 'abbrev-mode nil 'abbrev)
(delight 'auto-fill-function "β" t)
(delight 'auto-revert-mode "β³" 'autorevert)
(delight 'auto-revert-tail-mode "β€" 'autorevert)
(delight 'eldoc-mode "π" 'eldoc)
(delight 'footnote-mode "ΒΉ" 'footnote)
(delight 'flyspell-mode (propertize "π" 'face 'flyspell-incorrect) 'flyspell)
(delight 'hi-lock-mode nil 'hi-lock)
(delight 'hs-minor-mode "β¦" 'hideshow)
(delight 'isearch-mode "π" 'isearch)
(delight 'org-indent-mode "Β»" 'org-indent)
(delight 'magit-blame-mode "π" 'magit-blame)
(delight 'mml-mode "π§" 'mml)
(delight 'page-break-lines-mode nil 'page-break-lines)
(delight 'scroll-lock-mode "π" 'scroll-lock)
(delight 'text-scale-mode
'(:eval (if (>= text-scale-mode-amount 0) "π" "π"))
'face-remap)
(delight 'visual-line-mode nil t)
(delight 'which-key-mode nil 'which-key)
(delight 'whitespace-mode nil 'whitespace)
(delight 'with-editor-mode "βΈ" 'with-editor)
;; TODO: Narrow (β, βΆ)
(if (< emacs-major-version 27)
(delight 'compilation-in-progress
(propertize "β" 'face 'compilation-mode-line-run)
'compile)
(let* ((indicator (alist-get 'compilation-in-progress mode-line-modes))
(old-props (text-properties-at 0 (car indicator)))
(new-props '(face compilation-mode-line-run)))
(setcar indicator (apply #'propertize "β" (append new-props old-props)))))
(add-hook 'magit-pre-refresh-hook 'diff-hl-magit-pre-refresh)
(add-hook 'magit-post-refresh-hook 'diff-hl-magit-post-refresh)
;; Don't use Customize here, since that would set the variable's value
;; in stone, and I would miss out on future updates by Magit.
(add-hook 'git-commit-setup-hook 'git-commit-turn-on-flyspell)
(defun my/revision-at-point ()
(cond
((derived-mode-p 'magit-mode)
(magit-branch-or-commit-at-point))
((derived-mode-p 'vc-git-log-view-mode)
(log-view-current-tag))
((derived-mode-p 'vc-annotate-mode)
(car (vc-annotate-extract-revision-at-line)))))
(defun my/describe-revision (rev)
"Format a Git revision in a format suitable for changelogs."
(interactive
(list (let ((rev (my/revision-at-point)))
(read-string (format-prompt "Revision" rev)
nil nil rev))))
(my/kill (string-trim
(shell-command-to-string
(format "git show --no-patch --date=short --format='%s' %s"
"%cd \"%s\" (%h)" rev)))))
;; Major modes configuration
(defun my/c-modes-hook ()
(c-set-style "bsd")
(c-set-offset 'arglist-close 0))
(add-hook 'c-mode-common-hook 'my/c-modes-hook)
(defun my/python-hook ()
(setq-local forward-sexp-function nil))
(add-hook 'python-mode-hook 'my/python-hook)
(defun my/compilation-notify (buffer results)
(let* ((title (buffer-name buffer))
(status (if (string-equal results "finished\n") "success" "failure"))
(icon (format "%s/icons/compilation-%s.png" user-emacs-directory status)))
(require 'notifications)
(notifications-notify :title title :body results :app-icon icon :timeout 3000)))
(add-to-list 'compilation-finish-functions 'my/compilation-notify)
(defun my/make-tabless (f)
"Make a function which will run F with `indent-tabs-mode' disabled."
(lambda ()
(:documentation (format "Run `%s' with `indent-tabs-mode' set to nil." f))
(interactive)
(let ((indent-tabs-mode nil))
(call-interactively f))))
(defun my/makefile-hook ()
;; I would rather align backslashes with spaces rather than tabs;
;; however, I would also like indent-tabs-mode to remain non-nil.
(local-set-key (kbd "C-c C-\\") (my/make-tabless 'makefile-backslash-region))
(local-set-key (kbd "M-q") (my/make-tabless 'fill-paragraph)))
(add-hook 'makefile-mode-hook 'my/makefile-hook)
(defun my/shell-hook ()
(setq truncate-lines nil)
(setq-local font-lock-comment-face 'default)
(setq-local font-lock-string-face 'default)
(setq-local recenter-positions '(top middle bottom)))
(add-hook 'shell-mode-hook 'my/shell-hook)
(add-hook 'dired-mode-hook 'diff-hl-dired-mode-unless-remote)
(add-to-list 'ibuffer-saved-filter-groups
'("my/ibuffer-groups"
("REPL"
(or (derived-mode . comint-mode)
(mode . lisp-interaction-mode)))
("Programming" (derived-mode . prog-mode))
("Folders" (mode . dired-mode))
("Messaging"
(or (mode . erc-mode)
(mode . message-mode)
(derived-mode . gnus-mode)))
("Documentation"
(or (mode . Info-mode)
(mode . Man-mode)
(mode . help-mode)))
("Version control"
(or (derived-mode . magit-mode)
(name . "\\`\\*vc")))))
(add-hook 'ibuffer-mode-hook
(lambda ()
(ibuffer-switch-to-saved-filter-groups "my/ibuffer-groups")))
(eval-after-load 'org
'(when (version< org-version "9.4")
(define-key org-mode-map (kbd "C-j") 'org-return)
(define-key org-mode-map (kbd "RET") 'org-return-indent)))
;; Helper functions and miscellaneous settings.
(defun my/froggify ()
(ispell-change-dictionary "fr")
(setq-local colon-double-space nil)
(setq-local sentence-end-double-space nil)
(setq-local fill-nobreak-predicate
(cons 'fill-french-nobreak-p fill-nobreak-predicate))
(setq-local my/froggified t))
(defun my/unfroggify ()
(ispell-change-dictionary "default")
(setq-local colon-double-space t)
(setq-local sentence-end-double-space t)
(setq-local fill-nobreak-predicate
(remq 'fill-french-nobreak-p fill-nobreak-predicate))
(setq-local my/froggified nil))
(defun my/croak ()
(interactive)
(if (and (boundp 'my/froggified) my/froggified)
(my/unfroggify)
(my/froggify)))
;; Utilities for mailing lists.
(defun my/kill-message-id ()
(interactive)
(my/kill (mail-header-message-id (gnus-summary-article-header))))
(defun my/describe-message (id url)
(my/kill (format "%s\n%s\n"
(if (string-prefix-p "<" id)
id
(format "<%s>" id))
url)))
(defun my/describe-message-id (list id)
"Format references from the Message-ID of a gnu.org list."
(interactive
(list
(read-string "List: ") ; TODO: default to current list.
(let ((default-id
(mail-header-message-id (gnus-summary-article-header))))
(read-string (format-prompt "Message-ID" default-id)
nil nil default-id))))
(with-current-buffer
(url-retrieve-synchronously
(concat
;; For some reason, literal "+" chars cause the search to fail.
;; Escape them.
"https://lists.gnu.org/archive/cgi-bin/namazu.cgi"
"?query=%2Bmessage-id:"
(replace-regexp-in-string "\\+" "%2B" id)
"&submit=Search!"
"&idxname=" list))
(search-forward-regexp
(rx "<a href=\""
(group "/archive/html/" (literal list) "/"
(+ (any "0-9-")) "/msg" (+ (any "0-9")) ".html")
"\">"))
(let ((url (concat "https://lists.gnu.org" (match-string 1))))
(my/describe-message id url))))
(defun my/describe-message-url (url)
"Format references from an article archived on MHonArc."
(interactive
(list
(let ((default (or (thing-at-point 'url)
(and (derived-mode-p 'eww-mode)
(shr-url-at-point nil)))))
(read-string (format-prompt "URL" default) nil nil default))))
(with-current-buffer (url-retrieve-synchronously url)
(search-forward-regexp "^<!--X-Message-Id: \\(.+\\) -->$")
(let ((id (xml-substitute-numeric-entities (match-string 1))))
(my/describe-message id url))))
;; Font stuff π€·π€¦. Emacs comes with sensible defaults (e.g. the
;; default fontset includes Symbola for various subgroups of the
;; "symbol" script), but no color font by default.
(when (>= emacs-major-version 27)
;; Prefer a color font for emojis.
(set-fontset-font t 'symbol "Noto Color Emoji" nil 'prepend)
;; Make sure the default font does not get overzealous (β β).
(setq use-default-font-for-symbols nil))
(defun my/project-root ()
(when-let ((project (project-current)))
(car (project-roots project))))
(defun my/project-name ()
(when-let ((root (my/project-root)))
(when (not (file-equal-p root "~"))
(file-name-nondirectory (string-trim-right root "/")))))
(defun my/connection-name ()
(when-let ((method (file-remote-p default-directory 'method)))
(if (string-match-p "sudo" method)
method
(format "%s:%s" method (file-remote-p default-directory 'host)))))
(defun my/frame-title-format ()
(let ((prefix
;; Messing with match data during redisplay is dangerous
;; (cf. bug#33697).
(save-match-data
;; For some reason, calling filename-parsing functions
;; while TRAMP is busy opens the gates to Infinite
;; Minibuffer Recursion Hell. Cautiously side-step that.
(or
(my/connection-name)
(my/project-name)))))
(concat (when prefix (format "[%s] " prefix))
"%b")))
(setq frame-title-format '(:eval (my/frame-title-format)))
(setq-default paragraph-start (concat "[ ]*- \\|" paragraph-start))
;; TODO: decruftify mode-line (e.g. remove superflous parens)
|