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
8 ;; Date: Thu 16-Aug-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 3, 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.
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,
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
48 ;; Indicate that this version of Muse supports nested tags
49 (provide 'muse-nested-tags
)
51 (defvar muse-version
"3.10"
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."
60 (message muse-version
)))
63 "Options controlling the behavior of Muse.
64 The markup used by Muse is intended to be very friendly to people
68 (defvar muse-under-windows-p
(memq system-type
'(ms-dos windows-nt
)))
76 (require 'muse-regexps
)
78 (defvar muse-update-values-hook nil
79 "Hook for values that are automatically generated.
80 This is to be used by add-on modules for Muse.
81 It is run just before colorizing or publishing a buffer.")
83 ;; Default file extension
85 ;; By default, use the .muse file extension.
86 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.muse\\'" . muse-mode-choose-mode))
88 ;; We need to have this at top-level, as well, so that any Muse or
89 ;; Planner documents opened during init will just work.
90 (add-to-list 'auto-mode-alist
'("\\.muse\\'" . muse-mode-choose-mode
))
93 (defvar muse-ignored-extensions
))
95 (defvar muse-ignored-extensions-regexp nil
96 "A regexp of extensions to omit from the ending of a Muse page name.
97 This is autogenerated from `muse-ignored-extensions'.")
99 (defun muse-update-file-extension (sym val
)
100 "Update the value of `muse-file-extension'."
101 (let ((old (and (boundp sym
) (symbol-value sym
))))
103 (when (and (featurep 'muse-mode
)
104 (or (not (stringp val
))
106 (not (string= old val
))))
107 ;; remove old auto-mode-alist association
108 (when (and (boundp sym
) (stringp old
))
109 (setq auto-mode-alist
110 (delete (cons (concat "\\." old
"\\'")
111 'muse-mode-choose-mode
)
113 ;; associate the new file extension with muse-mode
115 (add-to-list 'auto-mode-alist
116 (cons (concat "\\." val
"\\'")
117 'muse-mode-choose-mode
)))
118 ;; update the ignored extensions regexp
119 (when (fboundp 'muse-update-ignored-extensions-regexp
)
120 (muse-update-ignored-extensions-regexp
121 'muse-ignored-extensions muse-ignored-extensions
)))))
123 (defcustom muse-file-extension
"muse"
124 "File extension of Muse files. Omit the period at the beginning.
125 If you don't want Muse files to have an extension, set this to nil."
127 (const :tag
"None" nil
)
129 :set
'muse-update-file-extension
132 (defcustom muse-completing-read-function
'completing-read
133 "Function to call when prompting user to choose between a list of options.
134 This should take the same arguments as `completing-read'."
138 (defun muse-update-ignored-extensions-regexp (sym val
)
139 "Update the value of `muse-ignored-extensions-regexp'."
142 (setq muse-ignored-extensions-regexp
144 (regexp-quote (or muse-file-extension
"")) "\\|"
145 (mapconcat 'identity val
"\\|")
147 (setq muse-ignored-extensions-regexp
148 (if muse-file-extension
149 (concat "\\.\\(" muse-file-extension
"\\)\\'")
152 (add-hook 'muse-update-values-hook
154 (muse-update-ignored-extensions-regexp
155 'muse-ignored-extensions muse-ignored-extensions
)))
157 (defcustom muse-ignored-extensions
'("bz2" "gz" "[Zz]")
158 "A list of extensions to omit from the ending of a Muse page name.
161 Don't put a period at the beginning of each extension unless you
162 understand that it is part of a regexp."
163 :type
'(repeat (regexp :tag
"Extension"))
164 :set
'muse-update-ignored-extensions-regexp
167 (defun muse-update-file-extension-after-init ()
168 ;; This is short, but it has to be a function, otherwise Emacs21
169 ;; does not load it properly when running after-init-hook
170 (unless (string= muse-file-extension
"muse")
171 (let ((val muse-file-extension
)
172 (muse-file-extension "muse"))
173 (muse-update-file-extension 'muse-file-extension val
))))
175 ;; Once the user's init file has been processed, determine whether
176 ;; they want a file extension
177 (add-hook 'after-init-hook
'muse-update-file-extension-after-init
)
181 (require 'muse-protocols
)
185 (defsubst muse-delete-file-if-exists
(file)
186 (when (file-exists-p file
)
188 (message "Removed %s" file
)))
190 (defsubst muse-time-less-p
(t1 t2
)
191 "Say whether time T1 is less than time T2."
192 (or (< (car t1
) (car t2
))
193 (and (= (car t1
) (car t2
))
194 (< (nth 1 t1
) (nth 1 t2
)))))
197 (defvar muse-publishing-current-file nil
))
199 (defun muse-current-file ()
200 "Return the name of the currently visited or published file."
201 (or (and (boundp 'muse-publishing-current-file
)
202 muse-publishing-current-file
)
204 (concat default-directory
(buffer-name))))
206 (defun muse-page-name (&optional name
)
207 "Return the canonical form of a Muse page name.
209 What this means is that the directory part of NAME is removed,
210 and the file extensions in `muse-ignored-extensions' are also
213 (unless (and name
(not (string= name
"")))
214 (setq name
(muse-current-file)))
216 (let ((page (file-name-nondirectory name
)))
217 (if (and muse-ignored-extensions-regexp
218 (string-match muse-ignored-extensions-regexp page
))
219 (replace-match "" t t page
)
222 (defun muse-display-warning (message)
223 "Display the given MESSAGE as a warning."
224 (if (fboundp 'display-warning
)
225 (display-warning 'muse message
226 (if (featurep 'xemacs
)
229 (let ((buf (get-buffer-create "*Muse warnings*")))
230 (with-current-buffer buf
231 (goto-char (point-max))
232 (insert "Warning (muse): " message
)
238 (defun muse-eval-lisp (form)
239 "Evaluate the given form and return the result as a string."
243 (let ((object (eval (read form
))))
245 ((stringp object
) object
)
247 (not (eq object nil
)))
248 (let ((string (pp-to-string object
)))
249 (substring string
0 (1- (length string
)))))
251 (number-to-string object
))
254 (pp-to-string object
))))
256 (muse-display-warning (format "%s: Error evaluating %s: %s"
257 (muse-page-name) form err
))
258 "; INVALID LISP CODE"))))
260 (defmacro muse-with-temp-buffer
(&rest body
)
261 "Create a temporary buffer, and evaluate BODY there like `progn'.
262 See also `with-temp-file' and `with-output-to-string'.
264 Unlike `with-temp-buffer', this will never attempt to save the
265 temp buffer. It is meant to be used along with
266 `insert-file-contents' or `muse-insert-file-contents'.
268 Additionally, if `debug-on-error' is set to t, keep the buffer
269 around for debugging purposes rather than removing it."
270 (let ((temp-buffer (make-symbol "temp-buffer")))
271 `(let ((,temp-buffer
(generate-new-buffer " *muse-temp*")))
274 (with-current-buffer ,temp-buffer
277 (with-current-buffer ,temp-buffer
280 (if (and (boundp 'muse-batch-publishing-p
)
281 muse-batch-publishing-p
)
283 (message "%s: Error occured: %s"
284 (muse-page-name) err
)
286 (muse-display-warning
287 (format (concat "An error occurred while publishing"
288 " %s:\n %s\n\nSet debug-on-error to"
289 " `t' if you would like a backtrace.")
290 (muse-page-name) err
))))))
291 (when (buffer-live-p ,temp-buffer
)
292 (with-current-buffer ,temp-buffer
293 (set-buffer-modified-p nil
))
294 (unless debug-on-error
(kill-buffer ,temp-buffer
)))))))
296 (put 'muse-with-temp-buffer
'lisp-indent-function
0)
297 (put 'muse-with-temp-buffer
'edebug-form-spec
'(body))
299 (defun muse-insert-file-contents (filename &optional visit
)
300 "Insert the contents of file FILENAME after point.
301 Do character code conversion, but none of the other unnecessary
302 things like format decoding or `find-file-hook'.
304 If VISIT is non-nil, the buffer's visited filename
305 and last save file modtime are set, and it is marked unmodified.
306 If visiting and the file does not exist, visiting is completed
307 before the error is signaled."
308 (let ((format-alist nil
)
309 (after-insert-file-functions nil
)
310 (find-buffer-file-type-function
311 (if (fboundp 'find-buffer-file-type
)
312 (symbol-function 'find-buffer-file-type
)
314 (inhibit-file-name-handlers
315 (append '(jka-compr-handler image-file-handler
)
316 inhibit-file-name-handlers
))
317 (inhibit-file-name-operation 'insert-file-contents
))
320 (fset 'find-buffer-file-type
(lambda (filename) t
))
321 (insert-file-contents filename visit
))
322 (if find-buffer-file-type-function
323 (fset 'find-buffer-file-type find-buffer-file-type-function
)
324 (fmakunbound 'find-buffer-file-type
)))))
326 (defun muse-write-file (filename)
327 "Write current buffer into file FILENAME.
328 Unlike `write-file', this does not visit the file, try to back it
329 up, or interact with vc.el in any way.
331 If the file was not written successfully, return nil. Otherwise,
333 (let ((backup-inhibited t
)
334 (buffer-file-name filename
)
335 (buffer-file-truename (file-truename filename
)))
339 (if (not (file-writable-p buffer-file-name
))
341 (muse-display-warning
342 (format "Cannot write file %s:\n %s" buffer-file-name
343 (let ((dir (file-name-directory buffer-file-name
)))
344 (if (not (file-directory-p dir
))
345 (if (file-exists-p dir
)
346 (format "%s is not a directory" dir
)
347 (format "No directory named %s exists" dir
))
348 (if (not (file-exists-p buffer-file-name
))
349 (format "Directory %s write-protected" dir
)
350 "File is write-protected"))))))
351 (let ((coding-system-for-write
352 (or (and (boundp 'save-buffer-coding-system
)
353 save-buffer-coding-system
)
354 coding-system-for-write
)))
355 (write-region (point-min) (point-max) buffer-file-name
))
356 (when (boundp 'last-file-coding-system-used
)
357 (when (boundp 'buffer-file-coding-system-explicit
)
358 (setq buffer-file-coding-system-explicit
359 last-coding-system-used
))
360 (if save-buffer-coding-system
361 (setq save-buffer-coding-system last-coding-system-used
)
362 (setq buffer-file-coding-system last-coding-system-used
)))
365 (defun muse-collect-alist (list element
&optional test
)
366 "Collect items from LIST whose car is equal to ELEMENT.
367 If TEST is specified, use it to compare ELEMENT."
368 (unless test
(setq test
'equal
))
371 (when (funcall test element
(car item
))
372 (setq items
(cons item items
))))
375 (defmacro muse-sort-with-closure
(list predicate closure
)
376 "Sort LIST, stably, comparing elements using PREDICATE.
377 Returns the sorted list. LIST is modified by side effects.
378 PREDICATE is called with two elements of list and CLOSURE.
379 PREDICATE should return non-nil if the first element should sort
381 `(sort ,list
(lambda (a b
) (funcall ,predicate a b
,closure
))))
383 (put 'muse-sort-with-closure
'lisp-indent-function
0)
384 (put 'muse-sort-with-closure
'edebug-form-spec
'(form function-form form
))
386 (defun muse-sort-by-rating (rated-list &optional test
)
387 "Sort RATED-LIST according to the rating of each element.
388 The rating is stripped out in the returned list.
389 Default sorting is highest-first.
391 If TEST if specified, use it to sort the list. The default test is '>."
392 (unless test
(setq test
'>))
393 (mapcar (function cdr
)
394 (muse-sort-with-closure
396 (lambda (a b closure
)
397 (let ((na (numberp (car a
)))
398 (nb (numberp (car b
))))
399 (cond ((and na nb
) (funcall closure
(car a
) (car b
)))
404 (defun muse-escape-specials-in-string (specials string
&optional reverse
)
405 "Apply the transformations in SPECIALS to STRING.
407 The transforms should form a fully reversible and non-ambiguous
408 syntax when STRING is parsed from left to right.
410 If REVERSE is specified, reverse an already-escaped string."
411 (let ((rules (mapcar (lambda (rule)
412 (cons (regexp-quote (if reverse
415 (if reverse
(car rule
) (cdr rule
))))
419 (goto-char (point-min))
422 (unless (catch 'found
424 (when (looking-at (car rule
))
425 (replace-match (cdr rule
) t t
)
430 (defun muse-trim-whitespace (string)
431 "Return a version of STRING with no initial nor trailing whitespace."
432 (muse-replace-regexp-in-string
433 (concat "\\`[" muse-regexp-blank
"]+\\|[" muse-regexp-blank
"]+\\'")
436 (defun muse-path-sans-extension (path)
437 "Return PATH sans final \"extension\".
439 The extension, in a file name, is the part that follows the last `.',
440 except that a leading `.', if any, doesn't count.
442 This differs from `file-name-sans-extension' in that it will
443 never modify the directory part of the path."
444 (concat (file-name-directory path
)
445 (file-name-nondirectory (file-name-sans-extension path
))))
447 ;; The following code was extracted from cl
449 (defun muse-const-expr-p (x)
451 (or (eq (car x
) 'quote
)
452 (and (memq (car x
) '(function function
*))
453 (or (symbolp (nth 1 x
))
454 (and (eq (and (consp (nth 1 x
))
455 (car (nth 1 x
))) 'lambda
) 'func
)))))
456 ((symbolp x
) (and (memq x
'(nil t
)) t
))
459 (put 'muse-assertion-failed
'error-conditions
'(error))
460 (put 'muse-assertion-failed
'error-message
"Assertion failed")
462 (defun muse-list* (arg &rest rest
)
463 "Return a new list with specified args as elements, cons'd to last arg.
464 Thus, `(list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to
465 `(cons A (cons B (cons C D)))'."
466 (cond ((not rest
) arg
)
467 ((not (cdr rest
)) (cons arg
(car rest
)))
468 (t (let* ((n (length rest
))
469 (copy (copy-sequence rest
))
470 (last (nthcdr (- n
2) copy
)))
471 (setcdr last
(car (cdr last
)))
474 (defmacro muse-assert
(form &optional show-args string
&rest args
)
475 "Verify that FORM returns non-nil; signal an error if not.
476 Second arg SHOW-ARGS means to include arguments of FORM in message.
477 Other args STRING and ARGS... are arguments to be passed to `error'.
478 They are not evaluated unless the assertion fails. If STRING is
479 omitted, a default message listing FORM itself is used."
485 (and (not (muse-const-expr-p x
)) x
)))
490 (muse-list* 'error string
(append sargs args
))
491 (list 'signal
'(quote muse-assertion-failed
)
492 (muse-list* 'list
(list 'quote form
) sargs
))))
495 ;; Compatibility functions
497 (if (fboundp 'looking-back
)
498 (defalias 'muse-looking-back
'looking-back
)
499 (defun muse-looking-back (regexp &optional limit
&rest ignored
)
501 (re-search-backward (concat "\\(?:" regexp
"\\)\\=") limit t
))))
504 (if (fboundp 'line-end-position
)
505 (defalias 'muse-line-end-position
'line-end-position
)
506 (defun muse-line-end-position (&optional n
)
507 (save-excursion (end-of-line n
) (point))))
509 (if (fboundp 'line-beginning-position
)
510 (defalias 'muse-line-beginning-position
'line-beginning-position
)
511 (defun muse-line-beginning-position (&optional n
)
512 (save-excursion (beginning-of-line n
) (point))))
514 (if (fboundp 'match-string-no-properties
)
515 (defalias 'muse-match-string-no-properties
'match-string-no-properties
)
516 (defun muse-match-string-no-properties (num &optional string
)
517 (match-string num string
))))
519 (defun muse-replace-regexp-in-string (regexp replacement text
&optional fixedcase literal
)
520 "Replace REGEXP with REPLACEMENT in TEXT.
522 Return a new string containing the replacements.
524 If fourth arg FIXEDCASE is non-nil, do not alter case of replacement text.
525 If fifth arg LITERAL is non-nil, insert REPLACEMENT literally."
527 ((and (featurep 'xemacs
) (fboundp 'replace-in-string
))
528 (replace-in-string text regexp replacement literal
))
529 ((fboundp 'replace-regexp-in-string
)
530 (replace-regexp-in-string regexp replacement text fixedcase literal
))
531 (t (let ((repl-len (length replacement
))
533 (unless (string= regexp
"")
535 (while (setq start
(string-match regexp text start
))
536 (setq start
(+ start repl-len
)
537 text
(replace-match replacement fixedcase literal
541 (if (fboundp 'add-to-invisibility-spec
)
542 (defalias 'muse-add-to-invisibility-spec
'add-to-invisibility-spec
)
543 (defun muse-add-to-invisibility-spec (element)
544 "Add ELEMENT to `buffer-invisibility-spec'.
545 See documentation for `buffer-invisibility-spec' for the kind of elements
547 (if (eq buffer-invisibility-spec t
)
548 (setq buffer-invisibility-spec
(list t
)))
549 (setq buffer-invisibility-spec
550 (cons element buffer-invisibility-spec
))))
552 (if (fboundp 'read-directory-name
)
553 (defalias 'muse-read-directory-name
'read-directory-name
)
554 (defun muse-read-directory-name (prompt &optional dir default-dirname mustmatch initial
)
555 "Read directory name - see `read-file-name' for details."
557 (setq dir default-directory
))
558 (read-file-name prompt dir
(or default-dirname
559 (if initial
(expand-file-name initial dir
)
563 (defun muse-file-remote-p (file)
564 "Test whether FILE specifies a location on a remote system.
565 Return non-nil if the location is indeed remote.
567 For example, the filename \"/user@host:/foo\" specifies a location
568 on the system \"/user@host:\"."
569 (cond ((fboundp 'file-remote-p
)
570 (file-remote-p file
))
571 ((fboundp 'tramp-handle-file-remote-p
)
572 (tramp-handle-file-remote-p file
))
573 ((and (boundp 'ange-ftp-name-format
)
574 (string-match (car ange-ftp-name-format
) file
))
578 (if (fboundp 'delete-and-extract-region
)
579 (defalias 'muse-delete-and-extract-region
'delete-and-extract-region
)
580 (defun muse-delete-and-extract-region (start end
)
581 "Delete the text between START and END and return it."
582 (prog1 (buffer-substring start end
)
583 (delete-region start end
))))
585 ;; Set face globally in a predictable fashion
586 (defun muse-copy-face (old new
)
587 "Copy face OLD to NEW."
588 (if (featurep 'xemacs
)
589 (copy-face old new
'all
)
590 (copy-face old new
)))
592 ;; Widget compatibility functions
594 (defun muse-widget-type-value-create (widget)
595 "Convert and instantiate the value of the :type attribute of WIDGET.
596 Store the newly created widget in the :children attribute.
598 The value of the :type attribute should be an unconverted widget type."
599 (let ((value (widget-get widget
:value
))
600 (type (widget-get widget
:type
)))
601 (widget-put widget
:children
602 (list (widget-create-child-value widget
603 (widget-convert type
)
606 (defun muse-widget-child-value-get (widget)
607 "Get the value of the first member of :children in WIDGET."
608 (widget-value (car (widget-get widget
:children
))))
610 (defun muse-widget-type-match (widget value
)
611 "Non-nil if the :type value of WIDGET matches VALUE.
613 The value of the :type attribute should be an unconverted widget type."
614 (widget-apply (widget-convert (widget-get widget
:type
)) :match value
))
616 ;; Link-handling functions and variables
618 (defun muse-get-link (&optional target
)
619 "Based on the match data, retrieve the link.
620 Use TARGET to get the string, if it is specified."
621 (muse-match-string-no-properties 1 target
))
623 (defun muse-get-link-desc (&optional target
)
624 "Based on the match data, retrieve the link description.
625 Use TARGET to get the string, if it is specified."
626 (muse-match-string-no-properties 2 target
))
628 (defvar muse-link-specials
632 "Syntax used for escaping and unescaping links.
633 This allows brackets to occur in explicit links as long as you
634 use the standard Muse functions to create them.")
636 (defun muse-link-escape (text)
637 "Escape characters in TEXT that conflict with the explicit link
640 (muse-escape-specials-in-string muse-link-specials text
)))
642 (defun muse-link-unescape (text)
643 "Un-escape characters in TEXT that conflict with the explicit
646 (muse-escape-specials-in-string muse-link-specials text t
)))
648 (defun muse-handle-url (&optional string
)
649 "If STRING or point has a URL, match and return it."
650 (if (if string
(string-match muse-url-regexp string
)
651 (looking-at muse-url-regexp
))
652 (match-string 0 string
)))
654 (defcustom muse-implicit-link-functions
'(muse-handle-url)
655 "A list of functions to handle an implicit link.
656 An implicit link is one that is not surrounded by brackets.
658 By default, Muse handles URLs only.
659 If you want to handle WikiWords, load muse-wiki.el."
661 :options
'(muse-handle-url)
664 (defun muse-handle-implicit-link (&optional link
)
665 "Handle implicit links. If LINK is not specified, look at point.
666 An implicit link is one that is not surrounded by brackets.
667 By default, Muse handles URLs only.
668 If you want to handle WikiWords, load muse-wiki.el.
670 This function modifies the match data so that match 0 is the
673 The match data is restored after each unsuccessful handler
674 function call. If LINK is specified, only restore at very end.
676 This behavior is needed because the part of the buffer that
677 `muse-implicit-link-regexp' matches must be narrowed to the part
678 that is an accepted link."
679 (let ((funcs muse-implicit-link-functions
)
681 (data (match-data t
)))
683 (setq res
(funcall (car funcs
) link
))
686 (unless link
(set-match-data data
))
687 (setq funcs
(cdr funcs
))))
688 (when link
(set-match-data data
))
691 (defcustom muse-explicit-link-functions nil
692 "A list of functions to handle an explicit link.
693 An explicit link is one [[like][this]] or [[this]]."
697 (defun muse-handle-explicit-link (&optional link
)
698 "Handle explicit links. If LINK is not specified, look at point.
699 An explicit link is one that looks [[like][this]] or [[this]].
701 The match data is preserved. If no handlers are able to process
702 LINK, return LINK (if specified) or the 1st match string. If
703 LINK is not specified, it is assumed that Muse has matched
704 against `muse-explicit-link-regexp' before calling this
706 (let ((funcs muse-explicit-link-functions
)
710 (setq res
(funcall (car funcs
) link
))
713 (setq funcs
(cdr funcs
)))))
717 (or link
(muse-get-link))))))
719 ;; Movement functions
721 (defun muse-list-item-type (str)
722 "Determine the type of list given STR.
723 Returns either 'ul, 'ol, 'dl-term, 'dl-entry, or nil."
725 (cond ((or (string= str
"")
728 ((string-match muse-dl-entry-regexp str
)
730 ((string-match muse-dl-term-regexp str
)
732 ((string-match muse-ol-item-regexp str
)
734 ((string-match muse-ul-item-regexp str
)
738 (defun muse-list-item-critical-point (&optional offset
)
739 "Figure out where the important markup character for the
740 currently-matched list item is.
742 If OFFSET is specified, it is the number of groupings outside of
743 the contents of `muse-list-item-regexp'."
744 (unless offset
(setq offset
0))
745 (if (match-end (+ offset
2))
746 ;; at a definition list
747 (match-end (+ offset
2))
748 ;; at a different kind of list
749 (match-beginning (+ offset
1))))
751 (defun muse-forward-paragraph (&optional pattern
)
752 "Move forward safely by one paragraph, or according to PATTERN."
753 (when (get-text-property (point) 'end-list
)
754 (goto-char (next-single-property-change (point) 'end-list
)))
755 (setq pattern
(if pattern
756 (concat "^\\(?:" pattern
"\\|\n\\|\\'\\)")
757 "^\\s-*\\(\n\\|\\'\\)"))
758 (let ((next-list-end (or (next-single-property-change (point) 'end-list
)
761 (if (re-search-forward pattern nil t
)
762 (goto-char (match-beginning 0))
763 (goto-char (point-max)))
764 (when (> (point) next-list-end
)
765 (goto-char next-list-end
))))
767 (defun muse-forward-list-item-1 (type empty-line indented-line
)
768 "Determine whether a nested list item is after point."
769 (if (match-beginning 1)
770 ;; if we are given a dl entry, skip past everything on the same
771 ;; level, except for other dl entries
772 (and (eq type
'dl-entry
)
773 (not (eq (char-after (match-beginning 2)) ?\
:)))
774 ;; blank line encountered with no list item on the same
779 (and (looking-at indented-line
)
780 (not (looking-at empty-line
))))
781 ;; found that this blank line is followed by some
782 ;; indentation, plus other text, so we'll keep
788 (defun muse-forward-list-item (type indent
&optional no-skip-nested
)
789 "Move forward to the next item of TYPE.
790 Return non-nil if successful, nil otherwise.
791 The beginning indentation is given by INDENT.
793 If NO-SKIP-NESTED is non-nil, do not skip past nested items.
794 Note that if you desire this behavior, you will also need to
795 provide a very liberal INDENT value, such as
796 \(concat \"[\" muse-regexp-blank \"]*\")."
797 (let* ((list-item (format muse-list-item-regexp indent
))
798 (empty-line (concat "^[" muse-regexp-blank
"]*\n"))
799 (indented-line (concat "^" indent
"[" muse-regexp-blank
"]"))
800 (list-pattern (concat "\\(?:" empty-line
"\\)?"
801 "\\(" list-item
"\\)")))
803 (muse-forward-paragraph list-pattern
)
804 ;; make sure we don't go past boundary
805 (and (not (or (get-text-property (point) 'end-list
)
806 (>= (point) (point-max))))
807 ;; move past markup that is part of another construct
808 (or (and (match-beginning 1)
809 (or (get-text-property
810 (muse-list-item-critical-point 1) 'muse-link
)
811 (and (derived-mode-p 'muse-mode
)
813 (muse-list-item-critical-point 1)
816 (and (not no-skip-nested
)
817 (muse-forward-list-item-1 type empty-line
819 (cond ((or (get-text-property (point) 'end-list
)
820 (>= (point) (point-max)))
821 ;; at a list boundary, so stop
823 ((let ((str (when (match-beginning 2)
824 ;; get the entire line
826 (goto-char (match-beginning 2))
827 (buffer-substring (muse-line-beginning-position)
828 (muse-line-end-position))))))
829 (and str
(eq type
(muse-list-item-type str
))))
830 ;; same type, so indicate that there are more items to be
832 (goto-char (match-beginning 1)))
834 (when (match-beginning 1)
835 (goto-char (match-beginning 1)))
836 ;; move to just before foreign list item markup
839 (defun muse-goto-tag-end (tag nested
)
840 "Move forward past the end of TAG.
842 If NESTED is non-nil, look for other instances of this tag that
843 may be nested inside of this tag, and skip past them."
845 (search-forward (concat "</" tag
">") nil t
)
847 (tag-regexp (concat "\\(<\\(/?\\)" tag
">\\)"))
849 (while (and (> nesting
0)
850 (setq match-found
(re-search-forward tag-regexp nil t
)))
851 ;; for the sake of font-locking code, skip matches in comments
852 (unless (get-text-property (match-beginning 0) 'muse-comment
)
853 (if (string-equal (match-string 2) "/")
854 (setq nesting
(1- nesting
))
855 (setq nesting
(1+ nesting
)))))
858 ;;; muse.el ends here