Fix bug introduced in last patch.
[muse-el.git] / lisp / muse-colors.el
blobceeaa08800523324306aefa08523bc6a67439362
1 ;;; muse-colors.el --- coloring and highlighting used by Muse
3 ;; Copyright (C) 2004, 2005, 2006 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 2, 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.
26 ;;; Commentary:
28 ;;; Contributors:
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
32 ;; fix this.
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.
42 ;;; Code:
44 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
46 ;; Emacs Muse Highlighting
48 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
50 (require 'muse-mode)
51 (require 'muse-regexps)
52 (require 'font-lock)
54 (defgroup muse-colors nil
55 "Options controlling the behavior of Emacs Muse highlighting.
56 See `muse-colors-buffer' for more information."
57 :group 'muse-mode)
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
64 headings.
66 If you want to customize each of the headings individually, set
67 this to nil."
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))
71 :group 'muse-colors)
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
76 them.
78 The actual contents of the buffer are not changed, only the
79 displayed text."
80 :type 'boolean
81 :group 'muse-colors)
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."
90 :type 'boolean
91 :group 'muse-colors)
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"))
105 :group 'muse-colors)
108 ;;;###autoload
109 (defun muse-colors-toggle-inline-images ()
110 "Toggle inlined images on/off."
111 (interactive)
112 ;; toggle the custom setting
113 (if (not muse-colors-inline-images)
114 (setq muse-colors-inline-images t)
115 (setq muse-colors-inline-images nil))
116 ;; reprocess the buffer
117 (muse-colors-buffer))
119 (define-key muse-mode-map [(control ?c) (control ?i)]
120 'muse-colors-toggle-inline-images)
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 (dolist (num '(1 2 3 4 5))
135 (let ((newsym (intern (concat "muse-header-" (int-to-string num)))))
136 ;; put in the proper group and give documentation
137 (if later
138 (unless (featurep 'xemacs)
139 (muse-copy-face 'variable-pitch newsym)
140 (set-face-attribute newsym nil :height (1+ (* 0.1 (- 5 num)))
141 :weight 'bold))
142 (if (featurep 'xemacs)
143 (eval `(defface ,newsym
144 '((t (:size
145 ,(nth (1- num) '("24pt" "18pt" "14pt" "12pt" "11pt"))
146 :bold t)))
147 "Muse header face"
148 :group 'muse-colors))
149 (eval `(defface ,newsym
150 '((t (:height ,(1+ (* 0.1 (- 5 num)))
151 :inherit variable-pitch
152 :weight bold)))
153 "Muse header face"
154 :group 'muse-colors)))))))
156 (progn (muse-make-faces-default))
158 (defun muse-make-faces (&optional frame)
159 (cond
160 ((not muse-colors-autogen-headings)
161 nil)
162 ((eq muse-colors-autogen-headings t)
163 (muse-make-faces-default t))
165 (dolist (num '(1 2 3 4 5))
166 (let ((newsym (intern (concat "muse-header-" (int-to-string num)))))
167 ;; copy the desired face definition
168 (muse-copy-face (nth (1- num) muse-colors-outline-faces-list)
169 newsym))))))
171 ;; after displaying the Emacs splash screen, the faces are wiped out,
172 ;; so recover from that
173 (add-hook 'window-setup-hook #'muse-make-faces)
174 ;; ditto for when a new frame is created
175 (when (boundp 'after-make-frame-functions)
176 (add-hook 'after-make-frame-functions #'muse-make-faces))
178 (defface muse-link
179 '((((class color) (background light))
180 (:foreground "blue" :underline "blue" :bold t))
181 (((class color) (background dark))
182 (:foreground "cyan" :underline "cyan" :bold t))
183 (t (:bold t)))
184 "Face for Muse cross-references."
185 :group 'muse-colors)
187 (defface muse-bad-link
188 '((((class color) (background light))
189 (:foreground "red" :underline "red" :bold t))
190 (((class color) (background dark))
191 (:foreground "coral" :underline "coral" :bold t))
192 (t (:bold t)))
193 "Face for bad Muse cross-references."
194 :group 'muse-colors)
196 (defface muse-verbatim
197 '((((class color) (background light))
198 (:foreground "slate gray"))
199 (((class color) (background dark))
200 (:foreground "gray")))
201 "Face for verbatim text."
202 :group 'muse-colors)
204 (defface muse-emphasis-1
205 '((t (:italic t)))
206 "Face for italic emphasized text."
207 :group 'muse-colors)
209 (defface muse-emphasis-2
210 '((t (:bold t)))
211 "Face for bold emphasized text."
212 :group 'muse-colors)
214 (defface muse-emphasis-3
215 '((t (:bold t :italic t)))
216 "Face for bold italic emphasized text."
217 :group 'muse-colors)
219 (muse-copy-face 'italic 'muse-emphasis-1)
220 (muse-copy-face 'bold 'muse-emphasis-2)
221 (muse-copy-face 'bold-italic 'muse-emphasis-3)
223 (defcustom muse-colors-buffer-hook nil
224 "A hook run after a region is highlighted.
225 Each function receives three arguments: BEG END VERBOSE.
226 BEG and END mark the range being highlighted, and VERBOSE specifies
227 whether progress messages should be displayed to the user."
228 :type 'hook
229 :group 'muse-colors)
231 (defvar muse-colors-regexp nil)
232 (defvar muse-colors-vector nil)
234 (defun muse-configure-highlighting (sym val)
235 (setq muse-colors-regexp
236 (concat "\\(" (mapconcat (function
237 (lambda (rule)
238 (if (symbolp (car rule))
239 (symbol-value (car rule))
240 (car rule)))) val "\\|") "\\)")
241 muse-colors-vector (make-vector 128 nil))
242 (let ((rules val))
243 (while rules
244 (if (eq (cadr (car rules)) t)
245 (let ((i 0) (l 128))
246 (while (< i l)
247 (unless (aref muse-colors-vector i)
248 (aset muse-colors-vector i (nth 2 (car rules))))
249 (setq i (1+ i))))
250 (aset muse-colors-vector (cadr (car rules))
251 (nth 2 (car rules))))
252 (setq rules (cdr rules))))
253 (set sym val))
255 (eval-when-compile
256 (defvar end))
258 (defun muse-colors-emphasized ()
259 ;; Here we need to check four different points - the start and end
260 ;; of the leading *s, and the start and end of the trailing *s. We
261 ;; allow the outsides to be surrounded by whitespace or punctuation,
262 ;; but no word characters, and the insides must not be surrounded by
263 ;; whitespace or punctuation. Thus the following are valid:
265 ;; " *foo bar* "
266 ;; "**foo**,"
267 ;; and the following is invalid:
268 ;; "** testing **"
269 (let* ((beg (match-beginning 0))
270 (e1 (match-end 0))
271 (leader (- e1 beg))
272 b2 e2 multiline)
273 (unless (eq (get-text-property beg 'invisible) 'muse)
274 ;; check if it's a header
275 (if (eq (char-after e1) ?\ )
276 (when (or (= beg (point-min))
277 (eq (char-before beg) ?\n))
278 (add-text-properties
279 (muse-line-beginning-position) (muse-line-end-position)
280 (list 'face (intern (concat "muse-header-"
281 (int-to-string leader))))))
282 ;; beginning of line or space or symbol
283 (when (or (= beg (point-min))
284 (eq (char-syntax (char-before beg)) ?\ )
285 (memq (char-before beg)
286 '(?\- ?\[ ?\< ?\( ?\' ?\` ?\" ?\n)))
287 (save-excursion
288 (skip-chars-forward "^*<>\n" end)
289 (when (eq (char-after) ?\n)
290 (setq multiline t)
291 (skip-chars-forward "^*<>" end))
292 (setq b2 (point))
293 (skip-chars-forward "*" end)
294 (setq e2 (point))
295 ;; Abort if space exists just before end
296 ;; or bad leader
297 ;; or no '*' at end
298 ;; or word constituent follows
299 (unless (or (> leader 5)
300 (not (eq leader (- e2 b2)))
301 (eq (char-syntax (char-before b2)) ?\ )
302 (not (eq (char-after b2) ?*))
303 (and (not (eobp))
304 (eq (char-syntax (char-after (1+ b2))) ?w)))
305 (add-text-properties beg e1 '(invisible muse))
306 (add-text-properties
307 e1 b2 (list 'face (cond ((= leader 1) 'muse-emphasis-1)
308 ((= leader 2) 'muse-emphasis-2)
309 ((= leader 3) 'muse-emphasis-3))))
310 (add-text-properties b2 e2 '(invisible muse))
311 (when multiline
312 (add-text-properties
313 beg e2 '(font-lock-multiline t))))))))))
315 (defun muse-colors-underlined ()
316 (let ((start (match-beginning 0))
317 multiline)
318 (unless (eq (get-text-property start 'invisible) 'muse)
319 ;; beginning of line or space or symbol
320 (when (or (= start (point-min))
321 (eq (char-syntax (char-before start)) ?\ )
322 (memq (char-before start)
323 '(?\- ?\[ ?\< ?\( ?\' ?\` ?\" ?\n)))
324 (save-excursion
325 (skip-chars-forward "^_<>\n" end)
326 (when (eq (char-after) ?\n)
327 (setq multiline t)
328 (skip-chars-forward "^_<>" end))
329 ;; Abort if space exists just before end
330 ;; or no '_' at end
331 ;; or word constituent follows
332 (unless (or (eq (char-syntax (char-before (point))) ?\ )
333 (not (eq (char-after (point)) ?_))
334 (and (not (eobp))
335 (eq (char-syntax (char-after (1+ (point)))) ?w)))
336 (add-text-properties start (1+ start) '(invisible muse))
337 (add-text-properties (1+ start) (point) '(face underline))
338 (add-text-properties (point)
339 (min (1+ (point)) (point-max))
340 '(invisible muse))
341 (when multiline
342 (add-text-properties
343 start (min (1+ (point)) (point-max))
344 '(font-lock-multiline t)))))))))
346 (defun muse-colors-verbatim ()
347 (let ((start (match-beginning 0))
348 multiline)
349 (unless (eq (get-text-property start 'invisible) 'muse)
350 ;; beginning of line or space or symbol
351 (when (or (= start (point-min))
352 (eq (char-syntax (char-before start)) ?\ )
353 (memq (char-before start)
354 '(?\- ?\[ ?\< ?\( ?\' ?\` ?\" ?\n)))
355 (let ((pos (point)))
356 (skip-chars-forward "^=\n" end)
357 (when (eq (char-after) ?\n)
358 (setq multiline t)
359 (skip-chars-forward "^=" end))
360 ;; Abort if space exists just before end
361 ;; or no '=' at end
362 ;; or word constituent follows
363 (unless (or (eq (char-syntax (char-before (point))) ?\ )
364 (not (eq (char-after (point)) ?=))
365 (and (not (eobp))
366 (eq (char-syntax (char-after (1+ (point)))) ?w)))
367 (setq pos (min (1+ (point)) (point-max)))
368 (add-text-properties start (1+ start) '(invisible muse))
369 (add-text-properties (1+ start) (point) '(face muse-verbatim))
370 (add-text-properties (point)
371 (min (1+ (point)) (point-max))
372 '(invisible muse))
373 (when multiline
374 (add-text-properties
375 start (min (1+ (point)) (point-max))
376 '(font-lock-multiline t))))
377 (goto-char pos))))))
379 (defcustom muse-colors-markup
380 `(;; make emphasized text appear emphasized
381 ("\\*\\{1,5\\}" ?* muse-colors-emphasized)
383 ;; make underlined text appear underlined
384 (,(concat "_[^" muse-regexp-blank "_\n]")
385 ?_ muse-colors-underlined)
387 ("^#title " ?\# muse-colors-title)
389 (muse-explicit-link-regexp ?\[ muse-colors-explicit-link)
391 ;; render in teletype and suppress further parsing
392 (,(concat "=[^" muse-regexp-blank "=\n]") ?= muse-colors-verbatim)
394 ;; highlight any markup tags encountered
395 (muse-tag-regexp ?\< muse-colors-custom-tags)
397 ;; this has to come later since it doesn't have a special
398 ;; character in the second cell
399 (muse-url-regexp t muse-colors-implicit-link)
401 "Expressions to highlight an Emacs Muse buffer.
402 These are arranged in a rather special fashion, so as to be as quick as
403 possible.
405 Each element of the list is itself a list, of the form:
407 (LOCATE-REGEXP TEST-CHAR MATCH-FUNCTION)
409 LOCATE-REGEXP is a partial regexp, and should be the smallest possible
410 regexp to differentiate this rule from other rules. It may also be a
411 symbol containing such a regexp. The buffer region is scanned only
412 once, and LOCATE-REGEXP indicates where the scanner should stop to
413 look for highlighting possibilities.
415 TEST-CHAR is a char or t. The character should match the beginning
416 text matched by LOCATE-REGEXP. These chars are used to build a vector
417 for fast MATCH-FUNCTION calling.
419 MATCH-FUNCTION is the function called when a region has been
420 identified. It is responsible for adding the appropriate text
421 properties to change the appearance of the buffer.
423 This markup is used to modify the appearance of the original text to
424 make it look more like the published HTML would look (like making some
425 markup text invisible, inlining images, etc).
427 font-lock is used to apply the markup rules, so that they can happen
428 on a deferred basis. They are not always accurate, but you can use
429 \\[font-lock-fontifty-block] near the point of error to force
430 fontification in that area.
432 Lastly, none of the regexp should contain grouping elements that will
433 affect the match data results."
434 :type '(repeat
435 (list :tag "Highlight rule"
436 (choice (regexp :tag "Locate regexp")
437 (symbol :tag "Regexp symbol"))
438 (choice (character :tag "Confirm character")
439 (const :tag "Default rule" t))
440 function))
441 :set 'muse-configure-highlighting
442 :group 'muse-colors)
444 ;; XEmacs users don't have `font-lock-multiline'.
445 (unless (boundp 'font-lock-multiline)
446 (defvar font-lock-multiline nil))
448 (defun muse-use-font-lock ()
449 (muse-add-to-invisibility-spec 'muse)
450 (set (make-local-variable 'font-lock-multiline) 'undecided)
451 (set (make-local-variable 'font-lock-defaults)
452 `(nil t nil nil 'beginning-of-line
453 (font-lock-fontify-region-function . muse-colors-region)
454 (font-lock-unfontify-region-function
455 . muse-unhighlight-region)))
456 (set (make-local-variable 'font-lock-fontify-region-function)
457 'muse-colors-region)
458 (set (make-local-variable 'font-lock-unfontify-region-function)
459 'muse-unhighlight-region)
460 (muse-make-faces)
461 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup)
462 (font-lock-mode t))
464 (defun muse-colors-buffer ()
465 "Re-highlight the entire Muse buffer."
466 (interactive)
467 (muse-colors-region (point-min) (point-max) t))
469 (defvar muse-colors-fontifying-p nil
470 "Indicate whether Muse is fontifying the current buffer.")
471 (make-variable-buffer-local 'muse-colors-fontifying-p)
473 (defun muse-colors-region (beg end &optional verbose)
474 "Apply highlighting according to `muse-colors-markup'.
475 Note that this function should NOT change the buffer, nor should any
476 of the functions listed in `muse-colors-markup'."
477 (let ((buffer-undo-list t)
478 (inhibit-read-only t)
479 (inhibit-point-motion-hooks t)
480 (inhibit-modification-hooks t)
481 (modified-p (buffer-modified-p))
482 (muse-colors-fontifying-p t)
483 deactivate-mark)
484 (unwind-protect
485 (save-excursion
486 (save-restriction
487 (widen)
488 ;; check to see if we should expand the beg/end area for
489 ;; proper multiline matches
490 (when (and font-lock-multiline
491 (> beg (point-min))
492 (get-text-property (1- beg) 'font-lock-multiline))
493 ;; We are just after or in a multiline match.
494 (setq beg (or (previous-single-property-change
495 beg 'font-lock-multiline)
496 (point-min)))
497 (goto-char beg)
498 (setq beg (muse-line-beginning-position)))
499 (when font-lock-multiline
500 (setq end (or (text-property-any end (point-max)
501 'font-lock-multiline nil)
502 (point-max))))
503 (goto-char end)
504 (setq end (muse-line-beginning-position 2))
505 ;; Undo any fontification in the area.
506 (font-lock-unfontify-region beg end)
507 ;; And apply fontification based on `muse-colors-markup'
508 (let ((len (float (- end beg)))
509 (case-fold-search nil)
510 markup-func)
511 (goto-char beg)
512 (while (and (< (point) end)
513 (re-search-forward muse-colors-regexp end t))
514 (if verbose
515 (message "Highlighting buffer...%d%%"
516 (* (/ (float (- (point) beg)) len) 100)))
517 (setq markup-func
518 (aref muse-colors-vector
519 (char-after (match-beginning 0))))
520 (when markup-func (funcall markup-func)))
521 (run-hook-with-args 'muse-colors-buffer-hook
522 beg end verbose)
523 (if verbose (message "Highlighting buffer...done")))))
524 (set-buffer-modified-p modified-p))))
526 (defcustom muse-colors-tags
527 '(("example" t nil muse-colors-example-tag)
528 ("code" t nil muse-colors-example-tag)
529 ("verbatim" t nil muse-colors-literal-tag)
530 ("lisp" t t muse-colors-lisp-tag)
531 ("literal" t nil muse-colors-literal-tag))
532 "A list of tag specifications for specially highlighting text.
533 XML-style tags are the best way to add custom highlighting to Muse.
534 This is easily accomplished by customizing this list of markup tags.
536 For each entry, the name of the tag is given, whether it expects
537 a closing tag and/or an optional set of attributes, and a
538 function that performs whatever action is desired within the
539 delimited region.
541 The function is called with three arguments, the beginning and
542 end of the region surrounded by the tags. If properties are
543 allowed, they are passed as a third argument in the form of an
544 alist. The `end' argument to the function is the last character
545 of the enclosed tag or region.
547 Functions should not modify the contents of the buffer."
548 :type '(repeat (list (string :tag "Markup tag")
549 (boolean :tag "Expect closing tag" :value t)
550 (boolean :tag "Parse attributes" :value nil)
551 function))
552 :group 'muse-colors)
554 (defsubst muse-colors-tag-info (tagname &rest args)
555 (assoc tagname muse-colors-tags))
557 (defun muse-colors-custom-tags ()
558 "Highlight `muse-colors-tags'."
559 (let ((tag-info (muse-colors-tag-info (match-string 4))))
560 (when tag-info
561 (let ((closed-tag (match-string 3))
562 (start (match-beginning 0))
563 end attrs)
564 (when (nth 2 tag-info)
565 (let ((attrstr (match-string 2)))
566 (while (and attrstr
567 (string-match (concat "\\([^"
568 muse-regexp-blank
569 "=\n]+\\)\\(=\""
570 "\\([^\"]+\\)\"\\)?")
571 attrstr))
572 (let ((attr (cons (downcase
573 (muse-match-string-no-properties 1 attrstr))
574 (muse-match-string-no-properties 3 attrstr))))
575 (setq attrstr (replace-match "" t t attrstr))
576 (if attrs
577 (nconc attrs (list attr))
578 (setq attrs (list attr)))))))
579 (if (and (cadr tag-info) (not closed-tag))
580 (if (search-forward (concat "</" (car tag-info) ">") nil t)
581 (setq end (match-end 0))
582 (setq tag-info nil)))
583 (when tag-info
584 (let ((args (list start end)))
585 (if (nth 2 tag-info)
586 (nconc args (list attrs)))
587 (apply (nth 3 tag-info) args)))))))
589 (defun muse-unhighlight-region (begin end &optional verbose)
590 "Remove all visual highlights in the buffer (except font-lock)."
591 (let ((buffer-undo-list t)
592 (inhibit-read-only t)
593 (inhibit-point-motion-hooks t)
594 (inhibit-modification-hooks t)
595 (modified-p (buffer-modified-p))
596 deactivate-mark)
597 (unwind-protect
598 (remove-text-properties
599 begin end '(face nil font-lock-multiline nil end-glyph nil
600 invisible nil intangible nil display nil
601 mouse-face nil keymap nil help-echo nil
602 muse-link nil))
603 (set-buffer-modified-p modified-p))))
605 (defun muse-colors-example-tag (beg end)
606 "Strip properties and colorize with `muse-verbatim'."
607 (muse-unhighlight-region beg end)
608 (let ((multi (save-excursion
609 (goto-char beg)
610 (forward-line 1)
611 (> end (point)))))
612 (add-text-properties beg end `(face muse-verbatim
613 font-lock-multiline ,multi))))
615 (defun muse-colors-literal-tag (beg end)
616 "Strip properties and mark as literal."
617 (muse-unhighlight-region beg end)
618 (let ((multi (save-excursion
619 (goto-char beg)
620 (forward-line 1)
621 (> end (point)))))
622 (add-text-properties beg end `(font-lock-multiline ,multi))))
624 (defun muse-colors-lisp-tag (beg end attrs)
625 (if (not muse-colors-evaluate-lisp-tags)
626 (muse-colors-literal-tag beg end)
627 (muse-unhighlight-region beg end)
628 (let (beg-lisp end-lisp)
629 (save-match-data
630 (goto-char beg)
631 (setq beg-lisp (and (looking-at "<[^>]+>")
632 (match-end 0)))
633 (goto-char end)
634 (setq end-lisp (and (muse-looking-back "</[^>]+>")
635 (match-beginning 0))))
636 (add-text-properties
637 beg end
638 (list 'font-lock-multiline t
639 'display (muse-eval-lisp
640 (concat
641 "(progn "
642 (buffer-substring-no-properties beg-lisp end-lisp)
643 ")"))
644 'intangible t)))))
646 (defvar muse-mode-local-map
647 (let ((map (make-sparse-keymap)))
648 (define-key map [return] 'muse-follow-name-at-point)
649 (define-key map [(control ?m)] 'muse-follow-name-at-point)
650 (define-key map [(shift return)] 'muse-follow-name-at-point-other-window)
651 (if (featurep 'xemacs)
652 (progn
653 (define-key map [(button2)] 'muse-follow-name-at-mouse)
654 (define-key map [(shift button2)]
655 'muse-follow-name-at-mouse-other-window))
656 (define-key map [(shift control ?m)]
657 'muse-follow-name-at-point-other-window)
658 (define-key map [mouse-2] 'muse-follow-name-at-mouse)
659 (define-key map [(shift mouse-2)]
660 'muse-follow-name-at-mouse-other-window)
661 (unless (eq emacs-major-version 21)
662 (set-keymap-parent map muse-mode-map)))
663 map)
664 "Local keymap used by Muse while on a link.")
666 (defvar muse-keymap-property
667 (if (or (featurep 'xemacs)
668 (>= emacs-major-version 21))
669 'keymap
670 'local-map))
672 (defsubst muse-link-properties (help-str &optional face)
673 (append (if face
674 (list 'face face 'mouse-face 'highlight 'muse-link t)
675 (list 'invisible 'muse 'intangible t))
676 (list 'help-echo help-str 'rear-nonsticky t
677 muse-keymap-property muse-mode-local-map)))
679 (defun muse-link-face (link-name &optional explicit)
680 "Return the type of LINK-NAME as a face symbol.
681 For EXPLICIT links, this is either a normal link or a bad-link
682 face. For implicit links, it is either colored normally or
683 ignored."
684 (save-match-data
685 (let ((link (if explicit
686 (muse-handle-explicit-link link-name)
687 (muse-handle-implicit-link link-name))))
688 (when link
689 (cond ((string-match muse-url-regexp link)
690 'muse-link)
691 ((string-match muse-file-regexp link)
692 (if (file-exists-p link)
693 'muse-link
694 'muse-bad-link))
695 ((not (featurep 'muse-project))
696 'muse-link)
698 (if (string-match "#" link)
699 (setq link (substring link 0 (match-beginning 0))))
700 (if (or (and (muse-project-of-file)
701 (muse-project-page-file
702 link muse-current-project t))
703 (file-exists-p link))
704 'muse-link
705 'muse-bad-link)))))))
707 (defun muse-colors-use-publishing-directory (link)
708 "Make LINK relative to the directory where we will publish the
709 current file."
710 (let ((style (car (muse-project-applicable-styles
711 link (cddr (muse-project)))))
712 path)
713 (when (and style
714 (setq path (muse-style-element :path style)))
715 (expand-file-name link path))))
717 (defun muse-colors-resolve-image-file (link)
718 "Determine if we can create images and see if the link is an image
719 file."
720 (save-match-data
721 (and (or (fboundp 'create-image)
722 (fboundp 'make-glyph))
723 (string-match muse-image-regexp link))))
725 (defun muse-make-file-glyph (filename)
726 "Given a file name, return a newly-created image glyph.
727 This is a hack for supporting inline images in XEmacs."
728 (let ((case-fold-search nil))
729 ;; Scan filename to determine image type
730 (when (fboundp 'make-glyph)
731 (save-match-data
732 (cond ((string-match "jpe?g" filename)
733 (make-glyph (vector 'jpeg :file filename) 'buffer))
734 ((string-match "gif" filename)
735 (make-glyph (vector 'gif :file filename) 'buffer))
736 ((string-match "png" filename)
737 (make-glyph (vector 'png :file filename) 'buffer)))))))
739 (defun muse-colors-insert-image (link beg end invis-props)
740 "Create an image using create-image or make-glyph and insert it
741 in place of an image link defined by BEG and END."
742 (setq link (expand-file-name link))
743 (let ((image-file (cond
744 ((eq muse-colors-inline-image-method 'default-directory)
745 link)
746 ((functionp muse-colors-inline-image-method)
747 (funcall muse-colors-inline-image-method link))))
748 glyph)
749 (when (stringp image-file)
750 (if (fboundp 'create-image)
751 ;; use create-image and display property
752 (add-text-properties beg end
753 (list 'display (create-image image-file)))
754 ;; use make-glyph and invisible property
755 (and (setq glyph (muse-make-file-glyph image-file))
756 (progn
757 (add-text-properties beg end invis-props)
758 (add-text-properties beg end (list
759 'end-glyph glyph
760 'help-echo link))))))))
762 (defun muse-colors-explicit-link ()
763 "Color explicit links."
764 (when (eq ?\[ (char-after (match-beginning 0)))
765 ;; remove flyspell overlays
766 (when (fboundp 'flyspell-unhighlight-at)
767 (let ((cur (match-beginning 0)))
768 (while (> (match-end 0) cur)
769 (flyspell-unhighlight-at cur)
770 (setq cur (1+ cur)))))
771 (save-excursion
772 (goto-char (match-beginning 0))
773 (looking-at muse-explicit-link-regexp))
774 (let* ((unesc-link (muse-get-link))
775 (unesc-desc (muse-get-link-desc))
776 (link (muse-link-unescape unesc-link))
777 (desc (muse-link-unescape unesc-desc))
778 (props (muse-link-properties desc (muse-link-face link t)))
779 (invis-props (append props (muse-link-properties desc))))
780 ;; see if we should try and inline an image
781 (if (and muse-colors-inline-images
782 (or (muse-colors-resolve-image-file link)
783 (and desc
784 (muse-colors-resolve-image-file desc)
785 (setq link desc))))
786 ;; we found an image, so inline it
787 (muse-colors-insert-image
788 link
789 (match-beginning 0) (match-end 0) invis-props)
790 (if desc
791 (progn
792 ;; we put the normal face properties on the invisible
793 ;; portion too, since emacs sometimes will position
794 ;; the cursor on an intangible character
795 (add-text-properties (match-beginning 0)
796 (match-beginning 2) invis-props)
797 (add-text-properties (match-beginning 2) (match-end 2) props)
798 (add-text-properties (match-end 2) (match-end 0) invis-props)
799 ;; in case specials were escaped, cause the unescaped
800 ;; text to be displayed
801 (unless (string= desc unesc-desc)
802 (add-text-properties (match-beginning 2) (match-end 2)
803 (list 'display desc))))
804 (add-text-properties (match-beginning 0)
805 (match-beginning 1) invis-props)
806 (add-text-properties (match-beginning 1) (match-end 0) props)
807 (add-text-properties (match-end 1) (match-end 0) invis-props)
808 (unless (string= link unesc-link)
809 (add-text-properties (match-beginning 1) (match-end 1)
810 (list 'display link))))
811 (goto-char (match-end 0))
812 (add-text-properties
813 (match-beginning 0) (match-end 0)
814 (muse-link-properties (muse-match-string-no-properties 0)
815 (muse-link-face link t)))))))
817 (defun muse-colors-implicit-link ()
818 "Color implicit links."
819 ;; remove flyspell overlays
820 (when (fboundp 'flyspell-unhighlight-at)
821 (let ((cur (match-beginning 0)))
822 (while (> (match-end 0) cur)
823 (flyspell-unhighlight-at cur)
824 (setq cur (1+ cur)))))
825 (unless (or (eq (get-text-property (match-beginning 0) 'invisible) 'muse)
826 (eq (char-before (match-beginning 0)) ?\")
827 (eq (char-after (match-end 0)) ?\"))
828 (let ((link (muse-match-string-no-properties 1))
829 (face (muse-link-face (match-string 1))))
830 (when face
831 (add-text-properties (match-beginning 1) (match-end 0)
832 (muse-link-properties
833 (muse-match-string-no-properties 1) face))))))
835 (defun muse-colors-title ()
836 (add-text-properties (+ 7 (match-beginning 0))
837 (muse-line-end-position)
838 '(face muse-header-1)))
840 (provide 'muse-colors)
842 ;;; muse-colors.el ends here