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