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