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 "s" 'describe-syntax)
88 (define-key help-map "t" 'help-with-tutorial)
90 (define-key help-map "w" 'where-is)
92 (define-key help-map "v" 'describe-variable)
94 (define-key help-map "q" 'help-quit)
96 (define-key help-mode-map [mouse-2] 'help-follow-mouse)
97 (define-key help-mode-map "\C-c\C-b" 'help-go-back)
98 (define-key help-mode-map "\C-c\C-c" 'help-follow)
99 (define-key help-mode-map "\t" 'help-next-ref)
100 (define-key help-mode-map [backtab] 'help-previous-ref)
101 (define-key help-mode-map [(shift tab)] 'help-previous-ref)
102 ;; Documentation only, since we use minor-mode-overriding-map-alist.
103 (define-key help-mode-map "\r" 'help-follow)
105 ;; Font-locking is incompatible with the new xref stuff.
106 ;(defvar help-font-lock-keywords
108 ; (let ((name-char "[-+a-zA-Z0-9_*]") (sym-char "[-+a-zA-Z0-9_:*]"))
111 ; ;; The symbol itself.
112 ; (list (concat "\\`\\(" name-char "+\\)\\(\\(:\\)\\|\\('\\)\\)")
113 ; '(1 (if (match-beginning 3)
114 ; font-lock-function-name-face
115 ; font-lock-variable-name-face)))
117 ; ;; Words inside `' which tend to be symbol names.
118 ; (list (concat "`\\(" sym-char sym-char "+\\)'")
119 ; 1 'font-lock-constant-face t)
121 ; ;; CLisp `:' keywords as references.
122 ; (list (concat "\\<:" sym-char "+\\>") 0 'font-lock-builtin-face t))))
123 ; "Default expressions to highlight in Help mode.")
125 (defvar help-xref-stack nil
126 "A stack of ways by which to return to help buffers after following xrefs.
127 Used by `help-follow' and `help-xref-go-back'.
128 An element looks like (POSITION FUNCTION ARGS...).
129 To use the element, do (apply FUNCTION ARGS) then (goto-char POSITION).")
130 (put 'help-xref-stack 'permanent-local t)
132 (defvar help-xref-stack-item nil
133 "An item for `help-follow' in this buffer to push onto `help-xref-stack'.
134 The format is (FUNCTION ARGS...).")
135 (put 'help-xref-stack-item 'permanent-local t)
137 (setq-default help-xref-stack nil help-xref-stack-item nil)
140 "Major mode for viewing help text and navigating references in it.
141 Entry to this mode runs the normal hook `help-mode-hook'.
145 (kill-all-local-variables)
146 (use-local-map help-mode-map)
147 (setq mode-name "Help")
148 (setq major-mode 'help-mode)
149 (make-local-variable 'font-lock-defaults)
150 (setq font-lock-defaults nil) ; font-lock would defeat xref
152 (make-local-variable 'view-no-disable-on-exit)
153 (setq view-no-disable-on-exit t)
154 ;; `help-make-xrefs' would be run here if not invoked from
155 ;; `help-mode-maybe'.
156 (run-hooks 'help-mode-hook))
158 (defun help-mode-setup ()
160 (setq buffer-read-only nil))
162 (add-hook 'temp-buffer-setup-hook 'help-mode-setup)
164 (defun help-mode-finish ()
165 (when (eq major-mode 'help-mode)
166 ;; View mode's read-only status of existing *Help* buffer is lost
167 ;; by with-output-to-temp-buffer.
169 (help-make-xrefs (current-buffer)))
170 (setq view-return-to-alist
171 (list (cons (selected-window) help-return-method))))
173 (add-hook 'temp-buffer-show-hook 'help-mode-finish)
176 "Just exit from the Help command's command loop."
180 (defun help-with-tutorial (&optional arg)
181 "Select the Emacs learn-by-doing tutorial.
182 If there is a tutorial version written in the language
183 of the selected language environment, that version is used.
184 If there's no tutorial in that language, `TUTORIAL' is selected.
185 With arg, you are asked to choose which language."
188 (read-language-name 'tutorial "Language: " "English")
189 (if (get-language-info current-language-environment 'tutorial)
190 current-language-environment
193 (setq filename (get-language-info lang 'tutorial))
194 (setq file (expand-file-name (concat "~/" filename)))
195 (delete-other-windows)
196 (if (get-file-buffer file)
197 (switch-to-buffer (get-file-buffer file))
198 (switch-to-buffer (create-file-buffer file))
199 (setq buffer-file-name file)
200 (setq default-directory (expand-file-name "~/"))
201 (setq buffer-auto-save-file-name nil)
202 (insert-file-contents (expand-file-name filename data-directory))
203 (goto-char (point-min))
204 (search-forward "\n<<")
206 (delete-region (point) (progn (end-of-line) (point)))
207 (let ((n (- (window-height (selected-window))
208 (count-lines (point-min) (point))
212 ;; Some people get confused by the large gap.
214 (insert "[Middle of page left blank for didactic purposes. "
215 "Text continues below]")
216 (newline (- n (/ n 2)))))
217 (goto-char (point-min))
218 (set-buffer-modified-p nil))))
220 (defun mode-line-key-binding (key)
221 "Value is the binding of KEY in the mode line or nil if none."
222 (let (string-info defn)
223 (when (and (eq 'mode-line (aref key 0))
224 (consp (setq string-info (nth 4 (event-start (aref key 1))))))
225 (let* ((string (car string-info))
226 (pos (cdr string-info))
227 (local-map (and (> pos 0)
228 (< pos (length string))
229 (get-text-property pos 'local-map string))))
230 (setq defn (and local-map (lookup-key local-map key)))))
233 (defun describe-key-briefly (key &optional insert)
234 "Print the name of the function KEY invokes. KEY is a string.
235 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
236 (interactive "kDescribe key briefly: \nP")
238 (let ((modifiers (event-modifiers (aref key 0)))
239 (standard-output (if insert (current-buffer) t))
241 ;; For a mouse button event, go to the button it applies to
242 ;; to get the right key bindings. And go to the right place
243 ;; in case the keymap depends on where you clicked.
244 (if (or (memq 'click modifiers) (memq 'down modifiers)
245 (memq 'drag modifiers))
246 (setq window (posn-window (event-start (aref key 0)))
247 position (posn-point (event-start (aref key 0)))))
250 (set-buffer (window-buffer window))
251 (goto-char position)))
252 ;; Ok, now look up the key and name the command.
253 (let ((defn (or (mode-line-key-binding key)
255 (key-desc (key-description key)))
256 (if (or (null defn) (integerp defn))
257 (princ (format "%s is undefined" key-desc))
258 (princ (format (if insert
261 "%s at that spot runs the command %s"
262 "%s runs the command %s"))
264 (if (symbolp defn) defn (prin1-to-string defn)))))))))
266 (defvar help-return-method nil
267 "What to do to \"exit\" the help buffer.
269 (WINDOW . t) delete the selected window, go to WINDOW.
270 (WINDOW . quit-window) do quit-window, then select WINDOW.
271 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
273 (defun print-help-return-message (&optional function)
274 "Display or return message saying how to restore windows after help command.
275 Computes a message and applies the optional argument FUNCTION to it.
276 If FUNCTION is nil, applies `message' to it, thus printing it."
277 (and (not (get-buffer-window standard-output))
279 (cond ((special-display-p (buffer-name standard-output))
280 (setq help-return-method (cons (selected-window) t))
281 ;; If the help output buffer is a special display buffer,
282 ;; don't say anything about how to get rid of it.
283 ;; First of all, the user will do that with the window
284 ;; manager, not with Emacs.
285 ;; Secondly, the buffer has not been displayed yet,
286 ;; so we don't know whether its frame will be selected.
288 ((not (one-window-p t))
289 (setq help-return-method
290 (cons (selected-window) 'quit-window))
291 "Type \\[switch-to-buffer-other-window] RET to restore the other window.")
293 (setq help-return-method (cons (selected-window) t))
294 "Type \\[delete-other-windows] to remove help window.")
296 (setq help-return-method
297 (list (selected-window) (window-buffer)
298 (window-start) (window-point)))
299 "Type \\[switch-to-buffer] RET to remove help window."))))
300 (funcall (or function 'message)
303 (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: ")
322 (let ((modifiers (event-modifiers (aref key 0)))
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)))))
333 (set-buffer (window-buffer window))
334 (goto-char position)))
335 (let ((defn (or (mode-line-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))
341 (princ " at that spot"))
342 (princ " runs the command ")
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."
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"))
363 (princ (documentation major-mode))
364 (help-setup-xref (list #'help-xref-mode (current-buffer)) (interactive-p))
365 (let ((minor-modes minor-mode-alist))
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
372 (if (and (symbol-value minor-mode)
373 (fboundp minor-mode))
374 (let ((pretty-minor-mode minor-mode))
375 (if (string-match "-mode$" (symbol-name minor-mode))
376 (setq pretty-minor-mode
378 (substring (symbol-name minor-mode)
379 0 (match-beginning 0)))))
380 (while (and indicator (symbolp indicator)
382 (not (eq indicator (symbol-value indicator))))
383 (setq indicator (symbol-value indicator)))
385 (princ (format "%s minor mode (%s):\n"
388 (format "indicator%s" indicator)
390 (princ (documentation minor-mode)))))
391 (setq minor-modes (cdr minor-modes))))
392 (print-help-return-message)))
394 ;; So keyboard macro definitions are documented correctly
395 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
397 (defun describe-distribution ()
398 "Display info on how to obtain the latest version of GNU Emacs."
401 (expand-file-name "DISTRIB" data-directory)))
403 (defun describe-copying ()
404 "Display info on how you may redistribute copies of GNU Emacs."
407 (expand-file-name "COPYING" data-directory))
408 (goto-char (point-min)))
410 (defun describe-project ()
411 "Display info on the GNU project."
414 (expand-file-name "GNU" data-directory))
415 (goto-char (point-min)))
417 (defun describe-no-warranty ()
418 "Display info on all the kinds of warranty Emacs does NOT have."
421 (let (case-fold-search)
422 (search-forward "NO WARRANTY")
425 (defun describe-prefix-bindings ()
426 "Describe the bindings of the prefix used to reach this command.
427 The prefix described consists of all but the last event
428 of the key sequence that ran this command."
430 (let* ((key (this-command-keys)))
433 (substring key 0 (1- (length key)))
434 (let ((prefix (make-vector (1- (length key)) nil))
436 (while (< i (length prefix))
437 (aset prefix i (aref key i))
440 ;; Make C-h after a prefix, when not specifically bound,
441 ;; run describe-prefix-bindings.
442 (setq prefix-help-command 'describe-prefix-bindings)
444 (defun view-emacs-news (&optional arg)
445 "Display info on recent changes to Emacs.
446 With numeric argument display information on correspondingly older changes."
448 (let* ((arg (if arg (prefix-numeric-value arg) 0)))
450 (expand-file-name (concat (make-string arg ?O) "NEWS")
453 (defun view-emacs-FAQ ()
454 "Display the Emacs Frequently Asked Questions (FAQ) file."
456 ;;; (find-file-read-only (expand-file-name "FAQ" data-directory))
457 (info "(emacs-faq)"))
459 (defun view-lossage ()
460 "Display last 100 input keystrokes."
462 (with-output-to-temp-buffer "*Help*"
463 (princ (mapconcat (function (lambda (key)
464 (if (or (integerp key)
467 (single-key-description key)
468 (prin1-to-string key nil))))
472 (set-buffer standard-output)
473 (goto-char (point-min))
474 (while (progn (move-to-column 50) (not (eobp)))
475 (search-forward " " nil t)
477 (setq help-xref-stack nil
478 help-xref-stack-item nil))
479 (print-help-return-message)))
481 (defalias 'help 'help-for-help)
482 (make-help-screen help-for-help
483 "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:"
484 "You have typed %THIS-KEY%, the help character. Type a Help option:
485 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
487 a command-apropos. Give a substring, and see a list of commands
488 (functions interactively callable) that contain
489 that substring. See also the apropos command.
490 b describe-bindings. Display table of all key bindings.
491 c describe-key-briefly. Type a command key sequence;
492 it prints the function name that sequence runs.
493 C describe-coding-system. This describes either a specific coding system
494 (if you type its name) or the coding systems currently in use
495 (if you type just RET).
496 f describe-function. Type a function name and get documentation of it.
497 C-f Info-goto-emacs-command-node. Type a function name;
498 it takes you to the Info node for that command.
499 i info. The info documentation reader.
500 I describe-input-method. Describe a specific input method (if you type
501 its name) or the current input method (if you type just RET).
502 C-i info-lookup-symbol. Display the definition of a specific symbol
503 as found in the manual for the language this buffer is written in.
504 k describe-key. Type a command key sequence;
505 it displays the full documentation.
506 C-k Info-goto-emacs-key-command-node. Type a command key sequence;
507 it takes you to the Info node for the command bound to that key.
508 l view-lossage. Show last 100 characters you typed.
509 L describe-language-environment. This describes either the a
510 specific language environment (if you type its name)
511 or the current language environment (if you type just RET).
512 m describe-mode. Print documentation of current minor modes,
513 and the current major mode, including their special commands.
514 n view-emacs-news. Display news of recent Emacs changes.
515 p finder-by-keyword. Find packages matching a given topic keyword.
516 s describe-syntax. Display contents of syntax table, plus explanations
517 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
518 v describe-variable. Type name of a variable;
519 it displays the variable's documentation and value.
520 w where-is. Type command name; it prints which keystrokes
523 F Display the frequently asked questions file.
524 h Display the HELLO file which illustrates various scripts.
525 C-c Display Emacs copying permission (General Public License).
526 C-d Display Emacs ordering information.
527 C-n Display news of recent Emacs changes.
528 C-p Display information about the GNU project.
529 C-w Display information on absence of warranty for GNU Emacs."
532 (defun function-called-at-point ()
533 "Return a function around point or else called by the list containing point.
534 If that doesn't give a function, return nil."
535 (let ((stab (syntax-table)))
536 (set-syntax-table emacs-lisp-mode-syntax-table)
538 (or (condition-case ()
540 (or (not (zerop (skip-syntax-backward "_w")))
541 (eq (char-syntax (following-char)) ?w)
542 (eq (char-syntax (following-char)) ?_)
544 (skip-chars-forward "'")
545 (let ((obj (read (current-buffer))))
546 (and (symbolp obj) (fboundp obj) obj)))
551 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
552 ;; Move up to surrounding paren, then after the open.
555 ;; If there is space here, this is probably something
556 ;; other than a real Lisp function call, so ignore it.
557 (if (looking-at "[ \t]")
558 (error "Probably not a Lisp function call"))
560 (setq obj (read (current-buffer)))
561 (and (symbolp obj) (fboundp obj) obj))))
563 (set-syntax-table stab))))
565 (defvar symbol-file-load-history-loaded nil
566 "Non-nil means we have loaded the file `fns-VERSION.el' in `exec-directory'.
567 That file records the part of `load-history' for preloaded files,
568 which is cleared out before dumping to make Emacs smaller.")
570 (defun symbol-file (function)
571 "Return the input source from which FUNCTION was loaded.
572 The value is normally a string that was passed to `load':
573 either an absolute file name, or a library name
574 \(with no directory name and no `.el' or `.elc' at the end).
575 It can also be nil, if the definition is not associated with any file."
576 (unless symbol-file-load-history-loaded
577 (load (expand-file-name
578 ;; fns-XX.YY.ZZ.el does not work on DOS filesystem.
579 (if (eq system-type 'ms-dos)
581 (format "fns-%s.el" emacs-version))
583 ;; The file name fns-%s.el already has a .el extension.
585 (setq symbol-file-load-history-loaded t))
586 (let ((files load-history)
589 (if (memq function (cdr (car files)))
590 (setq file (car (car files)) files nil))
591 (setq files (cdr files)))
594 (defun describe-function (function)
595 "Display the full documentation of FUNCTION (a symbol)."
597 (let ((fn (function-called-at-point))
598 (enable-recursive-minibuffers t)
600 (setq val (completing-read (if fn
601 (format "Describe function (default %s): " fn)
602 "Describe function: ")
603 obarray 'fboundp t nil nil (symbol-name fn)))
604 (list (if (equal val "")
607 (with-output-to-temp-buffer "*Help*"
609 ;; Use " is " instead of a colon so that
610 ;; it is easier to get out the function name using forward-sexp.
612 (describe-function-1 function nil (interactive-p))
613 (print-help-return-message)
615 (set-buffer standard-output)
616 ;; Return the text we displayed.
618 (message "You didn't specify a function")))
620 (defun describe-function-1 (function parens interactive-p)
621 (let* ((def (if (symbolp function)
622 (symbol-function function)
624 file-name string need-close
625 (beg (if (commandp def) "an interactive " "a ")))
627 (cond ((or (stringp def)
631 (concat beg "built-in function"))
632 ((byte-code-function-p def)
633 (concat beg "compiled Lisp function"))
635 (while (symbolp (symbol-function def))
636 (setq def (symbol-function def)))
637 (format "an alias for `%s'" def))
638 ((eq (car-safe def) 'lambda)
639 (concat beg "Lisp function"))
640 ((eq (car-safe def) 'macro)
642 ((eq (car-safe def) 'mocklisp)
643 "a mocklisp function")
644 ((eq (car-safe def) 'autoload)
645 (setq file-name (nth 1 def))
646 (format "%s autoloaded %s"
647 (if (commandp def) "an interactive" "an")
648 (if (eq (nth 4 def) 'keymap) "keymap"
649 (if (nth 4 def) "Lisp macro" "Lisp function"))
651 ;; perhaps use keymapp here instead
652 ((eq (car-safe def) 'keymap)
654 (elts (cdr-safe def)))
656 (if (char-table-p (car-safe elts))
659 (setq elts (cdr-safe elts)))
664 (when (and parens (not (equal string "")))
668 (with-current-buffer "*Help*"
671 (if (re-search-backward "alias for `\\([^`']+\\)'" nil t)
672 (help-xref-button 1 #'describe-function def)))))
674 (setq file-name (symbol-file function)))
678 ;; We used to add .el to the file name,
679 ;; but that's completely wrong when the user used load-file.
682 ;; Make a hyperlink to the library.
683 (with-current-buffer "*Help*"
685 (re-search-backward "`\\([^`']+\\)'" nil t)
686 (help-xref-button 1 #'(lambda (arg)
688 (find-function-noselect arg)))
689 (pop-to-buffer (car location))
690 (goto-char (cdr location))))
692 (if need-close (princ ")"))
695 ;; Handle symbols aliased to other symbols.
696 (setq def (indirect-function def))
697 ;; If definition is a macro, find the function inside it.
698 (if (eq (car-safe def) 'macro)
699 (setq def (cdr def)))
700 (let ((arglist (cond ((byte-code-function-p def)
701 (car (append def nil)))
702 ((eq (car-safe def) 'lambda)
707 (princ (cons (if (symbolp function) function "anonymous")
708 (mapcar (lambda (arg)
709 (if (memq arg '(&optional &rest))
711 (intern (upcase (symbol-name arg)))))
714 (let ((doc (documentation function)))
718 (help-setup-xref (list #'describe-function function) interactive-p))
719 (princ "not documented")))))
721 (defun variable-at-point ()
722 "Return the bound variable symbol found around point.
723 Return 0 if there is no such symbol."
725 (let ((stab (syntax-table)))
728 (set-syntax-table emacs-lisp-mode-syntax-table)
729 (or (not (zerop (skip-syntax-backward "_w")))
730 (eq (char-syntax (following-char)) ?w)
731 (eq (char-syntax (following-char)) ?_)
733 (skip-chars-forward "'")
734 (let ((obj (read (current-buffer))))
735 (or (and (symbolp obj) (boundp obj) obj)
737 (set-syntax-table stab)))
740 (defun describe-variable (variable)
741 "Display the full documentation of VARIABLE (a symbol).
742 Returns the documentation as a string, also."
744 (let ((v (variable-at-point))
745 (enable-recursive-minibuffers t)
747 (setq val (completing-read (if (symbolp v)
748 (format "Describe variable (default %s): " v)
749 "Describe variable: ")
750 obarray 'boundp t nil nil
751 (if (symbolp v) (symbol-name v))))
752 (list (if (equal val "")
754 (if (symbolp variable)
756 (with-output-to-temp-buffer "*Help*"
758 (if (not (boundp variable))
763 (princ "'s value is ")
765 (pp (symbol-value variable))
767 (if (local-variable-p variable)
769 (princ (format "Local in buffer %s; " (buffer-name)))
770 (if (not (default-boundp variable))
771 (princ "globally void")
772 (princ "global value is ")
774 (pp (default-value variable)))
778 (set-buffer standard-output)
779 (if (> (count-lines (point-min) (point-max)) 10)
781 (goto-char (point-min))
785 (delete-region (point) (progn (end-of-line) (point)))
786 (insert "'s value is shown below.\n\n")
788 (insert "\n\nValue:"))))))
789 (princ "Documentation:")
791 (let ((doc (documentation-property variable 'variable-documentation)))
792 (princ (or doc "not documented as a variable.")))
793 (help-setup-xref (list #'describe-variable variable) (interactive-p))
795 ;; Make a link to customize if this variable can be customized.
796 ;; Note, it is not reliable to test only for a custom-type property
797 ;; because those are only present after the var's definition
799 (if (or (get variable 'custom-type) ; after defcustom
800 (get variable 'custom-loads) ; from loaddefs.el
801 (get variable 'standard-value)) ; from cus-start.el
802 (let ((customize-label "customize"))
805 (princ (concat "You can " customize-label " this variable."))
806 (with-current-buffer "*Help*"
809 (concat "\\(" customize-label "\\)") nil t)
810 (help-xref-button 1 #'(lambda (v)
811 (customize-variable v)) variable)
813 ;; Make a hyperlink to the library if appropriate. (Don't
814 ;; change the format of the buffer's initial line in case
815 ;; anything expects the current format.)
816 (let ((file-name (symbol-file variable)))
818 (princ "\n\nDefined in `")
821 (with-current-buffer "*Help*"
823 (re-search-backward "`\\([^`']+\\)'" nil t)
824 (help-xref-button 1 (lambda (arg)
826 (find-variable-noselect arg)))
827 (pop-to-buffer (car location))
828 (goto-char (cdr location))))
831 (print-help-return-message)
833 (set-buffer standard-output)
834 ;; Return the text we displayed.
836 (message "You did not specify a variable")))
838 (defun describe-bindings (&optional prefix buffer)
839 "Show a list of all defined keys, and their definitions.
840 We put that list in a buffer, and display the buffer.
842 The optional argument PREFIX, if non-nil, should be a key sequence;
843 then we display only bindings that start with that prefix.
844 The optional argument BUFFER specifies which buffer's bindings
845 to display (default, the current buffer)."
847 (or buffer (setq buffer (current-buffer)))
848 (with-current-buffer buffer
849 (describe-bindings-internal nil prefix))
850 (with-current-buffer "*Help*"
851 (help-setup-xref (list #'describe-bindings prefix buffer)
854 (defun where-is (definition &optional insert)
855 "Print message listing key sequences that invoke the command DEFINITION.
856 Argument is a command definition, usually a symbol with a function definition.
857 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
859 (let ((fn (function-called-at-point))
860 (enable-recursive-minibuffers t)
862 (setq val (completing-read (if fn
863 (format "Where is command (default %s): " fn)
864 "Where is command: ")
866 (list (if (equal val "")
868 current-prefix-arg)))
869 (let* ((keys (where-is-internal definition overriding-local-map nil nil))
870 (keys1 (mapconcat 'key-description keys ", "))
871 (standard-output (if insert (current-buffer) t)))
873 (if (> (length keys1) 0)
874 (princ (format "%s (%s)" keys1 definition))
875 (princ (format "M-x %s RET" definition)))
876 (if (> (length keys1) 0)
877 (princ (format "%s is on %s" definition keys1))
878 (princ (format "%s is not on any key" definition)))))
881 (defun locate-library (library &optional nosuffix path interactive-call)
882 "Show the precise file name of Emacs library LIBRARY.
883 This command searches the directories in `load-path' like `M-x load-library'
884 to find the file that `M-x load-library RET LIBRARY RET' would load.
885 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
886 to the specified name LIBRARY.
888 If the optional third arg PATH is specified, that list of directories
889 is used instead of `load-path'.
891 When called from a program, the file name is normaly returned as a
892 string. When run interactively, the argument INTERACTIVE-CALL is t,
893 and the file name is displayed in the echo area."
894 (interactive (list (read-string "Locate library: ")
903 (let ((try (expand-file-name (concat library suf) dir)))
904 (and (file-readable-p try)
905 (null (file-directory-p try))
908 (throw 'answer try)))))
912 ;;; load doesn't handle this yet.
913 ;;; (let ((basic '(".elc" ".el" ""))
914 ;;; (compressed '(".Z" ".gz" "")))
915 ;;; ;; If autocompression mode is on,
916 ;;; ;; consider all combinations of library suffixes
917 ;;; ;; and compression suffixes.
918 ;;; (if (rassq 'jka-compr-handler file-name-handler-alist)
920 ;;; (mapcar (lambda (compelt)
921 ;;; (mapcar (lambda (baselt)
922 ;;; (concat baselt compelt))
927 (or path load-path)))
928 (and interactive-call
930 (message "Library is file %s" result)
931 (message "No library %s in search path" library)))
935 ;;; Grokking cross-reference information in doc strings and
938 ;; This may have some scope for extension and the same or something
939 ;; similar should be done for widget doc strings, which currently use
940 ;; another mechanism.
942 (defcustom help-highlight-p t
943 "*If non-nil, `help-make-xrefs' highlight cross-references.
944 Under a window system it highlights them with face defined by
945 `help-highlight-face'."
950 (defcustom help-highlight-face 'underline
951 "Face used by `help-make-xrefs' to highlight cross-references.
952 Must be previously-defined."
957 (defvar help-back-label "[back]"
958 "Label to use by `help-make-xrefs' for the go-back reference.")
960 (defvar help-xref-symbol-regexp
961 (concat "\\(\\<\\(\\(variable\\|option\\)\\|"
962 "\\(function\\|command\\)\\|"
963 "\\(symbol\\)\\)\\s-+\\)?"
964 ;; Note starting with word-syntax character:
965 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'")
966 "Regexp matching doc string references to symbols.
968 The words preceding the quoted symbol can be used in doc strings to
969 distinguish references to variables, functions and symbols.")
971 (defvar help-xref-info-regexp
972 "\\<[Ii]nfo[ \t\n]+node[ \t\n]+`\\([^']+\\)'"
973 "Regexp matching doc string references to an Info node.")
975 (defun help-setup-xref (item interactive-p)
976 "Invoked from commands using the \"*Help*\" buffer to install some xref info.
978 ITEM is a (FUNCTION . ARGS) pair appropriate for recreating the help
979 buffer after following a reference. INTERACTIVE-P is non-nil if the
980 calling command was invoked interactively. In this case the stack of
981 items for help buffer \"back\" buttons is cleared."
983 (setq help-xref-stack nil))
984 (setq help-xref-stack-item item))
986 (defun help-make-xrefs (&optional buffer)
987 "Parse and hyperlink documentation cross-references in the given BUFFER.
989 Find cross-reference information in a buffer and, if
990 `help-highlight-p' is non-nil, highlight it with face defined by
991 `help-highlight-face'; activate such cross references for selection
992 with `help-follow'. Cross-references have the canonical form `...'
993 and the type of reference may be disambiguated by the preceding
994 word(s) used in `help-xref-symbol-regexp'.
996 A special reference `back' is made to return back through a stack of
997 help buffers. Variable `help-back-label' specifies the text for
1001 (set-buffer (or buffer (current-buffer)))
1002 (goto-char (point-min))
1003 ;; Skip the header-type info, though it might be useful to parse
1004 ;; it at some stage (e.g. "function in `library'").
1006 (let ((old-modified (buffer-modified-p)))
1007 (let ((stab (syntax-table))
1008 (case-fold-search t)
1009 (inhibit-read-only t))
1010 (set-syntax-table emacs-lisp-mode-syntax-table)
1011 ;; The following should probably be abstracted out.
1016 (while (re-search-forward help-xref-info-regexp nil t)
1017 (let ((data (match-string 1)))
1019 (unless (string-match "^([^)]+)" data)
1020 (setq data (concat "(emacs)" data))))
1021 (help-xref-button 1 #'info data))))
1024 (while (re-search-forward help-xref-symbol-regexp nil t)
1025 (let* ((data (match-string 6))
1026 (sym (intern-soft data)))
1029 ((match-string 3) ; `variable' &c
1030 (and (boundp sym) ; `variable' doesn't ensure
1031 ; it's actually bound
1032 (help-xref-button 6 #'describe-variable sym)))
1033 ((match-string 4) ; `function' &c
1034 (and (fboundp sym) ; similarly
1035 (help-xref-button 6 #'describe-function sym)))
1036 ((match-string 5)) ; nothing for symbol
1037 ((or (boundp sym) (fboundp sym))
1038 ;; We can't intuit whether to use the
1039 ;; variable or function doc -- supply both.
1040 (help-xref-button 6 #'help-xref-interned sym)))))))
1041 ;; An obvious case of a key substitution:
1043 (while (re-search-forward
1044 ;; Assume command name is only word characters
1045 ;; and dashes to get things like `use M-x foo.'.
1046 "\\<M-x\\s-+\\(\\sw\\(\\sw\\|-\\)+\\)" nil t)
1047 (let ((sym (intern-soft (match-string 1))))
1049 (help-xref-button 1 #'describe-function sym)))))
1050 ;; Look for commands in whole keymap substitutions:
1052 ;; Make sure to find the first keymap.
1053 (goto-char (point-min))
1054 ;; Find a header and the column at which the command
1055 ;; name will be found.
1056 (while (re-search-forward "^key +binding\n\\(-+ +\\)-+\n\n"
1058 (let ((col (- (match-end 1) (match-beginning 1))))
1060 ;; Ignore single blank lines in table, but not
1061 ;; double ones, which should terminate it.
1062 (and (not (looking-at "\n\\s-*\n"))
1064 (and (eolp) (forward-line))
1066 (skip-chars-backward "^\t\n")
1067 (if (and (>= (current-column) col)
1068 (looking-at "\\(\\sw\\|-\\)+$"))
1069 (let ((sym (intern-soft (match-string 0))))
1072 0 #'describe-function sym))))
1073 (zerop (forward-line)))))))))
1074 (set-syntax-table stab))
1075 ;; Make a back-reference in this buffer if appropriate.
1076 (when help-xref-stack
1077 (goto-char (point-max))
1079 (insert "\n\n" help-back-label))
1080 ;; Just to provide the match data:
1081 (looking-at (concat "\n\n\\(" (regexp-quote help-back-label) "\\)"))
1082 (help-xref-button 1 #'help-xref-go-back (current-buffer))))
1083 ;; View mode steals RET from us.
1084 (set (make-local-variable 'minor-mode-overriding-map-alist)
1085 (list (cons 'view-mode
1086 (let ((map (make-sparse-keymap)))
1087 (set-keymap-parent map view-mode-map)
1088 (define-key map "\r" 'help-follow)
1090 (set-buffer-modified-p old-modified))))
1092 (defun help-xref-button (match-number function data)
1093 "Make a hyperlink for cross-reference text previously matched.
1095 MATCH-NUMBER is the subexpression of interest in the last matched
1096 regexp. FUNCTION is a function to invoke when the button is
1097 activated, applied to DATA. DATA may be a single value or a list.
1098 See `help-make-xrefs'."
1099 ;; Don't mung properties we've added specially in some instances.
1100 (unless (get-text-property (match-beginning match-number) 'help-xref)
1101 (add-text-properties (match-beginning match-number)
1102 (match-end match-number)
1103 (list 'mouse-face 'highlight
1104 'help-xref (cons function
1108 (if help-highlight-p
1109 (put-text-property (match-beginning match-number)
1110 (match-end match-number)
1111 'face help-highlight-face))))
1114 ;; Additional functions for (re-)creating types of help buffers.
1115 (defun help-xref-interned (symbol)
1116 "Follow a hyperlink which appeared to be an arbitrary interned SYMBOL.
1118 Both variable and function documentation are extracted into a single
1120 (let ((fdoc (when (fboundp symbol) (describe-function symbol))))
1121 (when (or (boundp symbol) (not fdoc))
1122 (describe-variable symbol)
1123 ;; We now have a help buffer on the variable. Insert the function
1126 (with-current-buffer "*Help*"
1127 (goto-char (point-min))
1128 (let ((inhibit-read-only t))
1129 (insert fdoc "\n\n" (symbol-name symbol) " is also a variable.\n\n"))
1130 (help-setup-xref (list #'help-xref-interned symbol) nil))))))
1132 (defun help-xref-mode (buffer)
1133 "Do a `describe-mode' for the specified BUFFER."
1138 ;;; Navigation/hyperlinking with xrefs
1140 (defun help-follow-mouse (click)
1141 "Follow the cross-reference that you click on."
1143 (let* ((start (event-start click))
1144 (window (car start))
1145 (pos (car (cdr start))))
1146 (with-current-buffer (window-buffer window)
1147 (help-follow pos))))
1149 (defun help-xref-go-back (buffer)
1150 "From BUFFER, go back to previous help buffer text using `help-xref-stack'."
1151 (let (item position method args)
1152 (with-current-buffer buffer
1153 (when help-xref-stack
1154 (setq help-xref-stack (cdr help-xref-stack)) ; due to help-follow
1155 (setq item (car help-xref-stack)
1159 (setq help-xref-stack (cdr help-xref-stack))))
1161 (goto-char position)))
1163 (defun help-go-back ()
1164 "Invoke the [back] button (if any) in the Help mode buffer."
1166 (help-follow (1- (point-max))))
1168 (defun help-follow (&optional pos)
1169 "Follow cross-reference at POS, defaulting to point.
1171 For the cross-reference format, see `help-make-xrefs'."
1176 (or (and (not (= pos (point-max)))
1177 (get-text-property pos 'help-xref))
1178 (and (not (= pos (point-min)))
1179 (get-text-property (1- pos) 'help-xref))
1180 ;; check if the symbol under point is a function or variable
1184 (goto-char pos) (skip-syntax-backward "w_")
1185 (buffer-substring (point)
1186 (progn (skip-syntax-forward "w_")
1188 (when (or (boundp sym) (fboundp sym))
1189 (list #'help-xref-interned sym)))))
1190 (method (car help-data))
1191 (args (cdr help-data)))
1193 (setq help-xref-stack (cons (cons (point) help-xref-stack-item)
1195 (setq help-xref-stack-item nil)
1196 ;; There is a reference at point. Follow it.
1197 (apply method args))))
1199 ;; For tabbing through buffer.
1200 (defun help-next-ref ()
1201 "Find the next help cross-reference in the buffer."
1205 (if (get-text-property (point) 'help-xref) ; move off reference
1206 (goto-char (or (next-single-property-change (point) 'help-xref)
1208 (cond ((setq pos (next-single-property-change (point) 'help-xref))
1209 (if pos (goto-char pos)))
1211 (message "No cross references in the buffer.")
1214 (goto-char (point-min)))))))
1216 (defun help-previous-ref ()
1217 "Find the previous help cross-reference in the buffer."
1221 (if (get-text-property (point) 'help-xref) ; move off reference
1222 (goto-char (or (previous-single-property-change (point) 'help-xref)
1224 (cond ((setq pos (previous-single-property-change (point) 'help-xref))
1225 (if pos (goto-char pos)))
1227 (message "No cross references in the buffer.")
1230 (goto-char (point-max)))))))
1233 ;;; Automatic resizing of temporary buffers.
1235 (defcustom temp-buffer-resize-mode nil
1236 "Non-nil means resize windows displaying temporary buffers.
1237 This makes the window the right height for its contents, but never
1238 more than `temp-buffer-max-height' nor less than `window-min-height'.
1239 This applies to `help', `apropos' and `completion' buffers, and some others.
1241 Setting this variable directly does not take effect;
1242 use either \\[customize] or the function `temp-buffer-resize-mode'."
1243 :get (lambda (symbol)
1244 (and (memq 'resize-temp-buffer-window temp-buffer-show-hook) t))
1245 :set (lambda (symbol value)
1246 (temp-buffer-resize-mode (if value 1 -1)))
1247 :initialize 'custom-initialize-default
1252 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
1253 "*Maximum height of a window displaying a temporary buffer.
1254 This is the maximum height (in text lines) which `resize-temp-buffer-window'
1255 will give to a window displaying a temporary buffer.
1256 It can also be a function which will be called with the object corresponding
1257 to the buffer to be displayed as argument and should return an integer
1259 :type '(choice integer function)
1263 (defun temp-buffer-resize-mode (arg)
1264 "Toggle the mode which that makes windows smaller for temporary buffers.
1265 With prefix argument ARG, turn the resizing of windows displaying temporary
1266 buffers on if ARG is positive or off otherwise.
1267 See the documentation of the variable `temp-buffer-resize-mode' for
1272 (not (memq 'resize-temp-buffer-window temp-buffer-show-hook))
1273 (> (prefix-numeric-value arg) 0))))
1276 ;; `help-mode-maybe' may add a `back' button and thus increase the
1277 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
1278 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
1279 (setq temp-buffer-resize-mode t))
1280 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)
1281 (setq temp-buffer-resize-mode nil))))
1283 (defun resize-temp-buffer-window ()
1284 "Resize the current window to fit its contents.
1285 Will not make it higher than `temp-buffer-max-height' nor smaller than
1286 `window-min-height'. Do nothing if it is the only window on its frame, if it
1287 is not as wide as the frame or if some of the window's contents are scrolled
1289 (unless (or (one-window-p 'nomini)
1290 (not (pos-visible-in-window-p (point-min)))
1291 (/= (frame-width) (window-width)))
1292 (let* ((max-height (if (functionp temp-buffer-max-height)
1293 (funcall temp-buffer-max-height (current-buffer))
1294 temp-buffer-max-height))
1295 (win-height (1- (window-height)))
1296 (min-height (1- window-min-height))
1297 (text-height (window-buffer-height(selected-window)))
1298 (new-height (max (min text-height max-height) min-height)))
1299 (enlarge-window (- new-height win-height)))))
1301 ;;; help.el ends here