[bspwm,x] a few minor changes
[~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
365 :demand t
366 :config)
367#+end_src
abdc6c07 368
421132e9
AB
369*** [[https://github.com/ch11ng/exwm][exwm]] (window manager)
370
371#+begin_src emacs-lisp
372(use-package exwm
373 :config
374 (require 'exwm-config)
51fb5bd8
AB
375 (exwm-config-default)
376 (require 'exwm-systemtray)
377 (exwm-systemtray-enable))
421132e9
AB
378#+end_src
379
380*** [[https://orgmode.org/][Org mode]]
381
382#+begin_quote
383Org mode is for keeping notes, maintaining TODO lists, planning
384projects, and authoring documents with a fast and effective plain-text
385system.
386#+end_quote
387
388In short, my favourite way of life.
bab98ee5
AB
389
390#+begin_src emacs-lisp
391(setq org-src-tab-acts-natively t
392 org-src-preserve-indentation nil
393 org-edit-src-content-indentation 0)
394#+end_src
395
421132e9
AB
396*** [[https://magit.vc/][Magit]]
397
398#+begin_quote
399It's Magit! A Git porcelain inside Emacs.
400#+end_quote
401
402Not just how I do git, but /the/ way to do git.
403
404#+begin_src emacs-lisp
405(use-package magit
406 :general
407 ("s-g" 'magit-status))
408#+end_src
409
410*** [[https://github.com/abo-abo/swiper][Ivy]] (and friends)
411
412#+begin_quote
413Ivy - a generic completion frontend for Emacs, Swiper - isearch with
414an overview, and more. Oh, man!
415#+end_quote
416
417There's no way I could top that, so I won't attempt to.
418
419**** Ivy
420
421#+begin_src emacs-lisp
422(use-package ivy
423 :general
424 (ivy-minibuffer-map
425 [escape] 'keyboard-escape-quit
426 "C-j" 'ivy-next-line
427 "C-k" 'ivy-previous-line
428 [S-up] 'ivy-previous-history-element
429 [S-down] 'ivy-next-history-element
430 "DEL" 'ivy-backward-delete-char)
431 :config
432 (ivy-mode 1))
433#+end_src
434
435**** Swiper
436
437#+begin_src emacs-lisp
438(use-package swiper
439 :general ("C-s" 'swiper))
440#+end_src
441
442**** Counsel
443
444#+begin_src emacs-lisp
445(use-package counsel
446 :general
447 ("M-x" 'counsel-M-x
448 "C-x C-f" 'counsel-find-file
449 "s-r" 'counsel-recentf)
450 (imap minibuffer-local-map
451 "C-r" 'counsel-minibuffer-history)
452 :config
453 (counsel-mode 1)
454 (defalias 'locate #'counsel-locate))
455#+end_src
456
35ea1ba4 457* Footer
279a98e2
AB
458:PROPERTIES:
459:CUSTOM_ID: footer
460:END:
35ea1ba4
AB
461
462#+begin_src emacs-lisp :comments none
35ea1ba4
AB
463;;; init.el ends here
464#+end_src