summary refs log tree commit diff stats
path: root/early-init.el
blob: 9f8e7f09594cae8804664f79a14c77b2f1873389 (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
;;; early-init.el -*- lexical-binding: t; coding: utf-8-unix -*-
;; Author: Case Duckworth <acdw@acdw.net>
;; Created: Sometime during Covid-19, 2020
;; Keywords: configuration
;; URL: https://tildegit.org/acdw/emacs

;; This file is NOT part of GNU Emacs.

;;; License:
;; Everyone is permitted to do whatever with this software, without
;; limitation.  This software comes without any warranty whatsoever,
;; but with two pieces of advice:
;; - Don't hurt yourself.
;; - Make good choices.

;;; Comentary:
;; Starting with Emacs 27.1, `early-init' is sourced before `package'
;; or any frames.  So those are the settings I run in this file.

;;; Code:

;;; Add `acdw.el'
(push (expand-file-name "lisp/" user-emacs-directory)
      load-path)
(require 'acdw)
(require 'acdw-frame)
;;; Speed up init
;; see doom-emacs, et al.

(setq acdw/orig-file-name-handler-alist file-name-handler-alist
      file-name-handler-alist nil
      gc-cons-percentage 0.8
      gc-cons-threshold most-positive-fixnum
      load-prefer-newer noninteractive)

(add-hook 'after-init-hook
          (defun after-init@reset ()
            "Reset `file-name-handler-alist' and garbage collection."
            (setq gc-cons-percentage 0.1
                  gc-cons-threshold (* 800 1024 1024))
            (dolist (handler file-name-handler-alist)
              (add-to-list 'acdw/orig-file-name-handler-alist handler))
            (setq file-name-handler-alist acdw/orig-file-name-handler-alist)))

;;; Frame settings
(setq default-frame-alist               ; Remove most UI
      `((tool-bar-lines . 0)            ; No tool bar
        (menu-bar-lines . 0)            ; No menu bar
        (vertical-scroll-bars)          ; No scroll bars
        (horizontal-scroll-bars)        ; ... at all
        (width . 84)                    ; A /little/ wider than
                                        ; `fill-column' (set later)
        (height . 30)
        (left-fringe . 8)               ; Width of fringes
        (right-fringe . 8)              ; (8 is default)
        (font . ,(acdw/system
                  (:home "DejaVu Sans Mono 10")
                  (:work "Consolas 12")
                  (:other "monospace 10"))))
      frame-inhibit-implied-resize t	; Don't resize randomly
      frame-resize-pixelwise t          ; Resize by pixels, not chars
      inhibit-x-resources t             ; Don't load ~/.Xresources
      )

(add-hook 'after-init-hook
          (defun after-init@disable-ui-modes ()
            "Disable UI modes after init.
I already disable them from the `default-frame-alist' for speed
and anti-flickering reasons, but this function allows running,
say, `tool-bar-mode' once to toggle the tool bar back on."
            (dolist (mode ;; each mode is of the form (MODE . FRAME-ALIST-VAR)
                     '((tool-bar-mode . tool-bar-lines)
                       (menu-bar-mode . menu-bar-lines)
                       (scroll-bar-mode . vertical-scroll-bars)
                       (horizontal-scroll-bar-mode . horizontal-scroll-bars)))
              (let ((setting (alist-get (cdr mode) default-frame-alist)))
                (when (or (not setting)
                          (zerop setting))
                  (funcall (car mode) -1))))))

