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