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