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