1 ;;; muse-mode.el --- Mode for editing Muse files; has font-lock support.
3 ;; Copyright (C) 2004, 2005 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
31 ;; `muse-insert-tag' function.
35 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
37 ;; Emacs Muse Major Mode
39 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
42 (require 'muse-regexps
)
43 (require 'muse-project
)
44 (require 'muse-publish
)
46 (autoload 'muse-use-font-lock
"muse-colors")
51 (require 'pcomplete
) ; load if available
56 (defgroup muse-mode nil
57 "Options controlling the behavior of the Muse editing Mode.
58 See `muse-publish' for more information."
61 (defcustom muse-mode-highlight-p t
62 "If non-nil, highlight the content of Muse buffers."
67 (defcustom muse-mode-auto-p t
68 "If non-nil, automagically determine when Muse mode should be activated."
73 (add-hook 'find-file-hooks
'muse-mode-maybe
)
74 (remove-hook 'find-file-hooks
'muse-mode-maybe
))
78 (defcustom muse-mode-intangible-links nil
79 "If non-nil, use the intangible property on links.
80 This can cause problems with flyspell (and potentially fill-mode),
81 so only enable this if you don't use either of these."
85 (defcustom muse-mode-hook nil
86 "A hook that is run when Muse mode is entered."
88 :options
'(flyspell-mode footnote-mode turn-on-auto-fill
89 highlight-changes-mode
)
93 (let ((map (make-sparse-keymap)))
94 (define-key map
[(control ?c
) (control ?a
)] 'muse-index
)
95 (define-key map
[(control ?c
) (control ?b
)] 'muse-browse-result
)
96 (define-key map
[(control ?c
) (control ?c
)] 'muse-follow-name-at-point
)
97 (define-key map
[(control ?c
) (control ?e
)] 'muse-edit-link-at-point
)
98 (define-key map
[(control ?c
) (control ?t
)] 'muse-publish-this-file
)
99 (define-key map
[(control ?c
) (control ?v
)] 'muse-follow-name-at-point
)
101 (define-key map
[(control ?c
) (control ?l
)] 'font-lock-mode
)
103 (define-key map
[(control ?c
) ?
=] 'muse-what-changed
)
105 (define-key map
[tab] 'muse-next-reference)
106 (define-key map [(control ?i)] 'muse-next-reference)
108 (if (featurep 'xemacs)
110 (define-key map [(button2)] 'muse-follow-name-at-mouse)
111 (define-key map [(shift button2)]
112 'muse-follow-name-at-mouse-other-window))
113 (define-key map [(shift control ?m)]
114 'muse-follow-name-at-point-other-window)
115 (define-key map [mouse-2] 'muse-follow-name-at-mouse)
116 (define-key map [(shift mouse-2)]
117 'muse-follow-name-at-mouse-other-window))
119 (if (featurep 'xemacs)
120 (define-key map [(shift tab)] 'muse-previous-reference)
121 (define-key map [(shift iso-lefttab)] 'muse-previous-reference)
122 (define-key map [(shift control ?i)] 'muse-previous-reference))
124 (define-key map [(control ?c) (control ?f)] 'muse-project-find-file)
125 (define-key map [(control ?c) (control ?p)] 'muse-project-publish)
127 (define-key map [(control ?c) tab] 'muse-insert-tag)
128 (define-key map [(control ?c) (control ?i)] 'muse-insert-tag)
130 (when (featurep 'pcomplete)
131 (define-key map [(meta tab)] 'pcomplete)
132 (define-key map [(meta control ?i)] 'pcomplete))
135 "Keymap used by Emacs Muse mode.")
140 (define-derived-mode muse-mode text-mode "Muse"
141 "Muse is an Emacs mode for authoring and publishing documents.
143 ;; Since we're not inheriting from normal-mode, we need to
144 ;; explicitly run file variables.
146 (hack-local-variables)
147 (error (message "File local-variables error: %s"
148 (prin1-to-string err))))
149 ;; Avoid lock-up caused by use of the 'intangible' text property
151 (unless muse-mode-intangible-links
152 (set (make-local-variable 'inhibit-point-motion-hooks) t))
153 (if muse-mode-highlight-p
154 (muse-use-font-lock))
155 (setq muse-current-project (muse-project-of-file))
156 (muse-project-set-variables)
157 ;; Make sure several variables get updated if the user has changed
158 ;; them without using the customize interface.
159 (muse-update-ignored-extensions-regexp 'muse-ignored-extensions
160 muse-ignored-extensions)
161 ;; Make fill not split up links
162 (when (boundp 'fill-nobreak-predicate)
163 (make-local-variable 'fill-nobreak-predicate)
164 ;; Work around annoying inconsistency in fill handling between
165 ;; Emacs CVS and all other Emacs types.
166 (if (not (muse-extreg-usable-p))
167 (setq fill-nobreak-predicate 'muse-mode-fill-nobreak-p)
168 (add-to-list 'fill-nobreak-predicate
169 'muse-mode-fill-nobreak-p)))
170 ;; Make adaptive fill work nicely with item lists
171 (set (make-local-variable 'adaptive-fill-regexp)
172 (concat "[" muse-regexp-blank "]*\\(-+["
175 muse-regexp-blank "]*\\)*"))
176 (when (featurep 'pcomplete)
177 ;; If pcomplete is available, set it up
178 (set (make-variable-buffer-local 'pcomplete-default-completion-function)
179 'muse-mode-completions)
180 (set (make-variable-buffer-local 'pcomplete-command-completion-function)
181 'muse-mode-completions)
182 (set (make-variable-buffer-local 'pcomplete-parse-arguments-function)
183 'muse-mode-current-word)))
186 'flyspell-mode-predicate
187 'muse-mode-flyspell-p)
189 (defun muse-mode-fill-nobreak-p ()
190 "Return nil if we should allow a fill to occur at point.
191 Otherwise return non-nil.
193 This is used to keep long explicit links from being mangled by
197 (and (re-search-backward "\\[\\[\\|\\]\\]"
198 (line-beginning-position) t)
199 (string= (or (match-string 0) "")
202 (defun muse-mode-flyspell-p ()
203 "Return non-nil if we should allow spell-checking to occur at point.
204 Otherwise return nil.
206 This is used to keep links from being improperly colorized by flyspell."
208 (null (muse-link-at-point))))
210 (defun muse-mode-maybe ()
211 "Maybe turn Emacs Muse mode on for this file."
212 (let ((project (muse-project-of-file)))
214 (funcall (or (muse-get-keyword :major-mode (cadr project) t)
217 ;;; Support page name completion using pcomplete
219 (defun muse-completions ()
220 "Return a list of possible completions names for this buffer."
221 (let ((project (muse-project-of-file)))
223 (while (pcomplete-here
224 (mapcar 'car (muse-project-file-alist project)))))))
226 (defun muse-current-word ()
230 (skip-chars-backward (concat "^\\["
232 (narrow-to-region (point) end))
233 (pcomplete-parse-buffer-arguments))))
235 ;;; Navigate/visit links or URLs. Use TAB, S-TAB and RET (or mouse-2).
237 (defun muse-link-at-point (&optional pos)
238 "Return link text if a URL or link is at point."
239 (let ((case-fold-search nil)
240 (here (or pos (point))))
242 (and (char-after pos)
243 (not (eq (char-syntax (char-after pos)) ?\ ))))
246 ;; Check for explicit link here or before point
247 (if (or (looking-at muse-explicit-link-regexp)
249 (re-search-backward "\\[\\[\\|\\]\\]"
250 (muse-line-beginning-position)
252 (string= (or (match-string 0) "") "[[")
253 (looking-at muse-explicit-link-regexp)))
255 (goto-char (match-beginning 1))
256 (muse-handle-explicit-link))
258 ;; Check for bare URL or other link type
259 (skip-chars-backward (concat "^'\"<>{}(\n"
261 (and (looking-at muse-implicit-link-regexp)
262 (muse-handle-implicit-link)))))))
264 (defun muse-make-link (link &optional name)
265 "Return a link to LINK with NAME as the text."
268 (not (string= name ""))
269 (not (string= link name)))
270 (concat "[[" (or link "") "][" name "]]")
271 (concat "[[" (or link "") "]]")))
273 (defun muse-edit-link-at-point ()
274 "Edit the current link.
275 Do not rename the page originally referred to."
277 (if (muse-link-at-point)
280 (read-string "Link: "
281 (muse-match-string-no-properties 1))
282 (read-string "Text: "
283 (muse-match-string-no-properties 2)))
285 (error "There is no valid link at point")))
287 (defun muse-visit-link-default (link &optional other-window anchor)
288 "Visit the URL or link named by LINK.
289 If ANCHOR is specified, search for it after opening LINK.
291 This is the default function to call when visiting links; it is
292 used by `muse-visit-link' if you have not specified :visit-link
293 in `muse-project-alist'."
294 (if (string-match muse-url-regexp link)
296 (let ((project (muse-project-of-file)))
298 (muse-project-find-file link project
300 'find-file-other-window))
302 (find-file-other-window link)
305 (search-forward anchor nil t))))
307 (defun muse-visit-link (link &optional other-window)
308 "Visit the URL or link named by LINK."
309 (let ((visit-link-function
310 (muse-get-keyword :visit-link (cadr (muse-project-of-file)) t))
312 (if visit-link-function
313 (funcall visit-link-function link other-window)
314 (if (string-match "#" link)
315 (setq anchor (substring link (match-beginning 0))
316 link (substring link 0 (match-beginning 0))))
317 (muse-visit-link-default link other-window anchor))))
319 (defun muse-browse-result (style &optional other-window)
320 "Visit the current page's published result."
321 (interactive (list (muse-publish-get-style) current-prefix-arg))
322 (setq style (muse-style style))
324 (muse-publish-output-file buffer-file-name
325 (muse-style-element :path style) style)))
326 (if (not (file-readable-p result-path))
327 (error "Cannot open output file '%s'" result-path)
329 (find-file-other-window result-path)
330 (let ((func (muse-style-element :browser style t)))
332 (funcall func result-path)
333 (message "The %s publishing style does not support browsing."
336 (defun muse-follow-name-at-point (&optional other-window)
337 "Visit the link at point, or insert a newline if none is found."
339 (let ((link (muse-link-at-point)))
341 (muse-visit-link link other-window)
342 (error "There is no valid link at point"))))
344 (defun muse-follow-name-at-point-other-window ()
345 "Visit the link at point in other window."
347 (muse-follow-name-at-point t))
349 (defun muse-follow-name-at-mouse (event &optional other-window)
350 "Visit the link at point, or yank text if none is found."
353 (cond ((fboundp 'event-window) ; XEmacs
354 (set-buffer (window-buffer (event-window event)))
355 (and (funcall (symbol-function 'event-point) event)
356 (goto-char (funcall (symbol-function 'event-point) event))))
357 ((fboundp 'posn-window) ; Emacs
358 (set-buffer (window-buffer (posn-window (event-start event))))
359 (goto-char (posn-point (event-start event)))))
360 (let ((link (muse-link-at-point)))
362 (muse-visit-link link other-window)
363 ;; Fall back to normal binding for this event
365 (lookup-key (current-global-map) (this-command-keys)))))))
367 (defun muse-follow-name-at-mouse-other-window (event)
368 "Visit the link at point"
370 ;; throw away the old window position, since other-window will
372 (select-window (car (cadr event)))
373 (muse-follow-name-at-mouse event t))
375 (defun muse-next-reference ()
376 "Move forward to next Muse link or URL, cycling if necessary."
378 (let ((cycled 0) pos)
380 (when (eq (get-text-property (point) 'face) 'muse-link-face)
381 (goto-char (or (next-single-property-change (point) 'face)
384 (let ((next (point)))
385 (if (while (and (null pos)
387 (next-single-property-change
389 (when (eq (get-text-property next 'face) 'muse-link-face)
392 (goto-char (point-min))
393 (setq cycled (1+ cycled))))))
397 (defun muse-previous-reference ()
398 "Move backward to the next Muse link or URL, cycling if necessary.
399 This function is not entirely accurate, but it's close enough."
401 (let ((cycled 0) pos)
404 (let ((prev (point)))
405 (if (while (and (null pos)
407 (previous-single-property-change
409 (when (eq (get-text-property prev 'face) 'muse-link-face)
412 (goto-char (point-max))
413 (setq cycled (1+ cycled))))))
417 (defun muse-what-changed ()
418 "Show the unsaved changes that have been made to the current file."
420 (diff-backup buffer-file-name))
422 ;;; Generate an index of all known Muse pages
424 (defun muse-generate-index (&optional as-list exclude-private)
425 "Generate an index of all Muse pages."
426 (let ((index (muse-index-as-string as-list exclude-private)))
427 (with-current-buffer (get-buffer-create "*Muse Index*")
433 "Display an index of all known Muse pages."
435 (message "Generating Muse index...")
436 (let ((project (muse-project)))
437 (with-current-buffer (muse-generate-index)
438 (goto-char (point-min))
440 (setq muse-current-project project)
441 (pop-to-buffer (current-buffer))))
442 (message "Generating Muse index...done"))
444 (defun muse-index-as-string (&optional as-list exclude-private)
445 "Generate an index of all Muse pages."
446 (let ((files (sort (copy-alist (muse-project-file-alist))
449 (string-lessp (car l) (car r)))))))
452 (unless (and exclude-private
453 (muse-project-private-p (cdar files)))
454 (insert (if as-list " - " "") "[[" (caar files) "]]\n"))
455 (setq files (cdr files)))
458 ;;; Insert tags interactively on C-c TAB
460 (defvar muse-tag-history nil
461 "List of recently-entered tags; used by `muse-insert-tag'.
462 If you want a tag to start as the default, you may manually set
463 this variable to a list.")
465 (defvar muse-custom-tags nil
466 "Keep track of any new tags entered in `muse-insert-tag'.
467 If there are (X)HTML tags that you use frequently with that
468 function, you might want to set this manually.")
470 (defun muse-insert-tag (tag)
471 "Insert a tag interactively with a blank line after it."
476 (when muse-tag-history
477 (concat "(default: " (car muse-tag-history) ") ")))
478 (mapcar 'list (nconc (mapcar 'car muse-publish-markup-tags)
480 nil nil nil 'muse-tag-history
481 (car muse-tag-history))))
483 (setq tag (car muse-tag-history)))
484 (let ((tag-entry (assoc tag muse-publish-markup-tags))
486 ;; Add to custom list if no entry exists
488 (add-to-list 'muse-custom-tags tag))
490 (when (nth 2 tag-entry)
491 (setq options (read-string "Option: ")))
492 (unless (equal options "")
493 (setq options (concat " " options)))
494 ;; Insert the tag, closing if necessary
495 (when tag (insert (concat "<" tag options ">")))
496 (when (nth 1 tag-entry)
497 (insert (concat "\n\n</" tag ">\n"))
502 ;;; muse-mode.el ends here