4d57bf4e9e08fe2b48edd04c6f3a091981015ce2
[~bandali/configs] / emacs / init.org
1 #+title: =aminb='s Emacs Init file
2 #+property: header-args :results silent :comments link :tangle ~/dotfiles/emacs/init.el
3
4 * Intro
5
6 This org file is tangled to [[./init.el][init.el]] and constitutes my Emacs
7 configuration. =straight.el=, =use-package=, =general.el=, =exwm=,
8 =org-mode=, and =magit= are some of the awesome packages powering my
9 Emacs setup.
10
11 * Contents :toc_1:noexport:
12
13 - [[#intro][Intro]]
14 - [[#header][Header]]
15 - [[#initial-setup][Initial setup]]
16 - [[#core][Core]]
17 - [[#footer][Footer]]
18
19 * Header
20 :PROPERTIES:
21 :CUSTOM_ID: header
22 :END:
23
24 ** First line
25
26 #+begin_src emacs-lisp :comments none
27 ;;; init.el --- Amin Bandali's Emacs config -*- lexical-binding: t ; eval: (view-mode 1)-*-
28 #+end_src
29
30 Enable =view-mode=, which both makes the file read-only (as a reminder
31 that =init.el= is an auto-generated file, not supposed to be edited),
32 and provides some convenient key bindings for browsing through the
33 file.
34
35 ** License
36
37 #+begin_src emacs-lisp :comments none
38 ;; Copyright (C) 2018 Amin Bandali <amin@aminb.org>
39
40 ;; This program is free software: you can redistribute it and/or modify
41 ;; it under the terms of the GNU General Public License as published by
42 ;; the Free Software Foundation, either version 3 of the License, or
43 ;; (at your option) any later version.
44
45 ;; This program is distributed in the hope that it will be useful,
46 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
47 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48 ;; GNU General Public License for more details.
49
50 ;; You should have received a copy of the GNU General Public License
51 ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
52 #+end_src
53
54 ** Commentary
55
56 #+begin_src emacs-lisp :comments none
57 ;;; Commentary:
58
59 ;; Emacs configuration of Amin Bandali, computer scientist and functional
60 ;; programmer.
61
62 ;; THIS FILE IS AUTO-GENERATED FROM `init.org'.
63 #+end_src
64
65 ** Naming conventions
66
67 The conventions below were inspired by [[https://github.com/hlissner/doom-emacs][Doom]]'s conventions, found
68 [[https://github.com/hlissner/doom-emacs/blob/5dacbb7cb1c6ac246a9ccd15e6c4290def67757c/core/core.el#L3-L17][here]]. Naturally, I use my initials, =ab=, instead of =doom=.
69
70 #+begin_src emacs-lisp :comments none
71 ;; Naming conventions:
72 ;;
73 ;; ab-... public variables or non-interactive functions
74 ;; ab--... private anything (non-interactive), not safe for direct use
75 ;; ab/... an interactive function; safe for M-x or keybinding
76 ;; ab:... an evil operator, motion, or command
77 ;; ab|... a hook function
78 ;; ab*... an advising function
79 ;; ab@... a hydra command
80 ;; ...! a macro
81 #+end_src
82
83 * Initial setup
84 :PROPERTIES:
85 :CUSTOM_ID: initial-setup
86 :END:
87
88 #+begin_src emacs-lisp :comments none
89 ;;; Code:
90 #+end_src
91
92 ** Startup time
93
94 Measure and display startup time. Also, temporarily increase
95 ~gc-cons-threshhold~ during startup to reduce reduce garbage
96 collection frequency. Taken from [[https://github.com/dieggsy/dotfiles/tree/3d95bc08033920e077855caf545a975eba52d28d/emacs.d#startup-time][here]].
97
98 #+begin_src emacs-lisp
99 (defconst ab--emacs-start-time (current-time))
100 (defconst ab--gc-cons-threshold gc-cons-threshold)
101 (defconst ab--gc-cons-percentage gc-cons-percentage)
102 (defvar ab--file-name-handler-alist file-name-handler-alist)
103 (setq gc-cons-threshold 400000000
104 gc-cons-percentage 0.6
105 file-name-handler-alist nil
106 ;; sidesteps a bug when profiling with esup
107 esup-child-profile-require-level 0)
108 #+end_src
109
110 Reset the variables back to default after init.
111
112 #+begin_src emacs-lisp
113 (add-hook
114 'after-init-hook
115 `(lambda ()
116 (setq gc-cons-threshold ab--gc-cons-threshold
117 gc-cons-percentage ab--gc-cons-percentage
118 file-name-handler-alist ab--file-name-handler-alist)
119 (let ((elapsed (float-time (time-subtract (current-time)
120 ab--emacs-start-time))))
121 (message "Loading %s...done (%.3fs) [after-init]"
122 ,load-file-name elapsed))))
123 #+end_src
124
125 ** Package management
126
127 *** =straight.el=
128
129 #+begin_quote
130 Next-generation, purely functional package manager for the Emacs
131 hacker.
132 #+end_quote
133
134 =straight.el= allows me to have a fully reproducible Emacs setup.
135
136 **** Bootstrap
137
138 #+begin_src emacs-lisp
139 (let ((bootstrap-file (concat user-emacs-directory "straight/repos/straight.el/bootstrap.el"))
140 (bootstrap-version 3))
141 (unless (file-exists-p bootstrap-file)
142 (with-current-buffer
143 (url-retrieve-synchronously
144 "https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
145 'silent 'inhibit-cookies)
146 (goto-char (point-max))
147 (eval-print-last-sexp)))
148 (load bootstrap-file nil 'nomessage))
149 #+end_src
150
151 **** Useful helpers
152
153 #+begin_src emacs-lisp
154 (defun ab/reload-init ()
155 "Reload init.el."
156 (interactive)
157 (straight-transaction
158 (straight-mark-transaction-as-init)
159 (message "Reloading init.el...")
160 (load user-init-file nil 'nomessage)
161 (message "Reloading init.el... done.")))
162
163 (defun ab/eval-buffer ()
164 "Evaluate the current buffer as Elisp code."
165 (interactive)
166 (message "Evaluating %s..." (buffer-name))
167 (straight-transaction
168 (if (null buffer-file-name)
169 (eval-buffer)
170 (when (string= buffer-file-name user-init-file)
171 (straight-mark-transaction-as-init))
172 (load-file buffer-file-name)))
173 (message "Evaluating %s... done." (buffer-name)))
174 #+end_src
175
176 *** =use-package=
177
178 #+begin_quote
179 A use-package declaration for simplifying your .emacs
180 #+end_quote
181
182 =use-package= is an awesome utility for managing and configuring
183 packages in a neatly organized way and without compromising on
184 performance. So let's install it using =striaght.el= and have it use
185 =straight.el= for installing packages.
186
187 #+begin_src emacs-lisp
188 (straight-use-package 'use-package)
189 (setq straight-use-package-by-default t)
190 #+end_src
191
192 ** No littering in =~/.emacs.d=
193
194 #+begin_quote
195 Help keeping ~/.emacs.d clean
196 #+end_quote
197
198 By default, even for Emacs' built-in packages, the configuration files
199 and persistent data are all over the place. Use =no-littering= to help
200 contain the mess.
201
202 #+begin_src emacs-lisp
203 (use-package no-littering
204 :demand t
205 :config
206 (savehist-mode 1)
207 (add-to-list 'savehist-additional-variables 'kill-ring)
208 (save-place-mode 1)
209 (setq auto-save-file-name-transforms
210 `((".*" ,(no-littering-expand-var-file-name "auto-save/") t))))
211 #+end_src
212
213 ** Custom file (=custom.el=)
214
215 I'm not planning on using the custom file much, but even so, I
216 definitely don't want it mixing with =init.el=. So, here, let's give
217 it it's own file.
218
219 #+begin_src emacs-lisp
220 (setq custom-file (no-littering-expand-etc-file-name "custom.el"))
221 (when (file-exists-p custom-file)
222 (load custom-file))
223 #+end_src
224
225 ** Better =$PATH= handling
226
227 Let's use [[https://github.com/purcell/exec-path-from-shell][exec-path-from-shell]] to make Emacs use the =$PATH= as set up
228 in my shell.
229
230 #+begin_src emacs-lisp
231 (use-package exec-path-from-shell
232 :defer 1
233 :init
234 (setq exec-path-from-shell-check-startup-files nil)
235 :config
236 (exec-path-from-shell-initialize)
237 ;; while we're at it, let's fix access to our running ssh-agent
238 (exec-path-from-shell-copy-env "SSH_AGENT_PID")
239 (exec-path-from-shell-copy-env "SSH_AUTH_SOCK"))
240 #+end_src
241
242 ** Server
243
244 Start server if not already running. Alternatively, can be done by
245 issuing =emacs --daemon= in the terminal, which can be automated with
246 a systemd service or using =brew services start emacs= on macOS. I use
247 Emacs as my window manager (via =exwm=), so I always start Emacs on
248 login; so starting the server from inside Emacs is good enough for me.
249
250 See [[https://www.gnu.org/software/emacs/manual/html_node/emacs/Emacs-Server.html#Emacs-Server][Using Emacs as a Server]].
251
252 #+begin_src emacs-lisp
253 (require 'server)
254 (unless (server-running-p)
255 (server-start))
256 #+end_src
257
258 * Core
259 :PROPERTIES:
260 :CUSTOM_ID: core
261 :END:
262
263 ** Defaults
264
265 *** Disable disabled commands
266
267 Emacs disables some commands by default that could persumably be
268 confusing for novice users. Let's disable that.
269
270 #+begin_src emacs-lisp
271 (setq disabled-command-function nil)
272 #+end_src
273
274 *** Kill-ring
275
276 Save what I copy into clipboard from other applications into Emacs'
277 kill-ring, which would allow me to still be able to easily access it
278 in case I kill (cut or copy) something else inside Emacs before
279 yanking (pasting) what I'd originally intended to.
280
281 #+begin_src emacs-lisp
282 (setq save-interprogram-paste-before-kill t)
283 #+end_src
284
285 *** Keep more =*Messages*=
286
287 #+begin_src emacs-lisp
288 (setq message-log-max 10000)
289 #+end_src
290
291 *** Minibuffer
292
293 #+begin_src emacs-lisp
294 (setq enable-recursive-minibuffers t
295 resize-mini-windows t)
296 #+end_src
297
298 *** Lazy-person-friendly yes/no prompts
299
300 Lazy people would prefer to type fewer keystrokes, especially for yes
301 or no questions. I'm lazy.
302
303 #+begin_src emacs-lisp
304 (defalias 'yes-or-no-p #'y-or-n-p)
305 #+end_src
306
307 *** =*scratch*=
308
309 Let's customize the =*scratch*= buffer a bit. First off, I don't need
310 the default hint.
311
312 #+begin_src emacs-lisp
313 (setq initial-scratch-message "")
314 #+end_src
315
316 Also, let's use Text mode as the major mode, in case I want to
317 customize it (=*scratch*='s default major mode, Fundamental mode,
318 can't really be customized).
319
320 #+begin_src emacs-lisp
321 (setq initial-major-mode 'text-mode)
322 #+end_src
323
324 *** More useful frame titles
325
326 Show either the file name or the buffer name (in case the buffer isn't
327 visiting a file). Borrowed from Emacs Prelude.
328
329 #+begin_src emacs-lisp
330 (setq frame-title-format
331 '("" invocation-name " - "
332 (:eval (if (buffer-file-name)
333 (abbreviate-file-name (buffer-file-name))
334 "%b"))))
335 #+end_src
336
337 *** Backups
338
339 Emacs' default backup settings aren't that great. Let's use more
340 sensible options. See documentation for the ~make-backup-file~
341 variable.
342
343 #+begin_src emacs-lisp
344 (setq backup-by-copying t
345 version-control t)
346 #+end_src
347
348 ** Packages
349
350 The packages in this section are absolutely essential to my everyday
351 workflow, and they play key roles in how I do my computing. They
352 immensely enhance the Emacs experience for me; both using Emacs, and
353 customizing it.
354
355 *** [[https://github.com/noctuid/general.el][general.el]]
356
357 #+begin_quote
358 More convenient key definitions in emacs
359 #+end_quote
360
361 More like /the most/ convenient key definitions in Emacs.
362
363 #+begin_src emacs-lisp
364 (use-package general
365 :demand t)
366 #+end_src
367
368 *** [[https://github.com/ch11ng/exwm][exwm]] (window manager)
369
370 #+begin_src emacs-lisp
371 (use-package exwm
372 :config
373 (require 'exwm-config)
374 (exwm-config-default)
375 (require 'exwm-systemtray)
376 (exwm-systemtray-enable)
377 (require 'exwm-randr)
378 (exwm-randr-enable))
379 #+end_src
380
381 *** [[https://orgmode.org/][Org mode]]
382
383 #+begin_quote
384 Org mode is for keeping notes, maintaining TODO lists, planning
385 projects, and authoring documents with a fast and effective plain-text
386 system.
387 #+end_quote
388
389 In short, my favourite way of life.
390
391 #+begin_src emacs-lisp
392 (setq org-src-tab-acts-natively t
393 org-src-preserve-indentation nil
394 org-edit-src-content-indentation 0)
395 #+end_src
396
397 *** [[https://magit.vc/][Magit]]
398
399 #+begin_quote
400 It's Magit! A Git porcelain inside Emacs.
401 #+end_quote
402
403 Not just how I do git, but /the/ way to do git.
404
405 #+begin_src emacs-lisp
406 (use-package magit
407 :general
408 ("s-g" 'magit-status))
409 #+end_src
410
411 *** [[https://github.com/abo-abo/swiper][Ivy]] (and friends)
412
413 #+begin_quote
414 Ivy - a generic completion frontend for Emacs, Swiper - isearch with
415 an overview, and more. Oh, man!
416 #+end_quote
417
418 There's no way I could top that, so I won't attempt to.
419
420 **** Ivy
421
422 #+begin_src emacs-lisp
423 (use-package ivy
424 :general
425 (ivy-minibuffer-map
426 [escape] 'keyboard-escape-quit
427 "C-j" 'ivy-next-line
428 "C-k" 'ivy-previous-line
429 [S-up] 'ivy-previous-history-element
430 [S-down] 'ivy-next-history-element
431 "DEL" 'ivy-backward-delete-char)
432 :config
433 (ivy-mode 1))
434 #+end_src
435
436 **** Swiper
437
438 #+begin_src emacs-lisp
439 (use-package swiper
440 :general ("C-s" 'swiper))
441 #+end_src
442
443 **** Counsel
444
445 #+begin_src emacs-lisp
446 (use-package counsel
447 :general
448 ("M-x" 'counsel-M-x
449 "C-x C-f" 'counsel-find-file
450 "s-r" 'counsel-recentf)
451 (imap minibuffer-local-map
452 "C-r" 'counsel-minibuffer-history)
453 :config
454 (counsel-mode 1)
455 (defalias 'locate #'counsel-locate))
456 #+end_src
457
458 * Footer
459 :PROPERTIES:
460 :CUSTOM_ID: footer
461 :END:
462
463 #+begin_src emacs-lisp :comments none
464 ;;; init.el ends here
465 #+end_src