about summary refs log tree commit diff stats
path: root/emacs.d/early-init.el
blob: 7374bd1a8fc80cab6309e5eb4e375c80d9e24a7f (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
;;; ~/.emacs.d/early-init.el -*- lexical-binding: t; -*-
;; Author: Case Duckworth <acdw@acdw.net>

(setopt frame-inhibit-implied-resize t)
(setopt frame-resize-pixelwise t)
(setopt window-resize-pixelwise t)
(setopt default-frame-alist
        '((menu-bar-lines . 0)
          (tool-bar-lines . 0)
          (vertical-scroll-bars)
          (horizontal-scroll-bars)))

(defvar *fonts*
  '((default
     :family ;;("Recursive Mono Casual Static" "DejaVu Sans Mono")
     ("Public Sans" "DejaVu Sans")
     :height 100)
    (variable-pitch
     :family ("Public Sans" "DejaVu Sans")
     :height 1.0)
    (fixed-pitch
     :family ("Recursive Mono Casual Static" "DejaVu Sans Mono"))
    (fixed-pitch-serif
     :family ("Recursive Mono Linear Static" "DejaVu Sans Mono"))))

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

;;; Custom functions

(defun pulse@eval (start end &rest _)
  "ADVICE: makes `pulse-momentary-highlight-region' accept other arities."
  (pulse-momentary-highlight-region start end))

(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))))

(defun delete-trailing-whitespace-except-current-line ()
  "Delete all trailing whitespace except current line."
  (save-excursion
    (delete-trailing-whitespace (point-min)
                                (line-beginning-position))
    (delete-trailing-whitespace (line-end-position)
                                (point-max))))

