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