Keep lists from getting mashed together on M-q.
[muse-el.git] / lisp / muse-mode.el
blob260096aa36f968defa699c6379d609d1c9af520a
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 :set #'(lambda (sym val)
91 (when (featurep 'muse-wiki)
92 (add-to-list 'val 'muse-wiki-update-custom-values))
93 (set sym val))
94 :group 'muse-mode)
96 (defvar muse-mode-map
97 (let ((map (make-sparse-keymap)))
98 (define-key map [(control ?c) (control ?a)] 'muse-index)
99 (define-key map [(control ?c) (control ?b)] 'muse-browse-result)
100 (define-key map [(control ?c) (control ?c)] 'muse-follow-name-at-point)
101 (define-key map [(control ?c) (control ?e)] 'muse-edit-link-at-point)
102 (define-key map [(control ?c) (control ?t)] 'muse-publish-this-file)
103 (define-key map [(control ?c) (control ?v)] 'muse-follow-name-at-point)
105 (define-key map [(control ?c) (control ?l)] 'font-lock-mode)
107 (define-key map [(control ?c) ?=] 'muse-what-changed)
109 (define-key map [tab] 'muse-next-reference)
110 (define-key map [(control ?i)] 'muse-next-reference)
112 (if (featurep 'xemacs)
113 (progn
114 (define-key map [(button2)] 'muse-follow-name-at-mouse)
115 (define-key map [(shift button2)]
116 'muse-follow-name-at-mouse-other-window))
117 (define-key map [(shift control ?m)]
118 'muse-follow-name-at-point-other-window)
119 (define-key map [mouse-2] 'muse-follow-name-at-mouse)
120 (define-key map [(shift mouse-2)]
121 'muse-follow-name-at-mouse-other-window))
123 (define-key map [(shift tab)] 'muse-previous-reference)
124 (unless (featurep 'xemacs)
125 (define-key map [(shift iso-lefttab)] 'muse-previous-reference)
126 (define-key map [(shift control ?i)] 'muse-previous-reference))
128 (define-key map [(control ?c) (control ?f)] 'muse-project-find-file)
129 (define-key map [(control ?c) (control ?p)] 'muse-project-publish)
131 (define-key map [(control ?c) tab] 'muse-insert-tag)
132 (define-key map [(control ?c) (control ?i)] 'muse-insert-tag)
134 (when (featurep 'pcomplete)
135 (define-key map [(meta tab)] 'pcomplete)
136 (define-key map [(meta control ?i)] 'pcomplete))
138 map)
139 "Keymap used by Emacs Muse mode.")
141 ;; Code:
143 ;;;###autoload
144 (define-derived-mode muse-mode text-mode "Muse"
145 "Muse is an Emacs mode for authoring and publishing documents.
146 \\{muse-mode-map}"
147 ;; Since we're not inheriting from normal-mode, we need to
148 ;; explicitly run file variables.
149 (condition-case err
150 (hack-local-variables)
151 (error (message "File local-variables error: %s"
152 (prin1-to-string err))))
153 ;; Avoid lock-up caused by use of the 'intangible' text property
154 ;; with flyspell.
155 (unless muse-mode-intangible-links
156 (set (make-local-variable 'inhibit-point-motion-hooks) t))
157 (setq muse-current-project (muse-project-of-file))
158 (muse-project-set-variables)
159 ;; Make sure several variables get updated if the user has changed
160 ;; them without using the customize interface.
161 (muse-update-ignored-extensions-regexp 'muse-ignored-extensions
162 muse-ignored-extensions)
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 CVS and all other Emacs types.
168 (if (not (muse-extreg-usable-p))
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 (concat "[" muse-regexp-blank "]+\\(-\\|[0-9]+\\.\\)["
175 muse-regexp-blank "]+\\|\\[[0-9]+\\]["
176 muse-regexp-blank "]*\\|["
177 muse-regexp-blank "]*"))
178 (set (make-local-variable 'paragraph-start)
179 (concat paragraph-start "\\|[" muse-regexp-blank
180 "]+\\(-\\|[0-9]+\\.\\)[" muse-regexp-blank
181 "]+\\|\\[[0-9]+\\][" muse-regexp-blank "]*"))
182 (when (featurep 'pcomplete)
183 ;; If pcomplete is available, set it up
184 (set (make-local-variable 'pcomplete-default-completion-function)
185 'muse-mode-completions)
186 (set (make-local-variable 'pcomplete-command-completion-function)
187 'muse-mode-completions)
188 (set (make-local-variable 'pcomplete-parse-arguments-function)
189 'muse-mode-current-word))
190 (when muse-mode-highlight-p
191 (muse-use-font-lock)))
193 (put 'muse-mode
194 'flyspell-mode-predicate
195 'muse-mode-flyspell-p)
197 (defun muse-mode-fill-nobreak-p ()
198 "Return nil if we should allow a fill to occur at point.
199 Otherwise return non-nil.
201 This is used to keep long explicit links from being mangled by
202 fill mode."
203 (save-excursion
204 (save-match-data
205 (and (re-search-backward "\\[\\[\\|\\]\\]"
206 (line-beginning-position) t)
207 (string= (or (match-string 0) "")
208 "[[")))))
210 (defun muse-mode-flyspell-p ()
211 "Return non-nil if we should allow spell-checking to occur at point.
212 Otherwise return nil.
214 This is used to keep links from being improperly colorized by flyspell."
215 (save-match-data
216 (null (muse-link-at-point))))
218 (defun muse-mode-choose-mode ()
219 "Turn the proper Emacs Muse related mode on for this file."
220 (let ((project (muse-project-of-file)))
221 (funcall (or (and project (muse-get-keyword :major-mode (cadr project) t))
222 'muse-mode))))
224 (defun muse-mode-maybe ()
225 "Maybe turn Emacs Muse mode on for this file."
226 (let ((project (muse-project-of-file)))
227 (and project
228 (funcall (or (muse-get-keyword :major-mode (cadr project) t)
229 'muse-mode)))))
231 ;;; Support page name completion using pcomplete
233 (defun muse-completions ()
234 "Return a list of possible completions names for this buffer."
235 (let ((project (muse-project-of-file)))
236 (if project
237 (while (pcomplete-here
238 (mapcar 'car (muse-project-file-alist project)))))))
240 (defun muse-current-word ()
241 (let ((end (point)))
242 (save-restriction
243 (save-excursion
244 (skip-chars-backward (concat "^\\["
245 muse-regexp-space))
246 (narrow-to-region (point) end))
247 (pcomplete-parse-buffer-arguments))))
249 ;;; Navigate/visit links or URLs. Use TAB, S-TAB and RET (or mouse-2).
251 (defun muse-link-at-point (&optional pos)
252 "Return link text if a URL or link is at point."
253 (let ((case-fold-search nil)
254 (here (or pos (point))))
255 (when (or (null pos)
256 (and (char-after pos)
257 (not (eq (char-syntax (char-after pos)) ?\ ))))
258 (save-excursion
259 (goto-char here)
260 ;; Check for explicit link here or before point
261 (if (or (looking-at muse-explicit-link-regexp)
262 (and
263 (re-search-backward "\\[\\[\\|\\]\\]"
264 (muse-line-beginning-position)
266 (string= (or (match-string 0) "") "[[")
267 (looking-at muse-explicit-link-regexp)))
268 (progn
269 (goto-char (match-beginning 1))
270 (muse-handle-explicit-link))
271 (goto-char here)
272 ;; Check for bare URL or other link type
273 (skip-chars-backward (concat "^'\"<>{}(\n"
274 muse-regexp-space))
275 (and (looking-at muse-implicit-link-regexp)
276 (muse-handle-implicit-link)))))))
278 (defun muse-make-link (link &optional name)
279 "Return a link to LINK with NAME as the text."
280 (if (and name
281 link
282 (not (string= name ""))
283 (not (string= link name)))
284 (concat "[[" (or link "") "][" name "]]")
285 (concat "[[" (or link "") "]]")))
287 (defun muse-edit-link-at-point ()
288 "Edit the current link.
289 Do not rename the page originally referred to."
290 (interactive)
291 (if (muse-link-at-point)
292 (replace-match
293 (muse-make-link
294 (read-string "Link: "
295 (muse-match-string-no-properties 1))
296 (read-string "Text: "
297 (muse-match-string-no-properties 2)))
298 t t)
299 (error "There is no valid link at point")))
301 (defun muse-visit-link-default (link &optional other-window anchor)
302 "Visit the URL or link named by LINK.
303 If ANCHOR is specified, search for it after opening LINK.
305 This is the default function to call when visiting links; it is
306 used by `muse-visit-link' if you have not specified :visit-link
307 in `muse-project-alist'."
308 (if (string-match muse-url-regexp link)
309 (browse-url link)
310 (let ((project (muse-project-of-file)))
311 (if project
312 (muse-project-find-file link project
313 (and other-window
314 'find-file-other-window))
315 (if other-window
316 (find-file-other-window link)
317 (find-file link))))
318 (if anchor
319 (search-forward anchor nil t))))
321 (defun muse-visit-link (link &optional other-window)
322 "Visit the URL or link named by LINK."
323 (let ((visit-link-function
324 (muse-get-keyword :visit-link (cadr (muse-project-of-file)) t))
325 anchor)
326 (if visit-link-function
327 (funcall visit-link-function link other-window)
328 (if (string-match "#" link)
329 (setq anchor (substring link (match-beginning 0))
330 link (substring link 0 (match-beginning 0))))
331 (muse-visit-link-default link other-window anchor))))
333 (defun muse-browse-result (style &optional other-window)
334 "Visit the current page's published result."
335 (interactive (list (muse-publish-get-style) current-prefix-arg))
336 (setq style (muse-style style))
337 (let ((result-path
338 (muse-publish-output-file buffer-file-name
339 (muse-style-element :path style) style)))
340 (if (not (file-readable-p result-path))
341 (error "Cannot open output file '%s'" result-path)
342 (if other-window
343 (find-file-other-window result-path)
344 (let ((func (muse-style-element :browser style t)))
345 (if func
346 (funcall func result-path)
347 (message "The %s publishing style does not support browsing."
348 style)))))))
350 (defun muse-follow-name-at-point (&optional other-window)
351 "Visit the link at point, or insert a newline if none is found."
352 (interactive "P")
353 (let ((link (muse-link-at-point)))
354 (if link
355 (muse-visit-link link other-window)
356 (error "There is no valid link at point"))))
358 (defun muse-follow-name-at-point-other-window ()
359 "Visit the link at point in other window."
360 (interactive)
361 (muse-follow-name-at-point t))
363 (defun muse-follow-name-at-mouse (event &optional other-window)
364 "Visit the link at point, or yank text if none is found."
365 (interactive "eN")
366 (save-excursion
367 (cond ((fboundp 'event-window) ; XEmacs
368 (set-buffer (window-buffer (event-window event)))
369 (and (funcall (symbol-function 'event-point) event)
370 (goto-char (funcall (symbol-function 'event-point) event))))
371 ((fboundp 'posn-window) ; Emacs
372 (set-buffer (window-buffer (posn-window (event-start event))))
373 (goto-char (posn-point (event-start event)))))
374 (let ((link (muse-link-at-point)))
375 (if link
376 (muse-visit-link link other-window)
377 ;; Fall back to normal binding for this event
378 (call-interactively
379 (lookup-key (current-global-map) (this-command-keys)))))))
381 (defun muse-follow-name-at-mouse-other-window (event)
382 "Visit the link at point"
383 (interactive "e")
384 ;; throw away the old window position, since other-window will
385 ;; change it anyway
386 (select-window (car (cadr event)))
387 (muse-follow-name-at-mouse event t))
389 (defun muse-next-reference ()
390 "Move forward to next Muse link or URL, cycling if necessary."
391 (interactive)
392 (let ((cycled 0) pos)
393 (save-excursion
394 (when (memq (get-text-property (point) 'face)
395 '(muse-link-face muse-bad-link-face))
396 (goto-char (or (next-single-property-change (point) 'face)
397 (point-max))))
398 (while (< cycled 2)
399 (let ((next (point)))
400 (if (while (and (null pos)
401 (setq next
402 (next-single-property-change
403 next 'face)))
404 (when (memq (get-text-property next 'face)
405 '(muse-link-face muse-bad-link-face))
406 (setq pos next)))
407 (setq cycled 2)
408 (goto-char (point-min))
409 (setq cycled (1+ cycled))))))
410 (if pos
411 (goto-char pos))))
413 (defun muse-previous-reference ()
414 "Move backward to the next Muse link or URL, cycling if necessary.
415 This function is not entirely accurate, but it's close enough."
416 (interactive)
417 (let ((cycled 0) pos)
418 (save-excursion
419 (while (< cycled 2)
420 (let ((prev (point)))
421 (if (while (and (null pos)
422 (setq prev
423 (previous-single-property-change
424 prev 'face)))
425 (when (memq (get-text-property prev 'face)
426 '(muse-link-face muse-bad-link-face))
427 (setq pos prev)))
428 (setq cycled 2)
429 (goto-char (point-max))
430 (setq cycled (1+ cycled))))))
431 (if pos
432 (goto-char pos))))
434 (defun muse-what-changed ()
435 "Show the unsaved changes that have been made to the current file."
436 (interactive)
437 (diff-backup buffer-file-name))
439 ;;; Generate an index of all known Muse pages
441 (defun muse-generate-index (&optional as-list exclude-private)
442 "Generate an index of all Muse pages."
443 (let ((index (muse-index-as-string as-list exclude-private)))
444 (with-current-buffer (get-buffer-create "*Muse Index*")
445 (erase-buffer)
446 (insert index)
447 (current-buffer))))
449 (defun muse-index ()
450 "Display an index of all known Muse pages."
451 (interactive)
452 (message "Generating Muse index...")
453 (let ((project (muse-project)))
454 (with-current-buffer (muse-generate-index)
455 (goto-char (point-min))
456 (muse-mode)
457 (setq muse-current-project project)
458 (pop-to-buffer (current-buffer))))
459 (message "Generating Muse index...done"))
461 (defun muse-index-as-string (&optional as-list exclude-private exclude-current)
462 "Generate an index of all Muse pages.
463 If AS-LIST is non-nil, insert a dash and spaces before each item.
464 If EXCLUDE-PRIVATE is non-nil, exclude files that have private permissions.
465 If EXCLUDE-CURRENT is non-nil, exclude the current file from the output."
466 (let ((files (sort (copy-alist (muse-project-file-alist))
467 (function
468 (lambda (l r)
469 (string-lessp (car l) (car r)))))))
470 (when (and exclude-current (muse-page-name))
471 (setq files (delete (assoc (muse-page-name) files) files)))
472 (muse-with-temp-buffer
473 (while files
474 (unless (and exclude-private
475 (muse-project-private-p (cdar files)))
476 (insert (if as-list " - " "") "[[" (caar files) "]]\n"))
477 (setq files (cdr files)))
478 (buffer-string))))
480 ;;; Insert tags interactively on C-c TAB
482 (defvar muse-tag-history nil
483 "List of recently-entered tags; used by `muse-insert-tag'.
484 If you want a tag to start as the default, you may manually set
485 this variable to a list.")
487 (defvar muse-custom-tags nil
488 "Keep track of any new tags entered in `muse-insert-tag'.
489 If there are (X)HTML tags that you use frequently with that
490 function, you might want to set this manually.")
492 (defun muse-insert-tag (tag)
493 "Insert a tag interactively with a blank line after it."
494 (interactive
495 (list
496 (completing-read
497 (concat "Tag: "
498 (when muse-tag-history
499 (concat "(default: " (car muse-tag-history) ") ")))
500 (mapcar 'list (nconc (mapcar 'car muse-publish-markup-tags)
501 muse-custom-tags))
502 nil nil nil 'muse-tag-history
503 (car muse-tag-history))))
504 (when (equal tag "")
505 (setq tag (car muse-tag-history)))
506 (let ((tag-entry (assoc tag muse-publish-markup-tags))
507 (options ""))
508 ;; Add to custom list if no entry exists
509 (unless tag-entry
510 (add-to-list 'muse-custom-tags tag))
511 ;; Get option
512 (when (nth 2 tag-entry)
513 (setq options (read-string "Option: ")))
514 (unless (equal options "")
515 (setq options (concat " " options)))
516 ;; Insert the tag, closing if necessary
517 (when tag (insert (concat "<" tag options ">")))
518 (when (nth 1 tag-entry)
519 (insert (concat "\n\n</" tag ">\n"))
520 (forward-line -2))))
522 (provide 'muse-mode)
524 ;;; muse-mode.el ends here