968b93d25aeb5527f8272bd7379248ffc2ad5440
[emacs.git] / .emacs.d / init.el
1 ;; Added by Package.el. This must come before configurations of
2 ;; installed packages. Don't delete this line. If you don't want it,
3 ;; just comment it out by adding a semicolon to the start of the line.
4 ;; You may delete these explanatory comments.
5 ;(package-initialize)
6
7 ;; Ganneff's emacs config
8
9 ;; Emacs can not read its config directly from org-mode files.
10 ;; But as my config nearly completly is inside an org-mode file,
11 ;; emacs need to be told to deal with it.
12 ;; Meet org-tangle/org-babel-load-file, which extracts the emacs-lisp
13 ;; source parts of my config files, writes them into .el files
14 ;; and then lets emacs load those.
15
16 ;; That way I have a nice environment to edit and comment - and easily export
17 ;; to web or elsewhere - while emacs has its elisp. The initial run after
18 ;; a config change takes longer, but as that only happens when I change
19 ;; something in the .org files - I don't care.
20
21 (eval-and-compile
22 (defvar jj--file-name-handler-alist file-name-handler-alist)
23 (setq gc-cons-threshold most-positive-fixnum
24 gc-cons-percentage 0.6
25 file-name-handler-alist nil))
26
27 ;; Go go go
28 (unless noninteractive
29 (message "Emacs is powering up... Be patient, Master %s!" (getenv "USER")))
30
31 ;; I like to see how long it takes to "boot" emacs (usually one or two
32 ;; seconds), so save the start time
33 (defvar emacs-load-start-time (current-time))
34
35 ;; If we are starting a daemon, load each and every package. If not,
36 ;; default use-package is to delay package loading, which is good when
37 ;; interactively starting emacs.
38 (if (daemonp)
39 (setq use-package-always-demand t))
40
41 ; Much larger threshold for garbage collection prevents it to run too often.
42 ;(setq gc-cons-threshold 480000000)
43
44 ;; Set a high number of lines for the message buffer so stuff doesn't "scroll out"
45 (setq message-log-max 16384)
46
47 ;; Don't load old .elc files when the .el file is newer
48 (setq load-prefer-newer t)
49
50 ;; ;; Set path to (my, recent) Org-Mode version
51 ;; (add-to-list 'load-path (concat (file-name-directory (or load-file-name (buffer-file-name))) "elisp/org/"))
52 ;; org-mode can load modules when it loads itself, lets tell it which ones we want.
53 ; org-checklist
54 (setq org-modules (quote
55 (org-bbdb org-bibtex org-crypt org-docview org-gnus
56 org-id org-info org-habit org-inlinetask
57 org-irc org-protocol org-w3m org-mouse
58 icalendar))) ; org-notmuch
59
60 ;; And load org-mode
61 (require 'org)
62 ;; Without this the whole config can't load
63 (require 'ob-tangle)
64 ;; And finally, let org-babel do its work. All the rest of the emacs
65 ;; initialization comes from initjj.org
66 ;(org-babel-load-file (expand-file-name "initjj.org" (file-name-directory (or load-file-name (buffer-file-name))))))
67
68 (defun my-tangle-section-canceled ()
69 "Checks if the previous section header was CANC"
70 (save-excursion
71 (if (re-search-backward "^\\*+\\s-+\\(.*?\\)?\\s-*$" nil t)
72 (progn
73 ;; (message "FOUND '%s'" (match-string 1))
74 (string-prefix-p "CANC" (match-string 1)))
75 nil)))
76
77 (defun my-tangle-config-org (orgfile elfile)
78 "This function will write all source blocks from =config.org= into
79 =config.el= that are ...
80
81 - not marked as :tangle no
82 - have a source-code of =emacs-lisp=
83 - doesn't have the todo-marker CANC"
84 (let* ((body-list ())
85 (gc-cons-threshold most-positive-fixnum)
86 (org-babel-src-block-regexp (concat
87 ;; (1) indentation (2) lang
88 "^\\([ \t]*\\)#\\+begin_src[ \t]+\\([^ \f\t\n\r\v]+\\)[ \t]*"
89 ;; (3) switches
90 "\\([^\":\n]*\"[^\"\n*]*\"[^\":\n]*\\|[^\":\n]*\\)"
91 ;; (4) header arguments
92 "\\([^\n]*\\)\n"
93 ;; (5) body
94 "\\([^\000]*?\n\\)??[ \t]*#\\+end_src")))
95 (with-temp-buffer
96 (insert-file-contents orgfile)
97 (goto-char (point-min))
98 (while (re-search-forward org-babel-src-block-regexp nil t)
99 (let ((lang (match-string 2))
100 (args (match-string 4))
101 (body (match-string 5))
102 (canc (my-tangle-section-canceled)))
103 (when (and (string= lang "emacs-lisp")
104 (not (string-match-p ":tangle\\s-+no" args))
105 (not canc))
106 (add-to-list 'body-list body)))))
107 (with-temp-file elfile
108 (insert ";; *- lexical-binding: t; -*-\n")
109 (insert (format ";; Don't edit this file, edit %s instead ...\n\n" orgfile))
110 ;; (insert (apply 'concat (reverse body-list)))
111 (apply 'insert (reverse body-list)))
112 (message "Wrote %s ..." elfile)
113 (byte-compile-file elfile)))
114
115
116 (let ((orgfile (expand-file-name "initjj.org" (file-name-directory (or load-file-name (buffer-file-name)))))
117 (elfile (expand-file-name "initjj.el" (file-name-directory (or load-file-name (buffer-file-name))))))
118 (when (or (not (file-exists-p elfile))
119 (file-newer-than-file-p orgfile elfile))
120 (my-tangle-config-org orgfile elfile))
121 (load-file elfile))
122
123 ; And back done a bit with it, startup is over
124 ;(setq gc-cons-threshold (* 100 1024 1024))
125
126 ;; Don't keep gc-cons-threshold too high. It helps to stave off the GC while
127 ;; Emacs starts up, but afterwards it causes stuttering and random freezes. So
128 ;; reset it to a reasonable default.
129 (setq gc-cons-threshold 16777216
130 gc-cons-percentage 0.1
131 file-name-handler-alist jj--file-name-handler-alist)
132 (custom-set-variables
133 ;; custom-set-variables was added by Custom.
134 ;; If you edit it by hand, you could mess it up, so be careful.
135 ;; Your init file should contain only one such instance.
136 ;; If there is more than one, they won't work right.
137 '(package-selected-packages (quote (notmuch validate use-package paradox keyfreq))))
138 (custom-set-faces
139 ;; custom-set-faces was added by Custom.
140 ;; If you edit it by hand, you could mess it up, so be careful.
141 ;; Your init file should contain only one such instance.
142 ;; If there is more than one, they won't work right.
143 )