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