muse-mode: Minor fix.
[muse-el.git] / lisp / muse-mode.el
blobef679a67ed89fc44e30897a1635c142b3a74373c
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 ;; If we're using Emacs21, this makes flyspell work like it should
182 (when (boundp 'flyspell-generic-check-word-p)
183 (set (make-local-variable 'flyspell-generic-check-word-p)
184 'muse-mode-flyspell-p))
185 ;; If pcomplete is available, set it up
186 (when (featurep 'pcomplete)
187 (set (make-local-variable 'pcomplete-default-completion-function)
188 'muse-mode-completions)
189 (set (make-local-variable 'pcomplete-command-completion-function)
190 'muse-mode-completions)
191 (set (make-local-variable 'pcomplete-parse-arguments-function)
192 'muse-mode-current-word))
193 (when muse-mode-highlight-p
194 (muse-use-font-lock)))
196 (put 'muse-mode
197 'flyspell-mode-predicate
198 'muse-mode-flyspell-p)
200 (defun muse-mode-fill-nobreak-p ()
201 "Return nil if we should allow a fill to occur at point.
202 Otherwise return non-nil.
204 This is used to keep long explicit links from being mangled by
205 fill mode."
206 (save-excursion
207 (save-match-data
208 (and (re-search-backward "\\[\\[\\|\\]\\]"
209 (line-beginning-position) t)
210 (string= (or (match-string 0) "")
211 "[[")))))
213 (defun muse-mode-flyspell-p ()
214 "Return non-nil if we should allow spell-checking to occur at point.
215 Otherwise return nil.
217 This is used to keep links from being improperly colorized by flyspell."
218 (save-match-data
219 (null (muse-link-at-point))))
221 (defun muse-mode-choose-mode ()
222 "Turn the proper Emacs Muse related mode on for this file."
223 (let ((project (muse-project-of-file)))
224 (funcall (or (and project (muse-get-keyword :major-mode (cadr project) t))
225 'muse-mode))))
227 (defun muse-mode-maybe ()
228 "Maybe turn Emacs Muse mode on for this file."
229 (let ((project (muse-project-of-file)))
230 (and project
231 (funcall (or (muse-get-keyword :major-mode (cadr project) t)
232 'muse-mode)))))
234 ;;; Support page name completion using pcomplete
236 (defun muse-completions ()
237 "Return a list of possible completions names for this buffer."
238 (let ((project (muse-project-of-file)))
239 (if project
240 (while (pcomplete-here
241 (mapcar 'car (muse-project-file-alist project)))))))
243 (defun muse-current-word ()
244 (let ((end (point)))
245 (save-excursion
246 (save-restriction
247 (skip-chars-backward (concat "^\\["
248 muse-regexp-space))
249 (narrow-to-region (point) end))
250 (pcomplete-parse-buffer-arguments))))
252 ;;; Navigate/visit links or URLs. Use TAB, S-TAB and RET (or mouse-2).
254 (defun muse-link-at-point (&optional pos)
255 "Return link text if a URL or link is at point."
256 (let ((case-fold-search nil)
257 (inhibit-point-motion-hooks t)
258 (here (or pos (point))))
259 (when (or (null pos)
260 (and (char-after pos)
261 (not (eq (char-syntax (char-after pos)) ?\ ))))
262 (save-excursion
263 (goto-char here)
264 ;; Check for explicit link here or before point
265 (if (or (looking-at muse-explicit-link-regexp)
266 (and
267 (re-search-backward "\\[\\[\\|\\]\\]"
268 (muse-line-beginning-position)
270 (string= (or (match-string 0) "") "[[")
271 (looking-at muse-explicit-link-regexp)))
272 (progn
273 (goto-char (match-beginning 1))
274 (muse-handle-explicit-link))
275 (goto-char here)
276 ;; Check for bare URL or other link type
277 (skip-chars-backward (concat "^'\"<>{}(\n"
278 muse-regexp-space))
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)))
287 (if (and name
288 link
289 (not (string= name ""))
290 (not (string= link name)))
291 (concat "[[" (or link "") "][" name "]]")
292 (concat "[[" (or link "") "]]")))
294 (defun muse-edit-link-at-point ()
295 "Edit the current link.
296 Do not rename the page originally referred to."
297 (interactive)
298 (if (muse-link-at-point)
299 (replace-match
300 (muse-make-link
301 (read-string "Link: "
302 (muse-match-string-no-properties 1))
303 (read-string "Text: "
304 (muse-match-string-no-properties 2)))
305 t t)
306 (error "There is no valid link at point")))
308 (defun muse-visit-link-default (link &optional other-window)
309 "Visit the URL or link named by LINK.
310 If ANCHOR is specified, search for it after opening LINK.
312 This is the default function to call when visiting links; it is
313 used by `muse-visit-link' if you have not specified :visit-link
314 in `muse-project-alist'."
315 (if (string-match muse-url-regexp link)
316 (muse-browse-url link)
317 (let (anchor
318 base-buffer)
319 (when (string-match "#" link)
320 (setq anchor (substring link (match-beginning 0))
321 link (if (= (match-beginning 0) 0)
322 ;; If there is an anchor but no link, default
323 ;; to the current page.
325 (substring link 0 (match-beginning 0)))))
326 (when link
327 (setq base-buffer (get-buffer link))
328 (if (and base-buffer (not (buffer-file-name base-buffer)))
329 ;; If file is temporary (no associated file), just switch to
330 ;; the buffer
331 (if other-window
332 (switch-to-buffer-other-window base-buffer)
333 (switch-to-buffer base-buffer))
334 (let ((project (muse-project-of-file)))
335 (if project
336 (muse-project-find-file link project
337 (and other-window
338 'find-file-other-window))
339 (if other-window
340 (find-file-other-window link)
341 (find-file link))))))
342 (when anchor
343 (let ((pos (point)))
344 (goto-char (point-min))
345 (unless (re-search-forward (concat "^\\W*" (regexp-quote anchor)
346 "\\b")
347 nil t)
348 (goto-char pos)))))))
350 (defun muse-visit-link (link &optional other-window)
351 "Visit the URL or link named by LINK."
352 (let ((visit-link-function
353 (muse-get-keyword :visit-link (cadr (muse-project-of-file)) t)))
354 (if visit-link-function
355 (funcall visit-link-function link other-window)
356 (muse-visit-link-default link other-window))))
358 (defun muse-browse-result (style &optional other-window)
359 "Visit the current page's published result."
360 (interactive (list (muse-publish-get-style) current-prefix-arg))
361 (setq style (muse-style style))
362 (let ((result-path
363 (muse-publish-output-file buffer-file-name
364 (muse-style-element :path style) style)))
365 (if (not (file-readable-p result-path))
366 (error "Cannot open output file '%s'" result-path)
367 (if other-window
368 (find-file-other-window result-path)
369 (let ((func (muse-style-element :browser style t)))
370 (if func
371 (funcall func result-path)
372 (message "The %s publishing style does not support browsing."
373 style)))))))
375 (defun muse-follow-name-at-point (&optional other-window)
376 "Visit the link at point, or insert a newline if none is found."
377 (interactive "P")
378 (let ((link (muse-link-at-point)))
379 (if link
380 (muse-visit-link link other-window)
381 (error "There is no valid link at point"))))
383 (defun muse-follow-name-at-point-other-window ()
384 "Visit the link at point in other window."
385 (interactive)
386 (muse-follow-name-at-point t))
388 (defun muse-follow-name-at-mouse (event &optional other-window)
389 "Visit the link at point, or yank text if none is found."
390 (interactive "eN")
391 (save-excursion
392 (cond ((fboundp 'event-window) ; XEmacs
393 (set-buffer (window-buffer (event-window event)))
394 (and (funcall (symbol-function 'event-point) event)
395 (goto-char (funcall (symbol-function 'event-point) event))))
396 ((fboundp 'posn-window) ; Emacs
397 (set-buffer (window-buffer (posn-window (event-start event))))
398 (goto-char (posn-point (event-start event)))))
399 (let ((link (muse-link-at-point)))
400 (if link
401 (muse-visit-link link other-window)
402 ;; Fall back to normal binding for this event
403 (call-interactively
404 (lookup-key (current-global-map) (this-command-keys)))))))
406 (defun muse-follow-name-at-mouse-other-window (event)
407 "Visit the link at point"
408 (interactive "e")
409 ;; throw away the old window position, since other-window will
410 ;; change it anyway
411 (select-window (car (cadr event)))
412 (muse-follow-name-at-mouse event t))
414 (defun muse-next-reference ()
415 "Move forward to next Muse link or URL, cycling if necessary."
416 (interactive)
417 (let ((cycled 0) pos)
418 (save-excursion
419 (when (memq (get-text-property (point) 'face)
420 '(muse-link-face muse-bad-link-face))
421 (goto-char (or (next-single-property-change (point) 'face)
422 (point-max))))
423 (while (< cycled 2)
424 (let ((next (point)))
425 (if (while (and (null pos)
426 (setq next
427 (next-single-property-change
428 next 'face)))
429 (when (memq (get-text-property next 'face)
430 '(muse-link-face muse-bad-link-face))
431 (setq pos next)))
432 (setq cycled 2)
433 (goto-char (point-min))
434 (setq cycled (1+ cycled))))))
435 (if pos
436 (goto-char pos))))
438 (defun muse-previous-reference ()
439 "Move backward to the next Muse link or URL, cycling if necessary.
440 This function is not entirely accurate, but it's close enough."
441 (interactive)
442 (let ((cycled 0) pos)
443 (save-excursion
444 (while (< cycled 2)
445 (let ((prev (point)))
446 (if (while (and (null pos)
447 (setq prev
448 (previous-single-property-change
449 prev 'face)))
450 (when (memq (get-text-property prev 'face)
451 '(muse-link-face muse-bad-link-face))
452 (setq pos prev)))
453 (setq cycled 2)
454 (goto-char (point-max))
455 (setq cycled (1+ cycled))))))
456 (if pos
457 (goto-char pos))))
459 (defun muse-what-changed ()
460 "Show the unsaved changes that have been made to the current file."
461 (interactive)
462 (diff-backup buffer-file-name))
464 ;;; Generate an index of all known Muse pages
466 (defun muse-generate-index (&optional as-list exclude-private)
467 "Generate an index of all Muse pages."
468 (let ((index (muse-index-as-string as-list exclude-private)))
469 (with-current-buffer (get-buffer-create "*Muse Index*")
470 (erase-buffer)
471 (insert index)
472 (current-buffer))))
474 (defun muse-index ()
475 "Display an index of all known Muse pages."
476 (interactive)
477 (message "Generating Muse index...")
478 (let ((project (muse-project)))
479 (with-current-buffer (muse-generate-index)
480 (goto-char (point-min))
481 (muse-mode)
482 (setq muse-current-project project)
483 (pop-to-buffer (current-buffer))))
484 (message "Generating Muse index...done"))
486 (defun muse-index-as-string (&optional as-list exclude-private exclude-current)
487 "Generate an index of all Muse pages.
488 If AS-LIST is non-nil, insert a dash and spaces before each item.
489 If EXCLUDE-PRIVATE is non-nil, exclude files that have private permissions.
490 If EXCLUDE-CURRENT is non-nil, exclude the current file from the output."
491 (let ((files (sort (copy-alist (muse-project-file-alist))
492 (function
493 (lambda (l r)
494 (string-lessp (car l) (car r)))))))
495 (when (and exclude-current (muse-page-name))
496 (setq files (delete (assoc (muse-page-name) files) files)))
497 (muse-with-temp-buffer
498 (while files
499 (unless (and exclude-private
500 (muse-project-private-p (cdar files)))
501 (insert (if as-list " - " "") "[[" (caar files) "]]\n"))
502 (setq files (cdr files)))
503 (buffer-string))))
505 ;;; Insert tags interactively on C-c TAB
507 (defvar muse-tag-history nil
508 "List of recently-entered tags; used by `muse-insert-tag'.
509 If you want a tag to start as the default, you may manually set
510 this variable to a list.")
512 (defvar muse-custom-tags nil
513 "Keep track of any new tags entered in `muse-insert-tag'.
514 If there are (X)HTML tags that you use frequently with that
515 function, you might want to set this manually.")
517 (defun muse-insert-tag (tag)
518 "Insert a tag interactively with a blank line after it."
519 (interactive
520 (list
521 (completing-read
522 (concat "Tag: "
523 (when muse-tag-history
524 (concat "(default: " (car muse-tag-history) ") ")))
525 (mapcar 'list (nconc (mapcar 'car muse-publish-markup-tags)
526 muse-custom-tags))
527 nil nil nil 'muse-tag-history
528 (car muse-tag-history))))
529 (when (equal tag "")
530 (setq tag (car muse-tag-history)))
531 (let ((tag-entry (assoc tag muse-publish-markup-tags))
532 (options ""))
533 ;; Add to custom list if no entry exists
534 (unless tag-entry
535 (add-to-list 'muse-custom-tags tag))
536 ;; Get option
537 (when (nth 2 tag-entry)
538 (setq options (read-string "Option: ")))
539 (unless (equal options "")
540 (setq options (concat " " options)))
541 ;; Insert the tag, closing if necessary
542 (when tag (insert (concat "<" tag options ">")))
543 (when (nth 1 tag-entry)
544 (insert (concat "\n\n</" tag ">\n"))
545 (forward-line -2))))
547 (provide 'muse-mode)
549 ;;; muse-mode.el ends here