/
init.el
424 lines (343 loc) · 13.4 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
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
;;; init.el --- Emacs Configuration File -*- lexical-binding: t -*-
;;; Commentary:
;; Enable debug when encounter errors
(setf debug-on-error t)
(when (eq system-type 'darwin)
;; font scale and fonts
(cond
((find-font (font-spec :family "Sarasa Term SC"))
(set-face-attribute 'default nil
:family "Sarasa Term SC"
:height 160))
(t (set-face-attribute :height 130)))
;; Set for how default frame looks like
(setq default-frame-alist '((menu-bar-lines . nil)
(tool-bar-lines . nil)
(vertical-scroll-bars . nil)))
;; pixel scroll mode
(pixel-scroll-precision-mode t)
;; I am using yabai
(when (executable-find "yabai")
(setf frame-resize-pixelwise t)
(add-to-list 'default-frame-alist '(undecorated . t))))
(when (eq system-type 'gnu/linux)
;; font scale
(set-face-attribute 'default nil :height 130)
;; set for default frame
(setq default-frame-alist '((menu-bar-lines . nil)
(tool-bar-lines . nil)))
;; pixel scroll mode
(pixel-scroll-precision-mode))
;; Use space by default instead of tab for indention
(setq-default indent-tabs-mode nil)
;; Use 2 whitespace for indention rather than tabs
(setq-default tab-width 2)
(defvar ryo:autosaves-dir
(expand-file-name "autosaves" user-emacs-directory)
"All autosaves files goes into `ryo:autosaves-dir'.")
;; make `ryo:autosaves-dir' if it's not exists
(unless (file-exists-p ryo:autosaves-dir)
(make-directory ryo:autosaves-dir t))
(setf auto-save-file-name-transforms
`(("\(?:[^/]/\)\(.*\)" ,(expand-file-name "\\1" ryo:autosaves-dir) t)))
(setf auto-save-default t
auto-save-timeout 10 ; second
auto-save-interval 100 ; after 233 input events, trigger autosave
)
(defvar ryo:backups-dir
(expand-file-name "backups" user-emacs-directory)
"All backup file goes into `ryo:backups-dir'.")
;; make `ryo:backups-dir' if it's not exists
(unless (file-exists-p ryo:backups-dir)
(make-directory ryo:backups-dir t))
;; All backups shall be stored into `backups' folder
(setf backup-directory-alist `((".*" . ,ryo:backups-dir)))
(require 'package)
(package-initialize)
;; Use emacs-china mirror for better connection
(setq package-archives '(("gnu" . "http://1.15.88.122/gnu/")
("melpa" . "http://1.15.88.122/melpa/")
("nongnu" . "http://1.15.88.122/nongnu/")
("org" . "http://1.15.88.122/org/")))
;; not refresh every time for better start up time
;; (package-refresh-contents)
;; use-package was built in after emacs 29
(when (version< emacs-version "29")
(package-install 'use-package))
(require 'use-package)
;; Always ensure the package
(setq use-package-always-ensure t)
(use-package company
:config
(global-company-mode))
(use-package flycheck
:init
(global-flycheck-mode)
:config
(setq-default flycheck-disabled-checkers
'(emacs-lisp-checkdoc)))
(use-package magit)
(use-package tramp)
(use-package eshell
:config
;;; This is the defination of `ryo:eshell',
;;; which give some customization of my personal eshell.
;; require vc-git for `ryo:eshell-git'.
(autoload 'vc-git-branches "vc-git")
;; custom variables
(defcustom ryo:eshell-normal-emoji-sets
'("[´・ᴗ・`]" "[´・ω・`]" "[ •_•]" "[•_• ]")
"A set of char displayed for normal prompt."
:group 'ryo:eshell)
(defcustom ryo:eshell-error-emoji-sets
'("ヘ[´Д`]ノ" "[;◔౪◔]")
"A set of char displayed for error prompt."
:group 'ryo:eshell)
(defcustom ryo:eshell-remote-emoji-sets
'("|ω・]")
"A set of char displayed for remote status."
:group 'ryo:eshell)
(defcustom ryo:eshell-path-name-shorten-trigger-length 30
"The minimum path length to trigger `ryo:eshell-shorten-path-name'."
:group 'ryo:eshell)
(defcustom ryo:eshell-path-name-maximum-length 80
"The maximum path length after `ryo:eshell-shorten-path-name'.")
;; custom faces
(defface ryo:eshell-emoji-normal-face
'((t :background "gold" :foreground "black"))
"Eshell normal emoji prompt face."
:group 'ryo:eshell)
(defface ryo:eshell-emoji-error-face
'((t :background "red" :foreground "white"))
"Eshell error emoji prompt face."
:group 'ryo:eshell)
(defface ryo:eshell-remote-face
'((t :foreground "red"))
"Eshell remote face."
:group 'ryo:eshell)
(defface ryo:eshell-path-face
'((t :foreground "dim gray"))
"Eshell path face."
:group 'ryo:eshell)
(defface ryo:eshell-git-branch-face
'((t :foreground "gray"))
"Eshell git branch face."
:group 'ryo:eshell)
(defface ryo:eshell-prompt-face
'((t :foreground "gold"))
"Eshell prompt face."
:group 'ryo:eshell)
;; custom functions
(defun ryo:eshell-remote-p ()
"Check if work remotely."
(tramp-tramp-file-p default-directory))
(defun ryo:eshell-git-branch ()
"Return current git branch, if no, return `nil'."
(let ((branch (car (vc-git-branches))))
(if branch branch nil)))
(defun ryo:eshell-shorten-path-name (path-name)
"Try to make the `path-name' shorten than
`ryo:eshell-path-name-shorten-trigger-length'. "
(let* ((path (split-string path-name "/"))
(cnt (1- (length path)))
(len (length path-name)))
(string-join
(cl-loop for rest = path then (cdr rest)
for elem = (car rest)
for i below cnt
for name = (string-join
(mapcar (lambda (s) (substring s 0 1))
(string-split elem "[-_\\. ]+" t)))
while (> len ryo:eshell-path-name-shorten-trigger-length)
do (setf len (- len (length name)))
collect name into shortened
finally (return (append shortened rest)))
"/")))
(defun ryo:eshell-path ()
"Return path of current working directory.
If the length of path name is longer `ryo:eshell-path-name-shorten-trigger-length',
try to shorten the path name; and if the shortened path name is still longer than
`ryo:eshell-path-name-maximum-length', try to cutoff the path name directly."
(let ((path-name (abbreviate-file-name (eshell/pwd))))
(if (length> path-name ryo:eshell-path-name-shorten-trigger-length)
(let ((shortened (ryo:eshell-shorten-path-name path-name)))
(if (length> shortened ryo:eshell-path-name-maximum-length)
(concat "..."
(substring path-name
(- (length shortened)
ryo:eshell-path-name-maximum-length)
(1- (length shortened))))
shortened))
path-name)))
(defun ryo:luck-in (&optional posibility limit)
"Return `t' or `nil' at `possibility' chance."
(let ((p (or posibility 0.5))
(lim (or limit 100)))
(if (< (/ (random lim) (float lim)) p) t nil)))
(defun ryo:pick (lst)
"Pick random element from `lst'."
(cl-loop for elem in lst
for count from (length lst) downto 1
if (ryo:luck-in (/ 1.0 count)) return elem))
(defmacro ryo:with-face (string face)
"Bind `string' with `face'."
`(propertize ,string 'face ,face))
(defun eshell-previous-matching-input-from-input (arg)
"Search backwards through input history for match for current input.
\(Previous history elements are earlier commands.)
With prefix argument N, search for Nth previous match.
If N is negative, search forwards for the -Nth following match.
This is a patch for `eshell-previous-matching-input-from-input' which fails
to match if you're using custom prompt that change every time."
(interactive "p")
(if (not (memq last-command '(eshell-previous-matching-input-from-input
eshell-next-matching-input-from-input)))
;; Starting a new search
(setq eshell-matching-input-from-input-string
(buffer-substring (save-excursion (eshell-bol) (point))
(point))
eshell-history-index nil))
(eshell-previous-matching-input
(concat eshell-prompt-regexp
(regexp-quote
(replace-regexp-in-string
eshell-prompt-regexp ""
eshell-matching-input-from-input-string)))
arg))
(defun ryo:eshell-emoji-prompt ()
"Eshell emoji prompt."
(setf eshell-prompt-regexp "^[^#>]* [#>] ")
(concat
;; first line: remote path git
(when (ryo:eshell-remote-p)
(concat (ryo:with-face (ryo:pick ryo:eshell-remote-emoji-sets)
'ryo:eshell-remote-face)
" "))
(ryo:with-face (ryo:eshell-path) 'ryo:eshell-path-face)
(let ((branch (ryo:eshell-git-branch)))
(when branch
(concat " " (ryo:with-face branch 'ryo:eshell-git-branch-face))))
"\n"
;; second line: status-emoji prompt
(if (zerop eshell-last-command-status)
(ryo:with-face (ryo:pick ryo:eshell-normal-emoji-sets)
'ryo:eshell-emoji-normal-face)
(ryo:with-face (ryo:pick ryo:eshell-error-emoji-sets)
'ryo:eshell-emoji-error-face))
(ryo:with-face (if (= (user-uid) 0) " #" " >") 'ryo:eshell-prompt-face)
" "))
(setf eshell-prompt-function #'ryo:eshell-emoji-prompt))
(use-package paredit
:hook ((emacs-lisp-mode
lisp-mode
ielm-mode
eval-expression-minibuffer-setup)
. enable-paredit-mode)
:bind (:map paredit-mode-map
("C-<return>" . ryo:paredit-C-RET))
:init
(defvar ryo:paredit-C-RET-probe-method '()
"Probes to trigger corresponding methods.
The elements of `ryo:paredit-C-RET-probe-method' should be a pair like:
(probe-function-to-trigger . methods-responding)
for example:
(#'minibufferp . #'read--expression-try-read)")
:config
(defun ryo:paredit-C-RET ()
"Trigger by `ryo:paredit-C-RET-probe-method', fall back to `paredit-RET'."
(interactive)
(cl-loop for (trigger . method) in ryo:paredit-C-RET-probe-method
if (or (and (symbolp trigger) (eq major-mode trigger))
(and (functionp trigger) (funcall trigger)))
do (cl-return (funcall method))
finally (paredit-RET)))
;; eval-expression-minibuffer-setup
(add-to-list 'ryo:paredit-C-RET-probe-method
(cons #'minibufferp #'read--expression-try-read))
;; ielm
(add-to-list 'ryo:paredit-C-RET-probe-method
(cons 'inferior-emacs-lisp-mode #'ielm-return)))
(use-package sly
:hook ((sly-mrepl-mode . enable-paredit-mode))
:config
(require 'sly-autoloads)
;; org-mode babel
(setf org-babel-lisp-eval-fn #'sly-eval)
;; sbcl with larger dynamic space size
(setf inferior-lisp-program '("sbcl" "--dynamic-space-size" "4GB"))
;; ryo/paredit-C-RET
(add-to-list 'ryo:paredit-C-RET-probe-method
(cons 'sly-mrepl-mode #'sly-mrepl-return)))
(use-package sly-quicklisp
:after '(sly))
(use-package irony
:hook (((c++-mode c-mode) . irony-mode)))
(use-package flycheck-irony
:after (irony))
(use-package irony-eldoc
:hook ((irony-mode . irony-eldoc))
:after (irony))
(use-package org
:config
;; org-mode and babel
(org-babel-do-load-languages
'org-babel-load-languages
'((emacs-lisp . t)
(lisp . t)
(dot . t)
(C . t)
(python . t)
(ruby . t)
(shell . t)
(gnuplot . t)))
;; auto display image after babel eval
(defun ryo:org-babel-display-image-after-eval ()
"Switch on auto image display after babel eval."
(interactive)
(add-hook 'org-babel-after-execute-hook #'org-display-inline-images))
(defun ryo:org-babel-no-display-image-after-eval ()
"Swtich off auto image display after babel eval."
(interactive)
(remove-hook 'org-babel-after-execute-hook #'org-display-inline-images))
(ryo:org-babel-display-image-after-eval) ; default on
;; org-mode and image preview
(setf org-image-actual-width nil)
;; org-mode and latex preview
;; org pretty symbol for raw input
(setf org-pretty-entities t
org-pretty-entities-include-sub-superscripts nil)
;; Use dvisvgm for SVG preview
(setf org-preview-latex-default-process 'dvisvgm)
(setf (plist-get org-format-latex-options :scale) 1.6)
;; set default latex compiler to xelatex
(setf org-latex-compiler "xelatex")
;; add xeCJK for Chinese support
(add-to-list 'org-latex-packages-alist
'("" "xeCJK" t ("xelatex")))
)
;;; Use CDLaTeX for quick LaTeX equation input
(use-package cdlatex
:hook ((org-mode . turn-on-org-cdlatex)))
(use-package separedit
:bind (:map prog-mode-map
("C-c '" . separedit))
:config
(setq separedit-default-mode 'org-mode))
(use-package markdown-mode
:config
;; hide markups symbols and urls for better lookings
(setq-default markdown-hide-markup t
markdown-hide-urls t)
;; show code colorized
(setq-default markdown-fontify-code-blocks-natively t))
(use-package markdown-toc
:after (markdown-mode)
:config
(defun ryo:add-markdown-toc-hook-before-save ()
"Add locally hook for `before-save-hook'."
(add-hook 'before-save-hook #'markdown-toc-generate-or-refresh-toc 0 'local))
(add-hook 'markdown-mode-hook #'ryo:add-markdown-toc-hook-before-save))
(use-package gnuplot)
(use-package doc-view
:config
(setf doc-view-resolution 400))