muse-html: Make <contents> work with Planner HTML output
[muse-el.git] / lisp / muse.el
blob8d5e5c973a782cfe934155586029e445e62abc0d
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.93 (3.03 RC3)
8 ;; Date: Fri 7-Apr-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/EmacsMuse.html
14 ;; Compatibility: Emacs21 XEmacs21 Emacs22
16 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
18 ;; Emacs Muse is free software; you can redistribute it and/or modify
19 ;; it under the terms of the GNU General Public License as published
20 ;; by the Free Software Foundation; either version 2, or (at your
21 ;; option) any later version.
23 ;; Emacs Muse is distributed in the hope that it will be useful, but
24 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 ;; General Public License for more details.
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with Emacs Muse; 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 ;; Indicate that this version of Muse supports nested tags
49 (provide 'muse-nested-tags)
51 (defvar muse-version "3.02.93"
52 "The version of Muse currently loaded")
54 (defun muse-version (&optional insert)
55 "Display the version of Muse that is currently loaded.
56 If INSERT is non-nil, insert the text instead of displaying it."
57 (interactive "P")
58 (if insert
59 (insert muse-version)
60 (message muse-version)))
62 (defgroup muse nil
63 "Options controlling the behavior of Muse.
64 The markup used by Muse is intended to be very friendly to people
65 familiar with Emacs."
66 :group 'hypermedia)
68 (defvar muse-under-windows-p (memq system-type '(ms-dos windows-nt)))
70 (require 'wid-edit)
71 (require 'muse-regexps)
73 (defvar muse-update-values-hook nil
74 "Hook for values that are automatically generated.
75 This is to be used by add-on modules for Muse.
76 It is run just before colorizing or publishing a buffer.")
78 ;; Default file extension
80 (eval-when-compile
81 (defvar muse-ignored-extensions))
83 (defvar muse-ignored-extensions-regexp nil
84 "A regexp of extensions to omit from the ending of a Muse page name.
85 This is autogenerated from `muse-ignored-extensions'.")
87 (defun muse-update-file-extension (sym val)
88 "Update the value of `muse-file-extension'."
89 (when (and (featurep 'muse-mode)
90 (boundp sym) (stringp (symbol-value sym))
91 (or (not (stringp val))
92 (not (string= (symbol-value sym) val))))
93 ;; remove old auto-mode-alist association
94 (setq auto-mode-alist
95 (delete (cons (concat "\\." (symbol-value sym) "\\'")
96 'muse-mode-choose-mode)
97 auto-mode-alist)))
98 (set sym val)
99 ;; associate .muse with muse-mode
100 (when (and (featurep 'muse-mode)
101 (stringp val)
102 (or (not (stringp (symbol-value sym)))
103 (not (string= (symbol-value sym) val))))
104 (add-to-list 'auto-mode-alist
105 (cons (concat "\\." val "\\'")
106 'muse-mode-choose-mode)))
107 ;; update the ignored extensions regexp
108 (when (and (fboundp 'muse-update-ignored-extensions-regexp)
109 (or (not (stringp (symbol-value sym)))
110 (not (stringp val))
111 (not (string= (symbol-value sym) val))))
112 (muse-update-ignored-extensions-regexp
113 'muse-ignored-extensions muse-ignored-extensions)))
115 (defcustom muse-file-extension "muse"
116 "File extension of Muse files. Omit the period at the beginning.
117 If you don't want Muse files to have an extension, set this to nil."
118 :type '(choice
119 (const :tag "None" nil)
120 (string))
121 :set 'muse-update-file-extension
122 :group 'muse)
124 (defun muse-update-ignored-extensions-regexp (sym val)
125 "Update the value of `muse-ignored-extensions-regexp'."
126 (set sym val)
127 (if val
128 (setq muse-ignored-extensions-regexp
129 (concat "\\.\\("
130 (regexp-quote (or muse-file-extension "")) "\\|"
131 (mapconcat 'identity val "\\|")
132 "\\)\\'"))
133 (setq muse-ignored-extensions-regexp
134 (if muse-file-extension
135 (concat "\\.\\(" muse-file-extension "\\)\\'")
136 nil))))
138 (add-hook 'muse-update-values-hook
139 (lambda ()
140 (muse-update-ignored-extensions-regexp
141 'muse-ignored-extensions muse-ignored-extensions)))
143 (defcustom muse-ignored-extensions '("bz2" "gz" "[Zz]")
144 "A list of extensions to omit from the ending of a Muse page name.
145 These are regexps.
147 Don't put a period at the beginning of each extension unless you
148 understand that it is part of a regexp."
149 :type '(repeat (regexp :tag "Extension"))
150 :set 'muse-update-ignored-extensions-regexp
151 :group 'muse)
153 (add-to-list 'auto-mode-alist
154 (cons (concat "\\." muse-file-extension "\\'")
155 'muse-mode-choose-mode))
157 (defun muse-update-file-extension-after-init ()
158 ;; This is short, but it has to be a function, otherwise Emacs21
159 ;; does not load it properly when running after-init-hook
160 (muse-update-file-extension 'muse-file-extension muse-file-extension))
162 ;; Once the user's init file has been processed, determine whether
163 ;; they want a file extension
164 (add-hook 'after-init-hook 'muse-update-file-extension-after-init)
166 ;; URL protocols
168 (require 'muse-protocols)
170 ;; Helper functions
172 (defsubst muse-delete-file-if-exists (file)
173 (when (file-exists-p file)
174 (delete-file file)
175 (message "Removed %s" file)))
177 (defsubst muse-time-less-p (t1 t2)
178 "Say whether time T1 is less than time T2."
179 (or (< (car t1) (car t2))
180 (and (= (car t1) (car t2))
181 (< (nth 1 t1) (nth 1 t2)))))
183 (eval-when-compile
184 (defvar muse-publishing-current-file nil))
186 (defun muse-current-file ()
187 "Return the name of the currently visited or published file."
188 (or (and (boundp 'muse-publishing-current-file)
189 muse-publishing-current-file)
190 (buffer-file-name)
191 (concat default-directory (buffer-name))))
193 (defun muse-page-name (&optional name)
194 "Return the canonical form of a Muse page name.
195 All this means is that certain extensions, like .gz, are removed."
196 (save-match-data
197 (unless (and name (not (string= name "")))
198 (setq name (muse-current-file)))
199 (if name
200 (let ((page (file-name-nondirectory name)))
201 (if (and muse-ignored-extensions-regexp
202 (string-match muse-ignored-extensions-regexp page))
203 (replace-match "" t t page)
204 page)))))
206 (defun muse-display-warning (message)
207 "Display the given MESSAGE as a warning."
208 (if (fboundp 'display-warning)
209 (display-warning 'muse message
210 (if (featurep 'xemacs)
211 'warning
212 :warning))
213 (let ((buf (get-buffer-create "*Muse warnings*")))
214 (with-current-buffer buf
215 (goto-char (point-max))
216 (insert "Warning (muse): " message)
217 (unless (bolp)
218 (newline)))
219 (display-buffer buf)
220 (sit-for 0))))
222 (defun muse-eval-lisp (form)
223 "Evaluate the given form and return the result as a string."
224 (require 'pp)
225 (save-match-data
226 (condition-case err
227 (let ((object (eval (read form))))
228 (cond
229 ((stringp object) object)
230 ((and (listp object)
231 (not (eq object nil)))
232 (let ((string (pp-to-string object)))
233 (substring string 0 (1- (length string)))))
234 ((numberp object)
235 (number-to-string object))
236 ((eq object nil) "")
238 (pp-to-string object))))
239 (error
240 (muse-display-warning (format "%s: Error evaluating %s: %s"
241 (muse-page-name) form err))
242 "; INVALID LISP CODE"))))
244 (defmacro muse-with-temp-buffer (&rest body)
245 "Create a temporary buffer, and evaluate BODY there like `progn'.
246 See also `with-temp-file' and `with-output-to-string'.
248 Unlike `with-temp-buffer', this will never attempt to save the temp buffer.
249 It is meant to be used along with `insert-file-contents'."
250 (let ((temp-buffer (make-symbol "temp-buffer")))
251 `(let ((,temp-buffer (generate-new-buffer " *muse-temp*")))
252 (unwind-protect
253 (if debug-on-error
254 (with-current-buffer ,temp-buffer
255 ,@body)
256 (condition-case err
257 (with-current-buffer ,temp-buffer
258 ,@body)
259 (error
260 (if (and (boundp 'muse-batch-publishing-p)
261 muse-batch-publishing-p)
262 (progn
263 (message "%s: Error occured: %s"
264 (muse-page-name) err)
265 (backtrace))
266 (muse-display-warning
267 (format (concat "An error occurred while publishing"
268 " %s: %s\n\nSet debug-on-error to"
269 " `t' if you would like a backtrace.")
270 (muse-page-name) err))))))
271 (when (buffer-live-p ,temp-buffer)
272 (with-current-buffer ,temp-buffer
273 (set-buffer-modified-p nil))
274 (unless debug-on-error (kill-buffer ,temp-buffer)))))))
276 (put 'muse-with-temp-buffer 'lisp-indent-function 0)
277 (put 'muse-with-temp-buffer 'edebug-form-spec '(body))
279 (defun muse-collect-alist (list element &optional test)
280 "Collect items from LIST whose car is equal to ELEMENT.
281 If TEST is specified, use it to compare ELEMENT."
282 (unless test (setq test 'equal))
283 (let ((items nil))
284 (dolist (item list)
285 (when (funcall test element (car item))
286 (setq items (cons item items))))
287 items))
289 (defmacro muse-sort-with-closure (list predicate closure)
290 "Sort LIST, stably, comparing elements using PREDICATE.
291 Returns the sorted list. LIST is modified by side effects.
292 PREDICATE is called with two elements of list and CLOSURE.
293 PREDICATE should return non-nil if the first element should sort
294 before the second."
295 `(sort ,list (lambda (a b) (funcall ,predicate a b ,closure))))
297 (put 'muse-sort-with-closure 'lisp-indent-function 0)
298 (put 'muse-sort-with-closure 'edebug-form-spec '(form function-form form))
300 (defun muse-sort-by-rating (rated-list &optional test)
301 "Sort RATED-LIST according to the rating of each element.
302 The rating is stripped out in the returned list.
303 Default sorting is highest-first.
305 If TEST if specified, use it to sort the list."
306 (unless test (setq test '>))
307 (mapcar (function cdr)
308 (muse-sort-with-closure
309 rated-list
310 (lambda (a b closure)
311 (let ((na (numberp (car a)))
312 (nb (numberp (car b))))
313 (cond ((and na nb) (funcall closure (car a) (car b)))
314 (na (not nb))
315 (t nil))))
316 test)))
318 (defun muse-escape-specials-in-string (specials string &optional reverse)
319 "Apply the transformations in SPECIALS to STRING.
321 The transforms should form a fully reversible and non-ambiguous
322 syntax when STRING is parsed from left to right.
324 If REVERSE is specified, reverse an already-escaped string."
325 (let ((rules (mapcar (lambda (rule)
326 (cons (regexp-quote (if reverse
327 (cdr rule)
328 (car rule)))
329 (if reverse (car rule) (cdr rule))))
330 specials)))
331 (with-temp-buffer
332 (insert string)
333 (goto-char (point-min))
334 (save-match-data
335 (while (not (eobp))
336 (unless (catch 'found
337 (dolist (rule rules)
338 (when (looking-at (car rule))
339 (replace-match (cdr rule) t t)
340 (throw 'found t))))
341 (forward-char))))
342 (buffer-string))))
344 (defun muse-trim-whitespace (string)
345 "Return a version of STRING with no initial nor trailing whitespace."
346 (muse-replace-regexp-in-string
347 (concat "\\`[" muse-regexp-blank "]+\\|[" muse-regexp-blank "]+\\'")
348 "" string))
350 (defun muse-path-sans-extension (path)
351 "Return PATH sans final \"extension\".
353 The extension, in a file name, is the part that follows the last `.',
354 except that a leading `.', if any, doesn't count.
356 This differs from `file-name-sans-extension' in that it will
357 never modify the directory part of the path."
358 (concat (file-name-directory path)
359 (file-name-nondirectory (file-name-sans-extension path))))
361 ;; The following code was extracted from cl
363 (defun muse-const-expr-p (x)
364 (cond ((consp x)
365 (or (eq (car x) 'quote)
366 (and (memq (car x) '(function function*))
367 (or (symbolp (nth 1 x))
368 (and (eq (and (consp (nth 1 x))
369 (car (nth 1 x))) 'lambda) 'func)))))
370 ((symbolp x) (and (memq x '(nil t)) t))
371 (t t)))
373 (put 'muse-assertion-failed 'error-conditions '(error))
374 (put 'muse-assertion-failed 'error-message "Assertion failed")
376 (defun muse-list* (arg &rest rest)
377 "Return a new list with specified args as elements, cons'd to last arg.
378 Thus, `(list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to
379 `(cons A (cons B (cons C D)))'."
380 (cond ((not rest) arg)
381 ((not (cdr rest)) (cons arg (car rest)))
382 (t (let* ((n (length rest))
383 (copy (copy-sequence rest))
384 (last (nthcdr (- n 2) copy)))
385 (setcdr last (car (cdr last)))
386 (cons arg copy)))))
388 (defmacro muse-assert (form &optional show-args string &rest args)
389 "Verify that FORM returns non-nil; signal an error if not.
390 Second arg SHOW-ARGS means to include arguments of FORM in message.
391 Other args STRING and ARGS... are arguments to be passed to `error'.
392 They are not evaluated unless the assertion fails. If STRING is
393 omitted, a default message listing FORM itself is used."
394 (let ((sargs
395 (and show-args
396 (delq nil (mapcar
397 (function
398 (lambda (x)
399 (and (not (muse-const-expr-p x)) x)))
400 (cdr form))))))
401 (list 'progn
402 (list 'or form
403 (if string
404 (muse-list* 'error string (append sargs args))
405 (list 'signal '(quote muse-assertion-failed)
406 (muse-list* 'list (list 'quote form) sargs))))
407 nil)))
409 ;; Compatibility functions
411 (if (fboundp 'looking-back)
412 (defalias 'muse-looking-back 'looking-back)
413 (defun muse-looking-back (regexp &optional limit &rest ignored)
414 (save-excursion
415 (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t))))
417 (if (fboundp 'line-end-position)
418 (defalias 'muse-line-end-position 'line-end-position)
419 (defun muse-line-end-position (&optional n)
420 (save-excursion (end-of-line n) (point))))
422 (if (fboundp 'line-beginning-position)
423 (defalias 'muse-line-beginning-position 'line-beginning-position)
424 (defun muse-line-beginning-position (&optional n)
425 (save-excursion (beginning-of-line n) (point))))
427 (eval-and-compile
428 (if (fboundp 'match-string-no-properties)
429 (defalias 'muse-match-string-no-properties 'match-string-no-properties)
430 (defun muse-match-string-no-properties (num &optional string)
431 (match-string num string))))
433 (defun muse-replace-regexp-in-string (regexp replacement text &optional fixedcase literal)
434 "Replace REGEXP with REPLACEMENT in TEXT.
436 Return a new string containing the replacements.
438 If fourth arg FIXEDCASE is non-nil, do not alter case of replacement text.
439 If fifth arg LITERAL is non-nil, insert REPLACEMENT literally."
440 (cond
441 ((fboundp 'replace-in-string)
442 (replace-in-string text regexp replacement literal))
443 ((fboundp 'replace-regexp-in-string)
444 (replace-regexp-in-string regexp replacement text fixedcase literal))
445 (t (let ((repl-len (length replacement))
446 start)
447 (unless (string= regexp "")
448 (save-match-data
449 (while (setq start (string-match regexp text start))
450 (setq start (+ start repl-len)
451 text (replace-match replacement fixedcase literal
452 text))))))
453 text)))
455 (if (fboundp 'add-to-invisibility-spec)
456 (defalias 'muse-add-to-invisibility-spec 'add-to-invisibility-spec)
457 (defun muse-add-to-invisibility-spec (element)
458 "Add ELEMENT to `buffer-invisibility-spec'.
459 See documentation for `buffer-invisibility-spec' for the kind of elements
460 that can be added."
461 (if (eq buffer-invisibility-spec t)
462 (setq buffer-invisibility-spec (list t)))
463 (setq buffer-invisibility-spec
464 (cons element buffer-invisibility-spec))))
466 (if (fboundp 'read-directory-name)
467 (defalias 'muse-read-directory-name 'read-directory-name)
468 (defun muse-read-directory-name (prompt &optional dir default-dirname mustmatch initial)
469 "Read directory name - see `read-file-name' for details."
470 (unless dir
471 (setq dir default-directory))
472 (read-file-name prompt dir (or default-dirname
473 (if initial (expand-file-name initial dir)
474 dir))
475 mustmatch initial)))
477 (defun muse-file-remote-p (file)
478 "Test whether FILE specifies a location on a remote system.
479 Return non-nil if the location is indeed remote.
481 For example, the filename \"/user@host:/foo\" specifies a location
482 on the system \"/user@host:\"."
483 (cond ((fboundp 'file-remote-p)
484 (file-remote-p file))
485 ((fboundp 'tramp-handle-file-remote-p)
486 (tramp-handle-file-remote-p file))
487 ((and (boundp 'ange-ftp-name-format)
488 (string-match ange-ftp-name-format file))
490 (t nil)))
492 ;; Set face globally in a predictable fashion
493 (defun muse-copy-face (old new)
494 "Copy face OLD to NEW."
495 (if (featurep 'xemacs)
496 (copy-face old new 'all)
497 (copy-face old new)))
499 ;; Widget compatibility functions
501 (defun muse-widget-type-value-create (widget)
502 "Convert and instantiate the value of the :type attribute of WIDGET.
503 Store the newly created widget in the :children attribute.
505 The value of the :type attribute should be an unconverted widget type."
506 (let ((value (widget-get widget :value))
507 (type (widget-get widget :type)))
508 (widget-put widget :children
509 (list (widget-create-child-value widget
510 (widget-convert type)
511 value)))))
513 (defun muse-widget-child-value-get (widget)
514 "Get the value of the first member of :children in WIDGET."
515 (widget-value (car (widget-get widget :children))))
517 (defun muse-widget-type-match (widget value)
518 "Non-nil if the :type value of WIDGET matches VALUE.
520 The value of the :type attribute should be an unconverted widget type."
521 (widget-apply (widget-convert (widget-get widget :type)) :match value))
523 ;; Link-handling functions and variables
525 (defun muse-get-link (&optional target)
526 "Based on the match data, retrieve the link.
527 Use TARGET to get the string, if it is specified."
528 (muse-match-string-no-properties 1 target))
530 (defun muse-get-link-desc (&optional target)
531 "Based on the match data, retrieve the link description.
532 Use TARGET to get the string, if it is specified."
533 (muse-match-string-no-properties 2 target))
535 (defvar muse-link-specials
536 '(("[" . "%5B")
537 ("]" . "%5D")
538 ("%" . "%%"))
539 "Syntax used for escaping and unescaping links.
540 This allows brackets to occur in explicit links as long as you
541 use the standard Muse functions to create them.")
543 (defun muse-link-escape (text)
544 "Escape characters in TEXT that conflict with the explicit link
545 regexp."
546 (when (stringp text)
547 (muse-escape-specials-in-string muse-link-specials text)))
549 (defun muse-link-unescape (text)
550 "Un-escape characters in TEXT that conflict with the explicit
551 link regexp."
552 (when (stringp text)
553 (muse-escape-specials-in-string muse-link-specials text t)))
555 (defun muse-handle-url (&optional string)
556 "If STRING or point has a URL, match and return it."
557 (if (if string (string-match muse-url-regexp string)
558 (looking-at muse-url-regexp))
559 (match-string 0 string)))
561 (defcustom muse-implicit-link-functions '(muse-handle-url)
562 "A list of functions to handle an implicit link.
563 An implicit link is one that is not surrounded by brackets.
565 By default, Muse handles URLs only.
566 If you want to handle WikiWords, load muse-wiki.el."
567 :type 'hook
568 :options '(muse-handle-url)
569 :group 'muse)
571 (defun muse-handle-implicit-link (&optional link)
572 "Handle implicit links. If LINK is not specified, look at point.
573 An implicit link is one that is not surrounded by brackets.
574 By default, Muse handles URLs only.
575 If you want to handle WikiWords, load muse-wiki.el.
577 This function modifies the match data so that match 1 is the
578 link. Match 2 will usually be nil, unless the description is
579 embedded in the text of the buffer.
581 The match data is restored after each unsuccessful handler
582 function call. If LINK is specified, only restore at very end.
584 This behavior is needed because the part of the buffer that
585 `muse-implicit-link-regexp' matches must be narrowed to the part
586 that is an accepted link."
587 (let ((funcs muse-implicit-link-functions)
588 (res nil)
589 (data (match-data t)))
590 (while funcs
591 (setq res (funcall (car funcs) link))
592 (if res
593 (setq funcs nil)
594 (unless link (set-match-data data))
595 (setq funcs (cdr funcs))))
596 (when link (set-match-data data))
597 res))
599 (defcustom muse-explicit-link-functions nil
600 "A list of functions to handle an explicit link.
601 An explicit link is one [[like][this]] or [[this]]."
602 :type 'hook
603 :group 'muse)
605 (defun muse-handle-explicit-link (&optional link)
606 "Handle explicit links. If LINK is not specified, look at point.
607 An explicit link is one that looks [[like][this]] or [[this]].
609 This function modifies the match data so that match 1 is the link
610 and match 2 is the description. Perhaps someday match 3 might be
611 the text to use for the alt element of an <a> or <img> tag.
613 The match data is saved. If no handlers are able to process
614 LINK, return LINK (if specified) or the 1st match string. If
615 LINK is not specified, it is assumed that Muse has matched
616 against `muse-explicit-link-regexp' before calling this
617 function."
618 (let ((funcs muse-explicit-link-functions)
619 (res nil))
620 (save-match-data
621 (while funcs
622 (setq res (funcall (car funcs) link))
623 (if res
624 (setq funcs nil)
625 (setq funcs (cdr funcs)))))
626 (muse-link-unescape
627 (if res
629 (or link (muse-get-link))))))
631 ;; Movement functions
633 (defun muse-list-item-type (str)
634 "Determine the type of list given STR.
635 Returns either 'ul, 'ol, 'dl-term, or 'dl-entry."
636 (cond ((or (string= str "")
637 (< (length str) 2))
638 nil)
639 ((and (= (aref str 0) ?\ )
640 (= (aref str 1) ?-))
641 'ul)
642 ((save-match-data
643 (string-match (concat "\\`[" muse-regexp-blank "][0-9]+\\.") str))
644 'ol)
645 ((save-match-data
646 (not (string-match (concat "\\`[" muse-regexp-blank "]*::") str)))
647 ;; if str is not any kind of list, it will be interpreted as
648 ;; a dl-term
649 'dl-term)
650 (t 'dl-entry)))
652 (defun muse-list-item-critical-point (&optional offset)
653 "Figure out where the important markup character for the
654 currently-matched list item is.
656 If OFFSET is specified, it is the number of groupings outside of
657 the contents of `muse-list-item-regexp'."
658 (unless offset (setq offset 0))
659 (if (match-end (+ offset 2))
660 ;; at a definition list
661 (match-end (+ offset 2))
662 ;; at a different kind of list
663 (match-beginning (+ offset 1))))
665 (defun muse-forward-paragraph (&optional pattern)
666 "Move forward safely by one paragraph, or according to PATTERN."
667 (when (get-text-property (point) 'end-list)
668 (goto-char (next-single-property-change (point) 'end-list)))
669 (setq pattern (if pattern
670 (concat "^\\(?:" pattern "\\|\n\\|\\'\\)")
671 "^\\s-*\\(\n\\|\\'\\)"))
672 (let ((next-list-end (or (next-single-property-change (point) 'end-list)
673 (point-max))))
674 (forward-line 1)
675 (if (re-search-forward pattern nil t)
676 (goto-char (match-beginning 0))
677 (goto-char (point-max)))
678 (when (> (point) next-list-end)
679 (goto-char next-list-end))))
681 (defun muse-forward-list-item-1 (type empty-line indented-line)
682 "Determine whether a nested list item is after point."
683 (if (match-beginning 1)
684 ;; if we are given a dl entry, skip past everything on the same
685 ;; level, except for other dl entries
686 (and (eq type 'dl-entry)
687 (not (eq (char-after (match-beginning 2)) ?\:)))
688 ;; blank line encountered with no list item on the same
689 ;; level after it
690 (let ((beg (point)))
691 (forward-line 1)
692 (if (save-match-data
693 (and (looking-at indented-line)
694 (not (looking-at empty-line))))
695 ;; found that this blank line is followed by some
696 ;; indentation, plus other text, so we'll keep
697 ;; going
699 (goto-char beg)
700 nil))))
702 (defun muse-forward-list-item (type indent &optional no-skip-nested)
703 "Move forward to the next item of TYPE.
704 Return non-nil if successful, nil otherwise.
705 The beginning indentation is given by INDENT.
707 If NO-SKIP-NESTED is non-nil, do not skip past nested items.
708 Note that if you desire this behavior, you will also need to
709 provide a very liberal INDENT value, such as
710 \(concat \"[\" muse-regexp-blank \"]*\")."
711 (let* ((list-item (format muse-list-item-regexp indent))
712 (empty-line (concat "^[" muse-regexp-blank "]*\n"))
713 (indented-line (concat "^" indent "[" muse-regexp-blank "]"))
714 (list-pattern (concat "\\(?:" empty-line "\\)?"
715 "\\(" list-item "\\)")))
716 (while (progn
717 (muse-forward-paragraph list-pattern)
718 ;; make sure we don't go past boundary
719 (and (not (or (get-text-property (point) 'end-list)
720 (>= (point) (point-max))))
721 ;; move past markup that is part of another construct
722 (or (and (match-beginning 1)
723 (or (get-text-property
724 (muse-list-item-critical-point 1) 'muse-link)
725 (get-text-property
726 (muse-list-item-critical-point 1) 'face)))
727 ;; skip nested items
728 (and (not no-skip-nested)
729 (muse-forward-list-item-1 type empty-line
730 indented-line))))))
731 (cond ((or (get-text-property (point) 'end-list)
732 (>= (point) (point-max)))
733 ;; at a list boundary, so stop
734 nil)
735 ((and (match-string 2)
736 (eq type (muse-list-item-type (match-string 2))))
737 ;; same type, so indicate that there are more items to be
738 ;; parsed
739 (goto-char (match-beginning 1)))
741 (when (match-beginning 1)
742 (goto-char (match-beginning 1)))
743 ;; move to just before foreign list item markup
744 nil))))
746 (defun muse-goto-tag-end (tag nested)
747 "Move forward past the end of TAG.
749 If NESTED is non-nil, look for other instances of this tag that
750 may be nested inside of this tag, and skip past them."
751 (if (not nested)
752 (search-forward (concat "</" tag ">") nil t)
753 (let ((nesting 1)
754 (tag-regexp (concat "^\\(<\\(/?\\)" tag ">\\)"))
755 (match-found nil))
756 (while (and (> nesting 0)
757 (setq match-found (re-search-forward tag-regexp nil t)))
758 (if (string-equal (match-string 2) "/")
759 (setq nesting (1- nesting))
760 (setq nesting (1+ nesting))))
761 match-found)))
763 (provide 'muse)
765 ;;; muse.el ends here