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