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