* reftex.texi (AUCTeX): Update links, section name.
[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, 2004
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 argument, display info only for the selected version."
317 (interactive "P")
318 (if (not arg)
319 (view-file (expand-file-name "NEWS" data-directory))
320 (let* ((map (sort
321 (delete-dups
322 (apply
323 'nconc
324 (mapcar
325 (lambda (file)
326 (with-temp-buffer
327 (insert-file-contents
328 (expand-file-name file data-directory))
329 (let (res)
330 (while (re-search-forward
331 (if (string-match "^ONEWS\\.[0-9]+$" file)
332 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
333 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
334 (setq res (cons (list (match-string-no-properties 1)
335 file) res)))
336 res)))
337 (append '("NEWS" "ONEWS")
338 (directory-files data-directory nil
339 "^ONEWS\\.[0-9]+$" nil)))))
340 (lambda (a b)
341 (string< (car b) (car a)))))
342 (current (caar map))
343 (version (completing-read
344 (format "Read NEWS for the version (default %s): " current)
345 (mapcar 'car map) nil nil nil nil current))
346 (file (cadr (assoc version map)))
347 res)
348 (if (not file)
349 (error "No news is good news")
350 (view-file (expand-file-name file data-directory))
351 (widen)
352 (goto-char (point-min))
353 (when (re-search-forward
354 (concat (if (string-match "^ONEWS\\.[0-9]+$" file)
355 "Changes in \\(?:Emacs\\|version\\)?[ \t]*"
356 "^\* [^0-9\n]*") version)
357 nil t)
358 (beginning-of-line)
359 (narrow-to-region
360 (point)
361 (save-excursion
362 (while (and (setq res
363 (re-search-forward
364 (if (string-match "^ONEWS\\.[0-9]+$" file)
365 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
366 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
367 (equal (match-string-no-properties 1) version)))
368 (or res (goto-char (point-max)))
369 (beginning-of-line)
370 (point))))))))
371
372 (defun view-todo (&optional arg)
373 "Display the Emacs TODO list."
374 (interactive "P")
375 (view-file (expand-file-name "TODO" data-directory)))
376
377 (defun view-echo-area-messages ()
378 "View the log of recent echo-area messages: the `*Messages*' buffer.
379 The number of messages retained in that buffer
380 is specified by the variable `message-log-max'."
381 (interactive)
382 (switch-to-buffer (get-buffer-create "*Messages*")))
383
384 (defun view-order-manuals ()
385 "Display the Emacs ORDERS file."
386 (interactive)
387 (view-file (expand-file-name "ORDERS" data-directory))
388 (goto-address))
389
390 (defun view-emacs-FAQ ()
391 "Display the Emacs Frequently Asked Questions (FAQ) file."
392 (interactive)
393 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
394 (info "(efaq)"))
395
396 (defun view-emacs-problems ()
397 "Display info on known problems with Emacs and possible workarounds."
398 (interactive)
399 (view-file (expand-file-name "PROBLEMS" data-directory)))
400
401 (defun view-lossage ()
402 "Display last 100 input keystrokes.
403
404 To record all your input on a file, use `open-dribble-file'."
405 (interactive)
406 (help-setup-xref (list #'view-lossage) (interactive-p))
407 (with-output-to-temp-buffer (help-buffer)
408 (princ (mapconcat (lambda (key)
409 (if (or (integerp key) (symbolp key) (listp key))
410 (single-key-description key)
411 (prin1-to-string key nil)))
412 (recent-keys)
413 " "))
414 (with-current-buffer standard-output
415 (goto-char (point-min))
416 (while (progn (move-to-column 50) (not (eobp)))
417 (search-forward " " nil t)
418 (insert "\n")))
419 (print-help-return-message)))
420
421 \f
422 ;; Key bindings
423
424 (defun describe-bindings (&optional prefix buffer)
425 "Show a list of all defined keys, and their definitions.
426 We put that list in a buffer, and display the buffer.
427
428 The optional argument PREFIX, if non-nil, should be a key sequence;
429 then we display only bindings that start with that prefix.
430 The optional argument BUFFER specifies which buffer's bindings
431 to display (default, the current buffer). BUFFER can be a buffer
432 or a buffer name."
433 (interactive)
434 (or buffer (setq buffer (current-buffer)))
435 (help-setup-xref (list #'describe-bindings prefix buffer) (interactive-p))
436 (with-current-buffer buffer
437 (describe-bindings-internal nil prefix)))
438
439 ;; This function used to be in keymap.c.
440 (defun describe-bindings-internal (&optional menus prefix)
441 "Show a list of all defined keys, and their definitions.
442 We put that list in a buffer, and display the buffer.
443
444 The optional argument MENUS, if non-nil, says to mention menu bindings.
445 \(Ordinarily these are omitted from the output.)
446 The optional argument PREFIX, if non-nil, should be a key sequence;
447 then we display only bindings that start with that prefix."
448 (interactive)
449 (let ((buf (current-buffer)))
450 (with-output-to-temp-buffer "*Help*"
451 (with-current-buffer standard-output
452 (describe-buffer-bindings buf prefix menus)))))
453
454 (defun where-is (definition &optional insert)
455 "Print message listing key sequences that invoke the command DEFINITION.
456 Argument is a command definition, usually a symbol with a function definition.
457 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
458 (interactive
459 (let ((fn (function-called-at-point))
460 (enable-recursive-minibuffers t)
461 val)
462 (setq val (completing-read
463 (if fn
464 (format "Where is command (default %s): " fn)
465 "Where is command: ")
466 obarray 'commandp t))
467 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
468 (let ((func (indirect-function definition))
469 (defs nil)
470 (standard-output (if insert (current-buffer) t)))
471 (mapatoms (lambda (symbol)
472 (and (fboundp symbol)
473 (not (eq symbol definition))
474 (eq func (condition-case ()
475 (indirect-function symbol)
476 (error symbol)))
477 (push symbol defs))))
478 (princ (mapconcat
479 #'(lambda (symbol)
480 (let* ((remapped (command-remapping symbol))
481 (keys (where-is-internal
482 symbol overriding-local-map nil nil remapped))
483 (keys (mapconcat 'key-description keys ", ")))
484 (if insert
485 (if (> (length keys) 0)
486 (if remapped
487 (format "%s (%s) (remapped from %s)"
488 keys remapped symbol)
489 (format "%s (%s)" keys symbol))
490 (format "M-x %s RET" symbol))
491 (if (> (length keys) 0)
492 (if remapped
493 (format "%s is remapped to %s which is on %s"
494 definition symbol keys)
495 (format "%s is on %s" symbol keys))
496 (format "%s is not on any key" symbol)))))
497 (cons definition defs)
498 ";\nand ")))
499 nil)
500
501 (defun string-key-binding (key)
502 "Value is the binding of KEY in a string.
503 If KEY is an event on a string, and that string has a `local-map'
504 or `keymap' property, return the binding of KEY in the string's keymap."
505 (let* ((defn nil)
506 (start (when (vectorp key)
507 (if (memq (aref key 0)
508 '(mode-line header-line left-margin right-margin))
509 (event-start (aref key 1))
510 (and (consp (aref key 0))
511 (event-start (aref key 0))))))
512 (string-info (and (consp start) (nth 4 start))))
513 (when string-info
514 (let* ((string (car string-info))
515 (pos (cdr string-info))
516 (local-map (and (>= pos 0)
517 (< pos (length string))
518 (or (get-text-property pos 'local-map string)
519 (get-text-property pos 'keymap string)))))
520 (setq defn (and local-map (lookup-key local-map key)))))
521 defn))
522
523 (defun help-key-description (key untranslated)
524 (let ((string (key-description key)))
525 (if (or (not untranslated)
526 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
527 string
528 (let ((otherstring (key-description untranslated)))
529 (if (equal string otherstring)
530 string
531 (format "%s (translated from %s)" string otherstring))))))
532
533 (defun describe-key-briefly (key &optional insert untranslated)
534 "Print the name of the function KEY invokes. KEY is a string.
535 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
536 If non-nil UNTRANSLATED is a vector of the untranslated events.
537 It can also be a number in which case the untranslated events from
538 the last key hit are used."
539 (interactive "kDescribe key briefly: \nP\np")
540 (if (numberp untranslated)
541 (setq untranslated (this-single-command-raw-keys)))
542 (save-excursion
543 (let ((modifiers (event-modifiers (aref key 0)))
544 (standard-output (if insert (current-buffer) t))
545 window position)
546 ;; For a mouse button event, go to the button it applies to
547 ;; to get the right key bindings. And go to the right place
548 ;; in case the keymap depends on where you clicked.
549 (if (or (memq 'click modifiers) (memq 'down modifiers)
550 (memq 'drag modifiers))
551 (setq window (posn-window (event-start (aref key 0)))
552 position (posn-point (event-start (aref key 0)))))
553 (if (windowp window)
554 (progn
555 (set-buffer (window-buffer window))
556 (goto-char position)))
557 ;; Ok, now look up the key and name the command.
558 (let ((defn (or (string-key-binding key)
559 (key-binding key)))
560 (key-desc (help-key-description key untranslated)))
561 (if (or (null defn) (integerp defn) (equal defn 'undefined))
562 (princ (format "%s is undefined" key-desc))
563 (princ (format (if (windowp window)
564 "%s at that spot runs the command %s"
565 "%s runs the command %s")
566 key-desc
567 (if (symbolp defn) defn (prin1-to-string defn)))))))))
568
569
570 (defun describe-key (key &optional untranslated)
571 "Display documentation of the function invoked by KEY.
572 KEY should be a key sequence--when calling from a program,
573 pass a string or a vector.
574 If non-nil UNTRANSLATED is a vector of the untranslated events.
575 It can also be a number in which case the untranslated events from
576 the last key hit are used."
577 (interactive "kDescribe key: \np")
578 (if (numberp untranslated)
579 (setq untranslated (this-single-command-raw-keys)))
580 (save-excursion
581 (let ((modifiers (event-modifiers (aref key 0)))
582 window position)
583 ;; For a mouse button event, go to the button it applies to
584 ;; to get the right key bindings. And go to the right place
585 ;; in case the keymap depends on where you clicked.
586 (if (or (memq 'click modifiers) (memq 'down modifiers)
587 (memq 'drag modifiers))
588 (setq window (posn-window (event-start (aref key 0)))
589 position (posn-point (event-start (aref key 0)))))
590 (when (windowp window)
591 (set-buffer (window-buffer window))
592 (goto-char position))
593 (let ((defn (or (string-key-binding key) (key-binding key))))
594 (if (or (null defn) (integerp defn) (equal defn 'undefined))
595 (message "%s is undefined" (help-key-description key untranslated))
596 (help-setup-xref (list #'describe-function defn) (interactive-p))
597 (with-output-to-temp-buffer (help-buffer)
598 (princ (help-key-description key untranslated))
599 (if (windowp window)
600 (princ " at that spot"))
601 (princ " runs the command ")
602 (prin1 defn)
603 (princ "\n which is ")
604 (describe-function-1 defn)
605 (print-help-return-message)))))))
606
607 \f
608 (defun describe-mode (&optional buffer)
609 "Display documentation of current major mode and minor modes.
610 A brief summary of the minor modes comes first, followed by the
611 major mode description. This is followed by detailed
612 descriptions of the minor modes, each on a separate page.
613
614 For this to work correctly for a minor mode, the mode's indicator
615 variable \(listed in `minor-mode-alist') must also be a function
616 whose documentation describes the minor mode."
617 (interactive)
618 (help-setup-xref (list #'describe-mode (or buffer (current-buffer)))
619 (interactive-p))
620 ;; For the sake of help-do-xref and help-xref-go-back,
621 ;; don't switch buffers before calling `help-buffer'.
622 (with-output-to-temp-buffer (help-buffer)
623 (save-excursion
624 (when buffer (set-buffer buffer))
625 (let (minor-modes)
626 ;; Find enabled minor mode we will want to mention.
627 (dolist (mode minor-mode-list)
628 ;; Document a minor mode if it is listed in minor-mode-alist,
629 ;; non-nil, and has a function definition.
630 (and (boundp mode) (symbol-value mode)
631 (fboundp mode)
632 (let ((pretty-minor-mode mode)
633 indicator)
634 (if (string-match "\\(-minor\\)?-mode\\'"
635 (symbol-name mode))
636 (setq pretty-minor-mode
637 (capitalize
638 (substring (symbol-name mode)
639 0 (match-beginning 0)))))
640 (setq indicator (cadr (assq mode minor-mode-alist)))
641 (while (and indicator (symbolp indicator)
642 (boundp indicator)
643 (not (eq indicator (symbol-value indicator))))
644 (setq indicator (symbol-value indicator)))
645 (push (list pretty-minor-mode mode indicator)
646 minor-modes))))
647 (if auto-fill-function
648 (push '("Auto Fill" auto-fill-mode " Fill")
649 minor-modes))
650 (setq minor-modes
651 (sort minor-modes
652 (lambda (a b) (string-lessp (car a) (car b)))))
653 (when minor-modes
654 (princ "Summary of minor modes:\n")
655 (dolist (mode minor-modes)
656 (let ((pretty-minor-mode (nth 0 mode))
657 (indicator (nth 2 mode)))
658 (princ (format " %s minor mode (%s):\n"
659 pretty-minor-mode
660 (if indicator
661 (format "indicator%s" indicator)
662 "no indicator")))))
663 (princ "\n(Full information about these minor modes
664 follows the description of the major mode.)\n\n"))
665 ;; Document the major mode.
666 (princ mode-name)
667 (princ " mode:\n")
668 (princ (documentation major-mode))
669 ;; Document the minor modes fully.
670 (dolist (mode minor-modes)
671 (let ((pretty-minor-mode (nth 0 mode))
672 (mode-function (nth 1 mode))
673 (indicator (nth 2 mode)))
674 (princ "\n\f\n")
675 (princ (format "%s minor mode (%s):\n"
676 pretty-minor-mode
677 (if indicator
678 (format "indicator%s" indicator)
679 "no indicator")))
680 (princ (documentation mode-function)))))
681 (print-help-return-message))))
682
683
684 (defun describe-minor-mode (minor-mode)
685 "Display documentation of a minor mode given as MINOR-MODE.
686 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
687 appeared on the mode-line."
688 (interactive (list (completing-read
689 "Minor mode: "
690 (nconc
691 (describe-minor-mode-completion-table-for-symbol)
692 (describe-minor-mode-completion-table-for-indicator)
693 ))))
694 (if (symbolp minor-mode)
695 (setq minor-mode (symbol-name minor-mode)))
696 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
697 (indicators (describe-minor-mode-completion-table-for-indicator)))
698 (cond
699 ((member minor-mode symbols)
700 (describe-minor-mode-from-symbol (intern minor-mode)))
701 ((member minor-mode indicators)
702 (describe-minor-mode-from-indicator minor-mode))
703 (t
704 (error "No such minor mode: %s" minor-mode)))))
705
706 ;; symbol
707 (defun describe-minor-mode-completion-table-for-symbol ()
708 ;; In order to list up all minor modes, minor-mode-list
709 ;; is used here instead of minor-mode-alist.
710 (delq nil (mapcar 'symbol-name minor-mode-list)))
711 (defun describe-minor-mode-from-symbol (symbol)
712 "Display documentation of a minor mode given as a symbol, SYMBOL"
713 (interactive (list (intern (completing-read
714 "Minor mode symbol: "
715 (describe-minor-mode-completion-table-for-symbol)))))
716 (if (fboundp symbol)
717 (describe-function symbol)
718 (describe-variable symbol)))
719
720 ;; indicator
721 (defun describe-minor-mode-completion-table-for-indicator ()
722 (delq nil
723 (mapcar (lambda (x)
724 (let ((i (format-mode-line x)))
725 ;; remove first space if existed
726 (cond
727 ((= 0 (length i))
728 nil)
729 ((eq (aref i 0) ?\ )
730 (substring i 1))
731 (t
732 i))))
733 minor-mode-alist)))
734 (defun describe-minor-mode-from-indicator (indicator)
735 "Display documentation of a minor mode specified by INDICATOR.
736 If you call this function interactively, you can give indicator which
737 is currently activated with completion."
738 (interactive (list
739 (completing-read
740 "Minor mode indicator: "
741 (describe-minor-mode-completion-table-for-indicator))))
742 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
743 (if minor-mode
744 (describe-minor-mode-from-symbol minor-mode)
745 (error "Cannot find minor mode for `%s'" indicator))))
746
747 (defun lookup-minor-mode-from-indicator (indicator)
748 "Return a minor mode symbol from its indicator on the modeline."
749 ;; remove first space if existed
750 (if (and (< 0 (length indicator))
751 (eq (aref indicator 0) ?\ ))
752 (setq indicator (substring indicator 1)))
753 (let ((minor-modes minor-mode-alist)
754 result)
755 (while minor-modes
756 (let* ((minor-mode (car (car minor-modes)))
757 (anindicator (format-mode-line
758 (car (cdr (car minor-modes))))))
759 ;; remove first space if existed
760 (if (and (stringp anindicator)
761 (> (length anindicator) 0)
762 (eq (aref anindicator 0) ?\ ))
763 (setq anindicator (substring anindicator 1)))
764 (if (equal indicator anindicator)
765 (setq result minor-mode
766 minor-modes nil)
767 (setq minor-modes (cdr minor-modes)))))
768 result))
769
770 \f
771 ;;; Automatic resizing of temporary buffers.
772
773 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
774 "*Maximum height of a window displaying a temporary buffer.
775 This is the maximum height (in text lines) which `resize-temp-buffer-window'
776 will give to a window displaying a temporary buffer.
777 It can also be a function which will be called with the object corresponding
778 to the buffer to be displayed as argument and should return an integer
779 positive number."
780 :type '(choice integer function)
781 :group 'help
782 :version "20.4")
783
784 (define-minor-mode temp-buffer-resize-mode
785 "Toggle the mode which makes windows smaller for temporary buffers.
786 With prefix argument ARG, turn the resizing of windows displaying temporary
787 buffers on if ARG is positive or off otherwise.
788 This makes the window the right height for its contents, but never
789 more than `temp-buffer-max-height' nor less than `window-min-height'.
790 This applies to `help', `apropos' and `completion' buffers, and some others."
791 :global t :group 'help
792 (if temp-buffer-resize-mode
793 ;; `help-make-xrefs' may add a `back' button and thus increase the
794 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
795 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
796 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
797
798 (defun resize-temp-buffer-window ()
799 "Resize the current window to fit its contents.
800 Will not make it higher than `temp-buffer-max-height' nor smaller than
801 `window-min-height'. Do nothing if it is the only window on its frame, if it
802 is not as wide as the frame or if some of the window's contents are scrolled
803 out of view."
804 (unless (or (one-window-p 'nomini)
805 (not (pos-visible-in-window-p (point-min)))
806 (/= (frame-width) (window-width)))
807 (fit-window-to-buffer
808 (selected-window)
809 (if (functionp temp-buffer-max-height)
810 (funcall temp-buffer-max-height (current-buffer))
811 temp-buffer-max-height))))
812
813 ;; Provide this for the sake of define-minor-mode which generates
814 ;; defcustoms which require 'help'.
815 (provide 'help)
816
817 ;;; arch-tag: cf427352-27e9-49b7-9a6f-741ebab02423
818 ;;; help.el ends here