1 ;;; help.el --- help commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999 Free Software Foundation, Inc.
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)
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.
27 ;; This code implements GNU Emacs' on-line help system, the one invoked by
28 ;; `M-x help-for-help'.
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 ;; Font-locking is incompatible with the new xref stuff.
108 ;(defvar help-font-lock-keywords
110 ; (let ((name-char "[-+a-zA-Z0-9_*]") (sym-char "[-+a-zA-Z0-9_:*]"))
113 ; ;; The symbol itself.
114 ; (list (concat "\\`\\(" name-char "+\\)\\(\\(:\\)\\|\\('\\)\\)")
115 ; '(1 (if (match-beginning 3)
116 ; font-lock-function-name-face
117 ; font-lock-variable-name-face)))
119 ; ;; Words inside `' which tend to be symbol names.
120 ; (list (concat "`\\(" sym-char sym-char "+\\)'")
121 ; 1 'font-lock-constant-face t)
123 ; ;; CLisp `:' keywords as references.
124 ; (list (concat "\\<:" sym-char "+\\>") 0 'font-lock-builtin-face t))))
125 ; "Default expressions to highlight in Help mode.")
127 (defvar help-xref-stack nil
128 "A stack of ways by which to return to help buffers after following xrefs.
129 Used by `help-follow' and `help-xref-go-back'.
130 An element looks like (POSITION FUNCTION ARGS...).
131 To use the element, do (apply FUNCTION ARGS) then (goto-char POSITION).")
132 (put 'help-xref-stack 'permanent-local t)
134 (defvar help-xref-stack-item nil
135 "An item for `help-follow' in this buffer to push onto `help-xref-stack'.
136 The format is (FUNCTION ARGS...).")
137 (put 'help-xref-stack-item 'permanent-local t)
139 (setq-default help-xref-stack nil help-xref-stack-item nil)
141 (defcustom help-mode-hook nil
142 "Hook run by `help-mode'."
147 "Major mode for viewing help text and navigating references in it.
148 Entry to this mode runs the normal hook `help-mode-hook'.
152 (kill-all-local-variables)
153 (use-local-map help-mode-map)
154 (setq mode-name "Help")
155 (setq major-mode 'help-mode)
156 (make-local-variable 'font-lock-defaults)
157 (setq font-lock-defaults nil) ; font-lock would defeat xref
159 (make-local-variable 'view-no-disable-on-exit)
160 (setq view-no-disable-on-exit t)
161 ;; `help-make-xrefs' would be run here if not invoked from
162 ;; `help-mode-maybe'.
163 (run-hooks 'help-mode-hook))
165 (defun help-mode-setup ()
167 (setq buffer-read-only nil))
169 (add-hook 'temp-buffer-setup-hook 'help-mode-setup)
171 (defun help-mode-finish ()
172 (when (eq major-mode 'help-mode)
173 ;; View mode's read-only status of existing *Help* buffer is lost
174 ;; by with-output-to-temp-buffer.
176 (help-make-xrefs (current-buffer)))
177 (setq view-return-to-alist
178 (list (cons (selected-window) help-return-method))))
180 (add-hook 'temp-buffer-show-hook 'help-mode-finish)
183 "Just exit from the Help command's command loop."
187 (defun help-with-tutorial (&optional arg)
188 "Select the Emacs learn-by-doing tutorial.
189 If there is a tutorial version written in the language
190 of the selected language environment, that version is used.
191 If there's no tutorial in that language, `TUTORIAL' is selected.
192 With arg, you are asked to choose which language."
195 (read-language-name 'tutorial "Language: " "English")
196 (if (get-language-info current-language-environment 'tutorial)
197 current-language-environment
200 (setq filename (get-language-info lang 'tutorial))
201 (setq file (expand-file-name (concat "~/" filename)))
202 (delete-other-windows)
203 (if (get-file-buffer file)
204 (switch-to-buffer (get-file-buffer file))
205 (switch-to-buffer (create-file-buffer file))
206 (setq buffer-file-name file)
207 (setq default-directory (expand-file-name "~/"))
208 (setq buffer-auto-save-file-name nil)
209 (insert-file-contents (expand-file-name filename data-directory))
210 (goto-char (point-min))
211 (search-forward "\n<<")
213 (delete-region (point) (progn (end-of-line) (point)))
214 (let ((n (- (window-height (selected-window))
215 (count-lines (point-min) (point))
219 ;; Some people get confused by the large gap.
221 (insert "[Middle of page left blank for didactic purposes. "
222 "Text continues below]")
223 (newline (- n (/ n 2)))))
224 (goto-char (point-min))
225 (set-buffer-modified-p nil))))
227 (defun mode-line-key-binding (key)
228 "Value is the binding of KEY in the mode line or nil if none."
229 (let (string-info defn)
230 (when (and (eq 'mode-line (aref key 0))
231 (consp (setq string-info (nth 4 (event-start (aref key 1))))))
232 (let* ((string (car string-info))
233 (pos (cdr string-info))
234 (local-map (and (> pos 0)
235 (< pos (length string))
236 (get-text-property pos 'local-map string))))
237 (setq defn (and local-map (lookup-key local-map key)))))
240 (defun describe-key-briefly (key &optional insert)
241 "Print the name of the function KEY invokes. KEY is a string.
242 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
243 (interactive "kDescribe key briefly: \nP")
245 (let ((modifiers (event-modifiers (aref key 0)))
246 (standard-output (if insert (current-buffer) t))
248 ;; For a mouse button event, go to the button it applies to
249 ;; to get the right key bindings. And go to the right place
250 ;; in case the keymap depends on where you clicked.
251 (if (or (memq 'click modifiers) (memq 'down modifiers)
252 (memq 'drag modifiers))
253 (setq window (posn-window (event-start (aref key 0)))
254 position (posn-point (event-start (aref key 0)))))
257 (set-buffer (window-buffer window))
258 (goto-char position)))
259 ;; Ok, now look up the key and name the command.
260 (let ((defn (or (mode-line-key-binding key)
262 (key-desc (key-description key)))
263 (if (or (null defn) (integerp defn))
264 (princ (format "%s is undefined" key-desc))
265 (princ (format (if insert
268 "%s at that spot runs the command %s"
269 "%s runs the command %s"))
271 (if (symbolp defn) defn (prin1-to-string defn)))))))))
273 (defvar help-return-method nil
274 "What to do to \"exit\" the help buffer.
276 (WINDOW . t) delete the selected window, go to WINDOW.
277 (WINDOW . quit-window) do quit-window, then select WINDOW.
278 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
280 (defun print-help-return-message (&optional function)
281 "Display or return message saying how to restore windows after help command.
282 Computes a message and applies the optional argument FUNCTION to it.
283 If FUNCTION is nil, applies `message' to it, thus printing it."
284 (and (not (get-buffer-window standard-output))
286 (cond ((special-display-p (buffer-name standard-output))
287 (setq help-return-method (cons (selected-window) t))
288 ;; If the help output buffer is a special display buffer,
289 ;; don't say anything about how to get rid of it.
290 ;; First of all, the user will do that with the window
291 ;; manager, not with Emacs.
292 ;; Secondly, the buffer has not been displayed yet,
293 ;; so we don't know whether its frame will be selected.
295 ((not (one-window-p t))
296 (setq help-return-method
297 (cons (selected-window) 'quit-window))
298 "Type \\[switch-to-buffer-other-window] RET to restore the other window.")
300 (setq help-return-method (cons (selected-window) t))
301 "Type \\[delete-other-windows] to remove help window.")
303 (setq help-return-method
304 (list (selected-window) (window-buffer)
305 (window-start) (window-point)))
306 "Type \\[switch-to-buffer] RET to remove help window."))))
307 (funcall (or function 'message)
310 (substitute-command-keys first-message)
312 (if first-message " " "")
313 ;; If the help buffer will go in a separate frame,
314 ;; it's no use mentioning a command to scroll, so don't.
315 (if (special-display-p (buffer-name standard-output))
317 (if (same-window-p (buffer-name standard-output))
318 ;; Say how to scroll this window.
319 (substitute-command-keys
320 "\\[scroll-up] to scroll the help.")
321 ;; Say how to scroll some other window.
322 (substitute-command-keys
323 "\\[scroll-other-window] to scroll the help."))))))))
325 (defun describe-key (key)
326 "Display documentation of the function invoked by KEY. KEY is a string."
327 (interactive "kDescribe key: ")
329 (let ((modifiers (event-modifiers (aref key 0)))
331 ;; For a mouse button event, go to the button it applies to
332 ;; to get the right key bindings. And go to the right place
333 ;; in case the keymap depends on where you clicked.
334 (if (or (memq 'click modifiers) (memq 'down modifiers)
335 (memq 'drag modifiers))
336 (setq window (posn-window (event-start (aref key 0)))
337 position (posn-point (event-start (aref key 0)))))
340 (set-buffer (window-buffer window))
341 (goto-char position)))
342 (let ((defn (or (mode-line-key-binding key) (key-binding key))))
343 (if (or (null defn) (integerp defn))
344 (message "%s is undefined" (key-description key))
345 (with-output-to-temp-buffer "*Help*"
346 (princ (key-description key))
348 (princ " at that spot"))
349 (princ " runs the command ")
351 (princ "\n which is ")
352 (describe-function-1 defn nil (interactive-p))
353 (print-help-return-message)))))))
355 (defun describe-mode ()
356 "Display documentation of current major mode and minor modes.
357 The major mode description comes first, followed by the minor modes,
358 each on a separate page.
360 For this to work correctly for a minor mode, the mode's indicator variable
361 \(listed in `minor-mode-alist') must also be a function whose documentation
362 describes the minor mode."
364 (with-output-to-temp-buffer "*Help*"
365 (when minor-mode-alist
366 (princ "The major mode is described first.
367 For minor modes, see following pages.\n\n"))
370 (princ (documentation major-mode))
371 (help-setup-xref (list #'help-xref-mode (current-buffer)) (interactive-p))
372 (let ((minor-modes minor-mode-alist))
374 (let* ((minor-mode (car (car minor-modes)))
375 (indicator (car (cdr (car minor-modes)))))
376 ;; Document a minor mode if it is listed in minor-mode-alist,
377 ;; bound locally in this buffer, non-nil, and has a function
379 (if (and (symbol-value minor-mode)
380 (fboundp minor-mode))
381 (let ((pretty-minor-mode minor-mode))
382 (if (string-match "-mode$" (symbol-name minor-mode))
383 (setq pretty-minor-mode
385 (substring (symbol-name minor-mode)
386 0 (match-beginning 0)))))
387 (while (and indicator (symbolp indicator)
389 (not (eq indicator (symbol-value indicator))))
390 (setq indicator (symbol-value indicator)))
392 (princ (format "%s minor mode (%s):\n"
395 (format "indicator%s" indicator)
397 (princ (documentation minor-mode)))))
398 (setq minor-modes (cdr minor-modes))))
399 (print-help-return-message)))
401 ;; So keyboard macro definitions are documented correctly
402 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
404 (defun describe-distribution ()
405 "Display info on how to obtain the latest version of GNU Emacs."
408 (expand-file-name "DISTRIB" data-directory)))
410 (defun describe-copying ()
411 "Display info on how you may redistribute copies of GNU Emacs."
414 (expand-file-name "COPYING" data-directory))
415 (goto-char (point-min)))
417 (defun describe-project ()
418 "Display info on the GNU project."
421 (expand-file-name "GNU" data-directory))
422 (goto-char (point-min)))
424 (defun describe-no-warranty ()
425 "Display info on all the kinds of warranty Emacs does NOT have."
428 (let (case-fold-search)
429 (search-forward "NO WARRANTY")
432 (defun describe-prefix-bindings ()
433 "Describe the bindings of the prefix used to reach this command.
434 The prefix described consists of all but the last event
435 of the key sequence that ran this command."
437 (let* ((key (this-command-keys)))
440 (substring key 0 (1- (length key)))
441 (let ((prefix (make-vector (1- (length key)) nil))
443 (while (< i (length prefix))
444 (aset prefix i (aref key i))
447 ;; Make C-h after a prefix, when not specifically bound,
448 ;; run describe-prefix-bindings.
449 (setq prefix-help-command 'describe-prefix-bindings)
451 (defun view-emacs-news (&optional arg)
452 "Display info on recent changes to Emacs.
453 With numeric argument display information on correspondingly older changes."
455 (let* ((arg (if arg (prefix-numeric-value arg) 0)))
457 (expand-file-name (concat (make-string arg ?O) "NEWS")
460 (defun view-emacs-FAQ ()
461 "Display the Emacs Frequently Asked Questions (FAQ) file."
463 ;;; (find-file-read-only (expand-file-name "FAQ" data-directory))
464 (info "(emacs-faq)"))
466 (defun view-emacs-problems ()
467 "Display info on known problems with Emacs and possible workarounds."
469 (view-file (expand-file-name "PROBLEMS" data-directory)))
471 (defun view-lossage ()
472 "Display last 100 input keystrokes."
474 (with-output-to-temp-buffer "*Help*"
475 (princ (mapconcat (function (lambda (key)
476 (if (or (integerp key)
479 (single-key-description key)
480 (prin1-to-string key nil))))
484 (set-buffer standard-output)
485 (goto-char (point-min))
486 (while (progn (move-to-column 50) (not (eobp)))
487 (search-forward " " nil t)
489 (setq help-xref-stack nil
490 help-xref-stack-item nil))
491 (print-help-return-message)))
493 (defalias 'help 'help-for-help)
494 (make-help-screen help-for-help
495 "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:"
496 "You have typed %THIS-KEY%, the help character. Type a Help option:
497 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
499 a command-apropos. Give a substring, and see a list of commands
500 (functions interactively callable) that contain
501 that substring. See also the apropos command.
502 b describe-bindings. Display table of all key bindings.
503 c describe-key-briefly. Type a command key sequence;
504 it prints the function name that sequence runs.
505 C describe-coding-system. This describes either a specific coding system
506 (if you type its name) or the coding systems currently in use
507 (if you type just RET).
508 f describe-function. Type a function name and get documentation of it.
509 C-f Info-goto-emacs-command-node. Type a function name;
510 it takes you to the Info node for that command.
511 i info. The info documentation reader.
512 I describe-input-method. Describe a specific input method (if you type
513 its name) or the current input method (if you type just RET).
514 C-i info-lookup-symbol. Display the definition of a specific symbol
515 as found in the manual for the language this buffer is written in.
516 k describe-key. Type a command key sequence;
517 it displays the full documentation.
518 C-k Info-goto-emacs-key-command-node. Type a command key sequence;
519 it takes you to the Info node for the command bound to that key.
520 l view-lossage. Show last 100 characters you typed.
521 L describe-language-environment. This describes either the a
522 specific language environment (if you type its name)
523 or the current language environment (if you type just RET).
524 m describe-mode. Print documentation of current minor modes,
525 and the current major mode, including their special commands.
526 n view-emacs-news. Display news of recent Emacs changes.
527 p finder-by-keyword. Find packages matching a given topic keyword.
528 s describe-syntax. Display contents of syntax table, plus explanations
529 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
530 v describe-variable. Type name of a variable;
531 it displays the variable's documentation and value.
532 w where-is. Type command name; it prints which keystrokes
535 F Display the frequently asked questions file.
536 h Display the HELLO file which illustrates various scripts.
537 C-c Display Emacs copying permission (General Public License).
538 C-d Display Emacs ordering information.
539 C-n Display news of recent Emacs changes.
540 C-p Display information about the GNU project.
541 C-w Display information on absence of warranty for GNU Emacs."
544 (defun function-called-at-point ()
545 "Return a function around point or else called by the list containing point.
546 If that doesn't give a function, return nil."
547 (let ((stab (syntax-table)))
548 (set-syntax-table emacs-lisp-mode-syntax-table)
550 (or (condition-case ()
552 (or (not (zerop (skip-syntax-backward "_w")))
553 (eq (char-syntax (following-char)) ?w)
554 (eq (char-syntax (following-char)) ?_)
556 (skip-chars-forward "'")
557 (let ((obj (read (current-buffer))))
558 (and (symbolp obj) (fboundp obj) obj)))
563 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
564 ;; Move up to surrounding paren, then after the open.
567 ;; If there is space here, this is probably something
568 ;; other than a real Lisp function call, so ignore it.
569 (if (looking-at "[ \t]")
570 (error "Probably not a Lisp function call"))
572 (setq obj (read (current-buffer)))
573 (and (symbolp obj) (fboundp obj) obj))))
575 (set-syntax-table stab))))
577 (defvar symbol-file-load-history-loaded nil
578 "Non-nil means we have loaded the file `fns-VERSION.el' in `exec-directory'.
579 That file records the part of `load-history' for preloaded files,
580 which is cleared out before dumping to make Emacs smaller.")
582 (defun symbol-file (function)
583 "Return the input source from which FUNCTION was loaded.
584 The value is normally a string that was passed to `load':
585 either an absolute file name, or a library name
586 \(with no directory name and no `.el' or `.elc' at the end).
587 It can also be nil, if the definition is not associated with any file."
588 (unless symbol-file-load-history-loaded
589 (load (expand-file-name
590 ;; fns-XX.YY.ZZ.el does not work on DOS filesystem.
591 (if (eq system-type 'ms-dos)
593 (format "fns-%s.el" emacs-version))
595 ;; The file name fns-%s.el already has a .el extension.
597 (setq symbol-file-load-history-loaded t))
598 (let ((files load-history)
601 (if (memq function (cdr (car files)))
602 (setq file (car (car files)) files nil))
603 (setq files (cdr files)))
606 (defun describe-function (function)
607 "Display the full documentation of FUNCTION (a symbol)."
609 (let ((fn (function-called-at-point))
610 (enable-recursive-minibuffers t)
612 (setq val (completing-read (if fn
613 (format "Describe function (default %s): " fn)
614 "Describe function: ")
615 obarray 'fboundp t nil nil (symbol-name fn)))
616 (list (if (equal val "")
619 (with-output-to-temp-buffer "*Help*"
621 ;; Use " is " instead of a colon so that
622 ;; it is easier to get out the function name using forward-sexp.
624 (describe-function-1 function nil (interactive-p))
625 (print-help-return-message)
627 (set-buffer standard-output)
628 ;; Return the text we displayed.
630 (message "You didn't specify a function")))
632 (defun describe-function-1 (function parens interactive-p)
633 (let* ((def (if (symbolp function)
634 (symbol-function function)
636 file-name string need-close
637 (beg (if (commandp def) "an interactive " "a ")))
639 (cond ((or (stringp def)
643 (concat beg "built-in function"))
644 ((byte-code-function-p def)
645 (concat beg "compiled Lisp function"))
647 (while (symbolp (symbol-function def))
648 (setq def (symbol-function def)))
649 (format "an alias for `%s'" def))
650 ((eq (car-safe def) 'lambda)
651 (concat beg "Lisp function"))
652 ((eq (car-safe def) 'macro)
654 ((eq (car-safe def) 'mocklisp)
655 "a mocklisp function")
656 ((eq (car-safe def) 'autoload)
657 (setq file-name (nth 1 def))
658 (format "%s autoloaded %s"
659 (if (commandp def) "an interactive" "an")
660 (if (eq (nth 4 def) 'keymap) "keymap"
661 (if (nth 4 def) "Lisp macro" "Lisp function"))
663 ;; perhaps use keymapp here instead
664 ((eq (car-safe def) 'keymap)
666 (elts (cdr-safe def)))
668 (if (char-table-p (car-safe elts))
671 (setq elts (cdr-safe elts)))
676 (when (and parens (not (equal string "")))
680 (with-current-buffer "*Help*"
683 (if (re-search-backward "alias for `\\([^`']+\\)'" nil t)
684 (help-xref-button 1 #'describe-function def)))))
686 (setq file-name (symbol-file function)))
690 ;; We used to add .el to the file name,
691 ;; but that's completely wrong when the user used load-file.
694 ;; Make a hyperlink to the library.
695 (with-current-buffer "*Help*"
697 (re-search-backward "`\\([^`']+\\)'" nil t)
698 (help-xref-button 1 #'(lambda (arg)
700 (find-function-noselect arg)))
701 (pop-to-buffer (car location))
702 (goto-char (cdr location))))
704 (if need-close (princ ")"))
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)
719 (princ (cons (if (symbolp function) function "anonymous")
720 (mapcar (lambda (arg)
721 (if (memq arg '(&optional &rest))
723 (intern (upcase (symbol-name arg)))))
726 (let ((doc (documentation function)))
730 (help-setup-xref (list #'describe-function function) interactive-p))
731 (princ "not documented")))))
733 (defun variable-at-point ()
734 "Return the bound variable symbol found around point.
735 Return 0 if there is no such symbol."
737 (let ((stab (syntax-table)))
740 (set-syntax-table emacs-lisp-mode-syntax-table)
741 (or (not (zerop (skip-syntax-backward "_w")))
742 (eq (char-syntax (following-char)) ?w)
743 (eq (char-syntax (following-char)) ?_)
745 (skip-chars-forward "'")
746 (let ((obj (read (current-buffer))))
747 (or (and (symbolp obj) (boundp obj) obj)
749 (set-syntax-table stab)))
752 (defun describe-variable (variable)
753 "Display the full documentation of VARIABLE (a symbol).
754 Returns the documentation as a string, also."
756 (let ((v (variable-at-point))
757 (enable-recursive-minibuffers t)
759 (setq val (completing-read (if (symbolp v)
760 (format "Describe variable (default %s): " v)
761 "Describe variable: ")
762 obarray 'boundp t nil nil
763 (if (symbolp v) (symbol-name v))))
764 (list (if (equal val "")
766 (if (symbolp variable)
768 (with-output-to-temp-buffer "*Help*"
770 (if (not (boundp variable))
775 (princ "'s value is ")
777 (pp (symbol-value variable))
779 (if (local-variable-p variable)
781 (princ (format "Local in buffer %s; " (buffer-name)))
782 (if (not (default-boundp variable))
783 (princ "globally void")
784 (princ "global value is ")
786 (pp (default-value variable)))
790 (set-buffer standard-output)
791 (if (> (count-lines (point-min) (point-max)) 10)
793 (goto-char (point-min))
797 (delete-region (point) (progn (end-of-line) (point)))
798 (insert "'s value is shown below.\n\n")
800 (insert "\n\nValue:"))))))
801 (princ "Documentation:")
803 (let ((doc (documentation-property variable 'variable-documentation)))
804 (princ (or doc "not documented as a variable.")))
805 (help-setup-xref (list #'describe-variable variable) (interactive-p))
807 ;; Make a link to customize if this variable can be customized.
808 ;; Note, it is not reliable to test only for a custom-type property
809 ;; because those are only present after the var's definition
811 (if (or (get variable 'custom-type) ; after defcustom
812 (get variable 'custom-loads) ; from loaddefs.el
813 (get variable 'standard-value)) ; from cus-start.el
814 (let ((customize-label "customize"))
817 (princ (concat "You can " customize-label " this variable."))
818 (with-current-buffer "*Help*"
821 (concat "\\(" customize-label "\\)") nil t)
822 (help-xref-button 1 #'(lambda (v)
823 (customize-variable v)) variable)
825 ;; Make a hyperlink to the library if appropriate. (Don't
826 ;; change the format of the buffer's initial line in case
827 ;; anything expects the current format.)
828 (let ((file-name (symbol-file variable)))
830 (princ "\n\nDefined in `")
833 (with-current-buffer "*Help*"
835 (re-search-backward "`\\([^`']+\\)'" nil t)
836 (help-xref-button 1 (lambda (arg)
838 (find-variable-noselect arg)))
839 (pop-to-buffer (car location))
840 (goto-char (cdr location))))
843 (print-help-return-message)
845 (set-buffer standard-output)
846 ;; Return the text we displayed.
848 (message "You did not specify a variable")))
850 (defun describe-bindings (&optional prefix buffer)
851 "Show a list of all defined keys, and their definitions.
852 We put that list in a buffer, and display the buffer.
854 The optional argument PREFIX, if non-nil, should be a key sequence;
855 then we display only bindings that start with that prefix.
856 The optional argument BUFFER specifies which buffer's bindings
857 to display (default, the current buffer)."
859 (or buffer (setq buffer (current-buffer)))
860 (with-current-buffer buffer
861 (describe-bindings-internal nil prefix))
862 (with-current-buffer "*Help*"
863 (help-setup-xref (list #'describe-bindings prefix buffer)
866 (defun where-is (definition &optional insert)
867 "Print message listing key sequences that invoke the command DEFINITION.
868 Argument is a command definition, usually a symbol with a function definition.
869 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
871 (let ((fn (function-called-at-point))
872 (enable-recursive-minibuffers t)
874 (setq val (completing-read (if fn
875 (format "Where is command (default %s): " fn)
876 "Where is command: ")
878 (list (if (equal val "")
880 current-prefix-arg)))
881 (let* ((keys (where-is-internal definition overriding-local-map nil nil))
882 (keys1 (mapconcat 'key-description keys ", "))
883 (standard-output (if insert (current-buffer) t)))
885 (if (> (length keys1) 0)
886 (princ (format "%s (%s)" keys1 definition))
887 (princ (format "M-x %s RET" definition)))
888 (if (> (length keys1) 0)
889 (princ (format "%s is on %s" definition keys1))
890 (princ (format "%s is not on any key" definition)))))
893 (defun locate-library (library &optional nosuffix path interactive-call)
894 "Show the precise file name of Emacs library LIBRARY.
895 This command searches the directories in `load-path' like `M-x load-library'
896 to find the file that `M-x load-library RET LIBRARY RET' would load.
897 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
898 to the specified name LIBRARY.
900 If the optional third arg PATH is specified, that list of directories
901 is used instead of `load-path'.
903 When called from a program, the file name is normaly returned as a
904 string. When run interactively, the argument INTERACTIVE-CALL is t,
905 and the file name is displayed in the echo area."
906 (interactive (list (read-string "Locate library: ")
915 (let ((try (expand-file-name (concat library suf) dir)))
916 (and (file-readable-p try)
917 (null (file-directory-p try))
920 (throw 'answer try)))))
924 ;;; load doesn't handle this yet.
925 ;;; (let ((basic '(".elc" ".el" ""))
926 ;;; (compressed '(".Z" ".gz" "")))
927 ;;; ;; If autocompression mode is on,
928 ;;; ;; consider all combinations of library suffixes
929 ;;; ;; and compression suffixes.
930 ;;; (if (rassq 'jka-compr-handler file-name-handler-alist)
932 ;;; (mapcar (lambda (compelt)
933 ;;; (mapcar (lambda (baselt)
934 ;;; (concat baselt compelt))
939 (or path load-path)))
940 (and interactive-call
942 (message "Library is file %s" result)
943 (message "No library %s in search path" library)))
947 ;;; Grokking cross-reference information in doc strings and
950 ;; This may have some scope for extension and the same or something
951 ;; similar should be done for widget doc strings, which currently use
952 ;; another mechanism.
954 (defcustom help-highlight-p t
955 "*If non-nil, `help-make-xrefs' highlight cross-references.
956 Under a window system it highlights them with face defined by
957 `help-highlight-face'."
962 (defcustom help-highlight-face 'underline
963 "Face used by `help-make-xrefs' to highlight cross-references.
964 Must be previously-defined."
969 (defvar help-back-label "[back]"
970 "Label to use by `help-make-xrefs' for the go-back reference.")
972 (defvar help-xref-symbol-regexp
973 (concat "\\(\\<\\(\\(variable\\|option\\)\\|"
974 "\\(function\\|command\\)\\|"
975 "\\(symbol\\)\\)\\s-+\\)?"
976 ;; Note starting with word-syntax character:
977 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'")
978 "Regexp matching doc string references to symbols.
980 The words preceding the quoted symbol can be used in doc strings to
981 distinguish references to variables, functions and symbols.")
983 (defvar help-xref-info-regexp
984 "\\<[Ii]nfo[ \t\n]+node[ \t\n]+`\\([^']+\\)'"
985 "Regexp matching doc string references to an Info node.")
987 (defun help-setup-xref (item interactive-p)
988 "Invoked from commands using the \"*Help*\" buffer to install some xref info.
990 ITEM is a (FUNCTION . ARGS) pair appropriate for recreating the help
991 buffer after following a reference. INTERACTIVE-P is non-nil if the
992 calling command was invoked interactively. In this case the stack of
993 items for help buffer \"back\" buttons is cleared."
995 (setq help-xref-stack nil))
996 (setq help-xref-stack-item item))
998 (defun help-make-xrefs (&optional buffer)
999 "Parse and hyperlink documentation cross-references in the given BUFFER.
1001 Find cross-reference information in a buffer and, if
1002 `help-highlight-p' is non-nil, highlight it with face defined by
1003 `help-highlight-face'; activate such cross references for selection
1004 with `help-follow'. Cross-references have the canonical form `...'
1005 and the type of reference may be disambiguated by the preceding
1006 word(s) used in `help-xref-symbol-regexp'.
1008 A special reference `back' is made to return back through a stack of
1009 help buffers. Variable `help-back-label' specifies the text for
1013 (set-buffer (or buffer (current-buffer)))
1014 (goto-char (point-min))
1015 ;; Skip the header-type info, though it might be useful to parse
1016 ;; it at some stage (e.g. "function in `library'").
1018 (let ((old-modified (buffer-modified-p)))
1019 (let ((stab (syntax-table))
1020 (case-fold-search t)
1021 (inhibit-read-only t))
1022 (set-syntax-table emacs-lisp-mode-syntax-table)
1023 ;; The following should probably be abstracted out.
1028 (while (re-search-forward help-xref-info-regexp nil t)
1029 (let ((data (match-string 1)))
1031 (unless (string-match "^([^)]+)" data)
1032 (setq data (concat "(emacs)" data))))
1033 (help-xref-button 1 #'info data))))
1036 (while (re-search-forward help-xref-symbol-regexp nil t)
1037 (let* ((data (match-string 6))
1038 (sym (intern-soft data)))
1041 ((match-string 3) ; `variable' &c
1042 (and (boundp sym) ; `variable' doesn't ensure
1043 ; it's actually bound
1044 (help-xref-button 6 #'describe-variable sym)))
1045 ((match-string 4) ; `function' &c
1046 (and (fboundp sym) ; similarly
1047 (help-xref-button 6 #'describe-function sym)))
1048 ((match-string 5)) ; nothing for symbol
1049 ((or (boundp sym) (fboundp sym))
1050 ;; We can't intuit whether to use the
1051 ;; variable or function doc -- supply both.
1052 (help-xref-button 6 #'help-xref-interned sym)))))))
1053 ;; An obvious case of a key substitution:
1055 (while (re-search-forward
1056 ;; Assume command name is only word characters
1057 ;; and dashes to get things like `use M-x foo.'.
1058 "\\<M-x\\s-+\\(\\sw\\(\\sw\\|-\\)+\\)" nil t)
1059 (let ((sym (intern-soft (match-string 1))))
1061 (help-xref-button 1 #'describe-function sym)))))
1062 ;; Look for commands in whole keymap substitutions:
1064 ;; Make sure to find the first keymap.
1065 (goto-char (point-min))
1066 ;; Find a header and the column at which the command
1067 ;; name will be found.
1068 (while (re-search-forward "^key +binding\n\\(-+ +\\)-+\n\n"
1070 (let ((col (- (match-end 1) (match-beginning 1))))
1072 ;; Ignore single blank lines in table, but not
1073 ;; double ones, which should terminate it.
1074 (and (not (looking-at "\n\\s-*\n"))
1076 (and (eolp) (forward-line))
1078 (skip-chars-backward "^\t\n")
1079 (if (and (>= (current-column) col)
1080 (looking-at "\\(\\sw\\|-\\)+$"))
1081 (let ((sym (intern-soft (match-string 0))))
1084 0 #'describe-function sym))))
1085 (zerop (forward-line)))))))))
1086 (set-syntax-table stab))
1087 ;; Make a back-reference in this buffer if appropriate.
1088 (when help-xref-stack
1089 (goto-char (point-max))
1091 (insert "\n\n" help-back-label))
1092 ;; Just to provide the match data:
1093 (looking-at (concat "\n\n\\(" (regexp-quote help-back-label) "\\)"))
1094 (help-xref-button 1 #'help-xref-go-back (current-buffer))))
1095 ;; View mode steals RET from us.
1096 (set (make-local-variable 'minor-mode-overriding-map-alist)
1097 (list (cons 'view-mode
1098 (let ((map (make-sparse-keymap)))
1099 (set-keymap-parent map view-mode-map)
1100 (define-key map "\r" 'help-follow)
1102 (set-buffer-modified-p old-modified))))
1104 (defun help-xref-button (match-number function data)
1105 "Make a hyperlink for cross-reference text previously matched.
1107 MATCH-NUMBER is the subexpression of interest in the last matched
1108 regexp. FUNCTION is a function to invoke when the button is
1109 activated, applied to DATA. DATA may be a single value or a list.
1110 See `help-make-xrefs'."
1111 ;; Don't mung properties we've added specially in some instances.
1112 (unless (get-text-property (match-beginning match-number) 'help-xref)
1113 (add-text-properties (match-beginning match-number)
1114 (match-end match-number)
1115 (list 'mouse-face 'highlight
1116 'help-xref (cons function
1120 (if help-highlight-p
1121 (put-text-property (match-beginning match-number)
1122 (match-end match-number)
1123 'face help-highlight-face))))
1126 ;; Additional functions for (re-)creating types of help buffers.
1127 (defun help-xref-interned (symbol)
1128 "Follow a hyperlink which appeared to be an arbitrary interned SYMBOL.
1130 Both variable and function documentation are extracted into a single
1132 (let ((fdoc (when (fboundp symbol) (describe-function symbol))))
1133 (when (or (boundp symbol) (not fdoc))
1134 (describe-variable symbol)
1135 ;; We now have a help buffer on the variable. Insert the function
1138 (with-current-buffer "*Help*"
1139 (goto-char (point-min))
1140 (let ((inhibit-read-only t))
1141 (insert fdoc "\n\n" (symbol-name symbol) " is also a variable.\n\n"))
1142 (help-setup-xref (list #'help-xref-interned symbol) nil))))))
1144 (defun help-xref-mode (buffer)
1145 "Do a `describe-mode' for the specified BUFFER."
1150 ;;; Navigation/hyperlinking with xrefs
1152 (defun help-follow-mouse (click)
1153 "Follow the cross-reference that you click on."
1155 (let* ((start (event-start click))
1156 (window (car start))
1157 (pos (car (cdr start))))
1158 (with-current-buffer (window-buffer window)
1159 (help-follow pos))))
1161 (defun help-xref-go-back (buffer)
1162 "From BUFFER, go back to previous help buffer text using `help-xref-stack'."
1163 (let (item position method args)
1164 (with-current-buffer buffer
1165 (when help-xref-stack
1166 (setq help-xref-stack (cdr help-xref-stack)) ; due to help-follow
1167 (setq item (car help-xref-stack)
1171 (setq help-xref-stack (cdr help-xref-stack))))
1173 (goto-char position)))
1175 (defun help-go-back ()
1176 "Invoke the [back] button (if any) in the Help mode buffer."
1178 (help-follow (1- (point-max))))
1180 (defun help-follow (&optional pos)
1181 "Follow cross-reference at POS, defaulting to point.
1183 For the cross-reference format, see `help-make-xrefs'."
1188 (or (and (not (= pos (point-max)))
1189 (get-text-property pos 'help-xref))
1190 (and (not (= pos (point-min)))
1191 (get-text-property (1- pos) 'help-xref))
1192 ;; check if the symbol under point is a function or variable
1196 (goto-char pos) (skip-syntax-backward "w_")
1197 (buffer-substring (point)
1198 (progn (skip-syntax-forward "w_")
1200 (when (or (boundp sym) (fboundp sym))
1201 (list #'help-xref-interned sym)))))
1202 (method (car help-data))
1203 (args (cdr help-data)))
1205 (setq help-xref-stack (cons (cons (point) help-xref-stack-item)
1207 (setq help-xref-stack-item nil)
1208 ;; There is a reference at point. Follow it.
1209 (apply method args))))
1211 ;; For tabbing through buffer.
1212 (defun help-next-ref ()
1213 "Find the next help cross-reference in the buffer."
1217 (if (get-text-property (point) 'help-xref) ; move off reference
1218 (goto-char (or (next-single-property-change (point) 'help-xref)
1220 (cond ((setq pos (next-single-property-change (point) 'help-xref))
1221 (if pos (goto-char pos)))
1223 (message "No cross references in the buffer.")
1226 (goto-char (point-min)))))))
1228 (defun help-previous-ref ()
1229 "Find the previous help cross-reference in the buffer."
1233 (if (get-text-property (point) 'help-xref) ; move off reference
1234 (goto-char (or (previous-single-property-change (point) 'help-xref)
1236 (cond ((setq pos (previous-single-property-change (point) 'help-xref))
1237 (if pos (goto-char pos)))
1239 (message "No cross references in the buffer.")
1242 (goto-char (point-max)))))))
1245 ;;; Automatic resizing of temporary buffers.
1247 (defcustom temp-buffer-resize-mode nil
1248 "Non-nil means resize windows displaying temporary buffers.
1249 This makes the window the right height for its contents, but never
1250 more than `temp-buffer-max-height' nor less than `window-min-height'.
1251 This applies to `help', `apropos' and `completion' buffers, and some others.
1253 Setting this variable directly does not take effect;
1254 use either \\[customize] or the function `temp-buffer-resize-mode'."
1255 :get (lambda (symbol)
1256 (and (memq 'resize-temp-buffer-window temp-buffer-show-hook) t))
1257 :set (lambda (symbol value)
1258 (temp-buffer-resize-mode (if value 1 -1)))
1259 :initialize 'custom-initialize-default
1264 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
1265 "*Maximum height of a window displaying a temporary buffer.
1266 This is the maximum height (in text lines) which `resize-temp-buffer-window'
1267 will give to a window displaying a temporary buffer.
1268 It can also be a function which will be called with the object corresponding
1269 to the buffer to be displayed as argument and should return an integer
1271 :type '(choice integer function)
1275 (defun temp-buffer-resize-mode (arg)
1276 "Toggle the mode which that makes windows smaller for temporary buffers.
1277 With prefix argument ARG, turn the resizing of windows displaying temporary
1278 buffers on if ARG is positive or off otherwise.
1279 See the documentation of the variable `temp-buffer-resize-mode' for
1284 (not (memq 'resize-temp-buffer-window temp-buffer-show-hook))
1285 (> (prefix-numeric-value arg) 0))))
1288 ;; `help-mode-maybe' may add a `back' button and thus increase the
1289 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
1290 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
1291 (setq temp-buffer-resize-mode t))
1292 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)
1293 (setq temp-buffer-resize-mode nil))))
1295 (defun resize-temp-buffer-window ()
1296 "Resize the current window to fit its contents.
1297 Will not make it higher than `temp-buffer-max-height' nor smaller than
1298 `window-min-height'. Do nothing if it is the only window on its frame, if it
1299 is not as wide as the frame or if some of the window's contents are scrolled
1301 (unless (or (one-window-p 'nomini)
1302 (not (pos-visible-in-window-p (point-min)))
1303 (/= (frame-width) (window-width)))
1304 (let* ((max-height (if (functionp temp-buffer-max-height)
1305 (funcall temp-buffer-max-height (current-buffer))
1306 temp-buffer-max-height))
1307 (win-height (1- (window-height)))
1308 (min-height (1- window-min-height))
1309 (text-height (window-buffer-height(selected-window)))
1310 (new-height (max (min text-height max-height) min-height)))
1311 (enlarge-window (- new-height win-height)))))
1313 ;;; help.el ends here