Add autoload cookie for muse-mode-choose-mode
[muse-el.git] / lisp / muse-mode.el
blob5228bd069a894c1c6f4e6a2bf1e6bf6a68699067
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 ;; Stefan Schlee (stefan_schlee AT yahoo DOT com) fixed a bug in
38 ;; muse-next-reference and muse-previous-reference involving links
39 ;; that begin at point 1.
41 ;;; Code:
43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
45 ;; Emacs Muse Major Mode
47 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
49 (require 'muse)
50 (require 'muse-regexps)
51 (require 'muse-project)
53 (autoload 'muse-use-font-lock "muse-colors")
54 (autoload 'muse-publish-this-file "muse-publish")
55 (autoload 'muse-publish-get-style "muse-publish")
56 (autoload 'muse-publish-output-file "muse-publish")
58 (require 'derived)
59 (eval-when-compile
60 (condition-case nil
61 (require 'pcomplete) ; load if available
62 (error nil)))
64 ;;; Options:
66 (defgroup muse-mode nil
67 "Options controlling the behavior of the Muse editing Mode."
68 :group 'muse)
70 (defcustom muse-mode-highlight-p t
71 "If non-nil, highlight the content of Muse buffers."
72 :type 'boolean
73 :require 'muse-colors
74 :group 'muse-mode)
76 (defcustom muse-mode-auto-p nil
77 "If non-nil, automagically determine when Muse mode should be activated."
78 :type 'boolean
79 :set (function
80 (lambda (sym value)
81 (if value
82 (add-hook 'find-file-hooks 'muse-mode-maybe)
83 (remove-hook 'find-file-hooks 'muse-mode-maybe))
84 (set sym value)))
85 :group 'muse-mode)
87 (defun muse-mode-maybe-after-init ()
88 (when muse-mode-auto-p
89 (add-hook 'find-file-hooks 'muse-mode-maybe)))
91 ;; If the user sets this value in their init file, make sure that
92 ;; it takes effect
93 (add-hook 'after-init-hook 'muse-mode-maybe-after-init)
95 (defcustom muse-mode-intangible-links nil
96 "If non-nil, use the intangible property on links.
97 This can cause problems with flyspell (and potentially fill-mode),
98 so only enable this if you don't use either of these."
99 :type 'boolean
100 :group 'muse-mode)
102 (defcustom muse-mode-hook nil
103 "A hook that is run when Muse mode is entered."
104 :type 'hook
105 :options '(flyspell-mode footnote-mode turn-on-auto-fill
106 highlight-changes-mode)
107 :group 'muse-mode)
109 (defcustom muse-grep-command
110 "find %D -type f ! -name '*~' | xargs -I {} echo \\\"{}\\\" | xargs egrep -n -e \"%W\""
111 "The command to use when grepping for backlinks and other
112 searches through the muse projects. The string %D is replaced by
113 the directories from muse-project-alist, space-separated. The
114 string %W is replaced with the name of the muse page or whatever
115 else you are searching for. This command has been modified to
116 handle spaces in filenames, which were giving egrep a problem.
118 Note: We highly recommend using glimpse to search large projects.
119 To use glimpse, install and edit a file called .glimpse_exclude
120 in your home directory. Put a list of glob patterns in that file
121 to exclude Emacs backup files, etc. Then, run the indexer using:
123 glimpseindex -o <list of Wiki directories>
125 Once that's completed, customize this variable to have the
126 following value:
128 glimpse -nyi \"%W\"
130 Your searches will go much, much faster, especially for very
131 large projects. Don't forget to add a user cronjob to update the
132 index at intervals."
133 :type 'string
134 :group 'muse-mode)
136 (defvar muse-insert-map
137 (let ((map (make-sparse-keymap)))
138 (define-key map "l" 'muse-insert-relative-link-to-file)
139 (define-key map "t" 'muse-insert-tag)
140 (define-key map "u" 'muse-insert-url)
142 map))
144 (defvar muse-mode-map
145 (let ((map (make-sparse-keymap)))
146 (define-key map [(control ?c) (control ?a)] 'muse-index)
147 (define-key map [(control ?c) (control ?c)] 'muse-follow-name-at-point)
148 (define-key map [(control ?c) (control ?e)] 'muse-edit-link-at-point)
149 (define-key map [(control ?c) (control ?l)] 'font-lock-mode)
150 (define-key map [(control ?c) (control ?t)]
151 'muse-project-publish-this-file)
152 (define-key map [(control ?c) (control ?T)] 'muse-publish-this-file)
153 (define-key map [(control ?c) (control ?v)] 'muse-browse-result)
155 (define-key map [(control ?c) ?=] 'muse-what-changed)
157 (define-key map [tab] 'muse-next-reference)
158 (define-key map [(control ?i)] 'muse-next-reference)
160 (if (featurep 'xemacs)
161 (progn
162 (define-key map [(button2)] 'muse-follow-name-at-mouse)
163 (define-key map [(shift button2)]
164 'muse-follow-name-at-mouse-other-window))
165 (define-key map [(shift control ?m)]
166 'muse-follow-name-at-point-other-window)
167 (define-key map [mouse-2] 'muse-follow-name-at-mouse)
168 (define-key map [(shift mouse-2)]
169 'muse-follow-name-at-mouse-other-window))
171 (define-key map [(shift tab)] 'muse-previous-reference)
172 (unless (featurep 'xemacs)
173 (define-key map [(shift iso-lefttab)] 'muse-previous-reference)
174 (define-key map [(shift control ?i)] 'muse-previous-reference))
176 (define-key map [(control ?c) (control ?f)] 'muse-project-find-file)
177 (define-key map [(control ?c) (control ?p)] 'muse-project-publish)
179 (define-key map [(control ?c) tab] 'muse-insert-thing)
181 ;; Searching functions
182 (define-key map [(control ?c) (control ?b)] 'muse-find-backlinks)
183 (define-key map [(control ?c) (control ?s)] 'muse-search)
185 ;; Enhanced list functions
186 (define-key map [(meta return)] 'muse-insert-list-item)
187 (define-key map [(control ?>)] 'muse-increase-list-item-indentation)
188 (define-key map [(control ?<)] 'muse-decrease-list-item-indentation)
190 (when (featurep 'pcomplete)
191 (define-key map [(meta tab)] 'pcomplete)
192 (define-key map [(meta control ?i)] 'pcomplete))
194 map)
195 "Keymap used by Emacs Muse mode.")
197 ;; Code:
199 ;;;###autoload
200 (define-derived-mode muse-mode text-mode "Muse"
201 "Muse is an Emacs mode for authoring and publishing documents.
202 \\{muse-mode-map}"
203 ;; Since we're not inheriting from normal-mode, we need to
204 ;; explicitly run file variables.
205 (condition-case err
206 (hack-local-variables)
207 (error (message "File local-variables error: %s"
208 (prin1-to-string err))))
209 ;; Avoid lock-up caused by use of the 'intangible' text property
210 ;; with flyspell.
211 (unless muse-mode-intangible-links
212 (set (make-local-variable 'inhibit-point-motion-hooks) t))
213 (setq muse-current-project (muse-project-of-file))
214 (muse-project-set-variables)
215 ;; Make fill not split up links
216 (when (boundp 'fill-nobreak-predicate)
217 (make-local-variable 'fill-nobreak-predicate)
218 ;; Work around annoying inconsistency in fill handling between
219 ;; Emacs 21 and 22.
220 (if (< emacs-major-version 22)
221 (setq fill-nobreak-predicate 'muse-mode-fill-nobreak-p)
222 (add-to-list 'fill-nobreak-predicate
223 'muse-mode-fill-nobreak-p)))
224 ;; Make fill work nicely with item lists
225 (set (make-local-variable 'adaptive-fill-regexp)
226 (concat "\\s-+\\(-\\|[0-9]+\\.\\)\\s-+\\|\\[[0-9]+\\]\\s-*"
227 "\\|.*\\s-*::\\s-+\\|\\s-*"))
228 (set (make-local-variable 'paragraph-start)
229 (concat paragraph-start
230 "\\|\\s-+\\(-\\|[0-9]+\\.\\)\\s-+\\|\\[[0-9]+\\]\\s-*"
231 "\\|.*\\s-*::\\s-+"))
232 ;; Comment syntax is `; comment'
233 (set (make-local-variable 'comment-start)
234 "; ")
235 (set (make-local-variable 'comment-start-skip)
236 "^;\\s-+")
237 ;; If we're using Emacs21, this makes flyspell work like it should
238 (when (boundp 'flyspell-generic-check-word-p)
239 (set (make-local-variable 'flyspell-generic-check-word-p)
240 'muse-mode-flyspell-p))
241 ;; If pcomplete is available, set it up
242 (when (featurep 'pcomplete)
243 (set (make-local-variable 'pcomplete-default-completion-function)
244 'muse-mode-completions)
245 (set (make-local-variable 'pcomplete-command-completion-function)
246 'muse-mode-completions)
247 (set (make-local-variable 'pcomplete-parse-arguments-function)
248 'muse-mode-current-word))
249 ;; Initialize any auto-generated variables
250 (run-hooks 'muse-update-values-hook)
251 (when muse-mode-highlight-p
252 (muse-use-font-lock)))
254 (put 'muse-mode
255 'flyspell-mode-predicate
256 'muse-mode-flyspell-p)
258 (defun muse-mode-fill-nobreak-p ()
259 "Return nil if we should allow a fill to occur at point.
260 Otherwise return non-nil.
262 This is used to keep long explicit links from being mangled by
263 fill mode."
264 (save-excursion
265 (save-match-data
266 (and (re-search-backward "\\[\\[\\|\\]\\]"
267 (line-beginning-position) t)
268 (string= (or (match-string 0) "")
269 "[[")))))
271 (defun muse-mode-flyspell-p ()
272 "Return non-nil if we should allow spell-checking to occur at point.
273 Otherwise return nil.
275 This is used to keep links from being improperly colorized by flyspell."
276 (and (not (get-text-property (if (bobp) (point) (1- (point)))
277 'muse-link))
278 (save-match-data
279 (null (muse-link-at-point)))))
281 ;;;###autoload
282 (defun muse-mode-choose-mode ()
283 "Turn the proper Emacs Muse related mode on for this file."
284 (let ((project (muse-project-of-file)))
285 (funcall (or (and project (muse-get-keyword :major-mode (cadr project) t))
286 'muse-mode))))
288 (defun muse-mode-maybe ()
289 "Maybe turn Emacs Muse mode on for this file."
290 (let ((project (muse-project-of-file)))
291 (and project
292 (funcall (or (muse-get-keyword :major-mode (cadr project) t)
293 'muse-mode)))))
295 ;;; Enhanced list editing
297 (defun muse-on-blank-line ()
298 "See if point is on a blank line"
299 (save-excursion
300 (beginning-of-line)
301 (looking-at (concat "[" muse-regexp-blank "]?[\n]+"))))
303 (defun muse-get-paragraph-start ()
304 "Return the start of the current paragraph. This function will
305 return nil if there are no prior paragraphs and the beginning of
306 the line if point is on a blank line."
307 (let ((para-start (concat "[\n]+[" muse-regexp-blank "]?[\n]+")))
308 ;; search back to start of paragraph
309 (save-excursion
310 (save-match-data
311 (if (not (muse-on-blank-line))
312 (re-search-backward para-start nil t)
313 (line-beginning-position))))))
315 (defun muse-insert-thing ()
316 "Prompt for something to insert into the current buffer."
317 (interactive)
318 (message "Insert:\nl link\nt Muse tag\nu URL")
319 (let (key cmd)
320 (let ((overriding-local-map muse-insert-map))
321 (setq key (read-key-sequence nil)))
322 (if (commandp (setq cmd (lookup-key muse-insert-map key)))
323 (progn (message "")
324 (call-interactively cmd))
325 (message "Not inserting anything"))))
327 ;;;###autoload
328 (defun muse-insert-list-item ()
329 "Insert a list item at the current point, taking into account
330 your current list type and indentation level."
331 (interactive)
332 (let ((newitem " - ")
333 (itemno nil)
334 (pstart (muse-get-paragraph-start))
335 (list-item (format muse-list-item-regexp
336 (concat "[" muse-regexp-blank "]*"))))
337 ;; search backwards for start of current item
338 (save-excursion
339 (when (re-search-backward list-item pstart t)
340 ;; save the matching item
341 (setq newitem (match-string 0))
342 ;; see what type it is
343 (if (string-match "::" (match-string 0))
344 ;; is a definition, replace the term
345 (setq newitem (concat " "
346 (read-string "Term: ")
347 " :: "))
348 ;; see if it's a numbered list
349 (when (string-match "[0-9]+" newitem)
350 ;; is numbered, so increment
351 (setq itemno (1+
352 (string-to-number
353 (match-string 0 newitem))))
354 (setq newitem (replace-match
355 (number-to-string itemno)
356 nil nil newitem))))))
357 ;; insert the new item
358 (insert (concat "\n" newitem))))
360 (defun muse-alter-list-item-indentation (operation)
361 "Alter the indentation of the current list item.
362 Valid values of OPERATION are 'increase and 'decrease."
363 (let ((pstart (muse-get-paragraph-start))
364 (list-item (format muse-list-item-regexp
365 (concat "[" muse-regexp-blank "]*")))
366 beg move-func indent)
367 ;; search backwards until start of paragraph to see if we are on a
368 ;; current item
369 (save-excursion
370 (if (or (progn (goto-char (muse-line-beginning-position))
371 ;; we are on an item
372 (looking-at list-item))
373 ;; not on item, so search backwards
374 (re-search-backward list-item pstart t))
375 (let ((beg (point)))
376 ;; we are on an item
377 (setq indent (buffer-substring (match-beginning 0)
378 (match-beginning 1)))
379 (muse-forward-list-item (muse-list-item-type (match-string 1))
380 (concat "[" muse-regexp-blank "]*")
382 (save-restriction
383 (narrow-to-region beg (point))
384 (goto-char (point-min))
385 (let ((halt nil))
386 (while (< (point) (point-max))
387 ;; increase or decrease the indentation
388 (unless halt
389 (cond ((eq operation 'increase)
390 (insert " "))
391 ((eq operation 'decrease)
392 (if (looking-at " ")
393 ;; we have enough space, so delete it
394 (delete-region (match-beginning 0)
395 (match-end 0))
396 (setq halt t)))))
397 (forward-line 1)))))
398 ;; we are not on an item, so warn
399 (message "You are not on a list item.")))))
401 ;;;###autoload
402 (defun muse-increase-list-item-indentation ()
403 "Increase the indentation of the current list item."
404 (interactive)
405 (muse-alter-list-item-indentation 'increase))
407 ;;;###autoload
408 (defun muse-decrease-list-item-indentation ()
409 "Decrease the indentation of the current list item."
410 (interactive)
411 (muse-alter-list-item-indentation 'decrease))
413 ;;; Support page name completion using pcomplete
415 (defun muse-mode-completions ()
416 "Return a list of possible completions names for this buffer."
417 (let ((project (muse-project-of-file)))
418 (if project
419 (while (pcomplete-here
420 (mapcar 'car (muse-project-file-alist project)))))))
422 (defun muse-mode-current-word ()
423 (let ((end (point)))
424 (save-excursion
425 (save-restriction
426 (skip-chars-backward (concat "^\\[\n" muse-regexp-blank))
427 (narrow-to-region (point) end))
428 (pcomplete-parse-buffer-arguments))))
430 ;;; Navigate/visit links or URLs. Use TAB, S-TAB and RET (or mouse-2).
432 (defun muse-link-at-point (&optional pos)
433 "Return link text if a URL or link is at point."
434 (let ((case-fold-search nil)
435 (inhibit-point-motion-hooks t)
436 (here (or pos (point))))
437 ;; if we are using muse-colors, we can just use link properties to
438 ;; determine whether we are on a link
439 (if (featurep 'muse-colors)
440 (when (get-text-property here 'muse-link)
441 (save-excursion
442 (when (and (not (bobp))
443 (get-text-property (1- here) 'muse-link))
444 (goto-char (or (previous-single-property-change here 'muse-link)
445 (point-min))))
446 (if (looking-at muse-explicit-link-regexp)
447 (progn
448 (goto-char (match-beginning 1))
449 (muse-handle-explicit-link))
450 (muse-handle-implicit-link))))
451 ;; use fallback method to find a link
452 (when (or (null pos)
453 (and (char-after pos)
454 (not (eq (char-syntax (char-after pos)) ?\ ))))
455 (save-excursion
456 (goto-char here)
457 ;; check for explicit link here or before point
458 (if (or (looking-at muse-explicit-link-regexp)
459 (and
460 (re-search-backward "\\[\\[\\|\\]\\]"
461 (muse-line-beginning-position)
463 (string= (or (match-string 0) "") "[[")
464 (looking-at muse-explicit-link-regexp)))
465 (progn
466 (goto-char (match-beginning 1))
467 (muse-handle-explicit-link))
468 (goto-char here)
469 ;; check for bare URL or other link type
470 (skip-chars-backward (concat "^'\"<>{}(\n" muse-regexp-blank))
471 (and (looking-at muse-implicit-link-regexp)
472 (muse-handle-implicit-link))))))))
474 (defun muse-make-link (link &optional desc)
475 "Return a link to LINK with DESC as the description."
476 (when (string-match muse-explicit-link-regexp link)
477 (unless desc (setq desc (muse-get-link-desc link)))
478 (setq link (muse-get-link link)))
479 (if (and desc
480 link
481 (not (string= desc ""))
482 (not (string= link desc)))
483 (concat "[[" (muse-link-escape link) "][" (muse-link-escape desc) "]]")
484 (concat "[[" (or (muse-link-escape link) "") "]]")))
486 ;;;###autoload
487 (defun muse-insert-relative-link-to-file ()
488 "Insert a relative link to a file, with optional description, at point."
489 ;; Perhaps the relative location should be configurable, so that the
490 ;; file search would start in the publishing directory and then
491 ;; insert the link relative to the publishing directory
492 (interactive)
493 (insert
494 (muse-make-link (file-relative-name (read-file-name "Link: "))
495 (read-string "Text: "))))
497 (defun muse-insert-url ()
498 "Insert a URL, with optional description, at point."
499 (interactive)
500 (insert
501 (muse-make-link (read-string "URL: ")
502 (read-string "Text: "))))
504 ;;;###autoload
505 (defun muse-edit-link-at-point ()
506 "Edit the current link.
507 Do not rename the page originally referred to."
508 (interactive)
509 (if (muse-link-at-point)
510 (let ((link (muse-link-unescape (muse-get-link)))
511 (desc (muse-link-unescape (muse-get-link-desc))))
512 (replace-match
513 (save-match-data
514 (muse-make-link
515 (read-string "Link: " link)
516 (read-string "Text: " desc)))
517 t t))
518 (error "There is no valid link at point")))
520 (defun muse-visit-link-default (link &optional other-window)
521 "Visit the URL or link named by LINK.
522 If ANCHOR is specified, search for it after opening LINK.
524 This is the default function to call when visiting links; it is
525 used by `muse-visit-link' if you have not specified :visit-link
526 in `muse-project-alist'."
527 (if (string-match muse-url-regexp link)
528 (muse-browse-url link)
529 (let (anchor
530 base-buffer)
531 (when (string-match "#" link)
532 (setq anchor (substring link (match-beginning 0))
533 link (if (= (match-beginning 0) 0)
534 ;; If there is an anchor but no link, default
535 ;; to the current page.
537 (substring link 0 (match-beginning 0)))))
538 (when link
539 (setq base-buffer (get-buffer link))
540 (if (and base-buffer (not (buffer-file-name base-buffer)))
541 ;; If file is temporary (no associated file), just switch to
542 ;; the buffer
543 (if other-window
544 (switch-to-buffer-other-window base-buffer)
545 (switch-to-buffer base-buffer))
546 (let ((project (muse-project-of-file)))
547 (if project
548 (muse-project-find-file link project
549 (and other-window
550 'find-file-other-window))
551 (if other-window
552 (find-file-other-window link)
553 (find-file link))))))
554 (when anchor
555 (let ((pos (point)))
556 (goto-char (point-min))
557 (unless (re-search-forward (concat "^\\W*" (regexp-quote anchor)
558 "\\b")
559 nil t)
560 (goto-char pos)))))))
562 (defun muse-visit-link (link &optional other-window)
563 "Visit the URL or link named by LINK."
564 (let ((visit-link-function
565 (muse-get-keyword :visit-link (cadr (muse-project-of-file)) t)))
566 (if visit-link-function
567 (funcall visit-link-function link other-window)
568 (muse-visit-link-default link other-window))))
570 ;;;###autoload
571 (defun muse-browse-result (style &optional other-window)
572 "Visit the current page's published result."
573 (interactive
574 (list (muse-project-get-applicable-style buffer-file-name
575 (cddr muse-current-project))
576 current-prefix-arg))
577 (setq style (muse-style style))
578 (let ((result-path
579 (muse-publish-output-file buffer-file-name
580 (muse-style-element :path style) style)))
581 (if (not (file-readable-p result-path))
582 (error "Cannot open output file '%s'" result-path)
583 (if other-window
584 (find-file-other-window result-path)
585 (let ((func (muse-style-element :browser style t)))
586 (if func
587 (funcall func result-path)
588 (message "The %s publishing style does not support browsing."
589 style)))))))
591 ;;;###autoload
592 (defun muse-follow-name-at-point (&optional other-window)
593 "Visit the link at point."
594 (interactive "P")
595 (let ((link (muse-link-at-point)))
596 (if link
597 (muse-visit-link link other-window)
598 (error "There is no valid link at point"))))
600 ;;;###autoload
601 (defun muse-follow-name-at-point-other-window ()
602 "Visit the link at point in other window."
603 (interactive)
604 (muse-follow-name-at-point t))
606 (defun muse-follow-name-at-mouse (event &optional other-window)
607 "Visit the link at point, or yank text if none is found."
608 (interactive "eN")
609 (unless
610 (save-excursion
611 (cond ((fboundp 'event-window) ; XEmacs
612 (set-buffer (window-buffer (event-window event)))
613 (and (funcall (symbol-function 'event-point) event)
614 (goto-char (funcall (symbol-function 'event-point)
615 event))))
616 ((fboundp 'posn-window) ; Emacs
617 (set-buffer (window-buffer (posn-window (event-start event))))
618 (goto-char (posn-point (event-start event)))))
619 (let ((link (muse-link-at-point)))
620 (when link
621 (muse-visit-link link other-window)
622 t)))
623 ;; Fall back to normal binding for this event
624 (call-interactively
625 (lookup-key (current-global-map) (this-command-keys)))))
627 (defun muse-follow-name-at-mouse-other-window (event)
628 "Visit the link at point"
629 (interactive "e")
630 ;; throw away the old window position, since other-window will
631 ;; change it anyway
632 (select-window (car (cadr event)))
633 (muse-follow-name-at-mouse event t))
635 ;;;###autoload
636 (defun muse-next-reference ()
637 "Move forward to next Muse link or URL, cycling if necessary."
638 (interactive)
639 (let ((pos))
640 (save-excursion
641 (when (get-text-property (point) 'muse-link)
642 (goto-char (or (next-single-property-change (point) 'muse-link)
643 (point-max))))
645 (setq pos (next-single-property-change (point) 'muse-link))
647 (when (not pos)
648 (if (get-text-property (point-min) 'muse-link)
649 (setq pos (point-min))
650 (setq pos (next-single-property-change (point-min) 'muse-link)))))
652 (when pos
653 (goto-char pos))))
655 ;;;###autoload
656 (defun muse-previous-reference ()
657 "Move backward to the next Muse link or URL, cycling if necessary.
658 In case of Emacs x <= 21 and ignoring of intangible properties (see
659 `muse-mode-intangible-links').
661 This function is not entirely accurate, but it's close enough."
662 (interactive)
663 (let ((pos))
664 (save-excursion
666 ;; Hack: The user perceives the two cases of point ("|")
667 ;; position (1) "|[[" and (2) "[[|" or "][|" as "point is at
668 ;; start of link". But in the sense of the function
669 ;; "previous-single-property-change" these two cases are
670 ;; different. The following code aligns these two cases. Emacs
671 ;; 21: If the intangible property is ignored case (2) is more
672 ;; complicate and this hack only solves the problem partially.
674 (when (and (get-text-property (point) 'muse-link)
675 (muse-looking-back "\\[\\|\\]"))
676 (goto-char (or (previous-single-property-change (point) 'muse-link)
677 (point-min))))
679 (when (eq (point) (point-min))
680 (goto-char (point-max)))
682 (setq pos (previous-single-property-change (point) 'muse-link))
684 (when (not pos)
685 (if (get-text-property (point-min) 'muse-link)
686 (setq pos (point-min))
687 (setq pos (previous-single-property-change (point-max)
688 'muse-link)))))
690 (when pos
691 (if (get-text-property pos 'muse-link)
692 (goto-char pos)
693 (goto-char (or (previous-single-property-change pos 'muse-link)
694 (point-min)))))))
696 ;;;###autoload
697 (defun muse-what-changed ()
698 "Show the unsaved changes that have been made to the current file."
699 (interactive)
700 (diff-backup buffer-file-name))
703 ;;; Find text in project pages, or pages referring to the current page
705 (defvar muse-search-history nil)
707 (defun muse-grep (string &optional grep-command-no-shadow)
708 "Grep for STRING in the project directories.
709 GREP-COMMAND if passed will supplant `muse-grep-command'."
710 ;; careful - grep-command leaks into compile, so we call it
711 ;; -no-shadow instead
712 (require 'compile)
713 (let* ((str (or grep-command-no-shadow muse-grep-command))
714 (muse-directories (mapcar
715 (lambda (thing)
716 (car (cadr thing)))
717 muse-project-alist))
718 (dirs (mapconcat (lambda (dir)
719 (shell-quote-argument
720 (expand-file-name dir)))
721 muse-directories " ")))
722 (while (string-match "%W" str)
723 (setq str (replace-match string t t str)))
724 (while (string-match "%D" str)
725 (setq str (replace-match dirs t t str)))
726 (if (fboundp 'compilation-start)
727 (compilation-start str nil (lambda (&rest args) "*search*")
728 grep-regexp-alist)
729 (and (fboundp 'compile-internal)
730 (compile-internal str "No more search hits" "search"
731 nil grep-regexp-alist)))))
733 ;;;###autoload
734 (defun muse-search-with-command (text)
735 "Search for the given TEXT string in the project directories
736 using the specified command."
737 (interactive
738 (list (let ((str (concat muse-grep-command)) pos)
739 (when (string-match "%W" str)
740 (setq pos (match-beginning 0))
741 (unless (featurep 'xemacs)
742 (setq pos (1+ pos)))
743 (setq str (replace-match "" t t str)))
744 (read-from-minibuffer "Search command: "
745 (cons str pos) nil nil
746 'muse-search-history))))
747 (muse-grep nil text))
749 ;;;###autoload
750 (defun muse-search ()
751 "Search for the given TEXT using the default grep command."
752 (interactive)
753 (muse-grep (read-string "Search: ")))
755 ;;;###autoload
756 (defun muse-find-backlinks ()
757 "Grep for the current pagename in all the project directories."
758 (interactive)
759 (muse-grep (muse-page-name)))
762 ;;; Generate an index of all known Muse pages
764 (defun muse-generate-index (&optional as-list exclude-private)
765 "Generate an index of all Muse pages."
766 (let ((index (muse-index-as-string as-list exclude-private)))
767 (with-current-buffer (get-buffer-create "*Muse Index*")
768 (erase-buffer)
769 (insert index)
770 (current-buffer))))
772 ;;;###autoload
773 (defun muse-index ()
774 "Display an index of all known Muse pages."
775 (interactive)
776 (message "Generating Muse index...")
777 (let ((project (muse-project)))
778 (with-current-buffer (muse-generate-index)
779 (goto-char (point-min))
780 (muse-mode)
781 (setq muse-current-project project)
782 (pop-to-buffer (current-buffer))))
783 (message "Generating Muse index...done"))
785 (defun muse-index-as-string (&optional as-list exclude-private exclude-current)
786 "Generate an index of all Muse pages.
787 If AS-LIST is non-nil, insert a dash and spaces before each item.
788 If EXCLUDE-PRIVATE is non-nil, exclude files that have private permissions.
789 If EXCLUDE-CURRENT is non-nil, exclude the current file from the output."
790 (let ((files (sort (copy-alist (muse-project-file-alist))
791 (function
792 (lambda (l r)
793 (string-lessp (car l) (car r)))))))
794 (when (and exclude-current (muse-page-name))
795 (setq files (delete (assoc (muse-page-name) files) files)))
796 (with-temp-buffer
797 (while files
798 (unless (and exclude-private
799 (muse-project-private-p (cdar files)))
800 (insert (if as-list " - " "") "[[" (caar files) "]]\n"))
801 (setq files (cdr files)))
802 (buffer-string))))
804 ;;; Insert tags interactively on C-c TAB t
806 (defvar muse-tag-history nil
807 "List of recently-entered tags; used by `muse-insert-tag'.
808 If you want a tag to start as the default, you may manually set
809 this variable to a list.")
811 (defvar muse-custom-tags nil
812 "Keep track of any new tags entered in `muse-insert-tag'.
813 If there are (X)HTML tags that you use frequently with that
814 function, you might want to set this manually.")
816 ;;;###autoload
817 (defun muse-insert-tag (tag)
818 "Insert a tag interactively with a blank line after it."
819 (interactive
820 (list
821 (completing-read
822 (concat "Tag: "
823 (when muse-tag-history
824 (concat "(default: " (car muse-tag-history) ") ")))
825 (progn
826 (require 'muse-publish)
827 (mapcar 'list (nconc (mapcar 'car muse-publish-markup-tags)
828 muse-custom-tags)))
829 nil nil nil 'muse-tag-history
830 (car muse-tag-history))))
831 (when (equal tag "")
832 (setq tag (car muse-tag-history)))
833 (unless (interactive-p)
834 (require 'muse-publish))
835 (let ((tag-entry (assoc tag muse-publish-markup-tags))
836 (options ""))
837 ;; Add to custom list if no entry exists
838 (unless tag-entry
839 (add-to-list 'muse-custom-tags tag))
840 ;; Get option
841 (when (nth 2 tag-entry)
842 (setq options (read-string "Option: ")))
843 (unless (equal options "")
844 (setq options (concat " " options)))
845 ;; Insert the tag, closing if necessary
846 (when tag (insert (concat "<" tag options ">")))
847 (when (nth 1 tag-entry)
848 (insert (concat "\n\n</" tag ">\n"))
849 (forward-line -2))))
851 (provide 'muse-mode)
853 ;;; muse-mode.el ends here