Sync to HEAD
[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 ;; 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., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, 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 (make-sparse-keymap)
42 "Keymap for characters following the Help key.")
43
44 (define-key global-map (char-to-string help-char) 'help-command)
45 (define-key global-map [help] 'help-command)
46 (define-key global-map [f1] 'help-command)
47 (fset 'help-command help-map)
48
49 (define-key help-map (char-to-string help-char) 'help-for-help)
50 (define-key help-map [help] 'help-for-help)
51 (define-key help-map [f1] 'help-for-help)
52 (define-key help-map "." 'display-local-help)
53 (define-key help-map "?" 'help-for-help)
54
55 (define-key help-map "\C-c" 'describe-copying)
56 (define-key help-map "\C-d" 'describe-distribution)
57 (define-key help-map "\C-e" 'view-emacs-problems)
58 (define-key help-map "\C-f" 'view-emacs-FAQ)
59 (define-key help-map "\C-m" 'view-order-manuals)
60 (define-key help-map "\C-n" 'view-emacs-news)
61 (define-key help-map "\C-p" 'describe-project)
62 (define-key help-map "\C-t" 'view-todo)
63 (define-key help-map "\C-w" 'describe-no-warranty)
64
65 ;; This does not fit the pattern, but it is natural given the C-\ command.
66 (define-key help-map "\C-\\" 'describe-input-method)
67
68 (define-key help-map "C" 'describe-coding-system)
69 (define-key help-map "F" 'Info-goto-emacs-command-node)
70 (define-key help-map "I" 'describe-input-method)
71 (define-key help-map "K" 'Info-goto-emacs-key-command-node)
72 (define-key help-map "L" 'describe-language-environment)
73 (define-key help-map "S" 'info-lookup-symbol)
74
75 (define-key help-map "a" 'apropos-command)
76
77 (define-key help-map "b" 'describe-bindings)
78
79 (define-key help-map "c" 'describe-key-briefly)
80
81 (define-key help-map "e" 'view-echo-area-messages)
82
83 (define-key help-map "f" 'describe-function)
84
85 (define-key help-map "h" 'view-hello-file)
86
87 (define-key help-map "i" 'info)
88 (define-key help-map "4i" 'info-other-window)
89
90 (define-key help-map "k" 'describe-key)
91
92 (define-key help-map "l" 'view-lossage)
93
94 (define-key help-map "m" 'describe-mode)
95
96 (define-key help-map "n" 'view-emacs-news)
97
98 (define-key help-map "p" 'finder-by-keyword)
99 (autoload 'finder-by-keyword "finder"
100 "Find packages matching a given keyword." t)
101
102 (define-key help-map "r" 'info-emacs-manual)
103
104 (define-key help-map "s" 'describe-syntax)
105
106 (define-key help-map "t" 'help-with-tutorial)
107
108 (define-key help-map "w" 'where-is)
109
110 (define-key help-map "v" 'describe-variable)
111
112 (define-key help-map "q" 'help-quit)
113
114 \f
115 (defun help-quit ()
116 "Just exit from the Help command's command loop."
117 (interactive)
118 nil)
119
120 (defvar help-return-method nil
121 "What to do to \"exit\" the help buffer.
122 This is a list
123 (WINDOW . t) delete the selected window, go to WINDOW.
124 (WINDOW . quit-window) do quit-window, then select WINDOW.
125 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
126
127 (defun print-help-return-message (&optional function)
128 "Display or return message saying how to restore windows after help command.
129 This function assumes that `standard-output' is the help buffer.
130 It computes a message, and applies the optional argument FUNCTION to it.
131 If FUNCTION is nil, it applies `message', thus displaying the message."
132 (and (not (get-buffer-window standard-output))
133 (let ((first-message
134 (cond ((special-display-p (buffer-name standard-output))
135 (setq help-return-method (cons (selected-window) t))
136 ;; If the help output buffer is a special display buffer,
137 ;; don't say anything about how to get rid of it.
138 ;; First of all, the user will do that with the window
139 ;; manager, not with Emacs.
140 ;; Secondly, the buffer has not been displayed yet,
141 ;; so we don't know whether its frame will be selected.
142 nil)
143 (display-buffer-reuse-frames
144 (setq help-return-method (cons (selected-window)
145 'quit-window))
146 nil)
147 ((not (one-window-p t))
148 (setq help-return-method
149 (cons (selected-window) 'quit-window))
150 "Type \\[switch-to-buffer-other-window] RET to restore the other window.")
151 (pop-up-windows
152 (setq help-return-method (cons (selected-window) t))
153 "Type \\[delete-other-windows] to remove help window.")
154 (t
155 (setq help-return-method
156 (list (selected-window) (window-buffer)
157 (window-start) (window-point)))
158 "Type \\[switch-to-buffer] RET to remove help window."))))
159 (funcall (or function 'message)
160 (concat
161 (if first-message
162 (substitute-command-keys first-message))
163 (if first-message " ")
164 ;; If the help buffer will go in a separate frame,
165 ;; it's no use mentioning a command to scroll, so don't.
166 (if (special-display-p (buffer-name standard-output))
167 nil
168 (if (same-window-p (buffer-name standard-output))
169 ;; Say how to scroll this window.
170 (substitute-command-keys
171 "\\[scroll-up] to scroll the help.")
172 ;; Say how to scroll some other window.
173 (substitute-command-keys
174 "\\[scroll-other-window] to scroll the help."))))))))
175
176 ;; So keyboard macro definitions are documented correctly
177 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
178
179 (defalias 'help 'help-for-help)
180 (make-help-screen help-for-help
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 substring, and see a list of commands
186 (functions interactively callable) that contain
187 that substring. 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 e view-echo-area-messages. Show the `*Messages*' buffer.
195 f describe-function. Type a function name and get documentation of it.
196 F Info-goto-emacs-command-node. Type a function name;
197 it takes you to the Info node for that command.
198 h Display the HELLO file which illustrates various scripts.
199 i info. The info documentation reader.
200 I describe-input-method. Describe a specific input method (if you type
201 its name) or the current input method (if you type just RET).
202 k describe-key. Type a command key sequence;
203 it displays the full documentation.
204 K Info-goto-emacs-key-command-node. Type a command key sequence;
205 it takes you to the Info node for the command bound to that key.
206 l view-lossage. Show last 100 characters you typed.
207 L describe-language-environment. This describes either a
208 specific language environment (if you type its name)
209 or the current language environment (if you type just RET).
210 m describe-mode. Print documentation of current minor modes,
211 and the current major mode, including their special commands.
212 p finder-by-keyword. Find packages matching a given topic keyword.
213 s describe-syntax. Display contents of syntax table, plus explanations.
214 S info-lookup-symbol. Display the definition of a specific symbol
215 as found in the manual for the language this buffer is written in.
216 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
217 v describe-variable. Type name of a variable;
218 it displays the variable's documentation and value.
219 w where-is. Type command name; it prints which keystrokes
220 invoke that command.
221 . display-local-help. Display any available local help at point
222 in the echo area.
223
224 C-c Display Emacs copying permission (GNU General Public License).
225 C-d Display Emacs ordering information.
226 C-e Display info about Emacs problems.
227 C-f Display the Emacs FAQ.
228 C-m Display how to order printed Emacs manuals.
229 C-n Display news of recent Emacs changes.
230 C-p Display information about the GNU project.
231 C-t Display the Emacs TODO list.
232 C-w Display information on absence of warranty for GNU Emacs."
233 help-map)
234
235 \f
236
237 (defun function-called-at-point ()
238 "Return a function around point or else called by the list containing point.
239 If that doesn't give a function, return nil."
240 (with-syntax-table emacs-lisp-mode-syntax-table
241 (or (condition-case ()
242 (save-excursion
243 (or (not (zerop (skip-syntax-backward "_w")))
244 (eq (char-syntax (following-char)) ?w)
245 (eq (char-syntax (following-char)) ?_)
246 (forward-sexp -1))
247 (skip-chars-forward "'")
248 (let ((obj (read (current-buffer))))
249 (and (symbolp obj) (fboundp obj) obj)))
250 (error nil))
251 (condition-case ()
252 (save-excursion
253 (save-restriction
254 (narrow-to-region (max (point-min)
255 (- (point) 1000)) (point-max))
256 ;; Move up to surrounding paren, then after the open.
257 (backward-up-list 1)
258 (forward-char 1)
259 ;; If there is space here, this is probably something
260 ;; other than a real Lisp function call, so ignore it.
261 (if (looking-at "[ \t]")
262 (error "Probably not a Lisp function call"))
263 (let ((obj (read (current-buffer))))
264 (and (symbolp obj) (fboundp obj) obj))))
265 (error nil)))))
266
267 \f
268 ;;; `User' help functions
269
270 (defun describe-distribution ()
271 "Display info on how to obtain the latest version of GNU Emacs."
272 (interactive)
273 (view-file (expand-file-name "DISTRIB" data-directory)))
274
275 (defun describe-copying ()
276 "Display info on how you may redistribute copies of GNU Emacs."
277 (interactive)
278 (view-file (expand-file-name "COPYING" data-directory))
279 (goto-char (point-min)))
280
281 (defun describe-project ()
282 "Display info on the GNU project."
283 (interactive)
284 (view-file (expand-file-name "THE-GNU-PROJECT" data-directory))
285 (goto-char (point-min)))
286
287 (defun describe-no-warranty ()
288 "Display info on all the kinds of warranty Emacs does NOT have."
289 (interactive)
290 (describe-copying)
291 (let (case-fold-search)
292 (search-forward "NO WARRANTY")
293 (recenter 0)))
294
295 (defun describe-prefix-bindings ()
296 "Describe the bindings of the prefix used to reach this command.
297 The prefix described consists of all but the last event
298 of the key sequence that ran this command."
299 (interactive)
300 (let* ((key (this-command-keys)))
301 (describe-bindings
302 (if (stringp key)
303 (substring key 0 (1- (length key)))
304 (let ((prefix (make-vector (1- (length key)) nil))
305 (i 0))
306 (while (< i (length prefix))
307 (aset prefix i (aref key i))
308 (setq i (1+ i)))
309 prefix)))))
310 ;; Make C-h after a prefix, when not specifically bound,
311 ;; run describe-prefix-bindings.
312 (setq prefix-help-command 'describe-prefix-bindings)
313
314 (defun view-emacs-news (&optional arg)
315 "Display info on recent changes to Emacs.
316 With numeric argument, display information on correspondingly older changes."
317 (interactive "P")
318 (let* ((arg (if arg (prefix-numeric-value arg) 0))
319 (file (cond ((eq arg 0) "NEWS")
320 ((eq arg 1) "ONEWS")
321 (t
322 (nth (- arg 2)
323 (nreverse (directory-files data-directory
324 nil "^ONEWS\\.[0-9]+$"
325 nil)))))))
326 (if file
327 (view-file (expand-file-name file data-directory))
328 (error "No such old news"))))
329
330 (defun view-todo (&optional arg)
331 "Display the Emacs TODO list."
332 (interactive "P")
333 (view-file (expand-file-name "TODO" data-directory)))
334
335 (defun view-echo-area-messages ()
336 "View the log of recent echo-area messages: the `*Messages*' buffer.
337 The number of messages retained in that buffer
338 is specified by the variable `message-log-max'."
339 (interactive)
340 (switch-to-buffer (get-buffer-create "*Messages*")))
341
342 (defun view-order-manuals ()
343 "Display the Emacs ORDERS file."
344 (interactive)
345 (view-file (expand-file-name "ORDERS" data-directory))
346 (goto-address))
347
348 (defun view-emacs-FAQ ()
349 "Display the Emacs Frequently Asked Questions (FAQ) file."
350 (interactive)
351 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
352 (info "(efaq)"))
353
354 (defun view-emacs-problems ()
355 "Display info on known problems with Emacs and possible workarounds."
356 (interactive)
357 (view-file (expand-file-name "PROBLEMS" data-directory)))
358
359 (defun view-lossage ()
360 "Display last 100 input keystrokes.
361
362 To record all your input on a file, use `open-dribble-file'."
363 (interactive)
364 (help-setup-xref (list #'view-lossage) (interactive-p))
365 (with-output-to-temp-buffer (help-buffer)
366 (princ (mapconcat (lambda (key)
367 (if (or (integerp key) (symbolp key) (listp key))
368 (single-key-description key)
369 (prin1-to-string key nil)))
370 (recent-keys)
371 " "))
372 (with-current-buffer standard-output
373 (goto-char (point-min))
374 (while (progn (move-to-column 50) (not (eobp)))
375 (search-forward " " nil t)
376 (insert "\n")))
377 (print-help-return-message)))
378
379 \f
380 ;; Key bindings
381
382 (defun describe-bindings (&optional prefix buffer)
383 "Show a list of all defined keys, and their definitions.
384 We put that list in a buffer, and display the buffer.
385
386 The optional argument PREFIX, if non-nil, should be a key sequence;
387 then we display only bindings that start with that prefix.
388 The optional argument BUFFER specifies which buffer's bindings
389 to display (default, the current buffer)."
390 (interactive)
391 (or buffer (setq buffer (current-buffer)))
392 (help-setup-xref (list #'describe-bindings prefix buffer) (interactive-p))
393 (with-current-buffer buffer
394 (describe-bindings-internal nil prefix)))
395
396 ;; This function used to be in keymap.c.
397 (defun describe-bindings-internal (&optional menus prefix)
398 "Show a list of all defined keys, and their definitions.
399 We put that list in a buffer, and display the buffer.
400
401 The optional argument MENUS, if non-nil, says to mention menu bindings.
402 \(Ordinarily these are omitted from the output.)
403 The optional argument PREFIX, if non-nil, should be a key sequence;
404 then we display only bindings that start with that prefix."
405 (interactive)
406 (let ((buf (current-buffer)))
407 (with-output-to-temp-buffer "*Help*"
408 (with-current-buffer standard-output
409 (describe-buffer-bindings buf prefix menus)))))
410
411 (defun where-is (definition &optional insert)
412 "Print message listing key sequences that invoke the command DEFINITION.
413 Argument is a command definition, usually a symbol with a function definition.
414 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
415 (interactive
416 (let ((fn (function-called-at-point))
417 (enable-recursive-minibuffers t)
418 val)
419 (setq val (completing-read
420 (if fn
421 (format "Where is command (default %s): " fn)
422 "Where is command: ")
423 obarray 'commandp t))
424 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
425 (let ((func (indirect-function definition))
426 (defs nil)
427 (standard-output (if insert (current-buffer) t)))
428 (mapatoms (lambda (symbol)
429 (and (fboundp symbol)
430 (not (eq symbol definition))
431 (eq func (condition-case ()
432 (indirect-function symbol)
433 (error symbol)))
434 (push symbol defs))))
435 (princ (mapconcat
436 #'(lambda (symbol)
437 (let* ((remapped (command-remapping symbol))
438 (keys (where-is-internal
439 symbol overriding-local-map nil nil remapped))
440 (keys (mapconcat 'key-description keys ", ")))
441 (if insert
442 (if (> (length keys) 0)
443 (if remapped
444 (format "%s (%s) (remapped from %s)"
445 keys remapped symbol)
446 (format "%s (%s)" keys symbol))
447 (format "M-x %s RET" symbol))
448 (if (> (length keys) 0)
449 (if remapped
450 (format "%s is remapped to %s which is on %s"
451 definition symbol keys)
452 (format "%s is on %s" symbol keys))
453 (format "%s is not on any key" symbol)))))
454 (cons definition defs)
455 ";\nand ")))
456 nil)
457
458 (defun string-key-binding (key)
459 "Value is the binding of KEY in a string.
460 If KEY is an event on a string, and that string has a `local-map'
461 or `keymap' property, return the binding of KEY in the string's keymap."
462 (let* ((defn nil)
463 (start (when (vectorp key)
464 (if (memq (aref key 0)
465 '(mode-line header-line left-margin right-margin))
466 (event-start (aref key 1))
467 (and (consp (aref key 0))
468 (event-start (aref key 0))))))
469 (string-info (and (consp start) (nth 4 start))))
470 (when string-info
471 (let* ((string (car string-info))
472 (pos (cdr string-info))
473 (local-map (and (>= pos 0)
474 (< pos (length string))
475 (or (get-text-property pos 'local-map string)
476 (get-text-property pos 'keymap string)))))
477 (setq defn (and local-map (lookup-key local-map key)))))
478 defn))
479
480 (defun help-key-description (key untranslated)
481 (let ((string (key-description key)))
482 (if (or (not untranslated)
483 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
484 string
485 (let ((otherstring (key-description untranslated)))
486 (if (equal string otherstring)
487 string
488 (format "%s (translated from %s)" string otherstring))))))
489
490 (defun describe-key-briefly (key &optional insert untranslated)
491 "Print the name of the function KEY invokes. KEY is a string.
492 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
493 If non-nil UNTRANSLATED is a vector of the untranslated events.
494 It can also be a number in which case the untranslated events from
495 the last key hit are used."
496 (interactive "kDescribe key briefly: \nP\np")
497 (if (numberp untranslated)
498 (setq untranslated (this-single-command-raw-keys)))
499 (save-excursion
500 (let ((modifiers (event-modifiers (aref key 0)))
501 (standard-output (if insert (current-buffer) t))
502 window position)
503 ;; For a mouse button event, go to the button it applies to
504 ;; to get the right key bindings. And go to the right place
505 ;; in case the keymap depends on where you clicked.
506 (if (or (memq 'click modifiers) (memq 'down modifiers)
507 (memq 'drag modifiers))
508 (setq window (posn-window (event-start (aref key 0)))
509 position (posn-point (event-start (aref key 0)))))
510 (if (windowp window)
511 (progn
512 (set-buffer (window-buffer window))
513 (goto-char position)))
514 ;; Ok, now look up the key and name the command.
515 (let ((defn (or (string-key-binding key)
516 (key-binding key)))
517 (key-desc (help-key-description key untranslated)))
518 (if (or (null defn) (integerp defn) (equal defn 'undefined))
519 (princ (format "%s is undefined" key-desc))
520 (princ (format (if (windowp window)
521 "%s at that spot runs the command %s"
522 "%s runs the command %s")
523 key-desc
524 (if (symbolp defn) defn (prin1-to-string defn)))))))))
525
526
527 (defun describe-key (key &optional untranslated)
528 "Display documentation of the function invoked by KEY.
529 KEY should be a key sequence--when calling from a program,
530 pass a string or a vector.
531 If non-nil UNTRANSLATED is a vector of the untranslated events.
532 It can also be a number in which case the untranslated events from
533 the last key hit are used."
534 (interactive "kDescribe key: \np")
535 (if (numberp untranslated)
536 (setq untranslated (this-single-command-raw-keys)))
537 (save-excursion
538 (let ((modifiers (event-modifiers (aref key 0)))
539 window position)
540 ;; For a mouse button event, go to the button it applies to
541 ;; to get the right key bindings. And go to the right place
542 ;; in case the keymap depends on where you clicked.
543 (if (or (memq 'click modifiers) (memq 'down modifiers)
544 (memq 'drag modifiers))
545 (setq window (posn-window (event-start (aref key 0)))
546 position (posn-point (event-start (aref key 0)))))
547 (when (windowp window)
548 (set-buffer (window-buffer window))
549 (goto-char position))
550 (let ((defn (or (string-key-binding key) (key-binding key))))
551 (if (or (null defn) (integerp defn) (equal defn 'undefined))
552 (message "%s is undefined" (help-key-description key untranslated))
553 (help-setup-xref (list #'describe-function defn) (interactive-p))
554 (with-output-to-temp-buffer (help-buffer)
555 (princ (help-key-description key untranslated))
556 (if (windowp window)
557 (princ " at that spot"))
558 (princ " runs the command ")
559 (prin1 defn)
560 (princ "\n which is ")
561 (describe-function-1 defn)
562 (print-help-return-message)))))))
563
564 \f
565 (defun describe-mode (&optional buffer)
566 "Display documentation of current major mode and minor modes.
567 A brief summary of the minor modes comes first, followed by the
568 major mode description. This is followed by detailed
569 descriptions of the minor modes, each on a separate page.
570
571 For this to work correctly for a minor mode, the mode's indicator
572 variable \(listed in `minor-mode-alist') must also be a function
573 whose documentation describes the minor mode."
574 (interactive)
575 (help-setup-xref (list #'describe-mode (or buffer (current-buffer)))
576 (interactive-p))
577 ;; For the sake of help-do-xref and help-xref-go-back,
578 ;; don't switch buffers before calling `help-buffer'.
579 (with-output-to-temp-buffer (help-buffer)
580 (save-excursion
581 (when buffer (set-buffer buffer))
582 (let (minor-modes)
583 ;; Find enabled minor mode we will want to mention.
584 (dolist (mode minor-mode-list)
585 ;; Document a minor mode if it is listed in minor-mode-alist,
586 ;; non-nil, and has a function definition.
587 (and (boundp mode) (symbol-value mode)
588 (fboundp mode)
589 (let ((pretty-minor-mode mode)
590 indicator)
591 (if (string-match "\\(-minor\\)?-mode\\'"
592 (symbol-name mode))
593 (setq pretty-minor-mode
594 (capitalize
595 (substring (symbol-name mode)
596 0 (match-beginning 0)))))
597 (setq indicator (cadr (assq mode minor-mode-alist)))
598 (while (and indicator (symbolp indicator)
599 (boundp indicator)
600 (not (eq indicator (symbol-value indicator))))
601 (setq indicator (symbol-value indicator)))
602 (push (list pretty-minor-mode mode indicator)
603 minor-modes))))
604 (if auto-fill-function
605 (push '("Auto Fill" auto-fill-mode " Fill")
606 minor-modes))
607 (setq minor-modes
608 (sort minor-modes
609 (lambda (a b) (string-lessp (car a) (car b)))))
610 (when minor-modes
611 (princ "Summary of minor modes:\n")
612 (dolist (mode minor-modes)
613 (let ((pretty-minor-mode (nth 0 mode))
614 (indicator (nth 2 mode)))
615 (princ (format " %s minor mode (%s):\n"
616 pretty-minor-mode
617 (if indicator
618 (format "indicator%s" indicator)
619 "no indicator")))))
620 (princ "\n(Full information about these minor modes
621 follows the description of the major mode.)\n\n"))
622 ;; Document the major mode.
623 (princ mode-name)
624 (princ " mode:\n")
625 (princ (documentation major-mode))
626 ;; Document the minor modes fully.
627 (dolist (mode minor-modes)
628 (let ((pretty-minor-mode (nth 0 mode))
629 (mode-function (nth 1 mode))
630 (indicator (nth 2 mode)))
631 (princ "\n\f\n")
632 (princ (format "%s minor mode (%s):\n"
633 pretty-minor-mode
634 (if indicator
635 (format "indicator%s" indicator)
636 "no indicator")))
637 (princ (documentation mode-function)))))
638 (print-help-return-message))))
639
640
641 (defun describe-minor-mode (minor-mode)
642 "Display documentation of a minor mode given as MINOR-MODE.
643 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
644 appeared on the mode-line."
645 (interactive (list (completing-read
646 "Minor mode: "
647 (nconc
648 (describe-minor-mode-completion-table-for-symbol)
649 (describe-minor-mode-completion-table-for-indicator)
650 ))))
651 (if (symbolp minor-mode)
652 (setq minor-mode (symbol-name minor-mode)))
653 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
654 (indicators (describe-minor-mode-completion-table-for-indicator)))
655 (cond
656 ((member minor-mode symbols)
657 (describe-minor-mode-from-symbol (intern minor-mode)))
658 ((member minor-mode indicators)
659 (describe-minor-mode-from-indicator minor-mode))
660 (t
661 (error "No such minor mode: %s" minor-mode)))))
662
663 ;; symbol
664 (defun describe-minor-mode-completion-table-for-symbol ()
665 ;; In order to list up all minor modes, minor-mode-list
666 ;; is used here instead of minor-mode-alist.
667 (delq nil (mapcar 'symbol-name minor-mode-list)))
668 (defun describe-minor-mode-from-symbol (symbol)
669 "Display documentation of a minor mode given as a symbol, SYMBOL"
670 (interactive (list (intern (completing-read
671 "Minor mode symbol: "
672 (describe-minor-mode-completion-table-for-symbol)))))
673 (if (fboundp symbol)
674 (describe-function symbol)
675 (describe-variable symbol)))
676
677 ;; indicator
678 (defun describe-minor-mode-completion-table-for-indicator ()
679 (delq nil
680 (mapcar (lambda (x)
681 (let ((i (format-mode-line x)))
682 ;; remove first space if existed
683 (cond
684 ((= 0 (length i))
685 nil)
686 ((eq (aref i 0) ?\ )
687 (substring i 1))
688 (t
689 i))))
690 minor-mode-alist)))
691 (defun describe-minor-mode-from-indicator (indicator)
692 "Display documentation of a minor mode specified by INDICATOR.
693 If you call this function interactively, you can give indicator which
694 is currently activated with completion."
695 (interactive (list
696 (completing-read
697 "Minor mode indicator: "
698 (describe-minor-mode-completion-table-for-indicator))))
699 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
700 (if minor-mode
701 (describe-minor-mode-from-symbol minor-mode)
702 (error "Cannot find minor mode for `%s'" indicator))))
703
704 (defun lookup-minor-mode-from-indicator (indicator)
705 "Return a minor mode symbol from its indicator on the modeline."
706 ;; remove first space if existed
707 (if (and (< 0 (length indicator))
708 (eq (aref indicator 0) ?\ ))
709 (setq indicator (substring indicator 1)))
710 (let ((minor-modes minor-mode-alist)
711 result)
712 (while minor-modes
713 (let* ((minor-mode (car (car minor-modes)))
714 (anindicator (format-mode-line
715 (car (cdr (car minor-modes))))))
716 ;; remove first space if existed
717 (if (and (stringp anindicator)
718 (> (length anindicator) 0)
719 (eq (aref anindicator 0) ?\ ))
720 (setq anindicator (substring anindicator 1)))
721 (if (equal indicator anindicator)
722 (setq result minor-mode
723 minor-modes nil)
724 (setq minor-modes (cdr minor-modes)))))
725 result))
726
727 \f
728 ;;; Automatic resizing of temporary buffers.
729
730 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
731 "*Maximum height of a window displaying a temporary buffer.
732 This is the maximum height (in text lines) which `resize-temp-buffer-window'
733 will give to a window displaying a temporary buffer.
734 It can also be a function which will be called with the object corresponding
735 to the buffer to be displayed as argument and should return an integer
736 positive number."
737 :type '(choice integer function)
738 :group 'help
739 :version "20.4")
740
741 (define-minor-mode temp-buffer-resize-mode
742 "Toggle the mode which makes windows smaller for temporary buffers.
743 With prefix argument ARG, turn the resizing of windows displaying temporary
744 buffers on if ARG is positive or off otherwise.
745 This makes the window the right height for its contents, but never
746 more than `temp-buffer-max-height' nor less than `window-min-height'.
747 This applies to `help', `apropos' and `completion' buffers, and some others."
748 :global t :group 'help
749 (if temp-buffer-resize-mode
750 ;; `help-make-xrefs' may add a `back' button and thus increase the
751 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
752 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
753 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
754
755 (defun resize-temp-buffer-window ()
756 "Resize the current window to fit its contents.
757 Will not make it higher than `temp-buffer-max-height' nor smaller than
758 `window-min-height'. Do nothing if it is the only window on its frame, if it
759 is not as wide as the frame or if some of the window's contents are scrolled
760 out of view."
761 (unless (or (one-window-p 'nomini)
762 (not (pos-visible-in-window-p (point-min)))
763 (/= (frame-width) (window-width)))
764 (fit-window-to-buffer
765 (selected-window)
766 (if (functionp temp-buffer-max-height)
767 (funcall temp-buffer-max-height (current-buffer))
768 temp-buffer-max-height))))
769
770 ;; Provide this for the sake of define-minor-mode which generates
771 ;; defcustoms which require 'help'.
772 (provide 'help)
773
774 ;;; arch-tag: cf427352-27e9-49b7-9a6f-741ebab02423
775 ;;; help.el ends here