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