Make Emacs Muse 3.02.93 (3.03 RC3) available
[muse-el.git] / lisp / muse.el
blob4c4607a8a7e83ecd6dd0f94e4dda44b0b759e2c9
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 (save-match-data
448 (while (setq start (string-match regexp text start))
449 (setq start (+ start repl-len)
450 text (replace-match replacement fixedcase literal text)))))
451 text)))
453 (if (fboundp 'add-to-invisibility-spec)
454 (defalias 'muse-add-to-invisibility-spec 'add-to-invisibility-spec)
455 (defun muse-add-to-invisibility-spec (element)
456 "Add ELEMENT to `buffer-invisibility-spec'.
457 See documentation for `buffer-invisibility-spec' for the kind of elements
458 that can be added."
459 (if (eq buffer-invisibility-spec t)
460 (setq buffer-invisibility-spec (list t)))
461 (setq buffer-invisibility-spec
462 (cons element buffer-invisibility-spec))))
464 (if (fboundp 'read-directory-name)
465 (defalias 'muse-read-directory-name 'read-directory-name)
466 (defun muse-read-directory-name (prompt &optional dir default-dirname mustmatch initial)
467 "Read directory name - see `read-file-name' for details."
468 (unless dir
469 (setq dir default-directory))
470 (read-file-name prompt dir (or default-dirname
471 (if initial (expand-file-name initial dir)
472 dir))
473 mustmatch initial)))
475 (defun muse-file-remote-p (file)
476 "Test whether FILE specifies a location on a remote system.
477 Return non-nil if the location is indeed remote.
479 For example, the filename \"/user@host:/foo\" specifies a location
480 on the system \"/user@host:\"."
481 (cond ((fboundp 'file-remote-p)
482 (file-remote-p file))
483 ((fboundp 'tramp-handle-file-remote-p)
484 (tramp-handle-file-remote-p file))
485 ((and (boundp 'ange-ftp-name-format)
486 (string-match ange-ftp-name-format file))
488 (t nil)))
490 ;; Set face globally in a predictable fashion
491 (defun muse-copy-face (old new)
492 "Copy face OLD to NEW."
493 (if (featurep 'xemacs)
494 (copy-face old new 'all)
495 (copy-face old new)))
497 ;; Widget compatibility functions
499 (defun muse-widget-type-value-create (widget)
500 "Convert and instantiate the value of the :type attribute of WIDGET.
501 Store the newly created widget in the :children attribute.
503 The value of the :type attribute should be an unconverted widget type."
504 (let ((value (widget-get widget :value))
505 (type (widget-get widget :type)))
506 (widget-put widget :children
507 (list (widget-create-child-value widget
508 (widget-convert type)
509 value)))))
511 (defun muse-widget-child-value-get (widget)
512 "Get the value of the first member of :children in WIDGET."
513 (widget-value (car (widget-get widget :children))))
515 (defun muse-widget-type-match (widget value)
516 "Non-nil if the :type value of WIDGET matches VALUE.
518 The value of the :type attribute should be an unconverted widget type."
519 (widget-apply (widget-convert (widget-get widget :type)) :match value))
521 ;; Link-handling functions and variables
523 (defun muse-get-link (&optional target)
524 "Based on the match data, retrieve the link.
525 Use TARGET to get the string, if it is specified."
526 (muse-match-string-no-properties 1 target))
528 (defun muse-get-link-desc (&optional target)
529 "Based on the match data, retrieve the link description.
530 Use TARGET to get the string, if it is specified."
531 (muse-match-string-no-properties 2 target))
533 (defvar muse-link-specials
534 '(("[" . "%5B")
535 ("]" . "%5D")
536 ("%" . "%%"))
537 "Syntax used for escaping and unescaping links.
538 This allows brackets to occur in explicit links as long as you
539 use the standard Muse functions to create them.")
541 (defun muse-link-escape (text)
542 "Escape characters in TEXT that conflict with the explicit link
543 regexp."
544 (when (stringp text)
545 (muse-escape-specials-in-string muse-link-specials text)))
547 (defun muse-link-unescape (text)
548 "Un-escape characters in TEXT that conflict with the explicit
549 link regexp."
550 (when (stringp text)
551 (muse-escape-specials-in-string muse-link-specials text t)))
553 (defun muse-handle-url (&optional string)
554 "If STRING or point has a URL, match and return it."
555 (if (if string (string-match muse-url-regexp string)
556 (looking-at muse-url-regexp))
557 (match-string 0 string)))
559 (defcustom muse-implicit-link-functions '(muse-handle-url)
560 "A list of functions to handle an implicit link.
561 An implicit link is one that is not surrounded by brackets.
563 By default, Muse handles URLs only.
564 If you want to handle WikiWords, load muse-wiki.el."
565 :type 'hook
566 :options '(muse-handle-url)
567 :group 'muse)
569 (defun muse-handle-implicit-link (&optional link)
570 "Handle implicit links. If LINK is not specified, look at point.
571 An implicit link is one that is not surrounded by brackets.
572 By default, Muse handles URLs only.
573 If you want to handle WikiWords, load muse-wiki.el.
575 This function modifies the match data so that match 1 is the
576 link. Match 2 will usually be nil, unless the description is
577 embedded in the text of the buffer.
579 The match data is restored after each unsuccessful handler
580 function call. If LINK is specified, only restore at very end.
582 This behavior is needed because the part of the buffer that
583 `muse-implicit-link-regexp' matches must be narrowed to the part
584 that is an accepted link."
585 (let ((funcs muse-implicit-link-functions)
586 (res nil)
587 (data (match-data t)))
588 (while funcs
589 (setq res (funcall (car funcs) link))
590 (if res
591 (setq funcs nil)
592 (unless link (set-match-data data))
593 (setq funcs (cdr funcs))))
594 (when link (set-match-data data))
595 res))
597 (defcustom muse-explicit-link-functions nil
598 "A list of functions to handle an explicit link.
599 An explicit link is one [[like][this]] or [[this]]."
600 :type 'hook
601 :group 'muse)
603 (defun muse-handle-explicit-link (&optional link)
604 "Handle explicit links. If LINK is not specified, look at point.
605 An explicit link is one that looks [[like][this]] or [[this]].
607 This function modifies the match data so that match 1 is the link
608 and match 2 is the description. Perhaps someday match 3 might be
609 the text to use for the alt element of an <a> or <img> tag.
611 The match data is saved. If no handlers are able to process
612 LINK, return LINK (if specified) or the 1st match string. If
613 LINK is not specified, it is assumed that Muse has matched
614 against `muse-explicit-link-regexp' before calling this
615 function."
616 (let ((funcs muse-explicit-link-functions)
617 (res nil))
618 (save-match-data
619 (while funcs
620 (setq res (funcall (car funcs) link))
621 (if res
622 (setq funcs nil)
623 (setq funcs (cdr funcs)))))
624 (muse-link-unescape
625 (if res
627 (or link (muse-get-link))))))
629 ;; Movement functions
631 (defun muse-list-item-type (str)
632 "Determine the type of list given STR.
633 Returns either 'ul, 'ol, 'dl-term, or 'dl-entry."
634 (cond ((or (string= str "")
635 (< (length str) 2))
636 nil)
637 ((and (= (aref str 0) ?\ )
638 (= (aref str 1) ?-))
639 'ul)
640 ((save-match-data
641 (string-match (concat "\\`[" muse-regexp-blank "][0-9]+\\.") str))
642 'ol)
643 ((save-match-data
644 (not (string-match (concat "\\`[" muse-regexp-blank "]*::") str)))
645 ;; if str is not any kind of list, it will be interpreted as
646 ;; a dl-term
647 'dl-term)
648 (t 'dl-entry)))
650 (defun muse-list-item-critical-point (&optional offset)
651 "Figure out where the important markup character for the
652 currently-matched list item is.
654 If OFFSET is specified, it is the number of groupings outside of
655 the contents of `muse-list-item-regexp'."
656 (unless offset (setq offset 0))
657 (if (match-end (+ offset 2))
658 ;; at a definition list
659 (match-end (+ offset 2))
660 ;; at a different kind of list
661 (match-beginning (+ offset 1))))
663 (defun muse-forward-paragraph (&optional pattern)
664 "Move forward safely by one paragraph, or according to PATTERN."
665 (when (get-text-property (point) 'end-list)
666 (goto-char (next-single-property-change (point) 'end-list)))
667 (setq pattern (if pattern
668 (concat "^\\(?:" pattern "\\|\n\\|\\'\\)")
669 "^\\s-*\\(\n\\|\\'\\)"))
670 (let ((next-list-end (or (next-single-property-change (point) 'end-list)
671 (point-max))))
672 (forward-line 1)
673 (if (re-search-forward pattern nil t)
674 (goto-char (match-beginning 0))
675 (goto-char (point-max)))
676 (when (> (point) next-list-end)
677 (goto-char next-list-end))))
679 (defun muse-forward-list-item-1 (type empty-line indented-line)
680 "Determine whether a nested list item is after point."
681 (if (match-beginning 1)
682 ;; if we are given a dl entry, skip past everything on the same
683 ;; level, except for other dl entries
684 (and (eq type 'dl-entry)
685 (not (eq (char-after (match-beginning 2)) ?\:)))
686 ;; blank line encountered with no list item on the same
687 ;; level after it
688 (let ((beg (point)))
689 (forward-line 1)
690 (if (save-match-data
691 (and (looking-at indented-line)
692 (not (looking-at empty-line))))
693 ;; found that this blank line is followed by some
694 ;; indentation, plus other text, so we'll keep
695 ;; going
697 (goto-char beg)
698 nil))))
700 (defun muse-forward-list-item (type indent &optional no-skip-nested)
701 "Move forward to the next item of TYPE.
702 Return non-nil if successful, nil otherwise.
703 The beginning indentation is given by INDENT.
705 If NO-SKIP-NESTED is non-nil, do not skip past nested items.
706 Note that if you desire this behavior, you will also need to
707 provide a very liberal INDENT value, such as
708 \(concat \"[\" muse-regexp-blank \"]*\")."
709 (let* ((list-item (format muse-list-item-regexp indent))
710 (empty-line (concat "^[" muse-regexp-blank "]*\n"))
711 (indented-line (concat "^" indent "[" muse-regexp-blank "]"))
712 (list-pattern (concat "\\(?:" empty-line "\\)?"
713 "\\(" list-item "\\)")))
714 (while (progn
715 (muse-forward-paragraph list-pattern)
716 ;; make sure we don't go past boundary
717 (and (not (or (get-text-property (point) 'end-list)
718 (>= (point) (point-max))))
719 ;; move past markup that is part of another construct
720 (or (and (match-beginning 1)
721 (or (get-text-property
722 (muse-list-item-critical-point 1) 'muse-link)
723 (get-text-property
724 (muse-list-item-critical-point 1) 'face)))
725 ;; skip nested items
726 (and (not no-skip-nested)
727 (muse-forward-list-item-1 type empty-line
728 indented-line))))))
729 (cond ((or (get-text-property (point) 'end-list)
730 (>= (point) (point-max)))
731 ;; at a list boundary, so stop
732 nil)
733 ((and (match-string 2)
734 (eq type (muse-list-item-type (match-string 2))))
735 ;; same type, so indicate that there are more items to be
736 ;; parsed
737 (goto-char (match-beginning 1)))
739 (when (match-beginning 1)
740 (goto-char (match-beginning 1)))
741 ;; move to just before foreign list item markup
742 nil))))
744 (defun muse-goto-tag-end (tag nested)
745 "Move forward past the end of TAG.
747 If NESTED is non-nil, look for other instances of this tag that
748 may be nested inside of this tag, and skip past them."
749 (if (not nested)
750 (search-forward (concat "</" tag ">") nil t)
751 (let ((nesting 1)
752 (tag-regexp (concat "^\\(<\\(/?\\)" tag ">\\)"))
753 (match-found nil))
754 (while (and (> nesting 0)
755 (setq match-found (re-search-forward tag-regexp nil t)))
756 (if (string-equal (match-string 2) "/")
757 (setq nesting (1- nesting))
758 (setq nesting (1+ nesting))))
759 match-found)))
761 (provide 'muse)
763 ;;; muse.el ends here