Commit | Line | Data |
---|---|---|
f617db13 MW |
1 | ;;; -*-emacs-lisp-*- |
2 | ;;; | |
3 | ;;; $Id$ | |
4 | ;;; | |
5 | ;;; Functions and macros for .emacs | |
6 | ;;; | |
7 | ;;; (c) 2004 Mark Wooding | |
8 | ;;; | |
9 | ||
10 | ;;;----- Licensing notice --------------------------------------------------- | |
11 | ;;; | |
12 | ;;; This program is free software; you can redistribute it and/or modify | |
13 | ;;; it under the terms of the GNU General Public License as published by | |
14 | ;;; the Free Software Foundation; either version 2 of the License, or | |
15 | ;;; (at your option) any later version. | |
852cd5fb | 16 | ;;; |
f617db13 MW |
17 | ;;; This program is distributed in the hope that it will be useful, |
18 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;;; GNU General Public License for more details. | |
852cd5fb | 21 | ;;; |
f617db13 MW |
22 | ;;; You should have received a copy of the GNU General Public License |
23 | ;;; along with this program; if not, write to the Free Software Foundation, | |
24 | ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
25 | ||
26 | ;;;----- Some general utilities --------------------------------------------- | |
27 | ||
28 | ;; --- Some error trapping --- | |
29 | ;; | |
30 | ;; If individual bits of this file go tits-up, we don't particularly want | |
31 | ;; the whole lot to stop right there and then, because it's bloody annoying. | |
32 | ||
33 | (defmacro trap (&rest forms) | |
34 | "Execute FORMS without allowing errors to propagate outside." | |
35 | `(condition-case err | |
36 | ,(if (cdr forms) (cons 'progn forms) (car forms)) | |
37 | (error (message "Error (trapped): %s" (error-message-string err))))) | |
38 | ||
f141fe0f MW |
39 | ;; --- Configuration reading --- |
40 | ||
41 | (defvar mdw-config nil) | |
42 | (defun mdw-config (sym) | |
43 | "Read the configuration variable named SYM." | |
44 | (unless mdw-config | |
45 | (setq mdw-config (with-temp-buffer | |
46 | (insert-file-contents "~/.mdw.conf") | |
47 | (replace-regexp "^[ \t]*\\(#.*\\|\\)\n" "" | |
48 | nil (point-min) (point-max)) | |
49 | (replace-regexp (concat "^[ \t]*" | |
50 | "\\([-a-zA-Z0-9_.]*\\)" | |
51 | "[ \t]*=[ \t]*" | |
52 | "\\(.*[^ \t\n]\\|\\)" | |
53 | "[ \t]**\\(\n\\|$\\)") | |
54 | "(\\1 . \"\\2\") " | |
55 | nil (point-min) (point-max)) | |
56 | (car (read-from-string | |
57 | (concat "(" (buffer-string) ")")))))) | |
58 | (cdr (assq sym mdw-config))) | |
59 | ||
cb6e2cd1 MW |
60 | ;; --- Is an Emacs library available? --- |
61 | ||
62 | (defun library-exists-p (name) | |
63 | "Return non-nil if NAME.el (or NAME.elc) is somewhere on the Emacs load | |
64 | path. The non-nil value is the filename we found for the library." | |
65 | (let ((path load-path) elt (foundp nil)) | |
66 | (while (and path (not foundp)) | |
67 | (setq elt (car path)) | |
68 | (setq path (cdr path)) | |
69 | (setq foundp (or (let ((file (concat elt "/" name ".elc"))) | |
70 | (and (file-exists-p file) file)) | |
71 | (let ((file (concat elt "/" name ".el"))) | |
72 | (and (file-exists-p file) file))))) | |
73 | foundp)) | |
74 | ||
75 | (defun maybe-autoload (symbol file &optional docstring interactivep type) | |
76 | "Set an autoload if the file actually exists." | |
77 | (and (library-exists-p file) | |
78 | (autoload symbol file docstring interactivep type))) | |
79 | ||
f617db13 MW |
80 | ;; --- Splitting windows --- |
81 | ||
b5d724dd MW |
82 | (or (and (fboundp 'scroll-bar-columns) |
83 | (fboundp 'fringe-columns)) | |
84 | (progn | |
85 | (defun scroll-bar-columns (side) | |
86 | (cond ((eq side 'left) 0) | |
87 | (window-system 3) | |
88 | (t 1))) | |
89 | (defun fringe-columns (side) | |
90 | (cond ((not window-system) 0) | |
91 | ((eq side 'left) 1) | |
92 | (t 2))))) | |
93 | ||
f617db13 MW |
94 | (defun mdw-divvy-window (&optional w) |
95 | "Split a wide window into appropriate widths." | |
96 | (interactive) | |
b5d724dd MW |
97 | (or w (setq w (if (and window-system |
98 | (>= emacs-major-version 22)) | |
99 | 77 | |
100 | 78))) | |
101 | (let* ((win (selected-window)) | |
102 | (sb-width (if (not window-system) | |
103 | 1 | |
104 | (+ (scroll-bar-columns 'left) | |
105 | (scroll-bar-columns 'right) | |
106 | (fringe-columns 'left) | |
107 | (fringe-columns 'right)))) | |
108 | (c (/ (+ (window-width) sb-width) | |
109 | (+ w sb-width)))) | |
f617db13 MW |
110 | (while (> c 1) |
111 | (setq c (1- c)) | |
b5d724dd | 112 | (split-window-horizontally (+ w sb-width)) |
f617db13 MW |
113 | (other-window 1)) |
114 | (select-window win))) | |
115 | ||
116 | ;; --- Functions for sexp diary entries --- | |
117 | ||
118 | (defun mdw-weekday (l) | |
119 | "Return non-nil if `date' falls on one of the days of the week in L. | |
120 | ||
121 | L is a list of day numbers (from 0 to 6 for Sunday through to Saturday) or | |
122 | symbols `sunday', `monday', etc. (or a mixture). If the date stored in | |
123 | `date' falls on a listed day, then the function returns non-nil." | |
124 | (let ((d (calendar-day-of-week date))) | |
125 | (or (memq d l) | |
126 | (memq (nth d '(sunday monday tuesday wednesday | |
127 | thursday friday saturday)) l)))) | |
128 | ||
129 | (defun mdw-todo (&optional when) | |
130 | "Return non-nil today, or on WHEN, whichever is later." | |
131 | (let ((w (calendar-absolute-from-gregorian (calendar-current-date))) | |
132 | (d (calendar-absolute-from-gregorian date))) | |
133 | (if when | |
134 | (setq w (max w (calendar-absolute-from-gregorian | |
135 | (cond | |
136 | ((not european-calendar-style) | |
137 | when) | |
138 | ((> (car when) 100) | |
139 | (list (nth 1 when) | |
140 | (nth 2 when) | |
141 | (nth 0 when))) | |
142 | (t | |
143 | (list (nth 1 when) | |
144 | (nth 0 when) | |
145 | (nth 2 when)))))))) | |
146 | (eq w d))) | |
147 | ||
148 | ;;;----- Utility functions -------------------------------------------------- | |
149 | ||
b5d724dd MW |
150 | (or (fboundp 'line-number-at-pos) |
151 | (defun line-number-at-pos (&optional pos) | |
152 | (let ((opoint (or pos (point))) start) | |
153 | (save-excursion | |
154 | (save-restriction | |
155 | (goto-char (point-min)) | |
156 | (widen) | |
157 | (forward-line 0) | |
158 | (setq start (point)) | |
159 | (goto-char opoint) | |
160 | (forward-line 0) | |
161 | (1+ (count-lines 1 (point)))))))) | |
459c9fb2 | 162 | |
f617db13 MW |
163 | ;; --- mdw-uniquify-alist --- |
164 | ||
165 | (defun mdw-uniquify-alist (&rest alists) | |
166 | ||
167 | "Return the concatenation of the ALISTS with duplicate elements removed. | |
168 | ||
169 | The first association with a given key prevails; others are ignored. The | |
170 | input lists are not modified, although they'll probably become garbage." | |
171 | ||
172 | (and alists | |
173 | (let ((start-list (cons nil nil))) | |
174 | (mdw-do-uniquify start-list | |
175 | start-list | |
176 | (car alists) | |
177 | (cdr alists))))) | |
178 | ||
179 | ;; --- mdw-do-uniquify --- | |
180 | ;; | |
181 | ;; The DONE argument is a list whose first element is `nil'. It contains the | |
182 | ;; uniquified alist built so far. The leading `nil' is stripped off at the | |
183 | ;; end of the operation; it's only there so that DONE always references a | |
184 | ;; cons cell. END refers to the final cons cell in the DONE list; it is | |
185 | ;; modified in place each time to avoid the overheads of `append'ing all the | |
186 | ;; time. The L argument is the alist we're currently processing; the | |
187 | ;; remaining alists are given in REST. | |
188 | ||
189 | (defun mdw-do-uniquify (done end l rest) | |
190 | "A helper function for mdw-uniquify-alist." | |
191 | ||
192 | ;; --- There are several different cases to deal with here --- | |
193 | ||
194 | (cond | |
195 | ||
196 | ;; --- Current list isn't empty --- | |
197 | ;; | |
198 | ;; Add the first item to the DONE list if there's not an item with the | |
199 | ;; same KEY already there. | |
200 | ||
201 | (l (or (assoc (car (car l)) done) | |
202 | (progn | |
203 | (setcdr end (cons (car l) nil)) | |
204 | (setq end (cdr end)))) | |
205 | (mdw-do-uniquify done end (cdr l) rest)) | |
206 | ||
207 | ;; --- The list we were working on is empty --- | |
208 | ;; | |
209 | ;; Shunt the next list into the current list position and go round again. | |
210 | ||
211 | (rest (mdw-do-uniquify done end (car rest) (cdr rest))) | |
212 | ||
213 | ;; --- Everything's done --- | |
214 | ;; | |
215 | ;; Remove the leading `nil' from the DONE list and return it. Finished! | |
216 | ||
217 | (t (cdr done)))) | |
218 | ||
219 | ;; --- Insert a date --- | |
220 | ||
221 | (defun date () | |
222 | "Insert the current date in a pleasing way." | |
223 | (interactive) | |
224 | (insert (save-excursion | |
225 | (let ((buffer (get-buffer-create "*tmp*"))) | |
226 | (unwind-protect (progn (set-buffer buffer) | |
227 | (erase-buffer) | |
228 | (shell-command "date +%Y-%m-%d" t) | |
229 | (goto-char (mark)) | |
230 | (delete-backward-char 1) | |
231 | (buffer-string)) | |
232 | (kill-buffer buffer)))))) | |
233 | ||
234 | ;; --- UUencoding --- | |
235 | ||
236 | (defun uuencode (file &optional name) | |
237 | "UUencodes a file, maybe calling it NAME, into the current buffer." | |
238 | (interactive "fInput file name: ") | |
239 | ||
240 | ;; --- If NAME isn't specified, then guess from the filename --- | |
241 | ||
242 | (if (not name) | |
243 | (setq name | |
244 | (substring file | |
245 | (or (string-match "[^/]*$" file) 0)))) | |
246 | ||
247 | (print (format "uuencode `%s' `%s'" file name)) | |
248 | ||
249 | ;; --- Now actually do the thing --- | |
250 | ||
251 | (call-process "uuencode" file t nil name)) | |
252 | ||
253 | (defvar np-file "~/.np" | |
254 | "*Where the `now-playing' file is.") | |
255 | ||
256 | (defun np (&optional arg) | |
257 | "Grabs a `now-playing' string." | |
258 | (interactive) | |
259 | (save-excursion | |
260 | (or arg (progn | |
852cd5fb | 261 | (goto-char (point-max)) |
f617db13 | 262 | (insert "\nNP: ") |
852cd5fb | 263 | (insert-file np-file))))) |
f617db13 MW |
264 | |
265 | (trap | |
266 | (require 'tramp) | |
267 | (require 'autorevert) | |
268 | (defun mdw-check-autorevert () | |
269 | (if (and (buffer-file-name) | |
270 | (tramp-tramp-file-p (buffer-file-name))) | |
271 | (unless global-auto-revert-ignore-buffer | |
272 | (setq global-auto-revert-ignore-buffer 'tramp)) | |
273 | (if (eq global-auto-revert-ignore-buffer 'tramp) | |
274 | (setq global-auto-revert-ignore-buffer nil)))) | |
275 | (defadvice find-file (after mdw-autorevert activate) | |
276 | (mdw-check-autorevert)) | |
277 | (defadvice write-file (after mdw-autorevert activate) | |
278 | (mdw-check-autorevert))) | |
279 | ||
280 | (defun mdwmail-mode () | |
281 | "Major mode for editing news and mail messages from external programs | |
282 | Not much right now. Just support for doing MailCrypt stuff." | |
283 | (interactive) | |
284 | (kill-all-local-variables) | |
285 | (use-local-map text-mode-map) | |
286 | (setq local-abbrev-table text-mode-abbrev-table) | |
287 | (setq major-mode 'mdwmail-mode) | |
288 | (setq mode-name "[mdw] mail") | |
289 | (make-local-variable 'paragraph-separate) | |
290 | (make-local-variable 'paragraph-start) | |
291 | (setq paragraph-start (concat "[ \t]*[-_][-_][-_]+$\\|^-- \\|-----\\|" | |
292 | paragraph-start)) | |
293 | (setq paragraph-separate (concat "[ \t]*[-_][-_][-_]+$\\|^-- \\|-----\\|" | |
294 | paragraph-separate)) | |
295 | (run-hooks 'text-mode-hook 'mdwmail-mode-hook 'mail-setup-hook)) | |
296 | ||
297 | ;; --- How to encrypt in mdwmail --- | |
298 | ||
299 | (defun mdwmail-mc-encrypt (&optional recip scm start end from sign) | |
300 | (or start | |
301 | (setq start (save-excursion | |
302 | (goto-char (point-min)) | |
303 | (or (search-forward "\n\n" nil t) (point-min))))) | |
304 | (or end | |
305 | (setq end (point-max))) | |
306 | (mc-encrypt-generic recip scm start end from sign)) | |
307 | ||
308 | ;; --- How to sign in mdwmail --- | |
309 | ||
310 | (defun mdwmail-mc-sign (key scm start end uclr) | |
311 | (or start | |
312 | (setq start (save-excursion | |
313 | (goto-char (point-min)) | |
314 | (or (search-forward "\n\n" nil t) (point-min))))) | |
315 | (or end | |
316 | (setq end (point-max))) | |
317 | (mc-sign-generic key scm start end uclr)) | |
318 | ||
319 | ;; --- Some signature mangling --- | |
320 | ||
321 | (defun mdwmail-mangle-signature () | |
322 | (save-excursion | |
323 | (goto-char (point-min)) | |
324 | (perform-replace "\n-- \n" "\n-- " nil nil nil))) | |
325 | (add-hook 'mail-setup-hook 'mdwmail-mangle-signature) | |
326 | ||
a203fba8 MW |
327 | ;;;----- URL viewing -------------------------------------------------------- |
328 | ||
329 | (defun mdw-w3m-browse-url (url &optional new-session-p) | |
330 | "Invoke w3m on the URL in its current window, or at least a different one. | |
331 | If NEW-SESSION-P, start a new session." | |
332 | (interactive "sURL: \nP") | |
333 | (save-excursion | |
63fb20c1 MW |
334 | (let ((window (selected-window))) |
335 | (unwind-protect | |
336 | (progn | |
337 | (select-window (or (and (not new-session-p) | |
338 | (get-buffer-window "*w3m*")) | |
339 | (progn | |
340 | (if (one-window-p t) (split-window)) | |
341 | (get-lru-window)))) | |
342 | (w3m-browse-url url new-session-p)) | |
343 | (select-window window))))) | |
a203fba8 MW |
344 | |
345 | (defvar mdw-good-url-browsers | |
346 | '((w3m . mdw-w3m-browse-url) | |
347 | browse-url-w3 | |
348 | browse-url-mozilla) | |
349 | "List of good browsers for mdw-good-url-browsers; each item is a browser | |
350 | function name, or a cons (CHECK . FUNC). A symbol FOO stands for (FOO | |
351 | . FOO).") | |
352 | ||
353 | (defun mdw-good-url-browser () | |
354 | "Return a good URL browser. Trundle the list of such things, finding the | |
355 | first item for which CHECK is fboundp, and returning the correponding FUNC." | |
356 | (let ((bs mdw-good-url-browsers) b check func answer) | |
357 | (while (and bs (not answer)) | |
358 | (setq b (car bs) | |
359 | bs (cdr bs)) | |
360 | (if (consp b) | |
361 | (setq check (car b) func (cdr b)) | |
362 | (setq check b func b)) | |
363 | (if (fboundp check) | |
364 | (setq answer func))) | |
365 | answer)) | |
366 | ||
f617db13 MW |
367 | ;;;----- Paragraph filling -------------------------------------------------- |
368 | ||
369 | ;; --- Useful variables --- | |
370 | ||
371 | (defvar mdw-fill-prefix nil | |
372 | "*Used by `mdw-line-prefix' and `mdw-fill-paragraph'. If there's | |
373 | no fill prefix currently set (by the `fill-prefix' variable) and there's | |
374 | a match from one of the regexps here, it gets used to set the fill-prefix | |
375 | for the current operation. | |
376 | ||
377 | The variable is a list of items of the form `REGEXP . PREFIX'; if the | |
378 | REGEXP matches, the PREFIX is used to set the fill prefix. It in turn is | |
379 | a list of things: | |
380 | ||
381 | STRING -- insert a literal string | |
382 | (match . N) -- insert the thing matched by bracketed subexpression N | |
383 | (pad . N) -- a string of whitespace the same width as subexpression N | |
384 | (expr . FORM) -- the result of evaluating FORM") | |
385 | ||
386 | (make-variable-buffer-local 'mdw-fill-prefix) | |
387 | ||
388 | (defvar mdw-hanging-indents | |
389 | "\\(\\(\\([*o]\\|--\\|[0-9]+\\.\\|\\[[0-9]+\\]\\|([a-zA-Z])\\)[ \t]+\\)?\\)" | |
390 | "*Standard regular expression matching things which might be part of a | |
391 | hanging indent. This is mainly useful in `auto-fill-mode'.") | |
392 | ||
393 | ;; --- Setting things up --- | |
394 | ||
395 | (fset 'mdw-do-auto-fill (symbol-function 'do-auto-fill)) | |
396 | ||
397 | ;; --- Utility functions --- | |
398 | ||
399 | (defun mdw-tabify (s) | |
400 | "Tabify the string S. This is a horrid hack." | |
401 | (save-excursion | |
402 | (save-match-data | |
403 | (let (start end) | |
404 | (beginning-of-line) | |
405 | (setq start (point-marker)) | |
406 | (insert s "\n") | |
407 | (setq end (point-marker)) | |
408 | (tabify start end) | |
409 | (setq s (buffer-substring start (1- end))) | |
410 | (delete-region start end) | |
411 | (set-marker start nil) | |
412 | (set-marker end nil) | |
413 | s)))) | |
414 | ||
415 | (defun mdw-examine-fill-prefixes (l) | |
416 | "Given a list of dynamic fill prefixes, pick one which matches context and | |
417 | return the static fill prefix to use. Point must be at the start of a line, | |
418 | and match data must be saved." | |
419 | (cond ((not l) nil) | |
420 | ((looking-at (car (car l))) | |
421 | (mdw-tabify (apply (function concat) | |
422 | (mapcar (function mdw-do-prefix-match) | |
423 | (cdr (car l)))))) | |
424 | (t (mdw-examine-fill-prefixes (cdr l))))) | |
425 | ||
426 | (defun mdw-maybe-car (p) | |
427 | "If P is a pair, return (car P), otherwise just return P." | |
428 | (if (consp p) (car p) p)) | |
429 | ||
430 | (defun mdw-padding (s) | |
431 | "Return a string the same width as S but made entirely from whitespace." | |
432 | (let* ((l (length s)) (i 0) (n (make-string l ? ))) | |
433 | (while (< i l) | |
434 | (if (= 9 (aref s i)) | |
435 | (aset n i 9)) | |
436 | (setq i (1+ i))) | |
437 | n)) | |
438 | ||
439 | (defun mdw-do-prefix-match (m) | |
440 | "Expand a dynamic prefix match element. See `mdw-fill-prefix' for | |
441 | details." | |
442 | (cond ((not (consp m)) (format "%s" m)) | |
443 | ((eq (car m) 'match) (match-string (mdw-maybe-car (cdr m)))) | |
444 | ((eq (car m) 'pad) (mdw-padding (match-string | |
445 | (mdw-maybe-car (cdr m))))) | |
446 | ((eq (car m) 'eval) (eval (cdr m))) | |
447 | (t ""))) | |
448 | ||
449 | (defun mdw-choose-dynamic-fill-prefix () | |
450 | "Work out the dynamic fill prefix based on the variable `mdw-fill-prefix'." | |
451 | (cond ((and fill-prefix (not (string= fill-prefix ""))) fill-prefix) | |
452 | ((not mdw-fill-prefix) fill-prefix) | |
453 | (t (save-excursion | |
454 | (beginning-of-line) | |
455 | (save-match-data | |
456 | (mdw-examine-fill-prefixes mdw-fill-prefix)))))) | |
457 | ||
458 | (defun do-auto-fill () | |
459 | "Handle auto-filling, working out a dynamic fill prefix in the case where | |
460 | there isn't a sensible static one." | |
461 | (let ((fill-prefix (mdw-choose-dynamic-fill-prefix))) | |
462 | (mdw-do-auto-fill))) | |
463 | ||
464 | (defun mdw-fill-paragraph () | |
465 | "Fill paragraph, getting a dynamic fill prefix." | |
466 | (interactive) | |
467 | (let ((fill-prefix (mdw-choose-dynamic-fill-prefix))) | |
468 | (fill-paragraph nil))) | |
469 | ||
470 | (defun mdw-standard-fill-prefix (rx &optional mat) | |
471 | "Set the dynamic fill prefix, handling standard hanging indents and stuff. | |
472 | This is just a short-cut for setting the thing by hand, and by design it | |
473 | doesn't cope with anything approximating a complicated case." | |
474 | (setq mdw-fill-prefix | |
475 | `((,(concat rx mdw-hanging-indents) | |
476 | (match . 1) | |
477 | (pad . ,(or mat 2)))))) | |
478 | ||
479 | ;;;----- Other common declarations ------------------------------------------ | |
480 | ||
481 | (defun mdw-set-frame-transparency (&optional n) | |
482 | (interactive "P") | |
483 | (let* ((alist (frame-parameters)) | |
484 | (trans (assq 'transparency alist))) | |
485 | (if trans | |
486 | (rplacd trans (not (if n (zerop n) (cdr trans)))) | |
487 | (setq trans (cons 'transparency (not (equal 0 n))))) | |
488 | (modify-frame-parameters (selected-frame) (list trans)))) | |
489 | ||
490 | ;; --- Mouse wheel support --- | |
491 | ||
492 | (defconst mdw-wheel-scroll-amount 15) | |
493 | (defun mdw-wheel-up (click) | |
494 | (interactive "@e") | |
495 | (mdw-wheel-scroll click (function scroll-down))) | |
496 | (defun mdw-wheel-down (click) | |
497 | (interactive "@e") | |
498 | (mdw-wheel-scroll click (function scroll-up))) | |
499 | ||
500 | (defun mdw-wheel-scroll (click func) | |
501 | (let ((win (selected-window))) | |
502 | (unwind-protect | |
503 | (progn | |
504 | (select-window (posn-window (event-start click))) | |
505 | (let ((arg 2)) | |
506 | (funcall func (/ (window-height) 2)))) | |
507 | (select-window win)))) | |
508 | ||
509 | ;; --- Going backwards --- | |
510 | ||
511 | (defun other-window-backwards (arg) | |
512 | (interactive "p") | |
513 | (other-window (- arg))) | |
514 | ||
515 | ;; --- Common mode settings --- | |
516 | ||
517 | (defvar mdw-auto-indent t | |
518 | "Whether to indent automatically after a newline.") | |
519 | ||
520 | (defun mdw-misc-mode-config () | |
521 | (and mdw-auto-indent | |
522 | (cond ((eq major-mode 'lisp-mode) | |
523 | (local-set-key "\C-m" 'mdw-indent-newline-and-indent)) | |
30c8a8fb MW |
524 | ((or (eq major-mode 'slime-repl-mode) |
525 | (eq major-mode 'asm-mode)) | |
526 | nil) | |
f617db13 MW |
527 | (t |
528 | (local-set-key "\C-m" 'newline-and-indent)))) | |
529 | (local-set-key [C-return] 'newline) | |
30c8a8fb MW |
530 | (or (eq major-mode 'asm-mode) |
531 | (local-set-key [?\;] 'self-insert-command)) | |
f617db13 MW |
532 | (local-set-key [?\#] 'self-insert-command) |
533 | (local-set-key [?\"] 'self-insert-command) | |
534 | (setq comment-column 40) | |
535 | (auto-fill-mode 1) | |
536 | (setq fill-column 77) | |
473ff3b0 | 537 | (setq show-trailing-whitespace t) |
f617db13 MW |
538 | (mdw-set-font)) |
539 | ||
540 | ;; --- Set up all sorts of faces --- | |
541 | ||
542 | (defvar mdw-set-font nil) | |
543 | ||
544 | (defvar mdw-punct-face 'mdw-punct-face "Face to use for punctuation") | |
545 | (make-face 'mdw-punct-face) | |
546 | (defvar mdw-number-face 'mdw-number-face "Face to use for numbers") | |
547 | (make-face 'mdw-number-face) | |
548 | ||
549 | ;;;----- General fontification ---------------------------------------------- | |
550 | ||
473ff3b0 MW |
551 | (defun mdw-set-fonts (frame faces) |
552 | (while faces | |
553 | (let ((face (caar faces))) | |
554 | (or (facep face) (make-face face)) | |
555 | (set-face-attribute face frame | |
556 | :family 'unspecified | |
557 | :width 'unspecified | |
558 | :height 'unspecified | |
559 | :weight 'unspecified | |
560 | :slant 'unspecified | |
561 | :foreground 'unspecified | |
562 | :background 'unspecified | |
563 | :underline 'unspecified | |
564 | :overline 'unspecified | |
565 | :strike-through 'unspecified | |
566 | :box 'unspecified | |
567 | :inverse-video 'unspecified | |
568 | :stipple 'unspecified | |
569 | ;:font 'unspecified | |
570 | :inherit 'unspecified) | |
571 | (apply 'set-face-attribute face frame (cdar faces)) | |
572 | (setq faces (cdr faces))))) | |
f617db13 MW |
573 | |
574 | (defun mdw-do-set-font (&optional frame) | |
575 | (interactive) | |
576 | (mdw-set-fonts (and (boundp 'frame) frame) `( | |
577 | (default :foreground "white" :background "black" | |
578 | ,@(cond ((eq window-system 'w32) | |
579 | '(:family "courier new" :height 85)) | |
580 | ((eq window-system 'x) | |
581 | '(:family "misc-fixed" :width semi-condensed)))) | |
b5d724dd MW |
582 | (fixed-pitch) |
583 | (minibuffer-prompt) | |
f617db13 MW |
584 | (modeline :foreground "blue" :background "yellow" |
585 | :box (:line-width 1 :style released-button)) | |
586 | (scroll-bar :foreground "black" :background "lightgrey") | |
587 | (fringe :foreground "yellow" :background "grey30") | |
588 | (show-paren-match-face :background "darkgreen") | |
589 | (show-paren-mismatch-face :background "red") | |
590 | (font-lock-warning-face :background "red" :weight bold) | |
591 | (highlight :background "DarkSeaGreen4") | |
592 | (holiday-face :background "red") | |
593 | (calendar-today-face :foreground "yellow" :weight bold) | |
594 | (comint-highlight-prompt :weight bold) | |
595 | (comint-highlight-input) | |
596 | (font-lock-builtin-face :weight bold) | |
597 | (font-lock-type-face :weight bold) | |
598 | (region :background "grey30") | |
599 | (isearch :background "palevioletred2") | |
600 | (mdw-punct-face :foreground ,(if window-system "burlywood2" "yellow")) | |
601 | (mdw-number-face :foreground "yellow") | |
602 | (font-lock-function-name-face :weight bold) | |
603 | (font-lock-variable-name-face :slant italic) | |
604 | (font-lock-comment-face | |
605 | :foreground ,(if window-system "SeaGreen1" "green") | |
606 | :slant italic) | |
607 | (font-lock-string-face :foreground ,(if window-system "SkyBlue1" "cyan")) | |
608 | (font-lock-keyword-face :weight bold) | |
609 | (font-lock-constant-face :weight bold) | |
610 | (font-lock-reference-face :weight bold) | |
611 | (woman-bold-face :weight bold) | |
612 | (woman-italic-face :slant italic) | |
613 | (diff-header-face :foreground "skyblue1") | |
614 | (diff-index-face :weight bold) | |
615 | (diff-file-header-face) | |
616 | (diff-context-face :foreground "grey70") | |
617 | (diff-added-face :foreground "white") | |
618 | (diff-removed-face :foreground "white" :slant italic) | |
619 | (whizzy-slice-face :background "grey10") | |
620 | (whizzy-error-face :background "darkred") | |
473ff3b0 | 621 | (trailing-whitespace :background "red") |
f617db13 MW |
622 | ))) |
623 | ||
624 | (defun mdw-set-font () | |
625 | (trap | |
626 | (turn-on-font-lock) | |
627 | (if (not mdw-set-font) | |
628 | (progn | |
629 | (setq mdw-set-font t) | |
630 | (mdw-do-set-font nil))))) | |
631 | ||
632 | ;;;----- C programming configuration ---------------------------------------- | |
633 | ||
634 | ;; --- Linux kernel hacking --- | |
635 | ||
636 | (defvar linux-c-mode-hook) | |
637 | ||
638 | (defun linux-c-mode () | |
639 | (interactive) | |
640 | (c-mode) | |
641 | (setq major-mode 'linux-c-mode) | |
642 | (setq mode-name "Linux C") | |
643 | (run-hooks 'linux-c-mode-hook)) | |
644 | ||
645 | ;; --- Make C indentation nice --- | |
646 | ||
647 | (defun mdw-c-style () | |
648 | (c-add-style "[mdw] C and C++ style" | |
649 | '((c-basic-offset . 2) | |
650 | (c-tab-always-indent . nil) | |
651 | (comment-column . 40) | |
652 | (c-class-key . "class") | |
653 | (c-offsets-alist (substatement-open . 0) | |
654 | (label . 0) | |
655 | (case-label . +) | |
656 | (access-label . -) | |
657 | (inclass . ++) | |
658 | (inline-open . ++) | |
659 | (statement-cont . 0) | |
660 | (statement-case-intro . +))) | |
661 | t)) | |
662 | ||
663 | (defun mdw-fontify-c-and-c++ () | |
664 | ||
665 | ;; --- Fiddle with some syntax codes --- | |
666 | ||
667 | (modify-syntax-entry ?_ "w") | |
668 | (modify-syntax-entry ?* ". 23") | |
669 | (modify-syntax-entry ?/ ". 124b") | |
670 | (modify-syntax-entry ?\n "> b") | |
671 | ||
672 | ;; --- Other stuff --- | |
673 | ||
674 | (mdw-c-style) | |
675 | (setq c-hanging-comment-ender-p nil) | |
676 | (setq c-backslash-column 72) | |
677 | (setq c-label-minimum-indentation 0) | |
f617db13 MW |
678 | (setq mdw-fill-prefix |
679 | `((,(concat "\\([ \t]*/?\\)" | |
680 | "\\([\*/][ \t]*\\)" | |
681 | "\\([A-Za-z]+:[ \t]*\\)?" | |
682 | mdw-hanging-indents) | |
683 | (pad . 1) (match . 2) (pad . 3) (pad . 4)))) | |
684 | ||
685 | ;; --- Now define things to be fontified --- | |
686 | ||
02109a0d | 687 | (make-local-variable 'font-lock-keywords) |
f617db13 MW |
688 | (let ((c-keywords |
689 | (make-regexp '( | |
4459800e MW |
690 | "and" ;C++ |
691 | "and_eq" ;C++ | |
f617db13 MW |
692 | "asm" ;K&R, GCC |
693 | "auto" ;K&R, C89 | |
4459800e MW |
694 | "bitand" ;C++ |
695 | "bitor" ;C++ | |
f617db13 MW |
696 | "bool" ;C++, C9X macro |
697 | "break" ;K&R, C89 | |
698 | "case" ;K&R, C89 | |
699 | "catch" ;C++ | |
700 | "char" ;K&R, C89 | |
701 | "class" ;C++ | |
702 | "complex" ;C9X macro, C++ template type | |
4459800e | 703 | "compl" ;C++ |
f617db13 MW |
704 | "const" ;C89 |
705 | "const_cast" ;C++ | |
706 | "continue" ;K&R, C89 | |
707 | "defined" ;C89 preprocessor | |
708 | "default" ;K&R, C89 | |
709 | "delete" ;C++ | |
710 | "do" ;K&R, C89 | |
711 | "double" ;K&R, C89 | |
712 | "dynamic_cast" ;C++ | |
713 | "else" ;K&R, C89 | |
714 | ;; "entry" ;K&R -- never used | |
715 | "enum" ;C89 | |
716 | "explicit" ;C++ | |
4459800e | 717 | "export" ;C++ |
f617db13 MW |
718 | "extern" ;K&R, C89 |
719 | "false" ;C++, C9X macro | |
720 | "float" ;K&R, C89 | |
721 | "for" ;K&R, C89 | |
4459800e | 722 | ;; "fortran" ;K&R |
f617db13 MW |
723 | "friend" ;C++ |
724 | "goto" ;K&R, C89 | |
725 | "if" ;K&R, C89 | |
726 | "imaginary" ;C9X macro | |
727 | "inline" ;C++, C9X, GCC | |
728 | "int" ;K&R, C89 | |
729 | "long" ;K&R, C89 | |
730 | "mutable" ;C++ | |
731 | "namespace" ;C++ | |
732 | "new" ;C++ | |
733 | "operator" ;C++ | |
4459800e MW |
734 | "or" ;C++ |
735 | "or_eq" ;C++ | |
f617db13 MW |
736 | "private" ;C++ |
737 | "protected" ;C++ | |
738 | "public" ;C++ | |
739 | "register" ;K&R, C89 | |
740 | "reinterpret_cast" ;C++ | |
741 | "restrict" ;C9X | |
742 | "return" ;K&R, C89 | |
743 | "short" ;K&R, C89 | |
744 | "signed" ;C89 | |
745 | "sizeof" ;K&R, C89 | |
746 | "static" ;K&R, C89 | |
747 | "static_cast" ;C++ | |
748 | "struct" ;K&R, C89 | |
749 | "switch" ;K&R, C89 | |
750 | "template" ;C++ | |
751 | "this" ;C++ | |
752 | "throw" ;C++ | |
753 | "true" ;C++, C9X macro | |
754 | "try" ;C++ | |
755 | "this" ;C++ | |
756 | "typedef" ;C89 | |
757 | "typeid" ;C++ | |
758 | "typeof" ;GCC | |
759 | "typename" ;C++ | |
760 | "union" ;K&R, C89 | |
761 | "unsigned" ;K&R, C89 | |
762 | "using" ;C++ | |
763 | "virtual" ;C++ | |
764 | "void" ;C89 | |
765 | "volatile" ;C89 | |
766 | "wchar_t" ;C++, C89 library type | |
767 | "while" ;K&R, C89 | |
4459800e MW |
768 | "xor" ;C++ |
769 | "xor_eq" ;C++ | |
f617db13 MW |
770 | "_Bool" ;C9X |
771 | "_Complex" ;C9X | |
772 | "_Imaginary" ;C9X | |
773 | "_Pragma" ;C9X preprocessor | |
774 | "__alignof__" ;GCC | |
775 | "__asm__" ;GCC | |
776 | "__attribute__" ;GCC | |
777 | "__complex__" ;GCC | |
778 | "__const__" ;GCC | |
779 | "__extension__" ;GCC | |
780 | "__imag__" ;GCC | |
781 | "__inline__" ;GCC | |
782 | "__label__" ;GCC | |
783 | "__real__" ;GCC | |
784 | "__signed__" ;GCC | |
785 | "__typeof__" ;GCC | |
786 | "__volatile__" ;GCC | |
787 | ))) | |
788 | (preprocessor-keywords | |
789 | (make-regexp '("assert" "define" "elif" "else" "endif" "error" | |
790 | "ident" "if" "ifdef" "ifndef" "import" "include" | |
791 | "line" "pragma" "unassert" "undef" "warning"))) | |
792 | (objc-keywords | |
793 | (make-regexp '("class" "defs" "encode" "end" "implementation" | |
794 | "interface" "private" "protected" "protocol" "public" | |
795 | "selector")))) | |
796 | ||
797 | (setq font-lock-keywords | |
798 | (list | |
f617db13 MW |
799 | |
800 | ;; --- Fontify include files as strings --- | |
801 | ||
802 | (list (concat "^[ \t]*\\#[ \t]*" | |
803 | "\\(include\\|import\\)" | |
804 | "[ \t]*\\(<[^>]+\\(>\\|\\)\\)") | |
805 | '(2 font-lock-string-face)) | |
806 | ||
807 | ;; --- Preprocessor directives are `references'? --- | |
808 | ||
809 | (list (concat "^\\([ \t]*#[ \t]*\\(\\(" | |
810 | preprocessor-keywords | |
811 | "\\)\\>\\|[0-9]+\\|$\\)\\)") | |
812 | '(1 font-lock-keyword-face)) | |
813 | ||
814 | ;; --- Handle the keywords defined above --- | |
815 | ||
816 | (list (concat "@\\<\\(" objc-keywords "\\)\\>") | |
817 | '(0 font-lock-keyword-face)) | |
818 | ||
819 | (list (concat "\\<\\(" c-keywords "\\)\\>") | |
820 | '(0 font-lock-keyword-face)) | |
821 | ||
822 | ;; --- Handle numbers too --- | |
823 | ;; | |
824 | ;; This looks strange, I know. It corresponds to the | |
825 | ;; preprocessor's idea of what a number looks like, rather than | |
826 | ;; anything sensible. | |
827 | ||
828 | (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)" | |
829 | "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*") | |
830 | '(0 mdw-number-face)) | |
831 | ||
832 | ;; --- And anything else is punctuation --- | |
833 | ||
834 | (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" | |
835 | '(0 mdw-punct-face)))))) | |
836 | ||
837 | ;;;----- AP calc mode ------------------------------------------------------- | |
838 | ||
839 | (defun apcalc-mode () | |
840 | (interactive) | |
841 | (c-mode) | |
842 | (setq major-mode 'apcalc-mode) | |
843 | (setq mode-name "AP Calc") | |
844 | (run-hooks 'apcalc-mode-hook)) | |
845 | ||
846 | (defun mdw-fontify-apcalc () | |
847 | ||
848 | ;; --- Fiddle with some syntax codes --- | |
849 | ||
850 | (modify-syntax-entry ?_ "w") | |
851 | (modify-syntax-entry ?* ". 23") | |
852 | (modify-syntax-entry ?/ ". 14") | |
853 | ||
854 | ;; --- Other stuff --- | |
855 | ||
856 | (mdw-c-style) | |
857 | (setq c-hanging-comment-ender-p nil) | |
858 | (setq c-backslash-column 72) | |
859 | (setq comment-start "/* ") | |
860 | (setq comment-end " */") | |
861 | (setq mdw-fill-prefix | |
862 | `((,(concat "\\([ \t]*/?\\)" | |
863 | "\\([\*/][ \t]*\\)" | |
864 | "\\([A-Za-z]+:[ \t]*\\)?" | |
865 | mdw-hanging-indents) | |
866 | (pad . 1) (match . 2) (pad . 3) (pad . 4)))) | |
867 | ||
868 | ;; --- Now define things to be fontified --- | |
869 | ||
02109a0d | 870 | (make-local-variable 'font-lock-keywords) |
f617db13 MW |
871 | (let ((c-keywords |
872 | (make-regexp '("break" "case" "cd" "continue" "define" "default" | |
873 | "do" "else" "exit" "for" "global" "goto" "help" "if" | |
874 | "local" "mat" "obj" "print" "quit" "read" "return" | |
875 | "show" "static" "switch" "while" "write")))) | |
876 | ||
877 | (setq font-lock-keywords | |
878 | (list | |
f617db13 MW |
879 | |
880 | ;; --- Handle the keywords defined above --- | |
881 | ||
882 | (list (concat "\\<\\(" c-keywords "\\)\\>") | |
883 | '(0 font-lock-keyword-face)) | |
884 | ||
885 | ;; --- Handle numbers too --- | |
886 | ;; | |
887 | ;; This looks strange, I know. It corresponds to the | |
888 | ;; preprocessor's idea of what a number looks like, rather than | |
889 | ;; anything sensible. | |
890 | ||
891 | (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)" | |
892 | "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*") | |
893 | '(0 mdw-number-face)) | |
894 | ||
895 | ;; --- And anything else is punctuation --- | |
896 | ||
897 | (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" | |
898 | '(0 mdw-punct-face)))))) | |
899 | ||
900 | ;;;----- Java programming configuration ------------------------------------- | |
901 | ||
902 | ;; --- Make indentation nice --- | |
903 | ||
904 | (defun mdw-java-style () | |
905 | (c-add-style "[mdw] Java style" | |
906 | '((c-basic-offset . 2) | |
907 | (c-tab-always-indent . nil) | |
908 | (c-offsets-alist (substatement-open . 0) | |
909 | (label . +) | |
910 | (case-label . +) | |
911 | (access-label . 0) | |
912 | (inclass . +) | |
913 | (statement-case-intro . +))) | |
914 | t)) | |
915 | ||
916 | ;; --- Declare Java fontification style --- | |
917 | ||
918 | (defun mdw-fontify-java () | |
919 | ||
920 | ;; --- Other stuff --- | |
921 | ||
922 | (mdw-java-style) | |
923 | (modify-syntax-entry ?_ "w") | |
924 | (setq c-hanging-comment-ender-p nil) | |
925 | (setq c-backslash-column 72) | |
926 | (setq comment-start "/* ") | |
927 | (setq comment-end " */") | |
928 | (setq mdw-fill-prefix | |
929 | `((,(concat "\\([ \t]*/?\\)" | |
930 | "\\([\*/][ \t]*\\)" | |
931 | "\\([A-Za-z]+:[ \t]*\\)?" | |
932 | mdw-hanging-indents) | |
933 | (pad . 1) (match . 2) (pad . 3) (pad . 4)))) | |
934 | ||
935 | ;; --- Now define things to be fontified --- | |
936 | ||
02109a0d | 937 | (make-local-variable 'font-lock-keywords) |
f617db13 MW |
938 | (let ((java-keywords |
939 | (make-regexp '("abstract" "boolean" "break" "byte" "case" "catch" | |
940 | "char" "class" "const" "continue" "default" "do" | |
941 | "double" "else" "extends" "final" "finally" "float" | |
942 | "for" "goto" "if" "implements" "import" "instanceof" | |
943 | "int" "interface" "long" "native" "new" "package" | |
944 | "private" "protected" "public" "return" "short" | |
945 | "static" "super" "switch" "synchronized" "this" | |
946 | "throw" "throws" "transient" "try" "void" "volatile" | |
947 | "while" | |
948 | ||
949 | "false" "null" "true")))) | |
950 | ||
951 | (setq font-lock-keywords | |
952 | (list | |
f617db13 MW |
953 | |
954 | ;; --- Handle the keywords defined above --- | |
955 | ||
956 | (list (concat "\\<\\(" java-keywords "\\)\\>") | |
957 | '(0 font-lock-keyword-face)) | |
958 | ||
959 | ;; --- Handle numbers too --- | |
960 | ;; | |
961 | ;; The following isn't quite right, but it's close enough. | |
962 | ||
963 | (list (concat "\\<\\(" | |
964 | "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|" | |
965 | "[0-9]+\\(\\.[0-9]*\\|\\)" | |
966 | "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)" | |
967 | "[lLfFdD]?") | |
968 | '(0 mdw-number-face)) | |
969 | ||
970 | ;; --- And anything else is punctuation --- | |
971 | ||
972 | (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" | |
973 | '(0 mdw-punct-face)))))) | |
974 | ||
e808c1e5 MW |
975 | ;;;----- C# programming configuration --------------------------------------- |
976 | ||
977 | ;; --- Make indentation nice --- | |
978 | ||
979 | (defun mdw-csharp-style () | |
980 | (c-add-style "[mdw] C# style" | |
981 | '((c-basic-offset . 2) | |
982 | (c-tab-always-indent . nil) | |
983 | (c-offsets-alist (substatement-open . 0) | |
984 | (label . 0) | |
985 | (case-label . +) | |
986 | (access-label . 0) | |
987 | (inclass . +) | |
988 | (statement-case-intro . +))) | |
989 | t)) | |
990 | ||
991 | ;; --- Declare C# fontification style --- | |
992 | ||
993 | (defun mdw-fontify-csharp () | |
994 | ||
995 | ;; --- Other stuff --- | |
996 | ||
997 | (mdw-csharp-style) | |
998 | (modify-syntax-entry ?_ "w") | |
999 | (setq c-hanging-comment-ender-p nil) | |
1000 | (setq c-backslash-column 72) | |
1001 | (setq comment-start "/* ") | |
1002 | (setq comment-end " */") | |
1003 | (setq mdw-fill-prefix | |
1004 | `((,(concat "\\([ \t]*/?\\)" | |
1005 | "\\([\*/][ \t]*\\)" | |
1006 | "\\([A-Za-z]+:[ \t]*\\)?" | |
1007 | mdw-hanging-indents) | |
1008 | (pad . 1) (match . 2) (pad . 3) (pad . 4)))) | |
1009 | ||
1010 | ;; --- Now define things to be fontified --- | |
1011 | ||
1012 | (make-local-variable 'font-lock-keywords) | |
1013 | (let ((csharp-keywords | |
1014 | (make-regexp '("abstract" "as" "base" "bool" "break" | |
1015 | "byte" "case" "catch" "char" "checked" | |
1016 | "class" "const" "continue" "decimal" "default" | |
1017 | "delegate" "do" "double" "else" "enum" | |
1018 | "event" "explicit" "extern" "false" "finally" | |
1019 | "fixed" "float" "for" "foreach" "goto" | |
1020 | "if" "implicit" "in" "int" "interface" | |
1021 | "internal" "is" "lock" "long" "namespace" | |
1022 | "new" "null" "object" "operator" "out" | |
1023 | "override" "params" "private" "protected" "public" | |
1024 | "readonly" "ref" "return" "sbyte" "sealed" | |
1025 | "short" "sizeof" "stackalloc" "static" "string" | |
1026 | "struct" "switch" "this" "throw" "true" | |
1027 | "try" "typeof" "uint" "ulong" "unchecked" | |
1028 | "unsafe" "ushort" "using" "virtual" "void" | |
1029 | "volatile" "while" "yield")))) | |
1030 | ||
1031 | (setq font-lock-keywords | |
1032 | (list | |
e808c1e5 MW |
1033 | |
1034 | ;; --- Handle the keywords defined above --- | |
1035 | ||
1036 | (list (concat "\\<\\(" csharp-keywords "\\)\\>") | |
1037 | '(0 font-lock-keyword-face)) | |
1038 | ||
1039 | ;; --- Handle numbers too --- | |
1040 | ;; | |
1041 | ;; The following isn't quite right, but it's close enough. | |
1042 | ||
1043 | (list (concat "\\<\\(" | |
1044 | "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|" | |
1045 | "[0-9]+\\(\\.[0-9]*\\|\\)" | |
1046 | "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)" | |
1047 | "[lLfFdD]?") | |
1048 | '(0 mdw-number-face)) | |
1049 | ||
1050 | ;; --- And anything else is punctuation --- | |
1051 | ||
1052 | (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" | |
1053 | '(0 mdw-punct-face)))))) | |
1054 | ||
1055 | (defun csharp-mode () | |
1056 | (interactive) | |
1057 | (java-mode) | |
1058 | (setq major-mode 'csharp-mode) | |
1059 | (setq mode-name "C#") | |
1060 | (mdw-fontify-csharp) | |
1061 | (run-hooks 'csharp-mode-hook)) | |
1062 | ||
f617db13 MW |
1063 | ;;;----- Awk programming configuration -------------------------------------- |
1064 | ||
1065 | ;; --- Make Awk indentation nice --- | |
1066 | ||
1067 | (defun mdw-awk-style () | |
1068 | (c-add-style "[mdw] Awk style" | |
1069 | '((c-basic-offset . 2) | |
1070 | (c-tab-always-indent . nil) | |
1071 | (c-offsets-alist (substatement-open . 0) | |
1072 | (statement-cont . 0) | |
1073 | (statement-case-intro . +))) | |
1074 | t)) | |
1075 | ||
1076 | ;; --- Declare Awk fontification style --- | |
1077 | ||
1078 | (defun mdw-fontify-awk () | |
1079 | ||
1080 | ;; --- Miscellaneous fiddling --- | |
1081 | ||
1082 | (modify-syntax-entry ?_ "w") | |
1083 | (mdw-awk-style) | |
1084 | (setq c-backslash-column 72) | |
1085 | (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)") | |
1086 | ||
1087 | ;; --- Now define things to be fontified --- | |
1088 | ||
02109a0d | 1089 | (make-local-variable 'font-lock-keywords) |
f617db13 MW |
1090 | (let ((c-keywords |
1091 | (make-regexp '("BEGIN" "END" "ARGC" "ARGIND" "ARGV" "CONVFMT" | |
1092 | "ENVIRON" "ERRNO" "FIELDWIDTHS" "FILENAME" "FNR" | |
1093 | "FS" "IGNORECASE" "NF" "NR" "OFMT" "OFS" "ORS" "RS" | |
1094 | "RSTART" "RLENGTH" "RT" "SUBSEP" | |
1095 | "atan2" "break" "close" "continue" "cos" "delete" | |
1096 | "do" "else" "exit" "exp" "fflush" "file" "for" "func" | |
1097 | "function" "gensub" "getline" "gsub" "if" "in" | |
1098 | "index" "int" "length" "log" "match" "next" "rand" | |
1099 | "return" "print" "printf" "sin" "split" "sprintf" | |
1100 | "sqrt" "srand" "strftime" "sub" "substr" "system" | |
1101 | "systime" "tolower" "toupper" "while")))) | |
1102 | ||
1103 | (setq font-lock-keywords | |
1104 | (list | |
f617db13 MW |
1105 | |
1106 | ;; --- Handle the keywords defined above --- | |
1107 | ||
1108 | (list (concat "\\<\\(" c-keywords "\\)\\>") | |
1109 | '(0 font-lock-keyword-face)) | |
1110 | ||
1111 | ;; --- Handle numbers too --- | |
1112 | ;; | |
1113 | ;; The following isn't quite right, but it's close enough. | |
1114 | ||
1115 | (list (concat "\\<\\(" | |
1116 | "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|" | |
1117 | "[0-9]+\\(\\.[0-9]*\\|\\)" | |
1118 | "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)" | |
1119 | "[uUlL]*") | |
1120 | '(0 mdw-number-face)) | |
1121 | ||
1122 | ;; --- And anything else is punctuation --- | |
1123 | ||
1124 | (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" | |
1125 | '(0 mdw-punct-face)))))) | |
1126 | ||
1127 | ;;;----- Perl programming style --------------------------------------------- | |
1128 | ||
1129 | ;; --- Perl indentation style --- | |
1130 | ||
1131 | (setq cperl-tab-always-indent nil) | |
1132 | ||
1133 | (setq cperl-indent-level 2) | |
1134 | (setq cperl-continued-statement-offset 2) | |
1135 | (setq cperl-continued-brace-offset 0) | |
1136 | (setq cperl-brace-offset -2) | |
1137 | (setq cperl-brace-imaginary-offset 0) | |
1138 | (setq cperl-label-offset 0) | |
1139 | ||
1140 | ;; --- Define perl fontification style --- | |
1141 | ||
1142 | (defun mdw-fontify-perl () | |
1143 | ||
1144 | ;; --- Miscellaneous fiddling --- | |
1145 | ||
1146 | (modify-syntax-entry ?_ "w") | |
1147 | (modify-syntax-entry ?$ "\\") | |
1148 | (modify-syntax-entry ?$ "\\" font-lock-syntax-table) | |
1149 | (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)") | |
1150 | ||
1151 | ;; --- Now define fontification things --- | |
1152 | ||
02109a0d | 1153 | (make-local-variable 'font-lock-keywords) |
f617db13 MW |
1154 | (let ((perl-keywords |
1155 | (make-regexp '("and" "cmp" "continue" "do" "else" "elsif" "eq" | |
1156 | "for" "foreach" "ge" "gt" "goto" "if" | |
1157 | "last" "le" "lt" "local" "my" "ne" "next" "or" | |
1158 | "package" "redo" "require" "return" "sub" | |
1159 | "undef" "unless" "until" "use" "while")))) | |
1160 | ||
1161 | (setq font-lock-keywords | |
1162 | (list | |
f617db13 MW |
1163 | |
1164 | ;; --- Set up the keywords defined above --- | |
1165 | ||
1166 | (list (concat "\\<\\(" perl-keywords "\\)\\>") | |
1167 | '(0 font-lock-keyword-face)) | |
1168 | ||
1169 | ;; --- At least numbers are simpler than C --- | |
1170 | ||
1171 | (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|" | |
1172 | "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)" | |
1173 | "\\([eE]\\([-+]\\|\\)[0-9_]+\\|\\)") | |
1174 | '(0 mdw-number-face)) | |
1175 | ||
1176 | ;; --- And anything else is punctuation --- | |
1177 | ||
1178 | (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" | |
1179 | '(0 mdw-punct-face)))))) | |
1180 | ||
1181 | (defun perl-number-tests (&optional arg) | |
1182 | "Assign consecutive numbers to lines containing `#t'. With ARG, | |
1183 | strip numbers instead." | |
1184 | (interactive "P") | |
1185 | (save-excursion | |
1186 | (goto-char (point-min)) | |
1187 | (let ((i 0) (fmt (if arg "" " %4d"))) | |
1188 | (while (search-forward "#t" nil t) | |
1189 | (delete-region (point) (line-end-position)) | |
1190 | (setq i (1+ i)) | |
1191 | (insert (format fmt i))) | |
1192 | (goto-char (point-min)) | |
1193 | (if (re-search-forward "\\(tests\\s-*=>\\s-*\\)\\w*" nil t) | |
1194 | (replace-match (format "\\1%d" i)))))) | |
1195 | ||
1196 | ;;;----- Python programming style ------------------------------------------- | |
1197 | ||
1198 | ;; --- Define Python fontification style --- | |
1199 | ||
1200 | (trap (require 'pyrex-mode)) | |
1201 | (defun mdw-fontify-python () | |
1202 | ||
1203 | ;; --- Miscellaneous fiddling --- | |
1204 | ||
1205 | (modify-syntax-entry ?_ "w") | |
1206 | (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)") | |
1207 | ||
1208 | ;; --- Now define fontification things --- | |
1209 | ||
02109a0d | 1210 | (make-local-variable 'font-lock-keywords) |
f617db13 MW |
1211 | (let ((python-keywords |
1212 | (make-regexp '("and" "as" "assert" "break" "class" "continue" "def" | |
1213 | "del" "elif" "else" "except" "exec" "finally" "for" | |
1214 | "from" "global" "if" "import" "in" "is" "lambda" | |
1215 | "not" "or" "pass" "print" "raise" "return" "try" | |
043e413b | 1216 | "while" "yield")))) |
f617db13 MW |
1217 | (setq font-lock-keywords |
1218 | (list | |
f617db13 MW |
1219 | |
1220 | ;; --- Set up the keywords defined above --- | |
1221 | ||
1222 | (list (concat "\\<\\(" python-keywords "\\)\\>") | |
1223 | '(0 font-lock-keyword-face)) | |
1224 | ||
1225 | ;; --- At least numbers are simpler than C --- | |
1226 | ||
1227 | (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|" | |
1228 | "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)" | |
1229 | "\\([eE]\\([-+]\\|\\)[0-9_]+\\|[lL]\\|\\)") | |
1230 | '(0 mdw-number-face)) | |
1231 | ||
1232 | ;; --- And anything else is punctuation --- | |
1233 | ||
1234 | (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" | |
1235 | '(0 mdw-punct-face)))))) | |
1236 | ||
1237 | ;;;----- ARM assembler programming configuration ---------------------------- | |
1238 | ||
1239 | ;; --- There doesn't appear to be an Emacs mode for this yet --- | |
1240 | ;; | |
1241 | ;; Better do something about that, I suppose. | |
1242 | ||
1243 | (defvar arm-assembler-mode-map nil) | |
1244 | (defvar arm-assembler-abbrev-table nil) | |
1245 | (defvar arm-assembler-mode-syntax-table (make-syntax-table)) | |
1246 | ||
1247 | (or arm-assembler-mode-map | |
1248 | (progn | |
1249 | (setq arm-assembler-mode-map (make-sparse-keymap)) | |
1250 | (define-key arm-assembler-mode-map "\C-m" 'arm-assembler-newline) | |
1251 | (define-key arm-assembler-mode-map [C-return] 'newline) | |
1252 | (define-key arm-assembler-mode-map "\t" 'tab-to-tab-stop))) | |
1253 | ||
1254 | (defun arm-assembler-mode () | |
1255 | "Major mode for ARM assembler programs" | |
1256 | (interactive) | |
1257 | ||
1258 | ;; --- Do standard major mode things --- | |
1259 | ||
1260 | (kill-all-local-variables) | |
1261 | (use-local-map arm-assembler-mode-map) | |
1262 | (setq local-abbrev-table arm-assembler-abbrev-table) | |
1263 | (setq major-mode 'arm-assembler-mode) | |
1264 | (setq mode-name "ARM assembler") | |
1265 | ||
1266 | ;; --- Set up syntax table --- | |
1267 | ||
1268 | (set-syntax-table arm-assembler-mode-syntax-table) | |
1269 | (modify-syntax-entry ?; ; Nasty hack | |
1270 | "<" arm-assembler-mode-syntax-table) | |
1271 | (modify-syntax-entry ?\n ">" arm-assembler-mode-syntax-table) | |
1272 | (modify-syntax-entry ?_ "_" arm-assembler-mode-syntax-table) | |
1273 | ||
1274 | (make-local-variable 'comment-start) | |
1275 | (setq comment-start ";") | |
1276 | (make-local-variable 'comment-end) | |
1277 | (setq comment-end "") | |
1278 | (make-local-variable 'comment-column) | |
1279 | (setq comment-column 48) | |
1280 | (make-local-variable 'comment-start-skip) | |
1281 | (setq comment-start-skip ";+[ \t]*") | |
1282 | ||
1283 | ;; --- Play with indentation --- | |
1284 | ||
1285 | (make-local-variable 'indent-line-function) | |
1286 | (setq indent-line-function 'indent-relative-maybe) | |
1287 | ||
1288 | ;; --- Set fill prefix --- | |
1289 | ||
1290 | (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)") | |
1291 | ||
1292 | ;; --- Fiddle with fontification --- | |
1293 | ||
02109a0d | 1294 | (make-local-variable 'font-lock-keywords) |
f617db13 MW |
1295 | (setq font-lock-keywords |
1296 | (list | |
f617db13 MW |
1297 | |
1298 | ;; --- Handle numbers too --- | |
1299 | ;; | |
1300 | ;; The following isn't quite right, but it's close enough. | |
1301 | ||
1302 | (list (concat "\\(" | |
1303 | "&[0-9a-fA-F]+\\|" | |
1304 | "\\<[0-9]+\\(\\.[0-9]*\\|_[0-9a-zA-Z]+\\|\\)" | |
1305 | "\\)") | |
1306 | '(0 mdw-number-face)) | |
1307 | ||
1308 | ;; --- Do something about operators --- | |
1309 | ||
1310 | (list "^[^ \t]*[ \t]+\\(GET\\|LNK\\)[ \t]+\\([^;\n]*\\)" | |
1311 | '(1 font-lock-keyword-face) | |
1312 | '(2 font-lock-string-face)) | |
1313 | (list ":[a-zA-Z]+:" | |
1314 | '(0 font-lock-keyword-face)) | |
1315 | ||
1316 | ;; --- Do menemonics and directives --- | |
1317 | ||
1318 | (list "^[^ \t]*[ \t]+\\([a-zA-Z]+\\)" | |
1319 | '(1 font-lock-keyword-face)) | |
1320 | ||
1321 | ;; --- And anything else is punctuation --- | |
1322 | ||
1323 | (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" | |
1324 | '(0 mdw-punct-face)))) | |
1325 | ||
1326 | (run-hooks 'arm-assembler-mode-hook)) | |
1327 | ||
30c8a8fb MW |
1328 | ;;;----- Assembler mode ----------------------------------------------------- |
1329 | ||
1330 | (defun mdw-fontify-asm () | |
1331 | (modify-syntax-entry ?' "\"") | |
1332 | (modify-syntax-entry ?. "w") | |
1333 | (setf fill-prefix nil) | |
1334 | (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)")) | |
1335 | ||
f617db13 MW |
1336 | ;;;----- TCL configuration -------------------------------------------------- |
1337 | ||
1338 | (defun mdw-fontify-tcl () | |
1339 | (mapcar #'(lambda (ch) (modify-syntax-entry ch ".")) '(?$)) | |
1340 | (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)") | |
02109a0d | 1341 | (make-local-variable 'font-lock-keywords) |
f617db13 MW |
1342 | (setq font-lock-keywords |
1343 | (list | |
f617db13 MW |
1344 | (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|" |
1345 | "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)" | |
1346 | "\\([eE]\\([-+]\\|\\)[0-9_]+\\|\\)") | |
1347 | '(0 mdw-number-face)) | |
1348 | (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" | |
1349 | '(0 mdw-punct-face))))) | |
1350 | ||
1351 | ;;;----- REXX configuration ------------------------------------------------- | |
1352 | ||
1353 | (defun mdw-rexx-electric-* () | |
1354 | (interactive) | |
1355 | (insert ?*) | |
1356 | (rexx-indent-line)) | |
1357 | ||
1358 | (defun mdw-rexx-indent-newline-indent () | |
1359 | (interactive) | |
1360 | (rexx-indent-line) | |
1361 | (if abbrev-mode (expand-abbrev)) | |
1362 | (newline-and-indent)) | |
1363 | ||
1364 | (defun mdw-fontify-rexx () | |
1365 | ||
1366 | ;; --- Various bits of fiddling --- | |
1367 | ||
1368 | (setq mdw-auto-indent nil) | |
1369 | (local-set-key [?\C-m] 'mdw-rexx-indent-newline-indent) | |
1370 | (local-set-key [?*] 'mdw-rexx-electric-*) | |
1371 | (mapcar #'(lambda (ch) (modify-syntax-entry ch "w")) | |
1372 | '(?. ?! ?? ?_ ?# ?@ ?$)) | |
1373 | (mdw-standard-fill-prefix "\\([ \t]*/?\*[ \t]*\\)") | |
1374 | ||
1375 | ;; --- Set up keywords and things for fontification --- | |
1376 | ||
1377 | (make-local-variable 'font-lock-keywords-case-fold-search) | |
1378 | (setq font-lock-keywords-case-fold-search t) | |
1379 | ||
1380 | (setq rexx-indent 2) | |
1381 | (setq rexx-end-indent rexx-indent) | |
1382 | (setq rexx-tab-always-indent nil) | |
1383 | (setq rexx-cont-indent rexx-indent) | |
1384 | ||
02109a0d | 1385 | (make-local-variable 'font-lock-keywords) |
f617db13 MW |
1386 | (let ((rexx-keywords |
1387 | (make-regexp '("address" "arg" "by" "call" "digits" "do" "drop" | |
1388 | "else" "end" "engineering" "exit" "expose" "for" | |
1389 | "forever" "form" "fuzz" "if" "interpret" "iterate" | |
1390 | "leave" "linein" "name" "nop" "numeric" "off" "on" | |
1391 | "options" "otherwise" "parse" "procedure" "pull" | |
1392 | "push" "queue" "return" "say" "select" "signal" | |
1393 | "scientific" "source" "then" "trace" "to" "until" | |
1394 | "upper" "value" "var" "version" "when" "while" | |
1395 | "with" | |
1396 | ||
1397 | "abbrev" "abs" "bitand" "bitor" "bitxor" "b2x" | |
1398 | "center" "center" "charin" "charout" "chars" | |
1399 | "compare" "condition" "copies" "c2d" "c2x" | |
1400 | "datatype" "date" "delstr" "delword" "d2c" "d2x" | |
1401 | "errortext" "format" "fuzz" "insert" "lastpos" | |
1402 | "left" "length" "lineout" "lines" "max" "min" | |
1403 | "overlay" "pos" "queued" "random" "reverse" "right" | |
1404 | "sign" "sourceline" "space" "stream" "strip" | |
1405 | "substr" "subword" "symbol" "time" "translate" | |
1406 | "trunc" "value" "verify" "word" "wordindex" | |
1407 | "wordlength" "wordpos" "words" "xrange" "x2b" "x2c" | |
1408 | "x2d")))) | |
1409 | ||
1410 | (setq font-lock-keywords | |
1411 | (list | |
f617db13 MW |
1412 | |
1413 | ;; --- Set up the keywords defined above --- | |
1414 | ||
1415 | (list (concat "\\<\\(" rexx-keywords "\\)\\>") | |
1416 | '(0 font-lock-keyword-face)) | |
1417 | ||
1418 | ;; --- Fontify all symbols the same way --- | |
1419 | ||
1420 | (list (concat "\\<\\([0-9.][A-Za-z0-9.!?_#@$]*[Ee][+-]?[0-9]+\\|" | |
1421 | "[A-Za-z0-9.!?_#@$]+\\)") | |
1422 | '(0 font-lock-variable-name-face)) | |
1423 | ||
1424 | ;; --- And everything else is punctuation --- | |
1425 | ||
1426 | (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" | |
1427 | '(0 mdw-punct-face)))))) | |
1428 | ||
1429 | ;;;----- Standard ML programming style -------------------------------------- | |
1430 | ||
1431 | (defun mdw-fontify-sml () | |
1432 | ||
1433 | ;; --- Make underscore an honorary letter --- | |
1434 | ||
1435 | (modify-syntax-entry ?_ "w") | |
1436 | (modify-syntax-entry ?' "w") | |
1437 | ||
1438 | ;; --- Set fill prefix --- | |
1439 | ||
1440 | (mdw-standard-fill-prefix "\\([ \t]*(\*[ \t]*\\)") | |
1441 | ||
1442 | ;; --- Now define fontification things --- | |
1443 | ||
02109a0d | 1444 | (make-local-variable 'font-lock-keywords) |
f617db13 MW |
1445 | (let ((sml-keywords |
1446 | (make-regexp '("abstype" "and" "andalso" "as" | |
1447 | "case" | |
1448 | "datatype" "do" | |
1449 | "else" "end" "eqtype" "exception" | |
1450 | "fn" "fun" "functor" | |
1451 | "handle" | |
1452 | "if" "in" "include" "infix" "infixr" | |
1453 | "let" "local" | |
1454 | "nonfix" | |
1455 | "of" "op" "open" "orelse" | |
1456 | "raise" "rec" | |
1457 | "sharing" "sig" "signature" "struct" "structure" | |
1458 | "then" "type" | |
1459 | "val" | |
1460 | "where" "while" "with" "withtype")))) | |
1461 | ||
1462 | (setq font-lock-keywords | |
1463 | (list | |
f617db13 MW |
1464 | |
1465 | ;; --- Set up the keywords defined above --- | |
1466 | ||
1467 | (list (concat "\\<\\(" sml-keywords "\\)\\>") | |
1468 | '(0 font-lock-keyword-face)) | |
1469 | ||
1470 | ;; --- At least numbers are simpler than C --- | |
1471 | ||
1472 | (list (concat "\\<\\(\\~\\|\\)" | |
1473 | "\\(0\\(\\([wW]\\|\\)[xX][0-9a-fA-F]+\\|" | |
852cd5fb MW |
1474 | "[wW][0-9]+\\)\\|" |
1475 | "\\([0-9]+\\(\\.[0-9]+\\|\\)" | |
1476 | "\\([eE]\\(\\~\\|\\)" | |
1477 | "[0-9]+\\|\\)\\)\\)") | |
f617db13 MW |
1478 | '(0 mdw-number-face)) |
1479 | ||
1480 | ;; --- And anything else is punctuation --- | |
1481 | ||
1482 | (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" | |
1483 | '(0 mdw-punct-face)))))) | |
1484 | ||
1485 | ;;;----- Haskell configuration ---------------------------------------------- | |
1486 | ||
1487 | (defun mdw-fontify-haskell () | |
1488 | ||
1489 | ;; --- Fiddle with syntax table to get comments right --- | |
1490 | ||
1491 | (modify-syntax-entry ?_ "w") | |
1492 | (modify-syntax-entry ?' "\"") | |
1493 | (modify-syntax-entry ?- ". 123") | |
1494 | (modify-syntax-entry ?{ ". 1b") | |
1495 | (modify-syntax-entry ?} ". 4b") | |
1496 | (modify-syntax-entry ?\n ">") | |
1497 | ||
1498 | ;; --- Set fill prefix --- | |
1499 | ||
1500 | (mdw-standard-fill-prefix "\\([ \t]*{?--?[ \t]*\\)") | |
1501 | ||
1502 | ;; --- Fiddle with fontification --- | |
1503 | ||
02109a0d | 1504 | (make-local-variable 'font-lock-keywords) |
f617db13 MW |
1505 | (let ((haskell-keywords |
1506 | (make-regexp '("as" "case" "ccall" "class" "data" "default" | |
1507 | "deriving" "do" "else" "foreign" "hiding" "if" | |
1508 | "import" "in" "infix" "infixl" "infixr" "instance" | |
1509 | "let" "module" "newtype" "of" "qualified" "safe" | |
1510 | "stdcall" "then" "type" "unsafe" "where")))) | |
1511 | ||
1512 | (setq font-lock-keywords | |
1513 | (list | |
f617db13 MW |
1514 | (list "--.*$" |
1515 | '(0 font-lock-comment-face)) | |
1516 | (list (concat "\\<\\(" haskell-keywords "\\)\\>") | |
1517 | '(0 font-lock-keyword-face)) | |
1518 | (list (concat "\\<0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|" | |
1519 | "\\<[0-9][0-9_]*\\(\\.[0-9]*\\|\\)" | |
1520 | "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)") | |
1521 | '(0 mdw-number-face)) | |
1522 | (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" | |
1523 | '(0 mdw-punct-face)))))) | |
1524 | ||
1525 | ;;;----- Texinfo configuration ---------------------------------------------- | |
1526 | ||
1527 | (defun mdw-fontify-texinfo () | |
1528 | ||
1529 | ;; --- Set fill prefix --- | |
1530 | ||
1531 | (mdw-standard-fill-prefix "\\([ \t]*@c[ \t]+\\)") | |
1532 | ||
1533 | ;; --- Real fontification things --- | |
1534 | ||
02109a0d | 1535 | (make-local-variable 'font-lock-keywords) |
f617db13 MW |
1536 | (setq font-lock-keywords |
1537 | (list | |
f617db13 MW |
1538 | |
1539 | ;; --- Environment names are keywords --- | |
1540 | ||
1541 | (list "@\\(end\\) *\\([a-zA-Z]*\\)?" | |
1542 | '(2 font-lock-keyword-face)) | |
1543 | ||
1544 | ;; --- Unmark escaped magic characters --- | |
1545 | ||
1546 | (list "\\(@\\)\\([@{}]\\)" | |
1547 | '(1 font-lock-keyword-face) | |
1548 | '(2 font-lock-variable-name-face)) | |
1549 | ||
1550 | ;; --- Make sure we get comments properly --- | |
1551 | ||
1552 | (list "@c\\(\\|omment\\)\\( .*\\)?$" | |
1553 | '(0 font-lock-comment-face)) | |
1554 | ||
1555 | ;; --- Command names are keywords --- | |
1556 | ||
1557 | (list "@\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)" | |
1558 | '(0 font-lock-keyword-face)) | |
1559 | ||
1560 | ;; --- Fontify TeX special characters as punctuation --- | |
1561 | ||
1562 | (list "[{}]+" | |
1563 | '(0 mdw-punct-face))))) | |
1564 | ||
1565 | ;;;----- TeX and LaTeX configuration ---------------------------------------- | |
1566 | ||
1567 | (defun mdw-fontify-tex () | |
1568 | (setq ispell-parser 'tex) | |
1569 | ||
1570 | ;; --- Don't make maths into a string --- | |
1571 | ||
1572 | (modify-syntax-entry ?$ ".") | |
1573 | (modify-syntax-entry ?$ "." font-lock-syntax-table) | |
1574 | (local-set-key [?$] 'self-insert-command) | |
1575 | ||
1576 | ;; --- Set fill prefix --- | |
1577 | ||
1578 | (mdw-standard-fill-prefix "\\([ \t]*%+[ \t]*\\)") | |
1579 | ||
1580 | ;; --- Real fontification things --- | |
1581 | ||
02109a0d | 1582 | (make-local-variable 'font-lock-keywords) |
f617db13 MW |
1583 | (setq font-lock-keywords |
1584 | (list | |
f617db13 MW |
1585 | |
1586 | ;; --- Environment names are keywords --- | |
1587 | ||
1588 | (list (concat "\\\\\\(begin\\|end\\|newenvironment\\)" | |
1589 | "{\\([^}\n]*\\)}") | |
1590 | '(2 font-lock-keyword-face)) | |
1591 | ||
1592 | ;; --- Suspended environment names are keywords too --- | |
1593 | ||
1594 | (list (concat "\\\\\\(suspend\\|resume\\)\\(\\[[^]]*\\]\\)?" | |
1595 | "{\\([^}\n]*\\)}") | |
1596 | '(3 font-lock-keyword-face)) | |
1597 | ||
1598 | ;; --- Command names are keywords --- | |
1599 | ||
1600 | (list "\\\\\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)" | |
1601 | '(0 font-lock-keyword-face)) | |
1602 | ||
1603 | ;; --- Handle @/.../ for italics --- | |
1604 | ||
1605 | ;; (list "\\(@/\\)\\([^/]*\\)\\(/\\)" | |
852cd5fb MW |
1606 | ;; '(1 font-lock-keyword-face) |
1607 | ;; '(3 font-lock-keyword-face)) | |
f617db13 MW |
1608 | |
1609 | ;; --- Handle @*...* for boldness --- | |
1610 | ||
1611 | ;; (list "\\(@\\*\\)\\([^*]*\\)\\(\\*\\)" | |
852cd5fb MW |
1612 | ;; '(1 font-lock-keyword-face) |
1613 | ;; '(3 font-lock-keyword-face)) | |
f617db13 MW |
1614 | |
1615 | ;; --- Handle @`...' for literal syntax things --- | |
1616 | ||
1617 | ;; (list "\\(@`\\)\\([^']*\\)\\('\\)" | |
852cd5fb MW |
1618 | ;; '(1 font-lock-keyword-face) |
1619 | ;; '(3 font-lock-keyword-face)) | |
f617db13 MW |
1620 | |
1621 | ;; --- Handle @<...> for nonterminals --- | |
1622 | ||
1623 | ;; (list "\\(@<\\)\\([^>]*\\)\\(>\\)" | |
852cd5fb MW |
1624 | ;; '(1 font-lock-keyword-face) |
1625 | ;; '(3 font-lock-keyword-face)) | |
f617db13 MW |
1626 | |
1627 | ;; --- Handle other @-commands --- | |
1628 | ||
1629 | ;; (list "@\\([^a-zA-Z]\\|[a-zA-Z]*\\)" | |
852cd5fb | 1630 | ;; '(0 font-lock-keyword-face)) |
f617db13 MW |
1631 | |
1632 | ;; --- Make sure we get comments properly --- | |
1633 | ||
1634 | (list "%.*" | |
1635 | '(0 font-lock-comment-face)) | |
1636 | ||
1637 | ;; --- Fontify TeX special characters as punctuation --- | |
1638 | ||
1639 | (list "[$^_{}#&]" | |
1640 | '(0 mdw-punct-face))))) | |
1641 | ||
1642 | ;;;----- Shell scripts ------------------------------------------------------ | |
1643 | ||
1644 | (defun mdw-setup-sh-script-mode () | |
1645 | ||
1646 | ;; --- Fetch the shell interpreter's name --- | |
1647 | ||
1648 | (let ((shell-name sh-shell-file)) | |
1649 | ||
1650 | ;; --- Try reading the hash-bang line --- | |
1651 | ||
1652 | (save-excursion | |
1653 | (goto-char (point-min)) | |
1654 | (if (looking-at "#![ \t]*\\([^ \t\n]*\\)") | |
1655 | (setq shell-name (match-string 1)))) | |
1656 | ||
1657 | ;; --- Now try to set the shell --- | |
1658 | ;; | |
1659 | ;; Don't let `sh-set-shell' bugger up my script. | |
1660 | ||
1661 | (let ((executable-set-magic #'(lambda (s &rest r) s))) | |
1662 | (sh-set-shell shell-name))) | |
1663 | ||
1664 | ;; --- Now enable my keys and the fontification --- | |
1665 | ||
1666 | (mdw-misc-mode-config) | |
1667 | ||
1668 | ;; --- Set the indentation level correctly --- | |
1669 | ||
1670 | (setq sh-indentation 2) | |
1671 | (setq sh-basic-offset 2)) | |
1672 | ||
1673 | ;;;----- Messages-file mode ------------------------------------------------- | |
1674 | ||
1675 | (defun message-mode-guts () | |
1676 | (setq messages-mode-syntax-table (make-syntax-table)) | |
1677 | (set-syntax-table messages-mode-syntax-table) | |
1678 | (modify-syntax-entry ?_ "w" messages-mode-syntax-table) | |
1679 | (modify-syntax-entry ?- "w" messages-mode-syntax-table) | |
1680 | (modify-syntax-entry ?0 "w" messages-mode-syntax-table) | |
1681 | (modify-syntax-entry ?1 "w" messages-mode-syntax-table) | |
1682 | (modify-syntax-entry ?2 "w" messages-mode-syntax-table) | |
1683 | (modify-syntax-entry ?3 "w" messages-mode-syntax-table) | |
1684 | (modify-syntax-entry ?4 "w" messages-mode-syntax-table) | |
1685 | (modify-syntax-entry ?5 "w" messages-mode-syntax-table) | |
1686 | (modify-syntax-entry ?6 "w" messages-mode-syntax-table) | |
1687 | (modify-syntax-entry ?7 "w" messages-mode-syntax-table) | |
1688 | (modify-syntax-entry ?8 "w" messages-mode-syntax-table) | |
1689 | (modify-syntax-entry ?9 "w" messages-mode-syntax-table) | |
1690 | (make-local-variable 'comment-start) | |
1691 | (make-local-variable 'comment-end) | |
1692 | (make-local-variable 'indent-line-function) | |
1693 | (setq indent-line-function 'indent-relative) | |
1694 | (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)") | |
1695 | (make-local-variable 'font-lock-defaults) | |
1696 | (make-local-variable 'message-mode-keywords) | |
1697 | (let ((keywords | |
1698 | (make-regexp '("array" "bitmap" "callback" "docs[ \t]+enum" | |
1699 | "export" "enum" "fixed-octetstring" "flags" | |
1700 | "harmless" "map" "nested" "optional" | |
1701 | "optional-tagged" "package" "primitive" | |
1702 | "primitive-nullfree" "relaxed[ \t]+enum" | |
1703 | "set" "table" "tagged-optional" "union" | |
1704 | "variadic" "vector" "version" "version-tag")))) | |
1705 | (setq message-mode-keywords | |
1706 | (list | |
1707 | (list (concat "\\<\\(" keywords "\\)\\>:") | |
1708 | '(0 font-lock-keyword-face)) | |
1709 | '("\\([-a-zA-Z0-9]+:\\)" (0 font-lock-warning-face)) | |
1710 | '("\\(\\<[a-z][-_a-zA-Z0-9]*\\)" | |
1711 | (0 font-lock-variable-name-face)) | |
1712 | '("\\<\\([0-9]+\\)\\>" (0 mdw-number-face)) | |
1713 | '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" | |
1714 | (0 mdw-punct-face))))) | |
1715 | (setq font-lock-defaults | |
1716 | '(message-mode-keywords nil nil nil nil)) | |
1717 | (run-hooks 'messages-file-hook)) | |
1718 | ||
1719 | (defun messages-mode () | |
1720 | (interactive) | |
1721 | (fundamental-mode) | |
1722 | (setq major-mode 'messages-mode) | |
1723 | (setq mode-name "Messages") | |
1724 | (message-mode-guts) | |
1725 | (modify-syntax-entry ?# "<" messages-mode-syntax-table) | |
1726 | (modify-syntax-entry ?\n ">" messages-mode-syntax-table) | |
1727 | (setq comment-start "# ") | |
1728 | (setq comment-end "") | |
1729 | (turn-on-font-lock-if-enabled) | |
1730 | (run-hooks 'messages-mode-hook)) | |
1731 | ||
1732 | (defun cpp-messages-mode () | |
1733 | (interactive) | |
1734 | (fundamental-mode) | |
1735 | (setq major-mode 'cpp-messages-mode) | |
1736 | (setq mode-name "CPP Messages") | |
1737 | (message-mode-guts) | |
1738 | (modify-syntax-entry ?* ". 23" messages-mode-syntax-table) | |
1739 | (modify-syntax-entry ?/ ". 14" messages-mode-syntax-table) | |
1740 | (setq comment-start "/* ") | |
1741 | (setq comment-end " */") | |
1742 | (let ((preprocessor-keywords | |
1743 | (make-regexp '("assert" "define" "elif" "else" "endif" "error" | |
1744 | "ident" "if" "ifdef" "ifndef" "import" "include" | |
1745 | "line" "pragma" "unassert" "undef" "warning")))) | |
1746 | (setq message-mode-keywords | |
1747 | (append (list (list (concat "^[ \t]*\\#[ \t]*" | |
1748 | "\\(include\\|import\\)" | |
1749 | "[ \t]*\\(<[^>]+\\(>\\|\\)\\)") | |
1750 | '(2 font-lock-string-face)) | |
1751 | (list (concat "^\\([ \t]*#[ \t]*\\(\\(" | |
1752 | preprocessor-keywords | |
852cd5fb | 1753 | "\\)\\>\\|[0-9]+\\|$\\)\\)") |
f617db13 MW |
1754 | '(1 font-lock-keyword-face))) |
1755 | message-mode-keywords))) | |
f617db13 | 1756 | (turn-on-font-lock-if-enabled) |
297d60aa | 1757 | (run-hooks 'cpp-messages-mode-hook)) |
f617db13 | 1758 | |
297d60aa MW |
1759 | (add-hook 'messages-mode-hook 'mdw-misc-mode-config t) |
1760 | (add-hook 'cpp-messages-mode-hook 'mdw-misc-mode-config t) | |
f617db13 MW |
1761 | ; (add-hook 'messages-file-hook 'mdw-fontify-messages t) |
1762 | ||
1763 | ;;;----- Messages-file mode ------------------------------------------------- | |
1764 | ||
1765 | (defvar mallow-driver-substitution-face 'mallow-driver-substitution-face | |
1766 | "Face to use for subsittution directives.") | |
1767 | (make-face 'mallow-driver-substitution-face) | |
1768 | (defvar mallow-driver-text-face 'mallow-driver-text-face | |
1769 | "Face to use for body text.") | |
1770 | (make-face 'mallow-driver-text-face) | |
1771 | ||
1772 | (defun mallow-driver-mode () | |
1773 | (interactive) | |
1774 | (fundamental-mode) | |
1775 | (setq major-mode 'mallow-driver-mode) | |
1776 | (setq mode-name "Mallow driver") | |
1777 | (setq mallow-driver-mode-syntax-table (make-syntax-table)) | |
1778 | (set-syntax-table mallow-driver-mode-syntax-table) | |
1779 | (make-local-variable 'comment-start) | |
1780 | (make-local-variable 'comment-end) | |
1781 | (make-local-variable 'indent-line-function) | |
1782 | (setq indent-line-function 'indent-relative) | |
1783 | (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)") | |
1784 | (make-local-variable 'font-lock-defaults) | |
1785 | (make-local-variable 'mallow-driver-mode-keywords) | |
1786 | (let ((keywords | |
1787 | (make-regexp '("each" "divert" "file" "if" | |
1788 | "perl" "set" "string" "type" "write")))) | |
1789 | (setq mallow-driver-mode-keywords | |
1790 | (list | |
1791 | (list (concat "^%\\s *\\(}\\|\\(" keywords "\\)\\>\\).*$") | |
1792 | '(0 font-lock-keyword-face)) | |
1793 | (list "^%\\s *\\(#.*\\|\\)$" | |
1794 | '(0 font-lock-comment-face)) | |
1795 | (list "^%" | |
1796 | '(0 font-lock-keyword-face)) | |
1797 | (list "^|?\\(.+\\)$" '(1 mallow-driver-text-face)) | |
1798 | (list "\\${[^}]*}" | |
1799 | '(0 mallow-driver-substitution-face t))))) | |
1800 | (setq font-lock-defaults | |
1801 | '(mallow-driver-mode-keywords nil nil nil nil)) | |
1802 | (modify-syntax-entry ?\" "_" mallow-driver-mode-syntax-table) | |
1803 | (modify-syntax-entry ?\n ">" mallow-driver-mode-syntax-table) | |
1804 | (setq comment-start "%# ") | |
1805 | (setq comment-end "") | |
1806 | (turn-on-font-lock-if-enabled) | |
1807 | (run-hooks 'mallow-driver-mode-hook)) | |
1808 | ||
1809 | (add-hook 'mallow-driver-hook 'mdw-misc-mode-config t) | |
1810 | ||
1811 | ;;;----- NFast debugs ------------------------------------------------------- | |
1812 | ||
1813 | (defun nfast-debug-mode () | |
1814 | (interactive) | |
1815 | (fundamental-mode) | |
1816 | (setq major-mode 'nfast-debug-mode) | |
1817 | (setq mode-name "NFast debug") | |
1818 | (setq messages-mode-syntax-table (make-syntax-table)) | |
1819 | (set-syntax-table messages-mode-syntax-table) | |
1820 | (make-local-variable 'font-lock-defaults) | |
1821 | (make-local-variable 'nfast-debug-mode-keywords) | |
1822 | (setq truncate-lines t) | |
1823 | (setq nfast-debug-mode-keywords | |
1824 | (list | |
1825 | '("^\\(NFast_\\(Connect\\|Disconnect\\|Submit\\|Wait\\)\\)" | |
1826 | (0 font-lock-keyword-face)) | |
1827 | (list (concat "^[ \t]+\\(\\(" | |
1828 | "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]" | |
1829 | "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]" | |
1830 | "[ \t]+\\)*" | |
1831 | "[0-9a-fA-F]+\\)[ \t]*$") | |
1832 | '(0 mdw-number-face)) | |
1833 | '("^[ \t]+\.status=[ \t]+\\<\\(OK\\)\\>" | |
1834 | (1 font-lock-keyword-face)) | |
1835 | '("^[ \t]+\.status=[ \t]+\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>" | |
1836 | (1 font-lock-warning-face)) | |
1837 | '("^[ \t]+\.status[ \t]+\\<\\(zero\\)\\>" | |
1838 | (1 nil)) | |
1839 | (list (concat "^[ \t]+\\.cmd=[ \t]+" | |
1840 | "\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>") | |
1841 | '(1 font-lock-keyword-face)) | |
1842 | '("-?\\<\\([0-9]+\\|0x[0-9a-fA-F]+\\)\\>" (0 mdw-number-face)) | |
1843 | '("^\\([ \t]+[a-z0-9.]+\\)" (0 font-lock-variable-name-face)) | |
1844 | '("\\<\\([a-z][a-z0-9.]+\\)\\>=" (1 font-lock-variable-name-face)) | |
1845 | '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" (0 mdw-punct-face)))) | |
1846 | (setq font-lock-defaults | |
1847 | '(nfast-debug-mode-keywords nil nil nil nil)) | |
1848 | (turn-on-font-lock-if-enabled) | |
1849 | (run-hooks 'nfast-debug-mode-hook)) | |
1850 | ||
1851 | ;;;----- Other languages ---------------------------------------------------- | |
1852 | ||
1853 | ;; --- Smalltalk --- | |
1854 | ||
1855 | (defun mdw-setup-smalltalk () | |
1856 | (and mdw-auto-indent | |
1857 | (local-set-key "\C-m" 'smalltalk-newline-and-indent)) | |
1858 | (make-variable-buffer-local 'mdw-auto-indent) | |
1859 | (setq mdw-auto-indent nil) | |
1860 | (local-set-key "\C-i" 'smalltalk-reindent)) | |
1861 | ||
1862 | (defun mdw-fontify-smalltalk () | |
02109a0d | 1863 | (make-local-variable 'font-lock-keywords) |
f617db13 MW |
1864 | (setq font-lock-keywords |
1865 | (list | |
f617db13 MW |
1866 | (list "\\<[A-Z][a-zA-Z0-9]*\\>" |
1867 | '(0 font-lock-keyword-face)) | |
1868 | (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|" | |
1869 | "[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)" | |
1870 | "\\([eE]\\([-+]\\|\\)[0-9_]+\\|\\)") | |
1871 | '(0 mdw-number-face)) | |
1872 | (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" | |
1873 | '(0 mdw-punct-face))))) | |
1874 | ||
1875 | ;; --- Lispy languages --- | |
1876 | ||
1877 | (defun mdw-indent-newline-and-indent () | |
1878 | (interactive) | |
1879 | (indent-for-tab-command) | |
1880 | (newline-and-indent)) | |
1881 | ||
1882 | (eval-after-load "cl-indent" | |
1883 | '(progn | |
1884 | (mapc #'(lambda (pair) | |
1885 | (put (car pair) | |
1886 | 'common-lisp-indent-function | |
1887 | (cdr pair))) | |
1888 | '((destructuring-bind . ((&whole 4 &rest 1) 4 &body)) | |
1889 | (multiple-value-bind . ((&whole 4 &rest 1) 4 &body)))))) | |
1890 | ||
1891 | (defun mdw-common-lisp-indent () | |
1892 | (make-variable-buffer-local 'lisp-indent-function) | |
1893 | (setq lisp-indent-function 'common-lisp-indent-function)) | |
1894 | ||
1895 | (defun mdw-fontify-lispy () | |
1896 | ||
1897 | ;; --- Set fill prefix --- | |
1898 | ||
1899 | (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)") | |
1900 | ||
1901 | ;; --- Not much fontification needed --- | |
1902 | ||
02109a0d | 1903 | (make-local-variable 'font-lock-keywords) |
f617db13 MW |
1904 | (setq font-lock-keywords |
1905 | (list | |
f617db13 MW |
1906 | (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" |
1907 | '(0 mdw-punct-face))))) | |
1908 | ||
1909 | (defun comint-send-and-indent () | |
1910 | (interactive) | |
1911 | (comint-send-input) | |
1912 | (and mdw-auto-indent | |
1913 | (indent-for-tab-command))) | |
1914 | ||
1915 | ;;;----- Text mode ---------------------------------------------------------- | |
1916 | ||
1917 | (defun mdw-text-mode () | |
1918 | (setq fill-column 72) | |
1919 | (flyspell-mode t) | |
1920 | (mdw-standard-fill-prefix | |
1921 | "\\([ \t]*\\([A-Za-z0-9]*[>#|:] ?\\)*[ \t]*\\)" 3) | |
1922 | (auto-fill-mode 1)) | |
1923 | ||
1924 | ;;;----- Shell mode --------------------------------------------------------- | |
1925 | ||
1926 | (defun mdw-sh-mode-setup () | |
1927 | (local-set-key [?\C-a] 'comint-bol) | |
1928 | (add-hook 'comint-output-filter-functions | |
1929 | 'comint-watch-for-password-prompt)) | |
1930 | ||
1931 | (defun mdw-term-mode-setup () | |
9a3fa88e | 1932 |