Manual: Initial documentation effort for ikiwiki example.
[muse-el.git] / lisp / muse.el
blobc9f29d17463b3740b44dce8f4abd72061f71ec5e
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.12"
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 and end-of-line conversion, but none
320 of the other unnecessary things like format decoding or
321 `find-file-hook'.
323 If VISIT is non-nil, the buffer's visited filename
324 and last save file modtime are set, and it is marked unmodified.
325 If visiting and the file does not exist, visiting is completed
326 before the error is signaled."
327 (let ((format-alist nil)
328 (after-insert-file-functions nil)
329 (inhibit-file-name-handlers
330 (append '(jka-compr-handler image-file-handler)
331 inhibit-file-name-handlers))
332 (inhibit-file-name-operation 'insert-file-contents))
333 (insert-file-contents filename visit)))
335 (defun muse-write-file (filename)
336 "Write current buffer into file FILENAME.
337 Unlike `write-file', this does not visit the file, try to back it
338 up, or interact with vc.el in any way.
340 If the file was not written successfully, return nil. Otherwise,
341 return non-nil."
342 (let ((backup-inhibited t)
343 (buffer-file-name filename)
344 (buffer-file-truename (file-truename filename)))
345 (save-current-buffer
346 (save-restriction
347 (widen)
348 (if (not (file-writable-p buffer-file-name))
349 (prog1 nil
350 (muse-display-warning
351 (format "Cannot write file %s:\n %s" buffer-file-name
352 (let ((dir (file-name-directory buffer-file-name)))
353 (if (not (file-directory-p dir))
354 (if (file-exists-p dir)
355 (format "%s is not a directory" dir)
356 (format "No directory named %s exists" dir))
357 (if (not (file-exists-p buffer-file-name))
358 (format "Directory %s write-protected" dir)
359 "File is write-protected"))))))
360 (let ((coding-system-for-write
361 (or (and (boundp 'save-buffer-coding-system)
362 save-buffer-coding-system)
363 coding-system-for-write)))
364 (write-region (point-min) (point-max) buffer-file-name))
365 (when (boundp 'last-file-coding-system-used)
366 (when (boundp 'buffer-file-coding-system-explicit)
367 (setq buffer-file-coding-system-explicit
368 last-coding-system-used))
369 (if save-buffer-coding-system
370 (setq save-buffer-coding-system last-coding-system-used)
371 (setq buffer-file-coding-system last-coding-system-used)))
372 t)))))
374 (defun muse-collect-alist (list element &optional test)
375 "Collect items from LIST whose car is equal to ELEMENT.
376 If TEST is specified, use it to compare ELEMENT."
377 (unless test (setq test 'equal))
378 (let ((items nil))
379 (dolist (item list)
380 (when (funcall test element (car item))
381 (setq items (cons item items))))
382 items))
384 (defmacro muse-sort-with-closure (list predicate closure)
385 "Sort LIST, stably, comparing elements using PREDICATE.
386 Returns the sorted list. LIST is modified by side effects.
387 PREDICATE is called with two elements of list and CLOSURE.
388 PREDICATE should return non-nil if the first element should sort
389 before the second."
390 `(sort ,list (lambda (a b) (funcall ,predicate a b ,closure))))
392 (put 'muse-sort-with-closure 'lisp-indent-function 0)
393 (put 'muse-sort-with-closure 'edebug-form-spec '(form function-form form))
395 (defun muse-sort-by-rating (rated-list &optional test)
396 "Sort RATED-LIST according to the rating of each element.
397 The rating is stripped out in the returned list.
398 Default sorting is highest-first.
400 If TEST if specified, use it to sort the list. The default test is '>."
401 (unless test (setq test '>))
402 (mapcar (function cdr)
403 (muse-sort-with-closure
404 rated-list
405 (lambda (a b closure)
406 (let ((na (numberp (car a)))
407 (nb (numberp (car b))))
408 (cond ((and na nb) (funcall closure (car a) (car b)))
409 (na (not nb))
410 (t nil))))
411 test)))
413 (defun muse-escape-specials-in-string (specials string &optional reverse)
414 "Apply the transformations in SPECIALS to STRING.
416 The transforms should form a fully reversible and non-ambiguous
417 syntax when STRING is parsed from left to right.
419 If REVERSE is specified, reverse an already-escaped string."
420 (let ((rules (mapcar (lambda (rule)
421 (cons (regexp-quote (if reverse
422 (cdr rule)
423 (car rule)))
424 (if reverse (car rule) (cdr rule))))
425 specials)))
426 (save-match-data
427 (with-temp-buffer
428 (insert string)
429 (goto-char (point-min))
430 (while (not (eobp))
431 (unless (catch 'found
432 (dolist (rule rules)
433 (when (looking-at (car rule))
434 (replace-match (cdr rule) t t)
435 (throw 'found t))))
436 (forward-char)))
437 (buffer-string)))))
439 (defun muse-trim-whitespace (string)
440 "Return a version of STRING with no initial nor trailing whitespace."
441 (muse-replace-regexp-in-string
442 (concat "\\`[" muse-regexp-blank "]+\\|[" muse-regexp-blank "]+\\'")
443 "" string))
445 (defun muse-path-sans-extension (path)
446 "Return PATH sans final \"extension\".
448 The extension, in a file name, is the part that follows the last `.',
449 except that a leading `.', if any, doesn't count.
451 This differs from `file-name-sans-extension' in that it will
452 never modify the directory part of the path."
453 (concat (file-name-directory path)
454 (file-name-nondirectory (file-name-sans-extension path))))
456 ;; The following code was extracted from cl
458 (defun muse-const-expr-p (x)
459 (cond ((consp x)
460 (or (eq (car x) 'quote)
461 (and (memq (car x) '(function function*))
462 (or (symbolp (nth 1 x))
463 (and (eq (and (consp (nth 1 x))
464 (car (nth 1 x))) 'lambda) 'func)))))
465 ((symbolp x) (and (memq x '(nil t)) t))
466 (t t)))
468 (put 'muse-assertion-failed 'error-conditions '(error))
469 (put 'muse-assertion-failed 'error-message "Assertion failed")
471 (defun muse-list* (arg &rest rest)
472 "Return a new list with specified args as elements, cons'd to last arg.
473 Thus, `(list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to
474 `(cons A (cons B (cons C D)))'."
475 (cond ((not rest) arg)
476 ((not (cdr rest)) (cons arg (car rest)))
477 (t (let* ((n (length rest))
478 (copy (copy-sequence rest))
479 (last (nthcdr (- n 2) copy)))
480 (setcdr last (car (cdr last)))
481 (cons arg copy)))))
483 (defmacro muse-assert (form &optional show-args string &rest args)
484 "Verify that FORM returns non-nil; signal an error if not.
485 Second arg SHOW-ARGS means to include arguments of FORM in message.
486 Other args STRING and ARGS... are arguments to be passed to `error'.
487 They are not evaluated unless the assertion fails. If STRING is
488 omitted, a default message listing FORM itself is used."
489 (let ((sargs
490 (and show-args
491 (delq nil (mapcar
492 (function
493 (lambda (x)
494 (and (not (muse-const-expr-p x)) x)))
495 (cdr form))))))
496 (list 'progn
497 (list 'or form
498 (if string
499 (muse-list* 'error string (append sargs args))
500 (list 'signal '(quote muse-assertion-failed)
501 (muse-list* 'list (list 'quote form) sargs))))
502 nil)))
504 ;; Compatibility functions
506 (if (fboundp 'looking-back)
507 (defalias 'muse-looking-back 'looking-back)
508 (defun muse-looking-back (regexp &optional limit &rest ignored)
509 (save-excursion
510 (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t))))
512 (eval-and-compile
513 (if (fboundp 'line-end-position)
514 (defalias 'muse-line-end-position 'line-end-position)
515 (defun muse-line-end-position (&optional n)
516 (save-excursion (end-of-line n) (point))))
518 (if (fboundp 'line-beginning-position)
519 (defalias 'muse-line-beginning-position 'line-beginning-position)
520 (defun muse-line-beginning-position (&optional n)
521 (save-excursion (beginning-of-line n) (point))))
523 (if (fboundp 'match-string-no-properties)
524 (defalias 'muse-match-string-no-properties 'match-string-no-properties)
525 (defun muse-match-string-no-properties (num &optional string)
526 (match-string num string))))
528 (defun muse-replace-regexp-in-string (regexp replacement text &optional fixedcase literal)
529 "Replace REGEXP with REPLACEMENT in TEXT.
531 Return a new string containing the replacements.
533 If fourth arg FIXEDCASE is non-nil, do not alter case of replacement text.
534 If fifth arg LITERAL is non-nil, insert REPLACEMENT literally."
535 (cond
536 ((and (featurep 'xemacs) (fboundp 'replace-in-string))
537 (and (fboundp 'replace-in-string) ; stupid byte-compiler warning
538 (replace-in-string text regexp replacement literal)))
539 ((fboundp 'replace-regexp-in-string)
540 (replace-regexp-in-string regexp replacement text fixedcase literal))
541 (t (error (concat "Neither `replace-in-string' nor "
542 "`replace-regexp-in-string' was found")))))
544 (if (fboundp 'add-to-invisibility-spec)
545 (defalias 'muse-add-to-invisibility-spec 'add-to-invisibility-spec)
546 (defun muse-add-to-invisibility-spec (element)
547 "Add ELEMENT to `buffer-invisibility-spec'.
548 See documentation for `buffer-invisibility-spec' for the kind of elements
549 that can be added."
550 (if (eq buffer-invisibility-spec t)
551 (setq buffer-invisibility-spec (list t)))
552 (setq buffer-invisibility-spec
553 (cons element buffer-invisibility-spec))))
555 (if (fboundp 'read-directory-name)
556 (defalias 'muse-read-directory-name 'read-directory-name)
557 (defun muse-read-directory-name (prompt &optional dir default-dirname mustmatch initial)
558 "Read directory name - see `read-file-name' for details."
559 (unless dir
560 (setq dir default-directory))
561 (read-file-name prompt dir (or default-dirname
562 (if initial (expand-file-name initial dir)
563 dir))
564 mustmatch initial)))
566 (defun muse-file-remote-p (file)
567 "Test whether FILE specifies a location on a remote system.
568 Return non-nil if the location is indeed remote.
570 For example, the filename \"/user@host:/foo\" specifies a location
571 on the system \"/user@host:\"."
572 (cond ((fboundp 'file-remote-p)
573 (file-remote-p file))
574 ((fboundp 'tramp-handle-file-remote-p)
575 (tramp-handle-file-remote-p file))
576 ((and (boundp 'ange-ftp-name-format)
577 (string-match (car ange-ftp-name-format) file))
579 (t nil)))
581 (if (fboundp 'delete-and-extract-region)
582 (defalias 'muse-delete-and-extract-region 'delete-and-extract-region)
583 (defun muse-delete-and-extract-region (start end)
584 "Delete the text between START and END and return it."
585 (prog1 (buffer-substring start end)
586 (delete-region start end))))
588 (if (fboundp 'delete-dups)
589 (defalias 'muse-delete-dups 'delete-dups)
590 (defun muse-delete-dups (list)
591 "Destructively remove `equal' duplicates from LIST.
592 Store the result in LIST and return it. LIST must be a proper list.
593 Of several `equal' occurrences of an element in LIST, the first
594 one is kept."
595 (let ((tail list))
596 (while tail
597 (setcdr tail (delete (car tail) (cdr tail)))
598 (setq tail (cdr tail))))
599 list))
601 ;; Set face globally in a predictable fashion
602 (defun muse-copy-face (old new)
603 "Copy face OLD to NEW."
604 (if (featurep 'xemacs)
605 (copy-face old new 'all)
606 (copy-face old new)))
608 ;; Widget compatibility functions
610 (defun muse-widget-type-value-create (widget)
611 "Convert and instantiate the value of the :type attribute of WIDGET.
612 Store the newly created widget in the :children attribute.
614 The value of the :type attribute should be an unconverted widget type."
615 (let ((value (widget-get widget :value))
616 (type (widget-get widget :type)))
617 (widget-put widget :children
618 (list (widget-create-child-value widget
619 (widget-convert type)
620 value)))))
622 (defun muse-widget-child-value-get (widget)
623 "Get the value of the first member of :children in WIDGET."
624 (widget-value (car (widget-get widget :children))))
626 (defun muse-widget-type-match (widget value)
627 "Non-nil if the :type value of WIDGET matches VALUE.
629 The value of the :type attribute should be an unconverted widget type."
630 (widget-apply (widget-convert (widget-get widget :type)) :match value))
632 ;; Link-handling functions and variables
634 (defun muse-get-link (&optional target)
635 "Based on the match data, retrieve the link.
636 Use TARGET to get the string, if it is specified."
637 (muse-match-string-no-properties 1 target))
639 (defun muse-get-link-desc (&optional target)
640 "Based on the match data, retrieve the link description.
641 Use TARGET to get the string, if it is specified."
642 (muse-match-string-no-properties 2 target))
644 (defvar muse-link-specials
645 '(("[" . "%5B")
646 ("]" . "%5D")
647 ("%" . "%%"))
648 "Syntax used for escaping and unescaping links.
649 This allows brackets to occur in explicit links as long as you
650 use the standard Muse functions to create them.")
652 (defun muse-link-escape (text)
653 "Escape characters in TEXT that conflict with the explicit link
654 regexp."
655 (when (stringp text)
656 (muse-escape-specials-in-string muse-link-specials text)))
658 (defun muse-link-unescape (text)
659 "Un-escape characters in TEXT that conflict with the explicit
660 link regexp."
661 (when (stringp text)
662 (muse-escape-specials-in-string muse-link-specials text t)))
664 (defun muse-handle-url (&optional string)
665 "If STRING or point has a URL, match and return it."
666 (if (if string (string-match muse-url-regexp string)
667 (looking-at muse-url-regexp))
668 (match-string 0 string)))
670 (defcustom muse-implicit-link-functions '(muse-handle-url)
671 "A list of functions to handle an implicit link.
672 An implicit link is one that is not surrounded by brackets.
674 By default, Muse handles URLs only.
675 If you want to handle WikiWords, load muse-wiki.el."
676 :type 'hook
677 :options '(muse-handle-url)
678 :group 'muse)
680 (defun muse-handle-implicit-link (&optional link)
681 "Handle implicit links. If LINK is not specified, look at point.
682 An implicit link is one that is not surrounded by brackets.
683 By default, Muse handles URLs only.
684 If you want to handle WikiWords, load muse-wiki.el.
686 This function modifies the match data so that match 0 is the
687 link.
689 The match data is restored after each unsuccessful handler
690 function call. If LINK is specified, only restore at very end.
692 This behavior is needed because the part of the buffer that
693 `muse-implicit-link-regexp' matches must be narrowed to the part
694 that is an accepted link."
695 (let ((funcs muse-implicit-link-functions)
696 (res nil)
697 (data (match-data t)))
698 (while funcs
699 (setq res (funcall (car funcs) link))
700 (if res
701 (setq funcs nil)
702 (unless link (set-match-data data))
703 (setq funcs (cdr funcs))))
704 (when link (set-match-data data))
705 res))
707 (defcustom muse-explicit-link-functions nil
708 "A list of functions to handle an explicit link.
709 An explicit link is one [[like][this]] or [[this]]."
710 :type 'hook
711 :group 'muse)
713 (defun muse-handle-explicit-link (&optional link)
714 "Handle explicit links. If LINK is not specified, look at point.
715 An explicit link is one that looks [[like][this]] or [[this]].
717 The match data is preserved. If no handlers are able to process
718 LINK, return LINK (if specified) or the 1st match string. If
719 LINK is not specified, it is assumed that Muse has matched
720 against `muse-explicit-link-regexp' before calling this
721 function."
722 (let ((funcs muse-explicit-link-functions)
723 (res nil))
724 (save-match-data
725 (while funcs
726 (setq res (funcall (car funcs) link))
727 (if res
728 (setq funcs nil)
729 (setq funcs (cdr funcs)))))
730 (muse-link-unescape
731 (if res
733 (or link (muse-get-link))))))
735 ;; Movement functions
737 (defun muse-list-item-type (str)
738 "Determine the type of list given STR.
739 Returns either 'ul, 'ol, 'dl-term, 'dl-entry, or nil."
740 (save-match-data
741 (cond ((or (string= str "")
742 (< (length str) 2))
743 nil)
744 ((string-match muse-dl-entry-regexp str)
745 'dl-entry)
746 ((string-match muse-dl-term-regexp str)
747 'dl-term)
748 ((string-match muse-ol-item-regexp str)
749 'ol)
750 ((string-match muse-ul-item-regexp str)
751 'ul)
752 (t nil))))
754 (defun muse-list-item-critical-point (&optional offset)
755 "Figure out where the important markup character for the
756 currently-matched list item is.
758 If OFFSET is specified, it is the number of groupings outside of
759 the contents of `muse-list-item-regexp'."
760 (unless offset (setq offset 0))
761 (if (match-end (+ offset 2))
762 ;; at a definition list
763 (match-end (+ offset 2))
764 ;; at a different kind of list
765 (match-beginning (+ offset 1))))
767 (defun muse-forward-paragraph (&optional pattern)
768 "Move forward safely by one paragraph, or according to PATTERN."
769 (when (get-text-property (point) 'end-list)
770 (goto-char (next-single-property-change (point) 'end-list)))
771 (setq pattern (if pattern
772 (concat "^\\(?:" pattern "\\|\n\\|\\'\\)")
773 "^\\s-*\\(\n\\|\\'\\)"))
774 (let ((next-list-end (or (next-single-property-change (point) 'end-list)
775 (point-max))))
776 (forward-line 1)
777 (if (re-search-forward pattern nil t)
778 (goto-char (match-beginning 0))
779 (goto-char (point-max)))
780 (when (> (point) next-list-end)
781 (goto-char next-list-end))))
783 (defun muse-forward-list-item-1 (type empty-line indented-line)
784 "Determine whether a nested list item is after point."
785 (if (match-beginning 1)
786 ;; if we are given a dl entry, skip past everything on the same
787 ;; level, except for other dl entries
788 (and (eq type 'dl-entry)
789 (not (eq (char-after (match-beginning 2)) ?\:)))
790 ;; blank line encountered with no list item on the same
791 ;; level after it
792 (let ((beg (point)))
793 (forward-line 1)
794 (if (save-match-data
795 (and (looking-at indented-line)
796 (not (looking-at empty-line))))
797 ;; found that this blank line is followed by some
798 ;; indentation, plus other text, so we'll keep
799 ;; going
801 (goto-char beg)
802 nil))))
804 (defun muse-forward-list-item (type indent &optional no-skip-nested)
805 "Move forward to the next item of TYPE.
806 Return non-nil if successful, nil otherwise.
807 The beginning indentation is given by INDENT.
809 If NO-SKIP-NESTED is non-nil, do not skip past nested items.
810 Note that if you desire this behavior, you will also need to
811 provide a very liberal INDENT value, such as
812 \(concat \"[\" muse-regexp-blank \"]*\")."
813 (let* ((list-item (format muse-list-item-regexp indent))
814 (empty-line (concat "^[" muse-regexp-blank "]*\n"))
815 (indented-line (concat "^" indent "[" muse-regexp-blank "]"))
816 (list-pattern (concat "\\(?:" empty-line "\\)?"
817 "\\(" list-item "\\)")))
818 (while (progn
819 (muse-forward-paragraph list-pattern)
820 ;; make sure we don't go past boundary
821 (and (not (or (get-text-property (point) 'end-list)
822 (>= (point) (point-max))))
823 ;; move past markup that is part of another construct
824 (or (and (match-beginning 1)
825 (or (get-text-property
826 (muse-list-item-critical-point 1) 'muse-link)
827 (and (derived-mode-p 'muse-mode)
828 (get-text-property
829 (muse-list-item-critical-point 1)
830 'face))))
831 ;; skip nested items
832 (and (not no-skip-nested)
833 (muse-forward-list-item-1 type empty-line
834 indented-line))))))
835 (cond ((or (get-text-property (point) 'end-list)
836 (>= (point) (point-max)))
837 ;; at a list boundary, so stop
838 nil)
839 ((let ((str (when (match-beginning 2)
840 ;; get the entire line
841 (save-excursion
842 (goto-char (match-beginning 2))
843 (buffer-substring (muse-line-beginning-position)
844 (muse-line-end-position))))))
845 (and str (eq type (muse-list-item-type str))))
846 ;; same type, so indicate that there are more items to be
847 ;; parsed
848 (goto-char (match-beginning 1)))
850 (when (match-beginning 1)
851 (goto-char (match-beginning 1)))
852 ;; move to just before foreign list item markup
853 nil))))
855 (defun muse-goto-tag-end (tag nested)
856 "Move forward past the end of TAG.
858 If NESTED is non-nil, look for other instances of this tag that
859 may be nested inside of this tag, and skip past them."
860 (if (not nested)
861 (search-forward (concat "</" tag ">") nil t)
862 (let ((nesting 1)
863 (tag-regexp (concat "\\(<\\(/?\\)" tag "\\([ >]\\)\\)"))
864 (match-found nil))
865 (while (and (> nesting 0)
866 (setq match-found (re-search-forward tag-regexp nil t)))
867 ;; for the sake of font-locking code, skip matches in comments
868 (unless (get-text-property (match-beginning 0) 'muse-comment)
869 (if (string-equal (match-string 2) "/")
870 (and (string-equal (match-string 3) ">")
871 (setq nesting (1- nesting)))
872 (setq nesting (1+ nesting)))))
873 match-found)))
875 ;;; muse.el ends here