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