a74b693f88c2afeb0abb63a8c52712e1306a1605
[profile] / el / dot-emacs.el
1 ;;; -*- mode: emacs-lisp; coding: utf-8 -*-
2 ;;;
3 ;;; Functions and macros for .emacs
4 ;;;
5 ;;; (c) 2004 Mark Wooding
6 ;;;
7
8 ;;;----- Licensing notice ---------------------------------------------------
9 ;;;
10 ;;; This program is free software; you can redistribute it and/or modify
11 ;;; it under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 2 of the License, or
13 ;;; (at your option) any later version.
14 ;;;
15 ;;; This program is distributed in the hope that it will be useful,
16 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
19 ;;;
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with this program; if not, write to the Free Software Foundation,
22 ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
24 ;;;--------------------------------------------------------------------------
25 ;;; Check command-line.
26
27 (defgroup mdw nil
28 "Customization for mdw's Emacs configuration."
29 :prefix "mdw-")
30
31 (defun mdw-check-command-line-switch (switch)
32 (let ((probe nil) (next command-line-args) (found nil))
33 (while next
34 (cond ((string= (car next) switch)
35 (setq found t)
36 (if probe (rplacd probe (cdr next))
37 (setq command-line-args (cdr next))))
38 (t
39 (setq probe next)))
40 (setq next (cdr next)))
41 found))
42
43 (defvar mdw-fast-startup nil
44 "Whether .emacs should optimize for rapid startup.
45 This may be at the expense of cool features.")
46 (setq mdw-fast-startup
47 (mdw-check-command-line-switch "--mdw-fast-startup"))
48
49 (defvar mdw-splashy-startup nil
50 "Whether to show a splash screen and related frippery.")
51 (setq mdw-splashy-startup
52 (mdw-check-command-line-switch "--mdw-splashy-startup"))
53
54 ;;;--------------------------------------------------------------------------
55 ;;; Some general utilities.
56
57 (eval-when-compile
58 (unless (fboundp 'make-regexp) (load "make-regexp"))
59 (require 'cl))
60
61 (defmacro mdw-regexps (&rest list)
62 "Turn a LIST of strings into a single regular expression at compile-time."
63 (declare (indent nil)
64 (debug 0))
65 `',(make-regexp list))
66
67 (defun mdw-wrong ()
68 "This is not the key sequence you're looking for."
69 (interactive)
70 (error "wrong button"))
71
72 (defun mdw-emacs-version-p (major &optional minor)
73 "Return non-nil if the running Emacs is at least version MAJOR.MINOR."
74 (or (> emacs-major-version major)
75 (and (= emacs-major-version major)
76 (>= emacs-minor-version (or minor 0)))))
77
78 (defun mdw-submode-p (mode parent)
79 "Return non-nil if MODE is indirectly derived from PARENT."
80 (let ((answer nil))
81 (while (cond ((eq mode parent) (setq answer t) nil)
82 (t (setq mode (get mode 'derived-mode-parent)))))
83 answer))
84
85 ;; Some error trapping.
86 ;;
87 ;; If individual bits of this file go tits-up, we don't particularly want
88 ;; the whole lot to stop right there and then, because it's bloody annoying.
89
90 (eval-and-compile
91 (defmacro trap (&rest forms)
92 "Execute FORMS without allowing errors to propagate outside."
93 (declare (indent 0)
94 (debug t))
95 `(condition-case err
96 ,(if (cdr forms) (cons 'progn forms) (car forms))
97 (error (message "Error (trapped): %s in %s"
98 (error-message-string err)
99 ',forms)))))
100
101 ;; Configuration reading.
102
103 (defvar mdw-config nil)
104 (defun mdw-config (sym)
105 "Read the configuration variable named SYM."
106 (unless mdw-config
107 (setq mdw-config
108 (flet ((replace (what with)
109 (goto-char (point-min))
110 (while (re-search-forward what nil t)
111 (replace-match with t))))
112 (with-temp-buffer
113 (insert-file-contents "~/.mdw.conf")
114 (replace "^[ \t]*\\(#.*\\)?\n" "")
115 (replace (concat "^[ \t]*"
116 "\\([-a-zA-Z0-9_.]*\\)"
117 "[ \t]*=[ \t]*"
118 "\\(.*[^ \t\n]\\)?"
119 "[ \t]**\\(\n\\|$\\)")
120 "(\\1 . \"\\2\")\n")
121 (car (read-from-string
122 (concat "(" (buffer-string) ")")))))))
123 (cdr (assq sym mdw-config)))
124
125 ;; Width configuration.
126
127 (defcustom mdw-column-width
128 (string-to-number (or (mdw-config 'emacs-width) "77"))
129 "Width of Emacs columns."
130 :type 'integer)
131 (defcustom mdw-text-width mdw-column-width
132 "Expected width of text within columns."
133 :type 'integer
134 :safe 'integerp)
135
136 ;; Local variables hacking.
137
138 (defun run-local-vars-mode-hook ()
139 "Run a hook for the major-mode after local variables have been processed."
140 (run-hooks (intern (concat (symbol-name major-mode)
141 "-local-variables-hook"))))
142 (add-hook 'hack-local-variables-hook 'run-local-vars-mode-hook)
143
144 ;; Set up the load path convincingly.
145
146 (dolist (dir (append (and (boundp 'debian-emacs-flavor)
147 (list (concat "/usr/share/"
148 (symbol-name debian-emacs-flavor)
149 "/site-lisp")))))
150 (dolist (sub (directory-files dir t))
151 (when (and (file-accessible-directory-p sub)
152 (not (member sub load-path)))
153 (setq load-path (nconc load-path (list sub))))))
154
155 ;; Is an Emacs library available?
156
157 (defun library-exists-p (name)
158 "Return non-nil if NAME is an available library.
159 Return non-nil if NAME.el (or NAME.elc) somewhere on the Emacs
160 load path. The non-nil value is the filename we found for the
161 library."
162 (let ((path load-path) elt (foundp nil))
163 (while (and path (not foundp))
164 (setq elt (car path))
165 (setq path (cdr path))
166 (setq foundp (or (let ((file (concat elt "/" name ".elc")))
167 (and (file-exists-p file) file))
168 (let ((file (concat elt "/" name ".el")))
169 (and (file-exists-p file) file)))))
170 foundp))
171
172 (defun maybe-autoload (symbol file &optional docstring interactivep type)
173 "Set an autoload if the file actually exists."
174 (and (library-exists-p file)
175 (autoload symbol file docstring interactivep type)))
176
177 (defun mdw-kick-menu-bar (&optional frame)
178 "Regenerate FRAME's menu bar so it doesn't have empty menus."
179 (interactive)
180 (unless frame (setq frame (selected-frame)))
181 (let ((old (frame-parameter frame 'menu-bar-lines)))
182 (set-frame-parameter frame 'menu-bar-lines 0)
183 (set-frame-parameter frame 'menu-bar-lines old)))
184
185 ;; Page motion.
186
187 (defun mdw-fixup-page-position ()
188 (unless (eq (char-before (point)) ?\f)
189 (forward-line 0)))
190
191 (defadvice backward-page (after mdw-fixup compile activate)
192 (mdw-fixup-page-position))
193 (defadvice forward-page (after mdw-fixup compile activate)
194 (mdw-fixup-page-position))
195
196 ;; Splitting windows.
197
198 (unless (fboundp 'scroll-bar-columns)
199 (defun scroll-bar-columns (side)
200 (cond ((eq side 'left) 0)
201 (window-system 3)
202 (t 1))))
203 (unless (fboundp 'fringe-columns)
204 (defun fringe-columns (side)
205 (cond ((not window-system) 0)
206 ((eq side 'left) 1)
207 (t 2))))
208
209 (defun mdw-horizontal-window-overhead ()
210 "Computes the horizontal window overhead.
211 This is the number of columns used by fringes, scroll bars and other such
212 cruft."
213 (if (not window-system)
214 1
215 (let ((tot 0))
216 (dolist (what '(scroll-bar fringe))
217 (dolist (side '(left right))
218 (incf tot (funcall (intern (concat (symbol-name what) "-columns"))
219 side))))
220 tot)))
221
222 (defun mdw-split-window-horizontally (&optional width)
223 "Split a window horizontally.
224 Without a numeric argument, split the window approximately in
225 half. With a numeric argument WIDTH, allocate WIDTH columns to
226 the left-hand window (if positive) or -WIDTH columns to the
227 right-hand window (if negative). Space for scroll bars and
228 fringes is not taken out of the allowance for WIDTH, unlike
229 \\[split-window-horizontally]."
230 (interactive "P")
231 (split-window-horizontally
232 (cond ((null width) nil)
233 ((>= width 0) (+ width (mdw-horizontal-window-overhead)))
234 ((< width 0) width))))
235
236 (defun mdw-preferred-column-width ()
237 "Return the preferred column width."
238 (if (and window-system (mdw-emacs-version-p 22)) mdw-column-width
239 (1+ mdw-column-width)))
240
241 (defun mdw-divvy-window (&optional width)
242 "Split a wide window into appropriate widths."
243 (interactive "P")
244 (setq width (if width (prefix-numeric-value width)
245 (mdw-preferred-column-width)))
246 (let* ((win (selected-window))
247 (sb-width (mdw-horizontal-window-overhead))
248 (c (/ (+ (window-width) sb-width)
249 (+ width sb-width))))
250 (while (> c 1)
251 (setq c (1- c))
252 (split-window-horizontally (+ width sb-width))
253 (other-window 1))
254 (select-window win)))
255
256 (defun mdw-set-frame-width (columns &optional width)
257 "Set the current frame to be the correct width for COLUMNS columns.
258
259 If WIDTH is non-nil, then it provides the width for the new columns. (This
260 can be set interactively with a prefix argument.)"
261 (interactive "nColumns:
262 P")
263 (setq width (if width (prefix-numeric-value width)
264 (mdw-preferred-column-width)))
265 (let ((sb-width (mdw-horizontal-window-overhead)))
266 (set-frame-width (selected-frame)
267 (- (* columns (+ width sb-width))
268 sb-width))
269 (mdw-divvy-window width)))
270
271 (defcustom mdw-frame-width-fudge
272 (cond ((<= emacs-major-version 20) 1)
273 ((= emacs-major-version 26) 3)
274 (t 0))
275 "The number of extra columns to add to the desired frame width.
276
277 This is sadly necessary because Emacs 26 is broken in this regard."
278 :type 'integer)
279
280 (defcustom mdw-frame-colour-alist
281 '((black . ("#000000" . "#ffffff"))
282 (red . ("#2a0000" . "#ffffff"))
283 (green . ("#002a00" . "#ffffff"))
284 (blue . ("#00002a" . "#ffffff")))
285 "Alist mapping symbol names to (FOREGROUND . BACKGROUND) colour pairs."
286 :type '(alist :key-type symbol :value-type (cons color color)))
287
288 (defun mdw-set-frame-colour (colour &optional frame)
289 (interactive "xColour name or (FOREGROUND . BACKGROUND) pair:
290 ")
291 (when (and colour (symbolp colour))
292 (let ((entry (assq colour mdw-frame-colour-alist)))
293 (unless entry (error "Unknown colour `%s'" colour))
294 (setf colour (cdr entry))))
295 (set-frame-parameter frame 'background-color (car colour))
296 (set-frame-parameter frame 'foreground-color (cdr colour)))
297
298 ;; Window configuration switching.
299
300 (defvar mdw-current-window-configuration nil
301 "The current window configuration register name, or `nil'.")
302
303 (defun mdw-switch-window-configuration (register &optional no-save)
304 "Switch make REGISTER be the new current window configuration.
305 If a current window configuration register is established, and
306 NO-SAVE is nil, then save the current window configuration to
307 that register first.
308
309 Signal an error if the new register contains something other than
310 a window configuration. If the register is unset then save the
311 current window configuration to it immediately.
312
313 With one or three C-u, or an odd numeric prefix argument, set
314 NO-SAVE, so the previous window configuration register is left
315 unchanged.
316
317 With two or three C-u, or a prefix argument which is an odd
318 multiple of 2, just clear the record of the current window
319 configuration register, so that the next switch doesn't save the
320 prevailing configuration."
321 (interactive
322 (let ((arg current-prefix-arg))
323 (list (if (or (and (consp arg) (= (car arg) 16) (= (car arg) 64))
324 (and (integerp arg) (not (zerop (logand arg 2)))))
325 nil
326 (register-read-with-preview "Switch to window configuration: "))
327 (or (and (consp arg) (= (car arg) 4) (= (car arg) 64))
328 (and (integerp arg) (not (zerop (logand arg 1))))))))
329
330 (let ((previous mdw-current-window-configuration)
331 (current-windows (list (current-window-configuration)
332 (point-marker)))
333 (register-value (and register (get-register register))))
334 (when (and mdw-current-window-configuration (not no-save))
335 (set-register mdw-current-window-configuration current-windows))
336 (cond ((null register)
337 (setq mdw-current-window-configuration nil)
338 (if previous
339 (message "Left window configuration `%c'." previous)
340 (message "Nothing to do!")))
341 ((not (or (null register-value)
342 (and (consp register-value)
343 (window-configuration-p (car register-value))
344 (integer-or-marker-p (cadr register-value))
345 (null (caddr register-value)))))
346 (error "Register `%c' is not a window configuration" register))
347 (t
348 (cond ((null register-value)
349 (set-register register current-windows)
350 (message "Started new window configuration `%c'."
351 register))
352 (t
353 (set-window-configuration (car register-value))
354 (goto-char (cadr register-value))
355 (message "Switched to window configuration `%c'."
356 register)))
357 (setq mdw-current-window-configuration register)))))
358
359 ;; Don't raise windows unless I say so.
360
361 (defcustom mdw-inhibit-raise-frame nil
362 "Whether `raise-frame' should do nothing when the frame is mapped."
363 :type 'boolean)
364
365 (defadvice raise-frame
366 (around mdw-inhibit (&optional frame) activate compile)
367 "Don't actually do anything if `mdw-inhibit-raise-frame' is true, and the
368 frame is actually mapped on the screen."
369 (if mdw-inhibit-raise-frame
370 (make-frame-visible frame)
371 ad-do-it))
372
373 (defmacro mdw-advise-to-inhibit-raise-frame (function)
374 "Advise the FUNCTION not to raise frames, even if it wants to."
375 `(defadvice ,function
376 (around mdw-inhibit-raise (&rest hunoz) activate compile)
377 "Don't raise the window unless you have to."
378 (let ((mdw-inhibit-raise-frame t))
379 ad-do-it)))
380
381 (mdw-advise-to-inhibit-raise-frame select-frame-set-input-focus)
382 (mdw-advise-to-inhibit-raise-frame appt-disp-window)
383 (mdw-advise-to-inhibit-raise-frame mouse-select-window)
384
385 ;; Bug fix for markdown-mode, which breaks point positioning during
386 ;; `query-replace'.
387 (defadvice markdown-check-change-for-wiki-link
388 (around mdw-save-match activate compile)
389 "Save match data around the `markdown-mode' `after-change-functions' hook."
390 (save-match-data ad-do-it))
391
392 ;; Bug fix for `bbdb-canonicalize-address': on Emacs 24, `run-hook-with-args'
393 ;; always returns nil, with the result that all email addresses are lost.
394 ;; Replace the function entirely.
395 (defadvice bbdb-canonicalize-address
396 (around mdw-bug-fix activate compile)
397 "Don't use `run-hook-with-args', because that doesn't work."
398 (let ((net (ad-get-arg 0)))
399
400 ;; Make sure this is a proper hook list.
401 (if (functionp bbdb-canonicalize-net-hook)
402 (setq bbdb-canonicalize-net-hook (list bbdb-canonicalize-net-hook)))
403
404 ;; Iterate over the hooks until things converge.
405 (let ((donep nil))
406 (while (not donep)
407 (let (next (changep nil)
408 hook (hooks bbdb-canonicalize-net-hook))
409 (while hooks
410 (setq hook (pop hooks))
411 (setq next (funcall hook net))
412 (if (not (equal next net))
413 (setq changep t
414 net next)))
415 (setq donep (not changep)))))
416 (setq ad-return-value net)))
417
418 ;; Transient mark mode hacks.
419
420 (defadvice exchange-point-and-mark
421 (around mdw-highlight (&optional arg) activate compile)
422 "Maybe don't actually exchange point and mark.
423 If `transient-mark-mode' is on and the mark is inactive, then
424 just activate it. A non-trivial prefix argument will force the
425 usual behaviour. A trivial prefix argument (i.e., just C-u) will
426 activate the mark and temporarily enable `transient-mark-mode' if
427 it's currently off."
428 (cond ((or mark-active
429 (and (not transient-mark-mode) (not arg))
430 (and arg (or (not (consp arg))
431 (not (= (car arg) 4)))))
432 ad-do-it)
433 (t
434 (or transient-mark-mode (setq transient-mark-mode 'only))
435 (set-mark (mark t)))))
436
437 ;; Functions for sexp diary entries.
438
439 (defvar mdw-diary-for-org-mode-p nil
440 "Display diary along with the agenda?")
441
442 (defun mdw-not-org-mode (form)
443 "As FORM, but not in Org mode agenda."
444 (and (not mdw-diary-for-org-mode-p)
445 (eval form)))
446
447 (defun mdw-weekday (l)
448 "Return non-nil if `date' falls on one of the days of the week in L.
449 L is a list of day numbers (from 0 to 6 for Sunday through to
450 Saturday) or symbols `sunday', `monday', etc. (or a mixture). If
451 the date stored in `date' falls on a listed day, then the
452 function returns non-nil."
453 (let ((d (calendar-day-of-week date)))
454 (or (memq d l)
455 (memq (nth d '(sunday monday tuesday wednesday
456 thursday friday saturday)) l))))
457
458 (defun mdw-discordian-date (date)
459 "Return the Discordian calendar date corresponding to DATE.
460
461 The return value is (YOLD . st-tibs-day) or (YOLD SEASON DAYNUM DOW).
462
463 The original is by David Pearson. I modified it to produce date components
464 as output rather than a string."
465 (let* ((days ["Sweetmorn" "Boomtime" "Pungenday"
466 "Prickle-Prickle" "Setting Orange"])
467 (months ["Chaos" "Discord" "Confusion"
468 "Bureaucracy" "Aftermath"])
469 (day-count [0 31 59 90 120 151 181 212 243 273 304 334])
470 (year (- (calendar-extract-year date) 1900))
471 (month (1- (calendar-extract-month date)))
472 (day (1- (calendar-extract-day date)))
473 (julian (+ (aref day-count month) day))
474 (dyear (+ year 3066)))
475 (if (and (= month 1) (= day 28))
476 (cons dyear 'st-tibs-day)
477 (list dyear
478 (aref months (floor (/ julian 73)))
479 (1+ (mod julian 73))
480 (aref days (mod julian 5))))))
481
482 (defun mdw-diary-discordian-date ()
483 "Convert the date in `date' to a string giving the Discordian date."
484 (let* ((ddate (mdw-discordian-date date))
485 (tail (format "in the YOLD %d" (car ddate))))
486 (if (eq (cdr ddate) 'st-tibs-day)
487 (format "St Tib's Day %s" tail)
488 (let ((season (cadr ddate))
489 (daynum (caddr ddate))
490 (dayname (cadddr ddate)))
491 (format "%s, the %d%s day of %s %s"
492 dayname
493 daynum
494 (let ((ldig (mod daynum 10)))
495 (cond ((= ldig 1) "st")
496 ((= ldig 2) "nd")
497 ((= ldig 3) "rd")
498 (t "th")))
499 season
500 tail)))))
501
502 (defun mdw-todo (&optional when)
503 "Return non-nil today, or on WHEN, whichever is later."
504 (let ((w (calendar-absolute-from-gregorian (calendar-current-date)))
505 (d (calendar-absolute-from-gregorian date)))
506 (if when
507 (setq w (max w (calendar-absolute-from-gregorian
508 (cond
509 ((not european-calendar-style)
510 when)
511 ((> (car when) 100)
512 (list (nth 1 when)
513 (nth 2 when)
514 (nth 0 when)))
515 (t
516 (list (nth 1 when)
517 (nth 0 when)
518 (nth 2 when))))))))
519 (eq w d)))
520
521 (defadvice org-agenda-list (around mdw-preserve-links activate)
522 (let ((mdw-diary-for-org-mode-p t))
523 ad-do-it))
524
525 (defcustom diary-time-regexp nil
526 "Regexp matching times in the diary buffer."
527 :type 'regexp)
528
529 (defadvice diary-add-to-list (before mdw-trim-leading-space compile activate)
530 "Trim leading space from the diary entry string."
531 (save-match-data
532 (let ((str (ad-get-arg 1))
533 (done nil) old)
534 (while (not done)
535 (setq old str)
536 (setq str (cond ((null str) nil)
537 ((string-match "\\(^\\|\n\\)[ \t]+" str)
538 (replace-match "\\1" nil nil str))
539 ((and mdw-diary-for-org-mode-p
540 (string-match (concat
541 "\\(^\\|\n\\)"
542 "\\(" diary-time-regexp
543 "\\(-" diary-time-regexp "\\)?"
544 "\\)"
545 "\\(\t[ \t]*\\| [ \t]+\\)")
546 str))
547 (replace-match "\\1\\2 " nil nil str))
548 ((and (not mdw-diary-for-org-mode-p)
549 (string-match "\\[\\[[^][]*]\\[\\([^][]*\\)]]"
550 str))
551 (replace-match "\\1" nil nil str))
552 (t str)))
553 (if (equal str old) (setq done t)))
554 (ad-set-arg 1 str))))
555
556 (defadvice org-bbdb-anniversaries (after mdw-fixup-list compile activate)
557 "Return a string rather than a list."
558 (with-temp-buffer
559 (let ((anyp nil))
560 (dolist (e (let ((ee ad-return-value))
561 (if (atom ee) (list ee) ee)))
562 (when e
563 (when anyp (insert ?\n))
564 (insert e)
565 (setq anyp t)))
566 (setq ad-return-value
567 (and anyp (buffer-string))))))
568
569 ;; Fighting with Org-mode's evil key maps.
570
571 (defcustom mdw-evil-keymap-keys
572 '(([S-up] . [?\C-c up])
573 ([S-down] . [?\C-c down])
574 ([S-left] . [?\C-c left])
575 ([S-right] . [?\C-c right])
576 (([M-up] [?\e up]) . [C-up])
577 (([M-down] [?\e down]) . [C-down])
578 (([M-left] [?\e left]) . [C-left])
579 (([M-right] [?\e right]) . [C-right]))
580 "Defines evil keybindings to clobber in `mdw-clobber-evil-keymap'.
581 The value is an alist mapping evil keys (as a list, or singleton)
582 to good keys (in the same form)."
583 :type '(alist :key-type (choice key-sequence (repeat key-sequence))
584 :value-type key-sequence))
585
586 (defun mdw-clobber-evil-keymap (keymap)
587 "Replace evil key bindings in the KEYMAP.
588 Evil key bindings are defined in `mdw-evil-keymap-keys'."
589 (dolist (entry mdw-evil-keymap-keys)
590 (let ((binding nil)
591 (keys (if (listp (car entry))
592 (car entry)
593 (list (car entry))))
594 (replacements (if (listp (cdr entry))
595 (cdr entry)
596 (list (cdr entry)))))
597 (catch 'found
598 (dolist (key keys)
599 (setq binding (lookup-key keymap key))
600 (when binding
601 (throw 'found nil))))
602 (when binding
603 (dolist (key keys)
604 (define-key keymap key nil))
605 (dolist (key replacements)
606 (define-key keymap key binding))))))
607
608 (defcustom mdw-org-latex-defs
609 '(("strayman"
610 "\\documentclass{strayman}
611 \\usepackage[utf8]{inputenc}
612 \\usepackage[palatino, helvetica, courier, maths=cmr]{mdwfonts}
613 \\usepackage{graphicx, tikz, mdwtab, mdwmath, crypto, longtable}"
614 ("\\section{%s}" . "\\section*{%s}")
615 ("\\subsection{%s}" . "\\subsection*{%s}")
616 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
617 ("\\paragraph{%s}" . "\\paragraph*{%s}")
618 ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
619 "Additional LaTeX class definitions."
620 :type '(alist :key-type string
621 :value-type (list string
622 (alist :inline t
623 :key-type string
624 :value-type string))))
625
626 (eval-after-load "org-latex"
627 '(setq org-export-latex-classes
628 (append mdw-org-latex-defs org-export-latex-classes)))
629
630 (eval-after-load "ox-latex"
631 '(setq org-latex-classes (append mdw-org-latex-defs org-latex-classes)
632 org-latex-caption-above nil
633 org-latex-default-packages-alist '(("AUTO" "inputenc" t)
634 ("T1" "fontenc" t)
635 ("" "fixltx2e" nil)
636 ("" "graphicx" t)
637 ("" "longtable" nil)
638 ("" "float" nil)
639 ("" "wrapfig" nil)
640 ("" "rotating" nil)
641 ("normalem" "ulem" t)
642 ("" "textcomp" t)
643 ("" "marvosym" t)
644 ("" "wasysym" t)
645 ("" "amssymb" t)
646 ("" "hyperref" nil)
647 "\\tolerance=1000")))
648
649
650 (setq org-export-docbook-xslt-proc-command "xsltproc --output %o %s %i"
651 org-export-docbook-xsl-fo-proc-command "fop %i.safe %o"
652 org-export-docbook-xslt-stylesheet
653 "/usr/share/xml/docbook/stylesheet/docbook-xsl/fo/docbook.xsl")
654
655 ;; Glasses.
656
657 (setq glasses-separator "-"
658 glasses-separate-parentheses-p nil
659 glasses-uncapitalize-p t)
660
661 ;; Some hacks to do with window placement.
662
663 (defvar mdw-designated-window nil
664 "The window chosen by `mdw-designate-window', or nil.")
665
666 (defun mdw-designated-window-display-buffer-function (buffer not-this-window)
667 "Display buffer function to use the designated window."
668 (unless mdw-designated-window (error "No designated window!"))
669 (prog1 mdw-designated-window
670 (with-selected-window mdw-designated-window (switch-to-buffer buffer))
671 (setq mdw-designated-window nil
672 display-buffer-function nil)))
673
674 (defun mdw-display-buffer-in-designated-window (buffer alist)
675 "Display function to use the designated window."
676 (prog1 mdw-designated-window
677 (when mdw-designated-window
678 (with-selected-window mdw-designated-window
679 (switch-to-buffer buffer nil t)))
680 (setq mdw-designated-window nil)))
681
682 (defun mdw-designate-window (cancel)
683 "Use the selected window for the next pop-up buffer.
684 With a prefix argument, clear the designated window."
685 (interactive "P")
686 (let ((window (selected-window)))
687 (cond (cancel
688 (setq mdw-designated-window nil)
689 (unless (mdw-emacs-version-p 24)
690 (setq display-buffer-function nil))
691 (message "Window designation cleared."))
692 ((window-dedicated-p window)
693 (error "Window is dedicated to its buffer."))
694 (t
695 (setq mdw-designated-window window)
696 (unless (mdw-emacs-version-p 24)
697 (setq display-buffer-function
698 #'mdw-designated-window-display-buffer-function))
699 (message "Window designated.")))))
700
701 (when (mdw-emacs-version-p 24)
702 (setq display-buffer-base-action
703 (let* ((action display-buffer-base-action)
704 (funcs (car action))
705 (alist (cdr action)))
706 (cons (cons 'mdw-display-buffer-in-designated-window funcs)
707 alist))))
708
709 (defun mdw-clobber-other-windows-showing-buffer (buffer-or-name)
710 "Arrange that no windows on other frames are showing BUFFER-OR-NAME."
711 (interactive "bBuffer: ")
712 (let ((home-frame (selected-frame))
713 (buffer (get-buffer buffer-or-name))
714 (safe-buffer (get-buffer "*scratch*")))
715 (dolist (frame (frame-list))
716 (unless (eq frame home-frame)
717 (dolist (window (window-list frame))
718 (when (eq (window-buffer window) buffer)
719 (set-window-buffer window safe-buffer)))))))
720
721 (defvar mdw-inhibit-walk-windows nil
722 "If non-nil, then `walk-windows' does nothing.
723 This is used by advice on `switch-to-buffer-other-frame' to inhibit finding
724 buffers in random frames.")
725
726 (setq display-buffer--other-frame-action
727 '((display-buffer-reuse-window display-buffer-pop-up-frame)
728 (reusable-frames . nil)
729 (inhibit-same-window . t)))
730
731 (defadvice walk-windows (around mdw-inhibit activate)
732 "If `mdw-inhibit-walk-windows' is non-nil, then do nothing."
733 (and (not mdw-inhibit-walk-windows)
734 ad-do-it))
735
736 (defadvice switch-to-buffer-other-frame
737 (around mdw-always-new-frame activate)
738 "Always make a new frame.
739 Even if an existing window in some random frame looks tempting."
740 (let ((mdw-inhibit-walk-windows t)) ad-do-it))
741
742 (defadvice display-buffer (before mdw-inhibit-other-frames activate)
743 "Don't try to do anything fancy with other frames.
744 Pretend they don't exist. They might be on other display devices."
745 (ad-set-arg 2 nil))
746
747 (setq even-window-sizes nil
748 even-window-heights nil)
749
750 ;; Rename buffers along with files.
751
752 (defvar mdw-inhibit-rename-buffer nil
753 "If non-nil, `rename-file' won't rename the buffer visiting the file.")
754
755 (defmacro mdw-advise-to-inhibit-rename-buffer (function)
756 "Advise FUNCTION to set `mdw-inhibit-rename-buffer' while it runs.
757
758 This will prevent `rename-file' from renaming the buffer."
759 `(defadvice ,function (around mdw-inhibit-rename-buffer compile activate)
760 "Don't rename the buffer when renaming the underlying file."
761 (let ((mdw-inhibit-rename-buffer t))
762 ad-do-it)))
763 (mdw-advise-to-inhibit-rename-buffer recode-file-name)
764 (mdw-advise-to-inhibit-rename-buffer set-visited-file-name)
765 (mdw-advise-to-inhibit-rename-buffer backup-buffer)
766
767 (defadvice rename-file (after mdw-rename-buffers (from to &optional forcep)
768 compile activate)
769 "If a buffer is visiting the file, rename it to match the new name.
770
771 Don't do this if `mdw-inhibit-rename-buffer' is non-nil."
772 (unless mdw-inhibit-rename-buffer
773 (let ((buffer (get-file-buffer from)))
774 (when buffer
775 (let ((to (if (not (string= (file-name-nondirectory to) "")) to
776 (concat to (file-name-nondirectory from)))))
777 (with-current-buffer buffer
778 (set-visited-file-name to nil t)))))))
779
780 ;;;--------------------------------------------------------------------------
781 ;;; Improved compilation machinery.
782
783 ;; Uprated version of M-x compile.
784
785 (setq compile-command
786 (let ((ncpu (with-temp-buffer
787 (insert-file-contents "/proc/cpuinfo")
788 (buffer-string)
789 (count-matches "^processor\\s-*:"))))
790 (format "nice make -j%d -k" (* 2 ncpu))))
791
792 (defun mdw-compilation-buffer-name (mode)
793 (concat "*" (downcase mode) ": "
794 (abbreviate-file-name default-directory) "*"))
795 (setq compilation-buffer-name-function 'mdw-compilation-buffer-name)
796
797 (eval-after-load "compile"
798 '(progn
799 (define-key compilation-shell-minor-mode-map "\C-c\M-g" 'recompile)))
800
801 (defadvice compile (around hack-environment compile activate)
802 "Hack the environment inherited by inferiors in the compilation."
803 (let ((process-environment (copy-tree process-environment)))
804 (setenv "LD_PRELOAD" nil)
805 ad-do-it))
806
807 (defun mdw-compile (command &optional directory comint)
808 "Initiate a compilation COMMAND, maybe in a different DIRECTORY.
809 The DIRECTORY may be nil to not change. If COMINT is t, then
810 start an interactive compilation.
811
812 Interactively, prompt for the command if the variable
813 `compilation-read-command' is non-nil, or if requested through
814 the prefix argument. Prompt for the directory, and run
815 interactively, if requested through the prefix.
816
817 Use a prefix of 4, 6, 12, or 14, or type C-u between one and three times, to
818 force prompting for a directory.
819
820 Use a prefix of 2, 6, 10, or 14, or type C-u three times, to force
821 prompting for the command.
822
823 Use a prefix of 8, 10, 12, or 14, or type C-u twice or three times,
824 to force interactive compilation."
825 (interactive
826 (let* ((prefix (prefix-numeric-value current-prefix-arg))
827 (command (eval compile-command))
828 (dir (and (plusp (logand prefix #x54))
829 (read-directory-name "Compile in directory: "))))
830 (list (if (or compilation-read-command
831 (plusp (logand prefix #x42)))
832 (compilation-read-command command)
833 command)
834 dir
835 (plusp (logand prefix #x58)))))
836 (let ((default-directory (or directory default-directory)))
837 (compile command comint)))
838
839 ;; Flymake support.
840
841 (defun mdw-find-build-dir (build-file)
842 (catch 'found
843 (let* ((src-dir (file-name-as-directory (expand-file-name ".")))
844 (dir src-dir))
845 (loop
846 (when (file-exists-p (concat dir build-file))
847 (throw 'found dir))
848 (let ((sub (expand-file-name (file-relative-name src-dir dir)
849 (concat dir "build/"))))
850 (catch 'give-up
851 (loop
852 (when (file-exists-p (concat sub build-file))
853 (throw 'found sub))
854 (when (string= sub dir) (throw 'give-up nil))
855 (setq sub (file-name-directory (directory-file-name sub))))))
856 (when (string= dir
857 (setq dir (file-name-directory
858 (directory-file-name dir))))
859 (throw 'found nil))))))
860
861 (defun mdw-flymake-make-init ()
862 (let ((build-dir (mdw-find-build-dir "Makefile")))
863 (and build-dir
864 (let ((tmp-src (flymake-init-create-temp-buffer-copy
865 #'flymake-create-temp-inplace)))
866 (flymake-get-syntax-check-program-args
867 tmp-src build-dir t t
868 #'flymake-get-make-cmdline)))))
869
870 (setq flymake-allowed-file-name-masks
871 '(("\\.\\(?:[cC]\\|cc\\|cpp\\|cxx\\|c\\+\\+\\)\\'"
872 mdw-flymake-make-init)
873 ("\\.\\(?:[hH]\\|hh\\|hpp\\|hxx\\|h\\+\\+\\)\\'"
874 mdw-flymake-master-make-init)
875 ("\\.p[lm]" flymake-perl-init)))
876
877 (setq flymake-mode-map
878 (let ((map (if (boundp 'flymake-mode-map)
879 flymake-mode-map
880 (make-sparse-keymap))))
881 (define-key map [?\C-c ?\C-f ?\C-p] 'flymake-goto-prev-error)
882 (define-key map [?\C-c ?\C-f ?\C-n] 'flymake-goto-next-error)
883 (define-key map [?\C-c ?\C-f ?\C-c] 'flymake-compile)
884 (define-key map [?\C-c ?\C-f ?\C-k] 'flymake-stop-all-syntax-checks)
885 (define-key map [?\C-c ?\C-f ?\C-e] 'flymake-popup-current-error-menu)
886 map))
887
888 ;;;--------------------------------------------------------------------------
889 ;;; Mail and news hacking.
890
891 (define-derived-mode mdwmail-mode mail-mode "[mdw] mail"
892 "Major mode for editing news and mail messages from external programs.
893 Not much right now. Just support for doing MailCrypt stuff."
894 :syntax-table nil
895 :abbrev-table nil
896 (run-hooks 'mail-setup-hook))
897
898 (define-key mdwmail-mode-map [?\C-c ?\C-c] 'disabled-operation)
899
900 (add-hook 'mdwail-mode-hook
901 (lambda ()
902 (set-buffer-file-coding-system 'utf-8)
903 (make-local-variable 'paragraph-separate)
904 (make-local-variable 'paragraph-start)
905 (setq paragraph-start
906 (concat "[ \t]*[-_][-_][-_]+$\\|^-- \\|-----\\|"
907 paragraph-start))
908 (setq paragraph-separate
909 (concat "[ \t]*[-_][-_][-_]+$\\|^-- \\|-----\\|"
910 paragraph-separate))))
911
912 ;; How to encrypt in mdwmail.
913
914 (defun mdwmail-mc-encrypt (&optional recip scm start end from sign)
915 (or start
916 (setq start (save-excursion
917 (goto-char (point-min))
918 (or (search-forward "\n\n" nil t) (point-min)))))
919 (or end
920 (setq end (point-max)))
921 (mc-encrypt-generic recip scm start end from sign))
922
923 ;; How to sign in mdwmail.
924
925 (defun mdwmail-mc-sign (key scm start end uclr)
926 (or start
927 (setq start (save-excursion
928 (goto-char (point-min))
929 (or (search-forward "\n\n" nil t) (point-min)))))
930 (or end
931 (setq end (point-max)))
932 (mc-sign-generic key scm start end uclr))
933
934 ;; Some signature mangling.
935
936 (defun mdwmail-mangle-signature ()
937 (save-excursion
938 (goto-char (point-min))
939 (perform-replace "\n-- \n" "\n-- " nil nil nil)))
940 (add-hook 'mail-setup-hook 'mdwmail-mangle-signature)
941 (add-hook 'message-setup-hook 'mdwmail-mangle-signature)
942
943 ;; Insert my login name into message-ids, so I can score replies.
944
945 (defadvice message-unique-id (after mdw-user-name last activate compile)
946 "Ensure that the user's name appears at the end of the message-id string,
947 so that it can be used for convenient filtering."
948 (setq ad-return-value (concat ad-return-value "." (user-login-name))))
949
950 ;; Tell my movemail hack where movemail is.
951 ;;
952 ;; This is needed to shup up warnings about LD_PRELOAD.
953
954 (let ((path exec-path))
955 (while path
956 (let ((try (expand-file-name "movemail" (car path))))
957 (if (file-executable-p try)
958 (setenv "REAL_MOVEMAIL" try))
959 (setq path (cdr path)))))
960
961 ;; AUTHINFO GENERIC kludge.
962
963 (defcustom nntp-authinfo-generic nil
964 "Set to the `NNTPAUTH' string to pass on to `authinfo-kludge'.
965
966 Use this to arrange for per-server settings."
967 :type '(choice (const :tag "Use `NNTPAUTH' environment variable" nil)
968 string)
969 :safe 'stringp)
970
971 (defun nntp-open-authinfo-kludge (buffer)
972 "Open a connection to SERVER using `authinfo-kludge'."
973 (let ((proc (start-process "nntpd" buffer
974 "env" (concat "NNTPAUTH="
975 (or nntp-authinfo-generic
976 (getenv "NNTPAUTH")
977 (error "NNTPAUTH unset")))
978 "authinfo-kludge" nntp-address)))
979 (set-buffer buffer)
980 (nntp-wait-for-string "^\r*200")
981 (beginning-of-line)
982 (delete-region (point-min) (point))
983 proc))
984
985 (eval-after-load "erc"
986 '(load "~/.ercrc.el"))
987
988 ;; Heavy-duty Gnus patching.
989
990 (defun mdw-nnimap-transform-headers ()
991 (goto-char (point-min))
992 (let (article lines size string)
993 (block nil
994 (while (not (eobp))
995 (while (not (looking-at "\\* [0-9]+ FETCH"))
996 (delete-region (point) (progn (forward-line 1) (point)))
997 (when (eobp)
998 (return)))
999 (goto-char (match-end 0))
1000 ;; Unfold quoted {number} strings.
1001 (while (re-search-forward
1002 "[^]][ (]{\\([0-9]+\\)}\r?\n"
1003 (save-excursion
1004 ;; Start of the header section.
1005 (or (re-search-forward "] {[0-9]+}\r?\n" nil t)
1006 ;; Start of the next FETCH.
1007 (re-search-forward "\\* [0-9]+ FETCH" nil t)
1008 (point-max)))
1009 t)
1010 (setq size (string-to-number (match-string 1)))
1011 (delete-region (+ (match-beginning 0) 2) (point))
1012 (setq string (buffer-substring (point) (+ (point) size)))
1013 (delete-region (point) (+ (point) size))
1014 (insert (format "%S" (subst-char-in-string ?\n ?\s string)))
1015 ;; [mdw] missing from upstream
1016 (backward-char 1))
1017 (beginning-of-line)
1018 (setq article
1019 (and (re-search-forward "UID \\([0-9]+\\)"
1020 (line-end-position)
1021 t)
1022 (match-string 1)))
1023 (setq lines nil)
1024 (setq size
1025 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
1026 (line-end-position)
1027 t)
1028 (match-string 1)))
1029 (beginning-of-line)
1030 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
1031 (let ((structure (ignore-errors
1032 (read (current-buffer)))))
1033 (while (and (consp structure)
1034 (not (atom (car structure))))
1035 (setq structure (car structure)))
1036 (setq lines (if (and
1037 (stringp (car structure))
1038 (equal (upcase (nth 0 structure)) "MESSAGE")
1039 (equal (upcase (nth 1 structure)) "RFC822"))
1040 (nth 9 structure)
1041 (nth 7 structure)))))
1042 (delete-region (line-beginning-position) (line-end-position))
1043 (insert (format "211 %s Article retrieved." article))
1044 (forward-line 1)
1045 (when size
1046 (insert (format "Chars: %s\n" size)))
1047 (when lines
1048 (insert (format "Lines: %s\n" lines)))
1049 ;; Most servers have a blank line after the headers, but
1050 ;; Davmail doesn't.
1051 (unless (re-search-forward "^\r$\\|^)\r?$" nil t)
1052 (goto-char (point-max)))
1053 (delete-region (line-beginning-position) (line-end-position))
1054 (insert ".")
1055 (forward-line 1)))))
1056
1057 (eval-after-load 'nnimap
1058 '(defalias 'nnimap-transform-headers
1059 (symbol-function 'mdw-nnimap-transform-headers)))
1060
1061 (defadvice gnus-other-frame (around mdw-hack-frame-width compile activate)
1062 "Always arrange for mail/news frames to be 80 columns wide."
1063 (let ((default-frame-alist (cons `(width . ,(+ 80 mdw-frame-width-fudge))
1064 (delete* 'width default-frame-alist
1065 :key #'car))))
1066 ad-do-it))
1067
1068 ;; Preferred programs.
1069
1070 (setq mailcap-user-mime-data
1071 '(((type . "application/pdf") (viewer . "mupdf %s"))))
1072
1073 ;;;--------------------------------------------------------------------------
1074 ;;; Utility functions.
1075
1076 (or (fboundp 'line-number-at-pos)
1077 (defun line-number-at-pos (&optional pos)
1078 (let ((opoint (or pos (point))) start)
1079 (save-excursion
1080 (save-restriction
1081 (goto-char (point-min))
1082 (widen)
1083 (forward-line 0)
1084 (setq start (point))
1085 (goto-char opoint)
1086 (forward-line 0)
1087 (1+ (count-lines 1 (point))))))))
1088
1089 (defun mdw-uniquify-alist (&rest alists)
1090 "Return the concatenation of the ALISTS with duplicate elements removed.
1091 The first association with a given key prevails; others are
1092 ignored. The input lists are not modified, although they'll
1093 probably become garbage."
1094 (and alists
1095 (let ((start-list (cons nil nil)))
1096 (mdw-do-uniquify start-list
1097 start-list
1098 (car alists)
1099 (cdr alists)))))
1100
1101 (defun mdw-do-uniquify (done end l rest)
1102 "A helper function for mdw-uniquify-alist.
1103 The DONE argument is a list whose first element is `nil'. It
1104 contains the uniquified alist built so far. The leading `nil' is
1105 stripped off at the end of the operation; it's only there so that
1106 DONE always references a cons cell. END refers to the final cons
1107 cell in the DONE list; it is modified in place each time to avoid
1108 the overheads of `append'ing all the time. The L argument is the
1109 alist we're currently processing; the remaining alists are given
1110 in REST."
1111
1112 ;; There are several different cases to deal with here.
1113 (cond
1114
1115 ;; Current list isn't empty. Add the first item to the DONE list if
1116 ;; there's not an item with the same KEY already there.
1117 (l (or (assoc (car (car l)) done)
1118 (progn
1119 (setcdr end (cons (car l) nil))
1120 (setq end (cdr end))))
1121 (mdw-do-uniquify done end (cdr l) rest))
1122
1123 ;; The list we were working on is empty. Shunt the next list into the
1124 ;; current list position and go round again.
1125 (rest (mdw-do-uniquify done end (car rest) (cdr rest)))
1126
1127 ;; Everything's done. Remove the leading `nil' from the DONE list and
1128 ;; return it. Finished!
1129 (t (cdr done))))
1130
1131 (defun date ()
1132 "Insert the current date in a pleasing way."
1133 (interactive)
1134 (insert (save-excursion
1135 (let ((buffer (get-buffer-create "*tmp*")))
1136 (unwind-protect (progn (set-buffer buffer)
1137 (erase-buffer)
1138 (shell-command "date +%Y-%m-%d" t)
1139 (goto-char (mark))
1140 (delete-char -1)
1141 (buffer-string))
1142 (kill-buffer buffer))))))
1143
1144 (defun uuencode (file &optional name)
1145 "UUencodes a file, maybe calling it NAME, into the current buffer."
1146 (interactive "fInput file name: ")
1147
1148 ;; If NAME isn't specified, then guess from the filename.
1149 (if (not name)
1150 (setq name
1151 (substring file
1152 (or (string-match "[^/]*$" file) 0))))
1153 (print (format "uuencode `%s' `%s'" file name))
1154
1155 ;; Now actually do the thing.
1156 (call-process "uuencode" file t nil name))
1157
1158 (defcustom np-file "~/.np"
1159 "Where the `now-playing' file is."
1160 :type 'file
1161 :safe 'stringp)
1162
1163 (defun np (&optional arg)
1164 "Grabs a `now-playing' string."
1165 (interactive)
1166 (save-excursion
1167 (or arg (progn
1168 (goto-char (point-max))
1169 (insert "\nNP: ")
1170 (insert-file-contents np-file)))))
1171
1172 (defun mdw-version-< (ver-a ver-b)
1173 "Answer whether VER-A is strictly earlier than VER-B.
1174 VER-A and VER-B are version numbers, which are strings containing digit
1175 sequences separated by `.'."
1176 (let* ((la (mapcar (lambda (x) (car (read-from-string x)))
1177 (split-string ver-a "\\.")))
1178 (lb (mapcar (lambda (x) (car (read-from-string x)))
1179 (split-string ver-b "\\."))))
1180 (catch 'done
1181 (while t
1182 (cond ((null la) (throw 'done lb))
1183 ((null lb) (throw 'done nil))
1184 ((< (car la) (car lb)) (throw 'done t))
1185 ((= (car la) (car lb)) (setq la (cdr la) lb (cdr lb)))
1186 (t (throw 'done nil)))))))
1187
1188 (defun mdw-check-autorevert ()
1189 "Sets global-auto-revert-ignore-buffer appropriately for this buffer.
1190 This takes into consideration whether it's been found using
1191 tramp, which seems to get itself into a twist."
1192 (cond ((not (boundp 'global-auto-revert-ignore-buffer))
1193 nil)
1194 ((and (buffer-file-name)
1195 (fboundp 'tramp-tramp-file-p)
1196 (tramp-tramp-file-p (buffer-file-name)))
1197 (unless global-auto-revert-ignore-buffer
1198 (setq global-auto-revert-ignore-buffer 'tramp)))
1199 ((eq global-auto-revert-ignore-buffer 'tramp)
1200 (setq global-auto-revert-ignore-buffer nil))))
1201
1202 (defadvice find-file (after mdw-autorevert activate)
1203 (mdw-check-autorevert))
1204 (defadvice write-file (after mdw-autorevert activate)
1205 (mdw-check-autorevert))
1206
1207 (defun mdw-auto-revert ()
1208 "Recheck all of the autorevertable buffers, and update VC modelines."
1209 (interactive)
1210 (let ((auto-revert-check-vc-info t))
1211 (auto-revert-buffers)))
1212
1213 ;;;--------------------------------------------------------------------------
1214 ;;; Dired hacking.
1215
1216 (defadvice dired-maybe-insert-subdir
1217 (around mdw-marked-insertion first activate)
1218 "The DIRNAME may be a list of directory names to insert.
1219 Interactively, if files are marked, then insert all of them.
1220 With a numeric prefix argument, select that many entries near
1221 point; with a non-numeric prefix argument, prompt for listing
1222 options."
1223 (interactive
1224 (list (dired-get-marked-files nil
1225 (and (integerp current-prefix-arg)
1226 current-prefix-arg)
1227 #'file-directory-p)
1228 (and current-prefix-arg
1229 (not (integerp current-prefix-arg))
1230 (read-string "Switches for listing: "
1231 (or dired-subdir-switches
1232 dired-actual-switches)))))
1233 (let ((dirs (ad-get-arg 0)))
1234 (dolist (dir (if (listp dirs) dirs (list dirs)))
1235 (ad-set-arg 0 dir)
1236 ad-do-it)))
1237
1238 (defun mdw-dired-run (args &optional syncp)
1239 (interactive (let ((file (dired-get-filename t)))
1240 (list (read-string (format "Arguments for %s: " file))
1241 current-prefix-arg)))
1242 (funcall (if syncp 'shell-command 'async-shell-command)
1243 (concat (shell-quote-argument (dired-get-filename nil))
1244 " " args)))
1245
1246 (defadvice dired-do-flagged-delete
1247 (around mdw-delete-if-prefix-argument activate compile)
1248 (let ((delete-by-moving-to-trash (and (null current-prefix-arg)
1249 delete-by-moving-to-trash)))
1250 ad-do-it))
1251
1252 (eval-after-load "dired"
1253 '(define-key dired-mode-map "X" 'mdw-dired-run))
1254
1255 ;;;--------------------------------------------------------------------------
1256 ;;; URL viewing.
1257
1258 (defun mdw-w3m-browse-url (url &optional new-session-p)
1259 "Invoke w3m on the URL in its current window, or at least a different one.
1260 If NEW-SESSION-P, start a new session."
1261 (interactive "sURL: \nP")
1262 (save-excursion
1263 (let ((window (selected-window)))
1264 (unwind-protect
1265 (progn
1266 (select-window (or (and (not new-session-p)
1267 (get-buffer-window "*w3m*"))
1268 (progn
1269 (if (one-window-p t) (split-window))
1270 (get-lru-window))))
1271 (w3m-browse-url url new-session-p))
1272 (select-window window)))))
1273
1274 (eval-after-load 'w3m
1275 '(define-key w3m-mode-map [?\e ?\r] 'w3m-view-this-url-new-session))
1276
1277 (defcustom mdw-good-url-browsers
1278 '(browse-url-mozilla
1279 browse-url-generic
1280 (w3m . mdw-w3m-browse-url)
1281 browse-url-w3)
1282 "List of good browsers for mdw-good-url-browsers.
1283 Each item is a browser function name, or a cons (CHECK . FUNC).
1284 A symbol FOO stands for (FOO . FOO)."
1285 :type '(repeat (choice function (cons function function))))
1286
1287 (defun mdw-good-url-browser ()
1288 "Return a good URL browser.
1289 Trundle the list of such things, finding the first item for which
1290 CHECK is fboundp, and returning the correponding FUNC."
1291 (let ((bs mdw-good-url-browsers) b check func answer)
1292 (while (and bs (not answer))
1293 (setq b (car bs)
1294 bs (cdr bs))
1295 (if (consp b)
1296 (setq check (car b) func (cdr b))
1297 (setq check b func b))
1298 (if (fboundp check)
1299 (setq answer func)))
1300 answer))
1301
1302 (eval-after-load "w3m-search"
1303 '(progn
1304 (dolist
1305 (item
1306 '(("g" "Google" "http://www.google.co.uk/search?q=%s")
1307 ("gd" "Google Directory"
1308 "http://www.google.com/search?cat=gwd/Top&q=%s")
1309 ("gg" "Google Groups" "http://groups.google.com/groups?q=%s")
1310 ("ward" "Ward's wiki" "http://c2.com/cgi/wiki?%s")
1311 ("gi" "Images" "http://images.google.com/images?q=%s")
1312 ("rfc" "RFC"
1313 "http://metalzone.distorted.org.uk/ftp/pub/mirrors/rfc/rfc%s.txt.gz")
1314 ("wp" "Wikipedia"
1315 "http://en.wikipedia.org/wiki/Special:Search?go=Go&search=%s")
1316 ("imdb" "IMDb" "http://www.imdb.com/Find?%s")
1317 ("nc-wiki" "nCipher wiki"
1318 "http://wiki.ncipher.com/wiki/bin/view/Devel/?topic=%s")
1319 ("map" "Google maps" "http://maps.google.co.uk/maps?q=%s&hl=en")
1320 ("lp" "Launchpad bug by number"
1321 "https://bugs.launchpad.net/bugs/%s")
1322 ("lppkg" "Launchpad bugs by package"
1323 "https://bugs.launchpad.net/%s")
1324 ("msdn" "MSDN"
1325 "http://social.msdn.microsoft.com/Search/en-GB/?query=%s&ac=8")
1326 ("debbug" "Debian bug by number"
1327 "http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=%s")
1328 ("debbugpkg" "Debian bugs by package"
1329 "http://bugs.debian.org/cgi-bin/pkgreport.cgi?pkg=%s")
1330 ("ljlogin" "LJ login" "http://www.livejournal.com/login.bml")))
1331 (add-to-list 'w3m-search-engine-alist
1332 (list (cadr item) (caddr item) nil))
1333 (add-to-list 'w3m-uri-replace-alist
1334 (list (concat "\\`" (car item) ":")
1335 'w3m-search-uri-replace
1336 (cadr item))))))
1337
1338 ;;;--------------------------------------------------------------------------
1339 ;;; Paragraph filling.
1340
1341 ;; Useful variables.
1342
1343 (defcustom mdw-fill-prefix nil
1344 "Used by `mdw-line-prefix' and `mdw-fill-paragraph'.
1345 If there's no fill prefix currently set (by the `fill-prefix'
1346 variable) and there's a match from one of the regexps here, it
1347 gets used to set the fill-prefix for the current operation.
1348
1349 The variable is a list of items of the form `PATTERN . PREFIX'; if
1350 the PATTERN matches, the PREFIX is used to set the fill prefix.
1351
1352 A PATTERN is one of the following.
1353
1354 * STRING -- a regular expression, expected to match at point
1355 * (eval . FORM) -- a Lisp form which must evaluate non-nil
1356 * (if COND CONSEQ-PAT ALT-PAT) -- if COND evaluates non-nil, must match
1357 CONSEQ-PAT; otherwise must match ALT-PAT
1358 * (and PATTERN ...) -- must match all of the PATTERNs
1359 * (or PATTERN ...) -- must match at least one PATTERN
1360 * (not PATTERN) -- mustn't match (probably not useful)
1361
1362 A PREFIX is a list of the following kinds of things:
1363
1364 * STRING -- insert a literal string
1365 * (match . N) -- insert the thing matched by bracketed subexpression N
1366 * (pad . N) -- a string of whitespace the same width as subexpression N
1367 * (expr . FORM) -- the result of evaluating FORM
1368
1369 Information about `bracketed subexpressions' comes from the match data,
1370 as modified during matching.")
1371
1372 (make-variable-buffer-local 'mdw-fill-prefix)
1373
1374 (defcustom mdw-hanging-indents
1375 (concat "\\(\\("
1376 "\\([*o+]\\|-[-#]?\\|[0-9]+\\.\\|\\[[0-9]+\\]\\|([a-zA-Z])\\)"
1377 "[ \t]+"
1378 "\\)?\\)")
1379 "Standard regexp matching parts of a hanging indent.
1380 This is mainly useful in `auto-fill-mode'."
1381 :type 'regexp)
1382
1383 ;; Utility functions.
1384
1385 (defun mdw-maybe-tabify (s)
1386 "Tabify or untabify the string S, according to `indent-tabs-mode'."
1387 (let ((tabfun (if indent-tabs-mode #'tabify #'untabify)))
1388 (with-temp-buffer
1389 (save-match-data
1390 (insert s "\n")
1391 (let ((start (point-min)) (end (point-max)))
1392 (funcall tabfun (point-min) (point-max))
1393 (setq s (buffer-substring (point-min) (1- (point-max)))))))))
1394
1395 (defun mdw-fill-prefix-match-p (pat)
1396 "Return non-nil if PAT matches at the current position."
1397 (cond ((stringp pat) (looking-at pat))
1398 ((not (consp pat)) (error "Unknown pattern item `%S'" pat))
1399 ((eq (car pat) 'eval) (eval (cdr pat)))
1400 ((eq (car pat) 'if)
1401 (if (or (null (cdr pat))
1402 (null (cddr pat))
1403 (null (cdddr pat))
1404 (cddddr pat))
1405 (error "Invalid `if' pattern `%S'" pat))
1406 (mdw-fill-prefix-match-p (if (eval (cadr pat))
1407 (caddr pat)
1408 (cadddr pat))))
1409 ((eq (car pat) 'and)
1410 (let ((pats (cdr pat))
1411 (ok t))
1412 (while (and pats
1413 (or (mdw-fill-prefix-match-p (car pats))
1414 (setq ok nil)))
1415 (setq pats (cdr pats)))
1416 ok))
1417 ((eq (car pat) 'or)
1418 (let ((pats (cdr pat))
1419 (ok nil))
1420 (while (and pats
1421 (or (not (mdw-fill-prefix-match-p (car pats)))
1422 (progn (setq ok t) nil)))
1423 (setq pats (cdr pats)))
1424 ok))
1425 ((eq (car pat) 'not)
1426 (if (or (null (cdr pat)) (cddr pat))
1427 (error "Invalid `not' pattern `%S'" pat))
1428 (not (mdw-fill-prefix-match-p (car pats))))
1429 (t (error "Unknown pattern form `%S'" pat))))
1430
1431 (defun mdw-maybe-car (p)
1432 "If P is a pair, return (car P), otherwise just return P."
1433 (if (consp p) (car p) p))
1434
1435 (defun mdw-padding (s)
1436 "Return a string the same width as S but made entirely from whitespace."
1437 (let* ((l (length s)) (i 0) (n (make-string l ? )))
1438 (while (< i l)
1439 (if (= 9 (aref s i))
1440 (aset n i 9))
1441 (setq i (1+ i)))
1442 n))
1443
1444 (defun mdw-do-prefix-match (m)
1445 "Expand a dynamic prefix match element.
1446 See `mdw-fill-prefix' for details."
1447 (cond ((not (consp m)) (format "%s" m))
1448 ((eq (car m) 'match) (match-string (mdw-maybe-car (cdr m))))
1449 ((eq (car m) 'pad) (mdw-padding (match-string
1450 (mdw-maybe-car (cdr m)))))
1451 ((eq (car m) 'eval) (eval (cdr m)))
1452 (t "")))
1453
1454 (defun mdw-examine-fill-prefixes (l)
1455 "Given a list of dynamic fill prefixes, pick one which matches
1456 context and return the static fill prefix to use. Point must be
1457 at the start of a line, and match data must be saved."
1458 (let ((prefix nil))
1459 (while (cond ((null l) nil)
1460 ((mdw-fill-prefix-match-p (caar l))
1461 (setq prefix
1462 (mdw-maybe-tabify
1463 (apply #'concat
1464 (mapcar #'mdw-do-prefix-match
1465 (cdr (car l))))))
1466 nil))
1467 (setq l (cdr l)))
1468 prefix))
1469
1470 (defun mdw-choose-dynamic-fill-prefix ()
1471 "Work out the dynamic fill prefix based on the variable `mdw-fill-prefix'."
1472 (cond ((and fill-prefix (not (string= fill-prefix ""))) fill-prefix)
1473 ((not mdw-fill-prefix) fill-prefix)
1474 (t (save-excursion
1475 (beginning-of-line)
1476 (save-match-data
1477 (mdw-examine-fill-prefixes mdw-fill-prefix))))))
1478
1479 (defadvice do-auto-fill (around mdw-dynamic-fill-prefix () activate compile)
1480 "Handle auto-filling, working out a dynamic fill prefix in the
1481 case where there isn't a sensible static one."
1482 (let ((fill-prefix (mdw-choose-dynamic-fill-prefix)))
1483 ad-do-it))
1484
1485 (defun mdw-fill-paragraph ()
1486 "Fill paragraph, getting a dynamic fill prefix."
1487 (interactive)
1488 (let ((fill-prefix (mdw-choose-dynamic-fill-prefix)))
1489 (fill-paragraph nil)))
1490
1491 (defun mdw-point-within-string-p ()
1492 "Return non-nil if point is within a string."
1493 (let ((state (syntax-ppss)))
1494 (elt state 3)))
1495
1496 (defun mdw-standard-fill-prefix (rx &optional mat)
1497 "Set the dynamic fill prefix, handling standard hanging indents and stuff.
1498 This is just a short-cut for setting the thing by hand, and by
1499 design it doesn't cope with anything approximating a complicated
1500 case."
1501 (setq mdw-fill-prefix
1502 `(((if (mdw-point-within-string-p)
1503 ,(concat "\\(\\s-*\\)" mdw-hanging-indents)
1504 ,(concat rx mdw-hanging-indents))
1505 (match . 1)
1506 (pad . ,(or mat 2))))))
1507
1508 ;;;--------------------------------------------------------------------------
1509 ;;; Printing.
1510
1511 ;; Teach PostScript about a condensed variant of Courier. I'm using 85% of
1512 ;; the usual width, which happens to match `mdwfonts', and David Carlisle's
1513 ;; `pslatex'. (Once upon a time, I used 80%, but decided consistency with
1514 ;; `pslatex' was useful.)
1515 (setq ps-user-defined-prologue "
1516 /CourierCondensed /Courier
1517 /CourierCondensed-Bold /Courier-Bold
1518 /CourierCondensed-Oblique /Courier-Oblique
1519 /CourierCondensed-BoldOblique /Courier-BoldOblique
1520 4 { findfont [0.85 0 0 1 0 0] makefont definefont pop } repeat
1521 ")
1522
1523 ;; Hack `ps-print''s settings.
1524 (eval-after-load 'ps-print
1525 '(progn
1526
1527 ;; Notice that the comment-delimiters should be in italics too.
1528 (pushnew 'font-lock-comment-delimiter-face ps-italic-faces)
1529
1530 ;; Select more suitable colours for the main kinds of tokens. The
1531 ;; colours set on the Emacs faces are chosen for use against a dark
1532 ;; background, and work very badly on white paper.
1533 (ps-extend-face '(font-lock-comment-face "darkgreen" nil italic))
1534 (ps-extend-face '(font-lock-comment-delimiter-face "darkgreen" nil italic))
1535 (ps-extend-face '(font-lock-string-face "RoyalBlue4" nil))
1536 (ps-extend-face '(mdw-punct-face "sienna" nil))
1537 (ps-extend-face '(mdw-number-face "OrangeRed3" nil))
1538
1539 ;; Teach `ps-print' about my condensed varsions of Courier.
1540 (setq ps-font-info-database
1541 (append '((CourierCondensed
1542 (fonts (normal . "CourierCondensed")
1543 (bold . "CourierCondensed-Bold")
1544 (italic . "CourierCondensed-Oblique")
1545 (bold-italic . "CourierCondensed-BoldOblique"))
1546 (size . 10.0)
1547 (line-height . 10.55)
1548 (space-width . 5.1)
1549 (avg-char-width . 5.1)))
1550 (remove* 'CourierCondensed ps-font-info-database
1551 :key #'car)))))
1552
1553 ;; Arrange to strip overlays from the buffer before we print . This will
1554 ;; prevent `flyspell' from interfering with the printout. (It would be less
1555 ;; bad if `ps-print' could merge the `flyspell' overlay face with the
1556 ;; underlying `font-lock' face, but it can't (and that seems hard). So
1557 ;; instead we have this hack.
1558 ;;
1559 ;; The basic trick is to copy the relevant text from the buffer being printed
1560 ;; into a temporary buffer and... just print that. The text properties come
1561 ;; with the text and end up in the new buffer, and the overlays get lost
1562 ;; along the way. Only problem is that the headers identifying the file
1563 ;; being printed get confused, so remember the original buffer and reinstate
1564 ;; it when constructing the headers.
1565 (defvar mdw-printing-buffer)
1566
1567 (defadvice ps-generate-header
1568 (around mdw-use-correct-buffer () activate compile)
1569 "Print the correct name of the buffer being printed."
1570 (with-current-buffer mdw-printing-buffer
1571 ad-do-it))
1572
1573 (defadvice ps-generate
1574 (around mdw-strip-overlays (buffer from to genfunc) activate compile)
1575 "Strip overlays -- in particular, from `flyspell' -- before printout."
1576 (with-temp-buffer
1577 (let ((mdw-printing-buffer buffer))
1578 (insert-buffer-substring buffer from to)
1579 (ad-set-arg 0 (current-buffer))
1580 (ad-set-arg 1 (point-min))
1581 (ad-set-arg 2 (point-max))
1582 ad-do-it)))
1583
1584 ;;;--------------------------------------------------------------------------
1585 ;;; Other common declarations.
1586
1587 ;; Common mode settings.
1588
1589 (defcustom mdw-auto-indent t
1590 "Whether to indent automatically after a newline."
1591 :type 'boolean
1592 :safe 'booleanp)
1593
1594 (defun mdw-whitespace-mode (&optional arg)
1595 "Turn on/off whitespace mode, but don't highlight trailing space."
1596 (interactive "P")
1597 (when (and (boundp 'whitespace-style)
1598 (fboundp 'whitespace-mode))
1599 (let ((whitespace-style (remove 'trailing whitespace-style)))
1600 (whitespace-mode arg))
1601 (setq show-trailing-whitespace whitespace-mode)))
1602
1603 (defvar mdw-do-misc-mode-hacking nil)
1604
1605 (defun mdw-misc-mode-config ()
1606 (and mdw-auto-indent
1607 (cond ((eq major-mode 'lisp-mode)
1608 (local-set-key "\C-m" 'mdw-indent-newline-and-indent))
1609 ((derived-mode-p 'slime-repl-mode 'asm-mode 'comint-mode)
1610 nil)
1611 (t
1612 (local-set-key "\C-m" 'newline-and-indent))))
1613 (set (make-local-variable 'mdw-do-misc-mode-hacking) t)
1614 (local-set-key [C-return] 'newline)
1615 (make-local-variable 'page-delimiter)
1616 (setq page-delimiter (concat "^" "\f"
1617 "\\|" "^"
1618 ".\\{0,4\\}"
1619 "-\\{5\\}"
1620 "\\(" " " ".*" " " "\\)?"
1621 "-+"
1622 ".\\{0,2\\}"
1623 "$"))
1624 (setq comment-column 40)
1625 (auto-fill-mode 1)
1626 (setq fill-column mdw-text-width)
1627 (flyspell-prog-mode)
1628 (and (fboundp 'gtags-mode)
1629 (gtags-mode))
1630 (if (fboundp 'hs-minor-mode)
1631 (trap (hs-minor-mode t))
1632 (outline-minor-mode t))
1633 (reveal-mode t)
1634 (trap (turn-on-font-lock)))
1635
1636 (defun mdw-post-local-vars-misc-mode-config ()
1637 (setq whitespace-line-column mdw-text-width)
1638 (when (and mdw-do-misc-mode-hacking
1639 (not buffer-read-only))
1640 (setq show-trailing-whitespace t)
1641 (mdw-whitespace-mode 1)))
1642 (add-hook 'hack-local-variables-hook 'mdw-post-local-vars-misc-mode-config)
1643
1644 (defmacro mdw-advise-update-angry-fruit-salad (&rest funcs)
1645 `(progn ,@(mapcar (lambda (func)
1646 `(defadvice ,func
1647 (after mdw-angry-fruit-salad activate)
1648 (when mdw-do-misc-mode-hacking
1649 (setq show-trailing-whitespace
1650 (not buffer-read-only))
1651 (mdw-whitespace-mode (if buffer-read-only 0 1)))))
1652 funcs)))
1653 (mdw-advise-update-angry-fruit-salad toggle-read-only
1654 read-only-mode
1655 view-mode
1656 view-mode-enable
1657 view-mode-disable)
1658
1659 (eval-after-load 'gtags
1660 '(progn
1661 (dolist (key '([mouse-2] [mouse-3]))
1662 (define-key gtags-mode-map key nil))
1663 (define-key gtags-mode-map [C-S-mouse-2] 'gtags-find-tag-by-event)
1664 (define-key gtags-select-mode-map [C-S-mouse-2]
1665 'gtags-select-tag-by-event)
1666 (dolist (map (list gtags-mode-map gtags-select-mode-map))
1667 (define-key map [C-S-mouse-3] 'gtags-pop-stack))))
1668
1669 ;; Backup file handling.
1670
1671 (defcustom mdw-backup-disable-regexps nil
1672 "List of regular expressions: if a file name matches any of
1673 these then the file is not backed up."
1674 :type '(repeat regexp))
1675
1676 (defun mdw-backup-enable-predicate (name)
1677 "[mdw]'s default backup predicate.
1678 Allows a backup if the standard predicate would allow it, and it
1679 doesn't match any of the regular expressions in
1680 `mdw-backup-disable-regexps'."
1681 (and (normal-backup-enable-predicate name)
1682 (let ((answer t) (list mdw-backup-disable-regexps))
1683 (save-match-data
1684 (while list
1685 (if (string-match (car list) name)
1686 (setq answer nil))
1687 (setq list (cdr list)))
1688 answer))))
1689 (setq backup-enable-predicate 'mdw-backup-enable-predicate)
1690
1691 ;; Frame cleanup.
1692
1693 (defun mdw-last-one-out-turn-off-the-lights (frame)
1694 "Disconnect from an X display if this was the last frame on that display."
1695 (let ((frame-display (frame-parameter frame 'display)))
1696 (when (and frame-display
1697 (eq window-system 'x)
1698 (not (some (lambda (fr)
1699 (and (not (eq fr frame))
1700 (string= (frame-parameter fr 'display)
1701 frame-display)))
1702 (frame-list))))
1703 (run-with-idle-timer 0 nil #'x-close-connection frame-display))))
1704 (add-hook 'delete-frame-functions 'mdw-last-one-out-turn-off-the-lights)
1705
1706 ;;;--------------------------------------------------------------------------
1707 ;;; Fullscreen-ness.
1708
1709 (defcustom mdw-full-screen-parameters
1710 '((menu-bar-lines . 0)
1711 ;;(vertical-scroll-bars . nil)
1712 )
1713 "Frame parameters to set when making a frame fullscreen."
1714 :type '(alist :key-type symbol))
1715
1716 (defcustom mdw-full-screen-save
1717 '(width height)
1718 "Extra frame parameters to save when setting fullscreen."
1719 :type '(repeat symbol))
1720
1721 (defun mdw-toggle-full-screen (&optional frame)
1722 "Show the FRAME fullscreen."
1723 (interactive)
1724 (when window-system
1725 (cond ((frame-parameter frame 'fullscreen)
1726 (set-frame-parameter frame 'fullscreen nil)
1727 (modify-frame-parameters
1728 nil
1729 (or (frame-parameter frame 'mdw-full-screen-saved)
1730 (mapcar (lambda (assoc)
1731 (assq (car assoc) default-frame-alist))
1732 mdw-full-screen-parameters))))
1733 (t
1734 (let ((saved (mapcar (lambda (param)
1735 (cons param (frame-parameter frame param)))
1736 (append (mapcar #'car
1737 mdw-full-screen-parameters)
1738 mdw-full-screen-save))))
1739 (set-frame-parameter frame 'mdw-full-screen-saved saved))
1740 (modify-frame-parameters frame mdw-full-screen-parameters)
1741 (set-frame-parameter frame 'fullscreen 'fullboth)))))
1742
1743 ;;;--------------------------------------------------------------------------
1744 ;;; General fontification.
1745
1746 (make-face 'mdw-virgin-face)
1747
1748 (defmacro mdw-define-face (name &rest body)
1749 "Define a face, and make sure it's actually set as the definition."
1750 (declare (indent 1)
1751 (debug 0))
1752 `(progn
1753 (copy-face 'mdw-virgin-face ',name)
1754 (defvar ,name ',name)
1755 (put ',name 'face-defface-spec ',body)
1756 (face-spec-set ',name ',body nil)))
1757
1758 (mdw-define-face default
1759 (((type w32)) :family "courier new" :height 85)
1760 (((type x)) :family "6x13" :foundry "trad" :height 130)
1761 (((type color)) :foreground "white" :background "black")
1762 (t nil))
1763 (mdw-define-face fixed-pitch
1764 (((type w32)) :family "courier new" :height 85)
1765 (((type x)) :family "6x13" :foundry "trad" :height 130)
1766 (t :foreground "white" :background "black"))
1767 (mdw-define-face fixed-pitch-serif
1768 (((type w32)) :family "courier new" :height 85 :weight bold)
1769 (((type x)) :family "6x13" :foundry "trad" :height 130 :weight bold)
1770 (t :foreground "white" :background "black" :weight bold))
1771 (mdw-define-face variable-pitch
1772 (((type x)) :family "helvetica" :height 120))
1773 (mdw-define-face region
1774 (((min-colors 64)) :background "grey30")
1775 (((class color)) :background "blue")
1776 (t :inverse-video t))
1777 (mdw-define-face match
1778 (((class color)) :background "blue")
1779 (t :inverse-video t))
1780 (mdw-define-face mc/cursor-face
1781 (((class color)) :background "red")
1782 (t :inverse-video t))
1783 (mdw-define-face minibuffer-prompt
1784 (t :weight bold))
1785 (mdw-define-face mode-line
1786 (((class color)) :foreground "blue" :background "yellow"
1787 :box (:line-width 1 :style released-button))
1788 (t :inverse-video t))
1789 (mdw-define-face mode-line-inactive
1790 (((class color)) :foreground "yellow" :background "blue"
1791 :box (:line-width 1 :style released-button))
1792 (t :inverse-video t))
1793 (mdw-define-face nobreak-space
1794 (((type tty)))
1795 (t :inherit escape-glyph :underline t))
1796 (mdw-define-face scroll-bar
1797 (t :foreground "black" :background "lightgrey"))
1798 (mdw-define-face fringe
1799 (t :foreground "yellow"))
1800 (mdw-define-face show-paren-match
1801 (((min-colors 64)) :background "darkgreen")
1802 (((class color)) :background "green")
1803 (t :underline t))
1804 (mdw-define-face show-paren-mismatch
1805 (((class color)) :background "red")
1806 (t :inverse-video t))
1807 (mdw-define-face highlight
1808 (((min-colors 64)) :background "DarkSeaGreen4")
1809 (((class color)) :background "cyan")
1810 (t :inverse-video t))
1811
1812 (mdw-define-face viper-minibuffer-emacs (t nil))
1813 (mdw-define-face viper-minibuffer-insert (t nil))
1814 (mdw-define-face viper-minibuffer-vi (t nil))
1815 (mdw-define-face viper-replace-overlay
1816 (((min-colors 64)) :background "darkred")
1817 (((class color)) :background "red")
1818 (t :inverse-video t))
1819 (mdw-define-face viper-search (t :inherit isearch))
1820
1821 (mdw-define-face holiday-face
1822 (t :background "red"))
1823 (mdw-define-face calendar-today-face
1824 (t :foreground "yellow" :weight bold))
1825
1826 (mdw-define-face comint-highlight-prompt
1827 (t :weight bold))
1828 (mdw-define-face comint-highlight-input
1829 (t nil))
1830
1831 (mdw-define-face Man-underline
1832 (((type tty)) :underline t)
1833 (t :slant italic))
1834
1835 (mdw-define-face ido-subdir
1836 (t :foreground "cyan" :weight bold))
1837
1838 (mdw-define-face dired-directory
1839 (t :foreground "cyan" :weight bold))
1840 (mdw-define-face dired-symlink
1841 (t :foreground "cyan"))
1842 (mdw-define-face dired-perm-write
1843 (t nil))
1844
1845 (mdw-define-face trailing-whitespace
1846 (((class color)) :background "red")
1847 (t :inverse-video t))
1848 (mdw-define-face whitespace-line
1849 (((class color)) :background "darkred")
1850 (t :inverse-video t))
1851 (mdw-define-face mdw-punct-face
1852 (((min-colors 64)) :foreground "burlywood2")
1853 (((class color)) :foreground "yellow"))
1854 (mdw-define-face mdw-number-face
1855 (t :foreground "yellow"))
1856 (mdw-define-face mdw-trivial-face)
1857 (mdw-define-face font-lock-function-name-face
1858 (t :slant italic))
1859 (mdw-define-face font-lock-keyword-face
1860 (t :weight bold))
1861 (mdw-define-face font-lock-constant-face
1862 (t :slant italic))
1863 (mdw-define-face font-lock-builtin-face
1864 (t :weight bold))
1865 (mdw-define-face font-lock-type-face
1866 (t :weight bold :slant italic))
1867 (mdw-define-face font-lock-reference-face
1868 (t :weight bold))
1869 (mdw-define-face font-lock-variable-name-face
1870 (t :slant italic))
1871 (mdw-define-face font-lock-comment-delimiter-face
1872 (((min-colors 64)) :slant italic :foreground "SeaGreen1")
1873 (((class color)) :foreground "green")
1874 (t :weight bold))
1875 (mdw-define-face font-lock-comment-face
1876 (((min-colors 64)) :slant italic :foreground "SeaGreen1")
1877 (((class color)) :foreground "green")
1878 (t :weight bold))
1879 (mdw-define-face font-lock-string-face
1880 (((min-colors 64)) :foreground "SkyBlue1")
1881 (((class color)) :foreground "cyan")
1882 (t :weight bold))
1883
1884 (mdw-define-face message-separator
1885 (t :background "red" :foreground "white" :weight bold))
1886 (mdw-define-face message-cited-text
1887 (default :slant italic)
1888 (((min-colors 64)) :foreground "SkyBlue1")
1889 (((class color)) :foreground "cyan"))
1890 (mdw-define-face message-header-cc
1891 (default :slant italic)
1892 (((min-colors 64)) :foreground "SeaGreen1")
1893 (((class color)) :foreground "green"))
1894 (mdw-define-face message-header-newsgroups
1895 (default :slant italic)
1896 (((min-colors 64)) :foreground "SeaGreen1")
1897 (((class color)) :foreground "green"))
1898 (mdw-define-face message-header-subject
1899 (((min-colors 64)) :foreground "SeaGreen1")
1900 (((class color)) :foreground "green"))
1901 (mdw-define-face message-header-to
1902 (((min-colors 64)) :foreground "SeaGreen1")
1903 (((class color)) :foreground "green"))
1904 (mdw-define-face message-header-xheader
1905 (default :slant italic)
1906 (((min-colors 64)) :foreground "SeaGreen1")
1907 (((class color)) :foreground "green"))
1908 (mdw-define-face message-header-other
1909 (default :slant italic)
1910 (((min-colors 64)) :foreground "SeaGreen1")
1911 (((class color)) :foreground "green"))
1912 (mdw-define-face message-header-name
1913 (default :weight bold)
1914 (((min-colors 64)) :foreground "SeaGreen1")
1915 (((class color)) :foreground "green"))
1916
1917 (mdw-define-face which-func
1918 (t nil))
1919
1920 (mdw-define-face gnus-header-name
1921 (default :weight bold)
1922 (((min-colors 64)) :foreground "SeaGreen1")
1923 (((class color)) :foreground "green"))
1924 (mdw-define-face gnus-header-subject
1925 (((min-colors 64)) :foreground "SeaGreen1")
1926 (((class color)) :foreground "green"))
1927 (mdw-define-face gnus-header-from
1928 (((min-colors 64)) :foreground "SeaGreen1")
1929 (((class color)) :foreground "green"))
1930 (mdw-define-face gnus-header-to
1931 (((min-colors 64)) :foreground "SeaGreen1")
1932 (((class color)) :foreground "green"))
1933 (mdw-define-face gnus-header-content
1934 (default :slant italic)
1935 (((min-colors 64)) :foreground "SeaGreen1")
1936 (((class color)) :foreground "green"))
1937
1938 (mdw-define-face gnus-cite-1
1939 (((min-colors 64)) :foreground "SkyBlue1")
1940 (((class color)) :foreground "cyan"))
1941 (mdw-define-face gnus-cite-2
1942 (((min-colors 64)) :foreground "RoyalBlue2")
1943 (((class color)) :foreground "blue"))
1944 (mdw-define-face gnus-cite-3
1945 (((min-colors 64)) :foreground "MediumOrchid")
1946 (((class color)) :foreground "magenta"))
1947 (mdw-define-face gnus-cite-4
1948 (((min-colors 64)) :foreground "firebrick2")
1949 (((class color)) :foreground "red"))
1950 (mdw-define-face gnus-cite-5
1951 (((min-colors 64)) :foreground "burlywood2")
1952 (((class color)) :foreground "yellow"))
1953 (mdw-define-face gnus-cite-6
1954 (((min-colors 64)) :foreground "SeaGreen1")
1955 (((class color)) :foreground "green"))
1956 (mdw-define-face gnus-cite-7
1957 (((min-colors 64)) :foreground "SlateBlue1")
1958 (((class color)) :foreground "cyan"))
1959 (mdw-define-face gnus-cite-8
1960 (((min-colors 64)) :foreground "RoyalBlue2")
1961 (((class color)) :foreground "blue"))
1962 (mdw-define-face gnus-cite-9
1963 (((min-colors 64)) :foreground "purple2")
1964 (((class color)) :foreground "magenta"))
1965 (mdw-define-face gnus-cite-10
1966 (((min-colors 64)) :foreground "DarkOrange2")
1967 (((class color)) :foreground "red"))
1968 (mdw-define-face gnus-cite-11
1969 (t :foreground "grey"))
1970
1971 (mdw-define-face gnus-emphasis-underline
1972 (((type tty)) :underline t)
1973 (t :slant italic))
1974
1975 (mdw-define-face diff-header
1976 (t nil))
1977 (mdw-define-face diff-index
1978 (t :weight bold))
1979 (mdw-define-face diff-file-header
1980 (t :weight bold))
1981 (mdw-define-face diff-hunk-header
1982 (((min-colors 64)) :foreground "SkyBlue1")
1983 (((class color)) :foreground "cyan"))
1984 (mdw-define-face diff-function
1985 (default :weight bold)
1986 (((min-colors 64)) :foreground "SkyBlue1")
1987 (((class color)) :foreground "cyan"))
1988 (mdw-define-face diff-header
1989 (((min-colors 64)) :background "grey10"))
1990 (mdw-define-face diff-added
1991 (((class color)) :foreground "green"))
1992 (mdw-define-face diff-removed
1993 (((class color)) :foreground "red"))
1994 (mdw-define-face diff-context
1995 (t nil))
1996 (mdw-define-face diff-refine-change
1997 (((min-colors 64)) :background "RoyalBlue4")
1998 (t :underline t))
1999 (mdw-define-face diff-refine-removed
2000 (((min-colors 64)) :background "#500")
2001 (t :underline t))
2002 (mdw-define-face diff-refine-added
2003 (((min-colors 64)) :background "#050")
2004 (t :underline t))
2005
2006 (setq ediff-force-faces t)
2007 (mdw-define-face ediff-current-diff-A
2008 (((min-colors 64)) :background "darkred")
2009 (((class color)) :background "red")
2010 (t :inverse-video t))
2011 (mdw-define-face ediff-fine-diff-A
2012 (((min-colors 64)) :background "red3")
2013 (((class color)) :inverse-video t)
2014 (t :inverse-video nil))
2015 (mdw-define-face ediff-even-diff-A
2016 (((min-colors 64)) :background "#300"))
2017 (mdw-define-face ediff-odd-diff-A
2018 (((min-colors 64)) :background "#300"))
2019 (mdw-define-face ediff-current-diff-B
2020 (((min-colors 64)) :background "darkgreen")
2021 (((class color)) :background "magenta")
2022 (t :inverse-video t))
2023 (mdw-define-face ediff-fine-diff-B
2024 (((min-colors 64)) :background "green4")
2025 (((class color)) :inverse-video t)
2026 (t :inverse-video nil))
2027 (mdw-define-face ediff-even-diff-B
2028 (((min-colors 64)) :background "#020"))
2029 (mdw-define-face ediff-odd-diff-B
2030 (((min-colors 64)) :background "#020"))
2031 (mdw-define-face ediff-current-diff-C
2032 (((min-colors 64)) :background "darkblue")
2033 (((class color)) :background "blue")
2034 (t :inverse-video t))
2035 (mdw-define-face ediff-fine-diff-C
2036 (((min-colors 64)) :background "blue1")
2037 (((class color)) :inverse-video t)
2038 (t :inverse-video nil))
2039 (mdw-define-face ediff-even-diff-C
2040 (((min-colors 64)) :background "#004"))
2041 (mdw-define-face ediff-odd-diff-C
2042 (((min-colors 64)) :background "#004"))
2043 (mdw-define-face ediff-current-diff-Ancestor
2044 (((min-colors 64)) :background "#630")
2045 (((class color)) :background "blue")
2046 (t :inverse-video t))
2047 (mdw-define-face ediff-even-diff-Ancestor
2048 (((min-colors 64)) :background "#320"))
2049 (mdw-define-face ediff-odd-diff-Ancestor
2050 (((min-colors 64)) :background "#320"))
2051
2052 (mdw-define-face magit-hash
2053 (((min-colors 64)) :foreground "grey40")
2054 (((class color)) :foreground "blue"))
2055 (mdw-define-face magit-diff-hunk-heading
2056 (((min-colors 64)) :foreground "grey70" :background "grey25")
2057 (((class color)) :foreground "yellow"))
2058 (mdw-define-face magit-diff-hunk-heading-highlight
2059 (((min-colors 64)) :foreground "grey70" :background "grey35")
2060 (((class color)) :foreground "yellow" :background "blue"))
2061 (mdw-define-face magit-diff-added
2062 (((min-colors 64)) :foreground "#ddffdd" :background "#335533")
2063 (((class color)) :foreground "green"))
2064 (mdw-define-face magit-diff-added-highlight
2065 (((min-colors 64)) :foreground "#cceecc" :background "#336633")
2066 (((class color)) :foreground "green" :background "blue"))
2067 (mdw-define-face magit-diff-removed
2068 (((min-colors 64)) :foreground "#ffdddd" :background "#553333")
2069 (((class color)) :foreground "red"))
2070 (mdw-define-face magit-diff-removed-highlight
2071 (((min-colors 64)) :foreground "#eecccc" :background "#663333")
2072 (((class color)) :foreground "red" :background "blue"))
2073 (mdw-define-face magit-blame-heading
2074 (((min-colors 64)) :foreground "white" :background "grey25"
2075 :weight normal :slant normal)
2076 (((class color)) :foreground "white" :background "blue"
2077 :weight normal :slant normal))
2078 (mdw-define-face magit-blame-name
2079 (t :inherit magit-blame-heading :slant italic))
2080 (mdw-define-face magit-blame-date
2081 (((min-colors 64)) :inherit magit-blame-heading :foreground "grey60")
2082 (((class color)) :inherit magit-blame-heading :foreground "cyan"))
2083 (mdw-define-face magit-blame-summary
2084 (t :inherit magit-blame-heading :weight bold))
2085
2086 (mdw-define-face dylan-header-background
2087 (((min-colors 64)) :background "NavyBlue")
2088 (((class color)) :background "blue"))
2089
2090 (mdw-define-face erc-input-face
2091 (t :foreground "red"))
2092
2093 (mdw-define-face woman-bold
2094 (t :weight bold))
2095 (mdw-define-face woman-italic
2096 (t :slant italic))
2097
2098 (eval-after-load "rst"
2099 '(progn
2100 (mdw-define-face rst-level-1-face
2101 (t :foreground "SkyBlue1" :weight bold))
2102 (mdw-define-face rst-level-2-face
2103 (t :foreground "SeaGreen1" :weight bold))
2104 (mdw-define-face rst-level-3-face
2105 (t :weight bold))
2106 (mdw-define-face rst-level-4-face
2107 (t :slant italic))
2108 (mdw-define-face rst-level-5-face
2109 (t :underline t))
2110 (mdw-define-face rst-level-6-face
2111 ())))
2112
2113 (mdw-define-face p4-depot-added-face
2114 (t :foreground "green"))
2115 (mdw-define-face p4-depot-branch-op-face
2116 (t :foreground "yellow"))
2117 (mdw-define-face p4-depot-deleted-face
2118 (t :foreground "red"))
2119 (mdw-define-face p4-depot-unmapped-face
2120 (t :foreground "SkyBlue1"))
2121 (mdw-define-face p4-diff-change-face
2122 (t :foreground "yellow"))
2123 (mdw-define-face p4-diff-del-face
2124 (t :foreground "red"))
2125 (mdw-define-face p4-diff-file-face
2126 (t :foreground "SkyBlue1"))
2127 (mdw-define-face p4-diff-head-face
2128 (t :background "grey10"))
2129 (mdw-define-face p4-diff-ins-face
2130 (t :foreground "green"))
2131
2132 (mdw-define-face w3m-anchor-face
2133 (t :foreground "SkyBlue1" :underline t))
2134 (mdw-define-face w3m-arrived-anchor-face
2135 (t :foreground "SkyBlue1" :underline t))
2136
2137 (mdw-define-face whizzy-slice-face
2138 (t :background "grey10"))
2139 (mdw-define-face whizzy-error-face
2140 (t :background "darkred"))
2141
2142 ;; Ellipses used to indicate hidden text (and similar).
2143 (mdw-define-face mdw-ellipsis-face
2144 (((type tty)) :foreground "blue") (t :foreground "grey60"))
2145 (let ((dollar (make-glyph-code ?$ 'mdw-ellipsis-face))
2146 (backslash (make-glyph-code ?\\ 'mdw-ellipsis-face))
2147 (dot (make-glyph-code ?. 'mdw-ellipsis-face))
2148 (bar (make-glyph-code ?| mdw-ellipsis-face)))
2149 (set-display-table-slot standard-display-table 0 dollar)
2150 (set-display-table-slot standard-display-table 1 backslash)
2151 (set-display-table-slot standard-display-table 4
2152 (vector dot dot dot))
2153 (set-display-table-slot standard-display-table 5 bar))
2154
2155 ;;;--------------------------------------------------------------------------
2156 ;;; Where is point?
2157
2158 (mdw-define-face mdw-point-overlay-face
2159 (((type graphic)))
2160 (((min-colors 64)) :background "darkblue")
2161 (((class color)) :background "blue")
2162 (((type tty) (class mono)) :inverse-video t))
2163
2164 (defcustom mdw-point-overlay-fringe-display '(vertical-bar . vertical-bar)
2165 "Bitmaps to display in the left and right fringes in the current line."
2166 :type '(cons symbol symbol))
2167
2168 (defun mdw-configure-point-overlay ()
2169 (let ((ov (make-overlay 0 0)))
2170 (overlay-put ov 'priority 0)
2171 (let* ((fringe (or mdw-point-overlay-fringe-display (cons nil nil)))
2172 (left (car fringe)) (right (cdr fringe))
2173 (s ""))
2174 (when left
2175 (let ((ss "."))
2176 (put-text-property 0 1 'display `(left-fringe ,left) ss)
2177 (setq s (concat s ss))))
2178 (when right
2179 (let ((ss "."))
2180 (put-text-property 0 1 'display `(right-fringe ,right) ss)
2181 (setq s (concat s ss))))
2182 (when (or left right)
2183 (overlay-put ov 'before-string s)))
2184 (overlay-put ov 'face 'mdw-point-overlay-face)
2185 (delete-overlay ov)
2186 ov))
2187
2188 (defvar mdw-point-overlay (mdw-configure-point-overlay)
2189 "An overlay used for showing where point is in the selected window.")
2190 (defun mdw-reconfigure-point-overlay ()
2191 (interactive)
2192 (setq mdw-point-overlay (mdw-configure-point-overlay)))
2193
2194 (defun mdw-remove-point-overlay ()
2195 "Remove the current-point overlay."
2196 (delete-overlay mdw-point-overlay))
2197
2198 (defun mdw-update-point-overlay ()
2199 "Mark the current point position with an overlay."
2200 (if (not mdw-point-overlay-mode)
2201 (mdw-remove-point-overlay)
2202 (overlay-put mdw-point-overlay 'window (selected-window))
2203 (move-overlay mdw-point-overlay
2204 (line-beginning-position)
2205 (+ (line-end-position) 1))))
2206
2207 (defvar mdw-point-overlay-buffers nil
2208 "List of buffers using `mdw-point-overlay-mode'.")
2209
2210 (define-minor-mode mdw-point-overlay-mode
2211 "Indicate current line with an overlay."
2212 :global nil
2213 (let ((buffer (current-buffer)))
2214 (setq mdw-point-overlay-buffers
2215 (mapcan (lambda (buf)
2216 (if (and (buffer-live-p buf)
2217 (not (eq buf buffer)))
2218 (list buf)))
2219 mdw-point-overlay-buffers))
2220 (if mdw-point-overlay-mode
2221 (setq mdw-point-overlay-buffers
2222 (cons buffer mdw-point-overlay-buffers))))
2223 (cond (mdw-point-overlay-buffers
2224 (add-hook 'pre-command-hook 'mdw-remove-point-overlay)
2225 (add-hook 'post-command-hook 'mdw-update-point-overlay))
2226 (t
2227 (mdw-remove-point-overlay)
2228 (remove-hook 'pre-command-hook 'mdw-remove-point-overlay)
2229 (remove-hook 'post-command-hook 'mdw-update-point-overlay))))
2230
2231 (define-globalized-minor-mode mdw-global-point-overlay-mode
2232 mdw-point-overlay-mode
2233 (lambda () (if (not (minibufferp)) (mdw-point-overlay-mode t))))
2234
2235 (defvar mdw-terminal-title-alist nil)
2236 (defun mdw-update-terminal-title ()
2237 (when (let ((term (frame-parameter nil 'tty-type)))
2238 (and term (string-match "^xterm" term)))
2239 (let* ((tty (frame-parameter nil 'tty))
2240 (old (assoc tty mdw-terminal-title-alist))
2241 (new (format-mode-line frame-title-format)))
2242 (unless (and old (equal (cdr old) new))
2243 (if old (rplacd old new)
2244 (setq mdw-terminal-title-alist
2245 (cons (cons tty new) mdw-terminal-title-alist)))
2246 (send-string-to-terminal (concat "\e]2;" new "\e\\"))))))
2247
2248 (add-hook 'post-command-hook 'mdw-update-terminal-title)
2249
2250 ;;;--------------------------------------------------------------------------
2251 ;;; C programming configuration.
2252
2253 ;; Make C indentation nice.
2254
2255 (defun mdw-c-lineup-arglist (langelem)
2256 "Hack for DWIMmery in c-lineup-arglist."
2257 (if (save-excursion
2258 (c-block-in-arglist-dwim (c-langelem-2nd-pos c-syntactic-element)))
2259 0
2260 (c-lineup-arglist langelem)))
2261
2262 (defun mdw-c-indent-extern-mumble (langelem)
2263 "Indent `extern \"...\" {' lines."
2264 (save-excursion
2265 (back-to-indentation)
2266 (if (looking-at
2267 "\\s-*\\<extern\\>\\s-*\"\\([^\\\\\"]+\\|\\.\\)*\"\\s-*{")
2268 c-basic-offset
2269 nil)))
2270
2271 (defun mdw-c-indent-arglist-nested (langelem)
2272 "Indent continued argument lists.
2273 If we've nested more than one argument list, then only introduce a single
2274 indentation anyway."
2275 (let ((context c-syntactic-context)
2276 (pos (c-langelem-2nd-pos c-syntactic-element))
2277 (should-indent-p t))
2278 (while (and context
2279 (eq (caar context) 'arglist-cont-nonempty))
2280 (when (and (= (caddr (pop context)) pos)
2281 context
2282 (memq (caar context) '(arglist-intro
2283 arglist-cont-nonempty)))
2284 (setq should-indent-p nil)))
2285 (if should-indent-p '+ 0)))
2286
2287 (defvar mdw-define-c-styles-hook nil
2288 "Hook run when `cc-mode' starts up to define styles.")
2289
2290 (defun mdw-merge-style-alists (first second)
2291 (let ((output nil))
2292 (dolist (item first)
2293 (let ((key (car item)) (value (cdr item)))
2294 (if (let* ((key-name (symbol-name key))
2295 (key-len (length key-name)))
2296 (and (>= key-len 5)
2297 (string= (subseq key-name (- key-len 5)) "-alist")))
2298 (push (cons key
2299 (mdw-merge-style-alists value
2300 (cdr (assoc key second))))
2301 output)
2302 (push item output))))
2303 (dolist (item second)
2304 (unless (assoc (car item) first)
2305 (push item output)))
2306 (nreverse output)))
2307
2308 (defmacro* mdw-define-c-style (name (&optional parent) &rest assocs)
2309 "Define a C style, called NAME (a symbol) based on PARENT, setting ASSOCs.
2310 A function, named `mdw-define-c-style/NAME', is defined to actually install
2311 the style using `c-add-style', and added to the hook
2312 `mdw-define-c-styles-hook'. If CC Mode is already loaded, then the style is
2313 set."
2314 (declare (indent defun))
2315 (let* ((name-string (symbol-name name))
2316 (var (intern (concat "mdw-c-style/" name-string)))
2317 (func (intern (concat "mdw-define-c-style/" name-string))))
2318 `(progn
2319 (setq ,var
2320 ,(if (null parent)
2321 `',assocs
2322 (let ((parent-list (intern (concat "mdw-c-style/"
2323 (symbol-name parent)))))
2324 `(mdw-merge-style-alists ',assocs ,parent-list))))
2325 (defun ,func () (c-add-style ,name-string ,var))
2326 (and (featurep 'cc-mode) (,func))
2327 (add-hook 'mdw-define-c-styles-hook ',func)
2328 ',name)))
2329
2330 (eval-after-load "cc-mode"
2331 '(run-hooks 'mdw-define-c-styles-hook))
2332
2333 (mdw-define-c-style mdw-c ()
2334 (c-basic-offset . 2)
2335 (comment-column . 40)
2336 (c-class-key . "class")
2337 (c-backslash-column . 72)
2338 (c-label-minimum-indentation . 0)
2339 (c-offsets-alist (substatement-open . (add 0 c-indent-one-line-block))
2340 (defun-open . (add 0 c-indent-one-line-block))
2341 (arglist-cont-nonempty . mdw-c-lineup-arglist)
2342 (topmost-intro . mdw-c-indent-extern-mumble)
2343 (cpp-define-intro . 0)
2344 (knr-argdecl . 0)
2345 (inextern-lang . [0])
2346 (label . 0)
2347 (case-label . +)
2348 (access-label . -)
2349 (inclass . +)
2350 (inline-open . ++)
2351 (statement-cont . +)
2352 (statement-case-intro . +)))
2353
2354 (mdw-define-c-style mdw-trustonic-c (mdw-c)
2355 (c-basic-offset . 4)
2356 (c-offsets-alist (access-label . -2)))
2357
2358 (mdw-define-c-style mdw-trustonic-alec-c (mdw-trustonic-c)
2359 (comment-column . 0)
2360 (c-indent-comment-alist (anchored-comment . (column . 0))
2361 (end-block . (space . 1))
2362 (cpp-end-block . (space . 1))
2363 (other . (space . 1)))
2364 (c-offsets-alist (arglist-cont-nonempty . mdw-c-indent-arglist-nested)))
2365
2366 (defun mdw-set-default-c-style (modes style)
2367 "Update the default CC Mode style for MODES to be STYLE.
2368
2369 MODES may be a list of major mode names or a singleton. STYLE is a style
2370 name, as a symbol."
2371 (let ((modes (if (listp modes) modes (list modes)))
2372 (style (symbol-name style)))
2373 (setq c-default-style
2374 (append (mapcar (lambda (mode)
2375 (cons mode style))
2376 modes)
2377 (remove-if (lambda (assoc)
2378 (memq (car assoc) modes))
2379 (if (listp c-default-style)
2380 c-default-style
2381 (list (cons 'other c-default-style))))))))
2382 (setq c-default-style "mdw-c")
2383
2384 (mdw-set-default-c-style '(c-mode c++-mode) 'mdw-c)
2385
2386 (defvar mdw-c-comment-fill-prefix
2387 `((,(concat "\\([ \t]*/?\\)"
2388 "\\(\\*\\|//\\)"
2389 "\\([ \t]*\\)"
2390 "\\([A-Za-z]+:[ \t]*\\)?"
2391 mdw-hanging-indents)
2392 (pad . 1) (match . 2) (pad . 3) (pad . 4) (pad . 5)))
2393 "Fill prefix matching C comments (both kinds).")
2394
2395 (defun mdw-fontify-c-and-c++ ()
2396
2397 ;; Fiddle with some syntax codes.
2398 (modify-syntax-entry ?* ". 23")
2399 (modify-syntax-entry ?/ ". 124b")
2400 (modify-syntax-entry ?\n "> b")
2401
2402 ;; Other stuff.
2403 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2404
2405 ;; Now define things to be fontified.
2406 (make-local-variable 'font-lock-keywords)
2407 (let ((c-keywords
2408 (mdw-regexps "alignas" ;C11 macro, C++11
2409 "alignof" ;C++11
2410 "and" ;C++, C95 macro
2411 "and_eq" ;C++, C95 macro
2412 "asm" ;K&R, C++, GCC
2413 "atomic" ;C11 macro, C++11 template type
2414 "auto" ;K&R, C89
2415 "bitand" ;C++, C95 macro
2416 "bitor" ;C++, C95 macro
2417 "bool" ;C++, C99 macro
2418 "break" ;K&R, C89
2419 "case" ;K&R, C89
2420 "catch" ;C++
2421 "char" ;K&R, C89
2422 "char16_t" ;C++11, C11 library type
2423 "char32_t" ;C++11, C11 library type
2424 "class" ;C++
2425 "complex" ;C99 macro, C++ template type
2426 "compl" ;C++, C95 macro
2427 "const" ;C89
2428 "constexpr" ;C++11
2429 "const_cast" ;C++
2430 "continue" ;K&R, C89
2431 "decltype" ;C++11
2432 "defined" ;C89 preprocessor
2433 "default" ;K&R, C89
2434 "delete" ;C++
2435 "do" ;K&R, C89
2436 "double" ;K&R, C89
2437 "dynamic_cast" ;C++
2438 "else" ;K&R, C89
2439 ;; "entry" ;K&R -- never used
2440 "enum" ;C89
2441 "explicit" ;C++
2442 "export" ;C++
2443 "extern" ;K&R, C89
2444 "float" ;K&R, C89
2445 "for" ;K&R, C89
2446 ;; "fortran" ;K&R
2447 "friend" ;C++
2448 "goto" ;K&R, C89
2449 "if" ;K&R, C89
2450 "imaginary" ;C99 macro
2451 "inline" ;C++, C99, GCC
2452 "int" ;K&R, C89
2453 "long" ;K&R, C89
2454 "mutable" ;C++
2455 "namespace" ;C++
2456 "new" ;C++
2457 "noexcept" ;C++11
2458 "noreturn" ;C11 macro
2459 "not" ;C++, C95 macro
2460 "not_eq" ;C++, C95 macro
2461 "nullptr" ;C++11
2462 "operator" ;C++
2463 "or" ;C++, C95 macro
2464 "or_eq" ;C++, C95 macro
2465 "private" ;C++
2466 "protected" ;C++
2467 "public" ;C++
2468 "register" ;K&R, C89
2469 "reinterpret_cast" ;C++
2470 "restrict" ;C99
2471 "return" ;K&R, C89
2472 "short" ;K&R, C89
2473 "signed" ;C89
2474 "sizeof" ;K&R, C89
2475 "static" ;K&R, C89
2476 "static_assert" ;C11 macro, C++11
2477 "static_cast" ;C++
2478 "struct" ;K&R, C89
2479 "switch" ;K&R, C89
2480 "template" ;C++
2481 "throw" ;C++
2482 "try" ;C++
2483 "thread_local" ;C11 macro, C++11
2484 "typedef" ;C89
2485 "typeid" ;C++
2486 "typeof" ;GCC
2487 "typename" ;C++
2488 "union" ;K&R, C89
2489 "unsigned" ;K&R, C89
2490 "using" ;C++
2491 "virtual" ;C++
2492 "void" ;C89
2493 "volatile" ;C89
2494 "wchar_t" ;C++, C89 library type
2495 "while" ;K&R, C89
2496 "xor" ;C++, C95 macro
2497 "xor_eq" ;C++, C95 macro
2498 "_Alignas" ;C11
2499 "_Alignof" ;C11
2500 "_Atomic" ;C11
2501 "_Bool" ;C99
2502 "_Complex" ;C99
2503 "_Generic" ;C11
2504 "_Imaginary" ;C99
2505 "_Noreturn" ;C11
2506 "_Pragma" ;C99 preprocessor
2507 "_Static_assert" ;C11
2508 "_Thread_local" ;C11
2509 "__alignof__" ;GCC
2510 "__asm__" ;GCC
2511 "__attribute__" ;GCC
2512 "__complex__" ;GCC
2513 "__const__" ;GCC
2514 "__extension__" ;GCC
2515 "__imag__" ;GCC
2516 "__inline__" ;GCC
2517 "__label__" ;GCC
2518 "__real__" ;GCC
2519 "__signed__" ;GCC
2520 "__typeof__" ;GCC
2521 "__volatile__" ;GCC
2522 ))
2523 (c-builtins
2524 (mdw-regexps "false" ;C++, C99 macro
2525 "this" ;C++
2526 "true" ;C++, C99 macro
2527 ))
2528 (preprocessor-keywords
2529 (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
2530 "ident" "if" "ifdef" "ifndef" "import" "include"
2531 "line" "pragma" "unassert" "undef" "warning"))
2532 (objc-keywords
2533 (mdw-regexps "class" "defs" "encode" "end" "implementation"
2534 "interface" "private" "protected" "protocol" "public"
2535 "selector")))
2536
2537 (setq font-lock-keywords
2538 (list
2539
2540 ;; Fontify include files as strings.
2541 (list (concat "^[ \t]*\\#[ \t]*"
2542 "\\(include\\|import\\)"
2543 "[ \t]*\\(<[^>]+>?\\)")
2544 '(2 font-lock-string-face))
2545
2546 ;; Preprocessor directives are `references'?.
2547 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
2548 preprocessor-keywords
2549 "\\)\\>\\|[0-9]+\\|$\\)\\)")
2550 '(1 font-lock-keyword-face))
2551
2552 ;; Handle the keywords defined above.
2553 (list (concat "@\\<\\(" objc-keywords "\\)\\>")
2554 '(0 font-lock-keyword-face))
2555
2556 (list (concat "\\<\\(" c-keywords "\\)\\>")
2557 '(0 font-lock-keyword-face))
2558
2559 (list (concat "\\<\\(" c-builtins "\\)\\>")
2560 '(0 font-lock-variable-name-face))
2561
2562 ;; Handle numbers too.
2563 ;;
2564 ;; This looks strange, I know. It corresponds to the
2565 ;; preprocessor's idea of what a number looks like, rather than
2566 ;; anything sensible.
2567 (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
2568 "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
2569 '(0 mdw-number-face))
2570
2571 ;; And anything else is punctuation.
2572 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2573 '(0 mdw-punct-face))))))
2574
2575 (define-derived-mode sod-mode c-mode "Sod"
2576 "Major mode for editing Sod code.")
2577 (push '("\\.sod$" . sod-mode) auto-mode-alist)
2578
2579 (dolist (hook '(c-mode-hook objc-mode-hook c++-mode-hook))
2580 (add-hook hook 'mdw-misc-mode-config t)
2581 (add-hook hook 'mdw-fontify-c-and-c++ t))
2582
2583 ;;;--------------------------------------------------------------------------
2584 ;;; AP calc mode.
2585
2586 (define-derived-mode apcalc-mode c-mode "AP Calc"
2587 "Major mode for editing Calc code.")
2588
2589 (defun mdw-fontify-apcalc ()
2590
2591 ;; Fiddle with some syntax codes.
2592 (modify-syntax-entry ?* ". 23")
2593 (modify-syntax-entry ?/ ". 14")
2594
2595 ;; Other stuff.
2596 (setq comment-start "/* ")
2597 (setq comment-end " */")
2598 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2599
2600 ;; Now define things to be fontified.
2601 (make-local-variable 'font-lock-keywords)
2602 (let ((c-keywords
2603 (mdw-regexps "break" "case" "cd" "continue" "define" "default"
2604 "do" "else" "exit" "for" "global" "goto" "help" "if"
2605 "local" "mat" "obj" "print" "quit" "read" "return"
2606 "show" "static" "switch" "while" "write")))
2607
2608 (setq font-lock-keywords
2609 (list
2610
2611 ;; Handle the keywords defined above.
2612 (list (concat "\\<\\(" c-keywords "\\)\\>")
2613 '(0 font-lock-keyword-face))
2614
2615 ;; Handle numbers too.
2616 ;;
2617 ;; This looks strange, I know. It corresponds to the
2618 ;; preprocessor's idea of what a number looks like, rather than
2619 ;; anything sensible.
2620 (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
2621 "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
2622 '(0 mdw-number-face))
2623
2624 ;; And anything else is punctuation.
2625 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2626 '(0 mdw-punct-face))))))
2627
2628 (progn
2629 (add-hook 'apcalc-mode-hook 'mdw-misc-mode-config t)
2630 (add-hook 'apcalc-mode-hook 'mdw-fontify-apcalc t))
2631
2632 ;;;--------------------------------------------------------------------------
2633 ;;; Java programming configuration.
2634
2635 ;; Make indentation nice.
2636
2637 (mdw-define-c-style mdw-java ()
2638 (c-basic-offset . 2)
2639 (c-backslash-column . 72)
2640 (c-offsets-alist (substatement-open . 0)
2641 (label . +)
2642 (case-label . +)
2643 (access-label . 0)
2644 (inclass . +)
2645 (statement-case-intro . +)))
2646 (mdw-set-default-c-style 'java-mode 'mdw-java)
2647
2648 ;; Declare Java fontification style.
2649
2650 (defun mdw-fontify-java ()
2651
2652 ;; Fiddle with some syntax codes.
2653 (modify-syntax-entry ?@ ".")
2654 (modify-syntax-entry ?@ "." font-lock-syntax-table)
2655
2656 ;; Other stuff.
2657 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2658
2659 ;; Now define things to be fontified.
2660 (make-local-variable 'font-lock-keywords)
2661 (let ((java-keywords
2662 (mdw-regexps "abstract" "assert"
2663 "boolean" "break" "byte"
2664 "case" "catch" "char" "class" "const" "continue"
2665 "default" "do" "double"
2666 "else" "enum" "extends"
2667 "final" "finally" "float" "for"
2668 "goto"
2669 "if" "implements" "import" "instanceof" "int"
2670 "interface"
2671 "long"
2672 "native" "new"
2673 "package" "private" "protected" "public"
2674 "return"
2675 "short" "static" "strictfp" "switch" "synchronized"
2676 "throw" "throws" "transient" "try"
2677 "void" "volatile"
2678 "while"))
2679
2680 (java-builtins
2681 (mdw-regexps "false" "null" "super" "this" "true")))
2682
2683 (setq font-lock-keywords
2684 (list
2685
2686 ;; Handle the keywords defined above.
2687 (list (concat "\\<\\(" java-keywords "\\)\\>")
2688 '(0 font-lock-keyword-face))
2689
2690 ;; Handle the magic builtins defined above.
2691 (list (concat "\\<\\(" java-builtins "\\)\\>")
2692 '(0 font-lock-variable-name-face))
2693
2694 ;; Handle numbers too.
2695 ;;
2696 ;; The following isn't quite right, but it's close enough.
2697 (list (concat "\\<\\("
2698 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2699 "[0-9]+\\(\\.[0-9]*\\)?"
2700 "\\([eE][-+]?[0-9]+\\)?\\)"
2701 "[lLfFdD]?")
2702 '(0 mdw-number-face))
2703
2704 ;; And anything else is punctuation.
2705 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2706 '(0 mdw-punct-face))))))
2707
2708 (progn
2709 (add-hook 'java-mode-hook 'mdw-misc-mode-config t)
2710 (add-hook 'java-mode-hook 'mdw-fontify-java t))
2711
2712 ;;;--------------------------------------------------------------------------
2713 ;;; Javascript programming configuration.
2714
2715 (defun mdw-javascript-style ()
2716 (setq js-indent-level 2)
2717 (setq js-expr-indent-offset 0))
2718
2719 (defun mdw-fontify-javascript ()
2720
2721 ;; Other stuff.
2722 (mdw-javascript-style)
2723 (setq js-auto-indent-flag t)
2724
2725 ;; Now define things to be fontified.
2726 (make-local-variable 'font-lock-keywords)
2727 (let ((javascript-keywords
2728 (mdw-regexps "abstract" "boolean" "break" "byte" "case" "catch"
2729 "char" "class" "const" "continue" "debugger" "default"
2730 "delete" "do" "double" "else" "enum" "export" "extends"
2731 "final" "finally" "float" "for" "function" "goto" "if"
2732 "implements" "import" "in" "instanceof" "int"
2733 "interface" "let" "long" "native" "new" "package"
2734 "private" "protected" "public" "return" "short"
2735 "static" "super" "switch" "synchronized" "throw"
2736 "throws" "transient" "try" "typeof" "var" "void"
2737 "volatile" "while" "with" "yield"))
2738 (javascript-builtins
2739 (mdw-regexps "false" "null" "undefined" "Infinity" "NaN" "true"
2740 "arguments" "this")))
2741
2742 (setq font-lock-keywords
2743 (list
2744
2745 ;; Handle the keywords defined above.
2746 (list (concat "\\_<\\(" javascript-keywords "\\)\\_>")
2747 '(0 font-lock-keyword-face))
2748
2749 ;; Handle the predefined builtins defined above.
2750 (list (concat "\\_<\\(" javascript-builtins "\\)\\_>")
2751 '(0 font-lock-variable-name-face))
2752
2753 ;; Handle numbers too.
2754 ;;
2755 ;; The following isn't quite right, but it's close enough.
2756 (list (concat "\\_<\\("
2757 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2758 "[0-9]+\\(\\.[0-9]*\\)?"
2759 "\\([eE][-+]?[0-9]+\\)?\\)"
2760 "[lLfFdD]?")
2761 '(0 mdw-number-face))
2762
2763 ;; And anything else is punctuation.
2764 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2765 '(0 mdw-punct-face))))))
2766
2767 (progn
2768 (add-hook 'js-mode-hook 'mdw-misc-mode-config t)
2769 (add-hook 'js-mode-hook 'mdw-fontify-javascript t))
2770
2771 ;;;--------------------------------------------------------------------------
2772 ;;; Scala programming configuration.
2773
2774 (defun mdw-fontify-scala ()
2775
2776 ;; Comment filling.
2777 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2778
2779 ;; Define things to be fontified.
2780 (make-local-variable 'font-lock-keywords)
2781 (let ((scala-keywords
2782 (mdw-regexps "abstract" "case" "catch" "class" "def" "do" "else"
2783 "extends" "final" "finally" "for" "forSome" "if"
2784 "implicit" "import" "lazy" "match" "new" "object"
2785 "override" "package" "private" "protected" "return"
2786 "sealed" "throw" "trait" "try" "type" "val"
2787 "var" "while" "with" "yield"))
2788 (scala-constants
2789 (mdw-regexps "false" "null" "super" "this" "true"))
2790 (punctuation "[-!%^&*=+:@#~/?\\|`]"))
2791
2792 (setq font-lock-keywords
2793 (list
2794
2795 ;; Magical identifiers between backticks.
2796 (list (concat "`\\([^`]+\\)`")
2797 '(1 font-lock-variable-name-face))
2798
2799 ;; Handle the keywords defined above.
2800 (list (concat "\\_<\\(" scala-keywords "\\)\\_>")
2801 '(0 font-lock-keyword-face))
2802
2803 ;; Handle the constants defined above.
2804 (list (concat "\\_<\\(" scala-constants "\\)\\_>")
2805 '(0 font-lock-variable-name-face))
2806
2807 ;; Magical identifiers between backticks.
2808 (list (concat "`\\([^`]+\\)`")
2809 '(1 font-lock-variable-name-face))
2810
2811 ;; Handle numbers too.
2812 ;;
2813 ;; As usual, not quite right.
2814 (list (concat "\\_<\\("
2815 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2816 "[0-9]+\\(\\.[0-9]*\\)?"
2817 "\\([eE][-+]?[0-9]+\\)?\\)"
2818 "[lLfFdD]?")
2819 '(0 mdw-number-face))
2820
2821 ;; And everything else is punctuation.
2822 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2823 '(0 mdw-punct-face)))
2824
2825 font-lock-syntactic-keywords
2826 (list
2827
2828 ;; Single quotes around characters. But not when used to quote
2829 ;; symbol names. Ugh.
2830 (list (concat "\\('\\)"
2831 "\\(" "."
2832 "\\|" "\\\\" "\\(" "\\\\\\\\" "\\)*"
2833 "u+" "[0-9a-fA-F]\\{4\\}"
2834 "\\|" "\\\\" "[0-7]\\{1,3\\}"
2835 "\\|" "\\\\" "." "\\)"
2836 "\\('\\)")
2837 '(1 "\"")
2838 '(4 "\""))))))
2839
2840 (progn
2841 (add-hook 'scala-mode-hook 'mdw-misc-mode-config t)
2842 (add-hook 'scala-mode-hook 'mdw-fontify-scala t))
2843
2844 ;;;--------------------------------------------------------------------------
2845 ;;; C# programming configuration.
2846
2847 ;; Make indentation nice.
2848
2849 (mdw-define-c-style mdw-csharp ()
2850 (c-basic-offset . 2)
2851 (c-backslash-column . 72)
2852 (c-offsets-alist (substatement-open . 0)
2853 (label . 0)
2854 (case-label . +)
2855 (access-label . 0)
2856 (inclass . +)
2857 (statement-case-intro . +)))
2858 (mdw-set-default-c-style 'csharp-mode 'mdw-csharp)
2859
2860 ;; Declare C# fontification style.
2861
2862 (defun mdw-fontify-csharp ()
2863
2864 ;; Other stuff.
2865 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2866
2867 ;; Now define things to be fontified.
2868 (make-local-variable 'font-lock-keywords)
2869 (let ((csharp-keywords
2870 (mdw-regexps "abstract" "as" "bool" "break" "byte" "case" "catch"
2871 "char" "checked" "class" "const" "continue" "decimal"
2872 "default" "delegate" "do" "double" "else" "enum"
2873 "event" "explicit" "extern" "finally" "fixed" "float"
2874 "for" "foreach" "goto" "if" "implicit" "in" "int"
2875 "interface" "internal" "is" "lock" "long" "namespace"
2876 "new" "object" "operator" "out" "override" "params"
2877 "private" "protected" "public" "readonly" "ref"
2878 "return" "sbyte" "sealed" "short" "sizeof"
2879 "stackalloc" "static" "string" "struct" "switch"
2880 "throw" "try" "typeof" "uint" "ulong" "unchecked"
2881 "unsafe" "ushort" "using" "virtual" "void" "volatile"
2882 "while" "yield"))
2883
2884 (csharp-builtins
2885 (mdw-regexps "base" "false" "null" "this" "true")))
2886
2887 (setq font-lock-keywords
2888 (list
2889
2890 ;; Handle the keywords defined above.
2891 (list (concat "\\<\\(" csharp-keywords "\\)\\>")
2892 '(0 font-lock-keyword-face))
2893
2894 ;; Handle the magic builtins defined above.
2895 (list (concat "\\<\\(" csharp-builtins "\\)\\>")
2896 '(0 font-lock-variable-name-face))
2897
2898 ;; Handle numbers too.
2899 ;;
2900 ;; The following isn't quite right, but it's close enough.
2901 (list (concat "\\<\\("
2902 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2903 "[0-9]+\\(\\.[0-9]*\\)?"
2904 "\\([eE][-+]?[0-9]+\\)?\\)"
2905 "[lLfFdD]?")
2906 '(0 mdw-number-face))
2907
2908 ;; And anything else is punctuation.
2909 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2910 '(0 mdw-punct-face))))))
2911
2912 (define-derived-mode csharp-mode java-mode "C#"
2913 "Major mode for editing C# code.")
2914
2915 (add-hook 'csharp-mode-hook 'mdw-fontify-csharp t)
2916
2917 ;;;--------------------------------------------------------------------------
2918 ;;; F# programming configuration.
2919
2920 (setq fsharp-indent-offset 2)
2921
2922 (defun mdw-fontify-fsharp ()
2923
2924 (let ((punct "=<>+-*/|&%!@?"))
2925 (do ((i 0 (1+ i)))
2926 ((>= i (length punct)))
2927 (modify-syntax-entry (aref punct i) ".")))
2928
2929 (modify-syntax-entry ?_ "_")
2930 (modify-syntax-entry ?( "(")
2931 (modify-syntax-entry ?) ")")
2932
2933 (setq indent-tabs-mode nil)
2934
2935 (let ((fsharp-keywords
2936 (mdw-regexps "abstract" "and" "as" "assert" "atomic"
2937 "begin" "break"
2938 "checked" "class" "component" "const" "constraint"
2939 "constructor" "continue"
2940 "default" "delegate" "do" "done" "downcast" "downto"
2941 "eager" "elif" "else" "end" "exception" "extern"
2942 "finally" "fixed" "for" "fori" "fun" "function"
2943 "functor"
2944 "global"
2945 "if" "in" "include" "inherit" "inline" "interface"
2946 "internal"
2947 "lazy" "let"
2948 "match" "measure" "member" "method" "mixin" "module"
2949 "mutable"
2950 "namespace" "new"
2951 "object" "of" "open" "or" "override"
2952 "parallel" "params" "private" "process" "protected"
2953 "public" "pure"
2954 "rec" "recursive" "return"
2955 "sealed" "sig" "static" "struct"
2956 "tailcall" "then" "to" "trait" "try" "type"
2957 "upcast" "use"
2958 "val" "virtual" "void" "volatile"
2959 "when" "while" "with"
2960 "yield"))
2961
2962 (fsharp-builtins
2963 (mdw-regexps "asr" "land" "lor" "lsl" "lsr" "lxor" "mod"
2964 "base" "false" "null" "true"))
2965
2966 (bang-keywords
2967 (mdw-regexps "do" "let" "return" "use" "yield"))
2968
2969 (preprocessor-keywords
2970 (mdw-regexps "if" "indent" "else" "endif")))
2971
2972 (setq font-lock-keywords
2973 (list (list (concat "\\(^\\|[^\"]\\)"
2974 "\\(" "(\\*"
2975 "[^*]*\\*+"
2976 "\\(" "[^)*]" "[^*]*" "\\*+" "\\)*"
2977 ")"
2978 "\\|"
2979 "//.*"
2980 "\\)")
2981 '(2 font-lock-comment-face))
2982
2983 (list (concat "'" "\\("
2984 "\\\\"
2985 "\\(" "[ntbr'\\]"
2986 "\\|" "[0-9][0-9][0-9]"
2987 "\\|" "u" "[0-9a-fA-F]\\{4\\}"
2988 "\\|" "U" "[0-9a-fA-F]\\{8\\}"
2989 "\\)"
2990 "\\|"
2991 "." "\\)" "'"
2992 "\\|"
2993 "\"" "[^\"\\]*"
2994 "\\(" "\\\\" "\\(.\\|\n\\)"
2995 "[^\"\\]*" "\\)*"
2996 "\\(\"\\|\\'\\)")
2997 '(0 font-lock-string-face))
2998
2999 (list (concat "\\_<\\(" bang-keywords "\\)!" "\\|"
3000 "^#[ \t]*\\(" preprocessor-keywords "\\)\\_>"
3001 "\\|"
3002 "\\_<\\(" fsharp-keywords "\\)\\_>")
3003 '(0 font-lock-keyword-face))
3004 (list (concat "\\<\\(" fsharp-builtins "\\)\\_>")
3005 '(0 font-lock-variable-name-face))
3006
3007 (list (concat "\\_<"
3008 "\\(" "0[bB][01]+" "\\|"
3009 "0[oO][0-7]+" "\\|"
3010 "0[xX][0-9a-fA-F]+" "\\)"
3011 "\\(" "lf\\|LF" "\\|"
3012 "[uU]?[ysnlL]?" "\\)"
3013 "\\|"
3014 "\\_<"
3015 "[0-9]+" "\\("
3016 "[mMQRZING]"
3017 "\\|"
3018 "\\(\\.[0-9]*\\)?"
3019 "\\([eE][-+]?[0-9]+\\)?"
3020 "[fFmM]?"
3021 "\\|"
3022 "[uU]?[ysnlL]?"
3023 "\\)")
3024 '(0 mdw-number-face))
3025
3026 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3027 '(0 mdw-punct-face))))))
3028
3029 (defun mdw-fontify-inferior-fsharp ()
3030 (mdw-fontify-fsharp)
3031 (setq font-lock-keywords
3032 (append (list (list "^[#-]" '(0 font-lock-comment-face))
3033 (list "^>" '(0 font-lock-keyword-face)))
3034 font-lock-keywords)))
3035
3036 (progn
3037 (add-hook 'fsharp-mode-hook 'mdw-misc-mode-config t)
3038 (add-hook 'fsharp-mode-hook 'mdw-fontify-fsharp t)
3039 (add-hook 'inferior-fsharp-mode-hooks 'mdw-fontify-inferior-fsharp t))
3040
3041 ;;;--------------------------------------------------------------------------
3042 ;;; Go programming configuration.
3043
3044 (defun mdw-fontify-go ()
3045
3046 (make-local-variable 'font-lock-keywords)
3047 (let ((go-keywords
3048 (mdw-regexps "break" "case" "chan" "const" "continue"
3049 "default" "defer" "else" "fallthrough" "for"
3050 "func" "go" "goto" "if" "import"
3051 "interface" "map" "package" "range" "return"
3052 "select" "struct" "switch" "type" "var"))
3053 (go-intrinsics
3054 (mdw-regexps "bool" "byte" "complex64" "complex128" "error"
3055 "float32" "float64" "int" "uint8" "int16" "int32"
3056 "int64" "rune" "string" "uint" "uint8" "uint16"
3057 "uint32" "uint64" "uintptr" "void"
3058 "false" "iota" "nil" "true"
3059 "init" "main"
3060 "append" "cap" "copy" "delete" "imag" "len" "make"
3061 "new" "panic" "real" "recover")))
3062
3063 (setq font-lock-keywords
3064 (list
3065
3066 ;; Handle the keywords defined above.
3067 (list (concat "\\<\\(" go-keywords "\\)\\>")
3068 '(0 font-lock-keyword-face))
3069 (list (concat "\\<\\(" go-intrinsics "\\)\\>")
3070 '(0 font-lock-variable-name-face))
3071
3072 ;; Strings and characters.
3073 (list (concat "'"
3074 "\\(" "[^\\']" "\\|"
3075 "\\\\"
3076 "\\(" "[abfnrtv\\'\"]" "\\|"
3077 "[0-7]\\{3\\}" "\\|"
3078 "x" "[0-9A-Fa-f]\\{2\\}" "\\|"
3079 "u" "[0-9A-Fa-f]\\{4\\}" "\\|"
3080 "U" "[0-9A-Fa-f]\\{8\\}" "\\)" "\\)"
3081 "'"
3082 "\\|"
3083 "\""
3084 "\\(" "[^\n\\\"]+" "\\|" "\\\\." "\\)*"
3085 "\\(\"\\|$\\)"
3086 "\\|"
3087 "`" "[^`]+" "`")
3088 '(0 font-lock-string-face))
3089
3090 ;; Handle numbers too.
3091 ;;
3092 ;; The following isn't quite right, but it's close enough.
3093 (list (concat "\\<\\("
3094 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
3095 "[0-9]+\\(\\.[0-9]*\\)?"
3096 "\\([eE][-+]?[0-9]+\\)?\\)")
3097 '(0 mdw-number-face))
3098
3099 ;; And anything else is punctuation.
3100 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3101 '(0 mdw-punct-face))))))
3102 (progn
3103 (add-hook 'go-mode-hook 'mdw-misc-mode-config t)
3104 (add-hook 'go-mode-hook 'mdw-fontify-go t))
3105
3106 ;;;--------------------------------------------------------------------------
3107 ;;; Rust programming configuration.
3108
3109 (setq-default rust-indent-offset 2)
3110
3111 (defun mdw-self-insert-and-indent (count)
3112 (interactive "p")
3113 (self-insert-command count)
3114 (indent-according-to-mode))
3115
3116 (defun mdw-fontify-rust ()
3117
3118 ;; Hack syntax categories.
3119 (modify-syntax-entry ?$ ".")
3120 (modify-syntax-entry ?% ".")
3121 (modify-syntax-entry ?= ".")
3122
3123 ;; Fontify keywords and things.
3124 (make-local-variable 'font-lock-keywords)
3125 (let ((rust-keywords
3126 (mdw-regexps "abstract" "alignof" "as" "async" "await"
3127 "become" "box" "break"
3128 "const" "continue" "crate"
3129 "do" "dyn"
3130 "else" "enum" "extern"
3131 "final" "fn" "for"
3132 "if" "impl" "in"
3133 "let" "loop"
3134 "macro" "match" "mod" "move" "mut"
3135 "offsetof" "override"
3136 "priv" "proc" "pub" "pure"
3137 "ref" "return"
3138 "sizeof" "static" "struct" "super"
3139 "trait" "try" "type" "typeof"
3140 "union" "unsafe" "unsized" "use"
3141 "virtual"
3142 "where" "while"
3143 "yield"))
3144 (rust-builtins
3145 (mdw-regexps "array" "pointer" "slice" "tuple"
3146 "bool" "true" "false"
3147 "f32" "f64"
3148 "i8" "i16" "i32" "i64" "isize"
3149 "u8" "u16" "u32" "u64" "usize"
3150 "char" "str"
3151 "self" "Self")))
3152 (setq font-lock-keywords
3153 (list
3154
3155 ;; Handle the keywords defined above.
3156 (list (concat "\\_<\\(" rust-keywords "\\)\\_>")
3157 '(0 font-lock-keyword-face))
3158 (list (concat "\\_<\\(" rust-builtins "\\)\\_>")
3159 '(0 font-lock-variable-name-face))
3160
3161 ;; Handle numbers too.
3162 (list (concat "\\_<\\("
3163 "[0-9][0-9_]*"
3164 "\\(" "\\(\\.[0-9_]+\\)?[eE][-+]?[0-9_]+"
3165 "\\|" "\\.[0-9_]+"
3166 "\\)"
3167 "\\(f32\\|f64\\)?"
3168 "\\|" "\\(" "[0-9][0-9_]*"
3169 "\\|" "0x[0-9a-fA-F_]+"
3170 "\\|" "0o[0-7_]+"
3171 "\\|" "0b[01_]+"
3172 "\\)"
3173 "\\([ui]\\(8\\|16\\|32\\|64\\|size\\)\\)?"
3174 "\\)\\_>")
3175 '(0 mdw-number-face))
3176
3177 ;; And anything else is punctuation.
3178 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3179 '(0 mdw-punct-face)))))
3180
3181 ;; Hack key bindings.
3182 (local-set-key [?{] 'mdw-self-insert-and-indent)
3183 (local-set-key [?}] 'mdw-self-insert-and-indent))
3184
3185 (progn
3186 (add-hook 'rust-mode-hook 'mdw-misc-mode-config t)
3187 (add-hook 'rust-mode-hook 'mdw-fontify-rust t))
3188
3189 ;;;--------------------------------------------------------------------------
3190 ;;; Awk programming configuration.
3191
3192 ;; Make Awk indentation nice.
3193
3194 (mdw-define-c-style mdw-awk ()
3195 (c-basic-offset . 2)
3196 (c-offsets-alist (substatement-open . 0)
3197 (c-backslash-column . 72)
3198 (statement-cont . 0)
3199 (statement-case-intro . +)))
3200 (mdw-set-default-c-style 'awk-mode 'mdw-awk)
3201
3202 ;; Declare Awk fontification style.
3203
3204 (defun mdw-fontify-awk ()
3205
3206 ;; Miscellaneous fiddling.
3207 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3208
3209 ;; Now define things to be fontified.
3210 (make-local-variable 'font-lock-keywords)
3211 (let ((c-keywords
3212 (mdw-regexps "BEGIN" "END" "ARGC" "ARGIND" "ARGV" "CONVFMT"
3213 "ENVIRON" "ERRNO" "FIELDWIDTHS" "FILENAME" "FNR"
3214 "FS" "IGNORECASE" "NF" "NR" "OFMT" "OFS" "ORS" "RS"
3215 "RSTART" "RLENGTH" "RT" "SUBSEP"
3216 "atan2" "break" "close" "continue" "cos" "delete"
3217 "do" "else" "exit" "exp" "fflush" "file" "for" "func"
3218 "function" "gensub" "getline" "gsub" "if" "in"
3219 "index" "int" "length" "log" "match" "next" "rand"
3220 "return" "print" "printf" "sin" "split" "sprintf"
3221 "sqrt" "srand" "strftime" "sub" "substr" "system"
3222 "systime" "tolower" "toupper" "while")))
3223
3224 (setq font-lock-keywords
3225 (list
3226
3227 ;; Handle the keywords defined above.
3228 (list (concat "\\<\\(" c-keywords "\\)\\>")
3229 '(0 font-lock-keyword-face))
3230
3231 ;; Handle numbers too.
3232 ;;
3233 ;; The following isn't quite right, but it's close enough.
3234 (list (concat "\\<\\("
3235 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
3236 "[0-9]+\\(\\.[0-9]*\\)?"
3237 "\\([eE][-+]?[0-9]+\\)?\\)"
3238 "[uUlL]*")
3239 '(0 mdw-number-face))
3240
3241 ;; And anything else is punctuation.
3242 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3243 '(0 mdw-punct-face))))))
3244
3245 (progn
3246 (add-hook 'awk-mode-hook 'mdw-misc-mode-config t)
3247 (add-hook 'awk-mode-hook 'mdw-fontify-awk t))
3248
3249 ;;;--------------------------------------------------------------------------
3250 ;;; Perl programming style.
3251
3252 ;; Perl indentation style.
3253
3254 (setq-default perl-indent-level 2)
3255
3256 (setq-default cperl-indent-level 2
3257 cperl-continued-statement-offset 2
3258 cperl-indent-region-fix-constructs nil
3259 cperl-continued-brace-offset 0
3260 cperl-brace-offset -2
3261 cperl-brace-imaginary-offset 0
3262 cperl-label-offset 0)
3263
3264 ;; Define perl fontification style.
3265
3266 (defun mdw-fontify-perl ()
3267
3268 ;; Miscellaneous fiddling.
3269 (modify-syntax-entry ?$ "\\")
3270 (modify-syntax-entry ?$ "\\" font-lock-syntax-table)
3271 (modify-syntax-entry ?: "." font-lock-syntax-table)
3272 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3273
3274 ;; Now define fontification things.
3275 (make-local-variable 'font-lock-keywords)
3276 (let ((perl-keywords
3277 (mdw-regexps "and"
3278 "break"
3279 "cmp" "continue"
3280 "default" "do"
3281 "else" "elsif" "eq"
3282 "for" "foreach"
3283 "ge" "given" "gt" "goto"
3284 "if"
3285 "last" "le" "local" "lt"
3286 "my"
3287 "ne" "next"
3288 "or" "our"
3289 "package"
3290 "redo" "require" "return"
3291 "sub"
3292 "undef" "unless" "until" "use"
3293 "when" "while")))
3294
3295 (setq font-lock-keywords
3296 (list
3297
3298 ;; Set up the keywords defined above.
3299 (list (concat "\\<\\(" perl-keywords "\\)\\>")
3300 '(0 font-lock-keyword-face))
3301
3302 ;; At least numbers are simpler than C.
3303 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
3304 "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\)?"
3305 "\\([eE][-+]?[0-9_]+\\)?")
3306 '(0 mdw-number-face))
3307
3308 ;; And anything else is punctuation.
3309 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3310 '(0 mdw-punct-face))))))
3311
3312 (defun perl-number-tests (&optional arg)
3313 "Assign consecutive numbers to lines containing `#t'. With ARG,
3314 strip numbers instead."
3315 (interactive "P")
3316 (save-excursion
3317 (goto-char (point-min))
3318 (let ((i 0) (fmt (if arg "" " %4d")))
3319 (while (search-forward "#t" nil t)
3320 (delete-region (point) (line-end-position))
3321 (setq i (1+ i))
3322 (insert (format fmt i)))
3323 (goto-char (point-min))
3324 (if (re-search-forward "\\(tests\\s-*=>\\s-*\\)\\w*" nil t)
3325 (replace-match (format "\\1%d" i))))))
3326
3327 (dolist (hook '(perl-mode-hook cperl-mode-hook))
3328 (add-hook hook 'mdw-misc-mode-config t)
3329 (add-hook hook 'mdw-fontify-perl t))
3330
3331 ;;;--------------------------------------------------------------------------
3332 ;;; Python programming style.
3333
3334 (setq-default py-indent-offset 2
3335 python-indent 2
3336 python-indent-offset 2
3337 python-fill-docstring-style 'symmetric)
3338
3339 (defun mdw-fontify-pythonic (keywords)
3340
3341 ;; Miscellaneous fiddling.
3342 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3343 (setq indent-tabs-mode nil)
3344 (set (make-local-variable 'forward-sexp-function) nil)
3345
3346 ;; Now define fontification things.
3347 (make-local-variable 'font-lock-keywords)
3348 (setq font-lock-keywords
3349 (list
3350
3351 ;; Set up the keywords defined above.
3352 (list (concat "\\_<\\(" keywords "\\)\\_>")
3353 '(0 font-lock-keyword-face))
3354
3355 ;; At least numbers are simpler than C.
3356 (list (concat "\\_<0\\([xX][0-9a-fA-F]+\\|[oO]?[0-7]+\\|[bB][01]+\\)\\|"
3357 "\\_<[0-9][0-9]*\\(\\.[0-9]*\\)?"
3358 "\\([eE][-+]?[0-9]+\\|[lL]\\)?")
3359 '(0 mdw-number-face))
3360
3361 ;; And anything else is punctuation.
3362 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3363 '(0 mdw-punct-face)))))
3364
3365 ;; Define Python fontification styles.
3366
3367 (defun mdw-fontify-python ()
3368 (mdw-fontify-pythonic
3369 (mdw-regexps "and" "as" "assert" "break" "class" "continue" "def"
3370 "del" "elif" "else" "except" "exec" "finally" "for"
3371 "from" "global" "if" "import" "in" "is" "lambda"
3372 "not" "or" "pass" "print" "raise" "return" "try"
3373 "while" "with" "yield")))
3374
3375 (defun mdw-fontify-pyrex ()
3376 (mdw-fontify-pythonic
3377 (mdw-regexps "and" "as" "assert" "break" "cdef" "class" "continue"
3378 "ctypedef" "def" "del" "elif" "else" "enum" "except" "exec"
3379 "extern" "finally" "for" "from" "global" "if"
3380 "import" "in" "is" "lambda" "not" "or" "pass" "print"
3381 "property" "raise" "return" "struct" "try" "while" "with"
3382 "yield")))
3383
3384 (define-derived-mode pyrex-mode python-mode "Pyrex"
3385 "Major mode for editing Pyrex source code")
3386 (setq auto-mode-alist
3387 (append '(("\\.pyx$" . pyrex-mode)
3388 ("\\.pxd$" . pyrex-mode)
3389 ("\\.pxi$" . pyrex-mode))
3390 auto-mode-alist))
3391
3392 (progn
3393 (add-hook 'python-mode-hook 'mdw-misc-mode-config t)
3394 (add-hook 'python-mode-hook 'mdw-fontify-python t)
3395 (add-hook 'pyrex-mode-hook 'mdw-fontify-pyrex t))
3396
3397 ;;;--------------------------------------------------------------------------
3398 ;;; Lua programming style.
3399
3400 (setq-default lua-indent-level 2)
3401
3402 (defun mdw-fontify-lua ()
3403
3404 ;; Miscellaneous fiddling.
3405 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3406
3407 ;; Now define fontification things.
3408 (make-local-variable 'font-lock-keywords)
3409 (let ((lua-keywords
3410 (mdw-regexps "and" "break" "do" "else" "elseif" "end"
3411 "false" "for" "function" "goto" "if" "in" "local"
3412 "nil" "not" "or" "repeat" "return" "then" "true"
3413 "until" "while")))
3414 (setq font-lock-keywords
3415 (list
3416
3417 ;; Set up the keywords defined above.
3418 (list (concat "\\_<\\(" lua-keywords "\\)\\_>")
3419 '(0 font-lock-keyword-face))
3420
3421 ;; At least numbers are simpler than C.
3422 (list (concat "\\_<\\(" "0[xX]"
3423 "\\(" "[0-9a-fA-F]+"
3424 "\\(\\.[0-9a-fA-F]*\\)?"
3425 "\\|" "\\.[0-9a-fA-F]+"
3426 "\\)"
3427 "\\([pP][-+]?[0-9]+\\)?"
3428 "\\|" "\\(" "[0-9]+"
3429 "\\(\\.[0-9]*\\)?"
3430 "\\|" "\\.[0-9]+"
3431 "\\)"
3432 "\\([eE][-+]?[0-9]+\\)?"
3433 "\\)")
3434 '(0 mdw-number-face))
3435
3436 ;; And anything else is punctuation.
3437 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3438 '(0 mdw-punct-face))))))
3439
3440 (progn
3441 (add-hook 'lua-mode-hook 'mdw-misc-mode-config t)
3442 (add-hook 'lua-mode-hook 'mdw-fontify-lua t))
3443
3444 ;;;--------------------------------------------------------------------------
3445 ;;; Icon programming style.
3446
3447 ;; Icon indentation style.
3448
3449 (setq-default icon-brace-offset 0
3450 icon-continued-brace-offset 0
3451 icon-continued-statement-offset 2
3452 icon-indent-level 2)
3453
3454 ;; Define Icon fontification style.
3455
3456 (defun mdw-fontify-icon ()
3457
3458 ;; Miscellaneous fiddling.
3459 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3460
3461 ;; Now define fontification things.
3462 (make-local-variable 'font-lock-keywords)
3463 (let ((icon-keywords
3464 (mdw-regexps "break" "by" "case" "create" "default" "do" "else"
3465 "end" "every" "fail" "global" "if" "initial"
3466 "invocable" "link" "local" "next" "not" "of"
3467 "procedure" "record" "repeat" "return" "static"
3468 "suspend" "then" "to" "until" "while"))
3469 (preprocessor-keywords
3470 (mdw-regexps "define" "else" "endif" "error" "ifdef" "ifndef"
3471 "include" "line" "undef")))
3472 (setq font-lock-keywords
3473 (list
3474
3475 ;; Set up the keywords defined above.
3476 (list (concat "\\<\\(" icon-keywords "\\)\\>")
3477 '(0 font-lock-keyword-face))
3478
3479 ;; The things that Icon calls keywords.
3480 (list "&\\sw+\\>" '(0 font-lock-variable-name-face))
3481
3482 ;; At least numbers are simpler than C.
3483 (list (concat "\\<[0-9]+"
3484 "\\([rR][0-9a-zA-Z]+\\|"
3485 "\\.[0-9]+\\([eE][+-]?[0-9]+\\)?\\)\\>\\|"
3486 "\\.[0-9]+\\([eE][+-]?[0-9]+\\)?\\>")
3487 '(0 mdw-number-face))
3488
3489 ;; Preprocessor.
3490 (list (concat "^[ \t]*$[ \t]*\\<\\("
3491 preprocessor-keywords
3492 "\\)\\>")
3493 '(0 font-lock-keyword-face))
3494
3495 ;; And anything else is punctuation.
3496 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3497 '(0 mdw-punct-face))))))
3498
3499 (progn
3500 (add-hook 'icon-mode-hook 'mdw-misc-mode-config t)
3501 (add-hook 'icon-mode-hook 'mdw-fontify-icon t))
3502
3503 ;;;--------------------------------------------------------------------------
3504 ;;; Fortran mode.
3505
3506 (defun mdw-fontify-fortran-common ()
3507 (let ((fortran-keywords
3508 (mdw-regexps "access"
3509 "assign"
3510 "associate"
3511 "backspace"
3512 "blank"
3513 "block\\s-*data"
3514 "call"
3515 "case"
3516 "character"
3517 "class"
3518 "close"
3519 "common"
3520 "complex"
3521 "continue"
3522 "critical"
3523 "data"
3524 "dimension"
3525 "do"
3526 "double\\s-*precision"
3527 "else" "elseif" "elsewhere"
3528 "end"
3529 "endblock" "endblockdata"
3530 "endcritical"
3531 "enddo"
3532 "endinterface"
3533 "endmodule"
3534 "endprocedure"
3535 "endprogram"
3536 "endselect"
3537 "endsubmodule"
3538 "endsubroutine"
3539 "endtype"
3540 "endwhere"
3541 "endenum"
3542 "end\\s-*file"
3543 "endforall"
3544 "endfunction"
3545 "endif"
3546 "entry"
3547 "enum"
3548 "equivalence"
3549 "err"
3550 "external"
3551 "file"
3552 "fmt"
3553 "forall"
3554 "form"
3555 "format"
3556 "function"
3557 "go\\s-*to"
3558 "if"
3559 "implicit"
3560 "in" "inout"
3561 "inquire"
3562 "include"
3563 "integer"
3564 "interface"
3565 "intrinsic"
3566 "iostat"
3567 "len"
3568 "logical"
3569 "module"
3570 "open"
3571 "out"
3572 "parameter"
3573 "pause"
3574 "procedure"
3575 "program"
3576 "precision"
3577 "program"
3578 "read"
3579 "real"
3580 "rec"
3581 "recl"
3582 "return"
3583 "rewind"
3584 "save"
3585 "select" "selectcase" "selecttype"
3586 "status"
3587 "stop"
3588 "submodule"
3589 "subroutine"
3590 "then"
3591 "to"
3592 "type"
3593 "unit"
3594 "where"
3595 "write"))
3596 (fortran-operators (mdw-regexps "and"
3597 "eq"
3598 "eqv"
3599 "false"
3600 "ge"
3601 "gt"
3602 "le"
3603 "lt"
3604 "ne"
3605 "neqv"
3606 "not"
3607 "or"
3608 "true"))
3609 (fortran-intrinsics (mdw-regexps "abs" "dabs" "iabs" "cabs"
3610 "atan" "datan" "atan2" "datan2"
3611 "cmplx"
3612 "conjg"
3613 "cos" "dcos" "ccos"
3614 "dble"
3615 "dim" "idim"
3616 "exp" "dexp" "cexp"
3617 "float"
3618 "ifix"
3619 "aimag"
3620 "int" "aint" "idint"
3621 "alog" "dlog" "clog"
3622 "alog10" "dlog10"
3623 "max"
3624 "amax0" "amax1"
3625 "max0" "max1"
3626 "dmax1"
3627 "min"
3628 "amin0" "amin1"
3629 "min0" "min1"
3630 "dmin1"
3631 "mod" "amod" "dmod"
3632 "sin" "dsin" "csin"
3633 "sign" "isign" "dsign"
3634 "sngl"
3635 "sqrt" "dsqrt" "csqrt"
3636 "tanh"))
3637 (preprocessor-keywords
3638 (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
3639 "ident" "if" "ifdef" "ifndef" "import" "include"
3640 "line" "pragma" "unassert" "undef" "warning")))
3641 (setq font-lock-keywords-case-fold-search t
3642 font-lock-keywords
3643 (list
3644
3645 ;; Fontify include files as strings.
3646 (list (concat "^[ \t]*\\#[ \t]*" "include"
3647 "[ \t]*\\(<[^>]+>?\\)")
3648 '(1 font-lock-string-face))
3649
3650 ;; Preprocessor directives are `references'?.
3651 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
3652 preprocessor-keywords
3653 "\\)\\>\\|[0-9]+\\|$\\)\\)")
3654 '(1 font-lock-keyword-face))
3655
3656 ;; Set up the keywords defined above.
3657 (list (concat "\\<\\(" fortran-keywords "\\)\\>")
3658 '(0 font-lock-keyword-face))
3659
3660 ;; Set up the `.foo.' operators.
3661 (list (concat "\\.\\(" fortran-operators "\\)\\.")
3662 '(0 font-lock-keyword-face))
3663
3664 ;; Set up the intrinsic functions.
3665 (list (concat "\\<\\(" fortran-intrinsics "\\)\\>")
3666 '(0 font-lock-variable-name-face))
3667
3668 ;; Numbers.
3669 (list (concat "\\(" "\\<" "[0-9]+" "\\(\\.[0-9]*\\)?"
3670 "\\|" "\\.[0-9]+"
3671 "\\)"
3672 "\\(" "[de]" "[+-]?" "[0-9]+" "\\)?"
3673 "\\(" "_" "\\sw+" "\\)?"
3674 "\\|" "b'[01]*'" "\\|" "'[01]*'b"
3675 "\\|" "b\"[01]*\"" "\\|" "\"[01]*\"b"
3676 "\\|" "o'[0-7]*'" "\\|" "'[0-7]*'o"
3677 "\\|" "o\"[0-7]*\"" "\\|" "\"[0-7]*\"o"
3678 "\\|" "[xz]'[0-9a-f]*'" "\\|" "'[0-9a-f]*'[xz]"
3679 "\\|" "[xz]\"[0-9a-f]*\"" "\\|" "\"[0-9a-f]*\"[xz]")
3680 '(0 mdw-number-face))
3681
3682 ;; Any anything else is punctuation.
3683 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3684 '(0 mdw-punct-face))))
3685
3686 (modify-syntax-entry ?/ "." font-lock-syntax-table)
3687 (modify-syntax-entry ?< ".")
3688 (modify-syntax-entry ?> ".")))
3689
3690 (defun mdw-fontify-fortran () (mdw-fontify-fortran-common))
3691 (defun mdw-fontify-f90 () (mdw-fontify-fortran-common))
3692
3693 (setq fortran-do-indent 2
3694 fortran-if-indent 2
3695 fortran-structure-indent 2
3696 fortran-comment-line-start "*"
3697 fortran-comment-indent-style 'relative
3698 fortran-continuation-string "&"
3699 fortran-continuation-indent 4)
3700
3701 (setq f90-do-indent 2
3702 f90-if-indent 2
3703 f90-program-indent 2
3704 f90-continuation-indent 4
3705 f90-smart-end-names nil
3706 f90-smart-end 'no-blink)
3707
3708 (progn
3709 (add-hook 'fortran-mode-hook 'mdw-misc-mode-config t)
3710 (add-hook 'fortran-mode-hook 'mdw-fontify-fortran t)
3711 (add-hook 'f90-mode-hook 'mdw-misc-mode-config t)
3712 (add-hook 'f90-mode-hook 'mdw-fontify-f90 t))
3713
3714 ;;;--------------------------------------------------------------------------
3715 ;;; Assembler mode.
3716
3717 (defun mdw-fontify-asm ()
3718 (modify-syntax-entry ?' "\"")
3719 (modify-syntax-entry ?. "w")
3720 (modify-syntax-entry ?\n ">")
3721 (setf fill-prefix nil)
3722 (modify-syntax-entry ?. "_")
3723 (modify-syntax-entry ?* ". 23")
3724 (modify-syntax-entry ?/ ". 124b")
3725 (modify-syntax-entry ?\n "> b")
3726 (local-set-key ";" 'self-insert-command)
3727 (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)"))
3728
3729 (defun mdw-asm-set-comment ()
3730 (modify-syntax-entry ?; "."
3731 )
3732 (modify-syntax-entry asm-comment-char "< b")
3733 (setq comment-start (string asm-comment-char ? )))
3734 (add-hook 'asm-mode-local-variables-hook 'mdw-asm-set-comment)
3735 (put 'asm-comment-char 'safe-local-variable 'characterp)
3736
3737 (progn
3738 (add-hook 'asm-mode-hook 'mdw-misc-mode-config t)
3739 (add-hook 'asm-mode-hook 'mdw-fontify-asm t))
3740
3741 ;;;--------------------------------------------------------------------------
3742 ;;; TCL configuration.
3743
3744 (setq-default tcl-indent-level 2)
3745
3746 (defun mdw-fontify-tcl ()
3747 (dolist (ch '(?$))
3748 (modify-syntax-entry ch "."))
3749 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3750 (make-local-variable 'font-lock-keywords)
3751 (setq font-lock-keywords
3752 (list
3753 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
3754 "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\)?"
3755 "\\([eE][-+]?[0-9_]+\\)?")
3756 '(0 mdw-number-face))
3757 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3758 '(0 mdw-punct-face)))))
3759
3760 (progn
3761 (add-hook 'tcl-mode-hook 'mdw-misc-mode-config t)
3762 (add-hook 'tcl-mode-hook 'mdw-fontify-tcl t))
3763
3764 ;;;--------------------------------------------------------------------------
3765 ;;; Dylan programming configuration.
3766
3767 (defun mdw-fontify-dylan ()
3768
3769 (make-local-variable 'font-lock-keywords)
3770
3771 ;; Horrors. `dylan-mode' sets the `major-mode' name after calling this
3772 ;; hook, which undoes all of our configuration.
3773 (setq major-mode 'dylan-mode)
3774 (font-lock-set-defaults)
3775
3776 (let* ((word "[-_a-zA-Z!*@<>$%]+")
3777 (dylan-keywords (mdw-regexps
3778
3779 "C-address" "C-callable-wrapper" "C-function"
3780 "C-mapped-subtype" "C-pointer-type" "C-struct"
3781 "C-subtype" "C-union" "C-variable"
3782
3783 "above" "abstract" "afterwards" "all"
3784 "begin" "below" "block" "by"
3785 "case" "class" "cleanup" "constant" "create"
3786 "define" "domain"
3787 "else" "elseif" "end" "exception" "export"
3788 "finally" "for" "from" "function"
3789 "generic"
3790 "handler"
3791 "if" "in" "instance" "interface" "iterate"
3792 "keyed-by"
3793 "let" "library" "local"
3794 "macro" "method" "module"
3795 "otherwise"
3796 "profiling"
3797 "select" "slot" "subclass"
3798 "table" "then" "to"
3799 "unless" "until" "use"
3800 "variable" "virtual"
3801 "when" "while"))
3802 (sharp-keywords (mdw-regexps
3803 "all-keys" "key" "next" "rest" "include"
3804 "t" "f")))
3805 (setq font-lock-keywords
3806 (list (list (concat "\\<\\(" dylan-keywords
3807 "\\|" "with\\(out\\)?-" word
3808 "\\)\\>")
3809 '(0 font-lock-keyword-face))
3810 (list (concat "\\<" word ":" "\\|"
3811 "#\\(" sharp-keywords "\\)\\>")
3812 '(0 font-lock-variable-name-face))
3813 (list (concat "\\("
3814 "\\([-+]\\|\\<\\)[0-9]+" "\\("
3815 "\\(\\.[0-9]+\\)?" "\\([eE][-+][0-9]+\\)?"
3816 "\\|" "/[0-9]+"
3817 "\\)"
3818 "\\|" "\\.[0-9]+" "\\([eE][-+][0-9]+\\)?"
3819 "\\|" "#b[01]+"
3820 "\\|" "#o[0-7]+"
3821 "\\|" "#x[0-9a-zA-Z]+"
3822 "\\)\\>")
3823 '(0 mdw-number-face))
3824 (list (concat "\\("
3825 "\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\|"
3826 "\\_<[-+*/=<>:&|]+\\_>"
3827 "\\)")
3828 '(0 mdw-punct-face))))))
3829
3830 (progn
3831 (add-hook 'dylan-mode-hook 'mdw-misc-mode-config t)
3832 (add-hook 'dylan-mode-hook 'mdw-fontify-dylan t))
3833
3834 ;;;--------------------------------------------------------------------------
3835 ;;; Algol 68 configuration.
3836
3837 (setq-default a68-indent-step 2)
3838
3839 (defun mdw-fontify-algol-68 ()
3840
3841 ;; Fix up the syntax table.
3842 (modify-syntax-entry ?# "!" a68-mode-syntax-table)
3843 (dolist (ch '(?- ?+ ?= ?< ?> ?* ?/ ?| ?&))
3844 (modify-syntax-entry ch "." a68-mode-syntax-table))
3845
3846 (make-local-variable 'font-lock-keywords)
3847
3848 (let ((not-comment
3849 (let ((word "COMMENT"))
3850 (do ((regexp (concat "[^" (substring word 0 1) "]+")
3851 (concat regexp "\\|"
3852 (substring word 0 i)
3853 "[^" (substring word i (1+ i)) "]"))
3854 (i 1 (1+ i)))
3855 ((>= i (length word)) regexp)))))
3856 (setq font-lock-keywords
3857 (list (list (concat "\\<COMMENT\\>"
3858 "\\(" not-comment "\\)\\{0,5\\}"
3859 "\\(\\'\\|\\<COMMENT\\>\\)")
3860 '(0 font-lock-comment-face))
3861 (list (concat "\\<CO\\>"
3862 "\\([^C]+\\|C[^O]\\)\\{0,5\\}"
3863 "\\($\\|\\<CO\\>\\)")
3864 '(0 font-lock-comment-face))
3865 (list "\\<[A-Z_]+\\>"
3866 '(0 font-lock-keyword-face))
3867 (list (concat "\\<"
3868 "[0-9]+"
3869 "\\(\\.[0-9]+\\)?"
3870 "\\([eE][-+]?[0-9]+\\)?"
3871 "\\>")
3872 '(0 mdw-number-face))
3873 (list "\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/"
3874 '(0 mdw-punct-face))))))
3875
3876 (dolist (hook '(a68-mode-hook a68-mode-hooks))
3877 (add-hook hook 'mdw-misc-mode-config t)
3878 (add-hook hook 'mdw-fontify-algol-68 t))
3879
3880 ;;;--------------------------------------------------------------------------
3881 ;;; REXX configuration.
3882
3883 (defun mdw-rexx-electric-* ()
3884 (interactive)
3885 (insert ?*)
3886 (rexx-indent-line))
3887
3888 (defun mdw-rexx-indent-newline-indent ()
3889 (interactive)
3890 (rexx-indent-line)
3891 (if abbrev-mode (expand-abbrev))
3892 (newline-and-indent))
3893
3894 (defun mdw-fontify-rexx ()
3895
3896 ;; Various bits of fiddling.
3897 (setq mdw-auto-indent nil)
3898 (local-set-key [?\C-m] 'mdw-rexx-indent-newline-indent)
3899 (local-set-key [?*] 'mdw-rexx-electric-*)
3900 (dolist (ch '(?! ?? ?# ?@ ?$)) (modify-syntax-entry ch "w"))
3901 (dolist (ch '(?¬)) (modify-syntax-entry ch "."))
3902 (mdw-standard-fill-prefix "\\([ \t]*/?\*[ \t]*\\)")
3903
3904 ;; Set up keywords and things for fontification.
3905 (make-local-variable 'font-lock-keywords-case-fold-search)
3906 (setq font-lock-keywords-case-fold-search t)
3907
3908 (setq rexx-indent 2)
3909 (setq rexx-end-indent rexx-indent)
3910 (setq rexx-cont-indent rexx-indent)
3911
3912 (make-local-variable 'font-lock-keywords)
3913 (let ((rexx-keywords
3914 (mdw-regexps "address" "arg" "by" "call" "digits" "do" "drop"
3915 "else" "end" "engineering" "exit" "expose" "for"
3916 "forever" "form" "fuzz" "if" "interpret" "iterate"
3917 "leave" "linein" "name" "nop" "numeric" "off" "on"
3918 "options" "otherwise" "parse" "procedure" "pull"
3919 "push" "queue" "return" "say" "select" "signal"
3920 "scientific" "source" "then" "trace" "to" "until"
3921 "upper" "value" "var" "version" "when" "while"
3922 "with"
3923
3924 "abbrev" "abs" "bitand" "bitor" "bitxor" "b2x"
3925 "center" "center" "charin" "charout" "chars"
3926 "compare" "condition" "copies" "c2d" "c2x"
3927 "datatype" "date" "delstr" "delword" "d2c" "d2x"
3928 "errortext" "format" "fuzz" "insert" "lastpos"
3929 "left" "length" "lineout" "lines" "max" "min"
3930 "overlay" "pos" "queued" "random" "reverse" "right"
3931 "sign" "sourceline" "space" "stream" "strip"
3932 "substr" "subword" "symbol" "time" "translate"
3933 "trunc" "value" "verify" "word" "wordindex"
3934 "wordlength" "wordpos" "words" "xrange" "x2b" "x2c"
3935 "x2d")))
3936
3937 (setq font-lock-keywords
3938 (list
3939
3940 ;; Set up the keywords defined above.
3941 (list (concat "\\<\\(" rexx-keywords "\\)\\>")
3942 '(0 font-lock-keyword-face))
3943
3944 ;; Fontify all symbols the same way.
3945 (list (concat "\\<\\([0-9.][A-Za-z0-9.!?_#@$]*[Ee][+-]?[0-9]+\\|"
3946 "[A-Za-z0-9.!?_#@$]+\\)")
3947 '(0 font-lock-variable-name-face))
3948
3949 ;; And everything else is punctuation.
3950 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3951 '(0 mdw-punct-face))))))
3952
3953 (progn
3954 (add-hook 'rexx-mode-hook 'mdw-misc-mode-config t)
3955 (add-hook 'rexx-mode-hook 'mdw-fontify-rexx t))
3956
3957 ;;;--------------------------------------------------------------------------
3958 ;;; Standard ML programming style.
3959
3960 (setq-default sml-nested-if-indent t
3961 sml-case-indent nil
3962 sml-indent-level 4
3963 sml-type-of-indent nil)
3964
3965 (defun mdw-fontify-sml ()
3966
3967 ;; Make underscore an honorary letter.
3968 (modify-syntax-entry ?' "w")
3969
3970 ;; Set fill prefix.
3971 (mdw-standard-fill-prefix "\\([ \t]*(\*[ \t]*\\)")
3972
3973 ;; Now define fontification things.
3974 (make-local-variable 'font-lock-keywords)
3975 (let ((sml-keywords
3976 (mdw-regexps "abstype" "and" "andalso" "as"
3977 "case"
3978 "datatype" "do"
3979 "else" "end" "eqtype" "exception"
3980 "fn" "fun" "functor"
3981 "handle"
3982 "if" "in" "include" "infix" "infixr"
3983 "let" "local"
3984 "nonfix"
3985 "of" "op" "open" "orelse"
3986 "raise" "rec"
3987 "sharing" "sig" "signature" "struct" "structure"
3988 "then" "type"
3989 "val"
3990 "where" "while" "with" "withtype")))
3991
3992 (setq font-lock-keywords
3993 (list
3994
3995 ;; Set up the keywords defined above.
3996 (list (concat "\\<\\(" sml-keywords "\\)\\>")
3997 '(0 font-lock-keyword-face))
3998
3999 ;; At least numbers are simpler than C.
4000 (list (concat "\\<\\~?"
4001 "\\(0\\([wW]?[xX][0-9a-fA-F]+\\|"
4002 "[wW][0-9]+\\)\\|"
4003 "\\([0-9]+\\(\\.[0-9]+\\)?"
4004 "\\([eE]\\~?"
4005 "[0-9]+\\)?\\)\\)")
4006 '(0 mdw-number-face))
4007
4008 ;; And anything else is punctuation.
4009 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4010 '(0 mdw-punct-face))))))
4011
4012 (progn
4013 (add-hook 'sml-mode-hook 'mdw-misc-mode-config t)
4014 (add-hook 'sml-mode-hook 'mdw-fontify-sml t))
4015
4016 ;;;--------------------------------------------------------------------------
4017 ;;; Haskell configuration.
4018
4019 (setq-default haskell-indent-offset 2)
4020
4021 (defun mdw-fontify-haskell ()
4022
4023 ;; Fiddle with syntax table to get comments right.
4024 (modify-syntax-entry ?' "_")
4025 (modify-syntax-entry ?- ". 12")
4026 (modify-syntax-entry ?\n ">")
4027
4028 ;; Make punctuation be punctuation
4029 (let ((punct "=<>+-*/|&%!@?$.^:#`"))
4030 (do ((i 0 (1+ i)))
4031 ((>= i (length punct)))
4032 (modify-syntax-entry (aref punct i) ".")))
4033
4034 ;; Set fill prefix.
4035 (mdw-standard-fill-prefix "\\([ \t]*{?--?[ \t]*\\)")
4036
4037 ;; Fiddle with fontification.
4038 (make-local-variable 'font-lock-keywords)
4039 (let ((haskell-keywords
4040 (mdw-regexps "as"
4041 "case" "ccall" "class"
4042 "data" "default" "deriving" "do"
4043 "else" "exists"
4044 "forall" "foreign"
4045 "hiding"
4046 "if" "import" "in" "infix" "infixl" "infixr" "instance"
4047 "let"
4048 "mdo" "module"
4049 "newtype"
4050 "of"
4051 "proc"
4052 "qualified"
4053 "rec"
4054 "safe" "stdcall"
4055 "then" "type"
4056 "unsafe"
4057 "where"))
4058 (control-sequences
4059 (mdw-regexps "ACK" "BEL" "BS" "CAN" "CR" "DC1" "DC2" "DC3" "DC4"
4060 "DEL" "DLE" "EM" "ENQ" "EOT" "ESC" "ETB" "ETX" "FF"
4061 "FS" "GS" "HT" "LF" "NAK" "NUL" "RS" "SI" "SO" "SOH"
4062 "SP" "STX" "SUB" "SYN" "US" "VT")))
4063
4064 (setq font-lock-keywords
4065 (list
4066 (list (concat "{-" "[^-]*" "\\(-+[^-}][^-]*\\)*"
4067 "\\(-+}\\|-*\\'\\)"
4068 "\\|"
4069 "--.*$")
4070 '(0 font-lock-comment-face))
4071 (list (concat "\\_<\\(" haskell-keywords "\\)\\_>")
4072 '(0 font-lock-keyword-face))
4073 (list (concat "'\\("
4074 "[^\\]"
4075 "\\|"
4076 "\\\\"
4077 "\\(" "[abfnrtv\\\"']" "\\|"
4078 "^" "\\(" control-sequences "\\|"
4079 "[]A-Z@[\\^_]" "\\)" "\\|"
4080 "\\|"
4081 "[0-9]+" "\\|"
4082 "[oO][0-7]+" "\\|"
4083 "[xX][0-9A-Fa-f]+"
4084 "\\)"
4085 "\\)'")
4086 '(0 font-lock-string-face))
4087 (list "\\_<[A-Z]\\(\\sw+\\|\\s_+\\)*\\_>"
4088 '(0 font-lock-variable-name-face))
4089 (list (concat "\\_<0\\([xX][0-9a-fA-F]+\\|[oO][0-7]+\\)\\|"
4090 "\\_<[0-9]+\\(\\.[0-9]*\\)?"
4091 "\\([eE][-+]?[0-9]+\\)?")
4092 '(0 mdw-number-face))
4093 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4094 '(0 mdw-punct-face))))))
4095
4096 (progn
4097 (add-hook 'haskell-mode-hook 'mdw-misc-mode-config t)
4098 (add-hook 'haskell-mode-hook 'mdw-fontify-haskell t))
4099
4100 ;;;--------------------------------------------------------------------------
4101 ;;; Erlang configuration.
4102
4103 (setq-default erlang-electric-commands nil)
4104
4105 (defun mdw-fontify-erlang ()
4106
4107 ;; Set fill prefix.
4108 (mdw-standard-fill-prefix "\\([ \t]*{?%*[ \t]*\\)")
4109
4110 ;; Fiddle with fontification.
4111 (make-local-variable 'font-lock-keywords)
4112 (let ((erlang-keywords
4113 (mdw-regexps "after" "and" "andalso"
4114 "band" "begin" "bnot" "bor" "bsl" "bsr" "bxor"
4115 "case" "catch" "cond"
4116 "div" "end" "fun" "if" "let" "not"
4117 "of" "or" "orelse"
4118 "query" "receive" "rem" "try" "when" "xor")))
4119
4120 (setq font-lock-keywords
4121 (list
4122 (list "%.*$"
4123 '(0 font-lock-comment-face))
4124 (list (concat "\\<\\(" erlang-keywords "\\)\\>")
4125 '(0 font-lock-keyword-face))
4126 (list (concat "^-\\sw+\\>")
4127 '(0 font-lock-keyword-face))
4128 (list "\\<[0-9]+\\(#[0-9a-zA-Z]+\\|[eE][+-]?[0-9]+\\)?\\>"
4129 '(0 mdw-number-face))
4130 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4131 '(0 mdw-punct-face))))))
4132
4133 (progn
4134 (add-hook 'erlang-mode-hook 'mdw-misc-mode-config t)
4135 (add-hook 'erlang-mode-hook 'mdw-fontify-erlang t))
4136
4137 ;;;--------------------------------------------------------------------------
4138 ;;; Texinfo configuration.
4139
4140 (defun mdw-fontify-texinfo ()
4141
4142 ;; Set fill prefix.
4143 (mdw-standard-fill-prefix "\\([ \t]*@c[ \t]+\\)")
4144
4145 ;; Real fontification things.
4146 (make-local-variable 'font-lock-keywords)
4147 (setq font-lock-keywords
4148 (list
4149
4150 ;; Environment names are keywords.
4151 (list "@\\(end\\) *\\([a-zA-Z]*\\)?"
4152 '(2 font-lock-keyword-face))
4153
4154 ;; Unmark escaped magic characters.
4155 (list "\\(@\\)\\([@{}]\\)"
4156 '(1 font-lock-keyword-face)
4157 '(2 font-lock-variable-name-face))
4158
4159 ;; Make sure we get comments properly.
4160 (list "@c\\(omment\\)?\\( .*\\)?$"
4161 '(0 font-lock-comment-face))
4162
4163 ;; Command names are keywords.
4164 (list "@\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)"
4165 '(0 font-lock-keyword-face))
4166
4167 ;; Fontify TeX special characters as punctuation.
4168 (list "[{}]+"
4169 '(0 mdw-punct-face)))))
4170
4171 (dolist (hook '(texinfo-mode-hook TeXinfo-mode-hook))
4172 (add-hook hook 'mdw-misc-mode-config t)
4173 (add-hook hook 'mdw-fontify-texinfo t))
4174
4175 ;;;--------------------------------------------------------------------------
4176 ;;; TeX and LaTeX configuration.
4177
4178 (setq-default LaTeX-table-label "tbl:"
4179 TeX-auto-untabify nil
4180 LaTeX-syntactic-comments nil
4181 LaTeX-fill-break-at-separators '(\\\[))
4182
4183 (defun mdw-fontify-tex ()
4184 (setq ispell-parser 'tex)
4185 (turn-on-reftex)
4186
4187 ;; Don't make maths into a string.
4188 (modify-syntax-entry ?$ ".")
4189 (modify-syntax-entry ?$ "." font-lock-syntax-table)
4190 (local-set-key [?$] 'self-insert-command)
4191
4192 ;; Make `tab' be useful, given that tab stops in TeX don't work well.
4193 (local-set-key "\C-\M-i" 'indent-relative)
4194 (setq indent-tabs-mode nil)
4195
4196 ;; Set fill prefix.
4197 (mdw-standard-fill-prefix "\\([ \t]*%+[ \t]*\\)")
4198
4199 ;; Real fontification things.
4200 (make-local-variable 'font-lock-keywords)
4201 (setq font-lock-keywords
4202 (list
4203
4204 ;; Environment names are keywords.
4205 (list (concat "\\\\\\(begin\\|end\\|newenvironment\\)"
4206 "{\\([^}\n]*\\)}")
4207 '(2 font-lock-keyword-face))
4208
4209 ;; Suspended environment names are keywords too.
4210 (list (concat "\\\\\\(suspend\\|resume\\)\\(\\[[^]]*\\]\\)?"
4211 "{\\([^}\n]*\\)}")
4212 '(3 font-lock-keyword-face))
4213
4214 ;; Command names are keywords.
4215 (list "\\\\\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)"
4216 '(0 font-lock-keyword-face))
4217
4218 ;; Handle @/.../ for italics.
4219 ;; (list "\\(@/\\)\\([^/]*\\)\\(/\\)"
4220 ;; '(1 font-lock-keyword-face)
4221 ;; '(3 font-lock-keyword-face))
4222
4223 ;; Handle @*...* for boldness.
4224 ;; (list "\\(@\\*\\)\\([^*]*\\)\\(\\*\\)"
4225 ;; '(1 font-lock-keyword-face)
4226 ;; '(3 font-lock-keyword-face))
4227
4228 ;; Handle @`...' for literal syntax things.
4229 ;; (list "\\(@`\\)\\([^']*\\)\\('\\)"
4230 ;; '(1 font-lock-keyword-face)
4231 ;; '(3 font-lock-keyword-face))
4232
4233 ;; Handle @<...> for nonterminals.
4234 ;; (list "\\(@<\\)\\([^>]*\\)\\(>\\)"
4235 ;; '(1 font-lock-keyword-face)
4236 ;; '(3 font-lock-keyword-face))
4237
4238 ;; Handle other @-commands.
4239 ;; (list "@\\([^a-zA-Z]\\|[a-zA-Z]*\\)"
4240 ;; '(0 font-lock-keyword-face))
4241
4242 ;; Make sure we get comments properly.
4243 (list "%.*"
4244 '(0 font-lock-comment-face))
4245
4246 ;; Fontify TeX special characters as punctuation.
4247 (list "[$^_{}#&]"
4248 '(0 mdw-punct-face)))))
4249
4250 (setq TeX-install-font-lock 'tex-font-setup)
4251
4252 (eval-after-load 'font-latex
4253 '(defun font-latex-jit-lock-force-redisplay (buf start end)
4254 "Compatibility for Emacsen not offering `jit-lock-force-redisplay'."
4255 ;; The following block is an expansion of `jit-lock-force-redisplay'
4256 ;; and involved macros taken from CVS Emacs on 2007-04-28.
4257 (with-current-buffer buf
4258 (let ((modified (buffer-modified-p)))
4259 (unwind-protect
4260 (let ((buffer-undo-list t)
4261 (inhibit-read-only t)
4262 (inhibit-point-motion-hooks t)
4263 (inhibit-modification-hooks t)
4264 deactivate-mark
4265 buffer-file-name
4266 buffer-file-truename)
4267 (put-text-property start end 'fontified t))
4268 (unless modified
4269 (restore-buffer-modified-p nil)))))))
4270
4271 (setq TeX-output-view-style
4272 '(("^dvi$"
4273 ("^landscape$" "^pstricks$\\|^pst-\\|^psfrag$")
4274 "%(o?)dvips -t landscape %d -o && xdg-open %f")
4275 ("^dvi$" "^pstricks$\\|^pst-\\|^psfrag$"
4276 "%(o?)dvips %d -o && xdg-open %f")
4277 ("^dvi$"
4278 ("^a4\\(?:dutch\\|paper\\|wide\\)\\|sem-a4$" "^landscape$")
4279 "%(o?)xdvi %dS -paper a4r -s 0 %d")
4280 ("^dvi$" "^a4\\(?:dutch\\|paper\\|wide\\)\\|sem-a4$"
4281 "%(o?)xdvi %dS -paper a4 %d")
4282 ("^dvi$"
4283 ("^a5\\(?:comb\\|paper\\)$" "^landscape$")
4284 "%(o?)xdvi %dS -paper a5r -s 0 %d")
4285 ("^dvi$" "^a5\\(?:comb\\|paper\\)$" "%(o?)xdvi %dS -paper a5 %d")
4286 ("^dvi$" "^b5paper$" "%(o?)xdvi %dS -paper b5 %d")
4287 ("^dvi$" "^letterpaper$" "%(o?)xdvi %dS -paper us %d")
4288 ("^dvi$" "^legalpaper$" "%(o?)xdvi %dS -paper legal %d")
4289 ("^dvi$" "^executivepaper$" "%(o?)xdvi %dS -paper 7.25x10.5in %d")
4290 ("^dvi$" "." "%(o?)xdvi %dS %d")
4291 ("^pdf$" "." "xdg-open %o")
4292 ("^html?$" "." "sensible-browser %o")))
4293
4294 (setq TeX-view-program-list
4295 '(("mupdf" ("mupdf %o" (mode-io-correlate " %(outpage)")))))
4296
4297 (setq TeX-view-program-selection
4298 '(((output-dvi style-pstricks) "dvips and gv")
4299 (output-dvi "xdvi")
4300 (output-pdf "mupdf")
4301 (output-html "sensible-browser")))
4302
4303 (setq TeX-open-quote "\""
4304 TeX-close-quote "\"")
4305
4306 (setq reftex-use-external-file-finders t
4307 reftex-auto-recenter-toc t)
4308
4309 (setq reftex-label-alist
4310 '(("theorem" ?T "th:" "~\\ref{%s}" t ("theorems?" "th\\.") -2)
4311 ("axiom" ?A "ax:" "~\\ref{%s}" t ("axioms?" "ax\\.") -2)
4312 ("definition" ?D "def:" "~\\ref{%s}" t ("definitions?" "def\\.") -2)
4313 ("proposition" ?P "prop:" "~\\ref{%s}" t
4314 ("propositions?" "prop\\.") -2)
4315 ("lemma" ?L "lem:" "~\\ref{%s}" t ("lemmas?" "lem\\.") -2)
4316 ("example" ?X "eg:" "~\\ref{%s}" t ("examples?") -2)
4317 ("exercise" ?E "ex:" "~\\ref{%s}" t ("exercises?" "ex\\.") -2)
4318 ("enumerate" ?i "i:" "~\\ref{%s}" item ("items?"))))
4319 (setq reftex-section-prefixes
4320 '((0 . "part:")
4321 (1 . "ch:")
4322 (t . "sec:")))
4323
4324 (setq bibtex-field-delimiters 'double-quotes
4325 bibtex-align-at-equal-sign t
4326 bibtex-entry-format '(realign opts-or-alts required-fields
4327 numerical-fields last-comma delimiters
4328 unify-case sort-fields braces)
4329 bibtex-sort-ignore-string-entries nil
4330 bibtex-maintain-sorted-entries 'entry-class
4331 bibtex-include-OPTkey t
4332 bibtex-autokey-names-stretch 1
4333 bibtex-autokey-expand-strings t
4334 bibtex-autokey-name-separator "-"
4335 bibtex-autokey-year-length 4
4336 bibtex-autokey-titleword-separator "-"
4337 bibtex-autokey-name-year-separator "-"
4338 bibtex-autokey-year-title-separator ":")
4339
4340 (progn
4341 (dolist (hook '(tex-mode-hook latex-mode-hook
4342 TeX-mode-hook LaTeX-mode-hook))
4343 (add-hook hook 'mdw-misc-mode-config t)
4344 (add-hook hook 'mdw-fontify-tex t))
4345 (add-hook 'bibtex-mode-hook (lambda () (setq fill-column 76))))
4346
4347 ;;;--------------------------------------------------------------------------
4348 ;;; HTML, CSS, and other web foolishness.
4349
4350 (setq-default css-indent-offset 8)
4351
4352 ;;;--------------------------------------------------------------------------
4353 ;;; SGML hacking.
4354
4355 (setq-default psgml-html-build-new-buffer nil)
4356
4357 (defun mdw-sgml-mode ()
4358 (interactive)
4359 (sgml-mode)
4360 (mdw-standard-fill-prefix "")
4361 (make-local-variable 'sgml-delimiters)
4362 (setq sgml-delimiters
4363 '("AND" "&" "COM" "--" "CRO" "&#" "DSC" "]" "DSO" "[" "DTGC" "]"
4364 "DTGO" "[" "ERO" "&" "ETAGO" ":e" "GRPC" ")" "GRPO" "(" "LIT"
4365 "\"" "LITA" "'" "MDC" ">" "MDO" "<!" "MINUS" "-" "MSC" "]]"
4366 "NESTC" "{" "NET" "}" "OPT" "?" "OR" "|" "PERO" "%" "PIC" ">"
4367 "PIO" "<?" "PLUS" "+" "REFC" "." "REP" "*" "RNI" "#" "SEQ" ","
4368 "STAGO" ":" "TAGC" "." "VI" "=" "MS-START" "<![" "MS-END" "]]>"
4369 "XML-ECOM" "-->" "XML-PIC" "?>" "XML-SCOM" "<!--" "XML-TAGCE"
4370 "/>" "NULL" ""))
4371 (setq major-mode 'mdw-sgml-mode)
4372 (setq mode-name "[mdw] SGML")
4373 (run-hooks 'mdw-sgml-mode-hook))
4374
4375 ;;;--------------------------------------------------------------------------
4376 ;;; Configuration files.
4377
4378 (defcustom mdw-conf-quote-normal nil
4379 "Control syntax category of quote characters `\"' and `''.
4380 If this is `t', consider quote characters to be normal
4381 punctuation, as for `conf-quote-normal'. If this is `nil' then
4382 leave quote characters as quotes. If this is a list, then
4383 consider the quote characters in the list to be normal
4384 punctuation. If this is a single quote character, then consider
4385 that character only to be normal punctuation."
4386 :type '(choice boolean character (repeat character))
4387 :safe 'mdw-conf-quote-normal-acceptable-value-p)
4388 (defun mdw-conf-quote-normal-acceptable-value-p (value)
4389 "Is the VALUE is an acceptable value for `mdw-conf-quote-normal'?"
4390 (or (booleanp value)
4391 (every (lambda (v) (memq v '(?\" ?')))
4392 (if (listp value) value (list value)))))
4393
4394 (defun mdw-fix-up-quote ()
4395 "Apply the setting of `mdw-conf-quote-normal'."
4396 (let ((flag mdw-conf-quote-normal))
4397 (cond ((eq flag t)
4398 (conf-quote-normal t))
4399 ((not flag)
4400 nil)
4401 (t
4402 (let ((table (copy-syntax-table (syntax-table))))
4403 (dolist (ch (if (listp flag) flag (list flag)))
4404 (modify-syntax-entry ch "." table))
4405 (set-syntax-table table)
4406 (and font-lock-mode (font-lock-fontify-buffer)))))))
4407
4408 (progn
4409 (add-hook 'conf-mode-hook 'mdw-misc-mode-config t)
4410 (add-hook 'conf-mode-local-variables-hook 'mdw-fix-up-quote t t))
4411
4412 ;;;--------------------------------------------------------------------------
4413 ;;; Shell scripts.
4414
4415 (defun mdw-setup-sh-script-mode ()
4416
4417 ;; Fetch the shell interpreter's name.
4418 (let ((shell-name sh-shell-file))
4419
4420 ;; Try reading the hash-bang line.
4421 (save-excursion
4422 (goto-char (point-min))
4423 (if (looking-at "#![ \t]*\\([^ \t\n]*\\)")
4424 (setq shell-name (match-string 1))))
4425
4426 ;; Now try to set the shell.
4427 ;;
4428 ;; Don't let `sh-set-shell' bugger up my script.
4429 (let ((executable-set-magic #'(lambda (s &rest r) s)))
4430 (sh-set-shell shell-name)))
4431
4432 ;; Don't insert here-document scaffolding automatically.
4433 (local-set-key "<" 'self-insert-command)
4434
4435 ;; Now enable my keys and the fontification.
4436 (mdw-misc-mode-config)
4437
4438 ;; Set the indentation level correctly.
4439 (setq sh-indentation 2)
4440 (setq sh-basic-offset 2))
4441
4442 (setq sh-shell-file "/bin/sh")
4443
4444 ;; Awful hacking to override the shell detection for particular scripts.
4445 (defmacro define-custom-shell-mode (name shell)
4446 `(defun ,name ()
4447 (interactive)
4448 (set (make-local-variable 'sh-shell-file) ,shell)
4449 (sh-mode)))
4450 (define-custom-shell-mode bash-mode "/bin/bash")
4451 (define-custom-shell-mode rc-mode "/usr/bin/rc")
4452 (put 'sh-shell-file 'permanent-local t)
4453
4454 ;; Hack the rc syntax table. Backquotes aren't paired in rc.
4455 (eval-after-load "sh-script"
4456 '(or (assq 'rc sh-mode-syntax-table-input)
4457 (let ((frag '(nil
4458 ?# "<"
4459 ?\n ">#"
4460 ?\" "\"\""
4461 ?\' "\"\'"
4462 ?$ "'"
4463 ?\` "."
4464 ?! "_"
4465 ?% "_"
4466 ?. "_"
4467 ?^ "_"
4468 ?~ "_"
4469 ?, "_"
4470 ?= "."
4471 ?< "."
4472 ?> "."))
4473 (assoc (assq 'rc sh-mode-syntax-table-input)))
4474 (if assoc
4475 (rplacd assoc frag)
4476 (setq sh-mode-syntax-table-input
4477 (cons (cons 'rc frag)
4478 sh-mode-syntax-table-input))))))
4479
4480 (progn
4481 (add-hook 'sh-mode-hook 'mdw-misc-mode-config t)
4482 (add-hook 'sh-mode-hook 'mdw-setup-sh-script-mode t))
4483
4484 ;;;--------------------------------------------------------------------------
4485 ;;; Emacs shell mode.
4486
4487 (defun mdw-eshell-prompt ()
4488 (let ((left "[") (right "]"))
4489 (when (= (user-uid) 0)
4490 (setq left "«" right "»"))
4491 (concat left
4492 (save-match-data
4493 (replace-regexp-in-string "\\..*$" "" (system-name)))
4494 " "
4495 (let* ((pwd (eshell/pwd)) (npwd (length pwd))
4496 (home (expand-file-name "~")) (nhome (length home)))
4497 (if (and (>= npwd nhome)
4498 (or (= nhome npwd)
4499 (= (elt pwd nhome) ?/))
4500 (string= (substring pwd 0 nhome) home))
4501 (concat "~" (substring pwd (length home)))
4502 pwd))
4503 right)))
4504 (setq-default eshell-prompt-function 'mdw-eshell-prompt)
4505 (setq-default eshell-prompt-regexp "^\\[[^]>]+\\(\\]\\|>>?\\)")
4506
4507 (defun eshell/e (file) (find-file file) nil)
4508 (defun eshell/ee (file) (find-file-other-window file) nil)
4509 (defun eshell/w3m (url) (w3m-goto-url url) nil)
4510
4511 (mdw-define-face eshell-prompt (t :weight bold))
4512 (mdw-define-face eshell-ls-archive (t :weight bold :foreground "red"))
4513 (mdw-define-face eshell-ls-backup (t :foreground "lightgrey" :slant italic))
4514 (mdw-define-face eshell-ls-product (t :foreground "lightgrey" :slant italic))
4515 (mdw-define-face eshell-ls-clutter (t :foreground "lightgrey" :slant italic))
4516 (mdw-define-face eshell-ls-executable (t :weight bold))
4517 (mdw-define-face eshell-ls-directory (t :foreground "cyan" :weight bold))
4518 (mdw-define-face eshell-ls-readonly (t nil))
4519 (mdw-define-face eshell-ls-symlink (t :foreground "cyan"))
4520
4521 (defun mdw-eshell-hack () (setenv "LD_PRELOAD" nil))
4522 (add-hook 'eshell-mode-hook 'mdw-eshell-hack)
4523
4524 ;;;--------------------------------------------------------------------------
4525 ;;; Messages-file mode.
4526
4527 (defun messages-mode-guts ()
4528 (setq messages-mode-syntax-table (make-syntax-table))
4529 (set-syntax-table messages-mode-syntax-table)
4530 (modify-syntax-entry ?0 "w" messages-mode-syntax-table)
4531 (modify-syntax-entry ?1 "w" messages-mode-syntax-table)
4532 (modify-syntax-entry ?2 "w" messages-mode-syntax-table)
4533 (modify-syntax-entry ?3 "w" messages-mode-syntax-table)
4534 (modify-syntax-entry ?4 "w" messages-mode-syntax-table)
4535 (modify-syntax-entry ?5 "w" messages-mode-syntax-table)
4536 (modify-syntax-entry ?6 "w" messages-mode-syntax-table)
4537 (modify-syntax-entry ?7 "w" messages-mode-syntax-table)
4538 (modify-syntax-entry ?8 "w" messages-mode-syntax-table)
4539 (modify-syntax-entry ?9 "w" messages-mode-syntax-table)
4540 (make-local-variable 'comment-start)
4541 (make-local-variable 'comment-end)
4542 (make-local-variable 'indent-line-function)
4543 (setq indent-line-function 'indent-relative)
4544 (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
4545 (make-local-variable 'font-lock-defaults)
4546 (make-local-variable 'messages-mode-keywords)
4547 (let ((keywords
4548 (mdw-regexps "array" "bitmap" "callback" "docs[ \t]+enum"
4549 "export" "enum" "fixed-octetstring" "flags"
4550 "harmless" "map" "nested" "optional"
4551 "optional-tagged" "package" "primitive"
4552 "primitive-nullfree" "relaxed[ \t]+enum"
4553 "set" "table" "tagged-optional" "union"
4554 "variadic" "vector" "version" "version-tag")))
4555 (setq messages-mode-keywords
4556 (list
4557 (list (concat "\\<\\(" keywords "\\)\\>:")
4558 '(0 font-lock-keyword-face))
4559 '("\\([-a-zA-Z0-9]+:\\)" (0 font-lock-warning-face))
4560 '("\\(\\<[a-z][-_a-zA-Z0-9]*\\)"
4561 (0 font-lock-variable-name-face))
4562 '("\\<\\([0-9]+\\)\\>" (0 mdw-number-face))
4563 '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4564 (0 mdw-punct-face)))))
4565 (setq font-lock-defaults
4566 '(messages-mode-keywords nil nil nil nil))
4567 (run-hooks 'messages-file-hook))
4568
4569 (defun messages-mode ()
4570 (interactive)
4571 (fundamental-mode)
4572 (setq major-mode 'messages-mode)
4573 (setq mode-name "Messages")
4574 (messages-mode-guts)
4575 (modify-syntax-entry ?# "<" messages-mode-syntax-table)
4576 (modify-syntax-entry ?\n ">" messages-mode-syntax-table)
4577 (setq comment-start "# ")
4578 (setq comment-end "")
4579 (run-hooks 'messages-mode-hook))
4580
4581 (defun cpp-messages-mode ()
4582 (interactive)
4583 (fundamental-mode)
4584 (setq major-mode 'cpp-messages-mode)
4585 (setq mode-name "CPP Messages")
4586 (messages-mode-guts)
4587 (modify-syntax-entry ?* ". 23" messages-mode-syntax-table)
4588 (modify-syntax-entry ?/ ". 14" messages-mode-syntax-table)
4589 (setq comment-start "/* ")
4590 (setq comment-end " */")
4591 (let ((preprocessor-keywords
4592 (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
4593 "ident" "if" "ifdef" "ifndef" "import" "include"
4594 "line" "pragma" "unassert" "undef" "warning")))
4595 (setq messages-mode-keywords
4596 (append (list (list (concat "^[ \t]*\\#[ \t]*"
4597 "\\(include\\|import\\)"
4598 "[ \t]*\\(<[^>]+\\(>\\)?\\)")
4599 '(2 font-lock-string-face))
4600 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
4601 preprocessor-keywords
4602 "\\)\\>\\|[0-9]+\\|$\\)\\)")
4603 '(1 font-lock-keyword-face)))
4604 messages-mode-keywords)))
4605 (run-hooks 'cpp-messages-mode-hook))
4606
4607 (progn
4608 (add-hook 'messages-mode-hook 'mdw-misc-mode-config t)
4609 (add-hook 'cpp-messages-mode-hook 'mdw-misc-mode-config t)
4610 ;; (add-hook 'messages-file-hook 'mdw-fontify-messages t)
4611 )
4612
4613 ;;;--------------------------------------------------------------------------
4614 ;;; Messages-file mode.
4615
4616 (defvar mallow-driver-substitution-face 'mallow-driver-substitution-face
4617 "Face to use for subsittution directives.")
4618 (make-face 'mallow-driver-substitution-face)
4619 (defvar mallow-driver-text-face 'mallow-driver-text-face
4620 "Face to use for body text.")
4621 (make-face 'mallow-driver-text-face)
4622
4623 (defun mallow-driver-mode ()
4624 (interactive)
4625 (fundamental-mode)
4626 (setq major-mode 'mallow-driver-mode)
4627 (setq mode-name "Mallow driver")
4628 (setq mallow-driver-mode-syntax-table (make-syntax-table))
4629 (set-syntax-table mallow-driver-mode-syntax-table)
4630 (make-local-variable 'comment-start)
4631 (make-local-variable 'comment-end)
4632 (make-local-variable 'indent-line-function)
4633 (setq indent-line-function 'indent-relative)
4634 (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
4635 (make-local-variable 'font-lock-defaults)
4636 (make-local-variable 'mallow-driver-mode-keywords)
4637 (let ((keywords
4638 (mdw-regexps "each" "divert" "file" "if"
4639 "perl" "set" "string" "type" "write")))
4640 (setq mallow-driver-mode-keywords
4641 (list
4642 (list (concat "^%\\s *\\(}\\|\\(" keywords "\\)\\>\\).*$")
4643 '(0 font-lock-keyword-face))
4644 (list "^%\\s *\\(#.*\\)?$"
4645 '(0 font-lock-comment-face))
4646 (list "^%"
4647 '(0 font-lock-keyword-face))
4648 (list "^|?\\(.+\\)$" '(1 mallow-driver-text-face))
4649 (list "\\${[^}]*}"
4650 '(0 mallow-driver-substitution-face t)))))
4651 (setq font-lock-defaults
4652 '(mallow-driver-mode-keywords nil nil nil nil))
4653 (modify-syntax-entry ?\" "_" mallow-driver-mode-syntax-table)
4654 (modify-syntax-entry ?\n ">" mallow-driver-mode-syntax-table)
4655 (setq comment-start "%# ")
4656 (setq comment-end "")
4657 (run-hooks 'mallow-driver-mode-hook))
4658
4659 (progn
4660 (add-hook 'mallow-driver-hook 'mdw-misc-mode-config t))
4661
4662 ;;;--------------------------------------------------------------------------
4663 ;;; NFast debugs.
4664
4665 (defun nfast-debug-mode ()
4666 (interactive)
4667 (fundamental-mode)
4668 (setq major-mode 'nfast-debug-mode)
4669 (setq mode-name "NFast debug")
4670 (setq messages-mode-syntax-table (make-syntax-table))
4671 (set-syntax-table messages-mode-syntax-table)
4672 (make-local-variable 'font-lock-defaults)
4673 (make-local-variable 'nfast-debug-mode-keywords)
4674 (setq truncate-lines t)
4675 (setq nfast-debug-mode-keywords
4676 (list
4677 '("^\\(NFast_\\(Connect\\|Disconnect\\|Submit\\|Wait\\)\\)"
4678 (0 font-lock-keyword-face))
4679 (list (concat "^[ \t]+\\(\\("
4680 "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
4681 "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
4682 "[ \t]+\\)*"
4683 "[0-9a-fA-F]+\\)[ \t]*$")
4684 '(0 mdw-number-face))
4685 '("^[ \t]+\.status=[ \t]+\\<\\(OK\\)\\>"
4686 (1 font-lock-keyword-face))
4687 '("^[ \t]+\.status=[ \t]+\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>"
4688 (1 font-lock-warning-face))
4689 '("^[ \t]+\.status[ \t]+\\<\\(zero\\)\\>"
4690 (1 nil))
4691 (list (concat "^[ \t]+\\.cmd=[ \t]+"
4692 "\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>")
4693 '(1 font-lock-keyword-face))
4694 '("-?\\<\\([0-9]+\\|0x[0-9a-fA-F]+\\)\\>" (0 mdw-number-face))
4695 '("^\\([ \t]+[a-z0-9.]+\\)" (0 font-lock-variable-name-face))
4696 '("\\<\\([a-z][a-z0-9.]+\\)\\>=" (1 font-lock-variable-name-face))
4697 '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" (0 mdw-punct-face))))
4698 (setq font-lock-defaults
4699 '(nfast-debug-mode-keywords nil nil nil nil))
4700 (run-hooks 'nfast-debug-mode-hook))
4701
4702 ;;;--------------------------------------------------------------------------
4703 ;;; Lispy languages.
4704
4705 ;; Unpleasant bodge.
4706 (unless (boundp 'slime-repl-mode-map)
4707 (setq slime-repl-mode-map (make-sparse-keymap)))
4708
4709 (defun mdw-indent-newline-and-indent ()
4710 (interactive)
4711 (indent-for-tab-command)
4712 (newline-and-indent))
4713
4714 (eval-after-load "cl-indent"
4715 '(progn
4716 (mapc #'(lambda (pair)
4717 (put (car pair)
4718 'common-lisp-indent-function
4719 (cdr pair)))
4720 '((destructuring-bind . ((&whole 4 &rest 1) 4 &body))
4721 (multiple-value-bind . ((&whole 4 &rest 1) 4 &body))))))
4722
4723 (defun mdw-common-lisp-indent ()
4724 (make-local-variable 'lisp-indent-function)
4725 (setq lisp-indent-function 'common-lisp-indent-function))
4726
4727 (defmacro mdw-advise-hyperspec-lookup (func args)
4728 `(defadvice ,func (around mdw-browse-w3m ,args activate compile)
4729 (if (fboundp 'w3m)
4730 (let ((browse-url-browser-function #'mdw-w3m-browse-url))
4731 ad-do-it)
4732 ad-do-it)))
4733 (mdw-advise-hyperspec-lookup common-lisp-hyperspec (symbol))
4734 (mdw-advise-hyperspec-lookup common-lisp-hyperspec-format (char))
4735 (mdw-advise-hyperspec-lookup common-lisp-hyperspec-lookup-reader-macro (char))
4736
4737 (defun mdw-fontify-lispy ()
4738
4739 ;; Set fill prefix.
4740 (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)")
4741
4742 ;; Not much fontification needed.
4743 (make-local-variable 'font-lock-keywords)
4744 (setq font-lock-keywords
4745 (list (list (concat "\\("
4746 "\\_<[-+]?"
4747 "\\(" "[0-9]+/[0-9]+"
4748 "\\|" "\\(" "[0-9]+" "\\(\\.[0-9]*\\)?" "\\|"
4749 "\\.[0-9]+" "\\)"
4750 "\\([dDeEfFlLsS][-+]?[0-9]+\\)?"
4751 "\\)"
4752 "\\|"
4753 "#"
4754 "\\(" "x" "[-+]?"
4755 "[0-9A-Fa-f]+" "\\(/[0-9A-Fa-f]+\\)?"
4756 "\\|" "o" "[-+]?" "[0-7]+" "\\(/[0-7]+\\)?"
4757 "\\|" "b" "[-+]?" "[01]+" "\\(/[01]+\\)?"
4758 "\\|" "[0-9]+" "r" "[-+]?"
4759 "[0-9a-zA-Z]+" "\\(/[0-9a-zA-Z]+\\)?"
4760 "\\)"
4761 "\\)\\_>")
4762 '(0 mdw-number-face))
4763 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4764 '(0 mdw-punct-face)))))
4765
4766 ;; Special indentation.
4767
4768 (defcustom mdw-lisp-loop-default-indent 2
4769 "Default indent for simple `loop' body."
4770 :type 'integer
4771 :safe 'integerp)
4772 (defcustom mdw-lisp-setf-value-indent 2
4773 "Default extra indent for `setf' values."
4774 :type 'integer :safe 'integerp)
4775
4776 (setq lisp-simple-loop-indentation 0
4777 lisp-loop-keyword-indentation 0
4778 lisp-loop-forms-indentation 2
4779 lisp-lambda-list-keyword-parameter-alignment t)
4780
4781 (defun mdw-indent-funcall
4782 (path state &optional indent-point sexp-column normal-indent)
4783 "Indent `funcall' more usefully.
4784 Essentially, treat `funcall foo' as a function name, and align the arguments
4785 to `foo'."
4786 (and (or (not (consp path)) (null (cadr path)))
4787 (save-excursion
4788 (goto-char (cadr state))
4789 (forward-char 1)
4790 (let ((start-line (line-number-at-pos)))
4791 (and (condition-case nil (progn (forward-sexp 3) t)
4792 (scan-error nil))
4793 (progn
4794 (forward-sexp -1)
4795 (and (= start-line (line-number-at-pos))
4796 (current-column))))))))
4797 (progn
4798 (put 'funcall 'common-lisp-indent-function 'mdw-indent-funcall)
4799 (put 'funcall 'lisp-indent-function 'mdw-indent-funcall))
4800
4801 (defun mdw-indent-setf
4802 (path state &optional indent-point sexp-column normal-indent)
4803 "Indent `setf' more usefully.
4804 If the values aren't on the same lines as their variables then indent them
4805 by `mdw-lisp-setf-value-indent' spaces."
4806 (and (or (not (consp path)) (null (cadr path)))
4807 (let ((basic-indent (save-excursion
4808 (goto-char (cadr state))
4809 (forward-char 1)
4810 (and (condition-case nil
4811 (progn (forward-sexp 2) t)
4812 (scan-error nil))
4813 (progn
4814 (forward-sexp -1)
4815 (current-column)))))
4816 (offset (if (consp path) (car path)
4817 (catch 'done
4818 (save-excursion
4819 (let ((start path)
4820 (count 0))
4821 (goto-char (cadr state))
4822 (forward-char 1)
4823 (while (< (point) start)
4824 (condition-case nil (forward-sexp 1)
4825 (scan-error (throw 'done nil)))
4826 (incf count))
4827 (1- count)))))))
4828 (and basic-indent offset
4829 (list (+ basic-indent
4830 (if (oddp offset) 0
4831 mdw-lisp-setf-value-indent))
4832 basic-indent)))))
4833 (progn
4834 (put 'setf 'common-lisp-indent-functopion 'mdw-indent-setf)
4835 (put 'psetf 'common-lisp-indent-function 'mdw-indent-setf)
4836 (put 'setq 'common-lisp-indent-function 'mdw-indent-setf)
4837 (put 'setf 'lisp-indent-function 'mdw-indent-setf)
4838 (put 'setq 'lisp-indent-function 'mdw-indent-setf)
4839 (put 'setq-local 'lisp-indent-function 'mdw-indent-setf)
4840 (put 'setq-default 'lisp-indent-function 'mdw-indent-setf))
4841
4842 (defadvice common-lisp-loop-part-indentation
4843 (around mdw-fix-loop-indentation (indent-point state) activate compile)
4844 "Improve `loop' indentation.
4845 If the first subform is on the same line as the `loop' keyword, then
4846 align the other subforms beneath it. Otherwise, indent them
4847 `mdw-lisp-loop-default-indent' columns in from the opening parenthesis."
4848
4849 (let* ((loop-indentation (save-excursion
4850 (goto-char (elt state 1))
4851 (current-column))))
4852
4853 ;; Don't really care about this.
4854 (when (and (boundp 'lisp-indent-backquote-substitution-mode)
4855 (eq lisp-indent-backquote-substitution-mode 'corrected))
4856 (save-excursion
4857 (goto-char (elt state 1))
4858 (incf loop-indentation
4859 (cond ((eq (char-before) ?,) -1)
4860 ((and (eq (char-before) ?@)
4861 (progn (backward-char)
4862 (eq (char-before) ?,)))
4863 -2)
4864 (t 0)))))
4865
4866 ;; If the first loop item is on the same line as the `loop' itself then
4867 ;; use that as the baseline. Otherwise advance by the default indent.
4868 (goto-char (cadr state))
4869 (forward-char 1)
4870 (let ((baseline-indent
4871 (if (= (line-number-at-pos)
4872 (if (condition-case nil (progn (forward-sexp 2) t)
4873 (scan-error nil))
4874 (progn (forward-sexp -1) (line-number-at-pos))
4875 -1))
4876 (current-column)
4877 (+ loop-indentation mdw-lisp-loop-default-indent))))
4878
4879 (goto-char indent-point)
4880 (beginning-of-line)
4881
4882 (setq ad-return-value
4883 (list
4884 (cond ((condition-case ()
4885 (save-excursion
4886 (goto-char (elt state 1))
4887 (forward-char 1)
4888 (forward-sexp 2)
4889 (backward-sexp 1)
4890 (not (looking-at "\\(:\\|\\sw\\)")))
4891 (error nil))
4892 (+ baseline-indent lisp-simple-loop-indentation))
4893 ((looking-at "^\\s-*\\(:?\\sw+\\|;\\)")
4894 (+ baseline-indent lisp-loop-keyword-indentation))
4895 (t
4896 (+ baseline-indent lisp-loop-forms-indentation)))
4897
4898 ;; Tell the caller that the next line needs recomputation,
4899 ;; even though it doesn't start a sexp.
4900 loop-indentation)))))
4901
4902 ;; SLIME setup.
4903
4904 (defcustom mdw-friendly-name "[mdw]"
4905 "How I want to be addressed."
4906 :type 'string
4907 :safe 'stringp)
4908 (defadvice slime-user-first-name
4909 (around mdw-use-friendly-name compile activate)
4910 (if mdw-friendly-name (setq ad-return-value mdw-friendly-name)
4911 ad-do-it))
4912
4913 (eval-and-compile
4914 (trap
4915 (if (not mdw-fast-startup)
4916 (progn
4917 (require 'slime-autoloads)
4918 (slime-setup '(slime-autodoc slime-c-p-c))))))
4919
4920 (let ((stuff '((cmucl ("cmucl"))
4921 (sbcl ("sbcl") :coding-system utf-8-unix)
4922 (clisp ("clisp") :coding-system utf-8-unix))))
4923 (or (boundp 'slime-lisp-implementations)
4924 (setq slime-lisp-implementations nil))
4925 (while stuff
4926 (let* ((head (car stuff))
4927 (found (assq (car head) slime-lisp-implementations)))
4928 (setq stuff (cdr stuff))
4929 (if found
4930 (rplacd found (cdr head))
4931 (setq slime-lisp-implementations
4932 (cons head slime-lisp-implementations))))))
4933 (setq slime-default-lisp 'sbcl)
4934
4935 ;; Hooks.
4936
4937 (progn
4938 (dolist (hook '(emacs-lisp-mode-hook
4939 scheme-mode-hook
4940 lisp-mode-hook
4941 inferior-lisp-mode-hook
4942 lisp-interaction-mode-hook
4943 ielm-mode-hook
4944 slime-repl-mode-hook))
4945 (add-hook hook 'mdw-misc-mode-config t)
4946 (add-hook hook 'mdw-fontify-lispy t))
4947 (add-hook 'lisp-mode-hook 'mdw-common-lisp-indent t)
4948 (add-hook 'inferior-lisp-mode-hook
4949 #'(lambda () (local-set-key "\C-m" 'comint-send-and-indent)) t))
4950
4951 ;;;--------------------------------------------------------------------------
4952 ;;; Other languages.
4953
4954 ;; Smalltalk.
4955
4956 (defun mdw-setup-smalltalk ()
4957 (and mdw-auto-indent
4958 (local-set-key "\C-m" 'smalltalk-newline-and-indent))
4959 (make-local-variable 'mdw-auto-indent)
4960 (setq mdw-auto-indent nil)
4961 (local-set-key "\C-i" 'smalltalk-reindent))
4962
4963 (defun mdw-fontify-smalltalk ()
4964 (make-local-variable 'font-lock-keywords)
4965 (setq font-lock-keywords
4966 (list
4967 (list "\\<[A-Z][a-zA-Z0-9]*\\>"
4968 '(0 font-lock-keyword-face))
4969 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
4970 "[0-9][0-9_]*\\(\\.[0-9_]*\\)?"
4971 "\\([eE][-+]?[0-9_]+\\)?")
4972 '(0 mdw-number-face))
4973 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4974 '(0 mdw-punct-face)))))
4975
4976 (progn
4977 (add-hook 'smalltalk-mode 'mdw-misc-mode-config t)
4978 (add-hook 'smalltalk-mode 'mdw-fontify-smalltalk t))
4979
4980 ;; m4.
4981
4982 (defun mdw-setup-m4 ()
4983
4984 ;; Inexplicably, Emacs doesn't match braces in m4 mode. This is very
4985 ;; annoying: fix it.
4986 (modify-syntax-entry ?{ "(")
4987 (modify-syntax-entry ?} ")")
4988
4989 ;; Fill prefix.
4990 (mdw-standard-fill-prefix "\\([ \t]*\\(?:#+\\|\\<dnl\\>\\)[ \t]*\\)"))
4991
4992 (dolist (hook '(m4-mode-hook autoconf-mode-hook autotest-mode-hook))
4993 (add-hook hook #'mdw-misc-mode-config t)
4994 (add-hook hook #'mdw-setup-m4 t))
4995
4996 ;; Make.
4997
4998 (progn
4999 (add-hook 'makefile-mode-hook 'mdw-misc-mode-config t))
5000
5001 ;;;--------------------------------------------------------------------------
5002 ;;; Text mode.
5003
5004 (defun mdw-text-mode ()
5005 (setq fill-column 72)
5006 (flyspell-mode t)
5007 (mdw-standard-fill-prefix
5008 "\\([ \t]*\\([>#|:] ?\\)*[ \t]*\\)" 3)
5009 (auto-fill-mode 1))
5010
5011 (eval-after-load "flyspell"
5012 '(define-key flyspell-mode-map "\C-\M-i" nil))
5013
5014 (progn
5015 (add-hook 'text-mode-hook 'mdw-text-mode t))
5016
5017 ;;;--------------------------------------------------------------------------
5018 ;;; Outline and hide/show modes.
5019
5020 (defun mdw-outline-collapse-all ()
5021 "Completely collapse everything in the entire buffer."
5022 (interactive)
5023 (save-excursion
5024 (goto-char (point-min))
5025 (while (< (point) (point-max))
5026 (hide-subtree)
5027 (forward-line))))
5028
5029 (setq hs-hide-comments-when-hiding-all nil)
5030
5031 (defadvice hs-hide-all (after hide-first-comment activate)
5032 (save-excursion (hs-hide-initial-comment-block)))
5033
5034 ;;;--------------------------------------------------------------------------
5035 ;;; Shell mode.
5036
5037 (defun mdw-sh-mode-setup ()
5038 (local-set-key [?\C-a] 'comint-bol)
5039 (add-hook 'comint-output-filter-functions
5040 'comint-watch-for-password-prompt))
5041
5042 (defun mdw-term-mode-setup ()
5043 (setq term-prompt-regexp shell-prompt-pattern)
5044 (make-local-variable 'mouse-yank-at-point)
5045 (make-local-variable 'transient-mark-mode)
5046 (setq mouse-yank-at-point t)
5047 (auto-fill-mode -1)
5048 (setq tab-width 8))
5049
5050 (defun comint-send-and-indent ()
5051 (interactive)
5052 (comint-send-input)
5053 (and mdw-auto-indent
5054 (indent-for-tab-command)))
5055
5056 (defadvice comint-line-beginning-position
5057 (around mdw-calculate-it-properly () activate compile)
5058 "Calculate the actual line start for multi-line input."
5059 (if (or comint-use-prompt-regexp
5060 (eq (field-at-pos (point)) 'output))
5061 ad-do-it
5062 (setq ad-return-value
5063 (constrain-to-field (line-beginning-position) (point)))))
5064
5065 (defun term-send-meta-right () (interactive) (term-send-raw-string "\e\e[C"))
5066 (defun term-send-meta-left () (interactive) (term-send-raw-string "\e\e[D"))
5067 (defun term-send-ctrl-uscore () (interactive) (term-send-raw-string "\C-_"))
5068 (defun term-send-meta-meta-something ()
5069 (interactive)
5070 (term-send-raw-string "\e\e")
5071 (term-send-raw))
5072 (eval-after-load 'term
5073 '(progn
5074 (define-key term-raw-map [?\e ?\e] nil)
5075 (define-key term-raw-map [?\e ?\e t] 'term-send-meta-meta-something)
5076 (define-key term-raw-map [?\C-/] 'term-send-ctrl-uscore)
5077 (define-key term-raw-map [M-right] 'term-send-meta-right)
5078 (define-key term-raw-map [?\e ?\M-O ?C] 'term-send-meta-right)
5079 (define-key term-raw-map [M-left] 'term-send-meta-left)
5080 (define-key term-raw-map [?\e ?\M-O ?D] 'term-send-meta-left)))
5081
5082 (defadvice term-exec (before program-args-list compile activate)
5083 "If the PROGRAM argument is a list, interpret it as (PROGRAM . SWITCHES).
5084 This allows you to pass a list of arguments through `ansi-term'."
5085 (let ((program (ad-get-arg 2)))
5086 (if (listp program)
5087 (progn
5088 (ad-set-arg 2 (car program))
5089 (ad-set-arg 4 (cdr program))))))
5090
5091 (defadvice term-exec-1 (around hack-environment compile activate)
5092 "Hack the environment inherited by inferiors in the terminal."
5093 (let ((process-environment (copy-tree process-environment)))
5094 (setenv "LD_PRELOAD" nil)
5095 ad-do-it))
5096
5097 (defadvice shell (around hack-environment compile activate)
5098 "Hack the environment inherited by inferiors in the shell."
5099 (let ((process-environment (copy-tree process-environment)))
5100 (setenv "LD_PRELOAD" nil)
5101 ad-do-it))
5102
5103 (defun ssh (host)
5104 "Open a terminal containing an ssh session to the HOST."
5105 (interactive "sHost: ")
5106 (ansi-term (list "ssh" host) (format "ssh@%s" host)))
5107
5108 (defcustom git-grep-command
5109 "env GIT_PAGER=cat git grep --no-color -nH -e "
5110 "The default command for \\[git-grep]."
5111 :type 'string)
5112
5113 (defvar git-grep-history nil)
5114
5115 (defun git-grep (command-args)
5116 "Run `git grep' with user-specified args and collect output in a buffer."
5117 (interactive
5118 (list (read-shell-command "Run git grep (like this): "
5119 git-grep-command 'git-grep-history)))
5120 (let ((grep-use-null-device nil))
5121 (grep command-args)))
5122
5123 ;;;--------------------------------------------------------------------------
5124 ;;; Magit configuration.
5125
5126 (setq magit-diff-refine-hunk 't
5127 magit-view-git-manual-method 'man
5128 magit-log-margin '(nil age magit-log-margin-width t 18)
5129 magit-wip-after-save-local-mode-lighter ""
5130 magit-wip-after-apply-mode-lighter ""
5131 magit-wip-before-change-mode-lighter "")
5132 (eval-after-load "magit"
5133 '(progn (global-magit-file-mode 1)
5134 (magit-wip-after-save-mode 1)
5135 (magit-wip-after-apply-mode 1)
5136 (magit-wip-before-change-mode 1)
5137 (add-to-list 'magit-no-confirm 'safe-with-wip)
5138 (add-to-list 'magit-no-confirm 'trash)
5139 (push '(:eval (if (or magit-wip-after-save-local-mode
5140 magit-wip-after-apply-mode
5141 magit-wip-before-change-mode)
5142 (format " wip:%s%s%s"
5143 (if magit-wip-after-apply-mode "A" "")
5144 (if magit-wip-before-change-mode "C" "")
5145 (if magit-wip-after-save-local-mode "S" ""))))
5146 minor-mode-alist)
5147 (dolist (popup '(magit-diff-popup
5148 magit-diff-refresh-popup
5149 magit-diff-mode-refresh-popup
5150 magit-revision-mode-refresh-popup))
5151 (magit-define-popup-switch popup ?R "Reverse diff" "-R"))
5152 (magit-define-popup-switch 'magit-rebase-popup ?r
5153 "Rebase merges" "--rebase-merges")))
5154
5155 (defadvice magit-wip-commit-buffer-file
5156 (around mdw-just-this-buffer activate compile)
5157 (let ((magit-save-repository-buffers nil)) ad-do-it))
5158
5159 (defadvice magit-discard
5160 (around mdw-delete-if-prefix-argument activate compile)
5161 (let ((magit-delete-by-moving-to-trash
5162 (and (null current-prefix-arg)
5163 magit-delete-by-moving-to-trash)))
5164 ad-do-it))
5165
5166 (setq magit-repolist-columns
5167 '(("Name" 16 magit-repolist-column-ident nil)
5168 ("Version" 18 magit-repolist-column-version nil)
5169 ("St" 2 magit-repolist-column-dirty nil)
5170 ("L<U" 3 mdw-repolist-column-unpulled-from-upstream nil)
5171 ("L>U" 3 mdw-repolist-column-unpushed-to-upstream nil)
5172 ("Path" 32 magit-repolist-column-path nil)))
5173
5174 (setq magit-repository-directories '(("~/etc/profile" . 0)
5175 ("~/src/" . 1)))
5176
5177 (defadvice magit-list-repos (around mdw-dirname () activate compile)
5178 "Make sure the returned names are directory names.
5179 Otherwise child processes get started in the wrong directory and
5180 there is sadness."
5181 (setq ad-return-value (mapcar #'file-name-as-directory ad-do-it)))
5182
5183 (defun mdw-repolist-column-unpulled-from-upstream (_id)
5184 "Insert number of upstream commits not in the current branch."
5185 (let ((upstream (magit-get-upstream-branch (magit-get-current-branch) t)))
5186 (and upstream
5187 (let ((n (cadr (magit-rev-diff-count "HEAD" upstream))))
5188 (propertize (number-to-string n) 'face
5189 (if (> n 0) 'bold 'shadow))))))
5190
5191 (defun mdw-repolist-column-unpushed-to-upstream (_id)
5192 "Insert number of commits in the current branch but not its upstream."
5193 (let ((upstream (magit-get-upstream-branch (magit-get-current-branch) t)))
5194 (and upstream
5195 (let ((n (car (magit-rev-diff-count "HEAD" upstream))))
5196 (propertize (number-to-string n) 'face
5197 (if (> n 0) 'bold 'shadow))))))
5198
5199 (defun mdw-try-smerge ()
5200 (save-excursion
5201 (goto-char (point-min))
5202 (when (re-search-forward "^<<<<<<< " nil t)
5203 (smerge-mode 1))))
5204 (add-hook 'find-file-hook 'mdw-try-smerge t)
5205
5206 (defcustom mdw-magit-new-window-modes
5207 '(magit-diff-mode
5208 magit-log-mode
5209 magit-process-mode
5210 magit-revision-mode
5211 magit-stash-mode
5212 magit-status-mode)
5213 "Magit modes which should cause a new window to be used."
5214 :type '(repeat symbol))
5215
5216 (defun mdw-display-magit-buffer (buffer)
5217 "Like `magit-display-buffer-traditional'.
5218 But uses `mdw-magit-new-window-modes' for its list of modes
5219 rather than baking the list into the function."
5220 (display-buffer buffer
5221 (let ((mode (with-current-buffer buffer major-mode)))
5222 (if (and (not mdw-designated-window)
5223 (derived-mode-p 'magit-mode)
5224 (mdw-submode-p mode 'magit-mode)
5225 (not (memq mode mdw-magit-new-window-modes)))
5226 '(display-buffer-same-window . nil)
5227 nil))))
5228 (setq magit-display-buffer-function 'mdw-display-magit-buffer)
5229
5230 (defun mdw-display-magit-file-buffer (buffer)
5231 "Show a file buffer from a diff."
5232 (select-window (display-buffer buffer)))
5233 (setq magit-display-file-buffer-function 'mdw-display-magit-file-buffer)
5234
5235 ;;;--------------------------------------------------------------------------
5236 ;;; GUD, and especially GDB.
5237
5238 ;; Inhibit window dedication. I mean, seriously, wtf?
5239 (defadvice gdb-display-buffer (after mdw-undedicated (buf) compile activate)
5240 "Don't make windows dedicated. Seriously."
5241 (set-window-dedicated-p ad-return-value nil))
5242 (defadvice gdb-set-window-buffer
5243 (after mdw-undedicated (name &optional ignore-dedicated window)
5244 compile activate)
5245 "Don't make windows dedicated. Seriously."
5246 (set-window-dedicated-p (or window (selected-window)) nil))
5247
5248 ;;;--------------------------------------------------------------------------
5249 ;;; SQL stuff.
5250
5251 (setq sql-postgres-options '("-n" "-P" "pager=off")
5252 sql-postgres-login-params
5253 '((user :default "mdw")
5254 (database :default "mdw")
5255 (server :default "db.distorted.org.uk")))
5256
5257 ;;;--------------------------------------------------------------------------
5258 ;;; Man pages.
5259
5260 ;; Turn off `noip' when running `man': it interferes with `man-db''s own
5261 ;; seccomp(2)-based sandboxing, which is (in this case, at least) strictly
5262 ;; better.
5263 (defadvice Man-getpage-in-background
5264 (around mdw-inhibit-noip (topic) compile activate)
5265 "Inhibit the `noip' preload hack when invoking `man'."
5266 (let* ((old-preload (getenv "LD_PRELOAD"))
5267 (preloads (and old-preload
5268 (save-match-data (split-string old-preload ":"))))
5269 (any nil)
5270 (filtered nil))
5271 (save-match-data
5272 (while preloads
5273 (let ((item (pop preloads)))
5274 (if (string-match "\\(/\\|^\\)noip\.so\\(:\\|$\\)" item)
5275 (setq any t)
5276 (push item filtered)))))
5277 (if any
5278 (unwind-protect
5279 (progn
5280 (setenv "LD_PRELOAD"
5281 (and filtered
5282 (with-output-to-string
5283 (setq filtered (nreverse filtered))
5284 (let ((first t))
5285 (while filtered
5286 (if first (setq first nil)
5287 (write-char ?:))
5288 (write-string (pop filtered)))))))
5289 ad-do-it)
5290 (setenv "LD_PRELOAD" old-preload))
5291 ad-do-it)))
5292
5293 ;;;--------------------------------------------------------------------------
5294 ;;; MPC configuration.
5295
5296 (eval-when-compile (trap (require 'mpc)))
5297
5298 (setq mpc-browser-tags '(Artist|Composer|Performer Album|Playlist))
5299
5300 (defun mdw-mpc-now-playing ()
5301 (interactive)
5302 (require 'mpc)
5303 (save-excursion
5304 (set-buffer (mpc-proc-cmd (mpc-proc-cmd-list '("status" "currentsong"))))
5305 (mpc--status-callback))
5306 (let ((state (cdr (assq 'state mpc-status))))
5307 (cond ((member state '("stop"))
5308 (message "mpd stopped."))
5309 ((member state '("play" "pause"))
5310 (let* ((artist (cdr (assq 'Artist mpc-status)))
5311 (album (cdr (assq 'Album mpc-status)))
5312 (title (cdr (assq 'Title mpc-status)))
5313 (file (cdr (assq 'file mpc-status)))
5314 (duration-string (cdr (assq 'Time mpc-status)))
5315 (time-string (cdr (assq 'time mpc-status)))
5316 (time (and time-string
5317 (string-to-number
5318 (if (string-match ":" time-string)
5319 (substring time-string
5320 0 (match-beginning 0))
5321 (time-string)))))
5322 (duration (and duration-string
5323 (string-to-number duration-string)))
5324 (pos (and time duration
5325 (format " [%d:%02d/%d:%02d]"
5326 (/ time 60) (mod time 60)
5327 (/ duration 60) (mod duration 60))))
5328 (fmt (cond ((and artist title)
5329 (format "`%s' by %s%s" title artist
5330 (if album (format ", from `%s'" album)
5331 "")))
5332 (file
5333 (format "`%s' (no tags)" file))
5334 (t
5335 "(no idea what's playing!)"))))
5336 (if (string= state "play")
5337 (message "mpd playing %s%s" fmt (or pos ""))
5338 (message "mpd paused in %s%s" fmt (or pos "")))))
5339 (t
5340 (message "mpd in unknown state `%s'" state)))))
5341
5342 (defmacro mdw-define-mpc-wrapper (func bvl interactive &rest body)
5343 `(defun ,func ,bvl
5344 (interactive ,@interactive)
5345 (require 'mpc)
5346 ,@body
5347 (mdw-mpc-now-playing)))
5348
5349 (mdw-define-mpc-wrapper mdw-mpc-play-or-pause () nil
5350 (if (member (cdr (assq 'state (mpc-cmd-status))) '("play"))
5351 (mpc-pause)
5352 (mpc-play)))
5353
5354 (mdw-define-mpc-wrapper mdw-mpc-next () nil (mpc-next))
5355 (mdw-define-mpc-wrapper mdw-mpc-prev () nil (mpc-prev))
5356 (mdw-define-mpc-wrapper mdw-mpc-stop () nil (mpc-stop))
5357
5358 (defun mdw-mpc-louder (step)
5359 (interactive (list (if current-prefix-arg
5360 (prefix-numeric-value current-prefix-arg)
5361 +10)))
5362 (mpc-proc-cmd (format "volume %+d" step)))
5363
5364 (defun mdw-mpc-quieter (step)
5365 (interactive (list (if current-prefix-arg
5366 (prefix-numeric-value current-prefix-arg)
5367 +10)))
5368 (mpc-proc-cmd (format "volume %+d" (- step))))
5369
5370 (defun mdw-mpc-hack-lines (arg interactivep func)
5371 (if (and interactivep (use-region-p))
5372 (let ((from (region-beginning)) (to (region-end)))
5373 (goto-char from)
5374 (beginning-of-line)
5375 (funcall func)
5376 (forward-line)
5377 (while (< (point) to)
5378 (funcall func)
5379 (forward-line)))
5380 (let ((n (prefix-numeric-value arg)))
5381 (cond ((minusp n)
5382 (unless (bolp)
5383 (beginning-of-line)
5384 (funcall func)
5385 (incf n))
5386 (while (minusp n)
5387 (forward-line -1)
5388 (funcall func)
5389 (incf n)))
5390 (t
5391 (beginning-of-line)
5392 (while (plusp n)
5393 (funcall func)
5394 (forward-line)
5395 (decf n)))))))
5396
5397 (defun mdw-mpc-select-one ()
5398 (when (and (get-char-property (point) 'mpc-file)
5399 (not (get-char-property (point) 'mpc-select)))
5400 (mpc-select-toggle)))
5401
5402 (defun mdw-mpc-unselect-one ()
5403 (when (get-char-property (point) 'mpc-select)
5404 (mpc-select-toggle)))
5405
5406 (defun mdw-mpc-select (&optional arg interactivep)
5407 (interactive (list current-prefix-arg t))
5408 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
5409
5410 (defun mdw-mpc-unselect (&optional arg interactivep)
5411 (interactive (list current-prefix-arg t))
5412 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-unselect-one))
5413
5414 (defun mdw-mpc-unselect-backwards (arg)
5415 (interactive "p")
5416 (mdw-mpc-hack-lines (- arg) t 'mdw-mpc-unselect-one))
5417
5418 (defun mdw-mpc-unselect-all ()
5419 (interactive)
5420 (setq mpc-select nil)
5421 (mpc-selection-refresh))
5422
5423 (defun mdw-mpc-next-line (arg)
5424 (interactive "p")
5425 (beginning-of-line)
5426 (forward-line arg))
5427
5428 (defun mdw-mpc-previous-line (arg)
5429 (interactive "p")
5430 (beginning-of-line)
5431 (forward-line (- arg)))
5432
5433 (defun mdw-mpc-playlist-add (&optional arg interactivep)
5434 (interactive (list current-prefix-arg t))
5435 (let ((mpc-select mpc-select))
5436 (when (or arg (and interactivep (use-region-p)))
5437 (setq mpc-select nil)
5438 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
5439 (setq mpc-select (reverse mpc-select))
5440 (mpc-playlist-add)))
5441
5442 (defun mdw-mpc-playlist-delete (&optional arg interactivep)
5443 (interactive (list current-prefix-arg t))
5444 (setq mpc-select (nreverse mpc-select))
5445 (mpc-select-save
5446 (when (or arg (and interactivep (use-region-p)))
5447 (setq mpc-select nil)
5448 (mpc-selection-refresh)
5449 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
5450 (mpc-playlist-delete)))
5451
5452 (defun mdw-mpc-hack-tagbrowsers ()
5453 (setq-local mode-line-format
5454 '("%e"
5455 mode-line-frame-identification
5456 mode-line-buffer-identification)))
5457 (add-hook 'mpc-tagbrowser-mode-hook 'mdw-mpc-hack-tagbrowsers)
5458
5459 (defun mdw-mpc-hack-songs ()
5460 (setq-local header-line-format
5461 ;; '("MPC " mpc-volume " " mpc-current-song)
5462 (list (propertize " " 'display '(space :align-to 0))
5463 ;; 'mpc-songs-format-description
5464 '(:eval
5465 (let ((deactivate-mark) (hscroll (window-hscroll)))
5466 (with-temp-buffer
5467 (mpc-format mpc-songs-format 'self hscroll)
5468 ;; That would be simpler than the hscroll handling in
5469 ;; mpc-format, but currently move-to-column does not
5470 ;; recognize :space display properties.
5471 ;; (move-to-column hscroll)
5472 ;; (delete-region (point-min) (point))
5473 (buffer-string)))))))
5474 (add-hook 'mpc-songs-mode-hook 'mdw-mpc-hack-songs)
5475
5476 (eval-after-load "mpc"
5477 '(progn
5478 (define-key mpc-mode-map "m" 'mdw-mpc-select)
5479 (define-key mpc-mode-map "u" 'mdw-mpc-unselect)
5480 (define-key mpc-mode-map "\177" 'mdw-mpc-unselect-backwards)
5481 (define-key mpc-mode-map "\e\177" 'mdw-mpc-unselect-all)
5482 (define-key mpc-mode-map "n" 'mdw-mpc-next-line)
5483 (define-key mpc-mode-map "p" 'mdw-mpc-previous-line)
5484 (define-key mpc-mode-map "/" 'mpc-songs-search)
5485 (setq mpc-songs-mode-map (make-sparse-keymap))
5486 (set-keymap-parent mpc-songs-mode-map mpc-mode-map)
5487 (define-key mpc-songs-mode-map "l" 'mpc-playlist)
5488 (define-key mpc-songs-mode-map "+" 'mdw-mpc-playlist-add)
5489 (define-key mpc-songs-mode-map "-" 'mdw-mpc-playlist-delete)
5490 (define-key mpc-songs-mode-map "\r" 'mpc-songs-jump-to)))
5491
5492 ;;;--------------------------------------------------------------------------
5493 ;;; Inferior Emacs Lisp.
5494
5495 (setq comint-prompt-read-only t)
5496
5497 (eval-after-load "comint"
5498 '(progn
5499 (define-key comint-mode-map "\C-w" 'comint-kill-region)
5500 (define-key comint-mode-map [C-S-backspace] 'comint-kill-whole-line)))
5501
5502 (eval-after-load "ielm"
5503 '(progn
5504 (define-key ielm-map "\C-w" 'comint-kill-region)
5505 (define-key ielm-map [C-S-backspace] 'comint-kill-whole-line)))
5506
5507 ;;;----- That's all, folks --------------------------------------------------
5508
5509 (provide 'dot-emacs)