Put movement by list and paragraph functions in muse.el.
[muse-el.git] / lisp / muse.el
blob4bfde98ea825adb6d490a86b62cfd4d141f7fc4b
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.6
8 ;; Date: Sat 12-Jan-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/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.6"
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-display-warning (message)
170 "Display the given MESSAGE as a warning."
171 (if (fboundp 'display-warning)
172 (display-warning 'muse message
173 (if (featurep 'xemacs)
174 'warning
175 :warning))
176 (message message)))
178 (defun muse-eval-lisp (form)
179 "Evaluate the given form and return the result as a string."
180 (require 'pp)
181 (save-match-data
182 (condition-case err
183 (let ((object (eval (read form))))
184 (cond
185 ((stringp object) object)
186 ((and (listp object)
187 (not (eq object nil)))
188 (let ((string (pp-to-string object)))
189 (substring string 0 (1- (length string)))))
190 ((numberp object)
191 (number-to-string object))
192 ((eq object nil) "")
194 (pp-to-string object))))
195 (error
196 (muse-display-warning (format "%s: Error evaluating %s: %s"
197 (muse-page-name) form err))
198 "; INVALID LISP CODE"))))
200 (defmacro muse-with-temp-buffer (&rest body)
201 "Create a temporary buffer, and evaluate BODY there like `progn'.
202 See also `with-temp-file' and `with-output-to-string'.
203 Unlike `with-temp-buffer', this will never attempt to save the temp buffer.
204 It is meant to be used along with `insert-file-contents'."
205 (let ((temp-buffer (make-symbol "temp-buffer")))
206 `(let ((,temp-buffer (generate-new-buffer " *muse-temp*")))
207 (unwind-protect
208 (if debug-on-error
209 (with-current-buffer ,temp-buffer
210 ,@body)
211 (condition-case err
212 (with-current-buffer ,temp-buffer
213 ,@body)
214 (error
215 (if (and (boundp 'muse-batch-publishing-p)
216 muse-batch-publishing-p)
217 (progn
218 (message "%s: Error occured: %s"
219 (muse-page-name) err)
220 (backtrace))
221 (muse-display-warning
222 (format (concat "An error occurred while publishing"
223 " %s: %s\n\nSet debug-on-error to"
224 " `t' if you would like a backtrace.")
225 (muse-page-name) err))))))
226 (when (buffer-live-p ,temp-buffer)
227 (with-current-buffer ,temp-buffer
228 (set-buffer-modified-p nil))
229 (unless debug-on-error (kill-buffer ,temp-buffer)))))))
230 (put 'muse-with-temp-buffer 'lisp-indent-function 0)
231 (put 'muse-with-temp-buffer 'edebug-form-spec '(body))
233 ;; The following code was extracted from cl
235 (defun muse-const-expr-p (x)
236 (cond ((consp x)
237 (or (eq (car x) 'quote)
238 (and (memq (car x) '(function function*))
239 (or (symbolp (nth 1 x))
240 (and (eq (and (consp (nth 1 x))
241 (car (nth 1 x))) 'lambda) 'func)))))
242 ((symbolp x) (and (memq x '(nil t)) t))
243 (t t)))
245 (put 'muse-assertion-failed 'error-conditions '(error))
246 (put 'muse-assertion-failed 'error-message "Assertion failed")
248 (defun muse-list* (arg &rest rest)
249 "Return a new list with specified args as elements, cons'd to last arg.
250 Thus, `(list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to
251 `(cons A (cons B (cons C D)))'."
252 (cond ((not rest) arg)
253 ((not (cdr rest)) (cons arg (car rest)))
254 (t (let* ((n (length rest))
255 (copy (copy-sequence rest))
256 (last (nthcdr (- n 2) copy)))
257 (setcdr last (car (cdr last)))
258 (cons arg copy)))))
260 (defmacro muse-assert (form &optional show-args string &rest args)
261 "Verify that FORM returns non-nil; signal an error if not.
262 Second arg SHOW-ARGS means to include arguments of FORM in message.
263 Other args STRING and ARGS... are arguments to be passed to `error'.
264 They are not evaluated unless the assertion fails. If STRING is
265 omitted, a default message listing FORM itself is used."
266 (let ((sargs
267 (and show-args
268 (delq nil (mapcar
269 (function
270 (lambda (x)
271 (and (not (muse-const-expr-p x)) x)))
272 (cdr form))))))
273 (list 'progn
274 (list 'or form
275 (if string
276 (muse-list* 'error string (append sargs args))
277 (list 'signal '(quote muse-assertion-failed)
278 (muse-list* 'list (list 'quote form) sargs))))
279 nil)))
281 ;; Compatibility functions
283 (defun muse-looking-back (regexp &optional limit)
284 (if (fboundp 'looking-back)
285 (looking-back regexp limit)
286 (save-excursion
287 (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t))))
289 (defun muse-line-end-position (&optional n)
290 (if (fboundp 'line-end-position)
291 (line-end-position n)
292 (save-excursion (end-of-line n) (point))))
294 (defun muse-line-beginning-position (&optional n)
295 (if (fboundp 'line-beginning-position)
296 (line-beginning-position n)
297 (save-excursion (beginning-of-line n) (point))))
299 (defun muse-match-string-no-properties (num &optional string)
300 (if (fboundp 'match-string-no-properties)
301 (match-string-no-properties num string)
302 (match-string num string)))
304 (defun muse-replace-regexp-in-string (regexp replacement text &optional fixedcase literal)
305 "Replace REGEXP with REPLACEMENT in TEXT.
306 If fourth arg FIXEDCASE is non-nil, do not alter case of replacement text.
307 If fifth arg LITERAL is non-nil, insert REPLACEMENT literally."
308 (cond
309 ((fboundp 'replace-regexp-in-string)
310 (replace-regexp-in-string regexp replacement text fixedcase literal))
311 ((fboundp 'replace-in-string)
312 (replace-in-string text regexp replacement literal))
313 (t (let ((repl-len (length replacement))
314 start)
315 (while (setq start (string-match regexp text start))
316 (setq start (+ start repl-len)
317 text (replace-match replacement fixedcase literal text))))
318 text)))
320 (defun muse-add-to-invisibility-spec (element)
321 "Add ELEMENT to `buffer-invisibility-spec'.
322 See documentation for `buffer-invisibility-spec' for the kind of elements
323 that can be added."
324 (if (fboundp 'add-to-invisibility-spec)
325 (add-to-invisibility-spec element)
326 (if (eq buffer-invisibility-spec t)
327 (setq buffer-invisibility-spec (list t)))
328 (setq buffer-invisibility-spec
329 (cons element buffer-invisibility-spec))))
331 (defun muse-read-directory-name (prompt &optional dir default-dirname mustmatch initial)
332 "Read directory name - see `read-file-name' for details."
333 (if (fboundp 'read-directory-name)
334 (read-directory-name prompt dir default-dirname mustmatch initial)
335 (unless dir
336 (setq dir default-directory))
337 (read-file-name prompt dir (or default-dirname
338 (if initial (expand-file-name initial dir)
339 dir))
340 mustmatch initial)))
342 ;; Widget compatibility functions
344 (defun muse-widget-type-value-create (widget)
345 "Convert and instantiate the value of the :type attribute of WIDGET.
346 Store the newly created widget in the :children attribute.
348 The value of the :type attribute should be an unconverted widget type."
349 (let ((value (widget-get widget :value))
350 (type (widget-get widget :type)))
351 (widget-put widget :children
352 (list (widget-create-child-value widget
353 (widget-convert type)
354 value)))))
356 (defun muse-widget-child-value-get (widget)
357 "Get the value of the first member of :children in WIDGET."
358 (widget-value (car (widget-get widget :children))))
360 (defun muse-widget-type-match (widget value)
361 "Non-nil if the :type value of WIDGET matches VALUE.
363 The value of the :type attribute should be an unconverted widget type."
364 (widget-apply (widget-convert (widget-get widget :type)) :match value))
366 ;; Link-handling functions and variables
368 (defun muse-link-escape (text)
369 "Escape characters in TEXT that conflict with the explicit link
370 regexp."
371 (if text
372 (progn
373 (muse-replace-regexp-in-string "\\[" "%5B" text t t)
374 (muse-replace-regexp-in-string "\\]" "%5D" text t t)
375 text)
376 ""))
378 (defun muse-link-unescape (text)
379 "Un-escape characters in TEXT that conflict with the explicit
380 link regexp."
381 (if text
382 (progn
383 (muse-replace-regexp-in-string "%5B" "[" text t t)
384 (muse-replace-regexp-in-string "%5D" "]" text t t)
385 text)
386 ""))
388 (defun muse-handle-url (&optional string)
389 "If STRING or point has a URL, match and return it."
390 (if (if string (string-match muse-url-regexp string)
391 (looking-at muse-url-regexp))
392 (match-string 0 string)))
394 (defcustom muse-implicit-link-functions '(muse-handle-url)
395 "A list of functions to handle an implicit link.
396 An implicit link is one that is not surrounded by brackets.
398 By default, Muse handles URLs only.
399 If you want to handle WikiWords, load muse-wiki.el."
400 :type 'hook
401 :options '(muse-handle-url)
402 :group 'muse)
404 (defun muse-handle-implicit-link (&optional link)
405 "Handle implicit links. If LINK is not specified, look at point.
406 An implicit link is one that is not surrounded by brackets.
407 By default, Muse handles URLs only.
408 If you want to handle WikiWords, load muse-wiki.el.
410 This function modifies the match data so that match 1 is the
411 link. Match 2 will usually be nil, unless the description is
412 embedded in the text of the buffer.
414 The match data is restored after each unsuccessful handler
415 function call. If LINK is specified, only restore at very end.
417 This behavior is needed because the part of the buffer that
418 `muse-implicit-link-regexp' matches must be narrowed to the part
419 that is an accepted link."
420 (let ((funcs muse-implicit-link-functions)
421 (res nil)
422 (data (match-data t)))
423 (while funcs
424 (setq res (funcall (car funcs) link))
425 (if res
426 (setq funcs nil)
427 (unless link (set-match-data data))
428 (setq funcs (cdr funcs))))
429 (when link (set-match-data data))
430 res))
432 (defcustom muse-explicit-link-functions nil
433 "A list of functions to handle an explicit link.
434 An explicit link is one [[like][this]] or [[this]]."
435 :type 'hook
436 :group 'muse)
438 (defun muse-handle-explicit-link (&optional link)
439 "Handle explicit links. If LINK is not specified, look at point.
440 An explicit link is one that looks [[like][this]] or [[this]].
442 This function modifies the match data so that match 1 is the link
443 and match 2 is the description. Perhaps someday match 3 might be
444 the text to use for the alt element of an <a> or <img> tag.
446 The match data is saved. If no handlers are able to process
447 LINK, return LINK (if specified) or the 1st match string. If
448 LINK is not specified, it is assumed that Muse has matched
449 against `muse-explicit-link-regexp' before calling this
450 function."
451 (let ((funcs muse-explicit-link-functions)
452 (res nil))
453 (save-match-data
454 (while funcs
455 (setq res (funcall (car funcs) link))
456 (if res
457 (setq funcs nil)
458 (setq funcs (cdr funcs)))))
459 (muse-link-unescape
460 (if res
462 (or link (match-string 1))))))
464 ;; Movement functions
466 (defun muse-list-item-type (str)
467 "Determine the type of list given STR.
468 Returns either 'ul, 'ol, or 'dl."
469 (cond ((or (string= str "")
470 (< (length str) 2))
471 nil)
472 ((and (= (aref str 0) ?\ )
473 (= (aref str 1) ?-))
474 'ul)
475 ((save-match-data
476 (string-match (concat "\\`[" muse-regexp-blank "][0-9]+\\.") str))
477 'ol)
478 (t 'dl)))
480 (defun muse-forward-paragraph (&optional pattern)
481 (when (get-text-property (point) 'end-list)
482 (goto-char (next-single-property-change (point) 'end-list)))
483 (let ((next-list-end (or (next-single-property-change (point) 'end-list)
484 (point-max))))
485 (forward-line 1)
486 (if (re-search-forward (if pattern
487 (concat "^\\(?:" pattern "\\|\n\\|\\'\\)")
488 "^\\s-*\\(\n\\|\\'\\)") nil t)
489 (goto-char (match-beginning 0))
490 (goto-char (point-max)))
491 (when (> (point) next-list-end)
492 (goto-char next-list-end))))
494 (defun muse-forward-dl-term (indent)
495 "Move forward to the next definition list term according to level.
496 Return non-nil if successful, nil otherwise.
497 The beginning indentation is given by INDENT."
498 (let (status)
499 (while (and
500 (setq status
501 (muse-forward-dl-part indent))
502 (string= (match-string 3) ""))
503 (goto-char (match-end 0)))
504 status))
506 (defun muse-forward-dl-entry (indent)
507 "Move forward to the next definition list entry according to level.
508 Return non-nil if successful, nil otherwise.
509 The beginning indentation is given by INDENT."
510 (let (status)
511 (while (and
512 (setq status
513 (muse-forward-dl-part indent t))
514 (not (string= (match-string 3) "")))
515 (goto-char (match-end 0)))
516 status))
518 (defun muse-forward-dl-part (indent &optional entry-p)
519 "Move forward to the next definition list part.
520 Return non-nil if successful, nil otherwise.
521 The beginning indentation is given by INDENT.
523 If ENTRY-P is non-nil, seach ahead by definition list entries.
524 Otherwise, search ahead by definition list terms."
525 (let* ((list-item (format muse-list-item-regexp indent))
526 (empty-line (concat "^[" muse-regexp-blank "]*\n"))
527 (indented-line (concat "^" indent "[" muse-regexp-blank "]"))
528 (list-pattern (concat "\\(?:" empty-line "\\)?"
529 "\\(" list-item "\\)")))
530 (while (progn
531 (muse-forward-paragraph list-pattern)
532 (or (let ((term (match-string 3)))
533 ;; see whether term begins with whitespace - if so,
534 ;; move past it
535 (and term
536 (or entry-p
537 (not (string= term "")))
538 (save-match-data
539 (string-match (concat "\\`[" muse-regexp-blank "]")
540 term))))
541 (when (and (not (match-beginning 1))
542 (not (get-text-property (point) 'end-list))
543 (< (point) (point-max)))
544 ;; blank line encountered with no list item on the
545 ;; same 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
552 ;; some indentation, plus other text, so
553 ;; we'll keep going
555 (goto-char beg)
556 nil))))))
557 (cond ((or (get-text-property (point) 'end-list)
558 (>= (point) (point-max)))
559 ;; at a list boundary, so stop
560 nil)
561 ((match-string 2)
562 ;; move just before next occurrence of list item
563 (goto-char (match-beginning 1))
564 (if (eq 'dl (muse-list-item-type (match-string 2)))
565 ;; if same type, indicate that there are more items to
566 ;; be parsed
568 nil))
570 ;; list item found, but was not a definition list
571 (when (match-beginning 1)
572 (goto-char (match-beginning 1)))
573 nil))))
575 (defun muse-forward-list-item (type indent)
576 "Move forward to the next item of TYPE.
577 Return non-nil if successful, nil otherwise.
578 The beginning indentation is given by INDENT."
579 (let* ((list-item (format muse-list-item-regexp indent))
580 (empty-line (concat "^[" muse-regexp-blank "]*\n"))
581 (indented-line (concat "^" indent "[" muse-regexp-blank "]"))
582 (list-pattern (concat "\\(?:" empty-line "\\)?"
583 "\\(" list-item "\\)")))
584 (while (progn
585 (muse-forward-paragraph list-pattern)
586 (when (and (not (match-beginning 1))
587 (not (get-text-property (point) 'end-list))
588 (< (point) (point-max)))
589 ;; blank line encountered with no list item on the same
590 ;; level after it
591 (let ((beg (point)))
592 (forward-line 1)
593 (if (and (looking-at indented-line)
594 (not (looking-at empty-line)))
595 ;; found that this blank line is followed by some
596 ;; indentation, plus other text, so we'll keep
597 ;; going
599 (goto-char beg)
600 nil)))))
601 (cond ((or (get-text-property (point) 'end-list)
602 (>= (point) (point-max)))
603 ;; at a list boundary, so stop
604 nil)
605 ((and (match-string 2)
606 (eq type (muse-list-item-type (match-string 2))))
607 ;; same type, so indicate that there are more items to be
608 ;; parsed
609 (goto-char (match-beginning 1)))
611 (when (match-beginning 1)
612 (goto-char (match-beginning 1)))
613 ;; move to just before foreign list item markup
614 nil))))
616 (provide 'muse)
618 ;;; muse.el ends here