Merged from mwolson@gnu.org--2006-muse-el (patch 92)
[muse-el.git] / lisp / muse-mode.el
blob52e7985e4d87087e198aeb19385c6b9878f3d1f9
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)
136 (define-key map "u" 'muse-insert-url)
138 map))
140 (defvar muse-mode-map
141 (let ((map (make-sparse-keymap)))
142 (define-key map [(control ?c) (control ?a)] 'muse-index)
143 (define-key map [(control ?c) (control ?c)] 'muse-follow-name-at-point)
144 (define-key map [(control ?c) (control ?e)] 'muse-edit-link-at-point)
145 (define-key map [(control ?c) (control ?l)] 'font-lock-mode)
146 (define-key map [(control ?c) (control ?t)]
147 'muse-project-publish-this-file)
148 (define-key map [(control ?c) (control ?T)] 'muse-publish-this-file)
149 (define-key map [(control ?c) (control ?v)] 'muse-browse-result)
151 (define-key map [(control ?c) ?=] 'muse-what-changed)
153 (define-key map [tab] 'muse-next-reference)
154 (define-key map [(control ?i)] 'muse-next-reference)
156 (if (featurep 'xemacs)
157 (progn
158 (define-key map [(button2)] 'muse-follow-name-at-mouse)
159 (define-key map [(shift button2)]
160 'muse-follow-name-at-mouse-other-window))
161 (define-key map [(shift control ?m)]
162 'muse-follow-name-at-point-other-window)
163 (define-key map [mouse-2] 'muse-follow-name-at-mouse)
164 (define-key map [(shift mouse-2)]
165 'muse-follow-name-at-mouse-other-window))
167 (define-key map [(shift tab)] 'muse-previous-reference)
168 (unless (featurep 'xemacs)
169 (define-key map [(shift iso-lefttab)] 'muse-previous-reference)
170 (define-key map [(shift control ?i)] 'muse-previous-reference))
172 (define-key map [(control ?c) (control ?f)] 'muse-project-find-file)
173 (define-key map [(control ?c) (control ?p)] 'muse-project-publish)
175 (define-key map [(control ?c) tab] 'muse-insert-thing)
177 ;; Searching functions
178 (define-key map [(control ?c) (control ?b)] 'muse-find-backlinks)
179 (define-key map [(control ?c) (control ?s)] 'muse-search)
181 ;; Enhanced list functions
182 (define-key map [(meta return)] 'muse-insert-list-item)
183 (define-key map [(control ?>)] 'muse-increase-list-item-indentation)
184 (define-key map [(control ?<)] 'muse-decrease-list-item-indentation)
186 (when (featurep 'pcomplete)
187 (define-key map [(meta tab)] 'pcomplete)
188 (define-key map [(meta control ?i)] 'pcomplete))
190 map)
191 "Keymap used by Emacs Muse mode.")
193 ;; Code:
195 ;;;###autoload
196 (define-derived-mode muse-mode text-mode "Muse"
197 "Muse is an Emacs mode for authoring and publishing documents.
198 \\{muse-mode-map}"
199 ;; Since we're not inheriting from normal-mode, we need to
200 ;; explicitly run file variables.
201 (condition-case err
202 (hack-local-variables)
203 (error (message "File local-variables error: %s"
204 (prin1-to-string err))))
205 ;; Avoid lock-up caused by use of the 'intangible' text property
206 ;; with flyspell.
207 (unless muse-mode-intangible-links
208 (set (make-local-variable 'inhibit-point-motion-hooks) t))
209 (setq muse-current-project (muse-project-of-file))
210 (muse-project-set-variables)
211 ;; Make fill not split up links
212 (when (boundp 'fill-nobreak-predicate)
213 (make-local-variable 'fill-nobreak-predicate)
214 ;; Work around annoying inconsistency in fill handling between
215 ;; Emacs 21 and 22.
216 (if (< emacs-major-version 22)
217 (setq fill-nobreak-predicate 'muse-mode-fill-nobreak-p)
218 (add-to-list 'fill-nobreak-predicate
219 'muse-mode-fill-nobreak-p)))
220 ;; Make fill work nicely with item lists
221 (set (make-local-variable 'adaptive-fill-regexp)
222 (concat "\\s-+\\(-\\|[0-9]+\\.\\)\\s-+\\|\\[[0-9]+\\]\\s-*"
223 "\\|.*\\s-*::\\s-+\\|\\s-*"))
224 (set (make-local-variable 'paragraph-start)
225 (concat paragraph-start
226 "\\|\\s-+\\(-\\|[0-9]+\\.\\)\\s-+\\|\\[[0-9]+\\]\\s-*"
227 "\\|.*\\s-*::\\s-+"))
228 ;; Comment syntax is `; comment'
229 (set (make-local-variable 'comment-start)
230 "; ")
231 (set (make-local-variable 'comment-start-skip)
232 "^;\\s-+")
233 ;; If we're using Emacs21, this makes flyspell work like it should
234 (when (boundp 'flyspell-generic-check-word-p)
235 (set (make-local-variable 'flyspell-generic-check-word-p)
236 'muse-mode-flyspell-p))
237 ;; If pcomplete is available, set it up
238 (when (featurep 'pcomplete)
239 (set (make-local-variable 'pcomplete-default-completion-function)
240 'muse-mode-completions)
241 (set (make-local-variable 'pcomplete-command-completion-function)
242 'muse-mode-completions)
243 (set (make-local-variable 'pcomplete-parse-arguments-function)
244 'muse-mode-current-word))
245 ;; Initialize any auto-generated variables
246 (run-hooks 'muse-update-values-hook)
247 (when muse-mode-highlight-p
248 (muse-use-font-lock)))
250 (put 'muse-mode
251 'flyspell-mode-predicate
252 'muse-mode-flyspell-p)
254 (defun muse-mode-fill-nobreak-p ()
255 "Return nil if we should allow a fill to occur at point.
256 Otherwise return non-nil.
258 This is used to keep long explicit links from being mangled by
259 fill mode."
260 (save-excursion
261 (save-match-data
262 (and (re-search-backward "\\[\\[\\|\\]\\]"
263 (line-beginning-position) t)
264 (string= (or (match-string 0) "")
265 "[[")))))
267 (defun muse-mode-flyspell-p ()
268 "Return non-nil if we should allow spell-checking to occur at point.
269 Otherwise return nil.
271 This is used to keep links from being improperly colorized by flyspell."
272 (and (not (get-text-property (if (bobp) (point) (1- (point)))
273 'muse-link))
274 (save-match-data
275 (null (muse-link-at-point)))))
277 (defun muse-mode-choose-mode ()
278 "Turn the proper Emacs Muse related mode on for this file."
279 (let ((project (muse-project-of-file)))
280 (funcall (or (and project (muse-get-keyword :major-mode (cadr project) t))
281 'muse-mode))))
283 (defun muse-mode-maybe ()
284 "Maybe turn Emacs Muse mode on for this file."
285 (let ((project (muse-project-of-file)))
286 (and project
287 (funcall (or (muse-get-keyword :major-mode (cadr project) t)
288 'muse-mode)))))
290 ;;; Enhanced list editing
292 (defun muse-on-blank-line ()
293 "See if point is on a blank line"
294 (save-excursion
295 (beginning-of-line)
296 (looking-at (concat "[" muse-regexp-blank "]?[\n]+"))))
298 (defun muse-get-paragraph-start ()
299 "Return the start of the current paragraph. This function will
300 return nil if there are no prior paragraphs and the beginning of
301 the line if point is on a blank line."
302 (let ((para-start (concat "[\n]+[" muse-regexp-blank "]?[\n]+")))
303 ;; search back to start of paragraph
304 (save-excursion
305 (save-match-data
306 (if (not (muse-on-blank-line))
307 (re-search-backward para-start nil t)
308 (line-beginning-position))))))
310 (defun muse-insert-thing ()
311 "Prompt for something to insert into the current buffer."
312 (interactive)
313 (message "Insert:\nl link\nt Muse tag\nu URL")
314 (let (key cmd)
315 (let ((overriding-local-map muse-insert-map))
316 (setq key (read-key-sequence nil)))
317 (if (commandp (setq cmd (lookup-key muse-insert-map key)))
318 (progn (message "")
319 (call-interactively cmd))
320 (message "Not inserting anything"))))
322 ;;;###autoload
323 (defun muse-insert-list-item ()
324 "Insert a list item at the current point, taking into account
325 your current list type and indentation level."
326 (interactive)
327 (let ((newitem " - ")
328 (itemno nil)
329 (pstart (muse-get-paragraph-start))
330 (list-item (format muse-list-item-regexp
331 (concat "[" muse-regexp-blank "]*"))))
332 ;; search backwards for start of current item
333 (save-excursion
334 (when (re-search-backward list-item pstart t)
335 ;; save the matching item
336 (setq newitem (match-string 0))
337 ;; see what type it is
338 (if (string-match "::" (match-string 0))
339 ;; is a definition, replace the term
340 (setq newitem (concat " "
341 (read-string "Term: ")
342 " :: "))
343 ;; see if it's a numbered list
344 (when (string-match "[0-9]+" newitem)
345 ;; is numbered, so increment
346 (setq itemno (1+
347 (string-to-number
348 (match-string 0 newitem))))
349 (setq newitem (replace-match
350 (number-to-string itemno)
351 nil nil newitem))))))
352 ;; insert the new item
353 (insert (concat "\n" newitem))))
355 (defun muse-alter-list-item-indentation (operation)
356 "Alter the indentation of the current list item.
357 Valid values of OPERATION are 'increase and 'decrease."
358 (let ((pstart (muse-get-paragraph-start))
359 (list-item (format muse-list-item-regexp
360 (concat "[" muse-regexp-blank "]*")))
361 beg move-func indent)
362 ;; search backwards until start of paragraph to see if we are on a
363 ;; current item
364 (save-excursion
365 (if (or (progn (goto-char (muse-line-beginning-position))
366 ;; we are on an item
367 (looking-at list-item))
368 ;; not on item, so search backwards
369 (re-search-backward list-item pstart t))
370 (let ((beg (point)))
371 ;; we are on an item
372 (setq indent (buffer-substring (match-beginning 0)
373 (match-beginning 1)))
374 (muse-forward-list-item (muse-list-item-type (match-string 1))
375 (concat "[" muse-regexp-blank "]*")
377 (save-restriction
378 (narrow-to-region beg (point))
379 (goto-char (point-min))
380 (let ((halt nil))
381 (while (< (point) (point-max))
382 ;; increase or decrease the indentation
383 (unless halt
384 (cond ((eq operation 'increase)
385 (insert " "))
386 ((eq operation 'decrease)
387 (if (looking-at " ")
388 ;; we have enough space, so delete it
389 (delete-region (match-beginning 0)
390 (match-end 0))
391 (setq halt t)))))
392 (forward-line 1)))))
393 ;; we are not on an item, so warn
394 (message "You are not on a list item.")))))
396 ;;;###autoload
397 (defun muse-increase-list-item-indentation ()
398 "Increase the indentation of the current list item."
399 (interactive)
400 (muse-alter-list-item-indentation 'increase))
402 ;;;###autoload
403 (defun muse-decrease-list-item-indentation ()
404 "Decrease the indentation of the current list item."
405 (interactive)
406 (muse-alter-list-item-indentation 'decrease))
408 ;;; Support page name completion using pcomplete
410 (defun muse-mode-completions ()
411 "Return a list of possible completions names for this buffer."
412 (let ((project (muse-project-of-file)))
413 (if project
414 (while (pcomplete-here
415 (mapcar 'car (muse-project-file-alist project)))))))
417 (defun muse-mode-current-word ()
418 (let ((end (point)))
419 (save-excursion
420 (save-restriction
421 (skip-chars-backward (concat "^\\[\n" muse-regexp-blank))
422 (narrow-to-region (point) end))
423 (pcomplete-parse-buffer-arguments))))
425 ;;; Navigate/visit links or URLs. Use TAB, S-TAB and RET (or mouse-2).
427 (defun muse-link-at-point (&optional pos)
428 "Return link text if a URL or link is at point."
429 (let ((case-fold-search nil)
430 (inhibit-point-motion-hooks t)
431 (here (or pos (point))))
432 ;; if we are using muse-colors, we can just use link properties to
433 ;; determine whether we are on a link
434 (if (featurep 'muse-colors)
435 (when (get-text-property here 'muse-link)
436 (save-excursion
437 (when (and (not (bobp))
438 (get-text-property (1- here) 'muse-link))
439 (goto-char (or (previous-single-property-change here 'muse-link)
440 (point-min))))
441 (if (looking-at muse-explicit-link-regexp)
442 (progn
443 (goto-char (match-beginning 1))
444 (muse-handle-explicit-link))
445 (muse-handle-implicit-link))))
446 ;; use fallback method to find a link
447 (when (or (null pos)
448 (and (char-after pos)
449 (not (eq (char-syntax (char-after pos)) ?\ ))))
450 (save-excursion
451 (goto-char here)
452 ;; check for explicit link here or before point
453 (if (or (looking-at muse-explicit-link-regexp)
454 (and
455 (re-search-backward "\\[\\[\\|\\]\\]"
456 (muse-line-beginning-position)
458 (string= (or (match-string 0) "") "[[")
459 (looking-at muse-explicit-link-regexp)))
460 (progn
461 (goto-char (match-beginning 1))
462 (muse-handle-explicit-link))
463 (goto-char here)
464 ;; check for bare URL or other link type
465 (skip-chars-backward (concat "^'\"<>{}(\n" muse-regexp-blank))
466 (and (looking-at muse-implicit-link-regexp)
467 (muse-handle-implicit-link))))))))
469 (defun muse-make-link (link &optional desc)
470 "Return a link to LINK with DESC as the description."
471 (when (string-match muse-explicit-link-regexp link)
472 (unless desc (setq desc (muse-get-link-desc link)))
473 (setq link (muse-get-link link)))
474 (if (and desc
475 link
476 (not (string= desc ""))
477 (not (string= link desc)))
478 (concat "[[" (muse-link-escape link) "][" (muse-link-escape desc) "]]")
479 (concat "[[" (or (muse-link-escape link) "") "]]")))
481 ;;;###autoload
482 (defun muse-insert-relative-link-to-file ()
483 "Insert a relative link to a file, with optional description, at point."
484 ;; Perhaps the relative location should be configurable, so that the
485 ;; file search would start in the publishing directory and then
486 ;; insert the link relative to the publishing directory
487 (interactive)
488 (insert
489 (muse-make-link (file-relative-name (read-file-name "Link: "))
490 (read-string "Text: "))))
492 (defun muse-insert-url ()
493 "Insert a URL, with optional description, at point."
494 (interactive)
495 (insert
496 (muse-make-link (read-string "URL: ")
497 (read-string "Text: "))))
499 ;;;###autoload
500 (defun muse-edit-link-at-point ()
501 "Edit the current link.
502 Do not rename the page originally referred to."
503 (interactive)
504 (if (muse-link-at-point)
505 (let ((link (muse-link-unescape (muse-get-link)))
506 (desc (muse-link-unescape (muse-get-link-desc))))
507 (replace-match
508 (save-match-data
509 (muse-make-link
510 (read-string "Link: " link)
511 (read-string "Text: " desc)))
512 t t))
513 (error "There is no valid link at point")))
515 (defun muse-visit-link-default (link &optional other-window)
516 "Visit the URL or link named by LINK.
517 If ANCHOR is specified, search for it after opening LINK.
519 This is the default function to call when visiting links; it is
520 used by `muse-visit-link' if you have not specified :visit-link
521 in `muse-project-alist'."
522 (if (string-match muse-url-regexp link)
523 (muse-browse-url link)
524 (let (anchor
525 base-buffer)
526 (when (string-match "#" link)
527 (setq anchor (substring link (match-beginning 0))
528 link (if (= (match-beginning 0) 0)
529 ;; If there is an anchor but no link, default
530 ;; to the current page.
532 (substring link 0 (match-beginning 0)))))
533 (when link
534 (setq base-buffer (get-buffer link))
535 (if (and base-buffer (not (buffer-file-name base-buffer)))
536 ;; If file is temporary (no associated file), just switch to
537 ;; the buffer
538 (if other-window
539 (switch-to-buffer-other-window base-buffer)
540 (switch-to-buffer base-buffer))
541 (let ((project (muse-project-of-file)))
542 (if project
543 (muse-project-find-file link project
544 (and other-window
545 'find-file-other-window))
546 (if other-window
547 (find-file-other-window link)
548 (find-file link))))))
549 (when anchor
550 (let ((pos (point)))
551 (goto-char (point-min))
552 (unless (re-search-forward (concat "^\\W*" (regexp-quote anchor)
553 "\\b")
554 nil t)
555 (goto-char pos)))))))
557 (defun muse-visit-link (link &optional other-window)
558 "Visit the URL or link named by LINK."
559 (let ((visit-link-function
560 (muse-get-keyword :visit-link (cadr (muse-project-of-file)) t)))
561 (if visit-link-function
562 (funcall visit-link-function link other-window)
563 (muse-visit-link-default link other-window))))
565 ;;;###autoload
566 (defun muse-browse-result (style &optional other-window)
567 "Visit the current page's published result."
568 (interactive
569 (list (muse-project-get-applicable-style buffer-file-name
570 (cddr muse-current-project))
571 current-prefix-arg))
572 (setq style (muse-style style))
573 (let ((result-path
574 (muse-publish-output-file buffer-file-name
575 (muse-style-element :path style) style)))
576 (if (not (file-readable-p result-path))
577 (error "Cannot open output file '%s'" result-path)
578 (if other-window
579 (find-file-other-window result-path)
580 (let ((func (muse-style-element :browser style t)))
581 (if func
582 (funcall func result-path)
583 (message "The %s publishing style does not support browsing."
584 style)))))))
586 ;;;###autoload
587 (defun muse-follow-name-at-point (&optional other-window)
588 "Visit the link at point."
589 (interactive "P")
590 (let ((link (muse-link-at-point)))
591 (if link
592 (muse-visit-link link other-window)
593 (error "There is no valid link at point"))))
595 ;;;###autoload
596 (defun muse-follow-name-at-point-other-window ()
597 "Visit the link at point in other window."
598 (interactive)
599 (muse-follow-name-at-point t))
601 (defun muse-follow-name-at-mouse (event &optional other-window)
602 "Visit the link at point, or yank text if none is found."
603 (interactive "eN")
604 (unless
605 (save-excursion
606 (cond ((fboundp 'event-window) ; XEmacs
607 (set-buffer (window-buffer (event-window event)))
608 (and (funcall (symbol-function 'event-point) event)
609 (goto-char (funcall (symbol-function 'event-point)
610 event))))
611 ((fboundp 'posn-window) ; Emacs
612 (set-buffer (window-buffer (posn-window (event-start event))))
613 (goto-char (posn-point (event-start event)))))
614 (let ((link (muse-link-at-point)))
615 (when link
616 (muse-visit-link link other-window)
617 t)))
618 ;; Fall back to normal binding for this event
619 (call-interactively
620 (lookup-key (current-global-map) (this-command-keys)))))
622 (defun muse-follow-name-at-mouse-other-window (event)
623 "Visit the link at point"
624 (interactive "e")
625 ;; throw away the old window position, since other-window will
626 ;; change it anyway
627 (select-window (car (cadr event)))
628 (muse-follow-name-at-mouse event t))
630 ;;;###autoload
631 (defun muse-next-reference ()
632 "Move forward to next Muse link or URL, cycling if necessary."
633 (interactive)
634 (let ((cycled 0) pos)
635 (save-excursion
636 (when (get-text-property (point) 'muse-link)
637 (goto-char (or (next-single-property-change (point) 'muse-link)
638 (point-max))))
639 (while (< cycled 2)
640 (let ((next (point)))
641 (if (while (and (null pos)
642 (setq next
643 (next-single-property-change next 'muse-link)))
644 (when (get-text-property next 'muse-link)
645 (setq pos next)))
646 (setq cycled 2)
647 (goto-char (point-min))
648 (setq cycled (1+ cycled))))))
649 (if pos
650 (goto-char pos))))
652 ;;;###autoload
653 (defun muse-previous-reference ()
654 "Move backward to the next Muse link or URL, cycling if necessary.
655 This function is not entirely accurate, but it's close enough."
656 (interactive)
657 (let ((cycled 0) pos)
658 (save-excursion
659 (while (< cycled 2)
660 (let ((prev (point)))
661 (if (while (and (null pos)
662 (setq prev
663 (previous-single-property-change
664 prev 'muse-link)))
665 (when (get-text-property prev 'muse-link)
666 (setq pos prev)))
667 (setq cycled 2)
668 (goto-char (point-max))
669 (setq cycled (1+ cycled))))))
670 (if pos
671 (goto-char pos))))
673 ;;;###autoload
674 (defun muse-what-changed ()
675 "Show the unsaved changes that have been made to the current file."
676 (interactive)
677 (diff-backup buffer-file-name))
680 ;;; Find text in project pages, or pages referring to the current page
682 (defvar muse-search-history nil)
684 (defun muse-grep (string &optional grep-command-no-shadow)
685 "Grep for STRING in the project directories.
686 GREP-COMMAND if passed will supplant `muse-grep-command'."
687 ;; careful - grep-command leaks into compile, so we call it
688 ;; -no-shadow instead
689 (require 'compile)
690 (let* ((str (or grep-command-no-shadow muse-grep-command))
691 (muse-directories (mapcar
692 (lambda (thing)
693 (car (cadr thing)))
694 muse-project-alist))
695 (dirs (mapconcat (lambda (dir)
696 (shell-quote-argument
697 (expand-file-name dir)))
698 muse-directories " ")))
699 (while (string-match "%W" str)
700 (setq str (replace-match string t t str)))
701 (while (string-match "%D" str)
702 (setq str (replace-match dirs t t str)))
703 (if (fboundp 'compilation-start)
704 (compilation-start str nil (lambda (&rest args) "*search*")
705 grep-regexp-alist)
706 (and (fboundp 'compile-internal)
707 (compile-internal str "No more search hits" "search"
708 nil grep-regexp-alist)))))
710 ;;;###autoload
711 (defun muse-search-with-command (text)
712 "Search for the given TEXT string in the project directories
713 using the specified command."
714 (interactive
715 (list (let ((str (concat muse-grep-command)) pos)
716 (when (string-match "%W" str)
717 (setq pos (match-beginning 0))
718 (unless (featurep 'xemacs)
719 (setq pos (1+ pos)))
720 (setq str (replace-match "" t t str)))
721 (read-from-minibuffer "Search command: "
722 (cons str pos) nil nil
723 'muse-search-history))))
724 (muse-grep nil text))
726 ;;;###autoload
727 (defun muse-search ()
728 "Search for the given TEXT using the default grep command."
729 (interactive)
730 (muse-grep (read-string "Search: ")))
732 ;;;###autoload
733 (defun muse-find-backlinks ()
734 "Grep for the current pagename in all the project directories."
735 (interactive)
736 (muse-grep (muse-page-name)))
739 ;;; Generate an index of all known Muse pages
741 (defun muse-generate-index (&optional as-list exclude-private)
742 "Generate an index of all Muse pages."
743 (let ((index (muse-index-as-string as-list exclude-private)))
744 (with-current-buffer (get-buffer-create "*Muse Index*")
745 (erase-buffer)
746 (insert index)
747 (current-buffer))))
749 ;;;###autoload
750 (defun muse-index ()
751 "Display an index of all known Muse pages."
752 (interactive)
753 (message "Generating Muse index...")
754 (let ((project (muse-project)))
755 (with-current-buffer (muse-generate-index)
756 (goto-char (point-min))
757 (muse-mode)
758 (setq muse-current-project project)
759 (pop-to-buffer (current-buffer))))
760 (message "Generating Muse index...done"))
762 (defun muse-index-as-string (&optional as-list exclude-private exclude-current)
763 "Generate an index of all Muse pages.
764 If AS-LIST is non-nil, insert a dash and spaces before each item.
765 If EXCLUDE-PRIVATE is non-nil, exclude files that have private permissions.
766 If EXCLUDE-CURRENT is non-nil, exclude the current file from the output."
767 (let ((files (sort (copy-alist (muse-project-file-alist))
768 (function
769 (lambda (l r)
770 (string-lessp (car l) (car r)))))))
771 (when (and exclude-current (muse-page-name))
772 (setq files (delete (assoc (muse-page-name) files) files)))
773 (with-temp-buffer
774 (while files
775 (unless (and exclude-private
776 (muse-project-private-p (cdar files)))
777 (insert (if as-list " - " "") "[[" (caar files) "]]\n"))
778 (setq files (cdr files)))
779 (buffer-string))))
781 ;;; Insert tags interactively on C-c TAB t
783 (defvar muse-tag-history nil
784 "List of recently-entered tags; used by `muse-insert-tag'.
785 If you want a tag to start as the default, you may manually set
786 this variable to a list.")
788 (defvar muse-custom-tags nil
789 "Keep track of any new tags entered in `muse-insert-tag'.
790 If there are (X)HTML tags that you use frequently with that
791 function, you might want to set this manually.")
793 ;;;###autoload
794 (defun muse-insert-tag (tag)
795 "Insert a tag interactively with a blank line after it."
796 (interactive
797 (list
798 (completing-read
799 (concat "Tag: "
800 (when muse-tag-history
801 (concat "(default: " (car muse-tag-history) ") ")))
802 (progn
803 (require 'muse-publish)
804 (mapcar 'list (nconc (mapcar 'car muse-publish-markup-tags)
805 muse-custom-tags)))
806 nil nil nil 'muse-tag-history
807 (car muse-tag-history))))
808 (when (equal tag "")
809 (setq tag (car muse-tag-history)))
810 (unless (interactive-p)
811 (require 'muse-publish))
812 (let ((tag-entry (assoc tag muse-publish-markup-tags))
813 (options ""))
814 ;; Add to custom list if no entry exists
815 (unless tag-entry
816 (add-to-list 'muse-custom-tags tag))
817 ;; Get option
818 (when (nth 2 tag-entry)
819 (setq options (read-string "Option: ")))
820 (unless (equal options "")
821 (setq options (concat " " options)))
822 ;; Insert the tag, closing if necessary
823 (when tag (insert (concat "<" tag options ">")))
824 (when (nth 1 tag-entry)
825 (insert (concat "\n\n</" tag ">\n"))
826 (forward-line -2))))
828 (provide 'muse-mode)
830 ;;; muse-mode.el ends here