-
Notifications
You must be signed in to change notification settings - Fork 6
/
init.el
367 lines (310 loc) · 10.6 KB
/
init.el
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
;; -*- lexical-binding:t -*-
;;;;
;; Nore Emacs
;; https://github.com/junjiemars/.emacs.d
;;;;
;; init.el
;;;;
;; Commentary: definitions.
;;;;
;;;
;; compile-time macro
;;;
(defmacro nore-emacs ()
"Nore Emacs git repo."
"https://github.com/junjiemars/.emacs.d")
(defmacro comment (&rest body)
"Ignore BODY, yields nil."
nil)
(defmacro progn% (&rest body)
"Return an \\=`progn\\='ed form if BODY has more than one sexp."
(if (cdr body) `(progn ,@body) (car body)))
(defmacro if% (cond then &rest else)
"If COND yields non-nil, do THEN, else do ELSE..."
(declare (indent 2))
(if (funcall `(lambda () ,cond))
`,then
`(progn% ,@else)))
(defmacro when% (cond &rest body)
"When COND yields non-nil, do BODY."
(declare (indent 1))
`(if% ,cond (progn% ,@body)))
(defmacro unless% (cond &rest body)
"Unless COND yields nil, do BODY."
(declare (indent 1))
`(if% ,cond nil ,@body))
(defvar *gensym-counter* 0 "The counter of \\=`gensym*\\='.")
(defmacro gensym* (&optional prefix)
"Generate a new uninterned symbol, PREFIX default is \"n\"."
`(make-symbol
(format "%s%d" (or ,prefix "n")
(prog1 *gensym-counter*
(setq *gensym-counter* (1+ *gensym-counter*))))))
(defmacro inhibit-file-name-handler (&rest body)
"Inhibit BODY under \\=`file-name-handler-alist\\='."
(declare (indent 0))
`(let ((file-name-handler-alist nil))
,@body))
(defmacro inhibit-gc (&rest body)
"Inhibit BODY under GC."
(declare (indent 0))
`(let ((gc-cons-percentage 0.6))
,@body))
;; end of compile-time macro
;;;
;; *-version% macro
;;;
(defconst +emacs-version+ (string-to-number emacs-version)
"The \\=`float\\=' version number of Emacs.")
(defmacro if-version% (cmp version then &rest else)
"If VERSION CMP with \\=`+emacs-version+\\=' yield non-nil, do
THEN, else do ELSE..."
(declare (indent 3))
`(if% (,cmp ,version +emacs-version+)
,then
(progn% ,@else)))
(defmacro when-version% (cmp version &rest body)
"When VERSION CMP with variable \\=`+emacs-version+\\=' yield
non-nil, do BODY."
(declare (indent 2))
`(if-version% ,cmp ,version (progn% ,@body)))
(defmacro v-name ()
"Return the versioned name."
`(concat (if (display-graphic-p) "g_" "t_")
(number-to-string +emacs-version+)))
;; end of *-version% macro
;;;
;; file macro
;;;
(defun file-name-sans-extension* (file)
"Return the FILE sans extension."
(let ((l (length file)))
(when (> l 0)
(let ((i (1- l)))
(substring-no-properties
file 0
(catch 'br
(while (>= i 0)
(let ((c (aref file i)))
(cond ((= ?/ c) (throw 'br l))
((= ?. c) (throw 'br i))
(t (setq i (1- i))))))))))))
(defun path! (file)
"Make and return the path of posixed FILE."
(inhibit-file-name-handler
(if (file-exists-p file)
file
(prog1 file
(let ((dir (file-name-directory file)))
(unless (file-exists-p dir)
(let ((i (1- (length dir)))
(ds nil))
(catch 'br
(while (> i 0)
(when (= ?/ (aref dir i))
(let ((s (substring-no-properties dir 0 (1+ i))))
(if (file-exists-p s)
(throw 'br t)
(setq ds (cons s ds)))))
(setq i (1- i))))
(while (car ds)
(make-directory-internal (car ds))
(setq ds (cdr ds))))))))))
;; end of file macro
;;;
;; versioned file macro
;;;
(defmacro emacs-home* (&optional file)
"Return path of FILE under \\='~/.emacs.d\\='."
`(concat ,(expand-file-name
(or (getenv-internal "EMACS_HOME") "~/.emacs.d/"))
,file))
(defmacro v-path (file)
"Return versioned FILE."
(let ((f1 (gensym*)))
`(inhibit-file-name-handler
(let ((,f1 ,file))
(concat (file-name-directory ,f1)
,(v-name) "/"
(file-name-nondirectory ,f1))))))
(defmacro v-home (&optional file)
"Return versioned FILE under \\=`emacs-home*\\='."
`(v-path (emacs-home* ,file)))
(defmacro v-home% (&optional file)
"Return versioned path of FILE under \\=`v-home\\=' at
compile-time."
(v-home file))
(defmacro v-home! (file)
"Make versioned path of FILE under \\=`v-home\\=' at compile-time."
(path! (v-home file)))
(defmacro v-home%> (file)
"Return the \\=`v-home\\=' FILE with the extension of compiled file."
(concat (v-home file) (comp-file-extension%)))
;; end of versioned file macro
;;;
;; compile macro
;;;
(defmacro if-native-comp% (then &rest else)
"If native compilation is built-in do THEN, else do ELSE..."
(declare (indent 1))
(if (and (fboundp 'native-comp-available-p)
(native-comp-available-p))
`,then
`(progn% ,@else)))
(defmacro when-native-comp% (&rest body)
"When native compilation support is built-in, do BODY."
(declare (indent 0))
`(if-native-comp% (progn% ,@body)))
(defmacro comp-file-extension% ()
"Return extension of compiled file."
`(if-native-comp% ".eln" ".elc"))
(defmacro v-comp-file! (src)
"Make a versioned cons copy of SRC."
(let ((s1 (gensym*)))
`(let ((,s1 ,src))
(let* ((d1 (v-path ,s1))
(d2 (concat (file-name-sans-extension* d1)
,(comp-file-extension%))))
(when (file-newer-than-file-p ,s1 d1)
(if (file-exists-p d2)
(delete-file d2)
(path! d1))
(copy-file ,s1 d1 t t))
(cons d1 d2)))))
(defmacro time (id &rest form)
"Run FORM and summarize resource usage."
(declare (indent 0))
`(let ((bt (float-time))
(gc gcs-done)
(gt gc-elapsed))
(prog1 (progn% ,@form)
(let ((ct (float-time)))
(message "%.6f %d %.6f %.2f %d %d %d %d %d %d %d %d %.6f %s"
(- ct bt)
(- gcs-done gc)
(- gc-elapsed gt)
gc-cons-percentage
pure-bytes-used
cons-cells-consed
floats-consed
vector-cells-consed
symbols-consed
string-chars-consed
intervals-consed
strings-consed
ct
,id)))))
(defmacro compile-and-load-file (src dst &optional only-compile)
"Compile SRC to DST.\n
If ONLY-COMPILE is t, does not load DST."
(let ((s1 (gensym*)) (d1 (gensym*)) (c1 (gensym*)))
`(let ((,s1 ,src) (,d1 ,dst) (,c1 ,only-compile))
(unless (file-exists-p ,d1)
(if-native-comp%
(native-compile ,s1 ,d1)
(byte-compile-file ,s1)))
(cond (,c1 ,d1)
(t (if-native-comp%
(native-elisp-load ,d1)
(load ,d1 nil nil t)))))))
(defun clean-compiled-files ()
"Clean all compiled files."
(interactive)
(let ((dirs (list `(,(v-home% "config/")
. "\\.el[cn]\\(\\.tmp\\)?\\'")
`(,(v-home% "private/")
. "\\.el[cn]\\(\\.tmp\\)?\\'")
`(,(v-home% "theme/")
. "\\.el[cn]\\(\\.tmp\\)?\\'")
`(,(v-home% ".exec/")
. "\\.el[cn]\\(\\.tmp\\)?\\'")
`(,(emacs-home*)
. "[_a-z]+\\.el[cn]+\\'"))))
(while dirs
(let* ((d (car dirs)) (f1 (car d)) (r1 (cdr d))
(fs (when (file-exists-p f1)
(directory-files f1 nil r1))))
(message "# Clean compiled files: %s..." f1)
(while fs
(delete-file (concat f1 (car fs)))
(setq fs (cdr fs))))
(setq dirs (cdr dirs)))))
;; end of compile macro
;;;
;; *-package% macro
;;;
(defmacro when-package% (&rest body)
"If package is built-in, do BODY."
(declare (indent 0))
`(when-version% <= 24.1 ,@body))
;; end of *-package% macro
;;;
;; Boot
;;;
(when-native-comp%
;; slient native-comp warning
(setq native-comp-async-report-warnings-errors 'silent)
;; first native-comp load
(setcar native-comp-eln-load-path (v-home! ".eln/"))
;; env
(when% (eq system-type 'darwin)
(defun library-path ()
"Even a blind pig can find an acorn once in a while."
(let ((arch (when (string-match
"\\-arch \\([_a-z0-9]+\\)"
system-configuration-options)
(substring-no-properties
system-configuration-options
(match-beginning 1) (match-end 1))))
(platform (when (string-match
"\\(\\-.*?\\)\\."
system-configuration)
(substring-no-properties
system-configuration
(match-beginning 1) (match-end 1))))
(cc3 (when (string-match
"-rpath \\([/a-z]+/\\([a-z]+\\)\\([0-9]+\\)\\)"
system-configuration-options)
(vector
;; path
(substring-no-properties
system-configuration-options
(match-beginning 1) (match-end 1))
;; cc
(substring-no-properties
system-configuration-options
(match-beginning 2) (match-end 2))
;; ver
(substring-no-properties
system-configuration-options
(match-beginning 3) (match-end 3))))))
(let ((path (concat (aref cc3 0) "/"
(aref cc3 1) "/"
arch platform)))
(concat
path "/"
(car (inhibit-file-name-handler
(directory-files
path nil (concat (aref cc3 2) "[.0-9]+") 1))))))))
(when% (eq system-type 'darwin)
(setq process-environment
(cons (concat "LIBRARY_PATH=" (library-path))
process-environment))))
(defmacro compile-and-load-file* (src dst &optional only-compile)
"The profiling shim of \\=`compile-and-load-file*\\='."
(if% (boundp '*nore-emacs-profile*)
`(time ,src (compile-and-load-file ,src ,dst ,only-compile))
`(compile-and-load-file ,src ,dst ,only-compile)))
;; boot
(unless% (boundp '*nore-emacs-no-boot*)
(inhibit-gc
;; (inhibit-file-name-handler)
(let ((u (v-comp-file! (emacs-home* "config/boot.el"))))
(compile-and-load-file* (car u) (cdr u)))))
;; end of Boot
;; package
(when-package%
(setq package-enable-at-startup nil)
(comment (package-initialize)))
;;; After loaded ...
;; end of init.el