1 ;;; muse-colors.el --- Coloring and highlighting used by Muse
3 ;; Copyright (C) 2004, 2005 Free Software Foundation, Inc.
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: muse-colors.el
7 ;; Date: Thu 11-Mar-2004
8 ;; Keywords: hypermedia
9 ;; Author: John Wiegley (johnw AT gnu DOT org)
10 ;; Maintainer: Michael Olson (mwolson AT gnu DOT org)
11 ;; URL: http://www.mwolson.org/projects/MuseMode.html
12 ;; Compatibility: Emacs21
14 ;; This file is not part of GNU Emacs.
16 ;; This is free software; you can redistribute it and/or modify it under
17 ;; the terms of the GNU General Public License as published by the Free
18 ;; Software Foundation; either version 2, or (at your option) any later
21 ;; This is distributed in the hope that it will be useful, but WITHOUT
22 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING. If not, write to the
28 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
29 ;; MA 02111-1307, USA.
35 ;; Lan Yufeng (nlany DOT web AT gmail DOT com) found an error where
36 ;; headings were being given the wrong face, contributing a patch to
41 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
43 ;; Emacs Muse Highlighting
45 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
48 (require 'muse-regexps
)
51 (defgroup muse-colors nil
52 "Options controlling the behavior of Emacs Muse highlighting.
53 See `muse-colors-buffer' for more information."
56 (defcustom muse-colors-autogen-headings t
57 "Specify whether the heading faces should be auto-generated.
58 The default is to scale them.
60 Choosing 'outline will copy the colors from the outline-mode
63 If you want to customize each of the headings individually, set
65 :type
'(choice (const :tag
"Default (scaled) headings" t
)
66 (const :tag
"Use outline-mode headings" outline
)
67 (const :tag
"Don't touch the headings" nil
))
70 (defvar muse-colors-outline-faces-list
71 (if (facep 'outline-1
)
72 '(outline-1 outline-2 outline-3 outline-4 outline-5
)
73 ;; These are supposed to be equivalent in coloring
74 '(font-lock-function-name-face
75 font-lock-variable-name-face
76 font-lock-keyword-face
77 font-lock-builtin-face
78 font-lock-comment-face
))
79 "Outline faces to use when assigning Muse header faces.")
81 (defun muse-make-faces ()
83 (dolist (num '(1 2 3 4 5))
84 (setq newsym
(intern (concat "muse-header-"
85 (int-to-string num
))))
87 ((null muse-colors-autogen-headings
)
88 (make-empty-face newsym
))
90 (if (eq muse-colors-autogen-headings
'outline
)
91 (copy-face (nth (1- num
)
92 muse-colors-outline-faces-list
)
94 (eval `(defface ,newsym
96 ,(nth (1- num
) '("24pt" "18pt" "14pt" "12pt" "11pt"))
99 :group
'muse-colors
))))
100 ((< emacs-major-version
21)
101 (if (eq muse-colors-autogen-headings
'outline
)
102 (copy-face (nth (1- num
)
103 muse-colors-outline-faces-list
)
105 (copy-face 'default newsym
)))
106 ((eq muse-colors-autogen-headings
'outline
)
107 (eval `(defface ,newsym
110 muse-colors-outline-faces-list
))))
112 :group
'muse-colors
)))
114 (eval `(defface ,newsym
115 '((t (:height
,(1+ (* 0.1 (- 5 num
)))
116 :inherit variable-pitch
119 :group
'muse-colors
)))))))
121 (defface muse-link-face
122 '((((class color
) (background light
))
123 (:foreground
"green" :underline
"green" :bold t
))
124 (((class color
) (background dark
))
125 (:foreground
"cyan" :underline
"cyan" :bold t
))
127 "Face for Muse cross-references."
130 (defface muse-bad-link-face
131 '((((class color
) (background light
))
132 (:foreground
"red" :underline
"red" :bold t
))
133 (((class color
) (background dark
))
134 (:foreground
"coral" :underline
"coral" :bold t
))
136 "Face for bad Muse cross-references."
139 (defface muse-verbatim-face
140 '((((class color
) (background light
))
141 (:foreground
"slate gray"))
142 (((class color
) (background dark
))
143 (:foreground
"gray")))
144 "Face for verbatim text."
147 (defcustom muse-colors-buffer-hook nil
148 "A hook run after a region is highlighted.
149 Each function receives three arguments: BEG END VERBOSE.
150 BEG and END mark the range being highlighted, and VERBOSE specifies
151 whether progress messages should be displayed to the user."
155 (defvar muse-colors-regexp nil
)
156 (defvar muse-colors-vector nil
)
158 (defun muse-configure-highlighting (sym val
)
159 (setq muse-colors-regexp
160 (concat "\\(" (mapconcat (function
162 (if (symbolp (car rule
))
163 (symbol-value (car rule
))
164 (car rule
)))) val
"\\|") "\\)")
165 muse-colors-vector
(make-vector 128 nil
))
168 (if (eq (cadr (car rules
)) t
)
171 (unless (aref muse-colors-vector i
)
172 (aset muse-colors-vector i
(nth 2 (car rules
))))
174 (aset muse-colors-vector
(cadr (car rules
))
175 (nth 2 (car rules
))))
176 (setq rules
(cdr rules
))))
182 (defun muse-colors-emphasized ()
183 ;; Here we need to check four different points - the start and end
184 ;; of the leading *s, and the start and end of the trailing *s. We
185 ;; allow the outsides to be surrounded by whitespace or punctuation,
186 ;; but no word characters, and the insides must not be surrounded by
187 ;; whitespace or punctuation. Thus the following are valid:
191 ;; and the following is invalid:
193 (let* ((beg (match-beginning 0))
197 (unless (eq (get-text-property beg
'invisible
) 'muse
)
198 ;; check if it's a header
199 (if (eq (char-after e1
) ?\
)
200 (when (or (= beg
(point-min))
201 (eq (char-before beg
) ?
\n))
203 (muse-line-beginning-position) (muse-line-end-position)
204 (list 'face
(intern (concat "muse-header-"
205 (int-to-string leader
))))))
206 ;; beginning of line or space or symbol
207 (when (or (= beg
(point-min))
208 (eq (char-syntax (char-before beg
)) ?\
)
209 (memq (char-before beg
)
210 '(?\- ?\
[ ?\
< ?\
( ?
\' ?\
` ?
\" ?
\n)))
212 (skip-chars-forward "^*<>\n" end
)
213 (when (eq (char-after) ?
\n)
215 (skip-chars-forward "^*<>" end
))
217 (skip-chars-forward "*" end
)
219 ;; Abort if space exists just before end
222 ;; or word constituent follows
223 (unless (or (> leader
5)
224 (not (eq leader
(- e2 b2
)))
225 (eq (char-syntax (char-before b2
)) ?\
)
226 (not (eq (char-after b2
) ?
*))
228 (eq (char-syntax (char-after (1+ b2
))) ?w
)))
229 (add-text-properties beg e1
'(invisible muse
))
231 e1 b2
(list 'face
(cond ((= leader
1) 'italic
)
233 ((= leader
3) 'bold-italic
))))
234 (add-text-properties b2 e2
'(invisible muse
))
237 beg e2
'(font-lock-multiline t
))))))))))
239 (defun muse-colors-underlined ()
240 (let ((start (match-beginning 0))
242 (unless (eq (get-text-property start
'invisible
) 'muse
)
243 ;; beginning of line or space or symbol
244 (when (or (= start
(point-min))
245 (eq (char-syntax (char-before start
)) ?\
)
246 (memq (char-before start
)
247 '(?\- ?\
[ ?\
< ?\
( ?
\' ?\
` ?
\" ?
\n)))
249 (skip-chars-forward "^_<>\n" end
)
250 (when (eq (char-after) ?
\n)
252 (skip-chars-forward "^_<>" end
))
253 ;; Abort if space exists just before end
255 ;; or word constituent follows
256 (unless (or (eq (char-syntax (char-before (point))) ?\
)
257 (not (eq (char-after (point)) ?_
))
259 (eq (char-syntax (char-after (1+ (point)))) ?w
)))
260 (add-text-properties start
(1+ start
) '(invisible muse
))
261 (add-text-properties (1+ start
) (point) '(face underline
))
262 (add-text-properties (point)
263 (min (1+ (point)) (point-max))
267 start
(min (1+ (point)) (point-max))
268 '(font-lock-multiline t
)))))))))
270 (defun muse-colors-verbatim ()
271 (let ((start (match-beginning 0))
273 (unless (eq (get-text-property start
'invisible
) 'muse
)
274 ;; beginning of line or space or symbol
275 (when (or (= start
(point-min))
276 (eq (char-syntax (char-before start
)) ?\
)
277 (memq (char-before start
)
278 '(?\- ?\
[ ?\
< ?\
( ?
\' ?\
` ?
\" ?
\n)))
280 (skip-chars-forward "^=\n" end
)
281 (when (eq (char-after) ?
\n)
283 (skip-chars-forward "^=" end
))
284 ;; Abort if space exists just before end
286 ;; or word constituent follows
287 (unless (or (eq (char-syntax (char-before (point))) ?\
)
288 (not (eq (char-after (point)) ?
=))
290 (eq (char-syntax (char-after (1+ (point)))) ?w
)))
291 (add-text-properties start
(1+ start
) '(invisible muse
))
292 (add-text-properties (1+ start
) (point) '(face muse-verbatim-face
))
293 (add-text-properties (point)
294 (min (1+ (point)) (point-max))
298 start
(min (1+ (point)) (point-max))
299 '(font-lock-multiline t
)))))))))
301 (defcustom muse-colors-markup
302 `(;; make emphasized text appear emphasized
303 ("\\*\\{1,5\\}" ?
* muse-colors-emphasized
)
305 ;; make underlined text appear underlined
306 (,(concat "_[^" muse-regexp-space
"_]")
307 ?_ muse-colors-underlined
)
309 ("^#title " ?\
# muse-colors-title
)
311 (muse-explicit-link-regexp ?\
[ muse-colors-explicit-link
)
313 ;; render in teletype and suppress further parsing
314 (,(concat "=[^" muse-regexp-space
"=]") ?
= muse-colors-verbatim
)
316 ;; highlight any markup tags encountered
317 (muse-tag-regexp ?\
< muse-colors-custom-tags
)
319 ;; this has to come later since it doesn't have a special
320 ;; character in the second cell
321 (muse-url-regexp t muse-colors-implicit-link
)
323 "Expressions to highlight an Emacs Muse buffer.
324 These are arranged in a rather special fashion, so as to be as quick as
327 Each element of the list is itself a list, of the form:
329 (LOCATE-REGEXP TEST-CHAR MATCH-FUNCTION)
331 LOCATE-REGEXP is a partial regexp, and should be the smallest possible
332 regexp to differentiate this rule from other rules. It may also be a
333 symbol containing such a regexp. The buffer region is scanned only
334 once, and LOCATE-REGEXP indicates where the scanner should stop to
335 look for highlighting possibilities.
337 TEST-CHAR is a char or t. The character should match the beginning
338 text matched by LOCATE-REGEXP. These chars are used to build a vector
339 for fast MATCH-FUNCTION calling.
341 MATCH-FUNCTION is the function called when a region has been
342 identified. It is responsible for adding the appropriate text
343 properties to change the appearance of the buffer.
345 This markup is used to modify the appearance of the original text to
346 make it look more like the published HTML would look (like making some
347 markup text invisible, inlining images, etc).
349 font-lock is used to apply the markup rules, so that they can happen
350 on a deferred basis. They are not always accurate, but you can use
351 \\[font-lock-fontifty-block] near the point of error to force
352 fontification in that area.
354 Lastly, none of the regexp should contain grouping elements that will
355 affect the match data results."
357 (list :tag
"Highlight rule"
358 (choice (regexp :tag
"Locate regexp")
359 (symbol :tag
"Regexp symbol"))
360 (choice (character :tag
"Confirm character")
361 (const :tag
"Default rule" t
))
363 :set
'muse-configure-highlighting
366 ;; XEmacs users don't have `font-lock-multiline'.
367 (unless (boundp 'font-lock-multiline
)
368 (defvar font-lock-multiline nil
))
370 (defun muse-use-font-lock ()
371 (muse-add-to-invisibility-spec 'muse
)
372 (set (make-local-variable 'font-lock-multiline
) 'undecided
)
373 (set (make-local-variable 'font-lock-defaults
)
374 `(nil t nil nil
'beginning-of-line
375 (font-lock-fontify-region-function . muse-colors-region
)
376 (font-lock-unfontify-region-function
377 . muse-unhighlight-region
)))
378 (set (make-local-variable 'font-lock-fontify-region-function
)
380 (set (make-local-variable 'font-lock-unfontify-region-function
)
381 'muse-unhighlight-region
)
383 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup
)
386 (defun muse-colors-buffer ()
387 "Re-highlight the entire Muse buffer."
389 (muse-colors-region (point-min) (point-max) t
))
391 (defun muse-colors-region (beg end
&optional verbose
)
392 "Apply highlighting according to `muse-colors-markup'.
393 Note that this function should NOT change the buffer, nor should any
394 of the functions listed in `muse-colors-markup'."
395 (let ((buffer-undo-list t
)
396 (inhibit-read-only t
)
397 (inhibit-point-motion-hooks t
)
398 (inhibit-modification-hooks t
)
399 (modified-p (buffer-modified-p))
405 ;; check to see if we should expand the beg/end area for
406 ;; proper multiline matches
407 (when (and font-lock-multiline
409 (get-text-property (1- beg
) 'font-lock-multiline
))
410 ;; We are just after or in a multiline match.
411 (setq beg
(or (previous-single-property-change
412 beg
'font-lock-multiline
)
415 (setq beg
(muse-line-beginning-position)))
416 (when font-lock-multiline
417 (setq end
(or (text-property-any end
(point-max)
418 'font-lock-multiline nil
)
421 (setq end
(muse-line-beginning-position 2))
422 ;; Undo any fontification in the area.
423 (font-lock-unfontify-region beg end
)
424 ;; And apply fontification based on `muse-colors-markup'
425 (let ((len (float (- end beg
)))
426 (case-fold-search nil
)
429 (while (and (< (point) end
)
430 (re-search-forward muse-colors-regexp end t
))
432 (message "Highlighting buffer...%d%%"
433 (* (/ (float (- (point) beg
)) len
) 100)))
435 (aref muse-colors-vector
436 (char-after (match-beginning 0))))
437 (when markup-func
(funcall markup-func
)))
438 (run-hook-with-args 'muse-colors-buffer-hook
440 (if verbose
(message "Highlighting buffer...done")))))
441 (set-buffer-modified-p modified-p
))))
443 (defcustom muse-colors-tags
444 '(("example" t nil muse-colors-example-tag
)
445 ("code" t nil muse-colors-example-tag
)
446 ("verbatim" t nil muse-colors-literal-tag
)
447 ("lisp" t nil muse-colors-literal-tag
)
448 ("literal" t nil muse-colors-literal-tag
))
449 "A list of tag specifications for specially highlighting text.
450 XML-style tags are the best way to add custom highlighting to Muse.
451 This is easily accomplished by customizing this list of markup tags.
453 For each entry, the name of the tag is given, whether it expects
454 a closing tag and/or an optional set of attributes, and a
455 function that performs whatever action is desired within the
458 The function is called with three arguments, the beginning and
459 end of the region surrounded by the tags. If properties are
460 allowed, they are passed as a third argument in the form of an
461 alist. The `end' argument to the function is the last character
462 of the enclosed tag or region.
464 Functions should not modify the contents of the buffer."
465 :type
'(repeat (list (string :tag
"Markup tag")
466 (boolean :tag
"Expect closing tag" :value t
)
467 (boolean :tag
"Parse attributes" :value nil
)
471 (defsubst muse-colors-tag-info
(tagname &rest args
)
472 (assoc tagname muse-colors-tags
))
474 (defun muse-colors-custom-tags ()
475 "Highlight `muse-colors-tags'."
476 (let ((tag-info (muse-colors-tag-info (match-string 4))))
478 (let ((closed-tag (match-string 3))
479 (start (match-beginning 0))
481 (when (nth 2 tag-info
)
482 (let ((attrstr (match-string 2)))
484 (string-match (concat "\\([^"
487 "\\([^\"]+\\)\"\\)?")
489 (let ((attr (cons (downcase
490 (muse-match-string-no-properties 1 attrstr
))
491 (muse-match-string-no-properties 3 attrstr
))))
492 (setq attrstr
(replace-match "" t t attrstr
))
494 (nconc attrs
(list attr
))
495 (setq attrs
(list attr
)))))))
496 (if (and (cadr tag-info
) (not closed-tag
))
497 (if (search-forward (concat "</" (car tag-info
) ">") nil t
)
498 (setq end
(match-end 0))
499 (setq tag-info nil
)))
501 (let ((args (list start end
)))
503 (nconc args
(list attrs
)))
504 (apply (nth 3 tag-info
) args
)))))))
506 (defun muse-unhighlight-region (begin end
&optional verbose
)
507 "Remove all visual highlights in the buffer (except font-lock)."
508 (let ((buffer-undo-list t
)
509 (inhibit-read-only t
)
510 (inhibit-point-motion-hooks t
)
511 (inhibit-modification-hooks t
)
512 (modified-p (buffer-modified-p))
515 (remove-text-properties
516 begin end
'(face nil font-lock-multiline nil
517 invisible nil intangible nil display nil
518 mouse-face nil keymap nil help-echo nil
))
519 (set-buffer-modified-p modified-p
))))
521 (defun muse-colors-example-tag (beg end
)
522 "Strip properties and colorize with `muse-verbatim-face'."
523 (muse-unhighlight-region beg end
)
524 (let ((multi (save-excursion
528 (add-text-properties beg end
`(face muse-verbatim-face
529 font-lock-multiline
,multi
))))
531 (defun muse-colors-literal-tag (beg end
)
532 "Strip properties and mark as literal."
533 (muse-unhighlight-region beg end
)
534 (let ((multi (save-excursion
538 (add-text-properties beg end
`(font-lock-multiline ,multi
))))
540 (defvar muse-mode-local-map
541 (let ((map (make-sparse-keymap)))
542 (define-key map
[return] 'muse-follow-name-at-point)
543 (define-key map [(control ?m)] 'muse-follow-name-at-point)
544 (define-key map [(shift return)] 'muse-follow-name-at-point-other-window)
545 (if (featurep 'xemacs)
547 (define-key map [(button2)] 'muse-follow-name-at-mouse)
548 (define-key map [(shift button2)]
549 'muse-follow-name-at-mouse-other-window))
550 (define-key map [(shift control ?m)]
551 'muse-follow-name-at-point-other-window)
552 (define-key map [mouse-2] 'muse-follow-name-at-mouse)
553 (define-key map [(shift mouse-2)]
554 'muse-follow-name-at-mouse-other-window)
555 (unless (eq emacs-major-version 21)
556 (set-keymap-parent map muse-mode-map)))
558 "Local keymap used by Muse while on a link.")
560 (defvar muse-keymap-property
561 (if (or (featurep 'xemacs)
562 (>= emacs-major-version 21))
566 (defsubst muse-link-properties (help-str &optional face)
568 (list 'face face 'rear-nonsticky t
569 muse-keymap-property muse-mode-local-map)
570 (list 'invisible 'muse 'intangible t 'rear-nonsticky t
571 muse-keymap-property muse-mode-local-map))
572 (list 'mouse-face 'highlight
574 muse-keymap-property muse-mode-local-map)))
576 (defun muse-link-face (link-name &optional explicit)
577 "Return the type of LINK-NAME as a face symbol.
578 For EXPLICIT links, this is either a normal link or a bad-link
579 face. For implicit links, it is either colored normally or
582 (let ((link (if explicit
583 (muse-handle-explicit-link link-name)
584 (muse-handle-implicit-link link-name))))
586 (if (or (not explicit)
587 (string-match muse-file-regexp link)
588 (string-match muse-url-regexp link))
590 (if (not (featurep 'muse-project))
592 (if (string-match "#" link)
593 (setq link (substring link 0 (match-beginning 0))))
594 (if (or (and (muse-project-of-file)
595 (muse-project-page-file link muse-current-project t))
596 (file-exists-p link))
599 'muse-bad-link-face))))))))
601 (defun muse-colors-explicit-link ()
602 "Color explicit links."
603 (when (eq ?\[ (char-after (match-beginning 0)))
604 ;; remove flyspell overlays
605 (when (fboundp 'flyspell-unhighlight-at)
606 (let ((cur (match-beginning 0)))
607 (while (> (match-end 0) cur)
608 (flyspell-unhighlight-at cur)
609 (setq cur (1+ cur)))))
611 (goto-char (match-beginning 0))
612 (looking-at muse-explicit-link-regexp))
613 (let* ((link (muse-match-string-no-properties 1))
614 (desc (muse-match-string-no-properties 2))
615 (props (muse-link-properties
616 desc (muse-link-face link t)))
617 (invis-props (append props (muse-link-properties desc))))
620 ;; we put the normal face properties on the invisible
621 ;; portion too, since emacs sometimes will position
622 ;; the cursor on an intangible character
623 (add-text-properties (match-beginning 0)
624 (match-beginning 2) invis-props)
625 (add-text-properties (match-beginning 2) (match-end 2) props)
626 (add-text-properties (match-end 2) (match-end 0) invis-props))
627 (add-text-properties (match-beginning 0)
628 (match-beginning 1) invis-props)
629 (add-text-properties (match-beginning 1) (match-end 0) props)
630 (add-text-properties (match-end 1) (match-end 0) invis-props))
631 (goto-char (match-end 0))
633 (match-beginning 0) (match-end 0)
634 (muse-link-properties (muse-match-string-no-properties 0)
635 (muse-link-face link t)))
636 (goto-char (match-end 0)))))
638 (defun muse-colors-implicit-link ()
639 "Color implicit links."
640 ;; remove flyspell overlays
641 (when (fboundp 'flyspell-unhighlight-at)
642 (let ((cur (match-beginning 0)))
643 (while (> (match-end 0) cur)
644 (flyspell-unhighlight-at cur)
645 (setq cur (1+ cur)))))
646 (unless (or (eq (get-text-property (match-beginning 0) 'invisible) 'muse)
647 (eq (char-before (match-beginning 0)) ?\")
648 (eq (char-after (match-end 0)) ?\"))
649 (let ((link (muse-match-string-no-properties 1))
650 (face (muse-link-face (match-string 1))))
652 (add-text-properties (match-beginning 1) (match-end 0)
653 (muse-link-properties
654 (muse-match-string-no-properties 1) face))))))
656 (defun muse-colors-title ()
657 (add-text-properties (+ 7 (match-beginning 0))
658 (muse-line-end-position)
659 '(face muse-header-1)))
661 (provide 'muse-colors)
663 ;;; muse-colors.el ends here