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