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