Fix Emacs21 issue with muse-insert-tag.
[muse-el.git] / lisp / muse-mode.el
blobeaf032a1a7cad6c03b5b8d3e2585c37fa64a8265
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 (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))
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 (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 "]*\\(-+["
173 muse-regexp-blank
174 "]*\\|[0-9]+\\.["
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)))
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-maybe ()
211 "Maybe turn Emacs Muse mode on for this file."
212 (let ((project (muse-project-of-file)))
213 (and project
214 (funcall (or (muse-get-keyword :major-mode (cadr project) t)
215 'muse-mode)))))
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)))
222 (if project
223 (while (pcomplete-here
224 (mapcar 'car (muse-project-file-alist project)))))))
226 (defun muse-current-word ()
227 (let ((end (point)))
228 (save-restriction
229 (save-excursion
230 (skip-chars-backward (concat "^\\["
231 muse-regexp-space))
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))))
241 (when (or (null pos)
242 (and (char-after pos)
243 (not (eq (char-syntax (char-after pos)) ?\ ))))
244 (save-excursion
245 (goto-char here)
246 ;; Check for explicit link here or before point
247 (if (or (looking-at muse-explicit-link-regexp)
248 (and
249 (re-search-backward "\\[\\[\\|\\]\\]"
250 (muse-line-beginning-position)
252 (string= (or (match-string 0) "") "[[")
253 (looking-at muse-explicit-link-regexp)))
254 (progn
255 (goto-char (match-beginning 1))
256 (muse-handle-explicit-link))
257 (goto-char here)
258 ;; Check for bare URL or other link type
259 (skip-chars-backward (concat "^'\"<>{}("
260 muse-regexp-space))
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."
266 (if (and name
267 link
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."
276 (interactive)
277 (if (muse-link-at-point)
278 (replace-match
279 (muse-make-link
280 (read-string "Link: "
281 (muse-match-string-no-properties 1))
282 (read-string "Text: "
283 (muse-match-string-no-properties 2)))
284 t t)
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)
295 (browse-url link)
296 (let ((project (muse-project-of-file)))
297 (if project
298 (muse-project-find-file link project
299 (and other-window
300 'find-file-other-window))
301 (if other-window
302 (find-file-other-window link)
303 (find-file link))))
304 (if anchor
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))
311 anchor)
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))
323 (let ((result-path
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)
328 (if other-window
329 (find-file-other-window result-path)
330 (let ((func (muse-style-element :browser style t)))
331 (if func
332 (funcall func result-path)
333 (message "The %s publishing style does not support browsing."
334 style)))))))
336 (defun muse-follow-name-at-point (&optional other-window)
337 "Visit the link at point, or insert a newline if none is found."
338 (interactive "P")
339 (let ((link (muse-link-at-point)))
340 (if link
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."
346 (interactive)
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."
351 (interactive "eN")
352 (save-excursion
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)))
361 (if link
362 (muse-visit-link link other-window)
363 ;; Fall back to normal binding for this event
364 (call-interactively
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"
369 (interactive "e")
370 ;; throw away the old window position, since other-window will
371 ;; change it anyway
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."
377 (interactive)
378 (let ((cycled 0) pos)
379 (save-excursion
380 (when (eq (get-text-property (point) 'face) 'muse-link-face)
381 (goto-char (or (next-single-property-change (point) 'face)
382 (point-max))))
383 (while (< cycled 2)
384 (let ((next (point)))
385 (if (while (and (null pos)
386 (setq next
387 (next-single-property-change
388 next 'face)))
389 (when (eq (get-text-property next 'face) 'muse-link-face)
390 (setq pos next)))
391 (setq cycled 2)
392 (goto-char (point-min))
393 (setq cycled (1+ cycled))))))
394 (if pos
395 (goto-char pos))))
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."
400 (interactive)
401 (let ((cycled 0) pos)
402 (save-excursion
403 (while (< cycled 2)
404 (let ((prev (point)))
405 (if (while (and (null pos)
406 (setq prev
407 (previous-single-property-change
408 prev 'face)))
409 (when (eq (get-text-property prev 'face) 'muse-link-face)
410 (setq pos prev)))
411 (setq cycled 2)
412 (goto-char (point-max))
413 (setq cycled (1+ cycled))))))
414 (if pos
415 (goto-char pos))))
417 (defun muse-what-changed ()
418 "Show the unsaved changes that have been made to the current file."
419 (interactive)
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*")
428 (erase-buffer)
429 (insert index)
430 (current-buffer))))
432 (defun muse-index ()
433 "Display an index of all known Muse pages."
434 (interactive)
435 (message "Generating Muse index...")
436 (let ((project (muse-project)))
437 (with-current-buffer (muse-generate-index)
438 (goto-char (point-min))
439 (muse-mode)
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))
447 (function
448 (lambda (l r)
449 (string-lessp (car l) (car r)))))))
450 (with-temp-buffer
451 (while files
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)))
456 (buffer-string))))
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."
472 (interactive
473 (list
474 (completing-read
475 (concat "Tag: "
476 (when muse-tag-history
477 (concat "(default: " (car muse-tag-history) ") ")))
478 (mapcar 'list (nconc (mapcar 'car muse-publish-markup-tags)
479 muse-custom-tags))
480 nil nil nil 'muse-tag-history
481 (car muse-tag-history))))
482 (when (equal tag "")
483 (setq tag (car muse-tag-history)))
484 (let ((tag-entry (assoc tag muse-publish-markup-tags))
485 (options ""))
486 ;; Add to custom list if no entry exists
487 (unless tag-entry
488 (add-to-list 'muse-custom-tags tag))
489 ;; Get option
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"))
498 (forward-line -2))))
500 (provide 'muse-mode)
502 ;;; muse-mode.el ends here