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