Release Emacs Muse 3.11
[muse-el.git] / lisp / muse.el
bloba2a9a38f43b1da43fc759d67628cb30db6dd8edb
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.11
8 ;; Date: Fri 24-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.
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.11"
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 (condition-case nil
73 (require 'derived)
74 (error nil))
75 (require 'wid-edit)
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))
92 (eval-when-compile
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))))
102 (set sym val)
103 (when (and (featurep 'muse-mode)
104 (or (not (stringp val))
105 (not (stringp old))
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)
112 auto-mode-alist)))
113 ;; associate the new file extension with muse-mode
114 (when (stringp val)
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."
126 :type '(choice
127 (const :tag "None" nil)
128 (string))
129 :set 'muse-update-file-extension
130 :group 'muse)
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'."
135 :type 'function
136 :group 'muse)
138 (defun muse-update-ignored-extensions-regexp (sym val)
139 "Update the value of `muse-ignored-extensions-regexp'."
140 (set sym val)
141 (if val
142 (setq muse-ignored-extensions-regexp
143 (concat "\\.\\("
144 (regexp-quote (or muse-file-extension "")) "\\|"
145 (mapconcat 'identity val "\\|")
146 "\\)\\'"))
147 (setq muse-ignored-extensions-regexp
148 (if muse-file-extension
149 (concat "\\.\\(" muse-file-extension "\\)\\'")
150 nil))))
152 (add-hook 'muse-update-values-hook
153 (lambda ()
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.
159 These are regexps.
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
165 :group 'muse)
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)
179 ;; URL protocols
181 (require 'muse-protocols)
183 ;; Helper functions
185 (defsubst muse-delete-file-if-exists (file)
186 (when (file-exists-p file)
187 (delete-file 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)))))
196 (eval-when-compile
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)
203 (buffer-file-name)
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
211 removed from NAME."
212 (save-match-data
213 (unless (and name (not (string= name "")))
214 (setq name (muse-current-file)))
215 (if name
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)
220 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)
227 'warning
228 :warning))
229 (let ((buf (get-buffer-create "*Muse warnings*")))
230 (with-current-buffer buf
231 (goto-char (point-max))
232 (insert "Warning (muse): " message)
233 (unless (bolp)
234 (newline)))
235 (display-buffer buf)
236 (sit-for 0))))
238 (defun muse-eval-lisp (form)
239 "Evaluate the given form and return the result as a string."
240 (require 'pp)
241 (save-match-data
242 (condition-case err
243 (let ((object (eval (read form))))
244 (cond
245 ((stringp object) object)
246 ((and (listp object)
247 (not (eq object nil)))
248 (let ((string (pp-to-string object)))
249 (substring string 0 (1- (length string)))))
250 ((numberp object)
251 (number-to-string object))
252 ((eq object nil) "")
254 (pp-to-string object))))
255 (error
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*")))
272 (unwind-protect
273 (if debug-on-error
274 (with-current-buffer ,temp-buffer
275 ,@body)
276 (condition-case err
277 (with-current-buffer ,temp-buffer
278 ,@body)
279 (error
280 (if (and (boundp 'muse-batch-publishing-p)
281 muse-batch-publishing-p)
282 (progn
283 (message "%s: Error occured: %s"
284 (muse-page-name) err)
285 (backtrace))
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)
313 nil))
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))
318 (unwind-protect
319 (progn
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,
332 return non-nil."
333 (let ((backup-inhibited t)
334 (buffer-file-name filename)
335 (buffer-file-truename (file-truename filename)))
336 (save-current-buffer
337 (save-restriction
338 (widen)
339 (if (not (file-writable-p buffer-file-name))
340 (prog1 nil
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)))
363 t)))))
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))
369 (let ((items nil))
370 (dolist (item list)
371 (when (funcall test element (car item))
372 (setq items (cons item items))))
373 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
380 before the second."
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
395 rated-list
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)))
400 (na (not nb))
401 (t nil))))
402 test)))
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
413 (cdr rule)
414 (car rule)))
415 (if reverse (car rule) (cdr rule))))
416 specials)))
417 (with-temp-buffer
418 (insert string)
419 (goto-char (point-min))
420 (save-match-data
421 (while (not (eobp))
422 (unless (catch 'found
423 (dolist (rule rules)
424 (when (looking-at (car rule))
425 (replace-match (cdr rule) t t)
426 (throw 'found t))))
427 (forward-char))))
428 (buffer-string))))
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 "]+\\'")
434 "" string))
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)
450 (cond ((consp 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))
457 (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)))
472 (cons arg copy)))))
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."
480 (let ((sargs
481 (and show-args
482 (delq nil (mapcar
483 (function
484 (lambda (x)
485 (and (not (muse-const-expr-p x)) x)))
486 (cdr form))))))
487 (list 'progn
488 (list 'or form
489 (if string
490 (muse-list* 'error string (append sargs args))
491 (list 'signal '(quote muse-assertion-failed)
492 (muse-list* 'list (list 'quote form) sargs))))
493 nil)))
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)
500 (save-excursion
501 (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t))))
503 (eval-and-compile
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."
526 (cond
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))
532 start)
533 (unless (string= regexp "")
534 (save-match-data
535 (while (setq start (string-match regexp text start))
536 (setq start (+ start repl-len)
537 text (replace-match replacement fixedcase literal
538 text))))))
539 text)))
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
546 that can be added."
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."
556 (unless dir
557 (setq dir default-directory))
558 (read-file-name prompt dir (or default-dirname
559 (if initial (expand-file-name initial dir)
560 dir))
561 mustmatch initial)))
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))
576 (t nil)))
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)
604 value)))))
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
629 '(("[" . "%5B")
630 ("]" . "%5D")
631 ("%" . "%%"))
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
638 regexp."
639 (when (stringp text)
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
644 link regexp."
645 (when (stringp text)
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."
660 :type 'hook
661 :options '(muse-handle-url)
662 :group 'muse)
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
671 link.
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)
680 (res nil)
681 (data (match-data t)))
682 (while funcs
683 (setq res (funcall (car funcs) link))
684 (if res
685 (setq funcs nil)
686 (unless link (set-match-data data))
687 (setq funcs (cdr funcs))))
688 (when link (set-match-data data))
689 res))
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]]."
694 :type 'hook
695 :group 'muse)
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
705 function."
706 (let ((funcs muse-explicit-link-functions)
707 (res nil))
708 (save-match-data
709 (while funcs
710 (setq res (funcall (car funcs) link))
711 (if res
712 (setq funcs nil)
713 (setq funcs (cdr funcs)))))
714 (muse-link-unescape
715 (if res
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."
724 (save-match-data
725 (cond ((or (string= str "")
726 (< (length str) 2))
727 nil)
728 ((string-match muse-dl-entry-regexp str)
729 'dl-entry)
730 ((string-match muse-dl-term-regexp str)
731 'dl-term)
732 ((string-match muse-ol-item-regexp str)
733 'ol)
734 ((string-match muse-ul-item-regexp str)
735 'ul)
736 (t nil))))
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)
759 (point-max))))
760 (forward-line 1)
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
775 ;; level after it
776 (let ((beg (point)))
777 (forward-line 1)
778 (if (save-match-data
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
783 ;; going
785 (goto-char beg)
786 nil))))
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 "\\)")))
802 (while (progn
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)
812 (get-text-property
813 (muse-list-item-critical-point 1)
814 'face))))
815 ;; skip nested items
816 (and (not no-skip-nested)
817 (muse-forward-list-item-1 type empty-line
818 indented-line))))))
819 (cond ((or (get-text-property (point) 'end-list)
820 (>= (point) (point-max)))
821 ;; at a list boundary, so stop
822 nil)
823 ((let ((str (when (match-beginning 2)
824 ;; get the entire line
825 (save-excursion
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
831 ;; parsed
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
837 nil))))
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."
844 (if (not nested)
845 (search-forward (concat "</" tag ">") nil t)
846 (let ((nesting 1)
847 (tag-regexp (concat "\\(<\\(/?\\)" tag ">\\)"))
848 (match-found nil))
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)))))
856 match-found)))
858 ;;; muse.el ends here