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