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