dot/emacs, el/dot-emacs.el: Move `display-buffer-reuse-frames' setting.
[profile] / el / dot-emacs.el
1 ;;; -*- mode: emacs-lisp; coding: utf-8 -*-
2 ;;;
3 ;;; Functions and macros for .emacs
4 ;;;
5 ;;; (c) 2004 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 Foundation,
22 ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
24 ;;;--------------------------------------------------------------------------
25 ;;; Check command-line.
26
27 (defgroup mdw nil
28 "Customization for mdw's Emacs configuration."
29 :prefix "mdw-")
30
31 (defun mdw-check-command-line-switch (switch)
32 (let ((probe nil) (next command-line-args) (found nil))
33 (while next
34 (cond ((string= (car next) switch)
35 (setq found t)
36 (if probe (rplacd probe (cdr next))
37 (setq command-line-args (cdr next))))
38 (t
39 (setq probe next)))
40 (setq next (cdr next)))
41 found))
42
43 (defvar mdw-fast-startup nil
44 "Whether .emacs should optimize for rapid startup.
45 This may be at the expense of cool features.")
46 (setq mdw-fast-startup
47 (mdw-check-command-line-switch "--mdw-fast-startup"))
48
49 (defvar mdw-splashy-startup nil
50 "Whether to show a splash screen and related frippery.")
51 (setq mdw-splashy-startup
52 (mdw-check-command-line-switch "--mdw-splashy-startup"))
53
54 ;;;--------------------------------------------------------------------------
55 ;;; Some general utilities.
56
57 (eval-when-compile
58 (unless (fboundp 'make-regexp) (load "make-regexp"))
59 (require 'cl))
60
61 (defmacro mdw-regexps (&rest list)
62 "Turn a LIST of strings into a single regular expression at compile-time."
63 (declare (indent nil)
64 (debug 0))
65 `',(make-regexp list))
66
67 (defun mdw-wrong ()
68 "This is not the key sequence you're looking for."
69 (interactive)
70 (error "wrong button"))
71
72 (defun mdw-emacs-version-p (major &optional minor)
73 "Return non-nil if the running Emacs is at least version MAJOR.MINOR."
74 (or (> emacs-major-version major)
75 (and (= emacs-major-version major)
76 (>= emacs-minor-version (or minor 0)))))
77
78 (defun mdw-submode-p (mode parent)
79 "Return non-nil if MODE is indirectly derived from PARENT."
80 (let ((answer nil))
81 (while (cond ((eq mode parent) (setq answer t) nil)
82 (t (setq mode (get mode 'derived-mode-parent)))))
83 answer))
84
85 ;; Some error trapping.
86 ;;
87 ;; If individual bits of this file go tits-up, we don't particularly want
88 ;; the whole lot to stop right there and then, because it's bloody annoying.
89
90 (eval-and-compile
91 (defmacro trap (&rest forms)
92 "Execute FORMS without allowing errors to propagate outside."
93 (declare (indent 0)
94 (debug t))
95 `(condition-case err
96 ,(if (cdr forms) (cons 'progn forms) (car forms))
97 (error (message "Error (trapped): %s in %s"
98 (error-message-string err)
99 ',forms)))))
100
101 ;; Configuration reading.
102
103 (defvar mdw-config nil)
104 (defun mdw-config (sym)
105 "Read the configuration variable named SYM."
106 (unless mdw-config
107 (setq mdw-config
108 (flet ((replace (what with)
109 (goto-char (point-min))
110 (while (re-search-forward what nil t)
111 (replace-match with t))))
112 (with-temp-buffer
113 (insert-file-contents "~/.mdw.conf")
114 (replace "^[ \t]*\\(#.*\\)?\n" "")
115 (replace (concat "^[ \t]*"
116 "\\([-a-zA-Z0-9_.]*\\)"
117 "[ \t]*=[ \t]*"
118 "\\(.*[^ \t\n]\\)?"
119 "[ \t]**\\(\n\\|$\\)")
120 "(\\1 . \"\\2\")\n")
121 (car (read-from-string
122 (concat "(" (buffer-string) ")")))))))
123 (cdr (assq sym mdw-config)))
124
125 ;; Width configuration.
126
127 (defcustom mdw-column-width
128 (string-to-number (or (mdw-config 'emacs-width) "77"))
129 "Width of Emacs columns."
130 :type 'integer)
131 (defcustom mdw-text-width mdw-column-width
132 "Expected width of text within columns."
133 :type 'integer
134 :safe 'integerp)
135
136 ;; Local variables hacking.
137
138 (defun run-local-vars-mode-hook ()
139 "Run a hook for the major-mode after local variables have been processed."
140 (run-hooks (intern (concat (symbol-name major-mode)
141 "-local-variables-hook"))))
142 (add-hook 'hack-local-variables-hook 'run-local-vars-mode-hook)
143
144 ;; Set up the load path convincingly.
145
146 (dolist (dir (append (and (boundp 'debian-emacs-flavor)
147 (list (concat "/usr/share/"
148 (symbol-name debian-emacs-flavor)
149 "/site-lisp")))))
150 (dolist (sub (directory-files dir t))
151 (when (and (file-accessible-directory-p sub)
152 (not (member sub load-path)))
153 (setq load-path (nconc load-path (list sub))))))
154
155 ;; Is an Emacs library available?
156
157 (defun library-exists-p (name)
158 "Return non-nil if NAME is an available library.
159 Return non-nil if NAME.el (or NAME.elc) somewhere on the Emacs
160 load path. The non-nil value is the filename we found for the
161 library."
162 (let ((path load-path) elt (foundp nil))
163 (while (and path (not foundp))
164 (setq elt (car path))
165 (setq path (cdr path))
166 (setq foundp (or (let ((file (concat elt "/" name ".elc")))
167 (and (file-exists-p file) file))
168 (let ((file (concat elt "/" name ".el")))
169 (and (file-exists-p file) file)))))
170 foundp))
171
172 (defun maybe-autoload (symbol file &optional docstring interactivep type)
173 "Set an autoload if the file actually exists."
174 (and (library-exists-p file)
175 (autoload symbol file docstring interactivep type)))
176
177 (defun mdw-kick-menu-bar (&optional frame)
178 "Regenerate FRAME's menu bar so it doesn't have empty menus."
179 (interactive)
180 (unless frame (setq frame (selected-frame)))
181 (let ((old (frame-parameter frame 'menu-bar-lines)))
182 (set-frame-parameter frame 'menu-bar-lines 0)
183 (set-frame-parameter frame 'menu-bar-lines old)))
184
185 ;; Page motion.
186
187 (defun mdw-fixup-page-position ()
188 (unless (eq (char-before (point)) ?\f)
189 (forward-line 0)))
190
191 (defadvice backward-page (after mdw-fixup compile activate)
192 (mdw-fixup-page-position))
193 (defadvice forward-page (after mdw-fixup compile activate)
194 (mdw-fixup-page-position))
195
196 ;; Splitting windows.
197
198 (unless (fboundp 'scroll-bar-columns)
199 (defun scroll-bar-columns (side)
200 (cond ((eq side 'left) 0)
201 (window-system 3)
202 (t 1))))
203 (unless (fboundp 'fringe-columns)
204 (defun fringe-columns (side)
205 (cond ((not window-system) 0)
206 ((eq side 'left) 1)
207 (t 2))))
208
209 (defun mdw-horizontal-window-overhead ()
210 "Computes the horizontal window overhead.
211 This is the number of columns used by fringes, scroll bars and other such
212 cruft."
213 (if (not window-system)
214 1
215 (let ((tot 0))
216 (dolist (what '(scroll-bar fringe))
217 (dolist (side '(left right))
218 (incf tot (funcall (intern (concat (symbol-name what) "-columns"))
219 side))))
220 tot)))
221
222 (defun mdw-split-window-horizontally (&optional width)
223 "Split a window horizontally.
224 Without a numeric argument, split the window approximately in
225 half. With a numeric argument WIDTH, allocate WIDTH columns to
226 the left-hand window (if positive) or -WIDTH columns to the
227 right-hand window (if negative). Space for scroll bars and
228 fringes is not taken out of the allowance for WIDTH, unlike
229 \\[split-window-horizontally]."
230 (interactive "P")
231 (split-window-horizontally
232 (cond ((null width) nil)
233 ((>= width 0) (+ width (mdw-horizontal-window-overhead)))
234 ((< width 0) width))))
235
236 (defun mdw-preferred-column-width ()
237 "Return the preferred column width."
238 (if (and window-system (mdw-emacs-version-p 22)) mdw-column-width
239 (1+ mdw-column-width)))
240
241 (defun mdw-divvy-window (&optional width)
242 "Split a wide window into appropriate widths."
243 (interactive "P")
244 (setq width (if width (prefix-numeric-value width)
245 (mdw-preferred-column-width)))
246 (let* ((win (selected-window))
247 (sb-width (mdw-horizontal-window-overhead))
248 (c (/ (+ (window-width) sb-width)
249 (+ width sb-width))))
250 (while (> c 1)
251 (setq c (1- c))
252 (split-window-horizontally (+ width sb-width))
253 (other-window 1))
254 (select-window win)))
255
256 (defun mdw-frame-width-for-columns (columns width)
257 "Return the preferred width for a frame with so many COLUMNS of WIDTH."
258 (let ((sb-width (mdw-horizontal-window-overhead)))
259 (- (* columns (+ width sb-width))
260 sb-width)))
261
262 (defun mdw-set-frame-width (columns &optional width)
263 "Set the current frame to be the correct width for COLUMNS columns.
264
265 If WIDTH is non-nil, then it provides the width for the new columns. (This
266 can be set interactively with a prefix argument.)"
267 (interactive "nColumns:
268 P")
269 (setq width (if width (prefix-numeric-value width)
270 (mdw-preferred-column-width)))
271 (set-frame-width (selected-frame)
272 (mdw-frame-width-for-columns columns width))
273 (mdw-divvy-window width))
274
275 (defcustom mdw-frame-width-fudge
276 (cond ((<= emacs-major-version 20) 1)
277 ((= emacs-major-version 26) 3)
278 (t 0))
279 "The number of extra columns to add to the desired frame width.
280
281 This is sadly necessary because Emacs 26 is broken in this regard."
282 :type 'integer)
283
284 (defcustom mdw-frame-colour-alist
285 '((black . ("#000000" . "#ffffff"))
286 (red . ("#2a0000" . "#ffffff"))
287 (green . ("#002a00" . "#ffffff"))
288 (blue . ("#00002a" . "#ffffff")))
289 "Alist mapping symbol names to (FOREGROUND . BACKGROUND) colour pairs."
290 :type '(alist :key-type symbol :value-type (cons color color)))
291
292 (defun mdw-set-frame-colour (colour &optional frame)
293 (interactive "xColour name or (FOREGROUND . BACKGROUND) pair:
294 ")
295 (when (and colour (symbolp colour))
296 (let ((entry (assq colour mdw-frame-colour-alist)))
297 (unless entry (error "Unknown colour `%s'" colour))
298 (setf colour (cdr entry))))
299 (set-frame-parameter frame 'background-color (car colour))
300 (set-frame-parameter frame 'foreground-color (cdr colour)))
301
302 ;; Window configuration switching.
303
304 (defvar mdw-current-window-configuration nil
305 "The current window configuration register name, or `nil'.")
306
307 (defun mdw-switch-window-configuration (register &optional no-save)
308 "Switch make REGISTER be the new current window configuration.
309 If a current window configuration register is established, and
310 NO-SAVE is nil, then save the current window configuration to
311 that register first.
312
313 Signal an error if the new register contains something other than
314 a window configuration. If the register is unset then save the
315 current window configuration to it immediately.
316
317 With one or three C-u, or an odd numeric prefix argument, set
318 NO-SAVE, so the previous window configuration register is left
319 unchanged.
320
321 With two or three C-u, or a prefix argument which is an odd
322 multiple of 2, just clear the record of the current window
323 configuration register, so that the next switch doesn't save the
324 prevailing configuration."
325 (interactive
326 (let ((arg current-prefix-arg))
327 (list (if (or (and (consp arg) (= (car arg) 16) (= (car arg) 64))
328 (and (integerp arg) (not (zerop (logand arg 2)))))
329 nil
330 (register-read-with-preview "Switch to window configuration: "))
331 (or (and (consp arg) (= (car arg) 4) (= (car arg) 64))
332 (and (integerp arg) (not (zerop (logand arg 1))))))))
333
334 (let ((previous mdw-current-window-configuration)
335 (current-windows (list (current-window-configuration)
336 (point-marker)))
337 (register-value (and register (get-register register))))
338 (when (and mdw-current-window-configuration (not no-save))
339 (set-register mdw-current-window-configuration current-windows))
340 (cond ((null register)
341 (setq mdw-current-window-configuration nil)
342 (if previous
343 (message "Left window configuration `%c'." previous)
344 (message "Nothing to do!")))
345 ((not (or (null register-value)
346 (and (consp register-value)
347 (window-configuration-p (car register-value))
348 (integer-or-marker-p (cadr register-value))
349 (null (caddr register-value)))))
350 (error "Register `%c' is not a window configuration" register))
351 (t
352 (cond ((null register-value)
353 (set-register register current-windows)
354 (message "Started new window configuration `%c'."
355 register))
356 (t
357 (set-window-configuration (car register-value))
358 (goto-char (cadr register-value))
359 (message "Switched to window configuration `%c'."
360 register)))
361 (setq mdw-current-window-configuration register)))))
362
363 ;; Don't raise windows unless I say so.
364
365 (defcustom mdw-inhibit-raise-frame nil
366 "Whether `raise-frame' should do nothing when the frame is mapped."
367 :type 'boolean)
368
369 (defadvice raise-frame
370 (around mdw-inhibit (&optional frame) activate compile)
371 "Don't actually do anything if `mdw-inhibit-raise-frame' is true, and the
372 frame is actually mapped on the screen."
373 (if mdw-inhibit-raise-frame
374 (make-frame-visible frame)
375 ad-do-it))
376
377 (defmacro mdw-advise-to-inhibit-raise-frame (function)
378 "Advise the FUNCTION not to raise frames, even if it wants to."
379 `(defadvice ,function
380 (around mdw-inhibit-raise (&rest hunoz) activate compile)
381 "Don't raise the window unless you have to."
382 (let ((mdw-inhibit-raise-frame t))
383 ad-do-it)))
384
385 (mdw-advise-to-inhibit-raise-frame select-frame-set-input-focus)
386 (mdw-advise-to-inhibit-raise-frame appt-disp-window)
387 (mdw-advise-to-inhibit-raise-frame mouse-select-window)
388
389 ;; Bug fix for markdown-mode, which breaks point positioning during
390 ;; `query-replace'.
391 (defadvice markdown-check-change-for-wiki-link
392 (around mdw-save-match activate compile)
393 "Save match data around the `markdown-mode' `after-change-functions' hook."
394 (save-match-data ad-do-it))
395
396 ;; Bug fix for `bbdb-canonicalize-address': on Emacs 24, `run-hook-with-args'
397 ;; always returns nil, with the result that all email addresses are lost.
398 ;; Replace the function entirely.
399 (defadvice bbdb-canonicalize-address
400 (around mdw-bug-fix activate compile)
401 "Don't use `run-hook-with-args', because that doesn't work."
402 (let ((net (ad-get-arg 0)))
403
404 ;; Make sure this is a proper hook list.
405 (if (functionp bbdb-canonicalize-net-hook)
406 (setq bbdb-canonicalize-net-hook (list bbdb-canonicalize-net-hook)))
407
408 ;; Iterate over the hooks until things converge.
409 (let ((donep nil))
410 (while (not donep)
411 (let (next (changep nil)
412 hook (hooks bbdb-canonicalize-net-hook))
413 (while hooks
414 (setq hook (pop hooks))
415 (setq next (funcall hook net))
416 (if (not (equal next net))
417 (setq changep t
418 net next)))
419 (setq donep (not changep)))))
420 (setq ad-return-value net)))
421
422 ;; Transient mark mode hacks.
423
424 (defadvice exchange-point-and-mark
425 (around mdw-highlight (&optional arg) activate compile)
426 "Maybe don't actually exchange point and mark.
427 If `transient-mark-mode' is on and the mark is inactive, then
428 just activate it. A non-trivial prefix argument will force the
429 usual behaviour. A trivial prefix argument (i.e., just C-u) will
430 activate the mark and temporarily enable `transient-mark-mode' if
431 it's currently off."
432 (cond ((or mark-active
433 (and (not transient-mark-mode) (not arg))
434 (and arg (or (not (consp arg))
435 (not (= (car arg) 4)))))
436 ad-do-it)
437 (t
438 (or transient-mark-mode (setq transient-mark-mode 'only))
439 (set-mark (mark t)))))
440
441 ;; Functions for sexp diary entries.
442
443 (defvar mdw-diary-for-org-mode-p nil
444 "Display diary along with the agenda?")
445
446 (defun mdw-not-org-mode (form)
447 "As FORM, but not in Org mode agenda."
448 (and (not mdw-diary-for-org-mode-p)
449 (eval form)))
450
451 (defun mdw-weekday (l)
452 "Return non-nil if `date' falls on one of the days of the week in L.
453 L is a list of day numbers (from 0 to 6 for Sunday through to
454 Saturday) or symbols `sunday', `monday', etc. (or a mixture). If
455 the date stored in `date' falls on a listed day, then the
456 function returns non-nil."
457 (let ((d (calendar-day-of-week date)))
458 (or (memq d l)
459 (memq (nth d '(sunday monday tuesday wednesday
460 thursday friday saturday)) l))))
461
462 (defun mdw-discordian-date (date)
463 "Return the Discordian calendar date corresponding to DATE.
464
465 The return value is (YOLD . st-tibs-day) or (YOLD SEASON DAYNUM DOW).
466
467 The original is by David Pearson. I modified it to produce date components
468 as output rather than a string."
469 (let* ((days ["Sweetmorn" "Boomtime" "Pungenday"
470 "Prickle-Prickle" "Setting Orange"])
471 (months ["Chaos" "Discord" "Confusion"
472 "Bureaucracy" "Aftermath"])
473 (day-count [0 31 59 90 120 151 181 212 243 273 304 334])
474 (year (- (calendar-extract-year date) 1900))
475 (month (1- (calendar-extract-month date)))
476 (day (1- (calendar-extract-day date)))
477 (julian (+ (aref day-count month) day))
478 (dyear (+ year 3066)))
479 (if (and (= month 1) (= day 28))
480 (cons dyear 'st-tibs-day)
481 (list dyear
482 (aref months (floor (/ julian 73)))
483 (1+ (mod julian 73))
484 (aref days (mod julian 5))))))
485
486 (defun mdw-diary-discordian-date ()
487 "Convert the date in `date' to a string giving the Discordian date."
488 (let* ((ddate (mdw-discordian-date date))
489 (tail (format "in the YOLD %d" (car ddate))))
490 (if (eq (cdr ddate) 'st-tibs-day)
491 (format "St Tib's Day %s" tail)
492 (let ((season (cadr ddate))
493 (daynum (caddr ddate))
494 (dayname (cadddr ddate)))
495 (format "%s, the %d%s day of %s %s"
496 dayname
497 daynum
498 (let ((ldig (mod daynum 10)))
499 (cond ((= ldig 1) "st")
500 ((= ldig 2) "nd")
501 ((= ldig 3) "rd")
502 (t "th")))
503 season
504 tail)))))
505
506 (defun mdw-todo (&optional when)
507 "Return non-nil today, or on WHEN, whichever is later."
508 (let ((w (calendar-absolute-from-gregorian (calendar-current-date)))
509 (d (calendar-absolute-from-gregorian date)))
510 (if when
511 (setq w (max w (calendar-absolute-from-gregorian
512 (cond
513 ((not european-calendar-style)
514 when)
515 ((> (car when) 100)
516 (list (nth 1 when)
517 (nth 2 when)
518 (nth 0 when)))
519 (t
520 (list (nth 1 when)
521 (nth 0 when)
522 (nth 2 when))))))))
523 (eq w d)))
524
525 (defadvice org-agenda-list (around mdw-preserve-links activate)
526 (let ((mdw-diary-for-org-mode-p t))
527 ad-do-it))
528
529 (defcustom diary-time-regexp nil
530 "Regexp matching times in the diary buffer."
531 :type 'regexp)
532
533 (defadvice diary-add-to-list (before mdw-trim-leading-space compile activate)
534 "Trim leading space from the diary entry string."
535 (save-match-data
536 (let ((str (ad-get-arg 1))
537 (done nil) old)
538 (while (not done)
539 (setq old str)
540 (setq str (cond ((null str) nil)
541 ((string-match "\\(^\\|\n\\)[ \t]+" str)
542 (replace-match "\\1" nil nil str))
543 ((and mdw-diary-for-org-mode-p
544 (string-match (concat
545 "\\(^\\|\n\\)"
546 "\\(" diary-time-regexp
547 "\\(-" diary-time-regexp "\\)?"
548 "\\)"
549 "\\(\t[ \t]*\\| [ \t]+\\)")
550 str))
551 (replace-match "\\1\\2 " nil nil str))
552 ((and (not mdw-diary-for-org-mode-p)
553 (string-match "\\[\\[[^][]*]\\[\\([^][]*\\)]]"
554 str))
555 (replace-match "\\1" nil nil str))
556 (t str)))
557 (if (equal str old) (setq done t)))
558 (ad-set-arg 1 str))))
559
560 (defadvice org-bbdb-anniversaries (after mdw-fixup-list compile activate)
561 "Return a string rather than a list."
562 (with-temp-buffer
563 (let ((anyp nil))
564 (dolist (e (let ((ee ad-return-value))
565 (if (atom ee) (list ee) ee)))
566 (when e
567 (when anyp (insert ?\n))
568 (insert e)
569 (setq anyp t)))
570 (setq ad-return-value
571 (and anyp (buffer-string))))))
572
573 ;; Fighting with Org-mode's evil key maps.
574
575 (defcustom mdw-evil-keymap-keys
576 '(([S-up] . [?\C-c up])
577 ([S-down] . [?\C-c down])
578 ([S-left] . [?\C-c left])
579 ([S-right] . [?\C-c right])
580 (([M-up] [?\e up]) . [C-up])
581 (([M-down] [?\e down]) . [C-down])
582 (([M-left] [?\e left]) . [C-left])
583 (([M-right] [?\e right]) . [C-right]))
584 "Defines evil keybindings to clobber in `mdw-clobber-evil-keymap'.
585 The value is an alist mapping evil keys (as a list, or singleton)
586 to good keys (in the same form)."
587 :type '(alist :key-type (choice key-sequence (repeat key-sequence))
588 :value-type key-sequence))
589
590 (defun mdw-clobber-evil-keymap (keymap)
591 "Replace evil key bindings in the KEYMAP.
592 Evil key bindings are defined in `mdw-evil-keymap-keys'."
593 (dolist (entry mdw-evil-keymap-keys)
594 (let ((binding nil)
595 (keys (if (listp (car entry))
596 (car entry)
597 (list (car entry))))
598 (replacements (if (listp (cdr entry))
599 (cdr entry)
600 (list (cdr entry)))))
601 (catch 'found
602 (dolist (key keys)
603 (setq binding (lookup-key keymap key))
604 (when binding
605 (throw 'found nil))))
606 (when binding
607 (dolist (key keys)
608 (define-key keymap key nil))
609 (dolist (key replacements)
610 (define-key keymap key binding))))))
611
612 (defcustom mdw-org-latex-defs
613 '(("strayman"
614 "\\documentclass{strayman}
615 \\usepackage[utf8]{inputenc}
616 \\usepackage[palatino, helvetica, courier, maths=cmr]{mdwfonts}
617 \\usepackage{graphicx, tikz, mdwtab, mdwmath, crypto, longtable}"
618 ("\\section{%s}" . "\\section*{%s}")
619 ("\\subsection{%s}" . "\\subsection*{%s}")
620 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
621 ("\\paragraph{%s}" . "\\paragraph*{%s}")
622 ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
623 "Additional LaTeX class definitions."
624 :type '(alist :key-type string
625 :value-type (list string
626 (alist :inline t
627 :key-type string
628 :value-type string))))
629
630 (eval-after-load "org-latex"
631 '(setq org-export-latex-classes
632 (append mdw-org-latex-defs org-export-latex-classes)))
633
634 (eval-after-load "ox-latex"
635 '(setq org-latex-classes (append mdw-org-latex-defs org-latex-classes)
636 org-latex-caption-above nil
637 org-latex-default-packages-alist '(("AUTO" "inputenc" t)
638 ("T1" "fontenc" t)
639 ("" "fixltx2e" nil)
640 ("" "graphicx" t)
641 ("" "longtable" nil)
642 ("" "float" nil)
643 ("" "wrapfig" nil)
644 ("" "rotating" nil)
645 ("normalem" "ulem" t)
646 ("" "textcomp" t)
647 ("" "marvosym" t)
648 ("" "wasysym" t)
649 ("" "amssymb" t)
650 ("" "hyperref" nil)
651 "\\tolerance=1000")))
652
653
654 (setq org-export-docbook-xslt-proc-command "xsltproc --output %o %s %i"
655 org-export-docbook-xsl-fo-proc-command "fop %i.safe %o"
656 org-export-docbook-xslt-stylesheet
657 "/usr/share/xml/docbook/stylesheet/docbook-xsl/fo/docbook.xsl")
658
659 ;; Glasses.
660
661 (setq glasses-separator "-"
662 glasses-separate-parentheses-p nil
663 glasses-uncapitalize-p t)
664
665 ;; Some hacks to do with window placement.
666
667 (defvar mdw-designated-window nil
668 "The window chosen by `mdw-designate-window', or nil.")
669
670 (defun mdw-designated-window-display-buffer-function (buffer not-this-window)
671 "Display buffer function to use the designated window."
672 (unless mdw-designated-window (error "No designated window!"))
673 (prog1 mdw-designated-window
674 (with-selected-window mdw-designated-window (switch-to-buffer buffer))
675 (setq mdw-designated-window nil
676 display-buffer-function nil)))
677
678 (defun mdw-display-buffer-in-designated-window (buffer alist)
679 "Display function to use the designated window."
680 (prog1 mdw-designated-window
681 (when mdw-designated-window
682 (with-selected-window mdw-designated-window
683 (switch-to-buffer buffer nil t)))
684 (setq mdw-designated-window nil)))
685
686 (defun mdw-designate-window (cancel)
687 "Use the selected window for the next pop-up buffer.
688 With a prefix argument, clear the designated window."
689 (interactive "P")
690 (let ((window (selected-window)))
691 (cond (cancel
692 (setq mdw-designated-window nil)
693 (unless (mdw-emacs-version-p 24)
694 (setq display-buffer-function nil))
695 (message "Window designation cleared."))
696 ((window-dedicated-p window)
697 (error "Window is dedicated to its buffer."))
698 (t
699 (setq mdw-designated-window window)
700 (unless (mdw-emacs-version-p 24)
701 (setq display-buffer-function
702 #'mdw-designated-window-display-buffer-function))
703 (message "Window designated.")))))
704
705 (when (mdw-emacs-version-p 24)
706 (setq display-buffer-base-action
707 (let* ((action display-buffer-base-action)
708 (funcs (car action))
709 (alist (cdr action)))
710 (cons (cons 'mdw-display-buffer-in-designated-window funcs)
711 alist))))
712
713 (defun mdw-clobber-other-windows-showing-buffer (buffer-or-name)
714 "Arrange that no windows on other frames are showing BUFFER-OR-NAME."
715 (interactive "bBuffer: ")
716 (let ((home-frame (selected-frame))
717 (buffer (get-buffer buffer-or-name))
718 (safe-buffer (get-buffer "*scratch*")))
719 (dolist (frame (frame-list))
720 (unless (eq frame home-frame)
721 (dolist (window (window-list frame))
722 (when (eq (window-buffer window) buffer)
723 (set-window-buffer window safe-buffer)))))))
724
725 (defvar mdw-inhibit-walk-windows nil
726 "If non-nil, then `walk-windows' does nothing.
727 This is used by advice on `switch-to-buffer-other-frame' to inhibit finding
728 buffers in random frames.")
729
730 (setq display-buffer--other-frame-action
731 '((display-buffer-reuse-window display-buffer-pop-up-frame)
732 (reusable-frames . nil)
733 (inhibit-same-window . t)))
734
735 (defadvice walk-windows (around mdw-inhibit activate)
736 "If `mdw-inhibit-walk-windows' is non-nil, then do nothing."
737 (and (not mdw-inhibit-walk-windows)
738 ad-do-it))
739
740 (defadvice switch-to-buffer-other-frame
741 (around mdw-always-new-frame activate)
742 "Always make a new frame.
743 Even if an existing window in some random frame looks tempting."
744 (let ((mdw-inhibit-walk-windows t)) ad-do-it))
745
746 (defadvice display-buffer (before mdw-inhibit-other-frames activate)
747 "Don't try to do anything fancy with other frames.
748 Pretend they don't exist. They might be on other display devices."
749 (ad-set-arg 2 nil))
750
751 (setq even-window-sizes nil
752 even-window-heights nil)
753
754 (setq display-buffer-reuse-frames nil)
755
756 ;; Rename buffers along with files.
757
758 (defvar mdw-inhibit-rename-buffer nil
759 "If non-nil, `rename-file' won't rename the buffer visiting the file.")
760
761 (defmacro mdw-advise-to-inhibit-rename-buffer (function)
762 "Advise FUNCTION to set `mdw-inhibit-rename-buffer' while it runs.
763
764 This will prevent `rename-file' from renaming the buffer."
765 `(defadvice ,function (around mdw-inhibit-rename-buffer compile activate)
766 "Don't rename the buffer when renaming the underlying file."
767 (let ((mdw-inhibit-rename-buffer t))
768 ad-do-it)))
769 (mdw-advise-to-inhibit-rename-buffer recode-file-name)
770 (mdw-advise-to-inhibit-rename-buffer set-visited-file-name)
771 (mdw-advise-to-inhibit-rename-buffer backup-buffer)
772
773 (defadvice rename-file (after mdw-rename-buffers (from to &optional forcep)
774 compile activate)
775 "If a buffer is visiting the file, rename it to match the new name.
776
777 Don't do this if `mdw-inhibit-rename-buffer' is non-nil."
778 (unless mdw-inhibit-rename-buffer
779 (let ((buffer (get-file-buffer from)))
780 (when buffer
781 (let ((to (if (not (string= (file-name-nondirectory to) "")) to
782 (concat to (file-name-nondirectory from)))))
783 (with-current-buffer buffer
784 (set-visited-file-name to nil t)))))))
785
786 ;;;--------------------------------------------------------------------------
787 ;;; Improved compilation machinery.
788
789 ;; Uprated version of M-x compile.
790
791 (setq compile-command
792 (let ((ncpu (with-temp-buffer
793 (insert-file-contents "/proc/cpuinfo")
794 (buffer-string)
795 (count-matches "^processor\\s-*:"))))
796 (format "nice make -j%d -k" (* 2 ncpu))))
797
798 (defun mdw-compilation-buffer-name (mode)
799 (concat "*" (downcase mode) ": "
800 (abbreviate-file-name default-directory) "*"))
801 (setq compilation-buffer-name-function 'mdw-compilation-buffer-name)
802
803 (eval-after-load "compile"
804 '(progn
805 (define-key compilation-shell-minor-mode-map "\C-c\M-g" 'recompile)))
806
807 (defadvice compile (around hack-environment compile activate)
808 "Hack the environment inherited by inferiors in the compilation."
809 (let ((process-environment (copy-tree process-environment)))
810 (setenv "LD_PRELOAD" nil)
811 ad-do-it))
812
813 (defun mdw-compile (command &optional directory comint)
814 "Initiate a compilation COMMAND, maybe in a different DIRECTORY.
815 The DIRECTORY may be nil to not change. If COMINT is t, then
816 start an interactive compilation.
817
818 Interactively, prompt for the command if the variable
819 `compilation-read-command' is non-nil, or if requested through
820 the prefix argument. Prompt for the directory, and run
821 interactively, if requested through the prefix.
822
823 Use a prefix of 4, 6, 12, or 14, or type C-u between one and three times, to
824 force prompting for a directory.
825
826 Use a prefix of 2, 6, 10, or 14, or type C-u three times, to force
827 prompting for the command.
828
829 Use a prefix of 8, 10, 12, or 14, or type C-u twice or three times,
830 to force interactive compilation."
831 (interactive
832 (let* ((prefix (prefix-numeric-value current-prefix-arg))
833 (command (eval compile-command))
834 (dir (and (plusp (logand prefix #x54))
835 (read-directory-name "Compile in directory: "))))
836 (list (if (or compilation-read-command
837 (plusp (logand prefix #x42)))
838 (compilation-read-command command)
839 command)
840 dir
841 (plusp (logand prefix #x58)))))
842 (let ((default-directory (or directory default-directory)))
843 (compile command comint)))
844
845 ;; Flymake support.
846
847 (defun mdw-find-build-dir (build-file)
848 (catch 'found
849 (let* ((src-dir (file-name-as-directory (expand-file-name ".")))
850 (dir src-dir))
851 (loop
852 (when (file-exists-p (concat dir build-file))
853 (throw 'found dir))
854 (let ((sub (expand-file-name (file-relative-name src-dir dir)
855 (concat dir "build/"))))
856 (catch 'give-up
857 (loop
858 (when (file-exists-p (concat sub build-file))
859 (throw 'found sub))
860 (when (string= sub dir) (throw 'give-up nil))
861 (setq sub (file-name-directory (directory-file-name sub))))))
862 (when (string= dir
863 (setq dir (file-name-directory
864 (directory-file-name dir))))
865 (throw 'found nil))))))
866
867 (defun mdw-flymake-make-init ()
868 (let ((build-dir (mdw-find-build-dir "Makefile")))
869 (and build-dir
870 (let ((tmp-src (flymake-init-create-temp-buffer-copy
871 #'flymake-create-temp-inplace)))
872 (flymake-get-syntax-check-program-args
873 tmp-src build-dir t t
874 #'flymake-get-make-cmdline)))))
875
876 (setq flymake-allowed-file-name-masks
877 '(("\\.\\(?:[cC]\\|cc\\|cpp\\|cxx\\|c\\+\\+\\)\\'"
878 mdw-flymake-make-init)
879 ("\\.\\(?:[hH]\\|hh\\|hpp\\|hxx\\|h\\+\\+\\)\\'"
880 mdw-flymake-master-make-init)
881 ("\\.p[lm]" flymake-perl-init)))
882
883 (setq flymake-mode-map
884 (let ((map (if (boundp 'flymake-mode-map)
885 flymake-mode-map
886 (make-sparse-keymap))))
887 (define-key map [?\C-c ?\C-f ?\C-p] 'flymake-goto-prev-error)
888 (define-key map [?\C-c ?\C-f ?\C-n] 'flymake-goto-next-error)
889 (define-key map [?\C-c ?\C-f ?\C-c] 'flymake-compile)
890 (define-key map [?\C-c ?\C-f ?\C-k] 'flymake-stop-all-syntax-checks)
891 (define-key map [?\C-c ?\C-f ?\C-e] 'flymake-popup-current-error-menu)
892 map))
893
894 ;;;--------------------------------------------------------------------------
895 ;;; Mail and news hacking.
896
897 (define-derived-mode mdwmail-mode mail-mode "[mdw] mail"
898 "Major mode for editing news and mail messages from external programs.
899 Not much right now. Just support for doing MailCrypt stuff."
900 :syntax-table nil
901 :abbrev-table nil
902 (run-hooks 'mail-setup-hook))
903
904 (define-key mdwmail-mode-map [?\C-c ?\C-c] 'disabled-operation)
905
906 (add-hook 'mdwail-mode-hook
907 (lambda ()
908 (set-buffer-file-coding-system 'utf-8)
909 (make-local-variable 'paragraph-separate)
910 (make-local-variable 'paragraph-start)
911 (setq paragraph-start
912 (concat "[ \t]*[-_][-_][-_]+$\\|^-- \\|-----\\|"
913 paragraph-start))
914 (setq paragraph-separate
915 (concat "[ \t]*[-_][-_][-_]+$\\|^-- \\|-----\\|"
916 paragraph-separate))))
917
918 ;; How to encrypt in mdwmail.
919
920 (defun mdwmail-mc-encrypt (&optional recip scm start end from sign)
921 (or start
922 (setq start (save-excursion
923 (goto-char (point-min))
924 (or (search-forward "\n\n" nil t) (point-min)))))
925 (or end
926 (setq end (point-max)))
927 (mc-encrypt-generic recip scm start end from sign))
928
929 ;; How to sign in mdwmail.
930
931 (defun mdwmail-mc-sign (key scm start end uclr)
932 (or start
933 (setq start (save-excursion
934 (goto-char (point-min))
935 (or (search-forward "\n\n" nil t) (point-min)))))
936 (or end
937 (setq end (point-max)))
938 (mc-sign-generic key scm start end uclr))
939
940 ;; Some signature mangling.
941
942 (defun mdwmail-mangle-signature ()
943 (save-excursion
944 (goto-char (point-min))
945 (perform-replace "\n-- \n" "\n-- " nil nil nil)))
946 (add-hook 'mail-setup-hook 'mdwmail-mangle-signature)
947 (add-hook 'message-setup-hook 'mdwmail-mangle-signature)
948
949 ;; Insert my login name into message-ids, so I can score replies.
950
951 (defadvice message-unique-id (after mdw-user-name last activate compile)
952 "Ensure that the user's name appears at the end of the message-id string,
953 so that it can be used for convenient filtering."
954 (setq ad-return-value (concat ad-return-value "." (user-login-name))))
955
956 ;; Tell my movemail hack where movemail is.
957 ;;
958 ;; This is needed to shup up warnings about LD_PRELOAD.
959
960 (let ((path exec-path))
961 (while path
962 (let ((try (expand-file-name "movemail" (car path))))
963 (if (file-executable-p try)
964 (setenv "REAL_MOVEMAIL" try))
965 (setq path (cdr path)))))
966
967 ;; AUTHINFO GENERIC kludge.
968
969 (defcustom nntp-authinfo-generic nil
970 "Set to the `NNTPAUTH' string to pass on to `authinfo-kludge'.
971
972 Use this to arrange for per-server settings."
973 :type '(choice (const :tag "Use `NNTPAUTH' environment variable" nil)
974 string)
975 :safe 'stringp)
976
977 (defun nntp-open-authinfo-kludge (buffer)
978 "Open a connection to SERVER using `authinfo-kludge'."
979 (let ((proc (start-process "nntpd" buffer
980 "env" (concat "NNTPAUTH="
981 (or nntp-authinfo-generic
982 (getenv "NNTPAUTH")
983 (error "NNTPAUTH unset")))
984 "authinfo-kludge" nntp-address)))
985 (set-buffer buffer)
986 (nntp-wait-for-string "^\r*200")
987 (beginning-of-line)
988 (delete-region (point-min) (point))
989 proc))
990
991 (eval-after-load "erc"
992 '(load "~/.ercrc.el"))
993
994 ;; Heavy-duty Gnus patching.
995
996 (defun mdw-nnimap-transform-headers ()
997 (goto-char (point-min))
998 (let (article lines size string)
999 (block nil
1000 (while (not (eobp))
1001 (while (not (looking-at "\\* [0-9]+ FETCH"))
1002 (delete-region (point) (progn (forward-line 1) (point)))
1003 (when (eobp)
1004 (return)))
1005 (goto-char (match-end 0))
1006 ;; Unfold quoted {number} strings.
1007 (while (re-search-forward
1008 "[^]][ (]{\\([0-9]+\\)}\r?\n"
1009 (save-excursion
1010 ;; Start of the header section.
1011 (or (re-search-forward "] {[0-9]+}\r?\n" nil t)
1012 ;; Start of the next FETCH.
1013 (re-search-forward "\\* [0-9]+ FETCH" nil t)
1014 (point-max)))
1015 t)
1016 (setq size (string-to-number (match-string 1)))
1017 (delete-region (+ (match-beginning 0) 2) (point))
1018 (setq string (buffer-substring (point) (+ (point) size)))
1019 (delete-region (point) (+ (point) size))
1020 (insert (format "%S" (subst-char-in-string ?\n ?\s string)))
1021 ;; [mdw] missing from upstream
1022 (backward-char 1))
1023 (beginning-of-line)
1024 (setq article
1025 (and (re-search-forward "UID \\([0-9]+\\)"
1026 (line-end-position)
1027 t)
1028 (match-string 1)))
1029 (setq lines nil)
1030 (setq size
1031 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
1032 (line-end-position)
1033 t)
1034 (match-string 1)))
1035 (beginning-of-line)
1036 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
1037 (let ((structure (ignore-errors
1038 (read (current-buffer)))))
1039 (while (and (consp structure)
1040 (not (atom (car structure))))
1041 (setq structure (car structure)))
1042 (setq lines (if (and
1043 (stringp (car structure))
1044 (equal (upcase (nth 0 structure)) "MESSAGE")
1045 (equal (upcase (nth 1 structure)) "RFC822"))
1046 (nth 9 structure)
1047 (nth 7 structure)))))
1048 (delete-region (line-beginning-position) (line-end-position))
1049 (insert (format "211 %s Article retrieved." article))
1050 (forward-line 1)
1051 (when size
1052 (insert (format "Chars: %s\n" size)))
1053 (when lines
1054 (insert (format "Lines: %s\n" lines)))
1055 ;; Most servers have a blank line after the headers, but
1056 ;; Davmail doesn't.
1057 (unless (re-search-forward "^\r$\\|^)\r?$" nil t)
1058 (goto-char (point-max)))
1059 (delete-region (line-beginning-position) (line-end-position))
1060 (insert ".")
1061 (forward-line 1)))))
1062
1063 (eval-after-load 'nnimap
1064 '(defalias 'nnimap-transform-headers
1065 (symbol-function 'mdw-nnimap-transform-headers)))
1066
1067 (defadvice gnus-other-frame (around mdw-hack-frame-width compile activate)
1068 "Always arrange for mail/news frames to be 80 columns wide."
1069 (let ((default-frame-alist (cons `(width . ,(+ 80 mdw-frame-width-fudge))
1070 (delete* 'width default-frame-alist
1071 :key #'car))))
1072 ad-do-it))
1073
1074 ;; Preferred programs.
1075
1076 (setq mailcap-user-mime-data
1077 '(((type . "application/pdf") (viewer . "mupdf %s"))))
1078
1079 ;;;--------------------------------------------------------------------------
1080 ;;; Utility functions.
1081
1082 (or (fboundp 'line-number-at-pos)
1083 (defun line-number-at-pos (&optional pos)
1084 (let ((opoint (or pos (point))) start)
1085 (save-excursion
1086 (save-restriction
1087 (goto-char (point-min))
1088 (widen)
1089 (forward-line 0)
1090 (setq start (point))
1091 (goto-char opoint)
1092 (forward-line 0)
1093 (1+ (count-lines 1 (point))))))))
1094
1095 (defun mdw-uniquify-alist (&rest alists)
1096 "Return the concatenation of the ALISTS with duplicate elements removed.
1097 The first association with a given key prevails; others are
1098 ignored. The input lists are not modified, although they'll
1099 probably become garbage."
1100 (and alists
1101 (let ((start-list (cons nil nil)))
1102 (mdw-do-uniquify start-list
1103 start-list
1104 (car alists)
1105 (cdr alists)))))
1106
1107 (defun mdw-do-uniquify (done end l rest)
1108 "A helper function for mdw-uniquify-alist.
1109 The DONE argument is a list whose first element is `nil'. It
1110 contains the uniquified alist built so far. The leading `nil' is
1111 stripped off at the end of the operation; it's only there so that
1112 DONE always references a cons cell. END refers to the final cons
1113 cell in the DONE list; it is modified in place each time to avoid
1114 the overheads of `append'ing all the time. The L argument is the
1115 alist we're currently processing; the remaining alists are given
1116 in REST."
1117
1118 ;; There are several different cases to deal with here.
1119 (cond
1120
1121 ;; Current list isn't empty. Add the first item to the DONE list if
1122 ;; there's not an item with the same KEY already there.
1123 (l (or (assoc (car (car l)) done)
1124 (progn
1125 (setcdr end (cons (car l) nil))
1126 (setq end (cdr end))))
1127 (mdw-do-uniquify done end (cdr l) rest))
1128
1129 ;; The list we were working on is empty. Shunt the next list into the
1130 ;; current list position and go round again.
1131 (rest (mdw-do-uniquify done end (car rest) (cdr rest)))
1132
1133 ;; Everything's done. Remove the leading `nil' from the DONE list and
1134 ;; return it. Finished!
1135 (t (cdr done))))
1136
1137 (defun date ()
1138 "Insert the current date in a pleasing way."
1139 (interactive)
1140 (insert (save-excursion
1141 (let ((buffer (get-buffer-create "*tmp*")))
1142 (unwind-protect (progn (set-buffer buffer)
1143 (erase-buffer)
1144 (shell-command "date +%Y-%m-%d" t)
1145 (goto-char (mark))
1146 (delete-char -1)
1147 (buffer-string))
1148 (kill-buffer buffer))))))
1149
1150 (defun uuencode (file &optional name)
1151 "UUencodes a file, maybe calling it NAME, into the current buffer."
1152 (interactive "fInput file name: ")
1153
1154 ;; If NAME isn't specified, then guess from the filename.
1155 (if (not name)
1156 (setq name
1157 (substring file
1158 (or (string-match "[^/]*$" file) 0))))
1159 (print (format "uuencode `%s' `%s'" file name))
1160
1161 ;; Now actually do the thing.
1162 (call-process "uuencode" file t nil name))
1163
1164 (defcustom np-file "~/.np"
1165 "Where the `now-playing' file is."
1166 :type 'file
1167 :safe 'stringp)
1168
1169 (defun np (&optional arg)
1170 "Grabs a `now-playing' string."
1171 (interactive)
1172 (save-excursion
1173 (or arg (progn
1174 (goto-char (point-max))
1175 (insert "\nNP: ")
1176 (insert-file-contents np-file)))))
1177
1178 (defun mdw-version-< (ver-a ver-b)
1179 "Answer whether VER-A is strictly earlier than VER-B.
1180 VER-A and VER-B are version numbers, which are strings containing digit
1181 sequences separated by `.'."
1182 (let* ((la (mapcar (lambda (x) (car (read-from-string x)))
1183 (split-string ver-a "\\.")))
1184 (lb (mapcar (lambda (x) (car (read-from-string x)))
1185 (split-string ver-b "\\."))))
1186 (catch 'done
1187 (while t
1188 (cond ((null la) (throw 'done lb))
1189 ((null lb) (throw 'done nil))
1190 ((< (car la) (car lb)) (throw 'done t))
1191 ((= (car la) (car lb)) (setq la (cdr la) lb (cdr lb)))
1192 (t (throw 'done nil)))))))
1193
1194 (defun mdw-check-autorevert ()
1195 "Sets global-auto-revert-ignore-buffer appropriately for this buffer.
1196 This takes into consideration whether it's been found using
1197 tramp, which seems to get itself into a twist."
1198 (cond ((not (boundp 'global-auto-revert-ignore-buffer))
1199 nil)
1200 ((and (buffer-file-name)
1201 (fboundp 'tramp-tramp-file-p)
1202 (tramp-tramp-file-p (buffer-file-name)))
1203 (unless global-auto-revert-ignore-buffer
1204 (setq global-auto-revert-ignore-buffer 'tramp)))
1205 ((eq global-auto-revert-ignore-buffer 'tramp)
1206 (setq global-auto-revert-ignore-buffer nil))))
1207
1208 (defadvice find-file (after mdw-autorevert activate)
1209 (mdw-check-autorevert))
1210 (defadvice write-file (after mdw-autorevert activate)
1211 (mdw-check-autorevert))
1212
1213 (defun mdw-auto-revert ()
1214 "Recheck all of the autorevertable buffers, and update VC modelines."
1215 (interactive)
1216 (let ((auto-revert-check-vc-info t))
1217 (auto-revert-buffers)))
1218
1219 ;;;--------------------------------------------------------------------------
1220 ;;; Dired hacking.
1221
1222 (defadvice dired-maybe-insert-subdir
1223 (around mdw-marked-insertion first activate)
1224 "The DIRNAME may be a list of directory names to insert.
1225 Interactively, if files are marked, then insert all of them.
1226 With a numeric prefix argument, select that many entries near
1227 point; with a non-numeric prefix argument, prompt for listing
1228 options."
1229 (interactive
1230 (list (dired-get-marked-files nil
1231 (and (integerp current-prefix-arg)
1232 current-prefix-arg)
1233 #'file-directory-p)
1234 (and current-prefix-arg
1235 (not (integerp current-prefix-arg))
1236 (read-string "Switches for listing: "
1237 (or dired-subdir-switches
1238 dired-actual-switches)))))
1239 (let ((dirs (ad-get-arg 0)))
1240 (dolist (dir (if (listp dirs) dirs (list dirs)))
1241 (ad-set-arg 0 dir)
1242 ad-do-it)))
1243
1244 (defun mdw-dired-run (args &optional syncp)
1245 (interactive (let ((file (dired-get-filename t)))
1246 (list (read-string (format "Arguments for %s: " file))
1247 current-prefix-arg)))
1248 (funcall (if syncp 'shell-command 'async-shell-command)
1249 (concat (shell-quote-argument (dired-get-filename nil))
1250 " " args)))
1251
1252 (defadvice dired-do-flagged-delete
1253 (around mdw-delete-if-prefix-argument activate compile)
1254 (let ((delete-by-moving-to-trash (and (null current-prefix-arg)
1255 delete-by-moving-to-trash)))
1256 ad-do-it))
1257
1258 (eval-after-load "dired"
1259 '(define-key dired-mode-map "X" 'mdw-dired-run))
1260
1261 ;;;--------------------------------------------------------------------------
1262 ;;; URL viewing.
1263
1264 (defun mdw-w3m-browse-url (url &optional new-session-p)
1265 "Invoke w3m on the URL in its current window, or at least a different one.
1266 If NEW-SESSION-P, start a new session."
1267 (interactive "sURL: \nP")
1268 (save-excursion
1269 (let ((window (selected-window)))
1270 (unwind-protect
1271 (progn
1272 (select-window (or (and (not new-session-p)
1273 (get-buffer-window "*w3m*"))
1274 (progn
1275 (if (one-window-p t) (split-window))
1276 (get-lru-window))))
1277 (w3m-browse-url url new-session-p))
1278 (select-window window)))))
1279
1280 (eval-after-load 'w3m
1281 '(define-key w3m-mode-map [?\e ?\r] 'w3m-view-this-url-new-session))
1282
1283 (defcustom mdw-good-url-browsers
1284 '(browse-url-mozilla
1285 browse-url-generic
1286 (w3m . mdw-w3m-browse-url)
1287 browse-url-w3)
1288 "List of good browsers for mdw-good-url-browsers.
1289 Each item is a browser function name, or a cons (CHECK . FUNC).
1290 A symbol FOO stands for (FOO . FOO)."
1291 :type '(repeat (choice function (cons function function))))
1292
1293 (defun mdw-good-url-browser ()
1294 "Return a good URL browser.
1295 Trundle the list of such things, finding the first item for which
1296 CHECK is fboundp, and returning the correponding FUNC."
1297 (let ((bs mdw-good-url-browsers) b check func answer)
1298 (while (and bs (not answer))
1299 (setq b (car bs)
1300 bs (cdr bs))
1301 (if (consp b)
1302 (setq check (car b) func (cdr b))
1303 (setq check b func b))
1304 (if (fboundp check)
1305 (setq answer func)))
1306 answer))
1307
1308 (eval-after-load "w3m-search"
1309 '(progn
1310 (dolist
1311 (item
1312 '(("g" "Google" "http://www.google.co.uk/search?q=%s")
1313 ("gd" "Google Directory"
1314 "http://www.google.com/search?cat=gwd/Top&q=%s")
1315 ("gg" "Google Groups" "http://groups.google.com/groups?q=%s")
1316 ("ward" "Ward's wiki" "http://c2.com/cgi/wiki?%s")
1317 ("gi" "Images" "http://images.google.com/images?q=%s")
1318 ("rfc" "RFC"
1319 "http://metalzone.distorted.org.uk/ftp/pub/mirrors/rfc/rfc%s.txt.gz")
1320 ("wp" "Wikipedia"
1321 "http://en.wikipedia.org/wiki/Special:Search?go=Go&search=%s")
1322 ("imdb" "IMDb" "http://www.imdb.com/Find?%s")
1323 ("nc-wiki" "nCipher wiki"
1324 "http://wiki.ncipher.com/wiki/bin/view/Devel/?topic=%s")
1325 ("map" "Google maps" "http://maps.google.co.uk/maps?q=%s&hl=en")
1326 ("lp" "Launchpad bug by number"
1327 "https://bugs.launchpad.net/bugs/%s")
1328 ("lppkg" "Launchpad bugs by package"
1329 "https://bugs.launchpad.net/%s")
1330 ("msdn" "MSDN"
1331 "http://social.msdn.microsoft.com/Search/en-GB/?query=%s&ac=8")
1332 ("debbug" "Debian bug by number"
1333 "http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=%s")
1334 ("debbugpkg" "Debian bugs by package"
1335 "http://bugs.debian.org/cgi-bin/pkgreport.cgi?pkg=%s")
1336 ("ljlogin" "LJ login" "http://www.livejournal.com/login.bml")))
1337 (add-to-list 'w3m-search-engine-alist
1338 (list (cadr item) (caddr item) nil))
1339 (add-to-list 'w3m-uri-replace-alist
1340 (list (concat "\\`" (car item) ":")
1341 'w3m-search-uri-replace
1342 (cadr item))))))
1343
1344 ;;;--------------------------------------------------------------------------
1345 ;;; Paragraph filling.
1346
1347 ;; Useful variables.
1348
1349 (defcustom mdw-fill-prefix nil
1350 "Used by `mdw-line-prefix' and `mdw-fill-paragraph'.
1351 If there's no fill prefix currently set (by the `fill-prefix'
1352 variable) and there's a match from one of the regexps here, it
1353 gets used to set the fill-prefix for the current operation.
1354
1355 The variable is a list of items of the form `PATTERN . PREFIX'; if
1356 the PATTERN matches, the PREFIX is used to set the fill prefix.
1357
1358 A PATTERN is one of the following.
1359
1360 * STRING -- a regular expression, expected to match at point
1361 * (eval . FORM) -- a Lisp form which must evaluate non-nil
1362 * (if COND CONSEQ-PAT ALT-PAT) -- if COND evaluates non-nil, must match
1363 CONSEQ-PAT; otherwise must match ALT-PAT
1364 * (and PATTERN ...) -- must match all of the PATTERNs
1365 * (or PATTERN ...) -- must match at least one PATTERN
1366 * (not PATTERN) -- mustn't match (probably not useful)
1367
1368 A PREFIX is a list of the following kinds of things:
1369
1370 * STRING -- insert a literal string
1371 * (match . N) -- insert the thing matched by bracketed subexpression N
1372 * (pad . N) -- a string of whitespace the same width as subexpression N
1373 * (expr . FORM) -- the result of evaluating FORM
1374
1375 Information about `bracketed subexpressions' comes from the match data,
1376 as modified during matching.")
1377
1378 (make-variable-buffer-local 'mdw-fill-prefix)
1379
1380 (defcustom mdw-hanging-indents
1381 (concat "\\(\\("
1382 "\\([*o+]\\|-[-#]?\\|[0-9]+\\.\\|\\[[0-9]+\\]\\|([a-zA-Z])\\)"
1383 "[ \t]+"
1384 "\\)?\\)")
1385 "Standard regexp matching parts of a hanging indent.
1386 This is mainly useful in `auto-fill-mode'."
1387 :type 'regexp)
1388
1389 ;; Utility functions.
1390
1391 (defun mdw-maybe-tabify (s)
1392 "Tabify or untabify the string S, according to `indent-tabs-mode'."
1393 (let ((tabfun (if indent-tabs-mode #'tabify #'untabify)))
1394 (with-temp-buffer
1395 (save-match-data
1396 (insert s "\n")
1397 (let ((start (point-min)) (end (point-max)))
1398 (funcall tabfun (point-min) (point-max))
1399 (setq s (buffer-substring (point-min) (1- (point-max)))))))))
1400
1401 (defun mdw-fill-prefix-match-p (pat)
1402 "Return non-nil if PAT matches at the current position."
1403 (cond ((stringp pat) (looking-at pat))
1404 ((not (consp pat)) (error "Unknown pattern item `%S'" pat))
1405 ((eq (car pat) 'eval) (eval (cdr pat)))
1406 ((eq (car pat) 'if)
1407 (if (or (null (cdr pat))
1408 (null (cddr pat))
1409 (null (cdddr pat))
1410 (cddddr pat))
1411 (error "Invalid `if' pattern `%S'" pat))
1412 (mdw-fill-prefix-match-p (if (eval (cadr pat))
1413 (caddr pat)
1414 (cadddr pat))))
1415 ((eq (car pat) 'and)
1416 (let ((pats (cdr pat))
1417 (ok t))
1418 (while (and pats
1419 (or (mdw-fill-prefix-match-p (car pats))
1420 (setq ok nil)))
1421 (setq pats (cdr pats)))
1422 ok))
1423 ((eq (car pat) 'or)
1424 (let ((pats (cdr pat))
1425 (ok nil))
1426 (while (and pats
1427 (or (not (mdw-fill-prefix-match-p (car pats)))
1428 (progn (setq ok t) nil)))
1429 (setq pats (cdr pats)))
1430 ok))
1431 ((eq (car pat) 'not)
1432 (if (or (null (cdr pat)) (cddr pat))
1433 (error "Invalid `not' pattern `%S'" pat))
1434 (not (mdw-fill-prefix-match-p (car pats))))
1435 (t (error "Unknown pattern form `%S'" pat))))
1436
1437 (defun mdw-maybe-car (p)
1438 "If P is a pair, return (car P), otherwise just return P."
1439 (if (consp p) (car p) p))
1440
1441 (defun mdw-padding (s)
1442 "Return a string the same width as S but made entirely from whitespace."
1443 (let* ((l (length s)) (i 0) (n (make-string l ? )))
1444 (while (< i l)
1445 (if (= 9 (aref s i))
1446 (aset n i 9))
1447 (setq i (1+ i)))
1448 n))
1449
1450 (defun mdw-do-prefix-match (m)
1451 "Expand a dynamic prefix match element.
1452 See `mdw-fill-prefix' for details."
1453 (cond ((not (consp m)) (format "%s" m))
1454 ((eq (car m) 'match) (match-string (mdw-maybe-car (cdr m))))
1455 ((eq (car m) 'pad) (mdw-padding (match-string
1456 (mdw-maybe-car (cdr m)))))
1457 ((eq (car m) 'eval) (eval (cdr m)))
1458 (t "")))
1459
1460 (defun mdw-examine-fill-prefixes (l)
1461 "Given a list of dynamic fill prefixes, pick one which matches
1462 context and return the static fill prefix to use. Point must be
1463 at the start of a line, and match data must be saved."
1464 (let ((prefix nil))
1465 (while (cond ((null l) nil)
1466 ((mdw-fill-prefix-match-p (caar l))
1467 (setq prefix
1468 (mdw-maybe-tabify
1469 (apply #'concat
1470 (mapcar #'mdw-do-prefix-match
1471 (cdr (car l))))))
1472 nil))
1473 (setq l (cdr l)))
1474 prefix))
1475
1476 (defun mdw-choose-dynamic-fill-prefix ()
1477 "Work out the dynamic fill prefix based on the variable `mdw-fill-prefix'."
1478 (cond ((and fill-prefix (not (string= fill-prefix ""))) fill-prefix)
1479 ((not mdw-fill-prefix) fill-prefix)
1480 (t (save-excursion
1481 (beginning-of-line)
1482 (save-match-data
1483 (mdw-examine-fill-prefixes mdw-fill-prefix))))))
1484
1485 (defadvice do-auto-fill (around mdw-dynamic-fill-prefix () activate compile)
1486 "Handle auto-filling, working out a dynamic fill prefix in the
1487 case where there isn't a sensible static one."
1488 (let ((fill-prefix (mdw-choose-dynamic-fill-prefix)))
1489 ad-do-it))
1490
1491 (defun mdw-fill-paragraph ()
1492 "Fill paragraph, getting a dynamic fill prefix."
1493 (interactive)
1494 (let ((fill-prefix (mdw-choose-dynamic-fill-prefix)))
1495 (fill-paragraph nil)))
1496
1497 (defun mdw-point-within-string-p ()
1498 "Return non-nil if point is within a string."
1499 (let ((state (syntax-ppss)))
1500 (elt state 3)))
1501
1502 (defun mdw-standard-fill-prefix (rx &optional mat)
1503 "Set the dynamic fill prefix, handling standard hanging indents and stuff.
1504 This is just a short-cut for setting the thing by hand, and by
1505 design it doesn't cope with anything approximating a complicated
1506 case."
1507 (setq mdw-fill-prefix
1508 `(((if (mdw-point-within-string-p)
1509 ,(concat "\\(\\s-*\\)" mdw-hanging-indents)
1510 ,(concat rx mdw-hanging-indents))
1511 (match . 1)
1512 (pad . ,(or mat 2))))))
1513
1514 ;;;--------------------------------------------------------------------------
1515 ;;; Printing.
1516
1517 ;; Teach PostScript about a condensed variant of Courier. I'm using 85% of
1518 ;; the usual width, which happens to match `mdwfonts', and David Carlisle's
1519 ;; `pslatex'. (Once upon a time, I used 80%, but decided consistency with
1520 ;; `pslatex' was useful.)
1521 (setq ps-user-defined-prologue "
1522 /CourierCondensed /Courier
1523 /CourierCondensed-Bold /Courier-Bold
1524 /CourierCondensed-Oblique /Courier-Oblique
1525 /CourierCondensed-BoldOblique /Courier-BoldOblique
1526 4 { findfont [0.85 0 0 1 0 0] makefont definefont pop } repeat
1527 ")
1528
1529 ;; Hack `ps-print''s settings.
1530 (eval-after-load 'ps-print
1531 '(progn
1532
1533 ;; Notice that the comment-delimiters should be in italics too.
1534 (pushnew 'font-lock-comment-delimiter-face ps-italic-faces)
1535
1536 ;; Select more suitable colours for the main kinds of tokens. The
1537 ;; colours set on the Emacs faces are chosen for use against a dark
1538 ;; background, and work very badly on white paper.
1539 (ps-extend-face '(font-lock-comment-face "darkgreen" nil italic))
1540 (ps-extend-face '(font-lock-comment-delimiter-face "darkgreen" nil italic))
1541 (ps-extend-face '(font-lock-string-face "RoyalBlue4" nil))
1542 (ps-extend-face '(mdw-punct-face "sienna" nil))
1543 (ps-extend-face '(mdw-number-face "OrangeRed3" nil))
1544
1545 ;; Teach `ps-print' about my condensed varsions of Courier.
1546 (setq ps-font-info-database
1547 (append '((CourierCondensed
1548 (fonts (normal . "CourierCondensed")
1549 (bold . "CourierCondensed-Bold")
1550 (italic . "CourierCondensed-Oblique")
1551 (bold-italic . "CourierCondensed-BoldOblique"))
1552 (size . 10.0)
1553 (line-height . 10.55)
1554 (space-width . 5.1)
1555 (avg-char-width . 5.1)))
1556 (remove* 'CourierCondensed ps-font-info-database
1557 :key #'car)))))
1558
1559 ;; Arrange to strip overlays from the buffer before we print . This will
1560 ;; prevent `flyspell' from interfering with the printout. (It would be less
1561 ;; bad if `ps-print' could merge the `flyspell' overlay face with the
1562 ;; underlying `font-lock' face, but it can't (and that seems hard). So
1563 ;; instead we have this hack.
1564 ;;
1565 ;; The basic trick is to copy the relevant text from the buffer being printed
1566 ;; into a temporary buffer and... just print that. The text properties come
1567 ;; with the text and end up in the new buffer, and the overlays get lost
1568 ;; along the way. Only problem is that the headers identifying the file
1569 ;; being printed get confused, so remember the original buffer and reinstate
1570 ;; it when constructing the headers.
1571 (defvar mdw-printing-buffer)
1572
1573 (defadvice ps-generate-header
1574 (around mdw-use-correct-buffer () activate compile)
1575 "Print the correct name of the buffer being printed."
1576 (with-current-buffer mdw-printing-buffer
1577 ad-do-it))
1578
1579 (defadvice ps-generate
1580 (around mdw-strip-overlays (buffer from to genfunc) activate compile)
1581 "Strip overlays -- in particular, from `flyspell' -- before printout."
1582 (with-temp-buffer
1583 (let ((mdw-printing-buffer buffer))
1584 (insert-buffer-substring buffer from to)
1585 (ad-set-arg 0 (current-buffer))
1586 (ad-set-arg 1 (point-min))
1587 (ad-set-arg 2 (point-max))
1588 ad-do-it)))
1589
1590 ;;;--------------------------------------------------------------------------
1591 ;;; Other common declarations.
1592
1593 ;; Common mode settings.
1594
1595 (defcustom mdw-auto-indent t
1596 "Whether to indent automatically after a newline."
1597 :type 'boolean
1598 :safe 'booleanp)
1599
1600 (defun mdw-whitespace-mode (&optional arg)
1601 "Turn on/off whitespace mode, but don't highlight trailing space."
1602 (interactive "P")
1603 (when (and (boundp 'whitespace-style)
1604 (fboundp 'whitespace-mode))
1605 (let ((whitespace-style (remove 'trailing whitespace-style)))
1606 (whitespace-mode arg))
1607 (setq show-trailing-whitespace whitespace-mode)))
1608
1609 (defvar mdw-do-misc-mode-hacking nil)
1610
1611 (defun mdw-misc-mode-config ()
1612 (and mdw-auto-indent
1613 (cond ((eq major-mode 'lisp-mode)
1614 (local-set-key "\C-m" 'mdw-indent-newline-and-indent))
1615 ((derived-mode-p 'slime-repl-mode 'asm-mode 'comint-mode)
1616 nil)
1617 (t
1618 (local-set-key "\C-m" 'newline-and-indent))))
1619 (set (make-local-variable 'mdw-do-misc-mode-hacking) t)
1620 (local-set-key [C-return] 'newline)
1621 (make-local-variable 'page-delimiter)
1622 (setq page-delimiter (concat "^" "\f"
1623 "\\|" "^"
1624 ".\\{0,4\\}"
1625 "-\\{5\\}"
1626 "\\(" " " ".*" " " "\\)?"
1627 "-+"
1628 ".\\{0,2\\}"
1629 "$"))
1630 (setq comment-column 40)
1631 (auto-fill-mode 1)
1632 (setq fill-column mdw-text-width)
1633 (flyspell-prog-mode)
1634 (and (fboundp 'gtags-mode)
1635 (gtags-mode))
1636 (if (fboundp 'hs-minor-mode)
1637 (trap (hs-minor-mode t))
1638 (outline-minor-mode t))
1639 (reveal-mode t)
1640 (trap (turn-on-font-lock)))
1641
1642 (defun mdw-post-local-vars-misc-mode-config ()
1643 (setq whitespace-line-column mdw-text-width)
1644 (when (and mdw-do-misc-mode-hacking
1645 (not buffer-read-only))
1646 (setq show-trailing-whitespace t)
1647 (mdw-whitespace-mode 1)))
1648 (add-hook 'hack-local-variables-hook 'mdw-post-local-vars-misc-mode-config)
1649
1650 (defmacro mdw-advise-update-angry-fruit-salad (&rest funcs)
1651 `(progn ,@(mapcar (lambda (func)
1652 `(defadvice ,func
1653 (after mdw-angry-fruit-salad activate)
1654 (when mdw-do-misc-mode-hacking
1655 (setq show-trailing-whitespace
1656 (not buffer-read-only))
1657 (mdw-whitespace-mode (if buffer-read-only 0 1)))))
1658 funcs)))
1659 (mdw-advise-update-angry-fruit-salad toggle-read-only
1660 read-only-mode
1661 view-mode
1662 view-mode-enable
1663 view-mode-disable)
1664
1665 (eval-after-load 'gtags
1666 '(progn
1667 (dolist (key '([mouse-2] [mouse-3]))
1668 (define-key gtags-mode-map key nil))
1669 (define-key gtags-mode-map [C-S-mouse-2] 'gtags-find-tag-by-event)
1670 (define-key gtags-select-mode-map [C-S-mouse-2]
1671 'gtags-select-tag-by-event)
1672 (dolist (map (list gtags-mode-map gtags-select-mode-map))
1673 (define-key map [C-S-mouse-3] 'gtags-pop-stack))))
1674
1675 ;; Backup file handling.
1676
1677 (defcustom mdw-backup-disable-regexps nil
1678 "List of regular expressions: if a file name matches any of
1679 these then the file is not backed up."
1680 :type '(repeat regexp))
1681
1682 (defun mdw-backup-enable-predicate (name)
1683 "[mdw]'s default backup predicate.
1684 Allows a backup if the standard predicate would allow it, and it
1685 doesn't match any of the regular expressions in
1686 `mdw-backup-disable-regexps'."
1687 (and (normal-backup-enable-predicate name)
1688 (let ((answer t) (list mdw-backup-disable-regexps))
1689 (save-match-data
1690 (while list
1691 (if (string-match (car list) name)
1692 (setq answer nil))
1693 (setq list (cdr list)))
1694 answer))))
1695 (setq backup-enable-predicate 'mdw-backup-enable-predicate)
1696
1697 ;; Frame cleanup.
1698
1699 (defun mdw-last-one-out-turn-off-the-lights (frame)
1700 "Disconnect from an X display if this was the last frame on that display."
1701 (let ((frame-display (frame-parameter frame 'display)))
1702 (when (and frame-display
1703 (eq window-system 'x)
1704 (not (some (lambda (fr)
1705 (and (not (eq fr frame))
1706 (string= (frame-parameter fr 'display)
1707 frame-display)))
1708 (frame-list))))
1709 (run-with-idle-timer 0 nil #'x-close-connection frame-display))))
1710 (add-hook 'delete-frame-functions 'mdw-last-one-out-turn-off-the-lights)
1711
1712 ;;;--------------------------------------------------------------------------
1713 ;;; Fullscreen-ness.
1714
1715 (defcustom mdw-full-screen-parameters
1716 '((menu-bar-lines . 0)
1717 ;;(vertical-scroll-bars . nil)
1718 )
1719 "Frame parameters to set when making a frame fullscreen."
1720 :type '(alist :key-type symbol))
1721
1722 (defcustom mdw-full-screen-save
1723 '(width height)
1724 "Extra frame parameters to save when setting fullscreen."
1725 :type '(repeat symbol))
1726
1727 (defun mdw-toggle-full-screen (&optional frame)
1728 "Show the FRAME fullscreen."
1729 (interactive)
1730 (when window-system
1731 (cond ((frame-parameter frame 'fullscreen)
1732 (set-frame-parameter frame 'fullscreen nil)
1733 (modify-frame-parameters
1734 nil
1735 (or (frame-parameter frame 'mdw-full-screen-saved)
1736 (mapcar (lambda (assoc)
1737 (assq (car assoc) default-frame-alist))
1738 mdw-full-screen-parameters))))
1739 (t
1740 (let ((saved (mapcar (lambda (param)
1741 (cons param (frame-parameter frame param)))
1742 (append (mapcar #'car
1743 mdw-full-screen-parameters)
1744 mdw-full-screen-save))))
1745 (set-frame-parameter frame 'mdw-full-screen-saved saved))
1746 (modify-frame-parameters frame mdw-full-screen-parameters)
1747 (set-frame-parameter frame 'fullscreen 'fullboth)))))
1748
1749 ;;;--------------------------------------------------------------------------
1750 ;;; General fontification.
1751
1752 (make-face 'mdw-virgin-face)
1753
1754 (defmacro mdw-define-face (name &rest body)
1755 "Define a face, and make sure it's actually set as the definition."
1756 (declare (indent 1)
1757 (debug 0))
1758 `(progn
1759 (copy-face 'mdw-virgin-face ',name)
1760 (defvar ,name ',name)
1761 (put ',name 'face-defface-spec ',body)
1762 (face-spec-set ',name ',body nil)))
1763
1764 (mdw-define-face default
1765 (((type w32)) :family "courier new" :height 85)
1766 (((type x)) :family "6x13" :foundry "trad" :height 130)
1767 (((type color)) :foreground "white" :background "black")
1768 (t nil))
1769 (mdw-define-face fixed-pitch
1770 (((type w32)) :family "courier new" :height 85)
1771 (((type x)) :family "6x13" :foundry "trad" :height 130)
1772 (t :foreground "white" :background "black"))
1773 (mdw-define-face fixed-pitch-serif
1774 (((type w32)) :family "courier new" :height 85 :weight bold)
1775 (((type x)) :family "6x13" :foundry "trad" :height 130 :weight bold)
1776 (t :foreground "white" :background "black" :weight bold))
1777 (mdw-define-face variable-pitch
1778 (((type x)) :family "helvetica" :height 120))
1779 (mdw-define-face region
1780 (((min-colors 64)) :background "grey30")
1781 (((class color)) :background "blue")
1782 (t :inverse-video t))
1783 (mdw-define-face match
1784 (((class color)) :background "blue")
1785 (t :inverse-video t))
1786 (mdw-define-face mc/cursor-face
1787 (((class color)) :background "red")
1788 (t :inverse-video t))
1789 (mdw-define-face minibuffer-prompt
1790 (t :weight bold))
1791 (mdw-define-face mode-line
1792 (((class color)) :foreground "blue" :background "yellow"
1793 :box (:line-width 1 :style released-button))
1794 (t :inverse-video t))
1795 (mdw-define-face mode-line-inactive
1796 (((class color)) :foreground "yellow" :background "blue"
1797 :box (:line-width 1 :style released-button))
1798 (t :inverse-video t))
1799 (mdw-define-face nobreak-space
1800 (((type tty)))
1801 (t :inherit escape-glyph :underline t))
1802 (mdw-define-face scroll-bar
1803 (t :foreground "black" :background "lightgrey"))
1804 (mdw-define-face fringe
1805 (t :foreground "yellow"))
1806 (mdw-define-face show-paren-match
1807 (((min-colors 64)) :background "darkgreen")
1808 (((class color)) :background "green")
1809 (t :underline t))
1810 (mdw-define-face show-paren-mismatch
1811 (((class color)) :background "red")
1812 (t :inverse-video t))
1813 (mdw-define-face highlight
1814 (((min-colors 64)) :background "DarkSeaGreen4")
1815 (((class color)) :background "cyan")
1816 (t :inverse-video t))
1817
1818 (mdw-define-face viper-minibuffer-emacs (t nil))
1819 (mdw-define-face viper-minibuffer-insert (t nil))
1820 (mdw-define-face viper-minibuffer-vi (t nil))
1821 (mdw-define-face viper-replace-overlay
1822 (((min-colors 64)) :background "darkred")
1823 (((class color)) :background "red")
1824 (t :inverse-video t))
1825 (mdw-define-face viper-search (t :inherit isearch))
1826
1827 (mdw-define-face compilation-error
1828 (((class color)) :foreground "red" :weight bold)
1829 (t :weight bold))
1830 (mdw-define-face compilation-warning
1831 (((class color)) :foreground "orange" :weight bold)
1832 (t :weight bold))
1833 (mdw-define-face compilation-info
1834 (((class color)) :foreground "green" :weight bold)
1835 (t :weight bold))
1836 (mdw-define-face compilation-line-number
1837 (t :weight bold))
1838 (mdw-define-face compilation-column-number
1839 (((min-colors 64)) :foreground "lightgrey"))
1840 (setq compilation-message-face 'mdw-virgin-face)
1841 (setq compilation-enter-directory-face 'font-lock-comment-face)
1842 (setq compilation-leave-directory-face 'font-lock-comment-face)
1843
1844 (mdw-define-face holiday-face
1845 (t :background "red"))
1846 (mdw-define-face calendar-today-face
1847 (t :foreground "yellow" :weight bold))
1848
1849 (mdw-define-face comint-highlight-prompt
1850 (t :weight bold))
1851 (mdw-define-face comint-highlight-input
1852 (t nil))
1853
1854 (mdw-define-face Man-underline
1855 (((type tty)) :underline t)
1856 (t :slant italic))
1857
1858 (mdw-define-face ido-subdir
1859 (t :foreground "cyan" :weight bold))
1860
1861 (mdw-define-face dired-directory
1862 (t :foreground "cyan" :weight bold))
1863 (mdw-define-face dired-symlink
1864 (t :foreground "cyan"))
1865 (mdw-define-face dired-perm-write
1866 (t nil))
1867
1868 (mdw-define-face trailing-whitespace
1869 (((class color)) :background "red")
1870 (t :inverse-video t))
1871 (mdw-define-face whitespace-line
1872 (((class color)) :background "darkred")
1873 (t :inverse-video t))
1874 (mdw-define-face mdw-punct-face
1875 (((min-colors 64)) :foreground "burlywood2")
1876 (((class color)) :foreground "yellow"))
1877 (mdw-define-face mdw-number-face
1878 (t :foreground "yellow"))
1879 (mdw-define-face mdw-trivial-face)
1880 (mdw-define-face font-lock-function-name-face
1881 (t :slant italic))
1882 (mdw-define-face font-lock-keyword-face
1883 (t :weight bold))
1884 (mdw-define-face font-lock-constant-face
1885 (t :slant italic))
1886 (mdw-define-face font-lock-builtin-face
1887 (t :weight bold))
1888 (mdw-define-face font-lock-type-face
1889 (t :weight bold :slant italic))
1890 (mdw-define-face font-lock-reference-face
1891 (t :weight bold))
1892 (mdw-define-face font-lock-variable-name-face
1893 (t :slant italic))
1894 (mdw-define-face font-lock-comment-face
1895 (((min-colors 64)) :slant italic :foreground "SeaGreen1")
1896 (((class color)) :foreground "green")
1897 (t :weight bold))
1898 (mdw-define-face font-lock-comment-delimiter-face
1899 (t :inherit font-lock-comment-face))
1900 (mdw-define-face font-lock-string-face
1901 (((min-colors 64)) :foreground "SkyBlue1")
1902 (((class color)) :foreground "cyan")
1903 (t :weight bold))
1904 (mdw-define-face font-lock-doc-face
1905 (t :inherit font-lock-string-face))
1906
1907 (mdw-define-face message-separator
1908 (t :background "red" :foreground "white" :weight bold))
1909 (mdw-define-face message-cited-text
1910 (default :slant italic)
1911 (((min-colors 64)) :foreground "SkyBlue1")
1912 (((class color)) :foreground "cyan"))
1913 (mdw-define-face message-header-cc
1914 (default :slant italic)
1915 (((min-colors 64)) :foreground "SeaGreen1")
1916 (((class color)) :foreground "green"))
1917 (mdw-define-face message-header-newsgroups
1918 (default :slant italic)
1919 (((min-colors 64)) :foreground "SeaGreen1")
1920 (((class color)) :foreground "green"))
1921 (mdw-define-face message-header-subject
1922 (((min-colors 64)) :foreground "SeaGreen1")
1923 (((class color)) :foreground "green"))
1924 (mdw-define-face message-header-to
1925 (((min-colors 64)) :foreground "SeaGreen1")
1926 (((class color)) :foreground "green"))
1927 (mdw-define-face message-header-xheader
1928 (default :slant italic)
1929 (((min-colors 64)) :foreground "SeaGreen1")
1930 (((class color)) :foreground "green"))
1931 (mdw-define-face message-header-other
1932 (default :slant italic)
1933 (((min-colors 64)) :foreground "SeaGreen1")
1934 (((class color)) :foreground "green"))
1935 (mdw-define-face message-header-name
1936 (default :weight bold)
1937 (((min-colors 64)) :foreground "SeaGreen1")
1938 (((class color)) :foreground "green"))
1939
1940 (mdw-define-face which-func
1941 (t nil))
1942
1943 (mdw-define-face gnus-header-name
1944 (default :weight bold)
1945 (((min-colors 64)) :foreground "SeaGreen1")
1946 (((class color)) :foreground "green"))
1947 (mdw-define-face gnus-header-subject
1948 (((min-colors 64)) :foreground "SeaGreen1")
1949 (((class color)) :foreground "green"))
1950 (mdw-define-face gnus-header-from
1951 (((min-colors 64)) :foreground "SeaGreen1")
1952 (((class color)) :foreground "green"))
1953 (mdw-define-face gnus-header-to
1954 (((min-colors 64)) :foreground "SeaGreen1")
1955 (((class color)) :foreground "green"))
1956 (mdw-define-face gnus-header-content
1957 (default :slant italic)
1958 (((min-colors 64)) :foreground "SeaGreen1")
1959 (((class color)) :foreground "green"))
1960
1961 (mdw-define-face gnus-cite-1
1962 (((min-colors 64)) :foreground "SkyBlue1")
1963 (((class color)) :foreground "cyan"))
1964 (mdw-define-face gnus-cite-2
1965 (((min-colors 64)) :foreground "RoyalBlue2")
1966 (((class color)) :foreground "blue"))
1967 (mdw-define-face gnus-cite-3
1968 (((min-colors 64)) :foreground "MediumOrchid")
1969 (((class color)) :foreground "magenta"))
1970 (mdw-define-face gnus-cite-4
1971 (((min-colors 64)) :foreground "firebrick2")
1972 (((class color)) :foreground "red"))
1973 (mdw-define-face gnus-cite-5
1974 (((min-colors 64)) :foreground "burlywood2")
1975 (((class color)) :foreground "yellow"))
1976 (mdw-define-face gnus-cite-6
1977 (((min-colors 64)) :foreground "SeaGreen1")
1978 (((class color)) :foreground "green"))
1979 (mdw-define-face gnus-cite-7
1980 (((min-colors 64)) :foreground "SlateBlue1")
1981 (((class color)) :foreground "cyan"))
1982 (mdw-define-face gnus-cite-8
1983 (((min-colors 64)) :foreground "RoyalBlue2")
1984 (((class color)) :foreground "blue"))
1985 (mdw-define-face gnus-cite-9
1986 (((min-colors 64)) :foreground "purple2")
1987 (((class color)) :foreground "magenta"))
1988 (mdw-define-face gnus-cite-10
1989 (((min-colors 64)) :foreground "DarkOrange2")
1990 (((class color)) :foreground "red"))
1991 (mdw-define-face gnus-cite-11
1992 (t :foreground "grey"))
1993
1994 (mdw-define-face gnus-emphasis-underline
1995 (((type tty)) :underline t)
1996 (t :slant italic))
1997
1998 (mdw-define-face diff-header
1999 (t nil))
2000 (mdw-define-face diff-index
2001 (t :weight bold))
2002 (mdw-define-face diff-file-header
2003 (t :weight bold))
2004 (mdw-define-face diff-hunk-header
2005 (((min-colors 64)) :foreground "SkyBlue1")
2006 (((class color)) :foreground "cyan"))
2007 (mdw-define-face diff-function
2008 (default :weight bold)
2009 (((min-colors 64)) :foreground "SkyBlue1")
2010 (((class color)) :foreground "cyan"))
2011 (mdw-define-face diff-header
2012 (((min-colors 64)) :background "grey10"))
2013 (mdw-define-face diff-added
2014 (((class color)) :foreground "green"))
2015 (mdw-define-face diff-removed
2016 (((class color)) :foreground "red"))
2017 (mdw-define-face diff-context
2018 (t nil))
2019 (mdw-define-face diff-refine-change
2020 (((min-colors 64)) :background "RoyalBlue4")
2021 (t :underline t))
2022 (mdw-define-face diff-refine-removed
2023 (((min-colors 64)) :background "#500")
2024 (t :underline t))
2025 (mdw-define-face diff-refine-added
2026 (((min-colors 64)) :background "#050")
2027 (t :underline t))
2028
2029 (setq ediff-force-faces t)
2030 (mdw-define-face ediff-current-diff-A
2031 (((min-colors 64)) :background "darkred")
2032 (((class color)) :background "red")
2033 (t :inverse-video t))
2034 (mdw-define-face ediff-fine-diff-A
2035 (((min-colors 64)) :background "red3")
2036 (((class color)) :inverse-video t)
2037 (t :inverse-video nil))
2038 (mdw-define-face ediff-even-diff-A
2039 (((min-colors 64)) :background "#300"))
2040 (mdw-define-face ediff-odd-diff-A
2041 (((min-colors 64)) :background "#300"))
2042 (mdw-define-face ediff-current-diff-B
2043 (((min-colors 64)) :background "darkgreen")
2044 (((class color)) :background "magenta")
2045 (t :inverse-video t))
2046 (mdw-define-face ediff-fine-diff-B
2047 (((min-colors 64)) :background "green4")
2048 (((class color)) :inverse-video t)
2049 (t :inverse-video nil))
2050 (mdw-define-face ediff-even-diff-B
2051 (((min-colors 64)) :background "#020"))
2052 (mdw-define-face ediff-odd-diff-B
2053 (((min-colors 64)) :background "#020"))
2054 (mdw-define-face ediff-current-diff-C
2055 (((min-colors 64)) :background "darkblue")
2056 (((class color)) :background "blue")
2057 (t :inverse-video t))
2058 (mdw-define-face ediff-fine-diff-C
2059 (((min-colors 64)) :background "blue1")
2060 (((class color)) :inverse-video t)
2061 (t :inverse-video nil))
2062 (mdw-define-face ediff-even-diff-C
2063 (((min-colors 64)) :background "#004"))
2064 (mdw-define-face ediff-odd-diff-C
2065 (((min-colors 64)) :background "#004"))
2066 (mdw-define-face ediff-current-diff-Ancestor
2067 (((min-colors 64)) :background "#630")
2068 (((class color)) :background "blue")
2069 (t :inverse-video t))
2070 (mdw-define-face ediff-even-diff-Ancestor
2071 (((min-colors 64)) :background "#320"))
2072 (mdw-define-face ediff-odd-diff-Ancestor
2073 (((min-colors 64)) :background "#320"))
2074
2075 (mdw-define-face magit-hash
2076 (((min-colors 64)) :foreground "grey40")
2077 (((class color)) :foreground "blue"))
2078 (mdw-define-face magit-diff-hunk-heading
2079 (((min-colors 64)) :foreground "grey70" :background "grey25")
2080 (((class color)) :foreground "yellow"))
2081 (mdw-define-face magit-diff-hunk-heading-highlight
2082 (((min-colors 64)) :foreground "grey70" :background "grey35")
2083 (((class color)) :foreground "yellow" :background "blue"))
2084 (mdw-define-face magit-diff-added
2085 (((min-colors 64)) :foreground "#ddffdd" :background "#335533")
2086 (((class color)) :foreground "green"))
2087 (mdw-define-face magit-diff-added-highlight
2088 (((min-colors 64)) :foreground "#cceecc" :background "#336633")
2089 (((class color)) :foreground "green" :background "blue"))
2090 (mdw-define-face magit-diff-removed
2091 (((min-colors 64)) :foreground "#ffdddd" :background "#553333")
2092 (((class color)) :foreground "red"))
2093 (mdw-define-face magit-diff-removed-highlight
2094 (((min-colors 64)) :foreground "#eecccc" :background "#663333")
2095 (((class color)) :foreground "red" :background "blue"))
2096 (mdw-define-face magit-blame-heading
2097 (((min-colors 64)) :foreground "white" :background "grey25"
2098 :weight normal :slant normal)
2099 (((class color)) :foreground "white" :background "blue"
2100 :weight normal :slant normal))
2101 (mdw-define-face magit-blame-name
2102 (t :inherit magit-blame-heading :slant italic))
2103 (mdw-define-face magit-blame-date
2104 (((min-colors 64)) :inherit magit-blame-heading :foreground "grey60")
2105 (((class color)) :inherit magit-blame-heading :foreground "cyan"))
2106 (mdw-define-face magit-blame-summary
2107 (t :inherit magit-blame-heading :weight bold))
2108
2109 (mdw-define-face dylan-header-background
2110 (((min-colors 64)) :background "NavyBlue")
2111 (((class color)) :background "blue"))
2112
2113 (mdw-define-face erc-input-face
2114 (t :foreground "red"))
2115
2116 (mdw-define-face woman-bold
2117 (t :weight bold))
2118 (mdw-define-face woman-italic
2119 (t :slant italic))
2120
2121 (eval-after-load "rst"
2122 '(progn
2123 (mdw-define-face rst-level-1-face
2124 (t :foreground "SkyBlue1" :weight bold))
2125 (mdw-define-face rst-level-2-face
2126 (t :foreground "SeaGreen1" :weight bold))
2127 (mdw-define-face rst-level-3-face
2128 (t :weight bold))
2129 (mdw-define-face rst-level-4-face
2130 (t :slant italic))
2131 (mdw-define-face rst-level-5-face
2132 (t :underline t))
2133 (mdw-define-face rst-level-6-face
2134 ())))
2135
2136 (mdw-define-face p4-depot-added-face
2137 (t :foreground "green"))
2138 (mdw-define-face p4-depot-branch-op-face
2139 (t :foreground "yellow"))
2140 (mdw-define-face p4-depot-deleted-face
2141 (t :foreground "red"))
2142 (mdw-define-face p4-depot-unmapped-face
2143 (t :foreground "SkyBlue1"))
2144 (mdw-define-face p4-diff-change-face
2145 (t :foreground "yellow"))
2146 (mdw-define-face p4-diff-del-face
2147 (t :foreground "red"))
2148 (mdw-define-face p4-diff-file-face
2149 (t :foreground "SkyBlue1"))
2150 (mdw-define-face p4-diff-head-face
2151 (t :background "grey10"))
2152 (mdw-define-face p4-diff-ins-face
2153 (t :foreground "green"))
2154
2155 (mdw-define-face w3m-anchor-face
2156 (t :foreground "SkyBlue1" :underline t))
2157 (mdw-define-face w3m-arrived-anchor-face
2158 (t :foreground "SkyBlue1" :underline t))
2159
2160 (mdw-define-face whizzy-slice-face
2161 (t :background "grey10"))
2162 (mdw-define-face whizzy-error-face
2163 (t :background "darkred"))
2164
2165 ;; Ellipses used to indicate hidden text (and similar).
2166 (mdw-define-face mdw-ellipsis-face
2167 (((type tty)) :foreground "blue") (t :foreground "grey60"))
2168 (let ((dollar (make-glyph-code ?$ 'mdw-ellipsis-face))
2169 (backslash (make-glyph-code ?\\ 'mdw-ellipsis-face))
2170 (dot (make-glyph-code ?. 'mdw-ellipsis-face))
2171 (bar (make-glyph-code ?| mdw-ellipsis-face)))
2172 (set-display-table-slot standard-display-table 0 dollar)
2173 (set-display-table-slot standard-display-table 1 backslash)
2174 (set-display-table-slot standard-display-table 4
2175 (vector dot dot dot))
2176 (set-display-table-slot standard-display-table 5 bar))
2177
2178 ;;;--------------------------------------------------------------------------
2179 ;;; Where is point?
2180
2181 (mdw-define-face mdw-point-overlay-face
2182 (((type graphic)))
2183 (((min-colors 64)) :background "darkblue")
2184 (((class color)) :background "blue")
2185 (((type tty) (class mono)) :inverse-video t))
2186
2187 (defcustom mdw-point-overlay-fringe-display '(vertical-bar . vertical-bar)
2188 "Bitmaps to display in the left and right fringes in the current line."
2189 :type '(cons symbol symbol))
2190
2191 (defun mdw-configure-point-overlay ()
2192 (let ((ov (make-overlay 0 0)))
2193 (overlay-put ov 'priority 0)
2194 (let* ((fringe (or mdw-point-overlay-fringe-display (cons nil nil)))
2195 (left (car fringe)) (right (cdr fringe))
2196 (s ""))
2197 (when left
2198 (let ((ss "."))
2199 (put-text-property 0 1 'display `(left-fringe ,left) ss)
2200 (setq s (concat s ss))))
2201 (when right
2202 (let ((ss "."))
2203 (put-text-property 0 1 'display `(right-fringe ,right) ss)
2204 (setq s (concat s ss))))
2205 (when (or left right)
2206 (overlay-put ov 'before-string s)))
2207 (overlay-put ov 'face 'mdw-point-overlay-face)
2208 (delete-overlay ov)
2209 ov))
2210
2211 (defvar mdw-point-overlay (mdw-configure-point-overlay)
2212 "An overlay used for showing where point is in the selected window.")
2213 (defun mdw-reconfigure-point-overlay ()
2214 (interactive)
2215 (setq mdw-point-overlay (mdw-configure-point-overlay)))
2216
2217 (defun mdw-remove-point-overlay ()
2218 "Remove the current-point overlay."
2219 (delete-overlay mdw-point-overlay))
2220
2221 (defun mdw-update-point-overlay ()
2222 "Mark the current point position with an overlay."
2223 (if (not mdw-point-overlay-mode)
2224 (mdw-remove-point-overlay)
2225 (overlay-put mdw-point-overlay 'window (selected-window))
2226 (move-overlay mdw-point-overlay
2227 (line-beginning-position)
2228 (+ (line-end-position) 1))))
2229
2230 (defvar mdw-point-overlay-buffers nil
2231 "List of buffers using `mdw-point-overlay-mode'.")
2232
2233 (define-minor-mode mdw-point-overlay-mode
2234 "Indicate current line with an overlay."
2235 :global nil
2236 (let ((buffer (current-buffer)))
2237 (setq mdw-point-overlay-buffers
2238 (mapcan (lambda (buf)
2239 (if (and (buffer-live-p buf)
2240 (not (eq buf buffer)))
2241 (list buf)))
2242 mdw-point-overlay-buffers))
2243 (if mdw-point-overlay-mode
2244 (setq mdw-point-overlay-buffers
2245 (cons buffer mdw-point-overlay-buffers))))
2246 (cond (mdw-point-overlay-buffers
2247 (add-hook 'pre-command-hook 'mdw-remove-point-overlay)
2248 (add-hook 'post-command-hook 'mdw-update-point-overlay))
2249 (t
2250 (mdw-remove-point-overlay)
2251 (remove-hook 'pre-command-hook 'mdw-remove-point-overlay)
2252 (remove-hook 'post-command-hook 'mdw-update-point-overlay))))
2253
2254 (define-globalized-minor-mode mdw-global-point-overlay-mode
2255 mdw-point-overlay-mode
2256 (lambda () (if (not (minibufferp)) (mdw-point-overlay-mode t))))
2257
2258 (defvar mdw-terminal-title-alist nil)
2259 (defun mdw-update-terminal-title ()
2260 (when (let ((term (frame-parameter nil 'tty-type)))
2261 (and term (string-match "^xterm" term)))
2262 (let* ((tty (frame-parameter nil 'tty))
2263 (old (assoc tty mdw-terminal-title-alist))
2264 (new (format-mode-line frame-title-format)))
2265 (unless (and old (equal (cdr old) new))
2266 (if old (rplacd old new)
2267 (setq mdw-terminal-title-alist
2268 (cons (cons tty new) mdw-terminal-title-alist)))
2269 (send-string-to-terminal (concat "\e]2;" new "\e\\"))))))
2270
2271 (add-hook 'post-command-hook 'mdw-update-terminal-title)
2272
2273 ;;;--------------------------------------------------------------------------
2274 ;;; Ediff hacking.
2275
2276 (defvar mdw-ediff-previous-windows)
2277 (defun mdw-ediff-setup ()
2278 (setq mdw-ediff-previous-windows (current-window-configuration)))
2279 (defun mdw-ediff-suspend-or-quit ()
2280 (set-window-configuration mdw-ediff-previous-windows))
2281 (add-hook 'ediff-before-setup-hook 'mdw-ediff-setup)
2282 (add-hook 'ediff-quit-hook 'mdw-ediff-suspend-or-quit t)
2283 (add-hook 'ediff-suspend-hook 'mdw-ediff-suspend-or-quit t)
2284
2285 ;;;--------------------------------------------------------------------------
2286 ;;; C programming configuration.
2287
2288 ;; Make C indentation nice.
2289
2290 (defun mdw-c-lineup-arglist (langelem)
2291 "Hack for DWIMmery in c-lineup-arglist."
2292 (if (save-excursion
2293 (c-block-in-arglist-dwim (c-langelem-2nd-pos c-syntactic-element)))
2294 0
2295 (c-lineup-arglist langelem)))
2296
2297 (defun mdw-c-indent-extern-mumble (langelem)
2298 "Indent `extern \"...\" {' lines."
2299 (save-excursion
2300 (back-to-indentation)
2301 (if (looking-at
2302 "\\s-*\\<extern\\>\\s-*\"\\([^\\\\\"]+\\|\\.\\)*\"\\s-*{")
2303 c-basic-offset
2304 nil)))
2305
2306 (defun mdw-c-indent-arglist-nested (langelem)
2307 "Indent continued argument lists.
2308 If we've nested more than one argument list, then only introduce a single
2309 indentation anyway."
2310 (let ((context c-syntactic-context)
2311 (pos (c-langelem-2nd-pos c-syntactic-element))
2312 (should-indent-p t))
2313 (while (and context
2314 (eq (caar context) 'arglist-cont-nonempty))
2315 (when (and (= (caddr (pop context)) pos)
2316 context
2317 (memq (caar context) '(arglist-intro
2318 arglist-cont-nonempty)))
2319 (setq should-indent-p nil)))
2320 (if should-indent-p '+ 0)))
2321
2322 (defvar mdw-define-c-styles-hook nil
2323 "Hook run when `cc-mode' starts up to define styles.")
2324
2325 (defun mdw-merge-style-alists (first second)
2326 (let ((output nil))
2327 (dolist (item first)
2328 (let ((key (car item)) (value (cdr item)))
2329 (if (let* ((key-name (symbol-name key))
2330 (key-len (length key-name)))
2331 (and (>= key-len 5)
2332 (string= (subseq key-name (- key-len 5)) "-alist")))
2333 (push (cons key
2334 (mdw-merge-style-alists value
2335 (cdr (assoc key second))))
2336 output)
2337 (push item output))))
2338 (dolist (item second)
2339 (unless (assoc (car item) first)
2340 (push item output)))
2341 (nreverse output)))
2342
2343 (defmacro* mdw-define-c-style (name (&optional parent) &rest assocs)
2344 "Define a C style, called NAME (a symbol) based on PARENT, setting ASSOCs.
2345 A function, named `mdw-define-c-style/NAME', is defined to actually install
2346 the style using `c-add-style', and added to the hook
2347 `mdw-define-c-styles-hook'. If CC Mode is already loaded, then the style is
2348 set."
2349 (declare (indent defun))
2350 (let* ((name-string (symbol-name name))
2351 (var (intern (concat "mdw-c-style/" name-string)))
2352 (func (intern (concat "mdw-define-c-style/" name-string))))
2353 `(progn
2354 (setq ,var
2355 ,(if (null parent)
2356 `',assocs
2357 (let ((parent-list (intern (concat "mdw-c-style/"
2358 (symbol-name parent)))))
2359 `(mdw-merge-style-alists ',assocs ,parent-list))))
2360 (defun ,func () (c-add-style ,name-string ,var))
2361 (and (featurep 'cc-mode) (,func))
2362 (add-hook 'mdw-define-c-styles-hook ',func)
2363 ',name)))
2364
2365 (eval-after-load "cc-mode"
2366 '(run-hooks 'mdw-define-c-styles-hook))
2367
2368 (mdw-define-c-style mdw-c ()
2369 (c-basic-offset . 2)
2370 (comment-column . 40)
2371 (c-class-key . "class")
2372 (c-backslash-column . 72)
2373 (c-label-minimum-indentation . 0)
2374 (c-offsets-alist (substatement-open . (add 0 c-indent-one-line-block))
2375 (defun-open . (add 0 c-indent-one-line-block))
2376 (arglist-cont-nonempty . mdw-c-lineup-arglist)
2377 (topmost-intro . mdw-c-indent-extern-mumble)
2378 (cpp-define-intro . 0)
2379 (knr-argdecl . 0)
2380 (inextern-lang . [0])
2381 (label . 0)
2382 (case-label . +)
2383 (access-label . -)
2384 (inclass . +)
2385 (inline-open . ++)
2386 (statement-cont . +)
2387 (statement-case-intro . +)))
2388
2389 (mdw-define-c-style mdw-trustonic-c (mdw-c)
2390 (c-basic-offset . 4)
2391 (c-offsets-alist (access-label . -2)))
2392
2393 (mdw-define-c-style mdw-trustonic-alec-c (mdw-trustonic-c)
2394 (comment-column . 0)
2395 (c-indent-comment-alist (anchored-comment . (column . 0))
2396 (end-block . (space . 1))
2397 (cpp-end-block . (space . 1))
2398 (other . (space . 1)))
2399 (c-offsets-alist (arglist-cont-nonempty . mdw-c-indent-arglist-nested)))
2400
2401 (defun mdw-set-default-c-style (modes style)
2402 "Update the default CC Mode style for MODES to be STYLE.
2403
2404 MODES may be a list of major mode names or a singleton. STYLE is a style
2405 name, as a symbol."
2406 (let ((modes (if (listp modes) modes (list modes)))
2407 (style (symbol-name style)))
2408 (setq c-default-style
2409 (append (mapcar (lambda (mode)
2410 (cons mode style))
2411 modes)
2412 (remove-if (lambda (assoc)
2413 (memq (car assoc) modes))
2414 (if (listp c-default-style)
2415 c-default-style
2416 (list (cons 'other c-default-style))))))))
2417 (setq c-default-style "mdw-c")
2418
2419 (mdw-set-default-c-style '(c-mode c++-mode) 'mdw-c)
2420
2421 (defvar mdw-c-comment-fill-prefix
2422 `((,(concat "\\([ \t]*/?\\)"
2423 "\\(\\*\\|//\\)"
2424 "\\([ \t]*\\)"
2425 "\\([A-Za-z]+:[ \t]*\\)?"
2426 mdw-hanging-indents)
2427 (pad . 1) (match . 2) (pad . 3) (pad . 4) (pad . 5)))
2428 "Fill prefix matching C comments (both kinds).")
2429
2430 (defun mdw-fontify-c-and-c++ ()
2431
2432 ;; Fiddle with some syntax codes.
2433 (modify-syntax-entry ?* ". 23")
2434 (modify-syntax-entry ?/ ". 124b")
2435 (modify-syntax-entry ?\n "> b")
2436
2437 ;; Other stuff.
2438 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2439
2440 ;; Now define things to be fontified.
2441 (make-local-variable 'font-lock-keywords)
2442 (let ((c-keywords
2443 (mdw-regexps "alignas" ;C11 macro, C++11
2444 "alignof" ;C++11
2445 "and" ;C++, C95 macro
2446 "and_eq" ;C++, C95 macro
2447 "asm" ;K&R, C++, GCC
2448 "atomic" ;C11 macro, C++11 template type
2449 "auto" ;K&R, C89
2450 "bitand" ;C++, C95 macro
2451 "bitor" ;C++, C95 macro
2452 "bool" ;C++, C99 macro
2453 "break" ;K&R, C89
2454 "case" ;K&R, C89
2455 "catch" ;C++
2456 "char" ;K&R, C89
2457 "char16_t" ;C++11, C11 library type
2458 "char32_t" ;C++11, C11 library type
2459 "class" ;C++
2460 "complex" ;C99 macro, C++ template type
2461 "compl" ;C++, C95 macro
2462 "const" ;C89
2463 "constexpr" ;C++11
2464 "const_cast" ;C++
2465 "continue" ;K&R, C89
2466 "decltype" ;C++11
2467 "defined" ;C89 preprocessor
2468 "default" ;K&R, C89
2469 "delete" ;C++
2470 "do" ;K&R, C89
2471 "double" ;K&R, C89
2472 "dynamic_cast" ;C++
2473 "else" ;K&R, C89
2474 ;; "entry" ;K&R -- never used
2475 "enum" ;C89
2476 "explicit" ;C++
2477 "export" ;C++
2478 "extern" ;K&R, C89
2479 "float" ;K&R, C89
2480 "for" ;K&R, C89
2481 ;; "fortran" ;K&R
2482 "friend" ;C++
2483 "goto" ;K&R, C89
2484 "if" ;K&R, C89
2485 "imaginary" ;C99 macro
2486 "inline" ;C++, C99, GCC
2487 "int" ;K&R, C89
2488 "long" ;K&R, C89
2489 "mutable" ;C++
2490 "namespace" ;C++
2491 "new" ;C++
2492 "noexcept" ;C++11
2493 "noreturn" ;C11 macro
2494 "not" ;C++, C95 macro
2495 "not_eq" ;C++, C95 macro
2496 "nullptr" ;C++11
2497 "operator" ;C++
2498 "or" ;C++, C95 macro
2499 "or_eq" ;C++, C95 macro
2500 "private" ;C++
2501 "protected" ;C++
2502 "public" ;C++
2503 "register" ;K&R, C89
2504 "reinterpret_cast" ;C++
2505 "restrict" ;C99
2506 "return" ;K&R, C89
2507 "short" ;K&R, C89
2508 "signed" ;C89
2509 "sizeof" ;K&R, C89
2510 "static" ;K&R, C89
2511 "static_assert" ;C11 macro, C++11
2512 "static_cast" ;C++
2513 "struct" ;K&R, C89
2514 "switch" ;K&R, C89
2515 "template" ;C++
2516 "throw" ;C++
2517 "try" ;C++
2518 "thread_local" ;C11 macro, C++11
2519 "typedef" ;C89
2520 "typeid" ;C++
2521 "typeof" ;GCC
2522 "typename" ;C++
2523 "union" ;K&R, C89
2524 "unsigned" ;K&R, C89
2525 "using" ;C++
2526 "virtual" ;C++
2527 "void" ;C89
2528 "volatile" ;C89
2529 "wchar_t" ;C++, C89 library type
2530 "while" ;K&R, C89
2531 "xor" ;C++, C95 macro
2532 "xor_eq" ;C++, C95 macro
2533 "_Alignas" ;C11
2534 "_Alignof" ;C11
2535 "_Atomic" ;C11
2536 "_Bool" ;C99
2537 "_Complex" ;C99
2538 "_Generic" ;C11
2539 "_Imaginary" ;C99
2540 "_Noreturn" ;C11
2541 "_Pragma" ;C99 preprocessor
2542 "_Static_assert" ;C11
2543 "_Thread_local" ;C11
2544 "__alignof__" ;GCC
2545 "__asm__" ;GCC
2546 "__attribute__" ;GCC
2547 "__complex__" ;GCC
2548 "__const__" ;GCC
2549 "__extension__" ;GCC
2550 "__imag__" ;GCC
2551 "__inline__" ;GCC
2552 "__label__" ;GCC
2553 "__real__" ;GCC
2554 "__signed__" ;GCC
2555 "__typeof__" ;GCC
2556 "__volatile__" ;GCC
2557 ))
2558 (c-builtins
2559 (mdw-regexps "false" ;C++, C99 macro
2560 "this" ;C++
2561 "true" ;C++, C99 macro
2562 ))
2563 (preprocessor-keywords
2564 (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
2565 "ident" "if" "ifdef" "ifndef" "import" "include"
2566 "line" "pragma" "unassert" "undef" "warning"))
2567 (objc-keywords
2568 (mdw-regexps "class" "defs" "encode" "end" "implementation"
2569 "interface" "private" "protected" "protocol" "public"
2570 "selector")))
2571
2572 (setq font-lock-keywords
2573 (list
2574
2575 ;; Fontify include files as strings.
2576 (list (concat "^[ \t]*\\#[ \t]*"
2577 "\\(include\\|import\\)"
2578 "[ \t]*\\(<[^>]+>?\\)")
2579 '(2 font-lock-string-face))
2580
2581 ;; Preprocessor directives are `references'?.
2582 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
2583 preprocessor-keywords
2584 "\\)\\>\\|[0-9]+\\|$\\)\\)")
2585 '(1 font-lock-keyword-face))
2586
2587 ;; Handle the keywords defined above.
2588 (list (concat "@\\<\\(" objc-keywords "\\)\\>")
2589 '(0 font-lock-keyword-face))
2590
2591 (list (concat "\\<\\(" c-keywords "\\)\\>")
2592 '(0 font-lock-keyword-face))
2593
2594 (list (concat "\\<\\(" c-builtins "\\)\\>")
2595 '(0 font-lock-variable-name-face))
2596
2597 ;; Handle numbers too.
2598 ;;
2599 ;; This looks strange, I know. It corresponds to the
2600 ;; preprocessor's idea of what a number looks like, rather than
2601 ;; anything sensible.
2602 (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
2603 "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
2604 '(0 mdw-number-face))
2605
2606 ;; And anything else is punctuation.
2607 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2608 '(0 mdw-punct-face))))))
2609
2610 (define-derived-mode sod-mode c-mode "Sod"
2611 "Major mode for editing Sod code.")
2612 (push '("\\.sod$" . sod-mode) auto-mode-alist)
2613
2614 (dolist (hook '(c-mode-hook objc-mode-hook c++-mode-hook))
2615 (add-hook hook 'mdw-misc-mode-config t)
2616 (add-hook hook 'mdw-fontify-c-and-c++ t))
2617
2618 ;;;--------------------------------------------------------------------------
2619 ;;; AP calc mode.
2620
2621 (define-derived-mode apcalc-mode c-mode "AP Calc"
2622 "Major mode for editing Calc code.")
2623
2624 (defun mdw-fontify-apcalc ()
2625
2626 ;; Fiddle with some syntax codes.
2627 (modify-syntax-entry ?* ". 23")
2628 (modify-syntax-entry ?/ ". 14")
2629
2630 ;; Other stuff.
2631 (setq comment-start "/* ")
2632 (setq comment-end " */")
2633 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2634
2635 ;; Now define things to be fontified.
2636 (make-local-variable 'font-lock-keywords)
2637 (let ((c-keywords
2638 (mdw-regexps "break" "case" "cd" "continue" "define" "default"
2639 "do" "else" "exit" "for" "global" "goto" "help" "if"
2640 "local" "mat" "obj" "print" "quit" "read" "return"
2641 "show" "static" "switch" "while" "write")))
2642
2643 (setq font-lock-keywords
2644 (list
2645
2646 ;; Handle the keywords defined above.
2647 (list (concat "\\<\\(" c-keywords "\\)\\>")
2648 '(0 font-lock-keyword-face))
2649
2650 ;; Handle numbers too.
2651 ;;
2652 ;; This looks strange, I know. It corresponds to the
2653 ;; preprocessor's idea of what a number looks like, rather than
2654 ;; anything sensible.
2655 (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
2656 "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
2657 '(0 mdw-number-face))
2658
2659 ;; And anything else is punctuation.
2660 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2661 '(0 mdw-punct-face))))))
2662
2663 (progn
2664 (add-hook 'apcalc-mode-hook 'mdw-misc-mode-config t)
2665 (add-hook 'apcalc-mode-hook 'mdw-fontify-apcalc t))
2666
2667 ;;;--------------------------------------------------------------------------
2668 ;;; Java programming configuration.
2669
2670 ;; Make indentation nice.
2671
2672 (mdw-define-c-style mdw-java ()
2673 (c-basic-offset . 2)
2674 (c-backslash-column . 72)
2675 (c-offsets-alist (substatement-open . 0)
2676 (label . +)
2677 (case-label . +)
2678 (access-label . 0)
2679 (inclass . +)
2680 (statement-case-intro . +)))
2681 (mdw-set-default-c-style 'java-mode 'mdw-java)
2682
2683 ;; Declare Java fontification style.
2684
2685 (defun mdw-fontify-java ()
2686
2687 ;; Fiddle with some syntax codes.
2688 (modify-syntax-entry ?@ ".")
2689 (modify-syntax-entry ?@ "." font-lock-syntax-table)
2690
2691 ;; Other stuff.
2692 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2693
2694 ;; Now define things to be fontified.
2695 (make-local-variable 'font-lock-keywords)
2696 (let ((java-keywords
2697 (mdw-regexps "abstract" "assert"
2698 "boolean" "break" "byte"
2699 "case" "catch" "char" "class" "const" "continue"
2700 "default" "do" "double"
2701 "else" "enum" "extends"
2702 "final" "finally" "float" "for"
2703 "goto"
2704 "if" "implements" "import" "instanceof" "int"
2705 "interface"
2706 "long"
2707 "native" "new"
2708 "package" "private" "protected" "public"
2709 "return"
2710 "short" "static" "strictfp" "switch" "synchronized"
2711 "throw" "throws" "transient" "try"
2712 "void" "volatile"
2713 "while"))
2714
2715 (java-builtins
2716 (mdw-regexps "false" "null" "super" "this" "true")))
2717
2718 (setq font-lock-keywords
2719 (list
2720
2721 ;; Handle the keywords defined above.
2722 (list (concat "\\<\\(" java-keywords "\\)\\>")
2723 '(0 font-lock-keyword-face))
2724
2725 ;; Handle the magic builtins defined above.
2726 (list (concat "\\<\\(" java-builtins "\\)\\>")
2727 '(0 font-lock-variable-name-face))
2728
2729 ;; Handle numbers too.
2730 ;;
2731 ;; The following isn't quite right, but it's close enough.
2732 (list (concat "\\<\\("
2733 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2734 "[0-9]+\\(\\.[0-9]*\\)?"
2735 "\\([eE][-+]?[0-9]+\\)?\\)"
2736 "[lLfFdD]?")
2737 '(0 mdw-number-face))
2738
2739 ;; And anything else is punctuation.
2740 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2741 '(0 mdw-punct-face))))))
2742
2743 (progn
2744 (add-hook 'java-mode-hook 'mdw-misc-mode-config t)
2745 (add-hook 'java-mode-hook 'mdw-fontify-java t))
2746
2747 ;;;--------------------------------------------------------------------------
2748 ;;; Javascript programming configuration.
2749
2750 (defun mdw-javascript-style ()
2751 (setq js-indent-level 2)
2752 (setq js-expr-indent-offset 0))
2753
2754 (defun mdw-fontify-javascript ()
2755
2756 ;; Other stuff.
2757 (mdw-javascript-style)
2758 (setq js-auto-indent-flag t)
2759
2760 ;; Now define things to be fontified.
2761 (make-local-variable 'font-lock-keywords)
2762 (let ((javascript-keywords
2763 (mdw-regexps "abstract" "boolean" "break" "byte" "case" "catch"
2764 "char" "class" "const" "continue" "debugger" "default"
2765 "delete" "do" "double" "else" "enum" "export" "extends"
2766 "final" "finally" "float" "for" "function" "goto" "if"
2767 "implements" "import" "in" "instanceof" "int"
2768 "interface" "let" "long" "native" "new" "package"
2769 "private" "protected" "public" "return" "short"
2770 "static" "super" "switch" "synchronized" "throw"
2771 "throws" "transient" "try" "typeof" "var" "void"
2772 "volatile" "while" "with" "yield"))
2773 (javascript-builtins
2774 (mdw-regexps "false" "null" "undefined" "Infinity" "NaN" "true"
2775 "arguments" "this")))
2776
2777 (setq font-lock-keywords
2778 (list
2779
2780 ;; Handle the keywords defined above.
2781 (list (concat "\\_<\\(" javascript-keywords "\\)\\_>")
2782 '(0 font-lock-keyword-face))
2783
2784 ;; Handle the predefined builtins defined above.
2785 (list (concat "\\_<\\(" javascript-builtins "\\)\\_>")
2786 '(0 font-lock-variable-name-face))
2787
2788 ;; Handle numbers too.
2789 ;;
2790 ;; The following isn't quite right, but it's close enough.
2791 (list (concat "\\_<\\("
2792 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2793 "[0-9]+\\(\\.[0-9]*\\)?"
2794 "\\([eE][-+]?[0-9]+\\)?\\)"
2795 "[lLfFdD]?")
2796 '(0 mdw-number-face))
2797
2798 ;; And anything else is punctuation.
2799 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2800 '(0 mdw-punct-face))))))
2801
2802 (progn
2803 (add-hook 'js-mode-hook 'mdw-misc-mode-config t)
2804 (add-hook 'js-mode-hook 'mdw-fontify-javascript t))
2805
2806 ;;;--------------------------------------------------------------------------
2807 ;;; Scala programming configuration.
2808
2809 (defun mdw-fontify-scala ()
2810
2811 ;; Comment filling.
2812 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2813
2814 ;; Define things to be fontified.
2815 (make-local-variable 'font-lock-keywords)
2816 (let ((scala-keywords
2817 (mdw-regexps "abstract" "case" "catch" "class" "def" "do" "else"
2818 "extends" "final" "finally" "for" "forSome" "if"
2819 "implicit" "import" "lazy" "match" "new" "object"
2820 "override" "package" "private" "protected" "return"
2821 "sealed" "throw" "trait" "try" "type" "val"
2822 "var" "while" "with" "yield"))
2823 (scala-constants
2824 (mdw-regexps "false" "null" "super" "this" "true"))
2825 (punctuation "[-!%^&*=+:@#~/?\\|`]"))
2826
2827 (setq font-lock-keywords
2828 (list
2829
2830 ;; Magical identifiers between backticks.
2831 (list (concat "`\\([^`]+\\)`")
2832 '(1 font-lock-variable-name-face))
2833
2834 ;; Handle the keywords defined above.
2835 (list (concat "\\_<\\(" scala-keywords "\\)\\_>")
2836 '(0 font-lock-keyword-face))
2837
2838 ;; Handle the constants defined above.
2839 (list (concat "\\_<\\(" scala-constants "\\)\\_>")
2840 '(0 font-lock-variable-name-face))
2841
2842 ;; Magical identifiers between backticks.
2843 (list (concat "`\\([^`]+\\)`")
2844 '(1 font-lock-variable-name-face))
2845
2846 ;; Handle numbers too.
2847 ;;
2848 ;; As usual, not quite right.
2849 (list (concat "\\_<\\("
2850 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2851 "[0-9]+\\(\\.[0-9]*\\)?"
2852 "\\([eE][-+]?[0-9]+\\)?\\)"
2853 "[lLfFdD]?")
2854 '(0 mdw-number-face))
2855
2856 ;; And everything else is punctuation.
2857 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2858 '(0 mdw-punct-face)))
2859
2860 font-lock-syntactic-keywords
2861 (list
2862
2863 ;; Single quotes around characters. But not when used to quote
2864 ;; symbol names. Ugh.
2865 (list (concat "\\('\\)"
2866 "\\(" "."
2867 "\\|" "\\\\" "\\(" "\\\\\\\\" "\\)*"
2868 "u+" "[0-9a-fA-F]\\{4\\}"
2869 "\\|" "\\\\" "[0-7]\\{1,3\\}"
2870 "\\|" "\\\\" "." "\\)"
2871 "\\('\\)")
2872 '(1 "\"")
2873 '(4 "\""))))))
2874
2875 (progn
2876 (add-hook 'scala-mode-hook 'mdw-misc-mode-config t)
2877 (add-hook 'scala-mode-hook 'mdw-fontify-scala t))
2878
2879 ;;;--------------------------------------------------------------------------
2880 ;;; C# programming configuration.
2881
2882 ;; Make indentation nice.
2883
2884 (mdw-define-c-style mdw-csharp ()
2885 (c-basic-offset . 2)
2886 (c-backslash-column . 72)
2887 (c-offsets-alist (substatement-open . 0)
2888 (label . 0)
2889 (case-label . +)
2890 (access-label . 0)
2891 (inclass . +)
2892 (statement-case-intro . +)))
2893 (mdw-set-default-c-style 'csharp-mode 'mdw-csharp)
2894
2895 ;; Declare C# fontification style.
2896
2897 (defun mdw-fontify-csharp ()
2898
2899 ;; Other stuff.
2900 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2901
2902 ;; Now define things to be fontified.
2903 (make-local-variable 'font-lock-keywords)
2904 (let ((csharp-keywords
2905 (mdw-regexps "abstract" "as" "bool" "break" "byte" "case" "catch"
2906 "char" "checked" "class" "const" "continue" "decimal"
2907 "default" "delegate" "do" "double" "else" "enum"
2908 "event" "explicit" "extern" "finally" "fixed" "float"
2909 "for" "foreach" "goto" "if" "implicit" "in" "int"
2910 "interface" "internal" "is" "lock" "long" "namespace"
2911 "new" "object" "operator" "out" "override" "params"
2912 "private" "protected" "public" "readonly" "ref"
2913 "return" "sbyte" "sealed" "short" "sizeof"
2914 "stackalloc" "static" "string" "struct" "switch"
2915 "throw" "try" "typeof" "uint" "ulong" "unchecked"
2916 "unsafe" "ushort" "using" "virtual" "void" "volatile"
2917 "while" "yield"))
2918
2919 (csharp-builtins
2920 (mdw-regexps "base" "false" "null" "this" "true")))
2921
2922 (setq font-lock-keywords
2923 (list
2924
2925 ;; Handle the keywords defined above.
2926 (list (concat "\\<\\(" csharp-keywords "\\)\\>")
2927 '(0 font-lock-keyword-face))
2928
2929 ;; Handle the magic builtins defined above.
2930 (list (concat "\\<\\(" csharp-builtins "\\)\\>")
2931 '(0 font-lock-variable-name-face))
2932
2933 ;; Handle numbers too.
2934 ;;
2935 ;; The following isn't quite right, but it's close enough.
2936 (list (concat "\\<\\("
2937 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2938 "[0-9]+\\(\\.[0-9]*\\)?"
2939 "\\([eE][-+]?[0-9]+\\)?\\)"
2940 "[lLfFdD]?")
2941 '(0 mdw-number-face))
2942
2943 ;; And anything else is punctuation.
2944 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2945 '(0 mdw-punct-face))))))
2946
2947 (define-derived-mode csharp-mode java-mode "C#"
2948 "Major mode for editing C# code.")
2949
2950 (add-hook 'csharp-mode-hook 'mdw-fontify-csharp t)
2951
2952 ;;;--------------------------------------------------------------------------
2953 ;;; F# programming configuration.
2954
2955 (setq fsharp-indent-offset 2)
2956
2957 (defun mdw-fontify-fsharp ()
2958
2959 (let ((punct "=<>+-*/|&%!@?"))
2960 (do ((i 0 (1+ i)))
2961 ((>= i (length punct)))
2962 (modify-syntax-entry (aref punct i) ".")))
2963
2964 (modify-syntax-entry ?_ "_")
2965 (modify-syntax-entry ?( "(")
2966 (modify-syntax-entry ?) ")")
2967
2968 (setq indent-tabs-mode nil)
2969
2970 (let ((fsharp-keywords
2971 (mdw-regexps "abstract" "and" "as" "assert" "atomic"
2972 "begin" "break"
2973 "checked" "class" "component" "const" "constraint"
2974 "constructor" "continue"
2975 "default" "delegate" "do" "done" "downcast" "downto"
2976 "eager" "elif" "else" "end" "exception" "extern"
2977 "finally" "fixed" "for" "fori" "fun" "function"
2978 "functor"
2979 "global"
2980 "if" "in" "include" "inherit" "inline" "interface"
2981 "internal"
2982 "lazy" "let"
2983 "match" "measure" "member" "method" "mixin" "module"
2984 "mutable"
2985 "namespace" "new"
2986 "object" "of" "open" "or" "override"
2987 "parallel" "params" "private" "process" "protected"
2988 "public" "pure"
2989 "rec" "recursive" "return"
2990 "sealed" "sig" "static" "struct"
2991 "tailcall" "then" "to" "trait" "try" "type"
2992 "upcast" "use"
2993 "val" "virtual" "void" "volatile"
2994 "when" "while" "with"
2995 "yield"))
2996
2997 (fsharp-builtins
2998 (mdw-regexps "asr" "land" "lor" "lsl" "lsr" "lxor" "mod"
2999 "base" "false" "null" "true"))
3000
3001 (bang-keywords
3002 (mdw-regexps "do" "let" "return" "use" "yield"))
3003
3004 (preprocessor-keywords
3005 (mdw-regexps "if" "indent" "else" "endif")))
3006
3007 (setq font-lock-keywords
3008 (list (list (concat "\\(^\\|[^\"]\\)"
3009 "\\(" "(\\*"
3010 "[^*]*\\*+"
3011 "\\(" "[^)*]" "[^*]*" "\\*+" "\\)*"
3012 ")"
3013 "\\|"
3014 "//.*"
3015 "\\)")
3016 '(2 font-lock-comment-face))
3017
3018 (list (concat "'" "\\("
3019 "\\\\"
3020 "\\(" "[ntbr'\\]"
3021 "\\|" "[0-9][0-9][0-9]"
3022 "\\|" "u" "[0-9a-fA-F]\\{4\\}"
3023 "\\|" "U" "[0-9a-fA-F]\\{8\\}"
3024 "\\)"
3025 "\\|"
3026 "." "\\)" "'"
3027 "\\|"
3028 "\"" "[^\"\\]*"
3029 "\\(" "\\\\" "\\(.\\|\n\\)"
3030 "[^\"\\]*" "\\)*"
3031 "\\(\"\\|\\'\\)")
3032 '(0 font-lock-string-face))
3033
3034 (list (concat "\\_<\\(" bang-keywords "\\)!" "\\|"
3035 "^#[ \t]*\\(" preprocessor-keywords "\\)\\_>"
3036 "\\|"
3037 "\\_<\\(" fsharp-keywords "\\)\\_>")
3038 '(0 font-lock-keyword-face))
3039 (list (concat "\\<\\(" fsharp-builtins "\\)\\_>")
3040 '(0 font-lock-variable-name-face))
3041
3042 (list (concat "\\_<"
3043 "\\(" "0[bB][01]+" "\\|"
3044 "0[oO][0-7]+" "\\|"
3045 "0[xX][0-9a-fA-F]+" "\\)"
3046 "\\(" "lf\\|LF" "\\|"
3047 "[uU]?[ysnlL]?" "\\)"
3048 "\\|"
3049 "\\_<"
3050 "[0-9]+" "\\("
3051 "[mMQRZING]"
3052 "\\|"
3053 "\\(\\.[0-9]*\\)?"
3054 "\\([eE][-+]?[0-9]+\\)?"
3055 "[fFmM]?"
3056 "\\|"
3057 "[uU]?[ysnlL]?"
3058 "\\)")
3059 '(0 mdw-number-face))
3060
3061 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3062 '(0 mdw-punct-face))))))
3063
3064 (defun mdw-fontify-inferior-fsharp ()
3065 (mdw-fontify-fsharp)
3066 (setq font-lock-keywords
3067 (append (list (list "^[#-]" '(0 font-lock-comment-face))
3068 (list "^>" '(0 font-lock-keyword-face)))
3069 font-lock-keywords)))
3070
3071 (progn
3072 (add-hook 'fsharp-mode-hook 'mdw-misc-mode-config t)
3073 (add-hook 'fsharp-mode-hook 'mdw-fontify-fsharp t)
3074 (add-hook 'inferior-fsharp-mode-hooks 'mdw-fontify-inferior-fsharp t))
3075
3076 ;;;--------------------------------------------------------------------------
3077 ;;; Go programming configuration.
3078
3079 (defun mdw-fontify-go ()
3080
3081 (make-local-variable 'font-lock-keywords)
3082 (let ((go-keywords
3083 (mdw-regexps "break" "case" "chan" "const" "continue"
3084 "default" "defer" "else" "fallthrough" "for"
3085 "func" "go" "goto" "if" "import"
3086 "interface" "map" "package" "range" "return"
3087 "select" "struct" "switch" "type" "var"))
3088 (go-intrinsics
3089 (mdw-regexps "bool" "byte" "complex64" "complex128" "error"
3090 "float32" "float64" "int" "uint8" "int16" "int32"
3091 "int64" "rune" "string" "uint" "uint8" "uint16"
3092 "uint32" "uint64" "uintptr" "void"
3093 "false" "iota" "nil" "true"
3094 "init" "main"
3095 "append" "cap" "copy" "delete" "imag" "len" "make"
3096 "new" "panic" "real" "recover")))
3097
3098 (setq font-lock-keywords
3099 (list
3100
3101 ;; Handle the keywords defined above.
3102 (list (concat "\\<\\(" go-keywords "\\)\\>")
3103 '(0 font-lock-keyword-face))
3104 (list (concat "\\<\\(" go-intrinsics "\\)\\>")
3105 '(0 font-lock-variable-name-face))
3106
3107 ;; Strings and characters.
3108 (list (concat "'"
3109 "\\(" "[^\\']" "\\|"
3110 "\\\\"
3111 "\\(" "[abfnrtv\\'\"]" "\\|"
3112 "[0-7]\\{3\\}" "\\|"
3113 "x" "[0-9A-Fa-f]\\{2\\}" "\\|"
3114 "u" "[0-9A-Fa-f]\\{4\\}" "\\|"
3115 "U" "[0-9A-Fa-f]\\{8\\}" "\\)" "\\)"
3116 "'"
3117 "\\|"
3118 "\""
3119 "\\(" "[^\n\\\"]+" "\\|" "\\\\." "\\)*"
3120 "\\(\"\\|$\\)"
3121 "\\|"
3122 "`" "[^`]+" "`")
3123 '(0 font-lock-string-face))
3124
3125 ;; Handle numbers too.
3126 ;;
3127 ;; The following isn't quite right, but it's close enough.
3128 (list (concat "\\<\\("
3129 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
3130 "[0-9]+\\(\\.[0-9]*\\)?"
3131 "\\([eE][-+]?[0-9]+\\)?\\)")
3132 '(0 mdw-number-face))
3133
3134 ;; And anything else is punctuation.
3135 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3136 '(0 mdw-punct-face))))))
3137 (progn
3138 (add-hook 'go-mode-hook 'mdw-misc-mode-config t)
3139 (add-hook 'go-mode-hook 'mdw-fontify-go t))
3140
3141 ;;;--------------------------------------------------------------------------
3142 ;;; Rust programming configuration.
3143
3144 (setq-default rust-indent-offset 2)
3145
3146 (defun mdw-self-insert-and-indent (count)
3147 (interactive "p")
3148 (self-insert-command count)
3149 (indent-according-to-mode))
3150
3151 (defun mdw-fontify-rust ()
3152
3153 ;; Hack syntax categories.
3154 (modify-syntax-entry ?$ ".")
3155 (modify-syntax-entry ?% ".")
3156 (modify-syntax-entry ?= ".")
3157
3158 ;; Fontify keywords and things.
3159 (make-local-variable 'font-lock-keywords)
3160 (let ((rust-keywords
3161 (mdw-regexps "abstract" "alignof" "as" "async" "await"
3162 "become" "box" "break"
3163 "const" "continue" "crate"
3164 "do" "dyn"
3165 "else" "enum" "extern"
3166 "final" "fn" "for"
3167 "if" "impl" "in"
3168 "let" "loop"
3169 "macro" "match" "mod" "move" "mut"
3170 "offsetof" "override"
3171 "priv" "proc" "pub" "pure"
3172 "ref" "return"
3173 "sizeof" "static" "struct" "super"
3174 "trait" "try" "type" "typeof"
3175 "union" "unsafe" "unsized" "use"
3176 "virtual"
3177 "where" "while"
3178 "yield"))
3179 (rust-builtins
3180 (mdw-regexps "array" "pointer" "slice" "tuple"
3181 "bool" "true" "false"
3182 "f32" "f64"
3183 "i8" "i16" "i32" "i64" "isize"
3184 "u8" "u16" "u32" "u64" "usize"
3185 "char" "str"
3186 "self" "Self")))
3187 (setq font-lock-keywords
3188 (list
3189
3190 ;; Handle the keywords defined above.
3191 (list (concat "\\_<\\(" rust-keywords "\\)\\_>")
3192 '(0 font-lock-keyword-face))
3193 (list (concat "\\_<\\(" rust-builtins "\\)\\_>")
3194 '(0 font-lock-variable-name-face))
3195
3196 ;; Handle numbers too.
3197 (list (concat "\\_<\\("
3198 "[0-9][0-9_]*"
3199 "\\(" "\\(\\.[0-9_]+\\)?[eE][-+]?[0-9_]+"
3200 "\\|" "\\.[0-9_]+"
3201 "\\)"
3202 "\\(f32\\|f64\\)?"
3203 "\\|" "\\(" "[0-9][0-9_]*"
3204 "\\|" "0x[0-9a-fA-F_]+"
3205 "\\|" "0o[0-7_]+"
3206 "\\|" "0b[01_]+"
3207 "\\)"
3208 "\\([ui]\\(8\\|16\\|32\\|64\\|size\\)\\)?"
3209 "\\)\\_>")
3210 '(0 mdw-number-face))
3211
3212 ;; And anything else is punctuation.
3213 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3214 '(0 mdw-punct-face)))
3215 font-lock-syntactic-face-function . nil))
3216
3217 ;; Hack key bindings.
3218 (local-set-key [?{] 'mdw-self-insert-and-indent)
3219 (local-set-key [?}] 'mdw-self-insert-and-indent))
3220
3221 (progn
3222 (add-hook 'rust-mode-hook 'mdw-misc-mode-config t)
3223 (add-hook 'rust-mode-hook 'mdw-fontify-rust t))
3224
3225 ;;;--------------------------------------------------------------------------
3226 ;;; Awk programming configuration.
3227
3228 ;; Make Awk indentation nice.
3229
3230 (mdw-define-c-style mdw-awk ()
3231 (c-basic-offset . 2)
3232 (c-offsets-alist (substatement-open . 0)
3233 (c-backslash-column . 72)
3234 (statement-cont . 0)
3235 (statement-case-intro . +)))
3236 (mdw-set-default-c-style 'awk-mode 'mdw-awk)
3237
3238 ;; Declare Awk fontification style.
3239
3240 (defun mdw-fontify-awk ()
3241
3242 ;; Miscellaneous fiddling.
3243 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3244
3245 ;; Now define things to be fontified.
3246 (make-local-variable 'font-lock-keywords)
3247 (let ((c-keywords
3248 (mdw-regexps "BEGIN" "END" "ARGC" "ARGIND" "ARGV" "CONVFMT"
3249 "ENVIRON" "ERRNO" "FIELDWIDTHS" "FILENAME" "FNR"
3250 "FS" "IGNORECASE" "NF" "NR" "OFMT" "OFS" "ORS" "RS"
3251 "RSTART" "RLENGTH" "RT" "SUBSEP"
3252 "atan2" "break" "close" "continue" "cos" "delete"
3253 "do" "else" "exit" "exp" "fflush" "file" "for" "func"
3254 "function" "gensub" "getline" "gsub" "if" "in"
3255 "index" "int" "length" "log" "match" "next" "rand"
3256 "return" "print" "printf" "sin" "split" "sprintf"
3257 "sqrt" "srand" "strftime" "sub" "substr" "system"
3258 "systime" "tolower" "toupper" "while")))
3259
3260 (setq font-lock-keywords
3261 (list
3262
3263 ;; Handle the keywords defined above.
3264 (list (concat "\\<\\(" c-keywords "\\)\\>")
3265 '(0 font-lock-keyword-face))
3266
3267 ;; Handle numbers too.
3268 ;;
3269 ;; The following isn't quite right, but it's close enough.
3270 (list (concat "\\<\\("
3271 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
3272 "[0-9]+\\(\\.[0-9]*\\)?"
3273 "\\([eE][-+]?[0-9]+\\)?\\)"
3274 "[uUlL]*")
3275 '(0 mdw-number-face))
3276
3277 ;; And anything else is punctuation.
3278 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3279 '(0 mdw-punct-face))))))
3280
3281 (progn
3282 (add-hook 'awk-mode-hook 'mdw-misc-mode-config t)
3283 (add-hook 'awk-mode-hook 'mdw-fontify-awk t))
3284
3285 ;;;--------------------------------------------------------------------------
3286 ;;; Perl programming style.
3287
3288 ;; Perl indentation style.
3289
3290 (setq-default perl-indent-level 2)
3291
3292 (setq-default cperl-indent-level 2
3293 cperl-continued-statement-offset 2
3294 cperl-indent-region-fix-constructs nil
3295 cperl-continued-brace-offset 0
3296 cperl-brace-offset -2
3297 cperl-brace-imaginary-offset 0
3298 cperl-label-offset 0)
3299
3300 ;; Define perl fontification style.
3301
3302 (defun mdw-fontify-perl ()
3303
3304 ;; Miscellaneous fiddling.
3305 (modify-syntax-entry ?$ "\\")
3306 (modify-syntax-entry ?$ "\\" font-lock-syntax-table)
3307 (modify-syntax-entry ?: "." font-lock-syntax-table)
3308 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3309
3310 ;; Now define fontification things.
3311 (make-local-variable 'font-lock-keywords)
3312 (let ((perl-keywords
3313 (mdw-regexps "and"
3314 "break"
3315 "cmp" "continue"
3316 "default" "do"
3317 "else" "elsif" "eq"
3318 "for" "foreach"
3319 "ge" "given" "gt" "goto"
3320 "if"
3321 "last" "le" "local" "lt"
3322 "my"
3323 "ne" "next"
3324 "or" "our"
3325 "package"
3326 "redo" "require" "return"
3327 "sub"
3328 "undef" "unless" "until" "use"
3329 "when" "while")))
3330
3331 (setq font-lock-keywords
3332 (list
3333
3334 ;; Set up the keywords defined above.
3335 (list (concat "\\<\\(" perl-keywords "\\)\\>")
3336 '(0 font-lock-keyword-face))
3337
3338 ;; At least numbers are simpler than C.
3339 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
3340 "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\)?"
3341 "\\([eE][-+]?[0-9_]+\\)?")
3342 '(0 mdw-number-face))
3343
3344 ;; And anything else is punctuation.
3345 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3346 '(0 mdw-punct-face))))))
3347
3348 (defun perl-number-tests (&optional arg)
3349 "Assign consecutive numbers to lines containing `#t'. With ARG,
3350 strip numbers instead."
3351 (interactive "P")
3352 (save-excursion
3353 (goto-char (point-min))
3354 (let ((i 0) (fmt (if arg "" " %4d")))
3355 (while (search-forward "#t" nil t)
3356 (delete-region (point) (line-end-position))
3357 (setq i (1+ i))
3358 (insert (format fmt i)))
3359 (goto-char (point-min))
3360 (if (re-search-forward "\\(tests\\s-*=>\\s-*\\)\\w*" nil t)
3361 (replace-match (format "\\1%d" i))))))
3362
3363 (dolist (hook '(perl-mode-hook cperl-mode-hook))
3364 (add-hook hook 'mdw-misc-mode-config t)
3365 (add-hook hook 'mdw-fontify-perl t))
3366
3367 ;;;--------------------------------------------------------------------------
3368 ;;; Python programming style.
3369
3370 (setq-default py-indent-offset 2
3371 python-indent 2
3372 python-indent-offset 2
3373 python-fill-docstring-style 'symmetric)
3374
3375 (defun mdw-fontify-pythonic (keywords)
3376
3377 ;; Miscellaneous fiddling.
3378 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3379 (setq indent-tabs-mode nil)
3380 (set (make-local-variable 'forward-sexp-function) nil)
3381
3382 ;; Now define fontification things.
3383 (make-local-variable 'font-lock-keywords)
3384 (setq font-lock-keywords
3385 (list
3386
3387 ;; Set up the keywords defined above.
3388 (list (concat "\\_<\\(" keywords "\\)\\_>")
3389 '(0 font-lock-keyword-face))
3390
3391 ;; At least numbers are simpler than C.
3392 (list (concat "\\_<0\\([xX][0-9a-fA-F]+\\|[oO]?[0-7]+\\|[bB][01]+\\)\\|"
3393 "\\_<[0-9][0-9]*\\(\\.[0-9]*\\)?"
3394 "\\([eE][-+]?[0-9]+\\|[lL]\\)?")
3395 '(0 mdw-number-face))
3396
3397 ;; And anything else is punctuation.
3398 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3399 '(0 mdw-punct-face)))))
3400
3401 ;; Define Python fontification styles.
3402
3403 (defun mdw-fontify-python ()
3404 (mdw-fontify-pythonic
3405 (mdw-regexps "and" "as" "assert" "break" "class" "continue" "def"
3406 "del" "elif" "else" "except" "exec" "finally" "for"
3407 "from" "global" "if" "import" "in" "is" "lambda"
3408 "not" "or" "pass" "print" "raise" "return" "try"
3409 "while" "with" "yield")))
3410
3411 (defun mdw-fontify-pyrex ()
3412 (mdw-fontify-pythonic
3413 (mdw-regexps "and" "as" "assert" "break" "cdef" "class" "continue"
3414 "ctypedef" "def" "del" "elif" "else" "enum" "except" "exec"
3415 "extern" "finally" "for" "from" "global" "if"
3416 "import" "in" "is" "lambda" "not" "or" "pass" "print"
3417 "property" "raise" "return" "struct" "try" "while" "with"
3418 "yield")))
3419
3420 (define-derived-mode pyrex-mode python-mode "Pyrex"
3421 "Major mode for editing Pyrex source code")
3422 (setq auto-mode-alist
3423 (append '(("\\.pyx$" . pyrex-mode)
3424 ("\\.pxd$" . pyrex-mode)
3425 ("\\.pxi$" . pyrex-mode))
3426 auto-mode-alist))
3427
3428 (progn
3429 (add-hook 'python-mode-hook 'mdw-misc-mode-config t)
3430 (add-hook 'python-mode-hook 'mdw-fontify-python t)
3431 (add-hook 'pyrex-mode-hook 'mdw-fontify-pyrex t))
3432
3433 ;;;--------------------------------------------------------------------------
3434 ;;; Lua programming style.
3435
3436 (setq-default lua-indent-level 2)
3437
3438 (defun mdw-fontify-lua ()
3439
3440 ;; Miscellaneous fiddling.
3441 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3442
3443 ;; Now define fontification things.
3444 (make-local-variable 'font-lock-keywords)
3445 (let ((lua-keywords
3446 (mdw-regexps "and" "break" "do" "else" "elseif" "end"
3447 "false" "for" "function" "goto" "if" "in" "local"
3448 "nil" "not" "or" "repeat" "return" "then" "true"
3449 "until" "while")))
3450 (setq font-lock-keywords
3451 (list
3452
3453 ;; Set up the keywords defined above.
3454 (list (concat "\\_<\\(" lua-keywords "\\)\\_>")
3455 '(0 font-lock-keyword-face))
3456
3457 ;; At least numbers are simpler than C.
3458 (list (concat "\\_<\\(" "0[xX]"
3459 "\\(" "[0-9a-fA-F]+"
3460 "\\(\\.[0-9a-fA-F]*\\)?"
3461 "\\|" "\\.[0-9a-fA-F]+"
3462 "\\)"
3463 "\\([pP][-+]?[0-9]+\\)?"
3464 "\\|" "\\(" "[0-9]+"
3465 "\\(\\.[0-9]*\\)?"
3466 "\\|" "\\.[0-9]+"
3467 "\\)"
3468 "\\([eE][-+]?[0-9]+\\)?"
3469 "\\)")
3470 '(0 mdw-number-face))
3471
3472 ;; And anything else is punctuation.
3473 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3474 '(0 mdw-punct-face))))))
3475
3476 (progn
3477 (add-hook 'lua-mode-hook 'mdw-misc-mode-config t)
3478 (add-hook 'lua-mode-hook 'mdw-fontify-lua t))
3479
3480 ;;;--------------------------------------------------------------------------
3481 ;;; Icon programming style.
3482
3483 ;; Icon indentation style.
3484
3485 (setq-default icon-brace-offset 0
3486 icon-continued-brace-offset 0
3487 icon-continued-statement-offset 2
3488 icon-indent-level 2)
3489
3490 ;; Define Icon fontification style.
3491
3492 (defun mdw-fontify-icon ()
3493
3494 ;; Miscellaneous fiddling.
3495 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3496
3497 ;; Now define fontification things.
3498 (make-local-variable 'font-lock-keywords)
3499 (let ((icon-keywords
3500 (mdw-regexps "break" "by" "case" "create" "default" "do" "else"
3501 "end" "every" "fail" "global" "if" "initial"
3502 "invocable" "link" "local" "next" "not" "of"
3503 "procedure" "record" "repeat" "return" "static"
3504 "suspend" "then" "to" "until" "while"))
3505 (preprocessor-keywords
3506 (mdw-regexps "define" "else" "endif" "error" "ifdef" "ifndef"
3507 "include" "line" "undef")))
3508 (setq font-lock-keywords
3509 (list
3510
3511 ;; Set up the keywords defined above.
3512 (list (concat "\\<\\(" icon-keywords "\\)\\>")
3513 '(0 font-lock-keyword-face))
3514
3515 ;; The things that Icon calls keywords.
3516 (list "&\\sw+\\>" '(0 font-lock-variable-name-face))
3517
3518 ;; At least numbers are simpler than C.
3519 (list (concat "\\<[0-9]+"
3520 "\\([rR][0-9a-zA-Z]+\\|"
3521 "\\.[0-9]+\\([eE][+-]?[0-9]+\\)?\\)\\>\\|"
3522 "\\.[0-9]+\\([eE][+-]?[0-9]+\\)?\\>")
3523 '(0 mdw-number-face))
3524
3525 ;; Preprocessor.
3526 (list (concat "^[ \t]*$[ \t]*\\<\\("
3527 preprocessor-keywords
3528 "\\)\\>")
3529 '(0 font-lock-keyword-face))
3530
3531 ;; And anything else is punctuation.
3532 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3533 '(0 mdw-punct-face))))))
3534
3535 (progn
3536 (add-hook 'icon-mode-hook 'mdw-misc-mode-config t)
3537 (add-hook 'icon-mode-hook 'mdw-fontify-icon t))
3538
3539 ;;;--------------------------------------------------------------------------
3540 ;;; Fortran mode.
3541
3542 (defun mdw-fontify-fortran-common ()
3543 (let ((fortran-keywords
3544 (mdw-regexps "access"
3545 "assign"
3546 "associate"
3547 "backspace"
3548 "blank"
3549 "block\\s-*data"
3550 "call"
3551 "case"
3552 "character"
3553 "class"
3554 "close"
3555 "common"
3556 "complex"
3557 "continue"
3558 "critical"
3559 "data"
3560 "dimension"
3561 "do"
3562 "double\\s-*precision"
3563 "else" "elseif" "elsewhere"
3564 "end"
3565 "endblock" "endblockdata"
3566 "endcritical"
3567 "enddo"
3568 "endinterface"
3569 "endmodule"
3570 "endprocedure"
3571 "endprogram"
3572 "endselect"
3573 "endsubmodule"
3574 "endsubroutine"
3575 "endtype"
3576 "endwhere"
3577 "endenum"
3578 "end\\s-*file"
3579 "endforall"
3580 "endfunction"
3581 "endif"
3582 "entry"
3583 "enum"
3584 "equivalence"
3585 "err"
3586 "external"
3587 "file"
3588 "fmt"
3589 "forall"
3590 "form"
3591 "format"
3592 "function"
3593 "go\\s-*to"
3594 "if"
3595 "implicit"
3596 "in" "inout"
3597 "inquire"
3598 "include"
3599 "integer"
3600 "interface"
3601 "intrinsic"
3602 "iostat"
3603 "len"
3604 "logical"
3605 "module"
3606 "open"
3607 "out"
3608 "parameter"
3609 "pause"
3610 "procedure"
3611 "program"
3612 "precision"
3613 "program"
3614 "read"
3615 "real"
3616 "rec"
3617 "recl"
3618 "return"
3619 "rewind"
3620 "save"
3621 "select" "selectcase" "selecttype"
3622 "status"
3623 "stop"
3624 "submodule"
3625 "subroutine"
3626 "then"
3627 "to"
3628 "type"
3629 "unit"
3630 "where"
3631 "write"))
3632 (fortran-operators (mdw-regexps "and"
3633 "eq"
3634 "eqv"
3635 "false"
3636 "ge"
3637 "gt"
3638 "le"
3639 "lt"
3640 "ne"
3641 "neqv"
3642 "not"
3643 "or"
3644 "true"))
3645 (fortran-intrinsics (mdw-regexps "abs" "dabs" "iabs" "cabs"
3646 "atan" "datan" "atan2" "datan2"
3647 "cmplx"
3648 "conjg"
3649 "cos" "dcos" "ccos"
3650 "dble"
3651 "dim" "idim"
3652 "exp" "dexp" "cexp"
3653 "float"
3654 "ifix"
3655 "aimag"
3656 "int" "aint" "idint"
3657 "alog" "dlog" "clog"
3658 "alog10" "dlog10"
3659 "max"
3660 "amax0" "amax1"
3661 "max0" "max1"
3662 "dmax1"
3663 "min"
3664 "amin0" "amin1"
3665 "min0" "min1"
3666 "dmin1"
3667 "mod" "amod" "dmod"
3668 "sin" "dsin" "csin"
3669 "sign" "isign" "dsign"
3670 "sngl"
3671 "sqrt" "dsqrt" "csqrt"
3672 "tanh"))
3673 (preprocessor-keywords
3674 (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
3675 "ident" "if" "ifdef" "ifndef" "import" "include"
3676 "line" "pragma" "unassert" "undef" "warning")))
3677 (setq font-lock-keywords-case-fold-search t
3678 font-lock-keywords
3679 (list
3680
3681 ;; Fontify include files as strings.
3682 (list (concat "^[ \t]*\\#[ \t]*" "include"
3683 "[ \t]*\\(<[^>]+>?\\)")
3684 '(1 font-lock-string-face))
3685
3686 ;; Preprocessor directives are `references'?.
3687 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
3688 preprocessor-keywords
3689 "\\)\\>\\|[0-9]+\\|$\\)\\)")
3690 '(1 font-lock-keyword-face))
3691
3692 ;; Set up the keywords defined above.
3693 (list (concat "\\<\\(" fortran-keywords "\\)\\>")
3694 '(0 font-lock-keyword-face))
3695
3696 ;; Set up the `.foo.' operators.
3697 (list (concat "\\.\\(" fortran-operators "\\)\\.")
3698 '(0 font-lock-keyword-face))
3699
3700 ;; Set up the intrinsic functions.
3701 (list (concat "\\<\\(" fortran-intrinsics "\\)\\>")
3702 '(0 font-lock-variable-name-face))
3703
3704 ;; Numbers.
3705 (list (concat "\\(" "\\<" "[0-9]+" "\\(\\.[0-9]*\\)?"
3706 "\\|" "\\.[0-9]+"
3707 "\\)"
3708 "\\(" "[de]" "[+-]?" "[0-9]+" "\\)?"
3709 "\\(" "_" "\\sw+" "\\)?"
3710 "\\|" "b'[01]*'" "\\|" "'[01]*'b"
3711 "\\|" "b\"[01]*\"" "\\|" "\"[01]*\"b"
3712 "\\|" "o'[0-7]*'" "\\|" "'[0-7]*'o"
3713 "\\|" "o\"[0-7]*\"" "\\|" "\"[0-7]*\"o"
3714 "\\|" "[xz]'[0-9a-f]*'" "\\|" "'[0-9a-f]*'[xz]"
3715 "\\|" "[xz]\"[0-9a-f]*\"" "\\|" "\"[0-9a-f]*\"[xz]")
3716 '(0 mdw-number-face))
3717
3718 ;; Any anything else is punctuation.
3719 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3720 '(0 mdw-punct-face))))
3721
3722 (modify-syntax-entry ?/ "." font-lock-syntax-table)
3723 (modify-syntax-entry ?< ".")
3724 (modify-syntax-entry ?> ".")))
3725
3726 (defun mdw-fontify-fortran () (mdw-fontify-fortran-common))
3727 (defun mdw-fontify-f90 () (mdw-fontify-fortran-common))
3728
3729 (setq fortran-do-indent 2
3730 fortran-if-indent 2
3731 fortran-structure-indent 2
3732 fortran-comment-line-start "*"
3733 fortran-comment-indent-style 'relative
3734 fortran-continuation-string "&"
3735 fortran-continuation-indent 4)
3736
3737 (setq f90-do-indent 2
3738 f90-if-indent 2
3739 f90-program-indent 2
3740 f90-continuation-indent 4
3741 f90-smart-end-names nil
3742 f90-smart-end 'no-blink)
3743
3744 (progn
3745 (add-hook 'fortran-mode-hook 'mdw-misc-mode-config t)
3746 (add-hook 'fortran-mode-hook 'mdw-fontify-fortran t)
3747 (add-hook 'f90-mode-hook 'mdw-misc-mode-config t)
3748 (add-hook 'f90-mode-hook 'mdw-fontify-f90 t))
3749
3750 ;;;--------------------------------------------------------------------------
3751 ;;; Assembler mode.
3752
3753 (defun mdw-fontify-asm ()
3754 (modify-syntax-entry ?' "\"")
3755 (modify-syntax-entry ?. "w")
3756 (modify-syntax-entry ?\n ">")
3757 (setf fill-prefix nil)
3758 (modify-syntax-entry ?. "_")
3759 (modify-syntax-entry ?* ". 23")
3760 (modify-syntax-entry ?/ ". 124b")
3761 (modify-syntax-entry ?\n "> b")
3762 (local-set-key ";" 'self-insert-command)
3763 (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)"))
3764
3765 (defun mdw-asm-set-comment ()
3766 (modify-syntax-entry ?; "."
3767 )
3768 (modify-syntax-entry asm-comment-char "< b")
3769 (setq comment-start (string asm-comment-char ? )))
3770 (add-hook 'asm-mode-local-variables-hook 'mdw-asm-set-comment)
3771 (put 'asm-comment-char 'safe-local-variable 'characterp)
3772
3773 (progn
3774 (add-hook 'asm-mode-hook 'mdw-misc-mode-config t)
3775 (add-hook 'asm-mode-hook 'mdw-fontify-asm t))
3776
3777 ;;;--------------------------------------------------------------------------
3778 ;;; TCL configuration.
3779
3780 (setq-default tcl-indent-level 2)
3781
3782 (defun mdw-fontify-tcl ()
3783 (dolist (ch '(?$))
3784 (modify-syntax-entry ch "."))
3785 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3786 (make-local-variable 'font-lock-keywords)
3787 (setq font-lock-keywords
3788 (list
3789 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
3790 "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\)?"
3791 "\\([eE][-+]?[0-9_]+\\)?")
3792 '(0 mdw-number-face))
3793 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3794 '(0 mdw-punct-face)))))
3795
3796 (progn
3797 (add-hook 'tcl-mode-hook 'mdw-misc-mode-config t)
3798 (add-hook 'tcl-mode-hook 'mdw-fontify-tcl t))
3799
3800 ;;;--------------------------------------------------------------------------
3801 ;;; Dylan programming configuration.
3802
3803 (defun mdw-fontify-dylan ()
3804
3805 (make-local-variable 'font-lock-keywords)
3806
3807 ;; Horrors. `dylan-mode' sets the `major-mode' name after calling this
3808 ;; hook, which undoes all of our configuration.
3809 (setq major-mode 'dylan-mode)
3810 (font-lock-set-defaults)
3811
3812 (let* ((word "[-_a-zA-Z!*@<>$%]+")
3813 (dylan-keywords (mdw-regexps
3814
3815 "C-address" "C-callable-wrapper" "C-function"
3816 "C-mapped-subtype" "C-pointer-type" "C-struct"
3817 "C-subtype" "C-union" "C-variable"
3818
3819 "above" "abstract" "afterwards" "all"
3820 "begin" "below" "block" "by"
3821 "case" "class" "cleanup" "constant" "create"
3822 "define" "domain"
3823 "else" "elseif" "end" "exception" "export"
3824 "finally" "for" "from" "function"
3825 "generic"
3826 "handler"
3827 "if" "in" "instance" "interface" "iterate"
3828 "keyed-by"
3829 "let" "library" "local"
3830 "macro" "method" "module"
3831 "otherwise"
3832 "profiling"
3833 "select" "slot" "subclass"
3834 "table" "then" "to"
3835 "unless" "until" "use"
3836 "variable" "virtual"
3837 "when" "while"))
3838 (sharp-keywords (mdw-regexps
3839 "all-keys" "key" "next" "rest" "include"
3840 "t" "f")))
3841 (setq font-lock-keywords
3842 (list (list (concat "\\<\\(" dylan-keywords
3843 "\\|" "with\\(out\\)?-" word
3844 "\\)\\>")
3845 '(0 font-lock-keyword-face))
3846 (list (concat "\\<" word ":" "\\|"
3847 "#\\(" sharp-keywords "\\)\\>")
3848 '(0 font-lock-variable-name-face))
3849 (list (concat "\\("
3850 "\\([-+]\\|\\<\\)[0-9]+" "\\("
3851 "\\(\\.[0-9]+\\)?" "\\([eE][-+][0-9]+\\)?"
3852 "\\|" "/[0-9]+"
3853 "\\)"
3854 "\\|" "\\.[0-9]+" "\\([eE][-+][0-9]+\\)?"
3855 "\\|" "#b[01]+"
3856 "\\|" "#o[0-7]+"
3857 "\\|" "#x[0-9a-zA-Z]+"
3858 "\\)\\>")
3859 '(0 mdw-number-face))
3860 (list (concat "\\("
3861 "\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\|"
3862 "\\_<[-+*/=<>:&|]+\\_>"
3863 "\\)")
3864 '(0 mdw-punct-face))))))
3865
3866 (progn
3867 (add-hook 'dylan-mode-hook 'mdw-misc-mode-config t)
3868 (add-hook 'dylan-mode-hook 'mdw-fontify-dylan t))
3869
3870 ;;;--------------------------------------------------------------------------
3871 ;;; Algol 68 configuration.
3872
3873 (setq-default a68-indent-step 2)
3874
3875 (defun mdw-fontify-algol-68 ()
3876
3877 ;; Fix up the syntax table.
3878 (modify-syntax-entry ?# "!" a68-mode-syntax-table)
3879 (dolist (ch '(?- ?+ ?= ?< ?> ?* ?/ ?| ?&))
3880 (modify-syntax-entry ch "." a68-mode-syntax-table))
3881
3882 (make-local-variable 'font-lock-keywords)
3883
3884 (let ((not-comment
3885 (let ((word "COMMENT"))
3886 (do ((regexp (concat "[^" (substring word 0 1) "]+")
3887 (concat regexp "\\|"
3888 (substring word 0 i)
3889 "[^" (substring word i (1+ i)) "]"))
3890 (i 1 (1+ i)))
3891 ((>= i (length word)) regexp)))))
3892 (setq font-lock-keywords
3893 (list (list (concat "\\<COMMENT\\>"
3894 "\\(" not-comment "\\)\\{0,5\\}"
3895 "\\(\\'\\|\\<COMMENT\\>\\)")
3896 '(0 font-lock-comment-face))
3897 (list (concat "\\<CO\\>"
3898 "\\([^C]+\\|C[^O]\\)\\{0,5\\}"
3899 "\\($\\|\\<CO\\>\\)")
3900 '(0 font-lock-comment-face))
3901 (list "\\<[A-Z_]+\\>"
3902 '(0 font-lock-keyword-face))
3903 (list (concat "\\<"
3904 "[0-9]+"
3905 "\\(\\.[0-9]+\\)?"
3906 "\\([eE][-+]?[0-9]+\\)?"
3907 "\\>")
3908 '(0 mdw-number-face))
3909 (list "\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/"
3910 '(0 mdw-punct-face))))))
3911
3912 (dolist (hook '(a68-mode-hook a68-mode-hooks))
3913 (add-hook hook 'mdw-misc-mode-config t)
3914 (add-hook hook 'mdw-fontify-algol-68 t))
3915
3916 ;;;--------------------------------------------------------------------------
3917 ;;; REXX configuration.
3918
3919 (defun mdw-rexx-electric-* ()
3920 (interactive)
3921 (insert ?*)
3922 (rexx-indent-line))
3923
3924 (defun mdw-rexx-indent-newline-indent ()
3925 (interactive)
3926 (rexx-indent-line)
3927 (if abbrev-mode (expand-abbrev))
3928 (newline-and-indent))
3929
3930 (defun mdw-fontify-rexx ()
3931
3932 ;; Various bits of fiddling.
3933 (setq mdw-auto-indent nil)
3934 (local-set-key [?\C-m] 'mdw-rexx-indent-newline-indent)
3935 (local-set-key [?*] 'mdw-rexx-electric-*)
3936 (dolist (ch '(?! ?? ?# ?@ ?$)) (modify-syntax-entry ch "w"))
3937 (dolist (ch '(?¬)) (modify-syntax-entry ch "."))
3938 (mdw-standard-fill-prefix "\\([ \t]*/?\*[ \t]*\\)")
3939
3940 ;; Set up keywords and things for fontification.
3941 (make-local-variable 'font-lock-keywords-case-fold-search)
3942 (setq font-lock-keywords-case-fold-search t)
3943
3944 (setq rexx-indent 2)
3945 (setq rexx-end-indent rexx-indent)
3946 (setq rexx-cont-indent rexx-indent)
3947
3948 (make-local-variable 'font-lock-keywords)
3949 (let ((rexx-keywords
3950 (mdw-regexps "address" "arg" "by" "call" "digits" "do" "drop"
3951 "else" "end" "engineering" "exit" "expose" "for"
3952 "forever" "form" "fuzz" "if" "interpret" "iterate"
3953 "leave" "linein" "name" "nop" "numeric" "off" "on"
3954 "options" "otherwise" "parse" "procedure" "pull"
3955 "push" "queue" "return" "say" "select" "signal"
3956 "scientific" "source" "then" "trace" "to" "until"
3957 "upper" "value" "var" "version" "when" "while"
3958 "with"
3959
3960 "abbrev" "abs" "bitand" "bitor" "bitxor" "b2x"
3961 "center" "center" "charin" "charout" "chars"
3962 "compare" "condition" "copies" "c2d" "c2x"
3963 "datatype" "date" "delstr" "delword" "d2c" "d2x"
3964 "errortext" "format" "fuzz" "insert" "lastpos"
3965 "left" "length" "lineout" "lines" "max" "min"
3966 "overlay" "pos" "queued" "random" "reverse" "right"
3967 "sign" "sourceline" "space" "stream" "strip"
3968 "substr" "subword" "symbol" "time" "translate"
3969 "trunc" "value" "verify" "word" "wordindex"
3970 "wordlength" "wordpos" "words" "xrange" "x2b" "x2c"
3971 "x2d")))
3972
3973 (setq font-lock-keywords
3974 (list
3975
3976 ;; Set up the keywords defined above.
3977 (list (concat "\\<\\(" rexx-keywords "\\)\\>")
3978 '(0 font-lock-keyword-face))
3979
3980 ;; Fontify all symbols the same way.
3981 (list (concat "\\<\\([0-9.][A-Za-z0-9.!?_#@$]*[Ee][+-]?[0-9]+\\|"
3982 "[A-Za-z0-9.!?_#@$]+\\)")
3983 '(0 font-lock-variable-name-face))
3984
3985 ;; And everything else is punctuation.
3986 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3987 '(0 mdw-punct-face))))))
3988
3989 (progn
3990 (add-hook 'rexx-mode-hook 'mdw-misc-mode-config t)
3991 (add-hook 'rexx-mode-hook 'mdw-fontify-rexx t))
3992
3993 ;;;--------------------------------------------------------------------------
3994 ;;; Standard ML programming style.
3995
3996 (setq-default sml-nested-if-indent t
3997 sml-case-indent nil
3998 sml-indent-level 4
3999 sml-type-of-indent nil)
4000
4001 (defun mdw-fontify-sml ()
4002
4003 ;; Make underscore an honorary letter.
4004 (modify-syntax-entry ?' "w")
4005
4006 ;; Set fill prefix.
4007 (mdw-standard-fill-prefix "\\([ \t]*(\*[ \t]*\\)")
4008
4009 ;; Now define fontification things.
4010 (make-local-variable 'font-lock-keywords)
4011 (let ((sml-keywords
4012 (mdw-regexps "abstype" "and" "andalso" "as"
4013 "case"
4014 "datatype" "do"
4015 "else" "end" "eqtype" "exception"
4016 "fn" "fun" "functor"
4017 "handle"
4018 "if" "in" "include" "infix" "infixr"
4019 "let" "local"
4020 "nonfix"
4021 "of" "op" "open" "orelse"
4022 "raise" "rec"
4023 "sharing" "sig" "signature" "struct" "structure"
4024 "then" "type"
4025 "val"
4026 "where" "while" "with" "withtype")))
4027
4028 (setq font-lock-keywords
4029 (list
4030
4031 ;; Set up the keywords defined above.
4032 (list (concat "\\<\\(" sml-keywords "\\)\\>")
4033 '(0 font-lock-keyword-face))
4034
4035 ;; At least numbers are simpler than C.
4036 (list (concat "\\<\\~?"
4037 "\\(0\\([wW]?[xX][0-9a-fA-F]+\\|"
4038 "[wW][0-9]+\\)\\|"
4039 "\\([0-9]+\\(\\.[0-9]+\\)?"
4040 "\\([eE]\\~?"
4041 "[0-9]+\\)?\\)\\)")
4042 '(0 mdw-number-face))
4043
4044 ;; And anything else is punctuation.
4045 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4046 '(0 mdw-punct-face))))))
4047
4048 (progn
4049 (add-hook 'sml-mode-hook 'mdw-misc-mode-config t)
4050 (add-hook 'sml-mode-hook 'mdw-fontify-sml t))
4051
4052 ;;;--------------------------------------------------------------------------
4053 ;;; Haskell configuration.
4054
4055 (setq-default haskell-indent-offset 2)
4056
4057 (defun mdw-fontify-haskell ()
4058
4059 ;; Fiddle with syntax table to get comments right.
4060 (modify-syntax-entry ?' "_")
4061 (modify-syntax-entry ?- ". 12")
4062 (modify-syntax-entry ?\n ">")
4063
4064 ;; Make punctuation be punctuation
4065 (let ((punct "=<>+-*/|&%!@?$.^:#`"))
4066 (do ((i 0 (1+ i)))
4067 ((>= i (length punct)))
4068 (modify-syntax-entry (aref punct i) ".")))
4069
4070 ;; Set fill prefix.
4071 (mdw-standard-fill-prefix "\\([ \t]*{?--?[ \t]*\\)")
4072
4073 ;; Fiddle with fontification.
4074 (make-local-variable 'font-lock-keywords)
4075 (let ((haskell-keywords
4076 (mdw-regexps "as"
4077 "case" "ccall" "class"
4078 "data" "default" "deriving" "do"
4079 "else" "exists"
4080 "forall" "foreign"
4081 "hiding"
4082 "if" "import" "in" "infix" "infixl" "infixr" "instance"
4083 "let"
4084 "mdo" "module"
4085 "newtype"
4086 "of"
4087 "proc"
4088 "qualified"
4089 "rec"
4090 "safe" "stdcall"
4091 "then" "type"
4092 "unsafe"
4093 "where"))
4094 (control-sequences
4095 (mdw-regexps "ACK" "BEL" "BS" "CAN" "CR" "DC1" "DC2" "DC3" "DC4"
4096 "DEL" "DLE" "EM" "ENQ" "EOT" "ESC" "ETB" "ETX" "FF"
4097 "FS" "GS" "HT" "LF" "NAK" "NUL" "RS" "SI" "SO" "SOH"
4098 "SP" "STX" "SUB" "SYN" "US" "VT")))
4099
4100 (setq font-lock-keywords
4101 (list
4102 (list (concat "{-" "[^-]*" "\\(-+[^-}][^-]*\\)*"
4103 "\\(-+}\\|-*\\'\\)"
4104 "\\|"
4105 "--.*$")
4106 '(0 font-lock-comment-face))
4107 (list (concat "\\_<\\(" haskell-keywords "\\)\\_>")
4108 '(0 font-lock-keyword-face))
4109 (list (concat "'\\("
4110 "[^\\]"
4111 "\\|"
4112 "\\\\"
4113 "\\(" "[abfnrtv\\\"']" "\\|"
4114 "^" "\\(" control-sequences "\\|"
4115 "[]A-Z@[\\^_]" "\\)" "\\|"
4116 "\\|"
4117 "[0-9]+" "\\|"
4118 "[oO][0-7]+" "\\|"
4119 "[xX][0-9A-Fa-f]+"
4120 "\\)"
4121 "\\)'")
4122 '(0 font-lock-string-face))
4123 (list "\\_<[A-Z]\\(\\sw+\\|\\s_+\\)*\\_>"
4124 '(0 font-lock-variable-name-face))
4125 (list (concat "\\_<0\\([xX][0-9a-fA-F]+\\|[oO][0-7]+\\)\\|"
4126 "\\_<[0-9]+\\(\\.[0-9]*\\)?"
4127 "\\([eE][-+]?[0-9]+\\)?")
4128 '(0 mdw-number-face))
4129 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4130 '(0 mdw-punct-face))))))
4131
4132 (progn
4133 (add-hook 'haskell-mode-hook 'mdw-misc-mode-config t)
4134 (add-hook 'haskell-mode-hook 'mdw-fontify-haskell t))
4135
4136 ;;;--------------------------------------------------------------------------
4137 ;;; Erlang configuration.
4138
4139 (setq-default erlang-electric-commands nil)
4140
4141 (defun mdw-fontify-erlang ()
4142
4143 ;; Set fill prefix.
4144 (mdw-standard-fill-prefix "\\([ \t]*{?%*[ \t]*\\)")
4145
4146 ;; Fiddle with fontification.
4147 (make-local-variable 'font-lock-keywords)
4148 (let ((erlang-keywords
4149 (mdw-regexps "after" "and" "andalso"
4150 "band" "begin" "bnot" "bor" "bsl" "bsr" "bxor"
4151 "case" "catch" "cond"
4152 "div" "end" "fun" "if" "let" "not"
4153 "of" "or" "orelse"
4154 "query" "receive" "rem" "try" "when" "xor")))
4155
4156 (setq font-lock-keywords
4157 (list
4158 (list "%.*$"
4159 '(0 font-lock-comment-face))
4160 (list (concat "\\<\\(" erlang-keywords "\\)\\>")
4161 '(0 font-lock-keyword-face))
4162 (list (concat "^-\\sw+\\>")
4163 '(0 font-lock-keyword-face))
4164 (list "\\<[0-9]+\\(#[0-9a-zA-Z]+\\|[eE][+-]?[0-9]+\\)?\\>"
4165 '(0 mdw-number-face))
4166 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4167 '(0 mdw-punct-face))))))
4168
4169 (progn
4170 (add-hook 'erlang-mode-hook 'mdw-misc-mode-config t)
4171 (add-hook 'erlang-mode-hook 'mdw-fontify-erlang t))
4172
4173 ;;;--------------------------------------------------------------------------
4174 ;;; Texinfo configuration.
4175
4176 (defun mdw-fontify-texinfo ()
4177
4178 ;; Set fill prefix.
4179 (mdw-standard-fill-prefix "\\([ \t]*@c[ \t]+\\)")
4180
4181 ;; Real fontification things.
4182 (make-local-variable 'font-lock-keywords)
4183 (setq font-lock-keywords
4184 (list
4185
4186 ;; Environment names are keywords.
4187 (list "@\\(end\\) *\\([a-zA-Z]*\\)?"
4188 '(2 font-lock-keyword-face))
4189
4190 ;; Unmark escaped magic characters.
4191 (list "\\(@\\)\\([@{}]\\)"
4192 '(1 font-lock-keyword-face)
4193 '(2 font-lock-variable-name-face))
4194
4195 ;; Make sure we get comments properly.
4196 (list "@c\\(omment\\)?\\( .*\\)?$"
4197 '(0 font-lock-comment-face))
4198
4199 ;; Command names are keywords.
4200 (list "@\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)"
4201 '(0 font-lock-keyword-face))
4202
4203 ;; Fontify TeX special characters as punctuation.
4204 (list "[{}]+"
4205 '(0 mdw-punct-face)))))
4206
4207 (dolist (hook '(texinfo-mode-hook TeXinfo-mode-hook))
4208 (add-hook hook 'mdw-misc-mode-config t)
4209 (add-hook hook 'mdw-fontify-texinfo t))
4210
4211 ;;;--------------------------------------------------------------------------
4212 ;;; TeX and LaTeX configuration.
4213
4214 (setq-default LaTeX-table-label "tbl:"
4215 TeX-auto-untabify nil
4216 LaTeX-syntactic-comments nil
4217 LaTeX-fill-break-at-separators '(\\\[))
4218
4219 (defun mdw-fontify-tex ()
4220 (setq ispell-parser 'tex)
4221 (turn-on-reftex)
4222
4223 ;; Don't make maths into a string.
4224 (modify-syntax-entry ?$ ".")
4225 (modify-syntax-entry ?$ "." font-lock-syntax-table)
4226 (local-set-key [?$] 'self-insert-command)
4227
4228 ;; Make `tab' be useful, given that tab stops in TeX don't work well.
4229 (local-set-key "\C-\M-i" 'indent-relative)
4230 (setq indent-tabs-mode nil)
4231
4232 ;; Set fill prefix.
4233 (mdw-standard-fill-prefix "\\([ \t]*%+[ \t]*\\)")
4234
4235 ;; Real fontification things.
4236 (make-local-variable 'font-lock-keywords)
4237 (setq font-lock-keywords
4238 (list
4239
4240 ;; Environment names are keywords.
4241 (list (concat "\\\\\\(begin\\|end\\|newenvironment\\)"
4242 "{\\([^}\n]*\\)}")
4243 '(2 font-lock-keyword-face))
4244
4245 ;; Suspended environment names are keywords too.
4246 (list (concat "\\\\\\(suspend\\|resume\\)\\(\\[[^]]*\\]\\)?"
4247 "{\\([^}\n]*\\)}")
4248 '(3 font-lock-keyword-face))
4249
4250 ;; Command names are keywords.
4251 (list "\\\\\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)"
4252 '(0 font-lock-keyword-face))
4253
4254 ;; Handle @/.../ for italics.
4255 ;; (list "\\(@/\\)\\([^/]*\\)\\(/\\)"
4256 ;; '(1 font-lock-keyword-face)
4257 ;; '(3 font-lock-keyword-face))
4258
4259 ;; Handle @*...* for boldness.
4260 ;; (list "\\(@\\*\\)\\([^*]*\\)\\(\\*\\)"
4261 ;; '(1 font-lock-keyword-face)
4262 ;; '(3 font-lock-keyword-face))
4263
4264 ;; Handle @`...' for literal syntax things.
4265 ;; (list "\\(@`\\)\\([^']*\\)\\('\\)"
4266 ;; '(1 font-lock-keyword-face)
4267 ;; '(3 font-lock-keyword-face))
4268
4269 ;; Handle @<...> for nonterminals.
4270 ;; (list "\\(@<\\)\\([^>]*\\)\\(>\\)"
4271 ;; '(1 font-lock-keyword-face)
4272 ;; '(3 font-lock-keyword-face))
4273
4274 ;; Handle other @-commands.
4275 ;; (list "@\\([^a-zA-Z]\\|[a-zA-Z]*\\)"
4276 ;; '(0 font-lock-keyword-face))
4277
4278 ;; Make sure we get comments properly.
4279 (list "%.*"
4280 '(0 font-lock-comment-face))
4281
4282 ;; Fontify TeX special characters as punctuation.
4283 (list "[$^_{}#&]"
4284 '(0 mdw-punct-face)))))
4285
4286 (setq TeX-install-font-lock 'tex-font-setup)
4287
4288 (eval-after-load 'font-latex
4289 '(defun font-latex-jit-lock-force-redisplay (buf start end)
4290 "Compatibility for Emacsen not offering `jit-lock-force-redisplay'."
4291 ;; The following block is an expansion of `jit-lock-force-redisplay'
4292 ;; and involved macros taken from CVS Emacs on 2007-04-28.
4293 (with-current-buffer buf
4294 (let ((modified (buffer-modified-p)))
4295 (unwind-protect
4296 (let ((buffer-undo-list t)
4297 (inhibit-read-only t)
4298 (inhibit-point-motion-hooks t)
4299 (inhibit-modification-hooks t)
4300 deactivate-mark
4301 buffer-file-name
4302 buffer-file-truename)
4303 (put-text-property start end 'fontified t))
4304 (unless modified
4305 (restore-buffer-modified-p nil)))))))
4306
4307 (setq TeX-output-view-style
4308 '(("^dvi$"
4309 ("^landscape$" "^pstricks$\\|^pst-\\|^psfrag$")
4310 "%(o?)dvips -t landscape %d -o && xdg-open %f")
4311 ("^dvi$" "^pstricks$\\|^pst-\\|^psfrag$"
4312 "%(o?)dvips %d -o && xdg-open %f")
4313 ("^dvi$"
4314 ("^a4\\(?:dutch\\|paper\\|wide\\)\\|sem-a4$" "^landscape$")
4315 "%(o?)xdvi %dS -paper a4r -s 0 %d")
4316 ("^dvi$" "^a4\\(?:dutch\\|paper\\|wide\\)\\|sem-a4$"
4317 "%(o?)xdvi %dS -paper a4 %d")
4318 ("^dvi$"
4319 ("^a5\\(?:comb\\|paper\\)$" "^landscape$")
4320 "%(o?)xdvi %dS -paper a5r -s 0 %d")
4321 ("^dvi$" "^a5\\(?:comb\\|paper\\)$" "%(o?)xdvi %dS -paper a5 %d")
4322 ("^dvi$" "^b5paper$" "%(o?)xdvi %dS -paper b5 %d")
4323 ("^dvi$" "^letterpaper$" "%(o?)xdvi %dS -paper us %d")
4324 ("^dvi$" "^legalpaper$" "%(o?)xdvi %dS -paper legal %d")
4325 ("^dvi$" "^executivepaper$" "%(o?)xdvi %dS -paper 7.25x10.5in %d")
4326 ("^dvi$" "." "%(o?)xdvi %dS %d")
4327 ("^pdf$" "." "xdg-open %o")
4328 ("^html?$" "." "sensible-browser %o")))
4329
4330 (setq TeX-view-program-list
4331 '(("mupdf" ("mupdf %o" (mode-io-correlate " %(outpage)")))))
4332
4333 (setq TeX-view-program-selection
4334 '(((output-dvi style-pstricks) "dvips and gv")
4335 (output-dvi "xdvi")
4336 (output-pdf "mupdf")
4337 (output-html "sensible-browser")))
4338
4339 (setq TeX-open-quote "\""
4340 TeX-close-quote "\"")
4341
4342 (setq reftex-use-external-file-finders t
4343 reftex-auto-recenter-toc t)
4344
4345 (setq reftex-label-alist
4346 '(("theorem" ?T "th:" "~\\ref{%s}" t ("theorems?" "th\\.") -2)
4347 ("axiom" ?A "ax:" "~\\ref{%s}" t ("axioms?" "ax\\.") -2)
4348 ("definition" ?D "def:" "~\\ref{%s}" t ("definitions?" "def\\.") -2)
4349 ("proposition" ?P "prop:" "~\\ref{%s}" t
4350 ("propositions?" "prop\\.") -2)
4351 ("lemma" ?L "lem:" "~\\ref{%s}" t ("lemmas?" "lem\\.") -2)
4352 ("example" ?X "eg:" "~\\ref{%s}" t ("examples?") -2)
4353 ("exercise" ?E "ex:" "~\\ref{%s}" t ("exercises?" "ex\\.") -2)
4354 ("enumerate" ?i "i:" "~\\ref{%s}" item ("items?"))))
4355 (setq reftex-section-prefixes
4356 '((0 . "part:")
4357 (1 . "ch:")
4358 (t . "sec:")))
4359
4360 (setq bibtex-field-delimiters 'double-quotes
4361 bibtex-align-at-equal-sign t
4362 bibtex-entry-format '(realign opts-or-alts required-fields
4363 numerical-fields last-comma delimiters
4364 unify-case sort-fields braces)
4365 bibtex-sort-ignore-string-entries nil
4366 bibtex-maintain-sorted-entries 'entry-class
4367 bibtex-include-OPTkey t
4368 bibtex-autokey-names-stretch 1
4369 bibtex-autokey-expand-strings t
4370 bibtex-autokey-name-separator "-"
4371 bibtex-autokey-year-length 4
4372 bibtex-autokey-titleword-separator "-"
4373 bibtex-autokey-name-year-separator "-"
4374 bibtex-autokey-year-title-separator ":")
4375
4376 (progn
4377 (dolist (hook '(tex-mode-hook latex-mode-hook
4378 TeX-mode-hook LaTeX-mode-hook))
4379 (add-hook hook 'mdw-misc-mode-config t)
4380 (add-hook hook 'mdw-fontify-tex t))
4381 (add-hook 'bibtex-mode-hook (lambda () (setq fill-column 76))))
4382
4383 ;;;--------------------------------------------------------------------------
4384 ;;; HTML, CSS, and other web foolishness.
4385
4386 (setq-default css-indent-offset 8)
4387
4388 ;;;--------------------------------------------------------------------------
4389 ;;; SGML hacking.
4390
4391 (setq-default psgml-html-build-new-buffer nil)
4392
4393 (defun mdw-sgml-mode ()
4394 (interactive)
4395 (sgml-mode)
4396 (mdw-standard-fill-prefix "")
4397 (make-local-variable 'sgml-delimiters)
4398 (setq sgml-delimiters
4399 '("AND" "&" "COM" "--" "CRO" "&#" "DSC" "]" "DSO" "[" "DTGC" "]"
4400 "DTGO" "[" "ERO" "&" "ETAGO" ":e" "GRPC" ")" "GRPO" "(" "LIT"
4401 "\"" "LITA" "'" "MDC" ">" "MDO" "<!" "MINUS" "-" "MSC" "]]"
4402 "NESTC" "{" "NET" "}" "OPT" "?" "OR" "|" "PERO" "%" "PIC" ">"
4403 "PIO" "<?" "PLUS" "+" "REFC" "." "REP" "*" "RNI" "#" "SEQ" ","
4404 "STAGO" ":" "TAGC" "." "VI" "=" "MS-START" "<![" "MS-END" "]]>"
4405 "XML-ECOM" "-->" "XML-PIC" "?>" "XML-SCOM" "<!--" "XML-TAGCE"
4406 "/>" "NULL" ""))
4407 (setq major-mode 'mdw-sgml-mode)
4408 (setq mode-name "[mdw] SGML")
4409 (run-hooks 'mdw-sgml-mode-hook))
4410
4411 ;;;--------------------------------------------------------------------------
4412 ;;; Configuration files.
4413
4414 (defcustom mdw-conf-quote-normal nil
4415 "Control syntax category of quote characters `\"' and `''.
4416 If this is `t', consider quote characters to be normal
4417 punctuation, as for `conf-quote-normal'. If this is `nil' then
4418 leave quote characters as quotes. If this is a list, then
4419 consider the quote characters in the list to be normal
4420 punctuation. If this is a single quote character, then consider
4421 that character only to be normal punctuation."
4422 :type '(choice boolean character (repeat character))
4423 :safe 'mdw-conf-quote-normal-acceptable-value-p)
4424 (defun mdw-conf-quote-normal-acceptable-value-p (value)
4425 "Is the VALUE is an acceptable value for `mdw-conf-quote-normal'?"
4426 (or (booleanp value)
4427 (every (lambda (v) (memq v '(?\" ?')))
4428 (if (listp value) value (list value)))))
4429
4430 (defun mdw-fix-up-quote ()
4431 "Apply the setting of `mdw-conf-quote-normal'."
4432 (let ((flag mdw-conf-quote-normal))
4433 (cond ((eq flag t)
4434 (conf-quote-normal t))
4435 ((not flag)
4436 nil)
4437 (t
4438 (let ((table (copy-syntax-table (syntax-table))))
4439 (dolist (ch (if (listp flag) flag (list flag)))
4440 (modify-syntax-entry ch "." table))
4441 (set-syntax-table table)
4442 (and font-lock-mode (font-lock-fontify-buffer)))))))
4443
4444 (progn
4445 (add-hook 'conf-mode-hook 'mdw-misc-mode-config t)
4446 (add-hook 'conf-mode-local-variables-hook 'mdw-fix-up-quote t t))
4447
4448 ;;;--------------------------------------------------------------------------
4449 ;;; Shell scripts.
4450
4451 (defun mdw-setup-sh-script-mode ()
4452
4453 ;; Fetch the shell interpreter's name.
4454 (let ((shell-name sh-shell-file))
4455
4456 ;; Try reading the hash-bang line.
4457 (save-excursion
4458 (goto-char (point-min))
4459 (if (looking-at "#![ \t]*\\([^ \t\n]*\\)")
4460 (setq shell-name (match-string 1))))
4461
4462 ;; Now try to set the shell.
4463 ;;
4464 ;; Don't let `sh-set-shell' bugger up my script.
4465 (let ((executable-set-magic #'(lambda (s &rest r) s)))
4466 (sh-set-shell shell-name)))
4467
4468 ;; Don't insert here-document scaffolding automatically.
4469 (local-set-key "<" 'self-insert-command)
4470
4471 ;; Now enable my keys and the fontification.
4472 (mdw-misc-mode-config)
4473
4474 ;; Set the indentation level correctly.
4475 (setq sh-indentation 2)
4476 (setq sh-basic-offset 2))
4477
4478 (setq sh-shell-file "/bin/sh")
4479
4480 ;; Awful hacking to override the shell detection for particular scripts.
4481 (defmacro define-custom-shell-mode (name shell)
4482 `(defun ,name ()
4483 (interactive)
4484 (set (make-local-variable 'sh-shell-file) ,shell)
4485 (sh-mode)))
4486 (define-custom-shell-mode bash-mode "/bin/bash")
4487 (define-custom-shell-mode rc-mode "/usr/bin/rc")
4488 (put 'sh-shell-file 'permanent-local t)
4489
4490 ;; Hack the rc syntax table. Backquotes aren't paired in rc.
4491 (eval-after-load "sh-script"
4492 '(or (assq 'rc sh-mode-syntax-table-input)
4493 (let ((frag '(nil
4494 ?# "<"
4495 ?\n ">#"
4496 ?\" "\"\""
4497 ?\' "\"\'"
4498 ?$ "'"
4499 ?\` "."
4500 ?! "_"
4501 ?% "_"
4502 ?. "_"
4503 ?^ "_"
4504 ?~ "_"
4505 ?, "_"
4506 ?= "."
4507 ?< "."
4508 ?> "."))
4509 (assoc (assq 'rc sh-mode-syntax-table-input)))
4510 (if assoc
4511 (rplacd assoc frag)
4512 (setq sh-mode-syntax-table-input
4513 (cons (cons 'rc frag)
4514 sh-mode-syntax-table-input))))))
4515
4516 (progn
4517 (add-hook 'sh-mode-hook 'mdw-misc-mode-config t)
4518 (add-hook 'sh-mode-hook 'mdw-setup-sh-script-mode t))
4519
4520 ;;;--------------------------------------------------------------------------
4521 ;;; Emacs shell mode.
4522
4523 (defun mdw-eshell-prompt ()
4524 (let ((left "[") (right "]"))
4525 (when (= (user-uid) 0)
4526 (setq left "«" right "»"))
4527 (concat left
4528 (save-match-data
4529 (replace-regexp-in-string "\\..*$" "" (system-name)))
4530 " "
4531 (let* ((pwd (eshell/pwd)) (npwd (length pwd))
4532 (home (expand-file-name "~")) (nhome (length home)))
4533 (if (and (>= npwd nhome)
4534 (or (= nhome npwd)
4535 (= (elt pwd nhome) ?/))
4536 (string= (substring pwd 0 nhome) home))
4537 (concat "~" (substring pwd (length home)))
4538 pwd))
4539 right)))
4540 (setq-default eshell-prompt-function 'mdw-eshell-prompt)
4541 (setq-default eshell-prompt-regexp "^\\[[^]>]+\\(\\]\\|>>?\\)")
4542
4543 (defun eshell/e (file) (find-file file) nil)
4544 (defun eshell/ee (file) (find-file-other-window file) nil)
4545 (defun eshell/w3m (url) (w3m-goto-url url) nil)
4546
4547 (mdw-define-face eshell-prompt (t :weight bold))
4548 (mdw-define-face eshell-ls-archive (t :weight bold :foreground "red"))
4549 (mdw-define-face eshell-ls-backup (t :foreground "lightgrey" :slant italic))
4550 (mdw-define-face eshell-ls-product (t :foreground "lightgrey" :slant italic))
4551 (mdw-define-face eshell-ls-clutter (t :foreground "lightgrey" :slant italic))
4552 (mdw-define-face eshell-ls-executable (t :weight bold))
4553 (mdw-define-face eshell-ls-directory (t :foreground "cyan" :weight bold))
4554 (mdw-define-face eshell-ls-readonly (t nil))
4555 (mdw-define-face eshell-ls-symlink (t :foreground "cyan"))
4556
4557 (defun mdw-eshell-hack () (setenv "LD_PRELOAD" nil))
4558 (add-hook 'eshell-mode-hook 'mdw-eshell-hack)
4559
4560 ;;;--------------------------------------------------------------------------
4561 ;;; Messages-file mode.
4562
4563 (defun messages-mode-guts ()
4564 (setq messages-mode-syntax-table (make-syntax-table))
4565 (set-syntax-table messages-mode-syntax-table)
4566 (modify-syntax-entry ?0 "w" messages-mode-syntax-table)
4567 (modify-syntax-entry ?1 "w" messages-mode-syntax-table)
4568 (modify-syntax-entry ?2 "w" messages-mode-syntax-table)
4569 (modify-syntax-entry ?3 "w" messages-mode-syntax-table)
4570 (modify-syntax-entry ?4 "w" messages-mode-syntax-table)
4571 (modify-syntax-entry ?5 "w" messages-mode-syntax-table)
4572 (modify-syntax-entry ?6 "w" messages-mode-syntax-table)
4573 (modify-syntax-entry ?7 "w" messages-mode-syntax-table)
4574 (modify-syntax-entry ?8 "w" messages-mode-syntax-table)
4575 (modify-syntax-entry ?9 "w" messages-mode-syntax-table)
4576 (make-local-variable 'comment-start)
4577 (make-local-variable 'comment-end)
4578 (make-local-variable 'indent-line-function)
4579 (setq indent-line-function 'indent-relative)
4580 (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
4581 (make-local-variable 'font-lock-defaults)
4582 (make-local-variable 'messages-mode-keywords)
4583 (let ((keywords
4584 (mdw-regexps "array" "bitmap" "callback" "docs[ \t]+enum"
4585 "export" "enum" "fixed-octetstring" "flags"
4586 "harmless" "map" "nested" "optional"
4587 "optional-tagged" "package" "primitive"
4588 "primitive-nullfree" "relaxed[ \t]+enum"
4589 "set" "table" "tagged-optional" "union"
4590 "variadic" "vector" "version" "version-tag")))
4591 (setq messages-mode-keywords
4592 (list
4593 (list (concat "\\<\\(" keywords "\\)\\>:")
4594 '(0 font-lock-keyword-face))
4595 '("\\([-a-zA-Z0-9]+:\\)" (0 font-lock-warning-face))
4596 '("\\(\\<[a-z][-_a-zA-Z0-9]*\\)"
4597 (0 font-lock-variable-name-face))
4598 '("\\<\\([0-9]+\\)\\>" (0 mdw-number-face))
4599 '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4600 (0 mdw-punct-face)))))
4601 (setq font-lock-defaults
4602 '(messages-mode-keywords nil nil nil nil))
4603 (run-hooks 'messages-file-hook))
4604
4605 (defun messages-mode ()
4606 (interactive)
4607 (fundamental-mode)
4608 (setq major-mode 'messages-mode)
4609 (setq mode-name "Messages")
4610 (messages-mode-guts)
4611 (modify-syntax-entry ?# "<" messages-mode-syntax-table)
4612 (modify-syntax-entry ?\n ">" messages-mode-syntax-table)
4613 (setq comment-start "# ")
4614 (setq comment-end "")
4615 (run-hooks 'messages-mode-hook))
4616
4617 (defun cpp-messages-mode ()
4618 (interactive)
4619 (fundamental-mode)
4620 (setq major-mode 'cpp-messages-mode)
4621 (setq mode-name "CPP Messages")
4622 (messages-mode-guts)
4623 (modify-syntax-entry ?* ". 23" messages-mode-syntax-table)
4624 (modify-syntax-entry ?/ ". 14" messages-mode-syntax-table)
4625 (setq comment-start "/* ")
4626 (setq comment-end " */")
4627 (let ((preprocessor-keywords
4628 (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
4629 "ident" "if" "ifdef" "ifndef" "import" "include"
4630 "line" "pragma" "unassert" "undef" "warning")))
4631 (setq messages-mode-keywords
4632 (append (list (list (concat "^[ \t]*\\#[ \t]*"
4633 "\\(include\\|import\\)"
4634 "[ \t]*\\(<[^>]+\\(>\\)?\\)")
4635 '(2 font-lock-string-face))
4636 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
4637 preprocessor-keywords
4638 "\\)\\>\\|[0-9]+\\|$\\)\\)")
4639 '(1 font-lock-keyword-face)))
4640 messages-mode-keywords)))
4641 (run-hooks 'cpp-messages-mode-hook))
4642
4643 (progn
4644 (add-hook 'messages-mode-hook 'mdw-misc-mode-config t)
4645 (add-hook 'cpp-messages-mode-hook 'mdw-misc-mode-config t)
4646 ;; (add-hook 'messages-file-hook 'mdw-fontify-messages t)
4647 )
4648
4649 ;;;--------------------------------------------------------------------------
4650 ;;; Messages-file mode.
4651
4652 (defvar mallow-driver-substitution-face 'mallow-driver-substitution-face
4653 "Face to use for subsittution directives.")
4654 (make-face 'mallow-driver-substitution-face)
4655 (defvar mallow-driver-text-face 'mallow-driver-text-face
4656 "Face to use for body text.")
4657 (make-face 'mallow-driver-text-face)
4658
4659 (defun mallow-driver-mode ()
4660 (interactive)
4661 (fundamental-mode)
4662 (setq major-mode 'mallow-driver-mode)
4663 (setq mode-name "Mallow driver")
4664 (setq mallow-driver-mode-syntax-table (make-syntax-table))
4665 (set-syntax-table mallow-driver-mode-syntax-table)
4666 (make-local-variable 'comment-start)
4667 (make-local-variable 'comment-end)
4668 (make-local-variable 'indent-line-function)
4669 (setq indent-line-function 'indent-relative)
4670 (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
4671 (make-local-variable 'font-lock-defaults)
4672 (make-local-variable 'mallow-driver-mode-keywords)
4673 (let ((keywords
4674 (mdw-regexps "each" "divert" "file" "if"
4675 "perl" "set" "string" "type" "write")))
4676 (setq mallow-driver-mode-keywords
4677 (list
4678 (list (concat "^%\\s *\\(}\\|\\(" keywords "\\)\\>\\).*$")
4679 '(0 font-lock-keyword-face))
4680 (list "^%\\s *\\(#.*\\)?$"
4681 '(0 font-lock-comment-face))
4682 (list "^%"
4683 '(0 font-lock-keyword-face))
4684 (list "^|?\\(.+\\)$" '(1 mallow-driver-text-face))
4685 (list "\\${[^}]*}"
4686 '(0 mallow-driver-substitution-face t)))))
4687 (setq font-lock-defaults
4688 '(mallow-driver-mode-keywords nil nil nil nil))
4689 (modify-syntax-entry ?\" "_" mallow-driver-mode-syntax-table)
4690 (modify-syntax-entry ?\n ">" mallow-driver-mode-syntax-table)
4691 (setq comment-start "%# ")
4692 (setq comment-end "")
4693 (run-hooks 'mallow-driver-mode-hook))
4694
4695 (progn
4696 (add-hook 'mallow-driver-hook 'mdw-misc-mode-config t))
4697
4698 ;;;--------------------------------------------------------------------------
4699 ;;; NFast debugs.
4700
4701 (defun nfast-debug-mode ()
4702 (interactive)
4703 (fundamental-mode)
4704 (setq major-mode 'nfast-debug-mode)
4705 (setq mode-name "NFast debug")
4706 (setq messages-mode-syntax-table (make-syntax-table))
4707 (set-syntax-table messages-mode-syntax-table)
4708 (make-local-variable 'font-lock-defaults)
4709 (make-local-variable 'nfast-debug-mode-keywords)
4710 (setq truncate-lines t)
4711 (setq nfast-debug-mode-keywords
4712 (list
4713 '("^\\(NFast_\\(Connect\\|Disconnect\\|Submit\\|Wait\\)\\)"
4714 (0 font-lock-keyword-face))
4715 (list (concat "^[ \t]+\\(\\("
4716 "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
4717 "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
4718 "[ \t]+\\)*"
4719 "[0-9a-fA-F]+\\)[ \t]*$")
4720 '(0 mdw-number-face))
4721 '("^[ \t]+\.status=[ \t]+\\<\\(OK\\)\\>"
4722 (1 font-lock-keyword-face))
4723 '("^[ \t]+\.status=[ \t]+\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>"
4724 (1 font-lock-warning-face))
4725 '("^[ \t]+\.status[ \t]+\\<\\(zero\\)\\>"
4726 (1 nil))
4727 (list (concat "^[ \t]+\\.cmd=[ \t]+"
4728 "\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>")
4729 '(1 font-lock-keyword-face))
4730 '("-?\\<\\([0-9]+\\|0x[0-9a-fA-F]+\\)\\>" (0 mdw-number-face))
4731 '("^\\([ \t]+[a-z0-9.]+\\)" (0 font-lock-variable-name-face))
4732 '("\\<\\([a-z][a-z0-9.]+\\)\\>=" (1 font-lock-variable-name-face))
4733 '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" (0 mdw-punct-face))))
4734 (setq font-lock-defaults
4735 '(nfast-debug-mode-keywords nil nil nil nil))
4736 (run-hooks 'nfast-debug-mode-hook))
4737
4738 ;;;--------------------------------------------------------------------------
4739 ;;; Lispy languages.
4740
4741 ;; Unpleasant bodge.
4742 (unless (boundp 'slime-repl-mode-map)
4743 (setq slime-repl-mode-map (make-sparse-keymap)))
4744
4745 (defun mdw-indent-newline-and-indent ()
4746 (interactive)
4747 (indent-for-tab-command)
4748 (newline-and-indent))
4749
4750 (eval-after-load "cl-indent"
4751 '(progn
4752 (mapc #'(lambda (pair)
4753 (put (car pair)
4754 'common-lisp-indent-function
4755 (cdr pair)))
4756 '((destructuring-bind . ((&whole 4 &rest 1) 4 &body))
4757 (multiple-value-bind . ((&whole 4 &rest 1) 4 &body))))))
4758
4759 (defun mdw-common-lisp-indent ()
4760 (make-local-variable 'lisp-indent-function)
4761 (setq lisp-indent-function 'common-lisp-indent-function))
4762
4763 (defmacro mdw-advise-hyperspec-lookup (func args)
4764 `(defadvice ,func (around mdw-browse-w3m ,args activate compile)
4765 (if (fboundp 'w3m)
4766 (let ((browse-url-browser-function #'mdw-w3m-browse-url))
4767 ad-do-it)
4768 ad-do-it)))
4769 (mdw-advise-hyperspec-lookup common-lisp-hyperspec (symbol))
4770 (mdw-advise-hyperspec-lookup common-lisp-hyperspec-format (char))
4771 (mdw-advise-hyperspec-lookup common-lisp-hyperspec-lookup-reader-macro (char))
4772
4773 (defun mdw-fontify-lispy ()
4774
4775 ;; Set fill prefix.
4776 (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)")
4777
4778 ;; Not much fontification needed.
4779 (make-local-variable 'font-lock-keywords)
4780 (setq font-lock-keywords
4781 (list (list (concat "\\("
4782 "\\_<[-+]?"
4783 "\\(" "[0-9]+/[0-9]+"
4784 "\\|" "\\(" "[0-9]+" "\\(\\.[0-9]*\\)?" "\\|"
4785 "\\.[0-9]+" "\\)"
4786 "\\([dDeEfFlLsS][-+]?[0-9]+\\)?"
4787 "\\)"
4788 "\\|"
4789 "#"
4790 "\\(" "x" "[-+]?"
4791 "[0-9A-Fa-f]+" "\\(/[0-9A-Fa-f]+\\)?"
4792 "\\|" "o" "[-+]?" "[0-7]+" "\\(/[0-7]+\\)?"
4793 "\\|" "b" "[-+]?" "[01]+" "\\(/[01]+\\)?"
4794 "\\|" "[0-9]+" "r" "[-+]?"
4795 "[0-9a-zA-Z]+" "\\(/[0-9a-zA-Z]+\\)?"
4796 "\\)"
4797 "\\)\\_>")
4798 '(0 mdw-number-face))
4799 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4800 '(0 mdw-punct-face)))))
4801
4802 ;; Special indentation.
4803
4804 (defcustom mdw-lisp-loop-default-indent 2
4805 "Default indent for simple `loop' body."
4806 :type 'integer
4807 :safe 'integerp)
4808 (defcustom mdw-lisp-setf-value-indent 2
4809 "Default extra indent for `setf' values."
4810 :type 'integer :safe 'integerp)
4811
4812 (setq lisp-simple-loop-indentation 0
4813 lisp-loop-keyword-indentation 0
4814 lisp-loop-forms-indentation 2
4815 lisp-lambda-list-keyword-parameter-alignment t)
4816
4817 (defun mdw-indent-funcall
4818 (path state &optional indent-point sexp-column normal-indent)
4819 "Indent `funcall' more usefully.
4820 Essentially, treat `funcall foo' as a function name, and align the arguments
4821 to `foo'."
4822 (and (or (not (consp path)) (null (cadr path)))
4823 (save-excursion
4824 (goto-char (cadr state))
4825 (forward-char 1)
4826 (let ((start-line (line-number-at-pos)))
4827 (and (condition-case nil (progn (forward-sexp 3) t)
4828 (scan-error nil))
4829 (progn
4830 (forward-sexp -1)
4831 (and (= start-line (line-number-at-pos))
4832 (current-column))))))))
4833 (progn
4834 (put 'funcall 'common-lisp-indent-function 'mdw-indent-funcall)
4835 (put 'funcall 'lisp-indent-function 'mdw-indent-funcall))
4836
4837 (defun mdw-indent-setf
4838 (path state &optional indent-point sexp-column normal-indent)
4839 "Indent `setf' more usefully.
4840 If the values aren't on the same lines as their variables then indent them
4841 by `mdw-lisp-setf-value-indent' spaces."
4842 (and (or (not (consp path)) (null (cadr path)))
4843 (let ((basic-indent (save-excursion
4844 (goto-char (cadr state))
4845 (forward-char 1)
4846 (and (condition-case nil
4847 (progn (forward-sexp 2) t)
4848 (scan-error nil))
4849 (progn
4850 (forward-sexp -1)
4851 (current-column)))))
4852 (offset (if (consp path) (car path)
4853 (catch 'done
4854 (save-excursion
4855 (let ((start path)
4856 (count 0))
4857 (goto-char (cadr state))
4858 (forward-char 1)
4859 (while (< (point) start)
4860 (condition-case nil (forward-sexp 1)
4861 (scan-error (throw 'done nil)))
4862 (incf count))
4863 (1- count)))))))
4864 (and basic-indent offset
4865 (list (+ basic-indent
4866 (if (oddp offset) 0
4867 mdw-lisp-setf-value-indent))
4868 basic-indent)))))
4869 (progn
4870 (put 'setf 'common-lisp-indent-functopion 'mdw-indent-setf)
4871 (put 'psetf 'common-lisp-indent-function 'mdw-indent-setf)
4872 (put 'setq 'common-lisp-indent-function 'mdw-indent-setf)
4873 (put 'setf 'lisp-indent-function 'mdw-indent-setf)
4874 (put 'setq 'lisp-indent-function 'mdw-indent-setf)
4875 (put 'setq-local 'lisp-indent-function 'mdw-indent-setf)
4876 (put 'setq-default 'lisp-indent-function 'mdw-indent-setf))
4877
4878 (defadvice common-lisp-loop-part-indentation
4879 (around mdw-fix-loop-indentation (indent-point state) activate compile)
4880 "Improve `loop' indentation.
4881 If the first subform is on the same line as the `loop' keyword, then
4882 align the other subforms beneath it. Otherwise, indent them
4883 `mdw-lisp-loop-default-indent' columns in from the opening parenthesis."
4884
4885 (let* ((loop-indentation (save-excursion
4886 (goto-char (elt state 1))
4887 (current-column))))
4888
4889 ;; Don't really care about this.
4890 (when (and (boundp 'lisp-indent-backquote-substitution-mode)
4891 (eq lisp-indent-backquote-substitution-mode 'corrected))
4892 (save-excursion
4893 (goto-char (elt state 1))
4894 (incf loop-indentation
4895 (cond ((eq (char-before) ?,) -1)
4896 ((and (eq (char-before) ?@)
4897 (progn (backward-char)
4898 (eq (char-before) ?,)))
4899 -2)
4900 (t 0)))))
4901
4902 ;; If the first loop item is on the same line as the `loop' itself then
4903 ;; use that as the baseline. Otherwise advance by the default indent.
4904 (goto-char (cadr state))
4905 (forward-char 1)
4906 (let ((baseline-indent
4907 (if (= (line-number-at-pos)
4908 (if (condition-case nil (progn (forward-sexp 2) t)
4909 (scan-error nil))
4910 (progn (forward-sexp -1) (line-number-at-pos))
4911 -1))
4912 (current-column)
4913 (+ loop-indentation mdw-lisp-loop-default-indent))))
4914
4915 (goto-char indent-point)
4916 (beginning-of-line)
4917
4918 (setq ad-return-value
4919 (list
4920 (cond ((condition-case ()
4921 (save-excursion
4922 (goto-char (elt state 1))
4923 (forward-char 1)
4924 (forward-sexp 2)
4925 (backward-sexp 1)
4926 (not (looking-at "\\(:\\|\\sw\\)")))
4927 (error nil))
4928 (+ baseline-indent lisp-simple-loop-indentation))
4929 ((looking-at "^\\s-*\\(:?\\sw+\\|;\\)")
4930 (+ baseline-indent lisp-loop-keyword-indentation))
4931 (t
4932 (+ baseline-indent lisp-loop-forms-indentation)))
4933
4934 ;; Tell the caller that the next line needs recomputation,
4935 ;; even though it doesn't start a sexp.
4936 loop-indentation)))))
4937
4938 ;; SLIME setup.
4939
4940 (defcustom mdw-friendly-name "[mdw]"
4941 "How I want to be addressed."
4942 :type 'string
4943 :safe 'stringp)
4944 (defadvice slime-user-first-name
4945 (around mdw-use-friendly-name compile activate)
4946 (if mdw-friendly-name (setq ad-return-value mdw-friendly-name)
4947 ad-do-it))
4948
4949 (eval-and-compile
4950 (trap
4951 (if (not mdw-fast-startup)
4952 (progn
4953 (require 'slime-autoloads)
4954 (slime-setup '(slime-autodoc slime-c-p-c))))))
4955
4956 (let ((stuff '((cmucl ("cmucl"))
4957 (sbcl ("sbcl") :coding-system utf-8-unix)
4958 (clisp ("clisp") :coding-system utf-8-unix))))
4959 (or (boundp 'slime-lisp-implementations)
4960 (setq slime-lisp-implementations nil))
4961 (while stuff
4962 (let* ((head (car stuff))
4963 (found (assq (car head) slime-lisp-implementations)))
4964 (setq stuff (cdr stuff))
4965 (if found
4966 (rplacd found (cdr head))
4967 (setq slime-lisp-implementations
4968 (cons head slime-lisp-implementations))))))
4969 (setq slime-default-lisp 'sbcl)
4970
4971 ;; Hooks.
4972
4973 (progn
4974 (dolist (hook '(emacs-lisp-mode-hook
4975 scheme-mode-hook
4976 lisp-mode-hook
4977 inferior-lisp-mode-hook
4978 lisp-interaction-mode-hook
4979 ielm-mode-hook
4980 slime-repl-mode-hook))
4981 (add-hook hook 'mdw-misc-mode-config t)
4982 (add-hook hook 'mdw-fontify-lispy t))
4983 (add-hook 'lisp-mode-hook 'mdw-common-lisp-indent t)
4984 (add-hook 'inferior-lisp-mode-hook
4985 #'(lambda () (local-set-key "\C-m" 'comint-send-and-indent)) t))
4986
4987 ;;;--------------------------------------------------------------------------
4988 ;;; Other languages.
4989
4990 ;; Smalltalk.
4991
4992 (defun mdw-setup-smalltalk ()
4993 (and mdw-auto-indent
4994 (local-set-key "\C-m" 'smalltalk-newline-and-indent))
4995 (make-local-variable 'mdw-auto-indent)
4996 (setq mdw-auto-indent nil)
4997 (local-set-key "\C-i" 'smalltalk-reindent))
4998
4999 (defun mdw-fontify-smalltalk ()
5000 (make-local-variable 'font-lock-keywords)
5001 (setq font-lock-keywords
5002 (list
5003 (list "\\<[A-Z][a-zA-Z0-9]*\\>"
5004 '(0 font-lock-keyword-face))
5005 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
5006 "[0-9][0-9_]*\\(\\.[0-9_]*\\)?"
5007 "\\([eE][-+]?[0-9_]+\\)?")
5008 '(0 mdw-number-face))
5009 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
5010 '(0 mdw-punct-face)))))
5011
5012 (progn
5013 (add-hook 'smalltalk-mode 'mdw-misc-mode-config t)
5014 (add-hook 'smalltalk-mode 'mdw-fontify-smalltalk t))
5015
5016 ;; m4.
5017
5018 (defun mdw-setup-m4 ()
5019
5020 ;; Inexplicably, Emacs doesn't match braces in m4 mode. This is very
5021 ;; annoying: fix it.
5022 (modify-syntax-entry ?{ "(")
5023 (modify-syntax-entry ?} ")")
5024
5025 ;; Fill prefix.
5026 (mdw-standard-fill-prefix "\\([ \t]*\\(?:#+\\|\\<dnl\\>\\)[ \t]*\\)"))
5027
5028 (dolist (hook '(m4-mode-hook autoconf-mode-hook autotest-mode-hook))
5029 (add-hook hook #'mdw-misc-mode-config t)
5030 (add-hook hook #'mdw-setup-m4 t))
5031
5032 ;; Make.
5033
5034 (progn
5035 (add-hook 'makefile-mode-hook 'mdw-misc-mode-config t))
5036
5037 ;;;--------------------------------------------------------------------------
5038 ;;; Text mode.
5039
5040 (defun mdw-text-mode ()
5041 (setq fill-column 72)
5042 (flyspell-mode t)
5043 (mdw-standard-fill-prefix
5044 "\\([ \t]*\\([>#|:] ?\\)*[ \t]*\\)" 3)
5045 (auto-fill-mode 1))
5046
5047 (eval-after-load "flyspell"
5048 '(define-key flyspell-mode-map "\C-\M-i" nil))
5049
5050 (progn
5051 (add-hook 'text-mode-hook 'mdw-text-mode t))
5052
5053 ;;;--------------------------------------------------------------------------
5054 ;;; Outline and hide/show modes.
5055
5056 (defun mdw-outline-collapse-all ()
5057 "Completely collapse everything in the entire buffer."
5058 (interactive)
5059 (save-excursion
5060 (goto-char (point-min))
5061 (while (< (point) (point-max))
5062 (hide-subtree)
5063 (forward-line))))
5064
5065 (setq hs-hide-comments-when-hiding-all nil)
5066
5067 (defadvice hs-hide-all (after hide-first-comment activate)
5068 (save-excursion (hs-hide-initial-comment-block)))
5069
5070 ;;;--------------------------------------------------------------------------
5071 ;;; Shell mode.
5072
5073 (defun mdw-sh-mode-setup ()
5074 (local-set-key [?\C-a] 'comint-bol)
5075 (add-hook 'comint-output-filter-functions
5076 'comint-watch-for-password-prompt))
5077
5078 (defun mdw-term-mode-setup ()
5079 (setq term-prompt-regexp shell-prompt-pattern)
5080 (make-local-variable 'mouse-yank-at-point)
5081 (make-local-variable 'transient-mark-mode)
5082 (setq mouse-yank-at-point t)
5083 (auto-fill-mode -1)
5084 (setq tab-width 8))
5085
5086 (defun comint-send-and-indent ()
5087 (interactive)
5088 (comint-send-input)
5089 (and mdw-auto-indent
5090 (indent-for-tab-command)))
5091
5092 (defadvice comint-line-beginning-position
5093 (around mdw-calculate-it-properly () activate compile)
5094 "Calculate the actual line start for multi-line input."
5095 (if (or comint-use-prompt-regexp
5096 (eq (field-at-pos (point)) 'output))
5097 ad-do-it
5098 (setq ad-return-value
5099 (constrain-to-field (line-beginning-position) (point)))))
5100
5101 (defun term-send-meta-right () (interactive) (term-send-raw-string "\e\e[C"))
5102 (defun term-send-meta-left () (interactive) (term-send-raw-string "\e\e[D"))
5103 (defun term-send-ctrl-uscore () (interactive) (term-send-raw-string "\C-_"))
5104 (defun term-send-meta-meta-something ()
5105 (interactive)
5106 (term-send-raw-string "\e\e")
5107 (term-send-raw))
5108 (eval-after-load 'term
5109 '(progn
5110 (define-key term-raw-map [?\e ?\e] nil)
5111 (define-key term-raw-map [?\e ?\e t] 'term-send-meta-meta-something)
5112 (define-key term-raw-map [?\C-/] 'term-send-ctrl-uscore)
5113 (define-key term-raw-map [M-right] 'term-send-meta-right)
5114 (define-key term-raw-map [?\e ?\M-O ?C] 'term-send-meta-right)
5115 (define-key term-raw-map [M-left] 'term-send-meta-left)
5116 (define-key term-raw-map [?\e ?\M-O ?D] 'term-send-meta-left)))
5117
5118 (defadvice term-exec (before program-args-list compile activate)
5119 "If the PROGRAM argument is a list, interpret it as (PROGRAM . SWITCHES).
5120 This allows you to pass a list of arguments through `ansi-term'."
5121 (let ((program (ad-get-arg 2)))
5122 (if (listp program)
5123 (progn
5124 (ad-set-arg 2 (car program))
5125 (ad-set-arg 4 (cdr program))))))
5126
5127 (defadvice term-exec-1 (around hack-environment compile activate)
5128 "Hack the environment inherited by inferiors in the terminal."
5129 (let ((process-environment (copy-tree process-environment)))
5130 (setenv "LD_PRELOAD" nil)
5131 ad-do-it))
5132
5133 (defadvice shell (around hack-environment compile activate)
5134 "Hack the environment inherited by inferiors in the shell."
5135 (let ((process-environment (copy-tree process-environment)))
5136 (setenv "LD_PRELOAD" nil)
5137 ad-do-it))
5138
5139 (defun ssh (host)
5140 "Open a terminal containing an ssh session to the HOST."
5141 (interactive "sHost: ")
5142 (ansi-term (list "ssh" host) (format "ssh@%s" host)))
5143
5144 (defcustom git-grep-command
5145 "env GIT_PAGER=cat git grep --no-color -nH -e "
5146 "The default command for \\[git-grep]."
5147 :type 'string)
5148
5149 (defvar git-grep-history nil)
5150
5151 (defun git-grep (command-args)
5152 "Run `git grep' with user-specified args and collect output in a buffer."
5153 (interactive
5154 (list (read-shell-command "Run git grep (like this): "
5155 git-grep-command 'git-grep-history)))
5156 (let ((grep-use-null-device nil))
5157 (grep command-args)))
5158
5159 ;;;--------------------------------------------------------------------------
5160 ;;; Magit configuration.
5161
5162 (setq magit-diff-refine-hunk 't
5163 magit-view-git-manual-method 'man
5164 magit-log-margin '(nil age magit-log-margin-width t 18)
5165 magit-wip-after-save-local-mode-lighter ""
5166 magit-wip-after-apply-mode-lighter ""
5167 magit-wip-before-change-mode-lighter "")
5168 (eval-after-load "magit"
5169 '(progn (global-magit-file-mode 1)
5170 (magit-wip-after-save-mode 1)
5171 (magit-wip-after-apply-mode 1)
5172 (magit-wip-before-change-mode 1)
5173 (add-to-list 'magit-no-confirm 'safe-with-wip)
5174 (add-to-list 'magit-no-confirm 'trash)
5175 (push '(:eval (if (or magit-wip-after-save-local-mode
5176 magit-wip-after-apply-mode
5177 magit-wip-before-change-mode)
5178 (format " wip:%s%s%s"
5179 (if magit-wip-after-apply-mode "A" "")
5180 (if magit-wip-before-change-mode "C" "")
5181 (if magit-wip-after-save-local-mode "S" ""))))
5182 minor-mode-alist)
5183 (dolist (popup '(magit-diff-popup
5184 magit-diff-refresh-popup
5185 magit-diff-mode-refresh-popup
5186 magit-revision-mode-refresh-popup))
5187 (magit-define-popup-switch popup ?R "Reverse diff" "-R"))
5188 (magit-define-popup-switch 'magit-rebase-popup ?r
5189 "Rebase merges" "--rebase-merges")))
5190
5191 (defadvice magit-wip-commit-buffer-file
5192 (around mdw-just-this-buffer activate compile)
5193 (let ((magit-save-repository-buffers nil)) ad-do-it))
5194
5195 (defadvice magit-discard
5196 (around mdw-delete-if-prefix-argument activate compile)
5197 (let ((magit-delete-by-moving-to-trash
5198 (and (null current-prefix-arg)
5199 magit-delete-by-moving-to-trash)))
5200 ad-do-it))
5201
5202 (setq magit-repolist-columns
5203 '(("Name" 16 magit-repolist-column-ident nil)
5204 ("Version" 18 magit-repolist-column-version nil)
5205 ("St" 2 magit-repolist-column-dirty nil)
5206 ("L<U" 3 mdw-repolist-column-unpulled-from-upstream nil)
5207 ("L>U" 3 mdw-repolist-column-unpushed-to-upstream nil)
5208 ("Path" 32 magit-repolist-column-path nil)))
5209
5210 (setq magit-repository-directories '(("~/etc/profile" . 0)
5211 ("~/src/" . 1)))
5212
5213 (defadvice magit-list-repos (around mdw-dirname () activate compile)
5214 "Make sure the returned names are directory names.
5215 Otherwise child processes get started in the wrong directory and
5216 there is sadness."
5217 (setq ad-return-value (mapcar #'file-name-as-directory ad-do-it)))
5218
5219 (defun mdw-repolist-column-unpulled-from-upstream (_id)
5220 "Insert number of upstream commits not in the current branch."
5221 (let ((upstream (magit-get-upstream-branch (magit-get-current-branch) t)))
5222 (and upstream
5223 (let ((n (cadr (magit-rev-diff-count "HEAD" upstream))))
5224 (propertize (number-to-string n) 'face
5225 (if (> n 0) 'bold 'shadow))))))
5226
5227 (defun mdw-repolist-column-unpushed-to-upstream (_id)
5228 "Insert number of commits in the current branch but not its upstream."
5229 (let ((upstream (magit-get-upstream-branch (magit-get-current-branch) t)))
5230 (and upstream
5231 (let ((n (car (magit-rev-diff-count "HEAD" upstream))))
5232 (propertize (number-to-string n) 'face
5233 (if (> n 0) 'bold 'shadow))))))
5234
5235 (defun mdw-try-smerge ()
5236 (save-excursion
5237 (goto-char (point-min))
5238 (when (re-search-forward "^<<<<<<< " nil t)
5239 (smerge-mode 1))))
5240 (add-hook 'find-file-hook 'mdw-try-smerge t)
5241
5242 (defcustom mdw-magit-new-window-modes
5243 '(magit-diff-mode
5244 magit-log-mode
5245 magit-process-mode
5246 magit-revision-mode
5247 magit-stash-mode
5248 magit-status-mode)
5249 "Magit modes which should cause a new window to be used."
5250 :type '(repeat symbol))
5251
5252 (defun mdw-display-magit-buffer (buffer)
5253 "Like `magit-display-buffer-traditional'.
5254 But uses `mdw-magit-new-window-modes' for its list of modes
5255 rather than baking the list into the function."
5256 (display-buffer buffer
5257 (let ((mode (with-current-buffer buffer major-mode)))
5258 (if (and (not mdw-designated-window)
5259 (derived-mode-p 'magit-mode)
5260 (mdw-submode-p mode 'magit-mode)
5261 (not (memq mode mdw-magit-new-window-modes)))
5262 '(display-buffer-same-window . nil)
5263 nil))))
5264 (setq magit-display-buffer-function 'mdw-display-magit-buffer)
5265
5266 (defun mdw-display-magit-file-buffer (buffer)
5267 "Show a file buffer from a diff."
5268 (select-window (display-buffer buffer)))
5269 (setq magit-display-file-buffer-function 'mdw-display-magit-file-buffer)
5270
5271 ;;;--------------------------------------------------------------------------
5272 ;;; GUD, and especially GDB.
5273
5274 ;; Inhibit window dedication. I mean, seriously, wtf?
5275 (defadvice gdb-display-buffer (after mdw-undedicated (buf) compile activate)
5276 "Don't make windows dedicated. Seriously."
5277 (set-window-dedicated-p ad-return-value nil))
5278 (defadvice gdb-set-window-buffer
5279 (after mdw-undedicated (name &optional ignore-dedicated window)
5280 compile activate)
5281 "Don't make windows dedicated. Seriously."
5282 (set-window-dedicated-p (or window (selected-window)) nil))
5283
5284 ;;;--------------------------------------------------------------------------
5285 ;;; SQL stuff.
5286
5287 (setq sql-postgres-options '("-n" "-P" "pager=off")
5288 sql-postgres-login-params
5289 '((user :default "mdw")
5290 (database :default "mdw")
5291 (server :default "db.distorted.org.uk")))
5292
5293 ;;;--------------------------------------------------------------------------
5294 ;;; Man pages.
5295
5296 ;; Turn off `noip' when running `man': it interferes with `man-db''s own
5297 ;; seccomp(2)-based sandboxing, which is (in this case, at least) strictly
5298 ;; better.
5299 (defadvice Man-getpage-in-background
5300 (around mdw-inhibit-noip (topic) compile activate)
5301 "Inhibit the `noip' preload hack when invoking `man'."
5302 (let* ((old-preload (getenv "LD_PRELOAD"))
5303 (preloads (and old-preload
5304 (save-match-data (split-string old-preload ":"))))
5305 (any nil)
5306 (filtered nil))
5307 (save-match-data
5308 (while preloads
5309 (let ((item (pop preloads)))
5310 (if (string-match "\\(/\\|^\\)noip\.so\\(:\\|$\\)" item)
5311 (setq any t)
5312 (push item filtered)))))
5313 (if any
5314 (unwind-protect
5315 (progn
5316 (setenv "LD_PRELOAD"
5317 (and filtered
5318 (with-output-to-string
5319 (setq filtered (nreverse filtered))
5320 (let ((first t))
5321 (while filtered
5322 (if first (setq first nil)
5323 (write-char ?:))
5324 (write-string (pop filtered)))))))
5325 ad-do-it)
5326 (setenv "LD_PRELOAD" old-preload))
5327 ad-do-it)))
5328
5329 ;;;--------------------------------------------------------------------------
5330 ;;; MPC configuration.
5331
5332 (eval-when-compile (trap (require 'mpc)))
5333
5334 (setq mpc-browser-tags '(Artist|Composer|Performer Album|Playlist))
5335
5336 (defun mdw-mpc-now-playing ()
5337 (interactive)
5338 (require 'mpc)
5339 (save-excursion
5340 (set-buffer (mpc-proc-cmd (mpc-proc-cmd-list '("status" "currentsong"))))
5341 (mpc--status-callback))
5342 (let ((state (cdr (assq 'state mpc-status))))
5343 (cond ((member state '("stop"))
5344 (message "mpd stopped."))
5345 ((member state '("play" "pause"))
5346 (let* ((artist (cdr (assq 'Artist mpc-status)))
5347 (album (cdr (assq 'Album mpc-status)))
5348 (title (cdr (assq 'Title mpc-status)))
5349 (file (cdr (assq 'file mpc-status)))
5350 (duration-string (cdr (assq 'Time mpc-status)))
5351 (time-string (cdr (assq 'time mpc-status)))
5352 (time (and time-string
5353 (string-to-number
5354 (if (string-match ":" time-string)
5355 (substring time-string
5356 0 (match-beginning 0))
5357 (time-string)))))
5358 (duration (and duration-string
5359 (string-to-number duration-string)))
5360 (pos (and time duration
5361 (format " [%d:%02d/%d:%02d]"
5362 (/ time 60) (mod time 60)
5363 (/ duration 60) (mod duration 60))))
5364 (fmt (cond ((and artist title)
5365 (format "`%s' by %s%s" title artist
5366 (if album (format ", from `%s'" album)
5367 "")))
5368 (file
5369 (format "`%s' (no tags)" file))
5370 (t
5371 "(no idea what's playing!)"))))
5372 (if (string= state "play")
5373 (message "mpd playing %s%s" fmt (or pos ""))
5374 (message "mpd paused in %s%s" fmt (or pos "")))))
5375 (t
5376 (message "mpd in unknown state `%s'" state)))))
5377
5378 (defmacro mdw-define-mpc-wrapper (func bvl interactive &rest body)
5379 `(defun ,func ,bvl
5380 (interactive ,@interactive)
5381 (require 'mpc)
5382 ,@body
5383 (mdw-mpc-now-playing)))
5384
5385 (mdw-define-mpc-wrapper mdw-mpc-play-or-pause () nil
5386 (if (member (cdr (assq 'state (mpc-cmd-status))) '("play"))
5387 (mpc-pause)
5388 (mpc-play)))
5389
5390 (mdw-define-mpc-wrapper mdw-mpc-next () nil (mpc-next))
5391 (mdw-define-mpc-wrapper mdw-mpc-prev () nil (mpc-prev))
5392 (mdw-define-mpc-wrapper mdw-mpc-stop () nil (mpc-stop))
5393
5394 (defun mdw-mpc-louder (step)
5395 (interactive (list (if current-prefix-arg
5396 (prefix-numeric-value current-prefix-arg)
5397 +10)))
5398 (mpc-proc-cmd (format "volume %+d" step)))
5399
5400 (defun mdw-mpc-quieter (step)
5401 (interactive (list (if current-prefix-arg
5402 (prefix-numeric-value current-prefix-arg)
5403 +10)))
5404 (mpc-proc-cmd (format "volume %+d" (- step))))
5405
5406 (defun mdw-mpc-hack-lines (arg interactivep func)
5407 (if (and interactivep (use-region-p))
5408 (let ((from (region-beginning)) (to (region-end)))
5409 (goto-char from)
5410 (beginning-of-line)
5411 (funcall func)
5412 (forward-line)
5413 (while (< (point) to)
5414 (funcall func)
5415 (forward-line)))
5416 (let ((n (prefix-numeric-value arg)))
5417 (cond ((minusp n)
5418 (unless (bolp)
5419 (beginning-of-line)
5420 (funcall func)
5421 (incf n))
5422 (while (minusp n)
5423 (forward-line -1)
5424 (funcall func)
5425 (incf n)))
5426 (t
5427 (beginning-of-line)
5428 (while (plusp n)
5429 (funcall func)
5430 (forward-line)
5431 (decf n)))))))
5432
5433 (defun mdw-mpc-select-one ()
5434 (when (and (get-char-property (point) 'mpc-file)
5435 (not (get-char-property (point) 'mpc-select)))
5436 (mpc-select-toggle)))
5437
5438 (defun mdw-mpc-unselect-one ()
5439 (when (get-char-property (point) 'mpc-select)
5440 (mpc-select-toggle)))
5441
5442 (defun mdw-mpc-select (&optional arg interactivep)
5443 (interactive (list current-prefix-arg t))
5444 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
5445
5446 (defun mdw-mpc-unselect (&optional arg interactivep)
5447 (interactive (list current-prefix-arg t))
5448 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-unselect-one))
5449
5450 (defun mdw-mpc-unselect-backwards (arg)
5451 (interactive "p")
5452 (mdw-mpc-hack-lines (- arg) t 'mdw-mpc-unselect-one))
5453
5454 (defun mdw-mpc-unselect-all ()
5455 (interactive)
5456 (setq mpc-select nil)
5457 (mpc-selection-refresh))
5458
5459 (defun mdw-mpc-next-line (arg)
5460 (interactive "p")
5461 (beginning-of-line)
5462 (forward-line arg))
5463
5464 (defun mdw-mpc-previous-line (arg)
5465 (interactive "p")
5466 (beginning-of-line)
5467 (forward-line (- arg)))
5468
5469 (defun mdw-mpc-playlist-add (&optional arg interactivep)
5470 (interactive (list current-prefix-arg t))
5471 (let ((mpc-select mpc-select))
5472 (when (or arg (and interactivep (use-region-p)))
5473 (setq mpc-select nil)
5474 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
5475 (setq mpc-select (reverse mpc-select))
5476 (mpc-playlist-add)))
5477
5478 (defun mdw-mpc-playlist-delete (&optional arg interactivep)
5479 (interactive (list current-prefix-arg t))
5480 (setq mpc-select (nreverse mpc-select))
5481 (mpc-select-save
5482 (when (or arg (and interactivep (use-region-p)))
5483 (setq mpc-select nil)
5484 (mpc-selection-refresh)
5485 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
5486 (mpc-playlist-delete)))
5487
5488 (defun mdw-mpc-hack-tagbrowsers ()
5489 (setq-local mode-line-format
5490 '("%e"
5491 mode-line-frame-identification
5492 mode-line-buffer-identification)))
5493 (add-hook 'mpc-tagbrowser-mode-hook 'mdw-mpc-hack-tagbrowsers)
5494
5495 (defun mdw-mpc-hack-songs ()
5496 (setq-local header-line-format
5497 ;; '("MPC " mpc-volume " " mpc-current-song)
5498 (list (propertize " " 'display '(space :align-to 0))
5499 ;; 'mpc-songs-format-description
5500 '(:eval
5501 (let ((deactivate-mark) (hscroll (window-hscroll)))
5502 (with-temp-buffer
5503 (mpc-format mpc-songs-format 'self hscroll)
5504 ;; That would be simpler than the hscroll handling in
5505 ;; mpc-format, but currently move-to-column does not
5506 ;; recognize :space display properties.
5507 ;; (move-to-column hscroll)
5508 ;; (delete-region (point-min) (point))
5509 (buffer-string)))))))
5510 (add-hook 'mpc-songs-mode-hook 'mdw-mpc-hack-songs)
5511
5512 (eval-after-load "mpc"
5513 '(progn
5514 (define-key mpc-mode-map "m" 'mdw-mpc-select)
5515 (define-key mpc-mode-map "u" 'mdw-mpc-unselect)
5516 (define-key mpc-mode-map "\177" 'mdw-mpc-unselect-backwards)
5517 (define-key mpc-mode-map "\e\177" 'mdw-mpc-unselect-all)
5518 (define-key mpc-mode-map "n" 'mdw-mpc-next-line)
5519 (define-key mpc-mode-map "p" 'mdw-mpc-previous-line)
5520 (define-key mpc-mode-map "/" 'mpc-songs-search)
5521 (setq mpc-songs-mode-map (make-sparse-keymap))
5522 (set-keymap-parent mpc-songs-mode-map mpc-mode-map)
5523 (define-key mpc-songs-mode-map "l" 'mpc-playlist)
5524 (define-key mpc-songs-mode-map "+" 'mdw-mpc-playlist-add)
5525 (define-key mpc-songs-mode-map "-" 'mdw-mpc-playlist-delete)
5526 (define-key mpc-songs-mode-map "\r" 'mpc-songs-jump-to)))
5527
5528 ;;;--------------------------------------------------------------------------
5529 ;;; Inferior Emacs Lisp.
5530
5531 (setq comint-prompt-read-only t)
5532
5533 (eval-after-load "comint"
5534 '(progn
5535 (define-key comint-mode-map "\C-w" 'comint-kill-region)
5536 (define-key comint-mode-map [C-S-backspace] 'comint-kill-whole-line)))
5537
5538 (eval-after-load "ielm"
5539 '(progn
5540 (define-key ielm-map "\C-w" 'comint-kill-region)
5541 (define-key ielm-map [C-S-backspace] 'comint-kill-whole-line)))
5542
5543 ;;;----- That's all, folks --------------------------------------------------
5544
5545 (provide 'dot-emacs)