chiark / gitweb /
Merge remote-tracking branch 'staging'
[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 (defvar mdw-fast-startup nil
28   "Whether .emacs should optimize for rapid startup.
29 This may be at the expense of cool features.")
30 (let ((probe nil) (next command-line-args))
31   (while next
32     (cond ((string= (car next) "--mdw-fast-startup")
33            (setq mdw-fast-startup t)
34            (if probe
35                (rplacd probe (cdr next))
36              (setq command-line-args (cdr next))))
37           (t
38            (setq probe next)))
39     (setq next (cdr next))))
40
41 ;;;--------------------------------------------------------------------------
42 ;;; Some general utilities.
43
44 (eval-when-compile
45   (unless (fboundp 'make-regexp)
46     (load "make-regexp"))
47   (require 'cl))
48
49 (defmacro mdw-regexps (&rest list)
50   "Turn a LIST of strings into a single regular expression at compile-time."
51   (declare (indent nil)
52            (debug 0))
53   `',(make-regexp list))
54
55 ;; Some error trapping.
56 ;;
57 ;; If individual bits of this file go tits-up, we don't particularly want
58 ;; the whole lot to stop right there and then, because it's bloody annoying.
59
60 (defmacro trap (&rest forms)
61   "Execute FORMS without allowing errors to propagate outside."
62   (declare (indent 0)
63            (debug t))
64   `(condition-case err
65        ,(if (cdr forms) (cons 'progn forms) (car forms))
66      (error (message "Error (trapped): %s in %s"
67                      (error-message-string err)
68                      ',forms))))
69
70 ;; Configuration reading.
71
72 (defvar mdw-config nil)
73 (defun mdw-config (sym)
74   "Read the configuration variable named SYM."
75   (unless mdw-config
76     (setq mdw-config
77           (flet ((replace (what with)
78                    (goto-char (point-min))
79                    (while (re-search-forward what nil t)
80                      (replace-match with t))))
81             (with-temp-buffer
82               (insert-file-contents "~/.mdw.conf")
83               (replace  "^[ \t]*\\(#.*\\|\\)\n" "")
84               (replace (concat "^[ \t]*"
85                                "\\([-a-zA-Z0-9_.]*\\)"
86                                "[ \t]*=[ \t]*"
87                                "\\(.*[^ \t\n]\\|\\)"
88                                "[ \t]**\\(\n\\|$\\)")
89                        "(\\1 . \"\\2\")\n")
90               (car (read-from-string
91                     (concat "(" (buffer-string) ")")))))))
92   (cdr (assq sym mdw-config)))
93
94 ;; Set up the load path convincingly.
95
96 (dolist (dir (append (and (boundp 'debian-emacs-flavor)
97                           (list (concat "/usr/share/"
98                                         (symbol-name debian-emacs-flavor)
99                                         "/site-lisp")))))
100   (dolist (sub (directory-files dir t))
101     (when (and (file-accessible-directory-p sub)
102                (not (member sub load-path)))
103       (setq load-path (nconc load-path (list sub))))))
104
105 ;; Is an Emacs library available?
106
107 (defun library-exists-p (name)
108   "Return non-nil if NAME is an available library.
109 Return non-nil if NAME.el (or NAME.elc) somewhere on the Emacs
110 load path.  The non-nil value is the filename we found for the
111 library."
112   (let ((path load-path) elt (foundp nil))
113     (while (and path (not foundp))
114       (setq elt (car path))
115       (setq path (cdr path))
116       (setq foundp (or (let ((file (concat elt "/" name ".elc")))
117                          (and (file-exists-p file) file))
118                        (let ((file (concat elt "/" name ".el")))
119                          (and (file-exists-p file) file)))))
120     foundp))
121
122 (defun maybe-autoload (symbol file &optional docstring interactivep type)
123   "Set an autoload if the file actually exists."
124   (and (library-exists-p file)
125        (autoload symbol file docstring interactivep type)))
126
127 (defun mdw-kick-menu-bar (&optional frame)
128   "Regenerate FRAME's menu bar so it doesn't have empty menus."
129   (interactive)
130   (unless frame (setq frame (selected-frame)))
131   (let ((old (frame-parameter frame 'menu-bar-lines)))
132     (set-frame-parameter frame 'menu-bar-lines 0)
133     (set-frame-parameter frame 'menu-bar-lines old)))
134
135 ;; Splitting windows.
136
137 (unless (fboundp 'scroll-bar-columns)
138   (defun scroll-bar-columns (side)
139     (cond ((eq side 'left) 0)
140           (window-system 3)
141           (t 1))))
142 (unless (fboundp 'fringe-columns)
143   (defun fringe-columns (side)
144     (cond ((not window-system) 0)
145           ((eq side 'left) 1)
146           (t 2))))
147
148 (defun mdw-horizontal-window-overhead ()
149   "Computes the horizontal window overhead.
150 This is the number of columns used by fringes, scroll bars and other such
151 cruft."
152   (if (not window-system)
153       1
154     (let ((tot 0))
155       (dolist (what '(scroll-bar fringe))
156         (dolist (side '(left right))
157           (incf tot (funcall (intern (concat (symbol-name what) "-columns"))
158                              side))))
159       tot)))
160
161 (defun mdw-split-window-horizontally (&optional width)
162   "Split a window horizontally.
163 Without a numeric argument, split the window approximately in
164 half.  With a numeric argument WIDTH, allocate WIDTH columns to
165 the left-hand window (if positive) or -WIDTH columns to the
166 right-hand window (if negative).  Space for scroll bars and
167 fringes is not taken out of the allowance for WIDTH, unlike
168 \\[split-window-horizontally]."
169   (interactive "P")
170   (split-window-horizontally
171    (cond ((null width) nil)
172          ((>= width 0) (+ width (mdw-horizontal-window-overhead)))
173          ((< width 0) width))))
174
175 (defun mdw-divvy-window (&optional width)
176   "Split a wide window into appropriate widths."
177   (interactive "P")
178   (setq width (cond (width (prefix-numeric-value width))
179                     ((and window-system
180                           (>= emacs-major-version 22))
181                      77)
182                     (t 78)))
183   (let* ((win (selected-window))
184          (sb-width (mdw-horizontal-window-overhead))
185          (c (/ (+ (window-width) sb-width)
186                (+ width sb-width))))
187     (while (> c 1)
188       (setq c (1- c))
189       (split-window-horizontally (+ width sb-width))
190       (other-window 1))
191     (select-window win)))
192
193 ;; Don't raise windows unless I say so.
194
195 (defvar mdw-inhibit-raise-frame nil
196   "*Whether `raise-frame' should do nothing when the frame is mapped.")
197
198 (defadvice raise-frame
199     (around mdw-inhibit (&optional frame) activate compile)
200   "Don't actually do anything if `mdw-inhibit-raise-frame' is true, and the
201 frame is actually mapped on the screen."
202   (if mdw-inhibit-raise-frame
203       (make-frame-visible frame)
204     ad-do-it))
205
206 (defmacro mdw-advise-to-inhibit-raise-frame (function)
207   "Advise the FUNCTION not to raise frames, even if it wants to."
208   `(defadvice ,function
209        (around mdw-inhibit-raise (&rest hunoz) activate compile)
210      "Don't raise the window unless you have to."
211      (let ((mdw-inhibit-raise-frame t))
212        ad-do-it)))
213
214 (mdw-advise-to-inhibit-raise-frame select-frame-set-input-focus)
215
216 ;; Transient mark mode hacks.
217
218 (defadvice exchange-point-and-mark
219     (around mdw-highlight (&optional arg) activate compile)
220   "Maybe don't actually exchange point and mark.
221 If `transient-mark-mode' is on and the mark is inactive, then
222 just activate it.  A non-trivial prefix argument will force the
223 usual behaviour.  A trivial prefix argument (i.e., just C-u) will
224 activate the mark and temporarily enable `transient-mark-mode' if
225 it's currently off."
226   (cond ((or mark-active
227              (and (not transient-mark-mode) (not arg))
228              (and arg (or (not (consp arg))
229                           (not (= (car arg) 4)))))
230          ad-do-it)
231         (t
232          (or transient-mark-mode (setq transient-mark-mode 'only))
233          (set-mark (mark t)))))
234
235 ;; Functions for sexp diary entries.
236
237 (defun mdw-weekday (l)
238   "Return non-nil if `date' falls on one of the days of the week in L.
239 L is a list of day numbers (from 0 to 6 for Sunday through to
240 Saturday) or symbols `sunday', `monday', etc. (or a mixture).  If
241 the date stored in `date' falls on a listed day, then the
242 function returns non-nil."
243   (let ((d (calendar-day-of-week date)))
244     (or (memq d l)
245         (memq (nth d '(sunday monday tuesday wednesday
246                               thursday friday saturday)) l))))
247
248 (defun mdw-todo (&optional when)
249   "Return non-nil today, or on WHEN, whichever is later."
250   (let ((w (calendar-absolute-from-gregorian (calendar-current-date)))
251         (d (calendar-absolute-from-gregorian date)))
252     (if when
253         (setq w (max w (calendar-absolute-from-gregorian
254                         (cond
255                          ((not european-calendar-style)
256                           when)
257                          ((> (car when) 100)
258                           (list (nth 1 when)
259                                 (nth 2 when)
260                                 (nth 0 when)))
261                          (t
262                           (list (nth 1 when)
263                                 (nth 0 when)
264                                 (nth 2 when))))))))
265     (eq w d)))
266
267 ;; Fighting with Org-mode's evil key maps.
268
269 (defvar mdw-evil-keymap-keys
270   '(([S-up] . [?\C-c up])
271     ([S-down] . [?\C-c down])
272     ([S-left] . [?\C-c left])
273     ([S-right] . [?\C-c right])
274     (([M-up] [?\e up]) . [C-up])
275     (([M-down] [?\e down]) . [C-down])
276     (([M-left] [?\e left]) . [C-left])
277     (([M-right] [?\e right]) . [C-right]))
278   "Defines evil keybindings to clobber in `mdw-clobber-evil-keymap'.
279 The value is an alist mapping evil keys (as a list, or singleton)
280 to good keys (in the same form).")
281
282 (defun mdw-clobber-evil-keymap (keymap)
283   "Replace evil key bindings in the KEYMAP.
284 Evil key bindings are defined in `mdw-evil-keymap-keys'."
285   (dolist (entry mdw-evil-keymap-keys)
286     (let ((binding nil)
287           (keys (if (listp (car entry))
288                     (car entry)
289                   (list (car entry))))
290           (replacements (if (listp (cdr entry))
291                             (cdr entry)
292                           (list (cdr entry)))))
293       (catch 'found
294         (dolist (key keys)
295           (setq binding (lookup-key keymap key))
296           (when binding
297             (throw 'found nil))))
298       (when binding
299         (dolist (key keys)
300           (define-key keymap key nil))
301         (dolist (key replacements)
302           (define-key keymap key binding))))))
303
304 (eval-after-load "org-latex"
305   '(progn
306      (push '("strayman"
307              "\\documentclass{strayman}
308 \\usepackage[utf8]{inputenc}
309 \\usepackage[palatino, helvetica, courier, maths=cmr]{mdwfonts}
310 \\usepackage[T1]{fontenc}
311 \\usepackage{graphicx, tikz, mdwtab, mdwmath, crypto, longtable}"
312              ("\\section{%s}" . "\\section*{%s}")
313              ("\\subsection{%s}" . "\\subsection*{%s}")
314              ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
315              ("\\paragraph{%s}" . "\\paragraph*{%s}")
316              ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
317            org-export-latex-classes)))
318
319 (setq org-export-docbook-xslt-proc-command "xsltproc --output %o %s %i"
320       org-export-docbook-xsl-fo-proc-command "fop %i.safe %o"
321       org-export-docbook-xslt-stylesheet
322       "/usr/share/xml/docbook/stylesheet/docbook-xsl/fo/docbook.xsl")
323
324 ;;;--------------------------------------------------------------------------
325 ;;; Mail and news hacking.
326
327 (define-derived-mode  mdwmail-mode mail-mode "[mdw] mail"
328   "Major mode for editing news and mail messages from external programs.
329 Not much right now.  Just support for doing MailCrypt stuff."
330   :syntax-table nil
331   :abbrev-table nil
332   (run-hooks 'mail-setup-hook))
333
334 (define-key mdwmail-mode-map [?\C-c ?\C-c] 'disabled-operation)
335
336 (add-hook 'mdwail-mode-hook
337           (lambda ()
338             (set-buffer-file-coding-system 'utf-8)
339             (make-local-variable 'paragraph-separate)
340             (make-local-variable 'paragraph-start)
341             (setq paragraph-start
342                   (concat "[ \t]*[-_][-_][-_]+$\\|^-- \\|-----\\|"
343                           paragraph-start))
344             (setq paragraph-separate
345                   (concat "[ \t]*[-_][-_][-_]+$\\|^-- \\|-----\\|"
346                           paragraph-separate))))
347
348 ;; How to encrypt in mdwmail.
349
350 (defun mdwmail-mc-encrypt (&optional recip scm start end from sign)
351   (or start
352       (setq start (save-excursion
353                     (goto-char (point-min))
354                     (or (search-forward "\n\n" nil t) (point-min)))))
355   (or end
356       (setq end (point-max)))
357   (mc-encrypt-generic recip scm start end from sign))
358
359 ;; How to sign in mdwmail.
360
361 (defun mdwmail-mc-sign (key scm start end uclr)
362   (or start
363       (setq start (save-excursion
364                     (goto-char (point-min))
365                     (or (search-forward "\n\n" nil t) (point-min)))))
366   (or end
367       (setq end (point-max)))
368   (mc-sign-generic key scm start end uclr))
369
370 ;; Some signature mangling.
371
372 (defun mdwmail-mangle-signature ()
373   (save-excursion
374     (goto-char (point-min))
375     (perform-replace "\n-- \n" "\n-- " nil nil nil)))
376 (add-hook 'mail-setup-hook 'mdwmail-mangle-signature)
377 (add-hook 'message-setup-hook 'mdwmail-mangle-signature)
378
379 ;; Insert my login name into message-ids, so I can score replies.
380
381 (defadvice message-unique-id (after mdw-user-name last activate compile)
382   "Ensure that the user's name appears at the end of the message-id string,
383 so that it can be used for convenient filtering."
384   (setq ad-return-value (concat ad-return-value "." (user-login-name))))
385
386 ;; Tell my movemail hack where movemail is.
387 ;;
388 ;; This is needed to shup up warnings about LD_PRELOAD.
389
390 (let ((path exec-path))
391   (while path
392     (let ((try (expand-file-name "movemail" (car path))))
393       (if (file-executable-p try)
394           (setenv "REAL_MOVEMAIL" try))
395       (setq path (cdr path)))))
396
397 (eval-after-load "erc"
398     '(load "~/.ercrc.el"))
399
400 ;;;--------------------------------------------------------------------------
401 ;;; Utility functions.
402
403 (or (fboundp 'line-number-at-pos)
404     (defun line-number-at-pos (&optional pos)
405       (let ((opoint (or pos (point))) start)
406         (save-excursion
407           (save-restriction
408             (goto-char (point-min))
409             (widen)
410             (forward-line 0)
411             (setq start (point))
412             (goto-char opoint)
413             (forward-line 0)
414             (1+ (count-lines 1 (point))))))))
415
416 (defun mdw-uniquify-alist (&rest alists)
417   "Return the concatenation of the ALISTS with duplicate elements removed.
418 The first association with a given key prevails; others are
419 ignored.  The input lists are not modified, although they'll
420 probably become garbage."
421   (and alists
422        (let ((start-list (cons nil nil)))
423          (mdw-do-uniquify start-list
424                           start-list
425                           (car alists)
426                           (cdr alists)))))
427
428 (defun mdw-do-uniquify (done end l rest)
429   "A helper function for mdw-uniquify-alist.
430 The DONE argument is a list whose first element is `nil'.  It
431 contains the uniquified alist built so far.  The leading `nil' is
432 stripped off at the end of the operation; it's only there so that
433 DONE always references a cons cell.  END refers to the final cons
434 cell in the DONE list; it is modified in place each time to avoid
435 the overheads of `append'ing all the time.  The L argument is the
436 alist we're currently processing; the remaining alists are given
437 in REST."
438
439   ;; There are several different cases to deal with here.
440   (cond
441
442    ;; Current list isn't empty.  Add the first item to the DONE list if
443    ;; there's not an item with the same KEY already there.
444    (l (or (assoc (car (car l)) done)
445           (progn
446             (setcdr end (cons (car l) nil))
447             (setq end (cdr end))))
448       (mdw-do-uniquify done end (cdr l) rest))
449
450    ;; The list we were working on is empty.  Shunt the next list into the
451    ;; current list position and go round again.
452    (rest (mdw-do-uniquify done end (car rest) (cdr rest)))
453
454    ;; Everything's done.  Remove the leading `nil' from the DONE list and
455    ;; return it.  Finished!
456    (t (cdr done))))
457
458 (defun date ()
459   "Insert the current date in a pleasing way."
460   (interactive)
461   (insert (save-excursion
462             (let ((buffer (get-buffer-create "*tmp*")))
463               (unwind-protect (progn (set-buffer buffer)
464                                      (erase-buffer)
465                                      (shell-command "date +%Y-%m-%d" t)
466                                      (goto-char (mark))
467                                      (delete-backward-char 1)
468                                      (buffer-string))
469                 (kill-buffer buffer))))))
470
471 (defun uuencode (file &optional name)
472   "UUencodes a file, maybe calling it NAME, into the current buffer."
473   (interactive "fInput file name: ")
474
475   ;; If NAME isn't specified, then guess from the filename.
476   (if (not name)
477       (setq name
478             (substring file
479                        (or (string-match "[^/]*$" file) 0))))
480   (print (format "uuencode `%s' `%s'" file name))
481
482   ;; Now actually do the thing.
483   (call-process "uuencode" file t nil name))
484
485 (defvar np-file "~/.np"
486   "*Where the `now-playing' file is.")
487
488 (defun np (&optional arg)
489   "Grabs a `now-playing' string."
490   (interactive)
491   (save-excursion
492     (or arg (progn
493               (goto-char (point-max))
494               (insert "\nNP: ")
495               (insert-file-contents np-file)))))
496
497 (defun mdw-version-< (ver-a ver-b)
498   "Answer whether VER-A is strictly earlier than VER-B.
499 VER-A and VER-B are version numbers, which are strings containing digit
500 sequences separated by `.'."
501   (let* ((la (mapcar (lambda (x) (car (read-from-string x)))
502                      (split-string ver-a "\\.")))
503          (lb (mapcar (lambda (x) (car (read-from-string x)))
504                      (split-string ver-b "\\."))))
505     (catch 'done
506       (while t
507         (cond ((null la) (throw 'done lb))
508               ((null lb) (throw 'done nil))
509               ((< (car la) (car lb)) (throw 'done t))
510               ((= (car la) (car lb)) (setq la (cdr la) lb (cdr lb))))))))
511
512 (defun mdw-check-autorevert ()
513   "Sets global-auto-revert-ignore-buffer appropriately for this buffer.
514 This takes into consideration whether it's been found using
515 tramp, which seems to get itself into a twist."
516   (cond ((not (boundp 'global-auto-revert-ignore-buffer))
517          nil)
518         ((and (buffer-file-name)
519               (fboundp 'tramp-tramp-file-p)
520               (tramp-tramp-file-p (buffer-file-name)))
521          (unless global-auto-revert-ignore-buffer
522            (setq global-auto-revert-ignore-buffer 'tramp)))
523         ((eq global-auto-revert-ignore-buffer 'tramp)
524          (setq global-auto-revert-ignore-buffer nil))))
525
526 (defadvice find-file (after mdw-autorevert activate)
527   (mdw-check-autorevert))
528 (defadvice write-file (after mdw-autorevert activate)
529   (mdw-check-autorevert))
530
531 ;;;--------------------------------------------------------------------------
532 ;;; Dired hacking.
533
534 (defadvice dired-maybe-insert-subdir
535     (around mdw-marked-insertion first activate)
536   "The DIRNAME may be a list of directory names to insert.
537 Interactively, if files are marked, then insert all of them.
538 With a numeric prefix argument, select that many entries near
539 point; with a non-numeric prefix argument, prompt for listing
540 options."
541   (interactive
542    (list (dired-get-marked-files nil
543                                  (and (integerp current-prefix-arg)
544                                       current-prefix-arg)
545                                  #'file-directory-p)
546          (and current-prefix-arg
547               (not (integerp current-prefix-arg))
548               (read-string "Switches for listing: "
549                            (or dired-subdir-switches
550                                dired-actual-switches)))))
551   (let ((dirs (ad-get-arg 0)))
552     (dolist (dir (if (listp dirs) dirs (list dirs)))
553       (ad-set-arg 0 dir)
554       ad-do-it)))
555
556 ;;;--------------------------------------------------------------------------
557 ;;; URL viewing.
558
559 (defun mdw-w3m-browse-url (url &optional new-session-p)
560   "Invoke w3m on the URL in its current window, or at least a different one.
561 If NEW-SESSION-P, start a new session."
562   (interactive "sURL: \nP")
563   (save-excursion
564     (let ((window (selected-window)))
565       (unwind-protect
566           (progn
567             (select-window (or (and (not new-session-p)
568                                     (get-buffer-window "*w3m*"))
569                                (progn
570                                  (if (one-window-p t) (split-window))
571                                  (get-lru-window))))
572             (w3m-browse-url url new-session-p))
573         (select-window window)))))
574
575 (defvar mdw-good-url-browsers
576   '(browse-url-generic
577     (w3m . mdw-w3m-browse-url)
578     browse-url-w3
579     browse-url-mozilla)
580   "List of good browsers for mdw-good-url-browsers.
581 Each item is a browser function name, or a cons (CHECK . FUNC).
582 A symbol FOO stands for (FOO . FOO).")
583
584 (defun mdw-good-url-browser ()
585   "Return a good URL browser.
586 Trundle the list of such things, finding the first item for which
587 CHECK is fboundp, and returning the correponding FUNC."
588   (let ((bs mdw-good-url-browsers) b check func answer)
589     (while (and bs (not answer))
590       (setq b (car bs)
591             bs (cdr bs))
592       (if (consp b)
593           (setq check (car b) func (cdr b))
594         (setq check b func b))
595       (if (fboundp check)
596           (setq answer func)))
597     answer))
598
599 (eval-after-load "w3m-search"
600   '(progn
601      (dolist
602          (item
603           '(("g" "Google" "http://www.google.co.uk/search?q=%s")
604             ("gd" "Google Directory"
605              "http://www.google.com/search?cat=gwd/Top&q=%s")
606             ("gg" "Google Groups" "http://groups.google.com/groups?q=%s")
607             ("ward" "Ward's wiki" "http://c2.com/cgi/wiki?%s")
608             ("gi" "Images" "http://images.google.com/images?q=%s")
609             ("rfc" "RFC"
610              "http://metalzone.distorted.org.uk/ftp/pub/mirrors/rfc/rfc%s.txt.gz")
611             ("wp" "Wikipedia"
612              "http://en.wikipedia.org/wiki/Special:Search?go=Go&search=%s")
613             ("imdb" "IMDb" "http://www.imdb.com/Find?%s")
614             ("nc-wiki" "nCipher wiki"
615              "http://wiki.ncipher.com/wiki/bin/view/Devel/?topic=%s")
616             ("map" "Google maps" "http://maps.google.co.uk/maps?q=%s&hl=en")
617             ("lp" "Launchpad bug by number"
618              "https://bugs.launchpad.net/bugs/%s")
619             ("lppkg" "Launchpad bugs by package"
620              "https://bugs.launchpad.net/%s")
621             ("msdn" "MSDN"
622              "http://social.msdn.microsoft.com/Search/en-GB/?query=%s&ac=8")
623             ("debbug" "Debian bug by number"
624              "http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=%s")
625             ("debbugpkg" "Debian bugs by package"
626              "http://bugs.debian.org/cgi-bin/pkgreport.cgi?pkg=%s")
627             ("ljlogin" "LJ login" "http://www.livejournal.com/login.bml")))
628        (add-to-list 'w3m-search-engine-alist
629                     (list (cadr item) (caddr item) nil))
630        (add-to-list 'w3m-uri-replace-alist
631                     (list (concat "\\`" (car item) ":")
632                           'w3m-search-uri-replace
633                           (cadr item))))))
634
635 ;;;--------------------------------------------------------------------------
636 ;;; Paragraph filling.
637
638 ;; Useful variables.
639
640 (defvar mdw-fill-prefix nil
641   "*Used by `mdw-line-prefix' and `mdw-fill-paragraph'.
642 If there's no fill prefix currently set (by the `fill-prefix'
643 variable) and there's a match from one of the regexps here, it
644 gets used to set the fill-prefix for the current operation.
645
646 The variable is a list of items of the form `REGEXP . PREFIX'; if
647 the REGEXP matches, the PREFIX is used to set the fill prefix.
648 It in turn is a list of things:
649
650   STRING -- insert a literal string
651   (match . N) -- insert the thing matched by bracketed subexpression N
652   (pad . N) -- a string of whitespace the same width as subexpression N
653   (expr . FORM) -- the result of evaluating FORM")
654
655 (make-variable-buffer-local 'mdw-fill-prefix)
656
657 (defvar mdw-hanging-indents
658   (concat "\\(\\("
659             "\\([*o+]\\|-[-#]?\\|[0-9]+\\.\\|\\[[0-9]+\\]\\|([a-zA-Z])\\)"
660             "[ \t]+"
661           "\\)?\\)")
662   "*Standard regexp matching parts of a hanging indent.
663 This is mainly useful in `auto-fill-mode'.")
664
665 ;; Setting things up.
666
667 (fset 'mdw-do-auto-fill (symbol-function 'do-auto-fill))
668
669 ;; Utility functions.
670
671 (defun mdw-maybe-tabify (s)
672   "Tabify or untabify the string S, according to `indent-tabs-mode'."
673   (let ((tabfun (if indent-tabs-mode #'tabify #'untabify)))
674     (with-temp-buffer
675       (save-match-data
676         (insert s "\n")
677         (let ((start (point-min)) (end (point-max)))
678           (funcall tabfun (point-min) (point-max))
679           (setq s (buffer-substring (point-min) (1- (point-max)))))))))
680
681 (defun mdw-examine-fill-prefixes (l)
682   "Given a list of dynamic fill prefixes, pick one which matches
683 context and return the static fill prefix to use.  Point must be
684 at the start of a line, and match data must be saved."
685   (cond ((not l) nil)
686                ((looking-at (car (car l)))
687                 (mdw-maybe-tabify (apply #'concat
688                                          (mapcar #'mdw-do-prefix-match
689                                                  (cdr (car l))))))
690                (t (mdw-examine-fill-prefixes (cdr l)))))
691
692 (defun mdw-maybe-car (p)
693   "If P is a pair, return (car P), otherwise just return P."
694   (if (consp p) (car p) p))
695
696 (defun mdw-padding (s)
697   "Return a string the same width as S but made entirely from whitespace."
698   (let* ((l (length s)) (i 0) (n (make-string l ? )))
699     (while (< i l)
700       (if (= 9 (aref s i))
701           (aset n i 9))
702       (setq i (1+ i)))
703     n))
704
705 (defun mdw-do-prefix-match (m)
706   "Expand a dynamic prefix match element.
707 See `mdw-fill-prefix' for details."
708   (cond ((not (consp m)) (format "%s" m))
709            ((eq (car m) 'match) (match-string (mdw-maybe-car (cdr m))))
710            ((eq (car m) 'pad) (mdw-padding (match-string
711                                             (mdw-maybe-car (cdr m)))))
712            ((eq (car m) 'eval) (eval (cdr m)))
713            (t "")))
714
715 (defun mdw-choose-dynamic-fill-prefix ()
716   "Work out the dynamic fill prefix based on the variable `mdw-fill-prefix'."
717   (cond ((and fill-prefix (not (string= fill-prefix ""))) fill-prefix)
718            ((not mdw-fill-prefix) fill-prefix)
719            (t (save-excursion
720                 (beginning-of-line)
721                 (save-match-data
722                   (mdw-examine-fill-prefixes mdw-fill-prefix))))))
723
724 (defun do-auto-fill ()
725   "Handle auto-filling, working out a dynamic fill prefix in the
726 case where there isn't a sensible static one."
727   (let ((fill-prefix (mdw-choose-dynamic-fill-prefix)))
728     (mdw-do-auto-fill)))
729
730 (defun mdw-fill-paragraph ()
731   "Fill paragraph, getting a dynamic fill prefix."
732   (interactive)
733   (let ((fill-prefix (mdw-choose-dynamic-fill-prefix)))
734     (fill-paragraph nil)))
735
736 (defun mdw-standard-fill-prefix (rx &optional mat)
737   "Set the dynamic fill prefix, handling standard hanging indents and stuff.
738 This is just a short-cut for setting the thing by hand, and by
739 design it doesn't cope with anything approximating a complicated
740 case."
741   (setq mdw-fill-prefix
742            `((,(concat rx mdw-hanging-indents)
743               (match . 1)
744               (pad . ,(or mat 2))))))
745
746 ;;;--------------------------------------------------------------------------
747 ;;; Other common declarations.
748
749 ;; Common mode settings.
750
751 (defvar mdw-auto-indent t
752   "Whether to indent automatically after a newline.")
753
754 (defun mdw-whitespace-mode (&optional arg)
755   "Turn on/off whitespace mode, but don't highlight trailing space."
756   (interactive "P")
757   (when (and (boundp 'whitespace-style)
758              (fboundp 'whitespace-mode))
759     (let ((whitespace-style (remove 'trailing whitespace-style)))
760       (whitespace-mode arg))
761     (setq show-trailing-whitespace whitespace-mode)))
762
763 (defun mdw-misc-mode-config ()
764   (and mdw-auto-indent
765        (cond ((eq major-mode 'lisp-mode)
766               (local-set-key "\C-m" 'mdw-indent-newline-and-indent))
767              ((or (eq major-mode 'slime-repl-mode)
768                   (eq major-mode 'asm-mode))
769               nil)
770              (t
771               (local-set-key "\C-m" 'newline-and-indent))))
772   (local-set-key [C-return] 'newline)
773   (make-local-variable 'page-delimiter)
774   (setq page-delimiter "\f\\|^.*-\\{6\\}.*$")
775   (setq comment-column 40)
776   (auto-fill-mode 1)
777   (setq fill-column 77)
778   (setq show-trailing-whitespace t)
779   (mdw-whitespace-mode 1)
780   (and (fboundp 'gtags-mode)
781        (gtags-mode))
782   (if (fboundp 'hs-minor-mode)
783       (trap (hs-minor-mode t))
784     (outline-minor-mode t))
785   (reveal-mode t)
786   (trap (turn-on-font-lock)))
787
788 (defun mdw-post-config-mode-hack ()
789   (mdw-whitespace-mode 1))
790
791 (eval-after-load 'gtags
792   '(progn
793      (dolist (key '([mouse-2] [mouse-3]))
794        (define-key gtags-mode-map key nil))
795      (define-key gtags-mode-map [C-S-mouse-2] 'gtags-find-tag-by-event)
796      (define-key gtags-select-mode-map [C-S-mouse-2]
797        'gtags-select-tag-by-event)
798      (dolist (map (list gtags-mode-map gtags-select-mode-map))
799        (define-key map [C-S-mouse-3] 'gtags-pop-stack))))
800
801 ;; Backup file handling.
802
803 (defvar mdw-backup-disable-regexps nil
804   "*List of regular expressions: if a file name matches any of
805 these then the file is not backed up.")
806
807 (defun mdw-backup-enable-predicate (name)
808   "[mdw]'s default backup predicate.
809 Allows a backup if the standard predicate would allow it, and it
810 doesn't match any of the regular expressions in
811 `mdw-backup-disable-regexps'."
812   (and (normal-backup-enable-predicate name)
813        (let ((answer t) (list mdw-backup-disable-regexps))
814          (save-match-data
815            (while list
816              (if (string-match (car list) name)
817                  (setq answer nil))
818              (setq list (cdr list)))
819            answer))))
820 (setq backup-enable-predicate 'mdw-backup-enable-predicate)
821
822 ;; Frame cleanup.
823
824 (defun mdw-last-one-out-turn-off-the-lights (frame)
825   "Disconnect from an X display if this was the last frame on that display."
826   (let ((frame-display (frame-parameter frame 'display)))
827     (when (and frame-display
828                (eq window-system 'x)
829                (not (some (lambda (fr)
830                             (and (not (eq fr frame))
831                                  (string= (frame-parameter fr 'display)
832                                           frame-display)))
833                           (frame-list))))
834       (run-with-idle-timer 0 nil #'x-close-connection frame-display))))
835 (add-hook 'delete-frame-functions 'mdw-last-one-out-turn-off-the-lights)
836
837 ;;;--------------------------------------------------------------------------
838 ;;; General fontification.
839
840 (defmacro mdw-define-face (name &rest body)
841   "Define a face, and make sure it's actually set as the definition."
842   (declare (indent 1)
843            (debug 0))
844   `(progn
845      (make-face ',name)
846      (defvar ,name ',name)
847      (put ',name 'face-defface-spec ',body)
848      (face-spec-set ',name ',body nil)))
849
850 (mdw-define-face default
851   (((type w32)) :family "courier new" :height 85)
852   (((type x)) :family "6x13" :foundry "trad" :height 130)
853   (((type color)) :foreground "white" :background "black")
854   (t nil))
855 (mdw-define-face fixed-pitch
856   (((type w32)) :family "courier new" :height 85)
857   (((type x)) :family "6x13" :foundry "trad" :height 130)
858   (t :foreground "white" :background "black"))
859 (if (>= emacs-major-version 23)
860     (mdw-define-face variable-pitch
861       (((type x)) :family "sans" :height 100))
862   (mdw-define-face variable-pitch
863     (((type x)) :family "helvetica" :height 90)))
864 (mdw-define-face region
865   (((type tty) (class color)) :background "blue")
866   (((type tty) (class mono)) :inverse-video t)
867   (t :background "grey30"))
868 (mdw-define-face minibuffer-prompt
869   (t :weight bold))
870 (mdw-define-face mode-line
871   (((class color)) :foreground "blue" :background "yellow"
872                    :box (:line-width 1 :style released-button))
873   (t :inverse-video t))
874 (mdw-define-face mode-line-inactive
875   (((class color)) :foreground "yellow" :background "blue"
876                    :box (:line-width 1 :style released-button))
877   (t :inverse-video t))
878 (mdw-define-face nobreak-space
879   (((type tty)))
880   (t :inherit escape-glyph :underline t))
881 (mdw-define-face scroll-bar
882   (t :foreground "black" :background "lightgrey"))
883 (mdw-define-face fringe
884   (t :foreground "yellow"))
885 (mdw-define-face show-paren-match
886   (((class color)) :background "darkgreen")
887   (t :underline t))
888 (mdw-define-face show-paren-mismatch
889   (((class color)) :background "red")
890   (t :inverse-video t))
891 (mdw-define-face highlight
892   (((type x) (class color)) :background "DarkSeaGreen4")
893   (((type tty) (class color)) :background "cyan")
894   (t :inverse-video t))
895
896 (mdw-define-face holiday-face
897   (t :background "red"))
898 (mdw-define-face calendar-today-face
899   (t :foreground "yellow" :weight bold))
900
901 (mdw-define-face comint-highlight-prompt
902   (t :weight bold))
903 (mdw-define-face comint-highlight-input
904   (t nil))
905
906 (mdw-define-face dired-directory
907   (t :foreground "cyan" :weight bold))
908 (mdw-define-face dired-symlink
909   (t :foreground "cyan"))
910 (mdw-define-face dired-perm-write
911   (t nil))
912
913 (mdw-define-face trailing-whitespace
914   (((class color)) :background "red")
915   (t :inverse-video t))
916 (mdw-define-face mdw-punct-face
917   (((type tty)) :foreground "yellow") (t :foreground "burlywood2"))
918 (mdw-define-face mdw-number-face
919   (t :foreground "yellow"))
920 (mdw-define-face mdw-trivial-face)
921 (mdw-define-face font-lock-function-name-face
922   (t :slant italic))
923 (mdw-define-face font-lock-keyword-face
924   (t :weight bold))
925 (mdw-define-face font-lock-constant-face
926   (t :slant italic))
927 (mdw-define-face font-lock-builtin-face
928   (t :weight bold))
929 (mdw-define-face font-lock-type-face
930   (t :weight bold :slant italic))
931 (mdw-define-face font-lock-reference-face
932   (t :weight bold))
933 (mdw-define-face font-lock-variable-name-face
934   (t :slant italic))
935 (mdw-define-face font-lock-comment-delimiter-face
936   (((class mono)) :weight bold)
937   (((type tty) (class color)) :foreground "green")
938   (t :slant italic :foreground "SeaGreen1"))
939 (mdw-define-face font-lock-comment-face
940   (((class mono)) :weight bold)
941   (((type tty) (class color)) :foreground "green")
942   (t :slant italic :foreground "SeaGreen1"))
943 (mdw-define-face font-lock-string-face
944   (((class mono)) :weight bold)
945   (((class color)) :foreground "SkyBlue1"))
946
947 (mdw-define-face message-separator
948   (t :background "red" :foreground "white" :weight bold))
949 (mdw-define-face message-cited-text
950   (default :slant italic)
951   (((type tty)) :foreground "green") (t :foreground "SeaGreen1"))
952 (mdw-define-face message-header-cc
953   (default :weight bold)
954   (((type tty)) :foreground "green") (t :foreground "SeaGreen1"))
955 (mdw-define-face message-header-newsgroups
956   (default :weight bold)
957   (((type tty)) :foreground "green") (t :foreground "SeaGreen1"))
958 (mdw-define-face message-header-subject
959   (default :weight bold)
960   (((type tty)) :foreground "green") (t :foreground "SeaGreen1"))
961 (mdw-define-face message-header-to
962   (default :weight bold)
963   (((type tty)) :foreground "green") (t :foreground "SeaGreen1"))
964 (mdw-define-face message-header-xheader
965   (default :weight bold)
966   (((type tty)) :foreground "green") (t :foreground "SeaGreen1"))
967 (mdw-define-face message-header-other
968   (default :weight bold)
969   (((type tty)) :foreground "green") (t :foreground "SeaGreen1"))
970 (mdw-define-face message-header-name
971   (((type tty)) :foreground "green") (t :foreground "SeaGreen1"))
972 (mdw-define-face which-func
973   (t nil))
974
975 (mdw-define-face diff-header
976   (t nil))
977 (mdw-define-face diff-index
978   (t :weight bold))
979 (mdw-define-face diff-file-header
980   (t :weight bold))
981 (mdw-define-face diff-hunk-header
982   (t :foreground "SkyBlue1"))
983 (mdw-define-face diff-function
984   (t :foreground "SkyBlue1" :weight bold))
985 (mdw-define-face diff-header
986   (t :background "grey10"))
987 (mdw-define-face diff-added
988   (t :foreground "green"))
989 (mdw-define-face diff-removed
990   (t :foreground "red"))
991 (mdw-define-face diff-context
992   (t nil))
993 (mdw-define-face diff-refine-change
994   (((class color) (type x)) :background "RoyalBlue4")
995   (t :underline t))
996
997 (mdw-define-face dylan-header-background
998   (((class color) (type x)) :background "NavyBlue")
999   (t :background "blue"))
1000
1001 (mdw-define-face magit-diff-add
1002   (t :foreground "green"))
1003 (mdw-define-face magit-diff-del
1004   (t :foreground "red"))
1005 (mdw-define-face magit-diff-file-header
1006   (t :weight bold))
1007 (mdw-define-face magit-diff-hunk-header
1008   (t :foreground "SkyBlue1"))
1009 (mdw-define-face magit-item-highlight
1010   (((type tty)) :background "blue")
1011   (t :background "DarkSeaGreen4"))
1012 (mdw-define-face magit-log-head-label-remote
1013   (((type tty)) :background "cyan" :foreground "green")
1014   (t :background "grey11" :foreground "DarkSeaGreen2" :box t))
1015 (mdw-define-face magit-log-head-label-local
1016   (((type tty)) :background "cyan" :foreground "yellow")
1017   (t :background "grey11" :foreground "LightSkyBlue1" :box t))
1018 (mdw-define-face magit-log-head-label-tags
1019   (((type tty)) :background "red" :foreground "yellow")
1020   (t :background "LemonChiffon1" :foreground "goldenrod4" :box t))
1021 (mdw-define-face magit-log-graph
1022   (((type tty)) :foreground "magenta")
1023   (t :foreground "grey80"))
1024
1025 (mdw-define-face erc-input-face
1026   (t :foreground "red"))
1027
1028 (mdw-define-face woman-bold
1029   (t :weight bold))
1030 (mdw-define-face woman-italic
1031   (t :slant italic))
1032
1033 (eval-after-load "rst"
1034   '(progn
1035      (mdw-define-face rst-level-1-face
1036        (t :foreground "SkyBlue1" :weight bold))
1037      (mdw-define-face rst-level-2-face
1038        (t :foreground "SeaGreen1" :weight bold))
1039      (mdw-define-face rst-level-3-face
1040        (t :weight bold))
1041      (mdw-define-face rst-level-4-face
1042        (t :slant italic))
1043      (mdw-define-face rst-level-5-face
1044        (t :underline t))
1045      (mdw-define-face rst-level-6-face
1046        ())))
1047
1048 (mdw-define-face p4-depot-added-face
1049   (t :foreground "green"))
1050 (mdw-define-face p4-depot-branch-op-face
1051   (t :foreground "yellow"))
1052 (mdw-define-face p4-depot-deleted-face
1053   (t :foreground "red"))
1054 (mdw-define-face p4-depot-unmapped-face
1055   (t :foreground "SkyBlue1"))
1056 (mdw-define-face p4-diff-change-face
1057   (t :foreground "yellow"))
1058 (mdw-define-face p4-diff-del-face
1059   (t :foreground "red"))
1060 (mdw-define-face p4-diff-file-face
1061   (t :foreground "SkyBlue1"))
1062 (mdw-define-face p4-diff-head-face
1063   (t :background "grey10"))
1064 (mdw-define-face p4-diff-ins-face
1065   (t :foreground "green"))
1066
1067 (mdw-define-face w3m-anchor-face
1068   (t :foreground "SkyBlue1" :underline t))
1069 (mdw-define-face w3m-arrived-anchor-face
1070   (t :foreground "SkyBlue1" :underline t))
1071
1072 (mdw-define-face whizzy-slice-face
1073   (t :background "grey10"))
1074 (mdw-define-face whizzy-error-face
1075   (t :background "darkred"))
1076
1077 ;; Ellipses used to indicate hidden text (and similar).
1078 (mdw-define-face mdw-ellipsis-face
1079   (((type tty)) :foreground "blue") (t :foreground "grey60"))
1080 (let ((dollar (make-glyph-code ?$ 'mdw-ellipsis-face))
1081       (backslash (make-glyph-code ?\ 'mdw-ellipsis-face))
1082       (dot (make-glyph-code ?. 'mdw-ellipsis-face))
1083       (bar (make-glyph-code ?| mdw-ellipsis-face)))
1084   (set-display-table-slot standard-display-table 0 dollar)
1085   (set-display-table-slot standard-display-table 1 backslash)
1086   (set-display-table-slot standard-display-table 4
1087                           (vector dot dot dot))
1088   (set-display-table-slot standard-display-table 5 bar))
1089
1090 ;;;--------------------------------------------------------------------------
1091 ;;; C programming configuration.
1092
1093 ;; Linux kernel hacking.
1094
1095 (defvar linux-c-mode-hook)
1096
1097 (defun linux-c-mode ()
1098   (interactive)
1099   (c-mode)
1100   (setq major-mode 'linux-c-mode)
1101   (setq mode-name "Linux C")
1102   (run-hooks 'linux-c-mode-hook))
1103
1104 ;; Make C indentation nice.
1105
1106 (defun mdw-c-lineup-arglist (langelem)
1107   "Hack for DWIMmery in c-lineup-arglist."
1108   (if (save-excursion
1109         (c-block-in-arglist-dwim (c-langelem-2nd-pos c-syntactic-element)))
1110       0
1111     (c-lineup-arglist langelem)))
1112
1113 (defun mdw-c-indent-extern-mumble (langelem)
1114   "Indent `extern \"...\" {' lines."
1115   (save-excursion
1116     (back-to-indentation)
1117     (if (looking-at
1118          "\\s-*\\<extern\\>\\s-*\"\\([^\\\\\"]+\\|\\.\\)*\"\\s-*{")
1119         c-basic-offset
1120       nil)))
1121
1122 (defun mdw-c-style ()
1123   (c-add-style "[mdw] C and C++ style"
1124                '((c-basic-offset . 2)
1125                  (comment-column . 40)
1126                  (c-class-key . "class")
1127                  (c-backslash-column . 72)
1128                  (c-offsets-alist
1129                   (substatement-open . (add 0 c-indent-one-line-block))
1130                   (defun-open . (add 0 c-indent-one-line-block))
1131                   (arglist-cont-nonempty . mdw-c-lineup-arglist)
1132                   (topmost-intro . mdw-c-indent-extern-mumble)
1133                   (cpp-define-intro . 0)
1134                   (knr-argdecl . 0)
1135                   (inextern-lang . [0])
1136                   (label . 0)
1137                   (case-label . +)
1138                   (access-label . -)
1139                   (inclass . +)
1140                   (inline-open . ++)
1141                   (statement-cont . +)
1142                   (statement-case-intro . +)))
1143                t))
1144
1145 (defvar mdw-c-comment-fill-prefix
1146   `((,(concat "\\([ \t]*/?\\)"
1147               "\\(\*\\|//]\\)"
1148               "\\([ \t]*\\)"
1149               "\\([A-Za-z]+:[ \t]*\\)?"
1150               mdw-hanging-indents)
1151      (pad . 1) (match . 2) (pad . 3) (pad . 4) (pad . 5)))
1152   "Fill prefix matching C comments (both kinds).")
1153
1154 (defun mdw-fontify-c-and-c++ ()
1155
1156   ;; Fiddle with some syntax codes.
1157   (modify-syntax-entry ?* ". 23")
1158   (modify-syntax-entry ?/ ". 124b")
1159   (modify-syntax-entry ?\n "> b")
1160
1161   ;; Other stuff.
1162   (mdw-c-style)
1163   (setq c-hanging-comment-ender-p nil)
1164   (setq c-backslash-column 72)
1165   (setq c-label-minimum-indentation 0)
1166   (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
1167
1168   ;; Now define things to be fontified.
1169   (make-local-variable 'font-lock-keywords)
1170   (let ((c-keywords
1171          (mdw-regexps "and"             ;C++
1172                       "and_eq"          ;C++
1173                       "asm"             ;K&R, GCC
1174                       "auto"            ;K&R, C89
1175                       "bitand"          ;C++
1176                       "bitor"           ;C++
1177                       "bool"            ;C++, C9X macro
1178                       "break"           ;K&R, C89
1179                       "case"            ;K&R, C89
1180                       "catch"           ;C++
1181                       "char"            ;K&R, C89
1182                       "class"           ;C++
1183                       "complex"         ;C9X macro, C++ template type
1184                       "compl"           ;C++
1185                       "const"           ;C89
1186                       "const_cast"      ;C++
1187                       "continue"        ;K&R, C89
1188                       "defined"         ;C89 preprocessor
1189                       "default"         ;K&R, C89
1190                       "delete"          ;C++
1191                       "do"              ;K&R, C89
1192                       "double"          ;K&R, C89
1193                       "dynamic_cast"    ;C++
1194                       "else"            ;K&R, C89
1195                       ;; "entry"        ;K&R -- never used
1196                       "enum"            ;C89
1197                       "explicit"        ;C++
1198                       "export"          ;C++
1199                       "extern"          ;K&R, C89
1200                       "false"           ;C++, C9X macro
1201                       "float"           ;K&R, C89
1202                       "for"             ;K&R, C89
1203                       ;; "fortran"      ;K&R
1204                       "friend"          ;C++
1205                       "goto"            ;K&R, C89
1206                       "if"              ;K&R, C89
1207                       "imaginary"       ;C9X macro
1208                       "inline"          ;C++, C9X, GCC
1209                       "int"             ;K&R, C89
1210                       "long"            ;K&R, C89
1211                       "mutable"         ;C++
1212                       "namespace"       ;C++
1213                       "new"             ;C++
1214                       "operator"        ;C++
1215                       "or"              ;C++
1216                       "or_eq"           ;C++
1217                       "private"         ;C++
1218                       "protected"       ;C++
1219                       "public"          ;C++
1220                       "register"        ;K&R, C89
1221                       "reinterpret_cast" ;C++
1222                       "restrict"         ;C9X
1223                       "return"           ;K&R, C89
1224                       "short"            ;K&R, C89
1225                       "signed"           ;C89
1226                       "sizeof"           ;K&R, C89
1227                       "static"           ;K&R, C89
1228                       "static_cast"      ;C++
1229                       "struct"           ;K&R, C89
1230                       "switch"           ;K&R, C89
1231                       "template"         ;C++
1232                       "this"             ;C++
1233                       "throw"            ;C++
1234                       "true"             ;C++, C9X macro
1235                       "try"              ;C++
1236                       "this"             ;C++
1237                       "typedef"          ;C89
1238                       "typeid"           ;C++
1239                       "typeof"           ;GCC
1240                       "typename"         ;C++
1241                       "union"            ;K&R, C89
1242                       "unsigned"         ;K&R, C89
1243                       "using"            ;C++
1244                       "virtual"          ;C++
1245                       "void"             ;C89
1246                       "volatile"         ;C89
1247                       "wchar_t"          ;C++, C89 library type
1248                       "while"            ;K&R, C89
1249                       "xor"              ;C++
1250                       "xor_eq"           ;C++
1251                       "_Bool"            ;C9X
1252                       "_Complex"         ;C9X
1253                       "_Imaginary"       ;C9X
1254                       "_Pragma"          ;C9X preprocessor
1255                       "__alignof__"      ;GCC
1256                       "__asm__"          ;GCC
1257                       "__attribute__"    ;GCC
1258                       "__complex__"      ;GCC
1259                       "__const__"        ;GCC
1260                       "__extension__"    ;GCC
1261                       "__imag__"         ;GCC
1262                       "__inline__"       ;GCC
1263                       "__label__"        ;GCC
1264                       "__real__"         ;GCC
1265                       "__signed__"       ;GCC
1266                       "__typeof__"       ;GCC
1267                       "__volatile__"     ;GCC
1268                       ))
1269         (preprocessor-keywords
1270          (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
1271                       "ident" "if" "ifdef" "ifndef" "import" "include"
1272                       "line" "pragma" "unassert" "undef" "warning"))
1273         (objc-keywords
1274          (mdw-regexps "class" "defs" "encode" "end" "implementation"
1275                       "interface" "private" "protected" "protocol" "public"
1276                       "selector")))
1277
1278     (setq font-lock-keywords
1279           (list
1280
1281            ;; Fontify include files as strings.
1282            (list (concat "^[ \t]*\\#[ \t]*"
1283                          "\\(include\\|import\\)"
1284                          "[ \t]*\\(<[^>]+\\(>\\|\\)\\)")
1285                  '(2 font-lock-string-face))
1286
1287            ;; Preprocessor directives are `references'?.
1288            (list (concat "^\\([ \t]*#[ \t]*\\(\\("
1289                          preprocessor-keywords
1290                          "\\)\\>\\|[0-9]+\\|$\\)\\)")
1291                  '(1 font-lock-keyword-face))
1292
1293            ;; Handle the keywords defined above.
1294            (list (concat "@\\<\\(" objc-keywords "\\)\\>")
1295                  '(0 font-lock-keyword-face))
1296
1297            (list (concat "\\<\\(" c-keywords "\\)\\>")
1298                  '(0 font-lock-keyword-face))
1299
1300            ;; Handle numbers too.
1301            ;;
1302            ;; This looks strange, I know.  It corresponds to the
1303            ;; preprocessor's idea of what a number looks like, rather than
1304            ;; anything sensible.
1305            (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
1306                          "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
1307                  '(0 mdw-number-face))
1308
1309            ;; And anything else is punctuation.
1310            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1311                  '(0 mdw-punct-face))))
1312
1313     (mdw-post-config-mode-hack)))
1314
1315 ;;;--------------------------------------------------------------------------
1316 ;;; AP calc mode.
1317
1318 (defun apcalc-mode ()
1319   (interactive)
1320   (c-mode)
1321   (setq major-mode 'apcalc-mode)
1322   (setq mode-name "AP Calc")
1323   (run-hooks 'apcalc-mode-hook))
1324
1325 (defun mdw-fontify-apcalc ()
1326
1327   ;; Fiddle with some syntax codes.
1328   (modify-syntax-entry ?* ". 23")
1329   (modify-syntax-entry ?/ ". 14")
1330
1331   ;; Other stuff.
1332   (mdw-c-style)
1333   (setq c-hanging-comment-ender-p nil)
1334   (setq c-backslash-column 72)
1335   (setq comment-start "/* ")
1336   (setq comment-end " */")
1337   (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
1338
1339   ;; Now define things to be fontified.
1340   (make-local-variable 'font-lock-keywords)
1341   (let ((c-keywords
1342          (mdw-regexps "break" "case" "cd" "continue" "define" "default"
1343                       "do" "else" "exit" "for" "global" "goto" "help" "if"
1344                       "local" "mat" "obj" "print" "quit" "read" "return"
1345                       "show" "static" "switch" "while" "write")))
1346
1347     (setq font-lock-keywords
1348           (list
1349
1350            ;; Handle the keywords defined above.
1351            (list (concat "\\<\\(" c-keywords "\\)\\>")
1352                  '(0 font-lock-keyword-face))
1353
1354            ;; Handle numbers too.
1355            ;;
1356            ;; This looks strange, I know.  It corresponds to the
1357            ;; preprocessor's idea of what a number looks like, rather than
1358            ;; anything sensible.
1359            (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
1360                          "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
1361                  '(0 mdw-number-face))
1362
1363            ;; And anything else is punctuation.
1364            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1365                  '(0 mdw-punct-face)))))
1366
1367   (mdw-post-config-mode-hack))
1368
1369 ;;;--------------------------------------------------------------------------
1370 ;;; Java programming configuration.
1371
1372 ;; Make indentation nice.
1373
1374 (defun mdw-java-style ()
1375   (c-add-style "[mdw] Java style"
1376                '((c-basic-offset . 2)
1377                  (c-offsets-alist (substatement-open . 0)
1378                                   (label . +)
1379                                   (case-label . +)
1380                                   (access-label . 0)
1381                                   (inclass . +)
1382                                   (statement-case-intro . +)))
1383                t))
1384
1385 ;; Declare Java fontification style.
1386
1387 (defun mdw-fontify-java ()
1388
1389   ;; Other stuff.
1390   (mdw-java-style)
1391   (setq c-hanging-comment-ender-p nil)
1392   (setq c-backslash-column 72)
1393   (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
1394
1395   ;; Now define things to be fontified.
1396   (make-local-variable 'font-lock-keywords)
1397   (let ((java-keywords
1398          (mdw-regexps "abstract" "boolean" "break" "byte" "case" "catch"
1399                       "char" "class" "const" "continue" "default" "do"
1400                       "double" "else" "extends" "final" "finally" "float"
1401                       "for" "goto" "if" "implements" "import" "instanceof"
1402                       "int" "interface" "long" "native" "new" "package"
1403                       "private" "protected" "public" "return" "short"
1404                       "static" "super" "switch" "synchronized" "this"
1405                       "throw" "throws" "transient" "try" "void" "volatile"
1406                       "while"
1407
1408                       "false" "null" "true")))
1409
1410     (setq font-lock-keywords
1411           (list
1412
1413            ;; Handle the keywords defined above.
1414            (list (concat "\\<\\(" java-keywords "\\)\\>")
1415                  '(0 font-lock-keyword-face))
1416
1417            ;; Handle numbers too.
1418            ;;
1419            ;; The following isn't quite right, but it's close enough.
1420            (list (concat "\\<\\("
1421                          "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
1422                          "[0-9]+\\(\\.[0-9]*\\|\\)"
1423                          "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
1424                          "[lLfFdD]?")
1425                  '(0 mdw-number-face))
1426
1427            ;; And anything else is punctuation.
1428            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1429                  '(0 mdw-punct-face)))))
1430
1431   (mdw-post-config-mode-hack))
1432
1433 ;;;--------------------------------------------------------------------------
1434 ;;; Javascript programming configuration.
1435
1436 (defun mdw-javascript-style ()
1437   (setq js-indent-level 2)
1438   (setq js-expr-indent-offset 0))
1439
1440 (defun mdw-fontify-javascript ()
1441
1442   ;; Other stuff.
1443   (mdw-javascript-style)
1444   (setq js-auto-indent-flag t)
1445
1446   ;; Now define things to be fontified.
1447   (make-local-variable 'font-lock-keywords)
1448   (let ((javascript-keywords
1449          (mdw-regexps "abstract" "boolean" "break" "byte" "case" "catch"
1450                       "char" "class" "const" "continue" "debugger" "default"
1451                       "delete" "do" "double" "else" "enum" "export" "extends"
1452                       "final" "finally" "float" "for" "function" "goto" "if"
1453                       "implements" "import" "in" "instanceof" "int"
1454                       "interface" "let" "long" "native" "new" "package"
1455                       "private" "protected" "public" "return" "short"
1456                       "static" "super" "switch" "synchronized" "throw"
1457                       "throws" "transient" "try" "typeof" "var" "void"
1458                       "volatile" "while" "with" "yield"
1459
1460                       "boolean" "byte" "char" "double" "float" "int" "long"
1461                       "short" "void"))
1462         (javascript-constants
1463          (mdw-regexps "false" "null" "undefined" "Infinity" "NaN" "true"
1464                       "arguments" "this")))
1465
1466     (setq font-lock-keywords
1467           (list
1468
1469            ;; Handle the keywords defined above.
1470            (list (concat "\\_<\\(" javascript-keywords "\\)\\_>")
1471                  '(0 font-lock-keyword-face))
1472
1473            ;; Handle the predefined constants defined above.
1474            (list (concat "\\_<\\(" javascript-constants "\\)\\_>")
1475                  '(0 font-lock-variable-name-face))
1476
1477            ;; Handle numbers too.
1478            ;;
1479            ;; The following isn't quite right, but it's close enough.
1480            (list (concat "\\_<\\("
1481                          "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
1482                          "[0-9]+\\(\\.[0-9]*\\|\\)"
1483                          "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
1484                          "[lLfFdD]?")
1485                  '(0 mdw-number-face))
1486
1487            ;; And anything else is punctuation.
1488            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1489                  '(0 mdw-punct-face)))))
1490
1491   (mdw-post-config-mode-hack))
1492
1493 ;;;--------------------------------------------------------------------------
1494 ;;; Scala programming configuration.
1495
1496 (defun mdw-fontify-scala ()
1497
1498   ;; Define things to be fontified.
1499   (make-local-variable 'font-lock-keywords)
1500   (let ((scala-keywords
1501          (mdw-regexps "abstract" "case" "catch" "class" "def" "do" "else"
1502                       "extends" "final" "finally" "for" "forSome" "if"
1503                       "implicit" "import" "lazy" "match" "new" "object"
1504                       "override" "package" "protected" "return" "sealed"
1505                       "super" "this" "throw" "trait" "try" "type" "val"
1506                       "var" "while" "with" "yield"))
1507         (scala-constants
1508          (mdw-regexps "false" "null" "true"))
1509         (punctuation "\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/"))
1510
1511     (setq font-lock-keywords
1512           (list
1513
1514            ;; Magical identifiers between backticks.
1515            (list (concat "`\\([^`]+\\)`")
1516                  '(1 font-lock-variable-name-face))
1517
1518            ;; Handle the keywords defined above.
1519            (list (concat "\\_<\\(" scala-keywords "\\)\\_>")
1520                  '(0 font-lock-keyword-face))
1521
1522            ;; Handle the constants defined above.
1523            (list (concat "\\_<\\(" scala-constants "\\)\\_>")
1524                  '(0 font-lock-variable-name-face))
1525
1526            ;; Magical identifiers between backticks.
1527            (list (concat "`\\([^`]+\\)`")
1528                  '(1 font-lock-variable-name-face))
1529
1530            ;; Handle numbers too.
1531            ;;
1532            ;; As usual, not quite right.
1533            (list (concat "\\_<\\("
1534                          "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
1535                          "[0-9]+\\(\\.[0-9]*\\|\\)"
1536                          "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
1537                          "[lLfFdD]?")
1538                  '(0 mdw-number-face))
1539
1540            ;; Identifiers with trailing operators.
1541            (list (concat "_\\(" punctuation "\\)+")
1542                  '(0 mdw-trivial-face))
1543
1544            ;; And everything else is punctuation.
1545            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1546                  '(0 mdw-punct-face)))
1547
1548           font-lock-syntactic-keywords
1549           (list
1550
1551            ;; Single quotes around characters.  But not when used to quote
1552            ;; symbol names.  Ugh.
1553            (list (concat "\\('\\)"
1554                          "\\(" "."
1555                          "\\|" "\\\\" "\\(" "\\\\\\\\" "\\)*"
1556                                "u+" "[0-9a-fA-F]\\{4\\}"
1557                          "\\|" "\\\\" "[0-7]\\{1,3\\}"
1558                          "\\|" "\\\\" "." "\\)"
1559                          "\\('\\)")
1560                  '(1 "\"")
1561                  '(4 "\"")))))
1562
1563   (mdw-post-config-mode-hack))
1564
1565 ;;;--------------------------------------------------------------------------
1566 ;;; C# programming configuration.
1567
1568 ;; Make indentation nice.
1569
1570 (defun mdw-csharp-style ()
1571   (c-add-style "[mdw] C# style"
1572                '((c-basic-offset . 2)
1573                  (c-offsets-alist (substatement-open . 0)
1574                                   (label . 0)
1575                                   (case-label . +)
1576                                   (access-label . 0)
1577                                   (inclass . +)
1578                                   (statement-case-intro . +)))
1579                t))
1580
1581 ;; Declare C# fontification style.
1582
1583 (defun mdw-fontify-csharp ()
1584
1585   ;; Other stuff.
1586   (mdw-csharp-style)
1587   (setq c-hanging-comment-ender-p nil)
1588   (setq c-backslash-column 72)
1589   (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
1590
1591   ;; Now define things to be fontified.
1592   (make-local-variable 'font-lock-keywords)
1593   (let ((csharp-keywords
1594          (mdw-regexps "abstract" "as" "base" "bool" "break"
1595                       "byte" "case" "catch" "char" "checked"
1596                       "class" "const" "continue" "decimal" "default"
1597                       "delegate" "do" "double" "else" "enum"
1598                       "event" "explicit" "extern" "false" "finally"
1599                       "fixed" "float" "for" "foreach" "goto"
1600                       "if" "implicit" "in" "int" "interface"
1601                       "internal" "is" "lock" "long" "namespace"
1602                       "new" "null" "object" "operator" "out"
1603                       "override" "params" "private" "protected" "public"
1604                       "readonly" "ref" "return" "sbyte" "sealed"
1605                       "short" "sizeof" "stackalloc" "static" "string"
1606                       "struct" "switch" "this" "throw" "true"
1607                       "try" "typeof" "uint" "ulong" "unchecked"
1608                       "unsafe" "ushort" "using" "virtual" "void"
1609                       "volatile" "while" "yield")))
1610
1611     (setq font-lock-keywords
1612           (list
1613
1614            ;; Handle the keywords defined above.
1615            (list (concat "\\<\\(" csharp-keywords "\\)\\>")
1616                  '(0 font-lock-keyword-face))
1617
1618            ;; Handle numbers too.
1619            ;;
1620            ;; The following isn't quite right, but it's close enough.
1621            (list (concat "\\<\\("
1622                          "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
1623                          "[0-9]+\\(\\.[0-9]*\\|\\)"
1624                          "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
1625                          "[lLfFdD]?")
1626                  '(0 mdw-number-face))
1627
1628            ;; And anything else is punctuation.
1629            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1630                  '(0 mdw-punct-face)))))
1631
1632   (mdw-post-config-mode-hack))
1633
1634 (define-derived-mode csharp-mode java-mode "C#"
1635   "Major mode for editing C# code.")
1636
1637 ;;;--------------------------------------------------------------------------
1638 ;;; F# programming configuration.
1639
1640 (setq fsharp-indent-offset 2)
1641
1642 (defun mdw-fontify-fsharp ()
1643
1644   (let ((punct "=<>+-*/|&%!@?"))
1645     (do ((i 0 (1+ i)))
1646         ((>= i (length punct)))
1647       (modify-syntax-entry (aref punct i) ".")))
1648
1649   (modify-syntax-entry ?_ "_")
1650   (modify-syntax-entry ?( "(")
1651   (modify-syntax-entry ?) ")")
1652
1653   (setq indent-tabs-mode nil)
1654
1655   (let ((fsharp-keywords
1656          (mdw-regexps "abstract" "and" "as" "assert" "atomic"
1657                       "base" "begin" "break"
1658                       "checked" "class" "component" "const" "constraint"
1659                       "constructor" "continue"
1660                       "default" "delegate" "do" "done" "downcast" "downto"
1661                       "eager" "elif" "else" "end" "exception" "extern"
1662                       "false" "finally" "fixed" "for" "fori" "fun" "function"
1663                       "functor"
1664                       "global"
1665                       "if" "in" "include" "inherit" "inline" "interface"
1666                       "internal"
1667                       "lazy" "let"
1668                       "match" "measure" "member" "method" "mixin" "module"
1669                       "mutable"
1670                       "namespace" "new" "null"
1671                       "object""of" "open" "or" "override"
1672                       "parallel" "params" "private" "process" "protected"
1673                       "public" "pure"
1674                       "rec" "recursive" "return"
1675                       "sealed" "sig" "static" "struct"
1676                       "tailcall" "then" "to" "trait" "true" "try" "type"
1677                       "upcast" "use"
1678                       "val" "virtual" "void" "volatile"
1679                       "when" "while" "with"
1680                       "yield"))
1681
1682         (fsharp-builtins
1683          (mdw-regexps "asr" "land" "lor" "lsl" "lsr" "lxor" "mod"))
1684
1685         (bang-keywords
1686          (mdw-regexps "do" "let" "return" "use" "yield"))
1687
1688         (preprocessor-keywords
1689          (mdw-regexps "if" "indent" "else" "endif")))
1690
1691     (setq font-lock-keywords
1692           (list (list (concat "\\(^\\|[^\"]\\)"
1693                               "\\(" "(\\*"
1694                                     "[^*]*\\*+"
1695                                     "\\(" "[^)*]" "[^*]*" "\\*+" "\\)*"
1696                                     ")"
1697                               "\\|"
1698                                     "//.*"
1699                               "\\)")
1700                       '(2 font-lock-comment-face))
1701
1702                 (list (concat "'" "\\("
1703                                     "\\\\"
1704                                     "\\(" "[ntbr'\\]"
1705                                     "\\|" "[0-9][0-9][0-9]"
1706                                     "\\|" "u" "[0-9a-fA-F]\\{4\\}"
1707                                     "\\|" "U" "[0-9a-fA-F]\\{8\\}"
1708                                     "\\)"
1709                                   "\\|"
1710                                   "." "\\)" "'"
1711                               "\\|"
1712                               "\"" "[^\"\\]*"
1713                                     "\\(" "\\\\" "\\(.\\|\n\\)"
1714                                           "[^\"\\]*" "\\)*"
1715                               "\\(\"\\|\\'\\)")
1716                       '(0 font-lock-string-face))
1717
1718                 (list (concat "\\_<\\(" bang-keywords "\\)!" "\\|"
1719                               "^#[ \t]*\\(" preprocessor-keywords "\\)\\_>"
1720                               "\\|"
1721                               "\\_<\\(" fsharp-keywords "\\)\\_>")
1722                       '(0 font-lock-keyword-face))
1723                 (list (concat "\\<\\(" fsharp-builtins "\\)\\_>")
1724                       '(0 font-lock-variable-name-face))
1725
1726                 (list (concat "\\_<"
1727                               "\\(" "0[bB][01]+" "\\|"
1728                                     "0[oO][0-7]+" "\\|"
1729                                     "0[xX][0-9a-fA-F]+" "\\)"
1730                               "\\(" "lf\\|LF" "\\|"
1731                                     "[uU]?[ysnlL]?" "\\)"
1732                               "\\|"
1733                               "\\_<"
1734                               "[0-9]+" "\\("
1735                                 "[mMQRZING]"
1736                                 "\\|"
1737                                 "\\(\\.[0-9]*\\)?"
1738                                 "\\([eE][-+]?[0-9]+\\)?"
1739                                 "[fFmM]?"
1740                                 "\\|"
1741                                 "[uU]?[ysnlL]?"
1742                               "\\)")
1743                       '(0 mdw-number-face))
1744
1745                 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1746                       '(0 mdw-punct-face)))))
1747
1748   (mdw-post-config-mode-hack))
1749
1750 (defun mdw-fontify-inferior-fsharp ()
1751   (mdw-fontify-fsharp)
1752   (setq font-lock-keywords
1753         (append (list (list "^[#-]" '(0 font-lock-comment-face))
1754                       (list "^>" '(0 font-lock-keyword-face)))
1755                 font-lock-keywords)))
1756
1757 ;;;--------------------------------------------------------------------------
1758 ;;; Go programming configuration.
1759
1760 (defun mdw-fontify-go ()
1761
1762   (make-local-variable 'font-lock-keywords)
1763   (let ((go-keywords
1764          (mdw-regexps "break" "case" "chan" "const" "continue"
1765                       "default" "defer" "else" "fallthrough" "for"
1766                       "func" "go" "goto" "if" "import"
1767                       "interface" "map" "package" "range" "return"
1768                       "select" "struct" "switch" "type" "var"))
1769         (go-intrinsics
1770          (mdw-regexps "bool" "byte" "complex64" "complex128" "error"
1771                       "float32" "float64" "int" "uint8" "int16" "int32"
1772                       "int64" "rune" "string" "uint" "uint8" "uint16"
1773                       "uint32" "uint64" "uintptr" "void"
1774                       "false" "iota" "nil" "true"
1775                       "init" "main"
1776                       "append" "cap" "copy" "delete" "imag" "len" "make"
1777                       "new" "panic" "real" "recover")))
1778
1779     (setq font-lock-keywords
1780           (list
1781
1782            ;; Handle the keywords defined above.
1783            (list (concat "\\<\\(" go-keywords "\\)\\>")
1784                  '(0 font-lock-keyword-face))
1785            (list (concat "\\<\\(" go-intrinsics "\\)\\>")
1786                  '(0 font-lock-variable-name-face))
1787
1788            ;; Strings and characters.
1789            (list (concat "'"
1790                          "\\(" "[^\\']" "\\|"
1791                                "\\\\"
1792                                "\\(" "[abfnrtv\\'\"]" "\\|"
1793                                      "[0-7]\\{3\\}" "\\|"
1794                                      "x" "[0-9A-Fa-f]\\{2\\}" "\\|"
1795                                      "u" "[0-9A-Fa-f]\\{4\\}" "\\|"
1796                                      "U" "[0-9A-Fa-f]\\{8\\}" "\\)" "\\)"
1797                          "'"
1798                          "\\|"
1799                          "\""
1800                          "\\(" "[^\n\\\"]+" "\\|" "\\\\." "\\)*"
1801                          "\\(\"\\|$\\)"
1802                          "\\|"
1803                          "`" "[^`]+" "`")
1804                  '(0 font-lock-string-face))
1805
1806            ;; Handle numbers too.
1807            ;;
1808            ;; The following isn't quite right, but it's close enough.
1809            (list (concat "\\<\\("
1810                          "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
1811                          "[0-9]+\\(\\.[0-9]*\\|\\)"
1812                          "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)")
1813                  '(0 mdw-number-face))
1814
1815            ;; And anything else is punctuation.
1816            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1817                  '(0 mdw-punct-face)))))
1818
1819   (mdw-post-config-mode-hack))
1820
1821 ;;;--------------------------------------------------------------------------
1822 ;;; Awk programming configuration.
1823
1824 ;; Make Awk indentation nice.
1825
1826 (defun mdw-awk-style ()
1827   (c-add-style "[mdw] Awk style"
1828                '((c-basic-offset . 2)
1829                  (c-offsets-alist (substatement-open . 0)
1830                                   (statement-cont . 0)
1831                                   (statement-case-intro . +)))
1832                t))
1833
1834 ;; Declare Awk fontification style.
1835
1836 (defun mdw-fontify-awk ()
1837
1838   ;; Miscellaneous fiddling.
1839   (mdw-awk-style)
1840   (setq c-backslash-column 72)
1841   (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
1842
1843   ;; Now define things to be fontified.
1844   (make-local-variable 'font-lock-keywords)
1845   (let ((c-keywords
1846          (mdw-regexps "BEGIN" "END" "ARGC" "ARGIND" "ARGV" "CONVFMT"
1847                       "ENVIRON" "ERRNO" "FIELDWIDTHS" "FILENAME" "FNR"
1848                       "FS" "IGNORECASE" "NF" "NR" "OFMT" "OFS" "ORS" "RS"
1849                       "RSTART" "RLENGTH" "RT"   "SUBSEP"
1850                       "atan2" "break" "close" "continue" "cos" "delete"
1851                       "do" "else" "exit" "exp" "fflush" "file" "for" "func"
1852                       "function" "gensub" "getline" "gsub" "if" "in"
1853                       "index" "int" "length" "log" "match" "next" "rand"
1854                       "return" "print" "printf" "sin" "split" "sprintf"
1855                       "sqrt" "srand" "strftime" "sub" "substr" "system"
1856                       "systime" "tolower" "toupper" "while")))
1857
1858     (setq font-lock-keywords
1859           (list
1860
1861            ;; Handle the keywords defined above.
1862            (list (concat "\\<\\(" c-keywords "\\)\\>")
1863                  '(0 font-lock-keyword-face))
1864
1865            ;; Handle numbers too.
1866            ;;
1867            ;; The following isn't quite right, but it's close enough.
1868            (list (concat "\\<\\("
1869                          "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
1870                          "[0-9]+\\(\\.[0-9]*\\|\\)"
1871                          "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
1872                          "[uUlL]*")
1873                  '(0 mdw-number-face))
1874
1875            ;; And anything else is punctuation.
1876            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1877                  '(0 mdw-punct-face)))))
1878
1879   (mdw-post-config-mode-hack))
1880
1881 ;;;--------------------------------------------------------------------------
1882 ;;; Perl programming style.
1883
1884 ;; Perl indentation style.
1885
1886 (fset 'perl-mode 'cperl-mode)
1887 (setq cperl-indent-level 2)
1888 (setq cperl-continued-statement-offset 2)
1889 (setq cperl-continued-brace-offset 0)
1890 (setq cperl-brace-offset -2)
1891 (setq cperl-brace-imaginary-offset 0)
1892 (setq cperl-label-offset 0)
1893
1894 ;; Define perl fontification style.
1895
1896 (defun mdw-fontify-perl ()
1897
1898   ;; Miscellaneous fiddling.
1899   (modify-syntax-entry ?$ "\\")
1900   (modify-syntax-entry ?$ "\\" font-lock-syntax-table)
1901   (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
1902
1903   ;; Now define fontification things.
1904   (make-local-variable 'font-lock-keywords)
1905   (let ((perl-keywords
1906          (mdw-regexps "and" "break" "cmp" "continue" "do" "else" "elsif" "eq"
1907                       "for" "foreach" "ge" "given" "gt" "goto" "if"
1908                       "last" "le" "lt" "local" "my" "ne" "next" "or"
1909                       "our" "package" "redo" "require" "return" "sub"
1910                       "undef" "unless" "until" "use" "when" "while")))
1911
1912     (setq font-lock-keywords
1913           (list
1914
1915            ;; Set up the keywords defined above.
1916            (list (concat "\\<\\(" perl-keywords "\\)\\>")
1917                  '(0 font-lock-keyword-face))
1918
1919            ;; At least numbers are simpler than C.
1920            (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
1921                          "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)"
1922                          "\\([eE]\\([-+]\\|\\)[0-9_]+\\|\\)")
1923                  '(0 mdw-number-face))
1924
1925            ;; And anything else is punctuation.
1926            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1927                  '(0 mdw-punct-face)))))
1928
1929   (mdw-post-config-mode-hack))
1930
1931 (defun perl-number-tests (&optional arg)
1932   "Assign consecutive numbers to lines containing `#t'.  With ARG,
1933 strip numbers instead."
1934   (interactive "P")
1935   (save-excursion
1936     (goto-char (point-min))
1937     (let ((i 0) (fmt (if arg "" " %4d")))
1938       (while (search-forward "#t" nil t)
1939         (delete-region (point) (line-end-position))
1940         (setq i (1+ i))
1941         (insert (format fmt i)))
1942       (goto-char (point-min))
1943       (if (re-search-forward "\\(tests\\s-*=>\\s-*\\)\\w*" nil t)
1944           (replace-match (format "\\1%d" i))))))
1945
1946 ;;;--------------------------------------------------------------------------
1947 ;;; Python programming style.
1948
1949 (defun mdw-fontify-pythonic (keywords)
1950
1951   ;; Miscellaneous fiddling.
1952   (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
1953   (setq indent-tabs-mode nil)
1954
1955   ;; Now define fontification things.
1956   (make-local-variable 'font-lock-keywords)
1957   (setq font-lock-keywords
1958         (list
1959
1960          ;; Set up the keywords defined above.
1961          (list (concat "\\_<\\(" keywords "\\)\\_>")
1962                '(0 font-lock-keyword-face))
1963
1964          ;; At least numbers are simpler than C.
1965          (list (concat "\\_<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
1966                        "\\_<[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)"
1967                        "\\([eE]\\([-+]\\|\\)[0-9_]+\\|[lL]\\|\\)")
1968                '(0 mdw-number-face))
1969
1970          ;; And anything else is punctuation.
1971          (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1972                '(0 mdw-punct-face))))
1973
1974   (mdw-post-config-mode-hack))
1975
1976 ;; Define Python fontification styles.
1977
1978 (defun mdw-fontify-python ()
1979   (mdw-fontify-pythonic
1980    (mdw-regexps "and" "as" "assert" "break" "class" "continue" "def"
1981                 "del" "elif" "else" "except" "exec" "finally" "for"
1982                 "from" "global" "if" "import" "in" "is" "lambda"
1983                 "not" "or" "pass" "print" "raise" "return" "try"
1984                 "while" "with" "yield")))
1985
1986 (defun mdw-fontify-pyrex ()
1987   (mdw-fontify-pythonic
1988    (mdw-regexps "and" "as" "assert" "break" "cdef" "class" "continue"
1989                 "ctypedef" "def" "del" "elif" "else" "except" "exec"
1990                 "extern" "finally" "for" "from" "global" "if"
1991                 "import" "in" "is" "lambda" "not" "or" "pass" "print"
1992                 "raise" "return" "struct" "try" "while" "with"
1993                 "yield")))
1994
1995 ;;;--------------------------------------------------------------------------
1996 ;;; Icon programming style.
1997
1998 ;; Icon indentation style.
1999
2000 (setq icon-brace-offset 0
2001       icon-continued-brace-offset 0
2002       icon-continued-statement-offset 2
2003       icon-indent-level 2)
2004
2005 ;; Define Icon fontification style.
2006
2007 (defun mdw-fontify-icon ()
2008
2009   ;; Miscellaneous fiddling.
2010   (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
2011
2012   ;; Now define fontification things.
2013   (make-local-variable 'font-lock-keywords)
2014   (let ((icon-keywords
2015          (mdw-regexps "break" "by" "case" "create" "default" "do" "else"
2016                       "end" "every" "fail" "global" "if" "initial"
2017                       "invocable" "link" "local" "next" "not" "of"
2018                       "procedure" "record" "repeat" "return" "static"
2019                       "suspend" "then" "to" "until" "while"))
2020         (preprocessor-keywords
2021          (mdw-regexps "define" "else" "endif" "error" "ifdef" "ifndef"
2022                       "include" "line" "undef")))
2023     (setq font-lock-keywords
2024           (list
2025
2026            ;; Set up the keywords defined above.
2027            (list (concat "\\<\\(" icon-keywords "\\)\\>")
2028                  '(0 font-lock-keyword-face))
2029
2030            ;; The things that Icon calls keywords.
2031            (list "&\\sw+\\>" '(0 font-lock-variable-name-face))
2032
2033            ;; At least numbers are simpler than C.
2034            (list (concat "\\<[0-9]+"
2035                          "\\([rR][0-9a-zA-Z]+\\|"
2036                          "\\.[0-9]+\\([eE][+-]?[0-9]+\\)?\\)\\>\\|"
2037                          "\\.[0-9]+\\([eE][+-]?[0-9]+\\)?\\>")
2038                  '(0 mdw-number-face))
2039
2040            ;; Preprocessor.
2041            (list (concat "^[ \t]*$[ \t]*\\<\\("
2042                          preprocessor-keywords
2043                          "\\)\\>")
2044                  '(0 font-lock-keyword-face))
2045
2046            ;; And anything else is punctuation.
2047            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2048                  '(0 mdw-punct-face)))))
2049
2050   (mdw-post-config-mode-hack))
2051
2052 ;;;--------------------------------------------------------------------------
2053 ;;; ARM assembler programming configuration.
2054
2055 ;; There doesn't appear to be an Emacs mode for this yet.
2056 ;;
2057 ;; Better do something about that, I suppose.
2058
2059 (defvar arm-assembler-mode-map nil)
2060 (defvar arm-assembler-abbrev-table nil)
2061 (defvar arm-assembler-mode-syntax-table (make-syntax-table))
2062
2063 (or arm-assembler-mode-map
2064     (progn
2065       (setq arm-assembler-mode-map (make-sparse-keymap))
2066       (define-key arm-assembler-mode-map "\C-m" 'arm-assembler-newline)
2067       (define-key arm-assembler-mode-map [C-return] 'newline)
2068       (define-key arm-assembler-mode-map "\t" 'tab-to-tab-stop)))
2069
2070 (defun arm-assembler-mode ()
2071   "Major mode for ARM assembler programs"
2072   (interactive)
2073
2074   ;; Do standard major mode things.
2075   (kill-all-local-variables)
2076   (use-local-map arm-assembler-mode-map)
2077   (setq local-abbrev-table arm-assembler-abbrev-table)
2078   (setq major-mode 'arm-assembler-mode)
2079   (setq mode-name "ARM assembler")
2080
2081   ;; Set up syntax table.
2082   (set-syntax-table arm-assembler-mode-syntax-table)
2083   (modify-syntax-entry ?;   ; Nasty hack
2084                        "<" arm-assembler-mode-syntax-table)
2085   (modify-syntax-entry ?\n ">" arm-assembler-mode-syntax-table)
2086   (modify-syntax-entry ?_ "_" arm-assembler-mode-syntax-table)
2087   (modify-syntax-entry ?' "\"'" arm-assembler-mode-syntax-table)
2088
2089   (make-local-variable 'comment-start)
2090   (setq comment-start ";")
2091   (make-local-variable 'comment-end)
2092   (setq comment-end "")
2093   (make-local-variable 'comment-column)
2094   (setq comment-column 48)
2095   (make-local-variable 'comment-start-skip)
2096   (setq comment-start-skip ";+[ \t]*")
2097
2098   ;; Play with indentation.
2099   (make-local-variable 'indent-line-function)
2100   (setq indent-line-function 'indent-relative-maybe)
2101
2102   ;; Set fill prefix.
2103   (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)")
2104
2105   ;; Fiddle with fontification.
2106   (make-local-variable 'font-lock-keywords)
2107   (setq font-lock-keywords
2108         (list
2109
2110          ;; Handle numbers too.
2111          ;;
2112          ;; The following isn't quite right, but it's close enough.
2113          (list (concat "\\("
2114                        "&[0-9a-fA-F]+\\|"
2115                        "\\<[0-9]+\\(\\.[0-9]*\\|_[0-9a-zA-Z]+\\|\\)"
2116                        "\\)")
2117                '(0 mdw-number-face))
2118
2119          ;; Do something about operators.
2120          (list "^[^ \t]*[ \t]+\\(GET\\|LNK\\)[ \t]+\\([^;\n]*\\)"
2121                '(1 font-lock-keyword-face)
2122                '(2 font-lock-string-face))
2123          (list ":[a-zA-Z]+:"
2124                '(0 font-lock-keyword-face))
2125
2126          ;; Do menemonics and directives.
2127          (list "^[^ \t]*[ \t]+\\([a-zA-Z]+\\)"
2128                '(1 font-lock-keyword-face))
2129
2130          ;; And anything else is punctuation.
2131          (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2132                '(0 mdw-punct-face))))
2133
2134   (mdw-post-config-mode-hack)
2135   (run-hooks 'arm-assembler-mode-hook))
2136
2137 ;;;--------------------------------------------------------------------------
2138 ;;; Assembler mode.
2139
2140 (defun mdw-fontify-asm ()
2141   (modify-syntax-entry ?' "\"")
2142   (modify-syntax-entry ?. "w")
2143   (setf fill-prefix nil)
2144   (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)"))
2145
2146 ;;;--------------------------------------------------------------------------
2147 ;;; TCL configuration.
2148
2149 (defun mdw-fontify-tcl ()
2150   (mapcar #'(lambda (ch) (modify-syntax-entry ch ".")) '(?$))
2151   (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
2152   (make-local-variable 'font-lock-keywords)
2153   (setq font-lock-keywords
2154         (list
2155          (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
2156                        "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)"
2157                        "\\([eE]\\([-+]\\|\\)[0-9_]+\\|\\)")
2158                '(0 mdw-number-face))
2159          (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2160                '(0 mdw-punct-face))))
2161   (mdw-post-config-mode-hack))
2162
2163 ;;;--------------------------------------------------------------------------
2164 ;;; Dylan programming configuration.
2165
2166 (defun mdw-fontify-dylan ()
2167
2168   (make-local-variable 'font-lock-keywords)
2169
2170   ;; Horrors.  `dylan-mode' sets the `major-mode' name after calling this
2171   ;; hook, which undoes all of our configuration.
2172   (setq major-mode 'dylan-mode)
2173   (font-lock-set-defaults)
2174
2175   (let* ((word "[-_a-zA-Z!*@<>$%]+")
2176          (dylan-keywords (mdw-regexps
2177
2178                           "C-address" "C-callable-wrapper" "C-function"
2179                           "C-mapped-subtype" "C-pointer-type" "C-struct"
2180                           "C-subtype" "C-union" "C-variable"
2181
2182                           "above" "abstract" "afterwards" "all"
2183                           "begin" "below" "block" "by"
2184                           "case" "class" "cleanup" "constant" "create"
2185                           "define" "domain"
2186                           "else" "elseif" "end" "exception" "export"
2187                           "finally" "for" "from" "function"
2188                           "generic"
2189                           "handler"
2190                           "if" "in" "instance" "interface" "iterate"
2191                           "keyed-by"
2192                           "let" "library" "local"
2193                           "macro" "method" "module"
2194                           "otherwise"
2195                           "profiling"
2196                           "select" "slot" "subclass"
2197                           "table" "then" "to"
2198                           "unless" "until" "use"
2199                           "variable" "virtual"
2200                           "when" "while"))
2201          (sharp-keywords (mdw-regexps
2202                           "all-keys" "key" "next" "rest" "include"
2203                           "t" "f")))
2204     (setq font-lock-keywords
2205           (list (list (concat "\\<\\(" dylan-keywords
2206                               "\\|" "with\\(out\\)?-" word
2207                               "\\)\\>")
2208                       '(0 font-lock-keyword-face))
2209                 (list (concat "\\<" word ":" "\\|"
2210                               "#\\(" sharp-keywords "\\)\\>")
2211                       '(0 font-lock-variable-name-face))
2212                 (list (concat "\\("
2213                               "\\([-+]\\|\\<\\)[0-9]+" "\\("
2214                                 "\\(\\.[0-9]+\\)?" "\\([eE][-+][0-9]+\\)?"
2215                                 "\\|" "/[0-9]+"
2216                               "\\)"
2217                               "\\|" "\\.[0-9]+" "\\([eE][-+][0-9]+\\)?"
2218                               "\\|" "#b[01]+"
2219                               "\\|" "#o[0-7]+"
2220                               "\\|" "#x[0-9a-zA-Z]+"
2221                               "\\)\\>")
2222                       '(0 mdw-number-face))
2223                 (list (concat "\\("
2224                               "\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\|"
2225                               "\\_<[-+*/=<>:&|]+\\_>"
2226                               "\\)")
2227                       '(0 mdw-punct-face)))))
2228
2229   (mdw-post-config-mode-hack))
2230
2231 ;;;--------------------------------------------------------------------------
2232 ;;; Algol 68 configuration.
2233
2234 (setq a68-indent-step 2)
2235
2236 (defun mdw-fontify-algol-68 ()
2237
2238   ;; Fix up the syntax table.
2239   (modify-syntax-entry ?# "!" a68-mode-syntax-table)
2240   (dolist (ch '(?- ?+ ?= ?< ?> ?* ?/ ?| ?&))
2241     (modify-syntax-entry ch "." a68-mode-syntax-table))
2242
2243   (make-local-variable 'font-lock-keywords)
2244
2245   (let ((not-comment
2246          (let ((word "COMMENT"))
2247            (do ((regexp (concat "[^" (substring word 0 1) "]+")
2248                         (concat regexp "\\|"
2249                                 (substring word 0 i)
2250                                 "[^" (substring word i (1+ i)) "]"))
2251                 (i 1 (1+ i)))
2252                ((>= i (length word)) regexp)))))
2253     (setq font-lock-keywords
2254           (list (list (concat "\\<COMMENT\\>"
2255                               "\\(" not-comment "\\)\\{0,5\\}"
2256                               "\\(\\'\\|\\<COMMENT\\>\\)")
2257                       '(0 font-lock-comment-face))
2258                 (list (concat "\\<CO\\>"
2259                               "\\([^C]+\\|C[^O]\\)\\{0,5\\}"
2260                               "\\($\\|\\<CO\\>\\)")
2261                       '(0 font-lock-comment-face))
2262                 (list "\\<[A-Z_]+\\>"
2263                       '(0 font-lock-keyword-face))
2264                 (list (concat "\\<"
2265                               "[0-9]+"
2266                               "\\(\\.[0-9]+\\)?"
2267                               "\\([eE][-+]?[0-9]+\\)?"
2268                               "\\>")
2269                       '(0 mdw-number-face))
2270                 (list "\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/"
2271                       '(0 mdw-punct-face)))))
2272
2273   (mdw-post-config-mode-hack))
2274
2275 ;;;--------------------------------------------------------------------------
2276 ;;; REXX configuration.
2277
2278 (defun mdw-rexx-electric-* ()
2279   (interactive)
2280   (insert ?*)
2281   (rexx-indent-line))
2282
2283 (defun mdw-rexx-indent-newline-indent ()
2284   (interactive)
2285   (rexx-indent-line)
2286   (if abbrev-mode (expand-abbrev))
2287   (newline-and-indent))
2288
2289 (defun mdw-fontify-rexx ()
2290
2291   ;; Various bits of fiddling.
2292   (setq mdw-auto-indent nil)
2293   (local-set-key [?\C-m] 'mdw-rexx-indent-newline-indent)
2294   (local-set-key [?*] 'mdw-rexx-electric-*)
2295   (mapcar #'(lambda (ch) (modify-syntax-entry ch "w"))
2296           '(?! ?? ?# ?@ ?$))
2297   (mdw-standard-fill-prefix "\\([ \t]*/?\*[ \t]*\\)")
2298
2299   ;; Set up keywords and things for fontification.
2300   (make-local-variable 'font-lock-keywords-case-fold-search)
2301   (setq font-lock-keywords-case-fold-search t)
2302
2303   (setq rexx-indent 2)
2304   (setq rexx-end-indent rexx-indent)
2305   (setq rexx-cont-indent rexx-indent)
2306
2307   (make-local-variable 'font-lock-keywords)
2308   (let ((rexx-keywords
2309          (mdw-regexps "address" "arg" "by" "call" "digits" "do" "drop"
2310                       "else" "end" "engineering" "exit" "expose" "for"
2311                       "forever" "form" "fuzz" "if" "interpret" "iterate"
2312                       "leave" "linein" "name" "nop" "numeric" "off" "on"
2313                       "options" "otherwise" "parse" "procedure" "pull"
2314                       "push" "queue" "return" "say" "select" "signal"
2315                       "scientific" "source" "then" "trace" "to" "until"
2316                       "upper" "value" "var" "version" "when" "while"
2317                       "with"
2318
2319                       "abbrev" "abs" "bitand" "bitor" "bitxor" "b2x"
2320                       "center" "center" "charin" "charout" "chars"
2321                       "compare" "condition" "copies" "c2d" "c2x"
2322                       "datatype" "date" "delstr" "delword" "d2c" "d2x"
2323                       "errortext" "format" "fuzz" "insert" "lastpos"
2324                       "left" "length" "lineout" "lines" "max" "min"
2325                       "overlay" "pos" "queued" "random" "reverse" "right"
2326                       "sign" "sourceline" "space" "stream" "strip"
2327                       "substr" "subword" "symbol" "time" "translate"
2328                       "trunc" "value" "verify" "word" "wordindex"
2329                       "wordlength" "wordpos" "words" "xrange" "x2b" "x2c"
2330                       "x2d")))
2331
2332     (setq font-lock-keywords
2333           (list
2334
2335            ;; Set up the keywords defined above.
2336            (list (concat "\\<\\(" rexx-keywords "\\)\\>")
2337                  '(0 font-lock-keyword-face))
2338
2339            ;; Fontify all symbols the same way.
2340            (list (concat "\\<\\([0-9.][A-Za-z0-9.!?_#@$]*[Ee][+-]?[0-9]+\\|"
2341                          "[A-Za-z0-9.!?_#@$]+\\)")
2342                  '(0 font-lock-variable-name-face))
2343
2344            ;; And everything else is punctuation.
2345            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2346                  '(0 mdw-punct-face)))))
2347
2348   (mdw-post-config-mode-hack))
2349
2350 ;;;--------------------------------------------------------------------------
2351 ;;; Standard ML programming style.
2352
2353 (defun mdw-fontify-sml ()
2354
2355   ;; Make underscore an honorary letter.
2356   (modify-syntax-entry ?' "w")
2357
2358   ;; Set fill prefix.
2359   (mdw-standard-fill-prefix "\\([ \t]*(\*[ \t]*\\)")
2360
2361   ;; Now define fontification things.
2362   (make-local-variable 'font-lock-keywords)
2363   (let ((sml-keywords
2364          (mdw-regexps "abstype" "and" "andalso" "as"
2365                       "case"
2366                       "datatype" "do"
2367                       "else" "end" "eqtype" "exception"
2368                       "fn" "fun" "functor"
2369                       "handle"
2370                       "if" "in" "include" "infix" "infixr"
2371                       "let" "local"
2372                       "nonfix"
2373                       "of" "op" "open" "orelse"
2374                       "raise" "rec"
2375                       "sharing" "sig" "signature" "struct" "structure"
2376                       "then" "type"
2377                       "val"
2378                       "where" "while" "with" "withtype")))
2379
2380     (setq font-lock-keywords
2381           (list
2382
2383            ;; Set up the keywords defined above.
2384            (list (concat "\\<\\(" sml-keywords "\\)\\>")
2385                  '(0 font-lock-keyword-face))
2386
2387            ;; At least numbers are simpler than C.
2388            (list (concat "\\<\\(\\~\\|\\)"
2389                             "\\(0\\(\\([wW]\\|\\)[xX][0-9a-fA-F]+\\|"
2390                                    "[wW][0-9]+\\)\\|"
2391                                 "\\([0-9]+\\(\\.[0-9]+\\|\\)"
2392                                          "\\([eE]\\(\\~\\|\\)"
2393                                                 "[0-9]+\\|\\)\\)\\)")
2394                  '(0 mdw-number-face))
2395
2396            ;; And anything else is punctuation.
2397            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2398                  '(0 mdw-punct-face)))))
2399
2400   (mdw-post-config-mode-hack))
2401
2402 ;;;--------------------------------------------------------------------------
2403 ;;; Haskell configuration.
2404
2405 (defun mdw-fontify-haskell ()
2406
2407   ;; Fiddle with syntax table to get comments right.
2408   (modify-syntax-entry ?' "_")
2409   (modify-syntax-entry ?- ". 12")
2410   (modify-syntax-entry ?\n ">")
2411
2412   ;; Make punctuation be punctuation
2413   (let ((punct "=<>+-*/|&%!@?$.^:#`"))
2414     (do ((i 0 (1+ i)))
2415         ((>= i (length punct)))
2416       (modify-syntax-entry (aref punct i) ".")))
2417
2418   ;; Set fill prefix.
2419   (mdw-standard-fill-prefix "\\([ \t]*{?--?[ \t]*\\)")
2420
2421   ;; Fiddle with fontification.
2422   (make-local-variable 'font-lock-keywords)
2423   (let ((haskell-keywords
2424          (mdw-regexps "as"
2425                       "case" "ccall" "class"
2426                       "data" "default" "deriving" "do"
2427                       "else" "exists"
2428                       "forall" "foreign"
2429                       "hiding"
2430                       "if" "import" "in" "infix" "infixl" "infixr" "instance"
2431                       "let"
2432                       "mdo" "module"
2433                       "newtype"
2434                       "of"
2435                       "proc"
2436                       "qualified"
2437                       "rec"
2438                       "safe" "stdcall"
2439                       "then" "type"
2440                       "unsafe"
2441                       "where"))
2442         (control-sequences
2443          (mdw-regexps "ACK" "BEL" "BS" "CAN" "CR" "DC1" "DC2" "DC3" "DC4"
2444                       "DEL" "DLE" "EM" "ENQ" "EOT" "ESC" "ETB" "ETX" "FF"
2445                       "FS" "GS" "HT" "LF" "NAK" "NUL" "RS" "SI" "SO" "SOH"
2446                       "SP" "STX" "SUB" "SYN" "US" "VT")))
2447
2448     (setq font-lock-keywords
2449           (list
2450            (list (concat "{-" "[^-]*" "\\(-+[^-}][^-]*\\)*"
2451                               "\\(-+}\\|-*\\'\\)"
2452                          "\\|"
2453                          "--.*$")
2454                  '(0 font-lock-comment-face))
2455            (list (concat "\\_<\\(" haskell-keywords "\\)\\_>")
2456                  '(0 font-lock-keyword-face))
2457            (list (concat "'\\("
2458                          "[^\\]"
2459                          "\\|"
2460                          "\\\\"
2461                          "\\(" "[abfnrtv\\\"']" "\\|"
2462                                "^" "\\(" control-sequences "\\|"
2463                                          "[]A-Z@[\\^_]" "\\)" "\\|"
2464                                "\\|"
2465                                "[0-9]+" "\\|"
2466                                "[oO][0-7]+" "\\|"
2467                                "[xX][0-9A-Fa-f]+"
2468                          "\\)"
2469                          "\\)'")
2470                  '(0 font-lock-string-face))
2471            (list "\\_<[A-Z]\\(\\sw+\\|\\s_+\\)*\\_>"
2472                  '(0 font-lock-variable-name-face))
2473            (list (concat "\\_<0\\([xX][0-9a-fA-F]+\\|[oO][0-7]+\\)\\|"
2474                          "\\_<[0-9]+\\(\\.[0-9]*\\|\\)"
2475                          "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)")
2476                  '(0 mdw-number-face))
2477            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2478                  '(0 mdw-punct-face)))))
2479
2480   (mdw-post-config-mode-hack))
2481
2482 ;;;--------------------------------------------------------------------------
2483 ;;; Erlang configuration.
2484
2485 (setq erlang-electric-commands nil)
2486
2487 (defun mdw-fontify-erlang ()
2488
2489   ;; Set fill prefix.
2490   (mdw-standard-fill-prefix "\\([ \t]*{?%*[ \t]*\\)")
2491
2492   ;; Fiddle with fontification.
2493   (make-local-variable 'font-lock-keywords)
2494   (let ((erlang-keywords
2495          (mdw-regexps "after" "and" "andalso"
2496                       "band" "begin" "bnot" "bor" "bsl" "bsr" "bxor"
2497                       "case" "catch" "cond"
2498                       "div" "end" "fun" "if" "let" "not"
2499                       "of" "or" "orelse"
2500                       "query" "receive" "rem" "try" "when" "xor")))
2501
2502     (setq font-lock-keywords
2503           (list
2504            (list "%.*$"
2505                  '(0 font-lock-comment-face))
2506            (list (concat "\\<\\(" erlang-keywords "\\)\\>")
2507                  '(0 font-lock-keyword-face))
2508            (list (concat "^-\\sw+\\>")
2509                  '(0 font-lock-keyword-face))
2510            (list "\\<[0-9]+\\(\\|#[0-9a-zA-Z]+\\|[eE][+-]?[0-9]+\\)\\>"
2511                  '(0 mdw-number-face))
2512            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2513                  '(0 mdw-punct-face)))))
2514
2515   (mdw-post-config-mode-hack))
2516
2517 ;;;--------------------------------------------------------------------------
2518 ;;; Texinfo configuration.
2519
2520 (defun mdw-fontify-texinfo ()
2521
2522   ;; Set fill prefix.
2523   (mdw-standard-fill-prefix "\\([ \t]*@c[ \t]+\\)")
2524
2525   ;; Real fontification things.
2526   (make-local-variable 'font-lock-keywords)
2527   (setq font-lock-keywords
2528         (list
2529
2530          ;; Environment names are keywords.
2531          (list "@\\(end\\)  *\\([a-zA-Z]*\\)?"
2532                '(2 font-lock-keyword-face))
2533
2534          ;; Unmark escaped magic characters.
2535          (list "\\(@\\)\\([@{}]\\)"
2536                '(1 font-lock-keyword-face)
2537                '(2 font-lock-variable-name-face))
2538
2539          ;; Make sure we get comments properly.
2540          (list "@c\\(\\|omment\\)\\( .*\\)?$"
2541                '(0 font-lock-comment-face))
2542
2543          ;; Command names are keywords.
2544          (list "@\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)"
2545                '(0 font-lock-keyword-face))
2546
2547          ;; Fontify TeX special characters as punctuation.
2548          (list "[{}]+"
2549                '(0 mdw-punct-face))))
2550
2551   (mdw-post-config-mode-hack))
2552
2553 ;;;--------------------------------------------------------------------------
2554 ;;; TeX and LaTeX configuration.
2555
2556 (defun mdw-fontify-tex ()
2557   (setq ispell-parser 'tex)
2558   (turn-on-reftex)
2559
2560   ;; Don't make maths into a string.
2561   (modify-syntax-entry ?$ ".")
2562   (modify-syntax-entry ?$ "." font-lock-syntax-table)
2563   (local-set-key [?$] 'self-insert-command)
2564
2565   ;; Set fill prefix.
2566   (mdw-standard-fill-prefix "\\([ \t]*%+[ \t]*\\)")
2567
2568   ;; Real fontification things.
2569   (make-local-variable 'font-lock-keywords)
2570   (setq font-lock-keywords
2571         (list
2572
2573          ;; Environment names are keywords.
2574          (list (concat "\\\\\\(begin\\|end\\|newenvironment\\)"
2575                        "{\\([^}\n]*\\)}")
2576                '(2 font-lock-keyword-face))
2577
2578          ;; Suspended environment names are keywords too.
2579          (list (concat "\\\\\\(suspend\\|resume\\)\\(\\[[^]]*\\]\\)?"
2580                        "{\\([^}\n]*\\)}")
2581                '(3 font-lock-keyword-face))
2582
2583          ;; Command names are keywords.
2584          (list "\\\\\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)"
2585                '(0 font-lock-keyword-face))
2586
2587          ;; Handle @/.../ for italics.
2588          ;; (list "\\(@/\\)\\([^/]*\\)\\(/\\)"
2589          ;;       '(1 font-lock-keyword-face)
2590          ;;       '(3 font-lock-keyword-face))
2591
2592          ;; Handle @*...* for boldness.
2593          ;; (list "\\(@\\*\\)\\([^*]*\\)\\(\\*\\)"
2594          ;;       '(1 font-lock-keyword-face)
2595          ;;       '(3 font-lock-keyword-face))
2596
2597          ;; Handle @`...' for literal syntax things.
2598          ;; (list "\\(@`\\)\\([^']*\\)\\('\\)"
2599          ;;       '(1 font-lock-keyword-face)
2600          ;;       '(3 font-lock-keyword-face))
2601
2602          ;; Handle @<...> for nonterminals.
2603          ;; (list "\\(@<\\)\\([^>]*\\)\\(>\\)"
2604          ;;       '(1 font-lock-keyword-face)
2605          ;;       '(3 font-lock-keyword-face))
2606
2607          ;; Handle other @-commands.
2608          ;; (list "@\\([^a-zA-Z]\\|[a-zA-Z]*\\)"
2609          ;;       '(0 font-lock-keyword-face))
2610
2611          ;; Make sure we get comments properly.
2612          (list "%.*"
2613                '(0 font-lock-comment-face))
2614
2615          ;; Fontify TeX special characters as punctuation.
2616          (list "[$^_{}#&]"
2617                '(0 mdw-punct-face))))
2618
2619   (mdw-post-config-mode-hack))
2620
2621 ;;;--------------------------------------------------------------------------
2622 ;;; SGML hacking.
2623
2624 (defun mdw-sgml-mode ()
2625   (interactive)
2626   (sgml-mode)
2627   (mdw-standard-fill-prefix "")
2628   (make-local-variable 'sgml-delimiters)
2629   (setq sgml-delimiters
2630         '("AND" "&" "COM" "--" "CRO" "&#" "DSC" "]" "DSO" "[" "DTGC" "]"
2631           "DTGO" "[" "ERO" "&" "ETAGO" ":e" "GRPC" ")" "GRPO" "(" "LIT" "\""
2632           "LITA" "'" "MDC" ">" "MDO" "<!" "MINUS" "-" "MSC" "]]" "NESTC" "{"
2633           "NET" "}" "OPT" "?" "OR" "|" "PERO" "%" "PIC" ">" "PIO" "<?"
2634           "PLUS" "+" "REFC" "." "REP" "*" "RNI" "#" "SEQ" "," "STAGO" ":"
2635           "TAGC" "." "VI" "=" "MS-START" "<![" "MS-END" "]]>"
2636           "XML-ECOM" "-->" "XML-PIC" "?>" "XML-SCOM" "<!--" "XML-TAGCE" "/>"
2637           "NULL" ""))
2638   (setq major-mode 'mdw-sgml-mode)
2639   (setq mode-name "[mdw] SGML")
2640   (run-hooks 'mdw-sgml-mode-hook))
2641
2642 ;;;--------------------------------------------------------------------------
2643 ;;; Configuration files.
2644
2645 (defvar mdw-conf-quote-normal nil
2646   "*Control syntax category of quote characters `\"' and `''.
2647 If this is `t', consider quote characters to be normal
2648 punctuation, as for `conf-quote-normal'.  If this is `nil' then
2649 leave quote characters as quotes.  If this is a list, then
2650 consider the quote characters in the list to be normal
2651 punctuation.  If this is a single quote character, then consider
2652 that character only to be normal punctuation.")
2653 (defun mdw-conf-quote-normal-acceptable-value-p (value)
2654   "Is the VALUE is an acceptable value for `mdw-conf-quote-normal'?"
2655   (or (booleanp value)
2656       (every (lambda (v) (memq v '(?\" ?')))
2657              (if (listp value) value (list value)))))
2658 (put 'mdw-conf-quote-normal 'safe-local-variable '
2659      mdw-conf-quote-normal-acceptable-value-p)
2660
2661 (defun mdw-fix-up-quote ()
2662   "Apply the setting of `mdw-conf-quote-normal'."
2663   (let ((flag mdw-conf-quote-normal))
2664     (cond ((eq flag t)
2665            (conf-quote-normal t))
2666           ((not flag)
2667            nil)
2668           (t
2669            (let ((table (copy-syntax-table (syntax-table))))
2670              (mapc (lambda (ch) (modify-syntax-entry ch "." table))
2671                    (if (listp flag) flag (list flag)))
2672              (set-syntax-table table)
2673              (and font-lock-mode (font-lock-fontify-buffer)))))))
2674 (defun mdw-fix-up-quote-hack ()
2675   "Unpleasant hack to call `mdw-fix-up-quote' at the right time.
2676 Annoyingly, `hack-local-variables' is done after `set-auto-mode'
2677 so we wouldn't see a local-variable setting of
2678 `mdw-conf-quote-normal' in `conf-mode-hook'.  Instead, wire
2679 ourselves onto `hack-local-variables-hook' here, and check the
2680 setting once it's actually been made."
2681   (add-hook 'hack-local-variables-hook 'mdw-fix-up-quote t t))
2682 (add-hook 'conf-mode-hook 'mdw-fix-up-quote-hack t)
2683
2684 ;;;--------------------------------------------------------------------------
2685 ;;; Shell scripts.
2686
2687 (defun mdw-setup-sh-script-mode ()
2688
2689   ;; Fetch the shell interpreter's name.
2690   (let ((shell-name sh-shell-file))
2691
2692     ;; Try reading the hash-bang line.
2693     (save-excursion
2694       (goto-char (point-min))
2695       (if (looking-at "#![ \t]*\\([^ \t\n]*\\)")
2696           (setq shell-name (match-string 1))))
2697
2698     ;; Now try to set the shell.
2699     ;;
2700     ;; Don't let `sh-set-shell' bugger up my script.
2701     (let ((executable-set-magic #'(lambda (s &rest r) s)))
2702       (sh-set-shell shell-name)))
2703
2704   ;; Now enable my keys and the fontification.
2705   (mdw-misc-mode-config)
2706
2707   ;; Set the indentation level correctly.
2708   (setq sh-indentation 2)
2709   (setq sh-basic-offset 2))
2710
2711 (setq sh-shell-file "/bin/sh")
2712
2713 ;; Awful hacking to override the shell detection for particular scripts.
2714 (defmacro define-custom-shell-mode (name shell)
2715   `(defun ,name ()
2716      (interactive)
2717      (set (make-local-variable 'sh-shell-file) ,shell)
2718      (sh-mode)))
2719 (define-custom-shell-mode bash-mode "/bin/bash")
2720 (define-custom-shell-mode rc-mode "/usr/bin/rc")
2721 (put 'sh-shell-file 'permanent-local t)
2722
2723 ;; Hack the rc syntax table.  Backquotes aren't paired in rc.
2724 (eval-after-load "sh-script"
2725   '(or (assq 'rc sh-mode-syntax-table-input)
2726        (let ((frag '(nil
2727                      ?# "<"
2728                      ?\n ">#"
2729                      ?\" "\"\""
2730                      ?\' "\"\'"
2731                      ?$ "'"
2732                      ?\` "."
2733                      ?! "_"
2734                      ?% "_"
2735                      ?. "_"
2736                      ?^ "_"
2737                      ?~ "_"
2738                      ?, "_"
2739                      ?= "."
2740                      ?< "."
2741                      ?> "."))
2742              (assoc (assq 'rc sh-mode-syntax-table-input)))
2743          (if assoc
2744              (rplacd assoc frag)
2745            (setq sh-mode-syntax-table-input
2746                  (cons (cons 'rc frag)
2747                        sh-mode-syntax-table-input))))))
2748
2749 ;;;--------------------------------------------------------------------------
2750 ;;; Emacs shell mode.
2751
2752 (defun mdw-eshell-prompt ()
2753   (let ((left "[") (right "]"))
2754     (when (= (user-uid) 0)
2755       (setq left "«" right "»"))
2756     (concat left
2757             (save-match-data
2758               (replace-regexp-in-string "\\..*$" "" (system-name)))
2759             " "
2760             (let* ((pwd (eshell/pwd)) (npwd (length pwd))
2761                    (home (expand-file-name "~")) (nhome (length home)))
2762               (if (and (>= npwd nhome)
2763                        (or (= nhome npwd)
2764                            (= (elt pwd nhome) ?/))
2765                        (string= (substring pwd 0 nhome) home))
2766                   (concat "~" (substring pwd (length home)))
2767                 pwd))
2768             right)))
2769 (setq eshell-prompt-function 'mdw-eshell-prompt)
2770 (setq eshell-prompt-regexp "^\\[[^]>]+\\(\\]\\|>>?\\)")
2771
2772 (defun eshell/e (file) (find-file file) nil)
2773 (defun eshell/ee (file) (find-file-other-window file) nil)
2774 (defun eshell/w3m (url) (w3m-goto-url url) nil)
2775
2776 (mdw-define-face eshell-prompt (t :weight bold))
2777 (mdw-define-face eshell-ls-archive (t :weight bold :foreground "red"))
2778 (mdw-define-face eshell-ls-backup (t :foreground "lightgrey" :slant italic))
2779 (mdw-define-face eshell-ls-product (t :foreground "lightgrey" :slant italic))
2780 (mdw-define-face eshell-ls-clutter (t :foreground "lightgrey" :slant italic))
2781 (mdw-define-face eshell-ls-executable (t :weight bold))
2782 (mdw-define-face eshell-ls-directory (t :foreground "cyan" :weight bold))
2783 (mdw-define-face eshell-ls-readonly (t nil))
2784 (mdw-define-face eshell-ls-symlink (t :foreground "cyan"))
2785
2786 ;;;--------------------------------------------------------------------------
2787 ;;; Messages-file mode.
2788
2789 (defun messages-mode-guts ()
2790   (setq messages-mode-syntax-table (make-syntax-table))
2791   (set-syntax-table messages-mode-syntax-table)
2792   (modify-syntax-entry ?0 "w" messages-mode-syntax-table)
2793   (modify-syntax-entry ?1 "w" messages-mode-syntax-table)
2794   (modify-syntax-entry ?2 "w" messages-mode-syntax-table)
2795   (modify-syntax-entry ?3 "w" messages-mode-syntax-table)
2796   (modify-syntax-entry ?4 "w" messages-mode-syntax-table)
2797   (modify-syntax-entry ?5 "w" messages-mode-syntax-table)
2798   (modify-syntax-entry ?6 "w" messages-mode-syntax-table)
2799   (modify-syntax-entry ?7 "w" messages-mode-syntax-table)
2800   (modify-syntax-entry ?8 "w" messages-mode-syntax-table)
2801   (modify-syntax-entry ?9 "w" messages-mode-syntax-table)
2802   (make-local-variable 'comment-start)
2803   (make-local-variable 'comment-end)
2804   (make-local-variable 'indent-line-function)
2805   (setq indent-line-function 'indent-relative)
2806   (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
2807   (make-local-variable 'font-lock-defaults)
2808   (make-local-variable 'messages-mode-keywords)
2809   (let ((keywords
2810          (mdw-regexps "array" "bitmap" "callback" "docs[ \t]+enum"
2811                       "export" "enum" "fixed-octetstring" "flags"
2812                       "harmless" "map" "nested" "optional"
2813                       "optional-tagged" "package" "primitive"
2814                       "primitive-nullfree" "relaxed[ \t]+enum"
2815                       "set" "table" "tagged-optional"   "union"
2816                       "variadic" "vector" "version" "version-tag")))
2817     (setq messages-mode-keywords
2818           (list
2819            (list (concat "\\<\\(" keywords "\\)\\>:")
2820                  '(0 font-lock-keyword-face))
2821            '("\\([-a-zA-Z0-9]+:\\)" (0 font-lock-warning-face))
2822            '("\\(\\<[a-z][-_a-zA-Z0-9]*\\)"
2823              (0 font-lock-variable-name-face))
2824            '("\\<\\([0-9]+\\)\\>" (0 mdw-number-face))
2825            '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2826              (0 mdw-punct-face)))))
2827   (setq font-lock-defaults
2828         '(messages-mode-keywords nil nil nil nil))
2829   (run-hooks 'messages-file-hook))
2830
2831 (defun messages-mode ()
2832   (interactive)
2833   (fundamental-mode)
2834   (setq major-mode 'messages-mode)
2835   (setq mode-name "Messages")
2836   (messages-mode-guts)
2837   (modify-syntax-entry ?# "<" messages-mode-syntax-table)
2838   (modify-syntax-entry ?\n ">" messages-mode-syntax-table)
2839   (setq comment-start "# ")
2840   (setq comment-end "")
2841   (run-hooks 'messages-mode-hook))
2842
2843 (defun cpp-messages-mode ()
2844   (interactive)
2845   (fundamental-mode)
2846   (setq major-mode 'cpp-messages-mode)
2847   (setq mode-name "CPP Messages")
2848   (messages-mode-guts)
2849   (modify-syntax-entry ?* ". 23" messages-mode-syntax-table)
2850   (modify-syntax-entry ?/ ". 14" messages-mode-syntax-table)
2851   (setq comment-start "/* ")
2852   (setq comment-end " */")
2853   (let ((preprocessor-keywords
2854          (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
2855                       "ident" "if" "ifdef" "ifndef" "import" "include"
2856                       "line" "pragma" "unassert" "undef" "warning")))
2857     (setq messages-mode-keywords
2858           (append (list (list (concat "^[ \t]*\\#[ \t]*"
2859                                       "\\(include\\|import\\)"
2860                                       "[ \t]*\\(<[^>]+\\(>\\|\\)\\)")
2861                               '(2 font-lock-string-face))
2862                         (list (concat "^\\([ \t]*#[ \t]*\\(\\("
2863                                       preprocessor-keywords
2864                                       "\\)\\>\\|[0-9]+\\|$\\)\\)")
2865                               '(1 font-lock-keyword-face)))
2866                   messages-mode-keywords)))
2867   (run-hooks 'cpp-messages-mode-hook))
2868
2869 (add-hook 'messages-mode-hook 'mdw-misc-mode-config t)
2870 (add-hook 'cpp-messages-mode-hook 'mdw-misc-mode-config t)
2871 ; (add-hook 'messages-file-hook 'mdw-fontify-messages t)
2872
2873 ;;;--------------------------------------------------------------------------
2874 ;;; Messages-file mode.
2875
2876 (defvar mallow-driver-substitution-face 'mallow-driver-substitution-face
2877   "Face to use for subsittution directives.")
2878 (make-face 'mallow-driver-substitution-face)
2879 (defvar mallow-driver-text-face 'mallow-driver-text-face
2880   "Face to use for body text.")
2881 (make-face 'mallow-driver-text-face)
2882
2883 (defun mallow-driver-mode ()
2884   (interactive)
2885   (fundamental-mode)
2886   (setq major-mode 'mallow-driver-mode)
2887   (setq mode-name "Mallow driver")
2888   (setq mallow-driver-mode-syntax-table (make-syntax-table))
2889   (set-syntax-table mallow-driver-mode-syntax-table)
2890   (make-local-variable 'comment-start)
2891   (make-local-variable 'comment-end)
2892   (make-local-variable 'indent-line-function)
2893   (setq indent-line-function 'indent-relative)
2894   (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
2895   (make-local-variable 'font-lock-defaults)
2896   (make-local-variable 'mallow-driver-mode-keywords)
2897   (let ((keywords
2898          (mdw-regexps "each" "divert" "file" "if"
2899                       "perl" "set" "string" "type" "write")))
2900     (setq mallow-driver-mode-keywords
2901           (list
2902            (list (concat "^%\\s *\\(}\\|\\(" keywords "\\)\\>\\).*$")
2903                  '(0 font-lock-keyword-face))
2904            (list "^%\\s *\\(#.*\\|\\)$"
2905                  '(0 font-lock-comment-face))
2906            (list "^%"
2907                  '(0 font-lock-keyword-face))
2908            (list "^|?\\(.+\\)$" '(1 mallow-driver-text-face))
2909            (list "\\${[^}]*}"
2910                  '(0 mallow-driver-substitution-face t)))))
2911   (setq font-lock-defaults
2912         '(mallow-driver-mode-keywords nil nil nil nil))
2913   (modify-syntax-entry ?\" "_" mallow-driver-mode-syntax-table)
2914   (modify-syntax-entry ?\n ">" mallow-driver-mode-syntax-table)
2915   (setq comment-start "%# ")
2916   (setq comment-end "")
2917   (run-hooks 'mallow-driver-mode-hook))
2918
2919 (add-hook 'mallow-driver-hook 'mdw-misc-mode-config t)
2920
2921 ;;;--------------------------------------------------------------------------
2922 ;;; NFast debugs.
2923
2924 (defun nfast-debug-mode ()
2925   (interactive)
2926   (fundamental-mode)
2927   (setq major-mode 'nfast-debug-mode)
2928   (setq mode-name "NFast debug")
2929   (setq messages-mode-syntax-table (make-syntax-table))
2930   (set-syntax-table messages-mode-syntax-table)
2931   (make-local-variable 'font-lock-defaults)
2932   (make-local-variable 'nfast-debug-mode-keywords)
2933   (setq truncate-lines t)
2934   (setq nfast-debug-mode-keywords
2935         (list
2936          '("^\\(NFast_\\(Connect\\|Disconnect\\|Submit\\|Wait\\)\\)"
2937            (0 font-lock-keyword-face))
2938          (list (concat "^[ \t]+\\(\\("
2939                        "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
2940                        "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
2941                        "[ \t]+\\)*"
2942                        "[0-9a-fA-F]+\\)[ \t]*$")
2943            '(0 mdw-number-face))
2944          '("^[ \t]+\.status=[ \t]+\\<\\(OK\\)\\>"
2945            (1 font-lock-keyword-face))
2946          '("^[ \t]+\.status=[ \t]+\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>"
2947            (1 font-lock-warning-face))
2948          '("^[ \t]+\.status[ \t]+\\<\\(zero\\)\\>"
2949            (1 nil))
2950          (list (concat "^[ \t]+\\.cmd=[ \t]+"
2951                        "\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>")
2952            '(1 font-lock-keyword-face))
2953          '("-?\\<\\([0-9]+\\|0x[0-9a-fA-F]+\\)\\>" (0 mdw-number-face))
2954          '("^\\([ \t]+[a-z0-9.]+\\)" (0 font-lock-variable-name-face))
2955          '("\\<\\([a-z][a-z0-9.]+\\)\\>=" (1 font-lock-variable-name-face))
2956          '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" (0 mdw-punct-face))))
2957   (setq font-lock-defaults
2958         '(nfast-debug-mode-keywords nil nil nil nil))
2959   (run-hooks 'nfast-debug-mode-hook))
2960
2961 ;;;--------------------------------------------------------------------------
2962 ;;; Other languages.
2963
2964 ;; Smalltalk.
2965
2966 (defun mdw-setup-smalltalk ()
2967   (and mdw-auto-indent
2968        (local-set-key "\C-m" 'smalltalk-newline-and-indent))
2969   (make-local-variable 'mdw-auto-indent)
2970   (setq mdw-auto-indent nil)
2971   (local-set-key "\C-i" 'smalltalk-reindent))
2972
2973 (defun mdw-fontify-smalltalk ()
2974   (make-local-variable 'font-lock-keywords)
2975   (setq font-lock-keywords
2976         (list
2977          (list "\\<[A-Z][a-zA-Z0-9]*\\>"
2978                '(0 font-lock-keyword-face))
2979          (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
2980                        "[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)"
2981                        "\\([eE]\\([-+]\\|\\)[0-9_]+\\|\\)")
2982                '(0 mdw-number-face))
2983          (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2984                '(0 mdw-punct-face))))
2985   (mdw-post-config-mode-hack))
2986
2987 ;; Lispy languages.
2988
2989 ;; Unpleasant bodge.
2990 (unless (boundp 'slime-repl-mode-map)
2991   (setq slime-repl-mode-map (make-sparse-keymap)))
2992
2993 (defun mdw-indent-newline-and-indent ()
2994   (interactive)
2995   (indent-for-tab-command)
2996   (newline-and-indent))
2997
2998 (eval-after-load "cl-indent"
2999   '(progn
3000      (mapc #'(lambda (pair)
3001                (put (car pair)
3002                     'common-lisp-indent-function
3003                     (cdr pair)))
3004       '((destructuring-bind . ((&whole 4 &rest 1) 4 &body))
3005         (multiple-value-bind . ((&whole 4 &rest 1) 4 &body))))))
3006
3007 (defun mdw-common-lisp-indent ()
3008   (make-local-variable 'lisp-indent-function)
3009   (setq lisp-indent-function 'common-lisp-indent-function))
3010
3011 (setq lisp-simple-loop-indentation 2
3012       lisp-loop-keyword-indentation 6
3013       lisp-loop-forms-indentation 6)
3014
3015 (defun mdw-fontify-lispy ()
3016
3017   ;; Set fill prefix.
3018   (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)")
3019
3020   ;; Not much fontification needed.
3021   (make-local-variable 'font-lock-keywords)
3022   (setq font-lock-keywords
3023         (list (list (concat "\\("
3024                             "\\_<[-+]?"
3025                             "\\(" "[0-9]+/[0-9]+"
3026                             "\\|" "\\(" "[0-9]+" "\\(\\.[0-9]*\\)?" "\\|"
3027                                         "\\.[0-9]+" "\\)"
3028                                   "\\([dDeEfFlLsS][-+]?[0-9]+\\)?"
3029                             "\\)"
3030                             "\\|"
3031                             "#"
3032                             "\\(" "x" "[-+]?"
3033                                   "[0-9A-Fa-f]+" "\\(/[0-9A-Fa-f]+\\)?"
3034                             "\\|" "o" "[-+]?" "[0-7]+" "\\(/[0-7]+\\)?"
3035                             "\\|" "b" "[-+]?" "[01]+" "\\(/[01]+\\)?"
3036                             "\\|" "[0-9]+" "r" "[-+]?"
3037                                   "[0-9a-zA-Z]+" "\\(/[0-9a-zA-Z]+\\)?"
3038                             "\\)"
3039                             "\\)\\_>")
3040                     '(0 mdw-number-face))
3041               (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3042                     '(0 mdw-punct-face))))
3043
3044   (mdw-post-config-mode-hack))
3045
3046 (defun comint-send-and-indent ()
3047   (interactive)
3048   (comint-send-input)
3049   (and mdw-auto-indent
3050        (indent-for-tab-command)))
3051
3052 (defun mdw-setup-m4 ()
3053
3054   ;; Inexplicably, Emacs doesn't match braces in m4 mode.  This is very
3055   ;; annoying: fix it.
3056   (modify-syntax-entry ?{ "(")
3057   (modify-syntax-entry ?} ")")
3058
3059   ;; Fill prefix.
3060   (mdw-standard-fill-prefix "\\([ \t]*\\(?:#+\\|\\<dnl\\>\\)[ \t]*\\)"))
3061
3062 ;;;--------------------------------------------------------------------------
3063 ;;; Text mode.
3064
3065 (defun mdw-text-mode ()
3066   (setq fill-column 72)
3067   (flyspell-mode t)
3068   (mdw-standard-fill-prefix
3069    "\\([ \t]*\\([>#|:] ?\\)*[ \t]*\\)" 3)
3070   (auto-fill-mode 1))
3071
3072 ;;;--------------------------------------------------------------------------
3073 ;;; Outline and hide/show modes.
3074
3075 (defun mdw-outline-collapse-all ()
3076   "Completely collapse everything in the entire buffer."
3077   (interactive)
3078   (save-excursion
3079     (goto-char (point-min))
3080     (while (< (point) (point-max))
3081       (hide-subtree)
3082       (forward-line))))
3083
3084 (setq hs-hide-comments-when-hiding-all nil)
3085
3086 (defadvice hs-hide-all (after hide-first-comment activate)
3087   (save-excursion (hs-hide-initial-comment-block)))
3088
3089 ;;;--------------------------------------------------------------------------
3090 ;;; Shell mode.
3091
3092 (defun mdw-sh-mode-setup ()
3093   (local-set-key [?\C-a] 'comint-bol)
3094   (add-hook 'comint-output-filter-functions
3095             'comint-watch-for-password-prompt))
3096
3097 (defun mdw-term-mode-setup ()
3098   (setq term-prompt-regexp shell-prompt-pattern)
3099   (make-local-variable 'mouse-yank-at-point)
3100   (make-local-variable 'transient-mark-mode)
3101   (setq mouse-yank-at-point t)
3102   (auto-fill-mode -1)
3103   (setq tab-width 8))
3104
3105 (defun term-send-meta-right () (interactive) (term-send-raw-string "\e\e[C"))
3106 (defun term-send-meta-left  () (interactive) (term-send-raw-string "\e\e[D"))
3107 (defun term-send-ctrl-uscore () (interactive) (term-send-raw-string "\C-_"))
3108 (defun term-send-meta-meta-something ()
3109   (interactive)
3110   (term-send-raw-string "\e\e")
3111   (term-send-raw))
3112 (eval-after-load 'term
3113   '(progn
3114      (define-key term-raw-map [?\e ?\e] nil)
3115      (define-key term-raw-map [?\e ?\e t] 'term-send-meta-meta-something)
3116      (define-key term-raw-map [?\C-/] 'term-send-ctrl-uscore)
3117      (define-key term-raw-map [M-right] 'term-send-meta-right)
3118      (define-key term-raw-map [?\e ?\M-O ?C] 'term-send-meta-right)
3119      (define-key term-raw-map [M-left] 'term-send-meta-left)
3120      (define-key term-raw-map [?\e ?\M-O ?D] 'term-send-meta-left)))
3121
3122 (defadvice term-exec (before program-args-list compile activate)
3123   "If the PROGRAM argument is a list, interpret it as (PROGRAM . SWITCHES).
3124 This allows you to pass a list of arguments through `ansi-term'."
3125   (let ((program (ad-get-arg 2)))
3126     (if (listp program)
3127         (progn
3128           (ad-set-arg 2 (car program))
3129           (ad-set-arg 4 (cdr program))))))
3130
3131 (defun ssh (host)
3132   "Open a terminal containing an ssh session to the HOST."
3133   (interactive "sHost: ")
3134   (ansi-term (list "ssh" host) (format "ssh@%s" host)))
3135
3136 ;;;--------------------------------------------------------------------------
3137 ;;; Inferior Emacs Lisp.
3138
3139 (setq comint-prompt-read-only t)
3140
3141 (eval-after-load "comint"
3142   '(progn
3143      (define-key comint-mode-map "\C-w" 'comint-kill-region)
3144      (define-key comint-mode-map [C-S-backspace] 'comint-kill-whole-line)))
3145
3146 (eval-after-load "ielm"
3147   '(progn
3148      (define-key ielm-map "\C-w" 'comint-kill-region)
3149      (define-key ielm-map [C-S-backspace] 'comint-kill-whole-line)))
3150
3151 ;;;----- That's all, folks --------------------------------------------------
3152
3153 (provide 'dot-emacs)