(add-hook 'after-make-frame-functions
          (defun after-make-frame@setup (&rest args)
            (ignore args)
            (let ((monospace-faces
                   (acdw/system
                    (:work '((:font "Consolas" :height 110)
                             (:font "DejaVu Sans Mono" :height 110)
                             (:font "monospace" :height 110)))
                    (_ '((:font "DejaVu Sans Mono" :height 100)
                         (:font "Consolas" :height 100)
                         (:font "monospace" :height 100))))))
              (acdw/set-first-face-attribute 'default monospace-faces)
              (acdw/set-first-face-attribute 'fixed-pitch monospace-faces)
              (acdw/set-first-face-attribute 'variable-pitch
                                             '((:font "Inter" :height 120)
                                               (:font "sans-serif"
                                                      :height 100))))
            (acdw/set-emoji-fonts "Segoe UI Emoji"
                                  "Noto Color Emoji"
                                  "Apple Color Emoji"
                                  "Symbola")
            (acdw/set-fringes '((left-curly-arrow [#b01100000
                                                   #b00110000
                                                   #b00011000
                                                   #b00001100]
                                                  4 8 center)
                                (right-curly-arrow [#b00000011
                                                    #b00000110
                                                    #b00001100
                                                    #b00011000]
                                                   4 8 center)
                                (left-arrow [#b01100000
                                             #b01010000]
                                            2 8 (top t))
                                (right-arrow [#b00000011
                                              #b00000101]
                                             2 8 (top t))))
            (setq indicate-empty-lines nil
                  indicate-buffer-boundaries '((top . right)
                                               (bottom . right)))
            (custom-set-faces '(fringe ((t (:foreground "dim gray")))))))
(add-hook 'server-after-make-frame-hook #'after-make-frame@setup)

;; I have this here because ... the first frame doesn't ? run ? the hook ???
(add-function :after after-focus-change-function
              (defun after-focus-change@first-frame-setup (&rest args)
                (ignore args)
                (after-make-frame@setup)
                (remove-function after-focus-change-function
                                 #'after-focus-change@first-frame-setup)))

;;; Bootstrap package manager (`straight.el')

;; 1. Update `exec-path'.
(dolist (path (list (expand-file-name "bin" user-emacs-directory)
                    (expand-file-name "~/bin")
                    (expand-file-name "~/.local/bin")
                    (expand-file-name "~/usr/bin")
                    (expand-file-name "~/cmd")
                    (expand-file-name "~/mingw64/bin")
                    (expand-file-name "~/clisp-2.49"))
              exec-path)
  (when (file-exists-p path)
    (add-to-list 'exec-path path :append)))

;; 1.5. Update $PATH to reflect changes.
(setenv "PATH" (mapconcat #'identity exec-path path-separator))

;; 2. Set `package' and `straight' variables.
(setq package-enable-at-startup nil
      package-quickstart nil
      straight-host-usernames '((github . "duckwork")
                                (gitlab . "acdw"))
      straight-base-dir (acdw/dir)
      straight-check-for-modifications '(check-on-save find-when-checking))

;; 3. Bootstrap `straight'.
(defvar bootstrap-version)
(let ((bootstrap-file
       (expand-file-name
        "straight/repos/straight.el/bootstrap.el"
        straight-base-dir))
      (bootstrap-version 5))
  (unless (file-exists-p bootstrap-file)
    (with-current-buffer
        (url-retrieve-synchronously
         (concat "https://raw.githubusercontent.com/"
                 "raxod502/straight.el/develop/install.el")
         'silent 'inhibit-cookies)
      (goto-char (point-max))
      (eval-print-last-sexp)))
  (load bootstrap-file nil 'nomessage))

;; Helper package, good commands here.
(require 'straight-x)

;; Appendix.  Get rid of a dumb alias.
;; straight-ಠ_ಠ-mode really slows down all minibuffer completion functions.
;; Since it's a (rarely-used, even) alias anyway, I just define it back to nil.
;; By the way, the alias is `straight-package-neutering-mode'.
(defalias 'straight-ಠ_ಠ-mode nil)

;;; Message startup time for profiling
;; This just redefines the Emacs function
;; `display-startup-echo-area-message', so no hooks needed.
(defun display-startup-echo-area-message ()
  "Show Emacs's startup time in the message buffer.  For profiling."
  (message "Emacs ready in %s with %d garbage collections."
           (format "%.2f seconds"
                   (float-time (time-subtract after-init-time
                                              before-init-time)))
           gcs-done))

;;; `setup'
(straight-use-package '(setup :host nil :repo "https://git.sr.ht/~pkal/setup"))
(require 'setup)

(setup setup
  ;; Install a package using `straight-use-package'
  (setup-define :straight
    (lambda (recipe)
      `(straight-use-package ',recipe))
    :documentation
    "Install RECIPE with `straight-use-package'.
This macro can be used as HEAD, and will replace itself with the
first RECIPE's package."
    :repeatable t
    :shorthand (lambda (sexp)
                 (let ((recipe (cadr sexp)))
                   (if (consp recipe)
                       (car recipe)
                     recipe))))
  ;; Install a package with straight, but only under a condition
  (setup-define :straight-if
    (lambda (recipe condition)
      `(if ,condition
           (straight-use-package ',recipe)
         ,(setup-quit)))
    :documentation
    "Install RECIPE with `straight-use-package' when CONDITION is met.
If CONDITION is false, stop evaluating the body.  This macro can
be used as HEAD, and will replace itself with the RECIPE's
package.  This macro is not repeatable."
    :repeatable nil
    :shorthand (lambda (sexp)
                 (let ((recipe (cadr sexp)))
                   (if (consp recipe) (car recipe) recipe)))))

;;; `no-littering'
(setup (:straight no-littering)
  (:option no-littering-etc-directory (acdw/dir)
           no-littering-var-directory (acdw/dir))
  (require 'no-littering))

;;; early-init.el ends here