(describe-function-1): Use semicolon instead of dot.
[bpt/emacs.git] / lisp / help-fns.el
1 ;;; help-fns.el --- Complex help functions
2
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002
4 ;; Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: help, internal
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; This file contains those help commands which are complicated, and
29 ;; which may not be used in every session. For example
30 ;; `describe-function' will probably be heavily used when doing elisp
31 ;; programming, but not if just editing C files. Simpler help commands
32 ;; are in help.el
33
34 ;;; Code:
35
36 (require 'help-mode)
37
38
39 ;;;###autoload
40 (defun help-with-tutorial (&optional arg)
41 "Select the Emacs learn-by-doing tutorial.
42 If there is a tutorial version written in the language
43 of the selected language environment, that version is used.
44 If there's no tutorial in that language, `TUTORIAL' is selected.
45 With arg, you are asked to choose which language."
46 (interactive "P")
47 (let ((lang (if arg
48 (read-language-name 'tutorial "Language: " "English")
49 (if (get-language-info current-language-environment 'tutorial)
50 current-language-environment
51 "English")))
52 file filename)
53 (setq filename (get-language-info lang 'tutorial))
54 (setq file (expand-file-name (concat "~/" filename)))
55 (delete-other-windows)
56 (if (get-file-buffer file)
57 (switch-to-buffer (get-file-buffer file))
58 (switch-to-buffer (create-file-buffer file))
59 (setq buffer-file-name file)
60 (setq default-directory (expand-file-name "~/"))
61 (setq buffer-auto-save-file-name nil)
62 (insert-file-contents (expand-file-name filename data-directory))
63 (goto-char (point-min))
64 (search-forward "\n<<")
65 (beginning-of-line)
66 ;; Convert the <<...>> line to the proper [...] line,
67 ;; or just delete the <<...>> line if a [...] line follows.
68 (cond ((save-excursion
69 (forward-line 1)
70 (looking-at "\\["))
71 (delete-region (point) (progn (forward-line 1) (point))))
72 ((looking-at "<<Blank lines inserted.*>>")
73 (replace-match "[Middle of page left blank for didactic purposes. Text continues below]"))
74 (t
75 (looking-at "<<")
76 (replace-match "[")
77 (search-forward ">>")
78 (replace-match "]")))
79 (beginning-of-line)
80 (let ((n (- (window-height (selected-window))
81 (count-lines (point-min) (point))
82 6)))
83 (if (< n 8)
84 (progn
85 ;; For a short gap, we don't need the [...] line,
86 ;; so delete it.
87 (delete-region (point) (progn (end-of-line) (point)))
88 (newline n))
89 ;; Some people get confused by the large gap.
90 (newline (/ n 2))
91
92 ;; Skip the [...] line (don't delete it).
93 (forward-line 1)
94 (newline (- n (/ n 2)))))
95 (goto-char (point-min))
96 (set-buffer-modified-p nil))))
97
98 ;;;###autoload
99 (defun locate-library (library &optional nosuffix path interactive-call)
100 "Show the precise file name of Emacs library LIBRARY.
101 This command searches the directories in `load-path' like `M-x load-library'
102 to find the file that `M-x load-library RET LIBRARY RET' would load.
103 Optional second arg NOSUFFIX non-nil means don't add suffixes `load-suffixes'
104 to the specified name LIBRARY.
105
106 If the optional third arg PATH is specified, that list of directories
107 is used instead of `load-path'.
108
109 When called from a program, the file name is normaly returned as a
110 string. When run interactively, the argument INTERACTIVE-CALL is t,
111 and the file name is displayed in the echo area."
112 (interactive (list (read-string "Locate library: ")
113 nil nil
114 t))
115 (catch 'answer
116 (dolist (dir (or path load-path))
117 (dolist (suf (append (unless nosuffix load-suffixes) '("")))
118 (let ((try (expand-file-name (concat library suf) dir)))
119 (and (file-readable-p try)
120 (null (file-directory-p try))
121 (progn
122 (if interactive-call
123 (message "Library is file %s" (abbreviate-file-name try)))
124 (throw 'answer try))))))
125 (if interactive-call
126 (message "No library %s in search path" library))
127 nil))
128
129 \f
130 ;; Functions
131
132 ;;;###autoload
133 (defun describe-function (function)
134 "Display the full documentation of FUNCTION (a symbol)."
135 (interactive
136 (let ((fn (function-called-at-point))
137 (enable-recursive-minibuffers t)
138 val)
139 (setq val (completing-read (if fn
140 (format "Describe function (default %s): " fn)
141 "Describe function: ")
142 obarray 'fboundp t nil nil (symbol-name fn)))
143 (list (if (equal val "")
144 fn (intern val)))))
145 (if (null function)
146 (message "You didn't specify a function")
147 (help-setup-xref (list #'describe-function function) (interactive-p))
148 (save-excursion
149 (with-output-to-temp-buffer (help-buffer)
150 (prin1 function)
151 ;; Use " is " instead of a colon so that
152 ;; it is easier to get out the function name using forward-sexp.
153 (princ " is ")
154 (describe-function-1 function)
155 (print-help-return-message)
156 (with-current-buffer standard-output
157 ;; Return the text we displayed.
158 (buffer-string))))))
159
160 ;;;###autoload
161 (defun describe-function-1 (function)
162 (let* ((def (if (symbolp function)
163 (symbol-function function)
164 function))
165 file-name string
166 (beg (if (commandp def) "an interactive " "a ")))
167 (setq string
168 (cond ((or (stringp def)
169 (vectorp def))
170 "a keyboard macro")
171 ((subrp def)
172 (if (eq 'unevalled (cdr (subr-arity def)))
173 (concat beg "special form")
174 (concat beg "built-in function")))
175 ((byte-code-function-p def)
176 (concat beg "compiled Lisp function"))
177 ((symbolp def)
178 (while (symbolp (symbol-function def))
179 (setq def (symbol-function def)))
180 (format "an alias for `%s'" def))
181 ((eq (car-safe def) 'lambda)
182 (concat beg "Lisp function"))
183 ((eq (car-safe def) 'macro)
184 "a Lisp macro")
185 ((eq (car-safe def) 'autoload)
186 (setq file-name (nth 1 def))
187 (format "%s autoloaded %s"
188 (if (commandp def) "an interactive" "an")
189 (if (eq (nth 4 def) 'keymap) "keymap"
190 (if (nth 4 def) "Lisp macro" "Lisp function"))
191 ))
192 ;; perhaps use keymapp here instead
193 ((eq (car-safe def) 'keymap)
194 (let ((is-full nil)
195 (elts (cdr-safe def)))
196 (while elts
197 (if (char-table-p (car-safe elts))
198 (setq is-full t
199 elts nil))
200 (setq elts (cdr-safe elts)))
201 (if is-full
202 "a full keymap"
203 "a sparse keymap")))
204 (t "")))
205 (princ string)
206 (with-current-buffer standard-output
207 (save-excursion
208 (save-match-data
209 (if (re-search-backward "alias for `\\([^`']+\\)'" nil t)
210 (help-xref-button 1 'help-function def)))))
211 (or file-name
212 (setq file-name (symbol-file function)))
213 (when (equal file-name "loaddefs.el")
214 ;; Find the real def site of the preloaded function.
215 ;; This is necessary only for defaliases.
216 (let ((location
217 (condition-case nil
218 (find-function-search-for-symbol function nil "loaddefs.el")
219 (error nil))))
220 (when location
221 (with-current-buffer (car location)
222 (goto-char (cdr location))
223 (when (re-search-backward
224 "^;;; Generated autoloads from \\(.*\\)" nil t)
225 (setq file-name (match-string 1)))))))
226 (cond
227 (file-name
228 (princ " in `")
229 ;; We used to add .el to the file name,
230 ;; but that's completely wrong when the user used load-file.
231 (princ file-name)
232 (princ "'")
233 ;; Make a hyperlink to the library.
234 (with-current-buffer standard-output
235 (save-excursion
236 (re-search-backward "`\\([^`']+\\)'" nil t)
237 (help-xref-button 1 'help-function-def function file-name)))))
238 (princ ".")
239 (terpri)
240 (when (commandp function)
241 (let* ((remapped (remap-command function))
242 (keys (where-is-internal
243 (or remapped function) overriding-local-map nil nil)))
244 (when remapped
245 (princ "It is remapped to `")
246 (princ (symbol-name remapped))
247 (princ "'"))
248 (when keys
249 (princ (if remapped " which is bound to " "It is bound to "))
250 ;; FIXME: This list can be very long (f.ex. for self-insert-command).
251 (princ (mapconcat 'key-description keys ", ")))
252 (when (or remapped keys)
253 (princ ".")
254 (terpri))))
255 ;; Handle symbols aliased to other symbols.
256 (setq def (indirect-function def))
257 ;; If definition is a macro, find the function inside it.
258 (if (eq (car-safe def) 'macro)
259 (setq def (cdr def)))
260 (let ((arglist (cond ((byte-code-function-p def)
261 (car (append def nil)))
262 ((eq (car-safe def) 'lambda)
263 (nth 1 def))
264 ((and (eq (car-safe def) 'autoload)
265 (not (eq (nth 4 def) 'keymap)))
266 (concat "[Arg list not available until "
267 "function definition is loaded.]"))
268 (t t))))
269 (cond ((listp arglist)
270 (princ (cons (if (symbolp function) function "anonymous")
271 (mapcar (lambda (arg)
272 (if (memq arg '(&optional &rest))
273 arg
274 (intern (upcase (symbol-name arg)))))
275 arglist)))
276 (terpri))
277 ((stringp arglist)
278 (princ arglist)
279 (terpri))))
280 (let ((obsolete (get function 'byte-obsolete-info)))
281 (when obsolete
282 (terpri)
283 (princ "This function is obsolete")
284 (if (nth 2 obsolete) (princ (format " since %s" (nth 2 obsolete))))
285 (princ ";") (terpri)
286 (princ (if (stringp (car obsolete)) (car obsolete)
287 (format "use `%s' instead." (car obsolete))))
288 (terpri)))
289 (let ((doc (documentation function)))
290 (if doc
291 (progn (terpri)
292 (princ doc)
293 (if (subrp def)
294 (with-current-buffer standard-output
295 (beginning-of-line)
296 ;; Builtins get the calling sequence at the end of
297 ;; the doc string. Move it to the same place as
298 ;; for other functions.
299
300 ;; In cases where `function' has been fset to a
301 ;; subr we can't search for function's name in
302 ;; the doc string. Kluge round that using the
303 ;; printed representation. The arg list then
304 ;; shows the wrong function name, but that
305 ;; might be a useful hint.
306 (let* ((rep (prin1-to-string def))
307 (name (progn
308 (string-match " \\([^ ]+\\)>$" rep)
309 (match-string 1 rep))))
310 (if (looking-at (format "(%s[ )]" (regexp-quote name)))
311 (let ((start (point-marker)))
312 (goto-char (point-min))
313 (forward-paragraph)
314 (insert-buffer-substring (current-buffer) start)
315 (insert ?\n)
316 (delete-region (1- start) (point-max)))
317 (goto-char (point-min))
318 (forward-paragraph)
319 (insert
320 "[Missing arglist. Please make a bug report.]\n")))
321 (goto-char (point-max)))))
322 (princ "Not documented.")))))
323
324 \f
325 ;; Variables
326
327 ;;;###autoload
328 (defun variable-at-point ()
329 "Return the bound variable symbol found around point.
330 Return 0 if there is no such symbol."
331 (condition-case ()
332 (with-syntax-table emacs-lisp-mode-syntax-table
333 (save-excursion
334 (or (not (zerop (skip-syntax-backward "_w")))
335 (eq (char-syntax (following-char)) ?w)
336 (eq (char-syntax (following-char)) ?_)
337 (forward-sexp -1))
338 (skip-chars-forward "'")
339 (let ((obj (read (current-buffer))))
340 (or (and (symbolp obj) (boundp obj) obj)
341 0))))
342 (error 0)))
343
344 ;;;###autoload
345 (defun describe-variable (variable &optional buffer)
346 "Display the full documentation of VARIABLE (a symbol).
347 Returns the documentation as a string, also.
348 If VARIABLE has a buffer-local value in BUFFER (default to the current buffer),
349 it is displayed along with the global value."
350 (interactive
351 (let ((v (variable-at-point))
352 (enable-recursive-minibuffers t)
353 val)
354 (setq val (completing-read (if (symbolp v)
355 (format
356 "Describe variable (default %s): " v)
357 "Describe variable: ")
358 obarray 'boundp t nil nil
359 (if (symbolp v) (symbol-name v))))
360 (list (if (equal val "")
361 v (intern val)))))
362 (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
363 (if (not (symbolp variable))
364 (message "You did not specify a variable")
365 (save-excursion
366 (let (valvoid)
367 (help-setup-xref (list #'describe-variable variable buffer)
368 (interactive-p))
369 (with-output-to-temp-buffer (help-buffer)
370 (with-current-buffer buffer
371 (prin1 variable)
372 (if (not (boundp variable))
373 (progn
374 (princ " is void")
375 (setq valvoid t))
376 (let ((val (symbol-value variable)))
377 (with-current-buffer standard-output
378 (princ "'s value is ")
379 (terpri)
380 (let ((from (point)))
381 (pp val)
382 (help-xref-on-pp from (point))
383 (if (< (point) (+ from 20))
384 (save-excursion
385 (goto-char from)
386 (delete-char -1)))))))
387 (terpri)
388 (when (local-variable-p variable)
389 (princ (format "Local in buffer %s; " (buffer-name)))
390 (if (not (default-boundp variable))
391 (princ "globally void")
392 (let ((val (default-value variable)))
393 (with-current-buffer standard-output
394 (princ "global value is ")
395 (terpri)
396 ;; Fixme: pp can take an age if you happen to
397 ;; ask for a very large expression. We should
398 ;; probably print it raw once and check it's a
399 ;; sensible size before prettyprinting. -- fx
400 (let ((from (point)))
401 (pp val)
402 (help-xref-on-pp from (point))
403 (if (< (point) (+ from 20))
404 (delete-region (1- from) from))))))
405 (terpri))
406 (terpri)
407 (with-current-buffer standard-output
408 (when (> (count-lines (point-min) (point-max)) 10)
409 ;; Note that setting the syntax table like below
410 ;; makes forward-sexp move over a `'s' at the end
411 ;; of a symbol.
412 (set-syntax-table emacs-lisp-mode-syntax-table)
413 (goto-char (point-min))
414 (if valvoid
415 (forward-line 1)
416 (forward-sexp 1)
417 (delete-region (point) (progn (end-of-line) (point)))
418 (insert " value is shown below.\n\n")
419 (save-excursion
420 (insert "\n\nValue:"))))
421 ;; Add a note for variables that have been make-var-buffer-local.
422 (when (and (local-variable-if-set-p variable)
423 (or (not (local-variable-p variable))
424 (with-temp-buffer
425 (local-variable-if-set-p variable))))
426 (save-excursion
427 (forward-line -1)
428 (insert "Automatically becomes buffer-local when set in any fashion.\n"))))
429 (princ "Documentation:")
430 (terpri)
431 (let ((obsolete (get variable 'byte-obsolete-variable)))
432 (when obsolete
433 (princ "This variable is obsolete")
434 (if (cdr obsolete) (princ (format " since %s" (cdr obsolete))))
435 (princ "; ") (terpri)
436 (princ (if (stringp (car obsolete)) (car obsolete)
437 (format "use `%s' instead." (car obsolete))))
438 (terpri)))
439 (let ((doc (documentation-property variable 'variable-documentation)))
440 (princ (or doc "Not documented as a variable.")))
441
442 ;; Make a link to customize if this variable can be customized.
443 ;; Note, it is not reliable to test only for a custom-type property
444 ;; because those are only present after the var's definition
445 ;; has been loaded.
446 (if (or (get variable 'custom-type) ; after defcustom
447 (get variable 'custom-loads) ; from loaddefs.el
448 (get variable 'standard-value)) ; from cus-start.el
449 (let ((customize-label "customize"))
450 (terpri)
451 (terpri)
452 (princ (concat "You can " customize-label " this variable."))
453 (with-current-buffer standard-output
454 (save-excursion
455 (re-search-backward
456 (concat "\\(" customize-label "\\)") nil t)
457 (help-xref-button 1 'help-customize-variable variable)))))
458 ;; Make a hyperlink to the library if appropriate. (Don't
459 ;; change the format of the buffer's initial line in case
460 ;; anything expects the current format.)
461 (let ((file-name (symbol-file variable)))
462 (when (equal file-name "loaddefs.el")
463 ;; Find the real def site of the preloaded variable.
464 (let ((location
465 (condition-case nil
466 (find-variable-noselect variable file-name)
467 (error nil))))
468 (when location
469 (with-current-buffer (car location)
470 (goto-char (cdr location))
471 (when (re-search-backward
472 "^;;; Generated autoloads from \\(.*\\)" nil t)
473 (setq file-name (match-string 1)))))))
474 (when file-name
475 (princ "\n\nDefined in `")
476 (princ file-name)
477 (princ "'.")
478 (with-current-buffer standard-output
479 (save-excursion
480 (re-search-backward "`\\([^`']+\\)'" nil t)
481 (help-xref-button 1 'help-variable-def
482 variable file-name)))))
483
484 (print-help-return-message)
485 (save-excursion
486 (set-buffer standard-output)
487 ;; Return the text we displayed.
488 (buffer-string))))))))
489
490
491 ;;;###autoload
492 (defun describe-syntax (&optional buffer)
493 "Describe the syntax specifications in the syntax table of BUFFER.
494 The descriptions are inserted in a help buffer, which is then displayed.
495 BUFFER defaults to the current buffer."
496 (interactive)
497 (setq buffer (or buffer (current-buffer)))
498 (help-setup-xref (list #'describe-syntax buffer) (interactive-p))
499 (with-output-to-temp-buffer (help-buffer)
500 (let ((table (with-current-buffer buffer (syntax-table))))
501 (with-current-buffer standard-output
502 (describe-vector table 'internal-describe-syntax-value)
503 (while (setq table (char-table-parent table))
504 (insert "\nThe parent syntax table is:")
505 (describe-vector table 'internal-describe-syntax-value))))))
506
507 (defun help-describe-category-set (value)
508 (insert (cond
509 ((null value) "default")
510 ((char-table-p value) "deeper char-table ...")
511 (t (condition-case err
512 (category-set-mnemonics value)
513 (error "invalid"))))))
514
515 ;;;###autoload
516 (defun describe-categories (&optional buffer)
517 "Describe the category specifications in the current category table.
518 The descriptions are inserted in a buffer, which is then displayed."
519 (interactive)
520 (setq buffer (or buffer (current-buffer)))
521 (help-setup-xref (list #'describe-categories buffer) (interactive-p))
522 (with-output-to-temp-buffer (help-buffer)
523 (let ((table (with-current-buffer buffer (category-table))))
524 (with-current-buffer standard-output
525 (describe-vector table 'help-describe-category-set)
526 (let ((docs (char-table-extra-slot table 0)))
527 (if (or (not (vectorp docs)) (/= (length docs) 95))
528 (insert "Invalid first extra slot in this char table\n")
529 (insert "Meanings of mnemonic characters are:\n")
530 (dotimes (i 95)
531 (let ((elt (aref docs i)))
532 (when elt
533 (insert (+ i ?\ ) ": " elt "\n"))))
534 (while (setq table (char-table-parent table))
535 (insert "\nThe parent category table is:")
536 (describe-vector table 'help-describe-category-set))))))))
537
538 (provide 'help-fns)
539
540 ;;; help-fns.el ends here