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