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