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