/
init.el
462 lines (380 loc) · 14.9 KB
/
init.el
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
;;; init.el --- Emacs Configuration -*- lexical-binding: t -*-
(defconst emacs-start-time (current-time))
(defconst mmdc-path "/home/alex/.npm-packages/bin/mmdc")
(require 'package)
(setq package-archives '(("melpa" . "https://melpa.org/packages/")
("org" . "https://orgmode.org/elpa/")
("elpa" . "https://elpa.gnu.org/packages/")))
(package-initialize)
(unless package-archive-contents
(package-refresh-contents))
(unless (package-installed-p 'use-package)
(package-install 'use-package))
(setq auto-save-default nil
create-lockfiles nil
make-backup-files nil)
(global-auto-revert-mode 1)
(set-language-environment "UTF-8")
(set-default-coding-systems 'utf-8)
(setq-default truncate-lines t)
(use-package whitespace
:init
(setq sentence-end-double-space nil
whitespace-style '(face empty trailing))
(setq-default indent-tabs-mode nil)
(global-whitespace-mode)
(add-hook 'before-save-hook #'whitespace-cleanup))
(scroll-bar-mode -1)
(tool-bar-mode -1)
(menu-bar-mode -1)
(blink-cursor-mode -1)
(setq inhibit-startup-message t)
(use-package ibuffer
:bind (("C-x C-b" . ibuffer))
:config
(setq ibuffer-use-other-window t))
(use-package icomplete
:init
(fido-mode 1))
(set-face-attribute 'default nil :font "Ubuntu Mono" :height 125)
(set-face-attribute 'fixed-pitch nil :font "Ubuntu Mono" :height 125)
(set-face-attribute 'variable-pitch nil :font "Ubuntu Light" :height 125)
(use-package all-the-icons
:ensure t)
(global-display-line-numbers-mode 1)
(dolist (hook '(doc-view-mode-hook
eshell-mode-hook
gfm-mode-hook
org-mode-hook
shell-mode-hook
term-mode-hook))
(add-hook hook (lambda () (display-line-numbers-mode 0))))
(use-package doom-modeline
:ensure t
:init (doom-modeline-mode 1)
:config
(column-number-mode 1)
(size-indication-mode 1)
(setq doom-modeline-buffer-file-name-style 'relative-to-project
doom-modeline-buffer-modification-icon t
doom-modeline-buffer-state-icon t
doom-modeline-height 25
doom-modeline-major-mode-icon t
doom-modeline-major-mode-color-icon nil
doom-modeline-minor-modes nil))
(use-package tab-bar
:config
(setq tab-bar-show nil))
(use-package modus-themes
:ensure t
:bind ("<f5>" . modus-themes-toggle)
:init
(setq modus-themes-diffs 'desaturated
modus-themes-headings '((t . rainbow-section-no-bold))
modus-themes-intense-hl-line t
modus-themes-lang-checkers 'straight-underline
modus-themes-links 'faint-neutral-underline
modus-themes-org-blocks 'grayscale
modus-themes-paren-match 'intense-bold
modus-themes-region 'bg-only-no-extend
modus-themes-scale-headings t
modus-themes-slanted-constructs t)
;; Default to the light theme
(modus-themes-load-operandi)
(show-paren-mode 1))
(defun me/buffer-select-by-major-mode (mode)
"A filter for use with `display-buffer-alist', will select a
buffer if it matches the given major-mode"
(lambda (buffer action)
(with-current-buffer buffer
(eq major-mode mode))))
(defun me/display-tabbed-buffer-in-side-window (buffer alist)
"See `display-buffer-in-side-window'"
(display-buffer-in-side-window buffer alist)
(with-current-buffer buffer
(tab-line-mode)))
(defun me/display-buffer-in-panel (predicate)
"Display buffers matching the given PREDICATE in the panel.
To borrow terminology from VSCode the panel is that collapsable
window at the bottom of the screen.
"
`(,predicate
(me/display-tabbed-buffer-in-side-window)
(window-height . 0.25)
(side . bottom)
(slot . 0)
(window-parameters . ((no-other-window . t)
(no-delete-other-windows . t)))))
(dolist (hook '(shell-mode-hook
eshell-mode-hook))
(add-hook hook (lambda () (tab-line-mode))))
(defun me/display-buffer-in-top-window (predicate)
"Display buffers matching the given PREDICATE in a top side window."
`(,predicate
(display-buffer-in-side-window)
(window-height . 0.2)
(side . top)
(slot . 0)
(window-parameters . ((no-other-window . t)))))
(use-package window
:init
(setq window-sides-vertical t)
(setq display-buffer-alist `(,(me/display-buffer-in-panel "\\*Help\\*")
,(me/display-buffer-in-panel "\\*Messages\\*")
,(me/display-buffer-in-panel "\\*\\(e?shell\\)\\*")
("\\*Process List\\*" ;; Setting no-other-window etc seems to break C-x C-c
(display-buffer-in-side-window) ;; Error => Wrong type argument window-live-p, nil...
(side . bottom)
(slot . 0)
(window-height . 0.25))
,(me/display-buffer-in-panel
(me/buffer-select-by-major-mode 'compilation-mode))
,(me/display-buffer-in-top-window "\\*Completions\\*")
,(me/display-buffer-in-panel "\\*Ibuffer\\*")
(,(me/buffer-select-by-major-mode 'dired-mode)
(display-buffer-in-side-window)
(window-width . 0.15)
(side . left)
(slot . 0)
(window-parameters . ((no-other-window . t))))
("\\*elfeed-entry\\*"
(display-buffer-reuse-mode-window display-buffer-at-bottom)
(window-height . 0.8))
,(me/display-buffer-in-panel
(me/buffer-select-by-major-mode 'inferior-python-mode))
))
:bind (("<f8>" . window-toggle-side-windows)))
(defun me/dired-open-directory ()
(interactive)
(dired default-directory))
(defun me/dired-mode-tweaks ()
(dired-hide-details-mode))
(use-package dired
:bind (("C-x d" . me/dired-open-directory))
:hook (dired-mode . me/dired-mode-tweaks))
(defun me/elfeed-start-new-tab ()
"Does the work of creating a new tab"
(tab-bar-new-tab)
(tab-bar-rename-tab "elfeed")
(elfeed))
(defun me/elfeed-start ()
"Switch to the elfeed tab, create one if it doesn't exist."
(interactive)
(let ((tabs (mapcar (lambda (tab) (alist-get 'name tab)) (tab-bar-tabs))))
(if (member "elfeed" tabs)
(tab-bar-select-tab-by-name "elfeed")
(me/elfeed-start-new-tab))))
(defun me/feed-table-row-to-item (row)
"Convert a ROW from a feed table into a valid elfeed entry"
(let* ((url (car row))
(tags (split-string (cadr row) " ")))
(append (list url) (mapcar 'make-symbol tags))))
(defun me/feed-table-to-list (feed-table)
"Convert an orgmode FEED-TABLE to a list compatible with elfeed."
(mapcar 'me/feed-table-row-to-item feed-table))
(defun me/feed-table-extract ()
"Find the table called `elfeed-feed-table' in my config file
and use it to set the `elfeed-feeds' variable."
(with-temp-buffer
(insert-file-contents "/home/alex/.emacs.d/README.org")
(org-table-map-tables (lambda ()
(setq tbl-name (plist-get (cadr (org-element-at-point)) :name))
(if (string= tbl-name "elfeed-feed-table")
(let ((tbl (cdr (cdr (org-table-to-lisp)))))
(setq elfeed-feeds (me/feed-table-to-list tbl)))))
t)))
(defun me/elfeed-update (arg)
"Refresh all RSS feeds.
This simply calls `elfeed-update' unless the prefix arg is set
or `elfeed-feeds' is nil in which case it will call
`me/feed-table-extract' beforehand."
(interactive "P")
(if (or (not (null arg))
(null elfeed-feeds))
(me/feed-table-extract))
(elfeed-update))
(use-package elfeed
:bind (("C-c e" . me/elfeed-start)
:map elfeed-search-mode-map
("g" . me/elfeed-update)
:map elfeed-show-mode-map
("q" . delete-window))
:ensure t
:config
(setq elfeed-use-curl t)
(setq elfeed-show-entry-switch 'pop-to-buffer)
(setq elfeed-show-entry-delete 'delete-window))
(use-package git-gutter
:config
(global-git-gutter-mode 1)
(set-face-foreground 'git-gutter:added "forest green")
(set-face-foreground 'git-gutter:modified "goldenrod")
(set-face-foreground 'git-gutter:deleted "brown")
(setq git-gutter:added-sign "▐"
git-gutter:modified-sign "▐"
git-gutter:removed-sign "▐"))
(use-package magit
:bind (("C-x g" . magit-status)))
(use-package lsp-mode
:ensure t
:init
(setq lsp-keymap-prefix "C-c l")
:hook ((python-mode . lsp))
:commands lsp)
(use-package ob-mermaid
:ensure t
:config
(setq ob-mermaid-cli-path mmdc-path))
(defun me/org-mode-tweaks ()
(setq-local fill-column 80)
(turn-on-auto-fill)
(flyspell-mode)
(variable-pitch-mode 1)
;; Switch certain elements back to fixed pitch
(set-face-attribute 'org-block nil :foreground nil :inherit 'fixed-pitch)
(set-face-attribute 'org-link nil :inherit '(button fixed-pitch))
(set-face-attribute 'org-code nil :inherit '(shadow fixed-pitch))
(set-face-attribute 'org-table nil :inherit '(shadow fixed-pitch))
(set-face-attribute 'org-verbatim nil :inherit '(shadow fixed-pitch))
(set-face-attribute 'org-special-keyword nil
:inherit '(font-lock-comment-face fixed-pitch))
(set-face-attribute 'org-meta-line nil
:inherit '(font-lock-comment-face fixed-pitch))
(set-face-attribute 'org-checkbox nil :inherit 'fixed-pitch))
(use-package org
:hook (org-mode . me/org-mode-tweaks)
:bind (("C-c a" . org-agenda)
("C-c c" . org-capture))
:config
(setq org-directory "~/Documents/org/")
(setq org-agenda-files (list org-directory))
(org-babel-do-load-languages 'org-babel-load-languages
'((emacs-lisp . t)
(python . t)
(shell . t)))
(setq org-capture-templates
'(("t" "Task" entry (file+headline "life.org" "Events")
"* TODO %?\n")
("e" "Event" entry (file+headline "life.org" "Events")
"* %?\nSCHEDULED: %^t")
("j" "Journal" entry (file+headline "life.org" "Journal")
"* %u\n%?\n\n** Exercise\n" :prepend t)))
(add-to-list 'org-modules 'org-habit t)
(setq org-adapt-indentation 'headline-data
org-habit-show-all-today t
org-log-into-drawer t))
(defun me/project-search ()
"Execute a project wide search with ripgrep."
(interactive)
(let ((dir (cdr (project-current t)))
(query (read-string "Search query: ")))
(rg query "*" dir)))
(use-package rg
:ensure t)
(use-package project
:bind (("C-x p f" . project-find-file)
("C-x p s" . me/project-search)))
(defun me/start-debugging ()
(interactive)
(let ((program (read-string "Debug program: ")))
(tab-new)
(setq gdb-many-windows t)
(gdb (format "gdb -i=mi %s" program))))
(advice-add 'gud-sentinel :after
(lambda (proc msg)
(when (memq (process-status proc) '(signal exit))
(tab-close))))
(use-package cc-mode
:bind (:map c-mode-map
("C-c d" . me/start-debugging)
("C-c g" . recompile))
:config
(setq-default c-basic-offset 4)
(setq compilation-scroll-output t))
(defun me/python-mode-tweaks ()
(setq-local fill-column 88))
(defun me/python-open-repl ()
"Open a Python REPL in the correct virtualenv for the
project.
This will first look for Jupyter and will fall back to Python if
it's not installed.
It will attempt to find the root folder for the current package
and open the shell there.
"
(interactive)
(let* ((dir (cdr (project-current t)))
(paths (list
(concat dir ".env/bin/jupyter")
(concat dir ".env/bin/python")))
(path (car (seq-filter 'file-exists-p paths))))
(setq python-shell-interpreter path
python-shell-prompt-detect-failure-warning nil)
(if (string-match-p (regexp-quote "jupyter") path)
(setq python-shell-interpreter-args "console --simple-prompt")
(setq python-shell-interpreter-args "-i"))
(let* ((package-dir (me/python-find-setup-py
(buffer-file-name (current-buffer))))
(default-directory (file-name-directory package-dir)))
(run-python))))
(defun me/python-flake8-project ()
"Run flake8 on the current project in a compilation buffer.
This function will attempt to find the setup.py file for the
package currently being edited using `me/python-find-setup-py'.
If a filepath is found, its parent directory is assumed to be the
package root and flake8 will be run in a compilation buffer via
`compilation-start'."
(interactive)
(let* ((filename (buffer-file-name (current-buffer)))
(setup-py (me/python-find-setup-py filename)))
(unless (null setup-py)
(let ((default-directory (file-name-directory setup-py)))
(compilation-start "flake8" nil
(lambda (_modename)
(format "%s: flake8" default-directory)))))))
(defun me/python-find-setup-py--from-dir (dir)
(if (string= dir "/")
nil
(let* ((setup-py (concat dir "setup.py")))
(if (file-exists-p setup-py)
setup-py
(me/python-find-setup-py
(file-name-directory (directory-file-name dir)))))))
(defun me/python-find-setup-py (filename)
"Find the setup.py file that corresponds with the package that
contains FILENAME"
(me/python-find-setup-py--from-dir
(file-name-directory filename)))
(use-package blacken
:ensure t
:hook (python-mode . blacken-mode))
(use-package python
:bind (:map python-mode-map
("C-c C-p" . me/python-open-repl)
("C-c g" . recompile))
:hook (python-mode . me/python-mode-tweaks)
:config
(setq compilation-scroll-output t)
(add-to-list 'python-shell-completion-native-disabled-interpreters
"jupyter"))
(use-package mermaid-mode
:ensure t
:config
(setq mermaid-mmdc-location mmdc-path))
(defun me/gfm-mode-tweaks ()
(setq-local fill-column 80)
(turn-on-auto-fill)
(flyspell-mode)
(variable-pitch-mode 1)
;; Switch certain elements back to fixed pitch
(set-face-attribute 'markdown-metadata-key-face nil :inherit 'fixed-pitch)
(set-face-attribute 'markdown-metadata-value-face nil :inherit 'fixed-pitch))
(use-package markdown-mode
:ensure t
:hook (gfm-mode . me/gfm-mode-tweaks)
:mode (("\\.md\\'" . gfm-mode)
("\\.markdown\\'" . gfm-mode)))
(setq custom-file "~/.emacs.d/custom.el")
(load custom-file 'noerror)
(let ((startup-time
(float-time (time-subtract (current-time) emacs-start-time))))
(message "Loaded configuration in %.3fs" startup-time))