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