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."
60 (defcustom muse-mode-highlight-p t
61 "If non-nil, highlight the content of Muse buffers."
66 (defcustom muse-mode-auto-p t
67 "If non-nil, automagically determine when Muse mode should be activated."
72 (add-hook 'find-file-hooks
'muse-mode-maybe
)
73 (remove-hook 'find-file-hooks
'muse-mode-maybe
))
77 (defcustom muse-mode-intangible-links nil
78 "If non-nil, use the intangible property on links.
79 This can cause problems with flyspell (and potentially fill-mode),
80 so only enable this if you don't use either of these."
84 (defcustom muse-mode-hook nil
85 "A hook that is run when Muse mode is entered."
87 :options
'(flyspell-mode footnote-mode turn-on-auto-fill
88 highlight-changes-mode
)
89 :set
#'(lambda (sym val
)
90 (when (featurep 'muse-wiki
)
91 (add-to-list 'val
'muse-wiki-update-custom-values
))
96 (let ((map (make-sparse-keymap)))
97 (define-key map
[(control ?c
) (control ?a
)] 'muse-index
)
98 (define-key map
[(control ?c
) (control ?b
)] 'muse-browse-result
)
99 (define-key map
[(control ?c
) (control ?c
)] 'muse-follow-name-at-point
)
100 (define-key map
[(control ?c
) (control ?e
)] 'muse-edit-link-at-point
)
101 (define-key map
[(control ?c
) (control ?t
)] 'muse-publish-this-file
)
102 (define-key map
[(control ?c
) (control ?v
)] 'muse-follow-name-at-point
)
104 (define-key map
[(control ?c
) (control ?l
)] 'font-lock-mode
)
106 (define-key map
[(control ?c
) ?
=] 'muse-what-changed
)
108 (define-key map
[tab] 'muse-next-reference)
109 (define-key map [(control ?i)] 'muse-next-reference)
111 (if (featurep 'xemacs)
113 (define-key map [(button2)] 'muse-follow-name-at-mouse)
114 (define-key map [(shift button2)]
115 'muse-follow-name-at-mouse-other-window))
116 (define-key map [(shift control ?m)]
117 'muse-follow-name-at-point-other-window)
118 (define-key map [mouse-2] 'muse-follow-name-at-mouse)
119 (define-key map [(shift mouse-2)]
120 'muse-follow-name-at-mouse-other-window))
122 (define-key map [(shift tab)] 'muse-previous-reference)
123 (unless (featurep 'xemacs)
124 (define-key map [(shift iso-lefttab)] 'muse-previous-reference)
125 (define-key map [(shift control ?i)] 'muse-previous-reference))
127 (define-key map [(control ?c) (control ?f)] 'muse-project-find-file)
128 (define-key map [(control ?c) (control ?p)] 'muse-project-publish)
130 (define-key map [(control ?c) tab] 'muse-insert-tag)
131 (define-key map [(control ?c) (control ?i)] 'muse-insert-tag)
133 (when (featurep 'pcomplete)
134 (define-key map [(meta tab)] 'pcomplete)
135 (define-key map [(meta control ?i)] 'pcomplete))
138 "Keymap used by Emacs Muse mode.")
143 (define-derived-mode muse-mode text-mode "Muse"
144 "Muse is an Emacs mode for authoring and publishing documents.
146 ;; Since we're not inheriting from normal-mode, we need to
147 ;; explicitly run file variables.
149 (hack-local-variables)
150 (error (message "File local-variables error: %s"
151 (prin1-to-string err))))
152 ;; Avoid lock-up caused by use of the 'intangible' text property
154 (unless muse-mode-intangible-links
155 (set (make-local-variable 'inhibit-point-motion-hooks) t))
156 (setq muse-current-project (muse-project-of-file))
157 (muse-project-set-variables)
158 ;; Make sure several variables get updated if the user has changed
159 ;; them without using the customize interface.
160 (muse-update-ignored-extensions-regexp 'muse-ignored-extensions
161 muse-ignored-extensions)
162 ;; Make fill not split up links
163 (when (boundp 'fill-nobreak-predicate)
164 (make-local-variable 'fill-nobreak-predicate)
165 ;; Work around annoying inconsistency in fill handling between
167 (if (< emacs-major-version 22)
168 (setq fill-nobreak-predicate 'muse-mode-fill-nobreak-p)
169 (add-to-list 'fill-nobreak-predicate
170 'muse-mode-fill-nobreak-p)))
171 ;; Make fill work nicely with item lists
172 (set (make-local-variable 'adaptive-fill-regexp)
173 "\\s-+\\(-\\|[0-9]+\\.\\)\\s-+\\|\\[[0-9]+\\]\\s-*\\|\\s-*")
174 (set (make-local-variable 'paragraph-start)
175 (concat paragraph-start "\\|\\s-+\\(-\\|[0-9]+\\.\\)\\s-+"
176 "\\|\\[[0-9]+\\]\\s-*"))
177 (when (featurep 'pcomplete)
178 ;; If pcomplete is available, set it up
179 (set (make-local-variable 'pcomplete-default-completion-function)
180 'muse-mode-completions)
181 (set (make-local-variable 'pcomplete-command-completion-function)
182 'muse-mode-completions)
183 (set (make-local-variable 'pcomplete-parse-arguments-function)
184 'muse-mode-current-word))
185 (when muse-mode-highlight-p
186 (muse-use-font-lock)))
189 'flyspell-mode-predicate
190 'muse-mode-flyspell-p)
192 (defun muse-mode-fill-nobreak-p ()
193 "Return nil if we should allow a fill to occur at point.
194 Otherwise return non-nil.
196 This is used to keep long explicit links from being mangled by
200 (and (re-search-backward "\\[\\[\\|\\]\\]"
201 (line-beginning-position) t)
202 (string= (or (match-string 0) "")
205 (defun muse-mode-flyspell-p ()
206 "Return non-nil if we should allow spell-checking to occur at point.
207 Otherwise return nil.
209 This is used to keep links from being improperly colorized by flyspell."
211 (null (muse-link-at-point))))
213 (defun muse-mode-choose-mode ()
214 "Turn the proper Emacs Muse related mode on for this file."
215 (let ((project (muse-project-of-file)))
216 (funcall (or (and project (muse-get-keyword :major-mode (cadr project) t))
219 (defun muse-mode-maybe ()
220 "Maybe turn Emacs Muse mode on for this file."
221 (let ((project (muse-project-of-file)))
223 (funcall (or (muse-get-keyword :major-mode (cadr project) t)
226 ;;; Support page name completion using pcomplete
228 (defun muse-completions ()
229 "Return a list of possible completions names for this buffer."
230 (let ((project (muse-project-of-file)))
232 (while (pcomplete-here
233 (mapcar 'car (muse-project-file-alist project)))))))
235 (defun muse-current-word ()
239 (skip-chars-backward (concat "^\\["
241 (narrow-to-region (point) end))
242 (pcomplete-parse-buffer-arguments))))
244 ;;; Navigate/visit links or URLs. Use TAB, S-TAB and RET (or mouse-2).
246 (defun muse-link-at-point (&optional pos)
247 "Return link text if a URL or link is at point."
248 (let ((case-fold-search nil)
249 (here (or pos (point))))
251 (and (char-after pos)
252 (not (eq (char-syntax (char-after pos)) ?\ ))))
255 ;; Check for explicit link here or before point
256 (if (or (looking-at muse-explicit-link-regexp)
258 (re-search-backward "\\[\\[\\|\\]\\]"
259 (muse-line-beginning-position)
261 (string= (or (match-string 0) "") "[[")
262 (looking-at muse-explicit-link-regexp)))
264 (goto-char (match-beginning 1))
265 (muse-handle-explicit-link))
267 ;; Check for bare URL or other link type
268 (skip-chars-backward (concat "^'\"<>{}(\n"
270 (and (looking-at muse-implicit-link-regexp)
271 (muse-handle-implicit-link)))))))
273 (defun muse-make-link (link &optional name)
274 "Return a link to LINK with NAME as the text."
277 (not (string= name ""))
278 (not (string= link name)))
279 (concat "[[" (or link "") "][" name "]]")
280 (concat "[[" (or link "") "]]")))
282 (defun muse-edit-link-at-point ()
283 "Edit the current link.
284 Do not rename the page originally referred to."
286 (if (muse-link-at-point)
289 (read-string "Link: "
290 (muse-match-string-no-properties 1))
291 (read-string "Text: "
292 (muse-match-string-no-properties 2)))
294 (error "There is no valid link at point")))
296 (defun muse-visit-link-default (link &optional other-window anchor)
297 "Visit the URL or link named by LINK.
298 If ANCHOR is specified, search for it after opening LINK.
300 This is the default function to call when visiting links; it is
301 used by `muse-visit-link' if you have not specified :visit-link
302 in `muse-project-alist'."
303 (if (string-match muse-url-regexp link)
305 (let ((project (muse-project-of-file)))
307 (muse-project-find-file link project
309 'find-file-other-window))
311 (find-file-other-window link)
314 (search-forward anchor nil t))))
316 (defun muse-visit-link (link &optional other-window)
317 "Visit the URL or link named by LINK."
318 (let ((visit-link-function
319 (muse-get-keyword :visit-link (cadr (muse-project-of-file)) t))
321 (if visit-link-function
322 (funcall visit-link-function link other-window)
323 (if (string-match "#" link)
324 (setq anchor (substring link (match-beginning 0))
325 link (substring link 0 (match-beginning 0))))
326 (muse-visit-link-default link other-window anchor))))
328 (defun muse-browse-result (style &optional other-window)
329 "Visit the current page's published result."
330 (interactive (list (muse-publish-get-style) current-prefix-arg))
331 (setq style (muse-style style))
333 (muse-publish-output-file buffer-file-name
334 (muse-style-element :path style) style)))
335 (if (not (file-readable-p result-path))
336 (error "Cannot open output file '%s'" result-path)
338 (find-file-other-window result-path)
339 (let ((func (muse-style-element :browser style t)))
341 (funcall func result-path)
342 (message "The %s publishing style does not support browsing."
345 (defun muse-follow-name-at-point (&optional other-window)
346 "Visit the link at point, or insert a newline if none is found."
348 (let ((link (muse-link-at-point)))
350 (muse-visit-link link other-window)
351 (error "There is no valid link at point"))))
353 (defun muse-follow-name-at-point-other-window ()
354 "Visit the link at point in other window."
356 (muse-follow-name-at-point t))
358 (defun muse-follow-name-at-mouse (event &optional other-window)
359 "Visit the link at point, or yank text if none is found."
362 (cond ((fboundp 'event-window) ; XEmacs
363 (set-buffer (window-buffer (event-window event)))
364 (and (funcall (symbol-function 'event-point) event)
365 (goto-char (funcall (symbol-function 'event-point) event))))
366 ((fboundp 'posn-window) ; Emacs
367 (set-buffer (window-buffer (posn-window (event-start event))))
368 (goto-char (posn-point (event-start event)))))
369 (let ((link (muse-link-at-point)))
371 (muse-visit-link link other-window)
372 ;; Fall back to normal binding for this event
374 (lookup-key (current-global-map) (this-command-keys)))))))
376 (defun muse-follow-name-at-mouse-other-window (event)
377 "Visit the link at point"
379 ;; throw away the old window position, since other-window will
381 (select-window (car (cadr event)))
382 (muse-follow-name-at-mouse event t))
384 (defun muse-next-reference ()
385 "Move forward to next Muse link or URL, cycling if necessary."
387 (let ((cycled 0) pos)
389 (when (memq (get-text-property (point) 'face)
390 '(muse-link-face muse-bad-link-face))
391 (goto-char (or (next-single-property-change (point) 'face)
394 (let ((next (point)))
395 (if (while (and (null pos)
397 (next-single-property-change
399 (when (memq (get-text-property next 'face)
400 '(muse-link-face muse-bad-link-face))
403 (goto-char (point-min))
404 (setq cycled (1+ cycled))))))
408 (defun muse-previous-reference ()
409 "Move backward to the next Muse link or URL, cycling if necessary.
410 This function is not entirely accurate, but it's close enough."
412 (let ((cycled 0) pos)
415 (let ((prev (point)))
416 (if (while (and (null pos)
418 (previous-single-property-change
420 (when (memq (get-text-property prev 'face)
421 '(muse-link-face muse-bad-link-face))
424 (goto-char (point-max))
425 (setq cycled (1+ cycled))))))
429 (defun muse-what-changed ()
430 "Show the unsaved changes that have been made to the current file."
432 (diff-backup buffer-file-name))
434 ;;; Generate an index of all known Muse pages
436 (defun muse-generate-index (&optional as-list exclude-private)
437 "Generate an index of all Muse pages."
438 (let ((index (muse-index-as-string as-list exclude-private)))
439 (with-current-buffer (get-buffer-create "*Muse Index*")
445 "Display an index of all known Muse pages."
447 (message "Generating Muse index...")
448 (let ((project (muse-project)))
449 (with-current-buffer (muse-generate-index)
450 (goto-char (point-min))
452 (setq muse-current-project project)
453 (pop-to-buffer (current-buffer))))
454 (message "Generating Muse index...done"))
456 (defun muse-index-as-string (&optional as-list exclude-private exclude-current)
457 "Generate an index of all Muse pages.
458 If AS-LIST is non-nil, insert a dash and spaces before each item.
459 If EXCLUDE-PRIVATE is non-nil, exclude files that have private permissions.
460 If EXCLUDE-CURRENT is non-nil, exclude the current file from the output."
461 (let ((files (sort (copy-alist (muse-project-file-alist))
464 (string-lessp (car l) (car r)))))))
465 (when (and exclude-current (muse-page-name))
466 (setq files (delete (assoc (muse-page-name) files) files)))
467 (muse-with-temp-buffer
469 (unless (and exclude-private
470 (muse-project-private-p (cdar files)))
471 (insert (if as-list " - " "") "[[" (caar files) "]]\n"))
472 (setq files (cdr files)))
475 ;;; Insert tags interactively on C-c TAB
477 (defvar muse-tag-history nil
478 "List of recently-entered tags; used by `muse-insert-tag'.
479 If you want a tag to start as the default, you may manually set
480 this variable to a list.")
482 (defvar muse-custom-tags nil
483 "Keep track of any new tags entered in `muse-insert-tag'.
484 If there are (X)HTML tags that you use frequently with that
485 function, you might want to set this manually.")
487 (defun muse-insert-tag (tag)
488 "Insert a tag interactively with a blank line after it."
493 (when muse-tag-history
494 (concat "(default: " (car muse-tag-history) ") ")))
495 (mapcar 'list (nconc (mapcar 'car muse-publish-markup-tags)
497 nil nil nil 'muse-tag-history
498 (car muse-tag-history))))
500 (setq tag (car muse-tag-history)))
501 (let ((tag-entry (assoc tag muse-publish-markup-tags))
503 ;; Add to custom list if no entry exists
505 (add-to-list 'muse-custom-tags tag))
507 (when (nth 2 tag-entry)
508 (setq options (read-string "Option: ")))
509 (unless (equal options "")
510 (setq options (concat " " options)))
511 ;; Insert the tag, closing if necessary
512 (when tag (insert (concat "<" tag options ">")))
513 (when (nth 1 tag-entry)
514 (insert (concat "\n\n</" tag ">\n"))
519 ;;; muse-mode.el ends here