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.
27 (defun mdw-check-command-line-switch (switch)
28 (let ((probe nil) (next command-line-args) (found nil))
30 (cond ((string= (car next) switch)
32 (if probe (rplacd probe (cdr next))
33 (setq command-line-args (cdr next))))
36 (setq next (cdr next)))
39 (defvar mdw-fast-startup nil
40 "Whether .emacs should optimize for rapid startup.
41 This may be at the expense of cool features.")
42 (setq mdw-fast-startup
43 (mdw-check-command-line-switch "--mdw-fast-startup"))
45 (defvar mdw-splashy-startup nil
46 "Whether to show a splash screen and related frippery.")
47 (setq mdw-splashy-startup
48 (mdw-check-command-line-switch "--mdw-splashy-startup"))
50 ;;;--------------------------------------------------------------------------
51 ;;; Some general utilities.
54 (unless (fboundp 'make-regexp)
58 (defmacro mdw-regexps (&rest list)
59 "Turn a LIST of strings into a single regular expression at compile-time."
62 `',(make-regexp list))
65 "This is not the key sequence you're looking for."
67 (error "wrong button"))
69 (defun mdw-emacs-version-p (major &optional minor)
70 "Return non-nil if the running Emacs is at least version MAJOR.MINOR."
71 (or (> emacs-major-version major)
72 (and (= emacs-major-version major)
73 (>= emacs-minor-version (or minor 0)))))
75 ;; Some error trapping.
77 ;; If individual bits of this file go tits-up, we don't particularly want
78 ;; the whole lot to stop right there and then, because it's bloody annoying.
80 (defmacro trap (&rest forms)
81 "Execute FORMS without allowing errors to propagate outside."
85 ,(if (cdr forms) (cons 'progn forms) (car forms))
86 (error (message "Error (trapped): %s in %s"
87 (error-message-string err)
90 ;; Configuration reading.
92 (defvar mdw-config nil)
93 (defun mdw-config (sym)
94 "Read the configuration variable named SYM."
97 (flet ((replace (what with)
98 (goto-char (point-min))
99 (while (re-search-forward what nil t)
100 (replace-match with t))))
102 (insert-file-contents "~/.mdw.conf")
103 (replace "^[ \t]*\\(#.*\\)?\n" "")
104 (replace (concat "^[ \t]*"
105 "\\([-a-zA-Z0-9_.]*\\)"
108 "[ \t]**\\(\n\\|$\\)")
110 (car (read-from-string
111 (concat "(" (buffer-string) ")")))))))
112 (cdr (assq sym mdw-config)))
114 ;; Width configuration.
116 (defvar mdw-column-width
117 (string-to-number (or (mdw-config 'emacs-width) "77"))
118 "Width of Emacs columns.")
119 (defvar mdw-text-width mdw-column-width
120 "Expected width of text within columns.")
121 (put 'mdw-text-width 'safe-local-variable 'integerp)
123 ;; Local variables hacking.
125 (defun run-local-vars-mode-hook ()
126 "Run a hook for the major-mode after local variables have been processed."
127 (run-hooks (intern (concat (symbol-name major-mode)
128 "-local-variables-hook"))))
129 (add-hook 'hack-local-variables-hook 'run-local-vars-mode-hook)
131 ;; Set up the load path convincingly.
133 (dolist (dir (append (and (boundp 'debian-emacs-flavor)
134 (list (concat "/usr/share/"
135 (symbol-name debian-emacs-flavor)
137 (dolist (sub (directory-files dir t))
138 (when (and (file-accessible-directory-p sub)
139 (not (member sub load-path)))
140 (setq load-path (nconc load-path (list sub))))))
142 ;; Is an Emacs library available?
144 (defun library-exists-p (name)
145 "Return non-nil if NAME is an available library.
146 Return non-nil if NAME.el (or NAME.elc) somewhere on the Emacs
147 load path. The non-nil value is the filename we found for the
149 (let ((path load-path) elt (foundp nil))
150 (while (and path (not foundp))
151 (setq elt (car path))
152 (setq path (cdr path))
153 (setq foundp (or (let ((file (concat elt "/" name ".elc")))
154 (and (file-exists-p file) file))
155 (let ((file (concat elt "/" name ".el")))
156 (and (file-exists-p file) file)))))
159 (defun maybe-autoload (symbol file &optional docstring interactivep type)
160 "Set an autoload if the file actually exists."
161 (and (library-exists-p file)
162 (autoload symbol file docstring interactivep type)))
164 (defun mdw-kick-menu-bar (&optional frame)
165 "Regenerate FRAME's menu bar so it doesn't have empty menus."
167 (unless frame (setq frame (selected-frame)))
168 (let ((old (frame-parameter frame 'menu-bar-lines)))
169 (set-frame-parameter frame 'menu-bar-lines 0)
170 (set-frame-parameter frame 'menu-bar-lines old)))
174 (defun mdw-fixup-page-position ()
175 (unless (eq (char-before (point)) ?
\f)
178 (defadvice backward-page (after mdw-fixup compile activate)
179 (mdw-fixup-page-position))
180 (defadvice forward-page (after mdw-fixup compile activate)
181 (mdw-fixup-page-position))
183 ;; Splitting windows.
185 (unless (fboundp 'scroll-bar-columns)
186 (defun scroll-bar-columns (side)
187 (cond ((eq side 'left) 0)
190 (unless (fboundp 'fringe-columns)
191 (defun fringe-columns (side)
192 (cond ((not window-system) 0)
196 (defun mdw-horizontal-window-overhead ()
197 "Computes the horizontal window overhead.
198 This is the number of columns used by fringes, scroll bars and other such
200 (if (not window-system)
203 (dolist (what '(scroll-bar fringe))
204 (dolist (side '(left right))
205 (incf tot (funcall (intern (concat (symbol-name what) "-columns"))
209 (defun mdw-split-window-horizontally (&optional width)
210 "Split a window horizontally.
211 Without a numeric argument, split the window approximately in
212 half. With a numeric argument WIDTH, allocate WIDTH columns to
213 the left-hand window (if positive) or -WIDTH columns to the
214 right-hand window (if negative). Space for scroll bars and
215 fringes is not taken out of the allowance for WIDTH, unlike
216 \\[split-window-horizontally]."
218 (split-window-horizontally
219 (cond ((null width) nil)
220 ((>= width 0) (+ width (mdw-horizontal-window-overhead)))
221 ((< width 0) width))))
223 (defun mdw-preferred-column-width ()
224 "Return the preferred column width."
225 (if (and window-system (mdw-emacs-version-p 22)) mdw-column-width
226 (1+ mdw-column-width)))
228 (defun mdw-divvy-window (&optional width)
229 "Split a wide window into appropriate widths."
231 (setq width (if width (prefix-numeric-value width)
232 (mdw-preferred-column-width)))
233 (let* ((win (selected-window))
234 (sb-width (mdw-horizontal-window-overhead))
235 (c (/ (+ (window-width) sb-width)
236 (+ width sb-width))))
239 (split-window-horizontally (+ width sb-width))
241 (select-window win)))
243 (defun mdw-set-frame-width (columns &optional width)
244 (interactive "nColumns:
246 (setq width (if width (prefix-numeric-value width)
247 (mdw-preferred-column-width)))
248 (let ((sb-width (mdw-horizontal-window-overhead)))
249 (set-frame-width (selected-frame)
250 (- (* columns (+ width sb-width))
252 (mdw-divvy-window width)))
254 (defvar mdw-frame-width-fudge
255 (cond ((<= emacs-major-version 20) 1)
256 ((= emacs-major-version 26) 3)
258 "The number of extra columns to add to the desired frame width.
260 This is sadly necessary because Emacs 26 is broken in this regard.")
262 ;; Don't raise windows unless I say so.
264 (defvar mdw-inhibit-raise-frame nil
265 "*Whether `raise-frame' should do nothing when the frame is mapped.")
267 (defadvice raise-frame
268 (around mdw-inhibit (&optional frame) activate compile)
269 "Don't actually do anything if `mdw-inhibit-raise-frame' is true, and the
270 frame is actually mapped on the screen."
271 (if mdw-inhibit-raise-frame
272 (make-frame-visible frame)
275 (defmacro mdw-advise-to-inhibit-raise-frame (function)
276 "Advise the FUNCTION not to raise frames, even if it wants to."
277 `(defadvice ,function
278 (around mdw-inhibit-raise (&rest hunoz) activate compile)
279 "Don't raise the window unless you have to."
280 (let ((mdw-inhibit-raise-frame t))
283 (mdw-advise-to-inhibit-raise-frame select-frame-set-input-focus)
284 (mdw-advise-to-inhibit-raise-frame appt-disp-window)
285 (mdw-advise-to-inhibit-raise-frame mouse-select-window)
287 ;; Bug fix for markdown-mode, which breaks point positioning during
289 (defadvice markdown-check-change-for-wiki-link
290 (around mdw-save-match activate compile)
291 "Save match data around the `markdown-mode' `after-change-functions' hook."
292 (save-match-data ad-do-it))
294 ;; Bug fix for `bbdb-canonicalize-address': on Emacs 24, `run-hook-with-args'
295 ;; always returns nil, with the result that all email addresses are lost.
296 ;; Replace the function entirely.
297 (defadvice bbdb-canonicalize-address
298 (around mdw-bug-fix activate compile)
299 "Don't use `run-hook-with-args', because that doesn't work."
300 (let ((net (ad-get-arg 0)))
302 ;; Make sure this is a proper hook list.
303 (if (functionp bbdb-canonicalize-net-hook)
304 (setq bbdb-canonicalize-net-hook (list bbdb-canonicalize-net-hook)))
306 ;; Iterate over the hooks until things converge.
309 (let (next (changep nil)
310 hook (hooks bbdb-canonicalize-net-hook))
312 (setq hook (pop hooks))
313 (setq next (funcall hook net))
314 (if (not (equal next net))
317 (setq donep (not changep)))))
318 (setq ad-return-value net)))
320 ;; Transient mark mode hacks.
322 (defadvice exchange-point-and-mark
323 (around mdw-highlight (&optional arg) activate compile)
324 "Maybe don't actually exchange point and mark.
325 If `transient-mark-mode' is on and the mark is inactive, then
326 just activate it. A non-trivial prefix argument will force the
327 usual behaviour. A trivial prefix argument (i.e., just C-u) will
328 activate the mark and temporarily enable `transient-mark-mode' if
330 (cond ((or mark-active
331 (and (not transient-mark-mode) (not arg))
332 (and arg (or (not (consp arg))
333 (not (= (car arg) 4)))))
336 (or transient-mark-mode (setq transient-mark-mode 'only))
337 (set-mark (mark t)))))
339 ;; Functions for sexp diary entries.
341 (defun mdw-not-org-mode (form)
342 "As FORM, but not in Org mode agenda."
343 (and (not mdw-diary-for-org-mode-p)
346 (defun mdw-weekday (l)
347 "Return non-nil if `date' falls on one of the days of the week in L.
348 L is a list of day numbers (from 0 to 6 for Sunday through to
349 Saturday) or symbols `sunday', `monday', etc. (or a mixture). If
350 the date stored in `date' falls on a listed day, then the
351 function returns non-nil."
352 (let ((d (calendar-day-of-week date)))
354 (memq (nth d '(sunday monday tuesday wednesday
355 thursday friday saturday)) l))))
357 (defun mdw-discordian-date (date)
358 "Return the Discordian calendar date corresponding to DATE.
360 The return value is (YOLD . st-tibs-day) or (YOLD SEASON DAYNUM DOW).
362 The original is by David Pearson. I modified it to produce date components
363 as output rather than a string."
364 (let* ((days ["Sweetmorn" "Boomtime" "Pungenday"
365 "Prickle-Prickle" "Setting Orange"])
366 (months ["Chaos" "Discord" "Confusion"
367 "Bureaucracy" "Aftermath"])
368 (day-count [0 31 59 90 120 151 181 212 243 273 304 334])
369 (year (- (calendar-extract-year date) 1900))
370 (month (1- (calendar-extract-month date)))
371 (day (1- (calendar-extract-day date)))
372 (julian (+ (aref day-count month) day))
373 (dyear (+ year 3066)))
374 (if (and (= month 1) (= day 28))
375 (cons dyear 'st-tibs-day)
377 (aref months (floor (/ julian 73)))
379 (aref days (mod julian 5))))))
381 (defun mdw-diary-discordian-date ()
382 "Convert the date in `date' to a string giving the Discordian date."
383 (let* ((ddate (mdw-discordian-date date))
384 (tail (format "in the YOLD %d" (car ddate))))
385 (if (eq (cdr ddate) 'st-tibs-day)
386 (format "St Tib's Day %s" tail)
387 (let ((season (cadr ddate))
388 (daynum (caddr ddate))
389 (dayname (cadddr ddate)))
390 (format "%s, the %d%s day of %s %s"
393 (let ((ldig (mod daynum 10)))
394 (cond ((= ldig 1) "st")
401 (defun mdw-todo (&optional when)
402 "Return non-nil today, or on WHEN, whichever is later."
403 (let ((w (calendar-absolute-from-gregorian (calendar-current-date)))
404 (d (calendar-absolute-from-gregorian date)))
406 (setq w (max w (calendar-absolute-from-gregorian
408 ((not european-calendar-style)
420 (defvar mdw-diary-for-org-mode-p nil)
422 (defadvice org-agenda-list (around mdw-preserve-links activate)
423 (let ((mdw-diary-for-org-mode-p t))
426 (defvar diary-time-regexp nil)
428 (defadvice diary-add-to-list (before mdw-trim-leading-space compile activate)
429 "Trim leading space from the diary entry string."
431 (let ((str (ad-get-arg 1))
435 (setq str (cond ((null str) nil)
436 ((string-match "\\(^\\|\n\\)[ \t]+" str)
437 (replace-match "\\1" nil nil str))
438 ((and mdw-diary-for-org-mode-p
439 (string-match (concat
441 "\\(" diary-time-regexp
442 "\\(-" diary-time-regexp "\\)?"
444 "\\(\t[ \t]*\\| [ \t]+\\)")
446 (replace-match "\\1\\2 " nil nil str))
447 ((and (not mdw-diary-for-org-mode-p)
448 (string-match "\\[\\[[^][]*]\\[\\([^][]*\\)]]"
450 (replace-match "\\1" nil nil str))
452 (if (equal str old) (setq done t)))
453 (ad-set-arg 1 str))))
455 (defadvice org-bbdb-anniversaries (after mdw-fixup-list compile activate)
456 "Return a string rather than a list."
459 (dolist (e (let ((ee ad-return-value))
460 (if (atom ee) (list ee) ee)))
462 (when anyp (insert ?\n))
465 (setq ad-return-value
466 (and anyp (buffer-string))))))
468 ;; Fighting with Org-mode's evil key maps.
470 (defvar mdw-evil-keymap-keys
471 '(([S-up] . [?\C-c up])
472 ([S-down] . [?\C-c down])
473 ([S-left] . [?\C-c left])
474 ([S-right] . [?\C-c right])
475 (([M-up] [?\e up]) . [C-up])
476 (([M-down] [?\e down]) . [C-down])
477 (([M-left] [?\e left]) . [C-left])
478 (([M-right] [?\e right]) . [C-right]))
479 "Defines evil keybindings to clobber in `mdw-clobber-evil-keymap'.
480 The value is an alist mapping evil keys (as a list, or singleton)
481 to good keys (in the same form).")
483 (defun mdw-clobber-evil-keymap (keymap)
484 "Replace evil key bindings in the KEYMAP.
485 Evil key bindings are defined in `mdw-evil-keymap-keys'."
486 (dolist (entry mdw-evil-keymap-keys)
488 (keys (if (listp (car entry))
491 (replacements (if (listp (cdr entry))
493 (list (cdr entry)))))
496 (setq binding (lookup-key keymap key))
498 (throw 'found nil))))
501 (define-key keymap key nil))
502 (dolist (key replacements)
503 (define-key keymap key binding))))))
505 (defvar mdw-org-latex-defs
507 "\\documentclass{strayman}
508 \\usepackage[utf8]{inputenc}
509 \\usepackage[palatino, helvetica, courier, maths=cmr]{mdwfonts}
510 \\usepackage{graphicx, tikz, mdwtab, mdwmath, crypto, longtable}"
511 ("\\section{%s}" . "\\section*{%s}")
512 ("\\subsection{%s}" . "\\subsection*{%s}")
513 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
514 ("\\paragraph{%s}" . "\\paragraph*{%s}")
515 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))))
517 (eval-after-load "org-latex"
518 '(setq org-export-latex-classes
519 (append mdw-org-latex-defs org-export-latex-classes)))
521 (eval-after-load "ox-latex"
522 '(setq org-latex-classes (append mdw-org-latex-defs org-latex-classes)
523 org-latex-default-packages-alist '(("AUTO" "inputenc" t)
531 ("normalem" "ulem" t)
537 "\\tolerance=1000")))
540 (setq org-export-docbook-xslt-proc-command "xsltproc --output %o %s %i"
541 org-export-docbook-xsl-fo-proc-command "fop %i.safe %o"
542 org-export-docbook-xslt-stylesheet
543 "/usr/share/xml/docbook/stylesheet/docbook-xsl/fo/docbook.xsl")
547 (setq glasses-separator "-"
548 glasses-separate-parentheses-p nil
549 glasses-uncapitalize-p t)
551 ;; Some hacks to do with window placement.
553 (defun mdw-clobber-other-windows-showing-buffer (buffer-or-name)
554 "Arrange that no windows on other frames are showing BUFFER-OR-NAME."
555 (interactive "bBuffer: ")
556 (let ((home-frame (selected-frame))
557 (buffer (get-buffer buffer-or-name))
558 (safe-buffer (get-buffer "*scratch*")))
559 (dolist (frame (frame-list))
560 (unless (eq frame home-frame)
561 (dolist (window (window-list frame))
562 (when (eq (window-buffer window) buffer)
563 (set-window-buffer window safe-buffer)))))))
565 (defvar mdw-inhibit-walk-windows nil
566 "If non-nil, then `walk-windows' does nothing.
567 This is used by advice on `switch-to-buffer-other-frame' to inhibit finding
568 buffers in random frames.")
570 (setq display-buffer--other-frame-action
571 '((display-buffer-reuse-window display-buffer-pop-up-frame)
572 (reusable-frames . nil)
573 (inhibit-same-window . t)))
575 (defadvice walk-windows (around mdw-inhibit activate)
576 "If `mdw-inhibit-walk-windows' is non-nil, then do nothing."
577 (and (not mdw-inhibit-walk-windows)
580 (defadvice switch-to-buffer-other-frame
581 (around mdw-always-new-frame activate)
582 "Always make a new frame.
583 Even if an existing window in some random frame looks tempting."
584 (let ((mdw-inhibit-walk-windows t)) ad-do-it))
586 (defadvice display-buffer (before mdw-inhibit-other-frames activate)
587 "Don't try to do anything fancy with other frames.
588 Pretend they don't exist. They might be on other display devices."
591 (setq even-window-sizes nil
592 even-window-heights nil)
594 ;; Rename buffers along with files.
596 (defvar mdw-inhibit-rename-buffer nil
597 "If non-nil, `rename-file' won't rename the buffer visiting the file.")
599 (defmacro mdw-advise-to-inhibit-rename-buffer (function)
600 "Advise FUNCTION to set `mdw-inhibit-rename-buffer' while it runs.
602 This will prevent `rename-file' from renaming the buffer."
603 `(defadvice ,function (around mdw-inhibit-rename-buffer compile activate)
604 "Don't rename the buffer when renaming the underlying file."
605 (let ((mdw-inhibit-rename-buffer t))
607 (mdw-advise-to-inhibit-rename-buffer recode-file-name)
608 (mdw-advise-to-inhibit-rename-buffer set-visited-file-name)
609 (mdw-advise-to-inhibit-rename-buffer backup-buffer)
611 (defadvice rename-file (after mdw-rename-buffers (from to &optional forcep)
613 "If a buffer is visiting the file, rename it to match the new name.
615 Don't do this if `mdw-inhibit-rename-buffer' is non-nil."
616 (unless mdw-inhibit-rename-buffer
617 (let ((buffer (get-file-buffer from)))
619 (with-current-buffer buffer
620 (set-visited-file-name to nil t))))))
622 ;;;--------------------------------------------------------------------------
623 ;;; Improved compilation machinery.
625 ;; Uprated version of M-x compile.
627 (setq compile-command
628 (let ((ncpu (with-temp-buffer
629 (insert-file-contents "/proc/cpuinfo")
631 (count-matches "^processor\\s-*:"))))
632 (format "make -j%d -k" (* 2 ncpu))))
634 (defun mdw-compilation-buffer-name (mode)
635 (concat "*" (downcase mode) ": "
636 (abbreviate-file-name default-directory) "*"))
637 (setq compilation-buffer-name-function 'mdw-compilation-buffer-name)
639 (eval-after-load "compile"
641 (define-key compilation-shell-minor-mode-map "\C-c\M-g" 'recompile)))
643 (defadvice compile (around hack-environment compile activate)
644 "Hack the environment inherited by inferiors in the compilation."
645 (let ((process-environment (copy-tree process-environment)))
646 (setenv "LD_PRELOAD" nil)
649 (defun mdw-compile (command &optional directory comint)
650 "Initiate a compilation COMMAND, maybe in a different DIRECTORY.
651 The DIRECTORY may be nil to not change. If COMINT is t, then
652 start an interactive compilation.
654 Interactively, prompt for the command if the variable
655 `compilation-read-command' is non-nil, or if requested through
656 the prefix argument. Prompt for the directory, and run
657 interactively, if requested through the prefix.
659 Use a prefix of 4, 6, 12, or 14, or type C-u between one and three times, to
660 force prompting for a directory.
662 Use a prefix of 2, 6, 10, or 14, or type C-u three times, to force
663 prompting for the command.
665 Use a prefix of 8, 10, 12, or 14, or type C-u twice or three times,
666 to force interactive compilation."
668 (let* ((prefix (prefix-numeric-value current-prefix-arg))
669 (command (eval compile-command))
670 (dir (and (plusp (logand prefix #x54))
671 (read-directory-name "Compile in directory: "))))
672 (list (if (or compilation-read-command
673 (plusp (logand prefix #x42)))
674 (compilation-read-command command)
677 (plusp (logand prefix #x58)))))
678 (let ((default-directory (or directory default-directory)))
679 (compile command comint)))
683 (defun mdw-find-build-dir (build-file)
685 (let* ((src-dir (file-name-as-directory (expand-file-name ".")))
688 (when (file-exists-p (concat dir build-file))
690 (let ((sub (expand-file-name (file-relative-name src-dir dir)
691 (concat dir "build/"))))
694 (when (file-exists-p (concat sub build-file))
696 (when (string= sub dir) (throw 'give-up nil))
697 (setq sub (file-name-directory (directory-file-name sub))))))
699 (setq dir (file-name-directory
700 (directory-file-name dir))))
701 (throw 'found nil))))))
703 (defun mdw-flymake-make-init ()
704 (let ((build-dir (mdw-find-build-dir "Makefile")))
706 (let ((tmp-src (flymake-init-create-temp-buffer-copy
707 #'flymake-create-temp-inplace)))
708 (flymake-get-syntax-check-program-args
709 tmp-src build-dir t t
710 #'flymake-get-make-cmdline)))))
712 (setq flymake-allowed-file-name-masks
713 '(("\\.\\(?:[cC]\\|cc\\|cpp\\|cxx\\|c\\+\\+\\)\\'"
714 mdw-flymake-make-init)
715 ("\\.\\(?:[hH]\\|hh\\|hpp\\|hxx\\|h\\+\\+\\)\\'"
716 mdw-flymake-master-make-init)
717 ("\\.p[lm]" flymake-perl-init)))
719 (setq flymake-mode-map
720 (let ((map (if (boundp 'flymake-mode-map)
722 (make-sparse-keymap))))
723 (define-key map [?\C-c ?\C-f ?\C-p] 'flymake-goto-prev-error)
724 (define-key map [?\C-c ?\C-f ?\C-n] 'flymake-goto-next-error)
725 (define-key map [?\C-c ?\C-f ?\C-c] 'flymake-compile)
726 (define-key map [?\C-c ?\C-f ?\C-k] 'flymake-stop-all-syntax-checks)
727 (define-key map [?\C-c ?\C-f ?\C-e] 'flymake-popup-current-error-menu)
730 ;;;--------------------------------------------------------------------------
731 ;;; Mail and news hacking.
733 (define-derived-mode mdwmail-mode mail-mode "[mdw] mail"
734 "Major mode for editing news and mail messages from external programs.
735 Not much right now. Just support for doing MailCrypt stuff."
738 (run-hooks 'mail-setup-hook))
740 (define-key mdwmail-mode-map [?\C-c ?\C-c] 'disabled-operation)
742 (add-hook 'mdwail-mode-hook
744 (set-buffer-file-coding-system 'utf-8)
745 (make-local-variable 'paragraph-separate)
746 (make-local-variable 'paragraph-start)
747 (setq paragraph-start
748 (concat "[ \t]*[-_][-_][-_]+$\\|^-- \\|-----\\|"
750 (setq paragraph-separate
751 (concat "[ \t]*[-_][-_][-_]+$\\|^-- \\|-----\\|"
752 paragraph-separate))))
754 ;; How to encrypt in mdwmail.
756 (defun mdwmail-mc-encrypt (&optional recip scm start end from sign)
758 (setq start (save-excursion
759 (goto-char (point-min))
760 (or (search-forward "\n\n" nil t) (point-min)))))
762 (setq end (point-max)))
763 (mc-encrypt-generic recip scm start end from sign))
765 ;; How to sign in mdwmail.
767 (defun mdwmail-mc-sign (key scm start end uclr)
769 (setq start (save-excursion
770 (goto-char (point-min))
771 (or (search-forward "\n\n" nil t) (point-min)))))
773 (setq end (point-max)))
774 (mc-sign-generic key scm start end uclr))
776 ;; Some signature mangling.
778 (defun mdwmail-mangle-signature ()
780 (goto-char (point-min))
781 (perform-replace "\n-- \n" "\n-- " nil nil nil)))
782 (add-hook 'mail-setup-hook 'mdwmail-mangle-signature)
783 (add-hook 'message-setup-hook 'mdwmail-mangle-signature)
785 ;; Insert my login name into message-ids, so I can score replies.
787 (defadvice message-unique-id (after mdw-user-name last activate compile)
788 "Ensure that the user's name appears at the end of the message-id string,
789 so that it can be used for convenient filtering."
790 (setq ad-return-value (concat ad-return-value "." (user-login-name))))
792 ;; Tell my movemail hack where movemail is.
794 ;; This is needed to shup up warnings about LD_PRELOAD.
796 (let ((path exec-path))
798 (let ((try (expand-file-name "movemail" (car path))))
799 (if (file-executable-p try)
800 (setenv "REAL_MOVEMAIL" try))
801 (setq path (cdr path)))))
803 ;; AUTHINFO GENERIC kludge.
805 (defvar nntp-authinfo-generic nil
806 "Set to the `NNTPAUTH' string to pass on to `authinfo-kludge'.
808 Use this to arrange for per-server settings.")
810 (defun nntp-open-authinfo-kludge (buffer)
811 "Open a connection to SERVER using `authinfo-kludge'."
812 (let ((proc (start-process "nntpd" buffer
813 "env" (concat "NNTPAUTH="
814 (or nntp-authinfo-generic
816 (error "NNTPAUTH unset")))
817 "authinfo-kludge" nntp-address)))
819 (nntp-wait-for-string "^\r*200")
821 (delete-region (point-min) (point))
824 (eval-after-load "erc"
825 '(load "~/.ercrc.el"))
827 ;; Heavy-duty Gnus patching.
829 (defun mdw-nnimap-transform-headers ()
830 (goto-char (point-min))
831 (let (article lines size string)
834 (while (not (looking-at "\\* [0-9]+ FETCH"))
835 (delete-region (point) (progn (forward-line 1) (point)))
838 (goto-char (match-end 0))
839 ;; Unfold quoted {number} strings.
840 (while (re-search-forward
841 "[^]][ (]{\\([0-9]+\\)}\r?\n"
843 ;; Start of the header section.
844 (or (re-search-forward "] {[0-9]+}\r?\n" nil t)
845 ;; Start of the next FETCH.
846 (re-search-forward "\\* [0-9]+ FETCH" nil t)
849 (setq size (string-to-number (match-string 1)))
850 (delete-region (+ (match-beginning 0) 2) (point))
851 (setq string (buffer-substring (point) (+ (point) size)))
852 (delete-region (point) (+ (point) size))
853 (insert (format "%S" (subst-char-in-string ?\n ?\s string)))
854 ;; [mdw] missing from upstream
858 (and (re-search-forward "UID \\([0-9]+\\)" (line-end-position)
863 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
868 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
869 (let ((structure (ignore-errors
870 (read (current-buffer)))))
871 (while (and (consp structure)
872 (not (atom (car structure))))
873 (setq structure (car structure)))
875 (stringp (car structure))
876 (equal (upcase (nth 0 structure)) "MESSAGE")
877 (equal (upcase (nth 1 structure)) "RFC822"))
879 (nth 7 structure)))))
880 (delete-region (line-beginning-position) (line-end-position))
881 (insert (format "211 %s Article retrieved." article))
884 (insert (format "Chars: %s\n" size)))
886 (insert (format "Lines: %s\n" lines)))
887 ;; Most servers have a blank line after the headers, but
889 (unless (re-search-forward "^\r$\\|^)\r?$" nil t)
890 (goto-char (point-max)))
891 (delete-region (line-beginning-position) (line-end-position))
895 (eval-after-load 'nnimap
896 '(defalias 'nnimap-transform-headers
897 (symbol-function 'mdw-nnimap-transform-headers)))
899 (defadvice gnus-other-frame (around mdw-hack-frame-width compile activate)
900 "Always arrange for mail/news frames to be 80 columns wide."
901 (let ((default-frame-alist (cons `(width . ,(+ 80 mdw-frame-width-fudge))
902 (cl-delete 'width default-frame-alist
906 ;; Preferred programs.
908 (setq mailcap-user-mime-data
909 '(((type . "application/pdf") (viewer . "mupdf %s"))))
911 ;;;--------------------------------------------------------------------------
912 ;;; Utility functions.
914 (or (fboundp 'line-number-at-pos)
915 (defun line-number-at-pos (&optional pos)
916 (let ((opoint (or pos (point))) start)
919 (goto-char (point-min))
925 (1+ (count-lines 1 (point))))))))
927 (defun mdw-uniquify-alist (&rest alists)
928 "Return the concatenation of the ALISTS with duplicate elements removed.
929 The first association with a given key prevails; others are
930 ignored. The input lists are not modified, although they'll
931 probably become garbage."
933 (let ((start-list (cons nil nil)))
934 (mdw-do-uniquify start-list
939 (defun mdw-do-uniquify (done end l rest)
940 "A helper function for mdw-uniquify-alist.
941 The DONE argument is a list whose first element is `nil'. It
942 contains the uniquified alist built so far. The leading `nil' is
943 stripped off at the end of the operation; it's only there so that
944 DONE always references a cons cell. END refers to the final cons
945 cell in the DONE list; it is modified in place each time to avoid
946 the overheads of `append'ing all the time. The L argument is the
947 alist we're currently processing; the remaining alists are given
950 ;; There are several different cases to deal with here.
953 ;; Current list isn't empty. Add the first item to the DONE list if
954 ;; there's not an item with the same KEY already there.
955 (l (or (assoc (car (car l)) done)
957 (setcdr end (cons (car l) nil))
958 (setq end (cdr end))))
959 (mdw-do-uniquify done end (cdr l) rest))
961 ;; The list we were working on is empty. Shunt the next list into the
962 ;; current list position and go round again.
963 (rest (mdw-do-uniquify done end (car rest) (cdr rest)))
965 ;; Everything's done. Remove the leading `nil' from the DONE list and
966 ;; return it. Finished!
970 "Insert the current date in a pleasing way."
972 (insert (save-excursion
973 (let ((buffer (get-buffer-create "*tmp*")))
974 (unwind-protect (progn (set-buffer buffer)
976 (shell-command "date +%Y-%m-%d" t)
980 (kill-buffer buffer))))))
982 (defun uuencode (file &optional name)
983 "UUencodes a file, maybe calling it NAME, into the current buffer."
984 (interactive "fInput file name: ")
986 ;; If NAME isn't specified, then guess from the filename.
990 (or (string-match "[^/]*$" file) 0))))
991 (print (format "uuencode `%s' `%s'" file name))
993 ;; Now actually do the thing.
994 (call-process "uuencode" file t nil name))
996 (defvar np-file "~/.np"
997 "*Where the `now-playing' file is.")
999 (defun np (&optional arg)
1000 "Grabs a `now-playing' string."
1004 (goto-char (point-max))
1006 (insert-file-contents np-file)))))
1008 (defun mdw-version-< (ver-a ver-b)
1009 "Answer whether VER-A is strictly earlier than VER-B.
1010 VER-A and VER-B are version numbers, which are strings containing digit
1011 sequences separated by `.'."
1012 (let* ((la (mapcar (lambda (x) (car (read-from-string x)))
1013 (split-string ver-a "\\.")))
1014 (lb (mapcar (lambda (x) (car (read-from-string x)))
1015 (split-string ver-b "\\."))))
1018 (cond ((null la) (throw 'done lb))
1019 ((null lb) (throw 'done nil))
1020 ((< (car la) (car lb)) (throw 'done t))
1021 ((= (car la) (car lb)) (setq la (cdr la) lb (cdr lb)))
1022 (t (throw 'done nil)))))))
1024 (defun mdw-check-autorevert ()
1025 "Sets global-auto-revert-ignore-buffer appropriately for this buffer.
1026 This takes into consideration whether it's been found using
1027 tramp, which seems to get itself into a twist."
1028 (cond ((not (boundp 'global-auto-revert-ignore-buffer))
1030 ((and (buffer-file-name)
1031 (fboundp 'tramp-tramp-file-p)
1032 (tramp-tramp-file-p (buffer-file-name)))
1033 (unless global-auto-revert-ignore-buffer
1034 (setq global-auto-revert-ignore-buffer 'tramp)))
1035 ((eq global-auto-revert-ignore-buffer 'tramp)
1036 (setq global-auto-revert-ignore-buffer nil))))
1038 (defadvice find-file (after mdw-autorevert activate)
1039 (mdw-check-autorevert))
1040 (defadvice write-file (after mdw-autorevert activate)
1041 (mdw-check-autorevert))
1043 (defun mdw-auto-revert ()
1044 "Recheck all of the autorevertable buffers, and update VC modelines."
1046 (let ((auto-revert-check-vc-info t))
1047 (auto-revert-buffers)))
1049 (defun comint-send-and-indent ()
1052 (and mdw-auto-indent
1053 (indent-for-tab-command)))
1055 (defadvice comint-line-beginning-position
1056 (around mdw-calculate-it-properly () activate compile)
1057 "Calculate the actual line start for multi-line input."
1058 (if (or comint-use-prompt-regexp
1059 (eq (field-at-pos (point)) 'output))
1061 (setq ad-return-value
1062 (constrain-to-field (line-beginning-position) (point)))))
1064 ;;;--------------------------------------------------------------------------
1067 (defadvice dired-maybe-insert-subdir
1068 (around mdw-marked-insertion first activate)
1069 "The DIRNAME may be a list of directory names to insert.
1070 Interactively, if files are marked, then insert all of them.
1071 With a numeric prefix argument, select that many entries near
1072 point; with a non-numeric prefix argument, prompt for listing
1075 (list (dired-get-marked-files nil
1076 (and (integerp current-prefix-arg)
1079 (and current-prefix-arg
1080 (not (integerp current-prefix-arg))
1081 (read-string "Switches for listing: "
1082 (or dired-subdir-switches
1083 dired-actual-switches)))))
1084 (let ((dirs (ad-get-arg 0)))
1085 (dolist (dir (if (listp dirs) dirs (list dirs)))
1089 (defun mdw-dired-run (args &optional syncp)
1090 (interactive (let ((file (dired-get-filename t)))
1091 (list (read-string (format "Arguments for %s: " file))
1092 current-prefix-arg)))
1093 (funcall (if syncp 'shell-command 'async-shell-command)
1094 (concat (shell-quote-argument (dired-get-filename nil))
1097 (defadvice dired-do-flagged-delete
1098 (around mdw-delete-if-prefix-argument activate compile)
1099 (let ((delete-by-moving-to-trash (and (null current-prefix-arg)
1100 delete-by-moving-to-trash)))
1103 (eval-after-load "dired"
1104 '(define-key dired-mode-map "X" 'mdw-dired-run))
1106 ;;;--------------------------------------------------------------------------
1109 (defun mdw-w3m-browse-url (url &optional new-session-p)
1110 "Invoke w3m on the URL in its current window, or at least a different one.
1111 If NEW-SESSION-P, start a new session."
1112 (interactive "sURL: \nP")
1114 (let ((window (selected-window)))
1117 (select-window (or (and (not new-session-p)
1118 (get-buffer-window "*w3m*"))
1120 (if (one-window-p t) (split-window))
1122 (w3m-browse-url url new-session-p))
1123 (select-window window)))))
1125 (eval-after-load 'w3m
1126 '(define-key w3m-mode-map [?\e ?\r] 'w3m-view-this-url-new-session))
1128 (defvar mdw-good-url-browsers
1129 '(browse-url-mozilla
1131 (w3m . mdw-w3m-browse-url)
1133 "List of good browsers for mdw-good-url-browsers.
1134 Each item is a browser function name, or a cons (CHECK . FUNC).
1135 A symbol FOO stands for (FOO . FOO).")
1137 (defun mdw-good-url-browser ()
1138 "Return a good URL browser.
1139 Trundle the list of such things, finding the first item for which
1140 CHECK is fboundp, and returning the correponding FUNC."
1141 (let ((bs mdw-good-url-browsers) b check func answer)
1142 (while (and bs (not answer))
1146 (setq check (car b) func (cdr b))
1147 (setq check b func b))
1149 (setq answer func)))
1152 (eval-after-load "w3m-search"
1156 '(("g" "Google" "http://www.google.co.uk/search?q=%s")
1157 ("gd" "Google Directory"
1158 "http://www.google.com/search?cat=gwd/Top&q=%s")
1159 ("gg" "Google Groups" "http://groups.google.com/groups?q=%s")
1160 ("ward" "Ward's wiki" "http://c2.com/cgi/wiki?%s")
1161 ("gi" "Images" "http://images.google.com/images?q=%s")
1163 "http://metalzone.distorted.org.uk/ftp/pub/mirrors/rfc/rfc%s.txt.gz")
1165 "http://en.wikipedia.org/wiki/Special:Search?go=Go&search=%s")
1166 ("imdb" "IMDb" "http://www.imdb.com/Find?%s")
1167 ("nc-wiki" "nCipher wiki"
1168 "http://wiki.ncipher.com/wiki/bin/view/Devel/?topic=%s")
1169 ("map" "Google maps" "http://maps.google.co.uk/maps?q=%s&hl=en")
1170 ("lp" "Launchpad bug by number"
1171 "https://bugs.launchpad.net/bugs/%s")
1172 ("lppkg" "Launchpad bugs by package"
1173 "https://bugs.launchpad.net/%s")
1175 "http://social.msdn.microsoft.com/Search/en-GB/?query=%s&ac=8")
1176 ("debbug" "Debian bug by number"
1177 "http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=%s")
1178 ("debbugpkg" "Debian bugs by package"
1179 "http://bugs.debian.org/cgi-bin/pkgreport.cgi?pkg=%s")
1180 ("ljlogin" "LJ login" "http://www.livejournal.com/login.bml")))
1181 (add-to-list 'w3m-search-engine-alist
1182 (list (cadr item) (caddr item) nil))
1183 (add-to-list 'w3m-uri-replace-alist
1184 (list (concat "\\`" (car item) ":")
1185 'w3m-search-uri-replace
1188 ;;;--------------------------------------------------------------------------
1189 ;;; Paragraph filling.
1191 ;; Useful variables.
1193 (defvar mdw-fill-prefix nil
1194 "*Used by `mdw-line-prefix' and `mdw-fill-paragraph'.
1195 If there's no fill prefix currently set (by the `fill-prefix'
1196 variable) and there's a match from one of the regexps here, it
1197 gets used to set the fill-prefix for the current operation.
1199 The variable is a list of items of the form `PATTERN . PREFIX'; if
1200 the PATTERN matches, the PREFIX is used to set the fill prefix.
1202 A PATTERN is one of the following.
1204 * STRING -- a regular expression, expected to match at point
1205 * (eval . FORM) -- a Lisp form which must evaluate non-nil
1206 * (if COND CONSEQ-PAT ALT-PAT) -- if COND evaluates non-nil, must match
1207 CONSEQ-PAT; otherwise must match ALT-PAT
1208 * (and PATTERN ...) -- must match all of the PATTERNs
1209 * (or PATTERN ...) -- must match at least one PATTERN
1210 * (not PATTERN) -- mustn't match (probably not useful)
1212 A PREFIX is a list of the following kinds of things:
1214 * STRING -- insert a literal string
1215 * (match . N) -- insert the thing matched by bracketed subexpression N
1216 * (pad . N) -- a string of whitespace the same width as subexpression N
1217 * (expr . FORM) -- the result of evaluating FORM
1219 Information about `bracketed subexpressions' comes from the match data,
1220 as modified during matching.")
1222 (make-variable-buffer-local 'mdw-fill-prefix)
1224 (defvar mdw-hanging-indents
1226 "\\([*o+]\\|-[-#]?\\|[0-9]+\\.\\|\\[[0-9]+\\]\\|([a-zA-Z])\\)"
1229 "*Standard regexp matching parts of a hanging indent.
1230 This is mainly useful in `auto-fill-mode'.")
1232 ;; Utility functions.
1234 (defun mdw-maybe-tabify (s)
1235 "Tabify or untabify the string S, according to `indent-tabs-mode'."
1236 (let ((tabfun (if indent-tabs-mode #'tabify #'untabify)))
1240 (let ((start (point-min)) (end (point-max)))
1241 (funcall tabfun (point-min) (point-max))
1242 (setq s (buffer-substring (point-min) (1- (point-max)))))))))
1244 (defun mdw-fill-prefix-match-p (pat)
1245 "Return non-nil if PAT matches at the current position."
1246 (cond ((stringp pat) (looking-at pat))
1247 ((not (consp pat)) (error "Unknown pattern item `%S'" pat))
1248 ((eq (car pat) 'eval) (eval (cdr pat)))
1250 (if (or (null (cdr pat))
1254 (error "Invalid `if' pattern `%S'" pat))
1255 (mdw-fill-prefix-match-p (if (eval (cadr pat))
1258 ((eq (car pat) 'and)
1259 (let ((pats (cdr pat))
1262 (or (mdw-fill-prefix-match-p (car pats))
1264 (setq pats (cdr pats)))
1267 (let ((pats (cdr pat))
1270 (or (not (mdw-fill-prefix-match-p (car pats)))
1271 (progn (setq ok t) nil)))
1272 (setq pats (cdr pats)))
1274 ((eq (car pat) 'not)
1275 (if (or (null (cdr pat)) (cddr pat))
1276 (error "Invalid `not' pattern `%S'" pat))
1277 (not (mdw-fill-prefix-match-p (car pats))))
1278 (t (error "Unknown pattern form `%S'" pat))))
1280 (defun mdw-maybe-car (p)
1281 "If P is a pair, return (car P), otherwise just return P."
1282 (if (consp p) (car p) p))
1284 (defun mdw-padding (s)
1285 "Return a string the same width as S but made entirely from whitespace."
1286 (let* ((l (length s)) (i 0) (n (make-string l ? )))
1288 (if (= 9 (aref s i))
1293 (defun mdw-do-prefix-match (m)
1294 "Expand a dynamic prefix match element.
1295 See `mdw-fill-prefix' for details."
1296 (cond ((not (consp m)) (format "%s" m))
1297 ((eq (car m) 'match) (match-string (mdw-maybe-car (cdr m))))
1298 ((eq (car m) 'pad) (mdw-padding (match-string
1299 (mdw-maybe-car (cdr m)))))
1300 ((eq (car m) 'eval) (eval (cdr m)))
1303 (defun mdw-examine-fill-prefixes (l)
1304 "Given a list of dynamic fill prefixes, pick one which matches
1305 context and return the static fill prefix to use. Point must be
1306 at the start of a line, and match data must be saved."
1308 (while (cond ((null l) nil)
1309 ((mdw-fill-prefix-match-p (caar l))
1313 (mapcar #'mdw-do-prefix-match
1319 (defun mdw-choose-dynamic-fill-prefix ()
1320 "Work out the dynamic fill prefix based on the variable `mdw-fill-prefix'."
1321 (cond ((and fill-prefix (not (string= fill-prefix ""))) fill-prefix)
1322 ((not mdw-fill-prefix) fill-prefix)
1326 (mdw-examine-fill-prefixes mdw-fill-prefix))))))
1328 (defadvice do-auto-fill (around mdw-dynamic-fill-prefix () activate compile)
1329 "Handle auto-filling, working out a dynamic fill prefix in the
1330 case where there isn't a sensible static one."
1331 (let ((fill-prefix (mdw-choose-dynamic-fill-prefix)))
1334 (defun mdw-fill-paragraph ()
1335 "Fill paragraph, getting a dynamic fill prefix."
1337 (let ((fill-prefix (mdw-choose-dynamic-fill-prefix)))
1338 (fill-paragraph nil)))
1340 (defun mdw-standard-fill-prefix (rx &optional mat)
1341 "Set the dynamic fill prefix, handling standard hanging indents and stuff.
1342 This is just a short-cut for setting the thing by hand, and by
1343 design it doesn't cope with anything approximating a complicated
1345 (setq mdw-fill-prefix
1346 `((,(concat rx mdw-hanging-indents)
1348 (pad . ,(or mat 2))))))
1350 ;;;--------------------------------------------------------------------------
1351 ;;; Other common declarations.
1353 ;; Common mode settings.
1355 (defvar mdw-auto-indent t
1356 "Whether to indent automatically after a newline.")
1358 (defun mdw-whitespace-mode (&optional arg)
1359 "Turn on/off whitespace mode, but don't highlight trailing space."
1361 (when (and (boundp 'whitespace-style)
1362 (fboundp 'whitespace-mode))
1363 (let ((whitespace-style (remove 'trailing whitespace-style)))
1364 (whitespace-mode arg))
1365 (setq show-trailing-whitespace whitespace-mode)))
1367 (defvar mdw-do-misc-mode-hacking nil)
1369 (defun mdw-misc-mode-config ()
1370 (and mdw-auto-indent
1371 (cond ((eq major-mode 'lisp-mode)
1372 (local-set-key "\C-m" 'mdw-indent-newline-and-indent))
1373 ((derived-mode-p 'slime-repl-mode 'asm-mode 'comint-mode)
1376 (local-set-key "\C-m" 'newline-and-indent))))
1377 (set (make-local-variable 'mdw-do-misc-mode-hacking) t)
1378 (local-set-key [C-return] 'newline)
1379 (make-local-variable 'page-delimiter)
1380 (setq page-delimiter (concat "^" "\f"
1384 "\\(" " " ".*" " " "\\)?"
1388 (setq comment-column 40)
1390 (setq fill-column mdw-text-width)
1391 (flyspell-prog-mode)
1392 (and (fboundp 'gtags-mode)
1394 (if (fboundp 'hs-minor-mode)
1395 (trap (hs-minor-mode t))
1396 (outline-minor-mode t))
1398 (trap (turn-on-font-lock)))
1400 (defun mdw-post-local-vars-misc-mode-config ()
1401 (setq whitespace-line-column mdw-text-width)
1402 (when (and mdw-do-misc-mode-hacking
1403 (not buffer-read-only))
1404 (setq show-trailing-whitespace t)
1405 (mdw-whitespace-mode 1)))
1406 (add-hook 'hack-local-variables-hook 'mdw-post-local-vars-misc-mode-config)
1408 (defmacro mdw-advise-update-angry-fruit-salad (&rest funcs)
1409 `(progn ,@(mapcar (lambda (func)
1411 (after mdw-angry-fruit-salad activate)
1412 (when mdw-do-misc-mode-hacking
1413 (setq show-trailing-whitespace
1414 (not buffer-read-only))
1415 (mdw-whitespace-mode (if buffer-read-only 0 1)))))
1417 (mdw-advise-update-angry-fruit-salad toggle-read-only
1423 (eval-after-load 'gtags
1425 (dolist (key '([mouse-2] [mouse-3]))
1426 (define-key gtags-mode-map key nil))
1427 (define-key gtags-mode-map [C-S-mouse-2] 'gtags-find-tag-by-event)
1428 (define-key gtags-select-mode-map [C-S-mouse-2]
1429 'gtags-select-tag-by-event)
1430 (dolist (map (list gtags-mode-map gtags-select-mode-map))
1431 (define-key map [C-S-mouse-3] 'gtags-pop-stack))))
1433 ;; Backup file handling.
1435 (defvar mdw-backup-disable-regexps nil
1436 "*List of regular expressions: if a file name matches any of
1437 these then the file is not backed up.")
1439 (defun mdw-backup-enable-predicate (name)
1440 "[mdw]'s default backup predicate.
1441 Allows a backup if the standard predicate would allow it, and it
1442 doesn't match any of the regular expressions in
1443 `mdw-backup-disable-regexps'."
1444 (and (normal-backup-enable-predicate name)
1445 (let ((answer t) (list mdw-backup-disable-regexps))
1448 (if (string-match (car list) name)
1450 (setq list (cdr list)))
1452 (setq backup-enable-predicate 'mdw-backup-enable-predicate)
1456 (defun mdw-last-one-out-turn-off-the-lights (frame)
1457 "Disconnect from an X display if this was the last frame on that display."
1458 (let ((frame-display (frame-parameter frame 'display)))
1459 (when (and frame-display
1460 (eq window-system 'x)
1461 (not (some (lambda (fr)
1462 (and (not (eq fr frame))
1463 (string= (frame-parameter fr 'display)
1466 (run-with-idle-timer 0 nil #'x-close-connection frame-display))))
1467 (add-hook 'delete-frame-functions 'mdw-last-one-out-turn-off-the-lights)
1469 ;;;--------------------------------------------------------------------------
1470 ;;; Fullscreen-ness.
1472 (defvar mdw-full-screen-parameters
1473 '((menu-bar-lines . 0)
1474 ;(vertical-scroll-bars . nil)
1476 "Frame parameters to set when making a frame fullscreen.")
1478 (defvar mdw-full-screen-save
1480 "Extra frame parameters to save when setting fullscreen.")
1482 (defun mdw-toggle-full-screen (&optional frame)
1483 "Show the FRAME fullscreen."
1486 (cond ((frame-parameter frame 'fullscreen)
1487 (set-frame-parameter frame 'fullscreen nil)
1488 (modify-frame-parameters
1490 (or (frame-parameter frame 'mdw-full-screen-saved)
1491 (mapcar (lambda (assoc)
1492 (assq (car assoc) default-frame-alist))
1493 mdw-full-screen-parameters))))
1495 (let ((saved (mapcar (lambda (param)
1496 (cons param (frame-parameter frame param)))
1497 (append (mapcar #'car
1498 mdw-full-screen-parameters)
1499 mdw-full-screen-save))))
1500 (set-frame-parameter frame 'mdw-full-screen-saved saved))
1501 (modify-frame-parameters frame mdw-full-screen-parameters)
1502 (set-frame-parameter frame 'fullscreen 'fullboth)))))
1504 ;;;--------------------------------------------------------------------------
1505 ;;; General fontification.
1507 (make-face 'mdw-virgin-face)
1509 (defmacro mdw-define-face (name &rest body)
1510 "Define a face, and make sure it's actually set as the definition."
1514 (copy-face 'mdw-virgin-face ',name)
1515 (defvar ,name ',name)
1516 (put ',name 'face-defface-spec ',body)
1517 (face-spec-set ',name ',body nil)))
1519 (mdw-define-face default
1520 (((type w32)) :family "courier new" :height 85)
1521 (((type x)) :family "6x13" :foundry "trad" :height 130)
1522 (((type color)) :foreground "white" :background "black")
1524 (mdw-define-face fixed-pitch
1525 (((type w32)) :family "courier new" :height 85)
1526 (((type x)) :family "6x13" :foundry "trad" :height 130)
1527 (t :foreground "white" :background "black"))
1528 (mdw-define-face fixed-pitch-serif
1529 (((type w32)) :family "courier new" :height 85 :weight bold)
1530 (((type x)) :family "6x13" :foundry "trad" :height 130 :weight bold)
1531 (t :foreground "white" :background "black" :weight bold))
1532 (mdw-define-face variable-pitch
1533 (((type x)) :family "helvetica" :height 120))
1534 (mdw-define-face region
1535 (((min-colors 64)) :background "grey30")
1536 (((class color)) :background "blue")
1537 (t :inverse-video t))
1538 (mdw-define-face match
1539 (((class color)) :background "blue")
1540 (t :inverse-video t))
1541 (mdw-define-face mc/cursor-face
1542 (((class color)) :background "red")
1543 (t :inverse-video t))
1544 (mdw-define-face minibuffer-prompt
1546 (mdw-define-face mode-line
1547 (((class color)) :foreground "blue" :background "yellow"
1548 :box (:line-width 1 :style released-button))
1549 (t :inverse-video t))
1550 (mdw-define-face mode-line-inactive
1551 (((class color)) :foreground "yellow" :background "blue"
1552 :box (:line-width 1 :style released-button))
1553 (t :inverse-video t))
1554 (mdw-define-face nobreak-space
1556 (t :inherit escape-glyph :underline t))
1557 (mdw-define-face scroll-bar
1558 (t :foreground "black" :background "lightgrey"))
1559 (mdw-define-face fringe
1560 (t :foreground "yellow"))
1561 (mdw-define-face show-paren-match
1562 (((min-colors 64)) :background "darkgreen")
1563 (((class color)) :background "green")
1565 (mdw-define-face show-paren-mismatch
1566 (((class color)) :background "red")
1567 (t :inverse-video t))
1568 (mdw-define-face highlight
1569 (((min-colors 64)) :background "DarkSeaGreen4")
1570 (((class color)) :background "cyan")
1571 (t :inverse-video t))
1573 (mdw-define-face holiday-face
1574 (t :background "red"))
1575 (mdw-define-face calendar-today-face
1576 (t :foreground "yellow" :weight bold))
1578 (mdw-define-face comint-highlight-prompt
1580 (mdw-define-face comint-highlight-input
1583 (mdw-define-face Man-underline
1584 (((type tty)) :underline t)
1587 (mdw-define-face ido-subdir
1588 (t :foreground "cyan" :weight bold))
1590 (mdw-define-face dired-directory
1591 (t :foreground "cyan" :weight bold))
1592 (mdw-define-face dired-symlink
1593 (t :foreground "cyan"))
1594 (mdw-define-face dired-perm-write
1597 (mdw-define-face trailing-whitespace
1598 (((class color)) :background "red")
1599 (t :inverse-video t))
1600 (mdw-define-face whitespace-line
1601 (((class color)) :background "darkred")
1602 (t :inverse-video t))
1603 (mdw-define-face mdw-punct-face
1604 (((min-colors 64)) :foreground "burlywood2")
1605 (((class color)) :foreground "yellow"))
1606 (mdw-define-face mdw-number-face
1607 (t :foreground "yellow"))
1608 (mdw-define-face mdw-trivial-face)
1609 (mdw-define-face font-lock-function-name-face
1611 (mdw-define-face font-lock-keyword-face
1613 (mdw-define-face font-lock-constant-face
1615 (mdw-define-face font-lock-builtin-face
1617 (mdw-define-face font-lock-type-face
1618 (t :weight bold :slant italic))
1619 (mdw-define-face font-lock-reference-face
1621 (mdw-define-face font-lock-variable-name-face
1623 (mdw-define-face font-lock-comment-delimiter-face
1624 (((min-colors 64)) :slant italic :foreground "SeaGreen1")
1625 (((class color)) :foreground "green")
1627 (mdw-define-face font-lock-comment-face
1628 (((min-colors 64)) :slant italic :foreground "SeaGreen1")
1629 (((class color)) :foreground "green")
1631 (mdw-define-face font-lock-string-face
1632 (((min-colors 64)) :foreground "SkyBlue1")
1633 (((class color)) :foreground "cyan")
1636 (mdw-define-face message-separator
1637 (t :background "red" :foreground "white" :weight bold))
1638 (mdw-define-face message-cited-text
1639 (default :slant italic)
1640 (((min-colors 64)) :foreground "SkyBlue1")
1641 (((class color)) :foreground "cyan"))
1642 (mdw-define-face message-header-cc
1643 (default :slant italic)
1644 (((min-colors 64)) :foreground "SeaGreen1")
1645 (((class color)) :foreground "green"))
1646 (mdw-define-face message-header-newsgroups
1647 (default :slant italic)
1648 (((min-colors 64)) :foreground "SeaGreen1")
1649 (((class color)) :foreground "green"))
1650 (mdw-define-face message-header-subject
1651 (((min-colors 64)) :foreground "SeaGreen1")
1652 (((class color)) :foreground "green"))
1653 (mdw-define-face message-header-to
1654 (((min-colors 64)) :foreground "SeaGreen1")
1655 (((class color)) :foreground "green"))
1656 (mdw-define-face message-header-xheader
1657 (default :slant italic)
1658 (((min-colors 64)) :foreground "SeaGreen1")
1659 (((class color)) :foreground "green"))
1660 (mdw-define-face message-header-other
1661 (default :slant italic)
1662 (((min-colors 64)) :foreground "SeaGreen1")
1663 (((class color)) :foreground "green"))
1664 (mdw-define-face message-header-name
1665 (default :weight bold)
1666 (((min-colors 64)) :foreground "SeaGreen1")
1667 (((class color)) :foreground "green"))
1669 (mdw-define-face which-func
1672 (mdw-define-face gnus-header-name
1673 (default :weight bold)
1674 (((min-colors 64)) :foreground "SeaGreen1")
1675 (((class color)) :foreground "green"))
1676 (mdw-define-face gnus-header-subject
1677 (((min-colors 64)) :foreground "SeaGreen1")
1678 (((class color)) :foreground "green"))
1679 (mdw-define-face gnus-header-from
1680 (((min-colors 64)) :foreground "SeaGreen1")
1681 (((class color)) :foreground "green"))
1682 (mdw-define-face gnus-header-to
1683 (((min-colors 64)) :foreground "SeaGreen1")
1684 (((class color)) :foreground "green"))
1685 (mdw-define-face gnus-header-content
1686 (default :slant italic)
1687 (((min-colors 64)) :foreground "SeaGreen1")
1688 (((class color)) :foreground "green"))
1690 (mdw-define-face gnus-cite-1
1691 (((min-colors 64)) :foreground "SkyBlue1")
1692 (((class color)) :foreground "cyan"))
1693 (mdw-define-face gnus-cite-2
1694 (((min-colors 64)) :foreground "RoyalBlue2")
1695 (((class color)) :foreground "blue"))
1696 (mdw-define-face gnus-cite-3
1697 (((min-colors 64)) :foreground "MediumOrchid")
1698 (((class color)) :foreground "magenta"))
1699 (mdw-define-face gnus-cite-4
1700 (((min-colors 64)) :foreground "firebrick2")
1701 (((class color)) :foreground "red"))
1702 (mdw-define-face gnus-cite-5
1703 (((min-colors 64)) :foreground "burlywood2")
1704 (((class color)) :foreground "yellow"))
1705 (mdw-define-face gnus-cite-6
1706 (((min-colors 64)) :foreground "SeaGreen1")
1707 (((class color)) :foreground "green"))
1708 (mdw-define-face gnus-cite-7
1709 (((min-colors 64)) :foreground "SlateBlue1")
1710 (((class color)) :foreground "cyan"))
1711 (mdw-define-face gnus-cite-8
1712 (((min-colors 64)) :foreground "RoyalBlue2")
1713 (((class color)) :foreground "blue"))
1714 (mdw-define-face gnus-cite-9
1715 (((min-colors 64)) :foreground "purple2")
1716 (((class color)) :foreground "magenta"))
1717 (mdw-define-face gnus-cite-10
1718 (((min-colors 64)) :foreground "DarkOrange2")
1719 (((class color)) :foreground "red"))
1720 (mdw-define-face gnus-cite-11
1721 (t :foreground "grey"))
1723 (mdw-define-face gnus-emphasis-underline
1724 (((type tty)) :underline t)
1727 (mdw-define-face diff-header
1729 (mdw-define-face diff-index
1731 (mdw-define-face diff-file-header
1733 (mdw-define-face diff-hunk-header
1734 (((min-colors 64)) :foreground "SkyBlue1")
1735 (((class color)) :foreground "cyan"))
1736 (mdw-define-face diff-function
1737 (default :weight bold)
1738 (((min-colors 64)) :foreground "SkyBlue1")
1739 (((class color)) :foreground "cyan"))
1740 (mdw-define-face diff-header
1741 (((min-colors 64)) :background "grey10"))
1742 (mdw-define-face diff-added
1743 (((class color)) :foreground "green"))
1744 (mdw-define-face diff-removed
1745 (((class color)) :foreground "red"))
1746 (mdw-define-face diff-context
1748 (mdw-define-face diff-refine-change
1749 (((min-colors 64)) :background "RoyalBlue4")
1751 (mdw-define-face diff-refine-removed
1752 (((min-colors 64)) :background "#500")
1754 (mdw-define-face diff-refine-added
1755 (((min-colors 64)) :background "#050")
1758 (setq ediff-force-faces t)
1759 (mdw-define-face ediff-current-diff-A
1760 (((min-colors 64)) :background "darkred")
1761 (((class color)) :background "red")
1762 (t :inverse-video t))
1763 (mdw-define-face ediff-fine-diff-A
1764 (((min-colors 64)) :background "red3")
1765 (((class color)) :inverse-video t)
1766 (t :inverse-video nil))
1767 (mdw-define-face ediff-even-diff-A
1768 (((min-colors 64)) :background "#300"))
1769 (mdw-define-face ediff-odd-diff-A
1770 (((min-colors 64)) :background "#300"))
1771 (mdw-define-face ediff-current-diff-B
1772 (((min-colors 64)) :background "darkgreen")
1773 (((class color)) :background "magenta")
1774 (t :inverse-video t))
1775 (mdw-define-face ediff-fine-diff-B
1776 (((min-colors 64)) :background "green4")
1777 (((class color)) :inverse-video t)
1778 (t :inverse-video nil))
1779 (mdw-define-face ediff-even-diff-B
1780 (((min-colors 64)) :background "#020"))
1781 (mdw-define-face ediff-odd-diff-B
1782 (((min-colors 64)) :background "#020"))
1783 (mdw-define-face ediff-current-diff-C
1784 (((min-colors 64)) :background "darkblue")
1785 (((class color)) :background "blue")
1786 (t :inverse-video t))
1787 (mdw-define-face ediff-fine-diff-C
1788 (((min-colors 64)) :background "blue1")
1789 (((class color)) :inverse-video t)
1790 (t :inverse-video nil))
1791 (mdw-define-face ediff-even-diff-C
1792 (((min-colors 64)) :background "#004"))
1793 (mdw-define-face ediff-odd-diff-C
1794 (((min-colors 64)) :background "#004"))
1795 (mdw-define-face ediff-current-diff-Ancestor
1796 (((min-colors 64)) :background "#630")
1797 (((class color)) :background "blue")
1798 (t :inverse-video t))
1799 (mdw-define-face ediff-even-diff-Ancestor
1800 (((min-colors 64)) :background "#320"))
1801 (mdw-define-face ediff-odd-diff-Ancestor
1802 (((min-colors 64)) :background "#320"))
1804 (mdw-define-face magit-hash
1805 (((min-colors 64)) :foreground "grey40")
1806 (((class color)) :foreground "blue"))
1807 (mdw-define-face magit-diff-hunk-heading
1808 (((min-colors 64)) :foreground "grey70" :background "grey25")
1809 (((class color)) :foreground "yellow"))
1810 (mdw-define-face magit-diff-hunk-heading-highlight
1811 (((min-colors 64)) :foreground "grey70" :background "grey35")
1812 (((class color)) :foreground "yellow" :background "blue"))
1813 (mdw-define-face magit-diff-added
1814 (((min-colors 64)) :foreground "#ddffdd" :background "#335533")
1815 (((class color)) :foreground "green"))
1816 (mdw-define-face magit-diff-added-highlight
1817 (((min-colors 64)) :foreground "#cceecc" :background "#336633")
1818 (((class color)) :foreground "green" :background "blue"))
1819 (mdw-define-face magit-diff-removed
1820 (((min-colors 64)) :foreground "#ffdddd" :background "#553333")
1821 (((class color)) :foreground "red"))
1822 (mdw-define-face magit-diff-removed-highlight
1823 (((min-colors 64)) :foreground "#eecccc" :background "#663333")
1824 (((class color)) :foreground "red" :background "blue"))
1825 (mdw-define-face magit-blame-heading
1826 (((min-colors 64)) :foreground "white" :background "grey25"
1827 :weight normal :slant normal)
1828 (((class color)) :foreground "white" :background "blue"
1829 :weight normal :slant normal))
1830 (mdw-define-face magit-blame-name
1831 (t :inherit magit-blame-heading :slant italic))
1832 (mdw-define-face magit-blame-date
1833 (((min-colors 64)) :inherit magit-blame-heading :foreground "grey60")
1834 (((class color)) :inherit magit-blame-heading :foreground "cyan"))
1835 (mdw-define-face magit-blame-summary
1836 (t :inherit magit-blame-heading :weight bold))
1838 (mdw-define-face dylan-header-background
1839 (((min-colors 64)) :background "NavyBlue")
1840 (((class color)) :background "blue"))
1842 (mdw-define-face erc-input-face
1843 (t :foreground "red"))
1845 (mdw-define-face woman-bold
1847 (mdw-define-face woman-italic
1850 (eval-after-load "rst"
1852 (mdw-define-face rst-level-1-face
1853 (t :foreground "SkyBlue1" :weight bold))
1854 (mdw-define-face rst-level-2-face
1855 (t :foreground "SeaGreen1" :weight bold))
1856 (mdw-define-face rst-level-3-face
1858 (mdw-define-face rst-level-4-face
1860 (mdw-define-face rst-level-5-face
1862 (mdw-define-face rst-level-6-face
1865 (mdw-define-face p4-depot-added-face
1866 (t :foreground "green"))
1867 (mdw-define-face p4-depot-branch-op-face
1868 (t :foreground "yellow"))
1869 (mdw-define-face p4-depot-deleted-face
1870 (t :foreground "red"))
1871 (mdw-define-face p4-depot-unmapped-face
1872 (t :foreground "SkyBlue1"))
1873 (mdw-define-face p4-diff-change-face
1874 (t :foreground "yellow"))
1875 (mdw-define-face p4-diff-del-face
1876 (t :foreground "red"))
1877 (mdw-define-face p4-diff-file-face
1878 (t :foreground "SkyBlue1"))
1879 (mdw-define-face p4-diff-head-face
1880 (t :background "grey10"))
1881 (mdw-define-face p4-diff-ins-face
1882 (t :foreground "green"))
1884 (mdw-define-face w3m-anchor-face
1885 (t :foreground "SkyBlue1" :underline t))
1886 (mdw-define-face w3m-arrived-anchor-face
1887 (t :foreground "SkyBlue1" :underline t))
1889 (mdw-define-face whizzy-slice-face
1890 (t :background "grey10"))
1891 (mdw-define-face whizzy-error-face
1892 (t :background "darkred"))
1894 ;; Ellipses used to indicate hidden text (and similar).
1895 (mdw-define-face mdw-ellipsis-face
1896 (((type tty)) :foreground "blue") (t :foreground "grey60"))
1897 (let ((dollar (make-glyph-code ?$ 'mdw-ellipsis-face))
1898 (backslash (make-glyph-code ?\\ 'mdw-ellipsis-face))
1899 (dot (make-glyph-code ?. 'mdw-ellipsis-face))
1900 (bar (make-glyph-code ?| mdw-ellipsis-face)))
1901 (set-display-table-slot standard-display-table 0 dollar)
1902 (set-display-table-slot standard-display-table 1 backslash)
1903 (set-display-table-slot standard-display-table 4
1904 (vector dot dot dot))
1905 (set-display-table-slot standard-display-table 5 bar))
1907 ;;;--------------------------------------------------------------------------
1910 (mdw-define-face mdw-point-overlay-face
1912 (((min-colors 64)) :background "darkblue")
1913 (((class color)) :background "blue")
1914 (((type tty) (class mono)) :inverse-video t))
1916 (defvar mdw-point-overlay-fringe-display '(vertical-bar . vertical-bar))
1918 (defun mdw-configure-point-overlay ()
1919 (let ((ov (make-overlay 0 0)))
1920 (overlay-put ov 'priority 0)
1921 (let* ((fringe (or mdw-point-overlay-fringe-display (cons nil nil)))
1922 (left (car fringe)) (right (cdr fringe))
1926 (put-text-property 0 1 'display `(left-fringe ,left) ss)
1927 (setq s (concat s ss))))
1930 (put-text-property 0 1 'display `(right-fringe ,right) ss)
1931 (setq s (concat s ss))))
1932 (when (or left right)
1933 (overlay-put ov 'before-string s)))
1934 (overlay-put ov 'face 'mdw-point-overlay-face)
1938 (defvar mdw-point-overlay (mdw-configure-point-overlay)
1939 "An overlay used for showing where point is in the selected window.")
1940 (defun mdw-reconfigure-point-overlay ()
1942 (setq mdw-point-overlay (mdw-configure-point-overlay)))
1944 (defun mdw-remove-point-overlay ()
1945 "Remove the current-point overlay."
1946 (delete-overlay mdw-point-overlay))
1948 (defun mdw-update-point-overlay ()
1949 "Mark the current point position with an overlay."
1950 (if (not mdw-point-overlay-mode)
1951 (mdw-remove-point-overlay)
1952 (overlay-put mdw-point-overlay 'window (selected-window))
1953 (move-overlay mdw-point-overlay
1954 (line-beginning-position)
1955 (+ (line-end-position) 1))))
1957 (defvar mdw-point-overlay-buffers nil
1958 "List of buffers using `mdw-point-overlay-mode'.")
1960 (define-minor-mode mdw-point-overlay-mode
1961 "Indicate current line with an overlay."
1963 (let ((buffer (current-buffer)))
1964 (setq mdw-point-overlay-buffers
1965 (mapcan (lambda (buf)
1966 (if (and (buffer-live-p buf)
1967 (not (eq buf buffer)))
1969 mdw-point-overlay-buffers))
1970 (if mdw-point-overlay-mode
1971 (setq mdw-point-overlay-buffers
1972 (cons buffer mdw-point-overlay-buffers))))
1973 (cond (mdw-point-overlay-buffers
1974 (add-hook 'pre-command-hook 'mdw-remove-point-overlay)
1975 (add-hook 'post-command-hook 'mdw-update-point-overlay))
1977 (mdw-remove-point-overlay)
1978 (remove-hook 'pre-command-hook 'mdw-remove-point-overlay)
1979 (remove-hook 'post-command-hook 'mdw-update-point-overlay))))
1981 (define-globalized-minor-mode mdw-global-point-overlay-mode
1982 mdw-point-overlay-mode
1983 (lambda () (if (not (minibufferp)) (mdw-point-overlay-mode t))))
1985 (defvar mdw-terminal-title-alist nil)
1986 (defun mdw-update-terminal-title ()
1987 (when (let ((term (frame-parameter nil 'tty-type)))
1988 (and term (string-match "^xterm" term)))
1989 (let* ((tty (frame-parameter nil 'tty))
1990 (old (assoc tty mdw-terminal-title-alist))
1991 (new (format-mode-line frame-title-format)))
1992 (unless (and old (equal (cdr old) new))
1993 (if old (rplacd old new)
1994 (setq mdw-terminal-title-alist
1995 (cons (cons tty new) mdw-terminal-title-alist)))
1996 (send-string-to-terminal (concat "\e]2;" new "\e\\"))))))
1998 (add-hook 'post-command-hook 'mdw-update-terminal-title)
2000 ;;;--------------------------------------------------------------------------
2001 ;;; C programming configuration.
2003 ;; Make C indentation nice.
2005 (defun mdw-c-lineup-arglist (langelem)
2006 "Hack for DWIMmery in c-lineup-arglist."
2008 (c-block-in-arglist-dwim (c-langelem-2nd-pos c-syntactic-element)))
2010 (c-lineup-arglist langelem)))
2012 (defun mdw-c-indent-extern-mumble (langelem)
2013 "Indent `extern \"...\" {' lines."
2015 (back-to-indentation)
2017 "\\s-*\\<extern\\>\\s-*\"\\([^\\\\\"]+\\|\\.\\)*\"\\s-*{")
2021 (defun mdw-c-indent-arglist-nested (langelem)
2022 "Indent continued argument lists.
2023 If we've nested more than one argument list, then only introduce a single
2024 indentation anyway."
2025 (let ((context c-syntactic-context)
2026 (pos (c-langelem-2nd-pos c-syntactic-element))
2027 (should-indent-p t))
2029 (eq (caar context) 'arglist-cont-nonempty))
2030 (when (and (= (caddr (pop context)) pos)
2032 (memq (caar context) '(arglist-intro
2033 arglist-cont-nonempty)))
2034 (setq should-indent-p nil)))
2035 (if should-indent-p '+ 0)))
2037 (defvar mdw-define-c-styles-hook nil
2038 "Hook run when `cc-mode' starts up to define styles.")
2040 (defun mdw-merge-style-alists (first second)
2042 (dolist (item first)
2043 (let ((key (car item)) (value (cdr item)))
2044 (if (string-suffix-p "-alist" (symbol-name key))
2046 (mdw-merge-style-alists value
2047 (cdr (assoc key second))))
2049 (push item output))))
2050 (dolist (item second)
2051 (unless (assoc (car item) first)
2052 (push item output)))
2055 (cl-defmacro mdw-define-c-style (name (&optional parent) &rest assocs)
2056 "Define a C style, called NAME (a symbol) based on PARENT, setting ASSOCs.
2057 A function, named `mdw-define-c-style/NAME', is defined to actually install
2058 the style using `c-add-style', and added to the hook
2059 `mdw-define-c-styles-hook'. If CC Mode is already loaded, then the style is
2061 (declare (indent defun))
2062 (let* ((name-string (symbol-name name))
2063 (var (intern (concat "mdw-c-style/" name-string)))
2064 (func (intern (concat "mdw-define-c-style/" name-string))))
2069 (let ((parent-list (intern (concat "mdw-c-style/"
2070 (symbol-name parent)))))
2071 `(mdw-merge-style-alists ',assocs ,parent-list))))
2072 (defun ,func () (c-add-style ,name-string ,var))
2073 (and (featurep 'cc-mode) (,func))
2074 (add-hook 'mdw-define-c-styles-hook ',func)
2077 (eval-after-load "cc-mode"
2078 '(run-hooks 'mdw-define-c-styles-hook))
2080 (mdw-define-c-style mdw-c ()
2081 (c-basic-offset . 2)
2082 (comment-column . 40)
2083 (c-class-key . "class")
2084 (c-backslash-column . 72)
2085 (c-label-minimum-indentation . 0)
2086 (c-offsets-alist (substatement-open . (add 0 c-indent-one-line-block))
2087 (defun-open . (add 0 c-indent-one-line-block))
2088 (arglist-cont-nonempty . mdw-c-lineup-arglist)
2089 (topmost-intro . mdw-c-indent-extern-mumble)
2090 (cpp-define-intro . 0)
2092 (inextern-lang . [0])
2098 (statement-cont . +)
2099 (statement-case-intro . +)))
2101 (mdw-define-c-style mdw-trustonic-basic-c (mdw-c)
2102 (c-basic-offset . 4)
2103 (comment-column . 0)
2104 (c-indent-comment-alist (anchored-comment . (column . 0))
2105 (end-block . (space . 1))
2106 (cpp-end-block . (space . 1))
2107 (other . (space . 1)))
2108 (c-offsets-alist (access-label . -2)))
2110 (mdw-define-c-style mdw-trustonic-c (mdw-trustonic-basic-c)
2111 (c-offsets-alist (arglist-cont-nonempty . mdw-c-indent-arglist-nested)))
2113 (defun mdw-set-default-c-style (modes style)
2114 "Update the default CC Mode style for MODES to be STYLE.
2116 MODES may be a list of major mode names or a singleton. STYLE is a style
2118 (let ((modes (if (listp modes) modes (list modes)))
2119 (style (symbol-name style)))
2120 (setq c-default-style
2121 (append (mapcar (lambda (mode)
2124 (remove-if (lambda (assoc)
2125 (memq (car assoc) modes))
2126 (if (listp c-default-style)
2128 (list (cons 'other c-default-style))))))))
2129 (setq c-default-style "mdw-c")
2131 (mdw-set-default-c-style '(c-mode c++-mode) 'mdw-c)
2133 (defvar mdw-c-comment-fill-prefix
2134 `((,(concat "\\([ \t]*/?\\)"
2137 "\\([A-Za-z]+:[ \t]*\\)?"
2138 mdw-hanging-indents)
2139 (pad . 1) (match . 2) (pad . 3) (pad . 4) (pad . 5)))
2140 "Fill prefix matching C comments (both kinds).")
2142 (defun mdw-fontify-c-and-c++ ()
2144 ;; Fiddle with some syntax codes.
2145 (modify-syntax-entry ?* ". 23")
2146 (modify-syntax-entry ?/ ". 124b")
2147 (modify-syntax-entry ?\n "> b")
2150 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2152 ;; Now define things to be fontified.
2153 (make-local-variable 'font-lock-keywords)
2155 (mdw-regexps "alignas" ;C11 macro, C++11
2157 "and" ;C++, C95 macro
2158 "and_eq" ;C++, C95 macro
2159 "asm" ;K&R, C++, GCC
2160 "atomic" ;C11 macro, C++11 template type
2162 "bitand" ;C++, C95 macro
2163 "bitor" ;C++, C95 macro
2164 "bool" ;C++, C99 macro
2169 "char16_t" ;C++11, C11 library type
2170 "char32_t" ;C++11, C11 library type
2172 "complex" ;C99 macro, C++ template type
2173 "compl" ;C++, C95 macro
2177 "continue" ;K&R, C89
2179 "defined" ;C89 preprocessor
2186 ;; "entry" ;K&R -- never used
2197 "imaginary" ;C99 macro
2198 "inline" ;C++, C99, GCC
2205 "noreturn" ;C11 macro
2206 "not" ;C++, C95 macro
2207 "not_eq" ;C++, C95 macro
2210 "or" ;C++, C95 macro
2211 "or_eq" ;C++, C95 macro
2215 "register" ;K&R, C89
2216 "reinterpret_cast" ;C++
2223 "static_assert" ;C11 macro, C++11
2230 "thread_local" ;C11 macro, C++11
2236 "unsigned" ;K&R, C89
2241 "wchar_t" ;C++, C89 library type
2243 "xor" ;C++, C95 macro
2244 "xor_eq" ;C++, C95 macro
2253 "_Pragma" ;C99 preprocessor
2254 "_Static_assert" ;C11
2255 "_Thread_local" ;C11
2258 "__attribute__" ;GCC
2261 "__extension__" ;GCC
2271 (mdw-regexps "false" ;C++, C99 macro
2273 "true" ;C++, C99 macro
2275 (preprocessor-keywords
2276 (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
2277 "ident" "if" "ifdef" "ifndef" "import" "include"
2278 "line" "pragma" "unassert" "undef" "warning"))
2280 (mdw-regexps "class" "defs" "encode" "end" "implementation"
2281 "interface" "private" "protected" "protocol" "public"
2284 (setq font-lock-keywords
2287 ;; Fontify include files as strings.
2288 (list (concat "^[ \t]*\\#[ \t]*"
2289 "\\(include\\|import\\)"
2290 "[ \t]*\\(<[^>]+>?\\)")
2291 '(2 font-lock-string-face))
2293 ;; Preprocessor directives are `references'?.
2294 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
2295 preprocessor-keywords
2296 "\\)\\>\\|[0-9]+\\|$\\)\\)")
2297 '(1 font-lock-keyword-face))
2299 ;; Handle the keywords defined above.
2300 (list (concat "@\\<\\(" objc-keywords "\\)\\>")
2301 '(0 font-lock-keyword-face))
2303 (list (concat "\\<\\(" c-keywords "\\)\\>")
2304 '(0 font-lock-keyword-face))
2306 (list (concat "\\<\\(" c-builtins "\\)\\>")
2307 '(0 font-lock-variable-name-face))
2309 ;; Handle numbers too.
2311 ;; This looks strange, I know. It corresponds to the
2312 ;; preprocessor's idea of what a number looks like, rather than
2313 ;; anything sensible.
2314 (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
2315 "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
2316 '(0 mdw-number-face))
2318 ;; And anything else is punctuation.
2319 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2320 '(0 mdw-punct-face))))))
2322 (define-derived-mode sod-mode c-mode "Sod"
2323 "Major mode for editing Sod code.")
2324 (push '("\\.sod$" . sod-mode) auto-mode-alist)
2326 (dolist (hook '(c-mode-hook objc-mode-hook c++-mode-hook))
2327 (add-hook hook 'mdw-misc-mode-config t)
2328 (add-hook hook 'mdw-fontify-c-and-c++ t))
2330 ;;;--------------------------------------------------------------------------
2333 (define-derived-mode apcalc-mode c-mode "AP Calc"
2334 "Major mode for editing Calc code.")
2336 (defun mdw-fontify-apcalc ()
2338 ;; Fiddle with some syntax codes.
2339 (modify-syntax-entry ?* ". 23")
2340 (modify-syntax-entry ?/ ". 14")
2343 (setq comment-start "/* ")
2344 (setq comment-end " */")
2345 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2347 ;; Now define things to be fontified.
2348 (make-local-variable 'font-lock-keywords)
2350 (mdw-regexps "break" "case" "cd" "continue" "define" "default"
2351 "do" "else" "exit" "for" "global" "goto" "help" "if"
2352 "local" "mat" "obj" "print" "quit" "read" "return"
2353 "show" "static" "switch" "while" "write")))
2355 (setq font-lock-keywords
2358 ;; Handle the keywords defined above.
2359 (list (concat "\\<\\(" c-keywords "\\)\\>")
2360 '(0 font-lock-keyword-face))
2362 ;; Handle numbers too.
2364 ;; This looks strange, I know. It corresponds to the
2365 ;; preprocessor's idea of what a number looks like, rather than
2366 ;; anything sensible.
2367 (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
2368 "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
2369 '(0 mdw-number-face))
2371 ;; And anything else is punctuation.
2372 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2373 '(0 mdw-punct-face))))))
2376 (add-hook 'apcalc-mode-hook 'mdw-misc-mode-config t)
2377 (add-hook 'apcalc-mode-hook 'mdw-fontify-apcalc t))
2379 ;;;--------------------------------------------------------------------------
2380 ;;; Java programming configuration.
2382 ;; Make indentation nice.
2384 (mdw-define-c-style mdw-java ()
2385 (c-basic-offset . 2)
2386 (c-backslash-column . 72)
2387 (c-offsets-alist (substatement-open . 0)
2392 (statement-case-intro . +)))
2393 (mdw-set-default-c-style 'java-mode 'mdw-java)
2395 ;; Declare Java fontification style.
2397 (defun mdw-fontify-java ()
2399 ;; Fiddle with some syntax codes.
2400 (modify-syntax-entry ?@ ".")
2401 (modify-syntax-entry ?@ "." font-lock-syntax-table)
2404 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2406 ;; Now define things to be fontified.
2407 (make-local-variable 'font-lock-keywords)
2408 (let ((java-keywords
2409 (mdw-regexps "abstract" "assert"
2410 "boolean" "break" "byte"
2411 "case" "catch" "char" "class" "const" "continue"
2412 "default" "do" "double"
2413 "else" "enum" "extends"
2414 "final" "finally" "float" "for"
2416 "if" "implements" "import" "instanceof" "int"
2420 "package" "private" "protected" "public"
2422 "short" "static" "strictfp" "switch" "synchronized"
2423 "throw" "throws" "transient" "try"
2428 (mdw-regexps "false" "null" "super" "this" "true")))
2430 (setq font-lock-keywords
2433 ;; Handle the keywords defined above.
2434 (list (concat "\\<\\(" java-keywords "\\)\\>")
2435 '(0 font-lock-keyword-face))
2437 ;; Handle the magic builtins defined above.
2438 (list (concat "\\<\\(" java-builtins "\\)\\>")
2439 '(0 font-lock-variable-name-face))
2441 ;; Handle numbers too.
2443 ;; The following isn't quite right, but it's close enough.
2444 (list (concat "\\<\\("
2445 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2446 "[0-9]+\\(\\.[0-9]*\\)?"
2447 "\\([eE][-+]?[0-9]+\\)?\\)"
2449 '(0 mdw-number-face))
2451 ;; And anything else is punctuation.
2452 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2453 '(0 mdw-punct-face))))))
2456 (add-hook 'java-mode-hook 'mdw-misc-mode-config t)
2457 (add-hook 'java-mode-hook 'mdw-fontify-java t))
2459 ;;;--------------------------------------------------------------------------
2460 ;;; Javascript programming configuration.
2462 (defun mdw-javascript-style ()
2463 (setq js-indent-level 2)
2464 (setq js-expr-indent-offset 0))
2466 (defun mdw-fontify-javascript ()
2469 (mdw-javascript-style)
2470 (setq js-auto-indent-flag t)
2472 ;; Now define things to be fontified.
2473 (make-local-variable 'font-lock-keywords)
2474 (let ((javascript-keywords
2475 (mdw-regexps "abstract" "boolean" "break" "byte" "case" "catch"
2476 "char" "class" "const" "continue" "debugger" "default"
2477 "delete" "do" "double" "else" "enum" "export" "extends"
2478 "final" "finally" "float" "for" "function" "goto" "if"
2479 "implements" "import" "in" "instanceof" "int"
2480 "interface" "let" "long" "native" "new" "package"
2481 "private" "protected" "public" "return" "short"
2482 "static" "super" "switch" "synchronized" "throw"
2483 "throws" "transient" "try" "typeof" "var" "void"
2484 "volatile" "while" "with" "yield"))
2485 (javascript-builtins
2486 (mdw-regexps "false" "null" "undefined" "Infinity" "NaN" "true"
2487 "arguments" "this")))
2489 (setq font-lock-keywords
2492 ;; Handle the keywords defined above.
2493 (list (concat "\\_<\\(" javascript-keywords "\\)\\_>")
2494 '(0 font-lock-keyword-face))
2496 ;; Handle the predefined builtins defined above.
2497 (list (concat "\\_<\\(" javascript-builtins "\\)\\_>")
2498 '(0 font-lock-variable-name-face))
2500 ;; Handle numbers too.
2502 ;; The following isn't quite right, but it's close enough.
2503 (list (concat "\\_<\\("
2504 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2505 "[0-9]+\\(\\.[0-9]*\\)?"
2506 "\\([eE][-+]?[0-9]+\\)?\\)"
2508 '(0 mdw-number-face))
2510 ;; And anything else is punctuation.
2511 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2512 '(0 mdw-punct-face))))))
2515 (add-hook 'js-mode-hook 'mdw-misc-mode-config t)
2516 (add-hook 'js-mode-hook 'mdw-fontify-javascript t))
2518 ;;;--------------------------------------------------------------------------
2519 ;;; Scala programming configuration.
2521 (defun mdw-fontify-scala ()
2524 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2526 ;; Define things to be fontified.
2527 (make-local-variable 'font-lock-keywords)
2528 (let ((scala-keywords
2529 (mdw-regexps "abstract" "case" "catch" "class" "def" "do" "else"
2530 "extends" "final" "finally" "for" "forSome" "if"
2531 "implicit" "import" "lazy" "match" "new" "object"
2532 "override" "package" "private" "protected" "return"
2533 "sealed" "throw" "trait" "try" "type" "val"
2534 "var" "while" "with" "yield"))
2536 (mdw-regexps "false" "null" "super" "this" "true"))
2537 (punctuation "[-!%^&*=+:@#~/?\\|`]"))
2539 (setq font-lock-keywords
2542 ;; Magical identifiers between backticks.
2543 (list (concat "`\\([^`]+\\)`")
2544 '(1 font-lock-variable-name-face))
2546 ;; Handle the keywords defined above.
2547 (list (concat "\\_<\\(" scala-keywords "\\)\\_>")
2548 '(0 font-lock-keyword-face))
2550 ;; Handle the constants defined above.
2551 (list (concat "\\_<\\(" scala-constants "\\)\\_>")
2552 '(0 font-lock-variable-name-face))
2554 ;; Magical identifiers between backticks.
2555 (list (concat "`\\([^`]+\\)`")
2556 '(1 font-lock-variable-name-face))
2558 ;; Handle numbers too.
2560 ;; As usual, not quite right.
2561 (list (concat "\\_<\\("
2562 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2563 "[0-9]+\\(\\.[0-9]*\\)?"
2564 "\\([eE][-+]?[0-9]+\\)?\\)"
2566 '(0 mdw-number-face))
2568 ;; And everything else is punctuation.
2569 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2570 '(0 mdw-punct-face)))
2572 font-lock-syntactic-keywords
2575 ;; Single quotes around characters. But not when used to quote
2576 ;; symbol names. Ugh.
2577 (list (concat "\\('\\)"
2579 "\\|" "\\\\" "\\(" "\\\\\\\\" "\\)*"
2580 "u+" "[0-9a-fA-F]\\{4\\}"
2581 "\\|" "\\\\" "[0-7]\\{1,3\\}"
2582 "\\|" "\\\\" "." "\\)"
2588 (add-hook 'scala-mode-hook 'mdw-misc-mode-config t)
2589 (add-hook 'scala-mode-hook 'mdw-fontify-scala t))
2591 ;;;--------------------------------------------------------------------------
2592 ;;; C# programming configuration.
2594 ;; Make indentation nice.
2596 (mdw-define-c-style mdw-csharp ()
2597 (c-basic-offset . 2)
2598 (c-backslash-column . 72)
2599 (c-offsets-alist (substatement-open . 0)
2604 (statement-case-intro . +)))
2605 (mdw-set-default-c-style 'csharp-mode 'mdw-csharp)
2607 ;; Declare C# fontification style.
2609 (defun mdw-fontify-csharp ()
2612 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2614 ;; Now define things to be fontified.
2615 (make-local-variable 'font-lock-keywords)
2616 (let ((csharp-keywords
2617 (mdw-regexps "abstract" "as" "bool" "break" "byte" "case" "catch"
2618 "char" "checked" "class" "const" "continue" "decimal"
2619 "default" "delegate" "do" "double" "else" "enum"
2620 "event" "explicit" "extern" "finally" "fixed" "float"
2621 "for" "foreach" "goto" "if" "implicit" "in" "int"
2622 "interface" "internal" "is" "lock" "long" "namespace"
2623 "new" "object" "operator" "out" "override" "params"
2624 "private" "protected" "public" "readonly" "ref"
2625 "return" "sbyte" "sealed" "short" "sizeof"
2626 "stackalloc" "static" "string" "struct" "switch"
2627 "throw" "try" "typeof" "uint" "ulong" "unchecked"
2628 "unsafe" "ushort" "using" "virtual" "void" "volatile"
2632 (mdw-regexps "base" "false" "null" "this" "true")))
2634 (setq font-lock-keywords
2637 ;; Handle the keywords defined above.
2638 (list (concat "\\<\\(" csharp-keywords "\\)\\>")
2639 '(0 font-lock-keyword-face))
2641 ;; Handle the magic builtins defined above.
2642 (list (concat "\\<\\(" csharp-builtins "\\)\\>")
2643 '(0 font-lock-variable-name-face))
2645 ;; Handle numbers too.
2647 ;; The following isn't quite right, but it's close enough.
2648 (list (concat "\\<\\("
2649 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2650 "[0-9]+\\(\\.[0-9]*\\)?"
2651 "\\([eE][-+]?[0-9]+\\)?\\)"
2653 '(0 mdw-number-face))
2655 ;; And anything else is punctuation.
2656 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2657 '(0 mdw-punct-face))))))
2659 (define-derived-mode csharp-mode java-mode "C#"
2660 "Major mode for editing C# code.")
2662 (add-hook 'csharp-mode-hook 'mdw-fontify-csharp t)
2664 ;;;--------------------------------------------------------------------------
2665 ;;; F# programming configuration.
2667 (setq fsharp-indent-offset 2)
2669 (defun mdw-fontify-fsharp ()
2671 (let ((punct "=<>+-*/|&%!@?"))
2673 ((>= i (length punct)))
2674 (modify-syntax-entry (aref punct i) ".")))
2676 (modify-syntax-entry ?_ "_")
2677 (modify-syntax-entry ?( "(")
2678 (modify-syntax-entry ?) ")")
2680 (setq indent-tabs-mode nil)
2682 (let ((fsharp-keywords
2683 (mdw-regexps "abstract" "and" "as" "assert" "atomic"
2685 "checked" "class" "component" "const" "constraint"
2686 "constructor" "continue"
2687 "default" "delegate" "do" "done" "downcast" "downto"
2688 "eager" "elif" "else" "end" "exception" "extern"
2689 "finally" "fixed" "for" "fori" "fun" "function"
2692 "if" "in" "include" "inherit" "inline" "interface"
2695 "match" "measure" "member" "method" "mixin" "module"
2698 "object" "of" "open" "or" "override"
2699 "parallel" "params" "private" "process" "protected"
2701 "rec" "recursive" "return"
2702 "sealed" "sig" "static" "struct"
2703 "tailcall" "then" "to" "trait" "try" "type"
2705 "val" "virtual" "void" "volatile"
2706 "when" "while" "with"
2710 (mdw-regexps "asr" "land" "lor" "lsl" "lsr" "lxor" "mod"
2711 "base" "false" "null" "true"))
2714 (mdw-regexps "do" "let" "return" "use" "yield"))
2716 (preprocessor-keywords
2717 (mdw-regexps "if" "indent" "else" "endif")))
2719 (setq font-lock-keywords
2720 (list (list (concat "\\(^\\|[^\"]\\)"
2723 "\\(" "[^)*]" "[^*]*" "\\*+" "\\)*"
2728 '(2 font-lock-comment-face))
2730 (list (concat "'" "\\("
2733 "\\|" "[0-9][0-9][0-9]"
2734 "\\|" "u" "[0-9a-fA-F]\\{4\\}"
2735 "\\|" "U" "[0-9a-fA-F]\\{8\\}"
2741 "\\(" "\\\\" "\\(.\\|\n\\)"
2744 '(0 font-lock-string-face))
2746 (list (concat "\\_<\\(" bang-keywords "\\)!" "\\|"
2747 "^#[ \t]*\\(" preprocessor-keywords "\\)\\_>"
2749 "\\_<\\(" fsharp-keywords "\\)\\_>")
2750 '(0 font-lock-keyword-face))
2751 (list (concat "\\<\\(" fsharp-builtins "\\)\\_>")
2752 '(0 font-lock-variable-name-face))
2754 (list (concat "\\_<"
2755 "\\(" "0[bB][01]+" "\\|"
2757 "0[xX][0-9a-fA-F]+" "\\)"
2758 "\\(" "lf\\|LF" "\\|"
2759 "[uU]?[ysnlL]?" "\\)"
2766 "\\([eE][-+]?[0-9]+\\)?"
2771 '(0 mdw-number-face))
2773 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2774 '(0 mdw-punct-face))))))
2776 (defun mdw-fontify-inferior-fsharp ()
2777 (mdw-fontify-fsharp)
2778 (setq font-lock-keywords
2779 (append (list (list "^[#-]" '(0 font-lock-comment-face))
2780 (list "^>" '(0 font-lock-keyword-face)))
2781 font-lock-keywords)))
2784 (add-hook 'fsharp-mode-hook 'mdw-misc-mode-config t)
2785 (add-hook 'fsharp-mode-hook 'mdw-fontify-fsharp t)
2786 (add-hook 'inferior-fsharp-mode-hooks 'mdw-fontify-inferior-fsharp t))
2788 ;;;--------------------------------------------------------------------------
2789 ;;; Go programming configuration.
2791 (defun mdw-fontify-go ()
2793 (make-local-variable 'font-lock-keywords)
2795 (mdw-regexps "break" "case" "chan" "const" "continue"
2796 "default" "defer" "else" "fallthrough" "for"
2797 "func" "go" "goto" "if" "import"
2798 "interface" "map" "package" "range" "return"
2799 "select" "struct" "switch" "type" "var"))
2801 (mdw-regexps "bool" "byte" "complex64" "complex128" "error"
2802 "float32" "float64" "int" "uint8" "int16" "int32"
2803 "int64" "rune" "string" "uint" "uint8" "uint16"
2804 "uint32" "uint64" "uintptr" "void"
2805 "false" "iota" "nil" "true"
2807 "append" "cap" "copy" "delete" "imag" "len" "make"
2808 "new" "panic" "real" "recover")))
2810 (setq font-lock-keywords
2813 ;; Handle the keywords defined above.
2814 (list (concat "\\<\\(" go-keywords "\\)\\>")
2815 '(0 font-lock-keyword-face))
2816 (list (concat "\\<\\(" go-intrinsics "\\)\\>")
2817 '(0 font-lock-variable-name-face))
2819 ;; Strings and characters.
2821 "\\(" "[^\\']" "\\|"
2823 "\\(" "[abfnrtv\\'\"]" "\\|"
2824 "[0-7]\\{3\\}" "\\|"
2825 "x" "[0-9A-Fa-f]\\{2\\}" "\\|"
2826 "u" "[0-9A-Fa-f]\\{4\\}" "\\|"
2827 "U" "[0-9A-Fa-f]\\{8\\}" "\\)" "\\)"
2831 "\\(" "[^\n\\\"]+" "\\|" "\\\\." "\\)*"
2835 '(0 font-lock-string-face))
2837 ;; Handle numbers too.
2839 ;; The following isn't quite right, but it's close enough.
2840 (list (concat "\\<\\("
2841 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2842 "[0-9]+\\(\\.[0-9]*\\)?"
2843 "\\([eE][-+]?[0-9]+\\)?\\)")
2844 '(0 mdw-number-face))
2846 ;; And anything else is punctuation.
2847 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2848 '(0 mdw-punct-face))))))
2850 (add-hook 'go-mode-hook 'mdw-misc-mode-config t)
2851 (add-hook 'go-mode-hook 'mdw-fontify-go t))
2853 ;;;--------------------------------------------------------------------------
2854 ;;; Rust programming configuration.
2856 (setq-default rust-indent-offset 2)
2858 (defun mdw-self-insert-and-indent (count)
2860 (self-insert-command count)
2861 (indent-according-to-mode))
2863 (defun mdw-fontify-rust ()
2865 ;; Hack syntax categories.
2866 (modify-syntax-entry ?$ ".")
2867 (modify-syntax-entry ?% ".")
2868 (modify-syntax-entry ?= ".")
2870 ;; Fontify keywords and things.
2871 (make-local-variable 'font-lock-keywords)
2872 (let ((rust-keywords
2873 (mdw-regexps "abstract" "alignof" "as" "async" "await"
2874 "become" "box" "break"
2875 "const" "continue" "crate"
2877 "else" "enum" "extern"
2881 "macro" "match" "mod" "move" "mut"
2882 "offsetof" "override"
2883 "priv" "proc" "pub" "pure"
2885 "sizeof" "static" "struct" "super"
2886 "trait" "try" "type" "typeof"
2887 "union" "unsafe" "unsized" "use"
2892 (mdw-regexps "array" "pointer" "slice" "tuple"
2893 "bool" "true" "false"
2895 "i8" "i16" "i32" "i64" "isize"
2896 "u8" "u16" "u32" "u64" "usize"
2899 (setq font-lock-keywords
2902 ;; Handle the keywords defined above.
2903 (list (concat "\\_<\\(" rust-keywords "\\)\\_>")
2904 '(0 font-lock-keyword-face))
2905 (list (concat "\\_<\\(" rust-builtins "\\)\\_>")
2906 '(0 font-lock-variable-name-face))
2908 ;; Handle numbers too.
2909 (list (concat "\\_<\\("
2911 "\\(" "\\(\\.[0-9_]+\\)?[eE][-+]?[0-9_]+"
2915 "\\|" "\\(" "[0-9][0-9_]*"
2916 "\\|" "0x[0-9a-fA-F_]+"
2920 "\\([ui]\\(8\\|16\\|32\\|64\\|size\\)\\)?"
2922 '(0 mdw-number-face))
2924 ;; And anything else is punctuation.
2925 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2926 '(0 mdw-punct-face)))))
2928 ;; Hack key bindings.
2929 (local-set-key [?{] 'mdw-self-insert-and-indent)
2930 (local-set-key [?}] 'mdw-self-insert-and-indent))
2933 (add-hook 'rust-mode-hook 'mdw-misc-mode-config t)
2934 (add-hook 'rust-mode-hook 'mdw-fontify-rust t))
2936 ;;;--------------------------------------------------------------------------
2937 ;;; Awk programming configuration.
2939 ;; Make Awk indentation nice.
2941 (mdw-define-c-style mdw-awk ()
2942 (c-basic-offset . 2)
2943 (c-offsets-alist (substatement-open . 0)
2944 (c-backslash-column . 72)
2945 (statement-cont . 0)
2946 (statement-case-intro . +)))
2947 (mdw-set-default-c-style 'awk-mode 'mdw-awk)
2949 ;; Declare Awk fontification style.
2951 (defun mdw-fontify-awk ()
2953 ;; Miscellaneous fiddling.
2954 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
2956 ;; Now define things to be fontified.
2957 (make-local-variable 'font-lock-keywords)
2959 (mdw-regexps "BEGIN" "END" "ARGC" "ARGIND" "ARGV" "CONVFMT"
2960 "ENVIRON" "ERRNO" "FIELDWIDTHS" "FILENAME" "FNR"
2961 "FS" "IGNORECASE" "NF" "NR" "OFMT" "OFS" "ORS" "RS"
2962 "RSTART" "RLENGTH" "RT" "SUBSEP"
2963 "atan2" "break" "close" "continue" "cos" "delete"
2964 "do" "else" "exit" "exp" "fflush" "file" "for" "func"
2965 "function" "gensub" "getline" "gsub" "if" "in"
2966 "index" "int" "length" "log" "match" "next" "rand"
2967 "return" "print" "printf" "sin" "split" "sprintf"
2968 "sqrt" "srand" "strftime" "sub" "substr" "system"
2969 "systime" "tolower" "toupper" "while")))
2971 (setq font-lock-keywords
2974 ;; Handle the keywords defined above.
2975 (list (concat "\\<\\(" c-keywords "\\)\\>")
2976 '(0 font-lock-keyword-face))
2978 ;; Handle numbers too.
2980 ;; The following isn't quite right, but it's close enough.
2981 (list (concat "\\<\\("
2982 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2983 "[0-9]+\\(\\.[0-9]*\\)?"
2984 "\\([eE][-+]?[0-9]+\\)?\\)"
2986 '(0 mdw-number-face))
2988 ;; And anything else is punctuation.
2989 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2990 '(0 mdw-punct-face))))))
2993 (add-hook 'awk-mode-hook 'mdw-misc-mode-config t)
2994 (add-hook 'awk-mode-hook 'mdw-fontify-awk t))
2996 ;;;--------------------------------------------------------------------------
2997 ;;; Perl programming style.
2999 ;; Perl indentation style.
3001 (setq-default perl-indent-level 2)
3003 (setq-default cperl-indent-level 2
3004 cperl-continued-statement-offset 2
3005 cperl-continued-brace-offset 0
3006 cperl-brace-offset -2
3007 cperl-brace-imaginary-offset 0
3008 cperl-label-offset 0)
3010 ;; Define perl fontification style.
3012 (defun mdw-fontify-perl ()
3014 ;; Miscellaneous fiddling.
3015 (modify-syntax-entry ?$ "\\")
3016 (modify-syntax-entry ?$ "\\" font-lock-syntax-table)
3017 (modify-syntax-entry ?: "." font-lock-syntax-table)
3018 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3020 ;; Now define fontification things.
3021 (make-local-variable 'font-lock-keywords)
3022 (let ((perl-keywords
3029 "ge" "given" "gt" "goto"
3031 "last" "le" "local" "lt"
3036 "redo" "require" "return"
3038 "undef" "unless" "until" "use"
3041 (setq font-lock-keywords
3044 ;; Set up the keywords defined above.
3045 (list (concat "\\<\\(" perl-keywords "\\)\\>")
3046 '(0 font-lock-keyword-face))
3048 ;; At least numbers are simpler than C.
3049 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
3050 "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\)?"
3051 "\\([eE][-+]?[0-9_]+\\)?")
3052 '(0 mdw-number-face))
3054 ;; And anything else is punctuation.
3055 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3056 '(0 mdw-punct-face))))))
3058 (defun perl-number-tests (&optional arg)
3059 "Assign consecutive numbers to lines containing `#t'. With ARG,
3060 strip numbers instead."
3063 (goto-char (point-min))
3064 (let ((i 0) (fmt (if arg "" " %4d")))
3065 (while (search-forward "#t" nil t)
3066 (delete-region (point) (line-end-position))
3068 (insert (format fmt i)))
3069 (goto-char (point-min))
3070 (if (re-search-forward "\\(tests\\s-*=>\\s-*\\)\\w*" nil t)
3071 (replace-match (format "\\1%d" i))))))
3073 (dolist (hook '(perl-mode-hook cperl-mode-hook))
3074 (add-hook hook 'mdw-misc-mode-config t)
3075 (add-hook hook 'mdw-fontify-perl t))
3077 ;;;--------------------------------------------------------------------------
3078 ;;; Python programming style.
3080 (setq-default py-indent-offset 2
3082 python-indent-offset 2
3083 python-fill-docstring-style 'symmetric)
3085 (defun mdw-fontify-pythonic (keywords)
3087 ;; Miscellaneous fiddling.
3088 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3089 (setq indent-tabs-mode nil)
3091 ;; Now define fontification things.
3092 (make-local-variable 'font-lock-keywords)
3093 (setq font-lock-keywords
3096 ;; Set up the keywords defined above.
3097 (list (concat "\\_<\\(" keywords "\\)\\_>")
3098 '(0 font-lock-keyword-face))
3100 ;; At least numbers are simpler than C.
3101 (list (concat "\\_<0\\([xX][0-9a-fA-F]+\\|[oO]?[0-7]+\\|[bB][01]+\\)\\|"
3102 "\\_<[0-9][0-9]*\\(\\.[0-9]*\\)?"
3103 "\\([eE][-+]?[0-9]+\\|[lL]\\)?")
3104 '(0 mdw-number-face))
3106 ;; And anything else is punctuation.
3107 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3108 '(0 mdw-punct-face)))))
3110 ;; Define Python fontification styles.
3112 (defun mdw-fontify-python ()
3113 (mdw-fontify-pythonic
3114 (mdw-regexps "and" "as" "assert" "break" "class" "continue" "def"
3115 "del" "elif" "else" "except" "exec" "finally" "for"
3116 "from" "global" "if" "import" "in" "is" "lambda"
3117 "not" "or" "pass" "print" "raise" "return" "try"
3118 "while" "with" "yield")))
3120 (defun mdw-fontify-pyrex ()
3121 (mdw-fontify-pythonic
3122 (mdw-regexps "and" "as" "assert" "break" "cdef" "class" "continue"
3123 "ctypedef" "def" "del" "elif" "else" "enum" "except" "exec"
3124 "extern" "finally" "for" "from" "global" "if"
3125 "import" "in" "is" "lambda" "not" "or" "pass" "print"
3126 "property" "raise" "return" "struct" "try" "while" "with"
3129 (define-derived-mode pyrex-mode python-mode "Pyrex"
3130 "Major mode for editing Pyrex source code")
3131 (setq auto-mode-alist
3132 (append '(("\\.pyx$" . pyrex-mode)
3133 ("\\.pxd$" . pyrex-mode)
3134 ("\\.pxi$" . pyrex-mode))
3138 (add-hook 'python-mode-hook 'mdw-misc-mode-config t)
3139 (add-hook 'python-mode-hook 'mdw-fontify-python t)
3140 (add-hook 'pyrex-mode-hook 'mdw-fontify-pyrex t))
3142 ;;;--------------------------------------------------------------------------
3143 ;;; Lua programming style.
3145 (setq-default lua-indent-level 2)
3147 (defun mdw-fontify-lua ()
3149 ;; Miscellaneous fiddling.
3150 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3152 ;; Now define fontification things.
3153 (make-local-variable 'font-lock-keywords)
3155 (mdw-regexps "and" "break" "do" "else" "elseif" "end"
3156 "false" "for" "function" "goto" "if" "in" "local"
3157 "nil" "not" "or" "repeat" "return" "then" "true"
3159 (setq font-lock-keywords
3162 ;; Set up the keywords defined above.
3163 (list (concat "\\_<\\(" lua-keywords "\\)\\_>")
3164 '(0 font-lock-keyword-face))
3166 ;; At least numbers are simpler than C.
3167 (list (concat "\\_<\\(" "0[xX]"
3168 "\\(" "[0-9a-fA-F]+"
3169 "\\(\\.[0-9a-fA-F]*\\)?"
3170 "\\|" "\\.[0-9a-fA-F]+"
3172 "\\([pP][-+]?[0-9]+\\)?"
3173 "\\|" "\\(" "[0-9]+"
3177 "\\([eE][-+]?[0-9]+\\)?"
3179 '(0 mdw-number-face))
3181 ;; And anything else is punctuation.
3182 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3183 '(0 mdw-punct-face))))))
3186 (add-hook 'lua-mode-hook 'mdw-misc-mode-config t)
3187 (add-hook 'lua-mode-hook 'mdw-fontify-lua t))
3189 ;;;--------------------------------------------------------------------------
3190 ;;; Icon programming style.
3192 ;; Icon indentation style.
3194 (setq-default icon-brace-offset 0
3195 icon-continued-brace-offset 0
3196 icon-continued-statement-offset 2
3197 icon-indent-level 2)
3199 ;; Define Icon fontification style.
3201 (defun mdw-fontify-icon ()
3203 ;; Miscellaneous fiddling.
3204 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3206 ;; Now define fontification things.
3207 (make-local-variable 'font-lock-keywords)
3208 (let ((icon-keywords
3209 (mdw-regexps "break" "by" "case" "create" "default" "do" "else"
3210 "end" "every" "fail" "global" "if" "initial"
3211 "invocable" "link" "local" "next" "not" "of"
3212 "procedure" "record" "repeat" "return" "static"
3213 "suspend" "then" "to" "until" "while"))
3214 (preprocessor-keywords
3215 (mdw-regexps "define" "else" "endif" "error" "ifdef" "ifndef"
3216 "include" "line" "undef")))
3217 (setq font-lock-keywords
3220 ;; Set up the keywords defined above.
3221 (list (concat "\\<\\(" icon-keywords "\\)\\>")
3222 '(0 font-lock-keyword-face))
3224 ;; The things that Icon calls keywords.
3225 (list "&\\sw+\\>" '(0 font-lock-variable-name-face))
3227 ;; At least numbers are simpler than C.
3228 (list (concat "\\<[0-9]+"
3229 "\\([rR][0-9a-zA-Z]+\\|"
3230 "\\.[0-9]+\\([eE][+-]?[0-9]+\\)?\\)\\>\\|"
3231 "\\.[0-9]+\\([eE][+-]?[0-9]+\\)?\\>")
3232 '(0 mdw-number-face))
3235 (list (concat "^[ \t]*$[ \t]*\\<\\("
3236 preprocessor-keywords
3238 '(0 font-lock-keyword-face))
3240 ;; And anything else is punctuation.
3241 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3242 '(0 mdw-punct-face))))))
3245 (add-hook 'icon-mode-hook 'mdw-misc-mode-config t)
3246 (add-hook 'icon-mode-hook 'mdw-fontify-icon t))
3248 ;;;--------------------------------------------------------------------------
3251 (defun mdw-fontify-asm ()
3252 (modify-syntax-entry ?' "\"")
3253 (modify-syntax-entry ?. "w")
3254 (modify-syntax-entry ?\n ">")
3255 (setf fill-prefix nil)
3256 (modify-syntax-entry ?. "_")
3257 (modify-syntax-entry ?* ". 23")
3258 (modify-syntax-entry ?/ ". 124b")
3259 (modify-syntax-entry ?\n "> b")
3260 (local-set-key ";" 'self-insert-command)
3261 (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)"))
3263 (defun mdw-asm-set-comment ()
3264 (modify-syntax-entry ?; "."
3266 (modify-syntax-entry asm-comment-char "< b")
3267 (setq comment-start (string asm-comment-char ? )))
3268 (add-hook 'asm-mode-local-variables-hook 'mdw-asm-set-comment)
3269 (put 'asm-comment-char 'safe-local-variable 'characterp)
3272 (add-hook 'asm-mode-hook 'mdw-misc-mode-config t)
3273 (add-hook 'asm-mode-hook 'mdw-fontify-asm t))
3275 ;;;--------------------------------------------------------------------------
3276 ;;; TCL configuration.
3278 (setq-default tcl-indent-level 2)
3280 (defun mdw-fontify-tcl ()
3282 (modify-syntax-entry ch "."))
3283 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3284 (make-local-variable 'font-lock-keywords)
3285 (setq font-lock-keywords
3287 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
3288 "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\)?"
3289 "\\([eE][-+]?[0-9_]+\\)?")
3290 '(0 mdw-number-face))
3291 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3292 '(0 mdw-punct-face)))))
3295 (add-hook 'tcl-mode-hook 'mdw-misc-mode-config t)
3296 (add-hook 'tcl-mode-hook 'mdw-fontify-tcl t))
3298 ;;;--------------------------------------------------------------------------
3299 ;;; Dylan programming configuration.
3301 (defun mdw-fontify-dylan ()
3303 (make-local-variable 'font-lock-keywords)
3305 ;; Horrors. `dylan-mode' sets the `major-mode' name after calling this
3306 ;; hook, which undoes all of our configuration.
3307 (setq major-mode 'dylan-mode)
3308 (font-lock-set-defaults)
3310 (let* ((word "[-_a-zA-Z!*@<>$%]+")
3311 (dylan-keywords (mdw-regexps
3313 "C-address" "C-callable-wrapper" "C-function"
3314 "C-mapped-subtype" "C-pointer-type" "C-struct"
3315 "C-subtype" "C-union" "C-variable"
3317 "above" "abstract" "afterwards" "all"
3318 "begin" "below" "block" "by"
3319 "case" "class" "cleanup" "constant" "create"
3321 "else" "elseif" "end" "exception" "export"
3322 "finally" "for" "from" "function"
3325 "if" "in" "instance" "interface" "iterate"
3327 "let" "library" "local"
3328 "macro" "method" "module"
3331 "select" "slot" "subclass"
3333 "unless" "until" "use"
3334 "variable" "virtual"
3336 (sharp-keywords (mdw-regexps
3337 "all-keys" "key" "next" "rest" "include"
3339 (setq font-lock-keywords
3340 (list (list (concat "\\<\\(" dylan-keywords
3341 "\\|" "with\\(out\\)?-" word
3343 '(0 font-lock-keyword-face))
3344 (list (concat "\\<" word ":" "\\|"
3345 "#\\(" sharp-keywords "\\)\\>")
3346 '(0 font-lock-variable-name-face))
3348 "\\([-+]\\|\\<\\)[0-9]+" "\\("
3349 "\\(\\.[0-9]+\\)?" "\\([eE][-+][0-9]+\\)?"
3352 "\\|" "\\.[0-9]+" "\\([eE][-+][0-9]+\\)?"
3355 "\\|" "#x[0-9a-zA-Z]+"
3357 '(0 mdw-number-face))
3359 "\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\|"
3360 "\\_<[-+*/=<>:&|]+\\_>"
3362 '(0 mdw-punct-face))))))
3365 (add-hook 'dylan-mode-hook 'mdw-misc-mode-config t)
3366 (add-hook 'dylan-mode-hook 'mdw-fontify-dylan t))
3368 ;;;--------------------------------------------------------------------------
3369 ;;; Algol 68 configuration.
3371 (setq-default a68-indent-step 2)
3373 (defun mdw-fontify-algol-68 ()
3375 ;; Fix up the syntax table.
3376 (modify-syntax-entry ?# "!" a68-mode-syntax-table)
3377 (dolist (ch '(?- ?+ ?= ?< ?> ?* ?/ ?| ?&))
3378 (modify-syntax-entry ch "." a68-mode-syntax-table))
3380 (make-local-variable 'font-lock-keywords)
3383 (let ((word "COMMENT"))
3384 (do ((regexp (concat "[^" (substring word 0 1) "]+")
3385 (concat regexp "\\|"
3386 (substring word 0 i)
3387 "[^" (substring word i (1+ i)) "]"))
3389 ((>= i (length word)) regexp)))))
3390 (setq font-lock-keywords
3391 (list (list (concat "\\<COMMENT\\>"
3392 "\\(" not-comment "\\)\\{0,5\\}"
3393 "\\(\\'\\|\\<COMMENT\\>\\)")
3394 '(0 font-lock-comment-face))
3395 (list (concat "\\<CO\\>"
3396 "\\([^C]+\\|C[^O]\\)\\{0,5\\}"
3397 "\\($\\|\\<CO\\>\\)")
3398 '(0 font-lock-comment-face))
3399 (list "\\<[A-Z_]+\\>"
3400 '(0 font-lock-keyword-face))
3404 "\\([eE][-+]?[0-9]+\\)?"
3406 '(0 mdw-number-face))
3407 (list "\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/"
3408 '(0 mdw-punct-face))))))
3410 (dolist (hook '(a68-mode-hook a68-mode-hooks))
3411 (add-hook hook 'mdw-misc-mode-config t)
3412 (add-hook hook 'mdw-fontify-algol-68 t))
3414 ;;;--------------------------------------------------------------------------
3415 ;;; REXX configuration.
3417 (defun mdw-rexx-electric-* ()
3422 (defun mdw-rexx-indent-newline-indent ()
3425 (if abbrev-mode (expand-abbrev))
3426 (newline-and-indent))
3428 (defun mdw-fontify-rexx ()
3430 ;; Various bits of fiddling.
3431 (setq mdw-auto-indent nil)
3432 (local-set-key [?\C-m] 'mdw-rexx-indent-newline-indent)
3433 (local-set-key [?*] 'mdw-rexx-electric-*)
3434 (dolist (ch '(?! ?? ?# ?@ ?$)) (modify-syntax-entry ch "w"))
3435 (dolist (ch '(?¬)) (modify-syntax-entry ch "."))
3436 (mdw-standard-fill-prefix "\\([ \t]*/?\*[ \t]*\\)")
3438 ;; Set up keywords and things for fontification.
3439 (make-local-variable 'font-lock-keywords-case-fold-search)
3440 (setq font-lock-keywords-case-fold-search t)
3442 (setq rexx-indent 2)
3443 (setq rexx-end-indent rexx-indent)
3444 (setq rexx-cont-indent rexx-indent)
3446 (make-local-variable 'font-lock-keywords)
3447 (let ((rexx-keywords
3448 (mdw-regexps "address" "arg" "by" "call" "digits" "do" "drop"
3449 "else" "end" "engineering" "exit" "expose" "for"
3450 "forever" "form" "fuzz" "if" "interpret" "iterate"
3451 "leave" "linein" "name" "nop" "numeric" "off" "on"
3452 "options" "otherwise" "parse" "procedure" "pull"
3453 "push" "queue" "return" "say" "select" "signal"
3454 "scientific" "source" "then" "trace" "to" "until"
3455 "upper" "value" "var" "version" "when" "while"
3458 "abbrev" "abs" "bitand" "bitor" "bitxor" "b2x"
3459 "center" "center" "charin" "charout" "chars"
3460 "compare" "condition" "copies" "c2d" "c2x"
3461 "datatype" "date" "delstr" "delword" "d2c" "d2x"
3462 "errortext" "format" "fuzz" "insert" "lastpos"
3463 "left" "length" "lineout" "lines" "max" "min"
3464 "overlay" "pos" "queued" "random" "reverse" "right"
3465 "sign" "sourceline" "space" "stream" "strip"
3466 "substr" "subword" "symbol" "time" "translate"
3467 "trunc" "value" "verify" "word" "wordindex"
3468 "wordlength" "wordpos" "words" "xrange" "x2b" "x2c"
3471 (setq font-lock-keywords
3474 ;; Set up the keywords defined above.
3475 (list (concat "\\<\\(" rexx-keywords "\\)\\>")
3476 '(0 font-lock-keyword-face))
3478 ;; Fontify all symbols the same way.
3479 (list (concat "\\<\\([0-9.][A-Za-z0-9.!?_#@$]*[Ee][+-]?[0-9]+\\|"
3480 "[A-Za-z0-9.!?_#@$]+\\)")
3481 '(0 font-lock-variable-name-face))
3483 ;; And everything else is punctuation.
3484 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3485 '(0 mdw-punct-face))))))
3488 (add-hook 'rexx-mode-hook 'mdw-misc-mode-config t)
3489 (add-hook 'rexx-mode-hook 'mdw-fontify-rexx t))
3491 ;;;--------------------------------------------------------------------------
3492 ;;; Standard ML programming style.
3494 (setq-default sml-nested-if-indent t
3497 sml-type-of-indent nil)
3499 (defun mdw-fontify-sml ()
3501 ;; Make underscore an honorary letter.
3502 (modify-syntax-entry ?' "w")
3505 (mdw-standard-fill-prefix "\\([ \t]*(\*[ \t]*\\)")
3507 ;; Now define fontification things.
3508 (make-local-variable 'font-lock-keywords)
3510 (mdw-regexps "abstype" "and" "andalso" "as"
3513 "else" "end" "eqtype" "exception"
3514 "fn" "fun" "functor"
3516 "if" "in" "include" "infix" "infixr"
3519 "of" "op" "open" "orelse"
3521 "sharing" "sig" "signature" "struct" "structure"
3524 "where" "while" "with" "withtype")))
3526 (setq font-lock-keywords
3529 ;; Set up the keywords defined above.
3530 (list (concat "\\<\\(" sml-keywords "\\)\\>")
3531 '(0 font-lock-keyword-face))
3533 ;; At least numbers are simpler than C.
3534 (list (concat "\\<\\~?"
3535 "\\(0\\([wW]?[xX][0-9a-fA-F]+\\|"
3537 "\\([0-9]+\\(\\.[0-9]+\\)?"
3540 '(0 mdw-number-face))
3542 ;; And anything else is punctuation.
3543 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3544 '(0 mdw-punct-face))))))
3547 (add-hook 'sml-mode-hook 'mdw-misc-mode-config t)
3548 (add-hook 'sml-mode-hook 'mdw-fontify-sml t))
3550 ;;;--------------------------------------------------------------------------
3551 ;;; Haskell configuration.
3553 (setq-default haskell-indent-offset 2)
3555 (defun mdw-fontify-haskell ()
3557 ;; Fiddle with syntax table to get comments right.
3558 (modify-syntax-entry ?' "_")
3559 (modify-syntax-entry ?- ". 12")
3560 (modify-syntax-entry ?\n ">")
3562 ;; Make punctuation be punctuation
3563 (let ((punct "=<>+-*/|&%!@?$.^:#`"))
3565 ((>= i (length punct)))
3566 (modify-syntax-entry (aref punct i) ".")))
3569 (mdw-standard-fill-prefix "\\([ \t]*{?--?[ \t]*\\)")
3571 ;; Fiddle with fontification.
3572 (make-local-variable 'font-lock-keywords)
3573 (let ((haskell-keywords
3575 "case" "ccall" "class"
3576 "data" "default" "deriving" "do"
3580 "if" "import" "in" "infix" "infixl" "infixr" "instance"
3593 (mdw-regexps "ACK" "BEL" "BS" "CAN" "CR" "DC1" "DC2" "DC3" "DC4"
3594 "DEL" "DLE" "EM" "ENQ" "EOT" "ESC" "ETB" "ETX" "FF"
3595 "FS" "GS" "HT" "LF" "NAK" "NUL" "RS" "SI" "SO" "SOH"
3596 "SP" "STX" "SUB" "SYN" "US" "VT")))
3598 (setq font-lock-keywords
3600 (list (concat "{-" "[^-]*" "\\(-+[^-}][^-]*\\)*"
3604 '(0 font-lock-comment-face))
3605 (list (concat "\\_<\\(" haskell-keywords "\\)\\_>")
3606 '(0 font-lock-keyword-face))
3607 (list (concat "'\\("
3611 "\\(" "[abfnrtv\\\"']" "\\|"
3612 "^" "\\(" control-sequences "\\|"
3613 "[]A-Z@[\\^_]" "\\)" "\\|"
3620 '(0 font-lock-string-face))
3621 (list "\\_<[A-Z]\\(\\sw+\\|\\s_+\\)*\\_>"
3622 '(0 font-lock-variable-name-face))
3623 (list (concat "\\_<0\\([xX][0-9a-fA-F]+\\|[oO][0-7]+\\)\\|"
3624 "\\_<[0-9]+\\(\\.[0-9]*\\)?"
3625 "\\([eE][-+]?[0-9]+\\)?")
3626 '(0 mdw-number-face))
3627 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3628 '(0 mdw-punct-face))))))
3631 (add-hook 'haskell-mode-hook 'mdw-misc-mode-config t)
3632 (add-hook 'haskell-mode-hook 'mdw-fontify-haskell t))
3634 ;;;--------------------------------------------------------------------------
3635 ;;; Erlang configuration.
3637 (setq-default erlang-electric-commands nil)
3639 (defun mdw-fontify-erlang ()
3642 (mdw-standard-fill-prefix "\\([ \t]*{?%*[ \t]*\\)")
3644 ;; Fiddle with fontification.
3645 (make-local-variable 'font-lock-keywords)
3646 (let ((erlang-keywords
3647 (mdw-regexps "after" "and" "andalso"
3648 "band" "begin" "bnot" "bor" "bsl" "bsr" "bxor"
3649 "case" "catch" "cond"
3650 "div" "end" "fun" "if" "let" "not"
3652 "query" "receive" "rem" "try" "when" "xor")))
3654 (setq font-lock-keywords
3657 '(0 font-lock-comment-face))
3658 (list (concat "\\<\\(" erlang-keywords "\\)\\>")
3659 '(0 font-lock-keyword-face))
3660 (list (concat "^-\\sw+\\>")
3661 '(0 font-lock-keyword-face))
3662 (list "\\<[0-9]+\\(#[0-9a-zA-Z]+\\|[eE][+-]?[0-9]+\\)?\\>"
3663 '(0 mdw-number-face))
3664 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3665 '(0 mdw-punct-face))))))
3668 (add-hook 'erlang-mode-hook 'mdw-misc-mode-config t)
3669 (add-hook 'erlang-mode-hook 'mdw-fontify-erlang t))
3671 ;;;--------------------------------------------------------------------------
3672 ;;; Texinfo configuration.
3674 (defun mdw-fontify-texinfo ()
3677 (mdw-standard-fill-prefix "\\([ \t]*@c[ \t]+\\)")
3679 ;; Real fontification things.
3680 (make-local-variable 'font-lock-keywords)
3681 (setq font-lock-keywords
3684 ;; Environment names are keywords.
3685 (list "@\\(end\\) *\\([a-zA-Z]*\\)?"
3686 '(2 font-lock-keyword-face))
3688 ;; Unmark escaped magic characters.
3689 (list "\\(@\\)\\([@{}]\\)"
3690 '(1 font-lock-keyword-face)
3691 '(2 font-lock-variable-name-face))
3693 ;; Make sure we get comments properly.
3694 (list "@c\\(omment\\)?\\( .*\\)?$"
3695 '(0 font-lock-comment-face))
3697 ;; Command names are keywords.
3698 (list "@\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)"
3699 '(0 font-lock-keyword-face))
3701 ;; Fontify TeX special characters as punctuation.
3703 '(0 mdw-punct-face)))))
3705 (dolist (hook '(texinfo-mode-hook TeXinfo-mode-hook))
3706 (add-hook hook 'mdw-misc-mode-config t)
3707 (add-hook hook 'mdw-fontify-texinfo t))
3709 ;;;--------------------------------------------------------------------------
3710 ;;; TeX and LaTeX configuration.
3712 (setq-default LaTeX-table-label "tbl:"
3713 TeX-auto-untabify nil
3714 LaTeX-syntactic-comments nil
3715 LaTeX-fill-break-at-separators '(\\\[))
3717 (defun mdw-fontify-tex ()
3718 (setq ispell-parser 'tex)
3721 ;; Don't make maths into a string.
3722 (modify-syntax-entry ?$ ".")
3723 (modify-syntax-entry ?$ "." font-lock-syntax-table)
3724 (local-set-key [?$] 'self-insert-command)
3726 ;; Make `tab' be useful, given that tab stops in TeX don't work well.
3727 (local-set-key "\C-\M-i" 'indent-relative)
3728 (setq indent-tabs-mode nil)
3731 (mdw-standard-fill-prefix "\\([ \t]*%+[ \t]*\\)")
3733 ;; Real fontification things.
3734 (make-local-variable 'font-lock-keywords)
3735 (setq font-lock-keywords
3738 ;; Environment names are keywords.
3739 (list (concat "\\\\\\(begin\\|end\\|newenvironment\\)"
3741 '(2 font-lock-keyword-face))
3743 ;; Suspended environment names are keywords too.
3744 (list (concat "\\\\\\(suspend\\|resume\\)\\(\\[[^]]*\\]\\)?"
3746 '(3 font-lock-keyword-face))
3748 ;; Command names are keywords.
3749 (list "\\\\\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)"
3750 '(0 font-lock-keyword-face))
3752 ;; Handle @/.../ for italics.
3753 ;; (list "\\(@/\\)\\([^/]*\\)\\(/\\)"
3754 ;; '(1 font-lock-keyword-face)
3755 ;; '(3 font-lock-keyword-face))
3757 ;; Handle @*...* for boldness.
3758 ;; (list "\\(@\\*\\)\\([^*]*\\)\\(\\*\\)"
3759 ;; '(1 font-lock-keyword-face)
3760 ;; '(3 font-lock-keyword-face))
3762 ;; Handle @`...' for literal syntax things.
3763 ;; (list "\\(@`\\)\\([^']*\\)\\('\\)"
3764 ;; '(1 font-lock-keyword-face)
3765 ;; '(3 font-lock-keyword-face))
3767 ;; Handle @<...> for nonterminals.
3768 ;; (list "\\(@<\\)\\([^>]*\\)\\(>\\)"
3769 ;; '(1 font-lock-keyword-face)
3770 ;; '(3 font-lock-keyword-face))
3772 ;; Handle other @-commands.
3773 ;; (list "@\\([^a-zA-Z]\\|[a-zA-Z]*\\)"
3774 ;; '(0 font-lock-keyword-face))
3776 ;; Make sure we get comments properly.
3778 '(0 font-lock-comment-face))
3780 ;; Fontify TeX special characters as punctuation.
3782 '(0 mdw-punct-face)))))
3784 (setq TeX-install-font-lock 'tex-font-setup)
3786 (eval-after-load 'font-latex
3787 '(defun font-latex-jit-lock-force-redisplay (buf start end)
3788 "Compatibility for Emacsen not offering `jit-lock-force-redisplay'."
3789 ;; The following block is an expansion of `jit-lock-force-redisplay'
3790 ;; and involved macros taken from CVS Emacs on 2007-04-28.
3791 (with-current-buffer buf
3792 (let ((modified (buffer-modified-p)))
3794 (let ((buffer-undo-list t)
3795 (inhibit-read-only t)
3796 (inhibit-point-motion-hooks t)
3797 (inhibit-modification-hooks t)
3800 buffer-file-truename)
3801 (put-text-property start end 'fontified t))
3803 (restore-buffer-modified-p nil)))))))
3805 (setq TeX-output-view-style
3807 ("^landscape$" "^pstricks$\\|^pst-\\|^psfrag$")
3808 "%(o?)dvips -t landscape %d -o && xdg-open %f")
3809 ("^dvi$" "^pstricks$\\|^pst-\\|^psfrag$"
3810 "%(o?)dvips %d -o && xdg-open %f")
3812 ("^a4\\(?:dutch\\|paper\\|wide\\)\\|sem-a4$" "^landscape$")
3813 "%(o?)xdvi %dS -paper a4r -s 0 %d")
3814 ("^dvi$" "^a4\\(?:dutch\\|paper\\|wide\\)\\|sem-a4$"
3815 "%(o?)xdvi %dS -paper a4 %d")
3817 ("^a5\\(?:comb\\|paper\\)$" "^landscape$")
3818 "%(o?)xdvi %dS -paper a5r -s 0 %d")
3819 ("^dvi$" "^a5\\(?:comb\\|paper\\)$" "%(o?)xdvi %dS -paper a5 %d")
3820 ("^dvi$" "^b5paper$" "%(o?)xdvi %dS -paper b5 %d")
3821 ("^dvi$" "^letterpaper$" "%(o?)xdvi %dS -paper us %d")
3822 ("^dvi$" "^legalpaper$" "%(o?)xdvi %dS -paper legal %d")
3823 ("^dvi$" "^executivepaper$" "%(o?)xdvi %dS -paper 7.25x10.5in %d")
3824 ("^dvi$" "." "%(o?)xdvi %dS %d")
3825 ("^pdf$" "." "xdg-open %o")
3826 ("^html?$" "." "sensible-browser %o")))
3828 (setq TeX-view-program-list
3829 '(("mupdf" ("mupdf %o" (mode-io-correlate " %(outpage)")))))
3831 (setq TeX-view-program-selection
3832 '(((output-dvi style-pstricks) "dvips and gv")
3834 (output-pdf "mupdf")
3835 (output-html "sensible-browser")))
3837 (setq TeX-open-quote "\""
3838 TeX-close-quote "\"")
3840 (setq reftex-use-external-file-finders t
3841 reftex-auto-recenter-toc t)
3843 (setq reftex-label-alist
3844 '(("theorem" ?T "th:" "~\\ref{%s}" t ("theorems?" "th\\.") -2)
3845 ("axiom" ?A "ax:" "~\\ref{%s}" t ("axioms?" "ax\\.") -2)
3846 ("definition" ?D "def:" "~\\ref{%s}" t ("definitions?" "def\\.") -2)
3847 ("proposition" ?P "prop:" "~\\ref{%s}" t
3848 ("propositions?" "prop\\.") -2)
3849 ("lemma" ?L "lem:" "~\\ref{%s}" t ("lemmas?" "lem\\.") -2)
3850 ("example" ?X "eg:" "~\\ref{%s}" t ("examples?") -2)
3851 ("exercise" ?E "ex:" "~\\ref{%s}" t ("exercises?" "ex\\.") -2)
3852 ("enumerate" ?i "i:" "~\\ref{%s}" item ("items?"))))
3853 (setq reftex-section-prefixes
3858 (setq bibtex-field-delimiters 'double-quotes
3859 bibtex-align-at-equal-sign t
3860 bibtex-entry-format '(realign opts-or-alts required-fields
3861 numerical-fields last-comma delimiters
3862 unify-case sort-fields braces)
3863 bibtex-sort-ignore-string-entries nil
3864 bibtex-maintain-sorted-entries 'entry-class
3865 bibtex-include-OPTkey t
3866 bibtex-autokey-names-stretch 1
3867 bibtex-autokey-expand-strings t
3868 bibtex-autokey-name-separator "-"
3869 bibtex-autokey-year-length 4
3870 bibtex-autokey-titleword-separator "-"
3871 bibtex-autokey-name-year-separator "-"
3872 bibtex-autokey-year-title-separator ":")
3875 (dolist (hook '(tex-mode-hook latex-mode-hook
3876 TeX-mode-hook LaTeX-mode-hook))
3877 (add-hook hook 'mdw-misc-mode-config t)
3878 (add-hook hook 'mdw-fontify-tex t))
3879 (add-hook 'bibtex-mode-hook (lambda () (setq fill-column 76))))
3881 ;;;--------------------------------------------------------------------------
3882 ;;; HTML, CSS, and other web foolishness.
3884 (setq-default css-indent-offset 2)
3886 ;;;--------------------------------------------------------------------------
3889 (setq-default psgml-html-build-new-buffer nil)
3891 (defun mdw-sgml-mode ()
3894 (mdw-standard-fill-prefix "")
3895 (make-local-variable 'sgml-delimiters)
3896 (setq sgml-delimiters
3897 '("AND" "&" "COM" "--" "CRO" "&#" "DSC" "]" "DSO" "[" "DTGC" "]"
3898 "DTGO" "[" "ERO" "&" "ETAGO" ":e" "GRPC" ")" "GRPO" "(" "LIT" "\""
3899 "LITA" "'" "MDC" ">" "MDO" "<!" "MINUS" "-" "MSC" "]]" "NESTC" "{"
3900 "NET" "}" "OPT" "?" "OR" "|" "PERO" "%" "PIC" ">" "PIO" "<?"
3901 "PLUS" "+" "REFC" "." "REP" "*" "RNI" "#" "SEQ" "," "STAGO" ":"
3902 "TAGC" "." "VI" "=" "MS-START" "<![" "MS-END" "]]>"
3903 "XML-ECOM" "-->" "XML-PIC" "?>" "XML-SCOM" "<!--" "XML-TAGCE" "/>"
3905 (setq major-mode 'mdw-sgml-mode)
3906 (setq mode-name "[mdw] SGML")
3907 (run-hooks 'mdw-sgml-mode-hook))
3909 ;;;--------------------------------------------------------------------------
3910 ;;; Configuration files.
3912 (defvar mdw-conf-quote-normal nil
3913 "*Control syntax category of quote characters `\"' and `''.
3914 If this is `t', consider quote characters to be normal
3915 punctuation, as for `conf-quote-normal'. If this is `nil' then
3916 leave quote characters as quotes. If this is a list, then
3917 consider the quote characters in the list to be normal
3918 punctuation. If this is a single quote character, then consider
3919 that character only to be normal punctuation.")
3920 (defun mdw-conf-quote-normal-acceptable-value-p (value)
3921 "Is the VALUE is an acceptable value for `mdw-conf-quote-normal'?"
3922 (or (booleanp value)
3923 (every (lambda (v) (memq v '(?\" ?')))
3924 (if (listp value) value (list value)))))
3925 (put 'mdw-conf-quote-normal 'safe-local-variable
3926 'mdw-conf-quote-normal-acceptable-value-p)
3928 (defun mdw-fix-up-quote ()
3929 "Apply the setting of `mdw-conf-quote-normal'."
3930 (let ((flag mdw-conf-quote-normal))
3932 (conf-quote-normal t))
3936 (let ((table (copy-syntax-table (syntax-table))))
3937 (dolist (ch (if (listp flag) flag (list flag)))
3938 (modify-syntax-entry ch "." table))
3939 (set-syntax-table table)
3940 (and font-lock-mode (font-lock-fontify-buffer)))))))
3943 (add-hook 'conf-mode-hook 'mdw-misc-mode-config t)
3944 (add-hook 'conf-mode-local-variables-hook 'mdw-fix-up-quote t t))
3946 ;;;--------------------------------------------------------------------------
3949 (defun mdw-setup-sh-script-mode ()
3951 ;; Fetch the shell interpreter's name.
3952 (let ((shell-name sh-shell-file))
3954 ;; Try reading the hash-bang line.
3956 (goto-char (point-min))
3957 (if (looking-at "#![ \t]*\\([^ \t\n]*\\)")
3958 (setq shell-name (match-string 1))))
3960 ;; Now try to set the shell.
3962 ;; Don't let `sh-set-shell' bugger up my script.
3963 (let ((executable-set-magic #'(lambda (s &rest r) s)))
3964 (sh-set-shell shell-name)))
3966 ;; Don't insert here-document scaffolding automatically.
3967 (local-set-key "<" 'self-insert-command)
3969 ;; Now enable my keys and the fontification.
3970 (mdw-misc-mode-config)
3972 ;; Set the indentation level correctly.
3973 (setq sh-indentation 2)
3974 (setq sh-basic-offset 2))
3976 (setq sh-shell-file "/bin/sh")
3978 ;; Awful hacking to override the shell detection for particular scripts.
3979 (defmacro define-custom-shell-mode (name shell)
3982 (set (make-local-variable 'sh-shell-file) ,shell)
3984 (define-custom-shell-mode bash-mode "/bin/bash")
3985 (define-custom-shell-mode rc-mode "/usr/bin/rc")
3986 (put 'sh-shell-file 'permanent-local t)
3988 ;; Hack the rc syntax table. Backquotes aren't paired in rc.
3989 (eval-after-load "sh-script"
3990 '(or (assq 'rc sh-mode-syntax-table-input)
4007 (assoc (assq 'rc sh-mode-syntax-table-input)))
4010 (setq sh-mode-syntax-table-input
4011 (cons (cons 'rc frag)
4012 sh-mode-syntax-table-input))))))
4015 (add-hook 'sh-mode-hook 'mdw-misc-mode-config t)
4016 (add-hook 'sh-mode-hook 'mdw-setup-sh-script-mode t))
4018 ;;;--------------------------------------------------------------------------
4019 ;;; Emacs shell mode.
4021 (defun mdw-eshell-prompt ()
4022 (let ((left "[") (right "]"))
4023 (when (= (user-uid) 0)
4024 (setq left "«" right "»"))
4027 (replace-regexp-in-string "\\..*$" "" (system-name)))
4029 (let* ((pwd (eshell/pwd)) (npwd (length pwd))
4030 (home (expand-file-name "~")) (nhome (length home)))
4031 (if (and (>= npwd nhome)
4033 (= (elt pwd nhome) ?/))
4034 (string= (substring pwd 0 nhome) home))
4035 (concat "~" (substring pwd (length home)))
4038 (setq-default eshell-prompt-function 'mdw-eshell-prompt)
4039 (setq-default eshell-prompt-regexp "^\\[[^]>]+\\(\\]\\|>>?\\)")
4041 (defun eshell/e (file) (find-file file) nil)
4042 (defun eshell/ee (file) (find-file-other-window file) nil)
4043 (defun eshell/w3m (url) (w3m-goto-url url) nil)
4045 (mdw-define-face eshell-prompt (t :weight bold))
4046 (mdw-define-face eshell-ls-archive (t :weight bold :foreground "red"))
4047 (mdw-define-face eshell-ls-backup (t :foreground "lightgrey" :slant italic))
4048 (mdw-define-face eshell-ls-product (t :foreground "lightgrey" :slant italic))
4049 (mdw-define-face eshell-ls-clutter (t :foreground "lightgrey" :slant italic))
4050 (mdw-define-face eshell-ls-executable (t :weight bold))
4051 (mdw-define-face eshell-ls-directory (t :foreground "cyan" :weight bold))
4052 (mdw-define-face eshell-ls-readonly (t nil))
4053 (mdw-define-face eshell-ls-symlink (t :foreground "cyan"))
4055 (defun mdw-eshell-hack () (setenv "LD_PRELOAD" nil))
4056 (add-hook 'eshell-mode-hook 'mdw-eshell-hack)
4058 ;;;--------------------------------------------------------------------------
4059 ;;; Messages-file mode.
4061 (defun messages-mode-guts ()
4062 (setq messages-mode-syntax-table (make-syntax-table))
4063 (set-syntax-table messages-mode-syntax-table)
4064 (modify-syntax-entry ?0 "w" messages-mode-syntax-table)
4065 (modify-syntax-entry ?1 "w" messages-mode-syntax-table)
4066 (modify-syntax-entry ?2 "w" messages-mode-syntax-table)
4067 (modify-syntax-entry ?3 "w" messages-mode-syntax-table)
4068 (modify-syntax-entry ?4 "w" messages-mode-syntax-table)
4069 (modify-syntax-entry ?5 "w" messages-mode-syntax-table)
4070 (modify-syntax-entry ?6 "w" messages-mode-syntax-table)
4071 (modify-syntax-entry ?7 "w" messages-mode-syntax-table)
4072 (modify-syntax-entry ?8 "w" messages-mode-syntax-table)
4073 (modify-syntax-entry ?9 "w" messages-mode-syntax-table)
4074 (make-local-variable 'comment-start)
4075 (make-local-variable 'comment-end)
4076 (make-local-variable 'indent-line-function)
4077 (setq indent-line-function 'indent-relative)
4078 (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
4079 (make-local-variable 'font-lock-defaults)
4080 (make-local-variable 'messages-mode-keywords)
4082 (mdw-regexps "array" "bitmap" "callback" "docs[ \t]+enum"
4083 "export" "enum" "fixed-octetstring" "flags"
4084 "harmless" "map" "nested" "optional"
4085 "optional-tagged" "package" "primitive"
4086 "primitive-nullfree" "relaxed[ \t]+enum"
4087 "set" "table" "tagged-optional" "union"
4088 "variadic" "vector" "version" "version-tag")))
4089 (setq messages-mode-keywords
4091 (list (concat "\\<\\(" keywords "\\)\\>:")
4092 '(0 font-lock-keyword-face))
4093 '("\\([-a-zA-Z0-9]+:\\)" (0 font-lock-warning-face))
4094 '("\\(\\<[a-z][-_a-zA-Z0-9]*\\)"
4095 (0 font-lock-variable-name-face))
4096 '("\\<\\([0-9]+\\)\\>" (0 mdw-number-face))
4097 '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4098 (0 mdw-punct-face)))))
4099 (setq font-lock-defaults
4100 '(messages-mode-keywords nil nil nil nil))
4101 (run-hooks 'messages-file-hook))
4103 (defun messages-mode ()
4106 (setq major-mode 'messages-mode)
4107 (setq mode-name "Messages")
4108 (messages-mode-guts)
4109 (modify-syntax-entry ?# "<" messages-mode-syntax-table)
4110 (modify-syntax-entry ?\n ">" messages-mode-syntax-table)
4111 (setq comment-start "# ")
4112 (setq comment-end "")
4113 (run-hooks 'messages-mode-hook))
4115 (defun cpp-messages-mode ()
4118 (setq major-mode 'cpp-messages-mode)
4119 (setq mode-name "CPP Messages")
4120 (messages-mode-guts)
4121 (modify-syntax-entry ?* ". 23" messages-mode-syntax-table)
4122 (modify-syntax-entry ?/ ". 14" messages-mode-syntax-table)
4123 (setq comment-start "/* ")
4124 (setq comment-end " */")
4125 (let ((preprocessor-keywords
4126 (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
4127 "ident" "if" "ifdef" "ifndef" "import" "include"
4128 "line" "pragma" "unassert" "undef" "warning")))
4129 (setq messages-mode-keywords
4130 (append (list (list (concat "^[ \t]*\\#[ \t]*"
4131 "\\(include\\|import\\)"
4132 "[ \t]*\\(<[^>]+\\(>\\)?\\)")
4133 '(2 font-lock-string-face))
4134 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
4135 preprocessor-keywords
4136 "\\)\\>\\|[0-9]+\\|$\\)\\)")
4137 '(1 font-lock-keyword-face)))
4138 messages-mode-keywords)))
4139 (run-hooks 'cpp-messages-mode-hook))
4142 (add-hook 'messages-mode-hook 'mdw-misc-mode-config t)
4143 (add-hook 'cpp-messages-mode-hook 'mdw-misc-mode-config t)
4144 ;; (add-hook 'messages-file-hook 'mdw-fontify-messages t)
4147 ;;;--------------------------------------------------------------------------
4148 ;;; Messages-file mode.
4150 (defvar mallow-driver-substitution-face 'mallow-driver-substitution-face
4151 "Face to use for subsittution directives.")
4152 (make-face 'mallow-driver-substitution-face)
4153 (defvar mallow-driver-text-face 'mallow-driver-text-face
4154 "Face to use for body text.")
4155 (make-face 'mallow-driver-text-face)
4157 (defun mallow-driver-mode ()
4160 (setq major-mode 'mallow-driver-mode)
4161 (setq mode-name "Mallow driver")
4162 (setq mallow-driver-mode-syntax-table (make-syntax-table))
4163 (set-syntax-table mallow-driver-mode-syntax-table)
4164 (make-local-variable 'comment-start)
4165 (make-local-variable 'comment-end)
4166 (make-local-variable 'indent-line-function)
4167 (setq indent-line-function 'indent-relative)
4168 (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
4169 (make-local-variable 'font-lock-defaults)
4170 (make-local-variable 'mallow-driver-mode-keywords)
4172 (mdw-regexps "each" "divert" "file" "if"
4173 "perl" "set" "string" "type" "write")))
4174 (setq mallow-driver-mode-keywords
4176 (list (concat "^%\\s *\\(}\\|\\(" keywords "\\)\\>\\).*$")
4177 '(0 font-lock-keyword-face))
4178 (list "^%\\s *\\(#.*\\)?$"
4179 '(0 font-lock-comment-face))
4181 '(0 font-lock-keyword-face))
4182 (list "^|?\\(.+\\)$" '(1 mallow-driver-text-face))
4184 '(0 mallow-driver-substitution-face t)))))
4185 (setq font-lock-defaults
4186 '(mallow-driver-mode-keywords nil nil nil nil))
4187 (modify-syntax-entry ?\" "_" mallow-driver-mode-syntax-table)
4188 (modify-syntax-entry ?\n ">" mallow-driver-mode-syntax-table)
4189 (setq comment-start "%# ")
4190 (setq comment-end "")
4191 (run-hooks 'mallow-driver-mode-hook))
4194 (add-hook 'mallow-driver-hook 'mdw-misc-mode-config t))
4196 ;;;--------------------------------------------------------------------------
4199 (defun nfast-debug-mode ()
4202 (setq major-mode 'nfast-debug-mode)
4203 (setq mode-name "NFast debug")
4204 (setq messages-mode-syntax-table (make-syntax-table))
4205 (set-syntax-table messages-mode-syntax-table)
4206 (make-local-variable 'font-lock-defaults)
4207 (make-local-variable 'nfast-debug-mode-keywords)
4208 (setq truncate-lines t)
4209 (setq nfast-debug-mode-keywords
4211 '("^\\(NFast_\\(Connect\\|Disconnect\\|Submit\\|Wait\\)\\)"
4212 (0 font-lock-keyword-face))
4213 (list (concat "^[ \t]+\\(\\("
4214 "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
4215 "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
4217 "[0-9a-fA-F]+\\)[ \t]*$")
4218 '(0 mdw-number-face))
4219 '("^[ \t]+\.status=[ \t]+\\<\\(OK\\)\\>"
4220 (1 font-lock-keyword-face))
4221 '("^[ \t]+\.status=[ \t]+\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>"
4222 (1 font-lock-warning-face))
4223 '("^[ \t]+\.status[ \t]+\\<\\(zero\\)\\>"
4225 (list (concat "^[ \t]+\\.cmd=[ \t]+"
4226 "\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>")
4227 '(1 font-lock-keyword-face))
4228 '("-?\\<\\([0-9]+\\|0x[0-9a-fA-F]+\\)\\>" (0 mdw-number-face))
4229 '("^\\([ \t]+[a-z0-9.]+\\)" (0 font-lock-variable-name-face))
4230 '("\\<\\([a-z][a-z0-9.]+\\)\\>=" (1 font-lock-variable-name-face))
4231 '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" (0 mdw-punct-face))))
4232 (setq font-lock-defaults
4233 '(nfast-debug-mode-keywords nil nil nil nil))
4234 (run-hooks 'nfast-debug-mode-hook))
4236 ;;;--------------------------------------------------------------------------
4237 ;;; Lispy languages.
4239 ;; Unpleasant bodge.
4240 (unless (boundp 'slime-repl-mode-map)
4241 (setq slime-repl-mode-map (make-sparse-keymap)))
4243 (defun mdw-indent-newline-and-indent ()
4245 (indent-for-tab-command)
4246 (newline-and-indent))
4248 (eval-after-load "cl-indent"
4250 (mapc #'(lambda (pair)
4252 'common-lisp-indent-function
4254 '((destructuring-bind . ((&whole 4 &rest 1) 4 &body))
4255 (multiple-value-bind . ((&whole 4 &rest 1) 4 &body))))))
4257 (defun mdw-common-lisp-indent ()
4258 (make-local-variable 'lisp-indent-function)
4259 (setq lisp-indent-function 'common-lisp-indent-function))
4261 (setq-default lisp-simple-loop-indentation 2
4262 lisp-loop-keyword-indentation 6
4263 lisp-loop-forms-indentation 6)
4265 (defmacro mdw-advise-hyperspec-lookup (func args)
4266 `(defadvice ,func (around mdw-browse-w3m ,args activate compile)
4268 (let ((browse-url-browser-function #'mdw-w3m-browse-url))
4271 (mdw-advise-hyperspec-lookup common-lisp-hyperspec (symbol))
4272 (mdw-advise-hyperspec-lookup common-lisp-hyperspec-format (char))
4273 (mdw-advise-hyperspec-lookup common-lisp-hyperspec-lookup-reader-macro (char))
4275 (defun mdw-fontify-lispy ()
4278 (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)")
4280 ;; Not much fontification needed.
4281 (make-local-variable 'font-lock-keywords)
4282 (setq font-lock-keywords
4283 (list (list (concat "\\("
4285 "\\(" "[0-9]+/[0-9]+"
4286 "\\|" "\\(" "[0-9]+" "\\(\\.[0-9]*\\)?" "\\|"
4288 "\\([dDeEfFlLsS][-+]?[0-9]+\\)?"
4293 "[0-9A-Fa-f]+" "\\(/[0-9A-Fa-f]+\\)?"
4294 "\\|" "o" "[-+]?" "[0-7]+" "\\(/[0-7]+\\)?"
4295 "\\|" "b" "[-+]?" "[01]+" "\\(/[01]+\\)?"
4296 "\\|" "[0-9]+" "r" "[-+]?"
4297 "[0-9a-zA-Z]+" "\\(/[0-9a-zA-Z]+\\)?"
4300 '(0 mdw-number-face))
4301 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4302 '(0 mdw-punct-face)))))
4307 (if (not mdw-fast-startup)
4309 (require 'slime-autoloads)
4310 (slime-setup '(slime-autodoc slime-c-p-c)))))
4312 (let ((stuff '((cmucl ("cmucl"))
4313 (sbcl ("sbcl") :coding-system utf-8-unix)
4314 (clisp ("clisp") :coding-system utf-8-unix))))
4315 (or (boundp 'slime-lisp-implementations)
4316 (setq slime-lisp-implementations nil))
4318 (let* ((head (car stuff))
4319 (found (assq (car head) slime-lisp-implementations)))
4320 (setq stuff (cdr stuff))
4322 (rplacd found (cdr head))
4323 (setq slime-lisp-implementations
4324 (cons head slime-lisp-implementations))))))
4325 (setq slime-default-lisp 'sbcl)
4330 (dolist (hook '(emacs-lisp-mode-hook
4333 inferior-lisp-mode-hook
4334 lisp-interaction-mode-hook
4336 slime-repl-mode-hook))
4337 (add-hook hook 'mdw-misc-mode-config t)
4338 (add-hook hook 'mdw-fontify-lispy t))
4339 (add-hook 'lisp-mode-hook 'mdw-common-lisp-indent t)
4340 (add-hook 'inferior-lisp-mode-hook
4341 #'(lambda () (local-set-key "\C-m" 'comint-send-and-indent)) t))
4343 ;;;--------------------------------------------------------------------------
4344 ;;; Other languages.
4348 (defun mdw-setup-smalltalk ()
4349 (and mdw-auto-indent
4350 (local-set-key "\C-m" 'smalltalk-newline-and-indent))
4351 (make-local-variable 'mdw-auto-indent)
4352 (setq mdw-auto-indent nil)
4353 (local-set-key "\C-i" 'smalltalk-reindent))
4355 (defun mdw-fontify-smalltalk ()
4356 (make-local-variable 'font-lock-keywords)
4357 (setq font-lock-keywords
4359 (list "\\<[A-Z][a-zA-Z0-9]*\\>"
4360 '(0 font-lock-keyword-face))
4361 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
4362 "[0-9][0-9_]*\\(\\.[0-9_]*\\)?"
4363 "\\([eE][-+]?[0-9_]+\\)?")
4364 '(0 mdw-number-face))
4365 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
4366 '(0 mdw-punct-face)))))
4369 (add-hook 'smalltalk-mode 'mdw-misc-mode-config t)
4370 (add-hook 'smalltalk-mode 'mdw-fontify-smalltalk t))
4374 (defun mdw-setup-m4 ()
4376 ;; Inexplicably, Emacs doesn't match braces in m4 mode. This is very
4377 ;; annoying: fix it.
4378 (modify-syntax-entry ?{ "(")
4379 (modify-syntax-entry ?} ")")
4382 (mdw-standard-fill-prefix "\\([ \t]*\\(?:#+\\|\\<dnl\\>\\)[ \t]*\\)"))
4384 (dolist (hook '(m4-mode-hook autoconf-mode-hook autotest-mode-hook))
4385 (add-hook hook #'mdw-misc-mode-config t)
4386 (add-hook hook #'mdw-setup-m4 t))
4391 (add-hook 'makefile-mode-hook 'mdw-misc-mode-config t))
4393 ;;;--------------------------------------------------------------------------
4396 (defun mdw-text-mode ()
4397 (setq fill-column 72)
4399 (mdw-standard-fill-prefix
4400 "\\([ \t]*\\([>#|:] ?\\)*[ \t]*\\)" 3)
4403 (eval-after-load "flyspell"
4404 '(define-key flyspell-mode-map "\C-\M-i" nil))
4407 (add-hook 'text-mode-hook 'mdw-text-mode t))
4409 ;;;--------------------------------------------------------------------------
4410 ;;; Outline and hide/show modes.
4412 (defun mdw-outline-collapse-all ()
4413 "Completely collapse everything in the entire buffer."
4416 (goto-char (point-min))
4417 (while (< (point) (point-max))
4421 (setq hs-hide-comments-when-hiding-all nil)
4423 (defadvice hs-hide-all (after hide-first-comment activate)
4424 (save-excursion (hs-hide-initial-comment-block)))
4426 ;;;--------------------------------------------------------------------------
4429 (defun mdw-sh-mode-setup ()
4430 (local-set-key [?\C-a] 'comint-bol)
4431 (add-hook 'comint-output-filter-functions
4432 'comint-watch-for-password-prompt))
4434 (defun mdw-term-mode-setup ()
4435 (setq term-prompt-regexp shell-prompt-pattern)
4436 (make-local-variable 'mouse-yank-at-point)
4437 (make-local-variable 'transient-mark-mode)
4438 (setq mouse-yank-at-point t)
4442 (defun term-send-meta-right () (interactive) (term-send-raw-string "\e\e[C"))
4443 (defun term-send-meta-left () (interactive) (term-send-raw-string "\e\e[D"))
4444 (defun term-send-ctrl-uscore () (interactive) (term-send-raw-string "\C-_"))
4445 (defun term-send-meta-meta-something ()
4447 (term-send-raw-string "\e\e")
4449 (eval-after-load 'term
4451 (define-key term-raw-map [?\e ?\e] nil)
4452 (define-key term-raw-map [?\e ?\e t] 'term-send-meta-meta-something)
4453 (define-key term-raw-map [?\C-/] 'term-send-ctrl-uscore)
4454 (define-key term-raw-map [M-right] 'term-send-meta-right)
4455 (define-key term-raw-map [?\e ?\M-O ?C] 'term-send-meta-right)
4456 (define-key term-raw-map [M-left] 'term-send-meta-left)
4457 (define-key term-raw-map [?\e ?\M-O ?D] 'term-send-meta-left)))
4459 (defadvice term-exec (before program-args-list compile activate)
4460 "If the PROGRAM argument is a list, interpret it as (PROGRAM . SWITCHES).
4461 This allows you to pass a list of arguments through `ansi-term'."
4462 (let ((program (ad-get-arg 2)))
4465 (ad-set-arg 2 (car program))
4466 (ad-set-arg 4 (cdr program))))))
4468 (defadvice term-exec-1 (around hack-environment compile activate)
4469 "Hack the environment inherited by inferiors in the terminal."
4470 (let ((process-environment (copy-tree process-environment)))
4471 (setenv "LD_PRELOAD" nil)
4474 (defadvice shell (around hack-environment compile activate)
4475 "Hack the environment inherited by inferiors in the shell."
4476 (let ((process-environment (copy-tree process-environment)))
4477 (setenv "LD_PRELOAD" nil)
4481 "Open a terminal containing an ssh session to the HOST."
4482 (interactive "sHost: ")
4483 (ansi-term (list "ssh" host) (format "ssh@%s" host)))
4485 (defvar git-grep-command
4486 "env GIT_PAGER=cat git grep --no-color -nH -e "
4487 "*The default command for \\[git-grep].")
4489 (defvar git-grep-history nil)
4491 (defun git-grep (command-args)
4492 "Run `git grep' with user-specified args and collect output in a buffer."
4494 (list (read-shell-command "Run git grep (like this): "
4495 git-grep-command 'git-grep-history)))
4496 (let ((grep-use-null-device nil))
4497 (grep command-args)))
4499 ;;;--------------------------------------------------------------------------
4500 ;;; Magit configuration.
4502 (setq magit-diff-refine-hunk 't
4503 magit-view-git-manual-method 'man
4504 magit-log-margin '(nil age magit-log-margin-width t 18)
4505 magit-wip-after-save-local-mode-lighter ""
4506 magit-wip-after-apply-mode-lighter ""
4507 magit-wip-before-change-mode-lighter "")
4508 (eval-after-load "magit"
4509 '(progn (global-magit-file-mode 1)
4510 (magit-wip-after-save-mode 1)
4511 (magit-wip-after-apply-mode 1)
4512 (magit-wip-before-change-mode 1)
4513 (add-to-list 'magit-no-confirm 'safe-with-wip)
4514 (add-to-list 'magit-no-confirm 'trash)
4515 (push '(:eval (if (or magit-wip-after-save-local-mode
4516 magit-wip-after-apply-mode
4517 magit-wip-before-change-mode)
4518 (format " wip:%s%s%s"
4519 (if magit-wip-after-apply-mode "A" "")
4520 (if magit-wip-before-change-mode "C" "")
4521 (if magit-wip-after-save-local-mode "S" ""))))
4523 (dolist (popup '(magit-diff-popup
4524 magit-diff-refresh-popup
4525 magit-diff-mode-refresh-popup
4526 magit-revision-mode-refresh-popup))
4527 (magit-define-popup-switch popup ?R "Reverse diff" "-R"))))
4529 (defadvice magit-wip-commit-buffer-file
4530 (around mdw-just-this-buffer activate compile)
4531 (let ((magit-save-repository-buffers nil)) ad-do-it))
4533 (defadvice magit-discard
4534 (around mdw-delete-if-prefix-argument activate compile)
4535 (let ((magit-delete-by-moving-to-trash
4536 (and (null current-prefix-arg)
4537 magit-delete-by-moving-to-trash)))
4540 (setq magit-repolist-columns
4541 '(("Name" 16 magit-repolist-column-ident nil)
4542 ("Version" 18 magit-repolist-column-version nil)
4543 ("St" 2 magit-repolist-column-dirty nil)
4544 ("L<U" 3 mdw-repolist-column-unpulled-from-upstream nil)
4545 ("L>U" 3 mdw-repolist-column-unpushed-to-upstream nil)
4546 ("Path" 32 magit-repolist-column-path nil)))
4548 (setq magit-repository-directories '(("~/etc/profile" . 0)
4551 (defadvice magit-list-repos (around mdw-dirname () activate compile)
4552 "Make sure the returned names are directory names.
4553 Otherwise child processes get started in the wrong directory and
4555 (setq ad-return-value (mapcar #'file-name-as-directory ad-do-it)))
4557 (defun mdw-repolist-column-unpulled-from-upstream (_id)
4558 "Insert number of upstream commits not in the current branch."
4559 (let ((upstream (magit-get-upstream-branch (magit-get-current-branch) t)))
4561 (let ((n (cadr (magit-rev-diff-count "HEAD" upstream))))
4562 (propertize (number-to-string n) 'face
4563 (if (> n 0) 'bold 'shadow))))))
4565 (defun mdw-repolist-column-unpushed-to-upstream (_id)
4566 "Insert number of commits in the current branch but not its upstream."
4567 (let ((upstream (magit-get-upstream-branch (magit-get-current-branch) t)))
4569 (let ((n (car (magit-rev-diff-count "HEAD" upstream))))
4570 (propertize (number-to-string n) 'face
4571 (if (> n 0) 'bold 'shadow))))))
4573 (defun mdw-try-smerge ()
4575 (goto-char (point-min))
4576 (when (re-search-forward "^<<<<<<< " nil t)
4578 (add-hook 'find-file-hook 'mdw-try-smerge t)
4580 ;;;--------------------------------------------------------------------------
4581 ;;; GUD, and especially GDB.
4583 ;; Inhibit window dedication. I mean, seriously, wtf?
4584 (defadvice gdb-display-buffer (after mdw-undedicated (buf) compile activate)
4585 "Don't make windows dedicated. Seriously."
4586 (set-window-dedicated-p ad-return-value nil))
4587 (defadvice gdb-set-window-buffer
4588 (after mdw-undedicated (name &optional ignore-dedicated window)
4590 "Don't make windows dedicated. Seriously."
4591 (set-window-dedicated-p (or window (selected-window)) nil))
4593 ;;;--------------------------------------------------------------------------
4596 ;; Turn off `noip' when running `man': it interferes with `man-db''s own
4597 ;; seccomp(2)-based sandboxing, which is (in this case, at least) strictly
4599 (defadvice Man-getpage-in-background
4600 (around mdw-inhibit-noip (topic) compile activate)
4601 "Inhibit the `noip' preload hack when invoking `man'."
4602 (let* ((old-preload (getenv "LD_PRELOAD"))
4603 (preloads (and old-preload
4604 (save-match-data (split-string old-preload ":"))))
4609 (let ((item (pop preloads)))
4610 (if (string-match "\\(/\\|^\\)noip\.so\\(:\\|$\\)" item)
4612 (push item filtered)))))
4616 (setenv "LD_PRELOAD"
4618 (with-output-to-string
4619 (setq filtered (nreverse filtered))
4622 (if first (setq first nil)
4624 (write-string (pop filtered)))))))
4626 (setenv "LD_PRELOAD" old-preload))
4629 ;;;--------------------------------------------------------------------------
4630 ;;; MPC configuration.
4632 (eval-when-compile (trap (require 'mpc)))
4634 (setq mpc-browser-tags '(Artist|Composer|Performer Album|Playlist))
4636 (defun mdw-mpc-now-playing ()
4640 (set-buffer (mpc-proc-cmd (mpc-proc-cmd-list '("status" "currentsong"))))
4641 (mpc--status-callback))
4642 (let ((state (cdr (assq 'state mpc-status))))
4643 (cond ((member state '("stop"))
4644 (message "mpd stopped."))
4645 ((member state '("play" "pause"))
4646 (let* ((artist (cdr (assq 'Artist mpc-status)))
4647 (album (cdr (assq 'Album mpc-status)))
4648 (title (cdr (assq 'Title mpc-status)))
4649 (file (cdr (assq 'file mpc-status)))
4650 (duration-string (cdr (assq 'Time mpc-status)))
4651 (time-string (cdr (assq 'time mpc-status)))
4652 (time (and time-string
4654 (if (string-match ":" time-string)
4655 (substring time-string
4656 0 (match-beginning 0))
4658 (duration (and duration-string
4659 (string-to-number duration-string)))
4660 (pos (and time duration
4661 (format " [%d:%02d/%d:%02d]"
4662 (/ time 60) (mod time 60)
4663 (/ duration 60) (mod duration 60))))
4664 (fmt (cond ((and artist title)
4665 (format "`%s' by %s%s" title artist
4666 (if album (format ", from `%s'" album)
4669 (format "`%s' (no tags)" file))
4671 "(no idea what's playing!)"))))
4672 (if (string= state "play")
4673 (message "mpd playing %s%s" fmt (or pos ""))
4674 (message "mpd paused in %s%s" fmt (or pos "")))))
4676 (message "mpd in unknown state `%s'" state)))))
4678 (defmacro mdw-define-mpc-wrapper (func bvl interactive &rest body)
4680 (interactive ,@interactive)
4683 (mdw-mpc-now-playing)))
4685 (mdw-define-mpc-wrapper mdw-mpc-play-or-pause () nil
4686 (if (member (cdr (assq 'state (mpc-cmd-status))) '("play"))
4690 (mdw-define-mpc-wrapper mdw-mpc-next () nil (mpc-next))
4691 (mdw-define-mpc-wrapper mdw-mpc-prev () nil (mpc-prev))
4692 (mdw-define-mpc-wrapper mdw-mpc-stop () nil (mpc-stop))
4694 (defun mdw-mpc-louder (step)
4695 (interactive (list (if current-prefix-arg
4696 (prefix-numeric-value current-prefix-arg)
4698 (mpc-proc-cmd (format "volume %+d" step)))
4700 (defun mdw-mpc-quieter (step)
4701 (interactive (list (if current-prefix-arg
4702 (prefix-numeric-value current-prefix-arg)
4704 (mpc-proc-cmd (format "volume %+d" (- step))))
4706 (defun mdw-mpc-hack-lines (arg interactivep func)
4707 (if (and interactivep (use-region-p))
4708 (let ((from (region-beginning)) (to (region-end)))
4713 (while (< (point) to)
4716 (let ((n (prefix-numeric-value arg)))
4733 (defun mdw-mpc-select-one ()
4734 (when (and (get-char-property (point) 'mpc-file)
4735 (not (get-char-property (point) 'mpc-select)))
4736 (mpc-select-toggle)))
4738 (defun mdw-mpc-unselect-one ()
4739 (when (get-char-property (point) 'mpc-select)
4740 (mpc-select-toggle)))
4742 (defun mdw-mpc-select (&optional arg interactivep)
4743 (interactive (list current-prefix-arg t))
4744 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
4746 (defun mdw-mpc-unselect (&optional arg interactivep)
4747 (interactive (list current-prefix-arg t))
4748 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-unselect-one))
4750 (defun mdw-mpc-unselect-backwards (arg)
4752 (mdw-mpc-hack-lines (- arg) t 'mdw-mpc-unselect-one))
4754 (defun mdw-mpc-unselect-all ()
4756 (setq mpc-select nil)
4757 (mpc-selection-refresh))
4759 (defun mdw-mpc-next-line (arg)
4764 (defun mdw-mpc-previous-line (arg)
4767 (forward-line (- arg)))
4769 (defun mdw-mpc-playlist-add (&optional arg interactivep)
4770 (interactive (list current-prefix-arg t))
4771 (let ((mpc-select mpc-select))
4772 (when (or arg (and interactivep (use-region-p)))
4773 (setq mpc-select nil)
4774 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
4775 (setq mpc-select (reverse mpc-select))
4776 (mpc-playlist-add)))
4778 (defun mdw-mpc-playlist-delete (&optional arg interactivep)
4779 (interactive (list current-prefix-arg t))
4780 (setq mpc-select (nreverse mpc-select))
4782 (when (or arg (and interactivep (use-region-p)))
4783 (setq mpc-select nil)
4784 (mpc-selection-refresh)
4785 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
4786 (mpc-playlist-delete)))
4788 (defun mdw-mpc-hack-tagbrowsers ()
4789 (setq-local mode-line-format
4791 mode-line-frame-identification
4792 mode-line-buffer-identification)))
4793 (add-hook 'mpc-tagbrowser-mode-hook 'mdw-mpc-hack-tagbrowsers)
4795 (defun mdw-mpc-hack-songs ()
4796 (setq-local header-line-format
4797 ;; '("MPC " mpc-volume " " mpc-current-song)
4798 (list (propertize " " 'display '(space :align-to 0))
4799 ;; 'mpc-songs-format-description
4801 (let ((deactivate-mark) (hscroll (window-hscroll)))
4803 (mpc-format mpc-songs-format 'self hscroll)
4804 ;; That would be simpler than the hscroll handling in
4805 ;; mpc-format, but currently move-to-column does not
4806 ;; recognize :space display properties.
4807 ;; (move-to-column hscroll)
4808 ;; (delete-region (point-min) (point))
4809 (buffer-string)))))))
4810 (add-hook 'mpc-songs-mode-hook 'mdw-mpc-hack-songs)
4812 (eval-after-load "mpc"
4814 (define-key mpc-mode-map "m" 'mdw-mpc-select)
4815 (define-key mpc-mode-map "u" 'mdw-mpc-unselect)
4816 (define-key mpc-mode-map "\177" 'mdw-mpc-unselect-backwards)
4817 (define-key mpc-mode-map "\e\177" 'mdw-mpc-unselect-all)
4818 (define-key mpc-mode-map "n" 'mdw-mpc-next-line)
4819 (define-key mpc-mode-map "p" 'mdw-mpc-previous-line)
4820 (define-key mpc-mode-map "/" 'mpc-songs-search)
4821 (setq mpc-songs-mode-map (make-sparse-keymap))
4822 (set-keymap-parent mpc-songs-mode-map mpc-mode-map)
4823 (define-key mpc-songs-mode-map "l" 'mpc-playlist)
4824 (define-key mpc-songs-mode-map "+" 'mdw-mpc-playlist-add)
4825 (define-key mpc-songs-mode-map "-" 'mdw-mpc-playlist-delete)
4826 (define-key mpc-songs-mode-map "\r" 'mpc-songs-jump-to)))
4828 ;;;--------------------------------------------------------------------------
4829 ;;; Inferior Emacs Lisp.
4831 (setq comint-prompt-read-only t)
4833 (eval-after-load "comint"
4835 (define-key comint-mode-map "\C-w" 'comint-kill-region)
4836 (define-key comint-mode-map [C-S-backspace] 'comint-kill-whole-line)))
4838 (eval-after-load "ielm"
4840 (define-key ielm-map "\C-w" 'comint-kill-region)
4841 (define-key ielm-map [C-S-backspace] 'comint-kill-whole-line)))
4843 ;;;----- That's all, folks --------------------------------------------------
4845 (provide 'dot-emacs)