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.]")))
310 (let ((obsolete (get function
'byte-obsolete-info
)))
313 (princ "This function is obsolete")
314 (if (nth 2 obsolete
) (princ (format " since %s" (nth 2 obsolete
))))
316 (princ (if (stringp (car obsolete
)) (car obsolete
)
317 (format "use `%s' instead." (car obsolete
))))
320 (princ (or doc
"Not documented.")))))
326 (defun variable-at-point ()
327 "Return the bound variable symbol found around point.
328 Return 0 if there is no such symbol."
330 (with-syntax-table emacs-lisp-mode-syntax-table
332 (or (not (zerop (skip-syntax-backward "_w")))
333 (eq (char-syntax (following-char)) ?w
)
334 (eq (char-syntax (following-char)) ?_
)
336 (skip-chars-forward "'")
337 (let ((obj (read (current-buffer))))
338 (or (and (symbolp obj
) (boundp obj
) obj
)
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."
349 (let ((v (variable-at-point))
350 (enable-recursive-minibuffers t
)
352 (setq val
(completing-read (if (symbolp v
)
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
"")
360 (unless (buffer-live-p buffer
) (setq buffer
(current-buffer)))
361 (if (not (symbolp variable
))
362 (message "You did not specify a variable")
365 (help-setup-xref (list #'describe-variable variable buffer
)
367 (with-output-to-temp-buffer (help-buffer)
368 (with-current-buffer buffer
370 (if (not (boundp variable
))
374 (let ((val (symbol-value variable
)))
375 (with-current-buffer standard-output
376 (princ "'s value is ")
378 (let ((from (point)))
380 (help-xref-on-pp from
(point))
381 (if (< (point) (+ from
20))
384 (delete-char -
1)))))))
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 ")
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)))
400 (help-xref-on-pp from
(point))
401 (if (< (point) (+ from
20))
402 (delete-region (1- from
) from
))))))
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
410 (set-syntax-table emacs-lisp-mode-syntax-table
)
411 (goto-char (point-min))
415 (delete-region (point) (progn (end-of-line) (point)))
416 (insert " value is shown below.\n\n")
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
))
423 (local-variable-if-set-p variable
))))
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
)
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
))
439 (princ "This variable is obsolete")
440 (if (cdr obsolete
) (princ (format " since %s" (cdr obsolete
))))
442 (princ (if (stringp (car obsolete
)) (car obsolete
)
443 (format "use `%s' instead." (car obsolete
))))
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
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"))
457 (princ (concat "You can " customize-label
" this variable."))
458 (with-current-buffer standard-output
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.
471 (find-variable-noselect variable file-name
)
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)))))))
480 (princ "\n\nDefined in `")
483 (with-current-buffer standard-output
485 (re-search-backward "`\\([^`']+\\)'" nil t
)
486 (help-xref-button 1 'help-variable-def
487 variable file-name
)))))
489 (print-help-return-message)
491 (set-buffer standard-output
)
492 ;; Return the text we displayed.
493 (buffer-string))))))))
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."
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
))))))
512 (defun help-describe-category-set (value)
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"))))))
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."
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")
536 (let ((elt (aref docs i
)))
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
))))))))
545 ;;; help-fns.el ends here