Fix #4923: Definition lists with numbers are not possible.
[muse-el.git] / lisp / muse-mode.el
blob98f823c73d34c5fee834cd71de0414307142943c
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 :group 'muse)
60 (defcustom muse-mode-highlight-p t
61 "If non-nil, highlight the content of Muse buffers."
62 :type 'boolean
63 :require 'muse-colors
64 :group 'muse-mode)
66 (defcustom muse-mode-auto-p nil
67 "If non-nil, automagically determine when Muse mode should be activated."
68 :type 'boolean
69 :set (function
70 (lambda (sym value)
71 (if value
72 (add-hook 'find-file-hooks 'muse-mode-maybe)
73 (remove-hook 'find-file-hooks 'muse-mode-maybe))
74 (set sym value)))
75 :group 'muse-mode)
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."
81 :type 'boolean
82 :group 'muse-mode)
84 (defcustom muse-mode-hook nil
85 "A hook that is run when Muse mode is entered."
86 :type 'hook
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))
92 (set sym val))
93 :group 'muse-mode)
95 (defvar muse-mode-map
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)
112 (progn
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))
137 map)
138 "Keymap used by Emacs Muse mode.")
140 ;; Code:
142 ;;;###autoload
143 (define-derived-mode muse-mode text-mode "Muse"
144 "Muse is an Emacs mode for authoring and publishing documents.
145 \\{muse-mode-map}"
146 ;; Since we're not inheriting from normal-mode, we need to
147 ;; explicitly run file variables.
148 (condition-case err
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
153 ;; with flyspell.
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
167 ;; Emacs 21 and 22.
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)
180 "; ")
181 (set (make-local-variable 'comment-start-skip)
182 "^;\\s-+")
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)))
198 (put 'muse-mode
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
207 fill mode."
208 (save-excursion
209 (save-match-data
210 (and (re-search-backward "\\[\\[\\|\\]\\]"
211 (line-beginning-position) t)
212 (string= (or (match-string 0) "")
213 "[[")))))
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."
220 (save-match-data
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))
227 'muse-mode))))
229 (defun muse-mode-maybe ()
230 "Maybe turn Emacs Muse mode on for this file."
231 (let ((project (muse-project-of-file)))
232 (and project
233 (funcall (or (muse-get-keyword :major-mode (cadr project) t)
234 'muse-mode)))))
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)))
241 (if project
242 (while (pcomplete-here
243 (mapcar 'car (muse-project-file-alist project)))))))
245 (defun muse-current-word ()
246 (let ((end (point)))
247 (save-excursion
248 (save-restriction
249 (skip-chars-backward (concat "^\\["
250 muse-regexp-space))
251 (narrow-to-region (point) end))
252 (pcomplete-parse-buffer-arguments))))
254 ;;; Navigate/visit links or URLs. Use TAB, S-TAB and RET (or mouse-2).
256 (defun muse-link-at-point (&optional pos)
257 "Return link text if a URL or link is at point."
258 (let ((case-fold-search nil)
259 (inhibit-point-motion-hooks t)
260 (here (or pos (point))))
261 (when (or (null pos)
262 (and (char-after pos)
263 (not (eq (char-syntax (char-after pos)) ?\ ))))
264 (save-excursion
265 (goto-char here)
266 ;; Check for explicit link here or before point
267 (if (or (looking-at muse-explicit-link-regexp)
268 (and
269 (re-search-backward "\\[\\[\\|\\]\\]"
270 (muse-line-beginning-position)
272 (string= (or (match-string 0) "") "[[")
273 (looking-at muse-explicit-link-regexp)))
274 (progn
275 (goto-char (match-beginning 1))
276 (muse-handle-explicit-link))
277 (goto-char here)
278 ;; Check for bare URL or other link type
279 (skip-chars-backward (concat "^'\"<>{}(\n"
280 muse-regexp-space))
281 (and (looking-at muse-implicit-link-regexp)
282 (muse-handle-implicit-link)))))))
284 (defun muse-make-link (link &optional name)
285 "Return a link to LINK with NAME as the text."
286 (when (string-match muse-explicit-link-regexp link)
287 (unless name (setq name (match-string 2 link)))
288 (setq link (match-string 1 link)))
289 (if (and name
290 link
291 (not (string= name ""))
292 (not (string= link name)))
293 (concat "[[" (or link "") "][" name "]]")
294 (concat "[[" (or link "") "]]")))
296 (defun muse-edit-link-at-point ()
297 "Edit the current link.
298 Do not rename the page originally referred to."
299 (interactive)
300 (if (muse-link-at-point)
301 (replace-match
302 (save-match-data
303 (muse-make-link
304 (read-string "Link: "
305 (muse-match-string-no-properties 1))
306 (read-string "Text: "
307 (muse-match-string-no-properties 2))))
308 t t)
309 (error "There is no valid link at point")))
311 (defun muse-visit-link-default (link &optional other-window)
312 "Visit the URL or link named by LINK.
313 If ANCHOR is specified, search for it after opening LINK.
315 This is the default function to call when visiting links; it is
316 used by `muse-visit-link' if you have not specified :visit-link
317 in `muse-project-alist'."
318 (if (string-match muse-url-regexp link)
319 (muse-browse-url link)
320 (let (anchor
321 base-buffer)
322 (when (string-match "#" link)
323 (setq anchor (substring link (match-beginning 0))
324 link (if (= (match-beginning 0) 0)
325 ;; If there is an anchor but no link, default
326 ;; to the current page.
328 (substring link 0 (match-beginning 0)))))
329 (when link
330 (setq base-buffer (get-buffer link))
331 (if (and base-buffer (not (buffer-file-name base-buffer)))
332 ;; If file is temporary (no associated file), just switch to
333 ;; the buffer
334 (if other-window
335 (switch-to-buffer-other-window base-buffer)
336 (switch-to-buffer base-buffer))
337 (let ((project (muse-project-of-file)))
338 (if project
339 (muse-project-find-file link project
340 (and other-window
341 'find-file-other-window))
342 (if other-window
343 (find-file-other-window link)
344 (find-file link))))))
345 (when anchor
346 (let ((pos (point)))
347 (goto-char (point-min))
348 (unless (re-search-forward (concat "^\\W*" (regexp-quote anchor)
349 "\\b")
350 nil t)
351 (goto-char pos)))))))
353 (defun muse-visit-link (link &optional other-window)
354 "Visit the URL or link named by LINK."
355 (let ((visit-link-function
356 (muse-get-keyword :visit-link (cadr (muse-project-of-file)) t)))
357 (if visit-link-function
358 (funcall visit-link-function link other-window)
359 (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))
365 (let ((result-path
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)
370 (if other-window
371 (find-file-other-window result-path)
372 (let ((func (muse-style-element :browser style t)))
373 (if func
374 (funcall func result-path)
375 (message "The %s publishing style does not support browsing."
376 style)))))))
378 (defun muse-follow-name-at-point (&optional other-window)
379 "Visit the link at point, or insert a newline if none is found."
380 (interactive "P")
381 (let ((link (muse-link-at-point)))
382 (if link
383 (muse-visit-link link other-window)
384 (error "There is no valid link at point"))))
386 (defun muse-follow-name-at-point-other-window ()
387 "Visit the link at point in other window."
388 (interactive)
389 (muse-follow-name-at-point t))
391 (defun muse-follow-name-at-mouse (event &optional other-window)
392 "Visit the link at point, or yank text if none is found."
393 (interactive "eN")
394 (save-excursion
395 (cond ((fboundp 'event-window) ; XEmacs
396 (set-buffer (window-buffer (event-window event)))
397 (and (funcall (symbol-function 'event-point) event)
398 (goto-char (funcall (symbol-function 'event-point) event))))
399 ((fboundp 'posn-window) ; Emacs
400 (set-buffer (window-buffer (posn-window (event-start event))))
401 (goto-char (posn-point (event-start event)))))
402 (let ((link (muse-link-at-point)))
403 (if link
404 (muse-visit-link link other-window)
405 ;; Fall back to normal binding for this event
406 (call-interactively
407 (lookup-key (current-global-map) (this-command-keys)))))))
409 (defun muse-follow-name-at-mouse-other-window (event)
410 "Visit the link at point"
411 (interactive "e")
412 ;; throw away the old window position, since other-window will
413 ;; change it anyway
414 (select-window (car (cadr event)))
415 (muse-follow-name-at-mouse event t))
417 (defun muse-next-reference ()
418 "Move forward to next Muse link or URL, cycling if necessary."
419 (interactive)
420 (let ((cycled 0) pos)
421 (save-excursion
422 (when (memq (get-text-property (point) 'face)
423 '(muse-link-face muse-bad-link-face))
424 (goto-char (or (next-single-property-change (point) 'face)
425 (point-max))))
426 (while (< cycled 2)
427 (let ((next (point)))
428 (if (while (and (null pos)
429 (setq next
430 (next-single-property-change
431 next 'face)))
432 (when (memq (get-text-property next 'face)
433 '(muse-link-face muse-bad-link-face))
434 (setq pos next)))
435 (setq cycled 2)
436 (goto-char (point-min))
437 (setq cycled (1+ cycled))))))
438 (if pos
439 (goto-char pos))))
441 (defun muse-previous-reference ()
442 "Move backward to the next Muse link or URL, cycling if necessary.
443 This function is not entirely accurate, but it's close enough."
444 (interactive)
445 (let ((cycled 0) pos)
446 (save-excursion
447 (while (< cycled 2)
448 (let ((prev (point)))
449 (if (while (and (null pos)
450 (setq prev
451 (previous-single-property-change
452 prev 'face)))
453 (when (memq (get-text-property prev 'face)
454 '(muse-link-face muse-bad-link-face))
455 (setq pos prev)))
456 (setq cycled 2)
457 (goto-char (point-max))
458 (setq cycled (1+ cycled))))))
459 (if pos
460 (goto-char pos))))
462 (defun muse-what-changed ()
463 "Show the unsaved changes that have been made to the current file."
464 (interactive)
465 (diff-backup buffer-file-name))
467 ;;; Generate an index of all known Muse pages
469 (defun muse-generate-index (&optional as-list exclude-private)
470 "Generate an index of all Muse pages."
471 (let ((index (muse-index-as-string as-list exclude-private)))
472 (with-current-buffer (get-buffer-create "*Muse Index*")
473 (erase-buffer)
474 (insert index)
475 (current-buffer))))
477 (defun muse-index ()
478 "Display an index of all known Muse pages."
479 (interactive)
480 (message "Generating Muse index...")
481 (let ((project (muse-project)))
482 (with-current-buffer (muse-generate-index)
483 (goto-char (point-min))
484 (muse-mode)
485 (setq muse-current-project project)
486 (pop-to-buffer (current-buffer))))
487 (message "Generating Muse index...done"))
489 (defun muse-index-as-string (&optional as-list exclude-private exclude-current)
490 "Generate an index of all Muse pages.
491 If AS-LIST is non-nil, insert a dash and spaces before each item.
492 If EXCLUDE-PRIVATE is non-nil, exclude files that have private permissions.
493 If EXCLUDE-CURRENT is non-nil, exclude the current file from the output."
494 (let ((files (sort (copy-alist (muse-project-file-alist))
495 (function
496 (lambda (l r)
497 (string-lessp (car l) (car r)))))))
498 (when (and exclude-current (muse-page-name))
499 (setq files (delete (assoc (muse-page-name) files) files)))
500 (with-temp-buffer
501 (while files
502 (unless (and exclude-private
503 (muse-project-private-p (cdar files)))
504 (insert (if as-list " - " "") "[[" (caar files) "]]\n"))
505 (setq files (cdr files)))
506 (buffer-string))))
508 ;;; Insert tags interactively on C-c TAB
510 (defvar muse-tag-history nil
511 "List of recently-entered tags; used by `muse-insert-tag'.
512 If you want a tag to start as the default, you may manually set
513 this variable to a list.")
515 (defvar muse-custom-tags nil
516 "Keep track of any new tags entered in `muse-insert-tag'.
517 If there are (X)HTML tags that you use frequently with that
518 function, you might want to set this manually.")
520 (defun muse-insert-tag (tag)
521 "Insert a tag interactively with a blank line after it."
522 (interactive
523 (list
524 (completing-read
525 (concat "Tag: "
526 (when muse-tag-history
527 (concat "(default: " (car muse-tag-history) ") ")))
528 (mapcar 'list (nconc (mapcar 'car muse-publish-markup-tags)
529 muse-custom-tags))
530 nil nil nil 'muse-tag-history
531 (car muse-tag-history))))
532 (when (equal tag "")
533 (setq tag (car muse-tag-history)))
534 (let ((tag-entry (assoc tag muse-publish-markup-tags))
535 (options ""))
536 ;; Add to custom list if no entry exists
537 (unless tag-entry
538 (add-to-list 'muse-custom-tags tag))
539 ;; Get option
540 (when (nth 2 tag-entry)
541 (setq options (read-string "Option: ")))
542 (unless (equal options "")
543 (setq options (concat " " options)))
544 ;; Insert the tag, closing if necessary
545 (when tag (insert (concat "<" tag options ">")))
546 (when (nth 1 tag-entry)
547 (insert (concat "\n\n</" tag ">\n"))
548 (forward-line -2))))
550 (provide 'muse-mode)
552 ;;; muse-mode.el ends here