No, we really did need the auto-mode-alist part at top-level
[muse-el.git] / lisp / muse.el
blobfa5ceb51b8afe79ae1bb112d4ae69fe74c9efa66
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 ;; 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'."
255 (let ((temp-buffer (make-symbol "temp-buffer")))
256 `(let ((,temp-buffer (generate-new-buffer " *muse-temp*")))
257 (unwind-protect
258 (if debug-on-error
259 (with-current-buffer ,temp-buffer
260 ,@body)
261 (condition-case err
262 (with-current-buffer ,temp-buffer
263 ,@body)
264 (error
265 (if (and (boundp 'muse-batch-publishing-p)
266 muse-batch-publishing-p)
267 (progn
268 (message "%s: Error occured: %s"
269 (muse-page-name) err)
270 (backtrace))
271 (muse-display-warning
272 (format (concat "An error occurred while publishing"
273 " %s: %s\n\nSet debug-on-error to"
274 " `t' if you would like a backtrace.")
275 (muse-page-name) err))))))
276 (when (buffer-live-p ,temp-buffer)
277 (with-current-buffer ,temp-buffer
278 (set-buffer-modified-p nil))
279 (unless debug-on-error (kill-buffer ,temp-buffer)))))))
281 (put 'muse-with-temp-buffer 'lisp-indent-function 0)
282 (put 'muse-with-temp-buffer 'edebug-form-spec '(body))
284 (defun muse-collect-alist (list element &optional test)
285 "Collect items from LIST whose car is equal to ELEMENT.
286 If TEST is specified, use it to compare ELEMENT."
287 (unless test (setq test 'equal))
288 (let ((items nil))
289 (dolist (item list)
290 (when (funcall test element (car item))
291 (setq items (cons item items))))
292 items))
294 (defmacro muse-sort-with-closure (list predicate closure)
295 "Sort LIST, stably, comparing elements using PREDICATE.
296 Returns the sorted list. LIST is modified by side effects.
297 PREDICATE is called with two elements of list and CLOSURE.
298 PREDICATE should return non-nil if the first element should sort
299 before the second."
300 `(sort ,list (lambda (a b) (funcall ,predicate a b ,closure))))
302 (put 'muse-sort-with-closure 'lisp-indent-function 0)
303 (put 'muse-sort-with-closure 'edebug-form-spec '(form function-form form))
305 (defun muse-sort-by-rating (rated-list &optional test)
306 "Sort RATED-LIST according to the rating of each element.
307 The rating is stripped out in the returned list.
308 Default sorting is highest-first.
310 If TEST if specified, use it to sort the list."
311 (unless test (setq test '>))
312 (mapcar (function cdr)
313 (muse-sort-with-closure
314 rated-list
315 (lambda (a b closure)
316 (let ((na (numberp (car a)))
317 (nb (numberp (car b))))
318 (cond ((and na nb) (funcall closure (car a) (car b)))
319 (na (not nb))
320 (t nil))))
321 test)))
323 (defun muse-escape-specials-in-string (specials string &optional reverse)
324 "Apply the transformations in SPECIALS to STRING.
326 The transforms should form a fully reversible and non-ambiguous
327 syntax when STRING is parsed from left to right.
329 If REVERSE is specified, reverse an already-escaped string."
330 (let ((rules (mapcar (lambda (rule)
331 (cons (regexp-quote (if reverse
332 (cdr rule)
333 (car rule)))
334 (if reverse (car rule) (cdr rule))))
335 specials)))
336 (with-temp-buffer
337 (insert string)
338 (goto-char (point-min))
339 (save-match-data
340 (while (not (eobp))
341 (unless (catch 'found
342 (dolist (rule rules)
343 (when (looking-at (car rule))
344 (replace-match (cdr rule) t t)
345 (throw 'found t))))
346 (forward-char))))
347 (buffer-string))))
349 (defun muse-trim-whitespace (string)
350 "Return a version of STRING with no initial nor trailing whitespace."
351 (muse-replace-regexp-in-string
352 (concat "\\`[" muse-regexp-blank "]+\\|[" muse-regexp-blank "]+\\'")
353 "" string))
355 (defun muse-path-sans-extension (path)
356 "Return PATH sans final \"extension\".
358 The extension, in a file name, is the part that follows the last `.',
359 except that a leading `.', if any, doesn't count.
361 This differs from `file-name-sans-extension' in that it will
362 never modify the directory part of the path."
363 (concat (file-name-directory path)
364 (file-name-nondirectory (file-name-sans-extension path))))
366 ;; The following code was extracted from cl
368 (defun muse-const-expr-p (x)
369 (cond ((consp x)
370 (or (eq (car x) 'quote)
371 (and (memq (car x) '(function function*))
372 (or (symbolp (nth 1 x))
373 (and (eq (and (consp (nth 1 x))
374 (car (nth 1 x))) 'lambda) 'func)))))
375 ((symbolp x) (and (memq x '(nil t)) t))
376 (t t)))
378 (put 'muse-assertion-failed 'error-conditions '(error))
379 (put 'muse-assertion-failed 'error-message "Assertion failed")
381 (defun muse-list* (arg &rest rest)
382 "Return a new list with specified args as elements, cons'd to last arg.
383 Thus, `(list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to
384 `(cons A (cons B (cons C D)))'."
385 (cond ((not rest) arg)
386 ((not (cdr rest)) (cons arg (car rest)))
387 (t (let* ((n (length rest))
388 (copy (copy-sequence rest))
389 (last (nthcdr (- n 2) copy)))
390 (setcdr last (car (cdr last)))
391 (cons arg copy)))))
393 (defmacro muse-assert (form &optional show-args string &rest args)
394 "Verify that FORM returns non-nil; signal an error if not.
395 Second arg SHOW-ARGS means to include arguments of FORM in message.
396 Other args STRING and ARGS... are arguments to be passed to `error'.
397 They are not evaluated unless the assertion fails. If STRING is
398 omitted, a default message listing FORM itself is used."
399 (let ((sargs
400 (and show-args
401 (delq nil (mapcar
402 (function
403 (lambda (x)
404 (and (not (muse-const-expr-p x)) x)))
405 (cdr form))))))
406 (list 'progn
407 (list 'or form
408 (if string
409 (muse-list* 'error string (append sargs args))
410 (list 'signal '(quote muse-assertion-failed)
411 (muse-list* 'list (list 'quote form) sargs))))
412 nil)))
414 ;; Compatibility functions
416 (if (fboundp 'looking-back)
417 (defalias 'muse-looking-back 'looking-back)
418 (defun muse-looking-back (regexp &optional limit &rest ignored)
419 (save-excursion
420 (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t))))
422 (if (fboundp 'line-end-position)
423 (defalias 'muse-line-end-position 'line-end-position)
424 (defun muse-line-end-position (&optional n)
425 (save-excursion (end-of-line n) (point))))
427 (if (fboundp 'line-beginning-position)
428 (defalias 'muse-line-beginning-position 'line-beginning-position)
429 (defun muse-line-beginning-position (&optional n)
430 (save-excursion (beginning-of-line n) (point))))
432 (eval-and-compile
433 (if (fboundp 'match-string-no-properties)
434 (defalias 'muse-match-string-no-properties 'match-string-no-properties)
435 (defun muse-match-string-no-properties (num &optional string)
436 (match-string num string))))
438 (defun muse-replace-regexp-in-string (regexp replacement text &optional fixedcase literal)
439 "Replace REGEXP with REPLACEMENT in TEXT.
441 Return a new string containing the replacements.
443 If fourth arg FIXEDCASE is non-nil, do not alter case of replacement text.
444 If fifth arg LITERAL is non-nil, insert REPLACEMENT literally."
445 (cond
446 ((fboundp 'replace-in-string)
447 (replace-in-string text regexp replacement literal))
448 ((fboundp 'replace-regexp-in-string)
449 (replace-regexp-in-string regexp replacement text fixedcase literal))
450 (t (let ((repl-len (length replacement))
451 start)
452 (unless (string= regexp "")
453 (save-match-data
454 (while (setq start (string-match regexp text start))
455 (setq start (+ start repl-len)
456 text (replace-match replacement fixedcase literal
457 text))))))
458 text)))
460 (if (fboundp 'add-to-invisibility-spec)
461 (defalias 'muse-add-to-invisibility-spec 'add-to-invisibility-spec)
462 (defun muse-add-to-invisibility-spec (element)
463 "Add ELEMENT to `buffer-invisibility-spec'.
464 See documentation for `buffer-invisibility-spec' for the kind of elements
465 that can be added."
466 (if (eq buffer-invisibility-spec t)
467 (setq buffer-invisibility-spec (list t)))
468 (setq buffer-invisibility-spec
469 (cons element buffer-invisibility-spec))))
471 (if (fboundp 'read-directory-name)
472 (defalias 'muse-read-directory-name 'read-directory-name)
473 (defun muse-read-directory-name (prompt &optional dir default-dirname mustmatch initial)
474 "Read directory name - see `read-file-name' for details."
475 (unless dir
476 (setq dir default-directory))
477 (read-file-name prompt dir (or default-dirname
478 (if initial (expand-file-name initial dir)
479 dir))
480 mustmatch initial)))
482 (defun muse-file-remote-p (file)
483 "Test whether FILE specifies a location on a remote system.
484 Return non-nil if the location is indeed remote.
486 For example, the filename \"/user@host:/foo\" specifies a location
487 on the system \"/user@host:\"."
488 (cond ((fboundp 'file-remote-p)
489 (file-remote-p file))
490 ((fboundp 'tramp-handle-file-remote-p)
491 (tramp-handle-file-remote-p file))
492 ((and (boundp 'ange-ftp-name-format)
493 (string-match ange-ftp-name-format file))
495 (t nil)))
497 ;; Set face globally in a predictable fashion
498 (defun muse-copy-face (old new)
499 "Copy face OLD to NEW."
500 (if (featurep 'xemacs)
501 (copy-face old new 'all)
502 (copy-face old new)))
504 ;; Widget compatibility functions
506 (defun muse-widget-type-value-create (widget)
507 "Convert and instantiate the value of the :type attribute of WIDGET.
508 Store the newly created widget in the :children attribute.
510 The value of the :type attribute should be an unconverted widget type."
511 (let ((value (widget-get widget :value))
512 (type (widget-get widget :type)))
513 (widget-put widget :children
514 (list (widget-create-child-value widget
515 (widget-convert type)
516 value)))))
518 (defun muse-widget-child-value-get (widget)
519 "Get the value of the first member of :children in WIDGET."
520 (widget-value (car (widget-get widget :children))))
522 (defun muse-widget-type-match (widget value)
523 "Non-nil if the :type value of WIDGET matches VALUE.
525 The value of the :type attribute should be an unconverted widget type."
526 (widget-apply (widget-convert (widget-get widget :type)) :match value))
528 ;; Link-handling functions and variables
530 (defun muse-get-link (&optional target)
531 "Based on the match data, retrieve the link.
532 Use TARGET to get the string, if it is specified."
533 (muse-match-string-no-properties 1 target))
535 (defun muse-get-link-desc (&optional target)
536 "Based on the match data, retrieve the link description.
537 Use TARGET to get the string, if it is specified."
538 (muse-match-string-no-properties 2 target))
540 (defvar muse-link-specials
541 '(("[" . "%5B")
542 ("]" . "%5D")
543 ("%" . "%%"))
544 "Syntax used for escaping and unescaping links.
545 This allows brackets to occur in explicit links as long as you
546 use the standard Muse functions to create them.")
548 (defun muse-link-escape (text)
549 "Escape characters in TEXT that conflict with the explicit link
550 regexp."
551 (when (stringp text)
552 (muse-escape-specials-in-string muse-link-specials text)))
554 (defun muse-link-unescape (text)
555 "Un-escape characters in TEXT that conflict with the explicit
556 link regexp."
557 (when (stringp text)
558 (muse-escape-specials-in-string muse-link-specials text t)))
560 (defun muse-handle-url (&optional string)
561 "If STRING or point has a URL, match and return it."
562 (if (if string (string-match muse-url-regexp string)
563 (looking-at muse-url-regexp))
564 (match-string 0 string)))
566 (defcustom muse-implicit-link-functions '(muse-handle-url)
567 "A list of functions to handle an implicit link.
568 An implicit link is one that is not surrounded by brackets.
570 By default, Muse handles URLs only.
571 If you want to handle WikiWords, load muse-wiki.el."
572 :type 'hook
573 :options '(muse-handle-url)
574 :group 'muse)
576 (defun muse-handle-implicit-link (&optional link)
577 "Handle implicit links. If LINK is not specified, look at point.
578 An implicit link is one that is not surrounded by brackets.
579 By default, Muse handles URLs only.
580 If you want to handle WikiWords, load muse-wiki.el.
582 This function modifies the match data so that match 1 is the
583 link. Match 2 will usually be nil, unless the description is
584 embedded in the text of the buffer.
586 The match data is restored after each unsuccessful handler
587 function call. If LINK is specified, only restore at very end.
589 This behavior is needed because the part of the buffer that
590 `muse-implicit-link-regexp' matches must be narrowed to the part
591 that is an accepted link."
592 (let ((funcs muse-implicit-link-functions)
593 (res nil)
594 (data (match-data t)))
595 (while funcs
596 (setq res (funcall (car funcs) link))
597 (if res
598 (setq funcs nil)
599 (unless link (set-match-data data))
600 (setq funcs (cdr funcs))))
601 (when link (set-match-data data))
602 res))
604 (defcustom muse-explicit-link-functions nil
605 "A list of functions to handle an explicit link.
606 An explicit link is one [[like][this]] or [[this]]."
607 :type 'hook
608 :group 'muse)
610 (defun muse-handle-explicit-link (&optional link)
611 "Handle explicit links. If LINK is not specified, look at point.
612 An explicit link is one that looks [[like][this]] or [[this]].
614 This function modifies the match data so that match 1 is the link
615 and match 2 is the description. Perhaps someday match 3 might be
616 the text to use for the alt element of an <a> or <img> tag.
618 The match data is saved. If no handlers are able to process
619 LINK, return LINK (if specified) or the 1st match string. If
620 LINK is not specified, it is assumed that Muse has matched
621 against `muse-explicit-link-regexp' before calling this
622 function."
623 (let ((funcs muse-explicit-link-functions)
624 (res nil))
625 (save-match-data
626 (while funcs
627 (setq res (funcall (car funcs) link))
628 (if res
629 (setq funcs nil)
630 (setq funcs (cdr funcs)))))
631 (muse-link-unescape
632 (if res
634 (or link (muse-get-link))))))
636 ;; Movement functions
638 (defun muse-list-item-type (str)
639 "Determine the type of list given STR.
640 Returns either 'ul, 'ol, 'dl-term, or 'dl-entry."
641 (cond ((or (string= str "")
642 (< (length str) 2))
643 nil)
644 ((and (= (aref str 0) ?\ )
645 (= (aref str 1) ?-))
646 'ul)
647 ((save-match-data
648 (string-match (concat "\\`[" muse-regexp-blank "][0-9]+\\.") str))
649 'ol)
650 ((save-match-data
651 (not (string-match (concat "\\`[" muse-regexp-blank "]*::") str)))
652 ;; if str is not any kind of list, it will be interpreted as
653 ;; a dl-term
654 'dl-term)
655 (t 'dl-entry)))
657 (defun muse-list-item-critical-point (&optional offset)
658 "Figure out where the important markup character for the
659 currently-matched list item is.
661 If OFFSET is specified, it is the number of groupings outside of
662 the contents of `muse-list-item-regexp'."
663 (unless offset (setq offset 0))
664 (if (match-end (+ offset 2))
665 ;; at a definition list
666 (match-end (+ offset 2))
667 ;; at a different kind of list
668 (match-beginning (+ offset 1))))
670 (defun muse-forward-paragraph (&optional pattern)
671 "Move forward safely by one paragraph, or according to PATTERN."
672 (when (get-text-property (point) 'end-list)
673 (goto-char (next-single-property-change (point) 'end-list)))
674 (setq pattern (if pattern
675 (concat "^\\(?:" pattern "\\|\n\\|\\'\\)")
676 "^\\s-*\\(\n\\|\\'\\)"))
677 (let ((next-list-end (or (next-single-property-change (point) 'end-list)
678 (point-max))))
679 (forward-line 1)
680 (if (re-search-forward pattern nil t)
681 (goto-char (match-beginning 0))
682 (goto-char (point-max)))
683 (when (> (point) next-list-end)
684 (goto-char next-list-end))))
686 (defun muse-forward-list-item-1 (type empty-line indented-line)
687 "Determine whether a nested list item is after point."
688 (if (match-beginning 1)
689 ;; if we are given a dl entry, skip past everything on the same
690 ;; level, except for other dl entries
691 (and (eq type 'dl-entry)
692 (not (eq (char-after (match-beginning 2)) ?\:)))
693 ;; blank line encountered with no list item on the same
694 ;; level after it
695 (let ((beg (point)))
696 (forward-line 1)
697 (if (save-match-data
698 (and (looking-at indented-line)
699 (not (looking-at empty-line))))
700 ;; found that this blank line is followed by some
701 ;; indentation, plus other text, so we'll keep
702 ;; going
704 (goto-char beg)
705 nil))))
707 (defun muse-forward-list-item (type indent &optional no-skip-nested)
708 "Move forward to the next item of TYPE.
709 Return non-nil if successful, nil otherwise.
710 The beginning indentation is given by INDENT.
712 If NO-SKIP-NESTED is non-nil, do not skip past nested items.
713 Note that if you desire this behavior, you will also need to
714 provide a very liberal INDENT value, such as
715 \(concat \"[\" muse-regexp-blank \"]*\")."
716 (let* ((list-item (format muse-list-item-regexp indent))
717 (empty-line (concat "^[" muse-regexp-blank "]*\n"))
718 (indented-line (concat "^" indent "[" muse-regexp-blank "]"))
719 (list-pattern (concat "\\(?:" empty-line "\\)?"
720 "\\(" list-item "\\)")))
721 (while (progn
722 (muse-forward-paragraph list-pattern)
723 ;; make sure we don't go past boundary
724 (and (not (or (get-text-property (point) 'end-list)
725 (>= (point) (point-max))))
726 ;; move past markup that is part of another construct
727 (or (and (match-beginning 1)
728 (or (get-text-property
729 (muse-list-item-critical-point 1) 'muse-link)
730 (get-text-property
731 (muse-list-item-critical-point 1) 'face)))
732 ;; skip nested items
733 (and (not no-skip-nested)
734 (muse-forward-list-item-1 type empty-line
735 indented-line))))))
736 (cond ((or (get-text-property (point) 'end-list)
737 (>= (point) (point-max)))
738 ;; at a list boundary, so stop
739 nil)
740 ((and (match-string 2)
741 (eq type (muse-list-item-type (match-string 2))))
742 ;; same type, so indicate that there are more items to be
743 ;; parsed
744 (goto-char (match-beginning 1)))
746 (when (match-beginning 1)
747 (goto-char (match-beginning 1)))
748 ;; move to just before foreign list item markup
749 nil))))
751 (defun muse-goto-tag-end (tag nested)
752 "Move forward past the end of TAG.
754 If NESTED is non-nil, look for other instances of this tag that
755 may be nested inside of this tag, and skip past them."
756 (if (not nested)
757 (search-forward (concat "</" tag ">") nil t)
758 (let ((nesting 1)
759 (tag-regexp (concat "\\(<\\(/?\\)" tag ">\\)"))
760 (match-found nil))
761 (while (and (> nesting 0)
762 (setq match-found (re-search-forward tag-regexp nil t)))
763 (if (string-equal (match-string 2) "/")
764 (setq nesting (1- nesting))
765 (setq nesting (1+ nesting))))
766 match-found)))
768 ;;; muse.el ends here