(describe-function-1): Skip arglist note if
[emacs.git] / lisp / help-fns.el
blobba543070bc99c518b87a48a26b9ab6fa4bf8371a
1 ;;; help-fns.el --- Complex help functions
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002
4 ;; Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: help, internal
9 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;;; Commentary:
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
34 ;;; Code:
36 (require 'help-mode)
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]"))
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))
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))))
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.
114 If the optional third arg PATH is specified, that list of directories
115 is used instead of `load-path'.
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
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))
138 ;; Functions
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))))))
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))))))
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)))
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)))
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"))
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 ;; If definition is a keymap, skip arglist note.
304 (unless (keymapp def)
305 (princ (cond
306 ((listp arglist) (help-make-usage function arglist))
307 ((stringp arglist) arglist)
308 ((and doc (subrp def) (setq usage (help-split-fundoc doc def)))
309 (setq doc (cdr usage)) (car usage))
310 (t "[Missing arglist. Please make a bug report.]")))
311 (terpri))
312 (let ((obsolete (and
313 ;; function might be a lambda construct.
314 (symbolp function)
315 (get function 'byte-obsolete-info))))
316 (when obsolete
317 (terpri)
318 (princ "This function is obsolete")
319 (if (nth 2 obsolete) (princ (format " since %s" (nth 2 obsolete))))
320 (princ ";") (terpri)
321 (princ (if (stringp (car obsolete)) (car obsolete)
322 (format "use `%s' instead." (car obsolete))))
323 (terpri)))
324 (terpri)
325 (princ (or doc "Not documented.")))))
328 ;; Variables
330 ;;;###autoload
331 (defun variable-at-point ()
332 "Return the bound variable symbol found around point.
333 Return 0 if there is no such symbol."
334 (condition-case ()
335 (with-syntax-table emacs-lisp-mode-syntax-table
336 (save-excursion
337 (or (not (zerop (skip-syntax-backward "_w")))
338 (eq (char-syntax (following-char)) ?w)
339 (eq (char-syntax (following-char)) ?_)
340 (forward-sexp -1))
341 (skip-chars-forward "'")
342 (let ((obj (read (current-buffer))))
343 (or (and (symbolp obj) (boundp obj) obj)
344 0))))
345 (error 0)))
347 ;;;###autoload
348 (defun describe-variable (variable &optional buffer)
349 "Display the full documentation of VARIABLE (a symbol).
350 Returns the documentation as a string, also.
351 If VARIABLE has a buffer-local value in BUFFER (default to the current buffer),
352 it is displayed along with the global value."
353 (interactive
354 (let ((v (variable-at-point))
355 (enable-recursive-minibuffers t)
356 val)
357 (setq val (completing-read (if (symbolp v)
358 (format
359 "Describe variable (default %s): " v)
360 "Describe variable: ")
361 obarray 'boundp t nil nil
362 (if (symbolp v) (symbol-name v))))
363 (list (if (equal val "")
364 v (intern val)))))
365 (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
366 (if (not (symbolp variable))
367 (message "You did not specify a variable")
368 (save-excursion
369 (let (valvoid)
370 (help-setup-xref (list #'describe-variable variable buffer)
371 (interactive-p))
372 (with-output-to-temp-buffer (help-buffer)
373 (with-current-buffer buffer
374 (prin1 variable)
375 (if (not (boundp variable))
376 (progn
377 (princ " is void")
378 (setq valvoid t))
379 (let ((val (symbol-value variable)))
380 (with-current-buffer standard-output
381 (princ "'s value is ")
382 (terpri)
383 (let ((from (point)))
384 (pp val)
385 (help-xref-on-pp from (point))
386 (if (< (point) (+ from 20))
387 (save-excursion
388 (goto-char from)
389 (delete-char -1)))))))
390 (terpri)
391 (when (local-variable-p variable)
392 (princ (format "Local in buffer %s; " (buffer-name)))
393 (if (not (default-boundp variable))
394 (princ "globally void")
395 (let ((val (default-value variable)))
396 (with-current-buffer standard-output
397 (princ "global value is ")
398 (terpri)
399 ;; Fixme: pp can take an age if you happen to
400 ;; ask for a very large expression. We should
401 ;; probably print it raw once and check it's a
402 ;; sensible size before prettyprinting. -- fx
403 (let ((from (point)))
404 (pp val)
405 (help-xref-on-pp from (point))
406 (if (< (point) (+ from 20))
407 (delete-region (1- from) from))))))
408 (terpri))
409 (terpri)
410 (with-current-buffer standard-output
411 (when (> (count-lines (point-min) (point-max)) 10)
412 ;; Note that setting the syntax table like below
413 ;; makes forward-sexp move over a `'s' at the end
414 ;; of a symbol.
415 (set-syntax-table emacs-lisp-mode-syntax-table)
416 (goto-char (point-min))
417 (if valvoid
418 (forward-line 1)
419 (forward-sexp 1)
420 (delete-region (point) (progn (end-of-line) (point)))
421 (insert " value is shown below.\n\n")
422 (save-excursion
423 (insert "\n\nValue:"))))
424 ;; Add a note for variables that have been make-var-buffer-local.
425 (when (and (local-variable-if-set-p variable)
426 (or (not (local-variable-p variable))
427 (with-temp-buffer
428 (local-variable-if-set-p variable))))
429 (save-excursion
430 (forward-line -1)
431 (insert "Automatically becomes buffer-local when set in any fashion.\n"))))
432 ;; Mention if it's an alias
433 (let* ((alias (condition-case nil
434 (indirect-variable variable)
435 (error variable)))
436 (obsolete (get variable 'byte-obsolete-variable))
437 (doc (or (documentation-property variable 'variable-documentation)
438 (documentation-property alias 'variable-documentation))))
439 (unless (eq alias variable)
440 (princ (format "This variable is an alias for `%s'." alias))
441 (terpri)
442 (terpri))
443 (when obsolete
444 (princ "This variable is obsolete")
445 (if (cdr obsolete) (princ (format " since %s" (cdr obsolete))))
446 (princ ";") (terpri)
447 (princ (if (stringp (car obsolete)) (car obsolete)
448 (format "use `%s' instead." (car obsolete))))
449 (terpri)
450 (terpri))
451 (princ (or doc "Not documented as a variable.")))
452 ;; Make a link to customize if this variable can be customized.
453 ;; Note, it is not reliable to test only for a custom-type property
454 ;; because those are only present after the var's definition
455 ;; has been loaded.
456 (if (or (get variable 'custom-type) ; after defcustom
457 (get variable 'custom-loads) ; from loaddefs.el
458 (get variable 'standard-value)) ; from cus-start.el
459 (let ((customize-label "customize"))
460 (terpri)
461 (terpri)
462 (princ (concat "You can " customize-label " this variable."))
463 (with-current-buffer standard-output
464 (save-excursion
465 (re-search-backward
466 (concat "\\(" customize-label "\\)") nil t)
467 (help-xref-button 1 'help-customize-variable variable)))))
468 ;; Make a hyperlink to the library if appropriate. (Don't
469 ;; change the format of the buffer's initial line in case
470 ;; anything expects the current format.)
471 (let ((file-name (symbol-file variable)))
472 (when (equal file-name "loaddefs.el")
473 ;; Find the real def site of the preloaded variable.
474 (let ((location
475 (condition-case nil
476 (find-variable-noselect variable file-name)
477 (error nil))))
478 (when location
479 (with-current-buffer (car location)
480 (goto-char (cdr location))
481 (when (re-search-backward
482 "^;;; Generated autoloads from \\(.*\\)" nil t)
483 (setq file-name (match-string 1)))))))
484 (when file-name
485 (princ "\n\nDefined in `")
486 (princ file-name)
487 (princ "'.")
488 (with-current-buffer standard-output
489 (save-excursion
490 (re-search-backward "`\\([^`']+\\)'" nil t)
491 (help-xref-button 1 'help-variable-def
492 variable file-name)))))
494 (print-help-return-message)
495 (save-excursion
496 (set-buffer standard-output)
497 ;; Return the text we displayed.
498 (buffer-string))))))))
501 ;;;###autoload
502 (defun describe-syntax (&optional buffer)
503 "Describe the syntax specifications in the syntax table of BUFFER.
504 The descriptions are inserted in a help buffer, which is then displayed.
505 BUFFER defaults to the current buffer."
506 (interactive)
507 (setq buffer (or buffer (current-buffer)))
508 (help-setup-xref (list #'describe-syntax buffer) (interactive-p))
509 (with-output-to-temp-buffer (help-buffer)
510 (let ((table (with-current-buffer buffer (syntax-table))))
511 (with-current-buffer standard-output
512 (describe-vector table 'internal-describe-syntax-value)
513 (while (setq table (char-table-parent table))
514 (insert "\nThe parent syntax table is:")
515 (describe-vector table 'internal-describe-syntax-value))))))
517 (defun help-describe-category-set (value)
518 (insert (cond
519 ((null value) "default")
520 ((char-table-p value) "deeper char-table ...")
521 (t (condition-case err
522 (category-set-mnemonics value)
523 (error "invalid"))))))
525 ;;;###autoload
526 (defun describe-categories (&optional buffer)
527 "Describe the category specifications in the current category table.
528 The descriptions are inserted in a buffer, which is then displayed."
529 (interactive)
530 (setq buffer (or buffer (current-buffer)))
531 (help-setup-xref (list #'describe-categories buffer) (interactive-p))
532 (with-output-to-temp-buffer (help-buffer)
533 (let ((table (with-current-buffer buffer (category-table))))
534 (with-current-buffer standard-output
535 (describe-vector table 'help-describe-category-set)
536 (let ((docs (char-table-extra-slot table 0)))
537 (if (or (not (vectorp docs)) (/= (length docs) 95))
538 (insert "Invalid first extra slot in this char table\n")
539 (insert "Meanings of mnemonic characters are:\n")
540 (dotimes (i 95)
541 (let ((elt (aref docs i)))
542 (when elt
543 (insert (+ i ?\ ) ": " elt "\n"))))
544 (while (setq table (char-table-parent table))
545 (insert "\nThe parent category table is:")
546 (describe-vector table 'help-describe-category-set))))))))
548 (provide 'help-fns)
550 ;;; help-fns.el ends here