(diff-default-read-only): Change default.
[emacs.git] / lisp / help-mode.el
blob032509b9614c2934065b90fd13214a365a5d5b1d
1 ;;; help-mode.el --- `help-mode' used by *Help* buffers
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002
4 ;; Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: help, internal
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
28 ;; Defines `help-mode', which is the mode used by *Help* buffers, and
29 ;; associated support machinery, such as adding hyperlinks, etc.,
31 ;;; Code:
33 (require 'button)
34 (require 'view)
36 (defvar help-mode-map (make-sparse-keymap)
37 "Keymap for help mode.")
39 (set-keymap-parent help-mode-map button-buffer-map)
41 (define-key help-mode-map [mouse-2] 'help-follow-mouse)
42 (define-key help-mode-map "\C-c\C-b" 'help-go-back)
43 (define-key help-mode-map "\C-c\C-c" 'help-follow)
44 ;; Documentation only, since we use minor-mode-overriding-map-alist.
45 (define-key help-mode-map "\r" 'help-follow)
47 (defvar help-xref-stack nil
48 "A stack of ways by which to return to help buffers after following xrefs.
49 Used by `help-follow' and `help-xref-go-back'.
50 An element looks like (POSITION FUNCTION ARGS...).
51 To use the element, do (apply FUNCTION ARGS) then goto the point.")
52 (put 'help-xref-stack 'permanent-local t)
53 (make-variable-buffer-local 'help-xref-stack)
55 (defvar help-xref-stack-item nil
56 "An item for `help-follow' in this buffer to push onto `help-xref-stack'.
57 The format is (FUNCTION ARGS...).")
58 (put 'help-xref-stack-item 'permanent-local t)
59 (make-variable-buffer-local 'help-xref-stack-item)
61 (setq-default help-xref-stack nil help-xref-stack-item nil)
63 (defcustom help-mode-hook nil
64 "Hook run by `help-mode'."
65 :type 'hook
66 :group 'help)
68 ;; Button types used by help
70 (define-button-type 'help-xref
71 'action #'help-button-action)
73 (defun help-button-action (button)
74 "Call BUTTON's help function."
75 (help-do-xref (button-start button)
76 (button-get button 'help-function)
77 (button-get button 'help-args)))
79 ;; These 6 calls to define-button-type were generated in a dolist
80 ;; loop, but that is bad because it means these button types don't
81 ;; have an easily found definition.
83 (define-button-type 'help-function
84 :supertype 'help-xref
85 'help-function 'describe-function
86 'help-echo (purecopy "mouse-2, RET: describe this function"))
88 (define-button-type 'help-variable
89 :supertype 'help-xref
90 'help-function 'describe-variable
91 'help-echo (purecopy "mouse-2, RET: describe this variable"))
93 (define-button-type 'help-face
94 :supertype 'help-xref
95 'help-function 'describe-face
96 'help-echo (purecopy "mouse-2, RET: describe this face"))
98 (define-button-type 'help-coding-system
99 :supertype 'help-xref
100 'help-function 'describe-coding-system
101 'help-echo (purecopy "mouse-2, RET: describe this coding system"))
103 (define-button-type 'help-input-method
104 :supertype 'help-xref
105 'help-function 'describe-input-method
106 'help-echo (purecopy "mouse-2, RET: describe this input method"))
108 (define-button-type 'help-character-set
109 :supertype 'help-xref
110 'help-function 'describe-character-set
111 'help-echo (purecopy "mouse-2, RET: describe this character set"))
113 ;; make some more ideosyncratic button types
115 (define-button-type 'help-symbol
116 :supertype 'help-xref
117 'help-function #'help-xref-interned
118 'help-echo (purecopy "mouse-2, RET: describe this symbol"))
120 (define-button-type 'help-back
121 :supertype 'help-xref
122 'help-function #'help-xref-go-back
123 'help-echo (purecopy "mouse-2, RET: go back to previous help buffer"))
125 (define-button-type 'help-info
126 :supertype 'help-xref
127 'help-function #'info
128 'help-echo (purecopy"mouse-2, RET: read this Info node"))
130 (define-button-type 'help-customize-variable
131 :supertype 'help-xref
132 'help-function (lambda (v)
133 (if help-xref-stack
134 (pop help-xref-stack))
135 (customize-variable v))
136 'help-echo (purecopy "mouse-2, RET: customize variable"))
138 (define-button-type 'help-customize-face
139 :supertype 'help-xref
140 'help-function (lambda (v)
141 (if help-xref-stack
142 (pop help-xref-stack))
143 (customize-face v))
144 'help-echo (purecopy "mouse-2, RET: customize face"))
146 (define-button-type 'help-function-def
147 :supertype 'help-xref
148 'help-function (lambda (fun file)
149 (require 'find-func)
150 ;; Don't use find-function-noselect because it follows
151 ;; aliases (which fails for built-in functions).
152 (let ((location
153 (if (bufferp file) (cons file fun)
154 (find-function-search-for-symbol fun nil file))))
155 (pop-to-buffer (car location))
156 (goto-char (cdr location))))
157 'help-echo (purecopy "mouse-2, RET: find function's definition"))
159 (define-button-type 'help-variable-def
160 :supertype 'help-xref
161 'help-function (lambda (var &optional file)
162 (let ((location
163 (find-variable-noselect var file)))
164 (pop-to-buffer (car location))
165 (goto-char (cdr location))))
166 'help-echo (purecopy"mouse-2, RET: find variable's definition"))
169 ;;;###autoload
170 (defun help-mode ()
171 "Major mode for viewing help text and navigating references in it.
172 Entry to this mode runs the normal hook `help-mode-hook'.
173 Commands:
174 \\{help-mode-map}"
175 (interactive)
176 (kill-all-local-variables)
177 (use-local-map help-mode-map)
178 (setq mode-name "Help")
179 (setq major-mode 'help-mode)
180 (view-mode)
181 (make-local-variable 'view-no-disable-on-exit)
182 (setq view-no-disable-on-exit t)
183 (run-hooks 'help-mode-hook))
185 ;;;###autoload
186 (defun help-mode-setup ()
187 (help-mode)
188 (setq buffer-read-only nil))
190 ;;;###autoload
191 (defun help-mode-finish ()
192 (when (eq major-mode 'help-mode)
193 ;; View mode's read-only status of existing *Help* buffer is lost
194 ;; by with-output-to-temp-buffer.
195 (toggle-read-only 1)
196 (help-make-xrefs (current-buffer)))
197 (setq view-return-to-alist
198 (list (cons (selected-window) help-return-method))))
201 ;; Grokking cross-reference information in doc strings and
202 ;; hyperlinking it.
204 ;; This may have some scope for extension and the same or something
205 ;; similar should be done for widget doc strings, which currently use
206 ;; another mechanism.
208 (defvar help-back-label (purecopy "[back]")
209 "Label to use by `help-make-xrefs' for the go-back reference.")
211 (defconst help-xref-symbol-regexp
212 (purecopy (concat "\\(\\<\\(\\(variable\\|option\\)\\|"
213 "\\(function\\|command\\)\\|"
214 "\\(face\\)\\|"
215 "\\(symbol\\)\\|"
216 "\\(source \\(?:code \\)?\\(?:of\\|for\\)\\)\\)"
217 "[ \t\n]+\\)?"
218 ;; Note starting with word-syntax character:
219 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'"))
220 "Regexp matching doc string references to symbols.
222 The words preceding the quoted symbol can be used in doc strings to
223 distinguish references to variables, functions and symbols.")
225 (defvar help-xref-mule-regexp nil
226 "Regexp matching doc string references to MULE-related keywords.
228 It is usually nil, and is temporarily bound to an appropriate regexp
229 when help commands related to multilingual environment (e.g.,
230 `describe-coding-system') are invoked.")
233 (defconst help-xref-info-regexp
234 (purecopy "\\<[Ii]nfo[ \t\n]+\\(node\\|anchor\\)[ \t\n]+`\\([^']+\\)'")
235 "Regexp matching doc string references to an Info node.")
237 ;;;###autoload
238 (defun help-setup-xref (item interactive-p)
239 "Invoked from commands using the \"*Help*\" buffer to install some xref info.
241 ITEM is a (FUNCTION . ARGS) pair appropriate for recreating the help
242 buffer after following a reference. INTERACTIVE-P is non-nil if the
243 calling command was invoked interactively. In this case the stack of
244 items for help buffer \"back\" buttons is cleared.
246 This should be called very early, before the output buffer is cleared,
247 because we want to record the \"previous\" position of point so we can
248 restore it properly when going back."
249 (with-current-buffer (help-buffer)
250 (when help-xref-stack-item
251 (push (cons (point) help-xref-stack-item) help-xref-stack))
252 (when interactive-p
253 (let ((tail (nthcdr 10 help-xref-stack)))
254 ;; Truncate the stack.
255 (if tail (setcdr tail nil))))
256 (setq help-xref-stack-item item)))
258 (defvar help-xref-following nil
259 "Non-nil when following a help cross-reference.")
261 (defun help-buffer ()
262 (buffer-name ;for with-output-to-temp-buffer
263 (if help-xref-following
264 (current-buffer)
265 (get-buffer-create "*Help*"))))
267 (defvar help-xref-override-view-map
268 (let ((map (make-sparse-keymap)))
269 (set-keymap-parent map view-mode-map)
270 (define-key map "\r" nil)
271 map)
272 "Replacement keymap for `view-mode' in help buffers.")
274 ;;;###autoload
275 (defun help-make-xrefs (&optional buffer)
276 "Parse and hyperlink documentation cross-references in the given BUFFER.
278 Find cross-reference information in a buffer and activate such cross
279 references for selection with `help-follow'. Cross-references have
280 the canonical form `...' and the type of reference may be
281 disambiguated by the preceding word(s) used in
282 `help-xref-symbol-regexp'. Faces only get cross-referenced if
283 preceded or followed by the word `face'. Variables without
284 variable documentation do not get cross-referenced, unless
285 preceded by the word `variable' or `option'.
287 If the variable `help-xref-mule-regexp' is non-nil, find also
288 cross-reference information related to multilingual environment
289 \(e.g., coding-systems). This variable is also used to disambiguate
290 the type of reference as the same way as `help-xref-symbol-regexp'.
292 A special reference `back' is made to return back through a stack of
293 help buffers. Variable `help-back-label' specifies the text for
294 that."
295 (interactive "b")
296 (save-excursion
297 (set-buffer (or buffer (current-buffer)))
298 (goto-char (point-min))
299 ;; Skip the header-type info, though it might be useful to parse
300 ;; it at some stage (e.g. "function in `library'").
301 (forward-paragraph)
302 (let ((old-modified (buffer-modified-p)))
303 (let ((stab (syntax-table))
304 (case-fold-search t)
305 (inhibit-read-only t))
306 (set-syntax-table emacs-lisp-mode-syntax-table)
307 ;; The following should probably be abstracted out.
308 (unwind-protect
309 (progn
310 ;; Info references
311 (save-excursion
312 (while (re-search-forward help-xref-info-regexp nil t)
313 (let ((data (match-string 2)))
314 (save-match-data
315 (unless (string-match "^([^)]+)" data)
316 (setq data (concat "(emacs)" data))))
317 (help-xref-button 2 'help-info data))))
318 ;; Mule related keywords. Do this before trying
319 ;; `help-xref-symbol-regexp' because some of Mule
320 ;; keywords have variable or function definitions.
321 (if help-xref-mule-regexp
322 (save-excursion
323 (while (re-search-forward help-xref-mule-regexp nil t)
324 (let* ((data (match-string 7))
325 (sym (intern-soft data)))
326 (cond
327 ((match-string 3) ; coding system
328 (and sym (coding-system-p sym)
329 (help-xref-button 6 'help-coding-system sym)))
330 ((match-string 4) ; input method
331 (and (assoc data input-method-alist)
332 (help-xref-button 7 'help-input-method data)))
333 ((or (match-string 5) (match-string 6)) ; charset
334 (and sym (charsetp sym)
335 (help-xref-button 7 'help-character-set sym)))
336 ((assoc data input-method-alist)
337 (help-xref-button 7 'help-character-set data))
338 ((and sym (coding-system-p sym))
339 (help-xref-button 7 'help-coding-system sym))
340 ((and sym (charsetp sym))
341 (help-xref-button 7 'help-character-set sym)))))))
342 ;; Quoted symbols
343 (save-excursion
344 (while (re-search-forward help-xref-symbol-regexp nil t)
345 (let* ((data (match-string 8))
346 (sym (intern-soft data)))
347 (if sym
348 (cond
349 ((match-string 3) ; `variable' &c
350 (and (boundp sym) ; `variable' doesn't ensure
351 ; it's actually bound
352 (help-xref-button 8 'help-variable sym)))
353 ((match-string 4) ; `function' &c
354 (and (fboundp sym) ; similarly
355 (help-xref-button 8 'help-function sym)))
356 ((match-string 5) ; `face'
357 (and (facep sym)
358 (help-xref-button 8 'help-face sym)))
359 ((match-string 6)) ; nothing for `symbol'
360 ((match-string 7)
361 ;;; this used:
362 ;;; #'(lambda (arg)
363 ;;; (let ((location
364 ;;; (find-function-noselect arg)))
365 ;;; (pop-to-buffer (car location))
366 ;;; (goto-char (cdr location))))
367 (help-xref-button 8 'help-function-def sym))
368 ((facep sym)
369 (if (save-match-data (looking-at "[ \t\n]+face\\W"))
370 (help-xref-button 8 'help-face sym)))
371 ((and (boundp sym) (fboundp sym))
372 ;; We can't intuit whether to use the
373 ;; variable or function doc -- supply both.
374 (help-xref-button 8 'help-symbol sym))
375 ((and
376 (boundp sym)
377 (documentation-property sym
378 'variable-documentation))
379 (help-xref-button 8 'help-variable sym))
380 ((fboundp sym)
381 (help-xref-button 8 'help-function sym)))))))
382 ;; An obvious case of a key substitution:
383 (save-excursion
384 (while (re-search-forward
385 ;; Assume command name is only word characters
386 ;; and dashes to get things like `use M-x foo.'.
387 "\\<M-x\\s-+\\(\\sw\\(\\sw\\|-\\)+\\)" nil t)
388 (let ((sym (intern-soft (match-string 1))))
389 (if (fboundp sym)
390 (help-xref-button 1 'help-function sym)))))
391 ;; Look for commands in whole keymap substitutions:
392 (save-excursion
393 ;; Make sure to find the first keymap.
394 (goto-char (point-min))
395 ;; Find a header and the column at which the command
396 ;; name will be found.
398 ;; If the keymap substitution isn't the last thing in
399 ;; the doc string, and if there is anything on the
400 ;; same line after it, this code won't recognize the end of it.
401 (while (re-search-forward "^key +binding\n\\(-+ +\\)-+\n\n"
402 nil t)
403 (let ((col (- (match-end 1) (match-beginning 1))))
404 (while
405 (and (not (eobp))
406 ;; Stop at a pair of blank lines.
407 (not (looking-at "\n\\s-*\n")))
408 ;; Skip a single blank line.
409 (and (eolp) (forward-line))
410 (end-of-line)
411 (skip-chars-backward "^\t\n")
412 (if (and (>= (current-column) col)
413 (looking-at "\\(\\sw\\|-\\)+$"))
414 (let ((sym (intern-soft (match-string 0))))
415 (if (fboundp sym)
416 (help-xref-button 0 'help-function sym))))
417 (forward-line))))))
418 (set-syntax-table stab))
419 ;; Delete extraneous newlines at the end of the docstring
420 (goto-char (point-max))
421 (while (and (not (bobp)) (bolp))
422 (delete-char -1))
423 ;; Make a back-reference in this buffer if appropriate.
424 (when help-xref-stack
425 (insert "\n\n")
426 (help-insert-xref-button help-back-label 'help-back
427 (current-buffer))))
428 ;; View mode steals RET from us.
429 (set (make-local-variable 'minor-mode-overriding-map-alist)
430 (list (cons 'view-mode help-xref-override-view-map)))
431 (set-buffer-modified-p old-modified))))
433 ;;;###autoload
434 (defun help-xref-button (match-number type &rest args)
435 "Make a hyperlink for cross-reference text previously matched.
436 MATCH-NUMBER is the subexpression of interest in the last matched
437 regexp. TYPE is the type of button to use. Any remaining arguments are
438 passed to the button's help-function when it is invoked.
439 See `help-make-xrefs'."
440 ;; Don't mung properties we've added specially in some instances.
441 (unless (button-at (match-beginning match-number))
442 (make-text-button (match-beginning match-number)
443 (match-end match-number)
444 'type type 'help-args args)))
446 ;;;###autoload
447 (defun help-insert-xref-button (string type &rest args)
448 "Insert STRING and make a hyperlink from cross-reference text on it.
449 TYPE is the type of button to use. Any remaining arguments are passed
450 to the button's help-function when it is invoked.
451 See `help-make-xrefs'."
452 (unless (button-at (point))
453 (insert-text-button string 'type type 'help-args args)))
455 ;;;###autoload
456 (defun help-xref-on-pp (from to)
457 "Add xrefs for symbols in `pp's output between FROM and TO."
458 (if (> (- to from) 5000) nil
459 (with-syntax-table emacs-lisp-mode-syntax-table
460 (save-excursion
461 (save-restriction
462 (narrow-to-region from to)
463 (goto-char (point-min))
464 (condition-case nil
465 (while (not (eobp))
466 (cond
467 ((looking-at "\"") (forward-sexp 1))
468 ((looking-at "#<") (search-forward ">" nil 'move))
469 ((looking-at "\\(\\(\\sw\\|\\s_\\)+\\)")
470 (let* ((sym (intern-soft (match-string 1)))
471 (type (cond ((fboundp sym) 'help-function)
472 ((or (memq sym '(t nil))
473 (keywordp sym))
474 nil)
475 ((and sym (boundp sym))
476 'help-variable))))
477 (when type (help-xref-button 1 type sym)))
478 (goto-char (match-end 1)))
479 (t (forward-char 1))))
480 (error nil)))))))
483 ;; Additional functions for (re-)creating types of help buffers.
484 (defun help-xref-interned (symbol)
485 "Follow a hyperlink which appeared to be an arbitrary interned SYMBOL.
486 Both variable, function and face documentation are extracted into a single
487 help buffer."
488 (with-current-buffer (help-buffer)
489 ;; Push the previous item on the stack before clobbering the output buffer.
490 (help-setup-xref nil nil)
491 (let ((facedoc (when (facep symbol)
492 ;; Don't record the current entry in the stack.
493 (setq help-xref-stack-item nil)
494 (describe-face symbol)))
495 (fdoc (when (fboundp symbol)
496 ;; Don't record the current entry in the stack.
497 (setq help-xref-stack-item nil)
498 (describe-function symbol)))
499 (sdoc (when (boundp symbol)
500 ;; Don't record the current entry in the stack.
501 (setq help-xref-stack-item nil)
502 (describe-variable symbol))))
503 (cond
504 (sdoc
505 ;; We now have a help buffer on the variable.
506 ;; Insert the function and face text before it.
507 (when (or fdoc facedoc)
508 (goto-char (point-min))
509 (let ((inhibit-read-only t))
510 (when fdoc
511 (insert fdoc "\n\n")
512 (when facedoc
513 (insert (make-string 30 ?-) "\n\n" (symbol-name symbol)
514 " is also a " "face." "\n\n")))
515 (when facedoc
516 (insert facedoc "\n\n"))
517 (insert (make-string 30 ?-) "\n\n" (symbol-name symbol)
518 " is also a " "variable." "\n\n"))
519 ;; Don't record the `describe-variable' item in the stack.
520 (setq help-xref-stack-item nil)
521 (help-setup-xref (list #'help-xref-interned symbol) nil)))
522 (fdoc
523 ;; We now have a help buffer on the function.
524 ;; Insert face text before it.
525 (when facedoc
526 (goto-char (point-max))
527 (let ((inhibit-read-only t))
528 (insert "\n\n" (make-string 30 ?-) "\n\n" (symbol-name symbol)
529 " is also a " "face." "\n\n" facedoc))
530 ;; Don't record the `describe-function' item in the stack.
531 (setq help-xref-stack-item nil)
532 (help-setup-xref (list #'help-xref-interned symbol) nil)))))))
535 ;; Navigation/hyperlinking with xrefs
537 (defun help-follow-mouse (click)
538 "Follow the cross-reference that you CLICK on."
539 (interactive "e")
540 (let* ((start (event-start click))
541 (window (car start))
542 (pos (car (cdr start))))
543 (with-current-buffer (window-buffer window)
544 (help-follow pos))))
546 (defun help-xref-go-back (buffer)
547 "From BUFFER, go back to previous help buffer text using `help-xref-stack'."
548 (let (item position method args)
549 (with-current-buffer buffer
550 (when help-xref-stack
551 (setq item (pop help-xref-stack)
552 ;; Clear the current item so that it won't get pushed
553 ;; by the function we're about to call. TODO: We could also
554 ;; push it onto a "forward" stack and add a `forw' button.
555 help-xref-stack-item nil
556 position (car item)
557 method (cadr item)
558 args (cddr item))))
559 (apply method args)
560 (with-current-buffer buffer
561 (if (get-buffer-window buffer)
562 (set-window-point (get-buffer-window buffer) position)
563 (goto-char position)))))
565 (defun help-go-back ()
566 "Invoke the [back] button (if any) in the Help mode buffer."
567 (interactive)
568 (let ((back-button (button-at (1- (point-max)))))
569 (if back-button
570 (button-activate back-button)
571 (error "No [back] button"))))
573 (defun help-do-xref (pos function args)
574 "Call the help cross-reference function FUNCTION with args ARGS.
575 Things are set up properly so that the resulting help-buffer has
576 a proper [back] button."
577 ;; There is a reference at point. Follow it.
578 (let ((help-xref-following t))
579 (apply function args)))
581 (defun help-follow (&optional pos)
582 "Follow cross-reference at POS, defaulting to point.
584 For the cross-reference format, see `help-make-xrefs'."
585 (interactive "d")
586 (unless pos
587 (setq pos (point)))
588 (unless (push-button pos)
589 ;; check if the symbol under point is a function or variable
590 (let ((sym
591 (intern
592 (save-excursion
593 (goto-char pos) (skip-syntax-backward "w_")
594 (buffer-substring (point)
595 (progn (skip-syntax-forward "w_")
596 (point)))))))
597 (when (or (boundp sym) (fboundp sym) (facep sym))
598 (help-do-xref pos #'help-xref-interned (list sym))))))
601 (provide 'help-mode)
603 ;;; arch-tag: 850954ae-3725-4cb4-8e91-0bf6d52d6b0b
604 ;;; help-mode.el ends here