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.
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.
29 ;; Andrea Riciputi (ariciputi AT pito DOT com) gave an initial
30 ;; implementation for tag completion by means of the `muse-insert-tag'
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.
46 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
48 ;; Emacs Muse Major Mode
50 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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")
66 (require 'pcomplete
) ; load if available
71 (defgroup muse-mode nil
72 "Options controlling the behavior of the Muse editing Mode."
75 (defcustom muse-mode-highlight-p t
76 "If non-nil, highlight the content of Muse buffers."
81 (defcustom muse-mode-auto-p nil
82 "If non-nil, automagically determine when Muse mode should be activated."
87 (add-hook 'find-file-hooks
'muse-mode-maybe
)
88 (remove-hook 'find-file-hooks
'muse-mode-maybe
))
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
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."
107 (defcustom muse-mode-hook nil
108 "A hook that is run when Muse mode is entered."
110 :options
'(flyspell-mode footnote-mode turn-on-auto-fill
111 highlight-changes-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
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
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
)
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)
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))
203 "Keymap used by Emacs Muse mode.")
206 (define-derived-mode muse-mode text-mode "Muse"
207 "Muse is an Emacs mode for authoring and publishing documents.
209 ;; Since we're not inheriting from normal-mode, we need to
210 ;; explicitly run file variables.
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
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
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-*"
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)
247 (set (make-local-variable 'comment-start-skip)
249 (set (make-local-variable 'indent-line-function)
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)))
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
280 (and (re-search-backward "\\[\\[\\|\\]\\]"
281 (line-beginning-position) t)
282 (string= (or (match-string 0) "")
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
290 ARG is passed to `fill-paragraph'."
291 (and (not (muse-mode-fill-nobreak-p))
294 (looking-at muse-dl-term-regexp))
295 (let ((fill-prefix " ")
296 (fill-paragraph-function nil))
298 (fill-paragraph arg)))))
300 (defun muse-mode-flyspell-p ()
301 "Return non-nil if we should allow spell-checking to occur at point.
302 Otherwise return nil.
304 This is used to keep links from being improperly colorized by flyspell."
305 (and (not (get-text-property (if (bobp) (point) (1- (point)))
308 (null (muse-link-at-point)))))
311 (defun muse-mode-choose-mode ()
312 "Turn the proper Emacs Muse related mode on for this file."
313 (let ((project (muse-project-of-file)))
314 (funcall (or (and project (muse-get-keyword :major-mode (cadr project) t))
317 (defun muse-mode-maybe ()
318 "Maybe turn Emacs Muse mode on for this file."
319 (let ((project (muse-project-of-file)))
321 (funcall (or (muse-get-keyword :major-mode (cadr project) t)
324 ;;; Enhanced list editing
326 (defun muse-on-blank-line ()
327 "See if point is on a blank line"
330 (looking-at (concat "[" muse-regexp-blank "]*$"))))
332 (defun muse-get-paragraph-start ()
333 "Return the start of the current paragraph. This function will
334 return nil if there are no prior paragraphs and the beginning of
335 the line if point is on a blank line."
336 (let ((para-start (concat "^[" muse-regexp-blank "]*$")))
337 ;; search back to start of paragraph
340 (if (not (muse-on-blank-line))
341 (re-search-backward para-start nil t)
342 (line-beginning-position))))))
344 (defun muse-insert-thing ()
345 "Prompt for something to insert into the current buffer."
347 (message "Insert:\nl link\nt Muse tag\nu URL")
349 (let ((overriding-local-map muse-insert-map))
350 (setq key (read-key-sequence nil)))
351 (if (commandp (setq cmd (lookup-key muse-insert-map key)))
353 (call-interactively cmd))
354 (message "Not inserting anything"))))
357 (defun muse-insert-list-item ()
358 "Insert a list item at the current point, taking into account
359 your current list type and indentation level."
361 (let ((newitem " - ")
363 (pstart (muse-get-paragraph-start))
364 (list-item (format muse-list-item-regexp
365 (concat "[" muse-regexp-blank "]*"))))
366 ;; search backwards for start of current item
368 (when (re-search-backward list-item pstart t)
369 ;; save the matching item
370 (setq newitem (match-string 0))
371 ;; see what type it is
372 (if (string-match "::" (match-string 0))
373 ;; is a definition, replace the term
374 (setq newitem (concat " "
375 (read-string "Term: ")
377 ;; see if it's a numbered list
378 (when (string-match "[0-9]+" newitem)
379 ;; is numbered, so increment
382 (match-string 0 newitem))))
383 (setq newitem (replace-match
384 (number-to-string itemno)
385 nil nil newitem))))))
386 ;; insert the new item
387 (insert (concat "\n" newitem))))
389 (defun muse-alter-list-item-indentation (operation)
390 "Alter the indentation of the current list item.
391 Valid values of OPERATION are 'increase and 'decrease."
392 (let ((pstart (muse-get-paragraph-start))
393 (list-item (format muse-list-item-regexp
394 (concat "[" muse-regexp-blank "]*")))
395 beg move-func indent)
396 ;; search backwards until start of paragraph to see if we are on a
399 (if (or (progn (goto-char (muse-line-beginning-position))
401 (looking-at list-item))
402 ;; not on item, so search backwards
403 (re-search-backward list-item pstart t))
406 (setq indent (buffer-substring (match-beginning 0)
407 (match-beginning 1)))
408 (muse-forward-list-item (muse-list-item-type (match-string 1))
409 (concat "[" muse-regexp-blank "]*")
412 (narrow-to-region beg (point))
413 (goto-char (point-min))
415 (while (< (point) (point-max))
416 ;; increase or decrease the indentation
418 (cond ((eq operation 'increase)
420 ((eq operation 'decrease)
422 ;; we have enough space, so delete it
423 (delete-region (match-beginning 0)
427 ;; we are not on an item, so warn
428 (message "You are not on a list item.")))))
431 (defun muse-increase-list-item-indentation ()
432 "Increase the indentation of the current list item."
434 (muse-alter-list-item-indentation 'increase))
437 (defun muse-decrease-list-item-indentation ()
438 "Decrease the indentation of the current list item."
440 (muse-alter-list-item-indentation 'decrease))
442 ;;; Support page name completion using pcomplete
444 (defun muse-mode-completions ()
445 "Return a list of possible completions names for this buffer."
446 (let ((project (muse-project-of-file)))
448 (while (pcomplete-here
449 (mapcar 'car (muse-project-file-alist project)))))))
451 (defun muse-mode-current-word ()
455 (skip-chars-backward (concat "^\\[\n" muse-regexp-blank))
456 (narrow-to-region (point) end))
457 (pcomplete-parse-buffer-arguments))))
459 ;;; Navigate/visit links or URLs. Use TAB, S-TAB and RET (or mouse-2).
461 (defun muse-link-at-point (&optional pos)
462 "Return link text if a URL or link is at point."
463 (let ((case-fold-search nil)
464 (inhibit-point-motion-hooks t)
465 (here (or pos (point))))
466 ;; if we are using muse-colors, we can just use link properties to
467 ;; determine whether we are on a link
468 (if (featurep 'muse-colors)
469 (when (get-text-property here 'muse-link)
471 (when (and (not (bobp))
472 (get-text-property (1- here) 'muse-link))
473 (goto-char (or (previous-single-property-change here 'muse-link)
475 (if (looking-at muse-explicit-link-regexp)
477 (goto-char (match-beginning 1))
478 (muse-handle-explicit-link))
479 (muse-handle-implicit-link))))
480 ;; use fallback method to find a link
482 (and (char-after pos)
483 (not (eq (char-syntax (char-after pos)) ?\ ))))
486 ;; check for explicit link here or before point
487 (if (or (looking-at muse-explicit-link-regexp)
489 (re-search-backward "\\[\\[\\|\\]\\]"
490 (muse-line-beginning-position)
492 (string= (or (match-string 0) "") "[[")
493 (looking-at muse-explicit-link-regexp)))
495 (goto-char (match-beginning 1))
496 (muse-handle-explicit-link))
498 ;; check for bare URL or other link type
499 (skip-chars-backward (concat "^'\"<>{}(\n" muse-regexp-blank))
500 (and (looking-at muse-implicit-link-regexp)
501 (muse-handle-implicit-link))))))))
503 (defun muse-make-link (link &optional desc)
504 "Return a link to LINK with DESC as the description."
505 (when (string-match muse-explicit-link-regexp link)
506 (unless desc (setq desc (muse-get-link-desc link)))
507 (setq link (muse-get-link link)))
510 (not (string= desc ""))
511 (not (string= link desc)))
512 (concat "[[" (muse-link-escape link) "][" (muse-link-escape desc) "]]")
513 (concat "[[" (or (muse-link-escape link) "") "]]")))
516 (defun muse-insert-relative-link-to-file ()
517 "Insert a relative link to a file, with optional description, at point."
518 ;; Perhaps the relative location should be configurable, so that the
519 ;; file search would start in the publishing directory and then
520 ;; insert the link relative to the publishing directory
523 (muse-make-link (file-relative-name (read-file-name "Link: "))
524 (read-string "Text: "))))
526 (defcustom muse-insert-url-initial-input "http://"
527 "The string to insert before reading a URL interactively.
528 This is used by the `muse-insert-url' command."
532 (defun muse-insert-url ()
533 "Insert a URL, with optional description, at point."
536 (muse-make-link (read-string "URL: " muse-insert-url-initial-input)
537 (read-string "Text: "))))
540 (defun muse-edit-link-at-point ()
541 "Edit the current link.
542 Do not rename the page originally referred to."
544 (if (muse-link-at-point)
545 (let ((link (muse-link-unescape (muse-get-link)))
546 (desc (muse-link-unescape (muse-get-link-desc))))
550 (read-string "Link: " link)
551 (read-string "Text: " desc)))
553 (error "There is no valid link at point")))
555 (defun muse-visit-link-default (link &optional other-window)
556 "Visit the URL or link named by LINK.
557 If ANCHOR is specified, search for it after opening LINK.
559 This is the default function to call when visiting links; it is
560 used by `muse-visit-link' if you have not specified :visit-link
561 in `muse-project-alist'."
562 (if (string-match muse-url-regexp link)
563 (muse-browse-url link)
566 (when (string-match "#" link)
567 (setq anchor (substring link (match-beginning 0))
568 link (if (= (match-beginning 0) 0)
569 ;; If there is an anchor but no link, default
570 ;; to the current page.
572 (substring link 0 (match-beginning 0)))))
574 (setq base-buffer (get-buffer link))
575 (if (and base-buffer (not (buffer-file-name base-buffer)))
576 ;; If file is temporary (no associated file), just switch to
579 (switch-to-buffer-other-window base-buffer)
580 (switch-to-buffer base-buffer))
581 (let ((project (muse-project-of-file)))
583 (muse-project-find-file link project
585 'find-file-other-window))
587 (find-file-other-window link)
588 (find-file link))))))
591 (regexp (concat "^\\W*" (regexp-quote anchor) "\\b"))
593 (goto-char (point-min))
594 (while (and (setq last (re-search-forward regexp nil t))
595 (muse-link-at-point)))
598 (message "Could not find anchor `%s'" anchor)))))))
600 (defun muse-visit-link (link &optional other-window)
601 "Visit the URL or link named by LINK."
602 (let ((visit-link-function
603 (muse-get-keyword :visit-link (cadr (muse-project-of-file)) t)))
604 (if visit-link-function
605 (funcall visit-link-function link other-window)
606 (muse-visit-link-default link other-window))))
609 (defun muse-browse-result (style &optional other-window)
610 "Visit the current page's published result."
612 (list (muse-project-get-applicable-style buffer-file-name
613 (cddr muse-current-project))
615 (setq style (muse-style style))
616 (let* ((output-dir (muse-style-element :path style))
617 (result-path (muse-publish-output-file buffer-file-name output-dir
619 (muse-current-output-style (list :base (car style)
621 (if (not (file-readable-p result-path))
622 (error "Cannot open output file '%s'" result-path)
624 (find-file-other-window result-path)
625 (let ((func (muse-style-element :browser style t)))
627 (funcall func result-path)
628 (message "The %s publishing style does not support browsing."
632 (defun muse-follow-name-at-point (&optional other-window)
633 "Visit the link at point."
635 (let ((link (muse-link-at-point)))
637 (muse-visit-link link other-window)
638 (error "There is no valid link at point"))))
641 (defun muse-follow-name-at-point-other-window ()
642 "Visit the link at point in other window."
644 (muse-follow-name-at-point t))
646 (defun muse-follow-name-at-mouse (event &optional other-window)
647 "Visit the link at point, or yank text if none is found."
651 (cond ((fboundp 'event-window) ; XEmacs
652 (set-buffer (window-buffer (event-window event)))
653 (and (funcall (symbol-function 'event-point) event)
654 (goto-char (funcall (symbol-function 'event-point)
656 ((fboundp 'posn-window) ; Emacs
657 (set-buffer (window-buffer (posn-window (event-start event))))
658 (goto-char (posn-point (event-start event)))))
659 (let ((link (muse-link-at-point)))
661 (muse-visit-link link other-window)
663 ;; Fall back to normal binding for this event
665 (lookup-key (current-global-map) (this-command-keys)))))
667 (defun muse-follow-name-at-mouse-other-window (event)
668 "Visit the link at point"
670 ;; throw away the old window position, since other-window will
672 (select-window (car (cadr event)))
673 (muse-follow-name-at-mouse event t))
676 (defun muse-next-reference ()
677 "Move forward to next Muse link or URL, cycling if necessary."
681 (when (get-text-property (point) 'muse-link)
682 (goto-char (or (next-single-property-change (point) 'muse-link)
685 (setq pos (next-single-property-change (point) 'muse-link))
688 (if (get-text-property (point-min) 'muse-link)
689 (setq pos (point-min))
690 (setq pos (next-single-property-change (point-min) 'muse-link)))))
696 (defun muse-previous-reference ()
697 "Move backward to the next Muse link or URL, cycling if necessary.
698 In case of Emacs x <= 21 and ignoring of intangible properties (see
699 `muse-mode-intangible-links').
701 This function is not entirely accurate, but it's close enough."
706 ;; Hack: The user perceives the two cases of point ("|")
707 ;; position (1) "|[[" and (2) "[[|" or "][|" as "point is at
708 ;; start of link". But in the sense of the function
709 ;; "previous-single-property-change" these two cases are
710 ;; different. The following code aligns these two cases. Emacs
711 ;; 21: If the intangible property is ignored case (2) is more
712 ;; complicate and this hack only solves the problem partially.
714 (when (and (get-text-property (point) 'muse-link)
715 (muse-looking-back "\\[\\|\\]"))
716 (goto-char (or (previous-single-property-change (point) 'muse-link)
719 (when (eq (point) (point-min))
720 (goto-char (point-max)))
722 (setq pos (previous-single-property-change (point) 'muse-link))
725 (if (get-text-property (point-min) 'muse-link)
726 (setq pos (point-min))
727 (setq pos (previous-single-property-change (point-max)
731 (if (get-text-property pos 'muse-link)
733 (goto-char (or (previous-single-property-change pos 'muse-link)
737 (defun muse-what-changed ()
738 "Show the unsaved changes that have been made to the current file."
740 (diff-backup buffer-file-name))
743 ;;; Find text in project pages, or pages referring to the current page
745 (defvar muse-search-history nil)
747 (defun muse-grep (string &optional grep-command-no-shadow)
748 "Grep for STRING in the project directories.
749 GREP-COMMAND if passed will supplant `muse-grep-command'."
750 ;; careful - grep-command leaks into compile, so we call it
751 ;; -no-shadow instead
753 (let* ((str (or grep-command-no-shadow muse-grep-command))
754 (muse-directories (mapcar
758 (dirs (mapconcat (lambda (dir)
759 (shell-quote-argument
760 (expand-file-name dir)))
761 muse-directories " ")))
762 (if (string= dirs "")
763 (muse-display-warning
764 "No directories were found in the current project; aborting search")
765 (while (string-match "%W" str)
766 (setq str (replace-match string t t str)))
767 (while (string-match "%D" str)
768 (setq str (replace-match dirs t t str)))
769 (if (fboundp 'compilation-start)
770 (compilation-start str nil (lambda (&rest args) "*search*")
772 (and (fboundp 'compile-internal)
773 (compile-internal str "No more search hits" "search"
774 nil grep-regexp-alist))))))
777 (defun muse-search-with-command (text)
778 "Search for the given TEXT string in the project directories
779 using the specified command."
781 (list (let ((str (concat muse-grep-command)) pos)
782 (when (string-match "%W" str)
783 (setq pos (match-beginning 0))
784 (unless (featurep 'xemacs)
786 (setq str (replace-match "" t t str)))
787 (read-from-minibuffer "Search command: "
788 (cons str pos) nil nil
789 'muse-search-history))))
790 (muse-grep nil text))
793 (defun muse-search ()
794 "Search for the given TEXT using the default grep command."
796 (muse-grep (read-string "Search: ")))
799 (defun muse-find-backlinks ()
800 "Grep for the current pagename in all the project directories."
802 (muse-grep (muse-page-name)))
805 ;;; Generate an index of all known Muse pages
807 (defun muse-generate-index (&optional as-list exclude-private)
808 "Generate an index of all Muse pages."
809 (let ((index (muse-index-as-string as-list exclude-private)))
810 (with-current-buffer (get-buffer-create "*Muse Index*")
817 "Display an index of all known Muse pages."
819 (message "Generating Muse index...")
820 (let ((project (muse-project)))
821 (with-current-buffer (muse-generate-index)
822 (goto-char (point-min))
824 (setq muse-current-project project)
825 (pop-to-buffer (current-buffer))))
826 (message "Generating Muse index...done"))
828 (defun muse-index-as-string (&optional as-list exclude-private exclude-current)
829 "Generate an index of all Muse pages.
830 If AS-LIST is non-nil, insert a dash and spaces before each item.
831 If EXCLUDE-PRIVATE is non-nil, exclude files that have private permissions.
832 If EXCLUDE-CURRENT is non-nil, exclude the current file from the output."
833 (let ((files (sort (copy-alist (muse-project-file-alist))
836 (string-lessp (car l) (car r)))))))
837 (when (and exclude-current (muse-page-name))
838 (setq files (delete (assoc (muse-page-name) files) files)))
841 (unless (and exclude-private
842 (muse-project-private-p (cdar files)))
843 (insert (if as-list " - " "") "[[" (caar files) "]]\n"))
844 (setq files (cdr files)))
847 ;;; Insert tags interactively on C-c TAB t
849 (defvar muse-tag-history nil
850 "List of recently-entered tags; used by `muse-insert-tag'.
851 If you want a tag to start as the default, you may manually set
852 this variable to a list.")
854 (defvar muse-custom-tags nil
855 "Keep track of any new tags entered in `muse-insert-tag'.
856 If there are (X)HTML tags that you use frequently with that
857 function, you might want to set this manually.")
860 (defun muse-insert-tag (tag)
861 "Insert a tag interactively with a blank line after it."
865 muse-completing-read-function
867 (when muse-tag-history
868 (concat "(default: " (car muse-tag-history) ") ")))
870 (require 'muse-publish)
871 (mapcar 'list (nconc (mapcar 'car muse-publish-markup-tags)
873 nil nil nil 'muse-tag-history
874 (car muse-tag-history))))
876 (setq tag (car muse-tag-history)))
877 (unless (interactive-p)
878 (require 'muse-publish))
879 (let ((tag-entry (assoc tag muse-publish-markup-tags))
881 ;; Add to custom list if no entry exists
883 (add-to-list 'muse-custom-tags tag))
885 (when (nth 2 tag-entry)
886 (setq options (read-string "Option: ")))
887 (unless (equal options "")
888 (setq options (concat " " options)))
889 ;; Insert the tag, closing if necessary
890 (when tag (insert (concat "<" tag options ">")))
891 (when (nth 1 tag-entry)
892 (insert (concat "\n\n</" tag ">\n"))
895 ;;; Muse list edit minor mode
897 (defvar muse-list-edit-minor-mode-map
898 (let ((map (make-sparse-keymap)))
899 (define-key map [(meta return)] 'muse-l-e-m-m-insert-list-item)
900 (define-key map [(control ?>)] 'muse-l-e-m-m-increase-list-item-indent)
901 (define-key map [(control ?<)] 'muse-l-e-m-m-decrease-list-item-indent)
904 "Keymap used by Muse list edit minor mode.")
906 (defvar muse-l-e-m-m-list-item-regexp
907 (concat "^%s\\(\\([^\n" muse-regexp-blank "].*?\\)?::"
908 "\\(?:[" muse-regexp-blank "]+\\|$\\)"
909 "\\|[" muse-regexp-blank "]?[-*+][" muse-regexp-blank "]*"
910 "\\|[" muse-regexp-blank "][0-9]+\\.[" muse-regexp-blank "]*\\)")
911 "Regexp used to match the beginning of a list item.
912 This is used by `muse-list-edit-minor-mode'.
913 The '%s' will be replaced with a whitespace regexp when publishing.")
915 (defun muse-l-e-m-m-insert-list-item ()
916 "Insert a list item at the current point, taking into account
917 your current list type and indentation level."
919 (let ((muse-list-item-regexp muse-l-e-m-m-list-item-regexp))
920 (call-interactively 'muse-insert-list-item)))
922 (defun muse-l-e-m-m-increase-list-item-indent ()
923 "Increase the indentation of the current list item."
925 (let ((muse-list-item-regexp muse-l-e-m-m-list-item-regexp))
926 (call-interactively 'muse-increase-list-item-indentation)))
928 (defun muse-l-e-m-m-decrease-list-item-indent ()
929 "Decrease the indentation of the current list item."
931 (let ((muse-list-item-regexp muse-l-e-m-m-list-item-regexp))
932 (call-interactively 'muse-decrease-list-item-indentation)))
934 (defvar muse-l-e-m-m-data nil
935 "A list of data that was changed by Muse list edit minor mode.")
936 (make-variable-buffer-local 'muse-l-e-m-m-data)
939 (define-minor-mode muse-list-edit-minor-mode
940 "This is a global minor mode for editing files with lists.
941 It is meant to be used with other major modes, and not with Muse mode.
943 Interactively, with no prefix argument, toggle the mode.
944 With universal prefix ARG turn mode on.
945 With zero or negative ARG turn mode off.
947 This minor mode provides the Muse keybindings for editing lists,
948 and support for filling lists properly.
950 It recognizes not only Muse-style lists, which use the \"-\"
951 character or numbers, but also lists that use asterisks or plus
952 signs. This should make the minor mode generally useful.
954 Definition lists and footnotes are also recognized.
956 Note that list items may omit leading spaces, for compatibility
957 with modes that set `left-margin', such as
958 `debian-changelog-mode'.
960 \\{muse-list-edit-minor-mode-map}"
963 :keymap muse-list-edit-minor-mode-map
966 (if (not muse-list-edit-minor-mode)
968 (when muse-l-e-m-m-data
969 (setq adaptive-fill-regexp (cdr (assoc "a-f-r" muse-l-e-m-m-data))
970 paragraph-start (cdr (assoc "p-s" muse-l-e-m-m-data))
971 fill-prefix (cdr (assoc "f-p" muse-l-e-m-m-data)))
972 (setq muse-l-e-m-m-data nil))
974 (unless muse-l-e-m-m-data
975 ;; save previous fill-related data so we can restore it later
976 (setq muse-l-e-m-m-data
977 (list (cons "a-f-r" adaptive-fill-regexp)
978 (cons "p-s" paragraph-start)
979 (cons "f-p" fill-prefix))))
980 ;; make fill work nicely with item lists
981 (let ((regexp (concat "\\s-*\\([-*+]\\|[0-9]+\\.\\)\\s-+"
982 "\\|\\[[0-9]+\\]\\s-*"
983 "\\|.*\\s-*::\\s-+")))
984 (set (make-local-variable 'adaptive-fill-regexp)
985 (concat regexp "\\|\\s-*"))
986 (set (make-local-variable 'paragraph-start)
987 (concat paragraph-start "\\|" regexp)))
988 ;; force fill-prefix to be nil, because if it is a string that has
989 ;; initial spaces, it messes up fill-paragraph's algorithm
990 (set (make-local-variable 'fill-prefix) nil)))
992 (defun turn-on-muse-list-edit-minor-mode ()
993 "Unconditionally turn on Muse list edit minor mode."
994 (muse-list-edit-minor-mode 1))
996 (defun turn-off-muse-list-edit-minor-mode ()
997 "Unconditionally turn off Muse list edit minor mode."
998 (muse-list-edit-minor-mode -1))
1000 ;;; muse-mode.el ends here