summaryrefslogtreecommitdiff
path: root/.config/emacs/init.el
blob: fe2400d1af70cfbaa9b4595f75a5d156fc65ea10 (plain)
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
;; This config requires Emacs 24.4(+?)
;; Hey, Emacs: -*- Indent-tabs-mode: nil -*-
;; Without (advice-add) it should work in older versions of Emacs 24.
;;;; Use XDG-ish locations ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq xdg-cache-home (file-name-as-directory (or (getenv "XDG_CACHE_HOME") "~/.cache")))
(setq
 package-user-dir             (concat xdg-cache-home "emacs/elpa")
 ido-save-directory-list-file (concat xdg-cache-home "emacs/ido.last.el")
 el-get-dir                   (concat xdg-cache-home "emacs/el-get/")
 eshell-directory-name        (concat xdg-cache-home "emacs/eshell/")
 wl-score-files-directory     (concat xdg-cache-home "emacs/wl-score-files/")
 elmo-msgdb-directory         (concat xdg-cache-home "emacs/elmo-msgdb/")
 elmo-cache-directory         (concat xdg-cache-home "emacs/elmo-cache/")
 auto-save-list-file-prefix   (concat xdg-cache-home "emacs/auto-save-list/saves-")
 tramp-persistency-file-name  (concat xdg-cache-home "emacs/tramp-cache.el")
 custom-file                  (concat user-emacs-directory "custom.el")
 wl-init-file                 (concat user-emacs-directory "wl.el")
 )

