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