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