Merge ../master
[muse-el.git] / lisp / muse-mode.el
blobac6f7fac70aaa16293634bc7a2f461f162f133fe
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 3, 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 ;;; Muse mode
148 (defvar muse-mode-map
149 (let ((map (make-sparse-keymap)))
150 (define-key map [(control ?c) (control ?a)] 'muse-index)
151 (define-key map [(control ?c) (control ?e)] 'muse-edit-link-at-point)
152 (define-key map [(control ?c) (control ?l)] 'font-lock-mode)
153 (define-key map [(control ?c) (control ?t)]
154 'muse-project-publish-this-file)
155 (define-key map [(control ?c) (control ?T)] 'muse-publish-this-file)
156 (define-key map [(control ?c) (meta control ?t)] 'muse-publish-this-file)
157 (define-key map [(control ?c) (control ?v)] 'muse-browse-result)
159 (define-key map [(control ?c) ?=] 'muse-what-changed)
161 (define-key map [tab] 'muse-next-reference)
162 (define-key map [(control ?i)] 'muse-next-reference)
164 (if (featurep 'xemacs)
165 (progn
166 (define-key map [(button2)] 'muse-follow-name-at-mouse)
167 (define-key map [(shift button2)]
168 'muse-follow-name-at-mouse-other-window))
169 (define-key map [(shift control ?m)]
170 'muse-follow-name-at-point-other-window)
171 (define-key map [mouse-2] 'muse-follow-name-at-mouse)
172 (define-key map [(shift mouse-2)]
173 'muse-follow-name-at-mouse-other-window))
175 (define-key map [(shift tab)] 'muse-previous-reference)
176 (unless (featurep 'xemacs)
177 (define-key map [(shift iso-lefttab)] 'muse-previous-reference)
178 (define-key map [(shift control ?i)] 'muse-previous-reference))
180 (define-key map [(control ?c) (control ?f)] 'muse-project-find-file)
181 (define-key map [(control ?c) (control ?p)] 'muse-project-publish)
183 (define-key map [(control ?c) (control ?i)] 'muse-insert-thing)
184 (define-key map [(control ?c) tab] 'muse-insert-thing)
186 ;; Searching functions
187 (define-key map [(control ?c) (control ?b)] 'muse-find-backlinks)
188 (define-key map [(control ?c) (control ?s)] 'muse-search)
190 ;; Enhanced list functions
191 (define-key map [(meta return)] 'muse-insert-list-item)
192 (define-key map [(control ?>)] 'muse-increase-list-item-indentation)
193 (define-key map [(control ?<)] 'muse-decrease-list-item-indentation)
195 (when (featurep 'pcomplete)
196 (define-key map [(meta tab)] 'pcomplete)
197 (define-key map [(meta control ?i)] 'pcomplete))
199 map)
200 "Keymap used by Emacs Muse mode.")
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 (let ((regexp (concat "\\s-+\\(-\\|[0-9]+\\.\\)\\s-+"
229 "\\|\\[[0-9]+\\]\\s-*"
230 "\\|.*\\s-*::\\s-+")))
231 (set (make-local-variable 'adaptive-fill-regexp)
232 (concat regexp "\\|\\s-*"))
233 (set (make-local-variable 'paragraph-start)
234 (concat paragraph-start "\\|" regexp)))
235 ;; Comment syntax is `; comment'
236 (set (make-local-variable 'comment-start)
237 "; ")
238 (set (make-local-variable 'comment-start-skip)
239 "^;\\s-+")
240 (set (make-local-variable 'indent-line-function)
241 #'ignore)
242 ;; If we're using Emacs21, this makes flyspell work like it should
243 (when (boundp 'flyspell-generic-check-word-p)
244 (set (make-local-variable 'flyspell-generic-check-word-p)
245 'muse-mode-flyspell-p))
246 ;; If pcomplete is available, set it up
247 (when (featurep 'pcomplete)
248 (set (make-local-variable 'pcomplete-default-completion-function)
249 'muse-mode-completions)
250 (set (make-local-variable 'pcomplete-command-completion-function)
251 'muse-mode-completions)
252 (set (make-local-variable 'pcomplete-parse-arguments-function)
253 'muse-mode-current-word))
254 ;; Initialize any auto-generated variables
255 (run-hooks 'muse-update-values-hook)
256 (when muse-mode-highlight-p
257 (muse-use-font-lock)))
259 (put 'muse-mode
260 'flyspell-mode-predicate
261 'muse-mode-flyspell-p)
263 (defun muse-mode-fill-nobreak-p ()
264 "Return nil if we should allow a fill to occur at point.
265 Otherwise return non-nil.
267 This is used to keep long explicit links from being mangled by
268 fill mode."
269 (save-excursion
270 (save-match-data
271 (and (re-search-backward "\\[\\[\\|\\]\\]"
272 (line-beginning-position) t)
273 (string= (or (match-string 0) "")
274 "[[")))))
276 (defun muse-mode-flyspell-p ()
277 "Return non-nil if we should allow spell-checking to occur at point.
278 Otherwise return nil.
280 This is used to keep links from being improperly colorized by flyspell."
281 (and (not (get-text-property (if (bobp) (point) (1- (point)))
282 'muse-link))
283 (save-match-data
284 (null (muse-link-at-point)))))
286 ;;;###autoload
287 (defun muse-mode-choose-mode ()
288 "Turn the proper Emacs Muse related mode on for this file."
289 (let ((project (muse-project-of-file)))
290 (funcall (or (and project (muse-get-keyword :major-mode (cadr project) t))
291 'muse-mode))))
293 (defun muse-mode-maybe ()
294 "Maybe turn Emacs Muse mode on for this file."
295 (let ((project (muse-project-of-file)))
296 (and project
297 (funcall (or (muse-get-keyword :major-mode (cadr project) t)
298 'muse-mode)))))
300 ;;; Enhanced list editing
302 (defun muse-on-blank-line ()
303 "See if point is on a blank line"
304 (save-excursion
305 (beginning-of-line)
306 (looking-at (concat "[" muse-regexp-blank "]*$"))))
308 (defun muse-get-paragraph-start ()
309 "Return the start of the current paragraph. This function will
310 return nil if there are no prior paragraphs and the beginning of
311 the line if point is on a blank line."
312 (let ((para-start (concat "^[" muse-regexp-blank "]*$")))
313 ;; search back to start of paragraph
314 (save-excursion
315 (save-match-data
316 (if (not (muse-on-blank-line))
317 (re-search-backward para-start nil t)
318 (line-beginning-position))))))
320 (defun muse-insert-thing ()
321 "Prompt for something to insert into the current buffer."
322 (interactive)
323 (message "Insert:\nl link\nt Muse tag\nu URL")
324 (let (key cmd)
325 (let ((overriding-local-map muse-insert-map))
326 (setq key (read-key-sequence nil)))
327 (if (commandp (setq cmd (lookup-key muse-insert-map key)))
328 (progn (message "")
329 (call-interactively cmd))
330 (message "Not inserting anything"))))
332 ;;;###autoload
333 (defun muse-insert-list-item ()
334 "Insert a list item at the current point, taking into account
335 your current list type and indentation level."
336 (interactive)
337 (let ((newitem " - ")
338 (itemno nil)
339 (pstart (muse-get-paragraph-start))
340 (list-item (format muse-list-item-regexp
341 (concat "[" muse-regexp-blank "]*"))))
342 ;; search backwards for start of current item
343 (save-excursion
344 (when (re-search-backward list-item pstart t)
345 ;; save the matching item
346 (setq newitem (match-string 0))
347 ;; see what type it is
348 (if (string-match "::" (match-string 0))
349 ;; is a definition, replace the term
350 (setq newitem (concat " "
351 (read-string "Term: ")
352 " :: "))
353 ;; see if it's a numbered list
354 (when (string-match "[0-9]+" newitem)
355 ;; is numbered, so increment
356 (setq itemno (1+
357 (string-to-number
358 (match-string 0 newitem))))
359 (setq newitem (replace-match
360 (number-to-string itemno)
361 nil nil newitem))))))
362 ;; insert the new item
363 (insert (concat "\n" newitem))))
365 (defun muse-alter-list-item-indentation (operation)
366 "Alter the indentation of the current list item.
367 Valid values of OPERATION are 'increase and 'decrease."
368 (let ((pstart (muse-get-paragraph-start))
369 (list-item (format muse-list-item-regexp
370 (concat "[" muse-regexp-blank "]*")))
371 beg move-func indent)
372 ;; search backwards until start of paragraph to see if we are on a
373 ;; current item
374 (save-excursion
375 (if (or (progn (goto-char (muse-line-beginning-position))
376 ;; we are on an item
377 (looking-at list-item))
378 ;; not on item, so search backwards
379 (re-search-backward list-item pstart t))
380 (let ((beg (point)))
381 ;; we are on an item
382 (setq indent (buffer-substring (match-beginning 0)
383 (match-beginning 1)))
384 (muse-forward-list-item (muse-list-item-type (match-string 1))
385 (concat "[" muse-regexp-blank "]*")
387 (save-restriction
388 (narrow-to-region beg (point))
389 (goto-char (point-min))
390 (let ((halt nil))
391 (while (< (point) (point-max))
392 ;; increase or decrease the indentation
393 (unless halt
394 (cond ((eq operation 'increase)
395 (insert " "))
396 ((eq operation 'decrease)
397 (if (looking-at " ")
398 ;; we have enough space, so delete it
399 (delete-region (match-beginning 0)
400 (match-end 0))
401 (setq halt t)))))
402 (forward-line 1)))))
403 ;; we are not on an item, so warn
404 (message "You are not on a list item.")))))
406 ;;;###autoload
407 (defun muse-increase-list-item-indentation ()
408 "Increase the indentation of the current list item."
409 (interactive)
410 (muse-alter-list-item-indentation 'increase))
412 ;;;###autoload
413 (defun muse-decrease-list-item-indentation ()
414 "Decrease the indentation of the current list item."
415 (interactive)
416 (muse-alter-list-item-indentation 'decrease))
418 ;;; Support page name completion using pcomplete
420 (defun muse-mode-completions ()
421 "Return a list of possible completions names for this buffer."
422 (let ((project (muse-project-of-file)))
423 (if project
424 (while (pcomplete-here
425 (mapcar 'car (muse-project-file-alist project)))))))
427 (defun muse-mode-current-word ()
428 (let ((end (point)))
429 (save-excursion
430 (save-restriction
431 (skip-chars-backward (concat "^\\[\n" muse-regexp-blank))
432 (narrow-to-region (point) end))
433 (pcomplete-parse-buffer-arguments))))
435 ;;; Navigate/visit links or URLs. Use TAB, S-TAB and RET (or mouse-2).
437 (defun muse-link-at-point (&optional pos)
438 "Return link text if a URL or link is at point."
439 (let ((case-fold-search nil)
440 (inhibit-point-motion-hooks t)
441 (here (or pos (point))))
442 ;; if we are using muse-colors, we can just use link properties to
443 ;; determine whether we are on a link
444 (if (featurep 'muse-colors)
445 (when (get-text-property here 'muse-link)
446 (save-excursion
447 (when (and (not (bobp))
448 (get-text-property (1- here) 'muse-link))
449 (goto-char (or (previous-single-property-change here 'muse-link)
450 (point-min))))
451 (if (looking-at muse-explicit-link-regexp)
452 (progn
453 (goto-char (match-beginning 1))
454 (muse-handle-explicit-link))
455 (muse-handle-implicit-link))))
456 ;; use fallback method to find a link
457 (when (or (null pos)
458 (and (char-after pos)
459 (not (eq (char-syntax (char-after pos)) ?\ ))))
460 (save-excursion
461 (goto-char here)
462 ;; check for explicit link here or before point
463 (if (or (looking-at muse-explicit-link-regexp)
464 (and
465 (re-search-backward "\\[\\[\\|\\]\\]"
466 (muse-line-beginning-position)
468 (string= (or (match-string 0) "") "[[")
469 (looking-at muse-explicit-link-regexp)))
470 (progn
471 (goto-char (match-beginning 1))
472 (muse-handle-explicit-link))
473 (goto-char here)
474 ;; check for bare URL or other link type
475 (skip-chars-backward (concat "^'\"<>{}(\n" muse-regexp-blank))
476 (and (looking-at muse-implicit-link-regexp)
477 (muse-handle-implicit-link))))))))
479 (defun muse-make-link (link &optional desc)
480 "Return a link to LINK with DESC as the description."
481 (when (string-match muse-explicit-link-regexp link)
482 (unless desc (setq desc (muse-get-link-desc link)))
483 (setq link (muse-get-link link)))
484 (if (and desc
485 link
486 (not (string= desc ""))
487 (not (string= link desc)))
488 (concat "[[" (muse-link-escape link) "][" (muse-link-escape desc) "]]")
489 (concat "[[" (or (muse-link-escape link) "") "]]")))
491 ;;;###autoload
492 (defun muse-insert-relative-link-to-file ()
493 "Insert a relative link to a file, with optional description, at point."
494 ;; Perhaps the relative location should be configurable, so that the
495 ;; file search would start in the publishing directory and then
496 ;; insert the link relative to the publishing directory
497 (interactive)
498 (insert
499 (muse-make-link (file-relative-name (read-file-name "Link: "))
500 (read-string "Text: "))))
502 (defun muse-insert-url ()
503 "Insert a URL, with optional description, at point."
504 (interactive)
505 (insert
506 (muse-make-link (read-string "URL: ")
507 (read-string "Text: "))))
509 ;;;###autoload
510 (defun muse-edit-link-at-point ()
511 "Edit the current link.
512 Do not rename the page originally referred to."
513 (interactive)
514 (if (muse-link-at-point)
515 (let ((link (muse-link-unescape (muse-get-link)))
516 (desc (muse-link-unescape (muse-get-link-desc))))
517 (replace-match
518 (save-match-data
519 (muse-make-link
520 (read-string "Link: " link)
521 (read-string "Text: " desc)))
522 t t))
523 (error "There is no valid link at point")))
525 (defun muse-visit-link-default (link &optional other-window)
526 "Visit the URL or link named by LINK.
527 If ANCHOR is specified, search for it after opening LINK.
529 This is the default function to call when visiting links; it is
530 used by `muse-visit-link' if you have not specified :visit-link
531 in `muse-project-alist'."
532 (if (string-match muse-url-regexp link)
533 (muse-browse-url link)
534 (let (anchor
535 base-buffer)
536 (when (string-match "#" link)
537 (setq anchor (substring link (match-beginning 0))
538 link (if (= (match-beginning 0) 0)
539 ;; If there is an anchor but no link, default
540 ;; to the current page.
542 (substring link 0 (match-beginning 0)))))
543 (when link
544 (setq base-buffer (get-buffer link))
545 (if (and base-buffer (not (buffer-file-name base-buffer)))
546 ;; If file is temporary (no associated file), just switch to
547 ;; the buffer
548 (if other-window
549 (switch-to-buffer-other-window base-buffer)
550 (switch-to-buffer base-buffer))
551 (let ((project (muse-project-of-file)))
552 (if project
553 (muse-project-find-file link project
554 (and other-window
555 'find-file-other-window))
556 (if other-window
557 (find-file-other-window link)
558 (find-file link))))))
559 (when anchor
560 (let ((pos (point)))
561 (goto-char (point-min))
562 (unless (re-search-forward (concat "^\\W*" (regexp-quote anchor)
563 "\\b")
564 nil t)
565 (goto-char pos)))))))
567 (defun muse-visit-link (link &optional other-window)
568 "Visit the URL or link named by LINK."
569 (let ((visit-link-function
570 (muse-get-keyword :visit-link (cadr (muse-project-of-file)) t)))
571 (if visit-link-function
572 (funcall visit-link-function link other-window)
573 (muse-visit-link-default link other-window))))
575 ;;;###autoload
576 (defun muse-browse-result (style &optional other-window)
577 "Visit the current page's published result."
578 (interactive
579 (list (muse-project-get-applicable-style buffer-file-name
580 (cddr muse-current-project))
581 current-prefix-arg))
582 (setq style (muse-style style))
583 (let* ((output-dir (muse-style-element :path style))
584 (result-path (muse-publish-output-file buffer-file-name output-dir
585 style))
586 (muse-current-output-style (list :base (car style)
587 :path output-dir)))
588 (if (not (file-readable-p result-path))
589 (error "Cannot open output file '%s'" result-path)
590 (if other-window
591 (find-file-other-window result-path)
592 (let ((func (muse-style-element :browser style t)))
593 (if func
594 (funcall func result-path)
595 (message "The %s publishing style does not support browsing."
596 style)))))))
598 ;;;###autoload
599 (defun muse-follow-name-at-point (&optional other-window)
600 "Visit the link at point."
601 (interactive "P")
602 (let ((link (muse-link-at-point)))
603 (if link
604 (muse-visit-link link other-window)
605 (error "There is no valid link at point"))))
607 ;;;###autoload
608 (defun muse-follow-name-at-point-other-window ()
609 "Visit the link at point in other window."
610 (interactive)
611 (muse-follow-name-at-point t))
613 (defun muse-follow-name-at-mouse (event &optional other-window)
614 "Visit the link at point, or yank text if none is found."
615 (interactive "eN")
616 (unless
617 (save-excursion
618 (cond ((fboundp 'event-window) ; XEmacs
619 (set-buffer (window-buffer (event-window event)))
620 (and (funcall (symbol-function 'event-point) event)
621 (goto-char (funcall (symbol-function 'event-point)
622 event))))
623 ((fboundp 'posn-window) ; Emacs
624 (set-buffer (window-buffer (posn-window (event-start event))))
625 (goto-char (posn-point (event-start event)))))
626 (let ((link (muse-link-at-point)))
627 (when link
628 (muse-visit-link link other-window)
629 t)))
630 ;; Fall back to normal binding for this event
631 (call-interactively
632 (lookup-key (current-global-map) (this-command-keys)))))
634 (defun muse-follow-name-at-mouse-other-window (event)
635 "Visit the link at point"
636 (interactive "e")
637 ;; throw away the old window position, since other-window will
638 ;; change it anyway
639 (select-window (car (cadr event)))
640 (muse-follow-name-at-mouse event t))
642 ;;;###autoload
643 (defun muse-next-reference ()
644 "Move forward to next Muse link or URL, cycling if necessary."
645 (interactive)
646 (let ((pos))
647 (save-excursion
648 (when (get-text-property (point) 'muse-link)
649 (goto-char (or (next-single-property-change (point) 'muse-link)
650 (point-max))))
652 (setq pos (next-single-property-change (point) 'muse-link))
654 (when (not pos)
655 (if (get-text-property (point-min) 'muse-link)
656 (setq pos (point-min))
657 (setq pos (next-single-property-change (point-min) 'muse-link)))))
659 (when pos
660 (goto-char pos))))
662 ;;;###autoload
663 (defun muse-previous-reference ()
664 "Move backward to the next Muse link or URL, cycling if necessary.
665 In case of Emacs x <= 21 and ignoring of intangible properties (see
666 `muse-mode-intangible-links').
668 This function is not entirely accurate, but it's close enough."
669 (interactive)
670 (let ((pos))
671 (save-excursion
673 ;; Hack: The user perceives the two cases of point ("|")
674 ;; position (1) "|[[" and (2) "[[|" or "][|" as "point is at
675 ;; start of link". But in the sense of the function
676 ;; "previous-single-property-change" these two cases are
677 ;; different. The following code aligns these two cases. Emacs
678 ;; 21: If the intangible property is ignored case (2) is more
679 ;; complicate and this hack only solves the problem partially.
681 (when (and (get-text-property (point) 'muse-link)
682 (muse-looking-back "\\[\\|\\]"))
683 (goto-char (or (previous-single-property-change (point) 'muse-link)
684 (point-min))))
686 (when (eq (point) (point-min))
687 (goto-char (point-max)))
689 (setq pos (previous-single-property-change (point) 'muse-link))
691 (when (not pos)
692 (if (get-text-property (point-min) 'muse-link)
693 (setq pos (point-min))
694 (setq pos (previous-single-property-change (point-max)
695 'muse-link)))))
697 (when pos
698 (if (get-text-property pos 'muse-link)
699 (goto-char pos)
700 (goto-char (or (previous-single-property-change pos 'muse-link)
701 (point-min)))))))
703 ;;;###autoload
704 (defun muse-what-changed ()
705 "Show the unsaved changes that have been made to the current file."
706 (interactive)
707 (diff-backup buffer-file-name))
710 ;;; Find text in project pages, or pages referring to the current page
712 (defvar muse-search-history nil)
714 (defun muse-grep (string &optional grep-command-no-shadow)
715 "Grep for STRING in the project directories.
716 GREP-COMMAND if passed will supplant `muse-grep-command'."
717 ;; careful - grep-command leaks into compile, so we call it
718 ;; -no-shadow instead
719 (require 'compile)
720 (let* ((str (or grep-command-no-shadow muse-grep-command))
721 (muse-directories (mapcar
722 (lambda (thing)
723 (car (cadr thing)))
724 muse-project-alist))
725 (dirs (mapconcat (lambda (dir)
726 (shell-quote-argument
727 (expand-file-name dir)))
728 muse-directories " ")))
729 (if (string= dirs "")
730 (muse-display-warning
731 "No directories were found in the current project; aborting search")
732 (while (string-match "%W" str)
733 (setq str (replace-match string t t str)))
734 (while (string-match "%D" str)
735 (setq str (replace-match dirs t t str)))
736 (if (fboundp 'compilation-start)
737 (compilation-start str nil (lambda (&rest args) "*search*")
738 grep-regexp-alist)
739 (and (fboundp 'compile-internal)
740 (compile-internal str "No more search hits" "search"
741 nil grep-regexp-alist))))))
743 ;;;###autoload
744 (defun muse-search-with-command (text)
745 "Search for the given TEXT string in the project directories
746 using the specified command."
747 (interactive
748 (list (let ((str (concat muse-grep-command)) pos)
749 (when (string-match "%W" str)
750 (setq pos (match-beginning 0))
751 (unless (featurep 'xemacs)
752 (setq pos (1+ pos)))
753 (setq str (replace-match "" t t str)))
754 (read-from-minibuffer "Search command: "
755 (cons str pos) nil nil
756 'muse-search-history))))
757 (muse-grep nil text))
759 ;;;###autoload
760 (defun muse-search ()
761 "Search for the given TEXT using the default grep command."
762 (interactive)
763 (muse-grep (read-string "Search: ")))
765 ;;;###autoload
766 (defun muse-find-backlinks ()
767 "Grep for the current pagename in all the project directories."
768 (interactive)
769 (muse-grep (muse-page-name)))
772 ;;; Generate an index of all known Muse pages
774 (defun muse-generate-index (&optional as-list exclude-private)
775 "Generate an index of all Muse pages."
776 (let ((index (muse-index-as-string as-list exclude-private)))
777 (with-current-buffer (get-buffer-create "*Muse Index*")
778 (erase-buffer)
779 (insert index)
780 (current-buffer))))
782 ;;;###autoload
783 (defun muse-index ()
784 "Display an index of all known Muse pages."
785 (interactive)
786 (message "Generating Muse index...")
787 (let ((project (muse-project)))
788 (with-current-buffer (muse-generate-index)
789 (goto-char (point-min))
790 (muse-mode)
791 (setq muse-current-project project)
792 (pop-to-buffer (current-buffer))))
793 (message "Generating Muse index...done"))
795 (defun muse-index-as-string (&optional as-list exclude-private exclude-current)
796 "Generate an index of all Muse pages.
797 If AS-LIST is non-nil, insert a dash and spaces before each item.
798 If EXCLUDE-PRIVATE is non-nil, exclude files that have private permissions.
799 If EXCLUDE-CURRENT is non-nil, exclude the current file from the output."
800 (let ((files (sort (copy-alist (muse-project-file-alist))
801 (function
802 (lambda (l r)
803 (string-lessp (car l) (car r)))))))
804 (when (and exclude-current (muse-page-name))
805 (setq files (delete (assoc (muse-page-name) files) files)))
806 (with-temp-buffer
807 (while files
808 (unless (and exclude-private
809 (muse-project-private-p (cdar files)))
810 (insert (if as-list " - " "") "[[" (caar files) "]]\n"))
811 (setq files (cdr files)))
812 (buffer-string))))
814 ;;; Insert tags interactively on C-c TAB t
816 (defvar muse-tag-history nil
817 "List of recently-entered tags; used by `muse-insert-tag'.
818 If you want a tag to start as the default, you may manually set
819 this variable to a list.")
821 (defvar muse-custom-tags nil
822 "Keep track of any new tags entered in `muse-insert-tag'.
823 If there are (X)HTML tags that you use frequently with that
824 function, you might want to set this manually.")
826 ;;;###autoload
827 (defun muse-insert-tag (tag)
828 "Insert a tag interactively with a blank line after it."
829 (interactive
830 (list
831 (funcall
832 muse-completing-read-function
833 (concat "Tag: "
834 (when muse-tag-history
835 (concat "(default: " (car muse-tag-history) ") ")))
836 (progn
837 (require 'muse-publish)
838 (mapcar 'list (nconc (mapcar 'car muse-publish-markup-tags)
839 muse-custom-tags)))
840 nil nil nil 'muse-tag-history
841 (car muse-tag-history))))
842 (when (equal tag "")
843 (setq tag (car muse-tag-history)))
844 (unless (interactive-p)
845 (require 'muse-publish))
846 (let ((tag-entry (assoc tag muse-publish-markup-tags))
847 (options ""))
848 ;; Add to custom list if no entry exists
849 (unless tag-entry
850 (add-to-list 'muse-custom-tags tag))
851 ;; Get option
852 (when (nth 2 tag-entry)
853 (setq options (read-string "Option: ")))
854 (unless (equal options "")
855 (setq options (concat " " options)))
856 ;; Insert the tag, closing if necessary
857 (when tag (insert (concat "<" tag options ">")))
858 (when (nth 1 tag-entry)
859 (insert (concat "\n\n</" tag ">\n"))
860 (forward-line -2))))
862 ;;; Muse list edit minor mode
864 (defvar muse-list-edit-minor-mode-map
865 (let ((map (make-sparse-keymap)))
866 (define-key map [(meta return)] 'muse-l-e-m-m-insert-list-item)
867 (define-key map [(control ?>)] 'muse-l-e-m-m-increase-list-item-indent)
868 (define-key map [(control ?<)] 'muse-l-e-m-m-decrease-list-item-indent)
870 map)
871 "Keymap used by Muse list edit minor mode.")
873 (defvar muse-l-e-m-m-list-item-regexp
874 (concat "^%s\\(\\([^\n" muse-regexp-blank "].*?\\)?::"
875 "\\(?:[" muse-regexp-blank "]+\\|$\\)"
876 "\\|[" muse-regexp-blank "]?[-*+][" muse-regexp-blank "]*"
877 "\\|[" muse-regexp-blank "][0-9]+\\.[" muse-regexp-blank "]*\\)")
878 "Regexp used to match the beginning of a list item.
879 This is used by `muse-list-edit-minor-mode'.
880 The '%s' will be replaced with a whitespace regexp when publishing.")
882 (defun muse-l-e-m-m-insert-list-item ()
883 "Insert a list item at the current point, taking into account
884 your current list type and indentation level."
885 (interactive)
886 (let ((muse-list-item-regexp muse-l-e-m-m-list-item-regexp))
887 (call-interactively 'muse-insert-list-item)))
889 (defun muse-l-e-m-m-increase-list-item-indent ()
890 "Increase the indentation of the current list item."
891 (interactive)
892 (let ((muse-list-item-regexp muse-l-e-m-m-list-item-regexp))
893 (call-interactively 'muse-increase-list-item-indentation)))
895 (defun muse-l-e-m-m-decrease-list-item-indent ()
896 "Decrease the indentation of the current list item."
897 (interactive)
898 (let ((muse-list-item-regexp muse-l-e-m-m-list-item-regexp))
899 (call-interactively 'muse-decrease-list-item-indentation)))
901 (defvar muse-l-e-m-m-data nil
902 "A list of data that was changed by Muse list edit minor mode.")
903 (make-variable-buffer-local 'muse-l-e-m-m-data)
905 ;;;###autoload
906 (define-minor-mode muse-list-edit-minor-mode
907 "This is a global minor mode for editing files with lists.
908 It is meant to be used with other major modes, and not with Muse mode.
910 Interactively, with no prefix argument, toggle the mode.
911 With universal prefix ARG turn mode on.
912 With zero or negative ARG turn mode off.
914 This minor mode provides the Muse keybindings for editing lists,
915 and support for filling lists properly.
917 This recognizes not only Muse-style lists, which use the \"-\"
918 character or numbers, but also lists that use asterisks or plus
919 signs. This should make the minor mode generally useful.
921 Definition lists and footnotes are also recognized.
923 Note that list items may omit leading spaces, for compatibility
924 with modes that set `left-margin', such as
925 `debian-changelog-mode'.
927 \\{muse-list-edit-minor-mode-map}"
928 :init-value nil
929 :lighter ""
930 :keymap muse-list-edit-minor-mode-map
931 :global nil
932 :group 'muse-mode
933 (if (not muse-list-edit-minor-mode)
934 ;; deactivate
935 (when muse-l-e-m-m-data
936 (setq adaptive-fill-regexp (cdr (assoc "a-f-r" muse-l-e-m-m-data))
937 paragraph-start (cdr (assoc "p-s" muse-l-e-m-m-data))
938 fill-prefix (cdr (assoc "f-p" muse-l-e-m-m-data)))
939 (setq muse-l-e-m-m-data nil))
940 ;; activate
941 (unless muse-l-e-m-m-data
942 ;; save previous fill-related data so we can restore it later
943 (setq muse-l-e-m-m-data
944 (list (cons "a-f-r" adaptive-fill-regexp)
945 (cons "p-s" paragraph-start)
946 (cons "f-p" fill-prefix))))
947 ;; make fill work nicely with item lists
948 (let ((regexp (concat "\\s-*\\([-*+]\\|[0-9]+\\.\\)\\s-+"
949 "\\|\\[[0-9]+\\]\\s-*"
950 "\\|.*\\s-*::\\s-+")))
951 (set (make-local-variable 'adaptive-fill-regexp)
952 (concat regexp "\\|\\s-*"))
953 (set (make-local-variable 'paragraph-start)
954 (concat paragraph-start "\\|" regexp)))
955 ;; force fill-prefix to be nil, because if it is a string that has
956 ;; initial spaces, it messes up fill-paragraph's algorithm
957 (set (make-local-variable 'fill-prefix) nil)))
959 (defun turn-on-muse-list-edit-minor-mode ()
960 "Unconditionally turn on Muse list edit minor mode."
961 (muse-list-edit-minor-mode 1))
963 (defun turn-off-muse-list-edit-minor-mode ()
964 "Unconditionally turn off Muse list edit minor mode."
965 (muse-list-edit-minor-mode -1))
967 ;;; muse-mode.el ends here