(defun run-after-frame-init (func)
  "Run FUNC after the first frame is initialized.
If already so, run FUNC immediately."
  (cond
   ((daemonp)
    (add-hook 'server-after-make-frame-hook func)
    (advice-add func :after (lambda ()
                              (remove-hook 'server-after-make-frame-hook
                                           func)
                              (advice-remove func
                                             'after-frame-init-removing-advice))


                '((name . after-frame-init-removing-advice))))
   ((not after-init-time)
    (add-hook 'after-init-hook func))
   (:else (funcall func))))

(defun first-found-font (&rest cands)
  "Return the first font of CANDS that is installed, or nil."
  (cl-loop with ffl = (font-family-list)
           for font in cands
           if (member font ffl)
           return font))

(defun setup-faces ()
  "Setup Emacs faces."
  ;; Default faces
  (cl-loop for (face . spec) in *fonts*
           do (set-face-attribute face nil
                                  :family (apply #'first-found-font
                                                 (plist-get spec :family))
                                  :height (or (plist-get spec :height)
                                              'unspecified)))
  ;; Specialized fonts
  (cl-loop with ffl = (font-family-list)
           for (charset . font)
           in '((latin . "Noto Sans")
                (han . "Noto Sans CJK SC Regular")
                (kana . "Noto Sans CJK JP Regular")
                (hangul . "Noto Sans CJK KR Regular")
                (cjk-misc . "Noto Sans CJK KR Regular")
                (khmer . "Noto Sans Khmer")
                (lao . "Noto Sans Lao")
                (burmese . "Noto Sans Myanmar")
                (thai . "Noto Sans Thai")
                (ethiopic . "Noto Sans Ethiopic")
                (hebrew . "Noto Sans Hebrew")
                (arabic . "Noto Sans Arabic")
                (gujarati . "Noto Sans Gujarati")
                (devanagari . "Noto Sans Devanagari")
                (kannada . "Noto Sans Kannada")
                (malayalam . "Noto Sans Malayalam")
                (oriya . "Noto Sans Oriya")
                (sinhala . "Noto Sans Sinhala")
                (tamil . "Noto Sans Tamil")
                (telugu . "Noto Sans Telugu")
                (tibetan . "Noto Sans Tibetan")
                ;; emojis
                (symbol . "Noto Emoji")
                (symbol . "Noto Color Emoji")
                (symbol . "Segoe UI Emoji")
                (symbol . "Apple Color Emoji")
                (symbol . "FreeSans")
                (symbol . "FreeMono")
                (symbol . "FreeSerif")
                (symbol . "Unifont")
                (symbol . "Symbola"))
           if (member font ffl)
           do (set-fontset-font t charset font)))

(defmacro inhibit-messages (&rest body)
  "Inhibit all messages in BODY."
  (declare (indent defun))
  `(cl-letf (((symbol-function 'message) #'ignore))
     ,@body))

(defun kill-buffer-dwim (&optional buffer-or-name)
  "Kill BUFFER-OR-NAME or the current buffer."
  (interactive "P")
  (cond
   ((bufferp buffer-or-name)
    (kill-buffer buffer-or-name))
   ((null buffer-or-name)
    (kill-current-buffer))
   (:else
    (kill-buffer (read-buffer "Kill: " nil :require-match)))))

(defun other-window-dwim (&optional arg)
  "Switch to another window/buffer.
Calls `other-window', which see, unless
- the current window is alone on its frame
- `other-window-dwim' is called with \\[universal-argument]
In these cases, switch to the last-used buffer."
  (interactive "P")
  (if (or arg (one-window-p))
      (switch-to-buffer (other-buffer) nil t)
    (other-window 1)))

(defun delete-window-dwim ()
  "Delete the current window or bury its buffer.
If the current window is alone in its frame, bury the buffer
instead."
  (interactive)
  (unless (ignore-errors (delete-window) t)
    (bury-buffer)))

(defun cycle-spacing* (&optional n)
  "Negate N argument on `cycle-spacing'."
  (interactive "*p")
  (cycle-spacing (- n)))

(defmacro find-user-file (name &optional file-name)
  "Template macro to generate user file finding functions."
  (declare (indent 1))
  (let ((file-name (or file-name (intern (format "user-%s-file" name))))
        (func-name (intern (format "find-user-%s-file" name))))
    `(defun ,func-name (&optional arg)
       ,(format "Edit `%s' in the current window.
With ARG, edit in the other window." file-name)
       (interactive "P")
       (funcall (if arg #'find-file-other-window #'find-file)
                ,file-name))))

(defun indent-buffer+ ()
  "Indent the current buffer and (un)`tabify'.
Whether it tabifies or untabifies depends on `space-indent-modes'."
  (interactive)
  (save-mark-and-excursion
    (indent-region (point-min) (point-max))
    (if (apply #'derived-mode-p space-indent-modes)
        (untabify (point-min) (point-max))
      (tabify (point-min) (point-max)))))

(defun package-ensure (pkg)
  "Install PKG if it's not already installed."
  (unless (package-installed-p pkg)
    (package-vc-install pkg)))

(defun minibuffer-delete-directory ()
  "Delete the last directory in a file-completing minibuffer."
  (interactive)
  (let ((here (point))
        (meta (completion-metadata
               "" minibuffer-completion-table
               minibuffer-completion-predicate)))
    (if (eq (completion-metadata-get meta 'category) 'file)
        (when (search-backward "/" (minibuffer-prompt-end) t)
          (delete-region (point) here))
      (backward-kill-word 1))))

(defun save-buffers-kill* (arg)
  "Save all the buffers and kill ... something.
If ARG is 1 (called normally), kill the current terminal.
If ARG is 4 (with C-u), kill emacs but ask if there are processes running.
If ARG is 16, kill emacs without asking about processes."
  (interactive "p")
  (pcase arg
    (1 (save-buffers-kill-terminal))
    (4 (save-buffers-kill-emacs t))
    (16 (let ((confirm-kill-processes nil)
              (kill-emacs-query-functions nil)
              (confirm-kill-emacs nil))
          (save-buffers-kill-emacs t)))))

(defun regexp-concat (&rest regexps)
  (string-join regexps "\\|"))