Release Emacs Muse 3.10
[muse-el.git] / lisp / muse.el
blob471e7880ca9a742acab95d9bb249a6dcc6c0fd89
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.10
8 ;; Date: Thu 16-Aug-2007
9 ;; Keywords: hypermedia
10 ;; Author: John Wiegley (johnw AT gnu DOT org)
11 ;; Maintainer: Michael Olson <mwolson@gnu.org>
12 ;; Description: An authoring and publishing tool for Emacs
13 ;; URL: http://mwolson.org/projects/EmacsMuse.html
14 ;; Compatibility: Emacs21 XEmacs21 Emacs22
16 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
18 ;; Emacs Muse is free software; you can redistribute it and/or modify
19 ;; it under the terms of the GNU General Public License as published
20 ;; by the Free Software Foundation; either version 3, or (at your
21 ;; option) any later version.
23 ;; Emacs Muse is distributed in the hope that it will be useful, but
24 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 ;; General Public License for more details.
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with Emacs Muse; see the file COPYING. If not, write to the
30 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
31 ;; Boston, MA 02110-1301, USA.
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.10"
52 "The version of Muse currently loaded")
54 (defun muse-version (&optional insert)
55 "Display the version of Muse that is currently loaded.
56 If INSERT is non-nil, insert the text instead of displaying it."
57 (interactive "P")
58 (if insert
59 (insert muse-version)
60 (message muse-version)))
62 (defgroup muse nil
63 "Options controlling the behavior of Muse.
64 The markup used by Muse is intended to be very friendly to people
65 familiar with Emacs."
66 :group 'hypermedia)
68 (defvar muse-under-windows-p (memq system-type '(ms-dos windows-nt)))
70 (provide 'muse)
72 (require 'wid-edit)
73 (require 'muse-regexps)
75 (defvar muse-update-values-hook nil
76 "Hook for values that are automatically generated.
77 This is to be used by add-on modules for Muse.
78 It is run just before colorizing or publishing a buffer.")
80 ;; Default file extension
82 ;; By default, use the .muse file extension.
83 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.muse\\'" . muse-mode-choose-mode))
85 ;; We need to have this at top-level, as well, so that any Muse or
86 ;; Planner documents opened during init will just work.
87 (add-to-list 'auto-mode-alist '("\\.muse\\'" . muse-mode-choose-mode))
89 (eval-when-compile
90 (defvar muse-ignored-extensions))
92 (defvar muse-ignored-extensions-regexp nil
93 "A regexp of extensions to omit from the ending of a Muse page name.
94 This is autogenerated from `muse-ignored-extensions'.")
96 (defun muse-update-file-extension (sym val)
97 "Update the value of `muse-file-extension'."
98 (when (and (featurep 'muse-mode)
99 (boundp sym) (stringp (symbol-value sym))
100 (or (not (stringp val))
101 (not (string= (symbol-value sym) val))))
102 ;; remove old auto-mode-alist association
103 (setq auto-mode-alist
104 (delete (cons (concat "\\." (symbol-value sym) "\\'")
105 'muse-mode-choose-mode)
106 auto-mode-alist)))
107 (set sym val)
108 ;; associate the new file extension with muse-mode
109 (when (and (featurep 'muse-mode)
110 (stringp val)
111 (or (not (stringp (symbol-value sym)))
112 (not (string= (symbol-value sym) val))))
113 (add-to-list 'auto-mode-alist
114 (cons (concat "\\." val "\\'")
115 'muse-mode-choose-mode)))
116 ;; update the ignored extensions regexp
117 (when (and (fboundp 'muse-update-ignored-extensions-regexp)
118 (or (not (stringp (symbol-value sym)))
119 (not (stringp val))
120 (not (string= (symbol-value sym) val))))
121 (muse-update-ignored-extensions-regexp
122 'muse-ignored-extensions muse-ignored-extensions)))
124 (defcustom muse-file-extension "muse"
125 "File extension of Muse files. Omit the period at the beginning.
126 If you don't want Muse files to have an extension, set this to nil."
127 :type '(choice
128 (const :tag "None" nil)
129 (string))
130 :set 'muse-update-file-extension
131 :group 'muse)
133 (defcustom muse-completing-read-function 'completing-read
134 "Function to call when prompting user to choose between a list of options.
135 This should take the same arguments as `completing-read'."
136 :type 'function
137 :group 'muse)
139 (defun muse-update-ignored-extensions-regexp (sym val)
140 "Update the value of `muse-ignored-extensions-regexp'."
141 (set sym val)
142 (if val
143 (setq muse-ignored-extensions-regexp
144 (concat "\\.\\("
145 (regexp-quote (or muse-file-extension "")) "\\|"
146 (mapconcat 'identity val "\\|")
147 "\\)\\'"))
148 (setq muse-ignored-extensions-regexp
149 (if muse-file-extension
150 (concat "\\.\\(" muse-file-extension "\\)\\'")
151 nil))))
153 (add-hook 'muse-update-values-hook
154 (lambda ()
155 (muse-update-ignored-extensions-regexp
156 'muse-ignored-extensions muse-ignored-extensions)))
158 (defcustom muse-ignored-extensions '("bz2" "gz" "[Zz]")
159 "A list of extensions to omit from the ending of a Muse page name.
160 These are regexps.
162 Don't put a period at the beginning of each extension unless you
163 understand that it is part of a regexp."
164 :type '(repeat (regexp :tag "Extension"))
165 :set 'muse-update-ignored-extensions-regexp
166 :group 'muse)
168 (defun muse-update-file-extension-after-init ()
169 ;; This is short, but it has to be a function, otherwise Emacs21
170 ;; does not load it properly when running after-init-hook
171 (muse-update-file-extension 'muse-file-extension muse-file-extension))
173 ;; Once the user's init file has been processed, determine whether
174 ;; they want a file extension
175 (add-hook 'after-init-hook 'muse-update-file-extension-after-init)
177 ;; URL protocols
179 (require 'muse-protocols)
181 ;; Helper functions
183 (defsubst muse-delete-file-if-exists (file)
184 (when (file-exists-p file)
185 (delete-file file)
186 (message "Removed %s" file)))
188 (defsubst muse-time-less-p (t1 t2)
189 "Say whether time T1 is less than time T2."
190 (or (< (car t1) (car t2))
191 (and (= (car t1) (car t2))
192 (< (nth 1 t1) (nth 1 t2)))))
194 (eval-when-compile
195 (defvar muse-publishing-current-file nil))
197 (defun muse-current-file ()
198 "Return the name of the currently visited or published file."
199 (or (and (boundp 'muse-publishing-current-file)
200 muse-publishing-current-file)
201 (buffer-file-name)
202 (concat default-directory (buffer-name))))
204 (defun muse-page-name (&optional name)
205 "Return the canonical form of a Muse page name.
207 What this means is that the directory part of NAME is removed,
208 and the file extensions in `muse-ignored-extensions' are also
209 removed from NAME."
210 (save-match-data
211 (unless (and name (not (string= name "")))
212 (setq name (muse-current-file)))
213 (if name
214 (let ((page (file-name-nondirectory name)))
215 (if (and muse-ignored-extensions-regexp
216 (string-match muse-ignored-extensions-regexp page))
217 (replace-match "" t t page)
218 page)))))
220 (defun muse-display-warning (message)
221 "Display the given MESSAGE as a warning."
222 (if (fboundp 'display-warning)
223 (display-warning 'muse message
224 (if (featurep 'xemacs)
225 'warning
226 :warning))
227 (let ((buf (get-buffer-create "*Muse warnings*")))
228 (with-current-buffer buf
229 (goto-char (point-max))
230 (insert "Warning (muse): " message)
231 (unless (bolp)
232 (newline)))
233 (display-buffer buf)
234 (sit-for 0))))
236 (defun muse-eval-lisp (form)
237 "Evaluate the given form and return the result as a string."
238 (require 'pp)
239 (save-match-data
240 (condition-case err
241 (let ((object (eval (read form))))
242 (cond
243 ((stringp object) object)
244 ((and (listp object)
245 (not (eq object nil)))
246 (let ((string (pp-to-string object)))
247 (substring string 0 (1- (length string)))))
248 ((numberp object)
249 (number-to-string object))
250 ((eq object nil) "")
252 (pp-to-string object))))
253 (error
254 (muse-display-warning (format "%s: Error evaluating %s: %s"
255 (muse-page-name) form err))
256 "; INVALID LISP CODE"))))
258 (defmacro muse-with-temp-buffer (&rest body)
259 "Create a temporary buffer, and evaluate BODY there like `progn'.
260 See also `with-temp-file' and `with-output-to-string'.
262 Unlike `with-temp-buffer', this will never attempt to save the
263 temp buffer. It is meant to be used along with
264 `insert-file-contents' or `muse-insert-file-contents'.
266 Additionally, if `debug-on-error' is set to t, keep the buffer
267 around for debugging purposes rather than removing it."
268 (let ((temp-buffer (make-symbol "temp-buffer")))
269 `(let ((,temp-buffer (generate-new-buffer " *muse-temp*")))
270 (unwind-protect
271 (if debug-on-error
272 (with-current-buffer ,temp-buffer
273 ,@body)
274 (condition-case err
275 (with-current-buffer ,temp-buffer
276 ,@body)
277 (error
278 (if (and (boundp 'muse-batch-publishing-p)
279 muse-batch-publishing-p)
280 (progn
281 (message "%s: Error occured: %s"
282 (muse-page-name) err)
283 (backtrace))
284 (muse-display-warning
285 (format (concat "An error occurred while publishing"
286 " %s:\n %s\n\nSet debug-on-error to"
287 " `t' if you would like a backtrace.")
288 (muse-page-name) err))))))
289 (when (buffer-live-p ,temp-buffer)
290 (with-current-buffer ,temp-buffer
291 (set-buffer-modified-p nil))
292 (unless debug-on-error (kill-buffer ,temp-buffer)))))))
294 (put 'muse-with-temp-buffer 'lisp-indent-function 0)
295 (put 'muse-with-temp-buffer 'edebug-form-spec '(body))
297 (defun muse-insert-file-contents (filename &optional visit)
298 "Insert the contents of file FILENAME after point.
299 Do character code conversion, but none of the other unnecessary
300 things like format decoding or `find-file-hook'.
302 If VISIT is non-nil, the buffer's visited filename
303 and last save file modtime are set, and it is marked unmodified.
304 If visiting and the file does not exist, visiting is completed
305 before the error is signaled."
306 (let ((format-alist nil)
307 (after-insert-file-functions nil)
308 (find-buffer-file-type-function
309 (if (fboundp 'find-buffer-file-type)
310 (symbol-function 'find-buffer-file-type)
311 nil))
312 (inhibit-file-name-handlers
313 (append '(jka-compr-handler image-file-handler)
314 inhibit-file-name-handlers))
315 (inhibit-file-name-operation 'insert-file-contents))
316 (unwind-protect
317 (progn
318 (fset 'find-buffer-file-type (lambda (filename) t))
319 (insert-file-contents filename visit))
320 (if find-buffer-file-type-function
321 (fset 'find-buffer-file-type find-buffer-file-type-function)
322 (fmakunbound 'find-buffer-file-type)))))
324 (defun muse-write-file (filename)
325 "Write current buffer into file FILENAME.
326 Unlike `write-file', this does not visit the file, try to back it
327 up, or interact with vc.el in any way.
329 If the file was not written successfully, return nil. Otherwise,
330 return non-nil."
331 (let ((backup-inhibited t)
332 (buffer-file-name filename)
333 (buffer-file-truename (file-truename filename)))
334 (save-current-buffer
335 (save-restriction
336 (widen)
337 (if (not (file-writable-p buffer-file-name))
338 (prog1 nil
339 (muse-display-warning
340 (format "Cannot write file %s:\n %s" buffer-file-name
341 (let ((dir (file-name-directory buffer-file-name)))
342 (if (not (file-directory-p dir))
343 (if (file-exists-p dir)
344 (format "%s is not a directory" dir)
345 (format "No directory named %s exists" dir))
346 (if (not (file-exists-p buffer-file-name))
347 (format "Directory %s write-protected" dir)
348 "File is write-protected"))))))
349 (let ((coding-system-for-write
350 (or (and (boundp 'save-buffer-coding-system)
351 save-buffer-coding-system)
352 coding-system-for-write)))
353 (write-region (point-min) (point-max) buffer-file-name))
354 (when (boundp 'last-file-coding-system-used)
355 (when (boundp 'buffer-file-coding-system-explicit)
356 (setq buffer-file-coding-system-explicit
357 last-coding-system-used))
358 (if save-buffer-coding-system
359 (setq save-buffer-coding-system last-coding-system-used)
360 (setq buffer-file-coding-system last-coding-system-used)))
361 t)))))
363 (defun muse-collect-alist (list element &optional test)
364 "Collect items from LIST whose car is equal to ELEMENT.
365 If TEST is specified, use it to compare ELEMENT."
366 (unless test (setq test 'equal))
367 (let ((items nil))
368 (dolist (item list)
369 (when (funcall test element (car item))
370 (setq items (cons item items))))
371 items))
373 (defmacro muse-sort-with-closure (list predicate closure)
374 "Sort LIST, stably, comparing elements using PREDICATE.
375 Returns the sorted list. LIST is modified by side effects.
376 PREDICATE is called with two elements of list and CLOSURE.
377 PREDICATE should return non-nil if the first element should sort
378 before the second."
379 `(sort ,list (lambda (a b) (funcall ,predicate a b ,closure))))
381 (put 'muse-sort-with-closure 'lisp-indent-function 0)
382 (put 'muse-sort-with-closure 'edebug-form-spec '(form function-form form))
384 (defun muse-sort-by-rating (rated-list &optional test)
385 "Sort RATED-LIST according to the rating of each element.
386 The rating is stripped out in the returned list.
387 Default sorting is highest-first.
389 If TEST if specified, use it to sort the list. The default test is '>."
390 (unless test (setq test '>))
391 (mapcar (function cdr)
392 (muse-sort-with-closure
393 rated-list
394 (lambda (a b closure)
395 (let ((na (numberp (car a)))
396 (nb (numberp (car b))))
397 (cond ((and na nb) (funcall closure (car a) (car b)))
398 (na (not nb))
399 (t nil))))
400 test)))
402 (defun muse-escape-specials-in-string (specials string &optional reverse)
403 "Apply the transformations in SPECIALS to STRING.
405 The transforms should form a fully reversible and non-ambiguous
406 syntax when STRING is parsed from left to right.
408 If REVERSE is specified, reverse an already-escaped string."
409 (let ((rules (mapcar (lambda (rule)
410 (cons (regexp-quote (if reverse
411 (cdr rule)
412 (car rule)))
413 (if reverse (car rule) (cdr rule))))
414 specials)))
415 (with-temp-buffer
416 (insert string)
417 (goto-char (point-min))
418 (save-match-data
419 (while (not (eobp))
420 (unless (catch 'found
421 (dolist (rule rules)
422 (when (looking-at (car rule))
423 (replace-match (cdr rule) t t)
424 (throw 'found t))))
425 (forward-char))))
426 (buffer-string))))
428 (defun muse-trim-whitespace (string)
429 "Return a version of STRING with no initial nor trailing whitespace."
430 (muse-replace-regexp-in-string
431 (concat "\\`[" muse-regexp-blank "]+\\|[" muse-regexp-blank "]+\\'")
432 "" string))
434 (defun muse-path-sans-extension (path)
435 "Return PATH sans final \"extension\".
437 The extension, in a file name, is the part that follows the last `.',
438 except that a leading `.', if any, doesn't count.
440 This differs from `file-name-sans-extension' in that it will
441 never modify the directory part of the path."
442 (concat (file-name-directory path)
443 (file-name-nondirectory (file-name-sans-extension path))))
445 ;; The following code was extracted from cl
447 (defun muse-const-expr-p (x)
448 (cond ((consp x)
449 (or (eq (car x) 'quote)
450 (and (memq (car x) '(function function*))
451 (or (symbolp (nth 1 x))
452 (and (eq (and (consp (nth 1 x))
453 (car (nth 1 x))) 'lambda) 'func)))))
454 ((symbolp x) (and (memq x '(nil t)) t))
455 (t t)))
457 (put 'muse-assertion-failed 'error-conditions '(error))
458 (put 'muse-assertion-failed 'error-message "Assertion failed")
460 (defun muse-list* (arg &rest rest)
461 "Return a new list with specified args as elements, cons'd to last arg.
462 Thus, `(list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to
463 `(cons A (cons B (cons C D)))'."
464 (cond ((not rest) arg)
465 ((not (cdr rest)) (cons arg (car rest)))
466 (t (let* ((n (length rest))
467 (copy (copy-sequence rest))
468 (last (nthcdr (- n 2) copy)))
469 (setcdr last (car (cdr last)))
470 (cons arg copy)))))
472 (defmacro muse-assert (form &optional show-args string &rest args)
473 "Verify that FORM returns non-nil; signal an error if not.
474 Second arg SHOW-ARGS means to include arguments of FORM in message.
475 Other args STRING and ARGS... are arguments to be passed to `error'.
476 They are not evaluated unless the assertion fails. If STRING is
477 omitted, a default message listing FORM itself is used."
478 (let ((sargs
479 (and show-args
480 (delq nil (mapcar
481 (function
482 (lambda (x)
483 (and (not (muse-const-expr-p x)) x)))
484 (cdr form))))))
485 (list 'progn
486 (list 'or form
487 (if string
488 (muse-list* 'error string (append sargs args))
489 (list 'signal '(quote muse-assertion-failed)
490 (muse-list* 'list (list 'quote form) sargs))))
491 nil)))
493 ;; Compatibility functions
495 (if (fboundp 'looking-back)
496 (defalias 'muse-looking-back 'looking-back)
497 (defun muse-looking-back (regexp &optional limit &rest ignored)
498 (save-excursion
499 (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t))))
501 (eval-and-compile
502 (if (fboundp 'line-end-position)
503 (defalias 'muse-line-end-position 'line-end-position)
504 (defun muse-line-end-position (&optional n)
505 (save-excursion (end-of-line n) (point))))
507 (if (fboundp 'line-beginning-position)
508 (defalias 'muse-line-beginning-position 'line-beginning-position)
509 (defun muse-line-beginning-position (&optional n)
510 (save-excursion (beginning-of-line n) (point))))
512 (if (fboundp 'match-string-no-properties)
513 (defalias 'muse-match-string-no-properties 'match-string-no-properties)
514 (defun muse-match-string-no-properties (num &optional string)
515 (match-string num string))))
517 (defun muse-replace-regexp-in-string (regexp replacement text &optional fixedcase literal)
518 "Replace REGEXP with REPLACEMENT in TEXT.
520 Return a new string containing the replacements.
522 If fourth arg FIXEDCASE is non-nil, do not alter case of replacement text.
523 If fifth arg LITERAL is non-nil, insert REPLACEMENT literally."
524 (cond
525 ((and (featurep 'xemacs) (fboundp 'replace-in-string))
526 (replace-in-string text regexp replacement literal))
527 ((fboundp 'replace-regexp-in-string)
528 (replace-regexp-in-string regexp replacement text fixedcase literal))
529 (t (let ((repl-len (length replacement))
530 start)
531 (unless (string= regexp "")
532 (save-match-data
533 (while (setq start (string-match regexp text start))
534 (setq start (+ start repl-len)
535 text (replace-match replacement fixedcase literal
536 text))))))
537 text)))
539 (if (fboundp 'add-to-invisibility-spec)
540 (defalias 'muse-add-to-invisibility-spec 'add-to-invisibility-spec)
541 (defun muse-add-to-invisibility-spec (element)
542 "Add ELEMENT to `buffer-invisibility-spec'.
543 See documentation for `buffer-invisibility-spec' for the kind of elements
544 that can be added."
545 (if (eq buffer-invisibility-spec t)
546 (setq buffer-invisibility-spec (list t)))
547 (setq buffer-invisibility-spec
548 (cons element buffer-invisibility-spec))))
550 (if (fboundp 'read-directory-name)
551 (defalias 'muse-read-directory-name 'read-directory-name)
552 (defun muse-read-directory-name (prompt &optional dir default-dirname mustmatch initial)
553 "Read directory name - see `read-file-name' for details."
554 (unless dir
555 (setq dir default-directory))
556 (read-file-name prompt dir (or default-dirname
557 (if initial (expand-file-name initial dir)
558 dir))
559 mustmatch initial)))
561 (defun muse-file-remote-p (file)
562 "Test whether FILE specifies a location on a remote system.
563 Return non-nil if the location is indeed remote.
565 For example, the filename \"/user@host:/foo\" specifies a location
566 on the system \"/user@host:\"."
567 (cond ((fboundp 'file-remote-p)
568 (file-remote-p file))
569 ((fboundp 'tramp-handle-file-remote-p)
570 (tramp-handle-file-remote-p file))
571 ((and (boundp 'ange-ftp-name-format)
572 (string-match (car ange-ftp-name-format) file))
574 (t nil)))
576 ;; Set face globally in a predictable fashion
577 (defun muse-copy-face (old new)
578 "Copy face OLD to NEW."
579 (if (featurep 'xemacs)
580 (copy-face old new 'all)
581 (copy-face old new)))
583 ;; Widget compatibility functions
585 (defun muse-widget-type-value-create (widget)
586 "Convert and instantiate the value of the :type attribute of WIDGET.
587 Store the newly created widget in the :children attribute.
589 The value of the :type attribute should be an unconverted widget type."
590 (let ((value (widget-get widget :value))
591 (type (widget-get widget :type)))
592 (widget-put widget :children
593 (list (widget-create-child-value widget
594 (widget-convert type)
595 value)))))
597 (defun muse-widget-child-value-get (widget)
598 "Get the value of the first member of :children in WIDGET."
599 (widget-value (car (widget-get widget :children))))
601 (defun muse-widget-type-match (widget value)
602 "Non-nil if the :type value of WIDGET matches VALUE.
604 The value of the :type attribute should be an unconverted widget type."
605 (widget-apply (widget-convert (widget-get widget :type)) :match value))
607 ;; Link-handling functions and variables
609 (defun muse-get-link (&optional target)
610 "Based on the match data, retrieve the link.
611 Use TARGET to get the string, if it is specified."
612 (muse-match-string-no-properties 1 target))
614 (defun muse-get-link-desc (&optional target)
615 "Based on the match data, retrieve the link description.
616 Use TARGET to get the string, if it is specified."
617 (muse-match-string-no-properties 2 target))
619 (defvar muse-link-specials
620 '(("[" . "%5B")
621 ("]" . "%5D")
622 ("%" . "%%"))
623 "Syntax used for escaping and unescaping links.
624 This allows brackets to occur in explicit links as long as you
625 use the standard Muse functions to create them.")
627 (defun muse-link-escape (text)
628 "Escape characters in TEXT that conflict with the explicit link
629 regexp."
630 (when (stringp text)
631 (muse-escape-specials-in-string muse-link-specials text)))
633 (defun muse-link-unescape (text)
634 "Un-escape characters in TEXT that conflict with the explicit
635 link regexp."
636 (when (stringp text)
637 (muse-escape-specials-in-string muse-link-specials text t)))
639 (defun muse-handle-url (&optional string)
640 "If STRING or point has a URL, match and return it."
641 (if (if string (string-match muse-url-regexp string)
642 (looking-at muse-url-regexp))
643 (match-string 0 string)))
645 (defcustom muse-implicit-link-functions '(muse-handle-url)
646 "A list of functions to handle an implicit link.
647 An implicit link is one that is not surrounded by brackets.
649 By default, Muse handles URLs only.
650 If you want to handle WikiWords, load muse-wiki.el."
651 :type 'hook
652 :options '(muse-handle-url)
653 :group 'muse)
655 (defun muse-handle-implicit-link (&optional link)
656 "Handle implicit links. If LINK is not specified, look at point.
657 An implicit link is one that is not surrounded by brackets.
658 By default, Muse handles URLs only.
659 If you want to handle WikiWords, load muse-wiki.el.
661 This function modifies the match data so that match 0 is the
662 link.
664 The match data is restored after each unsuccessful handler
665 function call. If LINK is specified, only restore at very end.
667 This behavior is needed because the part of the buffer that
668 `muse-implicit-link-regexp' matches must be narrowed to the part
669 that is an accepted link."
670 (let ((funcs muse-implicit-link-functions)
671 (res nil)
672 (data (match-data t)))
673 (while funcs
674 (setq res (funcall (car funcs) link))
675 (if res
676 (setq funcs nil)
677 (unless link (set-match-data data))
678 (setq funcs (cdr funcs))))
679 (when link (set-match-data data))
680 res))
682 (defcustom muse-explicit-link-functions nil
683 "A list of functions to handle an explicit link.
684 An explicit link is one [[like][this]] or [[this]]."
685 :type 'hook
686 :group 'muse)
688 (defun muse-handle-explicit-link (&optional link)
689 "Handle explicit links. If LINK is not specified, look at point.
690 An explicit link is one that looks [[like][this]] or [[this]].
692 The match data is preserved. If no handlers are able to process
693 LINK, return LINK (if specified) or the 1st match string. If
694 LINK is not specified, it is assumed that Muse has matched
695 against `muse-explicit-link-regexp' before calling this
696 function."
697 (let ((funcs muse-explicit-link-functions)
698 (res nil))
699 (save-match-data
700 (while funcs
701 (setq res (funcall (car funcs) link))
702 (if res
703 (setq funcs nil)
704 (setq funcs (cdr funcs)))))
705 (muse-link-unescape
706 (if res
708 (or link (muse-get-link))))))
710 ;; Movement functions
712 (defun muse-list-item-type (str)
713 "Determine the type of list given STR.
714 Returns either 'ul, 'ol, 'dl-term, 'dl-entry, or nil."
715 (save-match-data
716 (cond ((or (string= str "")
717 (< (length str) 2))
718 nil)
719 ((string-match muse-dl-entry-regexp str)
720 'dl-entry)
721 ((string-match muse-dl-term-regexp str)
722 'dl-term)
723 ((string-match muse-ol-item-regexp str)
724 'ol)
725 ((string-match muse-ul-item-regexp str)
726 'ul)
727 (t nil))))
729 (defun muse-list-item-critical-point (&optional offset)
730 "Figure out where the important markup character for the
731 currently-matched list item is.
733 If OFFSET is specified, it is the number of groupings outside of
734 the contents of `muse-list-item-regexp'."
735 (unless offset (setq offset 0))
736 (if (match-end (+ offset 2))
737 ;; at a definition list
738 (match-end (+ offset 2))
739 ;; at a different kind of list
740 (match-beginning (+ offset 1))))
742 (defun muse-forward-paragraph (&optional pattern)
743 "Move forward safely by one paragraph, or according to PATTERN."
744 (when (get-text-property (point) 'end-list)
745 (goto-char (next-single-property-change (point) 'end-list)))
746 (setq pattern (if pattern
747 (concat "^\\(?:" pattern "\\|\n\\|\\'\\)")
748 "^\\s-*\\(\n\\|\\'\\)"))
749 (let ((next-list-end (or (next-single-property-change (point) 'end-list)
750 (point-max))))
751 (forward-line 1)
752 (if (re-search-forward pattern nil t)
753 (goto-char (match-beginning 0))
754 (goto-char (point-max)))
755 (when (> (point) next-list-end)
756 (goto-char next-list-end))))
758 (defun muse-forward-list-item-1 (type empty-line indented-line)
759 "Determine whether a nested list item is after point."
760 (if (match-beginning 1)
761 ;; if we are given a dl entry, skip past everything on the same
762 ;; level, except for other dl entries
763 (and (eq type 'dl-entry)
764 (not (eq (char-after (match-beginning 2)) ?\:)))
765 ;; blank line encountered with no list item on the same
766 ;; level after it
767 (let ((beg (point)))
768 (forward-line 1)
769 (if (save-match-data
770 (and (looking-at indented-line)
771 (not (looking-at empty-line))))
772 ;; found that this blank line is followed by some
773 ;; indentation, plus other text, so we'll keep
774 ;; going
776 (goto-char beg)
777 nil))))
779 (defun muse-forward-list-item (type indent &optional no-skip-nested)
780 "Move forward to the next item of TYPE.
781 Return non-nil if successful, nil otherwise.
782 The beginning indentation is given by INDENT.
784 If NO-SKIP-NESTED is non-nil, do not skip past nested items.
785 Note that if you desire this behavior, you will also need to
786 provide a very liberal INDENT value, such as
787 \(concat \"[\" muse-regexp-blank \"]*\")."
788 (let* ((list-item (format muse-list-item-regexp indent))
789 (empty-line (concat "^[" muse-regexp-blank "]*\n"))
790 (indented-line (concat "^" indent "[" muse-regexp-blank "]"))
791 (list-pattern (concat "\\(?:" empty-line "\\)?"
792 "\\(" list-item "\\)")))
793 (while (progn
794 (muse-forward-paragraph list-pattern)
795 ;; make sure we don't go past boundary
796 (and (not (or (get-text-property (point) 'end-list)
797 (>= (point) (point-max))))
798 ;; move past markup that is part of another construct
799 (or (and (match-beginning 1)
800 (or (get-text-property
801 (muse-list-item-critical-point 1) 'muse-link)
802 (and (derived-mode-p 'muse-mode)
803 (get-text-property
804 (muse-list-item-critical-point 1)
805 'face))))
806 ;; skip nested items
807 (and (not no-skip-nested)
808 (muse-forward-list-item-1 type empty-line
809 indented-line))))))
810 (cond ((or (get-text-property (point) 'end-list)
811 (>= (point) (point-max)))
812 ;; at a list boundary, so stop
813 nil)
814 ((let ((str (when (match-beginning 2)
815 ;; get the entire line
816 (save-excursion
817 (goto-char (match-beginning 2))
818 (buffer-substring (muse-line-beginning-position)
819 (muse-line-end-position))))))
820 (and str (eq type (muse-list-item-type str))))
821 ;; same type, so indicate that there are more items to be
822 ;; parsed
823 (goto-char (match-beginning 1)))
825 (when (match-beginning 1)
826 (goto-char (match-beginning 1)))
827 ;; move to just before foreign list item markup
828 nil))))
830 (defun muse-goto-tag-end (tag nested)
831 "Move forward past the end of TAG.
833 If NESTED is non-nil, look for other instances of this tag that
834 may be nested inside of this tag, and skip past them."
835 (if (not nested)
836 (search-forward (concat "</" tag ">") nil t)
837 (let ((nesting 1)
838 (tag-regexp (concat "\\(<\\(/?\\)" tag ">\\)"))
839 (match-found nil))
840 (while (and (> nesting 0)
841 (setq match-found (re-search-forward tag-regexp nil t)))
842 ;; for the sake of font-locking code, skip matches in comments
843 (unless (get-text-property (match-beginning 0) 'muse-comment)
844 (if (string-equal (match-string 2) "/")
845 (setq nesting (1- nesting))
846 (setq nesting (1+ nesting)))))
847 match-found)))
849 ;;; muse.el ends here