1 ;;; -*- mode: emacs-lisp; coding: utf-8 -*-
3 ;;; Functions and macros for .emacs
5 ;;; (c) 2004 Mark Wooding
8 ;;;----- Licensing notice ---------------------------------------------------
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.
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.
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.
24 ;;;--------------------------------------------------------------------------
25 ;;; Check command-line.
28 "Customization for mdw's Emacs configuration."
31 (defun mdw-check-command-line-switch (switch)
32 (let ((probe nil) (next command-line-args) (found nil))
34 (cond ((string= (car next) switch)
36 (if probe (rplacd probe (cdr next))
37 (setq command-line-args (cdr next))))
40 (setq next (cdr next)))
43 (defvar mdw-fast-startup nil
44 "Whether .emacs should optimize for rapid startup.
45 This may be at the expense of cool features.")
46 (setq mdw-fast-startup
47 (mdw-check-command-line-switch "--mdw-fast-startup"))
49 (defvar mdw-splashy-startup nil
50 "Whether to show a splash screen and related frippery.")
51 (setq mdw-splashy-startup
52 (mdw-check-command-line-switch "--mdw-splashy-startup"))
54 ;;;--------------------------------------------------------------------------
55 ;;; Some general utilities.
58 (unless (fboundp 'make-regexp) (load "make-regexp"))
61 (defmacro mdw-regexps (&rest list)
62 "Turn a LIST of strings into a single regular expression at compile-time."
65 `',(make-regexp (sort (copy-list list) #'string<)))
68 "This is not the key sequence you're looking for."
70 (error "wrong button"))
72 (defun mdw-emacs-version-p (major &optional minor)
73 "Return non-nil if the running Emacs is at least version MAJOR.MINOR."
74 (or (> emacs-major-version major)
75 (and (= emacs-major-version major)
76 (>= emacs-minor-version (or minor 0)))))
78 (defun mdw-submode-p (mode parent)
79 "Return non-nil if MODE is indirectly derived from PARENT."
81 (while (cond ((eq mode parent) (setq answer t) nil)
82 (t (setq mode (get mode 'derived-mode-parent)))))
85 ;; Some error trapping.
87 ;; If individual bits of this file go tits-up, we don't particularly want
88 ;; the whole lot to stop right there and then, because it's bloody annoying.
91 (defmacro trap (&rest forms)
92 "Execute FORMS without allowing errors to propagate outside."
96 ,(if (cdr forms) (cons 'progn forms) (car forms))
97 (error (message "Error (trapped): %s in %s"
98 (error-message-string err)
101 ;; Configuration reading.
103 (defvar mdw-config nil)
104 (defun mdw-config (sym)
105 "Read the configuration variable named SYM."
108 (flet ((replace (what with)
109 (goto-char (point-min))
110 (while (re-search-forward what nil t)
111 (replace-match with t))))
113 (insert-file-contents "~/.mdw.conf")
114 (replace "^[ \t]*\\(#.*\\)?\n" "")
115 (replace (concat "^[ \t]*"
116 "\\([-a-zA-Z0-9_.]*\\)"
119 "[ \t]**\\(\n\\|$\\)")
121 (car (read-from-string
122 (concat "(" (buffer-string) ")")))))))
123 (cdr (assq sym mdw-config)))
125 ;; Width configuration.
127 (defcustom mdw-column-width
128 (string-to-number (or (mdw-config 'emacs-width) "77"))
129 "Width of Emacs columns."
131 (defcustom mdw-text-width mdw-column-width
132 "Expected width of text within columns."
136 ;; Local variables hacking.
138 (defun run-local-vars-mode-hook ()
139 "Run a hook for the major-mode after local variables have been processed."
140 (run-hooks (intern (concat (symbol-name major-mode)
141 "-local-variables-hook"))))
142 (add-hook 'hack-local-variables-hook 'run-local-vars-mode-hook)
144 ;; Set up the load path convincingly.
146 (dolist (dir (append (and (boundp 'debian-emacs-flavor)
147 (list (concat "/usr/share/"
148 (symbol-name debian-emacs-flavor)
150 (dolist (sub (directory-files dir t))
151 (when (and (file-accessible-directory-p sub)
152 (not (member sub load-path)))
153 (setq load-path (nconc load-path (list sub))))))
155 ;; Is an Emacs library available?
157 (defun library-exists-p (name)
158 "Return non-nil if NAME is an available library.
159 Return non-nil if NAME.el (or NAME.elc) somewhere on the Emacs
160 load path. The non-nil value is the filename we found for the
162 (let ((path load-path) elt (foundp nil))
163 (while (and path (not foundp))
164 (setq elt (car path))
165 (setq path (cdr path))
166 (setq foundp (or (let ((file (concat elt "/" name ".elc")))
167 (and (file-exists-p file) file))
168 (let ((file (concat elt "/" name ".el")))
169 (and (file-exists-p file) file)))))
172 (defun maybe-autoload (symbol file &optional docstring interactivep type)
173 "Set an autoload if the file actually exists."
174 (and (library-exists-p file)
175 (autoload symbol file docstring interactivep type)))
177 (defun mdw-kick-menu-bar (&optional frame)
178 "Regenerate FRAME's menu bar so it doesn't have empty menus."
180 (unless frame (setq frame (selected-frame)))
181 (let ((old (frame-parameter frame 'menu-bar-lines)))
182 (set-frame-parameter frame 'menu-bar-lines 0)
183 (set-frame-parameter frame 'menu-bar-lines old)))
187 (defun mdw-fixup-page-position ()
188 (unless (eq (char-before (point)) ?
\f)
191 (defadvice backward-page (after mdw-fixup compile activate)
192 (mdw-fixup-page-position))
193 (defadvice forward-page (after mdw-fixup compile activate)
194 (mdw-fixup-page-position))
196 ;; Splitting windows.
198 (unless (fboundp 'scroll-bar-columns)
199 (defun scroll-bar-columns (side)
200 (cond ((eq side 'left) 0)
203 (unless (fboundp 'fringe-columns)
204 (defun fringe-columns (side)
205 (cond ((not window-system) 0)
209 (defun mdw-horizontal-window-overhead ()
210 "Computes the horizontal window overhead.
211 This is the number of columns used by fringes, scroll bars and other such
213 (if (not window-system)
216 (dolist (what '(scroll-bar fringe))
217 (dolist (side '(left right))
218 (incf tot (funcall (intern (concat (symbol-name what) "-columns"))
222 (defun mdw-split-window-horizontally (&optional width)
223 "Split a window horizontally.
224 Without a numeric argument, split the window approximately in
225 half. With a numeric argument WIDTH, allocate WIDTH columns to
226 the left-hand window (if positive) or -WIDTH columns to the
227 right-hand window (if negative). Space for scroll bars and
228 fringes is not taken out of the allowance for WIDTH, unlike
229 \\[split-window-horizontally]."
231 (split-window-horizontally
232 (cond ((null width) nil)
233 ((>= width 0) (+ width (mdw-horizontal-window-overhead)))
234 ((< width 0) width))))
236 (defun mdw-preferred-column-width ()
237 "Return the preferred column width."
238 (if (and window-system (mdw-emacs-version-p 22)) mdw-column-width
239 (1+ mdw-column-width)))
241 (defun mdw-divvy-window (&optional width)
242 "Split a wide window into appropriate widths."
244 (setq width (if width (prefix-numeric-value width)
245 (mdw-preferred-column-width)))
246 (let* ((win (selected-window))
247 (sb-width (mdw-horizontal-window-overhead))
248 (c (/ (+ (window-width) sb-width)
249 (+ width sb-width))))
252 (split-window-horizontally (+ width sb-width))
254 (select-window win)))
256 (defun mdw-frame-width-quantized-p (frame-width column-width)
257 "Return whether the FRAME-WIDTH was chosen specifically for COLUMN-WIDTH."
258 (let ((sb-width (mdw-horizontal-window-overhead)))
259 (zerop (mod (+ frame-width sb-width)
260 (+ column-width sb-width)))))
262 (defun mdw-frame-width-for-columns (columns width)
263 "Return the preferred width for a frame with so many COLUMNS of WIDTH."
264 (let ((sb-width (mdw-horizontal-window-overhead)))
265 (- (* columns (+ width sb-width))
268 (defun mdw-set-frame-width (columns &optional width)
269 "Set the current frame to be the correct width for COLUMNS columns.
271 If WIDTH is non-nil, then it provides the width for the new columns. (This
272 can be set interactively with a prefix argument.)"
273 (interactive "nColumns:
275 (setq width (if width (prefix-numeric-value width)
276 (mdw-preferred-column-width)))
277 (set-frame-width (selected-frame)
278 (mdw-frame-width-for-columns columns width))
279 (mdw-divvy-window width))
281 (defcustom mdw-frame-width-fudge
282 (cond ((<= emacs-major-version 20) 1)
283 ((= emacs-major-version 26) 3)
285 "The number of extra columns to add to the desired frame width.
287 This is sadly necessary because Emacs 26 is broken in this regard."
290 (defcustom mdw-frame-colour-alist
291 '((black . ("#000000" . "#ffffff"))
292 (red . ("#2a0000" . "#ffffff"))
293 (green . ("#002a00" . "#ffffff"))
294 (blue . ("#00002a" . "#ffffff")))
295 "Alist mapping symbol names to (FOREGROUND . BACKGROUND) colour pairs."
296 :type '(alist :key-type symbol :value-type (cons color color)))
298 (defun mdw-set-frame-colour (colour &optional frame)
299 (interactive "xColour name or (FOREGROUND . BACKGROUND) pair:
301 (when (and colour (symbolp colour))
302 (let ((entry (assq colour mdw-frame-colour-alist)))
303 (unless entry (error "Unknown colour `%s'" colour))
304 (setf colour (cdr entry))))
305 (set-frame-parameter frame 'background-color (car colour))
306 (set-frame-parameter frame 'foreground-color (cdr colour)))
308 ;; Window configuration switching.
310 (defvar mdw-current-window-configuration nil
311 "The current window configuration register name, or `nil'.")
313 (defun mdw-switch-window-configuration (register &optional no-save)
314 "Switch make REGISTER be the new current window configuration.
315 If a current window configuration register is established, and
316 NO-SAVE is nil, then save the current window configuration to
319 Signal an error if the new register contains something other than
320 a window configuration. If the register is unset then save the
321 current window configuration to it immediately.
323 With one or three C-u, or an odd numeric prefix argument, set
324 NO-SAVE, so the previous window configuration register is left
327 With two or three C-u, or a prefix argument which is an odd
328 multiple of 2, just clear the record of the current window
329 configuration register, so that the next switch doesn't save the
330 prevailing configuration."
332 (let ((arg current-prefix-arg))
333 (list (if (or (and (consp arg) (= (car arg) 16) (= (car arg) 64))
334 (and (integerp arg) (not (zerop (logand arg 2)))))
336 (register-read-with-preview "Switch to window configuration: "))
337 (or (and (consp arg) (= (car arg) 4) (= (car arg) 64))
338 (and (integerp arg) (not (zerop (logand arg 1))))))))
340 (let ((previous mdw-current-window-configuration)
341 (current-windows (list (current-window-configuration)
343 (register-value (and register (get-register register))))
344 (when (and mdw-current-window-configuration (not no-save))
345 (set-register mdw-current-window-configuration current-windows))
346 (cond ((null register)
347 (setq mdw-current-window-configuration nil)
349 (message "Left window configuration `%c'." previous)
350 (message "Nothing to do!")))
351 ((not (or (null register-value)
352 (and (consp register-value)
353 (window-configuration-p (car register-value))
354 (integer-or-marker-p (cadr register-value))
355 (null (caddr register-value)))))
356 (error "Register `%c' is not a window configuration" register))
358 (cond ((null register-value)
359 (set-register register current-windows)
360 (message "Started new window configuration `%c'."
363 (set-window-configuration (car register-value))
364 (goto-char (cadr register-value))
365 (message "Switched to window configuration `%c'."
367 (setq mdw-current-window-configuration register)))))
369 ;; Don't raise windows unless I say so.
371 (defcustom mdw-inhibit-raise-frame nil
372 "Whether `raise-frame' should do nothing when the frame is mapped."
375 (defadvice raise-frame
376 (around mdw-inhibit (&optional frame) activate compile)
377 "Don't actually do anything if `mdw-inhibit-raise-frame' is true, and the
378 frame is actually mapped on the screen."
379 (if mdw-inhibit-raise-frame
380 (make-frame-visible frame)
383 (defmacro mdw-advise-to-inhibit-raise-frame (function)
384 "Advise the FUNCTION not to raise frames, even if it wants to."
385 `(defadvice ,function
386 (around mdw-inhibit-raise (&rest hunoz) activate compile)
387 "Don't raise the window unless you have to."
388 (let ((mdw-inhibit-raise-frame t))
391 (mdw-advise-to-inhibit-raise-frame select-frame-set-input-focus)
392 (mdw-advise-to-inhibit-raise-frame appt-disp-window)
393 (mdw-advise-to-inhibit-raise-frame mouse-select-window)
395 ;; Bug fix for markdown-mode, which breaks point positioning during
397 (defadvice markdown-check-change-for-wiki-link
398 (around mdw-save-match activate compile)
399 "Save match data around the `markdown-mode' `after-change-functions' hook."
400 (save-match-data ad-do-it))
402 ;; Bug fix for `bbdb-canonicalize-address': on Emacs 24, `run-hook-with-args'
403 ;; always returns nil, with the result that all email addresses are lost.
404 ;; Replace the function entirely.
405 (defadvice bbdb-canonicalize-address
406 (around mdw-bug-fix activate compile)
407 "Don't use `run-hook-with-args', because that doesn't work."
408 (let ((net (ad-get-arg 0)))
410 ;; Make sure this is a proper hook list.
411 (if (functionp bbdb-canonicalize-net-hook)
412 (setq bbdb-canonicalize-net-hook (list bbdb-canonicalize-net-hook)))
414 ;; Iterate over the hooks until things converge.
417 (let (next (changep nil)
418 hook (hooks bbdb-canonicalize-net-hook))
420 (setq hook (pop hooks))
421 (setq next (funcall hook net))
422 (if (not (equal next net))
425 (setq donep (not changep)))))
426 (setq ad-return-value net)))
428 ;; Transient mark mode hacks.
430 (defadvice exchange-point-and-mark
431 (around mdw-highlight (&optional arg) activate compile)
432 "Maybe don't actually exchange point and mark.
433 If `transient-mark-mode' is on and the mark is inactive, then
434 just activate it. A non-trivial prefix argument will force the
435 usual behaviour. A trivial prefix argument (i.e., just C-u) will
436 activate the mark and temporarily enable `transient-mark-mode' if
438 (cond ((or mark-active
439 (and (not transient-mark-mode) (not arg))
440 (and arg (or (not (consp arg))
441 (not (= (car arg) 4)))))
444 (or transient-mark-mode (setq transient-mark-mode 'only))
445 (set-mark (mark t)))))
447 ;; Functions for sexp diary entries.
449 (defvar mdw-diary-for-org-mode-p nil
450 "Display diary along with the agenda?")
452 (defun mdw-not-org-mode (form)
453 "As FORM, but not in Org mode agenda."
454 (and (not mdw-diary-for-org-mode-p)
457 (defun mdw-weekday (l)
458 "Return non-nil if `date' falls on one of the days of the week in L.
459 L is a list of day numbers (from 0 to 6 for Sunday through to
460 Saturday) or symbols `sunday', `monday', etc. (or a mixture). If
461 the date stored in `date' falls on a listed day, then the
462 function returns non-nil."
463 (let ((d (calendar-day-of-week date)))
465 (memq (nth d '(sunday monday tuesday wednesday
466 thursday friday saturday)) l))))
468 (defun mdw-discordian-date (date)
469 "Return the Discordian calendar date corresponding to DATE.
471 The return value is (YOLD . st-tibs-day) or (YOLD SEASON DAYNUM DOW).
473 The original is by David Pearson. I modified it to produce date components
474 as output rather than a string."
475 (let* ((days ["Sweetmorn" "Boomtime" "Pungenday"
476 "Prickle-Prickle" "Setting Orange"])
477 (months ["Chaos" "Discord" "Confusion"
478 "Bureaucracy" "Aftermath"])
479 (day-count [0 31 59 90 120 151 181 212 243 273 304 334])
480 (year (- (calendar-extract-year date) 1900))
481 (month (1- (calendar-extract-month date)))
482 (day (1- (calendar-extract-day date)))
483 (julian (+ (aref day-count month) day))
484 (dyear (+ year 3066)))
485 (if (and (= month 1) (= day 28))
486 (cons dyear 'st-tibs-day)
488 (aref months (floor (/ julian 73)))
490 (aref days (mod julian 5))))))
492 (defun mdw-diary-discordian-date ()
493 "Convert the date in `date' to a string giving the Discordian date."
494 (let* ((ddate (mdw-discordian-date date))
495 (tail (format "in the YOLD %d" (car ddate))))
496 (if (eq (cdr ddate) 'st-tibs-day)
497 (format "St Tib's Day %s" tail)
498 (let ((season (cadr ddate))
499 (daynum (caddr ddate))
500 (dayname (cadddr ddate)))
501 (format "%s, the %d%s day of %s %s"
504 (let ((ldig (mod daynum 10)))
505 (cond ((= ldig 1) "st")
512 (defun mdw-todo (&optional when)
513 "Return non-nil today, or on WHEN, whichever is later."
514 (let ((w (calendar-absolute-from-gregorian (calendar-current-date)))
515 (d (calendar-absolute-from-gregorian date)))
517 (setq w (max w (calendar-absolute-from-gregorian
519 ((not european-calendar-style)
531 (defadvice org-agenda-list (around mdw-preserve-links activate)
532 (let ((mdw-diary-for-org-mode-p t))
535 (defcustom diary-time-regexp nil
536 "Regexp matching times in the diary buffer."
539 (defadvice diary-add-to-list (before mdw-trim-leading-space compile activate)
540 "Trim leading space from the diary entry string."
542 (let ((str (ad-get-arg 1))
546 (setq str (cond ((null str) nil)
547 ((string-match "\\(^\\|\n\\)[ \t]+" str)
548 (replace-match "\\1" nil nil str))
549 ((and mdw-diary-for-org-mode-p
550 (string-match (concat
552 "\\(" diary-time-regexp
553 "\\(-" diary-time-regexp "\\)?"
555 "\\(\t[ \t]*\\| [ \t]+\\)")
557 (replace-match "\\1\\2 " nil nil str))
558 ((and (not mdw-diary-for-org-mode-p)
559 (string-match "\\[\\[[^][]*]\\[\\([^][]*\\)]]"
561 (replace-match "\\1" nil nil str))
563 (if (equal str old) (setq done t)))
564 (ad-set-arg 1 str))))
566 (defadvice org-bbdb-anniversaries (after mdw-fixup-list compile activate)
567 "Return a string rather than a list."
570 (dolist (e (let ((ee ad-return-value))
571 (if (atom ee) (list ee) ee)))
573 (when anyp (insert ?\n))
576 (setq ad-return-value
577 (and anyp (buffer-string))))))
579 ;; Fighting with Org-mode's evil key maps.
581 (defcustom mdw-evil-keymap-keys
582 '(([S-up] . [?\C-c up])
583 ([S-down] . [?\C-c down])
584 ([S-left] . [?\C-c left])
585 ([S-right] . [?\C-c right])
586 (([M-up] [?\e up]) . [C-up])
587 (([M-down] [?\e down]) . [C-down])
588 (([M-left] [?\e left]) . [C-left])
589 (([M-right] [?\e right]) . [C-right]))
590 "Defines evil keybindings to clobber in `mdw-clobber-evil-keymap'.
591 The value is an alist mapping evil keys (as a list, or singleton)
592 to good keys (in the same form)."
593 :type '(alist :key-type (choice key-sequence (repeat key-sequence))
594 :value-type key-sequence))
596 (defun mdw-clobber-evil-keymap (keymap)
597 "Replace evil key bindings in the KEYMAP.
598 Evil key bindings are defined in `mdw-evil-keymap-keys'."
599 (dolist (entry mdw-evil-keymap-keys)
601 (keys (if (listp (car entry))
604 (replacements (if (listp (cdr entry))
606 (list (cdr entry)))))
609 (setq binding (lookup-key keymap key))
611 (throw 'found nil))))
614 (define-key keymap key nil))
615 (dolist (key replacements)
616 (define-key keymap key binding))))))
618 (defcustom mdw-org-latex-defs
620 "\\documentclass{strayman}
621 \\usepackage[utf8]{inputenc}
622 \\usepackage[palatino, helvetica, courier, maths=cmr]{mdwfonts}
623 \\usepackage{graphicx, tikz, mdwtab, mdwmath, crypto, longtable}"
624 ("\\section{%s}" . "\\section*{%s}")
625 ("\\subsection{%s}" . "\\subsection*{%s}")
626 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
627 ("\\paragraph{%s}" . "\\paragraph*{%s}")
628 ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
629 "Additional LaTeX class definitions."
630 :type '(alist :key-type string
631 :value-type (list string
634 :value-type string))))
636 (eval-after-load "org-latex"
637 '(setq org-export-latex-classes
638 (append mdw-org-latex-defs org-export-latex-classes)))
640 (eval-after-load "ox-latex"
641 '(setq org-latex-classes (append mdw-org-latex-defs org-latex-classes)
642 org-latex-caption-above nil
643 org-latex-default-packages-alist '(("AUTO" "inputenc" t)
651 ("normalem" "ulem" t)
657 "\\tolerance=1000")))
659 (setq org-export-docbook-xslt-proc-command "xsltproc --output %o %s %i"
660 org-export-docbook-xsl-fo-proc-command "fop %i.safe %o"
661 org-export-docbook-xslt-stylesheet
662 "/usr/share/xml/docbook/stylesheet/docbook-xsl/fo/docbook.xsl")
666 (setq glasses-separator "-"
667 glasses-separate-parentheses-p nil
668 glasses-uncapitalize-p t)
670 ;; Some hacks to do with window placement.
672 (defvar mdw-designated-window nil
673 "The window chosen by `mdw-designate-window', or nil.")
675 (defun mdw-designated-window-display-buffer-function (buffer not-this-window)
676 "Display buffer function to use the designated window."
677 (unless mdw-designated-window (error "No designated window!"))
678 (prog1 mdw-designated-window
679 (with-selected-window mdw-designated-window (switch-to-buffer buffer))
680 (setq mdw-designated-window nil
681 display-buffer-function nil)))
683 (defun mdw-display-buffer-in-designated-window (buffer alist)
684 "Display function to use the designated window."
685 (prog1 mdw-designated-window
686 (when mdw-designated-window
687 (with-selected-window mdw-designated-window
688 (switch-to-buffer buffer nil t)))
689 (setq mdw-designated-window nil)))
691 (defun mdw-designate-window (cancel)
692 "Use the selected window for the next pop-up buffer.
693 With a prefix argument, clear the designated window."
695 (let ((window (selected-window)))
697 (setq mdw-designated-window nil)
698 (unless (mdw-emacs-version-p 24)
699 (setq display-buffer-function nil))
700 (message "Window designation cleared."))
701 ((window-dedicated-p window)
702 (error "Window is dedicated to its buffer."))
704 (setq mdw-designated-window window)
705 (unless (mdw-emacs-version-p 24)
706 (setq display-buffer-function
707 #'mdw-designated-window-display-buffer-function))
708 (message "Window designated.")))))
710 (when (mdw-emacs-version-p 24)
711 (setq display-buffer-base-action
712 (let* ((action display-buffer-base-action)
714 (alist (cdr action)))
715 (cons (cons 'mdw-display-buffer-in-designated-window funcs)
718 (defun mdw-clobber-other-windows-showing-buffer (buffer-or-name)
719 "Arrange that no windows on other frames are showing BUFFER-OR-NAME."
720 (interactive "bBuffer: ")
721 (let ((home-frame (selected-frame))
722 (buffer (get-buffer buffer-or-name))
723 (safe-buffer (get-buffer "*scratch*")))
724 (dolist (frame (frame-list))
725 (unless (eq frame home-frame)
726 (dolist (window (window-list frame))
727 (when (eq (window-buffer window) buffer)
728 (set-window-buffer window safe-buffer)))))))
730 (defvar mdw-inhibit-walk-windows nil
731 "If non-nil, then `walk-windows' does nothing.
732 This is used by advice on `switch-to-buffer-other-frame' to inhibit finding
733 buffers in random frames.")
735 (setq display-buffer--other-frame-action
736 '((display-buffer-reuse-window display-buffer-pop-up-frame)
737 (reusable-frames . nil)
738 (inhibit-same-window . t)))
740 (defadvice walk-windows (around mdw-inhibit activate)
741 "If `mdw-inhibit-walk-windows' is non-nil, then do nothing."
742 (and (not mdw-inhibit-walk-windows)
745 (defadvice switch-to-buffer-other-frame
746 (around mdw-always-new-frame activate)
747 "Always make a new frame.
748 Even if an existing window in some random frame looks tempting."
749 (let ((mdw-inhibit-walk-windows t)) ad-do-it))
751 (defadvice display-buffer (before mdw-inhibit-other-frames activate)
752 "Don't try to do anything fancy with other frames.
753 Pretend they don't exist. They might be on other display devices."
756 (setq even-window-sizes nil
757 even-window-heights nil)
759 (setq display-buffer-reuse-frames nil)
761 (defun mdw-last-window-in-frame-p (window)
762 "Return whether WINDOW is the last in its frame."
765 (let ((next (window-next-sibling window)))
766 (while (and next (window-minibuffer-p next))
767 (setq next (window-next-sibling next)))
768 (if next (throw 'done nil)))
769 (setq window (window-parent window)))
772 (defun mdw-display-buffer-in-tolerable-window (buffer alist)
773 "Try finding a tolerable window in which to display BUFFER.
774 Begone, foul DWIMmerlaik!
776 This is all totally subject to arbitrary change in the future, but the
777 emphasis is on predictability rather than crazy DWIMmery."
778 (let* ((selected (selected-window)) chosen
779 (full-height-p (window-full-height-p selected))
780 (full-width-p (window-full-width-p selected)))
783 ((and full-height-p full-width-p)
784 ;; We're basically the only window in the frame. If we want to get
785 ;; anywhere, we'll have to split the window.
787 (let ((width (window-width selected))
788 (preferred-width (mdw-preferred-column-width)))
789 (if (and (>= width (mdw-frame-width-for-columns 2 preferred-width))
790 (mdw-frame-width-quantized-p width preferred-width))
791 (setq chosen (split-window-right preferred-width))
792 (setq chosen (split-window-below)))
793 (display-buffer-record-window 'window chosen buffer)))
795 ((mdw-last-window-in-frame-p selected)
796 ;; This is the last window in the frame. I don't think I want to
797 ;; clobber the first window, so rebound and clobber the previous one
798 ;; instead. (This obviously has the same effect if there are only two
799 ;; windows, but seems more useful if there are three.)
801 (setq chosen (previous-window selected 'never nil))
802 (display-buffer-record-window 'reuse chosen buffer))
805 ;; There's another window in front of us. Let's use that one.
806 (setq chosen (next-window selected 'never nil)))
807 (display-buffer-record-window 'reuse chosen buffer))
809 (if (eq chosen selected)
810 (error "Failed to select a different window!"))
813 (with-selected-window chosen (switch-to-buffer buffer)))
816 ;; Hack the display actions so that they do something sensible.
817 (setq display-buffer-fallback-action
818 '((display-buffer--maybe-same-window
819 display-buffer-reuse-window
820 display-buffer-pop-up-window
821 mdw-display-buffer-in-tolerable-window)))
823 ;; Rename buffers along with files.
825 (defvar mdw-inhibit-rename-buffer nil
826 "If non-nil, `rename-file' won't rename the buffer visiting the file.")
828 (defmacro mdw-advise-to-inhibit-rename-buffer (function)
829 "Advise FUNCTION to set `mdw-inhibit-rename-buffer' while it runs.
831 This will prevent `rename-file' from renaming the buffer."
832 `(defadvice ,function (around mdw-inhibit-rename-buffer compile activate)
833 "Don't rename the buffer when renaming the underlying file."
834 (let ((mdw-inhibit-rename-buffer t))
836 (mdw-advise-to-inhibit-rename-buffer recode-file-name)
837 (mdw-advise-to-inhibit-rename-buffer set-visited-file-name)
838 (mdw-advise-to-inhibit-rename-buffer backup-buffer)
840 (defadvice rename-file (after mdw-rename-buffers (from to &optional forcep)
842 "If a buffer is visiting the file, rename it to match the new name.
844 Don't do this if `mdw-inhibit-rename-buffer' is non-nil."
845 (unless mdw-inhibit-rename-buffer
846 (let ((buffer (get-file-buffer from)))
848 (let ((to (if (not (string= (file-name-nondirectory to) "")) to
849 (concat to (file-name-nondirectory from)))))
850 (with-current-buffer buffer
851 (set-visited-file-name to nil t)))))))
853 ;;;--------------------------------------------------------------------------
854 ;;; Improved compilation machinery.
856 ;; Uprated version of M-x compile.
858 (setq compile-command
859 (let ((ncpu (with-temp-buffer
860 (insert-file-contents "/proc/cpuinfo")
862 (count-matches "^processor\\s-*:"))))
863 (format "nice make -j%d -k" (* 2 ncpu))))
865 (defun mdw-compilation-buffer-name (mode)
866 (concat "*" (downcase mode) ": "
867 (abbreviate-file-name default-directory) "*"))
868 (setq compilation-buffer-name-function 'mdw-compilation-buffer-name)
870 (eval-after-load "compile"
872 (define-key compilation-shell-minor-mode-map "\C-c\M-g" 'recompile)))
874 (defadvice compile (around hack-environment compile activate)
875 "Hack the environment inherited by inferiors in the compilation."
876 (let ((process-environment (copy-tree process-environment)))
877 (setenv "LD_PRELOAD" nil)
880 (defun mdw-compile (command &optional directory comint)
881 "Initiate a compilation COMMAND, maybe in a different DIRECTORY.
882 The DIRECTORY may be nil to not change. If COMINT is t, then
883 start an interactive compilation.
885 Interactively, prompt for the command if the variable
886 `compilation-read-command' is non-nil, or if requested through
887 the prefix argument. Prompt for the directory, and run
888 interactively, if requested through the prefix.
890 Use a prefix of 4, 6, 12, or 14, or type C-u between one and three times, to
891 force prompting for a directory.
893 Use a prefix of 2, 6, 10, or 14, or type C-u three times, to force
894 prompting for the command.
896 Use a prefix of 8, 10, 12, or 14, or type C-u twice or three times,
897 to force interactive compilation."
899 (let* ((prefix (prefix-numeric-value current-prefix-arg))
900 (command (eval compile-command))
901 (dir (and (plusp (logand prefix #x54))
902 (read-directory-name "Compile in directory: "))))
903 (list (if (or compilation-read-command
904 (plusp (logand prefix #x42)))
905 (compilation-read-command command)
908 (plusp (logand prefix #x58)))))
909 (let ((default-directory (or directory default-directory)))
910 (compile command comint)))
914 (defun mdw-find-build-dir (build-file)
916 (let* ((src-dir (file-name-as-directory (expand-file-name ".")))
919 (when (file-exists-p (concat dir build-file))
921 (let ((sub (expand-file-name (file-relative-name src-dir dir)
922 (concat dir "build/"))))
925 (when (file-exists-p (concat sub build-file))
927 (when (string= sub dir) (throw 'give-up nil))
928 (setq sub (file-name-directory (directory-file-name sub))))))
930 (setq dir (file-name-directory
931 (directory-file-name dir))))
932 (throw 'found nil))))))
934 (defun mdw-flymake-make-init ()
935 (let ((build-dir (mdw-find-build-dir "Makefile")))
937 (let ((tmp-src (flymake-init-create-temp-buffer-copy
938 #'flymake-create-temp-inplace)))
939 (flymake-get-syntax-check-program-args
940 tmp-src build-dir t t
941 #'flymake-get-make-cmdline)))))
943 (setq flymake-allowed-file-name-masks
944 '(("\\.\\(?:[cC]\\|cc\\|cpp\\|cxx\\|c\\+\\+\\)\\'"
945 mdw-flymake-make-init)
946 ("\\.\\(?:[hH]\\|hh\\|hpp\\|hxx\\|h\\+\\+\\)\\'"
947 mdw-flymake-master-make-init)
948 ("\\.p[lm]" flymake-perl-init)))
950 (setq flymake-mode-map
951 (let ((map (if (boundp 'flymake-mode-map)
953 (make-sparse-keymap))))
954 (define-key map [?\C-c ?\C-f ?\C-p] 'flymake-goto-prev-error)
955 (define-key map [?\C-c ?\C-f ?\C-n] 'flymake-goto-next-error)
956 (define-key map [?\C-c ?\C-f ?\C-c] 'flymake-compile)
957 (define-key map [?\C-c ?\C-f ?\C-k] 'flymake-stop-all-syntax-checks)
958 (define-key map [?\C-c ?\C-f ?\C-e] 'flymake-popup-current-error-menu)
961 ;;;--------------------------------------------------------------------------
962 ;;; Mail and news hacking.
964 (define-derived-mode mdwmail-mode mail-mode "[mdw] mail"
965 "Major mode for editing news and mail messages from external programs.
966 Not much right now. Just support for doing MailCrypt stuff."
969 (run-hooks 'mail-setup-hook))
971 (define-key mdwmail-mode-map [?\C-c ?\C-c] 'disabled-operation)
973 (add-hook 'mdwail-mode-hook
975 (set-buffer-file-coding-system 'utf-8)
976 (make-local-variable 'paragraph-separate)
977 (make-local-variable 'paragraph-start)
978 (setq paragraph-start
979 (concat "[ \t]*[-_][-_][-_]+$\\|^-- \\|-----\\|"
981 (setq paragraph-separate
982 (concat "[ \t]*[-_][-_][-_]+$\\|^-- \\|-----\\|"
983 paragraph-separate))))
985 ;; How to encrypt in mdwmail.
987 (defun mdwmail-mc-encrypt (&optional recip scm start end from sign)
989 (setq start (save-excursion
990 (goto-char (point-min))
991 (or (search-forward "\n\n" nil t) (point-min)))))
993 (setq end (point-max)))
994 (mc-encrypt-generic recip scm start end from sign))
996 ;; How to sign in mdwmail.
998 (defun mdwmail-mc-sign (key scm start end uclr)
1000 (setq start (save-excursion
1001 (goto-char (point-min))
1002 (or (search-forward "\n\n" nil t) (point-min)))))
1004 (setq end (point-max)))
1005 (mc-sign-generic key scm start end uclr))
1007 ;; Some signature mangling.
1009 (defun mdwmail-mangle-signature ()
1011 (goto-char (point-min))
1012 (perform-replace "\n-- \n" "\n-- " nil nil nil)))
1013 (add-hook 'mail-setup-hook 'mdwmail-mangle-signature)
1014 (add-hook 'message-setup-hook 'mdwmail-mangle-signature)
1016 ;; Insert my login name into message-ids, so I can score replies.
1018 (defadvice message-unique-id (after mdw-user-name last activate compile)
1019 "Ensure that the user's name appears at the end of the message-id string,
1020 so that it can be used for convenient filtering."
1021 (setq ad-return-value (concat ad-return-value "." (user-login-name))))
1023 ;; Tell my movemail hack where movemail is.
1025 ;; This is needed to shup up warnings about LD_PRELOAD.
1027 (let ((path exec-path))
1029 (let ((try (expand-file-name "movemail" (car path))))
1030 (if (file-executable-p try)
1031 (setenv "REAL_MOVEMAIL" try))
1032 (setq path (cdr path)))))
1034 ;; AUTHINFO GENERIC kludge.
1036 (defcustom nntp-authinfo-generic nil
1037 "Set to the `NNTPAUTH' string to pass on to `authinfo-kludge'.
1039 Use this to arrange for per-server settings."
1040 :type '(choice (const :tag "Use `NNTPAUTH' environment variable" nil)
1044 (defun nntp-open-authinfo-kludge (buffer)
1045 "Open a connection to SERVER using `authinfo-kludge'."
1046 (let ((proc (start-process "nntpd" buffer
1047 "env" (concat "NNTPAUTH="
1048 (or nntp-authinfo-generic
1050 (error "NNTPAUTH unset")))
1051 "authinfo-kludge" nntp-address)))
1053 (nntp-wait-for-string "^\r*200")
1055 (delete-region (point-min) (point))
1058 (eval-after-load "erc"
1059 '(load "~/.ercrc.el"))
1061 ;; Heavy-duty Gnus patching.
1063 (defun mdw-nnimap-transform-headers ()
1064 (goto-char (point-min))
1065 (let (article lines size string)
1068 (while (not (looking-at "\\* [0-9]+ FETCH"))
1069 (delete-region (point) (progn (forward-line 1) (point)))
1072 (goto-char (match-end 0))
1073 ;; Unfold quoted {number} strings.
1074 (while (re-search-forward
1075 "[^]][ (]{\\([0-9]+\\)}\r?\n"
1077 ;; Start of the header section.
1078 (or (re-search-forward "] {[0-9]+}\r?\n" nil t)
1079 ;; Start of the next FETCH.
1080 (re-search-forward "\\* [0-9]+ FETCH" nil t)
1083 (setq size (string-to-number (match-string 1)))
1084 (delete-region (+ (match-beginning 0) 2) (point))
1085 (setq string (buffer-substring (point) (+ (point) size)))
1086 (delete-region (point) (+ (point) size))
1087 (insert (format "%S" (subst-char-in-string ?\n ?\s string)))
1088 ;; [mdw] missing from upstream
1092 (and (re-search-forward "UID \\([0-9]+\\)"
1098 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
1103 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
1104 (let ((structure (ignore-errors
1105 (read (current-buffer)))))
1106 (while (and (consp structure)
1107 (not (atom (car structure))))
1108 (setq structure (car structure)))
1109 (setq lines (if (and
1110 (stringp (car structure))
1111 (equal (upcase (nth 0 structure)) "MESSAGE")
1112 (equal (upcase (nth 1 structure)) "RFC822"))
1114 (nth 7 structure)))))
1115 (delete-region (line-beginning-position) (line-end-position))
1116 (insert (format "211 %s Article retrieved." article))
1119 (insert (format "Chars: %s\n" size)))
1121 (insert (format "Lines: %s\n" lines)))
1122 ;; Most servers have a blank line after the headers, but
1124 (unless (re-search-forward "^\r$\\|^)\r?$" nil t)
1125 (goto-char (point-max)))
1126 (delete-region (line-beginning-position) (line-end-position))
1128 (forward-line 1)))))
1130 (eval-after-load 'nnimap
1131 '(defalias 'nnimap-transform-headers
1132 (symbol-function 'mdw-nnimap-transform-headers)))
1134 (defadvice gnus-other-frame (around mdw-hack-frame-width compile activate)
1135 "Always arrange for mail/news frames to be 80 columns wide."
1136 (let ((default-frame-alist (cons `(width . ,(+ 80 mdw-frame-width-fudge))
1137 (delete* 'width default-frame-alist
1141 ;; Preferred programs.
1143 (setq mailcap-user-mime-data
1144 '(((type . "application/pdf") (viewer . "mupdf %s"))))
1146 ;;;--------------------------------------------------------------------------
1147 ;;; Utility functions.
1149 (or (fboundp 'line-number-at-pos)
1150 (defun line-number-at-pos (&optional pos)
1151 (let ((opoint (or pos (point))) start)
1154 (goto-char (point-min))
1157 (setq start (point))
1160 (1+ (count-lines 1 (point))))))))
1162 (defun mdw-uniquify-alist (&rest alists)
1163 "Return the concatenation of the ALISTS with duplicate elements removed.
1164 The first association with a given key prevails; others are
1165 ignored. The input lists are not modified, although they'll
1166 probably become garbage."
1168 (let ((start-list (cons nil nil)))
1169 (mdw-do-uniquify start-list
1174 (defun mdw-do-uniquify (done end l rest)
1175 "A helper function for mdw-uniquify-alist.
1176 The DONE argument is a list whose first element is `nil'. It
1177 contains the uniquified alist built so far. The leading `nil' is
1178 stripped off at the end of the operation; it's only there so that
1179 DONE always references a cons cell. END refers to the final cons
1180 cell in the DONE list; it is modified in place each time to avoid
1181 the overheads of `append'ing all the time. The L argument is the
1182 alist we're currently processing; the remaining alists are given
1185 ;; There are several different cases to deal with here.
1188 ;; Current list isn't empty. Add the first item to the DONE list if
1189 ;; there's not an item with the same KEY already there.
1190 (l (or (assoc (car (car l)) done)
1192 (setcdr end (cons (car l) nil))
1193 (setq end (cdr end))))
1194 (mdw-do-uniquify done end (cdr l) rest))
1196 ;; The list we were working on is empty. Shunt the next list into the
1197 ;; current list position and go round again.
1198 (rest (mdw-do-uniquify done end (car rest) (cdr rest)))
1200 ;; Everything's done. Remove the leading `nil' from the DONE list and
1201 ;; return it. Finished!
1205 "Insert the current date in a pleasing way."
1207 (insert (save-excursion
1208 (let ((buffer (get-buffer-create "*tmp*")))
1209 (unwind-protect (progn (set-buffer buffer)
1211 (shell-command "date +%Y-%m-%d" t)
1215 (kill-buffer buffer))))))
1217 (defun uuencode (file &optional name)
1218 "UUencodes a file, maybe calling it NAME, into the current buffer."
1219 (interactive "fInput file name: ")
1221 ;; If NAME isn't specified, then guess from the filename.
1225 (or (string-match "[^/]*$" file) 0))))
1226 (print (format "uuencode `%s' `%s'" file name))
1228 ;; Now actually do the thing.
1229 (call-process "uuencode" file t nil name))
1231 (defcustom np-file "~/.np"
1232 "Where the `now-playing' file is."
1236 (defun np (&optional arg)
1237 "Grabs a `now-playing' string."
1241 (goto-char (point-max))
1243 (insert-file-contents np-file)))))
1245 (defun mdw-version-< (ver-a ver-b)
1246 "Answer whether VER-A is strictly earlier than VER-B.
1247 VER-A and VER-B are version numbers, which are strings containing digit
1248 sequences separated by `.'."
1249 (let* ((la (mapcar (lambda (x) (car (read-from-string x)))
1250 (split-string ver-a "\\.")))
1251 (lb (mapcar (lambda (x) (car (read-from-string x)))
1252 (split-string ver-b "\\."))))
1255 (cond ((null la) (throw 'done lb))
1256 ((null lb) (throw 'done nil))
1257 ((< (car la) (car lb)) (throw 'done t))
1258 ((= (car la) (car lb)) (setq la (cdr la) lb (cdr lb)))
1259 (t (throw 'done nil)))))))
1261 (defun mdw-check-autorevert ()
1262 "Sets global-auto-revert-ignore-buffer appropriately for this buffer.
1263 This takes into consideration whether it's been found using
1264 tramp, which seems to get itself into a twist."
1265 (cond ((not (boundp 'global-auto-revert-ignore-buffer))
1267 ((and (buffer-file-name)
1268 (fboundp 'tramp-tramp-file-p)
1269 (tramp-tramp-file-p (buffer-file-name)))
1270 (unless global-auto-revert-ignore-buffer
1271 (setq global-auto-revert-ignore-buffer 'tramp)))
1272 ((eq global-auto-revert-ignore-buffer 'tramp)
1273 (setq global-auto-revert-ignore-buffer nil))))
1275 (defadvice find-file (after mdw-autorevert activate)
1276 (mdw-check-autorevert))
1277 (defadvice write-file (after mdw-autorevert activate)
1278 (mdw-check-autorevert))
1280 (defun mdw-auto-revert ()
1281 "Recheck all of the autorevertable buffers, and update VC modelines."
1283 (let ((auto-revert-check-vc-info t))
1284 (auto-revert-buffers)))
1286 ;;;--------------------------------------------------------------------------
1289 (defadvice dired-maybe-insert-subdir
1290 (around mdw-marked-insertion first activate)
1291 "The DIRNAME may be a list of directory names to insert.
1292 Interactively, if files are marked, then insert all of them.
1293 With a numeric prefix argument, select that many entries near
1294 point; with a non-numeric prefix argument, prompt for listing
1297 (list (dired-get-marked-files nil
1298 (and (integerp current-prefix-arg)
1301 (and current-prefix-arg
1302 (not (integerp current-prefix-arg))
1303 (read-string "Switches for listing: "
1304 (or dired-subdir-switches
1305 dired-actual-switches)))))
1306 (let ((dirs (ad-get-arg 0)))
1307 (dolist (dir (if (listp dirs) dirs (list dirs)))
1311 (defun mdw-dired-run (args &optional syncp)
1312 (interactive (let ((file (dired-get-filename t)))
1313 (list (read-string (format "Arguments for %s: " file))
1314 current-prefix-arg)))
1315 (funcall (if syncp 'shell-command 'async-shell-command)
1316 (concat (shell-quote-argument (dired-get-filename nil))
1319 (defadvice dired-do-flagged-delete
1320 (around mdw-delete-if-prefix-argument activate compile)
1321 (let ((delete-by-moving-to-trash (and (null current-prefix-arg)
1322 delete-by-moving-to-trash)))
1325 (eval-after-load "dired"
1326 '(define-key dired-mode-map "X" 'mdw-dired-run))
1328 ;;;--------------------------------------------------------------------------
1331 (defun mdw-w3m-browse-url (url &optional new-session-p)
1332 "Invoke w3m on the URL in its current window, or at least a different one.
1333 If NEW-SESSION-P, start a new session."
1334 (interactive "sURL: \nP")
1336 (let ((window (selected-window)))
1339 (select-window (or (and (not new-session-p)
1340 (get-buffer-window "*w3m*"))
1342 (if (one-window-p t) (split-window))
1344 (w3m-browse-url url new-session-p))
1345 (select-window window)))))
1347 (eval-after-load 'w3m
1348 '(define-key w3m-mode-map [?\e ?\r] 'w3m-view-this-url-new-session))
1350 (defcustom mdw-good-url-browsers
1351 '(browse-url-mozilla
1353 (w3m . mdw-w3m-browse-url)
1355 "List of good browsers for mdw-good-url-browsers.
1356 Each item is a browser function name, or a cons (CHECK . FUNC).
1357 A symbol FOO stands for (FOO . FOO)."
1358 :type '(repeat (choice function (cons function function))))
1360 (defun mdw-good-url-browser ()
1361 "Return a good URL browser.
1362 Trundle the list of such things, finding the first item for which
1363 CHECK is fboundp, and returning the correponding FUNC."
1364 (let ((bs mdw-good-url-browsers) b check func answer)
1365 (while (and bs (not answer))
1369 (setq check (car b) func (cdr b))
1370 (setq check b func b))
1372 (setq answer func)))
1375 (eval-after-load "w3m-search"
1379 '(("g" "Google" "http://www.google.co.uk/search?q=%s")
1380 ("gd" "Google Directory"
1381 "http://www.google.com/search?cat=gwd/Top&q=%s")
1382 ("gg" "Google Groups" "http://groups.google.com/groups?q=%s")
1383 ("ward" "Ward's wiki" "http://c2.com/cgi/wiki?%s")
1384 ("gi" "Images" "http://images.google.com/images?q=%s")
1386 "http://metalzone.distorted.org.uk/ftp/pub/mirrors/rfc/rfc%s.txt.gz")
1388 "http://en.wikipedia.org/wiki/Special:Search?go=Go&search=%s")
1389 ("imdb" "IMDb" "http://www.imdb.com/Find?%s")
1390 ("nc-wiki" "nCipher wiki"
1391 "http://wiki.ncipher.com/wiki/bin/view/Devel/?topic=%s")
1392 ("map" "Google maps" "http://maps.google.co.uk/maps?q=%s&hl=en")
1393 ("lp" "Launchpad bug by number"
1394 "https://bugs.launchpad.net/bugs/%s")
1395 ("lppkg" "Launchpad bugs by package"
1396 "https://bugs.launchpad.net/%s")
1398 "http://social.msdn.microsoft.com/Search/en-GB/?query=%s&ac=8")
1399 ("debbug" "Debian bug by number"
1400 "http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=%s")
1401 ("debbugpkg" "Debian bugs by package"
1402 "http://bugs.debian.org/cgi-bin/pkgreport.cgi?pkg=%s")
1403 ("ljlogin" "LJ login" "http://www.livejournal.com/login.bml")))
1404 (add-to-list 'w3m-search-engine-alist
1405 (list (cadr item) (caddr item) nil))
1406 (add-to-list 'w3m-uri-replace-alist
1407 (list (concat "\\`" (car item) ":")
1408 'w3m-search-uri-replace
1411 ;;;--------------------------------------------------------------------------
1412 ;;; Paragraph filling.
1414 ;; Useful variables.
1416 (defcustom mdw-fill-prefix nil
1417 "Used by `mdw-line-prefix' and `mdw-fill-paragraph'.
1418 If there's no fill prefix currently set (by the `fill-prefix'
1419 variable) and there's a match from one of the regexps here, it
1420 gets used to set the fill-prefix for the current operation.
1422 The variable is a list of items of the form `PATTERN . PREFIX'; if
1423 the PATTERN matches, the PREFIX is used to set the fill prefix.
1425 A PATTERN is one of the following.
1427 * STRING -- a regular expression, expected to match at point
1428 * (eval . FORM) -- a Lisp form which must evaluate non-nil
1429 * (if COND CONSEQ-PAT ALT-PAT) -- if COND evaluates non-nil, must match
1430 CONSEQ-PAT; otherwise must match ALT-PAT
1431 * (and PATTERN ...) -- must match all of the PATTERNs
1432 * (or PATTERN ...) -- must match at least one PATTERN
1433 * (not PATTERN) -- mustn't match (probably not useful)
1435 A PREFIX is a list of the following kinds of things:
1437 * STRING -- insert a literal string
1438 * (match . N) -- insert the thing matched by bracketed subexpression N
1439 * (pad . N) -- a string of whitespace the same width as subexpression N
1440 * (expr . FORM) -- the result of evaluating FORM
1442 Information about `bracketed subexpressions' comes from the match data,
1443 as modified during matching.")
1445 (make-variable-buffer-local 'mdw-fill-prefix)
1447 (defcustom mdw-hanging-indents
1449 "\\([*o+]\\|-[-#]?\\|[0-9]+\\.\\|\\[[0-9]+\\]\\|([a-zA-Z])\\)"
1452 "Standard regexp matching parts of a hanging indent.
1453 This is mainly useful in `auto-fill-mode'."
1456 ;; Utility functions.
1458 (defun mdw-maybe-tabify (s)
1459 "Tabify or untabify the string S, according to `indent-tabs-mode'."
1460 (let ((tabfun (if indent-tabs-mode #'tabify #'untabify)))
1464 (let ((start (point-min)) (end (point-max)))
1465 (funcall tabfun (point-min) (point-max))
1466 (setq s (buffer-substring (point-min) (1- (point-max)))))))))
1468 (defun mdw-fill-prefix-match-p (pat)
1469 "Return non-nil if PAT matches at the current position."
1470 (cond ((stringp pat) (looking-at pat))
1471 ((not (consp pat)) (error "Unknown pattern item `%S'" pat))
1472 ((eq (car pat) 'eval) (eval (cdr pat)))
1474 (if (or (null (cdr pat))
1478 (error "Invalid `if' pattern `%S'" pat))
1479 (mdw-fill-prefix-match-p (if (eval (cadr pat))
1482 ((eq (car pat) 'and)
1483 (let ((pats (cdr pat))
1486 (or (mdw-fill-prefix-match-p (car pats))
1488 (setq pats (cdr pats)))
1491 (let ((pats (cdr pat))
1494 (or (not (mdw-fill-prefix-match-p (car pats)))
1495 (progn (setq ok t) nil)))
1496 (setq pats (cdr pats)))
1498 ((eq (car pat) 'not)
1499 (if (or (null (cdr pat)) (cddr pat))
1500 (error "Invalid `not' pattern `%S'" pat))
1501 (not (mdw-fill-prefix-match-p (car pats))))
1502 (t (error "Unknown pattern form `%S'" pat))))
1504 (defun mdw-maybe-car (p)
1505 "If P is a pair, return (car P), otherwise just return P."
1506 (if (consp p) (car p) p))
1508 (defun mdw-padding (s)
1509 "Return a string the same width as S but made entirely from whitespace."
1510 (let* ((l (length s)) (i 0) (n (make-string l ? )))
1512 (if (= 9 (aref s i))
1517 (defun mdw-do-prefix-match (m)
1518 "Expand a dynamic prefix match element.
1519 See `mdw-fill-prefix' for details."
1520 (cond ((not (consp m)) (format "%s" m))
1521 ((eq (car m) 'match) (match-string (mdw-maybe-car (cdr m))))
1522 ((eq (car m) 'pad) (mdw-padding (match-string
1523 (mdw-maybe-car (cdr m)))))
1524 ((eq (car m) 'eval) (eval (cdr m)))
1527 (defun mdw-examine-fill-prefixes (l)
1528 "Given a list of dynamic fill prefixes, pick one which matches
1529 context and return the static fill prefix to use. Point must be
1530 at the start of a line, and match data must be saved."
1532 (while (cond ((null l) nil)
1533 ((mdw-fill-prefix-match-p (caar l))
1537 (mapcar #'mdw-do-prefix-match
1543 (defun mdw-choose-dynamic-fill-prefix ()
1544 "Work out the dynamic fill prefix based on the variable `mdw-fill-prefix'."
1545 (cond ((and fill-prefix (not (string= fill-prefix ""))) fill-prefix)
1546 ((not mdw-fill-prefix) fill-prefix)
1550 (mdw-examine-fill-prefixes mdw-fill-prefix))))))
1552 (defadvice do-auto-fill (around mdw-dynamic-fill-prefix () activate compile)
1553 "Handle auto-filling, working out a dynamic fill prefix in the
1554 case where there isn't a sensible static one."
1555 (let ((fill-prefix (mdw-choose-dynamic-fill-prefix)))
1558 (defun mdw-fill-paragraph ()
1559 "Fill paragraph, getting a dynamic fill prefix."
1561 (let ((fill-prefix (mdw-choose-dynamic-fill-prefix)))
1562 (fill-paragraph nil)))
1564 (defun mdw-point-within-string-p ()
1565 "Return non-nil if point is within a string."
1566 (let ((state (syntax-ppss)))
1569 (defun mdw-standard-fill-prefix (rx &optional mat)
1570 "Set the dynamic fill prefix, handling standard hanging indents and stuff.
1571 This is just a short-cut for setting the thing by hand, and by
1572 design it doesn't cope with anything approximating a complicated
1574 (setq mdw-fill-prefix
1575 `(((if (mdw-point-within-string-p)
1576 ,(concat "\\(\\s-*\\)" mdw-hanging-indents)
1577 ,(concat rx mdw-hanging-indents))
1579 (pad . ,(or mat 2))))))
1581 ;;;--------------------------------------------------------------------------
1584 ;; Teach PostScript about a condensed variant of Courier. I'm using 85% of
1585 ;; the usual width, which happens to match `mdwfonts', and David Carlisle's
1586 ;; `pslatex'. (Once upon a time, I used 80%, but decided consistency with
1587 ;; `pslatex' was useful.)
1588 (setq ps-user-defined-prologue "
1589 /CourierCondensed /Courier
1590 /CourierCondensed-Bold /Courier-Bold
1591 /CourierCondensed-Oblique /Courier-Oblique
1592 /CourierCondensed-BoldOblique /Courier-BoldOblique
1593 4 { findfont [0.85 0 0 1 0 0] makefont definefont pop } repeat
1596 ;; Hack `ps-print''s settings.
1597 (eval-after-load 'ps-print
1600 ;; Notice that the comment-delimiters should be in italics too.
1601 (pushnew 'font-lock-comment-delimiter-face ps-italic-faces)
1603 ;; Select more suitable colours for the main kinds of tokens. The
1604 ;; colours set on the Emacs faces are chosen for use against a dark
1605 ;; background, and work very badly on white paper.
1606 (ps-extend-face '(font-lock-comment-face "darkgreen" nil italic))
1607 (ps-extend-face '(font-lock-comment-delimiter-face "darkgreen" nil italic))
1608 (ps-extend-face '(font-lock-string-face "RoyalBlue4" nil))
1609 (ps-extend-face '(mdw-punct-face "sienna" nil))
1610 (ps-extend-face '(mdw-number-face "OrangeRed3" nil))
1612 ;; Teach `ps-print' about my condensed varsions of Courier.
1613 (setq ps-font-info-database
1614 (append '((CourierCondensed
1615 (fonts (normal . "CourierCondensed")
1616 (bold . "CourierCondensed-Bold")
1617 (italic . "CourierCondensed-Oblique")
1618 (bold-italic . "CourierCondensed-BoldOblique"))
1620 (line-height . 10.55)
1622 (avg-char-width . 5.1)))
1623 (remove* 'CourierCondensed ps-font-info-database
1626 ;; Arrange to strip overlays from the buffer before we print . This will
1627 ;; prevent `flyspell' from interfering with the printout. (It would be less
1628 ;; bad if `ps-print' could merge the `flyspell' overlay face with the
1629 ;; underlying `font-lock' face, but it can't (and that seems hard). So
1630 ;; instead we have this hack.
1632 ;; The basic trick is to copy the relevant text from the buffer being printed
1633 ;; into a temporary buffer and... just print that. The text properties come
1634 ;; with the text and end up in the new buffer, and the overlays get lost
1635 ;; along the way. Only problem is that the headers identifying the file
1636 ;; being printed get confused, so remember the original buffer and reinstate
1637 ;; it when constructing the headers.
1638 (defvar mdw-printing-buffer)
1640 (defadvice ps-generate-header
1641 (around mdw-use-correct-buffer () activate compile)
1642 "Print the correct name of the buffer being printed."
1643 (with-current-buffer mdw-printing-buffer
1646 (defadvice ps-generate
1647 (around mdw-strip-overlays (buffer from to genfunc) activate compile)
1648 "Strip overlays -- in particular, from `flyspell' -- before printout."
1650 (let ((mdw-printing-buffer buffer))
1651 (insert-buffer-substring buffer from to)
1652 (ad-set-arg 0 (current-buffer))
1653 (ad-set-arg 1 (point-min))
1654 (ad-set-arg 2 (point-max))
1657 ;;;--------------------------------------------------------------------------
1658 ;;; Other common declarations.
1660 ;; Common mode settings.
1662 (defcustom mdw-auto-indent t
1663 "Whether to indent automatically after a newline."
1667 (defun mdw-whitespace-mode (&optional arg)
1668 "Turn on/off whitespace mode, but don't highlight trailing space."
1670 (when (and (boundp 'whitespace-style)
1671 (fboundp 'whitespace-mode))
1672 (let ((whitespace-style (remove 'trailing whitespace-style)))
1673 (whitespace-mode arg))
1674 (setq show-trailing-whitespace whitespace-mode)))
1676 (defvar mdw-do-misc-mode-hacking nil)
1678 (defun mdw-misc-mode-config ()
1679 (and mdw-auto-indent
1680 (cond ((eq major-mode 'lisp-mode)
1681 (local-set-key "\C-m" 'mdw-indent-newline-and-indent))
1682 ((derived-mode-p 'slime-repl-mode 'asm-mode 'comint-mode)
1685 (local-set-key "\C-m" 'newline-and-indent))))
1686 (set (make-local-variable 'mdw-do-misc-mode-hacking) t)
1687 (local-set-key [C-return] 'newline)
1688 (make-local-variable 'page-delimiter)
1689 (setq page-delimiter (concat "^" "\f"
1693 "\\(" " " ".*" " " "\\)?"
1697 (setq comment-column 40)
1699 (setq fill-column mdw-text-width)
1700 (flyspell-prog-mode)
1701 (and (fboundp 'gtags-mode)
1703 (if (fboundp 'hs-minor-mode)
1704 (trap (hs-minor-mode t))
1705 (outline-minor-mode t))
1707 (trap (turn-on-font-lock)))
1709 (defun mdw-post-local-vars-misc-mode-config ()
1710 (setq whitespace-line-column mdw-text-width)
1711 (when (and mdw-do-misc-mode-hacking
1712 (not buffer-read-only))
1713 (setq show-trailing-whitespace t)
1714 (mdw-whitespace-mode 1)))
1715 (add-hook 'hack-local-variables-hook 'mdw-post-local-vars-misc-mode-config)
1717 (defmacro mdw-advise-update-angry-fruit-salad (&rest funcs)
1718 `(progn ,@(mapcar (lambda (func)
1720 (after mdw-angry-fruit-salad activate)
1721 (when mdw-do-misc-mode-hacking
1722 (setq show-trailing-whitespace
1723 (not buffer-read-only))
1724 (mdw-whitespace-mode (if buffer-read-only 0 1)))))
1726 (mdw-advise-update-angry-fruit-salad toggle-read-only
1732 (eval-after-load 'gtags
1734 (dolist (key '([mouse-2] [mouse-3]))
1735 (define-key gtags-mode-map key nil))
1736 (define-key gtags-mode-map [C-S-mouse-2] 'gtags-find-tag-by-event)
1737 (define-key gtags-select-mode-map [C-S-mouse-2]
1738 'gtags-select-tag-by-event)
1739 (dolist (map (list gtags-mode-map gtags-select-mode-map))
1740 (define-key map [C-S-mouse-3] 'gtags-pop-stack))))
1742 ;; Backup file handling.
1744 (defcustom mdw-backup-disable-regexps nil
1745 "List of regular expressions: if a file name matches any of
1746 these then the file is not backed up."
1747 :type '(repeat regexp))
1749 (defun mdw-backup-enable-predicate (name)
1750 "[mdw]'s default backup predicate.
1751 Allows a backup if the standard predicate would allow it, and it
1752 doesn't match any of the regular expressions in
1753 `mdw-backup-disable-regexps'."
1754 (and (normal-backup-enable-predicate name)
1755 (let ((answer t) (list mdw-backup-disable-regexps))
1758 (if (string-match (car list) name)
1760 (setq list (cdr list)))
1762 (setq backup-enable-predicate 'mdw-backup-enable-predicate)
1766 (defun mdw-last-one-out-turn-off-the-lights (frame)
1767 "Disconnect from an X display if this was the last frame on that display."
1768 (let ((frame-display (frame-parameter frame 'display)))
1769 (when (and frame-display
1770 (eq window-system 'x)
1771 (not (some (lambda (fr)
1772 (and (not (eq fr frame))
1773 (string= (frame-parameter fr 'display)
1776 (run-with-idle-timer 0 nil #'x-close-connection frame-display))))
1777 (add-hook 'delete-frame-functions 'mdw-last-one-out-turn-off-the-lights)
1779 ;;;--------------------------------------------------------------------------
1780 ;;; Fullscreen-ness.
1782 (defcustom mdw-full-screen-parameters
1783 '((menu-bar-lines . 0)
1784 ;;(vertical-scroll-bars . nil)
1786 "Frame parameters to set when making a frame fullscreen."
1787 :type '(alist :key-type symbol))
1789 (defcustom mdw-full-screen-save
1791 "Extra frame parameters to save when setting fullscreen."
1792 :type '(repeat symbol))
1794 (defun mdw-toggle-full-screen (&optional frame)
1795 "Show the FRAME fullscreen."
1798 (cond ((frame-parameter frame 'fullscreen)
1799 (set-frame-parameter frame 'fullscreen nil)
1800 (modify-frame-parameters
1802 (or (frame-parameter frame 'mdw-full-screen-saved)
1803 (mapcar (lambda (assoc)
1804 (assq (car assoc) default-frame-alist))
1805 mdw-full-screen-parameters))))
1807 (let ((saved (mapcar (lambda (param)
1808 (cons param (frame-parameter frame param)))
1809 (append (mapcar #'car
1810 mdw-full-screen-parameters)
1811 mdw-full-screen-save))))
1812 (set-frame-parameter frame 'mdw-full-screen-saved saved))
1813 (modify-frame-parameters frame mdw-full-screen-parameters)
1814 (set-frame-parameter frame 'fullscreen 'fullboth)))))
1816 ;;;--------------------------------------------------------------------------
1817 ;;; General fontification.
1819 (make-face 'mdw-virgin-face)
1821 (defmacro mdw-define-face (name &rest body)
1822 "Define a face, and make sure it's actually set as the definition."
1826 (copy-face 'mdw-virgin-face ',name)
1827 (defvar ,name ',name)
1828 (put ',name 'face-defface-spec ',body)
1829 (face-spec-set ',name ',body nil)))
1831 (mdw-define-face default
1832 (((type w32)) :family "courier new" :height 85)
1833 (((type x)) :family "6x13" :foundry "trad" :height 130)
1834 (((type color)) :foreground "white" :background "black")
1836 (mdw-define-face fixed-pitch
1837 (((type w32)) :family "courier new" :height 85)
1838 (((type x)) :family "6x13" :foundry "trad" :height 130)
1839 (t :foreground "white" :background "black"))
1840 (mdw-define-face fixed-pitch-serif
1841 (((type w32)) :family "courier new" :height 85 :weight bold)
1842 (((type x)) :family "6x13" :foundry "trad" :height 130 :weight bold)
1843 (t :foreground "white" :background "black" :weight bold))
1844 (mdw-define-face variable-pitch
1845 (((type x)) :family "helvetica" :height 120))
1846 (mdw-define-face region
1847 (((min-colors 64)) :background "grey30")
1848 (((class color)) :background "blue")
1849 (t :inverse-video t))
1850 (mdw-define-face match
1851 (((class color)) :background "blue")
1852 (t :inverse-video t))
1853 (mdw-define-face mc/cursor-face
1854 (((class color)) :background "red")
1855 (t :inverse-video t))
1856 (mdw-define-face minibuffer-prompt
1858 (mdw-define-face mode-line
1859 (((class color)) :foreground "blue" :background "yellow"
1860 :box (:line-width 1 :style released-button))
1861 (t :inverse-video t))
1862 (mdw-define-face mode-line-inactive
1863 (((class color)) :foreground "yellow" :background "blue"
1864 :box (:line-width 1 :style released-button))
1865 (t :inverse-video t))
1866 (mdw-define-face nobreak-space
1868 (t :inherit escape-glyph :underline t))
1869 (mdw-define-face scroll-bar
1870 (t :foreground "black" :background "lightgrey"))
1871 (mdw-define-face fringe
1872 (t :foreground "yellow"))
1873 (mdw-define-face show-paren-match
1874 (((min-colors 64)) :background "darkgreen")
1875 (((class color)) :background "green")
1877 (mdw-define-face show-paren-mismatch
1878 (((class color)) :background "red")
1879 (t :inverse-video t))
1880 (mdw-define-face highlight
1881 (((min-colors 64)) :background "DarkSeaGreen4")
1882 (((class color)) :background "cyan")
1883 (t :inverse-video t))
1885 (mdw-define-face viper-minibuffer-emacs (t nil))
1886 (mdw-define-face viper-minibuffer-insert (t nil))
1887 (mdw-define-face viper-minibuffer-vi (t nil))
1888 (mdw-define-face viper-replace-overlay
1889 (((min-colors 64)) :background "darkred")
1890 (((class color)) :background "red")
1891 (t :inverse-video t))
1892 (mdw-define-face viper-search (t :inherit isearch))
1894 (mdw-define-face compilation-error
1895 (((class color)) :foreground "red" :weight bold)
1897 (mdw-define-face compilation-warning
1898 (((class color)) :foreground "orange" :weight bold)
1900 (mdw-define-face compilation-info
1901 (((class color)) :foreground "green" :weight bold)
1903 (mdw-define-face compilation-line-number
1905 (mdw-define-face compilation-column-number
1906 (((min-colors 64)) :foreground "lightgrey"))
1907 (setq compilation-message-face 'mdw-virgin-face)
1908 (setq compilation-enter-directory-face 'font-lock-comment-face)
1909 (setq compilation-leave-directory-face 'font-lock-comment-face)
1911 (mdw-define-face holiday-face
1912 (t :background "red"))
1913 (mdw-define-face calendar-today-face
1914 (t :foreground "yellow" :weight bold))
1916 (mdw-define-face comint-highlight-prompt
1918 (mdw-define-face comint-highlight-input
1921 (mdw-define-face Man-underline
1922 (((type tty)) :underline t)
1925 (mdw-define-face ido-subdir
1926 (t :foreground "cyan" :weight bold))
1928 (mdw-define-face dired-directory
1929 (t :foreground "cyan" :weight bold))
1930 (mdw-define-face dired-symlink
1931 (t :foreground "cyan"))
1932 (mdw-define-face dired-perm-write
1935 (mdw-define-face trailing-whitespace
1936 (((class color)) :background "red")
1937 (t :inverse-video t))
1938 (mdw-define-face whitespace-line
1939 (((class color)) :background "darkred")
1940 (t :inverse-video t))
1941 (mdw-define-face mdw-punct-face
1942 (((min-colors 64)) :foreground "burlywood2")
1943 (((class color)) :foreground "yellow"))
1944 (mdw-define-face mdw-number-face
1945 (t :foreground "yellow"))
1946 (mdw-define-face mdw-trivial-face)
1947 (mdw-define-face font-lock-function-name-face
1949 (mdw-define-face font-lock-keyword-face
1951 (mdw-define-face font-lock-constant-face
1953 (mdw-define-face font-lock-builtin-face
1955 (mdw-define-face font-lock-type-face
1956 (t :weight bold :slant italic))
1957 (mdw-define-face font-lock-reference-face
1959 (mdw-define-face font-lock-variable-name-face
1961 (mdw-define-face font-lock-comment-face
1962 (((min-colors 64)) :slant italic :foreground "SeaGreen1")
1963 (((class color)) :foreground "green")
1965 (mdw-define-face font-lock-comment-delimiter-face
1966 (t :inherit font-lock-comment-face))
1967 (mdw-define-face font-lock-string-face
1968 (((min-colors 64)) :foreground "SkyBlue1")
1969 (((class color)) :foreground "cyan")
1971 (mdw-define-face font-lock-doc-face
1972 (t :inherit font-lock-string-face))
1974 (mdw-define-face message-separator
1975 (t :background "red" :foreground "white" :weight bold))
1976 (mdw-define-face message-cited-text
1977 (default :slant italic)
1978 (((min-colors 64)) :foreground "SkyBlue1")
1979 (((class color)) :foreground "cyan"))
1980 (mdw-define-face message-header-cc
1981 (default :slant italic)
1982 (((min-colors 64)) :foreground "SeaGreen1")
1983 (((class color)) :foreground "green"))
1984 (mdw-define-face message-header-newsgroups
1985 (default :slant italic)
1986 (((min-colors 64)) :foreground "SeaGreen1")
1987 (((class color)) :foreground "green"))
1988 (mdw-define-face message-header-subject
1989 (((min-colors 64)) :foreground "SeaGreen1")
1990 (((class color)) :foreground "green"))
1991 (mdw-define-face message-header-to
1992 (((min-colors 64)) :foreground "SeaGreen1")
1993 (((class color)) :foreground "green"))
1994 (mdw-define-face message-header-xheader
1995 (default :slant italic)
1996 (((min-colors 64)) :foreground "SeaGreen1")
1997 (((class color)) :foreground "green"))
1998 (mdw-define-face message-header-other
1999 (default :slant italic)
2000 (((min-colors 64)) :foreground "SeaGreen1")
2001 (((class color)) :foreground "green"))
2002 (mdw-define-face message-header-name
2003 (default :weight bold)
2004 (((min-colors 64)) :foreground "SeaGreen1")
2005 (((class color)) :foreground "green"))
2007 (mdw-define-face which-func
2010 (mdw-define-face gnus-header-name
2011 (default :weight bold)
2012 (((min-colors 64)) :foreground "SeaGreen1")
2013 (((class color)) :foreground "green"))
2014 (mdw-define-face gnus-header-subject
2015 (((min-colors 64)) :foreground "SeaGreen1")
2016 (((class color)) :foreground "green"))
2017 (mdw-define-face gnus-header-from
2018 (((min-colors 64)) :foreground "SeaGreen1")
2019 (((class color)) :foreground "green"))
2020 (mdw-define-face gnus-header-to
2021 (((min-colors 64)) :foreground "SeaGreen1")
2022 (((class color)) :foreground "green"))
2023 (mdw-define-face gnus-header-content
2024 (default :slant italic)
2025 (((min-colors 64)) :foreground "SeaGreen1")
2026 (((class color)) :foreground "green"))
2028 (mdw-define-face gnus-cite-1
2029 (((min-colors 64)) :foreground "SkyBlue1")
2030 (((class color)) :foreground "cyan"))
2031 (mdw-define-face gnus-cite-2
2032 (((min-colors 64)) :foreground "RoyalBlue2")
2033 (((class color)) :foreground "blue"))
2034 (mdw-define-face gnus-cite-3
2035 (((min-colors 64)) :foreground "MediumOrchid")
2036 (((class color)) :foreground "magenta"))
2037 (mdw-define-face gnus-cite-4
2038 (((min-colors 64)) :foreground "firebrick2")
2039 (((class color)) :foreground "red"))
2040 (mdw-define-face gnus-cite-5
2041 (((min-colors 64)) :foreground "burlywood2")
2042 (((class color)) :foreground "yellow"))
2043 (mdw-define-face gnus-cite-6
2044 (((min-colors 64)) :foreground "SeaGreen1")
2045 (((class color)) :foreground "green"))
2046 (mdw-define-face gnus-cite-7
2047 (((min-colors 64)) :foreground "SlateBlue1")
2048 (((class color)) :foreground "cyan"))
2049 (mdw-define-face gnus-cite-8
2050 (((min-colors 64)) :foreground "RoyalBlue2")
2051 (((class color)) :foreground "blue"))
2052 (mdw-define-face gnus-cite-9
2053 (((min-colors 64)) :foreground "purple2")
2054 (((class color)) :foreground "magenta"))
2055 (mdw-define-face gnus-cite-10
2056 (((min-colors 64)) :foreground "DarkOrange2")
2057 (((class color)) :foreground "red"))
2058 (mdw-define-face gnus-cite-11
2059 (t :foreground "grey"))
2061 (mdw-define-face gnus-emphasis-underline
2062 (((type tty)) :underline t)
2065 (mdw-define-face diff-header
2067 (mdw-define-face diff-index
2069 (mdw-define-face diff-file-header
2071 (mdw-define-face diff-hunk-header
2072 (((min-colors 64)) :foreground "SkyBlue1")
2073 (((class color)) :foreground "cyan"))
2074 (mdw-define-face diff-function
2075 (default :weight bold)
2076 (((min-colors 64)) :foreground "SkyBlue1")
2077 (((class color)) :foreground "cyan"))
2078 (mdw-define-face diff-header
2079 (((min-colors 64)) :background "grey10"))
2080 (mdw-define-face diff-added
2081 (((class color)) :foreground "green"))
2082 (mdw-define-face diff-removed
2083 (((class color)) :foreground "red"))
2084 (mdw-define-face diff-context
2086 (mdw-define-face diff-refine-change
2087 (((min-colors 64)) :background "RoyalBlue4")
2089 (mdw-define-face diff-refine-removed
2090 (((min-colors 64)) :background "#500")
2092 (mdw-define-face diff-refine-added
2093 (((min-colors 64)) :background "#050")
2096 (setq ediff-force-faces t)
2097 (mdw-define-face ediff-current-diff-A
2098 (((min-colors 64)) :background "darkred")
2099 (((class color)) :background "red")
2100 (t :inverse-video t))
2101 (mdw-define-face ediff-fine-diff-A
2102 (((min-colors 64)) :background "red3")
2103 (((class color)) :inverse-video t)
2104 (t :inverse-video nil))
2105 (mdw-define-face ediff-even-diff-A
2106 (((min-colors 64)) :background "#300"))
2107 (mdw-define-face ediff-odd-diff-A
2108 (((min-colors 64)) :background "#300"))
2109 (mdw-define-face ediff-current-diff-B
2110 (((min-colors 64)) :background "darkgreen")
2111 (((class color)) :background "magenta")
2112 (t :inverse-video t))
2113 (mdw-define-face ediff-fine-diff-B
2114 (((min-colors 64)) :background "green4")
2115 (((class color)) :inverse-video t)
2116 (t :inverse-video nil))
2117 (mdw-define-face ediff-even-diff-B
2118 (((min-colors 64)) :background "#020"))
2119 (mdw-define-face ediff-odd-diff-B
2120 (((min-colors 64)) :background "#020"))
2121 (mdw-define-face ediff-current-diff-C
2122 (((min-colors 64)) :background "darkblue")
2123 (((class color)) :background "blue")
2124 (t :inverse-video t))
2125 (mdw-define-face ediff-fine-diff-C
2126 (((min-colors 64)) :background "blue1")
2127 (((class color)) :inverse-video t)
2128 (t :inverse-video nil))
2129 (mdw-define-face ediff-even-diff-C
2130 (((min-colors 64)) :background "#004"))
2131 (mdw-define-face ediff-odd-diff-C
2132 (((min-colors 64)) :background "#004"))
2133 (mdw-define-face ediff-current-diff-Ancestor
2134 (((min-colors 64)) :background "#630")
2135 (((class color)) :background "blue")
2136 (t :inverse-video t))
2137 (mdw-define-face ediff-even-diff-Ancestor
2138 (((min-colors 64)) :background "#320"))
2139 (mdw-define-face ediff-odd-diff-Ancestor
2140 (((min-colors 64)) :background "#320"))
2142 (mdw-define-face magit-hash
2143 (((min-colors 64)) :foreground "grey40")
2144 (((class color)) :foreground "blue"))
2145 (mdw-define-face magit-diff-hunk-heading
2146 (((min-colors 64)) :foreground "grey70" :background "grey25")
2147 (((class color)) :foreground "yellow"))
2148 (mdw-define-face magit-diff-hunk-heading-highlight
2149 (((min-colors 64)) :foreground "grey70" :background "grey35")
2150 (((class color)) :foreground "yellow" :background "blue"))
2151 (mdw-define-face magit-diff-added
2152 (((min-colors 64)) :foreground "#ddffdd" :background "#335533")
2153 (((class color)) :foreground "green"))
2154 (mdw-define-face magit-diff-added-highlight
2155 (((min-colors 64)) :foreground "#cceecc" :background "#336633")
2156 (((class color)) :foreground "green" :background "blue"))
2157 (mdw-define-face magit-diff-removed
2158 (((min-colors 64)) :foreground "#ffdddd" :background "#553333")
2159 (((class color)) :foreground "red"))
2160 (mdw-define-face magit-diff-removed-highlight
2161 (((min-colors 64)) :foreground "#eecccc" :background "#663333")
2162 (((class color)) :foreground "red" :background "blue"))
2163 (mdw-define-face magit-blame-heading
2164 (((min-colors 64)) :foreground "white" :background "grey25"
2165 :weight normal :slant normal)
2166 (((class color)) :foreground "white" :background "blue"
2167 :weight normal :slant normal))
2168 (mdw-define-face magit-blame-name
2169 (t :inherit magit-blame-heading :slant italic))
2170 (mdw-define-face magit-blame-date
2171 (((min-colors 64)) :inherit magit-blame-heading :foreground "grey60")
2172 (((class color)) :inherit magit-blame-heading :foreground "cyan"))
2173 (mdw-define-face magit-blame-summary
2174 (t :inherit magit-blame-heading :weight bold))
2176 (mdw-define-face dylan-header-background
2177 (((min-colors 64)) :background "NavyBlue")
2178 (((class color)) :background "blue"))
2180 (mdw-define-face erc-input-face
2181 (t :foreground "red"))
2183 (mdw-define-face woman-bold
2185 (mdw-define-face woman-italic
2188 (eval-after-load "rst"
2190 (mdw-define-face rst-level-1-face
2191 (t :foreground "SkyBlue1" :weight bold))
2192 (mdw-define-face rst-level-2-face
2193 (t :foreground "SeaGreen1" :weight bold))
2194 (mdw-define-face rst-level-3-face
2196 (mdw-define-face rst-level-4-face
2198 (mdw-define-face rst-level-5-face
2200 (mdw-define-face rst-level-6-face
2203 (mdw-define-face p4-depot-added-face
2204 (t :foreground "green"))
2205 (mdw-define-face p4-depot-branch-op-face
2206 (t :foreground "yellow"))
2207 (mdw-define-face p4-depot-deleted-face
2208 (t :foreground "red"))
2209 (mdw-define-face p4-depot-unmapped-face
2210 (t :foreground "SkyBlue1"))
2211 (mdw-define-face p4-diff-change-face
2212 (t :foreground "yellow"))
2213 (mdw-define-face p4-diff-del-face
2214 (t :foreground "red"))
2215 (mdw-define-face p4-diff-file-face
2216 (t :foreground "SkyBlue1"))
2217 (mdw-define-face p4-diff-head-face
2218 (t :background "grey10"))
2219 (mdw-define-face p4-diff-ins-face
2220 (t :foreground "green"))
2222 (mdw-define-face w3m-anchor-face
2223 (t :foreground "SkyBlue1" :underline t))
2224 (mdw-define-face w3m-arrived-anchor-face
2225 (t :foreground "SkyBlue1" :underline t))
2227 (mdw-define-face whizzy-slice-face
2228 (t :background "grey10"))
2229 (mdw-define-face whizzy-error-face
2230 (t :background "darkred"))
2232 ;; Ellipses used to indicate hidden text (and similar).
2233 (mdw-define-face mdw-ellipsis-face
2234 (((type tty)) :foreground "blue") (t :foreground "grey60"))
2235 (let ((dollar (make-glyph-code ?$ 'mdw-ellipsis-face))
2236 (backslash (make-glyph-code ?\\ 'mdw-ellipsis-face))
2237 (dot (make-glyph-code ?. 'mdw-ellipsis-face))
2238 (bar (make-glyph-code ?| mdw-ellipsis-face)))
2239 (set-display-table-slot standard-display-table 0 dollar)
2240 (set-display-table-slot standard-display-table 1 backslash)
2241 (set-display-table-slot standard-display-table 4
2242 (vector dot dot dot))
2243 (set-display-table-slot standard-display-table 5 bar))
2245 ;;;--------------------------------------------------------------------------
2248 (mdw-define-face mdw-point-overlay-face
2250 (((min-colors 64)) :background "darkblue")
2251 (((class color)) :background "blue")
2252 (((type tty) (class mono)) :inverse-video t))
2254 (defcustom mdw-point-overlay-fringe-display '(vertical-bar . vertical-bar)
2255 "Bitmaps to display in the left and right fringes in the current line."
2256 :type '(cons symbol symbol))
2258 (defun mdw-configure-point-overlay ()
2259 (let ((ov (make-overlay 0 0)))
2260 (overlay-put ov 'priority 0)
2261 (let* ((fringe (or mdw-point-overlay-fringe-display (cons nil nil)))
2262 (left (car fringe)) (right (cdr fringe))
2266 (put-text-property 0 1 'display `(left-fringe ,left) ss)
2267 (setq s (concat s ss))))
2270 (put-text-property 0 1 'display `(right-fringe ,right) ss)
2271 (setq s (concat s ss))))
2272 (when (or left right)
2273 (overlay-put ov 'before-string s)))
2274 (overlay-put ov 'face 'mdw-point-overlay-face)
2278 (defvar mdw-point-overlay (mdw-configure-point-overlay)
2279 "An overlay used for showing where point is in the selected window.")
2280 (defun mdw-reconfigure-point-overlay ()
2282 (setq mdw-point-overlay (mdw-configure-point-overlay)))
2284 (defun mdw-remove-point-overlay ()
2285 "Remove the current-point overlay."
2286 (delete-overlay mdw-point-overlay))
2288 (defun mdw-update-point-overlay ()
2289 "Mark the current point position with an overlay."
2290 (if (not mdw-point-overlay-mode)
2291 (mdw-remove-point-overlay)
2292 (overlay-put mdw-point-overlay 'window (selected-window))
2293 (move-overlay mdw-point-overlay
2294 (line-beginning-position)
2295 (+ (line-end-position) 1))))
2297 (defvar mdw-point-overlay-buffers nil
2298 "List of buffers using `mdw-point-overlay-mode'.")
2300 (define-minor-mode mdw-point-overlay-mode
2301 "Indicate current line with an overlay."
2303 (let ((buffer (current-buffer)))
2304 (setq mdw-point-overlay-buffers
2305 (mapcan (lambda (buf)
2306 (if (and (buffer-live-p buf)
2307 (not (eq buf buffer)))
2309 mdw-point-overlay-buffers))
2310 (if mdw-point-overlay-mode
2311 (setq mdw-point-overlay-buffers
2312 (cons buffer mdw-point-overlay-buffers))))
2313 (cond (mdw-point-overlay-buffers
2314 (add-hook 'pre-command-hook 'mdw-remove-point-overlay)
2315 (add-hook 'post-command-hook 'mdw-update-point-overlay))
2317 (mdw-remove-point-overlay)
2318 (remove-hook 'pre-command-hook 'mdw-remove-point-overlay)
2319 (remove-hook 'post-command-hook 'mdw-update-point-overlay))))
2321 (define-globalized-minor-mode mdw-global-point-overlay-mode
2322 mdw-point-overlay-mode
2323 (lambda () (if (not (minibufferp)) (mdw-point-overlay-mode t))))
2325 (defvar mdw-terminal-title-alist nil)
2326 (defun mdw-update-terminal-title ()
2327 (when (let ((term (frame-parameter nil 'tty-type)))
2328 (and term (string-match "^xterm" term)))
2329 (let* ((tty (frame-parameter nil 'tty))
2330 (old (assoc tty mdw-terminal-title-alist))
2331 (new (format-mode-line frame-title-format)))
2332 (unless (and old (equal (cdr old) new))
2333 (if old (rplacd old new)
2334 (setq mdw-terminal-title-alist
2335 (cons (cons tty new) mdw-terminal-title-alist)))
2336 (send-string-to-terminal (concat "\e]2;" new "\e\\"))))))
2338 (add-hook 'post-command-hook 'mdw-update-terminal-title)
2340 ;;;--------------------------------------------------------------------------
2343 (defvar mdw-ediff-previous-windows)
2344 (defun mdw-ediff-setup ()
2345 (setq mdw-ediff-previous-windows (current-window-configuration)))
2346 (defun mdw-ediff-suspend-or-quit ()
2347 (set-window-configuration mdw-ediff-previous-windows))
2348 (add-hook 'ediff-before-setup-hook 'mdw-ediff-setup)
2349 (add-hook 'ediff-quit-hook 'mdw-ediff-suspend-or-quit t)
2350 (add-hook 'ediff-suspend-hook 'mdw-ediff-suspend-or-quit t)
2352 ;;;--------------------------------------------------------------------------
2353 ;;; C programming configuration.
2355 ;; Make C indentation nice.
2357 (defun mdw-c-lineup-arglist (langelem)
2358 "Hack for DWIMmery in c-lineup-arglist."
2360 (c-block-in-arglist-dwim (c-langelem-2nd-pos c-syntactic-element)))
2362 (c-lineup-arglist langelem)))
2364 (defun mdw-c-indent-extern-mumble (langelem)
2365 "Indent `extern \"...\" {' lines."
2367 (back-to-indentation)
2369 "\\s-*\\<extern\\>\\s-*\"\\([^\\\\\"]+\\|\\.\\)*\"\\s-*{")
2373 (defun mdw-c-indent-arglist-nested (langelem)
2374 "Indent continued argument lists.
2375 If we've nested more than one argument list, then only introduce a single
2376 indentation anyway."
2377 (let ((context c-syntactic-context)
2378 (pos (c-langelem-2nd-pos c-syntactic-element))
2379 (should-indent-p t))
2381 (eq (caar context) 'arglist-cont-nonempty))
2382 (when (and (= (caddr (pop context)) pos)
2384 (memq (caar context) '(arglist-intro
2385 arglist-cont-nonempty)))
2386 (setq should-indent-p nil)))
2387 (if should-indent-p '+ 0)))
2389 (defvar mdw-define-c-styles-hook nil
2390 "Hook run when `cc-mode' starts up to define styles.")
2392 (defun mdw-merge-style-alists (first second)
2394 (dolist (item first)
2395 (let ((key (car item)) (value (cdr item)))
2396 (if (let* ((key-name (symbol-name key))
2397 (key-len (length key-name)))
2399 (string= (subseq key-name (- key-len 6)) "-alist")))
2401 (mdw-merge-style-alists value
2402 (cdr (assoc key second))))
2404 (push item output))))
2405 (dolist (item second)
2406 (unless (assoc (car item) first)
2407 (push item output)))
2410 (defmacro* mdw-define-c-style (name (&optional parent) &rest assocs)
2411 "Define a C style, called NAME (a symbol) based on PARENT, setting ASSOCs.
2412 A function, named `mdw-define-c-style/NAME', is defined to actually install
2413 the style using `c-add-style', and added to the hook
2414 `mdw-define-c-styles-hook'. If CC Mode is already loaded, then the style is
2416 (declare (indent defun))
2417 (let* ((name-string (symbol-name name))
2418 (var (intern (concat "mdw-c-style/" name-string)))
2419 (func (intern (concat "mdw-define-c-style/" name-string))))
2424 (let ((parent-list (intern (concat "mdw-c-style/"
2425 (symbol-name parent)))))
2426 `(mdw-merge-style-alists ',assocs ,parent-list))))
2427 (defun ,func () (c-add-style ,name-string ,var))
2428 (and (featurep 'cc-mode) (,func))
2429 (add-hook 'mdw-define-c-styles-hook ',func)
2432 (eval-after-load "cc-mode"
2433 '(run-hooks 'mdw-define-c-styles-hook))
2435 (mdw-define-c-style mdw-c ()
2436 (c-basic-offset . 2)
2437 (comment-column . 40)
2438 (c-class-key . "class")
2439 (c-backslash-column . 72)
2440 (c-label-minimum-indentation . 0)
2441 (c-offsets-alist (substatement-open . (add 0 c-indent-one-line-block))
2442 (defun-open . (add 0 c-indent-one-line-block))
2443 (arglist-cont-nonempty . mdw-c-lineup-arglist)
2444 (topmost-intro . mdw-c-indent-extern-mumble)
2445 (cpp-define-intro . 0)
2447 (inextern-lang . [0])
2453 (statement-cont . +)
2454 (statement-case-intro . +)))
2456 (mdw-define-c-style mdw-trustonic-c (mdw-c)
2457 (c-basic-offset . 4)
2458 (c-offsets-alist (access-label . -2)))
2460 (mdw-define-c-style mdw-trustonic-alec-c (mdw-trustonic-c)
2461 (comment-column . 0)
2462 (c-indent-comment-alist (anchored-comment . (column . 0))
2463 (end-block . (space . 1))
2464 (cpp-end-block . (space . 1))
2465 (other . (space . 1)))
2466 (c-offsets-alist (arglist-cont-nonempty . mdw-c-indent-arglist-nested)))
2468 (defun mdw-set-default-c-style (modes style)
2469 "Update the default CC Mode style for MODES to be STYLE.
2471 MODES may be a list of major mode names or a singleton. STYLE is a style
2473 (let ((modes (if (listp modes) modes (list modes)))
2474 (style (symbol-name style)))
2475 (setq c-default-style
2476 (append (mapcar (lambda (mode)
2479 (remove-if (lambda (assoc)
2480 (memq (car assoc) modes))
2481 (if (listp c-default-style)
2483 (list (cons 'other c-default-style))))))))
2484 (setq c-default-style "mdw-c")
2486 (mdw-set-default-c-style '(c-mode c++-mode) 'mdw-c)
2488 (defvar mdw-c-comment-fill-prefix
2489 `((,(concat "\\([ \t]*/?\\)"
2492 "\\([A-Za-z]+:[ \t]*\\)?"
2493 mdw-hanging-indents)
2494 (pad . 1) (match . 2) (pad . 3) (pad . 4) (pad . 5)))
2495 "Fill prefix matching C comments (both kinds).")
2497 (defun mdw-fontify-c-and-c++ ()
2499 ;; Fiddle with some syntax codes.
2500 (modify-syntax-entry ?* ". 23")
2501 (modify-syntax-entry ?/ ". 124b")
2502 (modify-syntax-entry ?\n "> b")
2505 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2507 ;; Now define things to be fontified.
2508 (make-local-variable 'font-lock-keywords)
2510 (mdw-regexps "alignas" ;C11 macro, C++11
2512 "and" ;C++, C95 macro
2513 "and_eq" ;C++, C95 macro
2514 "asm" ;K&R, C++, GCC
2515 "atomic" ;C11 macro, C++11 template type
2517 "bitand" ;C++, C95 macro
2518 "bitor" ;C++, C95 macro
2519 "bool" ;C++, C99 macro
2524 "char16_t" ;C++11, C11 library type
2525 "char32_t" ;C++11, C11 library type
2527 "complex" ;C99 macro, C++ template type
2528 "compl" ;C++, C95 macro
2532 "continue" ;K&R, C89
2534 "defined" ;C89 preprocessor
2541 ;; "entry" ;K&R -- never used
2552 "imaginary" ;C99 macro
2553 "inline" ;C++, C99, GCC
2560 "noreturn" ;C11 macro
2561 "not" ;C++, C95 macro
2562 "not_eq" ;C++, C95 macro
2565 "or" ;C++, C95 macro
2566 "or_eq" ;C++, C95 macro
2570 "register" ;K&R, C89
2571 "reinterpret_cast" ;C++
2578 "static_assert" ;C11 macro, C++11
2585 "thread_local" ;C11 macro, C++11
2591 "unsigned" ;K&R, C89
2596 "wchar_t" ;C++, C89 library type
2598 "xor" ;C++, C95 macro
2599 "xor_eq" ;C++, C95 macro
2608 "_Pragma" ;C99 preprocessor
2609 "_Static_assert" ;C11
2610 "_Thread_local" ;C11
2613 "__attribute__" ;GCC
2616 "__extension__" ;GCC
2626 (mdw-regexps "false" ;C++, C99 macro
2628 "true" ;C++, C99 macro
2630 (preprocessor-keywords
2631 (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
2632 "ident" "if" "ifdef" "ifndef" "import" "include"
2633 "line" "pragma" "unassert" "undef" "warning"))
2635 (mdw-regexps "class" "defs" "encode" "end" "implementation"
2636 "interface" "private" "protected" "protocol" "public"
2639 (setq font-lock-keywords
2642 ;; Fontify include files as strings.
2643 (list (concat "^[ \t]*\\#[ \t]*"
2644 "\\(include\\|import\\)"
2645 "[ \t]*\\(<[^>]+>?\\)")
2646 '(2 font-lock-string-face))
2648 ;; Preprocessor directives are `references'?.
2649 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
2650 preprocessor-keywords
2651 "\\)\\>\\|[0-9]+\\|$\\)\\)")
2652 '(1 font-lock-keyword-face))
2654 ;; Handle the keywords defined above.
2655 (list (concat "@\\<\\(" objc-keywords "\\)\\>")
2656 '(0 font-lock-keyword-face))
2658 (list (concat "\\<\\(" c-keywords "\\)\\>")
2659 '(0 font-lock-keyword-face))
2661 (list (concat "\\<\\(" c-builtins "\\)\\>")
2662 '(0 font-lock-variable-name-face))
2664 ;; Handle numbers too.
2666 ;; This looks strange, I know. It corresponds to the
2667 ;; preprocessor's idea of what a number looks like, rather than
2668 ;; anything sensible.
2669 (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
2670 "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
2671 '(0 mdw-number-face))
2673 ;; And anything else is punctuation.
2674 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2675 '(0 mdw-punct-face))))))
2677 (define-derived-mode sod-mode c-mode "Sod"
2678 "Major mode for editing Sod code.")
2679 (push '("\\.sod$" . sod-mode) auto-mode-alist)
2681 (dolist (hook '(c-mode-hook objc-mode-hook c++-mode-hook))
2682 (add-hook hook 'mdw-misc-mode-config t)
2683 (add-hook hook 'mdw-fontify-c-and-c++ t))
2685 ;;;--------------------------------------------------------------------------
2688 (define-derived-mode apcalc-mode c-mode "AP Calc"
2689 "Major mode for editing Calc code.")
2691 (defun mdw-fontify-apcalc ()
2693 ;; Fiddle with some syntax codes.
2694 (modify-syntax-entry ?* ". 23")
2695 (modify-syntax-entry ?/ ". 14")
2698 (setq comment-start "/* ")
2699 (setq comment-end " */")
2700 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2702 ;; Now define things to be fontified.
2703 (make-local-variable 'font-lock-keywords)
2705 (mdw-regexps "break" "case" "cd" "continue" "define" "default"
2706 "do" "else" "exit" "for" "global" "goto" "help" "if"
2707 "local" "mat" "obj" "print" "quit" "read" "return"
2708 "show" "static" "switch" "while" "write")))
2710 (setq font-lock-keywords
2713 ;; Handle the keywords defined above.
2714 (list (concat "\\<\\(" c-keywords "\\)\\>")
2715 '(0 font-lock-keyword-face))
2717 ;; Handle numbers too.
2719 ;; This looks strange, I know. It corresponds to the
2720 ;; preprocessor's idea of what a number looks like, rather than
2721 ;; anything sensible.
2722 (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
2723 "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
2724 '(0 mdw-number-face))
2726 ;; And anything else is punctuation.
2727 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2728 '(0 mdw-punct-face))))))
2731 (add-hook 'apcalc-mode-hook 'mdw-misc-mode-config t)
2732 (add-hook 'apcalc-mode-hook 'mdw-fontify-apcalc t))
2734 ;;;--------------------------------------------------------------------------
2735 ;;; Java programming configuration.
2737 ;; Make indentation nice.
2739 (mdw-define-c-style mdw-java ()
2740 (c-basic-offset . 2)
2741 (c-backslash-column . 72)
2742 (c-offsets-alist (substatement-open . 0)
2747 (statement-case-intro . +)))
2748 (mdw-set-default-c-style 'java-mode 'mdw-java)
2750 ;; Declare Java fontification style.
2752 (defun mdw-fontify-java ()
2754 ;; Fiddle with some syntax codes.
2755 (modify-syntax-entry ?@ ".")
2756 (modify-syntax-entry ?@ "." font-lock-syntax-table)
2759 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2761 ;; Now define things to be fontified.
2762 (make-local-variable 'font-lock-keywords)
2763 (let ((java-keywords
2764 (mdw-regexps "abstract" "assert"
2765 "boolean" "break" "byte"
2766 "case" "catch" "char" "class" "const" "continue"
2767 "default" "do" "double"
2768 "else" "enum" "extends"
2769 "final" "finally" "float" "for"
2771 "if" "implements" "import" "instanceof" "int"
2775 "package" "private" "protected" "public"
2777 "short" "static" "strictfp" "switch" "synchronized"
2778 "throw" "throws" "transient" "try"
2783 (mdw-regexps "false" "null" "super" "this" "true")))
2785 (setq font-lock-keywords
2788 ;; Handle the keywords defined above.
2789 (list (concat "\\<\\(" java-keywords "\\)\\>")
2790 '(0 font-lock-keyword-face))
2792 ;; Handle the magic builtins defined above.
2793 (list (concat "\\<\\(" java-builtins "\\)\\>")
2794 '(0 font-lock-variable-name-face))
2796 ;; Handle numbers too.
2798 ;; The following isn't quite right, but it's close enough.
2799 (list (concat "\\<\\("
2800 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2801 "[0-9]+\\(\\.[0-9]*\\)?"
2802 "\\([eE][-+]?[0-9]+\\)?\\)"
2804 '(0 mdw-number-face))
2806 ;; And anything else is punctuation.
2807 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2808 '(0 mdw-punct-face))))))
2811 (add-hook 'java-mode-hook 'mdw-misc-mode-config t)
2812 (add-hook 'java-mode-hook 'mdw-fontify-java t))
2814 ;;;--------------------------------------------------------------------------
2815 ;;; Javascript programming configuration.
2817 (defun mdw-javascript-style ()
2818 (setq js-indent-level 2)
2819 (setq js-expr-indent-offset 0))
2821 (defun mdw-fontify-javascript ()
2824 (mdw-javascript-style)
2825 (setq js-auto-indent-flag t)
2827 ;; Now define things to be fontified.
2828 (make-local-variable 'font-lock-keywords)
2829 (let ((javascript-keywords
2830 (mdw-regexps "abstract" "boolean" "break" "byte" "case" "catch"
2831 "char" "class" "const" "continue" "debugger" "default"
2832 "delete" "do" "double" "else" "enum" "export" "extends"
2833 "final" "finally" "float" "for" "function" "goto" "if"
2834 "implements" "import" "in" "instanceof" "int"
2835 "interface" "let" "long" "native" "new" "package"
2836 "private" "protected" "public" "return" "short"
2837 "static" "super" "switch" "synchronized" "throw"
2838 "throws" "transient" "try" "typeof" "var" "void"
2839 "volatile" "while" "with" "yield"))
2840 (javascript-builtins
2841 (mdw-regexps "false" "null" "undefined" "Infinity" "NaN" "true"
2842 "arguments" "this")))
2844 (setq font-lock-keywords
2847 ;; Handle the keywords defined above.
2848 (list (concat "\\_<\\(" javascript-keywords "\\)\\_>")
2849 '(0 font-lock-keyword-face))
2851 ;; Handle the predefined builtins defined above.
2852 (list (concat "\\_<\\(" javascript-builtins "\\)\\_>")
2853 '(0 font-lock-variable-name-face))
2855 ;; Handle numbers too.
2857 ;; The following isn't quite right, but it's close enough.
2858 (list (concat "\\_<\\("
2859 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2860 "[0-9]+\\(\\.[0-9]*\\)?"
2861 "\\([eE][-+]?[0-9]+\\)?\\)"
2863 '(0 mdw-number-face))
2865 ;; And anything else is punctuation.
2866 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2867 '(0 mdw-punct-face))))))
2870 (add-hook 'js-mode-hook 'mdw-misc-mode-config t)
2871 (add-hook 'js-mode-hook 'mdw-fontify-javascript t))
2873 ;;;--------------------------------------------------------------------------
2874 ;;; Scala programming configuration.
2876 (defun mdw-fontify-scala ()
2879 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2881 ;; Define things to be fontified.
2882 (make-local-variable 'font-lock-keywords)
2883 (let ((scala-keywords
2884 (mdw-regexps "abstract" "case" "catch" "class" "def" "do" "else"
2885 "extends" "final" "finally" "for" "forSome" "if"
2886 "implicit" "import" "lazy" "match" "new" "object"
2887 "override" "package" "private" "protected" "return"
2888 "sealed" "throw" "trait" "try" "type" "val"
2889 "var" "while" "with" "yield"))
2891 (mdw-regexps "false" "null" "super" "this" "true"))
2892 (punctuation "[-!%^&*=+:@#~/?\\|`]"))
2894 (setq font-lock-keywords
2897 ;; Magical identifiers between backticks.
2898 (list (concat "`\\([^`]+\\)`")
2899 '(1 font-lock-variable-name-face))
2901 ;; Handle the keywords defined above.
2902 (list (concat "\\_<\\(" scala-keywords "\\)\\_>")
2903 '(0 font-lock-keyword-face))
2905 ;; Handle the constants defined above.
2906 (list (concat "\\_<\\(" scala-constants "\\)\\_>")
2907 '(0 font-lock-variable-name-face))
2909 ;; Magical identifiers between backticks.
2910 (list (concat "`\\([^`]+\\)`")
2911 '(1 font-lock-variable-name-face))
2913 ;; Handle numbers too.
2915 ;; As usual, not quite right.
2916 (list (concat "\\_<\\("
2917 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2918 "[0-9]+\\(\\.[0-9]*\\)?"
2919 "\\([eE][-+]?[0-9]+\\)?\\)"
2921 '(0 mdw-number-face))
2923 ;; And everything else is punctuation.
2924 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2925 '(0 mdw-punct-face)))
2927 font-lock-syntactic-keywords
2930 ;; Single quotes around characters. But not when used to quote
2931 ;; symbol names. Ugh.
2932 (list (concat "\\('\\)"
2934 "\\|" "\\\\" "\\(" "\\\\\\\\" "\\)*"
2935 "u+" "[0-9a-fA-F]\\{4\\}"
2936 "\\|" "\\\\" "[0-7]\\{1,3\\}"
2937 "\\|" "\\\\" "." "\\)"
2943 (add-hook 'scala-mode-hook 'mdw-misc-mode-config t)
2944 (add-hook 'scala-mode-hook 'mdw-fontify-scala t))
2946 ;;;--------------------------------------------------------------------------
2947 ;;; C# programming configuration.
2949 ;; Make indentation nice.
2951 (mdw-define-c-style mdw-csharp ()
2952 (c-basic-offset . 2)
2953 (c-backslash-column . 72)
2954 (c-offsets-alist (substatement-open . 0)
2959 (statement-case-intro . +)))
2960 (mdw-set-default-c-style 'csharp-mode 'mdw-csharp)
2962 ;; Declare C# fontification style.
2964 (defun mdw-fontify-csharp ()
2967 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2969 ;; Now define things to be fontified.
2970 (make-local-variable 'font-lock-keywords)
2971 (let ((csharp-keywords
2972 (mdw-regexps "abstract" "as" "bool" "break" "byte" "case" "catch"
2973 "char" "checked" "class" "const" "continue" "decimal"
2974 "default" "delegate" "do" "double" "else" "enum"
2975 "event" "explicit" "extern" "finally" "fixed" "float"
2976 "for" "foreach" "goto" "if" "implicit" "in" "int"
2977 "interface" "internal" "is" "lock" "long" "namespace"
2978 "new" "object" "operator" "out" "override" "params"
2979 "private" "protected" "public" "readonly" "ref"
2980 "return" "sbyte" "sealed" "short" "sizeof"
2981 "stackalloc" "static" "string" "struct" "switch"
2982 "throw" "try" "typeof" "uint" "ulong" "unchecked"
2983 "unsafe" "ushort" "using" "virtual" "void" "volatile"
2987 (mdw-regexps "base" "false" "null" "this" "true")))
2989 (setq font-lock-keywords
2992 ;; Handle the keywords defined above.
2993 (list (concat "\\<\\(" csharp-keywords "\\)\\>")
2994 '(0 font-lock-keyword-face))
2996 ;; Handle the magic builtins defined above.
2997 (list (concat "\\<\\(" csharp-builtins "\\)\\>")
2998 '(0 font-lock-variable-name-face))
3000 ;; Handle numbers too.
3002 ;; The following isn't quite right, but it's close enough.
3003 (list (concat "\\<\\("
3004 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
3005 "[0-9]+\\(\\.[0-9]*\\)?"
3006 "\\([eE][-+]?[0-9]+\\)?\\)"
3008 '(0 mdw-number-face))
3010 ;; And anything else is punctuation.
3011 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3012 '(0 mdw-punct-face))))))
3014 (define-derived-mode csharp-mode java-mode "C#"
3015 "Major mode for editing C# code.")
3017 (add-hook 'csharp-mode-hook 'mdw-fontify-csharp t)
3019 ;;;--------------------------------------------------------------------------
3020 ;;; F# programming configuration.
3022 (setq fsharp-indent-offset 2)
3024 (defun mdw-fontify-fsharp ()
3026 (let ((punct "=<>+-*/|&%!@?"))
3028 ((>= i (length punct)))
3029 (modify-syntax-entry (aref punct i) ".")))
3031 (modify-syntax-entry ?_ "_")
3032 (modify-syntax-entry ?( "(")
3033 (modify-syntax-entry ?) ")")
3035 (setq indent-tabs-mode nil)
3037 (let ((fsharp-keywords
3038 (mdw-regexps "abstract" "and" "as" "assert" "atomic"
3040 "checked" "class" "component" "const" "constraint"
3041 "constructor" "continue"
3042 "default" "delegate" "do" "done" "downcast" "downto"
3043 "eager" "elif" "else" "end" "exception" "extern"
3044 "finally" "fixed" "for" "fori" "fun" "function"
3047 "if" "in" "include" "inherit" "inline" "interface"
3050 "match" "measure" "member" "method" "mixin" "module"
3053 "object" "of" "open" "or" "override"
3054 "parallel" "params" "private" "process" "protected"
3056 "rec" "recursive" "return"
3057 "sealed" "sig" "static" "struct"
3058 "tailcall" "then" "to" "trait" "try" "type"
3060 "val" "virtual" "void" "volatile"
3061 "when" "while" "with"
3065 (mdw-regexps "asr" "land" "lor" "lsl" "lsr" "lxor" "mod"
3066 "base" "false" "null" "true"))
3069 (mdw-regexps "do" "let" "return" "use" "yield"))
3071 (preprocessor-keywords
3072 (mdw-regexps "if" "indent" "else" "endif")))
3074 (setq font-lock-keywords
3075 (list (list (concat "\\(^\\|[^\"]\\)"
3078 "\\(" "[^)*]" "[^*]*" "\\*+" "\\)*"
3083 '(2 font-lock-comment-face))
3085 (list (concat "'" "\\("
3088 "\\|" "[0-9][0-9][0-9]"
3089 "\\|" "u" "[0-9a-fA-F]\\{4\\}"
3090 "\\|" "U" "[0-9a-fA-F]\\{8\\}"
3096 "\\(" "\\\\" "\\(.\\|\n\\)"
3099 '(0 font-lock-string-face))
3101 (list (concat "\\_<\\(" bang-keywords "\\)!" "\\|"
3102 "^#[ \t]*\\(" preprocessor-keywords "\\)\\_>"
3104 "\\_<\\(" fsharp-keywords "\\)\\_>")
3105 '(0 font-lock-keyword-face))
3106 (list (concat "\\<\\(" fsharp-builtins "\\)\\_>")
3107 '(0 font-lock-variable-name-face))
3109 (list (concat "\\_<"
3110 "\\(" "0[bB][01]+" "\\|"
3112 "0[xX][0-9a-fA-F]+" "\\)"
3113 "\\(" "lf\\|LF" "\\|"
3114 "[uU]?[ysnlL]?" "\\)"
3121 "\\([eE][-+]?[0-9]+\\)?"
3126 '(0 mdw-number-face))
3128 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3129 '(0 mdw-punct-face))))))
3131 (defun mdw-fontify-inferior-fsharp ()
3132 (mdw-fontify-fsharp)
3133 (setq font-lock-keywords
3134 (append (list (list "^[#-]" '(0 font-lock-comment-face))
3135 (list "^>" '(0 font-lock-keyword-face)))
3136 font-lock-keywords)))
3139 (add-hook 'fsharp-mode-hook 'mdw-misc-mode-config t)
3140 (add-hook 'fsharp-mode-hook 'mdw-fontify-fsharp t)
3141 (add-hook 'inferior-fsharp-mode-hooks 'mdw-fontify-inferior-fsharp t))
3143 ;;;--------------------------------------------------------------------------
3144 ;;; Go programming configuration.
3146 (defun mdw-fontify-go ()
3148 (make-local-variable 'font-lock-keywords)
3150 (mdw-regexps "break" "case" "chan" "const" "continue"
3151 "default" "defer" "else" "fallthrough" "for"
3152 "func" "go" "goto" "if" "import"
3153 "interface" "map" "package" "range" "return"
3154 "select" "struct" "switch" "type" "var"))
3156 (mdw-regexps "bool" "byte" "complex64" "complex128" "error"
3157 "float32" "float64" "int" "uint8" "int16" "int32"
3158 "int64" "rune" "string" "uint" "uint8" "uint16"
3159 "uint32" "uint64" "uintptr" "void"
3160 "false" "iota" "nil" "true"
3162 "append" "cap" "copy" "delete" "imag" "len" "make"
3163 "new" "panic" "real" "recover")))
3165 (setq font-lock-keywords
3168 ;; Handle the keywords defined above.
3169 (list (concat "\\<\\(" go-keywords "\\)\\>")
3170 '(0 font-lock-keyword-face))
3171 (list (concat "\\<\\(" go-intrinsics "\\)\\>")
3172 '(0 font-lock-variable-name-face))
3174 ;; Strings and characters.
3176 "\\(" "[^\\']" "\\|"
3178 "\\(" "[abfnrtv\\'\"]" "\\|"
3179 "[0-7]\\{3\\}" "\\|"
3180 "x" "[0-9A-Fa-f]\\{2\\}" "\\|"
3181 "u" "[0-9A-Fa-f]\\{4\\}" "\\|"
3182 "U" "[0-9A-Fa-f]\\{8\\}" "\\)" "\\)"
3186 "\\(" "[^\n\\\"]+" "\\|" "\\\\." "\\)*"
3190 '(0 font-lock-string-face))
3192 ;; Handle numbers too.
3194 ;; The following isn't quite right, but it's close enough.
3195 (list (concat "\\<\\("
3196 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
3197 "[0-9]+\\(\\.[0-9]*\\)?"
3198 "\\([eE][-+]?[0-9]+\\)?\\)")
3199 '(0 mdw-number-face))
3201 ;; And anything else is punctuation.
3202 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3203 '(0 mdw-punct-face))))))
3205 (add-hook 'go-mode-hook 'mdw-misc-mode-config t)
3206 (add-hook 'go-mode-hook 'mdw-fontify-go t))
3208 ;;;--------------------------------------------------------------------------
3209 ;;; Rust programming configuration.
3211 (setq-default rust-indent-offset 2)
3213 (defun mdw-self-insert-and-indent (count)
3215 (self-insert-command count)
3216 (indent-according-to-mode))
3218 (defun mdw-fontify-rust ()
3220 ;; Hack syntax categories.
3221 (modify-syntax-entry ?$ ".")
3222 (modify-syntax-entry ?% ".")
3223 (modify-syntax-entry ?= ".")
3225 ;; Fontify keywords and things.
3226 (make-local-variable 'font-lock-keywords)
3227 (let ((rust-keywords
3228 (mdw-regexps "abstract" "alignof" "as" "async" "await"
3229 "become" "box" "break"
3230 "const" "continue" "crate"
3232 "else" "enum" "extern"
3236 "macro" "match" "mod" "move" "mut"
3237 "offsetof" "override"
3238 "priv" "proc" "pub" "pure"
3240 "sizeof" "static" "struct" "super"
3241 "trait" "try" "type" "typeof"
3242 "union" "unsafe" "unsized" "use"
3247 (mdw-regexps "array" "pointer" "slice" "tuple"
3248 "bool" "true" "false"
3250 "i8" "i16" "i32" "i64" "isize"
3251 "u8" "u16" "u32" "u64" "usize"
3254 (setq font-lock-keywords
3257 ;; Handle the keywords defined above.
3258 (list (concat "\\_<\\(" rust-keywords "\\)\\_>")
3259 '(0 font-lock-keyword-face))
3260 (list (concat "\\_<\\(" rust-builtins "\\)\\_>")
3261 '(0 font-lock-variable-name-face))
3263 ;; Handle numbers too.
3264 (list (concat "\\_<\\("
3266 "\\(" "\\(\\.[0-9_]+\\)?[eE][-+]?[0-9_]+"
3270 "\\|" "\\(" "[0-9][0-9_]*"
3271 "\\|" "0x[0-9a-fA-F_]+"
3275 "\\([ui]\\(8\\|16\\|32\\|64\\|size\\)\\)?"
3277 '(0 mdw-number-face))
3279 ;; And anything else is punctuation.
3280 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3281 '(0 mdw-punct-face)))
3282 font-lock-syntactic-face-function nil))
3284 ;; Hack key bindings.
3285 (local-set-key [?{] 'mdw-self-insert-and-indent)
3286 (local-set-key [?}] 'mdw-self-insert-and-indent))
3289 (add-hook 'rust-mode-hook 'mdw-misc-mode-config t)
3290 (add-hook 'rust-mode-hook 'mdw-fontify-rust t))
3292 ;;;--------------------------------------------------------------------------
3293 ;;; Awk programming configuration.
3295 ;; Make Awk indentation nice.
3297 (mdw-define-c-style mdw-awk ()
3298 (c-basic-offset . 2)
3299 (c-offsets-alist (substatement-open . 0)
3300 (c-backslash-column . 72)
3301 (statement-cont . 0)
3302 (statement-case-intro . +)))
3303 (mdw-set-default-c-style 'awk-mode 'mdw-awk)
3305 ;; Declare Awk fontification style.
3307 (defun mdw-fontify-awk ()
3309 ;; Miscellaneous fiddling.
3310 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3312 ;; Now define things to be fontified.
3313 (make-local-variable 'font-lock-keywords)
3315 (mdw-regexps "BEGIN" "END" "ARGC" "ARGIND" "ARGV" "CONVFMT"
3316 "ENVIRON" "ERRNO" "FIELDWIDTHS" "FILENAME" "FNR"
3317 "FS" "IGNORECASE" "NF" "NR" "OFMT" "OFS" "ORS" "RS"
3318 "RSTART" "RLENGTH" "RT" "SUBSEP"
3319 "atan2" "break" "close" "continue" "cos" "delete"
3320 "do" "else" "exit" "exp" "fflush" "file" "for" "func"
3321 "function" "gensub" "getline" "gsub" "if" "in"
3322 "index" "int" "length" "log" "match" "next" "rand"
3323 "return" "print" "printf" "sin" "split" "sprintf"
3324 "sqrt" "srand" "strftime" "sub" "substr" "system"
3325 "systime" "tolower" "toupper" "while")))
3327 (setq font-lock-keywords
3330 ;; Handle the keywords defined above.
3331 (list (concat "\\<\\(" c-keywords "\\)\\>")
3332 '(0 font-lock-keyword-face))
3334 ;; Handle numbers too.
3336 ;; The following isn't quite right, but it's close enough.
3337 (list (concat "\\<\\("
3338 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
3339 "[0-9]+\\(\\.[0-9]*\\)?"
3340 "\\([eE][-+]?[0-9]+\\)?\\)"
3342 '(0 mdw-number-face))
3344 ;; And anything else is punctuation.
3345 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3346 '(0 mdw-punct-face))))))
3349 (add-hook 'awk-mode-hook 'mdw-misc-mode-config t)
3350 (add-hook 'awk-mode-hook 'mdw-fontify-awk t))
3352 ;;;--------------------------------------------------------------------------
3353 ;;; Perl programming style.
3355 ;; Perl indentation style.
3357 (setq-default perl-indent-level 2)
3359 (setq-default cperl-indent-level 2
3360 cperl-continued-statement-offset 2
3361 cperl-indent-region-fix-constructs nil
3362 cperl-continued-brace-offset 0
3363 cperl-brace-offset -2
3364 cperl-brace-imaginary-offset 0
3365 cperl-label-offset 0)
3367 ;; Define perl fontification style.
3369 (defun mdw-fontify-perl ()
3371 ;; Miscellaneous fiddling.
3372 (modify-syntax-entry ?$ "\\")
3373 (modify-syntax-entry ?$ "\\" font-lock-syntax-table)
3374 (modify-syntax-entry ?: "." font-lock-syntax-table)
3375 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3377 ;; Now define fontification things.
3378 (make-local-variable 'font-lock-keywords)
3379 (let ((perl-keywords
3386 "ge" "given" "gt" "goto"
3388 "last" "le" "local" "lt"
3393 "redo" "require" "return"
3395 "undef" "unless" "until" "use"
3398 (setq font-lock-keywords
3401 ;; Set up the keywords defined above.
3402 (list (concat "\\<\\(" perl-keywords "\\)\\>")
3403 '(0 font-lock-keyword-face))
3405 ;; At least numbers are simpler than C.
3406 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
3407 "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\)?"
3408 "\\([eE][-+]?[0-9_]+\\)?")
3409 '(0 mdw-number-face))
3411 ;; And anything else is punctuation.
3412 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3413 '(0 mdw-punct-face))))))
3415 (defun perl-number-tests (&optional arg)
3416 "Assign consecutive numbers to lines containing `#t'. With ARG,
3417 strip numbers instead."
3420 (goto-char (point-min))
3421 (let ((i 0) (fmt (if arg "" " %4d")))
3422 (while (search-forward "#t" nil t)
3423 (delete-region (point) (line-end-position))
3425 (insert (format fmt i)))
3426 (goto-char (point-min))
3427 (if (re-search-forward "\\(tests\\s-*=>\\s-*\\)\\w*" nil t)
3428 (replace-match (format "\\1%d" i))))))
3430 (dolist (hook '(perl-mode-hook cperl-mode-hook))
3431 (add-hook hook 'mdw-misc-mode-config t)
3432 (add-hook hook 'mdw-fontify-perl t))
3434 ;;;--------------------------------------------------------------------------
3435 ;;; Python programming style.
3437 (setq-default py-indent-offset 2
3439 python-indent-offset 2
3440 python-fill-docstring-style 'symmetric)
3442 (defun mdw-fontify-pythonic (keywords builtins)
3444 ;; Miscellaneous fiddling.
3445 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3446 (setq indent-tabs-mode nil)
3447 (set (make-local-variable 'forward-sexp-function) nil)
3449 ;; Now define fontification things.
3450 (make-local-variable 'font-lock-keywords)
3451 (setq font-lock-keywords
3454 ;; Set up the keywords defined above.
3455 (list (concat "\\_<\\(" keywords "\\)\\_>")
3456 '(0 font-lock-keyword-face))
3457 (list (concat "\\(^\\|[^.]\\)\\_<\\(" builtins "\\)\\_>")
3458 '(2 font-lock-variable-name-face))
3459 (list (concat "\\_<\\(__\\(\\sw+\\|\\s_+\\)+__\\)\\_>")
3460 '(0 font-lock-variable-name-face))
3462 ;; At least numbers are simpler than C.
3463 (list (concat "\\_<0\\([xX][0-9a-fA-F]+\\|[oO]?[0-7]+\\|[bB][01]+\\)\\|"
3464 "\\_<[0-9][0-9]*\\(\\.[0-9]*\\)?"
3465 "\\([eE][-+]?[0-9]+\\|[lL]\\)?")
3466 '(0 mdw-number-face))
3468 ;; And anything else is punctuation.
3469 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3470 '(0 mdw-punct-face)))))
3472 ;; Define Python fontification styles.
3474 (defun mdw-fontify-python ()
3475 (mdw-fontify-pythonic
3476 (mdw-regexps "and" "as" "assert" "async" "await"
3478 "case" "class" "continue"
3480 "elif" "else" "except" ;"exec"
3481 "finally" "for" "from"
3483 "if" "import" "in" "is"
3495 (mdw-regexps "Ellipsis"
3497 "None" "NotImplemented"
3502 "BaseExceptionGroup"
3506 "FloatingPointError"
3519 "ConnectionAbortedError"
3520 "ConnectionRefusedError"
3521 "ConnectionResetError"
3526 "NotADirectoryError"
3532 "ModuleNotFoundError"
3541 "NotImplementedError"
3550 "UnicodeDecodeError"
3551 "UnicodeEncodeError"
3552 "UnicodeTranslateError"
3556 "DeprecationWarning"
3560 "PendingDeprecationWarning"
3570 "abs" "absolute_import" "aiter"
3571 "all" "anext" "any" "apply" "ascii"
3572 "basestring" "bin" "bool" "breakpoint"
3573 "buffer" "bytearray" "bytes"
3574 "callable" "coerce" "chr" "classmethod"
3575 "cmp" "compile" "complex"
3576 "delattr" "dict" "dir" "divmod"
3577 "enumerate" "eval" "exec" "execfile"
3578 "file" "filter" "float" "format" "frozenset"
3580 "hasattr" "hash" "help" "hex"
3581 "id" "input" "int" "intern"
3582 "isinstance" "issubclass" "iter"
3583 "len" "list" "locals" "long"
3584 "map" "max" "memoryview" "min"
3586 "object" "oct" "open" "ord"
3587 "pow" "print" "property"
3588 "range" "raw_input" "reduce" "reload"
3589 "repr" "reversed" "round"
3590 "set" "setattr" "slice" "sorted"
3591 "staticmethod" "str" "sum" "super"
3599 (defun mdw-fontify-pyrex ()
3600 (mdw-fontify-pythonic
3601 (mdw-regexps "and" "as" "assert" "break" "cdef" "class" "continue"
3602 "ctypedef" "def" "del" "elif" "else" "enum" "except" "exec"
3603 "extern" "finally" "for" "from" "global" "if"
3604 "import" "in" "is" "lambda" "not" "or" "pass" "print"
3605 "property" "raise" "return" "struct" "try" "while" "with"
3609 (define-derived-mode pyrex-mode python-mode "Pyrex"
3610 "Major mode for editing Pyrex source code")
3611 (setq auto-mode-alist
3612 (append '(("\\.pyx$" . pyrex-mode)
3613 ("\\.pxd$" . pyrex-mode)
3614 ("\\.pxi$" . pyrex-mode))
3618 (add-hook 'python-mode-hook 'mdw-misc-mode-config t)
3619 (add-hook 'python-mode-hook 'mdw-fontify-python t)
3620 (add-hook 'pyrex-mode-hook 'mdw-fontify-pyrex t))
3622 ;;;--------------------------------------------------------------------------
3623 ;;; Lua programming style.
3625 (setq-default lua-indent-level 2)
3627 (defun mdw-fontify-lua ()
3629 ;; Miscellaneous fiddling.
3630 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3632 ;; Now define fontification things.
3633 (make-local-variable 'font-lock-keywords)
3635 (mdw-regexps "and" "break" "do" "else" "elseif" "end"
3636 "false" "for" "function" "goto" "if" "in" "local"
3637 "nil" "not" "or" "repeat" "return" "then" "true"
3639 (setq font-lock-keywords
3642 ;; Set up the keywords defined above.
3643 (list (concat "\\_<\\(" lua-keywords "\\)\\_>")
3644 '(0 font-lock-keyword-face))
3646 ;; At least numbers are simpler than C.
3647 (list (concat "\\_<\\(" "0[xX]"
3648 "\\(" "[0-9a-fA-F]+"
3649 "\\(\\.[0-9a-fA-F]*\\)?"
3650 "\\|" "\\.[0-9a-fA-F]+"
3652 "\\([pP][-+]?[0-9]+\\)?"
3653 "\\|" "\\(" "[0-9]+"
3657 "\\([eE][-+]?[0-9]+\\)?"
3659 '(0 mdw-number-face))
3661 ;; And anything else is punctuation.
3662 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3663 '(0 mdw-punct-face))))))
3666 (add-hook 'lua-mode-hook 'mdw-misc-mode-config t)
3667 (add-hook 'lua-mode-hook 'mdw-fontify-lua t))
3669 ;;;--------------------------------------------------------------------------
3670 ;;; Icon programming style.
3672 ;; Icon indentation style.
3674 (setq-default icon-brace-offset 0
3675 icon-continued-brace-offset 0
3676 icon-continued-statement-offset 2
3677 icon-indent-level 2)
3679 ;; Define Icon fontification style.
3681 (defun mdw-fontify-icon ()
3683 ;; Miscellaneous fiddling.
3684 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3686 ;; Now define fontification things.
3687 (make-local-variable 'font-lock-keywords)
3688 (let ((icon-keywords
3689 (mdw-regexps "break" "by" "case" "create" "default" "do" "else"
3690 "end" "every" "fail" "global" "if" "initial"
3691 "invocable" "link" "local" "next" "not" "of"
3692 "procedure" "record" "repeat" "return" "static"
3693 "suspend" "then" "to" "until" "while"))
3694 (preprocessor-keywords
3695 (mdw-regexps "define" "else" "endif" "error" "ifdef" "ifndef"
3696 "include" "line" "undef")))
3697 (setq font-lock-keywords
3700 ;; Set up the keywords defined above.
3701 (list (concat "\\<\\(" icon-keywords "\\)\\>")
3702 '(0 font-lock-keyword-face))
3704 ;; The things that Icon calls keywords.
3705 (list "&\\sw+\\>" '(0 font-lock-variable-name-face))
3707 ;; At least numbers are simpler than C.
3708 (list (concat "\\<[0-9]+"
3709 "\\([rR][0-9a-zA-Z]+\\|"
3710 "\\.[0-9]+\\([eE][+-]?[0-9]+\\)?\\)\\>\\|"
3711 "\\.[0-9]+\\([eE][+-]?[0-9]+\\)?\\>")
3712 '(0 mdw-number-face))
3715 (list (concat "^[ \t]*$[ \t]*\\<\\("
3716 preprocessor-keywords
3718 '(0 font-lock-keyword-face))
3720 ;; And anything else is punctuation.
3721 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3722 '(0 mdw-punct-face))))))
3725 (add-hook 'icon-mode-hook 'mdw-misc-mode-config t)
3726 (add-hook 'icon-mode-hook 'mdw-fontify-icon t))
3728 ;;;--------------------------------------------------------------------------
3731 (defun mdw-fontify-fortran-common ()
3732 (let ((fortran-keywords
3733 (mdw-regexps "access"
3751 "double\\s-*precision"
3752 "else" "elseif" "elsewhere"
3754 "endblock" "endblockdata"
3810 "select" "selectcase" "selecttype"
3821 (fortran-operators (mdw-regexps "and"
3834 (fortran-intrinsics (mdw-regexps "abs" "dabs" "iabs" "cabs"
3835 "atan" "datan" "atan2" "datan2"
3845 "int" "aint" "idint"
3846 "alog" "dlog" "clog"
3858 "sign" "isign" "dsign"
3860 "sqrt" "dsqrt" "csqrt"
3862 (preprocessor-keywords
3863 (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
3864 "ident" "if" "ifdef" "ifndef" "import" "include"
3865 "line" "pragma" "unassert" "undef" "warning")))
3866 (setq font-lock-keywords-case-fold-search t
3870 ;; Fontify include files as strings.
3871 (list (concat "^[ \t]*\\#[ \t]*" "include"
3872 "[ \t]*\\(<[^>]+>?\\)")
3873 '(1 font-lock-string-face))
3875 ;; Preprocessor directives are `references'?.
3876 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
3877 preprocessor-keywords
3878 "\\)\\>\\|[0-9]+\\|$\\)\\)")
3879 '(1 font-lock-keyword-face))
3881 ;; Set up the keywords defined above.
3882 (list (concat "\\<\\(" fortran-keywords "\\)\\>")
3883 '(0 font-lock-keyword-face))
3885 ;; Set up the `.foo.' operators.
3886 (list (concat "\\.\\(" fortran-operators "\\)\\.")
3887 '(0 font-lock-keyword-face))
3889 ;; Set up the intrinsic functions.
3890 (list (concat "\\<\\(" fortran-intrinsics "\\)\\>")
3891 '(0 font-lock-variable-name-face))
3894 (list (concat "\\(" "\\<" "[0-9]+" "\\(\\.[0-9]*\\)?"
3897 "\\(" "[de]" "[+-]?" "[0-9]+" "\\)?"
3898 "\\(" "_" "\\sw+" "\\)?"
3899 "\\|" "b'[01]*'" "\\|" "'[01]*'b"
3900 "\\|" "b\"[01]*\"" "\\|" "\"[01]*\"b"
3901 "\\|" "o'[0-7]*'" "\\|" "'[0-7]*'o"
3902 "\\|" "o\"[0-7]*\"" "\\|" "\"[0-7]*\"o"
3903 "\\|" "[xz]'[0-9a-f]*'" "\\|" "'[0-9a-f]*'[xz]"
3904 "\\|" "[xz]\"[0-9a-f]*\"" "\\|" "\"[0-9a-f]*\"[xz]")
3905 '(0 mdw-number-face))
3907 ;; Any anything else is punctuation.
3908 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3909 '(0 mdw-punct-face))))
3911 (modify-syntax-entry ?/ "." font-lock-syntax-table)
3912 (modify-syntax-entry ?< ".")
3913 (modify-syntax-entry ?> ".")))
3915 (defun mdw-fontify-fortran () (mdw-fontify-fortran-common))
3916 (defun mdw-fontify-f90 () (mdw-fontify-fortran-common))
3918 (setq fortran-do-indent 2
3920 fortran-structure-indent 2
3921 fortran-comment-line-start "*"
3922 fortran-comment-indent-style 'relative
3923 fortran-continuation-string "&"
3924 fortran-continuation-indent 4)
3926 (setq f90-do-indent 2
3928 f90-program-indent 2
3929 f90-continuation-indent 4
3930 f90-smart-end-names nil
3931 f90-smart-end 'no-blink)
3934 (add-hook 'fortran-mode-hook 'mdw-misc-mode-config t)
3935 (add-hook 'fortran-mode-hook 'mdw-fontify-fortran t)
3936 (add-hook 'f90-mode-hook 'mdw-misc-mode-config t)
3937 (add-hook 'f90-mode-hook 'mdw-fontify-f90 t))
3939 ;;;--------------------------------------------------------------------------
3942 (defun mdw-fontify-asm ()
3943 (modify-syntax-entry ?' "\"")
3944 (modify-syntax-entry ?. "w")
3945 (modify-syntax-entry ?\n ">")
3946 (setf fill-prefix nil)
3947 (modify-syntax-entry ?. "_")
3948 (modify-syntax-entry ?* ". 23")
3949 (modify-syntax-entry ?/ ". 124b")
3950 (modify-syntax-entry ?\n "> b")
3951 (local-set-key ";" 'self-insert-command)
3952 (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)"))
3954 (defun mdw-asm-set-comment ()
3955 (modify-syntax-entry ?; "."
3957 (modify-syntax-entry asm-comment-char "< b")
3958 (setq comment-start (string asm-comment-char ? )))
3959 (add-hook 'asm-mode-local-variables-hook 'mdw-asm-set-comment)
3960 (put 'asm-comment-char 'safe-local-variable 'characterp)
3963 (add-hook 'asm-mode-hook 'mdw-misc-mode-config t)
3964 (add-hook 'asm-mode-hook 'mdw-fontify-asm t))
3966 ;;;--------------------------------------------------------------------------
3967 ;;; TCL configuration.
3969 (setq-default tcl-indent-level 2)
3971 (defun mdw-fontify-tcl ()
3973 (modify-syntax-entry ch "."))
3974 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3975 (make-local-variable 'font-lock-keywords)
3976 (setq font-lock-keywords
3978 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
3979 "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\)?"
3980 "\\([eE][-+]?[0-9_]+\\)?")
3981 '(0 mdw-number-face))
3982 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3983 '(0 mdw-punct-face)))))
3986 (add-hook 'tcl-mode-hook 'mdw-misc-mode-config t)
3987 (add-hook 'tcl-mode-hook 'mdw-fontify-tcl t))
3989 ;;;--------------------------------------------------------------------------
3990 ;;; Dylan programming configuration.
3992 (defun mdw-fontify-dylan ()
3994 (make-local-variable 'font-lock-keywords)
3996 ;; Horrors. `dylan-mode' sets the `major-mode' name after calling this
3997 ;; hook, which undoes all of our configuration.
3998 (setq major-mode 'dylan-mode)
3999 (font-lock-set-defaults)
4001 (let* ((word "[-_a-zA-Z!*@<>$%]+")
4002 (dylan-keywords (mdw-regexps
4004 "C-address" "C-callable-wrapper" "C-function"
4005 "C-mapped-subtype" "C-pointer-type" "C-struct"
4006 "C-subtype" "C-union" "C-variable"
4008 "above" "abstract" "afterwards" "all"
4009 "begin" "below" "block" "by"
4010 "case" "class" "cleanup" "constant" "create"
4012 "else" "elseif" "end" "exception" "export"
4013 "finally" "for" "from" "function"
4016 "if" "in" "instance" "interface" "iterate"
4018 "let" "library" "local"
4019 "macro" "method" "module"
4022 "select" "slot" "subclass"
4024 "unless" "until" "use"
4025 "variable" "virtual"
4027 (sharp-keywords (mdw-regexps
4028 "all-keys" "key" "next" "rest" "include"
4030 (setq font-lock-keywords
4031 (list (list (concat "\\<\\(" dylan-keywords
4032 "\\|" "with\\(out\\)?-" word
4034 '(0 font-lock-keyword-face))
4035 (list (concat "\\<" word ":" "\\|"
4036 "#\\(" sharp-keywords "\\)\\>")
4037 '(0 font-lock-variable-name-face))
4039 "\\([-+]\\|\\<\\)[0-9]+" "\\("
4040 "\\(\\.[0-9]+\\)?" "\\([eE][-+][0-9]+\\)?"
4043 "\\|" "\\.[0-9]+" "\\([eE][-+][0-9]+\\)?"
4046 "\\|" "#x[0-9a-zA-Z]+"
4048 '(0 mdw-number-face))
4050 "\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\|"
4051 "\\_<[-+*/=<>:&|]+\\_>"
4053 '(0 mdw-punct-face))))))
4056 (add-hook 'dylan-mode-hook 'mdw-misc-mode-config t)
4057 (add-hook 'dylan-mode-hook 'mdw-fontify-dylan t))
4059 ;;;--------------------------------------------------------------------------
4060 ;;; Algol 68 configuration.
4062 (setq-default a68-indent-step 2)
4064 (defun mdw-fontify-algol-68 ()
4066 ;; Fix up the syntax table.
4067 (modify-syntax-entry ?# "!" a68-mode-syntax-table)
4068 (dolist (ch '(?- ?+ ?= ?< ?> ?* ?/ ?| ?&))
4069 (modify-syntax-entry ch "." a68-mode-syntax-table))
4071 (make-local-variable 'font-lock-keywords)
4074 (let ((word "COMMENT"))
4075 (do ((regexp (concat "[^" (substring word 0 1) "]+")
4076 (concat regexp "\\|"
4077 (substring word 0 i)
4078 "[^" (substring word i (1+ i)) "]"))
4080 ((>= i (length word)) regexp)))))
4081 (setq font-lock-keywords
4082 (list (list (concat "\\<COMMENT\\>"
4083 "\\(" not-comment "\\)\\{0,5\\}"
4084 "\\(\\'\\|\\<COMMENT\\>\\)")
4085 '(0 font-lock-comment-face))
4086 (list (concat "\\<CO\\>"
4087 "\\([^C]+\\|C[^O]\\)\\{0,5\\}"
4088 "\\($\\|\\<CO\\>\\)")
4089 '(0 font-lock-comment-face))
4090 (list "\\<[A-Z_]+\\>"
4091 '(0 font-lock-keyword-face))
4095 "\\([eE][-+]?[0-9]+\\)?"
4097 '(0 mdw-number-face))
4098 (list "\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/"
4099 '(0 mdw-punct-face))))))
4101 (dolist (hook '(a68-mode-hook a68-mode-hooks))
4102 (add-hook hook 'mdw-misc-mode-config t)
4103 (add-hook hook 'mdw-fontify-algol-68 t))
4105 ;;;--------------------------------------------------------------------------
4106 ;;; REXX configuration.
4108 (defun mdw-rexx-electric-* ()
4113 (defun mdw-rexx-indent-newline-indent ()
4116 (if abbrev-mode (expand-abbrev))
4117 (newline-and-indent))
4119 (defun mdw-fontify-rexx ()
4121 ;; Various bits of fiddling.
4122 (setq mdw-auto-indent nil)
4123 (local-set-key [?\C-m] 'mdw-rexx-indent-newline-indent)
4124 (local-set-key [?*] 'mdw-rexx-electric-*)
4125 (dolist (ch '(?! ?? ?# ?@ ?$)) (modify-syntax-entry ch "w"))
4126 (dolist (ch '(?¬)) (modify-syntax-entry ch "."))
4127 (mdw-standard-fill-prefix "\\([ \t]*/?\*[ \t]*\\)")
4129 ;; Set up keywords and things for fontification.
4130 (make-local-variable 'font-lock-keywords-case-fold-search)
4131 (setq font-lock-keywords-case-fold-search t)
4133 (setq rexx-indent 2)
4134 (setq rexx-end-indent rexx-indent)
4135 (setq rexx-cont-indent rexx-indent)
4137 (make-local-variable 'font-lock-keywords)
4138 (let ((rexx-keywords
4139 (mdw-regexps "address" "arg" "by" "call" "digits" "do" "drop"
4140 "else" "end" "engineering" "exit" "expose" "for"
4141 "forever" "form" "fuzz" "if" "interpret" "iterate"
4142 "leave" "linein" "name" "nop" "numeric" "off" "on"
4143 "options" "otherwise" "parse" "procedure" "pull"
4144 "push" "queue" "return" "say" "select" "signal"
4145 "scientific" "source" "then" "trace" "to" "until"
4146 "upper" "value" "var" "version" "when" "while"
4149 "abbrev" "abs" "bitand" "bitor" "bitxor" "b2x"
4150 "center" "center" "charin" "charout" "chars"
4151 "compare" "condition" "copies" "c2d" "c2x"
4152 "datatype" "date" "delstr" "delword" "d2c" "d2x"
4153 "errortext" "format" "fuzz" "insert" "lastpos"
4154 "left" "length" "lineout" "lines" "max" "min"
4155 "overlay" "pos" "queued" "random" "reverse" "right"
4156 "sign" "sourceline" "space" "stream" "strip"
4157 "substr" "subword" "symbol" "time" "translate"
4158 "trunc" "value" "verify" "word" "wordindex"
4159 "wordlength" "wordpos" "words" "xrange" "x2b" "x2c"
4162 (setq font-lock-keywords
4165 ;; Set up the keywords defined above.
4166 (list (concat "\\<\\(" rexx-keywords "\\)\\>")
4167 '(0 font-lock-keyword-face))
4169 ;; Fontify all symbols the same way.
4170 (list (concat "\\<\\([0-9.][A-Za-z0-9.!?_#@$]*[Ee][+-]?[0-9]+\\|"
4171 "[A-Za-z0-9.!?_#@$]+\\)")
4172 '(0 font-lock-variable-name-face))
4174 ;; And everything else is punctuation.
4175 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4176 '(0 mdw-punct-face))))))
4179 (add-hook 'rexx-mode-hook 'mdw-misc-mode-config t)
4180 (add-hook 'rexx-mode-hook 'mdw-fontify-rexx t))
4182 ;;;--------------------------------------------------------------------------
4183 ;;; Standard ML programming style.
4185 (setq-default sml-nested-if-indent t
4188 sml-type-of-indent nil)
4190 (defun mdw-fontify-sml ()
4192 ;; Make underscore an honorary letter.
4193 (modify-syntax-entry ?' "w")
4196 (mdw-standard-fill-prefix "\\([ \t]*(\*[ \t]*\\)")
4198 ;; Now define fontification things.
4199 (make-local-variable 'font-lock-keywords)
4201 (mdw-regexps "abstype" "and" "andalso" "as"
4204 "else" "end" "eqtype" "exception"
4205 "fn" "fun" "functor"
4207 "if" "in" "include" "infix" "infixr"
4210 "of" "op" "open" "orelse"
4212 "sharing" "sig" "signature" "struct" "structure"
4215 "where" "while" "with" "withtype")))
4217 (setq font-lock-keywords
4220 ;; Set up the keywords defined above.
4221 (list (concat "\\<\\(" sml-keywords "\\)\\>")
4222 '(0 font-lock-keyword-face))
4224 ;; At least numbers are simpler than C.
4225 (list (concat "\\<\\~?"
4226 "\\(0\\([wW]?[xX][0-9a-fA-F]+\\|"
4228 "\\([0-9]+\\(\\.[0-9]+\\)?"
4231 '(0 mdw-number-face))
4233 ;; And anything else is punctuation.
4234 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4235 '(0 mdw-punct-face))))))
4238 (add-hook 'sml-mode-hook 'mdw-misc-mode-config t)
4239 (add-hook 'sml-mode-hook 'mdw-fontify-sml t))
4241 ;;;--------------------------------------------------------------------------
4242 ;;; Haskell configuration.
4244 (setq-default haskell-indent-offset 2)
4246 (defun mdw-fontify-haskell ()
4248 ;; Fiddle with syntax table to get comments right.
4249 (modify-syntax-entry ?' "_")
4250 (modify-syntax-entry ?- ". 12")
4251 (modify-syntax-entry ?\n ">")
4253 ;; Make punctuation be punctuation
4254 (let ((punct "=<>+-*/|&%!@?$.^:#`"))
4256 ((>= i (length punct)))
4257 (modify-syntax-entry (aref punct i) ".")))
4260 (mdw-standard-fill-prefix "\\([ \t]*{?--?[ \t]*\\)")
4262 ;; Fiddle with fontification.
4263 (make-local-variable 'font-lock-keywords)
4264 (let ((haskell-keywords
4266 "case" "ccall" "class"
4267 "data" "default" "deriving" "do"
4271 "if" "import" "in" "infix" "infixl" "infixr" "instance"
4284 (mdw-regexps "ACK" "BEL" "BS" "CAN" "CR" "DC1" "DC2" "DC3" "DC4"
4285 "DEL" "DLE" "EM" "ENQ" "EOT" "ESC" "ETB" "ETX" "FF"
4286 "FS" "GS" "HT" "LF" "NAK" "NUL" "RS" "SI" "SO" "SOH"
4287 "SP" "STX" "SUB" "SYN" "US" "VT")))
4289 (setq font-lock-keywords
4291 (list (concat "{-" "[^-]*" "\\(-+[^-}][^-]*\\)*"
4295 '(0 font-lock-comment-face))
4296 (list (concat "\\_<\\(" haskell-keywords "\\)\\_>")
4297 '(0 font-lock-keyword-face))
4298 (list (concat "'\\("
4302 "\\(" "[abfnrtv\\\"']" "\\|"
4303 "^" "\\(" control-sequences "\\|"
4304 "[]A-Z@[\\^_]" "\\)" "\\|"
4311 '(0 font-lock-string-face))
4312 (list "\\_<[A-Z]\\(\\sw+\\|\\s_+\\)*\\_>"
4313 '(0 font-lock-variable-name-face))
4314 (list (concat "\\_<0\\([xX][0-9a-fA-F]+\\|[oO][0-7]+\\)\\|"
4315 "\\_<[0-9]+\\(\\.[0-9]*\\)?"
4316 "\\([eE][-+]?[0-9]+\\)?")
4317 '(0 mdw-number-face))
4318 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4319 '(0 mdw-punct-face))))))
4322 (add-hook 'haskell-mode-hook 'mdw-misc-mode-config t)
4323 (add-hook 'haskell-mode-hook 'mdw-fontify-haskell t))
4325 ;;;--------------------------------------------------------------------------
4326 ;;; Erlang configuration.
4328 (setq-default erlang-electric-commands nil)
4330 (defun mdw-fontify-erlang ()
4333 (mdw-standard-fill-prefix "\\([ \t]*{?%*[ \t]*\\)")
4335 ;; Fiddle with fontification.
4336 (make-local-variable 'font-lock-keywords)
4337 (let ((erlang-keywords
4338 (mdw-regexps "after" "and" "andalso"
4339 "band" "begin" "bnot" "bor" "bsl" "bsr" "bxor"
4340 "case" "catch" "cond"
4341 "div" "end" "fun" "if" "let" "not"
4343 "query" "receive" "rem" "try" "when" "xor")))
4345 (setq font-lock-keywords
4348 '(0 font-lock-comment-face))
4349 (list (concat "\\<\\(" erlang-keywords "\\)\\>")
4350 '(0 font-lock-keyword-face))
4351 (list (concat "^-\\sw+\\>")
4352 '(0 font-lock-keyword-face))
4353 (list "\\<[0-9]+\\(#[0-9a-zA-Z]+\\|[eE][+-]?[0-9]+\\)?\\>"
4354 '(0 mdw-number-face))
4355 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4356 '(0 mdw-punct-face))))))
4359 (add-hook 'erlang-mode-hook 'mdw-misc-mode-config t)
4360 (add-hook 'erlang-mode-hook 'mdw-fontify-erlang t))
4362 ;;;--------------------------------------------------------------------------
4363 ;;; Texinfo configuration.
4365 (defun mdw-fontify-texinfo ()
4368 (mdw-standard-fill-prefix "\\([ \t]*@c[ \t]+\\)")
4370 ;; Real fontification things.
4371 (make-local-variable 'font-lock-keywords)
4372 (setq font-lock-keywords
4375 ;; Environment names are keywords.
4376 (list "@\\(end\\) *\\([a-zA-Z]*\\)?"
4377 '(2 font-lock-keyword-face))
4379 ;; Unmark escaped magic characters.
4380 (list "\\(@\\)\\([@{}]\\)"
4381 '(1 font-lock-keyword-face)
4382 '(2 font-lock-variable-name-face))
4384 ;; Make sure we get comments properly.
4385 (list "@c\\(omment\\)?\\( .*\\)?$"
4386 '(0 font-lock-comment-face))
4388 ;; Command names are keywords.
4389 (list "@\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)"
4390 '(0 font-lock-keyword-face))
4392 ;; Fontify TeX special characters as punctuation.
4394 '(0 mdw-punct-face)))))
4396 (dolist (hook '(texinfo-mode-hook TeXinfo-mode-hook))
4397 (add-hook hook 'mdw-misc-mode-config t)
4398 (add-hook hook 'mdw-fontify-texinfo t))
4400 ;;;--------------------------------------------------------------------------
4401 ;;; TeX and LaTeX configuration.
4403 (setq-default LaTeX-table-label "tbl:"
4404 TeX-auto-untabify nil
4405 LaTeX-syntactic-comments nil
4406 LaTeX-fill-break-at-separators '(\\\[))
4408 (defun mdw-fontify-tex ()
4409 (setq ispell-parser 'tex)
4412 ;; Don't make maths into a string.
4413 (modify-syntax-entry ?$ ".")
4414 (modify-syntax-entry ?$ "." font-lock-syntax-table)
4415 (local-set-key [?$] 'self-insert-command)
4417 ;; Make `tab' be useful, given that tab stops in TeX don't work well.
4418 (local-set-key "\C-\M-i" 'indent-relative)
4419 (setq indent-tabs-mode nil)
4422 (mdw-standard-fill-prefix "\\([ \t]*%+[ \t]*\\)")
4424 ;; Real fontification things.
4425 (make-local-variable 'font-lock-keywords)
4426 (setq font-lock-keywords
4429 ;; Environment names are keywords.
4430 (list (concat "\\\\\\(begin\\|end\\|newenvironment\\)"
4432 '(2 font-lock-keyword-face))
4434 ;; Suspended environment names are keywords too.
4435 (list (concat "\\\\\\(suspend\\|resume\\)\\(\\[[^]]*\\]\\)?"
4437 '(3 font-lock-keyword-face))
4439 ;; Command names are keywords.
4440 (list "\\\\\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)"
4441 '(0 font-lock-keyword-face))
4443 ;; Handle @/.../ for italics.
4444 ;; (list "\\(@/\\)\\([^/]*\\)\\(/\\)"
4445 ;; '(1 font-lock-keyword-face)
4446 ;; '(3 font-lock-keyword-face))
4448 ;; Handle @*...* for boldness.
4449 ;; (list "\\(@\\*\\)\\([^*]*\\)\\(\\*\\)"
4450 ;; '(1 font-lock-keyword-face)
4451 ;; '(3 font-lock-keyword-face))
4453 ;; Handle @`...' for literal syntax things.
4454 ;; (list "\\(@`\\)\\([^']*\\)\\('\\)"
4455 ;; '(1 font-lock-keyword-face)
4456 ;; '(3 font-lock-keyword-face))
4458 ;; Handle @<...> for nonterminals.
4459 ;; (list "\\(@<\\)\\([^>]*\\)\\(>\\)"
4460 ;; '(1 font-lock-keyword-face)
4461 ;; '(3 font-lock-keyword-face))
4463 ;; Handle other @-commands.
4464 ;; (list "@\\([^a-zA-Z]\\|[a-zA-Z]*\\)"
4465 ;; '(0 font-lock-keyword-face))
4467 ;; Make sure we get comments properly.
4469 '(0 font-lock-comment-face))
4471 ;; Fontify TeX special characters as punctuation.
4473 '(0 mdw-punct-face)))))
4475 (setq TeX-install-font-lock 'tex-font-setup)
4477 (eval-after-load 'font-latex
4478 '(defun font-latex-jit-lock-force-redisplay (buf start end)
4479 "Compatibility for Emacsen not offering `jit-lock-force-redisplay'."
4480 ;; The following block is an expansion of `jit-lock-force-redisplay'
4481 ;; and involved macros taken from CVS Emacs on 2007-04-28.
4482 (with-current-buffer buf
4483 (let ((modified (buffer-modified-p)))
4485 (let ((buffer-undo-list t)
4486 (inhibit-read-only t)
4487 (inhibit-point-motion-hooks t)
4488 (inhibit-modification-hooks t)
4491 buffer-file-truename)
4492 (put-text-property start end 'fontified t))
4494 (restore-buffer-modified-p nil)))))))
4496 (setq TeX-output-view-style
4498 ("^landscape$" "^pstricks$\\|^pst-\\|^psfrag$")
4499 "%(o?)dvips -t landscape %d -o && xdg-open %f")
4500 ("^dvi$" "^pstricks$\\|^pst-\\|^psfrag$"
4501 "%(o?)dvips %d -o && xdg-open %f")
4503 ("^a4\\(?:dutch\\|paper\\|wide\\)\\|sem-a4$" "^landscape$")
4504 "%(o?)xdvi %dS -paper a4r -s 0 %d")
4505 ("^dvi$" "^a4\\(?:dutch\\|paper\\|wide\\)\\|sem-a4$"
4506 "%(o?)xdvi %dS -paper a4 %d")
4508 ("^a5\\(?:comb\\|paper\\)$" "^landscape$")
4509 "%(o?)xdvi %dS -paper a5r -s 0 %d")
4510 ("^dvi$" "^a5\\(?:comb\\|paper\\)$" "%(o?)xdvi %dS -paper a5 %d")
4511 ("^dvi$" "^b5paper$" "%(o?)xdvi %dS -paper b5 %d")
4512 ("^dvi$" "^letterpaper$" "%(o?)xdvi %dS -paper us %d")
4513 ("^dvi$" "^legalpaper$" "%(o?)xdvi %dS -paper legal %d")
4514 ("^dvi$" "^executivepaper$" "%(o?)xdvi %dS -paper 7.25x10.5in %d")
4515 ("^dvi$" "." "%(o?)xdvi %dS %d")
4516 ("^pdf$" "." "xdg-open %o")
4517 ("^html?$" "." "sensible-browser %o")))
4519 (setq TeX-view-program-list
4520 '(("mupdf" ("mupdf %o" (mode-io-correlate " %(outpage)")))))
4522 (setq TeX-view-program-selection
4523 '(((output-dvi style-pstricks) "dvips and gv")
4525 (output-pdf "mupdf")
4526 (output-html "sensible-browser")))
4528 (setq TeX-open-quote "\""
4529 TeX-close-quote "\"")
4531 (setq reftex-use-external-file-finders t
4532 reftex-auto-recenter-toc t)
4534 (setq reftex-label-alist
4535 '(("theorem" ?T "th:" "~\\ref{%s}" t ("theorems?" "th\\.") -2)
4536 ("axiom" ?A "ax:" "~\\ref{%s}" t ("axioms?" "ax\\.") -2)
4537 ("definition" ?D "def:" "~\\ref{%s}" t ("definitions?" "def\\.") -2)
4538 ("proposition" ?P "prop:" "~\\ref{%s}" t
4539 ("propositions?" "prop\\.") -2)
4540 ("lemma" ?L "lem:" "~\\ref{%s}" t ("lemmas?" "lem\\.") -2)
4541 ("example" ?X "eg:" "~\\ref{%s}" t ("examples?") -2)
4542 ("exercise" ?E "ex:" "~\\ref{%s}" t ("exercises?" "ex\\.") -2)
4543 ("enumerate" ?i "i:" "~\\ref{%s}" item ("items?"))))
4544 (setq reftex-section-prefixes
4549 (setq bibtex-field-delimiters 'double-quotes
4550 bibtex-align-at-equal-sign t
4551 bibtex-entry-format '(realign opts-or-alts required-fields
4552 numerical-fields last-comma delimiters
4553 unify-case sort-fields braces)
4554 bibtex-sort-ignore-string-entries nil
4555 bibtex-maintain-sorted-entries 'entry-class
4556 bibtex-include-OPTkey t
4557 bibtex-autokey-names-stretch 1
4558 bibtex-autokey-expand-strings t
4559 bibtex-autokey-name-separator "-"
4560 bibtex-autokey-year-length 4
4561 bibtex-autokey-titleword-separator "-"
4562 bibtex-autokey-name-year-separator "-"
4563 bibtex-autokey-year-title-separator ":")
4566 (dolist (hook '(tex-mode-hook latex-mode-hook
4567 TeX-mode-hook LaTeX-mode-hook))
4568 (add-hook hook 'mdw-misc-mode-config t)
4569 (add-hook hook 'mdw-fontify-tex t))
4570 (add-hook 'bibtex-mode-hook (lambda () (setq fill-column 76))))
4572 ;;;--------------------------------------------------------------------------
4573 ;;; HTML, CSS, and other web foolishness.
4575 (setq-default css-indent-offset 8)
4577 ;;;--------------------------------------------------------------------------
4580 (setq-default psgml-html-build-new-buffer nil)
4582 (defun mdw-sgml-mode ()
4585 (mdw-standard-fill-prefix "")
4586 (make-local-variable 'sgml-delimiters)
4587 (setq sgml-delimiters
4588 '("AND" "&" "COM" "--" "CRO" "&#" "DSC" "]" "DSO" "[" "DTGC" "]"
4589 "DTGO" "[" "ERO" "&" "ETAGO" ":e" "GRPC" ")" "GRPO" "(" "LIT"
4590 "\"" "LITA" "'" "MDC" ">" "MDO" "<!" "MINUS" "-" "MSC" "]]"
4591 "NESTC" "{" "NET" "}" "OPT" "?" "OR" "|" "PERO" "%" "PIC" ">"
4592 "PIO" "<?" "PLUS" "+" "REFC" "." "REP" "*" "RNI" "#" "SEQ" ","
4593 "STAGO" ":" "TAGC" "." "VI" "=" "MS-START" "<![" "MS-END" "]]>"
4594 "XML-ECOM" "-->" "XML-PIC" "?>" "XML-SCOM" "<!--" "XML-TAGCE"
4596 (setq major-mode 'mdw-sgml-mode)
4597 (setq mode-name "[mdw] SGML")
4598 (run-hooks 'mdw-sgml-mode-hook))
4600 ;;;--------------------------------------------------------------------------
4601 ;;; Configuration files.
4603 (defcustom mdw-conf-quote-normal nil
4604 "Control syntax category of quote characters `\"' and `''.
4605 If this is `t', consider quote characters to be normal
4606 punctuation, as for `conf-quote-normal'. If this is `nil' then
4607 leave quote characters as quotes. If this is a list, then
4608 consider the quote characters in the list to be normal
4609 punctuation. If this is a single quote character, then consider
4610 that character only to be normal punctuation."
4611 :type '(choice boolean character (repeat character))
4612 :safe 'mdw-conf-quote-normal-acceptable-value-p)
4613 (defun mdw-conf-quote-normal-acceptable-value-p (value)
4614 "Is the VALUE is an acceptable value for `mdw-conf-quote-normal'?"
4615 (or (booleanp value)
4616 (every (lambda (v) (memq v '(?\" ?')))
4617 (if (listp value) value (list value)))))
4619 (defun mdw-fix-up-quote ()
4620 "Apply the setting of `mdw-conf-quote-normal'."
4621 (let ((flag mdw-conf-quote-normal))
4623 (conf-quote-normal t))
4627 (let ((table (copy-syntax-table (syntax-table))))
4628 (dolist (ch (if (listp flag) flag (list flag)))
4629 (modify-syntax-entry ch "." table))
4630 (set-syntax-table table)
4631 (and font-lock-mode (font-lock-fontify-buffer)))))))
4634 (add-hook 'conf-mode-hook 'mdw-misc-mode-config t)
4635 (add-hook 'conf-mode-local-variables-hook 'mdw-fix-up-quote t t))
4637 ;;;--------------------------------------------------------------------------
4640 (defun mdw-setup-sh-script-mode ()
4642 ;; Fetch the shell interpreter's name.
4643 (let ((shell-name sh-shell-file))
4645 ;; Try reading the hash-bang line.
4647 (goto-char (point-min))
4648 (if (looking-at "#![ \t]*\\([^ \t\n]*\\)")
4649 (setq shell-name (match-string 1))))
4651 ;; Now try to set the shell.
4653 ;; Don't let `sh-set-shell' bugger up my script.
4654 (let ((executable-set-magic #'(lambda (s &rest r) s)))
4655 (sh-set-shell shell-name)))
4657 ;; Don't insert here-document scaffolding automatically.
4658 (local-set-key "<" 'self-insert-command)
4660 ;; Now enable my keys and the fontification.
4661 (mdw-misc-mode-config)
4663 ;; Set the indentation level correctly.
4664 (setq sh-indentation 2)
4665 (setq sh-basic-offset 2))
4667 (setq sh-shell-file "/bin/sh")
4669 ;; Awful hacking to override the shell detection for particular scripts.
4670 (defmacro define-custom-shell-mode (name shell)
4673 (set (make-local-variable 'sh-shell-file) ,shell)
4675 (define-custom-shell-mode bash-mode "/bin/bash")
4676 (define-custom-shell-mode rc-mode "/usr/bin/rc")
4677 (put 'sh-shell-file 'permanent-local t)
4679 ;; Hack the rc syntax table. Backquotes aren't paired in rc.
4680 (eval-after-load "sh-script"
4681 '(or (assq 'rc sh-mode-syntax-table-input)
4698 (assoc (assq 'rc sh-mode-syntax-table-input)))
4701 (setq sh-mode-syntax-table-input
4702 (cons (cons 'rc frag)
4703 sh-mode-syntax-table-input))))))
4706 (add-hook 'sh-mode-hook 'mdw-misc-mode-config t)
4707 (add-hook 'sh-mode-hook 'mdw-setup-sh-script-mode t))
4709 ;;;--------------------------------------------------------------------------
4710 ;;; Emacs shell mode.
4712 (defun mdw-eshell-prompt ()
4713 (let ((left "[") (right "]"))
4714 (when (= (user-uid) 0)
4715 (setq left "«" right "»"))
4718 (replace-regexp-in-string "\\..*$" "" (system-name)))
4720 (let* ((pwd (eshell/pwd)) (npwd (length pwd))
4721 (home (expand-file-name "~")) (nhome (length home)))
4722 (if (and (>= npwd nhome)
4724 (= (elt pwd nhome) ?/))
4725 (string= (substring pwd 0 nhome) home))
4726 (concat "~" (substring pwd (length home)))
4729 (setq-default eshell-prompt-function 'mdw-eshell-prompt)
4730 (setq-default eshell-prompt-regexp "^\\[[^]>]+\\(\\]\\|>>?\\)")
4732 (defun eshell/e (file) (find-file file) nil)
4733 (defun eshell/ee (file) (find-file-other-window file) nil)
4734 (defun eshell/w3m (url) (w3m-goto-url url) nil)
4736 (mdw-define-face eshell-prompt (t :weight bold))
4737 (mdw-define-face eshell-ls-archive (t :weight bold :foreground "red"))
4738 (mdw-define-face eshell-ls-backup (t :foreground "lightgrey" :slant italic))
4739 (mdw-define-face eshell-ls-product (t :foreground "lightgrey" :slant italic))
4740 (mdw-define-face eshell-ls-clutter (t :foreground "lightgrey" :slant italic))
4741 (mdw-define-face eshell-ls-executable (t :weight bold))
4742 (mdw-define-face eshell-ls-directory (t :foreground "cyan" :weight bold))
4743 (mdw-define-face eshell-ls-readonly (t nil))
4744 (mdw-define-face eshell-ls-symlink (t :foreground "cyan"))
4746 (defun mdw-eshell-hack () (setenv "LD_PRELOAD" nil))
4747 (add-hook 'eshell-mode-hook 'mdw-eshell-hack)
4749 ;;;--------------------------------------------------------------------------
4750 ;;; Messages-file mode.
4752 (defun messages-mode-guts ()
4753 (setq messages-mode-syntax-table (make-syntax-table))
4754 (set-syntax-table messages-mode-syntax-table)
4755 (modify-syntax-entry ?0 "w" messages-mode-syntax-table)
4756 (modify-syntax-entry ?1 "w" messages-mode-syntax-table)
4757 (modify-syntax-entry ?2 "w" messages-mode-syntax-table)
4758 (modify-syntax-entry ?3 "w" messages-mode-syntax-table)
4759 (modify-syntax-entry ?4 "w" messages-mode-syntax-table)
4760 (modify-syntax-entry ?5 "w" messages-mode-syntax-table)
4761 (modify-syntax-entry ?6 "w" messages-mode-syntax-table)
4762 (modify-syntax-entry ?7 "w" messages-mode-syntax-table)
4763 (modify-syntax-entry ?8 "w" messages-mode-syntax-table)
4764 (modify-syntax-entry ?9 "w" messages-mode-syntax-table)
4765 (make-local-variable 'comment-start)
4766 (make-local-variable 'comment-end)
4767 (make-local-variable 'indent-line-function)
4768 (setq indent-line-function 'indent-relative)
4769 (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
4770 (make-local-variable 'font-lock-defaults)
4771 (make-local-variable 'messages-mode-keywords)
4773 (mdw-regexps "array" "bitmap" "callback" "docs[ \t]+enum"
4774 "export" "enum" "fixed-octetstring" "flags"
4775 "harmless" "map" "nested" "optional"
4776 "optional-tagged" "package" "primitive"
4777 "primitive-nullfree" "relaxed[ \t]+enum"
4778 "set" "table" "tagged-optional" "union"
4779 "variadic" "vector" "version" "version-tag")))
4780 (setq messages-mode-keywords
4782 (list (concat "\\<\\(" keywords "\\)\\>:")
4783 '(0 font-lock-keyword-face))
4784 '("\\([-a-zA-Z0-9]+:\\)" (0 font-lock-warning-face))
4785 '("\\(\\<[a-z][-_a-zA-Z0-9]*\\)"
4786 (0 font-lock-variable-name-face))
4787 '("\\<\\([0-9]+\\)\\>" (0 mdw-number-face))
4788 '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4789 (0 mdw-punct-face)))))
4790 (setq font-lock-defaults
4791 '(messages-mode-keywords nil nil nil nil))
4792 (run-hooks 'messages-file-hook))
4794 (defun messages-mode ()
4797 (setq major-mode 'messages-mode)
4798 (setq mode-name "Messages")
4799 (messages-mode-guts)
4800 (modify-syntax-entry ?# "<" messages-mode-syntax-table)
4801 (modify-syntax-entry ?\n ">" messages-mode-syntax-table)
4802 (setq comment-start "# ")
4803 (setq comment-end "")
4804 (run-hooks 'messages-mode-hook))
4806 (defun cpp-messages-mode ()
4809 (setq major-mode 'cpp-messages-mode)
4810 (setq mode-name "CPP Messages")
4811 (messages-mode-guts)
4812 (modify-syntax-entry ?* ". 23" messages-mode-syntax-table)
4813 (modify-syntax-entry ?/ ". 14" messages-mode-syntax-table)
4814 (setq comment-start "/* ")
4815 (setq comment-end " */")
4816 (let ((preprocessor-keywords
4817 (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
4818 "ident" "if" "ifdef" "ifndef" "import" "include"
4819 "line" "pragma" "unassert" "undef" "warning")))
4820 (setq messages-mode-keywords
4821 (append (list (list (concat "^[ \t]*\\#[ \t]*"
4822 "\\(include\\|import\\)"
4823 "[ \t]*\\(<[^>]+\\(>\\)?\\)")
4824 '(2 font-lock-string-face))
4825 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
4826 preprocessor-keywords
4827 "\\)\\>\\|[0-9]+\\|$\\)\\)")
4828 '(1 font-lock-keyword-face)))
4829 messages-mode-keywords)))
4830 (run-hooks 'cpp-messages-mode-hook))
4833 (add-hook 'messages-mode-hook 'mdw-misc-mode-config t)
4834 (add-hook 'cpp-messages-mode-hook 'mdw-misc-mode-config t)
4835 ;; (add-hook 'messages-file-hook 'mdw-fontify-messages t)
4838 ;;;--------------------------------------------------------------------------
4839 ;;; Messages-file mode.
4841 (defvar mallow-driver-substitution-face 'mallow-driver-substitution-face
4842 "Face to use for subsittution directives.")
4843 (make-face 'mallow-driver-substitution-face)
4844 (defvar mallow-driver-text-face 'mallow-driver-text-face
4845 "Face to use for body text.")
4846 (make-face 'mallow-driver-text-face)
4848 (defun mallow-driver-mode ()
4851 (setq major-mode 'mallow-driver-mode)
4852 (setq mode-name "Mallow driver")
4853 (setq mallow-driver-mode-syntax-table (make-syntax-table))
4854 (set-syntax-table mallow-driver-mode-syntax-table)
4855 (make-local-variable 'comment-start)
4856 (make-local-variable 'comment-end)
4857 (make-local-variable 'indent-line-function)
4858 (setq indent-line-function 'indent-relative)
4859 (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
4860 (make-local-variable 'font-lock-defaults)
4861 (make-local-variable 'mallow-driver-mode-keywords)
4863 (mdw-regexps "each" "divert" "file" "if"
4864 "perl" "set" "string" "type" "write")))
4865 (setq mallow-driver-mode-keywords
4867 (list (concat "^%\\s *\\(}\\|\\(" keywords "\\)\\>\\).*$")
4868 '(0 font-lock-keyword-face))
4869 (list "^%\\s *\\(#.*\\)?$"
4870 '(0 font-lock-comment-face))
4872 '(0 font-lock-keyword-face))
4873 (list "^|?\\(.+\\)$" '(1 mallow-driver-text-face))
4875 '(0 mallow-driver-substitution-face t)))))
4876 (setq font-lock-defaults
4877 '(mallow-driver-mode-keywords nil nil nil nil))
4878 (modify-syntax-entry ?\" "_" mallow-driver-mode-syntax-table)
4879 (modify-syntax-entry ?\n ">" mallow-driver-mode-syntax-table)
4880 (setq comment-start "%# ")
4881 (setq comment-end "")
4882 (run-hooks 'mallow-driver-mode-hook))
4885 (add-hook 'mallow-driver-hook 'mdw-misc-mode-config t))
4887 ;;;--------------------------------------------------------------------------
4890 (defun nfast-debug-mode ()
4893 (setq major-mode 'nfast-debug-mode)
4894 (setq mode-name "NFast debug")
4895 (setq messages-mode-syntax-table (make-syntax-table))
4896 (set-syntax-table messages-mode-syntax-table)
4897 (make-local-variable 'font-lock-defaults)
4898 (make-local-variable 'nfast-debug-mode-keywords)
4899 (setq truncate-lines t)
4900 (setq nfast-debug-mode-keywords
4902 '("^\\(NFast_\\(Connect\\|Disconnect\\|Submit\\|Wait\\)\\)"
4903 (0 font-lock-keyword-face))
4904 (list (concat "^[ \t]+\\(\\("
4905 "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
4906 "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
4908 "[0-9a-fA-F]+\\)[ \t]*$")
4909 '(0 mdw-number-face))
4910 '("^[ \t]+\.status=[ \t]+\\<\\(OK\\)\\>"
4911 (1 font-lock-keyword-face))
4912 '("^[ \t]+\.status=[ \t]+\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>"
4913 (1 font-lock-warning-face))
4914 '("^[ \t]+\.status[ \t]+\\<\\(zero\\)\\>"
4916 (list (concat "^[ \t]+\\.cmd=[ \t]+"
4917 "\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>")
4918 '(1 font-lock-keyword-face))
4919 '("-?\\<\\([0-9]+\\|0x[0-9a-fA-F]+\\)\\>" (0 mdw-number-face))
4920 '("^\\([ \t]+[a-z0-9.]+\\)" (0 font-lock-variable-name-face))
4921 '("\\<\\([a-z][a-z0-9.]+\\)\\>=" (1 font-lock-variable-name-face))
4922 '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" (0 mdw-punct-face))))
4923 (setq font-lock-defaults
4924 '(nfast-debug-mode-keywords nil nil nil nil))
4925 (run-hooks 'nfast-debug-mode-hook))
4927 ;;;--------------------------------------------------------------------------
4928 ;;; Lispy languages.
4930 ;; Unpleasant bodge.
4931 (unless (boundp 'slime-repl-mode-map)
4932 (setq slime-repl-mode-map (make-sparse-keymap)))
4934 (defun mdw-indent-newline-and-indent ()
4936 (indent-for-tab-command)
4937 (newline-and-indent))
4939 (eval-after-load "cl-indent"
4941 (mapc #'(lambda (pair)
4943 'common-lisp-indent-function
4945 '((destructuring-bind . ((&whole 4 &rest 1) 4 &body))
4946 (multiple-value-bind . ((&whole 4 &rest 1) 4 &body))))))
4948 (defun mdw-common-lisp-indent ()
4949 (make-local-variable 'lisp-indent-function)
4950 (setq lisp-indent-function 'common-lisp-indent-function))
4952 (defmacro mdw-advise-hyperspec-lookup (func args)
4953 `(defadvice ,func (around mdw-browse-w3m ,args activate compile)
4955 (let ((browse-url-browser-function #'mdw-w3m-browse-url))
4958 (mdw-advise-hyperspec-lookup common-lisp-hyperspec (symbol))
4959 (mdw-advise-hyperspec-lookup common-lisp-hyperspec-format (char))
4960 (mdw-advise-hyperspec-lookup common-lisp-hyperspec-lookup-reader-macro (char))
4962 (defun mdw-fontify-lispy ()
4965 (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)")
4967 ;; Not much fontification needed.
4968 (make-local-variable 'font-lock-keywords)
4969 (setq font-lock-keywords
4970 (list (list (concat "\\("
4972 "\\(" "[0-9]+/[0-9]+"
4973 "\\|" "\\(" "[0-9]+" "\\(\\.[0-9]*\\)?" "\\|"
4975 "\\([dDeEfFlLsS][-+]?[0-9]+\\)?"
4980 "[0-9A-Fa-f]+" "\\(/[0-9A-Fa-f]+\\)?"
4981 "\\|" "o" "[-+]?" "[0-7]+" "\\(/[0-7]+\\)?"
4982 "\\|" "b" "[-+]?" "[01]+" "\\(/[01]+\\)?"
4983 "\\|" "[0-9]+" "r" "[-+]?"
4984 "[0-9a-zA-Z]+" "\\(/[0-9a-zA-Z]+\\)?"
4987 '(0 mdw-number-face))
4988 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4989 '(0 mdw-punct-face)))))
4991 ;; Special indentation.
4993 (defcustom mdw-lisp-loop-default-indent 2
4994 "Default indent for simple `loop' body."
4997 (defcustom mdw-lisp-setf-value-indent 2
4998 "Default extra indent for `setf' values."
4999 :type 'integer :safe 'integerp)
5001 (setq lisp-simple-loop-indentation 0
5002 lisp-loop-keyword-indentation 0
5003 lisp-loop-forms-indentation 2
5004 lisp-lambda-list-keyword-parameter-alignment t)
5006 (defun mdw-indent-funcall
5007 (path state &optional indent-point sexp-column normal-indent)
5008 "Indent `funcall' more usefully.
5009 Essentially, treat `funcall foo' as a function name, and align the arguments
5011 (and (or (not (consp path)) (null (cadr path)))
5013 (goto-char (cadr state))
5015 (let ((start-line (line-number-at-pos)))
5016 (and (condition-case nil (progn (forward-sexp 3) t)
5020 (and (= start-line (line-number-at-pos))
5021 (current-column))))))))
5023 (put 'funcall 'common-lisp-indent-function 'mdw-indent-funcall)
5024 (put 'funcall 'lisp-indent-function 'mdw-indent-funcall))
5026 (defun mdw-indent-setf
5027 (path state &optional indent-point sexp-column normal-indent)
5028 "Indent `setf' more usefully.
5029 If the values aren't on the same lines as their variables then indent them
5030 by `mdw-lisp-setf-value-indent' spaces."
5031 (and (or (not (consp path)) (null (cadr path)))
5032 (let ((basic-indent (save-excursion
5033 (goto-char (cadr state))
5035 (and (condition-case nil
5036 (progn (forward-sexp 2) t)
5040 (current-column)))))
5041 (offset (if (consp path) (car path)
5046 (goto-char (cadr state))
5048 (while (< (point) start)
5049 (condition-case nil (forward-sexp 1)
5050 (scan-error (throw 'done nil)))
5053 (and basic-indent offset
5054 (list (+ basic-indent
5056 mdw-lisp-setf-value-indent))
5059 (put 'setf 'common-lisp-indent-functopion 'mdw-indent-setf)
5060 (put 'psetf 'common-lisp-indent-function 'mdw-indent-setf)
5061 (put 'setq 'common-lisp-indent-function 'mdw-indent-setf)
5062 (put 'setf 'lisp-indent-function 'mdw-indent-setf)
5063 (put 'setq 'lisp-indent-function 'mdw-indent-setf)
5064 (put 'setq-local 'lisp-indent-function 'mdw-indent-setf)
5065 (put 'setq-default 'lisp-indent-function 'mdw-indent-setf))
5067 (defadvice common-lisp-loop-part-indentation
5068 (around mdw-fix-loop-indentation (indent-point state) activate compile)
5069 "Improve `loop' indentation.
5070 If the first subform is on the same line as the `loop' keyword, then
5071 align the other subforms beneath it. Otherwise, indent them
5072 `mdw-lisp-loop-default-indent' columns in from the opening parenthesis."
5074 (let* ((loop-indentation (save-excursion
5075 (goto-char (elt state 1))
5078 ;; Don't really care about this.
5079 (when (and (boundp 'lisp-indent-backquote-substitution-mode)
5080 (eq lisp-indent-backquote-substitution-mode 'corrected))
5082 (goto-char (elt state 1))
5083 (incf loop-indentation
5084 (cond ((eq (char-before) ?,) -1)
5085 ((and (eq (char-before) ?@)
5086 (progn (backward-char)
5087 (eq (char-before) ?,)))
5091 ;; If the first loop item is on the same line as the `loop' itself then
5092 ;; use that as the baseline. Otherwise advance by the default indent.
5093 (goto-char (cadr state))
5095 (let ((baseline-indent
5096 (if (= (line-number-at-pos)
5097 (if (condition-case nil (progn (forward-sexp 2) t)
5099 (progn (forward-sexp -1) (line-number-at-pos))
5102 (+ loop-indentation mdw-lisp-loop-default-indent))))
5104 (goto-char indent-point)
5107 (setq ad-return-value
5109 (cond ((condition-case ()
5111 (goto-char (elt state 1))
5115 (not (looking-at "\\(:\\|\\sw\\)")))
5117 (+ baseline-indent lisp-simple-loop-indentation))
5118 ((looking-at "^\\s-*\\(:?\\sw+\\|;\\)")
5119 (+ baseline-indent lisp-loop-keyword-indentation))
5121 (+ baseline-indent lisp-loop-forms-indentation)))
5123 ;; Tell the caller that the next line needs recomputation,
5124 ;; even though it doesn't start a sexp.
5125 loop-indentation)))))
5129 (defcustom mdw-friendly-name "[mdw]"
5130 "How I want to be addressed."
5133 (defadvice slime-user-first-name
5134 (around mdw-use-friendly-name compile activate)
5135 (if mdw-friendly-name (setq ad-return-value mdw-friendly-name)
5140 (if (not mdw-fast-startup)
5142 (require 'slime-autoloads)
5143 (slime-setup '(slime-autodoc slime-c-p-c))))))
5145 (let ((stuff '((cmucl ("cmucl"))
5146 (sbcl ("sbcl") :coding-system utf-8-unix)
5147 (clisp ("clisp") :coding-system utf-8-unix))))
5148 (or (boundp 'slime-lisp-implementations)
5149 (setq slime-lisp-implementations nil))
5151 (let* ((head (car stuff))
5152 (found (assq (car head) slime-lisp-implementations)))
5153 (setq stuff (cdr stuff))
5155 (rplacd found (cdr head))
5156 (setq slime-lisp-implementations
5157 (cons head slime-lisp-implementations))))))
5158 (setq slime-default-lisp 'sbcl)
5163 (dolist (hook '(emacs-lisp-mode-hook
5166 inferior-lisp-mode-hook
5167 lisp-interaction-mode-hook
5169 slime-repl-mode-hook))
5170 (add-hook hook 'mdw-misc-mode-config t)
5171 (add-hook hook 'mdw-fontify-lispy t))
5172 (add-hook 'lisp-mode-hook 'mdw-common-lisp-indent t)
5173 (add-hook 'inferior-lisp-mode-hook
5174 #'(lambda () (local-set-key "\C-m" 'comint-send-and-indent)) t))
5176 ;;;--------------------------------------------------------------------------
5177 ;;; Other languages.
5181 (defun mdw-setup-smalltalk ()
5182 (and mdw-auto-indent
5183 (local-set-key "\C-m" 'smalltalk-newline-and-indent))
5184 (make-local-variable 'mdw-auto-indent)
5185 (setq mdw-auto-indent nil)
5186 (local-set-key "\C-i" 'smalltalk-reindent))
5188 (defun mdw-fontify-smalltalk ()
5189 (make-local-variable 'font-lock-keywords)
5190 (setq font-lock-keywords
5192 (list "\\<[A-Z][a-zA-Z0-9]*\\>"
5193 '(0 font-lock-keyword-face))
5194 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
5195 "[0-9][0-9_]*\\(\\.[0-9_]*\\)?"
5196 "\\([eE][-+]?[0-9_]+\\)?")
5197 '(0 mdw-number-face))
5198 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
5199 '(0 mdw-punct-face)))))
5202 (add-hook 'smalltalk-mode 'mdw-misc-mode-config t)
5203 (add-hook 'smalltalk-mode 'mdw-fontify-smalltalk t))
5207 (defun mdw-setup-m4 ()
5209 ;; Inexplicably, Emacs doesn't match braces in m4 mode. This is very
5210 ;; annoying: fix it.
5211 (modify-syntax-entry ?{ "(")
5212 (modify-syntax-entry ?} ")")
5215 (mdw-standard-fill-prefix "\\([ \t]*\\(?:#+\\|\\<dnl\\>\\)[ \t]*\\)"))
5217 (dolist (hook '(m4-mode-hook autoconf-mode-hook autotest-mode-hook))
5218 (add-hook hook #'mdw-misc-mode-config t)
5219 (add-hook hook #'mdw-setup-m4 t))
5224 (add-hook 'makefile-mode-hook 'mdw-misc-mode-config t))
5226 ;;;--------------------------------------------------------------------------
5229 (defun mdw-text-mode ()
5230 (setq fill-column 72)
5232 (mdw-standard-fill-prefix
5233 "\\([ \t]*\\([>#|:] ?\\)*[ \t]*\\)" 3)
5236 (eval-after-load "flyspell"
5237 '(define-key flyspell-mode-map "\C-\M-i" nil))
5240 (add-hook 'text-mode-hook 'mdw-text-mode t))
5242 ;;;--------------------------------------------------------------------------
5243 ;;; Outline and hide/show modes.
5245 (defun mdw-outline-collapse-all ()
5246 "Completely collapse everything in the entire buffer."
5249 (goto-char (point-min))
5250 (while (< (point) (point-max))
5254 (setq hs-hide-comments-when-hiding-all nil)
5256 (defadvice hs-hide-all (after hide-first-comment activate)
5257 (save-excursion (hs-hide-initial-comment-block)))
5259 ;;;--------------------------------------------------------------------------
5262 (defun mdw-sh-mode-setup ()
5263 (local-set-key [?\C-a] 'comint-bol)
5264 (add-hook 'comint-output-filter-functions
5265 'comint-watch-for-password-prompt))
5267 (defun mdw-term-mode-setup ()
5268 (setq term-prompt-regexp shell-prompt-pattern)
5269 (make-local-variable 'mouse-yank-at-point)
5270 (make-local-variable 'transient-mark-mode)
5271 (setq mouse-yank-at-point t)
5275 (defun comint-send-and-indent ()
5278 (and mdw-auto-indent
5279 (indent-for-tab-command)))
5281 (defadvice comint-line-beginning-position
5282 (around mdw-calculate-it-properly () activate compile)
5283 "Calculate the actual line start for multi-line input."
5284 (if (or comint-use-prompt-regexp
5285 (eq (field-at-pos (point)) 'output))
5287 (setq ad-return-value
5288 (constrain-to-field (line-beginning-position) (point)))))
5290 (defun term-send-meta-right () (interactive) (term-send-raw-string "\e\e[C"))
5291 (defun term-send-meta-left () (interactive) (term-send-raw-string "\e\e[D"))
5292 (defun term-send-ctrl-uscore () (interactive) (term-send-raw-string "\C-_"))
5293 (defun term-send-meta-meta-something ()
5295 (term-send-raw-string "\e\e")
5297 (eval-after-load 'term
5299 (define-key term-raw-map [?\e ?\e] nil)
5300 (define-key term-raw-map [?\e ?\e t] 'term-send-meta-meta-something)
5301 (define-key term-raw-map [?\C-/] 'term-send-ctrl-uscore)
5302 (define-key term-raw-map [M-right] 'term-send-meta-right)
5303 (define-key term-raw-map [?\e ?\M-O ?C] 'term-send-meta-right)
5304 (define-key term-raw-map [M-left] 'term-send-meta-left)
5305 (define-key term-raw-map [?\e ?\M-O ?D] 'term-send-meta-left)))
5307 (defadvice term-exec (before program-args-list compile activate)
5308 "If the PROGRAM argument is a list, interpret it as (PROGRAM . SWITCHES).
5309 This allows you to pass a list of arguments through `ansi-term'."
5310 (let ((program (ad-get-arg 2)))
5313 (ad-set-arg 2 (car program))
5314 (ad-set-arg 4 (cdr program))))))
5316 (defadvice term-exec-1 (around hack-environment compile activate)
5317 "Hack the environment inherited by inferiors in the terminal."
5318 (let ((process-environment (copy-tree process-environment)))
5319 (setenv "LD_PRELOAD" nil)
5322 (defadvice shell (around hack-environment compile activate)
5323 "Hack the environment inherited by inferiors in the shell."
5324 (let ((process-environment (copy-tree process-environment)))
5325 (setenv "LD_PRELOAD" nil)
5329 "Open a terminal containing an ssh session to the HOST."
5330 (interactive "sHost: ")
5331 (ansi-term (list "ssh" host) (format "ssh@%s" host)))
5333 (defcustom git-grep-command
5334 "env GIT_PAGER=cat git grep --no-color -nH -e "
5335 "The default command for \\[git-grep]."
5338 (defvar git-grep-history nil)
5340 (defun git-grep (command-args)
5341 "Run `git grep' with user-specified args and collect output in a buffer."
5343 (list (read-shell-command "Run git grep (like this): "
5344 git-grep-command 'git-grep-history)))
5345 (let ((grep-use-null-device nil))
5346 (grep command-args)))
5348 ;;;--------------------------------------------------------------------------
5349 ;;; Magit configuration.
5351 (setq magit-diff-refine-hunk 't
5352 magit-view-git-manual-method 'man
5353 magit-log-margin '(nil age magit-log-margin-width t 18)
5354 magit-wip-after-save-local-mode-lighter ""
5355 magit-wip-after-apply-mode-lighter ""
5356 magit-wip-before-change-mode-lighter "")
5357 (eval-after-load "magit"
5358 '(progn (global-magit-file-mode 1)
5359 (magit-wip-after-save-mode 1)
5360 (magit-wip-after-apply-mode 1)
5361 (magit-wip-before-change-mode 1)
5362 (add-to-list 'magit-no-confirm 'safe-with-wip)
5363 (add-to-list 'magit-no-confirm 'trash)
5364 (push '(:eval (if (or magit-wip-after-save-local-mode
5365 magit-wip-after-apply-mode
5366 magit-wip-before-change-mode)
5367 (format " wip:%s%s%s"
5368 (if magit-wip-after-apply-mode "A" "")
5369 (if magit-wip-before-change-mode "C" "")
5370 (if magit-wip-after-save-local-mode "S" ""))))
5372 (dolist (popup '(magit-diff-popup
5373 magit-diff-refresh-popup
5374 magit-diff-mode-refresh-popup
5375 magit-revision-mode-refresh-popup))
5376 (magit-define-popup-switch popup ?R "Reverse diff" "-R"))
5377 (magit-define-popup-switch 'magit-rebase-popup ?r
5378 "Rebase merges" "--rebase-merges")))
5380 (defadvice magit-wip-commit-buffer-file
5381 (around mdw-just-this-buffer activate compile)
5382 (let ((magit-save-repository-buffers nil)) ad-do-it))
5384 (defadvice magit-discard
5385 (around mdw-delete-if-prefix-argument activate compile)
5386 (let ((magit-delete-by-moving-to-trash
5387 (and (null current-prefix-arg)
5388 magit-delete-by-moving-to-trash)))
5391 (setq magit-repolist-columns
5392 '(("Name" 16 magit-repolist-column-ident nil)
5393 ("Version" 18 magit-repolist-column-version nil)
5394 ("St" 2 magit-repolist-column-dirty nil)
5395 ("L<U" 3 mdw-repolist-column-unpulled-from-upstream nil)
5396 ("L>U" 3 mdw-repolist-column-unpushed-to-upstream nil)
5397 ("Path" 32 magit-repolist-column-path nil)))
5399 (setq magit-repository-directories '(("~/etc/profile" . 0)
5402 (defadvice magit-list-repos (around mdw-dirname () activate compile)
5403 "Make sure the returned names are directory names.
5404 Otherwise child processes get started in the wrong directory and
5406 (setq ad-return-value (mapcar #'file-name-as-directory ad-do-it)))
5408 (defun mdw-repolist-column-unpulled-from-upstream (_id)
5409 "Insert number of upstream commits not in the current branch."
5410 (let ((upstream (magit-get-upstream-branch (magit-get-current-branch) t)))
5412 (let ((n (cadr (magit-rev-diff-count "HEAD" upstream))))
5413 (propertize (number-to-string n) 'face
5414 (if (> n 0) 'bold 'shadow))))))
5416 (defun mdw-repolist-column-unpushed-to-upstream (_id)
5417 "Insert number of commits in the current branch but not its upstream."
5418 (let ((upstream (magit-get-upstream-branch (magit-get-current-branch) t)))
5420 (let ((n (car (magit-rev-diff-count "HEAD" upstream))))
5421 (propertize (number-to-string n) 'face
5422 (if (> n 0) 'bold 'shadow))))))
5424 (defun mdw-try-smerge ()
5426 (goto-char (point-min))
5427 (when (re-search-forward "^<<<<<<< " nil t)
5429 (add-hook 'find-file-hook 'mdw-try-smerge t)
5431 (defcustom mdw-magit-new-window-modes
5438 "Magit modes which should cause a new window to be used."
5439 :type '(repeat symbol))
5441 (defun mdw-display-magit-buffer (buffer)
5442 "Like `magit-display-buffer-traditional'.
5443 But uses `mdw-magit-new-window-modes' for its list of modes
5444 rather than baking the list into the function."
5445 (display-buffer buffer
5446 (let ((mode (with-current-buffer buffer major-mode)))
5447 (if (and (not mdw-designated-window)
5448 (derived-mode-p 'magit-mode)
5449 (mdw-submode-p mode 'magit-mode)
5450 (not (memq mode mdw-magit-new-window-modes)))
5451 '(display-buffer-same-window . nil)
5453 (setq magit-display-buffer-function 'mdw-display-magit-buffer)
5455 (defun mdw-display-magit-file-buffer (buffer)
5456 "Show a file buffer from a diff."
5457 (select-window (display-buffer buffer)))
5458 (setq magit-display-file-buffer-function 'mdw-display-magit-file-buffer)
5460 ;;;--------------------------------------------------------------------------
5461 ;;; GUD, and especially GDB.
5463 ;; Inhibit window dedication. I mean, seriously, wtf?
5464 (defadvice gdb-display-buffer (after mdw-undedicated (buf) compile activate)
5465 "Don't make windows dedicated. Seriously."
5466 (set-window-dedicated-p ad-return-value nil))
5467 (defadvice gdb-set-window-buffer
5468 (after mdw-undedicated (name &optional ignore-dedicated window)
5470 "Don't make windows dedicated. Seriously."
5471 (set-window-dedicated-p (or window (selected-window)) nil))
5473 ;;;--------------------------------------------------------------------------
5476 (setq sql-postgres-options '("-n" "-P" "pager=off")
5477 sql-postgres-login-params
5478 '((user :default "mdw")
5479 (database :default "mdw")
5480 (server :default "db.distorted.org.uk")))
5482 ;;;--------------------------------------------------------------------------
5485 ;; Turn off `noip' when running `man': it interferes with `man-db''s own
5486 ;; seccomp(2)-based sandboxing, which is (in this case, at least) strictly
5488 (defadvice Man-getpage-in-background
5489 (around mdw-inhibit-noip (topic) compile activate)
5490 "Inhibit the `noip' preload hack when invoking `man'."
5491 (let* ((old-preload (getenv "LD_PRELOAD"))
5492 (preloads (and old-preload
5493 (save-match-data (split-string old-preload ":"))))
5498 (let ((item (pop preloads)))
5499 (if (string-match "\\(/\\|^\\)noip\.so\\(:\\|$\\)" item)
5501 (push item filtered)))))
5505 (setenv "LD_PRELOAD"
5507 (with-output-to-string
5508 (setq filtered (nreverse filtered))
5511 (if first (setq first nil)
5513 (write-string (pop filtered)))))))
5515 (setenv "LD_PRELOAD" old-preload))
5518 ;;;--------------------------------------------------------------------------
5519 ;;; MPC configuration.
5521 (eval-when-compile (trap (require 'mpc)))
5523 (setq mpc-browser-tags '(Artist|Composer|Performer Album|Playlist))
5525 (defun mdw-mpc-now-playing ()
5529 (set-buffer (mpc-proc-cmd (mpc-proc-cmd-list '("status" "currentsong"))))
5530 (mpc--status-callback))
5531 (let ((state (cdr (assq 'state mpc-status))))
5532 (cond ((member state '("stop"))
5533 (message "mpd stopped."))
5534 ((member state '("play" "pause"))
5535 (let* ((artist (cdr (assq 'Artist mpc-status)))
5536 (album (cdr (assq 'Album mpc-status)))
5537 (title (cdr (assq 'Title mpc-status)))
5538 (file (cdr (assq 'file mpc-status)))
5539 (duration-string (cdr (assq 'Time mpc-status)))
5540 (time-string (cdr (assq 'time mpc-status)))
5541 (time (and time-string
5543 (if (string-match ":" time-string)
5544 (substring time-string
5545 0 (match-beginning 0))
5547 (duration (and duration-string
5548 (string-to-number duration-string)))
5549 (pos (and time duration
5550 (format " [%d:%02d/%d:%02d]"
5551 (/ time 60) (mod time 60)
5552 (/ duration 60) (mod duration 60))))
5553 (fmt (cond ((and artist title)
5554 (format "`%s' by %s%s" title artist
5555 (if album (format ", from `%s'" album)
5558 (format "`%s' (no tags)" file))
5560 "(no idea what's playing!)"))))
5561 (if (string= state "play")
5562 (message "mpd playing %s%s" fmt (or pos ""))
5563 (message "mpd paused in %s%s" fmt (or pos "")))))
5565 (message "mpd in unknown state `%s'" state)))))
5567 (defmacro mdw-define-mpc-wrapper (func bvl interactive &rest body)
5569 (interactive ,@interactive)
5572 (mdw-mpc-now-playing)))
5574 (mdw-define-mpc-wrapper mdw-mpc-play-or-pause () nil
5575 (if (member (cdr (assq 'state (mpc-cmd-status))) '("play"))
5579 (mdw-define-mpc-wrapper mdw-mpc-next () nil (mpc-next))
5580 (mdw-define-mpc-wrapper mdw-mpc-prev () nil (mpc-prev))
5581 (mdw-define-mpc-wrapper mdw-mpc-stop () nil (mpc-stop))
5583 (defun mdw-mpc-louder (step)
5584 (interactive (list (if current-prefix-arg
5585 (prefix-numeric-value current-prefix-arg)
5587 (mpc-proc-cmd (format "volume %+d" step)))
5589 (defun mdw-mpc-quieter (step)
5590 (interactive (list (if current-prefix-arg
5591 (prefix-numeric-value current-prefix-arg)
5593 (mpc-proc-cmd (format "volume %+d" (- step))))
5595 (defun mdw-mpc-hack-lines (arg interactivep func)
5596 (if (and interactivep (use-region-p))
5597 (let ((from (region-beginning)) (to (region-end)))
5602 (while (< (point) to)
5605 (let ((n (prefix-numeric-value arg)))
5622 (defun mdw-mpc-select-one ()
5623 (when (and (get-char-property (point) 'mpc-file)
5624 (not (get-char-property (point) 'mpc-select)))
5625 (mpc-select-toggle)))
5627 (defun mdw-mpc-unselect-one ()
5628 (when (get-char-property (point) 'mpc-select)
5629 (mpc-select-toggle)))
5631 (defun mdw-mpc-select (&optional arg interactivep)
5632 (interactive (list current-prefix-arg t))
5633 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
5635 (defun mdw-mpc-unselect (&optional arg interactivep)
5636 (interactive (list current-prefix-arg t))
5637 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-unselect-one))
5639 (defun mdw-mpc-unselect-backwards (arg)
5641 (mdw-mpc-hack-lines (- arg) t 'mdw-mpc-unselect-one))
5643 (defun mdw-mpc-unselect-all ()
5645 (setq mpc-select nil)
5646 (mpc-selection-refresh))
5648 (defun mdw-mpc-next-line (arg)
5653 (defun mdw-mpc-previous-line (arg)
5656 (forward-line (- arg)))
5658 (defun mdw-mpc-playlist-add (&optional arg interactivep)
5659 (interactive (list current-prefix-arg t))
5660 (let ((mpc-select mpc-select))
5661 (when (or arg (and interactivep (use-region-p)))
5662 (setq mpc-select nil)
5663 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
5664 (setq mpc-select (reverse mpc-select))
5665 (mpc-playlist-add)))
5667 (defun mdw-mpc-playlist-delete (&optional arg interactivep)
5668 (interactive (list current-prefix-arg t))
5669 (setq mpc-select (nreverse mpc-select))
5671 (when (or arg (and interactivep (use-region-p)))
5672 (setq mpc-select nil)
5673 (mpc-selection-refresh)
5674 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
5675 (mpc-playlist-delete)))
5677 (defun mdw-mpc-hack-tagbrowsers ()
5678 (setq-local mode-line-format
5680 mode-line-frame-identification
5681 mode-line-buffer-identification)))
5682 (add-hook 'mpc-tagbrowser-mode-hook 'mdw-mpc-hack-tagbrowsers)
5684 (defun mdw-mpc-hack-songs ()
5685 (setq-local header-line-format
5686 ;; '("MPC " mpc-volume " " mpc-current-song)
5687 (list (propertize " " 'display '(space :align-to 0))
5688 ;; 'mpc-songs-format-description
5690 (let ((deactivate-mark) (hscroll (window-hscroll)))
5692 (mpc-format mpc-songs-format 'self hscroll)
5693 ;; That would be simpler than the hscroll handling in
5694 ;; mpc-format, but currently move-to-column does not
5695 ;; recognize :space display properties.
5696 ;; (move-to-column hscroll)
5697 ;; (delete-region (point-min) (point))
5698 (buffer-string)))))))
5699 (add-hook 'mpc-songs-mode-hook 'mdw-mpc-hack-songs)
5701 (eval-after-load "mpc"
5703 (define-key mpc-mode-map "m" 'mdw-mpc-select)
5704 (define-key mpc-mode-map "u" 'mdw-mpc-unselect)
5705 (define-key mpc-mode-map "\177" 'mdw-mpc-unselect-backwards)
5706 (define-key mpc-mode-map "\e\177" 'mdw-mpc-unselect-all)
5707 (define-key mpc-mode-map "n" 'mdw-mpc-next-line)
5708 (define-key mpc-mode-map "p" 'mdw-mpc-previous-line)
5709 (define-key mpc-mode-map "/" 'mpc-songs-search)
5710 (setq mpc-songs-mode-map (make-sparse-keymap))
5711 (set-keymap-parent mpc-songs-mode-map mpc-mode-map)
5712 (define-key mpc-songs-mode-map "l" 'mpc-playlist)
5713 (define-key mpc-songs-mode-map "+" 'mdw-mpc-playlist-add)
5714 (define-key mpc-songs-mode-map "-" 'mdw-mpc-playlist-delete)
5715 (define-key mpc-songs-mode-map "\r" 'mpc-songs-jump-to)))
5717 ;;;--------------------------------------------------------------------------
5718 ;;; Inferior Emacs Lisp.
5720 (setq comint-prompt-read-only t)
5722 (eval-after-load "comint"
5724 (define-key comint-mode-map "\C-w" 'comint-kill-region)
5725 (define-key comint-mode-map [C-S-backspace] 'comint-kill-whole-line)))
5727 (eval-after-load "ielm"
5729 (define-key ielm-map "\C-w" 'comint-kill-region)
5730 (define-key ielm-map [C-S-backspace] 'comint-kill-whole-line)))
5732 ;;;----- That's all, folks --------------------------------------------------
5734 (provide 'dot-emacs)