More tweaks of skeleton documentation wrt \n behavior at bol/eol.
[bpt/emacs.git] / lisp / help.el
1 ;;; help.el --- help commands for Emacs
2
3 ;; Copyright (C) 1985-1986, 1993-1994, 1998-2014 Free Software Foundation, Inc.
4
5 ;; Maintainer: emacs-devel@gnu.org
6 ;; Keywords: help, internal
7 ;; Package: emacs
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 3 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; This code implements GNU Emacs's on-line help system, the one invoked by
27 ;; `M-x help-for-help'.
28
29 ;;; Code:
30
31 ;; Get the macro make-help-screen when this is compiled,
32 ;; or run interpreted, but not when the compiled code is loaded.
33 (eval-when-compile (require 'help-macro))
34
35 ;; This makes `with-output-to-temp-buffer' buffers use `help-mode'.
36 (add-hook 'temp-buffer-setup-hook 'help-mode-setup)
37 (add-hook 'temp-buffer-show-hook 'help-mode-finish)
38
39 ;; `help-window-point-marker' is a marker you can move to a valid
40 ;; position of the buffer shown in the help window in order to override
41 ;; the standard positioning mechanism (`point-min') chosen by
42 ;; `with-output-to-temp-buffer' and `with-temp-buffer-window'.
43 ;; `with-help-window' has this point nowhere before exiting. Currently
44 ;; used by `view-lossage' to assert that the last keystrokes are always
45 ;; visible.
46 (defvar help-window-point-marker (make-marker)
47 "Marker to override default `window-point' in help windows.")
48
49 (defvar help-map
50 (let ((map (make-sparse-keymap)))
51 (define-key map (char-to-string help-char) 'help-for-help)
52 (define-key map [help] 'help-for-help)
53 (define-key map [f1] 'help-for-help)
54 (define-key map "." 'display-local-help)
55 (define-key map "?" 'help-for-help)
56
57 (define-key map "\C-a" 'about-emacs)
58 (define-key map "\C-c" 'describe-copying)
59 (define-key map "\C-d" 'view-emacs-debugging)
60 (define-key map "\C-e" 'view-external-packages)
61 (define-key map "\C-f" 'view-emacs-FAQ)
62 (define-key map "\C-m" 'view-order-manuals)
63 (define-key map "\C-n" 'view-emacs-news)
64 (define-key map "\C-o" 'describe-distribution)
65 (define-key map "\C-p" 'view-emacs-problems)
66 (define-key map "\C-t" 'view-emacs-todo)
67 (define-key map "\C-w" 'describe-no-warranty)
68
69 ;; This does not fit the pattern, but it is natural given the C-\ command.
70 (define-key map "\C-\\" 'describe-input-method)
71
72 (define-key map "C" 'describe-coding-system)
73 (define-key map "F" 'Info-goto-emacs-command-node)
74 (define-key map "I" 'describe-input-method)
75 (define-key map "K" 'Info-goto-emacs-key-command-node)
76 (define-key map "L" 'describe-language-environment)
77 (define-key map "S" 'info-lookup-symbol)
78
79 (define-key map "a" 'apropos-command)
80 (define-key map "b" 'describe-bindings)
81 (define-key map "c" 'describe-key-briefly)
82 (define-key map "d" 'apropos-documentation)
83 (define-key map "e" 'view-echo-area-messages)
84 (define-key map "f" 'describe-function)
85 (define-key map "g" 'describe-gnu-project)
86 (define-key map "h" 'view-hello-file)
87
88 (define-key map "i" 'info)
89 (define-key map "4i" 'info-other-window)
90
91 (define-key map "k" 'describe-key)
92 (define-key map "l" 'view-lossage)
93 (define-key map "m" 'describe-mode)
94 (define-key map "n" 'view-emacs-news)
95 (define-key map "p" 'finder-by-keyword)
96 (define-key map "P" 'describe-package)
97 (define-key map "r" 'info-emacs-manual)
98 (define-key map "s" 'describe-syntax)
99 (define-key map "t" 'help-with-tutorial)
100 (define-key map "w" 'where-is)
101 (define-key map "v" 'describe-variable)
102 (define-key map "q" 'help-quit)
103 map)
104 "Keymap for characters following the Help key.")
105
106 (define-key global-map (char-to-string help-char) 'help-command)
107 (define-key global-map [help] 'help-command)
108 (define-key global-map [f1] 'help-command)
109 (fset 'help-command help-map)
110
111 ;; insert-button makes the action nil if it is not store somewhere
112 (defvar help-button-cache nil)
113
114 \f
115 (defun help-quit ()
116 "Just exit from the Help command's command loop."
117 (interactive)
118 nil)
119
120 (defvar help-return-method nil
121 "What to do to \"exit\" the help buffer.
122 This is a list
123 (WINDOW . t) delete the selected window (and possibly its frame,
124 see `quit-window'), go to WINDOW.
125 (WINDOW . quit-window) do quit-window, then select WINDOW.
126 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
127
128 (define-obsolete-function-alias 'print-help-return-message 'help-print-return-message "23.2")
129 (defun help-print-return-message (&optional function)
130 "Display or return message saying how to restore windows after help command.
131 This function assumes that `standard-output' is the help buffer.
132 It computes a message, and applies the optional argument FUNCTION to it.
133 If FUNCTION is nil, it applies `message', thus displaying the message.
134 In addition, this function sets up `help-return-method', which see, that
135 specifies what to do when the user exits the help buffer.
136
137 Do not call this in the scope of `with-help-window'."
138 (and (not (get-buffer-window standard-output))
139 (let ((first-message
140 (cond ((or
141 pop-up-frames
142 (special-display-p (buffer-name standard-output)))
143 (setq help-return-method (cons (selected-window) t))
144 ;; If the help output buffer is a special display buffer,
145 ;; don't say anything about how to get rid of it.
146 ;; First of all, the user will do that with the window
147 ;; manager, not with Emacs.
148 ;; Secondly, the buffer has not been displayed yet,
149 ;; so we don't know whether its frame will be selected.
150 nil)
151 ((not (one-window-p t))
152 (setq help-return-method
153 (cons (selected-window) 'quit-window))
154 "Type \\[display-buffer] RET to restore the other window.")
155 (pop-up-windows
156 (setq help-return-method (cons (selected-window) t))
157 "Type \\[delete-other-windows] to remove help window.")
158 (t
159 (setq help-return-method
160 (list (selected-window) (window-buffer)
161 (window-start) (window-point)))
162 "Type \\[switch-to-buffer] RET to remove help window."))))
163 (funcall (or function 'message)
164 (concat
165 (if first-message
166 (substitute-command-keys first-message))
167 (if first-message " ")
168 ;; If the help buffer will go in a separate frame,
169 ;; it's no use mentioning a command to scroll, so don't.
170 (if (or pop-up-windows
171 (special-display-p (buffer-name standard-output)))
172 nil
173 (if (same-window-p (buffer-name standard-output))
174 ;; Say how to scroll this window.
175 (substitute-command-keys
176 "\\[scroll-up] to scroll the help.")
177 ;; Say how to scroll some other window.
178 (substitute-command-keys
179 "\\[scroll-other-window] to scroll the help."))))))))
180
181 ;; So keyboard macro definitions are documented correctly
182 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
183
184 (defalias 'help 'help-for-help-internal)
185 ;; find-function can find this.
186 (defalias 'help-for-help 'help-for-help-internal)
187 ;; It can't find this, but nobody will look.
188 (make-help-screen help-for-help-internal
189 (purecopy "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?")
190 ;; Don't purecopy this one, because it's not evaluated (it's
191 ;; directly used as a docstring in a function definition, so it'll
192 ;; be moved to the DOC file anyway: no need for purecopying it).
193 "You have typed %THIS-KEY%, the help character. Type a Help option:
194 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
195
196 a PATTERN Show commands whose name matches the PATTERN (a list of words
197 or a regexp). See also the `apropos' command.
198 b Display all key bindings.
199 c KEYS Display the command name run by the given key sequence.
200 C CODING Describe the given coding system, or RET for current ones.
201 d PATTERN Show a list of functions, variables, and other items whose
202 documentation matches the PATTERN (a list of words or a regexp).
203 e Go to the *Messages* buffer which logs echo-area messages.
204 f FUNCTION Display documentation for the given function.
205 F COMMAND Show the on-line manual's section that describes the command.
206 g Display information about the GNU project.
207 h Display the HELLO file which illustrates various scripts.
208 i Start the Info documentation reader: read on-line manuals.
209 I METHOD Describe a specific input method, or RET for current.
210 k KEYS Display the full documentation for the key sequence.
211 K KEYS Show the on-line manual's section for the command bound to KEYS.
212 l Show last 300 input keystrokes (lossage).
213 L LANG-ENV Describes a specific language environment, or RET for current.
214 m Display documentation of current minor modes and current major mode,
215 including their special commands.
216 n Display news of recent Emacs changes.
217 p TOPIC Find packages matching a given topic keyword.
218 P PACKAGE Describe the given Emacs Lisp package.
219 r Display the Emacs manual in Info mode.
220 s Display contents of current syntax table, plus explanations.
221 S SYMBOL Show the section for the given symbol in the on-line manual
222 for the programming language used in this buffer.
223 t Start the Emacs learn-by-doing tutorial.
224 v VARIABLE Display the given variable's documentation and value.
225 w COMMAND Display which keystrokes invoke the given command (where-is).
226 . Display any available local help at point in the echo area.
227
228 C-a Information about Emacs.
229 C-c Emacs copying permission (GNU General Public License).
230 C-d Instructions for debugging GNU Emacs.
231 C-e External packages and information about Emacs.
232 C-f Emacs FAQ.
233 C-m How to order printed Emacs manuals.
234 C-n News of recent Emacs changes.
235 C-o Emacs ordering and distribution information.
236 C-p Info about known Emacs problems.
237 C-t Emacs TODO list.
238 C-w Information on absence of warranty for GNU Emacs."
239 help-map)
240
241 \f
242
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 (with-syntax-table emacs-lisp-mode-syntax-table
247 (or (condition-case ()
248 (save-excursion
249 (or (not (zerop (skip-syntax-backward "_w")))
250 (eq (char-syntax (following-char)) ?w)
251 (eq (char-syntax (following-char)) ?_)
252 (forward-sexp -1))
253 (skip-chars-forward "'")
254 (let ((obj (read (current-buffer))))
255 (and (symbolp obj) (fboundp obj) obj)))
256 (error nil))
257 (condition-case ()
258 (save-excursion
259 (save-restriction
260 (narrow-to-region (max (point-min)
261 (- (point) 1000)) (point-max))
262 ;; Move up to surrounding paren, then after the open.
263 (backward-up-list 1)
264 (forward-char 1)
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))))
271 (error nil))
272 (let* ((str (find-tag-default))
273 (sym (if str (intern-soft str))))
274 (if (and sym (fboundp sym))
275 sym
276 (save-match-data
277 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
278 (setq sym (intern-soft (match-string 1 str)))
279 (and (fboundp sym) sym))))))))
280
281 \f
282 ;;; `User' help functions
283
284 (defun view-help-file (file &optional dir)
285 (view-file (expand-file-name file (or dir data-directory)))
286 (goto-address-mode 1)
287 (goto-char (point-min)))
288
289 (defun describe-distribution ()
290 "Display info on how to obtain the latest version of GNU Emacs."
291 (interactive)
292 (view-help-file "DISTRIB"))
293
294 (defun describe-copying ()
295 "Display info on how you may redistribute copies of GNU Emacs."
296 (interactive)
297 (view-help-file "COPYING"))
298
299 (defun describe-gnu-project ()
300 "Display info on the GNU project."
301 (interactive)
302 (view-help-file "THE-GNU-PROJECT"))
303
304 (define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2")
305
306 (defun describe-no-warranty ()
307 "Display info on all the kinds of warranty Emacs does NOT have."
308 (interactive)
309 (describe-copying)
310 (let (case-fold-search)
311 (search-forward "Disclaimer of Warranty")
312 (forward-line 0)
313 (recenter 0)))
314
315 (defun describe-prefix-bindings ()
316 "Describe the bindings of the prefix used to reach this command.
317 The prefix described consists of all but the last event
318 of the key sequence that ran this command."
319 (interactive)
320 (let ((key (this-command-keys)))
321 (describe-bindings
322 (if (stringp key)
323 (substring key 0 (1- (length key)))
324 (let ((prefix (make-vector (1- (length key)) nil))
325 (i 0))
326 (while (< i (length prefix))
327 (aset prefix i (aref key i))
328 (setq i (1+ i)))
329 prefix)))))
330 ;; Make C-h after a prefix, when not specifically bound,
331 ;; run describe-prefix-bindings.
332 (setq prefix-help-command 'describe-prefix-bindings)
333
334 (defun view-emacs-news (&optional version)
335 "Display info on recent changes to Emacs.
336 With argument, display info only for the selected version."
337 (interactive "P")
338 (unless version
339 (setq version emacs-major-version))
340 (when (consp version)
341 (let* ((all-versions
342 (let (res)
343 (mapc
344 (lambda (file)
345 (with-temp-buffer
346 (insert-file-contents
347 (expand-file-name file data-directory))
348 (while (re-search-forward
349 (if (member file '("NEWS.18" "NEWS.1-17"))
350 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
351 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
352 (setq res (cons (match-string-no-properties 1) res)))))
353 (cons "NEWS"
354 (directory-files data-directory nil
355 "^NEWS\\.[0-9][-0-9]*$" nil)))
356 (sort (delete-dups res) (lambda (a b) (string< b a)))))
357 (current (car all-versions)))
358 (setq version (completing-read
359 (format "Read NEWS for the version (default %s): " current)
360 all-versions nil nil nil nil current))
361 (if (integerp (string-to-number version))
362 (setq version (string-to-number version))
363 (unless (or (member version all-versions)
364 (<= (string-to-number version) (string-to-number current)))
365 (error "No news about version %s" version)))))
366 (when (integerp version)
367 (cond ((<= version 12)
368 (setq version (format "1.%d" version)))
369 ((<= version 18)
370 (setq version (format "%d" version)))
371 ((> version emacs-major-version)
372 (error "No news about Emacs %d (yet)" version))))
373 (let* ((vn (if (stringp version)
374 (string-to-number version)
375 version))
376 (file (cond
377 ((>= vn emacs-major-version) "NEWS")
378 ((< vn 18) "NEWS.1-17")
379 (t (format "NEWS.%d" vn))))
380 res)
381 (view-file (expand-file-name file data-directory))
382 (widen)
383 (goto-char (point-min))
384 (when (stringp version)
385 (when (re-search-forward
386 (concat (if (< vn 19)
387 "Changes in Emacs[ \t]*"
388 "^\* [^0-9\n]*") version "$")
389 nil t)
390 (beginning-of-line)
391 (narrow-to-region
392 (point)
393 (save-excursion
394 (while (and (setq res
395 (re-search-forward
396 (if (< vn 19)
397 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
398 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
399 (equal (match-string-no-properties 1) version)))
400 (or res (goto-char (point-max)))
401 (beginning-of-line)
402 (point)))))))
403
404 (defun view-emacs-todo (&optional _arg)
405 "Display the Emacs TODO list."
406 (interactive "P")
407 (view-help-file "TODO"))
408
409 (define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2")
410
411
412 (defun view-echo-area-messages ()
413 "View the log of recent echo-area messages: the `*Messages*' buffer.
414 The number of messages retained in that buffer
415 is specified by the variable `message-log-max'."
416 (interactive)
417 (with-current-buffer (messages-buffer)
418 (goto-char (point-max))
419 (display-buffer (current-buffer))))
420
421 (defun view-order-manuals ()
422 "Display information on how to buy printed copies of Emacs manuals."
423 (interactive)
424 ;; (view-help-file "ORDERS")
425 (info "(emacs)Printed Books"))
426
427 (defun view-emacs-FAQ ()
428 "Display the Emacs Frequently Asked Questions (FAQ) file."
429 (interactive)
430 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
431 (info "(efaq)"))
432
433 (defun view-emacs-problems ()
434 "Display info on known problems with Emacs and possible workarounds."
435 (interactive)
436 (view-help-file "PROBLEMS"))
437
438 (defun view-emacs-debugging ()
439 "Display info on how to debug Emacs problems."
440 (interactive)
441 (view-help-file "DEBUG"))
442
443 ;; This used to visit MORE.STUFF; maybe it should just be removed.
444 (defun view-external-packages ()
445 "Display info on where to get more Emacs packages."
446 (interactive)
447 (info "(efaq)Packages that do not come with Emacs"))
448
449 (defun view-lossage ()
450 "Display last 300 input keystrokes.
451
452 To record all your input, use `open-dribble-file'."
453 (interactive)
454 (help-setup-xref (list #'view-lossage)
455 (called-interactively-p 'interactive))
456 (with-help-window (help-buffer)
457 (princ (mapconcat (lambda (key)
458 (if (or (integerp key) (symbolp key) (listp key))
459 (single-key-description key)
460 (prin1-to-string key nil)))
461 (recent-keys)
462 " "))
463 (with-current-buffer standard-output
464 (goto-char (point-min))
465 (while (progn (move-to-column 50) (not (eobp)))
466 (when (search-forward " " nil t)
467 (delete-char -1))
468 (insert "\n"))
469 ;; jidanni wants to see the last keystrokes immediately.
470 (set-marker help-window-point-marker (point)))))
471
472 \f
473 ;; Key bindings
474
475 (defun describe-bindings (&optional prefix buffer)
476 "Display a buffer showing a list of all defined keys, and their definitions.
477 The keys are displayed in order of precedence.
478
479 The optional argument PREFIX, if non-nil, should be a key sequence;
480 then we display only bindings that start with that prefix.
481 The optional argument BUFFER specifies which buffer's bindings
482 to display (default, the current buffer). BUFFER can be a buffer
483 or a buffer name."
484 (interactive)
485 (or buffer (setq buffer (current-buffer)))
486 (help-setup-xref (list #'describe-bindings prefix buffer)
487 (called-interactively-p 'interactive))
488 (with-help-window (help-buffer)
489 ;; Be aware that `describe-buffer-bindings' puts its output into
490 ;; the current buffer.
491 (with-current-buffer (help-buffer)
492 (describe-buffer-bindings buffer prefix))))
493
494 ;; This function used to be in keymap.c.
495 (defun describe-bindings-internal (&optional menus prefix)
496 "Show a list of all defined keys, and their definitions.
497 We put that list in a buffer, and display the buffer.
498
499 The optional argument MENUS, if non-nil, says to mention menu bindings.
500 \(Ordinarily these are omitted from the output.)
501 The optional argument PREFIX, if non-nil, should be a key sequence;
502 then we display only bindings that start with that prefix."
503 (declare (obsolete describe-buffer-bindings "24.4"))
504 (let ((buf (current-buffer)))
505 (with-help-window (help-buffer)
506 ;; Be aware that `describe-buffer-bindings' puts its output into
507 ;; the current buffer.
508 (with-current-buffer (help-buffer)
509 (describe-buffer-bindings buf prefix menus)))))
510
511 (defun where-is (definition &optional insert)
512 "Print message listing key sequences that invoke the command DEFINITION.
513 Argument is a command definition, usually a symbol with a function definition.
514 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
515 (interactive
516 (let ((fn (function-called-at-point))
517 (enable-recursive-minibuffers t)
518 val)
519 (setq val (completing-read
520 (if fn
521 (format "Where is command (default %s): " fn)
522 "Where is command: ")
523 obarray 'commandp t))
524 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
525 (unless definition (error "No command"))
526 (let ((func (indirect-function definition))
527 (defs nil)
528 (standard-output (if insert (current-buffer) standard-output)))
529 ;; In DEFS, find all symbols that are aliases for DEFINITION.
530 (mapatoms (lambda (symbol)
531 (and (fboundp symbol)
532 (not (eq symbol definition))
533 (eq func (condition-case ()
534 (indirect-function symbol)
535 (error symbol)))
536 (push symbol defs))))
537 ;; Look at all the symbols--first DEFINITION,
538 ;; then its aliases.
539 (dolist (symbol (cons definition defs))
540 (let* ((remapped (command-remapping symbol))
541 (keys (where-is-internal
542 symbol overriding-local-map nil nil remapped))
543 (keys (mapconcat 'key-description keys ", "))
544 string)
545 (setq string
546 (if insert
547 (if (> (length keys) 0)
548 (if remapped
549 (format "%s (%s) (remapped from %s)"
550 keys remapped symbol)
551 (format "%s (%s)" keys symbol))
552 (format "M-x %s RET" symbol))
553 (if (> (length keys) 0)
554 (if remapped
555 (format "%s is remapped to %s which is on %s"
556 symbol remapped keys)
557 (format "%s is on %s" symbol keys))
558 ;; If this is the command the user asked about,
559 ;; and it is not on any key, say so.
560 ;; For other symbols, its aliases, say nothing
561 ;; about them unless they are on keys.
562 (if (eq symbol definition)
563 (format "%s is not on any key" symbol)))))
564 (when string
565 (unless (eq symbol definition)
566 (princ ";\n its alias "))
567 (princ string)))))
568 nil)
569
570 (defun help-key-description (key untranslated)
571 (let ((string (key-description key)))
572 (if (or (not untranslated)
573 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
574 string
575 (let ((otherstring (key-description untranslated)))
576 (if (equal string otherstring)
577 string
578 (format "%s (translated from %s)" string otherstring))))))
579
580 (defun describe-key-briefly (&optional key insert untranslated)
581 "Print the name of the function KEY invokes. KEY is a string.
582 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
583 If non-nil, UNTRANSLATED is a vector of the untranslated events.
584 It can also be a number in which case the untranslated events from
585 the last key hit are used.
586
587 If KEY is a menu item or a tool-bar button that is disabled, this command
588 temporarily enables it to allow getting help on disabled items and buttons."
589 (interactive
590 (let ((enable-disabled-menus-and-buttons t)
591 (cursor-in-echo-area t)
592 saved-yank-menu)
593 (unwind-protect
594 (let (key)
595 ;; If yank-menu is empty, populate it temporarily, so that
596 ;; "Select and Paste" menu can generate a complete event.
597 (when (null (cdr yank-menu))
598 (setq saved-yank-menu (copy-sequence yank-menu))
599 (menu-bar-update-yank-menu "(any string)" nil))
600 (setq key (read-key-sequence "Describe key (or click or menu item): "))
601 ;; Clear the echo area message (Bug#7014).
602 (message nil)
603 ;; If KEY is a down-event, read and discard the
604 ;; corresponding up-event. Note that there are also
605 ;; down-events on scroll bars and mode lines: the actual
606 ;; event then is in the second element of the vector.
607 (and (vectorp key)
608 (let ((last-idx (1- (length key))))
609 (and (eventp (aref key last-idx))
610 (memq 'down (event-modifiers (aref key last-idx)))))
611 (read-event))
612 (list
613 key
614 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
615 1))
616 ;; Put yank-menu back as it was, if we changed it.
617 (when saved-yank-menu
618 (setq yank-menu (copy-sequence saved-yank-menu))
619 (fset 'yank-menu (cons 'keymap yank-menu))))))
620 (if (numberp untranslated)
621 (setq untranslated (this-single-command-raw-keys)))
622 (let* ((event (if (and (symbolp (aref key 0))
623 (> (length key) 1)
624 (consp (aref key 1)))
625 (aref key 1)
626 (aref key 0)))
627 (modifiers (event-modifiers event))
628 (standard-output (if insert (current-buffer) standard-output))
629 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
630 (memq 'drag modifiers)) " at that spot" ""))
631 (defn (key-binding key t))
632 key-desc)
633 ;; Handle the case where we faked an entry in "Select and Paste" menu.
634 (if (and (eq defn nil)
635 (stringp (aref key (1- (length key))))
636 (eq (key-binding (substring key 0 -1)) 'yank-menu))
637 (setq defn 'menu-bar-select-yank))
638 ;; Don't bother user with strings from (e.g.) the select-paste menu.
639 (if (stringp (aref key (1- (length key))))
640 (aset key (1- (length key)) "(any string)"))
641 (if (and (> (length untranslated) 0)
642 (stringp (aref untranslated (1- (length untranslated)))))
643 (aset untranslated (1- (length untranslated)) "(any string)"))
644 ;; Now describe the key, perhaps as changed.
645 (setq key-desc (help-key-description key untranslated))
646 (if (or (null defn) (integerp defn) (equal defn 'undefined))
647 (princ (format "%s%s is undefined" key-desc mouse-msg))
648 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
649
650 (defun describe-key (&optional key untranslated up-event)
651 "Display documentation of the function invoked by KEY.
652 KEY can be any kind of a key sequence; it can include keyboard events,
653 mouse events, and/or menu events. When calling from a program,
654 pass KEY as a string or a vector.
655
656 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
657 It can also be a number, in which case the untranslated events from
658 the last key sequence entered are used.
659 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
660
661 If KEY is a menu item or a tool-bar button that is disabled, this command
662 temporarily enables it to allow getting help on disabled items and buttons."
663 (interactive
664 (let ((enable-disabled-menus-and-buttons t)
665 (cursor-in-echo-area t)
666 saved-yank-menu)
667 (unwind-protect
668 (let (key)
669 ;; If yank-menu is empty, populate it temporarily, so that
670 ;; "Select and Paste" menu can generate a complete event.
671 (when (null (cdr yank-menu))
672 (setq saved-yank-menu (copy-sequence yank-menu))
673 (menu-bar-update-yank-menu "(any string)" nil))
674 (setq key (read-key-sequence "Describe key (or click or menu item): "))
675 (list
676 key
677 (prefix-numeric-value current-prefix-arg)
678 ;; If KEY is a down-event, read and include the
679 ;; corresponding up-event. Note that there are also
680 ;; down-events on scroll bars and mode lines: the actual
681 ;; event then is in the second element of the vector.
682 (and (vectorp key)
683 (let ((last-idx (1- (length key))))
684 (and (eventp (aref key last-idx))
685 (memq 'down (event-modifiers (aref key last-idx)))))
686 (or (and (eventp (aref key 0))
687 (memq 'down (event-modifiers (aref key 0)))
688 ;; However, for the C-down-mouse-2 popup
689 ;; menu, there is no subsequent up-event. In
690 ;; this case, the up-event is the next
691 ;; element in the supplied vector.
692 (= (length key) 1))
693 (and (> (length key) 1)
694 (eventp (aref key 1))
695 (memq 'down (event-modifiers (aref key 1)))))
696 (read-event))))
697 ;; Put yank-menu back as it was, if we changed it.
698 (when saved-yank-menu
699 (setq yank-menu (copy-sequence saved-yank-menu))
700 (fset 'yank-menu (cons 'keymap yank-menu))))))
701 (if (numberp untranslated)
702 (setq untranslated (this-single-command-raw-keys)))
703 (let* ((event (aref key (if (and (symbolp (aref key 0))
704 (> (length key) 1)
705 (consp (aref key 1)))
706 1
707 0)))
708 (modifiers (event-modifiers event))
709 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
710 (memq 'drag modifiers)) " at that spot" ""))
711 (defn (key-binding key t))
712 defn-up defn-up-tricky ev-type
713 mouse-1-remapped mouse-1-tricky)
714
715 ;; Handle the case where we faked an entry in "Select and Paste" menu.
716 (when (and (eq defn nil)
717 (stringp (aref key (1- (length key))))
718 (eq (key-binding (substring key 0 -1)) 'yank-menu))
719 (setq defn 'menu-bar-select-yank))
720 (if (or (null defn) (integerp defn) (equal defn 'undefined))
721 (message "%s%s is undefined"
722 (help-key-description key untranslated) mouse-msg)
723 (help-setup-xref (list #'describe-function defn)
724 (called-interactively-p 'interactive))
725 ;; Don't bother user with strings from (e.g.) the select-paste menu.
726 (when (stringp (aref key (1- (length key))))
727 (aset key (1- (length key)) "(any string)"))
728 (when (and untranslated
729 (stringp (aref untranslated (1- (length untranslated)))))
730 (aset untranslated (1- (length untranslated))
731 "(any string)"))
732 ;; Need to do this before erasing *Help* buffer in case event
733 ;; is a mouse click in an existing *Help* buffer.
734 (when up-event
735 (setq ev-type (event-basic-type up-event))
736 (let ((sequence (vector up-event)))
737 (when (and (eq ev-type 'mouse-1)
738 mouse-1-click-follows-link
739 (not (eq mouse-1-click-follows-link 'double))
740 (setq mouse-1-remapped
741 (mouse-on-link-p (event-start up-event))))
742 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
743 (> mouse-1-click-follows-link 0)))
744 (cond ((stringp mouse-1-remapped)
745 (setq sequence mouse-1-remapped))
746 ((vectorp mouse-1-remapped)
747 (setcar up-event (elt mouse-1-remapped 0)))
748 (t (setcar up-event 'mouse-2))))
749 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
750 (when mouse-1-tricky
751 (setq sequence (vector up-event))
752 (aset sequence 0 'mouse-1)
753 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
754 (with-help-window (help-buffer)
755 (princ (help-key-description key untranslated))
756 (princ (format "\
757 %s runs the command %S, which is "
758 mouse-msg defn))
759 (describe-function-1 defn)
760 (when up-event
761 (unless (or (null defn-up)
762 (integerp defn-up)
763 (equal defn-up 'undefined))
764 (princ (format "
765
766 ----------------- up-event %s----------------
767
768 %s%s%s runs the command %S, which is "
769 (if mouse-1-tricky "(short click) " "")
770 (key-description (vector up-event))
771 mouse-msg
772 (if mouse-1-remapped
773 " is remapped to <mouse-2>, which" "")
774 defn-up))
775 (describe-function-1 defn-up))
776 (unless (or (null defn-up-tricky)
777 (integerp defn-up-tricky)
778 (eq defn-up-tricky 'undefined))
779 (princ (format "
780
781 ----------------- up-event (long click) ----------------
782
783 Pressing <%S>%s for longer than %d milli-seconds
784 runs the command %S, which is "
785 ev-type mouse-msg
786 mouse-1-click-follows-link
787 defn-up-tricky))
788 (describe-function-1 defn-up-tricky)))))))
789 \f
790 (defun describe-mode (&optional buffer)
791 "Display documentation of current major mode and minor modes.
792 A brief summary of the minor modes comes first, followed by the
793 major mode description. This is followed by detailed
794 descriptions of the minor modes, each on a separate page.
795
796 For this to work correctly for a minor mode, the mode's indicator
797 variable \(listed in `minor-mode-alist') must also be a function
798 whose documentation describes the minor mode.
799
800 If called from Lisp with a non-nil BUFFER argument, display
801 documentation for the major and minor modes of that buffer."
802 (interactive "@")
803 (unless buffer (setq buffer (current-buffer)))
804 (help-setup-xref (list #'describe-mode buffer)
805 (called-interactively-p 'interactive))
806 ;; For the sake of help-do-xref and help-xref-go-back,
807 ;; don't switch buffers before calling `help-buffer'.
808 (with-help-window (help-buffer)
809 (with-current-buffer buffer
810 (let (minor-modes)
811 ;; Older packages do not register in minor-mode-list but only in
812 ;; minor-mode-alist.
813 (dolist (x minor-mode-alist)
814 (setq x (car x))
815 (unless (memq x minor-mode-list)
816 (push x minor-mode-list)))
817 ;; Find enabled minor mode we will want to mention.
818 (dolist (mode minor-mode-list)
819 ;; Document a minor mode if it is listed in minor-mode-alist,
820 ;; non-nil, and has a function definition.
821 (let ((fmode (or (get mode :minor-mode-function) mode)))
822 (and (boundp mode) (symbol-value mode)
823 (fboundp fmode)
824 (let ((pretty-minor-mode
825 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
826 (symbol-name fmode))
827 (capitalize
828 (substring (symbol-name fmode)
829 0 (match-beginning 0)))
830 fmode)))
831 (push (list fmode pretty-minor-mode
832 (format-mode-line (assq mode minor-mode-alist)))
833 minor-modes)))))
834 (setq minor-modes
835 (sort minor-modes
836 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
837 (when minor-modes
838 (princ "Enabled minor modes:\n")
839 (make-local-variable 'help-button-cache)
840 (with-current-buffer standard-output
841 (dolist (mode minor-modes)
842 (let ((mode-function (nth 0 mode))
843 (pretty-minor-mode (nth 1 mode))
844 (indicator (nth 2 mode)))
845 (add-text-properties 0 (length pretty-minor-mode)
846 '(face bold) pretty-minor-mode)
847 (save-excursion
848 (goto-char (point-max))
849 (princ "\n\f\n")
850 (push (point-marker) help-button-cache)
851 ;; Document the minor modes fully.
852 (insert pretty-minor-mode)
853 (princ (format " minor mode (%s):\n"
854 (if (zerop (length indicator))
855 "no indicator"
856 (format "indicator%s"
857 indicator))))
858 (princ (documentation mode-function)))
859 (insert-button pretty-minor-mode
860 'action (car help-button-cache)
861 'follow-link t
862 'help-echo "mouse-2, RET: show full information")
863 (newline)))
864 (forward-line -1)
865 (fill-paragraph nil)
866 (forward-line 1))
867
868 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
869 ;; Document the major mode.
870 (let ((mode mode-name))
871 (with-current-buffer standard-output
872 (let ((start (point)))
873 (insert (format-mode-line mode nil nil buffer))
874 (add-text-properties start (point) '(face bold)))))
875 (princ " mode")
876 (let* ((mode major-mode)
877 (file-name (find-lisp-object-file-name mode nil)))
878 (when file-name
879 (princ (concat " defined in `" (file-name-nondirectory file-name) "'"))
880 ;; Make a hyperlink to the library.
881 (with-current-buffer standard-output
882 (save-excursion
883 (re-search-backward "`\\([^`']+\\)'" nil t)
884 (help-xref-button 1 'help-function-def mode file-name)))))
885 (princ ":\n")
886 (princ (documentation major-mode)))))
887 ;; For the sake of IELM and maybe others
888 nil)
889
890
891 (defun describe-minor-mode (minor-mode)
892 "Display documentation of a minor mode given as MINOR-MODE.
893 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
894 appeared on the mode-line."
895 (interactive (list (completing-read
896 "Minor mode: "
897 (nconc
898 (describe-minor-mode-completion-table-for-symbol)
899 (describe-minor-mode-completion-table-for-indicator)
900 ))))
901 (if (symbolp minor-mode)
902 (setq minor-mode (symbol-name minor-mode)))
903 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
904 (indicators (describe-minor-mode-completion-table-for-indicator)))
905 (cond
906 ((member minor-mode symbols)
907 (describe-minor-mode-from-symbol (intern minor-mode)))
908 ((member minor-mode indicators)
909 (describe-minor-mode-from-indicator minor-mode))
910 (t
911 (error "No such minor mode: %s" minor-mode)))))
912
913 ;; symbol
914 (defun describe-minor-mode-completion-table-for-symbol ()
915 ;; In order to list up all minor modes, minor-mode-list
916 ;; is used here instead of minor-mode-alist.
917 (delq nil (mapcar 'symbol-name minor-mode-list)))
918
919 (defun describe-minor-mode-from-symbol (symbol)
920 "Display documentation of a minor mode given as a symbol, SYMBOL"
921 (interactive (list (intern (completing-read
922 "Minor mode symbol: "
923 (describe-minor-mode-completion-table-for-symbol)))))
924 (if (fboundp symbol)
925 (describe-function symbol)
926 (describe-variable symbol)))
927
928 ;; indicator
929 (defun describe-minor-mode-completion-table-for-indicator ()
930 (delq nil
931 (mapcar (lambda (x)
932 (let ((i (format-mode-line x)))
933 ;; remove first space if existed
934 (cond
935 ((= 0 (length i))
936 nil)
937 ((eq (aref i 0) ?\s)
938 (substring i 1))
939 (t
940 i))))
941 minor-mode-alist)))
942
943 (defun describe-minor-mode-from-indicator (indicator)
944 "Display documentation of a minor mode specified by INDICATOR.
945 If you call this function interactively, you can give indicator which
946 is currently activated with completion."
947 (interactive (list
948 (completing-read
949 "Minor mode indicator: "
950 (describe-minor-mode-completion-table-for-indicator))))
951 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
952 (if minor-mode
953 (describe-minor-mode-from-symbol minor-mode)
954 (error "Cannot find minor mode for `%s'" indicator))))
955
956 (defun lookup-minor-mode-from-indicator (indicator)
957 "Return a minor mode symbol from its indicator on the mode line."
958 ;; remove first space if existed
959 (if (and (< 0 (length indicator))
960 (eq (aref indicator 0) ?\s))
961 (setq indicator (substring indicator 1)))
962 (let ((minor-modes minor-mode-alist)
963 result)
964 (while minor-modes
965 (let* ((minor-mode (car (car minor-modes)))
966 (anindicator (format-mode-line
967 (car (cdr (car minor-modes))))))
968 ;; remove first space if existed
969 (if (and (stringp anindicator)
970 (> (length anindicator) 0)
971 (eq (aref anindicator 0) ?\s))
972 (setq anindicator (substring anindicator 1)))
973 (if (equal indicator anindicator)
974 (setq result minor-mode
975 minor-modes nil)
976 (setq minor-modes (cdr minor-modes)))))
977 result))
978 \f
979 ;;; Automatic resizing of temporary buffers.
980 (defcustom temp-buffer-max-height
981 (lambda (buffer)
982 (if (eq (selected-window) (frame-root-window))
983 (/ (x-display-pixel-height) (frame-char-height) 2)
984 (/ (- (frame-height) 2) 2)))
985 "Maximum height of a window displaying a temporary buffer.
986 This is effective only when Temp Buffer Resize mode is enabled.
987 The value is the maximum height (in lines) which
988 `resize-temp-buffer-window' will give to a window displaying a
989 temporary buffer. It can also be a function to be called to
990 choose the height for such a buffer. It gets one argument, the
991 buffer, and should return a positive integer. At the time the
992 function is called, the window to be resized is selected."
993 :type '(choice integer function)
994 :group 'help
995 :version "24.3")
996
997 (defcustom temp-buffer-max-width
998 (lambda (buffer)
999 (if (eq (selected-window) (frame-root-window))
1000 (/ (x-display-pixel-width) (frame-char-width) 2)
1001 (/ (- (frame-width) 2) 2)))
1002 "Maximum width of a window displaying a temporary buffer.
1003 This is effective only when Temp Buffer Resize mode is enabled.
1004 The value is the maximum width (in columns) which
1005 `resize-temp-buffer-window' will give to a window displaying a
1006 temporary buffer. It can also be a function to be called to
1007 choose the width for such a buffer. It gets one argument, the
1008 buffer, and should return a positive integer. At the time the
1009 function is called, the window to be resized is selected."
1010 :type '(choice integer function)
1011 :group 'help
1012 :version "24.4")
1013
1014 (define-minor-mode temp-buffer-resize-mode
1015 "Toggle auto-resizing temporary buffer windows (Temp Buffer Resize Mode).
1016 With a prefix argument ARG, enable Temp Buffer Resize mode if ARG
1017 is positive, and disable it otherwise. If called from Lisp,
1018 enable the mode if ARG is omitted or nil.
1019
1020 When Temp Buffer Resize mode is enabled, the windows in which we
1021 show a temporary buffer are automatically resized in height to
1022 fit the buffer's contents, but never more than
1023 `temp-buffer-max-height' nor less than `window-min-height'.
1024
1025 A window is resized only if it has been specially created for the
1026 buffer. Windows that have shown another buffer before are not
1027 resized. A frame is resized only if `fit-frame-to-buffer' is
1028 non-nil.
1029
1030 This mode is used by `help', `apropos' and `completion' buffers,
1031 and some others."
1032 :global t :group 'help
1033 (if temp-buffer-resize-mode
1034 ;; `help-make-xrefs' may add a `back' button and thus increase the
1035 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
1036 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
1037 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
1038
1039 (defun resize-temp-buffer-window (&optional window)
1040 "Resize WINDOW to fit its contents.
1041 WINDOW must be a live window and defaults to the selected one.
1042 Do not resize if WINDOW was not created by `display-buffer'.
1043
1044 If WINDOW is part of a vertical combination, restrain its new
1045 size by `temp-buffer-max-height' and do not resize if its minimum
1046 accessible position is scrolled out of view. If WINDOW is part
1047 of a horizontal combination, restrain its new size by
1048 `temp-buffer-max-width'. In both cases, the value of the option
1049 `fit-window-to-buffer-horizontally' can inhibit resizing.
1050
1051 If WINDOW is the root window of its frame, resize the frame
1052 provided `fit-frame-to-buffer' is non-nil."
1053 (setq window (window-normalize-window window t))
1054 (let ((height (if (functionp temp-buffer-max-height)
1055 (with-selected-window window
1056 (funcall temp-buffer-max-height (window-buffer)))
1057 temp-buffer-max-height))
1058 (width (if (functionp temp-buffer-max-width)
1059 (with-selected-window window
1060 (funcall temp-buffer-max-width (window-buffer)))
1061 temp-buffer-max-width))
1062 (quit-cadr (cadr (window-parameter window 'quit-restore))))
1063 ;; Resize WINDOW iff it was made by `display-buffer'.
1064 (when (or (and (eq quit-cadr 'window)
1065 (or (and (window-combined-p window)
1066 (not (eq fit-window-to-buffer-horizontally
1067 'only))
1068 (pos-visible-in-window-p (point-min) window))
1069 (and (window-combined-p window t)
1070 fit-window-to-buffer-horizontally)))
1071 (and (eq quit-cadr 'frame)
1072 fit-frame-to-buffer
1073 (eq window (frame-root-window window))))
1074 (fit-window-to-buffer window height nil width))))
1075
1076 ;;; Help windows.
1077 (defcustom help-window-select 'other
1078 "Non-nil means select help window for viewing.
1079 Choices are:
1080 never (nil) Select help window only if there is no other window
1081 on its frame.
1082 other Select help window unless the selected window is the
1083 only other window on the help window's frame.
1084 always (t) Always select the help window.
1085
1086 This option has effect if and only if the help window was created
1087 by `with-help-window'"
1088 :type '(choice (const :tag "never (nil)" nil)
1089 (const :tag "other" other)
1090 (const :tag "always (t)" t))
1091 :group 'help
1092 :version "23.1")
1093
1094 (defcustom help-enable-auto-load t
1095 "Whether Help commands can perform autoloading.
1096 If non-nil, whenever \\[describe-function] is called for an
1097 autoloaded function whose docstring contains any key substitution
1098 construct (see `substitute-command-keys'), the library is loaded,
1099 so that the documentation can show the right key bindings."
1100 :type 'boolean
1101 :group 'help
1102 :version "24.3")
1103
1104 (defun help-window-display-message (quit-part window &optional scroll)
1105 "Display message telling how to quit and scroll help window.
1106 QUIT-PART is a string telling how to quit the help window WINDOW.
1107 Optional argument SCROLL non-nil means tell how to scroll WINDOW.
1108 SCROLL equal `other' means tell how to scroll the \"other\"
1109 window."
1110 (let ((scroll-part
1111 (cond
1112 ;; If we don't have QUIT-PART we probably reuse a window
1113 ;; showing the same buffer so we don't show any message.
1114 ((not quit-part) nil)
1115 ((pos-visible-in-window-p
1116 (with-current-buffer (window-buffer window)
1117 (point-max)) window t)
1118 ;; Buffer end is at least partially visible, no need to talk
1119 ;; about scrolling.
1120 ".")
1121 ((eq scroll 'other)
1122 ", \\[scroll-other-window] to scroll help.")
1123 (scroll ", \\[scroll-up] to scroll help."))))
1124 (message "%s"
1125 (substitute-command-keys (concat quit-part scroll-part)))))
1126
1127 (defun help-window-setup (window &optional value)
1128 "Set up help window WINDOW for `with-help-window'.
1129 WINDOW is the window used for displaying the help buffer.
1130 Return VALUE."
1131 (let* ((help-buffer (when (window-live-p window)
1132 (window-buffer window)))
1133 (help-setup (when (window-live-p window)
1134 (car (window-parameter window 'quit-restore)))))
1135 (when help-buffer
1136 ;; Handle `help-window-point-marker'.
1137 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1138 (set-window-point window help-window-point-marker)
1139 ;; Reset `help-window-point-marker'.
1140 (set-marker help-window-point-marker nil))
1141
1142 (cond
1143 ((or (eq window (selected-window))
1144 (and (or (eq help-window-select t)
1145 (eq help-setup 'frame)
1146 (and (eq help-window-select 'other)
1147 (eq (window-frame window) (selected-frame))
1148 (> (length (window-list nil 'no-mini)) 2)))
1149 (select-window window)))
1150 ;; The help window is or gets selected ...
1151 (help-window-display-message
1152 (cond
1153 ((eq help-setup 'window)
1154 ;; ... and is new, ...
1155 "Type \"q\" to delete help window")
1156 ((eq help-setup 'frame)
1157 "Type \"q\" to quit the help frame")
1158 ((eq help-setup 'other)
1159 ;; ... or displayed some other buffer before.
1160 "Type \"q\" to restore previous buffer"))
1161 window t))
1162 ((and (eq (window-frame window) (selected-frame))
1163 (= (length (window-list nil 'no-mini)) 2))
1164 ;; There are two windows on the help window's frame and the
1165 ;; other one is the selected one.
1166 (help-window-display-message
1167 (cond
1168 ((eq help-setup 'window)
1169 "Type \\[delete-other-windows] to delete the help window")
1170 ((eq help-setup 'other)
1171 "Type \"q\" in help window to restore its previous buffer"))
1172 window 'other))
1173 (t
1174 ;; The help window is not selected ...
1175 (help-window-display-message
1176 (cond
1177 ((eq help-setup 'window)
1178 ;; ... and is new, ...
1179 "Type \"q\" in help window to delete it")
1180 ((eq help-setup 'other)
1181 ;; ... or displayed some other buffer before.
1182 "Type \"q\" in help window to restore previous buffer"))
1183 window))))
1184 ;; Return VALUE.
1185 value))
1186
1187 ;; `with-help-window' is a wrapper for `with-temp-buffer-window'
1188 ;; providing the following additional twists:
1189
1190 ;; (1) It puts the buffer in `help-mode' (via `help-mode-setup') and
1191 ;; adds cross references (via `help-mode-finish').
1192
1193 ;; (2) It issues a message telling how to scroll and quit the help
1194 ;; window (via `help-window-setup').
1195
1196 ;; (3) An option (customizable via `help-window-select') to select the
1197 ;; help window automatically.
1198
1199 ;; (4) A marker (`help-window-point-marker') to move point in the help
1200 ;; window to an arbitrary buffer position.
1201 (defmacro with-help-window (buffer-name &rest body)
1202 "Display buffer named BUFFER-NAME in a help window.
1203 Evaluate the forms in BODY with standard output bound to a buffer
1204 called BUFFER-NAME (creating it if it does not exist), put that
1205 buffer in `help-mode', display the buffer in a window (see
1206 `with-temp-buffer-window' for details) and issue a message how to
1207 deal with that \"help\" window when it's no more needed. Select
1208 the help window if the current value of the user option
1209 `help-window-select' says so. Return last value in BODY."
1210 (declare (indent 1) (debug t))
1211 `(progn
1212 ;; Make `help-window-point-marker' point nowhere. The only place
1213 ;; where this should be set to a buffer position is within BODY.
1214 (set-marker help-window-point-marker nil)
1215 (let ((temp-buffer-window-setup-hook
1216 (cons 'help-mode-setup temp-buffer-window-setup-hook))
1217 (temp-buffer-window-show-hook
1218 (cons 'help-mode-finish temp-buffer-window-show-hook)))
1219 (with-temp-buffer-window
1220 ,buffer-name nil 'help-window-setup (progn ,@body)))))
1221
1222 ;; Called from C, on encountering `help-char' when reading a char.
1223 ;; Don't print to *Help*; that would clobber Help history.
1224 (defun help-form-show ()
1225 "Display the output of a non-nil `help-form'."
1226 (let ((msg (eval help-form)))
1227 (if (stringp msg)
1228 (with-output-to-temp-buffer " *Char Help*"
1229 (princ msg)))))
1230
1231 \f
1232 ;; The following functions used to be in help-fns.el, which is not preloaded.
1233 ;; But for various reasons, they are more widely needed, so they were
1234 ;; moved to this file, which is preloaded. http://debbugs.gnu.org/17001
1235
1236 (defun help-split-fundoc (docstring def)
1237 "Split a function DOCSTRING into the actual doc and the usage info.
1238 Return (USAGE . DOC) or nil if there's no usage info, where USAGE info
1239 is a string describing the argument list of DEF, such as
1240 \"(apply FUNCTION &rest ARGUMENTS)\".
1241 DEF is the function whose usage we're looking for in DOCSTRING."
1242 ;; Functions can get the calling sequence at the end of the doc string.
1243 ;; In cases where `function' has been fset to a subr we can't search for
1244 ;; function's name in the doc string so we use `fn' as the anonymous
1245 ;; function name instead.
1246 (when (and docstring (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring))
1247 (cons (format "(%s%s"
1248 ;; Replace `fn' with the actual function name.
1249 (if (symbolp def) def "anonymous")
1250 (match-string 1 docstring))
1251 (unless (zerop (match-beginning 0))
1252 (substring docstring 0 (match-beginning 0))))))
1253
1254 (defun help-add-fundoc-usage (docstring arglist)
1255 "Add the usage info to DOCSTRING.
1256 If DOCSTRING already has a usage info, then just return it unchanged.
1257 The usage info is built from ARGLIST. DOCSTRING can be nil.
1258 ARGLIST can also be t or a string of the form \"(FUN ARG1 ARG2 ...)\"."
1259 (unless (stringp docstring) (setq docstring ""))
1260 (if (or (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring)
1261 (eq arglist t))
1262 docstring
1263 (concat docstring
1264 (if (string-match "\n?\n\\'" docstring)
1265 (if (< (- (match-end 0) (match-beginning 0)) 2) "\n" "")
1266 "\n\n")
1267 (if (and (stringp arglist)
1268 (string-match "\\`([^ ]+\\(.*\\))\\'" arglist))
1269 (concat "(fn" (match-string 1 arglist) ")")
1270 (format "%S" (help-make-usage 'fn arglist))))))
1271
1272 (defun help-function-arglist (def &optional preserve-names)
1273 "Return a formal argument list for the function DEF.
1274 IF PRESERVE-NAMES is non-nil, return a formal arglist that uses
1275 the same names as used in the original source code, when possible."
1276 ;; Handle symbols aliased to other symbols.
1277 (if (and (symbolp def) (fboundp def)) (setq def (indirect-function def)))
1278 ;; If definition is a macro, find the function inside it.
1279 (if (eq (car-safe def) 'macro) (setq def (cdr def)))
1280 (cond
1281 ((and (byte-code-function-p def) (listp (aref def 0))) (aref def 0))
1282 ((eq (car-safe def) 'lambda) (nth 1 def))
1283 ((eq (car-safe def) 'closure) (nth 2 def))
1284 ((or (and (byte-code-function-p def) (integerp (aref def 0)))
1285 (subrp def))
1286 (or (when preserve-names
1287 (let* ((doc (condition-case nil (documentation def) (error nil)))
1288 (docargs (if doc (car (help-split-fundoc doc nil))))
1289 (arglist (if docargs
1290 (cdar (read-from-string (downcase docargs)))))
1291 (valid t))
1292 ;; Check validity.
1293 (dolist (arg arglist)
1294 (unless (and (symbolp arg)
1295 (let ((name (symbol-name arg)))
1296 (if (eq (aref name 0) ?&)
1297 (memq arg '(&rest &optional))
1298 (not (string-match "\\." name)))))
1299 (setq valid nil)))
1300 (when valid arglist)))
1301 (let* ((args-desc (if (not (subrp def))
1302 (aref def 0)
1303 (let ((a (subr-arity def)))
1304 (logior (car a)
1305 (if (numberp (cdr a))
1306 (lsh (cdr a) 8)
1307 (lsh 1 7))))))
1308 (max (lsh args-desc -8))
1309 (min (logand args-desc 127))
1310 (rest (logand args-desc 128))
1311 (arglist ()))
1312 (dotimes (i min)
1313 (push (intern (concat "arg" (number-to-string (1+ i)))) arglist))
1314 (when (> max min)
1315 (push '&optional arglist)
1316 (dotimes (i (- max min))
1317 (push (intern (concat "arg" (number-to-string (+ 1 i min))))
1318 arglist)))
1319 (unless (zerop rest) (push '&rest arglist) (push 'rest arglist))
1320 (nreverse arglist))))
1321 ((and (autoloadp def) (not (eq (nth 4 def) 'keymap)))
1322 "[Arg list not available until function definition is loaded.]")
1323 (t t)))
1324
1325 (defun help-make-usage (function arglist)
1326 (cons (if (symbolp function) function 'anonymous)
1327 (mapcar (lambda (arg)
1328 (if (not (symbolp arg)) arg
1329 (let ((name (symbol-name arg)))
1330 (cond
1331 ((string-match "\\`&" name) arg)
1332 ((string-match "\\`_" name)
1333 (intern (upcase (substring name 1))))
1334 (t (intern (upcase name)))))))
1335 arglist)))
1336
1337 \f
1338 (provide 'help)
1339
1340 ;;; help.el ends here