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