Implement escaping of "[" and "]" in extended links.
[muse-el.git] / lisp / muse.el
blobc6c90fcef1cf458a71ef5bfae7d140753301962b
1 ;;; muse.el --- an authoring and publishing tool for Emacs
3 ;; Copyright (C) 2004, 2005 Free Software Foundation, Inc.
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: muse.el
7 ;; Version: 3.02.5
8 ;; Date: Sat 17-Dec-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 XEmacs21 Emacs22
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 manual for more
42 ;; information.
44 ;;; Contributors:
46 ;;; Code:
48 (defvar muse-version "3.02.5"
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 ;; Default file extension
72 (eval-when-compile
73 (defvar muse-ignored-extensions))
75 (defvar muse-ignored-extensions-regexp nil
76 "A regexp of extensions to omit from the ending of a Muse page name.
77 This is autogenerated from `muse-ignored-extensions'.")
79 (defun muse-update-file-extension (sym val)
80 "Update the value of `muse-file-extension'."
81 (when (and (boundp sym) (symbol-value sym))
82 ;; remove old auto-mode-alist association
83 (setq auto-mode-alist
84 (delete (cons (concat "\\." (symbol-value sym) "\\'")
85 'muse-mode-choose-mode)
86 auto-mode-alist)))
87 (set sym val)
88 ;; associate .muse with muse-mode
89 (when val
90 (add-to-list 'auto-mode-alist
91 (cons (concat "\\." val "\\'")
92 'muse-mode-choose-mode)))
93 (when (fboundp 'muse-update-ignored-extensions-regexp)
94 (muse-update-ignored-extensions-regexp
95 'muse-ignored-extensions muse-ignored-extensions)))
97 (defcustom muse-file-extension "muse"
98 "File extension of Muse files. Omit the period at the beginning."
99 :type '(choice
100 (const :tag "None" nil)
101 (string))
102 :set 'muse-update-file-extension
103 :group 'muse)
105 (defun muse-update-ignored-extensions-regexp (sym val)
106 "Update the value of `muse-ignored-extensions-regexp'."
107 (set sym val)
108 (if val
109 (setq muse-ignored-extensions-regexp
110 (concat "\\.\\("
111 (regexp-quote (or muse-file-extension "")) "\\|"
112 (mapconcat 'identity val "\\|")
113 "\\)\\'"))
114 (setq muse-ignored-extensions-regexp
115 (if muse-file-extension
116 (concat "\\.\\(" muse-file-extension "\\)\\'")
117 nil))))
119 (defcustom muse-ignored-extensions '("bz2" "gz" "[Zz]")
120 "A list of extensions to omit from the ending of a Muse page name.
121 These are regexps.
123 Don't put a period at the beginning of each extension unless you
124 understand that it is part of a regexp."
125 :type '(repeat (regexp :tag "Extension"))
126 :set 'muse-update-ignored-extensions-regexp
127 :group 'muse)
129 ;; URL protocols
131 (require 'muse-protocols)
133 ;;; Return an list of known wiki names and the files they represent.
135 (defsubst muse-delete-file-if-exists (file)
136 (when (file-exists-p file)
137 (delete-file file)
138 (message "Removed %s" file)))
140 (defsubst muse-time-less-p (t1 t2)
141 "Say whether time T1 is less than time T2."
142 (or (< (car t1) (car t2))
143 (and (= (car t1) (car t2))
144 (< (nth 1 t1) (nth 1 t2)))))
146 (eval-when-compile
147 (defvar muse-publishing-current-file nil))
149 (defun muse-current-file ()
150 "Return the name of the currently visited or published file."
151 (or (and (boundp 'muse-publishing-current-file)
152 muse-publishing-current-file)
153 (buffer-file-name)
154 (concat default-directory (buffer-name))))
156 (defun muse-page-name (&optional name)
157 "Return the canonical form of a Muse page name.
158 All this means is that certain extensions, like .gz, are removed."
159 (save-match-data
160 (unless (and name (not (string= name "")))
161 (setq name (muse-current-file)))
162 (if name
163 (let ((page (file-name-nondirectory name)))
164 (if (and muse-ignored-extensions-regexp
165 (string-match muse-ignored-extensions-regexp page))
166 (replace-match "" t t page)
167 page)))))
169 (defun muse-eval-lisp (form)
170 "Evaluate the given form and return the result as a string."
171 (require 'pp)
172 (save-match-data
173 (condition-case err
174 (let ((object (eval (read form))))
175 (cond
176 ((stringp object) object)
177 ((and (listp object)
178 (not (eq object nil)))
179 (let ((string (pp-to-string object)))
180 (substring string 0 (1- (length string)))))
181 ((numberp object)
182 (number-to-string object))
183 ((eq object nil) "")
185 (pp-to-string object))))
186 (error
187 (if (fboundp 'display-warning)
188 (display-warning 'muse
189 (format "%s: Error evaluating %s: %s"
190 (muse-page-name) form err)
191 (if (featurep 'xemacs)
192 'warning
193 :warning))
194 (message "%s: Error evaluating %s: %s"
195 (muse-page-name) form err))
196 "; INVALID LISP CODE"))))
198 (defmacro muse-with-temp-buffer (&rest body)
199 "Create a temporary buffer, and evaluate BODY there like `progn'.
200 See also `with-temp-file' and `with-output-to-string'.
201 Unlike `with-temp-buffer', this will never attempt to save the temp buffer.
202 It is meant to be used along with `insert-file-contents'."
203 (let ((temp-buffer (make-symbol "temp-buffer")))
204 `(let ((,temp-buffer (generate-new-buffer " *muse-temp*")))
205 (unwind-protect
206 (if debug-on-error
207 (with-current-buffer ,temp-buffer
208 ,@body)
209 (condition-case err
210 (with-current-buffer ,temp-buffer
211 ,@body)
212 (error
213 (cond
214 ((and (boundp 'muse-batch-publishing-p)
215 muse-batch-publishing-p)
216 (message "%s: Error occured: %s"
217 (muse-page-name) err)
218 (backtrace))
219 ((fboundp 'display-warning)
220 (display-warning
221 'muse (format (concat "An error occurred while publishing"
222 " %s: %s\n\nSet debug-on-error to"
223 " `t' if you would like a backtrace.")
224 (muse-page-name) err)
225 (if (featurep 'xemacs)
226 'warning
227 :warning)))
229 (message (concat "An error occurred while publishing"
230 " %s: %s\n\nSet debug-on-error to"
231 " `t' if you would like a backtrace.")
232 (muse-page-name) err))))))
233 (when (buffer-live-p ,temp-buffer)
234 (with-current-buffer ,temp-buffer
235 (set-buffer-modified-p nil))
236 (unless debug-on-error (kill-buffer ,temp-buffer)))))))
237 (put 'muse-with-temp-buffer 'lisp-indent-function 0)
238 (put 'muse-with-temp-buffer 'edebug-form-spec '(body))
240 ;; The following code was extracted from cl
242 (defun muse-const-expr-p (x)
243 (cond ((consp x)
244 (or (eq (car x) 'quote)
245 (and (memq (car x) '(function function*))
246 (or (symbolp (nth 1 x))
247 (and (eq (and (consp (nth 1 x))
248 (car (nth 1 x))) 'lambda) 'func)))))
249 ((symbolp x) (and (memq x '(nil t)) t))
250 (t t)))
252 (put 'muse-assertion-failed 'error-conditions '(error))
253 (put 'muse-assertion-failed 'error-message "Assertion failed")
255 (defun muse-list* (arg &rest rest)
256 "Return a new list with specified args as elements, cons'd to last arg.
257 Thus, `(list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to
258 `(cons A (cons B (cons C D)))'."
259 (cond ((not rest) arg)
260 ((not (cdr rest)) (cons arg (car rest)))
261 (t (let* ((n (length rest))
262 (copy (copy-sequence rest))
263 (last (nthcdr (- n 2) copy)))
264 (setcdr last (car (cdr last)))
265 (cons arg copy)))))
267 (defmacro muse-assert (form &optional show-args string &rest args)
268 "Verify that FORM returns non-nil; signal an error if not.
269 Second arg SHOW-ARGS means to include arguments of FORM in message.
270 Other args STRING and ARGS... are arguments to be passed to `error'.
271 They are not evaluated unless the assertion fails. If STRING is
272 omitted, a default message listing FORM itself is used."
273 (let ((sargs
274 (and show-args
275 (delq nil (mapcar
276 (function
277 (lambda (x)
278 (and (not (muse-const-expr-p x)) x)))
279 (cdr form))))))
280 (list 'progn
281 (list 'or form
282 (if string
283 (muse-list* 'error string (append sargs args))
284 (list 'signal '(quote muse-assertion-failed)
285 (muse-list* 'list (list 'quote form) sargs))))
286 nil)))
288 ;; Compatibility functions
290 (defun muse-looking-back (regexp &optional limit)
291 (if (fboundp 'looking-back)
292 (looking-back regexp limit)
293 (save-excursion
294 (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t))))
296 (defun muse-line-end-position (&optional n)
297 (if (fboundp 'line-end-position)
298 (line-end-position n)
299 (save-excursion (end-of-line n) (point))))
301 (defun muse-line-beginning-position (&optional n)
302 (if (fboundp 'line-beginning-position)
303 (line-beginning-position n)
304 (save-excursion (beginning-of-line n) (point))))
306 (defun muse-match-string-no-properties (num &optional string)
307 (if (fboundp 'match-string-no-properties)
308 (match-string-no-properties num string)
309 (match-string num string)))
311 (defun muse-replace-regexp-in-string (regexp replacement text &optional fixedcase literal)
312 "Replace REGEXP with REPLACEMENT in TEXT.
313 If fourth arg FIXEDCASE is non-nil, do not alter case of replacement text.
314 If fifth arg LITERAL is non-nil, insert REPLACEMENT literally."
315 (cond
316 ((fboundp 'replace-regexp-in-string)
317 (replace-regexp-in-string regexp replacement text fixedcase literal))
318 ((fboundp 'replace-in-string)
319 (replace-in-string text regexp replacement literal))
320 (t (let ((repl-len (length replacement))
321 start)
322 (while (setq start (string-match regexp text start))
323 (setq start (+ start repl-len)
324 text (replace-match replacement fixedcase literal text))))
325 text)))
327 (defun muse-add-to-invisibility-spec (element)
328 "Add ELEMENT to `buffer-invisibility-spec'.
329 See documentation for `buffer-invisibility-spec' for the kind of elements
330 that can be added."
331 (if (fboundp 'add-to-invisibility-spec)
332 (add-to-invisibility-spec element)
333 (if (eq buffer-invisibility-spec t)
334 (setq buffer-invisibility-spec (list t)))
335 (setq buffer-invisibility-spec
336 (cons element buffer-invisibility-spec))))
338 (defun muse-read-directory-name (prompt &optional dir default-dirname mustmatch initial)
339 "Read directory name - see `read-file-name' for details."
340 (if (fboundp 'read-directory-name)
341 (read-directory-name prompt dir default-dirname mustmatch initial)
342 (unless dir
343 (setq dir default-directory))
344 (read-file-name prompt dir (or default-dirname
345 (if initial (expand-file-name initial dir)
346 dir))
347 mustmatch initial)))
349 ;; Widget compatibility functions
351 (defun muse-widget-type-value-create (widget)
352 "Convert and instantiate the value of the :type attribute of WIDGET.
353 Store the newly created widget in the :children attribute.
355 The value of the :type attribute should be an unconverted widget type."
356 (let ((value (widget-get widget :value))
357 (type (widget-get widget :type)))
358 (widget-put widget :children
359 (list (widget-create-child-value widget
360 (widget-convert type)
361 value)))))
363 (defun muse-widget-child-value-get (widget)
364 "Get the value of the first member of :children in WIDGET."
365 (widget-value (car (widget-get widget :children))))
367 (defun muse-widget-type-match (widget value)
368 "Non-nil if the :type value of WIDGET matches VALUE.
370 The value of the :type attribute should be an unconverted widget type."
371 (widget-apply (widget-convert (widget-get widget :type)) :match value))
373 ;; Link-handling functions and variables
375 (defun muse-link-escape (text)
376 "Escape characters in TEXT that conflict with the explicit link
377 regexp."
378 (if text
379 (progn
380 (muse-replace-regexp-in-string "\\[" "%5B" text t t)
381 (muse-replace-regexp-in-string "\\]" "%5D" text t t)
382 text)
383 ""))
385 (defun muse-link-unescape (text)
386 "Un-escape characters in TEXT that conflict with the explicit
387 link regexp."
388 (if text
389 (progn
390 (muse-replace-regexp-in-string "%5B" "[" text t t)
391 (muse-replace-regexp-in-string "%5D" "]" text t t)
392 text)
393 ""))
395 (defun muse-handle-url (&optional string)
396 "If STRING or point has a URL, match and return it."
397 (if (if string (string-match muse-url-regexp string)
398 (looking-at muse-url-regexp))
399 (match-string 0 string)))
401 (defcustom muse-implicit-link-functions '(muse-handle-url)
402 "A list of functions to handle an implicit link.
403 An implicit link is one that is not surrounded by brackets.
405 By default, Muse handles URLs only.
406 If you want to handle WikiWords, load muse-wiki.el."
407 :type 'hook
408 :options '(muse-handle-url)
409 :group 'muse)
411 (defun muse-handle-implicit-link (&optional link)
412 "Handle implicit links. If LINK is not specified, look at point.
413 An implicit link is one that is not surrounded by brackets.
414 By default, Muse handles URLs only.
415 If you want to handle WikiWords, load muse-wiki.el.
417 This function modifies the match data so that match 1 is the
418 link. Match 2 will usually be nil, unless the description is
419 embedded in the text of the buffer.
421 The match data is restored after each unsuccessful handler
422 function call. If LINK is specified, only restore at very end.
424 This behavior is needed because the part of the buffer that
425 `muse-implicit-link-regexp' matches must be narrowed to the part
426 that is an accepted link."
427 (let ((funcs muse-implicit-link-functions)
428 (res nil)
429 (data (match-data t)))
430 (while funcs
431 (setq res (funcall (car funcs) link))
432 (if res
433 (setq funcs nil)
434 (unless link (set-match-data data))
435 (setq funcs (cdr funcs))))
436 (when link (set-match-data data))
437 res))
439 (defcustom muse-explicit-link-functions nil
440 "A list of functions to handle an explicit link.
441 An explicit link is one [[like][this]] or [[this]]."
442 :type 'hook
443 :group 'muse)
445 (defun muse-handle-explicit-link (&optional link)
446 "Handle explicit links. If LINK is not specified, look at point.
447 An explicit link is one that looks [[like][this]] or [[this]].
449 This function modifies the match data so that match 1 is the link
450 and match 2 is the description. Perhaps someday match 3 might be
451 the text to use for the alt element of an <a> or <img> tag.
453 The match data is saved. If no handlers are able to process
454 LINK, return LINK (if specified) or the 1st match string. If
455 LINK is not specified, it is assumed that Muse has matched
456 against `muse-explicit-link-regexp' before calling this
457 function."
458 (let ((funcs muse-explicit-link-functions)
459 (res nil))
460 (save-match-data
461 (while funcs
462 (setq res (funcall (car funcs) link))
463 (if res
464 (setq funcs nil)
465 (setq funcs (cdr funcs)))))
466 (muse-link-unescape
467 (if res
469 (or link (match-string 1))))))
471 (provide 'muse)
473 ;;; muse.el ends here