Misc. blosxom fixes, like category directive.
[muse-el.git] / lisp / muse-colors.el
blobac248a0c6861405639de627fb8c20e443cca589f
1 ;;; muse-colors.el --- Coloring and highlighting used by Muse
3 ;; Copyright (C) 2004, 2005 Free Software Foundation, Inc.
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: muse-colors.el
7 ;; Date: Thu 11-Mar-2004
8 ;; Keywords: hypermedia
9 ;; Author: John Wiegley (johnw AT gnu DOT org)
10 ;; Maintainer: Michael Olson (mwolson AT gnu DOT org)
11 ;; URL: http://www.mwolson.org/projects/MuseMode.html
12 ;; Compatibility: Emacs21
14 ;; This file is not part of GNU Emacs.
16 ;; This is free software; you can redistribute it and/or modify it under
17 ;; the terms of the GNU General Public License as published by the Free
18 ;; Software Foundation; either version 2, or (at your option) any later
19 ;; version.
21 ;; This is distributed in the hope that it will be useful, but WITHOUT
22 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 ;; for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING. If not, write to the
28 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
29 ;; MA 02111-1307, USA.
31 ;;; Commentary:
33 ;;; Contributors:
35 ;; Lan Yufeng (nlany DOT web AT gmail DOT com) found an error where
36 ;; headings were being given the wrong face, contributing a patch to
37 ;; fix this.
39 ;;; Code:
41 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
43 ;; Emacs Muse Highlighting
45 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
47 (require 'muse-mode)
48 (require 'muse-regexps)
49 (require 'font-lock)
51 (defgroup muse-colors nil
52 "Options controlling the behavior of Emacs Muse highlighting.
53 See `muse-colors-buffer' for more information."
54 :group 'muse-mode)
56 (defcustom muse-colors-autogen-headings t
57 "Specify whether the heading faces should be auto-generated.
58 The default is to scale them.
60 Choosing 'outline will copy the colors from the outline-mode
61 headings.
63 If you want to customize each of the headings individually, set
64 this to nil."
65 :type '(choice (const :tag "Default (scaled) headings" t)
66 (const :tag "Use outline-mode headings" outline)
67 (const :tag "Don't touch the headings" nil))
68 :group 'muse-colors)
70 (defvar muse-colors-outline-faces-list
71 (if (facep 'outline-1)
72 '(outline-1 outline-2 outline-3 outline-4)
73 ;; These are supposed to be equivalent in coloring
74 '(font-lock-function-name-face
75 font-lock-variable-name-face
76 font-lock-keyword-face
77 font-lock-builtin-face))
78 "Outline faces to use when assigning Muse header faces.")
80 (defun muse-make-faces ()
81 (let (newsym)
82 (dolist (num '(1 2 3 4))
83 (setq newsym (intern (concat "muse-header-"
84 (int-to-string num))))
85 (cond
86 ((null muse-colors-autogen-headings)
87 (make-empty-face newsym))
88 ((featurep 'xemacs)
89 (if (eq muse-colors-autogen-headings 'outline)
90 (copy-face (nth (1- num)
91 muse-colors-outline-faces-list)
92 newsym)
93 (eval `(defface ,newsym
94 '((t (:size
95 ,(nth (1- num) '("24pt" "18pt" "14pt" "12pt"))
96 :bold t)))
97 "Muse header face"
98 :group 'muse-colors))))
99 ((< emacs-major-version 21)
100 (if (eq muse-colors-autogen-headings 'outline)
101 (copy-face (nth (1- num)
102 muse-colors-outline-faces-list)
103 newsym)
104 (copy-face 'default newsym)))
105 ((eq muse-colors-autogen-headings 'outline)
106 (eval `(defface ,newsym
107 '((t (:inherit
108 ,(nth (1- num)
109 muse-colors-outline-faces-list))))
110 "Muse header face"
111 :group 'muse-colors)))
113 (eval `(defface ,newsym
114 '((t (:height ,(1+ (* 0.1 (- 5 num)))
115 :inherit variable-pitch
116 :weight bold)))
117 "Muse header face"
118 :group 'muse-colors)))))))
120 (defface muse-link-face
121 '((((class color) (background light))
122 (:foreground "green" :underline "green" :bold t))
123 (((class color) (background dark))
124 (:foreground "cyan" :underline "cyan" :bold t))
125 (t (:bold t)))
126 "Face for Muse cross-references."
127 :group 'muse-colors)
129 (defface muse-bad-link-face
130 '((((class color) (background light))
131 (:foreground "red" :underline "red" :bold t))
132 (((class color) (background dark))
133 (:foreground "coral" :underline "coral" :bold t))
134 (t (:bold t)))
135 "Face for bad Muse cross-references."
136 :group 'muse-colors)
138 (defcustom muse-colors-buffer-hook nil
139 "A hook run after a region is highlighted.
140 Each function receives three arguments: BEG END VERBOSE.
141 BEG and END mark the range being highlighted, and VERBOSE specifies
142 whether progress messages should be displayed to the user."
143 :type 'hook
144 :group 'muse-colors)
146 (defvar muse-colors-regexp nil)
147 (defvar muse-colors-vector nil)
149 (defun muse-configure-highlighting (sym val)
150 (setq muse-colors-regexp
151 (concat "\\(" (mapconcat (function
152 (lambda (rule)
153 (if (symbolp (car rule))
154 (symbol-value (car rule))
155 (car rule)))) val "\\|") "\\)")
156 muse-colors-vector (make-vector 128 nil))
157 (let ((rules val))
158 (while rules
159 (if (eq (cadr (car rules)) t)
160 (let ((i 0) (l 128))
161 (while (< i l)
162 (unless (aref muse-colors-vector i)
163 (aset muse-colors-vector i (nth 2 (car rules))))
164 (setq i (1+ i))))
165 (aset muse-colors-vector (cadr (car rules))
166 (nth 2 (car rules))))
167 (setq rules (cdr rules))))
168 (set sym val))
170 (eval-when-compile
171 (defvar end))
173 (defun muse-colors-emphasized ()
174 ;; Here we need to check four different points - the start and end
175 ;; of the leading *s, and the start and end of the trailing *s. We
176 ;; allow the outsides to be surrounded by whitespace or punctuation,
177 ;; but no word characters, and the insides must not be surrounded by
178 ;; whitespace or punctuation. Thus the following are valid:
180 ;; " *foo bar* "
181 ;; "**foo**,"
182 ;; and the following is invalid:
183 ;; "** testing **"
184 (let* ((beg (match-beginning 0))
185 (e1 (match-end 0))
186 (leader (- e1 beg))
187 b2 e2 multiline)
188 (unless (eq (get-text-property beg 'invisible) 'muse)
189 ;; check if it's a header
190 (if (eq (char-after e1) ?\ )
191 (when (or (= beg (point-min))
192 (eq (char-before beg) ?\n))
193 (add-text-properties
194 (muse-line-beginning-position) (muse-line-end-position)
195 (list 'face (intern (concat "muse-header-"
196 (int-to-string leader))))))
197 ;; beginning of line or space or symbol
198 (when (or (= beg (point-min))
199 (eq (char-syntax (char-before beg)) ?\ )
200 (memq (char-before beg)
201 '(?\- ?\[ ?\< ?\( ?\' ?\` ?\" ?\n)))
202 (save-excursion
203 (skip-chars-forward "^*<>\n" end)
204 (when (eq (char-after) ?\n)
205 (setq multiline t)
206 (skip-chars-forward "^*<>" end))
207 (setq b2 (point))
208 (skip-chars-forward "*" end)
209 (setq e2 (point))
210 ;; Abort if space exists just before end
211 ;; or bad leader
212 ;; or no '*' at end
213 (unless (or (> leader 3)
214 (not (eq leader (- e2 b2)))
215 (eq (char-syntax (char-before b2)) ?\ )
216 (not (eq (char-after b2) ?*)))
217 (add-text-properties beg e1 '(invisible muse))
218 (add-text-properties
219 e1 b2 (list 'face (cond ((= leader 1) 'italic)
220 ((= leader 2) 'bold)
221 ((= leader 3) 'bold-italic))))
222 (add-text-properties b2 e2 '(invisible muse))
223 (when multiline
224 (add-text-properties
225 beg e2 '(font-lock-multiline t))))))))))
227 (defun muse-colors-underlined ()
228 (let ((start (match-beginning 0))
229 multiline)
230 (unless (eq (get-text-property start 'invisible) 'muse)
231 ;; beginning of line or space or symbol
232 (when (or (= start (point-min))
233 (eq (char-syntax (char-before start)) ?\ )
234 (memq (char-before start)
235 '(?\- ?\[ ?\< ?\( ?\' ?\` ?\" ?\n)))
236 (save-excursion
237 (skip-chars-forward "^_<>\n" end)
238 (when (eq (char-after) ?\n)
239 (setq multiline t)
240 (skip-chars-forward "^_<>" end))
241 ;; Abort if space exists just before end
242 ;; or no '_' at end
243 (unless (or (eq (char-syntax (char-before (point))) ?\ )
244 (not (eq (char-after (point)) ?_)))
245 (add-text-properties start (1+ start) '(invisible muse))
246 (add-text-properties (1+ start) (point) '(face underline))
247 (add-text-properties (point)
248 (min (1+ (point)) (point-max))
249 '(invisible muse))
250 (when multiline
251 (add-text-properties
252 start (min (1+ (point)) (point-max))
253 '(font-lock-multiline t)))))))))
255 (defun muse-colors-verbatim ()
256 (skip-chars-forward (concat "^" muse-regexp-space "=>") end))
258 (defcustom muse-colors-markup
259 `(;; render in teletype and suppress further parsing
260 (,(concat "\\b=[^"
261 muse-regexp-space
262 "=>]")
263 ?= muse-colors-verbatim)
265 ;; make emphasized text appear emphasized
266 ("\\*\\{1,4\\}" ?* muse-colors-emphasized)
268 ;; make underlined text appear underlined
269 (,(concat "_[^"
270 muse-regexp-blank
271 "_]")
272 ?_ muse-colors-underlined)
274 ("^#title" ?\# muse-colors-title)
276 (muse-explicit-link-regexp ?\[ muse-colors-link)
278 ;; highlight any markup tags encountered
279 (muse-tag-regexp ?\< muse-colors-custom-tags)
281 "Expressions to highlight an Emacs Muse buffer.
282 These are arranged in a rather special fashion, so as to be as quick as
283 possible.
285 Each element of the list is itself a list, of the form:
287 (LOCATE-REGEXP TEST-CHAR MATCH-FUNCTION)
289 LOCATE-REGEXP is a partial regexp, and should be the smallest possible
290 regexp to differentiate this rule from other rules. It may also be a
291 symbol containing such a regexp. The buffer region is scanned only
292 once, and LOCATE-REGEXP indicates where the scanner should stop to
293 look for highlighting possibilities.
295 TEST-CHAR is a char or t. The character should match the beginning
296 text matched by LOCATE-REGEXP. These chars are used to build a vector
297 for fast MATCH-FUNCTION calling.
299 MATCH-FUNCTION is the function called when a region has been
300 identified. It is responsible for adding the appropriate text
301 properties to change the appearance of the buffer.
303 This markup is used to modify the appearance of the original text to
304 make it look more like the published HTML would look (like making some
305 markup text invisible, inlining images, etc).
307 font-lock is used to apply the markup rules, so that they can happen
308 on a deferred basis. They are not always accurate, but you can use
309 \\[font-lock-fontifty-block] near the point of error to force
310 fontification in that area.
312 Lastly, none of the regexp should contain grouping elements that will
313 affect the match data results."
314 :type '(repeat
315 (list :tag "Highlight rule"
316 (choice (regexp :tag "Locate regexp")
317 (symbol :tag "Regexp symbol"))
318 (choice (character :tag "Confirm character")
319 (const :tag "Default rule" t))
320 function))
321 :set 'muse-configure-highlighting
322 :group 'muse-colors)
324 ;; XEmacs users don't have `font-lock-multiline'.
325 (unless (boundp 'font-lock-multiline)
326 (defvar font-lock-multiline nil))
328 (defun muse-use-font-lock ()
329 (muse-add-to-invisibility-spec 'muse)
330 (set (make-local-variable 'font-lock-multiline) 'undecided)
331 (set (make-local-variable 'font-lock-defaults)
332 `(nil t nil nil 'beginning-of-line
333 (font-lock-fontify-region-function . muse-colors-region)
334 (font-lock-unfontify-region-function
335 . muse-unhighlight-region)))
336 (set (make-local-variable 'font-lock-fontify-region-function)
337 'muse-colors-region)
338 (set (make-local-variable 'font-lock-unfontify-region-function)
339 'muse-unhighlight-region)
340 (muse-make-faces)
341 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup)
342 (font-lock-mode t))
344 (defun muse-colors-buffer ()
345 "Re-highlight the entire Muse buffer."
346 (interactive)
347 (muse-colors-region (point-min) (point-max) t))
349 (defun muse-colors-region (beg end &optional verbose)
350 "Apply highlighting according to `muse-colors-markup'.
351 Note that this function should NOT change the buffer, nor should any
352 of the functions listed in `muse-colors-markup'."
353 (let ((buffer-undo-list t)
354 (inhibit-read-only t)
355 (inhibit-point-motion-hooks t)
356 (inhibit-modification-hooks t)
357 (modified-p (buffer-modified-p))
358 deactivate-mark)
359 (unwind-protect
360 (save-excursion
361 (save-restriction
362 (widen)
363 ;; check to see if we should expand the beg/end area for
364 ;; proper multiline matches
365 (when (and font-lock-multiline
366 (> beg (point-min))
367 (get-text-property (1- beg) 'font-lock-multiline))
368 ;; We are just after or in a multiline match.
369 (setq beg (or (previous-single-property-change
370 beg 'font-lock-multiline)
371 (point-min)))
372 (goto-char beg)
373 (setq beg (muse-line-beginning-position)))
374 (when font-lock-multiline
375 (setq end (or (text-property-any end (point-max)
376 'font-lock-multiline nil)
377 (point-max))))
378 (goto-char end)
379 (setq end (muse-line-beginning-position 2))
380 ;; Undo any fontification in the area.
381 (font-lock-unfontify-region beg end)
382 ;; And apply fontification based on `muse-colors-markup'
383 (let ((len (float (- end beg)))
384 (case-fold-search nil)
385 markup-func)
386 (goto-char beg)
387 (when (< beg end)
388 (while (re-search-forward muse-colors-regexp end t)
389 (if verbose
390 (message "Highlighting buffer...%d%%"
391 (* (/ (float (- (point) beg)) len) 100)))
392 (setq markup-func
393 (aref muse-colors-vector
394 (char-after (match-beginning 0))))
395 (when markup-func (funcall markup-func))))
396 (run-hook-with-args 'muse-colors-buffer-hook
397 beg end verbose)
398 (if verbose (message "Highlighting buffer...done")))))
399 (set-buffer-modified-p modified-p))))
401 (defcustom muse-colors-tags
402 '(("example" t nil muse-colors-example-tag))
403 "A list of tag specifications for specially highlighting text.
404 XML-style tags are the best way to add custom highlighting to Muse.
405 This is easily accomplished by customizing this list of markup tags.
407 For each entry, the name of the tag is given, whether it expects
408 a closing tag and/or an optional set of attributes, and a
409 function that performs whatever action is desired within the
410 delimited region.
412 The function is called with three arguments, the beginning and
413 end of the region surrounded by the tags. If properties are
414 allowed, they are passed as a third argument in the form of an
415 alist. The `end' argument to the function is the last character
416 of the enclosed tag or region.
418 Functions should not modify the contents of the buffer."
419 :type '(repeat (list (string :tag "Markup tag")
420 (boolean :tag "Expect closing tag" :value t)
421 (boolean :tag "Parse attributes" :value nil)
422 function))
423 :group 'muse-colors)
425 (defsubst muse-colors-tag-info (tagname &rest args)
426 (assoc tagname muse-colors-tags))
428 (defun muse-colors-custom-tags ()
429 "Highlight `muse-colors-tags'."
430 (let ((tag-info (muse-colors-tag-info (match-string 4))))
431 (when tag-info
432 (let ((closed-tag (match-string 3))
433 (start (match-beginning 0))
434 end attrs)
435 (when (nth 2 tag-info)
436 (let ((attrstr (match-string 2)))
437 (while (and attrstr
438 (string-match (concat "\\([^"
439 muse-regexp-space
440 "=]+\\)\\(=\""
441 "\\([^\"]+\\)\"\\)?")
442 attrstr))
443 (let ((attr (cons (downcase
444 (muse-match-string-no-properties 1 attrstr))
445 (muse-match-string-no-properties 3 attrstr))))
446 (setq attrstr (replace-match "" t t attrstr))
447 (if attrs
448 (nconc attrs (list attr))
449 (setq attrs (list attr)))))))
450 (if (and (cadr tag-info) (not closed-tag))
451 (if (search-forward (concat "</" (car tag-info) ">") nil t)
452 (setq end (match-end 0))
453 (setq tag-info nil)))
454 (when tag-info
455 (let ((args (list start end)))
456 (if (nth 2 tag-info)
457 (nconc args (list attrs)))
458 (apply (nth 3 tag-info) args)))))))
460 (defun muse-colors-example-tag (beg end)
461 "Strip properties from stuff in example."
462 (set-text-properties beg end nil)
463 (goto-char end))
465 (defun muse-unhighlight-region (begin end &optional verbose)
466 "Remove all visual highlights in the buffer (except font-lock)."
467 (let ((buffer-undo-list t)
468 (inhibit-read-only t)
469 (inhibit-point-motion-hooks t)
470 (inhibit-modification-hooks t)
471 (modified-p (buffer-modified-p))
472 deactivate-mark)
473 (unwind-protect
474 (remove-text-properties
475 begin end '(face nil font-lock-multiline nil
476 invisible nil intangible nil display nil
477 mouse-face nil keymap nil help-echo nil))
478 (set-buffer-modified-p modified-p))))
480 (defvar muse-mode-local-map
481 (let ((map (make-sparse-keymap)))
482 (define-key map [return] 'muse-follow-name-at-point)
483 (define-key map [(control ?m)] 'muse-follow-name-at-point)
484 (define-key map [(shift return)] 'muse-follow-name-at-point-other-window)
485 (if (featurep 'xemacs)
486 (progn
487 (define-key map [(button2)] 'muse-follow-name-at-mouse)
488 (define-key map [(shift button2)]
489 'muse-follow-name-at-mouse-other-window))
490 (define-key map [(shift control ?m)]
491 'muse-follow-name-at-point-other-window)
492 (define-key map [mouse-2] 'muse-follow-name-at-mouse)
493 (define-key map [(shift mouse-2)]
494 'muse-follow-name-at-mouse-other-window)
495 (unless (eq emacs-major-version 21)
496 (set-keymap-parent map muse-mode-map)))
497 map)
498 "Local keymap used by Muse while on a link.")
500 (defvar muse-keymap-property
501 (if (or (featurep 'xemacs)
502 (>= emacs-major-version 21))
503 'keymap
504 'local-map))
506 (defsubst muse-link-properties (help-str &optional face)
507 (append (if face
508 (list 'face face 'rear-nonsticky t
509 muse-keymap-property muse-mode-local-map)
510 (list 'invisible 'muse 'intangible t 'rear-nonsticky t
511 muse-keymap-property muse-mode-local-map))
512 (list 'mouse-face 'highlight
513 'help-echo help-str
514 muse-keymap-property muse-mode-local-map)))
516 (defun muse-link-face (link-name &optional explicit)
517 "Return the type of LINK-NAME as a face symbol.
518 For EXPLICIT links, this is either a normal link or a bad-link
519 face. For implicit links, it is either colored normally or
520 ignored."
521 (save-match-data
522 (let ((link (if explicit
523 (muse-handle-explicit-link link-name)
524 (muse-handle-implicit-link link-name))))
525 (when link
526 (if (or (not explicit)
527 (string-match muse-file-regexp link)
528 (string-match muse-url-regexp link))
529 'muse-link-face
530 (if (not (featurep 'muse-project))
531 'muse-link-face
532 (if (string-match "#" link)
533 (setq link (substring link 0 (match-beginning 0))))
534 (if (or (and (muse-project-of-file)
535 (muse-project-page-file
536 link-name muse-current-project t))
537 (file-exists-p link))
538 'muse-link-face
539 (when explicit
540 'muse-bad-link-face))))))))
542 (defun muse-colors-link ()
543 (when (eq ?\[ (char-after (match-beginning 0)))
544 ;; remove flyspell overlays
545 (when (fboundp 'flyspell-unhighlight-at)
546 (let ((cur (match-beginning 0)))
547 (while (> (match-end 0) cur)
548 (flyspell-unhighlight-at cur)
549 (setq cur (1+ cur)))))
550 (let* (;; FIXME: link is not used
551 (link (muse-match-string-no-properties 2))
552 (desc (muse-match-string-no-properties 3))
553 (props (muse-link-properties
554 desc (muse-link-face (match-string 2) t)))
555 (invis-props (append props (muse-link-properties desc))))
556 (if desc
557 (progn
558 ;; we put the normal face properties on the invisible
559 ;; portion too, since emacs sometimes will position
560 ;; the cursor on an intangible character
561 (add-text-properties (match-beginning 0)
562 (match-beginning 3) invis-props)
563 (add-text-properties (match-beginning 3) (match-end 3) props)
564 (add-text-properties (match-end 3) (match-end 0) invis-props))
565 (add-text-properties (match-beginning 0)
566 (match-beginning 2) invis-props)
567 (add-text-properties (match-beginning 2) (match-end 0) props)
568 (add-text-properties (match-end 2) (match-end 0) invis-props)))
569 (goto-char (match-end 0))
570 (add-text-properties
571 (match-beginning 0) (match-end 0)
572 (muse-link-properties (muse-match-string-no-properties 0)
573 (muse-link-face (match-string 2) t)))
574 (goto-char (match-end 0))))
576 (defun muse-colors-title ()
577 (add-text-properties (+ 7 (match-beginning 0))
578 (muse-line-end-position)
579 '(face muse-header-1)))
581 (provide 'muse-colors)
583 ;;; muse-colors.el ends here