1 ;;; help.el --- help commands for Emacs
4 ;; Last-Modified: 30 Jun 1992
5 ;; Keywords: help, internal
7 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
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)
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
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 (defvar help-map
(make-sparse-keymap)
28 "Keymap for characters following the Help key.")
30 (define-key global-map
"\C-h" 'help-command
)
31 (fset 'help-command help-map
)
33 (define-key help-map
"\C-h" 'help-for-help
)
34 (define-key help-map
"?" 'help-for-help
)
36 (define-key help-map
"\C-c" 'describe-copying
)
37 (define-key help-map
"\C-d" 'describe-distribution
)
38 (define-key help-map
"\C-w" 'describe-no-warranty
)
39 (define-key help-map
"a" 'command-apropos
)
41 (define-key help-map
"b" 'describe-bindings
)
43 (define-key help-map
"c" 'describe-key-briefly
)
44 (define-key help-map
"k" 'describe-key
)
46 (define-key help-map
"d" 'describe-function
)
47 (define-key help-map
"f" 'describe-function
)
49 (define-key help-map
"i" 'info
)
50 (define-key help-map
"\C-f" 'Info-goto-emacs-command-node
)
51 (define-key help-map
"\C-k" 'Info-goto-emacs-key-command-node
)
53 (define-key help-map
"l" 'view-lossage
)
55 (define-key help-map
"m" 'describe-mode
)
57 (define-key help-map
"\C-n" 'view-emacs-news
)
58 (define-key help-map
"n" 'view-emacs-news
)
60 (define-key help-map
"s" 'describe-syntax
)
62 (define-key help-map
"t" 'help-with-tutorial
)
64 (define-key help-map
"w" 'where-is
)
66 (define-key help-map
"v" 'describe-variable
)
68 (defun help-with-tutorial ()
69 "Select the Emacs learn-by-doing tutorial."
71 (let ((file (expand-file-name "~/TUTORIAL")))
72 (delete-other-windows)
73 (if (get-file-buffer file
)
74 (switch-to-buffer (get-file-buffer file
))
75 (switch-to-buffer (create-file-buffer file
))
76 (setq buffer-file-name file
)
77 (setq default-directory
(expand-file-name "~/"))
78 (setq buffer-auto-save-file-name nil
)
79 (insert-file-contents (expand-file-name "TUTORIAL" data-directory
))
80 (goto-char (point-min))
81 (search-forward "\n<<")
83 (delete-region (point) (progn (end-of-line) (point)))
84 (newline (- (window-height (selected-window))
85 (count-lines (point-min) (point))
87 (goto-char (point-min))
88 (set-buffer-modified-p nil
))))
90 (defun describe-key-briefly (key)
91 "Print the name of the function KEY invokes. KEY is a string."
92 (interactive "kDescribe key briefly: ")
93 (let ((defn (key-binding key
)))
94 (if (or (null defn
) (integerp defn
))
95 (message "%s is undefined" (key-description key
))
96 (message "%s runs the command %s"
98 (if (symbolp defn
) defn
(prin1-to-string defn
))))))
100 (defun print-help-return-message (&optional function
)
101 "Display or return message saying how to restore windows after help command.
102 Computes a message and applies the optional argument FUNCTION to it.
103 If FUNCTION is nil, applies `message' to it, thus printing it."
104 (and (not (get-buffer-window standard-output
))
105 (funcall (or function
'message
)
107 (substitute-command-keys
110 "Type \\[delete-other-windows] to remove help window."
111 "Type \\[switch-to-buffer] RET to remove help window.")
112 "Type \\[switch-to-buffer-other-window] RET to restore the other window."))
113 (substitute-command-keys
114 " \\[scroll-other-window] to scroll the help.")))))
116 (defun describe-key (key)
117 "Display documentation of the function invoked by KEY. KEY is a string."
118 (interactive "kDescribe key: ")
119 (let ((defn (key-binding key
)))
120 (if (or (null defn
) (integerp defn
))
121 (message "%s is undefined" (key-description key
))
122 (with-output-to-temp-buffer "*Help*"
125 (if (documentation defn
)
126 (princ (documentation defn
))
127 (princ "not documented"))
128 (print-help-return-message)))))
130 (defun describe-mode (&optional minor
)
131 "Display documentation of current major mode.
132 If optional MINOR is non-nil (or prefix argument is given if interactive),
133 display documentation of active minor modes as well.
134 For this to work correctly for a minor mode, the mode's indicator variable
135 (listed in `minor-mode-alist') must also be a function whose documentation
136 describes the minor mode."
138 (with-output-to-temp-buffer "*Help*"
141 (princ (documentation major-mode
))
142 (let ((minor-modes minor-mode-alist
)
143 (locals (buffer-local-variables)))
145 (let* ((minor-mode (car (car minor-modes
)))
146 (indicator (car (cdr (car minor-modes
))))
147 (local-binding (assq minor-mode locals
)))
148 ;; Document a minor mode if it is listed in minor-mode-alist,
149 ;; bound locally in this buffer, non-nil, and has a function
151 (if (and local-binding
153 (fboundp minor-mode
))
155 (princ (format "\n\n\n%s minor mode (indicator%s):\n"
156 minor-mode indicator
))
157 (princ (documentation minor-mode
)))))
158 (setq minor-modes
(cdr minor-modes
))))
159 (print-help-return-message)))
161 ;; So keyboard macro definitions are documented correctly
162 (fset 'defining-kbd-macro
(symbol-function 'start-kbd-macro
))
164 (defun describe-distribution ()
165 "Display info on how to obtain the latest version of GNU Emacs."
168 (expand-file-name "DISTRIB" data-directory
)))
170 (defun describe-copying ()
171 "Display info on how you may redistribute copies of GNU Emacs."
174 (expand-file-name "COPYING" data-directory
))
175 (goto-char (point-min)))
177 (defun describe-no-warranty ()
178 "Display info on all the kinds of warranty Emacs does NOT have."
181 (let (case-fold-search)
182 (search-forward "NO WARRANTY")
185 (defun view-emacs-news ()
186 "Display info on recent changes to Emacs."
188 (find-file-read-only (expand-file-name "NEWS" data-directory
)))
190 (defun view-lossage ()
191 "Display last 100 input keystrokes."
193 (with-output-to-temp-buffer "*Help*"
194 (princ (key-description (recent-keys)))
196 (set-buffer standard-output
)
197 (goto-char (point-min))
198 (while (progn (move-to-column 50) (not (eobp)))
199 (search-forward " " nil t
)
201 (print-help-return-message)))
203 (defun help-for-help ()
204 "You have typed C-h, the help character. Type a Help option:
206 A command-apropos. Give a substring, and see a list of commands
207 (functions interactively callable) that contain
208 that substring. See also the apropos command.
209 B describe-bindings. Display table of all key bindings.
210 C describe-key-briefly. Type a command key sequence;
211 it prints the function name that sequence runs.
212 F describe-function. Type a function name and get documentation of it.
213 I info. The info documentation reader.
214 K describe-key. Type a command key sequence;
215 it displays the full documentation.
216 L view-lossage. Shows last 100 characters you typed.
217 M describe-mode. Print documentation of current major mode,
218 which describes the commands peculiar to it.
219 N view-emacs-news. Shows emacs news file.
220 S describe-syntax. Display contents of syntax table, plus explanations
221 T help-with-tutorial. Select the Emacs learn-by-doing tutorial.
222 V describe-variable. Type name of a variable;
223 it displays the variable's documentation and value.
224 W where-is. Type command name; it prints which keystrokes
226 C-c print Emacs copying permission (General Public License).
227 C-d print Emacs ordering information.
228 C-n print news of recent Emacs changes.
229 C-w print information on absence of warranty for GNU Emacs."
232 "A B C F I K L M N S T V W C-c C-d C-n C-w. Type C-h again for more help: ")
233 (let ((char (read-char)))
234 (if (or (= char ?\C-h
) (= char ??
))
235 (save-window-excursion
236 (switch-to-buffer "*Help*")
237 (delete-other-windows)
239 (insert (documentation 'help-for-help
))
240 (goto-char (point-min))
241 (while (memq char
'(?\C-h ?? ?\C-v ?\ ?
\177 ?\M-v
))
242 (if (memq char
'(?\C-v ?\
))
244 (if (memq char
'(?
\177 ?\M-v
))
246 (message "A B C F I K L M N S T V W C-c C-d C-n C-w%s: "
247 (if (pos-visible-in-window-p (point-max))
248 "" " or Space to scroll"))
249 (let ((cursor-in-echo-area t
))
250 (setq char
(read-char))))))
251 (let ((defn (cdr (assq (downcase char
) (cdr help-map
)))))
252 (if defn
(call-interactively defn
) (ding)))))
255 ;; Return a function which is called by the list containing point.
256 ;; If that gives no function, return a function whose name is around point.
257 ;; If that doesn't give a function, return nil.
258 (defun function-called-at-point ()
259 (or (condition-case ()
262 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
266 (setq obj
(read (current-buffer)))
267 (and (symbolp obj
) (fboundp obj
) obj
))))
272 (skip-chars-forward "'")
273 (let ((obj (read (current-buffer))))
274 (and (symbolp obj
) (fboundp obj
) obj
)))
277 (defun describe-function (function)
278 "Display the full documentation of FUNCTION (a symbol)."
280 (let ((fn (function-called-at-point))
281 (enable-recursive-minibuffers t
)
283 (setq val
(completing-read (if fn
284 (format "Describe function (default %s): " fn
)
285 "Describe function: ")
287 (list (if (equal val
"")
289 (with-output-to-temp-buffer "*Help*"
292 (let* ((def (symbol-function function
))
293 (beg (if (commandp def
) "an interactive " "a ")))
294 (princ (cond ((stringp def
) "a keyboard macro.")
296 (concat beg
"built-in function."))
297 ((compiled-function-p def
)
298 (concat beg
"compiled Lisp function."))
300 (format "alias for `%s'." def
))
301 ((eq (car-safe def
) 'lambda
)
302 (concat beg
"Lisp function."))
303 ((eq (car-safe def
) 'macro
)
305 ((eq (car-safe def
) 'mocklisp
)
306 "a mocklisp function.")
307 ((eq (car-safe def
) 'autoload
)
308 (format "%sLisp %s to autoload from `%s'."
310 (if (nth 4 def
) "macro" "function")
314 (if (documentation function
)
315 (princ (documentation function
))
316 (princ "not documented"))
317 (print-help-return-message)
318 ;; Return the text we displayed.
319 (save-excursion (set-buffer standard-output
) (buffer-string))))
321 (defun variable-at-point ()
325 (skip-chars-forward "'")
326 (let ((obj (read (current-buffer))))
327 (and (symbolp obj
) (boundp obj
) obj
)))
330 (defun describe-variable (variable)
331 "Display the full documentation of VARIABLE (a symbol).
332 Returns the documentation as a string, also."
334 (let ((v (variable-at-point))
335 (enable-recursive-minibuffers t
)
337 (setq val
(completing-read (if v
338 (format "Describe variable (default %s): " v
)
339 "Describe variable: ")
341 (list (if (equal val
"")
343 (with-output-to-temp-buffer "*Help*"
345 (princ "'s value is ")
346 (if (not (boundp variable
))
348 (prin1 (symbol-value variable
)))
350 (princ "Documentation:")
352 (let ((doc (documentation-property variable
'variable-documentation
)))
354 (princ (substitute-command-keys doc
))
355 (princ "not documented as a variable.")))
356 (print-help-return-message)
357 ;; Return the text we displayed.
358 (save-excursion (set-buffer standard-output
) (buffer-string))))
360 (defun command-apropos (string)
361 "Like apropos but lists only symbols that are names of commands
362 \(interactively callable functions). Argument REGEXP is a regular expression
363 that is matched against command symbol names. Returns list of symbols and
364 documentation found."
365 (interactive "sCommand apropos (regexp): ")
367 (let ((standard-output (get-buffer-create "*Help*")))
368 (print-help-return-message 'identity
))))
369 (apropos string
'commandp
)
370 (and message
(message message
))))
372 (defun locate-library (library &optional nosuffix
)
373 "Show the full path name of Emacs library LIBRARY.
374 This command searches the directories in `load-path' like `M-x load-library'
375 to find the file that `M-x load-library RET LIBRARY RET' would load.
376 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
377 to the specified name LIBRARY (a la calling `load' instead of `load-library')."
378 (interactive "sLocate library: ")
384 (let ((try (expand-file-name (concat library suf
) dir
)))
385 (and (file-readable-p try
)
386 (null (file-directory-p try
))
388 (message "Library is file %s" try
)
389 (throw 'answer try
)))))
390 (if nosuffix
'("") '(".elc" ".el" ""))))
392 (message "No library %s in search path" library
)
395 ;;; help.el ends here