guile-elisp bootstrap (lisp)
[bpt/emacs.git] / lisp / help.el
1 ;;; help.el --- help commands for Emacs
2
3 ;; Copyright (C) 1985-1986, 1993-1994, 1998-2014 Free Software Foundation, Inc.
4
5 ;; Maintainer: emacs-devel@gnu.org
6 ;; Keywords: help, internal
7 ;; Package: emacs
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; This code implements GNU Emacs's on-line help system, the one invoked by
27 ;; `M-x help-for-help'.
28
29 ;;; Code:
30
31 ;; Get the macro make-help-screen when this is compiled,
32 ;; or run interpreted, but not when the compiled code is loaded.
33 (eval-when-compile (require 'help-macro))
34
35 ;; `help-window-point-marker' is a marker you can move to a valid
36 ;; position of the buffer shown in the help window in order to override
37 ;; the standard positioning mechanism (`point-min') chosen by
38 ;; `with-output-to-temp-buffer' and `with-temp-buffer-window'.
39 ;; `with-help-window' has this point nowhere before exiting. Currently
40 ;; used by `view-lossage' to assert that the last keystrokes are always
41 ;; visible.
42 (defvar help-window-point-marker (make-marker)
43 "Marker to override default `window-point' in help windows.")
44
45 (defvar help-map
46 (let ((map (make-sparse-keymap)))
47 (define-key map (char-to-string help-char) 'help-for-help)
48 (define-key map [help] 'help-for-help)
49 (define-key map [f1] 'help-for-help)
50 (define-key map "." 'display-local-help)
51 (define-key map "?" 'help-for-help)
52
53 (define-key map "\C-a" 'about-emacs)
54 (define-key map "\C-c" 'describe-copying)
55 (define-key map "\C-d" 'view-emacs-debugging)
56 (define-key map "\C-e" 'view-external-packages)
57 (define-key map "\C-f" 'view-emacs-FAQ)
58 (define-key map "\C-m" 'view-order-manuals)
59 (define-key map "\C-n" 'view-emacs-news)
60 (define-key map "\C-o" 'describe-distribution)
61 (define-key map "\C-p" 'view-emacs-problems)
62 (define-key map "\C-t" 'view-emacs-todo)
63 (define-key map "\C-w" 'describe-no-warranty)
64
65 ;; This does not fit the pattern, but it is natural given the C-\ command.
66 (define-key map "\C-\\" 'describe-input-method)
67
68 (define-key map "C" 'describe-coding-system)
69 (define-key map "F" 'Info-goto-emacs-command-node)
70 (define-key map "I" 'describe-input-method)
71 (define-key map "K" 'Info-goto-emacs-key-command-node)
72 (define-key map "L" 'describe-language-environment)
73 (define-key map "S" 'info-lookup-symbol)
74
75 (define-key map "a" 'apropos-command)
76 (define-key map "b" 'describe-bindings)
77 (define-key map "c" 'describe-key-briefly)
78 (define-key map "d" 'apropos-documentation)
79 (define-key map "e" 'view-echo-area-messages)
80 (define-key map "f" 'describe-function)
81 (define-key map "g" 'describe-gnu-project)
82 (define-key map "h" 'view-hello-file)
83
84 (define-key map "i" 'info)
85 (define-key map "4i" 'info-other-window)
86
87 (define-key map "k" 'describe-key)
88 (define-key map "l" 'view-lossage)
89 (define-key map "m" 'describe-mode)
90 (define-key map "n" 'view-emacs-news)
91 (define-key map "p" 'finder-by-keyword)
92 (define-key map "P" 'describe-package)
93 (define-key map "r" 'info-emacs-manual)
94 (define-key map "s" 'describe-syntax)
95 (define-key map "t" 'help-with-tutorial)
96 (define-key map "w" 'where-is)
97 (define-key map "v" 'describe-variable)
98 (define-key map "q" 'help-quit)
99 map)
100 "Keymap for characters following the Help key.")
101
102 (define-key global-map (char-to-string help-char) 'help-command)
103 (define-key global-map [help] 'help-command)
104 (define-key global-map [f1] 'help-command)
105 (fset 'help-command help-map)
106
107 ;; insert-button makes the action nil if it is not store somewhere
108 (defvar help-button-cache nil)
109
110 \f
111 ;; `with-help-window' is a wrapper for `with-temp-buffer-window'
112 ;; providing the following additional twists:
113
114 ;; (1) It puts the buffer in `help-mode' (via `help-mode-setup') and
115 ;; adds cross references (via `help-mode-finish').
116
117 ;; (2) It issues a message telling how to scroll and quit the help
118 ;; window (via `help-window-setup').
119
120 ;; (3) An option (customizable via `help-window-select') to select the
121 ;; help window automatically.
122
123 ;; (4) A marker (`help-window-point-marker') to move point in the help
124 ;; window to an arbitrary buffer position.
125 (defmacro with-help-window (buffer-name &rest body)
126 "Display buffer named BUFFER-NAME in a help window.
127 Evaluate the forms in BODY with standard output bound to a buffer
128 called BUFFER-NAME (creating it if it does not exist), put that
129 buffer in `help-mode', display the buffer in a window (see
130 `with-temp-buffer-window' for details) and issue a message how to
131 deal with that \"help\" window when it's no more needed. Select
132 the help window if the current value of the user option
133 `help-window-select' says so. Return last value in BODY."
134 (declare (indent 1) (debug t))
135 `(progn
136 ;; Make `help-window-point-marker' point nowhere. The only place
137 ;; where this should be set to a buffer position is within BODY.
138 (set-marker help-window-point-marker nil)
139 (let ((temp-buffer-window-setup-hook
140 (cons 'help-mode-setup temp-buffer-window-setup-hook))
141 (temp-buffer-window-show-hook
142 (cons 'help-mode-finish temp-buffer-window-show-hook)))
143 (with-temp-buffer-window
144 ,buffer-name nil 'help-window-setup (progn ,@body)))))
145
146 (defun help-quit ()
147 "Just exit from the Help command's command loop."
148 (interactive)
149 nil)
150
151 (defvar help-return-method nil
152 "What to do to \"exit\" the help buffer.
153 This is a list
154 (WINDOW . t) delete the selected window (and possibly its frame,
155 see `quit-window'), go to WINDOW.
156 (WINDOW . quit-window) do quit-window, then select WINDOW.
157 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
158
159 (define-obsolete-function-alias 'print-help-return-message 'help-print-return-message "23.2")
160 (defun help-print-return-message (&optional function)
161 "Display or return message saying how to restore windows after help command.
162 This function assumes that `standard-output' is the help buffer.
163 It computes a message, and applies the optional argument FUNCTION to it.
164 If FUNCTION is nil, it applies `message', thus displaying the message.
165 In addition, this function sets up `help-return-method', which see, that
166 specifies what to do when the user exits the help buffer.
167
168 Do not call this in the scope of `with-help-window'."
169 (and (not (get-buffer-window standard-output))
170 (let ((first-message
171 (cond ((or
172 pop-up-frames
173 (special-display-p (buffer-name standard-output)))
174 (setq help-return-method (cons (selected-window) t))
175 ;; If the help output buffer is a special display buffer,
176 ;; don't say anything about how to get rid of it.
177 ;; First of all, the user will do that with the window
178 ;; manager, not with Emacs.
179 ;; Secondly, the buffer has not been displayed yet,
180 ;; so we don't know whether its frame will be selected.
181 nil)
182 ((not (one-window-p t))
183 (setq help-return-method
184 (cons (selected-window) 'quit-window))
185 "Type \\[display-buffer] RET to restore the other window.")
186 (pop-up-windows
187 (setq help-return-method (cons (selected-window) t))
188 "Type \\[delete-other-windows] to remove help window.")
189 (t
190 (setq help-return-method
191 (list (selected-window) (window-buffer)
192 (window-start) (window-point)))
193 "Type \\[switch-to-buffer] RET to remove help window."))))
194 (funcall (or function 'message)
195 (concat
196 (if first-message
197 (substitute-command-keys first-message))
198 (if first-message " ")
199 ;; If the help buffer will go in a separate frame,
200 ;; it's no use mentioning a command to scroll, so don't.
201 (if (or pop-up-windows
202 (special-display-p (buffer-name standard-output)))
203 nil
204 (if (same-window-p (buffer-name standard-output))
205 ;; Say how to scroll this window.
206 (substitute-command-keys
207 "\\[scroll-up] to scroll the help.")
208 ;; Say how to scroll some other window.
209 (substitute-command-keys
210 "\\[scroll-other-window] to scroll the help."))))))))
211
212 ;; So keyboard macro definitions are documented correctly
213 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
214
215 (defalias 'help 'help-for-help-internal)
216 ;; find-function can find this.
217 (defalias 'help-for-help 'help-for-help-internal)
218 ;; It can't find this, but nobody will look.
219 (make-help-screen help-for-help-internal
220 (purecopy "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?")
221 ;; Don't purecopy this one, because it's not evaluated (it's
222 ;; directly used as a docstring in a function definition, so it'll
223 ;; be moved to the DOC file anyway: no need for purecopying it).
224 "You have typed %THIS-KEY%, the help character. Type a Help option:
225 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
226
227 a PATTERN Show commands whose name matches the PATTERN (a list of words
228 or a regexp). See also the `apropos' command.
229 b Display all key bindings.
230 c KEYS Display the command name run by the given key sequence.
231 C CODING Describe the given coding system, or RET for current ones.
232 d PATTERN Show a list of functions, variables, and other items whose
233 documentation matches the PATTERN (a list of words or a regexp).
234 e Go to the *Messages* buffer which logs echo-area messages.
235 f FUNCTION Display documentation for the given function.
236 F COMMAND Show the on-line manual's section that describes the command.
237 g Display information about the GNU project.
238 h Display the HELLO file which illustrates various scripts.
239 i Start the Info documentation reader: read on-line manuals.
240 I METHOD Describe a specific input method, or RET for current.
241 k KEYS Display the full documentation for the key sequence.
242 K KEYS Show the on-line manual's section for the command bound to KEYS.
243 l Show last 300 input keystrokes (lossage).
244 L LANG-ENV Describes a specific language environment, or RET for current.
245 m Display documentation of current minor modes and current major mode,
246 including their special commands.
247 n Display news of recent Emacs changes.
248 p TOPIC Find packages matching a given topic keyword.
249 P PACKAGE Describe the given Emacs Lisp package.
250 r Display the Emacs manual in Info mode.
251 s Display contents of current syntax table, plus explanations.
252 S SYMBOL Show the section for the given symbol in the on-line manual
253 for the programming language used in this buffer.
254 t Start the Emacs learn-by-doing tutorial.
255 v VARIABLE Display the given variable's documentation and value.
256 w COMMAND Display which keystrokes invoke the given command (where-is).
257 . Display any available local help at point in the echo area.
258
259 C-a Information about Emacs.
260 C-c Emacs copying permission (GNU General Public License).
261 C-d Instructions for debugging GNU Emacs.
262 C-e External packages and information about Emacs.
263 C-f Emacs FAQ.
264 C-m How to order printed Emacs manuals.
265 C-n News of recent Emacs changes.
266 C-o Emacs ordering and distribution information.
267 C-p Info about known Emacs problems.
268 C-t Emacs TODO list.
269 C-w Information on absence of warranty for GNU Emacs."
270 help-map)
271
272 \f
273
274 (defun function-called-at-point ()
275 "Return a function around point or else called by the list containing point.
276 If that doesn't give a function, return nil."
277 (with-syntax-table emacs-lisp-mode-syntax-table
278 (or (condition-case ()
279 (save-excursion
280 (or (not (zerop (skip-syntax-backward "_w")))
281 (eq (char-syntax (following-char)) ?w)
282 (eq (char-syntax (following-char)) ?_)
283 (forward-sexp -1))
284 (skip-chars-forward "'")
285 (let ((obj (read (current-buffer))))
286 (and (symbolp obj) (fboundp obj) obj)))
287 (error nil))
288 (condition-case ()
289 (save-excursion
290 (save-restriction
291 (narrow-to-region (max (point-min)
292 (- (point) 1000)) (point-max))
293 ;; Move up to surrounding paren, then after the open.
294 (backward-up-list 1)
295 (forward-char 1)
296 ;; If there is space here, this is probably something
297 ;; other than a real Lisp function call, so ignore it.
298 (if (looking-at "[ \t]")
299 (error "Probably not a Lisp function call"))
300 (let ((obj (read (current-buffer))))
301 (and (symbolp obj) (fboundp obj) obj))))
302 (error nil))
303 (let* ((str (find-tag-default))
304 (sym (if str (intern-soft str))))
305 (if (and sym (fboundp sym))
306 sym
307 (save-match-data
308 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
309 (setq sym (intern-soft (match-string 1 str)))
310 (and (fboundp sym) sym))))))))
311
312 \f
313 ;;; `User' help functions
314
315 (defun view-help-file (file &optional dir)
316 (view-file (expand-file-name file (or dir data-directory)))
317 (goto-address-mode 1)
318 (goto-char (point-min)))
319
320 (defun describe-distribution ()
321 "Display info on how to obtain the latest version of GNU Emacs."
322 (interactive)
323 (view-help-file "DISTRIB"))
324
325 (defun describe-copying ()
326 "Display info on how you may redistribute copies of GNU Emacs."
327 (interactive)
328 (view-help-file "COPYING"))
329
330 ;; Maybe this command should just be removed.
331 (defun describe-gnu-project ()
332 "Browse online information on the GNU project."
333 (interactive)
334 (browse-url "http://www.gnu.org/gnu/thegnuproject.html"))
335
336 (define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2")
337
338 (defun describe-no-warranty ()
339 "Display info on all the kinds of warranty Emacs does NOT have."
340 (interactive)
341 (describe-copying)
342 (let (case-fold-search)
343 (search-forward "Disclaimer of Warranty")
344 (forward-line 0)
345 (recenter 0)))
346
347 (defun describe-prefix-bindings ()
348 "Describe the bindings of the prefix used to reach this command.
349 The prefix described consists of all but the last event
350 of the key sequence that ran this command."
351 (interactive)
352 (let ((key (this-command-keys)))
353 (describe-bindings
354 (if (stringp key)
355 (substring key 0 (1- (length key)))
356 (let ((prefix (make-vector (1- (length key)) nil))
357 (i 0))
358 (while (< i (length prefix))
359 (aset prefix i (aref key i))
360 (setq i (1+ i)))
361 prefix)))))
362 ;; Make C-h after a prefix, when not specifically bound,
363 ;; run describe-prefix-bindings.
364 (setq prefix-help-command 'describe-prefix-bindings)
365
366 (defun view-emacs-news (&optional version)
367 "Display info on recent changes to Emacs.
368 With argument, display info only for the selected version."
369 (interactive "P")
370 (unless version
371 (setq version emacs-major-version))
372 (when (consp version)
373 (let* ((all-versions
374 (let (res)
375 (mapc
376 (lambda (file)
377 (with-temp-buffer
378 (insert-file-contents
379 (expand-file-name file data-directory))
380 (while (re-search-forward
381 (if (member file '("NEWS.18" "NEWS.1-17"))
382 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
383 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
384 (setq res (cons (match-string-no-properties 1) res)))))
385 (cons "NEWS"
386 (directory-files data-directory nil
387 "^NEWS\\.[0-9][-0-9]*$" nil)))
388 (sort (delete-dups res) (lambda (a b) (string< b a)))))
389 (current (car all-versions)))
390 (setq version (completing-read
391 (format "Read NEWS for the version (default %s): " current)
392 all-versions nil nil nil nil current))
393 (if (integerp (string-to-number version))
394 (setq version (string-to-number version))
395 (unless (or (member version all-versions)
396 (<= (string-to-number version) (string-to-number current)))
397 (error "No news about version %s" version)))))
398 (when (integerp version)
399 (cond ((<= version 12)
400 (setq version (format "1.%d" version)))
401 ((<= version 18)
402 (setq version (format "%d" version)))
403 ((> version emacs-major-version)
404 (error "No news about Emacs %d (yet)" version))))
405 (let* ((vn (if (stringp version)
406 (string-to-number version)
407 version))
408 (file (cond
409 ((>= vn emacs-major-version) "NEWS")
410 ((< vn 18) "NEWS.1-17")
411 (t (format "NEWS.%d" vn))))
412 res)
413 (view-file (expand-file-name file data-directory))
414 (widen)
415 (goto-char (point-min))
416 (when (stringp version)
417 (when (re-search-forward
418 (concat (if (< vn 19)
419 "Changes in Emacs[ \t]*"
420 "^\* [^0-9\n]*") version "$")
421 nil t)
422 (beginning-of-line)
423 (narrow-to-region
424 (point)
425 (save-excursion
426 (while (and (setq res
427 (re-search-forward
428 (if (< vn 19)
429 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
430 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
431 (equal (match-string-no-properties 1) version)))
432 (or res (goto-char (point-max)))
433 (beginning-of-line)
434 (point)))))))
435
436 (defun view-emacs-todo (&optional _arg)
437 "Display the Emacs TODO list."
438 (interactive "P")
439 (view-help-file "TODO"))
440
441 (define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2")
442
443
444 (defun view-echo-area-messages ()
445 "View the log of recent echo-area messages: the `*Messages*' buffer.
446 The number of messages retained in that buffer
447 is specified by the variable `message-log-max'."
448 (interactive)
449 (with-current-buffer (messages-buffer)
450 (goto-char (point-max))
451 (display-buffer (current-buffer))))
452
453 (defun view-order-manuals ()
454 "Display information on how to buy printed copies of Emacs manuals."
455 (interactive)
456 ;; (view-help-file "ORDERS")
457 (info "(emacs)Printed Books"))
458
459 (defun view-emacs-FAQ ()
460 "Display the Emacs Frequently Asked Questions (FAQ) file."
461 (interactive)
462 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
463 (info "(efaq)"))
464
465 (defun view-emacs-problems ()
466 "Display info on known problems with Emacs and possible workarounds."
467 (interactive)
468 (view-help-file "PROBLEMS"))
469
470 (defun view-emacs-debugging ()
471 "Display info on how to debug Emacs problems."
472 (interactive)
473 (view-help-file "DEBUG"))
474
475 ;; This used to visit MORE.STUFF; maybe it should just be removed.
476 (defun view-external-packages ()
477 "Display info on where to get more Emacs packages."
478 (interactive)
479 (info "(efaq)Packages that do not come with Emacs"))
480
481 (defun view-lossage ()
482 "Display last 300 input keystrokes.
483
484 To record all your input, use `open-dribble-file'."
485 (interactive)
486 (help-setup-xref (list #'view-lossage)
487 (called-interactively-p 'interactive))
488 (with-help-window (help-buffer)
489 (princ (mapconcat (lambda (key)
490 (if (or (integerp key) (symbolp key) (listp key))
491 (single-key-description key)
492 (prin1-to-string key nil)))
493 (recent-keys)
494 " "))
495 (with-current-buffer standard-output
496 (goto-char (point-min))
497 (while (progn (move-to-column 50) (not (eobp)))
498 (when (search-forward " " nil t)
499 (delete-char -1))
500 (insert "\n"))
501 ;; jidanni wants to see the last keystrokes immediately.
502 (set-marker help-window-point-marker (point)))))
503
504 \f
505 ;; Key bindings
506
507 (defun describe-bindings (&optional prefix buffer)
508 "Display a buffer showing a list of all defined keys, and their definitions.
509 The keys are displayed in order of precedence.
510
511 The optional argument PREFIX, if non-nil, should be a key sequence;
512 then we display only bindings that start with that prefix.
513 The optional argument BUFFER specifies which buffer's bindings
514 to display (default, the current buffer). BUFFER can be a buffer
515 or a buffer name."
516 (interactive)
517 (or buffer (setq buffer (current-buffer)))
518 (help-setup-xref (list #'describe-bindings prefix buffer)
519 (called-interactively-p 'interactive))
520 (with-help-window (help-buffer)
521 ;; Be aware that `describe-buffer-bindings' puts its output into
522 ;; the current buffer.
523 (with-current-buffer (help-buffer)
524 (describe-buffer-bindings buffer prefix))))
525
526 ;; This function used to be in keymap.c.
527 (defun describe-bindings-internal (&optional menus prefix)
528 "Show a list of all defined keys, and their definitions.
529 We put that list in a buffer, and display the buffer.
530
531 The optional argument MENUS, if non-nil, says to mention menu bindings.
532 \(Ordinarily these are omitted from the output.)
533 The optional argument PREFIX, if non-nil, should be a key sequence;
534 then we display only bindings that start with that prefix."
535 (declare (obsolete describe-buffer-bindings "24.4"))
536 (let ((buf (current-buffer)))
537 (with-help-window (help-buffer)
538 ;; Be aware that `describe-buffer-bindings' puts its output into
539 ;; the current buffer.
540 (with-current-buffer (help-buffer)
541 (describe-buffer-bindings buf prefix menus)))))
542
543 (defun where-is (definition &optional insert)
544 "Print message listing key sequences that invoke the command DEFINITION.
545 Argument is a command definition, usually a symbol with a function definition.
546 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
547 (interactive
548 (let ((fn (function-called-at-point))
549 (enable-recursive-minibuffers t)
550 val)
551 (setq val (completing-read
552 (if fn
553 (format "Where is command (default %s): " fn)
554 "Where is command: ")
555 obarray 'commandp t nil nil
556 (and fn (symbol-name fn))))
557 (list (unless (equal val "") (intern val))
558 current-prefix-arg)))
559 (unless definition (error "No command"))
560 (let ((func (indirect-function definition))
561 (defs nil)
562 (standard-output (if insert (current-buffer) standard-output)))
563 ;; In DEFS, find all symbols that are aliases for DEFINITION.
564 (mapatoms (lambda (symbol)
565 (and (fboundp symbol)
566 (not (eq symbol definition))
567 (eq func (condition-case ()
568 (indirect-function symbol)
569 (error symbol)))
570 (push symbol defs))))
571 ;; Look at all the symbols--first DEFINITION,
572 ;; then its aliases.
573 (dolist (symbol (cons definition defs))
574 (let* ((remapped (command-remapping symbol))
575 (keys (where-is-internal
576 symbol overriding-local-map nil nil remapped))
577 (keys (mapconcat 'key-description keys ", "))
578 string)
579 (setq string
580 (if insert
581 (if (> (length keys) 0)
582 (if remapped
583 (format "%s (%s) (remapped from %s)"
584 keys remapped symbol)
585 (format "%s (%s)" keys symbol))
586 (format "M-x %s RET" symbol))
587 (if (> (length keys) 0)
588 (if remapped
589 (format "%s is remapped to %s which is on %s"
590 symbol remapped keys)
591 (format "%s is on %s" symbol keys))
592 ;; If this is the command the user asked about,
593 ;; and it is not on any key, say so.
594 ;; For other symbols, its aliases, say nothing
595 ;; about them unless they are on keys.
596 (if (eq symbol definition)
597 (format "%s is not on any key" symbol)))))
598 (when string
599 (unless (eq symbol definition)
600 (princ ";\n its alias "))
601 (princ string)))))
602 nil)
603
604 (defun help-key-description (key untranslated)
605 (let ((string (key-description key)))
606 (if (or (not untranslated)
607 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
608 string
609 (let ((otherstring (key-description untranslated)))
610 (if (equal string otherstring)
611 string
612 (format "%s (translated from %s)" string otherstring))))))
613
614 (defun describe-key-briefly (&optional key insert untranslated)
615 "Print the name of the function KEY invokes. KEY is a string.
616 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
617 If non-nil, UNTRANSLATED is a vector of the untranslated events.
618 It can also be a number in which case the untranslated events from
619 the last key hit are used.
620
621 If KEY is a menu item or a tool-bar button that is disabled, this command
622 temporarily enables it to allow getting help on disabled items and buttons."
623 (interactive
624 (let ((enable-disabled-menus-and-buttons t)
625 (cursor-in-echo-area t)
626 saved-yank-menu)
627 (unwind-protect
628 (let (key)
629 ;; If yank-menu is empty, populate it temporarily, so that
630 ;; "Select and Paste" menu can generate a complete event.
631 (when (null (cdr yank-menu))
632 (setq saved-yank-menu (copy-sequence yank-menu))
633 (menu-bar-update-yank-menu "(any string)" nil))
634 (setq key (read-key-sequence "Describe key (or click or menu item): "))
635 ;; Clear the echo area message (Bug#7014).
636 (message nil)
637 ;; If KEY is a down-event, read and discard the
638 ;; corresponding up-event. Note that there are also
639 ;; down-events on scroll bars and mode lines: the actual
640 ;; event then is in the second element of the vector.
641 (and (vectorp key)
642 (let ((last-idx (1- (length key))))
643 (and (eventp (aref key last-idx))
644 (memq 'down (event-modifiers (aref key last-idx)))))
645 (read-event))
646 (list
647 key
648 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
649 1))
650 ;; Put yank-menu back as it was, if we changed it.
651 (when saved-yank-menu
652 (setq yank-menu (copy-sequence saved-yank-menu))
653 (fset 'yank-menu (cons 'keymap yank-menu))))))
654 (if (numberp untranslated)
655 (setq untranslated (this-single-command-raw-keys)))
656 (let* ((event (if (and (symbolp (aref key 0))
657 (> (length key) 1)
658 (consp (aref key 1)))
659 (aref key 1)
660 (aref key 0)))
661 (modifiers (event-modifiers event))
662 (standard-output (if insert (current-buffer) standard-output))
663 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
664 (memq 'drag modifiers)) " at that spot" ""))
665 (defn (key-binding key t))
666 key-desc)
667 ;; Handle the case where we faked an entry in "Select and Paste" menu.
668 (if (and (eq defn nil)
669 (stringp (aref key (1- (length key))))
670 (eq (key-binding (substring key 0 -1)) 'yank-menu))
671 (setq defn 'menu-bar-select-yank))
672 ;; Don't bother user with strings from (e.g.) the select-paste menu.
673 (if (stringp (aref key (1- (length key))))
674 (aset key (1- (length key)) "(any string)"))
675 (if (and (> (length untranslated) 0)
676 (stringp (aref untranslated (1- (length untranslated)))))
677 (aset untranslated (1- (length untranslated)) "(any string)"))
678 ;; Now describe the key, perhaps as changed.
679 (setq key-desc (help-key-description key untranslated))
680 (if (or (null defn) (integerp defn) (equal defn 'undefined))
681 (princ (format "%s%s is undefined" key-desc mouse-msg))
682 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
683
684 (defun help--key-binding-keymap (key &optional accept-default no-remap position)
685 "Return a keymap holding a binding for KEY within current keymaps.
686 The effect of the arguments KEY, ACCEPT-DEFAULT, NO-REMAP and
687 POSITION is as documented in the function `key-binding'."
688 (let* ((active-maps (current-active-maps t position))
689 map found)
690 ;; We loop over active maps like key-binding does.
691 (while (and
692 (not found)
693 (setq map (pop active-maps)))
694 (setq found (lookup-key map key accept-default))
695 (when (integerp found)
696 ;; The first `found' characters of KEY were found but not the
697 ;; whole sequence.
698 (setq found nil)))
699 (when found
700 (if (and (symbolp found)
701 (not no-remap)
702 (command-remapping found))
703 ;; The user might want to know in which map the binding is
704 ;; found, or in which map the remapping is found. The
705 ;; default is to show the latter.
706 (help--key-binding-keymap (vector 'remap found))
707 map))))
708
709 (defun help--binding-locus (key position)
710 "Describe in which keymap KEY is defined.
711 Return a symbol pointing to that keymap if one exists ; otherwise
712 return nil."
713 (let ((map (help--key-binding-keymap key t nil position)))
714 (when map
715 (catch 'found
716 (let ((advertised-syms (nconc
717 (list 'overriding-terminal-local-map
718 'overriding-local-map)
719 (delq nil
720 (mapcar
721 (lambda (mode-and-map)
722 (let ((mode (car mode-and-map)))
723 (when (symbol-value mode)
724 (intern-soft
725 (format "%s-map" mode)))))
726 minor-mode-map-alist))
727 (list 'global-map
728 (intern-soft (format "%s-map" major-mode)))))
729 found)
730 ;; Look into these advertised symbols first.
731 (dolist (sym advertised-syms)
732 (when (and
733 (boundp sym)
734 (eq map (symbol-value sym)))
735 (throw 'found sym)))
736 ;; Only look in other symbols otherwise.
737 (mapatoms
738 (lambda (x)
739 (when (and (boundp x)
740 ;; Avoid let-bound symbols.
741 (special-variable-p x)
742 (eq (symbol-value x) map))
743 (throw 'found x))))
744 nil)))))
745
746 (defun describe-key (&optional key untranslated up-event)
747 "Display documentation of the function invoked by KEY.
748 KEY can be any kind of a key sequence; it can include keyboard events,
749 mouse events, and/or menu events. When calling from a program,
750 pass KEY as a string or a vector.
751
752 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
753 It can also be a number, in which case the untranslated events from
754 the last key sequence entered are used.
755 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
756
757 If KEY is a menu item or a tool-bar button that is disabled, this command
758 temporarily enables it to allow getting help on disabled items and buttons."
759 (interactive
760 (let ((enable-disabled-menus-and-buttons t)
761 (cursor-in-echo-area t)
762 saved-yank-menu)
763 (unwind-protect
764 (let (key)
765 ;; If yank-menu is empty, populate it temporarily, so that
766 ;; "Select and Paste" menu can generate a complete event.
767 (when (null (cdr yank-menu))
768 (setq saved-yank-menu (copy-sequence yank-menu))
769 (menu-bar-update-yank-menu "(any string)" nil))
770 (setq key (read-key-sequence "Describe key (or click or menu item): "))
771 (list
772 key
773 (prefix-numeric-value current-prefix-arg)
774 ;; If KEY is a down-event, read and include the
775 ;; corresponding up-event. Note that there are also
776 ;; down-events on scroll bars and mode lines: the actual
777 ;; event then is in the second element of the vector.
778 (and (vectorp key)
779 (let ((last-idx (1- (length key))))
780 (and (eventp (aref key last-idx))
781 (memq 'down (event-modifiers (aref key last-idx)))))
782 (or (and (eventp (aref key 0))
783 (memq 'down (event-modifiers (aref key 0)))
784 ;; However, for the C-down-mouse-2 popup
785 ;; menu, there is no subsequent up-event. In
786 ;; this case, the up-event is the next
787 ;; element in the supplied vector.
788 (= (length key) 1))
789 (and (> (length key) 1)
790 (eventp (aref key 1))
791 (memq 'down (event-modifiers (aref key 1)))))
792 (read-event))))
793 ;; Put yank-menu back as it was, if we changed it.
794 (when saved-yank-menu
795 (setq yank-menu (copy-sequence saved-yank-menu))
796 (fset 'yank-menu (cons 'keymap yank-menu))))))
797 (if (numberp untranslated)
798 (setq untranslated (this-single-command-raw-keys)))
799 (let* ((event (aref key (if (and (symbolp (aref key 0))
800 (> (length key) 1)
801 (consp (aref key 1)))
802 1
803 0)))
804 (modifiers (event-modifiers event))
805 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
806 (memq 'drag modifiers)) " at that spot" ""))
807 (defn (key-binding key t))
808 key-locus key-locus-up key-locus-up-tricky
809 defn-up defn-up-tricky ev-type
810 mouse-1-remapped mouse-1-tricky)
811
812 ;; Handle the case where we faked an entry in "Select and Paste" menu.
813 (when (and (eq defn nil)
814 (stringp (aref key (1- (length key))))
815 (eq (key-binding (substring key 0 -1)) 'yank-menu))
816 (setq defn 'menu-bar-select-yank))
817 (if (or (null defn) (integerp defn) (equal defn 'undefined))
818 (message "%s%s is undefined"
819 (help-key-description key untranslated) mouse-msg)
820 (help-setup-xref (list #'describe-function defn)
821 (called-interactively-p 'interactive))
822 ;; Don't bother user with strings from (e.g.) the select-paste menu.
823 (when (stringp (aref key (1- (length key))))
824 (aset key (1- (length key)) "(any string)"))
825 (when (and untranslated
826 (stringp (aref untranslated (1- (length untranslated)))))
827 (aset untranslated (1- (length untranslated))
828 "(any string)"))
829 ;; Need to do this before erasing *Help* buffer in case event
830 ;; is a mouse click in an existing *Help* buffer.
831 (when up-event
832 (setq ev-type (event-basic-type up-event))
833 (let ((sequence (vector up-event)))
834 (when (and (eq ev-type 'mouse-1)
835 mouse-1-click-follows-link
836 (not (eq mouse-1-click-follows-link 'double))
837 (setq mouse-1-remapped
838 (mouse-on-link-p (event-start up-event))))
839 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
840 (> mouse-1-click-follows-link 0)))
841 (cond ((stringp mouse-1-remapped)
842 (setq sequence mouse-1-remapped))
843 ((vectorp mouse-1-remapped)
844 (setcar up-event (elt mouse-1-remapped 0)))
845 (t (setcar up-event 'mouse-2))))
846 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
847 (setq key-locus-up (help--binding-locus sequence (event-start up-event)))
848 (when mouse-1-tricky
849 (setq sequence (vector up-event))
850 (aset sequence 0 'mouse-1)
851 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event)))
852 (setq key-locus-up-tricky (help--binding-locus sequence (event-start up-event))))))
853 (setq key-locus (help--binding-locus key (event-start event)))
854 (with-help-window (help-buffer)
855 (princ (help-key-description key untranslated))
856 (princ (format "%s runs the command %S%s, which is "
857 mouse-msg defn (if key-locus
858 (format " (found in %s)" key-locus)
859 "")))
860 (describe-function-1 defn)
861 (when up-event
862 (unless (or (null defn-up)
863 (integerp defn-up)
864 (equal defn-up 'undefined))
865 (princ (format "
866
867 ----------------- up-event %s----------------
868
869 %s%s%s runs the command %S%s, which is "
870 (if mouse-1-tricky "(short click) " "")
871 (key-description (vector up-event))
872 mouse-msg
873 (if mouse-1-remapped
874 " is remapped to <mouse-2>, which" "")
875 defn-up (if key-locus-up
876 (format " (found in %s)" key-locus-up)
877 "")))
878 (describe-function-1 defn-up))
879 (unless (or (null defn-up-tricky)
880 (integerp defn-up-tricky)
881 (eq defn-up-tricky 'undefined))
882 (princ (format "
883
884 ----------------- up-event (long click) ----------------
885
886 Pressing <%S>%s for longer than %d milli-seconds
887 runs the command %S%s, which is "
888 ev-type mouse-msg
889 mouse-1-click-follows-link
890 defn-up-tricky (if key-locus-up-tricky
891 (format " (found in %s)" key-locus-up-tricky)
892 "")))
893 (describe-function-1 defn-up-tricky)))))))
894 \f
895 (defun describe-mode (&optional buffer)
896 "Display documentation of current major mode and minor modes.
897 A brief summary of the minor modes comes first, followed by the
898 major mode description. This is followed by detailed
899 descriptions of the minor modes, each on a separate page.
900
901 For this to work correctly for a minor mode, the mode's indicator
902 variable \(listed in `minor-mode-alist') must also be a function
903 whose documentation describes the minor mode.
904
905 If called from Lisp with a non-nil BUFFER argument, display
906 documentation for the major and minor modes of that buffer."
907 (interactive "@")
908 (unless buffer (setq buffer (current-buffer)))
909 (help-setup-xref (list #'describe-mode buffer)
910 (called-interactively-p 'interactive))
911 ;; For the sake of help-do-xref and help-xref-go-back,
912 ;; don't switch buffers before calling `help-buffer'.
913 (with-help-window (help-buffer)
914 (with-current-buffer buffer
915 (let (minor-modes)
916 ;; Older packages do not register in minor-mode-list but only in
917 ;; minor-mode-alist.
918 (dolist (x minor-mode-alist)
919 (setq x (car x))
920 (unless (memq x minor-mode-list)
921 (push x minor-mode-list)))
922 ;; Find enabled minor mode we will want to mention.
923 (dolist (mode minor-mode-list)
924 ;; Document a minor mode if it is listed in minor-mode-alist,
925 ;; non-nil, and has a function definition.
926 (let ((fmode (or (get mode :minor-mode-function) mode)))
927 (and (boundp mode) (symbol-value mode)
928 (fboundp fmode)
929 (let ((pretty-minor-mode
930 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
931 (symbol-name fmode))
932 (capitalize
933 (substring (symbol-name fmode)
934 0 (match-beginning 0)))
935 fmode)))
936 (push (list fmode pretty-minor-mode
937 (format-mode-line (assq mode minor-mode-alist)))
938 minor-modes)))))
939 (setq minor-modes
940 (sort minor-modes
941 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
942 (when minor-modes
943 (princ "Enabled minor modes:\n")
944 (make-local-variable 'help-button-cache)
945 (with-current-buffer standard-output
946 (dolist (mode minor-modes)
947 (let ((mode-function (nth 0 mode))
948 (pretty-minor-mode (nth 1 mode))
949 (indicator (nth 2 mode)))
950 (add-text-properties 0 (length pretty-minor-mode)
951 '(face bold) pretty-minor-mode)
952 (save-excursion
953 (goto-char (point-max))
954 (princ "\n\f\n")
955 (push (point-marker) help-button-cache)
956 ;; Document the minor modes fully.
957 (insert pretty-minor-mode)
958 (princ (format " minor mode (%s):\n"
959 (if (zerop (length indicator))
960 "no indicator"
961 (format "indicator%s"
962 indicator))))
963 (princ (documentation mode-function)))
964 (insert-button pretty-minor-mode
965 'action (car help-button-cache)
966 'follow-link t
967 'help-echo "mouse-2, RET: show full information")
968 (newline)))
969 (forward-line -1)
970 (fill-paragraph nil)
971 (forward-line 1))
972
973 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
974 ;; Document the major mode.
975 (let ((mode mode-name))
976 (with-current-buffer standard-output
977 (let ((start (point)))
978 (insert (format-mode-line mode nil nil buffer))
979 (add-text-properties start (point) '(face bold)))))
980 (princ " mode")
981 (let* ((mode major-mode)
982 (file-name (find-lisp-object-file-name mode nil)))
983 (when file-name
984 (princ (concat " defined in `" (file-name-nondirectory file-name) "'"))
985 ;; Make a hyperlink to the library.
986 (with-current-buffer standard-output
987 (save-excursion
988 (re-search-backward "`\\([^`']+\\)'" nil t)
989 (help-xref-button 1 'help-function-def mode file-name)))))
990 (princ ":\n")
991 (princ (documentation major-mode)))))
992 ;; For the sake of IELM and maybe others
993 nil)
994
995
996 (defun describe-minor-mode (minor-mode)
997 "Display documentation of a minor mode given as MINOR-MODE.
998 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
999 appeared on the mode-line."
1000 (interactive (list (completing-read
1001 "Minor mode: "
1002 (nconc
1003 (describe-minor-mode-completion-table-for-symbol)
1004 (describe-minor-mode-completion-table-for-indicator)
1005 ))))
1006 (if (symbolp minor-mode)
1007 (setq minor-mode (symbol-name minor-mode)))
1008 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
1009 (indicators (describe-minor-mode-completion-table-for-indicator)))
1010 (cond
1011 ((member minor-mode symbols)
1012 (describe-minor-mode-from-symbol (intern minor-mode)))
1013 ((member minor-mode indicators)
1014 (describe-minor-mode-from-indicator minor-mode))
1015 (t
1016 (error "No such minor mode: %s" minor-mode)))))
1017
1018 ;; symbol
1019 (defun describe-minor-mode-completion-table-for-symbol ()
1020 ;; In order to list up all minor modes, minor-mode-list
1021 ;; is used here instead of minor-mode-alist.
1022 (delq nil (mapcar 'symbol-name minor-mode-list)))
1023
1024 (defun describe-minor-mode-from-symbol (symbol)
1025 "Display documentation of a minor mode given as a symbol, SYMBOL"
1026 (interactive (list (intern (completing-read
1027 "Minor mode symbol: "
1028 (describe-minor-mode-completion-table-for-symbol)))))
1029 (if (fboundp symbol)
1030 (describe-function symbol)
1031 (describe-variable symbol)))
1032
1033 ;; indicator
1034 (defun describe-minor-mode-completion-table-for-indicator ()
1035 (delq nil
1036 (mapcar (lambda (x)
1037 (let ((i (format-mode-line x)))
1038 ;; remove first space if existed
1039 (cond
1040 ((= 0 (length i))
1041 nil)
1042 ((eq (aref i 0) ?\s)
1043 (substring i 1))
1044 (t
1045 i))))
1046 minor-mode-alist)))
1047
1048 (defun describe-minor-mode-from-indicator (indicator)
1049 "Display documentation of a minor mode specified by INDICATOR.
1050 If you call this function interactively, you can give indicator which
1051 is currently activated with completion."
1052 (interactive (list
1053 (completing-read
1054 "Minor mode indicator: "
1055 (describe-minor-mode-completion-table-for-indicator))))
1056 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
1057 (if minor-mode
1058 (describe-minor-mode-from-symbol minor-mode)
1059 (error "Cannot find minor mode for `%s'" indicator))))
1060
1061 (defun lookup-minor-mode-from-indicator (indicator)
1062 "Return a minor mode symbol from its indicator on the mode line."
1063 ;; remove first space if existed
1064 (if (and (< 0 (length indicator))
1065 (eq (aref indicator 0) ?\s))
1066 (setq indicator (substring indicator 1)))
1067 (let ((minor-modes minor-mode-alist)
1068 result)
1069 (while minor-modes
1070 (let* ((minor-mode (car (car minor-modes)))
1071 (anindicator (format-mode-line
1072 (car (cdr (car minor-modes))))))
1073 ;; remove first space if existed
1074 (if (and (stringp anindicator)
1075 (> (length anindicator) 0)
1076 (eq (aref anindicator 0) ?\s))
1077 (setq anindicator (substring anindicator 1)))
1078 (if (equal indicator anindicator)
1079 (setq result minor-mode
1080 minor-modes nil)
1081 (setq minor-modes (cdr minor-modes)))))
1082 result))
1083 \f
1084 ;;; Automatic resizing of temporary buffers.
1085 (defcustom temp-buffer-max-height
1086 (lambda (buffer)
1087 (if (eq (selected-window) (frame-root-window))
1088 (/ (x-display-pixel-height) (frame-char-height) 2)
1089 (/ (- (frame-height) 2) 2)))
1090 "Maximum height of a window displaying a temporary buffer.
1091 This is effective only when Temp Buffer Resize mode is enabled.
1092 The value is the maximum height (in lines) which
1093 `resize-temp-buffer-window' will give to a window displaying a
1094 temporary buffer. It can also be a function to be called to
1095 choose the height for such a buffer. It gets one argument, the
1096 buffer, and should return a positive integer. At the time the
1097 function is called, the window to be resized is selected."
1098 :type '(choice integer function)
1099 :group 'help
1100 :version "24.3")
1101
1102 (defcustom temp-buffer-max-width
1103 (lambda (buffer)
1104 (if (eq (selected-window) (frame-root-window))
1105 (/ (x-display-pixel-width) (frame-char-width) 2)
1106 (/ (- (frame-width) 2) 2)))
1107 "Maximum width of a window displaying a temporary buffer.
1108 This is effective only when Temp Buffer Resize mode is enabled.
1109 The value is the maximum width (in columns) which
1110 `resize-temp-buffer-window' will give to a window displaying a
1111 temporary buffer. It can also be a function to be called to
1112 choose the width for such a buffer. It gets one argument, the
1113 buffer, and should return a positive integer. At the time the
1114 function is called, the window to be resized is selected."
1115 :type '(choice integer function)
1116 :group 'help
1117 :version "24.4")
1118
1119 (define-minor-mode temp-buffer-resize-mode
1120 "Toggle auto-resizing temporary buffer windows (Temp Buffer Resize Mode).
1121 With a prefix argument ARG, enable Temp Buffer Resize mode if ARG
1122 is positive, and disable it otherwise. If called from Lisp,
1123 enable the mode if ARG is omitted or nil.
1124
1125 When Temp Buffer Resize mode is enabled, the windows in which we
1126 show a temporary buffer are automatically resized in height to
1127 fit the buffer's contents, but never more than
1128 `temp-buffer-max-height' nor less than `window-min-height'.
1129
1130 A window is resized only if it has been specially created for the
1131 buffer. Windows that have shown another buffer before are not
1132 resized. A frame is resized only if `fit-frame-to-buffer' is
1133 non-nil.
1134
1135 This mode is used by `help', `apropos' and `completion' buffers,
1136 and some others."
1137 :global t :group 'help
1138 (if temp-buffer-resize-mode
1139 ;; `help-make-xrefs' may add a `back' button and thus increase the
1140 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
1141 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
1142 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
1143
1144 (defun resize-temp-buffer-window (&optional window)
1145 "Resize WINDOW to fit its contents.
1146 WINDOW must be a live window and defaults to the selected one.
1147 Do not resize if WINDOW was not created by `display-buffer'.
1148
1149 If WINDOW is part of a vertical combination, restrain its new
1150 size by `temp-buffer-max-height' and do not resize if its minimum
1151 accessible position is scrolled out of view. If WINDOW is part
1152 of a horizontal combination, restrain its new size by
1153 `temp-buffer-max-width'. In both cases, the value of the option
1154 `fit-window-to-buffer-horizontally' can inhibit resizing.
1155
1156 If WINDOW is the root window of its frame, resize the frame
1157 provided `fit-frame-to-buffer' is non-nil."
1158 (setq window (window-normalize-window window t))
1159 (let ((height (if (functionp temp-buffer-max-height)
1160 (with-selected-window window
1161 (funcall temp-buffer-max-height (window-buffer)))
1162 temp-buffer-max-height))
1163 (width (if (functionp temp-buffer-max-width)
1164 (with-selected-window window
1165 (funcall temp-buffer-max-width (window-buffer)))
1166 temp-buffer-max-width))
1167 (quit-cadr (cadr (window-parameter window 'quit-restore))))
1168 ;; Resize WINDOW iff it was made by `display-buffer'.
1169 (when (or (and (eq quit-cadr 'window)
1170 (or (and (window-combined-p window)
1171 (not (eq fit-window-to-buffer-horizontally
1172 'only))
1173 (pos-visible-in-window-p (point-min) window))
1174 (and (window-combined-p window t)
1175 fit-window-to-buffer-horizontally)))
1176 (and (eq quit-cadr 'frame)
1177 fit-frame-to-buffer
1178 (eq window (frame-root-window window))))
1179 (fit-window-to-buffer window height nil width))))
1180
1181 ;;; Help windows.
1182 (defcustom help-window-select 'other
1183 "Non-nil means select help window for viewing.
1184 Choices are:
1185 never (nil) Select help window only if there is no other window
1186 on its frame.
1187 other Select help window unless the selected window is the
1188 only other window on the help window's frame.
1189 always (t) Always select the help window.
1190
1191 This option has effect if and only if the help window was created
1192 by `with-help-window'"
1193 :type '(choice (const :tag "never (nil)" nil)
1194 (const :tag "other" other)
1195 (const :tag "always (t)" t))
1196 :group 'help
1197 :version "23.1")
1198
1199 (defcustom help-enable-auto-load t
1200 "Whether Help commands can perform autoloading.
1201 If non-nil, whenever \\[describe-function] is called for an
1202 autoloaded function whose docstring contains any key substitution
1203 construct (see `substitute-command-keys'), the library is loaded,
1204 so that the documentation can show the right key bindings."
1205 :type 'boolean
1206 :group 'help
1207 :version "24.3")
1208
1209 (defun help-window-display-message (quit-part window &optional scroll)
1210 "Display message telling how to quit and scroll help window.
1211 QUIT-PART is a string telling how to quit the help window WINDOW.
1212 Optional argument SCROLL non-nil means tell how to scroll WINDOW.
1213 SCROLL equal `other' means tell how to scroll the \"other\"
1214 window."
1215 (let ((scroll-part
1216 (cond
1217 ;; If we don't have QUIT-PART we probably reuse a window
1218 ;; showing the same buffer so we don't show any message.
1219 ((not quit-part) nil)
1220 ((pos-visible-in-window-p
1221 (with-current-buffer (window-buffer window)
1222 (point-max)) window t)
1223 ;; Buffer end is at least partially visible, no need to talk
1224 ;; about scrolling.
1225 ".")
1226 ((eq scroll 'other)
1227 ", \\[scroll-other-window] to scroll help.")
1228 (scroll ", \\[scroll-up] to scroll help."))))
1229 (message "%s"
1230 (substitute-command-keys (concat quit-part scroll-part)))))
1231
1232 (defun help-window-setup (window &optional value)
1233 "Set up help window WINDOW for `with-help-window'.
1234 WINDOW is the window used for displaying the help buffer.
1235 Return VALUE."
1236 (let* ((help-buffer (when (window-live-p window)
1237 (window-buffer window)))
1238 (help-setup (when (window-live-p window)
1239 (car (window-parameter window 'quit-restore)))))
1240 (when help-buffer
1241 ;; Handle `help-window-point-marker'.
1242 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1243 (set-window-point window help-window-point-marker)
1244 ;; Reset `help-window-point-marker'.
1245 (set-marker help-window-point-marker nil))
1246
1247 (cond
1248 ((or (eq window (selected-window))
1249 (and (or (eq help-window-select t)
1250 (eq help-setup 'frame)
1251 (and (eq help-window-select 'other)
1252 (eq (window-frame window) (selected-frame))
1253 (> (length (window-list nil 'no-mini)) 2)))
1254 (select-window window)))
1255 ;; The help window is or gets selected ...
1256 (help-window-display-message
1257 (cond
1258 ((eq help-setup 'window)
1259 ;; ... and is new, ...
1260 "Type \"q\" to delete help window")
1261 ((eq help-setup 'frame)
1262 "Type \"q\" to quit the help frame")
1263 ((eq help-setup 'other)
1264 ;; ... or displayed some other buffer before.
1265 "Type \"q\" to restore previous buffer"))
1266 window t))
1267 ((and (eq (window-frame window) (selected-frame))
1268 (= (length (window-list nil 'no-mini)) 2))
1269 ;; There are two windows on the help window's frame and the
1270 ;; other one is the selected one.
1271 (help-window-display-message
1272 (cond
1273 ((eq help-setup 'window)
1274 "Type \\[delete-other-windows] to delete the help window")
1275 ((eq help-setup 'other)
1276 "Type \"q\" in help window to restore its previous buffer"))
1277 window 'other))
1278 (t
1279 ;; The help window is not selected ...
1280 (help-window-display-message
1281 (cond
1282 ((eq help-setup 'window)
1283 ;; ... and is new, ...
1284 "Type \"q\" in help window to delete it")
1285 ((eq help-setup 'other)
1286 ;; ... or displayed some other buffer before.
1287 "Type \"q\" in help window to restore previous buffer"))
1288 window))))
1289 ;; Return VALUE.
1290 value))
1291
1292 ;; Called from C, on encountering `help-char' when reading a char.
1293 ;; Don't print to *Help*; that would clobber Help history.
1294 (defun help-form-show ()
1295 "Display the output of a non-nil `help-form'."
1296 (let ((msg (eval help-form)))
1297 (if (stringp msg)
1298 (with-output-to-temp-buffer " *Char Help*"
1299 (princ msg)))))
1300
1301 \f
1302 ;; The following functions used to be in help-fns.el, which is not preloaded.
1303 ;; But for various reasons, they are more widely needed, so they were
1304 ;; moved to this file, which is preloaded. http://debbugs.gnu.org/17001
1305
1306 (defun help-split-fundoc (docstring def)
1307 "Split a function DOCSTRING into the actual doc and the usage info.
1308 Return (USAGE . DOC) or nil if there's no usage info, where USAGE info
1309 is a string describing the argument list of DEF, such as
1310 \"(apply FUNCTION &rest ARGUMENTS)\".
1311 DEF is the function whose usage we're looking for in DOCSTRING."
1312 ;; Functions can get the calling sequence at the end of the doc string.
1313 ;; In cases where `function' has been fset to a subr we can't search for
1314 ;; function's name in the doc string so we use `fn' as the anonymous
1315 ;; function name instead.
1316 (when (and docstring (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring))
1317 (cons (format "(%s%s"
1318 ;; Replace `fn' with the actual function name.
1319 (if (symbolp def) def "anonymous")
1320 (match-string 1 docstring))
1321 (unless (zerop (match-beginning 0))
1322 (substring docstring 0 (match-beginning 0))))))
1323
1324 (defun help-add-fundoc-usage (docstring arglist)
1325 "Add the usage info to DOCSTRING.
1326 If DOCSTRING already has a usage info, then just return it unchanged.
1327 The usage info is built from ARGLIST. DOCSTRING can be nil.
1328 ARGLIST can also be t or a string of the form \"(FUN ARG1 ARG2 ...)\"."
1329 (unless (stringp docstring) (setq docstring ""))
1330 (if (or (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring)
1331 (eq arglist t))
1332 docstring
1333 (concat docstring
1334 (if (string-match "\n?\n\\'" docstring)
1335 (if (< (- (match-end 0) (match-beginning 0)) 2) "\n" "")
1336 "\n\n")
1337 (if (and (stringp arglist)
1338 (string-match "\\`([^ ]+\\(.*\\))\\'" arglist))
1339 (concat "(fn" (match-string 1 arglist) ")")
1340 (format "%S" (help-make-usage 'fn arglist))))))
1341
1342 (defun help-function-arglist (def &optional preserve-names)
1343 "Return a formal argument list for the function DEF.
1344 IF PRESERVE-NAMES is non-nil, return a formal arglist that uses
1345 the same names as used in the original source code, when possible."
1346 ;; Handle symbols aliased to other symbols.
1347 (if (and (symbolp def) (fboundp def)) (setq def (indirect-function def)))
1348 ;; If definition is a macro, find the function inside it.
1349 (if (eq (car-safe def) 'macro) (setq def (cdr def)))
1350 (cond
1351 ((and (byte-code-function-p def) (listp (aref def 0))) (aref def 0))
1352 ((eq (car-safe def) 'lambda) (nth 1 def))
1353 ((eq (car-safe def) 'closure) (nth 2 def))
1354 ((or (and (byte-code-function-p def) (integerp (aref def 0)))
1355 (subrp def))
1356 (or (when preserve-names
1357 (let* ((doc (condition-case nil (documentation def) (error nil)))
1358 (docargs (if doc (car (help-split-fundoc doc nil))))
1359 (arglist (if docargs
1360 (cdar (read-from-string (downcase docargs)))))
1361 (valid t))
1362 ;; Check validity.
1363 (dolist (arg arglist)
1364 (unless (and (symbolp arg)
1365 (let ((name (symbol-name arg)))
1366 (if (eq (aref name 0) ?&)
1367 (memq arg '(&rest &optional))
1368 (not (string-match "\\." name)))))
1369 (setq valid nil)))
1370 (when valid arglist)))
1371 (let* ((args-desc (if (not (subrp def))
1372 (aref def 0)
1373 (let ((a (subr-arity def)))
1374 (logior (car a)
1375 (if (numberp (cdr a))
1376 (lsh (cdr a) 8)
1377 (lsh 1 7))))))
1378 (max (lsh args-desc -8))
1379 (min (logand args-desc 127))
1380 (rest (logand args-desc 128))
1381 (arglist ()))
1382 (dotimes (i min)
1383 (push (intern (concat "arg" (number-to-string (1+ i)))) arglist))
1384 (when (> max min)
1385 (push '&optional arglist)
1386 (dotimes (i (- max min))
1387 (push (intern (concat "arg" (number-to-string (+ 1 i min))))
1388 arglist)))
1389 (unless (zerop rest) (push '&rest arglist) (push 'rest arglist))
1390 (nreverse arglist))))
1391 ((and (autoloadp def) (not (eq (nth 4 def) 'keymap)))
1392 "[Arg list not available until function definition is loaded.]")
1393 (t t)))
1394
1395 (defun help-make-usage (function arglist)
1396 (cons (if (symbolp function) function 'anonymous)
1397 (mapcar (lambda (arg)
1398 (if (not (symbolp arg)) arg
1399 (let ((name (symbol-name arg)))
1400 (cond
1401 ((string-match "\\`&" name) arg)
1402 ((string-match "\\`_" name)
1403 (intern (upcase (substring name 1))))
1404 (t (intern (upcase name)))))))
1405 arglist)))
1406
1407 \f
1408 (provide 'help)
1409
1410 ;;; help.el ends here