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