Fix failure to detect nested class tags
[muse-el.git] / lisp / muse.el
blob251fa38e5f30490ac536f7f5697cf0125fa9088a
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 (defun muse-update-values ()
84 "Update various values that are automatically generated.
86 Call this after changing `muse-project-alist'."
87 (interactive)
88 (run-hooks 'muse-update-values-hook)
89 (dolist (buffer (buffer-list))
90 (when (buffer-live-p buffer)
91 (with-current-buffer buffer
92 (when (and (derived-mode-p 'muse-mode)
93 muse-current-project)
94 (setq muse-current-project nil)
95 (setq muse-current-project (muse-project-of-file)))))))
97 ;; Default file extension
99 ;; By default, use the .muse file extension.
100 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.muse\\'" . muse-mode-choose-mode))
102 ;; We need to have this at top-level, as well, so that any Muse or
103 ;; Planner documents opened during init will just work.
104 (add-to-list 'auto-mode-alist '("\\.muse\\'" . muse-mode-choose-mode))
106 (eval-when-compile
107 (defvar muse-ignored-extensions))
109 (defvar muse-ignored-extensions-regexp nil
110 "A regexp of extensions to omit from the ending of a Muse page name.
111 This is autogenerated from `muse-ignored-extensions'.")
113 (defun muse-update-file-extension (sym val)
114 "Update the value of `muse-file-extension'."
115 (let ((old (and (boundp sym) (symbol-value sym))))
116 (set sym val)
117 (when (and (featurep 'muse-mode)
118 (or (not (stringp val))
119 (not (stringp old))
120 (not (string= old val))))
121 ;; remove old auto-mode-alist association
122 (when (and (boundp sym) (stringp old))
123 (setq auto-mode-alist
124 (delete (cons (concat "\\." old "\\'")
125 'muse-mode-choose-mode)
126 auto-mode-alist)))
127 ;; associate the new file extension with muse-mode
128 (when (stringp val)
129 (add-to-list 'auto-mode-alist
130 (cons (concat "\\." val "\\'")
131 'muse-mode-choose-mode)))
132 ;; update the ignored extensions regexp
133 (when (fboundp 'muse-update-ignored-extensions-regexp)
134 (muse-update-ignored-extensions-regexp
135 'muse-ignored-extensions muse-ignored-extensions)))))
137 (defcustom muse-file-extension "muse"
138 "File extension of Muse files. Omit the period at the beginning.
139 If you don't want Muse files to have an extension, set this to nil."
140 :type '(choice
141 (const :tag "None" nil)
142 (string))
143 :set 'muse-update-file-extension
144 :group 'muse)
146 (defcustom muse-completing-read-function 'completing-read
147 "Function to call when prompting user to choose between a list of options.
148 This should take the same arguments as `completing-read'."
149 :type 'function
150 :group 'muse)
152 (defun muse-update-ignored-extensions-regexp (sym val)
153 "Update the value of `muse-ignored-extensions-regexp'."
154 (set sym val)
155 (if val
156 (setq muse-ignored-extensions-regexp
157 (concat "\\.\\("
158 (regexp-quote (or muse-file-extension "")) "\\|"
159 (mapconcat 'identity val "\\|")
160 "\\)\\'"))
161 (setq muse-ignored-extensions-regexp
162 (if muse-file-extension
163 (concat "\\.\\(" muse-file-extension "\\)\\'")
164 nil))))
166 (add-hook 'muse-update-values-hook
167 (lambda ()
168 (muse-update-ignored-extensions-regexp
169 'muse-ignored-extensions muse-ignored-extensions)))
171 (defcustom muse-ignored-extensions '("bz2" "gz" "[Zz]")
172 "A list of extensions to omit from the ending of a Muse page name.
173 These are regexps.
175 Don't put a period at the beginning of each extension unless you
176 understand that it is part of a regexp."
177 :type '(repeat (regexp :tag "Extension"))
178 :set 'muse-update-ignored-extensions-regexp
179 :group 'muse)
181 (defun muse-update-file-extension-after-init ()
182 ;; This is short, but it has to be a function, otherwise Emacs21
183 ;; does not load it properly when running after-init-hook
184 (unless (string= muse-file-extension "muse")
185 (let ((val muse-file-extension)
186 (muse-file-extension "muse"))
187 (muse-update-file-extension 'muse-file-extension val))))
189 ;; Once the user's init file has been processed, determine whether
190 ;; they want a file extension
191 (add-hook 'after-init-hook 'muse-update-file-extension-after-init)
193 ;; URL protocols
195 (require 'muse-protocols)
197 ;; Helper functions
199 (defsubst muse-delete-file-if-exists (file)
200 (when (file-exists-p file)
201 (delete-file file)
202 (message "Removed %s" file)))
204 (defsubst muse-time-less-p (t1 t2)
205 "Say whether time T1 is less than time T2."
206 (or (< (car t1) (car t2))
207 (and (= (car t1) (car t2))
208 (< (nth 1 t1) (nth 1 t2)))))
210 (eval-when-compile
211 (defvar muse-publishing-current-file nil))
213 (defun muse-current-file ()
214 "Return the name of the currently visited or published file."
215 (or (and (boundp 'muse-publishing-current-file)
216 muse-publishing-current-file)
217 (buffer-file-name)
218 (concat default-directory (buffer-name))))
220 (defun muse-page-name (&optional name)
221 "Return the canonical form of a Muse page name.
223 What this means is that the directory part of NAME is removed,
224 and the file extensions in `muse-ignored-extensions' are also
225 removed from NAME."
226 (save-match-data
227 (unless (and name (not (string= name "")))
228 (setq name (muse-current-file)))
229 (if name
230 (let ((page (file-name-nondirectory name)))
231 (if (and muse-ignored-extensions-regexp
232 (string-match muse-ignored-extensions-regexp page))
233 (replace-match "" t t page)
234 page)))))
236 (defun muse-display-warning (message)
237 "Display the given MESSAGE as a warning."
238 (if (fboundp 'display-warning)
239 (display-warning 'muse message
240 (if (featurep 'xemacs)
241 'warning
242 :warning))
243 (let ((buf (get-buffer-create "*Muse warnings*")))
244 (with-current-buffer buf
245 (goto-char (point-max))
246 (insert "Warning (muse): " message)
247 (unless (bolp)
248 (newline)))
249 (display-buffer buf)
250 (sit-for 0))))
252 (defun muse-eval-lisp (form)
253 "Evaluate the given form and return the result as a string."
254 (require 'pp)
255 (save-match-data
256 (condition-case err
257 (let ((object (eval (read form))))
258 (cond
259 ((stringp object) object)
260 ((and (listp object)
261 (not (eq object nil)))
262 (let ((string (pp-to-string object)))
263 (substring string 0 (1- (length string)))))
264 ((numberp object)
265 (number-to-string object))
266 ((eq object nil) "")
268 (pp-to-string object))))
269 (error
270 (muse-display-warning (format "%s: Error evaluating %s: %s"
271 (muse-page-name) form err))
272 "; INVALID LISP CODE"))))
274 (defmacro muse-with-temp-buffer (&rest body)
275 "Create a temporary buffer, and evaluate BODY there like `progn'.
276 See also `with-temp-file' and `with-output-to-string'.
278 Unlike `with-temp-buffer', this will never attempt to save the
279 temp buffer. It is meant to be used along with
280 `insert-file-contents' or `muse-insert-file-contents'.
282 Additionally, if `debug-on-error' is set to t, keep the buffer
283 around for debugging purposes rather than removing it."
284 (let ((temp-buffer (make-symbol "temp-buffer")))
285 `(let ((,temp-buffer (generate-new-buffer " *muse-temp*")))
286 (unwind-protect
287 (if debug-on-error
288 (with-current-buffer ,temp-buffer
289 ,@body)
290 (condition-case err
291 (with-current-buffer ,temp-buffer
292 ,@body)
293 (error
294 (if (and (boundp 'muse-batch-publishing-p)
295 muse-batch-publishing-p)
296 (progn
297 (message "%s: Error occured: %s"
298 (muse-page-name) err)
299 (backtrace))
300 (muse-display-warning
301 (format (concat "An error occurred while publishing"
302 " %s:\n %s\n\nSet debug-on-error to"
303 " `t' if you would like a backtrace.")
304 (muse-page-name) err))))))
305 (when (buffer-live-p ,temp-buffer)
306 (with-current-buffer ,temp-buffer
307 (set-buffer-modified-p nil))
308 (unless debug-on-error (kill-buffer ,temp-buffer)))))))
310 (put 'muse-with-temp-buffer 'lisp-indent-function 0)
311 (put 'muse-with-temp-buffer 'edebug-form-spec '(body))
313 (defun muse-insert-file-contents (filename &optional visit)
314 "Insert the contents of file FILENAME after point.
315 Do character code conversion, but none of the other unnecessary
316 things like format decoding or `find-file-hook'.
318 If VISIT is non-nil, the buffer's visited filename
319 and last save file modtime are set, and it is marked unmodified.
320 If visiting and the file does not exist, visiting is completed
321 before the error is signaled."
322 (let ((format-alist nil)
323 (after-insert-file-functions nil)
324 (find-buffer-file-type-function
325 (if (fboundp 'find-buffer-file-type)
326 (symbol-function 'find-buffer-file-type)
327 nil))
328 (inhibit-file-name-handlers
329 (append '(jka-compr-handler image-file-handler)
330 inhibit-file-name-handlers))
331 (inhibit-file-name-operation 'insert-file-contents))
332 (unwind-protect
333 (progn
334 (fset 'find-buffer-file-type (lambda (filename) t))
335 (insert-file-contents filename visit))
336 (if find-buffer-file-type-function
337 (fset 'find-buffer-file-type find-buffer-file-type-function)
338 (fmakunbound 'find-buffer-file-type)))))
340 (defun muse-write-file (filename)
341 "Write current buffer into file FILENAME.
342 Unlike `write-file', this does not visit the file, try to back it
343 up, or interact with vc.el in any way.
345 If the file was not written successfully, return nil. Otherwise,
346 return non-nil."
347 (let ((backup-inhibited t)
348 (buffer-file-name filename)
349 (buffer-file-truename (file-truename filename)))
350 (save-current-buffer
351 (save-restriction
352 (widen)
353 (if (not (file-writable-p buffer-file-name))
354 (prog1 nil
355 (muse-display-warning
356 (format "Cannot write file %s:\n %s" buffer-file-name
357 (let ((dir (file-name-directory buffer-file-name)))
358 (if (not (file-directory-p dir))
359 (if (file-exists-p dir)
360 (format "%s is not a directory" dir)
361 (format "No directory named %s exists" dir))
362 (if (not (file-exists-p buffer-file-name))
363 (format "Directory %s write-protected" dir)
364 "File is write-protected"))))))
365 (let ((coding-system-for-write
366 (or (and (boundp 'save-buffer-coding-system)
367 save-buffer-coding-system)
368 coding-system-for-write)))
369 (write-region (point-min) (point-max) buffer-file-name))
370 (when (boundp 'last-file-coding-system-used)
371 (when (boundp 'buffer-file-coding-system-explicit)
372 (setq buffer-file-coding-system-explicit
373 last-coding-system-used))
374 (if save-buffer-coding-system
375 (setq save-buffer-coding-system last-coding-system-used)
376 (setq buffer-file-coding-system last-coding-system-used)))
377 t)))))
379 (defun muse-collect-alist (list element &optional test)
380 "Collect items from LIST whose car is equal to ELEMENT.
381 If TEST is specified, use it to compare ELEMENT."
382 (unless test (setq test 'equal))
383 (let ((items nil))
384 (dolist (item list)
385 (when (funcall test element (car item))
386 (setq items (cons item items))))
387 items))
389 (defmacro muse-sort-with-closure (list predicate closure)
390 "Sort LIST, stably, comparing elements using PREDICATE.
391 Returns the sorted list. LIST is modified by side effects.
392 PREDICATE is called with two elements of list and CLOSURE.
393 PREDICATE should return non-nil if the first element should sort
394 before the second."
395 `(sort ,list (lambda (a b) (funcall ,predicate a b ,closure))))
397 (put 'muse-sort-with-closure 'lisp-indent-function 0)
398 (put 'muse-sort-with-closure 'edebug-form-spec '(form function-form form))
400 (defun muse-sort-by-rating (rated-list &optional test)
401 "Sort RATED-LIST according to the rating of each element.
402 The rating is stripped out in the returned list.
403 Default sorting is highest-first.
405 If TEST if specified, use it to sort the list. The default test is '>."
406 (unless test (setq test '>))
407 (mapcar (function cdr)
408 (muse-sort-with-closure
409 rated-list
410 (lambda (a b closure)
411 (let ((na (numberp (car a)))
412 (nb (numberp (car b))))
413 (cond ((and na nb) (funcall closure (car a) (car b)))
414 (na (not nb))
415 (t nil))))
416 test)))
418 (defun muse-escape-specials-in-string (specials string &optional reverse)
419 "Apply the transformations in SPECIALS to STRING.
421 The transforms should form a fully reversible and non-ambiguous
422 syntax when STRING is parsed from left to right.
424 If REVERSE is specified, reverse an already-escaped string."
425 (let ((rules (mapcar (lambda (rule)
426 (cons (regexp-quote (if reverse
427 (cdr rule)
428 (car rule)))
429 (if reverse (car rule) (cdr rule))))
430 specials)))
431 (with-temp-buffer
432 (insert string)
433 (goto-char (point-min))
434 (save-match-data
435 (while (not (eobp))
436 (unless (catch 'found
437 (dolist (rule rules)
438 (when (looking-at (car rule))
439 (replace-match (cdr rule) t t)
440 (throw 'found t))))
441 (forward-char))))
442 (buffer-string))))
444 (defun muse-trim-whitespace (string)
445 "Return a version of STRING with no initial nor trailing whitespace."
446 (muse-replace-regexp-in-string
447 (concat "\\`[" muse-regexp-blank "]+\\|[" muse-regexp-blank "]+\\'")
448 "" string))
450 (defun muse-path-sans-extension (path)
451 "Return PATH sans final \"extension\".
453 The extension, in a file name, is the part that follows the last `.',
454 except that a leading `.', if any, doesn't count.
456 This differs from `file-name-sans-extension' in that it will
457 never modify the directory part of the path."
458 (concat (file-name-directory path)
459 (file-name-nondirectory (file-name-sans-extension path))))
461 ;; The following code was extracted from cl
463 (defun muse-const-expr-p (x)
464 (cond ((consp x)
465 (or (eq (car x) 'quote)
466 (and (memq (car x) '(function function*))
467 (or (symbolp (nth 1 x))
468 (and (eq (and (consp (nth 1 x))
469 (car (nth 1 x))) 'lambda) 'func)))))
470 ((symbolp x) (and (memq x '(nil t)) t))
471 (t t)))
473 (put 'muse-assertion-failed 'error-conditions '(error))
474 (put 'muse-assertion-failed 'error-message "Assertion failed")
476 (defun muse-list* (arg &rest rest)
477 "Return a new list with specified args as elements, cons'd to last arg.
478 Thus, `(list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to
479 `(cons A (cons B (cons C D)))'."
480 (cond ((not rest) arg)
481 ((not (cdr rest)) (cons arg (car rest)))
482 (t (let* ((n (length rest))
483 (copy (copy-sequence rest))
484 (last (nthcdr (- n 2) copy)))
485 (setcdr last (car (cdr last)))
486 (cons arg copy)))))
488 (defmacro muse-assert (form &optional show-args string &rest args)
489 "Verify that FORM returns non-nil; signal an error if not.
490 Second arg SHOW-ARGS means to include arguments of FORM in message.
491 Other args STRING and ARGS... are arguments to be passed to `error'.
492 They are not evaluated unless the assertion fails. If STRING is
493 omitted, a default message listing FORM itself is used."
494 (let ((sargs
495 (and show-args
496 (delq nil (mapcar
497 (function
498 (lambda (x)
499 (and (not (muse-const-expr-p x)) x)))
500 (cdr form))))))
501 (list 'progn
502 (list 'or form
503 (if string
504 (muse-list* 'error string (append sargs args))
505 (list 'signal '(quote muse-assertion-failed)
506 (muse-list* 'list (list 'quote form) sargs))))
507 nil)))
509 ;; Compatibility functions
511 (if (fboundp 'looking-back)
512 (defalias 'muse-looking-back 'looking-back)
513 (defun muse-looking-back (regexp &optional limit &rest ignored)
514 (save-excursion
515 (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t))))
517 (eval-and-compile
518 (if (fboundp 'line-end-position)
519 (defalias 'muse-line-end-position 'line-end-position)
520 (defun muse-line-end-position (&optional n)
521 (save-excursion (end-of-line n) (point))))
523 (if (fboundp 'line-beginning-position)
524 (defalias 'muse-line-beginning-position 'line-beginning-position)
525 (defun muse-line-beginning-position (&optional n)
526 (save-excursion (beginning-of-line n) (point))))
528 (if (fboundp 'match-string-no-properties)
529 (defalias 'muse-match-string-no-properties 'match-string-no-properties)
530 (defun muse-match-string-no-properties (num &optional string)
531 (match-string num string))))
533 (defun muse-replace-regexp-in-string (regexp replacement text &optional fixedcase literal)
534 "Replace REGEXP with REPLACEMENT in TEXT.
536 Return a new string containing the replacements.
538 If fourth arg FIXEDCASE is non-nil, do not alter case of replacement text.
539 If fifth arg LITERAL is non-nil, insert REPLACEMENT literally."
540 (cond
541 ((and (featurep 'xemacs) (fboundp 'replace-in-string))
542 (replace-in-string text regexp replacement literal))
543 ((fboundp 'replace-regexp-in-string)
544 (replace-regexp-in-string regexp replacement text fixedcase literal))
545 (t (let ((repl-len (length replacement))
546 start)
547 (unless (string= regexp "")
548 (save-match-data
549 (while (setq start (string-match regexp text start))
550 (setq start (+ start repl-len)
551 text (replace-match replacement fixedcase literal
552 text))))))
553 text)))
555 (if (fboundp 'add-to-invisibility-spec)
556 (defalias 'muse-add-to-invisibility-spec 'add-to-invisibility-spec)
557 (defun muse-add-to-invisibility-spec (element)
558 "Add ELEMENT to `buffer-invisibility-spec'.
559 See documentation for `buffer-invisibility-spec' for the kind of elements
560 that can be added."
561 (if (eq buffer-invisibility-spec t)
562 (setq buffer-invisibility-spec (list t)))
563 (setq buffer-invisibility-spec
564 (cons element buffer-invisibility-spec))))
566 (if (fboundp 'read-directory-name)
567 (defalias 'muse-read-directory-name 'read-directory-name)
568 (defun muse-read-directory-name (prompt &optional dir default-dirname mustmatch initial)
569 "Read directory name - see `read-file-name' for details."
570 (unless dir
571 (setq dir default-directory))
572 (read-file-name prompt dir (or default-dirname
573 (if initial (expand-file-name initial dir)
574 dir))
575 mustmatch initial)))
577 (defun muse-file-remote-p (file)
578 "Test whether FILE specifies a location on a remote system.
579 Return non-nil if the location is indeed remote.
581 For example, the filename \"/user@host:/foo\" specifies a location
582 on the system \"/user@host:\"."
583 (cond ((fboundp 'file-remote-p)
584 (file-remote-p file))
585 ((fboundp 'tramp-handle-file-remote-p)
586 (tramp-handle-file-remote-p file))
587 ((and (boundp 'ange-ftp-name-format)
588 (string-match (car ange-ftp-name-format) file))
590 (t nil)))
592 (if (fboundp 'delete-and-extract-region)
593 (defalias 'muse-delete-and-extract-region 'delete-and-extract-region)
594 (defun muse-delete-and-extract-region (start end)
595 "Delete the text between START and END and return it."
596 (prog1 (buffer-substring start end)
597 (delete-region start end))))
599 ;; Set face globally in a predictable fashion
600 (defun muse-copy-face (old new)
601 "Copy face OLD to NEW."
602 (if (featurep 'xemacs)
603 (copy-face old new 'all)
604 (copy-face old new)))
606 ;; Widget compatibility functions
608 (defun muse-widget-type-value-create (widget)
609 "Convert and instantiate the value of the :type attribute of WIDGET.
610 Store the newly created widget in the :children attribute.
612 The value of the :type attribute should be an unconverted widget type."
613 (let ((value (widget-get widget :value))
614 (type (widget-get widget :type)))
615 (widget-put widget :children
616 (list (widget-create-child-value widget
617 (widget-convert type)
618 value)))))
620 (defun muse-widget-child-value-get (widget)
621 "Get the value of the first member of :children in WIDGET."
622 (widget-value (car (widget-get widget :children))))
624 (defun muse-widget-type-match (widget value)
625 "Non-nil if the :type value of WIDGET matches VALUE.
627 The value of the :type attribute should be an unconverted widget type."
628 (widget-apply (widget-convert (widget-get widget :type)) :match value))
630 ;; Link-handling functions and variables
632 (defun muse-get-link (&optional target)
633 "Based on the match data, retrieve the link.
634 Use TARGET to get the string, if it is specified."
635 (muse-match-string-no-properties 1 target))
637 (defun muse-get-link-desc (&optional target)
638 "Based on the match data, retrieve the link description.
639 Use TARGET to get the string, if it is specified."
640 (muse-match-string-no-properties 2 target))
642 (defvar muse-link-specials
643 '(("[" . "%5B")
644 ("]" . "%5D")
645 ("%" . "%%"))
646 "Syntax used for escaping and unescaping links.
647 This allows brackets to occur in explicit links as long as you
648 use the standard Muse functions to create them.")
650 (defun muse-link-escape (text)
651 "Escape characters in TEXT that conflict with the explicit link
652 regexp."
653 (when (stringp text)
654 (muse-escape-specials-in-string muse-link-specials text)))
656 (defun muse-link-unescape (text)
657 "Un-escape characters in TEXT that conflict with the explicit
658 link regexp."
659 (when (stringp text)
660 (muse-escape-specials-in-string muse-link-specials text t)))
662 (defun muse-handle-url (&optional string)
663 "If STRING or point has a URL, match and return it."
664 (if (if string (string-match muse-url-regexp string)
665 (looking-at muse-url-regexp))
666 (match-string 0 string)))
668 (defcustom muse-implicit-link-functions '(muse-handle-url)
669 "A list of functions to handle an implicit link.
670 An implicit link is one that is not surrounded by brackets.
672 By default, Muse handles URLs only.
673 If you want to handle WikiWords, load muse-wiki.el."
674 :type 'hook
675 :options '(muse-handle-url)
676 :group 'muse)
678 (defun muse-handle-implicit-link (&optional link)
679 "Handle implicit links. If LINK is not specified, look at point.
680 An implicit link is one that is not surrounded by brackets.
681 By default, Muse handles URLs only.
682 If you want to handle WikiWords, load muse-wiki.el.
684 This function modifies the match data so that match 0 is the
685 link.
687 The match data is restored after each unsuccessful handler
688 function call. If LINK is specified, only restore at very end.
690 This behavior is needed because the part of the buffer that
691 `muse-implicit-link-regexp' matches must be narrowed to the part
692 that is an accepted link."
693 (let ((funcs muse-implicit-link-functions)
694 (res nil)
695 (data (match-data t)))
696 (while funcs
697 (setq res (funcall (car funcs) link))
698 (if res
699 (setq funcs nil)
700 (unless link (set-match-data data))
701 (setq funcs (cdr funcs))))
702 (when link (set-match-data data))
703 res))
705 (defcustom muse-explicit-link-functions nil
706 "A list of functions to handle an explicit link.
707 An explicit link is one [[like][this]] or [[this]]."
708 :type 'hook
709 :group 'muse)
711 (defun muse-handle-explicit-link (&optional link)
712 "Handle explicit links. If LINK is not specified, look at point.
713 An explicit link is one that looks [[like][this]] or [[this]].
715 The match data is preserved. If no handlers are able to process
716 LINK, return LINK (if specified) or the 1st match string. If
717 LINK is not specified, it is assumed that Muse has matched
718 against `muse-explicit-link-regexp' before calling this
719 function."
720 (let ((funcs muse-explicit-link-functions)
721 (res nil))
722 (save-match-data
723 (while funcs
724 (setq res (funcall (car funcs) link))
725 (if res
726 (setq funcs nil)
727 (setq funcs (cdr funcs)))))
728 (muse-link-unescape
729 (if res
731 (or link (muse-get-link))))))
733 ;; Movement functions
735 (defun muse-list-item-type (str)
736 "Determine the type of list given STR.
737 Returns either 'ul, 'ol, 'dl-term, 'dl-entry, or nil."
738 (save-match-data
739 (cond ((or (string= str "")
740 (< (length str) 2))
741 nil)
742 ((string-match muse-dl-entry-regexp str)
743 'dl-entry)
744 ((string-match muse-dl-term-regexp str)
745 'dl-term)
746 ((string-match muse-ol-item-regexp str)
747 'ol)
748 ((string-match muse-ul-item-regexp str)
749 'ul)
750 (t nil))))
752 (defun muse-list-item-critical-point (&optional offset)
753 "Figure out where the important markup character for the
754 currently-matched list item is.
756 If OFFSET is specified, it is the number of groupings outside of
757 the contents of `muse-list-item-regexp'."
758 (unless offset (setq offset 0))
759 (if (match-end (+ offset 2))
760 ;; at a definition list
761 (match-end (+ offset 2))
762 ;; at a different kind of list
763 (match-beginning (+ offset 1))))
765 (defun muse-forward-paragraph (&optional pattern)
766 "Move forward safely by one paragraph, or according to PATTERN."
767 (when (get-text-property (point) 'end-list)
768 (goto-char (next-single-property-change (point) 'end-list)))
769 (setq pattern (if pattern
770 (concat "^\\(?:" pattern "\\|\n\\|\\'\\)")
771 "^\\s-*\\(\n\\|\\'\\)"))
772 (let ((next-list-end (or (next-single-property-change (point) 'end-list)
773 (point-max))))
774 (forward-line 1)
775 (if (re-search-forward pattern nil t)
776 (goto-char (match-beginning 0))
777 (goto-char (point-max)))
778 (when (> (point) next-list-end)
779 (goto-char next-list-end))))
781 (defun muse-forward-list-item-1 (type empty-line indented-line)
782 "Determine whether a nested list item is after point."
783 (if (match-beginning 1)
784 ;; if we are given a dl entry, skip past everything on the same
785 ;; level, except for other dl entries
786 (and (eq type 'dl-entry)
787 (not (eq (char-after (match-beginning 2)) ?\:)))
788 ;; blank line encountered with no list item on the same
789 ;; level after it
790 (let ((beg (point)))
791 (forward-line 1)
792 (if (save-match-data
793 (and (looking-at indented-line)
794 (not (looking-at empty-line))))
795 ;; found that this blank line is followed by some
796 ;; indentation, plus other text, so we'll keep
797 ;; going
799 (goto-char beg)
800 nil))))
802 (defun muse-forward-list-item (type indent &optional no-skip-nested)
803 "Move forward to the next item of TYPE.
804 Return non-nil if successful, nil otherwise.
805 The beginning indentation is given by INDENT.
807 If NO-SKIP-NESTED is non-nil, do not skip past nested items.
808 Note that if you desire this behavior, you will also need to
809 provide a very liberal INDENT value, such as
810 \(concat \"[\" muse-regexp-blank \"]*\")."
811 (let* ((list-item (format muse-list-item-regexp indent))
812 (empty-line (concat "^[" muse-regexp-blank "]*\n"))
813 (indented-line (concat "^" indent "[" muse-regexp-blank "]"))
814 (list-pattern (concat "\\(?:" empty-line "\\)?"
815 "\\(" list-item "\\)")))
816 (while (progn
817 (muse-forward-paragraph list-pattern)
818 ;; make sure we don't go past boundary
819 (and (not (or (get-text-property (point) 'end-list)
820 (>= (point) (point-max))))
821 ;; move past markup that is part of another construct
822 (or (and (match-beginning 1)
823 (or (get-text-property
824 (muse-list-item-critical-point 1) 'muse-link)
825 (and (derived-mode-p 'muse-mode)
826 (get-text-property
827 (muse-list-item-critical-point 1)
828 'face))))
829 ;; skip nested items
830 (and (not no-skip-nested)
831 (muse-forward-list-item-1 type empty-line
832 indented-line))))))
833 (cond ((or (get-text-property (point) 'end-list)
834 (>= (point) (point-max)))
835 ;; at a list boundary, so stop
836 nil)
837 ((let ((str (when (match-beginning 2)
838 ;; get the entire line
839 (save-excursion
840 (goto-char (match-beginning 2))
841 (buffer-substring (muse-line-beginning-position)
842 (muse-line-end-position))))))
843 (and str (eq type (muse-list-item-type str))))
844 ;; same type, so indicate that there are more items to be
845 ;; parsed
846 (goto-char (match-beginning 1)))
848 (when (match-beginning 1)
849 (goto-char (match-beginning 1)))
850 ;; move to just before foreign list item markup
851 nil))))
853 (defun muse-goto-tag-end (tag nested)
854 "Move forward past the end of TAG.
856 If NESTED is non-nil, look for other instances of this tag that
857 may be nested inside of this tag, and skip past them."
858 (if (not nested)
859 (search-forward (concat "</" tag ">") nil t)
860 (let ((nesting 1)
861 (tag-regexp (concat "\\(<\\(/?\\)" tag "\\([ >]\\)\\)"))
862 (match-found nil))
863 (while (and (> nesting 0)
864 (setq match-found (re-search-forward tag-regexp nil t)))
865 ;; for the sake of font-locking code, skip matches in comments
866 (unless (get-text-property (match-beginning 0) 'muse-comment)
867 (if (string-equal (match-string 2) "/")
868 (and (string-equal (match-string 3) ">")
869 (setq nesting (1- nesting)))
870 (setq nesting (1+ nesting)))))
871 match-found)))
873 ;;; muse.el ends here