-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.el
102 lines (91 loc) · 4.62 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
;; initial load path setup
(defvar my-start-time (current-time)
"Time when Emacs was started")
;; Added by Package.el. This must come before configurations of
;; installed packages. Don't delete this line. If you don't want it,
;; just comment it out by adding a semicolon to the start of the line.
;; You may delete these explanatory comments.
(package-initialize)
(add-to-list 'load-path (concat user-emacs-directory "gp/plugins"))
(add-to-list 'custom-theme-load-path (concat user-emacs-directory "gp/themes/"))
(defun gp-byte-compile-on-save ()
"byte compile an .el file at save time, if
it's in the user emacs directory"
(when (and(string= (file-name-directory (buffer-file-name)) (expand-file-name user-emacs-directory))
(string= (file-name-extension (buffer-file-name)) "el"))
(byte-compile-file (buffer-file-name) nil)))
;; when we open an elisp file, add byte compile function to save hook
(add-hook 'emacs-lisp-mode-hook '(lambda ()
(add-hook 'after-save-hook #'gp-byte-compile-on-save)))
(defun gp-tangle-section-cancelled ()
"checks if previous section header was OFF"
(save-excursion
(if (re-search-backward "^\\*+\\s-+\\(.*?\\)?\\s-*$" nil t) (progn ;; message "FOUND '%s'" (match-string 1))
(string-prefix-p "OFF" (match-string 1)))
nil)))
(defun gp-tangle-config-org (orgfile elfile)
"this function will write all source blocks from =config.org= into
=config.el= that are ...
- not marked as :tangle no
- have a source code of =emacs-lisp=
- do not have a todo marker OFF"
(let* (;; list where we cobble together body parts
(body-list ())
;; disable special file headers when loading .org files
(file-name-handler-alist nil)
;; monster regexp to extract pieces out of an org file
(org-babel-src-block-regexp (concat
;; (1) indentation (2) lang
"^\\([ \t]*\\)#\\+begin_src[ \t]+\\([^ \f\t\n\r\v]+\\)[ \t]*"
;; (3) switches
"\\([^\":\n]*\"[^\"\n*]*\"[^\":\n]*\\|[^\":\n]*\\)"
;; (4) header arguments
"\\([^\n]*\\)\n"
;; (5) body
"\\([^\000]*?\n\\)??[ \t]*#\\+end_src")))
(with-temp-buffer
(insert-file-contents orgfile)
(goto-char (point-min))
(while (re-search-forward org-babel-src-block-regexp nil t)
(let ((lang (match-string 2))
(args (match-string 4))
(body (match-string 5))
(canc (gp-tangle-section-cancelled)))
(when (and (string= lang "emacs-lisp")
(not (string-match-p ":tangle\\s-+no" args))
(not canc))
;; remove commented lines
(setq body (replace-regexp-in-string "^\s+;.+" "" body))
;; remove whitespace-only lines
(setq body (replace-regexp-in-string "^\s*\n" "" body))
(add-to-list 'body-list body)))))
(with-temp-file elfile
(insert ";; *- lexical-binding: t; -*-\n")
(insert (format ";; Don't edit this file, edit %s instead ...\n\n" orgfile))
(apply 'insert (reverse body-list)))))
(defun gp-load-file (fname)
"This loads an elisp configuration file. If an .org file exists,
it will be first untangled. If an byte-compiled file does NOT exist,
it will be created. After this, the normal loading logic happens."
(let* (;; disable garbage collection while we do heavy string work
(gc-cons-threshold most-positive-fixnum)
;; fname with various extensions
(sansfile (expand-file-name (file-name-sans-extension fname) user-emacs-directory))
(orgfile (concat sansfile ".org"))
(elfile (concat sansfile ".el"))
(elcfile (concat sansfile ".elc")))
(when (file-exists-p orgfile)
;; when el file does not exist or org file is newer than el file
(when (or (not (file-exists-p elfile))
(file-newer-than-file-p orgfile elfile))
;; tangle the org file
(gp-tangle-config-org orgfile elfile)))
;; when a compiled file doesnt exist or the el file is newer than compiled version
(when (or (not (file-exists-p elcfile))
(file-newer-than-file-p elfile elcfile))
;; byte compile the el file
(byte-compile-file elfile))
;; load the compiled version
(load elfile nil 'nomessage)))
(gp-load-file "config")
(message "Start up time %.2fs" (float-time (time-subtract (current-time) my-start-time)))