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-init][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))))
268 ** Custom file (=custom.el=)
270 I'm not planning on using the custom file much, but even so, I
271 definitely don't want it mixing with =init.el=. So, here; let's give
272 it it's own file. While at it, treat themes as safe.
274 #+begin_src emacs-lisp
278 (setq custom-file (no-littering-expand-etc-file-name "custom.el"))
279 (when (file-exists-p custom-file)
281 (setf custom-safe-themes t))
284 ** Better =$PATH= handling
286 Let's use [[https://github.com/purcell/exec-path-from-shell][exec-path-from-shell]] to make Emacs use the =$PATH= as set up
289 #+begin_src emacs-lisp
290 ;; (use-package exec-path-from-shell
293 ;; (setq exec-path-from-shell-check-startup-files nil)
295 ;; (exec-path-from-shell-initialize)
296 ;; ;; while we're at it, let's fix access to our running ssh-agent
297 ;; (exec-path-from-shell-copy-env "SSH_AGENT_PID")
298 ;; (exec-path-from-shell-copy-env "SSH_AUTH_SOCK"))
303 Start server if not already running. Alternatively, can be done by
304 issuing =emacs --daemon= in the terminal, which can be automated with
305 a systemd service or using =brew services start emacs= on macOS. I use
306 Emacs as my window manager (via EXWM), so I always start Emacs on
307 login; so starting the server from inside Emacs is good enough for me.
309 See [[https://www.gnu.org/software/emacs/manual/html_node/emacs/Emacs-Server.html#Emacs-Server][Using Emacs as a Server]].
311 #+begin_src emacs-lisp
313 :config (or (server-running-p) (server-mode)))
325 Set fringe to a small value so we don't have big borders in EXWM, but
326 can still see the =diff-hl= colors in the fringe.
328 #+begin_src emacs-lisp
329 (fringe-mode '(3 . 1))
332 *** Disable disabled commands
334 Emacs disables some commands by default that could persumably be
335 confusing for novice users. Let's disable that.
337 #+begin_src emacs-lisp
338 (setq disabled-command-function nil)
343 Save what I copy into clipboard from other applications into Emacs'
344 kill-ring, which would allow me to still be able to easily access it
345 in case I kill (cut or copy) something else inside Emacs before
346 yanking (pasting) what I'd originally intended to.
348 #+begin_src emacs-lisp
349 (setq save-interprogram-paste-before-kill t)
354 #+begin_src emacs-lisp
355 (setq enable-recursive-minibuffers t
356 resize-mini-windows t)
359 *** Lazy-person-friendly yes/no prompts
361 Lazy people would prefer to type fewer keystrokes, especially for yes
362 or no questions. I'm lazy.
364 #+begin_src emacs-lisp
365 (defalias 'yes-or-no-p #'y-or-n-p)
368 *** Startup screen and =*scratch*=
370 Firstly, let Emacs know that I'd like to have =*scratch*= as my
373 #+begin_src emacs-lisp
374 (setq initial-buffer-choice t)
377 Now let's customize the =*scratch*= buffer a bit. First off, I don't
378 need the default hint.
380 #+begin_src emacs-lisp
381 (setq initial-scratch-message nil)
384 Also, let's use Text mode as the major mode, in case I want to
385 customize it (=*scratch*='s default major mode, Fundamental mode,
386 can't really be customized).
388 #+begin_src emacs-lisp
389 (setq initial-major-mode 'text-mode)
392 Inhibit the buffer list when more than 2 files are loaded.
394 #+begin_src emacs-lisp
395 (setq inhibit-startup-buffer-menu t)
398 I don't really need to see the startup screen or echo area message
401 #+begin_src emacs-lisp
402 (advice-add #'display-startup-echo-area-message :override #'ignore)
403 (setq inhibit-startup-screen t
404 inhibit-startup-echo-area-message user-login-name)
407 *** More useful frame titles
409 Show either the file name or the buffer name (in case the buffer isn't
410 visiting a file). Borrowed from Emacs Prelude.
412 #+begin_src emacs-lisp
413 (setq frame-title-format
414 '("" invocation-name " - "
415 (:eval (if (buffer-file-name)
416 (abbreviate-file-name (buffer-file-name))
422 Emacs' default backup settings aren't that great. Let's use more
423 sensible options. See documentation for the ~make-backup-file~
426 #+begin_src emacs-lisp
427 (setq backup-by-copying t
433 The packages in this section are absolutely essential to my everyday
434 workflow, and they play key roles in how I do my computing. They
435 immensely enhance the Emacs experience for me; both using Emacs, and
438 *** [[https://github.com/emacscollective/auto-compile][auto-compile]]
440 #+begin_src emacs-lisp
441 (use-package auto-compile
444 (auto-compile-on-load-mode)
445 (auto-compile-on-save-mode)
446 (setq auto-compile-display-buffer nil
447 auto-compile-mode-line-counter t
448 auto-compile-source-recreate-deletes-dest t
449 auto-compile-toggle-deletes-nonlib-dest t
450 auto-compile-update-autoloads t)
451 (add-hook 'auto-compile-inhibit-compile-hook
452 'auto-compile-inhibit-compile-detached-git-head))
455 *** TODO [[https://github.com/Kungsgeten/ryo-modal][ryo-modal]]
458 Roll your own modal mode
461 *** [[https://github.com/ch11ng/exwm][EXWM]] (window manager)
463 #+begin_src emacs-lisp
466 ;; (require 'exwm-config)
467 ;; (exwm-config-default)
468 ;; (require 'exwm-systemtray)
469 ;; (exwm-systemtray-enable)
470 ;; (require 'exwm-randr)
471 ;; (exwm-randr-enable))
474 *** [[https://orgmode.org/][Org mode]]
477 Org mode is for keeping notes, maintaining TODO lists, planning
478 projects, and authoring documents with a fast and effective plain-text
482 In short, my favourite way of life.
484 #+begin_src emacs-lisp
485 (setq org-src-tab-acts-natively t
486 org-src-preserve-indentation nil
487 org-edit-src-content-indentation 0)
490 *** [[https://magit.vc/][Magit]]
493 It's Magit! A Git porcelain inside Emacs.
496 Not just how I do git, but /the/ way to do git.
498 #+begin_src emacs-lisp
501 :bind (("s-g" . magit-status)
502 ("C-x g" . magit-status)
503 ("C-x M-g" . magit-dispatch-popup))
505 (magit-add-section-hook 'magit-status-sections-hook
506 'magit-insert-modules
507 'magit-insert-stashes
511 *** [[https://github.com/abo-abo/swiper][Ivy]] (and friends)
514 Ivy - a generic completion frontend for Emacs, Swiper - isearch with
515 an overview, and more. Oh, man!
518 There's no way I could top that, so I won't attempt to.
522 #+begin_src emacs-lisp
525 ;; (:map ivy-minibuffer-map
526 ;; ([escape] . keyboard-escape-quit)
527 ;; ("C-j" . ivy-next-line)
528 ;; ("C-k" . ivy-previous-line)
529 ;; ([S-up] . ivy-previous-history-element)
530 ;; ([S-down] . ivy-next-history-element)
531 ;; ("DEL" . ivy-backward-delete-char))
538 #+begin_src emacs-lisp
539 ;; (use-package swiper
540 ;; :bind (([remap isearch-forward] . swiper)
541 ;; ([remap isearch-backward] . swiper)))
546 #+begin_src emacs-lisp
547 ;; (use-package counsel
548 ;; :bind (([remap execute-extended-command] . counsel-M-x)
549 ;; ([remap find-file] . counsel-find-file)
550 ;; ("s-r" . counsel-recentf)
551 ;; :map minibuffer-local-map
552 ;; ("C-r" . counsel-minibuffer-history))
555 ;; (defalias 'locate #'counsel-locate))
558 * Borg's =layer/essentials=
560 TODO: break this giant source block down into individual org sections.
562 #+begin_src emacs-lisp
564 :config (dash-enable-font-lock))
568 (setq diff-hl-draw-borders nil)
569 (global-diff-hl-mode)
570 (add-hook 'magit-post-refresh-hook 'diff-hl-magit-post-refresh t))
574 :config (setq dired-listing-switches "-alh"))
577 :when (version< "25" emacs-version)
578 :config (global-eldoc-mode))
582 :config (temp-buffer-resize-mode))
585 (setq isearch-allow-scroll t))
587 (use-package lisp-mode
589 (add-hook 'emacs-lisp-mode-hook 'outline-minor-mode)
590 (add-hook 'emacs-lisp-mode-hook 'reveal-mode)
591 (defun indent-spaces-mode ()
592 (setq indent-tabs-mode nil))
593 (add-hook 'lisp-interaction-mode-hook #'indent-spaces-mode))
597 :config (setq Man-width 80))
600 :config (show-paren-mode))
602 (use-package prog-mode
603 :config (global-prettify-symbols-mode)
604 (defun indicate-buffer-boundaries-left ()
605 (setq indicate-buffer-boundaries 'left))
606 (add-hook 'prog-mode-hook #'indicate-buffer-boundaries-left))
610 :config (add-to-list 'recentf-exclude "^/\\(?:ssh\\|su\\|sudo\\)?:"))
612 (use-package savehist
613 :config (savehist-mode))
615 (use-package saveplace
616 :when (version< "25" emacs-version)
617 :config (save-place-mode))
620 :config (column-number-mode))
623 (add-hook 'text-mode-hook #'indicate-buffer-boundaries-left))
628 (add-to-list 'tramp-default-proxies-alist '(nil "\\`root\\'" "/ssh:%h:"))
629 (add-to-list 'tramp-default-proxies-alist '("localhost" nil nil))
630 (add-to-list 'tramp-default-proxies-alist
631 (list (regexp-quote (system-name)) nil nil)))
633 (use-package undo-tree
635 (global-undo-tree-mode)
636 (setq undo-tree-mode-lighter ""))
639 * Post initialization
641 :CUSTOM_ID: post-init
644 Display how long it took to load the init file.
646 #+begin_src emacs-lisp
647 (message "Loading %s...done (%.3fs)" user-init-file
648 (float-time (time-subtract (current-time)
649 ab--before-user-init-time)))
657 #+begin_src emacs-lisp :comments none
658 ;;; init.el ends here