870f265d186ac303f96d3f85d7eb7562458d87d1
[~bandali/configs] / init.el
1 ;;; init.el --- bandali's emacs configuration -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2018-2020 Amin Bandali <bandali@gnu.org>
4
5 ;; This program is free software: you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation, either version 3 of the License, or
8 ;; (at your option) any later version.
9
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
14
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
17
18 ;;; Commentary:
19
20 ;; GNU Emacs configuration of Amin Bandali, computer scientist,
21 ;; Free Software activist, and GNU maintainer & webmaster. Packages
22 ;; are installed through using Borg for a fully reproducible setup.
23
24 ;; Over the years, I've taken inspiration from configurations of many
25 ;; great people. Some that I can remember off the top of my head are:
26 ;;
27 ;; - https://github.com/dieggsy/dotfiles
28 ;; - https://github.com/dakra/dmacs
29 ;; - http://pages.sachachua.com/.emacs.d/Sacha.html
30 ;; - https://github.com/dakrone/eos
31 ;; - http://doc.rix.si/cce/cce.html
32 ;; - https://github.com/jwiegley/dot-emacs
33 ;; - https://github.com/wasamasa/dotemacs
34 ;; - https://github.com/hlissner/doom-emacs
35
36 ;;; Code:
37
38 ;;; Emacs initialization
39
40 (defvar b/before-user-init-time (current-time)
41 "Value of `current-time' when Emacs begins loading `user-init-file'.")
42 (defvar b/emacs-initialized nil
43 "Whether Emacs has been initialized.")
44
45 (when (not (bound-and-true-p b/emacs-initialized))
46 (message "Loading Emacs...done (%.3fs)"
47 (float-time (time-subtract b/before-user-init-time
48 before-init-time))))
49
50 ;; temporarily increase `gc-cons-threshhold' and `gc-cons-percentage'
51 ;; during startup to reduce garbage collection frequency. clearing
52 ;; `file-name-handler-alist' seems to help reduce startup time too.
53 (defvar b/gc-cons-threshold gc-cons-threshold)
54 (defvar b/gc-cons-percentage gc-cons-percentage)
55 (defvar b/file-name-handler-alist file-name-handler-alist)
56 (setq gc-cons-threshold (* 30 1024 1024) ; 30 MiB
57 gc-cons-percentage 0.6
58 file-name-handler-alist nil
59 ;; sidesteps a bug when profiling with esup
60 esup-child-profile-require-level 0)
61
62 ;; set them back to their defaults once we're done initializing
63 (defun b/post-init ()
64 "My post-initialize function, run after loading `user-init-file'."
65 (setq b/emacs-initialized t
66 gc-cons-threshold b/gc-cons-threshold
67 gc-cons-percentage b/gc-cons-percentage
68 file-name-handler-alist b/file-name-handler-alist)
69 (when (featurep 'exwm-workspace)
70 (with-eval-after-load 'exwm-workspace
71 (setq-default
72 mode-line-format
73 (append
74 mode-line-format
75 '((:eval
76 (format
77 "[%s]" (number-to-string
78 exwm-workspace-current-index))))))))
79
80 ;; make some mode-line spaces smaller
81 (csetq
82 mode-line-format
83 (mapcar
84 (lambda (x)
85 (if (and (stringp x) (or (string= x " ") (string= x " ")))
86 " "
87 x))
88 mode-line-format)
89 mode-line-buffer-identification
90 (propertized-buffer-identification "%10b")))
91 (add-hook 'after-init-hook #'b/post-init)
92
93 ;; increase number of lines kept in *Messages* log
94 (setq message-log-max 20000)
95
96 ;; optionally, uncomment to supress some byte-compiler warnings
97 ;; (see C-h v byte-compile-warnings RET for more info)
98 ;; (setq byte-compile-warnings
99 ;; '(not free-vars unresolved noruntime lexical make-local))
100
101 \f
102 ;;; whoami
103
104 (setq user-full-name "Amin Bandali"
105 user-mail-address "bandali@gnu.org")
106
107 \f
108 ;;; Package management
109
110 ;; (progn ; `borg'
111 ;; (add-to-list 'load-path
112 ;; (expand-file-name "lib/borg" user-emacs-directory))
113 ;; (require 'borg)
114 ;; (borg-initialize)
115 ;; (setq borg-rewrite-urls-alist
116 ;; '(("git@github.com:" . "https://github.com/")
117 ;; ("git@gitlab.com:" . "https://gitlab.com/"))))
118
119 ;; variables of interest:
120 ;; package-archive-priorities
121 ;; package-load-list
122 ;; package-pinned-packages
123
124 ;; (let* ((b (find-file-noselect "refinery-theme.el"))
125 ;; (d (with-current-buffer b (package-buffer-info))))
126 ;; (package-generate-description-file d "refinery-theme-pkg.el"))
127 (run-with-idle-timer 0.01 nil #'require 'package)
128 (with-eval-after-load 'package
129 (when (= (length package-archives) 1)
130 (csetq
131 package-archives
132 `(,@package-archives
133 ("org" . "https://orgmode.org/elpa/")
134 ("bndl" . "https://p.bndl.org/elpa/"))))
135 (package-initialize))
136
137 (csetq package-archive-upload-base "/ssh:caffeine:~/www/p/elpa")
138
139 \f
140 ;;; Initial setup
141
142 ;; keep ~/.emacs.d clean
143 (defvar b/etc-dir
144 (expand-file-name
145 (convert-standard-filename "etc/") user-emacs-directory)
146 "The directory where packages place their configuration files.")
147 (defvar b/var-dir
148 (expand-file-name
149 (convert-standard-filename "var/") user-emacs-directory)
150 "The directory where packages place their persistent data files.")
151 (defvar b/lisp-dir
152 (expand-file-name
153 (convert-standard-filename "lisp/") user-emacs-directory)
154 "The directory where packages place their persistent data files.")
155 (defun b/etc (file)
156 "Expand filename FILE relative to `b/etc-dir'."
157 (expand-file-name (convert-standard-filename file) b/etc-dir))
158 (defun b/var (file)
159 "Expand filename FILE relative to `b/var-dir'."
160 (expand-file-name (convert-standard-filename file) b/var-dir))
161 (defun b/lisp (file)
162 "Expand filename FILE relative to `b/lisp-dir'."
163 (expand-file-name (convert-standard-filename file) b/lisp-dir))
164
165 (csetq
166 auto-save-list-file-prefix (b/var "auto-save/sessions/")
167 nsm-settings-file (b/var "nsm-settings.el"))
168
169 ;; separate custom file (don't want it mixing with init.el)
170 (with-eval-after-load 'custom
171 (setq custom-file (b/etc "custom.el"))
172 (when (file-exists-p custom-file)
173 (load custom-file))
174 ;; while at it, treat themes as safe
175 ;; (setf custom-safe-themes t)
176 ;; only one custom theme at a time
177 (comment
178 (defadvice load-theme (before clear-previous-themes activate)
179 "Clear existing theme settings instead of layering them"
180 (mapc #'disable-theme custom-enabled-themes))))
181
182 ;; load the secrets file if it exists, otherwise show a warning
183 (comment
184 (with-demoted-errors
185 (load (b/etc "secrets"))))
186
187 ;; start up emacs server. see
188 ;; https://www.gnu.org/software/emacs/manual/html_node/emacs/Emacs-Server.html#Emacs-Server
189 (run-with-idle-timer 0.5 nil #'require 'server)
190 (with-eval-after-load 'server
191 (declare-function server-edit "server")
192 (global-set-key (kbd "C-c F D") #'server-edit)
193 (declare-function server-running-p "server")
194 (or (server-running-p) (server-mode)))
195
196 \f
197 ;;; Defaults
198
199 ;;;; C-level customizations
200
201 (csetq
202 ;; minibuffer
203 enable-recursive-minibuffers t
204 resize-mini-windows t
205 ;; more useful frame titles
206 ;; frame-title-format '("" invocation-name " - "
207 ;; (:eval
208 ;; (if (buffer-file-name)
209 ;; (abbreviate-file-name (buffer-file-name))
210 ;; "%b")))
211 ;; i don't feel like jumping out of my chair every now and again; so
212 ;; don't BEEP! at me, emacs
213 ring-bell-function 'ignore
214 ;; better scrolling
215 ;; scroll-margin 1
216 ;; scroll-conservatively 10000
217 scroll-step 1
218 scroll-conservatively 101
219 scroll-preserve-screen-position 1
220 ;; focus follows mouse
221 mouse-autoselect-window t)
222
223 (setq-default
224 ;; always use space for indentation
225 indent-tabs-mode nil
226 tab-width 4
227 ;; case-sensitive search (and `dabbrev-expand')
228 ;; case-fold-search nil
229 ;; cursor shape
230 cursor-type t)
231
232 (set-fontset-font t 'arabic "Vazir")
233
234 ;; unicode support
235 (comment
236 (dolist (ft (fontset-list))
237 (set-fontset-font
238 ft
239 'unicode
240 (font-spec :name "Source Code Pro" :size 14))
241 (set-fontset-font
242 ft
243 'unicode
244 (font-spec :name "DejaVu Sans Mono")
245 nil
246 'append)
247 ;; (set-fontset-font
248 ;; ft
249 ;; 'unicode
250 ;; (font-spec
251 ;; :name "Symbola monospacified for DejaVu Sans Mono")
252 ;; nil
253 ;; 'append)
254 ;; (set-fontset-font
255 ;; ft
256 ;; #x2115 ; ℕ
257 ;; (font-spec :name "DejaVu Sans Mono")
258 ;; nil
259 ;; 'append)
260 (set-fontset-font
261 ft
262 (cons ?Α ?ω)
263 (font-spec :name "DejaVu Sans Mono" :size 14)
264 nil
265 'prepend)))
266
267 ;;;; Elisp-level customizations
268
269 ;; startup
270 ;; don't need to see the startup echo area message
271 (advice-add #'display-startup-echo-area-message :override #'ignore)
272 (csetq
273 ;; i want *scratch* as my startup buffer
274 initial-buffer-choice t
275 ;; i don't need the default hint
276 initial-scratch-message nil
277 ;; use customizable text-mode as major mode for *scratch*
278 ;; (initial-major-mode 'text-mode)
279 ;; inhibit buffer list when more than 2 files are loaded
280 inhibit-startup-buffer-menu t
281 ;; don't need to see the startup screen or echo area message
282 inhibit-startup-screen t
283 inhibit-startup-echo-area-message user-login-name)
284
285 ;; files
286 (csetq
287 ;; backups (C-h v make-backup-files RET)
288 backup-by-copying t
289 backup-directory-alist (list (cons "." (b/var "backup/")))
290 version-control t
291 delete-old-versions t
292 ;; auto-save
293 auto-save-file-name-transforms `((".*" ,(b/var "auto-save/") t))
294 ;; insert newline at the end of files
295 require-final-newline t
296 ;; open read-only file buffers in view-mode
297 ;; (enables niceties like `q' for quit)
298 view-read-only t)
299
300 ;; novice
301 ;; disable disabled commands
302 (csetq disabled-command-function nil)
303
304 ;; lazy-person-friendly yes/no prompts
305 (defalias 'yes-or-no-p #'y-or-n-p)
306
307 ;; autorevert: enable automatic reloading of changed buffers and files
308 (csetq auto-revert-verbose nil
309 global-auto-revert-non-file-buffers nil)
310 (require 'autorevert)
311 (global-auto-revert-mode 1)
312
313 ;; time and battery in mode-line
314 (csetq
315 display-time-default-load-average nil
316 display-time-format " %a %b %-e %-l:%M%P"
317 display-time-mail-icon '(image :type xpm
318 :file "gnus/gnus-pointer.xpm"
319 :ascent center)
320 display-time-use-mail-icon t)
321 (require 'time)
322 (display-time-mode)
323
324 (csetq battery-mode-line-format " %p%% %t")
325 (require 'battery)
326 (display-battery-mode)
327
328 (require 'fringe)
329 ;; smaller fringe
330 ;; (fringe-mode '(3 . 1))
331 (fringe-mode nil)
332
333 (require 'winner)
334 ;; enable winner-mode (C-h f winner-mode RET)
335 (winner-mode 1)
336
337 (with-eval-after-load 'compile
338 ;; don't display *compilation* buffer on success. based on
339 ;; https://stackoverflow.com/a/17788551, with changes to use `cl-letf'
340 ;; instead of the now obsolete `flet'.
341 (defun b/compilation-finish-function (buffer outstr)
342 (unless (string-match "finished" outstr)
343 (switch-to-buffer-other-window buffer))
344 t)
345
346 (setq compilation-finish-functions #'b/compilation-finish-function)
347
348 (require 'cl-macs)
349
350 (defadvice compilation-start
351 (around inhibit-display
352 (command &optional mode name-function highlight-regexp))
353 (if (not (string-match "^\\(find\\|grep\\)" command))
354 (cl-letf (((symbol-function 'display-buffer) #'ignore))
355 (save-window-excursion ad-do-it))
356 ad-do-it))
357 (ad-activate 'compilation-start))
358
359 ;; isearch
360 (csetq
361 ;; allow scrolling in Isearch
362 isearch-allow-scroll t
363 ;; search for non-ASCII characters: i’d like non-ASCII characters such
364 ;; as ‘’“”«»‹›áⓐ𝒶 to be selected when i search for their ASCII
365 ;; counterpart. shoutout to
366 ;; http://endlessparentheses.com/new-in-emacs-25-1-easily-search-non-ascii-characters.html
367 search-default-mode #'char-fold-to-regexp)
368
369 ;; replace
370 ;; uncomment to extend the above behaviour to query-replace
371 ;; (csetq replace-char-fold t)
372
373 ;; vc
374 (global-set-key (kbd "C-x v C-=") #'vc-ediff)
375
376 (with-eval-after-load 'vc-git
377 (csetq vc-git-print-log-follow t))
378
379 (csetq ediff-window-setup-function 'ediff-setup-windows-plain
380 ediff-split-window-function 'split-window-horizontally)
381 (with-eval-after-load 'ediff
382 (add-hook 'ediff-after-quit-hook-internal #'winner-undo))
383
384 ;; face-remap
385 (csetq
386 ;; gentler font resizing
387 text-scale-mode-step 1.05)
388
389 (run-with-idle-timer 0.4 nil #'require 'mwheel)
390 (csetq mouse-wheel-scroll-amount '(1 ((shift) . 1)) ; one line at a time
391 mouse-wheel-progressive-speed nil ; don't accelerate scrolling
392 mouse-wheel-follow-mouse t) ; scroll window under mouse
393
394 (run-with-idle-timer 0.4 nil #'require 'pixel-scroll)
395 (with-eval-after-load 'pixel-scroll
396 (pixel-scroll-mode 1))
397
398 ;; epg-config
399 (csetq
400 epg-gpg-program (executable-find "gpg")
401 ;; ask for GPG passphrase in minibuffer
402 ;; this will fail if gpg>=2.1 is not available
403 epg-pinentry-mode 'loopback)
404
405 ;; (require 'pinentry)
406 ;; workaround for systemd-based distros:
407 ;; (setq pinentry--socket-dir server-socket-dir)
408 ;; (pinentry-start)
409
410 ;; auth-source
411 (csetq
412 auth-sources '("~/.authinfo.gpg")
413 authinfo-hidden (regexp-opt '("password" "client-secret" "token")))
414
415 \f
416 ;;; General key bindings
417
418 (global-set-key (kbd "C-a") #'b/move-indentation-or-beginning-of-line)
419 (global-set-key (kbd "C-c a i") #'ielm)
420 (global-set-key (kbd "C-c d") #'b/duplicate-line-or-region)
421 (global-set-key (kbd "C-S-j") #'b/join-line-top)
422 (global-set-key (kbd "C-c x") #'execute-extended-command)
423
424 ;; evaling and macro-expanding
425 (global-set-key (kbd "C-c e b") #'eval-buffer)
426 (global-set-key (kbd "C-c e e") #'eval-last-sexp)
427 (global-set-key (kbd "C-c e p") #'pp-macroexpand-last-sexp)
428 (global-set-key (kbd "C-c e r") #'eval-region)
429
430 ;; emacs things
431 (global-set-key (kbd "C-c e i") #'emacs-init-time)
432 (global-set-key (kbd "C-c e u") #'emacs-uptime)
433 (global-set-key (kbd "C-c e v") #'emacs-version)
434
435 ;; finding
436 (global-set-key (kbd "C-c f .") #'find-file)
437 (global-set-key (kbd "C-c f d") #'find-name-dired)
438 (global-set-key (kbd "C-c f l") #'find-library)
439
440 ;; frames
441 (global-set-key (kbd "C-c F m") #'make-frame-command)
442 (global-set-key (kbd "C-c F d") #'delete-frame)
443
444 ;; help/describe
445 (global-set-key (kbd "C-S-h C") #'describe-char)
446 (global-set-key (kbd "C-S-h F") #'describe-face)
447
448 ;; (global-set-key (kbd "C-x k") #'b/kill-current-buffer)
449 ;; (global-set-key (kbd "C-x K") #'kill-buffer)
450 ;; (global-set-key (kbd "C-x s") #'save-buffer)
451 ;; (global-set-key (kbd "C-x S") #'save-some-buffers)
452
453 (define-key emacs-lisp-mode-map (kbd "<C-return>") #'b/add-elisp-section)
454
455 (when (display-graphic-p)
456 (global-unset-key (kbd "C-z")))
457
458 \f
459 ;;; Essential packages
460
461 ;; (require 'bandali-exwm)
462
463 (require 'bandali-org)
464
465 (require 'bandali-theme)
466
467 ;; magit, *the* right way to do git
468 (comment
469 (csetq transient-history-file (b/var "transient/history.el")
470 transient-levels-file (b/etc "transient/levels.el")
471 transient-values-file (b/etc "transient/values.el"))
472 (with-eval-after-load 'magit
473 (declare-function magit-add-section-hook "magit-section"
474 (hook function &optional at append local))
475 (magit-add-section-hook 'magit-status-sections-hook
476 'magit-insert-modules
477 'magit-insert-stashes
478 'append)
479 ;; (magit-add-section-hook 'magit-status-sections-hook
480 ;; 'magit-insert-ignored-files
481 ;; 'magit-insert-untracked-files
482 ;; 'append)
483 (declare-function magit-display-buffer-fullframe-status-v1
484 "magit-mode" (buffer))
485 (csetq
486 magit-diff-refine-hunk t
487 magit-repository-directories '(("~/.emacs.d/" . 0)
488 ("~/src/git/" . 2))
489 ;; magit-completing-read-function 'magit-ido-completing-read
490 magit-display-buffer-function
491 #'magit-display-buffer-fullframe-status-v1)
492 (nconc magit-section-initial-visibility-alist
493 '(([unpulled status] . show)
494 ([unpushed status] . show)))
495 (custom-set-faces '(magit-diff-file-heading ((t (:weight normal)))))
496
497 (with-eval-after-load 'magit-extras
498 (csetq
499 magit-pop-revision-stack-format
500 (pcase-let ((`(,pt ,_eob ,index-regexp)
501 (default-value 'magit-pop-revision-stack-format)))
502 `(,pt "[%N: %h]: %ci\n %s
503 https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=%H"
504 ,index-regexp)))))
505 ;; global key bindings
506 (global-set-key (kbd "C-x g") #'magit-status)
507 (global-set-key (kbd "C-c g b") #'magit-blame-addition)
508 (global-set-key (kbd "C-c g l") #'magit-log-buffer-file)
509 (global-set-key (kbd "C-c g y") #'magit-pop-revision-stack)
510 )
511
512 ;; recently opened files
513 (csetq recentf-max-saved-items 2000
514 recentf-save-file (b/var "recentf-save.el"))
515 (run-with-idle-timer 0.2 nil #'require 'recentf)
516 (with-eval-after-load 'recentf
517 ;; (add-to-list 'recentf-keep #'file-remote-p)
518 (recentf-mode))
519
520 ;; needed for history for counsel
521 (csetq amx-save-file (b/var "amx-save.el"))
522 (add-to-list 'load-path (b/lisp "s"))
523 (add-to-list 'load-path (b/lisp "amx"))
524 (run-with-idle-timer 0.3 nil #'require 'amx)
525 (with-eval-after-load 'amx
526 (amx-mode))
527
528 (require 'bandali-ivy)
529
530 (require 'bandali-eshell)
531
532 (require 'bandali-ibuffer)
533
534 ;; outline
535 ;; (with-eval-after-load 'outline
536 ;; (when (featurep 'which-key)
537 ;; (which-key-add-key-based-replacements
538 ;; "C-c @" "outline"
539 ;; "s-O" "outline"))
540 ;; (define-key outline-minor-mode-map (kbd "<s-tab>")
541 ;; #'outline-toggle-children)
542 ;; (define-key outline-minor-mode-map (kbd "M-p")
543 ;; #'outline-previous-visible-heading)
544 ;; (define-key outline-minor-mode-map (kbd "M-n")
545 ;; #'outline-next-visible-heading)
546 ;; (defvar b/outline-prefix-map)
547 ;; (define-prefix-command 'b/outline-prefix-map)
548 ;; (define-key outline-minor-mode-map (kbd "s-O")
549 ;; 'b/outline-prefix-map)
550 ;; (define-key b/outline-prefix-map (kbd "TAB")
551 ;; #'outline-toggle-children)
552 ;; (define-key b/outline-prefix-map (kbd "a")
553 ;; #'outline-hide-body)
554 ;; (define-key b/outline-prefix-map (kbd "H")
555 ;; #'outline-hide-body)
556 ;; (define-key b/outline-prefix-map (kbd "S")
557 ;; #'outline-show-all)
558 ;; (define-key b/outline-prefix-map (kbd "h")
559 ;; #'outline-hide-subtree)
560 ;; (define-key b/outline-prefix-map (kbd "s")
561 ;; #'outline-show-subtree))
562 ;; (add-hook 'prog-mode-hook #'outline-minor-mode)
563
564 (require 'bandali-dired)
565
566 (with-eval-after-load 'help
567 (temp-buffer-resize-mode)
568 (csetq help-window-select t))
569
570 (with-eval-after-load 'help-mode
571 ;; local key bindings
572 (define-key help-mode-map (kbd "p") #'backward-button)
573 (define-key help-mode-map (kbd "n") #'forward-button))
574
575 (with-eval-after-load 'tramp
576 (csetq tramp-auto-save-directory (b/var "tramp/auto-save/")
577 tramp-persistency-file-name (b/var "tramp/persistency.el"))
578 (add-to-list 'tramp-default-proxies-alist '(nil "\\`root\\'" "/ssh:%h:"))
579 (add-to-list 'tramp-default-proxies-alist '("localhost" nil nil))
580 (add-to-list 'tramp-default-proxies-alist
581 (list (regexp-quote (system-name)) nil nil)))
582
583 (with-eval-after-load 'doc-view
584 (define-key doc-view-mode-map (kbd "M-RET") #'image-previous-line))
585
586 (csetq shr-max-width 80)
587
588 ;; Email (with Gnus, message, and EBDB)
589 (require 'bandali-gnus)
590 (with-eval-after-load 'sendmail
591 (csetq sendmail-program (executable-find "msmtp")
592 ;; message-sendmail-extra-arguments '("-v" "-d")
593 mail-specify-envelope-from t
594 mail-envelope-from 'header))
595 (require 'bandali-message)
596 (require 'bandali-ebdb)
597
598 ;; IRC (with ERC)
599 (require 'bandali-erc)
600
601 (add-to-list 'load-path (b/lisp "scpaste"))
602 (with-eval-after-load 'scpaste
603 (csetq scpaste-http-destination "https://p.bndl.org"
604 scpaste-scp-destination "p:~"))
605 (autoload 'scpaste "scpaste" nil t)
606 (autoload 'scpaste-region "scpaste" nil t)
607 (global-set-key (kbd "C-c a p p") #'scpaste)
608 (global-set-key (kbd "C-c a p r") #'scpaste-region)
609
610 \f
611 ;;; Editing
612
613 ;; display Lisp objects at point in the echo area
614 (when (version< "25" emacs-version)
615 (with-eval-after-load 'eldoc
616 (csetq eldoc-minor-mode-string " eldoc")
617 (global-eldoc-mode)))
618
619 ;; highlight matching parens
620 (require 'paren)
621 (show-paren-mode)
622
623 ;; (require 'elec-pair)
624 ;; (electric-pair-mode)
625
626 (csetq
627 ;; Save what I copy into clipboard from other applications into Emacs'
628 ;; kill-ring, which would allow me to still be able to easily access
629 ;; it in case I kill (cut or copy) something else inside Emacs before
630 ;; yanking (pasting) what I'd originally intended to.
631 save-interprogram-paste-before-kill t)
632 (with-eval-after-load 'simple
633 (column-number-mode 1))
634
635 ;; save minibuffer history
636 (require 'savehist)
637 (csetq savehist-file (b/var "savehist.el"))
638 (savehist-mode)
639 (add-to-list 'savehist-additional-variables 'kill-ring)
640
641 ;; automatically save place in files
642 (when (version< "25" emacs-version)
643 (csetq save-place-file (b/var "save-place.el"))
644 (save-place-mode))
645
646 (defun indicate-buffer-boundaries-left ()
647 (csetq indicate-buffer-boundaries 'left))
648 (with-eval-after-load 'prog-mode
649 (global-prettify-symbols-mode))
650 (add-hook 'prog-mode-hook #'indicate-buffer-boundaries-left)
651
652 (define-key text-mode-map (kbd "C-<return>") #'b/insert-asterism)
653 (add-hook 'text-mode-hook #'indicate-buffer-boundaries-left)
654 (add-hook 'text-mode-hook #'flyspell-mode)
655
656 (add-to-list 'auto-mode-alist '("\\.*rc$" . conf-mode))
657
658 (add-to-list 'auto-mode-alist '("\\.bashrc$" . sh-mode))
659
660 (with-eval-after-load 'flyspell
661 (csetq flyspell-mode-line-string " fly"))
662
663 ;; flycheck
664 ;; (run-with-idle-timer 0.6 nil #'require 'flycheck)
665 ;; (with-eval-after-load 'flycheck
666 ;; (csetq
667 ;; ;; Use the load-path from running Emacs when checking elisp files
668 ;; flycheck-emacs-lisp-load-path 'inherit
669 ;; ;; Only flycheck when I actually save the buffer
670 ;; flycheck-check-syntax-automatically '(mode-enabled save)
671 ;; flycheck-mode-line-prefix "flyc"))
672 ;; (define-key flycheck-mode-map (kbd "M-P") #'flycheck-previous-error)
673 ;; (define-key flycheck-mode-map (kbd "M-N") #'flycheck-next-error)
674 ;; (add-hook 'prog-mode-hook #'flycheck-mode)
675
676 ;; ispell
677 ;; http://endlessparentheses.com/ispell-and-apostrophes.html
678 ;; (run-with-idle-timer 0.6 nil #'require 'ispell)
679 ;; (with-eval-after-load 'ispell
680 ;; ;; ’ can be part of a word
681 ;; (csetq ispell-local-dictionary-alist
682 ;; `((nil "[[:alpha:]]" "[^[:alpha:]]"
683 ;; "['\x2019]" nil ("-B") nil utf-8))
684 ;; ispell-program-name (executable-find "hunspell"))
685 ;; ;; don't send ’ to the subprocess
686 ;; (defun endless/replace-apostrophe (args)
687 ;; (cons (replace-regexp-in-string
688 ;; "’" "'" (car args))
689 ;; (cdr args)))
690 ;; (advice-add #'ispell-send-string :filter-args
691 ;; #'endless/replace-apostrophe)
692 ;; ;; convert ' back to ’ from the subprocess
693 ;; (defun endless/replace-quote (args)
694 ;; (if (not (derived-mode-p 'org-mode))
695 ;; args
696 ;; (cons (replace-regexp-in-string
697 ;; "'" "’" (car args))
698 ;; (cdr args))))
699 ;; (advice-add #'ispell-parse-output :filter-args
700 ;; #'endless/replace-quote))
701
702 ;; abbrev
703 (csetq abbrev-file-name (b/etc "abbrev.el"))
704 (add-hook 'text-mode-hook #'abbrev-mode)
705
706 \f
707 ;;; Programming modes
708
709 (with-eval-after-load 'lisp-mode
710 (defun indent-spaces-mode ()
711 (setq indent-tabs-mode nil))
712 (add-hook 'lisp-interaction-mode-hook #'indent-spaces-mode))
713
714 ;; alloy
715 (add-to-list 'load-path (b/lisp "alloy-mode"))
716 (autoload 'alloy-mode "alloy-mode" nil t)
717 (with-eval-after-load 'alloy-mode
718 (csetq alloy-basic-offset 2)
719 ;; (defun b/alloy-simple-indent (start end)
720 ;; (interactive "r")
721 ;; ;; (if (region-active-p)
722 ;; ;; (indent-rigidly start end alloy-basic-offset)
723 ;; ;; (if (bolp)
724 ;; ;; (indent-rigidly (line-beginning-position)
725 ;; ;; (line-end-position)
726 ;; ;; alloy-basic-offset)))
727 ;; (indent-to (+ (current-column) alloy-basic-offset)))
728 ;; local key bindings
729 (define-key alloy-mode-map (kbd "RET") #'electric-newline-and-maybe-indent)
730 ;; (define-key alloy-mode-map (kbd "TAB") #'b/alloy-simple-indent)
731 (define-key alloy-mode-map (kbd "TAB") #'indent-for-tab-command))
732 (add-to-list 'auto-mode-alist '("\\.\\(als\\|dsh\\)\\'" . alloy-mode))
733 (add-hook 'alloy-mode-hook (lambda nil (setq-local indent-tabs-mode nil)))
734
735 ;; lean
736 ;; (eval-when-compile (defvar lean-mode-map))
737 ;; (run-with-idle-timer 0.4 nil #'require 'lean-mode)
738 ;; (with-eval-after-load 'lean-mode
739 ;; (require 'lean-input)
740 ;; (csetq default-input-method "Lean"
741 ;; lean-input-tweak-all '(lean-input-compose
742 ;; (lean-input-prepend "/")
743 ;; (lean-input-nonempty))
744 ;; lean-input-user-translations '(("/" "/")))
745 ;; (lean-input-setup)
746 ;; ;; local key bindings
747 ;; (define-key lean-mode-map (kbd "S-SPC") #'company-complete))
748
749 (with-eval-after-load 'sgml-mode
750 (csetq sgml-basic-offset 0))
751
752 (with-eval-after-load 'css-mode
753 (csetq css-indent-offset 2))
754
755 ;; po-mode
756 ;; (add-hook 'po-mode-hook (lambda nil (run-with-timer 0.1 nil 'View-exit)))
757
758 ;; auctex
759 ;; (csetq font-latex-fontify-sectioning 'color)
760
761 (with-eval-after-load 'tex-mode
762 (cl-delete-if
763 (lambda (p) (string-match "^---?" (car p)))
764 tex--prettify-symbols-alist))
765 (add-hook 'tex-mode-hook #'auto-fill-mode)
766 (add-hook 'tex-mode-hook #'flyspell-mode)
767
768 \f
769 ;;; Emacs enhancements & auxiliary packages
770
771 (with-eval-after-load 'man
772 (csetq Man-width 80))
773
774 (defun b/*scratch* ()
775 "Switch to `*scratch*' buffer, creating it if it does not exist."
776 (interactive)
777 (switch-to-buffer
778 (or (get-buffer "*scratch*")
779 (with-current-buffer (get-buffer-create "*scratch*")
780 (set-buffer-major-mode (current-buffer))
781 (current-buffer)))))
782 (global-set-key (kbd "C-c s") #'b/*scratch*)
783
784 ;; ,----
785 ;; | make pretty boxed quotes like this
786 ;; `----
787 (add-to-list 'load-path (b/lisp "boxquote"))
788 (run-with-idle-timer 0.6 nil #'require 'boxquote)
789 (with-eval-after-load 'boxquote
790 (defvar b/boxquote-prefix-map)
791 (define-prefix-command 'b/boxquote-prefix-map)
792 (global-set-key (kbd "C-c q") 'b/boxquote-prefix-map)
793 (define-key b/boxquote-prefix-map (kbd "b") #'boxquote-buffer)
794 (define-key b/boxquote-prefix-map (kbd "B") #'boxquote-insert-buffer)
795 (define-key b/boxquote-prefix-map (kbd "d") #'boxquote-defun)
796 (define-key b/boxquote-prefix-map (kbd "F") #'boxquote-insert-file)
797 (define-key b/boxquote-prefix-map (kbd "hf") #'boxquote-describe-function)
798 (define-key b/boxquote-prefix-map (kbd "hk") #'boxquote-describe-key)
799 (define-key b/boxquote-prefix-map (kbd "hv") #'boxquote-describe-variable)
800 (define-key b/boxquote-prefix-map (kbd "hw") #'boxquote-where-is)
801 (define-key b/boxquote-prefix-map (kbd "k") #'boxquote-kill)
802 (define-key b/boxquote-prefix-map (kbd "p") #'boxquote-paragraph)
803 (define-key b/boxquote-prefix-map (kbd "q") #'boxquote-boxquote)
804 (define-key b/boxquote-prefix-map (kbd "r") #'boxquote-region)
805 (define-key b/boxquote-prefix-map (kbd "s") #'boxquote-shell-command)
806 (define-key b/boxquote-prefix-map (kbd "t") #'boxquote-text)
807 (define-key b/boxquote-prefix-map (kbd "T") #'boxquote-title)
808 (define-key b/boxquote-prefix-map (kbd "u") #'boxquote-unbox)
809 (define-key b/boxquote-prefix-map (kbd "U") #'boxquote-unbox-region)
810 (define-key b/boxquote-prefix-map (kbd "y") #'boxquote-yank)
811 (define-key b/boxquote-prefix-map (kbd "M-q") #'boxquote-fill-paragraph)
812 (define-key b/boxquote-prefix-map (kbd "M-w") #'boxquote-kill-ring-save))
813
814 (add-to-list 'load-path (b/lisp "hl-todo"))
815 (run-with-idle-timer 0.5 nil #'require 'hl-todo)
816 (with-eval-after-load 'hl-todo
817 ;; highlight TODOs in buffers
818 (global-hl-todo-mode))
819
820 (add-to-list 'load-path (b/lisp "page-break-lines"))
821 (run-with-idle-timer 0.5 nil #'require 'page-break-lines)
822 (with-eval-after-load 'page-break-lines
823 (csetq page-break-lines-max-width fill-column)
824 (global-page-break-lines-mode))
825
826 ;; expand-region
827 (global-set-key (kbd "C-=") #'er/expand-region)
828
829 (run-with-idle-timer 0.6 nil #'require 'yasnippet)
830 (with-eval-after-load 'yasnippet
831 (declare-function yas-reload-all
832 "yasnippet" (&optional no-jit interactive))
833 (declare-function yas-maybe-expand-abbrev-key-filter
834 "yasnippet" (cmd))
835
836 (defconst yas-verbosity-cur yas-verbosity)
837 (setq yas-verbosity 2)
838 (csetq yas-snippet-dirs `(,(b/etc "yasnippet/snippets")))
839 (add-to-list 'yas-snippet-dirs "~/src/git/guix/etc/snippets" t)
840 (yas-reload-all)
841 (setq yas-verbosity yas-verbosity-cur)
842
843 (defun b/yas-maybe-expand-abbrev-key-filter (cmd)
844 (when (and (yas-maybe-expand-abbrev-key-filter cmd)
845 (not (bound-and-true-p git-commit-mode)))
846 cmd))
847 (defconst b/yas-maybe-expand
848 '(menu-item "" yas-expand
849 :filter b/yas-maybe-expand-abbrev-key-filter))
850 (define-key yas-minor-mode-map (kbd "SPC") b/yas-maybe-expand)
851
852 (yas-global-mode))
853
854 ;; debbugs
855 (global-set-key (kbd "C-c D d") #'debbugs-gnu)
856 (global-set-key (kbd "C-c D b") #'debbugs-gnu-bugs)
857 (global-set-key (kbd "C-c D e") ; bug-gnu-emacs
858 (lambda ()
859 (interactive)
860 (setq debbugs-gnu-current-suppress t)
861 (debbugs-gnu debbugs-gnu-default-severities
862 '("emacs"))))
863 (global-set-key (kbd "C-c D g") ; bug-gnuzilla
864 (lambda ()
865 (interactive)
866 (setq debbugs-gnu-current-suppress t)
867 (debbugs-gnu debbugs-gnu-default-severities
868 '("gnuzilla"))))
869 (global-set-key (kbd "C-c D G b") ; bug-guix
870 (lambda ()
871 (interactive)
872 (setq debbugs-gnu-current-suppress t)
873 (debbugs-gnu debbugs-gnu-default-severities
874 '("guix"))))
875 (global-set-key (kbd "C-c D G p") ; guix-patches
876 (lambda ()
877 (interactive)
878 (setq debbugs-gnu-current-suppress t)
879 (debbugs-gnu debbugs-gnu-default-severities
880 '("guix-patches"))))
881
882 ;; url and url-cache
883 (csetq
884 url-configuration-directory (b/var "url/configuration/")
885 url-cache-directory (b/var "url/cache/"))
886
887 ;; eww
888 (csetq eww-download-directory (file-name-as-directory
889 (getenv "XDG_DOWNLOAD_DIR")))
890 (global-set-key (kbd "C-c a e w") #'eww)
891
892 ;; ;; org-ref
893 ;; (csetq
894 ;; reftex-default-bibliography '("~/usr/org/references.bib")
895 ;; org-ref-default-bibliography '("~/usr/org/references.bib")
896 ;; org-ref-bibliography-notes "~/usr/org/notes.org"
897 ;; org-ref-pdf-directory "~/usr/org/bibtex-pdfs/")
898
899 ;; fill-column-indicator ?
900
901 ;; window
902 (csetq split-width-threshold 150)
903 (global-set-key (kbd "C-c w s l")
904 (lambda ()
905 (interactive)
906 (split-window-right)
907 (other-window 1)))
908 (global-set-key (kbd "C-c w s j")
909 (lambda ()
910 (interactive)
911 (split-window-below)
912 (other-window 1)))
913 (global-set-key (kbd "C-c w q") #'quit-window)
914
915 (run-with-idle-timer 0.6 nil #'require 'windmove)
916 (global-set-key (kbd "C-c w h") #'windmove-left)
917 (global-set-key (kbd "C-c w j") #'windmove-down)
918 (global-set-key (kbd "C-c w k") #'windmove-up)
919 (global-set-key (kbd "C-c w l") #'windmove-right)
920 (global-set-key (kbd "C-c w H") #'windmove-swap-states-left)
921 (global-set-key (kbd "C-c w J") #'windmove-swap-states-down)
922 (global-set-key (kbd "C-c w K") #'windmove-swap-states-up)
923 (global-set-key (kbd "C-c w L") #'windmove-swap-states-right)
924
925 ;; pass
926 ;; (global-set-key (kbd "C-c a p") #'pass)
927 ;; (add-hook 'pass-mode-hook #'View-exit)
928
929 ;; reftex
930 ;; uncomment to disable reftex-cite's default choice of previous word
931 ;; (with-eval-after-load 'reftex
932 ;; (require 'reftex-cite)
933 ;; (defun reftex-get-bibkey-default ()
934 ;; "If the cursor is in a citation macro, return the word before the macro."
935 ;; (let* ((macro (reftex-what-macro 1)))
936 ;; (save-excursion
937 ;; (when (and macro (string-match "cite" (car macro)))
938 ;; (goto-char (cdr macro)))
939 ;; (reftex-this-word)))))
940 (add-hook 'latex-mode-hook #'reftex-mode)
941
942 ;; dmenu
943 ;; (csetq
944 ;; dmenu-prompt-string "run: "
945 ;; dmenu-save-file (b/var "dmenu-items"))
946
947 ;; eosd ?
948
949 ;; delight
950 (run-with-idle-timer 0.5 nil #'require 'delight)
951 (with-eval-after-load 'delight
952 (delight 'auto-fill-function " f" "simple")
953 (delight 'abbrev-mode "" "abbrev")
954 (delight 'page-break-lines-mode "" "page-break-lines")
955 (delight 'ivy-mode "" "ivy")
956 (delight 'counsel-mode "" "counsel")
957 (delight 'mml-mode " mml" "mml")
958 (delight 'yas-minor-mode "" "yasnippet"))
959
960 \f
961 ;;; Post initialization
962
963 (message "Loading %s...done (%.3fs)" user-init-file
964 (float-time (time-subtract (current-time)
965 b/before-user-init-time)))
966
967 ;;; init.el ends here