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