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