el/dot-emacs.el: Highlght make(1)'s directory-change messages better.
[profile] / el / dot-emacs.el
CommitLineData
502f4699 1;;; -*- mode: emacs-lisp; coding: utf-8 -*-
f617db13 2;;;
f617db13
MW
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.
852cd5fb 14;;;
f617db13
MW
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.
852cd5fb 19;;;
f617db13
MW
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
6132bc01
MW
24;;;--------------------------------------------------------------------------
25;;; Check command-line.
c7a8da49 26
3ce407bb
MW
27(defgroup mdw nil
28 "Customization for mdw's Emacs configuration."
29 :prefix "mdw-")
30
61aab372
MW
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
c7a8da49
MW
43(defvar mdw-fast-startup nil
44 "Whether .emacs should optimize for rapid startup.
45This may be at the expense of cool features.")
61aab372
MW
46(setq mdw-fast-startup
47 (mdw-check-command-line-switch "--mdw-fast-startup"))
c7a8da49 48
3a87e7ef
MW
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
6132bc01
MW
54;;;--------------------------------------------------------------------------
55;;; Some general utilities.
f617db13 56
417dcddd 57(eval-when-compile
c6840556 58 (unless (fboundp 'make-regexp) (load "make-regexp"))
417dcddd 59 (require 'cl))
c7a8da49
MW
60
61(defmacro mdw-regexps (&rest list)
62 "Turn a LIST of strings into a single regular expression at compile-time."
9e789ed5
MW
63 (declare (indent nil)
64 (debug 0))
417dcddd 65 `',(make-regexp list))
c7a8da49 66
a1b01eb3
MW
67(defun mdw-wrong ()
68 "This is not the key sequence you're looking for."
69 (interactive)
70 (error "wrong button"))
71
e8ea88ba
MW
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
b8dc30fe
MW
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
6132bc01 85;; Some error trapping.
f617db13
MW
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
9c1cac7e
MW
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)))))
f617db13 100
6132bc01 101;; Configuration reading.
f141fe0f
MW
102
103(defvar mdw-config nil)
104(defun mdw-config (sym)
105 "Read the configuration variable named SYM."
106 (unless mdw-config
daff679f 107 (setq mdw-config
535c927f
MW
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) ")")))))))
f141fe0f
MW
123 (cdr (assq sym mdw-config)))
124
c7203018
MW
125;; Width configuration.
126
3ce407bb 127(defcustom mdw-column-width
c7203018 128 (string-to-number (or (mdw-config 'emacs-width) "77"))
3ce407bb
MW
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)
c7203018 135
18bb0f77
MW
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
6132bc01 144;; Set up the load path convincingly.
eac7b622
MW
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
6132bc01 155;; Is an Emacs library available?
cb6e2cd1
MW
156
157(defun library-exists-p (name)
6132bc01
MW
158 "Return non-nil if NAME is an available library.
159Return non-nil if NAME.el (or NAME.elc) somewhere on the Emacs
160load path. The non-nil value is the filename we found for the
161library."
cb6e2cd1
MW
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
8183de08
MW
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
3260d9c4
MW
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
6132bc01 196;; Splitting windows.
f617db13 197
8c2b05d5
MW
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
3ba0b777
MW
209(defun mdw-horizontal-window-overhead ()
210 "Computes the horizontal window overhead.
211This is the number of columns used by fringes, scroll bars and other such
212cruft."
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.
224Without a numeric argument, split the window approximately in
225half. With a numeric argument WIDTH, allocate WIDTH columns to
226the left-hand window (if positive) or -WIDTH columns to the
227right-hand window (if negative). Space for scroll bars and
228fringes 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
22e1b8c3
MW
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
8c2b05d5 241(defun mdw-divvy-window (&optional width)
f617db13 242 "Split a wide window into appropriate widths."
8c2b05d5 243 (interactive "P")
22e1b8c3
MW
244 (setq width (if width (prefix-numeric-value width)
245 (mdw-preferred-column-width)))
b5d724dd 246 (let* ((win (selected-window))
3ba0b777 247 (sb-width (mdw-horizontal-window-overhead))
b5d724dd 248 (c (/ (+ (window-width) sb-width)
8c2b05d5 249 (+ width sb-width))))
f617db13
MW
250 (while (> c 1)
251 (setq c (1- c))
8c2b05d5 252 (split-window-horizontally (+ width sb-width))
f617db13
MW
253 (other-window 1))
254 (select-window win)))
255
cc8708fc 256(defun mdw-set-frame-width (columns &optional width)
80d62d85
MW
257 "Set the current frame to be the correct width for COLUMNS columns.
258
259If WIDTH is non-nil, then it provides the width for the new columns. (This
260can be set interactively with a prefix argument.)"
cc8708fc
MW
261 (interactive "nColumns:
262P")
22e1b8c3
MW
263 (setq width (if width (prefix-numeric-value width)
264 (mdw-preferred-column-width)))
cc8708fc
MW
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
3ce407bb 271(defcustom mdw-frame-width-fudge
5e96bd82
MW
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
3ce407bb
MW
277This is sadly necessary because Emacs 26 is broken in this regard."
278 :type 'integer)
5e96bd82 279
3ce407bb 280(defcustom mdw-frame-colour-alist
117f652a
MW
281 '((black . ("#000000" . "#ffffff"))
282 (red . ("#2a0000" . "#ffffff"))
283 (green . ("#002a00" . "#ffffff"))
284 (blue . ("#00002a" . "#ffffff")))
3ce407bb
MW
285 "Alist mapping symbol names to (FOREGROUND . BACKGROUND) colour pairs."
286 :type '(alist :key-type symbol :value-type (cons color color)))
117f652a
MW
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
183e55e2
MW
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.
305If a current window configuration register is established, and
306NO-SAVE is nil, then save the current window configuration to
307that register first.
308
309Signal an error if the new register contains something other than
310a window configuration. If the register is unset then save the
311current window configuration to it immediately.
312
313With one or three C-u, or an odd numeric prefix argument, set
314NO-SAVE, so the previous window configuration register is left
315unchanged.
316
317With two or three C-u, or a prefix argument which is an odd
318multiple of 2, just clear the record of the current window
319configuration register, so that the next switch doesn't save the
320prevailing 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
4733bb30
MW
330 (let ((previous mdw-current-window-configuration)
331 (current-windows (list (current-window-configuration)
183e55e2
MW
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)
4733bb30
MW
337 (setq mdw-current-window-configuration nil)
338 (if previous
339 (message "Left window configuration `%c'." previous)
340 (message "Nothing to do!")))
183e55e2
MW
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)
4733bb30
MW
349 (set-register register current-windows)
350 (message "Started new window configuration `%c'."
351 register))
183e55e2
MW
352 (t
353 (set-window-configuration (car register-value))
4733bb30
MW
354 (goto-char (cadr register-value))
355 (message "Switched to window configuration `%c'."
356 register)))
183e55e2
MW
357 (setq mdw-current-window-configuration register)))))
358
cc2be23e
MW
359;; Don't raise windows unless I say so.
360
3ce407bb
MW
361(defcustom mdw-inhibit-raise-frame nil
362 "Whether `raise-frame' should do nothing when the frame is mapped."
363 :type 'boolean)
cc2be23e
MW
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
368frame 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)
b8e4b4a9 382(mdw-advise-to-inhibit-raise-frame appt-disp-window)
a3289165 383(mdw-advise-to-inhibit-raise-frame mouse-select-window)
cc2be23e 384
a1e4004c
MW
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
d54a4cf3
MW
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
c4b18360
MW
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.
423If `transient-mark-mode' is on and the mark is inactive, then
424just activate it. A non-trivial prefix argument will force the
425usual behaviour. A trivial prefix argument (i.e., just C-u) will
426activate the mark and temporarily enable `transient-mark-mode' if
427it'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
6132bc01 437;; Functions for sexp diary entries.
f617db13 438
6c0b6079
MW
439(defvar mdw-diary-for-org-mode-p nil
440 "Display diary along with the agenda?")
441
aede9fd7
MW
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
f617db13
MW
447(defun mdw-weekday (l)
448 "Return non-nil if `date' falls on one of the days of the week in L.
6132bc01
MW
449L is a list of day numbers (from 0 to 6 for Sunday through to
450Saturday) or symbols `sunday', `monday', etc. (or a mixture). If
451the date stored in `date' falls on a listed day, then the
452function returns non-nil."
f617db13
MW
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
d401f71b
MW
458(defun mdw-discordian-date (date)
459 "Return the Discordian calendar date corresponding to DATE.
460
461The return value is (YOLD . st-tibs-day) or (YOLD SEASON DAYNUM DOW).
462
463The original is by David Pearson. I modified it to produce date components
464as 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])
a88da179
MW
470 (year (- (calendar-extract-year date) 1900))
471 (month (1- (calendar-extract-month date)))
472 (day (1- (calendar-extract-day date)))
d401f71b
MW
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
f617db13
MW
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
aede9fd7
MW
521(defadvice org-agenda-list (around mdw-preserve-links activate)
522 (let ((mdw-diary-for-org-mode-p t))
523 ad-do-it))
524
3ce407bb
MW
525(defcustom diary-time-regexp nil
526 "Regexp matching times in the diary buffer."
527 :type 'regexp)
3d24d0fa 528
1d342abe 529(defadvice diary-add-to-list (before mdw-trim-leading-space compile activate)
9f58a8d2
MW
530 "Trim leading space from the diary entry string."
531 (save-match-data
2745469d
MW
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))
aede9fd7
MW
539 ((and mdw-diary-for-org-mode-p
540 (string-match (concat
2745469d 541 "\\(^\\|\n\\)"
aede9fd7
MW
542 "\\(" diary-time-regexp
543 "\\(-" diary-time-regexp "\\)?"
2745469d
MW
544 "\\)"
545 "\\(\t[ \t]*\\| [ \t]+\\)")
aede9fd7 546 str))
2745469d 547 (replace-match "\\1\\2 " nil nil str))
aede9fd7
MW
548 ((and (not mdw-diary-for-org-mode-p)
549 (string-match "\\[\\[[^][]*]\\[\\([^][]*\\)]]"
550 str))
551 (replace-match "\\1" nil nil str))
2745469d
MW
552 (t str)))
553 (if (equal str old) (setq done t)))
554 (ad-set-arg 1 str))))
9f58a8d2 555
319736bd
MW
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
535c927f 567 (and anyp (buffer-string))))))
319736bd 568
6132bc01 569;; Fighting with Org-mode's evil key maps.
16fe7c41 570
3ce407bb 571(defcustom mdw-evil-keymap-keys
16fe7c41
MW
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'.
581The value is an alist mapping evil keys (as a list, or singleton)
3ce407bb
MW
582to good keys (in the same form)."
583 :type '(alist :key-type (choice key-sequence (repeat key-sequence))
584 :value-type key-sequence))
16fe7c41
MW
585
586(defun mdw-clobber-evil-keymap (keymap)
587 "Replace evil key bindings in the KEYMAP.
588Evil 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
3ce407bb 608(defcustom mdw-org-latex-defs
1656a861
MW
609 '(("strayman"
610 "\\documentclass{strayman}
f0dbee84
MW
611\\usepackage[utf8]{inputenc}
612\\usepackage[palatino, helvetica, courier, maths=cmr]{mdwfonts}
f0dbee84 613\\usepackage{graphicx, tikz, mdwtab, mdwmath, crypto, longtable}"
1656a861
MW
614 ("\\section{%s}" . "\\section*{%s}")
615 ("\\subsection{%s}" . "\\subsection*{%s}")
616 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
617 ("\\paragraph{%s}" . "\\paragraph*{%s}")
3ce407bb
MW
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))))
1656a861
MW
625
626(eval-after-load "org-latex"
627 '(setq org-export-latex-classes
535c927f 628 (append mdw-org-latex-defs org-export-latex-classes)))
f0dbee84 629
fbc946b7
MW
630(eval-after-load "ox-latex"
631 '(setq org-latex-classes (append mdw-org-latex-defs org-latex-classes)
e58f4950 632 org-latex-caption-above nil
fbc946b7
MW
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
8ba985cb
MW
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
535c927f 653 "/usr/share/xml/docbook/stylesheet/docbook-xsl/fo/docbook.xsl")
8ba985cb 654
5a0925a4
MW
655;; Glasses.
656
657(setq glasses-separator "-"
658 glasses-separate-parentheses-p nil
659 glasses-uncapitalize-p t)
660
5dccbe61
MW
661;; Some hacks to do with window placement.
662
4ebbf6a7
MW
663(defvar mdw-designated-window nil
664 "The window chosen by `mdw-designate-window', or nil.")
665
734a6b72
MW
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
95a5792c
MW
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
4ebbf6a7
MW
682(defun mdw-designate-window (cancel)
683 "Use the selected window for the next pop-up buffer.
684With a prefix argument, clear the designated window."
685 (interactive "P")
734a6b72
MW
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))))
4ebbf6a7 708
5dccbe61
MW
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*")))
6c4bd06b
MW
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)))))))
5dccbe61
MW
720
721(defvar mdw-inhibit-walk-windows nil
722 "If non-nil, then `walk-windows' does nothing.
723This is used by advice on `switch-to-buffer-other-frame' to inhibit finding
724buffers in random frames.")
725
4ff90aeb 726(setq display-buffer--other-frame-action
535c927f
MW
727 '((display-buffer-reuse-window display-buffer-pop-up-frame)
728 (reusable-frames . nil)
729 (inhibit-same-window . t)))
4ff90aeb 730
5dccbe61
MW
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.
739Even 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.
744Pretend they don't exist. They might be on other display devices."
745 (ad-set-arg 2 nil))
746
5d561c35
MW
747(setq even-window-sizes nil
748 even-window-heights nil)
749
3782446f
MW
750;; Rename buffers along with files.
751
52ffecb1
MW
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
758This 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
3782446f
MW
767(defadvice rename-file (after mdw-rename-buffers (from to &optional forcep)
768 compile activate)
52ffecb1
MW
769 "If a buffer is visiting the file, rename it to match the new name.
770
771Don'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
f74fc666 775 (let ((to (if (not (string= (file-name-nondirectory to) "")) to
535c927f
MW
776 (concat to (file-name-nondirectory from)))))
777 (with-current-buffer buffer
778 (set-visited-file-name to nil t)))))))
3782446f 779
6132bc01 780;;;--------------------------------------------------------------------------
b7fc31ec
MW
781;;; Improved compilation machinery.
782
783;; Uprated version of M-x compile.
784
785(setq compile-command
535c927f
MW
786 (let ((ncpu (with-temp-buffer
787 (insert-file-contents "/proc/cpuinfo")
788 (buffer-string)
789 (count-matches "^processor\\s-*:"))))
c0a4a95f 790 (format "nice make -j%d -k" (* 2 ncpu))))
b7fc31ec
MW
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
8845865d
MW
801(defadvice compile (around hack-environment compile activate)
802 "Hack the environment inherited by inferiors in the compilation."
f8592fee 803 (let ((process-environment (copy-tree process-environment)))
8845865d
MW
804 (setenv "LD_PRELOAD" nil)
805 ad-do-it))
806
b7fc31ec
MW
807(defun mdw-compile (command &optional directory comint)
808 "Initiate a compilation COMMAND, maybe in a different DIRECTORY.
809The DIRECTORY may be nil to not change. If COMINT is t, then
810start an interactive compilation.
811
812Interactively, prompt for the command if the variable
813`compilation-read-command' is non-nil, or if requested through
814the prefix argument. Prompt for the directory, and run
815interactively, if requested through the prefix.
816
817Use a prefix of 4, 6, 12, or 14, or type C-u between one and three times, to
818force prompting for a directory.
819
820Use a prefix of 2, 6, 10, or 14, or type C-u three times, to force
821prompting for the command.
822
823Use a prefix of 8, 10, 12, or 14, or type C-u twice or three times,
824to 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
50d3dd03
MW
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
535c927f
MW
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)))
50d3dd03
MW
876
877(setq flymake-mode-map
535c927f
MW
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))
50d3dd03 887
b7fc31ec 888;;;--------------------------------------------------------------------------
6132bc01 889;;; Mail and news hacking.
a3bdb4d9
MW
890
891(define-derived-mode mdwmail-mode mail-mode "[mdw] mail"
6132bc01 892 "Major mode for editing news and mail messages from external programs.
a3bdb4d9
MW
893Not 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
535c927f
MW
906 (concat "[ \t]*[-_][-_][-_]+$\\|^-- \\|-----\\|"
907 paragraph-start))
a3bdb4d9
MW
908 (setq paragraph-separate
909 (concat "[ \t]*[-_][-_][-_]+$\\|^-- \\|-----\\|"
910 paragraph-separate))))
911
6132bc01 912;; How to encrypt in mdwmail.
a3bdb4d9
MW
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
6132bc01 923;; How to sign in mdwmail.
a3bdb4d9
MW
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
6132bc01 934;; Some signature mangling.
a3bdb4d9
MW
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
6132bc01 943;; Insert my login name into message-ids, so I can score replies.
a3bdb4d9
MW
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,
947so that it can be used for convenient filtering."
948 (setq ad-return-value (concat ad-return-value "." (user-login-name))))
949
6132bc01 950;; Tell my movemail hack where movemail is.
a3bdb4d9
MW
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
4d5799eb
MW
961;; AUTHINFO GENERIC kludge.
962
3ce407bb 963(defcustom nntp-authinfo-generic nil
4d5799eb
MW
964 "Set to the `NNTPAUTH' string to pass on to `authinfo-kludge'.
965
3ce407bb
MW
966Use this to arrange for per-server settings."
967 :type '(choice (const :tag "Use `NNTPAUTH' environment variable" nil)
968 string)
969 :safe 'stringp)
4d5799eb
MW
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
d17c6756 985(eval-after-load "erc"
3db66f7b 986 '(load "~/.ercrc.el"))
d17c6756 987
d2d1d5dc
MW
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))
16f0f915 1014 (insert (format "%S" (subst-char-in-string ?\n ?\s string)))
d2d1d5dc
MW
1015 ;; [mdw] missing from upstream
1016 (backward-char 1))
1017 (beginning-of-line)
1018 (setq article
535c927f
MW
1019 (and (re-search-forward "UID \\([0-9]+\\)"
1020 (line-end-position)
1021 t)
1022 (match-string 1)))
d2d1d5dc
MW
1023 (setq lines nil)
1024 (setq size
535c927f
MW
1025 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
1026 (line-end-position)
1027 t)
1028 (match-string 1)))
d2d1d5dc
MW
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
00fe36a6
MW
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))
a151a655
MW
1064 (delete* 'width default-frame-alist
1065 :key #'car))))
00fe36a6
MW
1066 ad-do-it))
1067
4b48cb5b
MW
1068;; Preferred programs.
1069
1070(setq mailcap-user-mime-data
535c927f 1071 '(((type . "application/pdf") (viewer . "mupdf %s"))))
4b48cb5b 1072
6132bc01
MW
1073;;;--------------------------------------------------------------------------
1074;;; Utility functions.
f617db13 1075
b5d724dd
MW
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))))))))
459c9fb2 1088
f617db13 1089(defun mdw-uniquify-alist (&rest alists)
f617db13 1090 "Return the concatenation of the ALISTS with duplicate elements removed.
6132bc01
MW
1091The first association with a given key prevails; others are
1092ignored. The input lists are not modified, although they'll
1093probably become garbage."
f617db13
MW
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
f617db13 1101(defun mdw-do-uniquify (done end l rest)
6132bc01
MW
1102 "A helper function for mdw-uniquify-alist.
1103The DONE argument is a list whose first element is `nil'. It
1104contains the uniquified alist built so far. The leading `nil' is
1105stripped off at the end of the operation; it's only there so that
1106DONE always references a cons cell. END refers to the final cons
1107cell in the DONE list; it is modified in place each time to avoid
1108the overheads of `append'ing all the time. The L argument is the
1109alist we're currently processing; the remaining alists are given
1110in REST."
1111
1112 ;; There are several different cases to deal with here.
f617db13
MW
1113 (cond
1114
6132bc01
MW
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.
f617db13
MW
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
6132bc01
MW
1123 ;; The list we were working on is empty. Shunt the next list into the
1124 ;; current list position and go round again.
f617db13
MW
1125 (rest (mdw-do-uniquify done end (car rest) (cdr rest)))
1126
6132bc01
MW
1127 ;; Everything's done. Remove the leading `nil' from the DONE list and
1128 ;; return it. Finished!
f617db13
MW
1129 (t (cdr done))))
1130
f617db13
MW
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))
fbf8b18e 1140 (delete-char -1)
f617db13
MW
1141 (buffer-string))
1142 (kill-buffer buffer))))))
1143
f617db13
MW
1144(defun uuencode (file &optional name)
1145 "UUencodes a file, maybe calling it NAME, into the current buffer."
1146 (interactive "fInput file name: ")
1147
6132bc01 1148 ;; If NAME isn't specified, then guess from the filename.
f617db13
MW
1149 (if (not name)
1150 (setq name
1151 (substring file
1152 (or (string-match "[^/]*$" file) 0))))
f617db13
MW
1153 (print (format "uuencode `%s' `%s'" file name))
1154
6132bc01 1155 ;; Now actually do the thing.
f617db13
MW
1156 (call-process "uuencode" file t nil name))
1157
3ce407bb
MW
1158(defcustom np-file "~/.np"
1159 "Where the `now-playing' file is."
1160 :type 'file
1161 :safe 'stringp)
f617db13
MW
1162
1163(defun np (&optional arg)
1164 "Grabs a `now-playing' string."
1165 (interactive)
1166 (save-excursion
1167 (or arg (progn
852cd5fb 1168 (goto-char (point-max))
f617db13 1169 (insert "\nNP: ")
daff679f 1170 (insert-file-contents np-file)))))
f617db13 1171
ae7460d4
MW
1172(defun mdw-version-< (ver-a ver-b)
1173 "Answer whether VER-A is strictly earlier than VER-B.
1174VER-A and VER-B are version numbers, which are strings containing digit
1175sequences 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))
f64c5a1a
MW
1185 ((= (car la) (car lb)) (setq la (cdr la) lb (cdr lb)))
1186 (t (throw 'done nil)))))))
ae7460d4 1187
c7a8da49 1188(defun mdw-check-autorevert ()
6132bc01
MW
1189 "Sets global-auto-revert-ignore-buffer appropriately for this buffer.
1190This takes into consideration whether it's been found using
1191tramp, which seems to get itself into a twist."
46e69f55
MW
1192 (cond ((not (boundp 'global-auto-revert-ignore-buffer))
1193 nil)
1194 ((and (buffer-file-name)
1195 (fboundp 'tramp-tramp-file-p)
c7a8da49
MW
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)
4d9f2d65 1205 (mdw-check-autorevert))
eae0aa6d 1206
a58a4227
MW
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
6132bc01
MW
1213;;;--------------------------------------------------------------------------
1214;;; Dired hacking.
5195cbc3
MW
1215
1216(defadvice dired-maybe-insert-subdir
1217 (around mdw-marked-insertion first activate)
6132bc01
MW
1218 "The DIRNAME may be a list of directory names to insert.
1219Interactively, if files are marked, then insert all of them.
1220With a numeric prefix argument, select that many entries near
1221point; with a non-numeric prefix argument, prompt for listing
1222options."
5195cbc3
MW
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
d40903f4
MW
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
2a67803a
MW
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
d40903f4
MW
1252(eval-after-load "dired"
1253 '(define-key dired-mode-map "X" 'mdw-dired-run))
1254
6132bc01
MW
1255;;;--------------------------------------------------------------------------
1256;;; URL viewing.
a203fba8
MW
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.
1260If NEW-SESSION-P, start a new session."
1261 (interactive "sURL: \nP")
1262 (save-excursion
63fb20c1
MW
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)))))
a203fba8 1273
2ae8f8e3
MW
1274(eval-after-load 'w3m
1275 '(define-key w3m-mode-map [?\e ?\r] 'w3m-view-this-url-new-session))
1276
3ce407bb 1277(defcustom mdw-good-url-browsers
94526c3f 1278 '(browse-url-mozilla
a0d16e44 1279 browse-url-generic
ed5e20a5 1280 (w3m . mdw-w3m-browse-url)
a0d16e44 1281 browse-url-w3)
6132bc01
MW
1282 "List of good browsers for mdw-good-url-browsers.
1283Each item is a browser function name, or a cons (CHECK . FUNC).
3ce407bb
MW
1284A symbol FOO stands for (FOO . FOO)."
1285 :type '(repeat (choice function (cons function function))))
a203fba8
MW
1286
1287(defun mdw-good-url-browser ()
6132bc01
MW
1288 "Return a good URL browser.
1289Trundle the list of such things, finding the first item for which
1290CHECK is fboundp, and returning the correponding FUNC."
a203fba8
MW
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
f36cdb77
MW
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
6132bc01
MW
1338;;;--------------------------------------------------------------------------
1339;;; Paragraph filling.
f617db13 1340
6132bc01 1341;; Useful variables.
f617db13 1342
3ce407bb
MW
1343(defcustom mdw-fill-prefix nil
1344 "Used by `mdw-line-prefix' and `mdw-fill-paragraph'.
6132bc01
MW
1345If there's no fill prefix currently set (by the `fill-prefix'
1346variable) and there's a match from one of the regexps here, it
1347gets used to set the fill-prefix for the current operation.
f617db13 1348
3fd348cb
MW
1349The variable is a list of items of the form `PATTERN . PREFIX'; if
1350the PATTERN matches, the PREFIX is used to set the fill prefix.
f617db13 1351
3fd348cb
MW
1352A 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
1362A 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
1369Information about `bracketed subexpressions' comes from the match data,
1370as modified during matching.")
f617db13
MW
1371
1372(make-variable-buffer-local 'mdw-fill-prefix)
1373
3ce407bb 1374(defcustom mdw-hanging-indents
10fa2414 1375 (concat "\\(\\("
f8bfe560 1376 "\\([*o+]\\|-[-#]?\\|[0-9]+\\.\\|\\[[0-9]+\\]\\|([a-zA-Z])\\)"
10fa2414
MW
1377 "[ \t]+"
1378 "\\)?\\)")
3ce407bb
MW
1379 "Standard regexp matching parts of a hanging indent.
1380This is mainly useful in `auto-fill-mode'."
1381 :type 'regexp)
f617db13 1382
6132bc01 1383;; Utility functions.
f617db13 1384
cd07f97f
MW
1385(defun mdw-maybe-tabify (s)
1386 "Tabify or untabify the string S, according to `indent-tabs-mode'."
c736b08b
MW
1387 (let ((tabfun (if indent-tabs-mode #'tabify #'untabify)))
1388 (with-temp-buffer
1389 (save-match-data
f617db13 1390 (insert s "\n")
cd07f97f 1391 (let ((start (point-min)) (end (point-max)))
c736b08b
MW
1392 (funcall tabfun (point-min) (point-max))
1393 (setq s (buffer-substring (point-min) (1- (point-max)))))))))
f617db13 1394
3fd348cb
MW
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
f617db13
MW
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)
6132bc01
MW
1445 "Expand a dynamic prefix match element.
1446See `mdw-fill-prefix' for details."
f617db13 1447 (cond ((not (consp m)) (format "%s" m))
6ed1b26a
MW
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 "")))
f617db13 1453
c8ff7b64
MW
1454(defun mdw-examine-fill-prefixes (l)
1455 "Given a list of dynamic fill prefixes, pick one which matches
1456context and return the static fill prefix to use. Point must be
1457at the start of a line, and match data must be saved."
4f5c07e8
MW
1458 (let ((prefix nil))
1459 (while (cond ((null l) nil)
3fd348cb 1460 ((mdw-fill-prefix-match-p (caar l))
4f5c07e8
MW
1461 (setq prefix
1462 (mdw-maybe-tabify
1463 (apply #'concat
c8ff7b64
MW
1464 (mapcar #'mdw-do-prefix-match
1465 (cdr (car l))))))
4f5c07e8
MW
1466 nil))
1467 (setq l (cdr l)))
1468 prefix))
c8ff7b64 1469
f617db13
MW
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)
6ed1b26a
MW
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))))))
f617db13 1478
b8c659bb 1479(defadvice do-auto-fill (around mdw-dynamic-fill-prefix () activate compile)
6132bc01
MW
1480 "Handle auto-filling, working out a dynamic fill prefix in the
1481case where there isn't a sensible static one."
f617db13 1482 (let ((fill-prefix (mdw-choose-dynamic-fill-prefix)))
b8c659bb 1483 ad-do-it))
f617db13
MW
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
1de7dc66
MW
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
f617db13
MW
1496(defun mdw-standard-fill-prefix (rx &optional mat)
1497 "Set the dynamic fill prefix, handling standard hanging indents and stuff.
6132bc01
MW
1498This is just a short-cut for setting the thing by hand, and by
1499design it doesn't cope with anything approximating a complicated
1500case."
f617db13 1501 (setq mdw-fill-prefix
535c927f
MW
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))))))
f617db13 1507
6132bc01 1508;;;--------------------------------------------------------------------------
f33e684c
MW
1509;;; Printing.
1510
e42946da
MW
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
dc272b52
MW
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
e42946da
MW
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)))
a151a655
MW
1550 (remove* 'CourierCondensed ps-font-info-database
1551 :key #'car)))))
e42946da 1552
f33e684c
MW
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;;;--------------------------------------------------------------------------
6132bc01 1585;;; Other common declarations.
f617db13 1586
6132bc01 1587;; Common mode settings.
f617db13 1588
3ce407bb
MW
1589(defcustom mdw-auto-indent t
1590 "Whether to indent automatically after a newline."
1591 :type 'boolean
1592 :safe 'booleanp)
f617db13 1593
0e58a7c2
MW
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)))
558fc014
MW
1600 (whitespace-mode arg))
1601 (setq show-trailing-whitespace whitespace-mode)))
0e58a7c2 1602
21beda17
MW
1603(defvar mdw-do-misc-mode-hacking nil)
1604
f617db13
MW
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))
4a7ce1ee 1609 ((derived-mode-p 'slime-repl-mode 'asm-mode 'comint-mode)
30c8a8fb 1610 nil)
f617db13
MW
1611 (t
1612 (local-set-key "\C-m" 'newline-and-indent))))
2e7c6a86 1613 (set (make-local-variable 'mdw-do-misc-mode-hacking) t)
f617db13 1614 (local-set-key [C-return] 'newline)
8a425bd7 1615 (make-local-variable 'page-delimiter)
5a29709b
MW
1616 (setq page-delimiter (concat "^" "\f"
1617 "\\|" "^"
1618 ".\\{0,4\\}"
1619 "-\\{5\\}"
1620 "\\(" " " ".*" " " "\\)?"
1621 "-+"
1622 ".\\{0,2\\}"
1623 "$"))
f617db13
MW
1624 (setq comment-column 40)
1625 (auto-fill-mode 1)
c7203018 1626 (setq fill-column mdw-text-width)
fbd237e6 1627 (flyspell-prog-mode)
253f61b4
MW
1628 (and (fboundp 'gtags-mode)
1629 (gtags-mode))
ddf6e116 1630 (if (fboundp 'hs-minor-mode)
612717ec 1631 (trap (hs-minor-mode t))
ddf6e116 1632 (outline-minor-mode t))
49b2646e 1633 (reveal-mode t)
1e7a9479 1634 (trap (turn-on-font-lock)))
f617db13 1635
2e7c6a86 1636(defun mdw-post-local-vars-misc-mode-config ()
c7203018 1637 (setq whitespace-line-column mdw-text-width)
2717a191
MW
1638 (when (and mdw-do-misc-mode-hacking
1639 (not buffer-read-only))
2e7c6a86
MW
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)
ed7b46b9 1643
2c1ccbb9
MW
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)
2717a191 1658
253f61b4 1659(eval-after-load 'gtags
506bada9
MW
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))))
253f61b4 1668
6132bc01 1669;; Backup file handling.
2ae647c4 1670
3ce407bb
MW
1671(defcustom mdw-backup-disable-regexps nil
1672 "List of regular expressions: if a file name matches any of
1673these then the file is not backed up."
1674 :type '(repeat regexp))
2ae647c4
MW
1675
1676(defun mdw-backup-enable-predicate (name)
6132bc01
MW
1677 "[mdw]'s default backup predicate.
1678Allows a backup if the standard predicate would allow it, and it
1679doesn't match any of the regular expressions in
1680`mdw-backup-disable-regexps'."
2ae647c4
MW
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
7bb78c67
MW
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)
7bb78c67 1699 (and (not (eq fr frame))
a04d8f3d 1700 (string= (frame-parameter fr 'display)
d70716b5 1701 frame-display)))
7bb78c67 1702 (frame-list))))
7bb78c67
MW
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
6132bc01 1706;;;--------------------------------------------------------------------------
f3674a83
MW
1707;;; Fullscreen-ness.
1708
3ce407bb 1709(defcustom mdw-full-screen-parameters
f3674a83 1710 '((menu-bar-lines . 0)
3ce407bb 1711 ;;(vertical-scroll-bars . nil)
f3674a83 1712 )
3ce407bb
MW
1713 "Frame parameters to set when making a frame fullscreen."
1714 :type '(alist :key-type symbol))
f3674a83 1715
3ce407bb 1716(defcustom mdw-full-screen-save
f3674a83 1717 '(width height)
3ce407bb
MW
1718 "Extra frame parameters to save when setting fullscreen."
1719 :type '(repeat symbol))
f3674a83
MW
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;;;--------------------------------------------------------------------------
6132bc01 1744;;; General fontification.
f617db13 1745
bc149706
MW
1746(make-face 'mdw-virgin-face)
1747
1e7a9479
MW
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
bc149706 1753 (copy-face 'mdw-virgin-face ',name)
1e7a9479
MW
1754 (defvar ,name ',name)
1755 (put ',name 'face-defface-spec ',body)
88cb9c2b 1756 (face-spec-set ',name ',body nil)))
1e7a9479
MW
1757
1758(mdw-define-face default
1759 (((type w32)) :family "courier new" :height 85)
caa63513 1760 (((type x)) :family "6x13" :foundry "trad" :height 130)
db10ce0a
MW
1761 (((type color)) :foreground "white" :background "black")
1762 (t nil))
1e7a9479
MW
1763(mdw-define-face fixed-pitch
1764 (((type w32)) :family "courier new" :height 85)
caa63513 1765 (((type x)) :family "6x13" :foundry "trad" :height 130)
1e7a9479 1766 (t :foreground "white" :background "black"))
da4332a9
MW
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))
f5ce374f
MW
1771(mdw-define-face variable-pitch
1772 (((type x)) :family "helvetica" :height 120))
1e7a9479 1773(mdw-define-face region
fefae026
MW
1774 (((min-colors 64)) :background "grey30")
1775 (((class color)) :background "blue")
4833e35c 1776 (t :inverse-video t))
fa156643 1777(mdw-define-face match
fefae026
MW
1778 (((class color)) :background "blue")
1779 (t :inverse-video t))
c6fe19d5 1780(mdw-define-face mc/cursor-face
fefae026
MW
1781 (((class color)) :background "red")
1782 (t :inverse-video t))
1e7a9479
MW
1783(mdw-define-face minibuffer-prompt
1784 (t :weight bold))
1785(mdw-define-face mode-line
db10ce0a
MW
1786 (((class color)) :foreground "blue" :background "yellow"
1787 :box (:line-width 1 :style released-button))
1788 (t :inverse-video t))
1e7a9479 1789(mdw-define-face mode-line-inactive
db10ce0a
MW
1790 (((class color)) :foreground "yellow" :background "blue"
1791 :box (:line-width 1 :style released-button))
1792 (t :inverse-video t))
ae0a853f
MW
1793(mdw-define-face nobreak-space
1794 (((type tty)))
1795 (t :inherit escape-glyph :underline t))
1e7a9479
MW
1796(mdw-define-face scroll-bar
1797 (t :foreground "black" :background "lightgrey"))
1798(mdw-define-face fringe
1799 (t :foreground "yellow"))
c383eb8a 1800(mdw-define-face show-paren-match
9cf75a93
MW
1801 (((min-colors 64)) :background "darkgreen")
1802 (((class color)) :background "green")
db10ce0a 1803 (t :underline t))
c383eb8a 1804(mdw-define-face show-paren-mismatch
db10ce0a
MW
1805 (((class color)) :background "red")
1806 (t :inverse-video t))
1e7a9479 1807(mdw-define-face highlight
fefae026
MW
1808 (((min-colors 64)) :background "DarkSeaGreen4")
1809 (((class color)) :background "cyan")
db10ce0a 1810 (t :inverse-video t))
1e7a9479 1811
d54399be
MW
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
855b4fd8
MW
1821(mdw-define-face compilation-error
1822 (((class color)) :foreground "red" :weight bold)
1823 (t :weight bold))
1824(mdw-define-face compilation-warning
1825 (((class color)) :foreground "orange" :weight bold)
1826 (t :weight bold))
1827(mdw-define-face compilation-info
1828 (((class color)) :foreground "green" :weight bold)
1829 (t :weight bold))
1830(mdw-define-face compilation-line-number
1831 (t :weight bold))
1832(mdw-define-face compilation-column-number
1833 (((min-colors 64)) :foreground "lightgrey"))
1af9b8d9 1834(setq compilation-message-face 'mdw-virgin-face)
d29d3810
MW
1835(setq compilation-enter-directory-face 'font-lock-comment-face)
1836(setq compilation-leave-directory-face 'font-lock-comment-face)
855b4fd8 1837
1e7a9479
MW
1838(mdw-define-face holiday-face
1839 (t :background "red"))
1840(mdw-define-face calendar-today-face
1841 (t :foreground "yellow" :weight bold))
1842
1843(mdw-define-face comint-highlight-prompt
1844 (t :weight bold))
5fd055c2
MW
1845(mdw-define-face comint-highlight-input
1846 (t nil))
1e7a9479 1847
13c19c5d
MW
1848(mdw-define-face Man-underline
1849 (((type tty)) :underline t)
1850 (t :slant italic))
1851
2e97e639
MW
1852(mdw-define-face ido-subdir
1853 (t :foreground "cyan" :weight bold))
1854
e0e2aca3
MW
1855(mdw-define-face dired-directory
1856 (t :foreground "cyan" :weight bold))
1857(mdw-define-face dired-symlink
1858 (t :foreground "cyan"))
1859(mdw-define-face dired-perm-write
1860 (t nil))
1861
1e7a9479 1862(mdw-define-face trailing-whitespace
db10ce0a
MW
1863 (((class color)) :background "red")
1864 (t :inverse-video t))
33aa287b
MW
1865(mdw-define-face whitespace-line
1866 (((class color)) :background "darkred")
a52bc3ca 1867 (t :inverse-video t))
1e7a9479 1868(mdw-define-face mdw-punct-face
fefae026
MW
1869 (((min-colors 64)) :foreground "burlywood2")
1870 (((class color)) :foreground "yellow"))
1e7a9479
MW
1871(mdw-define-face mdw-number-face
1872 (t :foreground "yellow"))
52bcde59 1873(mdw-define-face mdw-trivial-face)
1e7a9479 1874(mdw-define-face font-lock-function-name-face
c383eb8a 1875 (t :slant italic))
1e7a9479
MW
1876(mdw-define-face font-lock-keyword-face
1877 (t :weight bold))
1878(mdw-define-face font-lock-constant-face
1879 (t :slant italic))
1880(mdw-define-face font-lock-builtin-face
1881 (t :weight bold))
07965a39
MW
1882(mdw-define-face font-lock-type-face
1883 (t :weight bold :slant italic))
1e7a9479
MW
1884(mdw-define-face font-lock-reference-face
1885 (t :weight bold))
1886(mdw-define-face font-lock-variable-name-face
1887 (t :slant italic))
1e7a9479 1888(mdw-define-face font-lock-comment-face
fefae026
MW
1889 (((min-colors 64)) :slant italic :foreground "SeaGreen1")
1890 (((class color)) :foreground "green")
1891 (t :weight bold))
52322015
MW
1892(mdw-define-face font-lock-comment-delimiter-face
1893 (t :inherit font-lock-comment-face))
1e7a9479 1894(mdw-define-face font-lock-string-face
fefae026
MW
1895 (((min-colors 64)) :foreground "SkyBlue1")
1896 (((class color)) :foreground "cyan")
1897 (t :weight bold))
0d050337
MW
1898(mdw-define-face font-lock-doc-face
1899 (t :inherit font-lock-string-face))
898c7efb 1900
1e7a9479
MW
1901(mdw-define-face message-separator
1902 (t :background "red" :foreground "white" :weight bold))
1903(mdw-define-face message-cited-text
1904 (default :slant italic)
fefae026
MW
1905 (((min-colors 64)) :foreground "SkyBlue1")
1906 (((class color)) :foreground "cyan"))
1e7a9479 1907(mdw-define-face message-header-cc
4790fcb7 1908 (default :slant italic)
fefae026
MW
1909 (((min-colors 64)) :foreground "SeaGreen1")
1910 (((class color)) :foreground "green"))
1e7a9479 1911(mdw-define-face message-header-newsgroups
4790fcb7 1912 (default :slant italic)
fefae026
MW
1913 (((min-colors 64)) :foreground "SeaGreen1")
1914 (((class color)) :foreground "green"))
1e7a9479 1915(mdw-define-face message-header-subject
fefae026
MW
1916 (((min-colors 64)) :foreground "SeaGreen1")
1917 (((class color)) :foreground "green"))
1e7a9479 1918(mdw-define-face message-header-to
fefae026
MW
1919 (((min-colors 64)) :foreground "SeaGreen1")
1920 (((class color)) :foreground "green"))
1e7a9479 1921(mdw-define-face message-header-xheader
4790fcb7 1922 (default :slant italic)
fefae026
MW
1923 (((min-colors 64)) :foreground "SeaGreen1")
1924 (((class color)) :foreground "green"))
1e7a9479 1925(mdw-define-face message-header-other
4790fcb7 1926 (default :slant italic)
fefae026
MW
1927 (((min-colors 64)) :foreground "SeaGreen1")
1928 (((class color)) :foreground "green"))
1e7a9479 1929(mdw-define-face message-header-name
4790fcb7 1930 (default :weight bold)
fefae026
MW
1931 (((min-colors 64)) :foreground "SeaGreen1")
1932 (((class color)) :foreground "green"))
4790fcb7 1933
69498691
MW
1934(mdw-define-face which-func
1935 (t nil))
1e7a9479 1936
4790fcb7
MW
1937(mdw-define-face gnus-header-name
1938 (default :weight bold)
fefae026
MW
1939 (((min-colors 64)) :foreground "SeaGreen1")
1940 (((class color)) :foreground "green"))
4790fcb7 1941(mdw-define-face gnus-header-subject
fefae026
MW
1942 (((min-colors 64)) :foreground "SeaGreen1")
1943 (((class color)) :foreground "green"))
4790fcb7 1944(mdw-define-face gnus-header-from
fefae026
MW
1945 (((min-colors 64)) :foreground "SeaGreen1")
1946 (((class color)) :foreground "green"))
4790fcb7 1947(mdw-define-face gnus-header-to
fefae026
MW
1948 (((min-colors 64)) :foreground "SeaGreen1")
1949 (((class color)) :foreground "green"))
4790fcb7
MW
1950(mdw-define-face gnus-header-content
1951 (default :slant italic)
fefae026
MW
1952 (((min-colors 64)) :foreground "SeaGreen1")
1953 (((class color)) :foreground "green"))
4790fcb7
MW
1954
1955(mdw-define-face gnus-cite-1
fefae026
MW
1956 (((min-colors 64)) :foreground "SkyBlue1")
1957 (((class color)) :foreground "cyan"))
4790fcb7 1958(mdw-define-face gnus-cite-2
fefae026
MW
1959 (((min-colors 64)) :foreground "RoyalBlue2")
1960 (((class color)) :foreground "blue"))
4790fcb7 1961(mdw-define-face gnus-cite-3
fefae026
MW
1962 (((min-colors 64)) :foreground "MediumOrchid")
1963 (((class color)) :foreground "magenta"))
4790fcb7 1964(mdw-define-face gnus-cite-4
fefae026
MW
1965 (((min-colors 64)) :foreground "firebrick2")
1966 (((class color)) :foreground "red"))
4790fcb7 1967(mdw-define-face gnus-cite-5
fefae026
MW
1968 (((min-colors 64)) :foreground "burlywood2")
1969 (((class color)) :foreground "yellow"))
4790fcb7 1970(mdw-define-face gnus-cite-6
fefae026
MW
1971 (((min-colors 64)) :foreground "SeaGreen1")
1972 (((class color)) :foreground "green"))
4790fcb7 1973(mdw-define-face gnus-cite-7
fefae026
MW
1974 (((min-colors 64)) :foreground "SlateBlue1")
1975 (((class color)) :foreground "cyan"))
4790fcb7 1976(mdw-define-face gnus-cite-8
fefae026
MW
1977 (((min-colors 64)) :foreground "RoyalBlue2")
1978 (((class color)) :foreground "blue"))
4790fcb7 1979(mdw-define-face gnus-cite-9
fefae026
MW
1980 (((min-colors 64)) :foreground "purple2")
1981 (((class color)) :foreground "magenta"))
4790fcb7 1982(mdw-define-face gnus-cite-10
fefae026
MW
1983 (((min-colors 64)) :foreground "DarkOrange2")
1984 (((class color)) :foreground "red"))
4790fcb7
MW
1985(mdw-define-face gnus-cite-11
1986 (t :foreground "grey"))
1987
b911d2f6
MW
1988(mdw-define-face gnus-emphasis-underline
1989 (((type tty)) :underline t)
1990 (t :slant italic))
1991
2f238de8
MW
1992(mdw-define-face diff-header
1993 (t nil))
1e7a9479
MW
1994(mdw-define-face diff-index
1995 (t :weight bold))
1996(mdw-define-face diff-file-header
1997 (t :weight bold))
1998(mdw-define-face diff-hunk-header
fefae026
MW
1999 (((min-colors 64)) :foreground "SkyBlue1")
2000 (((class color)) :foreground "cyan"))
1e7a9479 2001(mdw-define-face diff-function
fefae026
MW
2002 (default :weight bold)
2003 (((min-colors 64)) :foreground "SkyBlue1")
2004 (((class color)) :foreground "cyan"))
1e7a9479 2005(mdw-define-face diff-header
fefae026 2006 (((min-colors 64)) :background "grey10"))
1e7a9479 2007(mdw-define-face diff-added
fefae026 2008 (((class color)) :foreground "green"))
1e7a9479 2009(mdw-define-face diff-removed
fefae026 2010 (((class color)) :foreground "red"))
5fd055c2
MW
2011(mdw-define-face diff-context
2012 (t nil))
2f238de8 2013(mdw-define-face diff-refine-change
fefae026 2014 (((min-colors 64)) :background "RoyalBlue4")
b31f422b 2015 (t :underline t))
5f454d3e 2016(mdw-define-face diff-refine-removed
fefae026 2017 (((min-colors 64)) :background "#500")
5f454d3e
MW
2018 (t :underline t))
2019(mdw-define-face diff-refine-added
fefae026 2020 (((min-colors 64)) :background "#050")
5f454d3e 2021 (t :underline t))
1e7a9479 2022
a62d0541
MW
2023(setq ediff-force-faces t)
2024(mdw-define-face ediff-current-diff-A
fefae026
MW
2025 (((min-colors 64)) :background "darkred")
2026 (((class color)) :background "red")
a62d0541
MW
2027 (t :inverse-video t))
2028(mdw-define-face ediff-fine-diff-A
fefae026
MW
2029 (((min-colors 64)) :background "red3")
2030 (((class color)) :inverse-video t)
a62d0541
MW
2031 (t :inverse-video nil))
2032(mdw-define-face ediff-even-diff-A
fefae026 2033 (((min-colors 64)) :background "#300"))
a62d0541 2034(mdw-define-face ediff-odd-diff-A
fefae026 2035 (((min-colors 64)) :background "#300"))
a62d0541 2036(mdw-define-face ediff-current-diff-B
fefae026
MW
2037 (((min-colors 64)) :background "darkgreen")
2038 (((class color)) :background "magenta")
a62d0541
MW
2039 (t :inverse-video t))
2040(mdw-define-face ediff-fine-diff-B
fefae026
MW
2041 (((min-colors 64)) :background "green4")
2042 (((class color)) :inverse-video t)
a62d0541
MW
2043 (t :inverse-video nil))
2044(mdw-define-face ediff-even-diff-B
fefae026 2045 (((min-colors 64)) :background "#020"))
a62d0541 2046(mdw-define-face ediff-odd-diff-B
fefae026 2047 (((min-colors 64)) :background "#020"))
a62d0541 2048(mdw-define-face ediff-current-diff-C
fefae026
MW
2049 (((min-colors 64)) :background "darkblue")
2050 (((class color)) :background "blue")
a62d0541
MW
2051 (t :inverse-video t))
2052(mdw-define-face ediff-fine-diff-C
fefae026
MW
2053 (((min-colors 64)) :background "blue1")
2054 (((class color)) :inverse-video t)
a62d0541
MW
2055 (t :inverse-video nil))
2056(mdw-define-face ediff-even-diff-C
fefae026 2057 (((min-colors 64)) :background "#004"))
a62d0541 2058(mdw-define-face ediff-odd-diff-C
fefae026 2059 (((min-colors 64)) :background "#004"))
a62d0541 2060(mdw-define-face ediff-current-diff-Ancestor
fefae026
MW
2061 (((min-colors 64)) :background "#630")
2062 (((class color)) :background "blue")
a62d0541
MW
2063 (t :inverse-video t))
2064(mdw-define-face ediff-even-diff-Ancestor
fefae026 2065 (((min-colors 64)) :background "#320"))
a62d0541 2066(mdw-define-face ediff-odd-diff-Ancestor
fefae026 2067 (((min-colors 64)) :background "#320"))
a62d0541 2068
53f93f0d 2069(mdw-define-face magit-hash
fefae026
MW
2070 (((min-colors 64)) :foreground "grey40")
2071 (((class color)) :foreground "blue"))
53f93f0d 2072(mdw-define-face magit-diff-hunk-heading
fefae026
MW
2073 (((min-colors 64)) :foreground "grey70" :background "grey25")
2074 (((class color)) :foreground "yellow"))
53f93f0d 2075(mdw-define-face magit-diff-hunk-heading-highlight
fefae026
MW
2076 (((min-colors 64)) :foreground "grey70" :background "grey35")
2077 (((class color)) :foreground "yellow" :background "blue"))
53f93f0d 2078(mdw-define-face magit-diff-added
fefae026
MW
2079 (((min-colors 64)) :foreground "#ddffdd" :background "#335533")
2080 (((class color)) :foreground "green"))
53f93f0d 2081(mdw-define-face magit-diff-added-highlight
fefae026
MW
2082 (((min-colors 64)) :foreground "#cceecc" :background "#336633")
2083 (((class color)) :foreground "green" :background "blue"))
53f93f0d 2084(mdw-define-face magit-diff-removed
fefae026
MW
2085 (((min-colors 64)) :foreground "#ffdddd" :background "#553333")
2086 (((class color)) :foreground "red"))
53f93f0d 2087(mdw-define-face magit-diff-removed-highlight
fefae026
MW
2088 (((min-colors 64)) :foreground "#eecccc" :background "#663333")
2089 (((class color)) :foreground "red" :background "blue"))
857045c6
MW
2090(mdw-define-face magit-blame-heading
2091 (((min-colors 64)) :foreground "white" :background "grey25"
2092 :weight normal :slant normal)
2093 (((class color)) :foreground "white" :background "blue"
2094 :weight normal :slant normal))
2095(mdw-define-face magit-blame-name
2096 (t :inherit magit-blame-heading :slant italic))
2097(mdw-define-face magit-blame-date
2098 (((min-colors 64)) :inherit magit-blame-heading :foreground "grey60")
2099 (((class color)) :inherit magit-blame-heading :foreground "cyan"))
2100(mdw-define-face magit-blame-summary
2101 (t :inherit magit-blame-heading :weight bold))
53f93f0d 2102
ad305d7e 2103(mdw-define-face dylan-header-background
fefae026
MW
2104 (((min-colors 64)) :background "NavyBlue")
2105 (((class color)) :background "blue"))
ad305d7e 2106
e1b8de18
MW
2107(mdw-define-face erc-input-face
2108 (t :foreground "red"))
2109
1e7a9479
MW
2110(mdw-define-face woman-bold
2111 (t :weight bold))
2112(mdw-define-face woman-italic
2113 (t :slant italic))
2114
5a83259f
MW
2115(eval-after-load "rst"
2116 '(progn
2117 (mdw-define-face rst-level-1-face
2118 (t :foreground "SkyBlue1" :weight bold))
2119 (mdw-define-face rst-level-2-face
2120 (t :foreground "SeaGreen1" :weight bold))
2121 (mdw-define-face rst-level-3-face
2122 (t :weight bold))
2123 (mdw-define-face rst-level-4-face
2124 (t :slant italic))
2125 (mdw-define-face rst-level-5-face
2126 (t :underline t))
2127 (mdw-define-face rst-level-6-face
2128 ())))
4f251391 2129
1e7a9479
MW
2130(mdw-define-face p4-depot-added-face
2131 (t :foreground "green"))
2132(mdw-define-face p4-depot-branch-op-face
2133 (t :foreground "yellow"))
2134(mdw-define-face p4-depot-deleted-face
2135 (t :foreground "red"))
2136(mdw-define-face p4-depot-unmapped-face
2137 (t :foreground "SkyBlue1"))
2138(mdw-define-face p4-diff-change-face
2139 (t :foreground "yellow"))
2140(mdw-define-face p4-diff-del-face
2141 (t :foreground "red"))
2142(mdw-define-face p4-diff-file-face
2143 (t :foreground "SkyBlue1"))
2144(mdw-define-face p4-diff-head-face
2145 (t :background "grey10"))
2146(mdw-define-face p4-diff-ins-face
2147 (t :foreground "green"))
2148
4c39e530
MW
2149(mdw-define-face w3m-anchor-face
2150 (t :foreground "SkyBlue1" :underline t))
2151(mdw-define-face w3m-arrived-anchor-face
2152 (t :foreground "SkyBlue1" :underline t))
2153
1e7a9479
MW
2154(mdw-define-face whizzy-slice-face
2155 (t :background "grey10"))
2156(mdw-define-face whizzy-error-face
2157 (t :background "darkred"))
f617db13 2158
5fedb342
MW
2159;; Ellipses used to indicate hidden text (and similar).
2160(mdw-define-face mdw-ellipsis-face
2161 (((type tty)) :foreground "blue") (t :foreground "grey60"))
c11ac343 2162(let ((dollar (make-glyph-code ?$ 'mdw-ellipsis-face))
a8a7976a 2163 (backslash (make-glyph-code ?\\ 'mdw-ellipsis-face))
c11ac343
MW
2164 (dot (make-glyph-code ?. 'mdw-ellipsis-face))
2165 (bar (make-glyph-code ?| mdw-ellipsis-face)))
2166 (set-display-table-slot standard-display-table 0 dollar)
2167 (set-display-table-slot standard-display-table 1 backslash)
5fedb342 2168 (set-display-table-slot standard-display-table 4
c11ac343
MW
2169 (vector dot dot dot))
2170 (set-display-table-slot standard-display-table 5 bar))
5fedb342 2171
6132bc01 2172;;;--------------------------------------------------------------------------
c70e3179
MW
2173;;; Where is point?
2174
6a2d05ae 2175(mdw-define-face mdw-point-overlay-face
3f32879e 2176 (((type graphic)))
c70e3179
MW
2177 (((min-colors 64)) :background "darkblue")
2178 (((class color)) :background "blue")
2179 (((type tty) (class mono)) :inverse-video t))
2180
3ce407bb
MW
2181(defcustom mdw-point-overlay-fringe-display '(vertical-bar . vertical-bar)
2182 "Bitmaps to display in the left and right fringes in the current line."
2183 :type '(cons symbol symbol))
c70e3179
MW
2184
2185(defun mdw-configure-point-overlay ()
2186 (let ((ov (make-overlay 0 0)))
2187 (overlay-put ov 'priority 0)
2188 (let* ((fringe (or mdw-point-overlay-fringe-display (cons nil nil)))
2189 (left (car fringe)) (right (cdr fringe))
2190 (s ""))
2191 (when left
2192 (let ((ss "."))
2193 (put-text-property 0 1 'display `(left-fringe ,left) ss)
2194 (setq s (concat s ss))))
2195 (when right
2196 (let ((ss "."))
2197 (put-text-property 0 1 'display `(right-fringe ,right) ss)
2198 (setq s (concat s ss))))
2199 (when (or left right)
2200 (overlay-put ov 'before-string s)))
6a2d05ae 2201 (overlay-put ov 'face 'mdw-point-overlay-face)
c70e3179
MW
2202 (delete-overlay ov)
2203 ov))
2204
2205(defvar mdw-point-overlay (mdw-configure-point-overlay)
2206 "An overlay used for showing where point is in the selected window.")
2207(defun mdw-reconfigure-point-overlay ()
2208 (interactive)
2209 (setq mdw-point-overlay (mdw-configure-point-overlay)))
2210
2211(defun mdw-remove-point-overlay ()
2212 "Remove the current-point overlay."
2213 (delete-overlay mdw-point-overlay))
2214
2215(defun mdw-update-point-overlay ()
2216 "Mark the current point position with an overlay."
2217 (if (not mdw-point-overlay-mode)
2218 (mdw-remove-point-overlay)
2219 (overlay-put mdw-point-overlay 'window (selected-window))
2220 (move-overlay mdw-point-overlay
2221 (line-beginning-position)
2222 (+ (line-end-position) 1))))
2223
2224(defvar mdw-point-overlay-buffers nil
2225 "List of buffers using `mdw-point-overlay-mode'.")
2226
2227(define-minor-mode mdw-point-overlay-mode
2228 "Indicate current line with an overlay."
2229 :global nil
2230 (let ((buffer (current-buffer)))
2231 (setq mdw-point-overlay-buffers
535c927f
MW
2232 (mapcan (lambda (buf)
2233 (if (and (buffer-live-p buf)
2234 (not (eq buf buffer)))
2235 (list buf)))
2236 mdw-point-overlay-buffers))
c70e3179
MW
2237 (if mdw-point-overlay-mode
2238 (setq mdw-point-overlay-buffers
535c927f 2239 (cons buffer mdw-point-overlay-buffers))))
c70e3179
MW
2240 (cond (mdw-point-overlay-buffers
2241 (add-hook 'pre-command-hook 'mdw-remove-point-overlay)
2242 (add-hook 'post-command-hook 'mdw-update-point-overlay))
2243 (t
2244 (mdw-remove-point-overlay)
2245 (remove-hook 'pre-command-hook 'mdw-remove-point-overlay)
2246 (remove-hook 'post-command-hook 'mdw-update-point-overlay))))
2247
2248(define-globalized-minor-mode mdw-global-point-overlay-mode
2249 mdw-point-overlay-mode
2250 (lambda () (if (not (minibufferp)) (mdw-point-overlay-mode t))))
2251
07e1e1f8 2252(defvar mdw-terminal-title-alist nil)
18cdb023
MW
2253(defun mdw-update-terminal-title ()
2254 (when (let ((term (frame-parameter nil 'tty-type)))
2255 (and term (string-match "^xterm" term)))
2256 (let* ((tty (frame-parameter nil 'tty))
07e1e1f8 2257 (old (assoc tty mdw-terminal-title-alist))
18cdb023 2258 (new (format-mode-line frame-title-format)))
165a1e68 2259 (unless (and old (equal (cdr old) new))
18cdb023 2260 (if old (rplacd old new)
07e1e1f8 2261 (setq mdw-terminal-title-alist
535c927f 2262 (cons (cons tty new) mdw-terminal-title-alist)))
18cdb023
MW
2263 (send-string-to-terminal (concat "\e]2;" new "\e\\"))))))
2264
2265(add-hook 'post-command-hook 'mdw-update-terminal-title)
2266
c70e3179 2267;;;--------------------------------------------------------------------------
6938850a
MW
2268;;; Ediff hacking.
2269
2270(defvar mdw-ediff-previous-windows)
2271(defun mdw-ediff-setup ()
2272 (setq mdw-ediff-previous-windows (current-window-configuration)))
2273(defun mdw-ediff-suspend-or-quit ()
2274 (set-window-configuration mdw-ediff-previous-windows))
2275(add-hook 'ediff-before-setup-hook 'mdw-ediff-setup)
2276(add-hook 'ediff-quit-hook 'mdw-ediff-suspend-or-quit t)
2277(add-hook 'ediff-suspend-hook 'mdw-ediff-suspend-or-quit t)
2278
2279;;;--------------------------------------------------------------------------
6132bc01 2280;;; C programming configuration.
f617db13 2281
6132bc01 2282;; Make C indentation nice.
f617db13 2283
f50c1bed
MW
2284(defun mdw-c-lineup-arglist (langelem)
2285 "Hack for DWIMmery in c-lineup-arglist."
2286 (if (save-excursion
2287 (c-block-in-arglist-dwim (c-langelem-2nd-pos c-syntactic-element)))
2288 0
2289 (c-lineup-arglist langelem)))
2290
2291(defun mdw-c-indent-extern-mumble (langelem)
2292 "Indent `extern \"...\" {' lines."
2293 (save-excursion
2294 (back-to-indentation)
2295 (if (looking-at
2296 "\\s-*\\<extern\\>\\s-*\"\\([^\\\\\"]+\\|\\.\\)*\"\\s-*{")
2297 c-basic-offset
2298 nil)))
2299
b521d36a
MW
2300(defun mdw-c-indent-arglist-nested (langelem)
2301 "Indent continued argument lists.
2302If we've nested more than one argument list, then only introduce a single
2303indentation anyway."
2304 (let ((context c-syntactic-context)
2305 (pos (c-langelem-2nd-pos c-syntactic-element))
2306 (should-indent-p t))
2307 (while (and context
2308 (eq (caar context) 'arglist-cont-nonempty))
2309 (when (and (= (caddr (pop context)) pos)
2310 context
2311 (memq (caar context) '(arglist-intro
2312 arglist-cont-nonempty)))
2313 (setq should-indent-p nil)))
2314 (if should-indent-p '+ 0)))
2315
c56296d0
MW
2316(defvar mdw-define-c-styles-hook nil
2317 "Hook run when `cc-mode' starts up to define styles.")
2318
e5751a93
MW
2319(defun mdw-merge-style-alists (first second)
2320 (let ((output nil))
2321 (dolist (item first)
2322 (let ((key (car item)) (value (cdr item)))
be2322df
MW
2323 (if (let* ((key-name (symbol-name key))
2324 (key-len (length key-name)))
2325 (and (>= key-len 5)
2326 (string= (subseq key-name (- key-len 5)) "-alist")))
e5751a93
MW
2327 (push (cons key
2328 (mdw-merge-style-alists value
2329 (cdr (assoc key second))))
2330 output)
2331 (push item output))))
2332 (dolist (item second)
2333 (unless (assoc (car item) first)
2334 (push item output)))
2335 (nreverse output)))
2336
a151a655 2337(defmacro* mdw-define-c-style (name (&optional parent) &rest assocs)
e5751a93 2338 "Define a C style, called NAME (a symbol) based on PARENT, setting ASSOCs.
c56296d0
MW
2339A function, named `mdw-define-c-style/NAME', is defined to actually install
2340the style using `c-add-style', and added to the hook
2341`mdw-define-c-styles-hook'. If CC Mode is already loaded, then the style is
2342set."
2343 (declare (indent defun))
2344 (let* ((name-string (symbol-name name))
e5751a93 2345 (var (intern (concat "mdw-c-style/" name-string)))
c56296d0
MW
2346 (func (intern (concat "mdw-define-c-style/" name-string))))
2347 `(progn
e5751a93 2348 (setq ,var
535c927f
MW
2349 ,(if (null parent)
2350 `',assocs
2351 (let ((parent-list (intern (concat "mdw-c-style/"
2352 (symbol-name parent)))))
2353 `(mdw-merge-style-alists ',assocs ,parent-list))))
e5751a93 2354 (defun ,func () (c-add-style ,name-string ,var))
c56296d0 2355 (and (featurep 'cc-mode) (,func))
e5751a93
MW
2356 (add-hook 'mdw-define-c-styles-hook ',func)
2357 ',name)))
c56296d0
MW
2358
2359(eval-after-load "cc-mode"
2360 '(run-hooks 'mdw-define-c-styles-hook))
2361
e5751a93 2362(mdw-define-c-style mdw-c ()
8ad8e046
MW
2363 (c-basic-offset . 2)
2364 (comment-column . 40)
b521d36a 2365 (c-class-key . "class")
8ad8e046 2366 (c-backslash-column . 72)
b521d36a
MW
2367 (c-label-minimum-indentation . 0)
2368 (c-offsets-alist (substatement-open . (add 0 c-indent-one-line-block))
2369 (defun-open . (add 0 c-indent-one-line-block))
8ad8e046 2370 (arglist-cont-nonempty . mdw-c-lineup-arglist)
b521d36a
MW
2371 (topmost-intro . mdw-c-indent-extern-mumble)
2372 (cpp-define-intro . 0)
2373 (knr-argdecl . 0)
2374 (inextern-lang . [0])
2375 (label . 0)
2376 (case-label . +)
8ad8e046 2377 (access-label . -)
b521d36a
MW
2378 (inclass . +)
2379 (inline-open . ++)
2380 (statement-cont . +)
2381 (statement-case-intro . +)))
2382
28082234 2383(mdw-define-c-style mdw-trustonic-c (mdw-c)
8ad8e046 2384 (c-basic-offset . 4)
e555fac7
MW
2385 (c-offsets-alist (access-label . -2)))
2386
28082234
MW
2387(mdw-define-c-style mdw-trustonic-alec-c (mdw-trustonic-c)
2388 (comment-column . 0)
84a086a6
MW
2389 (c-indent-comment-alist (anchored-comment . (column . 0))
2390 (end-block . (space . 1))
2391 (cpp-end-block . (space . 1))
2392 (other . (space . 1)))
e555fac7 2393 (c-offsets-alist (arglist-cont-nonempty . mdw-c-indent-arglist-nested)))
c56296d0
MW
2394
2395(defun mdw-set-default-c-style (modes style)
2396 "Update the default CC Mode style for MODES to be STYLE.
2397
2398MODES may be a list of major mode names or a singleton. STYLE is a style
2399name, as a symbol."
2400 (let ((modes (if (listp modes) modes (list modes)))
2401 (style (symbol-name style)))
2402 (setq c-default-style
535c927f
MW
2403 (append (mapcar (lambda (mode)
2404 (cons mode style))
2405 modes)
2406 (remove-if (lambda (assoc)
2407 (memq (car assoc) modes))
2408 (if (listp c-default-style)
2409 c-default-style
2410 (list (cons 'other c-default-style))))))))
c56296d0
MW
2411(setq c-default-style "mdw-c")
2412
2413(mdw-set-default-c-style '(c-mode c++-mode) 'mdw-c)
f617db13 2414
0e7d960b
MW
2415(defvar mdw-c-comment-fill-prefix
2416 `((,(concat "\\([ \t]*/?\\)"
a7474429 2417 "\\(\\*\\|//\\)"
0e7d960b
MW
2418 "\\([ \t]*\\)"
2419 "\\([A-Za-z]+:[ \t]*\\)?"
2420 mdw-hanging-indents)
2421 (pad . 1) (match . 2) (pad . 3) (pad . 4) (pad . 5)))
2422 "Fill prefix matching C comments (both kinds).")
2423
f617db13
MW
2424(defun mdw-fontify-c-and-c++ ()
2425
6132bc01 2426 ;; Fiddle with some syntax codes.
f617db13
MW
2427 (modify-syntax-entry ?* ". 23")
2428 (modify-syntax-entry ?/ ". 124b")
2429 (modify-syntax-entry ?\n "> b")
2430
6132bc01 2431 ;; Other stuff.
c56296d0 2432 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
f617db13 2433
6132bc01 2434 ;; Now define things to be fontified.
02109a0d 2435 (make-local-variable 'font-lock-keywords)
f617db13 2436 (let ((c-keywords
fe307a8c
MW
2437 (mdw-regexps "alignas" ;C11 macro, C++11
2438 "alignof" ;C++11
2439 "and" ;C++, C95 macro
0681f29e 2440 "and_eq" ;C++, C95 macro
7b84c078 2441 "asm" ;K&R, C++, GCC
fe307a8c 2442 "atomic" ;C11 macro, C++11 template type
26f18bd1 2443 "auto" ;K&R, C89
0681f29e
MW
2444 "bitand" ;C++, C95 macro
2445 "bitor" ;C++, C95 macro
d4783d9c 2446 "bool" ;C++, C99 macro
26f18bd1
MW
2447 "break" ;K&R, C89
2448 "case" ;K&R, C89
2449 "catch" ;C++
2450 "char" ;K&R, C89
fe307a8c
MW
2451 "char16_t" ;C++11, C11 library type
2452 "char32_t" ;C++11, C11 library type
26f18bd1 2453 "class" ;C++
d4783d9c 2454 "complex" ;C99 macro, C++ template type
0681f29e 2455 "compl" ;C++, C95 macro
26f18bd1 2456 "const" ;C89
fe307a8c 2457 "constexpr" ;C++11
26f18bd1
MW
2458 "const_cast" ;C++
2459 "continue" ;K&R, C89
fe307a8c 2460 "decltype" ;C++11
26f18bd1
MW
2461 "defined" ;C89 preprocessor
2462 "default" ;K&R, C89
2463 "delete" ;C++
2464 "do" ;K&R, C89
2465 "double" ;K&R, C89
2466 "dynamic_cast" ;C++
2467 "else" ;K&R, C89
2468 ;; "entry" ;K&R -- never used
2469 "enum" ;C89
2470 "explicit" ;C++
2471 "export" ;C++
2472 "extern" ;K&R, C89
2473 "float" ;K&R, C89
2474 "for" ;K&R, C89
2475 ;; "fortran" ;K&R
2476 "friend" ;C++
2477 "goto" ;K&R, C89
2478 "if" ;K&R, C89
d4783d9c
MW
2479 "imaginary" ;C99 macro
2480 "inline" ;C++, C99, GCC
26f18bd1
MW
2481 "int" ;K&R, C89
2482 "long" ;K&R, C89
2483 "mutable" ;C++
2484 "namespace" ;C++
2485 "new" ;C++
fe307a8c
MW
2486 "noexcept" ;C++11
2487 "noreturn" ;C11 macro
0681f29e
MW
2488 "not" ;C++, C95 macro
2489 "not_eq" ;C++, C95 macro
fe307a8c 2490 "nullptr" ;C++11
26f18bd1 2491 "operator" ;C++
0681f29e
MW
2492 "or" ;C++, C95 macro
2493 "or_eq" ;C++, C95 macro
26f18bd1
MW
2494 "private" ;C++
2495 "protected" ;C++
2496 "public" ;C++
2497 "register" ;K&R, C89
8d6d55b9 2498 "reinterpret_cast" ;C++
d4783d9c 2499 "restrict" ;C99
8d6d55b9
MW
2500 "return" ;K&R, C89
2501 "short" ;K&R, C89
2502 "signed" ;C89
2503 "sizeof" ;K&R, C89
2504 "static" ;K&R, C89
fe307a8c 2505 "static_assert" ;C11 macro, C++11
8d6d55b9
MW
2506 "static_cast" ;C++
2507 "struct" ;K&R, C89
2508 "switch" ;K&R, C89
2509 "template" ;C++
8d6d55b9 2510 "throw" ;C++
8d6d55b9 2511 "try" ;C++
fe307a8c 2512 "thread_local" ;C11 macro, C++11
8d6d55b9
MW
2513 "typedef" ;C89
2514 "typeid" ;C++
2515 "typeof" ;GCC
2516 "typename" ;C++
2517 "union" ;K&R, C89
2518 "unsigned" ;K&R, C89
2519 "using" ;C++
2520 "virtual" ;C++
2521 "void" ;C89
2522 "volatile" ;C89
2523 "wchar_t" ;C++, C89 library type
2524 "while" ;K&R, C89
0681f29e
MW
2525 "xor" ;C++, C95 macro
2526 "xor_eq" ;C++, C95 macro
fe307a8c
MW
2527 "_Alignas" ;C11
2528 "_Alignof" ;C11
2529 "_Atomic" ;C11
d4783d9c
MW
2530 "_Bool" ;C99
2531 "_Complex" ;C99
fe307a8c 2532 "_Generic" ;C11
d4783d9c 2533 "_Imaginary" ;C99
fe307a8c 2534 "_Noreturn" ;C11
d4783d9c 2535 "_Pragma" ;C99 preprocessor
fe307a8c
MW
2536 "_Static_assert" ;C11
2537 "_Thread_local" ;C11
8d6d55b9
MW
2538 "__alignof__" ;GCC
2539 "__asm__" ;GCC
2540 "__attribute__" ;GCC
2541 "__complex__" ;GCC
2542 "__const__" ;GCC
2543 "__extension__" ;GCC
2544 "__imag__" ;GCC
2545 "__inline__" ;GCC
2546 "__label__" ;GCC
2547 "__real__" ;GCC
2548 "__signed__" ;GCC
2549 "__typeof__" ;GCC
2550 "__volatile__" ;GCC
2551 ))
300f8827 2552 (c-builtins
26f18bd1 2553 (mdw-regexps "false" ;C++, C99 macro
165cecf8 2554 "this" ;C++
26f18bd1 2555 "true" ;C++, C99 macro
165cecf8 2556 ))
f617db13 2557 (preprocessor-keywords
8d6d55b9
MW
2558 (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
2559 "ident" "if" "ifdef" "ifndef" "import" "include"
2560 "line" "pragma" "unassert" "undef" "warning"))
f617db13 2561 (objc-keywords
8d6d55b9
MW
2562 (mdw-regexps "class" "defs" "encode" "end" "implementation"
2563 "interface" "private" "protected" "protocol" "public"
2564 "selector")))
f617db13
MW
2565
2566 (setq font-lock-keywords
535c927f 2567 (list
f617db13 2568
535c927f
MW
2569 ;; Fontify include files as strings.
2570 (list (concat "^[ \t]*\\#[ \t]*"
2571 "\\(include\\|import\\)"
2572 "[ \t]*\\(<[^>]+>?\\)")
2573 '(2 font-lock-string-face))
2574
2575 ;; Preprocessor directives are `references'?.
2576 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
2577 preprocessor-keywords
2578 "\\)\\>\\|[0-9]+\\|$\\)\\)")
2579 '(1 font-lock-keyword-face))
2580
2581 ;; Handle the keywords defined above.
2582 (list (concat "@\\<\\(" objc-keywords "\\)\\>")
2583 '(0 font-lock-keyword-face))
2584
2585 (list (concat "\\<\\(" c-keywords "\\)\\>")
2586 '(0 font-lock-keyword-face))
2587
2588 (list (concat "\\<\\(" c-builtins "\\)\\>")
2589 '(0 font-lock-variable-name-face))
2590
2591 ;; Handle numbers too.
2592 ;;
2593 ;; This looks strange, I know. It corresponds to the
2594 ;; preprocessor's idea of what a number looks like, rather than
2595 ;; anything sensible.
2596 (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
2597 "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
2598 '(0 mdw-number-face))
2599
2600 ;; And anything else is punctuation.
2601 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2602 '(0 mdw-punct-face))))))
f617db13 2603
fb16ed85
MW
2604(define-derived-mode sod-mode c-mode "Sod"
2605 "Major mode for editing Sod code.")
2606(push '("\\.sod$" . sod-mode) auto-mode-alist)
2607
b50c6712
MW
2608(dolist (hook '(c-mode-hook objc-mode-hook c++-mode-hook))
2609 (add-hook hook 'mdw-misc-mode-config t)
2610 (add-hook hook 'mdw-fontify-c-and-c++ t))
2611
6132bc01
MW
2612;;;--------------------------------------------------------------------------
2613;;; AP calc mode.
f617db13 2614
e7186cbe
MW
2615(define-derived-mode apcalc-mode c-mode "AP Calc"
2616 "Major mode for editing Calc code.")
f617db13
MW
2617
2618(defun mdw-fontify-apcalc ()
2619
6132bc01 2620 ;; Fiddle with some syntax codes.
f617db13
MW
2621 (modify-syntax-entry ?* ". 23")
2622 (modify-syntax-entry ?/ ". 14")
2623
6132bc01 2624 ;; Other stuff.
f617db13
MW
2625 (setq comment-start "/* ")
2626 (setq comment-end " */")
0e7d960b 2627 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
f617db13 2628
6132bc01 2629 ;; Now define things to be fontified.
02109a0d 2630 (make-local-variable 'font-lock-keywords)
f617db13 2631 (let ((c-keywords
8d6d55b9
MW
2632 (mdw-regexps "break" "case" "cd" "continue" "define" "default"
2633 "do" "else" "exit" "for" "global" "goto" "help" "if"
2634 "local" "mat" "obj" "print" "quit" "read" "return"
2635 "show" "static" "switch" "while" "write")))
f617db13
MW
2636
2637 (setq font-lock-keywords
535c927f 2638 (list
f617db13 2639
535c927f
MW
2640 ;; Handle the keywords defined above.
2641 (list (concat "\\<\\(" c-keywords "\\)\\>")
2642 '(0 font-lock-keyword-face))
f617db13 2643
535c927f
MW
2644 ;; Handle numbers too.
2645 ;;
2646 ;; This looks strange, I know. It corresponds to the
2647 ;; preprocessor's idea of what a number looks like, rather than
2648 ;; anything sensible.
2649 (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
2650 "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
2651 '(0 mdw-number-face))
f617db13 2652
535c927f
MW
2653 ;; And anything else is punctuation.
2654 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2655 '(0 mdw-punct-face))))))
f617db13 2656
b50c6712
MW
2657(progn
2658 (add-hook 'apcalc-mode-hook 'mdw-misc-mode-config t)
2659 (add-hook 'apcalc-mode-hook 'mdw-fontify-apcalc t))
2660
6132bc01
MW
2661;;;--------------------------------------------------------------------------
2662;;; Java programming configuration.
f617db13 2663
6132bc01 2664;; Make indentation nice.
f617db13 2665
a5807b1e 2666(mdw-define-c-style mdw-java ()
c56296d0
MW
2667 (c-basic-offset . 2)
2668 (c-backslash-column . 72)
2669 (c-offsets-alist (substatement-open . 0)
2670 (label . +)
2671 (case-label . +)
2672 (access-label . 0)
2673 (inclass . +)
2674 (statement-case-intro . +)))
2675(mdw-set-default-c-style 'java-mode 'mdw-java)
f617db13 2676
6132bc01 2677;; Declare Java fontification style.
f617db13
MW
2678
2679(defun mdw-fontify-java ()
2680
36eabf61
MW
2681 ;; Fiddle with some syntax codes.
2682 (modify-syntax-entry ?@ ".")
2683 (modify-syntax-entry ?@ "." font-lock-syntax-table)
2684
6132bc01 2685 ;; Other stuff.
0e7d960b 2686 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
f617db13 2687
6132bc01 2688 ;; Now define things to be fontified.
02109a0d 2689 (make-local-variable 'font-lock-keywords)
f617db13 2690 (let ((java-keywords
853d8555
MW
2691 (mdw-regexps "abstract" "assert"
2692 "boolean" "break" "byte"
2693 "case" "catch" "char" "class" "const" "continue"
2694 "default" "do" "double"
2695 "else" "enum" "extends"
2696 "final" "finally" "float" "for"
2697 "goto"
2698 "if" "implements" "import" "instanceof" "int"
2699 "interface"
2700 "long"
2701 "native" "new"
2702 "package" "private" "protected" "public"
2703 "return"
2704 "short" "static" "strictfp" "switch" "synchronized"
2705 "throw" "throws" "transient" "try"
2706 "void" "volatile"
2707 "while"))
8d6d55b9 2708
300f8827 2709 (java-builtins
165cecf8 2710 (mdw-regexps "false" "null" "super" "this" "true")))
f617db13
MW
2711
2712 (setq font-lock-keywords
535c927f 2713 (list
f617db13 2714
535c927f
MW
2715 ;; Handle the keywords defined above.
2716 (list (concat "\\<\\(" java-keywords "\\)\\>")
2717 '(0 font-lock-keyword-face))
f617db13 2718
535c927f
MW
2719 ;; Handle the magic builtins defined above.
2720 (list (concat "\\<\\(" java-builtins "\\)\\>")
2721 '(0 font-lock-variable-name-face))
f617db13 2722
535c927f
MW
2723 ;; Handle numbers too.
2724 ;;
2725 ;; The following isn't quite right, but it's close enough.
2726 (list (concat "\\<\\("
2727 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2728 "[0-9]+\\(\\.[0-9]*\\)?"
2729 "\\([eE][-+]?[0-9]+\\)?\\)"
2730 "[lLfFdD]?")
2731 '(0 mdw-number-face))
2732
2733 ;; And anything else is punctuation.
2734 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2735 '(0 mdw-punct-face))))))
f617db13 2736
b50c6712
MW
2737(progn
2738 (add-hook 'java-mode-hook 'mdw-misc-mode-config t)
2739 (add-hook 'java-mode-hook 'mdw-fontify-java t))
2740
6132bc01 2741;;;--------------------------------------------------------------------------
61d63206
MW
2742;;; Javascript programming configuration.
2743
2744(defun mdw-javascript-style ()
2745 (setq js-indent-level 2)
2746 (setq js-expr-indent-offset 0))
2747
2748(defun mdw-fontify-javascript ()
2749
2750 ;; Other stuff.
2751 (mdw-javascript-style)
2752 (setq js-auto-indent-flag t)
2753
2754 ;; Now define things to be fontified.
2755 (make-local-variable 'font-lock-keywords)
2756 (let ((javascript-keywords
2757 (mdw-regexps "abstract" "boolean" "break" "byte" "case" "catch"
2758 "char" "class" "const" "continue" "debugger" "default"
2759 "delete" "do" "double" "else" "enum" "export" "extends"
2760 "final" "finally" "float" "for" "function" "goto" "if"
2761 "implements" "import" "in" "instanceof" "int"
2762 "interface" "let" "long" "native" "new" "package"
2763 "private" "protected" "public" "return" "short"
2764 "static" "super" "switch" "synchronized" "throw"
2765 "throws" "transient" "try" "typeof" "var" "void"
4e23ea53 2766 "volatile" "while" "with" "yield"))
300f8827 2767 (javascript-builtins
61d63206
MW
2768 (mdw-regexps "false" "null" "undefined" "Infinity" "NaN" "true"
2769 "arguments" "this")))
2770
2771 (setq font-lock-keywords
535c927f 2772 (list
61d63206 2773
535c927f
MW
2774 ;; Handle the keywords defined above.
2775 (list (concat "\\_<\\(" javascript-keywords "\\)\\_>")
2776 '(0 font-lock-keyword-face))
61d63206 2777
535c927f
MW
2778 ;; Handle the predefined builtins defined above.
2779 (list (concat "\\_<\\(" javascript-builtins "\\)\\_>")
2780 '(0 font-lock-variable-name-face))
61d63206 2781
535c927f
MW
2782 ;; Handle numbers too.
2783 ;;
2784 ;; The following isn't quite right, but it's close enough.
2785 (list (concat "\\_<\\("
2786 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2787 "[0-9]+\\(\\.[0-9]*\\)?"
2788 "\\([eE][-+]?[0-9]+\\)?\\)"
2789 "[lLfFdD]?")
2790 '(0 mdw-number-face))
2791
2792 ;; And anything else is punctuation.
2793 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2794 '(0 mdw-punct-face))))))
61d63206 2795
b50c6712
MW
2796(progn
2797 (add-hook 'js-mode-hook 'mdw-misc-mode-config t)
2798 (add-hook 'js-mode-hook 'mdw-fontify-javascript t))
2799
61d63206 2800;;;--------------------------------------------------------------------------
ee7c3dea
MW
2801;;; Scala programming configuration.
2802
2803(defun mdw-fontify-scala ()
2804
7b5903d8
MW
2805 ;; Comment filling.
2806 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2807
ee7c3dea
MW
2808 ;; Define things to be fontified.
2809 (make-local-variable 'font-lock-keywords)
2810 (let ((scala-keywords
2811 (mdw-regexps "abstract" "case" "catch" "class" "def" "do" "else"
2812 "extends" "final" "finally" "for" "forSome" "if"
2813 "implicit" "import" "lazy" "match" "new" "object"
3f017188
MW
2814 "override" "package" "private" "protected" "return"
2815 "sealed" "throw" "trait" "try" "type" "val"
ee7c3dea
MW
2816 "var" "while" "with" "yield"))
2817 (scala-constants
3f017188 2818 (mdw-regexps "false" "null" "super" "this" "true"))
7b5903d8 2819 (punctuation "[-!%^&*=+:@#~/?\\|`]"))
ee7c3dea
MW
2820
2821 (setq font-lock-keywords
535c927f 2822 (list
ee7c3dea 2823
535c927f
MW
2824 ;; Magical identifiers between backticks.
2825 (list (concat "`\\([^`]+\\)`")
2826 '(1 font-lock-variable-name-face))
ee7c3dea 2827
535c927f
MW
2828 ;; Handle the keywords defined above.
2829 (list (concat "\\_<\\(" scala-keywords "\\)\\_>")
2830 '(0 font-lock-keyword-face))
ee7c3dea 2831
535c927f
MW
2832 ;; Handle the constants defined above.
2833 (list (concat "\\_<\\(" scala-constants "\\)\\_>")
2834 '(0 font-lock-variable-name-face))
ee7c3dea 2835
535c927f
MW
2836 ;; Magical identifiers between backticks.
2837 (list (concat "`\\([^`]+\\)`")
2838 '(1 font-lock-variable-name-face))
2839
2840 ;; Handle numbers too.
2841 ;;
2842 ;; As usual, not quite right.
2843 (list (concat "\\_<\\("
2844 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2845 "[0-9]+\\(\\.[0-9]*\\)?"
2846 "\\([eE][-+]?[0-9]+\\)?\\)"
2847 "[lLfFdD]?")
2848 '(0 mdw-number-face))
2849
2850 ;; And everything else is punctuation.
2851 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2852 '(0 mdw-punct-face)))
ee7c3dea
MW
2853
2854 font-lock-syntactic-keywords
535c927f 2855 (list
ee7c3dea 2856
535c927f
MW
2857 ;; Single quotes around characters. But not when used to quote
2858 ;; symbol names. Ugh.
2859 (list (concat "\\('\\)"
2860 "\\(" "."
2861 "\\|" "\\\\" "\\(" "\\\\\\\\" "\\)*"
2862 "u+" "[0-9a-fA-F]\\{4\\}"
2863 "\\|" "\\\\" "[0-7]\\{1,3\\}"
2864 "\\|" "\\\\" "." "\\)"
2865 "\\('\\)")
2866 '(1 "\"")
2867 '(4 "\""))))))
ee7c3dea 2868
b50c6712
MW
2869(progn
2870 (add-hook 'scala-mode-hook 'mdw-misc-mode-config t)
2871 (add-hook 'scala-mode-hook 'mdw-fontify-scala t))
2872
ee7c3dea 2873;;;--------------------------------------------------------------------------
6132bc01 2874;;; C# programming configuration.
e808c1e5 2875
6132bc01 2876;; Make indentation nice.
e808c1e5 2877
a5807b1e 2878(mdw-define-c-style mdw-csharp ()
c56296d0
MW
2879 (c-basic-offset . 2)
2880 (c-backslash-column . 72)
2881 (c-offsets-alist (substatement-open . 0)
2882 (label . 0)
2883 (case-label . +)
2884 (access-label . 0)
2885 (inclass . +)
2886 (statement-case-intro . +)))
2887(mdw-set-default-c-style 'csharp-mode 'mdw-csharp)
e808c1e5 2888
6132bc01 2889;; Declare C# fontification style.
e808c1e5
MW
2890
2891(defun mdw-fontify-csharp ()
2892
6132bc01 2893 ;; Other stuff.
0e7d960b 2894 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
e808c1e5 2895
6132bc01 2896 ;; Now define things to be fontified.
e808c1e5
MW
2897 (make-local-variable 'font-lock-keywords)
2898 (let ((csharp-keywords
165cecf8
MW
2899 (mdw-regexps "abstract" "as" "bool" "break" "byte" "case" "catch"
2900 "char" "checked" "class" "const" "continue" "decimal"
2901 "default" "delegate" "do" "double" "else" "enum"
2902 "event" "explicit" "extern" "finally" "fixed" "float"
2903 "for" "foreach" "goto" "if" "implicit" "in" "int"
2904 "interface" "internal" "is" "lock" "long" "namespace"
2905 "new" "object" "operator" "out" "override" "params"
2906 "private" "protected" "public" "readonly" "ref"
2907 "return" "sbyte" "sealed" "short" "sizeof"
2908 "stackalloc" "static" "string" "struct" "switch"
2909 "throw" "try" "typeof" "uint" "ulong" "unchecked"
2910 "unsafe" "ushort" "using" "virtual" "void" "volatile"
2911 "while" "yield"))
2912
300f8827 2913 (csharp-builtins
165cecf8 2914 (mdw-regexps "base" "false" "null" "this" "true")))
e808c1e5
MW
2915
2916 (setq font-lock-keywords
535c927f 2917 (list
e808c1e5 2918
535c927f
MW
2919 ;; Handle the keywords defined above.
2920 (list (concat "\\<\\(" csharp-keywords "\\)\\>")
2921 '(0 font-lock-keyword-face))
e808c1e5 2922
535c927f
MW
2923 ;; Handle the magic builtins defined above.
2924 (list (concat "\\<\\(" csharp-builtins "\\)\\>")
2925 '(0 font-lock-variable-name-face))
e808c1e5 2926
535c927f
MW
2927 ;; Handle numbers too.
2928 ;;
2929 ;; The following isn't quite right, but it's close enough.
2930 (list (concat "\\<\\("
2931 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2932 "[0-9]+\\(\\.[0-9]*\\)?"
2933 "\\([eE][-+]?[0-9]+\\)?\\)"
2934 "[lLfFdD]?")
2935 '(0 mdw-number-face))
2936
2937 ;; And anything else is punctuation.
2938 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2939 '(0 mdw-punct-face))))))
e808c1e5 2940
103c5923
MW
2941(define-derived-mode csharp-mode java-mode "C#"
2942 "Major mode for editing C# code.")
e808c1e5 2943
b50c6712
MW
2944(add-hook 'csharp-mode-hook 'mdw-fontify-csharp t)
2945
6132bc01 2946;;;--------------------------------------------------------------------------
81fb08fc
MW
2947;;; F# programming configuration.
2948
2949(setq fsharp-indent-offset 2)
2950
2951(defun mdw-fontify-fsharp ()
2952
2953 (let ((punct "=<>+-*/|&%!@?"))
2954 (do ((i 0 (1+ i)))
2955 ((>= i (length punct)))
2956 (modify-syntax-entry (aref punct i) ".")))
2957
2958 (modify-syntax-entry ?_ "_")
2959 (modify-syntax-entry ?( "(")
2960 (modify-syntax-entry ?) ")")
2961
2962 (setq indent-tabs-mode nil)
2963
2964 (let ((fsharp-keywords
2965 (mdw-regexps "abstract" "and" "as" "assert" "atomic"
165cecf8 2966 "begin" "break"
81fb08fc
MW
2967 "checked" "class" "component" "const" "constraint"
2968 "constructor" "continue"
2969 "default" "delegate" "do" "done" "downcast" "downto"
2970 "eager" "elif" "else" "end" "exception" "extern"
165cecf8 2971 "finally" "fixed" "for" "fori" "fun" "function"
81fb08fc
MW
2972 "functor"
2973 "global"
2974 "if" "in" "include" "inherit" "inline" "interface"
2975 "internal"
2976 "lazy" "let"
2977 "match" "measure" "member" "method" "mixin" "module"
2978 "mutable"
165cecf8
MW
2979 "namespace" "new"
2980 "object" "of" "open" "or" "override"
81fb08fc
MW
2981 "parallel" "params" "private" "process" "protected"
2982 "public" "pure"
2983 "rec" "recursive" "return"
2984 "sealed" "sig" "static" "struct"
165cecf8 2985 "tailcall" "then" "to" "trait" "try" "type"
81fb08fc
MW
2986 "upcast" "use"
2987 "val" "virtual" "void" "volatile"
2988 "when" "while" "with"
2989 "yield"))
2990
2991 (fsharp-builtins
165cecf8
MW
2992 (mdw-regexps "asr" "land" "lor" "lsl" "lsr" "lxor" "mod"
2993 "base" "false" "null" "true"))
81fb08fc
MW
2994
2995 (bang-keywords
2996 (mdw-regexps "do" "let" "return" "use" "yield"))
2997
2998 (preprocessor-keywords
2999 (mdw-regexps "if" "indent" "else" "endif")))
3000
3001 (setq font-lock-keywords
535c927f
MW
3002 (list (list (concat "\\(^\\|[^\"]\\)"
3003 "\\(" "(\\*"
3004 "[^*]*\\*+"
3005 "\\(" "[^)*]" "[^*]*" "\\*+" "\\)*"
3006 ")"
81fb08fc 3007 "\\|"
535c927f
MW
3008 "//.*"
3009 "\\)")
3010 '(2 font-lock-comment-face))
3011
3012 (list (concat "'" "\\("
3013 "\\\\"
3014 "\\(" "[ntbr'\\]"
3015 "\\|" "[0-9][0-9][0-9]"
3016 "\\|" "u" "[0-9a-fA-F]\\{4\\}"
3017 "\\|" "U" "[0-9a-fA-F]\\{8\\}"
3018 "\\)"
3019 "\\|"
3020 "." "\\)" "'"
81fb08fc 3021 "\\|"
535c927f
MW
3022 "\"" "[^\"\\]*"
3023 "\\(" "\\\\" "\\(.\\|\n\\)"
3024 "[^\"\\]*" "\\)*"
3025 "\\(\"\\|\\'\\)")
3026 '(0 font-lock-string-face))
3027
3028 (list (concat "\\_<\\(" bang-keywords "\\)!" "\\|"
3029 "^#[ \t]*\\(" preprocessor-keywords "\\)\\_>"
3030 "\\|"
3031 "\\_<\\(" fsharp-keywords "\\)\\_>")
3032 '(0 font-lock-keyword-face))
3033 (list (concat "\\<\\(" fsharp-builtins "\\)\\_>")
3034 '(0 font-lock-variable-name-face))
3035
3036 (list (concat "\\_<"
3037 "\\(" "0[bB][01]+" "\\|"
3038 "0[oO][0-7]+" "\\|"
3039 "0[xX][0-9a-fA-F]+" "\\)"
3040 "\\(" "lf\\|LF" "\\|"
3041 "[uU]?[ysnlL]?" "\\)"
3042 "\\|"
3043 "\\_<"
3044 "[0-9]+" "\\("
3045 "[mMQRZING]"
3046 "\\|"
3047 "\\(\\.[0-9]*\\)?"
3048 "\\([eE][-+]?[0-9]+\\)?"
3049 "[fFmM]?"
3050 "\\|"
3051 "[uU]?[ysnlL]?"
3052 "\\)")
3053 '(0 mdw-number-face))
81fb08fc 3054
535c927f
MW
3055 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3056 '(0 mdw-punct-face))))))
81fb08fc
MW
3057
3058(defun mdw-fontify-inferior-fsharp ()
3059 (mdw-fontify-fsharp)
3060 (setq font-lock-keywords
535c927f
MW
3061 (append (list (list "^[#-]" '(0 font-lock-comment-face))
3062 (list "^>" '(0 font-lock-keyword-face)))
3063 font-lock-keywords)))
81fb08fc 3064
b50c6712
MW
3065(progn
3066 (add-hook 'fsharp-mode-hook 'mdw-misc-mode-config t)
3067 (add-hook 'fsharp-mode-hook 'mdw-fontify-fsharp t)
3068 (add-hook 'inferior-fsharp-mode-hooks 'mdw-fontify-inferior-fsharp t))
3069
81fb08fc 3070;;;--------------------------------------------------------------------------
07965a39
MW
3071;;; Go programming configuration.
3072
3073(defun mdw-fontify-go ()
3074
3075 (make-local-variable 'font-lock-keywords)
3076 (let ((go-keywords
3077 (mdw-regexps "break" "case" "chan" "const" "continue"
3078 "default" "defer" "else" "fallthrough" "for"
3079 "func" "go" "goto" "if" "import"
3080 "interface" "map" "package" "range" "return"
fc79ff88
MW
3081 "select" "struct" "switch" "type" "var"))
3082 (go-intrinsics
3083 (mdw-regexps "bool" "byte" "complex64" "complex128" "error"
3084 "float32" "float64" "int" "uint8" "int16" "int32"
3085 "int64" "rune" "string" "uint" "uint8" "uint16"
3086 "uint32" "uint64" "uintptr" "void"
3087 "false" "iota" "nil" "true"
3088 "init" "main"
3089 "append" "cap" "copy" "delete" "imag" "len" "make"
3090 "new" "panic" "real" "recover")))
07965a39
MW
3091
3092 (setq font-lock-keywords
535c927f 3093 (list
07965a39 3094
535c927f
MW
3095 ;; Handle the keywords defined above.
3096 (list (concat "\\<\\(" go-keywords "\\)\\>")
3097 '(0 font-lock-keyword-face))
3098 (list (concat "\\<\\(" go-intrinsics "\\)\\>")
3099 '(0 font-lock-variable-name-face))
3100
3101 ;; Strings and characters.
3102 (list (concat "'"
3103 "\\(" "[^\\']" "\\|"
3104 "\\\\"
3105 "\\(" "[abfnrtv\\'\"]" "\\|"
3106 "[0-7]\\{3\\}" "\\|"
3107 "x" "[0-9A-Fa-f]\\{2\\}" "\\|"
3108 "u" "[0-9A-Fa-f]\\{4\\}" "\\|"
3109 "U" "[0-9A-Fa-f]\\{8\\}" "\\)" "\\)"
3110 "'"
3111 "\\|"
3112 "\""
3113 "\\(" "[^\n\\\"]+" "\\|" "\\\\." "\\)*"
3114 "\\(\"\\|$\\)"
3115 "\\|"
3116 "`" "[^`]+" "`")
3117 '(0 font-lock-string-face))
3118
3119 ;; Handle numbers too.
3120 ;;
3121 ;; The following isn't quite right, but it's close enough.
3122 (list (concat "\\<\\("
3123 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
3124 "[0-9]+\\(\\.[0-9]*\\)?"
3125 "\\([eE][-+]?[0-9]+\\)?\\)")
3126 '(0 mdw-number-face))
3127
3128 ;; And anything else is punctuation.
3129 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3130 '(0 mdw-punct-face))))))
b50c6712
MW
3131(progn
3132 (add-hook 'go-mode-hook 'mdw-misc-mode-config t)
3133 (add-hook 'go-mode-hook 'mdw-fontify-go t))
07965a39
MW
3134
3135;;;--------------------------------------------------------------------------
36db1ea7
MW
3136;;; Rust programming configuration.
3137
3138(setq-default rust-indent-offset 2)
3139
3140(defun mdw-self-insert-and-indent (count)
3141 (interactive "p")
3142 (self-insert-command count)
3143 (indent-according-to-mode))
3144
3145(defun mdw-fontify-rust ()
3146
3147 ;; Hack syntax categories.
cbd69b16 3148 (modify-syntax-entry ?$ ".")
8e234929 3149 (modify-syntax-entry ?% ".")
36db1ea7
MW
3150 (modify-syntax-entry ?= ".")
3151
3152 ;; Fontify keywords and things.
3153 (make-local-variable 'font-lock-keywords)
3154 (let ((rust-keywords
87def30c 3155 (mdw-regexps "abstract" "alignof" "as" "async" "await"
36db1ea7 3156 "become" "box" "break"
260564a3 3157 "const" "continue" "crate"
87def30c 3158 "do" "dyn"
36db1ea7 3159 "else" "enum" "extern"
b6f44b18 3160 "final" "fn" "for"
36db1ea7
MW
3161 "if" "impl" "in"
3162 "let" "loop"
3163 "macro" "match" "mod" "move" "mut"
3164 "offsetof" "override"
b6f44b18 3165 "priv" "proc" "pub" "pure"
36db1ea7 3166 "ref" "return"
b6f44b18 3167 "sizeof" "static" "struct" "super"
87def30c
MW
3168 "trait" "try" "type" "typeof"
3169 "union" "unsafe" "unsized" "use"
36db1ea7
MW
3170 "virtual"
3171 "where" "while"
3172 "yield"))
3173 (rust-builtins
3174 (mdw-regexps "array" "pointer" "slice" "tuple"
3175 "bool" "true" "false"
3176 "f32" "f64"
3177 "i8" "i16" "i32" "i64" "isize"
3178 "u8" "u16" "u32" "u64" "usize"
b6f44b18
MW
3179 "char" "str"
3180 "self" "Self")))
36db1ea7 3181 (setq font-lock-keywords
535c927f 3182 (list
36db1ea7 3183
535c927f
MW
3184 ;; Handle the keywords defined above.
3185 (list (concat "\\_<\\(" rust-keywords "\\)\\_>")
3186 '(0 font-lock-keyword-face))
3187 (list (concat "\\_<\\(" rust-builtins "\\)\\_>")
3188 '(0 font-lock-variable-name-face))
3189
3190 ;; Handle numbers too.
3191 (list (concat "\\_<\\("
3192 "[0-9][0-9_]*"
3193 "\\(" "\\(\\.[0-9_]+\\)?[eE][-+]?[0-9_]+"
3194 "\\|" "\\.[0-9_]+"
3195 "\\)"
3196 "\\(f32\\|f64\\)?"
3197 "\\|" "\\(" "[0-9][0-9_]*"
3198 "\\|" "0x[0-9a-fA-F_]+"
3199 "\\|" "0o[0-7_]+"
3200 "\\|" "0b[01_]+"
3201 "\\)"
3202 "\\([ui]\\(8\\|16\\|32\\|64\\|size\\)\\)?"
3203 "\\)\\_>")
3204 '(0 mdw-number-face))
3205
3206 ;; And anything else is punctuation.
3207 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
77ccc2aa
MW
3208 '(0 mdw-punct-face)))
3209 font-lock-syntactic-face-function . nil))
36db1ea7
MW
3210
3211 ;; Hack key bindings.
d2f85967 3212 (local-set-key [?{] 'mdw-self-insert-and-indent)
2e7c6a86 3213 (local-set-key [?}] 'mdw-self-insert-and-indent))
36db1ea7 3214
b50c6712
MW
3215(progn
3216 (add-hook 'rust-mode-hook 'mdw-misc-mode-config t)
3217 (add-hook 'rust-mode-hook 'mdw-fontify-rust t))
3218
36db1ea7 3219;;;--------------------------------------------------------------------------
6132bc01 3220;;; Awk programming configuration.
f617db13 3221
6132bc01 3222;; Make Awk indentation nice.
f617db13 3223
e0de0009 3224(mdw-define-c-style mdw-awk ()
c56296d0
MW
3225 (c-basic-offset . 2)
3226 (c-offsets-alist (substatement-open . 0)
3227 (c-backslash-column . 72)
3228 (statement-cont . 0)
3229 (statement-case-intro . +)))
3230(mdw-set-default-c-style 'awk-mode 'mdw-awk)
f617db13 3231
6132bc01 3232;; Declare Awk fontification style.
f617db13
MW
3233
3234(defun mdw-fontify-awk ()
3235
6132bc01 3236 ;; Miscellaneous fiddling.
f617db13
MW
3237 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3238
6132bc01 3239 ;; Now define things to be fontified.
02109a0d 3240 (make-local-variable 'font-lock-keywords)
f617db13 3241 (let ((c-keywords
8d6d55b9
MW
3242 (mdw-regexps "BEGIN" "END" "ARGC" "ARGIND" "ARGV" "CONVFMT"
3243 "ENVIRON" "ERRNO" "FIELDWIDTHS" "FILENAME" "FNR"
3244 "FS" "IGNORECASE" "NF" "NR" "OFMT" "OFS" "ORS" "RS"
3245 "RSTART" "RLENGTH" "RT" "SUBSEP"
3246 "atan2" "break" "close" "continue" "cos" "delete"
3247 "do" "else" "exit" "exp" "fflush" "file" "for" "func"
3248 "function" "gensub" "getline" "gsub" "if" "in"
3249 "index" "int" "length" "log" "match" "next" "rand"
3250 "return" "print" "printf" "sin" "split" "sprintf"
3251 "sqrt" "srand" "strftime" "sub" "substr" "system"
3252 "systime" "tolower" "toupper" "while")))
f617db13
MW
3253
3254 (setq font-lock-keywords
535c927f 3255 (list
f617db13 3256
535c927f
MW
3257 ;; Handle the keywords defined above.
3258 (list (concat "\\<\\(" c-keywords "\\)\\>")
3259 '(0 font-lock-keyword-face))
f617db13 3260
535c927f
MW
3261 ;; Handle numbers too.
3262 ;;
3263 ;; The following isn't quite right, but it's close enough.
3264 (list (concat "\\<\\("
3265 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
3266 "[0-9]+\\(\\.[0-9]*\\)?"
3267 "\\([eE][-+]?[0-9]+\\)?\\)"
3268 "[uUlL]*")
3269 '(0 mdw-number-face))
f617db13 3270
535c927f
MW
3271 ;; And anything else is punctuation.
3272 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3273 '(0 mdw-punct-face))))))
f617db13 3274
b50c6712
MW
3275(progn
3276 (add-hook 'awk-mode-hook 'mdw-misc-mode-config t)
3277 (add-hook 'awk-mode-hook 'mdw-fontify-awk t))
3278
6132bc01
MW
3279;;;--------------------------------------------------------------------------
3280;;; Perl programming style.
f617db13 3281
6132bc01 3282;; Perl indentation style.
f617db13 3283
08b1b191 3284(setq-default perl-indent-level 2)
88158daf 3285
08b1b191
MW
3286(setq-default cperl-indent-level 2
3287 cperl-continued-statement-offset 2
d7fc7501 3288 cperl-indent-region-fix-constructs nil
08b1b191
MW
3289 cperl-continued-brace-offset 0
3290 cperl-brace-offset -2
3291 cperl-brace-imaginary-offset 0
3292 cperl-label-offset 0)
f617db13 3293
6132bc01 3294;; Define perl fontification style.
f617db13
MW
3295
3296(defun mdw-fontify-perl ()
3297
6132bc01 3298 ;; Miscellaneous fiddling.
f617db13
MW
3299 (modify-syntax-entry ?$ "\\")
3300 (modify-syntax-entry ?$ "\\" font-lock-syntax-table)
a3b8176f 3301 (modify-syntax-entry ?: "." font-lock-syntax-table)
f617db13
MW
3302 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3303
6132bc01 3304 ;; Now define fontification things.
02109a0d 3305 (make-local-variable 'font-lock-keywords)
f617db13 3306 (let ((perl-keywords
821c4945
MW
3307 (mdw-regexps "and"
3308 "break"
3309 "cmp" "continue"
3310 "default" "do"
3311 "else" "elsif" "eq"
3312 "for" "foreach"
3313 "ge" "given" "gt" "goto"
3314 "if"
3315 "last" "le" "local" "lt"
3316 "my"
3317 "ne" "next"
3318 "or" "our"
3319 "package"
3320 "redo" "require" "return"
3321 "sub"
3322 "undef" "unless" "until" "use"
3323 "when" "while")))
f617db13
MW
3324
3325 (setq font-lock-keywords
535c927f 3326 (list
f617db13 3327
535c927f
MW
3328 ;; Set up the keywords defined above.
3329 (list (concat "\\<\\(" perl-keywords "\\)\\>")
3330 '(0 font-lock-keyword-face))
f617db13 3331
535c927f
MW
3332 ;; At least numbers are simpler than C.
3333 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
3334 "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\)?"
3335 "\\([eE][-+]?[0-9_]+\\)?")
3336 '(0 mdw-number-face))
f617db13 3337
535c927f
MW
3338 ;; And anything else is punctuation.
3339 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3340 '(0 mdw-punct-face))))))
f617db13
MW
3341
3342(defun perl-number-tests (&optional arg)
3343 "Assign consecutive numbers to lines containing `#t'. With ARG,
3344strip numbers instead."
3345 (interactive "P")
3346 (save-excursion
3347 (goto-char (point-min))
3348 (let ((i 0) (fmt (if arg "" " %4d")))
3349 (while (search-forward "#t" nil t)
3350 (delete-region (point) (line-end-position))
3351 (setq i (1+ i))
3352 (insert (format fmt i)))
3353 (goto-char (point-min))
3354 (if (re-search-forward "\\(tests\\s-*=>\\s-*\\)\\w*" nil t)
3355 (replace-match (format "\\1%d" i))))))
3356
b50c6712
MW
3357(dolist (hook '(perl-mode-hook cperl-mode-hook))
3358 (add-hook hook 'mdw-misc-mode-config t)
3359 (add-hook hook 'mdw-fontify-perl t))
3360
6132bc01
MW
3361;;;--------------------------------------------------------------------------
3362;;; Python programming style.
f617db13 3363
b50c6712
MW
3364(setq-default py-indent-offset 2
3365 python-indent 2
3366 python-indent-offset 2
3367 python-fill-docstring-style 'symmetric)
3368
99fe6ef5 3369(defun mdw-fontify-pythonic (keywords)
f617db13 3370
6132bc01 3371 ;; Miscellaneous fiddling.
f617db13 3372 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
7c0fcfde 3373 (setq indent-tabs-mode nil)
853c5c7c 3374 (set (make-local-variable 'forward-sexp-function) nil)
f617db13 3375
6132bc01 3376 ;; Now define fontification things.
02109a0d 3377 (make-local-variable 'font-lock-keywords)
99fe6ef5 3378 (setq font-lock-keywords
535c927f 3379 (list
f617db13 3380
535c927f
MW
3381 ;; Set up the keywords defined above.
3382 (list (concat "\\_<\\(" keywords "\\)\\_>")
3383 '(0 font-lock-keyword-face))
f617db13 3384
535c927f
MW
3385 ;; At least numbers are simpler than C.
3386 (list (concat "\\_<0\\([xX][0-9a-fA-F]+\\|[oO]?[0-7]+\\|[bB][01]+\\)\\|"
3387 "\\_<[0-9][0-9]*\\(\\.[0-9]*\\)?"
3388 "\\([eE][-+]?[0-9]+\\|[lL]\\)?")
3389 '(0 mdw-number-face))
f617db13 3390
535c927f
MW
3391 ;; And anything else is punctuation.
3392 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3393 '(0 mdw-punct-face)))))
99fe6ef5 3394
be2cc788 3395;; Define Python fontification styles.
99fe6ef5
MW
3396
3397(defun mdw-fontify-python ()
3398 (mdw-fontify-pythonic
3399 (mdw-regexps "and" "as" "assert" "break" "class" "continue" "def"
3400 "del" "elif" "else" "except" "exec" "finally" "for"
3401 "from" "global" "if" "import" "in" "is" "lambda"
3402 "not" "or" "pass" "print" "raise" "return" "try"
3403 "while" "with" "yield")))
3404
3405(defun mdw-fontify-pyrex ()
3406 (mdw-fontify-pythonic
3407 (mdw-regexps "and" "as" "assert" "break" "cdef" "class" "continue"
a63efb67 3408 "ctypedef" "def" "del" "elif" "else" "enum" "except" "exec"
99fe6ef5
MW
3409 "extern" "finally" "for" "from" "global" "if"
3410 "import" "in" "is" "lambda" "not" "or" "pass" "print"
a63efb67 3411 "property" "raise" "return" "struct" "try" "while" "with"
99fe6ef5 3412 "yield")))
f617db13 3413
b5263ae5
MW
3414(define-derived-mode pyrex-mode python-mode "Pyrex"
3415 "Major mode for editing Pyrex source code")
3416(setq auto-mode-alist
535c927f
MW
3417 (append '(("\\.pyx$" . pyrex-mode)
3418 ("\\.pxd$" . pyrex-mode)
3419 ("\\.pxi$" . pyrex-mode))
3420 auto-mode-alist))
b5263ae5 3421
b50c6712
MW
3422(progn
3423 (add-hook 'python-mode-hook 'mdw-misc-mode-config t)
3424 (add-hook 'python-mode-hook 'mdw-fontify-python t)
3425 (add-hook 'pyrex-mode-hook 'mdw-fontify-pyrex t))
3426
6132bc01 3427;;;--------------------------------------------------------------------------
772a7a3b
MW
3428;;; Lua programming style.
3429
08b1b191 3430(setq-default lua-indent-level 2)
772a7a3b
MW
3431
3432(defun mdw-fontify-lua ()
3433
3434 ;; Miscellaneous fiddling.
3435 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3436
3437 ;; Now define fontification things.
3438 (make-local-variable 'font-lock-keywords)
3439 (let ((lua-keywords
3440 (mdw-regexps "and" "break" "do" "else" "elseif" "end"
3441 "false" "for" "function" "goto" "if" "in" "local"
3442 "nil" "not" "or" "repeat" "return" "then" "true"
3443 "until" "while")))
3444 (setq font-lock-keywords
535c927f 3445 (list
772a7a3b 3446
535c927f
MW
3447 ;; Set up the keywords defined above.
3448 (list (concat "\\_<\\(" lua-keywords "\\)\\_>")
3449 '(0 font-lock-keyword-face))
3450
3451 ;; At least numbers are simpler than C.
3452 (list (concat "\\_<\\(" "0[xX]"
3453 "\\(" "[0-9a-fA-F]+"
3454 "\\(\\.[0-9a-fA-F]*\\)?"
3455 "\\|" "\\.[0-9a-fA-F]+"
3456 "\\)"
3457 "\\([pP][-+]?[0-9]+\\)?"
3458 "\\|" "\\(" "[0-9]+"
3459 "\\(\\.[0-9]*\\)?"
3460 "\\|" "\\.[0-9]+"
3461 "\\)"
3462 "\\([eE][-+]?[0-9]+\\)?"
3463 "\\)")
3464 '(0 mdw-number-face))
3465
3466 ;; And anything else is punctuation.
3467 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3468 '(0 mdw-punct-face))))))
772a7a3b 3469
b50c6712
MW
3470(progn
3471 (add-hook 'lua-mode-hook 'mdw-misc-mode-config t)
3472 (add-hook 'lua-mode-hook 'mdw-fontify-lua t))
3473
772a7a3b 3474;;;--------------------------------------------------------------------------
6132bc01 3475;;; Icon programming style.
cc1980e1 3476
6132bc01 3477;; Icon indentation style.
cc1980e1 3478
08b1b191
MW
3479(setq-default icon-brace-offset 0
3480 icon-continued-brace-offset 0
3481 icon-continued-statement-offset 2
3482 icon-indent-level 2)
cc1980e1 3483
6132bc01 3484;; Define Icon fontification style.
cc1980e1
MW
3485
3486(defun mdw-fontify-icon ()
3487
6132bc01 3488 ;; Miscellaneous fiddling.
cc1980e1
MW
3489 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3490
6132bc01 3491 ;; Now define fontification things.
cc1980e1
MW
3492 (make-local-variable 'font-lock-keywords)
3493 (let ((icon-keywords
3494 (mdw-regexps "break" "by" "case" "create" "default" "do" "else"
3495 "end" "every" "fail" "global" "if" "initial"
3496 "invocable" "link" "local" "next" "not" "of"
3497 "procedure" "record" "repeat" "return" "static"
3498 "suspend" "then" "to" "until" "while"))
3499 (preprocessor-keywords
3500 (mdw-regexps "define" "else" "endif" "error" "ifdef" "ifndef"
3501 "include" "line" "undef")))
3502 (setq font-lock-keywords
535c927f 3503 (list
cc1980e1 3504
535c927f
MW
3505 ;; Set up the keywords defined above.
3506 (list (concat "\\<\\(" icon-keywords "\\)\\>")
3507 '(0 font-lock-keyword-face))
cc1980e1 3508
535c927f
MW
3509 ;; The things that Icon calls keywords.
3510 (list "&\\sw+\\>" '(0 font-lock-variable-name-face))
cc1980e1 3511
535c927f
MW
3512 ;; At least numbers are simpler than C.
3513 (list (concat "\\<[0-9]+"
3514 "\\([rR][0-9a-zA-Z]+\\|"
3515 "\\.[0-9]+\\([eE][+-]?[0-9]+\\)?\\)\\>\\|"
3516 "\\.[0-9]+\\([eE][+-]?[0-9]+\\)?\\>")
3517 '(0 mdw-number-face))
cc1980e1 3518
535c927f
MW
3519 ;; Preprocessor.
3520 (list (concat "^[ \t]*$[ \t]*\\<\\("
3521 preprocessor-keywords
3522 "\\)\\>")
3523 '(0 font-lock-keyword-face))
cc1980e1 3524
535c927f
MW
3525 ;; And anything else is punctuation.
3526 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3527 '(0 mdw-punct-face))))))
cc1980e1 3528
b50c6712
MW
3529(progn
3530 (add-hook 'icon-mode-hook 'mdw-misc-mode-config t)
3531 (add-hook 'icon-mode-hook 'mdw-fontify-icon t))
3532
6132bc01 3533;;;--------------------------------------------------------------------------
8d00d2f4
MW
3534;;; Fortran mode.
3535
3536(defun mdw-fontify-fortran-common ()
3537 (let ((fortran-keywords
3538 (mdw-regexps "access"
3539 "assign"
3540 "associate"
3541 "backspace"
3542 "blank"
3543 "block\\s-*data"
3544 "call"
3545 "case"
3546 "character"
3547 "class"
3548 "close"
3549 "common"
3550 "complex"
3551 "continue"
3552 "critical"
3553 "data"
3554 "dimension"
3555 "do"
3556 "double\\s-*precision"
3557 "else" "elseif" "elsewhere"
3558 "end"
3559 "endblock" "endblockdata"
3560 "endcritical"
3561 "enddo"
3562 "endinterface"
3563 "endmodule"
3564 "endprocedure"
3565 "endprogram"
3566 "endselect"
3567 "endsubmodule"
3568 "endsubroutine"
3569 "endtype"
3570 "endwhere"
3571 "endenum"
3572 "end\\s-*file"
3573 "endforall"
3574 "endfunction"
3575 "endif"
3576 "entry"
3577 "enum"
3578 "equivalence"
3579 "err"
3580 "external"
3581 "file"
3582 "fmt"
3583 "forall"
3584 "form"
3585 "format"
3586 "function"
3587 "go\\s-*to"
3588 "if"
3589 "implicit"
3590 "in" "inout"
3591 "inquire"
3592 "include"
3593 "integer"
3594 "interface"
3595 "intrinsic"
3596 "iostat"
3597 "len"
3598 "logical"
3599 "module"
3600 "open"
3601 "out"
3602 "parameter"
3603 "pause"
3604 "procedure"
3605 "program"
3606 "precision"
3607 "program"
3608 "read"
3609 "real"
3610 "rec"
3611 "recl"
3612 "return"
3613 "rewind"
3614 "save"
3615 "select" "selectcase" "selecttype"
3616 "status"
3617 "stop"
3618 "submodule"
3619 "subroutine"
3620 "then"
3621 "to"
3622 "type"
3623 "unit"
3624 "where"
3625 "write"))
3626 (fortran-operators (mdw-regexps "and"
3627 "eq"
3628 "eqv"
3629 "false"
3630 "ge"
3631 "gt"
3632 "le"
3633 "lt"
3634 "ne"
3635 "neqv"
3636 "not"
3637 "or"
3638 "true"))
3639 (fortran-intrinsics (mdw-regexps "abs" "dabs" "iabs" "cabs"
3640 "atan" "datan" "atan2" "datan2"
3641 "cmplx"
3642 "conjg"
3643 "cos" "dcos" "ccos"
3644 "dble"
3645 "dim" "idim"
3646 "exp" "dexp" "cexp"
3647 "float"
3648 "ifix"
3649 "aimag"
3650 "int" "aint" "idint"
3651 "alog" "dlog" "clog"
3652 "alog10" "dlog10"
3653 "max"
3654 "amax0" "amax1"
3655 "max0" "max1"
3656 "dmax1"
3657 "min"
3658 "amin0" "amin1"
3659 "min0" "min1"
3660 "dmin1"
3661 "mod" "amod" "dmod"
3662 "sin" "dsin" "csin"
3663 "sign" "isign" "dsign"
3664 "sngl"
3665 "sqrt" "dsqrt" "csqrt"
3666 "tanh"))
3667 (preprocessor-keywords
3668 (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
3669 "ident" "if" "ifdef" "ifndef" "import" "include"
3670 "line" "pragma" "unassert" "undef" "warning")))
3671 (setq font-lock-keywords-case-fold-search t
3672 font-lock-keywords
3673 (list
3674
3675 ;; Fontify include files as strings.
3676 (list (concat "^[ \t]*\\#[ \t]*" "include"
3677 "[ \t]*\\(<[^>]+>?\\)")
3678 '(1 font-lock-string-face))
3679
3680 ;; Preprocessor directives are `references'?.
3681 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
3682 preprocessor-keywords
3683 "\\)\\>\\|[0-9]+\\|$\\)\\)")
3684 '(1 font-lock-keyword-face))
3685
3686 ;; Set up the keywords defined above.
3687 (list (concat "\\<\\(" fortran-keywords "\\)\\>")
3688 '(0 font-lock-keyword-face))
3689
3690 ;; Set up the `.foo.' operators.
3691 (list (concat "\\.\\(" fortran-operators "\\)\\.")
3692 '(0 font-lock-keyword-face))
3693
3694 ;; Set up the intrinsic functions.
3695 (list (concat "\\<\\(" fortran-intrinsics "\\)\\>")
3696 '(0 font-lock-variable-name-face))
3697
3698 ;; Numbers.
3699 (list (concat "\\(" "\\<" "[0-9]+" "\\(\\.[0-9]*\\)?"
3700 "\\|" "\\.[0-9]+"
3701 "\\)"
3702 "\\(" "[de]" "[+-]?" "[0-9]+" "\\)?"
3703 "\\(" "_" "\\sw+" "\\)?"
3704 "\\|" "b'[01]*'" "\\|" "'[01]*'b"
3705 "\\|" "b\"[01]*\"" "\\|" "\"[01]*\"b"
3706 "\\|" "o'[0-7]*'" "\\|" "'[0-7]*'o"
3707 "\\|" "o\"[0-7]*\"" "\\|" "\"[0-7]*\"o"
3708 "\\|" "[xz]'[0-9a-f]*'" "\\|" "'[0-9a-f]*'[xz]"
3709 "\\|" "[xz]\"[0-9a-f]*\"" "\\|" "\"[0-9a-f]*\"[xz]")
3710 '(0 mdw-number-face))
3711
3712 ;; Any anything else is punctuation.
3713 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3714 '(0 mdw-punct-face))))
3715
3716 (modify-syntax-entry ?/ "." font-lock-syntax-table)
3717 (modify-syntax-entry ?< ".")
3718 (modify-syntax-entry ?> ".")))
3719
3720(defun mdw-fontify-fortran () (mdw-fontify-fortran-common))
3721(defun mdw-fontify-f90 () (mdw-fontify-fortran-common))
3722
3723(setq fortran-do-indent 2
3724 fortran-if-indent 2
3725 fortran-structure-indent 2
3726 fortran-comment-line-start "*"
3727 fortran-comment-indent-style 'relative
3728 fortran-continuation-string "&"
3729 fortran-continuation-indent 4)
3730
3731(setq f90-do-indent 2
3732 f90-if-indent 2
3733 f90-program-indent 2
3734 f90-continuation-indent 4
3735 f90-smart-end-names nil
3736 f90-smart-end 'no-blink)
3737
3738(progn
3739 (add-hook 'fortran-mode-hook 'mdw-misc-mode-config t)
3740 (add-hook 'fortran-mode-hook 'mdw-fontify-fortran t)
3741 (add-hook 'f90-mode-hook 'mdw-misc-mode-config t)
3742 (add-hook 'f90-mode-hook 'mdw-fontify-f90 t))
3743
3744;;;--------------------------------------------------------------------------
6132bc01 3745;;; Assembler mode.
30c8a8fb
MW
3746
3747(defun mdw-fontify-asm ()
3748 (modify-syntax-entry ?' "\"")
3749 (modify-syntax-entry ?. "w")
9032280b 3750 (modify-syntax-entry ?\n ">")
30c8a8fb 3751 (setf fill-prefix nil)
5edd6d49
MW
3752 (modify-syntax-entry ?. "_")
3753 (modify-syntax-entry ?* ". 23")
3754 (modify-syntax-entry ?/ ". 124b")
3755 (modify-syntax-entry ?\n "> b")
b90c2a2c 3756 (local-set-key ";" 'self-insert-command)
30c8a8fb
MW
3757 (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)"))
3758
227b2b2b
MW
3759(defun mdw-asm-set-comment ()
3760 (modify-syntax-entry ?; "."
3761 )
5edd6d49 3762 (modify-syntax-entry asm-comment-char "< b")
227b2b2b
MW
3763 (setq comment-start (string asm-comment-char ? )))
3764(add-hook 'asm-mode-local-variables-hook 'mdw-asm-set-comment)
3765(put 'asm-comment-char 'safe-local-variable 'characterp)
9032280b 3766
b50c6712
MW
3767(progn
3768 (add-hook 'asm-mode-hook 'mdw-misc-mode-config t)
3769 (add-hook 'asm-mode-hook 'mdw-fontify-asm t))
3770
6132bc01
MW
3771;;;--------------------------------------------------------------------------
3772;;; TCL configuration.
f617db13 3773
b50c6712
MW
3774(setq-default tcl-indent-level 2)
3775
f617db13 3776(defun mdw-fontify-tcl ()
6c4bd06b
MW
3777 (dolist (ch '(?$))
3778 (modify-syntax-entry ch "."))
f617db13 3779 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
02109a0d 3780 (make-local-variable 'font-lock-keywords)
f617db13 3781 (setq font-lock-keywords
535c927f
MW
3782 (list
3783 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
3784 "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\)?"
3785 "\\([eE][-+]?[0-9_]+\\)?")
3786 '(0 mdw-number-face))
3787 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3788 '(0 mdw-punct-face)))))
f617db13 3789
b50c6712
MW
3790(progn
3791 (add-hook 'tcl-mode-hook 'mdw-misc-mode-config t)
3792 (add-hook 'tcl-mode-hook 'mdw-fontify-tcl t))
3793
6132bc01 3794;;;--------------------------------------------------------------------------
ad305d7e
MW
3795;;; Dylan programming configuration.
3796
3797(defun mdw-fontify-dylan ()
3798
3799 (make-local-variable 'font-lock-keywords)
3800
3801 ;; Horrors. `dylan-mode' sets the `major-mode' name after calling this
3802 ;; hook, which undoes all of our configuration.
3803 (setq major-mode 'dylan-mode)
3804 (font-lock-set-defaults)
3805
3806 (let* ((word "[-_a-zA-Z!*@<>$%]+")
3807 (dylan-keywords (mdw-regexps
3808
3809 "C-address" "C-callable-wrapper" "C-function"
3810 "C-mapped-subtype" "C-pointer-type" "C-struct"
3811 "C-subtype" "C-union" "C-variable"
3812
3813 "above" "abstract" "afterwards" "all"
3814 "begin" "below" "block" "by"
3815 "case" "class" "cleanup" "constant" "create"
3816 "define" "domain"
3817 "else" "elseif" "end" "exception" "export"
3818 "finally" "for" "from" "function"
3819 "generic"
3820 "handler"
3821 "if" "in" "instance" "interface" "iterate"
3822 "keyed-by"
3823 "let" "library" "local"
3824 "macro" "method" "module"
3825 "otherwise"
3826 "profiling"
3827 "select" "slot" "subclass"
3828 "table" "then" "to"
3829 "unless" "until" "use"
3830 "variable" "virtual"
3831 "when" "while"))
3832 (sharp-keywords (mdw-regexps
3833 "all-keys" "key" "next" "rest" "include"
3834 "t" "f")))
3835 (setq font-lock-keywords
535c927f
MW
3836 (list (list (concat "\\<\\(" dylan-keywords
3837 "\\|" "with\\(out\\)?-" word
3838 "\\)\\>")
3839 '(0 font-lock-keyword-face))
3840 (list (concat "\\<" word ":" "\\|"
3841 "#\\(" sharp-keywords "\\)\\>")
3842 '(0 font-lock-variable-name-face))
3843 (list (concat "\\("
3844 "\\([-+]\\|\\<\\)[0-9]+" "\\("
3845 "\\(\\.[0-9]+\\)?" "\\([eE][-+][0-9]+\\)?"
3846 "\\|" "/[0-9]+"
3847 "\\)"
3848 "\\|" "\\.[0-9]+" "\\([eE][-+][0-9]+\\)?"
3849 "\\|" "#b[01]+"
3850 "\\|" "#o[0-7]+"
3851 "\\|" "#x[0-9a-zA-Z]+"
3852 "\\)\\>")
3853 '(0 mdw-number-face))
3854 (list (concat "\\("
3855 "\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\|"
3856 "\\_<[-+*/=<>:&|]+\\_>"
3857 "\\)")
3858 '(0 mdw-punct-face))))))
ad305d7e 3859
b50c6712
MW
3860(progn
3861 (add-hook 'dylan-mode-hook 'mdw-misc-mode-config t)
3862 (add-hook 'dylan-mode-hook 'mdw-fontify-dylan t))
3863
ad305d7e 3864;;;--------------------------------------------------------------------------
7fce54c3
MW
3865;;; Algol 68 configuration.
3866
08b1b191 3867(setq-default a68-indent-step 2)
7fce54c3
MW
3868
3869(defun mdw-fontify-algol-68 ()
3870
3871 ;; Fix up the syntax table.
3872 (modify-syntax-entry ?# "!" a68-mode-syntax-table)
3873 (dolist (ch '(?- ?+ ?= ?< ?> ?* ?/ ?| ?&))
3874 (modify-syntax-entry ch "." a68-mode-syntax-table))
3875
3876 (make-local-variable 'font-lock-keywords)
3877
3878 (let ((not-comment
3879 (let ((word "COMMENT"))
3880 (do ((regexp (concat "[^" (substring word 0 1) "]+")
3881 (concat regexp "\\|"
3882 (substring word 0 i)
3883 "[^" (substring word i (1+ i)) "]"))
3884 (i 1 (1+ i)))
3885 ((>= i (length word)) regexp)))))
3886 (setq font-lock-keywords
535c927f
MW
3887 (list (list (concat "\\<COMMENT\\>"
3888 "\\(" not-comment "\\)\\{0,5\\}"
3889 "\\(\\'\\|\\<COMMENT\\>\\)")
3890 '(0 font-lock-comment-face))
3891 (list (concat "\\<CO\\>"
3892 "\\([^C]+\\|C[^O]\\)\\{0,5\\}"
3893 "\\($\\|\\<CO\\>\\)")
3894 '(0 font-lock-comment-face))
3895 (list "\\<[A-Z_]+\\>"
3896 '(0 font-lock-keyword-face))
3897 (list (concat "\\<"
3898 "[0-9]+"
3899 "\\(\\.[0-9]+\\)?"
3900 "\\([eE][-+]?[0-9]+\\)?"
3901 "\\>")
3902 '(0 mdw-number-face))
3903 (list "\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/"
3904 '(0 mdw-punct-face))))))
7fce54c3 3905
b50c6712
MW
3906(dolist (hook '(a68-mode-hook a68-mode-hooks))
3907 (add-hook hook 'mdw-misc-mode-config t)
3908 (add-hook hook 'mdw-fontify-algol-68 t))
3909
7fce54c3 3910;;;--------------------------------------------------------------------------
6132bc01 3911;;; REXX configuration.
f617db13
MW
3912
3913(defun mdw-rexx-electric-* ()
3914 (interactive)
3915 (insert ?*)
3916 (rexx-indent-line))
3917
3918(defun mdw-rexx-indent-newline-indent ()
3919 (interactive)
3920 (rexx-indent-line)
3921 (if abbrev-mode (expand-abbrev))
3922 (newline-and-indent))
3923
3924(defun mdw-fontify-rexx ()
3925
6132bc01 3926 ;; Various bits of fiddling.
f617db13
MW
3927 (setq mdw-auto-indent nil)
3928 (local-set-key [?\C-m] 'mdw-rexx-indent-newline-indent)
3929 (local-set-key [?*] 'mdw-rexx-electric-*)
6c4bd06b
MW
3930 (dolist (ch '(?! ?? ?# ?@ ?$)) (modify-syntax-entry ch "w"))
3931 (dolist (ch '(?¬)) (modify-syntax-entry ch "."))
f617db13
MW
3932 (mdw-standard-fill-prefix "\\([ \t]*/?\*[ \t]*\\)")
3933
6132bc01 3934 ;; Set up keywords and things for fontification.
f617db13
MW
3935 (make-local-variable 'font-lock-keywords-case-fold-search)
3936 (setq font-lock-keywords-case-fold-search t)
3937
3938 (setq rexx-indent 2)
3939 (setq rexx-end-indent rexx-indent)
f617db13
MW
3940 (setq rexx-cont-indent rexx-indent)
3941
02109a0d 3942 (make-local-variable 'font-lock-keywords)
f617db13 3943 (let ((rexx-keywords
8d6d55b9
MW
3944 (mdw-regexps "address" "arg" "by" "call" "digits" "do" "drop"
3945 "else" "end" "engineering" "exit" "expose" "for"
3946 "forever" "form" "fuzz" "if" "interpret" "iterate"
3947 "leave" "linein" "name" "nop" "numeric" "off" "on"
3948 "options" "otherwise" "parse" "procedure" "pull"
3949 "push" "queue" "return" "say" "select" "signal"
3950 "scientific" "source" "then" "trace" "to" "until"
3951 "upper" "value" "var" "version" "when" "while"
3952 "with"
3953
3954 "abbrev" "abs" "bitand" "bitor" "bitxor" "b2x"
3955 "center" "center" "charin" "charout" "chars"
3956 "compare" "condition" "copies" "c2d" "c2x"
3957 "datatype" "date" "delstr" "delword" "d2c" "d2x"
3958 "errortext" "format" "fuzz" "insert" "lastpos"
3959 "left" "length" "lineout" "lines" "max" "min"
3960 "overlay" "pos" "queued" "random" "reverse" "right"
3961 "sign" "sourceline" "space" "stream" "strip"
3962 "substr" "subword" "symbol" "time" "translate"
3963 "trunc" "value" "verify" "word" "wordindex"
3964 "wordlength" "wordpos" "words" "xrange" "x2b" "x2c"
3965 "x2d")))
f617db13
MW
3966
3967 (setq font-lock-keywords
535c927f 3968 (list
f617db13 3969
535c927f
MW
3970 ;; Set up the keywords defined above.
3971 (list (concat "\\<\\(" rexx-keywords "\\)\\>")
3972 '(0 font-lock-keyword-face))
f617db13 3973
535c927f
MW
3974 ;; Fontify all symbols the same way.
3975 (list (concat "\\<\\([0-9.][A-Za-z0-9.!?_#@$]*[Ee][+-]?[0-9]+\\|"
3976 "[A-Za-z0-9.!?_#@$]+\\)")
3977 '(0 font-lock-variable-name-face))
f617db13 3978
535c927f
MW
3979 ;; And everything else is punctuation.
3980 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3981 '(0 mdw-punct-face))))))
f617db13 3982
b50c6712
MW
3983(progn
3984 (add-hook 'rexx-mode-hook 'mdw-misc-mode-config t)
3985 (add-hook 'rexx-mode-hook 'mdw-fontify-rexx t))
3986
6132bc01
MW
3987;;;--------------------------------------------------------------------------
3988;;; Standard ML programming style.
f617db13 3989
b50c6712
MW
3990(setq-default sml-nested-if-indent t
3991 sml-case-indent nil
3992 sml-indent-level 4
3993 sml-type-of-indent nil)
3994
f617db13
MW
3995(defun mdw-fontify-sml ()
3996
6132bc01 3997 ;; Make underscore an honorary letter.
f617db13
MW
3998 (modify-syntax-entry ?' "w")
3999
6132bc01 4000 ;; Set fill prefix.
f617db13
MW
4001 (mdw-standard-fill-prefix "\\([ \t]*(\*[ \t]*\\)")
4002
6132bc01 4003 ;; Now define fontification things.
02109a0d 4004 (make-local-variable 'font-lock-keywords)
f617db13 4005 (let ((sml-keywords
8d6d55b9
MW
4006 (mdw-regexps "abstype" "and" "andalso" "as"
4007 "case"
4008 "datatype" "do"
4009 "else" "end" "eqtype" "exception"
4010 "fn" "fun" "functor"
4011 "handle"
4012 "if" "in" "include" "infix" "infixr"
4013 "let" "local"
4014 "nonfix"
4015 "of" "op" "open" "orelse"
4016 "raise" "rec"
4017 "sharing" "sig" "signature" "struct" "structure"
4018 "then" "type"
4019 "val"
4020 "where" "while" "with" "withtype")))
f617db13
MW
4021
4022 (setq font-lock-keywords
535c927f 4023 (list
f617db13 4024
535c927f
MW
4025 ;; Set up the keywords defined above.
4026 (list (concat "\\<\\(" sml-keywords "\\)\\>")
4027 '(0 font-lock-keyword-face))
f617db13 4028
535c927f
MW
4029 ;; At least numbers are simpler than C.
4030 (list (concat "\\<\\~?"
4031 "\\(0\\([wW]?[xX][0-9a-fA-F]+\\|"
4032 "[wW][0-9]+\\)\\|"
4033 "\\([0-9]+\\(\\.[0-9]+\\)?"
4034 "\\([eE]\\~?"
4035 "[0-9]+\\)?\\)\\)")
4036 '(0 mdw-number-face))
f617db13 4037
535c927f
MW
4038 ;; And anything else is punctuation.
4039 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4040 '(0 mdw-punct-face))))))
f617db13 4041
b50c6712
MW
4042(progn
4043 (add-hook 'sml-mode-hook 'mdw-misc-mode-config t)
4044 (add-hook 'sml-mode-hook 'mdw-fontify-sml t))
4045
6132bc01
MW
4046;;;--------------------------------------------------------------------------
4047;;; Haskell configuration.
f617db13 4048
b50c6712
MW
4049(setq-default haskell-indent-offset 2)
4050
f617db13
MW
4051(defun mdw-fontify-haskell ()
4052
6132bc01 4053 ;; Fiddle with syntax table to get comments right.
5952a020
MW
4054 (modify-syntax-entry ?' "_")
4055 (modify-syntax-entry ?- ". 12")
f617db13
MW
4056 (modify-syntax-entry ?\n ">")
4057
4d90cf3d
MW
4058 ;; Make punctuation be punctuation
4059 (let ((punct "=<>+-*/|&%!@?$.^:#`"))
4060 (do ((i 0 (1+ i)))
4061 ((>= i (length punct)))
4062 (modify-syntax-entry (aref punct i) ".")))
4063
6132bc01 4064 ;; Set fill prefix.
f617db13
MW
4065 (mdw-standard-fill-prefix "\\([ \t]*{?--?[ \t]*\\)")
4066
6132bc01 4067 ;; Fiddle with fontification.
02109a0d 4068 (make-local-variable 'font-lock-keywords)
f617db13 4069 (let ((haskell-keywords
5952a020
MW
4070 (mdw-regexps "as"
4071 "case" "ccall" "class"
4072 "data" "default" "deriving" "do"
4073 "else" "exists"
4074 "forall" "foreign"
4075 "hiding"
4076 "if" "import" "in" "infix" "infixl" "infixr" "instance"
4077 "let"
4078 "mdo" "module"
4079 "newtype"
4080 "of"
4081 "proc"
4082 "qualified"
4083 "rec"
4084 "safe" "stdcall"
4085 "then" "type"
4086 "unsafe"
4087 "where"))
4088 (control-sequences
4089 (mdw-regexps "ACK" "BEL" "BS" "CAN" "CR" "DC1" "DC2" "DC3" "DC4"
4090 "DEL" "DLE" "EM" "ENQ" "EOT" "ESC" "ETB" "ETX" "FF"
4091 "FS" "GS" "HT" "LF" "NAK" "NUL" "RS" "SI" "SO" "SOH"
4092 "SP" "STX" "SUB" "SYN" "US" "VT")))
f617db13
MW
4093
4094 (setq font-lock-keywords
535c927f
MW
4095 (list
4096 (list (concat "{-" "[^-]*" "\\(-+[^-}][^-]*\\)*"
4097 "\\(-+}\\|-*\\'\\)"
4098 "\\|"
4099 "--.*$")
4100 '(0 font-lock-comment-face))
4101 (list (concat "\\_<\\(" haskell-keywords "\\)\\_>")
4102 '(0 font-lock-keyword-face))
4103 (list (concat "'\\("
4104 "[^\\]"
4105 "\\|"
4106 "\\\\"
4107 "\\(" "[abfnrtv\\\"']" "\\|"
4108 "^" "\\(" control-sequences "\\|"
4109 "[]A-Z@[\\^_]" "\\)" "\\|"
4110 "\\|"
4111 "[0-9]+" "\\|"
4112 "[oO][0-7]+" "\\|"
4113 "[xX][0-9A-Fa-f]+"
4114 "\\)"
4115 "\\)'")
4116 '(0 font-lock-string-face))
4117 (list "\\_<[A-Z]\\(\\sw+\\|\\s_+\\)*\\_>"
4118 '(0 font-lock-variable-name-face))
4119 (list (concat "\\_<0\\([xX][0-9a-fA-F]+\\|[oO][0-7]+\\)\\|"
4120 "\\_<[0-9]+\\(\\.[0-9]*\\)?"
4121 "\\([eE][-+]?[0-9]+\\)?")
4122 '(0 mdw-number-face))
4123 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4124 '(0 mdw-punct-face))))))
f617db13 4125
b50c6712
MW
4126(progn
4127 (add-hook 'haskell-mode-hook 'mdw-misc-mode-config t)
4128 (add-hook 'haskell-mode-hook 'mdw-fontify-haskell t))
4129
6132bc01
MW
4130;;;--------------------------------------------------------------------------
4131;;; Erlang configuration.
2ded9493 4132
08b1b191 4133(setq-default erlang-electric-commands nil)
2ded9493
MW
4134
4135(defun mdw-fontify-erlang ()
4136
6132bc01 4137 ;; Set fill prefix.
2ded9493
MW
4138 (mdw-standard-fill-prefix "\\([ \t]*{?%*[ \t]*\\)")
4139
6132bc01 4140 ;; Fiddle with fontification.
2ded9493
MW
4141 (make-local-variable 'font-lock-keywords)
4142 (let ((erlang-keywords
4143 (mdw-regexps "after" "and" "andalso"
4144 "band" "begin" "bnot" "bor" "bsl" "bsr" "bxor"
4145 "case" "catch" "cond"
4146 "div" "end" "fun" "if" "let" "not"
4147 "of" "or" "orelse"
4148 "query" "receive" "rem" "try" "when" "xor")))
4149
4150 (setq font-lock-keywords
535c927f
MW
4151 (list
4152 (list "%.*$"
4153 '(0 font-lock-comment-face))
4154 (list (concat "\\<\\(" erlang-keywords "\\)\\>")
4155 '(0 font-lock-keyword-face))
4156 (list (concat "^-\\sw+\\>")
4157 '(0 font-lock-keyword-face))
4158 (list "\\<[0-9]+\\(#[0-9a-zA-Z]+\\|[eE][+-]?[0-9]+\\)?\\>"
4159 '(0 mdw-number-face))
4160 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4161 '(0 mdw-punct-face))))))
2ded9493 4162
b50c6712
MW
4163(progn
4164 (add-hook 'erlang-mode-hook 'mdw-misc-mode-config t)
4165 (add-hook 'erlang-mode-hook 'mdw-fontify-erlang t))
4166
6132bc01
MW
4167;;;--------------------------------------------------------------------------
4168;;; Texinfo configuration.
f617db13
MW
4169
4170(defun mdw-fontify-texinfo ()
4171
6132bc01 4172 ;; Set fill prefix.
f617db13
MW
4173 (mdw-standard-fill-prefix "\\([ \t]*@c[ \t]+\\)")
4174
6132bc01 4175 ;; Real fontification things.
02109a0d 4176 (make-local-variable 'font-lock-keywords)
f617db13 4177 (setq font-lock-keywords
535c927f 4178 (list
f617db13 4179
535c927f
MW
4180 ;; Environment names are keywords.
4181 (list "@\\(end\\) *\\([a-zA-Z]*\\)?"
4182 '(2 font-lock-keyword-face))
f617db13 4183
535c927f
MW
4184 ;; Unmark escaped magic characters.
4185 (list "\\(@\\)\\([@{}]\\)"
4186 '(1 font-lock-keyword-face)
4187 '(2 font-lock-variable-name-face))
f617db13 4188
535c927f
MW
4189 ;; Make sure we get comments properly.
4190 (list "@c\\(omment\\)?\\( .*\\)?$"
4191 '(0 font-lock-comment-face))
f617db13 4192
535c927f
MW
4193 ;; Command names are keywords.
4194 (list "@\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)"
4195 '(0 font-lock-keyword-face))
f617db13 4196
535c927f
MW
4197 ;; Fontify TeX special characters as punctuation.
4198 (list "[{}]+"
4199 '(0 mdw-punct-face)))))
f617db13 4200
b50c6712
MW
4201(dolist (hook '(texinfo-mode-hook TeXinfo-mode-hook))
4202 (add-hook hook 'mdw-misc-mode-config t)
4203 (add-hook hook 'mdw-fontify-texinfo t))
4204
6132bc01
MW
4205;;;--------------------------------------------------------------------------
4206;;; TeX and LaTeX configuration.
f617db13 4207
b50c6712
MW
4208(setq-default LaTeX-table-label "tbl:"
4209 TeX-auto-untabify nil
4210 LaTeX-syntactic-comments nil
4211 LaTeX-fill-break-at-separators '(\\\[))
4212
f617db13
MW
4213(defun mdw-fontify-tex ()
4214 (setq ispell-parser 'tex)
55f80fae 4215 (turn-on-reftex)
f617db13 4216
6132bc01 4217 ;; Don't make maths into a string.
f617db13
MW
4218 (modify-syntax-entry ?$ ".")
4219 (modify-syntax-entry ?$ "." font-lock-syntax-table)
4220 (local-set-key [?$] 'self-insert-command)
4221
df200ecd 4222 ;; Make `tab' be useful, given that tab stops in TeX don't work well.
060c23ce 4223 (local-set-key "\C-\M-i" 'indent-relative)
df200ecd
MW
4224 (setq indent-tabs-mode nil)
4225
6132bc01 4226 ;; Set fill prefix.
f617db13
MW
4227 (mdw-standard-fill-prefix "\\([ \t]*%+[ \t]*\\)")
4228
6132bc01 4229 ;; Real fontification things.
02109a0d 4230 (make-local-variable 'font-lock-keywords)
f617db13 4231 (setq font-lock-keywords
535c927f
MW
4232 (list
4233
4234 ;; Environment names are keywords.
4235 (list (concat "\\\\\\(begin\\|end\\|newenvironment\\)"
4236 "{\\([^}\n]*\\)}")
4237 '(2 font-lock-keyword-face))
4238
4239 ;; Suspended environment names are keywords too.
4240 (list (concat "\\\\\\(suspend\\|resume\\)\\(\\[[^]]*\\]\\)?"
4241 "{\\([^}\n]*\\)}")
4242 '(3 font-lock-keyword-face))
4243
4244 ;; Command names are keywords.
4245 (list "\\\\\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)"
4246 '(0 font-lock-keyword-face))
4247
4248 ;; Handle @/.../ for italics.
4249 ;; (list "\\(@/\\)\\([^/]*\\)\\(/\\)"
4250 ;; '(1 font-lock-keyword-face)
4251 ;; '(3 font-lock-keyword-face))
4252
4253 ;; Handle @*...* for boldness.
4254 ;; (list "\\(@\\*\\)\\([^*]*\\)\\(\\*\\)"
4255 ;; '(1 font-lock-keyword-face)
4256 ;; '(3 font-lock-keyword-face))
4257
4258 ;; Handle @`...' for literal syntax things.
4259 ;; (list "\\(@`\\)\\([^']*\\)\\('\\)"
4260 ;; '(1 font-lock-keyword-face)
4261 ;; '(3 font-lock-keyword-face))
4262
4263 ;; Handle @<...> for nonterminals.
4264 ;; (list "\\(@<\\)\\([^>]*\\)\\(>\\)"
4265 ;; '(1 font-lock-keyword-face)
4266 ;; '(3 font-lock-keyword-face))
4267
4268 ;; Handle other @-commands.
4269 ;; (list "@\\([^a-zA-Z]\\|[a-zA-Z]*\\)"
4270 ;; '(0 font-lock-keyword-face))
4271
4272 ;; Make sure we get comments properly.
4273 (list "%.*"
4274 '(0 font-lock-comment-face))
4275
4276 ;; Fontify TeX special characters as punctuation.
4277 (list "[$^_{}#&]"
4278 '(0 mdw-punct-face)))))
f617db13 4279
d9bba20d
MW
4280(setq TeX-install-font-lock 'tex-font-setup)
4281
8638f2f3
MW
4282(eval-after-load 'font-latex
4283 '(defun font-latex-jit-lock-force-redisplay (buf start end)
4284 "Compatibility for Emacsen not offering `jit-lock-force-redisplay'."
4285 ;; The following block is an expansion of `jit-lock-force-redisplay'
4286 ;; and involved macros taken from CVS Emacs on 2007-04-28.
4287 (with-current-buffer buf
4288 (let ((modified (buffer-modified-p)))
4289 (unwind-protect
4290 (let ((buffer-undo-list t)
4291 (inhibit-read-only t)
4292 (inhibit-point-motion-hooks t)
4293 (inhibit-modification-hooks t)
4294 deactivate-mark
4295 buffer-file-name
4296 buffer-file-truename)
4297 (put-text-property start end 'fontified t))
4298 (unless modified
4299 (restore-buffer-modified-p nil)))))))
4300
b50c6712 4301(setq TeX-output-view-style
535c927f
MW
4302 '(("^dvi$"
4303 ("^landscape$" "^pstricks$\\|^pst-\\|^psfrag$")
4304 "%(o?)dvips -t landscape %d -o && xdg-open %f")
4305 ("^dvi$" "^pstricks$\\|^pst-\\|^psfrag$"
4306 "%(o?)dvips %d -o && xdg-open %f")
4307 ("^dvi$"
4308 ("^a4\\(?:dutch\\|paper\\|wide\\)\\|sem-a4$" "^landscape$")
4309 "%(o?)xdvi %dS -paper a4r -s 0 %d")
4310 ("^dvi$" "^a4\\(?:dutch\\|paper\\|wide\\)\\|sem-a4$"
4311 "%(o?)xdvi %dS -paper a4 %d")
4312 ("^dvi$"
4313 ("^a5\\(?:comb\\|paper\\)$" "^landscape$")
4314 "%(o?)xdvi %dS -paper a5r -s 0 %d")
4315 ("^dvi$" "^a5\\(?:comb\\|paper\\)$" "%(o?)xdvi %dS -paper a5 %d")
4316 ("^dvi$" "^b5paper$" "%(o?)xdvi %dS -paper b5 %d")
4317 ("^dvi$" "^letterpaper$" "%(o?)xdvi %dS -paper us %d")
4318 ("^dvi$" "^legalpaper$" "%(o?)xdvi %dS -paper legal %d")
4319 ("^dvi$" "^executivepaper$" "%(o?)xdvi %dS -paper 7.25x10.5in %d")
4320 ("^dvi$" "." "%(o?)xdvi %dS %d")
4321 ("^pdf$" "." "xdg-open %o")
4322 ("^html?$" "." "sensible-browser %o")))
b50c6712
MW
4323
4324(setq TeX-view-program-list
535c927f 4325 '(("mupdf" ("mupdf %o" (mode-io-correlate " %(outpage)")))))
b50c6712
MW
4326
4327(setq TeX-view-program-selection
535c927f
MW
4328 '(((output-dvi style-pstricks) "dvips and gv")
4329 (output-dvi "xdvi")
4330 (output-pdf "mupdf")
4331 (output-html "sensible-browser")))
b50c6712
MW
4332
4333(setq TeX-open-quote "\""
4334 TeX-close-quote "\"")
4335
4336(setq reftex-use-external-file-finders t
4337 reftex-auto-recenter-toc t)
4338
4339(setq reftex-label-alist
535c927f
MW
4340 '(("theorem" ?T "th:" "~\\ref{%s}" t ("theorems?" "th\\.") -2)
4341 ("axiom" ?A "ax:" "~\\ref{%s}" t ("axioms?" "ax\\.") -2)
4342 ("definition" ?D "def:" "~\\ref{%s}" t ("definitions?" "def\\.") -2)
4343 ("proposition" ?P "prop:" "~\\ref{%s}" t
4344 ("propositions?" "prop\\.") -2)
4345 ("lemma" ?L "lem:" "~\\ref{%s}" t ("lemmas?" "lem\\.") -2)
4346 ("example" ?X "eg:" "~\\ref{%s}" t ("examples?") -2)
4347 ("exercise" ?E "ex:" "~\\ref{%s}" t ("exercises?" "ex\\.") -2)
4348 ("enumerate" ?i "i:" "~\\ref{%s}" item ("items?"))))
b50c6712 4349(setq reftex-section-prefixes
535c927f
MW
4350 '((0 . "part:")
4351 (1 . "ch:")
4352 (t . "sec:")))
b50c6712
MW
4353
4354(setq bibtex-field-delimiters 'double-quotes
4355 bibtex-align-at-equal-sign t
4356 bibtex-entry-format '(realign opts-or-alts required-fields
4357 numerical-fields last-comma delimiters
4358 unify-case sort-fields braces)
4359 bibtex-sort-ignore-string-entries nil
4360 bibtex-maintain-sorted-entries 'entry-class
4361 bibtex-include-OPTkey t
4362 bibtex-autokey-names-stretch 1
4363 bibtex-autokey-expand-strings t
4364 bibtex-autokey-name-separator "-"
4365 bibtex-autokey-year-length 4
4366 bibtex-autokey-titleword-separator "-"
4367 bibtex-autokey-name-year-separator "-"
4368 bibtex-autokey-year-title-separator ":")
4369
4370(progn
4371 (dolist (hook '(tex-mode-hook latex-mode-hook
4372 TeX-mode-hook LaTeX-mode-hook))
4373 (add-hook hook 'mdw-misc-mode-config t)
4374 (add-hook hook 'mdw-fontify-tex t))
4375 (add-hook 'bibtex-mode-hook (lambda () (setq fill-column 76))))
ad14c2fe 4376
6132bc01 4377;;;--------------------------------------------------------------------------
445ddb61
MW
4378;;; HTML, CSS, and other web foolishness.
4379
9d681d74 4380(setq-default css-indent-offset 8)
445ddb61
MW
4381
4382;;;--------------------------------------------------------------------------
6132bc01 4383;;; SGML hacking.
f25cf300 4384
b50c6712
MW
4385(setq-default psgml-html-build-new-buffer nil)
4386
f25cf300
MW
4387(defun mdw-sgml-mode ()
4388 (interactive)
4389 (sgml-mode)
4390 (mdw-standard-fill-prefix "")
8a425bd7 4391 (make-local-variable 'sgml-delimiters)
f25cf300 4392 (setq sgml-delimiters
535c927f
MW
4393 '("AND" "&" "COM" "--" "CRO" "&#" "DSC" "]" "DSO" "[" "DTGC" "]"
4394 "DTGO" "[" "ERO" "&" "ETAGO" ":e" "GRPC" ")" "GRPO" "(" "LIT"
4395 "\"" "LITA" "'" "MDC" ">" "MDO" "<!" "MINUS" "-" "MSC" "]]"
4396 "NESTC" "{" "NET" "}" "OPT" "?" "OR" "|" "PERO" "%" "PIC" ">"
4397 "PIO" "<?" "PLUS" "+" "REFC" "." "REP" "*" "RNI" "#" "SEQ" ","
4398 "STAGO" ":" "TAGC" "." "VI" "=" "MS-START" "<![" "MS-END" "]]>"
4399 "XML-ECOM" "-->" "XML-PIC" "?>" "XML-SCOM" "<!--" "XML-TAGCE"
4400 "/>" "NULL" ""))
f25cf300
MW
4401 (setq major-mode 'mdw-sgml-mode)
4402 (setq mode-name "[mdw] SGML")
4403 (run-hooks 'mdw-sgml-mode-hook))
6cb52f8b
MW
4404
4405;;;--------------------------------------------------------------------------
4406;;; Configuration files.
4407
3ce407bb
MW
4408(defcustom mdw-conf-quote-normal nil
4409 "Control syntax category of quote characters `\"' and `''.
6cb52f8b
MW
4410If this is `t', consider quote characters to be normal
4411punctuation, as for `conf-quote-normal'. If this is `nil' then
4412leave quote characters as quotes. If this is a list, then
4413consider the quote characters in the list to be normal
4414punctuation. If this is a single quote character, then consider
3ce407bb
MW
4415that character only to be normal punctuation."
4416 :type '(choice boolean character (repeat character))
4417 :safe 'mdw-conf-quote-normal-acceptable-value-p)
6cb52f8b
MW
4418(defun mdw-conf-quote-normal-acceptable-value-p (value)
4419 "Is the VALUE is an acceptable value for `mdw-conf-quote-normal'?"
4420 (or (booleanp value)
4421 (every (lambda (v) (memq v '(?\" ?')))
4422 (if (listp value) value (list value)))))
6cb52f8b
MW
4423
4424(defun mdw-fix-up-quote ()
4425 "Apply the setting of `mdw-conf-quote-normal'."
4426 (let ((flag mdw-conf-quote-normal))
4427 (cond ((eq flag t)
4428 (conf-quote-normal t))
4429 ((not flag)
4430 nil)
4431 (t
4432 (let ((table (copy-syntax-table (syntax-table))))
6c4bd06b
MW
4433 (dolist (ch (if (listp flag) flag (list flag)))
4434 (modify-syntax-entry ch "." table))
6cb52f8b
MW
4435 (set-syntax-table table)
4436 (and font-lock-mode (font-lock-fontify-buffer)))))))
b50c6712
MW
4437
4438(progn
4439 (add-hook 'conf-mode-hook 'mdw-misc-mode-config t)
4440 (add-hook 'conf-mode-local-variables-hook 'mdw-fix-up-quote t t))
f25cf300 4441
6132bc01
MW
4442;;;--------------------------------------------------------------------------
4443;;; Shell scripts.
f617db13
MW
4444
4445(defun mdw-setup-sh-script-mode ()
4446
6132bc01 4447 ;; Fetch the shell interpreter's name.
f617db13
MW
4448 (let ((shell-name sh-shell-file))
4449
6132bc01 4450 ;; Try reading the hash-bang line.
f617db13
MW
4451 (save-excursion
4452 (goto-char (point-min))
4453 (if (looking-at "#![ \t]*\\([^ \t\n]*\\)")
4454 (setq shell-name (match-string 1))))
4455
6132bc01 4456 ;; Now try to set the shell.
f617db13
MW
4457 ;;
4458 ;; Don't let `sh-set-shell' bugger up my script.
f617db13
MW
4459 (let ((executable-set-magic #'(lambda (s &rest r) s)))
4460 (sh-set-shell shell-name)))
4461
10c51541
MW
4462 ;; Don't insert here-document scaffolding automatically.
4463 (local-set-key "<" 'self-insert-command)
4464
6132bc01 4465 ;; Now enable my keys and the fontification.
f617db13
MW
4466 (mdw-misc-mode-config)
4467
6132bc01 4468 ;; Set the indentation level correctly.
f617db13
MW
4469 (setq sh-indentation 2)
4470 (setq sh-basic-offset 2))
4471
070c1dca
MW
4472(setq sh-shell-file "/bin/sh")
4473
6d6e095a
MW
4474;; Awful hacking to override the shell detection for particular scripts.
4475(defmacro define-custom-shell-mode (name shell)
4476 `(defun ,name ()
4477 (interactive)
4478 (set (make-local-variable 'sh-shell-file) ,shell)
4479 (sh-mode)))
4480(define-custom-shell-mode bash-mode "/bin/bash")
4481(define-custom-shell-mode rc-mode "/usr/bin/rc")
4482(put 'sh-shell-file 'permanent-local t)
4483
4484;; Hack the rc syntax table. Backquotes aren't paired in rc.
4485(eval-after-load "sh-script"
4486 '(or (assq 'rc sh-mode-syntax-table-input)
4487 (let ((frag '(nil
4488 ?# "<"
4489 ?\n ">#"
4490 ?\" "\"\""
4491 ?\' "\"\'"
4492 ?$ "'"
4493 ?\` "."
4494 ?! "_"
4495 ?% "_"
4496 ?. "_"
4497 ?^ "_"
4498 ?~ "_"
4499 ?, "_"
4500 ?= "."
4501 ?< "."
4502 ?> "."))
4503 (assoc (assq 'rc sh-mode-syntax-table-input)))
4504 (if assoc
4505 (rplacd assoc frag)
4506 (setq sh-mode-syntax-table-input
535c927f
MW
4507 (cons (cons 'rc frag)
4508 sh-mode-syntax-table-input))))))
6d6e095a 4509
b50c6712
MW
4510(progn
4511 (add-hook 'sh-mode-hook 'mdw-misc-mode-config t)
4512 (add-hook 'sh-mode-hook 'mdw-setup-sh-script-mode t))
4513
6132bc01 4514;;;--------------------------------------------------------------------------
092f0a38
MW
4515;;; Emacs shell mode.
4516
4517(defun mdw-eshell-prompt ()
4518 (let ((left "[") (right "]"))
4519 (when (= (user-uid) 0)
4520 (setq left "«" right "»"))
4521 (concat left
4522 (save-match-data
4523 (replace-regexp-in-string "\\..*$" "" (system-name)))
4524 " "
2d8b2924
MW
4525 (let* ((pwd (eshell/pwd)) (npwd (length pwd))
4526 (home (expand-file-name "~")) (nhome (length home)))
4527 (if (and (>= npwd nhome)
4528 (or (= nhome npwd)
5801e199
MW
4529 (= (elt pwd nhome) ?/))
4530 (string= (substring pwd 0 nhome) home))
2d8b2924
MW
4531 (concat "~" (substring pwd (length home)))
4532 pwd))
092f0a38 4533 right)))
08b1b191
MW
4534(setq-default eshell-prompt-function 'mdw-eshell-prompt)
4535(setq-default eshell-prompt-regexp "^\\[[^]>]+\\(\\]\\|>>?\\)")
092f0a38 4536
2d8b2924
MW
4537(defun eshell/e (file) (find-file file) nil)
4538(defun eshell/ee (file) (find-file-other-window file) nil)
4539(defun eshell/w3m (url) (w3m-goto-url url) nil)
415a23dd 4540
092f0a38
MW
4541(mdw-define-face eshell-prompt (t :weight bold))
4542(mdw-define-face eshell-ls-archive (t :weight bold :foreground "red"))
4543(mdw-define-face eshell-ls-backup (t :foreground "lightgrey" :slant italic))
4544(mdw-define-face eshell-ls-product (t :foreground "lightgrey" :slant italic))
4545(mdw-define-face eshell-ls-clutter (t :foreground "lightgrey" :slant italic))
4546(mdw-define-face eshell-ls-executable (t :weight bold))
4547(mdw-define-face eshell-ls-directory (t :foreground "cyan" :weight bold))
4548(mdw-define-face eshell-ls-readonly (t nil))
4549(mdw-define-face eshell-ls-symlink (t :foreground "cyan"))
4550
b1a598dc 4551(defun mdw-eshell-hack () (setenv "LD_PRELOAD" nil))
8845865d
MW
4552(add-hook 'eshell-mode-hook 'mdw-eshell-hack)
4553
092f0a38 4554;;;--------------------------------------------------------------------------
6132bc01 4555;;; Messages-file mode.
f617db13 4556
4bb22eea 4557(defun messages-mode-guts ()
f617db13
MW
4558 (setq messages-mode-syntax-table (make-syntax-table))
4559 (set-syntax-table messages-mode-syntax-table)
f617db13
MW
4560 (modify-syntax-entry ?0 "w" messages-mode-syntax-table)
4561 (modify-syntax-entry ?1 "w" messages-mode-syntax-table)
4562 (modify-syntax-entry ?2 "w" messages-mode-syntax-table)
4563 (modify-syntax-entry ?3 "w" messages-mode-syntax-table)
4564 (modify-syntax-entry ?4 "w" messages-mode-syntax-table)
4565 (modify-syntax-entry ?5 "w" messages-mode-syntax-table)
4566 (modify-syntax-entry ?6 "w" messages-mode-syntax-table)
4567 (modify-syntax-entry ?7 "w" messages-mode-syntax-table)
4568 (modify-syntax-entry ?8 "w" messages-mode-syntax-table)
4569 (modify-syntax-entry ?9 "w" messages-mode-syntax-table)
4570 (make-local-variable 'comment-start)
4571 (make-local-variable 'comment-end)
4572 (make-local-variable 'indent-line-function)
4573 (setq indent-line-function 'indent-relative)
4574 (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
4575 (make-local-variable 'font-lock-defaults)
4bb22eea 4576 (make-local-variable 'messages-mode-keywords)
f617db13 4577 (let ((keywords
8d6d55b9
MW
4578 (mdw-regexps "array" "bitmap" "callback" "docs[ \t]+enum"
4579 "export" "enum" "fixed-octetstring" "flags"
4580 "harmless" "map" "nested" "optional"
4581 "optional-tagged" "package" "primitive"
4582 "primitive-nullfree" "relaxed[ \t]+enum"
4583 "set" "table" "tagged-optional" "union"
4584 "variadic" "vector" "version" "version-tag")))
4bb22eea 4585 (setq messages-mode-keywords
535c927f
MW
4586 (list
4587 (list (concat "\\<\\(" keywords "\\)\\>:")
4588 '(0 font-lock-keyword-face))
4589 '("\\([-a-zA-Z0-9]+:\\)" (0 font-lock-warning-face))
4590 '("\\(\\<[a-z][-_a-zA-Z0-9]*\\)"
4591 (0 font-lock-variable-name-face))
4592 '("\\<\\([0-9]+\\)\\>" (0 mdw-number-face))
4593 '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4594 (0 mdw-punct-face)))))
f617db13 4595 (setq font-lock-defaults
535c927f 4596 '(messages-mode-keywords nil nil nil nil))
f617db13
MW
4597 (run-hooks 'messages-file-hook))
4598
4599(defun messages-mode ()
4600 (interactive)
4601 (fundamental-mode)
4602 (setq major-mode 'messages-mode)
4603 (setq mode-name "Messages")
4bb22eea 4604 (messages-mode-guts)
f617db13
MW
4605 (modify-syntax-entry ?# "<" messages-mode-syntax-table)
4606 (modify-syntax-entry ?\n ">" messages-mode-syntax-table)
4607 (setq comment-start "# ")
4608 (setq comment-end "")
f617db13
MW
4609 (run-hooks 'messages-mode-hook))
4610
4611(defun cpp-messages-mode ()
4612 (interactive)
4613 (fundamental-mode)
4614 (setq major-mode 'cpp-messages-mode)
4615 (setq mode-name "CPP Messages")
4bb22eea 4616 (messages-mode-guts)
f617db13
MW
4617 (modify-syntax-entry ?* ". 23" messages-mode-syntax-table)
4618 (modify-syntax-entry ?/ ". 14" messages-mode-syntax-table)
4619 (setq comment-start "/* ")
4620 (setq comment-end " */")
4621 (let ((preprocessor-keywords
8d6d55b9
MW
4622 (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
4623 "ident" "if" "ifdef" "ifndef" "import" "include"
4624 "line" "pragma" "unassert" "undef" "warning")))
4bb22eea 4625 (setq messages-mode-keywords
535c927f
MW
4626 (append (list (list (concat "^[ \t]*\\#[ \t]*"
4627 "\\(include\\|import\\)"
4628 "[ \t]*\\(<[^>]+\\(>\\)?\\)")
4629 '(2 font-lock-string-face))
4630 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
4631 preprocessor-keywords
4632 "\\)\\>\\|[0-9]+\\|$\\)\\)")
4633 '(1 font-lock-keyword-face)))
4634 messages-mode-keywords)))
297d60aa 4635 (run-hooks 'cpp-messages-mode-hook))
f617db13 4636
b50c6712
MW
4637(progn
4638 (add-hook 'messages-mode-hook 'mdw-misc-mode-config t)
4639 (add-hook 'cpp-messages-mode-hook 'mdw-misc-mode-config t)
4640 ;; (add-hook 'messages-file-hook 'mdw-fontify-messages t)
4641 )
f617db13 4642
6132bc01
MW
4643;;;--------------------------------------------------------------------------
4644;;; Messages-file mode.
f617db13
MW
4645
4646(defvar mallow-driver-substitution-face 'mallow-driver-substitution-face
4647 "Face to use for subsittution directives.")
4648(make-face 'mallow-driver-substitution-face)
4649(defvar mallow-driver-text-face 'mallow-driver-text-face
4650 "Face to use for body text.")
4651(make-face 'mallow-driver-text-face)
4652
4653(defun mallow-driver-mode ()
4654 (interactive)
4655 (fundamental-mode)
4656 (setq major-mode 'mallow-driver-mode)
4657 (setq mode-name "Mallow driver")
4658 (setq mallow-driver-mode-syntax-table (make-syntax-table))
4659 (set-syntax-table mallow-driver-mode-syntax-table)
4660 (make-local-variable 'comment-start)
4661 (make-local-variable 'comment-end)
4662 (make-local-variable 'indent-line-function)
4663 (setq indent-line-function 'indent-relative)
4664 (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
4665 (make-local-variable 'font-lock-defaults)
4666 (make-local-variable 'mallow-driver-mode-keywords)
4667 (let ((keywords
8d6d55b9
MW
4668 (mdw-regexps "each" "divert" "file" "if"
4669 "perl" "set" "string" "type" "write")))
f617db13 4670 (setq mallow-driver-mode-keywords
535c927f
MW
4671 (list
4672 (list (concat "^%\\s *\\(}\\|\\(" keywords "\\)\\>\\).*$")
4673 '(0 font-lock-keyword-face))
4674 (list "^%\\s *\\(#.*\\)?$"
4675 '(0 font-lock-comment-face))
4676 (list "^%"
4677 '(0 font-lock-keyword-face))
4678 (list "^|?\\(.+\\)$" '(1 mallow-driver-text-face))
4679 (list "\\${[^}]*}"
4680 '(0 mallow-driver-substitution-face t)))))
f617db13
MW
4681 (setq font-lock-defaults
4682 '(mallow-driver-mode-keywords nil nil nil nil))
4683 (modify-syntax-entry ?\" "_" mallow-driver-mode-syntax-table)
4684 (modify-syntax-entry ?\n ">" mallow-driver-mode-syntax-table)
4685 (setq comment-start "%# ")
4686 (setq comment-end "")
f617db13
MW
4687 (run-hooks 'mallow-driver-mode-hook))
4688
b50c6712
MW
4689(progn
4690 (add-hook 'mallow-driver-hook 'mdw-misc-mode-config t))
f617db13 4691
6132bc01
MW
4692;;;--------------------------------------------------------------------------
4693;;; NFast debugs.
f617db13
MW
4694
4695(defun nfast-debug-mode ()
4696 (interactive)
4697 (fundamental-mode)
4698 (setq major-mode 'nfast-debug-mode)
4699 (setq mode-name "NFast debug")
4700 (setq messages-mode-syntax-table (make-syntax-table))
4701 (set-syntax-table messages-mode-syntax-table)
4702 (make-local-variable 'font-lock-defaults)
4703 (make-local-variable 'nfast-debug-mode-keywords)
4704 (setq truncate-lines t)
4705 (setq nfast-debug-mode-keywords
535c927f
MW
4706 (list
4707 '("^\\(NFast_\\(Connect\\|Disconnect\\|Submit\\|Wait\\)\\)"
4708 (0 font-lock-keyword-face))
4709 (list (concat "^[ \t]+\\(\\("
4710 "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
4711 "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
4712 "[ \t]+\\)*"
4713 "[0-9a-fA-F]+\\)[ \t]*$")
4714 '(0 mdw-number-face))
4715 '("^[ \t]+\.status=[ \t]+\\<\\(OK\\)\\>"
4716 (1 font-lock-keyword-face))
4717 '("^[ \t]+\.status=[ \t]+\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>"
4718 (1 font-lock-warning-face))
4719 '("^[ \t]+\.status[ \t]+\\<\\(zero\\)\\>"
4720 (1 nil))
4721 (list (concat "^[ \t]+\\.cmd=[ \t]+"
4722 "\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>")
4723 '(1 font-lock-keyword-face))
4724 '("-?\\<\\([0-9]+\\|0x[0-9a-fA-F]+\\)\\>" (0 mdw-number-face))
4725 '("^\\([ \t]+[a-z0-9.]+\\)" (0 font-lock-variable-name-face))
4726 '("\\<\\([a-z][a-z0-9.]+\\)\\>=" (1 font-lock-variable-name-face))
4727 '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" (0 mdw-punct-face))))
f617db13 4728 (setq font-lock-defaults
535c927f 4729 '(nfast-debug-mode-keywords nil nil nil nil))
f617db13
MW
4730 (run-hooks 'nfast-debug-mode-hook))
4731
6132bc01 4732;;;--------------------------------------------------------------------------
658bc848 4733;;; Lispy languages.
f617db13 4734
873d87df
MW
4735;; Unpleasant bodge.
4736(unless (boundp 'slime-repl-mode-map)
4737 (setq slime-repl-mode-map (make-sparse-keymap)))
4738
f617db13
MW
4739(defun mdw-indent-newline-and-indent ()
4740 (interactive)
4741 (indent-for-tab-command)
4742 (newline-and-indent))
4743
4744(eval-after-load "cl-indent"
4745 '(progn
4746 (mapc #'(lambda (pair)
4747 (put (car pair)
4748 'common-lisp-indent-function
4749 (cdr pair)))
4750 '((destructuring-bind . ((&whole 4 &rest 1) 4 &body))
4751 (multiple-value-bind . ((&whole 4 &rest 1) 4 &body))))))
4752
4753(defun mdw-common-lisp-indent ()
8a425bd7 4754 (make-local-variable 'lisp-indent-function)
f617db13
MW
4755 (setq lisp-indent-function 'common-lisp-indent-function))
4756
36cd5c10
MW
4757(defmacro mdw-advise-hyperspec-lookup (func args)
4758 `(defadvice ,func (around mdw-browse-w3m ,args activate compile)
4759 (if (fboundp 'w3m)
4760 (let ((browse-url-browser-function #'mdw-w3m-browse-url))
4761 ad-do-it)
4762 ad-do-it)))
0c3e50d5
MW
4763(mdw-advise-hyperspec-lookup common-lisp-hyperspec (symbol))
4764(mdw-advise-hyperspec-lookup common-lisp-hyperspec-format (char))
4765(mdw-advise-hyperspec-lookup common-lisp-hyperspec-lookup-reader-macro (char))
36cd5c10 4766
f617db13
MW
4767(defun mdw-fontify-lispy ()
4768
6132bc01 4769 ;; Set fill prefix.
f617db13
MW
4770 (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)")
4771
6132bc01 4772 ;; Not much fontification needed.
02109a0d 4773 (make-local-variable 'font-lock-keywords)
535c927f
MW
4774 (setq font-lock-keywords
4775 (list (list (concat "\\("
4776 "\\_<[-+]?"
4777 "\\(" "[0-9]+/[0-9]+"
4778 "\\|" "\\(" "[0-9]+" "\\(\\.[0-9]*\\)?" "\\|"
4779 "\\.[0-9]+" "\\)"
4780 "\\([dDeEfFlLsS][-+]?[0-9]+\\)?"
4781 "\\)"
4782 "\\|"
4783 "#"
4784 "\\(" "x" "[-+]?"
4785 "[0-9A-Fa-f]+" "\\(/[0-9A-Fa-f]+\\)?"
4786 "\\|" "o" "[-+]?" "[0-7]+" "\\(/[0-7]+\\)?"
4787 "\\|" "b" "[-+]?" "[01]+" "\\(/[01]+\\)?"
4788 "\\|" "[0-9]+" "r" "[-+]?"
4789 "[0-9a-zA-Z]+" "\\(/[0-9a-zA-Z]+\\)?"
4790 "\\)"
4791 "\\)\\_>")
4792 '(0 mdw-number-face))
4793 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4794 '(0 mdw-punct-face)))))
f617db13 4795
7ab487de
MW
4796;; Special indentation.
4797
3ce407bb
MW
4798(defcustom mdw-lisp-loop-default-indent 2
4799 "Default indent for simple `loop' body."
4800 :type 'integer
4801 :safe 'integerp)
4802(defcustom mdw-lisp-setf-value-indent 2
4803 "Default extra indent for `setf' values."
4804 :type 'integer :safe 'integerp)
1be8ceca
MW
4805
4806(setq lisp-simple-loop-indentation 0
4807 lisp-loop-keyword-indentation 0
4808 lisp-loop-forms-indentation 2
4809 lisp-lambda-list-keyword-parameter-alignment t)
7ab487de 4810
48e4a665
MW
4811(defun mdw-indent-funcall
4812 (path state &optional indent-point sexp-column normal-indent)
a0075a4a
MW
4813 "Indent `funcall' more usefully.
4814Essentially, treat `funcall foo' as a function name, and align the arguments
4815to `foo'."
48e4a665 4816 (and (or (not (consp path)) (null (cadr path)))
a0075a4a
MW
4817 (save-excursion
4818 (goto-char (cadr state))
4819 (forward-char 1)
4820 (let ((start-line (line-number-at-pos)))
4821 (and (condition-case nil (progn (forward-sexp 3) t)
4822 (scan-error nil))
4823 (progn
4824 (forward-sexp -1)
4825 (and (= start-line (line-number-at-pos))
4826 (current-column))))))))
48e4a665
MW
4827(progn
4828 (put 'funcall 'common-lisp-indent-function 'mdw-indent-funcall)
4829 (put 'funcall 'lisp-indent-function 'mdw-indent-funcall))
a0075a4a 4830
fcbac48d
MW
4831(defun mdw-indent-setf
4832 (path state &optional indent-point sexp-column normal-indent)
4833 "Indent `setf' more usefully.
4834If the values aren't on the same lines as their variables then indent them
4835by `mdw-lisp-setf-value-indent' spaces."
4836 (and (or (not (consp path)) (null (cadr path)))
4837 (let ((basic-indent (save-excursion
4838 (goto-char (cadr state))
4839 (forward-char 1)
4840 (and (condition-case nil
4841 (progn (forward-sexp 2) t)
4842 (scan-error nil))
4843 (progn
4844 (forward-sexp -1)
4845 (current-column)))))
4846 (offset (if (consp path) (car path)
4847 (catch 'done
4848 (save-excursion
4849 (let ((start path)
4850 (count 0))
4851 (goto-char (cadr state))
4852 (forward-char 1)
4853 (while (< (point) start)
4854 (condition-case nil (forward-sexp 1)
4855 (scan-error (throw 'done nil)))
4856 (incf count))
4857 (1- count)))))))
4858 (and basic-indent offset
4859 (list (+ basic-indent
4860 (if (oddp offset) 0
4861 mdw-lisp-setf-value-indent))
4862 basic-indent)))))
4863(progn
4864 (put 'setf 'common-lisp-indent-functopion 'mdw-indent-setf)
4865 (put 'psetf 'common-lisp-indent-function 'mdw-indent-setf)
4866 (put 'setq 'common-lisp-indent-function 'mdw-indent-setf)
4867 (put 'setf 'lisp-indent-function 'mdw-indent-setf)
4868 (put 'setq 'lisp-indent-function 'mdw-indent-setf)
4869 (put 'setq-local 'lisp-indent-function 'mdw-indent-setf)
4870 (put 'setq-default 'lisp-indent-function 'mdw-indent-setf))
4871
1be8ceca
MW
4872(defadvice common-lisp-loop-part-indentation
4873 (around mdw-fix-loop-indentation (indent-point state) activate compile)
4874 "Improve `loop' indentation.
4875If the first subform is on the same line as the `loop' keyword, then
4876align the other subforms beneath it. Otherwise, indent them
4877`mdw-lisp-loop-default-indent' columns in from the opening parenthesis."
4878
4879 (let* ((loop-indentation (save-excursion
4880 (goto-char (elt state 1))
4881 (current-column))))
4882
4883 ;; Don't really care about this.
dede52e3
MW
4884 (when (and (boundp 'lisp-indent-backquote-substitution-mode)
4885 (eq lisp-indent-backquote-substitution-mode 'corrected))
1be8ceca
MW
4886 (save-excursion
4887 (goto-char (elt state 1))
a151a655 4888 (incf loop-indentation
1be8ceca
MW
4889 (cond ((eq (char-before) ?,) -1)
4890 ((and (eq (char-before) ?@)
4891 (progn (backward-char)
4892 (eq (char-before) ?,)))
4893 -2)
4894 (t 0)))))
4895
4896 ;; If the first loop item is on the same line as the `loop' itself then
4897 ;; use that as the baseline. Otherwise advance by the default indent.
4898 (goto-char (cadr state))
4899 (forward-char 1)
4900 (let ((baseline-indent
4901 (if (= (line-number-at-pos)
4902 (if (condition-case nil (progn (forward-sexp 2) t)
4903 (scan-error nil))
4904 (progn (forward-sexp -1) (line-number-at-pos))
4905 -1))
4906 (current-column)
4907 (+ loop-indentation mdw-lisp-loop-default-indent))))
4908
4909 (goto-char indent-point)
4910 (beginning-of-line)
4911
4912 (setq ad-return-value
535c927f 4913 (list
dede52e3
MW
4914 (cond ((condition-case ()
4915 (save-excursion
4916 (goto-char (elt state 1))
4917 (forward-char 1)
4918 (forward-sexp 2)
4919 (backward-sexp 1)
4920 (not (looking-at "\\(:\\|\\sw\\)")))
4921 (error nil))
535c927f
MW
4922 (+ baseline-indent lisp-simple-loop-indentation))
4923 ((looking-at "^\\s-*\\(:?\\sw+\\|;\\)")
4924 (+ baseline-indent lisp-loop-keyword-indentation))
4925 (t
4926 (+ baseline-indent lisp-loop-forms-indentation)))
4927
4928 ;; Tell the caller that the next line needs recomputation,
4929 ;; even though it doesn't start a sexp.
4930 loop-indentation)))))
1be8ceca 4931
b50c6712
MW
4932;; SLIME setup.
4933
3ce407bb
MW
4934(defcustom mdw-friendly-name "[mdw]"
4935 "How I want to be addressed."
4936 :type 'string
4937 :safe 'stringp)
5432b9cd
MW
4938(defadvice slime-user-first-name
4939 (around mdw-use-friendly-name compile activate)
4940 (if mdw-friendly-name (setq ad-return-value mdw-friendly-name)
4941 ad-do-it))
4942
a28427d1
MW
4943(eval-and-compile
4944 (trap
4945 (if (not mdw-fast-startup)
4946 (progn
4947 (require 'slime-autoloads)
4948 (slime-setup '(slime-autodoc slime-c-p-c))))))
b50c6712
MW
4949
4950(let ((stuff '((cmucl ("cmucl"))
4951 (sbcl ("sbcl") :coding-system utf-8-unix)
4952 (clisp ("clisp") :coding-system utf-8-unix))))
4953 (or (boundp 'slime-lisp-implementations)
4954 (setq slime-lisp-implementations nil))
4955 (while stuff
4956 (let* ((head (car stuff))
4957 (found (assq (car head) slime-lisp-implementations)))
4958 (setq stuff (cdr stuff))
4959 (if found
4960 (rplacd found (cdr head))
4961 (setq slime-lisp-implementations
535c927f 4962 (cons head slime-lisp-implementations))))))
b50c6712
MW
4963(setq slime-default-lisp 'sbcl)
4964
4965;; Hooks.
4966
4967(progn
4968 (dolist (hook '(emacs-lisp-mode-hook
4969 scheme-mode-hook
4970 lisp-mode-hook
4971 inferior-lisp-mode-hook
4972 lisp-interaction-mode-hook
4973 ielm-mode-hook
4974 slime-repl-mode-hook))
4975 (add-hook hook 'mdw-misc-mode-config t)
4976 (add-hook hook 'mdw-fontify-lispy t))
4977 (add-hook 'lisp-mode-hook 'mdw-common-lisp-indent t)
4978 (add-hook 'inferior-lisp-mode-hook
4979 #'(lambda () (local-set-key "\C-m" 'comint-send-and-indent)) t))
4980
658bc848
MW
4981;;;--------------------------------------------------------------------------
4982;;; Other languages.
4983
4984;; Smalltalk.
4985
4986(defun mdw-setup-smalltalk ()
4987 (and mdw-auto-indent
4988 (local-set-key "\C-m" 'smalltalk-newline-and-indent))
4989 (make-local-variable 'mdw-auto-indent)
4990 (setq mdw-auto-indent nil)
4991 (local-set-key "\C-i" 'smalltalk-reindent))
4992
4993(defun mdw-fontify-smalltalk ()
4994 (make-local-variable 'font-lock-keywords)
4995 (setq font-lock-keywords
535c927f
MW
4996 (list
4997 (list "\\<[A-Z][a-zA-Z0-9]*\\>"
4998 '(0 font-lock-keyword-face))
4999 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
5000 "[0-9][0-9_]*\\(\\.[0-9_]*\\)?"
5001 "\\([eE][-+]?[0-9_]+\\)?")
5002 '(0 mdw-number-face))
5003 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
5004 '(0 mdw-punct-face)))))
658bc848 5005
b50c6712
MW
5006(progn
5007 (add-hook 'smalltalk-mode 'mdw-misc-mode-config t)
5008 (add-hook 'smalltalk-mode 'mdw-fontify-smalltalk t))
5009
df77a575
MW
5010;; m4.
5011
ec007bea 5012(defun mdw-setup-m4 ()
ed5d93a4
MW
5013
5014 ;; Inexplicably, Emacs doesn't match braces in m4 mode. This is very
5015 ;; annoying: fix it.
5016 (modify-syntax-entry ?{ "(")
5017 (modify-syntax-entry ?} ")")
5018
5019 ;; Fill prefix.
ec007bea
MW
5020 (mdw-standard-fill-prefix "\\([ \t]*\\(?:#+\\|\\<dnl\\>\\)[ \t]*\\)"))
5021
b50c6712
MW
5022(dolist (hook '(m4-mode-hook autoconf-mode-hook autotest-mode-hook))
5023 (add-hook hook #'mdw-misc-mode-config t)
5024 (add-hook hook #'mdw-setup-m4 t))
5025
5026;; Make.
5027
5028(progn
5029 (add-hook 'makefile-mode-hook 'mdw-misc-mode-config t))
5030
6132bc01
MW
5031;;;--------------------------------------------------------------------------
5032;;; Text mode.
f617db13
MW
5033
5034(defun mdw-text-mode ()
5035 (setq fill-column 72)
5036 (flyspell-mode t)
5037 (mdw-standard-fill-prefix
c7a8da49 5038 "\\([ \t]*\\([>#|:] ?\\)*[ \t]*\\)" 3)
f617db13
MW
5039 (auto-fill-mode 1))
5040
060c23ce
MW
5041(eval-after-load "flyspell"
5042 '(define-key flyspell-mode-map "\C-\M-i" nil))
5043
b50c6712
MW
5044(progn
5045 (add-hook 'text-mode-hook 'mdw-text-mode t))
5046
6132bc01 5047;;;--------------------------------------------------------------------------
faf2cef7 5048;;; Outline and hide/show modes.
5de5db48
MW
5049
5050(defun mdw-outline-collapse-all ()
5051 "Completely collapse everything in the entire buffer."
5052 (interactive)
5053 (save-excursion
5054 (goto-char (point-min))
5055 (while (< (point) (point-max))
5056 (hide-subtree)
5057 (forward-line))))
5058
faf2cef7
MW
5059(setq hs-hide-comments-when-hiding-all nil)
5060
b200af26 5061(defadvice hs-hide-all (after hide-first-comment activate)
941c29ba 5062 (save-excursion (hs-hide-initial-comment-block)))
b200af26 5063
6132bc01
MW
5064;;;--------------------------------------------------------------------------
5065;;; Shell mode.
f617db13
MW
5066
5067(defun mdw-sh-mode-setup ()
5068 (local-set-key [?\C-a] 'comint-bol)
5069 (add-hook 'comint-output-filter-functions
5070 'comint-watch-for-password-prompt))
5071
5072(defun mdw-term-mode-setup ()
3d9147ea 5073 (setq term-prompt-regexp shell-prompt-pattern)
f617db13
MW
5074 (make-local-variable 'mouse-yank-at-point)
5075 (make-local-variable 'transient-mark-mode)
5076 (setq mouse-yank-at-point t)
f617db13
MW
5077 (auto-fill-mode -1)
5078 (setq tab-width 8))
5079
539dce4c
MW
5080(defun comint-send-and-indent ()
5081 (interactive)
5082 (comint-send-input)
5083 (and mdw-auto-indent
5084 (indent-for-tab-command)))
5085
5086(defadvice comint-line-beginning-position
5087 (around mdw-calculate-it-properly () activate compile)
5088 "Calculate the actual line start for multi-line input."
5089 (if (or comint-use-prompt-regexp
5090 (eq (field-at-pos (point)) 'output))
5091 ad-do-it
5092 (setq ad-return-value
5093 (constrain-to-field (line-beginning-position) (point)))))
5094
3d9147ea
MW
5095(defun term-send-meta-right () (interactive) (term-send-raw-string "\e\e[C"))
5096(defun term-send-meta-left () (interactive) (term-send-raw-string "\e\e[D"))
5097(defun term-send-ctrl-uscore () (interactive) (term-send-raw-string "\C-_"))
5098(defun term-send-meta-meta-something ()
5099 (interactive)
5100 (term-send-raw-string "\e\e")
5101 (term-send-raw))
5102(eval-after-load 'term
5103 '(progn
5104 (define-key term-raw-map [?\e ?\e] nil)
5105 (define-key term-raw-map [?\e ?\e t] 'term-send-meta-meta-something)
5106 (define-key term-raw-map [?\C-/] 'term-send-ctrl-uscore)
5107 (define-key term-raw-map [M-right] 'term-send-meta-right)
5108 (define-key term-raw-map [?\e ?\M-O ?C] 'term-send-meta-right)
5109 (define-key term-raw-map [M-left] 'term-send-meta-left)
5110 (define-key term-raw-map [?\e ?\M-O ?D] 'term-send-meta-left)))
5111
c4434c20
MW
5112(defadvice term-exec (before program-args-list compile activate)
5113 "If the PROGRAM argument is a list, interpret it as (PROGRAM . SWITCHES).
5114This allows you to pass a list of arguments through `ansi-term'."
5115 (let ((program (ad-get-arg 2)))
5116 (if (listp program)
5117 (progn
5118 (ad-set-arg 2 (car program))
5119 (ad-set-arg 4 (cdr program))))))
5120
8845865d
MW
5121(defadvice term-exec-1 (around hack-environment compile activate)
5122 "Hack the environment inherited by inferiors in the terminal."
f8592fee 5123 (let ((process-environment (copy-tree process-environment)))
8845865d
MW
5124 (setenv "LD_PRELOAD" nil)
5125 ad-do-it))
5126
5127(defadvice shell (around hack-environment compile activate)
5128 "Hack the environment inherited by inferiors in the shell."
f8592fee 5129 (let ((process-environment (copy-tree process-environment)))
8845865d
MW
5130 (setenv "LD_PRELOAD" nil)
5131 ad-do-it))
5132
c4434c20
MW
5133(defun ssh (host)
5134 "Open a terminal containing an ssh session to the HOST."
5135 (interactive "sHost: ")
5136 (ansi-term (list "ssh" host) (format "ssh@%s" host)))
5137
3ce407bb 5138(defcustom git-grep-command
20b6cd68 5139 "env GIT_PAGER=cat git grep --no-color -nH -e "
3ce407bb
MW
5140 "The default command for \\[git-grep]."
5141 :type 'string)
5aa1b95f
MW
5142
5143(defvar git-grep-history nil)
5144
5145(defun git-grep (command-args)
5146 "Run `git grep' with user-specified args and collect output in a buffer."
5147 (interactive
5148 (list (read-shell-command "Run git grep (like this): "
5149 git-grep-command 'git-grep-history)))
6a0a9a51
MW
5150 (let ((grep-use-null-device nil))
5151 (grep command-args)))
5aa1b95f 5152
c63bce81
MW
5153;;;--------------------------------------------------------------------------
5154;;; Magit configuration.
5155
30702ee3 5156(setq magit-diff-refine-hunk 't
c14a5ec3 5157 magit-view-git-manual-method 'man
83d2acdd 5158 magit-log-margin '(nil age magit-log-margin-width t 18)
c14a5ec3
MW
5159 magit-wip-after-save-local-mode-lighter ""
5160 magit-wip-after-apply-mode-lighter ""
5161 magit-wip-before-change-mode-lighter "")
5162(eval-after-load "magit"
5163 '(progn (global-magit-file-mode 1)
5164 (magit-wip-after-save-mode 1)
5165 (magit-wip-after-apply-mode 1)
5166 (magit-wip-before-change-mode 1)
60c22e1b 5167 (add-to-list 'magit-no-confirm 'safe-with-wip)
2a67803a 5168 (add-to-list 'magit-no-confirm 'trash)
87746eb7
MW
5169 (push '(:eval (if (or magit-wip-after-save-local-mode
5170 magit-wip-after-apply-mode
5171 magit-wip-before-change-mode)
5172 (format " wip:%s%s%s"
5173 (if magit-wip-after-apply-mode "A" "")
5174 (if magit-wip-before-change-mode "C" "")
5175 (if magit-wip-after-save-local-mode "S" ""))))
5176 minor-mode-alist)
60c22e1b
MW
5177 (dolist (popup '(magit-diff-popup
5178 magit-diff-refresh-popup
5179 magit-diff-mode-refresh-popup
5180 magit-revision-mode-refresh-popup))
60803ce3
MW
5181 (magit-define-popup-switch popup ?R "Reverse diff" "-R"))
5182 (magit-define-popup-switch 'magit-rebase-popup ?r
5183 "Rebase merges" "--rebase-merges")))
c14a5ec3 5184
28509f06
MW
5185(defadvice magit-wip-commit-buffer-file
5186 (around mdw-just-this-buffer activate compile)
5187 (let ((magit-save-repository-buffers nil)) ad-do-it))
5188
2a67803a
MW
5189(defadvice magit-discard
5190 (around mdw-delete-if-prefix-argument activate compile)
5191 (let ((magit-delete-by-moving-to-trash
5192 (and (null current-prefix-arg)
5193 magit-delete-by-moving-to-trash)))
5194 ad-do-it))
5195
ff6a7bee 5196(setq magit-repolist-columns
535c927f
MW
5197 '(("Name" 16 magit-repolist-column-ident nil)
5198 ("Version" 18 magit-repolist-column-version nil)
5199 ("St" 2 magit-repolist-column-dirty nil)
5200 ("L<U" 3 mdw-repolist-column-unpulled-from-upstream nil)
5201 ("L>U" 3 mdw-repolist-column-unpushed-to-upstream nil)
5202 ("Path" 32 magit-repolist-column-path nil)))
ff6a7bee
MW
5203
5204(setq magit-repository-directories '(("~/etc/profile" . 0)
5205 ("~/src/" . 1)))
5206
5207(defadvice magit-list-repos (around mdw-dirname () activate compile)
5208 "Make sure the returned names are directory names.
5209Otherwise child processes get started in the wrong directory and
5210there is sadness."
5211 (setq ad-return-value (mapcar #'file-name-as-directory ad-do-it)))
5212
5213(defun mdw-repolist-column-unpulled-from-upstream (_id)
5214 "Insert number of upstream commits not in the current branch."
5215 (let ((upstream (magit-get-upstream-branch (magit-get-current-branch) t)))
5216 (and upstream
5217 (let ((n (cadr (magit-rev-diff-count "HEAD" upstream))))
5218 (propertize (number-to-string n) 'face
5219 (if (> n 0) 'bold 'shadow))))))
5220
5221(defun mdw-repolist-column-unpushed-to-upstream (_id)
5222 "Insert number of commits in the current branch but not its upstream."
5223 (let ((upstream (magit-get-upstream-branch (magit-get-current-branch) t)))
5224 (and upstream
5225 (let ((n (car (magit-rev-diff-count "HEAD" upstream))))
5226 (propertize (number-to-string n) 'face
5227 (if (> n 0) 'bold 'shadow))))))
5228
5d824e2f
MW
5229(defun mdw-try-smerge ()
5230 (save-excursion
5231 (goto-char (point-min))
5232 (when (re-search-forward "^<<<<<<< " nil t)
5233 (smerge-mode 1))))
5234(add-hook 'find-file-hook 'mdw-try-smerge t)
5235
8a45d685
MW
5236(defcustom mdw-magit-new-window-modes
5237 '(magit-diff-mode
5238 magit-log-mode
5239 magit-process-mode
5240 magit-revision-mode
5241 magit-stash-mode
5242 magit-status-mode)
5243 "Magit modes which should cause a new window to be used."
5244 :type '(repeat symbol))
5245
5246(defun mdw-display-magit-buffer (buffer)
5247 "Like `magit-display-buffer-traditional'.
5248But uses `mdw-magit-new-window-modes' for its list of modes
5249rather than baking the list into the function."
5250 (display-buffer buffer
b8dc30fe
MW
5251 (let ((mode (with-current-buffer buffer major-mode)))
5252 (if (and (not mdw-designated-window)
5253 (derived-mode-p 'magit-mode)
5254 (mdw-submode-p mode 'magit-mode)
5255 (not (memq mode mdw-magit-new-window-modes)))
5256 '(display-buffer-same-window . nil)
5257 nil))))
8a45d685
MW
5258(setq magit-display-buffer-function 'mdw-display-magit-buffer)
5259
9f76176c
MW
5260(defun mdw-display-magit-file-buffer (buffer)
5261 "Show a file buffer from a diff."
5262 (select-window (display-buffer buffer)))
5263(setq magit-display-file-buffer-function 'mdw-display-magit-file-buffer)
5264
e07e3320 5265;;;--------------------------------------------------------------------------
e48c2e5b
MW
5266;;; GUD, and especially GDB.
5267
5268;; Inhibit window dedication. I mean, seriously, wtf?
5269(defadvice gdb-display-buffer (after mdw-undedicated (buf) compile activate)
5270 "Don't make windows dedicated. Seriously."
5271 (set-window-dedicated-p ad-return-value nil))
5272(defadvice gdb-set-window-buffer
5273 (after mdw-undedicated (name &optional ignore-dedicated window)
5274 compile activate)
5275 "Don't make windows dedicated. Seriously."
5276 (set-window-dedicated-p (or window (selected-window)) nil))
5277
5278;;;--------------------------------------------------------------------------
a2cb2ff4
MW
5279;;; SQL stuff.
5280
5281(setq sql-postgres-options '("-n" "-P" "pager=off")
5282 sql-postgres-login-params
5283 '((user :default "mdw")
5284 (database :default "mdw")
5285 (server :default "db.distorted.org.uk")))
5286
5287;;;--------------------------------------------------------------------------
234ade9d
MW
5288;;; Man pages.
5289
5290;; Turn off `noip' when running `man': it interferes with `man-db''s own
5291;; seccomp(2)-based sandboxing, which is (in this case, at least) strictly
5292;; better.
5293(defadvice Man-getpage-in-background
5294 (around mdw-inhibit-noip (topic) compile activate)
5295 "Inhibit the `noip' preload hack when invoking `man'."
5296 (let* ((old-preload (getenv "LD_PRELOAD"))
15e3b2e2
MW
5297 (preloads (and old-preload
5298 (save-match-data (split-string old-preload ":"))))
234ade9d
MW
5299 (any nil)
5300 (filtered nil))
4b7a0fa8
MW
5301 (save-match-data
5302 (while preloads
5303 (let ((item (pop preloads)))
5304 (if (string-match "\\(/\\|^\\)noip\.so\\(:\\|$\\)" item)
5305 (setq any t)
5306 (push item filtered)))))
234ade9d
MW
5307 (if any
5308 (unwind-protect
5309 (progn
5310 (setenv "LD_PRELOAD"
5311 (and filtered
5312 (with-output-to-string
5313 (setq filtered (nreverse filtered))
5314 (let ((first t))
5315 (while filtered
5316 (if first (setq first nil)
5317 (write-char ?:))
5318 (write-string (pop filtered)))))))
5319 ad-do-it)
5320 (setenv "LD_PRELOAD" old-preload))
5321 ad-do-it)))
5322
5323;;;--------------------------------------------------------------------------
0f81a131
MW
5324;;; MPC configuration.
5325
50a77b30
MW
5326(eval-when-compile (trap (require 'mpc)))
5327
0f81a131
MW
5328(setq mpc-browser-tags '(Artist|Composer|Performer Album|Playlist))
5329
5330(defun mdw-mpc-now-playing ()
5331 (interactive)
5332 (require 'mpc)
5333 (save-excursion
5334 (set-buffer (mpc-proc-cmd (mpc-proc-cmd-list '("status" "currentsong"))))
5335 (mpc--status-callback))
5336 (let ((state (cdr (assq 'state mpc-status))))
5337 (cond ((member state '("stop"))
5338 (message "mpd stopped."))
5339 ((member state '("play" "pause"))
5340 (let* ((artist (cdr (assq 'Artist mpc-status)))
5341 (album (cdr (assq 'Album mpc-status)))
5342 (title (cdr (assq 'Title mpc-status)))
5343 (file (cdr (assq 'file mpc-status)))
5344 (duration-string (cdr (assq 'Time mpc-status)))
5345 (time-string (cdr (assq 'time mpc-status)))
5346 (time (and time-string
355d1336 5347 (string-to-number
0f81a131
MW
5348 (if (string-match ":" time-string)
5349 (substring time-string
5350 0 (match-beginning 0))
5351 (time-string)))))
5352 (duration (and duration-string
355d1336 5353 (string-to-number duration-string)))
0f81a131
MW
5354 (pos (and time duration
5355 (format " [%d:%02d/%d:%02d]"
5356 (/ time 60) (mod time 60)
5357 (/ duration 60) (mod duration 60))))
5358 (fmt (cond ((and artist title)
5359 (format "`%s' by %s%s" title artist
5360 (if album (format ", from `%s'" album)
5361 "")))
5362 (file
5363 (format "`%s' (no tags)" file))
5364 (t
5365 "(no idea what's playing!)"))))
5366 (if (string= state "play")
5367 (message "mpd playing %s%s" fmt (or pos ""))
5368 (message "mpd paused in %s%s" fmt (or pos "")))))
5369 (t
5370 (message "mpd in unknown state `%s'" state)))))
5371
4aba12fa
MW
5372(defmacro mdw-define-mpc-wrapper (func bvl interactive &rest body)
5373 `(defun ,func ,bvl
5374 (interactive ,@interactive)
5375 (require 'mpc)
5376 ,@body
5377 (mdw-mpc-now-playing)))
5378
5379(mdw-define-mpc-wrapper mdw-mpc-play-or-pause () nil
5380 (if (member (cdr (assq 'state (mpc-cmd-status))) '("play"))
5381 (mpc-pause)
5382 (mpc-play)))
5383
5384(mdw-define-mpc-wrapper mdw-mpc-next () nil (mpc-next))
5385(mdw-define-mpc-wrapper mdw-mpc-prev () nil (mpc-prev))
5386(mdw-define-mpc-wrapper mdw-mpc-stop () nil (mpc-stop))
0f81a131 5387
5147578f
MW
5388(defun mdw-mpc-louder (step)
5389 (interactive (list (if current-prefix-arg
5390 (prefix-numeric-value current-prefix-arg)
5391 +10)))
5392 (mpc-proc-cmd (format "volume %+d" step)))
5393
5394(defun mdw-mpc-quieter (step)
5395 (interactive (list (if current-prefix-arg
5396 (prefix-numeric-value current-prefix-arg)
5397 +10)))
5398 (mpc-proc-cmd (format "volume %+d" (- step))))
5399
6dbdfe26
MW
5400(defun mdw-mpc-hack-lines (arg interactivep func)
5401 (if (and interactivep (use-region-p))
5402 (let ((from (region-beginning)) (to (region-end)))
5403 (goto-char from)
5404 (beginning-of-line)
5405 (funcall func)
5406 (forward-line)
5407 (while (< (point) to)
5408 (funcall func)
5409 (forward-line)))
5410 (let ((n (prefix-numeric-value arg)))
5411 (cond ((minusp n)
5412 (unless (bolp)
5413 (beginning-of-line)
5414 (funcall func)
5415 (incf n))
5416 (while (minusp n)
5417 (forward-line -1)
5418 (funcall func)
5419 (incf n)))
5420 (t
5421 (beginning-of-line)
5422 (while (plusp n)
5423 (funcall func)
5424 (forward-line)
5425 (decf n)))))))
5426
5427(defun mdw-mpc-select-one ()
4466dfac
MW
5428 (when (and (get-char-property (point) 'mpc-file)
5429 (not (get-char-property (point) 'mpc-select)))
6dbdfe26
MW
5430 (mpc-select-toggle)))
5431
5432(defun mdw-mpc-unselect-one ()
5433 (when (get-char-property (point) 'mpc-select)
5434 (mpc-select-toggle)))
5435
5436(defun mdw-mpc-select (&optional arg interactivep)
5437 (interactive (list current-prefix-arg t))
a30d0e33 5438 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
6dbdfe26
MW
5439
5440(defun mdw-mpc-unselect (&optional arg interactivep)
5441 (interactive (list current-prefix-arg t))
a30d0e33 5442 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-unselect-one))
6dbdfe26
MW
5443
5444(defun mdw-mpc-unselect-backwards (arg)
5445 (interactive "p")
a30d0e33 5446 (mdw-mpc-hack-lines (- arg) t 'mdw-mpc-unselect-one))
6dbdfe26
MW
5447
5448(defun mdw-mpc-unselect-all ()
5449 (interactive)
5450 (setq mpc-select nil)
5451 (mpc-selection-refresh))
5452
5453(defun mdw-mpc-next-line (arg)
5454 (interactive "p")
5455 (beginning-of-line)
5456 (forward-line arg))
5457
5458(defun mdw-mpc-previous-line (arg)
5459 (interactive "p")
5460 (beginning-of-line)
5461 (forward-line (- arg)))
5462
6d6f2b51
MW
5463(defun mdw-mpc-playlist-add (&optional arg interactivep)
5464 (interactive (list current-prefix-arg t))
5465 (let ((mpc-select mpc-select))
5466 (when (or arg (and interactivep (use-region-p)))
5467 (setq mpc-select nil)
5468 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
5469 (setq mpc-select (reverse mpc-select))
5470 (mpc-playlist-add)))
5471
5472(defun mdw-mpc-playlist-delete (&optional arg interactivep)
5473 (interactive (list current-prefix-arg t))
5474 (setq mpc-select (nreverse mpc-select))
5475 (mpc-select-save
5476 (when (or arg (and interactivep (use-region-p)))
5477 (setq mpc-select nil)
5478 (mpc-selection-refresh)
5479 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
5480 (mpc-playlist-delete)))
5481
75019c66
MW
5482(defun mdw-mpc-hack-tagbrowsers ()
5483 (setq-local mode-line-format
535c927f
MW
5484 '("%e"
5485 mode-line-frame-identification
5486 mode-line-buffer-identification)))
75019c66
MW
5487(add-hook 'mpc-tagbrowser-mode-hook 'mdw-mpc-hack-tagbrowsers)
5488
65f6a37a
MW
5489(defun mdw-mpc-hack-songs ()
5490 (setq-local header-line-format
5491 ;; '("MPC " mpc-volume " " mpc-current-song)
5492 (list (propertize " " 'display '(space :align-to 0))
5493 ;; 'mpc-songs-format-description
5494 '(:eval
5495 (let ((deactivate-mark) (hscroll (window-hscroll)))
5496 (with-temp-buffer
5497 (mpc-format mpc-songs-format 'self hscroll)
5498 ;; That would be simpler than the hscroll handling in
5499 ;; mpc-format, but currently move-to-column does not
5500 ;; recognize :space display properties.
5501 ;; (move-to-column hscroll)
5502 ;; (delete-region (point-min) (point))
5503 (buffer-string)))))))
5504(add-hook 'mpc-songs-mode-hook 'mdw-mpc-hack-songs)
5505
6dbdfe26
MW
5506(eval-after-load "mpc"
5507 '(progn
5508 (define-key mpc-mode-map "m" 'mdw-mpc-select)
5509 (define-key mpc-mode-map "u" 'mdw-mpc-unselect)
5510 (define-key mpc-mode-map "\177" 'mdw-mpc-unselect-backwards)
5511 (define-key mpc-mode-map "\e\177" 'mdw-mpc-unselect-all)
5512 (define-key mpc-mode-map "n" 'mdw-mpc-next-line)
5513 (define-key mpc-mode-map "p" 'mdw-mpc-previous-line)
56ba17be 5514 (define-key mpc-mode-map "/" 'mpc-songs-search)
6dbdfe26
MW
5515 (setq mpc-songs-mode-map (make-sparse-keymap))
5516 (set-keymap-parent mpc-songs-mode-map mpc-mode-map)
5517 (define-key mpc-songs-mode-map "l" 'mpc-playlist)
6d6f2b51
MW
5518 (define-key mpc-songs-mode-map "+" 'mdw-mpc-playlist-add)
5519 (define-key mpc-songs-mode-map "-" 'mdw-mpc-playlist-delete)
56ba17be 5520 (define-key mpc-songs-mode-map "\r" 'mpc-songs-jump-to)))
6dbdfe26 5521
0f81a131 5522;;;--------------------------------------------------------------------------
e07e3320
MW
5523;;; Inferior Emacs Lisp.
5524
5525(setq comint-prompt-read-only t)
5526
5527(eval-after-load "comint"
5528 '(progn
5529 (define-key comint-mode-map "\C-w" 'comint-kill-region)
5530 (define-key comint-mode-map [C-S-backspace] 'comint-kill-whole-line)))
5531
5532(eval-after-load "ielm"
5533 '(progn
5534 (define-key ielm-map "\C-w" 'comint-kill-region)
5535 (define-key ielm-map [C-S-backspace] 'comint-kill-whole-line)))
5536
f617db13
MW
5537;;;----- That's all, folks --------------------------------------------------
5538
5539(provide 'dot-emacs)