[make] add ta and ti Makefile rules for more convenient tangle/build
[~bandali/configs] / emacs / init.org
CommitLineData
35ea1ba4
AB
1#+title: =aminb='s Emacs Init file
2#+property: header-args :results silent :comments link :tangle ~/dotfiles/emacs/init.el
3
4* Intro
5
421132e9
AB
6This org file is tangled to [[./init.el][init.el]] and constitutes my Emacs
7configuration. =straight.el=, =use-package=, =general.el=, =exwm=,
8=org-mode=, and =magit= are some of the awesome packages powering my
9Emacs setup.
35ea1ba4 10
abdc6c07
AB
11* Contents :toc_1:noexport:
12
13- [[#intro][Intro]]
14- [[#header][Header]]
15- [[#initial-setup][Initial setup]]
056eba24 16- [[#core][Core]]
abdc6c07 17- [[#footer][Footer]]
35ea1ba4
AB
18
19* Header
279a98e2
AB
20:PROPERTIES:
21:CUSTOM_ID: header
22:END:
35ea1ba4
AB
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
30Enable =view-mode=, which both makes the file read-only (as a reminder
31that =init.el= is an auto-generated file, not supposed to be edited),
32and provides some convenient key bindings for browsing through the
33file.
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
6089982a 56#+begin_src emacs-lisp :comments none
35ea1ba4
AB
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
e4cd0d49
AB
65** Naming conventions
66
67The 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
1446ab91 80;; ...! a macro
e4cd0d49
AB
81#+end_src
82
fd134a2b 83* Initial setup
279a98e2
AB
84:PROPERTIES:
85:CUSTOM_ID: initial-setup
86:END:
35ea1ba4 87
6089982a 88#+begin_src emacs-lisp :comments none
35ea1ba4
AB
89;;; Code:
90#+end_src
91
6089982a
AB
92** Startup time
93
94Measure and display startup time. Also, temporarily increase
95~gc-cons-threshhold~ during startup to reduce reduce garbage
96collection frequency. Taken from [[https://github.com/dieggsy/dotfiles/tree/3d95bc08033920e077855caf545a975eba52d28d/emacs.d#startup-time][here]].
97
98#+begin_src emacs-lisp
63a849c3
AB
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)
6089982a
AB
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
110Reset the variables back to default after init.
111
112#+begin_src emacs-lisp
113(add-hook
114 'after-init-hook
115 `(lambda ()
63a849c3
AB
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)
6089982a 119 (let ((elapsed (float-time (time-subtract (current-time)
63a849c3 120 ab--emacs-start-time))))
6089982a
AB
121 (message "Loading %s...done (%.3fs) [after-init]"
122 ,load-file-name elapsed))))
123#+end_src
35ea1ba4 124
fd134a2b
AB
125** Package management
126
204986be 127*** =straight.el=
fd134a2b
AB
128
129#+begin_quote
130Next-generation, purely functional package manager for the Emacs
131hacker.
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
1446ab91 154(defun ab/reload-init ()
fd134a2b
AB
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
1446ab91 163(defun ab/eval-buffer ()
fd134a2b
AB
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
63a849c3 179A use-package declaration for simplifying your .emacs
fd134a2b
AB
180#+end_quote
181
182=use-package= is an awesome utility for managing and configuring
183packages in a neatly organized way and without compromising on
184performance. 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
63a849c3 195Help keeping ~/.emacs.d clean
fd134a2b
AB
196#+end_quote
197
198By default, even for Emacs' built-in packages, the configuration files
199and persistent data are all over the place. Use =no-littering= to help
200contain 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
bab98ee5
AB
213** Custom file (=custom.el=)
214
215I'm not planning on using the custom file much, but even so, I
216definitely don't want it mixing with =init.el=. So, here, let's give
217it it's own file.
218
219#+begin_src emacs-lisp
fd134a2b
AB
220(setq custom-file (no-littering-expand-etc-file-name "custom.el"))
221(when (file-exists-p custom-file)
222 (load custom-file))
bab98ee5
AB
223#+end_src
224
1446ab91
AB
225** Better =$PATH= handling
226
227Let's use [[https://github.com/purcell/exec-path-from-shell][exec-path-from-shell]] to make Emacs use the =$PATH= as set up
228in 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
244Start server if not already running. Alternatively, can be done by
245issuing =emacs --daemon= in the terminal, which can be automated with
246a systemd service or using =brew services start emacs= on macOS. I use
247Emacs as my window manager (via =exwm=), so I always start Emacs on
248login; so starting the server from inside Emacs is good enough for me.
249
250See [[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
1446ab91
AB
258* Core
259:PROPERTIES:
260:CUSTOM_ID: core
261:END:
262
263** Defaults
264
265*** Disable disabled commands
266
267Emacs disables some commands by default that could persumably be
268confusing 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
276Save what I copy into clipboard from other applications into Emacs'
277kill-ring, which would allow me to still be able to easily access it
278in case I kill (cut or copy) something else inside Emacs before
279yanking (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
421132e9
AB
291*** Minibuffer
292
293#+begin_src emacs-lisp
294(setq enable-recursive-minibuffers t
295 resize-mini-windows t)
296#+end_src
297
1446ab91
AB
298*** Lazy-person-friendly yes/no prompts
299
300Lazy people would prefer to type fewer keystrokes, especially for yes
301or 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
309Let's customize the =*scratch*= buffer a bit. First off, I don't need
310the default hint.
311
312#+begin_src emacs-lisp
313(setq initial-scratch-message "")
314#+end_src
315
316Also, let's use Text mode as the major mode, in case I want to
317customize it (=*scratch*='s default major mode, Fundamental mode,
318can'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
326Show either the file name or the buffer name (in case the buffer isn't
327visiting 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
421132e9 337*** Backups
056eba24
AB
338
339Emacs' default backup settings aren't that great. Let's use more
340sensible options. See documentation for the ~make-backup-file~
341variable.
342
343#+begin_src emacs-lisp
344(setq backup-by-copying t
345 version-control t)
346#+end_src
347
421132e9
AB
348** Packages
349
350The packages in this section are absolutely essential to my everyday
351workflow, and they play key roles in how I do my computing. They
352immensely enhance the Emacs experience for me; both using Emacs, and
353customizing it.
354
355*** [[https://github.com/noctuid/general.el][general.el]]
356
357#+begin_quote
358More convenient key definitions in emacs
359#+end_quote
360
361More like /the most/ convenient key definitions in Emacs.
362
363#+begin_src emacs-lisp
364(use-package general
2648b6c4 365 :demand t)
421132e9 366#+end_src
abdc6c07 367
421132e9
AB
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)
51fb5bd8
AB
374 (exwm-config-default)
375 (require 'exwm-systemtray)
2648b6c4
AB
376 (exwm-systemtray-enable)
377 (require 'exwm-randr)
378 (exwm-randr-enable))
421132e9
AB
379#+end_src
380
381*** [[https://orgmode.org/][Org mode]]
382
383#+begin_quote
384Org mode is for keeping notes, maintaining TODO lists, planning
385projects, and authoring documents with a fast and effective plain-text
386system.
387#+end_quote
388
389In short, my favourite way of life.
bab98ee5
AB
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
421132e9
AB
397*** [[https://magit.vc/][Magit]]
398
399#+begin_quote
400It's Magit! A Git porcelain inside Emacs.
401#+end_quote
402
403Not 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
414Ivy - a generic completion frontend for Emacs, Swiper - isearch with
415an overview, and more. Oh, man!
416#+end_quote
417
418There'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
35ea1ba4 458* Footer
279a98e2
AB
459:PROPERTIES:
460:CUSTOM_ID: footer
461:END:
35ea1ba4
AB
462
463#+begin_src emacs-lisp :comments none
35ea1ba4
AB
464;;; init.el ends here
465#+end_src