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