(c-guess-basic-syntax): Remove spurious call to `zerop'.
[emacs.git] / lisp / help-mode.el
blobd94f03200dc25e51bb0466ba8ebd10fe6c8a71ba
1 ;;; help-mode.el --- `help-mode' used by *Help* buffers
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002, 2004
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 'follow-link t
72 'action #'help-button-action)
74 (defun help-button-action (button)
75 "Call BUTTON's help function."
76 (help-do-xref (button-start button)
77 (button-get button 'help-function)
78 (button-get button 'help-args)))
80 ;; These 6 calls to define-button-type were generated in a dolist
81 ;; loop, but that is bad because it means these button types don't
82 ;; have an easily found definition.
84 (define-button-type 'help-function
85 :supertype 'help-xref
86 'help-function 'describe-function
87 'help-echo (purecopy "mouse-2, RET: describe this function"))
89 (define-button-type 'help-variable
90 :supertype 'help-xref
91 'help-function 'describe-variable
92 'help-echo (purecopy "mouse-2, RET: describe this variable"))
94 (define-button-type 'help-face
95 :supertype 'help-xref
96 'help-function 'describe-face
97 'help-echo (purecopy "mouse-2, RET: describe this face"))
99 (define-button-type 'help-coding-system
100 :supertype 'help-xref
101 'help-function 'describe-coding-system
102 'help-echo (purecopy "mouse-2, RET: describe this coding system"))
104 (define-button-type 'help-input-method
105 :supertype 'help-xref
106 'help-function 'describe-input-method
107 'help-echo (purecopy "mouse-2, RET: describe this input method"))
109 (define-button-type 'help-character-set
110 :supertype 'help-xref
111 'help-function 'describe-character-set
112 'help-echo (purecopy "mouse-2, RET: describe this character set"))
114 ;; make some more ideosyncratic button types
116 (define-button-type 'help-symbol
117 :supertype 'help-xref
118 'help-function #'help-xref-interned
119 'help-echo (purecopy "mouse-2, RET: describe this symbol"))
121 (define-button-type 'help-back
122 :supertype 'help-xref
123 'help-function #'help-xref-go-back
124 'help-echo (purecopy "mouse-2, RET: go back to previous help buffer"))
126 (define-button-type 'help-info
127 :supertype 'help-xref
128 'help-function #'info
129 'help-echo (purecopy"mouse-2, RET: read this Info node"))
131 (define-button-type 'help-customize-variable
132 :supertype 'help-xref
133 'help-function (lambda (v)
134 (if help-xref-stack
135 (pop help-xref-stack))
136 (customize-variable v))
137 'help-echo (purecopy "mouse-2, RET: customize variable"))
139 (define-button-type 'help-customize-face
140 :supertype 'help-xref
141 'help-function (lambda (v)
142 (if help-xref-stack
143 (pop help-xref-stack))
144 (customize-face v))
145 'help-echo (purecopy "mouse-2, RET: customize face"))
147 (define-button-type 'help-function-def
148 :supertype 'help-xref
149 'help-function (lambda (fun file)
150 (require 'find-func)
151 (when (eq file 'C-source)
152 (setq file
153 (help-C-file-name (indirect-function fun) 'fun)))
154 ;; Don't use find-function-noselect because it follows
155 ;; aliases (which fails for built-in functions).
156 (let ((location
157 (find-function-search-for-symbol fun nil file)))
158 (pop-to-buffer (car location))
159 (goto-char (cdr location))))
160 'help-echo (purecopy "mouse-2, RET: find function's definition"))
162 (define-button-type 'help-variable-def
163 :supertype 'help-xref
164 'help-function (lambda (var &optional file)
165 (when (eq file 'C-source)
166 (setq file (help-C-file-name var 'var)))
167 (let ((location (find-variable-noselect var file)))
168 (pop-to-buffer (car location))
169 (goto-char (cdr location))))
170 'help-echo (purecopy"mouse-2, RET: find variable's definition"))
172 (define-button-type 'help-face-def
173 :supertype 'help-xref
174 'help-function (lambda (fun file)
175 (require 'find-func)
176 ;; Don't use find-function-noselect because it follows
177 ;; aliases (which fails for built-in functions).
178 (let ((location
179 (find-function-search-for-symbol fun 'defface file)))
180 (pop-to-buffer (car location))
181 (goto-char (cdr location))))
182 'help-echo (purecopy "mouse-2, RET: find face's definition"))
185 ;;;###autoload
186 (defun help-mode ()
187 "Major mode for viewing help text and navigating references in it.
188 Entry to this mode runs the normal hook `help-mode-hook'.
189 Commands:
190 \\{help-mode-map}"
191 (interactive)
192 (kill-all-local-variables)
193 (use-local-map help-mode-map)
194 (setq mode-name "Help")
195 (setq major-mode 'help-mode)
196 (view-mode)
197 (make-local-variable 'view-no-disable-on-exit)
198 (setq view-no-disable-on-exit t)
199 (run-hooks 'help-mode-hook))
201 ;;;###autoload
202 (defun help-mode-setup ()
203 (help-mode)
204 (setq buffer-read-only nil))
206 ;;;###autoload
207 (defun help-mode-finish ()
208 (let ((entry (assq (selected-window) view-return-to-alist)))
209 (if entry
210 ;; When entering Help mode from the Help window,
211 ;; such as by following a link, preserve the same
212 ;; meaning for the q command.
213 ;; (setcdr entry (cons (selected-window) help-return-method))
215 (setq view-return-to-alist
216 (cons (cons (selected-window) help-return-method)
217 view-return-to-alist))))
218 (when (eq major-mode 'help-mode)
219 ;; View mode's read-only status of existing *Help* buffer is lost
220 ;; by with-output-to-temp-buffer.
221 (toggle-read-only 1)
222 (help-make-xrefs (current-buffer))))
224 ;; Grokking cross-reference information in doc strings and
225 ;; hyperlinking it.
227 ;; This may have some scope for extension and the same or something
228 ;; similar should be done for widget doc strings, which currently use
229 ;; another mechanism.
231 (defvar help-back-label (purecopy "[back]")
232 "Label to use by `help-make-xrefs' for the go-back reference.")
234 (defconst help-xref-symbol-regexp
235 (purecopy (concat "\\(\\<\\(\\(variable\\|option\\)\\|"
236 "\\(function\\|command\\)\\|"
237 "\\(face\\)\\|"
238 "\\(symbol\\)\\|"
239 "\\(source \\(?:code \\)?\\(?:of\\|for\\)\\)\\)"
240 "[ \t\n]+\\)?"
241 ;; Note starting with word-syntax character:
242 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'"))
243 "Regexp matching doc string references to symbols.
245 The words preceding the quoted symbol can be used in doc strings to
246 distinguish references to variables, functions and symbols.")
248 (defvar help-xref-mule-regexp nil
249 "Regexp matching doc string references to MULE-related keywords.
251 It is usually nil, and is temporarily bound to an appropriate regexp
252 when help commands related to multilingual environment (e.g.,
253 `describe-coding-system') are invoked.")
256 (defconst help-xref-info-regexp
257 (purecopy "\\<[Ii]nfo[ \t\n]+\\(node\\|anchor\\)[ \t\n]+`\\([^']+\\)'")
258 "Regexp matching doc string references to an Info node.")
260 ;;;###autoload
261 (defun help-setup-xref (item interactive-p)
262 "Invoked from commands using the \"*Help*\" buffer to install some xref info.
264 ITEM is a (FUNCTION . ARGS) pair appropriate for recreating the help
265 buffer after following a reference. INTERACTIVE-P is non-nil if the
266 calling command was invoked interactively. In this case the stack of
267 items for help buffer \"back\" buttons is cleared.
269 This should be called very early, before the output buffer is cleared,
270 because we want to record the \"previous\" position of point so we can
271 restore it properly when going back."
272 (with-current-buffer (help-buffer)
273 (when help-xref-stack-item
274 (push (cons (point) help-xref-stack-item) help-xref-stack))
275 (when interactive-p
276 (let ((tail (nthcdr 10 help-xref-stack)))
277 ;; Truncate the stack.
278 (if tail (setcdr tail nil))))
279 (setq help-xref-stack-item item)))
281 (defvar help-xref-following nil
282 "Non-nil when following a help cross-reference.")
284 (defun help-buffer ()
285 (buffer-name ;for with-output-to-temp-buffer
286 (if help-xref-following
287 (current-buffer)
288 (get-buffer-create "*Help*"))))
290 (defvar help-xref-override-view-map
291 (let ((map (make-sparse-keymap)))
292 (set-keymap-parent map view-mode-map)
293 (define-key map "\r" nil)
294 map)
295 "Replacement keymap for `view-mode' in help buffers.")
297 ;;;###autoload
298 (defun help-make-xrefs (&optional buffer)
299 "Parse and hyperlink documentation cross-references in the given BUFFER.
301 Find cross-reference information in a buffer and activate such cross
302 references for selection with `help-follow'. Cross-references have
303 the canonical form `...' and the type of reference may be
304 disambiguated by the preceding word(s) used in
305 `help-xref-symbol-regexp'. Faces only get cross-referenced if
306 preceded or followed by the word `face'. Variables without
307 variable documentation do not get cross-referenced, unless
308 preceded by the word `variable' or `option'.
310 If the variable `help-xref-mule-regexp' is non-nil, find also
311 cross-reference information related to multilingual environment
312 \(e.g., coding-systems). This variable is also used to disambiguate
313 the type of reference as the same way as `help-xref-symbol-regexp'.
315 A special reference `back' is made to return back through a stack of
316 help buffers. Variable `help-back-label' specifies the text for
317 that."
318 (interactive "b")
319 (save-excursion
320 (set-buffer (or buffer (current-buffer)))
321 (goto-char (point-min))
322 ;; Skip the header-type info, though it might be useful to parse
323 ;; it at some stage (e.g. "function in `library'").
324 (forward-paragraph)
325 (let ((old-modified (buffer-modified-p)))
326 (let ((stab (syntax-table))
327 (case-fold-search t)
328 (inhibit-read-only t))
329 (set-syntax-table emacs-lisp-mode-syntax-table)
330 ;; The following should probably be abstracted out.
331 (unwind-protect
332 (progn
333 ;; Info references
334 (save-excursion
335 (while (re-search-forward help-xref-info-regexp nil t)
336 (let ((data (match-string 2)))
337 (save-match-data
338 (unless (string-match "^([^)]+)" data)
339 (setq data (concat "(emacs)" data))))
340 (help-xref-button 2 'help-info data))))
341 ;; Mule related keywords. Do this before trying
342 ;; `help-xref-symbol-regexp' because some of Mule
343 ;; keywords have variable or function definitions.
344 (if help-xref-mule-regexp
345 (save-excursion
346 (while (re-search-forward help-xref-mule-regexp nil t)
347 (let* ((data (match-string 7))
348 (sym (intern-soft data)))
349 (cond
350 ((match-string 3) ; coding system
351 (and sym (coding-system-p sym)
352 (help-xref-button 6 'help-coding-system sym)))
353 ((match-string 4) ; input method
354 (and (assoc data input-method-alist)
355 (help-xref-button 7 'help-input-method data)))
356 ((or (match-string 5) (match-string 6)) ; charset
357 (and sym (charsetp sym)
358 (help-xref-button 7 'help-character-set sym)))
359 ((assoc data input-method-alist)
360 (help-xref-button 7 'help-character-set data))
361 ((and sym (coding-system-p sym))
362 (help-xref-button 7 'help-coding-system sym))
363 ((and sym (charsetp sym))
364 (help-xref-button 7 'help-character-set sym)))))))
365 ;; Quoted symbols
366 (save-excursion
367 (while (re-search-forward help-xref-symbol-regexp nil t)
368 (let* ((data (match-string 8))
369 (sym (intern-soft data)))
370 (if sym
371 (cond
372 ((match-string 3) ; `variable' &c
373 (and (boundp sym) ; `variable' doesn't ensure
374 ; it's actually bound
375 (help-xref-button 8 'help-variable sym)))
376 ((match-string 4) ; `function' &c
377 (and (fboundp sym) ; similarly
378 (help-xref-button 8 'help-function sym)))
379 ((match-string 5) ; `face'
380 (and (facep sym)
381 (help-xref-button 8 'help-face sym)))
382 ((match-string 6)) ; nothing for `symbol'
383 ((match-string 7)
384 ;;; this used:
385 ;;; #'(lambda (arg)
386 ;;; (let ((location
387 ;;; (find-function-noselect arg)))
388 ;;; (pop-to-buffer (car location))
389 ;;; (goto-char (cdr location))))
390 (help-xref-button 8 'help-function-def sym))
391 ((facep sym)
392 (if (save-match-data (looking-at "[ \t\n]+face\\W"))
393 (help-xref-button 8 'help-face sym)))
394 ((and (boundp sym) (fboundp sym))
395 ;; We can't intuit whether to use the
396 ;; variable or function doc -- supply both.
397 (help-xref-button 8 'help-symbol sym))
398 ((and
399 (boundp sym)
401 (documentation-property
402 sym 'variable-documentation)
403 (condition-case nil
404 (documentation-property
405 (indirect-variable sym)
406 'variable-documentation)
407 (cyclic-variable-indirection nil))))
408 (help-xref-button 8 'help-variable sym))
409 ((fboundp sym)
410 (help-xref-button 8 'help-function sym)))))))
411 ;; An obvious case of a key substitution:
412 (save-excursion
413 (while (re-search-forward
414 ;; Assume command name is only word characters
415 ;; and dashes to get things like `use M-x foo.'.
416 "\\<M-x\\s-+\\(\\sw\\(\\sw\\|-\\)+\\)" nil t)
417 (let ((sym (intern-soft (match-string 1))))
418 (if (fboundp sym)
419 (help-xref-button 1 'help-function sym)))))
420 ;; Look for commands in whole keymap substitutions:
421 (save-excursion
422 ;; Make sure to find the first keymap.
423 (goto-char (point-min))
424 ;; Find a header and the column at which the command
425 ;; name will be found.
427 ;; If the keymap substitution isn't the last thing in
428 ;; the doc string, and if there is anything on the
429 ;; same line after it, this code won't recognize the end of it.
430 (while (re-search-forward "^key +binding\n\\(-+ +\\)-+\n\n"
431 nil t)
432 (let ((col (- (match-end 1) (match-beginning 1))))
433 (while
434 (and (not (eobp))
435 ;; Stop at a pair of blank lines.
436 (not (looking-at "\n\\s-*\n")))
437 ;; Skip a single blank line.
438 (and (eolp) (forward-line))
439 (end-of-line)
440 (skip-chars-backward "^\t\n")
441 (if (and (>= (current-column) col)
442 (looking-at "\\(\\sw\\|-\\)+$"))
443 (let ((sym (intern-soft (match-string 0))))
444 (if (fboundp sym)
445 (help-xref-button 0 'help-function sym))))
446 (forward-line))))))
447 (set-syntax-table stab))
448 ;; Delete extraneous newlines at the end of the docstring
449 (goto-char (point-max))
450 (while (and (not (bobp)) (bolp))
451 (delete-char -1))
452 (insert "\n")
453 ;; Make a back-reference in this buffer if appropriate.
454 (when help-xref-stack
455 (insert "\n")
456 (help-insert-xref-button help-back-label 'help-back
457 (current-buffer))
458 (insert "\n")))
459 ;; View mode steals RET from us.
460 (set (make-local-variable 'minor-mode-overriding-map-alist)
461 (list (cons 'view-mode help-xref-override-view-map)))
462 (set-buffer-modified-p old-modified))))
464 ;;;###autoload
465 (defun help-xref-button (match-number type &rest args)
466 "Make a hyperlink for cross-reference text previously matched.
467 MATCH-NUMBER is the subexpression of interest in the last matched
468 regexp. TYPE is the type of button to use. Any remaining arguments are
469 passed to the button's help-function when it is invoked.
470 See `help-make-xrefs'."
471 ;; Don't mung properties we've added specially in some instances.
472 (unless (button-at (match-beginning match-number))
473 (make-text-button (match-beginning match-number)
474 (match-end match-number)
475 'type type 'help-args args)))
477 ;;;###autoload
478 (defun help-insert-xref-button (string type &rest args)
479 "Insert STRING and make a hyperlink from cross-reference text on it.
480 TYPE is the type of button to use. Any remaining arguments are passed
481 to the button's help-function when it is invoked.
482 See `help-make-xrefs'."
483 (unless (button-at (point))
484 (insert-text-button string 'type type 'help-args args)))
486 ;;;###autoload
487 (defun help-xref-on-pp (from to)
488 "Add xrefs for symbols in `pp's output between FROM and TO."
489 (if (> (- to from) 5000) nil
490 (with-syntax-table emacs-lisp-mode-syntax-table
491 (save-excursion
492 (save-restriction
493 (narrow-to-region from to)
494 (goto-char (point-min))
495 (condition-case nil
496 (while (not (eobp))
497 (cond
498 ((looking-at "\"") (forward-sexp 1))
499 ((looking-at "#<") (search-forward ">" nil 'move))
500 ((looking-at "\\(\\(\\sw\\|\\s_\\)+\\)")
501 (let* ((sym (intern-soft (match-string 1)))
502 (type (cond ((fboundp sym) 'help-function)
503 ((or (memq sym '(t nil))
504 (keywordp sym))
505 nil)
506 ((and sym (boundp sym))
507 'help-variable))))
508 (when type (help-xref-button 1 type sym)))
509 (goto-char (match-end 1)))
510 (t (forward-char 1))))
511 (error nil)))))))
514 ;; Additional functions for (re-)creating types of help buffers.
515 (defun help-xref-interned (symbol)
516 "Follow a hyperlink which appeared to be an arbitrary interned SYMBOL.
517 Both variable, function and face documentation are extracted into a single
518 help buffer."
519 (with-current-buffer (help-buffer)
520 ;; Push the previous item on the stack before clobbering the output buffer.
521 (help-setup-xref nil nil)
522 (let ((facedoc (when (facep symbol)
523 ;; Don't record the current entry in the stack.
524 (setq help-xref-stack-item nil)
525 (describe-face symbol)))
526 (fdoc (when (fboundp symbol)
527 ;; Don't record the current entry in the stack.
528 (setq help-xref-stack-item nil)
529 (describe-function symbol)))
530 (sdoc (when (boundp symbol)
531 ;; Don't record the current entry in the stack.
532 (setq help-xref-stack-item nil)
533 (describe-variable symbol))))
534 (cond
535 (sdoc
536 ;; We now have a help buffer on the variable.
537 ;; Insert the function and face text before it.
538 (when (or fdoc facedoc)
539 (goto-char (point-min))
540 (let ((inhibit-read-only t))
541 (when fdoc
542 (insert fdoc "\n\n")
543 (when facedoc
544 (insert (make-string 30 ?-) "\n\n" (symbol-name symbol)
545 " is also a " "face." "\n\n")))
546 (when facedoc
547 (insert facedoc "\n\n"))
548 (insert (make-string 30 ?-) "\n\n" (symbol-name symbol)
549 " is also a " "variable." "\n\n"))
550 ;; Don't record the `describe-variable' item in the stack.
551 (setq help-xref-stack-item nil)
552 (help-setup-xref (list #'help-xref-interned symbol) nil)))
553 (fdoc
554 ;; We now have a help buffer on the function.
555 ;; Insert face text before it.
556 (when facedoc
557 (goto-char (point-max))
558 (let ((inhibit-read-only t))
559 (insert "\n\n" (make-string 30 ?-) "\n\n" (symbol-name symbol)
560 " is also a " "face." "\n\n" facedoc))
561 ;; Don't record the `describe-function' item in the stack.
562 (setq help-xref-stack-item nil)
563 (help-setup-xref (list #'help-xref-interned symbol) nil)))))))
566 ;; Navigation/hyperlinking with xrefs
568 (defun help-follow-mouse (click)
569 "Follow the cross-reference that you CLICK on."
570 (interactive "e")
571 (let* ((start (event-start click))
572 (window (car start))
573 (pos (car (cdr start))))
574 (with-current-buffer (window-buffer window)
575 (help-follow pos))))
577 (defun help-xref-go-back (buffer)
578 "From BUFFER, go back to previous help buffer text using `help-xref-stack'."
579 (let (item position method args)
580 (with-current-buffer buffer
581 (when help-xref-stack
582 (setq item (pop help-xref-stack)
583 ;; Clear the current item so that it won't get pushed
584 ;; by the function we're about to call. TODO: We could also
585 ;; push it onto a "forward" stack and add a `forw' button.
586 help-xref-stack-item nil
587 position (car item)
588 method (cadr item)
589 args (cddr item))))
590 (apply method args)
591 (with-current-buffer buffer
592 (if (get-buffer-window buffer)
593 (set-window-point (get-buffer-window buffer) position)
594 (goto-char position)))))
596 (defun help-go-back ()
597 "Go back to previous topic in this help buffer."
598 (interactive)
599 (if help-xref-stack
600 (help-xref-go-back (current-buffer))
601 (error "No previous help buffer")))
603 (defun help-do-xref (pos function args)
604 "Call the help cross-reference function FUNCTION with args ARGS.
605 Things are set up properly so that the resulting help-buffer has
606 a proper [back] button."
607 ;; There is a reference at point. Follow it.
608 (let ((help-xref-following t))
609 (apply function args)))
611 (defun help-follow (&optional pos)
612 "Follow cross-reference at POS, defaulting to point.
614 For the cross-reference format, see `help-make-xrefs'."
615 (interactive "d")
616 (unless pos
617 (setq pos (point)))
618 (unless (push-button pos)
619 ;; check if the symbol under point is a function or variable
620 (let ((sym
621 (intern
622 (save-excursion
623 (goto-char pos) (skip-syntax-backward "w_")
624 (buffer-substring (point)
625 (progn (skip-syntax-forward "w_")
626 (point)))))))
627 (when (or (boundp sym) (fboundp sym) (facep sym))
628 (help-do-xref pos #'help-xref-interned (list sym))))))
631 (provide 'help-mode)
633 ;;; arch-tag: 850954ae-3725-4cb4-8e91-0bf6d52d6b0b
634 ;;; help-mode.el ends here