about summary refs log tree commit diff stats
path: root/early-init.el
blob: dc91cbaf914d8590b90db65cb4330a02da9335eb (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
;;; early-init.el -*- lexical-binding: t; coding: utf-8 -*-
;; Copyright (C) 2020-2021 Case Duckworth
;;
;; 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:

;; Speed up init
(setq gc-cons-threshold most-positive-fixnum
      gc-cons-percentage 0.6
      comp-deferred-compilation nil)

(defconst gc-cons-basis (* 800 1024)
  "The basis value to which to return after a max jump.
800,000 (800 KB) is Emacs' default.")

(add-hook 'after-init-hook #'(lambda ()
			       (setq gc-cons-threshold gc-cons-basis
				     gc-cons-percentage 0.1)))

(defun hook--gc-cons-maximize ()
  "Set `gc-cons-threshold' to the highest possible.
  For memory-intensive features."
  (setq gc-cons-threshold most-positive-fixnum))

(defun hook--gc-cons-baseline ()
  "Return `gc-cons-threshold' to `gc-cons-basis'.
  For after memory intensive operations."
  (setq gc-cons-threshold gc-cons-basis))

(add-hook 'minibuffer-setup-hook #'hook--gc-cons-maximize)
(add-hook 'minibuffer-exit-hook #'hook--gc-cons-baseline)

;; From doom-emacs
(unless (daemonp)
  (defvar doom--initial-file-name-handler-alist file-name-handler-alist)
  (setq file-name-handler-alist nil)
  (defun hook--reset-file-handler-alist ()
    (dolist (handler file-name-handler-alist)
      (add-to-list 'doom--initial-file-name-handler-alist handler))
    (setq file-name-handler-alist doom--initial-file-name-handler-alist))
  (add-hook 'emacs-startup-hook #'hook--reset-file-handler-alist))

;; Where are we?
(defconst acdw/system (pcase system-type
			('gnu/linux :home)
			((or 'msdos 'windows-nt) :work)
			(_ :other)))

;; Frame initiation

;; Initialize frames with as little UI as possible.
(setq-default
 default-frame-alist			; The default look of frames
 `((tool-bar-lines . 0)			; Remove tool bar
   (menu-bar-lines . 0)			; Remove menu bar
   (vertical-scroll-bars)		; Remove vertical scroll bars
   (horizontal-scroll-bars)		; Remove horizontal scroll bars
   (width . 84)				; A /little/ wider than `fill-column'
   (height . 30)			; Text characters
   (left-fringe . 8)			; Width of fringes
   (right-fringe . 8)			; (8 is the default)
   (font . ,(pcase acdw/system		; Default font
	     (:home "Terminus 12")
	     (:work "Consolas 11")))
   )

 x-underline-at-descent-line t		; underline at the descent line

 scroll-margin 0			; how many lines to show at window edge
 scroll-conservatively 101		; just enough to bring text into view
 scroll-preserve-screen-position 1	; always keep screen position
 
 frame-title-format			; Titles for frames
 '((:eval (if (buffer-file-name)	; (cf. `mode-line-format')
	      (abbreviate-file-name (buffer-file-name))
	    "%b"))
   " "
   mode-line-client
   mode-line-modified
   " - GNU Emacs")
 )

;; Set the rest of the fonts after initiation
(defun hook--setup-fonts ()
  (pcase acdw/system
    (:home (set-face-attribute 'default nil
			       :family "Terminus"
			       :height 120)
	   (set-face-attribute 'fixed-pitch nil
			       :family "Terminus"
			       :height 1.0)
	   (set-face-attribute 'variable-pitch nil
			       :family "DejaVu Sans"
			       :height 1.0))
    (:work (set-face-attribute 'default nil
			       :family "Consolas"
			       :height 110)
	   (set-face-attribute 'fixed-pitch nil
			       :family "Consolas"
			       :height 1.0)
	   (set-face-attribute 'variable-pitch nil
			       :family "Cambria"
			       :height 1.0))))

(add-hook 'after-init-hook #'hook--setup-fonts)

