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