summary refs log tree commit diff stats
path: root/init.el
blob: 46480423f861eb63ba3da313e63b764cb58aaf50 (plain)
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
463
464
465
466
467
468
469
470
471
472
473
474
;;; Emacs init.el  -*- lexical-binding: t; -*-
;; by Case Duckworth <acdw@acdw.net>
;; Bankruptcy 10: "Annoyance"

;;; Commentary:

;; This is my Emacs configuration.  There are many like it but this
;; one is mine.
;;
;; For the tenth time!

;;; Packages

(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)

(dolist (pkg `(consult
               marginalia
               visual-fill-column
               adaptive-wrap
               geiser
               ,(when (executable-find "csi") 'geiser-chicken)
               avy
               zzz-to-char
               hungry-delete
               undohist
               jinx))
  (when (and pkg (not (package-installed-p pkg)))
    (unless (ignore-errors (package-install pkg))
      (package-refresh-contents)
      (package-install pkg))))

(load (locate-user-emacs-file "definitions"))
(load (locate-user-emacs-file "packages"))
(load (locate-user-emacs-file "private"))

(setopt custom-file (locate-user-emacs-file "custom.el"))
(load custom-file :noerror)

;;; General keybinding changes

(keymap-global-set "M-o" #'other-window-or-switch-buffer)

(keymap-global-set "M-SPC" #'cycle-spacing@)

(keymap-global-set "M-u" #'universal-argument)
(keymap-set universal-argument-map "M-u" #'universal-argument-more)

;;; Theme

(if (daemonp)
    (add-hook 'server-after-make-frame-hook #'first-frame@set-fonts)
  (run-with-idle-timer 1 nil #'first-frame@set-fonts))

(tool-bar-mode -1)

(load-theme 'modus-vivendi :no-confirm :no-enable)
(load-theme 'modus-operandi :no-confirm)

(add-hook 'text-mode-hook #'visual-line-mode)

;;; Mode line

(defvar mode-line-position
  '(""
    (:eval (if line-number-mode "%3l" ""))
    (:eval (if column-number-mode
               (if column-number-indicator-zero-based
                   "/%2c"
                 "/%2C")
             ""))
    " (" (-3 "%p") ") "))

(setopt mode-line-format
        '(("%e" mode-line-front-space
           mode-line-client
           mode-line-modified
           mode-line-remote " "
           mode-line-buffer-identification
           (vc-mode vc-mode)
           " "
           (mode-line-position mode-line-position)
           mode-line-modes
           mode-line-misc-info
           mode-line-end-spaces)))

;; Remove modes from mode-line
(dolist (minor-mode '(frowny-mode
                      whitespace-mode
                      hungry-delete-mode))
  (setf (alist-get minor-mode minor-mode-alist) (list ""))
  (add-hook (intern (format "%s-hook" minor-mode))
            (lambda ()
              (setf (alist-get minor-mode minor-mode-alist) (list "")))))

;;; Completion & minibuffer

(setopt icomplete-in-buffer t
        icomplete-tidy-shadowed-file-names t)
(fido-vertical-mode)

(setopt completion-auto-help (not icomplete-mode)
        completion-auto-select 'second-tab
        completions-header-format nil
        completions-max-height 12
        completions-format 'one-column
        completion-styles '(basic partial-completion flex)
        completion-ignore-case t
        read-buffer-completion-ignore-case t
        read-file-name-completion-ignore-case t
        completions-detailed t
        enable-recursive-minibuffers t
        file-name-shadow-properties '(invisible t intangible t)
        minibuffer-eldef-shorten-default t
        minibuffer-prompt-properties '( read-only t
                                        cursor-intangible t
                                        face minibuffer-prompt)
        window-resize-pixelwise t
        frame-resize-pixelwise t)

(add-hook 'completion-list-mode-hook #'truncate-lines-mode)
(add-hook 'minibuffer-setup-hook #'truncate-lines-mode)

;; Up/down when completing in the minibuffer
(define-key minibuffer-local-map (kbd "C-p") #'minibuffer-previous-completion)
(define-key minibuffer-local-map (kbd "C-n") #'minibuffer-next-completion)

;; Up/down when competing in a normal buffer
(define-key completion-in-region-mode-map (kbd "C-p") #'minibuffer-previous-completion)
(define-key completion-in-region-mode-map (kbd "C-n") #'minibuffer-next-completion)

(setopt completions-sort #'renz/sort-multi-category)

(setopt tab-always-indent 'complete)

(file-name-shadow-mode)
(minibuffer-electric-default-mode)

(scroll-bar-mode -1)
(menu-bar-mode -1)

(add-hook 'prog-mode-hook #'indent-tabs-mode-maybe)

(setopt electric-pair-skip-whitespace 'chomp)
(electric-pair-mode)

(setopt sh-basic-offset tab-width)

(keymap-set emacs-lisp-mode-map "C-c C-c" #'eval-defun)
(keymap-set emacs-lisp-mode-map "C-c C-k" #'eval-buffer)
(keymap-set lisp-interaction-mode-map "C-c C-c" #'eval-defun)
(keymap-set lisp-interaction-mode-map "C-c C-k" #'eval-buffer)

(advice-add 'indent-region :around #'call-with-region-or-buffer)
(advice-add 'tabify :around #'call-with-region-or-buffer)
(advice-add 'untabify :around #'call-with-region-or-buffer)

(with-eval-after-load 'scheme
  (keymap-unset scheme-mode-map "M-o" t)
  ;; Comparse "keywords" --- CHICKEN (http://wiki.call-cc.org/eggref/5/comparse)
  (put 'sequence* 'scheme-indent-function 1)
  (put 'satisfies 'scheme-indent-function 1)
  (add-hook 'scheme-mode-hook #'geiser-mode))
(with-eval-after-load 'geiser-mode
  (keymap-set geiser-mode-map "C-c C-k" #'geiser-eval-buffer-and-go)
  (keymap-unset geiser-mode-map "C-." t))

(setopt visual-fill-column-center-text t
        visual-fill-column-width (+ fill-column 2))
(advice-add 'text-scale-adjust :after #'visual-fill-column-adjust)
(add-hook 'visual-line-mode-hook #'visual-fill-column-mode)
(add-hook 'visual-line-mode-hook #'adaptive-wrap-prefix-mode)

(setopt major-mode
        (lambda () ; guess major mode from buffer name
          (unless buffer-file-name
            (let ((buffer-file-name (buffer-name)))
              (set-auto-mode)))))

;; Dialogs
(unless (boundp 'use-short-answers)
  (fset 'yes-or-no-p 'y-or-n-p))

(setopt read-answer-short t
        use-dialog-box nil
        use-file-dialog nil
        use-short-answers t)

(require 'savehist)
(setopt history-length 1024
        history-delete-duplicates t
        ;; savehist-file (etc/ "savehist.el")
        savehist-save-minibuffer-history t
        savehist-autosave-interval 30)
(savehist-mode)

;; Killing and yanking
(setopt kill-do-not-save-duplicates t
        kill-read-only-ok t
        ;; XXX: This setting causes an error message the first time it's
        ;; called: "Selection owner couldn't convert: TIMESTAMP".          I have
        ;; absolutely no idea why I get this error, but it's generated in
        ;; `x_get_foreign_selection'.  I also can't inhibit the message or
        ;; do anything else with it, so for now, I'll just live with the
        ;; message.
        save-interprogram-paste-before-kill t
        yank-pop-change-selection t)
(delete-selection-mode)

;; Notifying the user
(setopt echo-keystrokes 0.01
        ring-bell-function #'ignore)

;; Point and mark
(setopt set-mark-command-repeat-pop t)

;; The system
(setopt read-process-output-max (* 10 1024 1024))

;; Startup
(setopt inhibit-startup-screen t
        initial-buffer-choice t
        initial-scratch-message nil)

(define-advice startup-echo-area-message (:override ())
  (if (get-buffer "*Warnings*")
      ";_;"
    "^_^"))

;; Text editing
(setopt fill-column 80
        sentence-end-double-space nil
        tab-width 8
        tab-always-indent 'complete)
(global-so-long-mode)

(setopt show-paren-delay 0.01
        show-paren-style 'parenthesis
        show-paren-when-point-in-periphery t
        show-paren-when-point-inside-paren t)
(show-paren-mode)


;; Encodings
(set-language-environment "UTF-8")
(setopt buffer-file-coding-system 'utf-8-unix
        coding-system-for-read 'utf-8-unix
        coding-system-for-write 'utf-8-unix
        default-process-coding-system '(utf-8-unix . utf-8-unix)
        locale-coding-system 'utf-8-unix)
(set-charset-priority 'unicode)
(prefer-coding-system 'utf-8-unix)
(set-default-coding-systems 'utf-8-unix)
(set-terminal-coding-system 'utf-8-unix)
(set-keyboard-coding-system 'utf-8-unix)
(pcase system-type
  ((or 'ms-dos 'windows-nt)
   (set-clipboard-coding-system 'utf-16-le)
   (set-selection-coding-system 'utf-16-le))
  (_
   (set-selection-coding-system 'utf-8)
   (set-clipboard-coding-system 'utf-8)))


;; Files
(setopt auto-revert-verbose nil
        global-auto-revert-non-file-buffers t
        create-lockfiles nil
        find-file-visit-truename t
        mode-require-final-newline t
        view-read-only t
        save-silently t)
(global-auto-revert-mode)

(setopt auto-save-default nil
        auto-save-interval 1
        auto-save-no-message t
        auto-save-timeout 1
        auto-save-visited-interval 1
        remote-file-name-inhibit-auto-save-visited t)
(add-to-list 'auto-save-file-name-transforms
             `(".*" ,(locate-user-emacs-file "auto-save/") t))
(auto-save-visited-mode)

(setopt backup-by-copying t
        version-control t
        kept-new-versions 8
        kept-old-versions 8
        delete-old-versions t)
(setq-default backup-directory-alist
              `(("^/dev/shm" . nil)
                ("^/tmp" . nil)
                (,(getenv "XDG_RUNTIME_DIR") . nil)
                ("." . ,(locate-user-emacs-file "backup"))))

(require 'recentf)
(setopt
 recentf-max-menu-items 500
 recentf-max-saved-items nil ; Save the whole list
 recentf-auto-cleanup 'mode
 recentf-case-fold-search t)
;; (add-to-list 'recentf-exclude etc/)
(add-to-list 'recentf-exclude "-autoloads.el\\'")
(add-hook 'buffer-list-update-hook #'recentf-track-opened-file)
(add-hook 'after-save-hook #'recentf-save-list)
(recentf-mode)

(require 'saveplace)
(setopt
 save-place-forget-unreadable-files (eq system-type
                                        'gnu/linux))
(save-place-mode)

(require 'uniquify)
(setq uniquify-after-kill-buffer-p t
      uniquify-buffer-name-style 'forward
      uniquify-ignore-buffers-re "^\\*"
      uniquify-separator path-separator)

(setq-local vc-follow-symlinks t
            vc-make-backup-files t)

;; Whitespace
(require 'whitespace)
(setopt whitespace-style
        '(face trailing tabs tab-mark))
(global-whitespace-mode)
(add-hook 'before-save-hook #'delete-trailing-whitespace-except-current-line)

;; Native compilation
(setopt native-comp-async-report-warnings-errors 'silent
        native-comp-deferred-compilation t
        native-compile-target-directory
        (locate-user-emacs-file "eln"))
(when (boundp 'native-comp-eln-load-path)
  (add-to-list 'native-comp-eln-load-path native-compile-target-directory))
(when (fboundp 'startup-redirect-eln-cache)
  (startup-redirect-eln-cache native-compile-target-directory))

(global-goto-address-mode)

;; Winner
(winner-mode)

;;; Hooks
(add-hook 'after-save-hook #'executable-make-buffer-file-executable-if-script-p)
(add-hook 'find-file-not-found-functions #'create-missing-directories)
(add-hook 'find-file-hook #'vc-remote-off)
(add-hook 'dired-mode-hook #'hl-line-mode)
(add-hook 'org-agenda-mode-hook #'hl-line-mode)

;;; Tab bar

(defun tab-bar-end-space ()
  `((end menu-item " " ignore)))

(setopt tab-bar-show t)
(add-to-list 'tab-bar-format 'tab-bar-format-align-right :append)
(add-to-list 'tab-bar-format 'tab-bar-format-global :append)
(add-to-list 'tab-bar-format 'tab-bar-end-space :append)
(tab-bar-mode)

;;; Org mode

(keymap-global-set "C-c a" #'org-agenda)
(setopt org-clock-clocked-in-display 'frame-title
        org-clock-frame-title-format
        '("%b" " - " (t org-mode-line-string)))

;;; Spelling

(defun list-of-strings-p (x)
  "Is X a list of strings?"
  (and x
       (listp x)
       (cl-every #'stringp x)))

(put 'ispell-local-words 'safe-local-variable
     'list-of-strings-p)

(add-hook 'text-mode-hook #'jinx-mode)
(with-eval-after-load 'jinx
  (keymap-set jinx-mode-map "M-$" #'jinx-correct)
  (keymap-set jinx-mode-map "C-M-$" #'jinx-languages))

;;; Copy rich text to the keyboard

;; Thanks to Oleh Krehel:
;; https://emacs.stackexchange.com/questions/54292/copy-results-of-org-export-directly-to-clipboard
;; So.  Emacs can't do this itself because it doesn't support sending clipboard
;; or selection contents as text/html.  We have to use xclip instead.
;; (defun org-to-html-to-clipboard (&rest org-export-args)
;;   "Export current org buffer to HTML, then copy it to the clipboard.
;; ORG-EXPORT-ARGS are passed to `org-export-to-file'."
;;   (let ((f (make-temp-file "org-html-export")))
;;     (apply #'org-export-to-file 'html f org-export-args)
;;     (start-process "xclip" " *xclip*"
;;                    "xclip" "-verbose" "-i" f
;;                    "-t" "text/html" "-selection" "clipboard")
;;     (message "HTML pasted to clipboard.")))

;; Wayland version.. TODO: make it work for both
(defun org-to-html-to-clipboard (&rest org-export-args)
  "Export current org buffer to HTML, then copy it to the clipboard.
ORG-EXPORT-ARGS are passed to `org-export-to-file'."
  (let ((buf (generate-new-buffer "*org-html-clipboard*" t)))
    (apply #'org-export-to-buffer 'html buf org-export-args)
    (with-current-buffer buf
      (call-process-region (point-min) (point-max)
                           "wl-copy" nil nil nil
                           "-t" "text/html")
      (kill-buffer-and-window))
    (message "HTML copied to clipboard.")))

(defun org-subtree-to-html-to-clipboard ()
  "Export current subtree to HTML."
  (interactive)
  (org-to-html-to-clipboard nil :subtree))

(undohist-initialize)

(require 'hungry-delete)
(setopt hungry-delete-chars-to-skip " \t"
        hungry-delete-skip-regexp (format "[%s]" hungry-delete-chars-to-skip)
        hungry-delete-join-reluctantly nil)
(add-to-list 'hungry-delete-except-modes 'eshell-mode)
(add-to-list 'hungry-delete-except-modes 'nim-mode)
(add-to-list 'hungry-delete-except-modes 'python-mode)
(global-hungry-delete-mode)

(setopt avy-background t
        avy-keys (string-to-list "asdfghjklqwertyuiopzxcvbnm"))
(keymap-global-set "M-j" #'avy-goto-char-timer)
(keymap-set isearch-mode-map "M-j" #'avy-isearch)
(keymap-global-set "M-z" #'zzz-to-char)

(marginalia-mode)

(keymap-global-set "C-x b" #'consult-buffer)
(keymap-global-set "C-x 4 b" #'consult-buffer-other-window)
(keymap-global-set "C-x 5 b" #'consult-buffer-other-frame)
(keymap-global-set "C-x r b" #'consult-bookmark)
(keymap-global-set "M-y" #'consult-yank-pop)
(keymap-global-set "M-g g" #'consult-goto-line)
(keymap-global-set "M-g M-g" #'consult-goto-line)
(keymap-global-set "M-g o" #'consult-outline)
(keymap-global-set "M-g m" #'consult-mark)
(keymap-global-set "M-g i" #'consult-imenu)
(keymap-global-set "M-s d" #'consult-find)
(keymap-global-set "M-s D" #'consult-locate)
(keymap-global-set "M-s g" #'consult-grep)
(keymap-global-set "M-s G" #'consult-git-grep)
(keymap-global-set "M-s r" #'consult-ripgrep)
(keymap-global-set "M-s l" #'consult-line)
(keymap-global-set "M-s k" #'consult-keep-lines)
(keymap-global-set "M-s u" #'consult-focus-lines)

(keymap-global-set "M-s e" #'consult-isearch-history)
(keymap-set isearch-mode-map "M-e" #'consult-isearch-history)
(keymap-set isearch-mode-map "M-s e" #'consult-isearch-history)
(keymap-set isearch-mode-map "M-s l" #'consult-line)

(keymap-set minibuffer-local-map "M-n" #'consult-history)
(keymap-set minibuffer-local-map "M-p" #'consult-history)

(setopt completion-in-region-function #'consult-completion-in-region
        xref-show-xrefs-function #'consult-xref
        xref-show-definitions-function #'consult-xref)

(setopt initial-scratch-message ";;; Emacs!\n\n")

(keymap-global-set "C-x C-b" #'ibuffer)
(add-hook 'ibuffer-hook #'hl-line-mode)