summaryrefslogtreecommitdiffstats
path: root/emacs/init.org
blob: 388a4e24c8750d3c6c9e069d2ce4b175761831e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#+title: =aminb='s Emacs Init file
#+property: header-args :results silent :comments link :tangle ~/dotfiles/emacs/init.el

* Intro

TODO: description

* Contents                                                   :toc_1:noexport:

- [[#intro][Intro]]
- [[#header][Header]]
- [[#initial-setup][Initial setup]]
- [[#core][Core]]
- [[#config][Config]]
- [[#footer][Footer]]

* Header
:PROPERTIES:
:CUSTOM_ID: header
:END:

** First line

#+begin_src emacs-lisp :comments none
;;; init.el --- Amin Bandali's Emacs config -*- lexical-binding: t ; eval: (view-mode 1)-*-
#+end_src

Enable =view-mode=, which both makes the file read-only (as a reminder
that =init.el= is an auto-generated file, not supposed to be edited),
and provides some convenient key bindings for browsing through the
file.

** License

#+begin_src emacs-lisp :comments none
;; Copyright (C) 2018  Amin Bandali <amin@aminb.org>

;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
#+end_src

** Commentary

#+begin_src emacs-lisp :comments none
;;; Commentary:

;; Emacs configuration of Amin Bandali, computer scientist and functional
;; programmer.

;; THIS FILE IS AUTO-GENERATED FROM `init.org'.
#+end_src

** Naming conventions

The conventions below were inspired by [[https://github.com/hlissner/doom-emacs][Doom]]'s conventions, found
[[https://github.com/hlissner/doom-emacs/blob/5dacbb7cb1c6ac246a9ccd15e6c4290def67757c/core/core.el#L3-L17][here]]. Naturally, I use my initials, =ab=, instead of =doom=.

#+begin_src emacs-lisp :comments none
;; Naming conventions:
;;
;;   ab-...   public variables or non-interactive functions
;;   ab--...  private anything (non-interactive), not safe for direct use
;;   ab/...   an interactive function; safe for M-x or keybinding
;;   ab:...   an evil operator, motion, or command
;;   ab|...   a hook function
;;   ab*...   an advising function
;;   ab@...   a hydra command
;;   ...!     a macro
#+end_src

* Initial setup
:PROPERTIES:
:CUSTOM_ID: initial-setup
:END:

#+begin_src emacs-lisp :comments none
;;; Code:
#+end_src

** Startup time

Measure and display startup time. Also, temporarily increase
~gc-cons-threshhold~ during startup to reduce reduce garbage
collection frequency. Taken from [[https://github.com/dieggsy/dotfiles/tree/3d95bc08033920e077855caf545a975eba52d28d/emacs.d#startup-time][here]].

#+begin_src emacs-lisp
(defconst ab--emacs-start-time (current-time))
(defconst ab--gc-cons-threshold gc-cons-threshold)
(defconst ab--gc-cons-percentage gc-cons-percentage)
(defvar ab--file-name-handler-alist file-name-handler-alist)
(setq gc-cons-threshold 400000000
      gc-cons-percentage 0.6
      file-name-handler-alist nil
      ;; sidesteps a bug when profiling with esup
      esup-child-profile-require-level 0)
#+end_src

Reset the variables back to default after init.

#+begin_src emacs-lisp
(add-hook
 'after-init-hook
 `(lambda ()
    (setq gc-cons-threshold ab--gc-cons-threshold
	  gc-cons-percentage ab--gc-cons-percentage
	  file-name-handler-alist ab--file-name-handler-alist)
    (let ((elapsed (float-time (time-subtract (current-time)
					      ab--emacs-start-time))))
      (message "Loading %s...done (%.3fs) [after-init]"
	       ,load-file-name elapsed))))
#+end_src

** Package management

*** =straight.el=

#+begin_quote
Next-generation, purely functional package manager for the Emacs
hacker.
#+end_quote

=straight.el= allows me to have a fully reproducible Emacs setup.

**** Bootstrap

#+begin_src emacs-lisp
(let ((bootstrap-file (concat user-emacs-directory "straight/repos/straight.el/bootstrap.el"))
      (bootstrap-version 3))
  (unless (file-exists-p bootstrap-file)
    (with-current-buffer
        (url-retrieve-synchronously
         "https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
         'silent 'inhibit-cookies)
      (goto-char (point-max))
      (eval-print-last-sexp)))
  (load bootstrap-file nil 'nomessage))
#+end_src

**** Useful helpers

#+begin_src emacs-lisp
(defun ab/reload-init ()
  "Reload init.el."
  (interactive)
  (straight-transaction
    (straight-mark-transaction-as-init)
    (message "Reloading init.el...")
    (load user-init-file nil 'nomessage)
    (message "Reloading init.el... done.")))

(defun ab/eval-buffer ()
  "Evaluate the current buffer as Elisp code."
  (interactive)
  (message "Evaluating %s..." (buffer-name))
  (straight-transaction
    (if (null buffer-file-name)
        (eval-buffer)
      (when (string= buffer-file-name user-init-file)
        (straight-mark-transaction-as-init))
      (load-file buffer-file-name)))
  (message "Evaluating %s... done." (buffer-name)))
#+end_src

*** =use-package=

#+begin_quote
A use-package declaration for simplifying your .emacs
#+end_quote

=use-package= is an awesome utility for managing and configuring
packages in a neatly organized way and without compromising on
performance. So let's install it using =striaght.el= and have it use
=straight.el= for installing packages.

#+begin_src emacs-lisp
(straight-use-package 'use-package)
(setq straight-use-package-by-default t)
#+end_src

** No littering in =~/.emacs.d=

#+begin_quote
Help keeping ~/.emacs.d clean
#+end_quote

By default, even for Emacs' built-in packages, the configuration files
and persistent data are all over the place. Use =no-littering= to help
contain the mess.

#+begin_src emacs-lisp
(use-package no-littering
  :demand t
  :config
  (savehist-mode 1)
  (add-to-list 'savehist-additional-variables 'kill-ring)
  (save-place-mode 1)
  (setq auto-save-file-name-transforms
        `((".*" ,(no-littering-expand-var-file-name "auto-save/") t))))
#+end_src

** Custom file (=custom.el=)

I'm not planning on using the custom file much, but even so, I
definitely don't want it mixing with =init.el=. So, here, let's give
it it's own file.

#+begin_src emacs-lisp
(setq custom-file (no-littering-expand-etc-file-name "custom.el"))
(when (file-exists-p custom-file)
  (load custom-file))
#+end_src

** Better =$PATH= handling

Let's use [[https://github.com/purcell/exec-path-from-shell][exec-path-from-shell]] to make Emacs use the =$PATH= as set up
in my shell.

#+begin_src emacs-lisp
(use-package exec-path-from-shell
  :defer 1
  :init
  (setq exec-path-from-shell-check-startup-files nil)
  :config
  (exec-path-from-shell-initialize)
  ;; while we're at it, let's fix access to our running ssh-agent
  (exec-path-from-shell-copy-env "SSH_AGENT_PID")
  (exec-path-from-shell-copy-env "SSH_AUTH_SOCK"))
#+end_src

** Server

Start server if not already running. Alternatively, can be done by
issuing =emacs --daemon= in the terminal, which can be automated with
a systemd service or using =brew services start emacs= on macOS. I use
Emacs as my window manager (via =exwm=), so I always start Emacs on
login; so starting the server from inside Emacs is good enough for me.

See [[https://www.gnu.org/software/emacs/manual/html_node/emacs/Emacs-Server.html#Emacs-Server][Using Emacs as a Server]].

#+begin_src emacs-lisp
(require 'server)
(unless (server-running-p)
  (server-start))
#+end_src

* Core
:PROPERTIES:
:CUSTOM_ID: core
:END:

** Defaults

*** Disable disabled commands

Emacs disables some commands by default that could persumably be
confusing for novice users. Let's disable that.

#+begin_src emacs-lisp
(setq disabled-command-function nil)
#+end_src

*** Kill-ring

Save what I copy into clipboard from other applications into Emacs'
kill-ring, which would allow me to still be able to easily access it
in case I kill (cut or copy) something else inside Emacs before
yanking (pasting) what I'd originally intended to.

#+begin_src emacs-lisp
(setq save-interprogram-paste-before-kill t)
#+end_src

*** Keep more =*Messages*=

#+begin_src emacs-lisp
(setq message-log-max 10000)
#+end_src

*** Lazy-person-friendly yes/no prompts

Lazy people would prefer to type fewer keystrokes, especially for yes
or no questions. I'm lazy.

#+begin_src emacs-lisp
(defalias 'yes-or-no-p #'y-or-n-p)
#+end_src

*** =*scratch*=

Let's customize the =*scratch*= buffer a bit. First off, I don't need
the default hint.

#+begin_src emacs-lisp
(setq initial-scratch-message "")
#+end_src

Also, let's use Text mode as the major mode, in case I want to
customize it (=*scratch*='s default major mode, Fundamental mode,
can't really be customized).

#+begin_src emacs-lisp
(setq initial-major-mode 'text-mode)
#+end_src

*** More useful frame titles

Show either the file name or the buffer name (in case the buffer isn't
visiting a file). Borrowed from Emacs Prelude.

#+begin_src emacs-lisp
(setq frame-title-format
      '("" invocation-name " - "
        (:eval (if (buffer-file-name)
                   (abbreviate-file-name (buffer-file-name))
                 "%b"))))
#+end_src

** Backups

Emacs' default backup settings aren't that great. Let's use more
sensible options. See documentation for the ~make-backup-file~
variable.

#+begin_src emacs-lisp
(setq backup-by-copying t
      version-control t)
#+end_src

* Config
:PROPERTIES:
:CUSTOM_ID: config
:END:

** Org

#+begin_src emacs-lisp
(setq org-src-tab-acts-natively t
      org-src-preserve-indentation nil
      org-edit-src-content-indentation 0)
#+end_src

* Footer
:PROPERTIES:
:CUSTOM_ID: footer
:END:

#+begin_src emacs-lisp :comments none
;;; init.el ends here
#+end_src