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