]> code.delx.au - gnu-emacs/blob - lisp/help.el
(describe-mode): Always show minor modes, at front.
[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 "p")
177 (with-output-to-temp-buffer "*Help*"
178 (if minor
179 (let ((minor-modes minor-mode-alist)
180 (locals (buffer-local-variables)))
181 (while minor-modes
182 (let* ((minor-mode (car (car minor-modes)))
183 (indicator (car (cdr (car minor-modes))))
184 (local-binding (assq minor-mode locals)))
185 ;; Document a minor mode if it is listed in minor-mode-alist,
186 ;; bound locally in this buffer, non-nil, and has a function
187 ;; definition.
188 (if (and local-binding
189 (cdr local-binding)
190 (fboundp minor-mode))
191 (let ((pretty-minor-mode minor-mode))
192 (if (string-match "-mode$" (symbol-name minor-mode))
193 (setq pretty-minor-mode
194 (capitalize
195 (substring (symbol-name minor-mode)
196 0 (match-beginning 0)))))
197 (while (and indicator (symbolp indicator))
198 (setq indicator (symbol-value indicator)))
199 (princ (format "%s minor mode (indicator%s):\n"
200 pretty-minor-mode indicator))
201 (princ (documentation minor-mode))
202 (princ "\n\n"))))
203 (setq minor-modes (cdr minor-modes)))))
204 (princ mode-name)
205 (princ " mode:\n")
206 (princ (documentation major-mode))
207 (print-help-return-message)))
208
209 ;; So keyboard macro definitions are documented correctly
210 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
211
212 (defun describe-distribution ()
213 "Display info on how to obtain the latest version of GNU Emacs."
214 (interactive)
215 (find-file-read-only
216 (expand-file-name "DISTRIB" data-directory)))
217
218 (defun describe-copying ()
219 "Display info on how you may redistribute copies of GNU Emacs."
220 (interactive)
221 (find-file-read-only
222 (expand-file-name "COPYING" data-directory))
223 (goto-char (point-min)))
224
225 (defun describe-project ()
226 "Display info on the GNU project."
227 (interactive)
228 (find-file-read-only
229 (expand-file-name "GNU" data-directory))
230 (goto-char (point-min)))
231
232 (defun describe-no-warranty ()
233 "Display info on all the kinds of warranty Emacs does NOT have."
234 (interactive)
235 (describe-copying)
236 (let (case-fold-search)
237 (search-forward "NO WARRANTY")
238 (recenter 0)))
239
240 (defun describe-prefix-bindings ()
241 "Describe the bindings of the prefix used to reach this command.
242 The prefix described consists of all but the last event
243 of the key sequence that ran this command."
244 (interactive)
245 (let* ((key (this-command-keys))
246 (prefix (make-vector (1- (length key)) nil))
247 i)
248 (setq i 0)
249 (while (< i (length prefix))
250 (aset prefix i (aref key i))
251 (setq i (1+ i)))
252 (describe-bindings prefix)))
253 ;; Make C-h after a prefix, when not specifically bound,
254 ;; run describe-prefix-bindings.
255 (setq prefix-help-command 'describe-prefix-bindings)
256
257 (defun view-emacs-news ()
258 "Display info on recent changes to Emacs."
259 (interactive)
260 (find-file-read-only (expand-file-name "NEWS" data-directory)))
261
262 (defun view-lossage ()
263 "Display last 100 input keystrokes."
264 (interactive)
265 (with-output-to-temp-buffer "*Help*"
266 (princ (key-description (recent-keys)))
267 (save-excursion
268 (set-buffer standard-output)
269 (goto-char (point-min))
270 (while (progn (move-to-column 50) (not (eobp)))
271 (search-forward " " nil t)
272 (insert "\n")))
273 (print-help-return-message)))
274
275 (defalias 'help 'help-for-help)
276 (make-help-screen help-for-help
277 "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:"
278 "You have typed \\[help-command], the help character. Type a Help option:
279 \(Use \\<help-map>\\[scroll-up] or \\[scroll-down] to scroll through this text.
280 Type \\<help-map>\\[help-quit] to exit the Help command.)
281
282 a command-apropos. Give a substring, and see a list of commands
283 (functions interactively callable) that contain
284 that substring. See also the apropos command.
285 b describe-bindings. Display table of all key bindings.
286 c describe-key-briefly. Type a command key sequence;
287 it prints the function name that sequence runs.
288 f describe-function. Type a function name and get documentation of it.
289 C-f Info-goto-emacs-command-node. Type a function name;
290 it takes you to the Info node for that command.
291 i info. The info documentation reader.
292 k describe-key. Type a command key sequence;
293 it displays the full documentation.
294 C-k Info-goto-emacs-key-command-node. Type a command key sequence;
295 it takes you to the Info node for the command bound to that key.
296 l view-lossage. Shows last 100 characters you typed.
297 m describe-mode. Print documentation of current major mode,
298 which describes the commands peculiar to it.
299 n view-emacs-news. Shows emacs news file.
300 p finder-by-keyword. Find packages matching a given topic keyword.
301 s describe-syntax. Display contents of syntax table, plus explanations
302 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
303 v describe-variable. Type name of a variable;
304 it displays the variable's documentation and value.
305 w where-is. Type command name; it prints which keystrokes
306 invoke that command.
307 C-c print Emacs copying permission (General Public License).
308 C-d print Emacs ordering information.
309 C-n print news of recent Emacs changes.
310 C-p print information about the GNU project.
311 C-w print information on absence of warranty for GNU Emacs."
312 help-map)
313
314 ;; Return a function which is called by the list containing point.
315 ;; If that gives no function, return a function whose name is around point.
316 ;; If that doesn't give a function, return nil.
317 (defun function-called-at-point ()
318 (or (condition-case ()
319 (save-excursion
320 (save-restriction
321 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
322 (backward-up-list 1)
323 (forward-char 1)
324 (let (obj)
325 (setq obj (read (current-buffer)))
326 (and (symbolp obj) (fboundp obj) obj))))
327 (error nil))
328 (condition-case ()
329 (save-excursion
330 (forward-sexp -1)
331 (skip-chars-forward "'")
332 (let ((obj (read (current-buffer))))
333 (and (symbolp obj) (fboundp obj) obj)))
334 (error nil))))
335
336 (defun describe-function (function)
337 "Display the full documentation of FUNCTION (a symbol)."
338 (interactive
339 (let ((fn (function-called-at-point))
340 (enable-recursive-minibuffers t)
341 val)
342 (setq val (completing-read (if fn
343 (format "Describe function (default %s): " fn)
344 "Describe function: ")
345 obarray 'fboundp t))
346 (list (if (equal val "")
347 fn (intern val)))))
348 (with-output-to-temp-buffer "*Help*"
349 (prin1 function)
350 (princ ": ")
351 (let* ((def (symbol-function function))
352 (beg (if (commandp def) "an interactive " "a ")))
353 (princ (cond ((or (stringp def)
354 (vectorp def))
355 "a keyboard macro.")
356 ((subrp def)
357 (concat beg "built-in function."))
358 ((byte-code-function-p def)
359 (concat beg "compiled Lisp function."))
360 ((symbolp def)
361 (format "alias for `%s'." def))
362 ((eq (car-safe def) 'lambda)
363 (concat beg "Lisp function."))
364 ((eq (car-safe def) 'macro)
365 "a Lisp macro.")
366 ((eq (car-safe def) 'mocklisp)
367 "a mocklisp function.")
368 ((eq (car-safe def) 'autoload)
369 (format "%s autoloaded Lisp %s."
370 (if (commandp def) "an interactive" "an")
371 (if (nth 4 def) "macro" "function")
372 ;;; Including the file name made this line too long.
373 ;;; (nth 1 def)
374 ))
375 (t "")))
376 (terpri)
377 (let ((arglist (cond ((byte-code-function-p def)
378 (car (append def nil)))
379 ((eq (car-safe def) 'lambda)
380 (nth 1 def))
381 (t t))))
382 (if (listp arglist)
383 (progn
384 (princ (cons function
385 (mapcar (lambda (arg)
386 (if (memq arg '(&optional &rest))
387 arg
388 (intern (upcase (symbol-name arg)))))
389 arglist)))
390 (terpri))))
391 (if (documentation function)
392 (progn (terpri)
393 (princ (documentation function)))
394 (princ "not documented"))
395 )
396 (print-help-return-message)
397 ;; Return the text we displayed.
398 (save-excursion (set-buffer standard-output) (buffer-string))))
399
400 (defun variable-at-point ()
401 (condition-case ()
402 (save-excursion
403 (forward-sexp -1)
404 (skip-chars-forward "'")
405 (let ((obj (read (current-buffer))))
406 (and (symbolp obj) (boundp obj) obj)))
407 (error nil)))
408
409 (defun describe-variable (variable)
410 "Display the full documentation of VARIABLE (a symbol).
411 Returns the documentation as a string, also."
412 (interactive
413 (let ((v (variable-at-point))
414 (enable-recursive-minibuffers t)
415 val)
416 (setq val (completing-read (if v
417 (format "Describe variable (default %s): " v)
418 "Describe variable: ")
419 obarray 'boundp t))
420 (list (if (equal val "")
421 v (intern val)))))
422 (with-output-to-temp-buffer "*Help*"
423 (prin1 variable)
424 (princ "'s value is ")
425 (if (not (boundp variable))
426 (princ "void.")
427 (prin1 (symbol-value variable)))
428 (terpri) (terpri)
429 (princ "Documentation:")
430 (terpri)
431 (let ((doc (documentation-property variable 'variable-documentation)))
432 (if doc
433 (princ (substitute-command-keys doc))
434 (princ "not documented as a variable.")))
435 (print-help-return-message)
436 ;; Return the text we displayed.
437 (save-excursion (set-buffer standard-output) (buffer-string))))
438
439 (defun where-is (definition)
440 "Print message listing key sequences that invoke specified command.
441 Argument is a command definition, usually a symbol with a function definition."
442 (interactive
443 (let ((fn (function-called-at-point))
444 (enable-recursive-minibuffers t)
445 val)
446 (setq val (completing-read (if fn
447 (format "Where is command (default %s): " fn)
448 "Where is command: ")
449 obarray 'fboundp t))
450 (list (if (equal val "")
451 fn (intern val)))))
452 (let* ((keys (where-is-internal definition overriding-local-map nil nil))
453 (keys1 (mapconcat 'key-description keys ", ")))
454 (if (> (length keys1) 0)
455 (message "%s is on %s" definition keys1)
456 (message "%s is not on any key" definition)))
457 nil)
458
459 (defun command-apropos (string)
460 "Like apropos but lists only symbols that are names of commands
461 \(interactively callable functions). Argument REGEXP is a regular expression
462 that is matched against command symbol names. Returns list of symbols and
463 documentation found."
464 (interactive "sCommand apropos (regexp): ")
465 (let ((message
466 (let ((standard-output (get-buffer-create "*Help*")))
467 (print-help-return-message 'identity))))
468 (if (apropos string t 'commandp)
469 (and message (message message)))))
470
471 (defun locate-library (library &optional nosuffix)
472 "Show the full path name of Emacs library LIBRARY.
473 This command searches the directories in `load-path' like `M-x load-library'
474 to find the file that `M-x load-library RET LIBRARY RET' would load.
475 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
476 to the specified name LIBRARY (a la calling `load' instead of `load-library')."
477 (interactive "sLocate library: ")
478 (catch 'answer
479 (mapcar
480 '(lambda (dir)
481 (mapcar
482 '(lambda (suf)
483 (let ((try (expand-file-name (concat library suf) dir)))
484 (and (file-readable-p try)
485 (null (file-directory-p try))
486 (progn
487 (message "Library is file %s" try)
488 (throw 'answer try)))))
489 (if nosuffix '("") '(".elc" ".el" ""))))
490 load-path)
491 (message "No library %s in search path" library)
492 nil))
493
494 ;;; help.el ends here