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