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