Merge from trunk and apply standard C changes.
[emacs.git] / lisp / help.el
blob3db1aeefa29b9e1dc89ace38e281465267a4ef2e
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, 2009, 2010 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 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' 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 "r" 'info-emacs-manual)
96 (define-key map "s" 'describe-syntax)
97 (define-key map "t" 'help-with-tutorial)
98 (define-key map "w" 'where-is)
99 (define-key map "v" 'describe-variable)
100 (define-key map "q" 'help-quit)
101 map)
102 "Keymap for characters following the Help key.")
104 (define-key global-map (char-to-string help-char) 'help-command)
105 (define-key global-map [help] 'help-command)
106 (define-key global-map [f1] 'help-command)
107 (fset 'help-command help-map)
109 (autoload 'finder-by-keyword "finder"
110 "Find packages matching a given keyword." t)
112 ;; insert-button makes the action nil if it is not store somewhere
113 (defvar help-button-cache nil)
116 (defun help-quit ()
117 "Just exit from the Help command's command loop."
118 (interactive)
119 nil)
121 (defvar help-return-method nil
122 "What to do to \"exit\" the help buffer.
123 This is a list
124 (WINDOW . t) delete the selected window (and possibly its frame,
125 see `quit-window' and `View-quit'), go to WINDOW.
126 (WINDOW . quit-window) do quit-window, then select WINDOW.
127 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
129 (define-obsolete-function-alias 'print-help-return-message 'help-print-return-message "23.2")
130 (defun help-print-return-message (&optional function)
131 "Display or return message saying how to restore windows after help command.
132 This function assumes that `standard-output' is the help buffer.
133 It computes a message, and applies the optional argument FUNCTION to it.
134 If FUNCTION is nil, it applies `message', thus displaying the message.
135 In addition, this function sets up `help-return-method', which see, that
136 specifies what to do when the user exits the help buffer."
137 (and (not (get-buffer-window standard-output))
138 (let ((first-message
139 (cond ((or
140 pop-up-frames
141 (special-display-p (buffer-name standard-output)))
142 (setq help-return-method (cons (selected-window) t))
143 ;; If the help output buffer is a special display buffer,
144 ;; don't say anything about how to get rid of it.
145 ;; First of all, the user will do that with the window
146 ;; manager, not with Emacs.
147 ;; Secondly, the buffer has not been displayed yet,
148 ;; so we don't know whether its frame will be selected.
149 nil)
150 (display-buffer-reuse-frames
151 (setq help-return-method (cons (selected-window)
152 'quit-window))
153 nil)
154 ((not (one-window-p t))
155 (setq help-return-method
156 (cons (selected-window) 'quit-window))
157 "Type \\[display-buffer] RET to restore the other window.")
158 (pop-up-windows
159 (setq help-return-method (cons (selected-window) t))
160 "Type \\[delete-other-windows] to remove help window.")
162 (setq help-return-method
163 (list (selected-window) (window-buffer)
164 (window-start) (window-point)))
165 "Type \\[switch-to-buffer] RET to remove help window."))))
166 (funcall (or function 'message)
167 (concat
168 (if first-message
169 (substitute-command-keys first-message))
170 (if first-message " ")
171 ;; If the help buffer will go in a separate frame,
172 ;; it's no use mentioning a command to scroll, so don't.
173 (if (or pop-up-windows
174 (special-display-p (buffer-name standard-output)))
176 (if (same-window-p (buffer-name standard-output))
177 ;; Say how to scroll this window.
178 (substitute-command-keys
179 "\\[scroll-up] to scroll the help.")
180 ;; Say how to scroll some other window.
181 (substitute-command-keys
182 "\\[scroll-other-window] to scroll the help."))))))))
184 ;; So keyboard macro definitions are documented correctly
185 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
187 (defalias 'help 'help-for-help-internal)
188 ;; find-function can find this.
189 (defalias 'help-for-help 'help-for-help-internal)
190 ;; It can't find this, but nobody will look.
191 (make-help-screen help-for-help-internal
192 (purecopy "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?")
193 ;; Don't purecopy this one, because it's not evaluated (it's
194 ;; directly used as a docstring in a function definition, so it'll
195 ;; be moved to the DOC file anyway: no need for purecopying it).
196 "You have typed %THIS-KEY%, the help character. Type a Help option:
197 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
199 a PATTERN Show commands whose name matches the PATTERN (a list of words
200 or a regexp). See also the `apropos' command.
201 b Display all key bindings.
202 c KEYS Display the command name run by the given key sequence.
203 C CODING Describe the given coding system, or RET for current ones.
204 d PATTERN Show a list of functions, variables, and other items whose
205 documentation matches the PATTERN (a list of words or a regexp).
206 e Go to the *Messages* buffer which logs echo-area messages.
207 f FUNCTION Display documentation for the given function.
208 F COMMAND Show the on-line manual's section that describes the command.
209 g Display information about the GNU project.
210 h Display the HELLO file which illustrates various scripts.
211 i Start the Info documentation reader: read on-line manuals.
212 I METHOD Describe a specific input method, or RET for current.
213 k KEYS Display the full documentation for the key sequence.
214 K KEYS Show the on-line manual's section for the command bound to KEYS.
215 l Show last 300 input keystrokes (lossage).
216 L LANG-ENV Describes a specific language environment, or RET for current.
217 m Display documentation of current minor modes and current major mode,
218 including their special commands.
219 n Display news of recent Emacs changes.
220 p TOPIC Find packages matching a given topic keyword.
221 r Display the Emacs manual in Info mode.
222 s Display contents of current syntax table, plus explanations.
223 S SYMBOL Show the section for the given symbol in the on-line manual
224 for the programming language used in this buffer.
225 t Start the Emacs learn-by-doing tutorial.
226 v VARIABLE Display the given variable's documentation and value.
227 w COMMAND Display which keystrokes invoke the given command (where-is).
228 . Display any available local help at point in the echo area.
230 C-a Information about Emacs.
231 C-c Emacs copying permission (GNU General Public License).
232 C-d Instructions for debugging GNU Emacs.
233 C-e External packages and information about Emacs.
234 C-f Emacs FAQ.
235 C-m How to order printed Emacs manuals.
236 C-n News of recent Emacs changes.
237 C-o Emacs ordering and distribution information.
238 C-p Info about known Emacs problems.
239 C-t Emacs TODO list.
240 C-w Information on absence of warranty for GNU Emacs."
241 help-map)
245 (defun function-called-at-point ()
246 "Return a function around point or else called by the list containing point.
247 If that doesn't give a function, return nil."
248 (with-syntax-table emacs-lisp-mode-syntax-table
249 (or (condition-case ()
250 (save-excursion
251 (or (not (zerop (skip-syntax-backward "_w")))
252 (eq (char-syntax (following-char)) ?w)
253 (eq (char-syntax (following-char)) ?_)
254 (forward-sexp -1))
255 (skip-chars-forward "'")
256 (let ((obj (read (current-buffer))))
257 (and (symbolp obj) (fboundp obj) obj)))
258 (error nil))
259 (condition-case ()
260 (save-excursion
261 (save-restriction
262 (narrow-to-region (max (point-min)
263 (- (point) 1000)) (point-max))
264 ;; Move up to surrounding paren, then after the open.
265 (backward-up-list 1)
266 (forward-char 1)
267 ;; If there is space here, this is probably something
268 ;; other than a real Lisp function call, so ignore it.
269 (if (looking-at "[ \t]")
270 (error "Probably not a Lisp function call"))
271 (let ((obj (read (current-buffer))))
272 (and (symbolp obj) (fboundp obj) obj))))
273 (error nil))
274 (let* ((str (find-tag-default))
275 (sym (if str (intern-soft str))))
276 (if (and sym (fboundp sym))
278 (save-match-data
279 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
280 (setq sym (intern-soft (match-string 1 str)))
281 (and (fboundp sym) sym))))))))
284 ;;; `User' help functions
286 (defun view-help-file (file &optional dir)
287 (view-file (expand-file-name file (or dir data-directory)))
288 (goto-address-mode 1)
289 (goto-char (point-min)))
291 (defun describe-distribution ()
292 "Display info on how to obtain the latest version of GNU Emacs."
293 (interactive)
294 (view-help-file "DISTRIB"))
296 (defun describe-copying ()
297 "Display info on how you may redistribute copies of GNU Emacs."
298 (interactive)
299 (view-help-file "COPYING"))
301 (defun describe-gnu-project ()
302 "Display info on the GNU project."
303 (interactive)
304 (view-help-file "THE-GNU-PROJECT"))
306 (define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2")
308 (defun describe-no-warranty ()
309 "Display info on all the kinds of warranty Emacs does NOT have."
310 (interactive)
311 (describe-copying)
312 (let (case-fold-search)
313 (search-forward "Disclaimer of Warranty")
314 (forward-line 0)
315 (recenter 0)))
317 (defun describe-prefix-bindings ()
318 "Describe the bindings of the prefix used to reach this command.
319 The prefix described consists of all but the last event
320 of the key sequence that ran this command."
321 (interactive)
322 (let ((key (this-command-keys)))
323 (describe-bindings
324 (if (stringp key)
325 (substring key 0 (1- (length key)))
326 (let ((prefix (make-vector (1- (length key)) nil))
327 (i 0))
328 (while (< i (length prefix))
329 (aset prefix i (aref key i))
330 (setq i (1+ i)))
331 prefix)))))
332 ;; Make C-h after a prefix, when not specifically bound,
333 ;; run describe-prefix-bindings.
334 (setq prefix-help-command 'describe-prefix-bindings)
336 (defun view-emacs-news (&optional version)
337 "Display info on recent changes to Emacs.
338 With argument, display info only for the selected version."
339 (interactive "P")
340 (unless version
341 (setq version emacs-major-version))
342 (when (consp version)
343 (let* ((all-versions
344 (let (res)
345 (mapc
346 (lambda (file)
347 (with-temp-buffer
348 (insert-file-contents
349 (expand-file-name file data-directory))
350 (while (re-search-forward
351 (if (member file '("NEWS.18" "NEWS.1-17"))
352 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
353 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
354 (setq res (cons (match-string-no-properties 1) res)))))
355 (cons "NEWS"
356 (directory-files data-directory nil
357 "^NEWS\\.[0-9][-0-9]*$" nil)))
358 (sort (delete-dups res) (lambda (a b) (string< b a)))))
359 (current (car all-versions)))
360 (setq version (completing-read
361 (format "Read NEWS for the version (default %s): " current)
362 all-versions nil nil nil nil current))
363 (if (integerp (string-to-number version))
364 (setq version (string-to-number version))
365 (unless (or (member version all-versions)
366 (<= (string-to-number version) (string-to-number current)))
367 (error "No news about version %s" version)))))
368 (when (integerp version)
369 (cond ((<= version 12)
370 (setq version (format "1.%d" version)))
371 ((<= version 18)
372 (setq version (format "%d" version)))
373 ((> version emacs-major-version)
374 (error "No news about Emacs %d (yet)" version))))
375 (let* ((vn (if (stringp version)
376 (string-to-number version)
377 version))
378 (file (cond
379 ((>= vn emacs-major-version) "NEWS")
380 ((< vn 18) "NEWS.1-17")
381 (t (format "NEWS.%d" vn))))
382 res)
383 (view-file (expand-file-name file data-directory))
384 (widen)
385 (goto-char (point-min))
386 (when (stringp version)
387 (when (re-search-forward
388 (concat (if (< vn 19)
389 "Changes in Emacs[ \t]*"
390 "^\* [^0-9\n]*") version "$")
391 nil t)
392 (beginning-of-line)
393 (narrow-to-region
394 (point)
395 (save-excursion
396 (while (and (setq res
397 (re-search-forward
398 (if (< vn 19)
399 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
400 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
401 (equal (match-string-no-properties 1) version)))
402 (or res (goto-char (point-max)))
403 (beginning-of-line)
404 (point)))))))
406 (defun view-emacs-todo (&optional arg)
407 "Display the Emacs TODO list."
408 (interactive "P")
409 (view-help-file "TODO"))
411 (define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2")
414 (defun view-echo-area-messages ()
415 "View the log of recent echo-area messages: the `*Messages*' buffer.
416 The number of messages retained in that buffer
417 is specified by the variable `message-log-max'."
418 (interactive)
419 (switch-to-buffer (get-buffer-create "*Messages*")))
421 (defun view-order-manuals ()
422 "Display the Emacs ORDERS file."
423 (interactive)
424 (view-help-file "ORDERS"))
426 (defun view-emacs-FAQ ()
427 "Display the Emacs Frequently Asked Questions (FAQ) file."
428 (interactive)
429 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
430 (info "(efaq)"))
432 (defun view-emacs-problems ()
433 "Display info on known problems with Emacs and possible workarounds."
434 (interactive)
435 (view-help-file "PROBLEMS"))
437 (defun view-emacs-debugging ()
438 "Display info on how to debug Emacs problems."
439 (interactive)
440 (view-help-file "DEBUG"))
442 (defun view-external-packages ()
443 "Display external packages and information about Emacs."
444 (interactive)
445 (view-help-file "MORE.STUFF"))
447 (defun view-lossage ()
448 "Display last 300 input keystrokes.
450 To record all your input on a file, use `open-dribble-file'."
451 (interactive)
452 (help-setup-xref (list #'view-lossage)
453 (called-interactively-p 'interactive))
454 (with-help-window (help-buffer)
455 (princ (mapconcat (lambda (key)
456 (if (or (integerp key) (symbolp key) (listp key))
457 (single-key-description key)
458 (prin1-to-string key nil)))
459 (recent-keys)
460 " "))
461 (with-current-buffer standard-output
462 (goto-char (point-min))
463 (while (progn (move-to-column 50) (not (eobp)))
464 (when (search-forward " " nil t)
465 (delete-char -1))
466 (insert "\n"))
467 ;; jidanni wants to see the last keystrokes immediately.
468 (set-marker help-window-point-marker (point)))))
471 ;; Key bindings
473 (defun describe-bindings (&optional prefix buffer)
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 PREFIX, if non-nil, should be a key sequence;
478 then we display only bindings that start with that prefix.
479 The optional argument BUFFER specifies which buffer's bindings
480 to display (default, the current buffer). BUFFER can be a buffer
481 or a buffer name."
482 (interactive)
483 (or buffer (setq buffer (current-buffer)))
484 (help-setup-xref (list #'describe-bindings prefix buffer)
485 (called-interactively-p 'interactive))
486 (with-current-buffer buffer
487 (describe-bindings-internal nil prefix)))
489 ;; This function used to be in keymap.c.
490 (defun describe-bindings-internal (&optional menus prefix)
491 "Show a list of all defined keys, and their definitions.
492 We put that list in a buffer, and display the buffer.
494 The optional argument MENUS, if non-nil, says to mention menu bindings.
495 \(Ordinarily these are omitted from the output.)
496 The optional argument PREFIX, if non-nil, should be a key sequence;
497 then we display only bindings that start with that prefix."
498 (let ((buf (current-buffer)))
499 (with-help-window "*Help*"
500 (with-current-buffer standard-output
501 (describe-buffer-bindings buf prefix menus)))))
503 (defun where-is (definition &optional insert)
504 "Print message listing key sequences that invoke the command DEFINITION.
505 Argument is a command definition, usually a symbol with a function definition.
506 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
507 (interactive
508 (let ((fn (function-called-at-point))
509 (enable-recursive-minibuffers t)
510 val)
511 (setq val (completing-read
512 (if fn
513 (format "Where is command (default %s): " fn)
514 "Where is command: ")
515 obarray 'commandp t))
516 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
517 (unless definition (error "No command"))
518 (let ((func (indirect-function definition))
519 (defs nil)
520 (standard-output (if insert (current-buffer) t)))
521 ;; In DEFS, find all symbols that are aliases for DEFINITION.
522 (mapatoms (lambda (symbol)
523 (and (fboundp symbol)
524 (not (eq symbol definition))
525 (eq func (condition-case ()
526 (indirect-function symbol)
527 (error symbol)))
528 (push symbol defs))))
529 ;; Look at all the symbols--first DEFINITION,
530 ;; then its aliases.
531 (dolist (symbol (cons definition defs))
532 (let* ((remapped (command-remapping symbol))
533 (keys (where-is-internal
534 symbol overriding-local-map nil nil remapped))
535 (keys (mapconcat 'key-description keys ", "))
536 string)
537 (setq string
538 (if insert
539 (if (> (length keys) 0)
540 (if remapped
541 (format "%s (%s) (remapped from %s)"
542 keys remapped symbol)
543 (format "%s (%s)" keys symbol))
544 (format "M-x %s RET" symbol))
545 (if (> (length keys) 0)
546 (if remapped
547 (format "%s is remapped to %s which is on %s"
548 symbol remapped keys)
549 (format "%s is on %s" symbol keys))
550 ;; If this is the command the user asked about,
551 ;; and it is not on any key, say so.
552 ;; For other symbols, its aliases, say nothing
553 ;; about them unless they are on keys.
554 (if (eq symbol definition)
555 (format "%s is not on any key" symbol)))))
556 (when string
557 (unless (eq symbol definition)
558 (princ ";\n its alias "))
559 (princ string)))))
560 nil)
562 (defun help-key-description (key untranslated)
563 (let ((string (key-description key)))
564 (if (or (not untranslated)
565 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
566 string
567 (let ((otherstring (key-description untranslated)))
568 (if (equal string otherstring)
569 string
570 (format "%s (translated from %s)" string otherstring))))))
572 (defun describe-key-briefly (&optional key insert untranslated)
573 "Print the name of the function KEY invokes. KEY is a string.
574 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
575 If non-nil, UNTRANSLATED is a vector of the untranslated events.
576 It can also be a number in which case the untranslated events from
577 the last key hit are used.
579 If KEY is a menu item or a tool-bar button that is disabled, this command
580 temporarily enables it to allow getting help on disabled items and buttons."
581 (interactive
582 (let ((enable-disabled-menus-and-buttons t)
583 (cursor-in-echo-area t)
584 saved-yank-menu)
585 (unwind-protect
586 (let (key)
587 ;; If yank-menu is empty, populate it temporarily, so that
588 ;; "Select and Paste" menu can generate a complete event.
589 (when (null (cdr yank-menu))
590 (setq saved-yank-menu (copy-sequence yank-menu))
591 (menu-bar-update-yank-menu "(any string)" nil))
592 (setq key (read-key-sequence "Describe key (or click or menu item): "))
593 ;; If KEY is a down-event, read and discard the
594 ;; corresponding up-event. Note that there are also
595 ;; down-events on scroll bars and mode lines: the actual
596 ;; event then is in the second element of the vector.
597 (and (vectorp key)
598 (let ((last-idx (1- (length key))))
599 (and (eventp (aref key last-idx))
600 (memq 'down (event-modifiers (aref key last-idx)))))
601 (read-event))
602 (list
604 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
606 ;; Put yank-menu back as it was, if we changed it.
607 (when saved-yank-menu
608 (setq yank-menu (copy-sequence saved-yank-menu))
609 (fset 'yank-menu (cons 'keymap yank-menu))))))
610 (if (numberp untranslated)
611 (setq untranslated (this-single-command-raw-keys)))
612 (let* ((event (if (and (symbolp (aref key 0))
613 (> (length key) 1)
614 (consp (aref key 1)))
615 (aref key 1)
616 (aref key 0)))
617 (modifiers (event-modifiers event))
618 (standard-output (if insert (current-buffer) t))
619 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
620 (memq 'drag modifiers)) " at that spot" ""))
621 (defn (key-binding key t))
622 key-desc)
623 ;; Handle the case where we faked an entry in "Select and Paste" menu.
624 (if (and (eq defn nil)
625 (stringp (aref key (1- (length key))))
626 (eq (key-binding (substring key 0 -1)) 'yank-menu))
627 (setq defn 'menu-bar-select-yank))
628 ;; Don't bother user with strings from (e.g.) the select-paste menu.
629 (if (stringp (aref key (1- (length key))))
630 (aset key (1- (length key)) "(any string)"))
631 (if (and (> (length untranslated) 0)
632 (stringp (aref untranslated (1- (length untranslated)))))
633 (aset untranslated (1- (length untranslated)) "(any string)"))
634 ;; Now describe the key, perhaps as changed.
635 (setq key-desc (help-key-description key untranslated))
636 (if (or (null defn) (integerp defn) (equal defn 'undefined))
637 (princ (format "%s%s is undefined" key-desc mouse-msg))
638 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
640 (defun describe-key (&optional key untranslated up-event)
641 "Display documentation of the function invoked by KEY.
642 KEY can be any kind of a key sequence; it can include keyboard events,
643 mouse events, and/or menu events. When calling from a program,
644 pass KEY as a string or a vector.
646 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
647 It can also be a number, in which case the untranslated events from
648 the last key sequence entered are used.
649 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
651 If KEY is a menu item or a tool-bar button that is disabled, this command
652 temporarily enables it to allow getting help on disabled items and buttons."
653 (interactive
654 (let ((enable-disabled-menus-and-buttons t)
655 (cursor-in-echo-area t)
656 saved-yank-menu)
657 (unwind-protect
658 (let (key)
659 ;; If yank-menu is empty, populate it temporarily, so that
660 ;; "Select and Paste" menu can generate a complete event.
661 (when (null (cdr yank-menu))
662 (setq saved-yank-menu (copy-sequence yank-menu))
663 (menu-bar-update-yank-menu "(any string)" nil))
664 (setq key (read-key-sequence "Describe key (or click or menu item): "))
665 (list
667 (prefix-numeric-value current-prefix-arg)
668 ;; If KEY is a down-event, read and include the
669 ;; corresponding up-event. Note that there are also
670 ;; down-events on scroll bars and mode lines: the actual
671 ;; event then is in the second element of the vector.
672 (and (vectorp key)
673 (let ((last-idx (1- (length key))))
674 (and (eventp (aref key last-idx))
675 (memq 'down (event-modifiers (aref key last-idx)))))
676 (or (and (eventp (aref key 0))
677 (memq 'down (event-modifiers (aref key 0)))
678 ;; However, for the C-down-mouse-2 popup
679 ;; menu, there is no subsequent up-event. In
680 ;; this case, the up-event is the next
681 ;; element in the supplied vector.
682 (= (length key) 1))
683 (and (> (length key) 1)
684 (eventp (aref key 1))
685 (memq 'down (event-modifiers (aref key 1)))))
686 (read-event))))
687 ;; Put yank-menu back as it was, if we changed it.
688 (when saved-yank-menu
689 (setq yank-menu (copy-sequence saved-yank-menu))
690 (fset 'yank-menu (cons 'keymap yank-menu))))))
691 (if (numberp untranslated)
692 (setq untranslated (this-single-command-raw-keys)))
693 (let* ((event (aref key (if (and (symbolp (aref key 0))
694 (> (length key) 1)
695 (consp (aref key 1)))
697 0)))
698 (modifiers (event-modifiers event))
699 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
700 (memq 'drag modifiers)) " at that spot" ""))
701 (defn (key-binding key t))
702 defn-up defn-up-tricky ev-type
703 mouse-1-remapped mouse-1-tricky)
705 ;; Handle the case where we faked an entry in "Select and Paste" menu.
706 (when (and (eq defn nil)
707 (stringp (aref key (1- (length key))))
708 (eq (key-binding (substring key 0 -1)) 'yank-menu))
709 (setq defn 'menu-bar-select-yank))
710 (if (or (null defn) (integerp defn) (equal defn 'undefined))
711 (message "%s%s is undefined"
712 (help-key-description key untranslated) mouse-msg)
713 (help-setup-xref (list #'describe-function defn)
714 (called-interactively-p 'interactive))
715 ;; Don't bother user with strings from (e.g.) the select-paste menu.
716 (when (stringp (aref key (1- (length key))))
717 (aset key (1- (length key)) "(any string)"))
718 (when (and untranslated
719 (stringp (aref untranslated (1- (length untranslated)))))
720 (aset untranslated (1- (length untranslated))
721 "(any string)"))
722 ;; Need to do this before erasing *Help* buffer in case event
723 ;; is a mouse click in an existing *Help* buffer.
724 (when up-event
725 (setq ev-type (event-basic-type up-event))
726 (let ((sequence (vector up-event)))
727 (when (and (eq ev-type 'mouse-1)
728 mouse-1-click-follows-link
729 (not (eq mouse-1-click-follows-link 'double))
730 (setq mouse-1-remapped
731 (mouse-on-link-p (event-start up-event))))
732 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
733 (> mouse-1-click-follows-link 0)))
734 (cond ((stringp mouse-1-remapped)
735 (setq sequence mouse-1-remapped))
736 ((vectorp mouse-1-remapped)
737 (setcar up-event (elt mouse-1-remapped 0)))
738 (t (setcar up-event 'mouse-2))))
739 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
740 (when mouse-1-tricky
741 (setq sequence (vector up-event))
742 (aset sequence 0 'mouse-1)
743 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
744 (with-help-window (help-buffer)
745 (princ (help-key-description key untranslated))
746 (princ (format "\
747 %s runs the command %S, which is "
748 mouse-msg defn))
749 (describe-function-1 defn)
750 (when up-event
751 (unless (or (null defn-up)
752 (integerp defn-up)
753 (equal defn-up 'undefined))
754 (princ (format "
756 ----------------- up-event %s----------------
758 %s%s%s runs the command %S, which is "
759 (if mouse-1-tricky "(short click) " "")
760 (key-description (vector up-event))
761 mouse-msg
762 (if mouse-1-remapped
763 " is remapped to <mouse-2>, which" "")
764 defn-up))
765 (describe-function-1 defn-up))
766 (unless (or (null defn-up-tricky)
767 (integerp defn-up-tricky)
768 (eq defn-up-tricky 'undefined))
769 (princ (format "
771 ----------------- up-event (long click) ----------------
773 Pressing <%S>%s for longer than %d milli-seconds
774 runs the command %S, which is "
775 ev-type mouse-msg
776 mouse-1-click-follows-link
777 defn-up-tricky))
778 (describe-function-1 defn-up-tricky)))))))
780 (defun describe-mode (&optional buffer)
781 "Display documentation of current major mode and minor modes.
782 A brief summary of the minor modes comes first, followed by the
783 major mode description. This is followed by detailed
784 descriptions of the minor modes, each on a separate page.
786 For this to work correctly for a minor mode, the mode's indicator
787 variable \(listed in `minor-mode-alist') must also be a function
788 whose documentation describes the minor mode."
789 (interactive "@")
790 (unless buffer (setq buffer (current-buffer)))
791 (help-setup-xref (list #'describe-mode buffer)
792 (called-interactively-p 'interactive))
793 ;; For the sake of help-do-xref and help-xref-go-back,
794 ;; don't switch buffers before calling `help-buffer'.
795 (with-help-window (help-buffer)
796 (with-current-buffer buffer
797 (let (minor-modes)
798 ;; Older packages do not register in minor-mode-list but only in
799 ;; minor-mode-alist.
800 (dolist (x minor-mode-alist)
801 (setq x (car x))
802 (unless (memq x minor-mode-list)
803 (push x minor-mode-list)))
804 ;; Find enabled minor mode we will want to mention.
805 (dolist (mode minor-mode-list)
806 ;; Document a minor mode if it is listed in minor-mode-alist,
807 ;; non-nil, and has a function definition.
808 (let ((fmode (or (get mode :minor-mode-function) mode)))
809 (and (boundp mode) (symbol-value mode)
810 (fboundp fmode)
811 (let ((pretty-minor-mode
812 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
813 (symbol-name fmode))
814 (capitalize
815 (substring (symbol-name fmode)
816 0 (match-beginning 0)))
817 fmode)))
818 (push (list fmode pretty-minor-mode
819 (format-mode-line (assq mode minor-mode-alist)))
820 minor-modes)))))
821 (setq minor-modes
822 (sort minor-modes
823 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
824 (when minor-modes
825 (princ "Enabled minor modes:\n")
826 (make-local-variable 'help-button-cache)
827 (with-current-buffer standard-output
828 (dolist (mode minor-modes)
829 (let ((mode-function (nth 0 mode))
830 (pretty-minor-mode (nth 1 mode))
831 (indicator (nth 2 mode)))
832 (add-text-properties 0 (length pretty-minor-mode)
833 '(face bold) pretty-minor-mode)
834 (save-excursion
835 (goto-char (point-max))
836 (princ "\n\f\n")
837 (push (point-marker) help-button-cache)
838 ;; Document the minor modes fully.
839 (insert pretty-minor-mode)
840 (princ (format " minor mode (%s):\n"
841 (if (zerop (length indicator))
842 "no indicator"
843 (format "indicator%s"
844 indicator))))
845 (princ (documentation mode-function)))
846 (insert-button pretty-minor-mode
847 'action (car help-button-cache)
848 'follow-link t
849 'help-echo "mouse-2, RET: show full information")
850 (newline)))
851 (forward-line -1)
852 (fill-paragraph nil)
853 (forward-line 1))
855 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
856 ;; Document the major mode.
857 (let ((mode mode-name))
858 (with-current-buffer standard-output
859 (let ((start (point)))
860 (insert (format-mode-line mode nil nil buffer))
861 (add-text-properties start (point) '(face bold)))))
862 (princ " mode:\n")
863 (princ (documentation major-mode)))))
864 ;; For the sake of IELM and maybe others
865 nil)
868 (defun describe-minor-mode (minor-mode)
869 "Display documentation of a minor mode given as MINOR-MODE.
870 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
871 appeared on the mode-line."
872 (interactive (list (completing-read
873 "Minor mode: "
874 (nconc
875 (describe-minor-mode-completion-table-for-symbol)
876 (describe-minor-mode-completion-table-for-indicator)
877 ))))
878 (if (symbolp minor-mode)
879 (setq minor-mode (symbol-name minor-mode)))
880 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
881 (indicators (describe-minor-mode-completion-table-for-indicator)))
882 (cond
883 ((member minor-mode symbols)
884 (describe-minor-mode-from-symbol (intern minor-mode)))
885 ((member minor-mode indicators)
886 (describe-minor-mode-from-indicator minor-mode))
888 (error "No such minor mode: %s" minor-mode)))))
890 ;; symbol
891 (defun describe-minor-mode-completion-table-for-symbol ()
892 ;; In order to list up all minor modes, minor-mode-list
893 ;; is used here instead of minor-mode-alist.
894 (delq nil (mapcar 'symbol-name minor-mode-list)))
896 (defun describe-minor-mode-from-symbol (symbol)
897 "Display documentation of a minor mode given as a symbol, SYMBOL"
898 (interactive (list (intern (completing-read
899 "Minor mode symbol: "
900 (describe-minor-mode-completion-table-for-symbol)))))
901 (if (fboundp symbol)
902 (describe-function symbol)
903 (describe-variable symbol)))
905 ;; indicator
906 (defun describe-minor-mode-completion-table-for-indicator ()
907 (delq nil
908 (mapcar (lambda (x)
909 (let ((i (format-mode-line x)))
910 ;; remove first space if existed
911 (cond
912 ((= 0 (length i))
913 nil)
914 ((eq (aref i 0) ?\s)
915 (substring i 1))
917 i))))
918 minor-mode-alist)))
920 (defun describe-minor-mode-from-indicator (indicator)
921 "Display documentation of a minor mode specified by INDICATOR.
922 If you call this function interactively, you can give indicator which
923 is currently activated with completion."
924 (interactive (list
925 (completing-read
926 "Minor mode indicator: "
927 (describe-minor-mode-completion-table-for-indicator))))
928 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
929 (if minor-mode
930 (describe-minor-mode-from-symbol minor-mode)
931 (error "Cannot find minor mode for `%s'" indicator))))
933 (defun lookup-minor-mode-from-indicator (indicator)
934 "Return a minor mode symbol from its indicator on the modeline."
935 ;; remove first space if existed
936 (if (and (< 0 (length indicator))
937 (eq (aref indicator 0) ?\s))
938 (setq indicator (substring indicator 1)))
939 (let ((minor-modes minor-mode-alist)
940 result)
941 (while minor-modes
942 (let* ((minor-mode (car (car minor-modes)))
943 (anindicator (format-mode-line
944 (car (cdr (car minor-modes))))))
945 ;; remove first space if existed
946 (if (and (stringp anindicator)
947 (> (length anindicator) 0)
948 (eq (aref anindicator 0) ?\s))
949 (setq anindicator (substring anindicator 1)))
950 (if (equal indicator anindicator)
951 (setq result minor-mode
952 minor-modes nil)
953 (setq minor-modes (cdr minor-modes)))))
954 result))
956 ;;; help windows
957 (defcustom help-window-select 'other
958 "Non-nil means select help window for viewing.
959 Choices are:
960 never (nil) Select help window only if there is no other window
961 on its frame.
962 other Select help window unless the selected window is the
963 only other window on its frame.
964 always (t) Always select the help window.
966 This option has effect if and only if the help window was created
967 by `with-help-window'"
968 :type '(choice (const :tag "never (nil)" nil)
969 (const :tag "other" other)
970 (const :tag "always (t)" t))
971 :group 'help
972 :version "23.1")
974 (defun help-window-display-message (quit-part window &optional scroll)
975 "Display message telling how to quit and scroll help window.
976 QUIT-PART is a string telling how to quit the help window WINDOW.
977 Optional argument SCROLL non-nil means tell how to scroll WINDOW.
978 SCROLL equal `other' means tell how to scroll the \"other\"
979 window."
980 (let ((scroll-part
981 (cond
982 ((pos-visible-in-window-p
983 (with-current-buffer (window-buffer window)
984 (point-max)) window)
985 ;; Buffer end is visible.
986 ".")
987 ((eq scroll 'other)
988 ", \\[scroll-other-window] to scroll help.")
989 (scroll ", \\[scroll-up] to scroll help."))))
990 (message "%s"
991 (substitute-command-keys (concat quit-part scroll-part)))))
993 ;; No matter what we do down here: There will be always a case that
994 ;; defeats our best intentions ;-)
995 (defun help-window-setup ()
996 "Set up help window."
997 (let* ((help-window (car-safe display-buffer-window-and-buffer))
998 (help-buffer (when (window-live-p help-window)
999 (window-buffer help-window)))
1000 (help-value (cdr-safe display-buffer-window-and-buffer))
1001 (number-of-windows
1002 ;; FIXME: This is intuitvely wrong for compound windows. What
1003 ;; we need is the number of windows eligible by `other-window'
1004 ;; on this frame.
1005 (length (window-list (window-frame help-window) 'no-mini))))
1006 (when help-buffer
1007 ;; Handle `help-window-point-marker'.
1008 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1009 (set-window-point help-window help-window-point-marker)
1010 ;; Reset `help-window-point-marker'.
1011 (set-marker help-window-point-marker nil))
1013 (cond
1014 ;; Don't say anything if the help window displayed the same
1015 ;; buffer before.
1016 ((or (not help-value) (eq help-buffer help-value)))
1017 ;; The following does not handle the case where the help window
1018 ;; fills its frame, is not selected, but does get selected
1019 ;; afterwards by external intervention.
1020 ((or (eq help-window (selected-window))
1021 (and (eq help-window-select t)
1022 (select-window help-window)))
1023 ;; The help window is or gets selected ...
1024 (help-window-display-message
1025 (cond
1026 ((buffer-live-p help-value)
1027 ;; ... and displayed some other buffer before, ...
1028 "Type \"q\" to restore previous buffer")
1029 ((eq (frame-root-window (window-frame help-window))
1030 help-window)
1031 (if view-remove-frame-by-deleting
1032 ;; ... is on a new deletable frame, ...
1033 "Type \"q\" to delete this frame"
1034 ;; ... is on a new non-deletable frame, ...
1035 "Type \"q\" to iconify this frame"))
1037 ;; ... is new.
1038 "Type \"q\" to delete this window"))
1039 help-window t))
1040 ((and (= number-of-windows 2)
1041 (eq (window-frame help-window)
1042 (window-frame (selected-window))))
1043 ;; There are two windows on the help window's frame and the
1044 ;; other one is the selected one.
1045 (if (memq help-window-select '(nil other))
1046 ;; Do not select the help window.
1047 (help-window-display-message
1048 (if (buffer-live-p help-value)
1049 ;; Offer `display-buffer' for consistency with
1050 ;; `help-print-return-message'. This is not TRT but
1051 ;; handled this way ever since.
1052 "Type \\[display-buffer] RET to restore previous buffer"
1053 ;; The classic "two windows" configuration.
1054 "Type \\[delete-other-windows] to delete the help window")
1055 help-window 'other)
1056 ;; Select help window and tell how to quit.
1057 (select-window help-window)
1058 (help-window-display-message
1059 (if (buffer-live-p help-value)
1060 "Type \"q\" to restore previous buffer"
1061 "Type \"q\" to delete this window") help-window t)))
1063 ;; Not much to say here.
1064 (help-window-display-message
1065 "Type \"q\" in help window to quit" help-window))))))
1067 ;; `with-help-window' is a wrapper for `with-output-to-temp-buffer'
1068 ;; providing the following additional twists:
1070 ;; (1) Issue more accurate messages telling how to scroll and quit the
1071 ;; help window.
1073 ;; (2) Make `view-mode-exit' DTRT in more cases.
1075 ;; (3) An option (customizable via `help-window-select') to select the
1076 ;; help window automatically.
1078 ;; (4) A marker (`help-window-point-marker') to move point in the help
1079 ;; window to an arbitrary buffer position.
1081 ;; Note: It's usually always wrong to use `help-print-return-message' in
1082 ;; the body of `with-help-window'.
1083 (defmacro with-help-window (buffer-name &rest body)
1084 "Display buffer BUFFER-NAME in a help window evaluating BODY.
1085 Select help window if the actual value of the user option
1086 `help-window-select' says so. Return last value in BODY."
1087 (declare (indent 1) (debug t))
1088 ;; Bind list-of-frames to `frame-list' and list-of-window-tuples to a
1089 ;; list of one <window window-buffer window-start window-point> tuple
1090 ;; for each live window.
1091 `(progn
1092 ;; Emacs 24 sets `display-buffer-window-and-buffer' to the window
1093 ;; it uses to display the buffer and some buffer information. Bind
1094 ;; it to nil and see what it makes
1095 (setq display-buffer-window-and-buffer nil)
1096 ;; Make `help-window-point-marker' point nowhere (the only place
1097 ;; where this should be set to a buffer position is within BODY).
1098 (set-marker help-window-point-marker nil)
1099 (prog1
1100 ;; Return value returned by `with-output-to-temp-buffer'.
1101 (with-output-to-temp-buffer ,buffer-name
1102 (progn ,@body))
1103 (when display-buffer-window-and-buffer (help-window-setup)))))
1105 (provide 'help)
1107 ;; arch-tag: cf427352-27e9-49b7-9a6f-741ebab02423
1108 ;;; help.el ends here