Fix pty handling on FreeBSD 8.0 (tiny change)
[emacs.git] / lisp / help.el
blob19cb811bcf53230f922a261e6c7d25bbc0271fcd
1 ;;; help.el --- help commands for Emacs
3 ;; Copyright (C) 1985-1986, 1993-1994, 1998-2012 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: help, internal
7 ;; Package: emacs
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
24 ;;; Commentary:
26 ;; This code implements GNU Emacs's on-line help system, the one invoked by
27 ;; `M-x help-for-help'.
29 ;;; Code:
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))
35 ;; This makes `with-output-to-temp-buffer' buffers use `help-mode'.
36 (add-hook 'temp-buffer-setup-hook 'help-mode-setup)
37 (add-hook 'temp-buffer-show-hook 'help-mode-finish)
39 ;; `help-window-point-marker' is a marker you can move to a valid
40 ;; position of the buffer shown in the help window in order to override
41 ;; the standard positioning mechanism (`point-min') chosen by
42 ;; `with-output-to-temp-buffer'. `with-help-window' has this point
43 ;; nowhere before exiting. Currently used by `view-lossage' to assert
44 ;; that the last keystrokes are always visible.
45 (defvar help-window-point-marker (make-marker)
46 "Marker to override default `window-point' in help windows.")
48 (defvar help-map
49 (let ((map (make-sparse-keymap)))
50 (define-key map (char-to-string help-char) 'help-for-help)
51 (define-key map [help] 'help-for-help)
52 (define-key map [f1] 'help-for-help)
53 (define-key map "." 'display-local-help)
54 (define-key map "?" 'help-for-help)
56 (define-key map "\C-a" 'about-emacs)
57 (define-key map "\C-c" 'describe-copying)
58 (define-key map "\C-d" 'view-emacs-debugging)
59 (define-key map "\C-e" 'view-external-packages)
60 (define-key map "\C-f" 'view-emacs-FAQ)
61 (define-key map "\C-m" 'view-order-manuals)
62 (define-key map "\C-n" 'view-emacs-news)
63 (define-key map "\C-o" 'describe-distribution)
64 (define-key map "\C-p" 'view-emacs-problems)
65 (define-key map "\C-t" 'view-emacs-todo)
66 (define-key map "\C-w" 'describe-no-warranty)
68 ;; This does not fit the pattern, but it is natural given the C-\ command.
69 (define-key map "\C-\\" 'describe-input-method)
71 (define-key map "C" 'describe-coding-system)
72 (define-key map "F" 'Info-goto-emacs-command-node)
73 (define-key map "I" 'describe-input-method)
74 (define-key map "K" 'Info-goto-emacs-key-command-node)
75 (define-key map "L" 'describe-language-environment)
76 (define-key map "S" 'info-lookup-symbol)
78 (define-key map "a" 'apropos-command)
79 (define-key map "b" 'describe-bindings)
80 (define-key map "c" 'describe-key-briefly)
81 (define-key map "d" 'apropos-documentation)
82 (define-key map "e" 'view-echo-area-messages)
83 (define-key map "f" 'describe-function)
84 (define-key map "g" 'describe-gnu-project)
85 (define-key map "h" 'view-hello-file)
87 (define-key map "i" 'info)
88 (define-key map "4i" 'info-other-window)
90 (define-key map "k" 'describe-key)
91 (define-key map "l" 'view-lossage)
92 (define-key map "m" 'describe-mode)
93 (define-key map "n" 'view-emacs-news)
94 (define-key map "p" 'finder-by-keyword)
95 (define-key map "P" 'describe-package)
96 (define-key map "r" 'info-emacs-manual)
97 (define-key map "s" 'describe-syntax)
98 (define-key map "t" 'help-with-tutorial)
99 (define-key map "w" 'where-is)
100 (define-key map "v" 'describe-variable)
101 (define-key map "q" 'help-quit)
102 map)
103 "Keymap for characters following the Help key.")
105 (define-key global-map (char-to-string help-char) 'help-command)
106 (define-key global-map [help] 'help-command)
107 (define-key global-map [f1] 'help-command)
108 (fset 'help-command help-map)
110 ;; insert-button makes the action nil if it is not store somewhere
111 (defvar help-button-cache nil)
114 (defun help-quit ()
115 "Just exit from the Help command's command loop."
116 (interactive)
117 nil)
119 (defvar help-return-method nil
120 "What to do to \"exit\" the help buffer.
121 This is a list
122 (WINDOW . t) delete the selected window (and possibly its frame,
123 see `quit-window'), go to WINDOW.
124 (WINDOW . quit-window) do quit-window, then select WINDOW.
125 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
127 (define-obsolete-function-alias 'print-help-return-message 'help-print-return-message "23.2")
128 (defun help-print-return-message (&optional function)
129 "Display or return message saying how to restore windows after help command.
130 This function assumes that `standard-output' is the help buffer.
131 It computes a message, and applies the optional argument FUNCTION to it.
132 If FUNCTION is nil, it applies `message', thus displaying the message.
133 In addition, this function sets up `help-return-method', which see, that
134 specifies what to do when the user exits the help buffer."
135 (and (not (get-buffer-window standard-output))
136 (let ((first-message
137 (cond ((or
138 pop-up-frames
139 (special-display-p (buffer-name standard-output)))
140 (setq help-return-method (cons (selected-window) t))
141 ;; If the help output buffer is a special display buffer,
142 ;; don't say anything about how to get rid of it.
143 ;; First of all, the user will do that with the window
144 ;; manager, not with Emacs.
145 ;; Secondly, the buffer has not been displayed yet,
146 ;; so we don't know whether its frame will be selected.
147 nil)
148 (display-buffer-reuse-frames
149 (setq help-return-method (cons (selected-window)
150 'quit-window))
151 nil)
152 ((not (one-window-p t))
153 (setq help-return-method
154 (cons (selected-window) 'quit-window))
155 "Type \\[display-buffer] RET to restore the other window.")
156 (pop-up-windows
157 (setq help-return-method (cons (selected-window) t))
158 "Type \\[delete-other-windows] to remove help window.")
160 (setq help-return-method
161 (list (selected-window) (window-buffer)
162 (window-start) (window-point)))
163 "Type \\[switch-to-buffer] RET to remove help window."))))
164 (funcall (or function 'message)
165 (concat
166 (if first-message
167 (substitute-command-keys first-message))
168 (if first-message " ")
169 ;; If the help buffer will go in a separate frame,
170 ;; it's no use mentioning a command to scroll, so don't.
171 (if (or pop-up-windows
172 (special-display-p (buffer-name standard-output)))
174 (if (same-window-p (buffer-name standard-output))
175 ;; Say how to scroll this window.
176 (substitute-command-keys
177 "\\[scroll-up] to scroll the help.")
178 ;; Say how to scroll some other window.
179 (substitute-command-keys
180 "\\[scroll-other-window] to scroll the help."))))))))
182 ;; So keyboard macro definitions are documented correctly
183 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
185 (defalias 'help 'help-for-help-internal)
186 ;; find-function can find this.
187 (defalias 'help-for-help 'help-for-help-internal)
188 ;; It can't find this, but nobody will look.
189 (make-help-screen help-for-help-internal
190 (purecopy "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?")
191 ;; Don't purecopy this one, because it's not evaluated (it's
192 ;; directly used as a docstring in a function definition, so it'll
193 ;; be moved to the DOC file anyway: no need for purecopying it).
194 "You have typed %THIS-KEY%, the help character. Type a Help option:
195 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
197 a PATTERN Show commands whose name matches the PATTERN (a list of words
198 or a regexp). See also the `apropos' command.
199 b Display all key bindings.
200 c KEYS Display the command name run by the given key sequence.
201 C CODING Describe the given coding system, or RET for current ones.
202 d PATTERN Show a list of functions, variables, and other items whose
203 documentation matches the PATTERN (a list of words or a regexp).
204 e Go to the *Messages* buffer which logs echo-area messages.
205 f FUNCTION Display documentation for the given function.
206 F COMMAND Show the on-line manual's section that describes the command.
207 g Display information about the GNU project.
208 h Display the HELLO file which illustrates various scripts.
209 i Start the Info documentation reader: read on-line manuals.
210 I METHOD Describe a specific input method, or RET for current.
211 k KEYS Display the full documentation for the key sequence.
212 K KEYS Show the on-line manual's section for the command bound to KEYS.
213 l Show last 300 input keystrokes (lossage).
214 L LANG-ENV Describes a specific language environment, or RET for current.
215 m Display documentation of current minor modes and current major mode,
216 including their special commands.
217 n Display news of recent Emacs changes.
218 p TOPIC Find packages matching a given topic keyword.
219 r Display the Emacs manual in Info mode.
220 s Display contents of current syntax table, plus explanations.
221 S SYMBOL Show the section for the given symbol in the on-line manual
222 for the programming language used in this buffer.
223 t Start the Emacs learn-by-doing tutorial.
224 v VARIABLE Display the given variable's documentation and value.
225 w COMMAND Display which keystrokes invoke the given command (where-is).
226 . Display any available local help at point in the echo area.
228 C-a Information about Emacs.
229 C-c Emacs copying permission (GNU General Public License).
230 C-d Instructions for debugging GNU Emacs.
231 C-e External packages and information about Emacs.
232 C-f Emacs FAQ.
233 C-m How to order printed Emacs manuals.
234 C-n News of recent Emacs changes.
235 C-o Emacs ordering and distribution information.
236 C-p Info about known Emacs problems.
237 C-t Emacs TODO list.
238 C-w Information on absence of warranty for GNU Emacs."
239 help-map)
243 (defun function-called-at-point ()
244 "Return a function around point or else called by the list containing point.
245 If that doesn't give a function, return nil."
246 (with-syntax-table emacs-lisp-mode-syntax-table
247 (or (condition-case ()
248 (save-excursion
249 (or (not (zerop (skip-syntax-backward "_w")))
250 (eq (char-syntax (following-char)) ?w)
251 (eq (char-syntax (following-char)) ?_)
252 (forward-sexp -1))
253 (skip-chars-forward "'")
254 (let ((obj (read (current-buffer))))
255 (and (symbolp obj) (fboundp obj) obj)))
256 (error nil))
257 (condition-case ()
258 (save-excursion
259 (save-restriction
260 (narrow-to-region (max (point-min)
261 (- (point) 1000)) (point-max))
262 ;; Move up to surrounding paren, then after the open.
263 (backward-up-list 1)
264 (forward-char 1)
265 ;; If there is space here, this is probably something
266 ;; other than a real Lisp function call, so ignore it.
267 (if (looking-at "[ \t]")
268 (error "Probably not a Lisp function call"))
269 (let ((obj (read (current-buffer))))
270 (and (symbolp obj) (fboundp obj) obj))))
271 (error nil))
272 (let* ((str (find-tag-default))
273 (sym (if str (intern-soft str))))
274 (if (and sym (fboundp sym))
276 (save-match-data
277 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
278 (setq sym (intern-soft (match-string 1 str)))
279 (and (fboundp sym) sym))))))))
282 ;;; `User' help functions
284 (defun view-help-file (file &optional dir)
285 (view-file (expand-file-name file (or dir data-directory)))
286 (goto-address-mode 1)
287 (goto-char (point-min)))
289 (defun describe-distribution ()
290 "Display info on how to obtain the latest version of GNU Emacs."
291 (interactive)
292 (view-help-file "DISTRIB"))
294 (defun describe-copying ()
295 "Display info on how you may redistribute copies of GNU Emacs."
296 (interactive)
297 (view-help-file "COPYING"))
299 (defun describe-gnu-project ()
300 "Display info on the GNU project."
301 (interactive)
302 (view-help-file "THE-GNU-PROJECT"))
304 (define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2")
306 (defun describe-no-warranty ()
307 "Display info on all the kinds of warranty Emacs does NOT have."
308 (interactive)
309 (describe-copying)
310 (let (case-fold-search)
311 (search-forward "Disclaimer of Warranty")
312 (forward-line 0)
313 (recenter 0)))
315 (defun describe-prefix-bindings ()
316 "Describe the bindings of the prefix used to reach this command.
317 The prefix described consists of all but the last event
318 of the key sequence that ran this command."
319 (interactive)
320 (let ((key (this-command-keys)))
321 (describe-bindings
322 (if (stringp key)
323 (substring key 0 (1- (length key)))
324 (let ((prefix (make-vector (1- (length key)) nil))
325 (i 0))
326 (while (< i (length prefix))
327 (aset prefix i (aref key i))
328 (setq i (1+ i)))
329 prefix)))))
330 ;; Make C-h after a prefix, when not specifically bound,
331 ;; run describe-prefix-bindings.
332 (setq prefix-help-command 'describe-prefix-bindings)
334 (defun view-emacs-news (&optional version)
335 "Display info on recent changes to Emacs.
336 With argument, display info only for the selected version."
337 (interactive "P")
338 (unless version
339 (setq version emacs-major-version))
340 (when (consp version)
341 (let* ((all-versions
342 (let (res)
343 (mapc
344 (lambda (file)
345 (with-temp-buffer
346 (insert-file-contents
347 (expand-file-name file data-directory))
348 (while (re-search-forward
349 (if (member file '("NEWS.18" "NEWS.1-17"))
350 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
351 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
352 (setq res (cons (match-string-no-properties 1) res)))))
353 (cons "NEWS"
354 (directory-files data-directory nil
355 "^NEWS\\.[0-9][-0-9]*$" nil)))
356 (sort (delete-dups res) (lambda (a b) (string< b a)))))
357 (current (car all-versions)))
358 (setq version (completing-read
359 (format "Read NEWS for the version (default %s): " current)
360 all-versions nil nil nil nil current))
361 (if (integerp (string-to-number version))
362 (setq version (string-to-number version))
363 (unless (or (member version all-versions)
364 (<= (string-to-number version) (string-to-number current)))
365 (error "No news about version %s" version)))))
366 (when (integerp version)
367 (cond ((<= version 12)
368 (setq version (format "1.%d" version)))
369 ((<= version 18)
370 (setq version (format "%d" version)))
371 ((> version emacs-major-version)
372 (error "No news about Emacs %d (yet)" version))))
373 (let* ((vn (if (stringp version)
374 (string-to-number version)
375 version))
376 (file (cond
377 ((>= vn emacs-major-version) "NEWS")
378 ((< vn 18) "NEWS.1-17")
379 (t (format "NEWS.%d" vn))))
380 res)
381 (view-file (expand-file-name file data-directory))
382 (widen)
383 (goto-char (point-min))
384 (when (stringp version)
385 (when (re-search-forward
386 (concat (if (< vn 19)
387 "Changes in Emacs[ \t]*"
388 "^\* [^0-9\n]*") version "$")
389 nil t)
390 (beginning-of-line)
391 (narrow-to-region
392 (point)
393 (save-excursion
394 (while (and (setq res
395 (re-search-forward
396 (if (< vn 19)
397 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
398 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
399 (equal (match-string-no-properties 1) version)))
400 (or res (goto-char (point-max)))
401 (beginning-of-line)
402 (point)))))))
404 (defun view-emacs-todo (&optional _arg)
405 "Display the Emacs TODO list."
406 (interactive "P")
407 (view-help-file "TODO"))
409 (define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2")
412 (defun view-echo-area-messages ()
413 "View the log of recent echo-area messages: the `*Messages*' buffer.
414 The number of messages retained in that buffer
415 is specified by the variable `message-log-max'."
416 (interactive)
417 (switch-to-buffer (get-buffer-create "*Messages*")))
419 (defun view-order-manuals ()
420 "Display the Emacs ORDERS file."
421 (interactive)
422 (view-help-file "ORDERS"))
424 (defun view-emacs-FAQ ()
425 "Display the Emacs Frequently Asked Questions (FAQ) file."
426 (interactive)
427 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
428 (info "(efaq)"))
430 (defun view-emacs-problems ()
431 "Display info on known problems with Emacs and possible workarounds."
432 (interactive)
433 (view-help-file "PROBLEMS"))
435 (defun view-emacs-debugging ()
436 "Display info on how to debug Emacs problems."
437 (interactive)
438 (view-help-file "DEBUG"))
440 (defun view-external-packages ()
441 "Display external packages and information about Emacs."
442 (interactive)
443 (view-help-file "MORE.STUFF"))
445 (defun view-lossage ()
446 "Display last 300 input keystrokes.
448 To record all your input on a file, use `open-dribble-file'."
449 (interactive)
450 (help-setup-xref (list #'view-lossage)
451 (called-interactively-p 'interactive))
452 (with-help-window (help-buffer)
453 (princ (mapconcat (lambda (key)
454 (if (or (integerp key) (symbolp key) (listp key))
455 (single-key-description key)
456 (prin1-to-string key nil)))
457 (recent-keys)
458 " "))
459 (with-current-buffer standard-output
460 (goto-char (point-min))
461 (while (progn (move-to-column 50) (not (eobp)))
462 (when (search-forward " " nil t)
463 (delete-char -1))
464 (insert "\n"))
465 ;; jidanni wants to see the last keystrokes immediately.
466 (set-marker help-window-point-marker (point)))))
469 ;; Key bindings
471 (defun describe-bindings (&optional prefix buffer)
472 "Show a list of all defined keys, and their definitions.
473 We put that list in a buffer, and display the buffer.
475 The optional argument PREFIX, if non-nil, should be a key sequence;
476 then we display only bindings that start with that prefix.
477 The optional argument BUFFER specifies which buffer's bindings
478 to display (default, the current buffer). BUFFER can be a buffer
479 or a buffer name."
480 (interactive)
481 (or buffer (setq buffer (current-buffer)))
482 (help-setup-xref (list #'describe-bindings prefix buffer)
483 (called-interactively-p 'interactive))
484 (with-current-buffer buffer
485 (describe-bindings-internal nil prefix)))
487 ;; This function used to be in keymap.c.
488 (defun describe-bindings-internal (&optional menus prefix)
489 "Show a list of all defined keys, and their definitions.
490 We put that list in a buffer, and display the buffer.
492 The optional argument MENUS, if non-nil, says to mention menu bindings.
493 \(Ordinarily these are omitted from the output.)
494 The optional argument PREFIX, if non-nil, should be a key sequence;
495 then we display only bindings that start with that prefix."
496 (let ((buf (current-buffer)))
497 (with-help-window "*Help*"
498 (with-current-buffer standard-output
499 (describe-buffer-bindings buf prefix menus)))))
501 (defun where-is (definition &optional insert)
502 "Print message listing key sequences that invoke the command DEFINITION.
503 Argument is a command definition, usually a symbol with a function definition.
504 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
505 (interactive
506 (let ((fn (function-called-at-point))
507 (enable-recursive-minibuffers t)
508 val)
509 (setq val (completing-read
510 (if fn
511 (format "Where is command (default %s): " fn)
512 "Where is command: ")
513 obarray 'commandp t))
514 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
515 (unless definition (error "No command"))
516 (let ((func (indirect-function definition))
517 (defs nil)
518 (standard-output (if insert (current-buffer) standard-output)))
519 ;; In DEFS, find all symbols that are aliases for DEFINITION.
520 (mapatoms (lambda (symbol)
521 (and (fboundp symbol)
522 (not (eq symbol definition))
523 (eq func (condition-case ()
524 (indirect-function symbol)
525 (error symbol)))
526 (push symbol defs))))
527 ;; Look at all the symbols--first DEFINITION,
528 ;; then its aliases.
529 (dolist (symbol (cons definition defs))
530 (let* ((remapped (command-remapping symbol))
531 (keys (where-is-internal
532 symbol overriding-local-map nil nil remapped))
533 (keys (mapconcat 'key-description keys ", "))
534 string)
535 (setq string
536 (if insert
537 (if (> (length keys) 0)
538 (if remapped
539 (format "%s (%s) (remapped from %s)"
540 keys remapped symbol)
541 (format "%s (%s)" keys symbol))
542 (format "M-x %s RET" symbol))
543 (if (> (length keys) 0)
544 (if remapped
545 (format "%s is remapped to %s which is on %s"
546 symbol remapped keys)
547 (format "%s is on %s" symbol keys))
548 ;; If this is the command the user asked about,
549 ;; and it is not on any key, say so.
550 ;; For other symbols, its aliases, say nothing
551 ;; about them unless they are on keys.
552 (if (eq symbol definition)
553 (format "%s is not on any key" symbol)))))
554 (when string
555 (unless (eq symbol definition)
556 (princ ";\n its alias "))
557 (princ string)))))
558 nil)
560 (defun help-key-description (key untranslated)
561 (let ((string (key-description key)))
562 (if (or (not untranslated)
563 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
564 string
565 (let ((otherstring (key-description untranslated)))
566 (if (equal string otherstring)
567 string
568 (format "%s (translated from %s)" string otherstring))))))
570 (defun describe-key-briefly (&optional key insert untranslated)
571 "Print the name of the function KEY invokes. KEY is a string.
572 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
573 If non-nil, UNTRANSLATED is a vector of the untranslated events.
574 It can also be a number in which case the untranslated events from
575 the last key hit are used.
577 If KEY is a menu item or a tool-bar button that is disabled, this command
578 temporarily enables it to allow getting help on disabled items and buttons."
579 (interactive
580 (let ((enable-disabled-menus-and-buttons t)
581 (cursor-in-echo-area t)
582 saved-yank-menu)
583 (unwind-protect
584 (let (key)
585 ;; If yank-menu is empty, populate it temporarily, so that
586 ;; "Select and Paste" menu can generate a complete event.
587 (when (null (cdr yank-menu))
588 (setq saved-yank-menu (copy-sequence yank-menu))
589 (menu-bar-update-yank-menu "(any string)" nil))
590 (setq key (read-key-sequence "Describe key (or click or menu item): "))
591 ;; If KEY is a down-event, read and discard the
592 ;; corresponding up-event. Note that there are also
593 ;; down-events on scroll bars and mode lines: the actual
594 ;; event then is in the second element of the vector.
595 (and (vectorp key)
596 (let ((last-idx (1- (length key))))
597 (and (eventp (aref key last-idx))
598 (memq 'down (event-modifiers (aref key last-idx)))))
599 (read-event))
600 (list
602 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
604 ;; Put yank-menu back as it was, if we changed it.
605 (when saved-yank-menu
606 (setq yank-menu (copy-sequence saved-yank-menu))
607 (fset 'yank-menu (cons 'keymap yank-menu))))))
608 (if (numberp untranslated)
609 (setq untranslated (this-single-command-raw-keys)))
610 (let* ((event (if (and (symbolp (aref key 0))
611 (> (length key) 1)
612 (consp (aref key 1)))
613 (aref key 1)
614 (aref key 0)))
615 (modifiers (event-modifiers event))
616 (standard-output (if insert (current-buffer) standard-output))
617 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
618 (memq 'drag modifiers)) " at that spot" ""))
619 (defn (key-binding key t))
620 key-desc)
621 ;; Handle the case where we faked an entry in "Select and Paste" menu.
622 (if (and (eq defn nil)
623 (stringp (aref key (1- (length key))))
624 (eq (key-binding (substring key 0 -1)) 'yank-menu))
625 (setq defn 'menu-bar-select-yank))
626 ;; Don't bother user with strings from (e.g.) the select-paste menu.
627 (if (stringp (aref key (1- (length key))))
628 (aset key (1- (length key)) "(any string)"))
629 (if (and (> (length untranslated) 0)
630 (stringp (aref untranslated (1- (length untranslated)))))
631 (aset untranslated (1- (length untranslated)) "(any string)"))
632 ;; Now describe the key, perhaps as changed.
633 (setq key-desc (help-key-description key untranslated))
634 (if (or (null defn) (integerp defn) (equal defn 'undefined))
635 (princ (format "%s%s is undefined" key-desc mouse-msg))
636 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
638 (defun describe-key (&optional key untranslated up-event)
639 "Display documentation of the function invoked by KEY.
640 KEY can be any kind of a key sequence; it can include keyboard events,
641 mouse events, and/or menu events. When calling from a program,
642 pass KEY as a string or a vector.
644 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
645 It can also be a number, in which case the untranslated events from
646 the last key sequence entered are used.
647 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
649 If KEY is a menu item or a tool-bar button that is disabled, this command
650 temporarily enables it to allow getting help on disabled items and buttons."
651 (interactive
652 (let ((enable-disabled-menus-and-buttons t)
653 (cursor-in-echo-area t)
654 saved-yank-menu)
655 (unwind-protect
656 (let (key)
657 ;; If yank-menu is empty, populate it temporarily, so that
658 ;; "Select and Paste" menu can generate a complete event.
659 (when (null (cdr yank-menu))
660 (setq saved-yank-menu (copy-sequence yank-menu))
661 (menu-bar-update-yank-menu "(any string)" nil))
662 (setq key (read-key-sequence "Describe key (or click or menu item): "))
663 (list
665 (prefix-numeric-value current-prefix-arg)
666 ;; If KEY is a down-event, read and include the
667 ;; corresponding up-event. Note that there are also
668 ;; down-events on scroll bars and mode lines: the actual
669 ;; event then is in the second element of the vector.
670 (and (vectorp key)
671 (let ((last-idx (1- (length key))))
672 (and (eventp (aref key last-idx))
673 (memq 'down (event-modifiers (aref key last-idx)))))
674 (or (and (eventp (aref key 0))
675 (memq 'down (event-modifiers (aref key 0)))
676 ;; However, for the C-down-mouse-2 popup
677 ;; menu, there is no subsequent up-event. In
678 ;; this case, the up-event is the next
679 ;; element in the supplied vector.
680 (= (length key) 1))
681 (and (> (length key) 1)
682 (eventp (aref key 1))
683 (memq 'down (event-modifiers (aref key 1)))))
684 (read-event))))
685 ;; Put yank-menu back as it was, if we changed it.
686 (when saved-yank-menu
687 (setq yank-menu (copy-sequence saved-yank-menu))
688 (fset 'yank-menu (cons 'keymap yank-menu))))))
689 (if (numberp untranslated)
690 (setq untranslated (this-single-command-raw-keys)))
691 (let* ((event (aref key (if (and (symbolp (aref key 0))
692 (> (length key) 1)
693 (consp (aref key 1)))
695 0)))
696 (modifiers (event-modifiers event))
697 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
698 (memq 'drag modifiers)) " at that spot" ""))
699 (defn (key-binding key t))
700 defn-up defn-up-tricky ev-type
701 mouse-1-remapped mouse-1-tricky)
703 ;; Handle the case where we faked an entry in "Select and Paste" menu.
704 (when (and (eq defn nil)
705 (stringp (aref key (1- (length key))))
706 (eq (key-binding (substring key 0 -1)) 'yank-menu))
707 (setq defn 'menu-bar-select-yank))
708 (if (or (null defn) (integerp defn) (equal defn 'undefined))
709 (message "%s%s is undefined"
710 (help-key-description key untranslated) mouse-msg)
711 (help-setup-xref (list #'describe-function defn)
712 (called-interactively-p 'interactive))
713 ;; Don't bother user with strings from (e.g.) the select-paste menu.
714 (when (stringp (aref key (1- (length key))))
715 (aset key (1- (length key)) "(any string)"))
716 (when (and untranslated
717 (stringp (aref untranslated (1- (length untranslated)))))
718 (aset untranslated (1- (length untranslated))
719 "(any string)"))
720 ;; Need to do this before erasing *Help* buffer in case event
721 ;; is a mouse click in an existing *Help* buffer.
722 (when up-event
723 (setq ev-type (event-basic-type up-event))
724 (let ((sequence (vector up-event)))
725 (when (and (eq ev-type 'mouse-1)
726 mouse-1-click-follows-link
727 (not (eq mouse-1-click-follows-link 'double))
728 (setq mouse-1-remapped
729 (mouse-on-link-p (event-start up-event))))
730 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
731 (> mouse-1-click-follows-link 0)))
732 (cond ((stringp mouse-1-remapped)
733 (setq sequence mouse-1-remapped))
734 ((vectorp mouse-1-remapped)
735 (setcar up-event (elt mouse-1-remapped 0)))
736 (t (setcar up-event 'mouse-2))))
737 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
738 (when mouse-1-tricky
739 (setq sequence (vector up-event))
740 (aset sequence 0 'mouse-1)
741 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
742 (with-help-window (help-buffer)
743 (princ (help-key-description key untranslated))
744 (princ (format "\
745 %s runs the command %S, which is "
746 mouse-msg defn))
747 (describe-function-1 defn)
748 (when up-event
749 (unless (or (null defn-up)
750 (integerp defn-up)
751 (equal defn-up 'undefined))
752 (princ (format "
754 ----------------- up-event %s----------------
756 %s%s%s runs the command %S, which is "
757 (if mouse-1-tricky "(short click) " "")
758 (key-description (vector up-event))
759 mouse-msg
760 (if mouse-1-remapped
761 " is remapped to <mouse-2>, which" "")
762 defn-up))
763 (describe-function-1 defn-up))
764 (unless (or (null defn-up-tricky)
765 (integerp defn-up-tricky)
766 (eq defn-up-tricky 'undefined))
767 (princ (format "
769 ----------------- up-event (long click) ----------------
771 Pressing <%S>%s for longer than %d milli-seconds
772 runs the command %S, which is "
773 ev-type mouse-msg
774 mouse-1-click-follows-link
775 defn-up-tricky))
776 (describe-function-1 defn-up-tricky)))))))
778 (defun describe-mode (&optional buffer)
779 "Display documentation of current major mode and minor modes.
780 A brief summary of the minor modes comes first, followed by the
781 major mode description. This is followed by detailed
782 descriptions of the minor modes, each on a separate page.
784 For this to work correctly for a minor mode, the mode's indicator
785 variable \(listed in `minor-mode-alist') must also be a function
786 whose documentation describes the minor mode.
788 If called from Lisp with a non-nil BUFFER argument, display
789 documentation for the major and minor modes of that buffer."
790 (interactive "@")
791 (unless buffer (setq buffer (current-buffer)))
792 (help-setup-xref (list #'describe-mode buffer)
793 (called-interactively-p 'interactive))
794 ;; For the sake of help-do-xref and help-xref-go-back,
795 ;; don't switch buffers before calling `help-buffer'.
796 (with-help-window (help-buffer)
797 (with-current-buffer buffer
798 (let (minor-modes)
799 ;; Older packages do not register in minor-mode-list but only in
800 ;; minor-mode-alist.
801 (dolist (x minor-mode-alist)
802 (setq x (car x))
803 (unless (memq x minor-mode-list)
804 (push x minor-mode-list)))
805 ;; Find enabled minor mode we will want to mention.
806 (dolist (mode minor-mode-list)
807 ;; Document a minor mode if it is listed in minor-mode-alist,
808 ;; non-nil, and has a function definition.
809 (let ((fmode (or (get mode :minor-mode-function) mode)))
810 (and (boundp mode) (symbol-value mode)
811 (fboundp fmode)
812 (let ((pretty-minor-mode
813 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
814 (symbol-name fmode))
815 (capitalize
816 (substring (symbol-name fmode)
817 0 (match-beginning 0)))
818 fmode)))
819 (push (list fmode pretty-minor-mode
820 (format-mode-line (assq mode minor-mode-alist)))
821 minor-modes)))))
822 (setq minor-modes
823 (sort minor-modes
824 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
825 (when minor-modes
826 (princ "Enabled minor modes:\n")
827 (make-local-variable 'help-button-cache)
828 (with-current-buffer standard-output
829 (dolist (mode minor-modes)
830 (let ((mode-function (nth 0 mode))
831 (pretty-minor-mode (nth 1 mode))
832 (indicator (nth 2 mode)))
833 (add-text-properties 0 (length pretty-minor-mode)
834 '(face bold) pretty-minor-mode)
835 (save-excursion
836 (goto-char (point-max))
837 (princ "\n\f\n")
838 (push (point-marker) help-button-cache)
839 ;; Document the minor modes fully.
840 (insert pretty-minor-mode)
841 (princ (format " minor mode (%s):\n"
842 (if (zerop (length indicator))
843 "no indicator"
844 (format "indicator%s"
845 indicator))))
846 (princ (documentation mode-function)))
847 (insert-button pretty-minor-mode
848 'action (car help-button-cache)
849 'follow-link t
850 'help-echo "mouse-2, RET: show full information")
851 (newline)))
852 (forward-line -1)
853 (fill-paragraph nil)
854 (forward-line 1))
856 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
857 ;; Document the major mode.
858 (let ((mode mode-name))
859 (with-current-buffer standard-output
860 (let ((start (point)))
861 (insert (format-mode-line mode nil nil buffer))
862 (add-text-properties start (point) '(face bold)))))
863 (princ " mode")
864 (let* ((mode major-mode)
865 (file-name (find-lisp-object-file-name mode nil)))
866 (when file-name
867 (princ (concat " defined in `" (file-name-nondirectory file-name) "'"))
868 ;; Make a hyperlink to the library.
869 (with-current-buffer standard-output
870 (save-excursion
871 (re-search-backward "`\\([^`']+\\)'" nil t)
872 (help-xref-button 1 'help-function-def mode file-name)))))
873 (princ ":\n")
874 (princ (documentation major-mode)))))
875 ;; For the sake of IELM and maybe others
876 nil)
879 (defun describe-minor-mode (minor-mode)
880 "Display documentation of a minor mode given as MINOR-MODE.
881 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
882 appeared on the mode-line."
883 (interactive (list (completing-read
884 "Minor mode: "
885 (nconc
886 (describe-minor-mode-completion-table-for-symbol)
887 (describe-minor-mode-completion-table-for-indicator)
888 ))))
889 (if (symbolp minor-mode)
890 (setq minor-mode (symbol-name minor-mode)))
891 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
892 (indicators (describe-minor-mode-completion-table-for-indicator)))
893 (cond
894 ((member minor-mode symbols)
895 (describe-minor-mode-from-symbol (intern minor-mode)))
896 ((member minor-mode indicators)
897 (describe-minor-mode-from-indicator minor-mode))
899 (error "No such minor mode: %s" minor-mode)))))
901 ;; symbol
902 (defun describe-minor-mode-completion-table-for-symbol ()
903 ;; In order to list up all minor modes, minor-mode-list
904 ;; is used here instead of minor-mode-alist.
905 (delq nil (mapcar 'symbol-name minor-mode-list)))
907 (defun describe-minor-mode-from-symbol (symbol)
908 "Display documentation of a minor mode given as a symbol, SYMBOL"
909 (interactive (list (intern (completing-read
910 "Minor mode symbol: "
911 (describe-minor-mode-completion-table-for-symbol)))))
912 (if (fboundp symbol)
913 (describe-function symbol)
914 (describe-variable symbol)))
916 ;; indicator
917 (defun describe-minor-mode-completion-table-for-indicator ()
918 (delq nil
919 (mapcar (lambda (x)
920 (let ((i (format-mode-line x)))
921 ;; remove first space if existed
922 (cond
923 ((= 0 (length i))
924 nil)
925 ((eq (aref i 0) ?\s)
926 (substring i 1))
928 i))))
929 minor-mode-alist)))
931 (defun describe-minor-mode-from-indicator (indicator)
932 "Display documentation of a minor mode specified by INDICATOR.
933 If you call this function interactively, you can give indicator which
934 is currently activated with completion."
935 (interactive (list
936 (completing-read
937 "Minor mode indicator: "
938 (describe-minor-mode-completion-table-for-indicator))))
939 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
940 (if minor-mode
941 (describe-minor-mode-from-symbol minor-mode)
942 (error "Cannot find minor mode for `%s'" indicator))))
944 (defun lookup-minor-mode-from-indicator (indicator)
945 "Return a minor mode symbol from its indicator on the mode line."
946 ;; remove first space if existed
947 (if (and (< 0 (length indicator))
948 (eq (aref indicator 0) ?\s))
949 (setq indicator (substring indicator 1)))
950 (let ((minor-modes minor-mode-alist)
951 result)
952 (while minor-modes
953 (let* ((minor-mode (car (car minor-modes)))
954 (anindicator (format-mode-line
955 (car (cdr (car minor-modes))))))
956 ;; remove first space if existed
957 (if (and (stringp anindicator)
958 (> (length anindicator) 0)
959 (eq (aref anindicator 0) ?\s))
960 (setq anindicator (substring anindicator 1)))
961 (if (equal indicator anindicator)
962 (setq result minor-mode
963 minor-modes nil)
964 (setq minor-modes (cdr minor-modes)))))
965 result))
967 ;;; Automatic resizing of temporary buffers.
968 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
969 "Maximum height of a window displaying a temporary buffer.
970 This is effective only when Temp Buffer Resize mode is enabled.
971 The value is the maximum height (in lines) which
972 `resize-temp-buffer-window' will give to a window displaying a
973 temporary buffer. It can also be a function to be called to
974 choose the height for such a buffer. It gets one argument, the
975 buffer, and should return a positive integer. At the time the
976 function is called, the window to be resized is selected."
977 :type '(choice integer function)
978 :group 'help
979 :version "20.4")
981 (define-minor-mode temp-buffer-resize-mode
982 "Toggle auto-shrinking temp buffer windows (Temp Buffer Resize mode).
983 With a prefix argument ARG, enable Temp Buffer Resize mode if ARG
984 is positive, and disable it otherwise. If called from Lisp,
985 enable the mode if ARG is omitted or nil.
987 When Temp Buffer Resize mode is enabled, the windows in which we
988 show a temporary buffer are automatically reduced in height to
989 fit the buffer's contents, but never more than
990 `temp-buffer-max-height' nor less than `window-min-height'.
992 This mode is used by `help', `apropos' and `completion' buffers,
993 and some others."
994 :global t :group 'help
995 (if temp-buffer-resize-mode
996 ;; `help-make-xrefs' may add a `back' button and thus increase the
997 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
998 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
999 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
1001 (defun resize-temp-buffer-window ()
1002 "Resize the selected window to fit its contents.
1003 Will not make it higher than `temp-buffer-max-height' nor smaller
1004 than `window-min-height'. Do nothing if the selected window is
1005 not vertically combined or some of its contents are scrolled out
1006 of view."
1007 (when (and (pos-visible-in-window-p (point-min))
1008 (window-combined-p))
1009 (fit-window-to-buffer
1011 (if (functionp temp-buffer-max-height)
1012 (funcall temp-buffer-max-height (window-buffer))
1013 temp-buffer-max-height))))
1015 ;;; Help windows.
1016 (defcustom help-window-select 'other
1017 "Non-nil means select help window for viewing.
1018 Choices are:
1019 never (nil) Select help window only if there is no other window
1020 on its frame.
1021 other Select help window unless the selected window is the
1022 only other window on the help window's frame.
1023 always (t) Always select the help window.
1025 This option has effect if and only if the help window was created
1026 by `with-help-window'"
1027 :type '(choice (const :tag "never (nil)" nil)
1028 (const :tag "other" other)
1029 (const :tag "always (t)" t))
1030 :group 'help
1031 :version "23.1")
1033 (defcustom help-enable-auto-load t
1034 "Whether Help commands can perform autoloading.
1035 If non-nil, whenever \\[describe-function] is called for an
1036 autoloaded function whose docstring contains any key substitution
1037 construct (see `substitute-command-keys'), the library is loaded,
1038 so that the documentation can show the right key bindings."
1039 :type 'boolean
1040 :group 'help
1041 :version "24.3")
1043 (defun help-window-display-message (quit-part window &optional scroll)
1044 "Display message telling how to quit and scroll help window.
1045 QUIT-PART is a string telling how to quit the help window WINDOW.
1046 Optional argument SCROLL non-nil means tell how to scroll WINDOW.
1047 SCROLL equal `other' means tell how to scroll the \"other\"
1048 window."
1049 (let ((scroll-part
1050 (cond
1051 ;; If we don't have QUIT-PART we probably reuse a window
1052 ;; showing the same buffer so we don't show any message.
1053 ((not quit-part) nil)
1054 ((pos-visible-in-window-p
1055 (with-current-buffer (window-buffer window)
1056 (point-max)) window t)
1057 ;; Buffer end is at least partially visible, no need to talk
1058 ;; about scrolling.
1059 ".")
1060 ((eq scroll 'other)
1061 ", \\[scroll-other-window] to scroll help.")
1062 (scroll ", \\[scroll-up] to scroll help."))))
1063 (message "%s"
1064 (substitute-command-keys (concat quit-part scroll-part)))))
1066 (defun help-window-setup (help-window)
1067 "Set up help window for `with-help-window'.
1068 HELP-WINDOW is the window used for displaying the help buffer."
1069 (let* ((help-buffer (when (window-live-p help-window)
1070 (window-buffer help-window)))
1071 (help-setup (when (window-live-p help-window)
1072 (car (window-parameter help-window 'quit-restore)))))
1073 (when help-buffer
1074 ;; Handle `help-window-point-marker'.
1075 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1076 (set-window-point help-window help-window-point-marker)
1077 ;; Reset `help-window-point-marker'.
1078 (set-marker help-window-point-marker nil))
1080 (cond
1081 ((or (eq help-window (selected-window))
1082 (and (or (eq help-window-select t)
1083 (eq help-setup 'frame)
1084 (and (eq help-window-select 'other)
1085 (eq (window-frame help-window) (selected-frame))
1086 (> (length (window-list nil 'no-mini)) 2)))
1087 (select-window help-window)))
1088 ;; The help window is or gets selected ...
1089 (help-window-display-message
1090 (cond
1091 ((eq help-setup 'window)
1092 ;; ... and is new, ...
1093 "Type \"q\" to delete help window")
1094 ((eq help-setup 'frame)
1095 "Type \"q\" to delete help frame")
1096 ((eq help-setup 'other)
1097 ;; ... or displayed some other buffer before.
1098 "Type \"q\" to restore previous buffer"))
1099 help-window t))
1100 ((and (eq (window-frame help-window) (selected-frame))
1101 (= (length (window-list nil 'no-mini)) 2))
1102 ;; There are two windows on the help window's frame and the
1103 ;; other one is the selected one.
1104 (help-window-display-message
1105 (cond
1106 ((eq help-setup 'window)
1107 "Type \\[delete-other-windows] to delete the help window")
1108 ((eq help-setup 'other)
1109 "Type \"q\" in help window to restore its previous buffer"))
1110 help-window 'other))
1112 ;; The help window is not selected ...
1113 (help-window-display-message
1114 (cond
1115 ((eq help-setup 'window)
1116 ;; ... and is new, ...
1117 "Type \"q\" in help window to delete it")
1118 ((eq help-setup 'other)
1119 ;; ... or displayed some other buffer before.
1120 "Type \"q\" in help window to restore previous buffer"))
1121 help-window))))))
1123 ;; `with-help-window' is a wrapper for `with-output-to-temp-buffer'
1124 ;; providing the following additional twists:
1126 ;; (1) Issue more accurate messages telling how to scroll and quit the
1127 ;; help window.
1129 ;; (2) An option (customizable via `help-window-select') to select the
1130 ;; help window automatically.
1132 ;; (3) A marker (`help-window-point-marker') to move point in the help
1133 ;; window to an arbitrary buffer position.
1135 ;; Note: It's usually always wrong to use `help-print-return-message' in
1136 ;; the body of `with-help-window'.
1137 (defmacro with-help-window (buffer-name &rest body)
1138 "Display buffer with name BUFFER-NAME in a help window evaluating BODY.
1139 Select help window if the actual value of the user option
1140 `help-window-select' says so. Return last value in BODY."
1141 (declare (indent 1) (debug t))
1142 `(progn
1143 ;; Make `help-window-point-marker' point nowhere. The only place
1144 ;; where this should be set to a buffer position is within BODY.
1145 (set-marker help-window-point-marker nil)
1146 (let* (help-window
1147 (temp-buffer-show-hook
1148 (cons (lambda () (setq help-window (selected-window)))
1149 temp-buffer-show-hook)))
1150 ;; Return value returned by `with-output-to-temp-buffer'.
1151 (prog1
1152 (with-output-to-temp-buffer ,buffer-name
1153 (progn ,@body))
1154 (help-window-setup help-window)))))
1156 ;; Called from C, on encountering `help-char' when reading a char.
1157 ;; Don't print to *Help*; that would clobber Help history.
1158 (defun help-form-show ()
1159 "Display the output of a non-nil `help-form'."
1160 (let ((msg (eval help-form)))
1161 (if (stringp msg)
1162 (with-output-to-temp-buffer " *Char Help*"
1163 (princ msg)))))
1165 (provide 'help)
1167 ;;; help.el ends here