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