el/dot-emacs.el: Configure the default CSS indentation.
[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 (defvar mdw-fast-startup nil
28 "Whether .emacs should optimize for rapid startup.
29 This may be at the expense of cool features.")
30 (let ((probe nil) (next command-line-args))
31 (while next
32 (cond ((string= (car next) "--mdw-fast-startup")
33 (setq mdw-fast-startup t)
34 (if probe
35 (rplacd probe (cdr next))
36 (setq command-line-args (cdr next))))
37 (t
38 (setq probe next)))
39 (setq next (cdr next))))
40
41 ;;;--------------------------------------------------------------------------
42 ;;; Some general utilities.
43
44 (eval-when-compile
45 (unless (fboundp 'make-regexp)
46 (load "make-regexp"))
47 (require 'cl))
48
49 (defmacro mdw-regexps (&rest list)
50 "Turn a LIST of strings into a single regular expression at compile-time."
51 (declare (indent nil)
52 (debug 0))
53 `',(make-regexp list))
54
55 (defun mdw-wrong ()
56 "This is not the key sequence you're looking for."
57 (interactive)
58 (error "wrong button"))
59
60 (defun mdw-emacs-version-p (major &optional minor)
61 "Return non-nil if the running Emacs is at least version MAJOR.MINOR."
62 (or (> emacs-major-version major)
63 (and (= emacs-major-version major)
64 (>= emacs-minor-version (or minor 0)))))
65
66 ;; Some error trapping.
67 ;;
68 ;; If individual bits of this file go tits-up, we don't particularly want
69 ;; the whole lot to stop right there and then, because it's bloody annoying.
70
71 (defmacro trap (&rest forms)
72 "Execute FORMS without allowing errors to propagate outside."
73 (declare (indent 0)
74 (debug t))
75 `(condition-case err
76 ,(if (cdr forms) (cons 'progn forms) (car forms))
77 (error (message "Error (trapped): %s in %s"
78 (error-message-string err)
79 ',forms))))
80
81 ;; Configuration reading.
82
83 (defvar mdw-config nil)
84 (defun mdw-config (sym)
85 "Read the configuration variable named SYM."
86 (unless mdw-config
87 (setq mdw-config
88 (flet ((replace (what with)
89 (goto-char (point-min))
90 (while (re-search-forward what nil t)
91 (replace-match with t))))
92 (with-temp-buffer
93 (insert-file-contents "~/.mdw.conf")
94 (replace "^[ \t]*\\(#.*\\|\\)\n" "")
95 (replace (concat "^[ \t]*"
96 "\\([-a-zA-Z0-9_.]*\\)"
97 "[ \t]*=[ \t]*"
98 "\\(.*[^ \t\n]\\|\\)"
99 "[ \t]**\\(\n\\|$\\)")
100 "(\\1 . \"\\2\")\n")
101 (car (read-from-string
102 (concat "(" (buffer-string) ")")))))))
103 (cdr (assq sym mdw-config)))
104
105 ;; Width configuration.
106
107 (defvar mdw-column-width
108 (string-to-number (or (mdw-config 'emacs-width) "77"))
109 "Width of Emacs columns.")
110 (defvar mdw-text-width mdw-column-width
111 "Expected width of text within columns.")
112 (put 'mdw-text-width 'safe-local-variable 'integerp)
113
114 ;; Local variables hacking.
115
116 (defun run-local-vars-mode-hook ()
117 "Run a hook for the major-mode after local variables have been processed."
118 (run-hooks (intern (concat (symbol-name major-mode)
119 "-local-variables-hook"))))
120 (add-hook 'hack-local-variables-hook 'run-local-vars-mode-hook)
121
122 ;; Set up the load path convincingly.
123
124 (dolist (dir (append (and (boundp 'debian-emacs-flavor)
125 (list (concat "/usr/share/"
126 (symbol-name debian-emacs-flavor)
127 "/site-lisp")))))
128 (dolist (sub (directory-files dir t))
129 (when (and (file-accessible-directory-p sub)
130 (not (member sub load-path)))
131 (setq load-path (nconc load-path (list sub))))))
132
133 ;; Is an Emacs library available?
134
135 (defun library-exists-p (name)
136 "Return non-nil if NAME is an available library.
137 Return non-nil if NAME.el (or NAME.elc) somewhere on the Emacs
138 load path. The non-nil value is the filename we found for the
139 library."
140 (let ((path load-path) elt (foundp nil))
141 (while (and path (not foundp))
142 (setq elt (car path))
143 (setq path (cdr path))
144 (setq foundp (or (let ((file (concat elt "/" name ".elc")))
145 (and (file-exists-p file) file))
146 (let ((file (concat elt "/" name ".el")))
147 (and (file-exists-p file) file)))))
148 foundp))
149
150 (defun maybe-autoload (symbol file &optional docstring interactivep type)
151 "Set an autoload if the file actually exists."
152 (and (library-exists-p file)
153 (autoload symbol file docstring interactivep type)))
154
155 (defun mdw-kick-menu-bar (&optional frame)
156 "Regenerate FRAME's menu bar so it doesn't have empty menus."
157 (interactive)
158 (unless frame (setq frame (selected-frame)))
159 (let ((old (frame-parameter frame 'menu-bar-lines)))
160 (set-frame-parameter frame 'menu-bar-lines 0)
161 (set-frame-parameter frame 'menu-bar-lines old)))
162
163 ;; Splitting windows.
164
165 (unless (fboundp 'scroll-bar-columns)
166 (defun scroll-bar-columns (side)
167 (cond ((eq side 'left) 0)
168 (window-system 3)
169 (t 1))))
170 (unless (fboundp 'fringe-columns)
171 (defun fringe-columns (side)
172 (cond ((not window-system) 0)
173 ((eq side 'left) 1)
174 (t 2))))
175
176 (defun mdw-horizontal-window-overhead ()
177 "Computes the horizontal window overhead.
178 This is the number of columns used by fringes, scroll bars and other such
179 cruft."
180 (if (not window-system)
181 1
182 (let ((tot 0))
183 (dolist (what '(scroll-bar fringe))
184 (dolist (side '(left right))
185 (incf tot (funcall (intern (concat (symbol-name what) "-columns"))
186 side))))
187 tot)))
188
189 (defun mdw-split-window-horizontally (&optional width)
190 "Split a window horizontally.
191 Without a numeric argument, split the window approximately in
192 half. With a numeric argument WIDTH, allocate WIDTH columns to
193 the left-hand window (if positive) or -WIDTH columns to the
194 right-hand window (if negative). Space for scroll bars and
195 fringes is not taken out of the allowance for WIDTH, unlike
196 \\[split-window-horizontally]."
197 (interactive "P")
198 (split-window-horizontally
199 (cond ((null width) nil)
200 ((>= width 0) (+ width (mdw-horizontal-window-overhead)))
201 ((< width 0) width))))
202
203 (defun mdw-preferred-column-width ()
204 "Return the preferred column width."
205 (if (and window-system (mdw-emacs-version-p 22)) mdw-column-width
206 (1+ mdw-column-width)))
207
208 (defun mdw-divvy-window (&optional width)
209 "Split a wide window into appropriate widths."
210 (interactive "P")
211 (setq width (if width (prefix-numeric-value width)
212 (mdw-preferred-column-width)))
213 (let* ((win (selected-window))
214 (sb-width (mdw-horizontal-window-overhead))
215 (c (/ (+ (window-width) sb-width)
216 (+ width sb-width))))
217 (while (> c 1)
218 (setq c (1- c))
219 (split-window-horizontally (+ width sb-width))
220 (other-window 1))
221 (select-window win)))
222
223 (defun mdw-set-frame-width (columns &optional width)
224 (interactive "nColumns:
225 P")
226 (setq width (if width (prefix-numeric-value width)
227 (mdw-preferred-column-width)))
228 (let ((sb-width (mdw-horizontal-window-overhead)))
229 (set-frame-width (selected-frame)
230 (- (* columns (+ width sb-width))
231 sb-width))
232 (mdw-divvy-window width)))
233
234 ;; Don't raise windows unless I say so.
235
236 (defvar mdw-inhibit-raise-frame nil
237 "*Whether `raise-frame' should do nothing when the frame is mapped.")
238
239 (defadvice raise-frame
240 (around mdw-inhibit (&optional frame) activate compile)
241 "Don't actually do anything if `mdw-inhibit-raise-frame' is true, and the
242 frame is actually mapped on the screen."
243 (if mdw-inhibit-raise-frame
244 (make-frame-visible frame)
245 ad-do-it))
246
247 (defmacro mdw-advise-to-inhibit-raise-frame (function)
248 "Advise the FUNCTION not to raise frames, even if it wants to."
249 `(defadvice ,function
250 (around mdw-inhibit-raise (&rest hunoz) activate compile)
251 "Don't raise the window unless you have to."
252 (let ((mdw-inhibit-raise-frame t))
253 ad-do-it)))
254
255 (mdw-advise-to-inhibit-raise-frame select-frame-set-input-focus)
256 (mdw-advise-to-inhibit-raise-frame appt-disp-window)
257 (mdw-advise-to-inhibit-raise-frame mouse-select-window)
258
259 ;; Bug fix for markdown-mode, which breaks point positioning during
260 ;; `query-replace'.
261 (defadvice markdown-check-change-for-wiki-link
262 (around mdw-save-match activate compile)
263 "Save match data around the `markdown-mode' `after-change-functions' hook."
264 (save-match-data ad-do-it))
265
266 ;; Bug fix for `bbdb-canonicalize-address': on Emacs 24, `run-hook-with-args'
267 ;; always returns nil, with the result that all email addresses are lost.
268 ;; Replace the function entirely.
269 (defadvice bbdb-canonicalize-address
270 (around mdw-bug-fix activate compile)
271 "Don't use `run-hook-with-args', because that doesn't work."
272 (let ((net (ad-get-arg 0)))
273
274 ;; Make sure this is a proper hook list.
275 (if (functionp bbdb-canonicalize-net-hook)
276 (setq bbdb-canonicalize-net-hook (list bbdb-canonicalize-net-hook)))
277
278 ;; Iterate over the hooks until things converge.
279 (let ((donep nil))
280 (while (not donep)
281 (let (next (changep nil)
282 hook (hooks bbdb-canonicalize-net-hook))
283 (while hooks
284 (setq hook (pop hooks))
285 (setq next (funcall hook net))
286 (if (not (equal next net))
287 (setq changep t
288 net next)))
289 (setq donep (not changep)))))
290 (setq ad-return-value net)))
291
292 ;; Transient mark mode hacks.
293
294 (defadvice exchange-point-and-mark
295 (around mdw-highlight (&optional arg) activate compile)
296 "Maybe don't actually exchange point and mark.
297 If `transient-mark-mode' is on and the mark is inactive, then
298 just activate it. A non-trivial prefix argument will force the
299 usual behaviour. A trivial prefix argument (i.e., just C-u) will
300 activate the mark and temporarily enable `transient-mark-mode' if
301 it's currently off."
302 (cond ((or mark-active
303 (and (not transient-mark-mode) (not arg))
304 (and arg (or (not (consp arg))
305 (not (= (car arg) 4)))))
306 ad-do-it)
307 (t
308 (or transient-mark-mode (setq transient-mark-mode 'only))
309 (set-mark (mark t)))))
310
311 ;; Functions for sexp diary entries.
312
313 (defun mdw-not-org-mode (form)
314 "As FORM, but not in Org mode agenda."
315 (and (not mdw-diary-for-org-mode-p)
316 (eval form)))
317
318 (defun mdw-weekday (l)
319 "Return non-nil if `date' falls on one of the days of the week in L.
320 L is a list of day numbers (from 0 to 6 for Sunday through to
321 Saturday) or symbols `sunday', `monday', etc. (or a mixture). If
322 the date stored in `date' falls on a listed day, then the
323 function returns non-nil."
324 (let ((d (calendar-day-of-week date)))
325 (or (memq d l)
326 (memq (nth d '(sunday monday tuesday wednesday
327 thursday friday saturday)) l))))
328
329 (defun mdw-discordian-date (date)
330 "Return the Discordian calendar date corresponding to DATE.
331
332 The return value is (YOLD . st-tibs-day) or (YOLD SEASON DAYNUM DOW).
333
334 The original is by David Pearson. I modified it to produce date components
335 as output rather than a string."
336 (let* ((days ["Sweetmorn" "Boomtime" "Pungenday"
337 "Prickle-Prickle" "Setting Orange"])
338 (months ["Chaos" "Discord" "Confusion"
339 "Bureaucracy" "Aftermath"])
340 (day-count [0 31 59 90 120 151 181 212 243 273 304 334])
341 (year (- (extract-calendar-year date) 1900))
342 (month (1- (extract-calendar-month date)))
343 (day (1- (extract-calendar-day date)))
344 (julian (+ (aref day-count month) day))
345 (dyear (+ year 3066)))
346 (if (and (= month 1) (= day 28))
347 (cons dyear 'st-tibs-day)
348 (list dyear
349 (aref months (floor (/ julian 73)))
350 (1+ (mod julian 73))
351 (aref days (mod julian 5))))))
352
353 (defun mdw-diary-discordian-date ()
354 "Convert the date in `date' to a string giving the Discordian date."
355 (let* ((ddate (mdw-discordian-date date))
356 (tail (format "in the YOLD %d" (car ddate))))
357 (if (eq (cdr ddate) 'st-tibs-day)
358 (format "St Tib's Day %s" tail)
359 (let ((season (cadr ddate))
360 (daynum (caddr ddate))
361 (dayname (cadddr ddate)))
362 (format "%s, the %d%s day of %s %s"
363 dayname
364 daynum
365 (let ((ldig (mod daynum 10)))
366 (cond ((= ldig 1) "st")
367 ((= ldig 2) "nd")
368 ((= ldig 3) "rd")
369 (t "th")))
370 season
371 tail)))))
372
373 (defun mdw-todo (&optional when)
374 "Return non-nil today, or on WHEN, whichever is later."
375 (let ((w (calendar-absolute-from-gregorian (calendar-current-date)))
376 (d (calendar-absolute-from-gregorian date)))
377 (if when
378 (setq w (max w (calendar-absolute-from-gregorian
379 (cond
380 ((not european-calendar-style)
381 when)
382 ((> (car when) 100)
383 (list (nth 1 when)
384 (nth 2 when)
385 (nth 0 when)))
386 (t
387 (list (nth 1 when)
388 (nth 0 when)
389 (nth 2 when))))))))
390 (eq w d)))
391
392 (defvar mdw-diary-for-org-mode-p nil)
393
394 (defadvice org-agenda-list (around mdw-preserve-links activate)
395 (let ((mdw-diary-for-org-mode-p t))
396 ad-do-it))
397
398 (defadvice diary-add-to-list (before mdw-trim-leading-space compile activate)
399 "Trim leading space from the diary entry string."
400 (save-match-data
401 (let ((str (ad-get-arg 1))
402 (done nil) old)
403 (while (not done)
404 (setq old str)
405 (setq str (cond ((null str) nil)
406 ((string-match "\\(^\\|\n\\)[ \t]+" str)
407 (replace-match "\\1" nil nil str))
408 ((and mdw-diary-for-org-mode-p
409 (string-match (concat
410 "\\(^\\|\n\\)"
411 "\\(" diary-time-regexp
412 "\\(-" diary-time-regexp "\\)?"
413 "\\)"
414 "\\(\t[ \t]*\\| [ \t]+\\)")
415 str))
416 (replace-match "\\1\\2 " nil nil str))
417 ((and (not mdw-diary-for-org-mode-p)
418 (string-match "\\[\\[[^][]*]\\[\\([^][]*\\)]]"
419 str))
420 (replace-match "\\1" nil nil str))
421 (t str)))
422 (if (equal str old) (setq done t)))
423 (ad-set-arg 1 str))))
424
425 (defadvice org-bbdb-anniversaries (after mdw-fixup-list compile activate)
426 "Return a string rather than a list."
427 (with-temp-buffer
428 (let ((anyp nil))
429 (dolist (e (let ((ee ad-return-value))
430 (if (atom ee) (list ee) ee)))
431 (when e
432 (when anyp (insert ?\n))
433 (insert e)
434 (setq anyp t)))
435 (setq ad-return-value
436 (and anyp (buffer-string))))))
437
438 ;; Fighting with Org-mode's evil key maps.
439
440 (defvar mdw-evil-keymap-keys
441 '(([S-up] . [?\C-c up])
442 ([S-down] . [?\C-c down])
443 ([S-left] . [?\C-c left])
444 ([S-right] . [?\C-c right])
445 (([M-up] [?\e up]) . [C-up])
446 (([M-down] [?\e down]) . [C-down])
447 (([M-left] [?\e left]) . [C-left])
448 (([M-right] [?\e right]) . [C-right]))
449 "Defines evil keybindings to clobber in `mdw-clobber-evil-keymap'.
450 The value is an alist mapping evil keys (as a list, or singleton)
451 to good keys (in the same form).")
452
453 (defun mdw-clobber-evil-keymap (keymap)
454 "Replace evil key bindings in the KEYMAP.
455 Evil key bindings are defined in `mdw-evil-keymap-keys'."
456 (dolist (entry mdw-evil-keymap-keys)
457 (let ((binding nil)
458 (keys (if (listp (car entry))
459 (car entry)
460 (list (car entry))))
461 (replacements (if (listp (cdr entry))
462 (cdr entry)
463 (list (cdr entry)))))
464 (catch 'found
465 (dolist (key keys)
466 (setq binding (lookup-key keymap key))
467 (when binding
468 (throw 'found nil))))
469 (when binding
470 (dolist (key keys)
471 (define-key keymap key nil))
472 (dolist (key replacements)
473 (define-key keymap key binding))))))
474
475 (defvar mdw-org-latex-defs
476 '(("strayman"
477 "\\documentclass{strayman}
478 \\usepackage[utf8]{inputenc}
479 \\usepackage[palatino, helvetica, courier, maths=cmr]{mdwfonts}
480 \\usepackage{graphicx, tikz, mdwtab, mdwmath, crypto, longtable}"
481 ("\\section{%s}" . "\\section*{%s}")
482 ("\\subsection{%s}" . "\\subsection*{%s}")
483 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
484 ("\\paragraph{%s}" . "\\paragraph*{%s}")
485 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))))
486
487 (eval-after-load "org-latex"
488 '(setq org-export-latex-classes
489 (append mdw-org-latex-defs org-export-latex-classes)))
490
491 (eval-after-load "ox-latex"
492 '(setq org-latex-classes (append mdw-org-latex-defs org-latex-classes)
493 org-latex-default-packages-alist '(("AUTO" "inputenc" t)
494 ("T1" "fontenc" t)
495 ("" "fixltx2e" nil)
496 ("" "graphicx" t)
497 ("" "longtable" nil)
498 ("" "float" nil)
499 ("" "wrapfig" nil)
500 ("" "rotating" nil)
501 ("normalem" "ulem" t)
502 ("" "textcomp" t)
503 ("" "marvosym" t)
504 ("" "wasysym" t)
505 ("" "amssymb" t)
506 ("" "hyperref" nil)
507 "\\tolerance=1000")))
508
509
510 (setq org-export-docbook-xslt-proc-command "xsltproc --output %o %s %i"
511 org-export-docbook-xsl-fo-proc-command "fop %i.safe %o"
512 org-export-docbook-xslt-stylesheet
513 "/usr/share/xml/docbook/stylesheet/docbook-xsl/fo/docbook.xsl")
514
515 ;; Glasses.
516
517 (setq glasses-separator "-"
518 glasses-separate-parentheses-p nil
519 glasses-uncapitalize-p t)
520
521 ;; Some hacks to do with window placement.
522
523 (defun mdw-clobber-other-windows-showing-buffer (buffer-or-name)
524 "Arrange that no windows on other frames are showing BUFFER-OR-NAME."
525 (interactive "bBuffer: ")
526 (let ((home-frame (selected-frame))
527 (buffer (get-buffer buffer-or-name))
528 (safe-buffer (get-buffer "*scratch*")))
529 (mapc (lambda (frame)
530 (or (eq frame home-frame)
531 (mapc (lambda (window)
532 (and (eq (window-buffer window) buffer)
533 (set-window-buffer window safe-buffer)))
534 (window-list frame))))
535 (frame-list))))
536
537 (defvar mdw-inhibit-walk-windows nil
538 "If non-nil, then `walk-windows' does nothing.
539 This is used by advice on `switch-to-buffer-other-frame' to inhibit finding
540 buffers in random frames.")
541
542 (defadvice walk-windows (around mdw-inhibit activate)
543 "If `mdw-inhibit-walk-windows' is non-nil, then do nothing."
544 (and (not mdw-inhibit-walk-windows)
545 ad-do-it))
546
547 (defadvice switch-to-buffer-other-frame
548 (around mdw-always-new-frame activate)
549 "Always make a new frame.
550 Even if an existing window in some random frame looks tempting."
551 (let ((mdw-inhibit-walk-windows t)) ad-do-it))
552
553 (defadvice display-buffer (before mdw-inhibit-other-frames activate)
554 "Don't try to do anything fancy with other frames.
555 Pretend they don't exist. They might be on other display devices."
556 (ad-set-arg 2 nil))
557
558 ;;;--------------------------------------------------------------------------
559 ;;; Improved compilation machinery.
560
561 ;; Uprated version of M-x compile.
562
563 (setq compile-command
564 (let ((ncpu (with-temp-buffer
565 (insert-file-contents "/proc/cpuinfo")
566 (buffer-string)
567 (count-matches "^processor\\s-*:"))))
568 (format "make -j%d -k" (* 2 ncpu))))
569
570 (defun mdw-compilation-buffer-name (mode)
571 (concat "*" (downcase mode) ": "
572 (abbreviate-file-name default-directory) "*"))
573 (setq compilation-buffer-name-function 'mdw-compilation-buffer-name)
574
575 (eval-after-load "compile"
576 '(progn
577 (define-key compilation-shell-minor-mode-map "\C-c\M-g" 'recompile)))
578
579 (defadvice compile (around hack-environment compile activate)
580 "Hack the environment inherited by inferiors in the compilation."
581 (let ((process-environment process-environment))
582 (setenv "LD_PRELOAD" nil)
583 ad-do-it))
584
585 (defun mdw-compile (command &optional directory comint)
586 "Initiate a compilation COMMAND, maybe in a different DIRECTORY.
587 The DIRECTORY may be nil to not change. If COMINT is t, then
588 start an interactive compilation.
589
590 Interactively, prompt for the command if the variable
591 `compilation-read-command' is non-nil, or if requested through
592 the prefix argument. Prompt for the directory, and run
593 interactively, if requested through the prefix.
594
595 Use a prefix of 4, 6, 12, or 14, or type C-u between one and three times, to
596 force prompting for a directory.
597
598 Use a prefix of 2, 6, 10, or 14, or type C-u three times, to force
599 prompting for the command.
600
601 Use a prefix of 8, 10, 12, or 14, or type C-u twice or three times,
602 to force interactive compilation."
603 (interactive
604 (let* ((prefix (prefix-numeric-value current-prefix-arg))
605 (command (eval compile-command))
606 (dir (and (plusp (logand prefix #x54))
607 (read-directory-name "Compile in directory: "))))
608 (list (if (or compilation-read-command
609 (plusp (logand prefix #x42)))
610 (compilation-read-command command)
611 command)
612 dir
613 (plusp (logand prefix #x58)))))
614 (let ((default-directory (or directory default-directory)))
615 (compile command comint)))
616
617 ;; Flymake support.
618
619 (defun mdw-find-build-dir (build-file)
620 (catch 'found
621 (let* ((src-dir (file-name-as-directory (expand-file-name ".")))
622 (dir src-dir))
623 (loop
624 (when (file-exists-p (concat dir build-file))
625 (throw 'found dir))
626 (let ((sub (expand-file-name (file-relative-name src-dir dir)
627 (concat dir "build/"))))
628 (catch 'give-up
629 (loop
630 (when (file-exists-p (concat sub build-file))
631 (throw 'found sub))
632 (when (string= sub dir) (throw 'give-up nil))
633 (setq sub (file-name-directory (directory-file-name sub))))))
634 (when (string= dir
635 (setq dir (file-name-directory
636 (directory-file-name dir))))
637 (throw 'found nil))))))
638
639 (defun mdw-flymake-make-init ()
640 (let ((build-dir (mdw-find-build-dir "Makefile")))
641 (and build-dir
642 (let ((tmp-src (flymake-init-create-temp-buffer-copy
643 #'flymake-create-temp-inplace)))
644 (flymake-get-syntax-check-program-args
645 tmp-src build-dir t t
646 #'flymake-get-make-cmdline)))))
647
648 (setq flymake-allowed-file-name-masks
649 '(("\\.\\(?:[cC]\\|cc\\|cpp\\|cxx\\|c\\+\\+\\)\\'"
650 mdw-flymake-make-init)
651 ("\\.\\(?:[hH]\\|hh\\|hpp\\|hxx\\|h\\+\\+\\)\\'"
652 mdw-flymake-master-make-init)
653 ("\\.p[lm]" flymake-perl-init)))
654
655 (setq flymake-mode-map
656 (let ((map (if (boundp 'flymake-mode-map)
657 flymake-mode-map
658 (make-sparse-keymap))))
659 (define-key map [?\C-c ?\C-f ?\C-p] 'flymake-goto-prev-error)
660 (define-key map [?\C-c ?\C-f ?\C-n] 'flymake-goto-next-error)
661 (define-key map [?\C-c ?\C-f ?\C-c] 'flymake-compile)
662 (define-key map [?\C-c ?\C-f ?\C-k] 'flymake-stop-all-syntax-checks)
663 (define-key map [?\C-c ?\C-f ?\C-e] 'flymake-popup-current-error-menu)
664 map))
665
666 ;;;--------------------------------------------------------------------------
667 ;;; Mail and news hacking.
668
669 (define-derived-mode mdwmail-mode mail-mode "[mdw] mail"
670 "Major mode for editing news and mail messages from external programs.
671 Not much right now. Just support for doing MailCrypt stuff."
672 :syntax-table nil
673 :abbrev-table nil
674 (run-hooks 'mail-setup-hook))
675
676 (define-key mdwmail-mode-map [?\C-c ?\C-c] 'disabled-operation)
677
678 (add-hook 'mdwail-mode-hook
679 (lambda ()
680 (set-buffer-file-coding-system 'utf-8)
681 (make-local-variable 'paragraph-separate)
682 (make-local-variable 'paragraph-start)
683 (setq paragraph-start
684 (concat "[ \t]*[-_][-_][-_]+$\\|^-- \\|-----\\|"
685 paragraph-start))
686 (setq paragraph-separate
687 (concat "[ \t]*[-_][-_][-_]+$\\|^-- \\|-----\\|"
688 paragraph-separate))))
689
690 ;; How to encrypt in mdwmail.
691
692 (defun mdwmail-mc-encrypt (&optional recip scm start end from sign)
693 (or start
694 (setq start (save-excursion
695 (goto-char (point-min))
696 (or (search-forward "\n\n" nil t) (point-min)))))
697 (or end
698 (setq end (point-max)))
699 (mc-encrypt-generic recip scm start end from sign))
700
701 ;; How to sign in mdwmail.
702
703 (defun mdwmail-mc-sign (key scm start end uclr)
704 (or start
705 (setq start (save-excursion
706 (goto-char (point-min))
707 (or (search-forward "\n\n" nil t) (point-min)))))
708 (or end
709 (setq end (point-max)))
710 (mc-sign-generic key scm start end uclr))
711
712 ;; Some signature mangling.
713
714 (defun mdwmail-mangle-signature ()
715 (save-excursion
716 (goto-char (point-min))
717 (perform-replace "\n-- \n" "\n-- " nil nil nil)))
718 (add-hook 'mail-setup-hook 'mdwmail-mangle-signature)
719 (add-hook 'message-setup-hook 'mdwmail-mangle-signature)
720
721 ;; Insert my login name into message-ids, so I can score replies.
722
723 (defadvice message-unique-id (after mdw-user-name last activate compile)
724 "Ensure that the user's name appears at the end of the message-id string,
725 so that it can be used for convenient filtering."
726 (setq ad-return-value (concat ad-return-value "." (user-login-name))))
727
728 ;; Tell my movemail hack where movemail is.
729 ;;
730 ;; This is needed to shup up warnings about LD_PRELOAD.
731
732 (let ((path exec-path))
733 (while path
734 (let ((try (expand-file-name "movemail" (car path))))
735 (if (file-executable-p try)
736 (setenv "REAL_MOVEMAIL" try))
737 (setq path (cdr path)))))
738
739 ;; AUTHINFO GENERIC kludge.
740
741 (defvar nntp-authinfo-generic nil
742 "Set to the `NNTPAUTH' string to pass on to `authinfo-kludge'.
743
744 Use this to arrange for per-server settings.")
745
746 (defun nntp-open-authinfo-kludge (buffer)
747 "Open a connection to SERVER using `authinfo-kludge'."
748 (let ((proc (start-process "nntpd" buffer
749 "env" (concat "NNTPAUTH="
750 (or nntp-authinfo-generic
751 (getenv "NNTPAUTH")
752 (error "NNTPAUTH unset")))
753 "authinfo-kludge" nntp-address)))
754 (set-buffer buffer)
755 (nntp-wait-for-string "^\r*200")
756 (beginning-of-line)
757 (delete-region (point-min) (point))
758 proc))
759
760 (eval-after-load "erc"
761 '(load "~/.ercrc.el"))
762
763 ;; Heavy-duty Gnus patching.
764
765 (defun mdw-nnimap-transform-headers ()
766 (goto-char (point-min))
767 (let (article lines size string)
768 (block nil
769 (while (not (eobp))
770 (while (not (looking-at "\\* [0-9]+ FETCH"))
771 (delete-region (point) (progn (forward-line 1) (point)))
772 (when (eobp)
773 (return)))
774 (goto-char (match-end 0))
775 ;; Unfold quoted {number} strings.
776 (while (re-search-forward
777 "[^]][ (]{\\([0-9]+\\)}\r?\n"
778 (save-excursion
779 ;; Start of the header section.
780 (or (re-search-forward "] {[0-9]+}\r?\n" nil t)
781 ;; Start of the next FETCH.
782 (re-search-forward "\\* [0-9]+ FETCH" nil t)
783 (point-max)))
784 t)
785 (setq size (string-to-number (match-string 1)))
786 (delete-region (+ (match-beginning 0) 2) (point))
787 (setq string (buffer-substring (point) (+ (point) size)))
788 (delete-region (point) (+ (point) size))
789 (insert (format "%S" (mm-subst-char-in-string ?\n ?\s string)))
790 ;; [mdw] missing from upstream
791 (backward-char 1))
792 (beginning-of-line)
793 (setq article
794 (and (re-search-forward "UID \\([0-9]+\\)" (line-end-position)
795 t)
796 (match-string 1)))
797 (setq lines nil)
798 (setq size
799 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
800 (line-end-position)
801 t)
802 (match-string 1)))
803 (beginning-of-line)
804 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
805 (let ((structure (ignore-errors
806 (read (current-buffer)))))
807 (while (and (consp structure)
808 (not (atom (car structure))))
809 (setq structure (car structure)))
810 (setq lines (if (and
811 (stringp (car structure))
812 (equal (upcase (nth 0 structure)) "MESSAGE")
813 (equal (upcase (nth 1 structure)) "RFC822"))
814 (nth 9 structure)
815 (nth 7 structure)))))
816 (delete-region (line-beginning-position) (line-end-position))
817 (insert (format "211 %s Article retrieved." article))
818 (forward-line 1)
819 (when size
820 (insert (format "Chars: %s\n" size)))
821 (when lines
822 (insert (format "Lines: %s\n" lines)))
823 ;; Most servers have a blank line after the headers, but
824 ;; Davmail doesn't.
825 (unless (re-search-forward "^\r$\\|^)\r?$" nil t)
826 (goto-char (point-max)))
827 (delete-region (line-beginning-position) (line-end-position))
828 (insert ".")
829 (forward-line 1)))))
830
831 (eval-after-load 'nnimap
832 '(defalias 'nnimap-transform-headers
833 (symbol-function 'mdw-nnimap-transform-headers)))
834
835 ;;;--------------------------------------------------------------------------
836 ;;; Utility functions.
837
838 (or (fboundp 'line-number-at-pos)
839 (defun line-number-at-pos (&optional pos)
840 (let ((opoint (or pos (point))) start)
841 (save-excursion
842 (save-restriction
843 (goto-char (point-min))
844 (widen)
845 (forward-line 0)
846 (setq start (point))
847 (goto-char opoint)
848 (forward-line 0)
849 (1+ (count-lines 1 (point))))))))
850
851 (defun mdw-uniquify-alist (&rest alists)
852 "Return the concatenation of the ALISTS with duplicate elements removed.
853 The first association with a given key prevails; others are
854 ignored. The input lists are not modified, although they'll
855 probably become garbage."
856 (and alists
857 (let ((start-list (cons nil nil)))
858 (mdw-do-uniquify start-list
859 start-list
860 (car alists)
861 (cdr alists)))))
862
863 (defun mdw-do-uniquify (done end l rest)
864 "A helper function for mdw-uniquify-alist.
865 The DONE argument is a list whose first element is `nil'. It
866 contains the uniquified alist built so far. The leading `nil' is
867 stripped off at the end of the operation; it's only there so that
868 DONE always references a cons cell. END refers to the final cons
869 cell in the DONE list; it is modified in place each time to avoid
870 the overheads of `append'ing all the time. The L argument is the
871 alist we're currently processing; the remaining alists are given
872 in REST."
873
874 ;; There are several different cases to deal with here.
875 (cond
876
877 ;; Current list isn't empty. Add the first item to the DONE list if
878 ;; there's not an item with the same KEY already there.
879 (l (or (assoc (car (car l)) done)
880 (progn
881 (setcdr end (cons (car l) nil))
882 (setq end (cdr end))))
883 (mdw-do-uniquify done end (cdr l) rest))
884
885 ;; The list we were working on is empty. Shunt the next list into the
886 ;; current list position and go round again.
887 (rest (mdw-do-uniquify done end (car rest) (cdr rest)))
888
889 ;; Everything's done. Remove the leading `nil' from the DONE list and
890 ;; return it. Finished!
891 (t (cdr done))))
892
893 (defun date ()
894 "Insert the current date in a pleasing way."
895 (interactive)
896 (insert (save-excursion
897 (let ((buffer (get-buffer-create "*tmp*")))
898 (unwind-protect (progn (set-buffer buffer)
899 (erase-buffer)
900 (shell-command "date +%Y-%m-%d" t)
901 (goto-char (mark))
902 (delete-backward-char 1)
903 (buffer-string))
904 (kill-buffer buffer))))))
905
906 (defun uuencode (file &optional name)
907 "UUencodes a file, maybe calling it NAME, into the current buffer."
908 (interactive "fInput file name: ")
909
910 ;; If NAME isn't specified, then guess from the filename.
911 (if (not name)
912 (setq name
913 (substring file
914 (or (string-match "[^/]*$" file) 0))))
915 (print (format "uuencode `%s' `%s'" file name))
916
917 ;; Now actually do the thing.
918 (call-process "uuencode" file t nil name))
919
920 (defvar np-file "~/.np"
921 "*Where the `now-playing' file is.")
922
923 (defun np (&optional arg)
924 "Grabs a `now-playing' string."
925 (interactive)
926 (save-excursion
927 (or arg (progn
928 (goto-char (point-max))
929 (insert "\nNP: ")
930 (insert-file-contents np-file)))))
931
932 (defun mdw-version-< (ver-a ver-b)
933 "Answer whether VER-A is strictly earlier than VER-B.
934 VER-A and VER-B are version numbers, which are strings containing digit
935 sequences separated by `.'."
936 (let* ((la (mapcar (lambda (x) (car (read-from-string x)))
937 (split-string ver-a "\\.")))
938 (lb (mapcar (lambda (x) (car (read-from-string x)))
939 (split-string ver-b "\\."))))
940 (catch 'done
941 (while t
942 (cond ((null la) (throw 'done lb))
943 ((null lb) (throw 'done nil))
944 ((< (car la) (car lb)) (throw 'done t))
945 ((= (car la) (car lb)) (setq la (cdr la) lb (cdr lb)))
946 (t (throw 'done nil)))))))
947
948 (defun mdw-check-autorevert ()
949 "Sets global-auto-revert-ignore-buffer appropriately for this buffer.
950 This takes into consideration whether it's been found using
951 tramp, which seems to get itself into a twist."
952 (cond ((not (boundp 'global-auto-revert-ignore-buffer))
953 nil)
954 ((and (buffer-file-name)
955 (fboundp 'tramp-tramp-file-p)
956 (tramp-tramp-file-p (buffer-file-name)))
957 (unless global-auto-revert-ignore-buffer
958 (setq global-auto-revert-ignore-buffer 'tramp)))
959 ((eq global-auto-revert-ignore-buffer 'tramp)
960 (setq global-auto-revert-ignore-buffer nil))))
961
962 (defadvice find-file (after mdw-autorevert activate)
963 (mdw-check-autorevert))
964 (defadvice write-file (after mdw-autorevert activate)
965 (mdw-check-autorevert))
966
967 (defun mdw-auto-revert ()
968 "Recheck all of the autorevertable buffers, and update VC modelines."
969 (interactive)
970 (let ((auto-revert-check-vc-info t))
971 (auto-revert-buffers)))
972
973 ;;;--------------------------------------------------------------------------
974 ;;; Dired hacking.
975
976 (defadvice dired-maybe-insert-subdir
977 (around mdw-marked-insertion first activate)
978 "The DIRNAME may be a list of directory names to insert.
979 Interactively, if files are marked, then insert all of them.
980 With a numeric prefix argument, select that many entries near
981 point; with a non-numeric prefix argument, prompt for listing
982 options."
983 (interactive
984 (list (dired-get-marked-files nil
985 (and (integerp current-prefix-arg)
986 current-prefix-arg)
987 #'file-directory-p)
988 (and current-prefix-arg
989 (not (integerp current-prefix-arg))
990 (read-string "Switches for listing: "
991 (or dired-subdir-switches
992 dired-actual-switches)))))
993 (let ((dirs (ad-get-arg 0)))
994 (dolist (dir (if (listp dirs) dirs (list dirs)))
995 (ad-set-arg 0 dir)
996 ad-do-it)))
997
998 (defun mdw-dired-run (args &optional syncp)
999 (interactive (let ((file (dired-get-filename t)))
1000 (list (read-string (format "Arguments for %s: " file))
1001 current-prefix-arg)))
1002 (funcall (if syncp 'shell-command 'async-shell-command)
1003 (concat (shell-quote-argument (dired-get-filename nil))
1004 " " args)))
1005
1006 (defadvice dired-do-flagged-delete
1007 (around mdw-delete-if-prefix-argument activate compile)
1008 (let ((delete-by-moving-to-trash (and (null current-prefix-arg)
1009 delete-by-moving-to-trash)))
1010 ad-do-it))
1011
1012 (eval-after-load "dired"
1013 '(define-key dired-mode-map "X" 'mdw-dired-run))
1014
1015 ;;;--------------------------------------------------------------------------
1016 ;;; URL viewing.
1017
1018 (defun mdw-w3m-browse-url (url &optional new-session-p)
1019 "Invoke w3m on the URL in its current window, or at least a different one.
1020 If NEW-SESSION-P, start a new session."
1021 (interactive "sURL: \nP")
1022 (save-excursion
1023 (let ((window (selected-window)))
1024 (unwind-protect
1025 (progn
1026 (select-window (or (and (not new-session-p)
1027 (get-buffer-window "*w3m*"))
1028 (progn
1029 (if (one-window-p t) (split-window))
1030 (get-lru-window))))
1031 (w3m-browse-url url new-session-p))
1032 (select-window window)))))
1033
1034 (eval-after-load 'w3m
1035 '(define-key w3m-mode-map [?\e ?\r] 'w3m-view-this-url-new-session))
1036
1037 (defvar mdw-good-url-browsers
1038 '(browse-url-chromium
1039 browse-url-mozilla
1040 browse-url-generic
1041 (w3m . mdw-w3m-browse-url)
1042 browse-url-w3)
1043 "List of good browsers for mdw-good-url-browsers.
1044 Each item is a browser function name, or a cons (CHECK . FUNC).
1045 A symbol FOO stands for (FOO . FOO).")
1046
1047 (defun mdw-good-url-browser ()
1048 "Return a good URL browser.
1049 Trundle the list of such things, finding the first item for which
1050 CHECK is fboundp, and returning the correponding FUNC."
1051 (let ((bs mdw-good-url-browsers) b check func answer)
1052 (while (and bs (not answer))
1053 (setq b (car bs)
1054 bs (cdr bs))
1055 (if (consp b)
1056 (setq check (car b) func (cdr b))
1057 (setq check b func b))
1058 (if (fboundp check)
1059 (setq answer func)))
1060 answer))
1061
1062 (eval-after-load "w3m-search"
1063 '(progn
1064 (dolist
1065 (item
1066 '(("g" "Google" "http://www.google.co.uk/search?q=%s")
1067 ("gd" "Google Directory"
1068 "http://www.google.com/search?cat=gwd/Top&q=%s")
1069 ("gg" "Google Groups" "http://groups.google.com/groups?q=%s")
1070 ("ward" "Ward's wiki" "http://c2.com/cgi/wiki?%s")
1071 ("gi" "Images" "http://images.google.com/images?q=%s")
1072 ("rfc" "RFC"
1073 "http://metalzone.distorted.org.uk/ftp/pub/mirrors/rfc/rfc%s.txt.gz")
1074 ("wp" "Wikipedia"
1075 "http://en.wikipedia.org/wiki/Special:Search?go=Go&search=%s")
1076 ("imdb" "IMDb" "http://www.imdb.com/Find?%s")
1077 ("nc-wiki" "nCipher wiki"
1078 "http://wiki.ncipher.com/wiki/bin/view/Devel/?topic=%s")
1079 ("map" "Google maps" "http://maps.google.co.uk/maps?q=%s&hl=en")
1080 ("lp" "Launchpad bug by number"
1081 "https://bugs.launchpad.net/bugs/%s")
1082 ("lppkg" "Launchpad bugs by package"
1083 "https://bugs.launchpad.net/%s")
1084 ("msdn" "MSDN"
1085 "http://social.msdn.microsoft.com/Search/en-GB/?query=%s&ac=8")
1086 ("debbug" "Debian bug by number"
1087 "http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=%s")
1088 ("debbugpkg" "Debian bugs by package"
1089 "http://bugs.debian.org/cgi-bin/pkgreport.cgi?pkg=%s")
1090 ("ljlogin" "LJ login" "http://www.livejournal.com/login.bml")))
1091 (add-to-list 'w3m-search-engine-alist
1092 (list (cadr item) (caddr item) nil))
1093 (add-to-list 'w3m-uri-replace-alist
1094 (list (concat "\\`" (car item) ":")
1095 'w3m-search-uri-replace
1096 (cadr item))))))
1097
1098 ;;;--------------------------------------------------------------------------
1099 ;;; Paragraph filling.
1100
1101 ;; Useful variables.
1102
1103 (defvar mdw-fill-prefix nil
1104 "*Used by `mdw-line-prefix' and `mdw-fill-paragraph'.
1105 If there's no fill prefix currently set (by the `fill-prefix'
1106 variable) and there's a match from one of the regexps here, it
1107 gets used to set the fill-prefix for the current operation.
1108
1109 The variable is a list of items of the form `REGEXP . PREFIX'; if
1110 the REGEXP matches, the PREFIX is used to set the fill prefix.
1111 It in turn is a list of things:
1112
1113 STRING -- insert a literal string
1114 (match . N) -- insert the thing matched by bracketed subexpression N
1115 (pad . N) -- a string of whitespace the same width as subexpression N
1116 (expr . FORM) -- the result of evaluating FORM")
1117
1118 (make-variable-buffer-local 'mdw-fill-prefix)
1119
1120 (defvar mdw-hanging-indents
1121 (concat "\\(\\("
1122 "\\([*o+]\\|-[-#]?\\|[0-9]+\\.\\|\\[[0-9]+\\]\\|([a-zA-Z])\\)"
1123 "[ \t]+"
1124 "\\)?\\)")
1125 "*Standard regexp matching parts of a hanging indent.
1126 This is mainly useful in `auto-fill-mode'.")
1127
1128 ;; Utility functions.
1129
1130 (defun mdw-maybe-tabify (s)
1131 "Tabify or untabify the string S, according to `indent-tabs-mode'."
1132 (let ((tabfun (if indent-tabs-mode #'tabify #'untabify)))
1133 (with-temp-buffer
1134 (save-match-data
1135 (insert s "\n")
1136 (let ((start (point-min)) (end (point-max)))
1137 (funcall tabfun (point-min) (point-max))
1138 (setq s (buffer-substring (point-min) (1- (point-max)))))))))
1139
1140 (defun mdw-examine-fill-prefixes (l)
1141 "Given a list of dynamic fill prefixes, pick one which matches
1142 context and return the static fill prefix to use. Point must be
1143 at the start of a line, and match data must be saved."
1144 (cond ((not l) nil)
1145 ((looking-at (car (car l)))
1146 (mdw-maybe-tabify (apply #'concat
1147 (mapcar #'mdw-do-prefix-match
1148 (cdr (car l))))))
1149 (t (mdw-examine-fill-prefixes (cdr l)))))
1150
1151 (defun mdw-maybe-car (p)
1152 "If P is a pair, return (car P), otherwise just return P."
1153 (if (consp p) (car p) p))
1154
1155 (defun mdw-padding (s)
1156 "Return a string the same width as S but made entirely from whitespace."
1157 (let* ((l (length s)) (i 0) (n (make-string l ? )))
1158 (while (< i l)
1159 (if (= 9 (aref s i))
1160 (aset n i 9))
1161 (setq i (1+ i)))
1162 n))
1163
1164 (defun mdw-do-prefix-match (m)
1165 "Expand a dynamic prefix match element.
1166 See `mdw-fill-prefix' for details."
1167 (cond ((not (consp m)) (format "%s" m))
1168 ((eq (car m) 'match) (match-string (mdw-maybe-car (cdr m))))
1169 ((eq (car m) 'pad) (mdw-padding (match-string
1170 (mdw-maybe-car (cdr m)))))
1171 ((eq (car m) 'eval) (eval (cdr m)))
1172 (t "")))
1173
1174 (defun mdw-choose-dynamic-fill-prefix ()
1175 "Work out the dynamic fill prefix based on the variable `mdw-fill-prefix'."
1176 (cond ((and fill-prefix (not (string= fill-prefix ""))) fill-prefix)
1177 ((not mdw-fill-prefix) fill-prefix)
1178 (t (save-excursion
1179 (beginning-of-line)
1180 (save-match-data
1181 (mdw-examine-fill-prefixes mdw-fill-prefix))))))
1182
1183 (defadvice do-auto-fill (around mdw-dynamic-fill-prefix () activate compile)
1184 "Handle auto-filling, working out a dynamic fill prefix in the
1185 case where there isn't a sensible static one."
1186 (let ((fill-prefix (mdw-choose-dynamic-fill-prefix)))
1187 ad-do-it))
1188
1189 (defun mdw-fill-paragraph ()
1190 "Fill paragraph, getting a dynamic fill prefix."
1191 (interactive)
1192 (let ((fill-prefix (mdw-choose-dynamic-fill-prefix)))
1193 (fill-paragraph nil)))
1194
1195 (defun mdw-standard-fill-prefix (rx &optional mat)
1196 "Set the dynamic fill prefix, handling standard hanging indents and stuff.
1197 This is just a short-cut for setting the thing by hand, and by
1198 design it doesn't cope with anything approximating a complicated
1199 case."
1200 (setq mdw-fill-prefix
1201 `((,(concat rx mdw-hanging-indents)
1202 (match . 1)
1203 (pad . ,(or mat 2))))))
1204
1205 ;;;--------------------------------------------------------------------------
1206 ;;; Other common declarations.
1207
1208 ;; Common mode settings.
1209
1210 (defvar mdw-auto-indent t
1211 "Whether to indent automatically after a newline.")
1212
1213 (defun mdw-whitespace-mode (&optional arg)
1214 "Turn on/off whitespace mode, but don't highlight trailing space."
1215 (interactive "P")
1216 (when (and (boundp 'whitespace-style)
1217 (fboundp 'whitespace-mode))
1218 (let ((whitespace-style (remove 'trailing whitespace-style)))
1219 (whitespace-mode arg))
1220 (setq show-trailing-whitespace whitespace-mode)))
1221
1222 (defvar mdw-do-misc-mode-hacking nil)
1223
1224 (defun mdw-misc-mode-config ()
1225 (and mdw-auto-indent
1226 (cond ((eq major-mode 'lisp-mode)
1227 (local-set-key "\C-m" 'mdw-indent-newline-and-indent))
1228 ((derived-mode-p 'slime-repl-mode 'asm-mode 'comint-mode)
1229 nil)
1230 (t
1231 (local-set-key "\C-m" 'newline-and-indent))))
1232 (set (make-local-variable 'mdw-do-misc-mode-hacking) t)
1233 (local-set-key [C-return] 'newline)
1234 (make-local-variable 'page-delimiter)
1235 (setq page-delimiter "\f\\|^.*-\\{6\\}.*$")
1236 (setq comment-column 40)
1237 (auto-fill-mode 1)
1238 (setq fill-column mdw-text-width)
1239 (and (fboundp 'gtags-mode)
1240 (gtags-mode))
1241 (if (fboundp 'hs-minor-mode)
1242 (trap (hs-minor-mode t))
1243 (outline-minor-mode t))
1244 (reveal-mode t)
1245 (trap (turn-on-font-lock)))
1246
1247 (defun mdw-post-local-vars-misc-mode-config ()
1248 (setq whitespace-line-column mdw-text-width)
1249 (when (and mdw-do-misc-mode-hacking
1250 (not buffer-read-only))
1251 (setq show-trailing-whitespace t)
1252 (mdw-whitespace-mode 1)))
1253 (add-hook 'hack-local-variables-hook 'mdw-post-local-vars-misc-mode-config)
1254
1255 (defmacro mdw-advise-update-angry-fruit-salad (&rest funcs)
1256 `(progn ,@(mapcar (lambda (func)
1257 `(defadvice ,func
1258 (after mdw-angry-fruit-salad activate)
1259 (when mdw-do-misc-mode-hacking
1260 (setq show-trailing-whitespace
1261 (not buffer-read-only))
1262 (mdw-whitespace-mode (if buffer-read-only 0 1)))))
1263 funcs)))
1264 (mdw-advise-update-angry-fruit-salad toggle-read-only
1265 read-only-mode
1266 view-mode
1267 view-mode-enable
1268 view-mode-disable)
1269
1270 (eval-after-load 'gtags
1271 '(progn
1272 (dolist (key '([mouse-2] [mouse-3]))
1273 (define-key gtags-mode-map key nil))
1274 (define-key gtags-mode-map [C-S-mouse-2] 'gtags-find-tag-by-event)
1275 (define-key gtags-select-mode-map [C-S-mouse-2]
1276 'gtags-select-tag-by-event)
1277 (dolist (map (list gtags-mode-map gtags-select-mode-map))
1278 (define-key map [C-S-mouse-3] 'gtags-pop-stack))))
1279
1280 ;; Backup file handling.
1281
1282 (defvar mdw-backup-disable-regexps nil
1283 "*List of regular expressions: if a file name matches any of
1284 these then the file is not backed up.")
1285
1286 (defun mdw-backup-enable-predicate (name)
1287 "[mdw]'s default backup predicate.
1288 Allows a backup if the standard predicate would allow it, and it
1289 doesn't match any of the regular expressions in
1290 `mdw-backup-disable-regexps'."
1291 (and (normal-backup-enable-predicate name)
1292 (let ((answer t) (list mdw-backup-disable-regexps))
1293 (save-match-data
1294 (while list
1295 (if (string-match (car list) name)
1296 (setq answer nil))
1297 (setq list (cdr list)))
1298 answer))))
1299 (setq backup-enable-predicate 'mdw-backup-enable-predicate)
1300
1301 ;; Frame cleanup.
1302
1303 (defun mdw-last-one-out-turn-off-the-lights (frame)
1304 "Disconnect from an X display if this was the last frame on that display."
1305 (let ((frame-display (frame-parameter frame 'display)))
1306 (when (and frame-display
1307 (eq window-system 'x)
1308 (not (some (lambda (fr)
1309 (and (not (eq fr frame))
1310 (string= (frame-parameter fr 'display)
1311 frame-display)))
1312 (frame-list))))
1313 (run-with-idle-timer 0 nil #'x-close-connection frame-display))))
1314 (add-hook 'delete-frame-functions 'mdw-last-one-out-turn-off-the-lights)
1315
1316 ;;;--------------------------------------------------------------------------
1317 ;;; Fullscreen-ness.
1318
1319 (defvar mdw-full-screen-parameters
1320 '((menu-bar-lines . 0)
1321 ;(vertical-scroll-bars . nil)
1322 )
1323 "Frame parameters to set when making a frame fullscreen.")
1324
1325 (defvar mdw-full-screen-save
1326 '(width height)
1327 "Extra frame parameters to save when setting fullscreen.")
1328
1329 (defun mdw-toggle-full-screen (&optional frame)
1330 "Show the FRAME fullscreen."
1331 (interactive)
1332 (when window-system
1333 (cond ((frame-parameter frame 'fullscreen)
1334 (set-frame-parameter frame 'fullscreen nil)
1335 (modify-frame-parameters
1336 nil
1337 (or (frame-parameter frame 'mdw-full-screen-saved)
1338 (mapcar (lambda (assoc)
1339 (assq (car assoc) default-frame-alist))
1340 mdw-full-screen-parameters))))
1341 (t
1342 (let ((saved (mapcar (lambda (param)
1343 (cons param (frame-parameter frame param)))
1344 (append (mapcar #'car
1345 mdw-full-screen-parameters)
1346 mdw-full-screen-save))))
1347 (set-frame-parameter frame 'mdw-full-screen-saved saved))
1348 (modify-frame-parameters frame mdw-full-screen-parameters)
1349 (set-frame-parameter frame 'fullscreen 'fullboth)))))
1350
1351 ;;;--------------------------------------------------------------------------
1352 ;;; General fontification.
1353
1354 (make-face 'mdw-virgin-face)
1355
1356 (defmacro mdw-define-face (name &rest body)
1357 "Define a face, and make sure it's actually set as the definition."
1358 (declare (indent 1)
1359 (debug 0))
1360 `(progn
1361 (copy-face 'mdw-virgin-face ',name)
1362 (defvar ,name ',name)
1363 (put ',name 'face-defface-spec ',body)
1364 (face-spec-set ',name ',body nil)))
1365
1366 (mdw-define-face default
1367 (((type w32)) :family "courier new" :height 85)
1368 (((type x)) :family "6x13" :foundry "trad" :height 130)
1369 (((type color)) :foreground "white" :background "black")
1370 (t nil))
1371 (mdw-define-face fixed-pitch
1372 (((type w32)) :family "courier new" :height 85)
1373 (((type x)) :family "6x13" :foundry "trad" :height 130)
1374 (t :foreground "white" :background "black"))
1375 (if (mdw-emacs-version-p 23)
1376 (mdw-define-face variable-pitch
1377 (((type x)) :family "sans" :height 100))
1378 (mdw-define-face variable-pitch
1379 (((type x)) :family "helvetica" :height 90)))
1380 (mdw-define-face region
1381 (((min-colors 64)) :background "grey30")
1382 (((class color)) :background "blue")
1383 (t :inverse-video t))
1384 (mdw-define-face match
1385 (((class color)) :background "blue")
1386 (t :inverse-video t))
1387 (mdw-define-face mc/cursor-face
1388 (((class color)) :background "red")
1389 (t :inverse-video t))
1390 (mdw-define-face minibuffer-prompt
1391 (t :weight bold))
1392 (mdw-define-face mode-line
1393 (((class color)) :foreground "blue" :background "yellow"
1394 :box (:line-width 1 :style released-button))
1395 (t :inverse-video t))
1396 (mdw-define-face mode-line-inactive
1397 (((class color)) :foreground "yellow" :background "blue"
1398 :box (:line-width 1 :style released-button))
1399 (t :inverse-video t))
1400 (mdw-define-face nobreak-space
1401 (((type tty)))
1402 (t :inherit escape-glyph :underline t))
1403 (mdw-define-face scroll-bar
1404 (t :foreground "black" :background "lightgrey"))
1405 (mdw-define-face fringe
1406 (t :foreground "yellow"))
1407 (mdw-define-face show-paren-match
1408 (((min-colors 64)) :background "darkgreen")
1409 (((class color)) :background "green")
1410 (t :underline t))
1411 (mdw-define-face show-paren-mismatch
1412 (((class color)) :background "red")
1413 (t :inverse-video t))
1414 (mdw-define-face highlight
1415 (((min-colors 64)) :background "DarkSeaGreen4")
1416 (((class color)) :background "cyan")
1417 (t :inverse-video t))
1418
1419 (mdw-define-face holiday-face
1420 (t :background "red"))
1421 (mdw-define-face calendar-today-face
1422 (t :foreground "yellow" :weight bold))
1423
1424 (mdw-define-face comint-highlight-prompt
1425 (t :weight bold))
1426 (mdw-define-face comint-highlight-input
1427 (t nil))
1428
1429 (mdw-define-face ido-subdir
1430 (t :foreground "cyan" :weight bold))
1431
1432 (mdw-define-face dired-directory
1433 (t :foreground "cyan" :weight bold))
1434 (mdw-define-face dired-symlink
1435 (t :foreground "cyan"))
1436 (mdw-define-face dired-perm-write
1437 (t nil))
1438
1439 (mdw-define-face trailing-whitespace
1440 (((class color)) :background "red")
1441 (t :inverse-video t))
1442 (mdw-define-face whitespace-line
1443 (((class color)) :background "darkred")
1444 (t :inverse-video t))
1445 (mdw-define-face mdw-punct-face
1446 (((min-colors 64)) :foreground "burlywood2")
1447 (((class color)) :foreground "yellow"))
1448 (mdw-define-face mdw-number-face
1449 (t :foreground "yellow"))
1450 (mdw-define-face mdw-trivial-face)
1451 (mdw-define-face font-lock-function-name-face
1452 (t :slant italic))
1453 (mdw-define-face font-lock-keyword-face
1454 (t :weight bold))
1455 (mdw-define-face font-lock-constant-face
1456 (t :slant italic))
1457 (mdw-define-face font-lock-builtin-face
1458 (t :weight bold))
1459 (mdw-define-face font-lock-type-face
1460 (t :weight bold :slant italic))
1461 (mdw-define-face font-lock-reference-face
1462 (t :weight bold))
1463 (mdw-define-face font-lock-variable-name-face
1464 (t :slant italic))
1465 (mdw-define-face font-lock-comment-delimiter-face
1466 (((min-colors 64)) :slant italic :foreground "SeaGreen1")
1467 (((class color)) :foreground "green")
1468 (t :weight bold))
1469 (mdw-define-face font-lock-comment-face
1470 (((min-colors 64)) :slant italic :foreground "SeaGreen1")
1471 (((class color)) :foreground "green")
1472 (t :weight bold))
1473 (mdw-define-face font-lock-string-face
1474 (((min-colors 64)) :foreground "SkyBlue1")
1475 (((class color)) :foreground "cyan")
1476 (t :weight bold))
1477
1478 (mdw-define-face message-separator
1479 (t :background "red" :foreground "white" :weight bold))
1480 (mdw-define-face message-cited-text
1481 (default :slant italic)
1482 (((min-colors 64)) :foreground "SkyBlue1")
1483 (((class color)) :foreground "cyan"))
1484 (mdw-define-face message-header-cc
1485 (default :slant italic)
1486 (((min-colors 64)) :foreground "SeaGreen1")
1487 (((class color)) :foreground "green"))
1488 (mdw-define-face message-header-newsgroups
1489 (default :slant italic)
1490 (((min-colors 64)) :foreground "SeaGreen1")
1491 (((class color)) :foreground "green"))
1492 (mdw-define-face message-header-subject
1493 (((min-colors 64)) :foreground "SeaGreen1")
1494 (((class color)) :foreground "green"))
1495 (mdw-define-face message-header-to
1496 (((min-colors 64)) :foreground "SeaGreen1")
1497 (((class color)) :foreground "green"))
1498 (mdw-define-face message-header-xheader
1499 (default :slant italic)
1500 (((min-colors 64)) :foreground "SeaGreen1")
1501 (((class color)) :foreground "green"))
1502 (mdw-define-face message-header-other
1503 (default :slant italic)
1504 (((min-colors 64)) :foreground "SeaGreen1")
1505 (((class color)) :foreground "green"))
1506 (mdw-define-face message-header-name
1507 (default :weight bold)
1508 (((min-colors 64)) :foreground "SeaGreen1")
1509 (((class color)) :foreground "green"))
1510
1511 (mdw-define-face which-func
1512 (t nil))
1513
1514 (mdw-define-face gnus-header-name
1515 (default :weight bold)
1516 (((min-colors 64)) :foreground "SeaGreen1")
1517 (((class color)) :foreground "green"))
1518 (mdw-define-face gnus-header-subject
1519 (((min-colors 64)) :foreground "SeaGreen1")
1520 (((class color)) :foreground "green"))
1521 (mdw-define-face gnus-header-from
1522 (((min-colors 64)) :foreground "SeaGreen1")
1523 (((class color)) :foreground "green"))
1524 (mdw-define-face gnus-header-to
1525 (((min-colors 64)) :foreground "SeaGreen1")
1526 (((class color)) :foreground "green"))
1527 (mdw-define-face gnus-header-content
1528 (default :slant italic)
1529 (((min-colors 64)) :foreground "SeaGreen1")
1530 (((class color)) :foreground "green"))
1531
1532 (mdw-define-face gnus-cite-1
1533 (((min-colors 64)) :foreground "SkyBlue1")
1534 (((class color)) :foreground "cyan"))
1535 (mdw-define-face gnus-cite-2
1536 (((min-colors 64)) :foreground "RoyalBlue2")
1537 (((class color)) :foreground "blue"))
1538 (mdw-define-face gnus-cite-3
1539 (((min-colors 64)) :foreground "MediumOrchid")
1540 (((class color)) :foreground "magenta"))
1541 (mdw-define-face gnus-cite-4
1542 (((min-colors 64)) :foreground "firebrick2")
1543 (((class color)) :foreground "red"))
1544 (mdw-define-face gnus-cite-5
1545 (((min-colors 64)) :foreground "burlywood2")
1546 (((class color)) :foreground "yellow"))
1547 (mdw-define-face gnus-cite-6
1548 (((min-colors 64)) :foreground "SeaGreen1")
1549 (((class color)) :foreground "green"))
1550 (mdw-define-face gnus-cite-7
1551 (((min-colors 64)) :foreground "SlateBlue1")
1552 (((class color)) :foreground "cyan"))
1553 (mdw-define-face gnus-cite-8
1554 (((min-colors 64)) :foreground "RoyalBlue2")
1555 (((class color)) :foreground "blue"))
1556 (mdw-define-face gnus-cite-9
1557 (((min-colors 64)) :foreground "purple2")
1558 (((class color)) :foreground "magenta"))
1559 (mdw-define-face gnus-cite-10
1560 (((min-colors 64)) :foreground "DarkOrange2")
1561 (((class color)) :foreground "red"))
1562 (mdw-define-face gnus-cite-11
1563 (t :foreground "grey"))
1564
1565 (mdw-define-face diff-header
1566 (t nil))
1567 (mdw-define-face diff-index
1568 (t :weight bold))
1569 (mdw-define-face diff-file-header
1570 (t :weight bold))
1571 (mdw-define-face diff-hunk-header
1572 (((min-colors 64)) :foreground "SkyBlue1")
1573 (((class color)) :foreground "cyan"))
1574 (mdw-define-face diff-function
1575 (default :weight bold)
1576 (((min-colors 64)) :foreground "SkyBlue1")
1577 (((class color)) :foreground "cyan"))
1578 (mdw-define-face diff-header
1579 (((min-colors 64)) :background "grey10"))
1580 (mdw-define-face diff-added
1581 (((class color)) :foreground "green"))
1582 (mdw-define-face diff-removed
1583 (((class color)) :foreground "red"))
1584 (mdw-define-face diff-context
1585 (t nil))
1586 (mdw-define-face diff-refine-change
1587 (((min-colors 64)) :background "RoyalBlue4")
1588 (t :underline t))
1589 (mdw-define-face diff-refine-removed
1590 (((min-colors 64)) :background "#500")
1591 (t :underline t))
1592 (mdw-define-face diff-refine-added
1593 (((min-colors 64)) :background "#050")
1594 (t :underline t))
1595
1596 (setq ediff-force-faces t)
1597 (mdw-define-face ediff-current-diff-A
1598 (((min-colors 64)) :background "darkred")
1599 (((class color)) :background "red")
1600 (t :inverse-video t))
1601 (mdw-define-face ediff-fine-diff-A
1602 (((min-colors 64)) :background "red3")
1603 (((class color)) :inverse-video t)
1604 (t :inverse-video nil))
1605 (mdw-define-face ediff-even-diff-A
1606 (((min-colors 64)) :background "#300"))
1607 (mdw-define-face ediff-odd-diff-A
1608 (((min-colors 64)) :background "#300"))
1609 (mdw-define-face ediff-current-diff-B
1610 (((min-colors 64)) :background "darkgreen")
1611 (((class color)) :background "magenta")
1612 (t :inverse-video t))
1613 (mdw-define-face ediff-fine-diff-B
1614 (((min-colors 64)) :background "green4")
1615 (((class color)) :inverse-video t)
1616 (t :inverse-video nil))
1617 (mdw-define-face ediff-even-diff-B
1618 (((min-colors 64)) :background "#020"))
1619 (mdw-define-face ediff-odd-diff-B
1620 (((min-colors 64)) :background "#020"))
1621 (mdw-define-face ediff-current-diff-C
1622 (((min-colors 64)) :background "darkblue")
1623 (((class color)) :background "blue")
1624 (t :inverse-video t))
1625 (mdw-define-face ediff-fine-diff-C
1626 (((min-colors 64)) :background "blue1")
1627 (((class color)) :inverse-video t)
1628 (t :inverse-video nil))
1629 (mdw-define-face ediff-even-diff-C
1630 (((min-colors 64)) :background "#004"))
1631 (mdw-define-face ediff-odd-diff-C
1632 (((min-colors 64)) :background "#004"))
1633 (mdw-define-face ediff-current-diff-Ancestor
1634 (((min-colors 64)) :background "#630")
1635 (((class color)) :background "blue")
1636 (t :inverse-video t))
1637 (mdw-define-face ediff-even-diff-Ancestor
1638 (((min-colors 64)) :background "#320"))
1639 (mdw-define-face ediff-odd-diff-Ancestor
1640 (((min-colors 64)) :background "#320"))
1641
1642 (mdw-define-face magit-hash
1643 (((min-colors 64)) :foreground "grey40")
1644 (((class color)) :foreground "blue"))
1645 (mdw-define-face magit-diff-hunk-heading
1646 (((min-colors 64)) :foreground "grey70" :background "grey25")
1647 (((class color)) :foreground "yellow"))
1648 (mdw-define-face magit-diff-hunk-heading-highlight
1649 (((min-colors 64)) :foreground "grey70" :background "grey35")
1650 (((class color)) :foreground "yellow" :background "blue"))
1651 (mdw-define-face magit-diff-added
1652 (((min-colors 64)) :foreground "#ddffdd" :background "#335533")
1653 (((class color)) :foreground "green"))
1654 (mdw-define-face magit-diff-added-highlight
1655 (((min-colors 64)) :foreground "#cceecc" :background "#336633")
1656 (((class color)) :foreground "green" :background "blue"))
1657 (mdw-define-face magit-diff-removed
1658 (((min-colors 64)) :foreground "#ffdddd" :background "#553333")
1659 (((class color)) :foreground "red"))
1660 (mdw-define-face magit-diff-removed-highlight
1661 (((min-colors 64)) :foreground "#eecccc" :background "#663333")
1662 (((class color)) :foreground "red" :background "blue"))
1663 (mdw-define-face magit-blame-heading
1664 (((min-colors 64)) :foreground "white" :background "grey25"
1665 :weight normal :slant normal)
1666 (((class color)) :foreground "white" :background "blue"
1667 :weight normal :slant normal))
1668 (mdw-define-face magit-blame-name
1669 (t :inherit magit-blame-heading :slant italic))
1670 (mdw-define-face magit-blame-date
1671 (((min-colors 64)) :inherit magit-blame-heading :foreground "grey60")
1672 (((class color)) :inherit magit-blame-heading :foreground "cyan"))
1673 (mdw-define-face magit-blame-summary
1674 (t :inherit magit-blame-heading :weight bold))
1675
1676 (mdw-define-face dylan-header-background
1677 (((min-colors 64)) :background "NavyBlue")
1678 (((class color)) :background "blue"))
1679
1680 (mdw-define-face erc-input-face
1681 (t :foreground "red"))
1682
1683 (mdw-define-face woman-bold
1684 (t :weight bold))
1685 (mdw-define-face woman-italic
1686 (t :slant italic))
1687
1688 (eval-after-load "rst"
1689 '(progn
1690 (mdw-define-face rst-level-1-face
1691 (t :foreground "SkyBlue1" :weight bold))
1692 (mdw-define-face rst-level-2-face
1693 (t :foreground "SeaGreen1" :weight bold))
1694 (mdw-define-face rst-level-3-face
1695 (t :weight bold))
1696 (mdw-define-face rst-level-4-face
1697 (t :slant italic))
1698 (mdw-define-face rst-level-5-face
1699 (t :underline t))
1700 (mdw-define-face rst-level-6-face
1701 ())))
1702
1703 (mdw-define-face p4-depot-added-face
1704 (t :foreground "green"))
1705 (mdw-define-face p4-depot-branch-op-face
1706 (t :foreground "yellow"))
1707 (mdw-define-face p4-depot-deleted-face
1708 (t :foreground "red"))
1709 (mdw-define-face p4-depot-unmapped-face
1710 (t :foreground "SkyBlue1"))
1711 (mdw-define-face p4-diff-change-face
1712 (t :foreground "yellow"))
1713 (mdw-define-face p4-diff-del-face
1714 (t :foreground "red"))
1715 (mdw-define-face p4-diff-file-face
1716 (t :foreground "SkyBlue1"))
1717 (mdw-define-face p4-diff-head-face
1718 (t :background "grey10"))
1719 (mdw-define-face p4-diff-ins-face
1720 (t :foreground "green"))
1721
1722 (mdw-define-face w3m-anchor-face
1723 (t :foreground "SkyBlue1" :underline t))
1724 (mdw-define-face w3m-arrived-anchor-face
1725 (t :foreground "SkyBlue1" :underline t))
1726
1727 (mdw-define-face whizzy-slice-face
1728 (t :background "grey10"))
1729 (mdw-define-face whizzy-error-face
1730 (t :background "darkred"))
1731
1732 ;; Ellipses used to indicate hidden text (and similar).
1733 (mdw-define-face mdw-ellipsis-face
1734 (((type tty)) :foreground "blue") (t :foreground "grey60"))
1735 (let ((dollar (make-glyph-code ?$ 'mdw-ellipsis-face))
1736 (backslash (make-glyph-code ?\\ 'mdw-ellipsis-face))
1737 (dot (make-glyph-code ?. 'mdw-ellipsis-face))
1738 (bar (make-glyph-code ?| mdw-ellipsis-face)))
1739 (set-display-table-slot standard-display-table 0 dollar)
1740 (set-display-table-slot standard-display-table 1 backslash)
1741 (set-display-table-slot standard-display-table 4
1742 (vector dot dot dot))
1743 (set-display-table-slot standard-display-table 5 bar))
1744
1745 ;;;--------------------------------------------------------------------------
1746 ;;; Where is point?
1747
1748 (mdw-define-face mdw-point-overlay-face
1749 (((type graphic)))
1750 (((min-colors 64)) :background "darkblue")
1751 (((class color)) :background "blue")
1752 (((type tty) (class mono)) :inverse-video t))
1753
1754 (defvar mdw-point-overlay-fringe-display '(vertical-bar . vertical-bar))
1755
1756 (defun mdw-configure-point-overlay ()
1757 (let ((ov (make-overlay 0 0)))
1758 (overlay-put ov 'priority 0)
1759 (let* ((fringe (or mdw-point-overlay-fringe-display (cons nil nil)))
1760 (left (car fringe)) (right (cdr fringe))
1761 (s ""))
1762 (when left
1763 (let ((ss "."))
1764 (put-text-property 0 1 'display `(left-fringe ,left) ss)
1765 (setq s (concat s ss))))
1766 (when right
1767 (let ((ss "."))
1768 (put-text-property 0 1 'display `(right-fringe ,right) ss)
1769 (setq s (concat s ss))))
1770 (when (or left right)
1771 (overlay-put ov 'before-string s)))
1772 (overlay-put ov 'face 'mdw-point-overlay-face)
1773 (delete-overlay ov)
1774 ov))
1775
1776 (defvar mdw-point-overlay (mdw-configure-point-overlay)
1777 "An overlay used for showing where point is in the selected window.")
1778 (defun mdw-reconfigure-point-overlay ()
1779 (interactive)
1780 (setq mdw-point-overlay (mdw-configure-point-overlay)))
1781
1782 (defun mdw-remove-point-overlay ()
1783 "Remove the current-point overlay."
1784 (delete-overlay mdw-point-overlay))
1785
1786 (defun mdw-update-point-overlay ()
1787 "Mark the current point position with an overlay."
1788 (if (not mdw-point-overlay-mode)
1789 (mdw-remove-point-overlay)
1790 (overlay-put mdw-point-overlay 'window (selected-window))
1791 (move-overlay mdw-point-overlay
1792 (line-beginning-position)
1793 (+ (line-end-position) 1))))
1794
1795 (defvar mdw-point-overlay-buffers nil
1796 "List of buffers using `mdw-point-overlay-mode'.")
1797
1798 (define-minor-mode mdw-point-overlay-mode
1799 "Indicate current line with an overlay."
1800 :global nil
1801 (let ((buffer (current-buffer)))
1802 (setq mdw-point-overlay-buffers
1803 (mapcan (lambda (buf)
1804 (if (and (buffer-live-p buf)
1805 (not (eq buf buffer)))
1806 (list buf)))
1807 mdw-point-overlay-buffers))
1808 (if mdw-point-overlay-mode
1809 (setq mdw-point-overlay-buffers
1810 (cons buffer mdw-point-overlay-buffers))))
1811 (cond (mdw-point-overlay-buffers
1812 (add-hook 'pre-command-hook 'mdw-remove-point-overlay)
1813 (add-hook 'post-command-hook 'mdw-update-point-overlay))
1814 (t
1815 (mdw-remove-point-overlay)
1816 (remove-hook 'pre-command-hook 'mdw-remove-point-overlay)
1817 (remove-hook 'post-command-hook 'mdw-update-point-overlay))))
1818
1819 (define-globalized-minor-mode mdw-global-point-overlay-mode
1820 mdw-point-overlay-mode
1821 (lambda () (if (not (minibufferp)) (mdw-point-overlay-mode t))))
1822
1823 ;;;--------------------------------------------------------------------------
1824 ;;; C programming configuration.
1825
1826 ;; Make C indentation nice.
1827
1828 (defun mdw-c-lineup-arglist (langelem)
1829 "Hack for DWIMmery in c-lineup-arglist."
1830 (if (save-excursion
1831 (c-block-in-arglist-dwim (c-langelem-2nd-pos c-syntactic-element)))
1832 0
1833 (c-lineup-arglist langelem)))
1834
1835 (defun mdw-c-indent-extern-mumble (langelem)
1836 "Indent `extern \"...\" {' lines."
1837 (save-excursion
1838 (back-to-indentation)
1839 (if (looking-at
1840 "\\s-*\\<extern\\>\\s-*\"\\([^\\\\\"]+\\|\\.\\)*\"\\s-*{")
1841 c-basic-offset
1842 nil)))
1843
1844 (defun mdw-c-indent-arglist-nested (langelem)
1845 "Indent continued argument lists.
1846 If we've nested more than one argument list, then only introduce a single
1847 indentation anyway."
1848 (let ((context c-syntactic-context)
1849 (pos (c-langelem-2nd-pos c-syntactic-element))
1850 (should-indent-p t))
1851 (while (and context
1852 (eq (caar context) 'arglist-cont-nonempty))
1853 (when (and (= (caddr (pop context)) pos)
1854 context
1855 (memq (caar context) '(arglist-intro
1856 arglist-cont-nonempty)))
1857 (setq should-indent-p nil)))
1858 (if should-indent-p '+ 0)))
1859
1860 (defvar mdw-define-c-styles-hook nil
1861 "Hook run when `cc-mode' starts up to define styles.")
1862
1863 (defmacro mdw-define-c-style (name &rest assocs)
1864 "Define a C style, called NAME (a symbol), setting ASSOCs.
1865 A function, named `mdw-define-c-style/NAME', is defined to actually install
1866 the style using `c-add-style', and added to the hook
1867 `mdw-define-c-styles-hook'. If CC Mode is already loaded, then the style is
1868 set."
1869 (declare (indent defun))
1870 (let* ((name-string (symbol-name name))
1871 (func (intern (concat "mdw-define-c-style/" name-string))))
1872 `(progn
1873 (defun ,func () (c-add-style ,name-string ',assocs))
1874 (and (featurep 'cc-mode) (,func))
1875 (add-hook 'mdw-define-c-styles-hook ',func))))
1876
1877 (eval-after-load "cc-mode"
1878 '(run-hooks 'mdw-define-c-styles-hook))
1879
1880 (mdw-define-c-style mdw-trustonic-c
1881 (c-basic-offset . 4)
1882 (comment-column . 0)
1883 (c-indent-comment-alist (anchored-comment . (column . 0))
1884 (end-block . (space . 1))
1885 (cpp-end-block . (space . 1))
1886 (other . (space . 1)))
1887 (c-class-key . "class")
1888 (c-backslash-column . 0)
1889 (c-auto-align-backslashes . nil)
1890 (c-label-minimum-indentation . 0)
1891 (c-offsets-alist (substatement-open . (add 0 c-indent-one-line-block))
1892 (defun-open . (add 0 c-indent-one-line-block))
1893 (arglist-cont-nonempty . mdw-c-indent-arglist-nested)
1894 (topmost-intro . mdw-c-indent-extern-mumble)
1895 (cpp-define-intro . 0)
1896 (knr-argdecl . 0)
1897 (inextern-lang . [0])
1898 (label . 0)
1899 (case-label . +)
1900 (access-label . -2)
1901 (inclass . +)
1902 (inline-open . ++)
1903 (statement-cont . +)
1904 (statement-case-intro . +)))
1905
1906 (mdw-define-c-style mdw-c
1907 (c-basic-offset . 2)
1908 (comment-column . 40)
1909 (c-class-key . "class")
1910 (c-backslash-column . 72)
1911 (c-label-minimum-indentation . 0)
1912 (c-offsets-alist (substatement-open . (add 0 c-indent-one-line-block))
1913 (defun-open . (add 0 c-indent-one-line-block))
1914 (arglist-cont-nonempty . mdw-c-lineup-arglist)
1915 (topmost-intro . mdw-c-indent-extern-mumble)
1916 (cpp-define-intro . 0)
1917 (knr-argdecl . 0)
1918 (inextern-lang . [0])
1919 (label . 0)
1920 (case-label . +)
1921 (access-label . -)
1922 (inclass . +)
1923 (inline-open . ++)
1924 (statement-cont . +)
1925 (statement-case-intro . +)))
1926
1927 (defun mdw-set-default-c-style (modes style)
1928 "Update the default CC Mode style for MODES to be STYLE.
1929
1930 MODES may be a list of major mode names or a singleton. STYLE is a style
1931 name, as a symbol."
1932 (let ((modes (if (listp modes) modes (list modes)))
1933 (style (symbol-name style)))
1934 (setq c-default-style
1935 (append (mapcar (lambda (mode)
1936 (cons mode style))
1937 modes)
1938 (remove-if (lambda (assoc)
1939 (memq (car assoc) modes))
1940 (if (listp c-default-style)
1941 c-default-style
1942 (list (cons 'other c-default-style))))))))
1943 (setq c-default-style "mdw-c")
1944
1945 (mdw-set-default-c-style '(c-mode c++-mode) 'mdw-c)
1946
1947 (defvar mdw-c-comment-fill-prefix
1948 `((,(concat "\\([ \t]*/?\\)"
1949 "\\(\\*\\|//\\)"
1950 "\\([ \t]*\\)"
1951 "\\([A-Za-z]+:[ \t]*\\)?"
1952 mdw-hanging-indents)
1953 (pad . 1) (match . 2) (pad . 3) (pad . 4) (pad . 5)))
1954 "Fill prefix matching C comments (both kinds).")
1955
1956 (defun mdw-fontify-c-and-c++ ()
1957
1958 ;; Fiddle with some syntax codes.
1959 (modify-syntax-entry ?* ". 23")
1960 (modify-syntax-entry ?/ ". 124b")
1961 (modify-syntax-entry ?\n "> b")
1962
1963 ;; Other stuff.
1964 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
1965
1966 ;; Now define things to be fontified.
1967 (make-local-variable 'font-lock-keywords)
1968 (let ((c-keywords
1969 (mdw-regexps "alignas" ;C11 macro, C++11
1970 "alignof" ;C++11
1971 "and" ;C++, C95 macro
1972 "and_eq" ;C++, C95 macro
1973 "asm" ;K&R, C++, GCC
1974 "atomic" ;C11 macro, C++11 template type
1975 "auto" ;K&R, C89
1976 "bitand" ;C++, C95 macro
1977 "bitor" ;C++, C95 macro
1978 "bool" ;C++, C99 macro
1979 "break" ;K&R, C89
1980 "case" ;K&R, C89
1981 "catch" ;C++
1982 "char" ;K&R, C89
1983 "char16_t" ;C++11, C11 library type
1984 "char32_t" ;C++11, C11 library type
1985 "class" ;C++
1986 "complex" ;C99 macro, C++ template type
1987 "compl" ;C++, C95 macro
1988 "const" ;C89
1989 "constexpr" ;C++11
1990 "const_cast" ;C++
1991 "continue" ;K&R, C89
1992 "decltype" ;C++11
1993 "defined" ;C89 preprocessor
1994 "default" ;K&R, C89
1995 "delete" ;C++
1996 "do" ;K&R, C89
1997 "double" ;K&R, C89
1998 "dynamic_cast" ;C++
1999 "else" ;K&R, C89
2000 ;; "entry" ;K&R -- never used
2001 "enum" ;C89
2002 "explicit" ;C++
2003 "export" ;C++
2004 "extern" ;K&R, C89
2005 "float" ;K&R, C89
2006 "for" ;K&R, C89
2007 ;; "fortran" ;K&R
2008 "friend" ;C++
2009 "goto" ;K&R, C89
2010 "if" ;K&R, C89
2011 "imaginary" ;C99 macro
2012 "inline" ;C++, C99, GCC
2013 "int" ;K&R, C89
2014 "long" ;K&R, C89
2015 "mutable" ;C++
2016 "namespace" ;C++
2017 "new" ;C++
2018 "noexcept" ;C++11
2019 "noreturn" ;C11 macro
2020 "not" ;C++, C95 macro
2021 "not_eq" ;C++, C95 macro
2022 "nullptr" ;C++11
2023 "operator" ;C++
2024 "or" ;C++, C95 macro
2025 "or_eq" ;C++, C95 macro
2026 "private" ;C++
2027 "protected" ;C++
2028 "public" ;C++
2029 "register" ;K&R, C89
2030 "reinterpret_cast" ;C++
2031 "restrict" ;C99
2032 "return" ;K&R, C89
2033 "short" ;K&R, C89
2034 "signed" ;C89
2035 "sizeof" ;K&R, C89
2036 "static" ;K&R, C89
2037 "static_assert" ;C11 macro, C++11
2038 "static_cast" ;C++
2039 "struct" ;K&R, C89
2040 "switch" ;K&R, C89
2041 "template" ;C++
2042 "throw" ;C++
2043 "try" ;C++
2044 "thread_local" ;C11 macro, C++11
2045 "typedef" ;C89
2046 "typeid" ;C++
2047 "typeof" ;GCC
2048 "typename" ;C++
2049 "union" ;K&R, C89
2050 "unsigned" ;K&R, C89
2051 "using" ;C++
2052 "virtual" ;C++
2053 "void" ;C89
2054 "volatile" ;C89
2055 "wchar_t" ;C++, C89 library type
2056 "while" ;K&R, C89
2057 "xor" ;C++, C95 macro
2058 "xor_eq" ;C++, C95 macro
2059 "_Alignas" ;C11
2060 "_Alignof" ;C11
2061 "_Atomic" ;C11
2062 "_Bool" ;C99
2063 "_Complex" ;C99
2064 "_Generic" ;C11
2065 "_Imaginary" ;C99
2066 "_Noreturn" ;C11
2067 "_Pragma" ;C99 preprocessor
2068 "_Static_assert" ;C11
2069 "_Thread_local" ;C11
2070 "__alignof__" ;GCC
2071 "__asm__" ;GCC
2072 "__attribute__" ;GCC
2073 "__complex__" ;GCC
2074 "__const__" ;GCC
2075 "__extension__" ;GCC
2076 "__imag__" ;GCC
2077 "__inline__" ;GCC
2078 "__label__" ;GCC
2079 "__real__" ;GCC
2080 "__signed__" ;GCC
2081 "__typeof__" ;GCC
2082 "__volatile__" ;GCC
2083 ))
2084 (c-constants
2085 (mdw-regexps "false" ;C++, C99 macro
2086 "this" ;C++
2087 "true" ;C++, C99 macro
2088 ))
2089 (preprocessor-keywords
2090 (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
2091 "ident" "if" "ifdef" "ifndef" "import" "include"
2092 "line" "pragma" "unassert" "undef" "warning"))
2093 (objc-keywords
2094 (mdw-regexps "class" "defs" "encode" "end" "implementation"
2095 "interface" "private" "protected" "protocol" "public"
2096 "selector")))
2097
2098 (setq font-lock-keywords
2099 (list
2100
2101 ;; Fontify include files as strings.
2102 (list (concat "^[ \t]*\\#[ \t]*"
2103 "\\(include\\|import\\)"
2104 "[ \t]*\\(<[^>]+\\(>\\|\\)\\)")
2105 '(2 font-lock-string-face))
2106
2107 ;; Preprocessor directives are `references'?.
2108 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
2109 preprocessor-keywords
2110 "\\)\\>\\|[0-9]+\\|$\\)\\)")
2111 '(1 font-lock-keyword-face))
2112
2113 ;; Handle the keywords defined above.
2114 (list (concat "@\\<\\(" objc-keywords "\\)\\>")
2115 '(0 font-lock-keyword-face))
2116
2117 (list (concat "\\<\\(" c-keywords "\\)\\>")
2118 '(0 font-lock-keyword-face))
2119
2120 (list (concat "\\<\\(" c-constants "\\)\\>")
2121 '(0 font-lock-variable-name-face))
2122
2123 ;; Handle numbers too.
2124 ;;
2125 ;; This looks strange, I know. It corresponds to the
2126 ;; preprocessor's idea of what a number looks like, rather than
2127 ;; anything sensible.
2128 (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
2129 "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
2130 '(0 mdw-number-face))
2131
2132 ;; And anything else is punctuation.
2133 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2134 '(0 mdw-punct-face))))))
2135
2136 (define-derived-mode sod-mode c-mode "Sod"
2137 "Major mode for editing Sod code.")
2138 (push '("\\.sod$" . sod-mode) auto-mode-alist)
2139
2140 ;;;--------------------------------------------------------------------------
2141 ;;; AP calc mode.
2142
2143 (define-derived-mode apcalc-mode c-mode "AP Calc"
2144 "Major mode for editing Calc code.")
2145
2146 (defun mdw-fontify-apcalc ()
2147
2148 ;; Fiddle with some syntax codes.
2149 (modify-syntax-entry ?* ". 23")
2150 (modify-syntax-entry ?/ ". 14")
2151
2152 ;; Other stuff.
2153 (setq comment-start "/* ")
2154 (setq comment-end " */")
2155 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2156
2157 ;; Now define things to be fontified.
2158 (make-local-variable 'font-lock-keywords)
2159 (let ((c-keywords
2160 (mdw-regexps "break" "case" "cd" "continue" "define" "default"
2161 "do" "else" "exit" "for" "global" "goto" "help" "if"
2162 "local" "mat" "obj" "print" "quit" "read" "return"
2163 "show" "static" "switch" "while" "write")))
2164
2165 (setq font-lock-keywords
2166 (list
2167
2168 ;; Handle the keywords defined above.
2169 (list (concat "\\<\\(" c-keywords "\\)\\>")
2170 '(0 font-lock-keyword-face))
2171
2172 ;; Handle numbers too.
2173 ;;
2174 ;; This looks strange, I know. It corresponds to the
2175 ;; preprocessor's idea of what a number looks like, rather than
2176 ;; anything sensible.
2177 (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
2178 "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
2179 '(0 mdw-number-face))
2180
2181 ;; And anything else is punctuation.
2182 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2183 '(0 mdw-punct-face))))))
2184
2185 ;;;--------------------------------------------------------------------------
2186 ;;; Java programming configuration.
2187
2188 ;; Make indentation nice.
2189
2190 (mdw-define-c-style mdw-java
2191 (c-basic-offset . 2)
2192 (c-backslash-column . 72)
2193 (c-offsets-alist (substatement-open . 0)
2194 (label . +)
2195 (case-label . +)
2196 (access-label . 0)
2197 (inclass . +)
2198 (statement-case-intro . +)))
2199 (mdw-set-default-c-style 'java-mode 'mdw-java)
2200
2201 ;; Declare Java fontification style.
2202
2203 (defun mdw-fontify-java ()
2204
2205 ;; Fiddle with some syntax codes.
2206 (modify-syntax-entry ?@ ".")
2207 (modify-syntax-entry ?@ "." font-lock-syntax-table)
2208
2209 ;; Other stuff.
2210 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2211
2212 ;; Now define things to be fontified.
2213 (make-local-variable 'font-lock-keywords)
2214 (let ((java-keywords
2215 (mdw-regexps "abstract" "assert"
2216 "boolean" "break" "byte"
2217 "case" "catch" "char" "class" "const" "continue"
2218 "default" "do" "double"
2219 "else" "enum" "extends"
2220 "final" "finally" "float" "for"
2221 "goto"
2222 "if" "implements" "import" "instanceof" "int"
2223 "interface"
2224 "long"
2225 "native" "new"
2226 "package" "private" "protected" "public"
2227 "return"
2228 "short" "static" "strictfp" "switch" "synchronized"
2229 "throw" "throws" "transient" "try"
2230 "void" "volatile"
2231 "while"))
2232
2233 (java-constants
2234 (mdw-regexps "false" "null" "super" "this" "true")))
2235
2236 (setq font-lock-keywords
2237 (list
2238
2239 ;; Handle the keywords defined above.
2240 (list (concat "\\<\\(" java-keywords "\\)\\>")
2241 '(0 font-lock-keyword-face))
2242
2243 ;; Handle the magic constants defined above.
2244 (list (concat "\\<\\(" java-constants "\\)\\>")
2245 '(0 font-lock-variable-name-face))
2246
2247 ;; Handle numbers too.
2248 ;;
2249 ;; The following isn't quite right, but it's close enough.
2250 (list (concat "\\<\\("
2251 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2252 "[0-9]+\\(\\.[0-9]*\\|\\)"
2253 "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
2254 "[lLfFdD]?")
2255 '(0 mdw-number-face))
2256
2257 ;; And anything else is punctuation.
2258 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2259 '(0 mdw-punct-face))))))
2260
2261 ;;;--------------------------------------------------------------------------
2262 ;;; Javascript programming configuration.
2263
2264 (defun mdw-javascript-style ()
2265 (setq js-indent-level 2)
2266 (setq js-expr-indent-offset 0))
2267
2268 (defun mdw-fontify-javascript ()
2269
2270 ;; Other stuff.
2271 (mdw-javascript-style)
2272 (setq js-auto-indent-flag t)
2273
2274 ;; Now define things to be fontified.
2275 (make-local-variable 'font-lock-keywords)
2276 (let ((javascript-keywords
2277 (mdw-regexps "abstract" "boolean" "break" "byte" "case" "catch"
2278 "char" "class" "const" "continue" "debugger" "default"
2279 "delete" "do" "double" "else" "enum" "export" "extends"
2280 "final" "finally" "float" "for" "function" "goto" "if"
2281 "implements" "import" "in" "instanceof" "int"
2282 "interface" "let" "long" "native" "new" "package"
2283 "private" "protected" "public" "return" "short"
2284 "static" "super" "switch" "synchronized" "throw"
2285 "throws" "transient" "try" "typeof" "var" "void"
2286 "volatile" "while" "with" "yield"
2287
2288 "boolean" "byte" "char" "double" "float" "int" "long"
2289 "short" "void"))
2290 (javascript-constants
2291 (mdw-regexps "false" "null" "undefined" "Infinity" "NaN" "true"
2292 "arguments" "this")))
2293
2294 (setq font-lock-keywords
2295 (list
2296
2297 ;; Handle the keywords defined above.
2298 (list (concat "\\_<\\(" javascript-keywords "\\)\\_>")
2299 '(0 font-lock-keyword-face))
2300
2301 ;; Handle the predefined constants defined above.
2302 (list (concat "\\_<\\(" javascript-constants "\\)\\_>")
2303 '(0 font-lock-variable-name-face))
2304
2305 ;; Handle numbers too.
2306 ;;
2307 ;; The following isn't quite right, but it's close enough.
2308 (list (concat "\\_<\\("
2309 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2310 "[0-9]+\\(\\.[0-9]*\\|\\)"
2311 "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
2312 "[lLfFdD]?")
2313 '(0 mdw-number-face))
2314
2315 ;; And anything else is punctuation.
2316 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2317 '(0 mdw-punct-face))))))
2318
2319 ;;;--------------------------------------------------------------------------
2320 ;;; Scala programming configuration.
2321
2322 (defun mdw-fontify-scala ()
2323
2324 ;; Comment filling.
2325 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2326
2327 ;; Define things to be fontified.
2328 (make-local-variable 'font-lock-keywords)
2329 (let ((scala-keywords
2330 (mdw-regexps "abstract" "case" "catch" "class" "def" "do" "else"
2331 "extends" "final" "finally" "for" "forSome" "if"
2332 "implicit" "import" "lazy" "match" "new" "object"
2333 "override" "package" "private" "protected" "return"
2334 "sealed" "throw" "trait" "try" "type" "val"
2335 "var" "while" "with" "yield"))
2336 (scala-constants
2337 (mdw-regexps "false" "null" "super" "this" "true"))
2338 (punctuation "[-!%^&*=+:@#~/?\\|`]"))
2339
2340 (setq font-lock-keywords
2341 (list
2342
2343 ;; Magical identifiers between backticks.
2344 (list (concat "`\\([^`]+\\)`")
2345 '(1 font-lock-variable-name-face))
2346
2347 ;; Handle the keywords defined above.
2348 (list (concat "\\_<\\(" scala-keywords "\\)\\_>")
2349 '(0 font-lock-keyword-face))
2350
2351 ;; Handle the constants defined above.
2352 (list (concat "\\_<\\(" scala-constants "\\)\\_>")
2353 '(0 font-lock-variable-name-face))
2354
2355 ;; Magical identifiers between backticks.
2356 (list (concat "`\\([^`]+\\)`")
2357 '(1 font-lock-variable-name-face))
2358
2359 ;; Handle numbers too.
2360 ;;
2361 ;; As usual, not quite right.
2362 (list (concat "\\_<\\("
2363 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2364 "[0-9]+\\(\\.[0-9]*\\|\\)"
2365 "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
2366 "[lLfFdD]?")
2367 '(0 mdw-number-face))
2368
2369 ;; And everything else is punctuation.
2370 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2371 '(0 mdw-punct-face)))
2372
2373 font-lock-syntactic-keywords
2374 (list
2375
2376 ;; Single quotes around characters. But not when used to quote
2377 ;; symbol names. Ugh.
2378 (list (concat "\\('\\)"
2379 "\\(" "."
2380 "\\|" "\\\\" "\\(" "\\\\\\\\" "\\)*"
2381 "u+" "[0-9a-fA-F]\\{4\\}"
2382 "\\|" "\\\\" "[0-7]\\{1,3\\}"
2383 "\\|" "\\\\" "." "\\)"
2384 "\\('\\)")
2385 '(1 "\"")
2386 '(4 "\""))))))
2387
2388 ;;;--------------------------------------------------------------------------
2389 ;;; C# programming configuration.
2390
2391 ;; Make indentation nice.
2392
2393 (mdw-define-c-style mdw-csharp
2394 (c-basic-offset . 2)
2395 (c-backslash-column . 72)
2396 (c-offsets-alist (substatement-open . 0)
2397 (label . 0)
2398 (case-label . +)
2399 (access-label . 0)
2400 (inclass . +)
2401 (statement-case-intro . +)))
2402 (mdw-set-default-c-style 'csharp-mode 'mdw-csharp)
2403
2404 ;; Declare C# fontification style.
2405
2406 (defun mdw-fontify-csharp ()
2407
2408 ;; Other stuff.
2409 (setq mdw-fill-prefix mdw-c-comment-fill-prefix)
2410
2411 ;; Now define things to be fontified.
2412 (make-local-variable 'font-lock-keywords)
2413 (let ((csharp-keywords
2414 (mdw-regexps "abstract" "as" "bool" "break" "byte" "case" "catch"
2415 "char" "checked" "class" "const" "continue" "decimal"
2416 "default" "delegate" "do" "double" "else" "enum"
2417 "event" "explicit" "extern" "finally" "fixed" "float"
2418 "for" "foreach" "goto" "if" "implicit" "in" "int"
2419 "interface" "internal" "is" "lock" "long" "namespace"
2420 "new" "object" "operator" "out" "override" "params"
2421 "private" "protected" "public" "readonly" "ref"
2422 "return" "sbyte" "sealed" "short" "sizeof"
2423 "stackalloc" "static" "string" "struct" "switch"
2424 "throw" "try" "typeof" "uint" "ulong" "unchecked"
2425 "unsafe" "ushort" "using" "virtual" "void" "volatile"
2426 "while" "yield"))
2427
2428 (csharp-constants
2429 (mdw-regexps "base" "false" "null" "this" "true")))
2430
2431 (setq font-lock-keywords
2432 (list
2433
2434 ;; Handle the keywords defined above.
2435 (list (concat "\\<\\(" csharp-keywords "\\)\\>")
2436 '(0 font-lock-keyword-face))
2437
2438 ;; Handle the magic constants defined above.
2439 (list (concat "\\<\\(" csharp-constants "\\)\\>")
2440 '(0 font-lock-variable-name-face))
2441
2442 ;; Handle numbers too.
2443 ;;
2444 ;; The following isn't quite right, but it's close enough.
2445 (list (concat "\\<\\("
2446 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2447 "[0-9]+\\(\\.[0-9]*\\|\\)"
2448 "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
2449 "[lLfFdD]?")
2450 '(0 mdw-number-face))
2451
2452 ;; And anything else is punctuation.
2453 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2454 '(0 mdw-punct-face))))))
2455
2456 (define-derived-mode csharp-mode java-mode "C#"
2457 "Major mode for editing C# code.")
2458
2459 ;;;--------------------------------------------------------------------------
2460 ;;; F# programming configuration.
2461
2462 (setq fsharp-indent-offset 2)
2463
2464 (defun mdw-fontify-fsharp ()
2465
2466 (let ((punct "=<>+-*/|&%!@?"))
2467 (do ((i 0 (1+ i)))
2468 ((>= i (length punct)))
2469 (modify-syntax-entry (aref punct i) ".")))
2470
2471 (modify-syntax-entry ?_ "_")
2472 (modify-syntax-entry ?( "(")
2473 (modify-syntax-entry ?) ")")
2474
2475 (setq indent-tabs-mode nil)
2476
2477 (let ((fsharp-keywords
2478 (mdw-regexps "abstract" "and" "as" "assert" "atomic"
2479 "begin" "break"
2480 "checked" "class" "component" "const" "constraint"
2481 "constructor" "continue"
2482 "default" "delegate" "do" "done" "downcast" "downto"
2483 "eager" "elif" "else" "end" "exception" "extern"
2484 "finally" "fixed" "for" "fori" "fun" "function"
2485 "functor"
2486 "global"
2487 "if" "in" "include" "inherit" "inline" "interface"
2488 "internal"
2489 "lazy" "let"
2490 "match" "measure" "member" "method" "mixin" "module"
2491 "mutable"
2492 "namespace" "new"
2493 "object" "of" "open" "or" "override"
2494 "parallel" "params" "private" "process" "protected"
2495 "public" "pure"
2496 "rec" "recursive" "return"
2497 "sealed" "sig" "static" "struct"
2498 "tailcall" "then" "to" "trait" "try" "type"
2499 "upcast" "use"
2500 "val" "virtual" "void" "volatile"
2501 "when" "while" "with"
2502 "yield"))
2503
2504 (fsharp-builtins
2505 (mdw-regexps "asr" "land" "lor" "lsl" "lsr" "lxor" "mod"
2506 "base" "false" "null" "true"))
2507
2508 (bang-keywords
2509 (mdw-regexps "do" "let" "return" "use" "yield"))
2510
2511 (preprocessor-keywords
2512 (mdw-regexps "if" "indent" "else" "endif")))
2513
2514 (setq font-lock-keywords
2515 (list (list (concat "\\(^\\|[^\"]\\)"
2516 "\\(" "(\\*"
2517 "[^*]*\\*+"
2518 "\\(" "[^)*]" "[^*]*" "\\*+" "\\)*"
2519 ")"
2520 "\\|"
2521 "//.*"
2522 "\\)")
2523 '(2 font-lock-comment-face))
2524
2525 (list (concat "'" "\\("
2526 "\\\\"
2527 "\\(" "[ntbr'\\]"
2528 "\\|" "[0-9][0-9][0-9]"
2529 "\\|" "u" "[0-9a-fA-F]\\{4\\}"
2530 "\\|" "U" "[0-9a-fA-F]\\{8\\}"
2531 "\\)"
2532 "\\|"
2533 "." "\\)" "'"
2534 "\\|"
2535 "\"" "[^\"\\]*"
2536 "\\(" "\\\\" "\\(.\\|\n\\)"
2537 "[^\"\\]*" "\\)*"
2538 "\\(\"\\|\\'\\)")
2539 '(0 font-lock-string-face))
2540
2541 (list (concat "\\_<\\(" bang-keywords "\\)!" "\\|"
2542 "^#[ \t]*\\(" preprocessor-keywords "\\)\\_>"
2543 "\\|"
2544 "\\_<\\(" fsharp-keywords "\\)\\_>")
2545 '(0 font-lock-keyword-face))
2546 (list (concat "\\<\\(" fsharp-builtins "\\)\\_>")
2547 '(0 font-lock-variable-name-face))
2548
2549 (list (concat "\\_<"
2550 "\\(" "0[bB][01]+" "\\|"
2551 "0[oO][0-7]+" "\\|"
2552 "0[xX][0-9a-fA-F]+" "\\)"
2553 "\\(" "lf\\|LF" "\\|"
2554 "[uU]?[ysnlL]?" "\\)"
2555 "\\|"
2556 "\\_<"
2557 "[0-9]+" "\\("
2558 "[mMQRZING]"
2559 "\\|"
2560 "\\(\\.[0-9]*\\)?"
2561 "\\([eE][-+]?[0-9]+\\)?"
2562 "[fFmM]?"
2563 "\\|"
2564 "[uU]?[ysnlL]?"
2565 "\\)")
2566 '(0 mdw-number-face))
2567
2568 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2569 '(0 mdw-punct-face))))))
2570
2571 (defun mdw-fontify-inferior-fsharp ()
2572 (mdw-fontify-fsharp)
2573 (setq font-lock-keywords
2574 (append (list (list "^[#-]" '(0 font-lock-comment-face))
2575 (list "^>" '(0 font-lock-keyword-face)))
2576 font-lock-keywords)))
2577
2578 ;;;--------------------------------------------------------------------------
2579 ;;; Go programming configuration.
2580
2581 (defun mdw-fontify-go ()
2582
2583 (make-local-variable 'font-lock-keywords)
2584 (let ((go-keywords
2585 (mdw-regexps "break" "case" "chan" "const" "continue"
2586 "default" "defer" "else" "fallthrough" "for"
2587 "func" "go" "goto" "if" "import"
2588 "interface" "map" "package" "range" "return"
2589 "select" "struct" "switch" "type" "var"))
2590 (go-intrinsics
2591 (mdw-regexps "bool" "byte" "complex64" "complex128" "error"
2592 "float32" "float64" "int" "uint8" "int16" "int32"
2593 "int64" "rune" "string" "uint" "uint8" "uint16"
2594 "uint32" "uint64" "uintptr" "void"
2595 "false" "iota" "nil" "true"
2596 "init" "main"
2597 "append" "cap" "copy" "delete" "imag" "len" "make"
2598 "new" "panic" "real" "recover")))
2599
2600 (setq font-lock-keywords
2601 (list
2602
2603 ;; Handle the keywords defined above.
2604 (list (concat "\\<\\(" go-keywords "\\)\\>")
2605 '(0 font-lock-keyword-face))
2606 (list (concat "\\<\\(" go-intrinsics "\\)\\>")
2607 '(0 font-lock-variable-name-face))
2608
2609 ;; Strings and characters.
2610 (list (concat "'"
2611 "\\(" "[^\\']" "\\|"
2612 "\\\\"
2613 "\\(" "[abfnrtv\\'\"]" "\\|"
2614 "[0-7]\\{3\\}" "\\|"
2615 "x" "[0-9A-Fa-f]\\{2\\}" "\\|"
2616 "u" "[0-9A-Fa-f]\\{4\\}" "\\|"
2617 "U" "[0-9A-Fa-f]\\{8\\}" "\\)" "\\)"
2618 "'"
2619 "\\|"
2620 "\""
2621 "\\(" "[^\n\\\"]+" "\\|" "\\\\." "\\)*"
2622 "\\(\"\\|$\\)"
2623 "\\|"
2624 "`" "[^`]+" "`")
2625 '(0 font-lock-string-face))
2626
2627 ;; Handle numbers too.
2628 ;;
2629 ;; The following isn't quite right, but it's close enough.
2630 (list (concat "\\<\\("
2631 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2632 "[0-9]+\\(\\.[0-9]*\\|\\)"
2633 "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)")
2634 '(0 mdw-number-face))
2635
2636 ;; And anything else is punctuation.
2637 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2638 '(0 mdw-punct-face))))))
2639
2640 ;;;--------------------------------------------------------------------------
2641 ;;; Rust programming configuration.
2642
2643 (setq-default rust-indent-offset 2)
2644
2645 (defun mdw-self-insert-and-indent (count)
2646 (interactive "p")
2647 (self-insert-command count)
2648 (indent-according-to-mode))
2649
2650 (defun mdw-fontify-rust ()
2651
2652 ;; Hack syntax categories.
2653 (modify-syntax-entry ?= ".")
2654
2655 ;; Fontify keywords and things.
2656 (make-local-variable 'font-lock-keywords)
2657 (let ((rust-keywords
2658 (mdw-regexps "abstract" "alignof" "as"
2659 "become" "box" "break"
2660 "const" "continue" "create"
2661 "do"
2662 "else" "enum" "extern"
2663 "final" "fn" "for"
2664 "if" "impl" "in"
2665 "let" "loop"
2666 "macro" "match" "mod" "move" "mut"
2667 "offsetof" "override"
2668 "priv" "proc" "pub" "pure"
2669 "ref" "return"
2670 "sizeof" "static" "struct" "super"
2671 "trait" "type" "typeof"
2672 "unsafe" "unsized" "use"
2673 "virtual"
2674 "where" "while"
2675 "yield"))
2676 (rust-builtins
2677 (mdw-regexps "array" "pointer" "slice" "tuple"
2678 "bool" "true" "false"
2679 "f32" "f64"
2680 "i8" "i16" "i32" "i64" "isize"
2681 "u8" "u16" "u32" "u64" "usize"
2682 "char" "str"
2683 "self" "Self")))
2684 (setq font-lock-keywords
2685 (list
2686
2687 ;; Handle the keywords defined above.
2688 (list (concat "\\_<\\(" rust-keywords "\\)\\_>")
2689 '(0 font-lock-keyword-face))
2690 (list (concat "\\_<\\(" rust-builtins "\\)\\_>")
2691 '(0 font-lock-variable-name-face))
2692
2693 ;; Handle numbers too.
2694 (list (concat "\\_<\\("
2695 "[0-9][0-9_]*"
2696 "\\(" "\\(\\.[0-9_]+\\)?[eE][-+]?[0-9_]+"
2697 "\\|" "\\.[0-9_]+"
2698 "\\)"
2699 "\\(f32\\|f64\\)?"
2700 "\\|" "\\(" "[0-9][0-9_]*"
2701 "\\|" "0x[0-9a-fA-F_]+"
2702 "\\|" "0o[0-7_]+"
2703 "\\|" "0b[01_]+"
2704 "\\)"
2705 "\\([ui]\\(8\\|16\\|32\\|64\\|s\\|size\\)\\)?"
2706 "\\)\\_>")
2707 '(0 mdw-number-face))
2708
2709 ;; And anything else is punctuation.
2710 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2711 '(0 mdw-punct-face)))))
2712
2713 ;; Hack key bindings.
2714 (local-set-key [?{] 'mdw-self-insert-and-indent)
2715 (local-set-key [?}] 'mdw-self-insert-and-indent))
2716
2717 ;;;--------------------------------------------------------------------------
2718 ;;; Awk programming configuration.
2719
2720 ;; Make Awk indentation nice.
2721
2722 (mdw-define-c-style mdw-awk
2723 (c-basic-offset . 2)
2724 (c-offsets-alist (substatement-open . 0)
2725 (c-backslash-column . 72)
2726 (statement-cont . 0)
2727 (statement-case-intro . +)))
2728 (mdw-set-default-c-style 'awk-mode 'mdw-awk)
2729
2730 ;; Declare Awk fontification style.
2731
2732 (defun mdw-fontify-awk ()
2733
2734 ;; Miscellaneous fiddling.
2735 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
2736
2737 ;; Now define things to be fontified.
2738 (make-local-variable 'font-lock-keywords)
2739 (let ((c-keywords
2740 (mdw-regexps "BEGIN" "END" "ARGC" "ARGIND" "ARGV" "CONVFMT"
2741 "ENVIRON" "ERRNO" "FIELDWIDTHS" "FILENAME" "FNR"
2742 "FS" "IGNORECASE" "NF" "NR" "OFMT" "OFS" "ORS" "RS"
2743 "RSTART" "RLENGTH" "RT" "SUBSEP"
2744 "atan2" "break" "close" "continue" "cos" "delete"
2745 "do" "else" "exit" "exp" "fflush" "file" "for" "func"
2746 "function" "gensub" "getline" "gsub" "if" "in"
2747 "index" "int" "length" "log" "match" "next" "rand"
2748 "return" "print" "printf" "sin" "split" "sprintf"
2749 "sqrt" "srand" "strftime" "sub" "substr" "system"
2750 "systime" "tolower" "toupper" "while")))
2751
2752 (setq font-lock-keywords
2753 (list
2754
2755 ;; Handle the keywords defined above.
2756 (list (concat "\\<\\(" c-keywords "\\)\\>")
2757 '(0 font-lock-keyword-face))
2758
2759 ;; Handle numbers too.
2760 ;;
2761 ;; The following isn't quite right, but it's close enough.
2762 (list (concat "\\<\\("
2763 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
2764 "[0-9]+\\(\\.[0-9]*\\|\\)"
2765 "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
2766 "[uUlL]*")
2767 '(0 mdw-number-face))
2768
2769 ;; And anything else is punctuation.
2770 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2771 '(0 mdw-punct-face))))))
2772
2773 ;;;--------------------------------------------------------------------------
2774 ;;; Perl programming style.
2775
2776 ;; Perl indentation style.
2777
2778 (setq perl-indent-level 2)
2779
2780 (setq cperl-indent-level 2)
2781 (setq cperl-continued-statement-offset 2)
2782 (setq cperl-continued-brace-offset 0)
2783 (setq cperl-brace-offset -2)
2784 (setq cperl-brace-imaginary-offset 0)
2785 (setq cperl-label-offset 0)
2786
2787 ;; Define perl fontification style.
2788
2789 (defun mdw-fontify-perl ()
2790
2791 ;; Miscellaneous fiddling.
2792 (modify-syntax-entry ?$ "\\")
2793 (modify-syntax-entry ?$ "\\" font-lock-syntax-table)
2794 (modify-syntax-entry ?: "." font-lock-syntax-table)
2795 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
2796
2797 ;; Now define fontification things.
2798 (make-local-variable 'font-lock-keywords)
2799 (let ((perl-keywords
2800 (mdw-regexps "and"
2801 "break"
2802 "cmp" "continue"
2803 "default" "do"
2804 "else" "elsif" "eq"
2805 "for" "foreach"
2806 "ge" "given" "gt" "goto"
2807 "if"
2808 "last" "le" "local" "lt"
2809 "my"
2810 "ne" "next"
2811 "or" "our"
2812 "package"
2813 "redo" "require" "return"
2814 "sub"
2815 "undef" "unless" "until" "use"
2816 "when" "while")))
2817
2818 (setq font-lock-keywords
2819 (list
2820
2821 ;; Set up the keywords defined above.
2822 (list (concat "\\<\\(" perl-keywords "\\)\\>")
2823 '(0 font-lock-keyword-face))
2824
2825 ;; At least numbers are simpler than C.
2826 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
2827 "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)"
2828 "\\([eE]\\([-+]\\|\\)[0-9_]+\\|\\)")
2829 '(0 mdw-number-face))
2830
2831 ;; And anything else is punctuation.
2832 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2833 '(0 mdw-punct-face))))))
2834
2835 (defun perl-number-tests (&optional arg)
2836 "Assign consecutive numbers to lines containing `#t'. With ARG,
2837 strip numbers instead."
2838 (interactive "P")
2839 (save-excursion
2840 (goto-char (point-min))
2841 (let ((i 0) (fmt (if arg "" " %4d")))
2842 (while (search-forward "#t" nil t)
2843 (delete-region (point) (line-end-position))
2844 (setq i (1+ i))
2845 (insert (format fmt i)))
2846 (goto-char (point-min))
2847 (if (re-search-forward "\\(tests\\s-*=>\\s-*\\)\\w*" nil t)
2848 (replace-match (format "\\1%d" i))))))
2849
2850 ;;;--------------------------------------------------------------------------
2851 ;;; Python programming style.
2852
2853 (defun mdw-fontify-pythonic (keywords)
2854
2855 ;; Miscellaneous fiddling.
2856 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
2857 (setq indent-tabs-mode nil)
2858
2859 ;; Now define fontification things.
2860 (make-local-variable 'font-lock-keywords)
2861 (setq font-lock-keywords
2862 (list
2863
2864 ;; Set up the keywords defined above.
2865 (list (concat "\\_<\\(" keywords "\\)\\_>")
2866 '(0 font-lock-keyword-face))
2867
2868 ;; At least numbers are simpler than C.
2869 (list (concat "\\_<0\\([xX][0-9a-fA-F]+\\|[oO]?[0-7]+\\|[bB][01]+\\)\\|"
2870 "\\_<[0-9][0-9]*\\(\\.[0-9]*\\|\\)"
2871 "\\([eE]\\([-+]\\|\\)[0-9]+\\|[lL]\\|\\)")
2872 '(0 mdw-number-face))
2873
2874 ;; And anything else is punctuation.
2875 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2876 '(0 mdw-punct-face)))))
2877
2878 ;; Define Python fontification styles.
2879
2880 (defun mdw-fontify-python ()
2881 (mdw-fontify-pythonic
2882 (mdw-regexps "and" "as" "assert" "break" "class" "continue" "def"
2883 "del" "elif" "else" "except" "exec" "finally" "for"
2884 "from" "global" "if" "import" "in" "is" "lambda"
2885 "not" "or" "pass" "print" "raise" "return" "try"
2886 "while" "with" "yield")))
2887
2888 (defun mdw-fontify-pyrex ()
2889 (mdw-fontify-pythonic
2890 (mdw-regexps "and" "as" "assert" "break" "cdef" "class" "continue"
2891 "ctypedef" "def" "del" "elif" "else" "enum" "except" "exec"
2892 "extern" "finally" "for" "from" "global" "if"
2893 "import" "in" "is" "lambda" "not" "or" "pass" "print"
2894 "property" "raise" "return" "struct" "try" "while" "with"
2895 "yield")))
2896
2897 (define-derived-mode pyrex-mode python-mode "Pyrex"
2898 "Major mode for editing Pyrex source code")
2899 (setq auto-mode-alist
2900 (append '(("\\.pyx$" . pyrex-mode)
2901 ("\\.pxd$" . pyrex-mode)
2902 ("\\.pxi$" . pyrex-mode))
2903 auto-mode-alist))
2904
2905 ;;;--------------------------------------------------------------------------
2906 ;;; Lua programming style.
2907
2908 (setq lua-indent-level 2)
2909
2910 (defun mdw-fontify-lua ()
2911
2912 ;; Miscellaneous fiddling.
2913 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
2914
2915 ;; Now define fontification things.
2916 (make-local-variable 'font-lock-keywords)
2917 (let ((lua-keywords
2918 (mdw-regexps "and" "break" "do" "else" "elseif" "end"
2919 "false" "for" "function" "goto" "if" "in" "local"
2920 "nil" "not" "or" "repeat" "return" "then" "true"
2921 "until" "while")))
2922 (setq font-lock-keywords
2923 (list
2924
2925 ;; Set up the keywords defined above.
2926 (list (concat "\\_<\\(" lua-keywords "\\)\\_>")
2927 '(0 font-lock-keyword-face))
2928
2929 ;; At least numbers are simpler than C.
2930 (list (concat "\\_<\\(" "0[xX]"
2931 "\\(" "[0-9a-fA-F]+"
2932 "\\(\\.[0-9a-fA-F]*\\)?"
2933 "\\|" "\\.[0-9a-fA-F]+"
2934 "\\)"
2935 "\\([pP][-+]?[0-9]+\\)?"
2936 "\\|" "\\(" "[0-9]+"
2937 "\\(\\.[0-9]*\\)?"
2938 "\\|" "\\.[0-9]+"
2939 "\\)"
2940 "\\([eE][-+]?[0-9]+\\)?"
2941 "\\)")
2942 '(0 mdw-number-face))
2943
2944 ;; And anything else is punctuation.
2945 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
2946 '(0 mdw-punct-face))))))
2947
2948 ;;;--------------------------------------------------------------------------
2949 ;;; Icon programming style.
2950
2951 ;; Icon indentation style.
2952
2953 (setq icon-brace-offset 0
2954 icon-continued-brace-offset 0
2955 icon-continued-statement-offset 2
2956 icon-indent-level 2)
2957
2958 ;; Define Icon fontification style.
2959
2960 (defun mdw-fontify-icon ()
2961
2962 ;; Miscellaneous fiddling.
2963 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
2964
2965 ;; Now define fontification things.
2966 (make-local-variable 'font-lock-keywords)
2967 (let ((icon-keywords
2968 (mdw-regexps "break" "by" "case" "create" "default" "do" "else"
2969 "end" "every" "fail" "global" "if" "initial"
2970 "invocable" "link" "local" "next" "not" "of"
2971 "procedure" "record" "repeat" "return" "static"
2972 "suspend" "then" "to" "until" "while"))
2973 (preprocessor-keywords
2974 (mdw-regexps "define" "else" "endif" "error" "ifdef" "ifndef"
2975 "include" "line" "undef")))
2976 (setq font-lock-keywords
2977 (list
2978
2979 ;; Set up the keywords defined above.
2980 (list (concat "\\<\\(" icon-keywords "\\)\\>")
2981 '(0 font-lock-keyword-face))
2982
2983 ;; The things that Icon calls keywords.
2984 (list "&\\sw+\\>" '(0 font-lock-variable-name-face))
2985
2986 ;; At least numbers are simpler than C.
2987 (list (concat "\\<[0-9]+"
2988 "\\([rR][0-9a-zA-Z]+\\|"
2989 "\\.[0-9]+\\([eE][+-]?[0-9]+\\)?\\)\\>\\|"
2990 "\\.[0-9]+\\([eE][+-]?[0-9]+\\)?\\>")
2991 '(0 mdw-number-face))
2992
2993 ;; Preprocessor.
2994 (list (concat "^[ \t]*$[ \t]*\\<\\("
2995 preprocessor-keywords
2996 "\\)\\>")
2997 '(0 font-lock-keyword-face))
2998
2999 ;; And anything else is punctuation.
3000 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3001 '(0 mdw-punct-face))))))
3002
3003 ;;;--------------------------------------------------------------------------
3004 ;;; Assembler mode.
3005
3006 (defun mdw-fontify-asm ()
3007 (modify-syntax-entry ?' "\"")
3008 (modify-syntax-entry ?. "w")
3009 (modify-syntax-entry ?\n ">")
3010 (setf fill-prefix nil)
3011 (modify-syntax-entry ?. "_")
3012 (modify-syntax-entry ?* ". 23")
3013 (modify-syntax-entry ?/ ". 124b")
3014 (modify-syntax-entry ?\n "> b")
3015 (local-set-key ";" 'self-insert-command)
3016 (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)"))
3017
3018 (defun mdw-asm-set-comment ()
3019 (modify-syntax-entry ?; "."
3020 )
3021 (modify-syntax-entry asm-comment-char "< b")
3022 (setq comment-start (string asm-comment-char ? )))
3023 (add-hook 'asm-mode-local-variables-hook 'mdw-asm-set-comment)
3024 (put 'asm-comment-char 'safe-local-variable 'characterp)
3025
3026 ;;;--------------------------------------------------------------------------
3027 ;;; TCL configuration.
3028
3029 (defun mdw-fontify-tcl ()
3030 (mapcar #'(lambda (ch) (modify-syntax-entry ch ".")) '(?$))
3031 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
3032 (make-local-variable 'font-lock-keywords)
3033 (setq font-lock-keywords
3034 (list
3035 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
3036 "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)"
3037 "\\([eE]\\([-+]\\|\\)[0-9_]+\\|\\)")
3038 '(0 mdw-number-face))
3039 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3040 '(0 mdw-punct-face)))))
3041
3042 ;;;--------------------------------------------------------------------------
3043 ;;; Dylan programming configuration.
3044
3045 (defun mdw-fontify-dylan ()
3046
3047 (make-local-variable 'font-lock-keywords)
3048
3049 ;; Horrors. `dylan-mode' sets the `major-mode' name after calling this
3050 ;; hook, which undoes all of our configuration.
3051 (setq major-mode 'dylan-mode)
3052 (font-lock-set-defaults)
3053
3054 (let* ((word "[-_a-zA-Z!*@<>$%]+")
3055 (dylan-keywords (mdw-regexps
3056
3057 "C-address" "C-callable-wrapper" "C-function"
3058 "C-mapped-subtype" "C-pointer-type" "C-struct"
3059 "C-subtype" "C-union" "C-variable"
3060
3061 "above" "abstract" "afterwards" "all"
3062 "begin" "below" "block" "by"
3063 "case" "class" "cleanup" "constant" "create"
3064 "define" "domain"
3065 "else" "elseif" "end" "exception" "export"
3066 "finally" "for" "from" "function"
3067 "generic"
3068 "handler"
3069 "if" "in" "instance" "interface" "iterate"
3070 "keyed-by"
3071 "let" "library" "local"
3072 "macro" "method" "module"
3073 "otherwise"
3074 "profiling"
3075 "select" "slot" "subclass"
3076 "table" "then" "to"
3077 "unless" "until" "use"
3078 "variable" "virtual"
3079 "when" "while"))
3080 (sharp-keywords (mdw-regexps
3081 "all-keys" "key" "next" "rest" "include"
3082 "t" "f")))
3083 (setq font-lock-keywords
3084 (list (list (concat "\\<\\(" dylan-keywords
3085 "\\|" "with\\(out\\)?-" word
3086 "\\)\\>")
3087 '(0 font-lock-keyword-face))
3088 (list (concat "\\<" word ":" "\\|"
3089 "#\\(" sharp-keywords "\\)\\>")
3090 '(0 font-lock-variable-name-face))
3091 (list (concat "\\("
3092 "\\([-+]\\|\\<\\)[0-9]+" "\\("
3093 "\\(\\.[0-9]+\\)?" "\\([eE][-+][0-9]+\\)?"
3094 "\\|" "/[0-9]+"
3095 "\\)"
3096 "\\|" "\\.[0-9]+" "\\([eE][-+][0-9]+\\)?"
3097 "\\|" "#b[01]+"
3098 "\\|" "#o[0-7]+"
3099 "\\|" "#x[0-9a-zA-Z]+"
3100 "\\)\\>")
3101 '(0 mdw-number-face))
3102 (list (concat "\\("
3103 "\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\|"
3104 "\\_<[-+*/=<>:&|]+\\_>"
3105 "\\)")
3106 '(0 mdw-punct-face))))))
3107
3108 ;;;--------------------------------------------------------------------------
3109 ;;; Algol 68 configuration.
3110
3111 (setq a68-indent-step 2)
3112
3113 (defun mdw-fontify-algol-68 ()
3114
3115 ;; Fix up the syntax table.
3116 (modify-syntax-entry ?# "!" a68-mode-syntax-table)
3117 (dolist (ch '(?- ?+ ?= ?< ?> ?* ?/ ?| ?&))
3118 (modify-syntax-entry ch "." a68-mode-syntax-table))
3119
3120 (make-local-variable 'font-lock-keywords)
3121
3122 (let ((not-comment
3123 (let ((word "COMMENT"))
3124 (do ((regexp (concat "[^" (substring word 0 1) "]+")
3125 (concat regexp "\\|"
3126 (substring word 0 i)
3127 "[^" (substring word i (1+ i)) "]"))
3128 (i 1 (1+ i)))
3129 ((>= i (length word)) regexp)))))
3130 (setq font-lock-keywords
3131 (list (list (concat "\\<COMMENT\\>"
3132 "\\(" not-comment "\\)\\{0,5\\}"
3133 "\\(\\'\\|\\<COMMENT\\>\\)")
3134 '(0 font-lock-comment-face))
3135 (list (concat "\\<CO\\>"
3136 "\\([^C]+\\|C[^O]\\)\\{0,5\\}"
3137 "\\($\\|\\<CO\\>\\)")
3138 '(0 font-lock-comment-face))
3139 (list "\\<[A-Z_]+\\>"
3140 '(0 font-lock-keyword-face))
3141 (list (concat "\\<"
3142 "[0-9]+"
3143 "\\(\\.[0-9]+\\)?"
3144 "\\([eE][-+]?[0-9]+\\)?"
3145 "\\>")
3146 '(0 mdw-number-face))
3147 (list "\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/"
3148 '(0 mdw-punct-face))))))
3149
3150 ;;;--------------------------------------------------------------------------
3151 ;;; REXX configuration.
3152
3153 (defun mdw-rexx-electric-* ()
3154 (interactive)
3155 (insert ?*)
3156 (rexx-indent-line))
3157
3158 (defun mdw-rexx-indent-newline-indent ()
3159 (interactive)
3160 (rexx-indent-line)
3161 (if abbrev-mode (expand-abbrev))
3162 (newline-and-indent))
3163
3164 (defun mdw-fontify-rexx ()
3165
3166 ;; Various bits of fiddling.
3167 (setq mdw-auto-indent nil)
3168 (local-set-key [?\C-m] 'mdw-rexx-indent-newline-indent)
3169 (local-set-key [?*] 'mdw-rexx-electric-*)
3170 (mapcar #'(lambda (ch) (modify-syntax-entry ch "w"))
3171 '(?! ?? ?# ?@ ?$))
3172 (mdw-standard-fill-prefix "\\([ \t]*/?\*[ \t]*\\)")
3173
3174 ;; Set up keywords and things for fontification.
3175 (make-local-variable 'font-lock-keywords-case-fold-search)
3176 (setq font-lock-keywords-case-fold-search t)
3177
3178 (setq rexx-indent 2)
3179 (setq rexx-end-indent rexx-indent)
3180 (setq rexx-cont-indent rexx-indent)
3181
3182 (make-local-variable 'font-lock-keywords)
3183 (let ((rexx-keywords
3184 (mdw-regexps "address" "arg" "by" "call" "digits" "do" "drop"
3185 "else" "end" "engineering" "exit" "expose" "for"
3186 "forever" "form" "fuzz" "if" "interpret" "iterate"
3187 "leave" "linein" "name" "nop" "numeric" "off" "on"
3188 "options" "otherwise" "parse" "procedure" "pull"
3189 "push" "queue" "return" "say" "select" "signal"
3190 "scientific" "source" "then" "trace" "to" "until"
3191 "upper" "value" "var" "version" "when" "while"
3192 "with"
3193
3194 "abbrev" "abs" "bitand" "bitor" "bitxor" "b2x"
3195 "center" "center" "charin" "charout" "chars"
3196 "compare" "condition" "copies" "c2d" "c2x"
3197 "datatype" "date" "delstr" "delword" "d2c" "d2x"
3198 "errortext" "format" "fuzz" "insert" "lastpos"
3199 "left" "length" "lineout" "lines" "max" "min"
3200 "overlay" "pos" "queued" "random" "reverse" "right"
3201 "sign" "sourceline" "space" "stream" "strip"
3202 "substr" "subword" "symbol" "time" "translate"
3203 "trunc" "value" "verify" "word" "wordindex"
3204 "wordlength" "wordpos" "words" "xrange" "x2b" "x2c"
3205 "x2d")))
3206
3207 (setq font-lock-keywords
3208 (list
3209
3210 ;; Set up the keywords defined above.
3211 (list (concat "\\<\\(" rexx-keywords "\\)\\>")
3212 '(0 font-lock-keyword-face))
3213
3214 ;; Fontify all symbols the same way.
3215 (list (concat "\\<\\([0-9.][A-Za-z0-9.!?_#@$]*[Ee][+-]?[0-9]+\\|"
3216 "[A-Za-z0-9.!?_#@$]+\\)")
3217 '(0 font-lock-variable-name-face))
3218
3219 ;; And everything else is punctuation.
3220 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3221 '(0 mdw-punct-face))))))
3222
3223 ;;;--------------------------------------------------------------------------
3224 ;;; Standard ML programming style.
3225
3226 (defun mdw-fontify-sml ()
3227
3228 ;; Make underscore an honorary letter.
3229 (modify-syntax-entry ?' "w")
3230
3231 ;; Set fill prefix.
3232 (mdw-standard-fill-prefix "\\([ \t]*(\*[ \t]*\\)")
3233
3234 ;; Now define fontification things.
3235 (make-local-variable 'font-lock-keywords)
3236 (let ((sml-keywords
3237 (mdw-regexps "abstype" "and" "andalso" "as"
3238 "case"
3239 "datatype" "do"
3240 "else" "end" "eqtype" "exception"
3241 "fn" "fun" "functor"
3242 "handle"
3243 "if" "in" "include" "infix" "infixr"
3244 "let" "local"
3245 "nonfix"
3246 "of" "op" "open" "orelse"
3247 "raise" "rec"
3248 "sharing" "sig" "signature" "struct" "structure"
3249 "then" "type"
3250 "val"
3251 "where" "while" "with" "withtype")))
3252
3253 (setq font-lock-keywords
3254 (list
3255
3256 ;; Set up the keywords defined above.
3257 (list (concat "\\<\\(" sml-keywords "\\)\\>")
3258 '(0 font-lock-keyword-face))
3259
3260 ;; At least numbers are simpler than C.
3261 (list (concat "\\<\\(\\~\\|\\)"
3262 "\\(0\\(\\([wW]\\|\\)[xX][0-9a-fA-F]+\\|"
3263 "[wW][0-9]+\\)\\|"
3264 "\\([0-9]+\\(\\.[0-9]+\\|\\)"
3265 "\\([eE]\\(\\~\\|\\)"
3266 "[0-9]+\\|\\)\\)\\)")
3267 '(0 mdw-number-face))
3268
3269 ;; And anything else is punctuation.
3270 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3271 '(0 mdw-punct-face))))))
3272
3273 ;;;--------------------------------------------------------------------------
3274 ;;; Haskell configuration.
3275
3276 (defun mdw-fontify-haskell ()
3277
3278 ;; Fiddle with syntax table to get comments right.
3279 (modify-syntax-entry ?' "_")
3280 (modify-syntax-entry ?- ". 12")
3281 (modify-syntax-entry ?\n ">")
3282
3283 ;; Make punctuation be punctuation
3284 (let ((punct "=<>+-*/|&%!@?$.^:#`"))
3285 (do ((i 0 (1+ i)))
3286 ((>= i (length punct)))
3287 (modify-syntax-entry (aref punct i) ".")))
3288
3289 ;; Set fill prefix.
3290 (mdw-standard-fill-prefix "\\([ \t]*{?--?[ \t]*\\)")
3291
3292 ;; Fiddle with fontification.
3293 (make-local-variable 'font-lock-keywords)
3294 (let ((haskell-keywords
3295 (mdw-regexps "as"
3296 "case" "ccall" "class"
3297 "data" "default" "deriving" "do"
3298 "else" "exists"
3299 "forall" "foreign"
3300 "hiding"
3301 "if" "import" "in" "infix" "infixl" "infixr" "instance"
3302 "let"
3303 "mdo" "module"
3304 "newtype"
3305 "of"
3306 "proc"
3307 "qualified"
3308 "rec"
3309 "safe" "stdcall"
3310 "then" "type"
3311 "unsafe"
3312 "where"))
3313 (control-sequences
3314 (mdw-regexps "ACK" "BEL" "BS" "CAN" "CR" "DC1" "DC2" "DC3" "DC4"
3315 "DEL" "DLE" "EM" "ENQ" "EOT" "ESC" "ETB" "ETX" "FF"
3316 "FS" "GS" "HT" "LF" "NAK" "NUL" "RS" "SI" "SO" "SOH"
3317 "SP" "STX" "SUB" "SYN" "US" "VT")))
3318
3319 (setq font-lock-keywords
3320 (list
3321 (list (concat "{-" "[^-]*" "\\(-+[^-}][^-]*\\)*"
3322 "\\(-+}\\|-*\\'\\)"
3323 "\\|"
3324 "--.*$")
3325 '(0 font-lock-comment-face))
3326 (list (concat "\\_<\\(" haskell-keywords "\\)\\_>")
3327 '(0 font-lock-keyword-face))
3328 (list (concat "'\\("
3329 "[^\\]"
3330 "\\|"
3331 "\\\\"
3332 "\\(" "[abfnrtv\\\"']" "\\|"
3333 "^" "\\(" control-sequences "\\|"
3334 "[]A-Z@[\\^_]" "\\)" "\\|"
3335 "\\|"
3336 "[0-9]+" "\\|"
3337 "[oO][0-7]+" "\\|"
3338 "[xX][0-9A-Fa-f]+"
3339 "\\)"
3340 "\\)'")
3341 '(0 font-lock-string-face))
3342 (list "\\_<[A-Z]\\(\\sw+\\|\\s_+\\)*\\_>"
3343 '(0 font-lock-variable-name-face))
3344 (list (concat "\\_<0\\([xX][0-9a-fA-F]+\\|[oO][0-7]+\\)\\|"
3345 "\\_<[0-9]+\\(\\.[0-9]*\\|\\)"
3346 "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)")
3347 '(0 mdw-number-face))
3348 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3349 '(0 mdw-punct-face))))))
3350
3351 ;;;--------------------------------------------------------------------------
3352 ;;; Erlang configuration.
3353
3354 (setq erlang-electric-commands nil)
3355
3356 (defun mdw-fontify-erlang ()
3357
3358 ;; Set fill prefix.
3359 (mdw-standard-fill-prefix "\\([ \t]*{?%*[ \t]*\\)")
3360
3361 ;; Fiddle with fontification.
3362 (make-local-variable 'font-lock-keywords)
3363 (let ((erlang-keywords
3364 (mdw-regexps "after" "and" "andalso"
3365 "band" "begin" "bnot" "bor" "bsl" "bsr" "bxor"
3366 "case" "catch" "cond"
3367 "div" "end" "fun" "if" "let" "not"
3368 "of" "or" "orelse"
3369 "query" "receive" "rem" "try" "when" "xor")))
3370
3371 (setq font-lock-keywords
3372 (list
3373 (list "%.*$"
3374 '(0 font-lock-comment-face))
3375 (list (concat "\\<\\(" erlang-keywords "\\)\\>")
3376 '(0 font-lock-keyword-face))
3377 (list (concat "^-\\sw+\\>")
3378 '(0 font-lock-keyword-face))
3379 (list "\\<[0-9]+\\(\\|#[0-9a-zA-Z]+\\|[eE][+-]?[0-9]+\\)\\>"
3380 '(0 mdw-number-face))
3381 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3382 '(0 mdw-punct-face))))))
3383
3384 ;;;--------------------------------------------------------------------------
3385 ;;; Texinfo configuration.
3386
3387 (defun mdw-fontify-texinfo ()
3388
3389 ;; Set fill prefix.
3390 (mdw-standard-fill-prefix "\\([ \t]*@c[ \t]+\\)")
3391
3392 ;; Real fontification things.
3393 (make-local-variable 'font-lock-keywords)
3394 (setq font-lock-keywords
3395 (list
3396
3397 ;; Environment names are keywords.
3398 (list "@\\(end\\) *\\([a-zA-Z]*\\)?"
3399 '(2 font-lock-keyword-face))
3400
3401 ;; Unmark escaped magic characters.
3402 (list "\\(@\\)\\([@{}]\\)"
3403 '(1 font-lock-keyword-face)
3404 '(2 font-lock-variable-name-face))
3405
3406 ;; Make sure we get comments properly.
3407 (list "@c\\(\\|omment\\)\\( .*\\)?$"
3408 '(0 font-lock-comment-face))
3409
3410 ;; Command names are keywords.
3411 (list "@\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)"
3412 '(0 font-lock-keyword-face))
3413
3414 ;; Fontify TeX special characters as punctuation.
3415 (list "[{}]+"
3416 '(0 mdw-punct-face)))))
3417
3418 ;;;--------------------------------------------------------------------------
3419 ;;; TeX and LaTeX configuration.
3420
3421 (defun mdw-fontify-tex ()
3422 (setq ispell-parser 'tex)
3423 (turn-on-reftex)
3424
3425 ;; Don't make maths into a string.
3426 (modify-syntax-entry ?$ ".")
3427 (modify-syntax-entry ?$ "." font-lock-syntax-table)
3428 (local-set-key [?$] 'self-insert-command)
3429
3430 ;; Make `tab' be useful, given that tab stops in TeX don't work well.
3431 (local-set-key "\C-\M-i" 'indent-relative)
3432 (setq indent-tabs-mode nil)
3433
3434 ;; Set fill prefix.
3435 (mdw-standard-fill-prefix "\\([ \t]*%+[ \t]*\\)")
3436
3437 ;; Real fontification things.
3438 (make-local-variable 'font-lock-keywords)
3439 (setq font-lock-keywords
3440 (list
3441
3442 ;; Environment names are keywords.
3443 (list (concat "\\\\\\(begin\\|end\\|newenvironment\\)"
3444 "{\\([^}\n]*\\)}")
3445 '(2 font-lock-keyword-face))
3446
3447 ;; Suspended environment names are keywords too.
3448 (list (concat "\\\\\\(suspend\\|resume\\)\\(\\[[^]]*\\]\\)?"
3449 "{\\([^}\n]*\\)}")
3450 '(3 font-lock-keyword-face))
3451
3452 ;; Command names are keywords.
3453 (list "\\\\\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)"
3454 '(0 font-lock-keyword-face))
3455
3456 ;; Handle @/.../ for italics.
3457 ;; (list "\\(@/\\)\\([^/]*\\)\\(/\\)"
3458 ;; '(1 font-lock-keyword-face)
3459 ;; '(3 font-lock-keyword-face))
3460
3461 ;; Handle @*...* for boldness.
3462 ;; (list "\\(@\\*\\)\\([^*]*\\)\\(\\*\\)"
3463 ;; '(1 font-lock-keyword-face)
3464 ;; '(3 font-lock-keyword-face))
3465
3466 ;; Handle @`...' for literal syntax things.
3467 ;; (list "\\(@`\\)\\([^']*\\)\\('\\)"
3468 ;; '(1 font-lock-keyword-face)
3469 ;; '(3 font-lock-keyword-face))
3470
3471 ;; Handle @<...> for nonterminals.
3472 ;; (list "\\(@<\\)\\([^>]*\\)\\(>\\)"
3473 ;; '(1 font-lock-keyword-face)
3474 ;; '(3 font-lock-keyword-face))
3475
3476 ;; Handle other @-commands.
3477 ;; (list "@\\([^a-zA-Z]\\|[a-zA-Z]*\\)"
3478 ;; '(0 font-lock-keyword-face))
3479
3480 ;; Make sure we get comments properly.
3481 (list "%.*"
3482 '(0 font-lock-comment-face))
3483
3484 ;; Fontify TeX special characters as punctuation.
3485 (list "[$^_{}#&]"
3486 '(0 mdw-punct-face)))))
3487
3488 (setq TeX-install-font-lock 'tex-font-setup)
3489
3490 (eval-after-load 'font-latex
3491 '(defun font-latex-jit-lock-force-redisplay (buf start end)
3492 "Compatibility for Emacsen not offering `jit-lock-force-redisplay'."
3493 ;; The following block is an expansion of `jit-lock-force-redisplay'
3494 ;; and involved macros taken from CVS Emacs on 2007-04-28.
3495 (with-current-buffer buf
3496 (let ((modified (buffer-modified-p)))
3497 (unwind-protect
3498 (let ((buffer-undo-list t)
3499 (inhibit-read-only t)
3500 (inhibit-point-motion-hooks t)
3501 (inhibit-modification-hooks t)
3502 deactivate-mark
3503 buffer-file-name
3504 buffer-file-truename)
3505 (put-text-property start end 'fontified t))
3506 (unless modified
3507 (restore-buffer-modified-p nil)))))))
3508
3509 (setq LaTeX-syntactic-comments nil
3510 LaTeX-fill-break-at-separators '(\\\[))
3511
3512 ;;;--------------------------------------------------------------------------
3513 ;;; HTML, CSS, and other web foolishness.
3514
3515 (setq css-indent-offset 2)
3516
3517 ;;;--------------------------------------------------------------------------
3518 ;;; SGML hacking.
3519
3520 (defun mdw-sgml-mode ()
3521 (interactive)
3522 (sgml-mode)
3523 (mdw-standard-fill-prefix "")
3524 (make-local-variable 'sgml-delimiters)
3525 (setq sgml-delimiters
3526 '("AND" "&" "COM" "--" "CRO" "&#" "DSC" "]" "DSO" "[" "DTGC" "]"
3527 "DTGO" "[" "ERO" "&" "ETAGO" ":e" "GRPC" ")" "GRPO" "(" "LIT" "\""
3528 "LITA" "'" "MDC" ">" "MDO" "<!" "MINUS" "-" "MSC" "]]" "NESTC" "{"
3529 "NET" "}" "OPT" "?" "OR" "|" "PERO" "%" "PIC" ">" "PIO" "<?"
3530 "PLUS" "+" "REFC" "." "REP" "*" "RNI" "#" "SEQ" "," "STAGO" ":"
3531 "TAGC" "." "VI" "=" "MS-START" "<![" "MS-END" "]]>"
3532 "XML-ECOM" "-->" "XML-PIC" "?>" "XML-SCOM" "<!--" "XML-TAGCE" "/>"
3533 "NULL" ""))
3534 (setq major-mode 'mdw-sgml-mode)
3535 (setq mode-name "[mdw] SGML")
3536 (run-hooks 'mdw-sgml-mode-hook))
3537
3538 ;;;--------------------------------------------------------------------------
3539 ;;; Configuration files.
3540
3541 (defvar mdw-conf-quote-normal nil
3542 "*Control syntax category of quote characters `\"' and `''.
3543 If this is `t', consider quote characters to be normal
3544 punctuation, as for `conf-quote-normal'. If this is `nil' then
3545 leave quote characters as quotes. If this is a list, then
3546 consider the quote characters in the list to be normal
3547 punctuation. If this is a single quote character, then consider
3548 that character only to be normal punctuation.")
3549 (defun mdw-conf-quote-normal-acceptable-value-p (value)
3550 "Is the VALUE is an acceptable value for `mdw-conf-quote-normal'?"
3551 (or (booleanp value)
3552 (every (lambda (v) (memq v '(?\" ?')))
3553 (if (listp value) value (list value)))))
3554 (put 'mdw-conf-quote-normal 'safe-local-variable
3555 'mdw-conf-quote-normal-acceptable-value-p)
3556
3557 (defun mdw-fix-up-quote ()
3558 "Apply the setting of `mdw-conf-quote-normal'."
3559 (let ((flag mdw-conf-quote-normal))
3560 (cond ((eq flag t)
3561 (conf-quote-normal t))
3562 ((not flag)
3563 nil)
3564 (t
3565 (let ((table (copy-syntax-table (syntax-table))))
3566 (mapc (lambda (ch) (modify-syntax-entry ch "." table))
3567 (if (listp flag) flag (list flag)))
3568 (set-syntax-table table)
3569 (and font-lock-mode (font-lock-fontify-buffer)))))))
3570 (add-hook 'conf-mode-local-variables-hook 'mdw-fix-up-quote t t)
3571
3572 ;;;--------------------------------------------------------------------------
3573 ;;; Shell scripts.
3574
3575 (defun mdw-setup-sh-script-mode ()
3576
3577 ;; Fetch the shell interpreter's name.
3578 (let ((shell-name sh-shell-file))
3579
3580 ;; Try reading the hash-bang line.
3581 (save-excursion
3582 (goto-char (point-min))
3583 (if (looking-at "#![ \t]*\\([^ \t\n]*\\)")
3584 (setq shell-name (match-string 1))))
3585
3586 ;; Now try to set the shell.
3587 ;;
3588 ;; Don't let `sh-set-shell' bugger up my script.
3589 (let ((executable-set-magic #'(lambda (s &rest r) s)))
3590 (sh-set-shell shell-name)))
3591
3592 ;; Don't insert here-document scaffolding automatically.
3593 (local-set-key "<" 'self-insert-command)
3594
3595 ;; Now enable my keys and the fontification.
3596 (mdw-misc-mode-config)
3597
3598 ;; Set the indentation level correctly.
3599 (setq sh-indentation 2)
3600 (setq sh-basic-offset 2))
3601
3602 (setq sh-shell-file "/bin/sh")
3603
3604 ;; Awful hacking to override the shell detection for particular scripts.
3605 (defmacro define-custom-shell-mode (name shell)
3606 `(defun ,name ()
3607 (interactive)
3608 (set (make-local-variable 'sh-shell-file) ,shell)
3609 (sh-mode)))
3610 (define-custom-shell-mode bash-mode "/bin/bash")
3611 (define-custom-shell-mode rc-mode "/usr/bin/rc")
3612 (put 'sh-shell-file 'permanent-local t)
3613
3614 ;; Hack the rc syntax table. Backquotes aren't paired in rc.
3615 (eval-after-load "sh-script"
3616 '(or (assq 'rc sh-mode-syntax-table-input)
3617 (let ((frag '(nil
3618 ?# "<"
3619 ?\n ">#"
3620 ?\" "\"\""
3621 ?\' "\"\'"
3622 ?$ "'"
3623 ?\` "."
3624 ?! "_"
3625 ?% "_"
3626 ?. "_"
3627 ?^ "_"
3628 ?~ "_"
3629 ?, "_"
3630 ?= "."
3631 ?< "."
3632 ?> "."))
3633 (assoc (assq 'rc sh-mode-syntax-table-input)))
3634 (if assoc
3635 (rplacd assoc frag)
3636 (setq sh-mode-syntax-table-input
3637 (cons (cons 'rc frag)
3638 sh-mode-syntax-table-input))))))
3639
3640 ;;;--------------------------------------------------------------------------
3641 ;;; Emacs shell mode.
3642
3643 (defun mdw-eshell-prompt ()
3644 (let ((left "[") (right "]"))
3645 (when (= (user-uid) 0)
3646 (setq left "«" right "»"))
3647 (concat left
3648 (save-match-data
3649 (replace-regexp-in-string "\\..*$" "" (system-name)))
3650 " "
3651 (let* ((pwd (eshell/pwd)) (npwd (length pwd))
3652 (home (expand-file-name "~")) (nhome (length home)))
3653 (if (and (>= npwd nhome)
3654 (or (= nhome npwd)
3655 (= (elt pwd nhome) ?/))
3656 (string= (substring pwd 0 nhome) home))
3657 (concat "~" (substring pwd (length home)))
3658 pwd))
3659 right)))
3660 (setq eshell-prompt-function 'mdw-eshell-prompt)
3661 (setq eshell-prompt-regexp "^\\[[^]>]+\\(\\]\\|>>?\\)")
3662
3663 (defun eshell/e (file) (find-file file) nil)
3664 (defun eshell/ee (file) (find-file-other-window file) nil)
3665 (defun eshell/w3m (url) (w3m-goto-url url) nil)
3666
3667 (mdw-define-face eshell-prompt (t :weight bold))
3668 (mdw-define-face eshell-ls-archive (t :weight bold :foreground "red"))
3669 (mdw-define-face eshell-ls-backup (t :foreground "lightgrey" :slant italic))
3670 (mdw-define-face eshell-ls-product (t :foreground "lightgrey" :slant italic))
3671 (mdw-define-face eshell-ls-clutter (t :foreground "lightgrey" :slant italic))
3672 (mdw-define-face eshell-ls-executable (t :weight bold))
3673 (mdw-define-face eshell-ls-directory (t :foreground "cyan" :weight bold))
3674 (mdw-define-face eshell-ls-readonly (t nil))
3675 (mdw-define-face eshell-ls-symlink (t :foreground "cyan"))
3676
3677 (defun mdw-eshell-hack () (setenv "LD_PRELOAD" nil))
3678 (add-hook 'eshell-mode-hook 'mdw-eshell-hack)
3679
3680 ;;;--------------------------------------------------------------------------
3681 ;;; Messages-file mode.
3682
3683 (defun messages-mode-guts ()
3684 (setq messages-mode-syntax-table (make-syntax-table))
3685 (set-syntax-table messages-mode-syntax-table)
3686 (modify-syntax-entry ?0 "w" messages-mode-syntax-table)
3687 (modify-syntax-entry ?1 "w" messages-mode-syntax-table)
3688 (modify-syntax-entry ?2 "w" messages-mode-syntax-table)
3689 (modify-syntax-entry ?3 "w" messages-mode-syntax-table)
3690 (modify-syntax-entry ?4 "w" messages-mode-syntax-table)
3691 (modify-syntax-entry ?5 "w" messages-mode-syntax-table)
3692 (modify-syntax-entry ?6 "w" messages-mode-syntax-table)
3693 (modify-syntax-entry ?7 "w" messages-mode-syntax-table)
3694 (modify-syntax-entry ?8 "w" messages-mode-syntax-table)
3695 (modify-syntax-entry ?9 "w" messages-mode-syntax-table)
3696 (make-local-variable 'comment-start)
3697 (make-local-variable 'comment-end)
3698 (make-local-variable 'indent-line-function)
3699 (setq indent-line-function 'indent-relative)
3700 (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
3701 (make-local-variable 'font-lock-defaults)
3702 (make-local-variable 'messages-mode-keywords)
3703 (let ((keywords
3704 (mdw-regexps "array" "bitmap" "callback" "docs[ \t]+enum"
3705 "export" "enum" "fixed-octetstring" "flags"
3706 "harmless" "map" "nested" "optional"
3707 "optional-tagged" "package" "primitive"
3708 "primitive-nullfree" "relaxed[ \t]+enum"
3709 "set" "table" "tagged-optional" "union"
3710 "variadic" "vector" "version" "version-tag")))
3711 (setq messages-mode-keywords
3712 (list
3713 (list (concat "\\<\\(" keywords "\\)\\>:")
3714 '(0 font-lock-keyword-face))
3715 '("\\([-a-zA-Z0-9]+:\\)" (0 font-lock-warning-face))
3716 '("\\(\\<[a-z][-_a-zA-Z0-9]*\\)"
3717 (0 font-lock-variable-name-face))
3718 '("\\<\\([0-9]+\\)\\>" (0 mdw-number-face))
3719 '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3720 (0 mdw-punct-face)))))
3721 (setq font-lock-defaults
3722 '(messages-mode-keywords nil nil nil nil))
3723 (run-hooks 'messages-file-hook))
3724
3725 (defun messages-mode ()
3726 (interactive)
3727 (fundamental-mode)
3728 (setq major-mode 'messages-mode)
3729 (setq mode-name "Messages")
3730 (messages-mode-guts)
3731 (modify-syntax-entry ?# "<" messages-mode-syntax-table)
3732 (modify-syntax-entry ?\n ">" messages-mode-syntax-table)
3733 (setq comment-start "# ")
3734 (setq comment-end "")
3735 (run-hooks 'messages-mode-hook))
3736
3737 (defun cpp-messages-mode ()
3738 (interactive)
3739 (fundamental-mode)
3740 (setq major-mode 'cpp-messages-mode)
3741 (setq mode-name "CPP Messages")
3742 (messages-mode-guts)
3743 (modify-syntax-entry ?* ". 23" messages-mode-syntax-table)
3744 (modify-syntax-entry ?/ ". 14" messages-mode-syntax-table)
3745 (setq comment-start "/* ")
3746 (setq comment-end " */")
3747 (let ((preprocessor-keywords
3748 (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
3749 "ident" "if" "ifdef" "ifndef" "import" "include"
3750 "line" "pragma" "unassert" "undef" "warning")))
3751 (setq messages-mode-keywords
3752 (append (list (list (concat "^[ \t]*\\#[ \t]*"
3753 "\\(include\\|import\\)"
3754 "[ \t]*\\(<[^>]+\\(>\\|\\)\\)")
3755 '(2 font-lock-string-face))
3756 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
3757 preprocessor-keywords
3758 "\\)\\>\\|[0-9]+\\|$\\)\\)")
3759 '(1 font-lock-keyword-face)))
3760 messages-mode-keywords)))
3761 (run-hooks 'cpp-messages-mode-hook))
3762
3763 (add-hook 'messages-mode-hook 'mdw-misc-mode-config t)
3764 (add-hook 'cpp-messages-mode-hook 'mdw-misc-mode-config t)
3765 ; (add-hook 'messages-file-hook 'mdw-fontify-messages t)
3766
3767 ;;;--------------------------------------------------------------------------
3768 ;;; Messages-file mode.
3769
3770 (defvar mallow-driver-substitution-face 'mallow-driver-substitution-face
3771 "Face to use for subsittution directives.")
3772 (make-face 'mallow-driver-substitution-face)
3773 (defvar mallow-driver-text-face 'mallow-driver-text-face
3774 "Face to use for body text.")
3775 (make-face 'mallow-driver-text-face)
3776
3777 (defun mallow-driver-mode ()
3778 (interactive)
3779 (fundamental-mode)
3780 (setq major-mode 'mallow-driver-mode)
3781 (setq mode-name "Mallow driver")
3782 (setq mallow-driver-mode-syntax-table (make-syntax-table))
3783 (set-syntax-table mallow-driver-mode-syntax-table)
3784 (make-local-variable 'comment-start)
3785 (make-local-variable 'comment-end)
3786 (make-local-variable 'indent-line-function)
3787 (setq indent-line-function 'indent-relative)
3788 (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
3789 (make-local-variable 'font-lock-defaults)
3790 (make-local-variable 'mallow-driver-mode-keywords)
3791 (let ((keywords
3792 (mdw-regexps "each" "divert" "file" "if"
3793 "perl" "set" "string" "type" "write")))
3794 (setq mallow-driver-mode-keywords
3795 (list
3796 (list (concat "^%\\s *\\(}\\|\\(" keywords "\\)\\>\\).*$")
3797 '(0 font-lock-keyword-face))
3798 (list "^%\\s *\\(#.*\\|\\)$"
3799 '(0 font-lock-comment-face))
3800 (list "^%"
3801 '(0 font-lock-keyword-face))
3802 (list "^|?\\(.+\\)$" '(1 mallow-driver-text-face))
3803 (list "\\${[^}]*}"
3804 '(0 mallow-driver-substitution-face t)))))
3805 (setq font-lock-defaults
3806 '(mallow-driver-mode-keywords nil nil nil nil))
3807 (modify-syntax-entry ?\" "_" mallow-driver-mode-syntax-table)
3808 (modify-syntax-entry ?\n ">" mallow-driver-mode-syntax-table)
3809 (setq comment-start "%# ")
3810 (setq comment-end "")
3811 (run-hooks 'mallow-driver-mode-hook))
3812
3813 (add-hook 'mallow-driver-hook 'mdw-misc-mode-config t)
3814
3815 ;;;--------------------------------------------------------------------------
3816 ;;; NFast debugs.
3817
3818 (defun nfast-debug-mode ()
3819 (interactive)
3820 (fundamental-mode)
3821 (setq major-mode 'nfast-debug-mode)
3822 (setq mode-name "NFast debug")
3823 (setq messages-mode-syntax-table (make-syntax-table))
3824 (set-syntax-table messages-mode-syntax-table)
3825 (make-local-variable 'font-lock-defaults)
3826 (make-local-variable 'nfast-debug-mode-keywords)
3827 (setq truncate-lines t)
3828 (setq nfast-debug-mode-keywords
3829 (list
3830 '("^\\(NFast_\\(Connect\\|Disconnect\\|Submit\\|Wait\\)\\)"
3831 (0 font-lock-keyword-face))
3832 (list (concat "^[ \t]+\\(\\("
3833 "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
3834 "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
3835 "[ \t]+\\)*"
3836 "[0-9a-fA-F]+\\)[ \t]*$")
3837 '(0 mdw-number-face))
3838 '("^[ \t]+\.status=[ \t]+\\<\\(OK\\)\\>"
3839 (1 font-lock-keyword-face))
3840 '("^[ \t]+\.status=[ \t]+\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>"
3841 (1 font-lock-warning-face))
3842 '("^[ \t]+\.status[ \t]+\\<\\(zero\\)\\>"
3843 (1 nil))
3844 (list (concat "^[ \t]+\\.cmd=[ \t]+"
3845 "\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>")
3846 '(1 font-lock-keyword-face))
3847 '("-?\\<\\([0-9]+\\|0x[0-9a-fA-F]+\\)\\>" (0 mdw-number-face))
3848 '("^\\([ \t]+[a-z0-9.]+\\)" (0 font-lock-variable-name-face))
3849 '("\\<\\([a-z][a-z0-9.]+\\)\\>=" (1 font-lock-variable-name-face))
3850 '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" (0 mdw-punct-face))))
3851 (setq font-lock-defaults
3852 '(nfast-debug-mode-keywords nil nil nil nil))
3853 (run-hooks 'nfast-debug-mode-hook))
3854
3855 ;;;--------------------------------------------------------------------------
3856 ;;; Other languages.
3857
3858 ;; Smalltalk.
3859
3860 (defun mdw-setup-smalltalk ()
3861 (and mdw-auto-indent
3862 (local-set-key "\C-m" 'smalltalk-newline-and-indent))
3863 (make-local-variable 'mdw-auto-indent)
3864 (setq mdw-auto-indent nil)
3865 (local-set-key "\C-i" 'smalltalk-reindent))
3866
3867 (defun mdw-fontify-smalltalk ()
3868 (make-local-variable 'font-lock-keywords)
3869 (setq font-lock-keywords
3870 (list
3871 (list "\\<[A-Z][a-zA-Z0-9]*\\>"
3872 '(0 font-lock-keyword-face))
3873 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
3874 "[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)"
3875 "\\([eE]\\([-+]\\|\\)[0-9_]+\\|\\)")
3876 '(0 mdw-number-face))
3877 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3878 '(0 mdw-punct-face)))))
3879
3880 ;; Lispy languages.
3881
3882 ;; Unpleasant bodge.
3883 (unless (boundp 'slime-repl-mode-map)
3884 (setq slime-repl-mode-map (make-sparse-keymap)))
3885
3886 (defun mdw-indent-newline-and-indent ()
3887 (interactive)
3888 (indent-for-tab-command)
3889 (newline-and-indent))
3890
3891 (eval-after-load "cl-indent"
3892 '(progn
3893 (mapc #'(lambda (pair)
3894 (put (car pair)
3895 'common-lisp-indent-function
3896 (cdr pair)))
3897 '((destructuring-bind . ((&whole 4 &rest 1) 4 &body))
3898 (multiple-value-bind . ((&whole 4 &rest 1) 4 &body))))))
3899
3900 (defun mdw-common-lisp-indent ()
3901 (make-local-variable 'lisp-indent-function)
3902 (setq lisp-indent-function 'common-lisp-indent-function))
3903
3904 (setq lisp-simple-loop-indentation 2
3905 lisp-loop-keyword-indentation 6
3906 lisp-loop-forms-indentation 6)
3907
3908 (defun mdw-fontify-lispy ()
3909
3910 ;; Set fill prefix.
3911 (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)")
3912
3913 ;; Not much fontification needed.
3914 (make-local-variable 'font-lock-keywords)
3915 (setq font-lock-keywords
3916 (list (list (concat "\\("
3917 "\\_<[-+]?"
3918 "\\(" "[0-9]+/[0-9]+"
3919 "\\|" "\\(" "[0-9]+" "\\(\\.[0-9]*\\)?" "\\|"
3920 "\\.[0-9]+" "\\)"
3921 "\\([dDeEfFlLsS][-+]?[0-9]+\\)?"
3922 "\\)"
3923 "\\|"
3924 "#"
3925 "\\(" "x" "[-+]?"
3926 "[0-9A-Fa-f]+" "\\(/[0-9A-Fa-f]+\\)?"
3927 "\\|" "o" "[-+]?" "[0-7]+" "\\(/[0-7]+\\)?"
3928 "\\|" "b" "[-+]?" "[01]+" "\\(/[01]+\\)?"
3929 "\\|" "[0-9]+" "r" "[-+]?"
3930 "[0-9a-zA-Z]+" "\\(/[0-9a-zA-Z]+\\)?"
3931 "\\)"
3932 "\\)\\_>")
3933 '(0 mdw-number-face))
3934 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
3935 '(0 mdw-punct-face)))))
3936
3937 (defun comint-send-and-indent ()
3938 (interactive)
3939 (comint-send-input)
3940 (and mdw-auto-indent
3941 (indent-for-tab-command)))
3942
3943 (defadvice comint-line-beginning-position
3944 (around mdw-calculate-it-properly () activate compile)
3945 "Calculate the actual line start for multi-line input."
3946 (if (or comint-use-prompt-regexp
3947 (eq (field-at-pos (point)) 'output))
3948 ad-do-it
3949 (setq ad-return-value
3950 (constrain-to-field (line-beginning-position) (point)))))
3951
3952 (defun mdw-setup-m4 ()
3953
3954 ;; Inexplicably, Emacs doesn't match braces in m4 mode. This is very
3955 ;; annoying: fix it.
3956 (modify-syntax-entry ?{ "(")
3957 (modify-syntax-entry ?} ")")
3958
3959 ;; Fill prefix.
3960 (mdw-standard-fill-prefix "\\([ \t]*\\(?:#+\\|\\<dnl\\>\\)[ \t]*\\)"))
3961
3962 ;;;--------------------------------------------------------------------------
3963 ;;; Text mode.
3964
3965 (defun mdw-text-mode ()
3966 (setq fill-column 72)
3967 (flyspell-mode t)
3968 (mdw-standard-fill-prefix
3969 "\\([ \t]*\\([>#|:] ?\\)*[ \t]*\\)" 3)
3970 (auto-fill-mode 1))
3971
3972 (eval-after-load "flyspell"
3973 '(define-key flyspell-mode-map "\C-\M-i" nil))
3974
3975 ;;;--------------------------------------------------------------------------
3976 ;;; Outline and hide/show modes.
3977
3978 (defun mdw-outline-collapse-all ()
3979 "Completely collapse everything in the entire buffer."
3980 (interactive)
3981 (save-excursion
3982 (goto-char (point-min))
3983 (while (< (point) (point-max))
3984 (hide-subtree)
3985 (forward-line))))
3986
3987 (setq hs-hide-comments-when-hiding-all nil)
3988
3989 (defadvice hs-hide-all (after hide-first-comment activate)
3990 (save-excursion (hs-hide-initial-comment-block)))
3991
3992 ;;;--------------------------------------------------------------------------
3993 ;;; Shell mode.
3994
3995 (defun mdw-sh-mode-setup ()
3996 (local-set-key [?\C-a] 'comint-bol)
3997 (add-hook 'comint-output-filter-functions
3998 'comint-watch-for-password-prompt))
3999
4000 (defun mdw-term-mode-setup ()
4001 (setq term-prompt-regexp shell-prompt-pattern)
4002 (make-local-variable 'mouse-yank-at-point)
4003 (make-local-variable 'transient-mark-mode)
4004 (setq mouse-yank-at-point t)
4005 (auto-fill-mode -1)
4006 (setq tab-width 8))
4007
4008 (defun term-send-meta-right () (interactive) (term-send-raw-string "\e\e[C"))
4009 (defun term-send-meta-left () (interactive) (term-send-raw-string "\e\e[D"))
4010 (defun term-send-ctrl-uscore () (interactive) (term-send-raw-string "\C-_"))
4011 (defun term-send-meta-meta-something ()
4012 (interactive)
4013 (term-send-raw-string "\e\e")
4014 (term-send-raw))
4015 (eval-after-load 'term
4016 '(progn
4017 (define-key term-raw-map [?\e ?\e] nil)
4018 (define-key term-raw-map [?\e ?\e t] 'term-send-meta-meta-something)
4019 (define-key term-raw-map [?\C-/] 'term-send-ctrl-uscore)
4020 (define-key term-raw-map [M-right] 'term-send-meta-right)
4021 (define-key term-raw-map [?\e ?\M-O ?C] 'term-send-meta-right)
4022 (define-key term-raw-map [M-left] 'term-send-meta-left)
4023 (define-key term-raw-map [?\e ?\M-O ?D] 'term-send-meta-left)))
4024
4025 (defadvice term-exec (before program-args-list compile activate)
4026 "If the PROGRAM argument is a list, interpret it as (PROGRAM . SWITCHES).
4027 This allows you to pass a list of arguments through `ansi-term'."
4028 (let ((program (ad-get-arg 2)))
4029 (if (listp program)
4030 (progn
4031 (ad-set-arg 2 (car program))
4032 (ad-set-arg 4 (cdr program))))))
4033
4034 (defadvice term-exec-1 (around hack-environment compile activate)
4035 "Hack the environment inherited by inferiors in the terminal."
4036 (let ((process-environment process-environment))
4037 (setenv "LD_PRELOAD" nil)
4038 ad-do-it))
4039
4040 (defadvice shell (around hack-environment compile activate)
4041 "Hack the environment inherited by inferiors in the shell."
4042 (let ((process-environment process-environment))
4043 (setenv "LD_PRELOAD" nil)
4044 ad-do-it))
4045
4046 (defun ssh (host)
4047 "Open a terminal containing an ssh session to the HOST."
4048 (interactive "sHost: ")
4049 (ansi-term (list "ssh" host) (format "ssh@%s" host)))
4050
4051 (defvar git-grep-command
4052 "env GIT_PAGER=cat git grep --no-color -nH -e "
4053 "*The default command for \\[git-grep].")
4054
4055 (defvar git-grep-history nil)
4056
4057 (defun git-grep (command-args)
4058 "Run `git grep' with user-specified args and collect output in a buffer."
4059 (interactive
4060 (list (read-shell-command "Run git grep (like this): "
4061 git-grep-command 'git-grep-history)))
4062 (let ((grep-use-null-device nil))
4063 (grep command-args)))
4064
4065 ;;;--------------------------------------------------------------------------
4066 ;;; Magit configuration.
4067
4068 (setq magit-diff-refine-hunk 'all
4069 magit-view-git-manual-method 'man
4070 magit-log-margin '(nil age magit-log-margin-width t 18)
4071 magit-wip-after-save-local-mode-lighter ""
4072 magit-wip-after-apply-mode-lighter ""
4073 magit-wip-before-change-mode-lighter "")
4074 (eval-after-load "magit"
4075 '(progn (global-magit-file-mode 1)
4076 (magit-wip-after-save-mode 1)
4077 (magit-wip-after-apply-mode 1)
4078 (magit-wip-before-change-mode 1)
4079 (add-to-list 'magit-no-confirm 'safe-with-wip)
4080 (add-to-list 'magit-no-confirm 'trash)
4081 (push '(:eval (if (or magit-wip-after-save-local-mode
4082 magit-wip-after-apply-mode
4083 magit-wip-before-change-mode)
4084 (format " wip:%s%s%s"
4085 (if magit-wip-after-apply-mode "A" "")
4086 (if magit-wip-before-change-mode "C" "")
4087 (if magit-wip-after-save-local-mode "S" ""))))
4088 minor-mode-alist)
4089 (dolist (popup '(magit-diff-popup
4090 magit-diff-refresh-popup
4091 magit-diff-mode-refresh-popup
4092 magit-revision-mode-refresh-popup))
4093 (magit-define-popup-switch popup ?R "Reverse diff" "-R"))))
4094
4095 (defadvice magit-wip-commit-buffer-file
4096 (around mdw-just-this-buffer activate compile)
4097 (let ((magit-save-repository-buffers nil)) ad-do-it))
4098
4099 (defadvice magit-discard
4100 (around mdw-delete-if-prefix-argument activate compile)
4101 (let ((magit-delete-by-moving-to-trash
4102 (and (null current-prefix-arg)
4103 magit-delete-by-moving-to-trash)))
4104 ad-do-it))
4105
4106 (setq magit-repolist-columns
4107 '(("Name" 16 magit-repolist-column-ident nil)
4108 ("Version" 18 magit-repolist-column-version nil)
4109 ("St" 2 magit-repolist-column-dirty nil)
4110 ("L<U" 3 mdw-repolist-column-unpulled-from-upstream nil)
4111 ("L>U" 3 mdw-repolist-column-unpushed-to-upstream nil)
4112 ("Path" 32 magit-repolist-column-path nil)))
4113
4114 (setq magit-repository-directories '(("~/etc/profile" . 0)
4115 ("~/src/" . 1)))
4116
4117 (defadvice magit-list-repos (around mdw-dirname () activate compile)
4118 "Make sure the returned names are directory names.
4119 Otherwise child processes get started in the wrong directory and
4120 there is sadness."
4121 (setq ad-return-value (mapcar #'file-name-as-directory ad-do-it)))
4122
4123 (defun mdw-repolist-column-unpulled-from-upstream (_id)
4124 "Insert number of upstream commits not in the current branch."
4125 (let ((upstream (magit-get-upstream-branch (magit-get-current-branch) t)))
4126 (and upstream
4127 (let ((n (cadr (magit-rev-diff-count "HEAD" upstream))))
4128 (propertize (number-to-string n) 'face
4129 (if (> n 0) 'bold 'shadow))))))
4130
4131 (defun mdw-repolist-column-unpushed-to-upstream (_id)
4132 "Insert number of commits in the current branch but not its upstream."
4133 (let ((upstream (magit-get-upstream-branch (magit-get-current-branch) t)))
4134 (and upstream
4135 (let ((n (car (magit-rev-diff-count "HEAD" upstream))))
4136 (propertize (number-to-string n) 'face
4137 (if (> n 0) 'bold 'shadow))))))
4138
4139 (defun mdw-try-smerge ()
4140 (save-excursion
4141 (goto-char (point-min))
4142 (when (re-search-forward "^<<<<<<< " nil t)
4143 (smerge-mode 1))))
4144 (add-hook 'find-file-hook 'mdw-try-smerge t)
4145
4146 ;;;--------------------------------------------------------------------------
4147 ;;; GUD, and especially GDB.
4148
4149 ;; Inhibit window dedication. I mean, seriously, wtf?
4150 (defadvice gdb-display-buffer (after mdw-undedicated (buf) compile activate)
4151 "Don't make windows dedicated. Seriously."
4152 (set-window-dedicated-p ad-return-value nil))
4153 (defadvice gdb-set-window-buffer
4154 (after mdw-undedicated (name &optional ignore-dedicated window)
4155 compile activate)
4156 "Don't make windows dedicated. Seriously."
4157 (set-window-dedicated-p (or window (selected-window)) nil))
4158
4159 ;;;--------------------------------------------------------------------------
4160 ;;; MPC configuration.
4161
4162 (eval-when-compile (trap (require 'mpc)))
4163
4164 (setq mpc-browser-tags '(Artist|Composer|Performer Album|Playlist))
4165
4166 (defun mdw-mpc-now-playing ()
4167 (interactive)
4168 (require 'mpc)
4169 (save-excursion
4170 (set-buffer (mpc-proc-cmd (mpc-proc-cmd-list '("status" "currentsong"))))
4171 (mpc--status-callback))
4172 (let ((state (cdr (assq 'state mpc-status))))
4173 (cond ((member state '("stop"))
4174 (message "mpd stopped."))
4175 ((member state '("play" "pause"))
4176 (let* ((artist (cdr (assq 'Artist mpc-status)))
4177 (album (cdr (assq 'Album mpc-status)))
4178 (title (cdr (assq 'Title mpc-status)))
4179 (file (cdr (assq 'file mpc-status)))
4180 (duration-string (cdr (assq 'Time mpc-status)))
4181 (time-string (cdr (assq 'time mpc-status)))
4182 (time (and time-string
4183 (string-to-number
4184 (if (string-match ":" time-string)
4185 (substring time-string
4186 0 (match-beginning 0))
4187 (time-string)))))
4188 (duration (and duration-string
4189 (string-to-number duration-string)))
4190 (pos (and time duration
4191 (format " [%d:%02d/%d:%02d]"
4192 (/ time 60) (mod time 60)
4193 (/ duration 60) (mod duration 60))))
4194 (fmt (cond ((and artist title)
4195 (format "`%s' by %s%s" title artist
4196 (if album (format ", from `%s'" album)
4197 "")))
4198 (file
4199 (format "`%s' (no tags)" file))
4200 (t
4201 "(no idea what's playing!)"))))
4202 (if (string= state "play")
4203 (message "mpd playing %s%s" fmt (or pos ""))
4204 (message "mpd paused in %s%s" fmt (or pos "")))))
4205 (t
4206 (message "mpd in unknown state `%s'" state)))))
4207
4208 (defmacro mdw-define-mpc-wrapper (func bvl interactive &rest body)
4209 `(defun ,func ,bvl
4210 (interactive ,@interactive)
4211 (require 'mpc)
4212 ,@body
4213 (mdw-mpc-now-playing)))
4214
4215 (mdw-define-mpc-wrapper mdw-mpc-play-or-pause () nil
4216 (if (member (cdr (assq 'state (mpc-cmd-status))) '("play"))
4217 (mpc-pause)
4218 (mpc-play)))
4219
4220 (mdw-define-mpc-wrapper mdw-mpc-next () nil (mpc-next))
4221 (mdw-define-mpc-wrapper mdw-mpc-prev () nil (mpc-prev))
4222 (mdw-define-mpc-wrapper mdw-mpc-stop () nil (mpc-stop))
4223
4224 (defun mdw-mpc-louder (step)
4225 (interactive (list (if current-prefix-arg
4226 (prefix-numeric-value current-prefix-arg)
4227 +10)))
4228 (mpc-proc-cmd (format "volume %+d" step)))
4229
4230 (defun mdw-mpc-quieter (step)
4231 (interactive (list (if current-prefix-arg
4232 (prefix-numeric-value current-prefix-arg)
4233 +10)))
4234 (mpc-proc-cmd (format "volume %+d" (- step))))
4235
4236 (defun mdw-mpc-hack-lines (arg interactivep func)
4237 (if (and interactivep (use-region-p))
4238 (let ((from (region-beginning)) (to (region-end)))
4239 (goto-char from)
4240 (beginning-of-line)
4241 (funcall func)
4242 (forward-line)
4243 (while (< (point) to)
4244 (funcall func)
4245 (forward-line)))
4246 (let ((n (prefix-numeric-value arg)))
4247 (cond ((minusp n)
4248 (unless (bolp)
4249 (beginning-of-line)
4250 (funcall func)
4251 (incf n))
4252 (while (minusp n)
4253 (forward-line -1)
4254 (funcall func)
4255 (incf n)))
4256 (t
4257 (beginning-of-line)
4258 (while (plusp n)
4259 (funcall func)
4260 (forward-line)
4261 (decf n)))))))
4262
4263 (defun mdw-mpc-select-one ()
4264 (when (and (get-char-property (point) 'mpc-file)
4265 (not (get-char-property (point) 'mpc-select)))
4266 (mpc-select-toggle)))
4267
4268 (defun mdw-mpc-unselect-one ()
4269 (when (get-char-property (point) 'mpc-select)
4270 (mpc-select-toggle)))
4271
4272 (defun mdw-mpc-select (&optional arg interactivep)
4273 (interactive (list current-prefix-arg t))
4274 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
4275
4276 (defun mdw-mpc-unselect (&optional arg interactivep)
4277 (interactive (list current-prefix-arg t))
4278 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-unselect-one))
4279
4280 (defun mdw-mpc-unselect-backwards (arg)
4281 (interactive "p")
4282 (mdw-mpc-hack-lines (- arg) t 'mdw-mpc-unselect-one))
4283
4284 (defun mdw-mpc-unselect-all ()
4285 (interactive)
4286 (setq mpc-select nil)
4287 (mpc-selection-refresh))
4288
4289 (defun mdw-mpc-next-line (arg)
4290 (interactive "p")
4291 (beginning-of-line)
4292 (forward-line arg))
4293
4294 (defun mdw-mpc-previous-line (arg)
4295 (interactive "p")
4296 (beginning-of-line)
4297 (forward-line (- arg)))
4298
4299 (defun mdw-mpc-playlist-add (&optional arg interactivep)
4300 (interactive (list current-prefix-arg t))
4301 (let ((mpc-select mpc-select))
4302 (when (or arg (and interactivep (use-region-p)))
4303 (setq mpc-select nil)
4304 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
4305 (setq mpc-select (reverse mpc-select))
4306 (mpc-playlist-add)))
4307
4308 (defun mdw-mpc-playlist-delete (&optional arg interactivep)
4309 (interactive (list current-prefix-arg t))
4310 (setq mpc-select (nreverse mpc-select))
4311 (mpc-select-save
4312 (when (or arg (and interactivep (use-region-p)))
4313 (setq mpc-select nil)
4314 (mpc-selection-refresh)
4315 (mdw-mpc-hack-lines arg interactivep 'mdw-mpc-select-one))
4316 (mpc-playlist-delete)))
4317
4318 (defun mdw-mpc-hack-tagbrowsers ()
4319 (setq-local mode-line-format
4320 '("%e"
4321 mode-line-frame-identification
4322 mode-line-buffer-identification)))
4323 (add-hook 'mpc-tagbrowser-mode-hook 'mdw-mpc-hack-tagbrowsers)
4324
4325 (defun mdw-mpc-hack-songs ()
4326 (setq-local header-line-format
4327 ;; '("MPC " mpc-volume " " mpc-current-song)
4328 (list (propertize " " 'display '(space :align-to 0))
4329 ;; 'mpc-songs-format-description
4330 '(:eval
4331 (let ((deactivate-mark) (hscroll (window-hscroll)))
4332 (with-temp-buffer
4333 (mpc-format mpc-songs-format 'self hscroll)
4334 ;; That would be simpler than the hscroll handling in
4335 ;; mpc-format, but currently move-to-column does not
4336 ;; recognize :space display properties.
4337 ;; (move-to-column hscroll)
4338 ;; (delete-region (point-min) (point))
4339 (buffer-string)))))))
4340 (add-hook 'mpc-songs-mode-hook 'mdw-mpc-hack-songs)
4341
4342 (eval-after-load "mpc"
4343 '(progn
4344 (define-key mpc-mode-map "m" 'mdw-mpc-select)
4345 (define-key mpc-mode-map "u" 'mdw-mpc-unselect)
4346 (define-key mpc-mode-map "\177" 'mdw-mpc-unselect-backwards)
4347 (define-key mpc-mode-map "\e\177" 'mdw-mpc-unselect-all)
4348 (define-key mpc-mode-map "n" 'mdw-mpc-next-line)
4349 (define-key mpc-mode-map "p" 'mdw-mpc-previous-line)
4350 (define-key mpc-mode-map "/" 'mpc-songs-search)
4351 (setq mpc-songs-mode-map (make-sparse-keymap))
4352 (set-keymap-parent mpc-songs-mode-map mpc-mode-map)
4353 (define-key mpc-songs-mode-map "l" 'mpc-playlist)
4354 (define-key mpc-songs-mode-map "+" 'mdw-mpc-playlist-add)
4355 (define-key mpc-songs-mode-map "-" 'mdw-mpc-playlist-delete)
4356 (define-key mpc-songs-mode-map "\r" 'mpc-songs-jump-to)))
4357
4358 ;;;--------------------------------------------------------------------------
4359 ;;; Inferior Emacs Lisp.
4360
4361 (setq comint-prompt-read-only t)
4362
4363 (eval-after-load "comint"
4364 '(progn
4365 (define-key comint-mode-map "\C-w" 'comint-kill-region)
4366 (define-key comint-mode-map [C-S-backspace] 'comint-kill-whole-line)))
4367
4368 (eval-after-load "ielm"
4369 '(progn
4370 (define-key ielm-map "\C-w" 'comint-kill-region)
4371 (define-key ielm-map [C-S-backspace] 'comint-kill-whole-line)))
4372
4373 ;;;----- That's all, folks --------------------------------------------------
4374
4375 (provide 'dot-emacs)