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