Note that muse-file-extension should not have "." in front.
[muse-el.git] / lisp / muse.el
blob66e271215645d9e02e90ad350e8ff0006e3a4210
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
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"
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 'muse-regexps)
66 ;;; Return an list of known wiki names and the files they represent.
68 (defsubst muse-delete-file-if-exists (file)
69 (when (file-exists-p file)
70 (delete-file file)
71 (message "Removed %s" file)))
73 (defsubst muse-time-less-p (t1 t2)
74 "Say whether time T1 is less than time T2."
75 (or (< (car t1) (car t2))
76 (and (= (car t1) (car t2))
77 (< (nth 1 t1) (nth 1 t2)))))
79 (eval-when-compile
80 (defvar muse-publishing-current-file nil))
82 (defun muse-page-name (&optional name)
83 "Return the canonical form of a Muse page name.
84 All this means is that certain extensions, like .gz, are removed."
85 (save-match-data
86 (unless name
87 (setq name (or (and (boundp 'muse-publishing-current-file)
88 muse-publishing-current-file)
89 buffer-file-name)))
90 (if name
91 (let ((page (file-name-nondirectory name)))
92 (if (and muse-ignored-extensions-regexp
93 (string-match muse-ignored-extensions-regexp page))
94 (replace-match "" t t page)
95 page)))))
97 (defun muse-eval-lisp (form)
98 "Evaluate the given form and return the result as a string."
99 (require 'pp)
100 (save-match-data
101 (let ((object (eval (read form))))
102 (cond
103 ((stringp object) object)
104 ((and (listp object)
105 (not (eq object nil)))
106 (let ((string (pp-to-string object)))
107 (substring string 0 (1- (length string)))))
108 ((numberp object)
109 (number-to-string object))
110 ((eq object nil) "")
112 (pp-to-string object))))))
114 ;; The following code was extracted from cl
116 (defun muse-const-expr-p (x)
117 (cond ((consp x)
118 (or (eq (car x) 'quote)
119 (and (memq (car x) '(function function*))
120 (or (symbolp (nth 1 x))
121 (and (eq (and (consp (nth 1 x))
122 (car (nth 1 x))) 'lambda) 'func)))))
123 ((symbolp x) (and (memq x '(nil t)) t))
124 (t t)))
126 (put 'muse-assertion-failed 'error-conditions '(error))
127 (put 'muse-assertion-failed 'error-message "Assertion failed")
129 (defun muse-list* (arg &rest rest)
130 "Return a new list with specified args as elements, cons'd to last arg.
131 Thus, `(list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to
132 `(cons A (cons B (cons C D)))'."
133 (cond ((not rest) arg)
134 ((not (cdr rest)) (cons arg (car rest)))
135 (t (let* ((n (length rest))
136 (copy (copy-sequence rest))
137 (last (nthcdr (- n 2) copy)))
138 (setcdr last (car (cdr last)))
139 (cons arg copy)))))
141 (defmacro muse-assert (form &optional show-args string &rest args)
142 "Verify that FORM returns non-nil; signal an error if not.
143 Second arg SHOW-ARGS means to include arguments of FORM in message.
144 Other args STRING and ARGS... are arguments to be passed to `error'.
145 They are not evaluated unless the assertion fails. If STRING is
146 omitted, a default message listing FORM itself is used."
147 (let ((sargs
148 (and show-args
149 (delq nil (mapcar
150 (function
151 (lambda (x)
152 (and (not (muse-const-expr-p x)) x)))
153 (cdr form))))))
154 (list 'progn
155 (list 'or form
156 (if string
157 (muse-list* 'error string (append sargs args))
158 (list 'signal '(quote muse-assertion-failed)
159 (muse-list* 'list (list 'quote form) sargs))))
160 nil)))
162 ;; Compatibility functions
164 (defun muse-looking-back (regexp &optional limit)
165 (if (fboundp 'looking-back)
166 (looking-back regexp limit)
167 (save-excursion
168 (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t))))
170 (defun muse-line-end-position (&optional n)
171 (if (fboundp 'line-end-position)
172 (line-end-position n)
173 (save-excursion (end-of-line n) (point))))
175 (defun muse-line-beginning-position (&optional n)
176 (if (fboundp 'line-beginning-position)
177 (line-beginning-position n)
178 (save-excursion (beginning-of-line n) (point))))
180 (defun muse-match-string-no-properties (num &optional string)
181 (if (fboundp 'match-string-no-properties)
182 (match-string-no-properties num string)
183 (match-string num string)))
185 (defun muse-replace-regexp-in-string (regexp replacement text &optional fixedcase literal)
186 "Replace REGEXP with REPLACEMENT in TEXT.
187 If fourth arg FIXEDCASE is non-nil, do not alter case of replacement text.
188 If fifth arg LITERAL is non-nil, insert REPLACEMENT literally."
189 (cond
190 ((fboundp 'replace-regexp-in-string)
191 (replace-regexp-in-string regexp replacement text fixedcase literal))
192 ((fboundp 'replace-in-string)
193 (replace-in-string text regexp replacement literal))
194 (t (while (string-match regexp text)
195 (setq text (replace-match replacement fixedcase literal text)))
196 text)))
198 (defun muse-add-to-invisibility-spec (element)
199 "Add ELEMENT to `buffer-invisibility-spec'.
200 See documentation for `buffer-invisibility-spec' for the kind of elements
201 that can be added."
202 (if (fboundp 'add-to-invisibility-spec)
203 (add-to-invisibility-spec element)
204 (if (eq buffer-invisibility-spec t)
205 (setq buffer-invisibility-spec (list t)))
206 (setq buffer-invisibility-spec
207 (cons element buffer-invisibility-spec))))
209 ;; Link-handling functions and variables
211 (defun muse-handle-url (&optional string)
212 "If STRING or point has a URL, match and return it."
213 (if (if string (string-match muse-url-regexp string)
214 (looking-at muse-url-regexp))
215 (match-string 0 string)))
217 (defcustom muse-implicit-link-functions '(muse-handle-url)
218 "A list of functions to handle an implicit link.
219 An implicit link is one that is not surrounded by brackets.
221 By default, Muse handles URLs only.
222 If you want to handle WikiWords, load muse-wiki.el."
223 :type '(repeat function)
224 :group 'muse)
226 (defun muse-handle-implicit-link (&optional link)
227 "Handle implicit links. If LINK is not specified, look at point.
228 An implicit link is one that is not surrounded by brackets.
229 By default, Muse handles URLs only.
230 If you want to handle WikiWords, load muse-wiki.el.
232 This function modifies the match data so that match 1 is the
233 link. Match 2 will usually be nil, unless the description is
234 embedded in the text of the buffer.
236 The match data is restored after each unsuccessful handler
237 function call. If LINK is specified, only restore at very end.
239 This behavior is needed because the part of the buffer that
240 `muse-implicit-link-regexp' matches must be narrowed to the part
241 that is an accepted link."
242 (let ((funcs muse-implicit-link-functions)
243 (res nil)
244 (data (match-data t)))
245 (while funcs
246 (setq res (funcall (car funcs) link))
247 (if res
248 (setq funcs nil)
249 (unless link (set-match-data data))
250 (setq funcs (cdr funcs))))
251 (when link (set-match-data data))
252 res))
254 (defcustom muse-explicit-link-functions nil
255 "A list of functions to handle an explicit link.
256 An explicit link is one [[like][this]] or [[this]]."
257 :type '(repeat function)
258 :group 'muse)
260 (defun muse-handle-explicit-link (&optional link)
261 "Handle explicit links. If LINK is not specified, look at point.
262 An explicit link is one that looks [[like][this]] or [[this]].
264 This function modifies the match data so that match 1 is the link
265 and match 2 is the description. Perhaps someday match 3 might be
266 the text to use for the alt element of an <a> or <img> tag.
268 The match data is saved. If no handlers are able to process
269 LINK, return LINK (if specified) or the 1st match string. If
270 LINK is not specified, it is assumed that Muse has matched
271 against `muse-explicit-link-regexp' before calling this
272 function."
273 (let ((funcs muse-explicit-link-functions)
274 (res nil))
275 (save-match-data
276 (while funcs
277 (setq res (funcall (car funcs) link))
278 (if res
279 (setq funcs nil)
280 (setq funcs (cdr funcs)))))
281 (if res
283 (or link (match-string 1)))))
285 ;; Default file extension
287 (defcustom muse-file-extension nil
288 "File extension of Muse files. Omit the period at the beginning."
289 :type '(choice
290 (const :tag "None" nil)
291 (string))
292 :set 'muse-update-ignored-extensions-regexp
293 :group 'muse)
295 (provide 'muse)
297 ;;; muse.el ends here