1 ;; Help commands for Emacs
2 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
4 ;; This file is part of GNU Emacs.
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation; either version 1, or (at your option)
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;; GNU General Public License for more details.
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21 (defvar help-map
(make-sparse-keymap)
22 "Keymap for characters following the Help key.")
24 (define-key global-map
"\C-h" 'help-command
)
25 (fset 'help-command help-map
)
27 (define-key help-map
"\C-h" 'help-for-help
)
28 (define-key help-map
"?" 'help-for-help
)
30 (define-key help-map
"\C-c" 'describe-copying
)
31 (define-key help-map
"\C-d" 'describe-distribution
)
32 (define-key help-map
"\C-w" 'describe-no-warranty
)
33 (define-key help-map
"a" 'command-apropos
)
35 (define-key help-map
"b" 'describe-bindings
)
37 (define-key help-map
"c" 'describe-key-briefly
)
38 (define-key help-map
"k" 'describe-key
)
40 (define-key help-map
"d" 'describe-function
)
41 (define-key help-map
"f" 'describe-function
)
43 (define-key help-map
"i" 'info
)
45 (define-key help-map
"l" 'view-lossage
)
47 (define-key help-map
"m" 'describe-mode
)
49 (define-key help-map
"\C-n" 'view-emacs-news
)
50 (define-key help-map
"n" 'view-emacs-news
)
52 (define-key help-map
"s" 'describe-syntax
)
54 (define-key help-map
"t" 'help-with-tutorial
)
56 (define-key help-map
"w" 'where-is
)
58 (define-key help-map
"v" 'describe-variable
)
60 (defun help-with-tutorial ()
61 "Select the Emacs learn-by-doing tutorial."
63 (let ((file (expand-file-name "~/TUTORIAL")))
64 (delete-other-windows)
65 (if (get-file-buffer file
)
66 (switch-to-buffer (get-file-buffer file
))
67 (switch-to-buffer (create-file-buffer file
))
68 (setq buffer-file-name file
)
69 (setq default-directory
(expand-file-name "~/"))
70 (setq auto-save-file-name nil
)
71 (insert-file-contents (expand-file-name "TUTORIAL" data-directory
))
72 (goto-char (point-min))
73 (search-forward "\n<<")
75 (delete-region (point) (progn (end-of-line) (point)))
76 (newline (- (window-height (selected-window))
77 (count-lines (point-min) (point))
79 (goto-char (point-min))
80 (set-buffer-modified-p nil
))))
82 (defun describe-key-briefly (key)
83 "Print the name of the function KEY invokes. KEY is a string."
84 (interactive "kDescribe key briefly: ")
85 (let ((defn (key-binding key
)))
86 (if (or (null defn
) (integerp defn
))
87 (message "%s is undefined" (key-description key
))
88 (message "%s runs the command %s"
90 (if (symbolp defn
) defn
(prin1-to-string defn
))))))
92 (defun print-help-return-message (&optional function
)
93 "Display or return message saying how to restore windows after help command.
94 Computes a message and applies the optional argument FUNCTION to it.
95 If FUNCTION is nil, applies `message' to it, thus printing it."
96 (and (not (get-buffer-window standard-output
))
97 (funcall (or function
'message
)
99 (substitute-command-keys
102 "Type \\[delete-other-windows] to remove help window."
103 "Type \\[switch-to-buffer] RET to remove help window.")
104 "Type \\[switch-to-buffer-other-window] RET to restore the other window."))
105 (substitute-command-keys
106 " \\[scroll-other-window] to scroll the help.")))))
108 (defun describe-key (key)
109 "Display documentation of the function invoked by KEY. KEY is a string."
110 (interactive "kDescribe key: ")
111 (let ((defn (key-binding key
)))
112 (if (or (null defn
) (integerp defn
))
113 (message "%s is undefined" (key-description key
))
114 (with-output-to-temp-buffer "*Help*"
117 (if (documentation defn
)
118 (princ (documentation defn
))
119 (princ "not documented"))
120 (print-help-return-message)))))
122 (defun describe-mode (&optional minor
)
123 "Display documentation of current major mode.
124 If optional MINOR is non-nil (or prefix argument is given if interactive),
125 display documentation of active minor modes as well.
126 For this to work correctly for a minor mode, the mode's indicator variable
127 (listed in `minor-mode-alist') must also be a function whose documentation
128 describes the minor mode."
130 (with-output-to-temp-buffer "*Help*"
133 (princ (documentation major-mode
))
134 (let ((minor-modes minor-mode-alist
)
135 (locals (buffer-local-variables)))
137 (let* ((minor-mode (car (car minor-modes
)))
138 (indicator (car (cdr (car minor-modes
))))
139 (local-binding (assq minor-mode locals
)))
140 ;; Document a minor mode if it is listed in minor-mode-alist,
141 ;; bound locally in this buffer, non-nil, and has a function
143 (if (and local-binding
145 (fboundp minor-mode
))
147 (princ (format "\n\n\n%s minor mode (indicator%s):\n"
148 minor-mode indicator
))
149 (princ (documentation minor-mode
)))))
150 (setq minor-modes
(cdr minor-modes
))))
151 (print-help-return-message)))
153 ;; So keyboard macro definitions are documented correctly
154 (fset 'defining-kbd-macro
(symbol-function 'start-kbd-macro
))
156 (defun describe-distribution ()
157 "Display info on how to obtain the latest version of GNU Emacs."
160 (expand-file-name "DISTRIB" data-directory
)))
162 (defun describe-copying ()
163 "Display info on how you may redistribute copies of GNU Emacs."
166 (expand-file-name "COPYING" data-directory
))
167 (goto-char (point-min)))
169 (defun describe-no-warranty ()
170 "Display info on all the kinds of warranty Emacs does NOT have."
173 (let (case-fold-search)
174 (search-forward "NO WARRANTY")
177 (defun view-emacs-news ()
178 "Display info on recent changes to Emacs."
180 (find-file-read-only (expand-file-name "NEWS" data-directory
)))
182 (defun view-lossage ()
183 "Display last 100 input keystrokes."
185 (with-output-to-temp-buffer "*Help*"
186 (princ (key-description (recent-keys)))
188 (set-buffer standard-output
)
189 (goto-char (point-min))
190 (while (progn (move-to-column 50) (not (eobp)))
191 (search-forward " " nil t
)
193 (print-help-return-message)))
195 (defun help-for-help ()
196 "You have typed C-h, the help character. Type a Help option:
198 A command-apropos. Give a substring, and see a list of commands
199 (functions interactively callable) that contain
200 that substring. See also the apropos command.
201 B describe-bindings. Display table of all key bindings.
202 C describe-key-briefly. Type a command key sequence;
203 it prints the function name that sequence runs.
204 F describe-function. Type a function name and get documentation of it.
205 I info. The info documentation reader.
206 K describe-key. Type a command key sequence;
207 it displays the full documentation.
208 L view-lossage. Shows last 100 characters you typed.
209 M describe-mode. Print documentation of current major mode,
210 which describes the commands peculiar to it.
211 N view-emacs-news. Shows emacs news file.
212 S describe-syntax. Display contents of syntax table, plus explanations
213 T help-with-tutorial. Select the Emacs learn-by-doing tutorial.
214 V describe-variable. Type name of a variable;
215 it displays the variable's documentation and value.
216 W where-is. Type command name; it prints which keystrokes
218 C-c print Emacs copying permission (General Public License).
219 C-d print Emacs ordering information.
220 C-n print news of recent Emacs changes.
221 C-w print information on absence of warranty for GNU Emacs."
224 "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: ")
225 (let ((char (read-char)))
226 (if (or (= char ?\C-h
) (= char ??
))
227 (save-window-excursion
228 (switch-to-buffer "*Help*")
229 (delete-other-windows)
231 (insert (documentation 'help-for-help
))
232 (goto-char (point-min))
233 (while (memq char
'(?\C-h ?? ?\C-v ?\ ?
\177 ?\M-v
))
234 (if (memq char
'(?\C-v ?\
))
236 (if (memq char
'(?
\177 ?\M-v
))
238 (message "A B C F I K L M N S T V W C-c C-d C-n C-w%s: "
239 (if (pos-visible-in-window-p (point-max))
240 "" " or Space to scroll"))
241 (let ((cursor-in-echo-area t
))
242 (setq char
(read-char))))))
243 (let ((defn (cdr (assq (downcase char
) (cdr help-map
)))))
244 (if defn
(call-interactively defn
) (ding)))))
247 ;; Return a function which is called by the list containing point.
248 ;; If that gives no function, return a function whose name is around point.
249 ;; If that doesn't give a function, return nil.
250 (defun function-called-at-point ()
251 (or (condition-case ()
254 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
258 (setq obj
(read (current-buffer)))
259 (and (symbolp obj
) (fboundp obj
) obj
))))
264 (skip-chars-forward "'")
265 (let ((obj (read (current-buffer))))
266 (and (symbolp obj
) (fboundp obj
) obj
)))
269 (defun describe-function (function)
270 "Display the full documentation of FUNCTION (a symbol)."
272 (let ((fn (function-called-at-point))
273 (enable-recursive-minibuffers t
)
275 (setq val
(completing-read (if fn
276 (format "Describe function (default %s): " fn
)
277 "Describe function: ")
279 (list (if (equal val
"")
281 (with-output-to-temp-buffer "*Help*"
284 (let* ((def (symbol-function function
))
285 (beg (if (commandp def
) "an interactive " "a ")))
286 (princ (cond ((stringp def
) "a keyboard macro.")
288 (concat beg
"built-in function."))
289 ((compiled-function-p def
)
290 (concat beg
"compiled Lisp function."))
292 (format "alias for `%s'." def
))
293 ((eq (car-safe def
) 'lambda
)
294 (concat beg
"Lisp function."))
295 ((eq (car-safe def
) 'macro
)
297 ((eq (car-safe def
) 'mocklisp
)
298 "a mocklisp function.")
299 ((eq (car-safe def
) 'autoload
)
300 (format "%sLisp %s to autoload from `%s'."
302 (if (nth 4 def
) "macro" "function")
306 (if (documentation function
)
307 (princ (documentation function
))
308 (princ "not documented"))
309 (print-help-return-message)
310 ;; Return the text we displayed.
311 (save-excursion (set-buffer standard-output
) (buffer-string))))
313 (defun variable-at-point ()
317 (skip-chars-forward "'")
318 (let ((obj (read (current-buffer))))
319 (and (symbolp obj
) (boundp obj
) obj
)))
322 (defun describe-variable (variable)
323 "Display the full documentation of VARIABLE (a symbol).
324 Returns the documentation as a string, also."
326 (let ((v (variable-at-point))
327 (enable-recursive-minibuffers t
)
329 (setq val
(completing-read (if v
330 (format "Describe variable (default %s): " v
)
331 "Describe variable: ")
333 (list (if (equal val
"")
335 (with-output-to-temp-buffer "*Help*"
337 (princ "'s value is ")
338 (if (not (boundp variable
))
340 (prin1 (symbol-value variable
)))
342 (princ "Documentation:")
344 (let ((doc (documentation-property variable
'variable-documentation
)))
346 (princ (substitute-command-keys doc
))
347 (princ "not documented as a variable.")))
348 (print-help-return-message)
349 ;; Return the text we displayed.
350 (save-excursion (set-buffer standard-output
) (buffer-string))))
352 (defun command-apropos (string)
353 "Like apropos but lists only symbols that are names of commands
354 \(interactively callable functions). Argument REGEXP is a regular expression
355 that is matched against command symbol names. Returns list of symbols and
356 documentation found."
357 (interactive "sCommand apropos (regexp): ")
359 (let ((standard-output (get-buffer-create "*Help*")))
360 (print-help-return-message 'identity
))))
361 (apropos string
'commandp
)
362 (and message
(message message
))))
364 (defun locate-library (library &optional nosuffix
)
365 "Show the full path name of Emacs library LIBRARY.
366 This command searches the directories in `load-path' like `M-x load-library'
367 to find the file that `M-x load-library RET LIBRARY RET' would load.
368 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
369 to the specified name LIBRARY (a la calling `load' instead of `load-library')."
370 (interactive "sLocate library: ")
376 (let ((try (expand-file-name (concat library suf
) dir
)))
377 (and (file-readable-p try
)
378 (null (file-directory-p try
))
380 (message "Library is file %s" try
)
381 (throw 'answer try
)))))
382 (if nosuffix
'("") '(".elc" ".el" ""))))
384 (message "No library %s in search path" library
)