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