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 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.
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 nil
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 (muse-update-url-regexp 'muse-url-protocols muse-url-protocols)
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
168 (if (< emacs-major-version 22)
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 fill work nicely with item lists
173 (set (make-local-variable 'adaptive-fill-regexp)
174 "\\s-+\\(-\\|[0-9]+\\.\\)\\s-+\\|\\[[0-9]+\\]\\s-*\\|\\s-*")
175 (set (make-local-variable 'paragraph-start)
176 (concat paragraph-start "\\|\\s-+\\(-\\|[0-9]+\\.\\)\\s-+"
177 "\\|\\[[0-9]+\\]\\s-*"))
178 ;; Comment syntax is `; comment'
179 (set (make-local-variable 'comment-start)
181 (set (make-local-variable 'comment-start-skip)
183 ;; If we're using Emacs21, this makes flyspell work like it should
184 (when (boundp 'flyspell-generic-check-word-p)
185 (set (make-local-variable 'flyspell-generic-check-word-p)
186 'muse-mode-flyspell-p))
187 ;; If pcomplete is available, set it up
188 (when (featurep 'pcomplete)
189 (set (make-local-variable 'pcomplete-default-completion-function)
190 'muse-mode-completions)
191 (set (make-local-variable 'pcomplete-command-completion-function)
192 'muse-mode-completions)
193 (set (make-local-variable 'pcomplete-parse-arguments-function)
194 'muse-mode-current-word))
195 (when muse-mode-highlight-p
196 (muse-use-font-lock)))
199 'flyspell-mode-predicate
200 'muse-mode-flyspell-p)
202 (defun muse-mode-fill-nobreak-p ()
203 "Return nil if we should allow a fill to occur at point.
204 Otherwise return non-nil.
206 This is used to keep long explicit links from being mangled by
210 (and (re-search-backward "\\[\\[\\|\\]\\]"
211 (line-beginning-position) t)
212 (string= (or (match-string 0) "")
215 (defun muse-mode-flyspell-p ()
216 "Return non-nil if we should allow spell-checking to occur at point.
217 Otherwise return nil.
219 This is used to keep links from being improperly colorized by flyspell."
221 (null (muse-link-at-point))))
223 (defun muse-mode-choose-mode ()
224 "Turn the proper Emacs Muse related mode on for this file."
225 (let ((project (muse-project-of-file)))
226 (funcall (or (and project (muse-get-keyword :major-mode (cadr project) t))
229 (defun muse-mode-maybe ()
230 "Maybe turn Emacs Muse mode on for this file."
231 (let ((project (muse-project-of-file)))
233 (funcall (or (muse-get-keyword :major-mode (cadr project) t)
236 ;;; Support page name completion using pcomplete
238 (defun muse-completions ()
239 "Return a list of possible completions names for this buffer."
240 (let ((project (muse-project-of-file)))
242 (while (pcomplete-here
243 (mapcar 'car (muse-project-file-alist project)))))))
245 (defun muse-current-word ()
249 (skip-chars-backward (concat "^\\[\n" muse-regexp-blank))
250 (narrow-to-region (point) end))
251 (pcomplete-parse-buffer-arguments))))
253 ;;; Navigate/visit links or URLs. Use TAB, S-TAB and RET (or mouse-2).
255 (defun muse-link-at-point (&optional pos)
256 "Return link text if a URL or link is at point."
257 (let ((case-fold-search nil)
258 (inhibit-point-motion-hooks t)
259 (here (or pos (point))))
261 (and (char-after pos)
262 (not (eq (char-syntax (char-after pos)) ?\ ))))
265 ;; Check for explicit link here or before point
266 (if (or (looking-at muse-explicit-link-regexp)
268 (re-search-backward "\\[\\[\\|\\]\\]"
269 (muse-line-beginning-position)
271 (string= (or (match-string 0) "") "[[")
272 (looking-at muse-explicit-link-regexp)))
274 (goto-char (match-beginning 1))
275 (muse-handle-explicit-link))
277 ;; Check for bare URL or other link type
278 (skip-chars-backward (concat "^'\"<>{}(\n" muse-regexp-blank))
279 (and (looking-at muse-implicit-link-regexp)
280 (muse-handle-implicit-link)))))))
282 (defun muse-make-link (link &optional name)
283 "Return a link to LINK with NAME as the text."
284 (when (string-match muse-explicit-link-regexp link)
285 (unless name (setq name (match-string 2 link)))
286 (setq link (match-string 1 link)))
289 (not (string= name ""))
290 (not (string= link name)))
291 (concat "[[" (muse-link-escape link) "][" (muse-link-escape name) "]]")
292 (concat "[[" (muse-link-escape link) "]]")))
295 (defun muse-edit-link-at-point ()
296 "Edit the current link.
297 Do not rename the page originally referred to."
299 (if (muse-link-at-point)
303 (read-string "Link: "
304 (muse-match-string-no-properties 1))
305 (read-string "Text: "
306 (muse-match-string-no-properties 2))))
308 (error "There is no valid link at point")))
310 (defun muse-visit-link-default (link &optional other-window)
311 "Visit the URL or link named by LINK.
312 If ANCHOR is specified, search for it after opening LINK.
314 This is the default function to call when visiting links; it is
315 used by `muse-visit-link' if you have not specified :visit-link
316 in `muse-project-alist'."
317 (if (string-match muse-url-regexp link)
318 (muse-browse-url link)
321 (when (string-match "#" link)
322 (setq anchor (substring link (match-beginning 0))
323 link (if (= (match-beginning 0) 0)
324 ;; If there is an anchor but no link, default
325 ;; to the current page.
327 (substring link 0 (match-beginning 0)))))
329 (setq base-buffer (get-buffer link))
330 (if (and base-buffer (not (buffer-file-name base-buffer)))
331 ;; If file is temporary (no associated file), just switch to
334 (switch-to-buffer-other-window base-buffer)
335 (switch-to-buffer base-buffer))
336 (let ((project (muse-project-of-file)))
338 (muse-project-find-file link project
340 'find-file-other-window))
342 (find-file-other-window link)
343 (find-file link))))))
346 (goto-char (point-min))
347 (unless (re-search-forward (concat "^\\W*" (regexp-quote anchor)
350 (goto-char pos)))))))
352 (defun muse-visit-link (link &optional other-window)
353 "Visit the URL or link named by LINK."
354 (let ((visit-link-function
355 (muse-get-keyword :visit-link (cadr (muse-project-of-file)) t)))
356 (if visit-link-function
357 (funcall visit-link-function link other-window)
358 (muse-visit-link-default link other-window))))
361 (defun muse-browse-result (style &optional other-window)
362 "Visit the current page's published result."
363 (interactive (list (muse-publish-get-style) current-prefix-arg))
364 (setq style (muse-style style))
366 (muse-publish-output-file buffer-file-name
367 (muse-style-element :path style) style)))
368 (if (not (file-readable-p result-path))
369 (error "Cannot open output file '%s'" result-path)
371 (find-file-other-window result-path)
372 (let ((func (muse-style-element :browser style t)))
374 (funcall func result-path)
375 (message "The %s publishing style does not support browsing."
379 (defun muse-follow-name-at-point (&optional other-window)
380 "Visit the link at point, or insert a newline if none is found."
382 (let ((link (muse-link-at-point)))
384 (muse-visit-link link other-window)
385 (error "There is no valid link at point"))))
388 (defun muse-follow-name-at-point-other-window ()
389 "Visit the link at point in other window."
391 (muse-follow-name-at-point t))
393 (defun muse-follow-name-at-mouse (event &optional other-window)
394 "Visit the link at point, or yank text if none is found."
397 (cond ((fboundp 'event-window) ; XEmacs
398 (set-buffer (window-buffer (event-window event)))
399 (and (funcall (symbol-function 'event-point) event)
400 (goto-char (funcall (symbol-function 'event-point) event))))
401 ((fboundp 'posn-window) ; Emacs
402 (set-buffer (window-buffer (posn-window (event-start event))))
403 (goto-char (posn-point (event-start event)))))
404 (let ((link (muse-link-at-point)))
406 (muse-visit-link link other-window)
407 ;; Fall back to normal binding for this event
409 (lookup-key (current-global-map) (this-command-keys)))))))
411 (defun muse-follow-name-at-mouse-other-window (event)
412 "Visit the link at point"
414 ;; throw away the old window position, since other-window will
416 (select-window (car (cadr event)))
417 (muse-follow-name-at-mouse event t))
420 (defun muse-next-reference ()
421 "Move forward to next Muse link or URL, cycling if necessary."
423 (let ((cycled 0) pos)
425 (when (memq (get-text-property (point) 'face)
426 '(muse-link-face muse-bad-link-face))
427 (goto-char (or (next-single-property-change (point) 'face)
430 (let ((next (point)))
431 (if (while (and (null pos)
433 (next-single-property-change
435 (when (memq (get-text-property next 'face)
436 '(muse-link-face muse-bad-link-face))
439 (goto-char (point-min))
440 (setq cycled (1+ cycled))))))
445 (defun muse-previous-reference ()
446 "Move backward to the next Muse link or URL, cycling if necessary.
447 This function is not entirely accurate, but it's close enough."
449 (let ((cycled 0) pos)
452 (let ((prev (point)))
453 (if (while (and (null pos)
455 (previous-single-property-change
457 (when (memq (get-text-property prev 'face)
458 '(muse-link-face muse-bad-link-face))
461 (goto-char (point-max))
462 (setq cycled (1+ cycled))))))
467 (defun muse-what-changed ()
468 "Show the unsaved changes that have been made to the current file."
470 (diff-backup buffer-file-name))
472 ;;; Generate an index of all known Muse pages
474 (defun muse-generate-index (&optional as-list exclude-private)
475 "Generate an index of all Muse pages."
476 (let ((index (muse-index-as-string as-list exclude-private)))
477 (with-current-buffer (get-buffer-create "*Muse Index*")
484 "Display an index of all known Muse pages."
486 (message "Generating Muse index...")
487 (let ((project (muse-project)))
488 (with-current-buffer (muse-generate-index)
489 (goto-char (point-min))
491 (setq muse-current-project project)
492 (pop-to-buffer (current-buffer))))
493 (message "Generating Muse index...done"))
495 (defun muse-index-as-string (&optional as-list exclude-private exclude-current)
496 "Generate an index of all Muse pages.
497 If AS-LIST is non-nil, insert a dash and spaces before each item.
498 If EXCLUDE-PRIVATE is non-nil, exclude files that have private permissions.
499 If EXCLUDE-CURRENT is non-nil, exclude the current file from the output."
500 (let ((files (sort (copy-alist (muse-project-file-alist))
503 (string-lessp (car l) (car r)))))))
504 (when (and exclude-current (muse-page-name))
505 (setq files (delete (assoc (muse-page-name) files) files)))
508 (unless (and exclude-private
509 (muse-project-private-p (cdar files)))
510 (insert (if as-list " - " "") "[[" (caar files) "]]\n"))
511 (setq files (cdr files)))
514 ;;; Insert tags interactively on C-c TAB
516 (defvar muse-tag-history nil
517 "List of recently-entered tags; used by `muse-insert-tag'.
518 If you want a tag to start as the default, you may manually set
519 this variable to a list.")
521 (defvar muse-custom-tags nil
522 "Keep track of any new tags entered in `muse-insert-tag'.
523 If there are (X)HTML tags that you use frequently with that
524 function, you might want to set this manually.")
527 (defun muse-insert-tag (tag)
528 "Insert a tag interactively with a blank line after it."
533 (when muse-tag-history
534 (concat "(default: " (car muse-tag-history) ") ")))
535 (mapcar 'list (nconc (mapcar 'car muse-publish-markup-tags)
537 nil nil nil 'muse-tag-history
538 (car muse-tag-history))))
540 (setq tag (car muse-tag-history)))
541 (let ((tag-entry (assoc tag muse-publish-markup-tags))
543 ;; Add to custom list if no entry exists
545 (add-to-list 'muse-custom-tags tag))
547 (when (nth 2 tag-entry)
548 (setq options (read-string "Option: ")))
549 (unless (equal options "")
550 (setq options (concat " " options)))
551 ;; Insert the tag, closing if necessary
552 (when tag (insert (concat "<" tag options ">")))
553 (when (nth 1 tag-entry)
554 (insert (concat "\n\n</" tag ">\n"))
559 ;;; muse-mode.el ends here