1 ;;; muse-colors.el --- coloring and highlighting used by Muse
3 ;; Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5 ;; Author: John Wiegley (johnw AT gnu DOT org)
6 ;; Keywords: hypermedia
7 ;; Date: Thu 11-Mar-2004
9 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
11 ;; Emacs Muse is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published
13 ;; by the Free Software Foundation; either version 3, or (at your
14 ;; option) any later version.
16 ;; Emacs Muse is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with Emacs Muse; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 ;; Lan Yufeng (nlany DOT web AT gmail DOT com) found an error where
31 ;; headings were being given the wrong face, contributing a patch to
34 ;; Sergey Vlasov (vsu AT altlinux DOT ru) fixed an issue with coloring
35 ;; links that are in consecutive lines.
37 ;; Jim Ottaway ported the <lisp> tag from emacs-wiki.
39 ;; Per B. Sederberg (per AT med DOT upenn DOT edu) contributed the
40 ;; viewing of inline images.
44 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
46 ;; Emacs Muse Highlighting
48 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
51 (require 'muse-regexps
)
54 (defgroup muse-colors nil
55 "Options controlling the behavior of Emacs Muse highlighting.
56 See `muse-colors-buffer' for more information."
59 (defcustom muse-colors-autogen-headings t
60 "Specify whether the heading faces should be auto-generated.
61 The default is to scale them.
63 Choosing 'outline will copy the colors from the outline-mode
66 If you want to customize each of the headings individually, set
68 :type
'(choice (const :tag
"Default (scaled) headings" t
)
69 (const :tag
"Use outline-mode headings" outline
)
70 (const :tag
"Don't touch the headings" nil
))
73 (defcustom muse-colors-evaluate-lisp-tags t
74 "Specify whether to evaluate the contents of <lisp> tags at
75 display time. If nil, don't evaluate them. If non-nil, evaluate
78 The actual contents of the buffer are not changed, only the
83 (defcustom muse-colors-inline-images t
84 "Specify whether to inline images inside the Emacs buffer. If
85 nil, don't inline them. If non-nil, an image link will be
86 replaced by the image.
88 The actual contents of the buffer are not changed, only whether
89 an image is displayed."
93 (defcustom muse-colors-inline-image-method
'default-directory
94 "Determine how to locate inline images.
95 Setting this to 'default-directory uses the current directory of
96 the current Muse buffer.
98 Setting this to a function calls that function with the filename
99 of the image to be inlined. The value that is returned will be
100 used as the filename of the image."
101 :type
'(choice (const :tag
"Current directory" default-directory
)
102 (const :tag
"Publishing directory"
103 muse-colors-use-publishing-directory
)
104 (function :tag
"Custom function"))
107 (defvar muse-colors-region-end nil
108 "Indicate the end of the region that is currently being font-locked.")
109 (make-variable-buffer-local 'muse-colors-region-end
)
112 (defun muse-colors-toggle-inline-images ()
113 "Toggle display of inlined images on/off."
115 ;; toggle the custom setting
116 (if (not muse-colors-inline-images
)
117 (setq muse-colors-inline-images t
)
118 (setq muse-colors-inline-images nil
))
119 ;; reprocess the buffer
120 (muse-colors-buffer))
122 (defvar muse-colors-outline-faces-list
123 (if (facep 'outline-1
)
124 '(outline-1 outline-2 outline-3 outline-4 outline-5
)
125 ;; these are equivalent in coloring to the outline faces
126 '(font-lock-function-name-face
127 font-lock-variable-name-face
128 font-lock-keyword-face
129 font-lock-builtin-face
130 font-lock-comment-face
))
131 "Outline faces to use when assigning Muse header faces.")
133 (defun muse-make-faces-default (&optional later
)
134 "Generate the default face definitions for headers."
135 (dolist (num '(1 2 3 4 5))
136 (let ((newsym (intern (concat "muse-header-" (int-to-string num
)))))
137 ;; put in the proper group and give documentation
139 (unless (featurep 'xemacs
)
140 (muse-copy-face 'variable-pitch newsym
)
141 (set-face-attribute newsym nil
:height
(1+ (* 0.1 (- 5 num
)))
143 (if (featurep 'xemacs
)
144 (eval `(defface ,newsym
146 ,(nth (1- num
) '("24pt" "18pt" "14pt" "12pt" "11pt"))
149 :group
'muse-colors
))
150 (eval `(defface ,newsym
151 '((t (:height
,(1+ (* 0.1 (- 5 num
)))
152 :inherit variable-pitch
155 :group
'muse-colors
)))))))
157 (progn (muse-make-faces-default))
159 (defun muse-make-faces (&optional frame
)
160 "Generate face definitions for headers based the user's preferences."
162 ((not muse-colors-autogen-headings
)
164 ((eq muse-colors-autogen-headings t
)
165 (muse-make-faces-default t
))
167 (dolist (num '(1 2 3 4 5))
168 (let ((newsym (intern (concat "muse-header-" (int-to-string num
)))))
169 ;; copy the desired face definition
170 (muse-copy-face (nth (1- num
) muse-colors-outline-faces-list
)
173 ;; after displaying the Emacs splash screen, the faces are wiped out,
174 ;; so recover from that
175 (add-hook 'window-setup-hook
#'muse-make-faces
)
176 ;; ditto for when a new frame is created
177 (when (boundp 'after-make-frame-functions
)
178 (add-hook 'after-make-frame-functions
#'muse-make-faces
))
181 '((((class color
) (background light
))
182 (:foreground
"blue" :underline
"blue" :bold t
))
183 (((class color
) (background dark
))
184 (:foreground
"cyan" :underline
"cyan" :bold t
))
186 "Face for Muse cross-references."
189 (defface muse-bad-link
190 '((((class color
) (background light
))
191 (:foreground
"red" :underline
"red" :bold t
))
192 (((class color
) (background dark
))
193 (:foreground
"coral" :underline
"coral" :bold t
))
195 "Face for bad Muse cross-references."
198 (defface muse-verbatim
199 '((((class color
) (background light
))
200 (:foreground
"slate gray"))
201 (((class color
) (background dark
))
202 (:foreground
"gray")))
203 "Face for verbatim text."
206 (defface muse-emphasis-1
208 "Face for italic emphasized text."
211 (defface muse-emphasis-2
213 "Face for bold emphasized text."
216 (defface muse-emphasis-3
217 '((t (:bold t
:italic t
)))
218 "Face for bold italic emphasized text."
221 (muse-copy-face 'italic
'muse-emphasis-1
)
222 (muse-copy-face 'bold
'muse-emphasis-2
)
223 (muse-copy-face 'bold-italic
'muse-emphasis-3
)
225 (defcustom muse-colors-buffer-hook nil
226 "A hook run after a region is highlighted.
227 Each function receives three arguments: BEG END VERBOSE.
228 BEG and END mark the range being highlighted, and VERBOSE specifies
229 whether progress messages should be displayed to the user."
233 (defvar muse-colors-regexp nil
234 "Regexp matching each car of `muse-colors-markup'.")
235 (defvar muse-colors-vector nil
236 "Vector of all characters that are part of Muse markup.
237 This is composed of the 2nd element of each `muse-colors-markup'
240 (defun muse-configure-highlighting (sym val
)
241 "Extract color markup information from VAL and set to SYM.
242 This is usually called with `muse-colors-markup' as both arguments."
246 (let ((value (cond ((symbolp (car rule
))
247 (symbol-value (car rule
)))
248 ((stringp (car rule
))
252 (setq rules
(cons rule rules
))
253 (setq regexps
(cons value regexps
)))))
254 (setq rules
(nreverse rules
)
255 regexps
(nreverse regexps
))
256 (setq muse-colors-regexp
(concat "\\("
257 (mapconcat #'identity regexps
"\\|")
259 muse-colors-vector
(make-vector 128 nil
))
261 (if (eq (cadr (car rules
)) t
)
264 (unless (aref muse-colors-vector i
)
265 (aset muse-colors-vector i
(nth 2 (car rules
))))
267 (aset muse-colors-vector
(cadr (car rules
))
268 (nth 2 (car rules
))))
269 (setq rules
(cdr rules
))))
272 (defun muse-colors-emphasized ()
273 "Color emphasized text and headings."
274 ;; Here we need to check four different points - the start and end
275 ;; of the leading *s, and the start and end of the trailing *s. We
276 ;; allow the outsides to be surrounded by whitespace or punctuation,
277 ;; but no word characters, and the insides must not be surrounded by
278 ;; whitespace or punctuation. Thus the following are valid:
282 ;; and the following is invalid:
284 (let* ((beg (match-beginning 0))
288 (unless (or (eq (get-text-property beg
'invisible
) 'muse
)
289 (get-text-property beg
'muse-comment
)
290 (get-text-property beg
'muse-directive
))
291 ;; check if it's a header
292 (if (eq (char-after e1
) ?\
)
293 (when (or (= beg
(point-min))
294 (eq (char-before beg
) ?
\n))
296 (muse-line-beginning-position) (muse-line-end-position)
297 (list 'face
(intern (concat "muse-header-"
298 (int-to-string leader
))))))
299 ;; beginning of line or space or symbol
300 (when (or (= beg
(point-min))
301 (eq (char-syntax (char-before beg
)) ?\
)
302 (memq (char-before beg
)
303 '(?\- ?\
[ ?\
< ?\
( ?
\' ?\
` ?
\" ?
\n)))
305 (skip-chars-forward "^*<>\n" muse-colors-region-end
)
306 (when (eq (char-after) ?
\n)
308 (skip-chars-forward "^*<>" muse-colors-region-end
))
310 (skip-chars-forward "*" muse-colors-region-end
)
312 ;; Abort if space exists just before end
315 ;; or word constituent follows
316 (unless (or (> leader
5)
317 (not (eq leader
(- e2 b2
)))
318 (eq (char-syntax (char-before b2
)) ?\
)
319 (not (eq (char-after b2
) ?
*))
321 (eq (char-syntax (char-after (1+ b2
))) ?w
)))
322 (add-text-properties beg e1
'(invisible muse
))
324 e1 b2
(list 'face
(cond ((= leader
1) 'muse-emphasis-1
)
325 ((= leader
2) 'muse-emphasis-2
)
326 ((= leader
3) 'muse-emphasis-3
))))
327 (add-text-properties b2 e2
'(invisible muse
))
330 beg e2
'(font-lock-multiline t
))))))))))
332 (defun muse-colors-underlined ()
333 "Color underlined text."
334 (let ((start (match-beginning 0))
336 (unless (or (eq (get-text-property start
'invisible
) 'muse
)
337 (get-text-property start
'muse-comment
)
338 (get-text-property start
'muse-directive
))
339 ;; beginning of line or space or symbol
340 (when (or (= start
(point-min))
341 (eq (char-syntax (char-before start
)) ?\
)
342 (memq (char-before start
)
343 '(?\- ?\
[ ?\
< ?\
( ?
\' ?\
` ?
\" ?
\n)))
345 (skip-chars-forward "^_<>\n" muse-colors-region-end
)
346 (when (eq (char-after) ?
\n)
348 (skip-chars-forward "^_<>" muse-colors-region-end
))
349 ;; Abort if space exists just before end
351 ;; or word constituent follows
352 (unless (or (eq (char-syntax (char-before (point))) ?\
)
353 (not (eq (char-after (point)) ?_
))
355 (eq (char-syntax (char-after (1+ (point)))) ?w
)))
356 (add-text-properties start
(1+ start
) '(invisible muse
))
357 (add-text-properties (1+ start
) (point) '(face underline
))
358 (add-text-properties (point)
359 (min (1+ (point)) (point-max))
363 start
(min (1+ (point)) (point-max))
364 '(font-lock-multiline t
)))))))))
366 (defun muse-colors-verbatim ()
367 "Render in teletype and suppress further parsing."
368 (let ((start (match-beginning 0))
370 (unless (or (eq (get-text-property start
'invisible
) 'muse
)
371 (get-text-property start
'muse-comment
)
372 (get-text-property start
'muse-directive
))
373 ;; beginning of line or space or symbol
374 (when (or (= start
(point-min))
375 (eq (char-syntax (char-before start
)) ?\
)
376 (memq (char-before start
)
377 '(?\- ?\
[ ?\
< ?\
( ?
\' ?\
` ?
\" ?
\n)))
379 (skip-chars-forward "^=\n" muse-colors-region-end
)
380 (when (eq (char-after) ?
\n)
382 (skip-chars-forward "^=" muse-colors-region-end
))
383 ;; Abort if space exists just before end
385 ;; or word constituent follows
386 (unless (or (eq (char-syntax (char-before (point))) ?\
)
387 (not (eq (char-after (point)) ?
=))
389 (eq (char-syntax (char-after (1+ (point)))) ?w
)))
390 (setq pos
(min (1+ (point)) (point-max)))
391 (add-text-properties start
(1+ start
) '(invisible muse
))
392 (add-text-properties (1+ start
) (point) '(face muse-verbatim
))
393 (add-text-properties (point)
394 (min (1+ (point)) (point-max))
398 start
(min (1+ (point)) (point-max))
399 '(font-lock-multiline t
))))
402 (defcustom muse-colors-markup
403 `(;; make emphasized text appear emphasized
404 ("\\*\\{1,5\\}" ?
* muse-colors-emphasized
)
406 ;; make underlined text appear underlined
407 (,(concat "_[^" muse-regexp-blank
"_\n]")
408 ?_ muse-colors-underlined
)
410 ("^#title " ?\
# muse-colors-title
)
412 (muse-explicit-link-regexp ?\
[ muse-colors-explicit-link
)
414 ;; render in teletype and suppress further parsing
415 (,(concat "=[^" muse-regexp-blank
"=\n]") ?
= muse-colors-verbatim
)
417 ;; highlight any markup tags encountered
418 (muse-tag-regexp ?\
< muse-colors-custom-tags
)
421 (,(concat "^;[" muse-regexp-blank
"]") ?\
; muse-colors-comment)
423 ;; this has to come later since it doesn't have a special
424 ;; character in the second cell
425 (muse-url-regexp t muse-colors-implicit-link
)
427 "Expressions to highlight an Emacs Muse buffer.
428 These are arranged in a rather special fashion, so as to be as quick as
431 Each element of the list is itself a list, of the form:
433 (LOCATE-REGEXP TEST-CHAR MATCH-FUNCTION)
435 LOCATE-REGEXP is a partial regexp, and should be the smallest possible
436 regexp to differentiate this rule from other rules. It may also be a
437 symbol containing such a regexp. The buffer region is scanned only
438 once, and LOCATE-REGEXP indicates where the scanner should stop to
439 look for highlighting possibilities.
441 TEST-CHAR is a char or t. The character should match the beginning
442 text matched by LOCATE-REGEXP. These chars are used to build a vector
443 for fast MATCH-FUNCTION calling.
445 MATCH-FUNCTION is the function called when a region has been
446 identified. It is responsible for adding the appropriate text
447 properties to change the appearance of the buffer.
449 This markup is used to modify the appearance of the original text to
450 make it look more like the published HTML would look (like making some
451 markup text invisible, inlining images, etc).
453 font-lock is used to apply the markup rules, so that they can happen
454 on a deferred basis. They are not always accurate, but you can use
455 \\[font-lock-fontifty-block] near the point of error to force
456 fontification in that area."
458 (list :tag
"Highlight rule"
459 (choice (regexp :tag
"Locate regexp")
460 (symbol :tag
"Regexp symbol"))
461 (choice (character :tag
"Confirm character")
462 (const :tag
"Default rule" t
))
464 :set
'muse-configure-highlighting
467 ;; XEmacs users don't have `font-lock-multiline'.
468 (unless (boundp 'font-lock-multiline
)
469 (defvar font-lock-multiline nil
))
471 (defun muse-use-font-lock ()
472 "Set up font-locking for Muse."
473 (muse-add-to-invisibility-spec 'muse
)
474 (set (make-local-variable 'font-lock-multiline
) 'undecided
)
475 (set (make-local-variable 'font-lock-defaults
)
476 `(nil t nil nil beginning-of-line
477 (font-lock-fontify-region-function . muse-colors-region
)
478 (font-lock-unfontify-region-function
479 . muse-unhighlight-region
)))
480 (set (make-local-variable 'font-lock-fontify-region-function
)
482 (set (make-local-variable 'font-lock-unfontify-region-function
)
483 'muse-unhighlight-region
)
485 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup
)
488 (defun muse-colors-buffer ()
489 "Re-highlight the entire Muse buffer."
491 (muse-colors-region (point-min) (point-max) t
))
493 (defvar muse-colors-fontifying-p nil
494 "Indicate whether Muse is fontifying the current buffer.")
495 (make-variable-buffer-local 'muse-colors-fontifying-p
)
497 (defvar muse-colors-delayed-commands nil
498 "Commands to be run immediately after highlighting a region.
500 This is meant to accommodate highlighting <lisp> in #title
501 directives after everything else.
503 It may be modified by Muse functions during highlighting, but not
505 (make-variable-buffer-local 'muse-colors-delayed-commands
)
507 (defun muse-colors-region (beg end
&optional verbose
)
508 "Apply highlighting according to `muse-colors-markup'.
509 Note that this function should NOT change the buffer, nor should any
510 of the functions listed in `muse-colors-markup'."
511 (let ((buffer-undo-list t
)
512 (inhibit-read-only t
)
513 (inhibit-point-motion-hooks t
)
514 (inhibit-modification-hooks t
)
515 (modified-p (buffer-modified-p))
516 (muse-colors-fontifying-p t
)
517 (muse-colors-region-end (muse-line-end-position end
))
518 (muse-colors-delayed-commands nil
)
524 ;; check to see if we should expand the beg/end area for
525 ;; proper multiline matches
526 (when (and font-lock-multiline
528 (get-text-property (1- beg
) 'font-lock-multiline
))
529 ;; We are just after or in a multiline match.
530 (setq beg
(or (previous-single-property-change
531 beg
'font-lock-multiline
)
534 (setq beg
(muse-line-beginning-position)))
535 (when font-lock-multiline
536 (setq end
(or (text-property-any end
(point-max)
537 'font-lock-multiline nil
)
540 (setq end
(muse-line-beginning-position 2))
541 ;; Undo any fontification in the area.
542 (font-lock-unfontify-region beg end
)
543 ;; And apply fontification based on `muse-colors-markup'
544 (let ((len (float (- end beg
)))
545 (case-fold-search nil
)
548 (while (and (< (point) end
)
549 (re-search-forward muse-colors-regexp end t
))
551 (message "Highlighting buffer...%d%%"
552 (* (/ (float (- (point) beg
)) len
) 100)))
554 (aref muse-colors-vector
555 (char-after (match-beginning 0))))
556 (when markup-func
(funcall markup-func
)))
557 (dolist (command muse-colors-delayed-commands
)
558 (apply (car command
) (cdr command
)))
559 (run-hook-with-args 'muse-colors-buffer-hook
561 (if verbose
(message "Highlighting buffer...done")))))
562 (set-buffer-modified-p modified-p
))))
564 (defcustom muse-colors-tags
565 '(("example" t nil nil muse-colors-example-tag
)
566 ("code" t nil nil muse-colors-example-tag
)
567 ("verbatim" t nil nil muse-colors-literal-tag
)
568 ("lisp" t t nil muse-colors-lisp-tag
)
569 ("literal" t nil nil muse-colors-literal-tag
))
570 "A list of tag specifications for specially highlighting text.
571 XML-style tags are the best way to add custom highlighting to Muse.
572 This is easily accomplished by customizing this list of markup tags.
574 For each entry, the name of the tag is given, whether it expects
575 a closing tag and/or an optional set of attributes, whether it is
576 nestable, and a function that performs whatever action is desired
577 within the delimited region.
579 The function is called with three arguments, the beginning and
580 end of the region surrounded by the tags. If properties are
581 allowed, they are passed as a third argument in the form of an
582 alist. The `end' argument to the function is the last character
583 of the enclosed tag or region.
585 Functions should not modify the contents of the buffer."
586 :type
'(repeat (list (string :tag
"Markup tag")
587 (boolean :tag
"Expect closing tag" :value t
)
588 (boolean :tag
"Parse attributes" :value nil
)
589 (boolean :tag
"Nestable" :value nil
)
593 (defvar muse-colors-inhibit-tags-in-directives t
594 "If non-nil, don't allow tags to be interpreted in directives.
595 This is used to delay highlighting of <lisp> tags in #title until later.")
596 (make-variable-buffer-local 'muse-colors-inhibit-tags-in-directives
)
598 (defsubst muse-colors-tag-info
(tagname &rest args
)
599 "Get tag info associated with TAGNAME, ignoring ARGS."
600 (assoc tagname muse-colors-tags
))
602 (defun muse-colors-custom-tags ()
603 "Highlight `muse-colors-tags'."
605 (goto-char (match-beginning 0))
606 (looking-at muse-tag-regexp
))
607 (let ((tag-info (muse-colors-tag-info (match-string 1))))
608 (unless (or (not tag-info
)
609 (get-text-property (match-beginning 0) 'muse-comment
)
610 (and muse-colors-inhibit-tags-in-directives
611 (get-text-property (match-beginning 0) 'muse-directive
)))
612 (let ((closed-tag (match-string 3))
613 (start (match-beginning 0))
615 (when (nth 2 tag-info
)
616 (let ((attrstr (match-string 2)))
618 (string-match (concat "\\([^"
621 "\\([^\"]+\\)\"\\)?")
623 (let ((attr (cons (downcase
624 (muse-match-string-no-properties 1 attrstr
))
625 (muse-match-string-no-properties 3 attrstr
))))
626 (setq attrstr
(replace-match "" t t attrstr
))
628 (nconc attrs
(list attr
))
629 (setq attrs
(list attr
)))))))
630 (if (and (cadr tag-info
) (not closed-tag
))
631 (if (muse-goto-tag-end (car tag-info
) (nth 3 tag-info
))
632 (setq end
(match-end 0))
633 (setq tag-info nil
)))
635 (let ((args (list start end
)))
637 (nconc args
(list attrs
)))
638 (apply (nth 4 tag-info
) args
)))))))
640 (defun muse-unhighlight-region (begin end
&optional verbose
)
641 "Remove all visual highlights in the buffer (except font-lock)."
642 (let ((buffer-undo-list t
)
643 (inhibit-read-only t
)
644 (inhibit-point-motion-hooks t
)
645 (inhibit-modification-hooks t
)
646 (modified-p (buffer-modified-p))
649 (remove-text-properties
650 begin end
'(face nil font-lock-multiline nil end-glyph nil
651 invisible nil intangible nil display nil
652 mouse-face nil keymap nil help-echo nil
653 muse-link nil muse-directive nil muse-comment nil
))
654 (set-buffer-modified-p modified-p
))))
656 (defun muse-colors-example-tag (beg end
)
657 "Strip properties and colorize with `muse-verbatim'."
658 (muse-unhighlight-region beg end
)
659 (let ((multi (save-excursion
663 (add-text-properties beg end
`(face muse-verbatim
664 font-lock-multiline
,multi
))))
666 (defun muse-colors-literal-tag (beg end
)
667 "Strip properties and mark as literal."
668 (muse-unhighlight-region beg end
)
669 (let ((multi (save-excursion
673 (add-text-properties beg end
`(font-lock-multiline ,multi
))))
675 (defun muse-colors-lisp-tag (beg end attrs
)
676 "Color the region enclosed by a <lisp> tag."
677 (if (not muse-colors-evaluate-lisp-tags
)
678 (muse-colors-literal-tag beg end
)
679 (muse-unhighlight-region beg end
)
680 (let (beg-lisp end-lisp
)
683 (setq beg-lisp
(and (looking-at "<[^>]+>")
686 (setq end-lisp
(and (muse-looking-back "</[^>]+>")
687 (match-beginning 0))))
690 (list 'font-lock-multiline t
691 'display
(muse-eval-lisp
694 (buffer-substring-no-properties beg-lisp end-lisp
)
698 (defvar muse-mode-local-map
699 (let ((map (make-sparse-keymap)))
700 (define-key map
[return] 'muse-follow-name-at-point)
701 (define-key map [(control ?m)] 'muse-follow-name-at-point)
702 (define-key map [(shift return)] 'muse-follow-name-at-point-other-window)
703 (if (featurep 'xemacs)
705 (define-key map [(button2)] 'muse-follow-name-at-mouse)
706 (define-key map [(shift button2)]
707 'muse-follow-name-at-mouse-other-window))
708 (define-key map [(shift control ?m)]
709 'muse-follow-name-at-point-other-window)
710 (define-key map [mouse-2] 'muse-follow-name-at-mouse)
711 (define-key map [(shift mouse-2)]
712 'muse-follow-name-at-mouse-other-window)
713 (unless (eq emacs-major-version 21)
714 (set-keymap-parent map muse-mode-map)))
716 "Local keymap used by Muse while on a link.")
718 (defvar muse-keymap-property
719 (if (or (featurep 'xemacs)
720 (>= emacs-major-version 21))
723 "The name of the keymap or local-map property.")
725 (defsubst muse-link-properties (help-str &optional face)
726 "Determine text properties to use for a link."
728 (list 'face face 'mouse-face 'highlight 'muse-link t)
729 (list 'invisible 'muse 'intangible t))
730 (list 'help-echo help-str 'rear-nonsticky t
731 muse-keymap-property muse-mode-local-map)))
733 (defun muse-link-face (link-name &optional explicit)
734 "Return the type of LINK-NAME as a face symbol.
735 For EXPLICIT links, this is either a normal link or a bad-link
736 face. For implicit links, it is either colored normally or
739 (let ((link (if explicit
740 (muse-handle-explicit-link link-name)
741 (muse-handle-implicit-link link-name))))
743 (cond ((string-match muse-url-regexp link)
745 ((muse-file-remote-p link)
747 ((string-match muse-file-regexp link)
748 (when (string-match "/[^/]+#[^#./]+\\'" link)
749 ;; strip anchor from the end of a path
750 (setq link (substring link 0 (match-beginning 0))))
751 (if (file-exists-p link)
754 ((not (featurep 'muse-project))
757 (if (string-match "#" link)
758 (setq link (substring link 0 (match-beginning 0))))
759 (if (or (and (muse-project-of-file)
760 (muse-project-page-file
761 link muse-current-project t))
762 (file-exists-p link))
764 'muse-bad-link)))))))
766 (defun muse-colors-use-publishing-directory (link)
767 "Make LINK relative to the directory where we will publish the
769 (let ((style (car (muse-project-applicable-styles
770 link (cddr (muse-project)))))
773 (setq path (muse-style-element :path style)))
774 (expand-file-name link path))))
776 (defun muse-colors-resolve-image-file (link)
777 "Determine if we can create images and see if the link is an image
780 (and (or (fboundp 'create-image)
781 (fboundp 'make-glyph))
782 (not (string-match "\\`[uU][rR][lL]:" link))
783 (string-match muse-image-regexp link))))
785 (defun muse-make-file-glyph (filename)
786 "Given a file name, return a newly-created image glyph.
787 This is a hack for supporting inline images in XEmacs."
788 (let ((case-fold-search nil))
789 ;; Scan filename to determine image type
790 (when (fboundp 'make-glyph)
792 (cond ((string-match "jpe?g" filename)
793 (make-glyph (vector 'jpeg :file filename) 'buffer))
794 ((string-match "gif" filename)
795 (make-glyph (vector 'gif :file filename) 'buffer))
796 ((string-match "png" filename)
797 (make-glyph (vector 'png :file filename) 'buffer)))))))
799 (defun muse-colors-insert-image (link beg end invis-props)
800 "Create an image using create-image or make-glyph and insert it
801 in place of an image link defined by BEG and END."
802 (setq link (expand-file-name link))
803 (let ((image-file (cond
804 ((eq muse-colors-inline-image-method 'default-directory)
806 ((functionp muse-colors-inline-image-method)
807 (funcall muse-colors-inline-image-method link))))
809 (when (stringp image-file)
810 (if (fboundp 'create-image)
811 ;; use create-image and display property
812 (let ((display-stuff (condition-case nil
813 (create-image image-file)
816 (add-text-properties beg end (list 'display display-stuff))))
817 ;; use make-glyph and invisible property
818 (and (setq glyph (muse-make-file-glyph image-file))
820 (add-text-properties beg end invis-props)
821 (add-text-properties beg end (list
823 'help-echo link))))))))
825 (defun muse-colors-explicit-link ()
826 "Color explicit links."
827 (when (and (eq ?\[ (char-after (match-beginning 0)))
828 (not (get-text-property (match-beginning 0) 'muse-comment))
829 (not (get-text-property (match-beginning 0) 'muse-directive)))
830 ;; remove flyspell overlays
831 (when (fboundp 'flyspell-unhighlight-at)
832 (let ((cur (match-beginning 0)))
833 (while (> (match-end 0) cur)
834 (flyspell-unhighlight-at cur)
835 (setq cur (1+ cur)))))
837 (goto-char (match-beginning 0))
838 (looking-at muse-explicit-link-regexp))
839 (let* ((unesc-link (muse-get-link))
840 (unesc-desc (muse-get-link-desc))
841 (link (muse-link-unescape unesc-link))
842 (desc (muse-link-unescape unesc-desc))
843 (props (muse-link-properties desc (muse-link-face link t)))
844 (invis-props (append props (muse-link-properties desc))))
845 ;; see if we should try and inline an image
846 (if (and muse-colors-inline-images
847 (or (muse-colors-resolve-image-file link)
849 (muse-colors-resolve-image-file desc)
851 ;; we found an image, so inline it
852 (muse-colors-insert-image
854 (match-beginning 0) (match-end 0) invis-props)
857 ;; we put the normal face properties on the invisible
858 ;; portion too, since emacs sometimes will position
859 ;; the cursor on an intangible character
860 (add-text-properties (match-beginning 0)
861 (match-beginning 2) invis-props)
862 (add-text-properties (match-beginning 2) (match-end 2) props)
863 (add-text-properties (match-end 2) (match-end 0) invis-props)
864 ;; in case specials were escaped, cause the unescaped
865 ;; text to be displayed
866 (unless (string= desc unesc-desc)
867 (add-text-properties (match-beginning 2) (match-end 2)
868 (list 'display desc))))
869 (add-text-properties (match-beginning 0)
870 (match-beginning 1) invis-props)
871 (add-text-properties (match-beginning 1) (match-end 0) props)
872 (add-text-properties (match-end 1) (match-end 0) invis-props)
873 (unless (string= link unesc-link)
874 (add-text-properties (match-beginning 1) (match-end 1)
875 (list 'display link))))
876 (goto-char (match-end 0))
878 (match-beginning 0) (match-end 0)
879 (muse-link-properties (muse-match-string-no-properties 0)
880 (muse-link-face link t)))))))
882 (defun muse-colors-implicit-link ()
883 "Color implicit links."
884 (unless (or (eq (get-text-property (match-beginning 0) 'invisible) 'muse)
885 (get-text-property (match-beginning 0) 'muse-comment)
886 (get-text-property (match-beginning 0) 'muse-directive)
887 (eq (char-before (match-beginning 0)) ?\")
888 (eq (char-after (match-end 0)) ?\"))
889 ;; remove flyspell overlays
890 (when (fboundp 'flyspell-unhighlight-at)
891 (let ((cur (match-beginning 0)))
892 (while (> (match-end 0) cur)
893 (flyspell-unhighlight-at cur)
894 (setq cur (1+ cur)))))
896 (let ((link (muse-match-string-no-properties 1))
897 (face (muse-link-face (match-string 1))))
899 (add-text-properties (match-beginning 1) (match-end 0)
900 (muse-link-properties
901 (muse-match-string-no-properties 1) face))))))
903 (defun muse-colors-title ()
904 "Color #title directives."
905 (let ((beg (+ 7 (match-beginning 0))))
906 (add-text-properties beg (muse-line-end-position) '(muse-directive t))
907 ;; colorize <lisp> tags in #title after other <lisp> tags have had a
908 ;; chance to run, so that we can have behavior that is consistent
909 ;; with how the document is published
910 (setq muse-colors-delayed-commands
911 (cons (list 'muse-colors-title-lisp beg (muse-line-end-position))
912 muse-colors-delayed-commands))))
914 (defun muse-colors-title-lisp (beg end)
915 "Called after other highlighting is done for a region in order to handle
916 <lisp> tags that exist in #title directives."
918 (narrow-to-region beg end)
919 (goto-char (point-min))
920 (let ((muse-colors-inhibit-tags-in-directives nil)
921 (muse-colors-tags '(("lisp" t t nil muse-colors-lisp-tag))))
922 (while (re-search-forward muse-tag-regexp nil t)
923 (muse-colors-custom-tags))))
924 (add-text-properties beg end '(face muse-header-1)))
926 (defun muse-colors-comment ()
928 (add-text-properties (match-beginning 0) (muse-line-end-position)
929 (list 'face 'font-lock-comment-face
933 (provide 'muse-colors)
935 ;;; muse-colors.el ends here