fe307b60bd89ab026f27ce8727c489dddde3642c
[~bandali/configs] / init.org
1 #+title: =aminb='s Literate Emacs Configuration
2 #+author: Amin Bandali
3 #+babel: :cache yes
4 #+property: header-args :tangle yes
5
6 * About
7 :PROPERTIES:
8 :CUSTOM_ID: about
9 :END:
10
11 This org file is my literate configuration for GNU Emacs, and is
12 tangled to [[./init.el][init.el]]. Packages are installed and managed using
13 [[https://github.com/emacscollective/borg][Borg]]. Over the years, I've taken inspiration from configurations of
14 many different people. Some of the configurations that I can remember
15 off the top of my head are:
16
17 - [[https://github.com/dieggsy/dotfiles][dieggsy/dotfiles]]: literate Emacs and dotfiles configuration, uses
18 straight.el for managing packages
19 - [[https://github.com/dakra/dmacs][dakra/dmacs]]: literate Emacs configuration, using Borg for managing
20 packages
21 - [[http://pages.sachachua.com/.emacs.d/Sacha.html][Sacha Chua's literate Emacs configuration]]
22 - [[https://github.com/dakrone/eos][dakrone/eos]]
23 - Ryan Rix's [[http://doc.rix.si/cce/cce.html][Complete Computing Environment]] ([[http://doc.rix.si/projects/fsem.html][about cce]])
24 - [[https://github.com/jwiegley/dot-emacs][jwiegley/dot-emacs]]: nix-based configuration
25 - [[https://github.com/wasamasa/dotemacs][wasamasa/dotemacs]]
26 - [[https://github.com/hlissner/doom-emacs][Doom Emacs]]
27
28 I'd like to have a fully reproducible Emacs setup (part of the reason
29 why I store my configuration in this repository) but unfortunately out
30 of the box, that's not achievable with =package.el=, not currently
31 anyway. So, I've opted to use Borg. For what it's worth, I briefly
32 experimented with [[https://github.com/raxod502/straight.el][straight.el]], but found that it added about 2 seconds
33 to my init time; which is unacceptable for me: I use Emacs as my
34 window manager (via EXWM) and coming from bspwm, I'm too used to
35 having fast startup times.
36
37 ** Installation
38
39 To use this config for your Emacs, first you need to clone this repo,
40 then bootstrap Borg, tell Borg to retrieve package submodules, and
41 byte-compiled the packages. Something along these lines should work:
42
43 #+begin_src sh :tangle no
44 git clone https://github.com/aminb/dotfiles ~/.emacs.d
45 cd ~/.emacs.d
46 make bootstrap-borg
47 make bootstrap
48 make build
49 #+end_src
50
51 * Contents :toc_1:noexport:
52
53 - [[#about][About]]
54 - [[#header][Header]]
55 - [[#initial-setup][Initial setup]]
56 - [[#core][Core]]
57 - [[#post-initialization][Post initialization]]
58 - [[#footer][Footer]]
59
60 * Header
61 :PROPERTIES:
62 :CUSTOM_ID: header
63 :END:
64
65 ** First line
66
67 #+begin_src emacs-lisp :comments none
68 ;;; init.el --- Amin Bandali's Emacs config -*- lexical-binding: t ; eval: (view-mode 1)-*-
69 #+end_src
70
71 Enable =view-mode=, which both makes the file read-only (as a reminder
72 that =init.el= is an auto-generated file, not supposed to be edited),
73 and provides some convenient key bindings for browsing through the
74 file.
75
76 ** License
77
78 #+begin_src emacs-lisp :comments none
79 ;; Copyright (C) 2018 Amin Bandali <amin@aminb.org>
80
81 ;; This program is free software: you can redistribute it and/or modify
82 ;; it under the terms of the GNU General Public License as published by
83 ;; the Free Software Foundation, either version 3 of the License, or
84 ;; (at your option) any later version.
85
86 ;; This program is distributed in the hope that it will be useful,
87 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
88 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
89 ;; GNU General Public License for more details.
90
91 ;; You should have received a copy of the GNU General Public License
92 ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
93 #+end_src
94
95 ** Commentary
96
97 #+begin_src emacs-lisp :comments none
98 ;;; Commentary:
99
100 ;; Emacs configuration of Amin Bandali, computer scientist and functional
101 ;; programmer.
102
103 ;; THIS FILE IS AUTO-GENERATED FROM `init.org'.
104 #+end_src
105
106 ** Naming conventions
107
108 The conventions below were inspired by [[https://github.com/hlissner/doom-emacs][Doom]]'s conventions, found
109 [[https://github.com/hlissner/doom-emacs/blob/5dacbb7cb1c6ac246a9ccd15e6c4290def67757c/core/core.el#L3-L17][here]]. Naturally, I use my initials, =ab=, instead of =doom=.
110
111 #+begin_src emacs-lisp :comments none
112 ;; Naming conventions:
113 ;;
114 ;; ab-... public variables or non-interactive functions
115 ;; ab--... private anything (non-interactive), not safe for direct use
116 ;; ab/... an interactive function; safe for M-x or keybinding
117 ;; ab:... an evil operator, motion, or command
118 ;; ab|... a hook function
119 ;; ab*... an advising function
120 ;; ab@... a hydra command
121 ;; ...! a macro
122 #+end_src
123
124 * Initial setup
125 :PROPERTIES:
126 :CUSTOM_ID: initial-setup
127 :END:
128
129 #+begin_src emacs-lisp :comments none
130 ;;; Code:
131 #+end_src
132
133 ** Emacs initialization
134
135 I'd like to do a couple of measurements of Emacs' startup time. First,
136 let's see how long Emacs takes to start up, before even loading
137 =init.el=, i.e. =user-init-file=:
138
139 #+begin_src emacs-lisp
140 (defvar ab--before-user-init-time (current-time)
141 "Value of `current-time' when Emacs begins loading `user-init-file'.")
142 (message "Loading Emacs...done (%.3fs)"
143 (float-time (time-subtract ab--before-user-init-time
144 before-init-time)))
145 #+end_src
146
147 Also, temporarily increase ~gc-cons-threshhold~ and
148 ~gc-cons-percentage~ during startup to reduce garbage collection
149 frequency. Clearing the ~file-name-handler-alist~ seems to help reduce
150 startup time as well.
151
152 #+begin_src emacs-lisp
153 (defvar ab--gc-cons-threshold gc-cons-threshold)
154 (defvar ab--gc-cons-percentage gc-cons-percentage)
155 (defvar ab--file-name-handler-alist file-name-handler-alist)
156 (setq gc-cons-threshold (* 400 1024 1024) ; 400 MiB
157 gc-cons-percentage 0.6
158 file-name-handler-alist nil
159 ;; sidesteps a bug when profiling with esup
160 esup-child-profile-require-level 0)
161 #+end_src
162
163 Of course, we'd like to set them back to their defaults once we're
164 done initializing.
165
166 #+begin_src emacs-lisp
167 (add-hook
168 'after-init-hook
169 (lambda ()
170 (setq gc-cons-threshold ab--gc-cons-threshold
171 gc-cons-percentage ab--gc-cons-percentage
172 file-name-handler-alist ab--file-name-handler-alist)))
173 #+end_src
174
175 Increase the number of lines kept in message logs (the =*Messages*=
176 buffer).
177
178 #+begin_src emacs-lisp
179 (setq message-log-max 20000)
180 #+end_src
181
182 Optionally, we could suppress some byte compiler warnings like below,
183 but for now I've decided to keep them enabled. See documentation for
184 ~byte-compile-warnings~ for more details.
185
186 #+begin_src emacs-lisp
187 ;; (setq byte-compile-warnings
188 ;; '(not free-vars unresolved noruntime lexical make-local))
189 #+end_src
190
191 ** whoami
192
193 #+begin_src emacs-lisp
194 (setq user-full-name "Amin Bandali"
195 user-mail-address "amin@aminb.org")
196 #+end_src
197
198 ** Package management
199
200 *** No =package.el=
201
202 I can do all my package management things with Borg, and don't need
203 Emacs' built-in =package.el=. Emacs 27 lets us disable =package.el= in
204 the =early-init-file= (see [[https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=24acb31c04b4048b85311d794e600ecd7ce60d3b][here]]).
205
206 #+begin_src emacs-lisp :tangle early-init.el
207 (setq package-enable-at-startup nil)
208 #+end_src
209
210 But since Emacs 27 isn't out yet (Emacs 26 is just around the corner
211 right now), and even when released it'll be long before most distros
212 ship in their repos, I'll still put the old workaround with the
213 commented call to ~package-initialize~ here anyway.
214
215 #+begin_src emacs-lisp
216 (setq package-enable-at-startup nil)
217 ;; (package-initialize)
218 #+end_src
219
220 *** Borg
221
222 #+begin_quote
223 Assimilate Emacs packages as Git submodules
224 #+end_quote
225
226 [[https://github.com/emacscollective/borg][Borg]] is at the heart of package management of my Emacs setup. In
227 short, it creates a git submodule in =lib/= for each package, which
228 can then be managed with the help of Magit or other tools.
229
230 #+begin_src emacs-lisp
231 (setq user-init-file (or load-file-name buffer-file-name)
232 user-emacs-directory (file-name-directory user-init-file))
233 (add-to-list 'load-path
234 (expand-file-name "lib/borg" user-emacs-directory))
235 (require 'borg)
236 (borg-initialize)
237 #+end_src
238
239 *** =use-package=
240
241 #+begin_quote
242 A use-package declaration for simplifying your .emacs
243 #+end_quote
244
245 [[https://github.com/jwiegley/use-package][use-package]] is an awesome utility for managing and configuring
246 packages (in our case especially the latter) in a neatly organized way
247 and without compromising on performance.
248
249 #+begin_src emacs-lisp
250 (require 'use-package)
251 (if nil ; set to t when need to debug init
252 (setq use-package-verbose t
253 use-package-expand-minimally nil
254 use-package-compute-statistics t
255 debug-on-error t)
256 (setq use-package-verbose nil
257 use-package-expand-minimally t))
258 #+end_src
259
260 *** Epkg
261
262 #+begin_quote
263 Browse the Emacsmirror package database
264 #+end_quote
265
266 Epkg provides access to a local copy of the [[https://emacsmirror.net][Emacsmirror]] package
267 database, low-level functions for querying the database, and a
268 =package.el=-like user interface for browsing the available packages.
269
270 #+begin_src emacs-lisp
271 (use-package epkg
272 :defer t)
273 #+end_src
274
275 ** No littering in =~/.emacs.d=
276
277 #+begin_quote
278 Help keeping ~/.emacs.d clean
279 #+end_quote
280
281 By default, even for Emacs' built-in packages, the configuration files
282 and persistent data are all over the place. Use =no-littering= to help
283 contain the mess.
284
285 #+begin_src emacs-lisp
286 (use-package no-littering
287 :demand t
288 :config
289 (savehist-mode 1)
290 (add-to-list 'savehist-additional-variables 'kill-ring)
291 (save-place-mode 1)
292 (setq auto-save-file-name-transforms
293 `((".*" ,(no-littering-expand-var-file-name "auto-save/") t))))
294 #+end_src
295
296 ** Custom file (=custom.el=)
297
298 I'm not planning on using the custom file much, but even so, I
299 definitely don't want it mixing with =init.el=. So, here; let's give
300 it it's own file. While at it, treat themes as safe.
301
302 #+begin_src emacs-lisp
303 (use-package custom
304 :no-require t
305 :config
306 (setq custom-file (no-littering-expand-etc-file-name "custom.el"))
307 (when (file-exists-p custom-file)
308 (load custom-file))
309 (setf custom-safe-themes t))
310 #+end_src
311
312 ** Better =$PATH= handling
313
314 Let's use [[https://github.com/purcell/exec-path-from-shell][exec-path-from-shell]] to make Emacs use the =$PATH= as set up
315 in my shell.
316
317 #+begin_src emacs-lisp
318 (use-package exec-path-from-shell
319 :defer 1
320 :init
321 (setq exec-path-from-shell-check-startup-files nil)
322 :config
323 (exec-path-from-shell-initialize)
324 ;; while we're at it, let's fix access to our running ssh-agent
325 (exec-path-from-shell-copy-env "SSH_AGENT_PID")
326 (exec-path-from-shell-copy-env "SSH_AUTH_SOCK"))
327 #+end_src
328
329 ** Only one custom theme at a time
330
331 #+begin_src emacs-lisp
332 (defadvice load-theme (before clear-previous-themes activate)
333 "Clear existing theme settings instead of layering them"
334 (mapc #'disable-theme custom-enabled-themes))
335 #+end_src
336
337 ** Server
338
339 Start server if not already running. Alternatively, can be done by
340 issuing =emacs --daemon= in the terminal, which can be automated with
341 a systemd service or using =brew services start emacs= on macOS. I use
342 Emacs as my window manager (via EXWM), so I always start Emacs on
343 login; so starting the server from inside Emacs is good enough for me.
344
345 See [[https://www.gnu.org/software/emacs/manual/html_node/emacs/Emacs-Server.html#Emacs-Server][Using Emacs as a Server]].
346
347 #+begin_src emacs-lisp
348 (use-package server
349 :config (or (server-running-p) (server-mode)))
350 #+end_src
351
352 ** Unicode support
353
354 Font stack with better unicode support, around =Ubuntu Mono= and
355 =Hack=.
356
357 #+begin_src emacs-lisp
358 (dolist (ft (fontset-list))
359 (set-fontset-font
360 ft
361 'unicode
362 (font-spec :name "Ubuntu Mono"))
363 (set-fontset-font
364 ft
365 'unicode
366 (font-spec :name "DejaVu Sans Mono")
367 nil
368 'append)
369 ;; (set-fontset-font
370 ;; ft
371 ;; 'unicode
372 ;; (font-spec
373 ;; :name "Symbola monospacified for DejaVu Sans Mono")
374 ;; nil
375 ;; 'append)
376 ;; (set-fontset-font
377 ;; ft
378 ;; #x2115 ; â„•
379 ;; (font-spec :name "DejaVu Sans Mono")
380 ;; nil
381 ;; 'append)
382 (set-fontset-font
383 ft
384 (cons ?Α ?ω)
385 (font-spec :name "DejaVu Sans Mono" :size 14)
386 nil
387 'prepend))
388 #+end_src
389
390 ** Libraries
391
392 #+begin_src emacs-lisp
393 (require 'cl-lib)
394 (require 'subr-x)
395 #+end_src
396
397 ** Useful utilities
398
399 #+begin_src emacs-lisp
400 (defun ab-enlist (exp)
401 "Return EXP wrapped in a list, or as-is if already a list."
402 (if (listp exp) exp (list exp)))
403
404 ; from https://github.com/hlissner/doom-emacs/commit/589108fdb270f24a98ba6209f6955fe41530b3ef
405 (defmacro after! (features &rest body)
406 "A smart wrapper around `with-eval-after-load'. Supresses warnings during
407 compilation."
408 (declare (indent defun) (debug t))
409 (list (if (or (not (bound-and-true-p byte-compile-current-file))
410 (dolist (next (ab-enlist features))
411 (if (symbolp next)
412 (require next nil :no-error)
413 (load next :no-message :no-error))))
414 #'progn
415 #'with-no-warnings)
416 (cond ((symbolp features)
417 `(eval-after-load ',features '(progn ,@body)))
418 ((and (consp features)
419 (memq (car features) '(:or :any)))
420 `(progn
421 ,@(cl-loop for next in (cdr features)
422 collect `(after! ,next ,@body))))
423 ((and (consp features)
424 (memq (car features) '(:and :all)))
425 (dolist (next (cdr features))
426 (setq body `(after! ,next ,@body)))
427 body)
428 ((listp features)
429 `(after! (:all ,@features) ,@body)))))
430 #+end_src
431
432 * Core
433 :PROPERTIES:
434 :CUSTOM_ID: core
435 :END:
436
437 ** Defaults
438
439 *** Time and battery in mode-line
440
441 Enable displaying time and battery in the mode-line, since I'm not
442 using the Xfce panel anymore. Also, I don't need to see the load
443 average on a regular basis, so disable that.
444
445 #+begin_src emacs-lisp
446 (use-package time
447 :ensure nil
448 :init
449 (setq display-time-default-load-average nil)
450 :config
451 (display-time-mode))
452
453 (use-package battery
454 :ensure nil
455 :config
456 (display-battery-mode))
457 #+end_src
458
459 *** Smaller fringe
460
461 Might want to set the fringe to a smaller value, especially if using
462 EXWM. I'm fine with the default for now.
463
464 #+begin_src emacs-lisp
465 ;; (fringe-mode '(3 . 1))
466 (fringe-mode nil)
467 #+end_src
468
469 *** Disable disabled commands
470
471 Emacs disables some commands by default that could persumably be
472 confusing for novice users. Let's disable that.
473
474 #+begin_src emacs-lisp
475 (setq disabled-command-function nil)
476 #+end_src
477
478 *** Kill-ring
479
480 Save what I copy into clipboard from other applications into Emacs'
481 kill-ring, which would allow me to still be able to easily access it
482 in case I kill (cut or copy) something else inside Emacs before
483 yanking (pasting) what I'd originally intended to.
484
485 #+begin_src emacs-lisp
486 (setq save-interprogram-paste-before-kill t)
487 #+end_src
488
489 *** Minibuffer
490
491 #+begin_src emacs-lisp
492 (setq enable-recursive-minibuffers t
493 resize-mini-windows t)
494 #+end_src
495
496 *** Lazy-person-friendly yes/no prompts
497
498 Lazy people would prefer to type fewer keystrokes, especially for yes
499 or no questions. I'm lazy.
500
501 #+begin_src emacs-lisp
502 (defalias 'yes-or-no-p #'y-or-n-p)
503 #+end_src
504
505 *** Startup screen and =*scratch*=
506
507 Firstly, let Emacs know that I'd like to have =*scratch*= as my
508 startup buffer.
509
510 #+begin_src emacs-lisp
511 (setq initial-buffer-choice t)
512 #+end_src
513
514 Now let's customize the =*scratch*= buffer a bit. First off, I don't
515 need the default hint.
516
517 #+begin_src emacs-lisp
518 (setq initial-scratch-message nil)
519 #+end_src
520
521 Also, let's use Text mode as the major mode, in case I want to
522 customize it (=*scratch*='s default major mode, Fundamental mode,
523 can't really be customized).
524
525 #+begin_src emacs-lisp
526 (setq initial-major-mode 'text-mode)
527 #+end_src
528
529 Inhibit the buffer list when more than 2 files are loaded.
530
531 #+begin_src emacs-lisp
532 (setq inhibit-startup-buffer-menu t)
533 #+end_src
534
535 I don't really need to see the startup screen or echo area message
536 either.
537
538 #+begin_src emacs-lisp
539 (advice-add #'display-startup-echo-area-message :override #'ignore)
540 (setq inhibit-startup-screen t
541 inhibit-startup-echo-area-message user-login-name)
542 #+end_src
543
544 *** More useful frame titles
545
546 Show either the file name or the buffer name (in case the buffer isn't
547 visiting a file). Borrowed from Emacs Prelude.
548
549 #+begin_src emacs-lisp
550 (setq frame-title-format
551 '("" invocation-name " - "
552 (:eval (if (buffer-file-name)
553 (abbreviate-file-name (buffer-file-name))
554 "%b"))))
555 #+end_src
556
557 *** Backups
558
559 Emacs' default backup settings aren't that great. Let's use more
560 sensible options. See documentation for the ~make-backup-file~
561 variable.
562
563 #+begin_src emacs-lisp
564 (setq backup-by-copying t
565 version-control t)
566 #+end_src
567
568 *** Auto revert
569
570 Enable automatic reloading of changed buffers and files.
571
572 #+begin_src emacs-lisp
573 (global-auto-revert-mode 1)
574 (setq auto-revert-verbose nil
575 global-auto-revert-non-file-buffers t)
576 #+end_src
577
578 *** Always use space for indentation
579
580 #+begin_src emacs-lisp
581 (setq-default
582 indent-tabs-mode nil
583 require-final-newline t
584 tab-width 4)
585 #+end_src
586
587 ** Packages
588
589 The packages in this section are absolutely essential to my everyday
590 workflow, and they play key roles in how I do my computing. They
591 immensely enhance the Emacs experience for me; both using Emacs, and
592 customizing it.
593
594 *** [[https://github.com/emacscollective/auto-compile][auto-compile]]
595
596 #+begin_src emacs-lisp
597 (use-package auto-compile
598 :demand t
599 :config
600 (auto-compile-on-load-mode)
601 (auto-compile-on-save-mode)
602 (setq auto-compile-display-buffer nil
603 auto-compile-mode-line-counter t
604 auto-compile-source-recreate-deletes-dest t
605 auto-compile-toggle-deletes-nonlib-dest t
606 auto-compile-update-autoloads t)
607 (add-hook 'auto-compile-inhibit-compile-hook
608 'auto-compile-inhibit-compile-detached-git-head))
609 #+end_src
610
611 *** [[https://github.com/Kungsgeten/ryo-modal][ryo-modal]]
612
613 #+begin_quote
614 Roll your own modal mode
615 #+end_quote
616
617 #+begin_src emacs-lisp
618 (use-package ryo-modal
619 :commands ryo-modal-mode
620 :bind ("M-m" . ryo-modal-mode)
621 :after which-key
622 :config
623 (push '((nil . "ryo:.*:") . (nil . "")) which-key-replacement-alist)
624 (ryo-modal-keys
625 ("," ryo-modal-repeat)
626 ("b" backward-char)
627 ("n" next-line)
628 ("p" previous-line)
629 ("f" forward-char)
630 ("a" move-beginning-of-line)
631 ("e" move-end-of-line)
632 ("SPC" (("b" (("b" ibuffer-list-buffers)
633 ("k" kill-this-buffer)
634 ("o" other-window)
635 ("s" save-buffer)))
636 ("h" (("c" describe-char)
637 ("f" describe-function)
638 ("F" describe-face)
639 ("i" info)
640 ("k" describe-key)
641 ("l" view-lossage)
642 ("v" describe-variable)))
643 ("q" (("q" save-buffers-kill-terminal)))))
644 ("d" (("w" kill-word)
645 ("d" kill-whole-line)
646 ("b" backward-kill-word)))
647 ("c" (("w" kill-word :exit t)
648 ("c" kill-whole-line :exit t))))
649
650 (ryo-modal-keys
651 ;; First argyment to ryo-modal-keys may be a list of keywords.
652 ;; These keywords will be applied to all keybindings.
653 (:norepeat t)
654 ("0" "M-0")
655 ("1" "M-1")
656 ("2" "M-2")
657 ("3" "M-3")
658 ("4" "M-4")
659 ("5" "M-5")
660 ("6" "M-6")
661 ("7" "M-7")
662 ("8" "M-8")
663 ("9" "M-9"))
664 :hook ((text-mode . ryo-modal-mode)
665 (prog-mode . ryo-modal-mode)))
666 #+end_src
667
668 *** [[https://orgmode.org/][Org mode]]
669
670 #+begin_quote
671 Org mode is for keeping notes, maintaining TODO lists, planning
672 projects, and authoring documents with a fast and effective plain-text
673 system.
674 #+end_quote
675
676 In short, my favourite way of life.
677
678 #+begin_src emacs-lisp
679 (use-package org
680 :ryo ("SPC b t" org-babel-tangle)
681 :config
682 (setq org-src-tab-acts-natively t
683 org-src-preserve-indentation nil
684 org-edit-src-content-indentation 0
685 org-html-divs '((preamble "header" "preamble")
686 (content "main" "content")
687 (postamble "footer" "postamble"))
688 org-html-doctype "html5"
689 org-html-html5-fancy t
690 org-html-postamble nil)
691 :hook (org-mode . org-indent-mode))
692 (use-package htmlize
693 :after org)
694 (use-package org-notmuch
695 :after (:any org notmuch))
696 #+end_src
697
698 *** [[https://magit.vc/][Magit]]
699
700 #+begin_quote
701 It's Magit! A Git porcelain inside Emacs.
702 #+end_quote
703
704 Not just how I do git, but /the/ way to do git.
705
706 #+begin_src emacs-lisp
707 (use-package magit
708 :ryo ("SPC" (("g s" magit-status)))
709 :defer t
710 :bind (("s-g" . magit-status)
711 ("C-x g" . magit-status)
712 ("C-x M-g" . magit-dispatch-popup))
713 :config
714 (magit-add-section-hook 'magit-status-sections-hook
715 'magit-insert-modules
716 'magit-insert-stashes
717 'append))
718 #+end_src
719
720 *** [[https://github.com/abo-abo/swiper][Ivy]] (and friends)
721
722 #+begin_quote
723 Ivy - a generic completion frontend for Emacs, Swiper - isearch with
724 an overview, and more. Oh, man!
725 #+end_quote
726
727 There's no way I could top that, so I won't attempt to.
728
729 **** Ivy
730
731 #+begin_src emacs-lisp
732 (use-package ivy
733 :defer 1
734 :bind
735 (:map ivy-minibuffer-map
736 ([escape] . keyboard-escape-quit)
737 ;; ("C-j" . ivy-next-line)
738 ;; ("C-k" . ivy-previous-line)
739 ([S-up] . ivy-previous-history-element)
740 ([S-down] . ivy-next-history-element)
741 ("DEL" . ivy-backward-delete-char))
742 :ryo ("SPC ," ivy-switch-buffer)
743 :config
744 (setq ivy-wrap t)
745 (ivy-mode 1))
746 #+end_src
747
748 **** Swiper
749
750 #+begin_src emacs-lisp
751 (use-package swiper
752 :ryo
753 ("SPC /" swiper)
754 ("s" swiper)
755 :bind (([remap isearch-forward] . swiper)
756 ([remap isearch-backward] . swiper)))
757 #+end_src
758
759 **** Counsel
760
761 #+begin_src emacs-lisp
762 (use-package counsel
763 :defer 1
764 :ryo
765 ("SPC" (("f r" counsel-recentf)
766 ("SPC" counsel-M-x)
767 ("." counsel-find-file)))
768 :bind (([remap execute-extended-command] . counsel-M-x)
769 ([remap find-file] . counsel-find-file)
770 ("s-r" . counsel-recentf)
771 :map minibuffer-local-map
772 ("C-r" . counsel-minibuffer-history))
773 :config
774 (counsel-mode 1)
775 (defalias 'locate #'counsel-locate))
776 #+end_src
777
778 * Borg's =layer/essentials=
779
780 TODO: break this giant source block down into individual org sections.
781
782 #+begin_src emacs-lisp
783 (use-package dash
784 :config (dash-enable-font-lock))
785
786 (use-package diff-hl
787 :config
788 (setq diff-hl-draw-borders nil)
789 (global-diff-hl-mode)
790 (add-hook 'magit-post-refresh-hook 'diff-hl-magit-post-refresh t))
791
792 (use-package dired
793 :defer t
794 :config (setq dired-listing-switches "-alh"))
795
796 (use-package eldoc
797 :when (version< "25" emacs-version)
798 :config (global-eldoc-mode))
799
800 (use-package help
801 :defer t
802 :config (temp-buffer-resize-mode))
803
804 (progn ; `isearch'
805 (setq isearch-allow-scroll t))
806
807 (use-package lisp-mode
808 :config
809 (add-hook 'emacs-lisp-mode-hook 'outline-minor-mode)
810 (add-hook 'emacs-lisp-mode-hook 'reveal-mode)
811 (defun indent-spaces-mode ()
812 (setq indent-tabs-mode nil))
813 (add-hook 'lisp-interaction-mode-hook #'indent-spaces-mode))
814
815 (use-package man
816 :defer t
817 :config (setq Man-width 80))
818
819 (use-package paren
820 :config (show-paren-mode))
821
822 (use-package prog-mode
823 :config (global-prettify-symbols-mode)
824 (defun indicate-buffer-boundaries-left ()
825 (setq indicate-buffer-boundaries 'left))
826 (add-hook 'prog-mode-hook #'indicate-buffer-boundaries-left))
827
828 (use-package recentf
829 :demand t
830 :config (add-to-list 'recentf-exclude "^/\\(?:ssh\\|su\\|sudo\\)?:"))
831
832 (use-package savehist
833 :config (savehist-mode))
834
835 (use-package saveplace
836 :when (version< "25" emacs-version)
837 :config (save-place-mode))
838
839 (use-package simple
840 :config (column-number-mode))
841
842 (progn ; `text-mode'
843 (add-hook 'text-mode-hook #'indicate-buffer-boundaries-left))
844
845 (use-package tramp
846 :defer t
847 :config
848 (add-to-list 'tramp-default-proxies-alist '(nil "\\`root\\'" "/ssh:%h:"))
849 (add-to-list 'tramp-default-proxies-alist '("localhost" nil nil))
850 (add-to-list 'tramp-default-proxies-alist
851 (list (regexp-quote (system-name)) nil nil)))
852
853 (use-package undo-tree
854 :ryo
855 ("/" undo-tree-undo)
856 ("_" undo-tree-redo)
857 :bind (("C-/" . undo-tree-undo)
858 ("M-_" . undo-tree-redo))
859 :config
860 (global-undo-tree-mode)
861 (setq undo-tree-mode-lighter ""))
862 #+end_src
863
864 * Editing
865
866 ** Company
867
868 #+begin_src emacs-lisp
869 (use-package company
870 :defer 5
871 :bind
872 (:map company-active-map
873 ([tab] . company-complete-common-or-cycle))
874 :custom
875 (company-idle-delay 0.3)
876 (company-minimum-prefix-length 1)
877 (company-selection-wrap-around t)
878 (company-dabbrev-char-regexp "\\sw\\|\\s_\\|[-_]")
879 :config
880 (global-company-mode t))
881 #+end_src
882
883 * Syntax and spell checking
884 #+begin_src emacs-lisp
885 (use-package flycheck
886 :hook (prog-mode . flycheck-mode)
887 :config
888 ;; Use the load-path from running Emacs when checking elisp files
889 (setq flycheck-emacs-lisp-load-path 'inherit)
890
891 ;; Only flycheck when I actually save the buffer
892 (setq flycheck-check-syntax-automatically '(mode-enabled save)))
893 #+end_src
894 * Programming modes
895
896 ** [[http://alloytools.org][Alloy]] (with [[https://github.com/dwwmmn/alloy-mode][alloy-mode]])
897
898 #+begin_src emacs-lisp
899 (use-package alloy-mode
900 :config (setq alloy-basic-offset 2))
901 #+end_src
902
903 ** [[https://coq.inria.fr][Coq]] (with [[https://github.com/ProofGeneral/PG][Proof General]])
904
905 #+begin_src emacs-lisp
906 (use-package proof-site ; Proof General
907 :load-path "lib/proof-site/generic/")
908 #+end_src
909
910 ** [[https://leanprover.github.io][Lean]] (with [[https://github.com/leanprover/lean-mode][lean-mode]])
911
912 #+begin_src emacs-lisp
913 (use-package lean-mode
914 :bind (:map lean-mode-map
915 ("S-SPC" . company-complete)))
916 #+end_src
917
918 ** Haskell
919
920 *** [[https://github.com/haskell/haskell-mode][haskell-mode]]
921
922 #+begin_src emacs-lisp
923 (use-package haskell-mode
924 :config
925 (setq haskell-indentation-layout-offset 4
926 haskell-indentation-left-offset 4
927 flycheck-checker 'haskell-hlint
928 flycheck-disabled-checkers '(haskell-stack-ghc haskell-ghc)))
929 #+end_src
930
931 *** [[https://github.com/jyp/dante][dante]]
932
933 #+begin_src emacs-lisp
934 (use-package dante
935 :after haskell-mode
936 :commands dante-mode
937 :hook (haskell-mode . dante-mode))
938 #+end_src
939
940 *** [[https://github.com/mpickering/hlint-refactor-mode][hlint-refactor]]
941
942 Emacs bindings for [[https://github.com/ndmitchell/hlint][hlint]]'s refactor option. This requires the refact
943 executable from [[https://github.com/mpickering/apply-refact][apply-refact]].
944
945 #+begin_src emacs-lisp
946 (use-package hlint-refactor
947 :bind (:map hlint-refactor-mode-map
948 ("C-c l b" . hlint-refactor-refactor-buffer)
949 ("C-c l r" . hlint-refactor-refactor-at-point))
950 :hook (haskell-mode . hlint-refactor-mode))
951 #+end_src
952
953 *** [[https://github.com/flycheck/flycheck-haskell][flycheck-haskell]]
954
955 #+begin_src emacs-lisp
956 (use-package flycheck-haskell)
957 #+end_src
958
959 *** [[https://github.com/ndmitchell/hlint/blob/20e116a043f2073c57b17b24ae6364b5e433ba7e/data/hs-lint.el][hs-lint.el]]
960 :PROPERTIES:
961 :header-args+: :tangle lisp/hs-lint.el :mkdirp yes
962 :END:
963
964 Currently using =flycheck-haskell= with the =haskell-hlint= checker
965 instead.
966
967 #+begin_src emacs-lisp :tangle no
968 ;;; hs-lint.el --- minor mode for HLint code checking
969
970 ;; Copyright 2009 (C) Alex Ott
971 ;;
972 ;; Author: Alex Ott <alexott@gmail.com>
973 ;; Keywords: haskell, lint, HLint
974 ;; Requirements:
975 ;; Status: distributed under terms of GPL2 or above
976
977 ;; Typical message from HLint looks like:
978 ;;
979 ;; /Users/ott/projects/lang-exp/haskell/test.hs:52:1: Eta reduce
980 ;; Found:
981 ;; count1 p l = length (filter p l)
982 ;; Why not:
983 ;; count1 p = length . filter p
984
985
986 (require 'compile)
987
988 (defgroup hs-lint nil
989 "Run HLint as inferior of Emacs, parse error messages."
990 :group 'tools
991 :group 'haskell)
992
993 (defcustom hs-lint-command "hlint"
994 "The default hs-lint command for \\[hlint]."
995 :type 'string
996 :group 'hs-lint)
997
998 (defcustom hs-lint-save-files t
999 "Save modified files when run HLint or no (ask user)"
1000 :type 'boolean
1001 :group 'hs-lint)
1002
1003 (defcustom hs-lint-replace-with-suggestions nil
1004 "Replace user's code with suggested replacements"
1005 :type 'boolean
1006 :group 'hs-lint)
1007
1008 (defcustom hs-lint-replace-without-ask nil
1009 "Replace user's code with suggested replacements automatically"
1010 :type 'boolean
1011 :group 'hs-lint)
1012
1013 (defun hs-lint-process-setup ()
1014 "Setup compilation variables and buffer for `hlint'."
1015 (run-hooks 'hs-lint-setup-hook))
1016
1017 ;; regex for replace suggestions
1018 ;;
1019 ;; ^\(.*?\):\([0-9]+\):\([0-9]+\): .*
1020 ;; Found:
1021 ;; \s +\(.*\)
1022 ;; Why not:
1023 ;; \s +\(.*\)
1024
1025 (defvar hs-lint-regex
1026 "^\\(.*?\\):\\([0-9]+\\):\\([0-9]+\\): .*[\n\C-m]Found:[\n\C-m]\\s +\\(.*\\)[\n\C-m]Why not:[\n\C-m]\\s +\\(.*\\)[\n\C-m]"
1027 "Regex for HLint messages")
1028
1029 (defun make-short-string (str maxlen)
1030 (if (< (length str) maxlen)
1031 str
1032 (concat (substring str 0 (- maxlen 3)) "...")))
1033
1034 (defun hs-lint-replace-suggestions ()
1035 "Perform actual replacement of suggestions"
1036 (goto-char (point-min))
1037 (while (re-search-forward hs-lint-regex nil t)
1038 (let* ((fname (match-string 1))
1039 (fline (string-to-number (match-string 2)))
1040 (old-code (match-string 4))
1041 (new-code (match-string 5))
1042 (msg (concat "Replace '" (make-short-string old-code 30)
1043 "' with '" (make-short-string new-code 30) "'"))
1044 (bline 0)
1045 (eline 0)
1046 (spos 0)
1047 (new-old-code ""))
1048 (save-excursion
1049 (switch-to-buffer (get-file-buffer fname))
1050 (goto-char (point-min))
1051 (forward-line (1- fline))
1052 (beginning-of-line)
1053 (setf bline (point))
1054 (when (or hs-lint-replace-without-ask
1055 (yes-or-no-p msg))
1056 (end-of-line)
1057 (setf eline (point))
1058 (beginning-of-line)
1059 (setf old-code (regexp-quote old-code))
1060 (while (string-match "\\\\ " old-code spos)
1061 (setf new-old-code (concat new-old-code
1062 (substring old-code spos (match-beginning 0))
1063 "\\ *"))
1064 (setf spos (match-end 0)))
1065 (setf new-old-code (concat new-old-code (substring old-code spos)))
1066 (remove-text-properties bline eline '(composition nil))
1067 (when (re-search-forward new-old-code eline t)
1068 (replace-match new-code nil t)))))))
1069
1070 (defun hs-lint-finish-hook (buf msg)
1071 "Function, that is executed at the end of HLint execution"
1072 (if hs-lint-replace-with-suggestions
1073 (hs-lint-replace-suggestions)
1074 (next-error 1 t)))
1075
1076 (define-compilation-mode hs-lint-mode "HLint"
1077 "Mode for check Haskell source code."
1078 (set (make-local-variable 'compilation-process-setup-function)
1079 'hs-lint-process-setup)
1080 (set (make-local-variable 'compilation-disable-input) t)
1081 (set (make-local-variable 'compilation-scroll-output) nil)
1082 (set (make-local-variable 'compilation-finish-functions)
1083 (list 'hs-lint-finish-hook))
1084 )
1085
1086 (defun hs-lint ()
1087 "Run HLint for current buffer with haskell source"
1088 (interactive)
1089 (save-some-buffers hs-lint-save-files)
1090 (compilation-start (concat hs-lint-command " \"" buffer-file-name "\"")
1091 'hs-lint-mode))
1092
1093 (provide 'hs-lint)
1094 ;;; hs-lint.el ends here
1095 #+end_src
1096
1097 #+begin_src emacs-lisp :tangle no
1098 (use-package hs-lint
1099 :load-path "lisp/"
1100 :bind (:map haskell-mode-map
1101 ("C-c l l" . hs-lint)))
1102 #+end_src
1103 * Emacs Enhancements
1104
1105 ** [[https://github.com/justbur/emacs-which-key][which-key]]
1106
1107 #+begin_quote
1108 Emacs package that displays available keybindings in popup
1109 #+end_quote
1110
1111 #+begin_src emacs-lisp
1112 (use-package which-key
1113 :defer 1
1114 :config (which-key-mode))
1115 #+end_src
1116
1117 ** [[https://github.com/seagle0128/doom-modeline][doom-modeline]]
1118
1119 #+begin_src emacs-lisp
1120 (use-package doom-modeline
1121 :demand t
1122 :config (setq doom-modeline-height 32)
1123 :hook (after-init . doom-modeline-init))
1124 #+end_src
1125
1126 ** [[https://github.com/11111000000/tao-theme-emacs][tao-theme]]
1127
1128 #+begin_src emacs-lisp :tangle no
1129 (use-package tao-theme
1130 :demand t
1131 :config (load-theme 'tao-yang t))
1132 #+end_src
1133
1134 ** [[https://github.com/maio/eink-emacs][eink-theme]]
1135
1136 #+begin_src emacs-lisp
1137 (load-theme 'eink t)
1138 #+end_src
1139
1140 * Email
1141 ** [[https://notmuchmail.org][notmuch]]
1142
1143 See [[notmuch:id:87muuqsvci.fsf@fencepost.gnu.org][bug follow-up]].
1144
1145 #+begin_src emacs-lisp
1146 (defun ab/notmuch ()
1147 "Delete other windows, then launch `notmuch'."
1148 (interactive)
1149 (require 'notmuch)
1150 (delete-other-windows)
1151 (notmuch))
1152
1153 ;; (map!
1154 ;; :leader
1155 ;; :desc "notmuch" :n "m" #'ab/notmuch
1156 ;; (:desc "search" :prefix "/"
1157 ;; :desc "notmuch" :n "m" #'counsel-notmuch))
1158 #+end_src
1159
1160 #+begin_src emacs-lisp
1161 (defvar ab-maildir "~/mail")
1162
1163 (use-package sendmail
1164 ;; :ensure nil
1165 :config
1166 (setq sendmail-program "/usr/bin/msmtp"
1167 mail-specify-envelope-from t
1168 mail-envelope-from 'header))
1169
1170 (use-package message
1171 ;; :ensure nil
1172 :config
1173 (setq message-kill-buffer-on-exit t
1174 message-send-mail-function 'message-send-mail-with-sendmail
1175 message-sendmail-envelope-from 'header
1176 message-directory "drafts"
1177 message-user-fqdn "aminb.org")
1178 (add-hook 'message-mode-hook
1179 (lambda () (setq fill-column 65
1180 message-fill-column 65)))
1181 (add-hook 'message-mode-hook
1182 #'flyspell-mode)
1183 ;; (add-hook 'notmuch-message-mode-hook #'+doom-modeline|set-special-modeline)
1184 ;; TODO: is there a way to only run this when replying and not composing?
1185 ;; (add-hook 'notmuch-message-mode-hook
1186 ;; (lambda () (progn
1187 ;; (newline)
1188 ;; (newline)
1189 ;; (forward-line -1)
1190 ;; (forward-line -1))))
1191 ;; (add-hook 'message-setup-hook
1192 ;; #'mml-secure-message-sign-pgpmime)
1193 )
1194
1195 (after! mml-sec
1196 (setq mml-secure-openpgp-encrypt-to-self t
1197 mml-secure-openpgp-sign-with-sender t))
1198
1199 (use-package notmuch
1200 :ryo ("SPC m" ab/notmuch)
1201 :config
1202 (setq notmuch-hello-sections
1203 '(notmuch-hello-insert-header
1204 notmuch-hello-insert-saved-searches
1205 ;; notmuch-hello-insert-search
1206 notmuch-hello-insert-alltags)
1207 notmuch-search-oldest-first nil
1208 notmuch-show-all-tags-list t
1209 notmuch-message-headers ; see bug follow-up above
1210 '("Subject" "To" "Cc" "Date" "List-Id" "X-RT-Originator")
1211 notmuch-hello-thousands-separator ","
1212 notmuch-fcc-dirs
1213 '(("amin@aminb.org" . "amin/Sent")
1214 ("abandali@uwaterloo.ca" . "\"uwaterloo/Sent Items\"")
1215 ("amin.bandali@uwaterloo.ca" . "\"uwaterloo/Sent Items\"")
1216 ("aminb@gnu.org" . "gnu/Sent")
1217 (".*" . "sent"))
1218 notmuch-search-result-format
1219 '(("date" . "%12s ")
1220 ("count" . "%-7s ")
1221 ("authors" . "%-40s ")
1222 ("subject" . "%s ")
1223 ("tags" . "(%s)")))
1224 ;; (add-hook 'visual-fill-column-mode-hook
1225 ;; (lambda ()
1226 ;; (when (string= major-mode 'notmuch-message-mode)
1227 ;; (setq visual-fill-column-width 70))))
1228 ;; (set! :evil-state 'notmuch-message-mode 'insert)
1229 ;; (advice-add #'notmuch-bury-or-kill-this-buffer
1230 ;; :override #'kill-this-buffer)
1231 :bind
1232 (:map notmuch-hello-mode-map
1233 ("g" . notmuch-poll-and-refresh-this-buffer)
1234 ("i" . (lambda ()
1235 "Search for `inbox' tagged messages"
1236 (interactive)
1237 (notmuch-hello-search "tag:inbox")))
1238 ("u" . (lambda ()
1239 "Search for `unread' tagged messages"
1240 (interactive)
1241 (notmuch-hello-search "tag:unread")))
1242 ("l" . (lambda ()
1243 "Search for `latest tagged messages"
1244 (interactive)
1245 (notmuch-hello-search "tag:latest")))
1246 ("M" . (lambda ()
1247 "Compose new mail and prompt for sender"
1248 (interactive)
1249 (let ((current-prefix-arg t))
1250 (call-interactively #'notmuch-mua-new-mail)))))
1251 (:map notmuch-search-mode-map
1252 ("g" . notmuch-poll-and-refresh-this-buffer)
1253 ("k" . (lambda ()
1254 "Mark message read"
1255 (interactive)
1256 (notmuch-search-tag '("-unread"))
1257 ;; (notmuch-search-archive-thread)
1258 (notmuch-search-next-thread)))
1259 ("u" . (lambda ()
1260 "Mark message unread"
1261 (interactive)
1262 (notmuch-search-tag '("+unread"))
1263 (notmuch-search-next-thread)))
1264 ("K" . (lambda ()
1265 "Mark message deleted"
1266 (interactive)
1267 (notmuch-search-tag '("-unread" "-inbox" "+deleted"))
1268 (notmuch-search-archive-thread)))
1269 ("S" . (lambda ()
1270 "Mark message as spam"
1271 (interactive)
1272 (notmuch-search-tag '("-unread" "-inbox" "-webmasters" "+spam"))
1273 (notmuch-search-archive-thread))))
1274 (:map notmuch-tree-mode-map ; TODO: additional bindings
1275 ("S" . (lambda ()
1276 "Mark message as spam"
1277 (interactive)
1278 (notmuch-tree-tag '("-unread" "-inbox" "-webmasters" "+spam"))
1279 (notmuch-tree-archive-thread))))
1280 )
1281
1282 ;; (use-package counsel-notmuch
1283 ;; :commands counsel-notmuch)
1284
1285 (after! notmuch-crypto
1286 (setq notmuch-crypto-process-mime t))
1287
1288 ;; (after! evil
1289 ;; (mapc (lambda (str) (evil-set-initial-state (car str) (cdr str)))
1290 ;; '((notmuch-hello-mode . emacs)
1291 ;; (notmuch-search-mode . emacs)
1292 ;; (notmuch-tree-mode . emacs))))
1293
1294 (after! recentf
1295 (add-to-list 'recentf-exclude (expand-file-name ab-maildir)))
1296 #+end_src
1297
1298 ** supercite
1299
1300 #+begin_src emacs-lisp :tangle no
1301 (use-package supercite
1302 :commands sc-cite-original
1303 :init
1304 (add-hook 'mail-citation-hook 'sc-cite-original)
1305
1306 (defun sc-remove-existing-signature ()
1307 (save-excursion
1308 (goto-char (region-beginning))
1309 (when (re-search-forward message-signature-separator (region-end) t)
1310 (delete-region (match-beginning 0) (region-end)))))
1311
1312 (add-hook 'mail-citation-hook 'sc-remove-existing-signature)
1313
1314 (defun sc-remove-if-not-mailing-list ()
1315 (unless (assoc "list-id" sc-mail-info)
1316 (setq attribution sc-default-attribution
1317 citation (concat sc-citation-delimiter
1318 sc-citation-separator))))
1319
1320 (add-hook 'sc-attribs-postselect-hook 'sc-remove-if-not-mailing-list)
1321
1322 :config
1323 (defun sc-fill-if-different (&optional prefix)
1324 "Fill the region bounded by `sc-fill-begin' and point.
1325 Only fill if optional PREFIX is different than
1326 `sc-fill-line-prefix'. If `sc-auto-fill-region-p' is nil, do not
1327 fill region. If PREFIX is not supplied, initialize fill
1328 variables. This is useful for a regi `begin' frame-entry."
1329 (if (not prefix)
1330 (setq sc-fill-line-prefix ""
1331 sc-fill-begin (line-beginning-position))
1332 (if (and sc-auto-fill-region-p
1333 (not (string= prefix sc-fill-line-prefix)))
1334 (let ((fill-prefix sc-fill-line-prefix))
1335 (unless (or (string= fill-prefix "")
1336 (save-excursion
1337 (goto-char sc-fill-begin)
1338 (or (looking-at ">+ +")
1339 (< (length
1340 (buffer-substring (point)
1341 (line-end-position)))
1342 65))))
1343 (fill-region sc-fill-begin (line-beginning-position)))
1344 (setq sc-fill-line-prefix prefix
1345 sc-fill-begin (line-beginning-position)))))
1346 nil))
1347 #+end_src
1348
1349 * Blogging
1350 ** [[https://ox-hugo.scripter.co][ox-hugo]]
1351
1352 #+begin_src emacs-lisp
1353 (use-package ox-hugo
1354 :after ox)
1355 #+end_src
1356
1357 * Post initialization
1358 :PROPERTIES:
1359 :CUSTOM_ID: post-initialization
1360 :END:
1361
1362 Display how long it took to load the init file.
1363
1364 #+begin_src emacs-lisp
1365 (message "Loading %s...done (%.3fs)" user-init-file
1366 (float-time (time-subtract (current-time)
1367 ab--before-user-init-time)))
1368 #+end_src
1369
1370 * Footer
1371 :PROPERTIES:
1372 :CUSTOM_ID: footer
1373 :END:
1374
1375 #+begin_src emacs-lisp :comments none
1376 ;;; init.el ends here
1377 #+end_src