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