(diff-default-read-only): Change default.
[emacs.git] / lisp / help.el
blob82a43c6868b7cb669d88fd1d03d1b05f2ae988cc
1 ;;; help.el --- help commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002
4 ;; Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: help, internal
9 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;;; Commentary:
28 ;; This code implements GNU Emacs' on-line help system, the one invoked by
29 ;; `M-x help-for-help'.
31 ;;; Code:
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))
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)
41 (defvar help-map (make-sparse-keymap)
42 "Keymap for characters following the Help key.")
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)
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)
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)
65 ;; This does not fit the pattern, but it is natural given the C-\ command.
66 (define-key help-map "\C-\\" 'describe-input-method)
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)
75 (define-key help-map "a" 'apropos-command)
77 (define-key help-map "b" 'describe-bindings)
79 (define-key help-map "c" 'describe-key-briefly)
81 (define-key help-map "e" 'view-echo-area-messages)
83 (define-key help-map "f" 'describe-function)
85 (define-key help-map "h" 'view-hello-file)
87 (define-key help-map "i" 'info)
88 (define-key help-map "4i" 'info-other-window)
90 (define-key help-map "k" 'describe-key)
92 (define-key help-map "l" 'view-lossage)
94 (define-key help-map "m" 'describe-mode)
96 (define-key help-map "n" 'view-emacs-news)
98 (define-key help-map "p" 'finder-by-keyword)
99 (autoload 'finder-by-keyword "finder"
100 "Find packages matching a given keyword." t)
102 (define-key help-map "r" 'info-emacs-manual)
104 (define-key help-map "s" 'describe-syntax)
106 (define-key help-map "t" 'help-with-tutorial)
108 (define-key help-map "w" 'where-is)
110 (define-key help-map "v" 'describe-variable)
112 (define-key help-map "q" 'help-quit)
115 (defun help-quit ()
116 "Just exit from the Help command's command loop."
117 (interactive)
118 nil)
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.")
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.")
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))
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."))))))))
176 ;; So keyboard macro definitions are documented correctly
177 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
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.)
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.
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)
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)))))
268 ;;; `User' help functions
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)))
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)))
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)))
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)))
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)
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")
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"))))
330 (defun view-todo (&optional arg)
331 "Display the Emacs TODO list."
332 (interactive "P")
333 (view-file (expand-file-name "TODO" data-directory)))
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*")))
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))
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)"))
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)))
359 (defun view-lossage ()
360 "Display last 100 input keystrokes.
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)))
380 ;; Key bindings
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.
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)))
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.
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)))))
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)
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))
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))))))
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)))))))))
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)))))))
565 (defun describe-mode (&optional buffer)
566 "Display documentation of current major mode and minor modes.
567 The major mode description comes first, followed by the minor modes,
568 each on a separate page.
569 For this to work correctly for a minor mode, the mode's indicator variable
570 \(listed in `minor-mode-alist') must also be a function whose documentation
571 describes the minor mode."
572 (interactive)
573 (help-setup-xref (list #'describe-mode (or buffer (current-buffer)))
574 (interactive-p))
575 ;; For the sake of help-do-xref and help-xref-go-back,
576 ;; don't switch buffers before calling `help-buffer'.
577 (with-output-to-temp-buffer (help-buffer)
578 (save-excursion
579 (when buffer (set-buffer buffer))
580 (let (minor-modes)
581 ;; Find enabled minor mode we will want to mention.
582 (dolist (mode minor-mode-list)
583 ;; Document a minor mode if it is listed in minor-mode-alist,
584 ;; non-nil, and has a function definition.
585 (and (boundp mode) (symbol-value mode)
586 (fboundp mode)
587 (let ((pretty-minor-mode mode)
588 indicator)
589 (if (string-match "\\(-minor\\)?-mode\\'"
590 (symbol-name mode))
591 (setq pretty-minor-mode
592 (capitalize
593 (substring (symbol-name mode)
594 0 (match-beginning 0)))))
595 (setq indicator (cadr (assq mode minor-mode-alist)))
596 (while (and indicator (symbolp indicator)
597 (boundp indicator)
598 (not (eq indicator (symbol-value indicator))))
599 (setq indicator (symbol-value indicator)))
600 (push (list pretty-minor-mode mode indicator)
601 minor-modes))))
602 (if auto-fill-function
603 (push '("Auto Fill" auto-fill-mode " Fill")
604 minor-modes))
605 (setq minor-modes
606 (sort minor-modes
607 (lambda (a b) (string-lessp (car a) (car b)))))
608 (when minor-modes
609 (princ "Summary of minor modes:\n")
610 (dolist (mode minor-modes)
611 (let ((pretty-minor-mode (nth 0 mode))
612 (indicator (nth 2 mode)))
613 (princ (format " %s minor mode (%s):\n"
614 pretty-minor-mode
615 (if indicator
616 (format "indicator%s" indicator)
617 "no indicator")))))
618 (princ "\n(Full information about these minor modes
619 follows the description of the major mode.)\n\n"))
620 ;; Document the major mode.
621 (princ mode-name)
622 (princ " mode:\n")
623 (princ (documentation major-mode))
624 ;; Document the minor modes fully.
625 (dolist (mode minor-modes)
626 (let ((pretty-minor-mode (nth 0 mode))
627 (mode-function (nth 1 mode))
628 (indicator (nth 2 mode)))
629 (princ "\n\f\n")
630 (princ (format "%s minor mode (%s):\n"
631 pretty-minor-mode
632 (if indicator
633 (format "indicator%s" indicator)
634 "no indicator")))
635 (princ (documentation mode-function)))))
636 (print-help-return-message))))
639 (defun describe-minor-mode (minor-mode)
640 "Display documentation of a minor mode given as MINOR-MODE.
641 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
642 appeared on the mode-line."
643 (interactive (list (completing-read
644 "Minor mode: "
645 (nconc
646 (describe-minor-mode-completion-table-for-symbol)
647 (describe-minor-mode-completion-table-for-indicator)
648 ))))
649 (if (symbolp minor-mode)
650 (setq minor-mode (symbol-name minor-mode)))
651 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
652 (indicators (describe-minor-mode-completion-table-for-indicator)))
653 (cond
654 ((member minor-mode symbols)
655 (describe-minor-mode-from-symbol (intern minor-mode)))
656 ((member minor-mode indicators)
657 (describe-minor-mode-from-indicator minor-mode))
659 (error "No such minor mode: %s" minor-mode)))))
661 ;; symbol
662 (defun describe-minor-mode-completion-table-for-symbol ()
663 ;; In order to list up all minor modes, minor-mode-list
664 ;; is used here instead of minor-mode-alist.
665 (delq nil (mapcar 'symbol-name minor-mode-list)))
666 (defun describe-minor-mode-from-symbol (symbol)
667 "Display documentation of a minor mode given as a symbol, SYMBOL"
668 (interactive (list (intern (completing-read
669 "Minor mode symbol: "
670 (describe-minor-mode-completion-table-for-symbol)))))
671 (if (fboundp symbol)
672 (describe-function symbol)
673 (describe-variable symbol)))
675 ;; indicator
676 (defun describe-minor-mode-completion-table-for-indicator ()
677 (delq nil
678 (mapcar (lambda (x)
679 (let ((i (format-mode-line x)))
680 ;; remove first space if existed
681 (cond
682 ((= 0 (length i))
683 nil)
684 ((eq (aref i 0) ?\ )
685 (substring i 1))
687 i))))
688 minor-mode-alist)))
689 (defun describe-minor-mode-from-indicator (indicator)
690 "Display documentation of a minor mode specified by INDICATOR.
691 If you call this function interactively, you can give indicator which
692 is currently activated with completion."
693 (interactive (list
694 (completing-read
695 "Minor mode indicator: "
696 (describe-minor-mode-completion-table-for-indicator))))
697 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
698 (if minor-mode
699 (describe-minor-mode-from-symbol minor-mode)
700 (error "Cannot find minor mode for `%s'" indicator))))
702 (defun lookup-minor-mode-from-indicator (indicator)
703 "Return a minor mode symbol from its indicator on the modeline."
704 ;; remove first space if existed
705 (if (and (< 0 (length indicator))
706 (eq (aref indicator 0) ?\ ))
707 (setq indicator (substring indicator 1)))
708 (let ((minor-modes minor-mode-alist)
709 result)
710 (while minor-modes
711 (let* ((minor-mode (car (car minor-modes)))
712 (anindicator (format-mode-line
713 (car (cdr (car minor-modes))))))
714 ;; remove first space if existed
715 (if (and (stringp anindicator)
716 (> (length anindicator) 0)
717 (eq (aref anindicator 0) ?\ ))
718 (setq anindicator (substring anindicator 1)))
719 (if (equal indicator anindicator)
720 (setq result minor-mode
721 minor-modes nil)
722 (setq minor-modes (cdr minor-modes)))))
723 result))
726 ;;; Automatic resizing of temporary buffers.
728 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
729 "*Maximum height of a window displaying a temporary buffer.
730 This is the maximum height (in text lines) which `resize-temp-buffer-window'
731 will give to a window displaying a temporary buffer.
732 It can also be a function which will be called with the object corresponding
733 to the buffer to be displayed as argument and should return an integer
734 positive number."
735 :type '(choice integer function)
736 :group 'help
737 :version "20.4")
739 (define-minor-mode temp-buffer-resize-mode
740 "Toggle the mode which makes windows smaller for temporary buffers.
741 With prefix argument ARG, turn the resizing of windows displaying temporary
742 buffers on if ARG is positive or off otherwise.
743 This makes the window the right height for its contents, but never
744 more than `temp-buffer-max-height' nor less than `window-min-height'.
745 This applies to `help', `apropos' and `completion' buffers, and some others."
746 :global t :group 'help
747 (if temp-buffer-resize-mode
748 ;; `help-make-xrefs' may add a `back' button and thus increase the
749 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
750 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
751 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
753 (defun resize-temp-buffer-window ()
754 "Resize the current window to fit its contents.
755 Will not make it higher than `temp-buffer-max-height' nor smaller than
756 `window-min-height'. Do nothing if it is the only window on its frame, if it
757 is not as wide as the frame or if some of the window's contents are scrolled
758 out of view."
759 (unless (or (one-window-p 'nomini)
760 (not (pos-visible-in-window-p (point-min)))
761 (/= (frame-width) (window-width)))
762 (fit-window-to-buffer
763 (selected-window)
764 (if (functionp temp-buffer-max-height)
765 (funcall temp-buffer-max-height (current-buffer))
766 temp-buffer-max-height))))
768 ;; Provide this for the sake of define-minor-mode which generates
769 ;; defcustoms which require 'help'.
770 (provide 'help)
772 ;;; arch-tag: cf427352-27e9-49b7-9a6f-741ebab02423
773 ;;; help.el ends here