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