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 (cl-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 (cl-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 ;; Local variables hacking.
127 (defun run-local-vars-mode-hook ()
128 "Run a hook for the major-mode after local variables have been processed."
129 (run-hooks (intern (concat (symbol-name major-mode)
130 "-local-variables-hook"))))
131 (add-hook 'hack-local-variables-hook 'run-local-vars-mode-hook)
133 ;; Set up the load path convincingly.
135 (dolist (dir (append (and (boundp 'debian-emacs-flavor)
136 (list (concat "/usr/share/"
137 (symbol-name debian-emacs-flavor)
139 (dolist (sub (directory-files dir t))
140 (when (and (file-accessible-directory-p sub)
141 (not (member sub load-path)))
142 (setq load-path (nconc load-path (list sub))))))
144 ;; Is an Emacs library available?
146 (defun library-exists-p (name)
147 "Return non-nil if NAME is an available library.
148 Return non-nil if NAME.el (or NAME.elc) somewhere on the Emacs
149 load path. The non-nil value is the filename we found for the
151 (let ((path load-path) elt (foundp nil))
152 (while (and path (not foundp))
153 (setq elt (car path))
154 (setq path (cdr path))
155 (setq foundp (or (let ((file (concat elt "/" name ".elc")))
156 (and (file-exists-p file) file))
157 (let ((file (concat elt "/" name ".el")))
158 (and (file-exists-p file) file)))))
161 (defun maybe-autoload (symbol file &optional docstring interactivep type)
162 "Set an autoload if the file actually exists."
163 (and (library-exists-p file)
164 (autoload symbol file docstring interactivep type)))
166 (defun mdw-kick-menu-bar (&optional frame)
167 "Regenerate FRAME's menu bar so it doesn't have empty menus."
169 (unless frame (setq frame (selected-frame)))
170 (let ((old (frame-parameter frame 'menu-bar-lines)))
171 (set-frame-parameter frame 'menu-bar-lines 0)
172 (set-frame-parameter frame 'menu-bar-lines old)))
176 (defun mdw-fixup-page-position ()
177 (unless (eq (char-before (point)) ?
\f)
180 (defadvice backward-page (after mdw-fixup compile activate)
181 (mdw-fixup-page-position))
182 (defadvice forward-page (after mdw-fixup compile activate)
183 (mdw-fixup-page-position))
185 ;; Bug fix for markdown-mode, which breaks point positioning during
187 (defadvice markdown-check-change-for-wiki-link
188 (around mdw-save-match activate compile)
189 "Save match data around the `markdown-mode' `after-change-functions' hook."
190 (save-match-data ad-do-it))
192 ;; Bug fix for `bbdb-canonicalize-address': on Emacs 24, `run-hook-with-args'
193 ;; always returns nil, with the result that all email addresses are lost.
194 ;; Replace the function entirely.
195 (defadvice bbdb-canonicalize-address
196 (around mdw-bug-fix activate compile)
197 "Don't use `run-hook-with-args', because that doesn't work."
198 (let ((net (ad-get-arg 0)))
200 ;; Make sure this is a proper hook list.
201 (if (functionp bbdb-canonicalize-net-hook)
202 (setq bbdb-canonicalize-net-hook (list bbdb-canonicalize-net-hook)))
204 ;; Iterate over the hooks until things converge.
207 (let (next (changep nil)
208 hook (hooks bbdb-canonicalize-net-hook))
210 (setq hook (pop hooks))
211 (setq next (funcall hook net))
212 (if (not (equal next net))
215 (setq donep (not changep)))))
216 (setq ad-return-value net)))
218 ;; Transient mark mode hacks.
220 (defadvice exchange-point-and-mark
221 (around mdw-highlight (&optional arg) activate compile)
222 "Maybe don't actually exchange point and mark.
223 If `transient-mark-mode' is on and the mark is inactive, then
224 just activate it. A non-trivial prefix argument will force the
225 usual behaviour. A trivial prefix argument (i.e., just C-u) will
226 activate the mark and temporarily enable `transient-mark-mode' if
228 (cond ((or mark-active
229 (and (not transient-mark-mode) (not arg))
230 (and arg (or (not (consp arg))
231 (not (= (car arg) 4)))))
234 (or transient-mark-mode (setq transient-mark-mode 'only))
235 (set-mark (mark t)))))
237 ;; Functions for sexp diary entries.
239 (defvar mdw-diary-for-org-mode-p nil
240 "Display diary along with the agenda?")
242 (defun mdw-not-org-mode (form)
243 "As FORM, but not in Org mode agenda."
244 (and (not mdw-diary-for-org-mode-p)
247 (defun mdw-weekday (l)
248 "Return non-nil if `date' falls on one of the days of the week in L.
249 L is a list of day numbers (from 0 to 6 for Sunday through to
250 Saturday) or symbols `sunday', `monday', etc. (or a mixture). If
251 the date stored in `date' falls on a listed day, then the
252 function returns non-nil."
253 (let ((d (calendar-day-of-week date)))
255 (memq (nth d '(sunday monday tuesday wednesday
256 thursday friday saturday)) l))))
258 (defun mdw-discordian-date (date)
259 "Return the Discordian calendar date corresponding to DATE.
261 The return value is (YOLD . st-tibs-day) or (YOLD SEASON DAYNUM DOW).
263 The original is by David Pearson. I modified it to produce date components
264 as output rather than a string."
265 (let* ((days ["Sweetmorn" "Boomtime" "Pungenday"
266 "Prickle-Prickle" "Setting Orange"])
267 (months ["Chaos" "Discord" "Confusion"
268 "Bureaucracy" "Aftermath"])
269 (day-count [0 31 59 90 120 151 181 212 243 273 304 334])
270 (year (- (calendar-extract-year date) 1900))
271 (month (1- (calendar-extract-month date)))
272 (day (1- (calendar-extract-day date)))
273 (julian (+ (aref day-count month) day))
274 (dyear (+ year 3066)))
275 (if (and (= month 1) (= day 28))
276 (cons dyear 'st-tibs-day)
278 (aref months (floor (/ julian 73)))
280 (aref days (mod julian 5))))))
282 (defun mdw-diary-discordian-date ()
283 "Convert the date in `date' to a string giving the Discordian date."
284 (let* ((ddate (mdw-discordian-date date))
285 (tail (format "in the YOLD %d" (car ddate))))
286 (if (eq (cdr ddate) 'st-tibs-day)
287 (format "St Tib's Day %s" tail)
288 (let ((season (cadr ddate))
289 (daynum (cl-caddr ddate))
290 (dayname (cl-cadddr ddate)))
291 (format "%s, the %d%s day of %s %s"
294 (let ((ldig (mod daynum 10)))
295 (cond ((= ldig 1) "st")
302 (defun mdw-todo (&optional when)
303 "Return non-nil today, or on WHEN, whichever is later."
304 (let ((w (calendar-absolute-from-gregorian (calendar-current-date)))
305 (d (calendar-absolute-from-gregorian date)))
307 (setq w (max w (calendar-absolute-from-gregorian
309 ((not european-calendar-style)
321 (defcustom diary-time-regexp nil
322 "Regexp matching times in the diary buffer."
325 (defadvice diary-add-to-list (before mdw-trim-leading-space compile activate)
326 "Trim leading space from the diary entry string."
328 (let ((str (ad-get-arg 1))
332 (setq str (cond ((null str) nil)
333 ((string-match "\\(^\\|\n\\)[ \t]+" str)
334 (replace-match "\\1" nil nil str))
335 ((and mdw-diary-for-org-mode-p
336 (string-match (concat
338 "\\(" diary-time-regexp
339 "\\(-" diary-time-regexp "\\)?"
341 "\\(\t[ \t]*\\| [ \t]+\\)")
343 (replace-match "\\1\\2 " nil nil str))
344 ((and (not mdw-diary-for-org-mode-p)
345 (string-match "\\[\\[[^][]*]\\[\\([^][]*\\)]]"
347 (replace-match "\\1" nil nil str))
349 (if (equal str old) (setq done t)))
350 (ad-set-arg 1 str))))
354 (setq glasses-separator "-"
355 glasses-separate-parentheses-p nil
356 glasses-uncapitalize-p t)
358 ;; Rename buffers along with files.
360 (defvar mdw-inhibit-rename-buffer nil
361 "If non-nil, `rename-file' won't rename the buffer visiting the file.")
363 (defmacro mdw-advise-to-inhibit-rename-buffer (function)
364 "Advise FUNCTION to set `mdw-inhibit-rename-buffer' while it runs.
366 This will prevent `rename-file' from renaming the buffer."
367 `(defadvice ,function (around mdw-inhibit-rename-buffer compile activate)
368 "Don't rename the buffer when renaming the underlying file."
369 (let ((mdw-inhibit-rename-buffer t))
371 (mdw-advise-to-inhibit-rename-buffer recode-file-name)
372 (mdw-advise-to-inhibit-rename-buffer set-visited-file-name)
373 (mdw-advise-to-inhibit-rename-buffer backup-buffer)
375 (defadvice rename-file (after mdw-rename-buffers (from to &optional forcep)
377 "If a buffer is visiting the file, rename it to match the new name.
379 Don't do this if `mdw-inhibit-rename-buffer' is non-nil."
380 (unless mdw-inhibit-rename-buffer
381 (let ((buffer (get-file-buffer from)))
383 (let ((to (if (not (string= (file-name-nondirectory to) "")) to
384 (concat to (file-name-nondirectory from)))))
385 (with-current-buffer buffer
386 (set-visited-file-name to nil t)))))))
388 ;;;--------------------------------------------------------------------------
389 ;;; Window management.
391 ;; Width configuration.
393 (defcustom mdw-column-width
394 (string-to-number (or (mdw-config 'emacs-width) "77"))
395 "Width of Emacs columns."
397 (defcustom mdw-text-width mdw-column-width
398 "Expected width of text within columns."
402 ;; Splitting windows.
404 (unless (fboundp 'scroll-bar-columns)
405 (defun scroll-bar-columns (side)
406 (cond ((eq side 'left) 0)
409 (unless (fboundp 'fringe-columns)
410 (defun fringe-columns (side)
411 (cond ((not window-system) 0)
415 (defun mdw-horizontal-window-overhead ()
416 "Computes the horizontal window overhead.
417 This is the number of columns used by fringes, scroll bars and other such
419 (if (not window-system)
422 (dolist (what '(scroll-bar fringe))
423 (dolist (side '(left right))
425 (funcall (intern (concat (symbol-name what) "-columns"))
429 (defun mdw-split-window-horizontally (&optional width)
430 "Split a window horizontally.
431 Without a numeric argument, split the window approximately in
432 half. With a numeric argument WIDTH, allocate WIDTH columns to
433 the left-hand window (if positive) or -WIDTH columns to the
434 right-hand window (if negative). Space for scroll bars and
435 fringes is not taken out of the allowance for WIDTH, unlike
436 \\[split-window-horizontally]."
438 (split-window-horizontally
439 (cond ((null width) nil)
440 ((>= width 0) (+ width (mdw-horizontal-window-overhead)))
441 ((< width 0) width))))
443 (defun mdw-preferred-column-width ()
444 "Return the preferred column width."
445 (if (and window-system (mdw-emacs-version-p 22)) mdw-column-width
446 (1+ mdw-column-width)))
448 (defun mdw-divvy-window (&optional width)
449 "Split a wide window into appropriate widths."
451 (setq width (if width (prefix-numeric-value width)
452 (mdw-preferred-column-width)))
453 (let* ((win (selected-window))
454 (sb-width (mdw-horizontal-window-overhead))
455 (c (/ (+ (window-width) sb-width)
456 (+ width sb-width))))
459 (split-window-horizontally (+ width sb-width))
461 (select-window win)))
463 (defun mdw-frame-width-quantized-p (frame-width column-width)
464 "Return whether the FRAME-WIDTH was chosen specifically for COLUMN-WIDTH."
465 (let ((sb-width (mdw-horizontal-window-overhead)))
466 (zerop (mod (+ frame-width sb-width)
467 (+ column-width sb-width)))))
469 (defun mdw-frame-width-for-columns (columns width)
470 "Return the preferred width for a frame with so many COLUMNS of WIDTH."
471 (let ((sb-width (mdw-horizontal-window-overhead)))
472 (- (* columns (+ width sb-width))
475 (defun mdw-set-frame-width (columns &optional width)
476 "Set the current frame to be the correct width for COLUMNS columns.
478 If WIDTH is non-nil, then it provides the width for the new columns. (This
479 can be set interactively with a prefix argument.)"
480 (interactive "nColumns:
482 (setq width (if width (prefix-numeric-value width)
483 (mdw-preferred-column-width)))
484 (set-frame-width (selected-frame)
485 (mdw-frame-width-for-columns columns width))
486 (mdw-divvy-window width))
488 (defcustom mdw-frame-width-fudge
489 (cond ((<= emacs-major-version 20) 1)
490 ((= emacs-major-version 26) 3)
492 "The number of extra columns to add to the desired frame width.
494 This is sadly necessary because Emacs 26 is broken in this regard."
497 (defcustom mdw-frame-colour-alist
498 '((black . ("#000000" . "#ffffff"))
499 (red . ("#2a0000" . "#ffffff"))
500 (green . ("#002a00" . "#ffffff"))
501 (blue . ("#00002a" . "#ffffff")))
502 "Alist mapping symbol names to (FOREGROUND . BACKGROUND) colour pairs."
503 :type '(alist :key-type symbol :value-type (cons color color)))
505 (defun mdw-set-frame-colour (colour &optional frame)
506 (interactive "xColour name or (FOREGROUND . BACKGROUND) pair:
508 (when (and colour (symbolp colour))
509 (let ((entry (assq colour mdw-frame-colour-alist)))
510 (unless entry (error "Unknown colour `%s'" colour))
511 (setf colour (cdr entry))))
512 (set-frame-parameter frame 'background-color (car colour))
513 (set-frame-parameter frame 'foreground-color (cdr colour)))
515 ;; Window configuration switching.
517 (defvar mdw-current-window-configuration nil
518 "The current window configuration register name, or `nil'.")
520 (defun mdw-switch-window-configuration (register &optional no-save)
521 "Switch make REGISTER be the new current window configuration.
522 If a current window configuration register is established, and
523 NO-SAVE is nil, then save the current window configuration to
526 Signal an error if the new register contains something other than
527 a window configuration. If the register is unset then save the
528 current window configuration to it immediately.
530 With one or three C-u, or an odd numeric prefix argument, set
531 NO-SAVE, so the previous window configuration register is left
534 With two or three C-u, or a prefix argument which is an odd
535 multiple of 2, just clear the record of the current window
536 configuration register, so that the next switch doesn't save the
537 prevailing configuration."
539 (let ((arg current-prefix-arg))
540 (list (if (or (and (consp arg) (= (car arg) 16) (= (car arg) 64))
541 (and (integerp arg) (not (zerop (logand arg 2)))))
543 (register-read-with-preview "Switch to window configuration: "))
544 (or (and (consp arg) (= (car arg) 4) (= (car arg) 64))
545 (and (integerp arg) (not (zerop (logand arg 1))))))))
547 (let ((previous mdw-current-window-configuration)
548 (current-windows (list (current-window-configuration)
550 (register-value (and register (get-register register))))
551 (when (and mdw-current-window-configuration (not no-save))
552 (set-register mdw-current-window-configuration current-windows))
553 (cond ((null register)
554 (setq mdw-current-window-configuration nil)
556 (message "Left window configuration `%c'." previous)
557 (message "Nothing to do!")))
558 ((not (or (null register-value)
559 (and (consp register-value)
560 (window-configuration-p (car register-value))
561 (integer-or-marker-p (cadr register-value))
562 (null (cl-caddr register-value)))))
563 (error "Register `%c' is not a window configuration" register))
565 (cond ((null register-value)
566 (set-register register current-windows)
567 (message "Started new window configuration `%c'."
570 (set-window-configuration (car register-value))
571 (goto-char (cadr register-value))
572 (message "Switched to window configuration `%c'."
574 (setq mdw-current-window-configuration register)))))
576 ;; Don't raise windows unless I say so.
578 (defcustom mdw-inhibit-raise-frame nil
579 "Whether `raise-frame' should do nothing when the frame is mapped."
582 (defadvice raise-frame
583 (around mdw-inhibit (&optional frame) activate compile)
584 "Don't actually do anything if `mdw-inhibit-raise-frame' is true, and the
585 frame is actually mapped on the screen."
586 (if mdw-inhibit-raise-frame
587 (make-frame-visible frame)
590 (defmacro mdw-advise-to-inhibit-raise-frame (function)
591 "Advise the FUNCTION not to raise frames, even if it wants to."
592 `(defadvice ,function
593 (around mdw-inhibit-raise (&rest hunoz) activate compile)
594 "Don't raise the window unless you have to."
595 (let ((mdw-inhibit-raise-frame t))
598 (mdw-advise-to-inhibit-raise-frame select-frame-set-input-focus)
599 (mdw-advise-to-inhibit-raise-frame appt-disp-window)
600 (mdw-advise-to-inhibit-raise-frame mouse-select-window)
602 ;; Window selection for `display-buffer'.
604 (defvar mdw-designated-window nil
605 "The window chosen by `mdw-designate-window', or nil.")
607 (defun mdw-designated-window-display-buffer-function (buffer not-this-window)
608 "Display buffer function to use the designated window."
609 (unless mdw-designated-window (error "No designated window!"))
610 (prog1 mdw-designated-window
611 (with-selected-window mdw-designated-window (switch-to-buffer buffer))
612 (setq mdw-designated-window nil
613 display-buffer-function nil)))
615 (defun mdw-display-buffer-in-designated-window (buffer alist)
616 "Display function to use the designated window."
617 (prog1 mdw-designated-window
618 (when mdw-designated-window
619 (with-selected-window mdw-designated-window
620 (switch-to-buffer buffer nil t)))
621 (setq mdw-designated-window nil)))
623 (defun mdw-designate-window (cancel)
624 "Use the selected window for the next pop-up buffer.
625 With a prefix argument, clear the designated window."
627 (let ((window (selected-window)))
629 (cond (mdw-designated-window
630 (setq mdw-designated-window nil)
631 (unless (mdw-emacs-version-p 24)
632 (setq display-buffer-function nil))
633 (message "Window designation cleared."))
635 (message "No designated window active."))))
636 ((window-dedicated-p window)
637 (error "Window is dedicated to its buffer."))
639 (setq mdw-designated-window window)
640 (unless (mdw-emacs-version-p 24)
641 (setq display-buffer-function
642 #'mdw-designated-window-display-buffer-function))
643 (message "Window designated.")))))
645 (when (mdw-emacs-version-p 24)
646 (setq display-buffer-base-action
647 (let* ((action display-buffer-base-action)
649 (alist (cdr action)))
650 (cons (cons 'mdw-display-buffer-in-designated-window funcs)
653 (defun mdw-clobber-other-windows-showing-buffer (buffer-or-name)
654 "Arrange that no windows on other frames are showing BUFFER-OR-NAME."
655 (interactive "bBuffer: ")
656 (let ((home-frame (selected-frame))
657 (buffer (get-buffer buffer-or-name))
658 (safe-buffer (get-buffer "*scratch*")))
659 (dolist (frame (frame-list))
660 (unless (eq frame home-frame)
661 (dolist (window (window-list frame))
662 (when (eq (window-buffer window) buffer)
663 (set-window-buffer window safe-buffer)))))))
665 (defvar mdw-inhibit-walk-windows nil
666 "If non-nil, then `walk-windows' does nothing.
667 This is used by advice on `switch-to-buffer-other-frame' to inhibit finding
668 buffers in random frames.")
670 (setq display-buffer--other-frame-action
671 '((display-buffer-reuse-window display-buffer-pop-up-frame)
672 (reusable-frames . nil)
673 (inhibit-same-window . t)))
675 (defadvice walk-windows (around mdw-inhibit activate)
676 "If `mdw-inhibit-walk-windows' is non-nil, then do nothing."
677 (and (not mdw-inhibit-walk-windows)
680 (defadvice switch-to-buffer-other-frame
681 (around mdw-always-new-frame activate)
682 "Always make a new frame.
683 Even if an existing window in some random frame looks tempting."
684 (let ((mdw-inhibit-walk-windows t)) ad-do-it))
686 (defadvice display-buffer (before mdw-inhibit-other-frames activate)
687 "Don't try to do anything fancy with other frames.
688 Pretend they don't exist. They might be on other display devices."
691 (setq even-window-sizes nil
692 even-window-heights nil
693 display-buffer-reuse-frames nil)
695 (defvar mdw-fallback-window-alist nil
696 "Alist mapping frames to fallback windows.")
698 (defun mdw-cleanup-fallback-window-alist ()
699 "Remove entries for dead frames and windows from the fallback alist."
701 (cursor mdw-fallback-window-alist))
703 (let* ((assoc (car cursor))
705 (cond ((and (frame-live-p (car assoc))
706 (window-live-p (cdr assoc)))
709 (setq mdw-fallback-window-alist tail))
712 (setq cursor tail)))))
714 (defun mdw-set-fallback-window (cancel)
715 "Prefer the selected window for pop-up buffers in this frame.
716 With a prefix argument, clear the fallback window."
718 (let* ((frame (selected-frame)) (window (selected-window))
719 (assoc (assq (selected-frame) mdw-fallback-window-alist)))
723 (message "Fallback window cleared."))
725 (message "No fallback window active in this frame."))))
726 ((window-dedicated-p window)
727 (error "Window is dedicated to its buffer."))
729 (if assoc (setcdr assoc window)
730 (push (cons frame window) mdw-fallback-window-alist))
731 (message "Fallback window set.")))
732 (mdw-cleanup-fallback-window-alist)))
734 (defun mdw-last-window-in-frame-p (window)
735 "Return whether WINDOW is the last in its frame."
738 (let ((next (window-next-sibling window)))
739 (while (and next (window-minibuffer-p next))
740 (setq next (window-next-sibling next)))
741 (if next (throw 'done nil)))
742 (setq window (window-parent window)))
745 (defun mdw-display-buffer-in-tolerable-window (buffer alist)
746 "Try finding a tolerable window in which to display BUFFER.
747 Begone, foul DWIMmerlaik!
749 This is all totally subject to arbitrary change in the future, but the
750 emphasis is on predictability rather than crazy DWIMmery."
751 (let* ((selected (selected-window)) chosen
752 (fallback (assq (selected-frame) mdw-fallback-window-alist))
753 (full-height-p (window-full-height-p selected))
754 (full-width-p (window-full-width-p selected)))
757 ((and fallback (window-live-p (cdr fallback)))
758 ;; There's a fallback window set for this frame. Use it.
760 (setq chosen (cdr fallback)
762 (display-buffer-record-window 'window chosen buffer))
764 ((and full-height-p full-width-p)
765 ;; We're basically the only window in the frame. If we want to get
766 ;; anywhere, we'll have to split the window.
768 (let ((width (window-width selected))
769 (preferred-width (mdw-preferred-column-width)))
770 (if (and (>= width (mdw-frame-width-for-columns 2 preferred-width))
771 (mdw-frame-width-quantized-p width preferred-width))
772 (setq chosen (split-window-right preferred-width))
773 (setq chosen (split-window-below)))
774 (display-buffer-record-window 'window chosen buffer)))
776 ((mdw-last-window-in-frame-p selected)
777 ;; This is the last window in the frame. I don't think I want to
778 ;; clobber the first window, so rebound and clobber the previous one
779 ;; instead. (This obviously has the same effect if there are only two
780 ;; windows, but seems more useful if there are three.)
782 (setq chosen (previous-window selected 'never nil))
783 (display-buffer-record-window 'reuse chosen buffer))
786 ;; There's another window in front of us. Let's use that one.
787 (setq chosen (next-window selected 'never nil)))
788 (display-buffer-record-window 'reuse chosen buffer))
790 (if (eq chosen selected)
791 (error "Failed to select a different window!"))
794 (with-selected-window chosen (switch-to-buffer buffer)))
797 ;; Hack the display actions so that they do something sensible.
798 (setq display-buffer-fallback-action
799 '((display-buffer--maybe-same-window
800 display-buffer-reuse-window
801 display-buffer-pop-up-window
802 mdw-display-buffer-in-tolerable-window)))
804 ;;;--------------------------------------------------------------------------
805 ;;; Org-mode hacking.
807 (defadvice org-agenda-list (around mdw-preserve-links activate)
808 (let ((mdw-diary-for-org-mode-p t))
811 (defadvice org-bbdb-anniversaries (after mdw-fixup-list compile activate)
812 "Return a string rather than a list."
815 (dolist (e (let ((ee ad-return-value))
816 (if (atom ee) (list ee) ee)))
818 (when anyp (insert ?\n))
821 (setq ad-return-value
822 (and anyp (buffer-string))))))
824 ;; Fighting with Org-mode's evil key maps.
826 (defcustom mdw-evil-keymap-keys
827 '(([S-up] . [?\C-c up])
828 ([S-down] . [?\C-c down])
829 ([S-left] . [?\C-c left])
830 ([S-right] . [?\C-c right])
831 (([M-up] [?\e up]) . [C-up])
832 (([M-down] [?\e down]) . [C-down])
833 (([M-left] [?\e left]) . [C-left])
834 (([M-right] [?\e right]) . [C-right]))
835 "Defines evil keybindings to clobber in `mdw-clobber-evil-keymap'.
836 The value is an alist mapping evil keys (as a list, or singleton)
837 to good keys (in the same form)."
838 :type '(alist :key-type (choice key-sequence (repeat key-sequence))
839 :value-type key-sequence))
841 (defun mdw-clobber-evil-keymap (keymap)
842 "Replace evil key bindings in the KEYMAP.
843 Evil key bindings are defined in `mdw-evil-keymap-keys'."
844 (dolist (entry mdw-evil-keymap-keys)
846 (keys (if (listp (car entry))
849 (replacements (if (listp (cdr entry))
851 (list (cdr entry)))))
854 (setq binding (lookup-key keymap key))
856 (throw 'found nil))))
859 (define-key keymap key nil))
860 (dolist (key replacements)
861 (define-key keymap key binding))))))
863 (defcustom mdw-org-latex-defs
865 "\\documentclass{strayman}
866 \\usepackage[utf8]{inputenc}
867 \\usepackage[palatino, helvetica, courier, maths=cmr]{mdwfonts}
868 \\usepackage{graphicx, tikz, mdwtab, mdwmath, crypto, longtable}"
869 ("\\section{%s}" . "\\section*{%s}")
870 ("\\subsection{%s}" . "\\subsection*{%s}")
871 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
872 ("\\paragraph{%s}" . "\\paragraph*{%s}")
873 ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
874 "Additional LaTeX class definitions."
875 :type '(alist :key-type string
876 :value-type (list string
879 :value-type string))))
881 (setq org-emphasis-regexp-components
882 '("- \t('\"{}" ; prematch
883 "- \t.,:!?;'\")}\\[" ; postmatch
884 " \t\r\n" ; /forbidden/ as border
886 1)) ; maximum newlines
888 (setq org-entities-user
889 ;; NAME LATEX MATHP HTML ASCII LATIN1 UTF8
890 '(("relax" "" nil "" "" "" "")))
892 (eval-after-load "org-latex"
893 '(setq org-export-latex-classes
894 (append mdw-org-latex-defs org-export-latex-classes)))
896 (eval-after-load "ox-latex"
897 '(setq org-latex-classes (append mdw-org-latex-defs org-latex-classes)
898 org-latex-caption-above nil
899 org-latex-default-packages-alist '(("AUTO" "inputenc" t)
907 ("normalem" "ulem" t)
913 "\\tolerance=1000")))
915 (setq org-export-docbook-xslt-proc-command "xsltproc --output %o %s %i"
916 org-export-docbook-xsl-fo-proc-command "fop %i.safe %o"
917 org-export-docbook-xslt-stylesheet
918 "/usr/share/xml/docbook/stylesheet/docbook-xsl/fo/docbook.xsl")
920 ;;;--------------------------------------------------------------------------
921 ;;; Improved compilation machinery.
923 ;; Uprated version of M-x compile.
925 (setq compile-command
926 (let ((ncpu (with-temp-buffer
927 (insert-file-contents "/proc/cpuinfo")
929 (count-matches "^processor\\s-*:"))))
930 (format "nice make -j%d -k" (* 2 ncpu))))
932 (defun mdw-compilation-buffer-name (mode)
933 (concat "*" (downcase mode) ": "
934 (abbreviate-file-name default-directory) "*"))
935 (setq compilation-buffer-name-function 'mdw-compilation-buffer-name)
937 (eval-after-load "compile"
939 (define-key compilation-shell-minor-mode-map "\C-c\M-g" 'recompile)))
941 (defadvice compile (around hack-environment compile activate)
942 "Hack the environment inherited by inferiors in the compilation."
943 (let ((process-environment (copy-tree process-environment)))
944 (setenv "LD_PRELOAD" nil)
947 (defun mdw-compile (command &optional directory comint)
948 "Initiate a compilation COMMAND, maybe in a different DIRECTORY.
949 The DIRECTORY may be nil to not change. If COMINT is t, then
950 start an interactive compilation.
952 Interactively, prompt for the command if the variable
953 `compilation-read-command' is non-nil, or if requested through
954 the prefix argument. Prompt for the directory, and run
955 interactively, if requested through the prefix.
957 Use a prefix of 4, 6, 12, or 14, or type C-u between one and three times, to
958 force prompting for a directory.
960 Use a prefix of 2, 6, 10, or 14, or type C-u three times, to force
961 prompting for the command.
963 Use a prefix of 8, 10, 12, or 14, or type C-u twice or three times,
964 to force interactive compilation."
966 (let* ((prefix (prefix-numeric-value current-prefix-arg))
967 (command (eval compile-command))
968 (dir (and (cl-plusp (logand prefix #x54))
969 (read-directory-name "Compile in directory: "))))
970 (list (if (or compilation-read-command
971 (cl-plusp (logand prefix #x42)))
972 (compilation-read-command command)
975 (cl-plusp (logand prefix #x58)))))
976 (let ((default-directory (or directory default-directory)))
977 (compile command comint)))
981 (defun mdw-find-build-dir (build-file)
983 (let* ((src-dir (file-name-as-directory (expand-file-name ".")))
986 (when (file-exists-p (concat dir build-file))
988 (let ((sub (expand-file-name (file-relative-name src-dir dir)
989 (concat dir "build/"))))
992 (when (file-exists-p (concat sub build-file))
994 (when (string= sub dir) (throw 'give-up nil))
995 (setq sub (file-name-directory (directory-file-name sub))))))
997 (setq dir (file-name-directory
998 (directory-file-name dir))))
999 (throw 'found nil))))))
1001 (defun mdw-flymake-make-init ()
1002 (let ((build-dir (mdw-find-build-dir "Makefile")))
1004 (let ((tmp-src (flymake-init-create-temp-buffer-copy
1005 #'flymake-create-temp-inplace)))
1006 (flymake-get-syntax-check-program-args
1007 tmp-src build-dir t t
1008 #'flymake-get-make-cmdline)))))
1010 (setq flymake-allowed-file-name-masks
1011 '(("\\.\\(?:[cC]\\|cc\\|cpp\\|cxx\\|c\\+\\+\\)\\'"
1012 mdw-flymake-make-init)
1013 ("\\.\\(?:[hH]\\|hh\\|hpp\\|hxx\\|h\\+\\+\\)\\'"
1014 mdw-flymake-master-make-init)
1015 ("\\.p[lm]" flymake-perl-init)))
1017 (setq flymake-mode-map
1018 (let ((map (if (boundp 'flymake-mode-map)
1020 (make-sparse-keymap))))
1021 (define-key map [?\C-c ?\C-f ?\C-p] 'flymake-goto-prev-error)
1022 (define-key map [?\C-c ?\C-f ?\C-n] 'flymake-goto-next-error)
1023 (define-key map [?\C-c ?\C-f ?\C-c] 'flymake-compile)
1024 (define-key map [?\C-c ?\C-f ?\C-k] 'flymake-stop-all-syntax-checks)
1025 (define-key map [?\C-c ?\C-f ?\C-e] 'flymake-popup-current-error-menu)
1028 ;;;--------------------------------------------------------------------------
1029 ;;; Mail and news hacking.
1031 (define-derived-mode mdwmail-mode mail-mode "[mdw] mail"
1032 "Major mode for editing news and mail messages from external programs.
1033 Not much right now. Just support for doing MailCrypt stuff."
1036 (run-hooks 'mail-setup-hook))
1038 (define-key mdwmail-mode-map [?\C-c ?\C-c] 'disabled-operation)
1040 (add-hook 'mdwail-mode-hook
1042 (set-buffer-file-coding-system 'utf-8)
1043 (make-local-variable 'paragraph-separate)
1044 (make-local-variable 'paragraph-start)
1045 (setq paragraph-start
1046 (concat "[ \t]*[-_][-_][-_]+$\\|^-- \\|-----\\|"
1048 (setq paragraph-separate
1049 (concat "[ \t]*[-_][-_][-_]+$\\|^-- \\|-----\\|"
1050 paragraph-separate))))
1052 ;; How to encrypt in mdwmail.
1054 (defun mdwmail-mc-encrypt (&optional recip scm start end from sign)
1056 (setq start (save-excursion
1057 (goto-char (point-min))
1058 (or (search-forward "\n\n" nil t) (point-min)))))
1060 (setq end (point-max)))
1061 (mc-encrypt-generic recip scm start end from sign))
1063 ;; How to sign in mdwmail.
1065 (defun mdwmail-mc-sign (key scm start end uclr)
1067 (setq start (save-excursion
1068 (goto-char (point-min))
1069 (or (search-forward "\n\n" nil t) (point-min)))))
1071 (setq end (point-max)))
1072 (mc-sign-generic key scm start end uclr))
1074 ;; Some signature mangling.
1076 (defun mdwmail-mangle-signature ()
1078 (goto-char (point-min))
1079 (perform-replace "\n-- \n" "\n-- " nil nil nil)))
1080 (add-hook 'mail-setup-hook 'mdwmail-mangle-signature)
1081 (add-hook 'message-setup-hook 'mdwmail-mangle-signature)
1083 ;; Insert my login name into message-ids, so I can score replies.
1085 (defadvice message-unique-id (after mdw-user-name last activate compile)
1086 "Ensure that the user's name appears at the end of the message-id string,
1087 so that it can be used for convenient filtering."
1088 (setq ad-return-value (concat ad-return-value "." (user-login-name))))
1090 ;; Tell my movemail hack where movemail is.
1092 ;; This is needed to shup up warnings about LD_PRELOAD.
1094 (let ((path exec-path))
1096 (let ((try (expand-file-name "movemail" (car path))))
1097 (if (file-executable-p try)
1098 (setenv "REAL_MOVEMAIL" try))
1099 (setq path (cdr path)))))
1101 ;; AUTHINFO GENERIC kludge.
1103 (defcustom nntp-authinfo-generic nil
1104 "Set to the `NNTPAUTH' string to pass on to `authinfo-kludge'.
1106 Use this to arrange for per-server settings."
1107 :type '(choice (const :tag "Use `NNTPAUTH' environment variable" nil)
1111 (defun nntp-open-authinfo-kludge (buffer)
1112 "Open a connection to SERVER using `authinfo-kludge'."
1113 (let ((proc (start-process "nntpd" buffer
1114 "env" (concat "NNTPAUTH="
1115 (or nntp-authinfo-generic
1117 (error "NNTPAUTH unset")))
1118 "authinfo-kludge" nntp-address)))
1120 (nntp-wait-for-string "^\r*200")
1122 (delete-region (point-min) (point))
1125 (eval-after-load "erc"
1126 '(load "~/.ercrc.el"))
1128 ;; Heavy-duty Gnus patching.
1130 (defun mdw-nnimap-transform-headers ()
1131 (goto-char (point-min))
1132 (let (article lines size string)
1135 (while (not (looking-at "\\* [0-9]+ FETCH"))
1136 (delete-region (point) (progn (forward-line 1) (point)))
1139 (goto-char (match-end 0))
1140 ;; Unfold quoted {number} strings.
1141 (while (re-search-forward
1142 "[^]][ (]{\\([0-9]+\\)}\r?\n"
1144 ;; Start of the header section.
1145 (or (re-search-forward "] {[0-9]+}\r?\n" nil t)
1146 ;; Start of the next FETCH.
1147 (re-search-forward "\\* [0-9]+ FETCH" nil t)
1150 (setq size (string-to-number (match-string 1)))
1151 (delete-region (+ (match-beginning 0) 2) (point))
1152 (setq string (buffer-substring (point) (+ (point) size)))
1153 (delete-region (point) (+ (point) size))
1154 (insert (format "%S" (subst-char-in-string ?\n ?\s string)))
1155 ;; [mdw] missing from upstream
1159 (and (re-search-forward "UID \\([0-9]+\\)"
1165 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
1170 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
1171 (let ((structure (ignore-errors
1172 (read (current-buffer)))))
1173 (while (and (consp structure)
1174 (not (atom (car structure))))
1175 (setq structure (car structure)))
1176 (setq lines (if (and
1177 (stringp (car structure))
1178 (equal (upcase (nth 0 structure)) "MESSAGE")
1179 (equal (upcase (nth 1 structure)) "RFC822"))
1181 (nth 7 structure)))))
1182 (delete-region (line-beginning-position) (line-end-position))
1183 (insert (format "211 %s Article retrieved." article))
1186 (insert (format "Chars: %s\n" size)))
1188 (insert (format "Lines: %s\n" lines)))
1189 ;; Most servers have a blank line after the headers, but
1191 (unless (re-search-forward "^\r$\\|^)\r?$" nil t)
1192 (goto-char (point-max)))
1193 (delete-region (line-beginning-position) (line-end-position))
1195 (forward-line 1)))))
1197 (eval-after-load 'nnimap
1198 '(defalias 'nnimap-transform-headers
1199 (symbol-function 'mdw-nnimap-transform-headers)))
1201 (defadvice gnus-other-frame (around mdw-hack-frame-width compile activate)
1202 "Always arrange for mail/news frames to be 80 columns wide."
1203 (let ((default-frame-alist (cons `(width . ,(+ 80 mdw-frame-width-fudge))
1204 (delete* 'width default-frame-alist
1208 ;; Preferred programs.
1210 (setq mailcap-user-mime-data
1211 '(((type . "application/pdf") (viewer . "mupdf %s"))))
1213 ;;;--------------------------------------------------------------------------
1214 ;;; Utility functions.
1216 (or (fboundp 'line-number-at-pos)
1217 (defun line-number-at-pos (&optional pos)
1218 (let ((opoint (or pos (point))) start)
1221 (goto-char (point-min))
1224 (setq start (point))
1227 (1+ (count-lines 1 (point))))))))
1229 (defun mdw-uniquify-alist (&rest alists)
1230 "Return the concatenation of the ALISTS with duplicate elements removed.
1231 The first association with a given key prevails; others are
1232 ignored. The input lists are not modified, although they'll
1233 probably become garbage."
1235 (let ((start-list (cons nil nil)))
1236 (mdw-do-uniquify start-list
1241 (defun mdw-do-uniquify (done end l rest)
1242 "A helper function for mdw-uniquify-alist.
1243 The DONE argument is a list whose first element is `nil'. It
1244 contains the uniquified alist built so far. The leading `nil' is
1245 stripped off at the end of the operation; it's only there so that
1246 DONE always references a cons cell. END refers to the final cons
1247 cell in the DONE list; it is modified in place each time to avoid
1248 the overheads of `append'ing all the time. The L argument is the
1249 alist we're currently processing; the remaining alists are given
1252 ;; There are several different cases to deal with here.
1255 ;; Current list isn't empty. Add the first item to the DONE list if
1256 ;; there's not an item with the same KEY already there.
1257 (l (or (assoc (car (car l)) done)
1259 (setcdr end (cons (car l) nil))
1260 (setq end (cdr end))))
1261 (mdw-do-uniquify done end (cdr l) rest))
1263 ;; The list we were working on is empty. Shunt the next list into the
1264 ;; current list position and go round again.
1265 (rest (mdw-do-uniquify done end (car rest) (cdr rest)))
1267 ;; Everything's done. Remove the leading `nil' from the DONE list and
1268 ;; return it. Finished!
1272 "Insert the current date in a pleasing way."
1274 (insert (save-excursion
1275 (let ((buffer (get-buffer-create "*tmp*")))
1276 (unwind-protect (progn (set-buffer buffer)
1278 (shell-command "date +%Y-%m-%d" t)
1282 (kill-buffer buffer))))))
1284 (defun uuencode (file &optional name)
1285 "UUencodes a file, maybe calling it NAME, into the current buffer."
1286 (interactive "fInput file name: ")
1288 ;; If NAME isn't specified, then guess from the filename.
1292 (or (string-match "[^/]*$" file) 0))))
1293 (print (format "uuencode `%s' `%s'" file name))
1295 ;; Now actually do the thing.
1296 (call-process "uuencode" file t nil name))
1298 (defcustom np-file "~/.np"
1299 "Where the `now-playing' file is."
1303 (defun np (&optional arg)
1304 "Grabs a `now-playing' string."
1308 (goto-char (point-max))
1310 (insert-file-contents np-file)))))
1312 (defun mdw-version-< (ver-a ver-b)
1313 "Answer whether VER-A is strictly earlier than VER-B.
1314 VER-A and VER-B are version numbers, which are strings containing digit
1315 sequences separated by `.'."
1316 (let* ((la (mapcar (lambda (x) (car (read-from-string x)))
1317 (split-string ver-a "\\.")))
1318 (lb (mapcar (lambda (x) (car (read-from-string x)))
1319 (split-string ver-b "\\."))))
1322 (cond ((null la) (throw 'done lb))
1323 ((null lb) (throw 'done nil))
1324 ((< (car la) (car lb)) (throw 'done t))
1325 ((= (car la) (car lb)) (setq la (cdr la) lb (cdr lb)))
1326 (t (throw 'done nil)))))))
1328 (defun mdw-check-autorevert ()
1329 "Sets global-auto-revert-ignore-buffer appropriately for this buffer.
1330 This takes into consideration whether it's been found using
1331 tramp, which seems to get itself into a twist."
1332 (cond ((not (boundp 'global-auto-revert-ignore-buffer))
1334 ((and (buffer-file-name)
1335 (fboundp 'tramp-tramp-file-p)
1336 (tramp-tramp-file-p (buffer-file-name)))
1337 (unless global-auto-revert-ignore-buffer
1338 (setq global-auto-revert-ignore-buffer 'tramp)))
1339 ((eq global-auto-revert-ignore-buffer 'tramp)
1340 (setq global-auto-revert-ignore-buffer nil))))
1342 (defadvice find-file (after mdw-autorevert activate)
1343 (mdw-check-autorevert))
1344 (defadvice write-file (after mdw-autorevert activate)
1345 (mdw-check-autorevert))
1347 (defun mdw-auto-revert ()
1348 "Recheck all of the autorevertable buffers, and update VC modelines."
1350 (let ((auto-revert-check-vc-info t))
1351 (auto-revert-buffers)))
1353 ;;;--------------------------------------------------------------------------
1356 (defadvice dired-maybe-insert-subdir
1357 (around mdw-marked-insertion first activate)
1358 "The DIRNAME may be a list of directory names to insert.
1359 Interactively, if files are marked, then insert all of them.
1360 With a numeric prefix argument, select that many entries near
1361 point; with a non-numeric prefix argument, prompt for listing
1364 (list (dired-get-marked-files nil
1365 (and (integerp current-prefix-arg)
1368 (and current-prefix-arg
1369 (not (integerp current-prefix-arg))
1370 (read-string "Switches for listing: "
1371 (or dired-subdir-switches
1372 dired-actual-switches)))))
1373 (let ((dirs (ad-get-arg 0)))
1374 (dolist (dir (if (listp dirs) dirs (list dirs)))
1378 (defun mdw-dired-run (args &optional syncp)
1379 (interactive (let ((file (dired-get-filename t)))
1380 (list (read-string (format "Arguments for %s: " file))
1381 current-prefix-arg)))
1382 (funcall (if syncp 'shell-command 'async-shell-command)
1383 (concat (shell-quote-argument (dired-get-filename nil))
1386 (defadvice dired-do-flagged-delete
1387 (around mdw-delete-if-prefix-argument activate compile)
1388 (let ((delete-by-moving-to-trash (and (null current-prefix-arg)
1389 delete-by-moving-to-trash)))
1392 (eval-after-load "dired"
1393 '(define-key dired-mode-map "X" 'mdw-dired-run))
1395 ;;;--------------------------------------------------------------------------
1398 (defun mdw-w3m-browse-url (url &optional new-session-p)
1399 "Invoke w3m on the URL in its current window, or at least a different one.
1400 If NEW-SESSION-P, start a new session."
1401 (interactive "sURL: \nP")
1403 (let ((window (selected-window)))
1406 (select-window (or (and (not new-session-p)
1407 (get-buffer-window "*w3m*"))
1409 (if (one-window-p t) (split-window))
1411 (w3m-browse-url url new-session-p))
1412 (select-window window)))))
1414 (eval-after-load 'w3m
1415 '(define-key w3m-mode-map [?\e ?\r] 'w3m-view-this-url-new-session))
1417 (defcustom mdw-good-url-browsers
1418 '(browse-url-mozilla
1420 (w3m . mdw-w3m-browse-url)
1422 "List of good browsers for mdw-good-url-browsers.
1423 Each item is a browser function name, or a cons (CHECK . FUNC).
1424 A symbol FOO stands for (FOO . FOO)."
1425 :type '(repeat (choice function (cons function function))))
1427 (defun mdw-good-url-browser ()
1428 "Return a good URL browser.
1429 Trundle the list of such things, finding the first item for which
1430 CHECK is fboundp, and returning the correponding FUNC."
1431 (let ((bs mdw-good-url-browsers) b check func answer)
1432 (while (and bs (not answer))
1436 (setq check (car b) func (cdr b))
1437 (setq check b func b))
1439 (setq answer func)))
1442 (eval-after-load "w3m-search"
1446 '(("g" "Google" "http://www.google.co.uk/search?q=%s")
1447 ("gd" "Google Directory"
1448 "http://www.google.com/search?cat=gwd/Top&q=%s")
1449 ("gg" "Google Groups" "http://groups.google.com/groups?q=%s")
1450 ("ward" "Ward's wiki" "http://c2.com/cgi/wiki?%s")
1451 ("gi" "Images" "http://images.google.com/images?q=%s")
1453 "http://metalzone.distorted.org.uk/ftp/pub/mirrors/rfc/rfc%s.txt.gz")
1455 "http://en.wikipedia.org/wiki/Special:Search?go=Go&search=%s")
1456 ("imdb" "IMDb" "http://www.imdb.com/Find?%s")
1457 ("nc-wiki" "nCipher wiki"
1458 "http://wiki.ncipher.com/wiki/bin/view/Devel/?topic=%s")
1459 ("map" "Google maps" "http://maps.google.co.uk/maps?q=%s&hl=en")
1460 ("lp" "Launchpad bug by number"
1461 "https://bugs.launchpad.net/bugs/%s")
1462 ("lppkg" "Launchpad bugs by package"
1463 "https://bugs.launchpad.net/%s")
1465 "http://social.msdn.microsoft.com/Search/en-GB/?query=%s&ac=8")
1466 ("debbug" "Debian bug by number"
1467 "http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=%s")
1468 ("debbugpkg" "Debian bugs by package"
1469 "http://bugs.debian.org/cgi-bin/pkgreport.cgi?pkg=%s")
1470 ("ljlogin" "LJ login" "http://www.livejournal.com/login.bml")))
1471 (add-to-list 'w3m-search-engine-alist
1472 (list (cadr item) (cl-caddr item) nil))
1473 (add-to-list 'w3m-uri-replace-alist
1474 (list (concat "\\`" (car item) ":")
1475 'w3m-search-uri-replace
1478 ;;;--------------------------------------------------------------------------
1479 ;;; Paragraph filling.
1481 ;; Useful variables.
1483 (defcustom mdw-fill-prefix nil
1484 "Used by `mdw-line-prefix' and `mdw-fill-paragraph'.
1485 If there's no fill prefix currently set (by the `fill-prefix'
1486 variable) and there's a match from one of the regexps here, it
1487 gets used to set the fill-prefix for the current operation.
1489 The variable is a list of items of the form `PATTERN . PREFIX'; if
1490 the PATTERN matches, the PREFIX is used to set the fill prefix.
1492 A PATTERN is one of the following.
1494 * STRING -- a regular expression, expected to match at point
1495 * (eval . FORM) -- a Lisp form which must evaluate non-nil
1496 * (if COND CONSEQ-PAT ALT-PAT) -- if COND evaluates non-nil, must match
1497 CONSEQ-PAT; otherwise must match ALT-PAT
1498 * (and PATTERN ...) -- must match all of the PATTERNs
1499 * (or PATTERN ...) -- must match at least one PATTERN
1500 * (not PATTERN) -- mustn't match (probably not useful)
1502 A PREFIX is a list of the following kinds of things:
1504 * STRING -- insert a literal string
1505 * (match . N) -- insert the thing matched by bracketed subexpression N
1506 * (pad . N) -- a string of whitespace the same width as subexpression N
1507 * (expr . FORM) -- the result of evaluating FORM
1509 Information about `bracketed subexpressions' comes from the match data,
1510 as modified during matching.")
1512 (make-variable-buffer-local 'mdw-fill-prefix)
1514 (defcustom mdw-hanging-indents
1516 "\\([*o+]\\|-[-#]?\\|[0-9]+\\.\\|\\[[0-9]+\\]\\|([a-zA-Z])\\)"
1519 "Standard regexp matching parts of a hanging indent.
1520 This is mainly useful in `auto-fill-mode'."
1523 ;; Utility functions.
1525 (defun mdw-maybe-tabify (s)
1526 "Tabify or untabify the string S, according to `indent-tabs-mode'."
1527 (let ((tabfun (if indent-tabs-mode #'tabify #'untabify)))
1531 (let ((start (point-min)) (end (point-max)))
1532 (funcall tabfun (point-min) (point-max))
1533 (setq s (buffer-substring (point-min) (1- (point-max)))))))))
1535 (defun mdw-fill-prefix-match-p (pat)
1536 "Return non-nil if PAT matches at the current position."
1537 (cond ((stringp pat) (looking-at pat))
1538 ((not (consp pat)) (error "Unknown pattern item `%S'" pat))
1539 ((eq (car pat) 'eval) (eval (cdr pat)))
1541 (if (or (null (cdr pat))
1543 (null (cl-cdddr pat))
1545 (error "Invalid `if' pattern `%S'" pat))
1546 (mdw-fill-prefix-match-p (if (eval (cadr pat))
1549 ((eq (car pat) 'and)
1550 (let ((pats (cdr pat))
1553 (or (mdw-fill-prefix-match-p (car pats))
1555 (setq pats (cdr pats)))
1558 (let ((pats (cdr pat))
1561 (or (not (mdw-fill-prefix-match-p (car pats)))
1562 (progn (setq ok t) nil)))
1563 (setq pats (cdr pats)))
1565 ((eq (car pat) 'not)
1566 (if (or (null (cdr pat)) (cddr pat))
1567 (error "Invalid `not' pattern `%S'" pat))
1568 (not (mdw-fill-prefix-match-p (car pats))))
1569 (t (error "Unknown pattern form `%S'" pat))))
1571 (defun mdw-maybe-car (p)
1572 "If P is a pair, return (car P), otherwise just return P."
1573 (if (consp p) (car p) p))
1575 (defun mdw-padding (s)
1576 "Return a string the same width as S but made entirely from whitespace."
1577 (let* ((l (length s)) (i 0) (n (make-string l ? )))
1579 (if (= 9 (aref s i))
1584 (defun mdw-do-prefix-match (m)
1585 "Expand a dynamic prefix match element.
1586 See `mdw-fill-prefix' for details."
1587 (cond ((not (consp m)) (format "%s" m))
1588 ((eq (car m) 'match) (match-string (mdw-maybe-car (cdr m))))
1589 ((eq (car m) 'pad) (mdw-padding (match-string
1590 (mdw-maybe-car (cdr m)))))
1591 ((eq (car m) 'eval) (eval (cdr m)))
1594 (defun mdw-examine-fill-prefixes (l)
1595 "Given a list of dynamic fill prefixes, pick one which matches
1596 context and return the static fill prefix to use. Point must be
1597 at the start of a line, and match data must be saved."
1599 (while (cond ((null l) nil)
1600 ((mdw-fill-prefix-match-p (caar l))
1604 (mapcar #'mdw-do-prefix-match
1610 (defun mdw-choose-dynamic-fill-prefix ()
1611 "Work out the dynamic fill prefix based on the variable `mdw-fill-prefix'."
1612 (cond ((and fill-prefix (not (string= fill-prefix ""))) fill-prefix)
1613 ((not mdw-fill-prefix) fill-prefix)
1617 (mdw-examine-fill-prefixes mdw-fill-prefix))))))
1619 (defadvice do-auto-fill (around mdw-dynamic-fill-prefix () activate compile)
1620 "Handle auto-filling, working out a dynamic fill prefix in the
1621 case where there isn't a sensible static one."
1622 (let ((fill-prefix (mdw-choose-dynamic-fill-prefix)))
1625 (defun mdw-fill-paragraph ()
1626 "Fill paragraph, getting a dynamic fill prefix."
1628 (let ((fill-prefix (mdw-choose-dynamic-fill-prefix)))
1629 (fill-paragraph nil)))
1631 (defun mdw-point-within-string-p ()
1632 "Return non-nil if point is within a string."
1633 (let ((state (syntax-ppss)))
1636 (defun mdw-standard-fill-prefix (rx &optional mat)
1637 "Set the dynamic fill prefix, handling standard hanging indents and stuff.
1638 This is just a short-cut for setting the thing by hand, and by
1639 design it doesn't cope with anything approximating a complicated
1641 (setq mdw-fill-prefix
1642 `(((if (mdw-point-within-string-p)
1643 ,(concat "\\(\\s-*\\)" mdw-hanging-indents)
1644 ,(concat rx mdw-hanging-indents))
1646 (pad . ,(or mat 2))))))
1648 ;;;--------------------------------------------------------------------------
1651 ;; Teach PostScript about a condensed variant of Courier. I'm using 85% of
1652 ;; the usual width, which happens to match `mdwfonts', and David Carlisle's
1653 ;; `pslatex'. (Once upon a time, I used 80%, but decided consistency with
1654 ;; `pslatex' was useful.)
1655 (setq ps-user-defined-prologue "
1656 /CourierCondensed /Courier
1657 /CourierCondensed-Bold /Courier-Bold
1658 /CourierCondensed-Oblique /Courier-Oblique
1659 /CourierCondensed-BoldOblique /Courier-BoldOblique
1660 4 { findfont [0.85 0 0 1 0 0] makefont definefont pop } repeat
1663 ;; Hack `ps-print''s settings.
1664 (eval-after-load 'ps-print
1667 ;; Notice that the comment-delimiters should be in italics too.
1668 (cl-pushnew 'font-lock-comment-delimiter-face ps-italic-faces)
1670 ;; Select more suitable colours for the main kinds of tokens. The
1671 ;; colours set on the Emacs faces are chosen for use against a dark
1672 ;; background, and work very badly on white paper.
1673 (ps-extend-face '(font-lock-comment-face "darkgreen" nil italic))
1674 (ps-extend-face '(font-lock-comment-delimiter-face "darkgreen" nil italic))
1675 (ps-extend-face '(font-lock-string-face "RoyalBlue4" nil))
1676 (ps-extend-face '(mdw-punct-face "sienna" nil))
1677 (ps-extend-face '(mdw-number-face "OrangeRed3" nil))
1679 ;; Teach `ps-print' about my condensed varsions of Courier.
1680 (setq ps-font-info-database
1681 (append '((CourierCondensed
1682 (fonts (normal . "CourierCondensed")
1683 (bold . "CourierCondensed-Bold")
1684 (italic . "CourierCondensed-Oblique")
1685 (bold-italic . "CourierCondensed-BoldOblique"))
1687 (line-height . 10.55)
1689 (avg-char-width . 5.1)))
1690 (cl-remove 'CourierCondensed ps-font-info-database
1693 ;; Arrange to strip overlays from the buffer before we print . This will
1694 ;; prevent `flyspell' from interfering with the printout. (It would be less
1695 ;; bad if `ps-print' could merge the `flyspell' overlay face with the
1696 ;; underlying `font-lock' face, but it can't (and that seems hard). So
1697 ;; instead we have this hack.
1699 ;; The basic trick is to copy the relevant text from the buffer being printed
1700 ;; into a temporary buffer and... just print that. The text properties come
1701 ;; with the text and end up in the new buffer, and the overlays get lost
1702 ;; along the way. Only problem is that the headers identifying the file
1703 ;; being printed get confused, so remember the original buffer and reinstate
1704 ;; it when constructing the headers.
1705 (defvar mdw-printing-buffer)
1707 (defadvice ps-generate-header
1708 (around mdw-use-correct-buffer () activate compile)
1709 "Print the correct name of the buffer being printed."
1710 (with-current-buffer mdw-printing-buffer
1713 (defadvice ps-generate
1714 (around mdw-strip-overlays (buffer from to genfunc) activate compile)
1715 "Strip overlays -- in particular, from `flyspell' -- before printout."
1717 (let ((mdw-printing-buffer buffer))
1718 (insert-buffer-substring buffer from to)
1719 (ad-set-arg 0 (current-buffer))
1720 (ad-set-arg 1 (point-min))
1721 (ad-set-arg 2 (point-max))
1724 ;;;--------------------------------------------------------------------------
1725 ;;; Other common declarations.
1727 ;; Common mode settings.
1729 (defcustom mdw-auto-indent t
1730 "Whether to indent automatically after a newline."
1734 (defun mdw-whitespace-mode (&optional arg)
1735 "Turn on/off whitespace mode, but don't highlight trailing space."
1737 (when (and (boundp 'whitespace-style)
1738 (fboundp 'whitespace-mode))
1739 (let ((whitespace-style (remove 'trailing whitespace-style)))
1740 (whitespace-mode arg))
1741 (setq show-trailing-whitespace whitespace-mode)))
1743 (defvar mdw-do-misc-mode-hacking nil)
1745 (defun mdw-misc-mode-config ()
1746 (and mdw-auto-indent
1747 (cond ((eq major-mode 'lisp-mode)
1748 (local-set-key "\C-m" 'mdw-indent-newline-and-indent))
1749 ((derived-mode-p 'slime-repl-mode 'asm-mode 'comint-mode)
1752 (local-set-key "\C-m" 'newline-and-indent))))
1753 (set (make-local-variable 'mdw-do-misc-mode-hacking) t)
1754 (local-set-key [C-return] 'newline)
1755 (make-local-variable 'page-delimiter)
1756 (setq page-delimiter (concat "^" "\f"
1760 "\\(" " " ".*" " " "\\)?"
1764 (setq comment-column 40)
1766 (setq fill-column mdw-text-width)
1767 (flyspell-prog-mode)
1768 (and (fboundp 'gtags-mode)
1770 (if (fboundp 'hs-minor-mode)
1771 (trap (hs-minor-mode t))
1772 (outline-minor-mode t))
1774 (trap (turn-on-font-lock)))
1776 (defun mdw-post-local-vars-misc-mode-config ()
1777 (setq whitespace-line-column mdw-text-width)
1778 (when (and mdw-do-misc-mode-hacking
1779 (not buffer-read-only))
1780 (setq show-trailing-whitespace t)
1781 (mdw-whitespace-mode 1)))
1782 (add-hook 'hack-local-variables-hook 'mdw-post-local-vars-misc-mode-config)
1784 (defmacro mdw-advise-update-angry-fruit-salad (&rest funcs)
1785 `(progn ,@(mapcar (lambda (func)
1787 (after mdw-angry-fruit-salad activate)
1788 (when mdw-do-misc-mode-hacking
1789 (setq show-trailing-whitespace
1790 (not buffer-read-only))
1791 (mdw-whitespace-mode (if buffer-read-only 0 1)))))
1793 (mdw-advise-update-angry-fruit-salad toggle-read-only
1799 (eval-after-load 'gtags
1801 (dolist (key '([mouse-2] [mouse-3]))
1802 (define-key gtags-mode-map key nil))
1803 (define-key gtags-mode-map [C-S-mouse-2] 'gtags-find-tag-by-event)
1804 (define-key gtags-select-mode-map [C-S-mouse-2]
1805 'gtags-select-tag-by-event)
1806 (dolist (map (list gtags-mode-map gtags-select-mode-map))
1807 (define-key map [C-S-mouse-3] 'gtags-pop-stack))))
1809 ;; Backup file handling.
1811 (defcustom mdw-backup-disable-regexps nil
1812 "List of regular expressions: if a file name matches any of
1813 these then the file is not backed up."
1814 :type '(repeat regexp))
1816 (defun mdw-backup-enable-predicate (name)
1817 "[mdw]'s default backup predicate.
1818 Allows a backup if the standard predicate would allow it, and it
1819 doesn't match any of the regular expressions in
1820 `mdw-backup-disable-regexps'."
1821 (and (normal-backup-enable-predicate name)
1822 (let ((answer t) (list mdw-backup-disable-regexps))
1825 (if (string-match (car list) name)
1827 (setq list (cdr list)))
1829 (setq backup-enable-predicate 'mdw-backup-enable-predicate)
1833 (defun mdw-last-one-out-turn-off-the-lights (frame)
1834 "Disconnect from an X display if this was the last frame on that display."
1835 (let ((frame-display (frame-parameter frame 'display)))
1836 (when (and frame-display
1837 (eq window-system 'x)
1838 (not (cl-some (lambda (fr)
1839 (and (not (eq fr frame))
1840 (string= (frame-parameter fr 'display)
1843 (run-with-idle-timer 0 nil #'x-close-connection frame-display))))
1844 (add-hook 'delete-frame-functions 'mdw-last-one-out-turn-off-the-lights)
1846 ;;;--------------------------------------------------------------------------
1847 ;;; Fullscreen-ness.
1849 (defcustom mdw-full-screen-parameters
1850 '((menu-bar-lines . 0)
1851 ;;(vertical-scroll-bars . nil)
1853 "Frame parameters to set when making a frame fullscreen."
1854 :type '(alist :key-type symbol))
1856 (defcustom mdw-full-screen-save
1858 "Extra frame parameters to save when setting fullscreen."
1859 :type '(repeat symbol))
1861 (defun mdw-toggle-full-screen (&optional frame)
1862 "Show the FRAME fullscreen."
1865 (cond ((frame-parameter frame 'fullscreen)
1866 (set-frame-parameter frame 'fullscreen nil)
1867 (modify-frame-parameters
1869 (or (frame-parameter frame 'mdw-full-screen-saved)
1870 (mapcar (lambda (assoc)
1871 (assq (car assoc) default-frame-alist))
1872 mdw-full-screen-parameters))))
1874 (let ((saved (mapcar (lambda (param)
1875 (cons param (frame-parameter frame param)))
1876 (append (mapcar #'car
1877 mdw-full-screen-parameters)
1878 mdw-full-screen-save))))
1879 (set-frame-parameter frame 'mdw-full-screen-saved saved))
1880 (modify-frame-parameters frame mdw-full-screen-parameters)
1881 (set-frame-parameter frame 'fullscreen 'fullboth)))))
1883 ;;;--------------------------------------------------------------------------
1884 ;;; General fontification.
1886 (make-face 'mdw-virgin-face)
1888 (defmacro mdw-define-face (name &rest body)
1889 "Define a face, and make sure it's actually set as the definition."
1893 (copy-face 'mdw-virgin-face ',name)
1894 (defvar ,name ',name)
1895 (put ',name 'face-defface-spec ',body)
1896 (face-spec-set ',name ',body nil)))
1898 (mdw-define-face default
1899 (((type w32)) :family "courier new" :height 85)
1900 (((type x)) :family "6x13" :foundry "trad" :height 130)
1901 (((type color)) :foreground "white" :background "black")
1903 (mdw-define-face fixed-pitch
1904 (((type w32)) :family "courier new" :height 85)
1905 (((type x)) :family "6x13" :foundry "trad" :height 130)
1906 (t :foreground "white" :background "black"))
1907 (mdw-define-face fixed-pitch-serif
1908 (((type w32)) :family "courier new" :height 85 :weight bold)
1909 (((type x)) :family "6x13" :foundry "trad" :height 130 :weight bold)
1910 (t :foreground "white" :background "black" :weight bold))
1911 (mdw-define-face variable-pitch
1912 (((type x)) :family "helvetica" :height 120))
1913 (mdw-define-face region
1914 (((min-colors 64)) :background "grey30")
1915 (((class color)) :background "blue")
1916 (t :inverse-video t))
1917 (mdw-define-face error
1918 (((class color)) :background "red")
1919 (t :inverse-video t))
1920 (mdw-define-face match
1921 (((class color)) :background "blue")
1922 (t :inverse-video t))
1923 (mdw-define-face mc/cursor-face
1924 (((class color)) :background "red")
1925 (t :inverse-video t))
1926 (mdw-define-face minibuffer-prompt
1928 (mdw-define-face mode-line
1929 (((class color)) :foreground "blue" :background "yellow"
1930 :box (:line-width 1 :style released-button))
1931 (t :inverse-video t))
1932 (mdw-define-face mode-line-inactive
1933 (((class color)) :foreground "yellow" :background "blue"
1934 :box (:line-width 1 :style released-button))
1935 (t :inverse-video t))
1936 (mdw-define-face nobreak-space
1938 (t :inherit escape-glyph :underline t))
1939 (mdw-define-face scroll-bar
1940 (t :foreground "black" :background "lightgrey"))
1941 (mdw-define-face fringe
1942 (t :foreground "yellow"))
1943 (mdw-define-face show-paren-match
1944 (((min-colors 64)) :background "darkgreen")
1945 (((class color)) :background "green")
1947 (mdw-define-face show-paren-mismatch
1948 (((class color)) :background "red")
1949 (t :inverse-video t))
1950 (mdw-define-face highlight
1951 (((min-colors 64)) :background "DarkSeaGreen4")
1952 (((class color)) :background "cyan")
1953 (t :inverse-video t))
1955 (mdw-define-face viper-minibuffer-emacs (t nil))
1956 (mdw-define-face viper-minibuffer-insert (t nil))
1957 (mdw-define-face viper-minibuffer-vi (t nil))
1958 (mdw-define-face viper-replace-overlay
1959 (((min-colors 64)) :background "darkred")
1960 (((class color)) :background "red")
1961 (t :inverse-video t))
1962 (mdw-define-face viper-search (t :inherit isearch))
1964 (mdw-define-face compilation-error
1965 (((class color)) :foreground "red" :weight bold)
1967 (mdw-define-face compilation-warning
1968 (((class color)) :foreground "orange" :weight bold)
1970 (mdw-define-face compilation-info
1971 (((class color)) :foreground "green" :weight bold)
1973 (mdw-define-face compilation-line-number
1975 (mdw-define-face compilation-column-number
1976 (((min-colors 64)) :foreground "lightgrey"))
1977 (setq compilation-message-face 'mdw-virgin-face)
1978 (setq compilation-enter-directory-face 'font-lock-comment-face)
1979 (setq compilation-leave-directory-face 'font-lock-comment-face)
1981 (mdw-define-face holiday-face
1982 (t :background "red"))
1983 (mdw-define-face calendar-today-face
1984 (t :foreground "yellow" :weight bold))
1986 (mdw-define-face flyspell-incorrect
1987 (((type x)) :underline (:color "red" :style wave))
1988 (((class color)) :foreground "red" :underline t)
1990 (mdw-define-face flyspell-duplicate
1991 (((type x)) :underline (:color "orange" :style wave))
1992 (((class color)) :foreground "orange" :underline t)
1995 (mdw-define-face comint-highlight-prompt
1997 (mdw-define-face comint-highlight-input
2000 (mdw-define-face Man-underline
2001 (((type tty)) :underline t)
2004 (mdw-define-face ido-subdir
2005 (t :foreground "cyan" :weight bold))
2007 (mdw-define-face dired-directory
2008 (t :foreground "cyan" :weight bold))
2009 (mdw-define-face dired-symlink
2010 (t :foreground "cyan"))
2011 (mdw-define-face dired-perm-write
2014 (mdw-define-face trailing-whitespace
2015 (((class color)) :background "red")
2016 (t :inverse-video t))
2017 (mdw-define-face whitespace-line
2018 (((class color)) :background "darkred")
2019 (t :inverse-video t))
2020 (mdw-define-face mdw-punct-face
2021 (((min-colors 64)) :foreground "burlywood2")
2022 (((class color)) :foreground "yellow"))
2023 (mdw-define-face mdw-number-face
2024 (t :foreground "yellow"))
2025 (mdw-define-face mdw-trivial-face)
2026 (mdw-define-face font-lock-function-name-face
2028 (mdw-define-face font-lock-keyword-face
2030 (mdw-define-face font-lock-constant-face
2032 (mdw-define-face font-lock-builtin-face
2034 (mdw-define-face font-lock-type-face
2035 (t :weight bold :slant italic))
2036 (mdw-define-face font-lock-reference-face
2038 (mdw-define-face font-lock-variable-name-face
2040 (mdw-define-face font-lock-comment-face
2041 (((min-colors 64)) :slant italic :foreground "SeaGreen1")
2042 (((class color)) :foreground "green")
2044 (mdw-define-face font-lock-comment-delimiter-face
2045 (t :inherit font-lock-comment-face))
2046 (mdw-define-face font-lock-string-face
2047 (((min-colors 64)) :foreground "SkyBlue1")
2048 (((class color)) :foreground "cyan")
2050 (mdw-define-face font-lock-doc-face
2051 (t :inherit font-lock-string-face))
2053 (mdw-define-face message-separator
2054 (t :background "red" :foreground "white" :weight bold))
2055 (mdw-define-face message-cited-text
2056 (default :slant italic)
2057 (((min-colors 64)) :foreground "SkyBlue1")
2058 (((class color)) :foreground "cyan"))
2059 (mdw-define-face message-header-cc
2060 (default :slant italic)
2061 (((min-colors 64)) :foreground "SeaGreen1")
2062 (((class color)) :foreground "green"))
2063 (mdw-define-face message-header-newsgroups
2064 (default :slant italic)
2065 (((min-colors 64)) :foreground "SeaGreen1")
2066 (((class color)) :foreground "green"))
2067 (mdw-define-face message-header-subject
2068 (((min-colors 64)) :foreground "SeaGreen1")
2069 (((class color)) :foreground "green"))
2070 (mdw-define-face message-header-to
2071 (((min-colors 64)) :foreground "SeaGreen1")
2072 (((class color)) :foreground "green"))
2073 (mdw-define-face message-header-xheader
2074 (default :slant italic)
2075 (((min-colors 64)) :foreground "SeaGreen1")
2076 (((class color)) :foreground "green"))
2077 (mdw-define-face message-header-other
2078 (default :slant italic)
2079 (((min-colors 64)) :foreground "SeaGreen1")
2080 (((class color)) :foreground "green"))
2081 (mdw-define-face message-header-name
2082 (default :weight bold)
2083 (((min-colors 64)) :foreground "SeaGreen1")
2084 (((class color)) :foreground "green"))
2086 (mdw-define-face which-func
2089 (mdw-define-face gnus-header-name
2090 (default :weight bold)
2091 (((min-colors 64)) :foreground "SeaGreen1")
2092 (((class color)) :foreground "green"))
2093 (mdw-define-face gnus-header-subject
2094 (((min-colors 64)) :foreground "SeaGreen1")
2095 (((class color)) :foreground "green"))
2096 (mdw-define-face gnus-header-from
2097 (((min-colors 64)) :foreground "SeaGreen1")
2098 (((class color)) :foreground "green"))
2099 (mdw-define-face gnus-header-to
2100 (((min-colors 64)) :foreground "SeaGreen1")
2101 (((class color)) :foreground "green"))
2102 (mdw-define-face gnus-header-content
2103 (default :slant italic)
2104 (((min-colors 64)) :foreground "SeaGreen1")
2105 (((class color)) :foreground "green"))
2107 (mdw-define-face gnus-cite-1
2108 (((min-colors 64)) :foreground "SkyBlue1")
2109 (((class color)) :foreground "cyan"))
2110 (mdw-define-face gnus-cite-2
2111 (((min-colors 64)) :foreground "RoyalBlue2")
2112 (((class color)) :foreground "blue"))
2113 (mdw-define-face gnus-cite-3
2114 (((min-colors 64)) :foreground "MediumOrchid")
2115 (((class color)) :foreground "magenta"))
2116 (mdw-define-face gnus-cite-4
2117 (((min-colors 64)) :foreground "firebrick2")
2118 (((class color)) :foreground "red"))
2119 (mdw-define-face gnus-cite-5
2120 (((min-colors 64)) :foreground "burlywood2")
2121 (((class color)) :foreground "yellow"))
2122 (mdw-define-face gnus-cite-6
2123 (((min-colors 64)) :foreground "SeaGreen1")
2124 (((class color)) :foreground "green"))
2125 (mdw-define-face gnus-cite-7
2126 (((min-colors 64)) :foreground "SlateBlue1")
2127 (((class color)) :foreground "cyan"))
2128 (mdw-define-face gnus-cite-8
2129 (((min-colors 64)) :foreground "RoyalBlue2")
2130 (((class color)) :foreground "blue"))
2131 (mdw-define-face gnus-cite-9
2132 (((min-colors 64)) :foreground "purple2")
2133 (((class color)) :foreground "magenta"))
2134 (mdw-define-face gnus-cite-10
2135 (((min-colors 64)) :foreground "DarkOrange2")
2136 (((class color)) :foreground "red"))
2137 (mdw-define-face gnus-cite-11
2138 (t :foreground "grey"))
2140 (mdw-define-face gnus-emphasis-underline
2141 (((type tty)) :underline t)
2144 (mdw-define-face diff-header
2146 (mdw-define-face diff-index
2148 (mdw-define-face diff-file-header
2150 (mdw-define-face diff-hunk-header
2151 (((min-colors 64)) :foreground "SkyBlue1")
2152 (((class color)) :foreground "cyan"))
2153 (mdw-define-face diff-function
2154 (default :weight bold)
2155 (((min-colors 64)) :foreground "SkyBlue1")
2156 (((class color)) :foreground "cyan"))
2157 (mdw-define-face diff-header
2158 (((min-colors 64)) :background "grey10"))
2159 (mdw-define-face diff-added
2160 (((class color)) :foreground "green"))
2161 (mdw-define-face diff-removed
2162 (((class color)) :foreground "red"))
2163 (mdw-define-face diff-context
2165 (mdw-define-face diff-refine-change
2166 (((min-colors 64)) :background "RoyalBlue4")
2168 (mdw-define-face diff-refine-removed
2169 (((min-colors 64)) :background "#500")
2171 (mdw-define-face diff-refine-added
2172 (((min-colors 64)) :background "#050")
2175 (setq ediff-force-faces t)
2176 (mdw-define-face ediff-current-diff-A
2177 (((min-colors 64)) :background "darkred")
2178 (((class color)) :background "red")
2179 (t :inverse-video t))
2180 (mdw-define-face ediff-fine-diff-A
2181 (((min-colors 64)) :background "red3")
2182 (((class color)) :inverse-video t)
2183 (t :inverse-video nil))
2184 (mdw-define-face ediff-even-diff-A
2185 (((min-colors 64)) :background "#300"))
2186 (mdw-define-face ediff-odd-diff-A
2187 (((min-colors 64)) :background "#300"))
2188 (mdw-define-face ediff-current-diff-B
2189 (((min-colors 64)) :background "darkgreen")
2190 (((class color)) :background "magenta")
2191 (t :inverse-video t))
2192 (mdw-define-face ediff-fine-diff-B
2193 (((min-colors 64)) :background "green4")
2194 (((class color)) :inverse-video t)
2195 (t :inverse-video nil))
2196 (mdw-define-face ediff-even-diff-B
2197 (((min-colors 64)) :background "#020"))
2198 (mdw-define-face ediff-odd-diff-B
2199 (((min-colors 64)) :background "#020"))
2200 (mdw-define-face ediff-current-diff-C
2201 (((min-colors 64)) :background "darkblue")
2202 (((class color)) :background "blue")
2203 (t :inverse-video t))
2204 (mdw-define-face ediff-fine-diff-C
2205 (((min-colors 64)) :background "blue1")
2206 (((class color)) :inverse-video t)
2207 (t :inverse-video nil))
2208 (mdw-define-face ediff-even-diff-C
2209 (((min-colors 64)) :background "#004"))
2210 (mdw-define-face ediff-odd-diff-C
2211 (((min-colors 64)) :background "#004"))
2212 (mdw-define-face ediff-current-diff-Ancestor
2213 (((min-colors 64)) :background "#630")
2214 (((class color)) :background "blue")
2215 (t :inverse-video t))
2216 (mdw-define-face ediff-even-diff-Ancestor
2217 (((min-colors 64)) :background "#320"))
2218 (mdw-define-face ediff-odd-diff-Ancestor
2219 (((min-colors 64)) :background "#320"))
2221 (mdw-define-face magit-hash
2222 (((min-colors 64)) :foreground "grey40")
2223 (((class color)) :foreground "blue"))
2224 (mdw-define-face magit-diff-hunk-heading
2225 (((min-colors 64)) :foreground "grey70" :background "grey25")
2226 (((class color)) :foreground "yellow"))
2227 (mdw-define-face magit-diff-hunk-heading-highlight
2228 (((min-colors 64)) :foreground "grey70" :background "grey35")
2229 (((class color)) :foreground "yellow" :background "blue"))
2230 (mdw-define-face magit-diff-added
2231 (((min-colors 64)) :foreground "#ddffdd" :background "#335533")
2232 (((class color)) :foreground "green"))
2233 (mdw-define-face magit-diff-added-highlight
2234 (((min-colors 64)) :foreground "#cceecc" :background "#336633")
2235 (((class color)) :foreground "green" :background "blue"))
2236 (mdw-define-face magit-diff-removed
2237 (((min-colors 64)) :foreground "#ffdddd" :background "#553333")
2238 (((class color)) :foreground "red"))
2239 (mdw-define-face magit-diff-removed-highlight
2240 (((min-colors 64)) :foreground "#eecccc" :background "#663333")
2241 (((class color)) :foreground "red" :background "blue"))
2242 (mdw-define-face magit-blame-heading
2243 (((min-colors 64)) :foreground "white" :background "grey25"
2244 :weight normal :slant normal)
2245 (((class color)) :foreground "white" :background "blue"
2246 :weight normal :slant normal))
2247 (mdw-define-face magit-blame-name
2248 (t :inherit magit-blame-heading :slant italic))
2249 (mdw-define-face magit-blame-date
2250 (((min-colors 64)) :inherit magit-blame-heading :foreground "grey60")
2251 (((class color)) :inherit magit-blame-heading :foreground "cyan"))
2252 (mdw-define-face magit-blame-summary
2253 (t :inherit magit-blame-heading :weight bold))
2255 (mdw-define-face dylan-header-background
2256 (((min-colors 64)) :background "NavyBlue")
2257 (((class color)) :background "blue"))
2259 (mdw-define-face erc-my-nick-face
2260 (t :foreground "yellow" :weight bold))
2261 (mdw-define-face erc-current-nick-face
2262 (t :foreground "yellow" :weight bold))
2263 (mdw-define-face erc-input-face
2264 (t :foreground "yellow"))
2265 (mdw-define-face erc-action-face
2267 (mdw-define-face erc-button
2268 (t :foreground "cyan" :underline t :weight semi-bold))
2270 (mdw-define-face woman-bold
2272 (mdw-define-face woman-italic
2275 (eval-after-load "rst"
2277 (mdw-define-face rst-level-1-face
2278 (t :foreground "SkyBlue1" :weight bold))
2279 (mdw-define-face rst-level-2-face
2280 (t :foreground "SeaGreen1" :weight bold))
2281 (mdw-define-face rst-level-3-face
2283 (mdw-define-face rst-level-4-face
2285 (mdw-define-face rst-level-5-face
2287 (mdw-define-face rst-level-6-face
2290 (mdw-define-face p4-depot-added-face
2291 (t :foreground "green"))
2292 (mdw-define-face p4-depot-branch-op-face
2293 (t :foreground "yellow"))
2294 (mdw-define-face p4-depot-deleted-face
2295 (t :foreground "red"))
2296 (mdw-define-face p4-depot-unmapped-face
2297 (t :foreground "SkyBlue1"))
2298 (mdw-define-face p4-diff-change-face
2299 (t :foreground "yellow"))
2300 (mdw-define-face p4-diff-del-face
2301 (t :foreground "red"))
2302 (mdw-define-face p4-diff-file-face
2303 (t :foreground "SkyBlue1"))
2304 (mdw-define-face p4-diff-head-face
2305 (t :background "grey10"))
2306 (mdw-define-face p4-diff-ins-face
2307 (t :foreground "green"))
2309 (mdw-define-face w3m-anchor-face
2310 (t :foreground "SkyBlue1" :underline t))
2311 (mdw-define-face w3m-arrived-anchor-face
2312 (t :foreground "SkyBlue1" :underline t))
2314 (mdw-define-face whizzy-slice-face
2315 (t :background "grey10"))
2316 (mdw-define-face whizzy-error-face
2317 (t :background "darkred"))
2319 ;; Ellipses used to indicate hidden text (and similar).
2320 (mdw-define-face mdw-ellipsis-face
2321 (((type tty)) :foreground "blue") (t :foreground "grey60"))
2322 (let ((dollar (make-glyph-code ?$ 'mdw-ellipsis-face))
2323 (backslash (make-glyph-code ?\\ 'mdw-ellipsis-face))
2324 (dot (make-glyph-code ?. 'mdw-ellipsis-face))
2325 (bar (make-glyph-code ?| mdw-ellipsis-face)))
2326 (set-display-table-slot standard-display-table 0 dollar)
2327 (set-display-table-slot standard-display-table 1 backslash)
2328 (set-display-table-slot standard-display-table 4
2329 (vector dot dot dot))
2330 (set-display-table-slot standard-display-table 5 bar))
2332 ;;;--------------------------------------------------------------------------
2335 (mdw-define-face mdw-point-overlay-face
2337 (((min-colors 64)) :background "darkblue")
2338 (((class color)) :background "blue")
2339 (((type tty) (class mono)) :inverse-video t))
2341 (defcustom mdw-point-overlay-fringe-display '(vertical-bar . vertical-bar)
2342 "Bitmaps to display in the left and right fringes in the current line."
2343 :type '(cons symbol symbol))
2345 (defun mdw-configure-point-overlay ()
2346 (let ((ov (make-overlay 0 0)))
2347 (overlay-put ov 'priority 0)
2348 (let* ((fringe (or mdw-point-overlay-fringe-display (cons nil nil)))
2349 (left (car fringe)) (right (cdr fringe))
2353 (put-text-property 0 1 'display `(left-fringe ,left) ss)
2354 (setq s (concat s ss))))
2357 (put-text-property 0 1 'display `(right-fringe ,right) ss)
2358 (setq s (concat s ss))))
2359 (when (or left right)
2360 (overlay-put ov 'before-string s)))
2361 (overlay-put ov 'face 'mdw-point-overlay-face)
2365 (defvar mdw-point-overlay (mdw-configure-point-overlay)
2366 "An overlay used for showing where point is in the selected window.")
2367 (defun mdw-reconfigure-point-overlay ()
2369 (setq mdw-point-overlay (mdw-configure-point-overlay)))
2371 (defun mdw-remove-point-overlay ()
2372 "Remove the current-point overlay."
2373 (delete-overlay mdw-point-overlay))
2375 (defun mdw-update-point-overlay ()
2376 "Mark the current point position with an overlay."
2377 (if (not mdw-point-overlay-mode)
2378 (mdw-remove-point-overlay)
2379 (overlay-put mdw-point-overlay 'window (selected-window))
2380 (move-overlay mdw-point-overlay
2381 (line-beginning-position)
2382 (+ (line-end-position) 1))))
2384 (defvar mdw-point-overlay-buffers nil
2385 "List of buffers using `mdw-point-overlay-mode'.")
2387 (define-minor-mode mdw-point-overlay-mode
2388 "Indicate current line with an overlay."
2390 (let ((buffer (current-buffer)))
2391 (setq mdw-point-overlay-buffers
2392 (cl-mapcan (lambda (buf)
2393 (if (and (buffer-live-p buf)
2394 (not (eq buf buffer)))
2396 mdw-point-overlay-buffers))
2397 (if mdw-point-overlay-mode
2398 (setq mdw-point-overlay-buffers
2399 (cons buffer mdw-point-overlay-buffers))))
2400 (cond (mdw-point-overlay-buffers
2401 (add-hook 'pre-command-hook 'mdw-remove-point-overlay)
2402 (add-hook 'post-command-hook 'mdw-update-point-overlay))
2404 (mdw-remove-point-overlay)
2405 (remove-hook 'pre-command-hook 'mdw-remove-point-overlay)
2406 (remove-hook 'post-command-hook 'mdw-update-point-overlay))))
2408 (define-globalized-minor-mode mdw-global-point-overlay-mode
2409 mdw-point-overlay-mode
2410 (lambda () (if (not (minibufferp)) (mdw-point-overlay-mode t))))
2412 (defvar mdw-terminal-title-alist nil)
2413 (defun mdw-update-terminal-title ()
2414 (when (let ((term (frame-parameter nil 'tty-type)))
2415 (and term (string-match "^xterm" term)))
2416 (let* ((tty (frame-parameter nil 'tty))
2417 (old (assoc tty mdw-terminal-title-alist))
2418 (new (format-mode-line frame-title-format)))
2419 (unless (and old (equal (cdr old) new))
2420 (if old (rplacd old new)
2421 (setq mdw-terminal-title-alist
2422 (cons (cons tty new) mdw-terminal-title-alist)))
2423 (send-string-to-terminal (concat "\e]2;" new "\e\\"))))))
2425 (add-hook 'post-command-hook 'mdw-update-terminal-title)
2427 ;;;--------------------------------------------------------------------------
2430 (defvar mdw-ediff-previous-windows)
2431 (defun mdw-ediff-setup ()
2432 (setq mdw-ediff-previous-windows (current-window-configuration)))
2433 (defun mdw-ediff-suspend-or-quit ()
2434 (set-window-configuration mdw-ediff-previous-windows))
2435 (add-hook 'ediff-before-setup-hook 'mdw-ediff-setup)
2436 (add-hook 'ediff-quit-hook 'mdw-ediff-suspend-or-quit t)
2437 (add-hook 'ediff-suspend-hook 'mdw-ediff-suspend-or-quit t)
2439 ;;;--------------------------------------------------------------------------
2440 ;;; C programming configuration.
2442 ;; Make C indentation nice.
2444 (defun mdw-c-lineup-arglist (langelem)
2445 "Hack for DWIMmery in c-lineup-arglist."
2447 (c-block-in-arglist-dwim (c-langelem-2nd-pos c-syntactic-element)))
2449 (c-lineup-arglist langelem)))
2451 (defun mdw-c-indent-extern-mumble (langelem)
2452 "Indent `extern \"...\" {' lines."
2454 (back-to-indentation)
2456 "\\s-*\\<extern\\>\\s-*\"\\([^\\\\\"]+\\|\\.\\)*\"\\s-*{")
2460 (defun mdw-c-indent-arglist-nested (langelem)
2461 "Indent continued argument lists.
2462 If we've nested more than one argument list, then only introduce a single
2463 indentation anyway."
2464 (let ((context c-syntactic-context)
2465 (pos (c-langelem-2nd-pos c-syntactic-element))
2466 (should-indent-p t))
2468 (eq (caar context) 'arglist-cont-nonempty))
2469 (when (and (= (cl-caddr (pop context)) pos)
2471 (memq (caar context) '(arglist-intro
2472 arglist-cont-nonempty)))
2473 (setq should-indent-p nil)))
2474 (if should-indent-p '+ 0)))
2476 (defvar mdw-define-c-styles-hook nil
2477 "Hook run when `cc-mode' starts up to define styles.")
2479 (defun mdw-merge-style-alists (first second)
2481 (dolist (item first)
2482 (let ((key (car item)) (value (cdr item)))
2483 (if (let* ((key-name (symbol-name key))
2484 (key-len (length key-name)))
2486 (string= (substring key-name (- key-len 6)) "-alist")))
2488 (mdw-merge-style-alists value
2489 (cdr (assoc key second))))
2491 (push item output))))
2492 (dolist (item second)
2493 (unless (assoc (car item) first)
2494 (push item output)))
2497 (cl-defmacro mdw-define-c-style (name (&optional parent) &rest assocs)
2498 "Define a C style, called NAME (a symbol) based on PARENT, setting ASSOCs.
2499 A function, named `mdw-define-c-style/NAME', is defined to actually install
2500 the style using `c-add-style', and added to the hook
2501 `mdw-define-c-styles-hook'. If CC Mode is already loaded, then the style is
2503 (declare (indent defun))
2504 (let* ((name-string (symbol-name name))
2505 (var (intern (concat "mdw-c-style/" name-string)))
2506 (func (intern (concat "mdw-define-c-style/" name-string))))
2511 (let ((parent-list (intern (concat "mdw-c-style/"
2512 (symbol-name parent)))))
2513 `(mdw-merge-style-alists ',assocs ,parent-list))))
2514 (defun ,func () (c-add-style ,name-string ,var))
2515 (and (featurep 'cc-mode) (,func))
2516 (add-hook 'mdw-define-c-styles-hook ',func)
2519 (eval-after-load "cc-mode"
2520 '(run-hooks 'mdw-define-c-styles-hook))
2522 (mdw-define-c-style mdw-c ()
2523 (c-basic-offset . 2)
2524 (comment-column . 40)
2525 (c-class-key . "class")
2526 (c-backslash-column . 72)
2527 (c-label-minimum-indentation . 0)
2528 (c-offsets-alist (substatement-open . (add 0 c-indent-one-line-block))
2529 (defun-open . (add 0 c-indent-one-line-block))
2530 (arglist-cont-nonempty . mdw-c-lineup-arglist)
2531 (topmost-intro . mdw-c-indent-extern-mumble)
2532 (cpp-define-intro . 0)
2534 (inextern-lang . [0])
2540 (statement-cont . +)
2541 (statement-case-intro . +)))
2543 (mdw-define-c-style mdw-trustonic-c (mdw-c)
2544 (c-basic-offset . 4)
2545 (c-offsets-alist (access-label . -2)))
2547 (mdw-define-c-style mdw-trustonic-alec-c (mdw-trustonic-c)
2548 (comment-column . 0)
2549 (c-indent-comment-alist (anchored-comment . (column . 0))
2550 (end-block . (space . 1))
2551 (cpp-end-block . (space . 1))
2552 (other . (space . 1)))
2553 (c-offsets-alist (arglist-cont-nonempty . mdw-c-indent-arglist-nested)))
2555 (defun mdw-set-default-c-style (modes style)
2556 "Update the default CC Mode style for MODES to be STYLE.
2558 MODES may be a list of major mode names or a singleton. STYLE is a style
2560 (let ((modes (if (listp modes) modes (list modes)))
2561 (style (symbol-name style)))
2562 (setq c-default-style
2563 (append (mapcar (lambda (mode)
2566 (cl-remove-if (lambda (assoc)
2567 (memq (car assoc) modes))
2568 (if (listp c-default-style)
2571 c-default-style))))))))
2572 (setq c-default-style "mdw-c")
2574 (mdw-set-default-c-style '(c-mode c++-mode) 'mdw-c)
2576 (defvar mdw-c-comment-fill-prefix
2577 `((,(concat "\\([ \t]*/?\\)"
2580 "\\([A-Za-z]+:[ \t]*\\)?"
2581 mdw-hanging-indents)
2582 (pad . 1) (match . 2) (pad . 3) (pad . 4) (pad . 5)))
2583 "Fill prefix matching C comments (both kinds).")
2585 (defun mdw-fontify-c-and-c++ ()
2587 ;; Fiddle with some syntax codes.
2588 (modify-syntax-entry ?* ". 23")
2589 (modify-syntax-entry ?/ ". 124b")
2590 (modify-syntax-entry ?\n "> b")
2593 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2595 ;; Now define things to be fontified.
2596 (make-local-variable 'font-lock-keywords)
2598 (mdw-regexps "alignas" ;C11 macro, C++11
2600 "and" ;C++, C95 macro
2601 "and_eq" ;C++, C95 macro
2602 "asm" ;K&R, C++, GCC
2603 "atomic" ;C11 macro, C++11 template type
2605 "bitand" ;C++, C95 macro
2606 "bitor" ;C++, C95 macro
2607 "bool" ;C++, C99 macro
2612 "char16_t" ;C++11, C11 library type
2613 "char32_t" ;C++11, C11 library type
2615 "complex" ;C99 macro, C++ template type
2616 "compl" ;C++, C95 macro
2620 "continue" ;K&R, C89
2622 "defined" ;C89 preprocessor
2629 ;; "entry" ;K&R -- never used
2640 "imaginary" ;C99 macro
2641 "inline" ;C++, C99, GCC
2648 "noreturn" ;C11 macro
2649 "not" ;C++, C95 macro
2650 "not_eq" ;C++, C95 macro
2653 "or" ;C++, C95 macro
2654 "or_eq" ;C++, C95 macro
2658 "register" ;K&R, C89
2659 "reinterpret_cast" ;C++
2666 "static_assert" ;C11 macro, C++11
2673 "thread_local" ;C11 macro, C++11
2679 "unsigned" ;K&R, C89
2684 "wchar_t" ;C++, C89 library type
2686 "xor" ;C++, C95 macro
2687 "xor_eq" ;C++, C95 macro
2696 "_Pragma" ;C99 preprocessor
2697 "_Static_assert" ;C11
2698 "_Thread_local" ;C11
2701 "__attribute__" ;GCC
2704 "__extension__" ;GCC
2714 (mdw-regexps "false" ;C++, C99 macro
2716 "true" ;C++, C99 macro
2718 (preprocessor-keywords
2719 (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
2720 "ident" "if" "ifdef" "ifndef" "import" "include"
2721 "line" "pragma" "unassert" "undef" "warning"))
2723 (mdw-regexps "class" "defs" "encode" "end" "implementation"
2724 "interface" "private" "protected" "protocol" "public"
2727 (setq font-lock-keywords
2730 ;; Fontify include files as strings.
2731 (list (concat "^[ \t]*\\#[ \t]*"
2732 "\\(include\\|import\\)"
2733 "[ \t]*\\(<[^>]+>?\\)")
2734 '(2 font-lock-string-face))
2736 ;; Preprocessor directives are `references'?.
2737 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
2738 preprocessor-keywords
2739 "\\)\\>\\|[0-9]+\\|$\\)\\)")
2740 '(1 font-lock-keyword-face))
2742 ;; Handle the keywords defined above.
2743 (list (concat "@\\<\\(" objc-keywords "\\)\\>")
2744 '(0 font-lock-keyword-face))
2746 (list (concat "\\<\\(" c-keywords "\\)\\>")
2747 '(0 font-lock-keyword-face))
2749 (list (concat "\\<\\(" c-builtins "\\)\\>")
2750 '(0 font-lock-variable-name-face))
2752 ;; Handle numbers too.
2754 ;; This looks strange, I know. It corresponds to the
2755 ;; preprocessor's idea of what a number looks like, rather than
2756 ;; anything sensible.
2757 (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
2758 "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
2759 '(0 mdw-number-face))
2761 ;; And anything else is punctuation.
2762 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2763 '(0 mdw-punct-face))))))
2765 (define-derived-mode sod-mode c-mode "Sod"
2766 "Major mode for editing Sod code.")
2767 (push '("\\.sod$" . sod-mode) auto-mode-alist)
2769 (dolist (hook '(c-mode-hook objc-mode-hook c++-mode-hook))
2770 (add-hook hook 'mdw-misc-mode-config t)
2771 (add-hook hook 'mdw-fontify-c-and-c++ t))
2773 ;;;--------------------------------------------------------------------------
2776 (define-derived-mode apcalc-mode c-mode "AP Calc"
2777 "Major mode for editing Calc code.")
2779 (defun mdw-fontify-apcalc ()
2781 ;; Fiddle with some syntax codes.
2782 (modify-syntax-entry ?* ". 23")
2783 (modify-syntax-entry ?/ ". 14")
2786 (setq comment-start "/* ")
2787 (setq comment-end " */")
2788 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2790 ;; Now define things to be fontified.
2791 (make-local-variable 'font-lock-keywords)
2793 (mdw-regexps "break" "case" "cd" "continue" "define" "default"
2794 "do" "else" "exit" "for" "global" "goto" "help" "if"
2795 "local" "mat" "obj" "print" "quit" "read" "return"
2796 "show" "static" "switch" "while" "write")))
2798 (setq font-lock-keywords
2801 ;; Handle the keywords defined above.
2802 (list (concat "\\<\\(" c-keywords "\\)\\>")
2803 '(0 font-lock-keyword-face))
2805 ;; Handle numbers too.
2807 ;; This looks strange, I know. It corresponds to the
2808 ;; preprocessor's idea of what a number looks like, rather than
2809 ;; anything sensible.
2810 (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
2811 "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
2812 '(0 mdw-number-face))
2814 ;; And anything else is punctuation.
2815 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2816 '(0 mdw-punct-face))))))
2819 (add-hook 'apcalc-mode-hook 'mdw-misc-mode-config t)
2820 (add-hook 'apcalc-mode-hook 'mdw-fontify-apcalc t))
2822 ;;;--------------------------------------------------------------------------
2823 ;;; Java programming configuration.
2825 ;; Make indentation nice.
2827 (mdw-define-c-style mdw-java ()
2828 (c-basic-offset . 2)
2829 (c-backslash-column . 72)
2830 (c-offsets-alist (substatement-open . 0)
2835 (statement-case-intro . +)))
2836 (mdw-set-default-c-style 'java-mode 'mdw-java)
2838 ;; Declare Java fontification style.
2840 (defun mdw-fontify-java ()
2842 ;; Fiddle with some syntax codes.
2843 (modify-syntax-entry ?@ ".")
2844 (modify-syntax-entry ?@ "." font-lock-syntax-table)
2847 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2849 ;; Now define things to be fontified.
2850 (make-local-variable 'font-lock-keywords)
2851 (let ((java-keywords
2852 (mdw-regexps "abstract" "assert"
2853 "boolean" "break" "byte"
2854 "case" "catch" "char" "class" "const" "continue"
2855 "default" "do" "double"
2856 "else" "enum" "extends"
2857 "final" "finally" "float" "for"
2859 "if" "implements" "import" "instanceof" "int"
2863 "package" "private" "protected" "public"
2865 "short" "static" "strictfp" "switch" "synchronized"
2866 "throw" "throws" "transient" "try"
2871 (mdw-regexps "false" "null" "super" "this" "true")))
2873 (setq font-lock-keywords
2876 ;; Handle the keywords defined above.
2877 (list (concat "\\<\\(" java-keywords "\\)\\>")
2878 '(0 font-lock-keyword-face))
2880 ;; Handle the magic builtins defined above.
2881 (list (concat "\\<\\(" java-builtins "\\)\\>")
2882 '(0 font-lock-variable-name-face))
2884 ;; Handle numbers too.
2886 ;; The following isn't quite right, but it's close enough.
2887 (list (concat "\\<\\("
2888 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2889 "[0-9]+\\(\\.[0-9]*\\)?"
2890 "\\([eE][-+]?[0-9]+\\)?\\)"
2892 '(0 mdw-number-face))
2894 ;; And anything else is punctuation.
2895 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2896 '(0 mdw-punct-face))))))
2899 (add-hook 'java-mode-hook 'mdw-misc-mode-config t)
2900 (add-hook 'java-mode-hook 'mdw-fontify-java t))
2902 ;;;--------------------------------------------------------------------------
2903 ;;; Javascript programming configuration.
2905 (defun mdw-javascript-style ()
2906 (setq js-indent-level 2)
2907 (setq js-expr-indent-offset 0))
2909 (defun mdw-fontify-javascript ()
2912 (mdw-javascript-style)
2913 (setq js-auto-indent-flag t)
2915 ;; Now define things to be fontified.
2916 (make-local-variable 'font-lock-keywords)
2917 (let ((javascript-keywords
2918 (mdw-regexps "abstract" "boolean" "break" "byte" "case" "catch"
2919 "char" "class" "const" "continue" "debugger" "default"
2920 "delete" "do" "double" "else" "enum" "export" "extends"
2921 "final" "finally" "float" "for" "function" "goto" "if"
2922 "implements" "import" "in" "instanceof" "int"
2923 "interface" "let" "long" "native" "new" "package"
2924 "private" "protected" "public" "return" "short"
2925 "static" "super" "switch" "synchronized" "throw"
2926 "throws" "transient" "try" "typeof" "var" "void"
2927 "volatile" "while" "with" "yield"))
2928 (javascript-builtins
2929 (mdw-regexps "false" "null" "undefined" "Infinity" "NaN" "true"
2930 "arguments" "this")))
2932 (setq font-lock-keywords
2935 ;; Handle the keywords defined above.
2936 (list (concat "\\_<\\(" javascript-keywords "\\)\\_>")
2937 '(0 font-lock-keyword-face))
2939 ;; Handle the predefined builtins defined above.
2940 (list (concat "\\_<\\(" javascript-builtins "\\)\\_>")
2941 '(0 font-lock-variable-name-face))
2943 ;; Handle numbers too.
2945 ;; The following isn't quite right, but it's close enough.
2946 (list (concat "\\_<\\("
2947 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2948 "[0-9]+\\(\\.[0-9]*\\)?"
2949 "\\([eE][-+]?[0-9]+\\)?\\)"
2951 '(0 mdw-number-face))
2953 ;; And anything else is punctuation.
2954 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2955 '(0 mdw-punct-face))))))
2958 (add-hook 'js-mode-hook 'mdw-misc-mode-config t)
2959 (add-hook 'js-mode-hook 'mdw-fontify-javascript t))
2961 ;;;--------------------------------------------------------------------------
2962 ;;; Scala programming configuration.
2964 (defun mdw-fontify-scala ()
2967 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2969 ;; Define things to be fontified.
2970 (make-local-variable 'font-lock-keywords)
2971 (let ((scala-keywords
2972 (mdw-regexps "abstract" "case" "catch" "class" "def" "do" "else"
2973 "extends" "final" "finally" "for" "forSome" "if"
2974 "implicit" "import" "lazy" "match" "new" "object"
2975 "override" "package" "private" "protected" "return"
2976 "sealed" "throw" "trait" "try" "type" "val"
2977 "var" "while" "with" "yield"))
2979 (mdw-regexps "false" "null" "super" "this" "true"))
2980 (punctuation "[-!%^&*=+:@#~/?\\|`]"))
2982 (setq font-lock-keywords
2985 ;; Magical identifiers between backticks.
2986 (list (concat "`\\([^`]+\\)`")
2987 '(1 font-lock-variable-name-face))
2989 ;; Handle the keywords defined above.
2990 (list (concat "\\_<\\(" scala-keywords "\\)\\_>")
2991 '(0 font-lock-keyword-face))
2993 ;; Handle the constants defined above.
2994 (list (concat "\\_<\\(" scala-constants "\\)\\_>")
2995 '(0 font-lock-variable-name-face))
2997 ;; Magical identifiers between backticks.
2998 (list (concat "`\\([^`]+\\)`")
2999 '(1 font-lock-variable-name-face))
3001 ;; Handle numbers too.
3003 ;; As usual, not quite right.
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 everything else is punctuation.
3012 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3013 '(0 mdw-punct-face)))
3015 font-lock-syntactic-keywords
3018 ;; Single quotes around characters. But not when used to quote
3019 ;; symbol names. Ugh.
3020 (list (concat "\\('\\)"
3022 "\\|" "\\\\" "\\(" "\\\\\\\\" "\\)*"
3023 "u+" "[0-9a-fA-F]\\{4\\}"
3024 "\\|" "\\\\" "[0-7]\\{1,3\\}"
3025 "\\|" "\\\\" "." "\\)"
3031 (add-hook 'scala-mode-hook 'mdw-misc-mode-config t)
3032 (add-hook 'scala-mode-hook 'mdw-fontify-scala t))
3034 ;;;--------------------------------------------------------------------------
3035 ;;; C# programming configuration.
3037 ;; Make indentation nice.
3039 (mdw-define-c-style mdw-csharp ()
3040 (c-basic-offset . 2)
3041 (c-backslash-column . 72)
3042 (c-offsets-alist (substatement-open . 0)
3047 (statement-case-intro . +)))
3048 (mdw-set-default-c-style 'csharp-mode 'mdw-csharp)
3050 ;; Declare C# fontification style.
3052 (defun mdw-fontify-csharp ()
3055 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
3057 ;; Now define things to be fontified.
3058 (make-local-variable 'font-lock-keywords)
3059 (let ((csharp-keywords
3060 (mdw-regexps "abstract" "as" "bool" "break" "byte" "case" "catch"
3061 "char" "checked" "class" "const" "continue" "decimal"
3062 "default" "delegate" "do" "double" "else" "enum"
3063 "event" "explicit" "extern" "finally" "fixed" "float"
3064 "for" "foreach" "goto" "if" "implicit" "in" "int"
3065 "interface" "internal" "is" "lock" "long" "namespace"
3066 "new" "object" "operator" "out" "override" "params"
3067 "private" "protected" "public" "readonly" "ref"
3068 "return" "sbyte" "sealed" "short" "sizeof"
3069 "stackalloc" "static" "string" "struct" "switch"
3070 "throw" "try" "typeof" "uint" "ulong" "unchecked"
3071 "unsafe" "ushort" "using" "virtual" "void" "volatile"
3075 (mdw-regexps "base" "false" "null" "this" "true")))
3077 (setq font-lock-keywords
3080 ;; Handle the keywords defined above.
3081 (list (concat "\\<\\(" csharp-keywords "\\)\\>")
3082 '(0 font-lock-keyword-face))
3084 ;; Handle the magic builtins defined above.
3085 (list (concat "\\<\\(" csharp-builtins "\\)\\>")
3086 '(0 font-lock-variable-name-face))
3088 ;; Handle numbers too.
3090 ;; The following isn't quite right, but it's close enough.
3091 (list (concat "\\<\\("
3092 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
3093 "[0-9]+\\(\\.[0-9]*\\)?"
3094 "\\([eE][-+]?[0-9]+\\)?\\)"
3096 '(0 mdw-number-face))
3098 ;; And anything else is punctuation.
3099 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3100 '(0 mdw-punct-face))))))
3102 (define-derived-mode csharp-mode java-mode "C#"
3103 "Major mode for editing C# code.")
3105 (add-hook 'csharp-mode-hook 'mdw-fontify-csharp t)
3107 ;;;--------------------------------------------------------------------------
3108 ;;; F# programming configuration.
3110 (setq fsharp-indent-offset 2)
3112 (defun mdw-fontify-fsharp ()
3114 (let ((punct "=<>+-*/|&%!@?"))
3115 (cl-do ((i 0 (1+ i)))
3116 ((>= i (length punct)))
3117 (modify-syntax-entry (aref punct i) ".")))
3119 (modify-syntax-entry ?_ "_")
3120 (modify-syntax-entry ?( "(")
3121 (modify-syntax-entry ?) ")")
3123 (setq indent-tabs-mode nil)
3125 (let ((fsharp-keywords
3126 (mdw-regexps "abstract" "and" "as" "assert" "atomic"
3128 "checked" "class" "component" "const" "constraint"
3129 "constructor" "continue"
3130 "default" "delegate" "do" "done" "downcast" "downto"
3131 "eager" "elif" "else" "end" "exception" "extern"
3132 "finally" "fixed" "for" "fori" "fun" "function"
3135 "if" "in" "include" "inherit" "inline" "interface"
3138 "match" "measure" "member" "method" "mixin" "module"
3141 "object" "of" "open" "or" "override"
3142 "parallel" "params" "private" "process" "protected"
3144 "rec" "recursive" "return"
3145 "sealed" "sig" "static" "struct"
3146 "tailcall" "then" "to" "trait" "try" "type"
3148 "val" "virtual" "void" "volatile"
3149 "when" "while" "with"
3153 (mdw-regexps "asr" "land" "lor" "lsl" "lsr" "lxor" "mod"
3154 "base" "false" "null" "true"))
3157 (mdw-regexps "do" "let" "return" "use" "yield"))
3159 (preprocessor-keywords
3160 (mdw-regexps "if" "indent" "else" "endif")))
3162 (setq font-lock-keywords
3163 (list (list (concat "\\(^\\|[^\"]\\)"
3166 "\\(" "[^)*]" "[^*]*" "\\*+" "\\)*"
3171 '(2 font-lock-comment-face))
3173 (list (concat "'" "\\("
3176 "\\|" "[0-9][0-9][0-9]"
3177 "\\|" "u" "[0-9a-fA-F]\\{4\\}"
3178 "\\|" "U" "[0-9a-fA-F]\\{8\\}"
3184 "\\(" "\\\\" "\\(.\\|\n\\)"
3187 '(0 font-lock-string-face))
3189 (list (concat "\\_<\\(" bang-keywords "\\)!" "\\|"
3190 "^#[ \t]*\\(" preprocessor-keywords "\\)\\_>"
3192 "\\_<\\(" fsharp-keywords "\\)\\_>")
3193 '(0 font-lock-keyword-face))
3194 (list (concat "\\<\\(" fsharp-builtins "\\)\\_>")
3195 '(0 font-lock-variable-name-face))
3197 (list (concat "\\_<"
3198 "\\(" "0[bB][01]+" "\\|"
3200 "0[xX][0-9a-fA-F]+" "\\)"
3201 "\\(" "lf\\|LF" "\\|"
3202 "[uU]?[ysnlL]?" "\\)"
3209 "\\([eE][-+]?[0-9]+\\)?"
3214 '(0 mdw-number-face))
3216 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3217 '(0 mdw-punct-face))))))
3219 (defun mdw-fontify-inferior-fsharp ()
3220 (mdw-fontify-fsharp)
3221 (setq font-lock-keywords
3222 (append (list (list "^[#-]" '(0 font-lock-comment-face))
3223 (list "^>" '(0 font-lock-keyword-face)))
3224 font-lock-keywords)))
3227 (add-hook 'fsharp-mode-hook 'mdw-misc-mode-config t)
3228 (add-hook 'fsharp-mode-hook 'mdw-fontify-fsharp t)
3229 (add-hook 'inferior-fsharp-mode-hooks 'mdw-fontify-inferior-fsharp t))
3231 ;;;--------------------------------------------------------------------------
3232 ;;; Go programming configuration.
3234 (defun mdw-fontify-go ()
3236 (make-local-variable 'font-lock-keywords)
3238 (mdw-regexps "break" "case" "chan" "const" "continue"
3239 "default" "defer" "else" "fallthrough" "for"
3240 "func" "go" "goto" "if" "import"
3241 "interface" "map" "package" "range" "return"
3242 "select" "struct" "switch" "type" "var"))
3244 (mdw-regexps "bool" "byte" "complex64" "complex128" "error"
3245 "float32" "float64" "int" "uint8" "int16" "int32"
3246 "int64" "rune" "string" "uint" "uint8" "uint16"
3247 "uint32" "uint64" "uintptr" "void"
3248 "false" "iota" "nil" "true"
3250 "append" "cap" "copy" "delete" "imag" "len" "make"
3251 "new" "panic" "real" "recover")))
3253 (setq font-lock-keywords
3256 ;; Handle the keywords defined above.
3257 (list (concat "\\<\\(" go-keywords "\\)\\>")
3258 '(0 font-lock-keyword-face))
3259 (list (concat "\\<\\(" go-intrinsics "\\)\\>")
3260 '(0 font-lock-variable-name-face))
3262 ;; Strings and characters.
3264 "\\(" "[^\\']" "\\|"
3266 "\\(" "[abfnrtv\\'\"]" "\\|"
3267 "[0-7]\\{3\\}" "\\|"
3268 "x" "[0-9A-Fa-f]\\{2\\}" "\\|"
3269 "u" "[0-9A-Fa-f]\\{4\\}" "\\|"
3270 "U" "[0-9A-Fa-f]\\{8\\}" "\\)" "\\)"
3274 "\\(" "[^\n\\\"]+" "\\|" "\\\\." "\\)*"
3278 '(0 font-lock-string-face))
3280 ;; Handle numbers too.
3282 ;; The following isn't quite right, but it's close enough.
3283 (list (concat "\\<\\("
3284 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
3285 "[0-9]+\\(\\.[0-9]*\\)?"
3286 "\\([eE][-+]?[0-9]+\\)?\\)")
3287 '(0 mdw-number-face))
3289 ;; And anything else is punctuation.
3290 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3291 '(0 mdw-punct-face))))))
3293 (add-hook 'go-mode-hook 'mdw-misc-mode-config t)
3294 (add-hook 'go-mode-hook 'mdw-fontify-go t))
3296 ;;;--------------------------------------------------------------------------
3297 ;;; Rust programming configuration.
3299 (setq-default rust-indent-offset 2)
3301 (defun mdw-self-insert-and-indent (count)
3303 (self-insert-command count)
3304 (indent-according-to-mode))
3306 (defun mdw-fontify-rust ()
3308 ;; Hack syntax categories.
3309 (modify-syntax-entry ?$ ".")
3310 (modify-syntax-entry ?% ".")
3311 (modify-syntax-entry ?= ".")
3313 ;; Fontify keywords and things.
3314 (make-local-variable 'font-lock-keywords)
3315 (let ((rust-keywords
3316 (mdw-regexps "abstract" "alignof" "as" "async" "await"
3317 "become" "box" "break"
3318 "const" "continue" "crate"
3320 "else" "enum" "extern"
3324 "macro" "match" "mod" "move" "mut"
3325 "offsetof" "override"
3326 "priv" "proc" "pub" "pure"
3328 "sizeof" "static" "struct" "super"
3329 "trait" "try" "type" "typeof"
3330 "union" "unsafe" "unsized" "use"
3335 (mdw-regexps "array" "pointer" "slice" "tuple"
3336 "bool" "true" "false"
3338 "i8" "i16" "i32" "i64" "isize"
3339 "u8" "u16" "u32" "u64" "usize"
3342 (setq font-lock-keywords
3345 ;; Handle the keywords defined above.
3346 (list (concat "\\_<\\(" rust-keywords "\\)\\_>")
3347 '(0 font-lock-keyword-face))
3348 (list (concat "\\_<\\(" rust-builtins "\\)\\_>")
3349 '(0 font-lock-variable-name-face))
3351 ;; Handle numbers too.
3352 (list (concat "\\_<\\("
3354 "\\(" "\\(\\.[0-9_]+\\)?[eE][-+]?[0-9_]+"
3358 "\\|" "\\(" "[0-9][0-9_]*"
3359 "\\|" "0x[0-9a-fA-F_]+"
3363 "\\([ui]\\(8\\|16\\|32\\|64\\|size\\)\\)?"
3365 '(0 mdw-number-face))
3367 ;; And anything else is punctuation.
3368 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3369 '(0 mdw-punct-face)))
3370 font-lock-syntactic-face-function nil))
3372 ;; Hack key bindings.
3373 (local-set-key [?{] 'mdw-self-insert-and-indent)
3374 (local-set-key [?}] 'mdw-self-insert-and-indent))
3377 (add-hook 'rust-mode-hook 'mdw-misc-mode-config t)
3378 (add-hook 'rust-mode-hook 'mdw-fontify-rust t))
3380 ;;;--------------------------------------------------------------------------
3381 ;;; Awk programming configuration.
3383 ;; Make Awk indentation nice.
3385 (mdw-define-c-style mdw-awk ()
3386 (c-basic-offset . 2)
3387 (c-offsets-alist (substatement-open . 0)
3388 (c-backslash-column . 72)
3389 (statement-cont . 0)
3390 (statement-case-intro . +)))
3391 (mdw-set-default-c-style 'awk-mode 'mdw-awk)
3393 ;; Declare Awk fontification style.
3395 (defun mdw-fontify-awk ()
3397 ;; Miscellaneous fiddling.
3398 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3400 ;; Now define things to be fontified.
3401 (make-local-variable 'font-lock-keywords)
3403 (mdw-regexps "BEGIN" "END" "ARGC" "ARGIND" "ARGV" "CONVFMT"
3404 "ENVIRON" "ERRNO" "FIELDWIDTHS" "FILENAME" "FNR"
3405 "FS" "IGNORECASE" "NF" "NR" "OFMT" "OFS" "ORS" "RS"
3406 "RSTART" "RLENGTH" "RT" "SUBSEP"
3407 "atan2" "break" "close" "continue" "cos" "delete"
3408 "do" "else" "exit" "exp" "fflush" "file" "for" "func"
3409 "function" "gensub" "getline" "gsub" "if" "in"
3410 "index" "int" "length" "log" "match" "next" "rand"
3411 "return" "print" "printf" "sin" "split" "sprintf"
3412 "sqrt" "srand" "strftime" "sub" "substr" "system"
3413 "systime" "tolower" "toupper" "while")))
3415 (setq font-lock-keywords
3418 ;; Handle the keywords defined above.
3419 (list (concat "\\<\\(" c-keywords "\\)\\>")
3420 '(0 font-lock-keyword-face))
3422 ;; Handle numbers too.
3424 ;; The following isn't quite right, but it's close enough.
3425 (list (concat "\\<\\("
3426 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
3427 "[0-9]+\\(\\.[0-9]*\\)?"
3428 "\\([eE][-+]?[0-9]+\\)?\\)"
3430 '(0 mdw-number-face))
3432 ;; And anything else is punctuation.
3433 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3434 '(0 mdw-punct-face))))))
3437 (add-hook 'awk-mode-hook 'mdw-misc-mode-config t)
3438 (add-hook 'awk-mode-hook 'mdw-fontify-awk t))
3440 ;;;--------------------------------------------------------------------------
3441 ;;; Perl programming style.
3443 ;; Perl indentation style.
3445 (setq-default perl-indent-level 2)
3447 (setq-default cperl-indent-level 2
3448 cperl-continued-statement-offset 2
3449 cperl-indent-region-fix-constructs nil
3450 cperl-continued-brace-offset 0
3451 cperl-brace-offset -2
3452 cperl-brace-imaginary-offset 0
3453 cperl-label-offset 0)
3455 ;; Define perl fontification style.
3457 (defun mdw-fontify-perl ()
3459 ;; Miscellaneous fiddling.
3460 (modify-syntax-entry ?$ "\\")
3461 (modify-syntax-entry ?$ "\\" font-lock-syntax-table)
3462 (modify-syntax-entry ?: "." font-lock-syntax-table)
3463 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3464 (setq auto-fill-function #'do-auto-fill)
3466 ;; Now define fontification things.
3467 (make-local-variable 'font-lock-keywords)
3468 (let ((perl-keywords
3475 "ge" "given" "gt" "goto"
3477 "last" "le" "local" "lt"
3482 "redo" "require" "return"
3484 "undef" "unless" "until" "use"
3487 (setq font-lock-keywords
3490 ;; Set up the keywords defined above.
3491 (list (concat "\\<\\(" perl-keywords "\\)\\>")
3492 '(0 font-lock-keyword-face))
3494 ;; At least numbers are simpler than C.
3495 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
3496 "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\)?"
3497 "\\([eE][-+]?[0-9_]+\\)?")
3498 '(0 mdw-number-face))
3500 ;; And anything else is punctuation.
3501 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3502 '(0 mdw-punct-face))))))
3504 (defun perl-number-tests (&optional arg)
3505 "Assign consecutive numbers to lines containing `#t'. With ARG,
3506 strip numbers instead."
3509 (goto-char (point-min))
3510 (let ((i 0) (fmt (if arg "" " %4d")))
3511 (while (search-forward "#t" nil t)
3512 (delete-region (point) (line-end-position))
3514 (insert (format fmt i)))
3515 (goto-char (point-min))
3516 (if (re-search-forward "\\(tests\\s-*=>\\s-*\\)\\w*" nil t)
3517 (replace-match (format "\\1%d" i))))))
3519 (dolist (hook '(perl-mode-hook cperl-mode-hook))
3520 (add-hook hook 'mdw-misc-mode-config t)
3521 (add-hook hook 'mdw-fontify-perl t))
3523 ;;;--------------------------------------------------------------------------
3524 ;;; Python programming style.
3526 (setq-default py-indent-offset 2
3528 python-indent-offset 2
3529 python-fill-docstring-style 'symmetric)
3531 (defun mdw-fontify-pythonic (keywords soft-keywords builtins)
3533 ;; Miscellaneous fiddling.
3534 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3535 (setq indent-tabs-mode nil)
3536 (set (make-local-variable 'forward-sexp-function) nil)
3538 ;; Now define fontification things.
3539 (make-local-variable 'font-lock-keywords)
3540 (setq font-lock-keywords
3543 ;; Set up the keywords defined above.
3544 (list (concat "\\_<\\(" keywords "\\)\\_>")
3545 '(0 font-lock-keyword-face))
3546 (list (concat "\\(^\\|[^.]\\)\\_<\\(" soft-keywords "\\)\\_>")
3547 '(2 font-lock-keyword-face))
3548 (list (concat "\\(^\\|[^.]\\)\\_<\\(" builtins "\\)\\_>")
3549 '(2 font-lock-variable-name-face))
3550 (list (concat "\\_<\\(__\\(\\sw+\\|\\s_+\\)+__\\)\\_>")
3551 '(0 font-lock-variable-name-face))
3553 ;; At least numbers are simpler than C.
3554 (list (concat "\\_<0\\([xX][0-9a-fA-F]+\\|[oO]?[0-7]+\\|[bB][01]+\\)\\|"
3555 "\\_<[0-9][0-9]*\\(\\.[0-9]*\\)?"
3556 "\\([eE][-+]?[0-9]+\\|[lL]\\)?")
3557 '(0 mdw-number-face))
3559 ;; And anything else is punctuation.
3560 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3561 '(0 mdw-punct-face)))))
3563 ;; Define Python fontification styles.
3565 (defun mdw-fontify-python ()
3566 (mdw-fontify-pythonic
3567 (mdw-regexps "and" "as" "assert" "async" "await"
3571 "elif" "else" "except" ;"exec"
3572 "finally" "for" "from"
3574 "if" "import" "in" "is"
3588 (mdw-regexps "Ellipsis"
3590 "None" "NotImplemented"
3595 "BaseExceptionGroup"
3599 "FloatingPointError"
3612 "ConnectionAbortedError"
3613 "ConnectionRefusedError"
3614 "ConnectionResetError"
3619 "NotADirectoryError"
3625 "ModuleNotFoundError"
3634 "NotImplementedError"
3643 "UnicodeDecodeError"
3644 "UnicodeEncodeError"
3645 "UnicodeTranslateError"
3649 "DeprecationWarning"
3653 "PendingDeprecationWarning"
3663 "abs" "absolute_import" "aiter"
3664 "all" "anext" "any" "apply" "ascii"
3665 "basestring" "bin" "bool" "breakpoint"
3666 "buffer" "bytearray" "bytes"
3667 "callable" "coerce" "chr" "classmethod"
3668 "cmp" "compile" "complex"
3669 "delattr" "dict" "dir" "divmod"
3670 "enumerate" "eval" "exec" "execfile"
3671 "file" "filter" "float" "format" "frozenset"
3673 "hasattr" "hash" "help" "hex"
3674 "id" "input" "int" "intern"
3675 "isinstance" "issubclass" "iter"
3676 "len" "list" "locals" "long"
3677 "map" "max" "memoryview" "min"
3679 "object" "oct" "open" "ord"
3680 "pow" "print" "property"
3681 "range" "raw_input" "reduce" "reload"
3682 "repr" "reversed" "round"
3683 "set" "setattr" "slice" "sorted"
3684 "staticmethod" "str" "sum" "super"
3692 (defun mdw-fontify-pyrex ()
3693 (mdw-fontify-pythonic
3694 (mdw-regexps "and" "as" "assert" "break" "cdef" "class" "continue"
3695 "ctypedef" "def" "del" "elif" "else" "enum" "except" "exec"
3696 "extern" "finally" "for" "from" "global" "if"
3697 "import" "in" "is" "lambda" "not" "or" "pass" "print"
3698 "property" "raise" "return" "struct" "try" "while" "with"
3703 (define-derived-mode pyrex-mode python-mode "Pyrex"
3704 "Major mode for editing Pyrex source code")
3705 (setq auto-mode-alist
3706 (append '(("\\.pyx$" . pyrex-mode)
3707 ("\\.pxd$" . pyrex-mode)
3708 ("\\.pxi$" . pyrex-mode))
3712 (add-hook 'python-mode-hook 'mdw-misc-mode-config t)
3713 (add-hook 'python-mode-hook 'mdw-fontify-python t)
3714 (add-hook 'pyrex-mode-hook 'mdw-fontify-pyrex t))
3716 ;;;--------------------------------------------------------------------------
3717 ;;; Lua programming style.
3719 (setq-default lua-indent-level 2)
3721 (defun mdw-fontify-lua ()
3723 ;; Miscellaneous fiddling.
3724 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3726 ;; Now define fontification things.
3727 (make-local-variable 'font-lock-keywords)
3729 (mdw-regexps "and" "break" "do" "else" "elseif" "end"
3730 "false" "for" "function" "goto" "if" "in" "local"
3731 "nil" "not" "or" "repeat" "return" "then" "true"
3733 (setq font-lock-keywords
3736 ;; Set up the keywords defined above.
3737 (list (concat "\\_<\\(" lua-keywords "\\)\\_>")
3738 '(0 font-lock-keyword-face))
3740 ;; At least numbers are simpler than C.
3741 (list (concat "\\_<\\(" "0[xX]"
3742 "\\(" "[0-9a-fA-F]+"
3743 "\\(\\.[0-9a-fA-F]*\\)?"
3744 "\\|" "\\.[0-9a-fA-F]+"
3746 "\\([pP][-+]?[0-9]+\\)?"
3747 "\\|" "\\(" "[0-9]+"
3751 "\\([eE][-+]?[0-9]+\\)?"
3753 '(0 mdw-number-face))
3755 ;; And anything else is punctuation.
3756 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3757 '(0 mdw-punct-face))))))
3760 (add-hook 'lua-mode-hook 'mdw-misc-mode-config t)
3761 (add-hook 'lua-mode-hook 'mdw-fontify-lua t))
3763 ;;;--------------------------------------------------------------------------
3764 ;;; Icon programming style.
3766 ;; Icon indentation style.
3768 (setq-default icon-brace-offset 0
3769 icon-continued-brace-offset 0
3770 icon-continued-statement-offset 2
3771 icon-indent-level 2)
3773 ;; Define Icon fontification style.
3775 (defun mdw-fontify-icon ()
3777 ;; Miscellaneous fiddling.
3778 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3780 ;; Now define fontification things.
3781 (make-local-variable 'font-lock-keywords)
3782 (let ((icon-keywords
3783 (mdw-regexps "break" "by" "case" "create" "default" "do" "else"
3784 "end" "every" "fail" "global" "if" "initial"
3785 "invocable" "link" "local" "next" "not" "of"
3786 "procedure" "record" "repeat" "return" "static"
3787 "suspend" "then" "to" "until" "while"))
3788 (preprocessor-keywords
3789 (mdw-regexps "define" "else" "endif" "error" "ifdef" "ifndef"
3790 "include" "line" "undef")))
3791 (setq font-lock-keywords
3794 ;; Set up the keywords defined above.
3795 (list (concat "\\<\\(" icon-keywords "\\)\\>")
3796 '(0 font-lock-keyword-face))
3798 ;; The things that Icon calls keywords.
3799 (list "&\\sw+\\>" '(0 font-lock-variable-name-face))
3801 ;; At least numbers are simpler than C.
3802 (list (concat "\\<[0-9]+"
3803 "\\([rR][0-9a-zA-Z]+\\|"
3804 "\\.[0-9]+\\([eE][+-]?[0-9]+\\)?\\)\\>\\|"
3805 "\\.[0-9]+\\([eE][+-]?[0-9]+\\)?\\>")
3806 '(0 mdw-number-face))
3809 (list (concat "^[ \t]*$[ \t]*\\<\\("
3810 preprocessor-keywords
3812 '(0 font-lock-keyword-face))
3814 ;; And anything else is punctuation.
3815 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3816 '(0 mdw-punct-face))))))
3819 (add-hook 'icon-mode-hook 'mdw-misc-mode-config t)
3820 (add-hook 'icon-mode-hook 'mdw-fontify-icon t))
3822 ;;;--------------------------------------------------------------------------
3825 (defun mdw-fontify-fortran-common ()
3826 (let ((fortran-keywords
3827 (mdw-regexps "access"
3845 "double\\s-*precision"
3846 "else" "elseif" "elsewhere"
3848 "endblock" "endblockdata"
3904 "select" "selectcase" "selecttype"
3915 (fortran-operators (mdw-regexps "and"
3928 (fortran-intrinsics (mdw-regexps "abs" "dabs" "iabs" "cabs"
3929 "atan" "datan" "atan2" "datan2"
3939 "int" "aint" "idint"
3940 "alog" "dlog" "clog"
3952 "sign" "isign" "dsign"
3954 "sqrt" "dsqrt" "csqrt"
3956 (preprocessor-keywords
3957 (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
3958 "ident" "if" "ifdef" "ifndef" "import" "include"
3959 "line" "pragma" "unassert" "undef" "warning")))
3960 (setq font-lock-keywords-case-fold-search t
3964 ;; Fontify include files as strings.
3965 (list (concat "^[ \t]*\\#[ \t]*" "include"
3966 "[ \t]*\\(<[^>]+>?\\)")
3967 '(1 font-lock-string-face))
3969 ;; Preprocessor directives are `references'?.
3970 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
3971 preprocessor-keywords
3972 "\\)\\>\\|[0-9]+\\|$\\)\\)")
3973 '(1 font-lock-keyword-face))
3975 ;; Set up the keywords defined above.
3976 (list (concat "\\<\\(" fortran-keywords "\\)\\>")
3977 '(0 font-lock-keyword-face))
3979 ;; Set up the `.foo.' operators.
3980 (list (concat "\\.\\(" fortran-operators "\\)\\.")
3981 '(0 font-lock-keyword-face))
3983 ;; Set up the intrinsic functions.
3984 (list (concat "\\<\\(" fortran-intrinsics "\\)\\>")
3985 '(0 font-lock-variable-name-face))
3988 (list (concat "\\(" "\\<" "[0-9]+" "\\(\\.[0-9]*\\)?"
3991 "\\(" "[de]" "[+-]?" "[0-9]+" "\\)?"
3992 "\\(" "_" "\\sw+" "\\)?"
3993 "\\|" "b'[01]*'" "\\|" "'[01]*'b"
3994 "\\|" "b\"[01]*\"" "\\|" "\"[01]*\"b"
3995 "\\|" "o'[0-7]*'" "\\|" "'[0-7]*'o"
3996 "\\|" "o\"[0-7]*\"" "\\|" "\"[0-7]*\"o"
3997 "\\|" "[xz]'[0-9a-f]*'" "\\|" "'[0-9a-f]*'[xz]"
3998 "\\|" "[xz]\"[0-9a-f]*\"" "\\|" "\"[0-9a-f]*\"[xz]")
3999 '(0 mdw-number-face))
4001 ;; Any anything else is punctuation.
4002 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4003 '(0 mdw-punct-face))))
4005 (modify-syntax-entry ?/ "." font-lock-syntax-table)
4006 (modify-syntax-entry ?< ".")
4007 (modify-syntax-entry ?> ".")))
4009 (defun mdw-fontify-fortran () (mdw-fontify-fortran-common))
4010 (defun mdw-fontify-f90 () (mdw-fontify-fortran-common))
4012 (setq fortran-do-indent 2
4014 fortran-structure-indent 2
4015 fortran-comment-line-start "*"
4016 fortran-comment-indent-style 'relative
4017 fortran-continuation-string "&"
4018 fortran-continuation-indent 4)
4020 (setq f90-do-indent 2
4022 f90-program-indent 2
4023 f90-continuation-indent 4
4024 f90-smart-end-names nil
4025 f90-smart-end 'no-blink)
4028 (add-hook 'fortran-mode-hook 'mdw-misc-mode-config t)
4029 (add-hook 'fortran-mode-hook 'mdw-fontify-fortran t)
4030 (add-hook 'f90-mode-hook 'mdw-misc-mode-config t)
4031 (add-hook 'f90-mode-hook 'mdw-fontify-f90 t))
4033 ;;;--------------------------------------------------------------------------
4036 (defun mdw-fontify-asm ()
4037 (modify-syntax-entry ?' "\"")
4038 (modify-syntax-entry ?. "w")
4039 (modify-syntax-entry ?\n ">")
4040 (setf fill-prefix nil)
4041 (modify-syntax-entry ?. "_")
4042 (modify-syntax-entry ?* ". 23")
4043 (modify-syntax-entry ?/ ". 124b")
4044 (modify-syntax-entry ?\n "> b")
4045 (local-set-key ";" 'self-insert-command)
4046 (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)"))
4048 (defun mdw-asm-set-comment ()
4049 (modify-syntax-entry ?; "."
4051 (modify-syntax-entry asm-comment-char "< b")
4052 (setq comment-start (string asm-comment-char ? )))
4053 (add-hook 'asm-mode-local-variables-hook 'mdw-asm-set-comment)
4054 (put 'asm-comment-char 'safe-local-variable 'characterp)
4057 (add-hook 'asm-mode-hook 'mdw-misc-mode-config t)
4058 (add-hook 'asm-mode-hook 'mdw-fontify-asm t))
4060 ;;;--------------------------------------------------------------------------
4061 ;;; TCL configuration.
4063 (setq-default tcl-indent-level 2)
4065 (defun mdw-fontify-tcl ()
4067 (modify-syntax-entry ch "."))
4068 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
4069 (make-local-variable 'font-lock-keywords)
4070 (setq font-lock-keywords
4072 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
4073 "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\)?"
4074 "\\([eE][-+]?[0-9_]+\\)?")
4075 '(0 mdw-number-face))
4076 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4077 '(0 mdw-punct-face)))))
4080 (add-hook 'tcl-mode-hook 'mdw-misc-mode-config t)
4081 (add-hook 'tcl-mode-hook 'mdw-fontify-tcl t))
4083 ;;;--------------------------------------------------------------------------
4084 ;;; Dylan programming configuration.
4086 (defun mdw-fontify-dylan ()
4088 (make-local-variable 'font-lock-keywords)
4090 ;; Horrors. `dylan-mode' sets the `major-mode' name after calling this
4091 ;; hook, which undoes all of our configuration.
4092 (setq major-mode 'dylan-mode)
4093 (font-lock-set-defaults)
4095 (let* ((word "[-_a-zA-Z!*@<>$%]+")
4096 (dylan-keywords (mdw-regexps
4098 "C-address" "C-callable-wrapper" "C-function"
4099 "C-mapped-subtype" "C-pointer-type" "C-struct"
4100 "C-subtype" "C-union" "C-variable"
4102 "above" "abstract" "afterwards" "all"
4103 "begin" "below" "block" "by"
4104 "case" "class" "cleanup" "constant" "create"
4106 "else" "elseif" "end" "exception" "export"
4107 "finally" "for" "from" "function"
4110 "if" "in" "instance" "interface" "iterate"
4112 "let" "library" "local"
4113 "macro" "method" "module"
4116 "select" "slot" "subclass"
4118 "unless" "until" "use"
4119 "variable" "virtual"
4121 (sharp-keywords (mdw-regexps
4122 "all-keys" "key" "next" "rest" "include"
4124 (setq font-lock-keywords
4125 (list (list (concat "\\<\\(" dylan-keywords
4126 "\\|" "with\\(out\\)?-" word
4128 '(0 font-lock-keyword-face))
4129 (list (concat "\\<" word ":" "\\|"
4130 "#\\(" sharp-keywords "\\)\\>")
4131 '(0 font-lock-variable-name-face))
4133 "\\([-+]\\|\\<\\)[0-9]+" "\\("
4134 "\\(\\.[0-9]+\\)?" "\\([eE][-+][0-9]+\\)?"
4137 "\\|" "\\.[0-9]+" "\\([eE][-+][0-9]+\\)?"
4140 "\\|" "#x[0-9a-zA-Z]+"
4142 '(0 mdw-number-face))
4144 "\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\|"
4145 "\\_<[-+*/=<>:&|]+\\_>"
4147 '(0 mdw-punct-face))))))
4150 (add-hook 'dylan-mode-hook 'mdw-misc-mode-config t)
4151 (add-hook 'dylan-mode-hook 'mdw-fontify-dylan t))
4153 ;;;--------------------------------------------------------------------------
4154 ;;; Algol 68 configuration.
4156 (setq-default a68-indent-step 2)
4158 (defun mdw-fontify-algol-68 ()
4160 ;; Fix up the syntax table.
4161 (modify-syntax-entry ?# "!" a68-mode-syntax-table)
4162 (dolist (ch '(?- ?+ ?= ?< ?> ?* ?/ ?| ?&))
4163 (modify-syntax-entry ch "." a68-mode-syntax-table))
4165 (make-local-variable 'font-lock-keywords)
4168 (let ((word "COMMENT"))
4169 (cl-do ((regexp (concat "[^" (substring word 0 1) "]+")
4170 (concat regexp "\\|"
4171 (substring word 0 i)
4172 "[^" (substring word i (1+ i)) "]"))
4174 ((>= i (length word)) regexp)))))
4175 (setq font-lock-keywords
4176 (list (list (concat "\\<COMMENT\\>"
4177 "\\(" not-comment "\\)\\{0,5\\}"
4178 "\\(\\'\\|\\<COMMENT\\>\\)")
4179 '(0 font-lock-comment-face))
4180 (list (concat "\\<CO\\>"
4181 "\\([^C]+\\|C[^O]\\)\\{0,5\\}"
4182 "\\($\\|\\<CO\\>\\)")
4183 '(0 font-lock-comment-face))
4184 (list "\\<[A-Z_]+\\>"
4185 '(0 font-lock-keyword-face))
4189 "\\([eE][-+]?[0-9]+\\)?"
4191 '(0 mdw-number-face))
4192 (list "\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/"
4193 '(0 mdw-punct-face))))))
4195 (dolist (hook '(a68-mode-hook a68-mode-hooks))
4196 (add-hook hook 'mdw-misc-mode-config t)
4197 (add-hook hook 'mdw-fontify-algol-68 t))
4199 ;;;--------------------------------------------------------------------------
4200 ;;; REXX configuration.
4202 (defun mdw-rexx-electric-* ()
4207 (defun mdw-rexx-indent-newline-indent ()
4210 (if abbrev-mode (expand-abbrev))
4211 (newline-and-indent))
4213 (defun mdw-fontify-rexx ()
4215 ;; Various bits of fiddling.
4216 (setq mdw-auto-indent nil)
4217 (local-set-key [?\C-m] 'mdw-rexx-indent-newline-indent)
4218 (local-set-key [?*] 'mdw-rexx-electric-*)
4219 (dolist (ch '(?! ?? ?# ?@ ?$)) (modify-syntax-entry ch "w"))
4220 (dolist (ch '(?¬)) (modify-syntax-entry ch "."))
4221 (mdw-standard-fill-prefix "\\([ \t]*/?\*[ \t]*\\)")
4223 ;; Set up keywords and things for fontification.
4224 (make-local-variable 'font-lock-keywords-case-fold-search)
4225 (setq font-lock-keywords-case-fold-search t)
4227 (setq rexx-indent 2)
4228 (setq rexx-end-indent rexx-indent)
4229 (setq rexx-cont-indent rexx-indent)
4231 (make-local-variable 'font-lock-keywords)
4232 (let ((rexx-keywords
4233 (mdw-regexps "address" "arg" "by" "call" "digits" "do" "drop"
4234 "else" "end" "engineering" "exit" "expose" "for"
4235 "forever" "form" "fuzz" "if" "interpret" "iterate"
4236 "leave" "linein" "name" "nop" "numeric" "off" "on"
4237 "options" "otherwise" "parse" "procedure" "pull"
4238 "push" "queue" "return" "say" "select" "signal"
4239 "scientific" "source" "then" "trace" "to" "until"
4240 "upper" "value" "var" "version" "when" "while"
4243 "abbrev" "abs" "bitand" "bitor" "bitxor" "b2x"
4244 "center" "center" "charin" "charout" "chars"
4245 "compare" "condition" "copies" "c2d" "c2x"
4246 "datatype" "date" "delstr" "delword" "d2c" "d2x"
4247 "errortext" "format" "fuzz" "insert" "lastpos"
4248 "left" "length" "lineout" "lines" "max" "min"
4249 "overlay" "pos" "queued" "random" "reverse" "right"
4250 "sign" "sourceline" "space" "stream" "strip"
4251 "substr" "subword" "symbol" "time" "translate"
4252 "trunc" "value" "verify" "word" "wordindex"
4253 "wordlength" "wordpos" "words" "xrange" "x2b" "x2c"
4256 (setq font-lock-keywords
4259 ;; Set up the keywords defined above.
4260 (list (concat "\\<\\(" rexx-keywords "\\)\\>")
4261 '(0 font-lock-keyword-face))
4263 ;; Fontify all symbols the same way.
4264 (list (concat "\\<\\([0-9.][A-Za-z0-9.!?_#@$]*[Ee][+-]?[0-9]+\\|"
4265 "[A-Za-z0-9.!?_#@$]+\\)")
4266 '(0 font-lock-variable-name-face))
4268 ;; And everything else is punctuation.
4269 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4270 '(0 mdw-punct-face))))))
4273 (add-hook 'rexx-mode-hook 'mdw-misc-mode-config t)
4274 (add-hook 'rexx-mode-hook 'mdw-fontify-rexx t))
4276 ;;;--------------------------------------------------------------------------
4277 ;;; Standard ML programming style.
4279 (setq-default sml-nested-if-indent t
4282 sml-type-of-indent nil)
4284 (defun mdw-fontify-sml ()
4286 ;; Make underscore an honorary letter.
4287 (modify-syntax-entry ?' "w")
4290 (mdw-standard-fill-prefix "\\([ \t]*(\*[ \t]*\\)")
4292 ;; Now define fontification things.
4293 (make-local-variable 'font-lock-keywords)
4295 (mdw-regexps "abstype" "and" "andalso" "as"
4298 "else" "end" "eqtype" "exception"
4299 "fn" "fun" "functor"
4301 "if" "in" "include" "infix" "infixr"
4304 "of" "op" "open" "orelse"
4306 "sharing" "sig" "signature" "struct" "structure"
4309 "where" "while" "with" "withtype")))
4311 (setq font-lock-keywords
4314 ;; Set up the keywords defined above.
4315 (list (concat "\\<\\(" sml-keywords "\\)\\>")
4316 '(0 font-lock-keyword-face))
4318 ;; At least numbers are simpler than C.
4319 (list (concat "\\<\\~?"
4320 "\\(0\\([wW]?[xX][0-9a-fA-F]+\\|"
4322 "\\([0-9]+\\(\\.[0-9]+\\)?"
4325 '(0 mdw-number-face))
4327 ;; And anything else is punctuation.
4328 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4329 '(0 mdw-punct-face))))))
4332 (add-hook 'sml-mode-hook 'mdw-misc-mode-config t)
4333 (add-hook 'sml-mode-hook 'mdw-fontify-sml t))
4335 ;;;--------------------------------------------------------------------------
4336 ;;; Haskell configuration.
4338 (setq-default haskell-indent-offset 2)
4339 (setq haskell-doc-prettify-types nil
4340 haskell-interactive-popup-errors nil)
4342 (defun mdw-fontify-haskell ()
4344 ;; Fiddle with syntax table to get comments right.
4345 (modify-syntax-entry ?' "_")
4346 (modify-syntax-entry ?- ". 12")
4347 (modify-syntax-entry ?\n ">")
4349 ;; Make punctuation be punctuation
4350 (let ((punct "=<>+-*/|&%!@?$.^:#`"))
4351 (cl-do ((i 0 (1+ i)))
4352 ((>= i (length punct)))
4353 (modify-syntax-entry (aref punct i) ".")))
4356 (mdw-standard-fill-prefix "\\([ \t]*{?--?[ \t]*\\)")
4358 ;; Fiddle with fontification.
4359 (make-local-variable 'font-lock-keywords)
4360 (let ((haskell-keywords
4362 "case" "ccall" "class"
4363 "data" "default" "deriving" "do"
4367 "if" "import" "in" "infix" "infixl" "infixr" "instance"
4380 (mdw-regexps "ACK" "BEL" "BS" "CAN" "CR" "DC1" "DC2" "DC3" "DC4"
4381 "DEL" "DLE" "EM" "ENQ" "EOT" "ESC" "ETB" "ETX" "FF"
4382 "FS" "GS" "HT" "LF" "NAK" "NUL" "RS" "SI" "SO" "SOH"
4383 "SP" "STX" "SUB" "SYN" "US" "VT")))
4385 (setq font-lock-keywords
4387 (list (concat "{-" "[^-]*" "\\(-+[^-}][^-]*\\)*"
4391 '(0 font-lock-comment-face))
4392 (list (concat "\\_<\\(" haskell-keywords "\\)\\_>")
4393 '(0 font-lock-keyword-face))
4394 (list (concat "'\\("
4398 "\\(" "[abfnrtv\\\"']" "\\|"
4399 "^" "\\(" control-sequences "\\|"
4400 "[]A-Z@[\\^_]" "\\)" "\\|"
4407 '(0 font-lock-string-face))
4408 (list "\\_<[A-Z]\\(\\sw+\\|\\s_+\\)*\\_>"
4409 '(0 font-lock-variable-name-face))
4410 (list (concat "\\_<0\\([xX][0-9a-fA-F]+\\|[oO][0-7]+\\)\\|"
4411 "\\_<[0-9]+\\(\\.[0-9]*\\)?"
4412 "\\([eE][-+]?[0-9]+\\)?")
4413 '(0 mdw-number-face))
4414 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4415 '(0 mdw-punct-face))))))
4418 (add-hook 'haskell-mode-hook 'mdw-misc-mode-config t)
4419 (add-hook 'haskell-mode-hook 'mdw-fontify-haskell t))
4421 ;;;--------------------------------------------------------------------------
4422 ;;; Erlang configuration.
4424 (setq-default erlang-electric-commands nil)
4426 (defun mdw-fontify-erlang ()
4429 (mdw-standard-fill-prefix "\\([ \t]*{?%*[ \t]*\\)")
4431 ;; Fiddle with fontification.
4432 (make-local-variable 'font-lock-keywords)
4433 (let ((erlang-keywords
4434 (mdw-regexps "after" "and" "andalso"
4435 "band" "begin" "bnot" "bor" "bsl" "bsr" "bxor"
4436 "case" "catch" "cond"
4437 "div" "end" "fun" "if" "let" "not"
4439 "query" "receive" "rem" "try" "when" "xor")))
4441 (setq font-lock-keywords
4444 '(0 font-lock-comment-face))
4445 (list (concat "\\<\\(" erlang-keywords "\\)\\>")
4446 '(0 font-lock-keyword-face))
4447 (list (concat "^-\\sw+\\>")
4448 '(0 font-lock-keyword-face))
4449 (list "\\<[0-9]+\\(#[0-9a-zA-Z]+\\|[eE][+-]?[0-9]+\\)?\\>"
4450 '(0 mdw-number-face))
4451 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4452 '(0 mdw-punct-face))))))
4455 (add-hook 'erlang-mode-hook 'mdw-misc-mode-config t)
4456 (add-hook 'erlang-mode-hook 'mdw-fontify-erlang t))
4458 ;;;--------------------------------------------------------------------------
4459 ;;; Texinfo configuration.
4461 (defun mdw-fontify-texinfo ()
4464 (mdw-standard-fill-prefix "\\([ \t]*@c[ \t]+\\)")
4466 ;; Real fontification things.
4467 (make-local-variable 'font-lock-keywords)
4468 (setq font-lock-keywords
4471 ;; Environment names are keywords.
4472 (list "@\\(end\\) *\\([a-zA-Z]*\\)?"
4473 '(2 font-lock-keyword-face))
4475 ;; Unmark escaped magic characters.
4476 (list "\\(@\\)\\([@{}]\\)"
4477 '(1 font-lock-keyword-face)
4478 '(2 font-lock-variable-name-face))
4480 ;; Make sure we get comments properly.
4481 (list "@c\\(omment\\)?\\( .*\\)?$"
4482 '(0 font-lock-comment-face))
4484 ;; Command names are keywords.
4485 (list "@\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)"
4486 '(0 font-lock-keyword-face))
4488 ;; Fontify TeX special characters as punctuation.
4490 '(0 mdw-punct-face)))))
4492 (dolist (hook '(texinfo-mode-hook TeXinfo-mode-hook))
4493 (add-hook hook 'mdw-misc-mode-config t)
4494 (add-hook hook 'mdw-fontify-texinfo t))
4496 ;;;--------------------------------------------------------------------------
4497 ;;; TeX and LaTeX configuration.
4499 (setq-default LaTeX-table-label "tbl:"
4500 TeX-auto-untabify nil
4501 LaTeX-syntactic-comments nil
4502 LaTeX-fill-break-at-separators '(\\\[))
4504 (defun mdw-fontify-tex ()
4505 (setq ispell-parser 'tex)
4508 ;; Don't make maths into a string.
4509 (modify-syntax-entry ?$ ".")
4510 (modify-syntax-entry ?$ "." font-lock-syntax-table)
4511 (local-set-key [?$] 'self-insert-command)
4513 ;; Make `tab' be useful, given that tab stops in TeX don't work well.
4514 (local-set-key "\C-\M-i" 'indent-relative)
4515 (setq indent-tabs-mode nil)
4518 (mdw-standard-fill-prefix "\\([ \t]*%+[ \t]*\\)")
4520 ;; Real fontification things.
4521 (make-local-variable 'font-lock-keywords)
4522 (setq font-lock-keywords
4525 ;; Environment names are keywords.
4526 (list (concat "\\\\\\(begin\\|end\\|newenvironment\\)"
4528 '(2 font-lock-keyword-face))
4530 ;; Suspended environment names are keywords too.
4531 (list (concat "\\\\\\(suspend\\|resume\\)\\(\\[[^]]*\\]\\)?"
4533 '(3 font-lock-keyword-face))
4535 ;; Command names are keywords.
4536 (list "\\\\\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)"
4537 '(0 font-lock-keyword-face))
4539 ;; Handle @/.../ for italics.
4540 ;; (list "\\(@/\\)\\([^/]*\\)\\(/\\)"
4541 ;; '(1 font-lock-keyword-face)
4542 ;; '(3 font-lock-keyword-face))
4544 ;; Handle @*...* for boldness.
4545 ;; (list "\\(@\\*\\)\\([^*]*\\)\\(\\*\\)"
4546 ;; '(1 font-lock-keyword-face)
4547 ;; '(3 font-lock-keyword-face))
4549 ;; Handle @`...' for literal syntax things.
4550 ;; (list "\\(@`\\)\\([^']*\\)\\('\\)"
4551 ;; '(1 font-lock-keyword-face)
4552 ;; '(3 font-lock-keyword-face))
4554 ;; Handle @<...> for nonterminals.
4555 ;; (list "\\(@<\\)\\([^>]*\\)\\(>\\)"
4556 ;; '(1 font-lock-keyword-face)
4557 ;; '(3 font-lock-keyword-face))
4559 ;; Handle other @-commands.
4560 ;; (list "@\\([^a-zA-Z]\\|[a-zA-Z]*\\)"
4561 ;; '(0 font-lock-keyword-face))
4563 ;; Make sure we get comments properly.
4565 '(0 font-lock-comment-face))
4567 ;; Fontify TeX special characters as punctuation.
4569 '(0 mdw-punct-face)))))
4571 (setq TeX-install-font-lock 'tex-font-setup)
4573 (eval-after-load 'font-latex
4574 '(defun font-latex-jit-lock-force-redisplay (buf start end)
4575 "Compatibility for Emacsen not offering `jit-lock-force-redisplay'."
4576 ;; The following block is an expansion of `jit-lock-force-redisplay'
4577 ;; and involved macros taken from CVS Emacs on 2007-04-28.
4578 (with-current-buffer buf
4579 (let ((modified (buffer-modified-p)))
4581 (let ((buffer-undo-list t)
4582 (inhibit-read-only t)
4583 (inhibit-point-motion-hooks t)
4584 (inhibit-modification-hooks t)
4587 buffer-file-truename)
4588 (put-text-property start end 'fontified t))
4590 (restore-buffer-modified-p nil)))))))
4592 (setq TeX-output-view-style
4594 ("^landscape$" "^pstricks$\\|^pst-\\|^psfrag$")
4595 "%(o?)dvips -t landscape %d -o && xdg-open %f")
4596 ("^dvi$" "^pstricks$\\|^pst-\\|^psfrag$"
4597 "%(o?)dvips %d -o && xdg-open %f")
4599 ("^a4\\(?:dutch\\|paper\\|wide\\)\\|sem-a4$" "^landscape$")
4600 "%(o?)xdvi %dS -paper a4r -s 0 %d")
4601 ("^dvi$" "^a4\\(?:dutch\\|paper\\|wide\\)\\|sem-a4$"
4602 "%(o?)xdvi %dS -paper a4 %d")
4604 ("^a5\\(?:comb\\|paper\\)$" "^landscape$")
4605 "%(o?)xdvi %dS -paper a5r -s 0 %d")
4606 ("^dvi$" "^a5\\(?:comb\\|paper\\)$" "%(o?)xdvi %dS -paper a5 %d")
4607 ("^dvi$" "^b5paper$" "%(o?)xdvi %dS -paper b5 %d")
4608 ("^dvi$" "^letterpaper$" "%(o?)xdvi %dS -paper us %d")
4609 ("^dvi$" "^legalpaper$" "%(o?)xdvi %dS -paper legal %d")
4610 ("^dvi$" "^executivepaper$" "%(o?)xdvi %dS -paper 7.25x10.5in %d")
4611 ("^dvi$" "." "%(o?)xdvi %dS %d")
4612 ("^pdf$" "." "xdg-open %o")
4613 ("^html?$" "." "sensible-browser %o")))
4615 (setq TeX-view-program-list
4616 '(("mupdf" ("mupdf %o" (mode-io-correlate " %(outpage)")))))
4618 (setq TeX-view-program-selection
4619 '(((output-dvi style-pstricks) "dvips and gv")
4621 (output-pdf "mupdf")
4622 (output-html "sensible-browser")))
4624 (setq TeX-open-quote "\""
4625 TeX-close-quote "\"")
4627 (setq reftex-use-external-file-finders t
4628 reftex-auto-recenter-toc t)
4630 (setq reftex-label-alist
4631 '(("theorem" ?T "th:" "~\\ref{%s}" t ("theorems?" "th\\.") -2)
4632 ("axiom" ?A "ax:" "~\\ref{%s}" t ("axioms?" "ax\\.") -2)
4633 ("definition" ?D "def:" "~\\ref{%s}" t ("definitions?" "def\\.") -2)
4634 ("proposition" ?P "prop:" "~\\ref{%s}" t
4635 ("propositions?" "prop\\.") -2)
4636 ("lemma" ?L "lem:" "~\\ref{%s}" t ("lemmas?" "lem\\.") -2)
4637 ("example" ?X "eg:" "~\\ref{%s}" t ("examples?") -2)
4638 ("exercise" ?E "ex:" "~\\ref{%s}" t ("exercises?" "ex\\.") -2)
4639 ("enumerate" ?i "i:" "~\\ref{%s}" item ("items?"))))
4640 (setq reftex-section-prefixes
4645 (setq bibtex-field-delimiters 'double-quotes
4646 bibtex-align-at-equal-sign t
4647 bibtex-entry-format '(realign opts-or-alts required-fields
4648 numerical-fields last-comma delimiters
4649 unify-case sort-fields braces)
4650 bibtex-sort-ignore-string-entries nil
4651 bibtex-maintain-sorted-entries 'entry-class
4652 bibtex-include-OPTkey t
4653 bibtex-autokey-names-stretch 1
4654 bibtex-autokey-expand-strings t
4655 bibtex-autokey-name-separator "-"
4656 bibtex-autokey-year-length 4
4657 bibtex-autokey-titleword-separator "-"
4658 bibtex-autokey-name-year-separator "-"
4659 bibtex-autokey-year-title-separator ":")
4662 (dolist (hook '(tex-mode-hook latex-mode-hook
4663 TeX-mode-hook LaTeX-mode-hook))
4664 (add-hook hook 'mdw-misc-mode-config t)
4665 (add-hook hook 'mdw-fontify-tex t))
4666 (add-hook 'bibtex-mode-hook (lambda () (setq fill-column 76))))
4668 ;;;--------------------------------------------------------------------------
4669 ;;; HTML, CSS, and other web foolishness.
4671 (setq-default css-indent-offset 8)
4673 ;;;--------------------------------------------------------------------------
4676 (setq-default psgml-html-build-new-buffer nil)
4678 (defun mdw-sgml-mode ()
4681 (mdw-standard-fill-prefix "")
4682 (make-local-variable 'sgml-delimiters)
4683 (setq sgml-delimiters
4684 '("AND" "&" "COM" "--" "CRO" "&#" "DSC" "]" "DSO" "[" "DTGC" "]"
4685 "DTGO" "[" "ERO" "&" "ETAGO" ":e" "GRPC" ")" "GRPO" "(" "LIT"
4686 "\"" "LITA" "'" "MDC" ">" "MDO" "<!" "MINUS" "-" "MSC" "]]"
4687 "NESTC" "{" "NET" "}" "OPT" "?" "OR" "|" "PERO" "%" "PIC" ">"
4688 "PIO" "<?" "PLUS" "+" "REFC" "." "REP" "*" "RNI" "#" "SEQ" ","
4689 "STAGO" ":" "TAGC" "." "VI" "=" "MS-START" "<![" "MS-END" "]]>"
4690 "XML-ECOM" "-->" "XML-PIC" "?>" "XML-SCOM" "<!--" "XML-TAGCE"
4692 (setq major-mode 'mdw-sgml-mode)
4693 (setq mode-name "[mdw] SGML")
4694 (run-hooks 'mdw-sgml-mode-hook))
4696 ;;;--------------------------------------------------------------------------
4697 ;;; Configuration files.
4699 (defcustom mdw-conf-quote-normal nil
4700 "Control syntax category of quote characters `\"' and `''.
4701 If this is `t', consider quote characters to be normal
4702 punctuation, as for `conf-quote-normal'. If this is `nil' then
4703 leave quote characters as quotes. If this is a list, then
4704 consider the quote characters in the list to be normal
4705 punctuation. If this is a single quote character, then consider
4706 that character only to be normal punctuation."
4707 :type '(choice boolean character (repeat character))
4708 :safe 'mdw-conf-quote-normal-acceptable-value-p)
4709 (defun mdw-conf-quote-normal-acceptable-value-p (value)
4710 "Is the VALUE is an acceptable value for `mdw-conf-quote-normal'?"
4711 (or (booleanp value)
4712 (cl-every (lambda (v) (memq v '(?\" ?')))
4713 (if (listp value) value (list value)))))
4715 (defun mdw-fix-up-quote ()
4716 "Apply the setting of `mdw-conf-quote-normal'."
4717 (let ((flag mdw-conf-quote-normal))
4719 (conf-quote-normal t))
4723 (let ((table (copy-syntax-table (syntax-table))))
4724 (dolist (ch (if (listp flag) flag (list flag)))
4725 (modify-syntax-entry ch "." table))
4726 (set-syntax-table table)
4727 (and font-lock-mode (font-lock-fontify-buffer)))))))
4730 (add-hook 'conf-mode-hook 'mdw-misc-mode-config t)
4731 (add-hook 'conf-mode-local-variables-hook 'mdw-fix-up-quote t t))
4733 ;;;--------------------------------------------------------------------------
4736 (defun mdw-setup-sh-script-mode ()
4738 ;; Fetch the shell interpreter's name.
4739 (let ((shell-name sh-shell-file))
4741 ;; Try reading the hash-bang line.
4743 (goto-char (point-min))
4744 (if (looking-at "#![ \t]*\\([^ \t\n]*\\)")
4745 (setq shell-name (match-string 1))))
4747 ;; Now try to set the shell.
4749 ;; Don't let `sh-set-shell' bugger up my script.
4750 (let ((executable-set-magic #'(lambda (s &rest r) s)))
4751 (sh-set-shell shell-name)))
4753 ;; Don't insert here-document scaffolding automatically.
4754 (local-set-key "<" 'self-insert-command)
4756 ;; Now enable my keys and the fontification.
4757 (mdw-misc-mode-config)
4759 ;; Set the indentation level correctly.
4760 (setq sh-indentation 2)
4761 (setq sh-basic-offset 2))
4763 (setq sh-shell-file "/bin/sh")
4765 ;; Awful hacking to override the shell detection for particular scripts.
4766 (defmacro define-custom-shell-mode (name shell)
4769 (set (make-local-variable 'sh-shell-file) ,shell)
4771 (define-custom-shell-mode bash-mode "/bin/bash")
4772 (define-custom-shell-mode rc-mode "/usr/bin/rc")
4773 (put 'sh-shell-file 'permanent-local t)
4775 ;; Hack the rc syntax table. Backquotes aren't paired in rc.
4776 (eval-after-load "sh-script"
4777 '(or (assq 'rc sh-mode-syntax-table-input)
4794 (assoc (assq 'rc sh-mode-syntax-table-input)))
4797 (setq sh-mode-syntax-table-input
4798 (cons (cons 'rc frag)
4799 sh-mode-syntax-table-input))))))
4802 (add-hook 'sh-mode-hook 'mdw-misc-mode-config t)
4803 (add-hook 'sh-mode-hook 'mdw-setup-sh-script-mode t))
4805 ;;;--------------------------------------------------------------------------
4806 ;;; Emacs shell mode.
4808 (defun mdw-eshell-prompt ()
4809 (let ((left "[") (right "]"))
4810 (when (= (user-uid) 0)
4811 (setq left "«" right "»"))
4814 (replace-regexp-in-string "\\..*$" "" (system-name)))
4816 (let* ((pwd (eshell/pwd)) (npwd (length pwd))
4817 (home (expand-file-name "~")) (nhome (length home)))
4818 (if (and (>= npwd nhome)
4820 (= (elt pwd nhome) ?/))
4821 (string= (substring pwd 0 nhome) home))
4822 (concat "~" (substring pwd (length home)))
4825 (setq-default eshell-prompt-function 'mdw-eshell-prompt)
4826 (setq-default eshell-prompt-regexp "^\\[[^]>]+\\(\\]\\|>>?\\)")
4828 (defun eshell/e (file) (find-file file) nil)
4829 (defun eshell/ee (file) (find-file-other-window file) nil)
4830 (defun eshell/w3m (url) (w3m-goto-url url) nil)
4832 (mdw-define-face eshell-prompt (t :weight bold))
4833 (mdw-define-face eshell-ls-archive (t :weight bold :foreground "red"))
4834 (mdw-define-face eshell-ls-backup (t :foreground "lightgrey" :slant italic))
4835 (mdw-define-face eshell-ls-product (t :foreground "lightgrey" :slant italic))
4836 (mdw-define-face eshell-ls-clutter (t :foreground "lightgrey" :slant italic))
4837 (mdw-define-face eshell-ls-executable (t :weight bold))
4838 (mdw-define-face eshell-ls-directory (t :foreground "cyan" :weight bold))
4839 (mdw-define-face eshell-ls-readonly (t nil))
4840 (mdw-define-face eshell-ls-symlink (t :foreground "cyan"))
4842 (defun mdw-eshell-hack () (setenv "LD_PRELOAD" nil))
4843 (add-hook 'eshell-mode-hook 'mdw-eshell-hack)
4845 ;;;--------------------------------------------------------------------------
4846 ;;; Messages-file mode.
4848 (defun messages-mode-guts ()
4849 (setq messages-mode-syntax-table (make-syntax-table))
4850 (set-syntax-table messages-mode-syntax-table)
4851 (modify-syntax-entry ?0 "w" messages-mode-syntax-table)
4852 (modify-syntax-entry ?1 "w" messages-mode-syntax-table)
4853 (modify-syntax-entry ?2 "w" messages-mode-syntax-table)
4854 (modify-syntax-entry ?3 "w" messages-mode-syntax-table)
4855 (modify-syntax-entry ?4 "w" messages-mode-syntax-table)
4856 (modify-syntax-entry ?5 "w" messages-mode-syntax-table)
4857 (modify-syntax-entry ?6 "w" messages-mode-syntax-table)
4858 (modify-syntax-entry ?7 "w" messages-mode-syntax-table)
4859 (modify-syntax-entry ?8 "w" messages-mode-syntax-table)
4860 (modify-syntax-entry ?9 "w" messages-mode-syntax-table)
4861 (make-local-variable 'comment-start)
4862 (make-local-variable 'comment-end)
4863 (make-local-variable 'indent-line-function)
4864 (setq indent-line-function 'indent-relative)
4865 (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
4866 (make-local-variable 'font-lock-defaults)
4867 (make-local-variable 'messages-mode-keywords)
4869 (mdw-regexps "array" "bitmap" "callback" "docs[ \t]+enum"
4870 "export" "enum" "fixed-octetstring" "flags"
4871 "harmless" "map" "nested" "optional"
4872 "optional-tagged" "package" "primitive"
4873 "primitive-nullfree" "relaxed[ \t]+enum"
4874 "set" "table" "tagged-optional" "union"
4875 "variadic" "vector" "version" "version-tag")))
4876 (setq messages-mode-keywords
4878 (list (concat "\\<\\(" keywords "\\)\\>:")
4879 '(0 font-lock-keyword-face))
4880 '("\\([-a-zA-Z0-9]+:\\)" (0 font-lock-warning-face))
4881 '("\\(\\<[a-z][-_a-zA-Z0-9]*\\)"
4882 (0 font-lock-variable-name-face))
4883 '("\\<\\([0-9]+\\)\\>" (0 mdw-number-face))
4884 '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4885 (0 mdw-punct-face)))))
4886 (setq font-lock-defaults
4887 '(messages-mode-keywords nil nil nil nil))
4888 (run-hooks 'messages-file-hook))
4890 (defun messages-mode ()
4893 (setq major-mode 'messages-mode)
4894 (setq mode-name "Messages")
4895 (messages-mode-guts)
4896 (modify-syntax-entry ?# "<" messages-mode-syntax-table)
4897 (modify-syntax-entry ?\n ">" messages-mode-syntax-table)
4898 (setq comment-start "# ")
4899 (setq comment-end "")
4900 (run-hooks 'messages-mode-hook))
4902 (defun cpp-messages-mode ()
4905 (setq major-mode 'cpp-messages-mode)
4906 (setq mode-name "CPP Messages")
4907 (messages-mode-guts)
4908 (modify-syntax-entry ?* ". 23" messages-mode-syntax-table)
4909 (modify-syntax-entry ?/ ". 14" messages-mode-syntax-table)
4910 (setq comment-start "/* ")
4911 (setq comment-end " */")
4912 (let ((preprocessor-keywords
4913 (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
4914 "ident" "if" "ifdef" "ifndef" "import" "include"
4915 "line" "pragma" "unassert" "undef" "warning")))
4916 (setq messages-mode-keywords
4917 (append (list (list (concat "^[ \t]*\\#[ \t]*"
4918 "\\(include\\|import\\)"
4919 "[ \t]*\\(<[^>]+\\(>\\)?\\)")
4920 '(2 font-lock-string-face))
4921 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
4922 preprocessor-keywords
4923 "\\)\\>\\|[0-9]+\\|$\\)\\)")
4924 '(1 font-lock-keyword-face)))
4925 messages-mode-keywords)))
4926 (run-hooks 'cpp-messages-mode-hook))
4929 (add-hook 'messages-mode-hook 'mdw-misc-mode-config t)
4930 (add-hook 'cpp-messages-mode-hook 'mdw-misc-mode-config t)
4931 ;; (add-hook 'messages-file-hook 'mdw-fontify-messages t)
4934 ;;;--------------------------------------------------------------------------
4935 ;;; Messages-file mode.
4937 (defvar mallow-driver-substitution-face 'mallow-driver-substitution-face
4938 "Face to use for subsittution directives.")
4939 (make-face 'mallow-driver-substitution-face)
4940 (defvar mallow-driver-text-face 'mallow-driver-text-face
4941 "Face to use for body text.")
4942 (make-face 'mallow-driver-text-face)
4944 (defun mallow-driver-mode ()
4947 (setq major-mode 'mallow-driver-mode)
4948 (setq mode-name "Mallow driver")
4949 (setq mallow-driver-mode-syntax-table (make-syntax-table))
4950 (set-syntax-table mallow-driver-mode-syntax-table)
4951 (make-local-variable 'comment-start)
4952 (make-local-variable 'comment-end)
4953 (make-local-variable 'indent-line-function)
4954 (setq indent-line-function 'indent-relative)
4955 (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
4956 (make-local-variable 'font-lock-defaults)
4957 (make-local-variable 'mallow-driver-mode-keywords)
4959 (mdw-regexps "each" "divert" "file" "if"
4960 "perl" "set" "string" "type" "write")))
4961 (setq mallow-driver-mode-keywords
4963 (list (concat "^%\\s *\\(}\\|\\(" keywords "\\)\\>\\).*$")
4964 '(0 font-lock-keyword-face))
4965 (list "^%\\s *\\(#.*\\)?$"
4966 '(0 font-lock-comment-face))
4968 '(0 font-lock-keyword-face))
4969 (list "^|?\\(.+\\)$" '(1 mallow-driver-text-face))
4971 '(0 mallow-driver-substitution-face t)))))
4972 (setq font-lock-defaults
4973 '(mallow-driver-mode-keywords nil nil nil nil))
4974 (modify-syntax-entry ?\" "_" mallow-driver-mode-syntax-table)
4975 (modify-syntax-entry ?\n ">" mallow-driver-mode-syntax-table)
4976 (setq comment-start "%# ")
4977 (setq comment-end "")
4978 (run-hooks 'mallow-driver-mode-hook))
4981 (add-hook 'mallow-driver-hook 'mdw-misc-mode-config t))
4983 ;;;--------------------------------------------------------------------------
4986 (defun nfast-debug-mode ()
4989 (setq major-mode 'nfast-debug-mode)
4990 (setq mode-name "NFast debug")
4991 (setq messages-mode-syntax-table (make-syntax-table))
4992 (set-syntax-table messages-mode-syntax-table)
4993 (make-local-variable 'font-lock-defaults)
4994 (make-local-variable 'nfast-debug-mode-keywords)
4995 (setq truncate-lines t)
4996 (setq nfast-debug-mode-keywords
4998 '("^\\(NFast_\\(Connect\\|Disconnect\\|Submit\\|Wait\\)\\)"
4999 (0 font-lock-keyword-face))
5000 (list (concat "^[ \t]+\\(\\("
5001 "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
5002 "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
5004 "[0-9a-fA-F]+\\)[ \t]*$")
5005 '(0 mdw-number-face))
5006 '("^[ \t]+\.status=[ \t]+\\<\\(OK\\)\\>"
5007 (1 font-lock-keyword-face))
5008 '("^[ \t]+\.status=[ \t]+\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>"
5009 (1 font-lock-warning-face))
5010 '("^[ \t]+\.status[ \t]+\\<\\(zero\\)\\>"
5012 (list (concat "^[ \t]+\\.cmd=[ \t]+"
5013 "\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>")
5014 '(1 font-lock-keyword-face))
5015 '("-?\\<\\([0-9]+\\|0x[0-9a-fA-F]+\\)\\>" (0 mdw-number-face))
5016 '("^\\([ \t]+[a-z0-9.]+\\)" (0 font-lock-variable-name-face))
5017 '("\\<\\([a-z][a-z0-9.]+\\)\\>=" (1 font-lock-variable-name-face))
5018 '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" (0 mdw-punct-face))))
5019 (setq font-lock-defaults
5020 '(nfast-debug-mode-keywords nil nil nil nil))
5021 (run-hooks 'nfast-debug-mode-hook))
5023 ;;;--------------------------------------------------------------------------
5024 ;;; Lispy languages.
5026 ;; Unpleasant bodge.
5027 (unless (boundp 'slime-repl-mode-map)
5028 (setq slime-repl-mode-map (make-sparse-keymap)))
5030 (defun mdw-indent-newline-and-indent ()
5032 (indent-for-tab-command)
5033 (newline-and-indent))
5035 (eval-after-load "cl-indent"
5037 (mapc #'(lambda (pair)
5039 'common-lisp-indent-function
5041 '((destructuring-bind . ((&whole 4 &rest 1) 4 &body))
5042 (multiple-value-bind . ((&whole 4 &rest 1) 4 &body))))))
5044 (defun mdw-common-lisp-indent ()
5045 (make-local-variable 'lisp-indent-function)
5046 (setq lisp-indent-function 'common-lisp-indent-function))
5048 (defmacro mdw-advise-hyperspec-lookup (func args)
5049 `(defadvice ,func (around mdw-browse-w3m ,args activate compile)
5051 (let ((browse-url-browser-function #'mdw-w3m-browse-url))
5054 (mdw-advise-hyperspec-lookup common-lisp-hyperspec (symbol))
5055 (mdw-advise-hyperspec-lookup common-lisp-hyperspec-format (char))
5056 (mdw-advise-hyperspec-lookup common-lisp-hyperspec-lookup-reader-macro (char))
5058 (defun mdw-fontify-lispy ()
5061 (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)")
5063 ;; Not much fontification needed.
5064 (make-local-variable 'font-lock-keywords)
5065 (setq font-lock-keywords
5066 (list (list (concat "\\("
5068 "\\(" "[0-9]+/[0-9]+"
5069 "\\|" "\\(" "[0-9]+" "\\(\\.[0-9]*\\)?" "\\|"
5071 "\\([dDeEfFlLsS][-+]?[0-9]+\\)?"
5076 "[0-9A-Fa-f]+" "\\(/[0-9A-Fa-f]+\\)?"
5077 "\\|" "o" "[-+]?" "[0-7]+" "\\(/[0-7]+\\)?"
5078 "\\|" "b" "[-+]?" "[01]+" "\\(/[01]+\\)?"
5079 "\\|" "[0-9]+" "r" "[-+]?"
5080 "[0-9a-zA-Z]+" "\\(/[0-9a-zA-Z]+\\)?"
5083 '(0 mdw-number-face))
5084 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
5085 '(0 mdw-punct-face)))))
5087 ;; Special indentation.
5089 (defcustom mdw-lisp-loop-default-indent 2
5090 "Default indent for simple `loop' body."
5093 (defcustom mdw-lisp-setf-value-indent 2
5094 "Default extra indent for `setf' values."
5095 :type 'integer :safe 'integerp)
5097 (setq lisp-simple-loop-indentation 0
5098 lisp-loop-keyword-indentation 0
5099 lisp-loop-forms-indentation 2
5100 lisp-lambda-list-keyword-parameter-alignment t)
5102 (defun mdw-indent-funcall
5103 (path state &optional indent-point sexp-column normal-indent)
5104 "Indent `funcall' more usefully.
5105 Essentially, treat `funcall foo' as a function name, and align the arguments
5107 (and (or (not (consp path)) (null (cadr path)))
5109 (goto-char (cadr state))
5111 (let ((start-line (line-number-at-pos)))
5112 (and (condition-case nil (progn (forward-sexp 3) t)
5116 (and (= start-line (line-number-at-pos))
5117 (current-column))))))))
5119 (put 'funcall 'common-lisp-indent-function 'mdw-indent-funcall)
5120 (put 'funcall 'lisp-indent-function 'mdw-indent-funcall))
5122 (defun mdw-indent-setf
5123 (path state &optional indent-point sexp-column normal-indent)
5124 "Indent `setf' more usefully.
5125 If the values aren't on the same lines as their variables then indent them
5126 by `mdw-lisp-setf-value-indent' spaces."
5127 (and (or (not (consp path)) (null (cadr path)))
5128 (let ((basic-indent (save-excursion
5129 (goto-char (cadr state))
5131 (and (condition-case nil
5132 (progn (forward-sexp 2) t)
5136 (current-column)))))
5137 (offset (if (consp path) (car path)
5142 (goto-char (cadr state))
5144 (while (< (point) start)
5145 (condition-case nil (forward-sexp 1)
5146 (scan-error (throw 'done nil)))
5149 (and basic-indent offset
5150 (list (+ basic-indent
5151 (if (cl-oddp offset) 0
5152 mdw-lisp-setf-value-indent))
5155 (put 'setf 'common-lisp-indent-functopion 'mdw-indent-setf)
5156 (put 'psetf 'common-lisp-indent-function 'mdw-indent-setf)
5157 (put 'setq 'common-lisp-indent-function 'mdw-indent-setf)
5158 (put 'setf 'lisp-indent-function 'mdw-indent-setf)
5159 (put 'setq 'lisp-indent-function 'mdw-indent-setf)
5160 (put 'setq-local 'lisp-indent-function 'mdw-indent-setf)
5161 (put 'setq-default 'lisp-indent-function 'mdw-indent-setf))
5163 (defadvice common-lisp-loop-part-indentation
5164 (around mdw-fix-loop-indentation (indent-point state) activate compile)
5165 "Improve `loop' indentation.
5166 If the first subform is on the same line as the `loop' keyword, then
5167 align the other subforms beneath it. Otherwise, indent them
5168 `mdw-lisp-loop-default-indent' columns in from the opening parenthesis."
5170 (let* ((loop-indentation (save-excursion
5171 (goto-char (elt state 1))
5174 ;; Don't really care about this.
5175 (when (and (boundp 'lisp-indent-backquote-substitution-mode)
5176 (eq lisp-indent-backquote-substitution-mode 'corrected))
5178 (goto-char (elt state 1))
5179 (cl-incf loop-indentation
5180 (cond ((eq (char-before) ?,) -1)
5181 ((and (eq (char-before) ?@)
5182 (progn (backward-char)
5183 (eq (char-before) ?,)))
5187 ;; If the first loop item is on the same line as the `loop' itself then
5188 ;; use that as the baseline. Otherwise advance by the default indent.
5189 (goto-char (cadr state))
5191 (let ((baseline-indent
5192 (if (= (line-number-at-pos)
5193 (if (condition-case nil (progn (forward-sexp 2) t)
5195 (progn (forward-sexp -1) (line-number-at-pos))
5198 (+ loop-indentation mdw-lisp-loop-default-indent))))
5200 (goto-char indent-point)
5203 (setq ad-return-value
5205 (cond ((condition-case ()
5207 (goto-char (elt state 1))
5211 (not (looking-at "\\(:\\|\\sw\\)")))
5213 (+ baseline-indent lisp-simple-loop-indentation))
5214 ((looking-at "^\\s-*\\(:?\\sw+\\|;\\)")
5215 (+ baseline-indent lisp-loop-keyword-indentation))
5217 (+ baseline-indent lisp-loop-forms-indentation)))
5219 ;; Tell the caller that the next line needs recomputation,
5220 ;; even though it doesn't start a sexp.
5221 loop-indentation)))))
5225 (defcustom mdw-friendly-name "[mdw]"
5226 "How I want to be addressed."
5229 (defadvice slime-user-first-name
5230 (around mdw-use-friendly-name compile activate)
5231 (if mdw-friendly-name (setq ad-return-value mdw-friendly-name)
5236 (if (not mdw-fast-startup)
5238 (require 'slime-autoloads)
5239 (slime-setup '(slime-autodoc slime-c-p-c))))))
5241 (let ((stuff '((cmucl ("cmucl"))
5242 (sbcl ("sbcl") :coding-system utf-8-unix)
5243 (clisp ("clisp") :coding-system utf-8-unix))))
5244 (or (boundp 'slime-lisp-implementations)
5245 (setq slime-lisp-implementations nil))
5247 (let* ((head (car stuff))
5248 (found (assq (car head) slime-lisp-implementations)))
5249 (setq stuff (cdr stuff))
5251 (rplacd found (cdr head))
5252 (setq slime-lisp-implementations
5253 (cons head slime-lisp-implementations))))))
5254 (setq slime-default-lisp 'sbcl)
5259 (dolist (hook '(emacs-lisp-mode-hook
5262 inferior-lisp-mode-hook
5263 lisp-interaction-mode-hook
5265 slime-repl-mode-hook))
5266 (add-hook hook 'mdw-misc-mode-config t)
5267 (add-hook hook 'mdw-fontify-lispy t))
5268 (add-hook 'lisp-mode-hook 'mdw-common-lisp-indent t)
5269 (add-hook 'inferior-lisp-mode-hook
5270 #'(lambda () (local-set-key "\C-m" 'comint-send-and-indent)) t))
5272 ;;;--------------------------------------------------------------------------
5273 ;;; Other languages.
5277 (defun mdw-setup-smalltalk ()
5278 (and mdw-auto-indent
5279 (local-set-key "\C-m" 'smalltalk-newline-and-indent))
5280 (make-local-variable 'mdw-auto-indent)
5281 (setq mdw-auto-indent nil)
5282 (local-set-key "\C-i" 'smalltalk-reindent))
5284 (defun mdw-fontify-smalltalk ()
5285 (make-local-variable 'font-lock-keywords)
5286 (setq font-lock-keywords
5288 (list "\\<[A-Z][a-zA-Z0-9]*\\>"
5289 '(0 font-lock-keyword-face))
5290 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
5291 "[0-9][0-9_]*\\(\\.[0-9_]*\\)?"
5292 "\\([eE][-+]?[0-9_]+\\)?")
5293 '(0 mdw-number-face))
5294 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
5295 '(0 mdw-punct-face)))))
5298 (add-hook 'smalltalk-mode 'mdw-misc-mode-config t)
5299 (add-hook 'smalltalk-mode 'mdw-fontify-smalltalk t))
5303 (defun mdw-setup-m4 ()
5305 ;; Inexplicably, Emacs doesn't match braces in m4 mode. This is very
5306 ;; annoying: fix it.
5307 (modify-syntax-entry ?{ "(")
5308 (modify-syntax-entry ?} ")")
5311 (mdw-standard-fill-prefix "\\([ \t]*\\(?:#+\\|\\<dnl\\>\\)[ \t]*\\)"))
5313 (dolist (hook '(m4-mode-hook autoconf-mode-hook autotest-mode-hook))
5314 (add-hook hook #'mdw-misc-mode-config t)
5315 (add-hook hook #'mdw-setup-m4 t))
5320 (add-hook 'makefile-mode-hook 'mdw-misc-mode-config t))
5325 (add-hook 'nroff-mode-hook 'mdw-misc-mode-config t))
5327 ;;;--------------------------------------------------------------------------
5330 (defun mdw-text-mode ()
5331 (setq fill-column 72)
5333 (mdw-standard-fill-prefix
5334 "\\([ \t]*\\([>#|:] ?\\)*[ \t]*\\)" 3)
5337 (eval-after-load "flyspell"
5338 '(define-key flyspell-mode-map "\C-\M-i" nil))
5341 (add-hook 'text-mode-hook 'mdw-text-mode t))
5343 ;;;--------------------------------------------------------------------------
5344 ;;; Outline and hide/show modes.
5346 (defun mdw-outline-collapse-all ()
5347 "Completely collapse everything in the entire buffer."
5350 (goto-char (point-min))
5351 (while (< (point) (point-max))
5355 (setq hs-hide-comments-when-hiding-all nil)
5357 (defadvice hs-hide-all (after hide-first-comment activate)
5358 (save-excursion (hs-hide-initial-comment-block)))
5360 ;;;--------------------------------------------------------------------------
5363 (defun mdw-sh-mode-setup ()
5364 (local-set-key [?\C-a] 'comint-bol)
5365 (add-hook 'comint-output-filter-functions
5366 'comint-watch-for-password-prompt))
5368 (defun mdw-term-mode-setup ()
5369 (setq term-prompt-regexp shell-prompt-pattern)
5370 (make-local-variable 'mouse-yank-at-point)
5371 (make-local-variable 'transient-mark-mode)
5372 (setq mouse-yank-at-point t)
5376 (defun comint-send-and-indent ()
5379 (and mdw-auto-indent
5380 (indent-for-tab-command)))
5382 (defadvice comint-line-beginning-position
5383 (around mdw-calculate-it-properly () activate compile)
5384 "Calculate the actual line start for multi-line input."
5385 (if (or comint-use-prompt-regexp
5386 (eq (field-at-pos (point)) 'output))
5388 (setq ad-return-value
5389 (constrain-to-field (line-beginning-position) (point)))))
5391 (defun term-send-meta-right () (interactive) (term-send-raw-string "\e\e[C"))
5392 (defun term-send-meta-left () (interactive) (term-send-raw-string "\e\e[D"))
5393 (defun term-send-ctrl-uscore () (interactive) (term-send-raw-string "\C-_"))
5394 (defun term-send-meta-meta-something ()
5396 (term-send-raw-string "\e\e")
5398 (eval-after-load 'term
5400 (define-key term-raw-map [?\e ?\e] nil)
5401 (define-key term-raw-map [?\e ?\e t] 'term-send-meta-meta-something)
5402 (define-key term-raw-map [?\C-/] 'term-send-ctrl-uscore)
5403 (define-key term-raw-map [M-right] 'term-send-meta-right)
5404 (define-key term-raw-map [?\e ?\M-O ?C] 'term-send-meta-right)
5405 (define-key term-raw-map [M-left] 'term-send-meta-left)
5406 (define-key term-raw-map [?\e ?\M-O ?D] 'term-send-meta-left)))
5408 (defadvice term-exec (before program-args-list compile activate)
5409 "If the PROGRAM argument is a list, interpret it as (PROGRAM . SWITCHES).
5410 This allows you to pass a list of arguments through `ansi-term'."
5411 (let ((program (ad-get-arg 2)))
5414 (ad-set-arg 2 (car program))
5415 (ad-set-arg 4 (cdr program))))))
5417 (defadvice term-exec-1 (around hack-environment compile activate)
5418 "Hack the environment inherited by inferiors in the terminal."
5419 (let ((process-environment (copy-tree process-environment)))
5420 (setenv "LD_PRELOAD" nil)
5423 (defadvice shell (around hack-environment compile activate)
5424 "Hack the environment inherited by inferiors in the shell."
5425 (let ((process-environment (copy-tree process-environment)))
5426 (setenv "LD_PRELOAD" nil)
5430 "Open a terminal containing an ssh session to the HOST."
5431 (interactive "sHost: ")
5432 (ansi-term (list "ssh" host) (format "ssh@%s" host)))
5434 (defcustom git-grep-command
5435 "env GIT_PAGER=cat git grep --no-color -nH -e "
5436 "The default command for \\[git-grep]."
5439 (defvar git-grep-history nil)
5441 (defun git-grep (command-args)
5442 "Run `git grep' with user-specified args and collect output in a buffer."
5444 (list (read-shell-command "Run git grep (like this): "
5445 git-grep-command 'git-grep-history)))
5446 (let ((grep-use-null-device nil))
5447 (grep command-args)))
5449 ;;;--------------------------------------------------------------------------
5450 ;;; Magit configuration.
5452 (setq magit-diff-refine-hunk 't
5453 magit-view-git-manual-method 'man
5454 magit-log-margin '(nil age magit-log-margin-width t 18)
5455 magit-wip-after-save-local-mode-lighter ""
5456 magit-wip-after-apply-mode-lighter ""
5457 magit-wip-before-change-mode-lighter "")
5458 (eval-after-load "magit"
5459 '(progn (global-magit-file-mode 1)
5460 (magit-wip-after-save-mode 1)
5461 (magit-wip-after-apply-mode 1)
5462 (magit-wip-before-change-mode 1)
5463 (add-to-list 'magit-no-confirm 'safe-with-wip)
5464 (add-to-list 'magit-no-confirm 'trash)
5465 (push '(:eval (if (or magit-wip-after-save-local-mode
5466 magit-wip-after-apply-mode
5467 magit-wip-before-change-mode)
5468 (format " wip:%s%s%s"
5469 (if magit-wip-after-apply-mode "A" "")
5470 (if magit-wip-before-change-mode "C" "")
5471 (if magit-wip-after-save-local-mode "S" ""))))
5473 (dolist (popup '(magit-diff-popup
5474 magit-diff-refresh-popup
5475 magit-diff-mode-refresh-popup
5476 magit-revision-mode-refresh-popup))
5477 (magit-define-popup-switch popup ?R "Reverse diff" "-R"))
5478 (magit-define-popup-switch 'magit-rebase-popup ?r
5479 "Rebase merges" "--rebase-merges")))
5481 (defadvice magit-wip-commit-buffer-file
5482 (around mdw-just-this-buffer activate compile)
5483 (let ((magit-save-repository-buffers nil)) ad-do-it))
5485 (defadvice magit-discard
5486 (around mdw-delete-if-prefix-argument activate compile)
5487 (let ((magit-delete-by-moving-to-trash
5488 (and (null current-prefix-arg)
5489 magit-delete-by-moving-to-trash)))
5492 (setq magit-repolist-columns
5493 '(("Name" 16 magit-repolist-column-ident nil)
5494 ("Version" 18 magit-repolist-column-version nil)
5495 ("St" 2 magit-repolist-column-dirty nil)
5496 ("L<U" 3 mdw-repolist-column-unpulled-from-upstream nil)
5497 ("L>U" 3 mdw-repolist-column-unpushed-to-upstream nil)
5498 ("Path" 32 magit-repolist-column-path nil)))
5500 (setq magit-repository-directories '(("~/etc/profile" . 0)
5503 (defadvice magit-list-repos (around mdw-dirname () activate compile)
5504 "Make sure the returned names are directory names.
5505 Otherwise child processes get started in the wrong directory and
5507 (setq ad-return-value (mapcar #'file-name-as-directory ad-do-it)))
5509 (defun mdw-repolist-column-unpulled-from-upstream (_id)
5510 "Insert number of upstream commits not in the current branch."
5511 (let ((upstream (magit-get-upstream-branch (magit-get-current-branch) t)))
5513 (let ((n (cadr (magit-rev-diff-count "HEAD" upstream))))
5514 (propertize (number-to-string n) 'face
5515 (if (> n 0) 'bold 'shadow))))))
5517 (defun mdw-repolist-column-unpushed-to-upstream (_id)
5518 "Insert number of commits in the current branch but not its upstream."
5519 (let ((upstream (magit-get-upstream-branch (magit-get-current-branch) t)))
5521 (let ((n (car (magit-rev-diff-count "HEAD" upstream))))
5522 (propertize (number-to-string n) 'face
5523 (if (> n 0) 'bold 'shadow))))))
5525 (defun mdw-try-smerge ()
5527 (goto-char (point-min))
5528 (when (re-search-forward "^<<<<<<< " nil t)
5530 (add-hook 'find-file-hook 'mdw-try-smerge t)
5532 (defcustom mdw-magit-new-window-modes
5539 "Magit modes which should cause a new window to be used."
5540 :type '(repeat symbol))
5542 (defun mdw-display-magit-buffer (buffer)
5543 "Like `magit-display-buffer-traditional'.
5544 But uses `mdw-magit-new-window-modes' for its list of modes
5545 rather than baking the list into the function."
5546 (display-buffer buffer
5547 (let ((mode (with-current-buffer buffer major-mode)))
5548 (if (and (not mdw-designated-window)
5549 (derived-mode-p 'magit-mode)
5550 (mdw-submode-p mode 'magit-mode)
5551 (not (memq mode mdw-magit-new-window-modes)))
5552 '(display-buffer-same-window . nil)
5554 (setq magit-display-buffer-function 'mdw-display-magit-buffer)
5556 (defun mdw-display-magit-file-buffer (buffer)
5557 "Show a file buffer from a diff."
5558 (select-window (display-buffer buffer)))
5559 (setq magit-display-file-buffer-function 'mdw-display-magit-file-buffer)
5561 ;;;--------------------------------------------------------------------------
5562 ;;; GUD, and especially GDB.
5564 ;; Inhibit window dedication. I mean, seriously, wtf?
5565 (defadvice gdb-display-buffer (after mdw-undedicated (buf) compile activate)
5566 "Don't make windows dedicated. Seriously."
5567 (set-window-dedicated-p ad-return-value nil))
5568 (defadvice gdb-set-window-buffer
5569 (after mdw-undedicated (name &optional ignore-dedicated window)
5571 "Don't make windows dedicated. Seriously."
5572 (set-window-dedicated-p (or window (selected-window)) nil))
5574 (defadvice gud-find-expr
5575 (around mdw-inhibit-read-only (&rest args) compile activate)
5576 "Inhibit errors caused by my setting of `comint-prompt-read-only'."
5577 (let ((inhibit-read-only t)) ad-do-it))
5579 ;;;--------------------------------------------------------------------------
5582 (setq sql-postgres-options '("-n" "-P" "pager=off")
5583 sql-postgres-login-params
5584 '((user :default "mdw")
5585 (database :default "mdw")
5586 (server :default "db.distorted.org.uk")))
5588 ;;;--------------------------------------------------------------------------
5591 ;; Turn off `noip' when running `man': it interferes with `man-db''s own
5592 ;; seccomp(2)-based sandboxing, which is (in this case, at least) strictly
5594 (defadvice Man-getpage-in-background
5595 (around mdw-inhibit-noip (topic) compile activate)
5596 "Inhibit the `noip' preload hack when invoking `man'."
5597 (let* ((old-preload (getenv "LD_PRELOAD"))
5598 (preloads (and old-preload
5599 (save-match-data (split-string old-preload ":"))))
5604 (let ((item (pop preloads)))
5605 (if (string-match "\\(/\\|^\\)noip\.so\\(:\\|$\\)" item)
5607 (push item filtered)))))
5611 (setenv "LD_PRELOAD"
5613 (with-output-to-string
5614 (setq filtered (nreverse filtered))
5617 (if first (setq first nil)
5619 (write-string (pop filtered)))))))
5621 (setenv "LD_PRELOAD" old-preload))
5624 ;;;--------------------------------------------------------------------------
5625 ;;; MPC configuration.
5627 (eval-when-compile (trap (require 'mpc)))
5629 (setq mpc-browser-tags '(Artist|Composer|Performer Album|Playlist))
5631 (defun mdw-mpc-now-playing ()
5635 (set-buffer (mpc-proc-cmd (mpc-proc-cmd-list '("status" "currentsong"))))
5636 (mpc--status-callback))
5637 (let ((state (cdr (assq 'state mpc-status))))
5638 (cond ((member state '("stop"))
5639 (message "mpd stopped."))
5640 ((member state '("play" "pause"))
5641 (let* ((artist (cdr (assq 'Artist mpc-status)))
5642 (album (cdr (assq 'Album mpc-status)))
5643 (title (cdr (assq 'Title mpc-status)))
5644 (file (cdr (assq 'file mpc-status)))
5645 (duration-string (cdr (assq 'Time mpc-status)))
5646 (time-string (cdr (assq 'time mpc-status)))
5647 (time (and time-string
5649 (if (string-match ":" time-string)
5650 (substring time-string
5651 0 (match-beginning 0))
5653 (duration (and duration-string
5654 (string-to-number duration-string)))
5655 (pos (and time duration
5656 (format " [%d:%02d/%d:%02d]"
5657 (/ time 60) (mod time 60)
5658 (/ duration 60) (mod duration 60))))
5659 (fmt (cond ((and artist title)
5660 (format "`%s' by %s%s" title artist
5661 (if album (format ", from `%s'" album)
5664 (format "`%s' (no tags)" file))
5666 "(no idea what's playing!)"))))
5667 (if (string= state "play")
5668 (message "mpd playing %s%s" fmt (or pos ""))
5669 (message "mpd paused in %s%s" fmt (or pos "")))))
5671 (message "mpd in unknown state `%s'" state)))))
5673 (defmacro mdw-define-mpc-wrapper (func bvl interactive &rest body)
5675 (interactive ,@interactive)
5678 (mdw-mpc-now-playing)))
5680 (mdw-define-mpc-wrapper mdw-mpc-play-or-pause () nil
5681 (if (member (cdr (assq 'state (mpc-cmd-status))) '("play"))
5685 (mdw-define-mpc-wrapper mdw-mpc-next () nil (mpc-next))
5686 (mdw-define-mpc-wrapper mdw-mpc-prev () nil (mpc-prev))
5687 (mdw-define-mpc-wrapper mdw-mpc-stop () nil (mpc-stop))
5689 (defun mdw-mpc-louder (step)
5690 (interactive (list (if current-prefix-arg
5691 (prefix-numeric-value current-prefix-arg)
5693 (mpc-proc-cmd (format "volume %+d" step)))
5695 (defun mdw-mpc-quieter (step)
5696 (interactive (list (if current-prefix-arg
5697 (prefix-numeric-value current-prefix-arg)
5699 (mpc-proc-cmd (format "volume %+d" (- step))))
5701 (defun mdw-mpc-hack-lines (arg interactivep func)
5702 (if (and interactivep (use-region-p))
5703 (let ((from (region-beginning)) (to (region-end)))
5708 (while (< (point) to)
5711 (let ((n (prefix-numeric-value arg)))
5712 (cond ((cl-minusp n)
5717 (while (cl-minusp n)
5728 (defun mdw-mpc-select-one ()
5729 (when (and (get-char-property (point) 'mpc-file)
5730 (not (get-char-property (point) 'mpc-select)))
5731 (mpc-select-toggle)))
5733 (defun mdw-mpc-unselect-one ()
5734 (when (get-char-property (point) 'mpc-select)
5735 (mpc-select-toggle)))
5737 (defun mdw-mpc-select (&optional arg interactivep)
5738 (interactive (list current-prefix-arg t))
5739 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
5741 (defun mdw-mpc-unselect (&optional arg interactivep)
5742 (interactive (list current-prefix-arg t))
5743 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-unselect-one))
5745 (defun mdw-mpc-unselect-backwards (arg)
5747 (mdw-mpc-hack-lines (- arg) t 'mdw-mpc-unselect-one))
5749 (defun mdw-mpc-unselect-all ()
5751 (setq mpc-select nil)
5752 (mpc-selection-refresh))
5754 (defun mdw-mpc-next-line (arg)
5759 (defun mdw-mpc-previous-line (arg)
5762 (forward-line (- arg)))
5764 (defun mdw-mpc-playlist-add (&optional arg interactivep)
5765 (interactive (list current-prefix-arg t))
5766 (let ((mpc-select mpc-select))
5767 (when (or arg (and interactivep (use-region-p)))
5768 (setq mpc-select nil)
5769 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
5770 (setq mpc-select (reverse mpc-select))
5771 (mpc-playlist-add)))
5773 (defun mdw-mpc-playlist-delete (&optional arg interactivep)
5774 (interactive (list current-prefix-arg t))
5775 (setq mpc-select (nreverse mpc-select))
5777 (when (or arg (and interactivep (use-region-p)))
5778 (setq mpc-select nil)
5779 (mpc-selection-refresh)
5780 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
5781 (mpc-playlist-delete)))
5783 (defun mdw-mpc-hack-tagbrowsers ()
5784 (setq-local mode-line-format
5786 mode-line-frame-identification
5787 mode-line-buffer-identification)))
5788 (add-hook 'mpc-tagbrowser-mode-hook 'mdw-mpc-hack-tagbrowsers)
5790 (defun mdw-mpc-hack-songs ()
5791 (setq-local header-line-format
5792 ;; '("MPC " mpc-volume " " mpc-current-song)
5793 (list (propertize " " 'display '(space :align-to 0))
5794 ;; 'mpc-songs-format-description
5796 (let ((deactivate-mark) (hscroll (window-hscroll)))
5798 (mpc-format mpc-songs-format 'self hscroll)
5799 ;; That would be simpler than the hscroll handling in
5800 ;; mpc-format, but currently move-to-column does not
5801 ;; recognize :space display properties.
5802 ;; (move-to-column hscroll)
5803 ;; (delete-region (point-min) (point))
5804 (buffer-string)))))))
5805 (add-hook 'mpc-songs-mode-hook 'mdw-mpc-hack-songs)
5807 (eval-after-load "mpc"
5809 (define-key mpc-mode-map "m" 'mdw-mpc-select)
5810 (define-key mpc-mode-map "u" 'mdw-mpc-unselect)
5811 (define-key mpc-mode-map "\177" 'mdw-mpc-unselect-backwards)
5812 (define-key mpc-mode-map "\e\177" 'mdw-mpc-unselect-all)
5813 (define-key mpc-mode-map "n" 'mdw-mpc-next-line)
5814 (define-key mpc-mode-map "p" 'mdw-mpc-previous-line)
5815 (define-key mpc-mode-map "/" 'mpc-songs-search)
5816 (setq mpc-songs-mode-map (make-sparse-keymap))
5817 (set-keymap-parent mpc-songs-mode-map mpc-mode-map)
5818 (define-key mpc-songs-mode-map "l" 'mpc-playlist)
5819 (define-key mpc-songs-mode-map "+" 'mdw-mpc-playlist-add)
5820 (define-key mpc-songs-mode-map "-" 'mdw-mpc-playlist-delete)
5821 (define-key mpc-songs-mode-map "\r" 'mpc-songs-jump-to)))
5823 ;;;--------------------------------------------------------------------------
5824 ;;; Inferior Emacs Lisp.
5826 (setq comint-prompt-read-only t)
5828 (eval-after-load "comint"
5830 (define-key comint-mode-map "\C-w" 'comint-kill-region)
5831 (define-key comint-mode-map [C-S-backspace] 'comint-kill-whole-line)))
5833 (eval-after-load "ielm"
5835 (define-key ielm-map "\C-w" 'comint-kill-region)
5836 (define-key ielm-map [C-S-backspace] 'comint-kill-whole-line)))
5838 ;;;----- That's all, folks --------------------------------------------------
5840 (provide 'dot-emacs)