1 ;;; help.el --- help commands for Emacs
3 ;; Copyright (C) 1985, 1986 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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 (require 'help-screen
)
28 (defvar help-map
(make-sparse-keymap)
29 "Keymap for characters following the Help key.")
31 (define-key global-map
(char-to-string help-char
) 'help-command
)
32 (fset 'help-command help-map
)
34 (define-key help-map
(char-to-string help-char
) 'help-for-help
)
35 (define-key help-map
"?" 'help-for-help
)
37 (define-key help-map
"\C-c" 'describe-copying
)
38 (define-key help-map
"\C-d" 'describe-distribution
)
39 (define-key help-map
"\C-w" 'describe-no-warranty
)
40 (define-key help-map
"a" 'command-apropos
)
42 (define-key help-map
"b" 'describe-bindings
)
44 (define-key help-map
"c" 'describe-key-briefly
)
45 (define-key help-map
"k" 'describe-key
)
47 (define-key help-map
"d" 'describe-function
)
48 (define-key help-map
"f" 'describe-function
)
50 (define-key help-map
"i" 'info
)
51 (define-key help-map
"\C-f" 'Info-goto-emacs-command-node
)
52 (define-key help-map
"\C-k" 'Info-goto-emacs-key-command-node
)
54 (define-key help-map
"l" 'view-lossage
)
56 (define-key help-map
"m" 'describe-mode
)
58 (define-key help-map
"\C-n" 'view-emacs-news
)
59 (define-key help-map
"n" 'view-emacs-news
)
61 (define-key help-map
"p" 'finder-by-keyword
)
62 (autoload 'finder-by-keyword
"finder.el")
64 (define-key help-map
"s" 'describe-syntax
)
66 (define-key help-map
"t" 'help-with-tutorial
)
68 (define-key help-map
"w" 'where-is
)
70 (define-key help-map
"v" 'describe-variable
)
72 (defun help-with-tutorial ()
73 "Select the Emacs learn-by-doing tutorial."
75 (let ((file (expand-file-name "~/TUTORIAL")))
76 (delete-other-windows)
77 (if (get-file-buffer file
)
78 (switch-to-buffer (get-file-buffer file
))
79 (switch-to-buffer (create-file-buffer file
))
80 (setq buffer-file-name file
)
81 (setq default-directory
(expand-file-name "~/"))
82 (setq buffer-auto-save-file-name nil
)
83 (insert-file-contents (expand-file-name "TUTORIAL" data-directory
))
84 (goto-char (point-min))
85 (search-forward "\n<<")
87 (delete-region (point) (progn (end-of-line) (point)))
88 (newline (- (window-height (selected-window))
89 (count-lines (point-min) (point))
91 (goto-char (point-min))
92 (set-buffer-modified-p nil
))))
94 (defun describe-key-briefly (key)
95 "Print the name of the function KEY invokes. KEY is a string."
96 (interactive "kDescribe key briefly: ")
97 (let ((defn (key-binding key
)))
98 (if (or (null defn
) (integerp defn
))
99 (message "%s is undefined" (key-description key
))
100 (message "%s runs the command %s"
101 (key-description key
)
102 (if (symbolp defn
) defn
(prin1-to-string defn
))))))
104 (defun print-help-return-message (&optional function
)
105 "Display or return message saying how to restore windows after help command.
106 Computes a message and applies the optional argument FUNCTION to it.
107 If FUNCTION is nil, applies `message' to it, thus printing it."
108 (and (not (get-buffer-window standard-output
))
109 (funcall (or function
'message
)
111 (substitute-command-keys
114 "Type \\[delete-other-windows] to remove help window."
115 "Type \\[switch-to-buffer] RET to remove help window.")
116 "Type \\[switch-to-buffer-other-window] RET to restore the other window."))
117 (substitute-command-keys
118 " \\[scroll-other-window] to scroll the help.")))))
120 (defun describe-key (key)
121 "Display documentation of the function invoked by KEY. KEY is a string."
122 (interactive "kDescribe key: ")
123 (let ((defn (key-binding key
)))
124 (if (or (null defn
) (integerp defn
))
125 (message "%s is undefined" (key-description key
))
126 (with-output-to-temp-buffer "*Help*"
129 (if (documentation defn
)
130 (princ (documentation defn
))
131 (princ "not documented"))
132 (print-help-return-message)))))
134 (defun describe-mode (&optional minor
)
135 "Display documentation of current major mode.
136 If optional MINOR is non-nil (or prefix argument is given if interactive),
137 display documentation of active minor modes as well.
138 For this to work correctly for a minor mode, the mode's indicator variable
139 (listed in `minor-mode-alist') must also be a function whose documentation
140 describes the minor mode."
142 (with-output-to-temp-buffer "*Help*"
145 (princ (documentation major-mode
))
146 (let ((minor-modes minor-mode-alist
)
147 (locals (buffer-local-variables)))
149 (let* ((minor-mode (car (car minor-modes
)))
150 (indicator (car (cdr (car minor-modes
))))
151 (local-binding (assq minor-mode locals
)))
152 ;; Document a minor mode if it is listed in minor-mode-alist,
153 ;; bound locally in this buffer, non-nil, and has a function
155 (if (and local-binding
157 (fboundp minor-mode
))
159 (princ (format "\n\n\n%s minor mode (indicator%s):\n"
160 minor-mode indicator
))
161 (princ (documentation minor-mode
)))))
162 (setq minor-modes
(cdr minor-modes
))))
163 (print-help-return-message)))
165 ;; So keyboard macro definitions are documented correctly
166 (fset 'defining-kbd-macro
(symbol-function 'start-kbd-macro
))
168 (defun describe-distribution ()
169 "Display info on how to obtain the latest version of GNU Emacs."
172 (expand-file-name "DISTRIB" data-directory
)))
174 (defun describe-copying ()
175 "Display info on how you may redistribute copies of GNU Emacs."
178 (expand-file-name "COPYING" data-directory
))
179 (goto-char (point-min)))
181 (defun describe-no-warranty ()
182 "Display info on all the kinds of warranty Emacs does NOT have."
185 (let (case-fold-search)
186 (search-forward "NO WARRANTY")
189 (defun view-emacs-news ()
190 "Display info on recent changes to Emacs."
192 (find-file-read-only (expand-file-name "NEWS" data-directory
)))
194 (defun view-lossage ()
195 "Display last 100 input keystrokes."
197 (with-output-to-temp-buffer "*Help*"
198 (princ (key-description (recent-keys)))
200 (set-buffer standard-output
)
201 (goto-char (point-min))
202 (while (progn (move-to-column 50) (not (eobp)))
203 (search-forward " " nil t
)
205 (print-help-return-message)))
207 (make-help-screen help-for-help
208 "A B C F I K L M N P S T V W C-c C-d C-n C-w. Type \\[help-for-help] again for more help: "
209 "You have typed \\[help-for-help], the help character. Type a Help option:
211 A command-apropos. Give a substring, and see a list of commands
212 (functions interactively callable) that contain
213 that substring. See also the apropos command.
214 B describe-bindings. Display table of all key bindings.
215 C describe-key-briefly. Type a command key sequence;
216 it prints the function name that sequence runs.
217 F describe-function. Type a function name and get documentation of it.
218 I info. The info documentation reader.
219 K describe-key. Type a command key sequence;
220 it displays the full documentation.
221 L view-lossage. Shows last 100 characters you typed.
222 M describe-mode. Print documentation of current major mode,
223 which describes the commands peculiar to it.
224 N view-emacs-news. Shows emacs news file.
225 P finder-by-keyword. Find packages matching a given topic keyword.
226 S describe-syntax. Display contents of syntax table, plus explanations
227 T help-with-tutorial. Select the Emacs learn-by-doing tutorial.
228 V describe-variable. Type name of a variable;
229 it displays the variable's documentation and value.
230 W where-is. Type command name; it prints which keystrokes
232 C-c print Emacs copying permission (General Public License).
233 C-d print Emacs ordering information.
234 C-n print news of recent Emacs changes.
235 C-w print information on absence of warranty for GNU Emacs."
238 ;; Return a function which is called by the list containing point.
239 ;; If that gives no function, return a function whose name is around point.
240 ;; If that doesn't give a function, return nil.
241 (defun function-called-at-point ()
242 (or (condition-case ()
245 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
249 (setq obj
(read (current-buffer)))
250 (and (symbolp obj
) (fboundp obj
) obj
))))
255 (skip-chars-forward "'")
256 (let ((obj (read (current-buffer))))
257 (and (symbolp obj
) (fboundp obj
) obj
)))
260 (defun describe-function (function)
261 "Display the full documentation of FUNCTION (a symbol)."
263 (let ((fn (function-called-at-point))
264 (enable-recursive-minibuffers t
)
266 (setq val
(completing-read (if fn
267 (format "Describe function (default %s): " fn
)
268 "Describe function: ")
270 (list (if (equal val
"")
272 (with-output-to-temp-buffer "*Help*"
275 (let* ((def (symbol-function function
))
276 (beg (if (commandp def
) "an interactive " "a ")))
277 (princ (cond ((stringp def
) "a keyboard macro.")
279 (concat beg
"built-in function."))
280 ((byte-code-function-p def
)
281 (concat beg
"compiled Lisp function."))
283 (format "alias for `%s'." def
))
284 ((eq (car-safe def
) 'lambda
)
285 (concat beg
"Lisp function."))
286 ((eq (car-safe def
) 'macro
)
288 ((eq (car-safe def
) 'mocklisp
)
289 "a mocklisp function.")
290 ((eq (car-safe def
) 'autoload
)
291 (format "%s autoloaded Lisp %s."
292 (if (commandp def
) "an interactive" "an")
293 (if (nth 4 def
) "macro" "function")
294 ;;; Including the file name made this line too long.
299 (if (documentation function
)
300 (princ (documentation function
))
301 (princ "not documented"))
302 (cond ((byte-code-function-p def
)
304 (set-buffer standard-output
)
305 (or (eq (char-after (1- (point-max))) ?
\n)
308 (princ (car (append def nil
))))
309 ((eq (car-safe def
) 'lambda
)
311 (set-buffer standard-output
)
312 (or (eq (char-after (1- (point-max))) ?
\n)
315 (princ (nth 1 def
)))))
316 (print-help-return-message)
317 ;; Return the text we displayed.
318 (save-excursion (set-buffer standard-output
) (buffer-string))))
320 (defun variable-at-point ()
324 (skip-chars-forward "'")
325 (let ((obj (read (current-buffer))))
326 (and (symbolp obj
) (boundp obj
) obj
)))
329 (defun describe-variable (variable)
330 "Display the full documentation of VARIABLE (a symbol).
331 Returns the documentation as a string, also."
333 (let ((v (variable-at-point))
334 (enable-recursive-minibuffers t
)
336 (setq val
(completing-read (if v
337 (format "Describe variable (default %s): " v
)
338 "Describe variable: ")
340 (list (if (equal val
"")
342 (with-output-to-temp-buffer "*Help*"
344 (princ "'s value is ")
345 (if (not (boundp variable
))
347 (prin1 (symbol-value variable
)))
349 (princ "Documentation:")
351 (let ((doc (documentation-property variable
'variable-documentation
)))
353 (princ (substitute-command-keys doc
))
354 (princ "not documented as a variable.")))
355 (print-help-return-message)
356 ;; Return the text we displayed.
357 (save-excursion (set-buffer standard-output
) (buffer-string))))
359 (defun command-apropos (string)
360 "Like apropos but lists only symbols that are names of commands
361 \(interactively callable functions). Argument REGEXP is a regular expression
362 that is matched against command symbol names. Returns list of symbols and
363 documentation found."
364 (interactive "sCommand apropos (regexp): ")
366 (let ((standard-output (get-buffer-create "*Help*")))
367 (print-help-return-message 'identity
))))
368 (if (apropos string t
'commandp
)
369 (and message
(message message
)))))
371 (defun locate-library (library &optional nosuffix
)
372 "Show the full path name of Emacs library LIBRARY.
373 This command searches the directories in `load-path' like `M-x load-library'
374 to find the file that `M-x load-library RET LIBRARY RET' would load.
375 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
376 to the specified name LIBRARY (a la calling `load' instead of `load-library')."
377 (interactive "sLocate library: ")
383 (let ((try (expand-file-name (concat library suf
) dir
)))
384 (and (file-readable-p try
)
385 (null (file-directory-p try
))
387 (message "Library is file %s" try
)
388 (throw 'answer try
)))))
389 (if nosuffix
'("") '(".elc" ".el" ""))))
391 (message "No library %s in search path" library
)
394 ;;; help.el ends here