]> code.delx.au - gnu-emacs/blob - lisp/help.el
(shrink-window-if-larger-than-buffer):
[gnu-emacs] / lisp / help.el
1 ;;; help.el --- help commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1993 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: help, internal
7
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
12 ;; the Free Software Foundation; either version 2, or (at your option)
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
24 ;;; Commentary:
25
26 ;; This code implements GNU Emac's on-line help system, the one invoked by
27 ;;`M-x help-for-help'.
28
29 ;;; Code:
30
31 ;; Get the macro make-help-screen when this is compiled,
32 ;; or run interpreted, but not when the compiled code is loaded.
33 (eval-when-compile (require 'help-macro))
34
35 (defvar help-map (make-sparse-keymap)
36 "Keymap for characters following the Help key.")
37
38 (define-key global-map (char-to-string help-char) 'help-command)
39 (fset 'help-command help-map)
40
41 (define-key help-map (char-to-string help-char) 'help-for-help)
42 (define-key help-map "?" 'help-for-help)
43
44 (define-key help-map "\C-c" 'describe-copying)
45 (define-key help-map "\C-d" 'describe-distribution)
46 (define-key help-map "\C-w" 'describe-no-warranty)
47 (define-key help-map "\C-p" 'describe-project)
48 (define-key help-map "a" 'command-apropos)
49
50 (define-key help-map "b" 'describe-bindings)
51
52 (define-key help-map "c" 'describe-key-briefly)
53 (define-key help-map "k" 'describe-key)
54
55 (define-key help-map "d" 'describe-function)
56 (define-key help-map "f" 'describe-function)
57
58 (define-key help-map "i" 'info)
59 (define-key help-map "\C-f" 'Info-goto-emacs-command-node)
60 (define-key help-map "\C-k" 'Info-goto-emacs-key-command-node)
61
62 (define-key help-map "l" 'view-lossage)
63
64 (define-key help-map "m" 'describe-mode)
65
66 (define-key help-map "\C-n" 'view-emacs-news)
67 (define-key help-map "n" 'view-emacs-news)
68
69 (define-key help-map "p" 'finder-by-keyword)
70 (autoload 'finder-by-keyword "finder")
71
72 (define-key help-map "s" 'describe-syntax)
73
74 (define-key help-map "t" 'help-with-tutorial)
75
76 (define-key help-map "w" 'where-is)
77
78 (define-key help-map "v" 'describe-variable)
79
80 (define-key help-map "q" 'help-quit)
81
82 (defun help-quit ()
83 (interactive)
84 nil)
85
86 (defun help-with-tutorial ()
87 "Select the Emacs learn-by-doing tutorial."
88 (interactive)
89 (let ((file (expand-file-name "~/TUTORIAL")))
90 (delete-other-windows)
91 (if (get-file-buffer file)
92 (switch-to-buffer (get-file-buffer file))
93 (switch-to-buffer (create-file-buffer file))
94 (setq buffer-file-name file)
95 (setq default-directory (expand-file-name "~/"))
96 (setq buffer-auto-save-file-name nil)
97 (insert-file-contents (expand-file-name "TUTORIAL" data-directory))
98 (goto-char (point-min))
99 (search-forward "\n<<")
100 (beginning-of-line)
101 (delete-region (point) (progn (end-of-line) (point)))
102 (let ((n (- (window-height (selected-window))
103 (count-lines (point-min) (point))
104 6)))
105 (if (< n 12)
106 (newline n)
107 ;; Some people get confused by the large gap.
108 (newline (/ n 2))
109 (insert "[Middle of page left blank for didactic purposes. "
110 "Text continues below]")
111 (newline (- n (/ n 2)))))
112 (goto-char (point-min))
113 (set-buffer-modified-p nil))))
114
115 (defun describe-key-briefly (key)
116 "Print the name of the function KEY invokes. KEY is a string."
117 (interactive "kDescribe key briefly: ")
118 ;; If this key seq ends with a down event, discard the
119 ;; following click or drag event. Otherwise that would
120 ;; erase the message.
121 (let ((type (aref key (1- (length key)))))
122 (if (listp type) (setq type (car type)))
123 (and (symbolp type)
124 (memq 'down (event-modifiers type))
125 (read-event)))
126 (let ((defn (key-binding key)))
127 (if (or (null defn) (integerp defn))
128 (message "%s is undefined" (key-description key))
129 (message "%s runs the command %s"
130 (key-description key)
131 (if (symbolp defn) defn (prin1-to-string defn))))))
132
133 (defun print-help-return-message (&optional function)
134 "Display or return message saying how to restore windows after help command.
135 Computes a message and applies the optional argument FUNCTION to it.
136 If FUNCTION is nil, applies `message' to it, thus printing it."
137 (and (not (get-buffer-window standard-output))
138 (funcall (or function 'message)
139 (concat
140 (substitute-command-keys
141 (if (one-window-p t)
142 (if pop-up-windows
143 "Type \\[delete-other-windows] to remove help window."
144 "Type \\[switch-to-buffer] RET to remove help window.")
145 "Type \\[switch-to-buffer-other-window] RET to restore the other window."))
146 (substitute-command-keys
147 " \\[scroll-other-window] to scroll the help.")))))
148
149 (defun describe-key (key)
150 "Display documentation of the function invoked by KEY. KEY is a string."
151 (interactive "kDescribe key: ")
152 ;; If this key seq ends with a down event, discard the
153 ;; following click or drag event. Otherwise that would
154 ;; erase the message.
155 (let ((type (aref key (1- (length key)))))
156 (if (listp type) (setq type (car type)))
157 (and (symbolp type)
158 (memq 'down (event-modifiers type))
159 (read-event)))
160 (let ((defn (key-binding key)))
161 (if (or (null defn) (integerp defn))
162 (message "%s is undefined" (key-description key))
163 (with-output-to-temp-buffer "*Help*"
164 (prin1 defn)
165 (princ ":\n")
166 (if (documentation defn)
167 (princ (documentation defn))
168 (princ "not documented"))
169 (print-help-return-message)))))
170
171 (defun describe-mode ()
172 "Display documentation of current major mode and minor modes.
173 For this to work correctly for a minor mode, the mode's indicator variable
174 \(listed in `minor-mode-alist') must also be a function whose documentation
175 describes the minor mode."
176 (interactive)
177 (with-output-to-temp-buffer "*Help*"
178 (let ((minor-modes minor-mode-alist)
179 (locals (buffer-local-variables)))
180 (while minor-modes
181 (let* ((minor-mode (car (car minor-modes)))
182 (indicator (car (cdr (car minor-modes))))
183 (local-binding (assq minor-mode locals)))
184 ;; Document a minor mode if it is listed in minor-mode-alist,
185 ;; bound locally in this buffer, non-nil, and has a function
186 ;; definition.
187 (if (and local-binding
188 (cdr local-binding)
189 (fboundp minor-mode))
190 (let ((pretty-minor-mode minor-mode))
191 (if (string-match "-mode$" (symbol-name minor-mode))
192 (setq pretty-minor-mode
193 (capitalize
194 (substring (symbol-name minor-mode)
195 0 (match-beginning 0)))))
196 (while (and indicator (symbolp indicator))
197 (setq indicator (symbol-value indicator)))
198 (princ (format "%s minor mode (indicator%s):\n"
199 pretty-minor-mode indicator))
200 (princ (documentation minor-mode))
201 (princ "\n\n"))))
202 (setq minor-modes (cdr minor-modes))))
203 (princ mode-name)
204 (princ " mode:\n")
205 (princ (documentation major-mode))
206 (print-help-return-message)))
207
208 ;; So keyboard macro definitions are documented correctly
209 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
210
211 (defun describe-distribution ()
212 "Display info on how to obtain the latest version of GNU Emacs."
213 (interactive)
214 (find-file-read-only
215 (expand-file-name "DISTRIB" data-directory)))
216
217 (defun describe-copying ()
218 "Display info on how you may redistribute copies of GNU Emacs."
219 (interactive)
220 (find-file-read-only
221 (expand-file-name "COPYING" data-directory))
222 (goto-char (point-min)))
223
224 (defun describe-project ()
225 "Display info on the GNU project."
226 (interactive)
227 (find-file-read-only
228 (expand-file-name "GNU" data-directory))
229 (goto-char (point-min)))
230
231 (defun describe-no-warranty ()
232 "Display info on all the kinds of warranty Emacs does NOT have."
233 (interactive)
234 (describe-copying)
235 (let (case-fold-search)
236 (search-forward "NO WARRANTY")
237 (recenter 0)))
238
239 (defun describe-prefix-bindings ()
240 "Describe the bindings of the prefix used to reach this command.
241 The prefix described consists of all but the last event
242 of the key sequence that ran this command."
243 (interactive)
244 (let* ((key (this-command-keys))
245 (prefix (make-vector (1- (length key)) nil))
246 i)
247 (setq i 0)
248 (while (< i (length prefix))
249 (aset prefix i (aref key i))
250 (setq i (1+ i)))
251 (describe-bindings prefix)))
252 ;; Make C-h after a prefix, when not specifically bound,
253 ;; run describe-prefix-bindings.
254 (setq prefix-help-command 'describe-prefix-bindings)
255
256 (defun view-emacs-news ()
257 "Display info on recent changes to Emacs."
258 (interactive)
259 (find-file-read-only (expand-file-name "NEWS" data-directory)))
260
261 (defun view-lossage ()
262 "Display last 100 input keystrokes."
263 (interactive)
264 (with-output-to-temp-buffer "*Help*"
265 (princ (key-description (recent-keys)))
266 (save-excursion
267 (set-buffer standard-output)
268 (goto-char (point-min))
269 (while (progn (move-to-column 50) (not (eobp)))
270 (search-forward " " nil t)
271 (insert "\n")))
272 (print-help-return-message)))
273
274 (defalias 'help 'help-for-help)
275 (make-help-screen help-for-help
276 "a b c f C-f i k C-k l m n p s t v w C-c C-d C-n C-w, or ? for more help:"
277 "You have typed \\[help-command], the help character. Type a Help option:
278 \(Use \\<help-map>\\[scroll-up] or \\[scroll-down] to scroll through this text.
279 Type \\<help-map>\\[help-quit] to exit the Help command.)
280
281 a command-apropos. Give a substring, and see a list of commands
282 (functions interactively callable) that contain
283 that substring. See also the apropos command.
284 b describe-bindings. Display table of all key bindings.
285 c describe-key-briefly. Type a command key sequence;
286 it prints the function name that sequence runs.
287 f describe-function. Type a function name and get documentation of it.
288 C-f Info-goto-emacs-command-node. Type a function name;
289 it takes you to the Info node for that command.
290 i info. The info documentation reader.
291 k describe-key. Type a command key sequence;
292 it displays the full documentation.
293 C-k Info-goto-emacs-key-command-node. Type a command key sequence;
294 it takes you to the Info node for the command bound to that key.
295 l view-lossage. Shows last 100 characters you typed.
296 m describe-mode. Print documentation of current major mode,
297 which describes the commands peculiar to it.
298 n view-emacs-news. Shows emacs news file.
299 p finder-by-keyword. Find packages matching a given topic keyword.
300 s describe-syntax. Display contents of syntax table, plus explanations
301 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
302 v describe-variable. Type name of a variable;
303 it displays the variable's documentation and value.
304 w where-is. Type command name; it prints which keystrokes
305 invoke that command.
306 C-c print Emacs copying permission (General Public License).
307 C-d print Emacs ordering information.
308 C-n print news of recent Emacs changes.
309 C-p print information about the GNU project.
310 C-w print information on absence of warranty for GNU Emacs."
311 help-map)
312
313 ;; Return a function which is called by the list containing point.
314 ;; If that gives no function, return a function whose name is around point.
315 ;; If that doesn't give a function, return nil.
316 (defun function-called-at-point ()
317 (or (condition-case ()
318 (save-excursion
319 (save-restriction
320 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
321 (backward-up-list 1)
322 (forward-char 1)
323 (let (obj)
324 (setq obj (read (current-buffer)))
325 (and (symbolp obj) (fboundp obj) obj))))
326 (error nil))
327 (condition-case ()
328 (save-excursion
329 (forward-sexp -1)
330 (skip-chars-forward "'")
331 (let ((obj (read (current-buffer))))
332 (and (symbolp obj) (fboundp obj) obj)))
333 (error nil))))
334
335 (defun describe-function (function)
336 "Display the full documentation of FUNCTION (a symbol)."
337 (interactive
338 (let ((fn (function-called-at-point))
339 (enable-recursive-minibuffers t)
340 val)
341 (setq val (completing-read (if fn
342 (format "Describe function (default %s): " fn)
343 "Describe function: ")
344 obarray 'fboundp t))
345 (list (if (equal val "")
346 fn (intern val)))))
347 (with-output-to-temp-buffer "*Help*"
348 (prin1 function)
349 (princ ": ")
350 (let* ((def (symbol-function function))
351 (beg (if (commandp def) "an interactive " "a ")))
352 (princ (cond ((or (stringp def)
353 (vectorp def))
354 "a keyboard macro.")
355 ((subrp def)
356 (concat beg "built-in function."))
357 ((byte-code-function-p def)
358 (concat beg "compiled Lisp function."))
359 ((symbolp def)
360 (format "alias for `%s'." def))
361 ((eq (car-safe def) 'lambda)
362 (concat beg "Lisp function."))
363 ((eq (car-safe def) 'macro)
364 "a Lisp macro.")
365 ((eq (car-safe def) 'mocklisp)
366 "a mocklisp function.")
367 ((eq (car-safe def) 'autoload)
368 (format "%s autoloaded Lisp %s."
369 (if (commandp def) "an interactive" "an")
370 (if (nth 4 def) "macro" "function")
371 ;;; Including the file name made this line too long.
372 ;;; (nth 1 def)
373 ))
374 (t "")))
375 (terpri)
376 (let ((arglist (cond ((byte-code-function-p def)
377 (car (append def nil)))
378 ((eq (car-safe def) 'lambda)
379 (nth 1 def))
380 (t t))))
381 (if (listp arglist)
382 (progn
383 (princ (cons function
384 (mapcar (lambda (arg)
385 (if (memq arg '(&optional &rest))
386 arg
387 (intern (upcase (symbol-name arg)))))
388 arglist)))
389 (terpri))))
390 (if (documentation function)
391 (progn (terpri)
392 (princ (documentation function)))
393 (princ "not documented"))
394 )
395 (print-help-return-message)
396 ;; Return the text we displayed.
397 (save-excursion (set-buffer standard-output) (buffer-string))))
398
399 (defun variable-at-point ()
400 (condition-case ()
401 (save-excursion
402 (forward-sexp -1)
403 (skip-chars-forward "'")
404 (let ((obj (read (current-buffer))))
405 (and (symbolp obj) (boundp obj) obj)))
406 (error nil)))
407
408 (defun describe-variable (variable)
409 "Display the full documentation of VARIABLE (a symbol).
410 Returns the documentation as a string, also."
411 (interactive
412 (let ((v (variable-at-point))
413 (enable-recursive-minibuffers t)
414 val)
415 (setq val (completing-read (if v
416 (format "Describe variable (default %s): " v)
417 "Describe variable: ")
418 obarray 'boundp t))
419 (list (if (equal val "")
420 v (intern val)))))
421 (with-output-to-temp-buffer "*Help*"
422 (prin1 variable)
423 (princ "'s value is ")
424 (if (not (boundp variable))
425 (princ "void.")
426 (prin1 (symbol-value variable)))
427 (terpri) (terpri)
428 (princ "Documentation:")
429 (terpri)
430 (let ((doc (documentation-property variable 'variable-documentation)))
431 (if doc
432 (princ (substitute-command-keys doc))
433 (princ "not documented as a variable.")))
434 (print-help-return-message)
435 ;; Return the text we displayed.
436 (save-excursion (set-buffer standard-output) (buffer-string))))
437
438 (defun where-is (definition)
439 "Print message listing key sequences that invoke specified command.
440 Argument is a command definition, usually a symbol with a function definition."
441 (interactive
442 (let ((fn (function-called-at-point))
443 (enable-recursive-minibuffers t)
444 val)
445 (setq val (completing-read (if fn
446 (format "Where is command (default %s): " fn)
447 "Where is command: ")
448 obarray 'fboundp t))
449 (list (if (equal val "")
450 fn (intern val)))))
451 (let* ((keys (where-is-internal definition overriding-local-map nil nil))
452 (keys1 (mapconcat 'key-description keys ", ")))
453 (if (> (length keys1) 0)
454 (message "%s is on %s" definition keys1)
455 (message "%s is not on any key" definition)))
456 nil)
457
458 (defun command-apropos (string)
459 "Like apropos but lists only symbols that are names of commands
460 \(interactively callable functions). Argument REGEXP is a regular expression
461 that is matched against command symbol names. Returns list of symbols and
462 documentation found."
463 (interactive "sCommand apropos (regexp): ")
464 (let ((message
465 (let ((standard-output (get-buffer-create "*Help*")))
466 (print-help-return-message 'identity))))
467 (if (apropos string t 'commandp)
468 (and message (message message)))))
469
470 (defun locate-library (library &optional nosuffix)
471 "Show the full path name of Emacs library LIBRARY.
472 This command searches the directories in `load-path' like `M-x load-library'
473 to find the file that `M-x load-library RET LIBRARY RET' would load.
474 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
475 to the specified name LIBRARY (a la calling `load' instead of `load-library')."
476 (interactive "sLocate library: ")
477 (catch 'answer
478 (mapcar
479 '(lambda (dir)
480 (mapcar
481 '(lambda (suf)
482 (let ((try (expand-file-name (concat library suf) dir)))
483 (and (file-readable-p try)
484 (null (file-directory-p try))
485 (progn
486 (message "Library is file %s" try)
487 (throw 'answer try)))))
488 (if nosuffix '("") '(".elc" ".el" ""))))
489 load-path)
490 (message "No library %s in search path" library)
491 nil))
492
493 ;;; help.el ends here