Make second release candidate (3.02.92) available.
[muse-el.git] / lisp / muse.el
blobc6a5afa03514970d3f4240eb096e000f9062dff3
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.92 (3.03 RC2)
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 (defvar muse-version "3.02.92"
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 (defvar muse-update-values-hook nil
71 "Hook for values that are automatically generated.
72 This is to be used by add-on modules for Muse.
73 It is run just before colorizing or publishing a buffer.")
75 ;; Default file extension
77 (eval-when-compile
78 (defvar muse-ignored-extensions))
80 (defvar muse-ignored-extensions-regexp nil
81 "A regexp of extensions to omit from the ending of a Muse page name.
82 This is autogenerated from `muse-ignored-extensions'.")
84 (defun muse-update-file-extension (sym val)
85 "Update the value of `muse-file-extension'."
86 (when (and (featurep 'muse-mode)
87 (boundp sym) (stringp (symbol-value sym))
88 (or (not (stringp val))
89 (not (string= (symbol-value sym) val))))
90 ;; remove old auto-mode-alist association
91 (setq auto-mode-alist
92 (delete (cons (concat "\\." (symbol-value sym) "\\'")
93 'muse-mode-choose-mode)
94 auto-mode-alist)))
95 (set sym val)
96 ;; associate .muse with muse-mode
97 (when (and (featurep 'muse-mode)
98 (stringp val)
99 (or (not (stringp (symbol-value sym)))
100 (not (string= (symbol-value sym) val))))
101 (add-to-list 'auto-mode-alist
102 (cons (concat "\\." val "\\'")
103 'muse-mode-choose-mode)))
104 ;; update the ignored extensions regexp
105 (when (and (fboundp 'muse-update-ignored-extensions-regexp)
106 (or (not (stringp (symbol-value sym)))
107 (not (stringp val))
108 (not (string= (symbol-value sym) val))))
109 (muse-update-ignored-extensions-regexp
110 'muse-ignored-extensions muse-ignored-extensions)))
112 (defcustom muse-file-extension "muse"
113 "File extension of Muse files. Omit the period at the beginning.
114 If you don't want Muse files to have an extension, set this to nil."
115 :type '(choice
116 (const :tag "None" nil)
117 (string))
118 :set 'muse-update-file-extension
119 :group 'muse)
121 (defun muse-update-ignored-extensions-regexp (sym val)
122 "Update the value of `muse-ignored-extensions-regexp'."
123 (set sym val)
124 (if val
125 (setq muse-ignored-extensions-regexp
126 (concat "\\.\\("
127 (regexp-quote (or muse-file-extension "")) "\\|"
128 (mapconcat 'identity val "\\|")
129 "\\)\\'"))
130 (setq muse-ignored-extensions-regexp
131 (if muse-file-extension
132 (concat "\\.\\(" muse-file-extension "\\)\\'")
133 nil))))
135 (add-hook 'muse-update-values-hook
136 (lambda ()
137 (muse-update-ignored-extensions-regexp
138 'muse-ignored-extensions muse-ignored-extensions)))
140 (defcustom muse-ignored-extensions '("bz2" "gz" "[Zz]")
141 "A list of extensions to omit from the ending of a Muse page name.
142 These are regexps.
144 Don't put a period at the beginning of each extension unless you
145 understand that it is part of a regexp."
146 :type '(repeat (regexp :tag "Extension"))
147 :set 'muse-update-ignored-extensions-regexp
148 :group 'muse)
150 (add-to-list 'auto-mode-alist
151 (cons (concat "\\." muse-file-extension "\\'")
152 'muse-mode-choose-mode))
154 (defun muse-update-file-extension-after-init ()
155 ;; This is short, but it has to be a function, otherwise Emacs21
156 ;; does not load it properly when running after-init-hook
157 (muse-update-file-extension 'muse-file-extension muse-file-extension))
159 ;; Once the user's init file has been processed, determine whether
160 ;; they want a file extension
161 (add-hook 'after-init-hook 'muse-update-file-extension-after-init)
163 ;; URL protocols
165 (require 'muse-protocols)
167 ;; Helper functions
169 (defsubst muse-delete-file-if-exists (file)
170 (when (file-exists-p file)
171 (delete-file file)
172 (message "Removed %s" file)))
174 (defsubst muse-time-less-p (t1 t2)
175 "Say whether time T1 is less than time T2."
176 (or (< (car t1) (car t2))
177 (and (= (car t1) (car t2))
178 (< (nth 1 t1) (nth 1 t2)))))
180 (eval-when-compile
181 (defvar muse-publishing-current-file nil))
183 (defun muse-current-file ()
184 "Return the name of the currently visited or published file."
185 (or (and (boundp 'muse-publishing-current-file)
186 muse-publishing-current-file)
187 (buffer-file-name)
188 (concat default-directory (buffer-name))))
190 (defun muse-page-name (&optional name)
191 "Return the canonical form of a Muse page name.
192 All this means is that certain extensions, like .gz, are removed."
193 (save-match-data
194 (unless (and name (not (string= name "")))
195 (setq name (muse-current-file)))
196 (if name
197 (let ((page (file-name-nondirectory name)))
198 (if (and muse-ignored-extensions-regexp
199 (string-match muse-ignored-extensions-regexp page))
200 (replace-match "" t t page)
201 page)))))
203 (defun muse-display-warning (message)
204 "Display the given MESSAGE as a warning."
205 (if (fboundp 'display-warning)
206 (display-warning 'muse message
207 (if (featurep 'xemacs)
208 'warning
209 :warning))
210 (let ((buf (get-buffer-create "*Muse warnings*")))
211 (with-current-buffer buf
212 (goto-char (point-max))
213 (insert "Warning (muse): " message)
214 (unless (bolp)
215 (newline)))
216 (display-buffer buf)
217 (sit-for 0))))
219 (defun muse-eval-lisp (form)
220 "Evaluate the given form and return the result as a string."
221 (require 'pp)
222 (save-match-data
223 (condition-case err
224 (let ((object (eval (read form))))
225 (cond
226 ((stringp object) object)
227 ((and (listp object)
228 (not (eq object nil)))
229 (let ((string (pp-to-string object)))
230 (substring string 0 (1- (length string)))))
231 ((numberp object)
232 (number-to-string object))
233 ((eq object nil) "")
235 (pp-to-string object))))
236 (error
237 (muse-display-warning (format "%s: Error evaluating %s: %s"
238 (muse-page-name) form err))
239 "; INVALID LISP CODE"))))
241 (defmacro muse-with-temp-buffer (&rest body)
242 "Create a temporary buffer, and evaluate BODY there like `progn'.
243 See also `with-temp-file' and `with-output-to-string'.
245 Unlike `with-temp-buffer', this will never attempt to save the temp buffer.
246 It is meant to be used along with `insert-file-contents'."
247 (let ((temp-buffer (make-symbol "temp-buffer")))
248 `(let ((,temp-buffer (generate-new-buffer " *muse-temp*")))
249 (unwind-protect
250 (if debug-on-error
251 (with-current-buffer ,temp-buffer
252 ,@body)
253 (condition-case err
254 (with-current-buffer ,temp-buffer
255 ,@body)
256 (error
257 (if (and (boundp 'muse-batch-publishing-p)
258 muse-batch-publishing-p)
259 (progn
260 (message "%s: Error occured: %s"
261 (muse-page-name) err)
262 (backtrace))
263 (muse-display-warning
264 (format (concat "An error occurred while publishing"
265 " %s: %s\n\nSet debug-on-error to"
266 " `t' if you would like a backtrace.")
267 (muse-page-name) err))))))
268 (when (buffer-live-p ,temp-buffer)
269 (with-current-buffer ,temp-buffer
270 (set-buffer-modified-p nil))
271 (unless debug-on-error (kill-buffer ,temp-buffer)))))))
273 (put 'muse-with-temp-buffer 'lisp-indent-function 0)
274 (put 'muse-with-temp-buffer 'edebug-form-spec '(body))
276 (defun muse-collect-alist (list element &optional test)
277 "Collect items from LIST whose car is equal to ELEMENT.
278 If TEST is specified, use it to compare ELEMENT."
279 (unless test (setq test 'equal))
280 (let ((items nil))
281 (dolist (item list)
282 (when (funcall test element (car item))
283 (setq items (cons item items))))
284 items))
286 (defmacro muse-sort-with-closure (list predicate closure)
287 "Sort LIST, stably, comparing elements using PREDICATE.
288 Returns the sorted list. LIST is modified by side effects.
289 PREDICATE is called with two elements of list and CLOSURE.
290 PREDICATE should return non-nil if the first element should sort
291 before the second."
292 `(sort ,list (lambda (a b) (funcall ,predicate a b ,closure))))
294 (put 'muse-sort-with-closure 'lisp-indent-function 0)
295 (put 'muse-sort-with-closure 'edebug-form-spec '(form function-form form))
297 (defun muse-sort-by-rating (rated-list &optional test)
298 "Sort RATED-LIST according to the rating of each element.
299 The rating is stripped out in the returned list.
300 Default sorting is highest-first.
302 If TEST if specified, use it to sort the list."
303 (unless test (setq test '>))
304 (mapcar (function cdr)
305 (muse-sort-with-closure
306 rated-list
307 (lambda (a b closure)
308 (let ((na (numberp (car a)))
309 (nb (numberp (car b))))
310 (cond ((and na nb) (funcall closure (car a) (car b)))
311 (na (not nb))
312 (t nil))))
313 test)))
315 (defun muse-escape-specials-in-string (specials string &optional reverse)
316 "Apply the transformations in SPECIALS to STRING.
318 The transforms should form a fully reversible and non-ambiguous
319 syntax when STRING is parsed from left to right.
321 If REVERSE is specified, reverse an already-escaped string."
322 (let ((rules (mapcar (lambda (rule)
323 (cons (regexp-quote (if reverse
324 (cdr rule)
325 (car rule)))
326 (if reverse (car rule) (cdr rule))))
327 specials)))
328 (with-temp-buffer
329 (insert string)
330 (goto-char (point-min))
331 (save-match-data
332 (while (not (eobp))
333 (unless (catch 'found
334 (dolist (rule rules)
335 (when (looking-at (car rule))
336 (replace-match (cdr rule) t t)
337 (throw 'found t))))
338 (forward-char))))
339 (buffer-string))))
341 (defun muse-trim-whitespace (string)
342 "Return a version of STRING with no initial nor trailing whitespace."
343 (muse-replace-regexp-in-string
344 (concat "\\`[" muse-regexp-blank "]+\\|[" muse-regexp-blank "]+\\'")
345 "" string))
347 ;; The following code was extracted from cl
349 (defun muse-const-expr-p (x)
350 (cond ((consp x)
351 (or (eq (car x) 'quote)
352 (and (memq (car x) '(function function*))
353 (or (symbolp (nth 1 x))
354 (and (eq (and (consp (nth 1 x))
355 (car (nth 1 x))) 'lambda) 'func)))))
356 ((symbolp x) (and (memq x '(nil t)) t))
357 (t t)))
359 (put 'muse-assertion-failed 'error-conditions '(error))
360 (put 'muse-assertion-failed 'error-message "Assertion failed")
362 (defun muse-list* (arg &rest rest)
363 "Return a new list with specified args as elements, cons'd to last arg.
364 Thus, `(list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to
365 `(cons A (cons B (cons C D)))'."
366 (cond ((not rest) arg)
367 ((not (cdr rest)) (cons arg (car rest)))
368 (t (let* ((n (length rest))
369 (copy (copy-sequence rest))
370 (last (nthcdr (- n 2) copy)))
371 (setcdr last (car (cdr last)))
372 (cons arg copy)))))
374 (defmacro muse-assert (form &optional show-args string &rest args)
375 "Verify that FORM returns non-nil; signal an error if not.
376 Second arg SHOW-ARGS means to include arguments of FORM in message.
377 Other args STRING and ARGS... are arguments to be passed to `error'.
378 They are not evaluated unless the assertion fails. If STRING is
379 omitted, a default message listing FORM itself is used."
380 (let ((sargs
381 (and show-args
382 (delq nil (mapcar
383 (function
384 (lambda (x)
385 (and (not (muse-const-expr-p x)) x)))
386 (cdr form))))))
387 (list 'progn
388 (list 'or form
389 (if string
390 (muse-list* 'error string (append sargs args))
391 (list 'signal '(quote muse-assertion-failed)
392 (muse-list* 'list (list 'quote form) sargs))))
393 nil)))
395 ;; Compatibility functions
397 (if (fboundp 'looking-back)
398 (defalias 'muse-looking-back 'looking-back)
399 (defun muse-looking-back (regexp &optional limit &rest ignored)
400 (save-excursion
401 (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t))))
403 (if (fboundp 'line-end-position)
404 (defalias 'muse-line-end-position 'line-end-position)
405 (defun muse-line-end-position (&optional n)
406 (save-excursion (end-of-line n) (point))))
408 (if (fboundp 'line-beginning-position)
409 (defalias 'muse-line-beginning-position 'line-beginning-position)
410 (defun muse-line-beginning-position (&optional n)
411 (save-excursion (beginning-of-line n) (point))))
413 (eval-and-compile
414 (if (fboundp 'match-string-no-properties)
415 (defalias 'muse-match-string-no-properties 'match-string-no-properties)
416 (defun muse-match-string-no-properties (num &optional string)
417 (match-string num string))))
419 (defun muse-replace-regexp-in-string (regexp replacement text &optional fixedcase literal)
420 "Replace REGEXP with REPLACEMENT in TEXT.
422 Return a new string containing the replacements.
424 If fourth arg FIXEDCASE is non-nil, do not alter case of replacement text.
425 If fifth arg LITERAL is non-nil, insert REPLACEMENT literally."
426 (cond
427 ((fboundp 'replace-in-string)
428 (replace-in-string text regexp replacement literal))
429 ((fboundp 'replace-regexp-in-string)
430 (replace-regexp-in-string regexp replacement text fixedcase literal))
431 (t (let ((repl-len (length replacement))
432 start)
433 (save-match-data
434 (while (setq start (string-match regexp text start))
435 (setq start (+ start repl-len)
436 text (replace-match replacement fixedcase literal text)))))
437 text)))
439 (if (fboundp 'add-to-invisibility-spec)
440 (defalias 'muse-add-to-invisibility-spec 'add-to-invisibility-spec)
441 (defun muse-add-to-invisibility-spec (element)
442 "Add ELEMENT to `buffer-invisibility-spec'.
443 See documentation for `buffer-invisibility-spec' for the kind of elements
444 that can be added."
445 (if (eq buffer-invisibility-spec t)
446 (setq buffer-invisibility-spec (list t)))
447 (setq buffer-invisibility-spec
448 (cons element buffer-invisibility-spec))))
450 (if (fboundp 'read-directory-name)
451 (defalias 'muse-read-directory-name 'read-directory-name)
452 (defun muse-read-directory-name (prompt &optional dir default-dirname mustmatch initial)
453 "Read directory name - see `read-file-name' for details."
454 (unless dir
455 (setq dir default-directory))
456 (read-file-name prompt dir (or default-dirname
457 (if initial (expand-file-name initial dir)
458 dir))
459 mustmatch initial)))
461 (defun muse-file-remote-p (file)
462 "Test whether FILE specifies a location on a remote system.
463 Return non-nil if the location is indeed remote.
465 For example, the filename \"/user@host:/foo\" specifies a location
466 on the system \"/user@host:\"."
467 (cond ((fboundp 'file-remote-p)
468 (file-remote-p file))
469 ((fboundp 'tramp-handle-file-remote-p)
470 (tramp-handle-file-remote-p file))
471 ((and (boundp 'ange-ftp-name-format)
472 (string-match ange-ftp-name-format file))
474 (t nil)))
476 ;; Set face globally in a predictable fashion
477 (defun muse-copy-face (old new)
478 "Copy face OLD to NEW."
479 (if (featurep 'xemacs)
480 (copy-face old new 'all)
481 (copy-face old new)))
483 ;; Widget compatibility functions
485 (defun muse-widget-type-value-create (widget)
486 "Convert and instantiate the value of the :type attribute of WIDGET.
487 Store the newly created widget in the :children attribute.
489 The value of the :type attribute should be an unconverted widget type."
490 (let ((value (widget-get widget :value))
491 (type (widget-get widget :type)))
492 (widget-put widget :children
493 (list (widget-create-child-value widget
494 (widget-convert type)
495 value)))))
497 (defun muse-widget-child-value-get (widget)
498 "Get the value of the first member of :children in WIDGET."
499 (widget-value (car (widget-get widget :children))))
501 (defun muse-widget-type-match (widget value)
502 "Non-nil if the :type value of WIDGET matches VALUE.
504 The value of the :type attribute should be an unconverted widget type."
505 (widget-apply (widget-convert (widget-get widget :type)) :match value))
507 ;; Link-handling functions and variables
509 (defun muse-get-link (&optional target)
510 "Based on the match data, retrieve the link.
511 Use TARGET to get the string, if it is specified."
512 (muse-match-string-no-properties 1 target))
514 (defun muse-get-link-desc (&optional target)
515 "Based on the match data, retrieve the link description.
516 Use TARGET to get the string, if it is specified."
517 (muse-match-string-no-properties 2 target))
519 (defvar muse-link-specials
520 '(("[" . "%5B")
521 ("]" . "%5D")
522 ("%" . "%%"))
523 "Syntax used for escaping and unescaping links.
524 This allows brackets to occur in explicit links as long as you
525 use the standard Muse functions to create them.")
527 (defun muse-link-escape (text)
528 "Escape characters in TEXT that conflict with the explicit link
529 regexp."
530 (when (stringp text)
531 (muse-escape-specials-in-string muse-link-specials text)))
533 (defun muse-link-unescape (text)
534 "Un-escape characters in TEXT that conflict with the explicit
535 link regexp."
536 (when (stringp text)
537 (muse-escape-specials-in-string muse-link-specials text t)))
539 (defun muse-handle-url (&optional string)
540 "If STRING or point has a URL, match and return it."
541 (if (if string (string-match muse-url-regexp string)
542 (looking-at muse-url-regexp))
543 (match-string 0 string)))
545 (defcustom muse-implicit-link-functions '(muse-handle-url)
546 "A list of functions to handle an implicit link.
547 An implicit link is one that is not surrounded by brackets.
549 By default, Muse handles URLs only.
550 If you want to handle WikiWords, load muse-wiki.el."
551 :type 'hook
552 :options '(muse-handle-url)
553 :group 'muse)
555 (defun muse-handle-implicit-link (&optional link)
556 "Handle implicit links. If LINK is not specified, look at point.
557 An implicit link is one that is not surrounded by brackets.
558 By default, Muse handles URLs only.
559 If you want to handle WikiWords, load muse-wiki.el.
561 This function modifies the match data so that match 1 is the
562 link. Match 2 will usually be nil, unless the description is
563 embedded in the text of the buffer.
565 The match data is restored after each unsuccessful handler
566 function call. If LINK is specified, only restore at very end.
568 This behavior is needed because the part of the buffer that
569 `muse-implicit-link-regexp' matches must be narrowed to the part
570 that is an accepted link."
571 (let ((funcs muse-implicit-link-functions)
572 (res nil)
573 (data (match-data t)))
574 (while funcs
575 (setq res (funcall (car funcs) link))
576 (if res
577 (setq funcs nil)
578 (unless link (set-match-data data))
579 (setq funcs (cdr funcs))))
580 (when link (set-match-data data))
581 res))
583 (defcustom muse-explicit-link-functions nil
584 "A list of functions to handle an explicit link.
585 An explicit link is one [[like][this]] or [[this]]."
586 :type 'hook
587 :group 'muse)
589 (defun muse-handle-explicit-link (&optional link)
590 "Handle explicit links. If LINK is not specified, look at point.
591 An explicit link is one that looks [[like][this]] or [[this]].
593 This function modifies the match data so that match 1 is the link
594 and match 2 is the description. Perhaps someday match 3 might be
595 the text to use for the alt element of an <a> or <img> tag.
597 The match data is saved. If no handlers are able to process
598 LINK, return LINK (if specified) or the 1st match string. If
599 LINK is not specified, it is assumed that Muse has matched
600 against `muse-explicit-link-regexp' before calling this
601 function."
602 (let ((funcs muse-explicit-link-functions)
603 (res nil))
604 (save-match-data
605 (while funcs
606 (setq res (funcall (car funcs) link))
607 (if res
608 (setq funcs nil)
609 (setq funcs (cdr funcs)))))
610 (muse-link-unescape
611 (if res
613 (or link (muse-get-link))))))
615 ;; Movement functions
617 (defun muse-list-item-type (str)
618 "Determine the type of list given STR.
619 Returns either 'ul, 'ol, 'dl-term, or 'dl-entry."
620 (cond ((or (string= str "")
621 (< (length str) 2))
622 nil)
623 ((and (= (aref str 0) ?\ )
624 (= (aref str 1) ?-))
625 'ul)
626 ((save-match-data
627 (string-match (concat "\\`[" muse-regexp-blank "][0-9]+\\.") str))
628 'ol)
629 ((save-match-data
630 (not (string-match (concat "\\`[" muse-regexp-blank "]*::") str)))
631 ;; if str is not any kind of list, it will be interpreted as
632 ;; a dl-term
633 'dl-term)
634 (t 'dl-entry)))
636 (defun muse-list-item-critical-point (&optional offset)
637 "Figure out where the important markup character for the
638 currently-matched list item is.
640 If OFFSET is specified, it is the number of groupings outside of
641 the contents of `muse-list-item-regexp'."
642 (unless offset (setq offset 0))
643 (if (match-end (+ offset 2))
644 ;; at a definition list
645 (match-end (+ offset 2))
646 ;; at a different kind of list
647 (match-beginning (+ offset 1))))
649 (defun muse-forward-paragraph (&optional pattern)
650 "Move forward safely by one paragraph, or according to PATTERN."
651 (when (get-text-property (point) 'end-list)
652 (goto-char (next-single-property-change (point) 'end-list)))
653 (setq pattern (if pattern
654 (concat "^\\(?:" pattern "\\|\n\\|\\'\\)")
655 "^\\s-*\\(\n\\|\\'\\)"))
656 (let ((next-list-end (or (next-single-property-change (point) 'end-list)
657 (point-max))))
658 (forward-line 1)
659 (if (re-search-forward pattern nil t)
660 (goto-char (match-beginning 0))
661 (goto-char (point-max)))
662 (when (> (point) next-list-end)
663 (goto-char next-list-end))))
665 (defun muse-forward-list-item-1 (type empty-line indented-line)
666 "Determine whether a nested list item is after point."
667 (if (match-beginning 1)
668 ;; if we are given a dl entry, skip past everything on the same
669 ;; level, except for other dl entries
670 (and (eq type 'dl-entry)
671 (not (eq (char-after (match-beginning 2)) ?\:)))
672 ;; blank line encountered with no list item on the same
673 ;; level after it
674 (let ((beg (point)))
675 (forward-line 1)
676 (if (save-match-data
677 (and (looking-at indented-line)
678 (not (looking-at empty-line))))
679 ;; found that this blank line is followed by some
680 ;; indentation, plus other text, so we'll keep
681 ;; going
683 (goto-char beg)
684 nil))))
686 (defun muse-forward-list-item (type indent &optional no-skip-nested)
687 "Move forward to the next item of TYPE.
688 Return non-nil if successful, nil otherwise.
689 The beginning indentation is given by INDENT.
691 If NO-SKIP-NESTED is non-nil, do not skip past nested items.
692 Note that if you desire this behavior, you will also need to
693 provide a very liberal INDENT value, such as
694 \(concat \"[\" muse-regexp-blank \"]*\")."
695 (let* ((list-item (format muse-list-item-regexp indent))
696 (empty-line (concat "^[" muse-regexp-blank "]*\n"))
697 (indented-line (concat "^" indent "[" muse-regexp-blank "]"))
698 (list-pattern (concat "\\(?:" empty-line "\\)?"
699 "\\(" list-item "\\)")))
700 (while (progn
701 (muse-forward-paragraph list-pattern)
702 ;; make sure we don't go past boundary
703 (and (not (or (get-text-property (point) 'end-list)
704 (>= (point) (point-max))))
705 ;; move past markup that is part of another construct
706 (or (and (match-beginning 1)
707 (or (get-text-property
708 (muse-list-item-critical-point 1) 'muse-link)
709 (get-text-property
710 (muse-list-item-critical-point 1) 'face)))
711 ;; skip nested items
712 (and (not no-skip-nested)
713 (muse-forward-list-item-1 type empty-line
714 indented-line))))))
715 (cond ((or (get-text-property (point) 'end-list)
716 (>= (point) (point-max)))
717 ;; at a list boundary, so stop
718 nil)
719 ((and (match-string 2)
720 (eq type (muse-list-item-type (match-string 2))))
721 ;; same type, so indicate that there are more items to be
722 ;; parsed
723 (goto-char (match-beginning 1)))
725 (when (match-beginning 1)
726 (goto-char (match-beginning 1)))
727 ;; move to just before foreign list item markup
728 nil))))
730 (provide 'muse)
732 ;;; muse.el ends here