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