Make escaping of brackets in links work properly.
[muse-el.git] / lisp / muse-mode.el
blobe9ef3c6c28fb7b14a3f4f14dbf5bb4e7103cbee8
1 ;;; muse-mode.el --- mode for editing Muse files; has font-lock support
3 ;; Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
5 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
7 ;; Emacs Muse is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published
9 ;; by the Free Software Foundation; either version 2, or (at your
10 ;; option) any later version.
12 ;; Emacs Muse is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;; General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with Emacs Muse; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 ;; Boston, MA 02110-1301, USA.
22 ;;; Commentary:
24 ;; The Emacs Muse major mode is basically a hyped-up text-mode which
25 ;; knows a lot more about the apparent structure of the document.
27 ;;; Contributors:
29 ;; Andrea Riciputi (ariciputi AT pito DOT com) gave an initial
30 ;; implementation for tag completion by means of the `muse-insert-tag'
31 ;; function.
33 ;; Per B. Sederberg (per AT med DOT upenn DOT edu) contributed the
34 ;; insertion of relative links and list items, backlink searching, and
35 ;; other things as well.
37 ;;; Code:
39 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
41 ;; Emacs Muse Major Mode
43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
45 (require 'muse)
46 (require 'muse-regexps)
47 (require 'muse-project)
49 (autoload 'muse-use-font-lock "muse-colors")
50 (autoload 'muse-publish-this-file "muse-publish")
51 (autoload 'muse-publish-get-style "muse-publish")
52 (autoload 'muse-publish-output-file "muse-publish")
54 (require 'derived)
55 (eval-when-compile
56 (condition-case nil
57 (require 'pcomplete) ; load if available
58 (error nil)))
60 ;;; Options:
62 (defgroup muse-mode nil
63 "Options controlling the behavior of the Muse editing Mode."
64 :group 'muse)
66 (defcustom muse-mode-highlight-p t
67 "If non-nil, highlight the content of Muse buffers."
68 :type 'boolean
69 :require 'muse-colors
70 :group 'muse-mode)
72 (defcustom muse-mode-auto-p nil
73 "If non-nil, automagically determine when Muse mode should be activated."
74 :type 'boolean
75 :set (function
76 (lambda (sym value)
77 (if value
78 (add-hook 'find-file-hooks 'muse-mode-maybe)
79 (remove-hook 'find-file-hooks 'muse-mode-maybe))
80 (set sym value)))
81 :group 'muse-mode)
83 (defun muse-mode-maybe-after-init ()
84 (when muse-mode-auto-p
85 (add-hook 'find-file-hooks 'muse-mode-maybe)))
87 ;; If the user sets this value in their init file, make sure that
88 ;; it takes effect
89 (add-hook 'after-init-hook 'muse-mode-maybe-after-init)
91 (defcustom muse-mode-intangible-links nil
92 "If non-nil, use the intangible property on links.
93 This can cause problems with flyspell (and potentially fill-mode),
94 so only enable this if you don't use either of these."
95 :type 'boolean
96 :group 'muse-mode)
98 (defcustom muse-mode-hook nil
99 "A hook that is run when Muse mode is entered."
100 :type 'hook
101 :options '(flyspell-mode footnote-mode turn-on-auto-fill
102 highlight-changes-mode)
103 :group 'muse-mode)
105 (defcustom muse-grep-command
106 "find %D -type f ! -name '*~' | xargs -I {} echo \\\"{}\\\" | xargs egrep -n -e \"%W\""
107 "The command to use when grepping for backlinks and other
108 searches through the muse projects. The string %D is replaced by
109 the directories from muse-project-alist, space-separated. The
110 string %W is replaced with the name of the muse page or whatever
111 else you are searching for. This command has been modified to
112 handle spaces in filenames, which were giving egrep a problem.
114 Note: We highly recommend using glimpse to search large projects.
115 To use glimpse, install and edit a file called .glimpse_exclude
116 in your home directory. Put a list of glob patterns in that file
117 to exclude Emacs backup files, etc. Then, run the indexer using:
119 glimpseindex -o <list of Wiki directories>
121 Once that's completed, customize this variable to have the
122 following value:
124 glimpse -nyi \"%W\"
126 Your searches will go much, much faster, especially for very
127 large projects. Don't forget to add a user cronjob to update the
128 index at intervals."
129 :type 'string
130 :group 'muse-mode)
132 (defvar muse-insert-map
133 (let ((map (make-sparse-keymap)))
134 (define-key map "l" 'muse-insert-relative-link-to-file)
135 (define-key map "t" 'muse-insert-tag)
137 map))
139 (defvar muse-mode-map
140 (let ((map (make-sparse-keymap)))
141 (define-key map [(control ?c) (control ?a)] 'muse-index)
142 (define-key map [(control ?c) (control ?c)] 'muse-follow-name-at-point)
143 (define-key map [(control ?c) (control ?e)] 'muse-edit-link-at-point)
144 (define-key map [(control ?c) (control ?l)] 'font-lock-mode)
145 (define-key map [(control ?c) (control ?t)] 'muse-publish-this-file)
146 (define-key map [(control ?c) (control ?v)] 'muse-browse-result)
148 (define-key map [(control ?c) ?=] 'muse-what-changed)
150 (define-key map [tab] 'muse-next-reference)
151 (define-key map [(control ?i)] 'muse-next-reference)
153 (if (featurep 'xemacs)
154 (progn
155 (define-key map [(button2)] 'muse-follow-name-at-mouse)
156 (define-key map [(shift button2)]
157 'muse-follow-name-at-mouse-other-window))
158 (define-key map [(shift control ?m)]
159 'muse-follow-name-at-point-other-window)
160 (define-key map [mouse-2] 'muse-follow-name-at-mouse)
161 (define-key map [(shift mouse-2)]
162 'muse-follow-name-at-mouse-other-window))
164 (define-key map [(shift tab)] 'muse-previous-reference)
165 (unless (featurep 'xemacs)
166 (define-key map [(shift iso-lefttab)] 'muse-previous-reference)
167 (define-key map [(shift control ?i)] 'muse-previous-reference))
169 (define-key map [(control ?c) (control ?f)] 'muse-project-find-file)
170 (define-key map [(control ?c) (control ?p)] 'muse-project-publish)
172 (define-key map [(control ?c) tab] 'muse-insert-thing)
174 ;; Searching functions
175 (define-key map [(control ?c) (control ?b)] 'muse-find-backlinks)
176 (define-key map [(control ?c) (control ?s)] 'muse-search)
178 ;; Enhanced list functions
179 (define-key map [(meta return)] 'muse-insert-list-item)
180 (define-key map [(control ?>)] 'muse-increase-list-item-indentation)
181 (define-key map [(control ?<)] 'muse-decrease-list-item-indentation)
183 (when (featurep 'pcomplete)
184 (define-key map [(meta tab)] 'pcomplete)
185 (define-key map [(meta control ?i)] 'pcomplete))
187 map)
188 "Keymap used by Emacs Muse mode.")
190 ;; Code:
192 ;;;###autoload
193 (define-derived-mode muse-mode text-mode "Muse"
194 "Muse is an Emacs mode for authoring and publishing documents.
195 \\{muse-mode-map}"
196 ;; Since we're not inheriting from normal-mode, we need to
197 ;; explicitly run file variables.
198 (condition-case err
199 (hack-local-variables)
200 (error (message "File local-variables error: %s"
201 (prin1-to-string err))))
202 ;; Avoid lock-up caused by use of the 'intangible' text property
203 ;; with flyspell.
204 (unless muse-mode-intangible-links
205 (set (make-local-variable 'inhibit-point-motion-hooks) t))
206 (setq muse-current-project (muse-project-of-file))
207 (muse-project-set-variables)
208 ;; Make fill not split up links
209 (when (boundp 'fill-nobreak-predicate)
210 (make-local-variable 'fill-nobreak-predicate)
211 ;; Work around annoying inconsistency in fill handling between
212 ;; Emacs 21 and 22.
213 (if (< emacs-major-version 22)
214 (setq fill-nobreak-predicate 'muse-mode-fill-nobreak-p)
215 (add-to-list 'fill-nobreak-predicate
216 'muse-mode-fill-nobreak-p)))
217 ;; Make fill work nicely with item lists
218 (set (make-local-variable 'adaptive-fill-regexp)
219 (concat "\\s-+\\(-\\|[0-9]+\\.\\)\\s-+\\|\\[[0-9]+\\]\\s-*"
220 "\\|\\s-*::\\s-*\\|\\s-*"))
221 (set (make-local-variable 'paragraph-start)
222 (concat paragraph-start "\\|\\s-+\\(-\\|[0-9]+\\.\\)\\s-+"
223 "\\|\\[[0-9]+\\]\\s-*\\|\\s-*::\\s-*"))
224 ;; Comment syntax is `; comment'
225 (set (make-local-variable 'comment-start)
226 "; ")
227 (set (make-local-variable 'comment-start-skip)
228 "^;\\s-+")
229 ;; If we're using Emacs21, this makes flyspell work like it should
230 (when (boundp 'flyspell-generic-check-word-p)
231 (set (make-local-variable 'flyspell-generic-check-word-p)
232 'muse-mode-flyspell-p))
233 ;; If pcomplete is available, set it up
234 (when (featurep 'pcomplete)
235 (set (make-local-variable 'pcomplete-default-completion-function)
236 'muse-mode-completions)
237 (set (make-local-variable 'pcomplete-command-completion-function)
238 'muse-mode-completions)
239 (set (make-local-variable 'pcomplete-parse-arguments-function)
240 'muse-mode-current-word))
241 ;; Initialize any auto-generated variables
242 (run-hooks 'muse-update-values-hook)
243 (when muse-mode-highlight-p
244 (muse-use-font-lock)))
246 (put 'muse-mode
247 'flyspell-mode-predicate
248 'muse-mode-flyspell-p)
250 (defun muse-mode-fill-nobreak-p ()
251 "Return nil if we should allow a fill to occur at point.
252 Otherwise return non-nil.
254 This is used to keep long explicit links from being mangled by
255 fill mode."
256 (save-excursion
257 (save-match-data
258 (and (re-search-backward "\\[\\[\\|\\]\\]"
259 (line-beginning-position) t)
260 (string= (or (match-string 0) "")
261 "[[")))))
263 (defun muse-mode-flyspell-p ()
264 "Return non-nil if we should allow spell-checking to occur at point.
265 Otherwise return nil.
267 This is used to keep links from being improperly colorized by flyspell."
268 (and (not (get-text-property (if (bobp) (point) (1- (point)))
269 'muse-link))
270 (save-match-data
271 (null (muse-link-at-point)))))
273 (defun muse-mode-choose-mode ()
274 "Turn the proper Emacs Muse related mode on for this file."
275 (let ((project (muse-project-of-file)))
276 (funcall (or (and project (muse-get-keyword :major-mode (cadr project) t))
277 'muse-mode))))
279 (defun muse-mode-maybe ()
280 "Maybe turn Emacs Muse mode on for this file."
281 (let ((project (muse-project-of-file)))
282 (and project
283 (funcall (or (muse-get-keyword :major-mode (cadr project) t)
284 'muse-mode)))))
286 ;;; Enhanced list editing
288 (defun muse-on-blank-line ()
289 "See if point is on a blank line"
290 (save-excursion
291 (beginning-of-line)
292 (looking-at (concat "[" muse-regexp-blank "]?[\n]+"))))
294 (defun muse-get-paragraph-start ()
295 "Return the start of the current paragraph. This function will
296 return nil if there are no prior paragraphs and the beginning of
297 the line if point is on a blank line."
298 (let ((para-start (concat "[\n]+[" muse-regexp-blank "]?[\n]+")))
299 ;; search back to start of paragraph
300 (save-excursion
301 (save-match-data
302 (if (not (muse-on-blank-line))
303 (re-search-backward para-start nil t)
304 (line-beginning-position))))))
306 (defun muse-insert-thing ()
307 "Prompt for something to insert into the current buffer."
308 (interactive)
309 (message "Insert:\nl link\nt tag")
310 (let (key cmd)
311 (let ((overriding-local-map muse-insert-map))
312 (setq key (read-key-sequence nil)))
313 (if (commandp (setq cmd (lookup-key muse-insert-map key)))
314 (progn (message "")
315 (call-interactively cmd))
316 (message "Not inserting anything"))))
318 ;;;###autoload
319 (defun muse-insert-list-item ()
320 "Insert a list item at the current point, taking into account
321 your current list type and indentation level."
322 (interactive)
323 (let ((newitem " - ")
324 (itemno nil)
325 (pstart (muse-get-paragraph-start))
326 (list-item (format muse-list-item-regexp
327 (concat "[" muse-regexp-blank "]*"))))
328 ;; search backwards for start of current item
329 (save-excursion
330 (when (re-search-backward list-item pstart t)
331 ;; save the matching item
332 (setq newitem (match-string 0))
333 ;; see what type it is
334 (if (string-match "::" (match-string 0))
335 ;; is a definition, replace the term
336 (setq newitem (concat " "
337 (read-string "Term: ")
338 " :: "))
339 ;; see if it's a numbered list
340 (when (string-match "[0-9]+" newitem)
341 ;; is numbered, so increment
342 (setq itemno (1+
343 (string-to-number
344 (match-string 0 newitem))))
345 (setq newitem (replace-match
346 (number-to-string itemno)
347 nil nil newitem))))))
348 ;; insert the new item
349 (insert (concat "\n" newitem))))
351 (defun muse-alter-list-item-indentation (operation)
352 "Alter the indentation of the current list item.
353 Valid values of OPERATION are 'increase and 'decrease."
354 (let ((pstart (muse-get-paragraph-start))
355 (list-item (format muse-list-item-regexp
356 (concat "[" muse-regexp-blank "]*")))
357 beg move-func indent)
358 ;; search backwards until start of paragraph to see if we are on a
359 ;; current item
360 (save-excursion
361 (if (or (progn (goto-char (muse-line-beginning-position))
362 ;; we are on an item
363 (looking-at list-item))
364 ;; not on item, so search backwards
365 (re-search-backward list-item pstart t))
366 (let ((beg (point)))
367 ;; we are on an item
368 (setq indent (buffer-substring (match-beginning 0)
369 (match-beginning 1)))
370 (muse-forward-list-item (muse-list-item-type (match-string 1))
371 (concat "[" muse-regexp-blank "]*")
373 (save-restriction
374 (narrow-to-region beg (point))
375 (goto-char (point-min))
376 (let ((halt nil))
377 (while (< (point) (point-max))
378 ;; increase or decrease the indentation
379 (unless halt
380 (cond ((eq operation 'increase)
381 (insert " "))
382 ((eq operation 'decrease)
383 (if (looking-at " ")
384 ;; we have enough space, so delete it
385 (delete-region (match-beginning 0)
386 (match-end 0))
387 (setq halt t)))))
388 (forward-line 1)))))
389 ;; we are not on an item, so warn
390 (message "You are not on a list item.")))))
392 ;;;###autoload
393 (defun muse-increase-list-item-indentation ()
394 "Increase the indentation of the current list item."
395 (interactive)
396 (muse-alter-list-item-indentation 'increase))
398 ;;;###autoload
399 (defun muse-decrease-list-item-indentation ()
400 "Decrease the indentation of the current list item."
401 (interactive)
402 (muse-alter-list-item-indentation 'decrease))
404 ;;; Support page name completion using pcomplete
406 (defun muse-mode-completions ()
407 "Return a list of possible completions names for this buffer."
408 (let ((project (muse-project-of-file)))
409 (if project
410 (while (pcomplete-here
411 (mapcar 'car (muse-project-file-alist project)))))))
413 (defun muse-mode-current-word ()
414 (let ((end (point)))
415 (save-excursion
416 (save-restriction
417 (skip-chars-backward (concat "^\\[\n" muse-regexp-blank))
418 (narrow-to-region (point) end))
419 (pcomplete-parse-buffer-arguments))))
421 ;;; Navigate/visit links or URLs. Use TAB, S-TAB and RET (or mouse-2).
423 (defun muse-link-at-point (&optional pos)
424 "Return link text if a URL or link is at point."
425 (let ((case-fold-search nil)
426 (inhibit-point-motion-hooks t)
427 (here (or pos (point))))
428 ;; if we are using muse-colors, we can just use link properties to
429 ;; determine whether we are on a link
430 (if (featurep 'muse-colors)
431 (when (get-text-property here 'muse-link)
432 (save-excursion
433 (when (and (not (bobp))
434 (get-text-property (1- here) 'muse-link))
435 (goto-char (or (previous-single-property-change here 'muse-link)
436 (point-min))))
437 (if (looking-at muse-explicit-link-regexp)
438 (progn
439 (goto-char (match-beginning 1))
440 (muse-handle-explicit-link))
441 (muse-handle-implicit-link))))
442 ;; use fallback method to find a link
443 (when (or (null pos)
444 (and (char-after pos)
445 (not (eq (char-syntax (char-after pos)) ?\ ))))
446 (save-excursion
447 (goto-char here)
448 ;; check for explicit link here or before point
449 (if (or (looking-at muse-explicit-link-regexp)
450 (and
451 (re-search-backward "\\[\\[\\|\\]\\]"
452 (muse-line-beginning-position)
454 (string= (or (match-string 0) "") "[[")
455 (looking-at muse-explicit-link-regexp)))
456 (progn
457 (goto-char (match-beginning 1))
458 (muse-handle-explicit-link))
459 (goto-char here)
460 ;; check for bare URL or other link type
461 (skip-chars-backward (concat "^'\"<>{}(\n" muse-regexp-blank))
462 (and (looking-at muse-implicit-link-regexp)
463 (muse-handle-implicit-link))))))))
465 (defun muse-make-link (link &optional desc)
466 "Return a link to LINK with DESC as the description."
467 (when (string-match muse-explicit-link-regexp link)
468 (unless desc (setq desc (muse-get-link-desc link)))
469 (setq link (muse-get-link link)))
470 (if (and desc
471 link
472 (not (string= desc ""))
473 (not (string= link desc)))
474 (concat "[[" (muse-link-escape link) "][" (muse-link-escape desc) "]]")
475 (concat "[[" (or (muse-link-escape link) "") "]]")))
477 ;;;###autoload
478 (defun muse-insert-relative-link-to-file ()
479 "Insert a relative link to a file, with optional description,
480 at the current point."
481 ;; Perhaps the relative location should be configurable, so that the
482 ;; file search would start in the publshing directory and then
483 ;; insert the link relative to the publishing directory
484 (interactive)
485 (insert
486 (muse-make-link
487 (file-relative-name (read-file-name "Link: "))
488 (read-string "Text: "))))
490 ;;;###autoload
491 (defun muse-edit-link-at-point ()
492 "Edit the current link.
493 Do not rename the page originally referred to."
494 (interactive)
495 (if (muse-link-at-point)
496 (let ((link (muse-link-unescape (muse-get-link)))
497 (desc (muse-link-unescape (muse-get-link-desc))))
498 (replace-match
499 (save-match-data
500 (muse-make-link
501 (read-string "Link: " link)
502 (read-string "Text: " desc)))
503 t t))
504 (error "There is no valid link at point")))
506 (defun muse-visit-link-default (link &optional other-window)
507 "Visit the URL or link named by LINK.
508 If ANCHOR is specified, search for it after opening LINK.
510 This is the default function to call when visiting links; it is
511 used by `muse-visit-link' if you have not specified :visit-link
512 in `muse-project-alist'."
513 (if (string-match muse-url-regexp link)
514 (muse-browse-url link)
515 (let (anchor
516 base-buffer)
517 (when (string-match "#" link)
518 (setq anchor (substring link (match-beginning 0))
519 link (if (= (match-beginning 0) 0)
520 ;; If there is an anchor but no link, default
521 ;; to the current page.
523 (substring link 0 (match-beginning 0)))))
524 (when link
525 (setq base-buffer (get-buffer link))
526 (if (and base-buffer (not (buffer-file-name base-buffer)))
527 ;; If file is temporary (no associated file), just switch to
528 ;; the buffer
529 (if other-window
530 (switch-to-buffer-other-window base-buffer)
531 (switch-to-buffer base-buffer))
532 (let ((project (muse-project-of-file)))
533 (if project
534 (muse-project-find-file link project
535 (and other-window
536 'find-file-other-window))
537 (if other-window
538 (find-file-other-window link)
539 (find-file link))))))
540 (when anchor
541 (let ((pos (point)))
542 (goto-char (point-min))
543 (unless (re-search-forward (concat "^\\W*" (regexp-quote anchor)
544 "\\b")
545 nil t)
546 (goto-char pos)))))))
548 (defun muse-visit-link (link &optional other-window)
549 "Visit the URL or link named by LINK."
550 (let ((visit-link-function
551 (muse-get-keyword :visit-link (cadr (muse-project-of-file)) t)))
552 (if visit-link-function
553 (funcall visit-link-function link other-window)
554 (muse-visit-link-default link other-window))))
556 ;;;###autoload
557 (defun muse-browse-result (style &optional other-window)
558 "Visit the current page's published result."
559 (interactive (list (muse-publish-get-style
560 (mapcar
561 (lambda (style)
562 (cons (muse-get-keyword :base style) style))
563 (muse-project-applicable-styles
564 buffer-file-name (cddr muse-current-project))))
565 current-prefix-arg))
566 (setq style (muse-style style))
567 (let ((result-path
568 (muse-publish-output-file buffer-file-name
569 (muse-style-element :path style) style)))
570 (if (not (file-readable-p result-path))
571 (error "Cannot open output file '%s'" result-path)
572 (if other-window
573 (find-file-other-window result-path)
574 (let ((func (muse-style-element :browser style t)))
575 (if func
576 (funcall func result-path)
577 (message "The %s publishing style does not support browsing."
578 style)))))))
580 ;;;###autoload
581 (defun muse-follow-name-at-point (&optional other-window)
582 "Visit the link at point, or insert a newline if none is found."
583 (interactive "P")
584 (let ((link (muse-link-at-point)))
585 (if link
586 (muse-visit-link link other-window)
587 (error "There is no valid link at point"))))
589 ;;;###autoload
590 (defun muse-follow-name-at-point-other-window ()
591 "Visit the link at point in other window."
592 (interactive)
593 (muse-follow-name-at-point t))
595 (defun muse-follow-name-at-mouse (event &optional other-window)
596 "Visit the link at point, or yank text if none is found."
597 (interactive "eN")
598 (save-excursion
599 (cond ((fboundp 'event-window) ; XEmacs
600 (set-buffer (window-buffer (event-window event)))
601 (and (funcall (symbol-function 'event-point) event)
602 (goto-char (funcall (symbol-function 'event-point) event))))
603 ((fboundp 'posn-window) ; Emacs
604 (set-buffer (window-buffer (posn-window (event-start event))))
605 (goto-char (posn-point (event-start event)))))
606 (let ((link (muse-link-at-point)))
607 (if link
608 (muse-visit-link link other-window)
609 ;; Fall back to normal binding for this event
610 (call-interactively
611 (lookup-key (current-global-map) (this-command-keys)))))))
613 (defun muse-follow-name-at-mouse-other-window (event)
614 "Visit the link at point"
615 (interactive "e")
616 ;; throw away the old window position, since other-window will
617 ;; change it anyway
618 (select-window (car (cadr event)))
619 (muse-follow-name-at-mouse event t))
621 ;;;###autoload
622 (defun muse-next-reference ()
623 "Move forward to next Muse link or URL, cycling if necessary."
624 (interactive)
625 (let ((cycled 0) pos)
626 (save-excursion
627 (when (get-text-property (point) 'muse-link)
628 (goto-char (or (next-single-property-change (point) 'muse-link)
629 (point-max))))
630 (while (< cycled 2)
631 (let ((next (point)))
632 (if (while (and (null pos)
633 (setq next
634 (next-single-property-change next 'muse-link)))
635 (when (get-text-property next 'muse-link)
636 (setq pos next)))
637 (setq cycled 2)
638 (goto-char (point-min))
639 (setq cycled (1+ cycled))))))
640 (if pos
641 (goto-char pos))))
643 ;;;###autoload
644 (defun muse-previous-reference ()
645 "Move backward to the next Muse link or URL, cycling if necessary.
646 This function is not entirely accurate, but it's close enough."
647 (interactive)
648 (let ((cycled 0) pos)
649 (save-excursion
650 (while (< cycled 2)
651 (let ((prev (point)))
652 (if (while (and (null pos)
653 (setq prev
654 (previous-single-property-change
655 prev 'muse-link)))
656 (when (get-text-property prev 'muse-link)
657 (setq pos prev)))
658 (setq cycled 2)
659 (goto-char (point-max))
660 (setq cycled (1+ cycled))))))
661 (if pos
662 (goto-char pos))))
664 ;;;###autoload
665 (defun muse-what-changed ()
666 "Show the unsaved changes that have been made to the current file."
667 (interactive)
668 (diff-backup buffer-file-name))
671 ;;; Find text in project pages, or pages referring to the current page
673 (defvar muse-search-history nil)
675 (defun muse-grep (string &optional grep-command-no-shadow)
676 "Grep for STRING in the project directories.
677 GREP-COMMAND if passed will supplant `muse-grep-command'."
678 ;; careful - grep-command leaks into compile, so we call it
679 ;; -no-shadow instead
680 (require 'compile)
681 (let* ((str (or grep-command-no-shadow muse-grep-command))
682 (muse-directories (mapcar
683 (lambda (thing)
684 (car (cadr thing)))
685 muse-project-alist))
686 (dirs (mapconcat (lambda (dir)
687 (shell-quote-argument
688 (expand-file-name dir)))
689 muse-directories " ")))
690 (while (string-match "%W" str)
691 (setq str (replace-match string t t str)))
692 (while (string-match "%D" str)
693 (setq str (replace-match dirs t t str)))
694 (if (fboundp 'compilation-start)
695 (compilation-start str nil (lambda (&rest args) "*search*")
696 grep-regexp-alist)
697 (and (fboundp 'compile-internal)
698 (compile-internal str "No more search hits" "search"
699 nil grep-regexp-alist)))))
701 ;;;###autoload
702 (defun muse-search-with-command (text)
703 "Search for the given TEXT string in the project directories
704 using the specified command."
705 (interactive
706 (list (let ((str (concat muse-grep-command)) pos)
707 (when (string-match "%W" str)
708 (setq pos (match-beginning 0))
709 (unless (featurep 'xemacs)
710 (setq pos (1+ pos)))
711 (setq str (replace-match "" t t str)))
712 (read-from-minibuffer "Search command: "
713 (cons str pos) nil nil
714 'muse-search-history))))
715 (muse-grep nil text))
717 ;;;###autoload
718 (defun muse-search ()
719 "Search for the given TEXT using the default grep command."
720 (interactive)
721 (muse-grep (read-string "Search: ")))
723 ;;;###autoload
724 (defun muse-find-backlinks ()
725 "Grep for the current pagename in all the project directories."
726 (interactive)
727 (muse-grep (muse-page-name)))
730 ;;; Generate an index of all known Muse pages
732 (defun muse-generate-index (&optional as-list exclude-private)
733 "Generate an index of all Muse pages."
734 (let ((index (muse-index-as-string as-list exclude-private)))
735 (with-current-buffer (get-buffer-create "*Muse Index*")
736 (erase-buffer)
737 (insert index)
738 (current-buffer))))
740 ;;;###autoload
741 (defun muse-index ()
742 "Display an index of all known Muse pages."
743 (interactive)
744 (message "Generating Muse index...")
745 (let ((project (muse-project)))
746 (with-current-buffer (muse-generate-index)
747 (goto-char (point-min))
748 (muse-mode)
749 (setq muse-current-project project)
750 (pop-to-buffer (current-buffer))))
751 (message "Generating Muse index...done"))
753 (defun muse-index-as-string (&optional as-list exclude-private exclude-current)
754 "Generate an index of all Muse pages.
755 If AS-LIST is non-nil, insert a dash and spaces before each item.
756 If EXCLUDE-PRIVATE is non-nil, exclude files that have private permissions.
757 If EXCLUDE-CURRENT is non-nil, exclude the current file from the output."
758 (let ((files (sort (copy-alist (muse-project-file-alist))
759 (function
760 (lambda (l r)
761 (string-lessp (car l) (car r)))))))
762 (when (and exclude-current (muse-page-name))
763 (setq files (delete (assoc (muse-page-name) files) files)))
764 (with-temp-buffer
765 (while files
766 (unless (and exclude-private
767 (muse-project-private-p (cdar files)))
768 (insert (if as-list " - " "") "[[" (caar files) "]]\n"))
769 (setq files (cdr files)))
770 (buffer-string))))
772 ;;; Insert tags interactively on C-c TAB t
774 (defvar muse-tag-history nil
775 "List of recently-entered tags; used by `muse-insert-tag'.
776 If you want a tag to start as the default, you may manually set
777 this variable to a list.")
779 (defvar muse-custom-tags nil
780 "Keep track of any new tags entered in `muse-insert-tag'.
781 If there are (X)HTML tags that you use frequently with that
782 function, you might want to set this manually.")
784 ;;;###autoload
785 (defun muse-insert-tag (tag)
786 "Insert a tag interactively with a blank line after it."
787 (interactive
788 (list
789 (completing-read
790 (concat "Tag: "
791 (when muse-tag-history
792 (concat "(default: " (car muse-tag-history) ") ")))
793 (progn
794 (require 'muse-publish)
795 (mapcar 'list (nconc (mapcar 'car muse-publish-markup-tags)
796 muse-custom-tags)))
797 nil nil nil 'muse-tag-history
798 (car muse-tag-history))))
799 (when (equal tag "")
800 (setq tag (car muse-tag-history)))
801 (unless (interactive-p)
802 (require 'muse-publish))
803 (let ((tag-entry (assoc tag muse-publish-markup-tags))
804 (options ""))
805 ;; Add to custom list if no entry exists
806 (unless tag-entry
807 (add-to-list 'muse-custom-tags tag))
808 ;; Get option
809 (when (nth 2 tag-entry)
810 (setq options (read-string "Option: ")))
811 (unless (equal options "")
812 (setq options (concat " " options)))
813 ;; Insert the tag, closing if necessary
814 (when tag (insert (concat "<" tag options ">")))
815 (when (nth 1 tag-entry)
816 (insert (concat "\n\n</" tag ">\n"))
817 (forward-line -2))))
819 (provide 'muse-mode)
821 ;;; muse-mode.el ends here