dot/mdw-build.conf: Don't override Debian build settings from environment.
[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."
f8592fee 581 (let ((process-environment (copy-tree process-environment)))
8845865d
MW
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
94526c3f 1038 '(browse-url-mozilla
a0d16e44 1039 browse-url-generic
ed5e20a5 1040 (w3m . mdw-w3m-browse-url)
a0d16e44 1041 browse-url-w3)
6132bc01
MW
1042 "List of good browsers for mdw-good-url-browsers.
1043Each item is a browser function name, or a cons (CHECK . FUNC).
1044A symbol FOO stands for (FOO . FOO).")
a203fba8
MW
1045
1046(defun mdw-good-url-browser ()
6132bc01
MW
1047 "Return a good URL browser.
1048Trundle the list of such things, finding the first item for which
1049CHECK is fboundp, and returning the correponding FUNC."
a203fba8
MW
1050 (let ((bs mdw-good-url-browsers) b check func answer)
1051 (while (and bs (not answer))
1052 (setq b (car bs)
1053 bs (cdr bs))
1054 (if (consp b)
1055 (setq check (car b) func (cdr b))
1056 (setq check b func b))
1057 (if (fboundp check)
1058 (setq answer func)))
1059 answer))
1060
f36cdb77
MW
1061(eval-after-load "w3m-search"
1062 '(progn
1063 (dolist
1064 (item
1065 '(("g" "Google" "http://www.google.co.uk/search?q=%s")
1066 ("gd" "Google Directory"
1067 "http://www.google.com/search?cat=gwd/Top&q=%s")
1068 ("gg" "Google Groups" "http://groups.google.com/groups?q=%s")
1069 ("ward" "Ward's wiki" "http://c2.com/cgi/wiki?%s")
1070 ("gi" "Images" "http://images.google.com/images?q=%s")
1071 ("rfc" "RFC"
1072 "http://metalzone.distorted.org.uk/ftp/pub/mirrors/rfc/rfc%s.txt.gz")
1073 ("wp" "Wikipedia"
1074 "http://en.wikipedia.org/wiki/Special:Search?go=Go&search=%s")
1075 ("imdb" "IMDb" "http://www.imdb.com/Find?%s")
1076 ("nc-wiki" "nCipher wiki"
1077 "http://wiki.ncipher.com/wiki/bin/view/Devel/?topic=%s")
1078 ("map" "Google maps" "http://maps.google.co.uk/maps?q=%s&hl=en")
1079 ("lp" "Launchpad bug by number"
1080 "https://bugs.launchpad.net/bugs/%s")
1081 ("lppkg" "Launchpad bugs by package"
1082 "https://bugs.launchpad.net/%s")
1083 ("msdn" "MSDN"
1084 "http://social.msdn.microsoft.com/Search/en-GB/?query=%s&ac=8")
1085 ("debbug" "Debian bug by number"
1086 "http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=%s")
1087 ("debbugpkg" "Debian bugs by package"
1088 "http://bugs.debian.org/cgi-bin/pkgreport.cgi?pkg=%s")
1089 ("ljlogin" "LJ login" "http://www.livejournal.com/login.bml")))
1090 (add-to-list 'w3m-search-engine-alist
1091 (list (cadr item) (caddr item) nil))
1092 (add-to-list 'w3m-uri-replace-alist
1093 (list (concat "\\`" (car item) ":")
1094 'w3m-search-uri-replace
1095 (cadr item))))))
1096
6132bc01
MW
1097;;;--------------------------------------------------------------------------
1098;;; Paragraph filling.
f617db13 1099
6132bc01 1100;; Useful variables.
f617db13
MW
1101
1102(defvar mdw-fill-prefix nil
6132bc01
MW
1103 "*Used by `mdw-line-prefix' and `mdw-fill-paragraph'.
1104If there's no fill prefix currently set (by the `fill-prefix'
1105variable) and there's a match from one of the regexps here, it
1106gets used to set the fill-prefix for the current operation.
f617db13 1107
6132bc01
MW
1108The variable is a list of items of the form `REGEXP . PREFIX'; if
1109the REGEXP matches, the PREFIX is used to set the fill prefix.
1110It in turn is a list of things:
f617db13
MW
1111
1112 STRING -- insert a literal string
1113 (match . N) -- insert the thing matched by bracketed subexpression N
1114 (pad . N) -- a string of whitespace the same width as subexpression N
1115 (expr . FORM) -- the result of evaluating FORM")
1116
1117(make-variable-buffer-local 'mdw-fill-prefix)
1118
1119(defvar mdw-hanging-indents
10fa2414 1120 (concat "\\(\\("
f8bfe560 1121 "\\([*o+]\\|-[-#]?\\|[0-9]+\\.\\|\\[[0-9]+\\]\\|([a-zA-Z])\\)"
10fa2414
MW
1122 "[ \t]+"
1123 "\\)?\\)")
6132bc01
MW
1124 "*Standard regexp matching parts of a hanging indent.
1125This is mainly useful in `auto-fill-mode'.")
f617db13 1126
6132bc01 1127;; Utility functions.
f617db13 1128
cd07f97f
MW
1129(defun mdw-maybe-tabify (s)
1130 "Tabify or untabify the string S, according to `indent-tabs-mode'."
c736b08b
MW
1131 (let ((tabfun (if indent-tabs-mode #'tabify #'untabify)))
1132 (with-temp-buffer
1133 (save-match-data
f617db13 1134 (insert s "\n")
cd07f97f 1135 (let ((start (point-min)) (end (point-max)))
c736b08b
MW
1136 (funcall tabfun (point-min) (point-max))
1137 (setq s (buffer-substring (point-min) (1- (point-max)))))))))
f617db13
MW
1138
1139(defun mdw-examine-fill-prefixes (l)
6132bc01
MW
1140 "Given a list of dynamic fill prefixes, pick one which matches
1141context and return the static fill prefix to use. Point must be
1142at the start of a line, and match data must be saved."
f617db13 1143 (cond ((not l) nil)
6ed1b26a
MW
1144 ((looking-at (car (car l)))
1145 (mdw-maybe-tabify (apply #'concat
1146 (mapcar #'mdw-do-prefix-match
1147 (cdr (car l))))))
1148 (t (mdw-examine-fill-prefixes (cdr l)))))
f617db13
MW
1149
1150(defun mdw-maybe-car (p)
1151 "If P is a pair, return (car P), otherwise just return P."
1152 (if (consp p) (car p) p))
1153
1154(defun mdw-padding (s)
1155 "Return a string the same width as S but made entirely from whitespace."
1156 (let* ((l (length s)) (i 0) (n (make-string l ? )))
1157 (while (< i l)
1158 (if (= 9 (aref s i))
1159 (aset n i 9))
1160 (setq i (1+ i)))
1161 n))
1162
1163(defun mdw-do-prefix-match (m)
6132bc01
MW
1164 "Expand a dynamic prefix match element.
1165See `mdw-fill-prefix' for details."
f617db13 1166 (cond ((not (consp m)) (format "%s" m))
6ed1b26a
MW
1167 ((eq (car m) 'match) (match-string (mdw-maybe-car (cdr m))))
1168 ((eq (car m) 'pad) (mdw-padding (match-string
1169 (mdw-maybe-car (cdr m)))))
1170 ((eq (car m) 'eval) (eval (cdr m)))
1171 (t "")))
f617db13
MW
1172
1173(defun mdw-choose-dynamic-fill-prefix ()
1174 "Work out the dynamic fill prefix based on the variable `mdw-fill-prefix'."
1175 (cond ((and fill-prefix (not (string= fill-prefix ""))) fill-prefix)
6ed1b26a
MW
1176 ((not mdw-fill-prefix) fill-prefix)
1177 (t (save-excursion
1178 (beginning-of-line)
1179 (save-match-data
1180 (mdw-examine-fill-prefixes mdw-fill-prefix))))))
f617db13 1181
b8c659bb 1182(defadvice do-auto-fill (around mdw-dynamic-fill-prefix () activate compile)
6132bc01
MW
1183 "Handle auto-filling, working out a dynamic fill prefix in the
1184case where there isn't a sensible static one."
f617db13 1185 (let ((fill-prefix (mdw-choose-dynamic-fill-prefix)))
b8c659bb 1186 ad-do-it))
f617db13
MW
1187
1188(defun mdw-fill-paragraph ()
1189 "Fill paragraph, getting a dynamic fill prefix."
1190 (interactive)
1191 (let ((fill-prefix (mdw-choose-dynamic-fill-prefix)))
1192 (fill-paragraph nil)))
1193
1194(defun mdw-standard-fill-prefix (rx &optional mat)
1195 "Set the dynamic fill prefix, handling standard hanging indents and stuff.
6132bc01
MW
1196This is just a short-cut for setting the thing by hand, and by
1197design it doesn't cope with anything approximating a complicated
1198case."
f617db13 1199 (setq mdw-fill-prefix
6ed1b26a
MW
1200 `((,(concat rx mdw-hanging-indents)
1201 (match . 1)
1202 (pad . ,(or mat 2))))))
f617db13 1203
6132bc01
MW
1204;;;--------------------------------------------------------------------------
1205;;; Other common declarations.
f617db13 1206
6132bc01 1207;; Common mode settings.
f617db13
MW
1208
1209(defvar mdw-auto-indent t
1210 "Whether to indent automatically after a newline.")
1211
0e58a7c2
MW
1212(defun mdw-whitespace-mode (&optional arg)
1213 "Turn on/off whitespace mode, but don't highlight trailing space."
1214 (interactive "P")
1215 (when (and (boundp 'whitespace-style)
1216 (fboundp 'whitespace-mode))
1217 (let ((whitespace-style (remove 'trailing whitespace-style)))
558fc014
MW
1218 (whitespace-mode arg))
1219 (setq show-trailing-whitespace whitespace-mode)))
0e58a7c2 1220
21beda17
MW
1221(defvar mdw-do-misc-mode-hacking nil)
1222
f617db13
MW
1223(defun mdw-misc-mode-config ()
1224 (and mdw-auto-indent
1225 (cond ((eq major-mode 'lisp-mode)
1226 (local-set-key "\C-m" 'mdw-indent-newline-and-indent))
4a7ce1ee 1227 ((derived-mode-p 'slime-repl-mode 'asm-mode 'comint-mode)
30c8a8fb 1228 nil)
f617db13
MW
1229 (t
1230 (local-set-key "\C-m" 'newline-and-indent))))
2e7c6a86 1231 (set (make-local-variable 'mdw-do-misc-mode-hacking) t)
f617db13 1232 (local-set-key [C-return] 'newline)
8a425bd7 1233 (make-local-variable 'page-delimiter)
8cb7626b 1234 (setq page-delimiter "\f\\|^.*-\\{6\\}.*$")
f617db13
MW
1235 (setq comment-column 40)
1236 (auto-fill-mode 1)
c7203018 1237 (setq fill-column mdw-text-width)
253f61b4
MW
1238 (and (fboundp 'gtags-mode)
1239 (gtags-mode))
ddf6e116 1240 (if (fboundp 'hs-minor-mode)
612717ec 1241 (trap (hs-minor-mode t))
ddf6e116 1242 (outline-minor-mode t))
49b2646e 1243 (reveal-mode t)
1e7a9479 1244 (trap (turn-on-font-lock)))
f617db13 1245
2e7c6a86 1246(defun mdw-post-local-vars-misc-mode-config ()
c7203018 1247 (setq whitespace-line-column mdw-text-width)
2717a191
MW
1248 (when (and mdw-do-misc-mode-hacking
1249 (not buffer-read-only))
2e7c6a86
MW
1250 (setq show-trailing-whitespace t)
1251 (mdw-whitespace-mode 1)))
1252(add-hook 'hack-local-variables-hook 'mdw-post-local-vars-misc-mode-config)
ed7b46b9 1253
2c1ccbb9
MW
1254(defmacro mdw-advise-update-angry-fruit-salad (&rest funcs)
1255 `(progn ,@(mapcar (lambda (func)
1256 `(defadvice ,func
1257 (after mdw-angry-fruit-salad activate)
1258 (when mdw-do-misc-mode-hacking
1259 (setq show-trailing-whitespace
1260 (not buffer-read-only))
1261 (mdw-whitespace-mode (if buffer-read-only 0 1)))))
1262 funcs)))
1263(mdw-advise-update-angry-fruit-salad toggle-read-only
1264 read-only-mode
1265 view-mode
1266 view-mode-enable
1267 view-mode-disable)
2717a191 1268
253f61b4 1269(eval-after-load 'gtags
506bada9
MW
1270 '(progn
1271 (dolist (key '([mouse-2] [mouse-3]))
1272 (define-key gtags-mode-map key nil))
1273 (define-key gtags-mode-map [C-S-mouse-2] 'gtags-find-tag-by-event)
1274 (define-key gtags-select-mode-map [C-S-mouse-2]
1275 'gtags-select-tag-by-event)
1276 (dolist (map (list gtags-mode-map gtags-select-mode-map))
1277 (define-key map [C-S-mouse-3] 'gtags-pop-stack))))
253f61b4 1278
6132bc01 1279;; Backup file handling.
2ae647c4
MW
1280
1281(defvar mdw-backup-disable-regexps nil
6132bc01
MW
1282 "*List of regular expressions: if a file name matches any of
1283these then the file is not backed up.")
2ae647c4
MW
1284
1285(defun mdw-backup-enable-predicate (name)
6132bc01
MW
1286 "[mdw]'s default backup predicate.
1287Allows a backup if the standard predicate would allow it, and it
1288doesn't match any of the regular expressions in
1289`mdw-backup-disable-regexps'."
2ae647c4
MW
1290 (and (normal-backup-enable-predicate name)
1291 (let ((answer t) (list mdw-backup-disable-regexps))
1292 (save-match-data
1293 (while list
1294 (if (string-match (car list) name)
1295 (setq answer nil))
1296 (setq list (cdr list)))
1297 answer))))
1298(setq backup-enable-predicate 'mdw-backup-enable-predicate)
1299
7bb78c67
MW
1300;; Frame cleanup.
1301
1302(defun mdw-last-one-out-turn-off-the-lights (frame)
1303 "Disconnect from an X display if this was the last frame on that display."
1304 (let ((frame-display (frame-parameter frame 'display)))
1305 (when (and frame-display
1306 (eq window-system 'x)
1307 (not (some (lambda (fr)
7bb78c67 1308 (and (not (eq fr frame))
a04d8f3d 1309 (string= (frame-parameter fr 'display)
d70716b5 1310 frame-display)))
7bb78c67 1311 (frame-list))))
7bb78c67
MW
1312 (run-with-idle-timer 0 nil #'x-close-connection frame-display))))
1313(add-hook 'delete-frame-functions 'mdw-last-one-out-turn-off-the-lights)
1314
6132bc01 1315;;;--------------------------------------------------------------------------
f3674a83
MW
1316;;; Fullscreen-ness.
1317
1318(defvar mdw-full-screen-parameters
1319 '((menu-bar-lines . 0)
1320 ;(vertical-scroll-bars . nil)
1321 )
1322 "Frame parameters to set when making a frame fullscreen.")
1323
1324(defvar mdw-full-screen-save
1325 '(width height)
1326 "Extra frame parameters to save when setting fullscreen.")
1327
1328(defun mdw-toggle-full-screen (&optional frame)
1329 "Show the FRAME fullscreen."
1330 (interactive)
1331 (when window-system
1332 (cond ((frame-parameter frame 'fullscreen)
1333 (set-frame-parameter frame 'fullscreen nil)
1334 (modify-frame-parameters
1335 nil
1336 (or (frame-parameter frame 'mdw-full-screen-saved)
1337 (mapcar (lambda (assoc)
1338 (assq (car assoc) default-frame-alist))
1339 mdw-full-screen-parameters))))
1340 (t
1341 (let ((saved (mapcar (lambda (param)
1342 (cons param (frame-parameter frame param)))
1343 (append (mapcar #'car
1344 mdw-full-screen-parameters)
1345 mdw-full-screen-save))))
1346 (set-frame-parameter frame 'mdw-full-screen-saved saved))
1347 (modify-frame-parameters frame mdw-full-screen-parameters)
1348 (set-frame-parameter frame 'fullscreen 'fullboth)))))
1349
1350;;;--------------------------------------------------------------------------
6132bc01 1351;;; General fontification.
f617db13 1352
bc149706
MW
1353(make-face 'mdw-virgin-face)
1354
1e7a9479
MW
1355(defmacro mdw-define-face (name &rest body)
1356 "Define a face, and make sure it's actually set as the definition."
1357 (declare (indent 1)
1358 (debug 0))
1359 `(progn
bc149706 1360 (copy-face 'mdw-virgin-face ',name)
1e7a9479
MW
1361 (defvar ,name ',name)
1362 (put ',name 'face-defface-spec ',body)
88cb9c2b 1363 (face-spec-set ',name ',body nil)))
1e7a9479
MW
1364
1365(mdw-define-face default
1366 (((type w32)) :family "courier new" :height 85)
caa63513 1367 (((type x)) :family "6x13" :foundry "trad" :height 130)
db10ce0a
MW
1368 (((type color)) :foreground "white" :background "black")
1369 (t nil))
1e7a9479
MW
1370(mdw-define-face fixed-pitch
1371 (((type w32)) :family "courier new" :height 85)
caa63513 1372 (((type x)) :family "6x13" :foundry "trad" :height 130)
1e7a9479 1373 (t :foreground "white" :background "black"))
e8ea88ba 1374(if (mdw-emacs-version-p 23)
c383eb8a
MW
1375 (mdw-define-face variable-pitch
1376 (((type x)) :family "sans" :height 100))
1377 (mdw-define-face variable-pitch
3f001ff6 1378 (((type x)) :family "helvetica" :height 90)))
1e7a9479 1379(mdw-define-face region
fefae026
MW
1380 (((min-colors 64)) :background "grey30")
1381 (((class color)) :background "blue")
4833e35c 1382 (t :inverse-video t))
fa156643 1383(mdw-define-face match
fefae026
MW
1384 (((class color)) :background "blue")
1385 (t :inverse-video t))
c6fe19d5 1386(mdw-define-face mc/cursor-face
fefae026
MW
1387 (((class color)) :background "red")
1388 (t :inverse-video t))
1e7a9479
MW
1389(mdw-define-face minibuffer-prompt
1390 (t :weight bold))
1391(mdw-define-face mode-line
db10ce0a
MW
1392 (((class color)) :foreground "blue" :background "yellow"
1393 :box (:line-width 1 :style released-button))
1394 (t :inverse-video t))
1e7a9479 1395(mdw-define-face mode-line-inactive
db10ce0a
MW
1396 (((class color)) :foreground "yellow" :background "blue"
1397 :box (:line-width 1 :style released-button))
1398 (t :inverse-video t))
ae0a853f
MW
1399(mdw-define-face nobreak-space
1400 (((type tty)))
1401 (t :inherit escape-glyph :underline t))
1e7a9479
MW
1402(mdw-define-face scroll-bar
1403 (t :foreground "black" :background "lightgrey"))
1404(mdw-define-face fringe
1405 (t :foreground "yellow"))
c383eb8a 1406(mdw-define-face show-paren-match
9cf75a93
MW
1407 (((min-colors 64)) :background "darkgreen")
1408 (((class color)) :background "green")
db10ce0a 1409 (t :underline t))
c383eb8a 1410(mdw-define-face show-paren-mismatch
db10ce0a
MW
1411 (((class color)) :background "red")
1412 (t :inverse-video t))
1e7a9479 1413(mdw-define-face highlight
fefae026
MW
1414 (((min-colors 64)) :background "DarkSeaGreen4")
1415 (((class color)) :background "cyan")
db10ce0a 1416 (t :inverse-video t))
1e7a9479
MW
1417
1418(mdw-define-face holiday-face
1419 (t :background "red"))
1420(mdw-define-face calendar-today-face
1421 (t :foreground "yellow" :weight bold))
1422
1423(mdw-define-face comint-highlight-prompt
1424 (t :weight bold))
5fd055c2
MW
1425(mdw-define-face comint-highlight-input
1426 (t nil))
1e7a9479 1427
2e97e639
MW
1428(mdw-define-face ido-subdir
1429 (t :foreground "cyan" :weight bold))
1430
e0e2aca3
MW
1431(mdw-define-face dired-directory
1432 (t :foreground "cyan" :weight bold))
1433(mdw-define-face dired-symlink
1434 (t :foreground "cyan"))
1435(mdw-define-face dired-perm-write
1436 (t nil))
1437
1e7a9479 1438(mdw-define-face trailing-whitespace
db10ce0a
MW
1439 (((class color)) :background "red")
1440 (t :inverse-video t))
33aa287b
MW
1441(mdw-define-face whitespace-line
1442 (((class color)) :background "darkred")
a52bc3ca 1443 (t :inverse-video t))
1e7a9479 1444(mdw-define-face mdw-punct-face
fefae026
MW
1445 (((min-colors 64)) :foreground "burlywood2")
1446 (((class color)) :foreground "yellow"))
1e7a9479
MW
1447(mdw-define-face mdw-number-face
1448 (t :foreground "yellow"))
52bcde59 1449(mdw-define-face mdw-trivial-face)
1e7a9479 1450(mdw-define-face font-lock-function-name-face
c383eb8a 1451 (t :slant italic))
1e7a9479
MW
1452(mdw-define-face font-lock-keyword-face
1453 (t :weight bold))
1454(mdw-define-face font-lock-constant-face
1455 (t :slant italic))
1456(mdw-define-face font-lock-builtin-face
1457 (t :weight bold))
07965a39
MW
1458(mdw-define-face font-lock-type-face
1459 (t :weight bold :slant italic))
1e7a9479
MW
1460(mdw-define-face font-lock-reference-face
1461 (t :weight bold))
1462(mdw-define-face font-lock-variable-name-face
1463 (t :slant italic))
1464(mdw-define-face font-lock-comment-delimiter-face
fefae026
MW
1465 (((min-colors 64)) :slant italic :foreground "SeaGreen1")
1466 (((class color)) :foreground "green")
1467 (t :weight bold))
1e7a9479 1468(mdw-define-face font-lock-comment-face
fefae026
MW
1469 (((min-colors 64)) :slant italic :foreground "SeaGreen1")
1470 (((class color)) :foreground "green")
1471 (t :weight bold))
1e7a9479 1472(mdw-define-face font-lock-string-face
fefae026
MW
1473 (((min-colors 64)) :foreground "SkyBlue1")
1474 (((class color)) :foreground "cyan")
1475 (t :weight bold))
898c7efb 1476
1e7a9479
MW
1477(mdw-define-face message-separator
1478 (t :background "red" :foreground "white" :weight bold))
1479(mdw-define-face message-cited-text
1480 (default :slant italic)
fefae026
MW
1481 (((min-colors 64)) :foreground "SkyBlue1")
1482 (((class color)) :foreground "cyan"))
1e7a9479 1483(mdw-define-face message-header-cc
4790fcb7 1484 (default :slant italic)
fefae026
MW
1485 (((min-colors 64)) :foreground "SeaGreen1")
1486 (((class color)) :foreground "green"))
1e7a9479 1487(mdw-define-face message-header-newsgroups
4790fcb7 1488 (default :slant italic)
fefae026
MW
1489 (((min-colors 64)) :foreground "SeaGreen1")
1490 (((class color)) :foreground "green"))
1e7a9479 1491(mdw-define-face message-header-subject
fefae026
MW
1492 (((min-colors 64)) :foreground "SeaGreen1")
1493 (((class color)) :foreground "green"))
1e7a9479 1494(mdw-define-face message-header-to
fefae026
MW
1495 (((min-colors 64)) :foreground "SeaGreen1")
1496 (((class color)) :foreground "green"))
1e7a9479 1497(mdw-define-face message-header-xheader
4790fcb7 1498 (default :slant italic)
fefae026
MW
1499 (((min-colors 64)) :foreground "SeaGreen1")
1500 (((class color)) :foreground "green"))
1e7a9479 1501(mdw-define-face message-header-other
4790fcb7 1502 (default :slant italic)
fefae026
MW
1503 (((min-colors 64)) :foreground "SeaGreen1")
1504 (((class color)) :foreground "green"))
1e7a9479 1505(mdw-define-face message-header-name
4790fcb7 1506 (default :weight bold)
fefae026
MW
1507 (((min-colors 64)) :foreground "SeaGreen1")
1508 (((class color)) :foreground "green"))
4790fcb7 1509
69498691
MW
1510(mdw-define-face which-func
1511 (t nil))
1e7a9479 1512
4790fcb7
MW
1513(mdw-define-face gnus-header-name
1514 (default :weight bold)
fefae026
MW
1515 (((min-colors 64)) :foreground "SeaGreen1")
1516 (((class color)) :foreground "green"))
4790fcb7 1517(mdw-define-face gnus-header-subject
fefae026
MW
1518 (((min-colors 64)) :foreground "SeaGreen1")
1519 (((class color)) :foreground "green"))
4790fcb7 1520(mdw-define-face gnus-header-from
fefae026
MW
1521 (((min-colors 64)) :foreground "SeaGreen1")
1522 (((class color)) :foreground "green"))
4790fcb7 1523(mdw-define-face gnus-header-to
fefae026
MW
1524 (((min-colors 64)) :foreground "SeaGreen1")
1525 (((class color)) :foreground "green"))
4790fcb7
MW
1526(mdw-define-face gnus-header-content
1527 (default :slant italic)
fefae026
MW
1528 (((min-colors 64)) :foreground "SeaGreen1")
1529 (((class color)) :foreground "green"))
4790fcb7
MW
1530
1531(mdw-define-face gnus-cite-1
fefae026
MW
1532 (((min-colors 64)) :foreground "SkyBlue1")
1533 (((class color)) :foreground "cyan"))
4790fcb7 1534(mdw-define-face gnus-cite-2
fefae026
MW
1535 (((min-colors 64)) :foreground "RoyalBlue2")
1536 (((class color)) :foreground "blue"))
4790fcb7 1537(mdw-define-face gnus-cite-3
fefae026
MW
1538 (((min-colors 64)) :foreground "MediumOrchid")
1539 (((class color)) :foreground "magenta"))
4790fcb7 1540(mdw-define-face gnus-cite-4
fefae026
MW
1541 (((min-colors 64)) :foreground "firebrick2")
1542 (((class color)) :foreground "red"))
4790fcb7 1543(mdw-define-face gnus-cite-5
fefae026
MW
1544 (((min-colors 64)) :foreground "burlywood2")
1545 (((class color)) :foreground "yellow"))
4790fcb7 1546(mdw-define-face gnus-cite-6
fefae026
MW
1547 (((min-colors 64)) :foreground "SeaGreen1")
1548 (((class color)) :foreground "green"))
4790fcb7 1549(mdw-define-face gnus-cite-7
fefae026
MW
1550 (((min-colors 64)) :foreground "SlateBlue1")
1551 (((class color)) :foreground "cyan"))
4790fcb7 1552(mdw-define-face gnus-cite-8
fefae026
MW
1553 (((min-colors 64)) :foreground "RoyalBlue2")
1554 (((class color)) :foreground "blue"))
4790fcb7 1555(mdw-define-face gnus-cite-9
fefae026
MW
1556 (((min-colors 64)) :foreground "purple2")
1557 (((class color)) :foreground "magenta"))
4790fcb7 1558(mdw-define-face gnus-cite-10
fefae026
MW
1559 (((min-colors 64)) :foreground "DarkOrange2")
1560 (((class color)) :foreground "red"))
4790fcb7
MW
1561(mdw-define-face gnus-cite-11
1562 (t :foreground "grey"))
1563
2f238de8
MW
1564(mdw-define-face diff-header
1565 (t nil))
1e7a9479
MW
1566(mdw-define-face diff-index
1567 (t :weight bold))
1568(mdw-define-face diff-file-header
1569 (t :weight bold))
1570(mdw-define-face diff-hunk-header
fefae026
MW
1571 (((min-colors 64)) :foreground "SkyBlue1")
1572 (((class color)) :foreground "cyan"))
1e7a9479 1573(mdw-define-face diff-function
fefae026
MW
1574 (default :weight bold)
1575 (((min-colors 64)) :foreground "SkyBlue1")
1576 (((class color)) :foreground "cyan"))
1e7a9479 1577(mdw-define-face diff-header
fefae026 1578 (((min-colors 64)) :background "grey10"))
1e7a9479 1579(mdw-define-face diff-added
fefae026 1580 (((class color)) :foreground "green"))
1e7a9479 1581(mdw-define-face diff-removed
fefae026 1582 (((class color)) :foreground "red"))
5fd055c2
MW
1583(mdw-define-face diff-context
1584 (t nil))
2f238de8 1585(mdw-define-face diff-refine-change
fefae026 1586 (((min-colors 64)) :background "RoyalBlue4")
b31f422b 1587 (t :underline t))
5f454d3e 1588(mdw-define-face diff-refine-removed
fefae026 1589 (((min-colors 64)) :background "#500")
5f454d3e
MW
1590 (t :underline t))
1591(mdw-define-face diff-refine-added
fefae026 1592 (((min-colors 64)) :background "#050")
5f454d3e 1593 (t :underline t))
1e7a9479 1594
a62d0541
MW
1595(setq ediff-force-faces t)
1596(mdw-define-face ediff-current-diff-A
fefae026
MW
1597 (((min-colors 64)) :background "darkred")
1598 (((class color)) :background "red")
a62d0541
MW
1599 (t :inverse-video t))
1600(mdw-define-face ediff-fine-diff-A
fefae026
MW
1601 (((min-colors 64)) :background "red3")
1602 (((class color)) :inverse-video t)
a62d0541
MW
1603 (t :inverse-video nil))
1604(mdw-define-face ediff-even-diff-A
fefae026 1605 (((min-colors 64)) :background "#300"))
a62d0541 1606(mdw-define-face ediff-odd-diff-A
fefae026 1607 (((min-colors 64)) :background "#300"))
a62d0541 1608(mdw-define-face ediff-current-diff-B
fefae026
MW
1609 (((min-colors 64)) :background "darkgreen")
1610 (((class color)) :background "magenta")
a62d0541
MW
1611 (t :inverse-video t))
1612(mdw-define-face ediff-fine-diff-B
fefae026
MW
1613 (((min-colors 64)) :background "green4")
1614 (((class color)) :inverse-video t)
a62d0541
MW
1615 (t :inverse-video nil))
1616(mdw-define-face ediff-even-diff-B
fefae026 1617 (((min-colors 64)) :background "#020"))
a62d0541 1618(mdw-define-face ediff-odd-diff-B
fefae026 1619 (((min-colors 64)) :background "#020"))
a62d0541 1620(mdw-define-face ediff-current-diff-C
fefae026
MW
1621 (((min-colors 64)) :background "darkblue")
1622 (((class color)) :background "blue")
a62d0541
MW
1623 (t :inverse-video t))
1624(mdw-define-face ediff-fine-diff-C
fefae026
MW
1625 (((min-colors 64)) :background "blue1")
1626 (((class color)) :inverse-video t)
a62d0541
MW
1627 (t :inverse-video nil))
1628(mdw-define-face ediff-even-diff-C
fefae026 1629 (((min-colors 64)) :background "#004"))
a62d0541 1630(mdw-define-face ediff-odd-diff-C
fefae026 1631 (((min-colors 64)) :background "#004"))
a62d0541 1632(mdw-define-face ediff-current-diff-Ancestor
fefae026
MW
1633 (((min-colors 64)) :background "#630")
1634 (((class color)) :background "blue")
a62d0541
MW
1635 (t :inverse-video t))
1636(mdw-define-face ediff-even-diff-Ancestor
fefae026 1637 (((min-colors 64)) :background "#320"))
a62d0541 1638(mdw-define-face ediff-odd-diff-Ancestor
fefae026 1639 (((min-colors 64)) :background "#320"))
a62d0541 1640
53f93f0d 1641(mdw-define-face magit-hash
fefae026
MW
1642 (((min-colors 64)) :foreground "grey40")
1643 (((class color)) :foreground "blue"))
53f93f0d 1644(mdw-define-face magit-diff-hunk-heading
fefae026
MW
1645 (((min-colors 64)) :foreground "grey70" :background "grey25")
1646 (((class color)) :foreground "yellow"))
53f93f0d 1647(mdw-define-face magit-diff-hunk-heading-highlight
fefae026
MW
1648 (((min-colors 64)) :foreground "grey70" :background "grey35")
1649 (((class color)) :foreground "yellow" :background "blue"))
53f93f0d 1650(mdw-define-face magit-diff-added
fefae026
MW
1651 (((min-colors 64)) :foreground "#ddffdd" :background "#335533")
1652 (((class color)) :foreground "green"))
53f93f0d 1653(mdw-define-face magit-diff-added-highlight
fefae026
MW
1654 (((min-colors 64)) :foreground "#cceecc" :background "#336633")
1655 (((class color)) :foreground "green" :background "blue"))
53f93f0d 1656(mdw-define-face magit-diff-removed
fefae026
MW
1657 (((min-colors 64)) :foreground "#ffdddd" :background "#553333")
1658 (((class color)) :foreground "red"))
53f93f0d 1659(mdw-define-face magit-diff-removed-highlight
fefae026
MW
1660 (((min-colors 64)) :foreground "#eecccc" :background "#663333")
1661 (((class color)) :foreground "red" :background "blue"))
857045c6
MW
1662(mdw-define-face magit-blame-heading
1663 (((min-colors 64)) :foreground "white" :background "grey25"
1664 :weight normal :slant normal)
1665 (((class color)) :foreground "white" :background "blue"
1666 :weight normal :slant normal))
1667(mdw-define-face magit-blame-name
1668 (t :inherit magit-blame-heading :slant italic))
1669(mdw-define-face magit-blame-date
1670 (((min-colors 64)) :inherit magit-blame-heading :foreground "grey60")
1671 (((class color)) :inherit magit-blame-heading :foreground "cyan"))
1672(mdw-define-face magit-blame-summary
1673 (t :inherit magit-blame-heading :weight bold))
53f93f0d 1674
ad305d7e 1675(mdw-define-face dylan-header-background
fefae026
MW
1676 (((min-colors 64)) :background "NavyBlue")
1677 (((class color)) :background "blue"))
ad305d7e 1678
e1b8de18
MW
1679(mdw-define-face erc-input-face
1680 (t :foreground "red"))
1681
1e7a9479
MW
1682(mdw-define-face woman-bold
1683 (t :weight bold))
1684(mdw-define-face woman-italic
1685 (t :slant italic))
1686
5a83259f
MW
1687(eval-after-load "rst"
1688 '(progn
1689 (mdw-define-face rst-level-1-face
1690 (t :foreground "SkyBlue1" :weight bold))
1691 (mdw-define-face rst-level-2-face
1692 (t :foreground "SeaGreen1" :weight bold))
1693 (mdw-define-face rst-level-3-face
1694 (t :weight bold))
1695 (mdw-define-face rst-level-4-face
1696 (t :slant italic))
1697 (mdw-define-face rst-level-5-face
1698 (t :underline t))
1699 (mdw-define-face rst-level-6-face
1700 ())))
4f251391 1701
1e7a9479
MW
1702(mdw-define-face p4-depot-added-face
1703 (t :foreground "green"))
1704(mdw-define-face p4-depot-branch-op-face
1705 (t :foreground "yellow"))
1706(mdw-define-face p4-depot-deleted-face
1707 (t :foreground "red"))
1708(mdw-define-face p4-depot-unmapped-face
1709 (t :foreground "SkyBlue1"))
1710(mdw-define-face p4-diff-change-face
1711 (t :foreground "yellow"))
1712(mdw-define-face p4-diff-del-face
1713 (t :foreground "red"))
1714(mdw-define-face p4-diff-file-face
1715 (t :foreground "SkyBlue1"))
1716(mdw-define-face p4-diff-head-face
1717 (t :background "grey10"))
1718(mdw-define-face p4-diff-ins-face
1719 (t :foreground "green"))
1720
4c39e530
MW
1721(mdw-define-face w3m-anchor-face
1722 (t :foreground "SkyBlue1" :underline t))
1723(mdw-define-face w3m-arrived-anchor-face
1724 (t :foreground "SkyBlue1" :underline t))
1725
1e7a9479
MW
1726(mdw-define-face whizzy-slice-face
1727 (t :background "grey10"))
1728(mdw-define-face whizzy-error-face
1729 (t :background "darkred"))
f617db13 1730
5fedb342
MW
1731;; Ellipses used to indicate hidden text (and similar).
1732(mdw-define-face mdw-ellipsis-face
1733 (((type tty)) :foreground "blue") (t :foreground "grey60"))
c11ac343 1734(let ((dollar (make-glyph-code ?$ 'mdw-ellipsis-face))
a8a7976a 1735 (backslash (make-glyph-code ?\\ 'mdw-ellipsis-face))
c11ac343
MW
1736 (dot (make-glyph-code ?. 'mdw-ellipsis-face))
1737 (bar (make-glyph-code ?| mdw-ellipsis-face)))
1738 (set-display-table-slot standard-display-table 0 dollar)
1739 (set-display-table-slot standard-display-table 1 backslash)
5fedb342 1740 (set-display-table-slot standard-display-table 4
c11ac343
MW
1741 (vector dot dot dot))
1742 (set-display-table-slot standard-display-table 5 bar))
5fedb342 1743
6132bc01 1744;;;--------------------------------------------------------------------------
c70e3179
MW
1745;;; Where is point?
1746
6a2d05ae 1747(mdw-define-face mdw-point-overlay-face
3f32879e 1748 (((type graphic)))
c70e3179
MW
1749 (((min-colors 64)) :background "darkblue")
1750 (((class color)) :background "blue")
1751 (((type tty) (class mono)) :inverse-video t))
1752
1753(defvar mdw-point-overlay-fringe-display '(vertical-bar . vertical-bar))
1754
1755(defun mdw-configure-point-overlay ()
1756 (let ((ov (make-overlay 0 0)))
1757 (overlay-put ov 'priority 0)
1758 (let* ((fringe (or mdw-point-overlay-fringe-display (cons nil nil)))
1759 (left (car fringe)) (right (cdr fringe))
1760 (s ""))
1761 (when left
1762 (let ((ss "."))
1763 (put-text-property 0 1 'display `(left-fringe ,left) ss)
1764 (setq s (concat s ss))))
1765 (when right
1766 (let ((ss "."))
1767 (put-text-property 0 1 'display `(right-fringe ,right) ss)
1768 (setq s (concat s ss))))
1769 (when (or left right)
1770 (overlay-put ov 'before-string s)))
6a2d05ae 1771 (overlay-put ov 'face 'mdw-point-overlay-face)
c70e3179
MW
1772 (delete-overlay ov)
1773 ov))
1774
1775(defvar mdw-point-overlay (mdw-configure-point-overlay)
1776 "An overlay used for showing where point is in the selected window.")
1777(defun mdw-reconfigure-point-overlay ()
1778 (interactive)
1779 (setq mdw-point-overlay (mdw-configure-point-overlay)))
1780
1781(defun mdw-remove-point-overlay ()
1782 "Remove the current-point overlay."
1783 (delete-overlay mdw-point-overlay))
1784
1785(defun mdw-update-point-overlay ()
1786 "Mark the current point position with an overlay."
1787 (if (not mdw-point-overlay-mode)
1788 (mdw-remove-point-overlay)
1789 (overlay-put mdw-point-overlay 'window (selected-window))
1790 (move-overlay mdw-point-overlay
1791 (line-beginning-position)
1792 (+ (line-end-position) 1))))
1793
1794(defvar mdw-point-overlay-buffers nil
1795 "List of buffers using `mdw-point-overlay-mode'.")
1796
1797(define-minor-mode mdw-point-overlay-mode
1798 "Indicate current line with an overlay."
1799 :global nil
1800 (let ((buffer (current-buffer)))
1801 (setq mdw-point-overlay-buffers
1802 (mapcan (lambda (buf)
1803 (if (and (buffer-live-p buf)
1804 (not (eq buf buffer)))
1805 (list buf)))
1806 mdw-point-overlay-buffers))
1807 (if mdw-point-overlay-mode
1808 (setq mdw-point-overlay-buffers
1809 (cons buffer mdw-point-overlay-buffers))))
1810 (cond (mdw-point-overlay-buffers
1811 (add-hook 'pre-command-hook 'mdw-remove-point-overlay)
1812 (add-hook 'post-command-hook 'mdw-update-point-overlay))
1813 (t
1814 (mdw-remove-point-overlay)
1815 (remove-hook 'pre-command-hook 'mdw-remove-point-overlay)
1816 (remove-hook 'post-command-hook 'mdw-update-point-overlay))))
1817
1818(define-globalized-minor-mode mdw-global-point-overlay-mode
1819 mdw-point-overlay-mode
1820 (lambda () (if (not (minibufferp)) (mdw-point-overlay-mode t))))
1821
1822;;;--------------------------------------------------------------------------
6132bc01 1823;;; C programming configuration.
f617db13 1824
6132bc01 1825;; Make C indentation nice.
f617db13 1826
f50c1bed
MW
1827(defun mdw-c-lineup-arglist (langelem)
1828 "Hack for DWIMmery in c-lineup-arglist."
1829 (if (save-excursion
1830 (c-block-in-arglist-dwim (c-langelem-2nd-pos c-syntactic-element)))
1831 0
1832 (c-lineup-arglist langelem)))
1833
1834(defun mdw-c-indent-extern-mumble (langelem)
1835 "Indent `extern \"...\" {' lines."
1836 (save-excursion
1837 (back-to-indentation)
1838 (if (looking-at
1839 "\\s-*\\<extern\\>\\s-*\"\\([^\\\\\"]+\\|\\.\\)*\"\\s-*{")
1840 c-basic-offset
1841 nil)))
1842
b521d36a
MW
1843(defun mdw-c-indent-arglist-nested (langelem)
1844 "Indent continued argument lists.
1845If we've nested more than one argument list, then only introduce a single
1846indentation anyway."
1847 (let ((context c-syntactic-context)
1848 (pos (c-langelem-2nd-pos c-syntactic-element))
1849 (should-indent-p t))
1850 (while (and context
1851 (eq (caar context) 'arglist-cont-nonempty))
1852 (when (and (= (caddr (pop context)) pos)
1853 context
1854 (memq (caar context) '(arglist-intro
1855 arglist-cont-nonempty)))
1856 (setq should-indent-p nil)))
1857 (if should-indent-p '+ 0)))
1858
c56296d0
MW
1859(defvar mdw-define-c-styles-hook nil
1860 "Hook run when `cc-mode' starts up to define styles.")
1861
1862(defmacro mdw-define-c-style (name &rest assocs)
1863 "Define a C style, called NAME (a symbol), setting ASSOCs.
1864A function, named `mdw-define-c-style/NAME', is defined to actually install
1865the style using `c-add-style', and added to the hook
1866`mdw-define-c-styles-hook'. If CC Mode is already loaded, then the style is
1867set."
1868 (declare (indent defun))
1869 (let* ((name-string (symbol-name name))
1870 (func (intern (concat "mdw-define-c-style/" name-string))))
1871 `(progn
1872 (defun ,func () (c-add-style ,name-string ',assocs))
1873 (and (featurep 'cc-mode) (,func))
1874 (add-hook 'mdw-define-c-styles-hook ',func))))
1875
1876(eval-after-load "cc-mode"
1877 '(run-hooks 'mdw-define-c-styles-hook))
1878
b521d36a
MW
1879(mdw-define-c-style mdw-trustonic-c
1880 (c-basic-offset . 4)
1881 (comment-column . 0)
1882 (c-indent-comment-alist (anchored-comment . (column . 0))
1883 (end-block . (space . 1))
1884 (cpp-end-block . (space . 1))
1885 (other . (space . 1)))
1886 (c-class-key . "class")
1887 (c-backslash-column . 0)
1888 (c-auto-align-backslashes . nil)
1889 (c-label-minimum-indentation . 0)
1890 (c-offsets-alist (substatement-open . (add 0 c-indent-one-line-block))
1891 (defun-open . (add 0 c-indent-one-line-block))
1892 (arglist-cont-nonempty . mdw-c-indent-arglist-nested)
1893 (topmost-intro . mdw-c-indent-extern-mumble)
1894 (cpp-define-intro . 0)
1895 (knr-argdecl . 0)
1896 (inextern-lang . [0])
1897 (label . 0)
1898 (case-label . +)
b07634df 1899 (access-label . -2)
b521d36a
MW
1900 (inclass . +)
1901 (inline-open . ++)
1902 (statement-cont . +)
1903 (statement-case-intro . +)))
1904
c56296d0
MW
1905(mdw-define-c-style mdw-c
1906 (c-basic-offset . 2)
1907 (comment-column . 40)
1908 (c-class-key . "class")
1909 (c-backslash-column . 72)
1910 (c-label-minimum-indentation . 0)
1911 (c-offsets-alist (substatement-open . (add 0 c-indent-one-line-block))
1912 (defun-open . (add 0 c-indent-one-line-block))
1913 (arglist-cont-nonempty . mdw-c-lineup-arglist)
1914 (topmost-intro . mdw-c-indent-extern-mumble)
1915 (cpp-define-intro . 0)
1916 (knr-argdecl . 0)
1917 (inextern-lang . [0])
1918 (label . 0)
1919 (case-label . +)
1920 (access-label . -)
1921 (inclass . +)
1922 (inline-open . ++)
1923 (statement-cont . +)
1924 (statement-case-intro . +)))
1925
1926(defun mdw-set-default-c-style (modes style)
1927 "Update the default CC Mode style for MODES to be STYLE.
1928
1929MODES may be a list of major mode names or a singleton. STYLE is a style
1930name, as a symbol."
1931 (let ((modes (if (listp modes) modes (list modes)))
1932 (style (symbol-name style)))
1933 (setq c-default-style
1934 (append (mapcar (lambda (mode)
1935 (cons mode style))
1936 modes)
1937 (remove-if (lambda (assoc)
1938 (memq (car assoc) modes))
1939 (if (listp c-default-style)
1940 c-default-style
1941 (list (cons 'other c-default-style))))))))
1942(setq c-default-style "mdw-c")
1943
1944(mdw-set-default-c-style '(c-mode c++-mode) 'mdw-c)
f617db13 1945
0e7d960b
MW
1946(defvar mdw-c-comment-fill-prefix
1947 `((,(concat "\\([ \t]*/?\\)"
a7474429 1948 "\\(\\*\\|//\\)"
0e7d960b
MW
1949 "\\([ \t]*\\)"
1950 "\\([A-Za-z]+:[ \t]*\\)?"
1951 mdw-hanging-indents)
1952 (pad . 1) (match . 2) (pad . 3) (pad . 4) (pad . 5)))
1953 "Fill prefix matching C comments (both kinds).")
1954
f617db13
MW
1955(defun mdw-fontify-c-and-c++ ()
1956
6132bc01 1957 ;; Fiddle with some syntax codes.
f617db13
MW
1958 (modify-syntax-entry ?* ". 23")
1959 (modify-syntax-entry ?/ ". 124b")
1960 (modify-syntax-entry ?\n "> b")
1961
6132bc01 1962 ;; Other stuff.
c56296d0 1963 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
f617db13 1964
6132bc01 1965 ;; Now define things to be fontified.
02109a0d 1966 (make-local-variable 'font-lock-keywords)
f617db13 1967 (let ((c-keywords
fe307a8c
MW
1968 (mdw-regexps "alignas" ;C11 macro, C++11
1969 "alignof" ;C++11
1970 "and" ;C++, C95 macro
0681f29e 1971 "and_eq" ;C++, C95 macro
7b84c078 1972 "asm" ;K&R, C++, GCC
fe307a8c 1973 "atomic" ;C11 macro, C++11 template type
26f18bd1 1974 "auto" ;K&R, C89
0681f29e
MW
1975 "bitand" ;C++, C95 macro
1976 "bitor" ;C++, C95 macro
d4783d9c 1977 "bool" ;C++, C99 macro
26f18bd1
MW
1978 "break" ;K&R, C89
1979 "case" ;K&R, C89
1980 "catch" ;C++
1981 "char" ;K&R, C89
fe307a8c
MW
1982 "char16_t" ;C++11, C11 library type
1983 "char32_t" ;C++11, C11 library type
26f18bd1 1984 "class" ;C++
d4783d9c 1985 "complex" ;C99 macro, C++ template type
0681f29e 1986 "compl" ;C++, C95 macro
26f18bd1 1987 "const" ;C89
fe307a8c 1988 "constexpr" ;C++11
26f18bd1
MW
1989 "const_cast" ;C++
1990 "continue" ;K&R, C89
fe307a8c 1991 "decltype" ;C++11
26f18bd1
MW
1992 "defined" ;C89 preprocessor
1993 "default" ;K&R, C89
1994 "delete" ;C++
1995 "do" ;K&R, C89
1996 "double" ;K&R, C89
1997 "dynamic_cast" ;C++
1998 "else" ;K&R, C89
1999 ;; "entry" ;K&R -- never used
2000 "enum" ;C89
2001 "explicit" ;C++
2002 "export" ;C++
2003 "extern" ;K&R, C89
2004 "float" ;K&R, C89
2005 "for" ;K&R, C89
2006 ;; "fortran" ;K&R
2007 "friend" ;C++
2008 "goto" ;K&R, C89
2009 "if" ;K&R, C89
d4783d9c
MW
2010 "imaginary" ;C99 macro
2011 "inline" ;C++, C99, GCC
26f18bd1
MW
2012 "int" ;K&R, C89
2013 "long" ;K&R, C89
2014 "mutable" ;C++
2015 "namespace" ;C++
2016 "new" ;C++
fe307a8c
MW
2017 "noexcept" ;C++11
2018 "noreturn" ;C11 macro
0681f29e
MW
2019 "not" ;C++, C95 macro
2020 "not_eq" ;C++, C95 macro
fe307a8c 2021 "nullptr" ;C++11
26f18bd1 2022 "operator" ;C++
0681f29e
MW
2023 "or" ;C++, C95 macro
2024 "or_eq" ;C++, C95 macro
26f18bd1
MW
2025 "private" ;C++
2026 "protected" ;C++
2027 "public" ;C++
2028 "register" ;K&R, C89
8d6d55b9 2029 "reinterpret_cast" ;C++
d4783d9c 2030 "restrict" ;C99
8d6d55b9
MW
2031 "return" ;K&R, C89
2032 "short" ;K&R, C89
2033 "signed" ;C89
2034 "sizeof" ;K&R, C89
2035 "static" ;K&R, C89
fe307a8c 2036 "static_assert" ;C11 macro, C++11
8d6d55b9
MW
2037 "static_cast" ;C++
2038 "struct" ;K&R, C89
2039 "switch" ;K&R, C89
2040 "template" ;C++
8d6d55b9 2041 "throw" ;C++
8d6d55b9 2042 "try" ;C++
fe307a8c 2043 "thread_local" ;C11 macro, C++11
8d6d55b9
MW
2044 "typedef" ;C89
2045 "typeid" ;C++
2046 "typeof" ;GCC
2047 "typename" ;C++
2048 "union" ;K&R, C89
2049 "unsigned" ;K&R, C89
2050 "using" ;C++
2051 "virtual" ;C++
2052 "void" ;C89
2053 "volatile" ;C89
2054 "wchar_t" ;C++, C89 library type
2055 "while" ;K&R, C89
0681f29e
MW
2056 "xor" ;C++, C95 macro
2057 "xor_eq" ;C++, C95 macro
fe307a8c
MW
2058 "_Alignas" ;C11
2059 "_Alignof" ;C11
2060 "_Atomic" ;C11
d4783d9c
MW
2061 "_Bool" ;C99
2062 "_Complex" ;C99
fe307a8c 2063 "_Generic" ;C11
d4783d9c 2064 "_Imaginary" ;C99
fe307a8c 2065 "_Noreturn" ;C11
d4783d9c 2066 "_Pragma" ;C99 preprocessor
fe307a8c
MW
2067 "_Static_assert" ;C11
2068 "_Thread_local" ;C11
8d6d55b9
MW
2069 "__alignof__" ;GCC
2070 "__asm__" ;GCC
2071 "__attribute__" ;GCC
2072 "__complex__" ;GCC
2073 "__const__" ;GCC
2074 "__extension__" ;GCC
2075 "__imag__" ;GCC
2076 "__inline__" ;GCC
2077 "__label__" ;GCC
2078 "__real__" ;GCC
2079 "__signed__" ;GCC
2080 "__typeof__" ;GCC
2081 "__volatile__" ;GCC
2082 ))
300f8827 2083 (c-builtins
26f18bd1 2084 (mdw-regexps "false" ;C++, C99 macro
165cecf8 2085 "this" ;C++
26f18bd1 2086 "true" ;C++, C99 macro
165cecf8 2087 ))
f617db13 2088 (preprocessor-keywords
8d6d55b9
MW
2089 (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
2090 "ident" "if" "ifdef" "ifndef" "import" "include"
2091 "line" "pragma" "unassert" "undef" "warning"))
f617db13 2092 (objc-keywords
8d6d55b9
MW
2093 (mdw-regexps "class" "defs" "encode" "end" "implementation"
2094 "interface" "private" "protected" "protocol" "public"
2095 "selector")))
f617db13
MW
2096
2097 (setq font-lock-keywords
2098 (list
f617db13 2099
6132bc01 2100 ;; Fontify include files as strings.
f617db13
MW
2101 (list (concat "^[ \t]*\\#[ \t]*"
2102 "\\(include\\|import\\)"
2103 "[ \t]*\\(<[^>]+\\(>\\|\\)\\)")
2104 '(2 font-lock-string-face))
2105
6132bc01 2106 ;; Preprocessor directives are `references'?.
f617db13
MW
2107 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
2108 preprocessor-keywords
2109 "\\)\\>\\|[0-9]+\\|$\\)\\)")
2110 '(1 font-lock-keyword-face))
2111
6132bc01 2112 ;; Handle the keywords defined above.
f617db13
MW
2113 (list (concat "@\\<\\(" objc-keywords "\\)\\>")
2114 '(0 font-lock-keyword-face))
2115
2116 (list (concat "\\<\\(" c-keywords "\\)\\>")
2117 '(0 font-lock-keyword-face))
2118
300f8827 2119 (list (concat "\\<\\(" c-builtins "\\)\\>")
165cecf8
MW
2120 '(0 font-lock-variable-name-face))
2121
6132bc01 2122 ;; Handle numbers too.
f617db13
MW
2123 ;;
2124 ;; This looks strange, I know. It corresponds to the
2125 ;; preprocessor's idea of what a number looks like, rather than
2126 ;; anything sensible.
f617db13
MW
2127 (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
2128 "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
2129 '(0 mdw-number-face))
2130
6132bc01 2131 ;; And anything else is punctuation.
f617db13 2132 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2e7c6a86 2133 '(0 mdw-punct-face))))))
f617db13 2134
fb16ed85
MW
2135(define-derived-mode sod-mode c-mode "Sod"
2136 "Major mode for editing Sod code.")
2137(push '("\\.sod$" . sod-mode) auto-mode-alist)
2138
6132bc01
MW
2139;;;--------------------------------------------------------------------------
2140;;; AP calc mode.
f617db13 2141
e7186cbe
MW
2142(define-derived-mode apcalc-mode c-mode "AP Calc"
2143 "Major mode for editing Calc code.")
f617db13
MW
2144
2145(defun mdw-fontify-apcalc ()
2146
6132bc01 2147 ;; Fiddle with some syntax codes.
f617db13
MW
2148 (modify-syntax-entry ?* ". 23")
2149 (modify-syntax-entry ?/ ". 14")
2150
6132bc01 2151 ;; Other stuff.
f617db13
MW
2152 (setq comment-start "/* ")
2153 (setq comment-end " */")
0e7d960b 2154 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
f617db13 2155
6132bc01 2156 ;; Now define things to be fontified.
02109a0d 2157 (make-local-variable 'font-lock-keywords)
f617db13 2158 (let ((c-keywords
8d6d55b9
MW
2159 (mdw-regexps "break" "case" "cd" "continue" "define" "default"
2160 "do" "else" "exit" "for" "global" "goto" "help" "if"
2161 "local" "mat" "obj" "print" "quit" "read" "return"
2162 "show" "static" "switch" "while" "write")))
f617db13
MW
2163
2164 (setq font-lock-keywords
2165 (list
f617db13 2166
6132bc01 2167 ;; Handle the keywords defined above.
f617db13
MW
2168 (list (concat "\\<\\(" c-keywords "\\)\\>")
2169 '(0 font-lock-keyword-face))
2170
6132bc01 2171 ;; Handle numbers too.
f617db13
MW
2172 ;;
2173 ;; This looks strange, I know. It corresponds to the
2174 ;; preprocessor's idea of what a number looks like, rather than
2175 ;; anything sensible.
f617db13
MW
2176 (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
2177 "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
2178 '(0 mdw-number-face))
2179
6132bc01 2180 ;; And anything else is punctuation.
f617db13 2181 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2e7c6a86 2182 '(0 mdw-punct-face))))))
f617db13 2183
6132bc01
MW
2184;;;--------------------------------------------------------------------------
2185;;; Java programming configuration.
f617db13 2186
6132bc01 2187;; Make indentation nice.
f617db13 2188
c56296d0
MW
2189(mdw-define-c-style mdw-java
2190 (c-basic-offset . 2)
2191 (c-backslash-column . 72)
2192 (c-offsets-alist (substatement-open . 0)
2193 (label . +)
2194 (case-label . +)
2195 (access-label . 0)
2196 (inclass . +)
2197 (statement-case-intro . +)))
2198(mdw-set-default-c-style 'java-mode 'mdw-java)
f617db13 2199
6132bc01 2200;; Declare Java fontification style.
f617db13
MW
2201
2202(defun mdw-fontify-java ()
2203
36eabf61
MW
2204 ;; Fiddle with some syntax codes.
2205 (modify-syntax-entry ?@ ".")
2206 (modify-syntax-entry ?@ "." font-lock-syntax-table)
2207
6132bc01 2208 ;; Other stuff.
0e7d960b 2209 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
f617db13 2210
6132bc01 2211 ;; Now define things to be fontified.
02109a0d 2212 (make-local-variable 'font-lock-keywords)
f617db13 2213 (let ((java-keywords
853d8555
MW
2214 (mdw-regexps "abstract" "assert"
2215 "boolean" "break" "byte"
2216 "case" "catch" "char" "class" "const" "continue"
2217 "default" "do" "double"
2218 "else" "enum" "extends"
2219 "final" "finally" "float" "for"
2220 "goto"
2221 "if" "implements" "import" "instanceof" "int"
2222 "interface"
2223 "long"
2224 "native" "new"
2225 "package" "private" "protected" "public"
2226 "return"
2227 "short" "static" "strictfp" "switch" "synchronized"
2228 "throw" "throws" "transient" "try"
2229 "void" "volatile"
2230 "while"))
8d6d55b9 2231
300f8827 2232 (java-builtins
165cecf8 2233 (mdw-regexps "false" "null" "super" "this" "true")))
f617db13
MW
2234
2235 (setq font-lock-keywords
2236 (list
f617db13 2237
6132bc01 2238 ;; Handle the keywords defined above.
f617db13
MW
2239 (list (concat "\\<\\(" java-keywords "\\)\\>")
2240 '(0 font-lock-keyword-face))
2241
300f8827
MW
2242 ;; Handle the magic builtins defined above.
2243 (list (concat "\\<\\(" java-builtins "\\)\\>")
165cecf8
MW
2244 '(0 font-lock-variable-name-face))
2245
6132bc01 2246 ;; Handle numbers too.
f617db13
MW
2247 ;;
2248 ;; The following isn't quite right, but it's close enough.
f617db13
MW
2249 (list (concat "\\<\\("
2250 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2251 "[0-9]+\\(\\.[0-9]*\\|\\)"
2252 "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
2253 "[lLfFdD]?")
2254 '(0 mdw-number-face))
2255
6132bc01 2256 ;; And anything else is punctuation.
f617db13 2257 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2e7c6a86 2258 '(0 mdw-punct-face))))))
f617db13 2259
6132bc01 2260;;;--------------------------------------------------------------------------
61d63206
MW
2261;;; Javascript programming configuration.
2262
2263(defun mdw-javascript-style ()
2264 (setq js-indent-level 2)
2265 (setq js-expr-indent-offset 0))
2266
2267(defun mdw-fontify-javascript ()
2268
2269 ;; Other stuff.
2270 (mdw-javascript-style)
2271 (setq js-auto-indent-flag t)
2272
2273 ;; Now define things to be fontified.
2274 (make-local-variable 'font-lock-keywords)
2275 (let ((javascript-keywords
2276 (mdw-regexps "abstract" "boolean" "break" "byte" "case" "catch"
2277 "char" "class" "const" "continue" "debugger" "default"
2278 "delete" "do" "double" "else" "enum" "export" "extends"
2279 "final" "finally" "float" "for" "function" "goto" "if"
2280 "implements" "import" "in" "instanceof" "int"
2281 "interface" "let" "long" "native" "new" "package"
2282 "private" "protected" "public" "return" "short"
2283 "static" "super" "switch" "synchronized" "throw"
2284 "throws" "transient" "try" "typeof" "var" "void"
4e23ea53 2285 "volatile" "while" "with" "yield"))
300f8827 2286 (javascript-builtins
61d63206
MW
2287 (mdw-regexps "false" "null" "undefined" "Infinity" "NaN" "true"
2288 "arguments" "this")))
2289
2290 (setq font-lock-keywords
2291 (list
2292
2293 ;; Handle the keywords defined above.
f7856acd 2294 (list (concat "\\_<\\(" javascript-keywords "\\)\\_>")
61d63206
MW
2295 '(0 font-lock-keyword-face))
2296
300f8827
MW
2297 ;; Handle the predefined builtins defined above.
2298 (list (concat "\\_<\\(" javascript-builtins "\\)\\_>")
61d63206
MW
2299 '(0 font-lock-variable-name-face))
2300
2301 ;; Handle numbers too.
2302 ;;
2303 ;; The following isn't quite right, but it's close enough.
f7856acd 2304 (list (concat "\\_<\\("
61d63206
MW
2305 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2306 "[0-9]+\\(\\.[0-9]*\\|\\)"
2307 "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
2308 "[lLfFdD]?")
2309 '(0 mdw-number-face))
2310
2311 ;; And anything else is punctuation.
2312 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2e7c6a86 2313 '(0 mdw-punct-face))))))
61d63206
MW
2314
2315;;;--------------------------------------------------------------------------
ee7c3dea
MW
2316;;; Scala programming configuration.
2317
2318(defun mdw-fontify-scala ()
2319
7b5903d8
MW
2320 ;; Comment filling.
2321 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2322
ee7c3dea
MW
2323 ;; Define things to be fontified.
2324 (make-local-variable 'font-lock-keywords)
2325 (let ((scala-keywords
2326 (mdw-regexps "abstract" "case" "catch" "class" "def" "do" "else"
2327 "extends" "final" "finally" "for" "forSome" "if"
2328 "implicit" "import" "lazy" "match" "new" "object"
3f017188
MW
2329 "override" "package" "private" "protected" "return"
2330 "sealed" "throw" "trait" "try" "type" "val"
ee7c3dea
MW
2331 "var" "while" "with" "yield"))
2332 (scala-constants
3f017188 2333 (mdw-regexps "false" "null" "super" "this" "true"))
7b5903d8 2334 (punctuation "[-!%^&*=+:@#~/?\\|`]"))
ee7c3dea
MW
2335
2336 (setq font-lock-keywords
2337 (list
2338
2339 ;; Magical identifiers between backticks.
2340 (list (concat "`\\([^`]+\\)`")
2341 '(1 font-lock-variable-name-face))
2342
2343 ;; Handle the keywords defined above.
2344 (list (concat "\\_<\\(" scala-keywords "\\)\\_>")
2345 '(0 font-lock-keyword-face))
2346
2347 ;; Handle the constants defined above.
2348 (list (concat "\\_<\\(" scala-constants "\\)\\_>")
2349 '(0 font-lock-variable-name-face))
2350
2351 ;; Magical identifiers between backticks.
2352 (list (concat "`\\([^`]+\\)`")
2353 '(1 font-lock-variable-name-face))
2354
2355 ;; Handle numbers too.
2356 ;;
2357 ;; As usual, not quite right.
2358 (list (concat "\\_<\\("
2359 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2360 "[0-9]+\\(\\.[0-9]*\\|\\)"
2361 "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
2362 "[lLfFdD]?")
2363 '(0 mdw-number-face))
2364
ee7c3dea
MW
2365 ;; And everything else is punctuation.
2366 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2367 '(0 mdw-punct-face)))
2368
2369 font-lock-syntactic-keywords
2370 (list
2371
2372 ;; Single quotes around characters. But not when used to quote
2373 ;; symbol names. Ugh.
2374 (list (concat "\\('\\)"
2375 "\\(" "."
2376 "\\|" "\\\\" "\\(" "\\\\\\\\" "\\)*"
2377 "u+" "[0-9a-fA-F]\\{4\\}"
2378 "\\|" "\\\\" "[0-7]\\{1,3\\}"
2379 "\\|" "\\\\" "." "\\)"
2380 "\\('\\)")
2381 '(1 "\"")
2e7c6a86 2382 '(4 "\""))))))
ee7c3dea
MW
2383
2384;;;--------------------------------------------------------------------------
6132bc01 2385;;; C# programming configuration.
e808c1e5 2386
6132bc01 2387;; Make indentation nice.
e808c1e5 2388
c56296d0
MW
2389(mdw-define-c-style mdw-csharp
2390 (c-basic-offset . 2)
2391 (c-backslash-column . 72)
2392 (c-offsets-alist (substatement-open . 0)
2393 (label . 0)
2394 (case-label . +)
2395 (access-label . 0)
2396 (inclass . +)
2397 (statement-case-intro . +)))
2398(mdw-set-default-c-style 'csharp-mode 'mdw-csharp)
e808c1e5 2399
6132bc01 2400;; Declare C# fontification style.
e808c1e5
MW
2401
2402(defun mdw-fontify-csharp ()
2403
6132bc01 2404 ;; Other stuff.
0e7d960b 2405 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
e808c1e5 2406
6132bc01 2407 ;; Now define things to be fontified.
e808c1e5
MW
2408 (make-local-variable 'font-lock-keywords)
2409 (let ((csharp-keywords
165cecf8
MW
2410 (mdw-regexps "abstract" "as" "bool" "break" "byte" "case" "catch"
2411 "char" "checked" "class" "const" "continue" "decimal"
2412 "default" "delegate" "do" "double" "else" "enum"
2413 "event" "explicit" "extern" "finally" "fixed" "float"
2414 "for" "foreach" "goto" "if" "implicit" "in" "int"
2415 "interface" "internal" "is" "lock" "long" "namespace"
2416 "new" "object" "operator" "out" "override" "params"
2417 "private" "protected" "public" "readonly" "ref"
2418 "return" "sbyte" "sealed" "short" "sizeof"
2419 "stackalloc" "static" "string" "struct" "switch"
2420 "throw" "try" "typeof" "uint" "ulong" "unchecked"
2421 "unsafe" "ushort" "using" "virtual" "void" "volatile"
2422 "while" "yield"))
2423
300f8827 2424 (csharp-builtins
165cecf8 2425 (mdw-regexps "base" "false" "null" "this" "true")))
e808c1e5
MW
2426
2427 (setq font-lock-keywords
2428 (list
e808c1e5 2429
6132bc01 2430 ;; Handle the keywords defined above.
e808c1e5
MW
2431 (list (concat "\\<\\(" csharp-keywords "\\)\\>")
2432 '(0 font-lock-keyword-face))
2433
300f8827
MW
2434 ;; Handle the magic builtins defined above.
2435 (list (concat "\\<\\(" csharp-builtins "\\)\\>")
165cecf8
MW
2436 '(0 font-lock-variable-name-face))
2437
6132bc01 2438 ;; Handle numbers too.
e808c1e5
MW
2439 ;;
2440 ;; The following isn't quite right, but it's close enough.
e808c1e5
MW
2441 (list (concat "\\<\\("
2442 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2443 "[0-9]+\\(\\.[0-9]*\\|\\)"
2444 "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
2445 "[lLfFdD]?")
2446 '(0 mdw-number-face))
2447
6132bc01 2448 ;; And anything else is punctuation.
e808c1e5 2449 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2e7c6a86 2450 '(0 mdw-punct-face))))))
e808c1e5 2451
103c5923
MW
2452(define-derived-mode csharp-mode java-mode "C#"
2453 "Major mode for editing C# code.")
e808c1e5 2454
6132bc01 2455;;;--------------------------------------------------------------------------
81fb08fc
MW
2456;;; F# programming configuration.
2457
2458(setq fsharp-indent-offset 2)
2459
2460(defun mdw-fontify-fsharp ()
2461
2462 (let ((punct "=<>+-*/|&%!@?"))
2463 (do ((i 0 (1+ i)))
2464 ((>= i (length punct)))
2465 (modify-syntax-entry (aref punct i) ".")))
2466
2467 (modify-syntax-entry ?_ "_")
2468 (modify-syntax-entry ?( "(")
2469 (modify-syntax-entry ?) ")")
2470
2471 (setq indent-tabs-mode nil)
2472
2473 (let ((fsharp-keywords
2474 (mdw-regexps "abstract" "and" "as" "assert" "atomic"
165cecf8 2475 "begin" "break"
81fb08fc
MW
2476 "checked" "class" "component" "const" "constraint"
2477 "constructor" "continue"
2478 "default" "delegate" "do" "done" "downcast" "downto"
2479 "eager" "elif" "else" "end" "exception" "extern"
165cecf8 2480 "finally" "fixed" "for" "fori" "fun" "function"
81fb08fc
MW
2481 "functor"
2482 "global"
2483 "if" "in" "include" "inherit" "inline" "interface"
2484 "internal"
2485 "lazy" "let"
2486 "match" "measure" "member" "method" "mixin" "module"
2487 "mutable"
165cecf8
MW
2488 "namespace" "new"
2489 "object" "of" "open" "or" "override"
81fb08fc
MW
2490 "parallel" "params" "private" "process" "protected"
2491 "public" "pure"
2492 "rec" "recursive" "return"
2493 "sealed" "sig" "static" "struct"
165cecf8 2494 "tailcall" "then" "to" "trait" "try" "type"
81fb08fc
MW
2495 "upcast" "use"
2496 "val" "virtual" "void" "volatile"
2497 "when" "while" "with"
2498 "yield"))
2499
2500 (fsharp-builtins
165cecf8
MW
2501 (mdw-regexps "asr" "land" "lor" "lsl" "lsr" "lxor" "mod"
2502 "base" "false" "null" "true"))
81fb08fc
MW
2503
2504 (bang-keywords
2505 (mdw-regexps "do" "let" "return" "use" "yield"))
2506
2507 (preprocessor-keywords
2508 (mdw-regexps "if" "indent" "else" "endif")))
2509
2510 (setq font-lock-keywords
2511 (list (list (concat "\\(^\\|[^\"]\\)"
2512 "\\(" "(\\*"
2513 "[^*]*\\*+"
2514 "\\(" "[^)*]" "[^*]*" "\\*+" "\\)*"
2515 ")"
2516 "\\|"
2517 "//.*"
2518 "\\)")
2519 '(2 font-lock-comment-face))
2520
2521 (list (concat "'" "\\("
2522 "\\\\"
2523 "\\(" "[ntbr'\\]"
2524 "\\|" "[0-9][0-9][0-9]"
2525 "\\|" "u" "[0-9a-fA-F]\\{4\\}"
2526 "\\|" "U" "[0-9a-fA-F]\\{8\\}"
2527 "\\)"
2528 "\\|"
2529 "." "\\)" "'"
2530 "\\|"
2531 "\"" "[^\"\\]*"
2532 "\\(" "\\\\" "\\(.\\|\n\\)"
2533 "[^\"\\]*" "\\)*"
2534 "\\(\"\\|\\'\\)")
2535 '(0 font-lock-string-face))
2536
2537 (list (concat "\\_<\\(" bang-keywords "\\)!" "\\|"
2538 "^#[ \t]*\\(" preprocessor-keywords "\\)\\_>"
2539 "\\|"
2540 "\\_<\\(" fsharp-keywords "\\)\\_>")
2541 '(0 font-lock-keyword-face))
2542 (list (concat "\\<\\(" fsharp-builtins "\\)\\_>")
2543 '(0 font-lock-variable-name-face))
2544
2545 (list (concat "\\_<"
2546 "\\(" "0[bB][01]+" "\\|"
2547 "0[oO][0-7]+" "\\|"
2548 "0[xX][0-9a-fA-F]+" "\\)"
2549 "\\(" "lf\\|LF" "\\|"
2550 "[uU]?[ysnlL]?" "\\)"
2551 "\\|"
2552 "\\_<"
2553 "[0-9]+" "\\("
2554 "[mMQRZING]"
2555 "\\|"
2556 "\\(\\.[0-9]*\\)?"
2557 "\\([eE][-+]?[0-9]+\\)?"
2558 "[fFmM]?"
2559 "\\|"
2560 "[uU]?[ysnlL]?"
2561 "\\)")
2562 '(0 mdw-number-face))
2563
2564 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2e7c6a86 2565 '(0 mdw-punct-face))))))
81fb08fc
MW
2566
2567(defun mdw-fontify-inferior-fsharp ()
2568 (mdw-fontify-fsharp)
2569 (setq font-lock-keywords
2570 (append (list (list "^[#-]" '(0 font-lock-comment-face))
2571 (list "^>" '(0 font-lock-keyword-face)))
2572 font-lock-keywords)))
2573
2574;;;--------------------------------------------------------------------------
07965a39
MW
2575;;; Go programming configuration.
2576
2577(defun mdw-fontify-go ()
2578
2579 (make-local-variable 'font-lock-keywords)
2580 (let ((go-keywords
2581 (mdw-regexps "break" "case" "chan" "const" "continue"
2582 "default" "defer" "else" "fallthrough" "for"
2583 "func" "go" "goto" "if" "import"
2584 "interface" "map" "package" "range" "return"
fc79ff88
MW
2585 "select" "struct" "switch" "type" "var"))
2586 (go-intrinsics
2587 (mdw-regexps "bool" "byte" "complex64" "complex128" "error"
2588 "float32" "float64" "int" "uint8" "int16" "int32"
2589 "int64" "rune" "string" "uint" "uint8" "uint16"
2590 "uint32" "uint64" "uintptr" "void"
2591 "false" "iota" "nil" "true"
2592 "init" "main"
2593 "append" "cap" "copy" "delete" "imag" "len" "make"
2594 "new" "panic" "real" "recover")))
07965a39
MW
2595
2596 (setq font-lock-keywords
2597 (list
2598
2599 ;; Handle the keywords defined above.
2600 (list (concat "\\<\\(" go-keywords "\\)\\>")
2601 '(0 font-lock-keyword-face))
fc79ff88
MW
2602 (list (concat "\\<\\(" go-intrinsics "\\)\\>")
2603 '(0 font-lock-variable-name-face))
07965a39 2604
cbbea94e
MW
2605 ;; Strings and characters.
2606 (list (concat "'"
2607 "\\(" "[^\\']" "\\|"
2608 "\\\\"
2609 "\\(" "[abfnrtv\\'\"]" "\\|"
2610 "[0-7]\\{3\\}" "\\|"
2611 "x" "[0-9A-Fa-f]\\{2\\}" "\\|"
2612 "u" "[0-9A-Fa-f]\\{4\\}" "\\|"
2613 "U" "[0-9A-Fa-f]\\{8\\}" "\\)" "\\)"
2614 "'"
2615 "\\|"
2616 "\""
2617 "\\(" "[^\n\\\"]+" "\\|" "\\\\." "\\)*"
2618 "\\(\"\\|$\\)"
2619 "\\|"
2620 "`" "[^`]+" "`")
2621 '(0 font-lock-string-face))
2622
07965a39
MW
2623 ;; Handle numbers too.
2624 ;;
2625 ;; The following isn't quite right, but it's close enough.
2626 (list (concat "\\<\\("
2627 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2628 "[0-9]+\\(\\.[0-9]*\\|\\)"
2629 "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)")
2630 '(0 mdw-number-face))
2631
2632 ;; And anything else is punctuation.
2633 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2e7c6a86 2634 '(0 mdw-punct-face))))))
07965a39
MW
2635
2636;;;--------------------------------------------------------------------------
36db1ea7
MW
2637;;; Rust programming configuration.
2638
2639(setq-default rust-indent-offset 2)
2640
2641(defun mdw-self-insert-and-indent (count)
2642 (interactive "p")
2643 (self-insert-command count)
2644 (indent-according-to-mode))
2645
2646(defun mdw-fontify-rust ()
2647
2648 ;; Hack syntax categories.
cbd69b16 2649 (modify-syntax-entry ?$ ".")
8e234929 2650 (modify-syntax-entry ?% ".")
36db1ea7
MW
2651 (modify-syntax-entry ?= ".")
2652
2653 ;; Fontify keywords and things.
2654 (make-local-variable 'font-lock-keywords)
2655 (let ((rust-keywords
87def30c 2656 (mdw-regexps "abstract" "alignof" "as" "async" "await"
36db1ea7 2657 "become" "box" "break"
260564a3 2658 "const" "continue" "crate"
87def30c 2659 "do" "dyn"
36db1ea7 2660 "else" "enum" "extern"
b6f44b18 2661 "final" "fn" "for"
36db1ea7
MW
2662 "if" "impl" "in"
2663 "let" "loop"
2664 "macro" "match" "mod" "move" "mut"
2665 "offsetof" "override"
b6f44b18 2666 "priv" "proc" "pub" "pure"
36db1ea7 2667 "ref" "return"
b6f44b18 2668 "sizeof" "static" "struct" "super"
87def30c
MW
2669 "trait" "try" "type" "typeof"
2670 "union" "unsafe" "unsized" "use"
36db1ea7
MW
2671 "virtual"
2672 "where" "while"
2673 "yield"))
2674 (rust-builtins
2675 (mdw-regexps "array" "pointer" "slice" "tuple"
2676 "bool" "true" "false"
2677 "f32" "f64"
2678 "i8" "i16" "i32" "i64" "isize"
2679 "u8" "u16" "u32" "u64" "usize"
b6f44b18
MW
2680 "char" "str"
2681 "self" "Self")))
36db1ea7
MW
2682 (setq font-lock-keywords
2683 (list
2684
2685 ;; Handle the keywords defined above.
d71a646d 2686 (list (concat "\\_<\\(" rust-keywords "\\)\\_>")
36db1ea7 2687 '(0 font-lock-keyword-face))
d71a646d 2688 (list (concat "\\_<\\(" rust-builtins "\\)\\_>")
36db1ea7
MW
2689 '(0 font-lock-variable-name-face))
2690
2691 ;; Handle numbers too.
d71a646d 2692 (list (concat "\\_<\\("
36db1ea7
MW
2693 "[0-9][0-9_]*"
2694 "\\(" "\\(\\.[0-9_]+\\)?[eE][-+]?[0-9_]+"
2695 "\\|" "\\.[0-9_]+"
2696 "\\)"
2697 "\\(f32\\|f64\\)?"
2698 "\\|" "\\(" "[0-9][0-9_]*"
2699 "\\|" "0x[0-9a-fA-F_]+"
2700 "\\|" "0o[0-7_]+"
2701 "\\|" "0b[01_]+"
2702 "\\)"
63b40831 2703 "\\([ui]\\(8\\|16\\|32\\|64\\|size\\)\\)?"
d71a646d 2704 "\\)\\_>")
36db1ea7
MW
2705 '(0 mdw-number-face))
2706
2707 ;; And anything else is punctuation.
2708 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2709 '(0 mdw-punct-face)))))
2710
2711 ;; Hack key bindings.
d2f85967 2712 (local-set-key [?{] 'mdw-self-insert-and-indent)
2e7c6a86 2713 (local-set-key [?}] 'mdw-self-insert-and-indent))
36db1ea7
MW
2714
2715;;;--------------------------------------------------------------------------
6132bc01 2716;;; Awk programming configuration.
f617db13 2717
6132bc01 2718;; Make Awk indentation nice.
f617db13 2719
c56296d0
MW
2720(mdw-define-c-style mdw-awk
2721 (c-basic-offset . 2)
2722 (c-offsets-alist (substatement-open . 0)
2723 (c-backslash-column . 72)
2724 (statement-cont . 0)
2725 (statement-case-intro . +)))
2726(mdw-set-default-c-style 'awk-mode 'mdw-awk)
f617db13 2727
6132bc01 2728;; Declare Awk fontification style.
f617db13
MW
2729
2730(defun mdw-fontify-awk ()
2731
6132bc01 2732 ;; Miscellaneous fiddling.
f617db13
MW
2733 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
2734
6132bc01 2735 ;; Now define things to be fontified.
02109a0d 2736 (make-local-variable 'font-lock-keywords)
f617db13 2737 (let ((c-keywords
8d6d55b9
MW
2738 (mdw-regexps "BEGIN" "END" "ARGC" "ARGIND" "ARGV" "CONVFMT"
2739 "ENVIRON" "ERRNO" "FIELDWIDTHS" "FILENAME" "FNR"
2740 "FS" "IGNORECASE" "NF" "NR" "OFMT" "OFS" "ORS" "RS"
2741 "RSTART" "RLENGTH" "RT" "SUBSEP"
2742 "atan2" "break" "close" "continue" "cos" "delete"
2743 "do" "else" "exit" "exp" "fflush" "file" "for" "func"
2744 "function" "gensub" "getline" "gsub" "if" "in"
2745 "index" "int" "length" "log" "match" "next" "rand"
2746 "return" "print" "printf" "sin" "split" "sprintf"
2747 "sqrt" "srand" "strftime" "sub" "substr" "system"
2748 "systime" "tolower" "toupper" "while")))
f617db13
MW
2749
2750 (setq font-lock-keywords
2751 (list
f617db13 2752
6132bc01 2753 ;; Handle the keywords defined above.
f617db13
MW
2754 (list (concat "\\<\\(" c-keywords "\\)\\>")
2755 '(0 font-lock-keyword-face))
2756
6132bc01 2757 ;; Handle numbers too.
f617db13
MW
2758 ;;
2759 ;; The following isn't quite right, but it's close enough.
f617db13
MW
2760 (list (concat "\\<\\("
2761 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2762 "[0-9]+\\(\\.[0-9]*\\|\\)"
2763 "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
2764 "[uUlL]*")
2765 '(0 mdw-number-face))
2766
6132bc01 2767 ;; And anything else is punctuation.
f617db13 2768 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2e7c6a86 2769 '(0 mdw-punct-face))))))
f617db13 2770
6132bc01
MW
2771;;;--------------------------------------------------------------------------
2772;;; Perl programming style.
f617db13 2773
6132bc01 2774;; Perl indentation style.
f617db13 2775
88158daf
MW
2776(setq perl-indent-level 2)
2777
f617db13
MW
2778(setq cperl-indent-level 2)
2779(setq cperl-continued-statement-offset 2)
2780(setq cperl-continued-brace-offset 0)
2781(setq cperl-brace-offset -2)
2782(setq cperl-brace-imaginary-offset 0)
2783(setq cperl-label-offset 0)
2784
6132bc01 2785;; Define perl fontification style.
f617db13
MW
2786
2787(defun mdw-fontify-perl ()
2788
6132bc01 2789 ;; Miscellaneous fiddling.
f617db13
MW
2790 (modify-syntax-entry ?$ "\\")
2791 (modify-syntax-entry ?$ "\\" font-lock-syntax-table)
a3b8176f 2792 (modify-syntax-entry ?: "." font-lock-syntax-table)
f617db13
MW
2793 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
2794
6132bc01 2795 ;; Now define fontification things.
02109a0d 2796 (make-local-variable 'font-lock-keywords)
f617db13 2797 (let ((perl-keywords
821c4945
MW
2798 (mdw-regexps "and"
2799 "break"
2800 "cmp" "continue"
2801 "default" "do"
2802 "else" "elsif" "eq"
2803 "for" "foreach"
2804 "ge" "given" "gt" "goto"
2805 "if"
2806 "last" "le" "local" "lt"
2807 "my"
2808 "ne" "next"
2809 "or" "our"
2810 "package"
2811 "redo" "require" "return"
2812 "sub"
2813 "undef" "unless" "until" "use"
2814 "when" "while")))
f617db13
MW
2815
2816 (setq font-lock-keywords
2817 (list
f617db13 2818
6132bc01 2819 ;; Set up the keywords defined above.
f617db13
MW
2820 (list (concat "\\<\\(" perl-keywords "\\)\\>")
2821 '(0 font-lock-keyword-face))
2822
6132bc01 2823 ;; At least numbers are simpler than C.
f617db13
MW
2824 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
2825 "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)"
2826 "\\([eE]\\([-+]\\|\\)[0-9_]+\\|\\)")
2827 '(0 mdw-number-face))
2828
6132bc01 2829 ;; And anything else is punctuation.
f617db13 2830 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2e7c6a86 2831 '(0 mdw-punct-face))))))
f617db13
MW
2832
2833(defun perl-number-tests (&optional arg)
2834 "Assign consecutive numbers to lines containing `#t'. With ARG,
2835strip numbers instead."
2836 (interactive "P")
2837 (save-excursion
2838 (goto-char (point-min))
2839 (let ((i 0) (fmt (if arg "" " %4d")))
2840 (while (search-forward "#t" nil t)
2841 (delete-region (point) (line-end-position))
2842 (setq i (1+ i))
2843 (insert (format fmt i)))
2844 (goto-char (point-min))
2845 (if (re-search-forward "\\(tests\\s-*=>\\s-*\\)\\w*" nil t)
2846 (replace-match (format "\\1%d" i))))))
2847
6132bc01
MW
2848;;;--------------------------------------------------------------------------
2849;;; Python programming style.
f617db13 2850
99fe6ef5 2851(defun mdw-fontify-pythonic (keywords)
f617db13 2852
6132bc01 2853 ;; Miscellaneous fiddling.
f617db13 2854 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
7c0fcfde 2855 (setq indent-tabs-mode nil)
f617db13 2856
6132bc01 2857 ;; Now define fontification things.
02109a0d 2858 (make-local-variable 'font-lock-keywords)
99fe6ef5
MW
2859 (setq font-lock-keywords
2860 (list
f617db13 2861
be2cc788 2862 ;; Set up the keywords defined above.
4b037109 2863 (list (concat "\\_<\\(" keywords "\\)\\_>")
99fe6ef5 2864 '(0 font-lock-keyword-face))
f617db13 2865
be2cc788 2866 ;; At least numbers are simpler than C.
b257436d 2867 (list (concat "\\_<0\\([xX][0-9a-fA-F]+\\|[oO]?[0-7]+\\|[bB][01]+\\)\\|"
b834ced3
MW
2868 "\\_<[0-9][0-9]*\\(\\.[0-9]*\\|\\)"
2869 "\\([eE]\\([-+]\\|\\)[0-9]+\\|[lL]\\|\\)")
99fe6ef5 2870 '(0 mdw-number-face))
f617db13 2871
be2cc788 2872 ;; And anything else is punctuation.
99fe6ef5 2873 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2e7c6a86 2874 '(0 mdw-punct-face)))))
99fe6ef5 2875
be2cc788 2876;; Define Python fontification styles.
99fe6ef5
MW
2877
2878(defun mdw-fontify-python ()
2879 (mdw-fontify-pythonic
2880 (mdw-regexps "and" "as" "assert" "break" "class" "continue" "def"
2881 "del" "elif" "else" "except" "exec" "finally" "for"
2882 "from" "global" "if" "import" "in" "is" "lambda"
2883 "not" "or" "pass" "print" "raise" "return" "try"
2884 "while" "with" "yield")))
2885
2886(defun mdw-fontify-pyrex ()
2887 (mdw-fontify-pythonic
2888 (mdw-regexps "and" "as" "assert" "break" "cdef" "class" "continue"
a63efb67 2889 "ctypedef" "def" "del" "elif" "else" "enum" "except" "exec"
99fe6ef5
MW
2890 "extern" "finally" "for" "from" "global" "if"
2891 "import" "in" "is" "lambda" "not" "or" "pass" "print"
a63efb67 2892 "property" "raise" "return" "struct" "try" "while" "with"
99fe6ef5 2893 "yield")))
f617db13 2894
b5263ae5
MW
2895(define-derived-mode pyrex-mode python-mode "Pyrex"
2896 "Major mode for editing Pyrex source code")
2897(setq auto-mode-alist
2898 (append '(("\\.pyx$" . pyrex-mode)
2899 ("\\.pxd$" . pyrex-mode)
2900 ("\\.pxi$" . pyrex-mode))
2901 auto-mode-alist))
2902
6132bc01 2903;;;--------------------------------------------------------------------------
772a7a3b
MW
2904;;; Lua programming style.
2905
2906(setq lua-indent-level 2)
2907
2908(defun mdw-fontify-lua ()
2909
2910 ;; Miscellaneous fiddling.
2911 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
2912
2913 ;; Now define fontification things.
2914 (make-local-variable 'font-lock-keywords)
2915 (let ((lua-keywords
2916 (mdw-regexps "and" "break" "do" "else" "elseif" "end"
2917 "false" "for" "function" "goto" "if" "in" "local"
2918 "nil" "not" "or" "repeat" "return" "then" "true"
2919 "until" "while")))
2920 (setq font-lock-keywords
2921 (list
2922
2923 ;; Set up the keywords defined above.
2924 (list (concat "\\_<\\(" lua-keywords "\\)\\_>")
2925 '(0 font-lock-keyword-face))
2926
2927 ;; At least numbers are simpler than C.
2928 (list (concat "\\_<\\(" "0[xX]"
2929 "\\(" "[0-9a-fA-F]+"
2930 "\\(\\.[0-9a-fA-F]*\\)?"
2931 "\\|" "\\.[0-9a-fA-F]+"
2932 "\\)"
2933 "\\([pP][-+]?[0-9]+\\)?"
2934 "\\|" "\\(" "[0-9]+"
2935 "\\(\\.[0-9]*\\)?"
2936 "\\|" "\\.[0-9]+"
2937 "\\)"
2938 "\\([eE][-+]?[0-9]+\\)?"
2939 "\\)")
2940 '(0 mdw-number-face))
2941
2942 ;; And anything else is punctuation.
2943 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2944 '(0 mdw-punct-face))))))
2945
2946;;;--------------------------------------------------------------------------
6132bc01 2947;;; Icon programming style.
cc1980e1 2948
6132bc01 2949;; Icon indentation style.
cc1980e1
MW
2950
2951(setq icon-brace-offset 0
2952 icon-continued-brace-offset 0
2953 icon-continued-statement-offset 2
2954 icon-indent-level 2)
2955
6132bc01 2956;; Define Icon fontification style.
cc1980e1
MW
2957
2958(defun mdw-fontify-icon ()
2959
6132bc01 2960 ;; Miscellaneous fiddling.
cc1980e1
MW
2961 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
2962
6132bc01 2963 ;; Now define fontification things.
cc1980e1
MW
2964 (make-local-variable 'font-lock-keywords)
2965 (let ((icon-keywords
2966 (mdw-regexps "break" "by" "case" "create" "default" "do" "else"
2967 "end" "every" "fail" "global" "if" "initial"
2968 "invocable" "link" "local" "next" "not" "of"
2969 "procedure" "record" "repeat" "return" "static"
2970 "suspend" "then" "to" "until" "while"))
2971 (preprocessor-keywords
2972 (mdw-regexps "define" "else" "endif" "error" "ifdef" "ifndef"
2973 "include" "line" "undef")))
2974 (setq font-lock-keywords
2975 (list
2976
6132bc01 2977 ;; Set up the keywords defined above.
cc1980e1
MW
2978 (list (concat "\\<\\(" icon-keywords "\\)\\>")
2979 '(0 font-lock-keyword-face))
2980
6132bc01 2981 ;; The things that Icon calls keywords.
cc1980e1
MW
2982 (list "&\\sw+\\>" '(0 font-lock-variable-name-face))
2983
6132bc01 2984 ;; At least numbers are simpler than C.
cc1980e1
MW
2985 (list (concat "\\<[0-9]+"
2986 "\\([rR][0-9a-zA-Z]+\\|"
2987 "\\.[0-9]+\\([eE][+-]?[0-9]+\\)?\\)\\>\\|"
2988 "\\.[0-9]+\\([eE][+-]?[0-9]+\\)?\\>")
2989 '(0 mdw-number-face))
2990
6132bc01 2991 ;; Preprocessor.
cc1980e1
MW
2992 (list (concat "^[ \t]*$[ \t]*\\<\\("
2993 preprocessor-keywords
2994 "\\)\\>")
2995 '(0 font-lock-keyword-face))
2996
6132bc01 2997 ;; And anything else is punctuation.
cc1980e1 2998 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2e7c6a86 2999 '(0 mdw-punct-face))))))
cc1980e1 3000
6132bc01 3001;;;--------------------------------------------------------------------------
6132bc01 3002;;; Assembler mode.
30c8a8fb
MW
3003
3004(defun mdw-fontify-asm ()
3005 (modify-syntax-entry ?' "\"")
3006 (modify-syntax-entry ?. "w")
9032280b 3007 (modify-syntax-entry ?\n ">")
30c8a8fb 3008 (setf fill-prefix nil)
5edd6d49
MW
3009 (modify-syntax-entry ?. "_")
3010 (modify-syntax-entry ?* ". 23")
3011 (modify-syntax-entry ?/ ". 124b")
3012 (modify-syntax-entry ?\n "> b")
b90c2a2c 3013 (local-set-key ";" 'self-insert-command)
30c8a8fb
MW
3014 (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)"))
3015
227b2b2b
MW
3016(defun mdw-asm-set-comment ()
3017 (modify-syntax-entry ?; "."
3018 )
5edd6d49 3019 (modify-syntax-entry asm-comment-char "< b")
227b2b2b
MW
3020 (setq comment-start (string asm-comment-char ? )))
3021(add-hook 'asm-mode-local-variables-hook 'mdw-asm-set-comment)
3022(put 'asm-comment-char 'safe-local-variable 'characterp)
9032280b 3023
6132bc01
MW
3024;;;--------------------------------------------------------------------------
3025;;; TCL configuration.
f617db13
MW
3026
3027(defun mdw-fontify-tcl ()
3028 (mapcar #'(lambda (ch) (modify-syntax-entry ch ".")) '(?$))
3029 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
02109a0d 3030 (make-local-variable 'font-lock-keywords)
f617db13
MW
3031 (setq font-lock-keywords
3032 (list
f617db13
MW
3033 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
3034 "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)"
3035 "\\([eE]\\([-+]\\|\\)[0-9_]+\\|\\)")
3036 '(0 mdw-number-face))
3037 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2e7c6a86 3038 '(0 mdw-punct-face)))))
f617db13 3039
6132bc01 3040;;;--------------------------------------------------------------------------
ad305d7e
MW
3041;;; Dylan programming configuration.
3042
3043(defun mdw-fontify-dylan ()
3044
3045 (make-local-variable 'font-lock-keywords)
3046
3047 ;; Horrors. `dylan-mode' sets the `major-mode' name after calling this
3048 ;; hook, which undoes all of our configuration.
3049 (setq major-mode 'dylan-mode)
3050 (font-lock-set-defaults)
3051
3052 (let* ((word "[-_a-zA-Z!*@<>$%]+")
3053 (dylan-keywords (mdw-regexps
3054
3055 "C-address" "C-callable-wrapper" "C-function"
3056 "C-mapped-subtype" "C-pointer-type" "C-struct"
3057 "C-subtype" "C-union" "C-variable"
3058
3059 "above" "abstract" "afterwards" "all"
3060 "begin" "below" "block" "by"
3061 "case" "class" "cleanup" "constant" "create"
3062 "define" "domain"
3063 "else" "elseif" "end" "exception" "export"
3064 "finally" "for" "from" "function"
3065 "generic"
3066 "handler"
3067 "if" "in" "instance" "interface" "iterate"
3068 "keyed-by"
3069 "let" "library" "local"
3070 "macro" "method" "module"
3071 "otherwise"
3072 "profiling"
3073 "select" "slot" "subclass"
3074 "table" "then" "to"
3075 "unless" "until" "use"
3076 "variable" "virtual"
3077 "when" "while"))
3078 (sharp-keywords (mdw-regexps
3079 "all-keys" "key" "next" "rest" "include"
3080 "t" "f")))
3081 (setq font-lock-keywords
3082 (list (list (concat "\\<\\(" dylan-keywords
ce29694e 3083 "\\|" "with\\(out\\)?-" word
ad305d7e
MW
3084 "\\)\\>")
3085 '(0 font-lock-keyword-face))
ce29694e
MW
3086 (list (concat "\\<" word ":" "\\|"
3087 "#\\(" sharp-keywords "\\)\\>")
ad305d7e
MW
3088 '(0 font-lock-variable-name-face))
3089 (list (concat "\\("
3090 "\\([-+]\\|\\<\\)[0-9]+" "\\("
3091 "\\(\\.[0-9]+\\)?" "\\([eE][-+][0-9]+\\)?"
3092 "\\|" "/[0-9]+"
3093 "\\)"
3094 "\\|" "\\.[0-9]+" "\\([eE][-+][0-9]+\\)?"
3095 "\\|" "#b[01]+"
3096 "\\|" "#o[0-7]+"
3097 "\\|" "#x[0-9a-zA-Z]+"
3098 "\\)\\>")
3099 '(0 mdw-number-face))
3100 (list (concat "\\("
3101 "\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\|"
3102 "\\_<[-+*/=<>:&|]+\\_>"
3103 "\\)")
2e7c6a86 3104 '(0 mdw-punct-face))))))
ad305d7e
MW
3105
3106;;;--------------------------------------------------------------------------
7fce54c3
MW
3107;;; Algol 68 configuration.
3108
3109(setq a68-indent-step 2)
3110
3111(defun mdw-fontify-algol-68 ()
3112
3113 ;; Fix up the syntax table.
3114 (modify-syntax-entry ?# "!" a68-mode-syntax-table)
3115 (dolist (ch '(?- ?+ ?= ?< ?> ?* ?/ ?| ?&))
3116 (modify-syntax-entry ch "." a68-mode-syntax-table))
3117
3118 (make-local-variable 'font-lock-keywords)
3119
3120 (let ((not-comment
3121 (let ((word "COMMENT"))
3122 (do ((regexp (concat "[^" (substring word 0 1) "]+")
3123 (concat regexp "\\|"
3124 (substring word 0 i)
3125 "[^" (substring word i (1+ i)) "]"))
3126 (i 1 (1+ i)))
3127 ((>= i (length word)) regexp)))))
3128 (setq font-lock-keywords
3129 (list (list (concat "\\<COMMENT\\>"
3130 "\\(" not-comment "\\)\\{0,5\\}"
3131 "\\(\\'\\|\\<COMMENT\\>\\)")
3132 '(0 font-lock-comment-face))
3133 (list (concat "\\<CO\\>"
3134 "\\([^C]+\\|C[^O]\\)\\{0,5\\}"
3135 "\\($\\|\\<CO\\>\\)")
3136 '(0 font-lock-comment-face))
3137 (list "\\<[A-Z_]+\\>"
3138 '(0 font-lock-keyword-face))
3139 (list (concat "\\<"
3140 "[0-9]+"
3141 "\\(\\.[0-9]+\\)?"
3142 "\\([eE][-+]?[0-9]+\\)?"
3143 "\\>")
3144 '(0 mdw-number-face))
3145 (list "\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/"
2e7c6a86 3146 '(0 mdw-punct-face))))))
7fce54c3
MW
3147
3148;;;--------------------------------------------------------------------------
6132bc01 3149;;; REXX configuration.
f617db13
MW
3150
3151(defun mdw-rexx-electric-* ()
3152 (interactive)
3153 (insert ?*)
3154 (rexx-indent-line))
3155
3156(defun mdw-rexx-indent-newline-indent ()
3157 (interactive)
3158 (rexx-indent-line)
3159 (if abbrev-mode (expand-abbrev))
3160 (newline-and-indent))
3161
3162(defun mdw-fontify-rexx ()
3163
6132bc01 3164 ;; Various bits of fiddling.
f617db13
MW
3165 (setq mdw-auto-indent nil)
3166 (local-set-key [?\C-m] 'mdw-rexx-indent-newline-indent)
3167 (local-set-key [?*] 'mdw-rexx-electric-*)
3168 (mapcar #'(lambda (ch) (modify-syntax-entry ch "w"))
e443a4cd 3169 '(?! ?? ?# ?@ ?$))
96f196d6
MW
3170 (mapcar #'(lambda (ch) (modify-syntax-entry ch "."))
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 3512;;;--------------------------------------------------------------------------
445ddb61
MW
3513;;; HTML, CSS, and other web foolishness.
3514
3515(setq css-indent-offset 2)
3516
3517;;;--------------------------------------------------------------------------
6132bc01 3518;;; SGML hacking.
f25cf300
MW
3519
3520(defun mdw-sgml-mode ()
3521 (interactive)
3522 (sgml-mode)
3523 (mdw-standard-fill-prefix "")
8a425bd7 3524 (make-local-variable 'sgml-delimiters)
f25cf300
MW
3525 (setq sgml-delimiters
3526 '("AND" "&" "COM" "--" "CRO" "&#" "DSC" "]" "DSO" "[" "DTGC" "]"
3527 "DTGO" "[" "ERO" "&" "ETAGO" ":e" "GRPC" ")" "GRPO" "(" "LIT" "\""
3528 "LITA" "'" "MDC" ">" "MDO" "<!" "MINUS" "-" "MSC" "]]" "NESTC" "{"
3529 "NET" "}" "OPT" "?" "OR" "|" "PERO" "%" "PIC" ">" "PIO" "<?"
3530 "PLUS" "+" "REFC" "." "REP" "*" "RNI" "#" "SEQ" "," "STAGO" ":"
3531 "TAGC" "." "VI" "=" "MS-START" "<![" "MS-END" "]]>"
3532 "XML-ECOM" "-->" "XML-PIC" "?>" "XML-SCOM" "<!--" "XML-TAGCE" "/>"
3533 "NULL" ""))
3534 (setq major-mode 'mdw-sgml-mode)
3535 (setq mode-name "[mdw] SGML")
3536 (run-hooks 'mdw-sgml-mode-hook))
6cb52f8b
MW
3537
3538;;;--------------------------------------------------------------------------
3539;;; Configuration files.
3540
3541(defvar mdw-conf-quote-normal nil
3542 "*Control syntax category of quote characters `\"' and `''.
3543If this is `t', consider quote characters to be normal
3544punctuation, as for `conf-quote-normal'. If this is `nil' then
3545leave quote characters as quotes. If this is a list, then
3546consider the quote characters in the list to be normal
3547punctuation. If this is a single quote character, then consider
3548that character only to be normal punctuation.")
3549(defun mdw-conf-quote-normal-acceptable-value-p (value)
3550 "Is the VALUE is an acceptable value for `mdw-conf-quote-normal'?"
3551 (or (booleanp value)
3552 (every (lambda (v) (memq v '(?\" ?')))
3553 (if (listp value) value (list value)))))
18bb0f77
MW
3554(put 'mdw-conf-quote-normal 'safe-local-variable
3555 'mdw-conf-quote-normal-acceptable-value-p)
6cb52f8b
MW
3556
3557(defun mdw-fix-up-quote ()
3558 "Apply the setting of `mdw-conf-quote-normal'."
3559 (let ((flag mdw-conf-quote-normal))
3560 (cond ((eq flag t)
3561 (conf-quote-normal t))
3562 ((not flag)
3563 nil)
3564 (t
3565 (let ((table (copy-syntax-table (syntax-table))))
3566 (mapc (lambda (ch) (modify-syntax-entry ch "." table))
3567 (if (listp flag) flag (list flag)))
3568 (set-syntax-table table)
3569 (and font-lock-mode (font-lock-fontify-buffer)))))))
18bb0f77 3570(add-hook 'conf-mode-local-variables-hook 'mdw-fix-up-quote t t)
f25cf300 3571
6132bc01
MW
3572;;;--------------------------------------------------------------------------
3573;;; Shell scripts.
f617db13
MW
3574
3575(defun mdw-setup-sh-script-mode ()
3576
6132bc01 3577 ;; Fetch the shell interpreter's name.
f617db13
MW
3578 (let ((shell-name sh-shell-file))
3579
6132bc01 3580 ;; Try reading the hash-bang line.
f617db13
MW
3581 (save-excursion
3582 (goto-char (point-min))
3583 (if (looking-at "#![ \t]*\\([^ \t\n]*\\)")
3584 (setq shell-name (match-string 1))))
3585
6132bc01 3586 ;; Now try to set the shell.
f617db13
MW
3587 ;;
3588 ;; Don't let `sh-set-shell' bugger up my script.
f617db13
MW
3589 (let ((executable-set-magic #'(lambda (s &rest r) s)))
3590 (sh-set-shell shell-name)))
3591
10c51541
MW
3592 ;; Don't insert here-document scaffolding automatically.
3593 (local-set-key "<" 'self-insert-command)
3594
6132bc01 3595 ;; Now enable my keys and the fontification.
f617db13
MW
3596 (mdw-misc-mode-config)
3597
6132bc01 3598 ;; Set the indentation level correctly.
f617db13
MW
3599 (setq sh-indentation 2)
3600 (setq sh-basic-offset 2))
3601
070c1dca
MW
3602(setq sh-shell-file "/bin/sh")
3603
6d6e095a
MW
3604;; Awful hacking to override the shell detection for particular scripts.
3605(defmacro define-custom-shell-mode (name shell)
3606 `(defun ,name ()
3607 (interactive)
3608 (set (make-local-variable 'sh-shell-file) ,shell)
3609 (sh-mode)))
3610(define-custom-shell-mode bash-mode "/bin/bash")
3611(define-custom-shell-mode rc-mode "/usr/bin/rc")
3612(put 'sh-shell-file 'permanent-local t)
3613
3614;; Hack the rc syntax table. Backquotes aren't paired in rc.
3615(eval-after-load "sh-script"
3616 '(or (assq 'rc sh-mode-syntax-table-input)
3617 (let ((frag '(nil
3618 ?# "<"
3619 ?\n ">#"
3620 ?\" "\"\""
3621 ?\' "\"\'"
3622 ?$ "'"
3623 ?\` "."
3624 ?! "_"
3625 ?% "_"
3626 ?. "_"
3627 ?^ "_"
3628 ?~ "_"
3629 ?, "_"
3630 ?= "."
3631 ?< "."
3632 ?> "."))
3633 (assoc (assq 'rc sh-mode-syntax-table-input)))
3634 (if assoc
3635 (rplacd assoc frag)
3636 (setq sh-mode-syntax-table-input
3637 (cons (cons 'rc frag)
3638 sh-mode-syntax-table-input))))))
3639
6132bc01 3640;;;--------------------------------------------------------------------------
092f0a38
MW
3641;;; Emacs shell mode.
3642
3643(defun mdw-eshell-prompt ()
3644 (let ((left "[") (right "]"))
3645 (when (= (user-uid) 0)
3646 (setq left "«" right "»"))
3647 (concat left
3648 (save-match-data
3649 (replace-regexp-in-string "\\..*$" "" (system-name)))
3650 " "
2d8b2924
MW
3651 (let* ((pwd (eshell/pwd)) (npwd (length pwd))
3652 (home (expand-file-name "~")) (nhome (length home)))
3653 (if (and (>= npwd nhome)
3654 (or (= nhome npwd)
5801e199
MW
3655 (= (elt pwd nhome) ?/))
3656 (string= (substring pwd 0 nhome) home))
2d8b2924
MW
3657 (concat "~" (substring pwd (length home)))
3658 pwd))
092f0a38
MW
3659 right)))
3660(setq eshell-prompt-function 'mdw-eshell-prompt)
ac4ae7cd 3661(setq eshell-prompt-regexp "^\\[[^]>]+\\(\\]\\|>>?\\)")
092f0a38 3662
2d8b2924
MW
3663(defun eshell/e (file) (find-file file) nil)
3664(defun eshell/ee (file) (find-file-other-window file) nil)
3665(defun eshell/w3m (url) (w3m-goto-url url) nil)
415a23dd 3666
092f0a38
MW
3667(mdw-define-face eshell-prompt (t :weight bold))
3668(mdw-define-face eshell-ls-archive (t :weight bold :foreground "red"))
3669(mdw-define-face eshell-ls-backup (t :foreground "lightgrey" :slant italic))
3670(mdw-define-face eshell-ls-product (t :foreground "lightgrey" :slant italic))
3671(mdw-define-face eshell-ls-clutter (t :foreground "lightgrey" :slant italic))
3672(mdw-define-face eshell-ls-executable (t :weight bold))
3673(mdw-define-face eshell-ls-directory (t :foreground "cyan" :weight bold))
3674(mdw-define-face eshell-ls-readonly (t nil))
3675(mdw-define-face eshell-ls-symlink (t :foreground "cyan"))
3676
b1a598dc 3677(defun mdw-eshell-hack () (setenv "LD_PRELOAD" nil))
8845865d
MW
3678(add-hook 'eshell-mode-hook 'mdw-eshell-hack)
3679
092f0a38 3680;;;--------------------------------------------------------------------------
6132bc01 3681;;; Messages-file mode.
f617db13 3682
4bb22eea 3683(defun messages-mode-guts ()
f617db13
MW
3684 (setq messages-mode-syntax-table (make-syntax-table))
3685 (set-syntax-table messages-mode-syntax-table)
f617db13
MW
3686 (modify-syntax-entry ?0 "w" messages-mode-syntax-table)
3687 (modify-syntax-entry ?1 "w" messages-mode-syntax-table)
3688 (modify-syntax-entry ?2 "w" messages-mode-syntax-table)
3689 (modify-syntax-entry ?3 "w" messages-mode-syntax-table)
3690 (modify-syntax-entry ?4 "w" messages-mode-syntax-table)
3691 (modify-syntax-entry ?5 "w" messages-mode-syntax-table)
3692 (modify-syntax-entry ?6 "w" messages-mode-syntax-table)
3693 (modify-syntax-entry ?7 "w" messages-mode-syntax-table)
3694 (modify-syntax-entry ?8 "w" messages-mode-syntax-table)
3695 (modify-syntax-entry ?9 "w" messages-mode-syntax-table)
3696 (make-local-variable 'comment-start)
3697 (make-local-variable 'comment-end)
3698 (make-local-variable 'indent-line-function)
3699 (setq indent-line-function 'indent-relative)
3700 (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
3701 (make-local-variable 'font-lock-defaults)
4bb22eea 3702 (make-local-variable 'messages-mode-keywords)
f617db13 3703 (let ((keywords
8d6d55b9
MW
3704 (mdw-regexps "array" "bitmap" "callback" "docs[ \t]+enum"
3705 "export" "enum" "fixed-octetstring" "flags"
3706 "harmless" "map" "nested" "optional"
3707 "optional-tagged" "package" "primitive"
3708 "primitive-nullfree" "relaxed[ \t]+enum"
3709 "set" "table" "tagged-optional" "union"
3710 "variadic" "vector" "version" "version-tag")))
4bb22eea 3711 (setq messages-mode-keywords
f617db13
MW
3712 (list
3713 (list (concat "\\<\\(" keywords "\\)\\>:")
3714 '(0 font-lock-keyword-face))
3715 '("\\([-a-zA-Z0-9]+:\\)" (0 font-lock-warning-face))
3716 '("\\(\\<[a-z][-_a-zA-Z0-9]*\\)"
3717 (0 font-lock-variable-name-face))
3718 '("\\<\\([0-9]+\\)\\>" (0 mdw-number-face))
3719 '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3720 (0 mdw-punct-face)))))
3721 (setq font-lock-defaults
4bb22eea 3722 '(messages-mode-keywords nil nil nil nil))
f617db13
MW
3723 (run-hooks 'messages-file-hook))
3724
3725(defun messages-mode ()
3726 (interactive)
3727 (fundamental-mode)
3728 (setq major-mode 'messages-mode)
3729 (setq mode-name "Messages")
4bb22eea 3730 (messages-mode-guts)
f617db13
MW
3731 (modify-syntax-entry ?# "<" messages-mode-syntax-table)
3732 (modify-syntax-entry ?\n ">" messages-mode-syntax-table)
3733 (setq comment-start "# ")
3734 (setq comment-end "")
f617db13
MW
3735 (run-hooks 'messages-mode-hook))
3736
3737(defun cpp-messages-mode ()
3738 (interactive)
3739 (fundamental-mode)
3740 (setq major-mode 'cpp-messages-mode)
3741 (setq mode-name "CPP Messages")
4bb22eea 3742 (messages-mode-guts)
f617db13
MW
3743 (modify-syntax-entry ?* ". 23" messages-mode-syntax-table)
3744 (modify-syntax-entry ?/ ". 14" messages-mode-syntax-table)
3745 (setq comment-start "/* ")
3746 (setq comment-end " */")
3747 (let ((preprocessor-keywords
8d6d55b9
MW
3748 (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
3749 "ident" "if" "ifdef" "ifndef" "import" "include"
3750 "line" "pragma" "unassert" "undef" "warning")))
4bb22eea 3751 (setq messages-mode-keywords
f617db13
MW
3752 (append (list (list (concat "^[ \t]*\\#[ \t]*"
3753 "\\(include\\|import\\)"
3754 "[ \t]*\\(<[^>]+\\(>\\|\\)\\)")
3755 '(2 font-lock-string-face))
3756 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
3757 preprocessor-keywords
852cd5fb 3758 "\\)\\>\\|[0-9]+\\|$\\)\\)")
f617db13 3759 '(1 font-lock-keyword-face)))
4bb22eea 3760 messages-mode-keywords)))
297d60aa 3761 (run-hooks 'cpp-messages-mode-hook))
f617db13 3762
297d60aa
MW
3763(add-hook 'messages-mode-hook 'mdw-misc-mode-config t)
3764(add-hook 'cpp-messages-mode-hook 'mdw-misc-mode-config t)
f617db13
MW
3765; (add-hook 'messages-file-hook 'mdw-fontify-messages t)
3766
6132bc01
MW
3767;;;--------------------------------------------------------------------------
3768;;; Messages-file mode.
f617db13
MW
3769
3770(defvar mallow-driver-substitution-face 'mallow-driver-substitution-face
3771 "Face to use for subsittution directives.")
3772(make-face 'mallow-driver-substitution-face)
3773(defvar mallow-driver-text-face 'mallow-driver-text-face
3774 "Face to use for body text.")
3775(make-face 'mallow-driver-text-face)
3776
3777(defun mallow-driver-mode ()
3778 (interactive)
3779 (fundamental-mode)
3780 (setq major-mode 'mallow-driver-mode)
3781 (setq mode-name "Mallow driver")
3782 (setq mallow-driver-mode-syntax-table (make-syntax-table))
3783 (set-syntax-table mallow-driver-mode-syntax-table)
3784 (make-local-variable 'comment-start)
3785 (make-local-variable 'comment-end)
3786 (make-local-variable 'indent-line-function)
3787 (setq indent-line-function 'indent-relative)
3788 (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
3789 (make-local-variable 'font-lock-defaults)
3790 (make-local-variable 'mallow-driver-mode-keywords)
3791 (let ((keywords
8d6d55b9
MW
3792 (mdw-regexps "each" "divert" "file" "if"
3793 "perl" "set" "string" "type" "write")))
f617db13
MW
3794 (setq mallow-driver-mode-keywords
3795 (list
3796 (list (concat "^%\\s *\\(}\\|\\(" keywords "\\)\\>\\).*$")
3797 '(0 font-lock-keyword-face))
3798 (list "^%\\s *\\(#.*\\|\\)$"
3799 '(0 font-lock-comment-face))
3800 (list "^%"
3801 '(0 font-lock-keyword-face))
3802 (list "^|?\\(.+\\)$" '(1 mallow-driver-text-face))
3803 (list "\\${[^}]*}"
3804 '(0 mallow-driver-substitution-face t)))))
3805 (setq font-lock-defaults
3806 '(mallow-driver-mode-keywords nil nil nil nil))
3807 (modify-syntax-entry ?\" "_" mallow-driver-mode-syntax-table)
3808 (modify-syntax-entry ?\n ">" mallow-driver-mode-syntax-table)
3809 (setq comment-start "%# ")
3810 (setq comment-end "")
f617db13
MW
3811 (run-hooks 'mallow-driver-mode-hook))
3812
3813(add-hook 'mallow-driver-hook 'mdw-misc-mode-config t)
3814
6132bc01
MW
3815;;;--------------------------------------------------------------------------
3816;;; NFast debugs.
f617db13
MW
3817
3818(defun nfast-debug-mode ()
3819 (interactive)
3820 (fundamental-mode)
3821 (setq major-mode 'nfast-debug-mode)
3822 (setq mode-name "NFast debug")
3823 (setq messages-mode-syntax-table (make-syntax-table))
3824 (set-syntax-table messages-mode-syntax-table)
3825 (make-local-variable 'font-lock-defaults)
3826 (make-local-variable 'nfast-debug-mode-keywords)
3827 (setq truncate-lines t)
3828 (setq nfast-debug-mode-keywords
3829 (list
3830 '("^\\(NFast_\\(Connect\\|Disconnect\\|Submit\\|Wait\\)\\)"
3831 (0 font-lock-keyword-face))
3832 (list (concat "^[ \t]+\\(\\("
3833 "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
3834 "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
3835 "[ \t]+\\)*"
3836 "[0-9a-fA-F]+\\)[ \t]*$")
3837 '(0 mdw-number-face))
3838 '("^[ \t]+\.status=[ \t]+\\<\\(OK\\)\\>"
3839 (1 font-lock-keyword-face))
3840 '("^[ \t]+\.status=[ \t]+\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>"
3841 (1 font-lock-warning-face))
3842 '("^[ \t]+\.status[ \t]+\\<\\(zero\\)\\>"
3843 (1 nil))
3844 (list (concat "^[ \t]+\\.cmd=[ \t]+"
3845 "\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>")
3846 '(1 font-lock-keyword-face))
3847 '("-?\\<\\([0-9]+\\|0x[0-9a-fA-F]+\\)\\>" (0 mdw-number-face))
3848 '("^\\([ \t]+[a-z0-9.]+\\)" (0 font-lock-variable-name-face))
3849 '("\\<\\([a-z][a-z0-9.]+\\)\\>=" (1 font-lock-variable-name-face))
3850 '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" (0 mdw-punct-face))))
3851 (setq font-lock-defaults
3852 '(nfast-debug-mode-keywords nil nil nil nil))
f617db13
MW
3853 (run-hooks 'nfast-debug-mode-hook))
3854
6132bc01
MW
3855;;;--------------------------------------------------------------------------
3856;;; Other languages.
f617db13 3857
6132bc01 3858;; Smalltalk.
f617db13
MW
3859
3860(defun mdw-setup-smalltalk ()
3861 (and mdw-auto-indent
3862 (local-set-key "\C-m" 'smalltalk-newline-and-indent))
8a425bd7 3863 (make-local-variable 'mdw-auto-indent)
f617db13
MW
3864 (setq mdw-auto-indent nil)
3865 (local-set-key "\C-i" 'smalltalk-reindent))
3866
3867(defun mdw-fontify-smalltalk ()
02109a0d 3868 (make-local-variable 'font-lock-keywords)
f617db13
MW
3869 (setq font-lock-keywords
3870 (list
f617db13
MW
3871 (list "\\<[A-Z][a-zA-Z0-9]*\\>"
3872 '(0 font-lock-keyword-face))
3873 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
3874 "[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)"
3875 "\\([eE]\\([-+]\\|\\)[0-9_]+\\|\\)")
3876 '(0 mdw-number-face))
3877 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2e7c6a86 3878 '(0 mdw-punct-face)))))
f617db13 3879
6132bc01 3880;; Lispy languages.
f617db13 3881
873d87df
MW
3882;; Unpleasant bodge.
3883(unless (boundp 'slime-repl-mode-map)
3884 (setq slime-repl-mode-map (make-sparse-keymap)))
3885
f617db13
MW
3886(defun mdw-indent-newline-and-indent ()
3887 (interactive)
3888 (indent-for-tab-command)
3889 (newline-and-indent))
3890
3891(eval-after-load "cl-indent"
3892 '(progn
3893 (mapc #'(lambda (pair)
3894 (put (car pair)
3895 'common-lisp-indent-function
3896 (cdr pair)))
3897 '((destructuring-bind . ((&whole 4 &rest 1) 4 &body))
3898 (multiple-value-bind . ((&whole 4 &rest 1) 4 &body))))))
3899
3900(defun mdw-common-lisp-indent ()
8a425bd7 3901 (make-local-variable 'lisp-indent-function)
f617db13
MW
3902 (setq lisp-indent-function 'common-lisp-indent-function))
3903
037be6de 3904(setq lisp-simple-loop-indentation 2
95575d1f
MW
3905 lisp-loop-keyword-indentation 6
3906 lisp-loop-forms-indentation 6)
3907
36cd5c10
MW
3908(defmacro mdw-advise-hyperspec-lookup (func args)
3909 `(defadvice ,func (around mdw-browse-w3m ,args activate compile)
3910 (if (fboundp 'w3m)
3911 (let ((browse-url-browser-function #'mdw-w3m-browse-url))
3912 ad-do-it)
3913 ad-do-it)))
3914(mdw-advise-hyperspec-lookup hyperspec-lookup (symbol))
3915(mdw-advise-hyperspec-lookup hyperspec-lookup-format (char))
3916(mdw-advise-hyperspec-lookup hyperspec-lookup-reader-macro (char))
3917
f617db13
MW
3918(defun mdw-fontify-lispy ()
3919
6132bc01 3920 ;; Set fill prefix.
f617db13
MW
3921 (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)")
3922
6132bc01 3923 ;; Not much fontification needed.
02109a0d 3924 (make-local-variable 'font-lock-keywords)
f617db13 3925 (setq font-lock-keywords
2287504f
MW
3926 (list (list (concat "\\("
3927 "\\_<[-+]?"
3928 "\\(" "[0-9]+/[0-9]+"
3929 "\\|" "\\(" "[0-9]+" "\\(\\.[0-9]*\\)?" "\\|"
3930 "\\.[0-9]+" "\\)"
3931 "\\([dDeEfFlLsS][-+]?[0-9]+\\)?"
3932 "\\)"
3933 "\\|"
3934 "#"
3935 "\\(" "x" "[-+]?"
3936 "[0-9A-Fa-f]+" "\\(/[0-9A-Fa-f]+\\)?"
3937 "\\|" "o" "[-+]?" "[0-7]+" "\\(/[0-7]+\\)?"
3938 "\\|" "b" "[-+]?" "[01]+" "\\(/[01]+\\)?"
3939 "\\|" "[0-9]+" "r" "[-+]?"
3940 "[0-9a-zA-Z]+" "\\(/[0-9a-zA-Z]+\\)?"
3941 "\\)"
3942 "\\)\\_>")
3943 '(0 mdw-number-face))
3944 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2e7c6a86 3945 '(0 mdw-punct-face)))))
f617db13
MW
3946
3947(defun comint-send-and-indent ()
3948 (interactive)
3949 (comint-send-input)
3950 (and mdw-auto-indent
3951 (indent-for-tab-command)))
3952
29e4917e
MW
3953(defadvice comint-line-beginning-position
3954 (around mdw-calculate-it-properly () activate compile)
3955 "Calculate the actual line start for multi-line input."
3956 (if (or comint-use-prompt-regexp
3957 (eq (field-at-pos (point)) 'output))
3958 ad-do-it
3959 (setq ad-return-value
3960 (constrain-to-field (line-beginning-position) (point)))))
3961
ec007bea 3962(defun mdw-setup-m4 ()
ed5d93a4
MW
3963
3964 ;; Inexplicably, Emacs doesn't match braces in m4 mode. This is very
3965 ;; annoying: fix it.
3966 (modify-syntax-entry ?{ "(")
3967 (modify-syntax-entry ?} ")")
3968
3969 ;; Fill prefix.
ec007bea
MW
3970 (mdw-standard-fill-prefix "\\([ \t]*\\(?:#+\\|\\<dnl\\>\\)[ \t]*\\)"))
3971
6132bc01
MW
3972;;;--------------------------------------------------------------------------
3973;;; Text mode.
f617db13
MW
3974
3975(defun mdw-text-mode ()
3976 (setq fill-column 72)
3977 (flyspell-mode t)
3978 (mdw-standard-fill-prefix
c7a8da49 3979 "\\([ \t]*\\([>#|:] ?\\)*[ \t]*\\)" 3)
f617db13
MW
3980 (auto-fill-mode 1))
3981
060c23ce
MW
3982(eval-after-load "flyspell"
3983 '(define-key flyspell-mode-map "\C-\M-i" nil))
3984
6132bc01 3985;;;--------------------------------------------------------------------------
faf2cef7 3986;;; Outline and hide/show modes.
5de5db48
MW
3987
3988(defun mdw-outline-collapse-all ()
3989 "Completely collapse everything in the entire buffer."
3990 (interactive)
3991 (save-excursion
3992 (goto-char (point-min))
3993 (while (< (point) (point-max))
3994 (hide-subtree)
3995 (forward-line))))
3996
faf2cef7
MW
3997(setq hs-hide-comments-when-hiding-all nil)
3998
b200af26 3999(defadvice hs-hide-all (after hide-first-comment activate)
941c29ba 4000 (save-excursion (hs-hide-initial-comment-block)))
b200af26 4001
6132bc01
MW
4002;;;--------------------------------------------------------------------------
4003;;; Shell mode.
f617db13
MW
4004
4005(defun mdw-sh-mode-setup ()
4006 (local-set-key [?\C-a] 'comint-bol)
4007 (add-hook 'comint-output-filter-functions
4008 'comint-watch-for-password-prompt))
4009
4010(defun mdw-term-mode-setup ()
3d9147ea 4011 (setq term-prompt-regexp shell-prompt-pattern)
f617db13
MW
4012 (make-local-variable 'mouse-yank-at-point)
4013 (make-local-variable 'transient-mark-mode)
4014 (setq mouse-yank-at-point t)
f617db13
MW
4015 (auto-fill-mode -1)
4016 (setq tab-width 8))
4017
3d9147ea
MW
4018(defun term-send-meta-right () (interactive) (term-send-raw-string "\e\e[C"))
4019(defun term-send-meta-left () (interactive) (term-send-raw-string "\e\e[D"))
4020(defun term-send-ctrl-uscore () (interactive) (term-send-raw-string "\C-_"))
4021(defun term-send-meta-meta-something ()
4022 (interactive)
4023 (term-send-raw-string "\e\e")
4024 (term-send-raw))
4025(eval-after-load 'term
4026 '(progn
4027 (define-key term-raw-map [?\e ?\e] nil)
4028 (define-key term-raw-map [?\e ?\e t] 'term-send-meta-meta-something)
4029 (define-key term-raw-map [?\C-/] 'term-send-ctrl-uscore)
4030 (define-key term-raw-map [M-right] 'term-send-meta-right)
4031 (define-key term-raw-map [?\e ?\M-O ?C] 'term-send-meta-right)
4032 (define-key term-raw-map [M-left] 'term-send-meta-left)
4033 (define-key term-raw-map [?\e ?\M-O ?D] 'term-send-meta-left)))
4034
c4434c20
MW
4035(defadvice term-exec (before program-args-list compile activate)
4036 "If the PROGRAM argument is a list, interpret it as (PROGRAM . SWITCHES).
4037This allows you to pass a list of arguments through `ansi-term'."
4038 (let ((program (ad-get-arg 2)))
4039 (if (listp program)
4040 (progn
4041 (ad-set-arg 2 (car program))
4042 (ad-set-arg 4 (cdr program))))))
4043
8845865d
MW
4044(defadvice term-exec-1 (around hack-environment compile activate)
4045 "Hack the environment inherited by inferiors in the terminal."
f8592fee 4046 (let ((process-environment (copy-tree process-environment)))
8845865d
MW
4047 (setenv "LD_PRELOAD" nil)
4048 ad-do-it))
4049
4050(defadvice shell (around hack-environment compile activate)
4051 "Hack the environment inherited by inferiors in the shell."
f8592fee 4052 (let ((process-environment (copy-tree process-environment)))
8845865d
MW
4053 (setenv "LD_PRELOAD" nil)
4054 ad-do-it))
4055
c4434c20
MW
4056(defun ssh (host)
4057 "Open a terminal containing an ssh session to the HOST."
4058 (interactive "sHost: ")
4059 (ansi-term (list "ssh" host) (format "ssh@%s" host)))
4060
5aa1b95f 4061(defvar git-grep-command
20b6cd68 4062 "env GIT_PAGER=cat git grep --no-color -nH -e "
5aa1b95f
MW
4063 "*The default command for \\[git-grep].")
4064
4065(defvar git-grep-history nil)
4066
4067(defun git-grep (command-args)
4068 "Run `git grep' with user-specified args and collect output in a buffer."
4069 (interactive
4070 (list (read-shell-command "Run git grep (like this): "
4071 git-grep-command 'git-grep-history)))
6a0a9a51
MW
4072 (let ((grep-use-null-device nil))
4073 (grep command-args)))
5aa1b95f 4074
c63bce81
MW
4075;;;--------------------------------------------------------------------------
4076;;; Magit configuration.
4077
c14a5ec3
MW
4078(setq magit-diff-refine-hunk 'all
4079 magit-view-git-manual-method 'man
83d2acdd 4080 magit-log-margin '(nil age magit-log-margin-width t 18)
c14a5ec3
MW
4081 magit-wip-after-save-local-mode-lighter ""
4082 magit-wip-after-apply-mode-lighter ""
4083 magit-wip-before-change-mode-lighter "")
4084(eval-after-load "magit"
4085 '(progn (global-magit-file-mode 1)
4086 (magit-wip-after-save-mode 1)
4087 (magit-wip-after-apply-mode 1)
4088 (magit-wip-before-change-mode 1)
60c22e1b 4089 (add-to-list 'magit-no-confirm 'safe-with-wip)
2a67803a 4090 (add-to-list 'magit-no-confirm 'trash)
87746eb7
MW
4091 (push '(:eval (if (or magit-wip-after-save-local-mode
4092 magit-wip-after-apply-mode
4093 magit-wip-before-change-mode)
4094 (format " wip:%s%s%s"
4095 (if magit-wip-after-apply-mode "A" "")
4096 (if magit-wip-before-change-mode "C" "")
4097 (if magit-wip-after-save-local-mode "S" ""))))
4098 minor-mode-alist)
60c22e1b
MW
4099 (dolist (popup '(magit-diff-popup
4100 magit-diff-refresh-popup
4101 magit-diff-mode-refresh-popup
4102 magit-revision-mode-refresh-popup))
4103 (magit-define-popup-switch popup ?R "Reverse diff" "-R"))))
c14a5ec3 4104
28509f06
MW
4105(defadvice magit-wip-commit-buffer-file
4106 (around mdw-just-this-buffer activate compile)
4107 (let ((magit-save-repository-buffers nil)) ad-do-it))
4108
2a67803a
MW
4109(defadvice magit-discard
4110 (around mdw-delete-if-prefix-argument activate compile)
4111 (let ((magit-delete-by-moving-to-trash
4112 (and (null current-prefix-arg)
4113 magit-delete-by-moving-to-trash)))
4114 ad-do-it))
4115
ff6a7bee
MW
4116(setq magit-repolist-columns
4117 '(("Name" 16 magit-repolist-column-ident nil)
4118 ("Version" 18 magit-repolist-column-version nil)
4119 ("St" 2 magit-repolist-column-dirty nil)
4120 ("L<U" 3 mdw-repolist-column-unpulled-from-upstream nil)
4121 ("L>U" 3 mdw-repolist-column-unpushed-to-upstream nil)
4122 ("Path" 32 magit-repolist-column-path nil)))
4123
4124(setq magit-repository-directories '(("~/etc/profile" . 0)
4125 ("~/src/" . 1)))
4126
4127(defadvice magit-list-repos (around mdw-dirname () activate compile)
4128 "Make sure the returned names are directory names.
4129Otherwise child processes get started in the wrong directory and
4130there is sadness."
4131 (setq ad-return-value (mapcar #'file-name-as-directory ad-do-it)))
4132
4133(defun mdw-repolist-column-unpulled-from-upstream (_id)
4134 "Insert number of upstream commits not in the current branch."
4135 (let ((upstream (magit-get-upstream-branch (magit-get-current-branch) t)))
4136 (and upstream
4137 (let ((n (cadr (magit-rev-diff-count "HEAD" upstream))))
4138 (propertize (number-to-string n) 'face
4139 (if (> n 0) 'bold 'shadow))))))
4140
4141(defun mdw-repolist-column-unpushed-to-upstream (_id)
4142 "Insert number of commits in the current branch but not its upstream."
4143 (let ((upstream (magit-get-upstream-branch (magit-get-current-branch) t)))
4144 (and upstream
4145 (let ((n (car (magit-rev-diff-count "HEAD" upstream))))
4146 (propertize (number-to-string n) 'face
4147 (if (> n 0) 'bold 'shadow))))))
4148
5d824e2f
MW
4149(defun mdw-try-smerge ()
4150 (save-excursion
4151 (goto-char (point-min))
4152 (when (re-search-forward "^<<<<<<< " nil t)
4153 (smerge-mode 1))))
4154(add-hook 'find-file-hook 'mdw-try-smerge t)
4155
e07e3320 4156;;;--------------------------------------------------------------------------
e48c2e5b
MW
4157;;; GUD, and especially GDB.
4158
4159;; Inhibit window dedication. I mean, seriously, wtf?
4160(defadvice gdb-display-buffer (after mdw-undedicated (buf) compile activate)
4161 "Don't make windows dedicated. Seriously."
4162 (set-window-dedicated-p ad-return-value nil))
4163(defadvice gdb-set-window-buffer
4164 (after mdw-undedicated (name &optional ignore-dedicated window)
4165 compile activate)
4166 "Don't make windows dedicated. Seriously."
4167 (set-window-dedicated-p (or window (selected-window)) nil))
4168
4169;;;--------------------------------------------------------------------------
0f81a131
MW
4170;;; MPC configuration.
4171
50a77b30
MW
4172(eval-when-compile (trap (require 'mpc)))
4173
0f81a131
MW
4174(setq mpc-browser-tags '(Artist|Composer|Performer Album|Playlist))
4175
4176(defun mdw-mpc-now-playing ()
4177 (interactive)
4178 (require 'mpc)
4179 (save-excursion
4180 (set-buffer (mpc-proc-cmd (mpc-proc-cmd-list '("status" "currentsong"))))
4181 (mpc--status-callback))
4182 (let ((state (cdr (assq 'state mpc-status))))
4183 (cond ((member state '("stop"))
4184 (message "mpd stopped."))
4185 ((member state '("play" "pause"))
4186 (let* ((artist (cdr (assq 'Artist mpc-status)))
4187 (album (cdr (assq 'Album mpc-status)))
4188 (title (cdr (assq 'Title mpc-status)))
4189 (file (cdr (assq 'file mpc-status)))
4190 (duration-string (cdr (assq 'Time mpc-status)))
4191 (time-string (cdr (assq 'time mpc-status)))
4192 (time (and time-string
355d1336 4193 (string-to-number
0f81a131
MW
4194 (if (string-match ":" time-string)
4195 (substring time-string
4196 0 (match-beginning 0))
4197 (time-string)))))
4198 (duration (and duration-string
355d1336 4199 (string-to-number duration-string)))
0f81a131
MW
4200 (pos (and time duration
4201 (format " [%d:%02d/%d:%02d]"
4202 (/ time 60) (mod time 60)
4203 (/ duration 60) (mod duration 60))))
4204 (fmt (cond ((and artist title)
4205 (format "`%s' by %s%s" title artist
4206 (if album (format ", from `%s'" album)
4207 "")))
4208 (file
4209 (format "`%s' (no tags)" file))
4210 (t
4211 "(no idea what's playing!)"))))
4212 (if (string= state "play")
4213 (message "mpd playing %s%s" fmt (or pos ""))
4214 (message "mpd paused in %s%s" fmt (or pos "")))))
4215 (t
4216 (message "mpd in unknown state `%s'" state)))))
4217
4aba12fa
MW
4218(defmacro mdw-define-mpc-wrapper (func bvl interactive &rest body)
4219 `(defun ,func ,bvl
4220 (interactive ,@interactive)
4221 (require 'mpc)
4222 ,@body
4223 (mdw-mpc-now-playing)))
4224
4225(mdw-define-mpc-wrapper mdw-mpc-play-or-pause () nil
4226 (if (member (cdr (assq 'state (mpc-cmd-status))) '("play"))
4227 (mpc-pause)
4228 (mpc-play)))
4229
4230(mdw-define-mpc-wrapper mdw-mpc-next () nil (mpc-next))
4231(mdw-define-mpc-wrapper mdw-mpc-prev () nil (mpc-prev))
4232(mdw-define-mpc-wrapper mdw-mpc-stop () nil (mpc-stop))
0f81a131 4233
5147578f
MW
4234(defun mdw-mpc-louder (step)
4235 (interactive (list (if current-prefix-arg
4236 (prefix-numeric-value current-prefix-arg)
4237 +10)))
4238 (mpc-proc-cmd (format "volume %+d" step)))
4239
4240(defun mdw-mpc-quieter (step)
4241 (interactive (list (if current-prefix-arg
4242 (prefix-numeric-value current-prefix-arg)
4243 +10)))
4244 (mpc-proc-cmd (format "volume %+d" (- step))))
4245
6dbdfe26
MW
4246(defun mdw-mpc-hack-lines (arg interactivep func)
4247 (if (and interactivep (use-region-p))
4248 (let ((from (region-beginning)) (to (region-end)))
4249 (goto-char from)
4250 (beginning-of-line)
4251 (funcall func)
4252 (forward-line)
4253 (while (< (point) to)
4254 (funcall func)
4255 (forward-line)))
4256 (let ((n (prefix-numeric-value arg)))
4257 (cond ((minusp n)
4258 (unless (bolp)
4259 (beginning-of-line)
4260 (funcall func)
4261 (incf n))
4262 (while (minusp n)
4263 (forward-line -1)
4264 (funcall func)
4265 (incf n)))
4266 (t
4267 (beginning-of-line)
4268 (while (plusp n)
4269 (funcall func)
4270 (forward-line)
4271 (decf n)))))))
4272
4273(defun mdw-mpc-select-one ()
4466dfac
MW
4274 (when (and (get-char-property (point) 'mpc-file)
4275 (not (get-char-property (point) 'mpc-select)))
6dbdfe26
MW
4276 (mpc-select-toggle)))
4277
4278(defun mdw-mpc-unselect-one ()
4279 (when (get-char-property (point) 'mpc-select)
4280 (mpc-select-toggle)))
4281
4282(defun mdw-mpc-select (&optional arg interactivep)
4283 (interactive (list current-prefix-arg t))
a30d0e33 4284 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
6dbdfe26
MW
4285
4286(defun mdw-mpc-unselect (&optional arg interactivep)
4287 (interactive (list current-prefix-arg t))
a30d0e33 4288 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-unselect-one))
6dbdfe26
MW
4289
4290(defun mdw-mpc-unselect-backwards (arg)
4291 (interactive "p")
a30d0e33 4292 (mdw-mpc-hack-lines (- arg) t 'mdw-mpc-unselect-one))
6dbdfe26
MW
4293
4294(defun mdw-mpc-unselect-all ()
4295 (interactive)
4296 (setq mpc-select nil)
4297 (mpc-selection-refresh))
4298
4299(defun mdw-mpc-next-line (arg)
4300 (interactive "p")
4301 (beginning-of-line)
4302 (forward-line arg))
4303
4304(defun mdw-mpc-previous-line (arg)
4305 (interactive "p")
4306 (beginning-of-line)
4307 (forward-line (- arg)))
4308
6d6f2b51
MW
4309(defun mdw-mpc-playlist-add (&optional arg interactivep)
4310 (interactive (list current-prefix-arg t))
4311 (let ((mpc-select mpc-select))
4312 (when (or arg (and interactivep (use-region-p)))
4313 (setq mpc-select nil)
4314 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
4315 (setq mpc-select (reverse mpc-select))
4316 (mpc-playlist-add)))
4317
4318(defun mdw-mpc-playlist-delete (&optional arg interactivep)
4319 (interactive (list current-prefix-arg t))
4320 (setq mpc-select (nreverse mpc-select))
4321 (mpc-select-save
4322 (when (or arg (and interactivep (use-region-p)))
4323 (setq mpc-select nil)
4324 (mpc-selection-refresh)
4325 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
4326 (mpc-playlist-delete)))
4327
75019c66
MW
4328(defun mdw-mpc-hack-tagbrowsers ()
4329 (setq-local mode-line-format
4330 '("%e"
4331 mode-line-frame-identification
4332 mode-line-buffer-identification)))
4333(add-hook 'mpc-tagbrowser-mode-hook 'mdw-mpc-hack-tagbrowsers)
4334
65f6a37a
MW
4335(defun mdw-mpc-hack-songs ()
4336 (setq-local header-line-format
4337 ;; '("MPC " mpc-volume " " mpc-current-song)
4338 (list (propertize " " 'display '(space :align-to 0))
4339 ;; 'mpc-songs-format-description
4340 '(:eval
4341 (let ((deactivate-mark) (hscroll (window-hscroll)))
4342 (with-temp-buffer
4343 (mpc-format mpc-songs-format 'self hscroll)
4344 ;; That would be simpler than the hscroll handling in
4345 ;; mpc-format, but currently move-to-column does not
4346 ;; recognize :space display properties.
4347 ;; (move-to-column hscroll)
4348 ;; (delete-region (point-min) (point))
4349 (buffer-string)))))))
4350(add-hook 'mpc-songs-mode-hook 'mdw-mpc-hack-songs)
4351
6dbdfe26
MW
4352(eval-after-load "mpc"
4353 '(progn
4354 (define-key mpc-mode-map "m" 'mdw-mpc-select)
4355 (define-key mpc-mode-map "u" 'mdw-mpc-unselect)
4356 (define-key mpc-mode-map "\177" 'mdw-mpc-unselect-backwards)
4357 (define-key mpc-mode-map "\e\177" 'mdw-mpc-unselect-all)
4358 (define-key mpc-mode-map "n" 'mdw-mpc-next-line)
4359 (define-key mpc-mode-map "p" 'mdw-mpc-previous-line)
56ba17be 4360 (define-key mpc-mode-map "/" 'mpc-songs-search)
6dbdfe26
MW
4361 (setq mpc-songs-mode-map (make-sparse-keymap))
4362 (set-keymap-parent mpc-songs-mode-map mpc-mode-map)
4363 (define-key mpc-songs-mode-map "l" 'mpc-playlist)
6d6f2b51
MW
4364 (define-key mpc-songs-mode-map "+" 'mdw-mpc-playlist-add)
4365 (define-key mpc-songs-mode-map "-" 'mdw-mpc-playlist-delete)
56ba17be 4366 (define-key mpc-songs-mode-map "\r" 'mpc-songs-jump-to)))
6dbdfe26 4367
0f81a131 4368;;;--------------------------------------------------------------------------
e07e3320
MW
4369;;; Inferior Emacs Lisp.
4370
4371(setq comint-prompt-read-only t)
4372
4373(eval-after-load "comint"
4374 '(progn
4375 (define-key comint-mode-map "\C-w" 'comint-kill-region)
4376 (define-key comint-mode-map [C-S-backspace] 'comint-kill-whole-line)))
4377
4378(eval-after-load "ielm"
4379 '(progn
4380 (define-key ielm-map "\C-w" 'comint-kill-region)
4381 (define-key ielm-map [C-S-backspace] 'comint-kill-whole-line)))
4382
f617db13
MW
4383;;;----- That's all, folks --------------------------------------------------
4384
4385(provide 'dot-emacs)