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.
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'."
292 (and (not (muse-mode-fill-nobreak-p))
295 (and (looking-at muse-dl-term-regexp)
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))
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 (and (not (get-text-property (if (bobp) (point) (1- (point)))
313 (null (muse-link-at-point)))))
316 (defun muse-mode-choose-mode ()
317 "Turn the proper Emacs Muse related mode on for this file."
318 (let ((project (muse-project-of-file)))
319 (funcall (or (and project (muse-get-keyword :major-mode (cadr project) t))
322 (defun muse-mode-maybe ()
323 "Maybe turn Emacs Muse mode on for this file."
324 (let ((project (muse-project-of-file)))
326 (funcall (or (muse-get-keyword :major-mode (cadr project) t)
329 ;;; Enhanced list editing
331 (defun muse-on-blank-line ()
332 "See if point is on a blank line"
335 (looking-at (concat "[" muse-regexp-blank "]*$"))))
337 (defun muse-get-paragraph-start ()
338 "Return the start of the current paragraph. This function will
339 return nil if there are no prior paragraphs and the beginning of
340 the line if point is on a blank line."
341 (let ((para-start (concat "^[" muse-regexp-blank "]*$")))
342 ;; search back to start of paragraph
345 (if (not (muse-on-blank-line))
346 (re-search-backward para-start nil t)
347 (line-beginning-position))))))
349 (defun muse-insert-thing ()
350 "Prompt for something to insert into the current buffer."
352 (message "Insert:\nl link\nt Muse tag\nu URL")
354 (let ((overriding-local-map muse-insert-map))
355 (setq key (read-key-sequence nil)))
356 (if (commandp (setq cmd (lookup-key muse-insert-map key)))
358 (call-interactively cmd))
359 (message "Not inserting anything"))))
362 (defun muse-insert-list-item ()
363 "Insert a list item at the current point, taking into account
364 your current list type and indentation level."
366 (let ((newitem " - ")
368 (pstart (muse-get-paragraph-start))
369 (list-item (format muse-list-item-regexp
370 (concat "[" muse-regexp-blank "]*"))))
371 ;; search backwards for start of current item
373 (when (re-search-backward list-item pstart t)
374 ;; save the matching item
375 (setq newitem (match-string 0))
376 ;; see what type it is
377 (if (string-match "::" (match-string 0))
378 ;; is a definition, replace the term
379 (setq newitem (concat " "
380 (read-string "Term: ")
382 ;; see if it's a numbered list
383 (when (string-match "[0-9]+" newitem)
384 ;; is numbered, so increment
387 (match-string 0 newitem))))
388 (setq newitem (replace-match
389 (number-to-string itemno)
390 nil nil newitem))))))
391 ;; insert the new item
392 (insert (concat "\n" newitem))))
394 (defun muse-alter-list-item-indentation (operation)
395 "Alter the indentation of the current list item.
396 Valid values of OPERATION are 'increase and 'decrease."
397 (let ((pstart (muse-get-paragraph-start))
398 (list-item (format muse-list-item-regexp
399 (concat "[" muse-regexp-blank "]*")))
400 beg move-func indent)
401 ;; search backwards until start of paragraph to see if we are on a
404 (if (or (progn (goto-char (muse-line-beginning-position))
406 (looking-at list-item))
407 ;; not on item, so search backwards
408 (re-search-backward list-item pstart t))
411 (setq indent (buffer-substring (match-beginning 0)
412 (match-beginning 1)))
413 (muse-forward-list-item (muse-list-item-type (match-string 1))
414 (concat "[" muse-regexp-blank "]*")
417 (narrow-to-region beg (point))
418 (goto-char (point-min))
420 (while (< (point) (point-max))
421 ;; increase or decrease the indentation
423 (cond ((eq operation 'increase)
425 ((eq operation 'decrease)
427 ;; we have enough space, so delete it
428 (delete-region (match-beginning 0)
432 ;; we are not on an item, so warn
433 (message "You are not on a list item.")))))
436 (defun muse-increase-list-item-indentation ()
437 "Increase the indentation of the current list item."
439 (muse-alter-list-item-indentation 'increase))
442 (defun muse-decrease-list-item-indentation ()
443 "Decrease the indentation of the current list item."
445 (muse-alter-list-item-indentation 'decrease))
447 ;;; Support page name completion using pcomplete
449 (defun muse-mode-completions ()
450 "Return a list of possible completions names for this buffer."
451 (let ((project (muse-project-of-file)))
453 (while (pcomplete-here
454 (mapcar 'car (muse-project-file-alist project)))))))
456 (defun muse-mode-current-word ()
460 (skip-chars-backward (concat "^\\[\n" muse-regexp-blank))
461 (narrow-to-region (point) end))
462 (pcomplete-parse-buffer-arguments))))
464 ;;; Navigate/visit links or URLs. Use TAB, S-TAB and RET (or mouse-2).
466 (defun muse-link-at-point (&optional pos)
467 "Return link text if a URL or link is at point."
468 (let ((case-fold-search nil)
469 (inhibit-point-motion-hooks t)
470 (here (or pos (point))))
471 ;; if we are using muse-colors, we can just use link properties to
472 ;; determine whether we are on a link
473 (if (featurep 'muse-colors)
474 (when (get-text-property here 'muse-link)
476 (when (and (not (bobp))
477 (get-text-property (1- here) 'muse-link))
478 (goto-char (or (previous-single-property-change here 'muse-link)
480 (if (looking-at muse-explicit-link-regexp)
482 (goto-char (match-beginning 1))
483 (muse-handle-explicit-link))
484 (muse-handle-implicit-link))))
485 ;; use fallback method to find a link
487 (and (char-after pos)
488 (not (eq (char-syntax (char-after pos)) ?\ ))))
491 ;; check for explicit link here or before point
492 (if (or (looking-at muse-explicit-link-regexp)
494 (re-search-backward "\\[\\[\\|\\]\\]"
495 (muse-line-beginning-position)
497 (string= (or (match-string 0) "") "[[")
498 (looking-at muse-explicit-link-regexp)))
500 (goto-char (match-beginning 1))
501 (muse-handle-explicit-link))
503 ;; check for bare URL or other link type
504 (skip-chars-backward (concat "^'\"<>{}(\n" muse-regexp-blank))
505 (and (looking-at muse-implicit-link-regexp)
506 (muse-handle-implicit-link))))))))
508 (defun muse-make-link (link &optional desc)
509 "Return a link to LINK with DESC as the description."
510 (when (string-match muse-explicit-link-regexp link)
511 (unless desc (setq desc (muse-get-link-desc link)))
512 (setq link (muse-get-link link)))
515 (not (string= desc ""))
516 (not (string= link desc)))
517 (concat "[[" (muse-link-escape link) "][" (muse-link-escape desc) "]]")
518 (concat "[[" (or (muse-link-escape link) "") "]]")))
521 (defun muse-insert-relative-link-to-file ()
522 "Insert a relative link to a file, with optional description, at point."
523 ;; Perhaps the relative location should be configurable, so that the
524 ;; file search would start in the publishing directory and then
525 ;; insert the link relative to the publishing directory
528 (muse-make-link (file-relative-name (read-file-name "Link: "))
529 (read-string "Text: "))))
531 (defcustom muse-insert-url-initial-input "http://"
532 "The string to insert before reading a URL interactively.
533 This is used by the `muse-insert-url' command."
537 (defun muse-insert-url ()
538 "Insert a URL, with optional description, at point."
541 (muse-make-link (read-string "URL: " muse-insert-url-initial-input)
542 (read-string "Text: "))))
545 (defun muse-edit-link-at-point ()
546 "Edit the current link.
547 Do not rename the page originally referred to."
549 (if (muse-link-at-point)
550 (let ((link (muse-link-unescape (muse-get-link)))
551 (desc (muse-link-unescape (muse-get-link-desc))))
555 (read-string "Link: " link)
556 (read-string "Text: " desc)))
558 (error "There is no valid link at point")))
560 (defun muse-visit-link-default (link &optional other-window)
561 "Visit the URL or link named by LINK.
562 If ANCHOR is specified, search for it after opening LINK.
564 This is the default function to call when visiting links; it is
565 used by `muse-visit-link' if you have not specified :visit-link
566 in `muse-project-alist'."
567 (if (string-match muse-url-regexp link)
568 (muse-browse-url link)
571 (when (string-match "#" link)
572 (setq anchor (substring link (match-beginning 0))
573 link (if (= (match-beginning 0) 0)
574 ;; If there is an anchor but no link, default
575 ;; to the current page.
577 (substring link 0 (match-beginning 0)))))
579 (setq base-buffer (get-buffer link))
580 (if (and base-buffer (not (buffer-file-name base-buffer)))
581 ;; If file is temporary (no associated file), just switch to
584 (switch-to-buffer-other-window base-buffer)
585 (switch-to-buffer base-buffer))
586 (let ((project (muse-project-of-file)))
588 (muse-project-find-file link project
590 'find-file-other-window))
592 (find-file-other-window link)
593 (find-file link))))))
596 (regexp (concat "^\\W*" (regexp-quote anchor) "\\b"))
598 (goto-char (point-min))
599 (while (and (setq last (re-search-forward regexp nil t))
600 (muse-link-at-point)))
603 (message "Could not find anchor `%s'" anchor)))))))
605 (defun muse-visit-link (link &optional other-window)
606 "Visit the URL or link named by LINK."
607 (let ((visit-link-function
608 (muse-get-keyword :visit-link (cadr (muse-project-of-file)) t)))
609 (if visit-link-function
610 (funcall visit-link-function link other-window)
611 (muse-visit-link-default link other-window))))
614 (defun muse-browse-result (style &optional other-window)
615 "Visit the current page's published result."
617 (list (muse-project-get-applicable-style buffer-file-name
618 (cddr muse-current-project))
620 (setq style (muse-style style))
621 (muse-project-publish-this-file nil style)
622 (let* ((output-dir (muse-style-element :path style))
623 (output-suffix (muse-style-element :osuffix style))
624 (output-path (muse-publish-output-file buffer-file-name output-dir
626 (target (if output-suffix
627 (concat (muse-path-sans-extension output-path)
630 (muse-current-output-style (list :base (car style)
632 (if (not (file-readable-p target))
633 (error "Cannot open output file '%s'" target)
635 (find-file-other-window target)
636 (let ((func (muse-style-element :browser style t)))
638 (funcall func target)
639 (message "The %s publishing style does not support browsing."
643 (defun muse-follow-name-at-point (&optional other-window)
644 "Visit the link at point."
646 (let ((link (muse-link-at-point)))
648 (muse-visit-link link other-window)
649 (error "There is no valid link at point"))))
652 (defun muse-follow-name-at-point-other-window ()
653 "Visit the link at point in other window."
655 (muse-follow-name-at-point t))
657 (defun muse-follow-name-at-mouse (event &optional other-window)
658 "Visit the link at point, or yank text if none is found."
662 (cond ((fboundp 'event-window) ; XEmacs
663 (set-buffer (window-buffer (event-window event)))
664 (and (funcall (symbol-function 'event-point) event)
665 (goto-char (funcall (symbol-function 'event-point)
667 ((fboundp 'posn-window) ; Emacs
668 (set-buffer (window-buffer (posn-window (event-start event))))
669 (goto-char (posn-point (event-start event)))))
670 (let ((link (muse-link-at-point)))
672 (muse-visit-link link other-window)
674 ;; Fall back to normal binding for this event
676 (lookup-key (current-global-map) (this-command-keys)))))
678 (defun muse-follow-name-at-mouse-other-window (event)
679 "Visit the link at point"
681 ;; throw away the old window position, since other-window will
683 (select-window (car (cadr event)))
684 (muse-follow-name-at-mouse event t))
687 (defun muse-next-reference ()
688 "Move forward to next Muse link or URL, cycling if necessary."
692 (when (get-text-property (point) 'muse-link)
693 (goto-char (or (next-single-property-change (point) 'muse-link)
696 (setq pos (next-single-property-change (point) 'muse-link))
699 (if (get-text-property (point-min) 'muse-link)
700 (setq pos (point-min))
701 (setq pos (next-single-property-change (point-min) 'muse-link)))))
707 (defun muse-previous-reference ()
708 "Move backward to the next Muse link or URL, cycling if necessary.
709 In case of Emacs x <= 21 and ignoring of intangible properties (see
710 `muse-mode-intangible-links').
712 This function is not entirely accurate, but it's close enough."
717 ;; Hack: The user perceives the two cases of point ("|")
718 ;; position (1) "|[[" and (2) "[[|" or "][|" as "point is at
719 ;; start of link". But in the sense of the function
720 ;; "previous-single-property-change" these two cases are
721 ;; different. The following code aligns these two cases. Emacs
722 ;; 21: If the intangible property is ignored case (2) is more
723 ;; complicate and this hack only solves the problem partially.
725 (when (and (get-text-property (point) 'muse-link)
726 (muse-looking-back "\\[\\|\\]"))
727 (goto-char (or (previous-single-property-change (point) 'muse-link)
730 (when (eq (point) (point-min))
731 (goto-char (point-max)))
733 (setq pos (previous-single-property-change (point) 'muse-link))
736 (if (get-text-property (point-min) 'muse-link)
737 (setq pos (point-min))
738 (setq pos (previous-single-property-change (point-max)
742 (if (get-text-property pos 'muse-link)
744 (goto-char (or (previous-single-property-change pos 'muse-link)
748 (defun muse-what-changed ()
749 "Show the unsaved changes that have been made to the current file."
751 (diff-backup buffer-file-name))
754 ;;; Find text in project pages, or pages referring to the current page
756 (defvar muse-search-history nil)
758 (defun muse-grep (string &optional grep-command-no-shadow)
759 "Grep for STRING in the project directories.
760 GREP-COMMAND if passed will supplant `muse-grep-command'."
761 ;; careful - grep-command leaks into compile, so we call it
762 ;; -no-shadow instead
764 (let* ((str (or grep-command-no-shadow muse-grep-command))
765 (muse-directories (mapcar
769 (dirs (mapconcat (lambda (dir)
770 (shell-quote-argument
771 (expand-file-name dir)))
772 muse-directories " ")))
773 (if (string= dirs "")
774 (muse-display-warning
775 "No directories were found in the current project; aborting search")
776 (while (string-match "%W" str)
777 (setq str (replace-match string t t str)))
778 (while (string-match "%D" str)
779 (setq str (replace-match dirs t t str)))
780 (if (fboundp 'compilation-start)
781 (compilation-start str nil (lambda (&rest args) "*search*")
783 (and (fboundp 'compile-internal)
784 (compile-internal str "No more search hits" "search"
785 nil grep-regexp-alist))))))
788 (defun muse-search-with-command (text)
789 "Search for the given TEXT string in the project directories
790 using the specified command."
792 (list (let ((str (concat muse-grep-command)) pos)
793 (when (string-match "%W" str)
794 (setq pos (match-beginning 0))
795 (unless (featurep 'xemacs)
797 (setq str (replace-match "" t t str)))
798 (read-from-minibuffer "Search command: "
799 (cons str pos) nil nil
800 'muse-search-history))))
801 (muse-grep nil text))
804 (defun muse-search ()
805 "Search for the given TEXT using the default grep command."
807 (muse-grep (read-string "Search: ")))
810 (defun muse-find-backlinks ()
811 "Grep for the current pagename in all the project directories."
813 (muse-grep (muse-page-name)))
816 ;;; Generate an index of all known Muse pages
818 (defun muse-generate-index (&optional as-list exclude-private)
819 "Generate an index of all Muse pages."
820 (let ((index (muse-index-as-string as-list exclude-private)))
821 (with-current-buffer (get-buffer-create "*Muse Index*")
828 "Display an index of all known Muse pages."
830 (message "Generating Muse index...")
831 (let ((project (muse-project)))
832 (with-current-buffer (muse-generate-index)
833 (goto-char (point-min))
835 (setq muse-current-project project)
836 (pop-to-buffer (current-buffer))))
837 (message "Generating Muse index...done"))
839 (defun muse-index-as-string (&optional as-list exclude-private exclude-current)
840 "Generate an index of all Muse pages.
841 If AS-LIST is non-nil, insert a dash and spaces before each item.
842 If EXCLUDE-PRIVATE is non-nil, exclude files that have private permissions.
843 If EXCLUDE-CURRENT is non-nil, exclude the current file from the output."
844 (let ((files (sort (copy-alist (muse-project-file-alist))
847 (string-lessp (car l) (car r)))))))
848 (when (and exclude-current (muse-page-name))
849 (setq files (delete (assoc (muse-page-name) files) files)))
852 (unless (and exclude-private
853 (muse-project-private-p (cdar files)))
854 (insert (if as-list " - " "") "[[" (caar files) "]]\n"))
855 (setq files (cdr files)))
858 ;;; Insert tags interactively on C-c TAB t
860 (defvar muse-tag-history nil
861 "List of recently-entered tags; used by `muse-insert-tag'.
862 If you want a tag to start as the default, you may manually set
863 this variable to a list.")
865 (defvar muse-custom-tags nil
866 "Keep track of any new tags entered in `muse-insert-tag'.
867 If there are (X)HTML tags that you use frequently with that
868 function, you might want to set this manually.")
871 (defun muse-insert-tag (tag)
872 "Insert a tag interactively with a blank line after it."
876 muse-completing-read-function
878 (when muse-tag-history
879 (concat "(default: " (car muse-tag-history) ") ")))
881 (require 'muse-publish)
882 (mapcar 'list (nconc (mapcar 'car muse-publish-markup-tags)
884 nil nil nil 'muse-tag-history
885 (car muse-tag-history))))
887 (setq tag (car muse-tag-history)))
888 (unless (interactive-p)
889 (require 'muse-publish))
890 (let ((tag-entry (assoc tag muse-publish-markup-tags))
892 ;; Add to custom list if no entry exists
894 (add-to-list 'muse-custom-tags tag))
896 (when (nth 2 tag-entry)
897 (setq options (read-string "Option: ")))
898 (unless (equal options "")
899 (setq options (concat " " options)))
900 ;; Insert the tag, closing if necessary
901 (when tag (insert (concat "<" tag options ">")))
902 (when (nth 1 tag-entry)
903 (insert (concat "\n\n</" tag ">\n"))
906 ;;; Muse list edit minor mode
908 (defvar muse-list-edit-minor-mode-map
909 (let ((map (make-sparse-keymap)))
910 (define-key map [(meta return)] 'muse-l-e-m-m-insert-list-item)
911 (define-key map [(control ?>)] 'muse-l-e-m-m-increase-list-item-indent)
912 (define-key map [(control ?<)] 'muse-l-e-m-m-decrease-list-item-indent)
915 "Keymap used by Muse list edit minor mode.")
917 (defvar muse-l-e-m-m-list-item-regexp
918 (concat "^%s\\(\\([^\n" muse-regexp-blank "].*?\\)?::"
919 "\\(?:[" muse-regexp-blank "]+\\|$\\)"
920 "\\|[" muse-regexp-blank "]?[-*+][" muse-regexp-blank "]*"
921 "\\|[" muse-regexp-blank "][0-9]+\\.[" muse-regexp-blank "]*\\)")
922 "Regexp used to match the beginning of a list item.
923 This is used by `muse-list-edit-minor-mode'.
924 The '%s' will be replaced with a whitespace regexp when publishing.")
926 (defun muse-l-e-m-m-insert-list-item ()
927 "Insert a list item at the current point, taking into account
928 your current list type and indentation level."
930 (let ((muse-list-item-regexp muse-l-e-m-m-list-item-regexp))
931 (call-interactively 'muse-insert-list-item)))
933 (defun muse-l-e-m-m-increase-list-item-indent ()
934 "Increase the indentation of the current list item."
936 (let ((muse-list-item-regexp muse-l-e-m-m-list-item-regexp))
937 (call-interactively 'muse-increase-list-item-indentation)))
939 (defun muse-l-e-m-m-decrease-list-item-indent ()
940 "Decrease the indentation of the current list item."
942 (let ((muse-list-item-regexp muse-l-e-m-m-list-item-regexp))
943 (call-interactively 'muse-decrease-list-item-indentation)))
945 (defvar muse-l-e-m-m-data nil
946 "A list of data that was changed by Muse list edit minor mode.")
947 (make-variable-buffer-local 'muse-l-e-m-m-data)
950 (define-minor-mode muse-list-edit-minor-mode
951 "This is a global minor mode for editing files with lists.
952 It is meant to be used with other major modes, and not with Muse mode.
954 Interactively, with no prefix argument, toggle the mode.
955 With universal prefix ARG turn mode on.
956 With zero or negative ARG turn mode off.
958 This minor mode provides the Muse keybindings for editing lists,
959 and support for filling lists properly.
961 It recognizes not only Muse-style lists, which use the \"-\"
962 character or numbers, but also lists that use asterisks or plus
963 signs. This should make the minor mode generally useful.
965 Definition lists and footnotes are also recognized.
967 Note that list items may omit leading spaces, for compatibility
968 with modes that set `left-margin', such as
969 `debian-changelog-mode'.
971 \\{muse-list-edit-minor-mode-map}"
974 :keymap muse-list-edit-minor-mode-map
977 (if (not muse-list-edit-minor-mode)
979 (when muse-l-e-m-m-data
980 (setq adaptive-fill-regexp (cdr (assoc "a-f-r" muse-l-e-m-m-data))
981 paragraph-start (cdr (assoc "p-s" muse-l-e-m-m-data))
982 fill-prefix (cdr (assoc "f-p" muse-l-e-m-m-data)))
983 (setq muse-l-e-m-m-data nil))
985 (unless muse-l-e-m-m-data
986 ;; save previous fill-related data so we can restore it later
987 (setq muse-l-e-m-m-data
988 (list (cons "a-f-r" adaptive-fill-regexp)
989 (cons "p-s" paragraph-start)
990 (cons "f-p" fill-prefix))))
991 ;; make fill work nicely with item lists
992 (let ((regexp (concat "\\s-*\\([-*+]\\|[0-9]+\\.\\)\\s-+"
993 "\\|\\[[0-9]+\\]\\s-*"
994 "\\|.*\\s-*::\\s-+")))
995 (set (make-local-variable 'adaptive-fill-regexp)
996 (concat regexp "\\|\\s-*"))
997 (set (make-local-variable 'paragraph-start)
998 (concat paragraph-start "\\|" regexp)))
999 ;; force fill-prefix to be nil, because if it is a string that has
1000 ;; initial spaces, it messes up fill-paragraph's algorithm
1001 (set (make-local-variable 'fill-prefix) nil)))
1003 (defun turn-on-muse-list-edit-minor-mode ()
1004 "Unconditionally turn on Muse list edit minor mode."
1005 (muse-list-edit-minor-mode 1))
1007 (defun turn-off-muse-list-edit-minor-mode ()
1008 "Unconditionally turn off Muse list edit minor mode."
1009 (muse-list-edit-minor-mode -1))
1011 ;;; muse-mode.el ends here