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."
48 (let (completion-buffer)
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 (setq completion-buffer standard-output
))
57 ;; Arrange to set completion-reference-buffer
58 ;; in *Completions* to point to the minibuffer,
59 ;; after entering the minibuffer.
60 (let ((minibuffer-setup-hook minibuffer-setup-hook
))
61 (add-hook 'minibuffer-setup-hook
63 (let ((mini (current-buffer)))
64 (with-current-buffer completion-buffer
65 (make-local-variable 'completion-reference-buffer
)
66 (setq completion-reference-buffer
68 (read-language-name 'tutorial
"Language: " "English")))
69 (if (get-language-info current-language-environment
'tutorial
)
70 current-language-environment
73 (setq filename
(get-language-info lang
'tutorial
))
74 (setq file
(expand-file-name (concat "~/" filename
)))
75 (delete-other-windows)
76 (if (get-file-buffer file
)
77 (switch-to-buffer (get-file-buffer file
))
78 (switch-to-buffer (create-file-buffer file
))
79 (setq buffer-file-name file
)
80 (setq default-directory
(expand-file-name "~/"))
81 (setq buffer-auto-save-file-name nil
)
82 (insert-file-contents (expand-file-name filename data-directory
))
83 (goto-char (point-min))
84 (search-forward "\n<<")
86 ;; Convert the <<...>> line to the proper [...] line,
87 ;; or just delete the <<...>> line if a [...] line follows.
88 (cond ((save-excursion
91 (delete-region (point) (progn (forward-line 1) (point))))
92 ((looking-at "<<Blank lines inserted.*>>")
93 (replace-match "[Middle of page left blank for didactic purposes. Text continues below]"))
100 (let ((n (- (window-height (selected-window))
101 (count-lines (point-min) (point))
105 ;; For a short gap, we don't need the [...] line,
107 (delete-region (point) (progn (end-of-line) (point)))
109 ;; Some people get confused by the large gap.
112 ;; Skip the [...] line (don't delete it).
114 (newline (- n
(/ n
2)))))
115 (goto-char (point-min))
116 (set-buffer-modified-p nil
))))
119 (defun locate-library (library &optional nosuffix path interactive-call
)
120 "Show the precise file name of Emacs library LIBRARY.
121 This command searches the directories in `load-path' like `\\[load-library]'
122 to find the file that `\\[load-library] RET LIBRARY RET' would load.
123 Optional second arg NOSUFFIX non-nil means don't add suffixes `load-suffixes'
124 to the specified name LIBRARY.
126 If the optional third arg PATH is specified, that list of directories
127 is used instead of `load-path'.
129 When called from a program, the file name is normaly returned as a
130 string. When run interactively, the argument INTERACTIVE-CALL is t,
131 and the file name is displayed in the echo area."
132 (interactive (list (completing-read "Locate library: "
133 'locate-file-completion
134 (cons load-path load-suffixes
))
137 (let ((file (locate-file library
139 (append (unless nosuffix load-suffixes
) '("")))))
142 (message "Library is file %s" (abbreviate-file-name file
))
143 (message "No library %s in search path" library
)))
150 (defun describe-function (function)
151 "Display the full documentation of FUNCTION (a symbol)."
153 (let ((fn (function-called-at-point))
154 (enable-recursive-minibuffers t
)
156 (setq val
(completing-read (if fn
157 (format "Describe function (default %s): " fn
)
158 "Describe function: ")
159 obarray
'fboundp t nil nil
(symbol-name fn
)))
160 (list (if (equal val
"")
163 (message "You didn't specify a function")
164 (help-setup-xref (list #'describe-function function
) (interactive-p))
166 (with-output-to-temp-buffer (help-buffer)
168 ;; Use " is " instead of a colon so that
169 ;; it is easier to get out the function name using forward-sexp.
171 (describe-function-1 function
)
172 (print-help-return-message)
173 (with-current-buffer standard-output
174 ;; Return the text we displayed.
177 (defun help-split-fundoc (doc def
)
178 "Split a function docstring DOC into the actual doc and the usage info.
179 Return (USAGE . DOC) or nil if there's no usage info.
180 DEF is the function whose usage we're looking for in DOC."
181 ;; Functions can get the calling sequence at the end of the doc string.
182 ;; In cases where `function' has been fset to a subr we can't search for
183 ;; function's name in the doc string so we use `fn' as the anonymous
184 ;; function name instead.
186 (let* ((rep (prin1-to-string (indirect-function def
)))
187 (name (if (string-match " \\([^ ]+\\)>$" rep
)
188 (match-string 1 rep
) (prin1-to-string def
))))
189 (if (string-match (format "\n\n(\\(fn\\|%s\\)\\(\\( .*\\)?)\\)\\'"
192 (cons (format "(%s%s"
193 ;; Replace `fn' with the actual function name.
194 (if (consp def
) "anonymous" def
)
195 (match-string 2 doc
))
196 (substring doc
0 (match-beginning 0)))))))
198 (defun help-function-arglist (def)
199 ;; Handle symbols aliased to other symbols.
200 (if (and (symbolp def
) (fboundp def
)) (setq def
(indirect-function def
)))
201 ;; If definition is a macro, find the function inside it.
202 (if (eq (car-safe def
) 'macro
) (setq def
(cdr def
)))
204 ((byte-code-function-p def
) (aref def
0))
205 ((eq (car-safe def
) 'lambda
) (nth 1 def
))
206 ((and (eq (car-safe def
) 'autoload
) (not (eq (nth 4 def
) 'keymap
)))
207 "[Arg list not available until function definition is loaded.]")
210 (defun help-make-usage (function arglist
)
211 (cons (if (symbolp function
) function
'anonymous
)
212 (mapcar (lambda (arg)
213 (if (not (symbolp arg
))
214 (if (and (consp arg
) (symbolp (car arg
)))
215 ;; CL style default values for optional args.
216 (cons (intern (upcase (symbol-name (car arg
))))
219 (let ((name (symbol-name arg
)))
220 (if (string-match "\\`&" name
) arg
221 (intern (upcase name
))))))
225 (defun describe-function-1 (function)
226 (let* ((def (if (symbolp function
)
227 (symbol-function function
)
230 (beg (if (commandp def
) "an interactive " "a ")))
232 (cond ((or (stringp def
)
236 (if (eq 'unevalled
(cdr (subr-arity def
)))
237 (concat beg
"special form")
238 (concat beg
"built-in function")))
239 ((byte-code-function-p def
)
240 (concat beg
"compiled Lisp function"))
242 (while (symbolp (symbol-function def
))
243 (setq def
(symbol-function def
)))
244 (format "an alias for `%s'" def
))
245 ((eq (car-safe def
) 'lambda
)
246 (concat beg
"Lisp function"))
247 ((eq (car-safe def
) 'macro
)
249 ((eq (car-safe def
) 'autoload
)
250 (setq file-name
(nth 1 def
))
251 (format "%s autoloaded %s"
252 (if (commandp def
) "an interactive" "an")
253 (if (eq (nth 4 def
) 'keymap
) "keymap"
254 (if (nth 4 def
) "Lisp macro" "Lisp function"))
258 (elts (cdr-safe def
)))
260 (if (char-table-p (car-safe elts
))
263 (setq elts
(cdr-safe elts
)))
269 (with-current-buffer standard-output
272 (if (re-search-backward "alias for `\\([^`']+\\)'" nil t
)
273 (help-xref-button 1 'help-function def
)))))
275 (setq file-name
(symbol-file function
)))
276 (when (equal file-name
"loaddefs.el")
277 ;; Find the real def site of the preloaded function.
278 ;; This is necessary only for defaliases.
281 (find-function-search-for-symbol function nil
"loaddefs.el")
284 (with-current-buffer (car location
)
285 (goto-char (cdr location
))
286 (when (re-search-backward
287 "^;;; Generated autoloads from \\(.*\\)" nil t
)
288 (setq file-name
(match-string 1)))))))
292 ;; We used to add .el to the file name,
293 ;; but that's completely wrong when the user used load-file.
296 ;; Make a hyperlink to the library.
297 (with-current-buffer standard-output
299 (re-search-backward "`\\([^`']+\\)'" nil t
)
300 (help-xref-button 1 'help-function-def function file-name
)))))
303 (when (commandp function
)
304 (let* ((remapped (remap-command function
))
305 (keys (where-is-internal
306 (or remapped function
) overriding-local-map nil nil
)))
308 (princ "It is remapped to `")
309 (princ (symbol-name remapped
))
312 (princ (if remapped
" which is bound to " "It is bound to "))
313 ;; FIXME: This list can be very long (f.ex. for self-insert-command).
314 (princ (mapconcat 'key-description keys
", ")))
315 (when (or remapped keys
)
318 (let* ((arglist (help-function-arglist def
))
319 (doc (documentation function
))
320 (usage (help-split-fundoc doc function
)))
321 ;; If definition is a keymap, skip arglist note.
322 (unless (keymapp def
)
324 (usage (setq doc
(cdr usage
)) (car usage
))
325 ((listp arglist
) (help-make-usage function arglist
))
326 ((stringp arglist
) arglist
)
327 ;; Maybe the arglist is in the docstring of the alias.
328 ((let ((fun function
))
329 (while (and (symbolp fun
)
330 (setq fun
(symbol-function fun
))
331 (not (setq usage
(help-split-fundoc
336 (t "[Missing arglist. Please make a bug report.]")))
339 ;; function might be a lambda construct.
341 (get function
'byte-obsolete-info
))))
344 (princ "This function is obsolete")
345 (if (nth 2 obsolete
) (princ (format " since %s" (nth 2 obsolete
))))
347 (princ (if (stringp (car obsolete
)) (car obsolete
)
348 (format "use `%s' instead." (car obsolete
))))
351 (princ (or doc
"Not documented.")))))
357 (defun variable-at-point ()
358 "Return the bound variable symbol found around point.
359 Return 0 if there is no such symbol."
361 (with-syntax-table emacs-lisp-mode-syntax-table
363 (or (not (zerop (skip-syntax-backward "_w")))
364 (eq (char-syntax (following-char)) ?w
)
365 (eq (char-syntax (following-char)) ?_
)
367 (skip-chars-forward "'")
368 (let ((obj (read (current-buffer))))
369 (or (and (symbolp obj
) (boundp obj
) obj
)
374 (defun describe-variable (variable &optional buffer
)
375 "Display the full documentation of VARIABLE (a symbol).
376 Returns the documentation as a string, also.
377 If VARIABLE has a buffer-local value in BUFFER (default to the current buffer),
378 it is displayed along with the global value."
380 (let ((v (variable-at-point))
381 (enable-recursive-minibuffers t
)
383 (setq val
(completing-read (if (symbolp v
)
385 "Describe variable (default %s): " v
)
386 "Describe variable: ")
387 obarray
'boundp t nil nil
388 (if (symbolp v
) (symbol-name v
))))
389 (list (if (equal val
"")
391 (unless (buffer-live-p buffer
) (setq buffer
(current-buffer)))
392 (if (not (symbolp variable
))
393 (message "You did not specify a variable")
395 (let* ((valvoid (not (with-current-buffer buffer
(boundp variable
))))
396 ;; Extract the value before setting up the output buffer,
397 ;; in case `buffer' *is* the output buffer.
398 (val (unless valvoid
(buffer-local-value variable buffer
))))
399 (help-setup-xref (list #'describe-variable variable buffer
)
401 (with-output-to-temp-buffer (help-buffer)
402 (with-current-buffer buffer
406 (with-current-buffer standard-output
407 (princ "'s value is ")
409 (let ((from (point)))
411 (help-xref-on-pp from
(point))
412 (if (< (point) (+ from
20))
413 (delete-region (1- from
) from
)))))
415 (when (local-variable-p variable
)
416 (princ (format "Local in buffer %s; " (buffer-name)))
417 (if (not (default-boundp variable
))
418 (princ "globally void")
419 (let ((val (default-value variable
)))
420 (with-current-buffer standard-output
421 (princ "global value is ")
423 ;; Fixme: pp can take an age if you happen to
424 ;; ask for a very large expression. We should
425 ;; probably print it raw once and check it's a
426 ;; sensible size before prettyprinting. -- fx
427 (let ((from (point)))
429 (help-xref-on-pp from
(point))
430 (if (< (point) (+ from
20))
431 (delete-region (1- from
) from
))))))
434 (with-current-buffer standard-output
435 (when (> (count-lines (point-min) (point-max)) 10)
436 ;; Note that setting the syntax table like below
437 ;; makes forward-sexp move over a `'s' at the end
439 (set-syntax-table emacs-lisp-mode-syntax-table
)
440 (goto-char (point-min))
444 (delete-region (point) (progn (end-of-line) (point)))
445 (insert " value is shown below.\n\n")
447 (insert "\n\nValue:"))))
448 ;; Add a note for variables that have been make-var-buffer-local.
449 (when (and (local-variable-if-set-p variable
)
450 (or (not (local-variable-p variable
))
452 (local-variable-if-set-p variable
))))
455 (insert "Automatically becomes buffer-local when set in any fashion.\n"))))
456 ;; Mention if it's an alias
457 (let* ((alias (condition-case nil
458 (indirect-variable variable
)
460 (obsolete (get variable
'byte-obsolete-variable
))
461 (doc (or (documentation-property variable
'variable-documentation
)
462 (documentation-property alias
'variable-documentation
))))
463 (unless (eq alias variable
)
464 (princ (format "This variable is an alias for `%s'." alias
))
468 (princ "This variable is obsolete")
469 (if (cdr obsolete
) (princ (format " since %s" (cdr obsolete
))))
471 (princ (if (stringp (car obsolete
)) (car obsolete
)
472 (format "use `%s' instead." (car obsolete
))))
475 (princ (or doc
"Not documented as a variable.")))
476 ;; Make a link to customize if this variable can be customized.
477 ;; Note, it is not reliable to test only for a custom-type property
478 ;; because those are only present after the var's definition
480 (if (or (get variable
'custom-type
) ; after defcustom
481 (get variable
'custom-loads
) ; from loaddefs.el
482 (get variable
'standard-value
)) ; from cus-start.el
483 (let ((customize-label "customize"))
486 (princ (concat "You can " customize-label
" this variable."))
487 (with-current-buffer standard-output
490 (concat "\\(" customize-label
"\\)") nil t
)
491 (help-xref-button 1 'help-customize-variable variable
)))))
492 ;; Make a hyperlink to the library if appropriate. (Don't
493 ;; change the format of the buffer's initial line in case
494 ;; anything expects the current format.)
495 (let ((file-name (symbol-file variable
)))
496 (when (equal file-name
"loaddefs.el")
497 ;; Find the real def site of the preloaded variable.
500 (find-variable-noselect variable file-name
)
503 (with-current-buffer (car location
)
504 (goto-char (cdr location
))
505 (when (re-search-backward
506 "^;;; Generated autoloads from \\(.*\\)" nil t
)
507 (setq file-name
(match-string 1)))))))
509 (princ "\n\nDefined in `")
512 (with-current-buffer standard-output
514 (re-search-backward "`\\([^`']+\\)'" nil t
)
515 (help-xref-button 1 'help-variable-def
516 variable file-name
)))))
518 (print-help-return-message)
520 (set-buffer standard-output
)
521 ;; Return the text we displayed.
522 (buffer-string))))))))
526 (defun describe-syntax (&optional buffer
)
527 "Describe the syntax specifications in the syntax table of BUFFER.
528 The descriptions are inserted in a help buffer, which is then displayed.
529 BUFFER defaults to the current buffer."
531 (setq buffer
(or buffer
(current-buffer)))
532 (help-setup-xref (list #'describe-syntax buffer
) (interactive-p))
533 (with-output-to-temp-buffer (help-buffer)
534 (let ((table (with-current-buffer buffer
(syntax-table))))
535 (with-current-buffer standard-output
536 (describe-vector table
'internal-describe-syntax-value
)
537 (while (setq table
(char-table-parent table
))
538 (insert "\nThe parent syntax table is:")
539 (describe-vector table
'internal-describe-syntax-value
))))))
541 (defun help-describe-category-set (value)
543 ((null value
) "default")
544 ((char-table-p value
) "deeper char-table ...")
545 (t (condition-case err
546 (category-set-mnemonics value
)
547 (error "invalid"))))))
550 (defun describe-categories (&optional buffer
)
551 "Describe the category specifications in the current category table.
552 The descriptions are inserted in a buffer, which is then displayed."
554 (setq buffer
(or buffer
(current-buffer)))
555 (help-setup-xref (list #'describe-categories buffer
) (interactive-p))
556 (with-output-to-temp-buffer (help-buffer)
557 (let ((table (with-current-buffer buffer
(category-table))))
558 (with-current-buffer standard-output
559 (describe-vector table
'help-describe-category-set
)
560 (let ((docs (char-table-extra-slot table
0)))
561 (if (or (not (vectorp docs
)) (/= (length docs
) 95))
562 (insert "Invalid first extra slot in this char table\n")
563 (insert "Meanings of mnemonic characters are:\n")
565 (let ((elt (aref docs i
)))
567 (insert (+ i ?\
) ": " elt
"\n"))))
568 (while (setq table
(char-table-parent table
))
569 (insert "\nThe parent category table is:")
570 (describe-vector table
'help-describe-category-set
))))))))
574 ;;; help-fns.el ends here