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