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