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