(set_window_buffer): Fix last change.
[emacs.git] / lisp / help.el
blob982d007676d582d4e170db1be3795ccbc77b7fdb
1 ;;; help.el --- help commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: help, internal
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; This code implements GNU Emacs' on-line help system, the one invoked by
28 ;; `M-x help-for-help'.
30 ;;; Code:
32 ;; Get the macro make-help-screen when this is compiled,
33 ;; or run interpreted, but not when the compiled code is loaded.
34 (eval-when-compile (require 'help-macro))
35 (eval-when-compile (require 'view))
37 (defvar help-map (make-sparse-keymap)
38 "Keymap for characters following the Help key.")
40 (defvar help-mode-map (make-sparse-keymap)
41 "Keymap for help mode.")
43 (define-key global-map (char-to-string help-char) 'help-command)
44 (define-key global-map [help] 'help-command)
45 (define-key global-map [f1] 'help-command)
46 (fset 'help-command help-map)
48 (define-key help-map (char-to-string help-char) 'help-for-help)
49 (define-key help-map [help] 'help-for-help)
50 (define-key help-map [f1] 'help-for-help)
51 (define-key help-map "?" 'help-for-help)
53 (define-key help-map "\C-c" 'describe-copying)
54 (define-key help-map "\C-d" 'describe-distribution)
55 (define-key help-map "\C-w" 'describe-no-warranty)
56 (define-key help-map "\C-p" 'describe-project)
57 (define-key help-map "a" 'apropos-command)
59 (define-key help-map "b" 'describe-bindings)
61 (define-key help-map "c" 'describe-key-briefly)
62 (define-key help-map "k" 'describe-key)
64 (define-key help-map "d" 'describe-function)
65 (define-key help-map "f" 'describe-function)
67 (define-key help-map "F" 'view-emacs-FAQ)
69 (define-key help-map "i" 'info)
70 (define-key help-map "4i" 'info-other-window)
71 (define-key help-map "\C-f" 'Info-goto-emacs-command-node)
72 (define-key help-map "\C-k" 'Info-goto-emacs-key-command-node)
73 (define-key help-map "\C-i" 'info-lookup-symbol)
75 (define-key help-map "l" 'view-lossage)
77 (define-key help-map "m" 'describe-mode)
79 (define-key help-map "\C-n" 'view-emacs-news)
80 (define-key help-map "n" 'view-emacs-news)
82 (define-key help-map "p" 'finder-by-keyword)
83 (autoload 'finder-by-keyword "finder"
84 "Find packages matching a given keyword." t)
86 (define-key help-map "P" 'view-emacs-problems)
88 (define-key help-map "s" 'describe-syntax)
90 (define-key help-map "t" 'help-with-tutorial)
92 (define-key help-map "w" 'where-is)
94 (define-key help-map "v" 'describe-variable)
96 (define-key help-map "q" 'help-quit)
98 (define-key help-mode-map [mouse-2] 'help-follow-mouse)
99 (define-key help-mode-map "\C-c\C-b" 'help-go-back)
100 (define-key help-mode-map "\C-c\C-c" 'help-follow)
101 (define-key help-mode-map "\t" 'help-next-ref)
102 (define-key help-mode-map [backtab] 'help-previous-ref)
103 (define-key help-mode-map [(shift tab)] 'help-previous-ref)
104 ;; Documentation only, since we use minor-mode-overriding-map-alist.
105 (define-key help-mode-map "\r" 'help-follow)
107 (defvar help-xref-stack nil
108 "A stack of ways by which to return to help buffers after following xrefs.
109 Used by `help-follow' and `help-xref-go-back'.
110 An element looks like (POSITION FUNCTION ARGS...), where POSITION is
111 `(POINT . BUFFER-NAME)'.
112 To use the element, do (apply FUNCTION ARGS) then goto the point in
113 the named buffer.")
114 (put 'help-xref-stack 'permanent-local t)
116 (defvar help-xref-stack-item nil
117 "An item for `help-follow' in this buffer to push onto `help-xref-stack'.
118 The format is (FUNCTION ARGS...).")
119 (put 'help-xref-stack-item 'permanent-local t)
121 (setq-default help-xref-stack nil help-xref-stack-item nil)
123 (defcustom help-mode-hook nil
124 "Hook run by `help-mode'."
125 :type 'hook
126 :group 'help)
128 (defun help-mode ()
129 "Major mode for viewing help text and navigating references in it.
130 Entry to this mode runs the normal hook `help-mode-hook'.
131 Commands:
132 \\{help-mode-map}"
133 (interactive)
134 (kill-all-local-variables)
135 (use-local-map help-mode-map)
136 (setq mode-name "Help")
137 (setq major-mode 'help-mode)
138 (make-local-variable 'font-lock-defaults)
139 (setq font-lock-defaults nil) ; font-lock would defeat xref
140 (view-mode)
141 (make-local-variable 'view-no-disable-on-exit)
142 (setq view-no-disable-on-exit t)
143 ;; `help-make-xrefs' would be run here if not invoked from
144 ;; `help-mode-maybe'.
145 (run-hooks 'help-mode-hook))
147 (defun help-mode-setup ()
148 (help-mode)
149 (setq buffer-read-only nil))
151 (add-hook 'temp-buffer-setup-hook 'help-mode-setup)
153 (defun help-mode-finish ()
154 (when (eq major-mode 'help-mode)
155 ;; View mode's read-only status of existing *Help* buffer is lost
156 ;; by with-output-to-temp-buffer.
157 (toggle-read-only 1)
158 (help-make-xrefs (current-buffer)))
159 (setq view-return-to-alist
160 (list (cons (selected-window) help-return-method))))
162 (add-hook 'temp-buffer-show-hook 'help-mode-finish)
164 (defun help-quit ()
165 "Just exit from the Help command's command loop."
166 (interactive)
167 nil)
169 (defun help-with-tutorial (&optional arg)
170 "Select the Emacs learn-by-doing tutorial.
171 If there is a tutorial version written in the language
172 of the selected language environment, that version is used.
173 If there's no tutorial in that language, `TUTORIAL' is selected.
174 With arg, you are asked to choose which language."
175 (interactive "P")
176 (let ((lang (if arg
177 (read-language-name 'tutorial "Language: " "English")
178 (if (get-language-info current-language-environment 'tutorial)
179 current-language-environment
180 "English")))
181 file filename)
182 (setq filename (get-language-info lang 'tutorial))
183 (setq file (expand-file-name (concat "~/" filename)))
184 (delete-other-windows)
185 (if (get-file-buffer file)
186 (switch-to-buffer (get-file-buffer file))
187 (switch-to-buffer (create-file-buffer file))
188 (setq buffer-file-name file)
189 (setq default-directory (expand-file-name "~/"))
190 (setq buffer-auto-save-file-name nil)
191 (insert-file-contents (expand-file-name filename data-directory))
192 (goto-char (point-min))
193 (search-forward "\n<<")
194 (beginning-of-line)
195 (delete-region (point) (progn (end-of-line) (point)))
196 (let ((n (- (window-height (selected-window))
197 (count-lines (point-min) (point))
198 6)))
199 (if (< n 12)
200 (newline n)
201 ;; Some people get confused by the large gap.
202 (newline (/ n 2))
203 (insert "[Middle of page left blank for didactic purposes. "
204 "Text continues below]")
205 (newline (- n (/ n 2)))))
206 (goto-char (point-min))
207 (set-buffer-modified-p nil))))
209 (defun mode-line-key-binding (key)
210 "Value is the binding of KEY in the mode line or nil if none."
211 (let (string-info defn)
212 (when (and (eq 'mode-line (aref key 0))
213 (consp (setq string-info (nth 4 (event-start (aref key 1))))))
214 (let* ((string (car string-info))
215 (pos (cdr string-info))
216 (local-map (and (> pos 0)
217 (< pos (length string))
218 (get-text-property pos 'local-map string))))
219 (setq defn (and local-map (lookup-key local-map key)))))
220 defn))
222 (defun describe-key-briefly (key &optional insert)
223 "Print the name of the function KEY invokes. KEY is a string.
224 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
225 (interactive "kDescribe key briefly: \nP")
226 (save-excursion
227 (let ((modifiers (event-modifiers (aref key 0)))
228 (standard-output (if insert (current-buffer) t))
229 window position)
230 ;; For a mouse button event, go to the button it applies to
231 ;; to get the right key bindings. And go to the right place
232 ;; in case the keymap depends on where you clicked.
233 (if (or (memq 'click modifiers) (memq 'down modifiers)
234 (memq 'drag modifiers))
235 (setq window (posn-window (event-start (aref key 0)))
236 position (posn-point (event-start (aref key 0)))))
237 (if (windowp window)
238 (progn
239 (set-buffer (window-buffer window))
240 (goto-char position)))
241 ;; Ok, now look up the key and name the command.
242 (let ((defn (or (mode-line-key-binding key)
243 (key-binding key)))
244 (key-desc (key-description key)))
245 (if (or (null defn) (integerp defn))
246 (princ (format "%s is undefined" key-desc))
247 (princ (format (if insert
248 "`%s' (`%s')"
249 (if (windowp window)
250 "%s at that spot runs the command %s"
251 "%s runs the command %s"))
252 key-desc
253 (if (symbolp defn) defn (prin1-to-string defn)))))))))
255 (defvar help-return-method nil
256 "What to do to \"exit\" the help buffer.
257 This is a list
258 (WINDOW . t) delete the selected window, go to WINDOW.
259 (WINDOW . quit-window) do quit-window, then select WINDOW.
260 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
262 (defun print-help-return-message (&optional function)
263 "Display or return message saying how to restore windows after help command.
264 Computes a message and applies the optional argument FUNCTION to it.
265 If FUNCTION is nil, applies `message' to it, thus printing it."
266 (and (not (get-buffer-window standard-output))
267 (let ((first-message
268 (cond ((special-display-p (buffer-name standard-output))
269 (setq help-return-method (cons (selected-window) t))
270 ;; If the help output buffer is a special display buffer,
271 ;; don't say anything about how to get rid of it.
272 ;; First of all, the user will do that with the window
273 ;; manager, not with Emacs.
274 ;; Secondly, the buffer has not been displayed yet,
275 ;; so we don't know whether its frame will be selected.
276 nil)
277 (display-buffer-reuse-frames
278 (setq help-return-method (cons (selected-window)
279 'quit-window))
280 nil)
281 ((not (one-window-p t))
282 (setq help-return-method
283 (cons (selected-window) 'quit-window))
284 "Type \\[switch-to-buffer-other-window] RET to restore the other window.")
285 (pop-up-windows
286 (setq help-return-method (cons (selected-window) t))
287 "Type \\[delete-other-windows] to remove help window.")
289 (setq help-return-method
290 (list (selected-window) (window-buffer)
291 (window-start) (window-point)))
292 "Type \\[switch-to-buffer] RET to remove help window."))))
293 (funcall (or function 'message)
294 (concat
295 (if first-message
296 (substitute-command-keys first-message))
297 (if first-message " ")
298 ;; If the help buffer will go in a separate frame,
299 ;; it's no use mentioning a command to scroll, so don't.
300 (if (special-display-p (buffer-name standard-output))
302 (if (same-window-p (buffer-name standard-output))
303 ;; Say how to scroll this window.
304 (substitute-command-keys
305 "\\[scroll-up] to scroll the help.")
306 ;; Say how to scroll some other window.
307 (substitute-command-keys
308 "\\[scroll-other-window] to scroll the help."))))))))
310 (defun describe-key (key)
311 "Display documentation of the function invoked by KEY. KEY is a string."
312 (interactive "kDescribe key: ")
313 (save-excursion
314 (let ((modifiers (event-modifiers (aref key 0)))
315 window position)
316 ;; For a mouse button event, go to the button it applies to
317 ;; to get the right key bindings. And go to the right place
318 ;; in case the keymap depends on where you clicked.
319 (if (or (memq 'click modifiers) (memq 'down modifiers)
320 (memq 'drag modifiers))
321 (setq window (posn-window (event-start (aref key 0)))
322 position (posn-point (event-start (aref key 0)))))
323 (if (windowp window)
324 (progn
325 (set-buffer (window-buffer window))
326 (goto-char position)))
327 (let ((defn (or (mode-line-key-binding key) (key-binding key))))
328 (if (or (null defn) (integerp defn))
329 (message "%s is undefined" (key-description key))
330 (with-output-to-temp-buffer "*Help*"
331 (princ (key-description key))
332 (if (windowp window)
333 (princ " at that spot"))
334 (princ " runs the command ")
335 (prin1 defn)
336 (princ "\n which is ")
337 (describe-function-1 defn nil (interactive-p))
338 (print-help-return-message)))))))
340 (defun describe-mode ()
341 "Display documentation of current major mode and minor modes.
342 The major mode description comes first, followed by the minor modes,
343 each on a separate page.
345 For this to work correctly for a minor mode, the mode's indicator variable
346 \(listed in `minor-mode-alist') must also be a function whose documentation
347 describes the minor mode."
348 (interactive)
349 (with-output-to-temp-buffer "*Help*"
350 (when minor-mode-alist
351 (princ "The major mode is described first.
352 For minor modes, see following pages.\n\n"))
353 (princ mode-name)
354 (princ " mode:\n")
355 (princ (documentation major-mode))
356 (help-setup-xref (list #'help-xref-mode (current-buffer)) (interactive-p))
357 (let ((minor-modes minor-mode-alist))
358 (while minor-modes
359 (let* ((minor-mode (car (car minor-modes)))
360 (indicator (car (cdr (car minor-modes)))))
361 ;; Document a minor mode if it is listed in minor-mode-alist,
362 ;; bound locally in this buffer, non-nil, and has a function
363 ;; definition.
364 (if (and (boundp minor-mode)
365 (symbol-value minor-mode)
366 (fboundp minor-mode))
367 (let ((pretty-minor-mode minor-mode))
368 (if (string-match "-mode$" (symbol-name minor-mode))
369 (setq pretty-minor-mode
370 (capitalize
371 (substring (symbol-name minor-mode)
372 0 (match-beginning 0)))))
373 (while (and indicator (symbolp indicator)
374 (boundp indicator)
375 (not (eq indicator (symbol-value indicator))))
376 (setq indicator (symbol-value indicator)))
377 (princ "\n\f\n")
378 (princ (format "%s minor mode (%s):\n"
379 pretty-minor-mode
380 (if indicator
381 (format "indicator%s" indicator)
382 "no indicator")))
383 (princ (documentation minor-mode)))))
384 (setq minor-modes (cdr minor-modes))))
385 (print-help-return-message)))
387 ;; So keyboard macro definitions are documented correctly
388 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
390 (defun describe-distribution ()
391 "Display info on how to obtain the latest version of GNU Emacs."
392 (interactive)
393 (find-file-read-only
394 (expand-file-name "DISTRIB" data-directory)))
396 (defun describe-copying ()
397 "Display info on how you may redistribute copies of GNU Emacs."
398 (interactive)
399 (find-file-read-only
400 (expand-file-name "COPYING" data-directory))
401 (goto-char (point-min)))
403 (defun describe-project ()
404 "Display info on the GNU project."
405 (interactive)
406 (find-file-read-only
407 (expand-file-name "GNU" data-directory))
408 (goto-char (point-min)))
410 (defun describe-no-warranty ()
411 "Display info on all the kinds of warranty Emacs does NOT have."
412 (interactive)
413 (describe-copying)
414 (let (case-fold-search)
415 (search-forward "NO WARRANTY")
416 (recenter 0)))
418 (defun describe-prefix-bindings ()
419 "Describe the bindings of the prefix used to reach this command.
420 The prefix described consists of all but the last event
421 of the key sequence that ran this command."
422 (interactive)
423 (let* ((key (this-command-keys)))
424 (describe-bindings
425 (if (stringp key)
426 (substring key 0 (1- (length key)))
427 (let ((prefix (make-vector (1- (length key)) nil))
428 (i 0))
429 (while (< i (length prefix))
430 (aset prefix i (aref key i))
431 (setq i (1+ i)))
432 prefix)))))
433 ;; Make C-h after a prefix, when not specifically bound,
434 ;; run describe-prefix-bindings.
435 (setq prefix-help-command 'describe-prefix-bindings)
437 (defun view-emacs-news (&optional arg)
438 "Display info on recent changes to Emacs.
439 With numeric argument, display information on correspondingly older changes."
440 (interactive "P")
441 (let* ((arg (if arg (prefix-numeric-value arg) 0))
442 (file (cond ((eq arg 0) "NEWS")
443 ((eq arg 1) "ONEWS")
445 (nth (- arg 2)
446 (nreverse (directory-files data-directory
447 nil "^ONEWS\\.[0-9]+$"
448 nil)))))))
449 (if file
450 (find-file-read-only (expand-file-name file data-directory))
451 (error "No such old news"))))
453 (defun view-emacs-FAQ ()
454 "Display the Emacs Frequently Asked Questions (FAQ) file."
455 (interactive)
456 ;;; (find-file-read-only (expand-file-name "FAQ" data-directory))
457 (info "(efaq)"))
459 (defun view-emacs-problems ()
460 "Display info on known problems with Emacs and possible workarounds."
461 (interactive)
462 (view-file (expand-file-name "PROBLEMS" data-directory)))
464 (defun view-lossage ()
465 "Display last 100 input keystrokes."
466 (interactive)
467 (with-output-to-temp-buffer "*Help*"
468 (princ (mapconcat (function (lambda (key)
469 (if (or (integerp key)
470 (symbolp key)
471 (listp key))
472 (single-key-description key)
473 (prin1-to-string key nil))))
474 (recent-keys)
475 " "))
476 (save-excursion
477 (set-buffer standard-output)
478 (goto-char (point-min))
479 (while (progn (move-to-column 50) (not (eobp)))
480 (search-forward " " nil t)
481 (insert "\n"))
482 (setq help-xref-stack nil
483 help-xref-stack-item nil))
484 (print-help-return-message)))
486 (defalias 'help 'help-for-help)
487 (make-help-screen help-for-help
488 "a b c C f F C-f i I k C-k l L m n p s t v w C-c C-d C-n C-p C-w; ? for help:"
489 "You have typed %THIS-KEY%, the help character. Type a Help option:
490 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
492 a command-apropos. Give a substring, and see a list of commands
493 (functions interactively callable) that contain
494 that substring. See also the apropos command.
495 b describe-bindings. Display table of all key bindings.
496 c describe-key-briefly. Type a command key sequence;
497 it prints the function name that sequence runs.
498 C describe-coding-system. This describes either a specific coding system
499 (if you type its name) or the coding systems currently in use
500 (if you type just RET).
501 f describe-function. Type a function name and get documentation of it.
502 C-f Info-goto-emacs-command-node. Type a function name;
503 it takes you to the Info node for that command.
504 i info. The info documentation reader.
505 I describe-input-method. Describe a specific input method (if you type
506 its name) or the current input method (if you type just RET).
507 C-i info-lookup-symbol. Display the definition of a specific symbol
508 as found in the manual for the language this buffer is written in.
509 k describe-key. Type a command key sequence;
510 it displays the full documentation.
511 C-k Info-goto-emacs-key-command-node. Type a command key sequence;
512 it takes you to the Info node for the command bound to that key.
513 l view-lossage. Show last 100 characters you typed.
514 L describe-language-environment. This describes either a
515 specific language environment (if you type its name)
516 or the current language environment (if you type just RET).
517 m describe-mode. Print documentation of current minor modes,
518 and the current major mode, including their special commands.
519 n view-emacs-news. Display news of recent Emacs changes.
520 p finder-by-keyword. Find packages matching a given topic keyword.
521 s describe-syntax. Display contents of syntax table, plus explanations
522 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
523 v describe-variable. Type name of a variable;
524 it displays the variable's documentation and value.
525 w where-is. Type command name; it prints which keystrokes
526 invoke that command.
528 F Display the frequently asked questions file.
529 h Display the HELLO file which illustrates various scripts.
530 C-c Display Emacs copying permission (General Public License).
531 C-d Display Emacs ordering information.
532 C-n Display news of recent Emacs changes.
533 C-p Display information about the GNU project.
534 C-w Display information on absence of warranty for GNU Emacs."
535 help-map)
537 (defun function-called-at-point ()
538 "Return a function around point or else called by the list containing point.
539 If that doesn't give a function, return nil."
540 (with-syntax-table emacs-lisp-mode-syntax-table
541 (or (condition-case ()
542 (save-excursion
543 (or (not (zerop (skip-syntax-backward "_w")))
544 (eq (char-syntax (following-char)) ?w)
545 (eq (char-syntax (following-char)) ?_)
546 (forward-sexp -1))
547 (skip-chars-forward "'")
548 (let ((obj (read (current-buffer))))
549 (and (symbolp obj) (fboundp obj) obj)))
550 (error nil))
551 (condition-case ()
552 (save-excursion
553 (save-restriction
554 (narrow-to-region (max (point-min)
555 (- (point) 1000)) (point-max))
556 ;; Move up to surrounding paren, then after the open.
557 (backward-up-list 1)
558 (forward-char 1)
559 ;; If there is space here, this is probably something
560 ;; other than a real Lisp function call, so ignore it.
561 (if (looking-at "[ \t]")
562 (error "Probably not a Lisp function call"))
563 (let ((obj (read (current-buffer))))
564 (and (symbolp obj) (fboundp obj) obj))))
565 (error nil)))))
567 (defvar symbol-file-load-history-loaded nil
568 "Non-nil means we have loaded the file `fns-VERSION.el' in `exec-directory'.
569 That file records the part of `load-history' for preloaded files,
570 which is cleared out before dumping to make Emacs smaller.")
572 (defun load-symbol-file-load-history ()
573 "Load the file `fns-VERSION.el' in `exec-directory' if not already done.
574 That file records the part of `load-history' for preloaded files,
575 which is cleared out before dumping to make Emacs smaller."
576 (unless symbol-file-load-history-loaded
577 (load (expand-file-name
578 ;; fns-XX.YY.ZZ.el does not work on DOS filesystem.
579 (if (eq system-type 'ms-dos)
580 "fns.el"
581 (format "fns-%s.el" emacs-version))
582 exec-directory)
583 ;; The file name fns-%s.el already has a .el extension.
584 nil nil t)
585 (setq symbol-file-load-history-loaded t)))
587 (defun symbol-file (function)
588 "Return the input source from which FUNCTION was loaded.
589 The value is normally a string that was passed to `load':
590 either an absolute file name, or a library name
591 \(with no directory name and no `.el' or `.elc' at the end).
592 It can also be nil, if the definition is not associated with any file."
593 (load-symbol-file-load-history)
594 (let ((files load-history)
595 file functions)
596 (while files
597 (if (memq function (cdr (car files)))
598 (setq file (car (car files)) files nil))
599 (setq files (cdr files)))
600 file))
602 (defun describe-function (function)
603 "Display the full documentation of FUNCTION (a symbol)."
604 (interactive
605 (let ((fn (function-called-at-point))
606 (enable-recursive-minibuffers t)
607 val)
608 (setq val (completing-read (if fn
609 (format "Describe function (default %s): " fn)
610 "Describe function: ")
611 obarray 'fboundp t nil nil (symbol-name fn)))
612 (list (if (equal val "")
613 fn (intern val)))))
614 (if function
615 (with-output-to-temp-buffer "*Help*"
616 (prin1 function)
617 ;; Use " is " instead of a colon so that
618 ;; it is easier to get out the function name using forward-sexp.
619 (princ " is ")
620 (describe-function-1 function nil (interactive-p))
621 (print-help-return-message)
622 (save-excursion
623 (set-buffer standard-output)
624 ;; Return the text we displayed.
625 (buffer-string)))
626 (message "You didn't specify a function")))
628 (defun describe-function-1 (function parens interactive-p)
629 (let* ((def (if (symbolp function)
630 (symbol-function function)
631 function))
632 file-name string need-close
633 (beg (if (commandp def) "an interactive " "a ")))
634 (setq string
635 (cond ((or (stringp def)
636 (vectorp def))
637 "a keyboard macro")
638 ((subrp def)
639 (if (eq 'unevalled (cdr (subr-arity def)))
640 (concat beg "special form")
641 (concat beg "built-in function")))
642 ((byte-code-function-p def)
643 (concat beg "compiled Lisp function"))
644 ((symbolp def)
645 (while (symbolp (symbol-function def))
646 (setq def (symbol-function def)))
647 (format "an alias for `%s'" def))
648 ((eq (car-safe def) 'lambda)
649 (concat beg "Lisp function"))
650 ((eq (car-safe def) 'macro)
651 "a Lisp macro")
652 ((eq (car-safe def) 'mocklisp)
653 "a mocklisp function")
654 ((eq (car-safe def) 'autoload)
655 (setq file-name (nth 1 def))
656 (format "%s autoloaded %s"
657 (if (commandp def) "an interactive" "an")
658 (if (eq (nth 4 def) 'keymap) "keymap"
659 (if (nth 4 def) "Lisp macro" "Lisp function"))
661 ;; perhaps use keymapp here instead
662 ((eq (car-safe def) 'keymap)
663 (let ((is-full nil)
664 (elts (cdr-safe def)))
665 (while elts
666 (if (char-table-p (car-safe elts))
667 (setq is-full t
668 elts nil))
669 (setq elts (cdr-safe elts)))
670 (if is-full
671 "a full keymap"
672 "a sparse keymap")))
673 (t "")))
674 (when (and parens (not (equal string "")))
675 (setq need-close t)
676 (princ "("))
677 (princ string)
678 (with-current-buffer "*Help*"
679 (save-excursion
680 (save-match-data
681 (if (re-search-backward "alias for `\\([^`']+\\)'" nil t)
682 (help-xref-button 1 #'describe-function def
683 "mouse-2, RET: describe this function")))))
684 (or file-name
685 (setq file-name (symbol-file function)))
686 (if file-name
687 (progn
688 (princ " in `")
689 ;; We used to add .el to the file name,
690 ;; but that's completely wrong when the user used load-file.
691 (princ file-name)
692 (princ "'")
693 ;; Make a hyperlink to the library.
694 (with-current-buffer "*Help*"
695 (save-excursion
696 (re-search-backward "`\\([^`']+\\)'" nil t)
697 (help-xref-button 1 #'(lambda (arg)
698 (let ((location
699 (find-function-noselect arg)))
700 (pop-to-buffer (car location))
701 (goto-char (cdr location))))
702 function
703 "mouse-2, RET: find function's definition")))))
704 (if need-close (princ ")"))
705 (princ ".")
706 (terpri)
707 ;; Handle symbols aliased to other symbols.
708 (setq def (indirect-function def))
709 ;; If definition is a macro, find the function inside it.
710 (if (eq (car-safe def) 'macro)
711 (setq def (cdr def)))
712 (let ((arglist (cond ((byte-code-function-p def)
713 (car (append def nil)))
714 ((eq (car-safe def) 'lambda)
715 (nth 1 def))
716 ((and (eq (car-safe def) 'autoload)
717 (not (eq (nth 4 def) 'keymap)))
718 (concat "[Arg list not available until "
719 "function definition is loaded.]"))
720 (t t))))
721 (cond ((listp arglist)
722 (princ (cons (if (symbolp function) function "anonymous")
723 (mapcar (lambda (arg)
724 (if (memq arg '(&optional &rest))
726 (intern (upcase (symbol-name arg)))))
727 arglist)))
728 (terpri))
729 ((stringp arglist)
730 (princ arglist)
731 (terpri))))
732 (let ((doc (documentation function)))
733 (if doc
734 (progn (terpri)
735 (princ doc)
736 (if (subrp (symbol-function function))
737 (with-current-buffer standard-output
738 (beginning-of-line)
739 ;; Builtins get the calling sequence at the end of
740 ;; the doc string. Move it to the same place as
741 ;; for other functions.
743 ;; In cases where `function' has been fset to a
744 ;; subr we can't search for function's name in
745 ;; the doc string. Kluge round that using the
746 ;; printed representation. The arg list then
747 ;; shows the wrong function name, but that
748 ;; might be a useful hint.
749 (let* ((rep (prin1-to-string def))
750 (name (progn
751 (string-match " \\([^ ]+\\)>$" rep)
752 (match-string 1 rep))))
753 (if (looking-at (format "(%s[ )]" (regexp-quote name)))
754 (let ((start (point-marker)))
755 (goto-char (point-min))
756 (forward-paragraph)
757 (insert-buffer-substring (current-buffer) start)
758 (insert ?\n)
759 (delete-region (1- start) (point-max)))
760 (goto-char (point-min))
761 (forward-paragraph)
762 (insert
763 "[Missing arglist. Please make a bug report.]\n")))
764 (goto-char (point-max))))
765 (help-setup-xref (list #'describe-function function)
766 interactive-p))
767 (princ "not documented")))))
769 (defun variable-at-point ()
770 "Return the bound variable symbol found around point.
771 Return 0 if there is no such symbol."
772 (condition-case ()
773 (with-syntax-table emacs-lisp-mode-syntax-table
774 (save-excursion
775 (or (not (zerop (skip-syntax-backward "_w")))
776 (eq (char-syntax (following-char)) ?w)
777 (eq (char-syntax (following-char)) ?_)
778 (forward-sexp -1))
779 (skip-chars-forward "'")
780 (let ((obj (read (current-buffer))))
781 (or (and (symbolp obj) (boundp obj) obj)
782 0))))
783 (error 0)))
785 (defun help-xref-on-pp (from to)
786 "Add xrefs for symbols in `pp's output between FROM and TO."
787 (let ((ost (syntax-table)))
788 (unwind-protect
789 (save-excursion
790 (save-restriction
791 (set-syntax-table emacs-lisp-mode-syntax-table)
792 (narrow-to-region from to)
793 (goto-char (point-min))
794 (while (not (eobp))
795 (cond
796 ((looking-at "\"") (forward-sexp 1))
797 ((looking-at "#<") (search-forward ">" nil 'move))
798 ((looking-at "\\(\\(\\sw\\|\\s_\\)+\\)")
799 (let* ((sym (intern-soft (match-string 1)))
800 (fn (cond ((fboundp sym) #'describe-function)
801 ((or (memq sym '(t nil))
802 (keywordp sym))
803 nil)
804 ((and sym (boundp sym))
805 #'describe-variable))))
806 (when fn (help-xref-button 1 fn sym)))
807 (goto-char (match-end 1)))
808 (t (forward-char 1))))))
809 (set-syntax-table ost))))
811 (defun describe-variable (variable &optional buffer)
812 "Display the full documentation of VARIABLE (a symbol).
813 Returns the documentation as a string, also.
814 If VARIABLE has a buffer-local value in BUFFER (default to the current buffer),
815 it is displayed along with the global value."
816 (interactive
817 (let ((v (variable-at-point))
818 (enable-recursive-minibuffers t)
819 val)
820 (setq val (completing-read (if (symbolp v)
821 (format
822 "Describe variable (default %s): " v)
823 "Describe variable: ")
824 obarray 'boundp t nil nil
825 (if (symbolp v) (symbol-name v))))
826 (list (if (equal val "")
827 v (intern val)))))
828 (unless (bufferp buffer) (setq buffer (current-buffer)))
829 (if (not (symbolp variable))
830 (message "You did not specify a variable")
831 (let (valvoid)
832 (with-current-buffer buffer
833 (with-output-to-temp-buffer "*Help*"
834 (prin1 variable)
835 (if (not (boundp variable))
836 (progn
837 (princ " is void")
838 (setq valvoid t))
839 (let ((val (symbol-value variable)))
840 (with-current-buffer standard-output
841 (princ "'s value is ")
842 (terpri)
843 (let ((from (point)))
844 (pp val)
845 (help-xref-on-pp from (point))))))
846 (terpri)
847 (if (local-variable-p variable)
848 (progn
849 (princ (format "Local in buffer %s; " (buffer-name)))
850 (if (not (default-boundp variable))
851 (princ "globally void")
852 (let ((val (default-value variable)))
853 (with-current-buffer standard-output
854 (princ "global value is ")
855 (terpri)
856 ;; Fixme: pp can take an age if you happen to
857 ;; ask for a very large expression. We should
858 ;; probably print it raw once and check it's a
859 ;; sensible size before prettyprinting. -- fx
860 (let ((from (point)))
861 (pp val)
862 (help-xref-on-pp from (point))))))
863 (terpri)))
864 (terpri)
865 (with-current-buffer standard-output
866 (if (> (count-lines (point-min) (point-max)) 10)
867 (progn
868 ;; Note that setting the syntax table like below
869 ;; makes forward-sexp move over a `'s' at the end
870 ;; of a symbol.
871 (set-syntax-table emacs-lisp-mode-syntax-table)
872 (goto-char (point-min))
873 (if valvoid
874 (forward-line 1)
875 (forward-sexp 1)
876 (delete-region (point) (progn (end-of-line) (point)))
877 (insert " value is shown below.\n\n")
878 (save-excursion
879 (insert "\n\nValue:"))))))
880 (princ "Documentation:")
881 (terpri)
882 (let ((doc (documentation-property variable 'variable-documentation)))
883 (princ (or doc "not documented as a variable.")))
884 (help-setup-xref (list #'describe-variable variable (current-buffer))
885 (interactive-p))
887 ;; Make a link to customize if this variable can be customized.
888 ;; Note, it is not reliable to test only for a custom-type property
889 ;; because those are only present after the var's definition
890 ;; has been loaded.
891 (if (or (get variable 'custom-type) ; after defcustom
892 (get variable 'custom-loads) ; from loaddefs.el
893 (get variable 'standard-value)) ; from cus-start.el
894 (let ((customize-label "customize"))
895 (terpri)
896 (terpri)
897 (princ (concat "You can " customize-label " this variable."))
898 (with-current-buffer "*Help*"
899 (save-excursion
900 (re-search-backward
901 (concat "\\(" customize-label "\\)") nil t)
902 (help-xref-button 1 (lambda (v)
903 (if help-xref-stack
904 (pop help-xref-stack))
905 (customize-variable v))
906 variable
907 "mouse-2, RET: customize variable")))))
908 ;; Make a hyperlink to the library if appropriate. (Don't
909 ;; change the format of the buffer's initial line in case
910 ;; anything expects the current format.)
911 (let ((file-name (symbol-file variable)))
912 (when file-name
913 (princ "\n\nDefined in `")
914 (princ file-name)
915 (princ "'.")
916 (with-current-buffer "*Help*"
917 (save-excursion
918 (re-search-backward "`\\([^`']+\\)'" nil t)
919 (help-xref-button
920 1 (lambda (arg)
921 (let ((location
922 (find-variable-noselect arg)))
923 (pop-to-buffer (car location))
924 (goto-char (cdr location))))
925 variable "mouse-2, RET: find variable's definition")))))
927 (print-help-return-message)
928 (save-excursion
929 (set-buffer standard-output)
930 ;; Return the text we displayed.
931 (buffer-string)))))))
933 (defun describe-bindings (&optional prefix buffer)
934 "Show a list of all defined keys, and their definitions.
935 We put that list in a buffer, and display the buffer.
937 The optional argument PREFIX, if non-nil, should be a key sequence;
938 then we display only bindings that start with that prefix.
939 The optional argument BUFFER specifies which buffer's bindings
940 to display (default, the current buffer)."
941 (interactive "P")
942 (or buffer (setq buffer (current-buffer)))
943 (with-current-buffer buffer
944 (describe-bindings-internal nil prefix))
945 (with-current-buffer "*Help*"
946 (help-setup-xref (list #'describe-bindings prefix buffer)
947 (interactive-p))))
949 (defun where-is (definition &optional insert)
950 "Print message listing key sequences that invoke the command DEFINITION.
951 Argument is a command definition, usually a symbol with a function definition.
952 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
953 (interactive
954 (let ((fn (function-called-at-point))
955 (enable-recursive-minibuffers t)
956 val)
957 (setq val (completing-read (if fn
958 (format "Where is command (default %s): " fn)
959 "Where is command: ")
960 obarray 'commandp t))
961 (list (if (equal val "")
962 fn (intern val))
963 current-prefix-arg)))
964 (let* ((keys (where-is-internal definition overriding-local-map nil nil))
965 (keys1 (mapconcat 'key-description keys ", "))
966 (standard-output (if insert (current-buffer) t)))
967 (if insert
968 (if (> (length keys1) 0)
969 (princ (format "%s (%s)" keys1 definition))
970 (princ (format "M-x %s RET" definition)))
971 (if (> (length keys1) 0)
972 (princ (format "%s is on %s" definition keys1))
973 (princ (format "%s is not on any key" definition)))))
974 nil)
976 (defun locate-library (library &optional nosuffix path interactive-call)
977 "Show the precise file name of Emacs library LIBRARY.
978 This command searches the directories in `load-path' like `M-x load-library'
979 to find the file that `M-x load-library RET LIBRARY RET' would load.
980 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
981 to the specified name LIBRARY.
983 If the optional third arg PATH is specified, that list of directories
984 is used instead of `load-path'.
986 When called from a program, the file name is normaly returned as a
987 string. When run interactively, the argument INTERACTIVE-CALL is t,
988 and the file name is displayed in the echo area."
989 (interactive (list (read-string "Locate library: ")
990 nil nil
992 (let (result)
993 (catch 'answer
994 (mapc
995 (lambda (dir)
996 (mapc
997 (lambda (suf)
998 (let ((try (expand-file-name (concat library suf) dir)))
999 (and (file-readable-p try)
1000 (null (file-directory-p try))
1001 (progn
1002 (setq result try)
1003 (throw 'answer try)))))
1004 (if nosuffix
1005 '("")
1006 '(".elc" ".el" "")
1007 ;;; load doesn't handle this yet.
1008 ;;; (let ((basic '(".elc" ".el" ""))
1009 ;;; (compressed '(".Z" ".gz" "")))
1010 ;;; ;; If autocompression mode is on,
1011 ;;; ;; consider all combinations of library suffixes
1012 ;;; ;; and compression suffixes.
1013 ;;; (if (rassq 'jka-compr-handler file-name-handler-alist)
1014 ;;; (apply 'nconc
1015 ;;; (mapcar (lambda (compelt)
1016 ;;; (mapcar (lambda (baselt)
1017 ;;; (concat baselt compelt))
1018 ;;; basic))
1019 ;;; compressed))
1020 ;;; basic))
1022 (or path load-path)))
1023 (and interactive-call
1024 (if result
1025 (message "Library is file %s" result)
1026 (message "No library %s in search path" library)))
1027 result))
1030 ;;; Grokking cross-reference information in doc strings and
1031 ;;; hyperlinking it.
1033 ;; This may have some scope for extension and the same or something
1034 ;; similar should be done for widget doc strings, which currently use
1035 ;; another mechanism.
1037 (defcustom help-highlight-p t
1038 "*If non-nil, `help-make-xrefs' highlight cross-references.
1039 Under a window system it highlights them with face defined by
1040 `help-highlight-face'."
1041 :group 'help
1042 :version "20.3"
1043 :type 'boolean)
1045 (defcustom help-highlight-face 'underline
1046 "Face used by `help-make-xrefs' to highlight cross-references.
1047 Must be previously-defined."
1048 :group 'help
1049 :version "20.3"
1050 :type 'face)
1052 (defvar help-back-label (purecopy "[back]")
1053 "Label to use by `help-make-xrefs' for the go-back reference.")
1055 (defconst help-xref-symbol-regexp
1056 (purecopy (concat "\\(\\<\\(\\(variable\\|option\\)\\|"
1057 "\\(function\\|command\\)\\|"
1058 "\\(face\\)\\|"
1059 "\\(symbol\\)\\)\\s-+\\)?"
1060 ;; Note starting with word-syntax character:
1061 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'"))
1062 "Regexp matching doc string references to symbols.
1064 The words preceding the quoted symbol can be used in doc strings to
1065 distinguish references to variables, functions and symbols.")
1067 (defconst help-xref-mule-regexp nil
1068 "Regexp matching doc string references to MULE-related keywords.
1070 It is usually nil, and is temporarily bound to an appropriate regexp
1071 when help commands related to multilingual environment (e.g.,
1072 `describe-coding-system') are invoked.")
1075 (defconst help-xref-info-regexp
1076 (purecopy "\\<[Ii]nfo[ \t\n]+node[ \t\n]+`\\([^']+\\)'")
1077 "Regexp matching doc string references to an Info node.")
1079 (defun help-setup-xref (item interactive-p)
1080 "Invoked from commands using the \"*Help*\" buffer to install some xref info.
1082 ITEM is a (FUNCTION . ARGS) pair appropriate for recreating the help
1083 buffer after following a reference. INTERACTIVE-P is non-nil if the
1084 calling command was invoked interactively. In this case the stack of
1085 items for help buffer \"back\" buttons is cleared."
1086 (if interactive-p
1087 (setq help-xref-stack nil))
1088 (setq help-xref-stack-item item))
1090 (defvar help-xref-following nil
1091 "Non-nil when following a help cross-reference.")
1093 (defun help-make-xrefs (&optional buffer)
1094 "Parse and hyperlink documentation cross-references in the given BUFFER.
1096 Find cross-reference information in a buffer and, if
1097 `help-highlight-p' is non-nil, highlight it with face defined by
1098 `help-highlight-face'; activate such cross references for selection
1099 with `help-follow'. Cross-references have the canonical form `...'
1100 and the type of reference may be disambiguated by the preceding
1101 word(s) used in `help-xref-symbol-regexp'.
1103 If the variable `help-xref-mule-regexp' is non-nil, find also
1104 cross-reference information related to multilingual environment
1105 \(e.g., coding-systems). This variable is also used to disambiguate
1106 the type of reference as the same way as `help-xref-symbol-regexp'.
1108 A special reference `back' is made to return back through a stack of
1109 help buffers. Variable `help-back-label' specifies the text for
1110 that."
1111 (interactive "b")
1112 (save-excursion
1113 (set-buffer (or buffer (current-buffer)))
1114 (goto-char (point-min))
1115 ;; Skip the header-type info, though it might be useful to parse
1116 ;; it at some stage (e.g. "function in `library'").
1117 (forward-paragraph)
1118 (let ((old-modified (buffer-modified-p)))
1119 (let ((stab (syntax-table))
1120 (case-fold-search t)
1121 (inhibit-read-only t))
1122 (set-syntax-table emacs-lisp-mode-syntax-table)
1123 ;; The following should probably be abstracted out.
1124 (unwind-protect
1125 (progn
1126 ;; Info references
1127 (save-excursion
1128 (while (re-search-forward help-xref-info-regexp nil t)
1129 (let ((data (match-string 1)))
1130 (save-match-data
1131 (unless (string-match "^([^)]+)" data)
1132 (setq data (concat "(emacs)" data))))
1133 (help-xref-button 1 #'info data
1134 "mouse-2, RET: read this Info node"))))
1135 ;; Mule related keywords. Do this before trying
1136 ;; `help-xref-symbol-regexp' because some of Mule
1137 ;; keywords have variable or function definitions.
1138 (if help-xref-mule-regexp
1139 (save-excursion
1140 (while (re-search-forward help-xref-mule-regexp nil t)
1141 (let* ((data (match-string 7))
1142 (sym (intern-soft data)))
1143 (cond
1144 ((match-string 3) ; coding system
1145 (and sym (coding-system-p sym)
1146 (help-xref-button
1147 7 #'describe-coding-system sym
1148 "mouse-2, RET: describe this coding system")))
1149 ((match-string 4) ; input method
1150 (and (assoc data input-method-alist)
1151 (help-xref-button
1152 7 #'describe-input-method data
1153 "mouse-2, RET: describe this input method")))
1154 ((or (match-string 5) (match-string 6)) ; charset
1155 (and sym (charsetp sym)
1156 (help-xref-button
1157 7 #'describe-character-set sym
1158 "mouse-2, RET: describe this character set")))
1159 ((assoc data input-method-alist)
1160 (help-xref-button
1161 7 #'describe-input-method data
1162 "mouse-2, RET: describe this input method"))
1163 ((and sym (coding-system-p sym))
1164 (help-xref-button
1165 7 #'describe-coding-system sym
1166 "mouse-2, RET: describe this coding system"))
1167 ((and sym (charsetp sym))
1168 (help-xref-button
1169 7 #'describe-character-set sym
1170 "mouse-2, RET: describe this character set")))))))
1171 ;; Quoted symbols
1172 (save-excursion
1173 (while (re-search-forward help-xref-symbol-regexp nil t)
1174 (let* ((data (match-string 7))
1175 (sym (intern-soft data)))
1176 (if sym
1177 (cond
1178 ((match-string 3) ; `variable' &c
1179 (and (boundp sym) ; `variable' doesn't ensure
1180 ; it's actually bound
1181 (help-xref-button
1182 7 #'describe-variable sym
1183 "mouse-2, RET: describe this variable")))
1184 ((match-string 4) ; `function' &c
1185 (and (fboundp sym) ; similarly
1186 (help-xref-button
1187 7 #'describe-function sym
1188 "mouse-2, RET: describe this function")))
1189 ((match-string 5) ; `face'
1190 (and (facep sym)
1191 (help-xref-button 7 #'describe-face sym
1192 "mouse-2, RET: describe this face")))
1193 ((match-string 6)) ; nothing for symbol
1194 ((and (boundp sym) (fboundp sym))
1195 ;; We can't intuit whether to use the
1196 ;; variable or function doc -- supply both.
1197 (help-xref-button
1198 7 #'help-xref-interned sym
1199 "mouse-2, RET: describe this symbol"))
1200 ((boundp sym)
1201 (help-xref-button
1202 7 #'describe-variable sym
1203 "mouse-2, RET: describe this variable"))
1204 ((fboundp sym)
1205 (help-xref-button
1206 7 #'describe-function sym
1207 "mouse-2, RET: describe this function"))
1208 ((facep sym)
1209 (help-xref-button
1210 7 #'describe-face sym)))))))
1211 ;; An obvious case of a key substitution:
1212 (save-excursion
1213 (while (re-search-forward
1214 ;; Assume command name is only word characters
1215 ;; and dashes to get things like `use M-x foo.'.
1216 "\\<M-x\\s-+\\(\\sw\\(\\sw\\|-\\)+\\)" nil t)
1217 (let ((sym (intern-soft (match-string 1))))
1218 (if (fboundp sym)
1219 (help-xref-button
1220 1 #'describe-function sym
1221 "mouse-2, RET: describe this command")))))
1222 ;; Look for commands in whole keymap substitutions:
1223 (save-excursion
1224 ;; Make sure to find the first keymap.
1225 (goto-char (point-min))
1226 ;; Find a header and the column at which the command
1227 ;; name will be found.
1228 (while (re-search-forward "^key +binding\n\\(-+ +\\)-+\n\n"
1229 nil t)
1230 (let ((col (- (match-end 1) (match-beginning 1))))
1231 (while
1232 ;; Ignore single blank lines in table, but not
1233 ;; double ones, which should terminate it.
1234 (and (not (looking-at "\n\\s-*\n"))
1235 (progn
1236 (and (eolp) (forward-line))
1237 (end-of-line)
1238 (skip-chars-backward "^\t\n")
1239 (if (and (>= (current-column) col)
1240 (looking-at "\\(\\sw\\|-\\)+$"))
1241 (let ((sym (intern-soft (match-string 0))))
1242 (if (fboundp sym)
1243 (help-xref-button
1244 0 #'describe-function sym
1245 "mouse-2, RET: describe this function"))))
1246 (zerop (forward-line)))))))))
1247 (set-syntax-table stab))
1248 ;; Delete extraneous newlines at the end of the docstring
1249 (goto-char (point-max))
1250 (while (and (not (bobp)) (bolp))
1251 (delete-char -1))
1252 ;; Make a back-reference in this buffer if appropriate.
1253 (when (and help-xref-following help-xref-stack)
1254 (save-excursion
1255 (insert "\n\n" help-back-label))
1256 ;; Just to provide the match data:
1257 (looking-at (concat "\n\n\\(" (regexp-quote help-back-label) "\\)"))
1258 (help-xref-button 1 #'help-xref-go-back (current-buffer))))
1259 ;; View mode steals RET from us.
1260 (set (make-local-variable 'minor-mode-overriding-map-alist)
1261 (list (cons 'view-mode
1262 (let ((map (make-sparse-keymap)))
1263 (set-keymap-parent map view-mode-map)
1264 (define-key map "\r" 'help-follow)
1265 map))))
1266 (set-buffer-modified-p old-modified))))
1268 (defun help-xref-button (match-number function data &optional help-echo)
1269 "Make a hyperlink for cross-reference text previously matched.
1271 MATCH-NUMBER is the subexpression of interest in the last matched
1272 regexp. FUNCTION is a function to invoke when the button is
1273 activated, applied to DATA. DATA may be a single value or a list.
1274 See `help-make-xrefs'.
1275 If optional arg HELP-ECHO is supplied, it is used as a help string."
1276 ;; Don't mung properties we've added specially in some instances.
1277 (unless (get-text-property (match-beginning match-number) 'help-xref)
1278 (add-text-properties (match-beginning match-number)
1279 (match-end match-number)
1280 (list 'mouse-face 'highlight
1281 'help-xref (cons function
1282 (if (listp data)
1283 data
1284 (list data)))))
1285 (if help-echo
1286 (put-text-property (match-beginning match-number)
1287 (match-end match-number)
1288 'help-echo help-echo))
1289 (if help-highlight-p
1290 (put-text-property (match-beginning match-number)
1291 (match-end match-number)
1292 'face help-highlight-face))))
1294 (defun help-insert-xref-button (string function data &optional help-echo)
1295 "Insert STRING and make a hyperlink from cross-reference text on it.
1297 FUNCTION is a function to invoke when the button is activated, applied
1298 to DATA. DATA may be a single value or a list. See `help-make-xrefs'.
1299 If optional arg HELP-ECHO is supplied, it is used as a help string."
1300 (let ((pos (point)))
1301 (insert string)
1302 (goto-char pos)
1303 (search-forward string)
1304 (help-xref-button 0 function data help-echo)))
1308 ;; Additional functions for (re-)creating types of help buffers.
1309 (defun help-xref-interned (symbol)
1310 "Follow a hyperlink which appeared to be an arbitrary interned SYMBOL.
1312 Both variable and function documentation are extracted into a single
1313 help buffer."
1314 (let ((fdoc (when (fboundp symbol) (describe-function symbol)))
1315 (facedoc (when (facep symbol) (describe-face symbol))))
1316 (when (or (boundp symbol) (not fdoc))
1317 (describe-variable symbol)
1318 ;; We now have a help buffer on the variable. Insert the function
1319 ;; text before it.
1320 (when (or fdoc facedoc)
1321 (with-current-buffer "*Help*"
1322 (goto-char (point-min))
1323 (let ((inhibit-read-only t))
1324 (when fdoc
1325 (insert fdoc "\n\n"))
1326 (when facedoc
1327 (insert (make-string 30 ?-) "\n\n" (symbol-name symbol)
1328 " is also a " "face." "\n\n" facedoc "\n\n"))
1329 (insert (make-string 30 ?-) "\n\n" (symbol-name symbol)
1330 " is also a " "variable." "\n\n"))
1331 (help-setup-xref (list #'help-xref-interned symbol) nil))))))
1333 (defun help-xref-mode (buffer)
1334 "Do a `describe-mode' for the specified BUFFER."
1335 (save-excursion
1336 (set-buffer buffer)
1337 (describe-mode)))
1339 ;;; Navigation/hyperlinking with xrefs
1341 (defun help-follow-mouse (click)
1342 "Follow the cross-reference that you click on."
1343 (interactive "e")
1344 (let* ((start (event-start click))
1345 (window (car start))
1346 (pos (car (cdr start))))
1347 (with-current-buffer (window-buffer window)
1348 (help-follow pos))))
1350 (defun help-xref-go-back (buffer)
1351 "From BUFFER, go back to previous help buffer text using `help-xref-stack'."
1352 (let (item position method args)
1353 (with-current-buffer buffer
1354 (when help-xref-stack
1355 (setq help-xref-stack (cdr help-xref-stack)) ; due to help-follow
1356 (setq item (pop help-xref-stack)
1357 position (car item)
1358 method (cadr item)
1359 args (cddr item))))
1360 (apply method args)
1361 ;; We assume that the buffer we just recreated has the saved name,
1362 ;; which might not always be true.
1363 (when (get-buffer (cdr position))
1364 (with-current-buffer (cdr position)
1365 (goto-char (car position))))))
1367 (defun help-go-back ()
1368 "Invoke the [back] button (if any) in the Help mode buffer."
1369 (interactive)
1370 (help-follow (1- (point-max))))
1372 (defun help-follow (&optional pos)
1373 "Follow cross-reference at POS, defaulting to point.
1375 For the cross-reference format, see `help-make-xrefs'."
1376 (interactive "d")
1377 (unless pos
1378 (setq pos (point)))
1379 (let* ((help-data
1380 (or (and (not (= pos (point-max)))
1381 (get-text-property pos 'help-xref))
1382 (and (not (= pos (point-min)))
1383 (get-text-property (1- pos) 'help-xref))
1384 ;; check if the symbol under point is a function or variable
1385 (let ((sym
1386 (intern
1387 (save-excursion
1388 (goto-char pos) (skip-syntax-backward "w_")
1389 (buffer-substring (point)
1390 (progn (skip-syntax-forward "w_")
1391 (point)))))))
1392 (when (or (boundp sym) (fboundp sym))
1393 (list #'help-xref-interned sym)))))
1394 (method (car help-data))
1395 (args (cdr help-data)))
1396 (when help-data
1397 (setq help-xref-stack (cons (cons (cons pos (buffer-name))
1398 help-xref-stack-item)
1399 help-xref-stack))
1400 (setq help-xref-stack-item nil)
1401 ;; There is a reference at point. Follow it.
1402 (let ((help-xref-following t))
1403 (apply method args)))))
1405 ;; For tabbing through buffer.
1406 (defun help-next-ref ()
1407 "Find the next help cross-reference in the buffer."
1408 (interactive)
1409 (let (pos)
1410 (while (not pos)
1411 (if (get-text-property (point) 'help-xref) ; move off reference
1412 (goto-char (or (next-single-property-change (point) 'help-xref)
1413 (point))))
1414 (cond ((setq pos (next-single-property-change (point) 'help-xref))
1415 (if pos (goto-char pos)))
1416 ((bobp)
1417 (message "No cross references in the buffer.")
1418 (setq pos t))
1419 (t ; be circular
1420 (goto-char (point-min)))))))
1422 (defun help-previous-ref ()
1423 "Find the previous help cross-reference in the buffer."
1424 (interactive)
1425 (let (pos)
1426 (while (not pos)
1427 (if (get-text-property (point) 'help-xref) ; move off reference
1428 (goto-char (or (previous-single-property-change (point) 'help-xref)
1429 (point))))
1430 (cond ((setq pos (previous-single-property-change (point) 'help-xref))
1431 (if pos (goto-char pos)))
1432 ((bobp)
1433 (message "No cross references in the buffer.")
1434 (setq pos t))
1435 (t ; be circular
1436 (goto-char (point-max)))))))
1439 ;;; Automatic resizing of temporary buffers.
1441 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
1442 "*Maximum height of a window displaying a temporary buffer.
1443 This is the maximum height (in text lines) which `resize-temp-buffer-window'
1444 will give to a window displaying a temporary buffer.
1445 It can also be a function which will be called with the object corresponding
1446 to the buffer to be displayed as argument and should return an integer
1447 positive number."
1448 :type '(choice integer function)
1449 :group 'help
1450 :version "20.4")
1452 (define-minor-mode temp-buffer-resize-mode
1453 "Toggle the mode which makes windows smaller for temporary buffers.
1454 With prefix argument ARG, turn the resizing of windows displaying temporary
1455 buffers on if ARG is positive or off otherwise.
1456 This makes the window the right height for its contents, but never
1457 more than `temp-buffer-max-height' nor less than `window-min-height'.
1458 This applies to `help', `apropos' and `completion' buffers, and some others."
1459 nil nil nil :global t :group 'help
1460 (if temp-buffer-resize-mode
1461 ;; `help-mode-maybe' may add a `back' button and thus increase the
1462 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
1463 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
1464 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
1466 (defun resize-temp-buffer-window ()
1467 "Resize the current window to fit its contents.
1468 Will not make it higher than `temp-buffer-max-height' nor smaller than
1469 `window-min-height'. Do nothing if it is the only window on its frame, if it
1470 is not as wide as the frame or if some of the window's contents are scrolled
1471 out of view."
1472 (unless (or (one-window-p 'nomini)
1473 (not (pos-visible-in-window-p (point-min)))
1474 (/= (frame-width) (window-width)))
1475 (fit-window-to-buffer
1476 (selected-window)
1477 (if (functionp temp-buffer-max-height)
1478 (funcall temp-buffer-max-height (current-buffer))
1479 temp-buffer-max-height))))
1481 ;; `help-manyarg-func-alist' is defined primitively (in doc.c).
1482 ;; New primitives with `MANY' or `UNEVALLED' arglists should be added
1483 ;; to this alist.
1484 ;; The parens and function name are redundant, but it's messy to add
1485 ;; them in `documentation'.
1487 ;; This will find any missing items:
1488 ;; (let (l)
1489 ;; (mapatoms (lambda (x)
1490 ;; (if (and (fboundp x)
1491 ;; (subrp (symbol-function x))
1492 ;; (not (numberp (cdr (subr-arity (symbol-function x)))))
1493 ;; (not (assq x help-manyarg-func-alist)))
1494 ;; (push x l))))
1495 ;; l)
1496 (defconst help-manyarg-func-alist
1497 (purecopy
1498 '((list . "(list &rest OBJECTS)")
1499 (vector . "(vector &rest OBJECTS)")
1500 (make-byte-code . "(make-byte-code &rest ELEMENTS)")
1501 (call-process
1502 . "(call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)")
1503 (call-process-region
1504 . "(call-process-region START END PROGRAM &optional DELETE BUFFER DISPLAY &rest ARGS)")
1505 (string . "(string &rest CHARACTERS)")
1506 (+ . "(+ &rest NUMBERS-OR-MARKERS)")
1507 (- . "(- &optional NUMBER-OR-MARKER &rest MORE-NUMBERS-OR-MARKERS)")
1508 (* . "(* &rest NUMBERS-OR-MARKERS)")
1509 (/ . "(/ DIVIDEND DIVISOR &rest DIVISORS)")
1510 (max . "(max NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS)")
1511 (min . "(min NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS)")
1512 (logand . "(logand &rest INTS-OR-MARKERS)")
1513 (logior . "(logior &rest INTS-OR-MARKERS)")
1514 (logxor . "(logxor &rest INTS-OR-MARKERS)")
1515 (encode-time
1516 . "(encode-time SECOND MINUTE HOUR DAY MONTH YEAR &optional ZONE)")
1517 (insert . "(insert &rest ARGS)")
1518 (insert-and-inherit . "(insert-and-inherit &rest ARGS)")
1519 (insert-before-markers . "(insert-before-markers &rest ARGS)")
1520 (message . "(message STRING &rest ARGUMENTS)")
1521 (message-box . "(message-box STRING &rest ARGUMENTS)")
1522 (message-or-box . "(message-or-box STRING &rest ARGUMENTS)")
1523 (propertize . "(propertize STRING &rest PROPERTIES)")
1524 (format . "(format STRING &rest OBJECTS)")
1525 (apply . "(apply FUNCTION &rest ARGUMENTS)")
1526 (run-hooks . "(run-hooks &rest HOOKS)")
1527 (run-hook-with-args . "(run-hook-with-args HOOK &rest ARGS)")
1528 (run-hook-with-args-until-failure
1529 . "(run-hook-with-args-until-failure HOOK &rest ARGS)")
1530 (run-hook-with-args-until-success
1531 . "(run-hook-with-args-until-success HOOK &rest ARGS)")
1532 (funcall . "(funcall FUNCTION &rest ARGUMENTS)")
1533 (append . "(append &rest SEQUENCES)")
1534 (concat . "(concat &rest SEQUENCES)")
1535 (vconcat . "(vconcat &rest SEQUENCES)")
1536 (nconc . "(nconc &rest LISTS)")
1537 (widget-apply . "(widget-apply WIDGET PROPERTY &rest ARGS)")
1538 (make-hash-table . "(make-hash-table &rest KEYWORD-ARGS)")
1539 (insert-string . "(insert-string &rest ARGS)")
1540 (start-process . "(start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS)")
1541 (setq-default . "(setq-default SYMBOL VALUE [SYMBOL VALUE...])")
1542 (save-excursion . "(save-excursion &rest BODY)")
1543 (save-current-buffer . "(save-current-buffer &rest BODY)")
1544 (save-restriction . "(save-restriction &rest BODY)")
1545 (or . "(or CONDITIONS ...)")
1546 (and . "(and CONDITIONS ...)")
1547 (if . "(if COND THEN ELSE...)")
1548 (cond . "(cond CLAUSES...)")
1549 (progn . "(progn BODY ...)")
1550 (prog1 . "(prog1 FIRST BODY...)")
1551 (prog2 . "(prog2 X Y BODY...)")
1552 (setq . "(setq SYM VAL SYM VAL ...)")
1553 (quote . "(quote ARG)")
1554 (function . "(function ARG)")
1555 (defun . "(defun NAME ARGLIST [DOCSTRING] BODY...)")
1556 (defmacro . "(defmacro NAME ARGLIST [DOCSTRING] BODY...)")
1557 (defvar . "(defvar SYMBOL [INITVALUE DOCSTRING])")
1558 (defconst . "(defconst SYMBOL INITVALUE [DOCSTRING])")
1559 (let* . "(let* VARLIST BODY...)")
1560 (let . "(let VARLIST BODY...)")
1561 (while . "(while TEST BODY...)")
1562 (catch . "(catch TAG BODY...)")
1563 (unwind-protect . "(unwind-protect BODYFORM UNWINDFORMS...)")
1564 (condition-case . "(condition-case VAR BODYFORM HANDLERS...)")
1565 (track-mouse . "(track-mouse BODY ...)")
1566 (ml-if . "(ml-if COND THEN ELSE...)")
1567 (ml-provide-prefix-argument . "(ml-provide-prefix-argument ARG1 ARG2)")
1568 (ml-prefix-argument-loop . "(ml-prefix-argument-loop ...)")
1569 (with-output-to-temp-buffer
1570 . "(with-output-to-temp-buffer BUFFNAME BODY ...)")
1571 (save-window-excursion . "(save-window-excursion BODY ...)")
1572 (find-operation-coding-system
1573 . "(find-operation-coding-system OPERATION ARGUMENTS ...)")
1574 (insert-before-markers-and-inherit
1575 . "(insert-before-markers-and-inherit &rest ARGS)"))))
1577 ;; Provide this for the sake of define-minor-mode which generates
1578 ;; defcustoms which require 'help'.
1580 (provide 'help)
1582 ;;; help.el ends here