about summary refs log tree commit diff stats
path: root/basics.el
blob: 68cdc2a8bbc85749497153648fe1205827c3e021 (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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
;;; basics.el --- Super basic Emacs settings -*- lexical-binding: t -*-

;;; Commentary:

;; These are the settings that I literally cannot live without.  Basic
;; settings, built-in packages, that kind of stuff.  Everything else
;; goes in init.el.

;;; Code:

(push (locate-user-emacs-file "lisp/") load-path)
(require 'acdw)

;;; Directories

(defdir etc/ (locate-user-emacs-file "etc/")
  "Where various Emacs files are placed."
  :makedir)

(defdir sync/ "~/Sync/"
  "My Syncthing directory."
  :makedir)

(defdir private/ (sync/ "emacs/private/")
  "Private files and stuff."
  :makedir)

(use-package no-littering
  :ensure t :demand t
  :preface
  (setq-default no-littering-etc-directory etc/
                no-littering-var-directory etc/))


;;; Settings

;; Async
(setq-default async-shell-command-buffer 'new-buffer
              async-shell-command-display-buffer nil)

;; Scrolling
(setq-default auto-hscroll-mode t
              auto-window-vscroll nil
              fast-but-imprecise-scrolling t
              hscroll-margin 1
              hscroll-step 1
              scroll-conservatively 25
              scroll-margin 0
              scroll-preserve-screen-position 1
              scroll-step 1)
(scroll-bar-mode -1)
(horizontal-scroll-bar-mode -1)

;; Cursor
(setq-default cursor-in-non-selected-windows 'hollow
              cursor-type 'bar
              blink-cursor-blinks 1
              blink-cursor-interval 0.25
              blink-cursor-delay 0.25)
(blink-cursor-mode)

;; Mouse
(setq-default mouse-drag-copy-region t
              mouse-wheel-progressive-speed nil
              mouse-yank-at-point t)

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

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

;; Minibuffer
(setq-default 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))
(file-name-shadow-mode)
(minibuffer-electric-default-mode)

(define-minor-mode truncate-lines-local-mode
  "Truncate lines locally in a buffer."
  :lighter " ..."
  :group 'display
  (setq-local truncate-lines truncate-lines-local-mode))

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

(require 'savehist)
(setq-default 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
(setq-default 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
(setq-default echo-keystrokes 0.01
              ring-bell-function #'ignore)

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

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

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

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

(menu-bar-mode -1)
(tool-bar-mode -1)
(tooltip-mode -1)

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

(setq-default 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)
(electric-pair-mode)

;; Encodings
(set-language-environment "UTF-8")
(setq-default 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)))

;; Abbrev
(setq-default abbrev-file-name (sync/ "abbrev.el")
              save-abbrevs 'silently)

;; Files
(setq-default 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)

(setq-default 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
             `(".*" ,(etc/ "auto-save/" t) t))
(auto-save-visited-mode)

(setq-default backup-by-copying t
              version-control t
              kept-new-versions 8
              kept-old-versions 8
              delete-old-versions t)

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

(require 'saveplace)
(setq-default ;; save-place-file (etc/ "places.el")
 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)
(setq-default whitespace-style
              '(face trailing tabs tab-mark))
(global-whitespace-mode)
(add-hook 'before-save-hook
          (defun delete-trailing-whitespace-except-current-line ()
            (save-excursion
              (delete-trailing-whitespace (point-min)
                                          (line-beginning-position))
              (delete-trailing-whitespace (line-end-position)
                                          (point-max)))))

;; Native compilation
(setq-default native-comp-async-report-warnings-errors 'silent
              native-comp-deferred-compilation t
              native-compile-target-directory (etc/ "eln" t))
(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))

;; Custom file
(setq-default custom-file (private/ "custom.el"))
(define-advice package--save-selected-packages
    (:around (orig &rest args) no-custom)
  "Don't save `package-selected-packages' to `custom-file'."
  (let ((custom-file (expand-file-name "custom.el"
                                       temporary-file-directory)))
    (apply orig args)))

;; Goto Address
(if (fboundp 'global-goto-address-mode)
    (global-goto-address-mode)
  (add-hook 'after-change-major-mode-hook #'goto-address-mode))

;; Winner
(winner-mode)


;;; Keybindings

(defun other-window|switch-buffer (arg)
  "Call `other-window' or `switch-buffer' depending on windows.
When called with prefix ARG, unconditionally switch buffer."
  (interactive "P")
  (if (or arg (one-window-p))
      (switch-to-buffer (other-buffer) nil t)
    (other-window 1)))

(defun delete-window|bury-buffer ()
  "Delete the current window, or bury the current buffer.
If the current window is the only window, bury the buffer."
  (interactive)
  (condition-case e
      (delete-window)
    (t (bury-buffer))))

(defun +cycle-spacing (&optional n)
  ;; `cycle-spacing' is wildly different in 29.1 over 28.
  "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."
  (interactive "*p")
  (cycle-spacing (- n)))

(global-set-key [remap eval-expression] #'pp-eval-expression)
(global-set-key (kbd "M-o") #'other-window|switch-buffer)
(global-set-key (kbd "C-x 0") #'delete-window|bury-buffer)
(global-set-key (kbd "M-SPC") #'+cycle-spacing)
(global-set-key (kbd "C-x C-k") #'kill-this-buffer)
(global-set-key (kbd "C-/") #'undo-only)
(global-set-key (kbd "C-?") #'undo-redo)

(global-set-key [f10] #'tmm-menubar)
(advice-add 'tmm-add-prompt :after 'minibuffer-hide-completions)

(when (fboundp '+lisp-comment-or-uncomment-sexp)
  (define-key lisp-mode-map (kbd "C-M-;") #'+lisp-comment-or-uncomment-sexp)
  (define-key emacs-lisp-mode-map (kbd "C-M-;")
              #'+lisp-comment-or-uncomment-sexp)
  (with-eval-after-load 'scheme
    (define-key scheme-mode-map (kbd "C-M-;")
                #'+lisp-comment-or-uncomment-sexp)))

(define-key emacs-lisp-mode-map (kbd "C-c C-c")
            #'eval-defun)
(define-key emacs-lisp-mode-map (kbd "C-c C-k")
            #'elisp-eval-region-or-buffer)
(define-key lisp-interaction-mode-map (kbd "C-c C-c")
            #'eval-defun)
(define-key lisp-interaction-mode-map (kbd "C-c C-k")
            #'elisp-eval-region-or-buffer)
(define-advice eval-region (:around (orig start end &rest args) pulse)
  (apply orig start end args)
  (pulse-momentary-highlight-region start end))

(global-set-key (kbd "C-x C-b") #'ibuffer)


;;; Hooks

(add-hook 'after-save-hook #'executable-make-buffer-file-executable-if-script-p)

(add-hook 'find-file-not-found-functions
          (defun create-missing-directories ()
            "Automatically create missing directories."
            (let ((target-dir (file-name-directory buffer-file-name)))
              (unless (file-exists-p target-dir)
                (make-directory target-dir :parents)))))

(add-hook 'find-file-hook
          (defun vc-remote-off ()
            "Turn VC off when remote."
            (when (file-remote-p (buffer-file-name))
              (setq-local vc-handled-backends nil))))


;;; Advice

(define-advice switch-to-buffer (:after (&rest _) normal-mode)
  "Automatically determine the mode for non-file buffers."
  (when-let ((_ (and (eq major-mode 'fundamental-mode)))
             (buffer-file-name (buffer-name)))
    (normal-mode)))

(define-advice canonically-space-region
    (:around (orig &rest args) double-space-sentences)
  "Always double-space sentences canonically."
  (let ((sentence-end-double-space t))
    (apply orig args)))


;;; Packages

(use-package _acdw
  :load-path private/)

(use-package custom-allowed
  :load-path "/home/case/src/emacs/custom-allowed/"
  :config
  (add-to-list 'custom-allowed-variables 'safe-local-variable-values)
  (add-to-list 'custom-allowed-variables 'ispell-buffer-session-localwords)
  (add-to-list 'custom-allowed-variables 'warning-suppress-types)
  (add-to-list 'custom-allowed-variables 'calendar-latitude)
  (add-to-list 'custom-allowed-variables 'calendar-longitude)
  (add-to-list 'custom-allowed-variables 'user-full-name)
  (add-to-list 'custom-allowed-variables 'user-mail-address)
  :hook
  (after-init-hook . custom-allowed-load-custom-file))

(use-package sophomore
  :load-path "/home/case/src/emacs/sophomore/"
  :config
  (sophomore-enable-all)
  (sophomore-disable 'view-hello-file
                     'describe-gnu-project
                     'suspend-frame)
  (sophomore-mode))

(use-package compat
  ;; This shouldn't be necessary, but sadly I believe that it is.
  :ensure t)

(use-package vertico
  :ensure t :demand t
  :config
  (setq vertico-cycle t)
  (add-hook 'vertico-mode-hook
            (defun vertico-mode@fix-completions ()
              (setopt completion-in-region-function
                      (if vertico-mode
                          #'consult-completion-in-region
                        #'completion--in-region))))
  (vertico-mode))

(use-package vertico-directory
  :after vertico
  :bind (:map vertico-map
              ("C-DEL" . vertico-directory-delete-word))
  :hook (rfn-shadow-update-overlay-hook . vertico-directory-tidy))

(use-package vertico-mouse
  :after vertico
  :config (vertico-mouse-mode))

;; Example configuration for Consult
(use-package consult
  :ensure t
  ;; Replace bindings. Lazily loaded due by `use-package'.
  :bind (;; C-c bindings (mode-specific-map)
         ("C-c h" . consult-history)
         ("C-c m" . consult-mode-command)
         ("C-c k" . consult-kmacro)
         ;; C-x bindings (ctl-x-map)
         ("C-x M-:" . consult-complex-command)
         ("C-x b" . consult-buffer)
         ("C-x 4 b" . consult-buffer-other-window)
         ("C-x 5 b" . consult-buffer-other-frame)
         ("C-x r b" . consult-bookmark)
         ("C-x p b" . consult-project-buffer)
         ;; Custom M-# bindings for fast register access
         ("M-#" . consult-register-load)
         ("M-'" . consult-register-store)
         ("C-M-#" . consult-register)
         ;; Other custom bindings
         ("M-y" . consult-yank-pop)
         ;; M-g bindings (goto-map)
         ("M-g e" . consult-compile-error)
         ("M-g f" . consult-flymake)
         ("M-g g" . consult-goto-line)
         ("M-g M-g" . consult-goto-line)
         ("M-g o" . consult-outline)
         ("M-g m" . consult-mark)
         ("M-g k" . consult-global-mark)
         ("M-g i" . consult-imenu)
         ("M-g I" . consult-imenu-multi)
         ;; M-s bindings (search-map)
         ("M-s d" . consult-find)
         ("M-s D" . consult-locate)
         ("M-s g" . consult-grep)
         ("M-s G" . consult-git-grep)
         ("M-s r" . consult-ripgrep)
         ("M-s l" . consult-line)
         ("M-s L" . consult-line-multi)
         ("M-s k" . consult-keep-lines)
         ("M-s u" . consult-focus-lines)
         ;; Isearch integration
         ("M-s e" . consult-isearch-history)
         :map isearch-mode-map
         ("M-e" . consult-isearch-history)
         ("M-s e" . consult-isearch-history)
         ("M-s l" . consult-line)
         ("M-s L" . consult-line-multi)
         ;; Minibuffer history
         :map minibuffer-local-map
         ("M-s" . consult-history)
         ("M-r" . consult-history))

  ;; Enable automatic preview at point in the *Completions* buffer. This is
  ;; relevant when you use the default completion UI.
  :hook (completion-list-mode . consult-preview-at-point-mode)

  ;; The :init configuration is always executed (Not lazy)
  :init

  ;; Optionally configure the register formatting. This improves the register
  ;; preview for `consult-register', `consult-register-load',
  ;; `consult-register-store' and the Emacs built-ins.
  (setq register-preview-delay 0.5
        register-preview-function #'consult-register-format)

  ;; Optionally tweak the register preview window.
  ;; This adds thin lines, sorting and hides the mode line of the window.
  (advice-add #'register-preview :override #'consult-register-window)

  (define-advice completing-read-multiple (:filter-args (args) indicator)
    (cons (format "[CRM%s] %s"
                  (replace-regexp-in-string
                   "\\`\\[.*?]\\*\\|\\[.*?]\\*\\'" ""
                   crm-separator)
                  (car args))
          (cdr args)))

  ;; Use Consult to select xref locations with preview
  (setq xref-show-xrefs-function #'consult-xref
        xref-show-definitions-function #'consult-xref)

  (setq completion-in-region-function #'consult-completion-in-region)

  ;; Configure other variables and modes in the :config section,
  ;; after lazily loading the package.
  :config

  ;; Optionally configure preview. The default value
  ;; is 'any, such that any key triggers the preview.
  ;; (setq consult-preview-key 'any)
  ;; (setq consult-preview-key (kbd "M-."))
  ;; (setq consult-preview-key (list (kbd "<S-down>") (kbd "<S-up>")))
  ;; For some commands and buffer sources it is useful to configure the
  ;; :preview-key on a per-command basis using the `consult-customize' macro.
  (consult-customize
   consult-theme :preview-key '(:debounce 0.2 any)
   consult-ripgrep consult-git-grep consult-grep
   consult-bookmark consult-recent-file consult-xref
   consult--source-bookmark consult--source-file-register
   consult--source-recent-file consult--source-project-recent-file
   ;; :preview-key (kbd "M-.")
   :preview-key '(:debounce 0.4 any))

  ;; Optionally configure the narrowing key.
  ;; Both < and C-+ work reasonably well.
  (setq consult-narrow-key "<") ;; (kbd "C-+")

  ;; Optionally make narrowing help available in the minibuffer.
  ;; You may want to use `embark-prefix-help-command' or which-key instead.
  (define-key consult-narrow-map (vconcat consult-narrow-key "?")
              #'consult-narrow-help))

(use-package orderless
  :ensure t :demand t
  :init
  (setopt completion-styles '(substring orderless basic)
          completion-category-defaults nil
          completion-category-overrides
          '((file (styles basic partial-completion orderless)))))

(use-package marginalia
  :ensure t :demand t
  :config
  (marginalia-mode))

(use-package embark
  :ensure t
  :bind
  (("C-." . embark-act)
   ("M-." . embark-dwim)
   ("C-h B" . embark-bindings))
  :init
  (setopt prefix-help-command #'embark-prefix-help-command)
  :config
  (add-to-list 'display-buffer-alist
               '("\\`\\*Embark Collect \\(Live\\|Completions\\)\\*"
                 nil
                 (window-parameters (mode-line-format . none)))))

(use-package embark-consult
  :ensure t
  :hook
  (embark-collect-mode . consult-preview-at-point-mode))

(use-package undo-fu
  :ensure t
  :init
  (setq undo-limit 67108864)            ; 64mb.
  (setq undo-strong-limit 100663296)    ; 96mb.
  (setq undo-outer-limit 1006632960)    ; 960mb.
  :bind (("C-/" . undo-fu-only-undo)
         ("C-?" . undo-fu-only-redo)))

(use-package undo-fu-session
  :ensure t
  :config
  (setopt undo-fu-session-compression (cond
                                       ((executable-find "gunzip") 'gz)
                                       ((executable-find "bzip2") 'bz2))
          undo-fu-session-incompatible-files '("/COMMIT_EDITMSG\\'"
                                               "/git-rebase-todo\\'"))
  (global-undo-fu-session-mode))

(use-package crux
  :ensure t :demand t
  :bind (("C-x 4 t" . crux-transpose-windows))
  :config
  (crux-with-region-or-buffer indent-region)
  (crux-with-region-or-buffer tabify)
  (crux-with-region-or-buffer untabify)
  (crux-reopen-as-root-mode))

(use-package pixel-scroll
  :demand t
  :bind (([right-margin wheel-down] . pixel-scroll-precision)
         ([right-margin double-wheel-down] . pixel-scroll-precision)
         ([right-margin triple-wheel-down] . pixel-scroll-precision)
         ([right-margin wheel-up] . pixel-scroll-precision)
         ([right-margin double-wheel-up] . pixel-scroll-precision)
         ([right-margin triple-wheel-up] . pixel-scroll-precision))
  :config
  (pixel-scroll-precision-mode))

;;; basics.el ends here