Compilation fix, minor shuffling in `muse-mode'.
[muse-el.git] / lisp / muse-mode.el
bloba1ed72380cb6657ddfff42b0cfb45b63d4dbf233
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
10 ;; version.
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
15 ;; for more details.
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.
22 ;;; Commentary:
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.
27 ;;; Contributors:
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.
33 ;;; Code:
35 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
37 ;; Emacs Muse Major Mode
39 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
41 (require 'muse)
42 (require 'muse-regexps)
43 (require 'muse-project)
44 (require 'muse-publish)
46 (autoload 'muse-use-font-lock "muse-colors")
48 (require 'derived)
49 (eval-when-compile
50 (condition-case nil
51 (require 'pcomplete) ; load if available
52 (error nil)))
54 ;;; Options:
56 (defgroup muse-mode nil
57 "Options controlling the behavior of the Muse editing Mode.
58 See `muse-publish' for more information."
59 :group 'muse)
61 (defcustom muse-mode-highlight-p t
62 "If non-nil, highlight the content of Muse buffers."
63 :type 'boolean
64 :require 'muse-colors
65 :group 'muse-mode)
67 (defcustom muse-mode-auto-p t
68 "If non-nil, automagically determine when Muse mode should be activated."
69 :type 'boolean
70 :set (function
71 (lambda (sym value)
72 (if value
73 (add-hook 'find-file-hooks 'muse-mode-maybe)
74 (remove-hook 'find-file-hooks 'muse-mode-maybe))
75 (set sym value)))
76 :group 'muse-mode)
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."
82 :type 'boolean
83 :group 'muse-mode)
85 (defcustom muse-mode-hook nil
86 "A hook that is run when Muse mode is entered."
87 :type 'hook
88 :options '(flyspell-mode footnote-mode turn-on-auto-fill
89 highlight-changes-mode)
90 :group 'muse-mode)
92 (defvar muse-mode-map
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)
109 (progn
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 (or (featurep 'xemacs) muse-under-windows-p)
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))
134 map)
135 "Keymap used by Emacs Muse mode.")
137 ;; Code:
139 ;;;###autoload
140 (define-derived-mode muse-mode text-mode "Muse"
141 "Muse is an Emacs mode for authoring and publishing documents.
142 \\{muse-mode-map}"
143 ;; Since we're not inheriting from normal-mode, we need to
144 ;; explicitly run file variables.
145 (condition-case err
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
150 ;; with flyspell.
151 (unless muse-mode-intangible-links
152 (set (make-local-variable 'inhibit-point-motion-hooks) t))
153 (setq muse-current-project (muse-project-of-file))
154 (muse-project-set-variables)
155 ;; Make sure several variables get updated if the user has changed
156 ;; them without using the customize interface.
157 (muse-update-ignored-extensions-regexp 'muse-ignored-extensions
158 muse-ignored-extensions)
159 ;; Make fill not split up links
160 (when (boundp 'fill-nobreak-predicate)
161 (make-local-variable 'fill-nobreak-predicate)
162 ;; Work around annoying inconsistency in fill handling between
163 ;; Emacs CVS and all other Emacs types.
164 (if (not (muse-extreg-usable-p))
165 (setq fill-nobreak-predicate 'muse-mode-fill-nobreak-p)
166 (add-to-list 'fill-nobreak-predicate
167 'muse-mode-fill-nobreak-p)))
168 ;; Make adaptive fill work nicely with item lists
169 (set (make-local-variable 'adaptive-fill-regexp)
170 (concat "[" muse-regexp-blank "]*\\(-+["
171 muse-regexp-blank
172 "]*\\|[0-9]+\\.["
173 muse-regexp-blank "]*\\)*"))
174 (when (featurep 'pcomplete)
175 ;; If pcomplete is available, set it up
176 (set (make-variable-buffer-local 'pcomplete-default-completion-function)
177 'muse-mode-completions)
178 (set (make-variable-buffer-local 'pcomplete-command-completion-function)
179 'muse-mode-completions)
180 (set (make-variable-buffer-local 'pcomplete-parse-arguments-function)
181 'muse-mode-current-word))
182 (when muse-mode-highlight-p
183 (muse-use-font-lock)))
185 (put 'muse-mode
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
194 fill mode."
195 (save-excursion
196 (save-match-data
197 (and (re-search-backward "\\[\\[\\|\\]\\]"
198 (line-beginning-position) t)
199 (string= (or (match-string 0) "")
200 "[[")))))
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."
207 (save-match-data
208 (null (muse-link-at-point))))
210 (defun muse-mode-choose-mode ()
211 "Turn the proper Emacs Muse related mode on for this file."
212 (let ((project (muse-project-of-file)))
213 (funcall (or (and project (muse-get-keyword :major-mode (cadr project) t))
214 'muse-mode))))
216 (defun muse-mode-maybe ()
217 "Maybe turn Emacs Muse mode on for this file."
218 (let ((project (muse-project-of-file)))
219 (and project
220 (funcall (or (muse-get-keyword :major-mode (cadr project) t)
221 'muse-mode)))))
223 ;;; Support page name completion using pcomplete
225 (defun muse-completions ()
226 "Return a list of possible completions names for this buffer."
227 (let ((project (muse-project-of-file)))
228 (if project
229 (while (pcomplete-here
230 (mapcar 'car (muse-project-file-alist project)))))))
232 (defun muse-current-word ()
233 (let ((end (point)))
234 (save-restriction
235 (save-excursion
236 (skip-chars-backward (concat "^\\["
237 muse-regexp-space))
238 (narrow-to-region (point) end))
239 (pcomplete-parse-buffer-arguments))))
241 ;;; Navigate/visit links or URLs. Use TAB, S-TAB and RET (or mouse-2).
243 (defun muse-link-at-point (&optional pos)
244 "Return link text if a URL or link is at point."
245 (let ((case-fold-search nil)
246 (here (or pos (point))))
247 (when (or (null pos)
248 (and (char-after pos)
249 (not (eq (char-syntax (char-after pos)) ?\ ))))
250 (save-excursion
251 (goto-char here)
252 ;; Check for explicit link here or before point
253 (if (or (looking-at muse-explicit-link-regexp)
254 (and
255 (re-search-backward "\\[\\[\\|\\]\\]"
256 (muse-line-beginning-position)
258 (string= (or (match-string 0) "") "[[")
259 (looking-at muse-explicit-link-regexp)))
260 (progn
261 (goto-char (match-beginning 1))
262 (muse-handle-explicit-link))
263 (goto-char here)
264 ;; Check for bare URL or other link type
265 (skip-chars-backward (concat "^'\"<>{}(\n"
266 muse-regexp-space))
267 (and (looking-at muse-implicit-link-regexp)
268 (muse-handle-implicit-link)))))))
270 (defun muse-make-link (link &optional name)
271 "Return a link to LINK with NAME as the text."
272 (if (and name
273 link
274 (not (string= name ""))
275 (not (string= link name)))
276 (concat "[[" (or link "") "][" name "]]")
277 (concat "[[" (or link "") "]]")))
279 (defun muse-edit-link-at-point ()
280 "Edit the current link.
281 Do not rename the page originally referred to."
282 (interactive)
283 (if (muse-link-at-point)
284 (replace-match
285 (muse-make-link
286 (read-string "Link: "
287 (muse-match-string-no-properties 1))
288 (read-string "Text: "
289 (muse-match-string-no-properties 2)))
290 t t)
291 (error "There is no valid link at point")))
293 (defun muse-visit-link-default (link &optional other-window anchor)
294 "Visit the URL or link named by LINK.
295 If ANCHOR is specified, search for it after opening LINK.
297 This is the default function to call when visiting links; it is
298 used by `muse-visit-link' if you have not specified :visit-link
299 in `muse-project-alist'."
300 (if (string-match muse-url-regexp link)
301 (browse-url link)
302 (let ((project (muse-project-of-file)))
303 (if project
304 (muse-project-find-file link project
305 (and other-window
306 'find-file-other-window))
307 (if other-window
308 (find-file-other-window link)
309 (find-file link))))
310 (if anchor
311 (search-forward anchor nil t))))
313 (defun muse-visit-link (link &optional other-window)
314 "Visit the URL or link named by LINK."
315 (let ((visit-link-function
316 (muse-get-keyword :visit-link (cadr (muse-project-of-file)) t))
317 anchor)
318 (if visit-link-function
319 (funcall visit-link-function link other-window)
320 (if (string-match "#" link)
321 (setq anchor (substring link (match-beginning 0))
322 link (substring link 0 (match-beginning 0))))
323 (muse-visit-link-default link other-window anchor))))
325 (defun muse-browse-result (style &optional other-window)
326 "Visit the current page's published result."
327 (interactive (list (muse-publish-get-style) current-prefix-arg))
328 (setq style (muse-style style))
329 (let ((result-path
330 (muse-publish-output-file buffer-file-name
331 (muse-style-element :path style) style)))
332 (if (not (file-readable-p result-path))
333 (error "Cannot open output file '%s'" result-path)
334 (if other-window
335 (find-file-other-window result-path)
336 (let ((func (muse-style-element :browser style t)))
337 (if func
338 (funcall func result-path)
339 (message "The %s publishing style does not support browsing."
340 style)))))))
342 (defun muse-follow-name-at-point (&optional other-window)
343 "Visit the link at point, or insert a newline if none is found."
344 (interactive "P")
345 (let ((link (muse-link-at-point)))
346 (if link
347 (muse-visit-link link other-window)
348 (error "There is no valid link at point"))))
350 (defun muse-follow-name-at-point-other-window ()
351 "Visit the link at point in other window."
352 (interactive)
353 (muse-follow-name-at-point t))
355 (defun muse-follow-name-at-mouse (event &optional other-window)
356 "Visit the link at point, or yank text if none is found."
357 (interactive "eN")
358 (save-excursion
359 (cond ((fboundp 'event-window) ; XEmacs
360 (set-buffer (window-buffer (event-window event)))
361 (and (funcall (symbol-function 'event-point) event)
362 (goto-char (funcall (symbol-function 'event-point) event))))
363 ((fboundp 'posn-window) ; Emacs
364 (set-buffer (window-buffer (posn-window (event-start event))))
365 (goto-char (posn-point (event-start event)))))
366 (let ((link (muse-link-at-point)))
367 (if link
368 (muse-visit-link link other-window)
369 ;; Fall back to normal binding for this event
370 (call-interactively
371 (lookup-key (current-global-map) (this-command-keys)))))))
373 (defun muse-follow-name-at-mouse-other-window (event)
374 "Visit the link at point"
375 (interactive "e")
376 ;; throw away the old window position, since other-window will
377 ;; change it anyway
378 (select-window (car (cadr event)))
379 (muse-follow-name-at-mouse event t))
381 (defun muse-next-reference ()
382 "Move forward to next Muse link or URL, cycling if necessary."
383 (interactive)
384 (let ((cycled 0) pos)
385 (save-excursion
386 (when (memq (get-text-property (point) 'face)
387 '(muse-link-face muse-bad-link-face))
388 (goto-char (or (next-single-property-change (point) 'face)
389 (point-max))))
390 (while (< cycled 2)
391 (let ((next (point)))
392 (if (while (and (null pos)
393 (setq next
394 (next-single-property-change
395 next 'face)))
396 (when (memq (get-text-property next 'face)
397 '(muse-link-face muse-bad-link-face))
398 (setq pos next)))
399 (setq cycled 2)
400 (goto-char (point-min))
401 (setq cycled (1+ cycled))))))
402 (if pos
403 (goto-char pos))))
405 (defun muse-previous-reference ()
406 "Move backward to the next Muse link or URL, cycling if necessary.
407 This function is not entirely accurate, but it's close enough."
408 (interactive)
409 (let ((cycled 0) pos)
410 (save-excursion
411 (while (< cycled 2)
412 (let ((prev (point)))
413 (if (while (and (null pos)
414 (setq prev
415 (previous-single-property-change
416 prev 'face)))
417 (when (memq (get-text-property prev 'face)
418 '(muse-link-face muse-bad-link-face))
419 (setq pos prev)))
420 (setq cycled 2)
421 (goto-char (point-max))
422 (setq cycled (1+ cycled))))))
423 (if pos
424 (goto-char pos))))
426 (defun muse-what-changed ()
427 "Show the unsaved changes that have been made to the current file."
428 (interactive)
429 (diff-backup buffer-file-name))
431 ;;; Generate an index of all known Muse pages
433 (defun muse-generate-index (&optional as-list exclude-private)
434 "Generate an index of all Muse pages."
435 (let ((index (muse-index-as-string as-list exclude-private)))
436 (with-current-buffer (get-buffer-create "*Muse Index*")
437 (erase-buffer)
438 (insert index)
439 (current-buffer))))
441 (defun muse-index ()
442 "Display an index of all known Muse pages."
443 (interactive)
444 (message "Generating Muse index...")
445 (let ((project (muse-project)))
446 (with-current-buffer (muse-generate-index)
447 (goto-char (point-min))
448 (muse-mode)
449 (setq muse-current-project project)
450 (pop-to-buffer (current-buffer))))
451 (message "Generating Muse index...done"))
453 (defun muse-index-as-string (&optional as-list exclude-private)
454 "Generate an index of all Muse pages."
455 (let ((files (sort (copy-alist (muse-project-file-alist))
456 (function
457 (lambda (l r)
458 (string-lessp (car l) (car r)))))))
459 (with-temp-buffer
460 (while files
461 (unless (and exclude-private
462 (muse-project-private-p (cdar files)))
463 (insert (if as-list " - " "") "[[" (caar files) "]]\n"))
464 (setq files (cdr files)))
465 (buffer-string))))
467 ;;; Insert tags interactively on C-c TAB
469 (defvar muse-tag-history nil
470 "List of recently-entered tags; used by `muse-insert-tag'.
471 If you want a tag to start as the default, you may manually set
472 this variable to a list.")
474 (defvar muse-custom-tags nil
475 "Keep track of any new tags entered in `muse-insert-tag'.
476 If there are (X)HTML tags that you use frequently with that
477 function, you might want to set this manually.")
479 (defun muse-insert-tag (tag)
480 "Insert a tag interactively with a blank line after it."
481 (interactive
482 (list
483 (completing-read
484 (concat "Tag: "
485 (when muse-tag-history
486 (concat "(default: " (car muse-tag-history) ") ")))
487 (mapcar 'list (nconc (mapcar 'car muse-publish-markup-tags)
488 muse-custom-tags))
489 nil nil nil 'muse-tag-history
490 (car muse-tag-history))))
491 (when (equal tag "")
492 (setq tag (car muse-tag-history)))
493 (let ((tag-entry (assoc tag muse-publish-markup-tags))
494 (options ""))
495 ;; Add to custom list if no entry exists
496 (unless tag-entry
497 (add-to-list 'muse-custom-tags tag))
498 ;; Get option
499 (when (nth 2 tag-entry)
500 (setq options (read-string "Option: ")))
501 (unless (equal options "")
502 (setq options (concat " " options)))
503 ;; Insert the tag, closing if necessary
504 (when tag (insert (concat "<" tag options ">")))
505 (when (nth 1 tag-entry)
506 (insert (concat "\n\n</" tag ">\n"))
507 (forward-line -2))))
509 (provide 'muse-mode)
511 ;;; muse-mode.el ends here