chiark / gitweb /
Support for Magnar Sveen's awesome `multiple-cursors' package.
[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 mc/cursor-face
869   (((type tty) (class mono)) :inverse-video t)
870   (t :background "red"))
871 (mdw-define-face minibuffer-prompt
872   (t :weight bold))
873 (mdw-define-face mode-line
874   (((class color)) :foreground "blue" :background "yellow"
875                    :box (:line-width 1 :style released-button))
876   (t :inverse-video t))
877 (mdw-define-face mode-line-inactive
878   (((class color)) :foreground "yellow" :background "blue"
879                    :box (:line-width 1 :style released-button))
880   (t :inverse-video t))
881 (mdw-define-face nobreak-space
882   (((type tty)))
883   (t :inherit escape-glyph :underline t))
884 (mdw-define-face scroll-bar
885   (t :foreground "black" :background "lightgrey"))
886 (mdw-define-face fringe
887   (t :foreground "yellow"))
888 (mdw-define-face show-paren-match
889   (((class color)) :background "darkgreen")
890   (t :underline t))
891 (mdw-define-face show-paren-mismatch
892   (((class color)) :background "red")
893   (t :inverse-video t))
894 (mdw-define-face highlight
895   (((type x) (class color)) :background "DarkSeaGreen4")
896   (((type tty) (class color)) :background "cyan")
897   (t :inverse-video t))
898
899 (mdw-define-face holiday-face
900   (t :background "red"))
901 (mdw-define-face calendar-today-face
902   (t :foreground "yellow" :weight bold))
903
904 (mdw-define-face comint-highlight-prompt
905   (t :weight bold))
906 (mdw-define-face comint-highlight-input
907   (t nil))
908
909 (mdw-define-face dired-directory
910   (t :foreground "cyan" :weight bold))
911 (mdw-define-face dired-symlink
912   (t :foreground "cyan"))
913 (mdw-define-face dired-perm-write
914   (t nil))
915
916 (mdw-define-face trailing-whitespace
917   (((class color)) :background "red")
918   (t :inverse-video t))
919 (mdw-define-face mdw-punct-face
920   (((type tty)) :foreground "yellow") (t :foreground "burlywood2"))
921 (mdw-define-face mdw-number-face
922   (t :foreground "yellow"))
923 (mdw-define-face mdw-trivial-face)
924 (mdw-define-face font-lock-function-name-face
925   (t :slant italic))
926 (mdw-define-face font-lock-keyword-face
927   (t :weight bold))
928 (mdw-define-face font-lock-constant-face
929   (t :slant italic))
930 (mdw-define-face font-lock-builtin-face
931   (t :weight bold))
932 (mdw-define-face font-lock-type-face
933   (t :weight bold :slant italic))
934 (mdw-define-face font-lock-reference-face
935   (t :weight bold))
936 (mdw-define-face font-lock-variable-name-face
937   (t :slant italic))
938 (mdw-define-face font-lock-comment-delimiter-face
939   (((class mono)) :weight bold)
940   (((type tty) (class color)) :foreground "green")
941   (t :slant italic :foreground "SeaGreen1"))
942 (mdw-define-face font-lock-comment-face
943   (((class mono)) :weight bold)
944   (((type tty) (class color)) :foreground "green")
945   (t :slant italic :foreground "SeaGreen1"))
946 (mdw-define-face font-lock-string-face
947   (((class mono)) :weight bold)
948   (((class color)) :foreground "SkyBlue1"))
949
950 (mdw-define-face message-separator
951   (t :background "red" :foreground "white" :weight bold))
952 (mdw-define-face message-cited-text
953   (default :slant italic)
954   (((type tty)) :foreground "green") (t :foreground "SeaGreen1"))
955 (mdw-define-face message-header-cc
956   (default :weight bold)
957   (((type tty)) :foreground "green") (t :foreground "SeaGreen1"))
958 (mdw-define-face message-header-newsgroups
959   (default :weight bold)
960   (((type tty)) :foreground "green") (t :foreground "SeaGreen1"))
961 (mdw-define-face message-header-subject
962   (default :weight bold)
963   (((type tty)) :foreground "green") (t :foreground "SeaGreen1"))
964 (mdw-define-face message-header-to
965   (default :weight bold)
966   (((type tty)) :foreground "green") (t :foreground "SeaGreen1"))
967 (mdw-define-face message-header-xheader
968   (default :weight bold)
969   (((type tty)) :foreground "green") (t :foreground "SeaGreen1"))
970 (mdw-define-face message-header-other
971   (default :weight bold)
972   (((type tty)) :foreground "green") (t :foreground "SeaGreen1"))
973 (mdw-define-face message-header-name
974   (((type tty)) :foreground "green") (t :foreground "SeaGreen1"))
975 (mdw-define-face which-func
976   (t nil))
977
978 (mdw-define-face diff-header
979   (t nil))
980 (mdw-define-face diff-index
981   (t :weight bold))
982 (mdw-define-face diff-file-header
983   (t :weight bold))
984 (mdw-define-face diff-hunk-header
985   (t :foreground "SkyBlue1"))
986 (mdw-define-face diff-function
987   (t :foreground "SkyBlue1" :weight bold))
988 (mdw-define-face diff-header
989   (t :background "grey10"))
990 (mdw-define-face diff-added
991   (t :foreground "green"))
992 (mdw-define-face diff-removed
993   (t :foreground "red"))
994 (mdw-define-face diff-context
995   (t nil))
996 (mdw-define-face diff-refine-change
997   (((class color) (type x)) :background "RoyalBlue4")
998   (t :underline t))
999
1000 (mdw-define-face dylan-header-background
1001   (((class color) (type x)) :background "NavyBlue")
1002   (t :background "blue"))
1003
1004 (mdw-define-face magit-diff-add
1005   (t :foreground "green"))
1006 (mdw-define-face magit-diff-del
1007   (t :foreground "red"))
1008 (mdw-define-face magit-diff-file-header
1009   (t :weight bold))
1010 (mdw-define-face magit-diff-hunk-header
1011   (t :foreground "SkyBlue1"))
1012 (mdw-define-face magit-item-highlight
1013   (((type tty)) :background "blue")
1014   (t :background "DarkSeaGreen4"))
1015 (mdw-define-face magit-log-head-label-remote
1016   (((type tty)) :background "cyan" :foreground "green")
1017   (t :background "grey11" :foreground "DarkSeaGreen2" :box t))
1018 (mdw-define-face magit-log-head-label-local
1019   (((type tty)) :background "cyan" :foreground "yellow")
1020   (t :background "grey11" :foreground "LightSkyBlue1" :box t))
1021 (mdw-define-face magit-log-head-label-tags
1022   (((type tty)) :background "red" :foreground "yellow")
1023   (t :background "LemonChiffon1" :foreground "goldenrod4" :box t))
1024 (mdw-define-face magit-log-graph
1025   (((type tty)) :foreground "magenta")
1026   (t :foreground "grey80"))
1027
1028 (mdw-define-face erc-input-face
1029   (t :foreground "red"))
1030
1031 (mdw-define-face woman-bold
1032   (t :weight bold))
1033 (mdw-define-face woman-italic
1034   (t :slant italic))
1035
1036 (eval-after-load "rst"
1037   '(progn
1038      (mdw-define-face rst-level-1-face
1039        (t :foreground "SkyBlue1" :weight bold))
1040      (mdw-define-face rst-level-2-face
1041        (t :foreground "SeaGreen1" :weight bold))
1042      (mdw-define-face rst-level-3-face
1043        (t :weight bold))
1044      (mdw-define-face rst-level-4-face
1045        (t :slant italic))
1046      (mdw-define-face rst-level-5-face
1047        (t :underline t))
1048      (mdw-define-face rst-level-6-face
1049        ())))
1050
1051 (mdw-define-face p4-depot-added-face
1052   (t :foreground "green"))
1053 (mdw-define-face p4-depot-branch-op-face
1054   (t :foreground "yellow"))
1055 (mdw-define-face p4-depot-deleted-face
1056   (t :foreground "red"))
1057 (mdw-define-face p4-depot-unmapped-face
1058   (t :foreground "SkyBlue1"))
1059 (mdw-define-face p4-diff-change-face
1060   (t :foreground "yellow"))
1061 (mdw-define-face p4-diff-del-face
1062   (t :foreground "red"))
1063 (mdw-define-face p4-diff-file-face
1064   (t :foreground "SkyBlue1"))
1065 (mdw-define-face p4-diff-head-face
1066   (t :background "grey10"))
1067 (mdw-define-face p4-diff-ins-face
1068   (t :foreground "green"))
1069
1070 (mdw-define-face w3m-anchor-face
1071   (t :foreground "SkyBlue1" :underline t))
1072 (mdw-define-face w3m-arrived-anchor-face
1073   (t :foreground "SkyBlue1" :underline t))
1074
1075 (mdw-define-face whizzy-slice-face
1076   (t :background "grey10"))
1077 (mdw-define-face whizzy-error-face
1078   (t :background "darkred"))
1079
1080 ;; Ellipses used to indicate hidden text (and similar).
1081 (mdw-define-face mdw-ellipsis-face
1082   (((type tty)) :foreground "blue") (t :foreground "grey60"))
1083 (let ((dollar (make-glyph-code ?$ 'mdw-ellipsis-face))
1084       (backslash (make-glyph-code ?\ 'mdw-ellipsis-face))
1085       (dot (make-glyph-code ?. 'mdw-ellipsis-face))
1086       (bar (make-glyph-code ?| mdw-ellipsis-face)))
1087   (set-display-table-slot standard-display-table 0 dollar)
1088   (set-display-table-slot standard-display-table 1 backslash)
1089   (set-display-table-slot standard-display-table 4
1090                           (vector dot dot dot))
1091   (set-display-table-slot standard-display-table 5 bar))
1092
1093 ;;;--------------------------------------------------------------------------
1094 ;;; C programming configuration.
1095
1096 ;; Linux kernel hacking.
1097
1098 (defvar linux-c-mode-hook)
1099
1100 (defun linux-c-mode ()
1101   (interactive)
1102   (c-mode)
1103   (setq major-mode 'linux-c-mode)
1104   (setq mode-name "Linux C")
1105   (run-hooks 'linux-c-mode-hook))
1106
1107 ;; Make C indentation nice.
1108
1109 (defun mdw-c-lineup-arglist (langelem)
1110   "Hack for DWIMmery in c-lineup-arglist."
1111   (if (save-excursion
1112         (c-block-in-arglist-dwim (c-langelem-2nd-pos c-syntactic-element)))
1113       0
1114     (c-lineup-arglist langelem)))
1115
1116 (defun mdw-c-indent-extern-mumble (langelem)
1117   "Indent `extern \"...\" {' lines."
1118   (save-excursion
1119     (back-to-indentation)
1120     (if (looking-at
1121          "\\s-*\\<extern\\>\\s-*\"\\([^\\\\\"]+\\|\\.\\)*\"\\s-*{")
1122         c-basic-offset
1123       nil)))
1124
1125 (defun mdw-c-style ()
1126   (c-add-style "[mdw] C and C++ style"
1127                '((c-basic-offset . 2)
1128                  (comment-column . 40)
1129                  (c-class-key . "class")
1130                  (c-backslash-column . 72)
1131                  (c-offsets-alist
1132                   (substatement-open . (add 0 c-indent-one-line-block))
1133                   (defun-open . (add 0 c-indent-one-line-block))
1134                   (arglist-cont-nonempty . mdw-c-lineup-arglist)
1135                   (topmost-intro . mdw-c-indent-extern-mumble)
1136                   (cpp-define-intro . 0)
1137                   (knr-argdecl . 0)
1138                   (inextern-lang . [0])
1139                   (label . 0)
1140                   (case-label . +)
1141                   (access-label . -)
1142                   (inclass . +)
1143                   (inline-open . ++)
1144                   (statement-cont . +)
1145                   (statement-case-intro . +)))
1146                t))
1147
1148 (defvar mdw-c-comment-fill-prefix
1149   `((,(concat "\\([ \t]*/?\\)"
1150               "\\(\*\\|//]\\)"
1151               "\\([ \t]*\\)"
1152               "\\([A-Za-z]+:[ \t]*\\)?"
1153               mdw-hanging-indents)
1154      (pad . 1) (match . 2) (pad . 3) (pad . 4) (pad . 5)))
1155   "Fill prefix matching C comments (both kinds).")
1156
1157 (defun mdw-fontify-c-and-c++ ()
1158
1159   ;; Fiddle with some syntax codes.
1160   (modify-syntax-entry ?* ". 23")
1161   (modify-syntax-entry ?/ ". 124b")
1162   (modify-syntax-entry ?\n "> b")
1163
1164   ;; Other stuff.
1165   (mdw-c-style)
1166   (setq c-hanging-comment-ender-p nil)
1167   (setq c-backslash-column 72)
1168   (setq c-label-minimum-indentation 0)
1169   (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
1170
1171   ;; Now define things to be fontified.
1172   (make-local-variable 'font-lock-keywords)
1173   (let ((c-keywords
1174          (mdw-regexps "and"             ;C++
1175                       "and_eq"          ;C++
1176                       "asm"             ;K&R, GCC
1177                       "auto"            ;K&R, C89
1178                       "bitand"          ;C++
1179                       "bitor"           ;C++
1180                       "bool"            ;C++, C9X macro
1181                       "break"           ;K&R, C89
1182                       "case"            ;K&R, C89
1183                       "catch"           ;C++
1184                       "char"            ;K&R, C89
1185                       "class"           ;C++
1186                       "complex"         ;C9X macro, C++ template type
1187                       "compl"           ;C++
1188                       "const"           ;C89
1189                       "const_cast"      ;C++
1190                       "continue"        ;K&R, C89
1191                       "defined"         ;C89 preprocessor
1192                       "default"         ;K&R, C89
1193                       "delete"          ;C++
1194                       "do"              ;K&R, C89
1195                       "double"          ;K&R, C89
1196                       "dynamic_cast"    ;C++
1197                       "else"            ;K&R, C89
1198                       ;; "entry"        ;K&R -- never used
1199                       "enum"            ;C89
1200                       "explicit"        ;C++
1201                       "export"          ;C++
1202                       "extern"          ;K&R, C89
1203                       "float"           ;K&R, C89
1204                       "for"             ;K&R, C89
1205                       ;; "fortran"      ;K&R
1206                       "friend"          ;C++
1207                       "goto"            ;K&R, C89
1208                       "if"              ;K&R, C89
1209                       "imaginary"       ;C9X macro
1210                       "inline"          ;C++, C9X, GCC
1211                       "int"             ;K&R, C89
1212                       "long"            ;K&R, C89
1213                       "mutable"         ;C++
1214                       "namespace"       ;C++
1215                       "new"             ;C++
1216                       "operator"        ;C++
1217                       "or"              ;C++
1218                       "or_eq"           ;C++
1219                       "private"         ;C++
1220                       "protected"       ;C++
1221                       "public"          ;C++
1222                       "register"        ;K&R, C89
1223                       "reinterpret_cast" ;C++
1224                       "restrict"         ;C9X
1225                       "return"           ;K&R, C89
1226                       "short"            ;K&R, C89
1227                       "signed"           ;C89
1228                       "sizeof"           ;K&R, C89
1229                       "static"           ;K&R, C89
1230                       "static_cast"      ;C++
1231                       "struct"           ;K&R, C89
1232                       "switch"           ;K&R, C89
1233                       "template"         ;C++
1234                       "throw"            ;C++
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         (c-constants
1270          (mdw-regexps "false"           ;C++, C9X macro
1271                       "this"             ;C++
1272                       "true"             ;C++, C9X macro
1273                       ))
1274         (preprocessor-keywords
1275          (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
1276                       "ident" "if" "ifdef" "ifndef" "import" "include"
1277                       "line" "pragma" "unassert" "undef" "warning"))
1278         (objc-keywords
1279          (mdw-regexps "class" "defs" "encode" "end" "implementation"
1280                       "interface" "private" "protected" "protocol" "public"
1281                       "selector")))
1282
1283     (setq font-lock-keywords
1284           (list
1285
1286            ;; Fontify include files as strings.
1287            (list (concat "^[ \t]*\\#[ \t]*"
1288                          "\\(include\\|import\\)"
1289                          "[ \t]*\\(<[^>]+\\(>\\|\\)\\)")
1290                  '(2 font-lock-string-face))
1291
1292            ;; Preprocessor directives are `references'?.
1293            (list (concat "^\\([ \t]*#[ \t]*\\(\\("
1294                          preprocessor-keywords
1295                          "\\)\\>\\|[0-9]+\\|$\\)\\)")
1296                  '(1 font-lock-keyword-face))
1297
1298            ;; Handle the keywords defined above.
1299            (list (concat "@\\<\\(" objc-keywords "\\)\\>")
1300                  '(0 font-lock-keyword-face))
1301
1302            (list (concat "\\<\\(" c-keywords "\\)\\>")
1303                  '(0 font-lock-keyword-face))
1304
1305            (list (concat "\\<\\(" c-constants "\\)\\>")
1306                  '(0 font-lock-variable-name-face))
1307
1308            ;; Handle numbers too.
1309            ;;
1310            ;; This looks strange, I know.  It corresponds to the
1311            ;; preprocessor's idea of what a number looks like, rather than
1312            ;; anything sensible.
1313            (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
1314                          "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
1315                  '(0 mdw-number-face))
1316
1317            ;; And anything else is punctuation.
1318            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1319                  '(0 mdw-punct-face))))
1320
1321     (mdw-post-config-mode-hack)))
1322
1323 ;;;--------------------------------------------------------------------------
1324 ;;; AP calc mode.
1325
1326 (defun apcalc-mode ()
1327   (interactive)
1328   (c-mode)
1329   (setq major-mode 'apcalc-mode)
1330   (setq mode-name "AP Calc")
1331   (run-hooks 'apcalc-mode-hook))
1332
1333 (defun mdw-fontify-apcalc ()
1334
1335   ;; Fiddle with some syntax codes.
1336   (modify-syntax-entry ?* ". 23")
1337   (modify-syntax-entry ?/ ". 14")
1338
1339   ;; Other stuff.
1340   (mdw-c-style)
1341   (setq c-hanging-comment-ender-p nil)
1342   (setq c-backslash-column 72)
1343   (setq comment-start "/* ")
1344   (setq comment-end " */")
1345   (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
1346
1347   ;; Now define things to be fontified.
1348   (make-local-variable 'font-lock-keywords)
1349   (let ((c-keywords
1350          (mdw-regexps "break" "case" "cd" "continue" "define" "default"
1351                       "do" "else" "exit" "for" "global" "goto" "help" "if"
1352                       "local" "mat" "obj" "print" "quit" "read" "return"
1353                       "show" "static" "switch" "while" "write")))
1354
1355     (setq font-lock-keywords
1356           (list
1357
1358            ;; Handle the keywords defined above.
1359            (list (concat "\\<\\(" c-keywords "\\)\\>")
1360                  '(0 font-lock-keyword-face))
1361
1362            ;; Handle numbers too.
1363            ;;
1364            ;; This looks strange, I know.  It corresponds to the
1365            ;; preprocessor's idea of what a number looks like, rather than
1366            ;; anything sensible.
1367            (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
1368                          "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
1369                  '(0 mdw-number-face))
1370
1371            ;; And anything else is punctuation.
1372            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1373                  '(0 mdw-punct-face)))))
1374
1375   (mdw-post-config-mode-hack))
1376
1377 ;;;--------------------------------------------------------------------------
1378 ;;; Java programming configuration.
1379
1380 ;; Make indentation nice.
1381
1382 (defun mdw-java-style ()
1383   (c-add-style "[mdw] Java style"
1384                '((c-basic-offset . 2)
1385                  (c-offsets-alist (substatement-open . 0)
1386                                   (label . +)
1387                                   (case-label . +)
1388                                   (access-label . 0)
1389                                   (inclass . +)
1390                                   (statement-case-intro . +)))
1391                t))
1392
1393 ;; Declare Java fontification style.
1394
1395 (defun mdw-fontify-java ()
1396
1397   ;; Other stuff.
1398   (mdw-java-style)
1399   (setq c-hanging-comment-ender-p nil)
1400   (setq c-backslash-column 72)
1401   (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
1402
1403   ;; Now define things to be fontified.
1404   (make-local-variable 'font-lock-keywords)
1405   (let ((java-keywords
1406          (mdw-regexps "abstract" "boolean" "break" "byte" "case" "catch"
1407                       "char" "class" "const" "continue" "default" "do"
1408                       "double" "else" "extends" "final" "finally" "float"
1409                       "for" "goto" "if" "implements" "import" "instanceof"
1410                       "int" "interface" "long" "native" "new" "package"
1411                       "private" "protected" "public" "return" "short"
1412                       "static" "switch" "synchronized" "throw" "throws"
1413                       "transient" "try" "void" "volatile" "while"))
1414
1415         (java-constants
1416          (mdw-regexps "false" "null" "super" "this" "true")))
1417
1418     (setq font-lock-keywords
1419           (list
1420
1421            ;; Handle the keywords defined above.
1422            (list (concat "\\<\\(" java-keywords "\\)\\>")
1423                  '(0 font-lock-keyword-face))
1424
1425            ;; Handle the magic constants defined above.
1426            (list (concat "\\<\\(" java-constants "\\)\\>")
1427                  '(0 font-lock-variable-name-face))
1428
1429            ;; Handle numbers too.
1430            ;;
1431            ;; The following isn't quite right, but it's close enough.
1432            (list (concat "\\<\\("
1433                          "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
1434                          "[0-9]+\\(\\.[0-9]*\\|\\)"
1435                          "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
1436                          "[lLfFdD]?")
1437                  '(0 mdw-number-face))
1438
1439            ;; And anything else is punctuation.
1440            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1441                  '(0 mdw-punct-face)))))
1442
1443   (mdw-post-config-mode-hack))
1444
1445 ;;;--------------------------------------------------------------------------
1446 ;;; Javascript programming configuration.
1447
1448 (defun mdw-javascript-style ()
1449   (setq js-indent-level 2)
1450   (setq js-expr-indent-offset 0))
1451
1452 (defun mdw-fontify-javascript ()
1453
1454   ;; Other stuff.
1455   (mdw-javascript-style)
1456   (setq js-auto-indent-flag t)
1457
1458   ;; Now define things to be fontified.
1459   (make-local-variable 'font-lock-keywords)
1460   (let ((javascript-keywords
1461          (mdw-regexps "abstract" "boolean" "break" "byte" "case" "catch"
1462                       "char" "class" "const" "continue" "debugger" "default"
1463                       "delete" "do" "double" "else" "enum" "export" "extends"
1464                       "final" "finally" "float" "for" "function" "goto" "if"
1465                       "implements" "import" "in" "instanceof" "int"
1466                       "interface" "let" "long" "native" "new" "package"
1467                       "private" "protected" "public" "return" "short"
1468                       "static" "super" "switch" "synchronized" "throw"
1469                       "throws" "transient" "try" "typeof" "var" "void"
1470                       "volatile" "while" "with" "yield"
1471
1472                       "boolean" "byte" "char" "double" "float" "int" "long"
1473                       "short" "void"))
1474         (javascript-constants
1475          (mdw-regexps "false" "null" "undefined" "Infinity" "NaN" "true"
1476                       "arguments" "this")))
1477
1478     (setq font-lock-keywords
1479           (list
1480
1481            ;; Handle the keywords defined above.
1482            (list (concat "\\_<\\(" javascript-keywords "\\)\\_>")
1483                  '(0 font-lock-keyword-face))
1484
1485            ;; Handle the predefined constants defined above.
1486            (list (concat "\\_<\\(" javascript-constants "\\)\\_>")
1487                  '(0 font-lock-variable-name-face))
1488
1489            ;; Handle numbers too.
1490            ;;
1491            ;; The following isn't quite right, but it's close enough.
1492            (list (concat "\\_<\\("
1493                          "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
1494                          "[0-9]+\\(\\.[0-9]*\\|\\)"
1495                          "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
1496                          "[lLfFdD]?")
1497                  '(0 mdw-number-face))
1498
1499            ;; And anything else is punctuation.
1500            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1501                  '(0 mdw-punct-face)))))
1502
1503   (mdw-post-config-mode-hack))
1504
1505 ;;;--------------------------------------------------------------------------
1506 ;;; Scala programming configuration.
1507
1508 (defun mdw-fontify-scala ()
1509
1510   ;; Define things to be fontified.
1511   (make-local-variable 'font-lock-keywords)
1512   (let ((scala-keywords
1513          (mdw-regexps "abstract" "case" "catch" "class" "def" "do" "else"
1514                       "extends" "final" "finally" "for" "forSome" "if"
1515                       "implicit" "import" "lazy" "match" "new" "object"
1516                       "override" "package" "private" "protected" "return"
1517                       "sealed" "throw" "trait" "try" "type" "val"
1518                       "var" "while" "with" "yield"))
1519         (scala-constants
1520          (mdw-regexps "false" "null" "super" "this" "true"))
1521         (punctuation "\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/"))
1522
1523     (setq font-lock-keywords
1524           (list
1525
1526            ;; Magical identifiers between backticks.
1527            (list (concat "`\\([^`]+\\)`")
1528                  '(1 font-lock-variable-name-face))
1529
1530            ;; Handle the keywords defined above.
1531            (list (concat "\\_<\\(" scala-keywords "\\)\\_>")
1532                  '(0 font-lock-keyword-face))
1533
1534            ;; Handle the constants defined above.
1535            (list (concat "\\_<\\(" scala-constants "\\)\\_>")
1536                  '(0 font-lock-variable-name-face))
1537
1538            ;; Magical identifiers between backticks.
1539            (list (concat "`\\([^`]+\\)`")
1540                  '(1 font-lock-variable-name-face))
1541
1542            ;; Handle numbers too.
1543            ;;
1544            ;; As usual, not quite right.
1545            (list (concat "\\_<\\("
1546                          "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
1547                          "[0-9]+\\(\\.[0-9]*\\|\\)"
1548                          "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
1549                          "[lLfFdD]?")
1550                  '(0 mdw-number-face))
1551
1552            ;; Identifiers with trailing operators.
1553            (list (concat "_\\(" punctuation "\\)+")
1554                  '(0 mdw-trivial-face))
1555
1556            ;; And everything else is punctuation.
1557            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1558                  '(0 mdw-punct-face)))
1559
1560           font-lock-syntactic-keywords
1561           (list
1562
1563            ;; Single quotes around characters.  But not when used to quote
1564            ;; symbol names.  Ugh.
1565            (list (concat "\\('\\)"
1566                          "\\(" "."
1567                          "\\|" "\\\\" "\\(" "\\\\\\\\" "\\)*"
1568                                "u+" "[0-9a-fA-F]\\{4\\}"
1569                          "\\|" "\\\\" "[0-7]\\{1,3\\}"
1570                          "\\|" "\\\\" "." "\\)"
1571                          "\\('\\)")
1572                  '(1 "\"")
1573                  '(4 "\"")))))
1574
1575   (mdw-post-config-mode-hack))
1576
1577 ;;;--------------------------------------------------------------------------
1578 ;;; C# programming configuration.
1579
1580 ;; Make indentation nice.
1581
1582 (defun mdw-csharp-style ()
1583   (c-add-style "[mdw] C# style"
1584                '((c-basic-offset . 2)
1585                  (c-offsets-alist (substatement-open . 0)
1586                                   (label . 0)
1587                                   (case-label . +)
1588                                   (access-label . 0)
1589                                   (inclass . +)
1590                                   (statement-case-intro . +)))
1591                t))
1592
1593 ;; Declare C# fontification style.
1594
1595 (defun mdw-fontify-csharp ()
1596
1597   ;; Other stuff.
1598   (mdw-csharp-style)
1599   (setq c-hanging-comment-ender-p nil)
1600   (setq c-backslash-column 72)
1601   (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
1602
1603   ;; Now define things to be fontified.
1604   (make-local-variable 'font-lock-keywords)
1605   (let ((csharp-keywords
1606          (mdw-regexps "abstract" "as" "bool" "break" "byte" "case" "catch"
1607                       "char" "checked" "class" "const" "continue" "decimal"
1608                       "default" "delegate" "do" "double" "else" "enum"
1609                       "event" "explicit" "extern" "finally" "fixed" "float"
1610                       "for" "foreach" "goto" "if" "implicit" "in" "int"
1611                       "interface" "internal" "is" "lock" "long" "namespace"
1612                       "new" "object" "operator" "out" "override" "params"
1613                       "private" "protected" "public" "readonly" "ref"
1614                       "return" "sbyte" "sealed" "short" "sizeof"
1615                       "stackalloc" "static" "string" "struct" "switch"
1616                       "throw" "try" "typeof" "uint" "ulong" "unchecked"
1617                       "unsafe" "ushort" "using" "virtual" "void" "volatile"
1618                       "while" "yield"))
1619
1620         (csharp-constants
1621          (mdw-regexps "base" "false" "null" "this" "true")))
1622
1623     (setq font-lock-keywords
1624           (list
1625
1626            ;; Handle the keywords defined above.
1627            (list (concat "\\<\\(" csharp-keywords "\\)\\>")
1628                  '(0 font-lock-keyword-face))
1629
1630            ;; Handle the magic constants defined above.
1631            (list (concat "\\<\\(" csharp-constants "\\)\\>")
1632                  '(0 font-lock-variable-name-face))
1633
1634            ;; Handle numbers too.
1635            ;;
1636            ;; The following isn't quite right, but it's close enough.
1637            (list (concat "\\<\\("
1638                          "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
1639                          "[0-9]+\\(\\.[0-9]*\\|\\)"
1640                          "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
1641                          "[lLfFdD]?")
1642                  '(0 mdw-number-face))
1643
1644            ;; And anything else is punctuation.
1645            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1646                  '(0 mdw-punct-face)))))
1647
1648   (mdw-post-config-mode-hack))
1649
1650 (define-derived-mode csharp-mode java-mode "C#"
1651   "Major mode for editing C# code.")
1652
1653 ;;;--------------------------------------------------------------------------
1654 ;;; F# programming configuration.
1655
1656 (setq fsharp-indent-offset 2)
1657
1658 (defun mdw-fontify-fsharp ()
1659
1660   (let ((punct "=<>+-*/|&%!@?"))
1661     (do ((i 0 (1+ i)))
1662         ((>= i (length punct)))
1663       (modify-syntax-entry (aref punct i) ".")))
1664
1665   (modify-syntax-entry ?_ "_")
1666   (modify-syntax-entry ?( "(")
1667   (modify-syntax-entry ?) ")")
1668
1669   (setq indent-tabs-mode nil)
1670
1671   (let ((fsharp-keywords
1672          (mdw-regexps "abstract" "and" "as" "assert" "atomic"
1673                       "begin" "break"
1674                       "checked" "class" "component" "const" "constraint"
1675                       "constructor" "continue"
1676                       "default" "delegate" "do" "done" "downcast" "downto"
1677                       "eager" "elif" "else" "end" "exception" "extern"
1678                       "finally" "fixed" "for" "fori" "fun" "function"
1679                       "functor"
1680                       "global"
1681                       "if" "in" "include" "inherit" "inline" "interface"
1682                       "internal"
1683                       "lazy" "let"
1684                       "match" "measure" "member" "method" "mixin" "module"
1685                       "mutable"
1686                       "namespace" "new"
1687                       "object" "of" "open" "or" "override"
1688                       "parallel" "params" "private" "process" "protected"
1689                       "public" "pure"
1690                       "rec" "recursive" "return"
1691                       "sealed" "sig" "static" "struct"
1692                       "tailcall" "then" "to" "trait" "try" "type"
1693                       "upcast" "use"
1694                       "val" "virtual" "void" "volatile"
1695                       "when" "while" "with"
1696                       "yield"))
1697
1698         (fsharp-builtins
1699          (mdw-regexps "asr" "land" "lor" "lsl" "lsr" "lxor" "mod"
1700                       "base" "false" "null" "true"))
1701
1702         (bang-keywords
1703          (mdw-regexps "do" "let" "return" "use" "yield"))
1704
1705         (preprocessor-keywords
1706          (mdw-regexps "if" "indent" "else" "endif")))
1707
1708     (setq font-lock-keywords
1709           (list (list (concat "\\(^\\|[^\"]\\)"
1710                               "\\(" "(\\*"
1711                                     "[^*]*\\*+"
1712                                     "\\(" "[^)*]" "[^*]*" "\\*+" "\\)*"
1713                                     ")"
1714                               "\\|"
1715                                     "//.*"
1716                               "\\)")
1717                       '(2 font-lock-comment-face))
1718
1719                 (list (concat "'" "\\("
1720                                     "\\\\"
1721                                     "\\(" "[ntbr'\\]"
1722                                     "\\|" "[0-9][0-9][0-9]"
1723                                     "\\|" "u" "[0-9a-fA-F]\\{4\\}"
1724                                     "\\|" "U" "[0-9a-fA-F]\\{8\\}"
1725                                     "\\)"
1726                                   "\\|"
1727                                   "." "\\)" "'"
1728                               "\\|"
1729                               "\"" "[^\"\\]*"
1730                                     "\\(" "\\\\" "\\(.\\|\n\\)"
1731                                           "[^\"\\]*" "\\)*"
1732                               "\\(\"\\|\\'\\)")
1733                       '(0 font-lock-string-face))
1734
1735                 (list (concat "\\_<\\(" bang-keywords "\\)!" "\\|"
1736                               "^#[ \t]*\\(" preprocessor-keywords "\\)\\_>"
1737                               "\\|"
1738                               "\\_<\\(" fsharp-keywords "\\)\\_>")
1739                       '(0 font-lock-keyword-face))
1740                 (list (concat "\\<\\(" fsharp-builtins "\\)\\_>")
1741                       '(0 font-lock-variable-name-face))
1742
1743                 (list (concat "\\_<"
1744                               "\\(" "0[bB][01]+" "\\|"
1745                                     "0[oO][0-7]+" "\\|"
1746                                     "0[xX][0-9a-fA-F]+" "\\)"
1747                               "\\(" "lf\\|LF" "\\|"
1748                                     "[uU]?[ysnlL]?" "\\)"
1749                               "\\|"
1750                               "\\_<"
1751                               "[0-9]+" "\\("
1752                                 "[mMQRZING]"
1753                                 "\\|"
1754                                 "\\(\\.[0-9]*\\)?"
1755                                 "\\([eE][-+]?[0-9]+\\)?"
1756                                 "[fFmM]?"
1757                                 "\\|"
1758                                 "[uU]?[ysnlL]?"
1759                               "\\)")
1760                       '(0 mdw-number-face))
1761
1762                 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1763                       '(0 mdw-punct-face)))))
1764
1765   (mdw-post-config-mode-hack))
1766
1767 (defun mdw-fontify-inferior-fsharp ()
1768   (mdw-fontify-fsharp)
1769   (setq font-lock-keywords
1770         (append (list (list "^[#-]" '(0 font-lock-comment-face))
1771                       (list "^>" '(0 font-lock-keyword-face)))
1772                 font-lock-keywords)))
1773
1774 ;;;--------------------------------------------------------------------------
1775 ;;; Go programming configuration.
1776
1777 (defun mdw-fontify-go ()
1778
1779   (make-local-variable 'font-lock-keywords)
1780   (let ((go-keywords
1781          (mdw-regexps "break" "case" "chan" "const" "continue"
1782                       "default" "defer" "else" "fallthrough" "for"
1783                       "func" "go" "goto" "if" "import"
1784                       "interface" "map" "package" "range" "return"
1785                       "select" "struct" "switch" "type" "var"))
1786         (go-intrinsics
1787          (mdw-regexps "bool" "byte" "complex64" "complex128" "error"
1788                       "float32" "float64" "int" "uint8" "int16" "int32"
1789                       "int64" "rune" "string" "uint" "uint8" "uint16"
1790                       "uint32" "uint64" "uintptr" "void"
1791                       "false" "iota" "nil" "true"
1792                       "init" "main"
1793                       "append" "cap" "copy" "delete" "imag" "len" "make"
1794                       "new" "panic" "real" "recover")))
1795
1796     (setq font-lock-keywords
1797           (list
1798
1799            ;; Handle the keywords defined above.
1800            (list (concat "\\<\\(" go-keywords "\\)\\>")
1801                  '(0 font-lock-keyword-face))
1802            (list (concat "\\<\\(" go-intrinsics "\\)\\>")
1803                  '(0 font-lock-variable-name-face))
1804
1805            ;; Strings and characters.
1806            (list (concat "'"
1807                          "\\(" "[^\\']" "\\|"
1808                                "\\\\"
1809                                "\\(" "[abfnrtv\\'\"]" "\\|"
1810                                      "[0-7]\\{3\\}" "\\|"
1811                                      "x" "[0-9A-Fa-f]\\{2\\}" "\\|"
1812                                      "u" "[0-9A-Fa-f]\\{4\\}" "\\|"
1813                                      "U" "[0-9A-Fa-f]\\{8\\}" "\\)" "\\)"
1814                          "'"
1815                          "\\|"
1816                          "\""
1817                          "\\(" "[^\n\\\"]+" "\\|" "\\\\." "\\)*"
1818                          "\\(\"\\|$\\)"
1819                          "\\|"
1820                          "`" "[^`]+" "`")
1821                  '(0 font-lock-string-face))
1822
1823            ;; Handle numbers too.
1824            ;;
1825            ;; The following isn't quite right, but it's close enough.
1826            (list (concat "\\<\\("
1827                          "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
1828                          "[0-9]+\\(\\.[0-9]*\\|\\)"
1829                          "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)")
1830                  '(0 mdw-number-face))
1831
1832            ;; And anything else is punctuation.
1833            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1834                  '(0 mdw-punct-face)))))
1835
1836   (mdw-post-config-mode-hack))
1837
1838 ;;;--------------------------------------------------------------------------
1839 ;;; Awk programming configuration.
1840
1841 ;; Make Awk indentation nice.
1842
1843 (defun mdw-awk-style ()
1844   (c-add-style "[mdw] Awk style"
1845                '((c-basic-offset . 2)
1846                  (c-offsets-alist (substatement-open . 0)
1847                                   (statement-cont . 0)
1848                                   (statement-case-intro . +)))
1849                t))
1850
1851 ;; Declare Awk fontification style.
1852
1853 (defun mdw-fontify-awk ()
1854
1855   ;; Miscellaneous fiddling.
1856   (mdw-awk-style)
1857   (setq c-backslash-column 72)
1858   (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
1859
1860   ;; Now define things to be fontified.
1861   (make-local-variable 'font-lock-keywords)
1862   (let ((c-keywords
1863          (mdw-regexps "BEGIN" "END" "ARGC" "ARGIND" "ARGV" "CONVFMT"
1864                       "ENVIRON" "ERRNO" "FIELDWIDTHS" "FILENAME" "FNR"
1865                       "FS" "IGNORECASE" "NF" "NR" "OFMT" "OFS" "ORS" "RS"
1866                       "RSTART" "RLENGTH" "RT"   "SUBSEP"
1867                       "atan2" "break" "close" "continue" "cos" "delete"
1868                       "do" "else" "exit" "exp" "fflush" "file" "for" "func"
1869                       "function" "gensub" "getline" "gsub" "if" "in"
1870                       "index" "int" "length" "log" "match" "next" "rand"
1871                       "return" "print" "printf" "sin" "split" "sprintf"
1872                       "sqrt" "srand" "strftime" "sub" "substr" "system"
1873                       "systime" "tolower" "toupper" "while")))
1874
1875     (setq font-lock-keywords
1876           (list
1877
1878            ;; Handle the keywords defined above.
1879            (list (concat "\\<\\(" c-keywords "\\)\\>")
1880                  '(0 font-lock-keyword-face))
1881
1882            ;; Handle numbers too.
1883            ;;
1884            ;; The following isn't quite right, but it's close enough.
1885            (list (concat "\\<\\("
1886                          "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
1887                          "[0-9]+\\(\\.[0-9]*\\|\\)"
1888                          "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
1889                          "[uUlL]*")
1890                  '(0 mdw-number-face))
1891
1892            ;; And anything else is punctuation.
1893            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1894                  '(0 mdw-punct-face)))))
1895
1896   (mdw-post-config-mode-hack))
1897
1898 ;;;--------------------------------------------------------------------------
1899 ;;; Perl programming style.
1900
1901 ;; Perl indentation style.
1902
1903 (fset 'perl-mode 'cperl-mode)
1904 (setq cperl-indent-level 2)
1905 (setq cperl-continued-statement-offset 2)
1906 (setq cperl-continued-brace-offset 0)
1907 (setq cperl-brace-offset -2)
1908 (setq cperl-brace-imaginary-offset 0)
1909 (setq cperl-label-offset 0)
1910
1911 ;; Define perl fontification style.
1912
1913 (defun mdw-fontify-perl ()
1914
1915   ;; Miscellaneous fiddling.
1916   (modify-syntax-entry ?$ "\\")
1917   (modify-syntax-entry ?$ "\\" font-lock-syntax-table)
1918   (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
1919
1920   ;; Now define fontification things.
1921   (make-local-variable 'font-lock-keywords)
1922   (let ((perl-keywords
1923          (mdw-regexps "and" "break" "cmp" "continue" "do" "else" "elsif" "eq"
1924                       "for" "foreach" "ge" "given" "gt" "goto" "if"
1925                       "last" "le" "lt" "local" "my" "ne" "next" "or"
1926                       "our" "package" "redo" "require" "return" "sub"
1927                       "undef" "unless" "until" "use" "when" "while")))
1928
1929     (setq font-lock-keywords
1930           (list
1931
1932            ;; Set up the keywords defined above.
1933            (list (concat "\\<\\(" perl-keywords "\\)\\>")
1934                  '(0 font-lock-keyword-face))
1935
1936            ;; At least numbers are simpler than C.
1937            (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
1938                          "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)"
1939                          "\\([eE]\\([-+]\\|\\)[0-9_]+\\|\\)")
1940                  '(0 mdw-number-face))
1941
1942            ;; And anything else is punctuation.
1943            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1944                  '(0 mdw-punct-face)))))
1945
1946   (mdw-post-config-mode-hack))
1947
1948 (defun perl-number-tests (&optional arg)
1949   "Assign consecutive numbers to lines containing `#t'.  With ARG,
1950 strip numbers instead."
1951   (interactive "P")
1952   (save-excursion
1953     (goto-char (point-min))
1954     (let ((i 0) (fmt (if arg "" " %4d")))
1955       (while (search-forward "#t" nil t)
1956         (delete-region (point) (line-end-position))
1957         (setq i (1+ i))
1958         (insert (format fmt i)))
1959       (goto-char (point-min))
1960       (if (re-search-forward "\\(tests\\s-*=>\\s-*\\)\\w*" nil t)
1961           (replace-match (format "\\1%d" i))))))
1962
1963 ;;;--------------------------------------------------------------------------
1964 ;;; Python programming style.
1965
1966 (defun mdw-fontify-pythonic (keywords)
1967
1968   ;; Miscellaneous fiddling.
1969   (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
1970   (setq indent-tabs-mode nil)
1971
1972   ;; Now define fontification things.
1973   (make-local-variable 'font-lock-keywords)
1974   (setq font-lock-keywords
1975         (list
1976
1977          ;; Set up the keywords defined above.
1978          (list (concat "\\_<\\(" keywords "\\)\\_>")
1979                '(0 font-lock-keyword-face))
1980
1981          ;; At least numbers are simpler than C.
1982          (list (concat "\\_<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
1983                        "\\_<[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)"
1984                        "\\([eE]\\([-+]\\|\\)[0-9_]+\\|[lL]\\|\\)")
1985                '(0 mdw-number-face))
1986
1987          ;; And anything else is punctuation.
1988          (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1989                '(0 mdw-punct-face))))
1990
1991   (mdw-post-config-mode-hack))
1992
1993 ;; Define Python fontification styles.
1994
1995 (defun mdw-fontify-python ()
1996   (mdw-fontify-pythonic
1997    (mdw-regexps "and" "as" "assert" "break" "class" "continue" "def"
1998                 "del" "elif" "else" "except" "exec" "finally" "for"
1999                 "from" "global" "if" "import" "in" "is" "lambda"
2000                 "not" "or" "pass" "print" "raise" "return" "try"
2001                 "while" "with" "yield")))
2002
2003 (defun mdw-fontify-pyrex ()
2004   (mdw-fontify-pythonic
2005    (mdw-regexps "and" "as" "assert" "break" "cdef" "class" "continue"
2006                 "ctypedef" "def" "del" "elif" "else" "except" "exec"
2007                 "extern" "finally" "for" "from" "global" "if"
2008                 "import" "in" "is" "lambda" "not" "or" "pass" "print"
2009                 "raise" "return" "struct" "try" "while" "with"
2010                 "yield")))
2011
2012 ;;;--------------------------------------------------------------------------
2013 ;;; Icon programming style.
2014
2015 ;; Icon indentation style.
2016
2017 (setq icon-brace-offset 0
2018       icon-continued-brace-offset 0
2019       icon-continued-statement-offset 2
2020       icon-indent-level 2)
2021
2022 ;; Define Icon fontification style.
2023
2024 (defun mdw-fontify-icon ()
2025
2026   ;; Miscellaneous fiddling.
2027   (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
2028
2029   ;; Now define fontification things.
2030   (make-local-variable 'font-lock-keywords)
2031   (let ((icon-keywords
2032          (mdw-regexps "break" "by" "case" "create" "default" "do" "else"
2033                       "end" "every" "fail" "global" "if" "initial"
2034                       "invocable" "link" "local" "next" "not" "of"
2035                       "procedure" "record" "repeat" "return" "static"
2036                       "suspend" "then" "to" "until" "while"))
2037         (preprocessor-keywords
2038          (mdw-regexps "define" "else" "endif" "error" "ifdef" "ifndef"
2039                       "include" "line" "undef")))
2040     (setq font-lock-keywords
2041           (list
2042
2043            ;; Set up the keywords defined above.
2044            (list (concat "\\<\\(" icon-keywords "\\)\\>")
2045                  '(0 font-lock-keyword-face))
2046
2047            ;; The things that Icon calls keywords.
2048            (list "&\\sw+\\>" '(0 font-lock-variable-name-face))
2049
2050            ;; At least numbers are simpler than C.
2051            (list (concat "\\<[0-9]+"
2052                          "\\([rR][0-9a-zA-Z]+\\|"
2053                          "\\.[0-9]+\\([eE][+-]?[0-9]+\\)?\\)\\>\\|"
2054                          "\\.[0-9]+\\([eE][+-]?[0-9]+\\)?\\>")
2055                  '(0 mdw-number-face))
2056
2057            ;; Preprocessor.
2058            (list (concat "^[ \t]*$[ \t]*\\<\\("
2059                          preprocessor-keywords
2060                          "\\)\\>")
2061                  '(0 font-lock-keyword-face))
2062
2063            ;; And anything else is punctuation.
2064            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2065                  '(0 mdw-punct-face)))))
2066
2067   (mdw-post-config-mode-hack))
2068
2069 ;;;--------------------------------------------------------------------------
2070 ;;; ARM assembler programming configuration.
2071
2072 ;; There doesn't appear to be an Emacs mode for this yet.
2073 ;;
2074 ;; Better do something about that, I suppose.
2075
2076 (defvar arm-assembler-mode-map nil)
2077 (defvar arm-assembler-abbrev-table nil)
2078 (defvar arm-assembler-mode-syntax-table (make-syntax-table))
2079
2080 (or arm-assembler-mode-map
2081     (progn
2082       (setq arm-assembler-mode-map (make-sparse-keymap))
2083       (define-key arm-assembler-mode-map "\C-m" 'arm-assembler-newline)
2084       (define-key arm-assembler-mode-map [C-return] 'newline)
2085       (define-key arm-assembler-mode-map "\t" 'tab-to-tab-stop)))
2086
2087 (defun arm-assembler-mode ()
2088   "Major mode for ARM assembler programs"
2089   (interactive)
2090
2091   ;; Do standard major mode things.
2092   (kill-all-local-variables)
2093   (use-local-map arm-assembler-mode-map)
2094   (setq local-abbrev-table arm-assembler-abbrev-table)
2095   (setq major-mode 'arm-assembler-mode)
2096   (setq mode-name "ARM assembler")
2097
2098   ;; Set up syntax table.
2099   (set-syntax-table arm-assembler-mode-syntax-table)
2100   (modify-syntax-entry ?;   ; Nasty hack
2101                        "<" arm-assembler-mode-syntax-table)
2102   (modify-syntax-entry ?\n ">" arm-assembler-mode-syntax-table)
2103   (modify-syntax-entry ?_ "_" arm-assembler-mode-syntax-table)
2104   (modify-syntax-entry ?' "\"'" arm-assembler-mode-syntax-table)
2105
2106   (make-local-variable 'comment-start)
2107   (setq comment-start ";")
2108   (make-local-variable 'comment-end)
2109   (setq comment-end "")
2110   (make-local-variable 'comment-column)
2111   (setq comment-column 48)
2112   (make-local-variable 'comment-start-skip)
2113   (setq comment-start-skip ";+[ \t]*")
2114
2115   ;; Play with indentation.
2116   (make-local-variable 'indent-line-function)
2117   (setq indent-line-function 'indent-relative-maybe)
2118
2119   ;; Set fill prefix.
2120   (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)")
2121
2122   ;; Fiddle with fontification.
2123   (make-local-variable 'font-lock-keywords)
2124   (setq font-lock-keywords
2125         (list
2126
2127          ;; Handle numbers too.
2128          ;;
2129          ;; The following isn't quite right, but it's close enough.
2130          (list (concat "\\("
2131                        "&[0-9a-fA-F]+\\|"
2132                        "\\<[0-9]+\\(\\.[0-9]*\\|_[0-9a-zA-Z]+\\|\\)"
2133                        "\\)")
2134                '(0 mdw-number-face))
2135
2136          ;; Do something about operators.
2137          (list "^[^ \t]*[ \t]+\\(GET\\|LNK\\)[ \t]+\\([^;\n]*\\)"
2138                '(1 font-lock-keyword-face)
2139                '(2 font-lock-string-face))
2140          (list ":[a-zA-Z]+:"
2141                '(0 font-lock-keyword-face))
2142
2143          ;; Do menemonics and directives.
2144          (list "^[^ \t]*[ \t]+\\([a-zA-Z]+\\)"
2145                '(1 font-lock-keyword-face))
2146
2147          ;; And anything else is punctuation.
2148          (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2149                '(0 mdw-punct-face))))
2150
2151   (mdw-post-config-mode-hack)
2152   (run-hooks 'arm-assembler-mode-hook))
2153
2154 ;;;--------------------------------------------------------------------------
2155 ;;; Assembler mode.
2156
2157 (defun mdw-fontify-asm ()
2158   (modify-syntax-entry ?' "\"")
2159   (modify-syntax-entry ?. "w")
2160   (setf fill-prefix nil)
2161   (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)"))
2162
2163 ;;;--------------------------------------------------------------------------
2164 ;;; TCL configuration.
2165
2166 (defun mdw-fontify-tcl ()
2167   (mapcar #'(lambda (ch) (modify-syntax-entry ch ".")) '(?$))
2168   (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
2169   (make-local-variable 'font-lock-keywords)
2170   (setq font-lock-keywords
2171         (list
2172          (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
2173                        "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)"
2174                        "\\([eE]\\([-+]\\|\\)[0-9_]+\\|\\)")
2175                '(0 mdw-number-face))
2176          (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2177                '(0 mdw-punct-face))))
2178   (mdw-post-config-mode-hack))
2179
2180 ;;;--------------------------------------------------------------------------
2181 ;;; Dylan programming configuration.
2182
2183 (defun mdw-fontify-dylan ()
2184
2185   (make-local-variable 'font-lock-keywords)
2186
2187   ;; Horrors.  `dylan-mode' sets the `major-mode' name after calling this
2188   ;; hook, which undoes all of our configuration.
2189   (setq major-mode 'dylan-mode)
2190   (font-lock-set-defaults)
2191
2192   (let* ((word "[-_a-zA-Z!*@<>$%]+")
2193          (dylan-keywords (mdw-regexps
2194
2195                           "C-address" "C-callable-wrapper" "C-function"
2196                           "C-mapped-subtype" "C-pointer-type" "C-struct"
2197                           "C-subtype" "C-union" "C-variable"
2198
2199                           "above" "abstract" "afterwards" "all"
2200                           "begin" "below" "block" "by"
2201                           "case" "class" "cleanup" "constant" "create"
2202                           "define" "domain"
2203                           "else" "elseif" "end" "exception" "export"
2204                           "finally" "for" "from" "function"
2205                           "generic"
2206                           "handler"
2207                           "if" "in" "instance" "interface" "iterate"
2208                           "keyed-by"
2209                           "let" "library" "local"
2210                           "macro" "method" "module"
2211                           "otherwise"
2212                           "profiling"
2213                           "select" "slot" "subclass"
2214                           "table" "then" "to"
2215                           "unless" "until" "use"
2216                           "variable" "virtual"
2217                           "when" "while"))
2218          (sharp-keywords (mdw-regexps
2219                           "all-keys" "key" "next" "rest" "include"
2220                           "t" "f")))
2221     (setq font-lock-keywords
2222           (list (list (concat "\\<\\(" dylan-keywords
2223                               "\\|" "with\\(out\\)?-" word
2224                               "\\)\\>")
2225                       '(0 font-lock-keyword-face))
2226                 (list (concat "\\<" word ":" "\\|"
2227                               "#\\(" sharp-keywords "\\)\\>")
2228                       '(0 font-lock-variable-name-face))
2229                 (list (concat "\\("
2230                               "\\([-+]\\|\\<\\)[0-9]+" "\\("
2231                                 "\\(\\.[0-9]+\\)?" "\\([eE][-+][0-9]+\\)?"
2232                                 "\\|" "/[0-9]+"
2233                               "\\)"
2234                               "\\|" "\\.[0-9]+" "\\([eE][-+][0-9]+\\)?"
2235                               "\\|" "#b[01]+"
2236                               "\\|" "#o[0-7]+"
2237                               "\\|" "#x[0-9a-zA-Z]+"
2238                               "\\)\\>")
2239                       '(0 mdw-number-face))
2240                 (list (concat "\\("
2241                               "\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\|"
2242                               "\\_<[-+*/=<>:&|]+\\_>"
2243                               "\\)")
2244                       '(0 mdw-punct-face)))))
2245
2246   (mdw-post-config-mode-hack))
2247
2248 ;;;--------------------------------------------------------------------------
2249 ;;; Algol 68 configuration.
2250
2251 (setq a68-indent-step 2)
2252
2253 (defun mdw-fontify-algol-68 ()
2254
2255   ;; Fix up the syntax table.
2256   (modify-syntax-entry ?# "!" a68-mode-syntax-table)
2257   (dolist (ch '(?- ?+ ?= ?< ?> ?* ?/ ?| ?&))
2258     (modify-syntax-entry ch "." a68-mode-syntax-table))
2259
2260   (make-local-variable 'font-lock-keywords)
2261
2262   (let ((not-comment
2263          (let ((word "COMMENT"))
2264            (do ((regexp (concat "[^" (substring word 0 1) "]+")
2265                         (concat regexp "\\|"
2266                                 (substring word 0 i)
2267                                 "[^" (substring word i (1+ i)) "]"))
2268                 (i 1 (1+ i)))
2269                ((>= i (length word)) regexp)))))
2270     (setq font-lock-keywords
2271           (list (list (concat "\\<COMMENT\\>"
2272                               "\\(" not-comment "\\)\\{0,5\\}"
2273                               "\\(\\'\\|\\<COMMENT\\>\\)")
2274                       '(0 font-lock-comment-face))
2275                 (list (concat "\\<CO\\>"
2276                               "\\([^C]+\\|C[^O]\\)\\{0,5\\}"
2277                               "\\($\\|\\<CO\\>\\)")
2278                       '(0 font-lock-comment-face))
2279                 (list "\\<[A-Z_]+\\>"
2280                       '(0 font-lock-keyword-face))
2281                 (list (concat "\\<"
2282                               "[0-9]+"
2283                               "\\(\\.[0-9]+\\)?"
2284                               "\\([eE][-+]?[0-9]+\\)?"
2285                               "\\>")
2286                       '(0 mdw-number-face))
2287                 (list "\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/"
2288                       '(0 mdw-punct-face)))))
2289
2290   (mdw-post-config-mode-hack))
2291
2292 ;;;--------------------------------------------------------------------------
2293 ;;; REXX configuration.
2294
2295 (defun mdw-rexx-electric-* ()
2296   (interactive)
2297   (insert ?*)
2298   (rexx-indent-line))
2299
2300 (defun mdw-rexx-indent-newline-indent ()
2301   (interactive)
2302   (rexx-indent-line)
2303   (if abbrev-mode (expand-abbrev))
2304   (newline-and-indent))
2305
2306 (defun mdw-fontify-rexx ()
2307
2308   ;; Various bits of fiddling.
2309   (setq mdw-auto-indent nil)
2310   (local-set-key [?\C-m] 'mdw-rexx-indent-newline-indent)
2311   (local-set-key [?*] 'mdw-rexx-electric-*)
2312   (mapcar #'(lambda (ch) (modify-syntax-entry ch "w"))
2313           '(?! ?? ?# ?@ ?$))
2314   (mdw-standard-fill-prefix "\\([ \t]*/?\*[ \t]*\\)")
2315
2316   ;; Set up keywords and things for fontification.
2317   (make-local-variable 'font-lock-keywords-case-fold-search)
2318   (setq font-lock-keywords-case-fold-search t)
2319
2320   (setq rexx-indent 2)
2321   (setq rexx-end-indent rexx-indent)
2322   (setq rexx-cont-indent rexx-indent)
2323
2324   (make-local-variable 'font-lock-keywords)
2325   (let ((rexx-keywords
2326          (mdw-regexps "address" "arg" "by" "call" "digits" "do" "drop"
2327                       "else" "end" "engineering" "exit" "expose" "for"
2328                       "forever" "form" "fuzz" "if" "interpret" "iterate"
2329                       "leave" "linein" "name" "nop" "numeric" "off" "on"
2330                       "options" "otherwise" "parse" "procedure" "pull"
2331                       "push" "queue" "return" "say" "select" "signal"
2332                       "scientific" "source" "then" "trace" "to" "until"
2333                       "upper" "value" "var" "version" "when" "while"
2334                       "with"
2335
2336                       "abbrev" "abs" "bitand" "bitor" "bitxor" "b2x"
2337                       "center" "center" "charin" "charout" "chars"
2338                       "compare" "condition" "copies" "c2d" "c2x"
2339                       "datatype" "date" "delstr" "delword" "d2c" "d2x"
2340                       "errortext" "format" "fuzz" "insert" "lastpos"
2341                       "left" "length" "lineout" "lines" "max" "min"
2342                       "overlay" "pos" "queued" "random" "reverse" "right"
2343                       "sign" "sourceline" "space" "stream" "strip"
2344                       "substr" "subword" "symbol" "time" "translate"
2345                       "trunc" "value" "verify" "word" "wordindex"
2346                       "wordlength" "wordpos" "words" "xrange" "x2b" "x2c"
2347                       "x2d")))
2348
2349     (setq font-lock-keywords
2350           (list
2351
2352            ;; Set up the keywords defined above.
2353            (list (concat "\\<\\(" rexx-keywords "\\)\\>")
2354                  '(0 font-lock-keyword-face))
2355
2356            ;; Fontify all symbols the same way.
2357            (list (concat "\\<\\([0-9.][A-Za-z0-9.!?_#@$]*[Ee][+-]?[0-9]+\\|"
2358                          "[A-Za-z0-9.!?_#@$]+\\)")
2359                  '(0 font-lock-variable-name-face))
2360
2361            ;; And everything else is punctuation.
2362            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2363                  '(0 mdw-punct-face)))))
2364
2365   (mdw-post-config-mode-hack))
2366
2367 ;;;--------------------------------------------------------------------------
2368 ;;; Standard ML programming style.
2369
2370 (defun mdw-fontify-sml ()
2371
2372   ;; Make underscore an honorary letter.
2373   (modify-syntax-entry ?' "w")
2374
2375   ;; Set fill prefix.
2376   (mdw-standard-fill-prefix "\\([ \t]*(\*[ \t]*\\)")
2377
2378   ;; Now define fontification things.
2379   (make-local-variable 'font-lock-keywords)
2380   (let ((sml-keywords
2381          (mdw-regexps "abstype" "and" "andalso" "as"
2382                       "case"
2383                       "datatype" "do"
2384                       "else" "end" "eqtype" "exception"
2385                       "fn" "fun" "functor"
2386                       "handle"
2387                       "if" "in" "include" "infix" "infixr"
2388                       "let" "local"
2389                       "nonfix"
2390                       "of" "op" "open" "orelse"
2391                       "raise" "rec"
2392                       "sharing" "sig" "signature" "struct" "structure"
2393                       "then" "type"
2394                       "val"
2395                       "where" "while" "with" "withtype")))
2396
2397     (setq font-lock-keywords
2398           (list
2399
2400            ;; Set up the keywords defined above.
2401            (list (concat "\\<\\(" sml-keywords "\\)\\>")
2402                  '(0 font-lock-keyword-face))
2403
2404            ;; At least numbers are simpler than C.
2405            (list (concat "\\<\\(\\~\\|\\)"
2406                             "\\(0\\(\\([wW]\\|\\)[xX][0-9a-fA-F]+\\|"
2407                                    "[wW][0-9]+\\)\\|"
2408                                 "\\([0-9]+\\(\\.[0-9]+\\|\\)"
2409                                          "\\([eE]\\(\\~\\|\\)"
2410                                                 "[0-9]+\\|\\)\\)\\)")
2411                  '(0 mdw-number-face))
2412
2413            ;; And anything else is punctuation.
2414            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2415                  '(0 mdw-punct-face)))))
2416
2417   (mdw-post-config-mode-hack))
2418
2419 ;;;--------------------------------------------------------------------------
2420 ;;; Haskell configuration.
2421
2422 (defun mdw-fontify-haskell ()
2423
2424   ;; Fiddle with syntax table to get comments right.
2425   (modify-syntax-entry ?' "_")
2426   (modify-syntax-entry ?- ". 12")
2427   (modify-syntax-entry ?\n ">")
2428
2429   ;; Make punctuation be punctuation
2430   (let ((punct "=<>+-*/|&%!@?$.^:#`"))
2431     (do ((i 0 (1+ i)))
2432         ((>= i (length punct)))
2433       (modify-syntax-entry (aref punct i) ".")))
2434
2435   ;; Set fill prefix.
2436   (mdw-standard-fill-prefix "\\([ \t]*{?--?[ \t]*\\)")
2437
2438   ;; Fiddle with fontification.
2439   (make-local-variable 'font-lock-keywords)
2440   (let ((haskell-keywords
2441          (mdw-regexps "as"
2442                       "case" "ccall" "class"
2443                       "data" "default" "deriving" "do"
2444                       "else" "exists"
2445                       "forall" "foreign"
2446                       "hiding"
2447                       "if" "import" "in" "infix" "infixl" "infixr" "instance"
2448                       "let"
2449                       "mdo" "module"
2450                       "newtype"
2451                       "of"
2452                       "proc"
2453                       "qualified"
2454                       "rec"
2455                       "safe" "stdcall"
2456                       "then" "type"
2457                       "unsafe"
2458                       "where"))
2459         (control-sequences
2460          (mdw-regexps "ACK" "BEL" "BS" "CAN" "CR" "DC1" "DC2" "DC3" "DC4"
2461                       "DEL" "DLE" "EM" "ENQ" "EOT" "ESC" "ETB" "ETX" "FF"
2462                       "FS" "GS" "HT" "LF" "NAK" "NUL" "RS" "SI" "SO" "SOH"
2463                       "SP" "STX" "SUB" "SYN" "US" "VT")))
2464
2465     (setq font-lock-keywords
2466           (list
2467            (list (concat "{-" "[^-]*" "\\(-+[^-}][^-]*\\)*"
2468                               "\\(-+}\\|-*\\'\\)"
2469                          "\\|"
2470                          "--.*$")
2471                  '(0 font-lock-comment-face))
2472            (list (concat "\\_<\\(" haskell-keywords "\\)\\_>")
2473                  '(0 font-lock-keyword-face))
2474            (list (concat "'\\("
2475                          "[^\\]"
2476                          "\\|"
2477                          "\\\\"
2478                          "\\(" "[abfnrtv\\\"']" "\\|"
2479                                "^" "\\(" control-sequences "\\|"
2480                                          "[]A-Z@[\\^_]" "\\)" "\\|"
2481                                "\\|"
2482                                "[0-9]+" "\\|"
2483                                "[oO][0-7]+" "\\|"
2484                                "[xX][0-9A-Fa-f]+"
2485                          "\\)"
2486                          "\\)'")
2487                  '(0 font-lock-string-face))
2488            (list "\\_<[A-Z]\\(\\sw+\\|\\s_+\\)*\\_>"
2489                  '(0 font-lock-variable-name-face))
2490            (list (concat "\\_<0\\([xX][0-9a-fA-F]+\\|[oO][0-7]+\\)\\|"
2491                          "\\_<[0-9]+\\(\\.[0-9]*\\|\\)"
2492                          "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)")
2493                  '(0 mdw-number-face))
2494            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2495                  '(0 mdw-punct-face)))))
2496
2497   (mdw-post-config-mode-hack))
2498
2499 ;;;--------------------------------------------------------------------------
2500 ;;; Erlang configuration.
2501
2502 (setq erlang-electric-commands nil)
2503
2504 (defun mdw-fontify-erlang ()
2505
2506   ;; Set fill prefix.
2507   (mdw-standard-fill-prefix "\\([ \t]*{?%*[ \t]*\\)")
2508
2509   ;; Fiddle with fontification.
2510   (make-local-variable 'font-lock-keywords)
2511   (let ((erlang-keywords
2512          (mdw-regexps "after" "and" "andalso"
2513                       "band" "begin" "bnot" "bor" "bsl" "bsr" "bxor"
2514                       "case" "catch" "cond"
2515                       "div" "end" "fun" "if" "let" "not"
2516                       "of" "or" "orelse"
2517                       "query" "receive" "rem" "try" "when" "xor")))
2518
2519     (setq font-lock-keywords
2520           (list
2521            (list "%.*$"
2522                  '(0 font-lock-comment-face))
2523            (list (concat "\\<\\(" erlang-keywords "\\)\\>")
2524                  '(0 font-lock-keyword-face))
2525            (list (concat "^-\\sw+\\>")
2526                  '(0 font-lock-keyword-face))
2527            (list "\\<[0-9]+\\(\\|#[0-9a-zA-Z]+\\|[eE][+-]?[0-9]+\\)\\>"
2528                  '(0 mdw-number-face))
2529            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2530                  '(0 mdw-punct-face)))))
2531
2532   (mdw-post-config-mode-hack))
2533
2534 ;;;--------------------------------------------------------------------------
2535 ;;; Texinfo configuration.
2536
2537 (defun mdw-fontify-texinfo ()
2538
2539   ;; Set fill prefix.
2540   (mdw-standard-fill-prefix "\\([ \t]*@c[ \t]+\\)")
2541
2542   ;; Real fontification things.
2543   (make-local-variable 'font-lock-keywords)
2544   (setq font-lock-keywords
2545         (list
2546
2547          ;; Environment names are keywords.
2548          (list "@\\(end\\)  *\\([a-zA-Z]*\\)?"
2549                '(2 font-lock-keyword-face))
2550
2551          ;; Unmark escaped magic characters.
2552          (list "\\(@\\)\\([@{}]\\)"
2553                '(1 font-lock-keyword-face)
2554                '(2 font-lock-variable-name-face))
2555
2556          ;; Make sure we get comments properly.
2557          (list "@c\\(\\|omment\\)\\( .*\\)?$"
2558                '(0 font-lock-comment-face))
2559
2560          ;; Command names are keywords.
2561          (list "@\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)"
2562                '(0 font-lock-keyword-face))
2563
2564          ;; Fontify TeX special characters as punctuation.
2565          (list "[{}]+"
2566                '(0 mdw-punct-face))))
2567
2568   (mdw-post-config-mode-hack))
2569
2570 ;;;--------------------------------------------------------------------------
2571 ;;; TeX and LaTeX configuration.
2572
2573 (defun mdw-fontify-tex ()
2574   (setq ispell-parser 'tex)
2575   (turn-on-reftex)
2576
2577   ;; Don't make maths into a string.
2578   (modify-syntax-entry ?$ ".")
2579   (modify-syntax-entry ?$ "." font-lock-syntax-table)
2580   (local-set-key [?$] 'self-insert-command)
2581
2582   ;; Set fill prefix.
2583   (mdw-standard-fill-prefix "\\([ \t]*%+[ \t]*\\)")
2584
2585   ;; Real fontification things.
2586   (make-local-variable 'font-lock-keywords)
2587   (setq font-lock-keywords
2588         (list
2589
2590          ;; Environment names are keywords.
2591          (list (concat "\\\\\\(begin\\|end\\|newenvironment\\)"
2592                        "{\\([^}\n]*\\)}")
2593                '(2 font-lock-keyword-face))
2594
2595          ;; Suspended environment names are keywords too.
2596          (list (concat "\\\\\\(suspend\\|resume\\)\\(\\[[^]]*\\]\\)?"
2597                        "{\\([^}\n]*\\)}")
2598                '(3 font-lock-keyword-face))
2599
2600          ;; Command names are keywords.
2601          (list "\\\\\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)"
2602                '(0 font-lock-keyword-face))
2603
2604          ;; Handle @/.../ for italics.
2605          ;; (list "\\(@/\\)\\([^/]*\\)\\(/\\)"
2606          ;;       '(1 font-lock-keyword-face)
2607          ;;       '(3 font-lock-keyword-face))
2608
2609          ;; Handle @*...* for boldness.
2610          ;; (list "\\(@\\*\\)\\([^*]*\\)\\(\\*\\)"
2611          ;;       '(1 font-lock-keyword-face)
2612          ;;       '(3 font-lock-keyword-face))
2613
2614          ;; Handle @`...' for literal syntax things.
2615          ;; (list "\\(@`\\)\\([^']*\\)\\('\\)"
2616          ;;       '(1 font-lock-keyword-face)
2617          ;;       '(3 font-lock-keyword-face))
2618
2619          ;; Handle @<...> for nonterminals.
2620          ;; (list "\\(@<\\)\\([^>]*\\)\\(>\\)"
2621          ;;       '(1 font-lock-keyword-face)
2622          ;;       '(3 font-lock-keyword-face))
2623
2624          ;; Handle other @-commands.
2625          ;; (list "@\\([^a-zA-Z]\\|[a-zA-Z]*\\)"
2626          ;;       '(0 font-lock-keyword-face))
2627
2628          ;; Make sure we get comments properly.
2629          (list "%.*"
2630                '(0 font-lock-comment-face))
2631
2632          ;; Fontify TeX special characters as punctuation.
2633          (list "[$^_{}#&]"
2634                '(0 mdw-punct-face))))
2635
2636   (mdw-post-config-mode-hack))
2637
2638 ;;;--------------------------------------------------------------------------
2639 ;;; SGML hacking.
2640
2641 (defun mdw-sgml-mode ()
2642   (interactive)
2643   (sgml-mode)
2644   (mdw-standard-fill-prefix "")
2645   (make-local-variable 'sgml-delimiters)
2646   (setq sgml-delimiters
2647         '("AND" "&" "COM" "--" "CRO" "&#" "DSC" "]" "DSO" "[" "DTGC" "]"
2648           "DTGO" "[" "ERO" "&" "ETAGO" ":e" "GRPC" ")" "GRPO" "(" "LIT" "\""
2649           "LITA" "'" "MDC" ">" "MDO" "<!" "MINUS" "-" "MSC" "]]" "NESTC" "{"
2650           "NET" "}" "OPT" "?" "OR" "|" "PERO" "%" "PIC" ">" "PIO" "<?"
2651           "PLUS" "+" "REFC" "." "REP" "*" "RNI" "#" "SEQ" "," "STAGO" ":"
2652           "TAGC" "." "VI" "=" "MS-START" "<![" "MS-END" "]]>"
2653           "XML-ECOM" "-->" "XML-PIC" "?>" "XML-SCOM" "<!--" "XML-TAGCE" "/>"
2654           "NULL" ""))
2655   (setq major-mode 'mdw-sgml-mode)
2656   (setq mode-name "[mdw] SGML")
2657   (run-hooks 'mdw-sgml-mode-hook))
2658
2659 ;;;--------------------------------------------------------------------------
2660 ;;; Configuration files.
2661
2662 (defvar mdw-conf-quote-normal nil
2663   "*Control syntax category of quote characters `\"' and `''.
2664 If this is `t', consider quote characters to be normal
2665 punctuation, as for `conf-quote-normal'.  If this is `nil' then
2666 leave quote characters as quotes.  If this is a list, then
2667 consider the quote characters in the list to be normal
2668 punctuation.  If this is a single quote character, then consider
2669 that character only to be normal punctuation.")
2670 (defun mdw-conf-quote-normal-acceptable-value-p (value)
2671   "Is the VALUE is an acceptable value for `mdw-conf-quote-normal'?"
2672   (or (booleanp value)
2673       (every (lambda (v) (memq v '(?\" ?')))
2674              (if (listp value) value (list value)))))
2675 (put 'mdw-conf-quote-normal 'safe-local-variable '
2676      mdw-conf-quote-normal-acceptable-value-p)
2677
2678 (defun mdw-fix-up-quote ()
2679   "Apply the setting of `mdw-conf-quote-normal'."
2680   (let ((flag mdw-conf-quote-normal))
2681     (cond ((eq flag t)
2682            (conf-quote-normal t))
2683           ((not flag)
2684            nil)
2685           (t
2686            (let ((table (copy-syntax-table (syntax-table))))
2687              (mapc (lambda (ch) (modify-syntax-entry ch "." table))
2688                    (if (listp flag) flag (list flag)))
2689              (set-syntax-table table)
2690              (and font-lock-mode (font-lock-fontify-buffer)))))))
2691 (defun mdw-fix-up-quote-hack ()
2692   "Unpleasant hack to call `mdw-fix-up-quote' at the right time.
2693 Annoyingly, `hack-local-variables' is done after `set-auto-mode'
2694 so we wouldn't see a local-variable setting of
2695 `mdw-conf-quote-normal' in `conf-mode-hook'.  Instead, wire
2696 ourselves onto `hack-local-variables-hook' here, and check the
2697 setting once it's actually been made."
2698   (add-hook 'hack-local-variables-hook 'mdw-fix-up-quote t t))
2699 (add-hook 'conf-mode-hook 'mdw-fix-up-quote-hack t)
2700
2701 ;;;--------------------------------------------------------------------------
2702 ;;; Shell scripts.
2703
2704 (defun mdw-setup-sh-script-mode ()
2705
2706   ;; Fetch the shell interpreter's name.
2707   (let ((shell-name sh-shell-file))
2708
2709     ;; Try reading the hash-bang line.
2710     (save-excursion
2711       (goto-char (point-min))
2712       (if (looking-at "#![ \t]*\\([^ \t\n]*\\)")
2713           (setq shell-name (match-string 1))))
2714
2715     ;; Now try to set the shell.
2716     ;;
2717     ;; Don't let `sh-set-shell' bugger up my script.
2718     (let ((executable-set-magic #'(lambda (s &rest r) s)))
2719       (sh-set-shell shell-name)))
2720
2721   ;; Now enable my keys and the fontification.
2722   (mdw-misc-mode-config)
2723
2724   ;; Set the indentation level correctly.
2725   (setq sh-indentation 2)
2726   (setq sh-basic-offset 2))
2727
2728 (setq sh-shell-file "/bin/sh")
2729
2730 ;; Awful hacking to override the shell detection for particular scripts.
2731 (defmacro define-custom-shell-mode (name shell)
2732   `(defun ,name ()
2733      (interactive)
2734      (set (make-local-variable 'sh-shell-file) ,shell)
2735      (sh-mode)))
2736 (define-custom-shell-mode bash-mode "/bin/bash")
2737 (define-custom-shell-mode rc-mode "/usr/bin/rc")
2738 (put 'sh-shell-file 'permanent-local t)
2739
2740 ;; Hack the rc syntax table.  Backquotes aren't paired in rc.
2741 (eval-after-load "sh-script"
2742   '(or (assq 'rc sh-mode-syntax-table-input)
2743        (let ((frag '(nil
2744                      ?# "<"
2745                      ?\n ">#"
2746                      ?\" "\"\""
2747                      ?\' "\"\'"
2748                      ?$ "'"
2749                      ?\` "."
2750                      ?! "_"
2751                      ?% "_"
2752                      ?. "_"
2753                      ?^ "_"
2754                      ?~ "_"
2755                      ?, "_"
2756                      ?= "."
2757                      ?< "."
2758                      ?> "."))
2759              (assoc (assq 'rc sh-mode-syntax-table-input)))
2760          (if assoc
2761              (rplacd assoc frag)
2762            (setq sh-mode-syntax-table-input
2763                  (cons (cons 'rc frag)
2764                        sh-mode-syntax-table-input))))))
2765
2766 ;;;--------------------------------------------------------------------------
2767 ;;; Emacs shell mode.
2768
2769 (defun mdw-eshell-prompt ()
2770   (let ((left "[") (right "]"))
2771     (when (= (user-uid) 0)
2772       (setq left "«" right "»"))
2773     (concat left
2774             (save-match-data
2775               (replace-regexp-in-string "\\..*$" "" (system-name)))
2776             " "
2777             (let* ((pwd (eshell/pwd)) (npwd (length pwd))
2778                    (home (expand-file-name "~")) (nhome (length home)))
2779               (if (and (>= npwd nhome)
2780                        (or (= nhome npwd)
2781                            (= (elt pwd nhome) ?/))
2782                        (string= (substring pwd 0 nhome) home))
2783                   (concat "~" (substring pwd (length home)))
2784                 pwd))
2785             right)))
2786 (setq eshell-prompt-function 'mdw-eshell-prompt)
2787 (setq eshell-prompt-regexp "^\\[[^]>]+\\(\\]\\|>>?\\)")
2788
2789 (defun eshell/e (file) (find-file file) nil)
2790 (defun eshell/ee (file) (find-file-other-window file) nil)
2791 (defun eshell/w3m (url) (w3m-goto-url url) nil)
2792
2793 (mdw-define-face eshell-prompt (t :weight bold))
2794 (mdw-define-face eshell-ls-archive (t :weight bold :foreground "red"))
2795 (mdw-define-face eshell-ls-backup (t :foreground "lightgrey" :slant italic))
2796 (mdw-define-face eshell-ls-product (t :foreground "lightgrey" :slant italic))
2797 (mdw-define-face eshell-ls-clutter (t :foreground "lightgrey" :slant italic))
2798 (mdw-define-face eshell-ls-executable (t :weight bold))
2799 (mdw-define-face eshell-ls-directory (t :foreground "cyan" :weight bold))
2800 (mdw-define-face eshell-ls-readonly (t nil))
2801 (mdw-define-face eshell-ls-symlink (t :foreground "cyan"))
2802
2803 ;;;--------------------------------------------------------------------------
2804 ;;; Messages-file mode.
2805
2806 (defun messages-mode-guts ()
2807   (setq messages-mode-syntax-table (make-syntax-table))
2808   (set-syntax-table messages-mode-syntax-table)
2809   (modify-syntax-entry ?0 "w" messages-mode-syntax-table)
2810   (modify-syntax-entry ?1 "w" messages-mode-syntax-table)
2811   (modify-syntax-entry ?2 "w" messages-mode-syntax-table)
2812   (modify-syntax-entry ?3 "w" messages-mode-syntax-table)
2813   (modify-syntax-entry ?4 "w" messages-mode-syntax-table)
2814   (modify-syntax-entry ?5 "w" messages-mode-syntax-table)
2815   (modify-syntax-entry ?6 "w" messages-mode-syntax-table)
2816   (modify-syntax-entry ?7 "w" messages-mode-syntax-table)
2817   (modify-syntax-entry ?8 "w" messages-mode-syntax-table)
2818   (modify-syntax-entry ?9 "w" messages-mode-syntax-table)
2819   (make-local-variable 'comment-start)
2820   (make-local-variable 'comment-end)
2821   (make-local-variable 'indent-line-function)
2822   (setq indent-line-function 'indent-relative)
2823   (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
2824   (make-local-variable 'font-lock-defaults)
2825   (make-local-variable 'messages-mode-keywords)
2826   (let ((keywords
2827          (mdw-regexps "array" "bitmap" "callback" "docs[ \t]+enum"
2828                       "export" "enum" "fixed-octetstring" "flags"
2829                       "harmless" "map" "nested" "optional"
2830                       "optional-tagged" "package" "primitive"
2831                       "primitive-nullfree" "relaxed[ \t]+enum"
2832                       "set" "table" "tagged-optional"   "union"
2833                       "variadic" "vector" "version" "version-tag")))
2834     (setq messages-mode-keywords
2835           (list
2836            (list (concat "\\<\\(" keywords "\\)\\>:")
2837                  '(0 font-lock-keyword-face))
2838            '("\\([-a-zA-Z0-9]+:\\)" (0 font-lock-warning-face))
2839            '("\\(\\<[a-z][-_a-zA-Z0-9]*\\)"
2840              (0 font-lock-variable-name-face))
2841            '("\\<\\([0-9]+\\)\\>" (0 mdw-number-face))
2842            '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2843              (0 mdw-punct-face)))))
2844   (setq font-lock-defaults
2845         '(messages-mode-keywords nil nil nil nil))
2846   (run-hooks 'messages-file-hook))
2847
2848 (defun messages-mode ()
2849   (interactive)
2850   (fundamental-mode)
2851   (setq major-mode 'messages-mode)
2852   (setq mode-name "Messages")
2853   (messages-mode-guts)
2854   (modify-syntax-entry ?# "<" messages-mode-syntax-table)
2855   (modify-syntax-entry ?\n ">" messages-mode-syntax-table)
2856   (setq comment-start "# ")
2857   (setq comment-end "")
2858   (run-hooks 'messages-mode-hook))
2859
2860 (defun cpp-messages-mode ()
2861   (interactive)
2862   (fundamental-mode)
2863   (setq major-mode 'cpp-messages-mode)
2864   (setq mode-name "CPP Messages")
2865   (messages-mode-guts)
2866   (modify-syntax-entry ?* ". 23" messages-mode-syntax-table)
2867   (modify-syntax-entry ?/ ". 14" messages-mode-syntax-table)
2868   (setq comment-start "/* ")
2869   (setq comment-end " */")
2870   (let ((preprocessor-keywords
2871          (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
2872                       "ident" "if" "ifdef" "ifndef" "import" "include"
2873                       "line" "pragma" "unassert" "undef" "warning")))
2874     (setq messages-mode-keywords
2875           (append (list (list (concat "^[ \t]*\\#[ \t]*"
2876                                       "\\(include\\|import\\)"
2877                                       "[ \t]*\\(<[^>]+\\(>\\|\\)\\)")
2878                               '(2 font-lock-string-face))
2879                         (list (concat "^\\([ \t]*#[ \t]*\\(\\("
2880                                       preprocessor-keywords
2881                                       "\\)\\>\\|[0-9]+\\|$\\)\\)")
2882                               '(1 font-lock-keyword-face)))
2883                   messages-mode-keywords)))
2884   (run-hooks 'cpp-messages-mode-hook))
2885
2886 (add-hook 'messages-mode-hook 'mdw-misc-mode-config t)
2887 (add-hook 'cpp-messages-mode-hook 'mdw-misc-mode-config t)
2888 ; (add-hook 'messages-file-hook 'mdw-fontify-messages t)
2889
2890 ;;;--------------------------------------------------------------------------
2891 ;;; Messages-file mode.
2892
2893 (defvar mallow-driver-substitution-face 'mallow-driver-substitution-face
2894   "Face to use for subsittution directives.")
2895 (make-face 'mallow-driver-substitution-face)
2896 (defvar mallow-driver-text-face 'mallow-driver-text-face
2897   "Face to use for body text.")
2898 (make-face 'mallow-driver-text-face)
2899
2900 (defun mallow-driver-mode ()
2901   (interactive)
2902   (fundamental-mode)
2903   (setq major-mode 'mallow-driver-mode)
2904   (setq mode-name "Mallow driver")
2905   (setq mallow-driver-mode-syntax-table (make-syntax-table))
2906   (set-syntax-table mallow-driver-mode-syntax-table)
2907   (make-local-variable 'comment-start)
2908   (make-local-variable 'comment-end)
2909   (make-local-variable 'indent-line-function)
2910   (setq indent-line-function 'indent-relative)
2911   (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
2912   (make-local-variable 'font-lock-defaults)
2913   (make-local-variable 'mallow-driver-mode-keywords)
2914   (let ((keywords
2915          (mdw-regexps "each" "divert" "file" "if"
2916                       "perl" "set" "string" "type" "write")))
2917     (setq mallow-driver-mode-keywords
2918           (list
2919            (list (concat "^%\\s *\\(}\\|\\(" keywords "\\)\\>\\).*$")
2920                  '(0 font-lock-keyword-face))
2921            (list "^%\\s *\\(#.*\\|\\)$"
2922                  '(0 font-lock-comment-face))
2923            (list "^%"
2924                  '(0 font-lock-keyword-face))
2925            (list "^|?\\(.+\\)$" '(1 mallow-driver-text-face))
2926            (list "\\${[^}]*}"
2927                  '(0 mallow-driver-substitution-face t)))))
2928   (setq font-lock-defaults
2929         '(mallow-driver-mode-keywords nil nil nil nil))
2930   (modify-syntax-entry ?\" "_" mallow-driver-mode-syntax-table)
2931   (modify-syntax-entry ?\n ">" mallow-driver-mode-syntax-table)
2932   (setq comment-start "%# ")
2933   (setq comment-end "")
2934   (run-hooks 'mallow-driver-mode-hook))
2935
2936 (add-hook 'mallow-driver-hook 'mdw-misc-mode-config t)
2937
2938 ;;;--------------------------------------------------------------------------
2939 ;;; NFast debugs.
2940
2941 (defun nfast-debug-mode ()
2942   (interactive)
2943   (fundamental-mode)
2944   (setq major-mode 'nfast-debug-mode)
2945   (setq mode-name "NFast debug")
2946   (setq messages-mode-syntax-table (make-syntax-table))
2947   (set-syntax-table messages-mode-syntax-table)
2948   (make-local-variable 'font-lock-defaults)
2949   (make-local-variable 'nfast-debug-mode-keywords)
2950   (setq truncate-lines t)
2951   (setq nfast-debug-mode-keywords
2952         (list
2953          '("^\\(NFast_\\(Connect\\|Disconnect\\|Submit\\|Wait\\)\\)"
2954            (0 font-lock-keyword-face))
2955          (list (concat "^[ \t]+\\(\\("
2956                        "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
2957                        "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
2958                        "[ \t]+\\)*"
2959                        "[0-9a-fA-F]+\\)[ \t]*$")
2960            '(0 mdw-number-face))
2961          '("^[ \t]+\.status=[ \t]+\\<\\(OK\\)\\>"
2962            (1 font-lock-keyword-face))
2963          '("^[ \t]+\.status=[ \t]+\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>"
2964            (1 font-lock-warning-face))
2965          '("^[ \t]+\.status[ \t]+\\<\\(zero\\)\\>"
2966            (1 nil))
2967          (list (concat "^[ \t]+\\.cmd=[ \t]+"
2968                        "\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>")
2969            '(1 font-lock-keyword-face))
2970          '("-?\\<\\([0-9]+\\|0x[0-9a-fA-F]+\\)\\>" (0 mdw-number-face))
2971          '("^\\([ \t]+[a-z0-9.]+\\)" (0 font-lock-variable-name-face))
2972          '("\\<\\([a-z][a-z0-9.]+\\)\\>=" (1 font-lock-variable-name-face))
2973          '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" (0 mdw-punct-face))))
2974   (setq font-lock-defaults
2975         '(nfast-debug-mode-keywords nil nil nil nil))
2976   (run-hooks 'nfast-debug-mode-hook))
2977
2978 ;;;--------------------------------------------------------------------------
2979 ;;; Other languages.
2980
2981 ;; Smalltalk.
2982
2983 (defun mdw-setup-smalltalk ()
2984   (and mdw-auto-indent
2985        (local-set-key "\C-m" 'smalltalk-newline-and-indent))
2986   (make-local-variable 'mdw-auto-indent)
2987   (setq mdw-auto-indent nil)
2988   (local-set-key "\C-i" 'smalltalk-reindent))
2989
2990 (defun mdw-fontify-smalltalk ()
2991   (make-local-variable 'font-lock-keywords)
2992   (setq font-lock-keywords
2993         (list
2994          (list "\\<[A-Z][a-zA-Z0-9]*\\>"
2995                '(0 font-lock-keyword-face))
2996          (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
2997                        "[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)"
2998                        "\\([eE]\\([-+]\\|\\)[0-9_]+\\|\\)")
2999                '(0 mdw-number-face))
3000          (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3001                '(0 mdw-punct-face))))
3002   (mdw-post-config-mode-hack))
3003
3004 ;; Lispy languages.
3005
3006 ;; Unpleasant bodge.
3007 (unless (boundp 'slime-repl-mode-map)
3008   (setq slime-repl-mode-map (make-sparse-keymap)))
3009
3010 (defun mdw-indent-newline-and-indent ()
3011   (interactive)
3012   (indent-for-tab-command)
3013   (newline-and-indent))
3014
3015 (eval-after-load "cl-indent"
3016   '(progn
3017      (mapc #'(lambda (pair)
3018                (put (car pair)
3019                     'common-lisp-indent-function
3020                     (cdr pair)))
3021       '((destructuring-bind . ((&whole 4 &rest 1) 4 &body))
3022         (multiple-value-bind . ((&whole 4 &rest 1) 4 &body))))))
3023
3024 (defun mdw-common-lisp-indent ()
3025   (make-local-variable 'lisp-indent-function)
3026   (setq lisp-indent-function 'common-lisp-indent-function))
3027
3028 (setq lisp-simple-loop-indentation 2
3029       lisp-loop-keyword-indentation 6
3030       lisp-loop-forms-indentation 6)
3031
3032 (defun mdw-fontify-lispy ()
3033
3034   ;; Set fill prefix.
3035   (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)")
3036
3037   ;; Not much fontification needed.
3038   (make-local-variable 'font-lock-keywords)
3039   (setq font-lock-keywords
3040         (list (list (concat "\\("
3041                             "\\_<[-+]?"
3042                             "\\(" "[0-9]+/[0-9]+"
3043                             "\\|" "\\(" "[0-9]+" "\\(\\.[0-9]*\\)?" "\\|"
3044                                         "\\.[0-9]+" "\\)"
3045                                   "\\([dDeEfFlLsS][-+]?[0-9]+\\)?"
3046                             "\\)"
3047                             "\\|"
3048                             "#"
3049                             "\\(" "x" "[-+]?"
3050                                   "[0-9A-Fa-f]+" "\\(/[0-9A-Fa-f]+\\)?"
3051                             "\\|" "o" "[-+]?" "[0-7]+" "\\(/[0-7]+\\)?"
3052                             "\\|" "b" "[-+]?" "[01]+" "\\(/[01]+\\)?"
3053                             "\\|" "[0-9]+" "r" "[-+]?"
3054                                   "[0-9a-zA-Z]+" "\\(/[0-9a-zA-Z]+\\)?"
3055                             "\\)"
3056                             "\\)\\_>")
3057                     '(0 mdw-number-face))
3058               (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3059                     '(0 mdw-punct-face))))
3060
3061   (mdw-post-config-mode-hack))
3062
3063 (defun comint-send-and-indent ()
3064   (interactive)
3065   (comint-send-input)
3066   (and mdw-auto-indent
3067        (indent-for-tab-command)))
3068
3069 (defun mdw-setup-m4 ()
3070
3071   ;; Inexplicably, Emacs doesn't match braces in m4 mode.  This is very
3072   ;; annoying: fix it.
3073   (modify-syntax-entry ?{ "(")
3074   (modify-syntax-entry ?} ")")
3075
3076   ;; Fill prefix.
3077   (mdw-standard-fill-prefix "\\([ \t]*\\(?:#+\\|\\<dnl\\>\\)[ \t]*\\)"))
3078
3079 ;;;--------------------------------------------------------------------------
3080 ;;; Text mode.
3081
3082 (defun mdw-text-mode ()
3083   (setq fill-column 72)
3084   (flyspell-mode t)
3085   (mdw-standard-fill-prefix
3086    "\\([ \t]*\\([>#|:] ?\\)*[ \t]*\\)" 3)
3087   (auto-fill-mode 1))
3088
3089 ;;;--------------------------------------------------------------------------
3090 ;;; Outline and hide/show modes.
3091
3092 (defun mdw-outline-collapse-all ()
3093   "Completely collapse everything in the entire buffer."
3094   (interactive)
3095   (save-excursion
3096     (goto-char (point-min))
3097     (while (< (point) (point-max))
3098       (hide-subtree)
3099       (forward-line))))
3100
3101 (setq hs-hide-comments-when-hiding-all nil)
3102
3103 (defadvice hs-hide-all (after hide-first-comment activate)
3104   (save-excursion (hs-hide-initial-comment-block)))
3105
3106 ;;;--------------------------------------------------------------------------
3107 ;;; Shell mode.
3108
3109 (defun mdw-sh-mode-setup ()
3110   (local-set-key [?\C-a] 'comint-bol)
3111   (add-hook 'comint-output-filter-functions
3112             'comint-watch-for-password-prompt))
3113
3114 (defun mdw-term-mode-setup ()
3115   (setq term-prompt-regexp shell-prompt-pattern)
3116   (make-local-variable 'mouse-yank-at-point)
3117   (make-local-variable 'transient-mark-mode)
3118   (setq mouse-yank-at-point t)
3119   (auto-fill-mode -1)
3120   (setq tab-width 8))
3121
3122 (defun term-send-meta-right () (interactive) (term-send-raw-string "\e\e[C"))
3123 (defun term-send-meta-left  () (interactive) (term-send-raw-string "\e\e[D"))
3124 (defun term-send-ctrl-uscore () (interactive) (term-send-raw-string "\C-_"))
3125 (defun term-send-meta-meta-something ()
3126   (interactive)
3127   (term-send-raw-string "\e\e")
3128   (term-send-raw))
3129 (eval-after-load 'term
3130   '(progn
3131      (define-key term-raw-map [?\e ?\e] nil)
3132      (define-key term-raw-map [?\e ?\e t] 'term-send-meta-meta-something)
3133      (define-key term-raw-map [?\C-/] 'term-send-ctrl-uscore)
3134      (define-key term-raw-map [M-right] 'term-send-meta-right)
3135      (define-key term-raw-map [?\e ?\M-O ?C] 'term-send-meta-right)
3136      (define-key term-raw-map [M-left] 'term-send-meta-left)
3137      (define-key term-raw-map [?\e ?\M-O ?D] 'term-send-meta-left)))
3138
3139 (defadvice term-exec (before program-args-list compile activate)
3140   "If the PROGRAM argument is a list, interpret it as (PROGRAM . SWITCHES).
3141 This allows you to pass a list of arguments through `ansi-term'."
3142   (let ((program (ad-get-arg 2)))
3143     (if (listp program)
3144         (progn
3145           (ad-set-arg 2 (car program))
3146           (ad-set-arg 4 (cdr program))))))
3147
3148 (defun ssh (host)
3149   "Open a terminal containing an ssh session to the HOST."
3150   (interactive "sHost: ")
3151   (ansi-term (list "ssh" host) (format "ssh@%s" host)))
3152
3153 ;;;--------------------------------------------------------------------------
3154 ;;; Inferior Emacs Lisp.
3155
3156 (setq comint-prompt-read-only t)
3157
3158 (eval-after-load "comint"
3159   '(progn
3160      (define-key comint-mode-map "\C-w" 'comint-kill-region)
3161      (define-key comint-mode-map [C-S-backspace] 'comint-kill-whole-line)))
3162
3163 (eval-after-load "ielm"
3164   '(progn
3165      (define-key ielm-map "\C-w" 'comint-kill-region)
3166      (define-key ielm-map [C-S-backspace] 'comint-kill-whole-line)))
3167
3168 ;;;----- That's all, folks --------------------------------------------------
3169
3170 (provide 'dot-emacs)