1 ;;; muse-mode.el --- mode for editing Muse files; has font-lock support
3 ;; Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
5 ;; This file is not part of GNU Emacs.
7 ;; This is free software; you can redistribute it and/or modify it under
8 ;; the terms of the GNU General Public License as published by the Free
9 ;; Software Foundation; either version 2, or (at your option) any later
12 ;; This is distributed in the hope that it will be useful, but WITHOUT
13 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; 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.
38 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
40 ;; Emacs Muse Major Mode
42 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
45 (require 'muse-regexps
)
46 (require 'muse-project
)
47 (require 'muse-publish
)
49 (autoload 'muse-use-font-lock
"muse-colors")
54 (require 'pcomplete
) ; load if available
59 (defgroup muse-mode nil
60 "Options controlling the behavior of the Muse editing Mode."
63 (defcustom muse-mode-highlight-p t
64 "If non-nil, highlight the content of Muse buffers."
69 (defcustom muse-mode-auto-p nil
70 "If non-nil, automagically determine when Muse mode should be activated."
75 (add-hook 'find-file-hooks
'muse-mode-maybe
)
76 (remove-hook 'find-file-hooks
'muse-mode-maybe
))
80 (defcustom muse-mode-intangible-links nil
81 "If non-nil, use the intangible property on links.
82 This can cause problems with flyspell (and potentially fill-mode),
83 so only enable this if you don't use either of these."
87 (defcustom muse-mode-hook nil
88 "A hook that is run when Muse mode is entered."
90 :options
'(flyspell-mode footnote-mode turn-on-auto-fill
91 highlight-changes-mode
)
92 :set
#'(lambda (sym val
)
93 (when (featurep 'muse-wiki
)
94 (add-to-list 'val
'muse-wiki-update-custom-values
))
99 (let ((map (make-sparse-keymap)))
100 (define-key map
[(control ?c
) (control ?a
)] 'muse-index
)
101 (define-key map
[(control ?c
) (control ?b
)] 'muse-browse-result
)
102 (define-key map
[(control ?c
) (control ?c
)] 'muse-follow-name-at-point
)
103 (define-key map
[(control ?c
) (control ?e
)] 'muse-edit-link-at-point
)
104 (define-key map
[(control ?c
) (control ?t
)] 'muse-publish-this-file
)
105 (define-key map
[(control ?c
) (control ?v
)] 'muse-follow-name-at-point
)
107 (define-key map
[(control ?c
) (control ?l
)] 'font-lock-mode
)
109 (define-key map
[(control ?c
) ?
=] 'muse-what-changed
)
111 (define-key map
[tab] 'muse-next-reference)
112 (define-key map [(control ?i)] 'muse-next-reference)
114 (if (featurep 'xemacs)
116 (define-key map [(button2)] 'muse-follow-name-at-mouse)
117 (define-key map [(shift button2)]
118 'muse-follow-name-at-mouse-other-window))
119 (define-key map [(shift control ?m)]
120 'muse-follow-name-at-point-other-window)
121 (define-key map [mouse-2] 'muse-follow-name-at-mouse)
122 (define-key map [(shift mouse-2)]
123 'muse-follow-name-at-mouse-other-window))
125 (define-key map [(shift tab)] 'muse-previous-reference)
126 (unless (featurep 'xemacs)
127 (define-key map [(shift iso-lefttab)] 'muse-previous-reference)
128 (define-key map [(shift control ?i)] 'muse-previous-reference))
130 (define-key map [(control ?c) (control ?f)] 'muse-project-find-file)
131 (define-key map [(control ?c) (control ?p)] 'muse-project-publish)
133 (define-key map [(control ?c) tab] 'muse-insert-tag)
134 (define-key map [(control ?c) (?i) (?t)] 'muse-insert-tag)
136 (define-key map [(control ?c) (?i) (?l)]
137 'muse-insert-relative-link-to-file)
139 ;; Enhanced link functions
140 (define-key map [(meta return)] 'muse-insert-list-item)
141 (define-key map [(control ?>)] 'muse-increase-list-item-indentation)
142 (define-key map [(control ?<)] 'muse-decrease-list-item-indentation)
144 (when (featurep 'pcomplete)
145 (define-key map [(meta tab)] 'pcomplete)
146 (define-key map [(meta control ?i)] 'pcomplete))
149 "Keymap used by Emacs Muse mode.")
154 (define-derived-mode muse-mode text-mode "Muse"
155 "Muse is an Emacs mode for authoring and publishing documents.
157 ;; Since we're not inheriting from normal-mode, we need to
158 ;; explicitly run file variables.
160 (hack-local-variables)
161 (error (message "File local-variables error: %s"
162 (prin1-to-string err))))
163 ;; Avoid lock-up caused by use of the 'intangible' text property
165 (unless muse-mode-intangible-links
166 (set (make-local-variable 'inhibit-point-motion-hooks) t))
167 (setq muse-current-project (muse-project-of-file))
168 (muse-project-set-variables)
169 ;; Make sure several variables get updated if the user has changed
170 ;; them without using the customize interface.
171 (muse-update-ignored-extensions-regexp 'muse-ignored-extensions
172 muse-ignored-extensions)
173 (muse-update-url-regexp 'muse-url-protocols muse-url-protocols)
174 ;; Make fill not split up links
175 (when (boundp 'fill-nobreak-predicate)
176 (make-local-variable 'fill-nobreak-predicate)
177 ;; Work around annoying inconsistency in fill handling between
179 (if (< emacs-major-version 22)
180 (setq fill-nobreak-predicate 'muse-mode-fill-nobreak-p)
181 (add-to-list 'fill-nobreak-predicate
182 'muse-mode-fill-nobreak-p)))
183 ;; Make fill work nicely with item lists
184 (set (make-local-variable 'adaptive-fill-regexp)
185 "\\s-+\\(-\\|[0-9]+\\.\\)\\s-+\\|\\[[0-9]+\\]\\s-*\\|\\s-*")
186 (set (make-local-variable 'paragraph-start)
187 (concat paragraph-start "\\|\\s-+\\(-\\|[0-9]+\\.\\)\\s-+"
188 "\\|\\[[0-9]+\\]\\s-*"))
189 ;; Comment syntax is `; comment'
190 (set (make-local-variable 'comment-start)
192 (set (make-local-variable 'comment-start-skip)
194 ;; If we're using Emacs21, this makes flyspell work like it should
195 (when (boundp 'flyspell-generic-check-word-p)
196 (set (make-local-variable 'flyspell-generic-check-word-p)
197 'muse-mode-flyspell-p))
198 ;; If pcomplete is available, set it up
199 (when (featurep 'pcomplete)
200 (set (make-local-variable 'pcomplete-default-completion-function)
201 'muse-mode-completions)
202 (set (make-local-variable 'pcomplete-command-completion-function)
203 'muse-mode-completions)
204 (set (make-local-variable 'pcomplete-parse-arguments-function)
205 'muse-mode-current-word))
206 (when muse-mode-highlight-p
207 (muse-use-font-lock)))
210 'flyspell-mode-predicate
211 'muse-mode-flyspell-p)
213 (defun muse-mode-fill-nobreak-p ()
214 "Return nil if we should allow a fill to occur at point.
215 Otherwise return non-nil.
217 This is used to keep long explicit links from being mangled by
221 (and (re-search-backward "\\[\\[\\|\\]\\]"
222 (line-beginning-position) t)
223 (string= (or (match-string 0) "")
226 (defun muse-mode-flyspell-p ()
227 "Return non-nil if we should allow spell-checking to occur at point.
228 Otherwise return nil.
230 This is used to keep links from being improperly colorized by flyspell."
232 (null (muse-link-at-point))))
234 (defun muse-mode-choose-mode ()
235 "Turn the proper Emacs Muse related mode on for this file."
236 (let ((project (muse-project-of-file)))
237 (funcall (or (and project (muse-get-keyword :major-mode (cadr project) t))
240 (defun muse-mode-maybe ()
241 "Maybe turn Emacs Muse mode on for this file."
242 (let ((project (muse-project-of-file)))
244 (funcall (or (muse-get-keyword :major-mode (cadr project) t)
247 ;;; Enhanced list editing
249 (defun muse-on-blank-line ()
250 "See if point is on a blank line"
253 (looking-at (concat "[" muse-regexp-blank "]?[\n]+"))))
255 (defun muse-get-paragraph-start ()
256 "Return the start of the current paragraph. This function will
257 return nil if there are no prior paragraphs and the beginning of
258 the line if point is on a blank line."
259 (let ((para-start (concat "[\n]+[" muse-regexp-blank "]?[\n]+")))
260 ;; search back to start of paragraph
263 (if (not (muse-on-blank-line))
264 (re-search-backward para-start nil t)
265 (line-beginning-position))))))
268 (defun muse-insert-list-item ()
269 "Insert a list item at the current point, taking into account
270 your current list type and indentation level."
272 (let ((newitem " - ")
274 (pstart (muse-get-paragraph-start))
275 (list-item (format muse-list-item-regexp
276 (concat "[" muse-regexp-blank "]*"))))
277 ;; search backwards for start of current item
279 (when (re-search-backward list-item pstart t)
280 ;; save the matching item
281 (setq newitem (match-string 0))
282 ;; see what type it is
283 (if (string-match "::" (match-string 0))
284 ;; is a definition, replace the term
285 (setq newitem (concat " "
286 (read-string "Term: ")
288 ;; see if it's a numbered list
289 (when (string-match "[0-9]+" newitem)
290 ;; is numbered, so increment
293 (match-string 0 newitem))))
294 (setq newitem (replace-match
295 (number-to-string itemno)
296 nil nil newitem))))))
297 ;; insert the new item
298 (insert (concat "\n" newitem))))
300 (defun muse-alter-list-item-indentation (operation)
301 "Alter the indentation of the current list item.
302 Valid values of OPERATION are 'increase and 'decrease."
303 (let ((pstart (muse-get-paragraph-start))
304 (list-item (format muse-list-item-regexp
305 (concat "[" muse-regexp-blank "]*")))
306 beg move-func indent)
307 ;; search backwards until start of paragraph to see if we are on a
310 (if (or (progn (goto-char (muse-line-beginning-position))
312 (looking-at list-item))
313 ;; not on item, so search backwards
314 (re-search-backward list-item pstart t))
317 (setq indent (buffer-substring (match-beginning 0)
318 (match-beginning 1)))
319 (muse-forward-list-item (muse-list-item-type (match-string 1))
321 (number-to-string (length indent))
324 (narrow-to-region beg (point))
325 (goto-char (point-min))
327 (while (< (point) (point-max))
328 ;; increase or decrease the indentation
330 (cond ((eq operation 'increase)
332 ((eq operation 'decrease)
334 ;; we have enough space, so delete it
335 (delete-region (match-beginning 0)
339 ;; we are not on an item, so warn
340 (message "You are not on a list item.")))))
343 (defun muse-increase-list-item-indentation ()
344 "Increase the indentation of the current list item."
346 (muse-alter-list-item-indentation 'increase))
349 (defun muse-decrease-list-item-indentation ()
350 "Decrease the indentation of the current list item."
352 (muse-alter-list-item-indentation 'decrease))
354 ;;; Support page name completion using pcomplete
356 (defun muse-completions ()
357 "Return a list of possible completions names for this buffer."
358 (let ((project (muse-project-of-file)))
360 (while (pcomplete-here
361 (mapcar 'car (muse-project-file-alist project)))))))
363 (defun muse-current-word ()
367 (skip-chars-backward (concat "^\\[\n" muse-regexp-blank))
368 (narrow-to-region (point) end))
369 (pcomplete-parse-buffer-arguments))))
371 ;;; Navigate/visit links or URLs. Use TAB, S-TAB and RET (or mouse-2).
373 (defun muse-link-at-point (&optional pos)
374 "Return link text if a URL or link is at point."
375 (let ((case-fold-search nil)
376 (inhibit-point-motion-hooks t)
377 (here (or pos (point))))
378 ;; if we are using muse-colors, we can just use link properties to
379 ;; determine whether we are on a link
380 (if (featurep 'muse-colors)
381 (when (get-text-property here 'muse-link)
383 (when (get-text-property (1- here) 'muse-link)
384 (goto-char (or (previous-single-property-change here 'muse-link)
386 (if (looking-at muse-explicit-link-regexp)
388 (goto-char (match-beginning 1))
389 (muse-handle-explicit-link))
390 (muse-handle-implicit-link))))
391 ;; use fallback method to find a link
393 (and (char-after pos)
394 (not (eq (char-syntax (char-after pos)) ?\ ))))
397 ;; check for explicit link here or before point
398 (if (or (looking-at muse-explicit-link-regexp)
400 (re-search-backward "\\[\\[\\|\\]\\]"
401 (muse-line-beginning-position)
403 (string= (or (match-string 0) "") "[[")
404 (looking-at muse-explicit-link-regexp)))
406 (goto-char (match-beginning 1))
407 (muse-handle-explicit-link))
409 ;; check for bare URL or other link type
410 (skip-chars-backward (concat "^'\"<>{}(\n" muse-regexp-blank))
411 (and (looking-at muse-implicit-link-regexp)
412 (muse-handle-implicit-link))))))))
414 (defun muse-make-link (link &optional name)
415 "Return a link to LINK with NAME as the text."
416 (when (string-match muse-explicit-link-regexp link)
417 (unless name (setq name (match-string 2 link)))
418 (setq link (match-string 1 link)))
421 (not (string= name ""))
422 (not (string= link name)))
423 (concat "[[" (muse-link-escape link) "][" (muse-link-escape name) "]]")
424 (concat "[[" (muse-link-escape link) "]]")))
427 (defun muse-insert-relative-link-to-file ()
428 "Insert a relative link to a file, with optional description,
429 at the current point."
430 ;; Perhaps the relative location should be configurable, so that the
431 ;; file search would start in the publshing directory and then
432 ;; insert the link relative to the publishing directory
436 (file-relative-name (read-file-name "Link: "))
437 (read-string "Text: "))))
440 (defun muse-edit-link-at-point ()
441 "Edit the current link.
442 Do not rename the page originally referred to."
444 (if (muse-link-at-point)
448 (read-string "Link: "
449 (muse-match-string-no-properties 1))
450 (read-string "Text: "
451 (muse-match-string-no-properties 2))))
453 (error "There is no valid link at point")))
455 (defun muse-visit-link-default (link &optional other-window)
456 "Visit the URL or link named by LINK.
457 If ANCHOR is specified, search for it after opening LINK.
459 This is the default function to call when visiting links; it is
460 used by `muse-visit-link' if you have not specified :visit-link
461 in `muse-project-alist'."
462 (if (string-match muse-url-regexp link)
463 (muse-browse-url link)
466 (when (string-match "#" link)
467 (setq anchor (substring link (match-beginning 0))
468 link (if (= (match-beginning 0) 0)
469 ;; If there is an anchor but no link, default
470 ;; to the current page.
472 (substring link 0 (match-beginning 0)))))
474 (setq base-buffer (get-buffer link))
475 (if (and base-buffer (not (buffer-file-name base-buffer)))
476 ;; If file is temporary (no associated file), just switch to
479 (switch-to-buffer-other-window base-buffer)
480 (switch-to-buffer base-buffer))
481 (let ((project (muse-project-of-file)))
483 (muse-project-find-file link project
485 'find-file-other-window))
487 (find-file-other-window link)
488 (find-file link))))))
491 (goto-char (point-min))
492 (unless (re-search-forward (concat "^\\W*" (regexp-quote anchor)
495 (goto-char pos)))))))
497 (defun muse-visit-link (link &optional other-window)
498 "Visit the URL or link named by LINK."
499 (let ((visit-link-function
500 (muse-get-keyword :visit-link (cadr (muse-project-of-file)) t)))
501 (if visit-link-function
502 (funcall visit-link-function link other-window)
503 (muse-visit-link-default link other-window))))
506 (defun muse-browse-result (style &optional other-window)
507 "Visit the current page's published result."
508 (interactive (list (muse-publish-get-style) current-prefix-arg))
509 (setq style (muse-style style))
511 (muse-publish-output-file buffer-file-name
512 (muse-style-element :path style) style)))
513 (if (not (file-readable-p result-path))
514 (error "Cannot open output file '%s'" result-path)
516 (find-file-other-window result-path)
517 (let ((func (muse-style-element :browser style t)))
519 (funcall func result-path)
520 (message "The %s publishing style does not support browsing."
524 (defun muse-follow-name-at-point (&optional other-window)
525 "Visit the link at point, or insert a newline if none is found."
527 (let ((link (muse-link-at-point)))
529 (muse-visit-link link other-window)
530 (error "There is no valid link at point"))))
533 (defun muse-follow-name-at-point-other-window ()
534 "Visit the link at point in other window."
536 (muse-follow-name-at-point t))
538 (defun muse-follow-name-at-mouse (event &optional other-window)
539 "Visit the link at point, or yank text if none is found."
542 (cond ((fboundp 'event-window) ; XEmacs
543 (set-buffer (window-buffer (event-window event)))
544 (and (funcall (symbol-function 'event-point) event)
545 (goto-char (funcall (symbol-function 'event-point) event))))
546 ((fboundp 'posn-window) ; Emacs
547 (set-buffer (window-buffer (posn-window (event-start event))))
548 (goto-char (posn-point (event-start event)))))
549 (let ((link (muse-link-at-point)))
551 (muse-visit-link link other-window)
552 ;; Fall back to normal binding for this event
554 (lookup-key (current-global-map) (this-command-keys)))))))
556 (defun muse-follow-name-at-mouse-other-window (event)
557 "Visit the link at point"
559 ;; throw away the old window position, since other-window will
561 (select-window (car (cadr event)))
562 (muse-follow-name-at-mouse event t))
565 (defun muse-next-reference ()
566 "Move forward to next Muse link or URL, cycling if necessary."
568 (let ((cycled 0) pos)
570 (when (memq (get-text-property (point) 'face)
571 '(muse-link-face muse-bad-link-face))
572 (goto-char (or (next-single-property-change (point) 'face)
575 (let ((next (point)))
576 (if (while (and (null pos)
578 (next-single-property-change
580 (when (memq (get-text-property next 'face)
581 '(muse-link-face muse-bad-link-face))
584 (goto-char (point-min))
585 (setq cycled (1+ cycled))))))
590 (defun muse-previous-reference ()
591 "Move backward to the next Muse link or URL, cycling if necessary.
592 This function is not entirely accurate, but it's close enough."
594 (let ((cycled 0) pos)
597 (let ((prev (point)))
598 (if (while (and (null pos)
600 (previous-single-property-change
602 (when (memq (get-text-property prev 'face)
603 '(muse-link-face muse-bad-link-face))
606 (goto-char (point-max))
607 (setq cycled (1+ cycled))))))
612 (defun muse-what-changed ()
613 "Show the unsaved changes that have been made to the current file."
615 (diff-backup buffer-file-name))
617 ;;; Generate an index of all known Muse pages
619 (defun muse-generate-index (&optional as-list exclude-private)
620 "Generate an index of all Muse pages."
621 (let ((index (muse-index-as-string as-list exclude-private)))
622 (with-current-buffer (get-buffer-create "*Muse Index*")
629 "Display an index of all known Muse pages."
631 (message "Generating Muse index...")
632 (let ((project (muse-project)))
633 (with-current-buffer (muse-generate-index)
634 (goto-char (point-min))
636 (setq muse-current-project project)
637 (pop-to-buffer (current-buffer))))
638 (message "Generating Muse index...done"))
640 (defun muse-index-as-string (&optional as-list exclude-private exclude-current)
641 "Generate an index of all Muse pages.
642 If AS-LIST is non-nil, insert a dash and spaces before each item.
643 If EXCLUDE-PRIVATE is non-nil, exclude files that have private permissions.
644 If EXCLUDE-CURRENT is non-nil, exclude the current file from the output."
645 (let ((files (sort (copy-alist (muse-project-file-alist))
648 (string-lessp (car l) (car r)))))))
649 (when (and exclude-current (muse-page-name))
650 (setq files (delete (assoc (muse-page-name) files) files)))
653 (unless (and exclude-private
654 (muse-project-private-p (cdar files)))
655 (insert (if as-list " - " "") "[[" (caar files) "]]\n"))
656 (setq files (cdr files)))
659 ;;; Insert tags interactively on C-c TAB
661 (defvar muse-tag-history nil
662 "List of recently-entered tags; used by `muse-insert-tag'.
663 If you want a tag to start as the default, you may manually set
664 this variable to a list.")
666 (defvar muse-custom-tags nil
667 "Keep track of any new tags entered in `muse-insert-tag'.
668 If there are (X)HTML tags that you use frequently with that
669 function, you might want to set this manually.")
672 (defun muse-insert-tag (tag)
673 "Insert a tag interactively with a blank line after it."
678 (when muse-tag-history
679 (concat "(default: " (car muse-tag-history) ") ")))
680 (mapcar 'list (nconc (mapcar 'car muse-publish-markup-tags)
682 nil nil nil 'muse-tag-history
683 (car muse-tag-history))))
685 (setq tag (car muse-tag-history)))
686 (let ((tag-entry (assoc tag muse-publish-markup-tags))
688 ;; Add to custom list if no entry exists
690 (add-to-list 'muse-custom-tags tag))
692 (when (nth 2 tag-entry)
693 (setq options (read-string "Option: ")))
694 (unless (equal options "")
695 (setq options (concat " " options)))
696 ;; Insert the tag, closing if necessary
697 (when tag (insert (concat "<" tag options ">")))
698 (when (nth 1 tag-entry)
699 (insert (concat "\n\n</" tag ">\n"))
704 ;;; muse-mode.el ends here