chiark / gitweb /
Merge branch 'master' of metalzone:etc/profile
[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 ;; Splitting windows.
128
129 (unless (fboundp 'scroll-bar-columns)
130   (defun scroll-bar-columns (side)
131     (cond ((eq side 'left) 0)
132           (window-system 3)
133           (t 1))))
134 (unless (fboundp 'fringe-columns)
135   (defun fringe-columns (side)
136     (cond ((not window-system) 0)
137           ((eq side 'left) 1)
138           (t 2))))
139
140 (defun mdw-horizontal-window-overhead ()
141   "Computes the horizontal window overhead.
142 This is the number of columns used by fringes, scroll bars and other such
143 cruft."
144   (if (not window-system)
145       1
146     (let ((tot 0))
147       (dolist (what '(scroll-bar fringe))
148         (dolist (side '(left right))
149           (incf tot (funcall (intern (concat (symbol-name what) "-columns"))
150                              side))))
151       tot)))
152
153 (defun mdw-split-window-horizontally (&optional width)
154   "Split a window horizontally.
155 Without a numeric argument, split the window approximately in
156 half.  With a numeric argument WIDTH, allocate WIDTH columns to
157 the left-hand window (if positive) or -WIDTH columns to the
158 right-hand window (if negative).  Space for scroll bars and
159 fringes is not taken out of the allowance for WIDTH, unlike
160 \\[split-window-horizontally]."
161   (interactive "P")
162   (split-window-horizontally
163    (cond ((null width) nil)
164          ((>= width 0) (+ width (mdw-horizontal-window-overhead)))
165          ((< width 0) width))))
166
167 (defun mdw-divvy-window (&optional width)
168   "Split a wide window into appropriate widths."
169   (interactive "P")
170   (setq width (cond (width (prefix-numeric-value width))
171                     ((and window-system
172                           (>= emacs-major-version 22))
173                      77)
174                     (t 78)))
175   (let* ((win (selected-window))
176          (sb-width (mdw-horizontal-window-overhead))
177          (c (/ (+ (window-width) sb-width)
178                (+ width sb-width))))
179     (while (> c 1)
180       (setq c (1- c))
181       (split-window-horizontally (+ width sb-width))
182       (other-window 1))
183     (select-window win)))
184
185 ;; Transient mark mode hacks.
186
187 (defadvice exchange-point-and-mark
188     (around mdw-highlight (&optional arg) activate compile)
189   "Maybe don't actually exchange point and mark.
190 If `transient-mark-mode' is on and the mark is inactive, then
191 just activate it.  A non-trivial prefix argument will force the
192 usual behaviour.  A trivial prefix argument (i.e., just C-u) will
193 activate the mark and temporarily enable `transient-mark-mode' if
194 it's currently off."
195   (cond ((or mark-active
196              (and (not transient-mark-mode) (not arg))
197              (and arg (or (not (consp arg))
198                           (not (= (car arg) 4)))))
199          ad-do-it)
200         (t
201          (or transient-mark-mode (setq transient-mark-mode 'only))
202          (set-mark (mark t)))))
203
204 ;; Functions for sexp diary entries.
205
206 (defun mdw-weekday (l)
207   "Return non-nil if `date' falls on one of the days of the week in L.
208 L is a list of day numbers (from 0 to 6 for Sunday through to
209 Saturday) or symbols `sunday', `monday', etc. (or a mixture).  If
210 the date stored in `date' falls on a listed day, then the
211 function returns non-nil."
212   (let ((d (calendar-day-of-week date)))
213     (or (memq d l)
214         (memq (nth d '(sunday monday tuesday wednesday
215                               thursday friday saturday)) l))))
216
217 (defun mdw-todo (&optional when)
218   "Return non-nil today, or on WHEN, whichever is later."
219   (let ((w (calendar-absolute-from-gregorian (calendar-current-date)))
220         (d (calendar-absolute-from-gregorian date)))
221     (if when
222         (setq w (max w (calendar-absolute-from-gregorian
223                         (cond
224                          ((not european-calendar-style)
225                           when)
226                          ((> (car when) 100)
227                           (list (nth 1 when)
228                                 (nth 2 when)
229                                 (nth 0 when)))
230                          (t
231                           (list (nth 1 when)
232                                 (nth 0 when)
233                                 (nth 2 when))))))))
234     (eq w d)))
235
236 ;; Fighting with Org-mode's evil key maps.
237
238 (defvar mdw-evil-keymap-keys
239   '(([S-up] . [?\C-c up])
240     ([S-down] . [?\C-c down])
241     ([S-left] . [?\C-c left])
242     ([S-right] . [?\C-c right])
243     (([M-up] [?\e up]) . [C-up])
244     (([M-down] [?\e down]) . [C-down])
245     (([M-left] [?\e left]) . [C-left])
246     (([M-right] [?\e right]) . [C-right]))
247   "Defines evil keybindings to clobber in `mdw-clobber-evil-keymap'.
248 The value is an alist mapping evil keys (as a list, or singleton)
249 to good keys (in the same form).")
250
251 (defun mdw-clobber-evil-keymap (keymap)
252   "Replace evil key bindings in the KEYMAP.
253 Evil key bindings are defined in `mdw-evil-keymap-keys'."
254   (dolist (entry mdw-evil-keymap-keys)
255     (let ((binding nil)
256           (keys (if (listp (car entry))
257                     (car entry)
258                   (list (car entry))))
259           (replacements (if (listp (cdr entry))
260                             (cdr entry)
261                           (list (cdr entry)))))
262       (catch 'found
263         (dolist (key keys)
264           (setq binding (lookup-key keymap key))
265           (when binding
266             (throw 'found nil))))
267       (when binding
268         (dolist (key keys)
269           (define-key keymap key nil))
270         (dolist (key replacements)
271           (define-key keymap key binding))))))
272
273 (eval-after-load "org-latex"
274   '(progn
275      (push '("strayman"
276              "\\documentclass{strayman}
277 \\usepackage[utf8]{inputenc}
278 \\usepackage[palatino, helvetica, courier, maths=cmr]{mdwfonts}
279 \\usepackage[T1]{fontenc}
280 \\usepackage{graphicx, tikz, mdwtab, mdwmath, crypto, longtable}"
281              ("\\section{%s}" . "\\section*{%s}")
282              ("\\subsection{%s}" . "\\subsection*{%s}")
283              ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
284              ("\\paragraph{%s}" . "\\paragraph*{%s}")
285              ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
286            org-export-latex-classes)))
287
288 ;;;--------------------------------------------------------------------------
289 ;;; Mail and news hacking.
290
291 (define-derived-mode  mdwmail-mode mail-mode "[mdw] mail"
292   "Major mode for editing news and mail messages from external programs.
293 Not much right now.  Just support for doing MailCrypt stuff."
294   :syntax-table nil
295   :abbrev-table nil
296   (run-hooks 'mail-setup-hook))
297
298 (define-key mdwmail-mode-map [?\C-c ?\C-c] 'disabled-operation)
299
300 (add-hook 'mdwail-mode-hook
301           (lambda ()
302             (set-buffer-file-coding-system 'utf-8)
303             (make-local-variable 'paragraph-separate)
304             (make-local-variable 'paragraph-start)
305             (setq paragraph-start
306                   (concat "[ \t]*[-_][-_][-_]+$\\|^-- \\|-----\\|"
307                           paragraph-start))
308             (setq paragraph-separate
309                   (concat "[ \t]*[-_][-_][-_]+$\\|^-- \\|-----\\|"
310                           paragraph-separate))))
311
312 ;; How to encrypt in mdwmail.
313
314 (defun mdwmail-mc-encrypt (&optional recip scm start end from sign)
315   (or start
316       (setq start (save-excursion
317                     (goto-char (point-min))
318                     (or (search-forward "\n\n" nil t) (point-min)))))
319   (or end
320       (setq end (point-max)))
321   (mc-encrypt-generic recip scm start end from sign))
322
323 ;; How to sign in mdwmail.
324
325 (defun mdwmail-mc-sign (key scm start end uclr)
326   (or start
327       (setq start (save-excursion
328                     (goto-char (point-min))
329                     (or (search-forward "\n\n" nil t) (point-min)))))
330   (or end
331       (setq end (point-max)))
332   (mc-sign-generic key scm start end uclr))
333
334 ;; Some signature mangling.
335
336 (defun mdwmail-mangle-signature ()
337   (save-excursion
338     (goto-char (point-min))
339     (perform-replace "\n-- \n" "\n-- " nil nil nil)))
340 (add-hook 'mail-setup-hook 'mdwmail-mangle-signature)
341 (add-hook 'message-setup-hook 'mdwmail-mangle-signature)
342
343 ;; Insert my login name into message-ids, so I can score replies.
344
345 (defadvice message-unique-id (after mdw-user-name last activate compile)
346   "Ensure that the user's name appears at the end of the message-id string,
347 so that it can be used for convenient filtering."
348   (setq ad-return-value (concat ad-return-value "." (user-login-name))))
349
350 ;; Tell my movemail hack where movemail is.
351 ;;
352 ;; This is needed to shup up warnings about LD_PRELOAD.
353
354 (let ((path exec-path))
355   (while path
356     (let ((try (expand-file-name "movemail" (car path))))
357       (if (file-executable-p try)
358           (setenv "REAL_MOVEMAIL" try))
359       (setq path (cdr path)))))
360
361 ;;;--------------------------------------------------------------------------
362 ;;; Utility functions.
363
364 (or (fboundp 'line-number-at-pos)
365     (defun line-number-at-pos (&optional pos)
366       (let ((opoint (or pos (point))) start)
367         (save-excursion
368           (save-restriction
369             (goto-char (point-min))
370             (widen)
371             (forward-line 0)
372             (setq start (point))
373             (goto-char opoint)
374             (forward-line 0)
375             (1+ (count-lines 1 (point))))))))
376
377 (defun mdw-uniquify-alist (&rest alists)
378   "Return the concatenation of the ALISTS with duplicate elements removed.
379 The first association with a given key prevails; others are
380 ignored.  The input lists are not modified, although they'll
381 probably become garbage."
382   (and alists
383        (let ((start-list (cons nil nil)))
384          (mdw-do-uniquify start-list
385                           start-list
386                           (car alists)
387                           (cdr alists)))))
388
389 (defun mdw-do-uniquify (done end l rest)
390   "A helper function for mdw-uniquify-alist.
391 The DONE argument is a list whose first element is `nil'.  It
392 contains the uniquified alist built so far.  The leading `nil' is
393 stripped off at the end of the operation; it's only there so that
394 DONE always references a cons cell.  END refers to the final cons
395 cell in the DONE list; it is modified in place each time to avoid
396 the overheads of `append'ing all the time.  The L argument is the
397 alist we're currently processing; the remaining alists are given
398 in REST."
399
400   ;; There are several different cases to deal with here.
401   (cond
402
403    ;; Current list isn't empty.  Add the first item to the DONE list if
404    ;; there's not an item with the same KEY already there.
405    (l (or (assoc (car (car l)) done)
406           (progn
407             (setcdr end (cons (car l) nil))
408             (setq end (cdr end))))
409       (mdw-do-uniquify done end (cdr l) rest))
410
411    ;; The list we were working on is empty.  Shunt the next list into the
412    ;; current list position and go round again.
413    (rest (mdw-do-uniquify done end (car rest) (cdr rest)))
414
415    ;; Everything's done.  Remove the leading `nil' from the DONE list and
416    ;; return it.  Finished!
417    (t (cdr done))))
418
419 (defun date ()
420   "Insert the current date in a pleasing way."
421   (interactive)
422   (insert (save-excursion
423             (let ((buffer (get-buffer-create "*tmp*")))
424               (unwind-protect (progn (set-buffer buffer)
425                                      (erase-buffer)
426                                      (shell-command "date +%Y-%m-%d" t)
427                                      (goto-char (mark))
428                                      (delete-backward-char 1)
429                                      (buffer-string))
430                 (kill-buffer buffer))))))
431
432 (defun uuencode (file &optional name)
433   "UUencodes a file, maybe calling it NAME, into the current buffer."
434   (interactive "fInput file name: ")
435
436   ;; If NAME isn't specified, then guess from the filename.
437   (if (not name)
438       (setq name
439             (substring file
440                        (or (string-match "[^/]*$" file) 0))))
441   (print (format "uuencode `%s' `%s'" file name))
442
443   ;; Now actually do the thing.
444   (call-process "uuencode" file t nil name))
445
446 (defvar np-file "~/.np"
447   "*Where the `now-playing' file is.")
448
449 (defun np (&optional arg)
450   "Grabs a `now-playing' string."
451   (interactive)
452   (save-excursion
453     (or arg (progn
454               (goto-char (point-max))
455               (insert "\nNP: ")
456               (insert-file-contents np-file)))))
457
458 (defun mdw-version-< (ver-a ver-b)
459   "Answer whether VER-A is strictly earlier than VER-B.
460 VER-A and VER-B are version numbers, which are strings containing digit
461 sequences separated by `.'."
462   (let* ((la (mapcar (lambda (x) (car (read-from-string x)))
463                      (split-string ver-a "\\.")))
464          (lb (mapcar (lambda (x) (car (read-from-string x)))
465                      (split-string ver-b "\\."))))
466     (catch 'done
467       (while t
468         (cond ((null la) (throw 'done lb))
469               ((null lb) (throw 'done nil))
470               ((< (car la) (car lb)) (throw 'done t))
471               ((= (car la) (car lb)) (setq la (cdr la) lb (cdr lb))))))))
472
473 (defun mdw-check-autorevert ()
474   "Sets global-auto-revert-ignore-buffer appropriately for this buffer.
475 This takes into consideration whether it's been found using
476 tramp, which seems to get itself into a twist."
477   (cond ((not (boundp 'global-auto-revert-ignore-buffer))
478          nil)
479         ((and (buffer-file-name)
480               (fboundp 'tramp-tramp-file-p)
481               (tramp-tramp-file-p (buffer-file-name)))
482          (unless global-auto-revert-ignore-buffer
483            (setq global-auto-revert-ignore-buffer 'tramp)))
484         ((eq global-auto-revert-ignore-buffer 'tramp)
485          (setq global-auto-revert-ignore-buffer nil))))
486
487 (defadvice find-file (after mdw-autorevert activate)
488   (mdw-check-autorevert))
489 (defadvice write-file (after mdw-autorevert activate)
490   (mdw-check-autorevert))
491
492 ;;;--------------------------------------------------------------------------
493 ;;; Dired hacking.
494
495 (defadvice dired-maybe-insert-subdir
496     (around mdw-marked-insertion first activate)
497   "The DIRNAME may be a list of directory names to insert.
498 Interactively, if files are marked, then insert all of them.
499 With a numeric prefix argument, select that many entries near
500 point; with a non-numeric prefix argument, prompt for listing
501 options."
502   (interactive
503    (list (dired-get-marked-files nil
504                                  (and (integerp current-prefix-arg)
505                                       current-prefix-arg)
506                                  #'file-directory-p)
507          (and current-prefix-arg
508               (not (integerp current-prefix-arg))
509               (read-string "Switches for listing: "
510                            (or dired-subdir-switches
511                                dired-actual-switches)))))
512   (let ((dirs (ad-get-arg 0)))
513     (dolist (dir (if (listp dirs) dirs (list dirs)))
514       (ad-set-arg 0 dir)
515       ad-do-it)))
516
517 ;;;--------------------------------------------------------------------------
518 ;;; URL viewing.
519
520 (defun mdw-w3m-browse-url (url &optional new-session-p)
521   "Invoke w3m on the URL in its current window, or at least a different one.
522 If NEW-SESSION-P, start a new session."
523   (interactive "sURL: \nP")
524   (save-excursion
525     (let ((window (selected-window)))
526       (unwind-protect
527           (progn
528             (select-window (or (and (not new-session-p)
529                                     (get-buffer-window "*w3m*"))
530                                (progn
531                                  (if (one-window-p t) (split-window))
532                                  (get-lru-window))))
533             (w3m-browse-url url new-session-p))
534         (select-window window)))))
535
536 (defvar mdw-good-url-browsers
537   '(browse-url-generic
538     (w3m . mdw-w3m-browse-url)
539     browse-url-w3
540     browse-url-mozilla)
541   "List of good browsers for mdw-good-url-browsers.
542 Each item is a browser function name, or a cons (CHECK . FUNC).
543 A symbol FOO stands for (FOO . FOO).")
544
545 (defun mdw-good-url-browser ()
546   "Return a good URL browser.
547 Trundle the list of such things, finding the first item for which
548 CHECK is fboundp, and returning the correponding FUNC."
549   (let ((bs mdw-good-url-browsers) b check func answer)
550     (while (and bs (not answer))
551       (setq b (car bs)
552             bs (cdr bs))
553       (if (consp b)
554           (setq check (car b) func (cdr b))
555         (setq check b func b))
556       (if (fboundp check)
557           (setq answer func)))
558     answer))
559
560 (eval-after-load "w3m-search"
561   '(progn
562      (dolist
563          (item
564           '(("g" "Google" "http://www.google.co.uk/search?q=%s")
565             ("gd" "Google Directory"
566              "http://www.google.com/search?cat=gwd/Top&q=%s")
567             ("gg" "Google Groups" "http://groups.google.com/groups?q=%s")
568             ("ward" "Ward's wiki" "http://c2.com/cgi/wiki?%s")
569             ("gi" "Images" "http://images.google.com/images?q=%s")
570             ("rfc" "RFC"
571              "http://metalzone.distorted.org.uk/ftp/pub/mirrors/rfc/rfc%s.txt.gz")
572             ("wp" "Wikipedia"
573              "http://en.wikipedia.org/wiki/Special:Search?go=Go&search=%s")
574             ("imdb" "IMDb" "http://www.imdb.com/Find?%s")
575             ("nc-wiki" "nCipher wiki"
576              "http://wiki.ncipher.com/wiki/bin/view/Devel/?topic=%s")
577             ("map" "Google maps" "http://maps.google.co.uk/maps?q=%s&hl=en")
578             ("lp" "Launchpad bug by number"
579              "https://bugs.launchpad.net/bugs/%s")
580             ("lppkg" "Launchpad bugs by package"
581              "https://bugs.launchpad.net/%s")
582             ("msdn" "MSDN"
583              "http://social.msdn.microsoft.com/Search/en-GB/?query=%s&ac=8")
584             ("debbug" "Debian bug by number"
585              "http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=%s")
586             ("debbugpkg" "Debian bugs by package"
587              "http://bugs.debian.org/cgi-bin/pkgreport.cgi?pkg=%s")
588             ("ljlogin" "LJ login" "http://www.livejournal.com/login.bml")))
589        (add-to-list 'w3m-search-engine-alist
590                     (list (cadr item) (caddr item) nil))
591        (add-to-list 'w3m-uri-replace-alist
592                     (list (concat "\\`" (car item) ":")
593                           'w3m-search-uri-replace
594                           (cadr item))))))
595
596 ;;;--------------------------------------------------------------------------
597 ;;; Paragraph filling.
598
599 ;; Useful variables.
600
601 (defvar mdw-fill-prefix nil
602   "*Used by `mdw-line-prefix' and `mdw-fill-paragraph'.
603 If there's no fill prefix currently set (by the `fill-prefix'
604 variable) and there's a match from one of the regexps here, it
605 gets used to set the fill-prefix for the current operation.
606
607 The variable is a list of items of the form `REGEXP . PREFIX'; if
608 the REGEXP matches, the PREFIX is used to set the fill prefix.
609 It in turn is a list of things:
610
611   STRING -- insert a literal string
612   (match . N) -- insert the thing matched by bracketed subexpression N
613   (pad . N) -- a string of whitespace the same width as subexpression N
614   (expr . FORM) -- the result of evaluating FORM")
615
616 (make-variable-buffer-local 'mdw-fill-prefix)
617
618 (defvar mdw-hanging-indents
619   (concat "\\(\\("
620             "\\([*o+]\\|-[-#]?\\|[0-9]+\\.\\|\\[[0-9]+\\]\\|([a-zA-Z])\\)"
621             "[ \t]+"
622           "\\)?\\)")
623   "*Standard regexp matching parts of a hanging indent.
624 This is mainly useful in `auto-fill-mode'.")
625
626 ;; Setting things up.
627
628 (fset 'mdw-do-auto-fill (symbol-function 'do-auto-fill))
629
630 ;; Utility functions.
631
632 (defun mdw-tabify (s)
633   "Tabify the string S.  This is a horrid hack."
634   (save-excursion
635     (save-match-data
636       (let (start end)
637         (beginning-of-line)
638         (setq start (point-marker))
639         (insert s "\n")
640         (setq end (point-marker))
641         (tabify start end)
642         (setq s (buffer-substring start (1- end)))
643         (delete-region start end)
644         (set-marker start nil)
645         (set-marker end nil)
646         s))))
647
648 (defun mdw-examine-fill-prefixes (l)
649   "Given a list of dynamic fill prefixes, pick one which matches
650 context and return the static fill prefix to use.  Point must be
651 at the start of a line, and match data must be saved."
652   (cond ((not l) nil)
653                ((looking-at (car (car l)))
654                 (mdw-tabify (apply (function concat)
655                                    (mapcar (function mdw-do-prefix-match)
656                                            (cdr (car l))))))
657                (t (mdw-examine-fill-prefixes (cdr l)))))
658
659 (defun mdw-maybe-car (p)
660   "If P is a pair, return (car P), otherwise just return P."
661   (if (consp p) (car p) p))
662
663 (defun mdw-padding (s)
664   "Return a string the same width as S but made entirely from whitespace."
665   (let* ((l (length s)) (i 0) (n (make-string l ? )))
666     (while (< i l)
667       (if (= 9 (aref s i))
668           (aset n i 9))
669       (setq i (1+ i)))
670     n))
671
672 (defun mdw-do-prefix-match (m)
673   "Expand a dynamic prefix match element.
674 See `mdw-fill-prefix' for details."
675   (cond ((not (consp m)) (format "%s" m))
676            ((eq (car m) 'match) (match-string (mdw-maybe-car (cdr m))))
677            ((eq (car m) 'pad) (mdw-padding (match-string
678                                             (mdw-maybe-car (cdr m)))))
679            ((eq (car m) 'eval) (eval (cdr m)))
680            (t "")))
681
682 (defun mdw-choose-dynamic-fill-prefix ()
683   "Work out the dynamic fill prefix based on the variable `mdw-fill-prefix'."
684   (cond ((and fill-prefix (not (string= fill-prefix ""))) fill-prefix)
685            ((not mdw-fill-prefix) fill-prefix)
686            (t (save-excursion
687                 (beginning-of-line)
688                 (save-match-data
689                   (mdw-examine-fill-prefixes mdw-fill-prefix))))))
690
691 (defun do-auto-fill ()
692   "Handle auto-filling, working out a dynamic fill prefix in the
693 case where there isn't a sensible static one."
694   (let ((fill-prefix (mdw-choose-dynamic-fill-prefix)))
695     (mdw-do-auto-fill)))
696
697 (defun mdw-fill-paragraph ()
698   "Fill paragraph, getting a dynamic fill prefix."
699   (interactive)
700   (let ((fill-prefix (mdw-choose-dynamic-fill-prefix)))
701     (fill-paragraph nil)))
702
703 (defun mdw-standard-fill-prefix (rx &optional mat)
704   "Set the dynamic fill prefix, handling standard hanging indents and stuff.
705 This is just a short-cut for setting the thing by hand, and by
706 design it doesn't cope with anything approximating a complicated
707 case."
708   (setq mdw-fill-prefix
709            `((,(concat rx mdw-hanging-indents)
710               (match . 1)
711               (pad . ,(or mat 2))))))
712
713 ;;;--------------------------------------------------------------------------
714 ;;; Other common declarations.
715
716 ;; Common mode settings.
717
718 (defvar mdw-auto-indent t
719   "Whether to indent automatically after a newline.")
720
721 (defun mdw-whitespace-mode (&optional arg)
722   "Turn on/off whitespace mode, but don't highlight trailing space."
723   (interactive "P")
724   (when (and (boundp 'whitespace-style)
725              (fboundp 'whitespace-mode))
726     (let ((whitespace-style (remove 'trailing whitespace-style)))
727       (whitespace-mode arg))
728     (setq show-trailing-whitespace whitespace-mode)))
729
730 (defun mdw-misc-mode-config ()
731   (and mdw-auto-indent
732        (cond ((eq major-mode 'lisp-mode)
733               (local-set-key "\C-m" 'mdw-indent-newline-and-indent))
734              ((or (eq major-mode 'slime-repl-mode)
735                   (eq major-mode 'asm-mode))
736               nil)
737              (t
738               (local-set-key "\C-m" 'newline-and-indent))))
739   (local-set-key [C-return] 'newline)
740   (make-local-variable 'page-delimiter)
741   (setq page-delimiter "\f\\|^.*-\\{6\\}.*$")
742   (setq comment-column 40)
743   (auto-fill-mode 1)
744   (setq fill-column 77)
745   (setq show-trailing-whitespace t)
746   (mdw-whitespace-mode 1)
747   (and (fboundp 'gtags-mode)
748        (gtags-mode))
749   (if (fboundp 'hs-minor-mode)
750       (hs-minor-mode t)
751     (outline-minor-mode t))
752   (reveal-mode t)
753   (trap (turn-on-font-lock)))
754
755 (defun mdw-post-config-mode-hack ()
756   (mdw-whitespace-mode 1))
757
758 (eval-after-load 'gtags
759   '(progn
760      (dolist (key '([mouse-2] [mouse-3]))
761        (define-key gtags-mode-map key nil))
762      (define-key gtags-mode-map [C-S-mouse-2] 'gtags-find-tag-by-event)
763      (define-key gtags-select-mode-map [C-S-mouse-2]
764        'gtags-select-tag-by-event)
765      (dolist (map (list gtags-mode-map gtags-select-mode-map))
766        (define-key map [C-S-mouse-3] 'gtags-pop-stack))))
767
768 ;; Backup file handling.
769
770 (defvar mdw-backup-disable-regexps nil
771   "*List of regular expressions: if a file name matches any of
772 these then the file is not backed up.")
773
774 (defun mdw-backup-enable-predicate (name)
775   "[mdw]'s default backup predicate.
776 Allows a backup if the standard predicate would allow it, and it
777 doesn't match any of the regular expressions in
778 `mdw-backup-disable-regexps'."
779   (and (normal-backup-enable-predicate name)
780        (let ((answer t) (list mdw-backup-disable-regexps))
781          (save-match-data
782            (while list
783              (if (string-match (car list) name)
784                  (setq answer nil))
785              (setq list (cdr list)))
786            answer))))
787 (setq backup-enable-predicate 'mdw-backup-enable-predicate)
788
789 ;; Frame cleanup.
790
791 (defun mdw-last-one-out-turn-off-the-lights (frame)
792   "Disconnect from an X display if this was the last frame on that display."
793   (let ((frame-display (frame-parameter frame 'display)))
794     (when (and frame-display
795                (eq window-system 'x)
796                (not (some (lambda (fr)
797                             (and (not (eq fr frame))
798                                  (string= (frame-parameter fr 'display)
799                                           frame-display)))
800                           (frame-list))))
801       (run-with-idle-timer 0 nil #'x-close-connection frame-display))))
802 (add-hook 'delete-frame-functions 'mdw-last-one-out-turn-off-the-lights)
803
804 ;;;--------------------------------------------------------------------------
805 ;;; General fontification.
806
807 (defmacro mdw-define-face (name &rest body)
808   "Define a face, and make sure it's actually set as the definition."
809   (declare (indent 1)
810            (debug 0))
811   `(progn
812      (make-face ',name)
813      (defvar ,name ',name)
814      (put ',name 'face-defface-spec ',body)
815      (face-spec-set ',name ',body nil)))
816
817 (mdw-define-face default
818   (((type w32)) :family "courier new" :height 85)
819   (((type x)) :family "6x13" :height 130)
820   (((type color)) :foreground "white" :background "black")
821   (t nil))
822 (mdw-define-face fixed-pitch
823   (((type w32)) :family "courier new" :height 85)
824   (((type x)) :family "6x13" :height 130)
825   (t :foreground "white" :background "black"))
826 (if (>= emacs-major-version 23)
827     (mdw-define-face variable-pitch
828       (((type x)) :family "sans" :height 100))
829   (mdw-define-face variable-pitch
830     (((type x)) :family "helvetica" :height 90)))
831 (mdw-define-face region
832   (((type tty) (class color)) :background "blue")
833   (((type tty) (class mono)) :inverse-video t)
834   (t :background "grey30"))
835 (mdw-define-face minibuffer-prompt
836   (t :weight bold))
837 (mdw-define-face mode-line
838   (((class color)) :foreground "blue" :background "yellow"
839                    :box (:line-width 1 :style released-button))
840   (t :inverse-video t))
841 (mdw-define-face mode-line-inactive
842   (((class color)) :foreground "yellow" :background "blue"
843                    :box (:line-width 1 :style released-button))
844   (t :inverse-video t))
845 (mdw-define-face scroll-bar
846   (t :foreground "black" :background "lightgrey"))
847 (mdw-define-face fringe
848   (t :foreground "yellow"))
849 (mdw-define-face show-paren-match
850   (((class color)) :background "darkgreen")
851   (t :underline t))
852 (mdw-define-face show-paren-mismatch
853   (((class color)) :background "red")
854   (t :inverse-video t))
855 (mdw-define-face highlight
856   (((class color)) :background "DarkSeaGreen4")
857   (t :inverse-video t))
858
859 (mdw-define-face holiday-face
860   (t :background "red"))
861 (mdw-define-face calendar-today-face
862   (t :foreground "yellow" :weight bold))
863
864 (mdw-define-face comint-highlight-prompt
865   (t :weight bold))
866 (mdw-define-face comint-highlight-input
867   (t nil))
868
869 (mdw-define-face dired-directory
870   (t :foreground "cyan" :weight bold))
871 (mdw-define-face dired-symlink
872   (t :foreground "cyan"))
873 (mdw-define-face dired-perm-write
874   (t nil))
875
876 (mdw-define-face trailing-whitespace
877   (((class color)) :background "red")
878   (t :inverse-video t))
879 (mdw-define-face mdw-punct-face
880   (((type tty)) :foreground "yellow") (t :foreground "burlywood2"))
881 (mdw-define-face mdw-number-face
882   (t :foreground "yellow"))
883 (mdw-define-face font-lock-function-name-face
884   (t :slant italic))
885 (mdw-define-face font-lock-keyword-face
886   (t :weight bold))
887 (mdw-define-face font-lock-constant-face
888   (t :slant italic))
889 (mdw-define-face font-lock-builtin-face
890   (t :weight bold))
891 (mdw-define-face font-lock-type-face
892   (t :weight bold :slant italic))
893 (mdw-define-face font-lock-reference-face
894   (t :weight bold))
895 (mdw-define-face font-lock-variable-name-face
896   (t :slant italic))
897 (mdw-define-face font-lock-comment-delimiter-face
898   (((class mono)) :weight bold)
899   (((type tty) (class color)) :foreground "green")
900   (t :slant italic :foreground "SeaGreen1"))
901 (mdw-define-face font-lock-comment-face
902   (((class mono)) :weight bold)
903   (((type tty) (class color)) :foreground "green")
904   (t :slant italic :foreground "SeaGreen1"))
905 (mdw-define-face font-lock-string-face
906   (((class mono)) :weight bold)
907   (((class color)) :foreground "SkyBlue1"))
908 (mdw-define-face message-separator
909   (t :background "red" :foreground "white" :weight bold))
910 (mdw-define-face message-cited-text
911   (default :slant italic)
912   (((type tty)) :foreground "green") (t :foreground "SeaGreen1"))
913 (mdw-define-face message-header-cc
914   (default :weight bold)
915   (((type tty)) :foreground "green") (t :foreground "SeaGreen1"))
916 (mdw-define-face message-header-newsgroups
917   (default :weight bold)
918   (((type tty)) :foreground "green") (t :foreground "SeaGreen1"))
919 (mdw-define-face message-header-subject
920   (default :weight bold)
921   (((type tty)) :foreground "green") (t :foreground "SeaGreen1"))
922 (mdw-define-face message-header-to
923   (default :weight bold)
924   (((type tty)) :foreground "green") (t :foreground "SeaGreen1"))
925 (mdw-define-face message-header-xheader
926   (default :weight bold)
927   (((type tty)) :foreground "green") (t :foreground "SeaGreen1"))
928 (mdw-define-face message-header-other
929   (default :weight bold)
930   (((type tty)) :foreground "green") (t :foreground "SeaGreen1"))
931 (mdw-define-face message-header-name
932   (((type tty)) :foreground "green") (t :foreground "SeaGreen1"))
933 (mdw-define-face which-func
934   (t nil))
935
936 (mdw-define-face diff-index
937   (t :weight bold))
938 (mdw-define-face diff-file-header
939   (t :weight bold))
940 (mdw-define-face diff-hunk-header
941   (t :foreground "SkyBlue1"))
942 (mdw-define-face diff-function
943   (t :foreground "SkyBlue1" :weight bold))
944 (mdw-define-face diff-header
945   (t :background "grey10"))
946 (mdw-define-face diff-added
947   (t :foreground "green"))
948 (mdw-define-face diff-removed
949   (t :foreground "red"))
950 (mdw-define-face diff-context
951   (t nil))
952
953 (mdw-define-face erc-input-face
954   (t :foreground "red"))
955
956 (mdw-define-face woman-bold
957   (t :weight bold))
958 (mdw-define-face woman-italic
959   (t :slant italic))
960
961 (mdw-define-face p4-depot-added-face
962   (t :foreground "green"))
963 (mdw-define-face p4-depot-branch-op-face
964   (t :foreground "yellow"))
965 (mdw-define-face p4-depot-deleted-face
966   (t :foreground "red"))
967 (mdw-define-face p4-depot-unmapped-face
968   (t :foreground "SkyBlue1"))
969 (mdw-define-face p4-diff-change-face
970   (t :foreground "yellow"))
971 (mdw-define-face p4-diff-del-face
972   (t :foreground "red"))
973 (mdw-define-face p4-diff-file-face
974   (t :foreground "SkyBlue1"))
975 (mdw-define-face p4-diff-head-face
976   (t :background "grey10"))
977 (mdw-define-face p4-diff-ins-face
978   (t :foreground "green"))
979
980 (mdw-define-face whizzy-slice-face
981   (t :background "grey10"))
982 (mdw-define-face whizzy-error-face
983   (t :background "darkred"))
984
985 ;;;--------------------------------------------------------------------------
986 ;;; C programming configuration.
987
988 ;; Linux kernel hacking.
989
990 (defvar linux-c-mode-hook)
991
992 (defun linux-c-mode ()
993   (interactive)
994   (c-mode)
995   (setq major-mode 'linux-c-mode)
996   (setq mode-name "Linux C")
997   (run-hooks 'linux-c-mode-hook))
998
999 ;; Make C indentation nice.
1000
1001 (defun mdw-c-lineup-arglist (langelem)
1002   "Hack for DWIMmery in c-lineup-arglist."
1003   (if (save-excursion
1004         (c-block-in-arglist-dwim (c-langelem-2nd-pos c-syntactic-element)))
1005       0
1006     (c-lineup-arglist langelem)))
1007
1008 (defun mdw-c-indent-extern-mumble (langelem)
1009   "Indent `extern \"...\" {' lines."
1010   (save-excursion
1011     (back-to-indentation)
1012     (if (looking-at
1013          "\\s-*\\<extern\\>\\s-*\"\\([^\\\\\"]+\\|\\.\\)*\"\\s-*{")
1014         c-basic-offset
1015       nil)))
1016
1017 (defun mdw-c-style ()
1018   (c-add-style "[mdw] C and C++ style"
1019                '((c-basic-offset . 2)
1020                  (comment-column . 40)
1021                  (c-class-key . "class")
1022                  (c-backslash-column . 72)
1023                  (c-offsets-alist
1024                   (substatement-open . (add 0 c-indent-one-line-block))
1025                   (defun-open . (add 0 c-indent-one-line-block))
1026                   (arglist-cont-nonempty . mdw-c-lineup-arglist)
1027                   (topmost-intro . mdw-c-indent-extern-mumble)
1028                   (cpp-define-intro . 0)
1029                   (knr-argdecl . 0)
1030                   (inextern-lang . [0])
1031                   (label . 0)
1032                   (case-label . +)
1033                   (access-label . -)
1034                   (inclass . +)
1035                   (inline-open . ++)
1036                   (statement-cont . +)
1037                   (statement-case-intro . +)))
1038                t))
1039
1040 (defvar mdw-c-comment-fill-prefix
1041   `((,(concat "\\([ \t]*/?\\)"
1042               "\\(\*\\|//]\\)"
1043               "\\([ \t]*\\)"
1044               "\\([A-Za-z]+:[ \t]*\\)?"
1045               mdw-hanging-indents)
1046      (pad . 1) (match . 2) (pad . 3) (pad . 4) (pad . 5)))
1047   "Fill prefix matching C comments (both kinds).")
1048
1049 (defun mdw-fontify-c-and-c++ ()
1050
1051   ;; Fiddle with some syntax codes.
1052   (modify-syntax-entry ?* ". 23")
1053   (modify-syntax-entry ?/ ". 124b")
1054   (modify-syntax-entry ?\n "> b")
1055
1056   ;; Other stuff.
1057   (mdw-c-style)
1058   (setq c-hanging-comment-ender-p nil)
1059   (setq c-backslash-column 72)
1060   (setq c-label-minimum-indentation 0)
1061   (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
1062
1063   ;; Now define things to be fontified.
1064   (make-local-variable 'font-lock-keywords)
1065   (let ((c-keywords
1066          (mdw-regexps "and"             ;C++
1067                       "and_eq"          ;C++
1068                       "asm"             ;K&R, GCC
1069                       "auto"            ;K&R, C89
1070                       "bitand"          ;C++
1071                       "bitor"           ;C++
1072                       "bool"            ;C++, C9X macro
1073                       "break"           ;K&R, C89
1074                       "case"            ;K&R, C89
1075                       "catch"           ;C++
1076                       "char"            ;K&R, C89
1077                       "class"           ;C++
1078                       "complex"         ;C9X macro, C++ template type
1079                       "compl"           ;C++
1080                       "const"           ;C89
1081                       "const_cast"      ;C++
1082                       "continue"        ;K&R, C89
1083                       "defined"         ;C89 preprocessor
1084                       "default"         ;K&R, C89
1085                       "delete"          ;C++
1086                       "do"              ;K&R, C89
1087                       "double"          ;K&R, C89
1088                       "dynamic_cast"    ;C++
1089                       "else"            ;K&R, C89
1090                       ;; "entry"        ;K&R -- never used
1091                       "enum"            ;C89
1092                       "explicit"        ;C++
1093                       "export"          ;C++
1094                       "extern"          ;K&R, C89
1095                       "false"           ;C++, C9X macro
1096                       "float"           ;K&R, C89
1097                       "for"             ;K&R, C89
1098                       ;; "fortran"      ;K&R
1099                       "friend"          ;C++
1100                       "goto"            ;K&R, C89
1101                       "if"              ;K&R, C89
1102                       "imaginary"       ;C9X macro
1103                       "inline"          ;C++, C9X, GCC
1104                       "int"             ;K&R, C89
1105                       "long"            ;K&R, C89
1106                       "mutable"         ;C++
1107                       "namespace"       ;C++
1108                       "new"             ;C++
1109                       "operator"        ;C++
1110                       "or"              ;C++
1111                       "or_eq"           ;C++
1112                       "private"         ;C++
1113                       "protected"       ;C++
1114                       "public"          ;C++
1115                       "register"        ;K&R, C89
1116                       "reinterpret_cast" ;C++
1117                       "restrict"         ;C9X
1118                       "return"           ;K&R, C89
1119                       "short"            ;K&R, C89
1120                       "signed"           ;C89
1121                       "sizeof"           ;K&R, C89
1122                       "static"           ;K&R, C89
1123                       "static_cast"      ;C++
1124                       "struct"           ;K&R, C89
1125                       "switch"           ;K&R, C89
1126                       "template"         ;C++
1127                       "this"             ;C++
1128                       "throw"            ;C++
1129                       "true"             ;C++, C9X macro
1130                       "try"              ;C++
1131                       "this"             ;C++
1132                       "typedef"          ;C89
1133                       "typeid"           ;C++
1134                       "typeof"           ;GCC
1135                       "typename"         ;C++
1136                       "union"            ;K&R, C89
1137                       "unsigned"         ;K&R, C89
1138                       "using"            ;C++
1139                       "virtual"          ;C++
1140                       "void"             ;C89
1141                       "volatile"         ;C89
1142                       "wchar_t"          ;C++, C89 library type
1143                       "while"            ;K&R, C89
1144                       "xor"              ;C++
1145                       "xor_eq"           ;C++
1146                       "_Bool"            ;C9X
1147                       "_Complex"         ;C9X
1148                       "_Imaginary"       ;C9X
1149                       "_Pragma"          ;C9X preprocessor
1150                       "__alignof__"      ;GCC
1151                       "__asm__"          ;GCC
1152                       "__attribute__"    ;GCC
1153                       "__complex__"      ;GCC
1154                       "__const__"        ;GCC
1155                       "__extension__"    ;GCC
1156                       "__imag__"         ;GCC
1157                       "__inline__"       ;GCC
1158                       "__label__"        ;GCC
1159                       "__real__"         ;GCC
1160                       "__signed__"       ;GCC
1161                       "__typeof__"       ;GCC
1162                       "__volatile__"     ;GCC
1163                       ))
1164         (preprocessor-keywords
1165          (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
1166                       "ident" "if" "ifdef" "ifndef" "import" "include"
1167                       "line" "pragma" "unassert" "undef" "warning"))
1168         (objc-keywords
1169          (mdw-regexps "class" "defs" "encode" "end" "implementation"
1170                       "interface" "private" "protected" "protocol" "public"
1171                       "selector")))
1172
1173     (setq font-lock-keywords
1174           (list
1175
1176            ;; Fontify include files as strings.
1177            (list (concat "^[ \t]*\\#[ \t]*"
1178                          "\\(include\\|import\\)"
1179                          "[ \t]*\\(<[^>]+\\(>\\|\\)\\)")
1180                  '(2 font-lock-string-face))
1181
1182            ;; Preprocessor directives are `references'?.
1183            (list (concat "^\\([ \t]*#[ \t]*\\(\\("
1184                          preprocessor-keywords
1185                          "\\)\\>\\|[0-9]+\\|$\\)\\)")
1186                  '(1 font-lock-keyword-face))
1187
1188            ;; Handle the keywords defined above.
1189            (list (concat "@\\<\\(" objc-keywords "\\)\\>")
1190                  '(0 font-lock-keyword-face))
1191
1192            (list (concat "\\<\\(" c-keywords "\\)\\>")
1193                  '(0 font-lock-keyword-face))
1194
1195            ;; Handle numbers too.
1196            ;;
1197            ;; This looks strange, I know.  It corresponds to the
1198            ;; preprocessor's idea of what a number looks like, rather than
1199            ;; anything sensible.
1200            (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
1201                          "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
1202                  '(0 mdw-number-face))
1203
1204            ;; And anything else is punctuation.
1205            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1206                  '(0 mdw-punct-face))))
1207
1208     (mdw-post-config-mode-hack)))
1209
1210 ;;;--------------------------------------------------------------------------
1211 ;;; AP calc mode.
1212
1213 (defun apcalc-mode ()
1214   (interactive)
1215   (c-mode)
1216   (setq major-mode 'apcalc-mode)
1217   (setq mode-name "AP Calc")
1218   (run-hooks 'apcalc-mode-hook))
1219
1220 (defun mdw-fontify-apcalc ()
1221
1222   ;; Fiddle with some syntax codes.
1223   (modify-syntax-entry ?* ". 23")
1224   (modify-syntax-entry ?/ ". 14")
1225
1226   ;; Other stuff.
1227   (mdw-c-style)
1228   (setq c-hanging-comment-ender-p nil)
1229   (setq c-backslash-column 72)
1230   (setq comment-start "/* ")
1231   (setq comment-end " */")
1232   (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
1233
1234   ;; Now define things to be fontified.
1235   (make-local-variable 'font-lock-keywords)
1236   (let ((c-keywords
1237          (mdw-regexps "break" "case" "cd" "continue" "define" "default"
1238                       "do" "else" "exit" "for" "global" "goto" "help" "if"
1239                       "local" "mat" "obj" "print" "quit" "read" "return"
1240                       "show" "static" "switch" "while" "write")))
1241
1242     (setq font-lock-keywords
1243           (list
1244
1245            ;; Handle the keywords defined above.
1246            (list (concat "\\<\\(" c-keywords "\\)\\>")
1247                  '(0 font-lock-keyword-face))
1248
1249            ;; Handle numbers too.
1250            ;;
1251            ;; This looks strange, I know.  It corresponds to the
1252            ;; preprocessor's idea of what a number looks like, rather than
1253            ;; anything sensible.
1254            (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
1255                          "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
1256                  '(0 mdw-number-face))
1257
1258            ;; And anything else is punctuation.
1259            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1260                  '(0 mdw-punct-face)))))
1261
1262   (mdw-post-config-mode-hack))
1263
1264 ;;;--------------------------------------------------------------------------
1265 ;;; Java programming configuration.
1266
1267 ;; Make indentation nice.
1268
1269 (defun mdw-java-style ()
1270   (c-add-style "[mdw] Java style"
1271                '((c-basic-offset . 2)
1272                  (c-offsets-alist (substatement-open . 0)
1273                                   (label . +)
1274                                   (case-label . +)
1275                                   (access-label . 0)
1276                                   (inclass . +)
1277                                   (statement-case-intro . +)))
1278                t))
1279
1280 ;; Declare Java fontification style.
1281
1282 (defun mdw-fontify-java ()
1283
1284   ;; Other stuff.
1285   (mdw-java-style)
1286   (setq c-hanging-comment-ender-p nil)
1287   (setq c-backslash-column 72)
1288   (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
1289
1290   ;; Now define things to be fontified.
1291   (make-local-variable 'font-lock-keywords)
1292   (let ((java-keywords
1293          (mdw-regexps "abstract" "boolean" "break" "byte" "case" "catch"
1294                       "char" "class" "const" "continue" "default" "do"
1295                       "double" "else" "extends" "final" "finally" "float"
1296                       "for" "goto" "if" "implements" "import" "instanceof"
1297                       "int" "interface" "long" "native" "new" "package"
1298                       "private" "protected" "public" "return" "short"
1299                       "static" "super" "switch" "synchronized" "this"
1300                       "throw" "throws" "transient" "try" "void" "volatile"
1301                       "while"
1302
1303                       "false" "null" "true")))
1304
1305     (setq font-lock-keywords
1306           (list
1307
1308            ;; Handle the keywords defined above.
1309            (list (concat "\\<\\(" java-keywords "\\)\\>")
1310                  '(0 font-lock-keyword-face))
1311
1312            ;; Handle numbers too.
1313            ;;
1314            ;; The following isn't quite right, but it's close enough.
1315            (list (concat "\\<\\("
1316                          "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
1317                          "[0-9]+\\(\\.[0-9]*\\|\\)"
1318                          "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
1319                          "[lLfFdD]?")
1320                  '(0 mdw-number-face))
1321
1322            ;; And anything else is punctuation.
1323            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1324                  '(0 mdw-punct-face)))))
1325
1326   (mdw-post-config-mode-hack))
1327
1328 ;;;--------------------------------------------------------------------------
1329 ;;; C# programming configuration.
1330
1331 ;; Make indentation nice.
1332
1333 (defun mdw-csharp-style ()
1334   (c-add-style "[mdw] C# style"
1335                '((c-basic-offset . 2)
1336                  (c-offsets-alist (substatement-open . 0)
1337                                   (label . 0)
1338                                   (case-label . +)
1339                                   (access-label . 0)
1340                                   (inclass . +)
1341                                   (statement-case-intro . +)))
1342                t))
1343
1344 ;; Declare C# fontification style.
1345
1346 (defun mdw-fontify-csharp ()
1347
1348   ;; Other stuff.
1349   (mdw-csharp-style)
1350   (setq c-hanging-comment-ender-p nil)
1351   (setq c-backslash-column 72)
1352   (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
1353
1354   ;; Now define things to be fontified.
1355   (make-local-variable 'font-lock-keywords)
1356   (let ((csharp-keywords
1357          (mdw-regexps "abstract" "as" "base" "bool" "break"
1358                       "byte" "case" "catch" "char" "checked"
1359                       "class" "const" "continue" "decimal" "default"
1360                       "delegate" "do" "double" "else" "enum"
1361                       "event" "explicit" "extern" "false" "finally"
1362                       "fixed" "float" "for" "foreach" "goto"
1363                       "if" "implicit" "in" "int" "interface"
1364                       "internal" "is" "lock" "long" "namespace"
1365                       "new" "null" "object" "operator" "out"
1366                       "override" "params" "private" "protected" "public"
1367                       "readonly" "ref" "return" "sbyte" "sealed"
1368                       "short" "sizeof" "stackalloc" "static" "string"
1369                       "struct" "switch" "this" "throw" "true"
1370                       "try" "typeof" "uint" "ulong" "unchecked"
1371                       "unsafe" "ushort" "using" "virtual" "void"
1372                       "volatile" "while" "yield")))
1373
1374     (setq font-lock-keywords
1375           (list
1376
1377            ;; Handle the keywords defined above.
1378            (list (concat "\\<\\(" csharp-keywords "\\)\\>")
1379                  '(0 font-lock-keyword-face))
1380
1381            ;; Handle numbers too.
1382            ;;
1383            ;; The following isn't quite right, but it's close enough.
1384            (list (concat "\\<\\("
1385                          "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
1386                          "[0-9]+\\(\\.[0-9]*\\|\\)"
1387                          "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
1388                          "[lLfFdD]?")
1389                  '(0 mdw-number-face))
1390
1391            ;; And anything else is punctuation.
1392            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1393                  '(0 mdw-punct-face)))))
1394
1395   (mdw-post-config-mode-hack))
1396
1397 (define-derived-mode csharp-mode java-mode "C#"
1398   "Major mode for editing C# code.")
1399
1400 ;;;--------------------------------------------------------------------------
1401 ;;; Go programming configuration.
1402
1403 (defun mdw-fontify-go ()
1404
1405   (make-local-variable 'font-lock-keywords)
1406   (let ((go-keywords
1407          (mdw-regexps "break" "case" "chan" "const" "continue"
1408                       "default" "defer" "else" "fallthrough" "for"
1409                       "func" "go" "goto" "if" "import"
1410                       "interface" "map" "package" "range" "return"
1411                       "select" "struct" "switch" "type" "var")))
1412
1413     (setq font-lock-keywords
1414           (list
1415
1416            ;; Handle the keywords defined above.
1417            (list (concat "\\<\\(" go-keywords "\\)\\>")
1418                  '(0 font-lock-keyword-face))
1419
1420            ;; Handle numbers too.
1421            ;;
1422            ;; The following isn't quite right, but it's close enough.
1423            (list (concat "\\<\\("
1424                          "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
1425                          "[0-9]+\\(\\.[0-9]*\\|\\)"
1426                          "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)")
1427                  '(0 mdw-number-face))
1428
1429            ;; And anything else is punctuation.
1430            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1431                  '(0 mdw-punct-face)))))
1432
1433   (mdw-post-config-mode-hack))
1434
1435 ;;;--------------------------------------------------------------------------
1436 ;;; Awk programming configuration.
1437
1438 ;; Make Awk indentation nice.
1439
1440 (defun mdw-awk-style ()
1441   (c-add-style "[mdw] Awk style"
1442                '((c-basic-offset . 2)
1443                  (c-offsets-alist (substatement-open . 0)
1444                                   (statement-cont . 0)
1445                                   (statement-case-intro . +)))
1446                t))
1447
1448 ;; Declare Awk fontification style.
1449
1450 (defun mdw-fontify-awk ()
1451
1452   ;; Miscellaneous fiddling.
1453   (mdw-awk-style)
1454   (setq c-backslash-column 72)
1455   (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
1456
1457   ;; Now define things to be fontified.
1458   (make-local-variable 'font-lock-keywords)
1459   (let ((c-keywords
1460          (mdw-regexps "BEGIN" "END" "ARGC" "ARGIND" "ARGV" "CONVFMT"
1461                       "ENVIRON" "ERRNO" "FIELDWIDTHS" "FILENAME" "FNR"
1462                       "FS" "IGNORECASE" "NF" "NR" "OFMT" "OFS" "ORS" "RS"
1463                       "RSTART" "RLENGTH" "RT"   "SUBSEP"
1464                       "atan2" "break" "close" "continue" "cos" "delete"
1465                       "do" "else" "exit" "exp" "fflush" "file" "for" "func"
1466                       "function" "gensub" "getline" "gsub" "if" "in"
1467                       "index" "int" "length" "log" "match" "next" "rand"
1468                       "return" "print" "printf" "sin" "split" "sprintf"
1469                       "sqrt" "srand" "strftime" "sub" "substr" "system"
1470                       "systime" "tolower" "toupper" "while")))
1471
1472     (setq font-lock-keywords
1473           (list
1474
1475            ;; Handle the keywords defined above.
1476            (list (concat "\\<\\(" c-keywords "\\)\\>")
1477                  '(0 font-lock-keyword-face))
1478
1479            ;; Handle numbers too.
1480            ;;
1481            ;; The following isn't quite right, but it's close enough.
1482            (list (concat "\\<\\("
1483                          "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
1484                          "[0-9]+\\(\\.[0-9]*\\|\\)"
1485                          "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
1486                          "[uUlL]*")
1487                  '(0 mdw-number-face))
1488
1489            ;; And anything else is punctuation.
1490            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1491                  '(0 mdw-punct-face)))))
1492
1493   (mdw-post-config-mode-hack))
1494
1495 ;;;--------------------------------------------------------------------------
1496 ;;; Perl programming style.
1497
1498 ;; Perl indentation style.
1499
1500 (setq cperl-indent-level 2)
1501 (setq cperl-continued-statement-offset 2)
1502 (setq cperl-continued-brace-offset 0)
1503 (setq cperl-brace-offset -2)
1504 (setq cperl-brace-imaginary-offset 0)
1505 (setq cperl-label-offset 0)
1506
1507 ;; Define perl fontification style.
1508
1509 (defun mdw-fontify-perl ()
1510
1511   ;; Miscellaneous fiddling.
1512   (modify-syntax-entry ?$ "\\")
1513   (modify-syntax-entry ?$ "\\" font-lock-syntax-table)
1514   (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
1515
1516   ;; Now define fontification things.
1517   (make-local-variable 'font-lock-keywords)
1518   (let ((perl-keywords
1519          (mdw-regexps "and" "cmp" "continue" "do" "else" "elsif" "eq"
1520                       "for" "foreach" "ge" "gt" "goto" "if"
1521                       "last" "le" "lt" "local" "my" "ne" "next" "or"
1522                       "package" "redo" "require" "return" "sub"
1523                       "undef" "unless" "until" "use" "while")))
1524
1525     (setq font-lock-keywords
1526           (list
1527
1528            ;; Set up the keywords defined above.
1529            (list (concat "\\<\\(" perl-keywords "\\)\\>")
1530                  '(0 font-lock-keyword-face))
1531
1532            ;; At least numbers are simpler than C.
1533            (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
1534                          "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)"
1535                          "\\([eE]\\([-+]\\|\\)[0-9_]+\\|\\)")
1536                  '(0 mdw-number-face))
1537
1538            ;; And anything else is punctuation.
1539            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1540                  '(0 mdw-punct-face)))))
1541
1542   (mdw-post-config-mode-hack))
1543
1544 (defun perl-number-tests (&optional arg)
1545   "Assign consecutive numbers to lines containing `#t'.  With ARG,
1546 strip numbers instead."
1547   (interactive "P")
1548   (save-excursion
1549     (goto-char (point-min))
1550     (let ((i 0) (fmt (if arg "" " %4d")))
1551       (while (search-forward "#t" nil t)
1552         (delete-region (point) (line-end-position))
1553         (setq i (1+ i))
1554         (insert (format fmt i)))
1555       (goto-char (point-min))
1556       (if (re-search-forward "\\(tests\\s-*=>\\s-*\\)\\w*" nil t)
1557           (replace-match (format "\\1%d" i))))))
1558
1559 ;;;--------------------------------------------------------------------------
1560 ;;; Python programming style.
1561
1562 (defun mdw-fontify-pythonic (keywords)
1563
1564   ;; Miscellaneous fiddling.
1565   (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
1566   (setq indent-tabs-mode nil)
1567
1568   ;; Now define fontification things.
1569   (make-local-variable 'font-lock-keywords)
1570   (setq font-lock-keywords
1571         (list
1572
1573          ;; Set up the keywords defined above.
1574          (list (concat "\\<\\(" keywords "\\)\\>")
1575                '(0 font-lock-keyword-face))
1576
1577          ;; At least numbers are simpler than C.
1578          (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
1579                        "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)"
1580                        "\\([eE]\\([-+]\\|\\)[0-9_]+\\|[lL]\\|\\)")
1581                '(0 mdw-number-face))
1582
1583          ;; And anything else is punctuation.
1584          (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1585                '(0 mdw-punct-face))))
1586
1587   (mdw-post-config-mode-hack))
1588
1589 ;; Define Python fontification styles.
1590
1591 (defun mdw-fontify-python ()
1592   (mdw-fontify-pythonic
1593    (mdw-regexps "and" "as" "assert" "break" "class" "continue" "def"
1594                 "del" "elif" "else" "except" "exec" "finally" "for"
1595                 "from" "global" "if" "import" "in" "is" "lambda"
1596                 "not" "or" "pass" "print" "raise" "return" "try"
1597                 "while" "with" "yield")))
1598
1599 (defun mdw-fontify-pyrex ()
1600   (mdw-fontify-pythonic
1601    (mdw-regexps "and" "as" "assert" "break" "cdef" "class" "continue"
1602                 "ctypedef" "def" "del" "elif" "else" "except" "exec"
1603                 "extern" "finally" "for" "from" "global" "if"
1604                 "import" "in" "is" "lambda" "not" "or" "pass" "print"
1605                 "raise" "return" "struct" "try" "while" "with"
1606                 "yield")))
1607
1608 ;;;--------------------------------------------------------------------------
1609 ;;; Icon programming style.
1610
1611 ;; Icon indentation style.
1612
1613 (setq icon-brace-offset 0
1614       icon-continued-brace-offset 0
1615       icon-continued-statement-offset 2
1616       icon-indent-level 2)
1617
1618 ;; Define Icon fontification style.
1619
1620 (defun mdw-fontify-icon ()
1621
1622   ;; Miscellaneous fiddling.
1623   (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
1624
1625   ;; Now define fontification things.
1626   (make-local-variable 'font-lock-keywords)
1627   (let ((icon-keywords
1628          (mdw-regexps "break" "by" "case" "create" "default" "do" "else"
1629                       "end" "every" "fail" "global" "if" "initial"
1630                       "invocable" "link" "local" "next" "not" "of"
1631                       "procedure" "record" "repeat" "return" "static"
1632                       "suspend" "then" "to" "until" "while"))
1633         (preprocessor-keywords
1634          (mdw-regexps "define" "else" "endif" "error" "ifdef" "ifndef"
1635                       "include" "line" "undef")))
1636     (setq font-lock-keywords
1637           (list
1638
1639            ;; Set up the keywords defined above.
1640            (list (concat "\\<\\(" icon-keywords "\\)\\>")
1641                  '(0 font-lock-keyword-face))
1642
1643            ;; The things that Icon calls keywords.
1644            (list "&\\sw+\\>" '(0 font-lock-variable-name-face))
1645
1646            ;; At least numbers are simpler than C.
1647            (list (concat "\\<[0-9]+"
1648                          "\\([rR][0-9a-zA-Z]+\\|"
1649                          "\\.[0-9]+\\([eE][+-]?[0-9]+\\)?\\)\\>\\|"
1650                          "\\.[0-9]+\\([eE][+-]?[0-9]+\\)?\\>")
1651                  '(0 mdw-number-face))
1652
1653            ;; Preprocessor.
1654            (list (concat "^[ \t]*$[ \t]*\\<\\("
1655                          preprocessor-keywords
1656                          "\\)\\>")
1657                  '(0 font-lock-keyword-face))
1658
1659            ;; And anything else is punctuation.
1660            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1661                  '(0 mdw-punct-face)))))
1662
1663   (mdw-post-config-mode-hack))
1664
1665 ;;;--------------------------------------------------------------------------
1666 ;;; ARM assembler programming configuration.
1667
1668 ;; There doesn't appear to be an Emacs mode for this yet.
1669 ;;
1670 ;; Better do something about that, I suppose.
1671
1672 (defvar arm-assembler-mode-map nil)
1673 (defvar arm-assembler-abbrev-table nil)
1674 (defvar arm-assembler-mode-syntax-table (make-syntax-table))
1675
1676 (or arm-assembler-mode-map
1677     (progn
1678       (setq arm-assembler-mode-map (make-sparse-keymap))
1679       (define-key arm-assembler-mode-map "\C-m" 'arm-assembler-newline)
1680       (define-key arm-assembler-mode-map [C-return] 'newline)
1681       (define-key arm-assembler-mode-map "\t" 'tab-to-tab-stop)))
1682
1683 (defun arm-assembler-mode ()
1684   "Major mode for ARM assembler programs"
1685   (interactive)
1686
1687   ;; Do standard major mode things.
1688   (kill-all-local-variables)
1689   (use-local-map arm-assembler-mode-map)
1690   (setq local-abbrev-table arm-assembler-abbrev-table)
1691   (setq major-mode 'arm-assembler-mode)
1692   (setq mode-name "ARM assembler")
1693
1694   ;; Set up syntax table.
1695   (set-syntax-table arm-assembler-mode-syntax-table)
1696   (modify-syntax-entry ?;   ; Nasty hack
1697                        "<" arm-assembler-mode-syntax-table)
1698   (modify-syntax-entry ?\n ">" arm-assembler-mode-syntax-table)
1699   (modify-syntax-entry ?_ "_" arm-assembler-mode-syntax-table)
1700
1701   (make-local-variable 'comment-start)
1702   (setq comment-start ";")
1703   (make-local-variable 'comment-end)
1704   (setq comment-end "")
1705   (make-local-variable 'comment-column)
1706   (setq comment-column 48)
1707   (make-local-variable 'comment-start-skip)
1708   (setq comment-start-skip ";+[ \t]*")
1709
1710   ;; Play with indentation.
1711   (make-local-variable 'indent-line-function)
1712   (setq indent-line-function 'indent-relative-maybe)
1713
1714   ;; Set fill prefix.
1715   (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)")
1716
1717   ;; Fiddle with fontification.
1718   (make-local-variable 'font-lock-keywords)
1719   (setq font-lock-keywords
1720         (list
1721
1722          ;; Handle numbers too.
1723          ;;
1724          ;; The following isn't quite right, but it's close enough.
1725          (list (concat "\\("
1726                        "&[0-9a-fA-F]+\\|"
1727                        "\\<[0-9]+\\(\\.[0-9]*\\|_[0-9a-zA-Z]+\\|\\)"
1728                        "\\)")
1729                '(0 mdw-number-face))
1730
1731          ;; Do something about operators.
1732          (list "^[^ \t]*[ \t]+\\(GET\\|LNK\\)[ \t]+\\([^;\n]*\\)"
1733                '(1 font-lock-keyword-face)
1734                '(2 font-lock-string-face))
1735          (list ":[a-zA-Z]+:"
1736                '(0 font-lock-keyword-face))
1737
1738          ;; Do menemonics and directives.
1739          (list "^[^ \t]*[ \t]+\\([a-zA-Z]+\\)"
1740                '(1 font-lock-keyword-face))
1741
1742          ;; And anything else is punctuation.
1743          (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1744                '(0 mdw-punct-face)))
1745
1746   (mdw-post-config-mode-hack))
1747   (run-hooks 'arm-assembler-mode-hook))
1748
1749 ;;;--------------------------------------------------------------------------
1750 ;;; Assembler mode.
1751
1752 (defun mdw-fontify-asm ()
1753   (modify-syntax-entry ?' "\"")
1754   (modify-syntax-entry ?. "w")
1755   (setf fill-prefix nil)
1756   (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)"))
1757
1758 ;;;--------------------------------------------------------------------------
1759 ;;; TCL configuration.
1760
1761 (defun mdw-fontify-tcl ()
1762   (mapcar #'(lambda (ch) (modify-syntax-entry ch ".")) '(?$))
1763   (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
1764   (make-local-variable 'font-lock-keywords)
1765   (setq font-lock-keywords
1766         (list
1767          (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
1768                        "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)"
1769                        "\\([eE]\\([-+]\\|\\)[0-9_]+\\|\\)")
1770                '(0 mdw-number-face))
1771          (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1772                '(0 mdw-punct-face))))
1773   (mdw-post-config-mode-hack))
1774
1775 ;;;--------------------------------------------------------------------------
1776 ;;; REXX configuration.
1777
1778 (defun mdw-rexx-electric-* ()
1779   (interactive)
1780   (insert ?*)
1781   (rexx-indent-line))
1782
1783 (defun mdw-rexx-indent-newline-indent ()
1784   (interactive)
1785   (rexx-indent-line)
1786   (if abbrev-mode (expand-abbrev))
1787   (newline-and-indent))
1788
1789 (defun mdw-fontify-rexx ()
1790
1791   ;; Various bits of fiddling.
1792   (setq mdw-auto-indent nil)
1793   (local-set-key [?\C-m] 'mdw-rexx-indent-newline-indent)
1794   (local-set-key [?*] 'mdw-rexx-electric-*)
1795   (mapcar #'(lambda (ch) (modify-syntax-entry ch "w"))
1796           '(?! ?? ?# ?@ ?$))
1797   (mdw-standard-fill-prefix "\\([ \t]*/?\*[ \t]*\\)")
1798
1799   ;; Set up keywords and things for fontification.
1800   (make-local-variable 'font-lock-keywords-case-fold-search)
1801   (setq font-lock-keywords-case-fold-search t)
1802
1803   (setq rexx-indent 2)
1804   (setq rexx-end-indent rexx-indent)
1805   (setq rexx-cont-indent rexx-indent)
1806
1807   (make-local-variable 'font-lock-keywords)
1808   (let ((rexx-keywords
1809          (mdw-regexps "address" "arg" "by" "call" "digits" "do" "drop"
1810                       "else" "end" "engineering" "exit" "expose" "for"
1811                       "forever" "form" "fuzz" "if" "interpret" "iterate"
1812                       "leave" "linein" "name" "nop" "numeric" "off" "on"
1813                       "options" "otherwise" "parse" "procedure" "pull"
1814                       "push" "queue" "return" "say" "select" "signal"
1815                       "scientific" "source" "then" "trace" "to" "until"
1816                       "upper" "value" "var" "version" "when" "while"
1817                       "with"
1818
1819                       "abbrev" "abs" "bitand" "bitor" "bitxor" "b2x"
1820                       "center" "center" "charin" "charout" "chars"
1821                       "compare" "condition" "copies" "c2d" "c2x"
1822                       "datatype" "date" "delstr" "delword" "d2c" "d2x"
1823                       "errortext" "format" "fuzz" "insert" "lastpos"
1824                       "left" "length" "lineout" "lines" "max" "min"
1825                       "overlay" "pos" "queued" "random" "reverse" "right"
1826                       "sign" "sourceline" "space" "stream" "strip"
1827                       "substr" "subword" "symbol" "time" "translate"
1828                       "trunc" "value" "verify" "word" "wordindex"
1829                       "wordlength" "wordpos" "words" "xrange" "x2b" "x2c"
1830                       "x2d")))
1831
1832     (setq font-lock-keywords
1833           (list
1834
1835            ;; Set up the keywords defined above.
1836            (list (concat "\\<\\(" rexx-keywords "\\)\\>")
1837                  '(0 font-lock-keyword-face))
1838
1839            ;; Fontify all symbols the same way.
1840            (list (concat "\\<\\([0-9.][A-Za-z0-9.!?_#@$]*[Ee][+-]?[0-9]+\\|"
1841                          "[A-Za-z0-9.!?_#@$]+\\)")
1842                  '(0 font-lock-variable-name-face))
1843
1844            ;; And everything else is punctuation.
1845            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1846                  '(0 mdw-punct-face)))))
1847
1848   (mdw-post-config-mode-hack))
1849
1850 ;;;--------------------------------------------------------------------------
1851 ;;; Standard ML programming style.
1852
1853 (defun mdw-fontify-sml ()
1854
1855   ;; Make underscore an honorary letter.
1856   (modify-syntax-entry ?' "w")
1857
1858   ;; Set fill prefix.
1859   (mdw-standard-fill-prefix "\\([ \t]*(\*[ \t]*\\)")
1860
1861   ;; Now define fontification things.
1862   (make-local-variable 'font-lock-keywords)
1863   (let ((sml-keywords
1864          (mdw-regexps "abstype" "and" "andalso" "as"
1865                       "case"
1866                       "datatype" "do"
1867                       "else" "end" "eqtype" "exception"
1868                       "fn" "fun" "functor"
1869                       "handle"
1870                       "if" "in" "include" "infix" "infixr"
1871                       "let" "local"
1872                       "nonfix"
1873                       "of" "op" "open" "orelse"
1874                       "raise" "rec"
1875                       "sharing" "sig" "signature" "struct" "structure"
1876                       "then" "type"
1877                       "val"
1878                       "where" "while" "with" "withtype")))
1879
1880     (setq font-lock-keywords
1881           (list
1882
1883            ;; Set up the keywords defined above.
1884            (list (concat "\\<\\(" sml-keywords "\\)\\>")
1885                  '(0 font-lock-keyword-face))
1886
1887            ;; At least numbers are simpler than C.
1888            (list (concat "\\<\\(\\~\\|\\)"
1889                             "\\(0\\(\\([wW]\\|\\)[xX][0-9a-fA-F]+\\|"
1890                                    "[wW][0-9]+\\)\\|"
1891                                 "\\([0-9]+\\(\\.[0-9]+\\|\\)"
1892                                          "\\([eE]\\(\\~\\|\\)"
1893                                                 "[0-9]+\\|\\)\\)\\)")
1894                  '(0 mdw-number-face))
1895
1896            ;; And anything else is punctuation.
1897            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1898                  '(0 mdw-punct-face)))))
1899
1900   (mdw-post-config-mode-hack))
1901
1902 ;;;--------------------------------------------------------------------------
1903 ;;; Haskell configuration.
1904
1905 (defun mdw-fontify-haskell ()
1906
1907   ;; Fiddle with syntax table to get comments right.
1908   (modify-syntax-entry ?' "\"")
1909   (modify-syntax-entry ?- ". 123")
1910   (modify-syntax-entry ?{ ". 1b")
1911   (modify-syntax-entry ?} ". 4b")
1912   (modify-syntax-entry ?\n ">")
1913
1914   ;; Set fill prefix.
1915   (mdw-standard-fill-prefix "\\([ \t]*{?--?[ \t]*\\)")
1916
1917   ;; Fiddle with fontification.
1918   (make-local-variable 'font-lock-keywords)
1919   (let ((haskell-keywords
1920          (mdw-regexps "as" "case" "ccall" "class" "data" "default"
1921                       "deriving" "do" "else" "foreign" "hiding" "if"
1922                       "import" "in" "infix" "infixl" "infixr" "instance"
1923                       "let" "module" "newtype" "of" "qualified" "safe"
1924                       "stdcall" "then" "type" "unsafe" "where")))
1925
1926     (setq font-lock-keywords
1927           (list
1928            (list "--.*$"
1929                  '(0 font-lock-comment-face))
1930            (list (concat "\\<\\(" haskell-keywords "\\)\\>")
1931                  '(0 font-lock-keyword-face))
1932            (list (concat "\\<0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
1933                          "\\<[0-9][0-9_]*\\(\\.[0-9]*\\|\\)"
1934                          "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)")
1935                  '(0 mdw-number-face))
1936            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1937                  '(0 mdw-punct-face)))))
1938
1939   (mdw-post-config-mode-hack))
1940
1941 ;;;--------------------------------------------------------------------------
1942 ;;; Erlang configuration.
1943
1944 (setq erlang-electric-commands nil)
1945
1946 (defun mdw-fontify-erlang ()
1947
1948   ;; Set fill prefix.
1949   (mdw-standard-fill-prefix "\\([ \t]*{?%*[ \t]*\\)")
1950
1951   ;; Fiddle with fontification.
1952   (make-local-variable 'font-lock-keywords)
1953   (let ((erlang-keywords
1954          (mdw-regexps "after" "and" "andalso"
1955                       "band" "begin" "bnot" "bor" "bsl" "bsr" "bxor"
1956                       "case" "catch" "cond"
1957                       "div" "end" "fun" "if" "let" "not"
1958                       "of" "or" "orelse"
1959                       "query" "receive" "rem" "try" "when" "xor")))
1960
1961     (setq font-lock-keywords
1962           (list
1963            (list "%.*$"
1964                  '(0 font-lock-comment-face))
1965            (list (concat "\\<\\(" erlang-keywords "\\)\\>")
1966                  '(0 font-lock-keyword-face))
1967            (list (concat "^-\\sw+\\>")
1968                  '(0 font-lock-keyword-face))
1969            (list "\\<[0-9]+\\(\\|#[0-9a-zA-Z]+\\|[eE][+-]?[0-9]+\\)\\>"
1970                  '(0 mdw-number-face))
1971            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1972                  '(0 mdw-punct-face)))))
1973
1974   (mdw-post-config-mode-hack))
1975
1976 ;;;--------------------------------------------------------------------------
1977 ;;; Texinfo configuration.
1978
1979 (defun mdw-fontify-texinfo ()
1980
1981   ;; Set fill prefix.
1982   (mdw-standard-fill-prefix "\\([ \t]*@c[ \t]+\\)")
1983
1984   ;; Real fontification things.
1985   (make-local-variable 'font-lock-keywords)
1986   (setq font-lock-keywords
1987         (list
1988
1989          ;; Environment names are keywords.
1990          (list "@\\(end\\)  *\\([a-zA-Z]*\\)?"
1991                '(2 font-lock-keyword-face))
1992
1993          ;; Unmark escaped magic characters.
1994          (list "\\(@\\)\\([@{}]\\)"
1995                '(1 font-lock-keyword-face)
1996                '(2 font-lock-variable-name-face))
1997
1998          ;; Make sure we get comments properly.
1999          (list "@c\\(\\|omment\\)\\( .*\\)?$"
2000                '(0 font-lock-comment-face))
2001
2002          ;; Command names are keywords.
2003          (list "@\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)"
2004                '(0 font-lock-keyword-face))
2005
2006          ;; Fontify TeX special characters as punctuation.
2007          (list "[{}]+"
2008                '(0 mdw-punct-face))))
2009
2010   (mdw-post-config-mode-hack))
2011
2012 ;;;--------------------------------------------------------------------------
2013 ;;; TeX and LaTeX configuration.
2014
2015 (defun mdw-fontify-tex ()
2016   (setq ispell-parser 'tex)
2017   (turn-on-reftex)
2018
2019   ;; Don't make maths into a string.
2020   (modify-syntax-entry ?$ ".")
2021   (modify-syntax-entry ?$ "." font-lock-syntax-table)
2022   (local-set-key [?$] 'self-insert-command)
2023
2024   ;; Set fill prefix.
2025   (mdw-standard-fill-prefix "\\([ \t]*%+[ \t]*\\)")
2026
2027   ;; Real fontification things.
2028   (make-local-variable 'font-lock-keywords)
2029   (setq font-lock-keywords
2030         (list
2031
2032          ;; Environment names are keywords.
2033          (list (concat "\\\\\\(begin\\|end\\|newenvironment\\)"
2034                        "{\\([^}\n]*\\)}")
2035                '(2 font-lock-keyword-face))
2036
2037          ;; Suspended environment names are keywords too.
2038          (list (concat "\\\\\\(suspend\\|resume\\)\\(\\[[^]]*\\]\\)?"
2039                        "{\\([^}\n]*\\)}")
2040                '(3 font-lock-keyword-face))
2041
2042          ;; Command names are keywords.
2043          (list "\\\\\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)"
2044                '(0 font-lock-keyword-face))
2045
2046          ;; Handle @/.../ for italics.
2047          ;; (list "\\(@/\\)\\([^/]*\\)\\(/\\)"
2048          ;;       '(1 font-lock-keyword-face)
2049          ;;       '(3 font-lock-keyword-face))
2050
2051          ;; Handle @*...* for boldness.
2052          ;; (list "\\(@\\*\\)\\([^*]*\\)\\(\\*\\)"
2053          ;;       '(1 font-lock-keyword-face)
2054          ;;       '(3 font-lock-keyword-face))
2055
2056          ;; Handle @`...' for literal syntax things.
2057          ;; (list "\\(@`\\)\\([^']*\\)\\('\\)"
2058          ;;       '(1 font-lock-keyword-face)
2059          ;;       '(3 font-lock-keyword-face))
2060
2061          ;; Handle @<...> for nonterminals.
2062          ;; (list "\\(@<\\)\\([^>]*\\)\\(>\\)"
2063          ;;       '(1 font-lock-keyword-face)
2064          ;;       '(3 font-lock-keyword-face))
2065
2066          ;; Handle other @-commands.
2067          ;; (list "@\\([^a-zA-Z]\\|[a-zA-Z]*\\)"
2068          ;;       '(0 font-lock-keyword-face))
2069
2070          ;; Make sure we get comments properly.
2071          (list "%.*"
2072                '(0 font-lock-comment-face))
2073
2074          ;; Fontify TeX special characters as punctuation.
2075          (list "[$^_{}#&]"
2076                '(0 mdw-punct-face))))
2077
2078   (mdw-post-config-mode-hack))
2079
2080 ;;;--------------------------------------------------------------------------
2081 ;;; SGML hacking.
2082
2083 (defun mdw-sgml-mode ()
2084   (interactive)
2085   (sgml-mode)
2086   (mdw-standard-fill-prefix "")
2087   (make-local-variable 'sgml-delimiters)
2088   (setq sgml-delimiters
2089         '("AND" "&" "COM" "--" "CRO" "&#" "DSC" "]" "DSO" "[" "DTGC" "]"
2090           "DTGO" "[" "ERO" "&" "ETAGO" ":e" "GRPC" ")" "GRPO" "(" "LIT" "\""
2091           "LITA" "'" "MDC" ">" "MDO" "<!" "MINUS" "-" "MSC" "]]" "NESTC" "{"
2092           "NET" "}" "OPT" "?" "OR" "|" "PERO" "%" "PIC" ">" "PIO" "<?"
2093           "PLUS" "+" "REFC" "." "REP" "*" "RNI" "#" "SEQ" "," "STAGO" ":"
2094           "TAGC" "." "VI" "=" "MS-START" "<![" "MS-END" "]]>"
2095           "XML-ECOM" "-->" "XML-PIC" "?>" "XML-SCOM" "<!--" "XML-TAGCE" "/>"
2096           "NULL" ""))
2097   (setq major-mode 'mdw-sgml-mode)
2098   (setq mode-name "[mdw] SGML")
2099   (run-hooks 'mdw-sgml-mode-hook))
2100
2101 ;;;--------------------------------------------------------------------------
2102 ;;; Shell scripts.
2103
2104 (defun mdw-setup-sh-script-mode ()
2105
2106   ;; Fetch the shell interpreter's name.
2107   (let ((shell-name sh-shell-file))
2108
2109     ;; Try reading the hash-bang line.
2110     (save-excursion
2111       (goto-char (point-min))
2112       (if (looking-at "#![ \t]*\\([^ \t\n]*\\)")
2113           (setq shell-name (match-string 1))))
2114
2115     ;; Now try to set the shell.
2116     ;;
2117     ;; Don't let `sh-set-shell' bugger up my script.
2118     (let ((executable-set-magic #'(lambda (s &rest r) s)))
2119       (sh-set-shell shell-name)))
2120
2121   ;; Now enable my keys and the fontification.
2122   (mdw-misc-mode-config)
2123
2124   ;; Set the indentation level correctly.
2125   (setq sh-indentation 2)
2126   (setq sh-basic-offset 2))
2127
2128 ;;;--------------------------------------------------------------------------
2129 ;;; Emacs shell mode.
2130
2131 (defun mdw-eshell-prompt ()
2132   (let ((left "[") (right "]"))
2133     (when (= (user-uid) 0)
2134       (setq left "«" right "»"))
2135     (concat left
2136             (save-match-data
2137               (replace-regexp-in-string "\\..*$" "" (system-name)))
2138             " "
2139             (let* ((pwd (eshell/pwd)) (npwd (length pwd))
2140                    (home (expand-file-name "~")) (nhome (length home)))
2141               (if (and (>= npwd nhome)
2142                        (or (= nhome npwd)
2143                            (= (elt pwd nhome) ?/))
2144                        (string= (substring pwd 0 nhome) home))
2145                   (concat "~" (substring pwd (length home)))
2146                 pwd))
2147             right)))
2148 (setq eshell-prompt-function 'mdw-eshell-prompt)
2149 (setq eshell-prompt-regexp "^\\[[^]>]+\\(\\]\\|>>?\\)")
2150
2151 (defun eshell/e (file) (find-file file) nil)
2152 (defun eshell/ee (file) (find-file-other-window file) nil)
2153 (defun eshell/w3m (url) (w3m-goto-url url) nil)
2154
2155 (mdw-define-face eshell-prompt (t :weight bold))
2156 (mdw-define-face eshell-ls-archive (t :weight bold :foreground "red"))
2157 (mdw-define-face eshell-ls-backup (t :foreground "lightgrey" :slant italic))
2158 (mdw-define-face eshell-ls-product (t :foreground "lightgrey" :slant italic))
2159 (mdw-define-face eshell-ls-clutter (t :foreground "lightgrey" :slant italic))
2160 (mdw-define-face eshell-ls-executable (t :weight bold))
2161 (mdw-define-face eshell-ls-directory (t :foreground "cyan" :weight bold))
2162 (mdw-define-face eshell-ls-readonly (t nil))
2163 (mdw-define-face eshell-ls-symlink (t :foreground "cyan"))
2164
2165 ;;;--------------------------------------------------------------------------
2166 ;;; Messages-file mode.
2167
2168 (defun messages-mode-guts ()
2169   (setq messages-mode-syntax-table (make-syntax-table))
2170   (set-syntax-table messages-mode-syntax-table)
2171   (modify-syntax-entry ?0 "w" messages-mode-syntax-table)
2172   (modify-syntax-entry ?1 "w" messages-mode-syntax-table)
2173   (modify-syntax-entry ?2 "w" messages-mode-syntax-table)
2174   (modify-syntax-entry ?3 "w" messages-mode-syntax-table)
2175   (modify-syntax-entry ?4 "w" messages-mode-syntax-table)
2176   (modify-syntax-entry ?5 "w" messages-mode-syntax-table)
2177   (modify-syntax-entry ?6 "w" messages-mode-syntax-table)
2178   (modify-syntax-entry ?7 "w" messages-mode-syntax-table)
2179   (modify-syntax-entry ?8 "w" messages-mode-syntax-table)
2180   (modify-syntax-entry ?9 "w" messages-mode-syntax-table)
2181   (make-local-variable 'comment-start)
2182   (make-local-variable 'comment-end)
2183   (make-local-variable 'indent-line-function)
2184   (setq indent-line-function 'indent-relative)
2185   (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
2186   (make-local-variable 'font-lock-defaults)
2187   (make-local-variable 'messages-mode-keywords)
2188   (let ((keywords
2189          (mdw-regexps "array" "bitmap" "callback" "docs[ \t]+enum"
2190                       "export" "enum" "fixed-octetstring" "flags"
2191                       "harmless" "map" "nested" "optional"
2192                       "optional-tagged" "package" "primitive"
2193                       "primitive-nullfree" "relaxed[ \t]+enum"
2194                       "set" "table" "tagged-optional"   "union"
2195                       "variadic" "vector" "version" "version-tag")))
2196     (setq messages-mode-keywords
2197           (list
2198            (list (concat "\\<\\(" keywords "\\)\\>:")
2199                  '(0 font-lock-keyword-face))
2200            '("\\([-a-zA-Z0-9]+:\\)" (0 font-lock-warning-face))
2201            '("\\(\\<[a-z][-_a-zA-Z0-9]*\\)"
2202              (0 font-lock-variable-name-face))
2203            '("\\<\\([0-9]+\\)\\>" (0 mdw-number-face))
2204            '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2205              (0 mdw-punct-face)))))
2206   (setq font-lock-defaults
2207         '(messages-mode-keywords nil nil nil nil))
2208   (run-hooks 'messages-file-hook))
2209
2210 (defun messages-mode ()
2211   (interactive)
2212   (fundamental-mode)
2213   (setq major-mode 'messages-mode)
2214   (setq mode-name "Messages")
2215   (messages-mode-guts)
2216   (modify-syntax-entry ?# "<" messages-mode-syntax-table)
2217   (modify-syntax-entry ?\n ">" messages-mode-syntax-table)
2218   (setq comment-start "# ")
2219   (setq comment-end "")
2220   (run-hooks 'messages-mode-hook))
2221
2222 (defun cpp-messages-mode ()
2223   (interactive)
2224   (fundamental-mode)
2225   (setq major-mode 'cpp-messages-mode)
2226   (setq mode-name "CPP Messages")
2227   (messages-mode-guts)
2228   (modify-syntax-entry ?* ". 23" messages-mode-syntax-table)
2229   (modify-syntax-entry ?/ ". 14" messages-mode-syntax-table)
2230   (setq comment-start "/* ")
2231   (setq comment-end " */")
2232   (let ((preprocessor-keywords
2233          (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
2234                       "ident" "if" "ifdef" "ifndef" "import" "include"
2235                       "line" "pragma" "unassert" "undef" "warning")))
2236     (setq messages-mode-keywords
2237           (append (list (list (concat "^[ \t]*\\#[ \t]*"
2238                                       "\\(include\\|import\\)"
2239                                       "[ \t]*\\(<[^>]+\\(>\\|\\)\\)")
2240                               '(2 font-lock-string-face))
2241                         (list (concat "^\\([ \t]*#[ \t]*\\(\\("
2242                                       preprocessor-keywords
2243                                       "\\)\\>\\|[0-9]+\\|$\\)\\)")
2244                               '(1 font-lock-keyword-face)))
2245                   messages-mode-keywords)))
2246   (run-hooks 'cpp-messages-mode-hook))
2247
2248 (add-hook 'messages-mode-hook 'mdw-misc-mode-config t)
2249 (add-hook 'cpp-messages-mode-hook 'mdw-misc-mode-config t)
2250 ; (add-hook 'messages-file-hook 'mdw-fontify-messages t)
2251
2252 ;;;--------------------------------------------------------------------------
2253 ;;; Messages-file mode.
2254
2255 (defvar mallow-driver-substitution-face 'mallow-driver-substitution-face
2256   "Face to use for subsittution directives.")
2257 (make-face 'mallow-driver-substitution-face)
2258 (defvar mallow-driver-text-face 'mallow-driver-text-face
2259   "Face to use for body text.")
2260 (make-face 'mallow-driver-text-face)
2261
2262 (defun mallow-driver-mode ()
2263   (interactive)
2264   (fundamental-mode)
2265   (setq major-mode 'mallow-driver-mode)
2266   (setq mode-name "Mallow driver")
2267   (setq mallow-driver-mode-syntax-table (make-syntax-table))
2268   (set-syntax-table mallow-driver-mode-syntax-table)
2269   (make-local-variable 'comment-start)
2270   (make-local-variable 'comment-end)
2271   (make-local-variable 'indent-line-function)
2272   (setq indent-line-function 'indent-relative)
2273   (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
2274   (make-local-variable 'font-lock-defaults)
2275   (make-local-variable 'mallow-driver-mode-keywords)
2276   (let ((keywords
2277          (mdw-regexps "each" "divert" "file" "if"
2278                       "perl" "set" "string" "type" "write")))
2279     (setq mallow-driver-mode-keywords
2280           (list
2281            (list (concat "^%\\s *\\(}\\|\\(" keywords "\\)\\>\\).*$")
2282                  '(0 font-lock-keyword-face))
2283            (list "^%\\s *\\(#.*\\|\\)$"
2284                  '(0 font-lock-comment-face))
2285            (list "^%"
2286                  '(0 font-lock-keyword-face))
2287            (list "^|?\\(.+\\)$" '(1 mallow-driver-text-face))
2288            (list "\\${[^}]*}"
2289                  '(0 mallow-driver-substitution-face t)))))
2290   (setq font-lock-defaults
2291         '(mallow-driver-mode-keywords nil nil nil nil))
2292   (modify-syntax-entry ?\" "_" mallow-driver-mode-syntax-table)
2293   (modify-syntax-entry ?\n ">" mallow-driver-mode-syntax-table)
2294   (setq comment-start "%# ")
2295   (setq comment-end "")
2296   (run-hooks 'mallow-driver-mode-hook))
2297
2298 (add-hook 'mallow-driver-hook 'mdw-misc-mode-config t)
2299
2300 ;;;--------------------------------------------------------------------------
2301 ;;; NFast debugs.
2302
2303 (defun nfast-debug-mode ()
2304   (interactive)
2305   (fundamental-mode)
2306   (setq major-mode 'nfast-debug-mode)
2307   (setq mode-name "NFast debug")
2308   (setq messages-mode-syntax-table (make-syntax-table))
2309   (set-syntax-table messages-mode-syntax-table)
2310   (make-local-variable 'font-lock-defaults)
2311   (make-local-variable 'nfast-debug-mode-keywords)
2312   (setq truncate-lines t)
2313   (setq nfast-debug-mode-keywords
2314         (list
2315          '("^\\(NFast_\\(Connect\\|Disconnect\\|Submit\\|Wait\\)\\)"
2316            (0 font-lock-keyword-face))
2317          (list (concat "^[ \t]+\\(\\("
2318                        "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
2319                        "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
2320                        "[ \t]+\\)*"
2321                        "[0-9a-fA-F]+\\)[ \t]*$")
2322            '(0 mdw-number-face))
2323          '("^[ \t]+\.status=[ \t]+\\<\\(OK\\)\\>"
2324            (1 font-lock-keyword-face))
2325          '("^[ \t]+\.status=[ \t]+\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>"
2326            (1 font-lock-warning-face))
2327          '("^[ \t]+\.status[ \t]+\\<\\(zero\\)\\>"
2328            (1 nil))
2329          (list (concat "^[ \t]+\\.cmd=[ \t]+"
2330                        "\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>")
2331            '(1 font-lock-keyword-face))
2332          '("-?\\<\\([0-9]+\\|0x[0-9a-fA-F]+\\)\\>" (0 mdw-number-face))
2333          '("^\\([ \t]+[a-z0-9.]+\\)" (0 font-lock-variable-name-face))
2334          '("\\<\\([a-z][a-z0-9.]+\\)\\>=" (1 font-lock-variable-name-face))
2335          '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" (0 mdw-punct-face))))
2336   (setq font-lock-defaults
2337         '(nfast-debug-mode-keywords nil nil nil nil))
2338   (run-hooks 'nfast-debug-mode-hook))
2339
2340 ;;;--------------------------------------------------------------------------
2341 ;;; Other languages.
2342
2343 ;; Smalltalk.
2344
2345 (defun mdw-setup-smalltalk ()
2346   (and mdw-auto-indent
2347        (local-set-key "\C-m" 'smalltalk-newline-and-indent))
2348   (make-local-variable 'mdw-auto-indent)
2349   (setq mdw-auto-indent nil)
2350   (local-set-key "\C-i" 'smalltalk-reindent))
2351
2352 (defun mdw-fontify-smalltalk ()
2353   (make-local-variable 'font-lock-keywords)
2354   (setq font-lock-keywords
2355         (list
2356          (list "\\<[A-Z][a-zA-Z0-9]*\\>"
2357                '(0 font-lock-keyword-face))
2358          (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
2359                        "[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)"
2360                        "\\([eE]\\([-+]\\|\\)[0-9_]+\\|\\)")
2361                '(0 mdw-number-face))
2362          (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2363                '(0 mdw-punct-face))))
2364   (mdw-post-config-mode-hack))
2365
2366 ;; Lispy languages.
2367
2368 ;; Unpleasant bodge.
2369 (unless (boundp 'slime-repl-mode-map)
2370   (setq slime-repl-mode-map (make-sparse-keymap)))
2371
2372 (defun mdw-indent-newline-and-indent ()
2373   (interactive)
2374   (indent-for-tab-command)
2375   (newline-and-indent))
2376
2377 (eval-after-load "cl-indent"
2378   '(progn
2379      (mapc #'(lambda (pair)
2380                (put (car pair)
2381                     'common-lisp-indent-function
2382                     (cdr pair)))
2383       '((destructuring-bind . ((&whole 4 &rest 1) 4 &body))
2384         (multiple-value-bind . ((&whole 4 &rest 1) 4 &body))))))
2385
2386 (defun mdw-common-lisp-indent ()
2387   (make-local-variable 'lisp-indent-function)
2388   (setq lisp-indent-function 'common-lisp-indent-function))
2389
2390 (setq lisp-simple-loop-indentation 2
2391       lisp-loop-keyword-indentation 6
2392       lisp-loop-forms-indentation 6)
2393
2394 (defun mdw-fontify-lispy ()
2395
2396   ;; Set fill prefix.
2397   (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)")
2398
2399   ;; Not much fontification needed.
2400   (make-local-variable 'font-lock-keywords)
2401   (setq font-lock-keywords
2402         (list
2403          (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2404                '(0 mdw-punct-face))))
2405
2406   (mdw-post-config-mode-hack))
2407
2408 (defun comint-send-and-indent ()
2409   (interactive)
2410   (comint-send-input)
2411   (and mdw-auto-indent
2412        (indent-for-tab-command)))
2413
2414 (defun mdw-setup-m4 ()
2415   (mdw-standard-fill-prefix "\\([ \t]*\\(?:#+\\|\\<dnl\\>\\)[ \t]*\\)"))
2416
2417 ;;;--------------------------------------------------------------------------
2418 ;;; Text mode.
2419
2420 (defun mdw-text-mode ()
2421   (setq fill-column 72)
2422   (flyspell-mode t)
2423   (mdw-standard-fill-prefix
2424    "\\([ \t]*\\([>#|:] ?\\)*[ \t]*\\)" 3)
2425   (auto-fill-mode 1))
2426
2427 ;;;--------------------------------------------------------------------------
2428 ;;; Outline and hide/show modes.
2429
2430 (defun mdw-outline-collapse-all ()
2431   "Completely collapse everything in the entire buffer."
2432   (interactive)
2433   (save-excursion
2434     (goto-char (point-min))
2435     (while (< (point) (point-max))
2436       (hide-subtree)
2437       (forward-line))))
2438
2439 (setq hs-hide-comments-when-hiding-all nil)
2440
2441 (defadvice hs-hide-all (after hide-first-comment activate)
2442   (save-excursion (hs-hide-initial-comment-block)))
2443
2444 ;;;--------------------------------------------------------------------------
2445 ;;; Shell mode.
2446
2447 (defun mdw-sh-mode-setup ()
2448   (local-set-key [?\C-a] 'comint-bol)
2449   (add-hook 'comint-output-filter-functions
2450             'comint-watch-for-password-prompt))
2451
2452 (defun mdw-term-mode-setup ()
2453   (setq term-prompt-regexp shell-prompt-pattern)
2454   (make-local-variable 'mouse-yank-at-point)
2455   (make-local-variable 'transient-mark-mode)
2456   (setq mouse-yank-at-point t)
2457   (auto-fill-mode -1)
2458   (setq tab-width 8))
2459
2460 (defun term-send-meta-right () (interactive) (term-send-raw-string "\e\e[C"))
2461 (defun term-send-meta-left  () (interactive) (term-send-raw-string "\e\e[D"))
2462 (defun term-send-ctrl-uscore () (interactive) (term-send-raw-string "\C-_"))
2463 (defun term-send-meta-meta-something ()
2464   (interactive)
2465   (term-send-raw-string "\e\e")
2466   (term-send-raw))
2467 (eval-after-load 'term
2468   '(progn
2469      (define-key term-raw-map [?\e ?\e] nil)
2470      (define-key term-raw-map [?\e ?\e t] 'term-send-meta-meta-something)
2471      (define-key term-raw-map [?\C-/] 'term-send-ctrl-uscore)
2472      (define-key term-raw-map [M-right] 'term-send-meta-right)
2473      (define-key term-raw-map [?\e ?\M-O ?C] 'term-send-meta-right)
2474      (define-key term-raw-map [M-left] 'term-send-meta-left)
2475      (define-key term-raw-map [?\e ?\M-O ?D] 'term-send-meta-left)))
2476
2477 (defadvice term-exec (before program-args-list compile activate)
2478   "If the PROGRAM argument is a list, interpret it as (PROGRAM . SWITCHES).
2479 This allows you to pass a list of arguments through `ansi-term'."
2480   (let ((program (ad-get-arg 2)))
2481     (if (listp program)
2482         (progn
2483           (ad-set-arg 2 (car program))
2484           (ad-set-arg 4 (cdr program))))))
2485
2486 (defun ssh (host)
2487   "Open a terminal containing an ssh session to the HOST."
2488   (interactive "sHost: ")
2489   (ansi-term (list "ssh" host) (format "ssh@%s" host)))
2490
2491 ;;;--------------------------------------------------------------------------
2492 ;;; Inferior Emacs Lisp.
2493
2494 (setq comint-prompt-read-only t)
2495
2496 (eval-after-load "comint"
2497   '(progn
2498      (define-key comint-mode-map "\C-w" 'comint-kill-region)
2499      (define-key comint-mode-map [C-S-backspace] 'comint-kill-whole-line)))
2500
2501 (eval-after-load "ielm"
2502   '(progn
2503      (define-key ielm-map "\C-w" 'comint-kill-region)
2504      (define-key ielm-map [C-S-backspace] 'comint-kill-whole-line)))
2505
2506 ;;;----- That's all, folks --------------------------------------------------
2507
2508 (provide 'dot-emacs)