(print-help-return-message): If pop-up-frames is non-nil, don't display
[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 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 \\[switch-to-buffer-other-window] 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 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. Give a list of words or a regexp, to get a list of
186 commands whose names match (they contain two or more of the words,
187 or a match for the regexp). See also the apropos command.
188 b describe-bindings. Display table of all key bindings.
189 c describe-key-briefly. Type a command key sequence;
190 it prints the function name that sequence runs.
191 C describe-coding-system. This describes either a specific coding system
192 (if you type its name) or the coding systems currently in use
193 (if you type just RET).
194 d apropos-documentation. Give a pattern (a list or words or a regexp), and
195 see a list of functions, variables, and other items whose built-in
196 doucmentation string matches that pattern. See also the apropos command.
197 e view-echo-area-messages. Show the buffer where the echo-area messages
198 are stored.
199 f describe-function. Type a function name and get its documentation.
200 F Info-goto-emacs-command-node. Type a function name;
201 it takes you to the on-line manual's section that describes
202 the command.
203 h Display the HELLO file which illustrates various scripts.
204 i info. The Info documentation reader: read on-line manuals.
205 I describe-input-method. Describe a specific input method (if you type
206 its name) or the current input method (if you type just RET).
207 k describe-key. Type a command key sequence;
208 it displays the full documentation for that key sequence.
209 K Info-goto-emacs-key-command-node. Type a command key sequence;
210 it takes you to the on-line manual's section that describes
211 the command bound to that key.
212 l view-lossage. Show last 100 characters you typed.
213 L describe-language-environment. This describes either a
214 specific language environment (if you type its name)
215 or the current language environment (if you type just RET).
216 m describe-mode. Display documentation of current minor modes,
217 and the current major mode, including their special commands.
218 n view-emacs-news. Display news of recent Emacs changes.
219 p finder-by-keyword. Find packages matching a given topic keyword.
220 s describe-syntax. Display contents of syntax table, plus explanations.
221 S info-lookup-symbol. Display the definition of a specific symbol
222 as found in the manual for the language this buffer is written in.
223 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
224 v describe-variable. Type name of a variable;
225 it displays the variable's documentation and value.
226 w where-is. Type command name; it prints which keystrokes
227 invoke that command.
228 . display-local-help. Display any available local help at point
229 in the echo area.
230
231 C-c Display Emacs copying permission (GNU General Public License).
232 C-d Display Emacs ordering information.
233 C-e Display info about Emacs problems.
234 C-f Display the Emacs FAQ.
235 C-m Display how to order printed Emacs manuals.
236 C-n Display news of recent Emacs changes.
237 C-p Display information about the GNU project.
238 C-t Display the Emacs TODO list.
239 C-w Display information on absence of warranty for GNU Emacs."
240 help-map)
241
242 \f
243
244 (defun function-called-at-point ()
245 "Return a function around point or else called by the list containing point.
246 If that doesn't give a function, return nil."
247 (or (with-syntax-table emacs-lisp-mode-syntax-table
248 (or (condition-case ()
249 (save-excursion
250 (or (not (zerop (skip-syntax-backward "_w")))
251 (eq (char-syntax (following-char)) ?w)
252 (eq (char-syntax (following-char)) ?_)
253 (forward-sexp -1))
254 (skip-chars-forward "'")
255 (let ((obj (read (current-buffer))))
256 (and (symbolp obj) (fboundp obj) obj)))
257 (error nil))
258 (condition-case ()
259 (save-excursion
260 (save-restriction
261 (narrow-to-region (max (point-min)
262 (- (point) 1000)) (point-max))
263 ;; Move up to surrounding paren, then after the open.
264 (backward-up-list 1)
265 (forward-char 1)
266 ;; If there is space here, this is probably something
267 ;; other than a real Lisp function call, so ignore it.
268 (if (looking-at "[ \t]")
269 (error "Probably not a Lisp function call"))
270 (let ((obj (read (current-buffer))))
271 (and (symbolp obj) (fboundp obj) obj))))
272 (error nil))))
273 (let* ((str (find-tag-default))
274 (sym (if str (intern-soft str))))
275 (if (and sym (fboundp sym))
276 sym
277 (save-match-data
278 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
279 (setq sym (intern-soft (match-string 1 str)))
280 (and (fboundp sym) sym)))))))
281
282 \f
283 ;;; `User' help functions
284
285 (defun describe-distribution ()
286 "Display info on how to obtain the latest version of GNU Emacs."
287 (interactive)
288 (view-file (expand-file-name "DISTRIB" data-directory)))
289
290 (defun describe-copying ()
291 "Display info on how you may redistribute copies of GNU Emacs."
292 (interactive)
293 (view-file (expand-file-name "COPYING" data-directory))
294 (goto-char (point-min)))
295
296 (defun describe-project ()
297 "Display info on the GNU project."
298 (interactive)
299 (view-file (expand-file-name "THE-GNU-PROJECT" data-directory))
300 (goto-char (point-min)))
301
302 (defun describe-no-warranty ()
303 "Display info on all the kinds of warranty Emacs does NOT have."
304 (interactive)
305 (describe-copying)
306 (let (case-fold-search)
307 (search-forward "NO WARRANTY")
308 (recenter 0)))
309
310 (defun describe-prefix-bindings ()
311 "Describe the bindings of the prefix used to reach this command.
312 The prefix described consists of all but the last event
313 of the key sequence that ran this command."
314 (interactive)
315 (let* ((key (this-command-keys)))
316 (describe-bindings
317 (if (stringp key)
318 (substring key 0 (1- (length key)))
319 (let ((prefix (make-vector (1- (length key)) nil))
320 (i 0))
321 (while (< i (length prefix))
322 (aset prefix i (aref key i))
323 (setq i (1+ i)))
324 prefix)))))
325 ;; Make C-h after a prefix, when not specifically bound,
326 ;; run describe-prefix-bindings.
327 (setq prefix-help-command 'describe-prefix-bindings)
328
329 (defun view-emacs-news (&optional arg)
330 "Display info on recent changes to Emacs.
331 With argument, display info only for the selected version."
332 (interactive "P")
333 (if (not arg)
334 (view-file (expand-file-name "NEWS" data-directory))
335 (let* ((map (sort
336 (delete-dups
337 (apply
338 'nconc
339 (mapcar
340 (lambda (file)
341 (with-temp-buffer
342 (insert-file-contents
343 (expand-file-name file data-directory))
344 (let (res)
345 (while (re-search-forward
346 (if (string-match "^ONEWS\\.[0-9]+$" file)
347 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
348 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
349 (setq res (cons (list (match-string-no-properties 1)
350 file) res)))
351 res)))
352 (append '("NEWS" "ONEWS")
353 (directory-files data-directory nil
354 "^ONEWS\\.[0-9]+$" nil)))))
355 (lambda (a b)
356 (string< (car b) (car a)))))
357 (current (caar map))
358 (version (completing-read
359 (format "Read NEWS for the version (default %s): " current)
360 (mapcar 'car map) nil nil nil nil current))
361 (file (cadr (assoc version map)))
362 res)
363 (if (not file)
364 (error "No news is good news")
365 (view-file (expand-file-name file data-directory))
366 (widen)
367 (goto-char (point-min))
368 (when (re-search-forward
369 (concat (if (string-match "^ONEWS\\.[0-9]+$" file)
370 "Changes in \\(?:Emacs\\|version\\)?[ \t]*"
371 "^\* [^0-9\n]*") version)
372 nil t)
373 (beginning-of-line)
374 (narrow-to-region
375 (point)
376 (save-excursion
377 (while (and (setq res
378 (re-search-forward
379 (if (string-match "^ONEWS\\.[0-9]+$" file)
380 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
381 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
382 (equal (match-string-no-properties 1) version)))
383 (or res (goto-char (point-max)))
384 (beginning-of-line)
385 (point))))))))
386
387 (defun view-todo (&optional arg)
388 "Display the Emacs TODO list."
389 (interactive "P")
390 (view-file (expand-file-name "TODO" data-directory)))
391
392 (defun view-echo-area-messages ()
393 "View the log of recent echo-area messages: the `*Messages*' buffer.
394 The number of messages retained in that buffer
395 is specified by the variable `message-log-max'."
396 (interactive)
397 (switch-to-buffer (get-buffer-create "*Messages*")))
398
399 (defun view-order-manuals ()
400 "Display the Emacs ORDERS file."
401 (interactive)
402 (view-file (expand-file-name "ORDERS" data-directory))
403 (goto-address))
404
405 (defun view-emacs-FAQ ()
406 "Display the Emacs Frequently Asked Questions (FAQ) file."
407 (interactive)
408 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
409 (info "(efaq)"))
410
411 (defun view-emacs-problems ()
412 "Display info on known problems with Emacs and possible workarounds."
413 (interactive)
414 (view-file (expand-file-name "PROBLEMS" data-directory)))
415
416 (defun view-lossage ()
417 "Display last 100 input keystrokes.
418
419 To record all your input on a file, use `open-dribble-file'."
420 (interactive)
421 (help-setup-xref (list #'view-lossage) (interactive-p))
422 (with-output-to-temp-buffer (help-buffer)
423 (princ (mapconcat (lambda (key)
424 (if (or (integerp key) (symbolp key) (listp key))
425 (single-key-description key)
426 (prin1-to-string key nil)))
427 (recent-keys)
428 " "))
429 (with-current-buffer standard-output
430 (goto-char (point-min))
431 (while (progn (move-to-column 50) (not (eobp)))
432 (search-forward " " nil t)
433 (insert "\n")))
434 (print-help-return-message)))
435
436 \f
437 ;; Key bindings
438
439 (defun describe-bindings (&optional prefix buffer)
440 "Show a list of all defined keys, and their definitions.
441 We put that list in a buffer, and display the buffer.
442
443 The optional argument PREFIX, if non-nil, should be a key sequence;
444 then we display only bindings that start with that prefix.
445 The optional argument BUFFER specifies which buffer's bindings
446 to display (default, the current buffer). BUFFER can be a buffer
447 or a buffer name."
448 (interactive)
449 (or buffer (setq buffer (current-buffer)))
450 (help-setup-xref (list #'describe-bindings prefix buffer) (interactive-p))
451 (with-current-buffer buffer
452 (describe-bindings-internal nil prefix)))
453
454 ;; This function used to be in keymap.c.
455 (defun describe-bindings-internal (&optional menus prefix)
456 "Show a list of all defined keys, and their definitions.
457 We put that list in a buffer, and display the buffer.
458
459 The optional argument MENUS, if non-nil, says to mention menu bindings.
460 \(Ordinarily these are omitted from the output.)
461 The optional argument PREFIX, if non-nil, should be a key sequence;
462 then we display only bindings that start with that prefix."
463 (interactive)
464 (let ((buf (current-buffer)))
465 (with-output-to-temp-buffer "*Help*"
466 (with-current-buffer standard-output
467 (describe-buffer-bindings buf prefix menus)))))
468
469 (defun where-is (definition &optional insert)
470 "Print message listing key sequences that invoke the command DEFINITION.
471 Argument is a command definition, usually a symbol with a function definition.
472 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
473 (interactive
474 (let ((fn (function-called-at-point))
475 (enable-recursive-minibuffers t)
476 val)
477 (setq val (completing-read
478 (if fn
479 (format "Where is command (default %s): " fn)
480 "Where is command: ")
481 obarray 'commandp t))
482 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
483 (let ((func (indirect-function definition))
484 (defs nil)
485 (standard-output (if insert (current-buffer) t)))
486 ;; In DEFS, find all symbols that are aliases for DEFINITION.
487 (mapatoms (lambda (symbol)
488 (and (fboundp symbol)
489 (not (eq symbol definition))
490 (eq func (condition-case ()
491 (indirect-function symbol)
492 (error symbol)))
493 (push symbol defs))))
494 ;; Look at all the symbols--first DEFINITION,
495 ;; then its aliases.
496 (dolist (symbol (cons definition defs))
497 (let* ((remapped (command-remapping symbol))
498 (keys (where-is-internal
499 symbol overriding-local-map nil nil remapped))
500 (keys (mapconcat 'key-description keys ", "))
501 string)
502 (setq string
503 (if insert
504 (if (> (length keys) 0)
505 (if remapped
506 (format "%s (%s) (remapped from %s)"
507 keys remapped symbol)
508 (format "%s (%s)" keys symbol))
509 (format "M-x %s RET" symbol))
510 (if (> (length keys) 0)
511 (if remapped
512 (format "%s is remapped to %s which is on %s"
513 definition symbol keys)
514 (format "%s is on %s" symbol keys))
515 ;; If this is the command the user asked about,
516 ;; and it is not on any key, say so.
517 ;; For other symbols, its aliases, say nothing
518 ;; about them unless they are on keys.
519 (if (eq symbol definition)
520 (format "%s is not on any key" symbol)))))
521 (when string
522 (unless (eq symbol definition)
523 (princ ";\n its alias "))
524 (princ string)))))
525 nil)
526
527 (defun string-key-binding (key)
528 "Value is the binding of KEY in a string.
529 If KEY is an event on a string, and that string has a `local-map'
530 or `keymap' property, return the binding of KEY in the string's keymap."
531 (let* ((defn nil)
532 (start (when (vectorp key)
533 (if (memq (aref key 0)
534 '(mode-line header-line left-margin right-margin))
535 (event-start (aref key 1))
536 (and (consp (aref key 0))
537 (event-start (aref key 0))))))
538 (string-info (and (consp start) (nth 4 start))))
539 (when string-info
540 (let* ((string (car string-info))
541 (pos (cdr string-info))
542 (local-map (and (>= pos 0)
543 (< pos (length string))
544 (or (get-text-property pos 'local-map string)
545 (get-text-property pos 'keymap string)))))
546 (setq defn (and local-map (lookup-key local-map key)))))
547 defn))
548
549 (defun help-key-description (key untranslated)
550 (let ((string (key-description key)))
551 (if (or (not untranslated)
552 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
553 string
554 (let ((otherstring (key-description untranslated)))
555 (if (equal string otherstring)
556 string
557 (format "%s (translated from %s)" string otherstring))))))
558
559 (defun describe-key-briefly (key &optional insert untranslated)
560 "Print the name of the function KEY invokes. KEY is a string.
561 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
562 If non-nil UNTRANSLATED is a vector of the untranslated events.
563 It can also be a number in which case the untranslated events from
564 the last key hit are used."
565 (interactive "kDescribe key briefly: \nP\np")
566 (if (numberp untranslated)
567 (setq untranslated (this-single-command-raw-keys)))
568 (save-excursion
569 (let ((modifiers (event-modifiers (aref key 0)))
570 (standard-output (if insert (current-buffer) t))
571 window position)
572 ;; For a mouse button event, go to the button it applies to
573 ;; to get the right key bindings. And go to the right place
574 ;; in case the keymap depends on where you clicked.
575 (if (or (memq 'click modifiers) (memq 'down modifiers)
576 (memq 'drag modifiers))
577 (setq window (posn-window (event-start (aref key 0)))
578 position (posn-point (event-start (aref key 0)))))
579 (if (windowp window)
580 (progn
581 (set-buffer (window-buffer window))
582 (goto-char position)))
583 ;; Ok, now look up the key and name the command.
584 (let ((defn (or (string-key-binding key)
585 (key-binding key t)))
586 key-desc)
587 ;; Don't bother user with strings from (e.g.) the select-paste menu.
588 (if (stringp (aref key (1- (length key))))
589 (aset key (1- (length key)) "(any string)"))
590 (if (and (> (length untranslated) 0)
591 (stringp (aref untranslated (1- (length untranslated)))))
592 (aset untranslated (1- (length untranslated))
593 "(any string)"))
594 ;; Now describe the key, perhaps as changed.
595 (setq key-desc (help-key-description key untranslated))
596 (if (or (null defn) (integerp defn) (equal defn 'undefined))
597 (princ (format "%s is undefined" key-desc))
598 (princ (format (if (windowp window)
599 "%s at that spot runs the command %s"
600 "%s runs the command %s")
601 key-desc
602 (if (symbolp defn) defn (prin1-to-string defn)))))))))
603
604 (defun describe-key (key &optional untranslated up-event)
605 "Display documentation of the function invoked by KEY.
606 KEY can be any kind of a key sequence; it can include keyboard events,
607 mouse events, and/or menu events. When calling from a program,
608 pass KEY as a string or a vector.
609
610 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
611 It can also be a number, in which case the untranslated events from
612 the last key sequence entered are used."
613 ;; UP-EVENT is the up-event that was discarded by reading KEY, or nil.
614 (interactive "kDescribe key (or click or menu item): \np\nU")
615 (if (numberp untranslated)
616 (setq untranslated (this-single-command-raw-keys)))
617 (save-excursion
618 (let ((modifiers (event-modifiers (aref key 0)))
619 window position)
620 ;; For a mouse button event, go to the button it applies to
621 ;; to get the right key bindings. And go to the right place
622 ;; in case the keymap depends on where you clicked.
623 (if (or (memq 'click modifiers) (memq 'down modifiers)
624 (memq 'drag modifiers))
625 (setq window (posn-window (event-start (aref key 0)))
626 position (posn-point (event-start (aref key 0)))))
627 (when (windowp window)
628 (set-buffer (window-buffer window))
629 (goto-char position))
630 (let ((defn (or (string-key-binding key) (key-binding key t))))
631 (if (or (null defn) (integerp defn) (equal defn 'undefined))
632 (message "%s is undefined" (help-key-description key untranslated))
633 (help-setup-xref (list #'describe-function defn) (interactive-p))
634 ;; Don't bother user with strings from (e.g.) the select-paste menu.
635 (if (stringp (aref key (1- (length key))))
636 (aset key (1- (length key)) "(any string)"))
637 (if (and untranslated
638 (stringp (aref untranslated (1- (length untranslated)))))
639 (aset untranslated (1- (length untranslated))
640 "(any string)"))
641 (with-output-to-temp-buffer (help-buffer)
642 (princ (help-key-description key untranslated))
643 (if (windowp window)
644 (princ " at that spot"))
645 (princ " runs the command ")
646 (prin1 defn)
647 (princ "\n which is ")
648 (describe-function-1 defn)
649 (when up-event
650 (let ((ev (aref up-event 0))
651 (descr (key-description up-event))
652 (hdr "\n\n-------------- up event ---------------\n\n")
653 defn
654 mouse-1-tricky mouse-1-remapped)
655 (when (and (consp ev)
656 (eq (car ev) 'mouse-1)
657 (windowp window)
658 mouse-1-click-follows-link
659 (not (eq mouse-1-click-follows-link 'double))
660 (with-current-buffer (window-buffer window)
661 (mouse-on-link-p (posn-point (event-start ev)))))
662 (setq mouse-1-tricky (integerp mouse-1-click-follows-link)
663 mouse-1-remapped (or (not mouse-1-tricky)
664 (> mouse-1-click-follows-link 0)))
665 (if mouse-1-remapped
666 (setcar ev 'mouse-2)))
667 (setq defn (or (string-key-binding up-event) (key-binding up-event)))
668 (unless (or (null defn) (integerp defn) (equal defn 'undefined))
669 (princ (if mouse-1-tricky
670 "\n\n----------------- up-event (short click) ----------------\n\n"
671 hdr))
672 (setq hdr nil)
673 (princ descr)
674 (if (windowp window)
675 (princ " at that spot"))
676 (if mouse-1-remapped
677 (princ " is remapped to <mouse-2>\n which" ))
678 (princ " runs the command ")
679 (prin1 defn)
680 (princ "\n which is ")
681 (describe-function-1 defn))
682 (when mouse-1-tricky
683 (setcar ev
684 (if (> mouse-1-click-follows-link 0) 'mouse-1 'mouse-2))
685 (setq defn (or (string-key-binding up-event) (key-binding up-event)))
686 (unless (or (null defn) (integerp defn) (equal defn 'undefined))
687 (princ (or hdr
688 "\n\n----------------- up-event (long click) ----------------\n\n"))
689 (princ "Pressing ")
690 (princ descr)
691 (if (windowp window)
692 (princ " at that spot"))
693 (princ (format " for longer than %d milli-seconds\n"
694 (abs mouse-1-click-follows-link)))
695 (if (not mouse-1-remapped)
696 (princ " remaps it to <mouse-2> which" ))
697 (princ " runs the command ")
698 (prin1 defn)
699 (princ "\n which is ")
700 (describe-function-1 defn)))))
701 (print-help-return-message)))))))
702
703 \f
704 (defun describe-mode (&optional buffer)
705 "Display documentation of current major mode and minor modes.
706 A brief summary of the minor modes comes first, followed by the
707 major mode description. This is followed by detailed
708 descriptions of the minor modes, each on a separate page.
709
710 For this to work correctly for a minor mode, the mode's indicator
711 variable \(listed in `minor-mode-alist') must also be a function
712 whose documentation describes the minor mode."
713 (interactive)
714 (unless buffer (setq buffer (current-buffer)))
715 (help-setup-xref (list #'describe-mode buffer)
716 (interactive-p))
717 ;; For the sake of help-do-xref and help-xref-go-back,
718 ;; don't switch buffers before calling `help-buffer'.
719 (with-output-to-temp-buffer (help-buffer)
720 (with-current-buffer buffer
721 (let (minor-modes)
722 ;; Older packages do not register in minor-mode-list but only in
723 ;; minor-mode-alist.
724 (dolist (x minor-mode-alist)
725 (setq x (car x))
726 (unless (memq x minor-mode-list)
727 (push x minor-mode-list)))
728 ;; Find enabled minor mode we will want to mention.
729 (dolist (mode minor-mode-list)
730 ;; Document a minor mode if it is listed in minor-mode-alist,
731 ;; non-nil, and has a function definition.
732 (let ((fmode (or (get mode :minor-mode-function) mode)))
733 (and (boundp mode) (symbol-value mode)
734 (fboundp fmode)
735 (let ((pretty-minor-mode
736 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
737 (symbol-name fmode))
738 (capitalize
739 (substring (symbol-name fmode)
740 0 (match-beginning 0)))
741 fmode)))
742 (push (list fmode pretty-minor-mode
743 (format-mode-line (assq mode minor-mode-alist)))
744 minor-modes)))))
745 (setq minor-modes
746 (sort minor-modes
747 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
748 (when minor-modes
749 (princ "Summary of minor modes:\n")
750 (make-local-variable 'help-button-cache)
751 (with-current-buffer standard-output
752 (dolist (mode minor-modes)
753 (let ((mode-function (nth 0 mode))
754 (pretty-minor-mode (nth 1 mode))
755 (indicator (nth 2 mode)))
756 (setq indicator (if (zerop (length indicator))
757 "no indicator"
758 (format "indicator%s" indicator)))
759 (add-text-properties 0 (length pretty-minor-mode)
760 '(face bold) pretty-minor-mode)
761 (save-excursion
762 (goto-char (point-max))
763 (princ "\n\f\n")
764 (push (point-marker) help-button-cache)
765 ;; Document the minor modes fully.
766 (insert pretty-minor-mode)
767 (princ (format " minor mode (%s):\n" indicator))
768 (princ (documentation mode-function)))
769 (princ " ")
770 (insert-button pretty-minor-mode
771 'action (car help-button-cache)
772 'follow-link t
773 'help-echo "mouse-2, RET: show full information")
774 (princ (format " minor mode (%s):\n" indicator)))))
775 (princ "\n(Full information about these minor modes
776 follows the description of the major mode.)\n\n"))
777 ;; Document the major mode.
778 (let ((mode mode-name))
779 (with-current-buffer standard-output
780 (insert mode)
781 (add-text-properties (- (point) (length mode)) (point) '(face bold))))
782 (princ " mode:\n")
783 (princ (documentation major-mode)))
784 (print-help-return-message))))
785
786
787 (defun describe-minor-mode (minor-mode)
788 "Display documentation of a minor mode given as MINOR-MODE.
789 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
790 appeared on the mode-line."
791 (interactive (list (completing-read
792 "Minor mode: "
793 (nconc
794 (describe-minor-mode-completion-table-for-symbol)
795 (describe-minor-mode-completion-table-for-indicator)
796 ))))
797 (if (symbolp minor-mode)
798 (setq minor-mode (symbol-name minor-mode)))
799 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
800 (indicators (describe-minor-mode-completion-table-for-indicator)))
801 (cond
802 ((member minor-mode symbols)
803 (describe-minor-mode-from-symbol (intern minor-mode)))
804 ((member minor-mode indicators)
805 (describe-minor-mode-from-indicator minor-mode))
806 (t
807 (error "No such minor mode: %s" minor-mode)))))
808
809 ;; symbol
810 (defun describe-minor-mode-completion-table-for-symbol ()
811 ;; In order to list up all minor modes, minor-mode-list
812 ;; is used here instead of minor-mode-alist.
813 (delq nil (mapcar 'symbol-name minor-mode-list)))
814 (defun describe-minor-mode-from-symbol (symbol)
815 "Display documentation of a minor mode given as a symbol, SYMBOL"
816 (interactive (list (intern (completing-read
817 "Minor mode symbol: "
818 (describe-minor-mode-completion-table-for-symbol)))))
819 (if (fboundp symbol)
820 (describe-function symbol)
821 (describe-variable symbol)))
822
823 ;; indicator
824 (defun describe-minor-mode-completion-table-for-indicator ()
825 (delq nil
826 (mapcar (lambda (x)
827 (let ((i (format-mode-line x)))
828 ;; remove first space if existed
829 (cond
830 ((= 0 (length i))
831 nil)
832 ((eq (aref i 0) ?\ )
833 (substring i 1))
834 (t
835 i))))
836 minor-mode-alist)))
837 (defun describe-minor-mode-from-indicator (indicator)
838 "Display documentation of a minor mode specified by INDICATOR.
839 If you call this function interactively, you can give indicator which
840 is currently activated with completion."
841 (interactive (list
842 (completing-read
843 "Minor mode indicator: "
844 (describe-minor-mode-completion-table-for-indicator))))
845 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
846 (if minor-mode
847 (describe-minor-mode-from-symbol minor-mode)
848 (error "Cannot find minor mode for `%s'" indicator))))
849
850 (defun lookup-minor-mode-from-indicator (indicator)
851 "Return a minor mode symbol from its indicator on the modeline."
852 ;; remove first space if existed
853 (if (and (< 0 (length indicator))
854 (eq (aref indicator 0) ?\ ))
855 (setq indicator (substring indicator 1)))
856 (let ((minor-modes minor-mode-alist)
857 result)
858 (while minor-modes
859 (let* ((minor-mode (car (car minor-modes)))
860 (anindicator (format-mode-line
861 (car (cdr (car minor-modes))))))
862 ;; remove first space if existed
863 (if (and (stringp anindicator)
864 (> (length anindicator) 0)
865 (eq (aref anindicator 0) ?\ ))
866 (setq anindicator (substring anindicator 1)))
867 (if (equal indicator anindicator)
868 (setq result minor-mode
869 minor-modes nil)
870 (setq minor-modes (cdr minor-modes)))))
871 result))
872
873 \f
874 ;;; Automatic resizing of temporary buffers.
875
876 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
877 "*Maximum height of a window displaying a temporary buffer.
878 This is the maximum height (in text lines) which `resize-temp-buffer-window'
879 will give to a window displaying a temporary buffer.
880 It can also be a function which will be called with the object corresponding
881 to the buffer to be displayed as argument and should return an integer
882 positive number."
883 :type '(choice integer function)
884 :group 'help
885 :version "20.4")
886
887 (define-minor-mode temp-buffer-resize-mode
888 "Toggle the mode which makes windows smaller for temporary buffers.
889 With prefix argument ARG, turn the resizing of windows displaying temporary
890 buffers on if ARG is positive or off otherwise.
891 This makes the window the right height for its contents, but never
892 more than `temp-buffer-max-height' nor less than `window-min-height'.
893 This applies to `help', `apropos' and `completion' buffers, and some others."
894 :global t :group 'help
895 (if temp-buffer-resize-mode
896 ;; `help-make-xrefs' may add a `back' button and thus increase the
897 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
898 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
899 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
900
901 (defun resize-temp-buffer-window ()
902 "Resize the current window to fit its contents.
903 Will not make it higher than `temp-buffer-max-height' nor smaller than
904 `window-min-height'. Do nothing if it is the only window on its frame, if it
905 is not as wide as the frame or if some of the window's contents are scrolled
906 out of view."
907 (unless (or (one-window-p 'nomini)
908 (not (pos-visible-in-window-p (point-min)))
909 (/= (frame-width) (window-width)))
910 (fit-window-to-buffer
911 (selected-window)
912 (if (functionp temp-buffer-max-height)
913 (funcall temp-buffer-max-height (current-buffer))
914 temp-buffer-max-height))))
915
916 ;; Provide this for the sake of define-minor-mode which generates
917 ;; defcustoms which require 'help'.
918 (provide 'help)
919
920 ;; arch-tag: cf427352-27e9-49b7-9a6f-741ebab02423
921 ;;; help.el ends here