entered into RCS
[bpt/emacs.git] / lisp / help.el
CommitLineData
1a06eabd
ER
1;;; help.el --- help commands for Emacs
2
e5167999
ER
3;; Maintainer: FSF
4;; Last-Modified: 30 Jun 1992
fd7fa35a 5;; Keywords: help, internal
e5167999 6
433ae6f6
RS
7;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
8
9;; This file is part of GNU Emacs.
10
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
e5167999 13;; the Free Software Foundation; either version 2, or (at your option)
433ae6f6
RS
14;; any later version.
15
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.
20
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.
24
e5167999
ER
25;;; Code:
26
433ae6f6
RS
27(defvar help-map (make-sparse-keymap)
28 "Keymap for characters following the Help key.")
29
30(define-key global-map "\C-h" 'help-command)
31(fset 'help-command help-map)
32
33(define-key help-map "\C-h" 'help-for-help)
34(define-key help-map "?" 'help-for-help)
35
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)
40
41(define-key help-map "b" 'describe-bindings)
42
43(define-key help-map "c" 'describe-key-briefly)
44(define-key help-map "k" 'describe-key)
45
46(define-key help-map "d" 'describe-function)
47(define-key help-map "f" 'describe-function)
48
49(define-key help-map "i" 'info)
e5d77022
JB
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)
433ae6f6
RS
52
53(define-key help-map "l" 'view-lossage)
54
55(define-key help-map "m" 'describe-mode)
56
57(define-key help-map "\C-n" 'view-emacs-news)
58(define-key help-map "n" 'view-emacs-news)
59
60(define-key help-map "s" 'describe-syntax)
61
62(define-key help-map "t" 'help-with-tutorial)
63
64(define-key help-map "w" 'where-is)
65
66(define-key help-map "v" 'describe-variable)
67
68(defun help-with-tutorial ()
69 "Select the Emacs learn-by-doing tutorial."
70 (interactive)
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 "~/"))
79058860 78 (setq buffer-auto-save-file-name nil)
1e6dacf6 79 (insert-file-contents (expand-file-name "TUTORIAL" data-directory))
433ae6f6
RS
80 (goto-char (point-min))
81 (search-forward "\n<<")
82 (beginning-of-line)
83 (delete-region (point) (progn (end-of-line) (point)))
84 (newline (- (window-height (selected-window))
85 (count-lines (point-min) (point))
86 6))
87 (goto-char (point-min))
88 (set-buffer-modified-p nil))))
89
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"
97 (key-description key)
98 (if (symbolp defn) defn (prin1-to-string defn))))))
99
100(defun print-help-return-message (&optional function)
101 "Display or return message saying how to restore windows after help command.
102Computes a message and applies the optional argument FUNCTION to it.
103If FUNCTION is nil, applies `message' to it, thus printing it."
104 (and (not (get-buffer-window standard-output))
105 (funcall (or function 'message)
106 (concat
107 (substitute-command-keys
108 (if (one-window-p t)
109 (if pop-up-windows
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.")))))
115
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*"
123 (prin1 defn)
124 (princ ":\n")
125 (if (documentation defn)
126 (princ (documentation defn))
127 (princ "not documented"))
128 (print-help-return-message)))))
129
130(defun describe-mode (&optional minor)
131 "Display documentation of current major mode.
132If optional MINOR is non-nil (or prefix argument is given if interactive),
133display documentation of active minor modes as well.
134For 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
136describes the minor mode."
137 (interactive)
138 (with-output-to-temp-buffer "*Help*"
139 (princ mode-name)
140 (princ " Mode:\n")
141 (princ (documentation major-mode))
142 (let ((minor-modes minor-mode-alist)
143 (locals (buffer-local-variables)))
144 (while minor-modes
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
150 ;; definition.
151 (if (and local-binding
152 (cdr local-binding)
153 (fboundp minor-mode))
154 (progn
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)))
160
161;; So keyboard macro definitions are documented correctly
162(fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
163
164(defun describe-distribution ()
165 "Display info on how to obtain the latest version of GNU Emacs."
166 (interactive)
167 (find-file-read-only
1e6dacf6 168 (expand-file-name "DISTRIB" data-directory)))
433ae6f6
RS
169
170(defun describe-copying ()
171 "Display info on how you may redistribute copies of GNU Emacs."
172 (interactive)
173 (find-file-read-only
1e6dacf6 174 (expand-file-name "COPYING" data-directory))
433ae6f6
RS
175 (goto-char (point-min)))
176
177(defun describe-no-warranty ()
178 "Display info on all the kinds of warranty Emacs does NOT have."
179 (interactive)
180 (describe-copying)
181 (let (case-fold-search)
182 (search-forward "NO WARRANTY")
183 (recenter 0)))
184
185(defun view-emacs-news ()
186 "Display info on recent changes to Emacs."
187 (interactive)
1e6dacf6 188 (find-file-read-only (expand-file-name "NEWS" data-directory)))
433ae6f6
RS
189
190(defun view-lossage ()
191 "Display last 100 input keystrokes."
192 (interactive)
193 (with-output-to-temp-buffer "*Help*"
194 (princ (key-description (recent-keys)))
195 (save-excursion
196 (set-buffer standard-output)
197 (goto-char (point-min))
198 (while (progn (move-to-column 50) (not (eobp)))
199 (search-forward " " nil t)
200 (insert "\n")))
201 (print-help-return-message)))
202
203(defun help-for-help ()
204 "You have typed C-h, the help character. Type a Help option:
205
206A 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.
209B describe-bindings. Display table of all key bindings.
210C describe-key-briefly. Type a command key sequence;
211 it prints the function name that sequence runs.
212F describe-function. Type a function name and get documentation of it.
213I info. The info documentation reader.
214K describe-key. Type a command key sequence;
215 it displays the full documentation.
216L view-lossage. Shows last 100 characters you typed.
217M describe-mode. Print documentation of current major mode,
218 which describes the commands peculiar to it.
219N view-emacs-news. Shows emacs news file.
220S describe-syntax. Display contents of syntax table, plus explanations
221T help-with-tutorial. Select the Emacs learn-by-doing tutorial.
222V describe-variable. Type name of a variable;
223 it displays the variable's documentation and value.
224W where-is. Type command name; it prints which keystrokes
225 invoke that command.
226C-c print Emacs copying permission (General Public License).
227C-d print Emacs ordering information.
228C-n print news of recent Emacs changes.
229C-w print information on absence of warranty for GNU Emacs."
230 (interactive)
231 (message
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)
238 (erase-buffer)
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 ?\ ))
243 (scroll-up))
244 (if (memq char '(?\177 ?\M-v))
245 (scroll-down))
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)))))
253
254
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 ()
260 (save-excursion
261 (save-restriction
262 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
263 (backward-up-list 1)
264 (forward-char 1)
265 (let (obj)
266 (setq obj (read (current-buffer)))
267 (and (symbolp obj) (fboundp obj) obj))))
268 (error nil))
269 (condition-case ()
270 (save-excursion
271 (forward-sexp -1)
272 (skip-chars-forward "'")
273 (let ((obj (read (current-buffer))))
274 (and (symbolp obj) (fboundp obj) obj)))
275 (error nil))))
276
277(defun describe-function (function)
278 "Display the full documentation of FUNCTION (a symbol)."
279 (interactive
280 (let ((fn (function-called-at-point))
281 (enable-recursive-minibuffers t)
282 val)
283 (setq val (completing-read (if fn
284 (format "Describe function (default %s): " fn)
285 "Describe function: ")
286 obarray 'fboundp t))
287 (list (if (equal val "")
288 fn (intern val)))))
289 (with-output-to-temp-buffer "*Help*"
290 (prin1 function)
291 (princ ": ")
292 (let* ((def (symbol-function function))
293 (beg (if (commandp def) "an interactive " "a ")))
294 (princ (cond ((stringp def) "a keyboard macro.")
295 ((subrp def)
296 (concat beg "built-in function."))
297 ((compiled-function-p def)
298 (concat beg "compiled Lisp function."))
299 ((symbolp def)
300 (format "alias for `%s'." def))
301 ((eq (car-safe def) 'lambda)
302 (concat beg "Lisp function."))
303 ((eq (car-safe def) 'macro)
304 "a Lisp 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'."
309 beg
310 (if (nth 4 def) "macro" "function")
311 (nth 1 def)))
312 (t "")))
313 (terpri))
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))))
320
321(defun variable-at-point ()
322 (condition-case ()
323 (save-excursion
324 (forward-sexp -1)
325 (skip-chars-forward "'")
326 (let ((obj (read (current-buffer))))
327 (and (symbolp obj) (boundp obj) obj)))
328 (error nil)))
329
330(defun describe-variable (variable)
331 "Display the full documentation of VARIABLE (a symbol).
332Returns the documentation as a string, also."
333 (interactive
334 (let ((v (variable-at-point))
335 (enable-recursive-minibuffers t)
336 val)
337 (setq val (completing-read (if v
338 (format "Describe variable (default %s): " v)
339 "Describe variable: ")
340 obarray 'boundp t))
341 (list (if (equal val "")
342 v (intern val)))))
343 (with-output-to-temp-buffer "*Help*"
344 (prin1 variable)
345 (princ "'s value is ")
346 (if (not (boundp variable))
347 (princ "void.")
348 (prin1 (symbol-value variable)))
349 (terpri) (terpri)
350 (princ "Documentation:")
351 (terpri)
352 (let ((doc (documentation-property variable 'variable-documentation)))
353 (if doc
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))))
359
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
363that is matched against command symbol names. Returns list of symbols and
364documentation found."
365 (interactive "sCommand apropos (regexp): ")
366 (let ((message
367 (let ((standard-output (get-buffer-create "*Help*")))
368 (print-help-return-message 'identity))))
369 (apropos string 'commandp)
370 (and message (message message))))
371
372(defun locate-library (library &optional nosuffix)
373 "Show the full path name of Emacs library LIBRARY.
374This command searches the directories in `load-path' like `M-x load-library'
375to find the file that `M-x load-library RET LIBRARY RET' would load.
376Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
377to the specified name LIBRARY (a la calling `load' instead of `load-library')."
378 (interactive "sLocate library: ")
379 (catch 'answer
380 (mapcar
381 '(lambda (dir)
382 (mapcar
383 '(lambda (suf)
384 (let ((try (expand-file-name (concat library suf) dir)))
385 (and (file-readable-p try)
386 (null (file-directory-p try))
387 (progn
388 (message "Library is file %s" try)
389 (throw 'answer try)))))
390 (if nosuffix '("") '(".elc" ".el" ""))))
391 load-path)
392 (message "No library %s in search path" library)
393 nil))
1a06eabd
ER
394
395;;; help.el ends here