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