summary refs log tree commit diff stats
path: root/lisp/+emacs.el
blob: 7453913c4bbc7b882e8b4d7e2bb34a34da06af3c (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
;;; +emacs.el --- measured defaults for Emacs -*- lexical-binding: t -*-

;;; Commentary:

;; I find myself copy-pasting a lot of "boilerplate" type code when
;; bankrupting my Emacs config and starting afresh.  Instead of doing
;; that, I'm putting it here, where it'll be easier to include in my
;; config.

;; Of course, some might say I could just ... stop bankrupting my
;; Emacs.  But like, why would I want to?

;; Other notable packages include
;; - https://git.sr.ht/~technomancy/better-defaults/
;; - https://github.com/susam/emfy

;;; Code:

(require 'early-init (locate-user-emacs-file "early-init.el"))

(defun +set-major-mode-from-buffer-name (&optional buf)
  "Set the major mode for BUF from the buffer's name.
Do this only if the buffer is not visiting a file."
  (unless buffer-file-name
    (let ((buffer-file-name (buffer-name buf)))
      (set-auto-mode))))

;;; General settings

(setq-default
 apropos-do-all t
 async-shell-command-buffer 'new-buffer
 async-shell-command-display-buffer nil
 auto-hscroll-mode 'current-line
 auto-revert-verbose nil
 auto-save-file-name-transforms `((".*" ,(.etc "auto-save/" t) t))
 auto-save-interval 60
 auto-save-list-file-prefix (.etc "auto-save/.saves-" t)
 auto-save-timeout 60
 auto-save-visited-interval 60
 auto-window-vscroll nil
 backup-by-copying t
 backup-directory-alist `((".*" . ,(.etc "backup/" t)))
 blink-cursor-blinks 1
 completion-category-defaults nil
 completion-category-overrides '((file (styles . (partial-completion))))
 completion-ignore-case t
 completion-styles '(substring partial-completion)
 create-lockfiles nil
 cursor-in-non-selected-windows 'hollow
 cursor-type 'bar
 custom-file (.etc "custom.el")
 delete-old-versions t
 echo-keystrokes 0.1
 ediff-window-setup-function 'ediff-setup-windows-plain
 eldoc-echo-area-use-multiline-p nil
 eldoc-idle-delay 0.1
 enable-recursive-minibuffers t
 executable-prefix-env t
 fast-but-imprecise-scrolling t
 file-name-shadow-properties '(invisible t intangible t)
 fill-column 80
 frame-resize-pixelwise t
 global-auto-revert-non-file-buffers t
 global-mark-ring-max 100
 hscroll-margin 1
 hscroll-step 1
 imenu-auto-rescan t
 indent-tabs-mode nil
 indicate-empty-lines nil
 indicate-buffer-boundaries 'left
 inhibit-startup-screen t
 initial-buffer-choice t
 kill-do-not-save-duplicates t
 kill-read-only-ok t
 kill-ring-max 500
 kmacro-ring-max 20
 load-prefer-newer t
 major-mode '+set-major-mode-from-buffer-name
 mark-ring-max 50
 minibuffer-eldef-shorten-default t
 minibuffer-prompt-properties (list 'read-only t
                                    'cursor-intangible t
                                    'face 'minibuffer-prompt)
 mode-require-final-newline 'visit-save
 mouse-drag-copy-region t
 mouse-wheel-progressive-speed nil
 mouse-yank-at-point t
 native-comp-async-report-warnings-errors 'silent
 read-answer-short t
 read-buffer-completion-ignore-case t
 read-extended-command-predicate (when (fboundp
                                        'command-completion-default-include-p)
                                   'command-completion-default-include-p)
 recenter-positions '(top middle bottom)
 regexp-search-ring-max 100
 regexp-search-ring-max 200
 save-interprogram-paste-before-kill t
 scroll-conservatively 101
 scroll-down-aggressively 0.01
 scroll-margin 1
 scroll-preserve-screen-position 1
 scroll-step 1
 scroll-up-aggressively 0.01
 search-ring-max 200
 search-ring-max 200
 sentence-end-double-space t
 set-mark-command-repeat-pop t
 show-paren-delay 0
 show-paren-style 'mixed
 show-paren-when-point-in-periphery t
 show-paren-when-point-inside-paren t
 ;;show-trailing-whitespace t
 tab-bar-show 1
 tab-width 4
 tramp-backup-directory-alist backup-directory-alist
 undo-limit 100000000                   ; 10 MB
 use-dialog-box nil
 use-file-dialog nil
 use-short-answers t
 vc-follow-symlinks t
 vc-make-backup-files t
 version-control t
 view-read-only t
 visible-bell nil
 window-resize-pixelwise t
 x-select-enable-clipboard t
 x-select-enable-primary t
 yank-pop-change-selection t
 )

;; Programming language offsets.
;; Set these after the initial block so I can use `tab-width'
(setq-default
 c-basic-offset tab-width)

;; Emacs 28 ships with an option, `use-short-answers', that makes this form
;; obsolete, but I still use 27 at work.
(when (version< emacs-version "28")
  (fset 'yes-or-no-p 'y-or-n-p))

;;; Encodings

(setq-default locale-coding-system 'utf-8-unix
              coding-system-for-read 'utf-8-unix
              coding-system-for-write 'utf-8-unix
              buffer-file-coding-system 'utf-8-unix
              default-process-coding-system '(utf-8-unix . utf-8-unix)
              x-select-request-type '(UTF8_STRING
                                      COMPOUND_TEXT
                                      TEXT
                                      STRING))

(set-charset-priority 'unicode)
(set-language-environment "UTF-8")
(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)))

;;; Modes

(dolist (enable-mode '(global-auto-revert-mode
                       blink-cursor-mode
                       electric-pair-mode
                       show-paren-mode
                       global-so-long-mode
                       minibuffer-depth-indicate-mode
                       file-name-shadow-mode
                       minibuffer-electric-default-mode
                       delete-selection-mode
                       column-number-mode))
  (when (fboundp enable-mode)
    (funcall enable-mode +1)))

(dolist (disable-mode '(tooltip-mode
                        tool-bar-mode
                        menu-bar-mode
                        scroll-bar-mode
                        horizontal-scroll-bar-mode))
  (when (fboundp disable-mode)
    (funcall disable-mode -1)))

