chiark / gitweb /
dot/emacs: Look for running server before leaping at starting a new one.
[profile] / dot / emacs
1 ;;; -*- mode: emacs-lisp; coding: utf-8 -*-
2 ;;;
3 ;;; Emacs configuration file
4 ;;;
5 ;;; (c) 1996-1999 Mark Wooding
6 ;;;
7
8 ;;;----- Licensing notice ---------------------------------------------------
9 ;;;
10 ;;; This program is free software; you can redistribute it and/or modify
11 ;;; it under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 2 of the License, or
13 ;;; (at your option) any later version.
14 ;;;
15 ;;; This program is distributed in the hope that it will be useful,
16 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;;; GNU General Public License for more details.
19 ;;;
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with this program; if not, write to the Free Software
22 ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
23
24 (let ((path "~/lib/emacs/")
25       (pkgs (list "bbdb/" "multiple-cursors/" "rust-mode/"))
26       pkg)
27   (setq load-path (nconc load-path (list path)))
28   (while (setq pkg (pop pkgs))
29     (let ((dir (concat path pkg)))
30       (if (file-exists-p dir)
31           (setq load-path (nconc load-path (list dir))))))
32   (let ((boot (concat path "mdw-pkgs.el")))
33     (if (file-exists-p boot)
34         (load boot))))
35
36 (require 'cl-lib)
37 (require 'dot-emacs)
38
39 (unless (mdw-emacs-version-p 25)
40   (trap (require 'bracketed-paste) (bracketed-paste-enable)))
41
42 ;;;--------------------------------------------------------------------------
43 ;;; Some random initialisation.
44
45 (setq mdw-init-window (selected-window))
46
47 ;; Load some other bits of code.
48
49 ;; Magic to make ELPA packages work.
50 (trap
51 (package-initialize)
52 )
53
54 (maybe-autoload 'cc-mode "cc-mode" nil t)
55 (maybe-autoload 'rexx-mode "rexx-mode" nil t)
56 (maybe-autoload 'cvs-update "pcl-cvs" nil t)
57 (maybe-autoload 'debian-changelog-mode "debian-changelog-mode" nil t)
58 (maybe-autoload 'git-status "git" nil t)
59 (maybe-autoload 'git-blame-mode "git-blame" nil t)
60 (maybe-autoload 'stgit "stgit" nil t)
61 (maybe-autoload 'nc-timesheet-prepare "nc-timesheet" nil t nil)
62 (maybe-autoload 'nc-timesheet-submit "nc-timesheet" nil t nil)
63 (maybe-autoload 'org-bbdb-anniversaries "org" nil t)
64
65 (and (library-exists-p "debian-changelog-mode")
66      (add-to-list 'auto-mode-alist
67                   `(,(concat "/debian/"
68                                "\\("
69                                  "[" "[:lower:]" "[:digit:]" "]"
70                                  "[" "[:lower:]" "[:digit:]" ".+-" "]+"
71                                  "\\."
72                                "\\)?"
73                                "changelog\\'")
74                     . debian-changelog-mode)))
75
76 (and (library-exists-p "vc-git")
77      (not (memq 'GIT vc-handled-backends))
78      (not (memq 'Git vc-handled-backends))
79      (not (memq 'git vc-handled-backends))
80      (setq vc-handled-backends (cons 'GIT vc-handled-backends)))
81
82 (setq magit-log-cutoff-length 512
83       magit-log-section-arguments '("-n256" "--decorate" "--graph"))
84 (defun mdw-magit-try-load-config-extensions ()
85   (trap (magit-load-config-extensions)))
86 (add-hook 'magit-mode-hook 'mdw-magit-try-load-config-extensions)
87
88 (trap (or mdw-fast-startup (require 'p4)))
89
90 (trap (or mdw-fast-startup (require 'tex-site)))
91
92 (trap (or mdw-fast-startup t (semantic-load-enable-code-helpers)))
93 (setq semanticdb-default-save-directory "~/.emacs.d/semanticdb/")
94 (eval-after-load "senator"
95   '(setq isearch-mode-hook
96            (remq 'senator-isearch-mode-hook isearch-mode-hook)
97          isearch-mode-end-hook
98            (remq 'senator-isearch-mode-hook isearch-mode-end-hook)))
99
100 ;; Skeleton stuff.
101
102 (trap (or mdw-fast-startup (require 'skel-init)))
103
104 ;; Window system-dependent things.
105
106 (require 'paren)
107 (trap (show-paren-mode t))
108 (or window-system (mdw-emacs-version-p 22) (menu-bar-mode -1))
109 (if (eq window-system 'x)
110     (setq x-select-enable-clipboard nil
111           x-select-enable-primary t))
112 (setq mouse-drag-copy-region t)
113
114 ;; Multiple cursors.
115
116 (global-set-key [?\C-c ?r] 'mdw-multiple-cursors-keymap)
117 (autoload 'mdw-multiple-cursors-keymap "mdw-multiple-cursors.el"
118   "A keymap for Magnar Sveen's awesome multiple-cursors." nil 'keymap)
119
120 ;; Rust mode.
121
122 (autoload 'rust-mode "rust-mode" nil t)
123 (add-to-list 'auto-mode-alist '("\\.rs\\'" . rust-mode))
124
125 ;; Temporary directory handling.
126
127 (defun mdw-check-dir-exists (dir)
128   (and dir
129        (file-directory-p dir)
130        dir))
131 (setq tmpdir (or (mdw-check-dir-exists (getenv "TMPDIR"))
132                  (mdw-check-dir-exists (format "/tmp/%s" (user-login-name)))
133                  "/tmp"))
134
135 ;; Emacs server behaviour.
136
137 (and (or window-system (mdw-emacs-version-p 23))
138      (not mdw-fast-startup)
139      (not (and (fboundp 'daemonp) (daemonp)))
140      (progn (setq server-temp-file-regexp (concat "^" tmpdir "\\|/draft$")
141                   edit-server-new-frame nil
142                   server-raise-frame nil
143                   gnuserv-frame t)
144             (trap (progn
145                     (require 'server)
146                     (let ((state (server-running-p)))
147                       (if state
148                           (message "Emacs server %s: not starting"
149                                    (cond ((eq state t) "already running")
150                                          (t "maybe running")))
151                         (server-start)))))
152             (trap (progn
153                     (require 'edit-server)
154                     (edit-server-start)
155                     (let ((edit (get-process "edit-server")))
156                       (and edit
157                            (set-process-query-on-exit-flag edit nil)))))))
158
159 ;; Saving state.
160
161 (or mdw-fast-startup (savehist-mode 1))
162 (setq history-length 200)
163
164 ;; Control backup behaviour.
165
166 (setq backup-by-copying nil)
167 (setq backup-by-copying-when-linked t)
168 (setq backup-by-copying-when-mismatch t)
169
170 (setq mdw-backup-disable-regexps
171         '("/\\.git/COMMIT_EDITMSG$"
172           "/\\.stgit\\(-edit\\.txt\\|msg\\.txt\\|\\.msg\\)$"
173           "^/tmp/" "^/var/tmp/"))
174
175 ;; Safe variables.
176
177 (setq safe-local-variable-values
178         '((make-backup-files . nil)))
179
180 ;; Calculator fiddling.
181
182 (setq calc-settings-file "~/.emacs-calc")
183 (load calc-settings-file)
184
185 ;; Saving window-configuration state.
186
187 (or mdw-fast-startup
188     (winner-mode 1))
189
190 ;; Some mail and news configuration.
191
192 (setq mail-from-style 'angles
193       mail-signature t
194       mail-yank-prefix "> "
195       mail-archive-file-name nil
196       message-default-mail-headers
197         "X-Auto-Response-Suppress: DR, RN, NRN, OOF\n"
198       message-default-news-headers ""
199       compose-mail-user-agent-warnings nil)
200
201 (setq rmail-display-summary t)
202 (setq rmail-file-name "~/Mail/rmail")
203
204 (setq sendmail-program "~/bin/sendmail-hack"
205       send-mail-function 'sendmail-send-it
206       mail-interactive t
207       mail-specify-envelope-from t
208       mail-envelope-from 'header
209       sendmail-error-reporting-interactive '("-odb" "-oee"))
210 (autoload 'sendmail-send-it "sendmail")
211 (autoload 'smtpmail-send-it "smtpmail")
212
213 (setq mail-user-agent 'message-user-agent
214       read-mail-command 'gnus)
215 (setq message-signature-separator "^-- \\(\\[mdw\\]\\)?$"
216       message-yank-prefix "> "
217       message-yank-cited-prefix "> "
218       message-send-mail-function 'message-send-mail-with-sendmail
219       message-interactive t
220       message-sendmail-extra-arguments '("-odb" "-oee")
221       message-sendmail-envelope-from 'header
222       message-indent-citation-function '(message-indent-citation
223                                          mdw-trim-spaces-after-citing))
224
225 (defun mdw-trim-spaces-after-citing ()
226   (save-excursion
227     (save-restriction
228       (narrow-to-region (point) (mark t))
229       (while (re-search-forward "^> +$" nil t)
230         (replace-match ">")))))
231
232 (and (fboundp 'turn-on-gnus-dired-mode)
233      (not mdw-fast-startup)
234      (add-hook 'dired-mode-hook 'turn-on-gnus-dired-mode))
235
236 (or mdw-fast-startup
237     (trap (bbdb-initialize 'gnus 'sendmail 'message)))
238 (setq bbdb-file "~/etc/brain/bbdb"
239       bbdb-north-american-phone-numbers-p nil
240       bbdb-dwim-net-address-allow-redundancy t
241       bbdb-extract-address-components-func 'bbdb-extract-address-components
242       bbdb-allow-duplicates t
243       bbdb/news-auto-create-p
244         (lambda ()
245           (let ((group gnus-newsgroup-name))
246             (and (string-match "^nn\\(imap\\|folder\\)\\+" group)
247                  (not (string-match (concat ":" "\\(" "\\(" "crap"
248                                                       "\\|" "spam"
249                                                       "\\|" "lists"
250                                                       "\\|" "corpus"
251                                                       "\\)" "\\."
252                                                 "\\|" "Junk Email$"
253                                                 "\\)")
254                                     group)))))
255       bbdb-user-mail-names
256         (concat "^"
257                 "\\(" "\\(mdw\\|markw\\|root\\|postmaster\\)"
258                       "\\([-+][^@]*\\)?"
259                       "@\\(\\(dist\\|esc\\)orted\\.org\\.uk\\|odin\\.gg\\)"
260                 "\\|" "\\(" "\\(mdw\\|mwooding\\)"
261                             "\\(\\+[^@]*\\)?"
262                       "\\|" "0" "\\(mdw\\|mwooding\\)" "k\\.[^@]*"
263                       "\\)" "@"
264                       "\\(chiark\\|slimy\\|coriolis\\)\\.greenend\\.org\\.uk"
265                 "\\|" "distorted\\.mdw@g\\(oogle\\)?mail.com"
266                 "\\|" "mwooding@\\(good\\|blackberry\\)\\.com"
267                 "\\|" "mark\\.wooding@trustonic\\.com"
268                 "\\)$")
269       bbdb-canonicalize-net-hook
270         (lambda (addr)
271           (cond ((null addr)
272                  nil)
273                 ((or (string-match "^mailer-daemon@" addr)
274                      (string-match "\.invalid$" addr)
275                      (string-match (concat "^reply-[0-9a-f]+-[0-9a-f]+_"
276                                            "HTML-[0-9]+-[0-9]+-[0-9]+"
277                                            "@\\(nationwide-communications\\."
278                                            "co\\.uk\\)$")
279                                    addr)
280                      (string-match (concat "^[0-9]+@bugs\\."
281                                            "\\(" "debian\\.org"
282                                            "\\|" "distorted\\.org\\.uk"
283                                            "\\)$")
284                                    addr)
285                      (string-match (concat "^MicrosoftExchange[0-9a-f]*"
286                                            "@newincco\\.onmicrosoft\\.com")
287                                    addr)
288                      (member (md5 addr)
289                              '("8815c5583970856799c85a3ee0eb6a9f" ;work wiki spam
290                                "0b94ab4d25dacaa5ac07243a09c9e22e" ;work bug spam
291                                "35fb1633379a1e4d4be3b139cae20111" ;work crucible spam
292                                ))
293                      (and (string-match "^news\\([0-9]+\\)@\\(.*\\)$" addr)
294                           (string= (md5 (match-string 2 addr))
295                                    "879b795aed959b1a000e9f95c132b16c")))
296                  nil)
297                 ((string-match "^\\([^@+]+\\)\\+[^@]*\\(@.*\\)$" addr)
298                  (concat (match-string 1 addr) (match-string 2 addr)))
299                 (t addr))))
300
301 ;; Customization.
302
303 (setq custom-file "~/.emacs-custom")
304 (trap (load custom-file))
305
306 (trap (load "~/.emacs-local"))
307
308 ;; Internationalization twiddling.
309
310 (trap
311   ;; Have top-bit-set characters work properly in terminals.
312   (let ((im (current-input-mode)))
313     (apply #'set-input-mode
314            (nconc (list (nth 0 im) (nth 1 im) 0) (nthcdr 3 im)))))
315
316 ;; Some unpleasant terminal hackery.  Screen prefixes the current terminal
317 ;; name with `screen.', and Emacs is too dim to split the name at the `.'.
318 ;; Help it out.
319
320 (defun terminal-init-screen.xterm ()
321   (tty-run-terminal-initialization (selected-frame) "screen"))
322 (defun terminal-init-screen.putty ()
323   (tty-run-terminal-initialization (selected-frame) "screen"))
324
325 ;; Don't disable any commands.
326
327 (mapatoms #'(lambda (sym) (put sym 'disabled nil)))
328
329 ;; Split a wide window.
330
331 (mdw-divvy-window)
332
333 ;; Postscript printing.
334
335 (let ((mm (/ 72 25.4)))
336   (setq ps-paper-type 'a4
337         ps-print-only-one-header t
338         ps-print-color-p t
339         ps-landscape-mode t ps-number-of-columns 4
340         ps-line-number t ps-line-number-step 5 ps-line-number-start 5
341         ps-zebra-stripes nil ps-zebra-stripe-height 5
342         ps-spool-duplex t ps-spool-tumble nil ps-spool-config 'setpagedevice
343         ps-top-margin (* 12 mm)
344         ps-bottom-margin (* 16 mm)
345         ps-header-offset (* 8 mm)
346         ps-left-margin (* 12 mm)
347         ps-right-margin (* 12 mm)
348         ps-inter-column (* 4 mm)
349         ps-font-family 'CourierCondensed ps-font-size '(4.5 . 5.16)
350         ps-line-number-font 'Palatino ps-line-number-font-size 3
351         ps-header-font-family 'Helvetica
352           ps-header-title-font-size '(8 . 10)
353           ps-header-font-size '(6 . 8)))
354
355 ;; Splash screen stuff.
356
357 (if (not mdw-splashy-startup)
358     (setq inhibit-splash-screen t
359           inhibit-startup-message t))
360
361 ;; Other goodies.
362
363 (trap (resize-minibuffer-mode 1))       ;Make minibuffer grow dynamically
364 (auto-compression-mode 1)               ;Enable automatic compression
365 (xterm-mouse-mode 1)
366 (setq enable-local-variables :safe
367       enable-local-eval nil)
368 (setq dabbrev-case-replace nil)         ;Retain case when completing
369 (setq linum-format "%7d ")
370 (setq next-line-add-newlines nil)       ;Don't add weird newlines
371 (setq split-height-threshold nil)       ;Reuse windows where sensible
372 (setq compilation-scroll-output 'first-error)
373 (setq iswitchb-default-method 'samewindow ;Don't confuse me by showing
374       ido-default-file-method 'selected-window ;buffers in other random
375       ido-default-buffer-method 'selected-window) ;frames
376 (setq ido-enable-prefix t
377       ido-enable-flex-matching t
378       ido-everywhere t
379       ido-confirm-unique-completion t
380       ido-use-filename-at-point nil
381       ido-auto-merge-work-directories-length -1)
382 (setq pcomplete-cycle-completions nil   ;Who thought that was a good idea?
383       eshell-cmpl-cycle-completions nil)
384 (setq dired-deletion-confirmer          ;Make deletion easier in dired
385         (symbol-function 'y-or-n-p)
386       delete-by-moving-to-trash (mdw-emacs-version-p 24)
387       dired-listing-switches "-alF"     ;Do `ls -F' things in dired windows
388       wdired-allow-to-change-permissions 'advanced)
389 (setq read-quoted-char-radix 16)        ;C-q HEX-STUFF [RET]
390 (setq case-fold-file-names nil)         ;Don't translate file names (grr...)
391 (setq scroll-step 5)                    ;Don't scroll too much at a time
392 (setq custom-unlispify-tag-names nil    ;Display variable names properly
393       custom-unlispify-menu-entries nil
394       custom-buffer-done-kill t)
395 (setq blink-cursor-blinks 0)            ;Keep blinking the cursor
396 (setq-default fill-column mdw-text-width) ;I use rather narrow windows
397 (setq adaptive-fill-mode nil)           ;I have my own machinery instead
398 (setq reb-re-syntax 'string)            ;Enough backslashes even with this
399 (setq-default comment-column 40)        ;Set a standard comment column
400 (setq-default truncate-partial-width-windows nil
401               truncate-lines t)
402 (setq line-move-visual t
403       visual-order-cursor-movement t)
404 (setq default-indicate-empty-lines t)
405 (setq view-read-only t)
406 (setq-default view-exit-action #'kill-buffer)
407 (and window-system (not mdw-fast-startup)
408      (setq confirm-kill-emacs #'yes-or-no-p))
409 (setq whitespace-style '(trailing empty indentation face lines-tail
410                          space-before-tab space-after-tab)
411       whitespace-line-column mdw-text-width)
412 (setq woman-use-own-frame nil           ;Keep man pages somewhere sensible
413       woman-fill-column 72)             ;Right margin position.
414 (setq diff-switches "-u"                ;I like reading unified diffs
415       cvs-diff-flags (list diff-switches))
416 (setq echo-keystrokes 10)               ;Long delay before keystrokes echo
417 (setq ange-ftp-ftp-program-name "pftp") ;Use passive FTP
418 (setq find-ls-option                    ;Build file lists efficiently
419       '("-print0 | xargs -0r ls -ld" . "ld"))
420 (setq bookmark-save-flag 0)             ;Save bookmarks automatically
421 (setq ediff-grab-mouse nil)
422 (setq vc-follow-symlinks t
423       vc-git-resolve-conflicts nil)
424 (setq x-gtk-file-dialog-help-text nil)
425 (setq use-dialog-box nil)
426 (setq Info-fontify-maximum-menu-size 100000)
427 (setq set-mark-command-repeat-pop t)
428 (setq password-cache-expiry nil)
429 (setq mouse-autoselect-window nil)
430 (setq-default proced-filter 'all
431               proced-sort 'user)
432 (setq ispell-program-name "aspell-hack"
433       ispell-local-dictionary "en_GB-ize-w_accents"
434       flyspell-default-dictionary "en_GB-ize-w_accents"
435       ispell-local-dictionary-alist
436       '(("en_GB-ize-w_accents" "[[:alpha:]]" "[^[:alpha:]]" "'" t
437          ("-d" "en_GB-ize-w_accents") nil utf-8))
438       ispell-silently-savep t)
439 (setq-default viper-case-fold-search t
440               viper-electric-mode t
441               viper-ex-style-motion nil
442               viper-ex-style-editing nil
443               viper-expert-level 5
444               viper-inhibit-startup-message t
445               ;;viper-insert-state-cursor-color "red"
446               viper-no-multiple-ESC nil
447               viper-re-query-replace t
448               viper-re-search t
449               ;;viper-replace-overlay-cursor-color "red"
450               viper-shift-width 2
451               viper-vi-style-in-minibuffer nil
452               viper-want-ctl-h-help t)
453 (trap
454   (require 'uniquify)
455   (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
456   (setq uniquify-after-kill-buffer-p t))
457 (transient-mark-mode t)
458 (setq mark-even-if-inactive t
459       shift-select-mode nil
460       delete-active-region nil
461       delete-selection-mode nil)
462 (trap
463   (tooltip-mode 0)
464   (tool-bar-mode 0))
465 (trap (or mdw-fast-startup (global-auto-revert-mode t)))
466
467 (defun mdw-set-viper-cursor-colour ()
468   ;; ?? doesn't seem to work
469   (set-frame-parameter nil 'cursor-color "green")
470   (message ";; %S" post-command-hook)
471   (sit-for 2))
472 ;;(add-hook 'viper-vi-state-hook 'mdw-set-viper-cursor-colour)
473
474 (defcustom mdw-black-background t
475   "Whether the Emacs background is black (or dark, at any rate)."
476   :type 'boolean)
477
478 (eval-after-load "outline"
479   '(progn
480      (trap (require 'foldout))
481      (define-key outline-mode-prefix-map [?\C-r] 'reveal-mode)
482      (define-key outline-mode-prefix-map [?\C--] 'mdw-outline-collapse-all)))
483
484 (define-key isearch-mode-map [?\C-?] 'isearch-del-char)
485 (define-key isearch-mode-map [backspace] 'isearch-del-char)
486 (define-key isearch-mode-map [?\e ?\C-?] 'isearch-delete-char)
487
488 (setq imenu-auto-rescan t
489       imenu-auto-rescan-maxout (* 128 1024)
490       imenu-max-items 60)
491
492 (setq cltl2-root-url (mdw-config 'cltl-url))
493 (setq common-lisp-hyperspec-root (mdw-config 'hyperspec-url))
494
495 (setq ediff-split-window-function 'split-window-horizontally)
496
497 (setq rfcview-rfc-location-pattern
498         "/ftp@mirror.distorted.org.uk:/pub/mirrors/rfc/rfc%s.txt"
499       rfcview-std-location-pattern
500         "/ftp@mirror.distorted.org.uk:/pub/mirrors/rfc/std/std%s.txt"
501       rfcview-index-location
502         "/ftp@mirror.distorted.org.uk:/pub/mirrors/rfc/rfc-index.txt")
503
504 ;;;--------------------------------------------------------------------------
505 ;;; W3 and URL fetching stuff.
506
507 (let ((proxy (mdw-config 'http-proxy)))
508   (setq url-proxy-services
509           `(("http" . ,proxy)
510             ("ftp" . ,proxy)
511             ("gopher" . ,proxy))))
512 (setq url-cookie-untrusted-urls '("."))
513
514 (setq browse-url-browser-function (mdw-good-url-browser)
515       browse-url-generic-program "mdw-chrome"
516       browse-url-mozilla-program "mdw-iceweasel")
517
518 (setq w3m-default-display-inline-images t
519       w3m-use-cookies t)
520
521 (eval-after-load "w3m"
522   '(let ((entries '(("application/pdf" "\\.pdf\\'" ("xdg-open" file) nil)
523                     ("application/x-pdf" "\\.pdf\\'" ("xdg-open" file) nil))))
524      (dolist (e entries)
525        (setq w3m-content-type-alist
526                (cons e (cl-remove (car e) w3m-content-type-alist
527                                   :key #'car :test #'string=))))))
528
529 (setq w3-do-incremental-display t
530       w3-use-menus '(file edit view go bookmark options
531                      buffers style search emacs nil help)
532       w3-display-inline-image t
533       w3-keybinding 'info)
534
535 ;;;--------------------------------------------------------------------------
536 ;;; Calendar configuration.
537
538 (setq diary-file
539       (let ((main-diary "~/etc/brain/diary")
540             (index "~/etc/index.diary"))
541         (if (file-exists-p index)
542             index
543           main-diary)))
544
545 ;; Trivial stuff for the sunrise/sunset calculations.
546
547 (setq calendar-latitude 52.2)
548 (setq calendar-longitude 0.1)
549 (setq calendar-location-name "Cambridge, UK")
550
551 ;; Holidays.
552
553 (and (not mdw-fast-startup)
554      (trap
555        (require 'ew-hols)
556        (setq other-holidays (append english-and-welsh-bank-holidays
557                                     other-holidays))))
558
559 ;; Date format fiddling.
560
561 (setq european-calendar-style t
562       calendar-date-style 'european
563       calendar-time-display-form
564         '(24-hours ":" minutes
565           (if time-zone " (") time-zone (if time-zone ")")))
566
567 (setq diary-date-forms
568         '((day "[-/]" month "[^-/0-9]")
569           (day " *" monthname "[ \t]*\\(\^M\\|\n\\)")
570           (backup day " *" monthname "\\W+\\<\\([^*0-9]\\|[0-9]+[:aApP]\\)")
571           (day "[-/]" month "[-/]" year "[^0-9]")
572           (day " *" monthname " *" year "[^0-9]")
573           (year "[-/]" month "[-/]" day "[^0-9]")
574           (dayname "\\W")))
575
576 ;; Fancy diary handling.
577
578 (add-hook 'diary-display-hook 'fancy-diary-display)
579 (setq diary-list-include-blanks t)
580 (cond ((mdw-emacs-version-p 23 1)
581        (add-hook 'list-diary-entries-hook 'diary-include-other-diary-files)
582        (add-hook 'mark-diary-entries-hook 'diary-mark-included-diary-files))
583       (t
584        (add-hook 'list-diary-entries-hook 'include-other-diary-files)
585        (add-hook 'mark-diary-entries-hook 'mark-included-diary-files)))
586
587 ;; Appointment management.
588
589 (add-hook 'diary-hook 'appt-make-list)
590 (setq appt-issue-message t
591       appt-display-interval 3
592       appt-message-warning-time 10
593       appt-display-diary nil)
594 (and (not mdw-fast-startup)
595      (trap (require 'org)
596            (require 'bbdb)
597            (appt-activate 1)))
598
599 ;; Org-mode agenda.
600
601 (setq org-agenda-include-diary t
602       org-directory "~/etc/brain.local/"
603       org-default-notes-file (concat org-directory "local.org")
604       org-tags-column -77
605       org-agenda-align-tags-to-column org-tags-column)
606
607 ;; Cosmetic stuff.
608
609 (setq calendar-view-diary-initially-flag nil
610       calendar-view-holidays-initially-flag nil
611       calendar-mark-diary-entries-flag t)
612
613 (setq display-time-24hr-format t)
614 (display-time)
615 (column-number-mode 1)
616 (trap
617   (if mdw-splashy-startup
618       (let ((calendar-view-diary-initially-flag t))
619         (calendar))))
620
621 ;;;--------------------------------------------------------------------------
622 ;;; MailCrypt.
623
624 ;; Define more mode hooks for MailCrypt.
625
626 (setq mdw-mc-modes
627         '((mdwmail-mode (encrypt . mdwmail-mc-encrypt)
628                         (sign . mdwmail-mc-sign))))
629
630 ;; Load the MailCrypt support.
631
632 (trap
633   (and (string-match "linux" (symbol-name system-type))
634        (not mdw-fast-startup)
635        (progn (require 'mailcrypt-init)
636               (require 'mailcrypt)
637               (setq mc-default-scheme 'mc-scheme-gpg)
638               (setq mc-pgp-user-id "mdw-nsict-pgp")
639               (setq mc-gpg-user-id "mdw-nsict-gpg")
640               (setq mc-modes-alist (append mc-modes-alist mdw-mc-modes))
641               (setq mc-pgp-always-sign t)
642               (setq mc-gpg-always-sign t)
643               (setq mc-always-replace 'never)
644               (setq mc-passwd-timeout 3600)
645               (setq mc-temp-directory tmpdir)
646               (setq mc-modes-alist (append mc-modes-alist mdw-mc-modes))
647               (define-key mc-write-mode-map "\C-c/S" 'mc-sign-region)
648               (define-key mc-write-mode-map "\C-c/E" 'mc-encrypt-region)
649               (add-hook 'text-mode-hook 'mc-install-write-mode))))
650
651 (setq epa-pinentry-mode 'loopback)
652
653 ;;;--------------------------------------------------------------------------
654 ;;; Other common declarations.
655
656 ;; Default frame size.
657
658 (setq frame-background-mode (if mdw-black-background 'dark 'light)
659       frame-inhibit-implied-resize t
660       default-frame-alist
661         `((width . ,(+ mdw-column-width mdw-frame-width-fudge))
662           (height . 33)
663           (vertical-scroll-bars . right)
664           (cursor-type . bar)
665           (cursor-blink . t)
666           (left-fringe . 5)
667           (right-fringe . 5)
668           (scroll-bar-width . 15)
669           (tool-bar-lines . 0)
670           (menu-bar-lines . 1)
671           (cursor-color . "red")
672           (background-mode . ,frame-background-mode))
673       initial-frame-alist
674         `((width . ,(+ mdw-column-width
675                        (if (>= emacs-major-version 21) 0 1)))
676           (menu-bar-lines . ,(if window-system 1 0)))
677       window-system-default-frame-alist
678         '((pm (font . "-os2-System VIO-medium-r-normal--*-40-*-*-m-*-cp850")
679               (menu-font . "8.Helv")
680               (background-color . "lightgrey"))
681           (w32 (font . "fixed613 10")
682                (background-color . "black")
683                (foreground-color . "white"))
684           (nil (menu-bar-lines . 0)
685                (background-color . nil))))
686 (let ((backg (frame-parameter nil 'background-color)))
687   (if (and backg window-system)
688       (push (cons 'background-color backg) default-frame-alist)))
689
690 ;; Other frame fiddling.
691
692 (defcustom mdw-hostname
693   (if (string-match "\\." system-name)
694       (substring system-name 0 (match-beginning 0))
695     system-name)
696   "Unqualified hostname Emacs is running on."
697   :type 'string
698   :safe 'stringp)
699 (setq frame-title-format '("Emacs " user-login-name "@" mdw-hostname " – %b")
700       icon-title-format frame-title-format)
701
702 ;; Global keymap changes.
703
704 (trap
705   (windmove-default-keybindings))
706 (setq windmove-wrap-around t)
707 (trap (require 'ido) (ido-mode 1)
708       (require 'ido-completing-read+)
709       (unless (fboundp 'ido-ubiquitous-mode) (require 'ido-ubiquitous))
710       (ido-ubiquitous-mode 1)
711       (setq ido-cr+-max-items nil)
712       (let ((excludes `(,(concat "\\`"
713                                  "gtags-find-"
714                                  "\\("    "tag"
715                                        "\\|file"
716                                        "\\|rtag"
717                                        "\\|symbol"
718                                  "\\)"
719                                  "\\("  "\\|-other-window"
720                                  "\\)"
721                                  "\\'"))))
722         (if (boundp 'ido-ubiquitous-command-overrides)
723             (setq ido-ubiquitous-command-overrides
724                     (append ido-ubiquitous-command-overrides
725                             (mapcar (lambda (exclude)
726                                       (if (symbolp exclude)
727                                           `(enable-old exact
728                                                        ,(symbol-name exclude))
729                                         `(enable-old regexp ,exclude)))
730                                     excludes)))
731           (setq ido-cr+-function-blacklist
732                   (append ido-cr+-function-blacklist excludes))))
733       (setq magit-completing-read-function 'magit-ido-completing-read)
734       (require 'smex) (smex-initialize) (global-set-key [?\M-x] 'smex))
735
736 (progn
737   (global-set-key [?\e ?\e] 'mdw-wrong)
738   (global-set-key [?\e ?\C-\] ?\C-\]] 'keyboard-escape-quit)
739   (global-set-key [?\C-c ?\M-x] 'execute-extended-command)
740   (global-set-key [?\C-c ?d ?a] 'devhelp-assistant-word-at-point)
741   (global-set-key [?\C-c ?d ?w] 'devhelp-word-at-point)
742   (global-set-key [?\C-c ?g ?a] 'org-agenda)
743   (global-set-key [?\C-c ?g ?b] 'org-iswitchb)
744   (global-set-key [?\C-c ?g ?l] 'org-store-link)
745   (global-set-key [?\C-c ?k] 'mdw-compile)
746   (global-set-key [?\C-c ?m ?4 ?f] 'magit-find-file-other-window)
747   (global-set-key [?\C-c ?m ?b] 'magit-blame)
748   (global-set-key [?\C-c ?m ?d] 'magit-dispatch-popup)
749   (global-set-key [?\C-c ?m ?f] 'magit-find-file)
750   (global-set-key [?\C-c ?m ?l] 'magit-toggle-buffer-lock)
751   (global-set-key [?\C-c ?m ?m] 'magit-status)
752   (global-set-key [?\C-c ?m ?r] 'magit-list-repositories)
753   (global-set-key [?\C-c ?m ?w] 'magit-wip-log)
754   (global-set-key [?\C-c ?p ?s] 'mdw-mpc-stop)
755   (global-set-key [?\C-c ?p ?<] 'mdw-mpc-prev)
756   (global-set-key [?\C-c ?p ?>] 'mdw-mpc-next)
757   (global-set-key [?\C-c ?p ?+] 'mdw-mpc-louder)
758   (global-set-key [?\C-c ?p ?-] 'mdw-mpc-quieter)
759   (global-set-key [?\C-c ?p ??] 'mdw-mpc-now-playing)
760   (global-set-key [?\C-c ?p ?p] 'mdw-mpc-play-or-pause)
761   (global-set-key [?\C-c ?t ?4 ?.] 'gtags-find-tag-other-window)
762   (global-set-key [?\C-c ?t ?.] 'gtags-find-tag)
763   (global-set-key [?\C-c ?t ?R] 'timeclock-reread-log)
764   (global-set-key [?\C-c ?t ?,] 'gtags-pop-stack)
765   (global-set-key [?\C-c ?t ?c] 'timeclock-change)
766   (global-set-key [?\C-c ?t ?i] 'timeclock-in)
767   (global-set-key [?\C-c ?t ?o] 'timeclock-out)
768   (global-set-key [?\C-c ?t ?r] 'gtags-find-rtag)
769   (global-set-key [?\C-c ?t ?s] 'timeclock-status-string)
770   (global-set-key [?\C-c ?t ?t] 'gtags-find-tag-from-here)
771   (global-set-key [?\C-c ?t ?w] 'timeclock-workday-remaining-string)
772   (global-set-key [?\C-c ?v ?f] 'view-file)
773   (global-set-key [?\C-c ?v ?i] 'vm-visit-imap-folder)
774   (global-set-key [?\C-c ?v ?m] 'vm-visit-folder)
775   (global-set-key [?\C-c ?v ?v] 'mdw-auto-revert)
776   (global-set-key [?\C-c ?w ? ] 'mdw-designate-window)
777   (global-set-key [?\C-c ?w ?b] 'w3m-bookmark-view)
778   (global-set-key [?\C-c ?w ?c] 'mdw-set-frame-colour)
779   (global-set-key [?\C-c ?w ?d] 'mdw-divvy-window)
780   (global-set-key [?\C-c ?w ?h] 'windmove-left)
781   (global-set-key [?\C-c ?w ?j] 'windmove-down)
782   (global-set-key [?\C-c ?w ?k] 'windmove-up)
783   (global-set-key [?\C-c ?w ?l] 'windmove-right)
784   (global-set-key [?\C-c ?w ?r] 'winner-redo)
785   (global-set-key [?\C-c ?w ?s] 'mdw-switch-window-configuration)
786   (global-set-key [?\C-c ?w ?u] 'winner-undo)
787   (global-set-key [?\C-c ?w ?w] 'mdw-set-frame-width)
788   (global-set-key [?\C-c ?w up] 'windmove-up)
789   (global-set-key [?\C-c ?w down] 'windmove-down)
790   (global-set-key [?\C-c ?w left] 'windmove-left)
791   (global-set-key [?\C-c ?w right] 'windmove-right)
792   (global-set-key [?\C-h ?\C-m] 'manual-entry)
793   (global-set-key [?\C-x ?4 ?\C-j] 'dired-jump-other-window)
794   (global-set-key [?\C-x ?4 ?j] 'dired-jump-other-window)
795   (global-set-key [?\C-x ?\C-j] 'dired-jump)
796   (global-set-key [?\C-x ?\C-n] 'skel-create-file)
797   (global-set-key [?\C-x ?4 ?n] 'skel-create-file-other-window)
798   (global-set-key [?\C-x ?5 ?n] 'skel-create-file-other-frame)
799   (global-set-key [?\C-x ?3] 'mdw-split-window-horizontally)
800   (global-set-key [?\C-x ?/] 'auto-fill-mode)
801   (global-set-key [?\M-#] 'calc-dispatch)
802   (global-set-key [?\M-q] 'mdw-fill-paragraph)
803   (global-set-key [delete] 'delete-char)
804   (global-set-key [insertchar] 'overwrite-mode)
805   (global-set-key [C-M-backspace] 'backward-kill-sexp)
806   (global-set-key [mode-line C-mouse-1] 'mouse-tear-off-window)
807   (global-set-key [vertical-scroll-bar C-down-mouse-1]
808                   'mouse-drag-vertical-line)
809   (global-set-key [vertical-scroll-bar C-mouse-1]
810                   #'(lambda () (interactive)))
811   (global-set-key [M-S-mouse-3] 'imenu)
812   (global-set-key [XF86WakeUp] "")
813   (global-set-key [f11] 'mdw-toggle-full-screen)
814   (and (not mdw-fast-startup) (fboundp 'hippie-expand)
815        (global-set-key [?\M-?] 'hippie-expand)))
816
817 (eval-after-load "hippie-exp"
818   '(setq hippie-expand-try-functions-list
819            (cl-remove-if (lambda (name)
820                            (memq name '(try-expand-list
821                                         try-expand-list-all-buffers)))
822                          hippie-expand-try-functions-list)))
823
824 (autoload 'dired-jump "dired-x")
825 (autoload 'dired-jump-other-window "dired-x")
826
827 (eval-after-load "dired"
828   '(progn
829      (or (lookup-key dired-mode-map  [?\C-x ?\C-q])
830          (define-key dired-mode-map [?\C-x ?\C-q]
831            'wdired-change-to-wdired-mode))
832      (trap (require 'dired-x))
833      (and (fboundp 'dired-do-relsymlink)
834           (define-key dired-mode-map [?\C-c ?\C-s] 'dired-do-relsymlink))))
835 (setq dired-guess-shell-alist-user
836         '(("\\.pdf\\'" "mupdf")))
837
838 (add-hook 'org-mode-hook
839           #'(lambda () (mdw-clobber-evil-keymap org-mode-map)))
840 (add-hook 'org-agenda-mode-hook
841           #'(lambda () (mdw-clobber-evil-keymap org-agenda-mode-map)))
842 (or mdw-fast-startup
843     (trap (progn
844             (org-remember-insinuate)
845             (global-set-key [?\C-c ?g ?r] 'org-remember))))
846
847 ;; Minor mode listing
848
849 (setq minor-mode-alist
850         (mdw-uniquify-alist '((reveal-mode (hs-minor-mode
851                                             " hs/r"
852                                             (global-reveal-mode
853                                              "" " Reveal")))
854                               (hs-minor-mode (reveal-mode "" " hs"))
855                               (abbrev-mode "")
856                               (gtags-mode ""))
857                             minor-mode-alist))
858
859 ;; Recognising types of files.
860
861 (setq auto-mode-alist
862         (append `(("\\.p[lm]$" . cperl-mode)
863                   ("\\.sage$" . python-mode)
864                   ("\\.m$" . objc-mode)
865                   ("\\.mxd$" . c-mode)
866                   ("\\.cs$" . csharp-mode)
867                   ("\\.go$" . go-mode)
868                   ("\\.org$" . org-mode)
869                   ("\\.make$" . makefile-mode)
870                   ("\\.mk?d$" . markdown-mode)
871                   ;; ("/[ch]/" . c-mode)
872                   (,(concat "/\\("
873                             "\\.stgit\\.msg" "\\|"
874                             "\\.topmsg" "\\|"
875                             "\\.git/COMMIT_EDITMSG" "\\|"
876                             "svn-commit\\.tmp" "\\|"
877                             "svk-commit[^/.]*\\.tmp"
878                             "\\)$")
879                    . text-mode)
880                   (,(concat "^" tmpdir "/\\("
881                             "svk-commit[^/.]*\\.tmp" "\\|"
882                             "gitci\\.[^/.]*" "\\|"
883                             "cvs[^/.]\\{6\\}" "\\|"
884                             "quilt_header\.[^/.]\\{6\\}"
885                             "\\)$")
886                    . text-mode)
887                   ("\\.calc?$" . apcalc-mode)
888                   ("/\\(s\\|sh\\)/" . arm-assembler-mode)
889                   ("\\.\\(cmd\\|exec\\|rexx\\)$" . rexx-mode)
890                   ("\\.st$" . smalltalk-mode)
891                   ("\\.msgs$" . messages-mode)
892                   ("/all-cmds\\.in$" . cpp-messages-mode)
893                   ("\\.\\(tex\\|dtx\\)$" . latex-mode)
894                   ("\\.gc$" . haskell.-mode)
895                   (,(concat "^" (getenv "HOME") "/News/") . mdwmail-mode)
896                   (,(concat "^" tmpdir "/\\(SLRN\\|snd\\|pico\\|mutt\\)")
897                    . mdwmail-mode))
898                 auto-mode-alist))
899
900 (setq interpreter-mode-alist
901         (append `(("runlisp" . lisp-mode)
902                   ("@BASH@" . sh-mode)
903                   ("@PYTHON@" . python-mode)
904                   ("sage" . python-mode)
905                   ("@PERL@" . cperl-mode)
906                   ("perl" . cperl-mode)
907                   ("@TCLSH@" . tcl-mode)
908                   ("@WISH@" . tcl-mode)
909                   ("tclsh" . tcl-mode)
910                   ("wish" . tcl-mode))
911                 interpreter-mode-alist))
912
913 (setq completion-ignored-extensions
914         (append `(".hc" ".hi") completion-ignored-extensions))
915 (dolist (dir (cl-remove-if-not (lambda (ext)
916                                  (= (aref ext (- (length ext) 1)) ?/))
917                                completion-ignored-extensions))
918   (if (/= (aref dir 0) ?/)
919       (setq completion-ignored-extensions
920               (cons (concat "/" dir)
921                     (remove dir completion-ignored-extensions)))))
922
923 ;; Some common local definitions.
924
925 (make-variable-buffer-local 'mdw-auto-indent)
926
927 (global-font-lock-mode t)
928
929 ;;;--------------------------------------------------------------------------
930 ;;; Rootly editingness.
931
932 (eval-after-load "tramp"
933   '(let ((fix-args (if (mdw-version-< tramp-version "2.1")
934                        #'append #'list)))
935      (setq tramp-methods
936              (mdw-uniquify-alist
937               `(("become"
938                  (tramp-connection-function tramp-open-connection-su)
939                  (tramp-remote-sh "/bin/sh")
940                  (tramp-login-program "become")
941                  (tramp-copy-program nil)
942                  (tramp-copy-args nil)
943                  (tramp-copy-keep-date-arg nil)
944                  (tramp-login-args ,(funcall fix-args `("TERM=dumb" "%u"))))
945                 ("really"
946                  (tramp-connection-function tramp-open-connection-su)
947                  (tramp-login-program "really")
948                  (tramp-login-args ,(funcall fix-args
949                                              `("-u" "%u")
950                                              `("--")
951                                              `("env" "TERM=dumb" "/bin/sh")))
952                  (tramp-copy-program nil)
953                  (tramp-copy-args nil)
954                  (tramp-copy-keep-date-arg nil)
955                  (tramp-remote-sh "/bin/sh"))
956                 ,@tramp-methods)))
957      (setq tramp-default-method "ssh")
958      (let ((rootlyness (cond ((executable-find "really") "really")
959                              ((executable-find "become") "become")
960                              ((executable-find "sudo") "sudo")
961                              (t "su")))
962            (this-host (concat "\\`\\(localhost\\|"
963                               (system-name) "\\)?\\'"))
964            (this-user (concat "\\`\\(" (user-login-name) "\\|"
965                               (user-real-login-name) "\\)?\\'")))
966        (setq tramp-default-method-alist
967                `((,this-host nil ,rootlyness)
968                  (nil ,this-user "ssh")
969                  (nil "." ,rootlyness)))
970        (setq tramp-default-proxies-alist
971                `((,this-host nil nil)
972                  (nil "." "/ssh:%h:"))))))
973
974 ;;;--------------------------------------------------------------------------
975 ;;; General fontification.
976
977 ;; Configure lazy fontification.
978
979 (and (fboundp 'lazy-lock-mode)
980      (setq font-lock-support-mode 'lazy-lock-mode))
981 ; (setq lazy-lock-defer-contextually t)
982 (setq lazy-lock-defer-time nil
983       font-lock-maximum-decoration 3
984       lazy-lock-minimum-size 0
985       lazy-lock-stealth-time 5
986       lazy-lock-stealth-lines 100
987       lazy-lock-stealth-verbose t)
988
989 ;; Late, for `mdw-black-background'.
990 (setq-default py-python-command-args
991                 `("-i" "-colors" ,(if mdw-black-background "Linux" "LightBG")))
992
993 ;;;--------------------------------------------------------------------------
994 ;;; Blogging.
995
996 (setq weblogger-config-alist
997         '(("vox"
998            ("user" . "mdw")
999            ("server-url" . "http://vox.distorted.org.uk/admin/mt-xmlrpc.cgi")
1000            ("weblog" . "1"))))
1001
1002 ;;;--------------------------------------------------------------------------
1003 ;;; Shell mode.
1004
1005 ;; Make the shell mode aware of my prompt.
1006
1007 (setq shell-prompt-pattern "^[^]#$%>»}\n]*\\([]#$%»}]\\|>>?\\) *")
1008 (setq comint-password-prompt-regexp
1009         (concat "\\(\\([Ee]nter \\|[Oo]ld \\|[Nn]ew \\|[a-zA-Z0-9_]*'s \\|^\\)"
1010                 "[Pp]assword\\|[Pp]ass ?phrase\\( [-a-zA-Z0-9._]+\\)?\\):")
1011       comint-file-name-chars "~/A-Za-z0-9+@:_.$#%,={}-"
1012       shell-file-name-chars comint-file-name-chars)
1013
1014 ;; Notice passwords, and make C-a work right.
1015
1016 (add-hook 'shell-mode-hook #'mdw-sh-mode-setup)
1017 (add-hook 'shell-mode-hook #'ansi-color-for-comint-mode-on)
1018 (setq shell-font-lock-keywords nil)
1019
1020 (add-hook 'term-mode-hook #'mdw-term-mode-setup)
1021
1022 ;;;--------------------------------------------------------------------------
1023 ;;; Finishing touches.
1024
1025 (trap (select-window mdw-init-window))
1026 (provide 'emacs-init)
1027
1028 ;;;----- That's all, folks --------------------------------------------------