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