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")))
660 (setq org-export-docbook-xslt-proc-command "xsltproc --output %o %s %i"
661 org-export-docbook-xsl-fo-proc-command "fop %i.safe %o"
662 org-export-docbook-xslt-stylesheet
663 "/usr/share/xml/docbook/stylesheet/docbook-xsl/fo/docbook.xsl")
667 (setq glasses-separator "-"
668 glasses-separate-parentheses-p nil
669 glasses-uncapitalize-p t)
671 ;; Some hacks to do with window placement.
673 (defvar mdw-designated-window nil
674 "The window chosen by `mdw-designate-window', or nil.")
676 (defun mdw-designated-window-display-buffer-function (buffer not-this-window)
677 "Display buffer function to use the designated window."
678 (unless mdw-designated-window (error "No designated window!"))
679 (prog1 mdw-designated-window
680 (with-selected-window mdw-designated-window (switch-to-buffer buffer))
681 (setq mdw-designated-window nil
682 display-buffer-function nil)))
684 (defun mdw-display-buffer-in-designated-window (buffer alist)
685 "Display function to use the designated window."
686 (prog1 mdw-designated-window
687 (when mdw-designated-window
688 (with-selected-window mdw-designated-window
689 (switch-to-buffer buffer nil t)))
690 (setq mdw-designated-window nil)))
692 (defun mdw-designate-window (cancel)
693 "Use the selected window for the next pop-up buffer.
694 With a prefix argument, clear the designated window."
696 (let ((window (selected-window)))
698 (setq mdw-designated-window nil)
699 (unless (mdw-emacs-version-p 24)
700 (setq display-buffer-function nil))
701 (message "Window designation cleared."))
702 ((window-dedicated-p window)
703 (error "Window is dedicated to its buffer."))
705 (setq mdw-designated-window window)
706 (unless (mdw-emacs-version-p 24)
707 (setq display-buffer-function
708 #'mdw-designated-window-display-buffer-function))
709 (message "Window designated.")))))
711 (when (mdw-emacs-version-p 24)
712 (setq display-buffer-base-action
713 (let* ((action display-buffer-base-action)
715 (alist (cdr action)))
716 (cons (cons 'mdw-display-buffer-in-designated-window funcs)
719 (defun mdw-clobber-other-windows-showing-buffer (buffer-or-name)
720 "Arrange that no windows on other frames are showing BUFFER-OR-NAME."
721 (interactive "bBuffer: ")
722 (let ((home-frame (selected-frame))
723 (buffer (get-buffer buffer-or-name))
724 (safe-buffer (get-buffer "*scratch*")))
725 (dolist (frame (frame-list))
726 (unless (eq frame home-frame)
727 (dolist (window (window-list frame))
728 (when (eq (window-buffer window) buffer)
729 (set-window-buffer window safe-buffer)))))))
731 (defvar mdw-inhibit-walk-windows nil
732 "If non-nil, then `walk-windows' does nothing.
733 This is used by advice on `switch-to-buffer-other-frame' to inhibit finding
734 buffers in random frames.")
736 (setq display-buffer--other-frame-action
737 '((display-buffer-reuse-window display-buffer-pop-up-frame)
738 (reusable-frames . nil)
739 (inhibit-same-window . t)))
741 (defadvice walk-windows (around mdw-inhibit activate)
742 "If `mdw-inhibit-walk-windows' is non-nil, then do nothing."
743 (and (not mdw-inhibit-walk-windows)
746 (defadvice switch-to-buffer-other-frame
747 (around mdw-always-new-frame activate)
748 "Always make a new frame.
749 Even if an existing window in some random frame looks tempting."
750 (let ((mdw-inhibit-walk-windows t)) ad-do-it))
752 (defadvice display-buffer (before mdw-inhibit-other-frames activate)
753 "Don't try to do anything fancy with other frames.
754 Pretend they don't exist. They might be on other display devices."
757 (setq even-window-sizes nil
758 even-window-heights nil)
760 (setq display-buffer-reuse-frames nil)
762 (defun mdw-last-window-in-frame-p (window)
763 "Return whether WINDOW is the last in its frame."
766 (let ((next (window-next-sibling window)))
767 (while (and next (window-minibuffer-p next))
768 (setq next (window-next-sibling next)))
769 (if next (throw 'done nil)))
770 (setq window (window-parent window)))
773 (defun mdw-display-buffer-in-tolerable-window (buffer alist)
774 "Try finding a tolerable window in which to display BUFFER.
775 Begone, foul DWIMmerlaik!
777 This is all totally subject to arbitrary change in the future, but the
778 emphasis is on predictability rather than crazy DWIMmery."
779 (let* ((selected (selected-window)) chosen
780 (full-height-p (window-full-height-p selected))
781 (full-width-p (window-full-width-p selected)))
784 ((and full-height-p full-width-p)
785 ;; We're basically the only window in the frame. If we want to get
786 ;; anywhere, we'll have to split the window.
788 (let ((width (window-width selected))
789 (preferred-width (mdw-preferred-column-width)))
790 (if (and (>= width (mdw-frame-width-for-columns 2 preferred-width))
791 (mdw-frame-width-quantized-p width preferred-width))
792 (setq chosen (split-window-right preferred-width))
793 (setq chosen (split-window-below)))
794 (display-buffer-record-window 'window chosen buffer)))
796 ((mdw-last-window-in-frame-p selected)
797 ;; This is the last window in the frame. I don't think I want to
798 ;; clobber the first window, so rebound and clobber the previous one
799 ;; instead. (This obviously has the same effect if there are only two
800 ;; windows, but seems more useful if there are three.)
802 (setq chosen (previous-window selected 'never nil))
803 (display-buffer-record-window 'reuse chosen buffer))
806 ;; There's another window in front of us. Let's use that one.
807 (setq chosen (next-window selected 'never nil)))
808 (display-buffer-record-window 'reuse chosen buffer))
810 (if (eq chosen selected)
811 (error "Failed to select a different window!"))
814 (with-selected-window chosen (switch-to-buffer buffer)))
817 ;; Hack the display actions so that they do something sensible.
818 (setq display-buffer-fallback-action
819 '((display-buffer--maybe-same-window
820 display-buffer-reuse-window
821 display-buffer-pop-up-window
822 mdw-display-buffer-in-tolerable-window)))
824 ;; Rename buffers along with files.
826 (defvar mdw-inhibit-rename-buffer nil
827 "If non-nil, `rename-file' won't rename the buffer visiting the file.")
829 (defmacro mdw-advise-to-inhibit-rename-buffer (function)
830 "Advise FUNCTION to set `mdw-inhibit-rename-buffer' while it runs.
832 This will prevent `rename-file' from renaming the buffer."
833 `(defadvice ,function (around mdw-inhibit-rename-buffer compile activate)
834 "Don't rename the buffer when renaming the underlying file."
835 (let ((mdw-inhibit-rename-buffer t))
837 (mdw-advise-to-inhibit-rename-buffer recode-file-name)
838 (mdw-advise-to-inhibit-rename-buffer set-visited-file-name)
839 (mdw-advise-to-inhibit-rename-buffer backup-buffer)
841 (defadvice rename-file (after mdw-rename-buffers (from to &optional forcep)
843 "If a buffer is visiting the file, rename it to match the new name.
845 Don't do this if `mdw-inhibit-rename-buffer' is non-nil."
846 (unless mdw-inhibit-rename-buffer
847 (let ((buffer (get-file-buffer from)))
849 (let ((to (if (not (string= (file-name-nondirectory to) "")) to
850 (concat to (file-name-nondirectory from)))))
851 (with-current-buffer buffer
852 (set-visited-file-name to nil t)))))))
854 ;;;--------------------------------------------------------------------------
855 ;;; Improved compilation machinery.
857 ;; Uprated version of M-x compile.
859 (setq compile-command
860 (let ((ncpu (with-temp-buffer
861 (insert-file-contents "/proc/cpuinfo")
863 (count-matches "^processor\\s-*:"))))
864 (format "nice make -j%d -k" (* 2 ncpu))))
866 (defun mdw-compilation-buffer-name (mode)
867 (concat "*" (downcase mode) ": "
868 (abbreviate-file-name default-directory) "*"))
869 (setq compilation-buffer-name-function 'mdw-compilation-buffer-name)
871 (eval-after-load "compile"
873 (define-key compilation-shell-minor-mode-map "\C-c\M-g" 'recompile)))
875 (defadvice compile (around hack-environment compile activate)
876 "Hack the environment inherited by inferiors in the compilation."
877 (let ((process-environment (copy-tree process-environment)))
878 (setenv "LD_PRELOAD" nil)
881 (defun mdw-compile (command &optional directory comint)
882 "Initiate a compilation COMMAND, maybe in a different DIRECTORY.
883 The DIRECTORY may be nil to not change. If COMINT is t, then
884 start an interactive compilation.
886 Interactively, prompt for the command if the variable
887 `compilation-read-command' is non-nil, or if requested through
888 the prefix argument. Prompt for the directory, and run
889 interactively, if requested through the prefix.
891 Use a prefix of 4, 6, 12, or 14, or type C-u between one and three times, to
892 force prompting for a directory.
894 Use a prefix of 2, 6, 10, or 14, or type C-u three times, to force
895 prompting for the command.
897 Use a prefix of 8, 10, 12, or 14, or type C-u twice or three times,
898 to force interactive compilation."
900 (let* ((prefix (prefix-numeric-value current-prefix-arg))
901 (command (eval compile-command))
902 (dir (and (plusp (logand prefix #x54))
903 (read-directory-name "Compile in directory: "))))
904 (list (if (or compilation-read-command
905 (plusp (logand prefix #x42)))
906 (compilation-read-command command)
909 (plusp (logand prefix #x58)))))
910 (let ((default-directory (or directory default-directory)))
911 (compile command comint)))
915 (defun mdw-find-build-dir (build-file)
917 (let* ((src-dir (file-name-as-directory (expand-file-name ".")))
920 (when (file-exists-p (concat dir build-file))
922 (let ((sub (expand-file-name (file-relative-name src-dir dir)
923 (concat dir "build/"))))
926 (when (file-exists-p (concat sub build-file))
928 (when (string= sub dir) (throw 'give-up nil))
929 (setq sub (file-name-directory (directory-file-name sub))))))
931 (setq dir (file-name-directory
932 (directory-file-name dir))))
933 (throw 'found nil))))))
935 (defun mdw-flymake-make-init ()
936 (let ((build-dir (mdw-find-build-dir "Makefile")))
938 (let ((tmp-src (flymake-init-create-temp-buffer-copy
939 #'flymake-create-temp-inplace)))
940 (flymake-get-syntax-check-program-args
941 tmp-src build-dir t t
942 #'flymake-get-make-cmdline)))))
944 (setq flymake-allowed-file-name-masks
945 '(("\\.\\(?:[cC]\\|cc\\|cpp\\|cxx\\|c\\+\\+\\)\\'"
946 mdw-flymake-make-init)
947 ("\\.\\(?:[hH]\\|hh\\|hpp\\|hxx\\|h\\+\\+\\)\\'"
948 mdw-flymake-master-make-init)
949 ("\\.p[lm]" flymake-perl-init)))
951 (setq flymake-mode-map
952 (let ((map (if (boundp 'flymake-mode-map)
954 (make-sparse-keymap))))
955 (define-key map [?\C-c ?\C-f ?\C-p] 'flymake-goto-prev-error)
956 (define-key map [?\C-c ?\C-f ?\C-n] 'flymake-goto-next-error)
957 (define-key map [?\C-c ?\C-f ?\C-c] 'flymake-compile)
958 (define-key map [?\C-c ?\C-f ?\C-k] 'flymake-stop-all-syntax-checks)
959 (define-key map [?\C-c ?\C-f ?\C-e] 'flymake-popup-current-error-menu)
962 ;;;--------------------------------------------------------------------------
963 ;;; Mail and news hacking.
965 (define-derived-mode mdwmail-mode mail-mode "[mdw] mail"
966 "Major mode for editing news and mail messages from external programs.
967 Not much right now. Just support for doing MailCrypt stuff."
970 (run-hooks 'mail-setup-hook))
972 (define-key mdwmail-mode-map [?\C-c ?\C-c] 'disabled-operation)
974 (add-hook 'mdwail-mode-hook
976 (set-buffer-file-coding-system 'utf-8)
977 (make-local-variable 'paragraph-separate)
978 (make-local-variable 'paragraph-start)
979 (setq paragraph-start
980 (concat "[ \t]*[-_][-_][-_]+$\\|^-- \\|-----\\|"
982 (setq paragraph-separate
983 (concat "[ \t]*[-_][-_][-_]+$\\|^-- \\|-----\\|"
984 paragraph-separate))))
986 ;; How to encrypt in mdwmail.
988 (defun mdwmail-mc-encrypt (&optional recip scm start end from sign)
990 (setq start (save-excursion
991 (goto-char (point-min))
992 (or (search-forward "\n\n" nil t) (point-min)))))
994 (setq end (point-max)))
995 (mc-encrypt-generic recip scm start end from sign))
997 ;; How to sign in mdwmail.
999 (defun mdwmail-mc-sign (key scm start end uclr)
1001 (setq start (save-excursion
1002 (goto-char (point-min))
1003 (or (search-forward "\n\n" nil t) (point-min)))))
1005 (setq end (point-max)))
1006 (mc-sign-generic key scm start end uclr))
1008 ;; Some signature mangling.
1010 (defun mdwmail-mangle-signature ()
1012 (goto-char (point-min))
1013 (perform-replace "\n-- \n" "\n-- " nil nil nil)))
1014 (add-hook 'mail-setup-hook 'mdwmail-mangle-signature)
1015 (add-hook 'message-setup-hook 'mdwmail-mangle-signature)
1017 ;; Insert my login name into message-ids, so I can score replies.
1019 (defadvice message-unique-id (after mdw-user-name last activate compile)
1020 "Ensure that the user's name appears at the end of the message-id string,
1021 so that it can be used for convenient filtering."
1022 (setq ad-return-value (concat ad-return-value "." (user-login-name))))
1024 ;; Tell my movemail hack where movemail is.
1026 ;; This is needed to shup up warnings about LD_PRELOAD.
1028 (let ((path exec-path))
1030 (let ((try (expand-file-name "movemail" (car path))))
1031 (if (file-executable-p try)
1032 (setenv "REAL_MOVEMAIL" try))
1033 (setq path (cdr path)))))
1035 ;; AUTHINFO GENERIC kludge.
1037 (defcustom nntp-authinfo-generic nil
1038 "Set to the `NNTPAUTH' string to pass on to `authinfo-kludge'.
1040 Use this to arrange for per-server settings."
1041 :type '(choice (const :tag "Use `NNTPAUTH' environment variable" nil)
1045 (defun nntp-open-authinfo-kludge (buffer)
1046 "Open a connection to SERVER using `authinfo-kludge'."
1047 (let ((proc (start-process "nntpd" buffer
1048 "env" (concat "NNTPAUTH="
1049 (or nntp-authinfo-generic
1051 (error "NNTPAUTH unset")))
1052 "authinfo-kludge" nntp-address)))
1054 (nntp-wait-for-string "^\r*200")
1056 (delete-region (point-min) (point))
1059 (eval-after-load "erc"
1060 '(load "~/.ercrc.el"))
1062 ;; Heavy-duty Gnus patching.
1064 (defun mdw-nnimap-transform-headers ()
1065 (goto-char (point-min))
1066 (let (article lines size string)
1069 (while (not (looking-at "\\* [0-9]+ FETCH"))
1070 (delete-region (point) (progn (forward-line 1) (point)))
1073 (goto-char (match-end 0))
1074 ;; Unfold quoted {number} strings.
1075 (while (re-search-forward
1076 "[^]][ (]{\\([0-9]+\\)}\r?\n"
1078 ;; Start of the header section.
1079 (or (re-search-forward "] {[0-9]+}\r?\n" nil t)
1080 ;; Start of the next FETCH.
1081 (re-search-forward "\\* [0-9]+ FETCH" nil t)
1084 (setq size (string-to-number (match-string 1)))
1085 (delete-region (+ (match-beginning 0) 2) (point))
1086 (setq string (buffer-substring (point) (+ (point) size)))
1087 (delete-region (point) (+ (point) size))
1088 (insert (format "%S" (subst-char-in-string ?\n ?\s string)))
1089 ;; [mdw] missing from upstream
1093 (and (re-search-forward "UID \\([0-9]+\\)"
1099 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
1104 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
1105 (let ((structure (ignore-errors
1106 (read (current-buffer)))))
1107 (while (and (consp structure)
1108 (not (atom (car structure))))
1109 (setq structure (car structure)))
1110 (setq lines (if (and
1111 (stringp (car structure))
1112 (equal (upcase (nth 0 structure)) "MESSAGE")
1113 (equal (upcase (nth 1 structure)) "RFC822"))
1115 (nth 7 structure)))))
1116 (delete-region (line-beginning-position) (line-end-position))
1117 (insert (format "211 %s Article retrieved." article))
1120 (insert (format "Chars: %s\n" size)))
1122 (insert (format "Lines: %s\n" lines)))
1123 ;; Most servers have a blank line after the headers, but
1125 (unless (re-search-forward "^\r$\\|^)\r?$" nil t)
1126 (goto-char (point-max)))
1127 (delete-region (line-beginning-position) (line-end-position))
1129 (forward-line 1)))))
1131 (eval-after-load 'nnimap
1132 '(defalias 'nnimap-transform-headers
1133 (symbol-function 'mdw-nnimap-transform-headers)))
1135 (defadvice gnus-other-frame (around mdw-hack-frame-width compile activate)
1136 "Always arrange for mail/news frames to be 80 columns wide."
1137 (let ((default-frame-alist (cons `(width . ,(+ 80 mdw-frame-width-fudge))
1138 (delete* 'width default-frame-alist
1142 ;; Preferred programs.
1144 (setq mailcap-user-mime-data
1145 '(((type . "application/pdf") (viewer . "mupdf %s"))))
1147 ;;;--------------------------------------------------------------------------
1148 ;;; Utility functions.
1150 (or (fboundp 'line-number-at-pos)
1151 (defun line-number-at-pos (&optional pos)
1152 (let ((opoint (or pos (point))) start)
1155 (goto-char (point-min))
1158 (setq start (point))
1161 (1+ (count-lines 1 (point))))))))
1163 (defun mdw-uniquify-alist (&rest alists)
1164 "Return the concatenation of the ALISTS with duplicate elements removed.
1165 The first association with a given key prevails; others are
1166 ignored. The input lists are not modified, although they'll
1167 probably become garbage."
1169 (let ((start-list (cons nil nil)))
1170 (mdw-do-uniquify start-list
1175 (defun mdw-do-uniquify (done end l rest)
1176 "A helper function for mdw-uniquify-alist.
1177 The DONE argument is a list whose first element is `nil'. It
1178 contains the uniquified alist built so far. The leading `nil' is
1179 stripped off at the end of the operation; it's only there so that
1180 DONE always references a cons cell. END refers to the final cons
1181 cell in the DONE list; it is modified in place each time to avoid
1182 the overheads of `append'ing all the time. The L argument is the
1183 alist we're currently processing; the remaining alists are given
1186 ;; There are several different cases to deal with here.
1189 ;; Current list isn't empty. Add the first item to the DONE list if
1190 ;; there's not an item with the same KEY already there.
1191 (l (or (assoc (car (car l)) done)
1193 (setcdr end (cons (car l) nil))
1194 (setq end (cdr end))))
1195 (mdw-do-uniquify done end (cdr l) rest))
1197 ;; The list we were working on is empty. Shunt the next list into the
1198 ;; current list position and go round again.
1199 (rest (mdw-do-uniquify done end (car rest) (cdr rest)))
1201 ;; Everything's done. Remove the leading `nil' from the DONE list and
1202 ;; return it. Finished!
1206 "Insert the current date in a pleasing way."
1208 (insert (save-excursion
1209 (let ((buffer (get-buffer-create "*tmp*")))
1210 (unwind-protect (progn (set-buffer buffer)
1212 (shell-command "date +%Y-%m-%d" t)
1216 (kill-buffer buffer))))))
1218 (defun uuencode (file &optional name)
1219 "UUencodes a file, maybe calling it NAME, into the current buffer."
1220 (interactive "fInput file name: ")
1222 ;; If NAME isn't specified, then guess from the filename.
1226 (or (string-match "[^/]*$" file) 0))))
1227 (print (format "uuencode `%s' `%s'" file name))
1229 ;; Now actually do the thing.
1230 (call-process "uuencode" file t nil name))
1232 (defcustom np-file "~/.np"
1233 "Where the `now-playing' file is."
1237 (defun np (&optional arg)
1238 "Grabs a `now-playing' string."
1242 (goto-char (point-max))
1244 (insert-file-contents np-file)))))
1246 (defun mdw-version-< (ver-a ver-b)
1247 "Answer whether VER-A is strictly earlier than VER-B.
1248 VER-A and VER-B are version numbers, which are strings containing digit
1249 sequences separated by `.'."
1250 (let* ((la (mapcar (lambda (x) (car (read-from-string x)))
1251 (split-string ver-a "\\.")))
1252 (lb (mapcar (lambda (x) (car (read-from-string x)))
1253 (split-string ver-b "\\."))))
1256 (cond ((null la) (throw 'done lb))
1257 ((null lb) (throw 'done nil))
1258 ((< (car la) (car lb)) (throw 'done t))
1259 ((= (car la) (car lb)) (setq la (cdr la) lb (cdr lb)))
1260 (t (throw 'done nil)))))))
1262 (defun mdw-check-autorevert ()
1263 "Sets global-auto-revert-ignore-buffer appropriately for this buffer.
1264 This takes into consideration whether it's been found using
1265 tramp, which seems to get itself into a twist."
1266 (cond ((not (boundp 'global-auto-revert-ignore-buffer))
1268 ((and (buffer-file-name)
1269 (fboundp 'tramp-tramp-file-p)
1270 (tramp-tramp-file-p (buffer-file-name)))
1271 (unless global-auto-revert-ignore-buffer
1272 (setq global-auto-revert-ignore-buffer 'tramp)))
1273 ((eq global-auto-revert-ignore-buffer 'tramp)
1274 (setq global-auto-revert-ignore-buffer nil))))
1276 (defadvice find-file (after mdw-autorevert activate)
1277 (mdw-check-autorevert))
1278 (defadvice write-file (after mdw-autorevert activate)
1279 (mdw-check-autorevert))
1281 (defun mdw-auto-revert ()
1282 "Recheck all of the autorevertable buffers, and update VC modelines."
1284 (let ((auto-revert-check-vc-info t))
1285 (auto-revert-buffers)))
1287 ;;;--------------------------------------------------------------------------
1290 (defadvice dired-maybe-insert-subdir
1291 (around mdw-marked-insertion first activate)
1292 "The DIRNAME may be a list of directory names to insert.
1293 Interactively, if files are marked, then insert all of them.
1294 With a numeric prefix argument, select that many entries near
1295 point; with a non-numeric prefix argument, prompt for listing
1298 (list (dired-get-marked-files nil
1299 (and (integerp current-prefix-arg)
1302 (and current-prefix-arg
1303 (not (integerp current-prefix-arg))
1304 (read-string "Switches for listing: "
1305 (or dired-subdir-switches
1306 dired-actual-switches)))))
1307 (let ((dirs (ad-get-arg 0)))
1308 (dolist (dir (if (listp dirs) dirs (list dirs)))
1312 (defun mdw-dired-run (args &optional syncp)
1313 (interactive (let ((file (dired-get-filename t)))
1314 (list (read-string (format "Arguments for %s: " file))
1315 current-prefix-arg)))
1316 (funcall (if syncp 'shell-command 'async-shell-command)
1317 (concat (shell-quote-argument (dired-get-filename nil))
1320 (defadvice dired-do-flagged-delete
1321 (around mdw-delete-if-prefix-argument activate compile)
1322 (let ((delete-by-moving-to-trash (and (null current-prefix-arg)
1323 delete-by-moving-to-trash)))
1326 (eval-after-load "dired"
1327 '(define-key dired-mode-map "X" 'mdw-dired-run))
1329 ;;;--------------------------------------------------------------------------
1332 (defun mdw-w3m-browse-url (url &optional new-session-p)
1333 "Invoke w3m on the URL in its current window, or at least a different one.
1334 If NEW-SESSION-P, start a new session."
1335 (interactive "sURL: \nP")
1337 (let ((window (selected-window)))
1340 (select-window (or (and (not new-session-p)
1341 (get-buffer-window "*w3m*"))
1343 (if (one-window-p t) (split-window))
1345 (w3m-browse-url url new-session-p))
1346 (select-window window)))))
1348 (eval-after-load 'w3m
1349 '(define-key w3m-mode-map [?\e ?\r] 'w3m-view-this-url-new-session))
1351 (defcustom mdw-good-url-browsers
1352 '(browse-url-mozilla
1354 (w3m . mdw-w3m-browse-url)
1356 "List of good browsers for mdw-good-url-browsers.
1357 Each item is a browser function name, or a cons (CHECK . FUNC).
1358 A symbol FOO stands for (FOO . FOO)."
1359 :type '(repeat (choice function (cons function function))))
1361 (defun mdw-good-url-browser ()
1362 "Return a good URL browser.
1363 Trundle the list of such things, finding the first item for which
1364 CHECK is fboundp, and returning the correponding FUNC."
1365 (let ((bs mdw-good-url-browsers) b check func answer)
1366 (while (and bs (not answer))
1370 (setq check (car b) func (cdr b))
1371 (setq check b func b))
1373 (setq answer func)))
1376 (eval-after-load "w3m-search"
1380 '(("g" "Google" "http://www.google.co.uk/search?q=%s")
1381 ("gd" "Google Directory"
1382 "http://www.google.com/search?cat=gwd/Top&q=%s")
1383 ("gg" "Google Groups" "http://groups.google.com/groups?q=%s")
1384 ("ward" "Ward's wiki" "http://c2.com/cgi/wiki?%s")
1385 ("gi" "Images" "http://images.google.com/images?q=%s")
1387 "http://metalzone.distorted.org.uk/ftp/pub/mirrors/rfc/rfc%s.txt.gz")
1389 "http://en.wikipedia.org/wiki/Special:Search?go=Go&search=%s")
1390 ("imdb" "IMDb" "http://www.imdb.com/Find?%s")
1391 ("nc-wiki" "nCipher wiki"
1392 "http://wiki.ncipher.com/wiki/bin/view/Devel/?topic=%s")
1393 ("map" "Google maps" "http://maps.google.co.uk/maps?q=%s&hl=en")
1394 ("lp" "Launchpad bug by number"
1395 "https://bugs.launchpad.net/bugs/%s")
1396 ("lppkg" "Launchpad bugs by package"
1397 "https://bugs.launchpad.net/%s")
1399 "http://social.msdn.microsoft.com/Search/en-GB/?query=%s&ac=8")
1400 ("debbug" "Debian bug by number"
1401 "http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=%s")
1402 ("debbugpkg" "Debian bugs by package"
1403 "http://bugs.debian.org/cgi-bin/pkgreport.cgi?pkg=%s")
1404 ("ljlogin" "LJ login" "http://www.livejournal.com/login.bml")))
1405 (add-to-list 'w3m-search-engine-alist
1406 (list (cadr item) (caddr item) nil))
1407 (add-to-list 'w3m-uri-replace-alist
1408 (list (concat "\\`" (car item) ":")
1409 'w3m-search-uri-replace
1412 ;;;--------------------------------------------------------------------------
1413 ;;; Paragraph filling.
1415 ;; Useful variables.
1417 (defcustom mdw-fill-prefix nil
1418 "Used by `mdw-line-prefix' and `mdw-fill-paragraph'.
1419 If there's no fill prefix currently set (by the `fill-prefix'
1420 variable) and there's a match from one of the regexps here, it
1421 gets used to set the fill-prefix for the current operation.
1423 The variable is a list of items of the form `PATTERN . PREFIX'; if
1424 the PATTERN matches, the PREFIX is used to set the fill prefix.
1426 A PATTERN is one of the following.
1428 * STRING -- a regular expression, expected to match at point
1429 * (eval . FORM) -- a Lisp form which must evaluate non-nil
1430 * (if COND CONSEQ-PAT ALT-PAT) -- if COND evaluates non-nil, must match
1431 CONSEQ-PAT; otherwise must match ALT-PAT
1432 * (and PATTERN ...) -- must match all of the PATTERNs
1433 * (or PATTERN ...) -- must match at least one PATTERN
1434 * (not PATTERN) -- mustn't match (probably not useful)
1436 A PREFIX is a list of the following kinds of things:
1438 * STRING -- insert a literal string
1439 * (match . N) -- insert the thing matched by bracketed subexpression N
1440 * (pad . N) -- a string of whitespace the same width as subexpression N
1441 * (expr . FORM) -- the result of evaluating FORM
1443 Information about `bracketed subexpressions' comes from the match data,
1444 as modified during matching.")
1446 (make-variable-buffer-local 'mdw-fill-prefix)
1448 (defcustom mdw-hanging-indents
1450 "\\([*o+]\\|-[-#]?\\|[0-9]+\\.\\|\\[[0-9]+\\]\\|([a-zA-Z])\\)"
1453 "Standard regexp matching parts of a hanging indent.
1454 This is mainly useful in `auto-fill-mode'."
1457 ;; Utility functions.
1459 (defun mdw-maybe-tabify (s)
1460 "Tabify or untabify the string S, according to `indent-tabs-mode'."
1461 (let ((tabfun (if indent-tabs-mode #'tabify #'untabify)))
1465 (let ((start (point-min)) (end (point-max)))
1466 (funcall tabfun (point-min) (point-max))
1467 (setq s (buffer-substring (point-min) (1- (point-max)))))))))
1469 (defun mdw-fill-prefix-match-p (pat)
1470 "Return non-nil if PAT matches at the current position."
1471 (cond ((stringp pat) (looking-at pat))
1472 ((not (consp pat)) (error "Unknown pattern item `%S'" pat))
1473 ((eq (car pat) 'eval) (eval (cdr pat)))
1475 (if (or (null (cdr pat))
1479 (error "Invalid `if' pattern `%S'" pat))
1480 (mdw-fill-prefix-match-p (if (eval (cadr pat))
1483 ((eq (car pat) 'and)
1484 (let ((pats (cdr pat))
1487 (or (mdw-fill-prefix-match-p (car pats))
1489 (setq pats (cdr pats)))
1492 (let ((pats (cdr pat))
1495 (or (not (mdw-fill-prefix-match-p (car pats)))
1496 (progn (setq ok t) nil)))
1497 (setq pats (cdr pats)))
1499 ((eq (car pat) 'not)
1500 (if (or (null (cdr pat)) (cddr pat))
1501 (error "Invalid `not' pattern `%S'" pat))
1502 (not (mdw-fill-prefix-match-p (car pats))))
1503 (t (error "Unknown pattern form `%S'" pat))))
1505 (defun mdw-maybe-car (p)
1506 "If P is a pair, return (car P), otherwise just return P."
1507 (if (consp p) (car p) p))
1509 (defun mdw-padding (s)
1510 "Return a string the same width as S but made entirely from whitespace."
1511 (let* ((l (length s)) (i 0) (n (make-string l ? )))
1513 (if (= 9 (aref s i))
1518 (defun mdw-do-prefix-match (m)
1519 "Expand a dynamic prefix match element.
1520 See `mdw-fill-prefix' for details."
1521 (cond ((not (consp m)) (format "%s" m))
1522 ((eq (car m) 'match) (match-string (mdw-maybe-car (cdr m))))
1523 ((eq (car m) 'pad) (mdw-padding (match-string
1524 (mdw-maybe-car (cdr m)))))
1525 ((eq (car m) 'eval) (eval (cdr m)))
1528 (defun mdw-examine-fill-prefixes (l)
1529 "Given a list of dynamic fill prefixes, pick one which matches
1530 context and return the static fill prefix to use. Point must be
1531 at the start of a line, and match data must be saved."
1533 (while (cond ((null l) nil)
1534 ((mdw-fill-prefix-match-p (caar l))
1538 (mapcar #'mdw-do-prefix-match
1544 (defun mdw-choose-dynamic-fill-prefix ()
1545 "Work out the dynamic fill prefix based on the variable `mdw-fill-prefix'."
1546 (cond ((and fill-prefix (not (string= fill-prefix ""))) fill-prefix)
1547 ((not mdw-fill-prefix) fill-prefix)
1551 (mdw-examine-fill-prefixes mdw-fill-prefix))))))
1553 (defadvice do-auto-fill (around mdw-dynamic-fill-prefix () activate compile)
1554 "Handle auto-filling, working out a dynamic fill prefix in the
1555 case where there isn't a sensible static one."
1556 (let ((fill-prefix (mdw-choose-dynamic-fill-prefix)))
1559 (defun mdw-fill-paragraph ()
1560 "Fill paragraph, getting a dynamic fill prefix."
1562 (let ((fill-prefix (mdw-choose-dynamic-fill-prefix)))
1563 (fill-paragraph nil)))
1565 (defun mdw-point-within-string-p ()
1566 "Return non-nil if point is within a string."
1567 (let ((state (syntax-ppss)))
1570 (defun mdw-standard-fill-prefix (rx &optional mat)
1571 "Set the dynamic fill prefix, handling standard hanging indents and stuff.
1572 This is just a short-cut for setting the thing by hand, and by
1573 design it doesn't cope with anything approximating a complicated
1575 (setq mdw-fill-prefix
1576 `(((if (mdw-point-within-string-p)
1577 ,(concat "\\(\\s-*\\)" mdw-hanging-indents)
1578 ,(concat rx mdw-hanging-indents))
1580 (pad . ,(or mat 2))))))
1582 ;;;--------------------------------------------------------------------------
1585 ;; Teach PostScript about a condensed variant of Courier. I'm using 85% of
1586 ;; the usual width, which happens to match `mdwfonts', and David Carlisle's
1587 ;; `pslatex'. (Once upon a time, I used 80%, but decided consistency with
1588 ;; `pslatex' was useful.)
1589 (setq ps-user-defined-prologue "
1590 /CourierCondensed /Courier
1591 /CourierCondensed-Bold /Courier-Bold
1592 /CourierCondensed-Oblique /Courier-Oblique
1593 /CourierCondensed-BoldOblique /Courier-BoldOblique
1594 4 { findfont [0.85 0 0 1 0 0] makefont definefont pop } repeat
1597 ;; Hack `ps-print''s settings.
1598 (eval-after-load 'ps-print
1601 ;; Notice that the comment-delimiters should be in italics too.
1602 (pushnew 'font-lock-comment-delimiter-face ps-italic-faces)
1604 ;; Select more suitable colours for the main kinds of tokens. The
1605 ;; colours set on the Emacs faces are chosen for use against a dark
1606 ;; background, and work very badly on white paper.
1607 (ps-extend-face '(font-lock-comment-face "darkgreen" nil italic))
1608 (ps-extend-face '(font-lock-comment-delimiter-face "darkgreen" nil italic))
1609 (ps-extend-face '(font-lock-string-face "RoyalBlue4" nil))
1610 (ps-extend-face '(mdw-punct-face "sienna" nil))
1611 (ps-extend-face '(mdw-number-face "OrangeRed3" nil))
1613 ;; Teach `ps-print' about my condensed varsions of Courier.
1614 (setq ps-font-info-database
1615 (append '((CourierCondensed
1616 (fonts (normal . "CourierCondensed")
1617 (bold . "CourierCondensed-Bold")
1618 (italic . "CourierCondensed-Oblique")
1619 (bold-italic . "CourierCondensed-BoldOblique"))
1621 (line-height . 10.55)
1623 (avg-char-width . 5.1)))
1624 (remove* 'CourierCondensed ps-font-info-database
1627 ;; Arrange to strip overlays from the buffer before we print . This will
1628 ;; prevent `flyspell' from interfering with the printout. (It would be less
1629 ;; bad if `ps-print' could merge the `flyspell' overlay face with the
1630 ;; underlying `font-lock' face, but it can't (and that seems hard). So
1631 ;; instead we have this hack.
1633 ;; The basic trick is to copy the relevant text from the buffer being printed
1634 ;; into a temporary buffer and... just print that. The text properties come
1635 ;; with the text and end up in the new buffer, and the overlays get lost
1636 ;; along the way. Only problem is that the headers identifying the file
1637 ;; being printed get confused, so remember the original buffer and reinstate
1638 ;; it when constructing the headers.
1639 (defvar mdw-printing-buffer)
1641 (defadvice ps-generate-header
1642 (around mdw-use-correct-buffer () activate compile)
1643 "Print the correct name of the buffer being printed."
1644 (with-current-buffer mdw-printing-buffer
1647 (defadvice ps-generate
1648 (around mdw-strip-overlays (buffer from to genfunc) activate compile)
1649 "Strip overlays -- in particular, from `flyspell' -- before printout."
1651 (let ((mdw-printing-buffer buffer))
1652 (insert-buffer-substring buffer from to)
1653 (ad-set-arg 0 (current-buffer))
1654 (ad-set-arg 1 (point-min))
1655 (ad-set-arg 2 (point-max))
1658 ;;;--------------------------------------------------------------------------
1659 ;;; Other common declarations.
1661 ;; Common mode settings.
1663 (defcustom mdw-auto-indent t
1664 "Whether to indent automatically after a newline."
1668 (defun mdw-whitespace-mode (&optional arg)
1669 "Turn on/off whitespace mode, but don't highlight trailing space."
1671 (when (and (boundp 'whitespace-style)
1672 (fboundp 'whitespace-mode))
1673 (let ((whitespace-style (remove 'trailing whitespace-style)))
1674 (whitespace-mode arg))
1675 (setq show-trailing-whitespace whitespace-mode)))
1677 (defvar mdw-do-misc-mode-hacking nil)
1679 (defun mdw-misc-mode-config ()
1680 (and mdw-auto-indent
1681 (cond ((eq major-mode 'lisp-mode)
1682 (local-set-key "\C-m" 'mdw-indent-newline-and-indent))
1683 ((derived-mode-p 'slime-repl-mode 'asm-mode 'comint-mode)
1686 (local-set-key "\C-m" 'newline-and-indent))))
1687 (set (make-local-variable 'mdw-do-misc-mode-hacking) t)
1688 (local-set-key [C-return] 'newline)
1689 (make-local-variable 'page-delimiter)
1690 (setq page-delimiter (concat "^" "\f"
1694 "\\(" " " ".*" " " "\\)?"
1698 (setq comment-column 40)
1700 (setq fill-column mdw-text-width)
1701 (flyspell-prog-mode)
1702 (and (fboundp 'gtags-mode)
1704 (if (fboundp 'hs-minor-mode)
1705 (trap (hs-minor-mode t))
1706 (outline-minor-mode t))
1708 (trap (turn-on-font-lock)))
1710 (defun mdw-post-local-vars-misc-mode-config ()
1711 (setq whitespace-line-column mdw-text-width)
1712 (when (and mdw-do-misc-mode-hacking
1713 (not buffer-read-only))
1714 (setq show-trailing-whitespace t)
1715 (mdw-whitespace-mode 1)))
1716 (add-hook 'hack-local-variables-hook 'mdw-post-local-vars-misc-mode-config)
1718 (defmacro mdw-advise-update-angry-fruit-salad (&rest funcs)
1719 `(progn ,@(mapcar (lambda (func)
1721 (after mdw-angry-fruit-salad activate)
1722 (when mdw-do-misc-mode-hacking
1723 (setq show-trailing-whitespace
1724 (not buffer-read-only))
1725 (mdw-whitespace-mode (if buffer-read-only 0 1)))))
1727 (mdw-advise-update-angry-fruit-salad toggle-read-only
1733 (eval-after-load 'gtags
1735 (dolist (key '([mouse-2] [mouse-3]))
1736 (define-key gtags-mode-map key nil))
1737 (define-key gtags-mode-map [C-S-mouse-2] 'gtags-find-tag-by-event)
1738 (define-key gtags-select-mode-map [C-S-mouse-2]
1739 'gtags-select-tag-by-event)
1740 (dolist (map (list gtags-mode-map gtags-select-mode-map))
1741 (define-key map [C-S-mouse-3] 'gtags-pop-stack))))
1743 ;; Backup file handling.
1745 (defcustom mdw-backup-disable-regexps nil
1746 "List of regular expressions: if a file name matches any of
1747 these then the file is not backed up."
1748 :type '(repeat regexp))
1750 (defun mdw-backup-enable-predicate (name)
1751 "[mdw]'s default backup predicate.
1752 Allows a backup if the standard predicate would allow it, and it
1753 doesn't match any of the regular expressions in
1754 `mdw-backup-disable-regexps'."
1755 (and (normal-backup-enable-predicate name)
1756 (let ((answer t) (list mdw-backup-disable-regexps))
1759 (if (string-match (car list) name)
1761 (setq list (cdr list)))
1763 (setq backup-enable-predicate 'mdw-backup-enable-predicate)
1767 (defun mdw-last-one-out-turn-off-the-lights (frame)
1768 "Disconnect from an X display if this was the last frame on that display."
1769 (let ((frame-display (frame-parameter frame 'display)))
1770 (when (and frame-display
1771 (eq window-system 'x)
1772 (not (some (lambda (fr)
1773 (and (not (eq fr frame))
1774 (string= (frame-parameter fr 'display)
1777 (run-with-idle-timer 0 nil #'x-close-connection frame-display))))
1778 (add-hook 'delete-frame-functions 'mdw-last-one-out-turn-off-the-lights)
1780 ;;;--------------------------------------------------------------------------
1781 ;;; Fullscreen-ness.
1783 (defcustom mdw-full-screen-parameters
1784 '((menu-bar-lines . 0)
1785 ;;(vertical-scroll-bars . nil)
1787 "Frame parameters to set when making a frame fullscreen."
1788 :type '(alist :key-type symbol))
1790 (defcustom mdw-full-screen-save
1792 "Extra frame parameters to save when setting fullscreen."
1793 :type '(repeat symbol))
1795 (defun mdw-toggle-full-screen (&optional frame)
1796 "Show the FRAME fullscreen."
1799 (cond ((frame-parameter frame 'fullscreen)
1800 (set-frame-parameter frame 'fullscreen nil)
1801 (modify-frame-parameters
1803 (or (frame-parameter frame 'mdw-full-screen-saved)
1804 (mapcar (lambda (assoc)
1805 (assq (car assoc) default-frame-alist))
1806 mdw-full-screen-parameters))))
1808 (let ((saved (mapcar (lambda (param)
1809 (cons param (frame-parameter frame param)))
1810 (append (mapcar #'car
1811 mdw-full-screen-parameters)
1812 mdw-full-screen-save))))
1813 (set-frame-parameter frame 'mdw-full-screen-saved saved))
1814 (modify-frame-parameters frame mdw-full-screen-parameters)
1815 (set-frame-parameter frame 'fullscreen 'fullboth)))))
1817 ;;;--------------------------------------------------------------------------
1818 ;;; General fontification.
1820 (make-face 'mdw-virgin-face)
1822 (defmacro mdw-define-face (name &rest body)
1823 "Define a face, and make sure it's actually set as the definition."
1827 (copy-face 'mdw-virgin-face ',name)
1828 (defvar ,name ',name)
1829 (put ',name 'face-defface-spec ',body)
1830 (face-spec-set ',name ',body nil)))
1832 (mdw-define-face default
1833 (((type w32)) :family "courier new" :height 85)
1834 (((type x)) :family "6x13" :foundry "trad" :height 130)
1835 (((type color)) :foreground "white" :background "black")
1837 (mdw-define-face fixed-pitch
1838 (((type w32)) :family "courier new" :height 85)
1839 (((type x)) :family "6x13" :foundry "trad" :height 130)
1840 (t :foreground "white" :background "black"))
1841 (mdw-define-face fixed-pitch-serif
1842 (((type w32)) :family "courier new" :height 85 :weight bold)
1843 (((type x)) :family "6x13" :foundry "trad" :height 130 :weight bold)
1844 (t :foreground "white" :background "black" :weight bold))
1845 (mdw-define-face variable-pitch
1846 (((type x)) :family "helvetica" :height 120))
1847 (mdw-define-face region
1848 (((min-colors 64)) :background "grey30")
1849 (((class color)) :background "blue")
1850 (t :inverse-video t))
1851 (mdw-define-face match
1852 (((class color)) :background "blue")
1853 (t :inverse-video t))
1854 (mdw-define-face mc/cursor-face
1855 (((class color)) :background "red")
1856 (t :inverse-video t))
1857 (mdw-define-face minibuffer-prompt
1859 (mdw-define-face mode-line
1860 (((class color)) :foreground "blue" :background "yellow"
1861 :box (:line-width 1 :style released-button))
1862 (t :inverse-video t))
1863 (mdw-define-face mode-line-inactive
1864 (((class color)) :foreground "yellow" :background "blue"
1865 :box (:line-width 1 :style released-button))
1866 (t :inverse-video t))
1867 (mdw-define-face nobreak-space
1869 (t :inherit escape-glyph :underline t))
1870 (mdw-define-face scroll-bar
1871 (t :foreground "black" :background "lightgrey"))
1872 (mdw-define-face fringe
1873 (t :foreground "yellow"))
1874 (mdw-define-face show-paren-match
1875 (((min-colors 64)) :background "darkgreen")
1876 (((class color)) :background "green")
1878 (mdw-define-face show-paren-mismatch
1879 (((class color)) :background "red")
1880 (t :inverse-video t))
1881 (mdw-define-face highlight
1882 (((min-colors 64)) :background "DarkSeaGreen4")
1883 (((class color)) :background "cyan")
1884 (t :inverse-video t))
1886 (mdw-define-face viper-minibuffer-emacs (t nil))
1887 (mdw-define-face viper-minibuffer-insert (t nil))
1888 (mdw-define-face viper-minibuffer-vi (t nil))
1889 (mdw-define-face viper-replace-overlay
1890 (((min-colors 64)) :background "darkred")
1891 (((class color)) :background "red")
1892 (t :inverse-video t))
1893 (mdw-define-face viper-search (t :inherit isearch))
1895 (mdw-define-face compilation-error
1896 (((class color)) :foreground "red" :weight bold)
1898 (mdw-define-face compilation-warning
1899 (((class color)) :foreground "orange" :weight bold)
1901 (mdw-define-face compilation-info
1902 (((class color)) :foreground "green" :weight bold)
1904 (mdw-define-face compilation-line-number
1906 (mdw-define-face compilation-column-number
1907 (((min-colors 64)) :foreground "lightgrey"))
1908 (setq compilation-message-face 'mdw-virgin-face)
1909 (setq compilation-enter-directory-face 'font-lock-comment-face)
1910 (setq compilation-leave-directory-face 'font-lock-comment-face)
1912 (mdw-define-face holiday-face
1913 (t :background "red"))
1914 (mdw-define-face calendar-today-face
1915 (t :foreground "yellow" :weight bold))
1917 (mdw-define-face comint-highlight-prompt
1919 (mdw-define-face comint-highlight-input
1922 (mdw-define-face Man-underline
1923 (((type tty)) :underline t)
1926 (mdw-define-face ido-subdir
1927 (t :foreground "cyan" :weight bold))
1929 (mdw-define-face dired-directory
1930 (t :foreground "cyan" :weight bold))
1931 (mdw-define-face dired-symlink
1932 (t :foreground "cyan"))
1933 (mdw-define-face dired-perm-write
1936 (mdw-define-face trailing-whitespace
1937 (((class color)) :background "red")
1938 (t :inverse-video t))
1939 (mdw-define-face whitespace-line
1940 (((class color)) :background "darkred")
1941 (t :inverse-video t))
1942 (mdw-define-face mdw-punct-face
1943 (((min-colors 64)) :foreground "burlywood2")
1944 (((class color)) :foreground "yellow"))
1945 (mdw-define-face mdw-number-face
1946 (t :foreground "yellow"))
1947 (mdw-define-face mdw-trivial-face)
1948 (mdw-define-face font-lock-function-name-face
1950 (mdw-define-face font-lock-keyword-face
1952 (mdw-define-face font-lock-constant-face
1954 (mdw-define-face font-lock-builtin-face
1956 (mdw-define-face font-lock-type-face
1957 (t :weight bold :slant italic))
1958 (mdw-define-face font-lock-reference-face
1960 (mdw-define-face font-lock-variable-name-face
1962 (mdw-define-face font-lock-comment-face
1963 (((min-colors 64)) :slant italic :foreground "SeaGreen1")
1964 (((class color)) :foreground "green")
1966 (mdw-define-face font-lock-comment-delimiter-face
1967 (t :inherit font-lock-comment-face))
1968 (mdw-define-face font-lock-string-face
1969 (((min-colors 64)) :foreground "SkyBlue1")
1970 (((class color)) :foreground "cyan")
1972 (mdw-define-face font-lock-doc-face
1973 (t :inherit font-lock-string-face))
1975 (mdw-define-face message-separator
1976 (t :background "red" :foreground "white" :weight bold))
1977 (mdw-define-face message-cited-text
1978 (default :slant italic)
1979 (((min-colors 64)) :foreground "SkyBlue1")
1980 (((class color)) :foreground "cyan"))
1981 (mdw-define-face message-header-cc
1982 (default :slant italic)
1983 (((min-colors 64)) :foreground "SeaGreen1")
1984 (((class color)) :foreground "green"))
1985 (mdw-define-face message-header-newsgroups
1986 (default :slant italic)
1987 (((min-colors 64)) :foreground "SeaGreen1")
1988 (((class color)) :foreground "green"))
1989 (mdw-define-face message-header-subject
1990 (((min-colors 64)) :foreground "SeaGreen1")
1991 (((class color)) :foreground "green"))
1992 (mdw-define-face message-header-to
1993 (((min-colors 64)) :foreground "SeaGreen1")
1994 (((class color)) :foreground "green"))
1995 (mdw-define-face message-header-xheader
1996 (default :slant italic)
1997 (((min-colors 64)) :foreground "SeaGreen1")
1998 (((class color)) :foreground "green"))
1999 (mdw-define-face message-header-other
2000 (default :slant italic)
2001 (((min-colors 64)) :foreground "SeaGreen1")
2002 (((class color)) :foreground "green"))
2003 (mdw-define-face message-header-name
2004 (default :weight bold)
2005 (((min-colors 64)) :foreground "SeaGreen1")
2006 (((class color)) :foreground "green"))
2008 (mdw-define-face which-func
2011 (mdw-define-face gnus-header-name
2012 (default :weight bold)
2013 (((min-colors 64)) :foreground "SeaGreen1")
2014 (((class color)) :foreground "green"))
2015 (mdw-define-face gnus-header-subject
2016 (((min-colors 64)) :foreground "SeaGreen1")
2017 (((class color)) :foreground "green"))
2018 (mdw-define-face gnus-header-from
2019 (((min-colors 64)) :foreground "SeaGreen1")
2020 (((class color)) :foreground "green"))
2021 (mdw-define-face gnus-header-to
2022 (((min-colors 64)) :foreground "SeaGreen1")
2023 (((class color)) :foreground "green"))
2024 (mdw-define-face gnus-header-content
2025 (default :slant italic)
2026 (((min-colors 64)) :foreground "SeaGreen1")
2027 (((class color)) :foreground "green"))
2029 (mdw-define-face gnus-cite-1
2030 (((min-colors 64)) :foreground "SkyBlue1")
2031 (((class color)) :foreground "cyan"))
2032 (mdw-define-face gnus-cite-2
2033 (((min-colors 64)) :foreground "RoyalBlue2")
2034 (((class color)) :foreground "blue"))
2035 (mdw-define-face gnus-cite-3
2036 (((min-colors 64)) :foreground "MediumOrchid")
2037 (((class color)) :foreground "magenta"))
2038 (mdw-define-face gnus-cite-4
2039 (((min-colors 64)) :foreground "firebrick2")
2040 (((class color)) :foreground "red"))
2041 (mdw-define-face gnus-cite-5
2042 (((min-colors 64)) :foreground "burlywood2")
2043 (((class color)) :foreground "yellow"))
2044 (mdw-define-face gnus-cite-6
2045 (((min-colors 64)) :foreground "SeaGreen1")
2046 (((class color)) :foreground "green"))
2047 (mdw-define-face gnus-cite-7
2048 (((min-colors 64)) :foreground "SlateBlue1")
2049 (((class color)) :foreground "cyan"))
2050 (mdw-define-face gnus-cite-8
2051 (((min-colors 64)) :foreground "RoyalBlue2")
2052 (((class color)) :foreground "blue"))
2053 (mdw-define-face gnus-cite-9
2054 (((min-colors 64)) :foreground "purple2")
2055 (((class color)) :foreground "magenta"))
2056 (mdw-define-face gnus-cite-10
2057 (((min-colors 64)) :foreground "DarkOrange2")
2058 (((class color)) :foreground "red"))
2059 (mdw-define-face gnus-cite-11
2060 (t :foreground "grey"))
2062 (mdw-define-face gnus-emphasis-underline
2063 (((type tty)) :underline t)
2066 (mdw-define-face diff-header
2068 (mdw-define-face diff-index
2070 (mdw-define-face diff-file-header
2072 (mdw-define-face diff-hunk-header
2073 (((min-colors 64)) :foreground "SkyBlue1")
2074 (((class color)) :foreground "cyan"))
2075 (mdw-define-face diff-function
2076 (default :weight bold)
2077 (((min-colors 64)) :foreground "SkyBlue1")
2078 (((class color)) :foreground "cyan"))
2079 (mdw-define-face diff-header
2080 (((min-colors 64)) :background "grey10"))
2081 (mdw-define-face diff-added
2082 (((class color)) :foreground "green"))
2083 (mdw-define-face diff-removed
2084 (((class color)) :foreground "red"))
2085 (mdw-define-face diff-context
2087 (mdw-define-face diff-refine-change
2088 (((min-colors 64)) :background "RoyalBlue4")
2090 (mdw-define-face diff-refine-removed
2091 (((min-colors 64)) :background "#500")
2093 (mdw-define-face diff-refine-added
2094 (((min-colors 64)) :background "#050")
2097 (setq ediff-force-faces t)
2098 (mdw-define-face ediff-current-diff-A
2099 (((min-colors 64)) :background "darkred")
2100 (((class color)) :background "red")
2101 (t :inverse-video t))
2102 (mdw-define-face ediff-fine-diff-A
2103 (((min-colors 64)) :background "red3")
2104 (((class color)) :inverse-video t)
2105 (t :inverse-video nil))
2106 (mdw-define-face ediff-even-diff-A
2107 (((min-colors 64)) :background "#300"))
2108 (mdw-define-face ediff-odd-diff-A
2109 (((min-colors 64)) :background "#300"))
2110 (mdw-define-face ediff-current-diff-B
2111 (((min-colors 64)) :background "darkgreen")
2112 (((class color)) :background "magenta")
2113 (t :inverse-video t))
2114 (mdw-define-face ediff-fine-diff-B
2115 (((min-colors 64)) :background "green4")
2116 (((class color)) :inverse-video t)
2117 (t :inverse-video nil))
2118 (mdw-define-face ediff-even-diff-B
2119 (((min-colors 64)) :background "#020"))
2120 (mdw-define-face ediff-odd-diff-B
2121 (((min-colors 64)) :background "#020"))
2122 (mdw-define-face ediff-current-diff-C
2123 (((min-colors 64)) :background "darkblue")
2124 (((class color)) :background "blue")
2125 (t :inverse-video t))
2126 (mdw-define-face ediff-fine-diff-C
2127 (((min-colors 64)) :background "blue1")
2128 (((class color)) :inverse-video t)
2129 (t :inverse-video nil))
2130 (mdw-define-face ediff-even-diff-C
2131 (((min-colors 64)) :background "#004"))
2132 (mdw-define-face ediff-odd-diff-C
2133 (((min-colors 64)) :background "#004"))
2134 (mdw-define-face ediff-current-diff-Ancestor
2135 (((min-colors 64)) :background "#630")
2136 (((class color)) :background "blue")
2137 (t :inverse-video t))
2138 (mdw-define-face ediff-even-diff-Ancestor
2139 (((min-colors 64)) :background "#320"))
2140 (mdw-define-face ediff-odd-diff-Ancestor
2141 (((min-colors 64)) :background "#320"))
2143 (mdw-define-face magit-hash
2144 (((min-colors 64)) :foreground "grey40")
2145 (((class color)) :foreground "blue"))
2146 (mdw-define-face magit-diff-hunk-heading
2147 (((min-colors 64)) :foreground "grey70" :background "grey25")
2148 (((class color)) :foreground "yellow"))
2149 (mdw-define-face magit-diff-hunk-heading-highlight
2150 (((min-colors 64)) :foreground "grey70" :background "grey35")
2151 (((class color)) :foreground "yellow" :background "blue"))
2152 (mdw-define-face magit-diff-added
2153 (((min-colors 64)) :foreground "#ddffdd" :background "#335533")
2154 (((class color)) :foreground "green"))
2155 (mdw-define-face magit-diff-added-highlight
2156 (((min-colors 64)) :foreground "#cceecc" :background "#336633")
2157 (((class color)) :foreground "green" :background "blue"))
2158 (mdw-define-face magit-diff-removed
2159 (((min-colors 64)) :foreground "#ffdddd" :background "#553333")
2160 (((class color)) :foreground "red"))
2161 (mdw-define-face magit-diff-removed-highlight
2162 (((min-colors 64)) :foreground "#eecccc" :background "#663333")
2163 (((class color)) :foreground "red" :background "blue"))
2164 (mdw-define-face magit-blame-heading
2165 (((min-colors 64)) :foreground "white" :background "grey25"
2166 :weight normal :slant normal)
2167 (((class color)) :foreground "white" :background "blue"
2168 :weight normal :slant normal))
2169 (mdw-define-face magit-blame-name
2170 (t :inherit magit-blame-heading :slant italic))
2171 (mdw-define-face magit-blame-date
2172 (((min-colors 64)) :inherit magit-blame-heading :foreground "grey60")
2173 (((class color)) :inherit magit-blame-heading :foreground "cyan"))
2174 (mdw-define-face magit-blame-summary
2175 (t :inherit magit-blame-heading :weight bold))
2177 (mdw-define-face dylan-header-background
2178 (((min-colors 64)) :background "NavyBlue")
2179 (((class color)) :background "blue"))
2181 (mdw-define-face erc-input-face
2182 (t :foreground "red"))
2184 (mdw-define-face woman-bold
2186 (mdw-define-face woman-italic
2189 (eval-after-load "rst"
2191 (mdw-define-face rst-level-1-face
2192 (t :foreground "SkyBlue1" :weight bold))
2193 (mdw-define-face rst-level-2-face
2194 (t :foreground "SeaGreen1" :weight bold))
2195 (mdw-define-face rst-level-3-face
2197 (mdw-define-face rst-level-4-face
2199 (mdw-define-face rst-level-5-face
2201 (mdw-define-face rst-level-6-face
2204 (mdw-define-face p4-depot-added-face
2205 (t :foreground "green"))
2206 (mdw-define-face p4-depot-branch-op-face
2207 (t :foreground "yellow"))
2208 (mdw-define-face p4-depot-deleted-face
2209 (t :foreground "red"))
2210 (mdw-define-face p4-depot-unmapped-face
2211 (t :foreground "SkyBlue1"))
2212 (mdw-define-face p4-diff-change-face
2213 (t :foreground "yellow"))
2214 (mdw-define-face p4-diff-del-face
2215 (t :foreground "red"))
2216 (mdw-define-face p4-diff-file-face
2217 (t :foreground "SkyBlue1"))
2218 (mdw-define-face p4-diff-head-face
2219 (t :background "grey10"))
2220 (mdw-define-face p4-diff-ins-face
2221 (t :foreground "green"))
2223 (mdw-define-face w3m-anchor-face
2224 (t :foreground "SkyBlue1" :underline t))
2225 (mdw-define-face w3m-arrived-anchor-face
2226 (t :foreground "SkyBlue1" :underline t))
2228 (mdw-define-face whizzy-slice-face
2229 (t :background "grey10"))
2230 (mdw-define-face whizzy-error-face
2231 (t :background "darkred"))
2233 ;; Ellipses used to indicate hidden text (and similar).
2234 (mdw-define-face mdw-ellipsis-face
2235 (((type tty)) :foreground "blue") (t :foreground "grey60"))
2236 (let ((dollar (make-glyph-code ?$ 'mdw-ellipsis-face))
2237 (backslash (make-glyph-code ?\\ 'mdw-ellipsis-face))
2238 (dot (make-glyph-code ?. 'mdw-ellipsis-face))
2239 (bar (make-glyph-code ?| mdw-ellipsis-face)))
2240 (set-display-table-slot standard-display-table 0 dollar)
2241 (set-display-table-slot standard-display-table 1 backslash)
2242 (set-display-table-slot standard-display-table 4
2243 (vector dot dot dot))
2244 (set-display-table-slot standard-display-table 5 bar))
2246 ;;;--------------------------------------------------------------------------
2249 (mdw-define-face mdw-point-overlay-face
2251 (((min-colors 64)) :background "darkblue")
2252 (((class color)) :background "blue")
2253 (((type tty) (class mono)) :inverse-video t))
2255 (defcustom mdw-point-overlay-fringe-display '(vertical-bar . vertical-bar)
2256 "Bitmaps to display in the left and right fringes in the current line."
2257 :type '(cons symbol symbol))
2259 (defun mdw-configure-point-overlay ()
2260 (let ((ov (make-overlay 0 0)))
2261 (overlay-put ov 'priority 0)
2262 (let* ((fringe (or mdw-point-overlay-fringe-display (cons nil nil)))
2263 (left (car fringe)) (right (cdr fringe))
2267 (put-text-property 0 1 'display `(left-fringe ,left) ss)
2268 (setq s (concat s ss))))
2271 (put-text-property 0 1 'display `(right-fringe ,right) ss)
2272 (setq s (concat s ss))))
2273 (when (or left right)
2274 (overlay-put ov 'before-string s)))
2275 (overlay-put ov 'face 'mdw-point-overlay-face)
2279 (defvar mdw-point-overlay (mdw-configure-point-overlay)
2280 "An overlay used for showing where point is in the selected window.")
2281 (defun mdw-reconfigure-point-overlay ()
2283 (setq mdw-point-overlay (mdw-configure-point-overlay)))
2285 (defun mdw-remove-point-overlay ()
2286 "Remove the current-point overlay."
2287 (delete-overlay mdw-point-overlay))
2289 (defun mdw-update-point-overlay ()
2290 "Mark the current point position with an overlay."
2291 (if (not mdw-point-overlay-mode)
2292 (mdw-remove-point-overlay)
2293 (overlay-put mdw-point-overlay 'window (selected-window))
2294 (move-overlay mdw-point-overlay
2295 (line-beginning-position)
2296 (+ (line-end-position) 1))))
2298 (defvar mdw-point-overlay-buffers nil
2299 "List of buffers using `mdw-point-overlay-mode'.")
2301 (define-minor-mode mdw-point-overlay-mode
2302 "Indicate current line with an overlay."
2304 (let ((buffer (current-buffer)))
2305 (setq mdw-point-overlay-buffers
2306 (mapcan (lambda (buf)
2307 (if (and (buffer-live-p buf)
2308 (not (eq buf buffer)))
2310 mdw-point-overlay-buffers))
2311 (if mdw-point-overlay-mode
2312 (setq mdw-point-overlay-buffers
2313 (cons buffer mdw-point-overlay-buffers))))
2314 (cond (mdw-point-overlay-buffers
2315 (add-hook 'pre-command-hook 'mdw-remove-point-overlay)
2316 (add-hook 'post-command-hook 'mdw-update-point-overlay))
2318 (mdw-remove-point-overlay)
2319 (remove-hook 'pre-command-hook 'mdw-remove-point-overlay)
2320 (remove-hook 'post-command-hook 'mdw-update-point-overlay))))
2322 (define-globalized-minor-mode mdw-global-point-overlay-mode
2323 mdw-point-overlay-mode
2324 (lambda () (if (not (minibufferp)) (mdw-point-overlay-mode t))))
2326 (defvar mdw-terminal-title-alist nil)
2327 (defun mdw-update-terminal-title ()
2328 (when (let ((term (frame-parameter nil 'tty-type)))
2329 (and term (string-match "^xterm" term)))
2330 (let* ((tty (frame-parameter nil 'tty))
2331 (old (assoc tty mdw-terminal-title-alist))
2332 (new (format-mode-line frame-title-format)))
2333 (unless (and old (equal (cdr old) new))
2334 (if old (rplacd old new)
2335 (setq mdw-terminal-title-alist
2336 (cons (cons tty new) mdw-terminal-title-alist)))
2337 (send-string-to-terminal (concat "\e]2;" new "\e\\"))))))
2339 (add-hook 'post-command-hook 'mdw-update-terminal-title)
2341 ;;;--------------------------------------------------------------------------
2344 (defvar mdw-ediff-previous-windows)
2345 (defun mdw-ediff-setup ()
2346 (setq mdw-ediff-previous-windows (current-window-configuration)))
2347 (defun mdw-ediff-suspend-or-quit ()
2348 (set-window-configuration mdw-ediff-previous-windows))
2349 (add-hook 'ediff-before-setup-hook 'mdw-ediff-setup)
2350 (add-hook 'ediff-quit-hook 'mdw-ediff-suspend-or-quit t)
2351 (add-hook 'ediff-suspend-hook 'mdw-ediff-suspend-or-quit t)
2353 ;;;--------------------------------------------------------------------------
2354 ;;; C programming configuration.
2356 ;; Make C indentation nice.
2358 (defun mdw-c-lineup-arglist (langelem)
2359 "Hack for DWIMmery in c-lineup-arglist."
2361 (c-block-in-arglist-dwim (c-langelem-2nd-pos c-syntactic-element)))
2363 (c-lineup-arglist langelem)))
2365 (defun mdw-c-indent-extern-mumble (langelem)
2366 "Indent `extern \"...\" {' lines."
2368 (back-to-indentation)
2370 "\\s-*\\<extern\\>\\s-*\"\\([^\\\\\"]+\\|\\.\\)*\"\\s-*{")
2374 (defun mdw-c-indent-arglist-nested (langelem)
2375 "Indent continued argument lists.
2376 If we've nested more than one argument list, then only introduce a single
2377 indentation anyway."
2378 (let ((context c-syntactic-context)
2379 (pos (c-langelem-2nd-pos c-syntactic-element))
2380 (should-indent-p t))
2382 (eq (caar context) 'arglist-cont-nonempty))
2383 (when (and (= (caddr (pop context)) pos)
2385 (memq (caar context) '(arglist-intro
2386 arglist-cont-nonempty)))
2387 (setq should-indent-p nil)))
2388 (if should-indent-p '+ 0)))
2390 (defvar mdw-define-c-styles-hook nil
2391 "Hook run when `cc-mode' starts up to define styles.")
2393 (defun mdw-merge-style-alists (first second)
2395 (dolist (item first)
2396 (let ((key (car item)) (value (cdr item)))
2397 (if (let* ((key-name (symbol-name key))
2398 (key-len (length key-name)))
2400 (string= (subseq key-name (- key-len 6)) "-alist")))
2402 (mdw-merge-style-alists value
2403 (cdr (assoc key second))))
2405 (push item output))))
2406 (dolist (item second)
2407 (unless (assoc (car item) first)
2408 (push item output)))
2411 (defmacro* mdw-define-c-style (name (&optional parent) &rest assocs)
2412 "Define a C style, called NAME (a symbol) based on PARENT, setting ASSOCs.
2413 A function, named `mdw-define-c-style/NAME', is defined to actually install
2414 the style using `c-add-style', and added to the hook
2415 `mdw-define-c-styles-hook'. If CC Mode is already loaded, then the style is
2417 (declare (indent defun))
2418 (let* ((name-string (symbol-name name))
2419 (var (intern (concat "mdw-c-style/" name-string)))
2420 (func (intern (concat "mdw-define-c-style/" name-string))))
2425 (let ((parent-list (intern (concat "mdw-c-style/"
2426 (symbol-name parent)))))
2427 `(mdw-merge-style-alists ',assocs ,parent-list))))
2428 (defun ,func () (c-add-style ,name-string ,var))
2429 (and (featurep 'cc-mode) (,func))
2430 (add-hook 'mdw-define-c-styles-hook ',func)
2433 (eval-after-load "cc-mode"
2434 '(run-hooks 'mdw-define-c-styles-hook))
2436 (mdw-define-c-style mdw-c ()
2437 (c-basic-offset . 2)
2438 (comment-column . 40)
2439 (c-class-key . "class")
2440 (c-backslash-column . 72)
2441 (c-label-minimum-indentation . 0)
2442 (c-offsets-alist (substatement-open . (add 0 c-indent-one-line-block))
2443 (defun-open . (add 0 c-indent-one-line-block))
2444 (arglist-cont-nonempty . mdw-c-lineup-arglist)
2445 (topmost-intro . mdw-c-indent-extern-mumble)
2446 (cpp-define-intro . 0)
2448 (inextern-lang . [0])
2454 (statement-cont . +)
2455 (statement-case-intro . +)))
2457 (mdw-define-c-style mdw-trustonic-c (mdw-c)
2458 (c-basic-offset . 4)
2459 (c-offsets-alist (access-label . -2)))
2461 (mdw-define-c-style mdw-trustonic-alec-c (mdw-trustonic-c)
2462 (comment-column . 0)
2463 (c-indent-comment-alist (anchored-comment . (column . 0))
2464 (end-block . (space . 1))
2465 (cpp-end-block . (space . 1))
2466 (other . (space . 1)))
2467 (c-offsets-alist (arglist-cont-nonempty . mdw-c-indent-arglist-nested)))
2469 (defun mdw-set-default-c-style (modes style)
2470 "Update the default CC Mode style for MODES to be STYLE.
2472 MODES may be a list of major mode names or a singleton. STYLE is a style
2474 (let ((modes (if (listp modes) modes (list modes)))
2475 (style (symbol-name style)))
2476 (setq c-default-style
2477 (append (mapcar (lambda (mode)
2480 (remove-if (lambda (assoc)
2481 (memq (car assoc) modes))
2482 (if (listp c-default-style)
2484 (list (cons 'other c-default-style))))))))
2485 (setq c-default-style "mdw-c")
2487 (mdw-set-default-c-style '(c-mode c++-mode) 'mdw-c)
2489 (defvar mdw-c-comment-fill-prefix
2490 `((,(concat "\\([ \t]*/?\\)"
2493 "\\([A-Za-z]+:[ \t]*\\)?"
2494 mdw-hanging-indents)
2495 (pad . 1) (match . 2) (pad . 3) (pad . 4) (pad . 5)))
2496 "Fill prefix matching C comments (both kinds).")
2498 (defun mdw-fontify-c-and-c++ ()
2500 ;; Fiddle with some syntax codes.
2501 (modify-syntax-entry ?* ". 23")
2502 (modify-syntax-entry ?/ ". 124b")
2503 (modify-syntax-entry ?\n "> b")
2506 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2508 ;; Now define things to be fontified.
2509 (make-local-variable 'font-lock-keywords)
2511 (mdw-regexps "alignas" ;C11 macro, C++11
2513 "and" ;C++, C95 macro
2514 "and_eq" ;C++, C95 macro
2515 "asm" ;K&R, C++, GCC
2516 "atomic" ;C11 macro, C++11 template type
2518 "bitand" ;C++, C95 macro
2519 "bitor" ;C++, C95 macro
2520 "bool" ;C++, C99 macro
2525 "char16_t" ;C++11, C11 library type
2526 "char32_t" ;C++11, C11 library type
2528 "complex" ;C99 macro, C++ template type
2529 "compl" ;C++, C95 macro
2533 "continue" ;K&R, C89
2535 "defined" ;C89 preprocessor
2542 ;; "entry" ;K&R -- never used
2553 "imaginary" ;C99 macro
2554 "inline" ;C++, C99, GCC
2561 "noreturn" ;C11 macro
2562 "not" ;C++, C95 macro
2563 "not_eq" ;C++, C95 macro
2566 "or" ;C++, C95 macro
2567 "or_eq" ;C++, C95 macro
2571 "register" ;K&R, C89
2572 "reinterpret_cast" ;C++
2579 "static_assert" ;C11 macro, C++11
2586 "thread_local" ;C11 macro, C++11
2592 "unsigned" ;K&R, C89
2597 "wchar_t" ;C++, C89 library type
2599 "xor" ;C++, C95 macro
2600 "xor_eq" ;C++, C95 macro
2609 "_Pragma" ;C99 preprocessor
2610 "_Static_assert" ;C11
2611 "_Thread_local" ;C11
2614 "__attribute__" ;GCC
2617 "__extension__" ;GCC
2627 (mdw-regexps "false" ;C++, C99 macro
2629 "true" ;C++, C99 macro
2631 (preprocessor-keywords
2632 (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
2633 "ident" "if" "ifdef" "ifndef" "import" "include"
2634 "line" "pragma" "unassert" "undef" "warning"))
2636 (mdw-regexps "class" "defs" "encode" "end" "implementation"
2637 "interface" "private" "protected" "protocol" "public"
2640 (setq font-lock-keywords
2643 ;; Fontify include files as strings.
2644 (list (concat "^[ \t]*\\#[ \t]*"
2645 "\\(include\\|import\\)"
2646 "[ \t]*\\(<[^>]+>?\\)")
2647 '(2 font-lock-string-face))
2649 ;; Preprocessor directives are `references'?.
2650 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
2651 preprocessor-keywords
2652 "\\)\\>\\|[0-9]+\\|$\\)\\)")
2653 '(1 font-lock-keyword-face))
2655 ;; Handle the keywords defined above.
2656 (list (concat "@\\<\\(" objc-keywords "\\)\\>")
2657 '(0 font-lock-keyword-face))
2659 (list (concat "\\<\\(" c-keywords "\\)\\>")
2660 '(0 font-lock-keyword-face))
2662 (list (concat "\\<\\(" c-builtins "\\)\\>")
2663 '(0 font-lock-variable-name-face))
2665 ;; Handle numbers too.
2667 ;; This looks strange, I know. It corresponds to the
2668 ;; preprocessor's idea of what a number looks like, rather than
2669 ;; anything sensible.
2670 (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
2671 "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
2672 '(0 mdw-number-face))
2674 ;; And anything else is punctuation.
2675 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2676 '(0 mdw-punct-face))))))
2678 (define-derived-mode sod-mode c-mode "Sod"
2679 "Major mode for editing Sod code.")
2680 (push '("\\.sod$" . sod-mode) auto-mode-alist)
2682 (dolist (hook '(c-mode-hook objc-mode-hook c++-mode-hook))
2683 (add-hook hook 'mdw-misc-mode-config t)
2684 (add-hook hook 'mdw-fontify-c-and-c++ t))
2686 ;;;--------------------------------------------------------------------------
2689 (define-derived-mode apcalc-mode c-mode "AP Calc"
2690 "Major mode for editing Calc code.")
2692 (defun mdw-fontify-apcalc ()
2694 ;; Fiddle with some syntax codes.
2695 (modify-syntax-entry ?* ". 23")
2696 (modify-syntax-entry ?/ ". 14")
2699 (setq comment-start "/* ")
2700 (setq comment-end " */")
2701 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2703 ;; Now define things to be fontified.
2704 (make-local-variable 'font-lock-keywords)
2706 (mdw-regexps "break" "case" "cd" "continue" "define" "default"
2707 "do" "else" "exit" "for" "global" "goto" "help" "if"
2708 "local" "mat" "obj" "print" "quit" "read" "return"
2709 "show" "static" "switch" "while" "write")))
2711 (setq font-lock-keywords
2714 ;; Handle the keywords defined above.
2715 (list (concat "\\<\\(" c-keywords "\\)\\>")
2716 '(0 font-lock-keyword-face))
2718 ;; Handle numbers too.
2720 ;; This looks strange, I know. It corresponds to the
2721 ;; preprocessor's idea of what a number looks like, rather than
2722 ;; anything sensible.
2723 (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
2724 "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
2725 '(0 mdw-number-face))
2727 ;; And anything else is punctuation.
2728 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2729 '(0 mdw-punct-face))))))
2732 (add-hook 'apcalc-mode-hook 'mdw-misc-mode-config t)
2733 (add-hook 'apcalc-mode-hook 'mdw-fontify-apcalc t))
2735 ;;;--------------------------------------------------------------------------
2736 ;;; Java programming configuration.
2738 ;; Make indentation nice.
2740 (mdw-define-c-style mdw-java ()
2741 (c-basic-offset . 2)
2742 (c-backslash-column . 72)
2743 (c-offsets-alist (substatement-open . 0)
2748 (statement-case-intro . +)))
2749 (mdw-set-default-c-style 'java-mode 'mdw-java)
2751 ;; Declare Java fontification style.
2753 (defun mdw-fontify-java ()
2755 ;; Fiddle with some syntax codes.
2756 (modify-syntax-entry ?@ ".")
2757 (modify-syntax-entry ?@ "." font-lock-syntax-table)
2760 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2762 ;; Now define things to be fontified.
2763 (make-local-variable 'font-lock-keywords)
2764 (let ((java-keywords
2765 (mdw-regexps "abstract" "assert"
2766 "boolean" "break" "byte"
2767 "case" "catch" "char" "class" "const" "continue"
2768 "default" "do" "double"
2769 "else" "enum" "extends"
2770 "final" "finally" "float" "for"
2772 "if" "implements" "import" "instanceof" "int"
2776 "package" "private" "protected" "public"
2778 "short" "static" "strictfp" "switch" "synchronized"
2779 "throw" "throws" "transient" "try"
2784 (mdw-regexps "false" "null" "super" "this" "true")))
2786 (setq font-lock-keywords
2789 ;; Handle the keywords defined above.
2790 (list (concat "\\<\\(" java-keywords "\\)\\>")
2791 '(0 font-lock-keyword-face))
2793 ;; Handle the magic builtins defined above.
2794 (list (concat "\\<\\(" java-builtins "\\)\\>")
2795 '(0 font-lock-variable-name-face))
2797 ;; Handle numbers too.
2799 ;; The following isn't quite right, but it's close enough.
2800 (list (concat "\\<\\("
2801 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2802 "[0-9]+\\(\\.[0-9]*\\)?"
2803 "\\([eE][-+]?[0-9]+\\)?\\)"
2805 '(0 mdw-number-face))
2807 ;; And anything else is punctuation.
2808 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2809 '(0 mdw-punct-face))))))
2812 (add-hook 'java-mode-hook 'mdw-misc-mode-config t)
2813 (add-hook 'java-mode-hook 'mdw-fontify-java t))
2815 ;;;--------------------------------------------------------------------------
2816 ;;; Javascript programming configuration.
2818 (defun mdw-javascript-style ()
2819 (setq js-indent-level 2)
2820 (setq js-expr-indent-offset 0))
2822 (defun mdw-fontify-javascript ()
2825 (mdw-javascript-style)
2826 (setq js-auto-indent-flag t)
2828 ;; Now define things to be fontified.
2829 (make-local-variable 'font-lock-keywords)
2830 (let ((javascript-keywords
2831 (mdw-regexps "abstract" "boolean" "break" "byte" "case" "catch"
2832 "char" "class" "const" "continue" "debugger" "default"
2833 "delete" "do" "double" "else" "enum" "export" "extends"
2834 "final" "finally" "float" "for" "function" "goto" "if"
2835 "implements" "import" "in" "instanceof" "int"
2836 "interface" "let" "long" "native" "new" "package"
2837 "private" "protected" "public" "return" "short"
2838 "static" "super" "switch" "synchronized" "throw"
2839 "throws" "transient" "try" "typeof" "var" "void"
2840 "volatile" "while" "with" "yield"))
2841 (javascript-builtins
2842 (mdw-regexps "false" "null" "undefined" "Infinity" "NaN" "true"
2843 "arguments" "this")))
2845 (setq font-lock-keywords
2848 ;; Handle the keywords defined above.
2849 (list (concat "\\_<\\(" javascript-keywords "\\)\\_>")
2850 '(0 font-lock-keyword-face))
2852 ;; Handle the predefined builtins defined above.
2853 (list (concat "\\_<\\(" javascript-builtins "\\)\\_>")
2854 '(0 font-lock-variable-name-face))
2856 ;; Handle numbers too.
2858 ;; The following isn't quite right, but it's close enough.
2859 (list (concat "\\_<\\("
2860 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2861 "[0-9]+\\(\\.[0-9]*\\)?"
2862 "\\([eE][-+]?[0-9]+\\)?\\)"
2864 '(0 mdw-number-face))
2866 ;; And anything else is punctuation.
2867 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2868 '(0 mdw-punct-face))))))
2871 (add-hook 'js-mode-hook 'mdw-misc-mode-config t)
2872 (add-hook 'js-mode-hook 'mdw-fontify-javascript t))
2874 ;;;--------------------------------------------------------------------------
2875 ;;; Scala programming configuration.
2877 (defun mdw-fontify-scala ()
2880 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2882 ;; Define things to be fontified.
2883 (make-local-variable 'font-lock-keywords)
2884 (let ((scala-keywords
2885 (mdw-regexps "abstract" "case" "catch" "class" "def" "do" "else"
2886 "extends" "final" "finally" "for" "forSome" "if"
2887 "implicit" "import" "lazy" "match" "new" "object"
2888 "override" "package" "private" "protected" "return"
2889 "sealed" "throw" "trait" "try" "type" "val"
2890 "var" "while" "with" "yield"))
2892 (mdw-regexps "false" "null" "super" "this" "true"))
2893 (punctuation "[-!%^&*=+:@#~/?\\|`]"))
2895 (setq font-lock-keywords
2898 ;; Magical identifiers between backticks.
2899 (list (concat "`\\([^`]+\\)`")
2900 '(1 font-lock-variable-name-face))
2902 ;; Handle the keywords defined above.
2903 (list (concat "\\_<\\(" scala-keywords "\\)\\_>")
2904 '(0 font-lock-keyword-face))
2906 ;; Handle the constants defined above.
2907 (list (concat "\\_<\\(" scala-constants "\\)\\_>")
2908 '(0 font-lock-variable-name-face))
2910 ;; Magical identifiers between backticks.
2911 (list (concat "`\\([^`]+\\)`")
2912 '(1 font-lock-variable-name-face))
2914 ;; Handle numbers too.
2916 ;; As usual, not quite right.
2917 (list (concat "\\_<\\("
2918 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2919 "[0-9]+\\(\\.[0-9]*\\)?"
2920 "\\([eE][-+]?[0-9]+\\)?\\)"
2922 '(0 mdw-number-face))
2924 ;; And everything else is punctuation.
2925 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2926 '(0 mdw-punct-face)))
2928 font-lock-syntactic-keywords
2931 ;; Single quotes around characters. But not when used to quote
2932 ;; symbol names. Ugh.
2933 (list (concat "\\('\\)"
2935 "\\|" "\\\\" "\\(" "\\\\\\\\" "\\)*"
2936 "u+" "[0-9a-fA-F]\\{4\\}"
2937 "\\|" "\\\\" "[0-7]\\{1,3\\}"
2938 "\\|" "\\\\" "." "\\)"
2944 (add-hook 'scala-mode-hook 'mdw-misc-mode-config t)
2945 (add-hook 'scala-mode-hook 'mdw-fontify-scala t))
2947 ;;;--------------------------------------------------------------------------
2948 ;;; C# programming configuration.
2950 ;; Make indentation nice.
2952 (mdw-define-c-style mdw-csharp ()
2953 (c-basic-offset . 2)
2954 (c-backslash-column . 72)
2955 (c-offsets-alist (substatement-open . 0)
2960 (statement-case-intro . +)))
2961 (mdw-set-default-c-style 'csharp-mode 'mdw-csharp)
2963 ;; Declare C# fontification style.
2965 (defun mdw-fontify-csharp ()
2968 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2970 ;; Now define things to be fontified.
2971 (make-local-variable 'font-lock-keywords)
2972 (let ((csharp-keywords
2973 (mdw-regexps "abstract" "as" "bool" "break" "byte" "case" "catch"
2974 "char" "checked" "class" "const" "continue" "decimal"
2975 "default" "delegate" "do" "double" "else" "enum"
2976 "event" "explicit" "extern" "finally" "fixed" "float"
2977 "for" "foreach" "goto" "if" "implicit" "in" "int"
2978 "interface" "internal" "is" "lock" "long" "namespace"
2979 "new" "object" "operator" "out" "override" "params"
2980 "private" "protected" "public" "readonly" "ref"
2981 "return" "sbyte" "sealed" "short" "sizeof"
2982 "stackalloc" "static" "string" "struct" "switch"
2983 "throw" "try" "typeof" "uint" "ulong" "unchecked"
2984 "unsafe" "ushort" "using" "virtual" "void" "volatile"
2988 (mdw-regexps "base" "false" "null" "this" "true")))
2990 (setq font-lock-keywords
2993 ;; Handle the keywords defined above.
2994 (list (concat "\\<\\(" csharp-keywords "\\)\\>")
2995 '(0 font-lock-keyword-face))
2997 ;; Handle the magic builtins defined above.
2998 (list (concat "\\<\\(" csharp-builtins "\\)\\>")
2999 '(0 font-lock-variable-name-face))
3001 ;; Handle numbers too.
3003 ;; The following isn't quite right, but it's close enough.
3004 (list (concat "\\<\\("
3005 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
3006 "[0-9]+\\(\\.[0-9]*\\)?"
3007 "\\([eE][-+]?[0-9]+\\)?\\)"
3009 '(0 mdw-number-face))
3011 ;; And anything else is punctuation.
3012 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3013 '(0 mdw-punct-face))))))
3015 (define-derived-mode csharp-mode java-mode "C#"
3016 "Major mode for editing C# code.")
3018 (add-hook 'csharp-mode-hook 'mdw-fontify-csharp t)
3020 ;;;--------------------------------------------------------------------------
3021 ;;; F# programming configuration.
3023 (setq fsharp-indent-offset 2)
3025 (defun mdw-fontify-fsharp ()
3027 (let ((punct "=<>+-*/|&%!@?"))
3029 ((>= i (length punct)))
3030 (modify-syntax-entry (aref punct i) ".")))
3032 (modify-syntax-entry ?_ "_")
3033 (modify-syntax-entry ?( "(")
3034 (modify-syntax-entry ?) ")")
3036 (setq indent-tabs-mode nil)
3038 (let ((fsharp-keywords
3039 (mdw-regexps "abstract" "and" "as" "assert" "atomic"
3041 "checked" "class" "component" "const" "constraint"
3042 "constructor" "continue"
3043 "default" "delegate" "do" "done" "downcast" "downto"
3044 "eager" "elif" "else" "end" "exception" "extern"
3045 "finally" "fixed" "for" "fori" "fun" "function"
3048 "if" "in" "include" "inherit" "inline" "interface"
3051 "match" "measure" "member" "method" "mixin" "module"
3054 "object" "of" "open" "or" "override"
3055 "parallel" "params" "private" "process" "protected"
3057 "rec" "recursive" "return"
3058 "sealed" "sig" "static" "struct"
3059 "tailcall" "then" "to" "trait" "try" "type"
3061 "val" "virtual" "void" "volatile"
3062 "when" "while" "with"
3066 (mdw-regexps "asr" "land" "lor" "lsl" "lsr" "lxor" "mod"
3067 "base" "false" "null" "true"))
3070 (mdw-regexps "do" "let" "return" "use" "yield"))
3072 (preprocessor-keywords
3073 (mdw-regexps "if" "indent" "else" "endif")))
3075 (setq font-lock-keywords
3076 (list (list (concat "\\(^\\|[^\"]\\)"
3079 "\\(" "[^)*]" "[^*]*" "\\*+" "\\)*"
3084 '(2 font-lock-comment-face))
3086 (list (concat "'" "\\("
3089 "\\|" "[0-9][0-9][0-9]"
3090 "\\|" "u" "[0-9a-fA-F]\\{4\\}"
3091 "\\|" "U" "[0-9a-fA-F]\\{8\\}"
3097 "\\(" "\\\\" "\\(.\\|\n\\)"
3100 '(0 font-lock-string-face))
3102 (list (concat "\\_<\\(" bang-keywords "\\)!" "\\|"
3103 "^#[ \t]*\\(" preprocessor-keywords "\\)\\_>"
3105 "\\_<\\(" fsharp-keywords "\\)\\_>")
3106 '(0 font-lock-keyword-face))
3107 (list (concat "\\<\\(" fsharp-builtins "\\)\\_>")
3108 '(0 font-lock-variable-name-face))
3110 (list (concat "\\_<"
3111 "\\(" "0[bB][01]+" "\\|"
3113 "0[xX][0-9a-fA-F]+" "\\)"
3114 "\\(" "lf\\|LF" "\\|"
3115 "[uU]?[ysnlL]?" "\\)"
3122 "\\([eE][-+]?[0-9]+\\)?"
3127 '(0 mdw-number-face))
3129 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3130 '(0 mdw-punct-face))))))
3132 (defun mdw-fontify-inferior-fsharp ()
3133 (mdw-fontify-fsharp)
3134 (setq font-lock-keywords
3135 (append (list (list "^[#-]" '(0 font-lock-comment-face))
3136 (list "^>" '(0 font-lock-keyword-face)))
3137 font-lock-keywords)))
3140 (add-hook 'fsharp-mode-hook 'mdw-misc-mode-config t)
3141 (add-hook 'fsharp-mode-hook 'mdw-fontify-fsharp t)
3142 (add-hook 'inferior-fsharp-mode-hooks 'mdw-fontify-inferior-fsharp t))
3144 ;;;--------------------------------------------------------------------------
3145 ;;; Go programming configuration.
3147 (defun mdw-fontify-go ()
3149 (make-local-variable 'font-lock-keywords)
3151 (mdw-regexps "break" "case" "chan" "const" "continue"
3152 "default" "defer" "else" "fallthrough" "for"
3153 "func" "go" "goto" "if" "import"
3154 "interface" "map" "package" "range" "return"
3155 "select" "struct" "switch" "type" "var"))
3157 (mdw-regexps "bool" "byte" "complex64" "complex128" "error"
3158 "float32" "float64" "int" "uint8" "int16" "int32"
3159 "int64" "rune" "string" "uint" "uint8" "uint16"
3160 "uint32" "uint64" "uintptr" "void"
3161 "false" "iota" "nil" "true"
3163 "append" "cap" "copy" "delete" "imag" "len" "make"
3164 "new" "panic" "real" "recover")))
3166 (setq font-lock-keywords
3169 ;; Handle the keywords defined above.
3170 (list (concat "\\<\\(" go-keywords "\\)\\>")
3171 '(0 font-lock-keyword-face))
3172 (list (concat "\\<\\(" go-intrinsics "\\)\\>")
3173 '(0 font-lock-variable-name-face))
3175 ;; Strings and characters.
3177 "\\(" "[^\\']" "\\|"
3179 "\\(" "[abfnrtv\\'\"]" "\\|"
3180 "[0-7]\\{3\\}" "\\|"
3181 "x" "[0-9A-Fa-f]\\{2\\}" "\\|"
3182 "u" "[0-9A-Fa-f]\\{4\\}" "\\|"
3183 "U" "[0-9A-Fa-f]\\{8\\}" "\\)" "\\)"
3187 "\\(" "[^\n\\\"]+" "\\|" "\\\\." "\\)*"
3191 '(0 font-lock-string-face))
3193 ;; Handle numbers too.
3195 ;; The following isn't quite right, but it's close enough.
3196 (list (concat "\\<\\("
3197 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
3198 "[0-9]+\\(\\.[0-9]*\\)?"
3199 "\\([eE][-+]?[0-9]+\\)?\\)")
3200 '(0 mdw-number-face))
3202 ;; And anything else is punctuation.
3203 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3204 '(0 mdw-punct-face))))))
3206 (add-hook 'go-mode-hook 'mdw-misc-mode-config t)
3207 (add-hook 'go-mode-hook 'mdw-fontify-go t))
3209 ;;;--------------------------------------------------------------------------
3210 ;;; Rust programming configuration.
3212 (setq-default rust-indent-offset 2)
3214 (defun mdw-self-insert-and-indent (count)
3216 (self-insert-command count)
3217 (indent-according-to-mode))
3219 (defun mdw-fontify-rust ()
3221 ;; Hack syntax categories.
3222 (modify-syntax-entry ?$ ".")
3223 (modify-syntax-entry ?% ".")
3224 (modify-syntax-entry ?= ".")
3226 ;; Fontify keywords and things.
3227 (make-local-variable 'font-lock-keywords)
3228 (let ((rust-keywords
3229 (mdw-regexps "abstract" "alignof" "as" "async" "await"
3230 "become" "box" "break"
3231 "const" "continue" "crate"
3233 "else" "enum" "extern"
3237 "macro" "match" "mod" "move" "mut"
3238 "offsetof" "override"
3239 "priv" "proc" "pub" "pure"
3241 "sizeof" "static" "struct" "super"
3242 "trait" "try" "type" "typeof"
3243 "union" "unsafe" "unsized" "use"
3248 (mdw-regexps "array" "pointer" "slice" "tuple"
3249 "bool" "true" "false"
3251 "i8" "i16" "i32" "i64" "isize"
3252 "u8" "u16" "u32" "u64" "usize"
3255 (setq font-lock-keywords
3258 ;; Handle the keywords defined above.
3259 (list (concat "\\_<\\(" rust-keywords "\\)\\_>")
3260 '(0 font-lock-keyword-face))
3261 (list (concat "\\_<\\(" rust-builtins "\\)\\_>")
3262 '(0 font-lock-variable-name-face))
3264 ;; Handle numbers too.
3265 (list (concat "\\_<\\("
3267 "\\(" "\\(\\.[0-9_]+\\)?[eE][-+]?[0-9_]+"
3271 "\\|" "\\(" "[0-9][0-9_]*"
3272 "\\|" "0x[0-9a-fA-F_]+"
3276 "\\([ui]\\(8\\|16\\|32\\|64\\|size\\)\\)?"
3278 '(0 mdw-number-face))
3280 ;; And anything else is punctuation.
3281 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3282 '(0 mdw-punct-face)))
3283 font-lock-syntactic-face-function nil))
3285 ;; Hack key bindings.
3286 (local-set-key [?{] 'mdw-self-insert-and-indent)
3287 (local-set-key [?}] 'mdw-self-insert-and-indent))
3290 (add-hook 'rust-mode-hook 'mdw-misc-mode-config t)
3291 (add-hook 'rust-mode-hook 'mdw-fontify-rust t))
3293 ;;;--------------------------------------------------------------------------
3294 ;;; Awk programming configuration.
3296 ;; Make Awk indentation nice.
3298 (mdw-define-c-style mdw-awk ()
3299 (c-basic-offset . 2)
3300 (c-offsets-alist (substatement-open . 0)
3301 (c-backslash-column . 72)
3302 (statement-cont . 0)
3303 (statement-case-intro . +)))
3304 (mdw-set-default-c-style 'awk-mode 'mdw-awk)
3306 ;; Declare Awk fontification style.
3308 (defun mdw-fontify-awk ()
3310 ;; Miscellaneous fiddling.
3311 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3313 ;; Now define things to be fontified.
3314 (make-local-variable 'font-lock-keywords)
3316 (mdw-regexps "BEGIN" "END" "ARGC" "ARGIND" "ARGV" "CONVFMT"
3317 "ENVIRON" "ERRNO" "FIELDWIDTHS" "FILENAME" "FNR"
3318 "FS" "IGNORECASE" "NF" "NR" "OFMT" "OFS" "ORS" "RS"
3319 "RSTART" "RLENGTH" "RT" "SUBSEP"
3320 "atan2" "break" "close" "continue" "cos" "delete"
3321 "do" "else" "exit" "exp" "fflush" "file" "for" "func"
3322 "function" "gensub" "getline" "gsub" "if" "in"
3323 "index" "int" "length" "log" "match" "next" "rand"
3324 "return" "print" "printf" "sin" "split" "sprintf"
3325 "sqrt" "srand" "strftime" "sub" "substr" "system"
3326 "systime" "tolower" "toupper" "while")))
3328 (setq font-lock-keywords
3331 ;; Handle the keywords defined above.
3332 (list (concat "\\<\\(" c-keywords "\\)\\>")
3333 '(0 font-lock-keyword-face))
3335 ;; Handle numbers too.
3337 ;; The following isn't quite right, but it's close enough.
3338 (list (concat "\\<\\("
3339 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
3340 "[0-9]+\\(\\.[0-9]*\\)?"
3341 "\\([eE][-+]?[0-9]+\\)?\\)"
3343 '(0 mdw-number-face))
3345 ;; And anything else is punctuation.
3346 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3347 '(0 mdw-punct-face))))))
3350 (add-hook 'awk-mode-hook 'mdw-misc-mode-config t)
3351 (add-hook 'awk-mode-hook 'mdw-fontify-awk t))
3353 ;;;--------------------------------------------------------------------------
3354 ;;; Perl programming style.
3356 ;; Perl indentation style.
3358 (setq-default perl-indent-level 2)
3360 (setq-default cperl-indent-level 2
3361 cperl-continued-statement-offset 2
3362 cperl-indent-region-fix-constructs nil
3363 cperl-continued-brace-offset 0
3364 cperl-brace-offset -2
3365 cperl-brace-imaginary-offset 0
3366 cperl-label-offset 0)
3368 ;; Define perl fontification style.
3370 (defun mdw-fontify-perl ()
3372 ;; Miscellaneous fiddling.
3373 (modify-syntax-entry ?$ "\\")
3374 (modify-syntax-entry ?$ "\\" font-lock-syntax-table)
3375 (modify-syntax-entry ?: "." font-lock-syntax-table)
3376 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3378 ;; Now define fontification things.
3379 (make-local-variable 'font-lock-keywords)
3380 (let ((perl-keywords
3387 "ge" "given" "gt" "goto"
3389 "last" "le" "local" "lt"
3394 "redo" "require" "return"
3396 "undef" "unless" "until" "use"
3399 (setq font-lock-keywords
3402 ;; Set up the keywords defined above.
3403 (list (concat "\\<\\(" perl-keywords "\\)\\>")
3404 '(0 font-lock-keyword-face))
3406 ;; At least numbers are simpler than C.
3407 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
3408 "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\)?"
3409 "\\([eE][-+]?[0-9_]+\\)?")
3410 '(0 mdw-number-face))
3412 ;; And anything else is punctuation.
3413 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3414 '(0 mdw-punct-face))))))
3416 (defun perl-number-tests (&optional arg)
3417 "Assign consecutive numbers to lines containing `#t'. With ARG,
3418 strip numbers instead."
3421 (goto-char (point-min))
3422 (let ((i 0) (fmt (if arg "" " %4d")))
3423 (while (search-forward "#t" nil t)
3424 (delete-region (point) (line-end-position))
3426 (insert (format fmt i)))
3427 (goto-char (point-min))
3428 (if (re-search-forward "\\(tests\\s-*=>\\s-*\\)\\w*" nil t)
3429 (replace-match (format "\\1%d" i))))))
3431 (dolist (hook '(perl-mode-hook cperl-mode-hook))
3432 (add-hook hook 'mdw-misc-mode-config t)
3433 (add-hook hook 'mdw-fontify-perl t))
3435 ;;;--------------------------------------------------------------------------
3436 ;;; Python programming style.
3438 (setq-default py-indent-offset 2
3440 python-indent-offset 2
3441 python-fill-docstring-style 'symmetric)
3443 (defun mdw-fontify-pythonic (keywords builtins)
3445 ;; Miscellaneous fiddling.
3446 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3447 (setq indent-tabs-mode nil)
3448 (set (make-local-variable 'forward-sexp-function) nil)
3450 ;; Now define fontification things.
3451 (make-local-variable 'font-lock-keywords)
3452 (setq font-lock-keywords
3455 ;; Set up the keywords defined above.
3456 (list (concat "\\_<\\(" keywords "\\)\\_>")
3457 '(0 font-lock-keyword-face))
3458 (list (concat "\\(^\\|[^.]\\)\\_<\\(" builtins "\\)\\_>")
3459 '(2 font-lock-variable-name-face))
3460 (list (concat "\\_<\\(__\\(\\sw+\\|\\s_+\\)+__\\)\\_>")
3461 '(0 font-lock-variable-name-face))
3463 ;; At least numbers are simpler than C.
3464 (list (concat "\\_<0\\([xX][0-9a-fA-F]+\\|[oO]?[0-7]+\\|[bB][01]+\\)\\|"
3465 "\\_<[0-9][0-9]*\\(\\.[0-9]*\\)?"
3466 "\\([eE][-+]?[0-9]+\\|[lL]\\)?")
3467 '(0 mdw-number-face))
3469 ;; And anything else is punctuation.
3470 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3471 '(0 mdw-punct-face)))))
3473 ;; Define Python fontification styles.
3475 (defun mdw-fontify-python ()
3476 (mdw-fontify-pythonic
3477 (mdw-regexps "and" "as" "assert"
3481 "elif" "else" "except" "exec"
3482 "finally" "for" "from"
3484 "if" "import" "in" "is"
3494 (mdw-regexps "Ellipsis"
3496 "None" "NotImplemented"
3504 "FloatingPointError"
3523 "NotImplementedError"
3531 "UnicodeDecodeError"
3532 "UnicodeEncodeError"
3533 "UnicodeTranslateError"
3537 "DeprecationWarning"
3540 "PendingDeprecationWarning"
3549 "abs" "absolute_import" "all" "any" "apply"
3550 "basestring" "bin" "bool" "buffer" "bytearray"
3551 "callable" "coerce" "chr" "classmethod"
3552 "cmp" "compile" "complex"
3553 "delattr" "dict" "dir" "divmod"
3554 "enumerate" "eval" "execfile"
3555 "file" "filter" "float" "format" "frozenset"
3557 "hasattr" "hash" "help" "hex"
3558 "id" "input" "int" "intern"
3559 "isinstance" "issubclass" "iter"
3560 "len" "list" "locals" "long"
3561 "map" "max" "memoryview" "min"
3563 "object" "oct" "open" "ord"
3564 "pow" "print" "property"
3565 "range" "raw_input" "reduce" "reload"
3566 "repr" "reversed" "round"
3567 "set" "setattr" "slice" "sorted"
3568 "staticmethod" "str" "sum" "super"
3576 (defun mdw-fontify-pyrex ()
3577 (mdw-fontify-pythonic
3578 (mdw-regexps "and" "as" "assert" "break" "cdef" "class" "continue"
3579 "ctypedef" "def" "del" "elif" "else" "enum" "except" "exec"
3580 "extern" "finally" "for" "from" "global" "if"
3581 "import" "in" "is" "lambda" "not" "or" "pass" "print"
3582 "property" "raise" "return" "struct" "try" "while" "with"
3586 (define-derived-mode pyrex-mode python-mode "Pyrex"
3587 "Major mode for editing Pyrex source code")
3588 (setq auto-mode-alist
3589 (append '(("\\.pyx$" . pyrex-mode)
3590 ("\\.pxd$" . pyrex-mode)
3591 ("\\.pxi$" . pyrex-mode))
3595 (add-hook 'python-mode-hook 'mdw-misc-mode-config t)
3596 (add-hook 'python-mode-hook 'mdw-fontify-python t)
3597 (add-hook 'pyrex-mode-hook 'mdw-fontify-pyrex t))
3599 ;;;--------------------------------------------------------------------------
3600 ;;; Lua programming style.
3602 (setq-default lua-indent-level 2)
3604 (defun mdw-fontify-lua ()
3606 ;; Miscellaneous fiddling.
3607 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3609 ;; Now define fontification things.
3610 (make-local-variable 'font-lock-keywords)
3612 (mdw-regexps "and" "break" "do" "else" "elseif" "end"
3613 "false" "for" "function" "goto" "if" "in" "local"
3614 "nil" "not" "or" "repeat" "return" "then" "true"
3616 (setq font-lock-keywords
3619 ;; Set up the keywords defined above.
3620 (list (concat "\\_<\\(" lua-keywords "\\)\\_>")
3621 '(0 font-lock-keyword-face))
3623 ;; At least numbers are simpler than C.
3624 (list (concat "\\_<\\(" "0[xX]"
3625 "\\(" "[0-9a-fA-F]+"
3626 "\\(\\.[0-9a-fA-F]*\\)?"
3627 "\\|" "\\.[0-9a-fA-F]+"
3629 "\\([pP][-+]?[0-9]+\\)?"
3630 "\\|" "\\(" "[0-9]+"
3634 "\\([eE][-+]?[0-9]+\\)?"
3636 '(0 mdw-number-face))
3638 ;; And anything else is punctuation.
3639 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3640 '(0 mdw-punct-face))))))
3643 (add-hook 'lua-mode-hook 'mdw-misc-mode-config t)
3644 (add-hook 'lua-mode-hook 'mdw-fontify-lua t))
3646 ;;;--------------------------------------------------------------------------
3647 ;;; Icon programming style.
3649 ;; Icon indentation style.
3651 (setq-default icon-brace-offset 0
3652 icon-continued-brace-offset 0
3653 icon-continued-statement-offset 2
3654 icon-indent-level 2)
3656 ;; Define Icon fontification style.
3658 (defun mdw-fontify-icon ()
3660 ;; Miscellaneous fiddling.
3661 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3663 ;; Now define fontification things.
3664 (make-local-variable 'font-lock-keywords)
3665 (let ((icon-keywords
3666 (mdw-regexps "break" "by" "case" "create" "default" "do" "else"
3667 "end" "every" "fail" "global" "if" "initial"
3668 "invocable" "link" "local" "next" "not" "of"
3669 "procedure" "record" "repeat" "return" "static"
3670 "suspend" "then" "to" "until" "while"))
3671 (preprocessor-keywords
3672 (mdw-regexps "define" "else" "endif" "error" "ifdef" "ifndef"
3673 "include" "line" "undef")))
3674 (setq font-lock-keywords
3677 ;; Set up the keywords defined above.
3678 (list (concat "\\<\\(" icon-keywords "\\)\\>")
3679 '(0 font-lock-keyword-face))
3681 ;; The things that Icon calls keywords.
3682 (list "&\\sw+\\>" '(0 font-lock-variable-name-face))
3684 ;; At least numbers are simpler than C.
3685 (list (concat "\\<[0-9]+"
3686 "\\([rR][0-9a-zA-Z]+\\|"
3687 "\\.[0-9]+\\([eE][+-]?[0-9]+\\)?\\)\\>\\|"
3688 "\\.[0-9]+\\([eE][+-]?[0-9]+\\)?\\>")
3689 '(0 mdw-number-face))
3692 (list (concat "^[ \t]*$[ \t]*\\<\\("
3693 preprocessor-keywords
3695 '(0 font-lock-keyword-face))
3697 ;; And anything else is punctuation.
3698 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3699 '(0 mdw-punct-face))))))
3702 (add-hook 'icon-mode-hook 'mdw-misc-mode-config t)
3703 (add-hook 'icon-mode-hook 'mdw-fontify-icon t))
3705 ;;;--------------------------------------------------------------------------
3708 (defun mdw-fontify-fortran-common ()
3709 (let ((fortran-keywords
3710 (mdw-regexps "access"
3728 "double\\s-*precision"
3729 "else" "elseif" "elsewhere"
3731 "endblock" "endblockdata"
3787 "select" "selectcase" "selecttype"
3798 (fortran-operators (mdw-regexps "and"
3811 (fortran-intrinsics (mdw-regexps "abs" "dabs" "iabs" "cabs"
3812 "atan" "datan" "atan2" "datan2"
3822 "int" "aint" "idint"
3823 "alog" "dlog" "clog"
3835 "sign" "isign" "dsign"
3837 "sqrt" "dsqrt" "csqrt"
3839 (preprocessor-keywords
3840 (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
3841 "ident" "if" "ifdef" "ifndef" "import" "include"
3842 "line" "pragma" "unassert" "undef" "warning")))
3843 (setq font-lock-keywords-case-fold-search t
3847 ;; Fontify include files as strings.
3848 (list (concat "^[ \t]*\\#[ \t]*" "include"
3849 "[ \t]*\\(<[^>]+>?\\)")
3850 '(1 font-lock-string-face))
3852 ;; Preprocessor directives are `references'?.
3853 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
3854 preprocessor-keywords
3855 "\\)\\>\\|[0-9]+\\|$\\)\\)")
3856 '(1 font-lock-keyword-face))
3858 ;; Set up the keywords defined above.
3859 (list (concat "\\<\\(" fortran-keywords "\\)\\>")
3860 '(0 font-lock-keyword-face))
3862 ;; Set up the `.foo.' operators.
3863 (list (concat "\\.\\(" fortran-operators "\\)\\.")
3864 '(0 font-lock-keyword-face))
3866 ;; Set up the intrinsic functions.
3867 (list (concat "\\<\\(" fortran-intrinsics "\\)\\>")
3868 '(0 font-lock-variable-name-face))
3871 (list (concat "\\(" "\\<" "[0-9]+" "\\(\\.[0-9]*\\)?"
3874 "\\(" "[de]" "[+-]?" "[0-9]+" "\\)?"
3875 "\\(" "_" "\\sw+" "\\)?"
3876 "\\|" "b'[01]*'" "\\|" "'[01]*'b"
3877 "\\|" "b\"[01]*\"" "\\|" "\"[01]*\"b"
3878 "\\|" "o'[0-7]*'" "\\|" "'[0-7]*'o"
3879 "\\|" "o\"[0-7]*\"" "\\|" "\"[0-7]*\"o"
3880 "\\|" "[xz]'[0-9a-f]*'" "\\|" "'[0-9a-f]*'[xz]"
3881 "\\|" "[xz]\"[0-9a-f]*\"" "\\|" "\"[0-9a-f]*\"[xz]")
3882 '(0 mdw-number-face))
3884 ;; Any anything else is punctuation.
3885 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3886 '(0 mdw-punct-face))))
3888 (modify-syntax-entry ?/ "." font-lock-syntax-table)
3889 (modify-syntax-entry ?< ".")
3890 (modify-syntax-entry ?> ".")))
3892 (defun mdw-fontify-fortran () (mdw-fontify-fortran-common))
3893 (defun mdw-fontify-f90 () (mdw-fontify-fortran-common))
3895 (setq fortran-do-indent 2
3897 fortran-structure-indent 2
3898 fortran-comment-line-start "*"
3899 fortran-comment-indent-style 'relative
3900 fortran-continuation-string "&"
3901 fortran-continuation-indent 4)
3903 (setq f90-do-indent 2
3905 f90-program-indent 2
3906 f90-continuation-indent 4
3907 f90-smart-end-names nil
3908 f90-smart-end 'no-blink)
3911 (add-hook 'fortran-mode-hook 'mdw-misc-mode-config t)
3912 (add-hook 'fortran-mode-hook 'mdw-fontify-fortran t)
3913 (add-hook 'f90-mode-hook 'mdw-misc-mode-config t)
3914 (add-hook 'f90-mode-hook 'mdw-fontify-f90 t))
3916 ;;;--------------------------------------------------------------------------
3919 (defun mdw-fontify-asm ()
3920 (modify-syntax-entry ?' "\"")
3921 (modify-syntax-entry ?. "w")
3922 (modify-syntax-entry ?\n ">")
3923 (setf fill-prefix nil)
3924 (modify-syntax-entry ?. "_")
3925 (modify-syntax-entry ?* ". 23")
3926 (modify-syntax-entry ?/ ". 124b")
3927 (modify-syntax-entry ?\n "> b")
3928 (local-set-key ";" 'self-insert-command)
3929 (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)"))
3931 (defun mdw-asm-set-comment ()
3932 (modify-syntax-entry ?; "."
3934 (modify-syntax-entry asm-comment-char "< b")
3935 (setq comment-start (string asm-comment-char ? )))
3936 (add-hook 'asm-mode-local-variables-hook 'mdw-asm-set-comment)
3937 (put 'asm-comment-char 'safe-local-variable 'characterp)
3940 (add-hook 'asm-mode-hook 'mdw-misc-mode-config t)
3941 (add-hook 'asm-mode-hook 'mdw-fontify-asm t))
3943 ;;;--------------------------------------------------------------------------
3944 ;;; TCL configuration.
3946 (setq-default tcl-indent-level 2)
3948 (defun mdw-fontify-tcl ()
3950 (modify-syntax-entry ch "."))
3951 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3952 (make-local-variable 'font-lock-keywords)
3953 (setq font-lock-keywords
3955 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
3956 "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\)?"
3957 "\\([eE][-+]?[0-9_]+\\)?")
3958 '(0 mdw-number-face))
3959 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3960 '(0 mdw-punct-face)))))
3963 (add-hook 'tcl-mode-hook 'mdw-misc-mode-config t)
3964 (add-hook 'tcl-mode-hook 'mdw-fontify-tcl t))
3966 ;;;--------------------------------------------------------------------------
3967 ;;; Dylan programming configuration.
3969 (defun mdw-fontify-dylan ()
3971 (make-local-variable 'font-lock-keywords)
3973 ;; Horrors. `dylan-mode' sets the `major-mode' name after calling this
3974 ;; hook, which undoes all of our configuration.
3975 (setq major-mode 'dylan-mode)
3976 (font-lock-set-defaults)
3978 (let* ((word "[-_a-zA-Z!*@<>$%]+")
3979 (dylan-keywords (mdw-regexps
3981 "C-address" "C-callable-wrapper" "C-function"
3982 "C-mapped-subtype" "C-pointer-type" "C-struct"
3983 "C-subtype" "C-union" "C-variable"
3985 "above" "abstract" "afterwards" "all"
3986 "begin" "below" "block" "by"
3987 "case" "class" "cleanup" "constant" "create"
3989 "else" "elseif" "end" "exception" "export"
3990 "finally" "for" "from" "function"
3993 "if" "in" "instance" "interface" "iterate"
3995 "let" "library" "local"
3996 "macro" "method" "module"
3999 "select" "slot" "subclass"
4001 "unless" "until" "use"
4002 "variable" "virtual"
4004 (sharp-keywords (mdw-regexps
4005 "all-keys" "key" "next" "rest" "include"
4007 (setq font-lock-keywords
4008 (list (list (concat "\\<\\(" dylan-keywords
4009 "\\|" "with\\(out\\)?-" word
4011 '(0 font-lock-keyword-face))
4012 (list (concat "\\<" word ":" "\\|"
4013 "#\\(" sharp-keywords "\\)\\>")
4014 '(0 font-lock-variable-name-face))
4016 "\\([-+]\\|\\<\\)[0-9]+" "\\("
4017 "\\(\\.[0-9]+\\)?" "\\([eE][-+][0-9]+\\)?"
4020 "\\|" "\\.[0-9]+" "\\([eE][-+][0-9]+\\)?"
4023 "\\|" "#x[0-9a-zA-Z]+"
4025 '(0 mdw-number-face))
4027 "\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\|"
4028 "\\_<[-+*/=<>:&|]+\\_>"
4030 '(0 mdw-punct-face))))))
4033 (add-hook 'dylan-mode-hook 'mdw-misc-mode-config t)
4034 (add-hook 'dylan-mode-hook 'mdw-fontify-dylan t))
4036 ;;;--------------------------------------------------------------------------
4037 ;;; Algol 68 configuration.
4039 (setq-default a68-indent-step 2)
4041 (defun mdw-fontify-algol-68 ()
4043 ;; Fix up the syntax table.
4044 (modify-syntax-entry ?# "!" a68-mode-syntax-table)
4045 (dolist (ch '(?- ?+ ?= ?< ?> ?* ?/ ?| ?&))
4046 (modify-syntax-entry ch "." a68-mode-syntax-table))
4048 (make-local-variable 'font-lock-keywords)
4051 (let ((word "COMMENT"))
4052 (do ((regexp (concat "[^" (substring word 0 1) "]+")
4053 (concat regexp "\\|"
4054 (substring word 0 i)
4055 "[^" (substring word i (1+ i)) "]"))
4057 ((>= i (length word)) regexp)))))
4058 (setq font-lock-keywords
4059 (list (list (concat "\\<COMMENT\\>"
4060 "\\(" not-comment "\\)\\{0,5\\}"
4061 "\\(\\'\\|\\<COMMENT\\>\\)")
4062 '(0 font-lock-comment-face))
4063 (list (concat "\\<CO\\>"
4064 "\\([^C]+\\|C[^O]\\)\\{0,5\\}"
4065 "\\($\\|\\<CO\\>\\)")
4066 '(0 font-lock-comment-face))
4067 (list "\\<[A-Z_]+\\>"
4068 '(0 font-lock-keyword-face))
4072 "\\([eE][-+]?[0-9]+\\)?"
4074 '(0 mdw-number-face))
4075 (list "\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/"
4076 '(0 mdw-punct-face))))))
4078 (dolist (hook '(a68-mode-hook a68-mode-hooks))
4079 (add-hook hook 'mdw-misc-mode-config t)
4080 (add-hook hook 'mdw-fontify-algol-68 t))
4082 ;;;--------------------------------------------------------------------------
4083 ;;; REXX configuration.
4085 (defun mdw-rexx-electric-* ()
4090 (defun mdw-rexx-indent-newline-indent ()
4093 (if abbrev-mode (expand-abbrev))
4094 (newline-and-indent))
4096 (defun mdw-fontify-rexx ()
4098 ;; Various bits of fiddling.
4099 (setq mdw-auto-indent nil)
4100 (local-set-key [?\C-m] 'mdw-rexx-indent-newline-indent)
4101 (local-set-key [?*] 'mdw-rexx-electric-*)
4102 (dolist (ch '(?! ?? ?# ?@ ?$)) (modify-syntax-entry ch "w"))
4103 (dolist (ch '(?¬)) (modify-syntax-entry ch "."))
4104 (mdw-standard-fill-prefix "\\([ \t]*/?\*[ \t]*\\)")
4106 ;; Set up keywords and things for fontification.
4107 (make-local-variable 'font-lock-keywords-case-fold-search)
4108 (setq font-lock-keywords-case-fold-search t)
4110 (setq rexx-indent 2)
4111 (setq rexx-end-indent rexx-indent)
4112 (setq rexx-cont-indent rexx-indent)
4114 (make-local-variable 'font-lock-keywords)
4115 (let ((rexx-keywords
4116 (mdw-regexps "address" "arg" "by" "call" "digits" "do" "drop"
4117 "else" "end" "engineering" "exit" "expose" "for"
4118 "forever" "form" "fuzz" "if" "interpret" "iterate"
4119 "leave" "linein" "name" "nop" "numeric" "off" "on"
4120 "options" "otherwise" "parse" "procedure" "pull"
4121 "push" "queue" "return" "say" "select" "signal"
4122 "scientific" "source" "then" "trace" "to" "until"
4123 "upper" "value" "var" "version" "when" "while"
4126 "abbrev" "abs" "bitand" "bitor" "bitxor" "b2x"
4127 "center" "center" "charin" "charout" "chars"
4128 "compare" "condition" "copies" "c2d" "c2x"
4129 "datatype" "date" "delstr" "delword" "d2c" "d2x"
4130 "errortext" "format" "fuzz" "insert" "lastpos"
4131 "left" "length" "lineout" "lines" "max" "min"
4132 "overlay" "pos" "queued" "random" "reverse" "right"
4133 "sign" "sourceline" "space" "stream" "strip"
4134 "substr" "subword" "symbol" "time" "translate"
4135 "trunc" "value" "verify" "word" "wordindex"
4136 "wordlength" "wordpos" "words" "xrange" "x2b" "x2c"
4139 (setq font-lock-keywords
4142 ;; Set up the keywords defined above.
4143 (list (concat "\\<\\(" rexx-keywords "\\)\\>")
4144 '(0 font-lock-keyword-face))
4146 ;; Fontify all symbols the same way.
4147 (list (concat "\\<\\([0-9.][A-Za-z0-9.!?_#@$]*[Ee][+-]?[0-9]+\\|"
4148 "[A-Za-z0-9.!?_#@$]+\\)")
4149 '(0 font-lock-variable-name-face))
4151 ;; And everything else is punctuation.
4152 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4153 '(0 mdw-punct-face))))))
4156 (add-hook 'rexx-mode-hook 'mdw-misc-mode-config t)
4157 (add-hook 'rexx-mode-hook 'mdw-fontify-rexx t))
4159 ;;;--------------------------------------------------------------------------
4160 ;;; Standard ML programming style.
4162 (setq-default sml-nested-if-indent t
4165 sml-type-of-indent nil)
4167 (defun mdw-fontify-sml ()
4169 ;; Make underscore an honorary letter.
4170 (modify-syntax-entry ?' "w")
4173 (mdw-standard-fill-prefix "\\([ \t]*(\*[ \t]*\\)")
4175 ;; Now define fontification things.
4176 (make-local-variable 'font-lock-keywords)
4178 (mdw-regexps "abstype" "and" "andalso" "as"
4181 "else" "end" "eqtype" "exception"
4182 "fn" "fun" "functor"
4184 "if" "in" "include" "infix" "infixr"
4187 "of" "op" "open" "orelse"
4189 "sharing" "sig" "signature" "struct" "structure"
4192 "where" "while" "with" "withtype")))
4194 (setq font-lock-keywords
4197 ;; Set up the keywords defined above.
4198 (list (concat "\\<\\(" sml-keywords "\\)\\>")
4199 '(0 font-lock-keyword-face))
4201 ;; At least numbers are simpler than C.
4202 (list (concat "\\<\\~?"
4203 "\\(0\\([wW]?[xX][0-9a-fA-F]+\\|"
4205 "\\([0-9]+\\(\\.[0-9]+\\)?"
4208 '(0 mdw-number-face))
4210 ;; And anything else is punctuation.
4211 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4212 '(0 mdw-punct-face))))))
4215 (add-hook 'sml-mode-hook 'mdw-misc-mode-config t)
4216 (add-hook 'sml-mode-hook 'mdw-fontify-sml t))
4218 ;;;--------------------------------------------------------------------------
4219 ;;; Haskell configuration.
4221 (setq-default haskell-indent-offset 2)
4223 (defun mdw-fontify-haskell ()
4225 ;; Fiddle with syntax table to get comments right.
4226 (modify-syntax-entry ?' "_")
4227 (modify-syntax-entry ?- ". 12")
4228 (modify-syntax-entry ?\n ">")
4230 ;; Make punctuation be punctuation
4231 (let ((punct "=<>+-*/|&%!@?$.^:#`"))
4233 ((>= i (length punct)))
4234 (modify-syntax-entry (aref punct i) ".")))
4237 (mdw-standard-fill-prefix "\\([ \t]*{?--?[ \t]*\\)")
4239 ;; Fiddle with fontification.
4240 (make-local-variable 'font-lock-keywords)
4241 (let ((haskell-keywords
4243 "case" "ccall" "class"
4244 "data" "default" "deriving" "do"
4248 "if" "import" "in" "infix" "infixl" "infixr" "instance"
4261 (mdw-regexps "ACK" "BEL" "BS" "CAN" "CR" "DC1" "DC2" "DC3" "DC4"
4262 "DEL" "DLE" "EM" "ENQ" "EOT" "ESC" "ETB" "ETX" "FF"
4263 "FS" "GS" "HT" "LF" "NAK" "NUL" "RS" "SI" "SO" "SOH"
4264 "SP" "STX" "SUB" "SYN" "US" "VT")))
4266 (setq font-lock-keywords
4268 (list (concat "{-" "[^-]*" "\\(-+[^-}][^-]*\\)*"
4272 '(0 font-lock-comment-face))
4273 (list (concat "\\_<\\(" haskell-keywords "\\)\\_>")
4274 '(0 font-lock-keyword-face))
4275 (list (concat "'\\("
4279 "\\(" "[abfnrtv\\\"']" "\\|"
4280 "^" "\\(" control-sequences "\\|"
4281 "[]A-Z@[\\^_]" "\\)" "\\|"
4288 '(0 font-lock-string-face))
4289 (list "\\_<[A-Z]\\(\\sw+\\|\\s_+\\)*\\_>"
4290 '(0 font-lock-variable-name-face))
4291 (list (concat "\\_<0\\([xX][0-9a-fA-F]+\\|[oO][0-7]+\\)\\|"
4292 "\\_<[0-9]+\\(\\.[0-9]*\\)?"
4293 "\\([eE][-+]?[0-9]+\\)?")
4294 '(0 mdw-number-face))
4295 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4296 '(0 mdw-punct-face))))))
4299 (add-hook 'haskell-mode-hook 'mdw-misc-mode-config t)
4300 (add-hook 'haskell-mode-hook 'mdw-fontify-haskell t))
4302 ;;;--------------------------------------------------------------------------
4303 ;;; Erlang configuration.
4305 (setq-default erlang-electric-commands nil)
4307 (defun mdw-fontify-erlang ()
4310 (mdw-standard-fill-prefix "\\([ \t]*{?%*[ \t]*\\)")
4312 ;; Fiddle with fontification.
4313 (make-local-variable 'font-lock-keywords)
4314 (let ((erlang-keywords
4315 (mdw-regexps "after" "and" "andalso"
4316 "band" "begin" "bnot" "bor" "bsl" "bsr" "bxor"
4317 "case" "catch" "cond"
4318 "div" "end" "fun" "if" "let" "not"
4320 "query" "receive" "rem" "try" "when" "xor")))
4322 (setq font-lock-keywords
4325 '(0 font-lock-comment-face))
4326 (list (concat "\\<\\(" erlang-keywords "\\)\\>")
4327 '(0 font-lock-keyword-face))
4328 (list (concat "^-\\sw+\\>")
4329 '(0 font-lock-keyword-face))
4330 (list "\\<[0-9]+\\(#[0-9a-zA-Z]+\\|[eE][+-]?[0-9]+\\)?\\>"
4331 '(0 mdw-number-face))
4332 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4333 '(0 mdw-punct-face))))))
4336 (add-hook 'erlang-mode-hook 'mdw-misc-mode-config t)
4337 (add-hook 'erlang-mode-hook 'mdw-fontify-erlang t))
4339 ;;;--------------------------------------------------------------------------
4340 ;;; Texinfo configuration.
4342 (defun mdw-fontify-texinfo ()
4345 (mdw-standard-fill-prefix "\\([ \t]*@c[ \t]+\\)")
4347 ;; Real fontification things.
4348 (make-local-variable 'font-lock-keywords)
4349 (setq font-lock-keywords
4352 ;; Environment names are keywords.
4353 (list "@\\(end\\) *\\([a-zA-Z]*\\)?"
4354 '(2 font-lock-keyword-face))
4356 ;; Unmark escaped magic characters.
4357 (list "\\(@\\)\\([@{}]\\)"
4358 '(1 font-lock-keyword-face)
4359 '(2 font-lock-variable-name-face))
4361 ;; Make sure we get comments properly.
4362 (list "@c\\(omment\\)?\\( .*\\)?$"
4363 '(0 font-lock-comment-face))
4365 ;; Command names are keywords.
4366 (list "@\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)"
4367 '(0 font-lock-keyword-face))
4369 ;; Fontify TeX special characters as punctuation.
4371 '(0 mdw-punct-face)))))
4373 (dolist (hook '(texinfo-mode-hook TeXinfo-mode-hook))
4374 (add-hook hook 'mdw-misc-mode-config t)
4375 (add-hook hook 'mdw-fontify-texinfo t))
4377 ;;;--------------------------------------------------------------------------
4378 ;;; TeX and LaTeX configuration.
4380 (setq-default LaTeX-table-label "tbl:"
4381 TeX-auto-untabify nil
4382 LaTeX-syntactic-comments nil
4383 LaTeX-fill-break-at-separators '(\\\[))
4385 (defun mdw-fontify-tex ()
4386 (setq ispell-parser 'tex)
4389 ;; Don't make maths into a string.
4390 (modify-syntax-entry ?$ ".")
4391 (modify-syntax-entry ?$ "." font-lock-syntax-table)
4392 (local-set-key [?$] 'self-insert-command)
4394 ;; Make `tab' be useful, given that tab stops in TeX don't work well.
4395 (local-set-key "\C-\M-i" 'indent-relative)
4396 (setq indent-tabs-mode nil)
4399 (mdw-standard-fill-prefix "\\([ \t]*%+[ \t]*\\)")
4401 ;; Real fontification things.
4402 (make-local-variable 'font-lock-keywords)
4403 (setq font-lock-keywords
4406 ;; Environment names are keywords.
4407 (list (concat "\\\\\\(begin\\|end\\|newenvironment\\)"
4409 '(2 font-lock-keyword-face))
4411 ;; Suspended environment names are keywords too.
4412 (list (concat "\\\\\\(suspend\\|resume\\)\\(\\[[^]]*\\]\\)?"
4414 '(3 font-lock-keyword-face))
4416 ;; Command names are keywords.
4417 (list "\\\\\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)"
4418 '(0 font-lock-keyword-face))
4420 ;; Handle @/.../ for italics.
4421 ;; (list "\\(@/\\)\\([^/]*\\)\\(/\\)"
4422 ;; '(1 font-lock-keyword-face)
4423 ;; '(3 font-lock-keyword-face))
4425 ;; Handle @*...* for boldness.
4426 ;; (list "\\(@\\*\\)\\([^*]*\\)\\(\\*\\)"
4427 ;; '(1 font-lock-keyword-face)
4428 ;; '(3 font-lock-keyword-face))
4430 ;; Handle @`...' for literal syntax things.
4431 ;; (list "\\(@`\\)\\([^']*\\)\\('\\)"
4432 ;; '(1 font-lock-keyword-face)
4433 ;; '(3 font-lock-keyword-face))
4435 ;; Handle @<...> for nonterminals.
4436 ;; (list "\\(@<\\)\\([^>]*\\)\\(>\\)"
4437 ;; '(1 font-lock-keyword-face)
4438 ;; '(3 font-lock-keyword-face))
4440 ;; Handle other @-commands.
4441 ;; (list "@\\([^a-zA-Z]\\|[a-zA-Z]*\\)"
4442 ;; '(0 font-lock-keyword-face))
4444 ;; Make sure we get comments properly.
4446 '(0 font-lock-comment-face))
4448 ;; Fontify TeX special characters as punctuation.
4450 '(0 mdw-punct-face)))))
4452 (setq TeX-install-font-lock 'tex-font-setup)
4454 (eval-after-load 'font-latex
4455 '(defun font-latex-jit-lock-force-redisplay (buf start end)
4456 "Compatibility for Emacsen not offering `jit-lock-force-redisplay'."
4457 ;; The following block is an expansion of `jit-lock-force-redisplay'
4458 ;; and involved macros taken from CVS Emacs on 2007-04-28.
4459 (with-current-buffer buf
4460 (let ((modified (buffer-modified-p)))
4462 (let ((buffer-undo-list t)
4463 (inhibit-read-only t)
4464 (inhibit-point-motion-hooks t)
4465 (inhibit-modification-hooks t)
4468 buffer-file-truename)
4469 (put-text-property start end 'fontified t))
4471 (restore-buffer-modified-p nil)))))))
4473 (setq TeX-output-view-style
4475 ("^landscape$" "^pstricks$\\|^pst-\\|^psfrag$")
4476 "%(o?)dvips -t landscape %d -o && xdg-open %f")
4477 ("^dvi$" "^pstricks$\\|^pst-\\|^psfrag$"
4478 "%(o?)dvips %d -o && xdg-open %f")
4480 ("^a4\\(?:dutch\\|paper\\|wide\\)\\|sem-a4$" "^landscape$")
4481 "%(o?)xdvi %dS -paper a4r -s 0 %d")
4482 ("^dvi$" "^a4\\(?:dutch\\|paper\\|wide\\)\\|sem-a4$"
4483 "%(o?)xdvi %dS -paper a4 %d")
4485 ("^a5\\(?:comb\\|paper\\)$" "^landscape$")
4486 "%(o?)xdvi %dS -paper a5r -s 0 %d")
4487 ("^dvi$" "^a5\\(?:comb\\|paper\\)$" "%(o?)xdvi %dS -paper a5 %d")
4488 ("^dvi$" "^b5paper$" "%(o?)xdvi %dS -paper b5 %d")
4489 ("^dvi$" "^letterpaper$" "%(o?)xdvi %dS -paper us %d")
4490 ("^dvi$" "^legalpaper$" "%(o?)xdvi %dS -paper legal %d")
4491 ("^dvi$" "^executivepaper$" "%(o?)xdvi %dS -paper 7.25x10.5in %d")
4492 ("^dvi$" "." "%(o?)xdvi %dS %d")
4493 ("^pdf$" "." "xdg-open %o")
4494 ("^html?$" "." "sensible-browser %o")))
4496 (setq TeX-view-program-list
4497 '(("mupdf" ("mupdf %o" (mode-io-correlate " %(outpage)")))))
4499 (setq TeX-view-program-selection
4500 '(((output-dvi style-pstricks) "dvips and gv")
4502 (output-pdf "mupdf")
4503 (output-html "sensible-browser")))
4505 (setq TeX-open-quote "\""
4506 TeX-close-quote "\"")
4508 (setq reftex-use-external-file-finders t
4509 reftex-auto-recenter-toc t)
4511 (setq reftex-label-alist
4512 '(("theorem" ?T "th:" "~\\ref{%s}" t ("theorems?" "th\\.") -2)
4513 ("axiom" ?A "ax:" "~\\ref{%s}" t ("axioms?" "ax\\.") -2)
4514 ("definition" ?D "def:" "~\\ref{%s}" t ("definitions?" "def\\.") -2)
4515 ("proposition" ?P "prop:" "~\\ref{%s}" t
4516 ("propositions?" "prop\\.") -2)
4517 ("lemma" ?L "lem:" "~\\ref{%s}" t ("lemmas?" "lem\\.") -2)
4518 ("example" ?X "eg:" "~\\ref{%s}" t ("examples?") -2)
4519 ("exercise" ?E "ex:" "~\\ref{%s}" t ("exercises?" "ex\\.") -2)
4520 ("enumerate" ?i "i:" "~\\ref{%s}" item ("items?"))))
4521 (setq reftex-section-prefixes
4526 (setq bibtex-field-delimiters 'double-quotes
4527 bibtex-align-at-equal-sign t
4528 bibtex-entry-format '(realign opts-or-alts required-fields
4529 numerical-fields last-comma delimiters
4530 unify-case sort-fields braces)
4531 bibtex-sort-ignore-string-entries nil
4532 bibtex-maintain-sorted-entries 'entry-class
4533 bibtex-include-OPTkey t
4534 bibtex-autokey-names-stretch 1
4535 bibtex-autokey-expand-strings t
4536 bibtex-autokey-name-separator "-"
4537 bibtex-autokey-year-length 4
4538 bibtex-autokey-titleword-separator "-"
4539 bibtex-autokey-name-year-separator "-"
4540 bibtex-autokey-year-title-separator ":")
4543 (dolist (hook '(tex-mode-hook latex-mode-hook
4544 TeX-mode-hook LaTeX-mode-hook))
4545 (add-hook hook 'mdw-misc-mode-config t)
4546 (add-hook hook 'mdw-fontify-tex t))
4547 (add-hook 'bibtex-mode-hook (lambda () (setq fill-column 76))))
4549 ;;;--------------------------------------------------------------------------
4550 ;;; HTML, CSS, and other web foolishness.
4552 (setq-default css-indent-offset 8)
4554 ;;;--------------------------------------------------------------------------
4557 (setq-default psgml-html-build-new-buffer nil)
4559 (defun mdw-sgml-mode ()
4562 (mdw-standard-fill-prefix "")
4563 (make-local-variable 'sgml-delimiters)
4564 (setq sgml-delimiters
4565 '("AND" "&" "COM" "--" "CRO" "&#" "DSC" "]" "DSO" "[" "DTGC" "]"
4566 "DTGO" "[" "ERO" "&" "ETAGO" ":e" "GRPC" ")" "GRPO" "(" "LIT"
4567 "\"" "LITA" "'" "MDC" ">" "MDO" "<!" "MINUS" "-" "MSC" "]]"
4568 "NESTC" "{" "NET" "}" "OPT" "?" "OR" "|" "PERO" "%" "PIC" ">"
4569 "PIO" "<?" "PLUS" "+" "REFC" "." "REP" "*" "RNI" "#" "SEQ" ","
4570 "STAGO" ":" "TAGC" "." "VI" "=" "MS-START" "<![" "MS-END" "]]>"
4571 "XML-ECOM" "-->" "XML-PIC" "?>" "XML-SCOM" "<!--" "XML-TAGCE"
4573 (setq major-mode 'mdw-sgml-mode)
4574 (setq mode-name "[mdw] SGML")
4575 (run-hooks 'mdw-sgml-mode-hook))
4577 ;;;--------------------------------------------------------------------------
4578 ;;; Configuration files.
4580 (defcustom mdw-conf-quote-normal nil
4581 "Control syntax category of quote characters `\"' and `''.
4582 If this is `t', consider quote characters to be normal
4583 punctuation, as for `conf-quote-normal'. If this is `nil' then
4584 leave quote characters as quotes. If this is a list, then
4585 consider the quote characters in the list to be normal
4586 punctuation. If this is a single quote character, then consider
4587 that character only to be normal punctuation."
4588 :type '(choice boolean character (repeat character))
4589 :safe 'mdw-conf-quote-normal-acceptable-value-p)
4590 (defun mdw-conf-quote-normal-acceptable-value-p (value)
4591 "Is the VALUE is an acceptable value for `mdw-conf-quote-normal'?"
4592 (or (booleanp value)
4593 (every (lambda (v) (memq v '(?\" ?')))
4594 (if (listp value) value (list value)))))
4596 (defun mdw-fix-up-quote ()
4597 "Apply the setting of `mdw-conf-quote-normal'."
4598 (let ((flag mdw-conf-quote-normal))
4600 (conf-quote-normal t))
4604 (let ((table (copy-syntax-table (syntax-table))))
4605 (dolist (ch (if (listp flag) flag (list flag)))
4606 (modify-syntax-entry ch "." table))
4607 (set-syntax-table table)
4608 (and font-lock-mode (font-lock-fontify-buffer)))))))
4611 (add-hook 'conf-mode-hook 'mdw-misc-mode-config t)
4612 (add-hook 'conf-mode-local-variables-hook 'mdw-fix-up-quote t t))
4614 ;;;--------------------------------------------------------------------------
4617 (defun mdw-setup-sh-script-mode ()
4619 ;; Fetch the shell interpreter's name.
4620 (let ((shell-name sh-shell-file))
4622 ;; Try reading the hash-bang line.
4624 (goto-char (point-min))
4625 (if (looking-at "#![ \t]*\\([^ \t\n]*\\)")
4626 (setq shell-name (match-string 1))))
4628 ;; Now try to set the shell.
4630 ;; Don't let `sh-set-shell' bugger up my script.
4631 (let ((executable-set-magic #'(lambda (s &rest r) s)))
4632 (sh-set-shell shell-name)))
4634 ;; Don't insert here-document scaffolding automatically.
4635 (local-set-key "<" 'self-insert-command)
4637 ;; Now enable my keys and the fontification.
4638 (mdw-misc-mode-config)
4640 ;; Set the indentation level correctly.
4641 (setq sh-indentation 2)
4642 (setq sh-basic-offset 2))
4644 (setq sh-shell-file "/bin/sh")
4646 ;; Awful hacking to override the shell detection for particular scripts.
4647 (defmacro define-custom-shell-mode (name shell)
4650 (set (make-local-variable 'sh-shell-file) ,shell)
4652 (define-custom-shell-mode bash-mode "/bin/bash")
4653 (define-custom-shell-mode rc-mode "/usr/bin/rc")
4654 (put 'sh-shell-file 'permanent-local t)
4656 ;; Hack the rc syntax table. Backquotes aren't paired in rc.
4657 (eval-after-load "sh-script"
4658 '(or (assq 'rc sh-mode-syntax-table-input)
4675 (assoc (assq 'rc sh-mode-syntax-table-input)))
4678 (setq sh-mode-syntax-table-input
4679 (cons (cons 'rc frag)
4680 sh-mode-syntax-table-input))))))
4683 (add-hook 'sh-mode-hook 'mdw-misc-mode-config t)
4684 (add-hook 'sh-mode-hook 'mdw-setup-sh-script-mode t))
4686 ;;;--------------------------------------------------------------------------
4687 ;;; Emacs shell mode.
4689 (defun mdw-eshell-prompt ()
4690 (let ((left "[") (right "]"))
4691 (when (= (user-uid) 0)
4692 (setq left "«" right "»"))
4695 (replace-regexp-in-string "\\..*$" "" (system-name)))
4697 (let* ((pwd (eshell/pwd)) (npwd (length pwd))
4698 (home (expand-file-name "~")) (nhome (length home)))
4699 (if (and (>= npwd nhome)
4701 (= (elt pwd nhome) ?/))
4702 (string= (substring pwd 0 nhome) home))
4703 (concat "~" (substring pwd (length home)))
4706 (setq-default eshell-prompt-function 'mdw-eshell-prompt)
4707 (setq-default eshell-prompt-regexp "^\\[[^]>]+\\(\\]\\|>>?\\)")
4709 (defun eshell/e (file) (find-file file) nil)
4710 (defun eshell/ee (file) (find-file-other-window file) nil)
4711 (defun eshell/w3m (url) (w3m-goto-url url) nil)
4713 (mdw-define-face eshell-prompt (t :weight bold))
4714 (mdw-define-face eshell-ls-archive (t :weight bold :foreground "red"))
4715 (mdw-define-face eshell-ls-backup (t :foreground "lightgrey" :slant italic))
4716 (mdw-define-face eshell-ls-product (t :foreground "lightgrey" :slant italic))
4717 (mdw-define-face eshell-ls-clutter (t :foreground "lightgrey" :slant italic))
4718 (mdw-define-face eshell-ls-executable (t :weight bold))
4719 (mdw-define-face eshell-ls-directory (t :foreground "cyan" :weight bold))
4720 (mdw-define-face eshell-ls-readonly (t nil))
4721 (mdw-define-face eshell-ls-symlink (t :foreground "cyan"))
4723 (defun mdw-eshell-hack () (setenv "LD_PRELOAD" nil))
4724 (add-hook 'eshell-mode-hook 'mdw-eshell-hack)
4726 ;;;--------------------------------------------------------------------------
4727 ;;; Messages-file mode.
4729 (defun messages-mode-guts ()
4730 (setq messages-mode-syntax-table (make-syntax-table))
4731 (set-syntax-table messages-mode-syntax-table)
4732 (modify-syntax-entry ?0 "w" messages-mode-syntax-table)
4733 (modify-syntax-entry ?1 "w" messages-mode-syntax-table)
4734 (modify-syntax-entry ?2 "w" messages-mode-syntax-table)
4735 (modify-syntax-entry ?3 "w" messages-mode-syntax-table)
4736 (modify-syntax-entry ?4 "w" messages-mode-syntax-table)
4737 (modify-syntax-entry ?5 "w" messages-mode-syntax-table)
4738 (modify-syntax-entry ?6 "w" messages-mode-syntax-table)
4739 (modify-syntax-entry ?7 "w" messages-mode-syntax-table)
4740 (modify-syntax-entry ?8 "w" messages-mode-syntax-table)
4741 (modify-syntax-entry ?9 "w" messages-mode-syntax-table)
4742 (make-local-variable 'comment-start)
4743 (make-local-variable 'comment-end)
4744 (make-local-variable 'indent-line-function)
4745 (setq indent-line-function 'indent-relative)
4746 (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
4747 (make-local-variable 'font-lock-defaults)
4748 (make-local-variable 'messages-mode-keywords)
4750 (mdw-regexps "array" "bitmap" "callback" "docs[ \t]+enum"
4751 "export" "enum" "fixed-octetstring" "flags"
4752 "harmless" "map" "nested" "optional"
4753 "optional-tagged" "package" "primitive"
4754 "primitive-nullfree" "relaxed[ \t]+enum"
4755 "set" "table" "tagged-optional" "union"
4756 "variadic" "vector" "version" "version-tag")))
4757 (setq messages-mode-keywords
4759 (list (concat "\\<\\(" keywords "\\)\\>:")
4760 '(0 font-lock-keyword-face))
4761 '("\\([-a-zA-Z0-9]+:\\)" (0 font-lock-warning-face))
4762 '("\\(\\<[a-z][-_a-zA-Z0-9]*\\)"
4763 (0 font-lock-variable-name-face))
4764 '("\\<\\([0-9]+\\)\\>" (0 mdw-number-face))
4765 '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4766 (0 mdw-punct-face)))))
4767 (setq font-lock-defaults
4768 '(messages-mode-keywords nil nil nil nil))
4769 (run-hooks 'messages-file-hook))
4771 (defun messages-mode ()
4774 (setq major-mode 'messages-mode)
4775 (setq mode-name "Messages")
4776 (messages-mode-guts)
4777 (modify-syntax-entry ?# "<" messages-mode-syntax-table)
4778 (modify-syntax-entry ?\n ">" messages-mode-syntax-table)
4779 (setq comment-start "# ")
4780 (setq comment-end "")
4781 (run-hooks 'messages-mode-hook))
4783 (defun cpp-messages-mode ()
4786 (setq major-mode 'cpp-messages-mode)
4787 (setq mode-name "CPP Messages")
4788 (messages-mode-guts)
4789 (modify-syntax-entry ?* ". 23" messages-mode-syntax-table)
4790 (modify-syntax-entry ?/ ". 14" messages-mode-syntax-table)
4791 (setq comment-start "/* ")
4792 (setq comment-end " */")
4793 (let ((preprocessor-keywords
4794 (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
4795 "ident" "if" "ifdef" "ifndef" "import" "include"
4796 "line" "pragma" "unassert" "undef" "warning")))
4797 (setq messages-mode-keywords
4798 (append (list (list (concat "^[ \t]*\\#[ \t]*"
4799 "\\(include\\|import\\)"
4800 "[ \t]*\\(<[^>]+\\(>\\)?\\)")
4801 '(2 font-lock-string-face))
4802 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
4803 preprocessor-keywords
4804 "\\)\\>\\|[0-9]+\\|$\\)\\)")
4805 '(1 font-lock-keyword-face)))
4806 messages-mode-keywords)))
4807 (run-hooks 'cpp-messages-mode-hook))
4810 (add-hook 'messages-mode-hook 'mdw-misc-mode-config t)
4811 (add-hook 'cpp-messages-mode-hook 'mdw-misc-mode-config t)
4812 ;; (add-hook 'messages-file-hook 'mdw-fontify-messages t)
4815 ;;;--------------------------------------------------------------------------
4816 ;;; Messages-file mode.
4818 (defvar mallow-driver-substitution-face 'mallow-driver-substitution-face
4819 "Face to use for subsittution directives.")
4820 (make-face 'mallow-driver-substitution-face)
4821 (defvar mallow-driver-text-face 'mallow-driver-text-face
4822 "Face to use for body text.")
4823 (make-face 'mallow-driver-text-face)
4825 (defun mallow-driver-mode ()
4828 (setq major-mode 'mallow-driver-mode)
4829 (setq mode-name "Mallow driver")
4830 (setq mallow-driver-mode-syntax-table (make-syntax-table))
4831 (set-syntax-table mallow-driver-mode-syntax-table)
4832 (make-local-variable 'comment-start)
4833 (make-local-variable 'comment-end)
4834 (make-local-variable 'indent-line-function)
4835 (setq indent-line-function 'indent-relative)
4836 (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
4837 (make-local-variable 'font-lock-defaults)
4838 (make-local-variable 'mallow-driver-mode-keywords)
4840 (mdw-regexps "each" "divert" "file" "if"
4841 "perl" "set" "string" "type" "write")))
4842 (setq mallow-driver-mode-keywords
4844 (list (concat "^%\\s *\\(}\\|\\(" keywords "\\)\\>\\).*$")
4845 '(0 font-lock-keyword-face))
4846 (list "^%\\s *\\(#.*\\)?$"
4847 '(0 font-lock-comment-face))
4849 '(0 font-lock-keyword-face))
4850 (list "^|?\\(.+\\)$" '(1 mallow-driver-text-face))
4852 '(0 mallow-driver-substitution-face t)))))
4853 (setq font-lock-defaults
4854 '(mallow-driver-mode-keywords nil nil nil nil))
4855 (modify-syntax-entry ?\" "_" mallow-driver-mode-syntax-table)
4856 (modify-syntax-entry ?\n ">" mallow-driver-mode-syntax-table)
4857 (setq comment-start "%# ")
4858 (setq comment-end "")
4859 (run-hooks 'mallow-driver-mode-hook))
4862 (add-hook 'mallow-driver-hook 'mdw-misc-mode-config t))
4864 ;;;--------------------------------------------------------------------------
4867 (defun nfast-debug-mode ()
4870 (setq major-mode 'nfast-debug-mode)
4871 (setq mode-name "NFast debug")
4872 (setq messages-mode-syntax-table (make-syntax-table))
4873 (set-syntax-table messages-mode-syntax-table)
4874 (make-local-variable 'font-lock-defaults)
4875 (make-local-variable 'nfast-debug-mode-keywords)
4876 (setq truncate-lines t)
4877 (setq nfast-debug-mode-keywords
4879 '("^\\(NFast_\\(Connect\\|Disconnect\\|Submit\\|Wait\\)\\)"
4880 (0 font-lock-keyword-face))
4881 (list (concat "^[ \t]+\\(\\("
4882 "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
4883 "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
4885 "[0-9a-fA-F]+\\)[ \t]*$")
4886 '(0 mdw-number-face))
4887 '("^[ \t]+\.status=[ \t]+\\<\\(OK\\)\\>"
4888 (1 font-lock-keyword-face))
4889 '("^[ \t]+\.status=[ \t]+\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>"
4890 (1 font-lock-warning-face))
4891 '("^[ \t]+\.status[ \t]+\\<\\(zero\\)\\>"
4893 (list (concat "^[ \t]+\\.cmd=[ \t]+"
4894 "\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>")
4895 '(1 font-lock-keyword-face))
4896 '("-?\\<\\([0-9]+\\|0x[0-9a-fA-F]+\\)\\>" (0 mdw-number-face))
4897 '("^\\([ \t]+[a-z0-9.]+\\)" (0 font-lock-variable-name-face))
4898 '("\\<\\([a-z][a-z0-9.]+\\)\\>=" (1 font-lock-variable-name-face))
4899 '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" (0 mdw-punct-face))))
4900 (setq font-lock-defaults
4901 '(nfast-debug-mode-keywords nil nil nil nil))
4902 (run-hooks 'nfast-debug-mode-hook))
4904 ;;;--------------------------------------------------------------------------
4905 ;;; Lispy languages.
4907 ;; Unpleasant bodge.
4908 (unless (boundp 'slime-repl-mode-map)
4909 (setq slime-repl-mode-map (make-sparse-keymap)))
4911 (defun mdw-indent-newline-and-indent ()
4913 (indent-for-tab-command)
4914 (newline-and-indent))
4916 (eval-after-load "cl-indent"
4918 (mapc #'(lambda (pair)
4920 'common-lisp-indent-function
4922 '((destructuring-bind . ((&whole 4 &rest 1) 4 &body))
4923 (multiple-value-bind . ((&whole 4 &rest 1) 4 &body))))))
4925 (defun mdw-common-lisp-indent ()
4926 (make-local-variable 'lisp-indent-function)
4927 (setq lisp-indent-function 'common-lisp-indent-function))
4929 (defmacro mdw-advise-hyperspec-lookup (func args)
4930 `(defadvice ,func (around mdw-browse-w3m ,args activate compile)
4932 (let ((browse-url-browser-function #'mdw-w3m-browse-url))
4935 (mdw-advise-hyperspec-lookup common-lisp-hyperspec (symbol))
4936 (mdw-advise-hyperspec-lookup common-lisp-hyperspec-format (char))
4937 (mdw-advise-hyperspec-lookup common-lisp-hyperspec-lookup-reader-macro (char))
4939 (defun mdw-fontify-lispy ()
4942 (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)")
4944 ;; Not much fontification needed.
4945 (make-local-variable 'font-lock-keywords)
4946 (setq font-lock-keywords
4947 (list (list (concat "\\("
4949 "\\(" "[0-9]+/[0-9]+"
4950 "\\|" "\\(" "[0-9]+" "\\(\\.[0-9]*\\)?" "\\|"
4952 "\\([dDeEfFlLsS][-+]?[0-9]+\\)?"
4957 "[0-9A-Fa-f]+" "\\(/[0-9A-Fa-f]+\\)?"
4958 "\\|" "o" "[-+]?" "[0-7]+" "\\(/[0-7]+\\)?"
4959 "\\|" "b" "[-+]?" "[01]+" "\\(/[01]+\\)?"
4960 "\\|" "[0-9]+" "r" "[-+]?"
4961 "[0-9a-zA-Z]+" "\\(/[0-9a-zA-Z]+\\)?"
4964 '(0 mdw-number-face))
4965 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4966 '(0 mdw-punct-face)))))
4968 ;; Special indentation.
4970 (defcustom mdw-lisp-loop-default-indent 2
4971 "Default indent for simple `loop' body."
4974 (defcustom mdw-lisp-setf-value-indent 2
4975 "Default extra indent for `setf' values."
4976 :type 'integer :safe 'integerp)
4978 (setq lisp-simple-loop-indentation 0
4979 lisp-loop-keyword-indentation 0
4980 lisp-loop-forms-indentation 2
4981 lisp-lambda-list-keyword-parameter-alignment t)
4983 (defun mdw-indent-funcall
4984 (path state &optional indent-point sexp-column normal-indent)
4985 "Indent `funcall' more usefully.
4986 Essentially, treat `funcall foo' as a function name, and align the arguments
4988 (and (or (not (consp path)) (null (cadr path)))
4990 (goto-char (cadr state))
4992 (let ((start-line (line-number-at-pos)))
4993 (and (condition-case nil (progn (forward-sexp 3) t)
4997 (and (= start-line (line-number-at-pos))
4998 (current-column))))))))
5000 (put 'funcall 'common-lisp-indent-function 'mdw-indent-funcall)
5001 (put 'funcall 'lisp-indent-function 'mdw-indent-funcall))
5003 (defun mdw-indent-setf
5004 (path state &optional indent-point sexp-column normal-indent)
5005 "Indent `setf' more usefully.
5006 If the values aren't on the same lines as their variables then indent them
5007 by `mdw-lisp-setf-value-indent' spaces."
5008 (and (or (not (consp path)) (null (cadr path)))
5009 (let ((basic-indent (save-excursion
5010 (goto-char (cadr state))
5012 (and (condition-case nil
5013 (progn (forward-sexp 2) t)
5017 (current-column)))))
5018 (offset (if (consp path) (car path)
5023 (goto-char (cadr state))
5025 (while (< (point) start)
5026 (condition-case nil (forward-sexp 1)
5027 (scan-error (throw 'done nil)))
5030 (and basic-indent offset
5031 (list (+ basic-indent
5033 mdw-lisp-setf-value-indent))
5036 (put 'setf 'common-lisp-indent-functopion 'mdw-indent-setf)
5037 (put 'psetf 'common-lisp-indent-function 'mdw-indent-setf)
5038 (put 'setq 'common-lisp-indent-function 'mdw-indent-setf)
5039 (put 'setf 'lisp-indent-function 'mdw-indent-setf)
5040 (put 'setq 'lisp-indent-function 'mdw-indent-setf)
5041 (put 'setq-local 'lisp-indent-function 'mdw-indent-setf)
5042 (put 'setq-default 'lisp-indent-function 'mdw-indent-setf))
5044 (defadvice common-lisp-loop-part-indentation
5045 (around mdw-fix-loop-indentation (indent-point state) activate compile)
5046 "Improve `loop' indentation.
5047 If the first subform is on the same line as the `loop' keyword, then
5048 align the other subforms beneath it. Otherwise, indent them
5049 `mdw-lisp-loop-default-indent' columns in from the opening parenthesis."
5051 (let* ((loop-indentation (save-excursion
5052 (goto-char (elt state 1))
5055 ;; Don't really care about this.
5056 (when (and (boundp 'lisp-indent-backquote-substitution-mode)
5057 (eq lisp-indent-backquote-substitution-mode 'corrected))
5059 (goto-char (elt state 1))
5060 (incf loop-indentation
5061 (cond ((eq (char-before) ?,) -1)
5062 ((and (eq (char-before) ?@)
5063 (progn (backward-char)
5064 (eq (char-before) ?,)))
5068 ;; If the first loop item is on the same line as the `loop' itself then
5069 ;; use that as the baseline. Otherwise advance by the default indent.
5070 (goto-char (cadr state))
5072 (let ((baseline-indent
5073 (if (= (line-number-at-pos)
5074 (if (condition-case nil (progn (forward-sexp 2) t)
5076 (progn (forward-sexp -1) (line-number-at-pos))
5079 (+ loop-indentation mdw-lisp-loop-default-indent))))
5081 (goto-char indent-point)
5084 (setq ad-return-value
5086 (cond ((condition-case ()
5088 (goto-char (elt state 1))
5092 (not (looking-at "\\(:\\|\\sw\\)")))
5094 (+ baseline-indent lisp-simple-loop-indentation))
5095 ((looking-at "^\\s-*\\(:?\\sw+\\|;\\)")
5096 (+ baseline-indent lisp-loop-keyword-indentation))
5098 (+ baseline-indent lisp-loop-forms-indentation)))
5100 ;; Tell the caller that the next line needs recomputation,
5101 ;; even though it doesn't start a sexp.
5102 loop-indentation)))))
5106 (defcustom mdw-friendly-name "[mdw]"
5107 "How I want to be addressed."
5110 (defadvice slime-user-first-name
5111 (around mdw-use-friendly-name compile activate)
5112 (if mdw-friendly-name (setq ad-return-value mdw-friendly-name)
5117 (if (not mdw-fast-startup)
5119 (require 'slime-autoloads)
5120 (slime-setup '(slime-autodoc slime-c-p-c))))))
5122 (let ((stuff '((cmucl ("cmucl"))
5123 (sbcl ("sbcl") :coding-system utf-8-unix)
5124 (clisp ("clisp") :coding-system utf-8-unix))))
5125 (or (boundp 'slime-lisp-implementations)
5126 (setq slime-lisp-implementations nil))
5128 (let* ((head (car stuff))
5129 (found (assq (car head) slime-lisp-implementations)))
5130 (setq stuff (cdr stuff))
5132 (rplacd found (cdr head))
5133 (setq slime-lisp-implementations
5134 (cons head slime-lisp-implementations))))))
5135 (setq slime-default-lisp 'sbcl)
5140 (dolist (hook '(emacs-lisp-mode-hook
5143 inferior-lisp-mode-hook
5144 lisp-interaction-mode-hook
5146 slime-repl-mode-hook))
5147 (add-hook hook 'mdw-misc-mode-config t)
5148 (add-hook hook 'mdw-fontify-lispy t))
5149 (add-hook 'lisp-mode-hook 'mdw-common-lisp-indent t)
5150 (add-hook 'inferior-lisp-mode-hook
5151 #'(lambda () (local-set-key "\C-m" 'comint-send-and-indent)) t))
5153 ;;;--------------------------------------------------------------------------
5154 ;;; Other languages.
5158 (defun mdw-setup-smalltalk ()
5159 (and mdw-auto-indent
5160 (local-set-key "\C-m" 'smalltalk-newline-and-indent))
5161 (make-local-variable 'mdw-auto-indent)
5162 (setq mdw-auto-indent nil)
5163 (local-set-key "\C-i" 'smalltalk-reindent))
5165 (defun mdw-fontify-smalltalk ()
5166 (make-local-variable 'font-lock-keywords)
5167 (setq font-lock-keywords
5169 (list "\\<[A-Z][a-zA-Z0-9]*\\>"
5170 '(0 font-lock-keyword-face))
5171 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
5172 "[0-9][0-9_]*\\(\\.[0-9_]*\\)?"
5173 "\\([eE][-+]?[0-9_]+\\)?")
5174 '(0 mdw-number-face))
5175 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
5176 '(0 mdw-punct-face)))))
5179 (add-hook 'smalltalk-mode 'mdw-misc-mode-config t)
5180 (add-hook 'smalltalk-mode 'mdw-fontify-smalltalk t))
5184 (defun mdw-setup-m4 ()
5186 ;; Inexplicably, Emacs doesn't match braces in m4 mode. This is very
5187 ;; annoying: fix it.
5188 (modify-syntax-entry ?{ "(")
5189 (modify-syntax-entry ?} ")")
5192 (mdw-standard-fill-prefix "\\([ \t]*\\(?:#+\\|\\<dnl\\>\\)[ \t]*\\)"))
5194 (dolist (hook '(m4-mode-hook autoconf-mode-hook autotest-mode-hook))
5195 (add-hook hook #'mdw-misc-mode-config t)
5196 (add-hook hook #'mdw-setup-m4 t))
5201 (add-hook 'makefile-mode-hook 'mdw-misc-mode-config t))
5203 ;;;--------------------------------------------------------------------------
5206 (defun mdw-text-mode ()
5207 (setq fill-column 72)
5209 (mdw-standard-fill-prefix
5210 "\\([ \t]*\\([>#|:] ?\\)*[ \t]*\\)" 3)
5213 (eval-after-load "flyspell"
5214 '(define-key flyspell-mode-map "\C-\M-i" nil))
5217 (add-hook 'text-mode-hook 'mdw-text-mode t))
5219 ;;;--------------------------------------------------------------------------
5220 ;;; Outline and hide/show modes.
5222 (defun mdw-outline-collapse-all ()
5223 "Completely collapse everything in the entire buffer."
5226 (goto-char (point-min))
5227 (while (< (point) (point-max))
5231 (setq hs-hide-comments-when-hiding-all nil)
5233 (defadvice hs-hide-all (after hide-first-comment activate)
5234 (save-excursion (hs-hide-initial-comment-block)))
5236 ;;;--------------------------------------------------------------------------
5239 (defun mdw-sh-mode-setup ()
5240 (local-set-key [?\C-a] 'comint-bol)
5241 (add-hook 'comint-output-filter-functions
5242 'comint-watch-for-password-prompt))
5244 (defun mdw-term-mode-setup ()
5245 (setq term-prompt-regexp shell-prompt-pattern)
5246 (make-local-variable 'mouse-yank-at-point)
5247 (make-local-variable 'transient-mark-mode)
5248 (setq mouse-yank-at-point t)
5252 (defun comint-send-and-indent ()
5255 (and mdw-auto-indent
5256 (indent-for-tab-command)))
5258 (defadvice comint-line-beginning-position
5259 (around mdw-calculate-it-properly () activate compile)
5260 "Calculate the actual line start for multi-line input."
5261 (if (or comint-use-prompt-regexp
5262 (eq (field-at-pos (point)) 'output))
5264 (setq ad-return-value
5265 (constrain-to-field (line-beginning-position) (point)))))
5267 (defun term-send-meta-right () (interactive) (term-send-raw-string "\e\e[C"))
5268 (defun term-send-meta-left () (interactive) (term-send-raw-string "\e\e[D"))
5269 (defun term-send-ctrl-uscore () (interactive) (term-send-raw-string "\C-_"))
5270 (defun term-send-meta-meta-something ()
5272 (term-send-raw-string "\e\e")
5274 (eval-after-load 'term
5276 (define-key term-raw-map [?\e ?\e] nil)
5277 (define-key term-raw-map [?\e ?\e t] 'term-send-meta-meta-something)
5278 (define-key term-raw-map [?\C-/] 'term-send-ctrl-uscore)
5279 (define-key term-raw-map [M-right] 'term-send-meta-right)
5280 (define-key term-raw-map [?\e ?\M-O ?C] 'term-send-meta-right)
5281 (define-key term-raw-map [M-left] 'term-send-meta-left)
5282 (define-key term-raw-map [?\e ?\M-O ?D] 'term-send-meta-left)))
5284 (defadvice term-exec (before program-args-list compile activate)
5285 "If the PROGRAM argument is a list, interpret it as (PROGRAM . SWITCHES).
5286 This allows you to pass a list of arguments through `ansi-term'."
5287 (let ((program (ad-get-arg 2)))
5290 (ad-set-arg 2 (car program))
5291 (ad-set-arg 4 (cdr program))))))
5293 (defadvice term-exec-1 (around hack-environment compile activate)
5294 "Hack the environment inherited by inferiors in the terminal."
5295 (let ((process-environment (copy-tree process-environment)))
5296 (setenv "LD_PRELOAD" nil)
5299 (defadvice shell (around hack-environment compile activate)
5300 "Hack the environment inherited by inferiors in the shell."
5301 (let ((process-environment (copy-tree process-environment)))
5302 (setenv "LD_PRELOAD" nil)
5306 "Open a terminal containing an ssh session to the HOST."
5307 (interactive "sHost: ")
5308 (ansi-term (list "ssh" host) (format "ssh@%s" host)))
5310 (defcustom git-grep-command
5311 "env GIT_PAGER=cat git grep --no-color -nH -e "
5312 "The default command for \\[git-grep]."
5315 (defvar git-grep-history nil)
5317 (defun git-grep (command-args)
5318 "Run `git grep' with user-specified args and collect output in a buffer."
5320 (list (read-shell-command "Run git grep (like this): "
5321 git-grep-command 'git-grep-history)))
5322 (let ((grep-use-null-device nil))
5323 (grep command-args)))
5325 ;;;--------------------------------------------------------------------------
5326 ;;; Magit configuration.
5328 (setq magit-diff-refine-hunk 't
5329 magit-view-git-manual-method 'man
5330 magit-log-margin '(nil age magit-log-margin-width t 18)
5331 magit-wip-after-save-local-mode-lighter ""
5332 magit-wip-after-apply-mode-lighter ""
5333 magit-wip-before-change-mode-lighter "")
5334 (eval-after-load "magit"
5335 '(progn (global-magit-file-mode 1)
5336 (magit-wip-after-save-mode 1)
5337 (magit-wip-after-apply-mode 1)
5338 (magit-wip-before-change-mode 1)
5339 (add-to-list 'magit-no-confirm 'safe-with-wip)
5340 (add-to-list 'magit-no-confirm 'trash)
5341 (push '(:eval (if (or magit-wip-after-save-local-mode
5342 magit-wip-after-apply-mode
5343 magit-wip-before-change-mode)
5344 (format " wip:%s%s%s"
5345 (if magit-wip-after-apply-mode "A" "")
5346 (if magit-wip-before-change-mode "C" "")
5347 (if magit-wip-after-save-local-mode "S" ""))))
5349 (dolist (popup '(magit-diff-popup
5350 magit-diff-refresh-popup
5351 magit-diff-mode-refresh-popup
5352 magit-revision-mode-refresh-popup))
5353 (magit-define-popup-switch popup ?R "Reverse diff" "-R"))
5354 (magit-define-popup-switch 'magit-rebase-popup ?r
5355 "Rebase merges" "--rebase-merges")))
5357 (defadvice magit-wip-commit-buffer-file
5358 (around mdw-just-this-buffer activate compile)
5359 (let ((magit-save-repository-buffers nil)) ad-do-it))
5361 (defadvice magit-discard
5362 (around mdw-delete-if-prefix-argument activate compile)
5363 (let ((magit-delete-by-moving-to-trash
5364 (and (null current-prefix-arg)
5365 magit-delete-by-moving-to-trash)))
5368 (setq magit-repolist-columns
5369 '(("Name" 16 magit-repolist-column-ident nil)
5370 ("Version" 18 magit-repolist-column-version nil)
5371 ("St" 2 magit-repolist-column-dirty nil)
5372 ("L<U" 3 mdw-repolist-column-unpulled-from-upstream nil)
5373 ("L>U" 3 mdw-repolist-column-unpushed-to-upstream nil)
5374 ("Path" 32 magit-repolist-column-path nil)))
5376 (setq magit-repository-directories '(("~/etc/profile" . 0)
5379 (defadvice magit-list-repos (around mdw-dirname () activate compile)
5380 "Make sure the returned names are directory names.
5381 Otherwise child processes get started in the wrong directory and
5383 (setq ad-return-value (mapcar #'file-name-as-directory ad-do-it)))
5385 (defun mdw-repolist-column-unpulled-from-upstream (_id)
5386 "Insert number of upstream commits not in the current branch."
5387 (let ((upstream (magit-get-upstream-branch (magit-get-current-branch) t)))
5389 (let ((n (cadr (magit-rev-diff-count "HEAD" upstream))))
5390 (propertize (number-to-string n) 'face
5391 (if (> n 0) 'bold 'shadow))))))
5393 (defun mdw-repolist-column-unpushed-to-upstream (_id)
5394 "Insert number of commits in the current branch but not its upstream."
5395 (let ((upstream (magit-get-upstream-branch (magit-get-current-branch) t)))
5397 (let ((n (car (magit-rev-diff-count "HEAD" upstream))))
5398 (propertize (number-to-string n) 'face
5399 (if (> n 0) 'bold 'shadow))))))
5401 (defun mdw-try-smerge ()
5403 (goto-char (point-min))
5404 (when (re-search-forward "^<<<<<<< " nil t)
5406 (add-hook 'find-file-hook 'mdw-try-smerge t)
5408 (defcustom mdw-magit-new-window-modes
5415 "Magit modes which should cause a new window to be used."
5416 :type '(repeat symbol))
5418 (defun mdw-display-magit-buffer (buffer)
5419 "Like `magit-display-buffer-traditional'.
5420 But uses `mdw-magit-new-window-modes' for its list of modes
5421 rather than baking the list into the function."
5422 (display-buffer buffer
5423 (let ((mode (with-current-buffer buffer major-mode)))
5424 (if (and (not mdw-designated-window)
5425 (derived-mode-p 'magit-mode)
5426 (mdw-submode-p mode 'magit-mode)
5427 (not (memq mode mdw-magit-new-window-modes)))
5428 '(display-buffer-same-window . nil)
5430 (setq magit-display-buffer-function 'mdw-display-magit-buffer)
5432 (defun mdw-display-magit-file-buffer (buffer)
5433 "Show a file buffer from a diff."
5434 (select-window (display-buffer buffer)))
5435 (setq magit-display-file-buffer-function 'mdw-display-magit-file-buffer)
5437 ;;;--------------------------------------------------------------------------
5438 ;;; GUD, and especially GDB.
5440 ;; Inhibit window dedication. I mean, seriously, wtf?
5441 (defadvice gdb-display-buffer (after mdw-undedicated (buf) compile activate)
5442 "Don't make windows dedicated. Seriously."
5443 (set-window-dedicated-p ad-return-value nil))
5444 (defadvice gdb-set-window-buffer
5445 (after mdw-undedicated (name &optional ignore-dedicated window)
5447 "Don't make windows dedicated. Seriously."
5448 (set-window-dedicated-p (or window (selected-window)) nil))
5450 ;;;--------------------------------------------------------------------------
5453 (setq sql-postgres-options '("-n" "-P" "pager=off")
5454 sql-postgres-login-params
5455 '((user :default "mdw")
5456 (database :default "mdw")
5457 (server :default "db.distorted.org.uk")))
5459 ;;;--------------------------------------------------------------------------
5462 ;; Turn off `noip' when running `man': it interferes with `man-db''s own
5463 ;; seccomp(2)-based sandboxing, which is (in this case, at least) strictly
5465 (defadvice Man-getpage-in-background
5466 (around mdw-inhibit-noip (topic) compile activate)
5467 "Inhibit the `noip' preload hack when invoking `man'."
5468 (let* ((old-preload (getenv "LD_PRELOAD"))
5469 (preloads (and old-preload
5470 (save-match-data (split-string old-preload ":"))))
5475 (let ((item (pop preloads)))
5476 (if (string-match "\\(/\\|^\\)noip\.so\\(:\\|$\\)" item)
5478 (push item filtered)))))
5482 (setenv "LD_PRELOAD"
5484 (with-output-to-string
5485 (setq filtered (nreverse filtered))
5488 (if first (setq first nil)
5490 (write-string (pop filtered)))))))
5492 (setenv "LD_PRELOAD" old-preload))
5495 ;;;--------------------------------------------------------------------------
5496 ;;; MPC configuration.
5498 (eval-when-compile (trap (require 'mpc)))
5500 (setq mpc-browser-tags '(Artist|Composer|Performer Album|Playlist))
5502 (defun mdw-mpc-now-playing ()
5506 (set-buffer (mpc-proc-cmd (mpc-proc-cmd-list '("status" "currentsong"))))
5507 (mpc--status-callback))
5508 (let ((state (cdr (assq 'state mpc-status))))
5509 (cond ((member state '("stop"))
5510 (message "mpd stopped."))
5511 ((member state '("play" "pause"))
5512 (let* ((artist (cdr (assq 'Artist mpc-status)))
5513 (album (cdr (assq 'Album mpc-status)))
5514 (title (cdr (assq 'Title mpc-status)))
5515 (file (cdr (assq 'file mpc-status)))
5516 (duration-string (cdr (assq 'Time mpc-status)))
5517 (time-string (cdr (assq 'time mpc-status)))
5518 (time (and time-string
5520 (if (string-match ":" time-string)
5521 (substring time-string
5522 0 (match-beginning 0))
5524 (duration (and duration-string
5525 (string-to-number duration-string)))
5526 (pos (and time duration
5527 (format " [%d:%02d/%d:%02d]"
5528 (/ time 60) (mod time 60)
5529 (/ duration 60) (mod duration 60))))
5530 (fmt (cond ((and artist title)
5531 (format "`%s' by %s%s" title artist
5532 (if album (format ", from `%s'" album)
5535 (format "`%s' (no tags)" file))
5537 "(no idea what's playing!)"))))
5538 (if (string= state "play")
5539 (message "mpd playing %s%s" fmt (or pos ""))
5540 (message "mpd paused in %s%s" fmt (or pos "")))))
5542 (message "mpd in unknown state `%s'" state)))))
5544 (defmacro mdw-define-mpc-wrapper (func bvl interactive &rest body)
5546 (interactive ,@interactive)
5549 (mdw-mpc-now-playing)))
5551 (mdw-define-mpc-wrapper mdw-mpc-play-or-pause () nil
5552 (if (member (cdr (assq 'state (mpc-cmd-status))) '("play"))
5556 (mdw-define-mpc-wrapper mdw-mpc-next () nil (mpc-next))
5557 (mdw-define-mpc-wrapper mdw-mpc-prev () nil (mpc-prev))
5558 (mdw-define-mpc-wrapper mdw-mpc-stop () nil (mpc-stop))
5560 (defun mdw-mpc-louder (step)
5561 (interactive (list (if current-prefix-arg
5562 (prefix-numeric-value current-prefix-arg)
5564 (mpc-proc-cmd (format "volume %+d" step)))
5566 (defun mdw-mpc-quieter (step)
5567 (interactive (list (if current-prefix-arg
5568 (prefix-numeric-value current-prefix-arg)
5570 (mpc-proc-cmd (format "volume %+d" (- step))))
5572 (defun mdw-mpc-hack-lines (arg interactivep func)
5573 (if (and interactivep (use-region-p))
5574 (let ((from (region-beginning)) (to (region-end)))
5579 (while (< (point) to)
5582 (let ((n (prefix-numeric-value arg)))
5599 (defun mdw-mpc-select-one ()
5600 (when (and (get-char-property (point) 'mpc-file)
5601 (not (get-char-property (point) 'mpc-select)))
5602 (mpc-select-toggle)))
5604 (defun mdw-mpc-unselect-one ()
5605 (when (get-char-property (point) 'mpc-select)
5606 (mpc-select-toggle)))
5608 (defun mdw-mpc-select (&optional arg interactivep)
5609 (interactive (list current-prefix-arg t))
5610 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
5612 (defun mdw-mpc-unselect (&optional arg interactivep)
5613 (interactive (list current-prefix-arg t))
5614 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-unselect-one))
5616 (defun mdw-mpc-unselect-backwards (arg)
5618 (mdw-mpc-hack-lines (- arg) t 'mdw-mpc-unselect-one))
5620 (defun mdw-mpc-unselect-all ()
5622 (setq mpc-select nil)
5623 (mpc-selection-refresh))
5625 (defun mdw-mpc-next-line (arg)
5630 (defun mdw-mpc-previous-line (arg)
5633 (forward-line (- arg)))
5635 (defun mdw-mpc-playlist-add (&optional arg interactivep)
5636 (interactive (list current-prefix-arg t))
5637 (let ((mpc-select mpc-select))
5638 (when (or arg (and interactivep (use-region-p)))
5639 (setq mpc-select nil)
5640 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
5641 (setq mpc-select (reverse mpc-select))
5642 (mpc-playlist-add)))
5644 (defun mdw-mpc-playlist-delete (&optional arg interactivep)
5645 (interactive (list current-prefix-arg t))
5646 (setq mpc-select (nreverse mpc-select))
5648 (when (or arg (and interactivep (use-region-p)))
5649 (setq mpc-select nil)
5650 (mpc-selection-refresh)
5651 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
5652 (mpc-playlist-delete)))
5654 (defun mdw-mpc-hack-tagbrowsers ()
5655 (setq-local mode-line-format
5657 mode-line-frame-identification
5658 mode-line-buffer-identification)))
5659 (add-hook 'mpc-tagbrowser-mode-hook 'mdw-mpc-hack-tagbrowsers)
5661 (defun mdw-mpc-hack-songs ()
5662 (setq-local header-line-format
5663 ;; '("MPC " mpc-volume " " mpc-current-song)
5664 (list (propertize " " 'display '(space :align-to 0))
5665 ;; 'mpc-songs-format-description
5667 (let ((deactivate-mark) (hscroll (window-hscroll)))
5669 (mpc-format mpc-songs-format 'self hscroll)
5670 ;; That would be simpler than the hscroll handling in
5671 ;; mpc-format, but currently move-to-column does not
5672 ;; recognize :space display properties.
5673 ;; (move-to-column hscroll)
5674 ;; (delete-region (point-min) (point))
5675 (buffer-string)))))))
5676 (add-hook 'mpc-songs-mode-hook 'mdw-mpc-hack-songs)
5678 (eval-after-load "mpc"
5680 (define-key mpc-mode-map "m" 'mdw-mpc-select)
5681 (define-key mpc-mode-map "u" 'mdw-mpc-unselect)
5682 (define-key mpc-mode-map "\177" 'mdw-mpc-unselect-backwards)
5683 (define-key mpc-mode-map "\e\177" 'mdw-mpc-unselect-all)
5684 (define-key mpc-mode-map "n" 'mdw-mpc-next-line)
5685 (define-key mpc-mode-map "p" 'mdw-mpc-previous-line)
5686 (define-key mpc-mode-map "/" 'mpc-songs-search)
5687 (setq mpc-songs-mode-map (make-sparse-keymap))
5688 (set-keymap-parent mpc-songs-mode-map mpc-mode-map)
5689 (define-key mpc-songs-mode-map "l" 'mpc-playlist)
5690 (define-key mpc-songs-mode-map "+" 'mdw-mpc-playlist-add)
5691 (define-key mpc-songs-mode-map "-" 'mdw-mpc-playlist-delete)
5692 (define-key mpc-songs-mode-map "\r" 'mpc-songs-jump-to)))
5694 ;;;--------------------------------------------------------------------------
5695 ;;; Inferior Emacs Lisp.
5697 (setq comint-prompt-read-only t)
5699 (eval-after-load "comint"
5701 (define-key comint-mode-map "\C-w" 'comint-kill-region)
5702 (define-key comint-mode-map [C-S-backspace] 'comint-kill-whole-line)))
5704 (eval-after-load "ielm"
5706 (define-key ielm-map "\C-w" 'comint-kill-region)
5707 (define-key ielm-map [C-S-backspace] 'comint-kill-whole-line)))
5709 ;;;----- That's all, folks --------------------------------------------------
5711 (provide 'dot-emacs)