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