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