Remove debugging code. From Stefan Monnier <monnier@cs.yale.edu>.
[emacs/old-mirror.git] / lisp / help-mode.el
blobb2b7690101a198eac2d66c572c4e82976f7fc0c5
1 ;;; help-mode.el --- `help-mode' used by *Help* buffers
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001
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 (eval-when-compile (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)
65 ;; Button types used by help
67 ;; Make some button types that all use the same naming conventions
68 (dolist (help-type '("function" "variable" "face"
69 "coding-system" "input-method" "character-set"))
70 (define-button-type (intern (purecopy (concat "help-" help-type)))
71 'help-function (intern (concat "describe-" help-type))
72 'help-echo (purecopy (concat "mouse-2, RET: describe this " help-type))
73 'action #'help-button-action))
75 ;; make some more ideosyncratic button types
77 (define-button-type 'help-symbol
78 'help-function #'help-xref-interned
79 'help-echo (purecopy "mouse-2, RET: describe this symbol")
80 'action #'help-button-action)
82 (define-button-type 'help-back
83 'help-function #'help-xref-go-back
84 'help-echo (purecopy "mouse-2, RET: go back to previous help buffer")
85 'action #'help-button-action)
87 (define-button-type 'help-info
88 'help-function #'info
89 'help-echo (purecopy"mouse-2, RET: read this Info node")
90 'action #'help-button-action)
92 (define-button-type 'help-customize-variable
93 'help-function (lambda (v)
94 (if help-xref-stack
95 (pop help-xref-stack))
96 (customize-variable v))
97 'help-echo (purecopy "mouse-2, RET: customize variable")
98 'action #'help-button-action)
100 (define-button-type 'help-customize-face
101 'help-function (lambda (v)
102 (if help-xref-stack
103 (pop help-xref-stack))
104 (customize-face v))
105 'help-echo (purecopy "mouse-2, RET: customize face")
106 'action #'help-button-action)
108 (define-button-type 'help-function-def
109 'help-function (lambda (fun file)
110 (require 'find-func)
111 ;; Don't use find-function-noselect because it follows
112 ;; aliases (which fails for built-in functions).
113 (let* ((location (find-function-search-for-symbol
114 fun nil file)))
115 (pop-to-buffer (car location))
116 (goto-char (cdr location))))
117 'help-echo (purecopy "mouse-2, RET: find function's definition")
118 'action #'help-button-action)
120 (define-button-type 'help-variable-def
121 'help-function (lambda (var &optional file)
122 (let ((location
123 (find-variable-noselect var file)))
124 (pop-to-buffer (car location))
125 (goto-char (cdr location))))
126 'help-echo (purecopy"mouse-2, RET: find variable's definition")
127 'action #'help-button-action)
129 (defun help-button-action (button)
130 "Call BUTTON's help function."
131 (help-do-xref (button-start button)
132 (button-get button 'help-function)
133 (button-get button 'help-args)))
136 ;;;###autoload
137 (define-derived-mode help-mode nil "Help"
138 "Major mode for viewing help text and navigating references in it.
139 Entry to this mode runs the normal hook `help-mode-hook'.
140 Commands:
141 \\{help-mode-map}"
142 (setq font-lock-defaults nil) ; font-lock would defeat xref
143 (view-mode)
144 (make-local-variable 'view-no-disable-on-exit)
145 (setq view-no-disable-on-exit t))
147 ;;;###autoload
148 (defun help-mode-setup ()
149 (help-mode)
150 (setq buffer-read-only nil))
152 ;;;###autoload
153 (defun help-mode-finish ()
154 (when (eq major-mode 'help-mode)
155 ;; View mode's read-only status of existing *Help* buffer is lost
156 ;; by with-output-to-temp-buffer.
157 (toggle-read-only 1)
158 (help-make-xrefs (current-buffer)))
159 (setq view-return-to-alist
160 (list (cons (selected-window) help-return-method))))
163 ;;; Grokking cross-reference information in doc strings and
164 ;;; hyperlinking it.
166 ;; This may have some scope for extension and the same or something
167 ;; similar should be done for widget doc strings, which currently use
168 ;; another mechanism.
170 (defcustom help-highlight-p t
171 "*If non-nil, `help-make-xrefs' highlight cross-references.
172 Under a window system it highlights them with face defined by
173 `help-highlight-face'."
174 :group 'help
175 :version "20.3"
176 :type 'boolean)
178 (defcustom help-highlight-face 'underline
179 "Face used by `help-make-xrefs' to highlight cross-references.
180 Must be previously-defined."
181 :group 'help
182 :version "20.3"
183 :type 'face)
185 (defvar help-back-label (purecopy "[back]")
186 "Label to use by `help-make-xrefs' for the go-back reference.")
188 (defconst help-xref-symbol-regexp
189 (purecopy (concat "\\(\\<\\(\\(variable\\|option\\)\\|"
190 "\\(function\\|command\\)\\|"
191 "\\(face\\)\\|"
192 "\\(symbol\\)\\|"
193 "\\(source \\(?:code \\)?\\(?:of\\|for\\)\\)\\)\\s-+\\)?"
194 ;; Note starting with word-syntax character:
195 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'"))
196 "Regexp matching doc string references to symbols.
198 The words preceding the quoted symbol can be used in doc strings to
199 distinguish references to variables, functions and symbols.")
201 (defconst help-xref-mule-regexp nil
202 "Regexp matching doc string references to MULE-related keywords.
204 It is usually nil, and is temporarily bound to an appropriate regexp
205 when help commands related to multilingual environment (e.g.,
206 `describe-coding-system') are invoked.")
209 (defconst help-xref-info-regexp
210 (purecopy "\\<[Ii]nfo[ \t\n]+node[ \t\n]+`\\([^']+\\)'")
211 "Regexp matching doc string references to an Info node.")
213 ;;;###autoload
214 (defun help-setup-xref (item interactive-p)
215 "Invoked from commands using the \"*Help*\" buffer to install some xref info.
217 ITEM is a (FUNCTION . ARGS) pair appropriate for recreating the help
218 buffer after following a reference. INTERACTIVE-P is non-nil if the
219 calling command was invoked interactively. In this case the stack of
220 items for help buffer \"back\" buttons is cleared.
222 This should be called very early, before the output buffer is cleared,
223 because we want to record the \"previous\" position of point so we can
224 restore it properly when going back."
225 (with-current-buffer (help-buffer)
226 (if interactive-p
227 ;; Why do we want to prevent the user from going back ?? -stef
228 (setq help-xref-stack nil)
229 (when help-xref-stack-item
230 (push (cons (point) help-xref-stack-item) help-xref-stack)))
231 (setq help-xref-stack-item item)))
233 (defvar help-xref-following nil
234 "Non-nil when following a help cross-reference.")
236 (defun help-buffer ()
237 (buffer-name ;for with-output-to-temp-buffer
238 (if help-xref-following
239 (current-buffer)
240 (get-buffer-create "*Help*"))))
242 ;;;###autoload
243 (defun help-make-xrefs (&optional buffer)
244 "Parse and hyperlink documentation cross-references in the given BUFFER.
246 Find cross-reference information in a buffer and, if
247 `help-highlight-p' is non-nil, highlight it with face defined by
248 `help-highlight-face'; activate such cross references for selection
249 with `help-follow'. Cross-references have the canonical form `...'
250 and the type of reference may be disambiguated by the preceding
251 word(s) used in `help-xref-symbol-regexp'.
253 If the variable `help-xref-mule-regexp' is non-nil, find also
254 cross-reference information related to multilingual environment
255 \(e.g., coding-systems). This variable is also used to disambiguate
256 the type of reference as the same way as `help-xref-symbol-regexp'.
258 A special reference `back' is made to return back through a stack of
259 help buffers. Variable `help-back-label' specifies the text for
260 that."
261 (interactive "b")
262 (save-excursion
263 (set-buffer (or buffer (current-buffer)))
264 (goto-char (point-min))
265 ;; Skip the header-type info, though it might be useful to parse
266 ;; it at some stage (e.g. "function in `library'").
267 (forward-paragraph)
268 (let ((old-modified (buffer-modified-p)))
269 (let ((stab (syntax-table))
270 (case-fold-search t)
271 (inhibit-read-only t))
272 (set-syntax-table emacs-lisp-mode-syntax-table)
273 ;; The following should probably be abstracted out.
274 (unwind-protect
275 (progn
276 ;; Info references
277 (save-excursion
278 (while (re-search-forward help-xref-info-regexp nil t)
279 (let ((data (match-string 1)))
280 (save-match-data
281 (unless (string-match "^([^)]+)" data)
282 (setq data (concat "(emacs)" data))))
283 (help-xref-button 1 'help-info data))))
284 ;; Mule related keywords. Do this before trying
285 ;; `help-xref-symbol-regexp' because some of Mule
286 ;; keywords have variable or function definitions.
287 (if help-xref-mule-regexp
288 (save-excursion
289 (while (re-search-forward help-xref-mule-regexp nil t)
290 (let* ((data (match-string 7))
291 (sym (intern-soft data)))
292 (cond
293 ((match-string 3) ; coding system
294 (and sym (coding-system-p sym)
295 (help-xref-button 6 'help-coding-system sym)))
296 ((match-string 4) ; input method
297 (and (assoc data input-method-alist)
298 (help-xref-button 7 'help-input-method data)))
299 ((or (match-string 5) (match-string 6)) ; charset
300 (and sym (charsetp sym)
301 (help-xref-button 7 'help-character-set sym)))
302 ((assoc data input-method-alist)
303 (help-xref-button 7 'help-character-set data))
304 ((and sym (coding-system-p sym))
305 (help-xref-button 7 'help-coding-system sym))
306 ((and sym (charsetp sym))
307 (help-xref-button 7 'help-character-set sym)))))))
308 ;; Quoted symbols
309 (save-excursion
310 (while (re-search-forward help-xref-symbol-regexp nil t)
311 (let* ((data (match-string 8))
312 (sym (intern-soft data)))
313 (if sym
314 (cond
315 ((match-string 3) ; `variable' &c
316 (and (boundp sym) ; `variable' doesn't ensure
317 ; it's actually bound
318 (help-xref-button 8 'help-variable sym)))
319 ((match-string 4) ; `function' &c
320 (and (fboundp sym) ; similarly
321 (help-xref-button 8 'help-function sym)))
322 ((match-string 5) ; `face'
323 (and (facep sym)
324 (help-xref-button 8 'help-face sym)))
325 ((match-string 6)) ; nothing for `symbol'
326 ((match-string 7)
327 ;; this used:
328 ;; #'(lambda (arg)
329 ;; (let ((location
330 ;; (find-function-noselect arg)))
331 ;; (pop-to-buffer (car location))
332 ;; (goto-char (cdr location))))
333 (help-xref-button 8 'help-function-def sym))
334 ((and (boundp sym) (fboundp sym))
335 ;; We can't intuit whether to use the
336 ;; variable or function doc -- supply both.
337 (help-xref-button 8 'help-symbol sym))
338 ((boundp sym)
339 (help-xref-button 8 'help-variable sym))
340 ((fboundp sym)
341 (help-xref-button 8 'help-function sym))
342 ((facep sym)
343 (help-xref-button 8 'help-face sym)))))))
344 ;; An obvious case of a key substitution:
345 (save-excursion
346 (while (re-search-forward
347 ;; Assume command name is only word characters
348 ;; and dashes to get things like `use M-x foo.'.
349 "\\<M-x\\s-+\\(\\sw\\(\\sw\\|-\\)+\\)" nil t)
350 (let ((sym (intern-soft (match-string 1))))
351 (if (fboundp sym)
352 (help-xref-button 1 'help-function sym)))))
353 ;; Look for commands in whole keymap substitutions:
354 (save-excursion
355 ;; Make sure to find the first keymap.
356 (goto-char (point-min))
357 ;; Find a header and the column at which the command
358 ;; name will be found.
359 (while (re-search-forward "^key +binding\n\\(-+ +\\)-+\n\n"
360 nil t)
361 (let ((col (- (match-end 1) (match-beginning 1))))
362 (while
363 ;; Ignore single blank lines in table, but not
364 ;; double ones, which should terminate it.
365 (and (not (looking-at "\n\\s-*\n"))
366 (progn
367 (and (eolp) (forward-line))
368 (end-of-line)
369 (skip-chars-backward "^\t\n")
370 (if (and (>= (current-column) col)
371 (looking-at "\\(\\sw\\|-\\)+$"))
372 (let ((sym (intern-soft (match-string 0))))
373 (if (fboundp sym)
374 (help-xref-button 0 'help-function sym))))
375 (zerop (forward-line)))))))))
376 (set-syntax-table stab))
377 ;; Delete extraneous newlines at the end of the docstring
378 (goto-char (point-max))
379 (while (and (not (bobp)) (bolp))
380 (delete-char -1))
381 ;; Make a back-reference in this buffer if appropriate.
382 (when help-xref-stack
383 (insert "\n\n")
384 (help-insert-xref-button help-back-label 'help-back
385 (current-buffer))))
386 ;; View mode steals RET from us.
387 (set (make-local-variable 'minor-mode-overriding-map-alist)
388 (list (cons 'view-mode
389 (let ((map (make-sparse-keymap)))
390 (set-keymap-parent map view-mode-map)
391 (define-key map "\r" 'help-follow)
392 map))))
393 (set-buffer-modified-p old-modified))))
395 ;;;###autoload
396 (defun help-xref-button (match-number type &rest args)
397 "Make a hyperlink for cross-reference text previously matched.
398 MATCH-NUMBER is the subexpression of interest in the last matched
399 regexp. TYPE is the type of button to use. Any remaining arguments are
400 passed to the button's help-function when it is invoked.
401 See `help-make-xrefs'."
402 ;; Don't mung properties we've added specially in some instances.
403 (unless (button-at (match-beginning match-number))
404 (make-text-button (match-beginning match-number)
405 (match-end match-number)
406 'type type 'help-args args)))
408 ;;;###autoload
409 (defun help-insert-xref-button (string type &rest args)
410 "Insert STRING and make a hyperlink from cross-reference text on it.
411 TYPE is the type of button to use. Any remaining arguments are passed
412 to the button's help-function when it is invoked.
413 See `help-make-xrefs'."
414 (unless (button-at (point))
415 (insert-text-button string 'type type 'help-args args)))
417 ;;;###autoload
418 (defun help-xref-on-pp (from to)
419 "Add xrefs for symbols in `pp's output between FROM and TO."
420 (let ((ost (syntax-table)))
421 (unwind-protect
422 (save-excursion
423 (save-restriction
424 (set-syntax-table emacs-lisp-mode-syntax-table)
425 (narrow-to-region from to)
426 (goto-char (point-min))
427 (while (not (eobp))
428 (cond
429 ((looking-at "\"") (forward-sexp 1))
430 ((looking-at "#<") (search-forward ">" nil 'move))
431 ((looking-at "\\(\\(\\sw\\|\\s_\\)+\\)")
432 (let* ((sym (intern-soft (match-string 1)))
433 (type (cond ((fboundp sym) 'help-function)
434 ((or (memq sym '(t nil))
435 (keywordp sym))
436 nil)
437 ((and sym (boundp sym))
438 'help-variable))))
439 (when type (help-xref-button 1 type sym)))
440 (goto-char (match-end 1)))
441 (t (forward-char 1))))))
442 (set-syntax-table ost))))
445 ;; Additional functions for (re-)creating types of help buffers.
446 (defun help-xref-interned (symbol)
447 "Follow a hyperlink which appeared to be an arbitrary interned SYMBOL.
448 Both variable, function and face documentation are extracted into a single
449 help buffer."
450 (with-current-buffer (help-buffer)
451 ;; Push the previous item on the stack before clobbering the output buffer.
452 (help-setup-xref nil nil)
453 (let ((facedoc (when (facep symbol)
454 ;; Don't record the current entry in the stack.
455 (setq help-xref-stack-item nil)
456 (describe-face symbol)))
457 (fdoc (when (fboundp symbol)
458 ;; Don't record the current entry in the stack.
459 (setq help-xref-stack-item nil)
460 (describe-function symbol)))
461 (sdoc (when (boundp symbol)
462 ;; Don't record the current entry in the stack.
463 (setq help-xref-stack-item nil)
464 (describe-variable symbol))))
465 (cond
466 (sdoc
467 ;; We now have a help buffer on the variable.
468 ;; Insert the function and face text before it.
469 (when (or fdoc facedoc)
470 (goto-char (point-min))
471 (let ((inhibit-read-only t))
472 (when fdoc
473 (insert fdoc "\n\n")
474 (when facedoc
475 (insert (make-string 30 ?-) "\n\n" (symbol-name symbol)
476 " is also a " "face." "\n\n")))
477 (when facedoc
478 (insert facedoc "\n\n"))
479 (insert (make-string 30 ?-) "\n\n" (symbol-name symbol)
480 " is also a " "variable." "\n\n"))
481 ;; Don't record the `describe-variable' item in the stack.
482 (setq help-xref-stack-item nil)
483 (help-setup-xref (list #'help-xref-interned symbol) nil)))
484 (fdoc
485 ;; We now have a help buffer on the function.
486 ;; Insert face text before it.
487 (when facedoc
488 (goto-char (point-max))
489 (let ((inhibit-read-only t))
490 (insert "\n\n" (make-string 30 ?-) "\n\n" (symbol-name symbol)
491 " is also a " "face." "\n\n" facedoc))
492 ;; Don't record the `describe-function' item in the stack.
493 (setq help-xref-stack-item nil)
494 (help-setup-xref (list #'help-xref-interned symbol) nil)))))))
497 ;;; Navigation/hyperlinking with xrefs
499 (defun help-follow-mouse (click)
500 "Follow the cross-reference that you CLICK on."
501 (interactive "e")
502 (let* ((start (event-start click))
503 (window (car start))
504 (pos (car (cdr start))))
505 (with-current-buffer (window-buffer window)
506 (help-follow pos))))
508 (defun help-xref-go-back (buffer)
509 "From BUFFER, go back to previous help buffer text using `help-xref-stack'."
510 (let (item position method args)
511 (with-current-buffer buffer
512 (when help-xref-stack
513 (setq item (pop help-xref-stack)
514 ;; Clear the current item so that it won't get pushed
515 ;; by the function we're about to call. TODO: We could also
516 ;; push it onto a "forward" stack and add a `forw' button.
517 help-xref-stack-item nil
518 position (car item)
519 method (cadr item)
520 args (cddr item))))
521 (apply method args)
522 ;; FIXME: are we sure we're in the right buffer ?
523 (goto-char position)))
525 (defun help-go-back ()
526 "Invoke the [back] button (if any) in the Help mode buffer."
527 (interactive)
528 (let ((back-button (button-at (1- (point-max)))))
529 (if back-button
530 (button-activate back-button)
531 (error "No [back] button"))))
533 (defun help-do-xref (pos function args)
534 "Call the help cross-reference function FUNCTION with args ARGS.
535 Things are set up properly so that the resulting help-buffer has
536 a proper [back] button."
537 ;; There is a reference at point. Follow it.
538 (let ((help-xref-following t))
539 (apply function args)))
541 (defun help-follow (&optional pos)
542 "Follow cross-reference at POS, defaulting to point.
544 For the cross-reference format, see `help-make-xrefs'."
545 (interactive "d")
546 (unless pos
547 (setq pos (point)))
548 (unless (push-button pos)
549 ;; check if the symbol under point is a function or variable
550 (let ((sym
551 (intern
552 (save-excursion
553 (goto-char pos) (skip-syntax-backward "w_")
554 (buffer-substring (point)
555 (progn (skip-syntax-forward "w_")
556 (point)))))))
557 (when (or (boundp sym) (fboundp sym) (facep sym))
558 (help-do-xref pos #'help-xref-interned (list sym))))))
561 (provide 'help-mode)
563 ;;; help-mode.el ends here