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