publish: Provide php tag
[muse-el.git] / lisp / muse-colors.el
blob02bbf8ecdcbf88a5bb8c2d852f403833cbcf321d
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.
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)
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)
111 ;;;###autoload
112 (defun muse-colors-toggle-inline-images ()
113 "Toggle display of inlined images on/off."
114 (interactive)
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)
121 ;; display informative message
122 (if muse-colors-inline-images
123 (message "Images are now displayed inline")
124 (message "Images are now displayed as links")))
126 (defvar muse-colors-outline-faces-list
127 (if (facep 'outline-1)
128 '(outline-1 outline-2 outline-3 outline-4 outline-5)
129 ;; these are equivalent in coloring to the outline faces
130 '(font-lock-function-name-face
131 font-lock-variable-name-face
132 font-lock-keyword-face
133 font-lock-builtin-face
134 font-lock-comment-face))
135 "Outline faces to use when assigning Muse header faces.")
137 (defun muse-make-faces-default (&optional later)
138 "Generate the default face definitions for headers."
139 (dolist (num '(1 2 3 4 5))
140 (let ((newsym (intern (concat "muse-header-" (int-to-string num)))))
141 ;; put in the proper group and give documentation
142 (if later
143 (unless (featurep 'xemacs)
144 (muse-copy-face 'variable-pitch newsym)
145 (set-face-attribute newsym nil :height (1+ (* 0.1 (- 5 num)))
146 :weight 'bold))
147 (if (featurep 'xemacs)
148 (eval `(defface ,newsym
149 '((t (:size
150 ,(nth (1- num) '("24pt" "18pt" "14pt" "12pt" "11pt"))
151 :bold t)))
152 "Muse header face"
153 :group 'muse-colors))
154 (eval `(defface ,newsym
155 '((t (:height ,(1+ (* 0.1 (- 5 num)))
156 :inherit variable-pitch
157 :weight bold)))
158 "Muse header face"
159 :group 'muse-colors)))))))
161 (progn (muse-make-faces-default))
163 (defun muse-make-faces (&optional frame)
164 "Generate face definitions for headers based the user's preferences."
165 (cond
166 ((not muse-colors-autogen-headings)
167 nil)
168 ((eq muse-colors-autogen-headings t)
169 (muse-make-faces-default t))
171 (dolist (num '(1 2 3 4 5))
172 (let ((newsym (intern (concat "muse-header-" (int-to-string num)))))
173 ;; copy the desired face definition
174 (muse-copy-face (nth (1- num) muse-colors-outline-faces-list)
175 newsym))))))
177 ;; after displaying the Emacs splash screen, the faces are wiped out,
178 ;; so recover from that
179 (add-hook 'window-setup-hook #'muse-make-faces)
180 ;; ditto for when a new frame is created
181 (when (boundp 'after-make-frame-functions)
182 (add-hook 'after-make-frame-functions #'muse-make-faces))
184 (defface muse-link
185 '((((class color) (background light))
186 (:foreground "blue" :underline "blue" :bold t))
187 (((class color) (background dark))
188 (:foreground "cyan" :underline "cyan" :bold t))
189 (t (:bold t)))
190 "Face for Muse cross-references."
191 :group 'muse-colors)
193 (defface muse-bad-link
194 '((((class color) (background light))
195 (:foreground "red" :underline "red" :bold t))
196 (((class color) (background dark))
197 (:foreground "coral" :underline "coral" :bold t))
198 (t (:bold t)))
199 "Face for bad Muse cross-references."
200 :group 'muse-colors)
202 (defface muse-verbatim
203 '((((class color) (background light))
204 (:foreground "slate gray"))
205 (((class color) (background dark))
206 (:foreground "gray")))
207 "Face for verbatim text."
208 :group 'muse-colors)
210 (defface muse-emphasis-1
211 '((t (:italic t)))
212 "Face for italic emphasized text."
213 :group 'muse-colors)
215 (defface muse-emphasis-2
216 '((t (:bold t)))
217 "Face for bold emphasized text."
218 :group 'muse-colors)
220 (defface muse-emphasis-3
221 '((t (:bold t :italic t)))
222 "Face for bold italic emphasized text."
223 :group 'muse-colors)
225 (muse-copy-face 'italic 'muse-emphasis-1)
226 (muse-copy-face 'bold 'muse-emphasis-2)
227 (muse-copy-face 'bold-italic 'muse-emphasis-3)
229 (defcustom muse-colors-buffer-hook nil
230 "A hook run after a region is highlighted.
231 Each function receives three arguments: BEG END VERBOSE.
232 BEG and END mark the range being highlighted, and VERBOSE specifies
233 whether progress messages should be displayed to the user."
234 :type 'hook
235 :group 'muse-colors)
237 (defvar muse-colors-regexp nil
238 "Regexp matching each car of `muse-colors-markup'.")
239 (defvar muse-colors-vector nil
240 "Vector of all characters that are part of Muse markup.
241 This is composed of the 2nd element of each `muse-colors-markup'
242 entry.")
244 (defun muse-configure-highlighting (sym val)
245 "Extract color markup information from VAL and set to SYM.
246 This is usually called with `muse-colors-markup' as both arguments."
247 (let ((regexps nil)
248 (rules nil))
249 (dolist (rule val)
250 (let ((value (cond ((symbolp (car rule))
251 (symbol-value (car rule)))
252 ((stringp (car rule))
253 (car rule))
254 (t nil))))
255 (when value
256 (setq rules (cons rule rules))
257 (setq regexps (cons value regexps)))))
258 (setq rules (nreverse rules)
259 regexps (nreverse regexps))
260 (setq muse-colors-regexp (concat "\\("
261 (mapconcat #'identity regexps "\\|")
262 "\\)")
263 muse-colors-vector (make-vector 128 nil))
264 (while rules
265 (if (eq (cadr (car rules)) t)
266 (let ((i 0) (l 128))
267 (while (< i l)
268 (unless (aref muse-colors-vector i)
269 (aset muse-colors-vector i (nth 2 (car rules))))
270 (setq i (1+ i))))
271 (aset muse-colors-vector (cadr (car rules))
272 (nth 2 (car rules))))
273 (setq rules (cdr rules))))
274 (set sym val))
276 (defun muse-colors-emphasized ()
277 "Color emphasized text and headings."
278 ;; Here we need to check four different points - the start and end
279 ;; of the leading *s, and the start and end of the trailing *s. We
280 ;; allow the outsides to be surrounded by whitespace or punctuation,
281 ;; but no word characters, and the insides must not be surrounded by
282 ;; whitespace or punctuation. Thus the following are valid:
284 ;; " *foo bar* "
285 ;; "**foo**,"
286 ;; and the following is invalid:
287 ;; "** testing **"
288 (let* ((beg (match-beginning 0))
289 (e1 (match-end 0))
290 (leader (- e1 beg))
291 b2 e2 multiline)
292 (unless (or (eq (get-text-property beg 'invisible) 'muse)
293 (get-text-property beg 'muse-comment)
294 (get-text-property beg 'muse-directive))
295 ;; check if it's a header
296 (if (eq (char-after e1) ?\ )
297 (when (or (= beg (point-min))
298 (eq (char-before beg) ?\n))
299 (add-text-properties
300 (muse-line-beginning-position) (muse-line-end-position)
301 (list 'face (intern (concat "muse-header-"
302 (int-to-string leader))))))
303 ;; beginning of line or space or symbol
304 (when (or (= beg (point-min))
305 (eq (char-syntax (char-before beg)) ?\ )
306 (memq (char-before beg)
307 '(?\- ?\[ ?\< ?\( ?\' ?\` ?\" ?\n)))
308 (save-excursion
309 (skip-chars-forward "^*<>\n" muse-colors-region-end)
310 (when (eq (char-after) ?\n)
311 (setq multiline t)
312 (skip-chars-forward "^*<>" muse-colors-region-end))
313 (setq b2 (point))
314 (skip-chars-forward "*" muse-colors-region-end)
315 (setq e2 (point))
316 ;; Abort if space exists just before end
317 ;; or bad leader
318 ;; or no '*' at end
319 ;; or word constituent follows
320 (unless (or (> leader 5)
321 (not (eq leader (- e2 b2)))
322 (eq (char-syntax (char-before b2)) ?\ )
323 (not (eq (char-after b2) ?*))
324 (and (not (eobp))
325 (eq (char-syntax (char-after (1+ b2))) ?w)))
326 (add-text-properties beg e1 '(invisible muse))
327 (add-text-properties
328 e1 b2 (list 'face (cond ((= leader 1) 'muse-emphasis-1)
329 ((= leader 2) 'muse-emphasis-2)
330 ((= leader 3) 'muse-emphasis-3))))
331 (add-text-properties b2 e2 '(invisible muse))
332 (when multiline
333 (add-text-properties
334 beg e2 '(font-lock-multiline t))))))))))
336 (defun muse-colors-underlined ()
337 "Color underlined text."
338 (let ((start (match-beginning 0))
339 multiline)
340 (unless (or (eq (get-text-property start 'invisible) 'muse)
341 (get-text-property start 'muse-comment)
342 (get-text-property start 'muse-directive))
343 ;; beginning of line or space or symbol
344 (when (or (= start (point-min))
345 (eq (char-syntax (char-before start)) ?\ )
346 (memq (char-before start)
347 '(?\- ?\[ ?\< ?\( ?\' ?\` ?\" ?\n)))
348 (save-excursion
349 (skip-chars-forward "^_<>\n" muse-colors-region-end)
350 (when (eq (char-after) ?\n)
351 (setq multiline t)
352 (skip-chars-forward "^_<>" muse-colors-region-end))
353 ;; Abort if space exists just before end
354 ;; or no '_' at end
355 ;; or word constituent follows
356 (unless (or (eq (char-syntax (char-before (point))) ?\ )
357 (not (eq (char-after (point)) ?_))
358 (and (not (eobp))
359 (eq (char-syntax (char-after (1+ (point)))) ?w)))
360 (add-text-properties start (1+ start) '(invisible muse))
361 (add-text-properties (1+ start) (point) '(face underline))
362 (add-text-properties (point)
363 (min (1+ (point)) (point-max))
364 '(invisible muse))
365 (when multiline
366 (add-text-properties
367 start (min (1+ (point)) (point-max))
368 '(font-lock-multiline t)))))))))
370 (defun muse-colors-verbatim ()
371 "Render in teletype and suppress further parsing."
372 (let ((start (match-beginning 0))
373 multiline)
374 (unless (or (eq (get-text-property start 'invisible) 'muse)
375 (get-text-property start 'muse-comment)
376 (get-text-property start 'muse-directive))
377 ;; beginning of line or space or symbol
378 (when (or (= start (point-min))
379 (eq (char-syntax (char-before start)) ?\ )
380 (memq (char-before start)
381 '(?\- ?\[ ?\< ?\( ?\' ?\` ?\" ?\n)))
382 (let ((pos (point)))
383 (skip-chars-forward "^=\n" muse-colors-region-end)
384 (when (eq (char-after) ?\n)
385 (setq multiline t)
386 (skip-chars-forward "^=" muse-colors-region-end))
387 ;; Abort if space exists just before end
388 ;; or no '=' at end
389 ;; or word constituent follows
390 (unless (or (eq (char-syntax (char-before (point))) ?\ )
391 (not (eq (char-after (point)) ?=))
392 (and (not (eobp))
393 (eq (char-syntax (char-after (1+ (point)))) ?w)))
394 (setq pos (min (1+ (point)) (point-max)))
395 (add-text-properties start (1+ start) '(invisible muse))
396 (add-text-properties (1+ start) (point) '(face muse-verbatim))
397 (add-text-properties (point)
398 (min (1+ (point)) (point-max))
399 '(invisible muse))
400 (when multiline
401 (add-text-properties
402 start (min (1+ (point)) (point-max))
403 '(font-lock-multiline t))))
404 (goto-char pos))))))
406 (defcustom muse-colors-markup
407 `(;; make emphasized text appear emphasized
408 ("\\*\\{1,5\\}" ?* muse-colors-emphasized)
410 ;; make underlined text appear underlined
411 (,(concat "_[^" muse-regexp-blank "_\n]")
412 ?_ muse-colors-underlined)
414 ("^#title " ?\# muse-colors-title)
416 (muse-explicit-link-regexp ?\[ muse-colors-explicit-link)
418 ;; render in teletype and suppress further parsing
419 (,(concat "=[^" muse-regexp-blank "=\n]") ?= muse-colors-verbatim)
421 ;; highlight any markup tags encountered
422 (muse-tag-regexp ?\< muse-colors-custom-tags)
424 ;; display comments
425 (,(concat "^;[" muse-regexp-blank "]") ?\; muse-colors-comment)
427 ;; this has to come later since it doesn't have a special
428 ;; character in the second cell
429 (muse-url-regexp t muse-colors-implicit-link)
431 "Expressions to highlight an Emacs Muse buffer.
432 These are arranged in a rather special fashion, so as to be as quick as
433 possible.
435 Each element of the list is itself a list, of the form:
437 (LOCATE-REGEXP TEST-CHAR MATCH-FUNCTION)
439 LOCATE-REGEXP is a partial regexp, and should be the smallest possible
440 regexp to differentiate this rule from other rules. It may also be a
441 symbol containing such a regexp. The buffer region is scanned only
442 once, and LOCATE-REGEXP indicates where the scanner should stop to
443 look for highlighting possibilities.
445 TEST-CHAR is a char or t. The character should match the beginning
446 text matched by LOCATE-REGEXP. These chars are used to build a vector
447 for fast MATCH-FUNCTION calling.
449 MATCH-FUNCTION is the function called when a region has been
450 identified. It is responsible for adding the appropriate text
451 properties to change the appearance of the buffer.
453 This markup is used to modify the appearance of the original text to
454 make it look more like the published HTML would look (like making some
455 markup text invisible, inlining images, etc).
457 font-lock is used to apply the markup rules, so that they can happen
458 on a deferred basis. They are not always accurate, but you can use
459 \\[font-lock-fontifty-block] near the point of error to force
460 fontification in that area."
461 :type '(repeat
462 (list :tag "Highlight rule"
463 (choice (regexp :tag "Locate regexp")
464 (symbol :tag "Regexp symbol"))
465 (choice (character :tag "Confirm character")
466 (const :tag "Default rule" t))
467 function))
468 :set 'muse-configure-highlighting
469 :group 'muse-colors)
471 ;; XEmacs users don't have `font-lock-multiline'.
472 (unless (boundp 'font-lock-multiline)
473 (defvar font-lock-multiline nil))
475 (defun muse-use-font-lock ()
476 "Set up font-locking for Muse."
477 (muse-add-to-invisibility-spec 'muse)
478 (set (make-local-variable 'font-lock-multiline) 'undecided)
479 (set (make-local-variable 'font-lock-defaults)
480 `(nil t nil nil beginning-of-line
481 (font-lock-fontify-region-function . muse-colors-region)
482 (font-lock-unfontify-region-function
483 . muse-unhighlight-region)))
484 (set (make-local-variable 'font-lock-fontify-region-function)
485 'muse-colors-region)
486 (set (make-local-variable 'font-lock-unfontify-region-function)
487 'muse-unhighlight-region)
488 (muse-make-faces)
489 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup)
490 (font-lock-mode t))
492 (defun muse-colors-buffer ()
493 "Re-highlight the entire Muse buffer."
494 (interactive)
495 (muse-colors-region (point-min) (point-max) t))
497 (defvar muse-colors-fontifying-p nil
498 "Indicate whether Muse is fontifying the current buffer.")
499 (make-variable-buffer-local 'muse-colors-fontifying-p)
501 (defvar muse-colors-delayed-commands nil
502 "Commands to be run immediately after highlighting a region.
504 This is meant to accommodate highlighting <lisp> in #title
505 directives after everything else.
507 It may be modified by Muse functions during highlighting, but not
508 the user.")
509 (make-variable-buffer-local 'muse-colors-delayed-commands)
511 (defun muse-colors-region (beg end &optional verbose)
512 "Apply highlighting according to `muse-colors-markup'.
513 Note that this function should NOT change the buffer, nor should any
514 of the functions listed in `muse-colors-markup'."
515 (let ((buffer-undo-list t)
516 (inhibit-read-only t)
517 (inhibit-point-motion-hooks t)
518 (inhibit-modification-hooks t)
519 (modified-p (buffer-modified-p))
520 (muse-colors-fontifying-p t)
521 (muse-colors-region-end (muse-line-end-position end))
522 (muse-colors-delayed-commands nil)
523 deactivate-mark)
524 (unwind-protect
525 (save-excursion
526 (save-restriction
527 (widen)
528 ;; check to see if we should expand the beg/end area for
529 ;; proper multiline matches
530 (when (and font-lock-multiline
531 (> beg (point-min))
532 (get-text-property (1- beg) 'font-lock-multiline))
533 ;; We are just after or in a multiline match.
534 (setq beg (or (previous-single-property-change
535 beg 'font-lock-multiline)
536 (point-min)))
537 (goto-char beg)
538 (setq beg (muse-line-beginning-position)))
539 (when font-lock-multiline
540 (setq end (or (text-property-any end (point-max)
541 'font-lock-multiline nil)
542 (point-max))))
543 (goto-char end)
544 (setq end (muse-line-beginning-position 2))
545 ;; Undo any fontification in the area.
546 (font-lock-unfontify-region beg end)
547 ;; And apply fontification based on `muse-colors-markup'
548 (let ((len (float (- end beg)))
549 (case-fold-search nil)
550 markup-func)
551 (goto-char beg)
552 (while (and (< (point) end)
553 (re-search-forward muse-colors-regexp end t))
554 (if verbose
555 (message "Highlighting buffer...%d%%"
556 (* (/ (float (- (point) beg)) len) 100)))
557 (setq markup-func
558 (aref muse-colors-vector
559 (char-after (match-beginning 0))))
560 (when markup-func (funcall markup-func)))
561 (dolist (command muse-colors-delayed-commands)
562 (apply (car command) (cdr command)))
563 (run-hook-with-args 'muse-colors-buffer-hook
564 beg end verbose)
565 (if verbose (message "Highlighting buffer...done")))))
566 (set-buffer-modified-p modified-p))))
568 (defcustom muse-colors-tags
569 '(("example" t nil nil muse-colors-example-tag)
570 ("code" t nil nil muse-colors-example-tag)
571 ("verbatim" t nil nil muse-colors-literal-tag)
572 ("lisp" t t nil muse-colors-lisp-tag)
573 ("literal" t nil nil muse-colors-literal-tag))
574 "A list of tag specifications for specially highlighting text.
575 XML-style tags are the best way to add custom highlighting to Muse.
576 This is easily accomplished by customizing this list of markup tags.
578 For each entry, the name of the tag is given, whether it expects
579 a closing tag and/or an optional set of attributes, whether it is
580 nestable, and a function that performs whatever action is desired
581 within the delimited region.
583 The function is called with three arguments, the beginning and
584 end of the region surrounded by the tags. If properties are
585 allowed, they are passed as a third argument in the form of an
586 alist. The `end' argument to the function is the last character
587 of the enclosed tag or region.
589 Functions should not modify the contents of the buffer."
590 :type '(repeat (list (string :tag "Markup tag")
591 (boolean :tag "Expect closing tag" :value t)
592 (boolean :tag "Parse attributes" :value nil)
593 (boolean :tag "Nestable" :value nil)
594 function))
595 :group 'muse-colors)
597 (defvar muse-colors-inhibit-tags-in-directives t
598 "If non-nil, don't allow tags to be interpreted in directives.
599 This is used to delay highlighting of <lisp> tags in #title until later.")
600 (make-variable-buffer-local 'muse-colors-inhibit-tags-in-directives)
602 (defsubst muse-colors-tag-info (tagname &rest args)
603 "Get tag info associated with TAGNAME, ignoring ARGS."
604 (assoc tagname muse-colors-tags))
606 (defun muse-colors-custom-tags ()
607 "Highlight `muse-colors-tags'."
608 (save-excursion
609 (goto-char (match-beginning 0))
610 (looking-at muse-tag-regexp))
611 (let ((tag-info (muse-colors-tag-info (match-string 1))))
612 (unless (or (not tag-info)
613 (get-text-property (match-beginning 0) 'muse-comment)
614 (and muse-colors-inhibit-tags-in-directives
615 (get-text-property (match-beginning 0) 'muse-directive)))
616 (let ((closed-tag (match-string 3))
617 (start (match-beginning 0))
618 end attrs)
619 (when (nth 2 tag-info)
620 (let ((attrstr (match-string 2)))
621 (while (and attrstr
622 (string-match (concat "\\([^"
623 muse-regexp-blank
624 "=\n]+\\)\\(=\""
625 "\\([^\"]+\\)\"\\)?")
626 attrstr))
627 (let ((attr (cons (downcase
628 (muse-match-string-no-properties 1 attrstr))
629 (muse-match-string-no-properties 3 attrstr))))
630 (setq attrstr (replace-match "" t t attrstr))
631 (if attrs
632 (nconc attrs (list attr))
633 (setq attrs (list attr)))))))
634 (if (and (cadr tag-info) (not closed-tag))
635 (if (muse-goto-tag-end (car tag-info) (nth 3 tag-info))
636 (setq end (match-end 0))
637 (setq tag-info nil)))
638 (when tag-info
639 (let ((args (list start end)))
640 (if (nth 2 tag-info)
641 (nconc args (list attrs)))
642 (apply (nth 4 tag-info) args)))))))
644 (defun muse-unhighlight-region (begin end &optional verbose)
645 "Remove all visual highlights in the buffer (except font-lock)."
646 (let ((buffer-undo-list t)
647 (inhibit-read-only t)
648 (inhibit-point-motion-hooks t)
649 (inhibit-modification-hooks t)
650 (modified-p (buffer-modified-p))
651 deactivate-mark)
652 (unwind-protect
653 (remove-text-properties
654 begin end '(face nil font-lock-multiline nil end-glyph nil
655 invisible nil intangible nil display nil
656 mouse-face nil keymap nil help-echo nil
657 muse-link nil muse-directive nil muse-comment nil))
658 (set-buffer-modified-p modified-p))))
660 (defun muse-colors-example-tag (beg end)
661 "Strip properties and colorize with `muse-verbatim'."
662 (muse-unhighlight-region beg end)
663 (let ((multi (save-excursion
664 (goto-char beg)
665 (forward-line 1)
666 (> end (point)))))
667 (add-text-properties beg end `(face muse-verbatim
668 font-lock-multiline ,multi))))
670 (defun muse-colors-literal-tag (beg end)
671 "Strip properties and mark as literal."
672 (muse-unhighlight-region beg end)
673 (let ((multi (save-excursion
674 (goto-char beg)
675 (forward-line 1)
676 (> end (point)))))
677 (add-text-properties beg end `(font-lock-multiline ,multi))))
679 (defun muse-colors-lisp-tag (beg end attrs)
680 "Color the region enclosed by a <lisp> tag."
681 (if (not muse-colors-evaluate-lisp-tags)
682 (muse-colors-literal-tag beg end)
683 (muse-unhighlight-region beg end)
684 (let (beg-lisp end-lisp)
685 (save-match-data
686 (goto-char beg)
687 (setq beg-lisp (and (looking-at "<[^>]+>")
688 (match-end 0)))
689 (goto-char end)
690 (setq end-lisp (and (muse-looking-back "</[^>]+>")
691 (match-beginning 0))))
692 (add-text-properties
693 beg end
694 (list 'font-lock-multiline t
695 'display (muse-eval-lisp
696 (concat
697 "(progn "
698 (buffer-substring-no-properties beg-lisp end-lisp)
699 ")"))
700 'intangible t)))))
702 (defvar muse-mode-local-map
703 (let ((map (make-sparse-keymap)))
704 (define-key map [return] 'muse-follow-name-at-point)
705 (define-key map [(control ?m)] 'muse-follow-name-at-point)
706 (define-key map [(shift return)] 'muse-follow-name-at-point-other-window)
707 (if (featurep 'xemacs)
708 (progn
709 (define-key map [(button2)] 'muse-follow-name-at-mouse)
710 (define-key map [(shift button2)]
711 'muse-follow-name-at-mouse-other-window))
712 (define-key map [(shift control ?m)]
713 'muse-follow-name-at-point-other-window)
714 (define-key map [mouse-2] 'muse-follow-name-at-mouse)
715 (define-key map [(shift mouse-2)]
716 'muse-follow-name-at-mouse-other-window)
717 (unless (eq emacs-major-version 21)
718 (set-keymap-parent map muse-mode-map)))
719 map)
720 "Local keymap used by Muse while on a link.")
722 (defvar muse-keymap-property
723 (if (or (featurep 'xemacs)
724 (>= emacs-major-version 21))
725 'keymap
726 'local-map)
727 "The name of the keymap or local-map property.")
729 (defsubst muse-link-properties (help-str &optional face)
730 "Determine text properties to use for a link."
731 (append (if face
732 (list 'face face 'mouse-face 'highlight 'muse-link t)
733 (list 'invisible 'muse 'intangible t))
734 (list 'help-echo help-str 'rear-nonsticky t
735 muse-keymap-property muse-mode-local-map)))
737 (defun muse-link-face (link-name &optional explicit)
738 "Return the type of LINK-NAME as a face symbol.
739 For EXPLICIT links, this is either a normal link or a bad-link
740 face. For implicit links, it is either colored normally or
741 ignored."
742 (save-match-data
743 (let ((link (if explicit
744 (muse-handle-explicit-link link-name)
745 (muse-handle-implicit-link link-name))))
746 (when link
747 (cond ((string-match muse-url-regexp link)
748 'muse-link)
749 ((muse-file-remote-p link)
750 'muse-link)
751 ((string-match muse-file-regexp link)
752 (when (string-match "/[^/]+#[^#./]+\\'" link)
753 ;; strip anchor from the end of a path
754 (setq link (substring link 0 (match-beginning 0))))
755 (if (file-exists-p link)
756 'muse-link
757 'muse-bad-link))
758 ((not (featurep 'muse-project))
759 'muse-link)
761 (if (string-match "#" link)
762 (setq link (substring link 0 (match-beginning 0))))
763 (if (or (and (muse-project-of-file)
764 (muse-project-page-file
765 link muse-current-project t))
766 (file-exists-p link))
767 'muse-link
768 'muse-bad-link)))))))
770 (defun muse-colors-use-publishing-directory (link)
771 "Make LINK relative to the directory where we will publish the
772 current file."
773 (let ((style (car (muse-project-applicable-styles
774 link (cddr (muse-project)))))
775 path)
776 (when (and style
777 (setq path (muse-style-element :path style)))
778 (expand-file-name link path))))
780 (defun muse-colors-resolve-image-file (link)
781 "Determine if we can create images and see if the link is an image
782 file."
783 (save-match-data
784 (and (or (fboundp 'create-image)
785 (fboundp 'make-glyph))
786 (not (string-match "\\`[uU][rR][lL]:" link))
787 (string-match muse-image-regexp link))))
789 (defun muse-make-file-glyph (filename)
790 "Given a file name, return a newly-created image glyph.
791 This is a hack for supporting inline images in XEmacs."
792 (let ((case-fold-search nil))
793 ;; Scan filename to determine image type
794 (when (fboundp 'make-glyph)
795 (save-match-data
796 (cond ((string-match "jpe?g" filename)
797 (make-glyph (vector 'jpeg :file filename) 'buffer))
798 ((string-match "gif" filename)
799 (make-glyph (vector 'gif :file filename) 'buffer))
800 ((string-match "png" filename)
801 (make-glyph (vector 'png :file filename) 'buffer)))))))
803 (defun muse-colors-insert-image (link beg end invis-props)
804 "Create an image using create-image or make-glyph and insert it
805 in place of an image link defined by BEG and END."
806 (setq link (expand-file-name link))
807 (let ((image-file (cond
808 ((eq muse-colors-inline-image-method 'default-directory)
809 link)
810 ((functionp muse-colors-inline-image-method)
811 (funcall muse-colors-inline-image-method link))))
812 glyph)
813 (when (stringp image-file)
814 (if (fboundp 'create-image)
815 ;; use create-image and display property
816 (let ((display-stuff (condition-case nil
817 (create-image image-file)
818 (error nil))))
819 (when display-stuff
820 (add-text-properties beg end (list 'display display-stuff))))
821 ;; use make-glyph and invisible property
822 (and (setq glyph (muse-make-file-glyph image-file))
823 (progn
824 (add-text-properties beg end invis-props)
825 (add-text-properties beg end (list
826 'end-glyph glyph
827 'help-echo link))))))))
829 (defun muse-colors-explicit-link ()
830 "Color explicit links."
831 (when (and (eq ?\[ (char-after (match-beginning 0)))
832 (not (get-text-property (match-beginning 0) 'muse-comment))
833 (not (get-text-property (match-beginning 0) 'muse-directive)))
834 ;; remove flyspell overlays
835 (when (fboundp 'flyspell-unhighlight-at)
836 (let ((cur (match-beginning 0)))
837 (while (> (match-end 0) cur)
838 (flyspell-unhighlight-at cur)
839 (setq cur (1+ cur)))))
840 (save-excursion
841 (goto-char (match-beginning 0))
842 (looking-at muse-explicit-link-regexp))
843 (let* ((unesc-link (muse-get-link))
844 (unesc-desc (muse-get-link-desc))
845 (link (muse-link-unescape unesc-link))
846 (desc (muse-link-unescape unesc-desc))
847 (props (muse-link-properties desc (muse-link-face link t)))
848 (invis-props (append props (muse-link-properties desc))))
849 ;; see if we should try and inline an image
850 (if (and muse-colors-inline-images
851 (or (muse-colors-resolve-image-file link)
852 (and desc
853 (muse-colors-resolve-image-file desc)
854 (setq link desc))))
855 ;; we found an image, so inline it
856 (muse-colors-insert-image
857 link
858 (match-beginning 0) (match-end 0) invis-props)
859 (if desc
860 (progn
861 ;; we put the normal face properties on the invisible
862 ;; portion too, since emacs sometimes will position
863 ;; the cursor on an intangible character
864 (add-text-properties (match-beginning 0)
865 (match-beginning 2) invis-props)
866 (add-text-properties (match-beginning 2) (match-end 2) props)
867 (add-text-properties (match-end 2) (match-end 0) invis-props)
868 ;; in case specials were escaped, cause the unescaped
869 ;; text to be displayed
870 (unless (string= desc unesc-desc)
871 (add-text-properties (match-beginning 2) (match-end 2)
872 (list 'display desc))))
873 (add-text-properties (match-beginning 0)
874 (match-beginning 1) invis-props)
875 (add-text-properties (match-beginning 1) (match-end 0) props)
876 (add-text-properties (match-end 1) (match-end 0) invis-props)
877 (unless (string= link unesc-link)
878 (add-text-properties (match-beginning 1) (match-end 1)
879 (list 'display link))))
880 (goto-char (match-end 0))
881 (add-text-properties
882 (match-beginning 0) (match-end 0)
883 (muse-link-properties (muse-match-string-no-properties 0)
884 (muse-link-face link t)))))))
886 (defun muse-colors-implicit-link ()
887 "Color implicit links."
888 (unless (or (eq (get-text-property (match-beginning 0) 'invisible) 'muse)
889 (get-text-property (match-beginning 0) 'muse-comment)
890 (get-text-property (match-beginning 0) 'muse-directive)
891 (eq (char-before (match-beginning 0)) ?\")
892 (eq (char-after (match-end 0)) ?\"))
893 ;; remove flyspell overlays
894 (when (fboundp 'flyspell-unhighlight-at)
895 (let ((cur (match-beginning 0)))
896 (while (> (match-end 0) cur)
897 (flyspell-unhighlight-at cur)
898 (setq cur (1+ cur)))))
899 ;; colorize link
900 (let ((link (muse-match-string-no-properties 1))
901 (face (muse-link-face (match-string 1))))
902 (when face
903 (add-text-properties (match-beginning 1) (match-end 0)
904 (muse-link-properties
905 (muse-match-string-no-properties 1) face))))))
907 (defun muse-colors-title ()
908 "Color #title directives."
909 (let ((beg (+ 7 (match-beginning 0))))
910 (add-text-properties beg (muse-line-end-position) '(muse-directive t))
911 ;; colorize <lisp> tags in #title after other <lisp> tags have had a
912 ;; chance to run, so that we can have behavior that is consistent
913 ;; with how the document is published
914 (setq muse-colors-delayed-commands
915 (cons (list 'muse-colors-title-lisp beg (muse-line-end-position))
916 muse-colors-delayed-commands))))
918 (defun muse-colors-title-lisp (beg end)
919 "Called after other highlighting is done for a region in order to handle
920 <lisp> tags that exist in #title directives."
921 (save-restriction
922 (narrow-to-region beg end)
923 (goto-char (point-min))
924 (let ((muse-colors-inhibit-tags-in-directives nil)
925 (muse-colors-tags '(("lisp" t t nil muse-colors-lisp-tag))))
926 (while (re-search-forward muse-tag-regexp nil t)
927 (muse-colors-custom-tags))))
928 (add-text-properties beg end '(face muse-header-1)))
930 (defun muse-colors-comment ()
931 "Color comments."
932 (add-text-properties (match-beginning 0) (muse-line-end-position)
933 (list 'face 'font-lock-comment-face
934 'muse-comment t)))
937 (provide 'muse-colors)
939 ;;; muse-colors.el ends here