add straight.el, use-package, and no-littering
[~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 TODO: toc
9
10 * Header
11
12 ** First line
13
14 #+begin_src emacs-lisp :comments none
15 ;;; init.el --- Amin Bandali's Emacs config -*- lexical-binding: t ; eval: (view-mode 1)-*-
16 #+end_src
17
18 Enable =view-mode=, which both makes the file read-only (as a reminder
19 that =init.el= is an auto-generated file, not supposed to be edited),
20 and provides some convenient key bindings for browsing through the
21 file.
22
23 ** License
24
25 #+begin_src emacs-lisp :comments none
26 ;; Copyright (C) 2018 Amin Bandali <amin@aminb.org>
27
28 ;; This program is free software: you can redistribute it and/or modify
29 ;; it under the terms of the GNU General Public License as published by
30 ;; the Free Software Foundation, either version 3 of the License, or
31 ;; (at your option) any later version.
32
33 ;; This program is distributed in the hope that it will be useful,
34 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
35 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 ;; GNU General Public License for more details.
37
38 ;; You should have received a copy of the GNU General Public License
39 ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
40 #+end_src
41
42 ** Commentary
43
44 #+begin_src emacs-lisp :comments none
45 ;;; Commentary:
46
47 ;; Emacs configuration of Amin Bandali, computer scientist and functional
48 ;; programmer.
49
50 ;; THIS FILE IS AUTO-GENERATED FROM `init.org'.
51 #+end_src
52
53 * Initial setup
54
55 #+begin_src emacs-lisp :comments none
56 ;;; Code:
57 #+end_src
58
59 ** Startup time
60
61 Measure and display startup time. Also, temporarily increase
62 ~gc-cons-threshhold~ during startup to reduce reduce garbage
63 collection frequency. Taken from [[https://github.com/dieggsy/dotfiles/tree/3d95bc08033920e077855caf545a975eba52d28d/emacs.d#startup-time][here]].
64
65 #+begin_src emacs-lisp
66 (defconst aminb/emacs-start-time (current-time))
67 (defconst aminb/gc-cons-threshold gc-cons-threshold)
68 (defconst aminb/gc-cons-percentage gc-cons-percentage)
69 (defvar aminb/file-name-handler-alist file-name-handler-alist)
70 (setq gc-cons-threshold 400000000
71 gc-cons-percentage 0.6
72 file-name-handler-alist nil
73 ;; sidesteps a bug when profiling with esup
74 esup-child-profile-require-level 0)
75 #+end_src
76
77 Reset the variables back to default after init.
78
79 #+begin_src emacs-lisp
80 (add-hook
81 'after-init-hook
82 `(lambda ()
83 (setq gc-cons-threshold aminb/gc-cons-threshold
84 gc-cons-percentage aminb/gc-cons-percentage
85 file-name-handler-alist aminb/file-name-handler-alist)
86 (let ((elapsed (float-time (time-subtract (current-time)
87 aminb/emacs-start-time))))
88 (message "Loading %s...done (%.3fs) [after-init]"
89 ,load-file-name elapsed))))
90 #+end_src
91
92 ** Package management
93
94 *** [[https://github.com/raxod502/straight.el][=straight.el=]]
95
96 #+begin_quote
97 Next-generation, purely functional package manager for the Emacs
98 hacker.
99 #+end_quote
100
101 =straight.el= allows me to have a fully reproducible Emacs setup.
102
103 **** Bootstrap
104
105 #+begin_src emacs-lisp
106 (let ((bootstrap-file (concat user-emacs-directory "straight/repos/straight.el/bootstrap.el"))
107 (bootstrap-version 3))
108 (unless (file-exists-p bootstrap-file)
109 (with-current-buffer
110 (url-retrieve-synchronously
111 "https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
112 'silent 'inhibit-cookies)
113 (goto-char (point-max))
114 (eval-print-last-sexp)))
115 (load bootstrap-file nil 'nomessage))
116 #+end_src
117
118 **** Useful helpers
119
120 #+begin_src emacs-lisp
121 (defun straight-reload-init ()
122 "Reload init.el."
123 (interactive)
124 (straight-transaction
125 (straight-mark-transaction-as-init)
126 (message "Reloading init.el...")
127 (load user-init-file nil 'nomessage)
128 (message "Reloading init.el... done.")))
129
130 (defun straight-eval-buffer ()
131 "Evaluate the current buffer as Elisp code."
132 (interactive)
133 (message "Evaluating %s..." (buffer-name))
134 (straight-transaction
135 (if (null buffer-file-name)
136 (eval-buffer)
137 (when (string= buffer-file-name user-init-file)
138 (straight-mark-transaction-as-init))
139 (load-file buffer-file-name)))
140 (message "Evaluating %s... done." (buffer-name)))
141 #+end_src
142
143 *** =use-package=
144
145 #+begin_quote
146 A use-package declaration for simplifying your .emacs.
147 #+end_quote
148
149 =use-package= is an awesome utility for managing and configuring
150 packages in a neatly organized way and without compromising on
151 performance. So let's install it using =striaght.el= and have it use
152 =straight.el= for installing packages.
153
154 #+begin_src emacs-lisp
155 (straight-use-package 'use-package)
156 (setq straight-use-package-by-default t)
157 #+end_src
158
159 ** No littering in =~/.emacs.d=
160
161 #+begin_quote
162 Help keeping ~/.emacs.d clean.
163 #+end_quote
164
165 By default, even for Emacs' built-in packages, the configuration files
166 and persistent data are all over the place. Use =no-littering= to help
167 contain the mess.
168
169 #+begin_src emacs-lisp
170 (use-package no-littering
171 :demand t
172 :config
173 (savehist-mode 1)
174 (add-to-list 'savehist-additional-variables 'kill-ring)
175 (save-place-mode 1)
176 (setq auto-save-file-name-transforms
177 `((".*" ,(no-littering-expand-var-file-name "auto-save/") t))))
178 #+end_src
179
180 ** Custom file (=custom.el=)
181
182 I'm not planning on using the custom file much, but even so, I
183 definitely don't want it mixing with =init.el=. So, here, let's give
184 it it's own file.
185
186 #+begin_src emacs-lisp
187 (setq custom-file (no-littering-expand-etc-file-name "custom.el"))
188 (when (file-exists-p custom-file)
189 (load custom-file))
190 #+end_src
191
192 ** Org
193
194 #+begin_src emacs-lisp
195 (setq org-src-tab-acts-natively t
196 org-src-preserve-indentation nil
197 org-edit-src-content-indentation 0)
198 #+end_src
199
200 * Footer
201
202 #+begin_src emacs-lisp :comments none
203 ;;; init.el ends here
204 #+end_src