;;; Hooks

(add-hook 'after-save-hook 'executable-make-buffer-file-executable-if-script-p)
(add-hook 'minibuffer-setup-hook 'cursor-intangible-mode)

;;; Better-default functions ...

(defun +cycle-spacing (&optional n preserve-nl-back mode)
  "Negate N argument on `cycle-spacing'.
That is, with a positive N, deletes newlines as well, leaving -N
spaces.  If N is negative, it will not delete newlines and leave
N spaces.  See docstring of `cycle-spacing' for the meaning of
PRESERVE-NL-BACK and MODE."
  (interactive "*p")
  (cycle-spacing (- n) preserve-nl-back mode))

(defun +save-buffers-quit (&optional arg)
  "Silently save each buffer, then kill the current connection.
If the current frame has no client, kill Emacs itself using
`save-buffers-kill-emacs' after confirming with the user.

With prefix ARG, silently save all file-visiting buffers, then
kill without asking."
  (interactive "P")
  (save-some-buffers t)
  (if (and (not (frame-parameter nil 'client))
           (and (not arg)))
      (when (yes-or-no-p "Sure you want to quit? ")
        (save-buffers-kill-emacs))
    (delete-frame nil :force)))

;; ... and advice

;; Indent the region after a yank.
(defun +yank@indent (&rest _)
  "Indent the current region."
  (indent-region (min (point) (mark)) (max (point) (mark))))
(advice-add #'yank :after #'+yank@indent)
(advice-add #'yank-pop :after #'+yank@indent)

;;; Bindings

;; I need to place these bindings under `+key-mode-map' so that they aren't
;; shadowed by other maps.  There might be a better way to do this.
(require '+key)

(dolist (binding '(("C-x C-c" . +save-buffers-quit)
                   ("M-SPC" . +cycle-spacing)
                   ("M-/" . hippie-expand)
                   ("M-=" . count-words)
                   ("C-x C-b" . ibuffer)
                   ("C-s" . isearch-forward-regexp)
                   ("C-r" . isearch-backward-regexp)
                   ("C-M-s" . isearch-forward)
                   ("C-M-r" . isearch-backward)))
  (define-key +key-mode-map (kbd (car binding)) (cdr binding)))

;;; Required libraries

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

(when (require 'goto-addr)
  (if (fboundp 'global-goto-address-mode)
      (global-goto-address-mode +1)
    (add-hook 'after-change-major-mode-hook 'goto-address-mode)))

(when (require 'recentf nil :noerror)
  (setq-default recentf-save-file (.etc "recentf.el")
                recentf-max-menu-items 100
                recentf-max-saved-items nil
                recentf-auto-cleanup 'mode)
  (add-to-list 'recentf-exclude .etc)
  (recentf-mode +1))

(when (require 'repeat nil :noerror)
  (setq-default repeat-exit-key "g"
                repeat-exit-timeout 5)
  (when (fboundp 'repeat-mode)
    ;; `repeat-mode' is defined in repeat.el, which is an older library.
    (repeat-mode +1)))

(when (require 'savehist nil :noerror)
  (setq-default history-length t
                history-delete-duplicates t
                history-autosave-interval 60
                savehist-file (.etc "savehist.el"))
  (dolist (var '(extended-command-history
                 global-mark-ring
                 kill-ring
                 regexp-search-ring
                 search-ring
                 mark-ring))
    (add-to-list 'savehist-additional-variables var))
  (savehist-mode +1))

(when (require 'saveplace nil :noerror)
  (setq-default save-place-file (.etc "places.el")
                save-place-forget-unreadable-files (eq system-type 'gnu/linux))
  (save-place-mode +1))

;; (when (require 'tramp)
;;   ;; thanks Irreal! https://irreal.org/blog/?p=895
;;   (add-to-list 'tramp-default-proxies-alist
;;                '(nil "\\`root\\'" "/ssh:%h:"))
;;   (add-to-list 'tramp-default-proxies-alist
;;                '((regexp-quote (system-name)) nil nil)))

(provide '+emacs)
;;; +emacs.el ends here