(set-visited-file-name, file-expand-wildcards): Fix docstring.
[emacs.git] / lisp / help.el
blobfc43d8db03d2227a74cc705385eda44b5aed047f
1 ;;; help.el --- help commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002, 2004
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 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))))))))
372 (defun view-todo (&optional arg)
373 "Display the Emacs TODO list."
374 (interactive "P")
375 (view-file (expand-file-name "TODO" data-directory)))
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*")))
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))
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)"))
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)))
401 (defun view-lossage ()
402 "Display last 100 input keystrokes.
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)))
422 ;; Key bindings
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.
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)."
432 (interactive)
433 (or buffer (setq buffer (current-buffer)))
434 (help-setup-xref (list #'describe-bindings prefix buffer) (interactive-p))
435 (with-current-buffer buffer
436 (describe-bindings-internal nil prefix)))
438 ;; This function used to be in keymap.c.
439 (defun describe-bindings-internal (&optional menus prefix)
440 "Show a list of all defined keys, and their definitions.
441 We put that list in a buffer, and display the buffer.
443 The optional argument MENUS, if non-nil, says to mention menu bindings.
444 \(Ordinarily these are omitted from the output.)
445 The optional argument PREFIX, if non-nil, should be a key sequence;
446 then we display only bindings that start with that prefix."
447 (interactive)
448 (let ((buf (current-buffer)))
449 (with-output-to-temp-buffer "*Help*"
450 (with-current-buffer standard-output
451 (describe-buffer-bindings buf prefix menus)))))
453 (defun where-is (definition &optional insert)
454 "Print message listing key sequences that invoke the command DEFINITION.
455 Argument is a command definition, usually a symbol with a function definition.
456 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
457 (interactive
458 (let ((fn (function-called-at-point))
459 (enable-recursive-minibuffers t)
460 val)
461 (setq val (completing-read
462 (if fn
463 (format "Where is command (default %s): " fn)
464 "Where is command: ")
465 obarray 'commandp t))
466 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
467 (let ((func (indirect-function definition))
468 (defs nil)
469 (standard-output (if insert (current-buffer) t)))
470 (mapatoms (lambda (symbol)
471 (and (fboundp symbol)
472 (not (eq symbol definition))
473 (eq func (condition-case ()
474 (indirect-function symbol)
475 (error symbol)))
476 (push symbol defs))))
477 (princ (mapconcat
478 #'(lambda (symbol)
479 (let* ((remapped (command-remapping symbol))
480 (keys (where-is-internal
481 symbol overriding-local-map nil nil remapped))
482 (keys (mapconcat 'key-description keys ", ")))
483 (if insert
484 (if (> (length keys) 0)
485 (if remapped
486 (format "%s (%s) (remapped from %s)"
487 keys remapped symbol)
488 (format "%s (%s)" keys symbol))
489 (format "M-x %s RET" symbol))
490 (if (> (length keys) 0)
491 (if remapped
492 (format "%s is remapped to %s which is on %s"
493 definition symbol keys)
494 (format "%s is on %s" symbol keys))
495 (format "%s is not on any key" symbol)))))
496 (cons definition defs)
497 ";\nand ")))
498 nil)
500 (defun string-key-binding (key)
501 "Value is the binding of KEY in a string.
502 If KEY is an event on a string, and that string has a `local-map'
503 or `keymap' property, return the binding of KEY in the string's keymap."
504 (let* ((defn nil)
505 (start (when (vectorp key)
506 (if (memq (aref key 0)
507 '(mode-line header-line left-margin right-margin))
508 (event-start (aref key 1))
509 (and (consp (aref key 0))
510 (event-start (aref key 0))))))
511 (string-info (and (consp start) (nth 4 start))))
512 (when string-info
513 (let* ((string (car string-info))
514 (pos (cdr string-info))
515 (local-map (and (>= pos 0)
516 (< pos (length string))
517 (or (get-text-property pos 'local-map string)
518 (get-text-property pos 'keymap string)))))
519 (setq defn (and local-map (lookup-key local-map key)))))
520 defn))
522 (defun help-key-description (key untranslated)
523 (let ((string (key-description key)))
524 (if (or (not untranslated)
525 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
526 string
527 (let ((otherstring (key-description untranslated)))
528 (if (equal string otherstring)
529 string
530 (format "%s (translated from %s)" string otherstring))))))
532 (defun describe-key-briefly (key &optional insert untranslated)
533 "Print the name of the function KEY invokes. KEY is a string.
534 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
535 If non-nil UNTRANSLATED is a vector of the untranslated events.
536 It can also be a number in which case the untranslated events from
537 the last key hit are used."
538 (interactive "kDescribe key briefly: \nP\np")
539 (if (numberp untranslated)
540 (setq untranslated (this-single-command-raw-keys)))
541 (save-excursion
542 (let ((modifiers (event-modifiers (aref key 0)))
543 (standard-output (if insert (current-buffer) t))
544 window position)
545 ;; For a mouse button event, go to the button it applies to
546 ;; to get the right key bindings. And go to the right place
547 ;; in case the keymap depends on where you clicked.
548 (if (or (memq 'click modifiers) (memq 'down modifiers)
549 (memq 'drag modifiers))
550 (setq window (posn-window (event-start (aref key 0)))
551 position (posn-point (event-start (aref key 0)))))
552 (if (windowp window)
553 (progn
554 (set-buffer (window-buffer window))
555 (goto-char position)))
556 ;; Ok, now look up the key and name the command.
557 (let ((defn (or (string-key-binding key)
558 (key-binding key)))
559 (key-desc (help-key-description key untranslated)))
560 (if (or (null defn) (integerp defn) (equal defn 'undefined))
561 (princ (format "%s is undefined" key-desc))
562 (princ (format (if (windowp window)
563 "%s at that spot runs the command %s"
564 "%s runs the command %s")
565 key-desc
566 (if (symbolp defn) defn (prin1-to-string defn)))))))))
569 (defun describe-key (key &optional untranslated)
570 "Display documentation of the function invoked by KEY.
571 KEY should be a key sequence--when calling from a program,
572 pass a string or a vector.
573 If non-nil UNTRANSLATED is a vector of the untranslated events.
574 It can also be a number in which case the untranslated events from
575 the last key hit are used."
576 (interactive "kDescribe key: \np")
577 (if (numberp untranslated)
578 (setq untranslated (this-single-command-raw-keys)))
579 (save-excursion
580 (let ((modifiers (event-modifiers (aref key 0)))
581 window position)
582 ;; For a mouse button event, go to the button it applies to
583 ;; to get the right key bindings. And go to the right place
584 ;; in case the keymap depends on where you clicked.
585 (if (or (memq 'click modifiers) (memq 'down modifiers)
586 (memq 'drag modifiers))
587 (setq window (posn-window (event-start (aref key 0)))
588 position (posn-point (event-start (aref key 0)))))
589 (when (windowp window)
590 (set-buffer (window-buffer window))
591 (goto-char position))
592 (let ((defn (or (string-key-binding key) (key-binding key))))
593 (if (or (null defn) (integerp defn) (equal defn 'undefined))
594 (message "%s is undefined" (help-key-description key untranslated))
595 (help-setup-xref (list #'describe-function defn) (interactive-p))
596 (with-output-to-temp-buffer (help-buffer)
597 (princ (help-key-description key untranslated))
598 (if (windowp window)
599 (princ " at that spot"))
600 (princ " runs the command ")
601 (prin1 defn)
602 (princ "\n which is ")
603 (describe-function-1 defn)
604 (print-help-return-message)))))))
607 (defun describe-mode (&optional buffer)
608 "Display documentation of current major mode and minor modes.
609 A brief summary of the minor modes comes first, followed by the
610 major mode description. This is followed by detailed
611 descriptions of the minor modes, each on a separate page.
613 For this to work correctly for a minor mode, the mode's indicator
614 variable \(listed in `minor-mode-alist') must also be a function
615 whose documentation describes the minor mode."
616 (interactive)
617 (help-setup-xref (list #'describe-mode (or buffer (current-buffer)))
618 (interactive-p))
619 ;; For the sake of help-do-xref and help-xref-go-back,
620 ;; don't switch buffers before calling `help-buffer'.
621 (with-output-to-temp-buffer (help-buffer)
622 (save-excursion
623 (when buffer (set-buffer buffer))
624 (let (minor-modes)
625 ;; Find enabled minor mode we will want to mention.
626 (dolist (mode minor-mode-list)
627 ;; Document a minor mode if it is listed in minor-mode-alist,
628 ;; non-nil, and has a function definition.
629 (and (boundp mode) (symbol-value mode)
630 (fboundp mode)
631 (let ((pretty-minor-mode mode)
632 indicator)
633 (if (string-match "\\(-minor\\)?-mode\\'"
634 (symbol-name mode))
635 (setq pretty-minor-mode
636 (capitalize
637 (substring (symbol-name mode)
638 0 (match-beginning 0)))))
639 (setq indicator (cadr (assq mode minor-mode-alist)))
640 (while (and indicator (symbolp indicator)
641 (boundp indicator)
642 (not (eq indicator (symbol-value indicator))))
643 (setq indicator (symbol-value indicator)))
644 (push (list pretty-minor-mode mode indicator)
645 minor-modes))))
646 (if auto-fill-function
647 (push '("Auto Fill" auto-fill-mode " Fill")
648 minor-modes))
649 (setq minor-modes
650 (sort minor-modes
651 (lambda (a b) (string-lessp (car a) (car b)))))
652 (when minor-modes
653 (princ "Summary of minor modes:\n")
654 (dolist (mode minor-modes)
655 (let ((pretty-minor-mode (nth 0 mode))
656 (indicator (nth 2 mode)))
657 (princ (format " %s minor mode (%s):\n"
658 pretty-minor-mode
659 (if indicator
660 (format "indicator%s" indicator)
661 "no indicator")))))
662 (princ "\n(Full information about these minor modes
663 follows the description of the major mode.)\n\n"))
664 ;; Document the major mode.
665 (princ mode-name)
666 (princ " mode:\n")
667 (princ (documentation major-mode))
668 ;; Document the minor modes fully.
669 (dolist (mode minor-modes)
670 (let ((pretty-minor-mode (nth 0 mode))
671 (mode-function (nth 1 mode))
672 (indicator (nth 2 mode)))
673 (princ "\n\f\n")
674 (princ (format "%s minor mode (%s):\n"
675 pretty-minor-mode
676 (if indicator
677 (format "indicator%s" indicator)
678 "no indicator")))
679 (princ (documentation mode-function)))))
680 (print-help-return-message))))
683 (defun describe-minor-mode (minor-mode)
684 "Display documentation of a minor mode given as MINOR-MODE.
685 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
686 appeared on the mode-line."
687 (interactive (list (completing-read
688 "Minor mode: "
689 (nconc
690 (describe-minor-mode-completion-table-for-symbol)
691 (describe-minor-mode-completion-table-for-indicator)
692 ))))
693 (if (symbolp minor-mode)
694 (setq minor-mode (symbol-name minor-mode)))
695 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
696 (indicators (describe-minor-mode-completion-table-for-indicator)))
697 (cond
698 ((member minor-mode symbols)
699 (describe-minor-mode-from-symbol (intern minor-mode)))
700 ((member minor-mode indicators)
701 (describe-minor-mode-from-indicator minor-mode))
703 (error "No such minor mode: %s" minor-mode)))))
705 ;; symbol
706 (defun describe-minor-mode-completion-table-for-symbol ()
707 ;; In order to list up all minor modes, minor-mode-list
708 ;; is used here instead of minor-mode-alist.
709 (delq nil (mapcar 'symbol-name minor-mode-list)))
710 (defun describe-minor-mode-from-symbol (symbol)
711 "Display documentation of a minor mode given as a symbol, SYMBOL"
712 (interactive (list (intern (completing-read
713 "Minor mode symbol: "
714 (describe-minor-mode-completion-table-for-symbol)))))
715 (if (fboundp symbol)
716 (describe-function symbol)
717 (describe-variable symbol)))
719 ;; indicator
720 (defun describe-minor-mode-completion-table-for-indicator ()
721 (delq nil
722 (mapcar (lambda (x)
723 (let ((i (format-mode-line x)))
724 ;; remove first space if existed
725 (cond
726 ((= 0 (length i))
727 nil)
728 ((eq (aref i 0) ?\ )
729 (substring i 1))
731 i))))
732 minor-mode-alist)))
733 (defun describe-minor-mode-from-indicator (indicator)
734 "Display documentation of a minor mode specified by INDICATOR.
735 If you call this function interactively, you can give indicator which
736 is currently activated with completion."
737 (interactive (list
738 (completing-read
739 "Minor mode indicator: "
740 (describe-minor-mode-completion-table-for-indicator))))
741 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
742 (if minor-mode
743 (describe-minor-mode-from-symbol minor-mode)
744 (error "Cannot find minor mode for `%s'" indicator))))
746 (defun lookup-minor-mode-from-indicator (indicator)
747 "Return a minor mode symbol from its indicator on the modeline."
748 ;; remove first space if existed
749 (if (and (< 0 (length indicator))
750 (eq (aref indicator 0) ?\ ))
751 (setq indicator (substring indicator 1)))
752 (let ((minor-modes minor-mode-alist)
753 result)
754 (while minor-modes
755 (let* ((minor-mode (car (car minor-modes)))
756 (anindicator (format-mode-line
757 (car (cdr (car minor-modes))))))
758 ;; remove first space if existed
759 (if (and (stringp anindicator)
760 (> (length anindicator) 0)
761 (eq (aref anindicator 0) ?\ ))
762 (setq anindicator (substring anindicator 1)))
763 (if (equal indicator anindicator)
764 (setq result minor-mode
765 minor-modes nil)
766 (setq minor-modes (cdr minor-modes)))))
767 result))
770 ;;; Automatic resizing of temporary buffers.
772 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
773 "*Maximum height of a window displaying a temporary buffer.
774 This is the maximum height (in text lines) which `resize-temp-buffer-window'
775 will give to a window displaying a temporary buffer.
776 It can also be a function which will be called with the object corresponding
777 to the buffer to be displayed as argument and should return an integer
778 positive number."
779 :type '(choice integer function)
780 :group 'help
781 :version "20.4")
783 (define-minor-mode temp-buffer-resize-mode
784 "Toggle the mode which makes windows smaller for temporary buffers.
785 With prefix argument ARG, turn the resizing of windows displaying temporary
786 buffers on if ARG is positive or off otherwise.
787 This makes the window the right height for its contents, but never
788 more than `temp-buffer-max-height' nor less than `window-min-height'.
789 This applies to `help', `apropos' and `completion' buffers, and some others."
790 :global t :group 'help
791 (if temp-buffer-resize-mode
792 ;; `help-make-xrefs' may add a `back' button and thus increase the
793 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
794 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
795 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
797 (defun resize-temp-buffer-window ()
798 "Resize the current window to fit its contents.
799 Will not make it higher than `temp-buffer-max-height' nor smaller than
800 `window-min-height'. Do nothing if it is the only window on its frame, if it
801 is not as wide as the frame or if some of the window's contents are scrolled
802 out of view."
803 (unless (or (one-window-p 'nomini)
804 (not (pos-visible-in-window-p (point-min)))
805 (/= (frame-width) (window-width)))
806 (fit-window-to-buffer
807 (selected-window)
808 (if (functionp temp-buffer-max-height)
809 (funcall temp-buffer-max-height (current-buffer))
810 temp-buffer-max-height))))
812 ;; Provide this for the sake of define-minor-mode which generates
813 ;; defcustoms which require 'help'.
814 (provide 'help)
816 ;;; arch-tag: cf427352-27e9-49b7-9a6f-741ebab02423
817 ;;; help.el ends here