Try to fix failure to recognize Muse file extension (2nd attempt).
[muse-el.git] / lisp / muse.el
blobb2267ca60da6e90fca830c70842d66eea540d641
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 (stringp val)
96 (featurep 'muse-mode))
97 (add-to-list 'auto-mode-alist
98 (cons (concat "\\." val "\\'")
99 'muse-mode-choose-mode)))
100 (when (fboundp 'muse-update-ignored-extensions-regexp)
101 (muse-update-ignored-extensions-regexp
102 'muse-ignored-extensions muse-ignored-extensions)))
104 (defcustom muse-file-extension "muse"
105 "File extension of Muse files. Omit the period at the beginning."
106 :type '(choice
107 (const :tag "None" nil)
108 (string))
109 :set 'muse-update-file-extension
110 :group 'muse)
112 (defun muse-update-ignored-extensions-regexp (sym val)
113 "Update the value of `muse-ignored-extensions-regexp'."
114 (set sym val)
115 (if val
116 (setq muse-ignored-extensions-regexp
117 (concat "\\.\\("
118 (regexp-quote (or muse-file-extension "")) "\\|"
119 (mapconcat 'identity val "\\|")
120 "\\)\\'"))
121 (setq muse-ignored-extensions-regexp
122 (if muse-file-extension
123 (concat "\\.\\(" muse-file-extension "\\)\\'")
124 nil))))
126 (add-hook 'muse-update-values-hook
127 (lambda ()
128 (muse-update-ignored-extensions-regexp
129 'muse-ignored-extensions-regexp muse-ignored-extensions-regexp)))
131 ;; Once the user's init file has been processed, determine whether
132 ;; they want a file extension
133 (if inhibit-startup-hooks
134 ;; we've already started Emacs, so just update the extension now
135 (unless (rassoc 'muse-mode-choose-mode auto-mode-alist)
136 (muse-update-file-extension 'muse-file-extension muse-file-extension))
137 (add-hook 'emacs-startup-hook
138 (lambda ()
139 (unless (rassoc 'muse-mode-choose-mode auto-mode-alist)
140 (muse-update-file-extension
141 'muse-file-extension muse-file-extension)))))
143 (defcustom muse-ignored-extensions '("bz2" "gz" "[Zz]")
144 "A list of extensions to omit from the ending of a Muse page name.
145 These are regexps.
147 Don't put a period at the beginning of each extension unless you
148 understand that it is part of a regexp."
149 :type '(repeat (regexp :tag "Extension"))
150 :set 'muse-update-ignored-extensions-regexp
151 :group 'muse)
153 ;; URL protocols
155 (require 'muse-protocols)
157 ;;; Return an list of known wiki names and the files they represent.
159 (defsubst muse-delete-file-if-exists (file)
160 (when (file-exists-p file)
161 (delete-file file)
162 (message "Removed %s" file)))
164 (defsubst muse-time-less-p (t1 t2)
165 "Say whether time T1 is less than time T2."
166 (or (< (car t1) (car t2))
167 (and (= (car t1) (car t2))
168 (< (nth 1 t1) (nth 1 t2)))))
170 (eval-when-compile
171 (defvar muse-publishing-current-file nil))
173 (defun muse-current-file ()
174 "Return the name of the currently visited or published file."
175 (or (and (boundp 'muse-publishing-current-file)
176 muse-publishing-current-file)
177 (buffer-file-name)
178 (concat default-directory (buffer-name))))
180 (defun muse-page-name (&optional name)
181 "Return the canonical form of a Muse page name.
182 All this means is that certain extensions, like .gz, are removed."
183 (save-match-data
184 (unless (and name (not (string= name "")))
185 (setq name (muse-current-file)))
186 (if name
187 (let ((page (file-name-nondirectory name)))
188 (if (and muse-ignored-extensions-regexp
189 (string-match muse-ignored-extensions-regexp page))
190 (replace-match "" t t page)
191 page)))))
193 (defun muse-display-warning (message)
194 "Display the given MESSAGE as a warning."
195 (if (fboundp 'display-warning)
196 (display-warning 'muse message
197 (if (featurep 'xemacs)
198 'warning
199 :warning))
200 (message message)))
202 (defun muse-eval-lisp (form)
203 "Evaluate the given form and return the result as a string."
204 (require 'pp)
205 (save-match-data
206 (condition-case err
207 (let ((object (eval (read form))))
208 (cond
209 ((stringp object) object)
210 ((and (listp object)
211 (not (eq object nil)))
212 (let ((string (pp-to-string object)))
213 (substring string 0 (1- (length string)))))
214 ((numberp object)
215 (number-to-string object))
216 ((eq object nil) "")
218 (pp-to-string object))))
219 (error
220 (muse-display-warning (format "%s: Error evaluating %s: %s"
221 (muse-page-name) form err))
222 "; INVALID LISP CODE"))))
224 (defmacro muse-with-temp-buffer (&rest body)
225 "Create a temporary buffer, and evaluate BODY there like `progn'.
226 See also `with-temp-file' and `with-output-to-string'.
227 Unlike `with-temp-buffer', this will never attempt to save the temp buffer.
228 It is meant to be used along with `insert-file-contents'."
229 (let ((temp-buffer (make-symbol "temp-buffer")))
230 `(let ((,temp-buffer (generate-new-buffer " *muse-temp*")))
231 (unwind-protect
232 (if debug-on-error
233 (with-current-buffer ,temp-buffer
234 ,@body)
235 (condition-case err
236 (with-current-buffer ,temp-buffer
237 ,@body)
238 (error
239 (if (and (boundp 'muse-batch-publishing-p)
240 muse-batch-publishing-p)
241 (progn
242 (message "%s: Error occured: %s"
243 (muse-page-name) err)
244 (backtrace))
245 (muse-display-warning
246 (format (concat "An error occurred while publishing"
247 " %s: %s\n\nSet debug-on-error to"
248 " `t' if you would like a backtrace.")
249 (muse-page-name) err))))))
250 (when (buffer-live-p ,temp-buffer)
251 (with-current-buffer ,temp-buffer
252 (set-buffer-modified-p nil))
253 (unless debug-on-error (kill-buffer ,temp-buffer)))))))
254 (put 'muse-with-temp-buffer 'lisp-indent-function 0)
255 (put 'muse-with-temp-buffer 'edebug-form-spec '(body))
257 ;; The following code was extracted from cl
259 (defun muse-const-expr-p (x)
260 (cond ((consp x)
261 (or (eq (car x) 'quote)
262 (and (memq (car x) '(function function*))
263 (or (symbolp (nth 1 x))
264 (and (eq (and (consp (nth 1 x))
265 (car (nth 1 x))) 'lambda) 'func)))))
266 ((symbolp x) (and (memq x '(nil t)) t))
267 (t t)))
269 (put 'muse-assertion-failed 'error-conditions '(error))
270 (put 'muse-assertion-failed 'error-message "Assertion failed")
272 (defun muse-list* (arg &rest rest)
273 "Return a new list with specified args as elements, cons'd to last arg.
274 Thus, `(list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to
275 `(cons A (cons B (cons C D)))'."
276 (cond ((not rest) arg)
277 ((not (cdr rest)) (cons arg (car rest)))
278 (t (let* ((n (length rest))
279 (copy (copy-sequence rest))
280 (last (nthcdr (- n 2) copy)))
281 (setcdr last (car (cdr last)))
282 (cons arg copy)))))
284 (defmacro muse-assert (form &optional show-args string &rest args)
285 "Verify that FORM returns non-nil; signal an error if not.
286 Second arg SHOW-ARGS means to include arguments of FORM in message.
287 Other args STRING and ARGS... are arguments to be passed to `error'.
288 They are not evaluated unless the assertion fails. If STRING is
289 omitted, a default message listing FORM itself is used."
290 (let ((sargs
291 (and show-args
292 (delq nil (mapcar
293 (function
294 (lambda (x)
295 (and (not (muse-const-expr-p x)) x)))
296 (cdr form))))))
297 (list 'progn
298 (list 'or form
299 (if string
300 (muse-list* 'error string (append sargs args))
301 (list 'signal '(quote muse-assertion-failed)
302 (muse-list* 'list (list 'quote form) sargs))))
303 nil)))
305 ;; Compatibility functions
307 (defun muse-looking-back (regexp &optional limit)
308 (if (fboundp 'looking-back)
309 (looking-back regexp limit)
310 (save-excursion
311 (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t))))
313 (defun muse-line-end-position (&optional n)
314 (if (fboundp 'line-end-position)
315 (line-end-position n)
316 (save-excursion (end-of-line n) (point))))
318 (defun muse-line-beginning-position (&optional n)
319 (if (fboundp 'line-beginning-position)
320 (line-beginning-position n)
321 (save-excursion (beginning-of-line n) (point))))
323 (defun muse-match-string-no-properties (num &optional string)
324 (if (fboundp 'match-string-no-properties)
325 (match-string-no-properties num string)
326 (match-string num string)))
328 (defun muse-replace-regexp-in-string (regexp replacement text &optional fixedcase literal)
329 "Replace REGEXP with REPLACEMENT in TEXT.
330 If fourth arg FIXEDCASE is non-nil, do not alter case of replacement text.
331 If fifth arg LITERAL is non-nil, insert REPLACEMENT literally."
332 (cond
333 ((fboundp 'replace-in-string)
334 (replace-in-string text regexp replacement literal))
335 ((fboundp 'replace-regexp-in-string)
336 (replace-regexp-in-string regexp replacement text fixedcase literal))
337 (t (let ((repl-len (length replacement))
338 start)
339 (while (setq start (string-match regexp text start))
340 (setq start (+ start repl-len)
341 text (replace-match replacement fixedcase literal text))))
342 text)))
344 (defun muse-add-to-invisibility-spec (element)
345 "Add ELEMENT to `buffer-invisibility-spec'.
346 See documentation for `buffer-invisibility-spec' for the kind of elements
347 that can be added."
348 (if (fboundp 'add-to-invisibility-spec)
349 (add-to-invisibility-spec element)
350 (if (eq buffer-invisibility-spec t)
351 (setq buffer-invisibility-spec (list t)))
352 (setq buffer-invisibility-spec
353 (cons element buffer-invisibility-spec))))
355 (defun muse-read-directory-name (prompt &optional dir default-dirname mustmatch initial)
356 "Read directory name - see `read-file-name' for details."
357 (if (fboundp 'read-directory-name)
358 (read-directory-name prompt dir default-dirname mustmatch initial)
359 (unless dir
360 (setq dir default-directory))
361 (read-file-name prompt dir (or default-dirname
362 (if initial (expand-file-name initial dir)
363 dir))
364 mustmatch initial)))
366 ;; Set face globally in a predictable fashion
367 (defun muse-copy-face (old new)
368 "Copy face OLD to NEW."
369 (if (featurep 'xemacs)
370 (copy-face old new 'all)
371 (copy-face old new)))
373 ;; Widget compatibility functions
375 (defun muse-widget-type-value-create (widget)
376 "Convert and instantiate the value of the :type attribute of WIDGET.
377 Store the newly created widget in the :children attribute.
379 The value of the :type attribute should be an unconverted widget type."
380 (let ((value (widget-get widget :value))
381 (type (widget-get widget :type)))
382 (widget-put widget :children
383 (list (widget-create-child-value widget
384 (widget-convert type)
385 value)))))
387 (defun muse-widget-child-value-get (widget)
388 "Get the value of the first member of :children in WIDGET."
389 (widget-value (car (widget-get widget :children))))
391 (defun muse-widget-type-match (widget value)
392 "Non-nil if the :type value of WIDGET matches VALUE.
394 The value of the :type attribute should be an unconverted widget type."
395 (widget-apply (widget-convert (widget-get widget :type)) :match value))
397 ;; Link-handling functions and variables
399 (defun muse-get-link (&optional target)
400 "Based on the match data, retrieve the link.
401 Use TARGET to get the string, if it is specified."
402 (muse-match-string-no-properties 1 target))
404 (defun muse-get-link-desc (&optional target)
405 "Based on the match data, retrieve the link description.
406 Use TARGET to get the string, if it is specified."
407 (muse-match-string-no-properties 2 target))
409 (defun muse-link-escape (text)
410 "Escape characters in TEXT that conflict with the explicit link
411 regexp."
412 (if text
413 (progn
414 (muse-replace-regexp-in-string "\\[" "%5B" text t t)
415 (muse-replace-regexp-in-string "\\]" "%5D" text t t)
416 text)
417 ""))
419 (defun muse-link-unescape (text)
420 "Un-escape characters in TEXT that conflict with the explicit
421 link regexp."
422 (if text
423 (progn
424 (muse-replace-regexp-in-string "%5B" "[" text t t)
425 (muse-replace-regexp-in-string "%5D" "]" text t t)
426 text)
427 ""))
429 (defun muse-handle-url (&optional string)
430 "If STRING or point has a URL, match and return it."
431 (if (if string (string-match muse-url-regexp string)
432 (looking-at muse-url-regexp))
433 (match-string 0 string)))
435 (defcustom muse-implicit-link-functions '(muse-handle-url)
436 "A list of functions to handle an implicit link.
437 An implicit link is one that is not surrounded by brackets.
439 By default, Muse handles URLs only.
440 If you want to handle WikiWords, load muse-wiki.el."
441 :type 'hook
442 :options '(muse-handle-url)
443 :group 'muse)
445 (defun muse-handle-implicit-link (&optional link)
446 "Handle implicit links. If LINK is not specified, look at point.
447 An implicit link is one that is not surrounded by brackets.
448 By default, Muse handles URLs only.
449 If you want to handle WikiWords, load muse-wiki.el.
451 This function modifies the match data so that match 1 is the
452 link. Match 2 will usually be nil, unless the description is
453 embedded in the text of the buffer.
455 The match data is restored after each unsuccessful handler
456 function call. If LINK is specified, only restore at very end.
458 This behavior is needed because the part of the buffer that
459 `muse-implicit-link-regexp' matches must be narrowed to the part
460 that is an accepted link."
461 (let ((funcs muse-implicit-link-functions)
462 (res nil)
463 (data (match-data t)))
464 (while funcs
465 (setq res (funcall (car funcs) link))
466 (if res
467 (setq funcs nil)
468 (unless link (set-match-data data))
469 (setq funcs (cdr funcs))))
470 (when link (set-match-data data))
471 res))
473 (defcustom muse-explicit-link-functions nil
474 "A list of functions to handle an explicit link.
475 An explicit link is one [[like][this]] or [[this]]."
476 :type 'hook
477 :group 'muse)
479 (defun muse-handle-explicit-link (&optional link)
480 "Handle explicit links. If LINK is not specified, look at point.
481 An explicit link is one that looks [[like][this]] or [[this]].
483 This function modifies the match data so that match 1 is the link
484 and match 2 is the description. Perhaps someday match 3 might be
485 the text to use for the alt element of an <a> or <img> tag.
487 The match data is saved. If no handlers are able to process
488 LINK, return LINK (if specified) or the 1st match string. If
489 LINK is not specified, it is assumed that Muse has matched
490 against `muse-explicit-link-regexp' before calling this
491 function."
492 (let ((funcs muse-explicit-link-functions)
493 (res nil))
494 (save-match-data
495 (while funcs
496 (setq res (funcall (car funcs) link))
497 (if res
498 (setq funcs nil)
499 (setq funcs (cdr funcs)))))
500 (muse-link-unescape
501 (if res
503 (or link (muse-get-link))))))
505 ;; Movement functions
507 (defun muse-list-item-type (str)
508 "Determine the type of list given STR.
509 Returns either 'ul, 'ol, 'dl-term, or 'dl-entry."
510 (cond ((or (string= str "")
511 (< (length str) 2))
512 nil)
513 ((and (= (aref str 0) ?\ )
514 (= (aref str 1) ?-))
515 'ul)
516 ((save-match-data
517 (string-match (concat "\\`[" muse-regexp-blank "][0-9]+\\.") str))
518 'ol)
519 ((save-match-data
520 (not (string-match (concat "\\`[" muse-regexp-blank "]*::") str)))
521 ;; if str is not any kind of list, it will be interpreted as
522 ;; a dl-term
523 'dl-term)
524 (t 'dl-entry)))
526 (defun muse-forward-paragraph (&optional pattern)
527 (when (get-text-property (point) 'end-list)
528 (goto-char (next-single-property-change (point) 'end-list)))
529 (let ((next-list-end (or (next-single-property-change (point) 'end-list)
530 (point-max))))
531 (forward-line 1)
532 (if (re-search-forward (if pattern
533 (concat "^\\(?:" pattern "\\|\n\\|\\'\\)")
534 "^\\s-*\\(\n\\|\\'\\)") nil t)
535 (goto-char (match-beginning 0))
536 (goto-char (point-max)))
537 (when (> (point) next-list-end)
538 (goto-char next-list-end))))
540 (defun muse-forward-list-item-1 (type empty-line indented-line)
541 "Determine whether a nested list item is after point."
542 (if (match-beginning 1)
543 ;; if we are given a dl entry, skip past everything on the same
544 ;; level, except for other dl entries
545 (and (eq type 'dl-entry)
546 (not (eq (char-after (match-beginning 2)) ?\:)))
547 ;; blank line encountered with no list item on the same
548 ;; level after it
549 (let ((beg (point)))
550 (forward-line 1)
551 (if (save-match-data
552 (and (looking-at indented-line)
553 (not (looking-at empty-line))))
554 ;; found that this blank line is followed by some
555 ;; indentation, plus other text, so we'll keep
556 ;; going
558 (goto-char beg)
559 nil))))
561 (defun muse-forward-list-item (type indent &optional no-skip-nested)
562 "Move forward to the next item of TYPE.
563 Return non-nil if successful, nil otherwise.
564 The beginning indentation is given by INDENT.
566 If NO-SKIP-NESTED is non-nil, do not skip past nested items.
567 Note that if you desire this behavior, you will also need to
568 provide a very liberal INDENT value, such as
569 \(concat \"[\" muse-regexp-blank \"]*\")."
570 (let* ((list-item (format muse-list-item-regexp indent))
571 (empty-line (concat "^[" muse-regexp-blank "]*\n"))
572 (indented-line (concat "^" indent "[" muse-regexp-blank "]"))
573 (list-pattern (concat "\\(?:" empty-line "\\)?"
574 "\\(" list-item "\\)")))
575 (while (progn
576 (muse-forward-paragraph list-pattern)
577 ;; make sure we don't go past boundary
578 (and (not no-skip-nested)
579 (not (or (get-text-property (point) 'end-list)
580 (>= (point) (point-max))))
581 (muse-forward-list-item-1 type empty-line indented-line))))
582 (cond ((or (get-text-property (point) 'end-list)
583 (>= (point) (point-max)))
584 ;; at a list boundary, so stop
585 nil)
586 ((and (match-string 2)
587 (eq type (muse-list-item-type (match-string 2))))
588 ;; same type, so indicate that there are more items to be
589 ;; parsed
590 (goto-char (match-beginning 1)))
592 (when (match-beginning 1)
593 (goto-char (match-beginning 1)))
594 ;; move to just before foreign list item markup
595 nil))))
597 (provide 'muse)
599 ;;; muse.el ends here