Don't make muse-list-edit-minor-mode global, just buffer-local
[muse-el.git] / lisp / muse-mode.el
blob8460f47b6bddb955b63851916b55c32ffb8c4b6e
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 2, 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 ;;; Code:
43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
45 ;; Emacs Muse Major Mode
47 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
49 (provide 'muse-mode)
51 (require 'muse)
52 (require 'muse-regexps)
53 (require 'muse-project)
55 (autoload 'muse-use-font-lock "muse-colors")
56 (autoload 'muse-publish-this-file "muse-publish")
57 (autoload 'muse-publish-get-style "muse-publish")
58 (autoload 'muse-publish-output-file "muse-publish")
60 (require 'derived)
61 (eval-when-compile
62 (condition-case nil
63 (require 'pcomplete) ; load if available
64 (error nil)))
66 ;;; Options:
68 (defgroup muse-mode nil
69 "Options controlling the behavior of the Muse editing Mode."
70 :group 'muse)
72 (defcustom muse-mode-highlight-p t
73 "If non-nil, highlight the content of Muse buffers."
74 :type 'boolean
75 :require 'muse-colors
76 :group 'muse-mode)
78 (defcustom muse-mode-auto-p nil
79 "If non-nil, automagically determine when Muse mode should be activated."
80 :type 'boolean
81 :set (function
82 (lambda (sym value)
83 (if value
84 (add-hook 'find-file-hooks 'muse-mode-maybe)
85 (remove-hook 'find-file-hooks 'muse-mode-maybe))
86 (set sym value)))
87 :group 'muse-mode)
89 (defun muse-mode-maybe-after-init ()
90 (when muse-mode-auto-p
91 (add-hook 'find-file-hooks 'muse-mode-maybe)))
93 ;; If the user sets this value in their init file, make sure that
94 ;; it takes effect
95 (add-hook 'after-init-hook 'muse-mode-maybe-after-init)
97 (defcustom muse-mode-intangible-links nil
98 "If non-nil, use the intangible property on links.
99 This can cause problems with flyspell (and potentially fill-mode),
100 so only enable this if you don't use either of these."
101 :type 'boolean
102 :group 'muse-mode)
104 (defcustom muse-mode-hook nil
105 "A hook that is run when Muse mode is entered."
106 :type 'hook
107 :options '(flyspell-mode footnote-mode turn-on-auto-fill
108 highlight-changes-mode)
109 :group 'muse-mode)
111 (defcustom muse-grep-command
112 "find %D -type f ! -name '*~' | xargs -I {} echo \\\"{}\\\" | xargs egrep -n -e \"%W\""
113 "The command to use when grepping for backlinks and other
114 searches through the muse projects. The string %D is replaced by
115 the directories from muse-project-alist, space-separated. The
116 string %W is replaced with the name of the muse page or whatever
117 else you are searching for. This command has been modified to
118 handle spaces in filenames, which were giving egrep a problem.
120 Note: We highly recommend using glimpse to search large projects.
121 To use glimpse, install and edit a file called .glimpse_exclude
122 in your home directory. Put a list of glob patterns in that file
123 to exclude Emacs backup files, etc. Then, run the indexer using:
125 glimpseindex -o <list of Wiki directories>
127 Once that's completed, customize this variable to have the
128 following value:
130 glimpse -nyi \"%W\"
132 Your searches will go much, much faster, especially for very
133 large projects. Don't forget to add a user cronjob to update the
134 index at intervals."
135 :type 'string
136 :group 'muse-mode)
138 (defvar muse-insert-map
139 (let ((map (make-sparse-keymap)))
140 (define-key map "l" 'muse-insert-relative-link-to-file)
141 (define-key map "t" 'muse-insert-tag)
142 (define-key map "u" 'muse-insert-url)
144 map))
146 ;;; Muse mode
148 (defvar muse-mode-map
149 (let ((map (make-sparse-keymap)))
150 (define-key map [(control ?c) (control ?a)] 'muse-index)
151 (define-key map [(control ?c) (control ?e)] 'muse-edit-link-at-point)
152 (define-key map [(control ?c) (control ?l)] 'font-lock-mode)
153 (define-key map [(control ?c) (control ?t)]
154 'muse-project-publish-this-file)
155 (define-key map [(control ?c) (control ?T)] 'muse-publish-this-file)
156 (define-key map [(control ?c) (meta control ?t)] 'muse-publish-this-file)
157 (define-key map [(control ?c) (control ?v)] 'muse-browse-result)
159 (define-key map [(control ?c) ?=] 'muse-what-changed)
161 (define-key map [tab] 'muse-next-reference)
162 (define-key map [(control ?i)] 'muse-next-reference)
164 (if (featurep 'xemacs)
165 (progn
166 (define-key map [(button2)] 'muse-follow-name-at-mouse)
167 (define-key map [(shift button2)]
168 'muse-follow-name-at-mouse-other-window))
169 (define-key map [(shift control ?m)]
170 'muse-follow-name-at-point-other-window)
171 (define-key map [mouse-2] 'muse-follow-name-at-mouse)
172 (define-key map [(shift mouse-2)]
173 'muse-follow-name-at-mouse-other-window))
175 (define-key map [(shift tab)] 'muse-previous-reference)
176 (unless (featurep 'xemacs)
177 (define-key map [(shift iso-lefttab)] 'muse-previous-reference)
178 (define-key map [(shift control ?i)] 'muse-previous-reference))
180 (define-key map [(control ?c) (control ?f)] 'muse-project-find-file)
181 (define-key map [(control ?c) (control ?p)] 'muse-project-publish)
183 (define-key map [(control ?c) (control ?i)] 'muse-insert-thing)
184 (define-key map [(control ?c) tab] 'muse-insert-thing)
186 ;; Searching functions
187 (define-key map [(control ?c) (control ?b)] 'muse-find-backlinks)
188 (define-key map [(control ?c) (control ?s)] 'muse-search)
190 ;; Enhanced list functions
191 (define-key map [(meta return)] 'muse-insert-list-item)
192 (define-key map [(control ?>)] 'muse-increase-list-item-indentation)
193 (define-key map [(control ?<)] 'muse-decrease-list-item-indentation)
195 (when (featurep 'pcomplete)
196 (define-key map [(meta tab)] 'pcomplete)
197 (define-key map [(meta control ?i)] 'pcomplete))
199 map)
200 "Keymap used by Emacs Muse mode.")
202 ;;;###autoload
203 (define-derived-mode muse-mode text-mode "Muse"
204 "Muse is an Emacs mode for authoring and publishing documents.
205 \\{muse-mode-map}"
206 ;; Since we're not inheriting from normal-mode, we need to
207 ;; explicitly run file variables.
208 (condition-case err
209 (hack-local-variables)
210 (error (message "File local-variables error: %s"
211 (prin1-to-string err))))
212 ;; Avoid lock-up caused by use of the 'intangible' text property
213 ;; with flyspell.
214 (unless muse-mode-intangible-links
215 (set (make-local-variable 'inhibit-point-motion-hooks) t))
216 (setq muse-current-project (muse-project-of-file))
217 (muse-project-set-variables)
218 ;; Make fill not split up links
219 (when (boundp 'fill-nobreak-predicate)
220 (make-local-variable 'fill-nobreak-predicate)
221 ;; Work around annoying inconsistency in fill handling between
222 ;; Emacs 21 and 22.
223 (if (< emacs-major-version 22)
224 (setq fill-nobreak-predicate 'muse-mode-fill-nobreak-p)
225 (add-to-list 'fill-nobreak-predicate
226 'muse-mode-fill-nobreak-p)))
227 ;; Make fill work nicely with item lists
228 (let ((regexp (concat "\\s-+\\(-\\|[0-9]+\\.\\)\\s-+"
229 "\\|\\[[0-9]+\\]\\s-*"
230 "\\|.*\\s-*::\\s-+")))
231 (set (make-local-variable 'adaptive-fill-regexp)
232 (concat regexp "\\|\\s-*"))
233 (set (make-local-variable 'paragraph-start)
234 (concat paragraph-start "\\|" regexp)))
235 ;; Comment syntax is `; comment'
236 (set (make-local-variable 'comment-start)
237 "; ")
238 (set (make-local-variable 'comment-start-skip)
239 "^;\\s-+")
240 ;; If we're using Emacs21, this makes flyspell work like it should
241 (when (boundp 'flyspell-generic-check-word-p)
242 (set (make-local-variable 'flyspell-generic-check-word-p)
243 'muse-mode-flyspell-p))
244 ;; If pcomplete is available, set it up
245 (when (featurep 'pcomplete)
246 (set (make-local-variable 'pcomplete-default-completion-function)
247 'muse-mode-completions)
248 (set (make-local-variable 'pcomplete-command-completion-function)
249 'muse-mode-completions)
250 (set (make-local-variable 'pcomplete-parse-arguments-function)
251 'muse-mode-current-word))
252 ;; Initialize any auto-generated variables
253 (run-hooks 'muse-update-values-hook)
254 (when muse-mode-highlight-p
255 (muse-use-font-lock)))
257 (put 'muse-mode
258 'flyspell-mode-predicate
259 'muse-mode-flyspell-p)
261 (defun muse-mode-fill-nobreak-p ()
262 "Return nil if we should allow a fill to occur at point.
263 Otherwise return non-nil.
265 This is used to keep long explicit links from being mangled by
266 fill mode."
267 (save-excursion
268 (save-match-data
269 (and (re-search-backward "\\[\\[\\|\\]\\]"
270 (line-beginning-position) t)
271 (string= (or (match-string 0) "")
272 "[[")))))
274 (defun muse-mode-flyspell-p ()
275 "Return non-nil if we should allow spell-checking to occur at point.
276 Otherwise return nil.
278 This is used to keep links from being improperly colorized by flyspell."
279 (and (not (get-text-property (if (bobp) (point) (1- (point)))
280 'muse-link))
281 (save-match-data
282 (null (muse-link-at-point)))))
284 ;;;###autoload
285 (defun muse-mode-choose-mode ()
286 "Turn the proper Emacs Muse related mode on for this file."
287 (let ((project (muse-project-of-file)))
288 (funcall (or (and project (muse-get-keyword :major-mode (cadr project) t))
289 'muse-mode))))
291 (defun muse-mode-maybe ()
292 "Maybe turn Emacs Muse mode on for this file."
293 (let ((project (muse-project-of-file)))
294 (and project
295 (funcall (or (muse-get-keyword :major-mode (cadr project) t)
296 'muse-mode)))))
298 ;;; Enhanced list editing
300 (defun muse-on-blank-line ()
301 "See if point is on a blank line"
302 (save-excursion
303 (beginning-of-line)
304 (looking-at (concat "[" muse-regexp-blank "]*$"))))
306 (defun muse-get-paragraph-start ()
307 "Return the start of the current paragraph. This function will
308 return nil if there are no prior paragraphs and the beginning of
309 the line if point is on a blank line."
310 (let ((para-start (concat "^[" muse-regexp-blank "]*$")))
311 ;; search back to start of paragraph
312 (save-excursion
313 (save-match-data
314 (if (not (muse-on-blank-line))
315 (re-search-backward para-start nil t)
316 (line-beginning-position))))))
318 (defun muse-insert-thing ()
319 "Prompt for something to insert into the current buffer."
320 (interactive)
321 (message "Insert:\nl link\nt Muse tag\nu URL")
322 (let (key cmd)
323 (let ((overriding-local-map muse-insert-map))
324 (setq key (read-key-sequence nil)))
325 (if (commandp (setq cmd (lookup-key muse-insert-map key)))
326 (progn (message "")
327 (call-interactively cmd))
328 (message "Not inserting anything"))))
330 ;;;###autoload
331 (defun muse-insert-list-item ()
332 "Insert a list item at the current point, taking into account
333 your current list type and indentation level."
334 (interactive)
335 (let ((newitem " - ")
336 (itemno nil)
337 (pstart (muse-get-paragraph-start))
338 (list-item (format muse-list-item-regexp
339 (concat "[" muse-regexp-blank "]*"))))
340 ;; search backwards for start of current item
341 (save-excursion
342 (when (re-search-backward list-item pstart t)
343 ;; save the matching item
344 (setq newitem (match-string 0))
345 ;; see what type it is
346 (if (string-match "::" (match-string 0))
347 ;; is a definition, replace the term
348 (setq newitem (concat " "
349 (read-string "Term: ")
350 " :: "))
351 ;; see if it's a numbered list
352 (when (string-match "[0-9]+" newitem)
353 ;; is numbered, so increment
354 (setq itemno (1+
355 (string-to-number
356 (match-string 0 newitem))))
357 (setq newitem (replace-match
358 (number-to-string itemno)
359 nil nil newitem))))))
360 ;; insert the new item
361 (insert (concat "\n" newitem))))
363 (defun muse-alter-list-item-indentation (operation)
364 "Alter the indentation of the current list item.
365 Valid values of OPERATION are 'increase and 'decrease."
366 (let ((pstart (muse-get-paragraph-start))
367 (list-item (format muse-list-item-regexp
368 (concat "[" muse-regexp-blank "]*")))
369 beg move-func indent)
370 ;; search backwards until start of paragraph to see if we are on a
371 ;; current item
372 (save-excursion
373 (if (or (progn (goto-char (muse-line-beginning-position))
374 ;; we are on an item
375 (looking-at list-item))
376 ;; not on item, so search backwards
377 (re-search-backward list-item pstart t))
378 (let ((beg (point)))
379 ;; we are on an item
380 (setq indent (buffer-substring (match-beginning 0)
381 (match-beginning 1)))
382 (muse-forward-list-item (muse-list-item-type (match-string 1))
383 (concat "[" muse-regexp-blank "]*")
385 (save-restriction
386 (narrow-to-region beg (point))
387 (goto-char (point-min))
388 (let ((halt nil))
389 (while (< (point) (point-max))
390 ;; increase or decrease the indentation
391 (unless halt
392 (cond ((eq operation 'increase)
393 (insert " "))
394 ((eq operation 'decrease)
395 (if (looking-at " ")
396 ;; we have enough space, so delete it
397 (delete-region (match-beginning 0)
398 (match-end 0))
399 (setq halt t)))))
400 (forward-line 1)))))
401 ;; we are not on an item, so warn
402 (message "You are not on a list item.")))))
404 ;;;###autoload
405 (defun muse-increase-list-item-indentation ()
406 "Increase the indentation of the current list item."
407 (interactive)
408 (muse-alter-list-item-indentation 'increase))
410 ;;;###autoload
411 (defun muse-decrease-list-item-indentation ()
412 "Decrease the indentation of the current list item."
413 (interactive)
414 (muse-alter-list-item-indentation 'decrease))
416 ;;; Support page name completion using pcomplete
418 (defun muse-mode-completions ()
419 "Return a list of possible completions names for this buffer."
420 (let ((project (muse-project-of-file)))
421 (if project
422 (while (pcomplete-here
423 (mapcar 'car (muse-project-file-alist project)))))))
425 (defun muse-mode-current-word ()
426 (let ((end (point)))
427 (save-excursion
428 (save-restriction
429 (skip-chars-backward (concat "^\\[\n" muse-regexp-blank))
430 (narrow-to-region (point) end))
431 (pcomplete-parse-buffer-arguments))))
433 ;;; Navigate/visit links or URLs. Use TAB, S-TAB and RET (or mouse-2).
435 (defun muse-link-at-point (&optional pos)
436 "Return link text if a URL or link is at point."
437 (let ((case-fold-search nil)
438 (inhibit-point-motion-hooks t)
439 (here (or pos (point))))
440 ;; if we are using muse-colors, we can just use link properties to
441 ;; determine whether we are on a link
442 (if (featurep 'muse-colors)
443 (when (get-text-property here 'muse-link)
444 (save-excursion
445 (when (and (not (bobp))
446 (get-text-property (1- here) 'muse-link))
447 (goto-char (or (previous-single-property-change here 'muse-link)
448 (point-min))))
449 (if (looking-at muse-explicit-link-regexp)
450 (progn
451 (goto-char (match-beginning 1))
452 (muse-handle-explicit-link))
453 (muse-handle-implicit-link))))
454 ;; use fallback method to find a link
455 (when (or (null pos)
456 (and (char-after pos)
457 (not (eq (char-syntax (char-after pos)) ?\ ))))
458 (save-excursion
459 (goto-char here)
460 ;; check for explicit link here or before point
461 (if (or (looking-at muse-explicit-link-regexp)
462 (and
463 (re-search-backward "\\[\\[\\|\\]\\]"
464 (muse-line-beginning-position)
466 (string= (or (match-string 0) "") "[[")
467 (looking-at muse-explicit-link-regexp)))
468 (progn
469 (goto-char (match-beginning 1))
470 (muse-handle-explicit-link))
471 (goto-char here)
472 ;; check for bare URL or other link type
473 (skip-chars-backward (concat "^'\"<>{}(\n" muse-regexp-blank))
474 (and (looking-at muse-implicit-link-regexp)
475 (muse-handle-implicit-link))))))))
477 (defun muse-make-link (link &optional desc)
478 "Return a link to LINK with DESC as the description."
479 (when (string-match muse-explicit-link-regexp link)
480 (unless desc (setq desc (muse-get-link-desc link)))
481 (setq link (muse-get-link link)))
482 (if (and desc
483 link
484 (not (string= desc ""))
485 (not (string= link desc)))
486 (concat "[[" (muse-link-escape link) "][" (muse-link-escape desc) "]]")
487 (concat "[[" (or (muse-link-escape link) "") "]]")))
489 ;;;###autoload
490 (defun muse-insert-relative-link-to-file ()
491 "Insert a relative link to a file, with optional description, at point."
492 ;; Perhaps the relative location should be configurable, so that the
493 ;; file search would start in the publishing directory and then
494 ;; insert the link relative to the publishing directory
495 (interactive)
496 (insert
497 (muse-make-link (file-relative-name (read-file-name "Link: "))
498 (read-string "Text: "))))
500 (defun muse-insert-url ()
501 "Insert a URL, with optional description, at point."
502 (interactive)
503 (insert
504 (muse-make-link (read-string "URL: ")
505 (read-string "Text: "))))
507 ;;;###autoload
508 (defun muse-edit-link-at-point ()
509 "Edit the current link.
510 Do not rename the page originally referred to."
511 (interactive)
512 (if (muse-link-at-point)
513 (let ((link (muse-link-unescape (muse-get-link)))
514 (desc (muse-link-unescape (muse-get-link-desc))))
515 (replace-match
516 (save-match-data
517 (muse-make-link
518 (read-string "Link: " link)
519 (read-string "Text: " desc)))
520 t t))
521 (error "There is no valid link at point")))
523 (defun muse-visit-link-default (link &optional other-window)
524 "Visit the URL or link named by LINK.
525 If ANCHOR is specified, search for it after opening LINK.
527 This is the default function to call when visiting links; it is
528 used by `muse-visit-link' if you have not specified :visit-link
529 in `muse-project-alist'."
530 (if (string-match muse-url-regexp link)
531 (muse-browse-url link)
532 (let (anchor
533 base-buffer)
534 (when (string-match "#" link)
535 (setq anchor (substring link (match-beginning 0))
536 link (if (= (match-beginning 0) 0)
537 ;; If there is an anchor but no link, default
538 ;; to the current page.
540 (substring link 0 (match-beginning 0)))))
541 (when link
542 (setq base-buffer (get-buffer link))
543 (if (and base-buffer (not (buffer-file-name base-buffer)))
544 ;; If file is temporary (no associated file), just switch to
545 ;; the buffer
546 (if other-window
547 (switch-to-buffer-other-window base-buffer)
548 (switch-to-buffer base-buffer))
549 (let ((project (muse-project-of-file)))
550 (if project
551 (muse-project-find-file link project
552 (and other-window
553 'find-file-other-window))
554 (if other-window
555 (find-file-other-window link)
556 (find-file link))))))
557 (when anchor
558 (let ((pos (point)))
559 (goto-char (point-min))
560 (unless (re-search-forward (concat "^\\W*" (regexp-quote anchor)
561 "\\b")
562 nil t)
563 (goto-char pos)))))))
565 (defun muse-visit-link (link &optional other-window)
566 "Visit the URL or link named by LINK."
567 (let ((visit-link-function
568 (muse-get-keyword :visit-link (cadr (muse-project-of-file)) t)))
569 (if visit-link-function
570 (funcall visit-link-function link other-window)
571 (muse-visit-link-default link other-window))))
573 ;;;###autoload
574 (defun muse-browse-result (style &optional other-window)
575 "Visit the current page's published result."
576 (interactive
577 (list (muse-project-get-applicable-style buffer-file-name
578 (cddr muse-current-project))
579 current-prefix-arg))
580 (setq style (muse-style style))
581 (let* ((output-dir (muse-style-element :path style))
582 (result-path (muse-publish-output-file buffer-file-name output-dir
583 style))
584 (muse-current-output-style (list :base (car style)
585 :path output-dir)))
586 (if (not (file-readable-p result-path))
587 (error "Cannot open output file '%s'" result-path)
588 (if other-window
589 (find-file-other-window result-path)
590 (let ((func (muse-style-element :browser style t)))
591 (if func
592 (funcall func result-path)
593 (message "The %s publishing style does not support browsing."
594 style)))))))
596 ;;;###autoload
597 (defun muse-follow-name-at-point (&optional other-window)
598 "Visit the link at point."
599 (interactive "P")
600 (let ((link (muse-link-at-point)))
601 (if link
602 (muse-visit-link link other-window)
603 (error "There is no valid link at point"))))
605 ;;;###autoload
606 (defun muse-follow-name-at-point-other-window ()
607 "Visit the link at point in other window."
608 (interactive)
609 (muse-follow-name-at-point t))
611 (defun muse-follow-name-at-mouse (event &optional other-window)
612 "Visit the link at point, or yank text if none is found."
613 (interactive "eN")
614 (unless
615 (save-excursion
616 (cond ((fboundp 'event-window) ; XEmacs
617 (set-buffer (window-buffer (event-window event)))
618 (and (funcall (symbol-function 'event-point) event)
619 (goto-char (funcall (symbol-function 'event-point)
620 event))))
621 ((fboundp 'posn-window) ; Emacs
622 (set-buffer (window-buffer (posn-window (event-start event))))
623 (goto-char (posn-point (event-start event)))))
624 (let ((link (muse-link-at-point)))
625 (when link
626 (muse-visit-link link other-window)
627 t)))
628 ;; Fall back to normal binding for this event
629 (call-interactively
630 (lookup-key (current-global-map) (this-command-keys)))))
632 (defun muse-follow-name-at-mouse-other-window (event)
633 "Visit the link at point"
634 (interactive "e")
635 ;; throw away the old window position, since other-window will
636 ;; change it anyway
637 (select-window (car (cadr event)))
638 (muse-follow-name-at-mouse event t))
640 ;;;###autoload
641 (defun muse-next-reference ()
642 "Move forward to next Muse link or URL, cycling if necessary."
643 (interactive)
644 (let ((pos))
645 (save-excursion
646 (when (get-text-property (point) 'muse-link)
647 (goto-char (or (next-single-property-change (point) 'muse-link)
648 (point-max))))
650 (setq pos (next-single-property-change (point) 'muse-link))
652 (when (not pos)
653 (if (get-text-property (point-min) 'muse-link)
654 (setq pos (point-min))
655 (setq pos (next-single-property-change (point-min) 'muse-link)))))
657 (when pos
658 (goto-char pos))))
660 ;;;###autoload
661 (defun muse-previous-reference ()
662 "Move backward to the next Muse link or URL, cycling if necessary.
663 In case of Emacs x <= 21 and ignoring of intangible properties (see
664 `muse-mode-intangible-links').
666 This function is not entirely accurate, but it's close enough."
667 (interactive)
668 (let ((pos))
669 (save-excursion
671 ;; Hack: The user perceives the two cases of point ("|")
672 ;; position (1) "|[[" and (2) "[[|" or "][|" as "point is at
673 ;; start of link". But in the sense of the function
674 ;; "previous-single-property-change" these two cases are
675 ;; different. The following code aligns these two cases. Emacs
676 ;; 21: If the intangible property is ignored case (2) is more
677 ;; complicate and this hack only solves the problem partially.
679 (when (and (get-text-property (point) 'muse-link)
680 (muse-looking-back "\\[\\|\\]"))
681 (goto-char (or (previous-single-property-change (point) 'muse-link)
682 (point-min))))
684 (when (eq (point) (point-min))
685 (goto-char (point-max)))
687 (setq pos (previous-single-property-change (point) 'muse-link))
689 (when (not pos)
690 (if (get-text-property (point-min) 'muse-link)
691 (setq pos (point-min))
692 (setq pos (previous-single-property-change (point-max)
693 'muse-link)))))
695 (when pos
696 (if (get-text-property pos 'muse-link)
697 (goto-char pos)
698 (goto-char (or (previous-single-property-change pos 'muse-link)
699 (point-min)))))))
701 ;;;###autoload
702 (defun muse-what-changed ()
703 "Show the unsaved changes that have been made to the current file."
704 (interactive)
705 (diff-backup buffer-file-name))
708 ;;; Find text in project pages, or pages referring to the current page
710 (defvar muse-search-history nil)
712 (defun muse-grep (string &optional grep-command-no-shadow)
713 "Grep for STRING in the project directories.
714 GREP-COMMAND if passed will supplant `muse-grep-command'."
715 ;; careful - grep-command leaks into compile, so we call it
716 ;; -no-shadow instead
717 (require 'compile)
718 (let* ((str (or grep-command-no-shadow muse-grep-command))
719 (muse-directories (mapcar
720 (lambda (thing)
721 (car (cadr thing)))
722 muse-project-alist))
723 (dirs (mapconcat (lambda (dir)
724 (shell-quote-argument
725 (expand-file-name dir)))
726 muse-directories " ")))
727 (if (string= dirs "")
728 (muse-display-warning
729 "No directories were found in the current project; aborting search")
730 (while (string-match "%W" str)
731 (setq str (replace-match string t t str)))
732 (while (string-match "%D" str)
733 (setq str (replace-match dirs t t str)))
734 (if (fboundp 'compilation-start)
735 (compilation-start str nil (lambda (&rest args) "*search*")
736 grep-regexp-alist)
737 (and (fboundp 'compile-internal)
738 (compile-internal str "No more search hits" "search"
739 nil grep-regexp-alist))))))
741 ;;;###autoload
742 (defun muse-search-with-command (text)
743 "Search for the given TEXT string in the project directories
744 using the specified command."
745 (interactive
746 (list (let ((str (concat muse-grep-command)) pos)
747 (when (string-match "%W" str)
748 (setq pos (match-beginning 0))
749 (unless (featurep 'xemacs)
750 (setq pos (1+ pos)))
751 (setq str (replace-match "" t t str)))
752 (read-from-minibuffer "Search command: "
753 (cons str pos) nil nil
754 'muse-search-history))))
755 (muse-grep nil text))
757 ;;;###autoload
758 (defun muse-search ()
759 "Search for the given TEXT using the default grep command."
760 (interactive)
761 (muse-grep (read-string "Search: ")))
763 ;;;###autoload
764 (defun muse-find-backlinks ()
765 "Grep for the current pagename in all the project directories."
766 (interactive)
767 (muse-grep (muse-page-name)))
770 ;;; Generate an index of all known Muse pages
772 (defun muse-generate-index (&optional as-list exclude-private)
773 "Generate an index of all Muse pages."
774 (let ((index (muse-index-as-string as-list exclude-private)))
775 (with-current-buffer (get-buffer-create "*Muse Index*")
776 (erase-buffer)
777 (insert index)
778 (current-buffer))))
780 ;;;###autoload
781 (defun muse-index ()
782 "Display an index of all known Muse pages."
783 (interactive)
784 (message "Generating Muse index...")
785 (let ((project (muse-project)))
786 (with-current-buffer (muse-generate-index)
787 (goto-char (point-min))
788 (muse-mode)
789 (setq muse-current-project project)
790 (pop-to-buffer (current-buffer))))
791 (message "Generating Muse index...done"))
793 (defun muse-index-as-string (&optional as-list exclude-private exclude-current)
794 "Generate an index of all Muse pages.
795 If AS-LIST is non-nil, insert a dash and spaces before each item.
796 If EXCLUDE-PRIVATE is non-nil, exclude files that have private permissions.
797 If EXCLUDE-CURRENT is non-nil, exclude the current file from the output."
798 (let ((files (sort (copy-alist (muse-project-file-alist))
799 (function
800 (lambda (l r)
801 (string-lessp (car l) (car r)))))))
802 (when (and exclude-current (muse-page-name))
803 (setq files (delete (assoc (muse-page-name) files) files)))
804 (with-temp-buffer
805 (while files
806 (unless (and exclude-private
807 (muse-project-private-p (cdar files)))
808 (insert (if as-list " - " "") "[[" (caar files) "]]\n"))
809 (setq files (cdr files)))
810 (buffer-string))))
812 ;;; Insert tags interactively on C-c TAB t
814 (defvar muse-tag-history nil
815 "List of recently-entered tags; used by `muse-insert-tag'.
816 If you want a tag to start as the default, you may manually set
817 this variable to a list.")
819 (defvar muse-custom-tags nil
820 "Keep track of any new tags entered in `muse-insert-tag'.
821 If there are (X)HTML tags that you use frequently with that
822 function, you might want to set this manually.")
824 ;;;###autoload
825 (defun muse-insert-tag (tag)
826 "Insert a tag interactively with a blank line after it."
827 (interactive
828 (list
829 (funcall
830 muse-completing-read-function
831 (concat "Tag: "
832 (when muse-tag-history
833 (concat "(default: " (car muse-tag-history) ") ")))
834 (progn
835 (require 'muse-publish)
836 (mapcar 'list (nconc (mapcar 'car muse-publish-markup-tags)
837 muse-custom-tags)))
838 nil nil nil 'muse-tag-history
839 (car muse-tag-history))))
840 (when (equal tag "")
841 (setq tag (car muse-tag-history)))
842 (unless (interactive-p)
843 (require 'muse-publish))
844 (let ((tag-entry (assoc tag muse-publish-markup-tags))
845 (options ""))
846 ;; Add to custom list if no entry exists
847 (unless tag-entry
848 (add-to-list 'muse-custom-tags tag))
849 ;; Get option
850 (when (nth 2 tag-entry)
851 (setq options (read-string "Option: ")))
852 (unless (equal options "")
853 (setq options (concat " " options)))
854 ;; Insert the tag, closing if necessary
855 (when tag (insert (concat "<" tag options ">")))
856 (when (nth 1 tag-entry)
857 (insert (concat "\n\n</" tag ">\n"))
858 (forward-line -2))))
860 ;;; Muse list edit minor mode
862 (defvar muse-list-edit-minor-mode-map
863 (let ((map (make-sparse-keymap)))
864 (define-key map [(meta return)] 'muse-l-e-m-m-insert-list-item)
865 (define-key map [(control ?>)] 'muse-l-e-m-m-i-list-item-indentation)
866 (define-key map [(control ?<)] 'muse-l-e-m-m-d-list-item-indentation)
868 map)
869 "Keymap used by Muse list edit minor mode.")
871 (defvar muse-l-e-m-m-list-item-regexp
872 (concat "^%s\\(\\([^\n" muse-regexp-blank "].*?\\)?::"
873 "\\(?:[" muse-regexp-blank "]+\\|$\\)"
874 "\\|[" muse-regexp-blank "]?[-*+][" muse-regexp-blank "]*"
875 "\\|[" muse-regexp-blank "][0-9]+\\.[" muse-regexp-blank "]*\\)")
876 "Regexp used to match the beginning of a list item.
877 This is used by `muse-list-edit-minor-mode'.
878 The '%s' will be replaced with a whitespace regexp when publishing.")
880 (defun muse-l-e-m-m-insert-list-item ()
881 "See `muse-insert-list-item' for documentation."
882 (interactive)
883 (let ((muse-list-item-regexp muse-l-e-m-m-list-item-regexp))
884 (call-interactively 'muse-insert-list-item)))
886 (defun muse-l-e-m-m-i-list-item-indentation ()
887 "See `muse-increase-list-item-indentation' for documentation."
888 (interactive)
889 (let ((muse-list-item-regexp muse-l-e-m-m-list-item-regexp))
890 (call-interactively 'muse-increase-list-item-indentation)))
892 (defun muse-l-e-m-m-d-list-item-indentation ()
893 "See `muse-decrease-list-item-indentation' for documentation."
894 (interactive)
895 (let ((muse-list-item-regexp muse-l-e-m-m-list-item-regexp))
896 (call-interactively 'muse-decrease-list-item-indentation)))
898 (defvar muse-l-e-m-m-data nil
899 "A list of data that was changed by Muse list edit minor mode.")
900 (make-variable-buffer-local 'muse-l-e-m-m-data)
902 ;;;###autoload
903 (define-minor-mode muse-list-edit-minor-mode
904 "This is a global minor mode for editing files with lists.
905 It is meant to be used with other major modes, and not with Muse mode.
907 Interactively, with no prefix argument, toggle the mode.
908 With universal prefix ARG turn mode on.
909 With zero or negative ARG turn mode off.
911 This recognizes not only Muse-style lists, which use the \"-\"
912 character or numbers, but also lists that use asterisks or plus
913 signs. This should make the minor mode generally useful.
915 Definition lists and footnotes are also recognized.
917 Note that list items may omit leading spaces, for compatibility
918 with modes that set `left-margin', such as
919 `debian-changelog-mode'.
921 The standard Muse keybindings for editing lists are also provided."
922 :init-value nil
923 :lighter ""
924 :keymap muse-list-edit-minor-mode-map
925 :global nil
926 :group 'muse-mode
927 (if (not muse-list-edit-minor-mode)
928 ;; deactivate
929 (when muse-l-e-m-m-data
930 (setq adaptive-fill-regexp (cdr (assoc "a-f-r" muse-l-e-m-m-data))
931 paragraph-start (cdr (assoc "p-s" muse-l-e-m-m-data))
932 fill-prefix (cdr (assoc "f-p" muse-l-e-m-m-data)))
933 (setq muse-l-e-m-m-data nil))
934 ;; activate
935 (unless muse-l-e-m-m-data
936 ;; save previous fill-related data so we can restore it later
937 (setq muse-l-e-m-m-data
938 (list (cons "a-f-r" adaptive-fill-regexp)
939 (cons "p-s" paragraph-start)
940 (cons "f-p" fill-prefix))))
941 ;; make fill work nicely with item lists
942 (let ((regexp (concat "\\s-*\\([-*+]\\|[0-9]+\\.\\)\\s-+"
943 "\\|\\[[0-9]+\\]\\s-*"
944 "\\|.*\\s-*::\\s-+")))
945 (set (make-local-variable 'adaptive-fill-regexp)
946 (concat regexp "\\|\\s-*"))
947 (set (make-local-variable 'paragraph-start)
948 (concat paragraph-start "\\|" regexp)))
949 ;; force fill-prefix to be nil, because if it is a string that has
950 ;; initial spaces, it messes up fill-paragraph's algorithm
951 (set (make-local-variable 'fill-prefix) nil)))
953 (defun turn-on-muse-list-edit-minor-mode ()
954 "Unconditionally turn on Muse list edit minor mode."
955 (muse-list-edit-minor-mode 1))
957 (defun turn-off-muse-list-edit-minor-mode ()
958 "Unconditionally turn off Muse list edit minor mode."
959 (muse-list-edit-minor-mode -1))
961 ;;; muse-mode.el ends here