chiark / gitweb /
Merge branch 'master' of http://git.distorted.org.uk/~mdw/profile
[profile] / el / dot-emacs.el
... / ...
CommitLineData
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.
29This 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.
109Return non-nil if NAME.el (or NAME.elc) somewhere on the Emacs
110load path. The non-nil value is the filename we found for the
111library."
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.
150This is the number of columns used by fringes, scroll bars and other such
151cruft."
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.
163Without a numeric argument, split the window approximately in
164half. With a numeric argument WIDTH, allocate WIDTH columns to
165the left-hand window (if positive) or -WIDTH columns to the
166right-hand window (if negative). Space for scroll bars and
167fringes 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.
198If `transient-mark-mode' is on and the mark is inactive, then
199just activate it. A non-trivial prefix argument will force the
200usual behaviour. A trivial prefix argument (i.e., just C-u) will
201activate the mark and temporarily enable `transient-mark-mode' if
202it'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.
216L is a list of day numbers (from 0 to 6 for Sunday through to
217Saturday) or symbols `sunday', `monday', etc. (or a mixture). If
218the date stored in `date' falls on a listed day, then the
219function 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'.
256The value is an alist mapping evil keys (as a list, or singleton)
257to good keys (in the same form).")
258
259(defun mdw-clobber-evil-keymap (keymap)
260 "Replace evil key bindings in the KEYMAP.
261Evil 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.
306Not 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,
360so 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.
395The first association with a given key prevails; others are
396ignored. The input lists are not modified, although they'll
397probably 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.
407The DONE argument is a list whose first element is `nil'. It
408contains the uniquified alist built so far. The leading `nil' is
409stripped off at the end of the operation; it's only there so that
410DONE always references a cons cell. END refers to the final cons
411cell in the DONE list; it is modified in place each time to avoid
412the overheads of `append'ing all the time. The L argument is the
413alist we're currently processing; the remaining alists are given
414in 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.
476VER-A and VER-B are version numbers, which are strings containing digit
477sequences 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.
491This takes into consideration whether it's been found using
492tramp, 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.
514Interactively, if files are marked, then insert all of them.
515With a numeric prefix argument, select that many entries near
516point; with a non-numeric prefix argument, prompt for listing
517options."
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.
538If 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.
558Each item is a browser function name, or a cons (CHECK . FUNC).
559A symbol FOO stands for (FOO . FOO).")
560
561(defun mdw-good-url-browser ()
562 "Return a good URL browser.
563Trundle the list of such things, finding the first item for which
564CHECK 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'.
619If there's no fill prefix currently set (by the `fill-prefix'
620variable) and there's a match from one of the regexps here, it
621gets used to set the fill-prefix for the current operation.
622
623The variable is a list of items of the form `REGEXP . PREFIX'; if
624the REGEXP matches, the PREFIX is used to set the fill prefix.
625It 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.
640This 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
666context and return the static fill prefix to use. Point must be
667at 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.
690See `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
709case 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.
721This is just a short-cut for setting the thing by hand, and by
722design it doesn't cope with anything approximating a complicated
723case."
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
788these then the file is not backed up.")
789
790(defun mdw-backup-enable-predicate (name)
791 "[mdw]'s default backup predicate.
792Allows a backup if the standard predicate would allow it, and it
793doesn'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 magit-diff-add
976 (t :foreground "green"))
977(mdw-define-face magit-diff-del
978 (t :foreground "red"))
979(mdw-define-face magit-diff-file-header
980 (t :weight bold))
981(mdw-define-face magit-diff-hunk-header
982 (t :foreground "SkyBlue1"))
983
984(mdw-define-face erc-input-face
985 (t :foreground "red"))
986
987(mdw-define-face woman-bold
988 (t :weight bold))
989(mdw-define-face woman-italic
990 (t :slant italic))
991
992(mdw-define-face rst-level-1-face
993 (t :foreground "SkyBlue1" :weight bold))
994(mdw-define-face rst-level-2-face
995 (t :foreground "SeaGreen1" :weight bold))
996(mdw-define-face rst-level-3-face
997 (t :weight bold))
998(mdw-define-face rst-level-4-face
999 (t :slant italic))
1000(mdw-define-face rst-level-5-face
1001 (t :underline t))
1002(mdw-define-face rst-level-6-face
1003 ())
1004
1005(mdw-define-face p4-depot-added-face
1006 (t :foreground "green"))
1007(mdw-define-face p4-depot-branch-op-face
1008 (t :foreground "yellow"))
1009(mdw-define-face p4-depot-deleted-face
1010 (t :foreground "red"))
1011(mdw-define-face p4-depot-unmapped-face
1012 (t :foreground "SkyBlue1"))
1013(mdw-define-face p4-diff-change-face
1014 (t :foreground "yellow"))
1015(mdw-define-face p4-diff-del-face
1016 (t :foreground "red"))
1017(mdw-define-face p4-diff-file-face
1018 (t :foreground "SkyBlue1"))
1019(mdw-define-face p4-diff-head-face
1020 (t :background "grey10"))
1021(mdw-define-face p4-diff-ins-face
1022 (t :foreground "green"))
1023
1024(mdw-define-face whizzy-slice-face
1025 (t :background "grey10"))
1026(mdw-define-face whizzy-error-face
1027 (t :background "darkred"))
1028
1029;; Ellipses used to indicate hidden text (and similar).
1030(mdw-define-face mdw-ellipsis-face
1031 (((type tty)) :foreground "blue") (t :foreground "grey60"))
1032(let ((dollar (make-glyph-code ?$ 'mdw-ellipsis-face))
1033 (backslash (make-glyph-code ?\ 'mdw-ellipsis-face))
1034 (dot (make-glyph-code ?. 'mdw-ellipsis-face))
1035 (bar (make-glyph-code ?| mdw-ellipsis-face)))
1036 (set-display-table-slot standard-display-table 0 dollar)
1037 (set-display-table-slot standard-display-table 1 backslash)
1038 (set-display-table-slot standard-display-table 4
1039 (vector dot dot dot))
1040 (set-display-table-slot standard-display-table 5 bar))
1041
1042;;;--------------------------------------------------------------------------
1043;;; C programming configuration.
1044
1045;; Linux kernel hacking.
1046
1047(defvar linux-c-mode-hook)
1048
1049(defun linux-c-mode ()
1050 (interactive)
1051 (c-mode)
1052 (setq major-mode 'linux-c-mode)
1053 (setq mode-name "Linux C")
1054 (run-hooks 'linux-c-mode-hook))
1055
1056;; Make C indentation nice.
1057
1058(defun mdw-c-lineup-arglist (langelem)
1059 "Hack for DWIMmery in c-lineup-arglist."
1060 (if (save-excursion
1061 (c-block-in-arglist-dwim (c-langelem-2nd-pos c-syntactic-element)))
1062 0
1063 (c-lineup-arglist langelem)))
1064
1065(defun mdw-c-indent-extern-mumble (langelem)
1066 "Indent `extern \"...\" {' lines."
1067 (save-excursion
1068 (back-to-indentation)
1069 (if (looking-at
1070 "\\s-*\\<extern\\>\\s-*\"\\([^\\\\\"]+\\|\\.\\)*\"\\s-*{")
1071 c-basic-offset
1072 nil)))
1073
1074(defun mdw-c-style ()
1075 (c-add-style "[mdw] C and C++ style"
1076 '((c-basic-offset . 2)
1077 (comment-column . 40)
1078 (c-class-key . "class")
1079 (c-backslash-column . 72)
1080 (c-offsets-alist
1081 (substatement-open . (add 0 c-indent-one-line-block))
1082 (defun-open . (add 0 c-indent-one-line-block))
1083 (arglist-cont-nonempty . mdw-c-lineup-arglist)
1084 (topmost-intro . mdw-c-indent-extern-mumble)
1085 (cpp-define-intro . 0)
1086 (knr-argdecl . 0)
1087 (inextern-lang . [0])
1088 (label . 0)
1089 (case-label . +)
1090 (access-label . -)
1091 (inclass . +)
1092 (inline-open . ++)
1093 (statement-cont . +)
1094 (statement-case-intro . +)))
1095 t))
1096
1097(defvar mdw-c-comment-fill-prefix
1098 `((,(concat "\\([ \t]*/?\\)"
1099 "\\(\*\\|//]\\)"
1100 "\\([ \t]*\\)"
1101 "\\([A-Za-z]+:[ \t]*\\)?"
1102 mdw-hanging-indents)
1103 (pad . 1) (match . 2) (pad . 3) (pad . 4) (pad . 5)))
1104 "Fill prefix matching C comments (both kinds).")
1105
1106(defun mdw-fontify-c-and-c++ ()
1107
1108 ;; Fiddle with some syntax codes.
1109 (modify-syntax-entry ?* ". 23")
1110 (modify-syntax-entry ?/ ". 124b")
1111 (modify-syntax-entry ?\n "> b")
1112
1113 ;; Other stuff.
1114 (mdw-c-style)
1115 (setq c-hanging-comment-ender-p nil)
1116 (setq c-backslash-column 72)
1117 (setq c-label-minimum-indentation 0)
1118 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
1119
1120 ;; Now define things to be fontified.
1121 (make-local-variable 'font-lock-keywords)
1122 (let ((c-keywords
1123 (mdw-regexps "and" ;C++
1124 "and_eq" ;C++
1125 "asm" ;K&R, GCC
1126 "auto" ;K&R, C89
1127 "bitand" ;C++
1128 "bitor" ;C++
1129 "bool" ;C++, C9X macro
1130 "break" ;K&R, C89
1131 "case" ;K&R, C89
1132 "catch" ;C++
1133 "char" ;K&R, C89
1134 "class" ;C++
1135 "complex" ;C9X macro, C++ template type
1136 "compl" ;C++
1137 "const" ;C89
1138 "const_cast" ;C++
1139 "continue" ;K&R, C89
1140 "defined" ;C89 preprocessor
1141 "default" ;K&R, C89
1142 "delete" ;C++
1143 "do" ;K&R, C89
1144 "double" ;K&R, C89
1145 "dynamic_cast" ;C++
1146 "else" ;K&R, C89
1147 ;; "entry" ;K&R -- never used
1148 "enum" ;C89
1149 "explicit" ;C++
1150 "export" ;C++
1151 "extern" ;K&R, C89
1152 "false" ;C++, C9X macro
1153 "float" ;K&R, C89
1154 "for" ;K&R, C89
1155 ;; "fortran" ;K&R
1156 "friend" ;C++
1157 "goto" ;K&R, C89
1158 "if" ;K&R, C89
1159 "imaginary" ;C9X macro
1160 "inline" ;C++, C9X, GCC
1161 "int" ;K&R, C89
1162 "long" ;K&R, C89
1163 "mutable" ;C++
1164 "namespace" ;C++
1165 "new" ;C++
1166 "operator" ;C++
1167 "or" ;C++
1168 "or_eq" ;C++
1169 "private" ;C++
1170 "protected" ;C++
1171 "public" ;C++
1172 "register" ;K&R, C89
1173 "reinterpret_cast" ;C++
1174 "restrict" ;C9X
1175 "return" ;K&R, C89
1176 "short" ;K&R, C89
1177 "signed" ;C89
1178 "sizeof" ;K&R, C89
1179 "static" ;K&R, C89
1180 "static_cast" ;C++
1181 "struct" ;K&R, C89
1182 "switch" ;K&R, C89
1183 "template" ;C++
1184 "this" ;C++
1185 "throw" ;C++
1186 "true" ;C++, C9X macro
1187 "try" ;C++
1188 "this" ;C++
1189 "typedef" ;C89
1190 "typeid" ;C++
1191 "typeof" ;GCC
1192 "typename" ;C++
1193 "union" ;K&R, C89
1194 "unsigned" ;K&R, C89
1195 "using" ;C++
1196 "virtual" ;C++
1197 "void" ;C89
1198 "volatile" ;C89
1199 "wchar_t" ;C++, C89 library type
1200 "while" ;K&R, C89
1201 "xor" ;C++
1202 "xor_eq" ;C++
1203 "_Bool" ;C9X
1204 "_Complex" ;C9X
1205 "_Imaginary" ;C9X
1206 "_Pragma" ;C9X preprocessor
1207 "__alignof__" ;GCC
1208 "__asm__" ;GCC
1209 "__attribute__" ;GCC
1210 "__complex__" ;GCC
1211 "__const__" ;GCC
1212 "__extension__" ;GCC
1213 "__imag__" ;GCC
1214 "__inline__" ;GCC
1215 "__label__" ;GCC
1216 "__real__" ;GCC
1217 "__signed__" ;GCC
1218 "__typeof__" ;GCC
1219 "__volatile__" ;GCC
1220 ))
1221 (preprocessor-keywords
1222 (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
1223 "ident" "if" "ifdef" "ifndef" "import" "include"
1224 "line" "pragma" "unassert" "undef" "warning"))
1225 (objc-keywords
1226 (mdw-regexps "class" "defs" "encode" "end" "implementation"
1227 "interface" "private" "protected" "protocol" "public"
1228 "selector")))
1229
1230 (setq font-lock-keywords
1231 (list
1232
1233 ;; Fontify include files as strings.
1234 (list (concat "^[ \t]*\\#[ \t]*"
1235 "\\(include\\|import\\)"
1236 "[ \t]*\\(<[^>]+\\(>\\|\\)\\)")
1237 '(2 font-lock-string-face))
1238
1239 ;; Preprocessor directives are `references'?.
1240 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
1241 preprocessor-keywords
1242 "\\)\\>\\|[0-9]+\\|$\\)\\)")
1243 '(1 font-lock-keyword-face))
1244
1245 ;; Handle the keywords defined above.
1246 (list (concat "@\\<\\(" objc-keywords "\\)\\>")
1247 '(0 font-lock-keyword-face))
1248
1249 (list (concat "\\<\\(" c-keywords "\\)\\>")
1250 '(0 font-lock-keyword-face))
1251
1252 ;; Handle numbers too.
1253 ;;
1254 ;; This looks strange, I know. It corresponds to the
1255 ;; preprocessor's idea of what a number looks like, rather than
1256 ;; anything sensible.
1257 (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
1258 "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
1259 '(0 mdw-number-face))
1260
1261 ;; And anything else is punctuation.
1262 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1263 '(0 mdw-punct-face))))
1264
1265 (mdw-post-config-mode-hack)))
1266
1267;;;--------------------------------------------------------------------------
1268;;; AP calc mode.
1269
1270(defun apcalc-mode ()
1271 (interactive)
1272 (c-mode)
1273 (setq major-mode 'apcalc-mode)
1274 (setq mode-name "AP Calc")
1275 (run-hooks 'apcalc-mode-hook))
1276
1277(defun mdw-fontify-apcalc ()
1278
1279 ;; Fiddle with some syntax codes.
1280 (modify-syntax-entry ?* ". 23")
1281 (modify-syntax-entry ?/ ". 14")
1282
1283 ;; Other stuff.
1284 (mdw-c-style)
1285 (setq c-hanging-comment-ender-p nil)
1286 (setq c-backslash-column 72)
1287 (setq comment-start "/* ")
1288 (setq comment-end " */")
1289 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
1290
1291 ;; Now define things to be fontified.
1292 (make-local-variable 'font-lock-keywords)
1293 (let ((c-keywords
1294 (mdw-regexps "break" "case" "cd" "continue" "define" "default"
1295 "do" "else" "exit" "for" "global" "goto" "help" "if"
1296 "local" "mat" "obj" "print" "quit" "read" "return"
1297 "show" "static" "switch" "while" "write")))
1298
1299 (setq font-lock-keywords
1300 (list
1301
1302 ;; Handle the keywords defined above.
1303 (list (concat "\\<\\(" c-keywords "\\)\\>")
1304 '(0 font-lock-keyword-face))
1305
1306 ;; Handle numbers too.
1307 ;;
1308 ;; This looks strange, I know. It corresponds to the
1309 ;; preprocessor's idea of what a number looks like, rather than
1310 ;; anything sensible.
1311 (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
1312 "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
1313 '(0 mdw-number-face))
1314
1315 ;; And anything else is punctuation.
1316 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1317 '(0 mdw-punct-face)))))
1318
1319 (mdw-post-config-mode-hack))
1320
1321;;;--------------------------------------------------------------------------
1322;;; Java programming configuration.
1323
1324;; Make indentation nice.
1325
1326(defun mdw-java-style ()
1327 (c-add-style "[mdw] Java style"
1328 '((c-basic-offset . 2)
1329 (c-offsets-alist (substatement-open . 0)
1330 (label . +)
1331 (case-label . +)
1332 (access-label . 0)
1333 (inclass . +)
1334 (statement-case-intro . +)))
1335 t))
1336
1337;; Declare Java fontification style.
1338
1339(defun mdw-fontify-java ()
1340
1341 ;; Other stuff.
1342 (mdw-java-style)
1343 (setq c-hanging-comment-ender-p nil)
1344 (setq c-backslash-column 72)
1345 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
1346
1347 ;; Now define things to be fontified.
1348 (make-local-variable 'font-lock-keywords)
1349 (let ((java-keywords
1350 (mdw-regexps "abstract" "boolean" "break" "byte" "case" "catch"
1351 "char" "class" "const" "continue" "default" "do"
1352 "double" "else" "extends" "final" "finally" "float"
1353 "for" "goto" "if" "implements" "import" "instanceof"
1354 "int" "interface" "long" "native" "new" "package"
1355 "private" "protected" "public" "return" "short"
1356 "static" "super" "switch" "synchronized" "this"
1357 "throw" "throws" "transient" "try" "void" "volatile"
1358 "while"
1359
1360 "false" "null" "true")))
1361
1362 (setq font-lock-keywords
1363 (list
1364
1365 ;; Handle the keywords defined above.
1366 (list (concat "\\<\\(" java-keywords "\\)\\>")
1367 '(0 font-lock-keyword-face))
1368
1369 ;; Handle numbers too.
1370 ;;
1371 ;; The following isn't quite right, but it's close enough.
1372 (list (concat "\\<\\("
1373 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
1374 "[0-9]+\\(\\.[0-9]*\\|\\)"
1375 "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
1376 "[lLfFdD]?")
1377 '(0 mdw-number-face))
1378
1379 ;; And anything else is punctuation.
1380 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1381 '(0 mdw-punct-face)))))
1382
1383 (mdw-post-config-mode-hack))
1384
1385;;;--------------------------------------------------------------------------
1386;;; Javascript programming configuration.
1387
1388(defun mdw-javascript-style ()
1389 (setq js-indent-level 2)
1390 (setq js-expr-indent-offset 0))
1391
1392(defun mdw-fontify-javascript ()
1393
1394 ;; Other stuff.
1395 (mdw-javascript-style)
1396 (setq js-auto-indent-flag t)
1397
1398 ;; Now define things to be fontified.
1399 (make-local-variable 'font-lock-keywords)
1400 (let ((javascript-keywords
1401 (mdw-regexps "abstract" "boolean" "break" "byte" "case" "catch"
1402 "char" "class" "const" "continue" "debugger" "default"
1403 "delete" "do" "double" "else" "enum" "export" "extends"
1404 "final" "finally" "float" "for" "function" "goto" "if"
1405 "implements" "import" "in" "instanceof" "int"
1406 "interface" "let" "long" "native" "new" "package"
1407 "private" "protected" "public" "return" "short"
1408 "static" "super" "switch" "synchronized" "throw"
1409 "throws" "transient" "try" "typeof" "var" "void"
1410 "volatile" "while" "with" "yield"
1411
1412 "boolean" "byte" "char" "double" "float" "int" "long"
1413 "short" "void"))
1414 (javascript-constants
1415 (mdw-regexps "false" "null" "undefined" "Infinity" "NaN" "true"
1416 "arguments" "this")))
1417
1418 (setq font-lock-keywords
1419 (list
1420
1421 ;; Handle the keywords defined above.
1422 (list (concat "\\<\\(" javascript-keywords "\\)\\>")
1423 '(0 font-lock-keyword-face))
1424
1425 ;; Handle the predefined constants defined above.
1426 (list (concat "\\<\\(" javascript-constants "\\)\\>")
1427 '(0 font-lock-variable-name-face))
1428
1429 ;; Handle numbers too.
1430 ;;
1431 ;; The following isn't quite right, but it's close enough.
1432 (list (concat "\\<\\("
1433 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
1434 "[0-9]+\\(\\.[0-9]*\\|\\)"
1435 "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
1436 "[lLfFdD]?")
1437 '(0 mdw-number-face))
1438
1439 ;; And anything else is punctuation.
1440 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1441 '(0 mdw-punct-face)))))
1442
1443 (mdw-post-config-mode-hack))
1444
1445;;;--------------------------------------------------------------------------
1446;;; C# programming configuration.
1447
1448;; Make indentation nice.
1449
1450(defun mdw-csharp-style ()
1451 (c-add-style "[mdw] C# style"
1452 '((c-basic-offset . 2)
1453 (c-offsets-alist (substatement-open . 0)
1454 (label . 0)
1455 (case-label . +)
1456 (access-label . 0)
1457 (inclass . +)
1458 (statement-case-intro . +)))
1459 t))
1460
1461;; Declare C# fontification style.
1462
1463(defun mdw-fontify-csharp ()
1464
1465 ;; Other stuff.
1466 (mdw-csharp-style)
1467 (setq c-hanging-comment-ender-p nil)
1468 (setq c-backslash-column 72)
1469 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
1470
1471 ;; Now define things to be fontified.
1472 (make-local-variable 'font-lock-keywords)
1473 (let ((csharp-keywords
1474 (mdw-regexps "abstract" "as" "base" "bool" "break"
1475 "byte" "case" "catch" "char" "checked"
1476 "class" "const" "continue" "decimal" "default"
1477 "delegate" "do" "double" "else" "enum"
1478 "event" "explicit" "extern" "false" "finally"
1479 "fixed" "float" "for" "foreach" "goto"
1480 "if" "implicit" "in" "int" "interface"
1481 "internal" "is" "lock" "long" "namespace"
1482 "new" "null" "object" "operator" "out"
1483 "override" "params" "private" "protected" "public"
1484 "readonly" "ref" "return" "sbyte" "sealed"
1485 "short" "sizeof" "stackalloc" "static" "string"
1486 "struct" "switch" "this" "throw" "true"
1487 "try" "typeof" "uint" "ulong" "unchecked"
1488 "unsafe" "ushort" "using" "virtual" "void"
1489 "volatile" "while" "yield")))
1490
1491 (setq font-lock-keywords
1492 (list
1493
1494 ;; Handle the keywords defined above.
1495 (list (concat "\\<\\(" csharp-keywords "\\)\\>")
1496 '(0 font-lock-keyword-face))
1497
1498 ;; Handle numbers too.
1499 ;;
1500 ;; The following isn't quite right, but it's close enough.
1501 (list (concat "\\<\\("
1502 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
1503 "[0-9]+\\(\\.[0-9]*\\|\\)"
1504 "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
1505 "[lLfFdD]?")
1506 '(0 mdw-number-face))
1507
1508 ;; And anything else is punctuation.
1509 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1510 '(0 mdw-punct-face)))))
1511
1512 (mdw-post-config-mode-hack))
1513
1514(define-derived-mode csharp-mode java-mode "C#"
1515 "Major mode for editing C# code.")
1516
1517;;;--------------------------------------------------------------------------
1518;;; Go programming configuration.
1519
1520(defun mdw-fontify-go ()
1521
1522 (make-local-variable 'font-lock-keywords)
1523 (let ((go-keywords
1524 (mdw-regexps "break" "case" "chan" "const" "continue"
1525 "default" "defer" "else" "fallthrough" "for"
1526 "func" "go" "goto" "if" "import"
1527 "interface" "map" "package" "range" "return"
1528 "select" "struct" "switch" "type" "var")))
1529
1530 (setq font-lock-keywords
1531 (list
1532
1533 ;; Handle the keywords defined above.
1534 (list (concat "\\<\\(" go-keywords "\\)\\>")
1535 '(0 font-lock-keyword-face))
1536
1537 ;; Handle numbers too.
1538 ;;
1539 ;; The following isn't quite right, but it's close enough.
1540 (list (concat "\\<\\("
1541 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
1542 "[0-9]+\\(\\.[0-9]*\\|\\)"
1543 "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)")
1544 '(0 mdw-number-face))
1545
1546 ;; And anything else is punctuation.
1547 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1548 '(0 mdw-punct-face)))))
1549
1550 (mdw-post-config-mode-hack))
1551
1552;;;--------------------------------------------------------------------------
1553;;; Awk programming configuration.
1554
1555;; Make Awk indentation nice.
1556
1557(defun mdw-awk-style ()
1558 (c-add-style "[mdw] Awk style"
1559 '((c-basic-offset . 2)
1560 (c-offsets-alist (substatement-open . 0)
1561 (statement-cont . 0)
1562 (statement-case-intro . +)))
1563 t))
1564
1565;; Declare Awk fontification style.
1566
1567(defun mdw-fontify-awk ()
1568
1569 ;; Miscellaneous fiddling.
1570 (mdw-awk-style)
1571 (setq c-backslash-column 72)
1572 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
1573
1574 ;; Now define things to be fontified.
1575 (make-local-variable 'font-lock-keywords)
1576 (let ((c-keywords
1577 (mdw-regexps "BEGIN" "END" "ARGC" "ARGIND" "ARGV" "CONVFMT"
1578 "ENVIRON" "ERRNO" "FIELDWIDTHS" "FILENAME" "FNR"
1579 "FS" "IGNORECASE" "NF" "NR" "OFMT" "OFS" "ORS" "RS"
1580 "RSTART" "RLENGTH" "RT" "SUBSEP"
1581 "atan2" "break" "close" "continue" "cos" "delete"
1582 "do" "else" "exit" "exp" "fflush" "file" "for" "func"
1583 "function" "gensub" "getline" "gsub" "if" "in"
1584 "index" "int" "length" "log" "match" "next" "rand"
1585 "return" "print" "printf" "sin" "split" "sprintf"
1586 "sqrt" "srand" "strftime" "sub" "substr" "system"
1587 "systime" "tolower" "toupper" "while")))
1588
1589 (setq font-lock-keywords
1590 (list
1591
1592 ;; Handle the keywords defined above.
1593 (list (concat "\\<\\(" c-keywords "\\)\\>")
1594 '(0 font-lock-keyword-face))
1595
1596 ;; Handle numbers too.
1597 ;;
1598 ;; The following isn't quite right, but it's close enough.
1599 (list (concat "\\<\\("
1600 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
1601 "[0-9]+\\(\\.[0-9]*\\|\\)"
1602 "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
1603 "[uUlL]*")
1604 '(0 mdw-number-face))
1605
1606 ;; And anything else is punctuation.
1607 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1608 '(0 mdw-punct-face)))))
1609
1610 (mdw-post-config-mode-hack))
1611
1612;;;--------------------------------------------------------------------------
1613;;; Perl programming style.
1614
1615;; Perl indentation style.
1616
1617(fset 'perl-mode 'cperl-mode)
1618(setq cperl-indent-level 2)
1619(setq cperl-continued-statement-offset 2)
1620(setq cperl-continued-brace-offset 0)
1621(setq cperl-brace-offset -2)
1622(setq cperl-brace-imaginary-offset 0)
1623(setq cperl-label-offset 0)
1624
1625;; Define perl fontification style.
1626
1627(defun mdw-fontify-perl ()
1628
1629 ;; Miscellaneous fiddling.
1630 (modify-syntax-entry ?$ "\\")
1631 (modify-syntax-entry ?$ "\\" font-lock-syntax-table)
1632 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
1633
1634 ;; Now define fontification things.
1635 (make-local-variable 'font-lock-keywords)
1636 (let ((perl-keywords
1637 (mdw-regexps "and" "break" "cmp" "continue" "do" "else" "elsif" "eq"
1638 "for" "foreach" "ge" "given" "gt" "goto" "if"
1639 "last" "le" "lt" "local" "my" "ne" "next" "or"
1640 "our" "package" "redo" "require" "return" "sub"
1641 "undef" "unless" "until" "use" "when" "while")))
1642
1643 (setq font-lock-keywords
1644 (list
1645
1646 ;; Set up the keywords defined above.
1647 (list (concat "\\<\\(" perl-keywords "\\)\\>")
1648 '(0 font-lock-keyword-face))
1649
1650 ;; At least numbers are simpler than C.
1651 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
1652 "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)"
1653 "\\([eE]\\([-+]\\|\\)[0-9_]+\\|\\)")
1654 '(0 mdw-number-face))
1655
1656 ;; And anything else is punctuation.
1657 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1658 '(0 mdw-punct-face)))))
1659
1660 (mdw-post-config-mode-hack))
1661
1662(defun perl-number-tests (&optional arg)
1663 "Assign consecutive numbers to lines containing `#t'. With ARG,
1664strip numbers instead."
1665 (interactive "P")
1666 (save-excursion
1667 (goto-char (point-min))
1668 (let ((i 0) (fmt (if arg "" " %4d")))
1669 (while (search-forward "#t" nil t)
1670 (delete-region (point) (line-end-position))
1671 (setq i (1+ i))
1672 (insert (format fmt i)))
1673 (goto-char (point-min))
1674 (if (re-search-forward "\\(tests\\s-*=>\\s-*\\)\\w*" nil t)
1675 (replace-match (format "\\1%d" i))))))
1676
1677;;;--------------------------------------------------------------------------
1678;;; Python programming style.
1679
1680(defun mdw-fontify-pythonic (keywords)
1681
1682 ;; Miscellaneous fiddling.
1683 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
1684 (setq indent-tabs-mode nil)
1685
1686 ;; Now define fontification things.
1687 (make-local-variable 'font-lock-keywords)
1688 (setq font-lock-keywords
1689 (list
1690
1691 ;; Set up the keywords defined above.
1692 (list (concat "\\_<\\(" keywords "\\)\\_>")
1693 '(0 font-lock-keyword-face))
1694
1695 ;; At least numbers are simpler than C.
1696 (list (concat "\\_<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
1697 "\\_<[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)"
1698 "\\([eE]\\([-+]\\|\\)[0-9_]+\\|[lL]\\|\\)")
1699 '(0 mdw-number-face))
1700
1701 ;; And anything else is punctuation.
1702 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1703 '(0 mdw-punct-face))))
1704
1705 (mdw-post-config-mode-hack))
1706
1707;; Define Python fontification styles.
1708
1709(defun mdw-fontify-python ()
1710 (mdw-fontify-pythonic
1711 (mdw-regexps "and" "as" "assert" "break" "class" "continue" "def"
1712 "del" "elif" "else" "except" "exec" "finally" "for"
1713 "from" "global" "if" "import" "in" "is" "lambda"
1714 "not" "or" "pass" "print" "raise" "return" "try"
1715 "while" "with" "yield")))
1716
1717(defun mdw-fontify-pyrex ()
1718 (mdw-fontify-pythonic
1719 (mdw-regexps "and" "as" "assert" "break" "cdef" "class" "continue"
1720 "ctypedef" "def" "del" "elif" "else" "except" "exec"
1721 "extern" "finally" "for" "from" "global" "if"
1722 "import" "in" "is" "lambda" "not" "or" "pass" "print"
1723 "raise" "return" "struct" "try" "while" "with"
1724 "yield")))
1725
1726;;;--------------------------------------------------------------------------
1727;;; Icon programming style.
1728
1729;; Icon indentation style.
1730
1731(setq icon-brace-offset 0
1732 icon-continued-brace-offset 0
1733 icon-continued-statement-offset 2
1734 icon-indent-level 2)
1735
1736;; Define Icon fontification style.
1737
1738(defun mdw-fontify-icon ()
1739
1740 ;; Miscellaneous fiddling.
1741 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
1742
1743 ;; Now define fontification things.
1744 (make-local-variable 'font-lock-keywords)
1745 (let ((icon-keywords
1746 (mdw-regexps "break" "by" "case" "create" "default" "do" "else"
1747 "end" "every" "fail" "global" "if" "initial"
1748 "invocable" "link" "local" "next" "not" "of"
1749 "procedure" "record" "repeat" "return" "static"
1750 "suspend" "then" "to" "until" "while"))
1751 (preprocessor-keywords
1752 (mdw-regexps "define" "else" "endif" "error" "ifdef" "ifndef"
1753 "include" "line" "undef")))
1754 (setq font-lock-keywords
1755 (list
1756
1757 ;; Set up the keywords defined above.
1758 (list (concat "\\<\\(" icon-keywords "\\)\\>")
1759 '(0 font-lock-keyword-face))
1760
1761 ;; The things that Icon calls keywords.
1762 (list "&\\sw+\\>" '(0 font-lock-variable-name-face))
1763
1764 ;; At least numbers are simpler than C.
1765 (list (concat "\\<[0-9]+"
1766 "\\([rR][0-9a-zA-Z]+\\|"
1767 "\\.[0-9]+\\([eE][+-]?[0-9]+\\)?\\)\\>\\|"
1768 "\\.[0-9]+\\([eE][+-]?[0-9]+\\)?\\>")
1769 '(0 mdw-number-face))
1770
1771 ;; Preprocessor.
1772 (list (concat "^[ \t]*$[ \t]*\\<\\("
1773 preprocessor-keywords
1774 "\\)\\>")
1775 '(0 font-lock-keyword-face))
1776
1777 ;; And anything else is punctuation.
1778 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1779 '(0 mdw-punct-face)))))
1780
1781 (mdw-post-config-mode-hack))
1782
1783;;;--------------------------------------------------------------------------
1784;;; ARM assembler programming configuration.
1785
1786;; There doesn't appear to be an Emacs mode for this yet.
1787;;
1788;; Better do something about that, I suppose.
1789
1790(defvar arm-assembler-mode-map nil)
1791(defvar arm-assembler-abbrev-table nil)
1792(defvar arm-assembler-mode-syntax-table (make-syntax-table))
1793
1794(or arm-assembler-mode-map
1795 (progn
1796 (setq arm-assembler-mode-map (make-sparse-keymap))
1797 (define-key arm-assembler-mode-map "\C-m" 'arm-assembler-newline)
1798 (define-key arm-assembler-mode-map [C-return] 'newline)
1799 (define-key arm-assembler-mode-map "\t" 'tab-to-tab-stop)))
1800
1801(defun arm-assembler-mode ()
1802 "Major mode for ARM assembler programs"
1803 (interactive)
1804
1805 ;; Do standard major mode things.
1806 (kill-all-local-variables)
1807 (use-local-map arm-assembler-mode-map)
1808 (setq local-abbrev-table arm-assembler-abbrev-table)
1809 (setq major-mode 'arm-assembler-mode)
1810 (setq mode-name "ARM assembler")
1811
1812 ;; Set up syntax table.
1813 (set-syntax-table arm-assembler-mode-syntax-table)
1814 (modify-syntax-entry ?; ; Nasty hack
1815 "<" arm-assembler-mode-syntax-table)
1816 (modify-syntax-entry ?\n ">" arm-assembler-mode-syntax-table)
1817 (modify-syntax-entry ?_ "_" arm-assembler-mode-syntax-table)
1818 (modify-syntax-entry ?' "\"'" arm-assembler-mode-syntax-table)
1819
1820 (make-local-variable 'comment-start)
1821 (setq comment-start ";")
1822 (make-local-variable 'comment-end)
1823 (setq comment-end "")
1824 (make-local-variable 'comment-column)
1825 (setq comment-column 48)
1826 (make-local-variable 'comment-start-skip)
1827 (setq comment-start-skip ";+[ \t]*")
1828
1829 ;; Play with indentation.
1830 (make-local-variable 'indent-line-function)
1831 (setq indent-line-function 'indent-relative-maybe)
1832
1833 ;; Set fill prefix.
1834 (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)")
1835
1836 ;; Fiddle with fontification.
1837 (make-local-variable 'font-lock-keywords)
1838 (setq font-lock-keywords
1839 (list
1840
1841 ;; Handle numbers too.
1842 ;;
1843 ;; The following isn't quite right, but it's close enough.
1844 (list (concat "\\("
1845 "&[0-9a-fA-F]+\\|"
1846 "\\<[0-9]+\\(\\.[0-9]*\\|_[0-9a-zA-Z]+\\|\\)"
1847 "\\)")
1848 '(0 mdw-number-face))
1849
1850 ;; Do something about operators.
1851 (list "^[^ \t]*[ \t]+\\(GET\\|LNK\\)[ \t]+\\([^;\n]*\\)"
1852 '(1 font-lock-keyword-face)
1853 '(2 font-lock-string-face))
1854 (list ":[a-zA-Z]+:"
1855 '(0 font-lock-keyword-face))
1856
1857 ;; Do menemonics and directives.
1858 (list "^[^ \t]*[ \t]+\\([a-zA-Z]+\\)"
1859 '(1 font-lock-keyword-face))
1860
1861 ;; And anything else is punctuation.
1862 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1863 '(0 mdw-punct-face)))
1864
1865 (mdw-post-config-mode-hack))
1866 (run-hooks 'arm-assembler-mode-hook))
1867
1868;;;--------------------------------------------------------------------------
1869;;; Assembler mode.
1870
1871(defun mdw-fontify-asm ()
1872 (modify-syntax-entry ?' "\"")
1873 (modify-syntax-entry ?. "w")
1874 (setf fill-prefix nil)
1875 (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)"))
1876
1877;;;--------------------------------------------------------------------------
1878;;; TCL configuration.
1879
1880(defun mdw-fontify-tcl ()
1881 (mapcar #'(lambda (ch) (modify-syntax-entry ch ".")) '(?$))
1882 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
1883 (make-local-variable 'font-lock-keywords)
1884 (setq font-lock-keywords
1885 (list
1886 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
1887 "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)"
1888 "\\([eE]\\([-+]\\|\\)[0-9_]+\\|\\)")
1889 '(0 mdw-number-face))
1890 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1891 '(0 mdw-punct-face))))
1892 (mdw-post-config-mode-hack))
1893
1894;;;--------------------------------------------------------------------------
1895;;; REXX configuration.
1896
1897(defun mdw-rexx-electric-* ()
1898 (interactive)
1899 (insert ?*)
1900 (rexx-indent-line))
1901
1902(defun mdw-rexx-indent-newline-indent ()
1903 (interactive)
1904 (rexx-indent-line)
1905 (if abbrev-mode (expand-abbrev))
1906 (newline-and-indent))
1907
1908(defun mdw-fontify-rexx ()
1909
1910 ;; Various bits of fiddling.
1911 (setq mdw-auto-indent nil)
1912 (local-set-key [?\C-m] 'mdw-rexx-indent-newline-indent)
1913 (local-set-key [?*] 'mdw-rexx-electric-*)
1914 (mapcar #'(lambda (ch) (modify-syntax-entry ch "w"))
1915 '(?! ?? ?# ?@ ?$))
1916 (mdw-standard-fill-prefix "\\([ \t]*/?\*[ \t]*\\)")
1917
1918 ;; Set up keywords and things for fontification.
1919 (make-local-variable 'font-lock-keywords-case-fold-search)
1920 (setq font-lock-keywords-case-fold-search t)
1921
1922 (setq rexx-indent 2)
1923 (setq rexx-end-indent rexx-indent)
1924 (setq rexx-cont-indent rexx-indent)
1925
1926 (make-local-variable 'font-lock-keywords)
1927 (let ((rexx-keywords
1928 (mdw-regexps "address" "arg" "by" "call" "digits" "do" "drop"
1929 "else" "end" "engineering" "exit" "expose" "for"
1930 "forever" "form" "fuzz" "if" "interpret" "iterate"
1931 "leave" "linein" "name" "nop" "numeric" "off" "on"
1932 "options" "otherwise" "parse" "procedure" "pull"
1933 "push" "queue" "return" "say" "select" "signal"
1934 "scientific" "source" "then" "trace" "to" "until"
1935 "upper" "value" "var" "version" "when" "while"
1936 "with"
1937
1938 "abbrev" "abs" "bitand" "bitor" "bitxor" "b2x"
1939 "center" "center" "charin" "charout" "chars"
1940 "compare" "condition" "copies" "c2d" "c2x"
1941 "datatype" "date" "delstr" "delword" "d2c" "d2x"
1942 "errortext" "format" "fuzz" "insert" "lastpos"
1943 "left" "length" "lineout" "lines" "max" "min"
1944 "overlay" "pos" "queued" "random" "reverse" "right"
1945 "sign" "sourceline" "space" "stream" "strip"
1946 "substr" "subword" "symbol" "time" "translate"
1947 "trunc" "value" "verify" "word" "wordindex"
1948 "wordlength" "wordpos" "words" "xrange" "x2b" "x2c"
1949 "x2d")))
1950
1951 (setq font-lock-keywords
1952 (list
1953
1954 ;; Set up the keywords defined above.
1955 (list (concat "\\<\\(" rexx-keywords "\\)\\>")
1956 '(0 font-lock-keyword-face))
1957
1958 ;; Fontify all symbols the same way.
1959 (list (concat "\\<\\([0-9.][A-Za-z0-9.!?_#@$]*[Ee][+-]?[0-9]+\\|"
1960 "[A-Za-z0-9.!?_#@$]+\\)")
1961 '(0 font-lock-variable-name-face))
1962
1963 ;; And everything else is punctuation.
1964 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1965 '(0 mdw-punct-face)))))
1966
1967 (mdw-post-config-mode-hack))
1968
1969;;;--------------------------------------------------------------------------
1970;;; Standard ML programming style.
1971
1972(defun mdw-fontify-sml ()
1973
1974 ;; Make underscore an honorary letter.
1975 (modify-syntax-entry ?' "w")
1976
1977 ;; Set fill prefix.
1978 (mdw-standard-fill-prefix "\\([ \t]*(\*[ \t]*\\)")
1979
1980 ;; Now define fontification things.
1981 (make-local-variable 'font-lock-keywords)
1982 (let ((sml-keywords
1983 (mdw-regexps "abstype" "and" "andalso" "as"
1984 "case"
1985 "datatype" "do"
1986 "else" "end" "eqtype" "exception"
1987 "fn" "fun" "functor"
1988 "handle"
1989 "if" "in" "include" "infix" "infixr"
1990 "let" "local"
1991 "nonfix"
1992 "of" "op" "open" "orelse"
1993 "raise" "rec"
1994 "sharing" "sig" "signature" "struct" "structure"
1995 "then" "type"
1996 "val"
1997 "where" "while" "with" "withtype")))
1998
1999 (setq font-lock-keywords
2000 (list
2001
2002 ;; Set up the keywords defined above.
2003 (list (concat "\\<\\(" sml-keywords "\\)\\>")
2004 '(0 font-lock-keyword-face))
2005
2006 ;; At least numbers are simpler than C.
2007 (list (concat "\\<\\(\\~\\|\\)"
2008 "\\(0\\(\\([wW]\\|\\)[xX][0-9a-fA-F]+\\|"
2009 "[wW][0-9]+\\)\\|"
2010 "\\([0-9]+\\(\\.[0-9]+\\|\\)"
2011 "\\([eE]\\(\\~\\|\\)"
2012 "[0-9]+\\|\\)\\)\\)")
2013 '(0 mdw-number-face))
2014
2015 ;; And anything else is punctuation.
2016 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2017 '(0 mdw-punct-face)))))
2018
2019 (mdw-post-config-mode-hack))
2020
2021;;;--------------------------------------------------------------------------
2022;;; Haskell configuration.
2023
2024(defun mdw-fontify-haskell ()
2025
2026 ;; Fiddle with syntax table to get comments right.
2027 (modify-syntax-entry ?' "\"")
2028 (modify-syntax-entry ?- ". 123")
2029 (modify-syntax-entry ?{ ". 1b")
2030 (modify-syntax-entry ?} ". 4b")
2031 (modify-syntax-entry ?\n ">")
2032
2033 ;; Set fill prefix.
2034 (mdw-standard-fill-prefix "\\([ \t]*{?--?[ \t]*\\)")
2035
2036 ;; Fiddle with fontification.
2037 (make-local-variable 'font-lock-keywords)
2038 (let ((haskell-keywords
2039 (mdw-regexps "as" "case" "ccall" "class" "data" "default"
2040 "deriving" "do" "else" "foreign" "hiding" "if"
2041 "import" "in" "infix" "infixl" "infixr" "instance"
2042 "let" "module" "newtype" "of" "qualified" "safe"
2043 "stdcall" "then" "type" "unsafe" "where")))
2044
2045 (setq font-lock-keywords
2046 (list
2047 (list "--.*$"
2048 '(0 font-lock-comment-face))
2049 (list (concat "\\<\\(" haskell-keywords "\\)\\>")
2050 '(0 font-lock-keyword-face))
2051 (list (concat "\\<0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2052 "\\<[0-9][0-9_]*\\(\\.[0-9]*\\|\\)"
2053 "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)")
2054 '(0 mdw-number-face))
2055 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2056 '(0 mdw-punct-face)))))
2057
2058 (mdw-post-config-mode-hack))
2059
2060;;;--------------------------------------------------------------------------
2061;;; Erlang configuration.
2062
2063(setq erlang-electric-commands nil)
2064
2065(defun mdw-fontify-erlang ()
2066
2067 ;; Set fill prefix.
2068 (mdw-standard-fill-prefix "\\([ \t]*{?%*[ \t]*\\)")
2069
2070 ;; Fiddle with fontification.
2071 (make-local-variable 'font-lock-keywords)
2072 (let ((erlang-keywords
2073 (mdw-regexps "after" "and" "andalso"
2074 "band" "begin" "bnot" "bor" "bsl" "bsr" "bxor"
2075 "case" "catch" "cond"
2076 "div" "end" "fun" "if" "let" "not"
2077 "of" "or" "orelse"
2078 "query" "receive" "rem" "try" "when" "xor")))
2079
2080 (setq font-lock-keywords
2081 (list
2082 (list "%.*$"
2083 '(0 font-lock-comment-face))
2084 (list (concat "\\<\\(" erlang-keywords "\\)\\>")
2085 '(0 font-lock-keyword-face))
2086 (list (concat "^-\\sw+\\>")
2087 '(0 font-lock-keyword-face))
2088 (list "\\<[0-9]+\\(\\|#[0-9a-zA-Z]+\\|[eE][+-]?[0-9]+\\)\\>"
2089 '(0 mdw-number-face))
2090 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2091 '(0 mdw-punct-face)))))
2092
2093 (mdw-post-config-mode-hack))
2094
2095;;;--------------------------------------------------------------------------
2096;;; Texinfo configuration.
2097
2098(defun mdw-fontify-texinfo ()
2099
2100 ;; Set fill prefix.
2101 (mdw-standard-fill-prefix "\\([ \t]*@c[ \t]+\\)")
2102
2103 ;; Real fontification things.
2104 (make-local-variable 'font-lock-keywords)
2105 (setq font-lock-keywords
2106 (list
2107
2108 ;; Environment names are keywords.
2109 (list "@\\(end\\) *\\([a-zA-Z]*\\)?"
2110 '(2 font-lock-keyword-face))
2111
2112 ;; Unmark escaped magic characters.
2113 (list "\\(@\\)\\([@{}]\\)"
2114 '(1 font-lock-keyword-face)
2115 '(2 font-lock-variable-name-face))
2116
2117 ;; Make sure we get comments properly.
2118 (list "@c\\(\\|omment\\)\\( .*\\)?$"
2119 '(0 font-lock-comment-face))
2120
2121 ;; Command names are keywords.
2122 (list "@\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)"
2123 '(0 font-lock-keyword-face))
2124
2125 ;; Fontify TeX special characters as punctuation.
2126 (list "[{}]+"
2127 '(0 mdw-punct-face))))
2128
2129 (mdw-post-config-mode-hack))
2130
2131;;;--------------------------------------------------------------------------
2132;;; TeX and LaTeX configuration.
2133
2134(defun mdw-fontify-tex ()
2135 (setq ispell-parser 'tex)
2136 (turn-on-reftex)
2137
2138 ;; Don't make maths into a string.
2139 (modify-syntax-entry ?$ ".")
2140 (modify-syntax-entry ?$ "." font-lock-syntax-table)
2141 (local-set-key [?$] 'self-insert-command)
2142
2143 ;; Set fill prefix.
2144 (mdw-standard-fill-prefix "\\([ \t]*%+[ \t]*\\)")
2145
2146 ;; Real fontification things.
2147 (make-local-variable 'font-lock-keywords)
2148 (setq font-lock-keywords
2149 (list
2150
2151 ;; Environment names are keywords.
2152 (list (concat "\\\\\\(begin\\|end\\|newenvironment\\)"
2153 "{\\([^}\n]*\\)}")
2154 '(2 font-lock-keyword-face))
2155
2156 ;; Suspended environment names are keywords too.
2157 (list (concat "\\\\\\(suspend\\|resume\\)\\(\\[[^]]*\\]\\)?"
2158 "{\\([^}\n]*\\)}")
2159 '(3 font-lock-keyword-face))
2160
2161 ;; Command names are keywords.
2162 (list "\\\\\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)"
2163 '(0 font-lock-keyword-face))
2164
2165 ;; Handle @/.../ for italics.
2166 ;; (list "\\(@/\\)\\([^/]*\\)\\(/\\)"
2167 ;; '(1 font-lock-keyword-face)
2168 ;; '(3 font-lock-keyword-face))
2169
2170 ;; Handle @*...* for boldness.
2171 ;; (list "\\(@\\*\\)\\([^*]*\\)\\(\\*\\)"
2172 ;; '(1 font-lock-keyword-face)
2173 ;; '(3 font-lock-keyword-face))
2174
2175 ;; Handle @`...' for literal syntax things.
2176 ;; (list "\\(@`\\)\\([^']*\\)\\('\\)"
2177 ;; '(1 font-lock-keyword-face)
2178 ;; '(3 font-lock-keyword-face))
2179
2180 ;; Handle @<...> for nonterminals.
2181 ;; (list "\\(@<\\)\\([^>]*\\)\\(>\\)"
2182 ;; '(1 font-lock-keyword-face)
2183 ;; '(3 font-lock-keyword-face))
2184
2185 ;; Handle other @-commands.
2186 ;; (list "@\\([^a-zA-Z]\\|[a-zA-Z]*\\)"
2187 ;; '(0 font-lock-keyword-face))
2188
2189 ;; Make sure we get comments properly.
2190 (list "%.*"
2191 '(0 font-lock-comment-face))
2192
2193 ;; Fontify TeX special characters as punctuation.
2194 (list "[$^_{}#&]"
2195 '(0 mdw-punct-face))))
2196
2197 (mdw-post-config-mode-hack))
2198
2199;;;--------------------------------------------------------------------------
2200;;; SGML hacking.
2201
2202(defun mdw-sgml-mode ()
2203 (interactive)
2204 (sgml-mode)
2205 (mdw-standard-fill-prefix "")
2206 (make-local-variable 'sgml-delimiters)
2207 (setq sgml-delimiters
2208 '("AND" "&" "COM" "--" "CRO" "&#" "DSC" "]" "DSO" "[" "DTGC" "]"
2209 "DTGO" "[" "ERO" "&" "ETAGO" ":e" "GRPC" ")" "GRPO" "(" "LIT" "\""
2210 "LITA" "'" "MDC" ">" "MDO" "<!" "MINUS" "-" "MSC" "]]" "NESTC" "{"
2211 "NET" "}" "OPT" "?" "OR" "|" "PERO" "%" "PIC" ">" "PIO" "<?"
2212 "PLUS" "+" "REFC" "." "REP" "*" "RNI" "#" "SEQ" "," "STAGO" ":"
2213 "TAGC" "." "VI" "=" "MS-START" "<![" "MS-END" "]]>"
2214 "XML-ECOM" "-->" "XML-PIC" "?>" "XML-SCOM" "<!--" "XML-TAGCE" "/>"
2215 "NULL" ""))
2216 (setq major-mode 'mdw-sgml-mode)
2217 (setq mode-name "[mdw] SGML")
2218 (run-hooks 'mdw-sgml-mode-hook))
2219
2220;;;--------------------------------------------------------------------------
2221;;; Configuration files.
2222
2223(defvar mdw-conf-quote-normal nil
2224 "*Control syntax category of quote characters `\"' and `''.
2225If this is `t', consider quote characters to be normal
2226punctuation, as for `conf-quote-normal'. If this is `nil' then
2227leave quote characters as quotes. If this is a list, then
2228consider the quote characters in the list to be normal
2229punctuation. If this is a single quote character, then consider
2230that character only to be normal punctuation.")
2231(defun mdw-conf-quote-normal-acceptable-value-p (value)
2232 "Is the VALUE is an acceptable value for `mdw-conf-quote-normal'?"
2233 (or (booleanp value)
2234 (every (lambda (v) (memq v '(?\" ?')))
2235 (if (listp value) value (list value)))))
2236(put 'mdw-conf-quote-normal 'safe-local-variable '
2237 mdw-conf-quote-normal-acceptable-value-p)
2238
2239(defun mdw-fix-up-quote ()
2240 "Apply the setting of `mdw-conf-quote-normal'."
2241 (let ((flag mdw-conf-quote-normal))
2242 (cond ((eq flag t)
2243 (conf-quote-normal t))
2244 ((not flag)
2245 nil)
2246 (t
2247 (let ((table (copy-syntax-table (syntax-table))))
2248 (mapc (lambda (ch) (modify-syntax-entry ch "." table))
2249 (if (listp flag) flag (list flag)))
2250 (set-syntax-table table)
2251 (and font-lock-mode (font-lock-fontify-buffer)))))))
2252(defun mdw-fix-up-quote-hack ()
2253 "Unpleasant hack to call `mdw-fix-up-quote' at the right time.
2254Annoyingly, `hack-local-variables' is done after `set-auto-mode'
2255so we wouldn't see a local-variable setting of
2256`mdw-conf-quote-normal' in `conf-mode-hook'. Instead, wire
2257ourselves onto `hack-local-variables-hook' here, and check the
2258setting once it's actually been made."
2259 (add-hook 'hack-local-variables-hook 'mdw-fix-up-quote t t))
2260(add-hook 'conf-mode-hook 'mdw-fix-up-quote-hack t)
2261
2262;;;--------------------------------------------------------------------------
2263;;; Shell scripts.
2264
2265(defun mdw-setup-sh-script-mode ()
2266
2267 ;; Fetch the shell interpreter's name.
2268 (let ((shell-name sh-shell-file))
2269
2270 ;; Try reading the hash-bang line.
2271 (save-excursion
2272 (goto-char (point-min))
2273 (if (looking-at "#![ \t]*\\([^ \t\n]*\\)")
2274 (setq shell-name (match-string 1))))
2275
2276 ;; Now try to set the shell.
2277 ;;
2278 ;; Don't let `sh-set-shell' bugger up my script.
2279 (let ((executable-set-magic #'(lambda (s &rest r) s)))
2280 (sh-set-shell shell-name)))
2281
2282 ;; Now enable my keys and the fontification.
2283 (mdw-misc-mode-config)
2284
2285 ;; Set the indentation level correctly.
2286 (setq sh-indentation 2)
2287 (setq sh-basic-offset 2))
2288
2289(setq sh-shell-file "/bin/sh")
2290
2291;; Awful hacking to override the shell detection for particular scripts.
2292(defmacro define-custom-shell-mode (name shell)
2293 `(defun ,name ()
2294 (interactive)
2295 (set (make-local-variable 'sh-shell-file) ,shell)
2296 (sh-mode)))
2297(define-custom-shell-mode bash-mode "/bin/bash")
2298(define-custom-shell-mode rc-mode "/usr/bin/rc")
2299(put 'sh-shell-file 'permanent-local t)
2300
2301;; Hack the rc syntax table. Backquotes aren't paired in rc.
2302(eval-after-load "sh-script"
2303 '(or (assq 'rc sh-mode-syntax-table-input)
2304 (let ((frag '(nil
2305 ?# "<"
2306 ?\n ">#"
2307 ?\" "\"\""
2308 ?\' "\"\'"
2309 ?$ "'"
2310 ?\` "."
2311 ?! "_"
2312 ?% "_"
2313 ?. "_"
2314 ?^ "_"
2315 ?~ "_"
2316 ?, "_"
2317 ?= "."
2318 ?< "."
2319 ?> "."))
2320 (assoc (assq 'rc sh-mode-syntax-table-input)))
2321 (if assoc
2322 (rplacd assoc frag)
2323 (setq sh-mode-syntax-table-input
2324 (cons (cons 'rc frag)
2325 sh-mode-syntax-table-input))))))
2326
2327;;;--------------------------------------------------------------------------
2328;;; Emacs shell mode.
2329
2330(defun mdw-eshell-prompt ()
2331 (let ((left "[") (right "]"))
2332 (when (= (user-uid) 0)
2333 (setq left "«" right "»"))
2334 (concat left
2335 (save-match-data
2336 (replace-regexp-in-string "\\..*$" "" (system-name)))
2337 " "
2338 (let* ((pwd (eshell/pwd)) (npwd (length pwd))
2339 (home (expand-file-name "~")) (nhome (length home)))
2340 (if (and (>= npwd nhome)
2341 (or (= nhome npwd)
2342 (= (elt pwd nhome) ?/))
2343 (string= (substring pwd 0 nhome) home))
2344 (concat "~" (substring pwd (length home)))
2345 pwd))
2346 right)))
2347(setq eshell-prompt-function 'mdw-eshell-prompt)
2348(setq eshell-prompt-regexp "^\\[[^]>]+\\(\\]\\|>>?\\)")
2349
2350(defun eshell/e (file) (find-file file) nil)
2351(defun eshell/ee (file) (find-file-other-window file) nil)
2352(defun eshell/w3m (url) (w3m-goto-url url) nil)
2353
2354(mdw-define-face eshell-prompt (t :weight bold))
2355(mdw-define-face eshell-ls-archive (t :weight bold :foreground "red"))
2356(mdw-define-face eshell-ls-backup (t :foreground "lightgrey" :slant italic))
2357(mdw-define-face eshell-ls-product (t :foreground "lightgrey" :slant italic))
2358(mdw-define-face eshell-ls-clutter (t :foreground "lightgrey" :slant italic))
2359(mdw-define-face eshell-ls-executable (t :weight bold))
2360(mdw-define-face eshell-ls-directory (t :foreground "cyan" :weight bold))
2361(mdw-define-face eshell-ls-readonly (t nil))
2362(mdw-define-face eshell-ls-symlink (t :foreground "cyan"))
2363
2364;;;--------------------------------------------------------------------------
2365;;; Messages-file mode.
2366
2367(defun messages-mode-guts ()
2368 (setq messages-mode-syntax-table (make-syntax-table))
2369 (set-syntax-table messages-mode-syntax-table)
2370 (modify-syntax-entry ?0 "w" messages-mode-syntax-table)
2371 (modify-syntax-entry ?1 "w" messages-mode-syntax-table)
2372 (modify-syntax-entry ?2 "w" messages-mode-syntax-table)
2373 (modify-syntax-entry ?3 "w" messages-mode-syntax-table)
2374 (modify-syntax-entry ?4 "w" messages-mode-syntax-table)
2375 (modify-syntax-entry ?5 "w" messages-mode-syntax-table)
2376 (modify-syntax-entry ?6 "w" messages-mode-syntax-table)
2377 (modify-syntax-entry ?7 "w" messages-mode-syntax-table)
2378 (modify-syntax-entry ?8 "w" messages-mode-syntax-table)
2379 (modify-syntax-entry ?9 "w" messages-mode-syntax-table)
2380 (make-local-variable 'comment-start)
2381 (make-local-variable 'comment-end)
2382 (make-local-variable 'indent-line-function)
2383 (setq indent-line-function 'indent-relative)
2384 (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
2385 (make-local-variable 'font-lock-defaults)
2386 (make-local-variable 'messages-mode-keywords)
2387 (let ((keywords
2388 (mdw-regexps "array" "bitmap" "callback" "docs[ \t]+enum"
2389 "export" "enum" "fixed-octetstring" "flags"
2390 "harmless" "map" "nested" "optional"
2391 "optional-tagged" "package" "primitive"
2392 "primitive-nullfree" "relaxed[ \t]+enum"
2393 "set" "table" "tagged-optional" "union"
2394 "variadic" "vector" "version" "version-tag")))
2395 (setq messages-mode-keywords
2396 (list
2397 (list (concat "\\<\\(" keywords "\\)\\>:")
2398 '(0 font-lock-keyword-face))
2399 '("\\([-a-zA-Z0-9]+:\\)" (0 font-lock-warning-face))
2400 '("\\(\\<[a-z][-_a-zA-Z0-9]*\\)"
2401 (0 font-lock-variable-name-face))
2402 '("\\<\\([0-9]+\\)\\>" (0 mdw-number-face))
2403 '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2404 (0 mdw-punct-face)))))
2405 (setq font-lock-defaults
2406 '(messages-mode-keywords nil nil nil nil))
2407 (run-hooks 'messages-file-hook))
2408
2409(defun messages-mode ()
2410 (interactive)
2411 (fundamental-mode)
2412 (setq major-mode 'messages-mode)
2413 (setq mode-name "Messages")
2414 (messages-mode-guts)
2415 (modify-syntax-entry ?# "<" messages-mode-syntax-table)
2416 (modify-syntax-entry ?\n ">" messages-mode-syntax-table)
2417 (setq comment-start "# ")
2418 (setq comment-end "")
2419 (run-hooks 'messages-mode-hook))
2420
2421(defun cpp-messages-mode ()
2422 (interactive)
2423 (fundamental-mode)
2424 (setq major-mode 'cpp-messages-mode)
2425 (setq mode-name "CPP Messages")
2426 (messages-mode-guts)
2427 (modify-syntax-entry ?* ". 23" messages-mode-syntax-table)
2428 (modify-syntax-entry ?/ ". 14" messages-mode-syntax-table)
2429 (setq comment-start "/* ")
2430 (setq comment-end " */")
2431 (let ((preprocessor-keywords
2432 (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
2433 "ident" "if" "ifdef" "ifndef" "import" "include"
2434 "line" "pragma" "unassert" "undef" "warning")))
2435 (setq messages-mode-keywords
2436 (append (list (list (concat "^[ \t]*\\#[ \t]*"
2437 "\\(include\\|import\\)"
2438 "[ \t]*\\(<[^>]+\\(>\\|\\)\\)")
2439 '(2 font-lock-string-face))
2440 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
2441 preprocessor-keywords
2442 "\\)\\>\\|[0-9]+\\|$\\)\\)")
2443 '(1 font-lock-keyword-face)))
2444 messages-mode-keywords)))
2445 (run-hooks 'cpp-messages-mode-hook))
2446
2447(add-hook 'messages-mode-hook 'mdw-misc-mode-config t)
2448(add-hook 'cpp-messages-mode-hook 'mdw-misc-mode-config t)
2449; (add-hook 'messages-file-hook 'mdw-fontify-messages t)
2450
2451;;;--------------------------------------------------------------------------
2452;;; Messages-file mode.
2453
2454(defvar mallow-driver-substitution-face 'mallow-driver-substitution-face
2455 "Face to use for subsittution directives.")
2456(make-face 'mallow-driver-substitution-face)
2457(defvar mallow-driver-text-face 'mallow-driver-text-face
2458 "Face to use for body text.")
2459(make-face 'mallow-driver-text-face)
2460
2461(defun mallow-driver-mode ()
2462 (interactive)
2463 (fundamental-mode)
2464 (setq major-mode 'mallow-driver-mode)
2465 (setq mode-name "Mallow driver")
2466 (setq mallow-driver-mode-syntax-table (make-syntax-table))
2467 (set-syntax-table mallow-driver-mode-syntax-table)
2468 (make-local-variable 'comment-start)
2469 (make-local-variable 'comment-end)
2470 (make-local-variable 'indent-line-function)
2471 (setq indent-line-function 'indent-relative)
2472 (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
2473 (make-local-variable 'font-lock-defaults)
2474 (make-local-variable 'mallow-driver-mode-keywords)
2475 (let ((keywords
2476 (mdw-regexps "each" "divert" "file" "if"
2477 "perl" "set" "string" "type" "write")))
2478 (setq mallow-driver-mode-keywords
2479 (list
2480 (list (concat "^%\\s *\\(}\\|\\(" keywords "\\)\\>\\).*$")
2481 '(0 font-lock-keyword-face))
2482 (list "^%\\s *\\(#.*\\|\\)$"
2483 '(0 font-lock-comment-face))
2484 (list "^%"
2485 '(0 font-lock-keyword-face))
2486 (list "^|?\\(.+\\)$" '(1 mallow-driver-text-face))
2487 (list "\\${[^}]*}"
2488 '(0 mallow-driver-substitution-face t)))))
2489 (setq font-lock-defaults
2490 '(mallow-driver-mode-keywords nil nil nil nil))
2491 (modify-syntax-entry ?\" "_" mallow-driver-mode-syntax-table)
2492 (modify-syntax-entry ?\n ">" mallow-driver-mode-syntax-table)
2493 (setq comment-start "%# ")
2494 (setq comment-end "")
2495 (run-hooks 'mallow-driver-mode-hook))
2496
2497(add-hook 'mallow-driver-hook 'mdw-misc-mode-config t)
2498
2499;;;--------------------------------------------------------------------------
2500;;; NFast debugs.
2501
2502(defun nfast-debug-mode ()
2503 (interactive)
2504 (fundamental-mode)
2505 (setq major-mode 'nfast-debug-mode)
2506 (setq mode-name "NFast debug")
2507 (setq messages-mode-syntax-table (make-syntax-table))
2508 (set-syntax-table messages-mode-syntax-table)
2509 (make-local-variable 'font-lock-defaults)
2510 (make-local-variable 'nfast-debug-mode-keywords)
2511 (setq truncate-lines t)
2512 (setq nfast-debug-mode-keywords
2513 (list
2514 '("^\\(NFast_\\(Connect\\|Disconnect\\|Submit\\|Wait\\)\\)"
2515 (0 font-lock-keyword-face))
2516 (list (concat "^[ \t]+\\(\\("
2517 "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
2518 "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
2519 "[ \t]+\\)*"
2520 "[0-9a-fA-F]+\\)[ \t]*$")
2521 '(0 mdw-number-face))
2522 '("^[ \t]+\.status=[ \t]+\\<\\(OK\\)\\>"
2523 (1 font-lock-keyword-face))
2524 '("^[ \t]+\.status=[ \t]+\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>"
2525 (1 font-lock-warning-face))
2526 '("^[ \t]+\.status[ \t]+\\<\\(zero\\)\\>"
2527 (1 nil))
2528 (list (concat "^[ \t]+\\.cmd=[ \t]+"
2529 "\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>")
2530 '(1 font-lock-keyword-face))
2531 '("-?\\<\\([0-9]+\\|0x[0-9a-fA-F]+\\)\\>" (0 mdw-number-face))
2532 '("^\\([ \t]+[a-z0-9.]+\\)" (0 font-lock-variable-name-face))
2533 '("\\<\\([a-z][a-z0-9.]+\\)\\>=" (1 font-lock-variable-name-face))
2534 '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" (0 mdw-punct-face))))
2535 (setq font-lock-defaults
2536 '(nfast-debug-mode-keywords nil nil nil nil))
2537 (run-hooks 'nfast-debug-mode-hook))
2538
2539;;;--------------------------------------------------------------------------
2540;;; Other languages.
2541
2542;; Smalltalk.
2543
2544(defun mdw-setup-smalltalk ()
2545 (and mdw-auto-indent
2546 (local-set-key "\C-m" 'smalltalk-newline-and-indent))
2547 (make-local-variable 'mdw-auto-indent)
2548 (setq mdw-auto-indent nil)
2549 (local-set-key "\C-i" 'smalltalk-reindent))
2550
2551(defun mdw-fontify-smalltalk ()
2552 (make-local-variable 'font-lock-keywords)
2553 (setq font-lock-keywords
2554 (list
2555 (list "\\<[A-Z][a-zA-Z0-9]*\\>"
2556 '(0 font-lock-keyword-face))
2557 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
2558 "[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)"
2559 "\\([eE]\\([-+]\\|\\)[0-9_]+\\|\\)")
2560 '(0 mdw-number-face))
2561 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2562 '(0 mdw-punct-face))))
2563 (mdw-post-config-mode-hack))
2564
2565;; Lispy languages.
2566
2567;; Unpleasant bodge.
2568(unless (boundp 'slime-repl-mode-map)
2569 (setq slime-repl-mode-map (make-sparse-keymap)))
2570
2571(defun mdw-indent-newline-and-indent ()
2572 (interactive)
2573 (indent-for-tab-command)
2574 (newline-and-indent))
2575
2576(eval-after-load "cl-indent"
2577 '(progn
2578 (mapc #'(lambda (pair)
2579 (put (car pair)
2580 'common-lisp-indent-function
2581 (cdr pair)))
2582 '((destructuring-bind . ((&whole 4 &rest 1) 4 &body))
2583 (multiple-value-bind . ((&whole 4 &rest 1) 4 &body))))))
2584
2585(defun mdw-common-lisp-indent ()
2586 (make-local-variable 'lisp-indent-function)
2587 (setq lisp-indent-function 'common-lisp-indent-function))
2588
2589(setq lisp-simple-loop-indentation 2
2590 lisp-loop-keyword-indentation 6
2591 lisp-loop-forms-indentation 6)
2592
2593(defun mdw-fontify-lispy ()
2594
2595 ;; Set fill prefix.
2596 (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)")
2597
2598 ;; Not much fontification needed.
2599 (make-local-variable 'font-lock-keywords)
2600 (setq font-lock-keywords
2601 (list
2602 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2603 '(0 mdw-punct-face))))
2604
2605 (mdw-post-config-mode-hack))
2606
2607(defun comint-send-and-indent ()
2608 (interactive)
2609 (comint-send-input)
2610 (and mdw-auto-indent
2611 (indent-for-tab-command)))
2612
2613(defun mdw-setup-m4 ()
2614 (mdw-standard-fill-prefix "\\([ \t]*\\(?:#+\\|\\<dnl\\>\\)[ \t]*\\)"))
2615
2616;;;--------------------------------------------------------------------------
2617;;; Text mode.
2618
2619(defun mdw-text-mode ()
2620 (setq fill-column 72)
2621 (flyspell-mode t)
2622 (mdw-standard-fill-prefix
2623 "\\([ \t]*\\([>#|:] ?\\)*[ \t]*\\)" 3)
2624 (auto-fill-mode 1))
2625
2626;;;--------------------------------------------------------------------------
2627;;; Outline and hide/show modes.
2628
2629(defun mdw-outline-collapse-all ()
2630 "Completely collapse everything in the entire buffer."
2631 (interactive)
2632 (save-excursion
2633 (goto-char (point-min))
2634 (while (< (point) (point-max))
2635 (hide-subtree)
2636 (forward-line))))
2637
2638(setq hs-hide-comments-when-hiding-all nil)
2639
2640(defadvice hs-hide-all (after hide-first-comment activate)
2641 (save-excursion (hs-hide-initial-comment-block)))
2642
2643;;;--------------------------------------------------------------------------
2644;;; Shell mode.
2645
2646(defun mdw-sh-mode-setup ()
2647 (local-set-key [?\C-a] 'comint-bol)
2648 (add-hook 'comint-output-filter-functions
2649 'comint-watch-for-password-prompt))
2650
2651(defun mdw-term-mode-setup ()
2652 (setq term-prompt-regexp shell-prompt-pattern)
2653 (make-local-variable 'mouse-yank-at-point)
2654 (make-local-variable 'transient-mark-mode)
2655 (setq mouse-yank-at-point t)
2656 (auto-fill-mode -1)
2657 (setq tab-width 8))
2658
2659(defun term-send-meta-right () (interactive) (term-send-raw-string "\e\e[C"))
2660(defun term-send-meta-left () (interactive) (term-send-raw-string "\e\e[D"))
2661(defun term-send-ctrl-uscore () (interactive) (term-send-raw-string "\C-_"))
2662(defun term-send-meta-meta-something ()
2663 (interactive)
2664 (term-send-raw-string "\e\e")
2665 (term-send-raw))
2666(eval-after-load 'term
2667 '(progn
2668 (define-key term-raw-map [?\e ?\e] nil)
2669 (define-key term-raw-map [?\e ?\e t] 'term-send-meta-meta-something)
2670 (define-key term-raw-map [?\C-/] 'term-send-ctrl-uscore)
2671 (define-key term-raw-map [M-right] 'term-send-meta-right)
2672 (define-key term-raw-map [?\e ?\M-O ?C] 'term-send-meta-right)
2673 (define-key term-raw-map [M-left] 'term-send-meta-left)
2674 (define-key term-raw-map [?\e ?\M-O ?D] 'term-send-meta-left)))
2675
2676(defadvice term-exec (before program-args-list compile activate)
2677 "If the PROGRAM argument is a list, interpret it as (PROGRAM . SWITCHES).
2678This allows you to pass a list of arguments through `ansi-term'."
2679 (let ((program (ad-get-arg 2)))
2680 (if (listp program)
2681 (progn
2682 (ad-set-arg 2 (car program))
2683 (ad-set-arg 4 (cdr program))))))
2684
2685(defun ssh (host)
2686 "Open a terminal containing an ssh session to the HOST."
2687 (interactive "sHost: ")
2688 (ansi-term (list "ssh" host) (format "ssh@%s" host)))
2689
2690;;;--------------------------------------------------------------------------
2691;;; Inferior Emacs Lisp.
2692
2693(setq comint-prompt-read-only t)
2694
2695(eval-after-load "comint"
2696 '(progn
2697 (define-key comint-mode-map "\C-w" 'comint-kill-region)
2698 (define-key comint-mode-map [C-S-backspace] 'comint-kill-whole-line)))
2699
2700(eval-after-load "ielm"
2701 '(progn
2702 (define-key ielm-map "\C-w" 'comint-kill-region)
2703 (define-key ielm-map [C-S-backspace] 'comint-kill-whole-line)))
2704
2705;;;----- That's all, folks --------------------------------------------------
2706
2707(provide 'dot-emacs)