Make things in contrib directory installable.
[muse-el.git] / lisp / muse.el
blobd44d5b5eb6353f1ba892af1d49bc158dad1c3162
1 ;;; muse.el --- an authoring and publishing tool for Emacs
3 ;; Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: muse.el
7 ;; Version: 3.02.90 (3.03 RC1)
8 ;; Date: Fri 7-Apr-2006
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/EmacsMuse.html
14 ;; Compatibility: Emacs21 XEmacs21 Emacs22
16 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
18 ;; Emacs Muse is free software; you can redistribute it and/or modify
19 ;; it under the terms of the GNU General Public License as published
20 ;; by the Free Software Foundation; either version 2, or (at your
21 ;; option) any later version.
23 ;; Emacs Muse is distributed in the hope that it will be useful, but
24 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 ;; General Public License for more details.
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with Emacs Muse; 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 manual for more
42 ;; information.
44 ;;; Contributors:
46 ;;; Code:
48 (defvar muse-version "3.02.90"
49 "The version of Muse currently loaded")
51 (defun muse-version (&optional insert)
52 "Display the version of Muse that is currently loaded.
53 If INSERT is non-nil, insert the text instead of displaying it."
54 (interactive "P")
55 (if insert
56 (insert muse-version)
57 (message muse-version)))
59 (defgroup muse nil
60 "Options controlling the behavior of Muse.
61 The markup used by Muse is intended to be very friendly to people
62 familiar with Emacs."
63 :group 'hypermedia)
65 (defvar muse-under-windows-p (memq system-type '(ms-dos windows-nt)))
67 (require 'wid-edit)
68 (require 'muse-regexps)
70 (defvar muse-update-values-hook nil
71 "Hook for values that are automatically generated.
72 This is to be used by add-on modules for Muse.
73 It is run just before colorizing or publishing a buffer.")
75 ;; Default file extension
77 (eval-when-compile
78 (defvar muse-ignored-extensions))
80 (defvar muse-ignored-extensions-regexp nil
81 "A regexp of extensions to omit from the ending of a Muse page name.
82 This is autogenerated from `muse-ignored-extensions'.")
84 (defun muse-update-file-extension (sym val)
85 "Update the value of `muse-file-extension'."
86 (when (and (boundp sym) (symbol-value sym)
87 (featurep 'muse-mode))
88 ;; remove old auto-mode-alist association
89 (setq auto-mode-alist
90 (delete (cons (concat "\\." (symbol-value sym) "\\'")
91 'muse-mode-choose-mode)
92 auto-mode-alist)))
93 (set sym val)
94 ;; associate .muse with muse-mode
95 (when (and val (featurep 'muse-mode))
96 (add-to-list 'auto-mode-alist
97 (cons (concat "\\." val "\\'")
98 'muse-mode-choose-mode)))
99 (when (fboundp 'muse-update-ignored-extensions-regexp)
100 (muse-update-ignored-extensions-regexp
101 'muse-ignored-extensions muse-ignored-extensions)))
103 (defcustom muse-file-extension "muse"
104 "File extension of Muse files. Omit the period at the beginning."
105 :type '(choice
106 (const :tag "None" nil)
107 (string))
108 :set 'muse-update-file-extension
109 :group 'muse)
111 (defun muse-update-ignored-extensions-regexp (sym val)
112 "Update the value of `muse-ignored-extensions-regexp'."
113 (set sym val)
114 (if val
115 (setq muse-ignored-extensions-regexp
116 (concat "\\.\\("
117 (regexp-quote (or muse-file-extension "")) "\\|"
118 (mapconcat 'identity val "\\|")
119 "\\)\\'"))
120 (setq muse-ignored-extensions-regexp
121 (if muse-file-extension
122 (concat "\\.\\(" muse-file-extension "\\)\\'")
123 nil))))
125 (add-hook 'muse-update-values-hook
126 (lambda ()
127 (muse-update-ignored-extensions-regexp
128 'muse-ignored-extensions muse-ignored-extensions)))
130 (defcustom muse-ignored-extensions '("bz2" "gz" "[Zz]")
131 "A list of extensions to omit from the ending of a Muse page name.
132 These are regexps.
134 Don't put a period at the beginning of each extension unless you
135 understand that it is part of a regexp."
136 :type '(repeat (regexp :tag "Extension"))
137 :set 'muse-update-ignored-extensions-regexp
138 :group 'muse)
140 ;; URL protocols
142 (require 'muse-protocols)
144 ;;; Return an list of known wiki names and the files they represent.
146 (defsubst muse-delete-file-if-exists (file)
147 (when (file-exists-p file)
148 (delete-file file)
149 (message "Removed %s" file)))
151 (defsubst muse-time-less-p (t1 t2)
152 "Say whether time T1 is less than time T2."
153 (or (< (car t1) (car t2))
154 (and (= (car t1) (car t2))
155 (< (nth 1 t1) (nth 1 t2)))))
157 (eval-when-compile
158 (defvar muse-publishing-current-file nil))
160 (defun muse-current-file ()
161 "Return the name of the currently visited or published file."
162 (or (and (boundp 'muse-publishing-current-file)
163 muse-publishing-current-file)
164 (buffer-file-name)
165 (concat default-directory (buffer-name))))
167 (defun muse-page-name (&optional name)
168 "Return the canonical form of a Muse page name.
169 All this means is that certain extensions, like .gz, are removed."
170 (save-match-data
171 (unless (and name (not (string= name "")))
172 (setq name (muse-current-file)))
173 (if name
174 (let ((page (file-name-nondirectory name)))
175 (if (and muse-ignored-extensions-regexp
176 (string-match muse-ignored-extensions-regexp page))
177 (replace-match "" t t page)
178 page)))))
180 (defun muse-display-warning (message)
181 "Display the given MESSAGE as a warning."
182 (if (fboundp 'display-warning)
183 (display-warning 'muse message
184 (if (featurep 'xemacs)
185 'warning
186 :warning))
187 (message message)))
189 (defun muse-eval-lisp (form)
190 "Evaluate the given form and return the result as a string."
191 (require 'pp)
192 (save-match-data
193 (condition-case err
194 (let ((object (eval (read form))))
195 (cond
196 ((stringp object) object)
197 ((and (listp object)
198 (not (eq object nil)))
199 (let ((string (pp-to-string object)))
200 (substring string 0 (1- (length string)))))
201 ((numberp object)
202 (number-to-string object))
203 ((eq object nil) "")
205 (pp-to-string object))))
206 (error
207 (muse-display-warning (format "%s: Error evaluating %s: %s"
208 (muse-page-name) form err))
209 "; INVALID LISP CODE"))))
211 (defmacro muse-with-temp-buffer (&rest body)
212 "Create a temporary buffer, and evaluate BODY there like `progn'.
213 See also `with-temp-file' and `with-output-to-string'.
214 Unlike `with-temp-buffer', this will never attempt to save the temp buffer.
215 It is meant to be used along with `insert-file-contents'."
216 (let ((temp-buffer (make-symbol "temp-buffer")))
217 `(let ((,temp-buffer (generate-new-buffer " *muse-temp*")))
218 (unwind-protect
219 (if debug-on-error
220 (with-current-buffer ,temp-buffer
221 ,@body)
222 (condition-case err
223 (with-current-buffer ,temp-buffer
224 ,@body)
225 (error
226 (if (and (boundp 'muse-batch-publishing-p)
227 muse-batch-publishing-p)
228 (progn
229 (message "%s: Error occured: %s"
230 (muse-page-name) err)
231 (backtrace))
232 (muse-display-warning
233 (format (concat "An error occurred while publishing"
234 " %s: %s\n\nSet debug-on-error to"
235 " `t' if you would like a backtrace.")
236 (muse-page-name) err))))))
237 (when (buffer-live-p ,temp-buffer)
238 (with-current-buffer ,temp-buffer
239 (set-buffer-modified-p nil))
240 (unless debug-on-error (kill-buffer ,temp-buffer)))))))
241 (put 'muse-with-temp-buffer 'lisp-indent-function 0)
242 (put 'muse-with-temp-buffer 'edebug-form-spec '(body))
244 ;; The following code was extracted from cl
246 (defun muse-const-expr-p (x)
247 (cond ((consp x)
248 (or (eq (car x) 'quote)
249 (and (memq (car x) '(function function*))
250 (or (symbolp (nth 1 x))
251 (and (eq (and (consp (nth 1 x))
252 (car (nth 1 x))) 'lambda) 'func)))))
253 ((symbolp x) (and (memq x '(nil t)) t))
254 (t t)))
256 (put 'muse-assertion-failed 'error-conditions '(error))
257 (put 'muse-assertion-failed 'error-message "Assertion failed")
259 (defun muse-list* (arg &rest rest)
260 "Return a new list with specified args as elements, cons'd to last arg.
261 Thus, `(list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to
262 `(cons A (cons B (cons C D)))'."
263 (cond ((not rest) arg)
264 ((not (cdr rest)) (cons arg (car rest)))
265 (t (let* ((n (length rest))
266 (copy (copy-sequence rest))
267 (last (nthcdr (- n 2) copy)))
268 (setcdr last (car (cdr last)))
269 (cons arg copy)))))
271 (defmacro muse-assert (form &optional show-args string &rest args)
272 "Verify that FORM returns non-nil; signal an error if not.
273 Second arg SHOW-ARGS means to include arguments of FORM in message.
274 Other args STRING and ARGS... are arguments to be passed to `error'.
275 They are not evaluated unless the assertion fails. If STRING is
276 omitted, a default message listing FORM itself is used."
277 (let ((sargs
278 (and show-args
279 (delq nil (mapcar
280 (function
281 (lambda (x)
282 (and (not (muse-const-expr-p x)) x)))
283 (cdr form))))))
284 (list 'progn
285 (list 'or form
286 (if string
287 (muse-list* 'error string (append sargs args))
288 (list 'signal '(quote muse-assertion-failed)
289 (muse-list* 'list (list 'quote form) sargs))))
290 nil)))
292 ;; Compatibility functions
294 (defun muse-looking-back (regexp &optional limit)
295 (if (fboundp 'looking-back)
296 (looking-back regexp limit)
297 (save-excursion
298 (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t))))
300 (defun muse-line-end-position (&optional n)
301 (if (fboundp 'line-end-position)
302 (line-end-position n)
303 (save-excursion (end-of-line n) (point))))
305 (defun muse-line-beginning-position (&optional n)
306 (if (fboundp 'line-beginning-position)
307 (line-beginning-position n)
308 (save-excursion (beginning-of-line n) (point))))
310 (defun muse-match-string-no-properties (num &optional string)
311 (if (fboundp 'match-string-no-properties)
312 (match-string-no-properties num string)
313 (match-string num string)))
315 (defun muse-replace-regexp-in-string (regexp replacement text &optional fixedcase literal)
316 "Replace REGEXP with REPLACEMENT in TEXT.
317 If fourth arg FIXEDCASE is non-nil, do not alter case of replacement text.
318 If fifth arg LITERAL is non-nil, insert REPLACEMENT literally."
319 (cond
320 ((fboundp 'replace-in-string)
321 (replace-in-string text regexp replacement literal))
322 ((fboundp 'replace-regexp-in-string)
323 (replace-regexp-in-string regexp replacement text fixedcase literal))
324 (t (let ((repl-len (length replacement))
325 start)
326 (while (setq start (string-match regexp text start))
327 (setq start (+ start repl-len)
328 text (replace-match replacement fixedcase literal text))))
329 text)))
331 (defun muse-add-to-invisibility-spec (element)
332 "Add ELEMENT to `buffer-invisibility-spec'.
333 See documentation for `buffer-invisibility-spec' for the kind of elements
334 that can be added."
335 (if (fboundp 'add-to-invisibility-spec)
336 (add-to-invisibility-spec element)
337 (if (eq buffer-invisibility-spec t)
338 (setq buffer-invisibility-spec (list t)))
339 (setq buffer-invisibility-spec
340 (cons element buffer-invisibility-spec))))
342 (defun muse-read-directory-name (prompt &optional dir default-dirname mustmatch initial)
343 "Read directory name - see `read-file-name' for details."
344 (if (fboundp 'read-directory-name)
345 (read-directory-name prompt dir default-dirname mustmatch initial)
346 (unless dir
347 (setq dir default-directory))
348 (read-file-name prompt dir (or default-dirname
349 (if initial (expand-file-name initial dir)
350 dir))
351 mustmatch initial)))
353 ;; Set face globally in a predictable fashion
354 (defun muse-copy-face (old new)
355 "Copy face OLD to NEW."
356 (if (featurep 'xemacs)
357 (copy-face old new 'all)
358 (copy-face old new)))
360 ;; Widget compatibility functions
362 (defun muse-widget-type-value-create (widget)
363 "Convert and instantiate the value of the :type attribute of WIDGET.
364 Store the newly created widget in the :children attribute.
366 The value of the :type attribute should be an unconverted widget type."
367 (let ((value (widget-get widget :value))
368 (type (widget-get widget :type)))
369 (widget-put widget :children
370 (list (widget-create-child-value widget
371 (widget-convert type)
372 value)))))
374 (defun muse-widget-child-value-get (widget)
375 "Get the value of the first member of :children in WIDGET."
376 (widget-value (car (widget-get widget :children))))
378 (defun muse-widget-type-match (widget value)
379 "Non-nil if the :type value of WIDGET matches VALUE.
381 The value of the :type attribute should be an unconverted widget type."
382 (widget-apply (widget-convert (widget-get widget :type)) :match value))
384 ;; Link-handling functions and variables
386 (defun muse-get-link (&optional target)
387 "Based on the match data, retrieve the link.
388 Use TARGET to get the string, if it is specified."
389 (muse-match-string-no-properties 1 target))
391 (defun muse-get-link-desc (&optional target)
392 "Based on the match data, retrieve the link description.
393 Use TARGET to get the string, if it is specified."
394 (muse-match-string-no-properties 2 target))
396 (defun muse-link-escape (text)
397 "Escape characters in TEXT that conflict with the explicit link
398 regexp."
399 (if text
400 (progn
401 (muse-replace-regexp-in-string "\\[" "%5B" text t t)
402 (muse-replace-regexp-in-string "\\]" "%5D" text t t)
403 text)
404 ""))
406 (defun muse-link-unescape (text)
407 "Un-escape characters in TEXT that conflict with the explicit
408 link regexp."
409 (if text
410 (progn
411 (muse-replace-regexp-in-string "%5B" "[" text t t)
412 (muse-replace-regexp-in-string "%5D" "]" text t t)
413 text)
414 ""))
416 (defun muse-handle-url (&optional string)
417 "If STRING or point has a URL, match and return it."
418 (if (if string (string-match muse-url-regexp string)
419 (looking-at muse-url-regexp))
420 (match-string 0 string)))
422 (defcustom muse-implicit-link-functions '(muse-handle-url)
423 "A list of functions to handle an implicit link.
424 An implicit link is one that is not surrounded by brackets.
426 By default, Muse handles URLs only.
427 If you want to handle WikiWords, load muse-wiki.el."
428 :type 'hook
429 :options '(muse-handle-url)
430 :group 'muse)
432 (defun muse-handle-implicit-link (&optional link)
433 "Handle implicit links. If LINK is not specified, look at point.
434 An implicit link is one that is not surrounded by brackets.
435 By default, Muse handles URLs only.
436 If you want to handle WikiWords, load muse-wiki.el.
438 This function modifies the match data so that match 1 is the
439 link. Match 2 will usually be nil, unless the description is
440 embedded in the text of the buffer.
442 The match data is restored after each unsuccessful handler
443 function call. If LINK is specified, only restore at very end.
445 This behavior is needed because the part of the buffer that
446 `muse-implicit-link-regexp' matches must be narrowed to the part
447 that is an accepted link."
448 (let ((funcs muse-implicit-link-functions)
449 (res nil)
450 (data (match-data t)))
451 (while funcs
452 (setq res (funcall (car funcs) link))
453 (if res
454 (setq funcs nil)
455 (unless link (set-match-data data))
456 (setq funcs (cdr funcs))))
457 (when link (set-match-data data))
458 res))
460 (defcustom muse-explicit-link-functions nil
461 "A list of functions to handle an explicit link.
462 An explicit link is one [[like][this]] or [[this]]."
463 :type 'hook
464 :group 'muse)
466 (defun muse-handle-explicit-link (&optional link)
467 "Handle explicit links. If LINK is not specified, look at point.
468 An explicit link is one that looks [[like][this]] or [[this]].
470 This function modifies the match data so that match 1 is the link
471 and match 2 is the description. Perhaps someday match 3 might be
472 the text to use for the alt element of an <a> or <img> tag.
474 The match data is saved. If no handlers are able to process
475 LINK, return LINK (if specified) or the 1st match string. If
476 LINK is not specified, it is assumed that Muse has matched
477 against `muse-explicit-link-regexp' before calling this
478 function."
479 (let ((funcs muse-explicit-link-functions)
480 (res nil))
481 (save-match-data
482 (while funcs
483 (setq res (funcall (car funcs) link))
484 (if res
485 (setq funcs nil)
486 (setq funcs (cdr funcs)))))
487 (muse-link-unescape
488 (if res
490 (or link (muse-get-link))))))
492 ;; Movement functions
494 (defun muse-list-item-type (str)
495 "Determine the type of list given STR.
496 Returns either 'ul, 'ol, 'dl-term, or 'dl-entry."
497 (cond ((or (string= str "")
498 (< (length str) 2))
499 nil)
500 ((and (= (aref str 0) ?\ )
501 (= (aref str 1) ?-))
502 'ul)
503 ((save-match-data
504 (string-match (concat "\\`[" muse-regexp-blank "][0-9]+\\.") str))
505 'ol)
506 ((save-match-data
507 (not (string-match (concat "\\`[" muse-regexp-blank "]*::") str)))
508 ;; if str is not any kind of list, it will be interpreted as
509 ;; a dl-term
510 'dl-term)
511 (t 'dl-entry)))
513 (defun muse-forward-paragraph (&optional pattern)
514 (when (get-text-property (point) 'end-list)
515 (goto-char (next-single-property-change (point) 'end-list)))
516 (let ((next-list-end (or (next-single-property-change (point) 'end-list)
517 (point-max))))
518 (forward-line 1)
519 (if (re-search-forward (if pattern
520 (concat "^\\(?:" pattern "\\|\n\\|\\'\\)")
521 "^\\s-*\\(\n\\|\\'\\)") nil t)
522 (goto-char (match-beginning 0))
523 (goto-char (point-max)))
524 (when (> (point) next-list-end)
525 (goto-char next-list-end))))
527 (defun muse-forward-list-item-1 (type empty-line indented-line)
528 "Determine whether a nested list item is after point."
529 (if (match-beginning 1)
530 ;; if we are given a dl entry, skip past everything on the same
531 ;; level, except for other dl entries
532 (and (eq type 'dl-entry)
533 (not (eq (char-after (match-beginning 2)) ?\:)))
534 ;; blank line encountered with no list item on the same
535 ;; level after it
536 (let ((beg (point)))
537 (forward-line 1)
538 (if (save-match-data
539 (and (looking-at indented-line)
540 (not (looking-at empty-line))))
541 ;; found that this blank line is followed by some
542 ;; indentation, plus other text, so we'll keep
543 ;; going
545 (goto-char beg)
546 nil))))
548 (defun muse-forward-list-item (type indent &optional no-skip-nested)
549 "Move forward to the next item of TYPE.
550 Return non-nil if successful, nil otherwise.
551 The beginning indentation is given by INDENT.
553 If NO-SKIP-NESTED is non-nil, do not skip past nested items.
554 Note that if you desire this behavior, you will also need to
555 provide a very liberal INDENT value, such as
556 \(concat \"[\" muse-regexp-blank \"]*\")."
557 (let* ((list-item (format muse-list-item-regexp indent))
558 (empty-line (concat "^[" muse-regexp-blank "]*\n"))
559 (indented-line (concat "^" indent "[" muse-regexp-blank "]"))
560 (list-pattern (concat "\\(?:" empty-line "\\)?"
561 "\\(" list-item "\\)")))
562 (while (progn
563 (muse-forward-paragraph list-pattern)
564 ;; make sure we don't go past boundary
565 (and (not no-skip-nested)
566 (not (or (get-text-property (point) 'end-list)
567 (>= (point) (point-max))))
568 (muse-forward-list-item-1 type empty-line indented-line))))
569 (cond ((or (get-text-property (point) 'end-list)
570 (>= (point) (point-max)))
571 ;; at a list boundary, so stop
572 nil)
573 ((and (match-string 2)
574 (eq type (muse-list-item-type (match-string 2))))
575 ;; same type, so indicate that there are more items to be
576 ;; parsed
577 (goto-char (match-beginning 1)))
579 (when (match-beginning 1)
580 (goto-char (match-beginning 1)))
581 ;; move to just before foreign list item markup
582 nil))))
584 (provide 'muse)
586 ;;; muse.el ends here