b19ffe28b3134f1186142d88189488f84eaa8fac
[~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 tangle-init
48 make bootstrap
49 make build
50 #+end_src
51
52 * Contents :toc_1:noexport:
53
54 - [[#about][About]]
55 - [[#header][Header]]
56 - [[#initial-setup][Initial setup]]
57 - [[#core][Core]]
58 - [[#post-initialization][Post initialization]]
59 - [[#footer][Footer]]
60
61 * Header
62 :PROPERTIES:
63 :CUSTOM_ID: header
64 :END:
65
66 ** First line
67
68 #+begin_src emacs-lisp :comments none
69 ;;; init.el --- Amin Bandali's Emacs config -*- lexical-binding: t ; eval: (view-mode 1)-*-
70 #+end_src
71
72 Enable =view-mode=, which both makes the file read-only (as a reminder
73 that =init.el= is an auto-generated file, not supposed to be edited),
74 and provides some convenient key bindings for browsing through the
75 file.
76
77 ** License
78
79 #+begin_src emacs-lisp :comments none
80 ;; Copyright (C) 2018 Amin Bandali <amin@aminb.org>
81
82 ;; This program is free software: you can redistribute it and/or modify
83 ;; it under the terms of the GNU General Public License as published by
84 ;; the Free Software Foundation, either version 3 of the License, or
85 ;; (at your option) any later version.
86
87 ;; This program is distributed in the hope that it will be useful,
88 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
89 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
90 ;; GNU General Public License for more details.
91
92 ;; You should have received a copy of the GNU General Public License
93 ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
94 #+end_src
95
96 ** Commentary
97
98 #+begin_src emacs-lisp :comments none
99 ;;; Commentary:
100
101 ;; Emacs configuration of Amin Bandali, computer scientist and functional
102 ;; programmer.
103
104 ;; THIS FILE IS AUTO-GENERATED FROM `init.org'.
105 #+end_src
106
107 ** Naming conventions
108
109 The conventions below were inspired by [[https://github.com/hlissner/doom-emacs][Doom]]'s conventions, found
110 [[https://github.com/hlissner/doom-emacs/blob/5dacbb7cb1c6ac246a9ccd15e6c4290def67757c/core/core.el#L3-L17][here]]. Naturally, I use my initials, =ab=, instead of =doom=.
111
112 #+begin_src emacs-lisp :comments none
113 ;; Naming conventions:
114 ;;
115 ;; ab-... public variables or non-interactive functions
116 ;; ab--... private anything (non-interactive), not safe for direct use
117 ;; ab/... an interactive function; safe for M-x or keybinding
118 ;; ab:... an evil operator, motion, or command
119 ;; ab|... a hook function
120 ;; ab*... an advising function
121 ;; ab@... a hydra command
122 ;; ...! a macro
123 #+end_src
124
125 * Initial setup
126 :PROPERTIES:
127 :CUSTOM_ID: initial-setup
128 :END:
129
130 #+begin_src emacs-lisp :comments none
131 ;;; Code:
132 #+end_src
133
134 ** Emacs initialization
135
136 I'd like to do a couple of measurements of Emacs' startup time. First,
137 let's see how long Emacs takes to start up, before even loading
138 =init.el=, i.e. =user-init-file=:
139
140 #+begin_src emacs-lisp
141 (defvar ab--before-user-init-time (current-time)
142 "Value of `current-time' when Emacs begins loading `user-init-file'.")
143 (message "Loading Emacs...done (%.3fs)"
144 (float-time (time-subtract ab--before-user-init-time
145 before-init-time)))
146 #+end_src
147
148 Also, temporarily increase ~gc-cons-threshhold~ and
149 ~gc-cons-percentage~ during startup to reduce garbage collection
150 frequency. Clearing the ~file-name-handler-alist~ seems to help reduce
151 startup time as well.
152
153 #+begin_src emacs-lisp
154 (defvar ab--gc-cons-threshold gc-cons-threshold)
155 (defvar ab--gc-cons-percentage gc-cons-percentage)
156 (defvar ab--file-name-handler-alist file-name-handler-alist)
157 (setq gc-cons-threshold (* 400 1024 1024) ; 400 MiB
158 gc-cons-percentage 0.6
159 file-name-handler-alist nil
160 ;; sidesteps a bug when profiling with esup
161 esup-child-profile-require-level 0)
162 #+end_src
163
164 Of course, we'd like to set them back to their defaults once we're
165 done initializing.
166
167 #+begin_src emacs-lisp
168 (add-hook
169 'after-init-hook
170 (lambda ()
171 (let ((elapsed (float-time (time-subtract (current-time)
172 ab--before-user-init-time))))
173 (message "Loading %s...done (%.3fs) [after-init]"
174 user-init-file elapsed))
175 (setq gc-cons-threshold ab--gc-cons-threshold
176 gc-cons-percentage ab--gc-cons-percentage
177 file-name-handler-alist ab--file-name-handler-alist)))
178 #+end_src
179
180 Increase the number of lines kept in message logs (the =*Messages*=
181 buffer).
182
183 #+begin_src emacs-lisp
184 (setq message-log-max 20000)
185 #+end_src
186
187 Optionally, we could suppress some byte compiler warnings like below,
188 but for now I've decided to keep them enabled. See documentation for
189 ~byte-compile-warnings~ for more details.
190
191 #+begin_src emacs-lisp
192 ;; (setq byte-compile-warnings
193 ;; '(not free-vars unresolved noruntime lexical make-local))
194 #+end_src
195
196 ** Package management
197
198 *** No =package.el=
199
200 I can do all my package management things with Borg, and don't need
201 Emacs' built-in =package.el=. Emacs 27 lets us disable =package.el= in
202 the =early-init-file= (see [[https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=24acb31c04b4048b85311d794e600ecd7ce60d3b][here]]).
203
204 #+begin_src emacs-lisp :tangle early-init.el
205 (setq package-enable-at-startup nil)
206 #+end_src
207
208 But since Emacs 27 isn't out yet (Emacs 26 is just around the corner
209 right now), and even when released it'll be long before most distros
210 ship in their repos, I'll still put the old workaround with the
211 commented call to ~package-initialize~ here anyway.
212
213 #+begin_src emacs-lisp
214 (setq package-enable-at-startup nil)
215 ;; (package-initialize)
216 #+end_src
217
218 *** Borg
219
220 #+begin_quote
221 Assimilate Emacs packages as Git submodules
222 #+end_quote
223
224 [[https://github.com/emacscollective/borg][Borg]] is at the heart of package management of my Emacs setup. In
225 short, it creates a git submodule in =lib/= for each package, which
226 can then be managed with the help of Magit or other tools.
227
228 #+begin_src emacs-lisp
229 (setq user-init-file (or load-file-name buffer-file-name)
230 user-emacs-directory (file-name-directory user-init-file))
231 (add-to-list 'load-path
232 (expand-file-name "lib/borg" user-emacs-directory))
233 (require 'borg)
234 (borg-initialize)
235 #+end_src
236
237 *** =use-package=
238
239 #+begin_quote
240 A use-package declaration for simplifying your .emacs
241 #+end_quote
242
243 [[https://github.com/jwiegley/use-package][use-package]] is an awesome utility for managing and configuring
244 packages (in our case especially the latter) in a neatly organized way
245 and without compromising on performance.
246
247 #+begin_src emacs-lisp
248 (require 'use-package)
249 (if nil ; set to t when need to debug init
250 (setq use-package-verbose t
251 use-package-expand-minimally nil
252 use-package-compute-statistics t
253 debug-on-error t)
254 (setq use-package-verbose nil
255 use-package-expand-minimally t))
256 #+end_src
257
258 *** Epkg
259
260 #+begin_quote
261 Browse the Emacsmirror package database
262 #+end_quote
263
264 Epkg provides access to a local copy of the [[https://emacsmirror.net][Emacsmirror]] package
265 database, low-level functions for querying the database, and a
266 =package.el=-like user interface for browsing the available packages.
267
268 #+begin_src emacs-lisp
269 (use-package epkg
270 :defer t)
271 #+end_src
272
273 ** No littering in =~/.emacs.d=
274
275 #+begin_quote
276 Help keeping ~/.emacs.d clean
277 #+end_quote
278
279 By default, even for Emacs' built-in packages, the configuration files
280 and persistent data are all over the place. Use =no-littering= to help
281 contain the mess.
282
283 #+begin_src emacs-lisp
284 (use-package no-littering
285 :demand t
286 :config
287 (savehist-mode 1)
288 (add-to-list 'savehist-additional-variables 'kill-ring)
289 (save-place-mode 1)
290 (setq auto-save-file-name-transforms
291 `((".*" ,(no-littering-expand-var-file-name "auto-save/") t))))
292 #+end_src
293
294 ** Custom file (=custom.el=)
295
296 I'm not planning on using the custom file much, but even so, I
297 definitely don't want it mixing with =init.el=. So, here; let's give
298 it it's own file. While at it, treat themes as safe.
299
300 #+begin_src emacs-lisp
301 (use-package custom
302 :no-require t
303 :config
304 (setq custom-file (no-littering-expand-etc-file-name "custom.el"))
305 (when (file-exists-p custom-file)
306 (load custom-file))
307 (setf custom-safe-themes t))
308 #+end_src
309
310 ** Better =$PATH= handling
311
312 Let's use [[https://github.com/purcell/exec-path-from-shell][exec-path-from-shell]] to make Emacs use the =$PATH= as set up
313 in my shell.
314
315 #+begin_src emacs-lisp
316 (use-package exec-path-from-shell
317 :defer 1
318 :init
319 (setq exec-path-from-shell-check-startup-files nil)
320 :config
321 (exec-path-from-shell-initialize)
322 ;; while we're at it, let's fix access to our running ssh-agent
323 (exec-path-from-shell-copy-env "SSH_AGENT_PID")
324 (exec-path-from-shell-copy-env "SSH_AUTH_SOCK"))
325 #+end_src
326
327 ** Server
328
329 Start server if not already running. Alternatively, can be done by
330 issuing =emacs --daemon= in the terminal, which can be automated with
331 a systemd service or using =brew services start emacs= on macOS. I use
332 Emacs as my window manager (via EXWM), so I always start Emacs on
333 login; so starting the server from inside Emacs is good enough for me.
334
335 See [[https://www.gnu.org/software/emacs/manual/html_node/emacs/Emacs-Server.html#Emacs-Server][Using Emacs as a Server]].
336
337 #+begin_src emacs-lisp
338 (use-package server
339 :config (or (server-running-p) (server-mode)))
340 #+end_src
341
342 * Core
343 :PROPERTIES:
344 :CUSTOM_ID: core
345 :END:
346
347 ** Defaults
348
349 *** Smaller fringe
350
351 Might want to set the fringe to a smaller value, especially if using
352 EXWM. I'm fine with the default for now.
353
354 #+begin_src emacs-lisp
355 ;; (fringe-mode '(3 . 1))
356 (fringe-mode nil)
357 #+end_src
358
359 *** Disable disabled commands
360
361 Emacs disables some commands by default that could persumably be
362 confusing for novice users. Let's disable that.
363
364 #+begin_src emacs-lisp
365 (setq disabled-command-function nil)
366 #+end_src
367
368 *** Kill-ring
369
370 Save what I copy into clipboard from other applications into Emacs'
371 kill-ring, which would allow me to still be able to easily access it
372 in case I kill (cut or copy) something else inside Emacs before
373 yanking (pasting) what I'd originally intended to.
374
375 #+begin_src emacs-lisp
376 (setq save-interprogram-paste-before-kill t)
377 #+end_src
378
379 *** Minibuffer
380
381 #+begin_src emacs-lisp
382 (setq enable-recursive-minibuffers t
383 resize-mini-windows t)
384 #+end_src
385
386 *** Lazy-person-friendly yes/no prompts
387
388 Lazy people would prefer to type fewer keystrokes, especially for yes
389 or no questions. I'm lazy.
390
391 #+begin_src emacs-lisp
392 (defalias 'yes-or-no-p #'y-or-n-p)
393 #+end_src
394
395 *** Startup screen and =*scratch*=
396
397 Firstly, let Emacs know that I'd like to have =*scratch*= as my
398 startup buffer.
399
400 #+begin_src emacs-lisp
401 (setq initial-buffer-choice t)
402 #+end_src
403
404 Now let's customize the =*scratch*= buffer a bit. First off, I don't
405 need the default hint.
406
407 #+begin_src emacs-lisp
408 (setq initial-scratch-message nil)
409 #+end_src
410
411 Also, let's use Text mode as the major mode, in case I want to
412 customize it (=*scratch*='s default major mode, Fundamental mode,
413 can't really be customized).
414
415 #+begin_src emacs-lisp
416 (setq initial-major-mode 'text-mode)
417 #+end_src
418
419 Inhibit the buffer list when more than 2 files are loaded.
420
421 #+begin_src emacs-lisp
422 (setq inhibit-startup-buffer-menu t)
423 #+end_src
424
425 I don't really need to see the startup screen or echo area message
426 either.
427
428 #+begin_src emacs-lisp
429 (advice-add #'display-startup-echo-area-message :override #'ignore)
430 (setq inhibit-startup-screen t
431 inhibit-startup-echo-area-message user-login-name)
432 #+end_src
433
434 *** More useful frame titles
435
436 Show either the file name or the buffer name (in case the buffer isn't
437 visiting a file). Borrowed from Emacs Prelude.
438
439 #+begin_src emacs-lisp
440 (setq frame-title-format
441 '("" invocation-name " - "
442 (:eval (if (buffer-file-name)
443 (abbreviate-file-name (buffer-file-name))
444 "%b"))))
445 #+end_src
446
447 *** Backups
448
449 Emacs' default backup settings aren't that great. Let's use more
450 sensible options. See documentation for the ~make-backup-file~
451 variable.
452
453 #+begin_src emacs-lisp
454 (setq backup-by-copying t
455 version-control t)
456 #+end_src
457
458 ** Packages
459
460 The packages in this section are absolutely essential to my everyday
461 workflow, and they play key roles in how I do my computing. They
462 immensely enhance the Emacs experience for me; both using Emacs, and
463 customizing it.
464
465 *** [[https://github.com/emacscollective/auto-compile][auto-compile]]
466
467 #+begin_src emacs-lisp
468 (use-package auto-compile
469 :demand t
470 :config
471 (auto-compile-on-load-mode)
472 (auto-compile-on-save-mode)
473 (setq auto-compile-display-buffer nil
474 auto-compile-mode-line-counter t
475 auto-compile-source-recreate-deletes-dest t
476 auto-compile-toggle-deletes-nonlib-dest t
477 auto-compile-update-autoloads t)
478 (add-hook 'auto-compile-inhibit-compile-hook
479 'auto-compile-inhibit-compile-detached-git-head))
480 #+end_src
481
482 *** TODO [[https://github.com/Kungsgeten/ryo-modal][ryo-modal]]
483
484 #+begin_quote
485 Roll your own modal mode
486 #+end_quote
487
488 *** [[https://github.com/ch11ng/exwm][EXWM]] (window manager)
489
490 #+begin_src emacs-lisp
491 (use-package exwm
492 :demand t
493 :config
494 (require 'exwm-config)
495 (exwm-config-default)
496 (require 'exwm-systemtray)
497 (exwm-systemtray-enable)
498 (require 'exwm-randr)
499 (exwm-randr-enable))
500 #+end_src
501
502 *** [[https://orgmode.org/][Org mode]]
503
504 #+begin_quote
505 Org mode is for keeping notes, maintaining TODO lists, planning
506 projects, and authoring documents with a fast and effective plain-text
507 system.
508 #+end_quote
509
510 In short, my favourite way of life.
511
512 #+begin_src emacs-lisp
513 (setq org-src-tab-acts-natively t
514 org-src-preserve-indentation nil
515 org-edit-src-content-indentation 0)
516 #+end_src
517
518 *** [[https://magit.vc/][Magit]]
519
520 #+begin_quote
521 It's Magit! A Git porcelain inside Emacs.
522 #+end_quote
523
524 Not just how I do git, but /the/ way to do git.
525
526 #+begin_src emacs-lisp
527 (use-package magit
528 :defer t
529 :bind (("s-g" . magit-status)
530 ("C-x g" . magit-status)
531 ("C-x M-g" . magit-dispatch-popup))
532 :config
533 (magit-add-section-hook 'magit-status-sections-hook
534 'magit-insert-modules
535 'magit-insert-stashes
536 'append))
537 #+end_src
538
539 *** [[https://github.com/abo-abo/swiper][Ivy]] (and friends)
540
541 #+begin_quote
542 Ivy - a generic completion frontend for Emacs, Swiper - isearch with
543 an overview, and more. Oh, man!
544 #+end_quote
545
546 There's no way I could top that, so I won't attempt to.
547
548 **** Ivy
549
550 #+begin_src emacs-lisp
551 (use-package ivy
552 :bind
553 (:map ivy-minibuffer-map
554 ([escape] . keyboard-escape-quit)
555 ("C-j" . ivy-next-line)
556 ("C-k" . ivy-previous-line)
557 ([S-up] . ivy-previous-history-element)
558 ([S-down] . ivy-next-history-element)
559 ("DEL" . ivy-backward-delete-char))
560 :config
561 (ivy-mode 1))
562 #+end_src
563
564 **** Swiper
565
566 #+begin_src emacs-lisp
567 (use-package swiper
568 :bind (([remap isearch-forward] . swiper)
569 ([remap isearch-backward] . swiper)))
570 #+end_src
571
572 **** Counsel
573
574 #+begin_src emacs-lisp
575 (use-package counsel
576 :defer 1.5
577 :bind (([remap execute-extended-command] . counsel-M-x)
578 ([remap find-file] . counsel-find-file)
579 ("s-r" . counsel-recentf)
580 :map minibuffer-local-map
581 ("C-r" . counsel-minibuffer-history))
582 :config
583 (counsel-mode 1)
584 (defalias 'locate #'counsel-locate))
585 #+end_src
586
587 * Borg's =layer/essentials=
588
589 TODO: break this giant source block down into individual org sections.
590
591 #+begin_src emacs-lisp
592 (use-package dash
593 :config (dash-enable-font-lock))
594
595 (use-package diff-hl
596 :config
597 (setq diff-hl-draw-borders nil)
598 (global-diff-hl-mode)
599 (add-hook 'magit-post-refresh-hook 'diff-hl-magit-post-refresh t))
600
601 (use-package dired
602 :defer t
603 :config (setq dired-listing-switches "-alh"))
604
605 (use-package eldoc
606 :when (version< "25" emacs-version)
607 :config (global-eldoc-mode))
608
609 (use-package help
610 :defer t
611 :config (temp-buffer-resize-mode))
612
613 (progn ; `isearch'
614 (setq isearch-allow-scroll t))
615
616 (use-package lisp-mode
617 :config
618 (add-hook 'emacs-lisp-mode-hook 'outline-minor-mode)
619 (add-hook 'emacs-lisp-mode-hook 'reveal-mode)
620 (defun indent-spaces-mode ()
621 (setq indent-tabs-mode nil))
622 (add-hook 'lisp-interaction-mode-hook #'indent-spaces-mode))
623
624 (use-package man
625 :defer t
626 :config (setq Man-width 80))
627
628 (use-package paren
629 :config (show-paren-mode))
630
631 (use-package prog-mode
632 :config (global-prettify-symbols-mode)
633 (defun indicate-buffer-boundaries-left ()
634 (setq indicate-buffer-boundaries 'left))
635 (add-hook 'prog-mode-hook #'indicate-buffer-boundaries-left))
636
637 (use-package recentf
638 :demand t
639 :config (add-to-list 'recentf-exclude "^/\\(?:ssh\\|su\\|sudo\\)?:"))
640
641 (use-package savehist
642 :config (savehist-mode))
643
644 (use-package saveplace
645 :when (version< "25" emacs-version)
646 :config (save-place-mode))
647
648 (use-package simple
649 :config (column-number-mode))
650
651 (progn ; `text-mode'
652 (add-hook 'text-mode-hook #'indicate-buffer-boundaries-left))
653
654 (use-package tramp
655 :defer t
656 :config
657 (add-to-list 'tramp-default-proxies-alist '(nil "\\`root\\'" "/ssh:%h:"))
658 (add-to-list 'tramp-default-proxies-alist '("localhost" nil nil))
659 (add-to-list 'tramp-default-proxies-alist
660 (list (regexp-quote (system-name)) nil nil)))
661
662 (use-package undo-tree
663 :config
664 (global-undo-tree-mode)
665 (setq undo-tree-mode-lighter ""))
666 #+end_src
667
668 * Post initialization
669 :PROPERTIES:
670 :CUSTOM_ID: post-initialization
671 :END:
672
673 Display how long it took to load the init file.
674
675 #+begin_src emacs-lisp
676 (message "Loading %s...done (%.3fs)" user-init-file
677 (float-time (time-subtract (current-time)
678 ab--before-user-init-time)))
679 #+end_src
680
681 * Footer
682 :PROPERTIES:
683 :CUSTOM_ID: footer
684 :END:
685
686 #+begin_src emacs-lisp :comments none
687 ;;; init.el ends here
688 #+end_src