muse-mode: Use two spaces when filling definition list terms
[muse-el.git] / lisp / muse-mode.el
blob11c40a52a1fe44e2c4ecd60b43eebf085806c423
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 ;; Gregory Collins (greg AT gregorycollins DOT net) fixed a bug with
42 ;; paragraph separation and headings when filling.
44 ;;; Code:
46 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
48 ;; Emacs Muse Major Mode
50 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
52 (provide 'muse-mode)
54 (require 'muse)
55 (require 'muse-regexps)
56 (require 'muse-project)
58 (autoload 'muse-use-font-lock "muse-colors")
59 (autoload 'muse-publish-this-file "muse-publish")
60 (autoload 'muse-publish-get-style "muse-publish")
61 (autoload 'muse-publish-output-file "muse-publish")
63 (require 'derived)
64 (eval-when-compile
65 (condition-case nil
66 (require 'pcomplete) ; load if available
67 (error nil)))
69 ;;; Options:
71 (defgroup muse-mode nil
72 "Options controlling the behavior of the Muse editing Mode."
73 :group 'muse)
75 (defcustom muse-mode-highlight-p t
76 "If non-nil, highlight the content of Muse buffers."
77 :type 'boolean
78 :require 'muse-colors
79 :group 'muse-mode)
81 (defcustom muse-mode-auto-p nil
82 "If non-nil, automagically determine when Muse mode should be activated."
83 :type 'boolean
84 :set (function
85 (lambda (sym value)
86 (if value
87 (add-hook 'find-file-hooks 'muse-mode-maybe)
88 (remove-hook 'find-file-hooks 'muse-mode-maybe))
89 (set sym value)))
90 :group 'muse-mode)
92 (defun muse-mode-maybe-after-init ()
93 (when muse-mode-auto-p
94 (add-hook 'find-file-hooks 'muse-mode-maybe)))
96 ;; If the user sets this value in their init file, make sure that
97 ;; it takes effect
98 (add-hook 'after-init-hook 'muse-mode-maybe-after-init)
100 (defcustom muse-mode-intangible-links nil
101 "If non-nil, use the intangible property on links.
102 This can cause problems with flyspell (and potentially fill-mode),
103 so only enable this if you don't use either of these."
104 :type 'boolean
105 :group 'muse-mode)
107 (defcustom muse-mode-hook nil
108 "A hook that is run when Muse mode is entered."
109 :type 'hook
110 :options '(flyspell-mode footnote-mode turn-on-auto-fill
111 highlight-changes-mode)
112 :group 'muse-mode)
114 (defcustom muse-grep-command
115 "find %D -type f ! -name '*~' | xargs -I {} echo \\\"{}\\\" | xargs egrep -n -e \"%W\""
116 "The command to use when grepping for backlinks and other
117 searches through the muse projects. The string %D is replaced by
118 the directories from muse-project-alist, space-separated. The
119 string %W is replaced with the name of the muse page or whatever
120 else you are searching for. This command has been modified to
121 handle spaces in filenames, which were giving egrep a problem.
123 Note: We highly recommend using glimpse to search large projects.
124 To use glimpse, install and edit a file called .glimpse_exclude
125 in your home directory. Put a list of glob patterns in that file
126 to exclude Emacs backup files, etc. Then, run the indexer using:
128 glimpseindex -o <list of Wiki directories>
130 Once that's completed, customize this variable to have the
131 following value:
133 glimpse -nyi \"%W\"
135 Your searches will go much, much faster, especially for very
136 large projects. Don't forget to add a user cronjob to update the
137 index at intervals."
138 :type 'string
139 :group 'muse-mode)
141 (defvar muse-insert-map
142 (let ((map (make-sparse-keymap)))
143 (define-key map "l" 'muse-insert-relative-link-to-file)
144 (define-key map "t" 'muse-insert-tag)
145 (define-key map "u" 'muse-insert-url)
147 map))
149 ;;; Muse mode
151 (defvar muse-mode-map
152 (let ((map (make-sparse-keymap)))
153 (define-key map [(control ?c) (control ?a)] 'muse-index)
154 (define-key map [(control ?c) (control ?e)] 'muse-edit-link-at-point)
155 (define-key map [(control ?c) (control ?l)] 'font-lock-mode)
156 (define-key map [(control ?c) (control ?t)]
157 'muse-project-publish-this-file)
158 (define-key map [(control ?c) (control ?T)] 'muse-publish-this-file)
159 (define-key map [(control ?c) (meta control ?t)] 'muse-publish-this-file)
160 (define-key map [(control ?c) (control ?v)] 'muse-browse-result)
162 (define-key map [(control ?c) ?=] 'muse-what-changed)
164 (define-key map [tab] 'muse-next-reference)
165 (define-key map [(control ?i)] 'muse-next-reference)
167 (if (featurep 'xemacs)
168 (progn
169 (define-key map [(button2)] 'muse-follow-name-at-mouse)
170 (define-key map [(shift button2)]
171 'muse-follow-name-at-mouse-other-window))
172 (define-key map [(shift control ?m)]
173 'muse-follow-name-at-point-other-window)
174 (define-key map [mouse-2] 'muse-follow-name-at-mouse)
175 (define-key map [(shift mouse-2)]
176 'muse-follow-name-at-mouse-other-window))
178 (define-key map [(shift tab)] 'muse-previous-reference)
179 (unless (featurep 'xemacs)
180 (define-key map [(shift iso-lefttab)] 'muse-previous-reference)
181 (define-key map [(shift control ?i)] 'muse-previous-reference))
183 (define-key map [(control ?c) (control ?f)] 'muse-project-find-file)
184 (define-key map [(control ?c) (control ?p)] 'muse-project-publish)
186 (define-key map [(control ?c) (control ?i)] 'muse-insert-thing)
187 (define-key map [(control ?c) tab] 'muse-insert-thing)
189 ;; Searching functions
190 (define-key map [(control ?c) (control ?b)] 'muse-find-backlinks)
191 (define-key map [(control ?c) (control ?s)] 'muse-search)
193 ;; Enhanced list functions
194 (define-key map [(meta return)] 'muse-insert-list-item)
195 (define-key map [(control ?>)] 'muse-increase-list-item-indentation)
196 (define-key map [(control ?<)] 'muse-decrease-list-item-indentation)
198 (when (featurep 'pcomplete)
199 (define-key map [(meta tab)] 'pcomplete)
200 (define-key map [(meta control ?i)] 'pcomplete))
202 map)
203 "Keymap used by Emacs Muse mode.")
205 ;;;###autoload
206 (define-derived-mode muse-mode text-mode "Muse"
207 "Muse is an Emacs mode for authoring and publishing documents.
208 \\{muse-mode-map}"
209 ;; Since we're not inheriting from normal-mode, we need to
210 ;; explicitly run file variables.
211 (condition-case err
212 (hack-local-variables)
213 (error (message "File local-variables error: %s"
214 (prin1-to-string err))))
215 ;; Avoid lock-up caused by use of the 'intangible' text property
216 ;; with flyspell.
217 (unless muse-mode-intangible-links
218 (set (make-local-variable 'inhibit-point-motion-hooks) t))
219 (setq muse-current-project (muse-project-of-file))
220 (muse-project-set-variables)
221 ;; Make fill not split up links
222 (when (boundp 'fill-nobreak-predicate)
223 (make-local-variable 'fill-nobreak-predicate)
224 ;; Work around annoying inconsistency in fill handling between
225 ;; Emacs 21 and 22.
226 (if (< emacs-major-version 22)
227 (setq fill-nobreak-predicate 'muse-mode-fill-nobreak-p)
228 (add-to-list 'fill-nobreak-predicate
229 'muse-mode-fill-nobreak-p)))
230 ;; Make fill work nicely with item lists
231 (let ((regexp (concat "\\s-+\\(-\\|[0-9]+\\.\\)\\s-+"
232 "\\|\\[[0-9]+\\]\\s-*"
233 "\\|.*\\s-*::\\s-+"
234 "\\|\\*+\\s-+")))
235 (set (make-local-variable 'adaptive-fill-regexp)
236 (concat regexp "\\|\\s-*"))
237 (set (make-local-variable 'paragraph-start)
238 (concat paragraph-start "\\|" regexp))
239 (set (make-local-variable 'paragraph-separate)
240 (concat paragraph-separate "\\|\\*+\\s-+")))
241 (set (make-local-variable 'fill-paragraph-function)
242 'muse-mode-fill-paragraph)
244 ;; Comment syntax is `; comment'
245 (set (make-local-variable 'comment-start)
246 "; ")
247 (set (make-local-variable 'comment-start-skip)
248 "^;\\s-+")
249 (set (make-local-variable 'indent-line-function)
250 #'ignore)
251 ;; If we're using Emacs21, this makes flyspell work like it should
252 (when (boundp 'flyspell-generic-check-word-p)
253 (set (make-local-variable 'flyspell-generic-check-word-p)
254 'muse-mode-flyspell-p))
255 ;; If pcomplete is available, set it up
256 (when (featurep 'pcomplete)
257 (set (make-local-variable 'pcomplete-default-completion-function)
258 'muse-mode-completions)
259 (set (make-local-variable 'pcomplete-command-completion-function)
260 'muse-mode-completions)
261 (set (make-local-variable 'pcomplete-parse-arguments-function)
262 'muse-mode-current-word))
263 ;; Initialize any auto-generated variables
264 (run-hooks 'muse-update-values-hook)
265 (when muse-mode-highlight-p
266 (muse-use-font-lock)))
268 (put 'muse-mode
269 'flyspell-mode-predicate
270 'muse-mode-flyspell-p)
272 (defun muse-mode-fill-nobreak-p ()
273 "Return nil if we should allow a fill to occur at point.
274 Otherwise return non-nil.
276 This is used to keep long explicit links from being mangled by
277 fill mode."
278 (save-excursion
279 (save-match-data
280 (and (re-search-backward "\\[\\[\\|\\]\\]"
281 (line-beginning-position) t)
282 (string= (or (match-string 0) "")
283 "[[")))))
285 (defun muse-mode-fill-paragraph (arg)
286 "If a definition list is at point, use special filling rules for it.
287 Otherwise return nil to let the normal filling function take care
288 of things.
290 ARG is passed to `fill-paragraph'."
291 (and (not (muse-mode-fill-nobreak-p))
292 (save-excursion
293 (beginning-of-line)
294 (looking-at muse-dl-term-regexp))
295 (let ((fill-prefix " ")
296 (fill-paragraph-function nil))
297 (prog1 t
298 (fill-paragraph arg)))))
300 (defun muse-mode-flyspell-p ()
301 "Return non-nil if we should allow spell-checking to occur at point.
302 Otherwise return nil.
304 This is used to keep links from being improperly colorized by flyspell."
305 (and (not (get-text-property (if (bobp) (point) (1- (point)))
306 'muse-link))
307 (save-match-data
308 (null (muse-link-at-point)))))
310 ;;;###autoload
311 (defun muse-mode-choose-mode ()
312 "Turn the proper Emacs Muse related mode on for this file."
313 (let ((project (muse-project-of-file)))
314 (funcall (or (and project (muse-get-keyword :major-mode (cadr project) t))
315 'muse-mode))))
317 (defun muse-mode-maybe ()
318 "Maybe turn Emacs Muse mode on for this file."
319 (let ((project (muse-project-of-file)))
320 (and project
321 (funcall (or (muse-get-keyword :major-mode (cadr project) t)
322 'muse-mode)))))
324 ;;; Enhanced list editing
326 (defun muse-on-blank-line ()
327 "See if point is on a blank line"
328 (save-excursion
329 (beginning-of-line)
330 (looking-at (concat "[" muse-regexp-blank "]*$"))))
332 (defun muse-get-paragraph-start ()
333 "Return the start of the current paragraph. This function will
334 return nil if there are no prior paragraphs and the beginning of
335 the line if point is on a blank line."
336 (let ((para-start (concat "^[" muse-regexp-blank "]*$")))
337 ;; search back to start of paragraph
338 (save-excursion
339 (save-match-data
340 (if (not (muse-on-blank-line))
341 (re-search-backward para-start nil t)
342 (line-beginning-position))))))
344 (defun muse-insert-thing ()
345 "Prompt for something to insert into the current buffer."
346 (interactive)
347 (message "Insert:\nl link\nt Muse tag\nu URL")
348 (let (key cmd)
349 (let ((overriding-local-map muse-insert-map))
350 (setq key (read-key-sequence nil)))
351 (if (commandp (setq cmd (lookup-key muse-insert-map key)))
352 (progn (message "")
353 (call-interactively cmd))
354 (message "Not inserting anything"))))
356 ;;;###autoload
357 (defun muse-insert-list-item ()
358 "Insert a list item at the current point, taking into account
359 your current list type and indentation level."
360 (interactive)
361 (let ((newitem " - ")
362 (itemno nil)
363 (pstart (muse-get-paragraph-start))
364 (list-item (format muse-list-item-regexp
365 (concat "[" muse-regexp-blank "]*"))))
366 ;; search backwards for start of current item
367 (save-excursion
368 (when (re-search-backward list-item pstart t)
369 ;; save the matching item
370 (setq newitem (match-string 0))
371 ;; see what type it is
372 (if (string-match "::" (match-string 0))
373 ;; is a definition, replace the term
374 (setq newitem (concat " "
375 (read-string "Term: ")
376 " :: "))
377 ;; see if it's a numbered list
378 (when (string-match "[0-9]+" newitem)
379 ;; is numbered, so increment
380 (setq itemno (1+
381 (string-to-number
382 (match-string 0 newitem))))
383 (setq newitem (replace-match
384 (number-to-string itemno)
385 nil nil newitem))))))
386 ;; insert the new item
387 (insert (concat "\n" newitem))))
389 (defun muse-alter-list-item-indentation (operation)
390 "Alter the indentation of the current list item.
391 Valid values of OPERATION are 'increase and 'decrease."
392 (let ((pstart (muse-get-paragraph-start))
393 (list-item (format muse-list-item-regexp
394 (concat "[" muse-regexp-blank "]*")))
395 beg move-func indent)
396 ;; search backwards until start of paragraph to see if we are on a
397 ;; current item
398 (save-excursion
399 (if (or (progn (goto-char (muse-line-beginning-position))
400 ;; we are on an item
401 (looking-at list-item))
402 ;; not on item, so search backwards
403 (re-search-backward list-item pstart t))
404 (let ((beg (point)))
405 ;; we are on an item
406 (setq indent (buffer-substring (match-beginning 0)
407 (match-beginning 1)))
408 (muse-forward-list-item (muse-list-item-type (match-string 1))
409 (concat "[" muse-regexp-blank "]*")
411 (save-restriction
412 (narrow-to-region beg (point))
413 (goto-char (point-min))
414 (let ((halt nil))
415 (while (< (point) (point-max))
416 ;; increase or decrease the indentation
417 (unless halt
418 (cond ((eq operation 'increase)
419 (insert " "))
420 ((eq operation 'decrease)
421 (if (looking-at " ")
422 ;; we have enough space, so delete it
423 (delete-region (match-beginning 0)
424 (match-end 0))
425 (setq halt t)))))
426 (forward-line 1)))))
427 ;; we are not on an item, so warn
428 (message "You are not on a list item.")))))
430 ;;;###autoload
431 (defun muse-increase-list-item-indentation ()
432 "Increase the indentation of the current list item."
433 (interactive)
434 (muse-alter-list-item-indentation 'increase))
436 ;;;###autoload
437 (defun muse-decrease-list-item-indentation ()
438 "Decrease the indentation of the current list item."
439 (interactive)
440 (muse-alter-list-item-indentation 'decrease))
442 ;;; Support page name completion using pcomplete
444 (defun muse-mode-completions ()
445 "Return a list of possible completions names for this buffer."
446 (let ((project (muse-project-of-file)))
447 (if project
448 (while (pcomplete-here
449 (mapcar 'car (muse-project-file-alist project)))))))
451 (defun muse-mode-current-word ()
452 (let ((end (point)))
453 (save-excursion
454 (save-restriction
455 (skip-chars-backward (concat "^\\[\n" muse-regexp-blank))
456 (narrow-to-region (point) end))
457 (pcomplete-parse-buffer-arguments))))
459 ;;; Navigate/visit links or URLs. Use TAB, S-TAB and RET (or mouse-2).
461 (defun muse-link-at-point (&optional pos)
462 "Return link text if a URL or link is at point."
463 (let ((case-fold-search nil)
464 (inhibit-point-motion-hooks t)
465 (here (or pos (point))))
466 ;; if we are using muse-colors, we can just use link properties to
467 ;; determine whether we are on a link
468 (if (featurep 'muse-colors)
469 (when (get-text-property here 'muse-link)
470 (save-excursion
471 (when (and (not (bobp))
472 (get-text-property (1- here) 'muse-link))
473 (goto-char (or (previous-single-property-change here 'muse-link)
474 (point-min))))
475 (if (looking-at muse-explicit-link-regexp)
476 (progn
477 (goto-char (match-beginning 1))
478 (muse-handle-explicit-link))
479 (muse-handle-implicit-link))))
480 ;; use fallback method to find a link
481 (when (or (null pos)
482 (and (char-after pos)
483 (not (eq (char-syntax (char-after pos)) ?\ ))))
484 (save-excursion
485 (goto-char here)
486 ;; check for explicit link here or before point
487 (if (or (looking-at muse-explicit-link-regexp)
488 (and
489 (re-search-backward "\\[\\[\\|\\]\\]"
490 (muse-line-beginning-position)
492 (string= (or (match-string 0) "") "[[")
493 (looking-at muse-explicit-link-regexp)))
494 (progn
495 (goto-char (match-beginning 1))
496 (muse-handle-explicit-link))
497 (goto-char here)
498 ;; check for bare URL or other link type
499 (skip-chars-backward (concat "^'\"<>{}(\n" muse-regexp-blank))
500 (and (looking-at muse-implicit-link-regexp)
501 (muse-handle-implicit-link))))))))
503 (defun muse-make-link (link &optional desc)
504 "Return a link to LINK with DESC as the description."
505 (when (string-match muse-explicit-link-regexp link)
506 (unless desc (setq desc (muse-get-link-desc link)))
507 (setq link (muse-get-link link)))
508 (if (and desc
509 link
510 (not (string= desc ""))
511 (not (string= link desc)))
512 (concat "[[" (muse-link-escape link) "][" (muse-link-escape desc) "]]")
513 (concat "[[" (or (muse-link-escape link) "") "]]")))
515 ;;;###autoload
516 (defun muse-insert-relative-link-to-file ()
517 "Insert a relative link to a file, with optional description, at point."
518 ;; Perhaps the relative location should be configurable, so that the
519 ;; file search would start in the publishing directory and then
520 ;; insert the link relative to the publishing directory
521 (interactive)
522 (insert
523 (muse-make-link (file-relative-name (read-file-name "Link: "))
524 (read-string "Text: "))))
526 (defun muse-insert-url ()
527 "Insert a URL, with optional description, at point."
528 (interactive)
529 (insert
530 (muse-make-link (read-string "URL: ")
531 (read-string "Text: "))))
533 ;;;###autoload
534 (defun muse-edit-link-at-point ()
535 "Edit the current link.
536 Do not rename the page originally referred to."
537 (interactive)
538 (if (muse-link-at-point)
539 (let ((link (muse-link-unescape (muse-get-link)))
540 (desc (muse-link-unescape (muse-get-link-desc))))
541 (replace-match
542 (save-match-data
543 (muse-make-link
544 (read-string "Link: " link)
545 (read-string "Text: " desc)))
546 t t))
547 (error "There is no valid link at point")))
549 (defun muse-visit-link-default (link &optional other-window)
550 "Visit the URL or link named by LINK.
551 If ANCHOR is specified, search for it after opening LINK.
553 This is the default function to call when visiting links; it is
554 used by `muse-visit-link' if you have not specified :visit-link
555 in `muse-project-alist'."
556 (if (string-match muse-url-regexp link)
557 (muse-browse-url link)
558 (let (anchor
559 base-buffer)
560 (when (string-match "#" link)
561 (setq anchor (substring link (match-beginning 0))
562 link (if (= (match-beginning 0) 0)
563 ;; If there is an anchor but no link, default
564 ;; to the current page.
566 (substring link 0 (match-beginning 0)))))
567 (when link
568 (setq base-buffer (get-buffer link))
569 (if (and base-buffer (not (buffer-file-name base-buffer)))
570 ;; If file is temporary (no associated file), just switch to
571 ;; the buffer
572 (if other-window
573 (switch-to-buffer-other-window base-buffer)
574 (switch-to-buffer base-buffer))
575 (let ((project (muse-project-of-file)))
576 (if project
577 (muse-project-find-file link project
578 (and other-window
579 'find-file-other-window))
580 (if other-window
581 (find-file-other-window link)
582 (find-file link))))))
583 (when anchor
584 (let ((pos (point))
585 (regexp (concat "^\\W*" (regexp-quote anchor) "\\b"))
586 last)
587 (goto-char (point-min))
588 (while (and (setq last (re-search-forward regexp nil t))
589 (muse-link-at-point)))
590 (unless last
591 (goto-char pos)
592 (message "Could not find anchor `%s'" anchor)))))))
594 (defun muse-visit-link (link &optional other-window)
595 "Visit the URL or link named by LINK."
596 (let ((visit-link-function
597 (muse-get-keyword :visit-link (cadr (muse-project-of-file)) t)))
598 (if visit-link-function
599 (funcall visit-link-function link other-window)
600 (muse-visit-link-default link other-window))))
602 ;;;###autoload
603 (defun muse-browse-result (style &optional other-window)
604 "Visit the current page's published result."
605 (interactive
606 (list (muse-project-get-applicable-style buffer-file-name
607 (cddr muse-current-project))
608 current-prefix-arg))
609 (setq style (muse-style style))
610 (let* ((output-dir (muse-style-element :path style))
611 (result-path (muse-publish-output-file buffer-file-name output-dir
612 style))
613 (muse-current-output-style (list :base (car style)
614 :path output-dir)))
615 (if (not (file-readable-p result-path))
616 (error "Cannot open output file '%s'" result-path)
617 (if other-window
618 (find-file-other-window result-path)
619 (let ((func (muse-style-element :browser style t)))
620 (if func
621 (funcall func result-path)
622 (message "The %s publishing style does not support browsing."
623 style)))))))
625 ;;;###autoload
626 (defun muse-follow-name-at-point (&optional other-window)
627 "Visit the link at point."
628 (interactive "P")
629 (let ((link (muse-link-at-point)))
630 (if link
631 (muse-visit-link link other-window)
632 (error "There is no valid link at point"))))
634 ;;;###autoload
635 (defun muse-follow-name-at-point-other-window ()
636 "Visit the link at point in other window."
637 (interactive)
638 (muse-follow-name-at-point t))
640 (defun muse-follow-name-at-mouse (event &optional other-window)
641 "Visit the link at point, or yank text if none is found."
642 (interactive "eN")
643 (unless
644 (save-excursion
645 (cond ((fboundp 'event-window) ; XEmacs
646 (set-buffer (window-buffer (event-window event)))
647 (and (funcall (symbol-function 'event-point) event)
648 (goto-char (funcall (symbol-function 'event-point)
649 event))))
650 ((fboundp 'posn-window) ; Emacs
651 (set-buffer (window-buffer (posn-window (event-start event))))
652 (goto-char (posn-point (event-start event)))))
653 (let ((link (muse-link-at-point)))
654 (when link
655 (muse-visit-link link other-window)
656 t)))
657 ;; Fall back to normal binding for this event
658 (call-interactively
659 (lookup-key (current-global-map) (this-command-keys)))))
661 (defun muse-follow-name-at-mouse-other-window (event)
662 "Visit the link at point"
663 (interactive "e")
664 ;; throw away the old window position, since other-window will
665 ;; change it anyway
666 (select-window (car (cadr event)))
667 (muse-follow-name-at-mouse event t))
669 ;;;###autoload
670 (defun muse-next-reference ()
671 "Move forward to next Muse link or URL, cycling if necessary."
672 (interactive)
673 (let ((pos))
674 (save-excursion
675 (when (get-text-property (point) 'muse-link)
676 (goto-char (or (next-single-property-change (point) 'muse-link)
677 (point-max))))
679 (setq pos (next-single-property-change (point) 'muse-link))
681 (when (not pos)
682 (if (get-text-property (point-min) 'muse-link)
683 (setq pos (point-min))
684 (setq pos (next-single-property-change (point-min) 'muse-link)))))
686 (when pos
687 (goto-char pos))))
689 ;;;###autoload
690 (defun muse-previous-reference ()
691 "Move backward to the next Muse link or URL, cycling if necessary.
692 In case of Emacs x <= 21 and ignoring of intangible properties (see
693 `muse-mode-intangible-links').
695 This function is not entirely accurate, but it's close enough."
696 (interactive)
697 (let ((pos))
698 (save-excursion
700 ;; Hack: The user perceives the two cases of point ("|")
701 ;; position (1) "|[[" and (2) "[[|" or "][|" as "point is at
702 ;; start of link". But in the sense of the function
703 ;; "previous-single-property-change" these two cases are
704 ;; different. The following code aligns these two cases. Emacs
705 ;; 21: If the intangible property is ignored case (2) is more
706 ;; complicate and this hack only solves the problem partially.
708 (when (and (get-text-property (point) 'muse-link)
709 (muse-looking-back "\\[\\|\\]"))
710 (goto-char (or (previous-single-property-change (point) 'muse-link)
711 (point-min))))
713 (when (eq (point) (point-min))
714 (goto-char (point-max)))
716 (setq pos (previous-single-property-change (point) 'muse-link))
718 (when (not pos)
719 (if (get-text-property (point-min) 'muse-link)
720 (setq pos (point-min))
721 (setq pos (previous-single-property-change (point-max)
722 'muse-link)))))
724 (when pos
725 (if (get-text-property pos 'muse-link)
726 (goto-char pos)
727 (goto-char (or (previous-single-property-change pos 'muse-link)
728 (point-min)))))))
730 ;;;###autoload
731 (defun muse-what-changed ()
732 "Show the unsaved changes that have been made to the current file."
733 (interactive)
734 (diff-backup buffer-file-name))
737 ;;; Find text in project pages, or pages referring to the current page
739 (defvar muse-search-history nil)
741 (defun muse-grep (string &optional grep-command-no-shadow)
742 "Grep for STRING in the project directories.
743 GREP-COMMAND if passed will supplant `muse-grep-command'."
744 ;; careful - grep-command leaks into compile, so we call it
745 ;; -no-shadow instead
746 (require 'compile)
747 (let* ((str (or grep-command-no-shadow muse-grep-command))
748 (muse-directories (mapcar
749 (lambda (thing)
750 (car (cadr thing)))
751 muse-project-alist))
752 (dirs (mapconcat (lambda (dir)
753 (shell-quote-argument
754 (expand-file-name dir)))
755 muse-directories " ")))
756 (if (string= dirs "")
757 (muse-display-warning
758 "No directories were found in the current project; aborting search")
759 (while (string-match "%W" str)
760 (setq str (replace-match string t t str)))
761 (while (string-match "%D" str)
762 (setq str (replace-match dirs t t str)))
763 (if (fboundp 'compilation-start)
764 (compilation-start str nil (lambda (&rest args) "*search*")
765 grep-regexp-alist)
766 (and (fboundp 'compile-internal)
767 (compile-internal str "No more search hits" "search"
768 nil grep-regexp-alist))))))
770 ;;;###autoload
771 (defun muse-search-with-command (text)
772 "Search for the given TEXT string in the project directories
773 using the specified command."
774 (interactive
775 (list (let ((str (concat muse-grep-command)) pos)
776 (when (string-match "%W" str)
777 (setq pos (match-beginning 0))
778 (unless (featurep 'xemacs)
779 (setq pos (1+ pos)))
780 (setq str (replace-match "" t t str)))
781 (read-from-minibuffer "Search command: "
782 (cons str pos) nil nil
783 'muse-search-history))))
784 (muse-grep nil text))
786 ;;;###autoload
787 (defun muse-search ()
788 "Search for the given TEXT using the default grep command."
789 (interactive)
790 (muse-grep (read-string "Search: ")))
792 ;;;###autoload
793 (defun muse-find-backlinks ()
794 "Grep for the current pagename in all the project directories."
795 (interactive)
796 (muse-grep (muse-page-name)))
799 ;;; Generate an index of all known Muse pages
801 (defun muse-generate-index (&optional as-list exclude-private)
802 "Generate an index of all Muse pages."
803 (let ((index (muse-index-as-string as-list exclude-private)))
804 (with-current-buffer (get-buffer-create "*Muse Index*")
805 (erase-buffer)
806 (insert index)
807 (current-buffer))))
809 ;;;###autoload
810 (defun muse-index ()
811 "Display an index of all known Muse pages."
812 (interactive)
813 (message "Generating Muse index...")
814 (let ((project (muse-project)))
815 (with-current-buffer (muse-generate-index)
816 (goto-char (point-min))
817 (muse-mode)
818 (setq muse-current-project project)
819 (pop-to-buffer (current-buffer))))
820 (message "Generating Muse index...done"))
822 (defun muse-index-as-string (&optional as-list exclude-private exclude-current)
823 "Generate an index of all Muse pages.
824 If AS-LIST is non-nil, insert a dash and spaces before each item.
825 If EXCLUDE-PRIVATE is non-nil, exclude files that have private permissions.
826 If EXCLUDE-CURRENT is non-nil, exclude the current file from the output."
827 (let ((files (sort (copy-alist (muse-project-file-alist))
828 (function
829 (lambda (l r)
830 (string-lessp (car l) (car r)))))))
831 (when (and exclude-current (muse-page-name))
832 (setq files (delete (assoc (muse-page-name) files) files)))
833 (with-temp-buffer
834 (while files
835 (unless (and exclude-private
836 (muse-project-private-p (cdar files)))
837 (insert (if as-list " - " "") "[[" (caar files) "]]\n"))
838 (setq files (cdr files)))
839 (buffer-string))))
841 ;;; Insert tags interactively on C-c TAB t
843 (defvar muse-tag-history nil
844 "List of recently-entered tags; used by `muse-insert-tag'.
845 If you want a tag to start as the default, you may manually set
846 this variable to a list.")
848 (defvar muse-custom-tags nil
849 "Keep track of any new tags entered in `muse-insert-tag'.
850 If there are (X)HTML tags that you use frequently with that
851 function, you might want to set this manually.")
853 ;;;###autoload
854 (defun muse-insert-tag (tag)
855 "Insert a tag interactively with a blank line after it."
856 (interactive
857 (list
858 (funcall
859 muse-completing-read-function
860 (concat "Tag: "
861 (when muse-tag-history
862 (concat "(default: " (car muse-tag-history) ") ")))
863 (progn
864 (require 'muse-publish)
865 (mapcar 'list (nconc (mapcar 'car muse-publish-markup-tags)
866 muse-custom-tags)))
867 nil nil nil 'muse-tag-history
868 (car muse-tag-history))))
869 (when (equal tag "")
870 (setq tag (car muse-tag-history)))
871 (unless (interactive-p)
872 (require 'muse-publish))
873 (let ((tag-entry (assoc tag muse-publish-markup-tags))
874 (options ""))
875 ;; Add to custom list if no entry exists
876 (unless tag-entry
877 (add-to-list 'muse-custom-tags tag))
878 ;; Get option
879 (when (nth 2 tag-entry)
880 (setq options (read-string "Option: ")))
881 (unless (equal options "")
882 (setq options (concat " " options)))
883 ;; Insert the tag, closing if necessary
884 (when tag (insert (concat "<" tag options ">")))
885 (when (nth 1 tag-entry)
886 (insert (concat "\n\n</" tag ">\n"))
887 (forward-line -2))))
889 ;;; Muse list edit minor mode
891 (defvar muse-list-edit-minor-mode-map
892 (let ((map (make-sparse-keymap)))
893 (define-key map [(meta return)] 'muse-l-e-m-m-insert-list-item)
894 (define-key map [(control ?>)] 'muse-l-e-m-m-increase-list-item-indent)
895 (define-key map [(control ?<)] 'muse-l-e-m-m-decrease-list-item-indent)
897 map)
898 "Keymap used by Muse list edit minor mode.")
900 (defvar muse-l-e-m-m-list-item-regexp
901 (concat "^%s\\(\\([^\n" muse-regexp-blank "].*?\\)?::"
902 "\\(?:[" muse-regexp-blank "]+\\|$\\)"
903 "\\|[" muse-regexp-blank "]?[-*+][" muse-regexp-blank "]*"
904 "\\|[" muse-regexp-blank "][0-9]+\\.[" muse-regexp-blank "]*\\)")
905 "Regexp used to match the beginning of a list item.
906 This is used by `muse-list-edit-minor-mode'.
907 The '%s' will be replaced with a whitespace regexp when publishing.")
909 (defun muse-l-e-m-m-insert-list-item ()
910 "Insert a list item at the current point, taking into account
911 your current list type and indentation level."
912 (interactive)
913 (let ((muse-list-item-regexp muse-l-e-m-m-list-item-regexp))
914 (call-interactively 'muse-insert-list-item)))
916 (defun muse-l-e-m-m-increase-list-item-indent ()
917 "Increase the indentation of the current list item."
918 (interactive)
919 (let ((muse-list-item-regexp muse-l-e-m-m-list-item-regexp))
920 (call-interactively 'muse-increase-list-item-indentation)))
922 (defun muse-l-e-m-m-decrease-list-item-indent ()
923 "Decrease the indentation of the current list item."
924 (interactive)
925 (let ((muse-list-item-regexp muse-l-e-m-m-list-item-regexp))
926 (call-interactively 'muse-decrease-list-item-indentation)))
928 (defvar muse-l-e-m-m-data nil
929 "A list of data that was changed by Muse list edit minor mode.")
930 (make-variable-buffer-local 'muse-l-e-m-m-data)
932 ;;;###autoload
933 (define-minor-mode muse-list-edit-minor-mode
934 "This is a global minor mode for editing files with lists.
935 It is meant to be used with other major modes, and not with Muse mode.
937 Interactively, with no prefix argument, toggle the mode.
938 With universal prefix ARG turn mode on.
939 With zero or negative ARG turn mode off.
941 This minor mode provides the Muse keybindings for editing lists,
942 and support for filling lists properly.
944 It recognizes not only Muse-style lists, which use the \"-\"
945 character or numbers, but also lists that use asterisks or plus
946 signs. This should make the minor mode generally useful.
948 Definition lists and footnotes are also recognized.
950 Note that list items may omit leading spaces, for compatibility
951 with modes that set `left-margin', such as
952 `debian-changelog-mode'.
954 \\{muse-list-edit-minor-mode-map}"
955 :init-value nil
956 :lighter ""
957 :keymap muse-list-edit-minor-mode-map
958 :global nil
959 :group 'muse-mode
960 (if (not muse-list-edit-minor-mode)
961 ;; deactivate
962 (when muse-l-e-m-m-data
963 (setq adaptive-fill-regexp (cdr (assoc "a-f-r" muse-l-e-m-m-data))
964 paragraph-start (cdr (assoc "p-s" muse-l-e-m-m-data))
965 fill-prefix (cdr (assoc "f-p" muse-l-e-m-m-data)))
966 (setq muse-l-e-m-m-data nil))
967 ;; activate
968 (unless muse-l-e-m-m-data
969 ;; save previous fill-related data so we can restore it later
970 (setq muse-l-e-m-m-data
971 (list (cons "a-f-r" adaptive-fill-regexp)
972 (cons "p-s" paragraph-start)
973 (cons "f-p" fill-prefix))))
974 ;; make fill work nicely with item lists
975 (let ((regexp (concat "\\s-*\\([-*+]\\|[0-9]+\\.\\)\\s-+"
976 "\\|\\[[0-9]+\\]\\s-*"
977 "\\|.*\\s-*::\\s-+")))
978 (set (make-local-variable 'adaptive-fill-regexp)
979 (concat regexp "\\|\\s-*"))
980 (set (make-local-variable 'paragraph-start)
981 (concat paragraph-start "\\|" regexp)))
982 ;; force fill-prefix to be nil, because if it is a string that has
983 ;; initial spaces, it messes up fill-paragraph's algorithm
984 (set (make-local-variable 'fill-prefix) nil)))
986 (defun turn-on-muse-list-edit-minor-mode ()
987 "Unconditionally turn on Muse list edit minor mode."
988 (muse-list-edit-minor-mode 1))
990 (defun turn-off-muse-list-edit-minor-mode ()
991 "Unconditionally turn off Muse list edit minor mode."
992 (muse-list-edit-minor-mode -1))
994 ;;; muse-mode.el ends here