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