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