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