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