[emacs] updates to initial setup and core defaults
[~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
6TODO: description
7
abdc6c07
AB
8* Contents :toc_1:noexport:
9
10- [[#intro][Intro]]
11- [[#header][Header]]
12- [[#initial-setup][Initial setup]]
13- [[#config][Config]]
14- [[#footer][Footer]]
35ea1ba4
AB
15
16* Header
279a98e2
AB
17:PROPERTIES:
18:CUSTOM_ID: header
19:END:
35ea1ba4
AB
20
21** First line
22
23#+begin_src emacs-lisp :comments none
24;;; init.el --- Amin Bandali's Emacs config -*- lexical-binding: t ; eval: (view-mode 1)-*-
25#+end_src
26
27Enable =view-mode=, which both makes the file read-only (as a reminder
28that =init.el= is an auto-generated file, not supposed to be edited),
29and provides some convenient key bindings for browsing through the
30file.
31
32** License
33
34#+begin_src emacs-lisp :comments none
35;; Copyright (C) 2018 Amin Bandali <amin@aminb.org>
36
37;; This program is free software: you can redistribute it and/or modify
38;; it under the terms of the GNU General Public License as published by
39;; the Free Software Foundation, either version 3 of the License, or
40;; (at your option) any later version.
41
42;; This program is distributed in the hope that it will be useful,
43;; but WITHOUT ANY WARRANTY; without even the implied warranty of
44;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45;; GNU General Public License for more details.
46
47;; You should have received a copy of the GNU General Public License
48;; along with this program. If not, see <https://www.gnu.org/licenses/>.
49#+end_src
50
51** Commentary
52
6089982a 53#+begin_src emacs-lisp :comments none
35ea1ba4
AB
54;;; Commentary:
55
56;; Emacs configuration of Amin Bandali, computer scientist and functional
57;; programmer.
58
59;; THIS FILE IS AUTO-GENERATED FROM `init.org'.
60#+end_src
61
e4cd0d49
AB
62** Naming conventions
63
64The conventions below were inspired by [[https://github.com/hlissner/doom-emacs][Doom]]'s conventions, found
65[[https://github.com/hlissner/doom-emacs/blob/5dacbb7cb1c6ac246a9ccd15e6c4290def67757c/core/core.el#L3-L17][here]]. Naturally, I use my initials, =ab=, instead of =doom=.
66
67#+begin_src emacs-lisp :comments none
68;; Naming conventions:
69;;
70;; ab-... public variables or non-interactive functions
71;; ab--... private anything (non-interactive), not safe for direct use
72;; ab/... an interactive function; safe for M-x or keybinding
73;; ab:... an evil operator, motion, or command
74;; ab|... a hook function
75;; ab*... an advising function
76;; ab@... a hydra command
1446ab91 77;; ...! a macro
e4cd0d49
AB
78#+end_src
79
fd134a2b 80* Initial setup
279a98e2
AB
81:PROPERTIES:
82:CUSTOM_ID: initial-setup
83:END:
35ea1ba4 84
6089982a 85#+begin_src emacs-lisp :comments none
35ea1ba4
AB
86;;; Code:
87#+end_src
88
6089982a
AB
89** Startup time
90
91Measure and display startup time. Also, temporarily increase
92~gc-cons-threshhold~ during startup to reduce reduce garbage
93collection frequency. Taken from [[https://github.com/dieggsy/dotfiles/tree/3d95bc08033920e077855caf545a975eba52d28d/emacs.d#startup-time][here]].
94
95#+begin_src emacs-lisp
63a849c3
AB
96(defconst ab--emacs-start-time (current-time))
97(defconst ab--gc-cons-threshold gc-cons-threshold)
98(defconst ab--gc-cons-percentage gc-cons-percentage)
99(defvar ab--file-name-handler-alist file-name-handler-alist)
6089982a
AB
100(setq gc-cons-threshold 400000000
101 gc-cons-percentage 0.6
102 file-name-handler-alist nil
103 ;; sidesteps a bug when profiling with esup
104 esup-child-profile-require-level 0)
105#+end_src
106
107Reset the variables back to default after init.
108
109#+begin_src emacs-lisp
110(add-hook
111 'after-init-hook
112 `(lambda ()
63a849c3
AB
113 (setq gc-cons-threshold ab--gc-cons-threshold
114 gc-cons-percentage ab--gc-cons-percentage
115 file-name-handler-alist ab--file-name-handler-alist)
6089982a 116 (let ((elapsed (float-time (time-subtract (current-time)
63a849c3 117 ab--emacs-start-time))))
6089982a
AB
118 (message "Loading %s...done (%.3fs) [after-init]"
119 ,load-file-name elapsed))))
120#+end_src
35ea1ba4 121
fd134a2b
AB
122** Package management
123
204986be 124*** =straight.el=
fd134a2b
AB
125
126#+begin_quote
127Next-generation, purely functional package manager for the Emacs
128hacker.
129#+end_quote
130
131=straight.el= allows me to have a fully reproducible Emacs setup.
132
133**** Bootstrap
134
135#+begin_src emacs-lisp
136(let ((bootstrap-file (concat user-emacs-directory "straight/repos/straight.el/bootstrap.el"))
137 (bootstrap-version 3))
138 (unless (file-exists-p bootstrap-file)
139 (with-current-buffer
140 (url-retrieve-synchronously
141 "https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
142 'silent 'inhibit-cookies)
143 (goto-char (point-max))
144 (eval-print-last-sexp)))
145 (load bootstrap-file nil 'nomessage))
146#+end_src
147
148**** Useful helpers
149
150#+begin_src emacs-lisp
1446ab91 151(defun ab/reload-init ()
fd134a2b
AB
152 "Reload init.el."
153 (interactive)
154 (straight-transaction
155 (straight-mark-transaction-as-init)
156 (message "Reloading init.el...")
157 (load user-init-file nil 'nomessage)
158 (message "Reloading init.el... done.")))
159
1446ab91 160(defun ab/eval-buffer ()
fd134a2b
AB
161 "Evaluate the current buffer as Elisp code."
162 (interactive)
163 (message "Evaluating %s..." (buffer-name))
164 (straight-transaction
165 (if (null buffer-file-name)
166 (eval-buffer)
167 (when (string= buffer-file-name user-init-file)
168 (straight-mark-transaction-as-init))
169 (load-file buffer-file-name)))
170 (message "Evaluating %s... done." (buffer-name)))
171#+end_src
172
173*** =use-package=
174
175#+begin_quote
63a849c3 176A use-package declaration for simplifying your .emacs
fd134a2b
AB
177#+end_quote
178
179=use-package= is an awesome utility for managing and configuring
180packages in a neatly organized way and without compromising on
181performance. So let's install it using =striaght.el= and have it use
182=straight.el= for installing packages.
183
184#+begin_src emacs-lisp
185(straight-use-package 'use-package)
186(setq straight-use-package-by-default t)
187#+end_src
188
189** No littering in =~/.emacs.d=
190
191#+begin_quote
63a849c3 192Help keeping ~/.emacs.d clean
fd134a2b
AB
193#+end_quote
194
195By default, even for Emacs' built-in packages, the configuration files
196and persistent data are all over the place. Use =no-littering= to help
197contain the mess.
198
199#+begin_src emacs-lisp
200(use-package no-littering
201 :demand t
202 :config
203 (savehist-mode 1)
204 (add-to-list 'savehist-additional-variables 'kill-ring)
205 (save-place-mode 1)
206 (setq auto-save-file-name-transforms
207 `((".*" ,(no-littering-expand-var-file-name "auto-save/") t))))
208#+end_src
209
bab98ee5
AB
210** Custom file (=custom.el=)
211
212I'm not planning on using the custom file much, but even so, I
213definitely don't want it mixing with =init.el=. So, here, let's give
214it it's own file.
215
216#+begin_src emacs-lisp
fd134a2b
AB
217(setq custom-file (no-littering-expand-etc-file-name "custom.el"))
218(when (file-exists-p custom-file)
219 (load custom-file))
bab98ee5
AB
220#+end_src
221
1446ab91
AB
222** Better =$PATH= handling
223
224Let's use [[https://github.com/purcell/exec-path-from-shell][exec-path-from-shell]] to make Emacs use the =$PATH= as set up
225in my shell.
226
227#+begin_src emacs-lisp
228(use-package exec-path-from-shell
229 :defer 1
230 :init
231 (setq exec-path-from-shell-check-startup-files nil)
232 :config
233 (exec-path-from-shell-initialize)
234 ;; while we're at it, let's fix access to our running ssh-agent
235 (exec-path-from-shell-copy-env "SSH_AGENT_PID")
236 (exec-path-from-shell-copy-env "SSH_AUTH_SOCK"))
237#+end_src
238
239** Server
240
241Start server if not already running. Alternatively, can be done by
242issuing =emacs --daemon= in the terminal, which can be automated with
243a systemd service or using =brew services start emacs= on macOS. I use
244Emacs as my window manager (via =exwm=), so I always start Emacs on
245login; so starting the server from inside Emacs is good enough for me.
246
247See [[https://www.gnu.org/software/emacs/manual/html_node/emacs/Emacs-Server.html#Emacs-Server][Using Emacs as a Server]].
248
249#+begin_src emacs-lisp
250(require 'server)
251(unless (server-running-p)
252 (server-start))
253#+end_src
254
204986be
AB
255** Backups
256
257Emacs' default backup settings aren't that great. Let's use more
258sensible options. See documentation for the ~make-backup-file~
259variable.
260
261#+begin_src emacs-lisp
262(setq backup-by-copying t
263 version-control t)
264#+end_src
265
1446ab91
AB
266* Core
267:PROPERTIES:
268:CUSTOM_ID: core
269:END:
270
271** Defaults
272
273*** Disable disabled commands
274
275Emacs disables some commands by default that could persumably be
276confusing for novice users. Let's disable that.
277
278#+begin_src emacs-lisp
279(setq disabled-command-function nil)
280#+end_src
281
282*** Kill-ring
283
284Save what I copy into clipboard from other applications into Emacs'
285kill-ring, which would allow me to still be able to easily access it
286in case I kill (cut or copy) something else inside Emacs before
287yanking (pasting) what I'd originally intended to.
288
289#+begin_src emacs-lisp
290(setq save-interprogram-paste-before-kill t)
291#+end_src
292
293*** Keep more =*Messages*=
294
295#+begin_src emacs-lisp
296(setq message-log-max 10000)
297#+end_src
298
299*** Lazy-person-friendly yes/no prompts
300
301Lazy people would prefer to type fewer keystrokes, especially for yes
302or no questions. I'm lazy.
303
304#+begin_src emacs-lisp
305(defalias 'yes-or-no-p #'y-or-n-p)
306#+end_src
307
308*** =*scratch*=
309
310Let's customize the =*scratch*= buffer a bit. First off, I don't need
311the default hint.
312
313#+begin_src emacs-lisp
314(setq initial-scratch-message "")
315#+end_src
316
317Also, let's use Text mode as the major mode, in case I want to
318customize it (=*scratch*='s default major mode, Fundamental mode,
319can't really be customized).
320
321#+begin_src emacs-lisp
322(setq initial-major-mode 'text-mode)
323#+end_src
324
325*** More useful frame titles
326
327Show either the file name or the buffer name (in case the buffer isn't
328visiting a file). Borrowed from Emacs Prelude.
329
330#+begin_src emacs-lisp
331(setq frame-title-format
332 '("" invocation-name " - "
333 (:eval (if (buffer-file-name)
334 (abbreviate-file-name (buffer-file-name))
335 "%b"))))
336#+end_src
337
abdc6c07 338* Config
279a98e2
AB
339:PROPERTIES:
340:CUSTOM_ID: config
341:END:
abdc6c07 342
bab98ee5
AB
343** Org
344
345#+begin_src emacs-lisp
346(setq org-src-tab-acts-natively t
347 org-src-preserve-indentation nil
348 org-edit-src-content-indentation 0)
349#+end_src
350
35ea1ba4 351* Footer
279a98e2
AB
352:PROPERTIES:
353:CUSTOM_ID: footer
354:END:
35ea1ba4
AB
355
356#+begin_src emacs-lisp :comments none
35ea1ba4
AB
357;;; init.el ends here
358#+end_src