1 ;;; help-fns.el --- Complex help functions
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002
4 ;; Free Software Foundation, Inc.
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)
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.
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
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."
49 ;; Display a completion list right away
51 (with-output-to-temp-buffer "*Completions*"
52 (display-completion-list
53 (all-completions "" language-info-alist
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
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<<")
74 ;; Convert the <<...>> line to the proper [...] line,
75 ;; or just delete the <<...>> line if a [...] line follows.
76 (cond ((save-excursion
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]"))
88 (let ((n (- (window-height (selected-window))
89 (count-lines (point-min) (point))
93 ;; For a short gap, we don't need the [...] line,
95 (delete-region (point) (progn (end-of-line) (point)))
97 ;; Some people get confused by the large gap.
100 ;; Skip the [...] line (don't delete it).
102 (newline (- n
(/ n
2)))))
103 (goto-char (point-min))
104 (set-buffer-modified-p nil
))))
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: ")
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
))
131 (message "Library is file %s" (abbreviate-file-name try
)))
132 (throw 'answer try
))))))
134 (message "No library %s in search path" library
))
141 (defun describe-function (function)
142 "Display the full documentation of FUNCTION (a symbol)."
144 (let ((fn (function-called-at-point))
145 (enable-recursive-minibuffers t
)
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
"")
154 (message "You didn't specify a function")
155 (help-setup-xref (list #'describe-function function
) (interactive-p))
157 (with-output-to-temp-buffer (help-buffer)
159 ;; Use " is " instead of a colon so that
160 ;; it is easier to get out the function name using forward-sexp.
162 (describe-function-1 function
)
163 (print-help-return-message)
164 (with-current-buffer standard-output
165 ;; Return the text we displayed.
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)
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.]")
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
))))))
201 (defun describe-function-1 (function)
202 (let* ((def (if (symbolp function
)
203 (symbol-function function
)
206 (beg (if (commandp def
) "an interactive " "a ")))
208 (cond ((or (stringp 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"))
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
)
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
)
235 (elts (cdr-safe def
)))
237 (if (char-table-p (car-safe elts
))
240 (setq elts
(cdr-safe elts
)))
246 (with-current-buffer standard-output
249 (if (re-search-backward "alias for `\\([^`']+\\)'" nil t
)
250 (help-xref-button 1 'help-function def
)))))
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.
258 (find-function-search-for-symbol function nil
"loaddefs.el")
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)))))))
269 ;; We used to add .el to the file name,
270 ;; but that's completely wrong when the user used load-file.
273 ;; Make a hyperlink to the library.
274 (with-current-buffer standard-output
276 (re-search-backward "`\\([^`']+\\)'" nil t
)
277 (help-xref-button 1 'help-function-def function file-name
)))))
280 (when (commandp function
)
281 (let* ((remapped (remap-command function
))
282 (keys (where-is-internal
283 (or remapped function
) overriding-local-map nil nil
)))
285 (princ "It is remapped to `")
286 (princ (symbol-name remapped
))
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
)
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
))
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.]")))
311 ;; function might be a lambda construct.
313 (get function
'byte-obsolete-info
))))
316 (princ "This function is obsolete")
317 (if (nth 2 obsolete
) (princ (format " since %s" (nth 2 obsolete
))))
319 (princ (if (stringp (car obsolete
)) (car obsolete
)
320 (format "use `%s' instead." (car obsolete
))))
323 (princ (or doc
"Not documented.")))))
329 (defun variable-at-point ()
330 "Return the bound variable symbol found around point.
331 Return 0 if there is no such symbol."
333 (with-syntax-table emacs-lisp-mode-syntax-table
335 (or (not (zerop (skip-syntax-backward "_w")))
336 (eq (char-syntax (following-char)) ?w
)
337 (eq (char-syntax (following-char)) ?_
)
339 (skip-chars-forward "'")
340 (let ((obj (read (current-buffer))))
341 (or (and (symbolp obj
) (boundp obj
) obj
)
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."
352 (let ((v (variable-at-point))
353 (enable-recursive-minibuffers t
)
355 (setq val
(completing-read (if (symbolp v
)
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
"")
363 (unless (buffer-live-p buffer
) (setq buffer
(current-buffer)))
364 (if (not (symbolp variable
))
365 (message "You did not specify a variable")
368 (help-setup-xref (list #'describe-variable variable buffer
)
370 (with-output-to-temp-buffer (help-buffer)
371 (with-current-buffer buffer
373 (if (not (boundp variable
))
377 (let ((val (symbol-value variable
)))
378 (with-current-buffer standard-output
379 (princ "'s value is ")
381 (let ((from (point)))
383 (help-xref-on-pp from
(point))
384 (if (< (point) (+ from
20))
387 (delete-char -
1)))))))
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 ")
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)))
403 (help-xref-on-pp from
(point))
404 (if (< (point) (+ from
20))
405 (delete-region (1- from
) from
))))))
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
413 (set-syntax-table emacs-lisp-mode-syntax-table
)
414 (goto-char (point-min))
418 (delete-region (point) (progn (end-of-line) (point)))
419 (insert " value is shown below.\n\n")
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
))
426 (local-variable-if-set-p variable
))))
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
)
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
))
442 (princ "This variable is obsolete")
443 (if (cdr obsolete
) (princ (format " since %s" (cdr obsolete
))))
445 (princ (if (stringp (car obsolete
)) (car obsolete
)
446 (format "use `%s' instead." (car obsolete
))))
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
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"))
460 (princ (concat "You can " customize-label
" this variable."))
461 (with-current-buffer standard-output
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.
474 (find-variable-noselect variable file-name
)
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)))))))
483 (princ "\n\nDefined in `")
486 (with-current-buffer standard-output
488 (re-search-backward "`\\([^`']+\\)'" nil t
)
489 (help-xref-button 1 'help-variable-def
490 variable file-name
)))))
492 (print-help-return-message)
494 (set-buffer standard-output
)
495 ;; Return the text we displayed.
496 (buffer-string))))))))
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."
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
))))))
515 (defun help-describe-category-set (value)
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"))))))
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."
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")
539 (let ((elt (aref docs i
)))
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
))))))))
548 ;;; help-fns.el ends here