(while-no-input): Don't splice BODY directly into the `or' form.
[emacs.git] / lisp / help.el
blob3b35fadf781f2391b344af6ad923ce340272e2bf
1 ;;; help.el --- help commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: help, internal
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, or (at your option)
14 ;; 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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; This code implements GNU Emacs' on-line help system, the one invoked by
29 ;; `M-x help-for-help'.
31 ;;; Code:
33 ;; Get the macro make-help-screen when this is compiled,
34 ;; or run interpreted, but not when the compiled code is loaded.
35 (eval-when-compile (require 'help-macro))
37 ;; This makes `with-output-to-temp-buffer' buffers use `help-mode'.
38 (add-hook 'temp-buffer-setup-hook 'help-mode-setup)
39 (add-hook 'temp-buffer-show-hook 'help-mode-finish)
41 (defvar help-map
42 (let ((map (make-sparse-keymap)))
43 (define-key map (char-to-string help-char) 'help-for-help)
44 (define-key map [help] 'help-for-help)
45 (define-key map [f1] 'help-for-help)
46 (define-key map "." 'display-local-help)
47 (define-key map "?" 'help-for-help)
49 (define-key map "\C-a" 'about-emacs)
50 (define-key map "\C-c" 'describe-copying)
51 (define-key map "\C-d" 'describe-distribution)
52 (define-key map "\C-e" 'view-emacs-problems)
53 (define-key map "\C-f" 'view-emacs-FAQ)
54 (define-key map "\C-m" 'view-order-manuals)
55 (define-key map "\C-n" 'view-emacs-news)
56 (define-key map "\C-p" 'describe-gnu-project)
57 (define-key map "\C-t" 'view-emacs-todo)
58 (define-key map "\C-w" 'describe-no-warranty)
60 ;; This does not fit the pattern, but it is natural given the C-\ command.
61 (define-key map "\C-\\" 'describe-input-method)
63 (define-key map "C" 'describe-coding-system)
64 (define-key map "F" 'Info-goto-emacs-command-node)
65 (define-key map "I" 'describe-input-method)
66 (define-key map "K" 'Info-goto-emacs-key-command-node)
67 (define-key map "L" 'describe-language-environment)
68 (define-key map "S" 'info-lookup-symbol)
70 (define-key map "a" 'apropos-command)
71 (define-key map "b" 'describe-bindings)
72 (define-key map "c" 'describe-key-briefly)
73 (define-key map "d" 'apropos-documentation)
74 (define-key map "e" 'view-echo-area-messages)
75 (define-key map "f" 'describe-function)
76 (define-key map "h" 'view-hello-file)
78 (define-key map "i" 'info)
79 (define-key map "4i" 'info-other-window)
81 (define-key map "k" 'describe-key)
82 (define-key map "l" 'view-lossage)
83 (define-key map "m" 'describe-mode)
84 (define-key map "n" 'view-emacs-news)
85 (define-key map "p" 'finder-by-keyword)
86 (define-key map "r" 'info-emacs-manual)
87 (define-key map "s" 'describe-syntax)
88 (define-key map "t" 'help-with-tutorial)
89 (define-key map "w" 'where-is)
90 (define-key map "v" 'describe-variable)
91 (define-key map "q" 'help-quit)
92 map)
93 "Keymap for characters following the Help key.")
95 (define-key global-map (char-to-string help-char) 'help-command)
96 (define-key global-map [help] 'help-command)
97 (define-key global-map [f1] 'help-command)
98 (fset 'help-command help-map)
100 (autoload 'finder-by-keyword "finder"
101 "Find packages matching a given keyword." t)
103 ;; insert-button makes the action nil if it is not store somewhere
104 (defvar help-button-cache nil)
107 (defun help-quit ()
108 "Just exit from the Help command's command loop."
109 (interactive)
110 nil)
112 (defvar help-return-method nil
113 "What to do to \"exit\" the help buffer.
114 This is a list
115 (WINDOW . t) delete the selected window (and possibly its frame,
116 see `quit-window' and `View-quit'), go to WINDOW.
117 (WINDOW . quit-window) do quit-window, then select WINDOW.
118 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
120 (defun print-help-return-message (&optional function)
121 "Display or return message saying how to restore windows after help command.
122 This function assumes that `standard-output' is the help buffer.
123 It computes a message, and applies the optional argument FUNCTION to it.
124 If FUNCTION is nil, it applies `message', thus displaying the message.
125 In addition, this function sets up `help-return-method', which see, that
126 specifies what to do when the user exits the help buffer."
127 (and (not (get-buffer-window standard-output))
128 (let ((first-message
129 (cond ((or
130 pop-up-frames
131 (special-display-p (buffer-name standard-output)))
132 (setq help-return-method (cons (selected-window) t))
133 ;; If the help output buffer is a special display buffer,
134 ;; don't say anything about how to get rid of it.
135 ;; First of all, the user will do that with the window
136 ;; manager, not with Emacs.
137 ;; Secondly, the buffer has not been displayed yet,
138 ;; so we don't know whether its frame will be selected.
139 nil)
140 (display-buffer-reuse-frames
141 (setq help-return-method (cons (selected-window)
142 'quit-window))
143 nil)
144 ((not (one-window-p t))
145 (setq help-return-method
146 (cons (selected-window) 'quit-window))
147 "Type \\[display-buffer] RET to restore the other window.")
148 (pop-up-windows
149 (setq help-return-method (cons (selected-window) t))
150 "Type \\[delete-other-windows] to remove help window.")
152 (setq help-return-method
153 (list (selected-window) (window-buffer)
154 (window-start) (window-point)))
155 "Type \\[switch-to-buffer] RET to remove help window."))))
156 (funcall (or function 'message)
157 (concat
158 (if first-message
159 (substitute-command-keys first-message))
160 (if first-message " ")
161 ;; If the help buffer will go in a separate frame,
162 ;; it's no use mentioning a command to scroll, so don't.
163 (if (or pop-up-windows
164 (special-display-p (buffer-name standard-output)))
166 (if (same-window-p (buffer-name standard-output))
167 ;; Say how to scroll this window.
168 (substitute-command-keys
169 "\\[scroll-up] to scroll the help.")
170 ;; Say how to scroll some other window.
171 (substitute-command-keys
172 "\\[scroll-other-window] to scroll the help."))))))))
174 ;; So keyboard macro definitions are documented correctly
175 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
177 (defalias 'help 'help-for-help-internal)
178 ;; find-function can find this.
179 (defalias 'help-for-help 'help-for-help-internal)
180 ;; It can't find this, but nobody will look.
181 (make-help-screen help-for-help-internal
182 "a b c C e f F i I k C-k l L m p r s t v w C-c C-d C-f C-n C-p C-t C-w . or ? :"
183 "You have typed %THIS-KEY%, the help character. Type a Help option:
184 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
186 a command-apropos. Type a list of words or a regexp; it shows a list of
187 commands whose names match. See also the apropos command.
188 b describe-bindings. Display a table of all key bindings.
189 c describe-key-briefly. Type a key sequence;
190 it displays the command name run by that key sequence.
191 C describe-coding-system. Type the name of the coding system to describe,
192 or just RET to describe the ones currently in use.
193 d apropos-documentation. Type a pattern (a list of words or a regexp), and
194 it shows a list of functions, variables, and other items whose
195 documentation matches that pattern. See also the apropos command.
196 e view-echo-area-messages. Go to the buffer that logs echo-area messages.
197 f describe-function. Type a function name and you see its documentation.
198 F Info-goto-emacs-command-node. Type a command name;
199 it goes to the on-line manual's section that describes the command.
200 h Display the HELLO file which illustrates various scripts.
201 i info. The Info documentation reader: read on-line manuals.
202 I describe-input-method. Describe a specific input method (if you type
203 its name) or the current input method (if you type just RET).
204 k describe-key. Type a key sequence;
205 it displays the full documentation for that key sequence.
206 K Info-goto-emacs-key-command-node. Type a key sequence;
207 it goes to the on-line manual's section that describes
208 the command bound to that key.
209 l view-lossage. Show last 100 characters you typed.
210 L describe-language-environment. This describes either a
211 specific language environment (if you type its name)
212 or the current language environment (if you type just RET).
213 m describe-mode. Display documentation of current minor modes,
214 and the current major mode, including their special commands.
215 n view-emacs-news. Display news of recent Emacs changes.
216 p finder-by-keyword. Find packages matching a given topic keyword.
217 r info-emacs-manual. Display the Emacs manual in Info mode.
218 s describe-syntax. Display contents of syntax table, plus explanations.
219 S info-lookup-symbol. Type a symbol; it goes to that symbol in the
220 on-line manual for the programming language used in this buffer.
221 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
222 v describe-variable. Type name of a variable;
223 it displays the variable's documentation and value.
224 w where-is. Type a command name; it displays which keystrokes
225 invoke that command.
226 . display-local-help. Display any available local help at point
227 in the echo area.
229 C-a Display information about Emacs.
230 C-c Display Emacs copying permission (GNU General Public License).
231 C-d Display Emacs ordering information.
232 C-e Display info about Emacs problems.
233 C-f Display the Emacs FAQ.
234 C-m Display how to order printed Emacs manuals.
235 C-n Display news of recent Emacs changes.
236 C-p Display information about the GNU project.
237 C-t Display the Emacs TODO list.
238 C-w Display 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 (or (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 describe-distribution ()
285 "Display info on how to obtain the latest version of GNU Emacs."
286 (interactive)
287 (view-file (expand-file-name "DISTRIB" data-directory)))
289 (defun describe-copying ()
290 "Display info on how you may redistribute copies of GNU Emacs."
291 (interactive)
292 (view-file (expand-file-name "COPYING" data-directory))
293 (goto-char (point-min)))
295 (defun describe-gnu-project ()
296 "Display info on the GNU project."
297 (interactive)
298 (view-file (expand-file-name "THE-GNU-PROJECT" data-directory))
299 (goto-char (point-min)))
301 (define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2")
303 (defun describe-no-warranty ()
304 "Display info on all the kinds of warranty Emacs does NOT have."
305 (interactive)
306 (describe-copying)
307 (let (case-fold-search)
308 (search-forward "NO WARRANTY")
309 (recenter 0)))
311 (defun describe-prefix-bindings ()
312 "Describe the bindings of the prefix used to reach this command.
313 The prefix described consists of all but the last event
314 of the key sequence that ran this command."
315 (interactive)
316 (let ((key (this-command-keys)))
317 (describe-bindings
318 (if (stringp key)
319 (substring key 0 (1- (length key)))
320 (let ((prefix (make-vector (1- (length key)) nil))
321 (i 0))
322 (while (< i (length prefix))
323 (aset prefix i (aref key i))
324 (setq i (1+ i)))
325 prefix)))))
326 ;; Make C-h after a prefix, when not specifically bound,
327 ;; run describe-prefix-bindings.
328 (setq prefix-help-command 'describe-prefix-bindings)
330 (defun view-emacs-news (&optional version)
331 "Display info on recent changes to Emacs.
332 With argument, display info only for the selected version."
333 (interactive "P")
334 (unless version
335 (setq version emacs-major-version))
336 (when (consp version)
337 (let* ((all-versions
338 (let (res)
339 (mapcar
340 (lambda (file)
341 (with-temp-buffer
342 (insert-file-contents
343 (expand-file-name file data-directory))
344 (while (re-search-forward
345 (if (member file '("NEWS.18" "NEWS.1-17"))
346 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
347 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
348 (setq res (cons (match-string-no-properties 1) res)))))
349 (cons "NEWS"
350 (directory-files data-directory nil
351 "^NEWS\\.[0-9][-0-9]*$" nil)))
352 (sort (delete-dups res) (lambda (a b) (string< b a)))))
353 (current (car all-versions)))
354 (setq version (completing-read
355 (format "Read NEWS for the version (default %s): " current)
356 all-versions nil nil nil nil current))
357 (if (integerp (string-to-number version))
358 (setq version (string-to-number version))
359 (unless (or (member version all-versions)
360 (<= (string-to-number version) (string-to-number current)))
361 (error "No news about version %s" version)))))
362 (when (integerp version)
363 (cond ((<= version 12)
364 (setq version (format "1.%d" version)))
365 ((<= version 18)
366 (setq version (format "%d" version)))
367 ((> version emacs-major-version)
368 (error "No news about Emacs %d (yet)" version))))
369 (let* ((vn (if (stringp version)
370 (string-to-number version)
371 version))
372 (file (cond
373 ((>= vn emacs-major-version) "NEWS")
374 ((< vn 18) "NEWS.1-17")
375 (t (format "NEWS.%d" vn))))
376 res)
377 (view-file (expand-file-name file data-directory))
378 (widen)
379 (goto-char (point-min))
380 (when (stringp version)
381 (when (re-search-forward
382 (concat (if (< vn 19)
383 "Changes in Emacs[ \t]*"
384 "^\* [^0-9\n]*") version "$")
385 nil t)
386 (beginning-of-line)
387 (narrow-to-region
388 (point)
389 (save-excursion
390 (while (and (setq res
391 (re-search-forward
392 (if (< vn 19)
393 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
394 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
395 (equal (match-string-no-properties 1) version)))
396 (or res (goto-char (point-max)))
397 (beginning-of-line)
398 (point)))))))
401 (defun view-emacs-todo (&optional arg)
402 "Display the Emacs TODO list."
403 (interactive "P")
404 (view-file (expand-file-name "TODO" data-directory)))
406 (define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2")
409 (defun view-echo-area-messages ()
410 "View the log of recent echo-area messages: the `*Messages*' buffer.
411 The number of messages retained in that buffer
412 is specified by the variable `message-log-max'."
413 (interactive)
414 (switch-to-buffer (get-buffer-create "*Messages*")))
416 (defun view-order-manuals ()
417 "Display the Emacs ORDERS file."
418 (interactive)
419 (view-file (expand-file-name "ORDERS" data-directory))
420 (goto-address))
422 (defun view-emacs-FAQ ()
423 "Display the Emacs Frequently Asked Questions (FAQ) file."
424 (interactive)
425 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
426 (info "(efaq)"))
428 (defun view-emacs-problems ()
429 "Display info on known problems with Emacs and possible workarounds."
430 (interactive)
431 (view-file (expand-file-name "PROBLEMS" data-directory)))
433 (defun view-lossage ()
434 "Display last 100 input keystrokes.
436 To record all your input on a file, use `open-dribble-file'."
437 (interactive)
438 (help-setup-xref (list #'view-lossage) (interactive-p))
439 (with-output-to-temp-buffer (help-buffer)
440 (princ (mapconcat (lambda (key)
441 (if (or (integerp key) (symbolp key) (listp key))
442 (single-key-description key)
443 (prin1-to-string key nil)))
444 (recent-keys)
445 " "))
446 (with-current-buffer standard-output
447 (goto-char (point-min))
448 (while (progn (move-to-column 50) (not (eobp)))
449 (when (search-forward " " nil t)
450 (delete-char -1))
451 (insert "\n")))
452 (print-help-return-message)))
455 ;; Key bindings
457 (defun describe-bindings (&optional prefix buffer)
458 "Show a list of all defined keys, and their definitions.
459 We put that list in a buffer, and display the buffer.
461 The optional argument PREFIX, if non-nil, should be a key sequence;
462 then we display only bindings that start with that prefix.
463 The optional argument BUFFER specifies which buffer's bindings
464 to display (default, the current buffer). BUFFER can be a buffer
465 or a buffer name."
466 (interactive)
467 (or buffer (setq buffer (current-buffer)))
468 (help-setup-xref (list #'describe-bindings prefix buffer) (interactive-p))
469 (with-current-buffer buffer
470 (describe-bindings-internal nil prefix)))
472 ;; This function used to be in keymap.c.
473 (defun describe-bindings-internal (&optional menus prefix)
474 "Show a list of all defined keys, and their definitions.
475 We put that list in a buffer, and display the buffer.
477 The optional argument MENUS, if non-nil, says to mention menu bindings.
478 \(Ordinarily these are omitted from the output.)
479 The optional argument PREFIX, if non-nil, should be a key sequence;
480 then we display only bindings that start with that prefix."
481 (interactive)
482 (let ((buf (current-buffer)))
483 (with-output-to-temp-buffer "*Help*"
484 (with-current-buffer standard-output
485 (describe-buffer-bindings buf prefix menus)))))
487 (defun where-is (definition &optional insert)
488 "Print message listing key sequences that invoke the command DEFINITION.
489 Argument is a command definition, usually a symbol with a function definition.
490 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
491 (interactive
492 (let ((fn (function-called-at-point))
493 (enable-recursive-minibuffers t)
494 val)
495 (setq val (completing-read
496 (if fn
497 (format "Where is command (default %s): " fn)
498 "Where is command: ")
499 obarray 'commandp t))
500 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
501 (unless definition (error "No command"))
502 (let ((func (indirect-function definition))
503 (defs nil)
504 (standard-output (if insert (current-buffer) t)))
505 ;; In DEFS, find all symbols that are aliases for DEFINITION.
506 (mapatoms (lambda (symbol)
507 (and (fboundp symbol)
508 (not (eq symbol definition))
509 (eq func (condition-case ()
510 (indirect-function symbol)
511 (error symbol)))
512 (push symbol defs))))
513 ;; Look at all the symbols--first DEFINITION,
514 ;; then its aliases.
515 (dolist (symbol (cons definition defs))
516 (let* ((remapped (command-remapping symbol))
517 (keys (where-is-internal
518 symbol overriding-local-map nil nil remapped))
519 (keys (mapconcat 'key-description keys ", "))
520 string)
521 (setq string
522 (if insert
523 (if (> (length keys) 0)
524 (if remapped
525 (format "%s (%s) (remapped from %s)"
526 keys remapped symbol)
527 (format "%s (%s)" keys symbol))
528 (format "M-x %s RET" symbol))
529 (if (> (length keys) 0)
530 (if remapped
531 (format "%s is remapped to %s which is on %s"
532 symbol remapped keys)
533 (format "%s is on %s" symbol keys))
534 ;; If this is the command the user asked about,
535 ;; and it is not on any key, say so.
536 ;; For other symbols, its aliases, say nothing
537 ;; about them unless they are on keys.
538 (if (eq symbol definition)
539 (format "%s is not on any key" symbol)))))
540 (when string
541 (unless (eq symbol definition)
542 (princ ";\n its alias "))
543 (princ string)))))
544 nil)
546 (defun help-key-description (key untranslated)
547 (let ((string (key-description key)))
548 (if (or (not untranslated)
549 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
550 string
551 (let ((otherstring (key-description untranslated)))
552 (if (equal string otherstring)
553 string
554 (format "%s (translated from %s)" string otherstring))))))
556 (defun describe-key-briefly (&optional key insert untranslated)
557 "Print the name of the function KEY invokes. KEY is a string.
558 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
559 If non-nil, UNTRANSLATED is a vector of the untranslated events.
560 It can also be a number in which case the untranslated events from
561 the last key hit are used.
563 If KEY is a menu item or a tool-bar button that is disabled, this command
564 temporarily enables it to allow getting help on disabled items and buttons."
565 (interactive
566 (let ((enable-disabled-menus-and-buttons t)
567 (cursor-in-echo-area t)
568 saved-yank-menu)
569 (unwind-protect
570 (let (key)
571 ;; If yank-menu is empty, populate it temporarily, so that
572 ;; "Select and Paste" menu can generate a complete event.
573 (when (null (cdr yank-menu))
574 (setq saved-yank-menu (copy-sequence yank-menu))
575 (menu-bar-update-yank-menu "(any string)" nil))
576 (setq key (read-key-sequence "Describe key (or click or menu item): "))
577 ;; If KEY is a down-event, read and discard the
578 ;; corresponding up-event. Note that there are also
579 ;; down-events on scroll bars and mode lines: the actual
580 ;; event then is in the second element of the vector.
581 (and (vectorp key)
582 (let ((last-idx (1- (length key))))
583 (and (eventp (aref key last-idx))
584 (memq 'down (event-modifiers (aref key last-idx)))))
585 (read-event))
586 (list
588 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
590 ;; Put yank-menu back as it was, if we changed it.
591 (when saved-yank-menu
592 (setq yank-menu (copy-sequence saved-yank-menu))
593 (fset 'yank-menu (cons 'keymap yank-menu))))))
594 (if (numberp untranslated)
595 (setq untranslated (this-single-command-raw-keys)))
596 (let* ((event (if (and (symbolp (aref key 0))
597 (> (length key) 1)
598 (consp (aref key 1)))
599 (aref key 1)
600 (aref key 0)))
601 (modifiers (event-modifiers event))
602 (standard-output (if insert (current-buffer) t))
603 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
604 (memq 'drag modifiers)) " at that spot" ""))
605 (defn (key-binding key t))
606 key-desc)
607 ;; Handle the case where we faked an entry in "Select and Paste" menu.
608 (if (and (eq defn nil)
609 (stringp (aref key (1- (length key))))
610 (eq (key-binding (substring key 0 -1)) 'yank-menu))
611 (setq defn 'menu-bar-select-yank))
612 ;; Don't bother user with strings from (e.g.) the select-paste menu.
613 (if (stringp (aref key (1- (length key))))
614 (aset key (1- (length key)) "(any string)"))
615 (if (and (> (length untranslated) 0)
616 (stringp (aref untranslated (1- (length untranslated)))))
617 (aset untranslated (1- (length untranslated)) "(any string)"))
618 ;; Now describe the key, perhaps as changed.
619 (setq key-desc (help-key-description key untranslated))
620 (if (or (null defn) (integerp defn) (equal defn 'undefined))
621 (princ (format "%s%s is undefined" key-desc mouse-msg))
622 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
624 (defun describe-key (&optional key untranslated up-event)
625 "Display documentation of the function invoked by KEY.
626 KEY can be any kind of a key sequence; it can include keyboard events,
627 mouse events, and/or menu events. When calling from a program,
628 pass KEY as a string or a vector.
630 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
631 It can also be a number, in which case the untranslated events from
632 the last key sequence entered are used.
633 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
635 If KEY is a menu item or a tool-bar button that is disabled, this command
636 temporarily enables it to allow getting help on disabled items and buttons."
637 (interactive
638 (let ((enable-disabled-menus-and-buttons t)
639 (cursor-in-echo-area t)
640 saved-yank-menu)
641 (unwind-protect
642 (let (key)
643 ;; If yank-menu is empty, populate it temporarily, so that
644 ;; "Select and Paste" menu can generate a complete event.
645 (when (null (cdr yank-menu))
646 (setq saved-yank-menu (copy-sequence yank-menu))
647 (menu-bar-update-yank-menu "(any string)" nil))
648 (setq key (read-key-sequence "Describe key (or click or menu item): "))
649 (list
651 (prefix-numeric-value current-prefix-arg)
652 ;; If KEY is a down-event, read and include the
653 ;; corresponding up-event. Note that there are also
654 ;; down-events on scroll bars and mode lines: the actual
655 ;; event then is in the second element of the vector.
656 (and (vectorp key)
657 (let ((last-idx (1- (length key))))
658 (and (eventp (aref key last-idx))
659 (memq 'down (event-modifiers (aref key last-idx)))))
660 (or (and (eventp (aref key 0))
661 (memq 'down (event-modifiers (aref key 0)))
662 ;; However, for the C-down-mouse-2 popup
663 ;; menu, there is no subsequent up-event. In
664 ;; this case, the up-event is the next
665 ;; element in the supplied vector.
666 (= (length key) 1))
667 (and (> (length key) 1)
668 (eventp (aref key 1))
669 (memq 'down (event-modifiers (aref key 1)))))
670 (read-event))))
671 ;; Put yank-menu back as it was, if we changed it.
672 (when saved-yank-menu
673 (setq yank-menu (copy-sequence saved-yank-menu))
674 (fset 'yank-menu (cons 'keymap yank-menu))))))
675 (if (numberp untranslated)
676 (setq untranslated (this-single-command-raw-keys)))
677 (let* ((event (aref key (if (and (symbolp (aref key 0))
678 (> (length key) 1)
679 (consp (aref key 1)))
681 0)))
682 (modifiers (event-modifiers event))
683 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
684 (memq 'drag modifiers)) " at that spot" ""))
685 (defn (key-binding key t))
686 defn-up defn-up-tricky ev-type
687 mouse-1-remapped mouse-1-tricky)
689 ;; Handle the case where we faked an entry in "Select and Paste" menu.
690 (when (and (eq defn nil)
691 (stringp (aref key (1- (length key))))
692 (eq (key-binding (substring key 0 -1)) 'yank-menu))
693 (setq defn 'menu-bar-select-yank))
694 (if (or (null defn) (integerp defn) (equal defn 'undefined))
695 (message "%s%s is undefined"
696 (help-key-description key untranslated) mouse-msg)
697 (help-setup-xref (list #'describe-function defn) (interactive-p))
698 ;; Don't bother user with strings from (e.g.) the select-paste menu.
699 (when (stringp (aref key (1- (length key))))
700 (aset key (1- (length key)) "(any string)"))
701 (when (and untranslated
702 (stringp (aref untranslated (1- (length untranslated)))))
703 (aset untranslated (1- (length untranslated))
704 "(any string)"))
705 ;; Need to do this before erasing *Help* buffer in case event
706 ;; is a mouse click in an existing *Help* buffer.
707 (when up-event
708 (setq ev-type (event-basic-type up-event))
709 (let ((sequence (vector up-event)))
710 (when (and (eq ev-type 'mouse-1)
711 mouse-1-click-follows-link
712 (not (eq mouse-1-click-follows-link 'double))
713 (setq mouse-1-remapped
714 (mouse-on-link-p (event-start up-event))))
715 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
716 (> mouse-1-click-follows-link 0)))
717 (cond ((stringp mouse-1-remapped)
718 (setq sequence mouse-1-remapped))
719 ((vectorp mouse-1-remapped)
720 (setcar up-event (elt mouse-1-remapped 0)))
721 (t (setcar up-event 'mouse-2))))
722 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
723 (when mouse-1-tricky
724 (setq sequence (vector up-event))
725 (aset sequence 0 'mouse-1)
726 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
727 (with-output-to-temp-buffer (help-buffer)
728 (princ (help-key-description key untranslated))
729 (princ (format "\
730 %s runs the command %S
731 which is "
732 mouse-msg defn))
733 (describe-function-1 defn)
734 (when up-event
735 (unless (or (null defn-up)
736 (integerp defn-up)
737 (equal defn-up 'undefined))
738 (princ (format "
740 ----------------- up-event %s----------------
742 <%S>%s%s runs the command %S
743 which is "
744 (if mouse-1-tricky "(short click) " "")
745 ev-type mouse-msg
746 (if mouse-1-remapped
747 " is remapped to <mouse-2>\nwhich" "")
748 defn-up))
749 (describe-function-1 defn-up))
750 (unless (or (null defn-up-tricky)
751 (integerp defn-up-tricky)
752 (eq defn-up-tricky 'undefined))
753 (princ (format "
755 ----------------- up-event (long click) ----------------
757 Pressing <%S>%s for longer than %d milli-seconds
758 runs the command %S
759 which is "
760 ev-type mouse-msg
761 mouse-1-click-follows-link
762 defn-up-tricky))
763 (describe-function-1 defn-up-tricky)))
764 (print-help-return-message)))))
766 (defun describe-mode (&optional buffer)
767 "Display documentation of current major mode and minor modes.
768 A brief summary of the minor modes comes first, followed by the
769 major mode description. This is followed by detailed
770 descriptions of the minor modes, each on a separate page.
772 For this to work correctly for a minor mode, the mode's indicator
773 variable \(listed in `minor-mode-alist') must also be a function
774 whose documentation describes the minor mode."
775 (interactive "@")
776 (unless buffer (setq buffer (current-buffer)))
777 (help-setup-xref (list #'describe-mode buffer)
778 (interactive-p))
779 ;; For the sake of help-do-xref and help-xref-go-back,
780 ;; don't switch buffers before calling `help-buffer'.
781 (with-output-to-temp-buffer (help-buffer)
782 (with-current-buffer buffer
783 (let (minor-modes)
784 ;; Older packages do not register in minor-mode-list but only in
785 ;; minor-mode-alist.
786 (dolist (x minor-mode-alist)
787 (setq x (car x))
788 (unless (memq x minor-mode-list)
789 (push x minor-mode-list)))
790 ;; Find enabled minor mode we will want to mention.
791 (dolist (mode minor-mode-list)
792 ;; Document a minor mode if it is listed in minor-mode-alist,
793 ;; non-nil, and has a function definition.
794 (let ((fmode (or (get mode :minor-mode-function) mode)))
795 (and (boundp mode) (symbol-value mode)
796 (fboundp fmode)
797 (let ((pretty-minor-mode
798 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
799 (symbol-name fmode))
800 (capitalize
801 (substring (symbol-name fmode)
802 0 (match-beginning 0)))
803 fmode)))
804 (push (list fmode pretty-minor-mode
805 (format-mode-line (assq mode minor-mode-alist)))
806 minor-modes)))))
807 (setq minor-modes
808 (sort minor-modes
809 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
810 (when minor-modes
811 (princ "Enabled minor modes:\n")
812 (make-local-variable 'help-button-cache)
813 (with-current-buffer standard-output
814 (dolist (mode minor-modes)
815 (let ((mode-function (nth 0 mode))
816 (pretty-minor-mode (nth 1 mode))
817 (indicator (nth 2 mode)))
818 (add-text-properties 0 (length pretty-minor-mode)
819 '(face bold) pretty-minor-mode)
820 (save-excursion
821 (goto-char (point-max))
822 (princ "\n\f\n")
823 (push (point-marker) help-button-cache)
824 ;; Document the minor modes fully.
825 (insert pretty-minor-mode)
826 (princ (format " minor mode (%s):\n"
827 (if (zerop (length indicator))
828 "no indicator"
829 (format "indicator%s"
830 indicator))))
831 (princ (documentation mode-function)))
832 (insert-button pretty-minor-mode
833 'action (car help-button-cache)
834 'follow-link t
835 'help-echo "mouse-2, RET: show full information")
836 (newline)))
837 (forward-line -1)
838 (fill-paragraph nil)
839 (forward-line 1))
841 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
842 ;; Document the major mode.
843 (let ((mode mode-name))
844 (with-current-buffer standard-output
845 (let ((start (point)))
846 (insert (format-mode-line mode))
847 (add-text-properties start (point) '(face bold)))))
848 (princ " mode:\n")
849 (princ (documentation major-mode)))
850 (print-help-return-message))))
853 (defun describe-minor-mode (minor-mode)
854 "Display documentation of a minor mode given as MINOR-MODE.
855 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
856 appeared on the mode-line."
857 (interactive (list (completing-read
858 "Minor mode: "
859 (nconc
860 (describe-minor-mode-completion-table-for-symbol)
861 (describe-minor-mode-completion-table-for-indicator)
862 ))))
863 (if (symbolp minor-mode)
864 (setq minor-mode (symbol-name minor-mode)))
865 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
866 (indicators (describe-minor-mode-completion-table-for-indicator)))
867 (cond
868 ((member minor-mode symbols)
869 (describe-minor-mode-from-symbol (intern minor-mode)))
870 ((member minor-mode indicators)
871 (describe-minor-mode-from-indicator minor-mode))
873 (error "No such minor mode: %s" minor-mode)))))
875 ;; symbol
876 (defun describe-minor-mode-completion-table-for-symbol ()
877 ;; In order to list up all minor modes, minor-mode-list
878 ;; is used here instead of minor-mode-alist.
879 (delq nil (mapcar 'symbol-name minor-mode-list)))
880 (defun describe-minor-mode-from-symbol (symbol)
881 "Display documentation of a minor mode given as a symbol, SYMBOL"
882 (interactive (list (intern (completing-read
883 "Minor mode symbol: "
884 (describe-minor-mode-completion-table-for-symbol)))))
885 (if (fboundp symbol)
886 (describe-function symbol)
887 (describe-variable symbol)))
889 ;; indicator
890 (defun describe-minor-mode-completion-table-for-indicator ()
891 (delq nil
892 (mapcar (lambda (x)
893 (let ((i (format-mode-line x)))
894 ;; remove first space if existed
895 (cond
896 ((= 0 (length i))
897 nil)
898 ((eq (aref i 0) ?\s)
899 (substring i 1))
901 i))))
902 minor-mode-alist)))
903 (defun describe-minor-mode-from-indicator (indicator)
904 "Display documentation of a minor mode specified by INDICATOR.
905 If you call this function interactively, you can give indicator which
906 is currently activated with completion."
907 (interactive (list
908 (completing-read
909 "Minor mode indicator: "
910 (describe-minor-mode-completion-table-for-indicator))))
911 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
912 (if minor-mode
913 (describe-minor-mode-from-symbol minor-mode)
914 (error "Cannot find minor mode for `%s'" indicator))))
916 (defun lookup-minor-mode-from-indicator (indicator)
917 "Return a minor mode symbol from its indicator on the modeline."
918 ;; remove first space if existed
919 (if (and (< 0 (length indicator))
920 (eq (aref indicator 0) ?\s))
921 (setq indicator (substring indicator 1)))
922 (let ((minor-modes minor-mode-alist)
923 result)
924 (while minor-modes
925 (let* ((minor-mode (car (car minor-modes)))
926 (anindicator (format-mode-line
927 (car (cdr (car minor-modes))))))
928 ;; remove first space if existed
929 (if (and (stringp anindicator)
930 (> (length anindicator) 0)
931 (eq (aref anindicator 0) ?\s))
932 (setq anindicator (substring anindicator 1)))
933 (if (equal indicator anindicator)
934 (setq result minor-mode
935 minor-modes nil)
936 (setq minor-modes (cdr minor-modes)))))
937 result))
940 ;;; Automatic resizing of temporary buffers.
942 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
943 "Maximum height of a window displaying a temporary buffer.
944 This is effective only when Temp Buffer Resize mode is enabled.
945 The value is the maximum height (in lines) which `resize-temp-buffer-window'
946 will give to a window displaying a temporary buffer.
947 It can also be a function to be called to choose the height for such a buffer.
948 It gets one argumemt, the buffer, and should return a positive integer."
949 :type '(choice integer function)
950 :group 'help
951 :version "20.4")
953 (define-minor-mode temp-buffer-resize-mode
954 "Toggle the mode which makes windows smaller for temporary buffers.
955 With prefix argument ARG, turn the resizing of windows displaying temporary
956 buffers on if ARG is positive or off otherwise.
957 This makes the window the right height for its contents, but never
958 more than `temp-buffer-max-height' nor less than `window-min-height'.
959 This applies to `help', `apropos' and `completion' buffers, and some others."
960 :global t :group 'help
961 (if temp-buffer-resize-mode
962 ;; `help-make-xrefs' may add a `back' button and thus increase the
963 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
964 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
965 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
967 (defun resize-temp-buffer-window ()
968 "Resize the selected window to fit its contents.
969 Will not make it higher than `temp-buffer-max-height' nor smaller than
970 `window-min-height'. Do nothing if it is the only window on its frame, if it
971 is not as wide as the frame or if some of the window's contents are scrolled
972 out of view."
973 (unless (or (one-window-p 'nomini)
974 (not (pos-visible-in-window-p (point-min)))
975 (not (window-full-width-p)))
976 (fit-window-to-buffer
977 (selected-window)
978 (if (functionp temp-buffer-max-height)
979 (funcall temp-buffer-max-height (current-buffer))
980 temp-buffer-max-height))))
983 (provide 'help)
985 ;; arch-tag: cf427352-27e9-49b7-9a6f-741ebab02423
986 ;;; help.el ends here