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