split init.el
[emacs.git] / .emacs.d / initjj.org
1 * Basic initialization (from initjj.org)
2 First of I want a set of variables defined, makes it easier to refer
3 to common things later.
4
5 ** Variables
6 *** System type
7 The variable /system-type/ from emacs isn't directly usable in my
8 case. It contains gnu/linux and gnu/kfreebsd - which has extra /,
9 which is just annoying when using them in a path entry.
10 Hence we do a little creative redefinition for ourself.
11 #+BEGIN_SRC emacs-lisp
12 (defun jj-system-type ()
13 "Return a string depending on the OS we use"
14 (interactive)
15 (cond
16 ((eq system-type 'darwin) "macosx")
17 ((eq system-type 'gnu/linux) "linux")
18 ((eq system-type 'gnu/kfreebsd) "kfreebsd")
19 ((eq system-type 'cygwin) "cygwin")
20 ((eq system-type 'windows-nt) "windows")))
21 #+END_SRC
22
23 *** jj-dir
24 The base directory for all our emacs files. It is based on the
25 directory where we load this file from.
26 #+BEGIN_SRC emacs-lisp
27 (defvar jj-dir (file-name-directory (or load-file-name (buffer-file-name)))
28 "The master directory for Ganneffs emacs configuration and storage.
29 Usually ~/.emacs.d/")
30 #+END_SRC
31
32 *** jj-config-dir
33 In which directory do we store the rest of our config?
34 #+BEGIN_SRC emacs-lisp
35 (defvar jj-config-dir
36 (expand-file-name "config" jj-dir)
37 "Ganneffs main emacs configuration can be found here.")
38 #+END_SRC
39
40 *** jj-emacs-config
41 Name of the main configuration file.
42 #+BEGIN_SRC emacs-lisp
43 (defvar jj-emacs-config
44 (expand-file-name "emacs.org" jj-config-dir)
45 "Ganneffs main emacs configuration file.")
46 #+END_SRC
47
48 *** jj-elisp-dir
49 Where do I store packages for emacs.
50 #+BEGIN_SRC emacs-lisp
51 (defvar jj-elisp-dir
52 (expand-file-name "elisp" jj-dir)
53 "This directory stores subdirs for local packages in Ganneffs emacs config.")
54 #+END_SRC
55
56 *** jj-elisp-local-dir
57 Some packages just come with a single file. I put them into this
58 local directory instead of giving them an own one.
59 #+BEGIN_SRC emacs-lisp
60 (defvar jj-elisp-local-dir
61 (expand-file-name "local" jj-elisp-dir)
62 "This directory stores extra elisp files for Ganneffs emacs config.")
63 #+END_SRC
64
65 *** jj-custom-file
66 The customization interface of emacs stores its values in this file.
67 #+BEGIN_SRC emacs-lisp
68 (defvar jj-custom-file
69 (expand-file-name "customized.el" jj-config-dir)
70 "Changes from the customization interface in Ganneffs emacs config.")
71 #+END_SRC
72
73 *** jj-cache-dir
74 Similar to /var on a unix system, volatile data, cache files, ...
75 #+BEGIN_SRC emacs-lisp
76 (defvar jj-cache-dir
77 (expand-file-name "cache" jj-dir)
78 "This directory stores cache files and other volatile data.")
79 #+END_SRC
80
81 *** jj-backup-directory
82 Backup copies of files I edit are stored here.
83 #+BEGIN_SRC emacs-lisp
84 (defvar jj-backup-directory
85 (expand-file-name (concat "emacs-autosave-" user-login-name) jj-cache-dir)
86 "This directory stores backup files.")
87 #+END_SRC
88
89 *** jj-theme-dir
90 Where my theme files are stored.
91 #+BEGIN_SRC emacs-lisp
92 (defvar jj-theme-dir
93 (expand-file-name "themes" jj-dir)
94 "This directory stores theme files for Ganneffs emacs config")
95 #+END_SRC
96
97 *** jj-sys-config
98 System dependent configuration information stored in here.
99 #+BEGIN_SRC emacs-lisp
100 (defvar jj-sys-config
101 (expand-file-name (concat system-name ".org") jj-config-dir)
102 "Ganneffs System/Host specific emacs configuration file.")
103 #+END_SRC
104
105 *** jj-os-config
106 Operating System dependent configuration information stored in here.
107 #+BEGIN_SRC emacs-lisp
108 (defvar jj-os-config
109 (expand-file-name (concat (jj-system-type) ".org") jj-config-dir)
110 "Ganneffs Operating system specific emacs configuration file.")
111 #+END_SRC
112
113 *** jj-user-config
114 User dependent configuration stored in here.
115 #+BEGIN_SRC emacs-lisp
116 (defvar jj-user-config
117 (expand-file-name (concat user-login-name ".org") jj-config-dir)
118 "Ganneffs username specific emacs configuration file.")
119 #+END_SRC
120
121 *** jj-ev-config
122 Emacs version dependent configuration stored in here.
123 #+BEGIN_SRC emacs-lisp
124 (defvar jj-ev-config (expand-file-name
125 (concat "emacs"
126 (number-to-string emacs-major-version) ".org")
127 jj-config-dir)
128 "Ganneffs emacs version specific configuration file.")
129 #+END_SRC
130
131 *** jj-color-style
132 Which color scheme should be loaded? I prefer dark very much.
133 #+BEGIN_SRC emacs-lisp
134 (defvar jj-color-style 'dark "Which color scheme of solarized to select. Dark or Light")
135 #+END_SRC
136
137 ** Processing
138 First we want to ensure that our cache and backup directories really exist.
139 #+BEGIN_SRC emacs-lisp
140 (if (not (file-exists-p jj-cache-dir))
141 (make-directory jj-cache-dir))
142 (if (not (file-exists-p jj-backup-directory))
143 (make-directory jj-backup-directory))
144 #+END_SRC
145
146 Add our local elisp directory to the load path early, as it contains
147 files we want to load during configuration processing, before the
148 load-path gets set up for real.
149 #+BEGIN_SRC emacs-lisp
150 ;; Have use-package/bindkey in here wich are needed for startup
151 (add-to-list 'load-path jj-elisp-local-dir)
152 #+END_SRC
153
154 I have a function that uses org-tangle to extract all emacs-lisp
155 codeblocks out of my org-mode configuration files. After which it
156 ensures that the generated elisp files get byte-compiled.
157
158 As my configuration requires certain parts of the configuration to
159 already be loaded when the byte-compilation happens, this is done
160 using an /after-init-hook/.
161 #+BEGIN_SRC emacs-lisp
162 (defvar jj-init-files '() "Temporary list of files that need a byte-compile")
163 (defun jj-byte-compile-init ()
164 "Byte compile a list of files"
165 (let (filename)
166 (dolist (filename jj-init-files)
167 (when (file-exists-p filename)
168 (message "Byte-compiling %s, standby" filename)
169 (byte-compile-file filename))))
170 (makunbound 'jj-init-files)
171 (makunbound 'jj-byte-compile-init)
172 )
173 (defun jj-compile-and-load (&optional arg)
174 "Use org-tangle to get the emacs-lisp parts from .org emacs
175 config files into .el ones, byte-compile those and then load
176 them."
177 (let ((el-file (concat (file-name-sans-extension arg) ".el")))
178 (cond
179 ((file-newer-than-file-p arg el-file)
180 (org-babel-tangle-file arg el-file "emacs-lisp")
181 (add-hook 'after-init-hook 'jj-byte-compile-init)
182 (add-to-list 'jj-init-files (symbol-value 'el-file))))
183 (load-file el-file)
184 ))
185 #+END_SRC
186
187 And finally we are going to load the various files.
188 #+BEGIN_SRC emacs-lisp
189 ;; Now read my config
190 ;; Note that those files *can* include further files.
191 ;; The basic config is always loaded, no matter where we start emacs.
192 (jj-compile-and-load jj-emacs-config)
193
194 ;; All the others are optional. We try the order of os, system, user
195 ;; specific files and load them, should they be there
196 (if (file-exists-p jj-os-config) (jj-compile-and-load jj-os-config))
197 (if (file-exists-p jj-sys-config) (jj-compile-and-load jj-sys-config))
198 (if (file-exists-p jj-user-config) (jj-compile-and-load jj-user-config))
199 (if (file-exists-p jj-ev-config) (jj-compile-and-load jj-ev-config))
200
201 (makunbound 'jj-compile-and-load)
202
203 ;; Lets get a message about startup time out
204 (when (require 'time-date nil t)
205 (message "Emacs startup time: %d seconds."
206 (time-to-seconds (time-since emacs-load-start-time)))
207 )
208 #+END_SRC