(describe-mode): Make minor mode list more concise.
[bpt/emacs.git] / lisp / help.el
1 ;;; help.el --- help commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: help, internal
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; 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.
25
26 ;;; Commentary:
27
28 ;; This code implements GNU Emacs' on-line help system, the one invoked by
29 ;; `M-x help-for-help'.
30
31 ;;; Code:
32
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))
36
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)
40
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)
48
49 (define-key map "\C-c" 'describe-copying)
50 (define-key map "\C-d" 'describe-distribution)
51 (define-key map "\C-e" 'view-emacs-problems)
52 (define-key map "\C-f" 'view-emacs-FAQ)
53 (define-key map "\C-m" 'view-order-manuals)
54 (define-key map "\C-n" 'view-emacs-news)
55 (define-key map "\C-p" 'describe-project)
56 (define-key map "\C-t" 'view-todo)
57 (define-key map "\C-w" 'describe-no-warranty)
58
59 ;; This does not fit the pattern, but it is natural given the C-\ command.
60 (define-key map "\C-\\" 'describe-input-method)
61
62 (define-key map "C" 'describe-coding-system)
63 (define-key map "F" 'Info-goto-emacs-command-node)
64 (define-key map "I" 'describe-input-method)
65 (define-key map "K" 'Info-goto-emacs-key-command-node)
66 (define-key map "L" 'describe-language-environment)
67 (define-key map "S" 'info-lookup-symbol)
68
69 (define-key map "a" 'apropos-command)
70 (define-key map "b" 'describe-bindings)
71 (define-key map "c" 'describe-key-briefly)
72 (define-key map "d" 'apropos-documentation)
73 (define-key map "e" 'view-echo-area-messages)
74 (define-key map "f" 'describe-function)
75 (define-key map "h" 'view-hello-file)
76
77 (define-key map "i" 'info)
78 (define-key map "4i" 'info-other-window)
79
80 (define-key map "k" 'describe-key)
81 (define-key map "l" 'view-lossage)
82 (define-key map "m" 'describe-mode)
83 (define-key map "n" 'view-emacs-news)
84 (define-key map "p" 'finder-by-keyword)
85 (define-key map "r" 'info-emacs-manual)
86 (define-key map "s" 'describe-syntax)
87 (define-key map "t" 'help-with-tutorial)
88 (define-key map "w" 'where-is)
89 (define-key map "v" 'describe-variable)
90 (define-key map "q" 'help-quit)
91 map)
92 "Keymap for characters following the Help key.")
93
94 (define-key global-map (char-to-string help-char) 'help-command)
95 (define-key global-map [help] 'help-command)
96 (define-key global-map [f1] 'help-command)
97 (fset 'help-command help-map)
98
99 (autoload 'finder-by-keyword "finder"
100 "Find packages matching a given keyword." t)
101
102 ;; insert-button makes the action nil if it is not store somewhere
103 (defvar help-button-cache nil)
104
105 \f
106 (defun help-quit ()
107 "Just exit from the Help command's command loop."
108 (interactive)
109 nil)
110
111 (defvar help-return-method nil
112 "What to do to \"exit\" the help buffer.
113 This is a list
114 (WINDOW . t) delete the selected window (and possibly its frame,
115 see `quit-window' and `View-quit'), go to WINDOW.
116 (WINDOW . quit-window) do quit-window, then select WINDOW.
117 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
118
119 (defun print-help-return-message (&optional function)
120 "Display or return message saying how to restore windows after help command.
121 This function assumes that `standard-output' is the help buffer.
122 It computes a message, and applies the optional argument FUNCTION to it.
123 If FUNCTION is nil, it applies `message', thus displaying the message.
124 In addition, this function sets up `help-return-method', which see, that
125 specifies what to do when the user exits the help buffer."
126 (and (not (get-buffer-window standard-output))
127 (let ((first-message
128 (cond ((or
129 pop-up-frames
130 (special-display-p (buffer-name standard-output)))
131 (setq help-return-method (cons (selected-window) t))
132 ;; If the help output buffer is a special display buffer,
133 ;; don't say anything about how to get rid of it.
134 ;; First of all, the user will do that with the window
135 ;; manager, not with Emacs.
136 ;; Secondly, the buffer has not been displayed yet,
137 ;; so we don't know whether its frame will be selected.
138 nil)
139 (display-buffer-reuse-frames
140 (setq help-return-method (cons (selected-window)
141 'quit-window))
142 nil)
143 ((not (one-window-p t))
144 (setq help-return-method
145 (cons (selected-window) 'quit-window))
146 "Type \\[display-buffer] RET to restore the other window.")
147 (pop-up-windows
148 (setq help-return-method (cons (selected-window) t))
149 "Type \\[delete-other-windows] to remove help window.")
150 (t
151 (setq help-return-method
152 (list (selected-window) (window-buffer)
153 (window-start) (window-point)))
154 "Type \\[switch-to-buffer] RET to remove help window."))))
155 (funcall (or function 'message)
156 (concat
157 (if first-message
158 (substitute-command-keys first-message))
159 (if first-message " ")
160 ;; If the help buffer will go in a separate frame,
161 ;; it's no use mentioning a command to scroll, so don't.
162 (if (or pop-up-windows
163 (special-display-p (buffer-name standard-output)))
164 nil
165 (if (same-window-p (buffer-name standard-output))
166 ;; Say how to scroll this window.
167 (substitute-command-keys
168 "\\[scroll-up] to scroll the help.")
169 ;; Say how to scroll some other window.
170 (substitute-command-keys
171 "\\[scroll-other-window] to scroll the help."))))))))
172
173 ;; So keyboard macro definitions are documented correctly
174 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
175
176 (defalias 'help 'help-for-help-internal)
177 ;; find-function can find this.
178 (defalias 'help-for-help 'help-for-help-internal)
179 ;; It can't find this, but nobody will look.
180 (make-help-screen help-for-help-internal
181 "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 ? :"
182 "You have typed %THIS-KEY%, the help character. Type a Help option:
183 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
184
185 a command-apropos. Type a list of words or a regexp; it shows a list of
186 commands whose names match. See also the apropos command.
187 b describe-bindings. Display a table of all key bindings.
188 c describe-key-briefly. Type a key sequence;
189 it displays the command name run by that key sequence.
190 C describe-coding-system. Type the name of the coding system to describe,
191 or just RET to describe the ones currently in use.
192 d apropos-documentation. Type a pattern (a list of words or a regexp), and
193 it shows a list of functions, variables, and other items whose
194 documentation matches that pattern. See also the apropos command.
195 e view-echo-area-messages. Go to the buffer that logs echo-area messages.
196 f describe-function. Type a function name and you see its documentation.
197 F Info-goto-emacs-command-node. Type a command name;
198 it goes to the on-line manual's section that describes the command.
199 h Display the HELLO file which illustrates various scripts.
200 i info. The Info documentation reader: read on-line manuals.
201 I describe-input-method. Describe a specific input method (if you type
202 its name) or the current input method (if you type just RET).
203 k describe-key. Type a key sequence;
204 it displays the full documentation for that key sequence.
205 K Info-goto-emacs-key-command-node. Type a key sequence;
206 it goes to the on-line manual's section that describes
207 the command bound to that key.
208 l view-lossage. Show last 100 characters you typed.
209 L describe-language-environment. This describes either a
210 specific language environment (if you type its name)
211 or the current language environment (if you type just RET).
212 m describe-mode. Display documentation of current minor modes,
213 and the current major mode, including their special commands.
214 n view-emacs-news. Display news of recent Emacs changes.
215 p finder-by-keyword. Find packages matching a given topic keyword.
216 r info-emacs-manual. Display the Emacs manual in Info mode.
217 s describe-syntax. Display contents of syntax table, plus explanations.
218 S info-lookup-symbol. Type a symbol; it goes to that symbol in the
219 on-line manual for the programming language used in this buffer.
220 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
221 v describe-variable. Type name of a variable;
222 it displays the variable's documentation and value.
223 w where-is. Type a command name; it displays which keystrokes
224 invoke that command.
225 . display-local-help. Display any available local help at point
226 in the echo area.
227
228 C-c Display Emacs copying permission (GNU General Public License).
229 C-d Display Emacs ordering information.
230 C-e Display info about Emacs problems.
231 C-f Display the Emacs FAQ.
232 C-m Display how to order printed Emacs manuals.
233 C-n Display news of recent Emacs changes.
234 C-p Display information about the GNU project.
235 C-t Display the Emacs TODO list.
236 C-w Display information on absence of warranty for GNU Emacs."
237 help-map)
238
239 \f
240
241 (defun function-called-at-point ()
242 "Return a function around point or else called by the list containing point.
243 If that doesn't give a function, return nil."
244 (or (with-syntax-table emacs-lisp-mode-syntax-table
245 (or (condition-case ()
246 (save-excursion
247 (or (not (zerop (skip-syntax-backward "_w")))
248 (eq (char-syntax (following-char)) ?w)
249 (eq (char-syntax (following-char)) ?_)
250 (forward-sexp -1))
251 (skip-chars-forward "'")
252 (let ((obj (read (current-buffer))))
253 (and (symbolp obj) (fboundp obj) obj)))
254 (error nil))
255 (condition-case ()
256 (save-excursion
257 (save-restriction
258 (narrow-to-region (max (point-min)
259 (- (point) 1000)) (point-max))
260 ;; Move up to surrounding paren, then after the open.
261 (backward-up-list 1)
262 (forward-char 1)
263 ;; If there is space here, this is probably something
264 ;; other than a real Lisp function call, so ignore it.
265 (if (looking-at "[ \t]")
266 (error "Probably not a Lisp function call"))
267 (let ((obj (read (current-buffer))))
268 (and (symbolp obj) (fboundp obj) obj))))
269 (error nil))))
270 (let* ((str (find-tag-default))
271 (sym (if str (intern-soft str))))
272 (if (and sym (fboundp sym))
273 sym
274 (save-match-data
275 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
276 (setq sym (intern-soft (match-string 1 str)))
277 (and (fboundp sym) sym)))))))
278
279 \f
280 ;;; `User' help functions
281
282 (defun describe-distribution ()
283 "Display info on how to obtain the latest version of GNU Emacs."
284 (interactive)
285 (view-file (expand-file-name "DISTRIB" data-directory)))
286
287 (defun describe-copying ()
288 "Display info on how you may redistribute copies of GNU Emacs."
289 (interactive)
290 (view-file (expand-file-name "COPYING" data-directory))
291 (goto-char (point-min)))
292
293 (defun describe-project ()
294 "Display info on the GNU project."
295 (interactive)
296 (view-file (expand-file-name "THE-GNU-PROJECT" data-directory))
297 (goto-char (point-min)))
298
299 (defun describe-no-warranty ()
300 "Display info on all the kinds of warranty Emacs does NOT have."
301 (interactive)
302 (describe-copying)
303 (let (case-fold-search)
304 (search-forward "NO WARRANTY")
305 (recenter 0)))
306
307 (defun describe-prefix-bindings ()
308 "Describe the bindings of the prefix used to reach this command.
309 The prefix described consists of all but the last event
310 of the key sequence that ran this command."
311 (interactive)
312 (let* ((key (this-command-keys)))
313 (describe-bindings
314 (if (stringp key)
315 (substring key 0 (1- (length key)))
316 (let ((prefix (make-vector (1- (length key)) nil))
317 (i 0))
318 (while (< i (length prefix))
319 (aset prefix i (aref key i))
320 (setq i (1+ i)))
321 prefix)))))
322 ;; Make C-h after a prefix, when not specifically bound,
323 ;; run describe-prefix-bindings.
324 (setq prefix-help-command 'describe-prefix-bindings)
325
326 (defun view-emacs-news (&optional version)
327 "Display info on recent changes to Emacs.
328 With argument, display info only for the selected version."
329 (interactive "P")
330 (unless version
331 (setq version emacs-major-version))
332 (when (consp version)
333 (let* ((all-versions
334 (let (res)
335 (mapcar
336 (lambda (file)
337 (with-temp-buffer
338 (insert-file-contents
339 (expand-file-name file data-directory))
340 (while (re-search-forward
341 (if (member file '("NEWS.18" "NEWS.1-17"))
342 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
343 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
344 (setq res (cons (match-string-no-properties 1) res)))))
345 (cons "NEWS"
346 (directory-files data-directory nil
347 "^NEWS\\.[0-9][-0-9]*$" nil)))
348 (sort (delete-dups res) (lambda (a b) (string< b a)))))
349 (current (car all-versions)))
350 (setq version (completing-read
351 (format "Read NEWS for the version (default %s): " current)
352 all-versions nil nil nil nil current))
353 (if (integerp (string-to-number version))
354 (setq version (string-to-number version))
355 (unless (or (member version all-versions)
356 (<= (string-to-number version) (string-to-number current)))
357 (error "No news about version %s" version)))))
358 (when (integerp version)
359 (cond ((<= version 12)
360 (setq version (format "1.%d" version)))
361 ((<= version 18)
362 (setq version (format "%d" version)))
363 ((> version emacs-major-version)
364 (error "No news about emacs %d (yet)" version))))
365 (let* ((vn (if (stringp version)
366 (string-to-number version)
367 version))
368 (file (cond
369 ((>= vn emacs-major-version) "NEWS")
370 ((< vn 18) "NEWS.1-17")
371 (t (format "NEWS.%d" vn))))
372 res)
373 (view-file (expand-file-name file data-directory))
374 (widen)
375 (goto-char (point-min))
376 (when (stringp version)
377 (when (re-search-forward
378 (concat (if (< vn 19)
379 "Changes in Emacs[ \t]*"
380 "^\* [^0-9\n]*") version "$")
381 nil t)
382 (beginning-of-line)
383 (narrow-to-region
384 (point)
385 (save-excursion
386 (while (and (setq res
387 (re-search-forward
388 (if (< vn 19)
389 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
390 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
391 (equal (match-string-no-properties 1) version)))
392 (or res (goto-char (point-max)))
393 (beginning-of-line)
394 (point)))))))
395
396
397 (defun view-todo (&optional arg)
398 "Display the Emacs TODO list."
399 (interactive "P")
400 (view-file (expand-file-name "TODO" data-directory)))
401
402 (defun view-echo-area-messages ()
403 "View the log of recent echo-area messages: the `*Messages*' buffer.
404 The number of messages retained in that buffer
405 is specified by the variable `message-log-max'."
406 (interactive)
407 (switch-to-buffer (get-buffer-create "*Messages*")))
408
409 (defun view-order-manuals ()
410 "Display the Emacs ORDERS file."
411 (interactive)
412 (view-file (expand-file-name "ORDERS" data-directory))
413 (goto-address))
414
415 (defun view-emacs-FAQ ()
416 "Display the Emacs Frequently Asked Questions (FAQ) file."
417 (interactive)
418 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
419 (info "(efaq)"))
420
421 (defun view-emacs-problems ()
422 "Display info on known problems with Emacs and possible workarounds."
423 (interactive)
424 (view-file (expand-file-name "PROBLEMS" data-directory)))
425
426 (defun view-lossage ()
427 "Display last 100 input keystrokes.
428
429 To record all your input on a file, use `open-dribble-file'."
430 (interactive)
431 (help-setup-xref (list #'view-lossage) (interactive-p))
432 (with-output-to-temp-buffer (help-buffer)
433 (princ (mapconcat (lambda (key)
434 (if (or (integerp key) (symbolp key) (listp key))
435 (single-key-description key)
436 (prin1-to-string key nil)))
437 (recent-keys)
438 " "))
439 (with-current-buffer standard-output
440 (goto-char (point-min))
441 (while (progn (move-to-column 50) (not (eobp)))
442 (when (search-forward " " nil t)
443 (delete-char -1))
444 (insert "\n")))
445 (print-help-return-message)))
446
447 \f
448 ;; Key bindings
449
450 (defun describe-bindings (&optional prefix buffer)
451 "Show a list of all defined keys, and their definitions.
452 We put that list in a buffer, and display the buffer.
453
454 The optional argument PREFIX, if non-nil, should be a key sequence;
455 then we display only bindings that start with that prefix.
456 The optional argument BUFFER specifies which buffer's bindings
457 to display (default, the current buffer). BUFFER can be a buffer
458 or a buffer name."
459 (interactive)
460 (or buffer (setq buffer (current-buffer)))
461 (help-setup-xref (list #'describe-bindings prefix buffer) (interactive-p))
462 (with-current-buffer buffer
463 (describe-bindings-internal nil prefix)))
464
465 ;; This function used to be in keymap.c.
466 (defun describe-bindings-internal (&optional menus prefix)
467 "Show a list of all defined keys, and their definitions.
468 We put that list in a buffer, and display the buffer.
469
470 The optional argument MENUS, if non-nil, says to mention menu bindings.
471 \(Ordinarily these are omitted from the output.)
472 The optional argument PREFIX, if non-nil, should be a key sequence;
473 then we display only bindings that start with that prefix."
474 (interactive)
475 (let ((buf (current-buffer)))
476 (with-output-to-temp-buffer "*Help*"
477 (with-current-buffer standard-output
478 (describe-buffer-bindings buf prefix menus)))))
479
480 (defun where-is (definition &optional insert)
481 "Print message listing key sequences that invoke the command DEFINITION.
482 Argument is a command definition, usually a symbol with a function definition.
483 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
484 (interactive
485 (let ((fn (function-called-at-point))
486 (enable-recursive-minibuffers t)
487 val)
488 (setq val (completing-read
489 (if fn
490 (format "Where is command (default %s): " fn)
491 "Where is command: ")
492 obarray 'commandp t))
493 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
494 (let ((func (indirect-function definition))
495 (defs nil)
496 (standard-output (if insert (current-buffer) t)))
497 ;; In DEFS, find all symbols that are aliases for DEFINITION.
498 (mapatoms (lambda (symbol)
499 (and (fboundp symbol)
500 (not (eq symbol definition))
501 (eq func (condition-case ()
502 (indirect-function symbol)
503 (error symbol)))
504 (push symbol defs))))
505 ;; Look at all the symbols--first DEFINITION,
506 ;; then its aliases.
507 (dolist (symbol (cons definition defs))
508 (let* ((remapped (command-remapping symbol))
509 (keys (where-is-internal
510 symbol overriding-local-map nil nil remapped))
511 (keys (mapconcat 'key-description keys ", "))
512 string)
513 (setq string
514 (if insert
515 (if (> (length keys) 0)
516 (if remapped
517 (format "%s (%s) (remapped from %s)"
518 keys remapped symbol)
519 (format "%s (%s)" keys symbol))
520 (format "M-x %s RET" symbol))
521 (if (> (length keys) 0)
522 (if remapped
523 (format "%s is remapped to %s which is on %s"
524 symbol remapped keys)
525 (format "%s is on %s" symbol keys))
526 ;; If this is the command the user asked about,
527 ;; and it is not on any key, say so.
528 ;; For other symbols, its aliases, say nothing
529 ;; about them unless they are on keys.
530 (if (eq symbol definition)
531 (format "%s is not on any key" symbol)))))
532 (when string
533 (unless (eq symbol definition)
534 (princ ";\n its alias "))
535 (princ string)))))
536 nil)
537
538 (defun string-key-binding (key)
539 "Value is the binding of KEY in a string.
540 If KEY is an event on a string, and that string has a `local-map'
541 or `keymap' property, return the binding of KEY in the string's keymap."
542 (let* ((defn nil)
543 (start (when (vectorp key)
544 (if (memq (aref key 0)
545 '(mode-line header-line left-margin right-margin))
546 (event-start (aref key 1))
547 (and (consp (aref key 0))
548 (event-start (aref key 0))))))
549 (string-info (and (consp start) (nth 4 start))))
550 (when string-info
551 (let* ((string (car string-info))
552 (pos (cdr string-info))
553 (local-map (and (>= pos 0)
554 (< pos (length string))
555 (or (get-text-property pos 'local-map string)
556 (get-text-property pos 'keymap string)))))
557 (setq defn (and local-map (lookup-key local-map key)))))
558 defn))
559
560 (defun help-key-description (key untranslated)
561 (let ((string (key-description key)))
562 (if (or (not untranslated)
563 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
564 string
565 (let ((otherstring (key-description untranslated)))
566 (if (equal string otherstring)
567 string
568 (format "%s (translated from %s)" string otherstring))))))
569
570 (defun describe-key-briefly (&optional key insert untranslated)
571 "Print the name of the function KEY invokes. KEY is a string.
572 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
573 If non-nil, UNTRANSLATED is a vector of the untranslated events.
574 It can also be a number in which case the untranslated events from
575 the last key hit are used.
576
577 If KEY is a menu item or a tool-bar button that is disabled, this command
578 temporarily enables it to allow getting help on disabled items and buttons."
579 (interactive
580 (let ((enable-disabled-menus-and-buttons t)
581 (cursor-in-echo-area t)
582 saved-yank-menu)
583 (unwind-protect
584 (let (key)
585 ;; If yank-menu is empty, populate it temporarily, so that
586 ;; "Select and Paste" menu can generate a complete event.
587 (when (null (cdr yank-menu))
588 (setq saved-yank-menu (copy-sequence yank-menu))
589 (menu-bar-update-yank-menu "(any string)" nil))
590 (setq key (read-key-sequence "Describe key (or click or menu item): "))
591 ;; If KEY is a down-event, read and discard the
592 ;; corresponding up-event.
593 (if (and (vectorp key)
594 (eventp (elt key 0))
595 (memq 'down (event-modifiers (elt key 0))))
596 (read-event))
597 (list
598 key
599 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
600 1))
601 ;; Put yank-menu back as it was, if we changed it.
602 (when saved-yank-menu
603 (setq yank-menu (copy-sequence saved-yank-menu))
604 (fset 'yank-menu (cons 'keymap yank-menu))))))
605 (if (numberp untranslated)
606 (setq untranslated (this-single-command-raw-keys)))
607 (save-excursion
608 (let ((modifiers (event-modifiers (aref key 0)))
609 (standard-output (if insert (current-buffer) t))
610 window position)
611 ;; For a mouse button event, go to the button it applies to
612 ;; to get the right key bindings. And go to the right place
613 ;; in case the keymap depends on where you clicked.
614 (if (or (memq 'click modifiers) (memq 'down modifiers)
615 (memq 'drag modifiers))
616 (setq window (posn-window (event-start (aref key 0)))
617 position (posn-point (event-start (aref key 0)))))
618 (if (windowp window)
619 (progn
620 (set-buffer (window-buffer window))
621 (goto-char position)))
622 ;; Ok, now look up the key and name the command.
623 (let ((defn (or (string-key-binding key)
624 (key-binding key t)))
625 key-desc)
626 ;; Handle the case where we faked an entry in "Select and Paste" menu.
627 (if (and (eq defn nil)
628 (stringp (aref key (1- (length key))))
629 (eq (key-binding (substring key 0 -1)) 'yank-menu))
630 (setq defn 'menu-bar-select-yank))
631 ;; Don't bother user with strings from (e.g.) the select-paste menu.
632 (if (stringp (aref key (1- (length key))))
633 (aset key (1- (length key)) "(any string)"))
634 (if (and (> (length untranslated) 0)
635 (stringp (aref untranslated (1- (length untranslated)))))
636 (aset untranslated (1- (length untranslated))
637 "(any string)"))
638 ;; Now describe the key, perhaps as changed.
639 (setq key-desc (help-key-description key untranslated))
640 (if (or (null defn) (integerp defn) (equal defn 'undefined))
641 (princ (format "%s is undefined" key-desc))
642 (princ (format (if (windowp window)
643 "%s at that spot runs the command %s"
644 "%s runs the command %s")
645 key-desc
646 (if (symbolp defn) defn (prin1-to-string defn)))))))))
647
648 (defun describe-key (&optional key untranslated up-event)
649 "Display documentation of the function invoked by KEY.
650 KEY can be any kind of a key sequence; it can include keyboard events,
651 mouse events, and/or menu events. When calling from a program,
652 pass KEY as a string or a vector.
653
654 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
655 It can also be a number, in which case the untranslated events from
656 the last key sequence entered are used.
657 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
658
659 If KEY is a menu item or a tool-bar button that is disabled, this command
660 temporarily enables it to allow getting help on disabled items and buttons."
661 (interactive
662 (let ((enable-disabled-menus-and-buttons t)
663 (cursor-in-echo-area t)
664 saved-yank-menu)
665 (unwind-protect
666 (let (key)
667 ;; If yank-menu is empty, populate it temporarily, so that
668 ;; "Select and Paste" menu can generate a complete event.
669 (when (null (cdr yank-menu))
670 (setq saved-yank-menu (copy-sequence yank-menu))
671 (menu-bar-update-yank-menu "(any string)" nil))
672 (setq key (read-key-sequence "Describe key (or click or menu item): "))
673 (list
674 key
675 (prefix-numeric-value current-prefix-arg)
676 ;; If KEY is a down-event, read the corresponding up-event
677 ;; and use it as the third argument.
678 (if (and (vectorp key)
679 (eventp (elt key 0))
680 (memq 'down (event-modifiers (elt key 0))))
681 (read-event))))
682 ;; Put yank-menu back as it was, if we changed it.
683 (when saved-yank-menu
684 (setq yank-menu (copy-sequence saved-yank-menu))
685 (fset 'yank-menu (cons 'keymap yank-menu))))))
686 (if (numberp untranslated)
687 (setq untranslated (this-single-command-raw-keys)))
688 (save-excursion
689 (let ((modifiers (event-modifiers (aref key 0)))
690 window position)
691 ;; For a mouse button event, go to the button it applies to
692 ;; to get the right key bindings. And go to the right place
693 ;; in case the keymap depends on where you clicked.
694 (if (or (memq 'click modifiers) (memq 'down modifiers)
695 (memq 'drag modifiers))
696 (setq window (posn-window (event-start (aref key 0)))
697 position (posn-point (event-start (aref key 0)))))
698 (when (windowp window)
699 (set-buffer (window-buffer window))
700 (goto-char position))
701 (let ((defn (or (string-key-binding key) (key-binding key t))))
702 ;; Handle the case where we faked an entry in "Select and Paste" menu.
703 (if (and (eq defn nil)
704 (stringp (aref key (1- (length key))))
705 (eq (key-binding (substring key 0 -1)) 'yank-menu))
706 (setq defn 'menu-bar-select-yank))
707 (if (or (null defn) (integerp defn) (equal defn 'undefined))
708 (message "%s is undefined" (help-key-description key untranslated))
709 (help-setup-xref (list #'describe-function defn) (interactive-p))
710 ;; Don't bother user with strings from (e.g.) the select-paste menu.
711 (if (stringp (aref key (1- (length key))))
712 (aset key (1- (length key)) "(any string)"))
713 (if (and untranslated
714 (stringp (aref untranslated (1- (length untranslated)))))
715 (aset untranslated (1- (length untranslated))
716 "(any string)"))
717 (with-output-to-temp-buffer (help-buffer)
718 (princ (help-key-description key untranslated))
719 (if (windowp window)
720 (princ " at that spot"))
721 (princ " runs the command ")
722 (prin1 defn)
723 (princ "\n which is ")
724 (describe-function-1 defn)
725 (when up-event
726 (let ((type (event-basic-type up-event))
727 (hdr "\n\n-------------- up event ---------------\n\n")
728 defn sequence
729 mouse-1-tricky mouse-1-remapped)
730 (setq sequence (vector up-event))
731 (when (and (eq type 'mouse-1)
732 (windowp window)
733 mouse-1-click-follows-link
734 (not (eq mouse-1-click-follows-link 'double))
735 (setq mouse-1-remapped
736 (with-current-buffer (window-buffer window)
737 (mouse-on-link-p (posn-point
738 (event-start up-event))))))
739 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
740 (> mouse-1-click-follows-link 0)))
741 (cond ((stringp mouse-1-remapped)
742 (setq sequence mouse-1-remapped))
743 ((vectorp mouse-1-remapped)
744 (setcar up-event (elt mouse-1-remapped 0)))
745 (t (setcar up-event 'mouse-2))))
746 (setq defn (or (string-key-binding sequence)
747 (key-binding sequence)))
748 (unless (or (null defn) (integerp defn) (equal defn 'undefined))
749 (princ (if mouse-1-tricky
750 "\n\n----------------- up-event (short click) ----------------\n\n"
751 hdr))
752 (setq hdr nil)
753 (princ (symbol-name type))
754 (if (windowp window)
755 (princ " at that spot"))
756 (if mouse-1-remapped
757 (princ " is remapped to <mouse-2>\n which" ))
758 (princ " runs the command ")
759 (prin1 defn)
760 (princ "\n which is ")
761 (describe-function-1 defn))
762 (when mouse-1-tricky
763 (setcar up-event 'mouse-1)
764 (setq defn (or (string-key-binding (vector up-event))
765 (key-binding (vector up-event))))
766 (unless (or (null defn) (integerp defn) (eq defn 'undefined))
767 (princ (or hdr
768 "\n\n----------------- up-event (long click) ----------------\n\n"))
769 (princ "Pressing mouse-1")
770 (if (windowp window)
771 (princ " at that spot"))
772 (princ (format " for longer than %d milli-seconds\n"
773 mouse-1-click-follows-link))
774 (princ " runs the command ")
775 (prin1 defn)
776 (princ "\n which is ")
777 (describe-function-1 defn)))))
778 (print-help-return-message)))))))
779 \f
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.
785
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 (interactive-p))
793 ;; For the sake of help-do-xref and help-xref-go-back,
794 ;; don't switch buffers before calling `help-buffer'.
795 (with-output-to-temp-buffer (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))
854
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))
861 (add-text-properties start (point) '(face bold)))))
862 (princ " mode:\n")
863 (princ (documentation major-mode)))
864 (print-help-return-message))))
865
866
867 (defun describe-minor-mode (minor-mode)
868 "Display documentation of a minor mode given as MINOR-MODE.
869 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
870 appeared on the mode-line."
871 (interactive (list (completing-read
872 "Minor mode: "
873 (nconc
874 (describe-minor-mode-completion-table-for-symbol)
875 (describe-minor-mode-completion-table-for-indicator)
876 ))))
877 (if (symbolp minor-mode)
878 (setq minor-mode (symbol-name minor-mode)))
879 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
880 (indicators (describe-minor-mode-completion-table-for-indicator)))
881 (cond
882 ((member minor-mode symbols)
883 (describe-minor-mode-from-symbol (intern minor-mode)))
884 ((member minor-mode indicators)
885 (describe-minor-mode-from-indicator minor-mode))
886 (t
887 (error "No such minor mode: %s" minor-mode)))))
888
889 ;; symbol
890 (defun describe-minor-mode-completion-table-for-symbol ()
891 ;; In order to list up all minor modes, minor-mode-list
892 ;; is used here instead of minor-mode-alist.
893 (delq nil (mapcar 'symbol-name minor-mode-list)))
894 (defun describe-minor-mode-from-symbol (symbol)
895 "Display documentation of a minor mode given as a symbol, SYMBOL"
896 (interactive (list (intern (completing-read
897 "Minor mode symbol: "
898 (describe-minor-mode-completion-table-for-symbol)))))
899 (if (fboundp symbol)
900 (describe-function symbol)
901 (describe-variable symbol)))
902
903 ;; indicator
904 (defun describe-minor-mode-completion-table-for-indicator ()
905 (delq nil
906 (mapcar (lambda (x)
907 (let ((i (format-mode-line x)))
908 ;; remove first space if existed
909 (cond
910 ((= 0 (length i))
911 nil)
912 ((eq (aref i 0) ?\ )
913 (substring i 1))
914 (t
915 i))))
916 minor-mode-alist)))
917 (defun describe-minor-mode-from-indicator (indicator)
918 "Display documentation of a minor mode specified by INDICATOR.
919 If you call this function interactively, you can give indicator which
920 is currently activated with completion."
921 (interactive (list
922 (completing-read
923 "Minor mode indicator: "
924 (describe-minor-mode-completion-table-for-indicator))))
925 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
926 (if minor-mode
927 (describe-minor-mode-from-symbol minor-mode)
928 (error "Cannot find minor mode for `%s'" indicator))))
929
930 (defun lookup-minor-mode-from-indicator (indicator)
931 "Return a minor mode symbol from its indicator on the modeline."
932 ;; remove first space if existed
933 (if (and (< 0 (length indicator))
934 (eq (aref indicator 0) ?\ ))
935 (setq indicator (substring indicator 1)))
936 (let ((minor-modes minor-mode-alist)
937 result)
938 (while minor-modes
939 (let* ((minor-mode (car (car minor-modes)))
940 (anindicator (format-mode-line
941 (car (cdr (car minor-modes))))))
942 ;; remove first space if existed
943 (if (and (stringp anindicator)
944 (> (length anindicator) 0)
945 (eq (aref anindicator 0) ?\ ))
946 (setq anindicator (substring anindicator 1)))
947 (if (equal indicator anindicator)
948 (setq result minor-mode
949 minor-modes nil)
950 (setq minor-modes (cdr minor-modes)))))
951 result))
952
953 \f
954 ;;; Automatic resizing of temporary buffers.
955
956 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
957 "Maximum height of a window displaying a temporary buffer.
958 This is effective only when Temp Buffer Resize mode is enabled.
959 The value is the maximum height (in lines) which `resize-temp-buffer-window'
960 will give to a window displaying a temporary buffer.
961 It can also be a function to be called to choose the height for such a buffer.
962 It gets one argumemt, the buffer, and should return a positive integer."
963 :type '(choice integer function)
964 :group 'help
965 :version "20.4")
966
967 (define-minor-mode temp-buffer-resize-mode
968 "Toggle the mode which makes windows smaller for temporary buffers.
969 With prefix argument ARG, turn the resizing of windows displaying temporary
970 buffers on if ARG is positive or off otherwise.
971 This makes the window the right height for its contents, but never
972 more than `temp-buffer-max-height' nor less than `window-min-height'.
973 This applies to `help', `apropos' and `completion' buffers, and some others."
974 :global t :group 'help
975 (if temp-buffer-resize-mode
976 ;; `help-make-xrefs' may add a `back' button and thus increase the
977 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
978 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
979 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
980
981 (defun resize-temp-buffer-window ()
982 "Resize the current window to fit its contents.
983 Will not make it higher than `temp-buffer-max-height' nor smaller than
984 `window-min-height'. Do nothing if it is the only window on its frame, if it
985 is not as wide as the frame or if some of the window's contents are scrolled
986 out of view."
987 (unless (or (one-window-p 'nomini)
988 (not (pos-visible-in-window-p (point-min)))
989 (/= (frame-width) (window-width)))
990 (fit-window-to-buffer
991 (selected-window)
992 (if (functionp temp-buffer-max-height)
993 (funcall temp-buffer-max-height (current-buffer))
994 temp-buffer-max-height))))
995
996 \f
997 (provide 'help)
998
999 ;; arch-tag: cf427352-27e9-49b7-9a6f-741ebab02423
1000 ;;; help.el ends here