;;;; The basics that I can't use Emacs without ;;;;;;;;;;;;;;;;;;;;;;;
(show-paren-mode 1)
(column-number-mode 1)
(line-number-mode 1)
(ido-mode 1)
(unless (daemonp) (server-mode 1))
(when (require 'whitespace nil t)
  (setq whitespace-style '(
        tab-mark
        space-mark
        newline-mark
        empty
        ))
  (global-set-key "\C-cw" 'global-whitespace-mode))
(setq minibuffer-prompt-properties '(
      read-only t
      point-entered minibuffer-avoid-prompt
      face minibuffer-prompt
      ))
(load custom-file 'noerror)


;;;; Early settings ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; This isn't particularly important, but set it before doing a whole
;; lot (loading packages), so there isn't a weird change in text size.
(set-face-attribute 'default nil :height 80)


;;;; Bootstrap el-get ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; This downloads and installs el-get (package management) if isn't
;; installed.

;; Derived from ELPA installation
;; http://tromey.com/elpa/install.html
(defun eval-url (url)
  (let ((buffer (url-retrieve-synchronously url)))
  (save-excursion
    (set-buffer buffer)
    (goto-char (point-min))
    (re-search-forward "^$" nil 'move)
    (eval-region (point) (point-max))
    (kill-buffer (current-buffer)))))

(add-to-list 'load-path (concat el-get-dir "el-get"))
(unless (require 'el-get nil t)
  (let ((el-get-install-branch "master")) ;; live life on the edge
    (eval-url "https://github.com/dimitri/el-get/raw/master/el-get-install.el")))


;;;; Install packages ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; This is kinda funny how it works.
;;
;; package.el/ELPA (The standard package manager in Emacs 24) has
;; terrible programatic management.  It's great at installing packages,
;; but only interactively.  So, I
;;   1) Disable package.el from getting loading all downloaded packages
;;   2) Use el-get to load a specified list of packages from elpa.
;;
;; This is also nice because I can do event-based programming for what
;; to do once a package is loaded, using :after.

;; For some reason, el-get adds a bunch of 3rd-party repos to
;; package-archives.  I guess I'm ok with that?  I don't have to
;; manage them.
(setq package-enable-at-startup nil)

;; What packages el-get should get from ELPA (if it gets them at all)
(setq el-get-sources '(
  ;; Minor modes
  (:name dtrt-indent :type elpa ;; Detect indent style for existing files
   :after (dtrt-indent-mode 1))
  (:name page-break-lines :type elpa ;; Display form-feeds pretty
   :before (advice-add 'page-break-lines-mode-maybe
                       :override #'page-break-lines-mode)
   :after (global-page-break-lines-mode 1))
  (:name smart-tabs-mode :type elpa ;; Indent with tabs, align with spaces
   :after (progn
           (smart-tabs-mode 1)
           (apply 'smart-tabs-insinuate
                  (mapcar 'car smart-tabs-insinuate-alist))))
  ;; Major modes
  (:name bison-mode :type elpa)
  (:name coffee-mode :type elpa
   :after (add-hook 'coffee-mode-hook
                    '(lambda ()
                       (set (make-local-variable 'tab-width) 2)
                       (set (make-local-variable 'indent-tabs-mode) nil)
                       )))
  (:name glsl-mode :type elpa)
  (:name go-mode :type elpa)
  (:name graphviz-dot-mode :type elpa)
  (:name haml-mode :type elpa)
  (:name markdown-mode :type elpa
   :after (add-to-list 'auto-mode-alist '("\\.ronn\\'" . markdown-mode)))
  (:name nginx-mode :type elpa
   :after (put 'nginx-indent-level 'safe-local-variable 'integerp))
  (:name scss-mode :type elpa)
  (:name yaml-mode :type elpa)
  ))
;; What packages el-get should install, both from above, and its
;; internal list of sources.
(el-get 'sync (append
  '(el-get)
  ;; Minor modes
  '(dtrt-indent
    page-break-lines
    smart-tabs-mode
    )
  ;; Major modes
  (if (file-exists-p "~/Maildir") ; wanderlust is huge, only use on emailing boxes
      '(apel flim semi wanderlust)
    '())
  '(
    ;;nxhtml ; nxhtml is invasive, only enable if actively using
    bison-mode
    coffee-mode
    glsl-mode
    go-mode
    graphviz-dot-mode
    haml-mode
    markdown-mode
    nginx-mode
    php-mode-improved
    scss-mode
    yaml-mode
    )))


;; Misc. crap

(when (fboundp 'tool-bar-mode) (tool-bar-mode -1))
(setq inhibit-startup-screen t)
(setq-default truncate-lines t)

(defun align-regexp--use-spaces (orig-fun &rest args)
  "Use spaces for alignment"
  (let ((indent-tabs-mode nil))
    (apply orig-fun args)))
(advice-add 'align-regexp :around
           #'align-regexp--use-spaces)

(defun sh-smie-sh-rules--fix (args)
  "Replace :after \"then\" with :after \"if\" because Emacs 24
sh-script.el is broken."
  (if (equal args (list :after "then"))
      (list :after "if")
    args))
(advice-add 'sh-smie-sh-rules :filter-args
           #'sh-smie-sh-rules--fix)

;; Ideally, figuring this out should be done by uniquify, but I
;; haven't determined how to get uniquify to think that it manages the
;; term buffer.
(defun term-get-short-cwd ()
    ;; local base=$PWD
    ;; local suffix=''
    ;; # The regex here is a list of directory names
    ;; # that aren't really helpful, and that the
    ;; # parent directory should be included also.
    ;; if [[ $base =~ (/(src|pkg|doc|pkg-libre|src-libre|trunk|tags|branches))*$ ]]; then
    ;;         suffix=$BASH_REMATCH
    ;;         base=${base%$suffix}
    ;; fi
    ;; base=${base##*/}
    ;; echo ${base}${suffix}
    (directory-file-name default-directory))
(defun term-handle-ansi-terminal-messages--uniquify (args)
  (rename-buffer (concat
                  (replace-regexp-in-string "<.*>$" "" (buffer-name))
                  "<"
                  (term-get-short-cwd)
                  ">")
                 t))
(advice-add 'term-handle-ansi-terminal-messages :after
           #'term-handle-ansi-terminal-messages--uniquify)



;; Make the mouse work in an xterm
(when (fboundp 'xterm-mouse-mode)
  (xterm-mouse-mode 1)
  (add-hook 'after-make-frame-functions
            '(lambda (frame)
               (if xterm-mouse-mode (xterm-mouse-mode 1))
               )))

;; Use mailcrypt to encrypt/decrypt email
(when (require 'mailcrypt nil t)
  (mc-setversion "gpg")
  (add-hook 'wl-summary-mode-hook 'mc-install-read-mode)
  (add-hook 'wl-mail-setup-hook 'mc-install-write-mode)
  (defun mc-wl-verify-signature ()
    (interactive)
    (save-window-excursion
      (wl-summary-jump-to-current-message)
      (mc-verify)))
  (defun mc-wl-decrypt-message ()
    (interactive)
    (save-window-excursion
      (wl-summary-jump-to-current-message)
      (let ((inhibit-read-only t))
        (mc-decrypt))))
  (eval-after-load "mailcrypt"
    '(setq mc-modes-alist
           (append
            (quote
             ((wl-draft-mode (encrypt . mc-encrypt-message)
                             (sign . mc-sign-message))
              (wl-summary-mode (decrypt . mc-wl-decrypt-message)
                               (verify . mc-wl-verify-signature))))
            mc-modes-alist)))
  )

;; Indent settings
(setq-default
 indent-tabs-mode t
 tab-width 8
 c-basic-offset 8
 sh-basic-offset 8
 sh-indent-comment t
 )

;; Backup settings
(setq
 backup-by-copying	t ;; don't clobber symlinks
 backup-directory-alist	'(("." . "~/.cache/emacs/saves")) ;; don't litter my fs tree
 delete-old-versions	t
 kept-new-versions	6
 kept-old-versions	2
 version-control	t ;; use versioned backups
 )

;; Web browser settings
(setq
 browse-url-generic-program (executable-find "v-www-browser")
 browse-url-browser-function 'browse-url-generic
 )

;; Ediff settings
(setq
 ediff-window-setup-function 'ediff-setup-windows-plain
 ediff-split-window-function 'split-window-horizontally
 )

;; Automatically load smerge mode for merge files
(defun try-smerge-mode ()
  (save-excursion
    (goto-char (point-min))
    (when (re-search-forward "^<<<<<<< " nil t)
      (smerge-mode 1))))
(add-hook 'find-file-hook 'try-smerge-mode t)

;; http://www.emacswiki.org/emacs/XModMapMode
(when (not (fboundp 'xmodmap-mode))
  (define-generic-mode 'xmodmap-mode
    '(?!)
    '("add" "clear" "keycode" "keysym" "pointer" "remove")
    nil
    '("[xX]modmap\\(rc\\)?\\'")
    nil
    "Simple mode for xmodmap files."))


;; All my weird mode-specific settings ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(add-hook 'text-mode-hook 'turn-on-auto-fill)

(add-hook 'lisp-mode-hook
          '(lambda ()
             (set (make-local-variable 'indent-tabs-mode) nil)
             ))

(add-hook 'emacs-lisp-mode-hook
          '(lambda ()
             (set (make-local-variable 'indent-tabs-mode) nil)
             ))

(add-hook 'term-mode-hook
          '(lambda ()
             (auto-fill-mode -1)
             (setq term-prompt-regexp "^[^#$%>\n]*[#$%>] *")
             (set (make-local-variable 'mouse-yank-at-point) t)
             (setq tab-width 8 )
             (setq truncate-lines nil)
             (set (make-local-variable 'autopair-dont-activate) t) ;; Don't let autopair break ansi-term
             ))

(add-hook 'ruby-mode-hook
          '(lambda ()
             (set (make-local-variable 'indent-tabs-mode) t)
             (set (make-local-variable 'ruby-indent-level) 4)
             (set (make-local-variable 'tab-width) 4)
             ))

(add-hook 'php-mode-hook
          '(lambda ()
             (c-set-offset 'cpp-macro 0)
             ))

(add-hook 'tex-mode-hook
          '(lambda ()
             (set (make-local-variable 'tab-always-indent) nil)
             (set (make-local-variable 'indent-tabs-mode) t)
             ))

(add-hook 'sh-mode-hook
          '(lambda ()
             (sh-electric-here-document-mode 0)
             ))

(add-to-list 'auto-mode-alist '("PKGBUILD" . sh-mode))
(add-to-list 'auto-mode-alist '("SRCBUILD" . sh-mode))
(add-to-list 'auto-mode-alist '("\\.jad\\'" . java-mode))
(put 'downcase-region 'disabled nil)