publish: Provide php tag
[muse-el.git] / lisp / muse.el
blob173b91f2b70bf61d0cab515308f3a216f9304679
1 ;;; muse.el --- an authoring and publishing tool for Emacs
3 ;; Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: muse.el
7 ;; Version: 3.11
8 ;; Date: Fri 24-Aug-2007
9 ;; Keywords: hypermedia
10 ;; Author: John Wiegley (johnw AT gnu DOT org)
11 ;; Maintainer: Michael Olson <mwolson@gnu.org>
12 ;; Description: An authoring and publishing tool for Emacs
13 ;; URL: http://mwolson.org/projects/EmacsMuse.html
14 ;; Compatibility: Emacs21 XEmacs21 Emacs22
16 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
18 ;; Emacs Muse is free software; you can redistribute it and/or modify
19 ;; it under the terms of the GNU General Public License as published
20 ;; by the Free Software Foundation; either version 3, or (at your
21 ;; option) any later version.
23 ;; Emacs Muse is distributed in the hope that it will be useful, but
24 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 ;; General Public License for more details.
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with Emacs Muse; see the file COPYING. If not, write to the
30 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
31 ;; Boston, MA 02110-1301, USA.
33 ;;; Commentary:
35 ;; Muse is a tool for easily authoring and publishing documents. It
36 ;; allows for rapid prototyping of hyperlinked text, which may then be
37 ;; exported to multiple output formats -- such as HTML, LaTeX,
38 ;; Texinfo, etc.
40 ;; The markup rules used by Muse are intended to be very friendly to
41 ;; people familiar with Emacs. See the included manual for more
42 ;; information.
44 ;;; Contributors:
46 ;;; Code:
48 ;; Indicate that this version of Muse supports nested tags
49 (provide 'muse-nested-tags)
51 (defvar muse-version "3.11"
52 "The version of Muse currently loaded")
54 (defun muse-version (&optional insert)
55 "Display the version of Muse that is currently loaded.
56 If INSERT is non-nil, insert the text instead of displaying it."
57 (interactive "P")
58 (if insert
59 (insert muse-version)
60 (message muse-version)))
62 (defgroup muse nil
63 "Options controlling the behavior of Muse.
64 The markup used by Muse is intended to be very friendly to people
65 familiar with Emacs."
66 :group 'hypermedia)
68 (defvar muse-under-windows-p (memq system-type '(ms-dos windows-nt)))
70 (provide 'muse)
72 (condition-case nil
73 (require 'derived)
74 (error nil))
75 (require 'wid-edit)
76 (require 'muse-regexps)
78 (defvar muse-update-values-hook nil
79 "Hook for values that are automatically generated.
80 This is to be used by add-on modules for Muse.
81 It is run just before colorizing or publishing a buffer.")
83 (defun muse-update-values ()
84 "Update various values that are automatically generated.
86 Call this after changing `muse-project-alist'."
87 (interactive)
88 (run-hooks 'muse-update-values-hook)
89 (dolist (buffer (buffer-list))
90 (when (buffer-live-p buffer)
91 (with-current-buffer buffer
92 (when (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 Additionally, if `debug-on-error' is set to t, keep the buffer
284 around for debugging purposes rather than removing it."
285 (let ((temp-buffer (make-symbol "temp-buffer")))
286 `(let ((,temp-buffer (generate-new-buffer " *muse-temp*")))
287 (unwind-protect
288 (if debug-on-error
289 (with-current-buffer ,temp-buffer
290 ,@body)
291 (condition-case err
292 (with-current-buffer ,temp-buffer
293 ,@body)
294 (error
295 (if (and (boundp 'muse-batch-publishing-p)
296 muse-batch-publishing-p)
297 (progn
298 (message "%s: Error occured: %s"
299 (muse-page-name) err)
300 (backtrace))
301 (muse-display-warning
302 (format (concat "An error occurred while publishing"
303 " %s:\n %s\n\nSet debug-on-error to"
304 " `t' if you would like a backtrace.")
305 (muse-page-name) err))))))
306 (when (buffer-live-p ,temp-buffer)
307 (with-current-buffer ,temp-buffer
308 (set-buffer-modified-p nil))
309 (unless debug-on-error (kill-buffer ,temp-buffer)))))))
311 (put 'muse-with-temp-buffer 'lisp-indent-function 0)
312 (put 'muse-with-temp-buffer 'edebug-form-spec '(body))
314 (defun muse-insert-file-contents (filename &optional visit)
315 "Insert the contents of file FILENAME after point.
316 Do character code conversion, but none of the other unnecessary
317 things like format decoding or `find-file-hook'.
319 If VISIT is non-nil, the buffer's visited filename
320 and last save file modtime are set, and it is marked unmodified.
321 If visiting and the file does not exist, visiting is completed
322 before the error is signaled."
323 (let ((format-alist nil)
324 (after-insert-file-functions nil)
325 (find-buffer-file-type-function
326 (if (fboundp 'find-buffer-file-type)
327 (symbol-function 'find-buffer-file-type)
328 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 (unwind-protect
334 (progn
335 (fset 'find-buffer-file-type (lambda (filename) t))
336 (insert-file-contents filename visit))
337 (if find-buffer-file-type-function
338 (fset 'find-buffer-file-type find-buffer-file-type-function)
339 (fmakunbound 'find-buffer-file-type)))))
341 (defun muse-write-file (filename)
342 "Write current buffer into file FILENAME.
343 Unlike `write-file', this does not visit the file, try to back it
344 up, or interact with vc.el in any way.
346 If the file was not written successfully, return nil. Otherwise,
347 return non-nil."
348 (let ((backup-inhibited t)
349 (buffer-file-name filename)
350 (buffer-file-truename (file-truename filename)))
351 (save-current-buffer
352 (save-restriction
353 (widen)
354 (if (not (file-writable-p buffer-file-name))
355 (prog1 nil
356 (muse-display-warning
357 (format "Cannot write file %s:\n %s" buffer-file-name
358 (let ((dir (file-name-directory buffer-file-name)))
359 (if (not (file-directory-p dir))
360 (if (file-exists-p dir)
361 (format "%s is not a directory" dir)
362 (format "No directory named %s exists" dir))
363 (if (not (file-exists-p buffer-file-name))
364 (format "Directory %s write-protected" dir)
365 "File is write-protected"))))))
366 (let ((coding-system-for-write
367 (or (and (boundp 'save-buffer-coding-system)
368 save-buffer-coding-system)
369 coding-system-for-write)))
370 (write-region (point-min) (point-max) buffer-file-name))
371 (when (boundp 'last-file-coding-system-used)
372 (when (boundp 'buffer-file-coding-system-explicit)
373 (setq buffer-file-coding-system-explicit
374 last-coding-system-used))
375 (if save-buffer-coding-system
376 (setq save-buffer-coding-system last-coding-system-used)
377 (setq buffer-file-coding-system last-coding-system-used)))
378 t)))))
380 (defun muse-collect-alist (list element &optional test)
381 "Collect items from LIST whose car is equal to ELEMENT.
382 If TEST is specified, use it to compare ELEMENT."
383 (unless test (setq test 'equal))
384 (let ((items nil))
385 (dolist (item list)
386 (when (funcall test element (car item))
387 (setq items (cons item items))))
388 items))
390 (defmacro muse-sort-with-closure (list predicate closure)
391 "Sort LIST, stably, comparing elements using PREDICATE.
392 Returns the sorted list. LIST is modified by side effects.
393 PREDICATE is called with two elements of list and CLOSURE.
394 PREDICATE should return non-nil if the first element should sort
395 before the second."
396 `(sort ,list (lambda (a b) (funcall ,predicate a b ,closure))))
398 (put 'muse-sort-with-closure 'lisp-indent-function 0)
399 (put 'muse-sort-with-closure 'edebug-form-spec '(form function-form form))
401 (defun muse-sort-by-rating (rated-list &optional test)
402 "Sort RATED-LIST according to the rating of each element.
403 The rating is stripped out in the returned list.
404 Default sorting is highest-first.
406 If TEST if specified, use it to sort the list. The default test is '>."
407 (unless test (setq test '>))
408 (mapcar (function cdr)
409 (muse-sort-with-closure
410 rated-list
411 (lambda (a b closure)
412 (let ((na (numberp (car a)))
413 (nb (numberp (car b))))
414 (cond ((and na nb) (funcall closure (car a) (car b)))
415 (na (not nb))
416 (t nil))))
417 test)))
419 (defun muse-escape-specials-in-string (specials string &optional reverse)
420 "Apply the transformations in SPECIALS to STRING.
422 The transforms should form a fully reversible and non-ambiguous
423 syntax when STRING is parsed from left to right.
425 If REVERSE is specified, reverse an already-escaped string."
426 (let ((rules (mapcar (lambda (rule)
427 (cons (regexp-quote (if reverse
428 (cdr rule)
429 (car rule)))
430 (if reverse (car rule) (cdr rule))))
431 specials)))
432 (with-temp-buffer
433 (insert string)
434 (goto-char (point-min))
435 (save-match-data
436 (while (not (eobp))
437 (unless (catch 'found
438 (dolist (rule rules)
439 (when (looking-at (car rule))
440 (replace-match (cdr rule) t t)
441 (throw 'found t))))
442 (forward-char))))
443 (buffer-string))))
445 (defun muse-trim-whitespace (string)
446 "Return a version of STRING with no initial nor trailing whitespace."
447 (muse-replace-regexp-in-string
448 (concat "\\`[" muse-regexp-blank "]+\\|[" muse-regexp-blank "]+\\'")
449 "" string))
451 (defun muse-path-sans-extension (path)
452 "Return PATH sans final \"extension\".
454 The extension, in a file name, is the part that follows the last `.',
455 except that a leading `.', if any, doesn't count.
457 This differs from `file-name-sans-extension' in that it will
458 never modify the directory part of the path."
459 (concat (file-name-directory path)
460 (file-name-nondirectory (file-name-sans-extension path))))
462 ;; The following code was extracted from cl
464 (defun muse-const-expr-p (x)
465 (cond ((consp x)
466 (or (eq (car x) 'quote)
467 (and (memq (car x) '(function function*))
468 (or (symbolp (nth 1 x))
469 (and (eq (and (consp (nth 1 x))
470 (car (nth 1 x))) 'lambda) 'func)))))
471 ((symbolp x) (and (memq x '(nil t)) t))
472 (t t)))
474 (put 'muse-assertion-failed 'error-conditions '(error))
475 (put 'muse-assertion-failed 'error-message "Assertion failed")
477 (defun muse-list* (arg &rest rest)
478 "Return a new list with specified args as elements, cons'd to last arg.
479 Thus, `(list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to
480 `(cons A (cons B (cons C D)))'."
481 (cond ((not rest) arg)
482 ((not (cdr rest)) (cons arg (car rest)))
483 (t (let* ((n (length rest))
484 (copy (copy-sequence rest))
485 (last (nthcdr (- n 2) copy)))
486 (setcdr last (car (cdr last)))
487 (cons arg copy)))))
489 (defmacro muse-assert (form &optional show-args string &rest args)
490 "Verify that FORM returns non-nil; signal an error if not.
491 Second arg SHOW-ARGS means to include arguments of FORM in message.
492 Other args STRING and ARGS... are arguments to be passed to `error'.
493 They are not evaluated unless the assertion fails. If STRING is
494 omitted, a default message listing FORM itself is used."
495 (let ((sargs
496 (and show-args
497 (delq nil (mapcar
498 (function
499 (lambda (x)
500 (and (not (muse-const-expr-p x)) x)))
501 (cdr form))))))
502 (list 'progn
503 (list 'or form
504 (if string
505 (muse-list* 'error string (append sargs args))
506 (list 'signal '(quote muse-assertion-failed)
507 (muse-list* 'list (list 'quote form) sargs))))
508 nil)))
510 ;; Compatibility functions
512 (if (fboundp 'looking-back)
513 (defalias 'muse-looking-back 'looking-back)
514 (defun muse-looking-back (regexp &optional limit &rest ignored)
515 (save-excursion
516 (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t))))
518 (eval-and-compile
519 (if (fboundp 'line-end-position)
520 (defalias 'muse-line-end-position 'line-end-position)
521 (defun muse-line-end-position (&optional n)
522 (save-excursion (end-of-line n) (point))))
524 (if (fboundp 'line-beginning-position)
525 (defalias 'muse-line-beginning-position 'line-beginning-position)
526 (defun muse-line-beginning-position (&optional n)
527 (save-excursion (beginning-of-line n) (point))))
529 (if (fboundp 'match-string-no-properties)
530 (defalias 'muse-match-string-no-properties 'match-string-no-properties)
531 (defun muse-match-string-no-properties (num &optional string)
532 (match-string num string))))
534 (defun muse-replace-regexp-in-string (regexp replacement text &optional fixedcase literal)
535 "Replace REGEXP with REPLACEMENT in TEXT.
537 Return a new string containing the replacements.
539 If fourth arg FIXEDCASE is non-nil, do not alter case of replacement text.
540 If fifth arg LITERAL is non-nil, insert REPLACEMENT literally."
541 (cond
542 ((and (featurep 'xemacs) (fboundp 'replace-in-string))
543 (and (fboundp 'replace-in-string) ; stupid byte-compiler warning
544 (replace-in-string text regexp replacement literal)))
545 ((fboundp 'replace-regexp-in-string)
546 (replace-regexp-in-string regexp replacement text fixedcase literal))
547 (t (error (concat "Neither `replace-in-string' nor "
548 "`replace-regexp-in-string' was found")))))
550 (if (fboundp 'add-to-invisibility-spec)
551 (defalias 'muse-add-to-invisibility-spec 'add-to-invisibility-spec)
552 (defun muse-add-to-invisibility-spec (element)
553 "Add ELEMENT to `buffer-invisibility-spec'.
554 See documentation for `buffer-invisibility-spec' for the kind of elements
555 that can be added."
556 (if (eq buffer-invisibility-spec t)
557 (setq buffer-invisibility-spec (list t)))
558 (setq buffer-invisibility-spec
559 (cons element buffer-invisibility-spec))))
561 (if (fboundp 'read-directory-name)
562 (defalias 'muse-read-directory-name 'read-directory-name)
563 (defun muse-read-directory-name (prompt &optional dir default-dirname mustmatch initial)
564 "Read directory name - see `read-file-name' for details."
565 (unless dir
566 (setq dir default-directory))
567 (read-file-name prompt dir (or default-dirname
568 (if initial (expand-file-name initial dir)
569 dir))
570 mustmatch initial)))
572 (defun muse-file-remote-p (file)
573 "Test whether FILE specifies a location on a remote system.
574 Return non-nil if the location is indeed remote.
576 For example, the filename \"/user@host:/foo\" specifies a location
577 on the system \"/user@host:\"."
578 (cond ((fboundp 'file-remote-p)
579 (file-remote-p file))
580 ((fboundp 'tramp-handle-file-remote-p)
581 (tramp-handle-file-remote-p file))
582 ((and (boundp 'ange-ftp-name-format)
583 (string-match (car ange-ftp-name-format) file))
585 (t nil)))
587 (if (fboundp 'delete-and-extract-region)
588 (defalias 'muse-delete-and-extract-region 'delete-and-extract-region)
589 (defun muse-delete-and-extract-region (start end)
590 "Delete the text between START and END and return it."
591 (prog1 (buffer-substring start end)
592 (delete-region start end))))
594 ;; Set face globally in a predictable fashion
595 (defun muse-copy-face (old new)
596 "Copy face OLD to NEW."
597 (if (featurep 'xemacs)
598 (copy-face old new 'all)
599 (copy-face old new)))
601 ;; Widget compatibility functions
603 (defun muse-widget-type-value-create (widget)
604 "Convert and instantiate the value of the :type attribute of WIDGET.
605 Store the newly created widget in the :children attribute.
607 The value of the :type attribute should be an unconverted widget type."
608 (let ((value (widget-get widget :value))
609 (type (widget-get widget :type)))
610 (widget-put widget :children
611 (list (widget-create-child-value widget
612 (widget-convert type)
613 value)))))
615 (defun muse-widget-child-value-get (widget)
616 "Get the value of the first member of :children in WIDGET."
617 (widget-value (car (widget-get widget :children))))
619 (defun muse-widget-type-match (widget value)
620 "Non-nil if the :type value of WIDGET matches VALUE.
622 The value of the :type attribute should be an unconverted widget type."
623 (widget-apply (widget-convert (widget-get widget :type)) :match value))
625 ;; Link-handling functions and variables
627 (defun muse-get-link (&optional target)
628 "Based on the match data, retrieve the link.
629 Use TARGET to get the string, if it is specified."
630 (muse-match-string-no-properties 1 target))
632 (defun muse-get-link-desc (&optional target)
633 "Based on the match data, retrieve the link description.
634 Use TARGET to get the string, if it is specified."
635 (muse-match-string-no-properties 2 target))
637 (defvar muse-link-specials
638 '(("[" . "%5B")
639 ("]" . "%5D")
640 ("%" . "%%"))
641 "Syntax used for escaping and unescaping links.
642 This allows brackets to occur in explicit links as long as you
643 use the standard Muse functions to create them.")
645 (defun muse-link-escape (text)
646 "Escape characters in TEXT that conflict with the explicit link
647 regexp."
648 (when (stringp text)
649 (muse-escape-specials-in-string muse-link-specials text)))
651 (defun muse-link-unescape (text)
652 "Un-escape characters in TEXT that conflict with the explicit
653 link regexp."
654 (when (stringp text)
655 (muse-escape-specials-in-string muse-link-specials text t)))
657 (defun muse-handle-url (&optional string)
658 "If STRING or point has a URL, match and return it."
659 (if (if string (string-match muse-url-regexp string)
660 (looking-at muse-url-regexp))
661 (match-string 0 string)))
663 (defcustom muse-implicit-link-functions '(muse-handle-url)
664 "A list of functions to handle an implicit link.
665 An implicit link is one that is not surrounded by brackets.
667 By default, Muse handles URLs only.
668 If you want to handle WikiWords, load muse-wiki.el."
669 :type 'hook
670 :options '(muse-handle-url)
671 :group 'muse)
673 (defun muse-handle-implicit-link (&optional link)
674 "Handle implicit links. If LINK is not specified, look at point.
675 An implicit link is one that is not surrounded by brackets.
676 By default, Muse handles URLs only.
677 If you want to handle WikiWords, load muse-wiki.el.
679 This function modifies the match data so that match 0 is the
680 link.
682 The match data is restored after each unsuccessful handler
683 function call. If LINK is specified, only restore at very end.
685 This behavior is needed because the part of the buffer that
686 `muse-implicit-link-regexp' matches must be narrowed to the part
687 that is an accepted link."
688 (let ((funcs muse-implicit-link-functions)
689 (res nil)
690 (data (match-data t)))
691 (while funcs
692 (setq res (funcall (car funcs) link))
693 (if res
694 (setq funcs nil)
695 (unless link (set-match-data data))
696 (setq funcs (cdr funcs))))
697 (when link (set-match-data data))
698 res))
700 (defcustom muse-explicit-link-functions nil
701 "A list of functions to handle an explicit link.
702 An explicit link is one [[like][this]] or [[this]]."
703 :type 'hook
704 :group 'muse)
706 (defun muse-handle-explicit-link (&optional link)
707 "Handle explicit links. If LINK is not specified, look at point.
708 An explicit link is one that looks [[like][this]] or [[this]].
710 The match data is preserved. If no handlers are able to process
711 LINK, return LINK (if specified) or the 1st match string. If
712 LINK is not specified, it is assumed that Muse has matched
713 against `muse-explicit-link-regexp' before calling this
714 function."
715 (let ((funcs muse-explicit-link-functions)
716 (res nil))
717 (save-match-data
718 (while funcs
719 (setq res (funcall (car funcs) link))
720 (if res
721 (setq funcs nil)
722 (setq funcs (cdr funcs)))))
723 (muse-link-unescape
724 (if res
726 (or link (muse-get-link))))))
728 ;; Movement functions
730 (defun muse-list-item-type (str)
731 "Determine the type of list given STR.
732 Returns either 'ul, 'ol, 'dl-term, 'dl-entry, or nil."
733 (save-match-data
734 (cond ((or (string= str "")
735 (< (length str) 2))
736 nil)
737 ((string-match muse-dl-entry-regexp str)
738 'dl-entry)
739 ((string-match muse-dl-term-regexp str)
740 'dl-term)
741 ((string-match muse-ol-item-regexp str)
742 'ol)
743 ((string-match muse-ul-item-regexp str)
744 'ul)
745 (t nil))))
747 (defun muse-list-item-critical-point (&optional offset)
748 "Figure out where the important markup character for the
749 currently-matched list item is.
751 If OFFSET is specified, it is the number of groupings outside of
752 the contents of `muse-list-item-regexp'."
753 (unless offset (setq offset 0))
754 (if (match-end (+ offset 2))
755 ;; at a definition list
756 (match-end (+ offset 2))
757 ;; at a different kind of list
758 (match-beginning (+ offset 1))))
760 (defun muse-forward-paragraph (&optional pattern)
761 "Move forward safely by one paragraph, or according to PATTERN."
762 (when (get-text-property (point) 'end-list)
763 (goto-char (next-single-property-change (point) 'end-list)))
764 (setq pattern (if pattern
765 (concat "^\\(?:" pattern "\\|\n\\|\\'\\)")
766 "^\\s-*\\(\n\\|\\'\\)"))
767 (let ((next-list-end (or (next-single-property-change (point) 'end-list)
768 (point-max))))
769 (forward-line 1)
770 (if (re-search-forward pattern nil t)
771 (goto-char (match-beginning 0))
772 (goto-char (point-max)))
773 (when (> (point) next-list-end)
774 (goto-char next-list-end))))
776 (defun muse-forward-list-item-1 (type empty-line indented-line)
777 "Determine whether a nested list item is after point."
778 (if (match-beginning 1)
779 ;; if we are given a dl entry, skip past everything on the same
780 ;; level, except for other dl entries
781 (and (eq type 'dl-entry)
782 (not (eq (char-after (match-beginning 2)) ?\:)))
783 ;; blank line encountered with no list item on the same
784 ;; level after it
785 (let ((beg (point)))
786 (forward-line 1)
787 (if (save-match-data
788 (and (looking-at indented-line)
789 (not (looking-at empty-line))))
790 ;; found that this blank line is followed by some
791 ;; indentation, plus other text, so we'll keep
792 ;; going
794 (goto-char beg)
795 nil))))
797 (defun muse-forward-list-item (type indent &optional no-skip-nested)
798 "Move forward to the next item of TYPE.
799 Return non-nil if successful, nil otherwise.
800 The beginning indentation is given by INDENT.
802 If NO-SKIP-NESTED is non-nil, do not skip past nested items.
803 Note that if you desire this behavior, you will also need to
804 provide a very liberal INDENT value, such as
805 \(concat \"[\" muse-regexp-blank \"]*\")."
806 (let* ((list-item (format muse-list-item-regexp indent))
807 (empty-line (concat "^[" muse-regexp-blank "]*\n"))
808 (indented-line (concat "^" indent "[" muse-regexp-blank "]"))
809 (list-pattern (concat "\\(?:" empty-line "\\)?"
810 "\\(" list-item "\\)")))
811 (while (progn
812 (muse-forward-paragraph list-pattern)
813 ;; make sure we don't go past boundary
814 (and (not (or (get-text-property (point) 'end-list)
815 (>= (point) (point-max))))
816 ;; move past markup that is part of another construct
817 (or (and (match-beginning 1)
818 (or (get-text-property
819 (muse-list-item-critical-point 1) 'muse-link)
820 (and (derived-mode-p 'muse-mode)
821 (get-text-property
822 (muse-list-item-critical-point 1)
823 'face))))
824 ;; skip nested items
825 (and (not no-skip-nested)
826 (muse-forward-list-item-1 type empty-line
827 indented-line))))))
828 (cond ((or (get-text-property (point) 'end-list)
829 (>= (point) (point-max)))
830 ;; at a list boundary, so stop
831 nil)
832 ((let ((str (when (match-beginning 2)
833 ;; get the entire line
834 (save-excursion
835 (goto-char (match-beginning 2))
836 (buffer-substring (muse-line-beginning-position)
837 (muse-line-end-position))))))
838 (and str (eq type (muse-list-item-type str))))
839 ;; same type, so indicate that there are more items to be
840 ;; parsed
841 (goto-char (match-beginning 1)))
843 (when (match-beginning 1)
844 (goto-char (match-beginning 1)))
845 ;; move to just before foreign list item markup
846 nil))))
848 (defun muse-goto-tag-end (tag nested)
849 "Move forward past the end of TAG.
851 If NESTED is non-nil, look for other instances of this tag that
852 may be nested inside of this tag, and skip past them."
853 (if (not nested)
854 (search-forward (concat "</" tag ">") nil t)
855 (let ((nesting 1)
856 (tag-regexp (concat "\\(<\\(/?\\)" tag "\\([ >]\\)\\)"))
857 (match-found nil))
858 (while (and (> nesting 0)
859 (setq match-found (re-search-forward tag-regexp nil t)))
860 ;; for the sake of font-locking code, skip matches in comments
861 (unless (get-text-property (match-beginning 0) 'muse-comment)
862 (if (string-equal (match-string 2) "/")
863 (and (string-equal (match-string 3) ">")
864 (setq nesting (1- nesting)))
865 (setq nesting (1+ nesting)))))
866 match-found)))
868 ;;; muse.el ends here