New file.
[bpt/emacs.git] / lisp / help-mode.el
1 ;;; help-mode.el --- `help-mode' used by *Help* buffers
2
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002
4 ;; Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: help, internal
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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.
25
26 ;;; Commentary:
27
28 ;; Defines `help-mode', which is the mode used by *Help* buffers, and
29 ;; associated support machinery, such as adding hyperlinks, etc.,
30
31 ;;; Code:
32
33 (require 'button)
34 (require 'view)
35
36 (defvar help-mode-map (make-sparse-keymap)
37 "Keymap for help mode.")
38
39 (set-keymap-parent help-mode-map button-buffer-map)
40
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)
46
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)
54
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)
60
61 (setq-default help-xref-stack nil help-xref-stack-item nil)
62
63 (defcustom help-mode-hook nil
64 "Hook run by `help-mode'."
65 :type 'hook
66 :group 'help)
67 \f
68 ;; Button types used by help
69
70 (define-button-type 'help-xref
71 'action #'help-button-action)
72
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)))
78
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.
82
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"))
87
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"))
92
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"))
97
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"))
102
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"))
107
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"))
112
113 ;; make some more ideosyncratic button types
114
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"))
119
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"))
124
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"))
129
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"))
137
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"))
145
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"))
158
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"))
167
168 \f
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))
184
185 ;;;###autoload
186 (defun help-mode-setup ()
187 (help-mode)
188 (setq buffer-read-only nil))
189
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))))
199
200 \f
201 ;; Grokking cross-reference information in doc strings and
202 ;; hyperlinking it.
203
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.
207
208 (defvar help-back-label (purecopy "[back]")
209 "Label to use by `help-make-xrefs' for the go-back reference.")
210
211 (defconst help-xref-symbol-regexp
212 (purecopy (concat "\\(\\<\\(\\(variable\\|option\\)\\|"
213 "\\(function\\|command\\)\\|"
214 "\\(face\\)\\|"
215 "\\(symbol\\)\\|"
216 "\\(source \\(?:code \\)?\\(?:of\\|for\\)\\)\\)\\s-+\\)?"
217 ;; Note starting with word-syntax character:
218 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'"))
219 "Regexp matching doc string references to symbols.
220
221 The words preceding the quoted symbol can be used in doc strings to
222 distinguish references to variables, functions and symbols.")
223
224 (defvar help-xref-mule-regexp nil
225 "Regexp matching doc string references to MULE-related keywords.
226
227 It is usually nil, and is temporarily bound to an appropriate regexp
228 when help commands related to multilingual environment (e.g.,
229 `describe-coding-system') are invoked.")
230
231
232 (defconst help-xref-info-regexp
233 (purecopy "\\<[Ii]nfo[ \t\n]+node[ \t\n]+`\\([^']+\\)'")
234 "Regexp matching doc string references to an Info node.")
235
236 ;;;###autoload
237 (defun help-setup-xref (item interactive-p)
238 "Invoked from commands using the \"*Help*\" buffer to install some xref info.
239
240 ITEM is a (FUNCTION . ARGS) pair appropriate for recreating the help
241 buffer after following a reference. INTERACTIVE-P is non-nil if the
242 calling command was invoked interactively. In this case the stack of
243 items for help buffer \"back\" buttons is cleared.
244
245 This should be called very early, before the output buffer is cleared,
246 because we want to record the \"previous\" position of point so we can
247 restore it properly when going back."
248 (with-current-buffer (help-buffer)
249 (when help-xref-stack-item
250 (push (cons (point) help-xref-stack-item) help-xref-stack))
251 (when interactive-p
252 (let ((tail (nthcdr 10 help-xref-stack)))
253 ;; Truncate the stack.
254 (if tail (setcdr tail nil))))
255 (setq help-xref-stack-item item)))
256
257 (defvar help-xref-following nil
258 "Non-nil when following a help cross-reference.")
259
260 (defun help-buffer ()
261 (buffer-name ;for with-output-to-temp-buffer
262 (if help-xref-following
263 (current-buffer)
264 (get-buffer-create "*Help*"))))
265
266 (defvar help-xref-override-view-map
267 (let ((map (make-sparse-keymap)))
268 (set-keymap-parent map view-mode-map)
269 (define-key map "\r" nil)
270 map)
271 "Replacement keymap for `view-mode' in help buffers.")
272
273 ;;;###autoload
274 (defun help-make-xrefs (&optional buffer)
275 "Parse and hyperlink documentation cross-references in the given BUFFER.
276
277 Find cross-reference information in a buffer and activate such cross
278 references for selection with `help-follow'. Cross-references have
279 the canonical form `...' and the type of reference may be
280 disambiguated by the preceding word(s) used in
281 `help-xref-symbol-regexp'.
282
283 If the variable `help-xref-mule-regexp' is non-nil, find also
284 cross-reference information related to multilingual environment
285 \(e.g., coding-systems). This variable is also used to disambiguate
286 the type of reference as the same way as `help-xref-symbol-regexp'.
287
288 A special reference `back' is made to return back through a stack of
289 help buffers. Variable `help-back-label' specifies the text for
290 that."
291 (interactive "b")
292 (save-excursion
293 (set-buffer (or buffer (current-buffer)))
294 (goto-char (point-min))
295 ;; Skip the header-type info, though it might be useful to parse
296 ;; it at some stage (e.g. "function in `library'").
297 (forward-paragraph)
298 (let ((old-modified (buffer-modified-p)))
299 (let ((stab (syntax-table))
300 (case-fold-search t)
301 (inhibit-read-only t))
302 (set-syntax-table emacs-lisp-mode-syntax-table)
303 ;; The following should probably be abstracted out.
304 (unwind-protect
305 (progn
306 ;; Info references
307 (save-excursion
308 (while (re-search-forward help-xref-info-regexp nil t)
309 (let ((data (match-string 1)))
310 (save-match-data
311 (unless (string-match "^([^)]+)" data)
312 (setq data (concat "(emacs)" data))))
313 (help-xref-button 1 'help-info data))))
314 ;; Mule related keywords. Do this before trying
315 ;; `help-xref-symbol-regexp' because some of Mule
316 ;; keywords have variable or function definitions.
317 (if help-xref-mule-regexp
318 (save-excursion
319 (while (re-search-forward help-xref-mule-regexp nil t)
320 (let* ((data (match-string 7))
321 (sym (intern-soft data)))
322 (cond
323 ((match-string 3) ; coding system
324 (and sym (coding-system-p sym)
325 (help-xref-button 6 'help-coding-system sym)))
326 ((match-string 4) ; input method
327 (and (assoc data input-method-alist)
328 (help-xref-button 7 'help-input-method data)))
329 ((or (match-string 5) (match-string 6)) ; charset
330 (and sym (charsetp sym)
331 (help-xref-button 7 'help-character-set sym)))
332 ((assoc data input-method-alist)
333 (help-xref-button 7 'help-character-set data))
334 ((and sym (coding-system-p sym))
335 (help-xref-button 7 'help-coding-system sym))
336 ((and sym (charsetp sym))
337 (help-xref-button 7 'help-character-set sym)))))))
338 ;; Quoted symbols
339 (save-excursion
340 (while (re-search-forward help-xref-symbol-regexp nil t)
341 (let* ((data (match-string 8))
342 (sym (intern-soft data)))
343 (if sym
344 (cond
345 ((match-string 3) ; `variable' &c
346 (and (boundp sym) ; `variable' doesn't ensure
347 ; it's actually bound
348 (help-xref-button 8 'help-variable sym)))
349 ((match-string 4) ; `function' &c
350 (and (fboundp sym) ; similarly
351 (help-xref-button 8 'help-function sym)))
352 ((match-string 5) ; `face'
353 (and (facep sym)
354 (help-xref-button 8 'help-face sym)))
355 ((match-string 6)) ; nothing for `symbol'
356 ((match-string 7)
357 ;; this used:
358 ;; #'(lambda (arg)
359 ;; (let ((location
360 ;; (find-function-noselect arg)))
361 ;; (pop-to-buffer (car location))
362 ;; (goto-char (cdr location))))
363 (help-xref-button 8 'help-function-def sym))
364 ((and (boundp sym) (fboundp sym))
365 ;; We can't intuit whether to use the
366 ;; variable or function doc -- supply both.
367 (help-xref-button 8 'help-symbol sym))
368 ((boundp sym)
369 (help-xref-button 8 'help-variable sym))
370 ((fboundp sym)
371 (help-xref-button 8 'help-function sym))
372 ((facep sym)
373 (help-xref-button 8 'help-face sym)))))))
374 ;; An obvious case of a key substitution:
375 (save-excursion
376 (while (re-search-forward
377 ;; Assume command name is only word characters
378 ;; and dashes to get things like `use M-x foo.'.
379 "\\<M-x\\s-+\\(\\sw\\(\\sw\\|-\\)+\\)" nil t)
380 (let ((sym (intern-soft (match-string 1))))
381 (if (fboundp sym)
382 (help-xref-button 1 'help-function sym)))))
383 ;; Look for commands in whole keymap substitutions:
384 (save-excursion
385 ;; Make sure to find the first keymap.
386 (goto-char (point-min))
387 ;; Find a header and the column at which the command
388 ;; name will be found.
389 (while (re-search-forward "^key +binding\n\\(-+ +\\)-+\n\n"
390 nil t)
391 (let ((col (- (match-end 1) (match-beginning 1))))
392 (while
393 ;; Ignore single blank lines in table, but not
394 ;; double ones, which should terminate it.
395 (and (not (looking-at "\n\\s-*\n"))
396 (progn
397 (and (eolp) (forward-line))
398 (end-of-line)
399 (skip-chars-backward "^\t\n")
400 (if (and (>= (current-column) col)
401 (looking-at "\\(\\sw\\|-\\)+$"))
402 (let ((sym (intern-soft (match-string 0))))
403 (if (fboundp sym)
404 (help-xref-button 0 'help-function sym))))
405 (zerop (forward-line)))))))))
406 (set-syntax-table stab))
407 ;; Delete extraneous newlines at the end of the docstring
408 (goto-char (point-max))
409 (while (and (not (bobp)) (bolp))
410 (delete-char -1))
411 ;; Make a back-reference in this buffer if appropriate.
412 (when help-xref-stack
413 (insert "\n\n")
414 (help-insert-xref-button help-back-label 'help-back
415 (current-buffer))))
416 ;; View mode steals RET from us.
417 (set (make-local-variable 'minor-mode-overriding-map-alist)
418 (list (cons 'view-mode help-xref-override-view-map)))
419 (set-buffer-modified-p old-modified))))
420
421 ;;;###autoload
422 (defun help-xref-button (match-number type &rest args)
423 "Make a hyperlink for cross-reference text previously matched.
424 MATCH-NUMBER is the subexpression of interest in the last matched
425 regexp. TYPE is the type of button to use. Any remaining arguments are
426 passed to the button's help-function when it is invoked.
427 See `help-make-xrefs'."
428 ;; Don't mung properties we've added specially in some instances.
429 (unless (button-at (match-beginning match-number))
430 (make-text-button (match-beginning match-number)
431 (match-end match-number)
432 'type type 'help-args args)))
433
434 ;;;###autoload
435 (defun help-insert-xref-button (string type &rest args)
436 "Insert STRING and make a hyperlink from cross-reference text on it.
437 TYPE is the type of button to use. Any remaining arguments are passed
438 to the button's help-function when it is invoked.
439 See `help-make-xrefs'."
440 (unless (button-at (point))
441 (insert-text-button string 'type type 'help-args args)))
442
443 ;;;###autoload
444 (defun help-xref-on-pp (from to)
445 "Add xrefs for symbols in `pp's output between FROM and TO."
446 (let ((ost (syntax-table)))
447 (unwind-protect
448 (save-excursion
449 (save-restriction
450 (set-syntax-table emacs-lisp-mode-syntax-table)
451 (narrow-to-region from to)
452 (goto-char (point-min))
453 (condition-case nil
454 (while (not (eobp))
455 (cond
456 ((looking-at "\"") (forward-sexp 1))
457 ((looking-at "#<") (search-forward ">" nil 'move))
458 ((looking-at "\\(\\(\\sw\\|\\s_\\)+\\)")
459 (let* ((sym (intern-soft (match-string 1)))
460 (type (cond ((fboundp sym) 'help-function)
461 ((or (memq sym '(t nil))
462 (keywordp sym))
463 nil)
464 ((and sym (boundp sym))
465 'help-variable))))
466 (when type (help-xref-button 1 type sym)))
467 (goto-char (match-end 1)))
468 (t (forward-char 1))))
469 (error nil))))
470 (set-syntax-table ost))))
471
472 \f
473 ;; Additional functions for (re-)creating types of help buffers.
474 (defun help-xref-interned (symbol)
475 "Follow a hyperlink which appeared to be an arbitrary interned SYMBOL.
476 Both variable, function and face documentation are extracted into a single
477 help buffer."
478 (with-current-buffer (help-buffer)
479 ;; Push the previous item on the stack before clobbering the output buffer.
480 (help-setup-xref nil nil)
481 (let ((facedoc (when (facep symbol)
482 ;; Don't record the current entry in the stack.
483 (setq help-xref-stack-item nil)
484 (describe-face symbol)))
485 (fdoc (when (fboundp symbol)
486 ;; Don't record the current entry in the stack.
487 (setq help-xref-stack-item nil)
488 (describe-function symbol)))
489 (sdoc (when (boundp symbol)
490 ;; Don't record the current entry in the stack.
491 (setq help-xref-stack-item nil)
492 (describe-variable symbol))))
493 (cond
494 (sdoc
495 ;; We now have a help buffer on the variable.
496 ;; Insert the function and face text before it.
497 (when (or fdoc facedoc)
498 (goto-char (point-min))
499 (let ((inhibit-read-only t))
500 (when fdoc
501 (insert fdoc "\n\n")
502 (when facedoc
503 (insert (make-string 30 ?-) "\n\n" (symbol-name symbol)
504 " is also a " "face." "\n\n")))
505 (when facedoc
506 (insert facedoc "\n\n"))
507 (insert (make-string 30 ?-) "\n\n" (symbol-name symbol)
508 " is also a " "variable." "\n\n"))
509 ;; Don't record the `describe-variable' item in the stack.
510 (setq help-xref-stack-item nil)
511 (help-setup-xref (list #'help-xref-interned symbol) nil)))
512 (fdoc
513 ;; We now have a help buffer on the function.
514 ;; Insert face text before it.
515 (when facedoc
516 (goto-char (point-max))
517 (let ((inhibit-read-only t))
518 (insert "\n\n" (make-string 30 ?-) "\n\n" (symbol-name symbol)
519 " is also a " "face." "\n\n" facedoc))
520 ;; Don't record the `describe-function' item in the stack.
521 (setq help-xref-stack-item nil)
522 (help-setup-xref (list #'help-xref-interned symbol) nil)))))))
523
524 \f
525 ;; Navigation/hyperlinking with xrefs
526
527 (defun help-follow-mouse (click)
528 "Follow the cross-reference that you CLICK on."
529 (interactive "e")
530 (let* ((start (event-start click))
531 (window (car start))
532 (pos (car (cdr start))))
533 (with-current-buffer (window-buffer window)
534 (help-follow pos))))
535
536 (defun help-xref-go-back (buffer)
537 "From BUFFER, go back to previous help buffer text using `help-xref-stack'."
538 (let (item position method args)
539 (with-current-buffer buffer
540 (when help-xref-stack
541 (setq item (pop help-xref-stack)
542 ;; Clear the current item so that it won't get pushed
543 ;; by the function we're about to call. TODO: We could also
544 ;; push it onto a "forward" stack and add a `forw' button.
545 help-xref-stack-item nil
546 position (car item)
547 method (cadr item)
548 args (cddr item))))
549 (apply method args)
550 (with-current-buffer buffer
551 (if (get-buffer-window buffer)
552 (set-window-point (get-buffer-window buffer) position)
553 (goto-char position)))))
554
555 (defun help-go-back ()
556 "Invoke the [back] button (if any) in the Help mode buffer."
557 (interactive)
558 (let ((back-button (button-at (1- (point-max)))))
559 (if back-button
560 (button-activate back-button)
561 (error "No [back] button"))))
562
563 (defun help-do-xref (pos function args)
564 "Call the help cross-reference function FUNCTION with args ARGS.
565 Things are set up properly so that the resulting help-buffer has
566 a proper [back] button."
567 ;; There is a reference at point. Follow it.
568 (let ((help-xref-following t))
569 (apply function args)))
570
571 (defun help-follow (&optional pos)
572 "Follow cross-reference at POS, defaulting to point.
573
574 For the cross-reference format, see `help-make-xrefs'."
575 (interactive "d")
576 (unless pos
577 (setq pos (point)))
578 (unless (push-button pos)
579 ;; check if the symbol under point is a function or variable
580 (let ((sym
581 (intern
582 (save-excursion
583 (goto-char pos) (skip-syntax-backward "w_")
584 (buffer-substring (point)
585 (progn (skip-syntax-forward "w_")
586 (point)))))))
587 (when (or (boundp sym) (fboundp sym) (facep sym))
588 (help-do-xref pos #'help-xref-interned (list sym))))))
589
590
591 (provide 'help-mode)
592
593 ;;; help-mode.el ends here