;; In case I do want the UI elements later, I also disable the modes
;; -- otherwise I'd have to run the mode twice to actually show the
;; thing.

(defun hook--disable-ui-modes ()
  (dolist (mode '(tool-bar-mode
		  menu-bar-mode
		  scroll-bar-mode
		  horizontal-scroll-bar-mode))
    (funcall mode -1)))

;; I run it on the `after-init-hook' so it doesn't slow down init so much.
(add-hook 'after-init-hook #'hook--disable-ui-modes)

;; Customize the fringe
(setq-default
 indicate-empty-lines t		    ; show an indicator at the end of the buffer
 indicate-buffer-boundaries 'right  ; show buffer boundaries on the right
 visual-line-fringe-indicators	    ; show continuation indicators on the left
 '(left-curly-arrow nil))

(defun hook--setup-fringe-bitmaps ()
  (define-fringe-bitmap 'left-curly-arrow
    [#b11000000
     #b01100000
     #b00110000
     #b00011000])
  (define-fringe-bitmap 'right-curly-arrow
    [#b00011000
     #b00110000
     #b01100000
     #b11000000])
  (define-fringe-bitmap 'left-arrow
    [#b00000000
     #b01010100
     #b01010100
     #b00000000])
  (define-fringe-bitmap 'right-arrow
    [#b00000000
     #b00101010
     #b00101010
     #b00000000]))
(add-hook 'after-init-hook #'hook--setup-fringe-bitmaps)

;; Resize like it's 2021
(setq-default frame-inhibit-implied-resize t
	      frame-resize-pixelwise t)

;; Bootstrap package manager (`straight')

;; First, I need to make sure it's in the `exec-path'.

(let ((win-app-dir "~/Applications"))
  (dolist (path (list
		 ;; Windows
		 (expand-file-name "exe" win-app-dir)
		 (expand-file-name "exe/bin" win-app-dir)
		 (expand-file-name "Git/bin" win-app-dir)
		 (expand-file-name "Git/usr/bin" win-app-dir)
		 (expand-file-name "Git/mingw64/bin" win-app-dir)
		 (expand-file-name "Everything" win-app-dir)
		 (expand-file-name "Win-builds/bin" win-app-dir)
		 (expand-file-name "Z/bin" win-app-dir)
		 ;; Linux
		 (expand-file-name "bin" user-emacs-directory)
		 (expand-file-name "~/bin")
		 (expand-file-name "~/.local/bin")
		 (expand-file-name "~/usr/bin")
		 ))
    (when (file-exists-p path)
      (add-to-list 'exec-path path :append))))

;; Set $PATH back to `exec-path', for symmetry's sake.
(setenv "PATH" (mapconcat #'identity exec-path path-separator))

;; Set some variables
(defvar acdw/etc-dir
  (expand-file-name "etc/" user-emacs-directory)
  "Where to put other configurations.")
(defvar acdw/var-dir
  (expand-file-name "var/" user-emacs-directory)
  "Where to put variable stuff.")

(setq-default package-enable-at-startup nil
	      package-quickstart nil
	      straight-use-package-by-default t
	      straight-host-usernames '((github . "duckwork")
					(gitlab . "acdw"))
	      straight-base-dir acdw/var-dir)

;; Run `straight''s bootstrap code
(defvar bootstrap-version)
(let ((bootstrap-file
       (expand-file-name
	"repos/straight.el/bootstrap.el"
	straight-base-dir))
      (bootstrap-version 5))
  (unless (file-exists-p bootstrap-file)
    (with-current-buffer
	(url-retrieve-synchronously
	 "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))


;; `use-package'

(straight-use-package 'use-package)
(require 'use-package)

;; Message startup time
(defun hook--message-startup-time ()
  "Message Emacs' startup time."
  (message "Emacs ready in %s with %d garbage collections."
	   (format "%.2f seconds"
		   (float-time (time-subtract after-init-time
					      before-init-time)))
	   gcs-done))

(add-hook 'emacs-startup-hook #'hook--message-startup-time)