add CUSOTM_IDs to make toc links work in Emacs
[~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 TODO: description
7
8 * Contents :toc_1:noexport:
9
10 - [[#intro][Intro]]
11 - [[#header][Header]]
12 - [[#initial-setup][Initial setup]]
13 - [[#config][Config]]
14 - [[#footer][Footer]]
15
16 * Header
17 :PROPERTIES:
18 :CUSTOM_ID: header
19 :END:
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
27 Enable =view-mode=, which both makes the file read-only (as a reminder
28 that =init.el= is an auto-generated file, not supposed to be edited),
29 and provides some convenient key bindings for browsing through the
30 file.
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
53 #+begin_src emacs-lisp :comments none
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
62 * Initial setup
63 :PROPERTIES:
64 :CUSTOM_ID: initial-setup
65 :END:
66
67 #+begin_src emacs-lisp :comments none
68 ;;; Code:
69 #+end_src
70
71 ** Startup time
72
73 Measure and display startup time. Also, temporarily increase
74 ~gc-cons-threshhold~ during startup to reduce reduce garbage
75 collection frequency. Taken from [[https://github.com/dieggsy/dotfiles/tree/3d95bc08033920e077855caf545a975eba52d28d/emacs.d#startup-time][here]].
76
77 #+begin_src emacs-lisp
78 (defconst aminb/emacs-start-time (current-time))
79 (defconst aminb/gc-cons-threshold gc-cons-threshold)
80 (defconst aminb/gc-cons-percentage gc-cons-percentage)
81 (defvar aminb/file-name-handler-alist file-name-handler-alist)
82 (setq gc-cons-threshold 400000000
83 gc-cons-percentage 0.6
84 file-name-handler-alist nil
85 ;; sidesteps a bug when profiling with esup
86 esup-child-profile-require-level 0)
87 #+end_src
88
89 Reset the variables back to default after init.
90
91 #+begin_src emacs-lisp
92 (add-hook
93 'after-init-hook
94 `(lambda ()
95 (setq gc-cons-threshold aminb/gc-cons-threshold
96 gc-cons-percentage aminb/gc-cons-percentage
97 file-name-handler-alist aminb/file-name-handler-alist)
98 (let ((elapsed (float-time (time-subtract (current-time)
99 aminb/emacs-start-time))))
100 (message "Loading %s...done (%.3fs) [after-init]"
101 ,load-file-name elapsed))))
102 #+end_src
103
104 ** Package management
105
106 *** =straight.el=
107
108 #+begin_quote
109 Next-generation, purely functional package manager for the Emacs
110 hacker.
111 #+end_quote
112
113 =straight.el= allows me to have a fully reproducible Emacs setup.
114
115 **** Bootstrap
116
117 #+begin_src emacs-lisp
118 (let ((bootstrap-file (concat user-emacs-directory "straight/repos/straight.el/bootstrap.el"))
119 (bootstrap-version 3))
120 (unless (file-exists-p bootstrap-file)
121 (with-current-buffer
122 (url-retrieve-synchronously
123 "https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
124 'silent 'inhibit-cookies)
125 (goto-char (point-max))
126 (eval-print-last-sexp)))
127 (load bootstrap-file nil 'nomessage))
128 #+end_src
129
130 **** Useful helpers
131
132 #+begin_src emacs-lisp
133 (defun straight-reload-init ()
134 "Reload init.el."
135 (interactive)
136 (straight-transaction
137 (straight-mark-transaction-as-init)
138 (message "Reloading init.el...")
139 (load user-init-file nil 'nomessage)
140 (message "Reloading init.el... done.")))
141
142 (defun straight-eval-buffer ()
143 "Evaluate the current buffer as Elisp code."
144 (interactive)
145 (message "Evaluating %s..." (buffer-name))
146 (straight-transaction
147 (if (null buffer-file-name)
148 (eval-buffer)
149 (when (string= buffer-file-name user-init-file)
150 (straight-mark-transaction-as-init))
151 (load-file buffer-file-name)))
152 (message "Evaluating %s... done." (buffer-name)))
153 #+end_src
154
155 *** =use-package=
156
157 #+begin_quote
158 A use-package declaration for simplifying your .emacs.
159 #+end_quote
160
161 =use-package= is an awesome utility for managing and configuring
162 packages in a neatly organized way and without compromising on
163 performance. So let's install it using =striaght.el= and have it use
164 =straight.el= for installing packages.
165
166 #+begin_src emacs-lisp
167 (straight-use-package 'use-package)
168 (setq straight-use-package-by-default t)
169 #+end_src
170
171 ** No littering in =~/.emacs.d=
172
173 #+begin_quote
174 Help keeping ~/.emacs.d clean.
175 #+end_quote
176
177 By default, even for Emacs' built-in packages, the configuration files
178 and persistent data are all over the place. Use =no-littering= to help
179 contain the mess.
180
181 #+begin_src emacs-lisp
182 (use-package no-littering
183 :demand t
184 :config
185 (savehist-mode 1)
186 (add-to-list 'savehist-additional-variables 'kill-ring)
187 (save-place-mode 1)
188 (setq auto-save-file-name-transforms
189 `((".*" ,(no-littering-expand-var-file-name "auto-save/") t))))
190 #+end_src
191
192 ** Custom file (=custom.el=)
193
194 I'm not planning on using the custom file much, but even so, I
195 definitely don't want it mixing with =init.el=. So, here, let's give
196 it it's own file.
197
198 #+begin_src emacs-lisp
199 (setq custom-file (no-littering-expand-etc-file-name "custom.el"))
200 (when (file-exists-p custom-file)
201 (load custom-file))
202 #+end_src
203
204 ** Backups
205
206 Emacs' default backup settings aren't that great. Let's use more
207 sensible options. See documentation for the ~make-backup-file~
208 variable.
209
210 #+begin_src emacs-lisp
211 (setq backup-by-copying t
212 version-control t)
213 #+end_src
214
215 * Config
216 :PROPERTIES:
217 :CUSTOM_ID: config
218 :END:
219
220 ** Org
221
222 #+begin_src emacs-lisp
223 (setq org-src-tab-acts-natively t
224 org-src-preserve-indentation nil
225 org-edit-src-content-indentation 0)
226 #+end_src
227
228 * Footer
229 :PROPERTIES:
230 :CUSTOM_ID: footer
231 :END:
232
233 #+begin_src emacs-lisp :comments none
234 ;;; init.el ends here
235 #+end_src