5 ;;; Functions and macros for .emacs
7 ;;; (c) 2004 Mark Wooding
10 ;;;----- Licensing notice ---------------------------------------------------
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.
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.
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.
26 ;;;----- Some general utilities ---------------------------------------------
28 ;; --- Some error trapping ---
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.
33 (defmacro trap (&rest forms)
34 "Execute FORMS without allowing errors to propagate outside."
36 ,(if (cdr forms) (cons 'progn forms) (car forms))
37 (error (message "Error (trapped): %s" (error-message-string err)))))
39 ;; --- Configuration reading ---
41 (defvar mdw-config nil)
42 (defun mdw-config (sym)
43 "Read the configuration variable named SYM."
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_.]*\\)"
53 "[ \t]**\\(\n\\|$\\)")
55 nil (point-min) (point-max))
56 (car (read-from-string
57 (concat "(" (buffer-string) ")"))))))
58 (cdr (assq sym mdw-config)))
60 ;; --- Is an Emacs library available? ---
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))
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)))))
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)))
80 ;; --- Splitting windows ---
82 (defconst mdw-scrollbar-width (if window-system 6 1)
83 "Guessed width of scroll bar.")
84 (defun mdw-divvy-window (&optional w)
85 "Split a wide window into appropriate widths."
88 (let ((win (selected-window))
89 (c (/ (+ (window-width) mdw-scrollbar-width)
90 (+ w mdw-scrollbar-width))))
93 (split-window-horizontally (+ w mdw-scrollbar-width))
97 ;; --- Functions for sexp diary entries ---
99 (defun mdw-weekday (l)
100 "Return non-nil if `date' falls on one of the days of the week in L.
102 L is a list of day numbers (from 0 to 6 for Sunday through to Saturday) or
103 symbols `sunday', `monday', etc. (or a mixture). If the date stored in
104 `date' falls on a listed day, then the function returns non-nil."
105 (let ((d (calendar-day-of-week date)))
107 (memq (nth d '(sunday monday tuesday wednesday
108 thursday friday saturday)) l))))
110 (defun mdw-todo (&optional when)
111 "Return non-nil today, or on WHEN, whichever is later."
112 (let ((w (calendar-absolute-from-gregorian (calendar-current-date)))
113 (d (calendar-absolute-from-gregorian date)))
115 (setq w (max w (calendar-absolute-from-gregorian
117 ((not european-calendar-style)
129 ;;;----- Utility functions --------------------------------------------------
131 ;; --- mdw-uniquify-alist ---
133 (defun mdw-uniquify-alist (&rest alists)
135 "Return the concatenation of the ALISTS with duplicate elements removed.
137 The first association with a given key prevails; others are ignored. The
138 input lists are not modified, although they'll probably become garbage."
141 (let ((start-list (cons nil nil)))
142 (mdw-do-uniquify start-list
147 ;; --- mdw-do-uniquify ---
149 ;; The DONE argument is a list whose first element is `nil'. It contains the
150 ;; uniquified alist built so far. The leading `nil' is stripped off at the
151 ;; end of the operation; it's only there so that DONE always references a
152 ;; cons cell. END refers to the final cons cell in the DONE list; it is
153 ;; modified in place each time to avoid the overheads of `append'ing all the
154 ;; time. The L argument is the alist we're currently processing; the
155 ;; remaining alists are given in REST.
157 (defun mdw-do-uniquify (done end l rest)
158 "A helper function for mdw-uniquify-alist."
160 ;; --- There are several different cases to deal with here ---
164 ;; --- Current list isn't empty ---
166 ;; Add the first item to the DONE list if there's not an item with the
167 ;; same KEY already there.
169 (l (or (assoc (car (car l)) done)
171 (setcdr end (cons (car l) nil))
172 (setq end (cdr end))))
173 (mdw-do-uniquify done end (cdr l) rest))
175 ;; --- The list we were working on is empty ---
177 ;; Shunt the next list into the current list position and go round again.
179 (rest (mdw-do-uniquify done end (car rest) (cdr rest)))
181 ;; --- Everything's done ---
183 ;; Remove the leading `nil' from the DONE list and return it. Finished!
187 ;; --- Insert a date ---
190 "Insert the current date in a pleasing way."
192 (insert (save-excursion
193 (let ((buffer (get-buffer-create "*tmp*")))
194 (unwind-protect (progn (set-buffer buffer)
196 (shell-command "date +%Y-%m-%d" t)
198 (delete-backward-char 1)
200 (kill-buffer buffer))))))
202 ;; --- UUencoding ---
204 (defun uuencode (file &optional name)
205 "UUencodes a file, maybe calling it NAME, into the current buffer."
206 (interactive "fInput file name: ")
208 ;; --- If NAME isn't specified, then guess from the filename ---
213 (or (string-match "[^/]*$" file) 0))))
215 (print (format "uuencode `%s' `%s'" file name))
217 ;; --- Now actually do the thing ---
219 (call-process "uuencode" file t nil name))
221 (defvar np-file "~/.np"
222 "*Where the `now-playing' file is.")
224 (defun np (&optional arg)
225 "Grabs a `now-playing' string."
229 (goto-char (point-max))
231 (insert-file np-file)))))
235 (require 'autorevert)
236 (defun mdw-check-autorevert ()
237 (if (and (buffer-file-name)
238 (tramp-tramp-file-p (buffer-file-name)))
239 (unless global-auto-revert-ignore-buffer
240 (setq global-auto-revert-ignore-buffer 'tramp))
241 (if (eq global-auto-revert-ignore-buffer 'tramp)
242 (setq global-auto-revert-ignore-buffer nil))))
243 (defadvice find-file (after mdw-autorevert activate)
244 (mdw-check-autorevert))
245 (defadvice write-file (after mdw-autorevert activate)
246 (mdw-check-autorevert)))
248 (defun mdwmail-mode ()
249 "Major mode for editing news and mail messages from external programs
250 Not much right now. Just support for doing MailCrypt stuff."
252 (kill-all-local-variables)
253 (use-local-map text-mode-map)
254 (setq local-abbrev-table text-mode-abbrev-table)
255 (setq major-mode 'mdwmail-mode)
256 (setq mode-name "[mdw] mail")
257 (make-local-variable 'paragraph-separate)
258 (make-local-variable 'paragraph-start)
259 (setq paragraph-start (concat "[ \t]*[-_][-_][-_]+$\\|^-- \\|-----\\|"
261 (setq paragraph-separate (concat "[ \t]*[-_][-_][-_]+$\\|^-- \\|-----\\|"
263 (run-hooks 'text-mode-hook 'mdwmail-mode-hook 'mail-setup-hook))
265 ;; --- How to encrypt in mdwmail ---
267 (defun mdwmail-mc-encrypt (&optional recip scm start end from sign)
269 (setq start (save-excursion
270 (goto-char (point-min))
271 (or (search-forward "\n\n" nil t) (point-min)))))
273 (setq end (point-max)))
274 (mc-encrypt-generic recip scm start end from sign))
276 ;; --- How to sign in mdwmail ---
278 (defun mdwmail-mc-sign (key scm start end uclr)
280 (setq start (save-excursion
281 (goto-char (point-min))
282 (or (search-forward "\n\n" nil t) (point-min)))))
284 (setq end (point-max)))
285 (mc-sign-generic key scm start end uclr))
287 ;; --- Some signature mangling ---
289 (defun mdwmail-mangle-signature ()
291 (goto-char (point-min))
292 (perform-replace "\n-- \n" "\n-- " nil nil nil)))
293 (add-hook 'mail-setup-hook 'mdwmail-mangle-signature)
295 ;;;----- URL viewing --------------------------------------------------------
297 (defun mdw-w3m-browse-url (url &optional new-session-p)
298 "Invoke w3m on the URL in its current window, or at least a different one.
299 If NEW-SESSION-P, start a new session."
300 (interactive "sURL: \nP")
302 (let ((window (selected-window)))
305 (select-window (or (and (not new-session-p)
306 (get-buffer-window "*w3m*"))
308 (if (one-window-p t) (split-window))
310 (w3m-browse-url url new-session-p))
311 (select-window window)))))
313 (defvar mdw-good-url-browsers
314 '((w3m . mdw-w3m-browse-url)
317 "List of good browsers for mdw-good-url-browsers; each item is a browser
318 function name, or a cons (CHECK . FUNC). A symbol FOO stands for (FOO
321 (defun mdw-good-url-browser ()
322 "Return a good URL browser. Trundle the list of such things, finding the
323 first item for which CHECK is fboundp, and returning the correponding FUNC."
324 (let ((bs mdw-good-url-browsers) b check func answer)
325 (while (and bs (not answer))
329 (setq check (car b) func (cdr b))
330 (setq check b func b))
335 ;;;----- Paragraph filling --------------------------------------------------
337 ;; --- Useful variables ---
339 (defvar mdw-fill-prefix nil
340 "*Used by `mdw-line-prefix' and `mdw-fill-paragraph'. If there's
341 no fill prefix currently set (by the `fill-prefix' variable) and there's
342 a match from one of the regexps here, it gets used to set the fill-prefix
343 for the current operation.
345 The variable is a list of items of the form `REGEXP . PREFIX'; if the
346 REGEXP matches, the PREFIX is used to set the fill prefix. It in turn is
349 STRING -- insert a literal string
350 (match . N) -- insert the thing matched by bracketed subexpression N
351 (pad . N) -- a string of whitespace the same width as subexpression N
352 (expr . FORM) -- the result of evaluating FORM")
354 (make-variable-buffer-local 'mdw-fill-prefix)
356 (defvar mdw-hanging-indents
357 "\\(\\(\\([*o]\\|--\\|[0-9]+\\.\\|\\[[0-9]+\\]\\|([a-zA-Z])\\)[ \t]+\\)?\\)"
358 "*Standard regular expression matching things which might be part of a
359 hanging indent. This is mainly useful in `auto-fill-mode'.")
361 ;; --- Setting things up ---
363 (fset 'mdw-do-auto-fill (symbol-function 'do-auto-fill))
365 ;; --- Utility functions ---
367 (defun mdw-tabify (s)
368 "Tabify the string S. This is a horrid hack."
373 (setq start (point-marker))
375 (setq end (point-marker))
377 (setq s (buffer-substring start (1- end)))
378 (delete-region start end)
379 (set-marker start nil)
383 (defun mdw-examine-fill-prefixes (l)
384 "Given a list of dynamic fill prefixes, pick one which matches context and
385 return the static fill prefix to use. Point must be at the start of a line,
386 and match data must be saved."
388 ((looking-at (car (car l)))
389 (mdw-tabify (apply (function concat)
390 (mapcar (function mdw-do-prefix-match)
392 (t (mdw-examine-fill-prefixes (cdr l)))))
394 (defun mdw-maybe-car (p)
395 "If P is a pair, return (car P), otherwise just return P."
396 (if (consp p) (car p) p))
398 (defun mdw-padding (s)
399 "Return a string the same width as S but made entirely from whitespace."
400 (let* ((l (length s)) (i 0) (n (make-string l ? )))
407 (defun mdw-do-prefix-match (m)
408 "Expand a dynamic prefix match element. See `mdw-fill-prefix' for
410 (cond ((not (consp m)) (format "%s" m))
411 ((eq (car m) 'match) (match-string (mdw-maybe-car (cdr m))))
412 ((eq (car m) 'pad) (mdw-padding (match-string
413 (mdw-maybe-car (cdr m)))))
414 ((eq (car m) 'eval) (eval (cdr m)))
417 (defun mdw-choose-dynamic-fill-prefix ()
418 "Work out the dynamic fill prefix based on the variable `mdw-fill-prefix'."
419 (cond ((and fill-prefix (not (string= fill-prefix ""))) fill-prefix)
420 ((not mdw-fill-prefix) fill-prefix)
424 (mdw-examine-fill-prefixes mdw-fill-prefix))))))
426 (defun do-auto-fill ()
427 "Handle auto-filling, working out a dynamic fill prefix in the case where
428 there isn't a sensible static one."
429 (let ((fill-prefix (mdw-choose-dynamic-fill-prefix)))
432 (defun mdw-fill-paragraph ()
433 "Fill paragraph, getting a dynamic fill prefix."
435 (let ((fill-prefix (mdw-choose-dynamic-fill-prefix)))
436 (fill-paragraph nil)))
438 (defun mdw-standard-fill-prefix (rx &optional mat)
439 "Set the dynamic fill prefix, handling standard hanging indents and stuff.
440 This is just a short-cut for setting the thing by hand, and by design it
441 doesn't cope with anything approximating a complicated case."
442 (setq mdw-fill-prefix
443 `((,(concat rx mdw-hanging-indents)
445 (pad . ,(or mat 2))))))
447 ;;;----- Other common declarations ------------------------------------------
449 (defun mdw-set-frame-transparency (&optional n)
451 (let* ((alist (frame-parameters))
452 (trans (assq 'transparency alist)))
454 (rplacd trans (not (if n (zerop n) (cdr trans))))
455 (setq trans (cons 'transparency (not (equal 0 n)))))
456 (modify-frame-parameters (selected-frame) (list trans))))
458 ;; --- Mouse wheel support ---
460 (defconst mdw-wheel-scroll-amount 15)
461 (defun mdw-wheel-up (click)
463 (mdw-wheel-scroll click (function scroll-down)))
464 (defun mdw-wheel-down (click)
466 (mdw-wheel-scroll click (function scroll-up)))
468 (defun mdw-wheel-scroll (click func)
469 (let ((win (selected-window)))
472 (select-window (posn-window (event-start click)))
474 (funcall func (/ (window-height) 2))))
475 (select-window win))))
477 ;; --- Going backwards ---
479 (defun other-window-backwards (arg)
481 (other-window (- arg)))
483 ;; --- Common mode settings ---
485 (defvar mdw-auto-indent t
486 "Whether to indent automatically after a newline.")
488 (defun mdw-misc-mode-config ()
490 (cond ((eq major-mode 'lisp-mode)
491 (local-set-key "\C-m" 'mdw-indent-newline-and-indent))
492 ((or (eq major-mode 'slime-repl-mode)
493 (eq major-mode 'asm-mode))
496 (local-set-key "\C-m" 'newline-and-indent))))
497 (local-set-key [C-return] 'newline)
498 (or (eq major-mode 'asm-mode)
499 (local-set-key [?\;] 'self-insert-command))
500 (local-set-key [?\#] 'self-insert-command)
501 (local-set-key [?\"] 'self-insert-command)
502 (setq comment-column 40)
504 (setq fill-column 77)
505 (setq show-trailing-whitespace t)
508 ;; --- Set up all sorts of faces ---
510 (defvar mdw-set-font nil)
512 (defvar mdw-punct-face 'mdw-punct-face "Face to use for punctuation")
513 (make-face 'mdw-punct-face)
514 (defvar mdw-number-face 'mdw-number-face "Face to use for numbers")
515 (make-face 'mdw-number-face)
517 ;;;----- General fontification ----------------------------------------------
519 (defun mdw-set-fonts (frame faces)
521 (let ((face (caar faces)))
522 (or (facep face) (make-face face))
523 (set-face-attribute face frame
529 :foreground 'unspecified
530 :background 'unspecified
531 :underline 'unspecified
532 :overline 'unspecified
533 :strike-through 'unspecified
535 :inverse-video 'unspecified
536 :stipple 'unspecified
538 :inherit 'unspecified)
539 (apply 'set-face-attribute face frame (cdar faces))
540 (setq faces (cdr faces)))))
542 (defun mdw-do-set-font (&optional frame)
544 (mdw-set-fonts (and (boundp 'frame) frame) `(
545 (default :foreground "white" :background "black"
546 ,@(cond ((eq window-system 'w32)
547 '(:family "courier new" :height 85))
548 ((eq window-system 'x)
549 '(:family "misc-fixed" :width semi-condensed))))
550 (modeline :foreground "blue" :background "yellow"
551 :box (:line-width 1 :style released-button))
552 (scroll-bar :foreground "black" :background "lightgrey")
553 (fringe :foreground "yellow" :background "grey30")
554 (show-paren-match-face :background "darkgreen")
555 (show-paren-mismatch-face :background "red")
556 (font-lock-warning-face :background "red" :weight bold)
557 (highlight :background "DarkSeaGreen4")
558 (holiday-face :background "red")
559 (calendar-today-face :foreground "yellow" :weight bold)
560 (comint-highlight-prompt :weight bold)
561 (comint-highlight-input)
562 (font-lock-builtin-face :weight bold)
563 (font-lock-type-face :weight bold)
564 (region :background "grey30")
565 (isearch :background "palevioletred2")
566 (mdw-punct-face :foreground ,(if window-system "burlywood2" "yellow"))
567 (mdw-number-face :foreground "yellow")
568 (font-lock-function-name-face :weight bold)
569 (font-lock-variable-name-face :slant italic)
570 (font-lock-comment-face
571 :foreground ,(if window-system "SeaGreen1" "green")
573 (font-lock-string-face :foreground ,(if window-system "SkyBlue1" "cyan"))
574 (font-lock-keyword-face :weight bold)
575 (font-lock-constant-face :weight bold)
576 (font-lock-reference-face :weight bold)
577 (woman-bold-face :weight bold)
578 (woman-italic-face :slant italic)
579 (diff-header-face :foreground "skyblue1")
580 (diff-index-face :weight bold)
581 (diff-file-header-face)
582 (diff-context-face :foreground "grey70")
583 (diff-added-face :foreground "white")
584 (diff-removed-face :foreground "white" :slant italic)
585 (whizzy-slice-face :background "grey10")
586 (whizzy-error-face :background "darkred")
587 (trailing-whitespace :background "red")
590 (defun mdw-set-font ()
593 (if (not mdw-set-font)
595 (setq mdw-set-font t)
596 (mdw-do-set-font nil)))))
598 ;;;----- C programming configuration ----------------------------------------
600 ;; --- Linux kernel hacking ---
602 (defvar linux-c-mode-hook)
604 (defun linux-c-mode ()
607 (setq major-mode 'linux-c-mode)
608 (setq mode-name "Linux C")
609 (run-hooks 'linux-c-mode-hook))
611 ;; --- Make C indentation nice ---
613 (defun mdw-c-style ()
614 (c-add-style "[mdw] C and C++ style"
615 '((c-basic-offset . 2)
616 (c-tab-always-indent . nil)
617 (comment-column . 40)
618 (c-class-key . "class")
619 (c-offsets-alist (substatement-open . 0)
626 (statement-case-intro . +)))
629 (defun mdw-fontify-c-and-c++ ()
631 ;; --- Fiddle with some syntax codes ---
633 (modify-syntax-entry ?_ "w")
634 (modify-syntax-entry ?* ". 23")
635 (modify-syntax-entry ?/ ". 124b")
636 (modify-syntax-entry ?\n "> b")
638 ;; --- Other stuff ---
641 (setq c-hanging-comment-ender-p nil)
642 (setq c-backslash-column 72)
643 (setq c-label-minimum-indentation 0)
644 (setq comment-start "/* ")
645 (setq comment-end " */")
646 (setq mdw-fill-prefix
647 `((,(concat "\\([ \t]*/?\\)"
649 "\\([A-Za-z]+:[ \t]*\\)?"
651 (pad . 1) (match . 2) (pad . 3) (pad . 4))))
653 ;; --- Now define things to be fontified ---
655 (make-local-variable 'font-lock-keywords)
664 "bool" ;C++, C9X macro
670 "complex" ;C9X macro, C++ template type
675 "defined" ;C89 preprocessor
682 ;; "entry" ;K&R -- never used
687 "false" ;C++, C9X macro
694 "imaginary" ;C9X macro
695 "inline" ;C++, C9X, GCC
708 "reinterpret_cast" ;C++
721 "true" ;C++, C9X macro
734 "wchar_t" ;C++, C89 library type
741 "_Pragma" ;C9X preprocessor
756 (preprocessor-keywords
757 (make-regexp '("assert" "define" "elif" "else" "endif" "error"
758 "ident" "if" "ifdef" "ifndef" "import" "include"
759 "line" "pragma" "unassert" "undef" "warning")))
761 (make-regexp '("class" "defs" "encode" "end" "implementation"
762 "interface" "private" "protected" "protocol" "public"
765 (setq font-lock-keywords
769 ;; --- Fontify include files as strings ---
771 (list (concat "^[ \t]*\\#[ \t]*"
772 "\\(include\\|import\\)"
773 "[ \t]*\\(<[^>]+\\(>\\|\\)\\)")
774 '(2 font-lock-string-face))
776 ;; --- Preprocessor directives are `references'? ---
778 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
779 preprocessor-keywords
780 "\\)\\>\\|[0-9]+\\|$\\)\\)")
781 '(1 font-lock-keyword-face))
783 ;; --- Handle the keywords defined above ---
785 (list (concat "@\\<\\(" objc-keywords "\\)\\>")
786 '(0 font-lock-keyword-face))
788 (list (concat "\\<\\(" c-keywords "\\)\\>")
789 '(0 font-lock-keyword-face))
791 ;; --- Handle numbers too ---
793 ;; This looks strange, I know. It corresponds to the
794 ;; preprocessor's idea of what a number looks like, rather than
795 ;; anything sensible.
797 (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
798 "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
799 '(0 mdw-number-face))
801 ;; --- And anything else is punctuation ---
803 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
804 '(0 mdw-punct-face))))))
806 ;;;----- AP calc mode -------------------------------------------------------
808 (defun apcalc-mode ()
811 (setq major-mode 'apcalc-mode)
812 (setq mode-name "AP Calc")
813 (run-hooks 'apcalc-mode-hook))
815 (defun mdw-fontify-apcalc ()
817 ;; --- Fiddle with some syntax codes ---
819 (modify-syntax-entry ?_ "w")
820 (modify-syntax-entry ?* ". 23")
821 (modify-syntax-entry ?/ ". 14")
823 ;; --- Other stuff ---
826 (setq c-hanging-comment-ender-p nil)
827 (setq c-backslash-column 72)
828 (setq comment-start "/* ")
829 (setq comment-end " */")
830 (setq mdw-fill-prefix
831 `((,(concat "\\([ \t]*/?\\)"
833 "\\([A-Za-z]+:[ \t]*\\)?"
835 (pad . 1) (match . 2) (pad . 3) (pad . 4))))
837 ;; --- Now define things to be fontified ---
839 (make-local-variable 'font-lock-keywords)
841 (make-regexp '("break" "case" "cd" "continue" "define" "default"
842 "do" "else" "exit" "for" "global" "goto" "help" "if"
843 "local" "mat" "obj" "print" "quit" "read" "return"
844 "show" "static" "switch" "while" "write"))))
846 (setq font-lock-keywords
850 ;; --- Handle the keywords defined above ---
852 (list (concat "\\<\\(" c-keywords "\\)\\>")
853 '(0 font-lock-keyword-face))
855 ;; --- Handle numbers too ---
857 ;; This looks strange, I know. It corresponds to the
858 ;; preprocessor's idea of what a number looks like, rather than
859 ;; anything sensible.
861 (list (concat "\\(\\<[0-9]\\|\\.[0-9]\\)"
862 "\\([Ee][+-]\\|[0-9A-Za-z_.]\\)*")
863 '(0 mdw-number-face))
865 ;; --- And anything else is punctuation ---
867 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
868 '(0 mdw-punct-face))))))
870 ;;;----- Java programming configuration -------------------------------------
872 ;; --- Make indentation nice ---
874 (defun mdw-java-style ()
875 (c-add-style "[mdw] Java style"
876 '((c-basic-offset . 2)
877 (c-tab-always-indent . nil)
878 (c-offsets-alist (substatement-open . 0)
883 (statement-case-intro . +)))
886 ;; --- Declare Java fontification style ---
888 (defun mdw-fontify-java ()
890 ;; --- Other stuff ---
893 (modify-syntax-entry ?_ "w")
894 (setq c-hanging-comment-ender-p nil)
895 (setq c-backslash-column 72)
896 (setq comment-start "/* ")
897 (setq comment-end " */")
898 (setq mdw-fill-prefix
899 `((,(concat "\\([ \t]*/?\\)"
901 "\\([A-Za-z]+:[ \t]*\\)?"
903 (pad . 1) (match . 2) (pad . 3) (pad . 4))))
905 ;; --- Now define things to be fontified ---
907 (make-local-variable 'font-lock-keywords)
909 (make-regexp '("abstract" "boolean" "break" "byte" "case" "catch"
910 "char" "class" "const" "continue" "default" "do"
911 "double" "else" "extends" "final" "finally" "float"
912 "for" "goto" "if" "implements" "import" "instanceof"
913 "int" "interface" "long" "native" "new" "package"
914 "private" "protected" "public" "return" "short"
915 "static" "super" "switch" "synchronized" "this"
916 "throw" "throws" "transient" "try" "void" "volatile"
919 "false" "null" "true"))))
921 (setq font-lock-keywords
925 ;; --- Handle the keywords defined above ---
927 (list (concat "\\<\\(" java-keywords "\\)\\>")
928 '(0 font-lock-keyword-face))
930 ;; --- Handle numbers too ---
932 ;; The following isn't quite right, but it's close enough.
934 (list (concat "\\<\\("
935 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
936 "[0-9]+\\(\\.[0-9]*\\|\\)"
937 "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
939 '(0 mdw-number-face))
941 ;; --- And anything else is punctuation ---
943 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
944 '(0 mdw-punct-face))))))
946 ;;;----- C# programming configuration ---------------------------------------
948 ;; --- Make indentation nice ---
950 (defun mdw-csharp-style ()
951 (c-add-style "[mdw] C# style"
952 '((c-basic-offset . 2)
953 (c-tab-always-indent . nil)
954 (c-offsets-alist (substatement-open . 0)
959 (statement-case-intro . +)))
962 ;; --- Declare C# fontification style ---
964 (defun mdw-fontify-csharp ()
966 ;; --- Other stuff ---
969 (modify-syntax-entry ?_ "w")
970 (setq c-hanging-comment-ender-p nil)
971 (setq c-backslash-column 72)
972 (setq comment-start "/* ")
973 (setq comment-end " */")
974 (setq mdw-fill-prefix
975 `((,(concat "\\([ \t]*/?\\)"
977 "\\([A-Za-z]+:[ \t]*\\)?"
979 (pad . 1) (match . 2) (pad . 3) (pad . 4))))
981 ;; --- Now define things to be fontified ---
983 (make-local-variable 'font-lock-keywords)
984 (let ((csharp-keywords
985 (make-regexp '("abstract" "as" "base" "bool" "break"
986 "byte" "case" "catch" "char" "checked"
987 "class" "const" "continue" "decimal" "default"
988 "delegate" "do" "double" "else" "enum"
989 "event" "explicit" "extern" "false" "finally"
990 "fixed" "float" "for" "foreach" "goto"
991 "if" "implicit" "in" "int" "interface"
992 "internal" "is" "lock" "long" "namespace"
993 "new" "null" "object" "operator" "out"
994 "override" "params" "private" "protected" "public"
995 "readonly" "ref" "return" "sbyte" "sealed"
996 "short" "sizeof" "stackalloc" "static" "string"
997 "struct" "switch" "this" "throw" "true"
998 "try" "typeof" "uint" "ulong" "unchecked"
999 "unsafe" "ushort" "using" "virtual" "void"
1000 "volatile" "while" "yield"))))
1002 (setq font-lock-keywords
1006 ;; --- Handle the keywords defined above ---
1008 (list (concat "\\<\\(" csharp-keywords "\\)\\>")
1009 '(0 font-lock-keyword-face))
1011 ;; --- Handle numbers too ---
1013 ;; The following isn't quite right, but it's close enough.
1015 (list (concat "\\<\\("
1016 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
1017 "[0-9]+\\(\\.[0-9]*\\|\\)"
1018 "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
1020 '(0 mdw-number-face))
1022 ;; --- And anything else is punctuation ---
1024 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1025 '(0 mdw-punct-face))))))
1027 (defun csharp-mode ()
1030 (setq major-mode 'csharp-mode)
1031 (setq mode-name "C#")
1032 (mdw-fontify-csharp)
1033 (run-hooks 'csharp-mode-hook))
1035 ;;;----- Awk programming configuration --------------------------------------
1037 ;; --- Make Awk indentation nice ---
1039 (defun mdw-awk-style ()
1040 (c-add-style "[mdw] Awk style"
1041 '((c-basic-offset . 2)
1042 (c-tab-always-indent . nil)
1043 (c-offsets-alist (substatement-open . 0)
1044 (statement-cont . 0)
1045 (statement-case-intro . +)))
1048 ;; --- Declare Awk fontification style ---
1050 (defun mdw-fontify-awk ()
1052 ;; --- Miscellaneous fiddling ---
1054 (modify-syntax-entry ?_ "w")
1056 (setq c-backslash-column 72)
1057 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
1059 ;; --- Now define things to be fontified ---
1061 (make-local-variable 'font-lock-keywords)
1063 (make-regexp '("BEGIN" "END" "ARGC" "ARGIND" "ARGV" "CONVFMT"
1064 "ENVIRON" "ERRNO" "FIELDWIDTHS" "FILENAME" "FNR"
1065 "FS" "IGNORECASE" "NF" "NR" "OFMT" "OFS" "ORS" "RS"
1066 "RSTART" "RLENGTH" "RT" "SUBSEP"
1067 "atan2" "break" "close" "continue" "cos" "delete"
1068 "do" "else" "exit" "exp" "fflush" "file" "for" "func"
1069 "function" "gensub" "getline" "gsub" "if" "in"
1070 "index" "int" "length" "log" "match" "next" "rand"
1071 "return" "print" "printf" "sin" "split" "sprintf"
1072 "sqrt" "srand" "strftime" "sub" "substr" "system"
1073 "systime" "tolower" "toupper" "while"))))
1075 (setq font-lock-keywords
1079 ;; --- Handle the keywords defined above ---
1081 (list (concat "\\<\\(" c-keywords "\\)\\>")
1082 '(0 font-lock-keyword-face))
1084 ;; --- Handle numbers too ---
1086 ;; The following isn't quite right, but it's close enough.
1088 (list (concat "\\<\\("
1089 "0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
1090 "[0-9]+\\(\\.[0-9]*\\|\\)"
1091 "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)\\)"
1093 '(0 mdw-number-face))
1095 ;; --- And anything else is punctuation ---
1097 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1098 '(0 mdw-punct-face))))))
1100 ;;;----- Perl programming style ---------------------------------------------
1102 ;; --- Perl indentation style ---
1104 (setq cperl-tab-always-indent nil)
1106 (setq cperl-indent-level 2)
1107 (setq cperl-continued-statement-offset 2)
1108 (setq cperl-continued-brace-offset 0)
1109 (setq cperl-brace-offset -2)
1110 (setq cperl-brace-imaginary-offset 0)
1111 (setq cperl-label-offset 0)
1113 ;; --- Define perl fontification style ---
1115 (defun mdw-fontify-perl ()
1117 ;; --- Miscellaneous fiddling ---
1119 (modify-syntax-entry ?_ "w")
1120 (modify-syntax-entry ?$ "\\")
1121 (modify-syntax-entry ?$ "\\" font-lock-syntax-table)
1122 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
1124 ;; --- Now define fontification things ---
1126 (make-local-variable 'font-lock-keywords)
1127 (let ((perl-keywords
1128 (make-regexp '("and" "cmp" "continue" "do" "else" "elsif" "eq"
1129 "for" "foreach" "ge" "gt" "goto" "if"
1130 "last" "le" "lt" "local" "my" "ne" "next" "or"
1131 "package" "redo" "require" "return" "sub"
1132 "undef" "unless" "until" "use" "while"))))
1134 (setq font-lock-keywords
1138 ;; --- Set up the keywords defined above ---
1140 (list (concat "\\<\\(" perl-keywords "\\)\\>")
1141 '(0 font-lock-keyword-face))
1143 ;; --- At least numbers are simpler than C ---
1145 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
1146 "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)"
1147 "\\([eE]\\([-+]\\|\\)[0-9_]+\\|\\)")
1148 '(0 mdw-number-face))
1150 ;; --- And anything else is punctuation ---
1152 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1153 '(0 mdw-punct-face))))))
1155 (defun perl-number-tests (&optional arg)
1156 "Assign consecutive numbers to lines containing `#t'. With ARG,
1157 strip numbers instead."
1160 (goto-char (point-min))
1161 (let ((i 0) (fmt (if arg "" " %4d")))
1162 (while (search-forward "#t" nil t)
1163 (delete-region (point) (line-end-position))
1165 (insert (format fmt i)))
1166 (goto-char (point-min))
1167 (if (re-search-forward "\\(tests\\s-*=>\\s-*\\)\\w*" nil t)
1168 (replace-match (format "\\1%d" i))))))
1170 ;;;----- Python programming style -------------------------------------------
1172 ;; --- Define Python fontification style ---
1174 (trap (require 'pyrex-mode))
1175 (defun mdw-fontify-python ()
1177 ;; --- Miscellaneous fiddling ---
1179 (modify-syntax-entry ?_ "w")
1180 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
1182 ;; --- Now define fontification things ---
1184 (make-local-variable 'font-lock-keywords)
1185 (let ((python-keywords
1186 (make-regexp '("and" "as" "assert" "break" "class" "continue" "def"
1187 "del" "elif" "else" "except" "exec" "finally" "for"
1188 "from" "global" "if" "import" "in" "is" "lambda"
1189 "not" "or" "pass" "print" "raise" "return" "try"
1191 (setq font-lock-keywords
1195 ;; --- Set up the keywords defined above ---
1197 (list (concat "\\<\\(" python-keywords "\\)\\>")
1198 '(0 font-lock-keyword-face))
1200 ;; --- At least numbers are simpler than C ---
1202 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
1203 "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)"
1204 "\\([eE]\\([-+]\\|\\)[0-9_]+\\|[lL]\\|\\)")
1205 '(0 mdw-number-face))
1207 ;; --- And anything else is punctuation ---
1209 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1210 '(0 mdw-punct-face))))))
1212 ;;;----- ARM assembler programming configuration ----------------------------
1214 ;; --- There doesn't appear to be an Emacs mode for this yet ---
1216 ;; Better do something about that, I suppose.
1218 (defvar arm-assembler-mode-map nil)
1219 (defvar arm-assembler-abbrev-table nil)
1220 (defvar arm-assembler-mode-syntax-table (make-syntax-table))
1222 (or arm-assembler-mode-map
1224 (setq arm-assembler-mode-map (make-sparse-keymap))
1225 (define-key arm-assembler-mode-map "\C-m" 'arm-assembler-newline)
1226 (define-key arm-assembler-mode-map [C-return] 'newline)
1227 (define-key arm-assembler-mode-map "\t" 'tab-to-tab-stop)))
1229 (defun arm-assembler-mode ()
1230 "Major mode for ARM assembler programs"
1233 ;; --- Do standard major mode things ---
1235 (kill-all-local-variables)
1236 (use-local-map arm-assembler-mode-map)
1237 (setq local-abbrev-table arm-assembler-abbrev-table)
1238 (setq major-mode 'arm-assembler-mode)
1239 (setq mode-name "ARM assembler")
1241 ;; --- Set up syntax table ---
1243 (set-syntax-table arm-assembler-mode-syntax-table)
1244 (modify-syntax-entry ?; ; Nasty hack
1245 "<" arm-assembler-mode-syntax-table)
1246 (modify-syntax-entry ?\n ">" arm-assembler-mode-syntax-table)
1247 (modify-syntax-entry ?_ "_" arm-assembler-mode-syntax-table)
1249 (make-local-variable 'comment-start)
1250 (setq comment-start ";")
1251 (make-local-variable 'comment-end)
1252 (setq comment-end "")
1253 (make-local-variable 'comment-column)
1254 (setq comment-column 48)
1255 (make-local-variable 'comment-start-skip)
1256 (setq comment-start-skip ";+[ \t]*")
1258 ;; --- Play with indentation ---
1260 (make-local-variable 'indent-line-function)
1261 (setq indent-line-function 'indent-relative-maybe)
1263 ;; --- Set fill prefix ---
1265 (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)")
1267 ;; --- Fiddle with fontification ---
1269 (make-local-variable 'font-lock-keywords)
1270 (setq font-lock-keywords
1274 ;; --- Handle numbers too ---
1276 ;; The following isn't quite right, but it's close enough.
1280 "\\<[0-9]+\\(\\.[0-9]*\\|_[0-9a-zA-Z]+\\|\\)"
1282 '(0 mdw-number-face))
1284 ;; --- Do something about operators ---
1286 (list "^[^ \t]*[ \t]+\\(GET\\|LNK\\)[ \t]+\\([^;\n]*\\)"
1287 '(1 font-lock-keyword-face)
1288 '(2 font-lock-string-face))
1290 '(0 font-lock-keyword-face))
1292 ;; --- Do menemonics and directives ---
1294 (list "^[^ \t]*[ \t]+\\([a-zA-Z]+\\)"
1295 '(1 font-lock-keyword-face))
1297 ;; --- And anything else is punctuation ---
1299 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1300 '(0 mdw-punct-face))))
1302 (run-hooks 'arm-assembler-mode-hook))
1304 ;;;----- Assembler mode -----------------------------------------------------
1306 (defun mdw-fontify-asm ()
1307 (modify-syntax-entry ?' "\"")
1308 (modify-syntax-entry ?. "w")
1309 (setf fill-prefix nil)
1310 (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)"))
1312 ;;;----- TCL configuration --------------------------------------------------
1314 (defun mdw-fontify-tcl ()
1315 (mapcar #'(lambda (ch) (modify-syntax-entry ch ".")) '(?$))
1316 (mdw-standard-fill-prefix "\\([ \t]*#+[ \t]*\\)")
1317 (make-local-variable 'font-lock-keywords)
1318 (setq font-lock-keywords
1321 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
1322 "\\<[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)"
1323 "\\([eE]\\([-+]\\|\\)[0-9_]+\\|\\)")
1324 '(0 mdw-number-face))
1325 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1326 '(0 mdw-punct-face)))))
1328 ;;;----- REXX configuration -------------------------------------------------
1330 (defun mdw-rexx-electric-* ()
1335 (defun mdw-rexx-indent-newline-indent ()
1338 (if abbrev-mode (expand-abbrev))
1339 (newline-and-indent))
1341 (defun mdw-fontify-rexx ()
1343 ;; --- Various bits of fiddling ---
1345 (setq mdw-auto-indent nil)
1346 (local-set-key [?\C-m] 'mdw-rexx-indent-newline-indent)
1347 (local-set-key [?*] 'mdw-rexx-electric-*)
1348 (mapcar #'(lambda (ch) (modify-syntax-entry ch "w"))
1349 '(?. ?! ?? ?_ ?# ?@ ?$))
1350 (mdw-standard-fill-prefix "\\([ \t]*/?\*[ \t]*\\)")
1352 ;; --- Set up keywords and things for fontification ---
1354 (make-local-variable 'font-lock-keywords-case-fold-search)
1355 (setq font-lock-keywords-case-fold-search t)
1357 (setq rexx-indent 2)
1358 (setq rexx-end-indent rexx-indent)
1359 (setq rexx-tab-always-indent nil)
1360 (setq rexx-cont-indent rexx-indent)
1362 (make-local-variable 'font-lock-keywords)
1363 (let ((rexx-keywords
1364 (make-regexp '("address" "arg" "by" "call" "digits" "do" "drop"
1365 "else" "end" "engineering" "exit" "expose" "for"
1366 "forever" "form" "fuzz" "if" "interpret" "iterate"
1367 "leave" "linein" "name" "nop" "numeric" "off" "on"
1368 "options" "otherwise" "parse" "procedure" "pull"
1369 "push" "queue" "return" "say" "select" "signal"
1370 "scientific" "source" "then" "trace" "to" "until"
1371 "upper" "value" "var" "version" "when" "while"
1374 "abbrev" "abs" "bitand" "bitor" "bitxor" "b2x"
1375 "center" "center" "charin" "charout" "chars"
1376 "compare" "condition" "copies" "c2d" "c2x"
1377 "datatype" "date" "delstr" "delword" "d2c" "d2x"
1378 "errortext" "format" "fuzz" "insert" "lastpos"
1379 "left" "length" "lineout" "lines" "max" "min"
1380 "overlay" "pos" "queued" "random" "reverse" "right"
1381 "sign" "sourceline" "space" "stream" "strip"
1382 "substr" "subword" "symbol" "time" "translate"
1383 "trunc" "value" "verify" "word" "wordindex"
1384 "wordlength" "wordpos" "words" "xrange" "x2b" "x2c"
1387 (setq font-lock-keywords
1391 ;; --- Set up the keywords defined above ---
1393 (list (concat "\\<\\(" rexx-keywords "\\)\\>")
1394 '(0 font-lock-keyword-face))
1396 ;; --- Fontify all symbols the same way ---
1398 (list (concat "\\<\\([0-9.][A-Za-z0-9.!?_#@$]*[Ee][+-]?[0-9]+\\|"
1399 "[A-Za-z0-9.!?_#@$]+\\)")
1400 '(0 font-lock-variable-name-face))
1402 ;; --- And everything else is punctuation ---
1404 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1405 '(0 mdw-punct-face))))))
1407 ;;;----- Standard ML programming style --------------------------------------
1409 (defun mdw-fontify-sml ()
1411 ;; --- Make underscore an honorary letter ---
1413 (modify-syntax-entry ?_ "w")
1414 (modify-syntax-entry ?' "w")
1416 ;; --- Set fill prefix ---
1418 (mdw-standard-fill-prefix "\\([ \t]*(\*[ \t]*\\)")
1420 ;; --- Now define fontification things ---
1422 (make-local-variable 'font-lock-keywords)
1424 (make-regexp '("abstype" "and" "andalso" "as"
1427 "else" "end" "eqtype" "exception"
1428 "fn" "fun" "functor"
1430 "if" "in" "include" "infix" "infixr"
1433 "of" "op" "open" "orelse"
1435 "sharing" "sig" "signature" "struct" "structure"
1438 "where" "while" "with" "withtype"))))
1440 (setq font-lock-keywords
1444 ;; --- Set up the keywords defined above ---
1446 (list (concat "\\<\\(" sml-keywords "\\)\\>")
1447 '(0 font-lock-keyword-face))
1449 ;; --- At least numbers are simpler than C ---
1451 (list (concat "\\<\\(\\~\\|\\)"
1452 "\\(0\\(\\([wW]\\|\\)[xX][0-9a-fA-F]+\\|"
1454 "\\([0-9]+\\(\\.[0-9]+\\|\\)"
1455 "\\([eE]\\(\\~\\|\\)"
1456 "[0-9]+\\|\\)\\)\\)")
1457 '(0 mdw-number-face))
1459 ;; --- And anything else is punctuation ---
1461 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1462 '(0 mdw-punct-face))))))
1464 ;;;----- Haskell configuration ----------------------------------------------
1466 (defun mdw-fontify-haskell ()
1468 ;; --- Fiddle with syntax table to get comments right ---
1470 (modify-syntax-entry ?_ "w")
1471 (modify-syntax-entry ?' "\"")
1472 (modify-syntax-entry ?- ". 123")
1473 (modify-syntax-entry ?{ ". 1b")
1474 (modify-syntax-entry ?} ". 4b")
1475 (modify-syntax-entry ?\n ">")
1477 ;; --- Set fill prefix ---
1479 (mdw-standard-fill-prefix "\\([ \t]*{?--?[ \t]*\\)")
1481 ;; --- Fiddle with fontification ---
1483 (make-local-variable 'font-lock-keywords)
1484 (let ((haskell-keywords
1485 (make-regexp '("as" "case" "ccall" "class" "data" "default"
1486 "deriving" "do" "else" "foreign" "hiding" "if"
1487 "import" "in" "infix" "infixl" "infixr" "instance"
1488 "let" "module" "newtype" "of" "qualified" "safe"
1489 "stdcall" "then" "type" "unsafe" "where"))))
1491 (setq font-lock-keywords
1495 '(0 font-lock-comment-face))
1496 (list (concat "\\<\\(" haskell-keywords "\\)\\>")
1497 '(0 font-lock-keyword-face))
1498 (list (concat "\\<0\\([xX][0-9a-fA-F]+\\|[0-7]+\\)\\|"
1499 "\\<[0-9][0-9_]*\\(\\.[0-9]*\\|\\)"
1500 "\\([eE]\\([-+]\\|\\)[0-9]+\\|\\)")
1501 '(0 mdw-number-face))
1502 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1503 '(0 mdw-punct-face))))))
1505 ;;;----- Texinfo configuration ----------------------------------------------
1507 (defun mdw-fontify-texinfo ()
1509 ;; --- Set fill prefix ---
1511 (mdw-standard-fill-prefix "\\([ \t]*@c[ \t]+\\)")
1513 ;; --- Real fontification things ---
1515 (make-local-variable 'font-lock-keywords)
1516 (setq font-lock-keywords
1520 ;; --- Environment names are keywords ---
1522 (list "@\\(end\\) *\\([a-zA-Z]*\\)?"
1523 '(2 font-lock-keyword-face))
1525 ;; --- Unmark escaped magic characters ---
1527 (list "\\(@\\)\\([@{}]\\)"
1528 '(1 font-lock-keyword-face)
1529 '(2 font-lock-variable-name-face))
1531 ;; --- Make sure we get comments properly ---
1533 (list "@c\\(\\|omment\\)\\( .*\\)?$"
1534 '(0 font-lock-comment-face))
1536 ;; --- Command names are keywords ---
1538 (list "@\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)"
1539 '(0 font-lock-keyword-face))
1541 ;; --- Fontify TeX special characters as punctuation ---
1544 '(0 mdw-punct-face)))))
1546 ;;;----- TeX and LaTeX configuration ----------------------------------------
1548 (defun mdw-fontify-tex ()
1549 (setq ispell-parser 'tex)
1551 ;; --- Don't make maths into a string ---
1553 (modify-syntax-entry ?$ ".")
1554 (modify-syntax-entry ?$ "." font-lock-syntax-table)
1555 (local-set-key [?$] 'self-insert-command)
1557 ;; --- Set fill prefix ---
1559 (mdw-standard-fill-prefix "\\([ \t]*%+[ \t]*\\)")
1561 ;; --- Real fontification things ---
1563 (make-local-variable 'font-lock-keywords)
1564 (setq font-lock-keywords
1568 ;; --- Environment names are keywords ---
1570 (list (concat "\\\\\\(begin\\|end\\|newenvironment\\)"
1572 '(2 font-lock-keyword-face))
1574 ;; --- Suspended environment names are keywords too ---
1576 (list (concat "\\\\\\(suspend\\|resume\\)\\(\\[[^]]*\\]\\)?"
1578 '(3 font-lock-keyword-face))
1580 ;; --- Command names are keywords ---
1582 (list "\\\\\\([^a-zA-Z@]\\|[a-zA-Z@]*\\)"
1583 '(0 font-lock-keyword-face))
1585 ;; --- Handle @/.../ for italics ---
1587 ;; (list "\\(@/\\)\\([^/]*\\)\\(/\\)"
1588 ;; '(1 font-lock-keyword-face)
1589 ;; '(3 font-lock-keyword-face))
1591 ;; --- Handle @*...* for boldness ---
1593 ;; (list "\\(@\\*\\)\\([^*]*\\)\\(\\*\\)"
1594 ;; '(1 font-lock-keyword-face)
1595 ;; '(3 font-lock-keyword-face))
1597 ;; --- Handle @`...' for literal syntax things ---
1599 ;; (list "\\(@`\\)\\([^']*\\)\\('\\)"
1600 ;; '(1 font-lock-keyword-face)
1601 ;; '(3 font-lock-keyword-face))
1603 ;; --- Handle @<...> for nonterminals ---
1605 ;; (list "\\(@<\\)\\([^>]*\\)\\(>\\)"
1606 ;; '(1 font-lock-keyword-face)
1607 ;; '(3 font-lock-keyword-face))
1609 ;; --- Handle other @-commands ---
1611 ;; (list "@\\([^a-zA-Z]\\|[a-zA-Z]*\\)"
1612 ;; '(0 font-lock-keyword-face))
1614 ;; --- Make sure we get comments properly ---
1617 '(0 font-lock-comment-face))
1619 ;; --- Fontify TeX special characters as punctuation ---
1622 '(0 mdw-punct-face)))))
1624 ;;;----- Shell scripts ------------------------------------------------------
1626 (defun mdw-setup-sh-script-mode ()
1628 ;; --- Fetch the shell interpreter's name ---
1630 (let ((shell-name sh-shell-file))
1632 ;; --- Try reading the hash-bang line ---
1635 (goto-char (point-min))
1636 (if (looking-at "#![ \t]*\\([^ \t\n]*\\)")
1637 (setq shell-name (match-string 1))))
1639 ;; --- Now try to set the shell ---
1641 ;; Don't let `sh-set-shell' bugger up my script.
1643 (let ((executable-set-magic #'(lambda (s &rest r) s)))
1644 (sh-set-shell shell-name)))
1646 ;; --- Now enable my keys and the fontification ---
1648 (mdw-misc-mode-config)
1650 ;; --- Set the indentation level correctly ---
1652 (setq sh-indentation 2)
1653 (setq sh-basic-offset 2))
1655 ;;;----- Messages-file mode -------------------------------------------------
1657 (defun message-mode-guts ()
1658 (setq messages-mode-syntax-table (make-syntax-table))
1659 (set-syntax-table messages-mode-syntax-table)
1660 (modify-syntax-entry ?_ "w" messages-mode-syntax-table)
1661 (modify-syntax-entry ?- "w" messages-mode-syntax-table)
1662 (modify-syntax-entry ?0 "w" messages-mode-syntax-table)
1663 (modify-syntax-entry ?1 "w" messages-mode-syntax-table)
1664 (modify-syntax-entry ?2 "w" messages-mode-syntax-table)
1665 (modify-syntax-entry ?3 "w" messages-mode-syntax-table)
1666 (modify-syntax-entry ?4 "w" messages-mode-syntax-table)
1667 (modify-syntax-entry ?5 "w" messages-mode-syntax-table)
1668 (modify-syntax-entry ?6 "w" messages-mode-syntax-table)
1669 (modify-syntax-entry ?7 "w" messages-mode-syntax-table)
1670 (modify-syntax-entry ?8 "w" messages-mode-syntax-table)
1671 (modify-syntax-entry ?9 "w" messages-mode-syntax-table)
1672 (make-local-variable 'comment-start)
1673 (make-local-variable 'comment-end)
1674 (make-local-variable 'indent-line-function)
1675 (setq indent-line-function 'indent-relative)
1676 (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
1677 (make-local-variable 'font-lock-defaults)
1678 (make-local-variable 'message-mode-keywords)
1680 (make-regexp '("array" "bitmap" "callback" "docs[ \t]+enum"
1681 "export" "enum" "fixed-octetstring" "flags"
1682 "harmless" "map" "nested" "optional"
1683 "optional-tagged" "package" "primitive"
1684 "primitive-nullfree" "relaxed[ \t]+enum"
1685 "set" "table" "tagged-optional" "union"
1686 "variadic" "vector" "version" "version-tag"))))
1687 (setq message-mode-keywords
1689 (list (concat "\\<\\(" keywords "\\)\\>:")
1690 '(0 font-lock-keyword-face))
1691 '("\\([-a-zA-Z0-9]+:\\)" (0 font-lock-warning-face))
1692 '("\\(\\<[a-z][-_a-zA-Z0-9]*\\)"
1693 (0 font-lock-variable-name-face))
1694 '("\\<\\([0-9]+\\)\\>" (0 mdw-number-face))
1695 '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1696 (0 mdw-punct-face)))))
1697 (setq font-lock-defaults
1698 '(message-mode-keywords nil nil nil nil))
1699 (run-hooks 'messages-file-hook))
1701 (defun messages-mode ()
1704 (setq major-mode 'messages-mode)
1705 (setq mode-name "Messages")
1707 (modify-syntax-entry ?# "<" messages-mode-syntax-table)
1708 (modify-syntax-entry ?\n ">" messages-mode-syntax-table)
1709 (setq comment-start "# ")
1710 (setq comment-end "")
1711 (turn-on-font-lock-if-enabled)
1712 (run-hooks 'messages-mode-hook))
1714 (defun cpp-messages-mode ()
1717 (setq major-mode 'cpp-messages-mode)
1718 (setq mode-name "CPP Messages")
1720 (modify-syntax-entry ?* ". 23" messages-mode-syntax-table)
1721 (modify-syntax-entry ?/ ". 14" messages-mode-syntax-table)
1722 (setq comment-start "/* ")
1723 (setq comment-end " */")
1724 (let ((preprocessor-keywords
1725 (make-regexp '("assert" "define" "elif" "else" "endif" "error"
1726 "ident" "if" "ifdef" "ifndef" "import" "include"
1727 "line" "pragma" "unassert" "undef" "warning"))))
1728 (setq message-mode-keywords
1729 (append (list (list (concat "^[ \t]*\\#[ \t]*"
1730 "\\(include\\|import\\)"
1731 "[ \t]*\\(<[^>]+\\(>\\|\\)\\)")
1732 '(2 font-lock-string-face))
1733 (list (concat "^\\([ \t]*#[ \t]*\\(\\("
1734 preprocessor-keywords
1735 "\\)\\>\\|[0-9]+\\|$\\)\\)")
1736 '(1 font-lock-keyword-face)))
1737 message-mode-keywords)))
1738 (turn-on-font-lock-if-enabled)
1739 (run-hooks 'cpp-messages-mode-hook))
1741 (add-hook 'messages-mode-hook 'mdw-misc-mode-config t)
1742 (add-hook 'cpp-messages-mode-hook 'mdw-misc-mode-config t)
1743 ; (add-hook 'messages-file-hook 'mdw-fontify-messages t)
1745 ;;;----- Messages-file mode -------------------------------------------------
1747 (defvar mallow-driver-substitution-face 'mallow-driver-substitution-face
1748 "Face to use for subsittution directives.")
1749 (make-face 'mallow-driver-substitution-face)
1750 (defvar mallow-driver-text-face 'mallow-driver-text-face
1751 "Face to use for body text.")
1752 (make-face 'mallow-driver-text-face)
1754 (defun mallow-driver-mode ()
1757 (setq major-mode 'mallow-driver-mode)
1758 (setq mode-name "Mallow driver")
1759 (setq mallow-driver-mode-syntax-table (make-syntax-table))
1760 (set-syntax-table mallow-driver-mode-syntax-table)
1761 (make-local-variable 'comment-start)
1762 (make-local-variable 'comment-end)
1763 (make-local-variable 'indent-line-function)
1764 (setq indent-line-function 'indent-relative)
1765 (mdw-standard-fill-prefix "\\([ \t]*\\(;\\|/?\\*\\)+[ \t]*\\)")
1766 (make-local-variable 'font-lock-defaults)
1767 (make-local-variable 'mallow-driver-mode-keywords)
1769 (make-regexp '("each" "divert" "file" "if"
1770 "perl" "set" "string" "type" "write"))))
1771 (setq mallow-driver-mode-keywords
1773 (list (concat "^%\\s *\\(}\\|\\(" keywords "\\)\\>\\).*$")
1774 '(0 font-lock-keyword-face))
1775 (list "^%\\s *\\(#.*\\|\\)$"
1776 '(0 font-lock-comment-face))
1778 '(0 font-lock-keyword-face))
1779 (list "^|?\\(.+\\)$" '(1 mallow-driver-text-face))
1781 '(0 mallow-driver-substitution-face t)))))
1782 (setq font-lock-defaults
1783 '(mallow-driver-mode-keywords nil nil nil nil))
1784 (modify-syntax-entry ?\" "_" mallow-driver-mode-syntax-table)
1785 (modify-syntax-entry ?\n ">" mallow-driver-mode-syntax-table)
1786 (setq comment-start "%# ")
1787 (setq comment-end "")
1788 (turn-on-font-lock-if-enabled)
1789 (run-hooks 'mallow-driver-mode-hook))
1791 (add-hook 'mallow-driver-hook 'mdw-misc-mode-config t)
1793 ;;;----- NFast debugs -------------------------------------------------------
1795 (defun nfast-debug-mode ()
1798 (setq major-mode 'nfast-debug-mode)
1799 (setq mode-name "NFast debug")
1800 (setq messages-mode-syntax-table (make-syntax-table))
1801 (set-syntax-table messages-mode-syntax-table)
1802 (make-local-variable 'font-lock-defaults)
1803 (make-local-variable 'nfast-debug-mode-keywords)
1804 (setq truncate-lines t)
1805 (setq nfast-debug-mode-keywords
1807 '("^\\(NFast_\\(Connect\\|Disconnect\\|Submit\\|Wait\\)\\)"
1808 (0 font-lock-keyword-face))
1809 (list (concat "^[ \t]+\\(\\("
1810 "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
1811 "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
1813 "[0-9a-fA-F]+\\)[ \t]*$")
1814 '(0 mdw-number-face))
1815 '("^[ \t]+\.status=[ \t]+\\<\\(OK\\)\\>"
1816 (1 font-lock-keyword-face))
1817 '("^[ \t]+\.status=[ \t]+\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>"
1818 (1 font-lock-warning-face))
1819 '("^[ \t]+\.status[ \t]+\\<\\(zero\\)\\>"
1821 (list (concat "^[ \t]+\\.cmd=[ \t]+"
1822 "\\<\\([a-zA-Z][0-9a-zA-Z]*\\)\\>")
1823 '(1 font-lock-keyword-face))
1824 '("-?\\<\\([0-9]+\\|0x[0-9a-fA-F]+\\)\\>" (0 mdw-number-face))
1825 '("^\\([ \t]+[a-z0-9.]+\\)" (0 font-lock-variable-name-face))
1826 '("\\<\\([a-z][a-z0-9.]+\\)\\>=" (1 font-lock-variable-name-face))
1827 '("\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)" (0 mdw-punct-face))))
1828 (setq font-lock-defaults
1829 '(nfast-debug-mode-keywords nil nil nil nil))
1830 (turn-on-font-lock-if-enabled)
1831 (run-hooks 'nfast-debug-mode-hook))
1833 ;;;----- Other languages ----------------------------------------------------
1835 ;; --- Smalltalk ---
1837 (defun mdw-setup-smalltalk ()
1838 (and mdw-auto-indent
1839 (local-set-key "\C-m" 'smalltalk-newline-and-indent))
1840 (make-variable-buffer-local 'mdw-auto-indent)
1841 (setq mdw-auto-indent nil)
1842 (local-set-key "\C-i" 'smalltalk-reindent))
1844 (defun mdw-fontify-smalltalk ()
1845 (make-local-variable 'font-lock-keywords)
1846 (setq font-lock-keywords
1849 (list "\\<[A-Z][a-zA-Z0-9]*\\>"
1850 '(0 font-lock-keyword-face))
1851 (list (concat "\\<0\\([xX][0-9a-fA-F_]+\\|[0-7_]+\\)\\|"
1852 "[0-9][0-9_]*\\(\\.[0-9_]*\\|\\)"
1853 "\\([eE]\\([-+]\\|\\)[0-9_]+\\|\\)")
1854 '(0 mdw-number-face))
1855 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1856 '(0 mdw-punct-face)))))
1858 ;; --- Lispy languages ---
1860 (defun mdw-indent-newline-and-indent ()
1862 (indent-for-tab-command)
1863 (newline-and-indent))
1865 (eval-after-load "cl-indent"
1867 (mapc #'(lambda (pair)
1869 'common-lisp-indent-function
1871 '((destructuring-bind . ((&whole 4 &rest 1) 4 &body))
1872 (multiple-value-bind . ((&whole 4 &rest 1) 4 &body))))))
1874 (defun mdw-common-lisp-indent ()
1875 (make-variable-buffer-local 'lisp-indent-function)
1876 (setq lisp-indent-function 'common-lisp-indent-function))
1878 (defun mdw-fontify-lispy ()
1880 ;; --- Set fill prefix ---
1882 (mdw-standard-fill-prefix "\\([ \t]*;+[ \t]*\\)")
1884 ;; --- Not much fontification needed ---
1886 (make-local-variable 'font-lock-keywords)
1887 (setq font-lock-keywords
1890 (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
1891 '(0 mdw-punct-face)))))
1893 (defun comint-send-and-indent ()
1896 (and mdw-auto-indent
1897 (indent-for-tab-command)))
1899 ;;;----- Text mode ----------------------------------------------------------
1901 (defun mdw-text-mode ()
1902 (setq fill-column 72)
1904 (mdw-standard-fill-prefix
1905 "\\([ \t]*\\([A-Za-z0-9]*[>#|:] ?\\)*[ \t]*\\)" 3)
1908 ;;;----- Shell mode ---------------------------------------------------------
1910 (defun mdw-sh-mode-setup ()
1911 (local-set-key [?\C-a] 'comint-bol)
1912 (add-hook 'comint-output-filter-functions
1913 'comint-watch-for-password-prompt))
1915 (defun mdw-term-mode-setup ()
1916 (setq term-prompt-regexp "^[^]#$%>»}\n]*[]#$%>»}] *")
1917 (make-local-variable 'mouse-yank-at-point)
1918 (make-local-variable 'transient-mark-mode)
1919 (setq mouse-yank-at-point t)
1920 (setq transient-mark-mode nil)
1924 ;;;----- That's all, folks --------------------------------------------------
1926 (provide 'dot-emacs)