Add coloring for example, verbatim, and =signs=; anchor fix.
[muse-el.git] / lisp / muse.el
blobbe1997f57848579fd8bc26b96552da8dd5cab48f
1 ;;; muse.el --- An authoring and publishing tool for Emacs.
3 ;; Copyright (C) 2004 Free Software Foundation, Inc.
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: muse.el
7 ;; Version: 3.01.90 (3.02 RC1)
8 ;; Date: Thu 15-Jun-2005
9 ;; Keywords: hypermedia
10 ;; Author: John Wiegley (johnw AT gnu DOT org)
11 ;; Maintainer: Michael Olson (mwolson AT gnu DOT org)
12 ;; Description: An authoring and publishing tool for Emacs
13 ;; URL: http://www.mwolson.org/projects/MuseMode.html
14 ;; Compatibility: Emacs21
16 ;; This file is not part of GNU Emacs.
18 ;; This is free software; you can redistribute it and/or modify it under
19 ;; the terms of the GNU General Public License as published by the Free
20 ;; Software Foundation; either version 2, or (at your option) any later
21 ;; version.
23 ;; This is distributed in the hope that it will be useful, but WITHOUT
24 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
25 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 ;; for more details.
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with GNU Emacs; see the file COPYING. If not, write to the
30 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
31 ;; Boston, MA 02110-1301, USA.
33 ;;; Commentary:
35 ;; Muse is a tool for easily authoring and publishing documents. It
36 ;; allows for rapid prototyping of hyperlinked text, which may then be
37 ;; exported to multiple output formats -- such as HTML, LaTeX,
38 ;; Texinfo, etc.
40 ;; The markup rules used by Muse are intended to be very friendly to
41 ;; people familiar with Emacs. See the included QuickStart file in
42 ;; the `examples' directory for more information.
44 ;;; Contributors:
46 ;;; Code:
48 (defvar muse-version "3.01.90 (3.02 RC1)"
49 "The version of Muse currently loaded")
51 (defun muse-version ()
52 "Display the version of Muse that is currently loaded."
53 (interactive)
54 (message muse-version))
56 (defgroup muse nil
57 "Options controlling the behavior of Muse.
58 The markup used by Muse is intended to be very friendly to people
59 familiar with Emacs."
60 :group 'hypermedia)
62 (defvar muse-under-windows-p (memq system-type '(ms-dos windows-nt)))
64 (require 'wid-edit)
65 (require 'muse-regexps)
67 ;; Default file extension
69 (defvar muse-ignored-extensions-regexp nil
70 "A regexp of extensions to omit from the ending of a Muse page name.
71 This is autogenerated from `muse-ignored-extensions'.")
73 (defcustom muse-file-extension nil
74 "File extension of Muse files. Omit the period at the beginning."
75 :type '(choice
76 (const :tag "None" nil)
77 (string))
78 :set #'(lambda (sym val)
79 (when (and (boundp sym) (symbol-value sym))
80 ;; remove old auto-mode-alist association
81 (setq auto-mode-alist
82 (delete (cons (concat "\\." (symbol-value sym) "\\'")
83 'muse-mode-choose-mode)
84 auto-mode-alist)))
85 (set sym val)
86 ;; associate .muse with muse-mode
87 (when val
88 (add-to-list 'auto-mode-alist
89 (cons (concat "\\." val "\\'")
90 'muse-mode-choose-mode)))
91 (when (fboundp 'muse-update-ignored-extensions-regexp)
92 (muse-update-ignored-extensions-regexp
93 'muse-ignored-extensions muse-ignored-extensions)))
94 :group 'muse)
96 (defun muse-update-ignored-extensions-regexp (sym val)
97 "Update the value of `muse-ignored-extensions-regexp'."
98 (set sym val)
99 (if val
100 (setq muse-ignored-extensions-regexp
101 (concat "\\.\\("
102 (regexp-quote (or muse-file-extension "")) "\\|"
103 (mapconcat 'identity val "\\|")
104 "\\)\\'"))
105 (setq muse-ignored-extensions-regexp
106 (if muse-file-extension
107 (concat "\\.\\(" muse-file-extension "\\)\\'")
108 nil))))
110 (defcustom muse-ignored-extensions '("bz2" "gz" "[Zz]")
111 "A list of extensions to omit from the ending of a Muse page name.
112 These are regexps.
114 Don't put a period at the beginning of each extension unless you
115 understand that it is part of a regexp."
116 :type '(repeat (regexp :tag "Extension"))
117 :set 'muse-update-ignored-extensions-regexp
118 :group 'muse)
120 ;;; Return an list of known wiki names and the files they represent.
122 (defsubst muse-delete-file-if-exists (file)
123 (when (file-exists-p file)
124 (delete-file file)
125 (message "Removed %s" file)))
127 (defsubst muse-time-less-p (t1 t2)
128 "Say whether time T1 is less than time T2."
129 (or (< (car t1) (car t2))
130 (and (= (car t1) (car t2))
131 (< (nth 1 t1) (nth 1 t2)))))
133 (eval-when-compile
134 (defvar muse-publishing-current-file nil))
136 (defun muse-page-name (&optional name)
137 "Return the canonical form of a Muse page name.
138 All this means is that certain extensions, like .gz, are removed."
139 (save-match-data
140 (unless (and name (not (string= name "")))
141 (setq name (or (and (boundp 'muse-publishing-current-file)
142 muse-publishing-current-file)
143 buffer-file-name)))
144 (if name
145 (let ((page (file-name-nondirectory name)))
146 (if (and muse-ignored-extensions-regexp
147 (string-match muse-ignored-extensions-regexp page))
148 (replace-match "" t t page)
149 page)))))
151 (defun muse-eval-lisp (form)
152 "Evaluate the given form and return the result as a string."
153 (require 'pp)
154 (save-match-data
155 (condition-case err
156 (let ((object (eval (read form))))
157 (cond
158 ((stringp object) object)
159 ((and (listp object)
160 (not (eq object nil)))
161 (let ((string (pp-to-string object)))
162 (substring string 0 (1- (length string)))))
163 ((numberp object)
164 (number-to-string object))
165 ((eq object nil) "")
167 (pp-to-string object))))
168 (error
169 (if (fboundp 'display-warning)
170 (display-warning 'muse
171 (format "%s: Error evaluating %s: %s"
172 (muse-page-name) form err)
173 (if (featurep 'xemacs)
174 'warning
175 :warning))
176 (message "%s: Error evaluating %s: %s"
177 (muse-page-name) form err))
178 "<!--INVALID LISP CODE-->"))))
180 (defmacro muse-with-temp-buffer (&rest body)
181 "Create a temporary buffer, and evaluate BODY there like `progn'.
182 See also `with-temp-file' and `with-output-to-string'.
183 Unlike `with-temp-buffer', this will never attempt to save the temp buffer."
184 (let ((temp-buffer (make-symbol "temp-buffer")))
185 `(let ((,temp-buffer (generate-new-buffer " *muse-temp*")))
186 (unwind-protect
187 (if debug-on-error
188 (with-current-buffer ,temp-buffer
189 ,@body)
190 (condition-case err
191 (with-current-buffer ,temp-buffer
192 ,@body)
193 (error
194 (if (and (boundp 'muse-batch-publishing-p)
195 muse-batch-publishing-p)
196 (progn
197 (message "%s: Error occured: %s"
198 (muse-page-name) err)
199 (backtrace))
200 (if (fboundp 'display-warning)
201 (display-warning 'muse
202 (format "%s: Error occurred: %s\n\n%s"
203 (muse-page-name) err
204 (pp (quote ,body)))
205 (if (featurep 'xemacs)
206 'warning
207 :warning))
208 (message "%s: Error occured: %s\n\n%s"
209 (muse-page-name) err (pp (quote ,body))))))))
210 (when (buffer-live-p ,temp-buffer)
211 (with-current-buffer ,temp-buffer
212 (set-buffer-modified-p nil))
213 (unless debug-on-error (kill-buffer ,temp-buffer)))))))
214 (put 'muse-with-temp-buffer 'lisp-indent-function 0)
215 (put 'muse-with-temp-buffer 'edebug-form-spec '(body))
217 ;; The following code was extracted from cl
219 (defun muse-const-expr-p (x)
220 (cond ((consp x)
221 (or (eq (car x) 'quote)
222 (and (memq (car x) '(function function*))
223 (or (symbolp (nth 1 x))
224 (and (eq (and (consp (nth 1 x))
225 (car (nth 1 x))) 'lambda) 'func)))))
226 ((symbolp x) (and (memq x '(nil t)) t))
227 (t t)))
229 (put 'muse-assertion-failed 'error-conditions '(error))
230 (put 'muse-assertion-failed 'error-message "Assertion failed")
232 (defun muse-list* (arg &rest rest)
233 "Return a new list with specified args as elements, cons'd to last arg.
234 Thus, `(list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to
235 `(cons A (cons B (cons C D)))'."
236 (cond ((not rest) arg)
237 ((not (cdr rest)) (cons arg (car rest)))
238 (t (let* ((n (length rest))
239 (copy (copy-sequence rest))
240 (last (nthcdr (- n 2) copy)))
241 (setcdr last (car (cdr last)))
242 (cons arg copy)))))
244 (defmacro muse-assert (form &optional show-args string &rest args)
245 "Verify that FORM returns non-nil; signal an error if not.
246 Second arg SHOW-ARGS means to include arguments of FORM in message.
247 Other args STRING and ARGS... are arguments to be passed to `error'.
248 They are not evaluated unless the assertion fails. If STRING is
249 omitted, a default message listing FORM itself is used."
250 (let ((sargs
251 (and show-args
252 (delq nil (mapcar
253 (function
254 (lambda (x)
255 (and (not (muse-const-expr-p x)) x)))
256 (cdr form))))))
257 (list 'progn
258 (list 'or form
259 (if string
260 (muse-list* 'error string (append sargs args))
261 (list 'signal '(quote muse-assertion-failed)
262 (muse-list* 'list (list 'quote form) sargs))))
263 nil)))
265 ;; Compatibility functions
267 (defun muse-looking-back (regexp &optional limit)
268 (if (fboundp 'looking-back)
269 (looking-back regexp limit)
270 (save-excursion
271 (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t))))
273 (defun muse-line-end-position (&optional n)
274 (if (fboundp 'line-end-position)
275 (line-end-position n)
276 (save-excursion (end-of-line n) (point))))
278 (defun muse-line-beginning-position (&optional n)
279 (if (fboundp 'line-beginning-position)
280 (line-beginning-position n)
281 (save-excursion (beginning-of-line n) (point))))
283 (defun muse-match-string-no-properties (num &optional string)
284 (if (fboundp 'match-string-no-properties)
285 (match-string-no-properties num string)
286 (match-string num string)))
288 (defun muse-replace-regexp-in-string (regexp replacement text &optional fixedcase literal)
289 "Replace REGEXP with REPLACEMENT in TEXT.
290 If fourth arg FIXEDCASE is non-nil, do not alter case of replacement text.
291 If fifth arg LITERAL is non-nil, insert REPLACEMENT literally."
292 (cond
293 ((fboundp 'replace-regexp-in-string)
294 (replace-regexp-in-string regexp replacement text fixedcase literal))
295 ((fboundp 'replace-in-string)
296 (replace-in-string text regexp replacement literal))
297 (t (while (string-match regexp text)
298 (setq text (replace-match replacement fixedcase literal text)))
299 text)))
301 (defun muse-add-to-invisibility-spec (element)
302 "Add ELEMENT to `buffer-invisibility-spec'.
303 See documentation for `buffer-invisibility-spec' for the kind of elements
304 that can be added."
305 (if (fboundp 'add-to-invisibility-spec)
306 (add-to-invisibility-spec element)
307 (if (eq buffer-invisibility-spec t)
308 (setq buffer-invisibility-spec (list t)))
309 (setq buffer-invisibility-spec
310 (cons element buffer-invisibility-spec))))
312 ;; Widget compatibility functions
314 (defun muse-widget-type-value-create (widget)
315 "Convert and instantiate the value of the :type attribute of WIDGET.
316 Store the newly created widget in the :children attribute.
318 The value of the :type attribute should be an unconverted widget type."
319 (let ((value (widget-get widget :value))
320 (type (widget-get widget :type)))
321 (widget-put widget :children
322 (list (widget-create-child-value widget
323 (widget-convert type)
324 value)))))
326 (defun muse-widget-child-value-get (widget)
327 "Get the value of the first member of :children in WIDGET."
328 (widget-value (car (widget-get widget :children))))
330 (defun muse-widget-type-match (widget value)
331 "Non-nil if the :type value of WIDGET matches VALUE.
333 The value of the :type attribute should be an unconverted widget type."
334 (widget-apply (widget-convert (widget-get widget :type)) :match value))
336 ;; Link-handling functions and variables
338 (defun muse-handle-url (&optional string)
339 "If STRING or point has a URL, match and return it."
340 (if (if string (string-match muse-url-regexp string)
341 (looking-at muse-url-regexp))
342 (match-string 0 string)))
344 (defcustom muse-implicit-link-functions '(muse-handle-url)
345 "A list of functions to handle an implicit link.
346 An implicit link is one that is not surrounded by brackets.
348 By default, Muse handles URLs only.
349 If you want to handle WikiWords, load muse-wiki.el."
350 :type 'hook
351 :options '(muse-handle-url)
352 :group 'muse)
354 (defun muse-handle-implicit-link (&optional link)
355 "Handle implicit links. If LINK is not specified, look at point.
356 An implicit link is one that is not surrounded by brackets.
357 By default, Muse handles URLs only.
358 If you want to handle WikiWords, load muse-wiki.el.
360 This function modifies the match data so that match 1 is the
361 link. Match 2 will usually be nil, unless the description is
362 embedded in the text of the buffer.
364 The match data is restored after each unsuccessful handler
365 function call. If LINK is specified, only restore at very end.
367 This behavior is needed because the part of the buffer that
368 `muse-implicit-link-regexp' matches must be narrowed to the part
369 that is an accepted link."
370 (let ((funcs muse-implicit-link-functions)
371 (res nil)
372 (data (match-data t)))
373 (while funcs
374 (setq res (funcall (car funcs) link))
375 (if res
376 (setq funcs nil)
377 (unless link (set-match-data data))
378 (setq funcs (cdr funcs))))
379 (when link (set-match-data data))
380 res))
382 (defcustom muse-explicit-link-functions nil
383 "A list of functions to handle an explicit link.
384 An explicit link is one [[like][this]] or [[this]]."
385 :type 'hook
386 :group 'muse)
388 (defun muse-handle-explicit-link (&optional link)
389 "Handle explicit links. If LINK is not specified, look at point.
390 An explicit link is one that looks [[like][this]] or [[this]].
392 This function modifies the match data so that match 1 is the link
393 and match 2 is the description. Perhaps someday match 3 might be
394 the text to use for the alt element of an <a> or <img> tag.
396 The match data is saved. If no handlers are able to process
397 LINK, return LINK (if specified) or the 1st match string. If
398 LINK is not specified, it is assumed that Muse has matched
399 against `muse-explicit-link-regexp' before calling this
400 function."
401 (let ((funcs muse-explicit-link-functions)
402 (res nil))
403 (save-match-data
404 (while funcs
405 (setq res (funcall (car funcs) link))
406 (if res
407 (setq funcs nil)
408 (setq funcs (cdr funcs)))))
409 (if res
411 (or link (match-string 1)))))
413 (provide 'muse)
415 ;;; muse.el ends here