Info on the terminal-package-finding algorithm.
[emacs.git] / lisp / help.el
blobfc0056233a3594ab0ab9c5cebbd0ab8560be21f3
1 ;;; help.el --- help commands for Emacs
3 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: help, internal
8 ;; This file is part of GNU Emacs.
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.
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.
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.
24 ;;; Code:
26 (require 'help-screen)
28 (defvar help-map (make-sparse-keymap)
29 "Keymap for characters following the Help key.")
31 (define-key global-map (char-to-string help-char) 'help-command)
32 (fset 'help-command help-map)
34 (define-key help-map (char-to-string help-char) 'help-for-help)
35 (define-key help-map "?" 'help-for-help)
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)
42 (define-key help-map "b" 'describe-bindings)
44 (define-key help-map "c" 'describe-key-briefly)
45 (define-key help-map "k" 'describe-key)
47 (define-key help-map "d" 'describe-function)
48 (define-key help-map "f" 'describe-function)
50 (define-key help-map "i" 'info)
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)
54 (define-key help-map "l" 'view-lossage)
56 (define-key help-map "m" 'describe-mode)
58 (define-key help-map "\C-n" 'view-emacs-news)
59 (define-key help-map "n" 'view-emacs-news)
61 (define-key help-map "p" 'finder-by-keyword)
62 (autoload 'finder-by-keyword "finder.el")
64 (define-key help-map "s" 'describe-syntax)
66 (define-key help-map "t" 'help-with-tutorial)
68 (define-key help-map "w" 'where-is)
70 (define-key help-map "v" 'describe-variable)
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 "~/"))
82 (setq buffer-auto-save-file-name nil)
83 (insert-file-contents (expand-file-name "TUTORIAL" data-directory))
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))))
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))))))
104 (defun print-help-return-message (&optional function)
105 "Display or return message saying how to restore windows after help command.
106 Computes a message and applies the optional argument FUNCTION to it.
107 If 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.")))))
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)))))
134 (defun describe-mode (&optional minor)
135 "Display documentation of current major mode.
136 If optional MINOR is non-nil (or prefix argument is given if interactive),
137 display documentation of active minor modes as well.
138 For 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
140 describes 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)))
165 ;; So keyboard macro definitions are documented correctly
166 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
168 (defun describe-distribution ()
169 "Display info on how to obtain the latest version of GNU Emacs."
170 (interactive)
171 (find-file-read-only
172 (expand-file-name "DISTRIB" data-directory)))
174 (defun describe-copying ()
175 "Display info on how you may redistribute copies of GNU Emacs."
176 (interactive)
177 (find-file-read-only
178 (expand-file-name "COPYING" data-directory))
179 (goto-char (point-min)))
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)))
189 (defun view-emacs-news ()
190 "Display info on recent changes to Emacs."
191 (interactive)
192 (find-file-read-only (expand-file-name "NEWS" data-directory)))
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)))
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: "
209 "You have typed \\[help-for-help], the help character. Type a Help option:
211 A 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.
214 B describe-bindings. Display table of all key bindings.
215 C describe-key-briefly. Type a command key sequence;
216 it prints the function name that sequence runs.
217 F describe-function. Type a function name and get documentation of it.
218 I info. The info documentation reader.
219 K describe-key. Type a command key sequence;
220 it displays the full documentation.
221 L view-lossage. Shows last 100 characters you typed.
222 M describe-mode. Print documentation of current major mode,
223 which describes the commands peculiar to it.
224 N view-emacs-news. Shows emacs news file.
225 P finder-by-keyword. Find packages matching a given topic keyword.
226 S describe-syntax. Display contents of syntax table, plus explanations
227 T help-with-tutorial. Select the Emacs learn-by-doing tutorial.
228 V describe-variable. Type name of a variable;
229 it displays the variable's documentation and value.
230 W where-is. Type command name; it prints which keystrokes
231 invoke that command.
232 C-c print Emacs copying permission (General Public License).
233 C-d print Emacs ordering information.
234 C-n print news of recent Emacs changes.
235 C-w print information on absence of warranty for GNU Emacs."
236 help-map)
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))))
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."))
280 ((byte-code-function-p def)
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)
291 (format "%s autoloaded Lisp %s."
292 (if (commandp def) "an interactive" "an")
293 (if (nth 4 def) "macro" "function")
294 ;;; Including the file name made this line too long.
295 ;;; (nth 1 def)
297 (t "")))
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)))))
316 (print-help-return-message)
317 ;; Return the text we displayed.
318 (save-excursion (set-buffer standard-output) (buffer-string))))
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)))
329 (defun describe-variable (variable)
330 "Display the full documentation of VARIABLE (a symbol).
331 Returns 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))))
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
362 that is matched against command symbol names. Returns list of symbols and
363 documentation found."
364 (interactive "sCommand apropos (regexp): ")
365 (let ((message
366 (let ((standard-output (get-buffer-create "*Help*")))
367 (print-help-return-message 'identity))))
368 (if (apropos string t 'commandp)
369 (and message (message message)))))
371 (defun locate-library (library &optional nosuffix)
372 "Show the full path name of Emacs library LIBRARY.
373 This command searches the directories in `load-path' like `M-x load-library'
374 to find the file that `M-x load-library RET LIBRARY RET' would load.
375 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
376 to 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))
394 ;;; help.el ends here