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