1 #+title: =aminb='s Literate Emacs Configuration
4 #+property: header-args :tangle yes
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 [[https://github.com/emacscollective/borg][Borg]].
16 I'd like to have a fully reproducible Emacs setup (part of the reason
17 why I store my configuration in this repository) but unfortunately out
18 of the box, that's not achievable with =package.el=, not currently
19 anyway. So, I've opted to use Borg. For what it's worth, I briefly
20 experimented with [[https://github.com/raxod502/straight.el][straight.el]], but found that it added about 2 seconds
21 to my init time; which is unacceptable for me: I use Emacs as my
22 window manager (via EXWM) and coming from bspwm, I'm too used to
23 having fast startup times.
25 * Contents :toc_1:noexport:
29 - [[#initial-setup][Initial setup]]
31 - [[#post-initialization][Post initialization]]
41 #+begin_src emacs-lisp :comments none
42 ;;; init.el --- Amin Bandali's Emacs config -*- lexical-binding: t ; eval: (view-mode 1)-*-
45 Enable =view-mode=, which both makes the file read-only (as a reminder
46 that =init.el= is an auto-generated file, not supposed to be edited),
47 and provides some convenient key bindings for browsing through the
52 #+begin_src emacs-lisp :comments none
53 ;; Copyright (C) 2018 Amin Bandali <amin@aminb.org>
55 ;; This program is free software: you can redistribute it and/or modify
56 ;; it under the terms of the GNU General Public License as published by
57 ;; the Free Software Foundation, either version 3 of the License, or
58 ;; (at your option) any later version.
60 ;; This program is distributed in the hope that it will be useful,
61 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
62 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
63 ;; GNU General Public License for more details.
65 ;; You should have received a copy of the GNU General Public License
66 ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
71 #+begin_src emacs-lisp :comments none
74 ;; Emacs configuration of Amin Bandali, computer scientist and functional
77 ;; THIS FILE IS AUTO-GENERATED FROM `init.org'.
82 The conventions below were inspired by [[https://github.com/hlissner/doom-emacs][Doom]]'s conventions, found
83 [[https://github.com/hlissner/doom-emacs/blob/5dacbb7cb1c6ac246a9ccd15e6c4290def67757c/core/core.el#L3-L17][here]]. Naturally, I use my initials, =ab=, instead of =doom=.
85 #+begin_src emacs-lisp :comments none
86 ;; Naming conventions:
88 ;; ab-... public variables or non-interactive functions
89 ;; ab--... private anything (non-interactive), not safe for direct use
90 ;; ab/... an interactive function; safe for M-x or keybinding
91 ;; ab:... an evil operator, motion, or command
92 ;; ab|... a hook function
93 ;; ab*... an advising function
94 ;; ab@... a hydra command
100 :CUSTOM_ID: initial-setup
103 #+begin_src emacs-lisp :comments none
107 ** Emacs initialization
109 I'd like to do a couple of measurements of Emacs' startup time. First,
110 let's see how long Emacs takes to start up, before even loading
111 =init.el=, i.e. =user-init-file=:
113 #+begin_src emacs-lisp
114 (defvar ab--before-user-init-time (current-time)
115 "Value of `current-time' when Emacs begins loading `user-init-file'.")
116 (message "Loading Emacs...done (%.3fs)"
117 (float-time (time-subtract ab--before-user-init-time
121 Also, temporarily increase ~gc-cons-threshhold~ and
122 ~gc-cons-percentage~ during startup to reduce garbage collection
123 frequency. Clearing the ~file-name-handler-alist~ seems to help reduce
124 startup time as well.
126 #+begin_src emacs-lisp
127 (defvar ab--gc-cons-threshold gc-cons-threshold)
128 (defvar ab--gc-cons-percentage gc-cons-percentage)
129 (defvar ab--file-name-handler-alist file-name-handler-alist)
130 (setq gc-cons-threshold (* 400 1024 1024) ; 400 MiB
131 gc-cons-percentage 0.6
132 file-name-handler-alist nil
133 ;; sidesteps a bug when profiling with esup
134 esup-child-profile-require-level 0)
137 Of course, we'd like to set them back to their defaults once we're
140 #+begin_src emacs-lisp
144 (let ((elapsed (float-time (time-subtract (current-time)
145 ab--before-user-init-time))))
146 (message "Loading %s...done (%.3fs) [after-init]"
147 user-init-file elapsed))
148 (setq gc-cons-threshold ab--gc-cons-threshold
149 gc-cons-percentage ab--gc-cons-percentage
150 file-name-handler-alist ab--file-name-handler-alist)))
153 Increase the number of lines kept in message logs (the =*Messages*=
156 #+begin_src emacs-lisp
157 (setq message-log-max 20000)
160 Optionally, we could suppress some byte compiler warnings like below,
161 but for now I've decided to keep them enabled. See documentation for
162 ~byte-compile-warnings~ for more details.
164 #+begin_src emacs-lisp
165 ;; (setq byte-compile-warnings
166 ;; '(not free-vars unresolved noruntime lexical make-local))
169 ** Package management
173 I can do all my package management things with Borg, and don't need
174 Emacs' built-in =package.el=. Emacs 27 lets us disable =package.el= in
175 the =early-init-file= (see [[https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=24acb31c04b4048b85311d794e600ecd7ce60d3b][here]]).
177 #+begin_src emacs-lisp :tangle early-init.el
178 (setq package-enable-at-startup nil)
181 But since Emacs 27 isn't out yet (Emacs 26 is just around the corner
182 right now), and even when released it'll be long before most distros
183 ship in their repos, I'll still put the old workaround with the
184 commented call to ~package-initialize~ here anyway.
186 #+begin_src emacs-lisp
187 (setq package-enable-at-startup nil)
188 ;; (package-initialize)
194 Assimilate Emacs packages as Git submodules
197 [[https://github.com/emacscollective/borg][Borg]] is at the heart of package management of my Emacs setup. In
198 short, it creates a git submodule in =lib/= for each package, which
199 can then be managed with the help of Magit or other tools.
201 #+begin_src emacs-lisp
202 (setq user-init-file (or load-file-name buffer-file-name)
203 user-emacs-directory (file-name-directory user-init-file))
204 (add-to-list 'load-path
205 (expand-file-name "lib/borg" user-emacs-directory))
213 A use-package declaration for simplifying your .emacs
216 [[https://github.com/jwiegley/use-package][use-package]] is an awesome utility for managing and configuring
217 packages (in our case especially the latter) in a neatly organized way
218 and without compromising on performance.
220 #+begin_src emacs-lisp
221 (require 'use-package)
222 (if nil ; set to t when need to debug init
223 (setq use-package-verbose t
224 use-package-expand-minimally nil
225 use-package-compute-statistics t
227 (setq use-package-verbose nil
228 use-package-expand-minimally t))
234 Browse the Emacsmirror package database
237 Epkg provides access to a local copy of the [[https://emacsmirror.net][Emacsmirror]] package
238 database, low-level functions for querying the database, and a
239 =package.el=-like user interface for browsing the available packages.
241 #+begin_src emacs-lisp
246 ** No littering in =~/.emacs.d=
249 Help keeping ~/.emacs.d clean
252 By default, even for Emacs' built-in packages, the configuration files
253 and persistent data are all over the place. Use =no-littering= to help
256 #+begin_src emacs-lisp
257 (use-package no-littering
261 (add-to-list 'savehist-additional-variables 'kill-ring)
263 (setq auto-save-file-name-transforms
264 `((".*" ,(no-littering-expand-var-file-name "auto-save/") t))))
267 ** Custom file (=custom.el=)
269 I'm not planning on using the custom file much, but even so, I
270 definitely don't want it mixing with =init.el=. So, here; let's give
271 it it's own file. While at it, treat themes as safe.
273 #+begin_src emacs-lisp
277 (setq custom-file (no-littering-expand-etc-file-name "custom.el"))
278 (when (file-exists-p custom-file)
280 (setf custom-safe-themes t))
283 ** Better =$PATH= handling
285 Let's use [[https://github.com/purcell/exec-path-from-shell][exec-path-from-shell]] to make Emacs use the =$PATH= as set up
288 #+begin_src emacs-lisp
289 (use-package exec-path-from-shell
292 (setq exec-path-from-shell-check-startup-files nil)
294 (exec-path-from-shell-initialize)
295 ;; while we're at it, let's fix access to our running ssh-agent
296 (exec-path-from-shell-copy-env "SSH_AGENT_PID")
297 (exec-path-from-shell-copy-env "SSH_AUTH_SOCK"))
302 Start server if not already running. Alternatively, can be done by
303 issuing =emacs --daemon= in the terminal, which can be automated with
304 a systemd service or using =brew services start emacs= on macOS. I use
305 Emacs as my window manager (via EXWM), so I always start Emacs on
306 login; so starting the server from inside Emacs is good enough for me.
308 See [[https://www.gnu.org/software/emacs/manual/html_node/emacs/Emacs-Server.html#Emacs-Server][Using Emacs as a Server]].
310 #+begin_src emacs-lisp
312 :config (or (server-running-p) (server-mode)))
324 Set fringe to a small value so we don't have big borders in EXWM, but
325 can still see the =diff-hl= colors in the fringe.
327 #+begin_src emacs-lisp
328 (fringe-mode '(3 . 1))
331 *** Disable disabled commands
333 Emacs disables some commands by default that could persumably be
334 confusing for novice users. Let's disable that.
336 #+begin_src emacs-lisp
337 (setq disabled-command-function nil)
342 Save what I copy into clipboard from other applications into Emacs'
343 kill-ring, which would allow me to still be able to easily access it
344 in case I kill (cut or copy) something else inside Emacs before
345 yanking (pasting) what I'd originally intended to.
347 #+begin_src emacs-lisp
348 (setq save-interprogram-paste-before-kill t)
353 #+begin_src emacs-lisp
354 (setq enable-recursive-minibuffers t
355 resize-mini-windows t)
358 *** Lazy-person-friendly yes/no prompts
360 Lazy people would prefer to type fewer keystrokes, especially for yes
361 or no questions. I'm lazy.
363 #+begin_src emacs-lisp
364 (defalias 'yes-or-no-p #'y-or-n-p)
367 *** Startup screen and =*scratch*=
369 Firstly, let Emacs know that I'd like to have =*scratch*= as my
372 #+begin_src emacs-lisp
373 (setq initial-buffer-choice t)
376 Now let's customize the =*scratch*= buffer a bit. First off, I don't
377 need the default hint.
379 #+begin_src emacs-lisp
380 (setq initial-scratch-message nil)
383 Also, let's use Text mode as the major mode, in case I want to
384 customize it (=*scratch*='s default major mode, Fundamental mode,
385 can't really be customized).
387 #+begin_src emacs-lisp
388 (setq initial-major-mode 'text-mode)
391 Inhibit the buffer list when more than 2 files are loaded.
393 #+begin_src emacs-lisp
394 (setq inhibit-startup-buffer-menu t)
397 I don't really need to see the startup screen or echo area message
400 #+begin_src emacs-lisp
401 (advice-add #'display-startup-echo-area-message :override #'ignore)
402 (setq inhibit-startup-screen t
403 inhibit-startup-echo-area-message user-login-name)
406 *** More useful frame titles
408 Show either the file name or the buffer name (in case the buffer isn't
409 visiting a file). Borrowed from Emacs Prelude.
411 #+begin_src emacs-lisp
412 (setq frame-title-format
413 '("" invocation-name " - "
414 (:eval (if (buffer-file-name)
415 (abbreviate-file-name (buffer-file-name))
421 Emacs' default backup settings aren't that great. Let's use more
422 sensible options. See documentation for the ~make-backup-file~
425 #+begin_src emacs-lisp
426 (setq backup-by-copying t
432 The packages in this section are absolutely essential to my everyday
433 workflow, and they play key roles in how I do my computing. They
434 immensely enhance the Emacs experience for me; both using Emacs, and
437 *** [[https://github.com/emacscollective/auto-compile][auto-compile]]
439 #+begin_src emacs-lisp
440 (use-package auto-compile
443 (auto-compile-on-load-mode)
444 (auto-compile-on-save-mode)
445 (setq auto-compile-display-buffer nil
446 auto-compile-mode-line-counter t
447 auto-compile-source-recreate-deletes-dest t
448 auto-compile-toggle-deletes-nonlib-dest t
449 auto-compile-update-autoloads t)
450 (add-hook 'auto-compile-inhibit-compile-hook
451 'auto-compile-inhibit-compile-detached-git-head))
454 *** TODO [[https://github.com/Kungsgeten/ryo-modal][ryo-modal]]
457 Roll your own modal mode
460 *** [[https://github.com/ch11ng/exwm][EXWM]] (window manager)
462 #+begin_src emacs-lisp
465 ;; (require 'exwm-config)
466 ;; (exwm-config-default)
467 ;; (require 'exwm-systemtray)
468 ;; (exwm-systemtray-enable)
469 ;; (require 'exwm-randr)
470 ;; (exwm-randr-enable))
473 *** [[https://orgmode.org/][Org mode]]
476 Org mode is for keeping notes, maintaining TODO lists, planning
477 projects, and authoring documents with a fast and effective plain-text
481 In short, my favourite way of life.
483 #+begin_src emacs-lisp
484 (setq org-src-tab-acts-natively t
485 org-src-preserve-indentation nil
486 org-edit-src-content-indentation 0)
489 *** [[https://magit.vc/][Magit]]
492 It's Magit! A Git porcelain inside Emacs.
495 Not just how I do git, but /the/ way to do git.
497 #+begin_src emacs-lisp
500 :bind (("s-g" . magit-status)
501 ("C-x g" . magit-status)
502 ("C-x M-g" . magit-dispatch-popup))
504 (magit-add-section-hook 'magit-status-sections-hook
505 'magit-insert-modules
506 'magit-insert-stashes
510 *** [[https://github.com/abo-abo/swiper][Ivy]] (and friends)
513 Ivy - a generic completion frontend for Emacs, Swiper - isearch with
514 an overview, and more. Oh, man!
517 There's no way I could top that, so I won't attempt to.
521 #+begin_src emacs-lisp
524 ;; (:map ivy-minibuffer-map
525 ;; ([escape] . keyboard-escape-quit)
526 ;; ("C-j" . ivy-next-line)
527 ;; ("C-k" . ivy-previous-line)
528 ;; ([S-up] . ivy-previous-history-element)
529 ;; ([S-down] . ivy-next-history-element)
530 ;; ("DEL" . ivy-backward-delete-char))
537 #+begin_src emacs-lisp
538 ;; (use-package swiper
539 ;; :bind (([remap isearch-forward] . swiper)
540 ;; ([remap isearch-backward] . swiper)))
545 #+begin_src emacs-lisp
546 ;; (use-package counsel
547 ;; :bind (([remap execute-extended-command] . counsel-M-x)
548 ;; ([remap find-file] . counsel-find-file)
549 ;; ("s-r" . counsel-recentf)
550 ;; :map minibuffer-local-map
551 ;; ("C-r" . counsel-minibuffer-history))
554 ;; (defalias 'locate #'counsel-locate))
557 * Borg's =layer/essentials=
559 TODO: break this giant source block down into individual org sections.
561 #+begin_src emacs-lisp
563 :config (dash-enable-font-lock))
567 (setq diff-hl-draw-borders nil)
568 (global-diff-hl-mode)
569 (add-hook 'magit-post-refresh-hook 'diff-hl-magit-post-refresh t))
573 :config (setq dired-listing-switches "-alh"))
576 :when (version< "25" emacs-version)
577 :config (global-eldoc-mode))
581 :config (temp-buffer-resize-mode))
584 (setq isearch-allow-scroll t))
586 (use-package lisp-mode
588 (add-hook 'emacs-lisp-mode-hook 'outline-minor-mode)
589 (add-hook 'emacs-lisp-mode-hook 'reveal-mode)
590 (defun indent-spaces-mode ()
591 (setq indent-tabs-mode nil))
592 (add-hook 'lisp-interaction-mode-hook #'indent-spaces-mode))
596 :config (setq Man-width 80))
599 :config (show-paren-mode))
601 (use-package prog-mode
602 :config (global-prettify-symbols-mode)
603 (defun indicate-buffer-boundaries-left ()
604 (setq indicate-buffer-boundaries 'left))
605 (add-hook 'prog-mode-hook #'indicate-buffer-boundaries-left))
609 :config (add-to-list 'recentf-exclude "^/\\(?:ssh\\|su\\|sudo\\)?:"))
611 (use-package savehist
612 :config (savehist-mode))
614 (use-package saveplace
615 :when (version< "25" emacs-version)
616 :config (save-place-mode))
619 :config (column-number-mode))
622 (add-hook 'text-mode-hook #'indicate-buffer-boundaries-left))
627 (add-to-list 'tramp-default-proxies-alist '(nil "\\`root\\'" "/ssh:%h:"))
628 (add-to-list 'tramp-default-proxies-alist '("localhost" nil nil))
629 (add-to-list 'tramp-default-proxies-alist
630 (list (regexp-quote (system-name)) nil nil)))
632 (use-package undo-tree
634 (global-undo-tree-mode)
635 (setq undo-tree-mode-lighter ""))
638 * Post initialization
640 :CUSTOM_ID: post-initialization
643 Display how long it took to load the init file.
645 #+begin_src emacs-lisp
646 (message "Loading %s...done (%.3fs)" user-init-file
647 (float-time (time-subtract (current-time)
648 ab--before-user-init-time)))
656 #+begin_src emacs-lisp :comments none
657 ;;; init.el ends here