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
)
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 "Outline faces to use when assigning Muse header faces.")
80 (defun muse-make-faces ()
82 (dolist (num '(1 2 3 4))
83 (setq newsym
(intern (concat "muse-header-"
84 (int-to-string num
))))
86 ((null muse-colors-autogen-headings
)
87 (make-empty-face newsym
))
89 (if (eq muse-colors-autogen-headings
'outline
)
90 (copy-face (nth (1- num
)
91 muse-colors-outline-faces-list
)
93 (eval `(defface ,newsym
95 ,(nth (1- num
) '("24pt" "18pt" "14pt" "12pt"))
98 :group
'muse-colors
))))
99 ((< emacs-major-version
21)
100 (if (eq muse-colors-autogen-headings
'outline
)
101 (copy-face (nth (1- num
)
102 muse-colors-outline-faces-list
)
104 (copy-face 'default newsym
)))
105 ((eq muse-colors-autogen-headings
'outline
)
106 (eval `(defface ,newsym
109 muse-colors-outline-faces-list
))))
111 :group
'muse-colors
)))
113 (eval `(defface ,newsym
114 '((t (:height
,(1+ (* 0.1 (- 5 num
)))
115 :inherit variable-pitch
118 :group
'muse-colors
)))))))
120 (defface muse-link-face
121 '((((class color
) (background light
))
122 (:foreground
"green" :underline
"green" :bold t
))
123 (((class color
) (background dark
))
124 (:foreground
"cyan" :underline
"cyan" :bold t
))
126 "Face for Muse cross-references."
129 (defface muse-bad-link-face
130 '((((class color
) (background light
))
131 (:foreground
"red" :underline
"red" :bold t
))
132 (((class color
) (background dark
))
133 (:foreground
"coral" :underline
"coral" :bold t
))
135 "Face for bad Muse cross-references."
138 (defcustom muse-colors-buffer-hook nil
139 "A hook run after a region is highlighted.
140 Each function receives three arguments: BEG END VERBOSE.
141 BEG and END mark the range being highlighted, and VERBOSE specifies
142 whether progress messages should be displayed to the user."
146 (defvar muse-colors-regexp nil
)
147 (defvar muse-colors-vector nil
)
149 (defun muse-configure-highlighting (sym val
)
150 (setq muse-colors-regexp
151 (concat "\\(" (mapconcat (function
153 (if (symbolp (car rule
))
154 (symbol-value (car rule
))
155 (car rule
)))) val
"\\|") "\\)")
156 muse-colors-vector
(make-vector 128 nil
))
159 (if (eq (cadr (car rules
)) t
)
162 (unless (aref muse-colors-vector i
)
163 (aset muse-colors-vector i
(nth 2 (car rules
))))
165 (aset muse-colors-vector
(cadr (car rules
))
166 (nth 2 (car rules
))))
167 (setq rules
(cdr rules
))))
173 (defun muse-colors-emphasized ()
174 ;; Here we need to check four different points - the start and end
175 ;; of the leading *s, and the start and end of the trailing *s. We
176 ;; allow the outsides to be surrounded by whitespace or punctuation,
177 ;; but no word characters, and the insides must not be surrounded by
178 ;; whitespace or punctuation. Thus the following are valid:
182 ;; and the following is invalid:
184 (let* ((beg (match-beginning 0))
188 (unless (eq (get-text-property beg
'invisible
) 'muse
)
189 ;; check if it's a header
190 (if (eq (char-after e1
) ?\
)
191 (when (or (= beg
(point-min))
192 (eq (char-before beg
) ?
\n))
194 (muse-line-beginning-position) (muse-line-end-position)
195 (list 'face
(intern (concat "muse-header-"
196 (int-to-string leader
))))))
197 ;; beginning of line or space or symbol
198 (when (or (= beg
(point-min))
199 (memq (char-syntax (char-before beg
)) '(?\ ?\-
))
200 (memq (char-before beg
)
201 '(?\- ?\
[ ?\
< ?\
( ?
\' ?\
` ?
\" ?
\n)))
203 (skip-chars-forward "^*<>\n" end
)
204 (when (eq (char-after) ?
\n)
206 (skip-chars-forward "^*<>" end
))
208 (skip-chars-forward "*" end
)
210 ;; Abort if space exists just before end
213 (unless (or (> leader
3)
214 (not (eq leader
(- e2 b2
)))
215 (eq (char-before b2
) ?\
)
216 (not (eq (char-after b2
) ?
*)))
217 (add-text-properties beg e1
'(invisible muse
))
219 e1 b2
(list 'face
(cond ((= leader
1) 'italic
)
221 ((= leader
3) 'bold-italic
))))
222 (add-text-properties b2 e2
'(invisible muse
))
225 beg e2
'(font-lock-multiline t
))))))))))
227 (defun muse-colors-underlined ()
228 (let ((start (match-beginning 0))
230 (unless (eq (get-text-property start
'invisible
) 'muse
)
231 ;; beginning of line or space or symbol
232 (when (or (= start
(point-min))
233 (memq (char-syntax (char-before start
)) '(?\ ?\-
))
234 (memq (char-before start
)
235 '(?\- ?\
[ ?\
< ?\
( ?
\' ?\
` ?
\" ?
\n)))
237 (skip-chars-forward "^_<>\n" end
)
238 (when (eq (char-after) ?
\n)
240 (skip-chars-forward "^_<>" end
))
241 ;; Abort if space exists just before end
243 (unless (or (eq (char-before (point)) ?\
)
244 (not (eq (char-after (point)) ?_
)))
245 (add-text-properties start
(1+ start
) '(invisible muse
))
246 (add-text-properties (1+ start
) (point) '(face underline
))
247 (add-text-properties (point)
248 (min (1+ (point)) (point-max))
252 start
(min (1+ (point)) (point-max))
253 '(font-lock-multiline t
)))))))))
255 (defun muse-colors-verbatim ()
256 (skip-chars-forward (concat "^" muse-regexp-space
"=>") end
))
258 (defcustom muse-colors-markup
259 `(;; render in teletype and suppress further parsing
263 ?
= muse-colors-verbatim
)
265 ;; make emphasized text appear emphasized
266 ("\\*\\{1,4\\}" ?
* muse-colors-emphasized
)
268 ;; make underlined text appear underlined
272 ?_ muse-colors-underlined
)
274 ("^#title" ?\
# muse-colors-title
)
276 (muse-explicit-link-regexp ?\
[ muse-colors-link
)
278 ;; highlight any markup tags encountered
279 (muse-tag-regexp ?\
< muse-colors-custom-tags
)
281 "Expressions to highlight an Emacs Muse buffer.
282 These are arranged in a rather special fashion, so as to be as quick as
285 Each element of the list is itself a list, of the form:
287 (LOCATE-REGEXP TEST-CHAR MATCH-FUNCTION)
289 LOCATE-REGEXP is a partial regexp, and should be the smallest possible
290 regexp to differentiate this rule from other rules. It may also be a
291 symbol containing such a regexp. The buffer region is scanned only
292 once, and LOCATE-REGEXP indicates where the scanner should stop to
293 look for highlighting possibilities.
295 TEST-CHAR is a char or t. The character should match the beginning
296 text matched by LOCATE-REGEXP. These chars are used to build a vector
297 for fast MATCH-FUNCTION calling.
299 MATCH-FUNCTION is the function called when a region has been
300 identified. It is responsible for adding the appropriate text
301 properties to change the appearance of the buffer.
303 This markup is used to modify the appearance of the original text to
304 make it look more like the published HTML would look (like making some
305 markup text invisible, inlining images, etc).
307 font-lock is used to apply the markup rules, so that they can happen
308 on a deferred basis. They are not always accurate, but you can use
309 \\[font-lock-fontifty-block] near the point of error to force
310 fontification in that area.
312 Lastly, none of the regexp should contain grouping elements that will
313 affect the match data results."
315 (list :tag
"Highlight rule"
316 (choice (regexp :tag
"Locate regexp")
317 (symbol :tag
"Regexp symbol"))
318 (choice (character :tag
"Confirm character")
319 (const :tag
"Default rule" t
))
321 :set
'muse-configure-highlighting
324 ;; XEmacs users don't have `font-lock-multiline'.
325 (unless (boundp 'font-lock-multiline
)
326 (defvar font-lock-multiline nil
))
328 (defun muse-use-font-lock ()
329 (muse-add-to-invisibility-spec 'muse
)
330 (set (make-local-variable 'font-lock-multiline
) 'undecided
)
331 (set (make-local-variable 'font-lock-defaults
)
332 `(nil t nil nil
'beginning-of-line
333 (font-lock-fontify-region-function . muse-colors-region
)
334 (font-lock-unfontify-region-function
335 . muse-unhighlight-region
)))
336 (set (make-local-variable 'font-lock-fontify-region-function
)
338 (set (make-local-variable 'font-lock-unfontify-region-function
)
339 'muse-unhighlight-region
)
341 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup
)
344 (defun muse-colors-buffer ()
345 "Re-highlight the entire Muse buffer."
347 (muse-colors-region (point-min) (point-max) t
))
349 (defun muse-colors-region (beg end
&optional verbose
)
350 "Apply highlighting according to `muse-colors-markup'.
351 Note that this function should NOT change the buffer, nor should any
352 of the functions listed in `muse-colors-markup'."
353 (let ((buffer-undo-list t
)
354 (inhibit-read-only t
)
355 (inhibit-point-motion-hooks t
)
356 (inhibit-modification-hooks t
)
357 (modified-p (buffer-modified-p))
363 ;; check to see if we should expand the beg/end area for
364 ;; proper multiline matches
365 (when (and font-lock-multiline
367 (get-text-property (1- beg
) 'font-lock-multiline
))
368 ;; We are just after or in a multiline match.
369 (setq beg
(or (previous-single-property-change
370 beg
'font-lock-multiline
)
373 (setq beg
(muse-line-beginning-position)))
374 (when font-lock-multiline
375 (setq end
(or (text-property-any end
(point-max)
376 'font-lock-multiline nil
)
379 (setq end
(muse-line-beginning-position 2))
380 ;; Undo any fontification in the area.
381 (font-lock-unfontify-region beg end
)
382 ;; And apply fontification based on `muse-colors-markup'
383 (let ((len (float (- end beg
)))
384 (case-fold-search nil
)
388 (while (re-search-forward muse-colors-regexp end t
)
390 (message "Highlighting buffer...%d%%"
391 (* (/ (float (- (point) beg
)) len
) 100)))
393 (aref muse-colors-vector
394 (char-after (match-beginning 0))))
395 (when markup-func
(funcall markup-func
))))
396 (run-hook-with-args 'muse-colors-buffer-hook
398 (if verbose
(message "Highlighting buffer...done")))))
399 (set-buffer-modified-p modified-p
))))
401 (defcustom muse-colors-tags
402 '(("example" t nil muse-colors-example-tag
))
403 "A list of tag specifications for specially highlighting text.
404 XML-style tags are the best way to add custom highlighting to Muse.
405 This is easily accomplished by customizing this list of markup tags.
407 For each entry, the name of the tag is given, whether it expects
408 a closing tag and/or an optional set of attributes, and a
409 function that performs whatever action is desired within the
412 The function is called with three arguments, the beginning and
413 end of the region surrounded by the tags. If properties are
414 allowed, they are passed as a third argument in the form of an
415 alist. The `end' argument to the function is the last character
416 of the enclosed tag or region.
418 Functions should not modify the contents of the buffer."
419 :type
'(repeat (list (string :tag
"Markup tag")
420 (boolean :tag
"Expect closing tag" :value t
)
421 (boolean :tag
"Parse attributes" :value nil
)
425 (defsubst muse-colors-tag-info
(tagname &rest args
)
426 (assoc tagname muse-colors-tags
))
428 (defun muse-colors-custom-tags ()
429 "Highlight `muse-colors-tags'."
430 (let ((tag-info (muse-colors-tag-info (match-string 4))))
432 (let ((closed-tag (match-string 3))
433 (start (match-beginning 0))
435 (when (nth 2 tag-info
)
436 (let ((attrstr (match-string 2)))
438 (string-match (concat "\\([^"
441 "\\([^\"]+\\)\"\\)?")
443 (let ((attr (cons (downcase
444 (muse-match-string-no-properties 1 attrstr
))
445 (muse-match-string-no-properties 3 attrstr
))))
446 (setq attrstr
(replace-match "" t t attrstr
))
448 (nconc attrs
(list attr
))
449 (setq attrs
(list attr
)))))))
450 (if (and (cadr tag-info
) (not closed-tag
))
451 (if (search-forward (concat "</" (car tag-info
) ">") nil t
)
452 (setq end
(match-end 0))
453 (setq tag-info nil
)))
455 (let ((args (list start end
)))
457 (nconc args
(list attrs
)))
458 (apply (nth 3 tag-info
) args
)))))))
460 (defun muse-colors-example-tag (beg end
)
461 "Strip properties from stuff in example."
462 (set-text-properties beg end nil
)
465 (defun muse-unhighlight-region (begin end
&optional verbose
)
466 "Remove all visual highlights in the buffer (except font-lock)."
467 (let ((buffer-undo-list t
)
468 (inhibit-read-only t
)
469 (inhibit-point-motion-hooks t
)
470 (inhibit-modification-hooks t
)
471 (modified-p (buffer-modified-p))
474 (remove-text-properties
475 begin end
'(face nil font-lock-multiline nil
476 invisible nil intangible nil display nil
477 mouse-face nil keymap nil help-echo nil
))
478 (set-buffer-modified-p modified-p
))))
480 (defvar muse-mode-local-map
481 (let ((map (make-sparse-keymap)))
482 (define-key map
[return] 'muse-follow-name-at-point)
483 (define-key map [(control ?m)] 'muse-follow-name-at-point)
484 (define-key map [(shift return)] 'muse-follow-name-at-point-other-window)
485 (if (featurep 'xemacs)
487 (define-key map [(button2)] 'muse-follow-name-at-mouse)
488 (define-key map [(shift button2)]
489 'muse-follow-name-at-mouse-other-window))
490 (define-key map [(shift control ?m)]
491 'muse-follow-name-at-point-other-window)
492 (define-key map [mouse-2] 'muse-follow-name-at-mouse)
493 (define-key map [(shift mouse-2)]
494 'muse-follow-name-at-mouse-other-window)
495 (unless (eq emacs-major-version 21)
496 (set-keymap-parent map muse-mode-map)))
498 "Local keymap used by Muse while on a link.")
500 (defvar muse-keymap-property
501 (if (or (featurep 'xemacs)
502 (>= emacs-major-version 21))
506 (defsubst muse-link-properties (help-str &optional face)
508 (list 'face face 'rear-nonsticky t
509 muse-keymap-property muse-mode-local-map)
510 (list 'invisible 'muse 'intangible t 'rear-nonsticky t
511 muse-keymap-property muse-mode-local-map))
512 (list 'mouse-face 'highlight
514 muse-keymap-property muse-mode-local-map)))
516 (defun muse-link-face (link-name &optional explicit)
517 "Return the type of LINK-NAME as a face symbol.
518 For EXPLICIT links, this is either a normal link or a bad-link
519 face. For implicit links, it is either colored normally or
522 (let ((link (if explicit
523 (muse-handle-explicit-link link-name)
524 (muse-handle-implicit-link link-name))))
526 (if (or (not explicit)
527 (string-match muse-file-regexp link)
528 (string-match muse-url-regexp link))
530 (if (not (featurep 'muse-project))
532 (if (string-match "#" link)
533 (setq link (substring link 0 (match-beginning 0))))
534 (if (or (and (muse-project-of-file)
535 (muse-project-page-file
536 link-name muse-current-project t))
537 (file-exists-p link))
540 'muse-bad-link-face))))))))
542 (defun muse-colors-link ()
543 (when (eq ?\[ (char-after (match-beginning 0)))
544 ;; remove flyspell overlays
545 (when (fboundp 'flyspell-unhighlight-at)
546 (let ((cur (match-beginning 0)))
547 (while (> (match-end 0) cur)
548 (flyspell-unhighlight-at cur)
549 (setq cur (1+ cur)))))
550 (let* (;; FIXME: link is not used
551 (link (muse-match-string-no-properties 2))
552 (desc (muse-match-string-no-properties 3))
553 (props (muse-link-properties
554 desc (muse-link-face (match-string 2) t)))
555 (invis-props (append props (muse-link-properties desc))))
558 ;; we put the normal face properties on the invisible
559 ;; portion too, since emacs sometimes will position
560 ;; the cursor on an intangible character
561 (add-text-properties (match-beginning 0)
562 (match-beginning 3) invis-props)
563 (add-text-properties (match-beginning 3) (match-end 3) props)
564 (add-text-properties (match-end 3) (match-end 0) invis-props))
565 (add-text-properties (match-beginning 0)
566 (match-beginning 2) invis-props)
567 (add-text-properties (match-beginning 2) (match-end 0) props)
568 (add-text-properties (match-end 2) (match-end 0) invis-props)))
569 (goto-char (match-end 0))
571 (match-beginning 0) (match-end 0)
572 (muse-link-properties (muse-match-string-no-properties 0)
573 (muse-link-face (match-string 2) t)))
574 (goto-char (match-end 0))))
576 (defun muse-colors-title ()
577 (add-text-properties (+ 7 (match-beginning 0))
578 (muse-line-end-position)
579 '(face muse-header-1)))
581 (provide 'muse-colors)
583 ;;; muse-colors.el ends here