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