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