1 ;;; help.el --- help commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
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 3, or (at your option)
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.
28 ;; This code implements GNU Emacs' on-line help system, the one invoked by
29 ;; `M-x help-for-help'.
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
)
42 (let ((map (make-sparse-keymap)))
43 (define-key map
(char-to-string help-char
) 'help-for-help
)
44 (define-key map
[help] 'help-for-help)
45 (define-key map [f1] 'help-for-help)
46 (define-key map "." 'display-local-help)
47 (define-key map "?" 'help-for-help)
49 (define-key map "\C-a" 'about-emacs)
50 (define-key map "\C-c" 'describe-copying)
51 (define-key map "\C-d" 'describe-distribution)
52 (define-key map "\C-e" 'view-emacs-problems)
53 (define-key map "\C-f" 'view-emacs-FAQ)
54 (define-key map "\C-m" 'view-order-manuals)
55 (define-key map "\C-n" 'view-emacs-news)
56 (define-key map "\C-p" 'describe-gnu-project)
57 (define-key map "\C-t" 'view-emacs-todo)
58 (define-key map "\C-w" 'describe-no-warranty)
60 ;; This does not fit the pattern, but it is natural given the C-\ command.
61 (define-key map "\C-\\" 'describe-input-method)
63 (define-key map "C" 'describe-coding-system)
64 (define-key map "F" 'Info-goto-emacs-command-node)
65 (define-key map "I" 'describe-input-method)
66 (define-key map "K" 'Info-goto-emacs-key-command-node)
67 (define-key map "L" 'describe-language-environment)
68 (define-key map "S" 'info-lookup-symbol)
70 (define-key map "a" 'apropos-command)
71 (define-key map "b" 'describe-bindings)
72 (define-key map "c" 'describe-key-briefly)
73 (define-key map "d" 'apropos-documentation)
74 (define-key map "e" 'view-echo-area-messages)
75 (define-key map "f" 'describe-function)
76 (define-key map "h" 'view-hello-file)
78 (define-key map "i" 'info)
79 (define-key map "4i" 'info-other-window)
81 (define-key map "k" 'describe-key)
82 (define-key map "l" 'view-lossage)
83 (define-key map "m" 'describe-mode)
84 (define-key map "n" 'view-emacs-news)
85 (define-key map "p" 'finder-by-keyword)
86 (define-key map "r" 'info-emacs-manual)
87 (define-key map "s" 'describe-syntax)
88 (define-key map "t" 'help-with-tutorial)
89 (define-key map "w" 'where-is)
90 (define-key map "v" 'describe-variable)
91 (define-key map "q" 'help-quit)
93 "Keymap for characters following the Help key.")
95 (define-key global-map (char-to-string help-char) 'help-command)
96 (define-key global-map [help] 'help-command
)
97 (define-key global-map
[f1] 'help-command)
98 (fset 'help-command help-map)
100 (autoload 'finder-by-keyword "finder"
101 "Find packages matching a given keyword." t)
103 ;; insert-button makes the action nil if it is not store somewhere
104 (defvar help-button-cache nil)
108 "Just exit from the Help command's command loop."
112 (defvar help-return-method nil
113 "What to do to \"exit\" the help buffer.
115 (WINDOW . t) delete the selected window (and possibly its frame,
116 see `quit-window' and `View-quit'), go to WINDOW.
117 (WINDOW . quit-window) do quit-window, then select WINDOW.
118 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
120 (defun print-help-return-message (&optional function)
121 "Display or return message saying how to restore windows after help command.
122 This function assumes that `standard-output' is the help buffer.
123 It computes a message, and applies the optional argument FUNCTION to it.
124 If FUNCTION is nil, it applies `message', thus displaying the message.
125 In addition, this function sets up `help-return-method', which see, that
126 specifies what to do when the user exits the help buffer."
127 (and (not (get-buffer-window standard-output))
131 (special-display-p (buffer-name standard-output)))
132 (setq help-return-method (cons (selected-window) t))
133 ;; If the help output buffer is a special display buffer,
134 ;; don't say anything about how to get rid of it.
135 ;; First of all, the user will do that with the window
136 ;; manager, not with Emacs.
137 ;; Secondly, the buffer has not been displayed yet,
138 ;; so we don't know whether its frame will be selected.
140 (display-buffer-reuse-frames
141 (setq help-return-method (cons (selected-window)
144 ((not (one-window-p t))
145 (setq help-return-method
146 (cons (selected-window) 'quit-window))
147 "Type \\[display-buffer] RET to restore the other window.")
149 (setq help-return-method (cons (selected-window) t))
150 "Type \\[delete-other-windows] to remove help window.")
152 (setq help-return-method
153 (list (selected-window) (window-buffer)
154 (window-start) (window-point)))
155 "Type \\[switch-to-buffer] RET to remove help window."))))
156 (funcall (or function 'message)
159 (substitute-command-keys first-message))
160 (if first-message " ")
161 ;; If the help buffer will go in a separate frame,
162 ;; it's no use mentioning a command to scroll, so don't.
163 (if (or pop-up-windows
164 (special-display-p (buffer-name standard-output)))
166 (if (same-window-p (buffer-name standard-output))
167 ;; Say how to scroll this window.
168 (substitute-command-keys
169 "\\[scroll-up] to scroll the help.")
170 ;; Say how to scroll some other window.
171 (substitute-command-keys
172 "\\[scroll-other-window] to scroll the help."))))))))
174 ;; So keyboard macro definitions are documented correctly
175 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
177 (defalias 'help 'help-for-help-internal)
178 ;; find-function can find this.
179 (defalias 'help-for-help 'help-for-help-internal)
180 ;; It can't find this, but nobody will look.
181 (make-help-screen help-for-help-internal
182 "a b c C e f F i I k C-k l L m p r s t v w C-c C-d C-f C-n C-p C-t C-w . or ? :"
183 "You have typed %THIS-KEY%, the help character. Type a Help option:
184 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
186 a command-apropos. Type a list of words or a regexp; it shows a list of
187 commands whose names match. See also the apropos command.
188 b describe-bindings. Display a table of all key bindings.
189 c describe-key-briefly. Type a key sequence;
190 it displays the command name run by that key sequence.
191 C describe-coding-system. Type the name of the coding system to describe,
192 or just RET to describe the ones currently in use.
193 d apropos-documentation. Type a pattern (a list of words or a regexp), and
194 it shows a list of functions, variables, and other items whose
195 documentation matches that pattern. See also the apropos command.
196 e view-echo-area-messages. Go to the buffer that logs echo-area messages.
197 f describe-function. Type a function name and you see its documentation.
198 F Info-goto-emacs-command-node. Type a command name;
199 it goes to the on-line manual's section that describes the command.
200 h Display the HELLO file which illustrates various scripts.
201 i info. The Info documentation reader: read on-line manuals.
202 I describe-input-method. Describe a specific input method (if you type
203 its name) or the current input method (if you type just RET).
204 k describe-key. Type a key sequence;
205 it displays the full documentation for that key sequence.
206 K Info-goto-emacs-key-command-node. Type a key sequence;
207 it goes to the on-line manual's section that describes
208 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. Display documentation of current minor modes,
214 and the current major mode, including their special commands.
215 n view-emacs-news. Display news of recent Emacs changes.
216 p finder-by-keyword. Find packages matching a given topic keyword.
217 r info-emacs-manual. Display the Emacs manual in Info mode.
218 s describe-syntax. Display contents of syntax table, plus explanations.
219 S info-lookup-symbol. Type a symbol; it goes to that symbol in the
220 on-line manual for the programming language used in this buffer.
221 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
222 v describe-variable. Type name of a variable;
223 it displays the variable's documentation and value.
224 w where-is. Type a command name; it displays which keystrokes
226 . display-local-help. Display any available local help at point
229 C-a Display information about Emacs.
230 C-c Display Emacs copying permission (GNU General Public License).
231 C-d Display Emacs ordering information.
232 C-e Display info about Emacs problems.
233 C-f Display the Emacs FAQ.
234 C-m Display how to order printed Emacs manuals.
235 C-n Display news of recent Emacs changes.
236 C-p Display information about the GNU project.
237 C-t Display the Emacs TODO list.
238 C-w Display information on absence of warranty for GNU Emacs."
243 (defun function-called-at-point ()
244 "Return a function around point or else called by the list containing point.
245 If that doesn't give a function, return nil."
246 (or (with-syntax-table emacs-lisp-mode-syntax-table
247 (or (condition-case ()
249 (or (not (zerop (skip-syntax-backward "_w")))
250 (eq (char-syntax (following-char)) ?w)
251 (eq (char-syntax (following-char)) ?_)
253 (skip-chars-forward "'")
254 (let ((obj (read (current-buffer))))
255 (and (symbolp obj) (fboundp obj) obj)))
260 (narrow-to-region (max (point-min)
261 (- (point) 1000)) (point-max))
262 ;; Move up to surrounding paren, then after the open.
265 ;; If there is space here, this is probably something
266 ;; other than a real Lisp function call, so ignore it.
267 (if (looking-at "[ \t]")
268 (error "Probably not a Lisp function call"))
269 (let ((obj (read (current-buffer))))
270 (and (symbolp obj) (fboundp obj) obj))))
272 (let* ((str (find-tag-default))
273 (sym (if str (intern-soft str))))
274 (if (and sym (fboundp sym))
277 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
278 (setq sym (intern-soft (match-string 1 str)))
279 (and (fboundp sym) sym)))))))
282 ;;; `User' help functions
284 (defun describe-distribution ()
285 "Display info on how to obtain the latest version of GNU Emacs."
287 (view-file (expand-file-name "DISTRIB" data-directory)))
289 (defun describe-copying ()
290 "Display info on how you may redistribute copies of GNU Emacs."
292 (view-file (expand-file-name "COPYING" data-directory))
293 (goto-char (point-min)))
295 (defun describe-gnu-project ()
296 "Display info on the GNU project."
298 (view-file (expand-file-name "THE-GNU-PROJECT" data-directory))
299 (goto-char (point-min)))
301 (define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2")
303 (defun describe-no-warranty ()
304 "Display info on all the kinds of warranty Emacs does NOT have."
307 (let (case-fold-search)
308 (search-forward "NO WARRANTY")
311 (defun describe-prefix-bindings ()
312 "Describe the bindings of the prefix used to reach this command.
313 The prefix described consists of all but the last event
314 of the key sequence that ran this command."
316 (let ((key (this-command-keys)))
319 (substring key 0 (1- (length key)))
320 (let ((prefix (make-vector (1- (length key)) nil))
322 (while (< i (length prefix))
323 (aset prefix i (aref key i))
326 ;; Make C-h after a prefix, when not specifically bound,
327 ;; run describe-prefix-bindings.
328 (setq prefix-help-command 'describe-prefix-bindings)
330 (defun view-emacs-news (&optional version)
331 "Display info on recent changes to Emacs.
332 With argument, display info only for the selected version."
335 (setq version emacs-major-version))
336 (when (consp version)
342 (insert-file-contents
343 (expand-file-name file data-directory))
344 (while (re-search-forward
345 (if (member file '("NEWS.18" "NEWS.1-17"))
346 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
347 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
348 (setq res (cons (match-string-no-properties 1) res)))))
350 (directory-files data-directory nil
351 "^NEWS\\.[0-9][-0-9]*$" nil)))
352 (sort (delete-dups res) (lambda (a b) (string< b a)))))
353 (current (car all-versions)))
354 (setq version (completing-read
355 (format "Read NEWS for the version (default %s): " current)
356 all-versions nil nil nil nil current))
357 (if (integerp (string-to-number version))
358 (setq version (string-to-number version))
359 (unless (or (member version all-versions)
360 (<= (string-to-number version) (string-to-number current)))
361 (error "No news about version %s" version)))))
362 (when (integerp version)
363 (cond ((<= version 12)
364 (setq version (format "1.%d" version)))
366 (setq version (format "%d" version)))
367 ((> version emacs-major-version)
368 (error "No news about Emacs %d (yet)" version))))
369 (let* ((vn (if (stringp version)
370 (string-to-number version)
373 ((>= vn emacs-major-version) "NEWS")
374 ((< vn 18) "NEWS.1-17")
375 (t (format "NEWS.%d" vn))))
377 (view-file (expand-file-name file data-directory))
379 (goto-char (point-min))
380 (when (stringp version)
381 (when (re-search-forward
382 (concat (if (< vn 19)
383 "Changes in Emacs[ \t]*"
384 "^\* [^0-9\n]*") version "$")
390 (while (and (setq res
393 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
394 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
395 (equal (match-string-no-properties 1) version)))
396 (or res (goto-char (point-max)))
401 (defun view-emacs-todo (&optional arg)
402 "Display the Emacs TODO list."
404 (view-file (expand-file-name "TODO" data-directory)))
406 (define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2")
409 (defun view-echo-area-messages ()
410 "View the log of recent echo-area messages: the `*Messages*' buffer.
411 The number of messages retained in that buffer
412 is specified by the variable `message-log-max'."
414 (switch-to-buffer (get-buffer-create "*Messages*")))
416 (defun view-order-manuals ()
417 "Display the Emacs ORDERS file."
419 (view-file (expand-file-name "ORDERS" data-directory))
422 (defun view-emacs-FAQ ()
423 "Display the Emacs Frequently Asked Questions (FAQ) file."
425 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
428 (defun view-emacs-problems ()
429 "Display info on known problems with Emacs and possible workarounds."
431 (view-file (expand-file-name "PROBLEMS" data-directory)))
433 (defun view-lossage ()
434 "Display last 100 input keystrokes.
436 To record all your input on a file, use `open-dribble-file'."
438 (help-setup-xref (list #'view-lossage) (interactive-p))
439 (with-output-to-temp-buffer (help-buffer)
440 (princ (mapconcat (lambda (key)
441 (if (or (integerp key) (symbolp key) (listp key))
442 (single-key-description key)
443 (prin1-to-string key nil)))
446 (with-current-buffer standard-output
447 (goto-char (point-min))
448 (while (progn (move-to-column 50) (not (eobp)))
449 (when (search-forward " " nil t)
452 (print-help-return-message)))
457 (defun describe-bindings (&optional prefix buffer)
458 "Show a list of all defined keys, and their definitions.
459 We put that list in a buffer, and display the buffer.
461 The optional argument PREFIX, if non-nil, should be a key sequence;
462 then we display only bindings that start with that prefix.
463 The optional argument BUFFER specifies which buffer's bindings
464 to display (default, the current buffer). BUFFER can be a buffer
467 (or buffer (setq buffer (current-buffer)))
468 (help-setup-xref (list #'describe-bindings prefix buffer) (interactive-p))
469 (with-current-buffer buffer
470 (describe-bindings-internal nil prefix)))
472 ;; This function used to be in keymap.c.
473 (defun describe-bindings-internal (&optional menus prefix)
474 "Show a list of all defined keys, and their definitions.
475 We put that list in a buffer, and display the buffer.
477 The optional argument MENUS, if non-nil, says to mention menu bindings.
478 \(Ordinarily these are omitted from the output.)
479 The optional argument PREFIX, if non-nil, should be a key sequence;
480 then we display only bindings that start with that prefix."
482 (let ((buf (current-buffer)))
483 (with-output-to-temp-buffer "*Help*"
484 (with-current-buffer standard-output
485 (describe-buffer-bindings buf prefix menus)))))
487 (defun where-is (definition &optional insert)
488 "Print message listing key sequences that invoke the command DEFINITION.
489 Argument is a command definition, usually a symbol with a function definition.
490 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
492 (let ((fn (function-called-at-point))
493 (enable-recursive-minibuffers t)
495 (setq val (completing-read
497 (format "Where is command (default %s): " fn)
498 "Where is command: ")
499 obarray 'commandp t))
500 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
501 (unless definition (error "No command"))
502 (let ((func (indirect-function definition))
504 (standard-output (if insert (current-buffer) t)))
505 ;; In DEFS, find all symbols that are aliases for DEFINITION.
506 (mapatoms (lambda (symbol)
507 (and (fboundp symbol)
508 (not (eq symbol definition))
509 (eq func (condition-case ()
510 (indirect-function symbol)
512 (push symbol defs))))
513 ;; Look at all the symbols--first DEFINITION,
515 (dolist (symbol (cons definition defs))
516 (let* ((remapped (command-remapping symbol))
517 (keys (where-is-internal
518 symbol overriding-local-map nil nil remapped))
519 (keys (mapconcat 'key-description keys ", "))
523 (if (> (length keys) 0)
525 (format "%s (%s) (remapped from %s)"
526 keys remapped symbol)
527 (format "%s (%s)" keys symbol))
528 (format "M-x %s RET" symbol))
529 (if (> (length keys) 0)
531 (format "%s is remapped to %s which is on %s"
532 symbol remapped keys)
533 (format "%s is on %s" symbol keys))
534 ;; If this is the command the user asked about,
535 ;; and it is not on any key, say so.
536 ;; For other symbols, its aliases, say nothing
537 ;; about them unless they are on keys.
538 (if (eq symbol definition)
539 (format "%s is not on any key" symbol)))))
541 (unless (eq symbol definition)
542 (princ ";\n its alias "))
546 (defun help-key-description (key untranslated)
547 (let ((string (key-description key)))
548 (if (or (not untranslated)
549 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
551 (let ((otherstring (key-description untranslated)))
552 (if (equal string otherstring)
554 (format "%s (translated from %s)" string otherstring))))))
556 (defun describe-key-briefly (&optional key insert untranslated)
557 "Print the name of the function KEY invokes. KEY is a string.
558 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
559 If non-nil, UNTRANSLATED is a vector of the untranslated events.
560 It can also be a number in which case the untranslated events from
561 the last key hit are used.
563 If KEY is a menu item or a tool-bar button that is disabled, this command
564 temporarily enables it to allow getting help on disabled items and buttons."
566 (let ((enable-disabled-menus-and-buttons t)
567 (cursor-in-echo-area t)
571 ;; If yank-menu is empty, populate it temporarily, so that
572 ;; "Select and Paste" menu can generate a complete event.
573 (when (null (cdr yank-menu))
574 (setq saved-yank-menu (copy-sequence yank-menu))
575 (menu-bar-update-yank-menu "(any string)" nil))
576 (setq key (read-key-sequence "Describe key (or click or menu item): "))
577 ;; If KEY is a down-event, read and discard the
578 ;; corresponding up-event. Note that there are also
579 ;; down-events on scroll bars and mode lines: the actual
580 ;; event then is in the second element of the vector.
582 (let ((last-idx (1- (length key))))
583 (and (eventp (aref key last-idx))
584 (memq 'down (event-modifiers (aref key last-idx)))))
588 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
590 ;; Put yank-menu back as it was, if we changed it.
591 (when saved-yank-menu
592 (setq yank-menu (copy-sequence saved-yank-menu))
593 (fset 'yank-menu (cons 'keymap yank-menu))))))
594 (if (numberp untranslated)
595 (setq untranslated (this-single-command-raw-keys)))
596 (let* ((event (if (and (symbolp (aref key 0))
598 (consp (aref key 1)))
601 (modifiers (event-modifiers event))
602 (standard-output (if insert (current-buffer) t))
603 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
604 (memq 'drag modifiers)) " at that spot" ""))
605 (defn (key-binding key t))
607 ;; Handle the case where we faked an entry in "Select and Paste" menu.
608 (if (and (eq defn nil)
609 (stringp (aref key (1- (length key))))
610 (eq (key-binding (substring key 0 -1)) 'yank-menu))
611 (setq defn 'menu-bar-select-yank))
612 ;; Don't bother user with strings from (e.g.) the select-paste menu.
613 (if (stringp (aref key (1- (length key))))
614 (aset key (1- (length key)) "(any string)"))
615 (if (and (> (length untranslated) 0)
616 (stringp (aref untranslated (1- (length untranslated)))))
617 (aset untranslated (1- (length untranslated)) "(any string)"))
618 ;; Now describe the key, perhaps as changed.
619 (setq key-desc (help-key-description key untranslated))
620 (if (or (null defn) (integerp defn) (equal defn 'undefined))
621 (princ (format "%s%s is undefined" key-desc mouse-msg))
622 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
624 (defun describe-key (&optional key untranslated up-event)
625 "Display documentation of the function invoked by KEY.
626 KEY can be any kind of a key sequence; it can include keyboard events,
627 mouse events, and/or menu events. When calling from a program,
628 pass KEY as a string or a vector.
630 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
631 It can also be a number, in which case the untranslated events from
632 the last key sequence entered are used.
633 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
635 If KEY is a menu item or a tool-bar button that is disabled, this command
636 temporarily enables it to allow getting help on disabled items and buttons."
638 (let ((enable-disabled-menus-and-buttons t)
639 (cursor-in-echo-area t)
643 ;; If yank-menu is empty, populate it temporarily, so that
644 ;; "Select and Paste" menu can generate a complete event.
645 (when (null (cdr yank-menu))
646 (setq saved-yank-menu (copy-sequence yank-menu))
647 (menu-bar-update-yank-menu "(any string)" nil))
648 (setq key (read-key-sequence "Describe key (or click or menu item): "))
651 (prefix-numeric-value current-prefix-arg)
652 ;; If KEY is a down-event, read and include the
653 ;; corresponding up-event. Note that there are also
654 ;; down-events on scroll bars and mode lines: the actual
655 ;; event then is in the second element of the vector.
657 (let ((last-idx (1- (length key))))
658 (and (eventp (aref key last-idx))
659 (memq 'down (event-modifiers (aref key last-idx)))))
660 (or (and (eventp (aref key 0))
661 (memq 'down (event-modifiers (aref key 0)))
662 ;; However, for the C-down-mouse-2 popup
663 ;; menu, there is no subsequent up-event. In
664 ;; this case, the up-event is the next
665 ;; element in the supplied vector.
667 (and (> (length key) 1)
668 (eventp (aref key 1))
669 (memq 'down (event-modifiers (aref key 1)))))
671 ;; Put yank-menu back as it was, if we changed it.
672 (when saved-yank-menu
673 (setq yank-menu (copy-sequence saved-yank-menu))
674 (fset 'yank-menu (cons 'keymap yank-menu))))))
675 (if (numberp untranslated)
676 (setq untranslated (this-single-command-raw-keys)))
677 (let* ((event (aref key (if (and (symbolp (aref key 0))
679 (consp (aref key 1)))
682 (modifiers (event-modifiers event))
683 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
684 (memq 'drag modifiers)) " at that spot" ""))
685 (defn (key-binding key t))
686 defn-up defn-up-tricky ev-type
687 mouse-1-remapped mouse-1-tricky)
689 ;; Handle the case where we faked an entry in "Select and Paste" menu.
690 (when (and (eq defn nil)
691 (stringp (aref key (1- (length key))))
692 (eq (key-binding (substring key 0 -1)) 'yank-menu))
693 (setq defn 'menu-bar-select-yank))
694 (if (or (null defn) (integerp defn) (equal defn 'undefined))
695 (message "%s%s is undefined"
696 (help-key-description key untranslated) mouse-msg)
697 (help-setup-xref (list #'describe-function defn) (interactive-p))
698 ;; Don't bother user with strings from (e.g.) the select-paste menu.
699 (when (stringp (aref key (1- (length key))))
700 (aset key (1- (length key)) "(any string)"))
701 (when (and untranslated
702 (stringp (aref untranslated (1- (length untranslated)))))
703 (aset untranslated (1- (length untranslated))
705 ;; Need to do this before erasing *Help* buffer in case event
706 ;; is a mouse click in an existing *Help* buffer.
708 (setq ev-type (event-basic-type up-event))
709 (let ((sequence (vector up-event)))
710 (when (and (eq ev-type 'mouse-1)
711 mouse-1-click-follows-link
712 (not (eq mouse-1-click-follows-link 'double))
713 (setq mouse-1-remapped
714 (mouse-on-link-p (event-start up-event))))
715 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
716 (> mouse-1-click-follows-link 0)))
717 (cond ((stringp mouse-1-remapped)
718 (setq sequence mouse-1-remapped))
719 ((vectorp mouse-1-remapped)
720 (setcar up-event (elt mouse-1-remapped 0)))
721 (t (setcar up-event 'mouse-2))))
722 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
724 (setq sequence (vector up-event))
725 (aset sequence 0 'mouse-1)
726 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
727 (with-output-to-temp-buffer (help-buffer)
728 (princ (help-key-description key untranslated))
730 %s runs the command %S
733 (describe-function-1 defn)
735 (unless (or (null defn-up)
737 (equal defn-up 'undefined))
740 ----------------- up-event %s----------------
742 <%S>%s%s runs the command %S
744 (if mouse-1-tricky "(short click) " "")
747 " is remapped to <mouse-2>\nwhich" "")
749 (describe-function-1 defn-up))
750 (unless (or (null defn-up-tricky)
751 (integerp defn-up-tricky)
752 (eq defn-up-tricky 'undefined))
755 ----------------- up-event (long click) ----------------
757 Pressing <%S>%s for longer than %d milli-seconds
761 mouse-1-click-follows-link
763 (describe-function-1 defn-up-tricky)))
764 (print-help-return-message)))))
766 (defun describe-mode (&optional buffer)
767 "Display documentation of current major mode and minor modes.
768 A brief summary of the minor modes comes first, followed by the
769 major mode description. This is followed by detailed
770 descriptions of the minor modes, each on a separate page.
772 For this to work correctly for a minor mode, the mode's indicator
773 variable \(listed in `minor-mode-alist') must also be a function
774 whose documentation describes the minor mode."
776 (unless buffer (setq buffer (current-buffer)))
777 (help-setup-xref (list #'describe-mode buffer)
779 ;; For the sake of help-do-xref and help-xref-go-back,
780 ;; don't switch buffers before calling `help-buffer'.
781 (with-output-to-temp-buffer (help-buffer)
782 (with-current-buffer buffer
784 ;; Older packages do not register in minor-mode-list but only in
786 (dolist (x minor-mode-alist)
788 (unless (memq x minor-mode-list)
789 (push x minor-mode-list)))
790 ;; Find enabled minor mode we will want to mention.
791 (dolist (mode minor-mode-list)
792 ;; Document a minor mode if it is listed in minor-mode-alist,
793 ;; non-nil, and has a function definition.
794 (let ((fmode (or (get mode :minor-mode-function) mode)))
795 (and (boundp mode) (symbol-value mode)
797 (let ((pretty-minor-mode
798 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
801 (substring (symbol-name fmode)
802 0 (match-beginning 0)))
804 (push (list fmode pretty-minor-mode
805 (format-mode-line (assq mode minor-mode-alist)))
809 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
811 (princ "Enabled minor modes:\n")
812 (make-local-variable 'help-button-cache)
813 (with-current-buffer standard-output
814 (dolist (mode minor-modes)
815 (let ((mode-function (nth 0 mode))
816 (pretty-minor-mode (nth 1 mode))
817 (indicator (nth 2 mode)))
818 (add-text-properties 0 (length pretty-minor-mode)
819 '(face bold) pretty-minor-mode)
821 (goto-char (point-max))
823 (push (point-marker) help-button-cache)
824 ;; Document the minor modes fully.
825 (insert pretty-minor-mode)
826 (princ (format " minor mode (%s):\n"
827 (if (zerop (length indicator))
829 (format "indicator%s"
831 (princ (documentation mode-function)))
832 (insert-button pretty-minor-mode
833 'action (car help-button-cache)
835 'help-echo "mouse-2, RET: show full information")
841 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
842 ;; Document the major mode.
843 (let ((mode mode-name))
844 (with-current-buffer standard-output
845 (let ((start (point)))
846 (insert (format-mode-line mode))
847 (add-text-properties start (point) '(face bold)))))
849 (princ (documentation major-mode)))
850 (print-help-return-message))))
853 (defun describe-minor-mode (minor-mode)
854 "Display documentation of a minor mode given as MINOR-MODE.
855 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
856 appeared on the mode-line."
857 (interactive (list (completing-read
860 (describe-minor-mode-completion-table-for-symbol)
861 (describe-minor-mode-completion-table-for-indicator)
863 (if (symbolp minor-mode)
864 (setq minor-mode (symbol-name minor-mode)))
865 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
866 (indicators (describe-minor-mode-completion-table-for-indicator)))
868 ((member minor-mode symbols)
869 (describe-minor-mode-from-symbol (intern minor-mode)))
870 ((member minor-mode indicators)
871 (describe-minor-mode-from-indicator minor-mode))
873 (error "No such minor mode: %s" minor-mode)))))
876 (defun describe-minor-mode-completion-table-for-symbol ()
877 ;; In order to list up all minor modes, minor-mode-list
878 ;; is used here instead of minor-mode-alist.
879 (delq nil (mapcar 'symbol-name minor-mode-list)))
880 (defun describe-minor-mode-from-symbol (symbol)
881 "Display documentation of a minor mode given as a symbol, SYMBOL"
882 (interactive (list (intern (completing-read
883 "Minor mode symbol: "
884 (describe-minor-mode-completion-table-for-symbol)))))
886 (describe-function symbol)
887 (describe-variable symbol)))
890 (defun describe-minor-mode-completion-table-for-indicator ()
893 (let ((i (format-mode-line x)))
894 ;; remove first space if existed
903 (defun describe-minor-mode-from-indicator (indicator)
904 "Display documentation of a minor mode specified by INDICATOR.
905 If you call this function interactively, you can give indicator which
906 is currently activated with completion."
909 "Minor mode indicator: "
910 (describe-minor-mode-completion-table-for-indicator))))
911 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
913 (describe-minor-mode-from-symbol minor-mode)
914 (error "Cannot find minor mode for `%s'" indicator))))
916 (defun lookup-minor-mode-from-indicator (indicator)
917 "Return a minor mode symbol from its indicator on the modeline."
918 ;; remove first space if existed
919 (if (and (< 0 (length indicator))
920 (eq (aref indicator 0) ?\s))
921 (setq indicator (substring indicator 1)))
922 (let ((minor-modes minor-mode-alist)
925 (let* ((minor-mode (car (car minor-modes)))
926 (anindicator (format-mode-line
927 (car (cdr (car minor-modes))))))
928 ;; remove first space if existed
929 (if (and (stringp anindicator)
930 (> (length anindicator) 0)
931 (eq (aref anindicator 0) ?\s))
932 (setq anindicator (substring anindicator 1)))
933 (if (equal indicator anindicator)
934 (setq result minor-mode
936 (setq minor-modes (cdr minor-modes)))))
940 ;;; Automatic resizing of temporary buffers.
942 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
943 "Maximum height of a window displaying a temporary buffer.
944 This is effective only when Temp Buffer Resize mode is enabled.
945 The value is the maximum height (in lines) which `resize-temp-buffer-window'
946 will give to a window displaying a temporary buffer.
947 It can also be a function to be called to choose the height for such a buffer.
948 It gets one argumemt, the buffer, and should return a positive integer."
949 :type '(choice integer function)
953 (define-minor-mode temp-buffer-resize-mode
954 "Toggle the mode which makes windows smaller for temporary buffers.
955 With prefix argument ARG, turn the resizing of windows displaying temporary
956 buffers on if ARG is positive or off otherwise.
957 This makes the window the right height for its contents, but never
958 more than `temp-buffer-max-height' nor less than `window-min-height'.
959 This applies to `help', `apropos' and `completion' buffers, and some others."
960 :global t :group 'help
961 (if temp-buffer-resize-mode
962 ;; `help-make-xrefs' may add a `back' button and thus increase the
963 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
964 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
965 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
967 (defun resize-temp-buffer-window ()
968 "Resize the selected window to fit its contents.
969 Will not make it higher than `temp-buffer-max-height' nor smaller than
970 `window-min-height'. Do nothing if it is the only window on its frame, if it
971 is not as wide as the frame or if some of the window's contents are scrolled
973 (unless (or (one-window-p 'nomini)
974 (not (pos-visible-in-window-p (point-min)))
975 (not (window-full-width-p)))
976 (fit-window-to-buffer
978 (if (functionp temp-buffer-max-height)
979 (funcall temp-buffer-max-height (current-buffer))
980 temp-buffer-max-height))))
985 ;; arch-tag: cf427352-27e9-49b7-9a6f-741ebab02423
986 ;;; help.el ends here