]> code.delx.au - gnu-emacs/blob - lisp/help.el
(describe-function-1): Kluge around cases of functions fset to subrs
[gnu-emacs] / lisp / help.el
1 ;;; help.el --- help commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: help, internal
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; This code implements GNU Emacs' on-line help system, the one invoked by
28 ;; `M-x help-for-help'.
29
30 ;;; Code:
31
32 ;; Get the macro make-help-screen when this is compiled,
33 ;; or run interpreted, but not when the compiled code is loaded.
34 (eval-when-compile (require 'help-macro))
35 (eval-when-compile (require 'view))
36
37 (defvar help-map (make-sparse-keymap)
38 "Keymap for characters following the Help key.")
39
40 (defvar help-mode-map (make-sparse-keymap)
41 "Keymap for help mode.")
42
43 (define-key global-map (char-to-string help-char) 'help-command)
44 (define-key global-map [help] 'help-command)
45 (define-key global-map [f1] 'help-command)
46 (fset 'help-command help-map)
47
48 (define-key help-map (char-to-string help-char) 'help-for-help)
49 (define-key help-map [help] 'help-for-help)
50 (define-key help-map [f1] 'help-for-help)
51 (define-key help-map "?" 'help-for-help)
52
53 (define-key help-map "\C-c" 'describe-copying)
54 (define-key help-map "\C-d" 'describe-distribution)
55 (define-key help-map "\C-w" 'describe-no-warranty)
56 (define-key help-map "\C-p" 'describe-project)
57 (define-key help-map "a" 'apropos-command)
58
59 (define-key help-map "b" 'describe-bindings)
60
61 (define-key help-map "c" 'describe-key-briefly)
62 (define-key help-map "k" 'describe-key)
63
64 (define-key help-map "d" 'describe-function)
65 (define-key help-map "f" 'describe-function)
66
67 (define-key help-map "F" 'view-emacs-FAQ)
68
69 (define-key help-map "i" 'info)
70 (define-key help-map "4i" 'info-other-window)
71 (define-key help-map "\C-f" 'Info-goto-emacs-command-node)
72 (define-key help-map "\C-k" 'Info-goto-emacs-key-command-node)
73 (define-key help-map "\C-i" 'info-lookup-symbol)
74
75 (define-key help-map "l" 'view-lossage)
76
77 (define-key help-map "m" 'describe-mode)
78
79 (define-key help-map "\C-n" 'view-emacs-news)
80 (define-key help-map "n" 'view-emacs-news)
81
82 (define-key help-map "p" 'finder-by-keyword)
83 (autoload 'finder-by-keyword "finder"
84 "Find packages matching a given keyword." t)
85
86 (define-key help-map "P" 'view-emacs-problems)
87
88 (define-key help-map "s" 'describe-syntax)
89
90 (define-key help-map "t" 'help-with-tutorial)
91
92 (define-key help-map "w" 'where-is)
93
94 (define-key help-map "v" 'describe-variable)
95
96 (define-key help-map "q" 'help-quit)
97
98 (define-key help-mode-map [mouse-2] 'help-follow-mouse)
99 (define-key help-mode-map "\C-c\C-b" 'help-go-back)
100 (define-key help-mode-map "\C-c\C-c" 'help-follow)
101 (define-key help-mode-map "\t" 'help-next-ref)
102 (define-key help-mode-map [backtab] 'help-previous-ref)
103 (define-key help-mode-map [(shift tab)] 'help-previous-ref)
104 ;; Documentation only, since we use minor-mode-overriding-map-alist.
105 (define-key help-mode-map "\r" 'help-follow)
106
107 (defvar help-xref-stack nil
108 "A stack of ways by which to return to help buffers after following xrefs.
109 Used by `help-follow' and `help-xref-go-back'.
110 An element looks like (POSITION FUNCTION ARGS...), where POSITION is
111 `(POINT . BUFFER-NAME)'.
112 To use the element, do (apply FUNCTION ARGS) then goto the point in
113 the named buffer.")
114 (put 'help-xref-stack 'permanent-local t)
115
116 (defvar help-xref-stack-item nil
117 "An item for `help-follow' in this buffer to push onto `help-xref-stack'.
118 The format is (FUNCTION ARGS...).")
119 (put 'help-xref-stack-item 'permanent-local t)
120
121 (setq-default help-xref-stack nil help-xref-stack-item nil)
122
123 (defcustom help-mode-hook nil
124 "Hook run by `help-mode'."
125 :type 'hook
126 :group 'help)
127
128 (defun help-mode ()
129 "Major mode for viewing help text and navigating references in it.
130 Entry to this mode runs the normal hook `help-mode-hook'.
131 Commands:
132 \\{help-mode-map}"
133 (interactive)
134 (kill-all-local-variables)
135 (use-local-map help-mode-map)
136 (setq mode-name "Help")
137 (setq major-mode 'help-mode)
138 (make-local-variable 'font-lock-defaults)
139 (setq font-lock-defaults nil) ; font-lock would defeat xref
140 (view-mode)
141 (make-local-variable 'view-no-disable-on-exit)
142 (setq view-no-disable-on-exit t)
143 ;; `help-make-xrefs' would be run here if not invoked from
144 ;; `help-mode-maybe'.
145 (run-hooks 'help-mode-hook))
146
147 (defun help-mode-setup ()
148 (help-mode)
149 (setq buffer-read-only nil))
150
151 (add-hook 'temp-buffer-setup-hook 'help-mode-setup)
152
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))))
161
162 (add-hook 'temp-buffer-show-hook 'help-mode-finish)
163
164 (defun help-quit ()
165 "Just exit from the Help command's command loop."
166 (interactive)
167 nil)
168
169 (defun help-with-tutorial (&optional arg)
170 "Select the Emacs learn-by-doing tutorial.
171 If there is a tutorial version written in the language
172 of the selected language environment, that version is used.
173 If there's no tutorial in that language, `TUTORIAL' is selected.
174 With arg, you are asked to choose which language."
175 (interactive "P")
176 (let ((lang (if arg
177 (read-language-name 'tutorial "Language: " "English")
178 (if (get-language-info current-language-environment 'tutorial)
179 current-language-environment
180 "English")))
181 file filename)
182 (setq filename (get-language-info lang 'tutorial))
183 (setq file (expand-file-name (concat "~/" filename)))
184 (delete-other-windows)
185 (if (get-file-buffer file)
186 (switch-to-buffer (get-file-buffer file))
187 (switch-to-buffer (create-file-buffer file))
188 (setq buffer-file-name file)
189 (setq default-directory (expand-file-name "~/"))
190 (setq buffer-auto-save-file-name nil)
191 (insert-file-contents (expand-file-name filename data-directory))
192 (goto-char (point-min))
193 (search-forward "\n<<")
194 (beginning-of-line)
195 (delete-region (point) (progn (end-of-line) (point)))
196 (let ((n (- (window-height (selected-window))
197 (count-lines (point-min) (point))
198 6)))
199 (if (< n 12)
200 (newline n)
201 ;; Some people get confused by the large gap.
202 (newline (/ n 2))
203 (insert "[Middle of page left blank for didactic purposes. "
204 "Text continues below]")
205 (newline (- n (/ n 2)))))
206 (goto-char (point-min))
207 (set-buffer-modified-p nil))))
208
209 (defun mode-line-key-binding (key)
210 "Value is the binding of KEY in the mode line or nil if none."
211 (let (string-info defn)
212 (when (and (eq 'mode-line (aref key 0))
213 (consp (setq string-info (nth 4 (event-start (aref key 1))))))
214 (let* ((string (car string-info))
215 (pos (cdr string-info))
216 (local-map (and (> pos 0)
217 (< pos (length string))
218 (get-text-property pos 'local-map string))))
219 (setq defn (and local-map (lookup-key local-map key)))))
220 defn))
221
222 (defun describe-key-briefly (key &optional insert)
223 "Print the name of the function KEY invokes. KEY is a string.
224 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
225 (interactive "kDescribe key briefly: \nP")
226 (save-excursion
227 (let ((modifiers (event-modifiers (aref key 0)))
228 (standard-output (if insert (current-buffer) t))
229 window position)
230 ;; For a mouse button event, go to the button it applies to
231 ;; to get the right key bindings. And go to the right place
232 ;; in case the keymap depends on where you clicked.
233 (if (or (memq 'click modifiers) (memq 'down modifiers)
234 (memq 'drag modifiers))
235 (setq window (posn-window (event-start (aref key 0)))
236 position (posn-point (event-start (aref key 0)))))
237 (if (windowp window)
238 (progn
239 (set-buffer (window-buffer window))
240 (goto-char position)))
241 ;; Ok, now look up the key and name the command.
242 (let ((defn (or (mode-line-key-binding key)
243 (key-binding key)))
244 (key-desc (key-description key)))
245 (if (or (null defn) (integerp defn))
246 (princ (format "%s is undefined" key-desc))
247 (princ (format (if insert
248 "`%s' (`%s')"
249 (if (windowp window)
250 "%s at that spot runs the command %s"
251 "%s runs the command %s"))
252 key-desc
253 (if (symbolp defn) defn (prin1-to-string defn)))))))))
254
255 (defvar help-return-method nil
256 "What to do to \"exit\" the help buffer.
257 This is a list
258 (WINDOW . t) delete the selected window, go to WINDOW.
259 (WINDOW . quit-window) do quit-window, then select WINDOW.
260 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
261
262 (defun print-help-return-message (&optional function)
263 "Display or return message saying how to restore windows after help command.
264 Computes a message and applies the optional argument FUNCTION to it.
265 If FUNCTION is nil, applies `message' to it, thus printing it."
266 (and (not (get-buffer-window standard-output))
267 (let ((first-message
268 (cond ((special-display-p (buffer-name standard-output))
269 (setq help-return-method (cons (selected-window) t))
270 ;; If the help output buffer is a special display buffer,
271 ;; don't say anything about how to get rid of it.
272 ;; First of all, the user will do that with the window
273 ;; manager, not with Emacs.
274 ;; Secondly, the buffer has not been displayed yet,
275 ;; so we don't know whether its frame will be selected.
276 nil)
277 ((not (one-window-p t))
278 (setq help-return-method
279 (cons (selected-window) 'quit-window))
280 "Type \\[switch-to-buffer-other-window] RET to restore the other window.")
281 (pop-up-windows
282 (setq help-return-method (cons (selected-window) t))
283 "Type \\[delete-other-windows] to remove help window.")
284 (t
285 (setq help-return-method
286 (list (selected-window) (window-buffer)
287 (window-start) (window-point)))
288 "Type \\[switch-to-buffer] RET to remove help window."))))
289 (funcall (or function 'message)
290 (concat
291 (if first-message
292 (substitute-command-keys first-message))
293 (if first-message " ")
294 ;; If the help buffer will go in a separate frame,
295 ;; it's no use mentioning a command to scroll, so don't.
296 (if (special-display-p (buffer-name standard-output))
297 nil
298 (if (same-window-p (buffer-name standard-output))
299 ;; Say how to scroll this window.
300 (substitute-command-keys
301 "\\[scroll-up] to scroll the help.")
302 ;; Say how to scroll some other window.
303 (substitute-command-keys
304 "\\[scroll-other-window] to scroll the help."))))))))
305
306 (defun describe-key (key)
307 "Display documentation of the function invoked by KEY. KEY is a string."
308 (interactive "kDescribe key: ")
309 (save-excursion
310 (let ((modifiers (event-modifiers (aref key 0)))
311 window position)
312 ;; For a mouse button event, go to the button it applies to
313 ;; to get the right key bindings. And go to the right place
314 ;; in case the keymap depends on where you clicked.
315 (if (or (memq 'click modifiers) (memq 'down modifiers)
316 (memq 'drag modifiers))
317 (setq window (posn-window (event-start (aref key 0)))
318 position (posn-point (event-start (aref key 0)))))
319 (if (windowp window)
320 (progn
321 (set-buffer (window-buffer window))
322 (goto-char position)))
323 (let ((defn (or (mode-line-key-binding key) (key-binding key))))
324 (if (or (null defn) (integerp defn))
325 (message "%s is undefined" (key-description key))
326 (with-output-to-temp-buffer "*Help*"
327 (princ (key-description key))
328 (if (windowp window)
329 (princ " at that spot"))
330 (princ " runs the command ")
331 (prin1 defn)
332 (princ "\n which is ")
333 (describe-function-1 defn nil (interactive-p))
334 (print-help-return-message)))))))
335
336 (defun describe-mode ()
337 "Display documentation of current major mode and minor modes.
338 The major mode description comes first, followed by the minor modes,
339 each on a separate page.
340
341 For this to work correctly for a minor mode, the mode's indicator variable
342 \(listed in `minor-mode-alist') must also be a function whose documentation
343 describes the minor mode."
344 (interactive)
345 (with-output-to-temp-buffer "*Help*"
346 (when minor-mode-alist
347 (princ "The major mode is described first.
348 For minor modes, see following pages.\n\n"))
349 (princ mode-name)
350 (princ " mode:\n")
351 (princ (documentation major-mode))
352 (help-setup-xref (list #'help-xref-mode (current-buffer)) (interactive-p))
353 (let ((minor-modes minor-mode-alist))
354 (while minor-modes
355 (let* ((minor-mode (car (car minor-modes)))
356 (indicator (car (cdr (car minor-modes)))))
357 ;; Document a minor mode if it is listed in minor-mode-alist,
358 ;; bound locally in this buffer, non-nil, and has a function
359 ;; definition.
360 (if (and (symbol-value minor-mode)
361 (fboundp minor-mode))
362 (let ((pretty-minor-mode minor-mode))
363 (if (string-match "-mode$" (symbol-name minor-mode))
364 (setq pretty-minor-mode
365 (capitalize
366 (substring (symbol-name minor-mode)
367 0 (match-beginning 0)))))
368 (while (and indicator (symbolp indicator)
369 (boundp indicator)
370 (not (eq indicator (symbol-value indicator))))
371 (setq indicator (symbol-value indicator)))
372 (princ "\n\f\n")
373 (princ (format "%s minor mode (%s):\n"
374 pretty-minor-mode
375 (if indicator
376 (format "indicator%s" indicator)
377 "no indicator")))
378 (princ (documentation minor-mode)))))
379 (setq minor-modes (cdr minor-modes))))
380 (print-help-return-message)))
381
382 ;; So keyboard macro definitions are documented correctly
383 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
384
385 (defun describe-distribution ()
386 "Display info on how to obtain the latest version of GNU Emacs."
387 (interactive)
388 (find-file-read-only
389 (expand-file-name "DISTRIB" data-directory)))
390
391 (defun describe-copying ()
392 "Display info on how you may redistribute copies of GNU Emacs."
393 (interactive)
394 (find-file-read-only
395 (expand-file-name "COPYING" data-directory))
396 (goto-char (point-min)))
397
398 (defun describe-project ()
399 "Display info on the GNU project."
400 (interactive)
401 (find-file-read-only
402 (expand-file-name "GNU" data-directory))
403 (goto-char (point-min)))
404
405 (defun describe-no-warranty ()
406 "Display info on all the kinds of warranty Emacs does NOT have."
407 (interactive)
408 (describe-copying)
409 (let (case-fold-search)
410 (search-forward "NO WARRANTY")
411 (recenter 0)))
412
413 (defun describe-prefix-bindings ()
414 "Describe the bindings of the prefix used to reach this command.
415 The prefix described consists of all but the last event
416 of the key sequence that ran this command."
417 (interactive)
418 (let* ((key (this-command-keys)))
419 (describe-bindings
420 (if (stringp key)
421 (substring key 0 (1- (length key)))
422 (let ((prefix (make-vector (1- (length key)) nil))
423 (i 0))
424 (while (< i (length prefix))
425 (aset prefix i (aref key i))
426 (setq i (1+ i)))
427 prefix)))))
428 ;; Make C-h after a prefix, when not specifically bound,
429 ;; run describe-prefix-bindings.
430 (setq prefix-help-command 'describe-prefix-bindings)
431
432 (defun view-emacs-news (&optional arg)
433 "Display info on recent changes to Emacs.
434 With numeric argument display information on correspondingly older changes."
435 (interactive "P")
436 (let* ((arg (if arg (prefix-numeric-value arg) 0)))
437 (find-file-read-only
438 (expand-file-name (concat (make-string arg ?O) "NEWS")
439 data-directory))))
440
441 (defun view-emacs-FAQ ()
442 "Display the Emacs Frequently Asked Questions (FAQ) file."
443 (interactive)
444 ;;; (find-file-read-only (expand-file-name "FAQ" data-directory))
445 (info "(efaq)"))
446
447 (defun view-emacs-problems ()
448 "Display info on known problems with Emacs and possible workarounds."
449 (interactive)
450 (view-file (expand-file-name "PROBLEMS" data-directory)))
451
452 (defun view-lossage ()
453 "Display last 100 input keystrokes."
454 (interactive)
455 (with-output-to-temp-buffer "*Help*"
456 (princ (mapconcat (function (lambda (key)
457 (if (or (integerp key)
458 (symbolp key)
459 (listp key))
460 (single-key-description key)
461 (prin1-to-string key nil))))
462 (recent-keys)
463 " "))
464 (save-excursion
465 (set-buffer standard-output)
466 (goto-char (point-min))
467 (while (progn (move-to-column 50) (not (eobp)))
468 (search-forward " " nil t)
469 (insert "\n"))
470 (setq help-xref-stack nil
471 help-xref-stack-item nil))
472 (print-help-return-message)))
473
474 (defalias 'help 'help-for-help)
475 (make-help-screen help-for-help
476 "a b c C f F C-f i I k C-k l L m n p s t v w C-c C-d C-n C-p C-w; ? for help:"
477 "You have typed %THIS-KEY%, the help character. Type a Help option:
478 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
479
480 a command-apropos. Give a substring, and see a list of commands
481 (functions interactively callable) that contain
482 that substring. See also the apropos command.
483 b describe-bindings. Display table of all key bindings.
484 c describe-key-briefly. Type a command key sequence;
485 it prints the function name that sequence runs.
486 C describe-coding-system. This describes either a specific coding system
487 (if you type its name) or the coding systems currently in use
488 (if you type just RET).
489 f describe-function. Type a function name and get documentation of it.
490 C-f Info-goto-emacs-command-node. Type a function name;
491 it takes you to the Info node for that command.
492 i info. The info documentation reader.
493 I describe-input-method. Describe a specific input method (if you type
494 its name) or the current input method (if you type just RET).
495 C-i info-lookup-symbol. Display the definition of a specific symbol
496 as found in the manual for the language this buffer is written in.
497 k describe-key. Type a command key sequence;
498 it displays the full documentation.
499 C-k Info-goto-emacs-key-command-node. Type a command key sequence;
500 it takes you to the Info node for the command bound to that key.
501 l view-lossage. Show last 100 characters you typed.
502 L describe-language-environment. This describes either the a
503 specific language environment (if you type its name)
504 or the current language environment (if you type just RET).
505 m describe-mode. Print documentation of current minor modes,
506 and the current major mode, including their special commands.
507 n view-emacs-news. Display news of recent Emacs changes.
508 p finder-by-keyword. Find packages matching a given topic keyword.
509 s describe-syntax. Display contents of syntax table, plus explanations
510 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
511 v describe-variable. Type name of a variable;
512 it displays the variable's documentation and value.
513 w where-is. Type command name; it prints which keystrokes
514 invoke that command.
515
516 F Display the frequently asked questions file.
517 h Display the HELLO file which illustrates various scripts.
518 C-c Display Emacs copying permission (General Public License).
519 C-d Display Emacs ordering information.
520 C-n Display news of recent Emacs changes.
521 C-p Display information about the GNU project.
522 C-w Display information on absence of warranty for GNU Emacs."
523 help-map)
524
525 (defun function-called-at-point ()
526 "Return a function around point or else called by the list containing point.
527 If that doesn't give a function, return nil."
528 (let ((stab (syntax-table)))
529 (set-syntax-table emacs-lisp-mode-syntax-table)
530 (unwind-protect
531 (or (condition-case ()
532 (save-excursion
533 (or (not (zerop (skip-syntax-backward "_w")))
534 (eq (char-syntax (following-char)) ?w)
535 (eq (char-syntax (following-char)) ?_)
536 (forward-sexp -1))
537 (skip-chars-forward "'")
538 (let ((obj (read (current-buffer))))
539 (and (symbolp obj) (fboundp obj) obj)))
540 (error nil))
541 (condition-case ()
542 (save-excursion
543 (save-restriction
544 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
545 ;; Move up to surrounding paren, then after the open.
546 (backward-up-list 1)
547 (forward-char 1)
548 ;; If there is space here, this is probably something
549 ;; other than a real Lisp function call, so ignore it.
550 (if (looking-at "[ \t]")
551 (error "Probably not a Lisp function call"))
552 (let (obj)
553 (setq obj (read (current-buffer)))
554 (and (symbolp obj) (fboundp obj) obj))))
555 (error nil)))
556 (set-syntax-table stab))))
557
558 (defvar symbol-file-load-history-loaded nil
559 "Non-nil means we have loaded the file `fns-VERSION.el' in `exec-directory'.
560 That file records the part of `load-history' for preloaded files,
561 which is cleared out before dumping to make Emacs smaller.")
562
563 (defun symbol-file (function)
564 "Return the input source from which FUNCTION was loaded.
565 The value is normally a string that was passed to `load':
566 either an absolute file name, or a library name
567 \(with no directory name and no `.el' or `.elc' at the end).
568 It can also be nil, if the definition is not associated with any file."
569 (unless symbol-file-load-history-loaded
570 (load (expand-file-name
571 ;; fns-XX.YY.ZZ.el does not work on DOS filesystem.
572 (if (eq system-type 'ms-dos)
573 "fns.el"
574 (format "fns-%s.el" emacs-version))
575 exec-directory)
576 ;; The file name fns-%s.el already has a .el extension.
577 nil nil t)
578 (setq symbol-file-load-history-loaded t))
579 (let ((files load-history)
580 file functions)
581 (while files
582 (if (memq function (cdr (car files)))
583 (setq file (car (car files)) files nil))
584 (setq files (cdr files)))
585 file))
586
587 (defun describe-function (function)
588 "Display the full documentation of FUNCTION (a symbol)."
589 (interactive
590 (let ((fn (function-called-at-point))
591 (enable-recursive-minibuffers t)
592 val)
593 (setq val (completing-read (if fn
594 (format "Describe function (default %s): " fn)
595 "Describe function: ")
596 obarray 'fboundp t nil nil (symbol-name fn)))
597 (list (if (equal val "")
598 fn (intern val)))))
599 (if function
600 (with-output-to-temp-buffer "*Help*"
601 (prin1 function)
602 ;; Use " is " instead of a colon so that
603 ;; it is easier to get out the function name using forward-sexp.
604 (princ " is ")
605 (describe-function-1 function nil (interactive-p))
606 (print-help-return-message)
607 (save-excursion
608 (set-buffer standard-output)
609 ;; Return the text we displayed.
610 (buffer-string)))
611 (message "You didn't specify a function")))
612
613 (defun describe-function-1 (function parens interactive-p)
614 (let* ((def (if (symbolp function)
615 (symbol-function function)
616 function))
617 file-name string need-close
618 (beg (if (commandp def) "an interactive " "a ")))
619 (setq string
620 (cond ((or (stringp def)
621 (vectorp def))
622 "a keyboard macro")
623 ((subrp def)
624 (if (eq 'unevalled (cdr (subr-arity def)))
625 (concat beg "special form")
626 (concat beg "built-in function")))
627 ((byte-code-function-p def)
628 (concat beg "compiled Lisp function"))
629 ((symbolp def)
630 (while (symbolp (symbol-function def))
631 (setq def (symbol-function def)))
632 (format "an alias for `%s'" def))
633 ((eq (car-safe def) 'lambda)
634 (concat beg "Lisp function"))
635 ((eq (car-safe def) 'macro)
636 "a Lisp macro")
637 ((eq (car-safe def) 'mocklisp)
638 "a mocklisp function")
639 ((eq (car-safe def) 'autoload)
640 (setq file-name (nth 1 def))
641 (format "%s autoloaded %s"
642 (if (commandp def) "an interactive" "an")
643 (if (eq (nth 4 def) 'keymap) "keymap"
644 (if (nth 4 def) "Lisp macro" "Lisp function"))
645 ))
646 ;; perhaps use keymapp here instead
647 ((eq (car-safe def) 'keymap)
648 (let ((is-full nil)
649 (elts (cdr-safe def)))
650 (while elts
651 (if (char-table-p (car-safe elts))
652 (setq is-full t
653 elts nil))
654 (setq elts (cdr-safe elts)))
655 (if is-full
656 "a full keymap"
657 "a sparse keymap")))
658 (t "")))
659 (when (and parens (not (equal string "")))
660 (setq need-close t)
661 (princ "("))
662 (princ string)
663 (with-current-buffer "*Help*"
664 (save-excursion
665 (save-match-data
666 (if (re-search-backward "alias for `\\([^`']+\\)'" nil t)
667 (help-xref-button 1 #'describe-function def
668 "mouse-2, RET: describe this function")))))
669 (or file-name
670 (setq file-name (symbol-file function)))
671 (if file-name
672 (progn
673 (princ " in `")
674 ;; We used to add .el to the file name,
675 ;; but that's completely wrong when the user used load-file.
676 (princ file-name)
677 (princ "'")
678 ;; Make a hyperlink to the library.
679 (with-current-buffer "*Help*"
680 (save-excursion
681 (re-search-backward "`\\([^`']+\\)'" nil t)
682 (help-xref-button 1 #'(lambda (arg)
683 (let ((location
684 (find-function-noselect arg)))
685 (pop-to-buffer (car location))
686 (goto-char (cdr location))))
687 function
688 "mouse-2, RET: find function's definition")))))
689 (if need-close (princ ")"))
690 (princ ".")
691 (terpri)
692 ;; Handle symbols aliased to other symbols.
693 (setq def (indirect-function def))
694 ;; If definition is a macro, find the function inside it.
695 (if (eq (car-safe def) 'macro)
696 (setq def (cdr def)))
697 (let ((arglist (cond ((byte-code-function-p def)
698 (car (append def nil)))
699 ((eq (car-safe def) 'lambda)
700 (nth 1 def))
701 ((and (eq (car-safe def) 'autoload)
702 (not (eq (nth 4 def) 'keymap)))
703 (concat "[Arg list not available until "
704 "function definition is loaded.]"))
705 (t t))))
706 (cond ((listp arglist)
707 (princ (cons (if (symbolp function) function "anonymous")
708 (mapcar (lambda (arg)
709 (if (memq arg '(&optional &rest))
710 arg
711 (intern (upcase (symbol-name arg)))))
712 arglist)))
713 (terpri))
714 ((stringp arglist)
715 (princ arglist)
716 (terpri))))
717 (let ((doc (documentation function)))
718 (if doc
719 (progn (terpri)
720 (princ doc)
721 (if (subrp (symbol-function function))
722 (with-current-buffer standard-output
723 (beginning-of-line)
724 ;; Builtins get the calling sequence at the end of
725 ;; the doc string. Move it to the same place as
726 ;; for other functions.
727
728 ;; In cases where `function' has been fset to a
729 ;; subr we can't search for function's name in
730 ;; the doc string. Kluge round that using the
731 ;; printed representation. The arg list then
732 ;; shows the wrong function name, but that
733 ;; might be a useful hint.
734 (let* ((rep (prin1-to-string def))
735 (name (progn
736 (string-match " \\([^ ]+\\)>$" rep)
737 (match-string 1 rep))))
738 (if (looking-at (format "(%s[ )]" name))
739 (let ((start (point-marker)))
740 (goto-char (point-min))
741 (forward-paragraph)
742 (insert-buffer-substring (current-buffer) start)
743 (insert ?\n)
744 (delete-region (1- start) (point-max)))
745 (goto-char (point-min))
746 (forward-paragraph)
747 (insert
748 "[Missing arglist. Please make a bug report.]\n")))
749 (goto-char (point-max))))
750 (help-setup-xref (list #'describe-function function)
751 interactive-p))
752 (princ "not documented")))))
753
754 (defun variable-at-point ()
755 "Return the bound variable symbol found around point.
756 Return 0 if there is no such symbol."
757 (condition-case ()
758 (let ((stab (syntax-table)))
759 (unwind-protect
760 (save-excursion
761 (set-syntax-table emacs-lisp-mode-syntax-table)
762 (or (not (zerop (skip-syntax-backward "_w")))
763 (eq (char-syntax (following-char)) ?w)
764 (eq (char-syntax (following-char)) ?_)
765 (forward-sexp -1))
766 (skip-chars-forward "'")
767 (let ((obj (read (current-buffer))))
768 (or (and (symbolp obj) (boundp obj) obj)
769 0)))
770 (set-syntax-table stab)))
771 (error 0)))
772
773 (defun help-xref-on-pp (from to)
774 "Add xrefs for symbols in `pp's output between FROM and TO."
775 (let ((ost (syntax-table)))
776 (unwind-protect
777 (save-excursion
778 (save-restriction
779 (set-syntax-table emacs-lisp-mode-syntax-table)
780 (narrow-to-region from to)
781 (goto-char (point-min))
782 (while (not (eobp))
783 (cond
784 ((looking-at "\"") (forward-sexp 1))
785 ((looking-at "#<") (search-forward ">" nil 'move))
786 ((looking-at "\\(\\(\\sw\\|\\s_\\)+\\)")
787 (let* ((sym (intern-soft
788 (buffer-substring (match-beginning 1)
789 (match-end 1))))
790 (fn (cond ((fboundp sym) #'describe-function)
791 ((or (memq sym '(t nil))
792 (keywordp sym))
793 nil)
794 ((and sym (boundp sym))
795 #'describe-variable))))
796 (when fn (help-xref-button 1 fn sym)))
797 (goto-char (match-end 1)))
798 (t (forward-char 1))))))
799 (set-syntax-table ost))))
800
801 (defun describe-variable (variable)
802 "Display the full documentation of VARIABLE (a symbol).
803 Returns the documentation as a string, also."
804 (interactive
805 (let ((v (variable-at-point))
806 (enable-recursive-minibuffers t)
807 val)
808 (setq val (completing-read (if (symbolp v)
809 (format
810 "Describe variable (default %s): " v)
811 "Describe variable: ")
812 obarray 'boundp t nil nil
813 (if (symbolp v) (symbol-name v))))
814 (list (if (equal val "")
815 v (intern val)))))
816 (if (symbolp variable)
817 (let (valvoid)
818 (with-output-to-temp-buffer "*Help*"
819 (prin1 variable)
820 (if (not (boundp variable))
821 (progn
822 (princ " is void")
823 (setq valvoid t))
824 (let ((val (symbol-value variable)))
825 (with-current-buffer standard-output
826 (princ "'s value is ")
827 (terpri)
828 (let ((from (point)))
829 (pp val)
830 (help-xref-on-pp from (point))))))
831 (terpri)
832 (if (local-variable-p variable)
833 (progn
834 (princ (format "Local in buffer %s; " (buffer-name)))
835 (if (not (default-boundp variable))
836 (princ "globally void")
837 (let ((val (default-value variable)))
838 (with-current-buffer standard-output
839 (princ "global value is ")
840 (terpri)
841 (let ((from (point)))
842 (pp val)
843 (help-xref-on-pp from (point))))))
844 (terpri)))
845 (terpri)
846 (save-current-buffer
847 (set-buffer standard-output)
848 (if (> (count-lines (point-min) (point-max)) 10)
849 (progn
850 (set-syntax-table emacs-lisp-mode-syntax-table)
851 (goto-char (point-min))
852 (if valvoid
853 (forward-line 1)
854 (forward-sexp 1)
855 (delete-region (point) (progn (end-of-line) (point)))
856 (insert "'s value is shown below.\n\n")
857 (save-excursion
858 (insert "\n\nValue:"))))))
859 (princ "Documentation:")
860 (terpri)
861 (let ((doc (documentation-property variable 'variable-documentation)))
862 (princ (or doc "not documented as a variable.")))
863 (help-setup-xref (list #'describe-variable variable) (interactive-p))
864
865 ;; Make a link to customize if this variable can be customized.
866 ;; Note, it is not reliable to test only for a custom-type property
867 ;; because those are only present after the var's definition
868 ;; has been loaded.
869 (if (or (get variable 'custom-type) ; after defcustom
870 (get variable 'custom-loads) ; from loaddefs.el
871 (get variable 'standard-value)) ; from cus-start.el
872 (let ((customize-label "customize"))
873 (terpri)
874 (terpri)
875 (princ (concat "You can " customize-label " this variable."))
876 (with-current-buffer "*Help*"
877 (save-excursion
878 (re-search-backward
879 (concat "\\(" customize-label "\\)") nil t)
880 (help-xref-button 1 (lambda (v)
881 (if help-xref-stack
882 (pop help-xref-stack))
883 (customize-variable v))
884 variable
885 "mouse-2, RET: customize variable")))))
886 ;; Make a hyperlink to the library if appropriate. (Don't
887 ;; change the format of the buffer's initial line in case
888 ;; anything expects the current format.)
889 (let ((file-name (symbol-file variable)))
890 (when file-name
891 (princ "\n\nDefined in `")
892 (princ file-name)
893 (princ "'.")
894 (with-current-buffer "*Help*"
895 (save-excursion
896 (re-search-backward "`\\([^`']+\\)'" nil t)
897 (help-xref-button
898 1 (lambda (arg)
899 (let ((location
900 (find-variable-noselect arg)))
901 (pop-to-buffer (car location))
902 (goto-char (cdr location))))
903 variable "mouse-2, RET: find variable's definition")))))
904
905 (print-help-return-message)
906 (save-excursion
907 (set-buffer standard-output)
908 ;; Return the text we displayed.
909 (buffer-string))))
910 (message "You did not specify a variable")))
911
912 (defun describe-bindings (&optional prefix buffer)
913 "Show a list of all defined keys, and their definitions.
914 We put that list in a buffer, and display the buffer.
915
916 The optional argument PREFIX, if non-nil, should be a key sequence;
917 then we display only bindings that start with that prefix.
918 The optional argument BUFFER specifies which buffer's bindings
919 to display (default, the current buffer)."
920 (interactive "P")
921 (or buffer (setq buffer (current-buffer)))
922 (with-current-buffer buffer
923 (describe-bindings-internal nil prefix))
924 (with-current-buffer "*Help*"
925 (help-setup-xref (list #'describe-bindings prefix buffer)
926 (interactive-p))))
927
928 (defun where-is (definition &optional insert)
929 "Print message listing key sequences that invoke the command DEFINITION.
930 Argument is a command definition, usually a symbol with a function definition.
931 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
932 (interactive
933 (let ((fn (function-called-at-point))
934 (enable-recursive-minibuffers t)
935 val)
936 (setq val (completing-read (if fn
937 (format "Where is command (default %s): " fn)
938 "Where is command: ")
939 obarray 'commandp t))
940 (list (if (equal val "")
941 fn (intern val))
942 current-prefix-arg)))
943 (let* ((keys (where-is-internal definition overriding-local-map nil nil))
944 (keys1 (mapconcat 'key-description keys ", "))
945 (standard-output (if insert (current-buffer) t)))
946 (if insert
947 (if (> (length keys1) 0)
948 (princ (format "%s (%s)" keys1 definition))
949 (princ (format "M-x %s RET" definition)))
950 (if (> (length keys1) 0)
951 (princ (format "%s is on %s" definition keys1))
952 (princ (format "%s is not on any key" definition)))))
953 nil)
954
955 (defun locate-library (library &optional nosuffix path interactive-call)
956 "Show the precise file name of Emacs library LIBRARY.
957 This command searches the directories in `load-path' like `M-x load-library'
958 to find the file that `M-x load-library RET LIBRARY RET' would load.
959 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
960 to the specified name LIBRARY.
961
962 If the optional third arg PATH is specified, that list of directories
963 is used instead of `load-path'.
964
965 When called from a program, the file name is normaly returned as a
966 string. When run interactively, the argument INTERACTIVE-CALL is t,
967 and the file name is displayed in the echo area."
968 (interactive (list (read-string "Locate library: ")
969 nil nil
970 t))
971 (let (result)
972 (catch 'answer
973 (mapc
974 (lambda (dir)
975 (mapc
976 (lambda (suf)
977 (let ((try (expand-file-name (concat library suf) dir)))
978 (and (file-readable-p try)
979 (null (file-directory-p try))
980 (progn
981 (setq result try)
982 (throw 'answer try)))))
983 (if nosuffix
984 '("")
985 '(".elc" ".el" "")
986 ;;; load doesn't handle this yet.
987 ;;; (let ((basic '(".elc" ".el" ""))
988 ;;; (compressed '(".Z" ".gz" "")))
989 ;;; ;; If autocompression mode is on,
990 ;;; ;; consider all combinations of library suffixes
991 ;;; ;; and compression suffixes.
992 ;;; (if (rassq 'jka-compr-handler file-name-handler-alist)
993 ;;; (apply 'nconc
994 ;;; (mapcar (lambda (compelt)
995 ;;; (mapcar (lambda (baselt)
996 ;;; (concat baselt compelt))
997 ;;; basic))
998 ;;; compressed))
999 ;;; basic))
1000 )))
1001 (or path load-path)))
1002 (and interactive-call
1003 (if result
1004 (message "Library is file %s" result)
1005 (message "No library %s in search path" library)))
1006 result))
1007
1008 \f
1009 ;;; Grokking cross-reference information in doc strings and
1010 ;;; hyperlinking it.
1011
1012 ;; This may have some scope for extension and the same or something
1013 ;; similar should be done for widget doc strings, which currently use
1014 ;; another mechanism.
1015
1016 (defcustom help-highlight-p t
1017 "*If non-nil, `help-make-xrefs' highlight cross-references.
1018 Under a window system it highlights them with face defined by
1019 `help-highlight-face'."
1020 :group 'help
1021 :version "20.3"
1022 :type 'boolean)
1023
1024 (defcustom help-highlight-face 'underline
1025 "Face used by `help-make-xrefs' to highlight cross-references.
1026 Must be previously-defined."
1027 :group 'help
1028 :version "20.3"
1029 :type 'face)
1030
1031 (defvar help-back-label (purecopy "[back]")
1032 "Label to use by `help-make-xrefs' for the go-back reference.")
1033
1034 (defconst help-xref-symbol-regexp
1035 (purecopy (concat "\\(\\<\\(\\(variable\\|option\\)\\|"
1036 "\\(function\\|command\\)\\|"
1037 "\\(face\\)\\|"
1038 "\\(symbol\\)\\)\\s-+\\)?"
1039 ;; Note starting with word-syntax character:
1040 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'"))
1041 "Regexp matching doc string references to symbols.
1042
1043 The words preceding the quoted symbol can be used in doc strings to
1044 distinguish references to variables, functions and symbols.")
1045
1046 (defconst help-xref-info-regexp
1047 (purecopy "\\<[Ii]nfo[ \t\n]+node[ \t\n]+`\\([^']+\\)'")
1048 "Regexp matching doc string references to an Info node.")
1049
1050 (defun help-setup-xref (item interactive-p)
1051 "Invoked from commands using the \"*Help*\" buffer to install some xref info.
1052
1053 ITEM is a (FUNCTION . ARGS) pair appropriate for recreating the help
1054 buffer after following a reference. INTERACTIVE-P is non-nil if the
1055 calling command was invoked interactively. In this case the stack of
1056 items for help buffer \"back\" buttons is cleared."
1057 (if interactive-p
1058 (setq help-xref-stack nil))
1059 (setq help-xref-stack-item item))
1060
1061 (defvar help-xref-following nil
1062 "Non-nil when following a help cross-reference.")
1063
1064 (defun help-make-xrefs (&optional buffer)
1065 "Parse and hyperlink documentation cross-references in the given BUFFER.
1066
1067 Find cross-reference information in a buffer and, if
1068 `help-highlight-p' is non-nil, highlight it with face defined by
1069 `help-highlight-face'; activate such cross references for selection
1070 with `help-follow'. Cross-references have the canonical form `...'
1071 and the type of reference may be disambiguated by the preceding
1072 word(s) used in `help-xref-symbol-regexp'.
1073
1074 A special reference `back' is made to return back through a stack of
1075 help buffers. Variable `help-back-label' specifies the text for
1076 that."
1077 (interactive "b")
1078 (save-excursion
1079 (set-buffer (or buffer (current-buffer)))
1080 (goto-char (point-min))
1081 ;; Skip the header-type info, though it might be useful to parse
1082 ;; it at some stage (e.g. "function in `library'").
1083 (forward-paragraph)
1084 (let ((old-modified (buffer-modified-p)))
1085 (let ((stab (syntax-table))
1086 (case-fold-search t)
1087 (inhibit-read-only t))
1088 (set-syntax-table emacs-lisp-mode-syntax-table)
1089 ;; The following should probably be abstracted out.
1090 (unwind-protect
1091 (progn
1092 ;; Info references
1093 (save-excursion
1094 (while (re-search-forward help-xref-info-regexp nil t)
1095 (let ((data (match-string 1)))
1096 (save-match-data
1097 (unless (string-match "^([^)]+)" data)
1098 (setq data (concat "(emacs)" data))))
1099 (help-xref-button 1 #'info data
1100 "mouse-2, RET: read this Info node"))))
1101 ;; Quoted symbols
1102 (save-excursion
1103 (while (re-search-forward help-xref-symbol-regexp nil t)
1104 (let* ((data (match-string 7))
1105 (sym (intern-soft data)))
1106 (if sym
1107 (cond
1108 ((match-string 3) ; `variable' &c
1109 (and (boundp sym) ; `variable' doesn't ensure
1110 ; it's actually bound
1111 (help-xref-button
1112 7 #'describe-variable sym
1113 "mouse-2, RET: describe this variable")))
1114 ((match-string 4) ; `function' &c
1115 (and (fboundp sym) ; similarly
1116 (help-xref-button
1117 7 #'describe-function sym
1118 "mouse-2, RET: describe this function")))
1119 ((match-string 5) ; `face'
1120 (and (facep sym)
1121 (help-xref-button 7 #'describe-face sym
1122 "mouse-2, RET: describe this face")))
1123 ((match-string 6)) ; nothing for symbol
1124 ((and (boundp sym) (fboundp sym))
1125 ;; We can't intuit whether to use the
1126 ;; variable or function doc -- supply both.
1127 (help-xref-button
1128 7 #'help-xref-interned sym
1129 "mouse-2, RET: describe this symbol"))
1130 ((boundp sym)
1131 (help-xref-button
1132 7 #'describe-variable sym
1133 "mouse-2, RET: describe this variable"))
1134 ((fboundp sym)
1135 (help-xref-button
1136 7 #'describe-function sym
1137 "mouse-2, RET: describe this function"))
1138 ((facep sym)
1139 (help-xref-button
1140 7 #'describe-face sym)))))))
1141 ;; An obvious case of a key substitution:
1142 (save-excursion
1143 (while (re-search-forward
1144 ;; Assume command name is only word characters
1145 ;; and dashes to get things like `use M-x foo.'.
1146 "\\<M-x\\s-+\\(\\sw\\(\\sw\\|-\\)+\\)" nil t)
1147 (let ((sym (intern-soft (match-string 1))))
1148 (if (fboundp sym)
1149 (help-xref-button
1150 1 #'describe-function sym
1151 "mouse-2, RET: describe this command")))))
1152 ;; Look for commands in whole keymap substitutions:
1153 (save-excursion
1154 ;; Make sure to find the first keymap.
1155 (goto-char (point-min))
1156 ;; Find a header and the column at which the command
1157 ;; name will be found.
1158 (while (re-search-forward "^key +binding\n\\(-+ +\\)-+\n\n"
1159 nil t)
1160 (let ((col (- (match-end 1) (match-beginning 1))))
1161 (while
1162 ;; Ignore single blank lines in table, but not
1163 ;; double ones, which should terminate it.
1164 (and (not (looking-at "\n\\s-*\n"))
1165 (progn
1166 (and (eolp) (forward-line))
1167 (end-of-line)
1168 (skip-chars-backward "^\t\n")
1169 (if (and (>= (current-column) col)
1170 (looking-at "\\(\\sw\\|-\\)+$"))
1171 (let ((sym (intern-soft (match-string 0))))
1172 (if (fboundp sym)
1173 (help-xref-button
1174 0 #'describe-function sym
1175 "mouse-2, RET: describe this function"))))
1176 (zerop (forward-line)))))))))
1177 (set-syntax-table stab))
1178 ;; Make a back-reference in this buffer if appropriate.
1179 (when (and help-xref-following help-xref-stack)
1180 (goto-char (point-max))
1181 (save-excursion
1182 (insert "\n\n" help-back-label))
1183 ;; Just to provide the match data:
1184 (looking-at (concat "\n\n\\(" (regexp-quote help-back-label) "\\)"))
1185 (help-xref-button 1 #'help-xref-go-back (current-buffer))))
1186 ;; View mode steals RET from us.
1187 (set (make-local-variable 'minor-mode-overriding-map-alist)
1188 (list (cons 'view-mode
1189 (let ((map (make-sparse-keymap)))
1190 (set-keymap-parent map view-mode-map)
1191 (define-key map "\r" 'help-follow)
1192 map))))
1193 (set-buffer-modified-p old-modified))))
1194
1195 (defun help-xref-button (match-number function data &optional help-echo)
1196 "Make a hyperlink for cross-reference text previously matched.
1197
1198 MATCH-NUMBER is the subexpression of interest in the last matched
1199 regexp. FUNCTION is a function to invoke when the button is
1200 activated, applied to DATA. DATA may be a single value or a list.
1201 See `help-make-xrefs'.
1202 If optional arg HELP-ECHO is supplied, it is used as a help string."
1203 ;; Don't mung properties we've added specially in some instances.
1204 (unless (get-text-property (match-beginning match-number) 'help-xref)
1205 (add-text-properties (match-beginning match-number)
1206 (match-end match-number)
1207 (list 'mouse-face 'highlight
1208 'help-xref (cons function
1209 (if (listp data)
1210 data
1211 (list data)))))
1212 (if help-echo
1213 (put-text-property (match-beginning match-number)
1214 (match-end match-number)
1215 'help-echo help-echo))
1216 (if help-highlight-p
1217 (put-text-property (match-beginning match-number)
1218 (match-end match-number)
1219 'face help-highlight-face))))
1220
1221 \f
1222 ;; Additional functions for (re-)creating types of help buffers.
1223 (defun help-xref-interned (symbol)
1224 "Follow a hyperlink which appeared to be an arbitrary interned SYMBOL.
1225
1226 Both variable and function documentation are extracted into a single
1227 help buffer."
1228 (let ((fdoc (when (fboundp symbol) (describe-function symbol)))
1229 (facedoc (when (facep symbol) (describe-face symbol))))
1230 (when (or (boundp symbol) (not fdoc))
1231 (describe-variable symbol)
1232 ;; We now have a help buffer on the variable. Insert the function
1233 ;; text before it.
1234 (when (or fdoc facedoc)
1235 (with-current-buffer "*Help*"
1236 (goto-char (point-min))
1237 (let ((inhibit-read-only t))
1238 (when fdoc
1239 (insert fdoc "\n\n"))
1240 (when facedoc
1241 (insert (make-string 30 ?-) "\n\n" (symbol-name symbol)
1242 " is also a " "face." "\n\n" facedoc "\n\n"))
1243 (insert (make-string 30 ?-) "\n\n" (symbol-name symbol)
1244 " is also a " "variable." "\n\n"))
1245 (help-setup-xref (list #'help-xref-interned symbol) nil))))))
1246
1247 (defun help-xref-mode (buffer)
1248 "Do a `describe-mode' for the specified BUFFER."
1249 (save-excursion
1250 (set-buffer buffer)
1251 (describe-mode)))
1252 \f
1253 ;;; Navigation/hyperlinking with xrefs
1254
1255 (defun help-follow-mouse (click)
1256 "Follow the cross-reference that you click on."
1257 (interactive "e")
1258 (let* ((start (event-start click))
1259 (window (car start))
1260 (pos (car (cdr start))))
1261 (with-current-buffer (window-buffer window)
1262 (help-follow pos))))
1263
1264 (defun help-xref-go-back (buffer)
1265 "From BUFFER, go back to previous help buffer text using `help-xref-stack'."
1266 (let (item position method args)
1267 (with-current-buffer buffer
1268 (when help-xref-stack
1269 (setq help-xref-stack (cdr help-xref-stack)) ; due to help-follow
1270 (setq item (pop help-xref-stack)
1271 position (car item)
1272 method (cadr item)
1273 args (cddr item))))
1274 (apply method args)
1275 ;; We assume that the buffer we just recreated has the saved name,
1276 ;; which might not always be true.
1277 (when (get-buffer (cdr position))
1278 (with-current-buffer (cdr position)
1279 (goto-char (car position))))))
1280
1281 (defun help-go-back ()
1282 "Invoke the [back] button (if any) in the Help mode buffer."
1283 (interactive)
1284 (help-follow (1- (point-max))))
1285
1286 (defun help-follow (&optional pos)
1287 "Follow cross-reference at POS, defaulting to point.
1288
1289 For the cross-reference format, see `help-make-xrefs'."
1290 (interactive "d")
1291 (unless pos
1292 (setq pos (point)))
1293 (let* ((help-data
1294 (or (and (not (= pos (point-max)))
1295 (get-text-property pos 'help-xref))
1296 (and (not (= pos (point-min)))
1297 (get-text-property (1- pos) 'help-xref))
1298 ;; check if the symbol under point is a function or variable
1299 (let ((sym
1300 (intern
1301 (save-excursion
1302 (goto-char pos) (skip-syntax-backward "w_")
1303 (buffer-substring (point)
1304 (progn (skip-syntax-forward "w_")
1305 (point)))))))
1306 (when (or (boundp sym) (fboundp sym))
1307 (list #'help-xref-interned sym)))))
1308 (method (car help-data))
1309 (args (cdr help-data)))
1310 (when help-data
1311 (setq help-xref-stack (cons (cons (cons pos (buffer-name))
1312 help-xref-stack-item)
1313 help-xref-stack))
1314 (setq help-xref-stack-item nil)
1315 ;; There is a reference at point. Follow it.
1316 (let ((help-xref-following t))
1317 (apply method args)))))
1318
1319 ;; For tabbing through buffer.
1320 (defun help-next-ref ()
1321 "Find the next help cross-reference in the buffer."
1322 (interactive)
1323 (let (pos)
1324 (while (not pos)
1325 (if (get-text-property (point) 'help-xref) ; move off reference
1326 (goto-char (or (next-single-property-change (point) 'help-xref)
1327 (point))))
1328 (cond ((setq pos (next-single-property-change (point) 'help-xref))
1329 (if pos (goto-char pos)))
1330 ((bobp)
1331 (message "No cross references in the buffer.")
1332 (setq pos t))
1333 (t ; be circular
1334 (goto-char (point-min)))))))
1335
1336 (defun help-previous-ref ()
1337 "Find the previous help cross-reference in the buffer."
1338 (interactive)
1339 (let (pos)
1340 (while (not pos)
1341 (if (get-text-property (point) 'help-xref) ; move off reference
1342 (goto-char (or (previous-single-property-change (point) 'help-xref)
1343 (point))))
1344 (cond ((setq pos (previous-single-property-change (point) 'help-xref))
1345 (if pos (goto-char pos)))
1346 ((bobp)
1347 (message "No cross references in the buffer.")
1348 (setq pos t))
1349 (t ; be circular
1350 (goto-char (point-max)))))))
1351
1352 \f
1353 ;;; Automatic resizing of temporary buffers.
1354
1355 (defcustom temp-buffer-resize-mode nil
1356 "Non-nil means resize windows displaying temporary buffers.
1357 This makes the window the right height for its contents, but never
1358 more than `temp-buffer-max-height' nor less than `window-min-height'.
1359 This applies to `help', `apropos' and `completion' buffers, and some others.
1360
1361 Setting this variable directly does not take effect;
1362 use either \\[customize] or the function `temp-buffer-resize-mode'."
1363 :get (lambda (symbol)
1364 (and (memq 'resize-temp-buffer-window temp-buffer-show-hook) t))
1365 :set (lambda (symbol value)
1366 (temp-buffer-resize-mode (if value 1 -1)))
1367 :initialize 'custom-initialize-default
1368 :type 'boolean
1369 :group 'help
1370 :version "20.4")
1371
1372 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
1373 "*Maximum height of a window displaying a temporary buffer.
1374 This is the maximum height (in text lines) which `resize-temp-buffer-window'
1375 will give to a window displaying a temporary buffer.
1376 It can also be a function which will be called with the object corresponding
1377 to the buffer to be displayed as argument and should return an integer
1378 positive number."
1379 :type '(choice integer function)
1380 :group 'help
1381 :version "20.4")
1382
1383 (defun temp-buffer-resize-mode (arg)
1384 "Toggle the mode which that makes windows smaller for temporary buffers.
1385 With prefix argument ARG, turn the resizing of windows displaying temporary
1386 buffers on if ARG is positive or off otherwise.
1387 See the documentation of the variable `temp-buffer-resize-mode' for
1388 more information."
1389 (interactive "P")
1390 (let ((turn-it-on
1391 (if (null arg)
1392 (not (memq 'resize-temp-buffer-window temp-buffer-show-hook))
1393 (> (prefix-numeric-value arg) 0))))
1394 (if turn-it-on
1395 (progn
1396 ;; `help-mode-maybe' may add a `back' button and thus increase the
1397 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
1398 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
1399 (setq temp-buffer-resize-mode t))
1400 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)
1401 (setq temp-buffer-resize-mode nil))))
1402
1403 (defun resize-temp-buffer-window ()
1404 "Resize the current window to fit its contents.
1405 Will not make it higher than `temp-buffer-max-height' nor smaller than
1406 `window-min-height'. Do nothing if it is the only window on its frame, if it
1407 is not as wide as the frame or if some of the window's contents are scrolled
1408 out of view."
1409 (unless (or (one-window-p 'nomini)
1410 (not (pos-visible-in-window-p (point-min)))
1411 (/= (frame-width) (window-width)))
1412 (let* ((max-height (if (functionp temp-buffer-max-height)
1413 (funcall temp-buffer-max-height (current-buffer))
1414 temp-buffer-max-height))
1415 (win-height (1- (window-height)))
1416 (min-height (1- window-min-height))
1417 (text-height (count-screen-lines))
1418 (new-height (max (min text-height max-height) min-height)))
1419 (enlarge-window (- new-height win-height)))))
1420
1421 ;; `help-manyarg-func-alist' is defined primitively (in doc.c).
1422 ;; New primitives with `MANY' or `UNEVALLED' arglists should be added
1423 ;; to this alist.
1424 ;; The parens and function name are redundant, but it's messy to add
1425 ;; them in `documentation'.
1426 (defconst help-manyarg-func-alist
1427 (purecopy
1428 '((list . "(list &rest OBJECTS)")
1429 (vector . "(vector &rest OBJECTS)")
1430 (make-byte-code . "(make-byte-code &rest ELEMENTS)")
1431 (call-process
1432 . "(call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)")
1433 (call-process-region
1434 . "(call-process-region START END PROGRAM &optional DELETE BUFFER DISPLAY &rest ARGS)")
1435 (string . "(string &rest CHARACTERS)")
1436 (+ . "(+ &rest NUMBERS-OR-MARKERS)")
1437 (- . "(- &optional NUMBER-OR-MARKER &rest MORE-NUMBERS-OR-MARKERS)")
1438 (* . "(* &rest NUMBERS-OR-MARKERS)")
1439 (/ . "(/ DIVIDEND DIVISOR &rest DIVISORS)")
1440 (max . "(max NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS)")
1441 (min . "(min NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS)")
1442 (logand . "(logand &rest INTS-OR-MARKERS)")
1443 (logior . "(logior &rest INTS-OR-MARKERS)")
1444 (logxor . "(logxor &rest INTS-OR-MARKERS)")
1445 (encode-time
1446 . "(encode-time SECOND MINUTE HOUR DAY MONTH YEAR &optional ZONE)")
1447 (insert . "(insert &rest ARGS)")
1448 (insert-before-markers . "(insert-before-markers &rest ARGS)")
1449 (message . "(message STRING &rest ARGUMENTS)")
1450 (message-box . "(message-box STRING &rest ARGUMENTS)")
1451 (message-or-box . "(message-or-box STRING &rest ARGUMENTS)")
1452 (propertize . "(propertize STRING &rest PROPERTIES)")
1453 (format . "(format STRING &rest OBJECTS)")
1454 (apply . "(apply FUNCTION &rest ARGUMENTS)")
1455 (run-hooks . "(run-hooks &rest HOOKS)")
1456 (run-hook-with-args . "(run-hook-with-args HOOK &rest ARGS)")
1457 (run-hook-with-args-until-failure
1458 . "(run-hook-with-args-until-failure HOOK &rest ARGS)")
1459 (run-hook-with-args-until-success
1460 . "(run-hook-with-args-until-success HOOK &rest ARGS)")
1461 (funcall . "(funcall FUNCTION &rest ARGUMENTS)")
1462 (append . "(append &rest SEQUENCES)")
1463 (concat . "(concat &rest SEQUENCES)")
1464 (vconcat . "(vconcat vconcat)")
1465 (nconc . "(nconc &rest LISTS)")
1466 (widget-apply . "(widget-apply WIDGET PROPERTY &rest ARGS)")
1467 (make-hash-table . "(make-hash-table &rest KEYWORD-ARGS)")
1468 (insert-string . "(insert-string &rest ARGS)")
1469 (start-process . "(start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS)")
1470 (setq-default . "(setq-default SYMBOL VALUE [SYMBOL VALUE...])")
1471 (save-excursion . "(save-excursion &rest BODY)")
1472 (save-current-buffer . "(save-current-buffer &rest BODY)")
1473 (save-restriction . "(save-restriction &rest BODY)")
1474 (or . "(or CONDITIONS ...)")
1475 (and . "(and CONDITIONS ...)")
1476 (if . "(if COND THEN ELSE...)")
1477 (cond . "(cond CLAUSES...)")
1478 (progn . "(progn BODY ...)")
1479 (prog1 . "(prog1 FIRST BODY...)")
1480 (prog2 . "(prog2 X Y BODY...)")
1481 (setq . "(setq SYM VAL SYM VAL ...)")
1482 (quote . "(quote ARG)")
1483 (function . "(function ARG)")
1484 (defun . "(defun NAME ARGLIST [DOCSTRING] BODY...)")
1485 (defmacro . "(defmacro NAME ARGLIST [DOCSTRING] BODY...)")
1486 (defvar . "(defvar SYMBOL [INITVALUE DOCSTRING])")
1487 (defconst . "(defconst SYMBOL INITVALUE [DOCSTRING])")
1488 (let* . "(let* VARLIST BODY...)")
1489 (let . "(let VARLIST BODY...)")
1490 (while . "(while TEST BODY...)")
1491 (catch . "(catch TAG BODY...)")
1492 (unwind-protect . "(unwind-protect BODYFORM UNWINDFORMS...)")
1493 (condition-case . "(condition-case VAR BODYFORM HANDLERS...)")
1494 (track-mouse . "(track-mouse BODY ...)")
1495 (ml-if . "(ml-if COND THEN ELSE...)")
1496 (ml-provide-prefix-argument . "(ml-provide-prefix-argument ARG1 ARG2)")
1497 (with-output-to-temp-buffer
1498 . "(with-output-to-temp-buffer BUFFNAME BODY ...)")
1499 (save-window-excursion . "(save-window-excursion BODY ...)"))))
1500
1501 ;;; help.el ends here