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
)
90 :set
#'(lambda (sym val
)
91 (when (featurep 'muse-wiki
)
92 (add-to-list 'val
'muse-wiki-update-custom-values
))
97 (let ((map (make-sparse-keymap)))
98 (define-key map
[(control ?c
) (control ?a
)] 'muse-index
)
99 (define-key map
[(control ?c
) (control ?b
)] 'muse-browse-result
)
100 (define-key map
[(control ?c
) (control ?c
)] 'muse-follow-name-at-point
)
101 (define-key map
[(control ?c
) (control ?e
)] 'muse-edit-link-at-point
)
102 (define-key map
[(control ?c
) (control ?t
)] 'muse-publish-this-file
)
103 (define-key map
[(control ?c
) (control ?v
)] 'muse-follow-name-at-point
)
105 (define-key map
[(control ?c
) (control ?l
)] 'font-lock-mode
)
107 (define-key map
[(control ?c
) ?
=] 'muse-what-changed
)
109 (define-key map
[tab] 'muse-next-reference)
110 (define-key map [(control ?i)] 'muse-next-reference)
112 (if (featurep 'xemacs)
114 (define-key map [(button2)] 'muse-follow-name-at-mouse)
115 (define-key map [(shift button2)]
116 'muse-follow-name-at-mouse-other-window))
117 (define-key map [(shift control ?m)]
118 'muse-follow-name-at-point-other-window)
119 (define-key map [mouse-2] 'muse-follow-name-at-mouse)
120 (define-key map [(shift mouse-2)]
121 'muse-follow-name-at-mouse-other-window))
123 (define-key map [(shift tab)] 'muse-previous-reference)
124 (unless (featurep 'xemacs)
125 (define-key map [(shift iso-lefttab)] 'muse-previous-reference)
126 (define-key map [(shift control ?i)] 'muse-previous-reference))
128 (define-key map [(control ?c) (control ?f)] 'muse-project-find-file)
129 (define-key map [(control ?c) (control ?p)] 'muse-project-publish)
131 (define-key map [(control ?c) tab] 'muse-insert-tag)
132 (define-key map [(control ?c) (control ?i)] 'muse-insert-tag)
134 (when (featurep 'pcomplete)
135 (define-key map [(meta tab)] 'pcomplete)
136 (define-key map [(meta control ?i)] 'pcomplete))
139 "Keymap used by Emacs Muse mode.")
144 (define-derived-mode muse-mode text-mode "Muse"
145 "Muse is an Emacs mode for authoring and publishing documents.
147 ;; Since we're not inheriting from normal-mode, we need to
148 ;; explicitly run file variables.
150 (hack-local-variables)
151 (error (message "File local-variables error: %s"
152 (prin1-to-string err))))
153 ;; Avoid lock-up caused by use of the 'intangible' text property
155 (unless muse-mode-intangible-links
156 (set (make-local-variable 'inhibit-point-motion-hooks) t))
157 (setq muse-current-project (muse-project-of-file))
158 (muse-project-set-variables)
159 ;; Make sure several variables get updated if the user has changed
160 ;; them without using the customize interface.
161 (muse-update-ignored-extensions-regexp 'muse-ignored-extensions
162 muse-ignored-extensions)
163 ;; Make fill not split up links
164 (when (boundp 'fill-nobreak-predicate)
165 (make-local-variable 'fill-nobreak-predicate)
166 ;; Work around annoying inconsistency in fill handling between
167 ;; Emacs CVS and all other Emacs types.
168 (if (not (muse-extreg-usable-p))
169 (setq fill-nobreak-predicate 'muse-mode-fill-nobreak-p)
170 (add-to-list 'fill-nobreak-predicate
171 'muse-mode-fill-nobreak-p)))
172 ;; Make adaptive fill work nicely with item lists
173 (set (make-local-variable 'adaptive-fill-regexp)
174 (concat "[" muse-regexp-blank "]*\\(-+["
177 muse-regexp-blank "]*\\)*"))
178 (when (featurep 'pcomplete)
179 ;; If pcomplete is available, set it up
180 (set (make-variable-buffer-local 'pcomplete-default-completion-function)
181 'muse-mode-completions)
182 (set (make-variable-buffer-local 'pcomplete-command-completion-function)
183 'muse-mode-completions)
184 (set (make-variable-buffer-local 'pcomplete-parse-arguments-function)
185 'muse-mode-current-word))
186 (when muse-mode-highlight-p
187 (muse-use-font-lock)))
190 'flyspell-mode-predicate
191 'muse-mode-flyspell-p)
193 (defun muse-mode-fill-nobreak-p ()
194 "Return nil if we should allow a fill to occur at point.
195 Otherwise return non-nil.
197 This is used to keep long explicit links from being mangled by
201 (and (re-search-backward "\\[\\[\\|\\]\\]"
202 (line-beginning-position) t)
203 (string= (or (match-string 0) "")
206 (defun muse-mode-flyspell-p ()
207 "Return non-nil if we should allow spell-checking to occur at point.
208 Otherwise return nil.
210 This is used to keep links from being improperly colorized by flyspell."
212 (null (muse-link-at-point))))
214 (defun muse-mode-choose-mode ()
215 "Turn the proper Emacs Muse related mode on for this file."
216 (let ((project (muse-project-of-file)))
217 (funcall (or (and project (muse-get-keyword :major-mode (cadr project) t))
220 (defun muse-mode-maybe ()
221 "Maybe turn Emacs Muse mode on for this file."
222 (let ((project (muse-project-of-file)))
224 (funcall (or (muse-get-keyword :major-mode (cadr project) t)
227 ;;; Support page name completion using pcomplete
229 (defun muse-completions ()
230 "Return a list of possible completions names for this buffer."
231 (let ((project (muse-project-of-file)))
233 (while (pcomplete-here
234 (mapcar 'car (muse-project-file-alist project)))))))
236 (defun muse-current-word ()
240 (skip-chars-backward (concat "^\\["
242 (narrow-to-region (point) end))
243 (pcomplete-parse-buffer-arguments))))
245 ;;; Navigate/visit links or URLs. Use TAB, S-TAB and RET (or mouse-2).
247 (defun muse-link-at-point (&optional pos)
248 "Return link text if a URL or link is at point."
249 (let ((case-fold-search nil)
250 (here (or pos (point))))
252 (and (char-after pos)
253 (not (eq (char-syntax (char-after pos)) ?\ ))))
256 ;; Check for explicit link here or before point
257 (if (or (looking-at muse-explicit-link-regexp)
259 (re-search-backward "\\[\\[\\|\\]\\]"
260 (muse-line-beginning-position)
262 (string= (or (match-string 0) "") "[[")
263 (looking-at muse-explicit-link-regexp)))
265 (goto-char (match-beginning 1))
266 (muse-handle-explicit-link))
268 ;; Check for bare URL or other link type
269 (skip-chars-backward (concat "^'\"<>{}(\n"
271 (and (looking-at muse-implicit-link-regexp)
272 (muse-handle-implicit-link)))))))
274 (defun muse-make-link (link &optional name)
275 "Return a link to LINK with NAME as the text."
278 (not (string= name ""))
279 (not (string= link name)))
280 (concat "[[" (or link "") "][" name "]]")
281 (concat "[[" (or link "") "]]")))
283 (defun muse-edit-link-at-point ()
284 "Edit the current link.
285 Do not rename the page originally referred to."
287 (if (muse-link-at-point)
290 (read-string "Link: "
291 (muse-match-string-no-properties 1))
292 (read-string "Text: "
293 (muse-match-string-no-properties 2)))
295 (error "There is no valid link at point")))
297 (defun muse-visit-link-default (link &optional other-window anchor)
298 "Visit the URL or link named by LINK.
299 If ANCHOR is specified, search for it after opening LINK.
301 This is the default function to call when visiting links; it is
302 used by `muse-visit-link' if you have not specified :visit-link
303 in `muse-project-alist'."
304 (if (string-match muse-url-regexp link)
306 (let ((project (muse-project-of-file)))
308 (muse-project-find-file link project
310 'find-file-other-window))
312 (find-file-other-window link)
315 (search-forward anchor nil t))))
317 (defun muse-visit-link (link &optional other-window)
318 "Visit the URL or link named by LINK."
319 (let ((visit-link-function
320 (muse-get-keyword :visit-link (cadr (muse-project-of-file)) t))
322 (if visit-link-function
323 (funcall visit-link-function link other-window)
324 (if (string-match "#" link)
325 (setq anchor (substring link (match-beginning 0))
326 link (substring link 0 (match-beginning 0))))
327 (muse-visit-link-default link other-window anchor))))
329 (defun muse-browse-result (style &optional other-window)
330 "Visit the current page's published result."
331 (interactive (list (muse-publish-get-style) current-prefix-arg))
332 (setq style (muse-style style))
334 (muse-publish-output-file buffer-file-name
335 (muse-style-element :path style) style)))
336 (if (not (file-readable-p result-path))
337 (error "Cannot open output file '%s'" result-path)
339 (find-file-other-window result-path)
340 (let ((func (muse-style-element :browser style t)))
342 (funcall func result-path)
343 (message "The %s publishing style does not support browsing."
346 (defun muse-follow-name-at-point (&optional other-window)
347 "Visit the link at point, or insert a newline if none is found."
349 (let ((link (muse-link-at-point)))
351 (muse-visit-link link other-window)
352 (error "There is no valid link at point"))))
354 (defun muse-follow-name-at-point-other-window ()
355 "Visit the link at point in other window."
357 (muse-follow-name-at-point t))
359 (defun muse-follow-name-at-mouse (event &optional other-window)
360 "Visit the link at point, or yank text if none is found."
363 (cond ((fboundp 'event-window) ; XEmacs
364 (set-buffer (window-buffer (event-window event)))
365 (and (funcall (symbol-function 'event-point) event)
366 (goto-char (funcall (symbol-function 'event-point) event))))
367 ((fboundp 'posn-window) ; Emacs
368 (set-buffer (window-buffer (posn-window (event-start event))))
369 (goto-char (posn-point (event-start event)))))
370 (let ((link (muse-link-at-point)))
372 (muse-visit-link link other-window)
373 ;; Fall back to normal binding for this event
375 (lookup-key (current-global-map) (this-command-keys)))))))
377 (defun muse-follow-name-at-mouse-other-window (event)
378 "Visit the link at point"
380 ;; throw away the old window position, since other-window will
382 (select-window (car (cadr event)))
383 (muse-follow-name-at-mouse event t))
385 (defun muse-next-reference ()
386 "Move forward to next Muse link or URL, cycling if necessary."
388 (let ((cycled 0) pos)
390 (when (memq (get-text-property (point) 'face)
391 '(muse-link-face muse-bad-link-face))
392 (goto-char (or (next-single-property-change (point) 'face)
395 (let ((next (point)))
396 (if (while (and (null pos)
398 (next-single-property-change
400 (when (memq (get-text-property next 'face)
401 '(muse-link-face muse-bad-link-face))
404 (goto-char (point-min))
405 (setq cycled (1+ cycled))))))
409 (defun muse-previous-reference ()
410 "Move backward to the next Muse link or URL, cycling if necessary.
411 This function is not entirely accurate, but it's close enough."
413 (let ((cycled 0) pos)
416 (let ((prev (point)))
417 (if (while (and (null pos)
419 (previous-single-property-change
421 (when (memq (get-text-property prev 'face)
422 '(muse-link-face muse-bad-link-face))
425 (goto-char (point-max))
426 (setq cycled (1+ cycled))))))
430 (defun muse-what-changed ()
431 "Show the unsaved changes that have been made to the current file."
433 (diff-backup buffer-file-name))
435 ;;; Generate an index of all known Muse pages
437 (defun muse-generate-index (&optional as-list exclude-private)
438 "Generate an index of all Muse pages."
439 (let ((index (muse-index-as-string as-list exclude-private)))
440 (with-current-buffer (get-buffer-create "*Muse Index*")
446 "Display an index of all known Muse pages."
448 (message "Generating Muse index...")
449 (let ((project (muse-project)))
450 (with-current-buffer (muse-generate-index)
451 (goto-char (point-min))
453 (setq muse-current-project project)
454 (pop-to-buffer (current-buffer))))
455 (message "Generating Muse index...done"))
457 (defun muse-index-as-string (&optional as-list exclude-private exclude-current)
458 "Generate an index of all Muse pages.
459 If AS-LIST is non-nil, insert a dash and spaces before each item.
460 If EXCLUDE-PRIVATE is non-nil, exclude files that have private permissions.
461 If EXCLUDE-CURRENT is non-nil, exclude the current file from the output."
462 (let ((files (sort (copy-alist (muse-project-file-alist))
465 (string-lessp (car l) (car r)))))))
466 (when (and exclude-current (muse-page-name))
467 (setq files (delete (assoc (muse-page-name) files) files)))
468 (muse-with-temp-buffer
470 (unless (and exclude-private
471 (muse-project-private-p (cdar files)))
472 (insert (if as-list " - " "") "[[" (caar files) "]]\n"))
473 (setq files (cdr files)))
476 ;;; Insert tags interactively on C-c TAB
478 (defvar muse-tag-history nil
479 "List of recently-entered tags; used by `muse-insert-tag'.
480 If you want a tag to start as the default, you may manually set
481 this variable to a list.")
483 (defvar muse-custom-tags nil
484 "Keep track of any new tags entered in `muse-insert-tag'.
485 If there are (X)HTML tags that you use frequently with that
486 function, you might want to set this manually.")
488 (defun muse-insert-tag (tag)
489 "Insert a tag interactively with a blank line after it."
494 (when muse-tag-history
495 (concat "(default: " (car muse-tag-history) ") ")))
496 (mapcar 'list (nconc (mapcar 'car muse-publish-markup-tags)
498 nil nil nil 'muse-tag-history
499 (car muse-tag-history))))
501 (setq tag (car muse-tag-history)))
502 (let ((tag-entry (assoc tag muse-publish-markup-tags))
504 ;; Add to custom list if no entry exists
506 (add-to-list 'muse-custom-tags tag))
508 (when (nth 2 tag-entry)
509 (setq options (read-string "Option: ")))
510 (unless (equal options "")
511 (setq options (concat " " options)))
512 ;; Insert the tag, closing if necessary
513 (when tag (insert (concat "<" tag options ">")))
514 (when (nth 1 tag-entry)
515 (insert (concat "\n\n</" tag ">\n"))
520 ;;; muse-mode.el ends here