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