* doc/lispref/variables.texi (Scope): Mention the availability of lexbind.
[emacs.git] / lisp / help-fns.el
blob87fb6a02bd3141f3cf4becb874916b9f3dde785c
1 ;;; help-fns.el --- Complex help functions
3 ;; Copyright (C) 1985-1986, 1993-1994, 1998-2011
4 ;; Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: help, internal
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This file contains those help commands which are complicated, and
28 ;; which may not be used in every session. For example
29 ;; `describe-function' will probably be heavily used when doing elisp
30 ;; programming, but not if just editing C files. Simpler help commands
31 ;; are in help.el
33 ;;; Code:
35 ;; Functions
37 ;;;###autoload
38 (defun describe-function (function)
39 "Display the full documentation of FUNCTION (a symbol)."
40 (interactive
41 (let ((fn (function-called-at-point))
42 (enable-recursive-minibuffers t)
43 val)
44 (setq val (completing-read (if fn
45 (format "Describe function (default %s): " fn)
46 "Describe function: ")
47 obarray 'fboundp t nil nil
48 (and fn (symbol-name fn))))
49 (list (if (equal val "")
50 fn (intern val)))))
51 (if (null function)
52 (message "You didn't specify a function")
53 (help-setup-xref (list #'describe-function function)
54 (called-interactively-p 'interactive))
55 (save-excursion
56 (with-help-window (help-buffer)
57 (prin1 function)
58 ;; Use " is " instead of a colon so that
59 ;; it is easier to get out the function name using forward-sexp.
60 (princ " is ")
61 (describe-function-1 function)
62 (with-current-buffer standard-output
63 ;; Return the text we displayed.
64 (buffer-string))))))
66 (defun help-split-fundoc (docstring def)
67 "Split a function DOCSTRING into the actual doc and the usage info.
68 Return (USAGE . DOC) or nil if there's no usage info.
69 DEF is the function whose usage we're looking for in DOCSTRING."
70 ;; Functions can get the calling sequence at the end of the doc string.
71 ;; In cases where `function' has been fset to a subr we can't search for
72 ;; function's name in the doc string so we use `fn' as the anonymous
73 ;; function name instead.
74 (when (and docstring (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring))
75 (cons (format "(%s%s"
76 ;; Replace `fn' with the actual function name.
77 (if (consp def) "anonymous" def)
78 (match-string 1 docstring))
79 (substring docstring 0 (match-beginning 0)))))
81 (defun help-add-fundoc-usage (docstring arglist)
82 "Add the usage info to DOCSTRING.
83 If DOCSTRING already has a usage info, then just return it unchanged.
84 The usage info is built from ARGLIST. DOCSTRING can be nil.
85 ARGLIST can also be t or a string of the form \"(FUN ARG1 ARG2 ...)\"."
86 (unless (stringp docstring) (setq docstring "Not documented"))
87 (if (or (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring) (eq arglist t))
88 docstring
89 (concat docstring
90 (if (string-match "\n?\n\\'" docstring)
91 (if (< (- (match-end 0) (match-beginning 0)) 2) "\n" "")
92 "\n\n")
93 (if (and (stringp arglist)
94 (string-match "\\`([^ ]+\\(.*\\))\\'" arglist))
95 (concat "(fn" (match-string 1 arglist) ")")
96 (format "%S" (help-make-usage 'fn arglist))))))
98 (defun help-function-arglist (def)
99 ;; Handle symbols aliased to other symbols.
100 (if (and (symbolp def) (fboundp def)) (setq def (indirect-function def)))
101 ;; If definition is a macro, find the function inside it.
102 (if (eq (car-safe def) 'macro) (setq def (cdr def)))
103 ;; and do the same for interpreted closures
104 (if (eq (car-safe def) 'closure) (setq def (cddr def)))
105 (cond
106 ((byte-code-function-p def) (aref def 0))
107 ((eq (car-safe def) 'lambda) (nth 1 def))
108 ((and (eq (car-safe def) 'autoload) (not (eq (nth 4 def) 'keymap)))
109 "[Arg list not available until function definition is loaded.]")
110 (t t)))
112 (defun help-make-usage (function arglist)
113 (cons (if (symbolp function) function 'anonymous)
114 (mapcar (lambda (arg)
115 (if (not (symbolp arg))
116 (if (and (consp arg) (symbolp (car arg)))
117 ;; CL style default values for optional args.
118 (cons (intern (upcase (symbol-name (car arg))))
119 (cdr arg))
120 arg)
121 (let ((name (symbol-name arg)))
122 (cond
123 ((string-match "\\`&" name) arg)
124 ((string-match "\\`_" name)
125 (intern (upcase (substring name 1))))
126 (t (intern (upcase name)))))))
127 arglist)))
129 ;; Could be this, if we make symbol-file do the work below.
130 ;; (defun help-C-file-name (subr-or-var kind)
131 ;; "Return the name of the C file where SUBR-OR-VAR is defined.
132 ;; KIND should be `var' for a variable or `subr' for a subroutine."
133 ;; (symbol-file (if (symbolp subr-or-var) subr-or-var
134 ;; (subr-name subr-or-var))
135 ;; (if (eq kind 'var) 'defvar 'defun)))
136 ;;;###autoload
137 (defun help-C-file-name (subr-or-var kind)
138 "Return the name of the C file where SUBR-OR-VAR is defined.
139 KIND should be `var' for a variable or `subr' for a subroutine."
140 (let ((docbuf (get-buffer-create " *DOC*"))
141 (name (if (eq 'var kind)
142 (concat "V" (symbol-name subr-or-var))
143 (concat "F" (subr-name subr-or-var)))))
144 (with-current-buffer docbuf
145 (goto-char (point-min))
146 (if (eobp)
147 (insert-file-contents-literally
148 (expand-file-name internal-doc-file-name doc-directory)))
149 (let ((file (catch 'loop
150 (while t
151 (let ((pnt (search-forward (concat "\x1f" name "\n"))))
152 (re-search-backward "\x1fS\\(.*\\)")
153 (let ((file (match-string 1)))
154 (if (member file build-files)
155 (throw 'loop file)
156 (goto-char pnt))))))))
157 (if (string-match "^ns.*\\(\\.o\\|obj\\)\\'" file)
158 (setq file (replace-match ".m" t t file 1))
159 (if (string-match "\\.\\(o\\|obj\\)\\'" file)
160 (setq file (replace-match ".c" t t file))))
161 (if (string-match "\\.\\(c\\|m\\)\\'" file)
162 (concat "src/" file)
163 file)))))
165 (defcustom help-downcase-arguments nil
166 "If non-nil, argument names in *Help* buffers are downcased."
167 :type 'boolean
168 :group 'help
169 :version "23.2")
171 (defun help-highlight-arg (arg)
172 "Highlight ARG as an argument name for a *Help* buffer.
173 Return ARG in face `help-argument-name'; ARG is also downcased
174 if the variable `help-downcase-arguments' is non-nil."
175 (propertize (if help-downcase-arguments (downcase arg) arg)
176 'face 'help-argument-name))
178 (defun help-do-arg-highlight (doc args)
179 (with-syntax-table (make-syntax-table emacs-lisp-mode-syntax-table)
180 (modify-syntax-entry ?\- "w")
181 (dolist (arg args doc)
182 (setq doc (replace-regexp-in-string
183 ;; This is heuristic, but covers all common cases
184 ;; except ARG1-ARG2
185 (concat "\\<" ; beginning of word
186 "\\(?:[a-z-]*-\\)?" ; for xxx-ARG
187 "\\("
188 (regexp-quote arg)
189 "\\)"
190 "\\(?:es\\|s\\|th\\)?" ; for ARGth, ARGs
191 "\\(?:-[a-z0-9-]+\\)?" ; for ARG-xxx, ARG-n
192 "\\(?:-[{([<`\"].*?\\)?"; for ARG-{x}, (x), <x>, [x], `x'
193 "\\>") ; end of word
194 (help-highlight-arg arg)
195 doc t t 1)))))
197 (defun help-highlight-arguments (usage doc &rest args)
198 (when (and usage (string-match "^(" usage))
199 (with-temp-buffer
200 (insert usage)
201 (goto-char (point-min))
202 (let ((case-fold-search nil)
203 (next (not (or args (looking-at "\\["))))
204 (opt nil))
205 ;; Make a list of all arguments
206 (skip-chars-forward "^ ")
207 (while next
208 (or opt (not (looking-at " &")) (setq opt t))
209 (if (not (re-search-forward " \\([\\[(]*\\)\\([^] &)\.]+\\)" nil t))
210 (setq next nil)
211 (setq args (cons (match-string 2) args))
212 (when (and opt (string= (match-string 1) "("))
213 ;; A pesky CL-style optional argument with default value,
214 ;; so let's skip over it
215 (search-backward "(")
216 (goto-char (scan-sexps (point) 1)))))
217 ;; Highlight aguments in the USAGE string
218 (setq usage (help-do-arg-highlight (buffer-string) args))
219 ;; Highlight arguments in the DOC string
220 (setq doc (and doc (help-do-arg-highlight doc args))))))
221 ;; Return value is like the one from help-split-fundoc, but highlighted
222 (cons usage doc))
224 ;; The following function was compiled from the former functions
225 ;; `describe-simplify-lib-file-name' and `find-source-lisp-file' with
226 ;; some excerpts from `describe-function-1' and `describe-variable'.
227 ;; The only additional twists provided are (1) locate the defining file
228 ;; for autoloaded functions, and (2) give preference to files in the
229 ;; "install directory" (directories found via `load-path') rather than
230 ;; to files in the "compile directory" (directories found by searching
231 ;; the loaddefs.el file). We autoload it because it's also used by
232 ;; `describe-face' (instead of `describe-simplify-lib-file-name').
234 ;;;###autoload
235 (defun find-lisp-object-file-name (object type)
236 "Guess the file that defined the Lisp object OBJECT, of type TYPE.
237 OBJECT should be a symbol associated with a function, variable, or face;
238 alternatively, it can be a function definition.
239 If TYPE is `defvar', search for a variable definition.
240 If TYPE is `defface', search for a face definition.
241 If TYPE is the value returned by `symbol-function' for a function symbol,
242 search for a function definition.
244 The return value is the absolute name of a readable file where OBJECT is
245 defined. If several such files exist, preference is given to a file
246 found via `load-path'. The return value can also be `C-source', which
247 means that OBJECT is a function or variable defined in C. If no
248 suitable file is found, return nil."
249 (let* ((autoloaded (eq (car-safe type) 'autoload))
250 (file-name (or (and autoloaded (nth 1 type))
251 (symbol-file
252 object (if (memq type (list 'defvar 'defface))
253 type
254 'defun)))))
255 (cond
256 (autoloaded
257 ;; An autoloaded function: Locate the file since `symbol-function'
258 ;; has only returned a bare string here.
259 (setq file-name
260 (locate-file file-name load-path '(".el" ".elc") 'readable)))
261 ((and (stringp file-name)
262 (string-match "[.]*loaddefs.el\\'" file-name))
263 ;; An autoloaded variable or face. Visit loaddefs.el in a buffer
264 ;; and try to extract the defining file. The following form is
265 ;; from `describe-function-1' and `describe-variable'.
266 (let ((location
267 (condition-case nil
268 (find-function-search-for-symbol object nil file-name)
269 (error nil))))
270 (when (cdr location)
271 (with-current-buffer (car location)
272 (goto-char (cdr location))
273 (when (re-search-backward
274 "^;;; Generated autoloads from \\(.*\\)" nil t)
275 (setq file-name
276 (locate-file
277 (file-name-sans-extension
278 (match-string-no-properties 1))
279 load-path '(".el" ".elc") 'readable))))))))
281 (cond
282 ((and (not file-name) (subrp type))
283 ;; A built-in function. The form is from `describe-function-1'.
284 (if (get-buffer " *DOC*")
285 (help-C-file-name type 'subr)
286 'C-source))
287 ((and (not file-name) (symbolp object)
288 (integerp (get object 'variable-documentation)))
289 ;; A variable defined in C. The form is from `describe-variable'.
290 (if (get-buffer " *DOC*")
291 (help-C-file-name object 'var)
292 'C-source))
293 ((not (stringp file-name))
294 ;; If we don't have a file-name string by now, we lost.
295 nil)
296 ;; Now, `file-name' should have become an absolute file name.
297 ;; For files loaded from ~/.emacs.elc, try ~/.emacs.
298 ((let (fn)
299 (and (string-equal file-name
300 (expand-file-name ".emacs.elc" "~"))
301 (file-readable-p (setq fn (expand-file-name ".emacs" "~")))
302 fn)))
303 ;; When the Elisp source file can be found in the install
304 ;; directory, return the name of that file.
305 ((let ((lib-name
306 (if (string-match "[.]elc\\'" file-name)
307 (substring-no-properties file-name 0 -1)
308 file-name)))
309 (or (and (file-readable-p lib-name) lib-name)
310 ;; The library might be compressed.
311 (and (file-readable-p (concat lib-name ".gz")) lib-name))))
312 ((let* ((lib-name (file-name-nondirectory file-name))
313 ;; The next form is from `describe-simplify-lib-file-name'.
314 (file-name
315 ;; Try converting the absolute file name to a library
316 ;; name, convert that back to a file name and see if we
317 ;; get the original one. If so, they are equivalent.
318 (if (equal file-name (locate-file lib-name load-path '("")))
319 (if (string-match "[.]elc\\'" lib-name)
320 (substring-no-properties lib-name 0 -1)
321 lib-name)
322 file-name))
323 ;; The next three forms are from `find-source-lisp-file'.
324 (elc-file (locate-file
325 (concat file-name
326 (if (string-match "\\.el\\'" file-name)
328 ".elc"))
329 load-path nil 'readable))
330 (str (when elc-file
331 (with-temp-buffer
332 (insert-file-contents-literally elc-file nil 0 256)
333 (buffer-string))))
334 (src-file (and str
335 (string-match ";;; from file \\(.*\\.el\\)" str)
336 (match-string 1 str))))
337 (and src-file (file-readable-p src-file) src-file))))))
339 (declare-function ad-get-advice-info "advice" (function))
341 ;;;###autoload
342 (defun describe-function-1 (function)
343 (let* ((advised (and (symbolp function) (featurep 'advice)
344 (ad-get-advice-info function)))
345 ;; If the function is advised, use the symbol that has the
346 ;; real definition, if that symbol is already set up.
347 (real-function
348 (or (and advised
349 (let ((origname (cdr (assq 'origname advised))))
350 (and (fboundp origname) origname)))
351 function))
352 ;; Get the real definition.
353 (def (if (symbolp real-function)
354 (symbol-function real-function)
355 function))
356 file-name string
357 (beg (if (commandp def) "an interactive " "a "))
358 (pt1 (with-current-buffer (help-buffer) (point)))
359 errtype)
360 (setq string
361 (cond ((or (stringp def) (vectorp def))
362 "a keyboard macro")
363 ((subrp def)
364 (if (eq 'unevalled (cdr (subr-arity def)))
365 (concat beg "special form")
366 (concat beg "built-in function")))
367 ((byte-code-function-p def)
368 (concat beg "compiled Lisp function"))
369 ((symbolp def)
370 (while (and (fboundp def)
371 (symbolp (symbol-function def)))
372 (setq def (symbol-function def)))
373 ;; Handle (defalias 'foo 'bar), where bar is undefined.
374 (or (fboundp def) (setq errtype 'alias))
375 (format "an alias for `%s'" def))
376 ((eq (car-safe def) 'lambda)
377 (concat beg "Lisp function"))
378 ((eq (car-safe def) 'macro)
379 "a Lisp macro")
380 ((eq (car-safe def) 'closure)
381 (concat beg "Lisp closure"))
382 ((eq (car-safe def) 'autoload)
383 (format "%s autoloaded %s"
384 (if (commandp def) "an interactive" "an")
385 (if (eq (nth 4 def) 'keymap) "keymap"
386 (if (nth 4 def) "Lisp macro" "Lisp function"))))
387 ((keymapp def)
388 (let ((is-full nil)
389 (elts (cdr-safe def)))
390 (while elts
391 (if (char-table-p (car-safe elts))
392 (setq is-full t
393 elts nil))
394 (setq elts (cdr-safe elts)))
395 (if is-full
396 "a full keymap"
397 "a sparse keymap")))
398 (t "")))
399 (princ string)
400 (if (eq errtype 'alias)
401 (princ ",\nwhich is not defined. Please make a bug report.")
402 (with-current-buffer standard-output
403 (save-excursion
404 (save-match-data
405 (when (re-search-backward "alias for `\\([^`']+\\)'" nil t)
406 (help-xref-button 1 'help-function def)))))
408 (setq file-name (find-lisp-object-file-name function def))
409 (when file-name
410 (princ " in `")
411 ;; We used to add .el to the file name,
412 ;; but that's completely wrong when the user used load-file.
413 (princ (if (eq file-name 'C-source)
414 "C source code"
415 (file-name-nondirectory file-name)))
416 (princ "'")
417 ;; Make a hyperlink to the library.
418 (with-current-buffer standard-output
419 (save-excursion
420 (re-search-backward "`\\([^`']+\\)'" nil t)
421 (help-xref-button 1 'help-function-def function file-name))))
422 (princ ".")
423 (with-current-buffer (help-buffer)
424 (fill-region-as-paragraph (save-excursion (goto-char pt1) (forward-line 0) (point))
425 (point)))
426 (terpri)(terpri)
427 (when (commandp function)
428 (let ((pt2 (with-current-buffer (help-buffer) (point)))
429 (remapped (command-remapping function)))
430 (unless (memq remapped '(ignore undefined))
431 (let ((keys (where-is-internal
432 (or remapped function) overriding-local-map nil nil))
433 non-modified-keys)
434 (if (and (eq function 'self-insert-command)
435 (vectorp (car-safe keys))
436 (consp (aref (car keys) 0)))
437 (princ "It is bound to many ordinary text characters.\n")
438 ;; Which non-control non-meta keys run this command?
439 (dolist (key keys)
440 (if (member (event-modifiers (aref key 0)) '(nil (shift)))
441 (push key non-modified-keys)))
442 (when remapped
443 (princ "It is remapped to `")
444 (princ (symbol-name remapped))
445 (princ "'"))
447 (when keys
448 (princ (if remapped ", which is bound to " "It is bound to "))
449 ;; If lots of ordinary text characters run this command,
450 ;; don't mention them one by one.
451 (if (< (length non-modified-keys) 10)
452 (princ (mapconcat 'key-description keys ", "))
453 (dolist (key non-modified-keys)
454 (setq keys (delq key keys)))
455 (if keys
456 (progn
457 (princ (mapconcat 'key-description keys ", "))
458 (princ ", and many ordinary text characters"))
459 (princ "many ordinary text characters"))))
460 (when (or remapped keys non-modified-keys)
461 (princ ".")
462 (terpri)))))
464 (with-current-buffer (help-buffer)
465 (fill-region-as-paragraph pt2 (point))
466 (unless (looking-back "\n\n")
467 (terpri)))))
468 ;; Note that list* etc do not get this property until
469 ;; cl-hack-byte-compiler runs, after bytecomp is loaded.
470 (when (and (symbolp function)
471 (eq (get function 'byte-compile)
472 'cl-byte-compile-compiler-macro))
473 (princ "This function has a compiler macro")
474 (let ((lib (get function 'compiler-macro-file)))
475 (when (stringp lib)
476 (princ (format " in `%s'" lib))
477 (with-current-buffer standard-output
478 (save-excursion
479 (re-search-backward "`\\([^`']+\\)'" nil t)
480 (help-xref-button 1 'help-function-cmacro function lib)))))
481 (princ ".\n\n"))
482 (let* ((advertised (gethash def advertised-signature-table t))
483 (arglist (if (listp advertised)
484 advertised (help-function-arglist def)))
485 (doc (documentation function))
486 (usage (help-split-fundoc doc function)))
487 (with-current-buffer standard-output
488 ;; If definition is a keymap, skip arglist note.
489 (unless (keymapp function)
490 (if usage (setq doc (cdr usage)))
491 (let* ((use (cond
492 ((and usage (not (listp advertised))) (car usage))
493 ((listp arglist)
494 (format "%S" (help-make-usage function arglist)))
495 ((stringp arglist) arglist)
496 ;; Maybe the arglist is in the docstring of a symbol
497 ;; this one is aliased to.
498 ((let ((fun real-function))
499 (while (and (symbolp fun)
500 (setq fun (symbol-function fun))
501 (not (setq usage (help-split-fundoc
502 (documentation fun)
503 function)))))
504 usage)
505 (car usage))
506 ((or (stringp def)
507 (vectorp def))
508 (format "\nMacro: %s" (format-kbd-macro def)))
509 (t "[Missing arglist. Please make a bug report.]")))
510 (high (help-highlight-arguments use doc)))
511 (let ((fill-begin (point)))
512 (insert (car high) "\n")
513 (fill-region fill-begin (point)))
514 (setq doc (cdr high))))
515 (let* ((obsolete (and
516 ;; function might be a lambda construct.
517 (symbolp function)
518 (get function 'byte-obsolete-info)))
519 (use (car obsolete)))
520 (when obsolete
521 (princ "\nThis function is obsolete")
522 (when (nth 2 obsolete)
523 (insert (format " since %s" (nth 2 obsolete))))
524 (insert (cond ((stringp use) (concat ";\n" use))
525 (use (format ";\nuse `%s' instead." use))
526 (t "."))
527 "\n"))
528 (insert "\n"
529 (or doc "Not documented."))))))))
532 ;; Variables
534 ;;;###autoload
535 (defun variable-at-point (&optional any-symbol)
536 "Return the bound variable symbol found at or before point.
537 Return 0 if there is no such symbol.
538 If ANY-SYMBOL is non-nil, don't insist the symbol be bound."
539 (with-syntax-table emacs-lisp-mode-syntax-table
540 (or (condition-case ()
541 (save-excursion
542 (or (not (zerop (skip-syntax-backward "_w")))
543 (eq (char-syntax (following-char)) ?w)
544 (eq (char-syntax (following-char)) ?_)
545 (forward-sexp -1))
546 (skip-chars-forward "'")
547 (let ((obj (read (current-buffer))))
548 (and (symbolp obj) (boundp obj) obj)))
549 (error nil))
550 (let* ((str (find-tag-default))
551 (sym (if str (intern-soft str))))
552 (if (and sym (or any-symbol (boundp sym)))
554 (save-match-data
555 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
556 (setq sym (intern-soft (match-string 1 str)))
557 (and (or any-symbol (boundp sym)) sym)))))
558 0)))
560 (defun describe-variable-custom-version-info (variable)
561 (let ((custom-version (get variable 'custom-version))
562 (cpv (get variable 'custom-package-version))
563 (output nil))
564 (if custom-version
565 (setq output
566 (format "This variable was introduced, or its default value was changed, in\nversion %s of Emacs.\n"
567 custom-version))
568 (when cpv
569 (let* ((package (car-safe cpv))
570 (version (if (listp (cdr-safe cpv))
571 (car (cdr-safe cpv))
572 (cdr-safe cpv)))
573 (pkg-versions (assq package customize-package-emacs-version-alist))
574 (emacsv (cdr (assoc version pkg-versions))))
575 (if (and package version)
576 (setq output
577 (format (concat "This variable was introduced, or its default value was changed, in\nversion %s of the %s package"
578 (if emacsv
579 (format " that is part of Emacs %s" emacsv))
580 ".\n")
581 version package))))))
582 output))
584 ;;;###autoload
585 (defun describe-variable (variable &optional buffer frame)
586 "Display the full documentation of VARIABLE (a symbol).
587 Returns the documentation as a string, also.
588 If VARIABLE has a buffer-local value in BUFFER or FRAME
589 \(default to the current buffer and current frame),
590 it is displayed along with the global value."
591 (interactive
592 (let ((v (variable-at-point))
593 (enable-recursive-minibuffers t)
594 val)
595 (setq val (completing-read (if (symbolp v)
596 (format
597 "Describe variable (default %s): " v)
598 "Describe variable: ")
599 obarray
600 '(lambda (vv)
601 (or (boundp vv)
602 (get vv 'variable-documentation)))
603 t nil nil
604 (if (symbolp v) (symbol-name v))))
605 (list (if (equal val "")
606 v (intern val)))))
607 (let (file-name)
608 (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
609 (unless (frame-live-p frame) (setq frame (selected-frame)))
610 (if (not (symbolp variable))
611 (message "You did not specify a variable")
612 (save-excursion
613 (let ((valvoid (not (with-current-buffer buffer (boundp variable))))
614 val val-start-pos locus)
615 ;; Extract the value before setting up the output buffer,
616 ;; in case `buffer' *is* the output buffer.
617 (unless valvoid
618 (with-selected-frame frame
619 (with-current-buffer buffer
620 (setq val (symbol-value variable)
621 locus (variable-binding-locus variable)))))
622 (help-setup-xref (list #'describe-variable variable buffer)
623 (called-interactively-p 'interactive))
624 (with-help-window (help-buffer)
625 (with-current-buffer buffer
626 (prin1 variable)
627 (setq file-name (find-lisp-object-file-name variable 'defvar))
629 (if file-name
630 (progn
631 (princ " is a variable defined in `")
632 (princ (if (eq file-name 'C-source)
633 "C source code"
634 (file-name-nondirectory file-name)))
635 (princ "'.\n")
636 (with-current-buffer standard-output
637 (save-excursion
638 (re-search-backward "`\\([^`']+\\)'" nil t)
639 (help-xref-button 1 'help-variable-def
640 variable file-name)))
641 (if valvoid
642 (princ "It is void as a variable.")
643 (princ "Its ")))
644 (if valvoid
645 (princ " is void as a variable.")
646 (princ "'s "))))
647 (unless valvoid
648 (with-current-buffer standard-output
649 (setq val-start-pos (point))
650 (princ "value is ")
651 (let ((from (point)))
652 (terpri)
653 (pp val)
654 (if (< (point) (+ 68 (line-beginning-position 0)))
655 (delete-region from (1+ from))
656 (delete-region (1- from) from))
657 (let* ((sv (get variable 'standard-value))
658 (origval (and (consp sv)
659 (condition-case nil
660 (eval (car sv))
661 (error :help-eval-error)))))
662 (when (and (consp sv)
663 (not (equal origval val))
664 (not (equal origval :help-eval-error)))
665 (princ "\nOriginal value was \n")
666 (setq from (point))
667 (pp origval)
668 (if (< (point) (+ from 20))
669 (delete-region (1- from) from)))))))
670 (terpri)
671 (when locus
672 (if (bufferp locus)
673 (princ (format "%socal in buffer %s; "
674 (if (get variable 'permanent-local)
675 "Permanently l" "L")
676 (buffer-name)))
677 (princ (format "It is a frame-local variable; ")))
678 (if (not (default-boundp variable))
679 (princ "globally void")
680 (let ((val (default-value variable)))
681 (with-current-buffer standard-output
682 (princ "global value is ")
683 (terpri)
684 ;; Fixme: pp can take an age if you happen to
685 ;; ask for a very large expression. We should
686 ;; probably print it raw once and check it's a
687 ;; sensible size before prettyprinting. -- fx
688 (let ((from (point)))
689 (pp val)
690 ;; See previous comment for this function.
691 ;; (help-xref-on-pp from (point))
692 (if (< (point) (+ from 20))
693 (delete-region (1- from) from))))))
694 (terpri))
696 ;; If the value is large, move it to the end.
697 (with-current-buffer standard-output
698 (when (> (count-lines (point-min) (point-max)) 10)
699 ;; Note that setting the syntax table like below
700 ;; makes forward-sexp move over a `'s' at the end
701 ;; of a symbol.
702 (set-syntax-table emacs-lisp-mode-syntax-table)
703 (goto-char val-start-pos)
704 ;; The line below previously read as
705 ;; (delete-region (point) (progn (end-of-line) (point)))
706 ;; which suppressed display of the buffer local value for
707 ;; large values.
708 (when (looking-at "value is") (replace-match ""))
709 (save-excursion
710 (insert "\n\nValue:")
711 (set (make-local-variable 'help-button-cache)
712 (point-marker)))
713 (insert "value is shown ")
714 (insert-button "below"
715 'action help-button-cache
716 'follow-link t
717 'help-echo "mouse-2, RET: show value")
718 (insert ".\n")))
719 (terpri)
721 (let* ((alias (condition-case nil
722 (indirect-variable variable)
723 (error variable)))
724 (obsolete (get variable 'byte-obsolete-variable))
725 (use (car obsolete))
726 (safe-var (get variable 'safe-local-variable))
727 (doc (or (documentation-property variable 'variable-documentation)
728 (documentation-property alias 'variable-documentation)))
729 (extra-line nil))
730 ;; Add a note for variables that have been make-var-buffer-local.
731 (when (and (local-variable-if-set-p variable)
732 (or (not (local-variable-p variable))
733 (with-temp-buffer
734 (local-variable-if-set-p variable))))
735 (setq extra-line t)
736 (princ " Automatically becomes buffer-local when set in any fashion.\n"))
738 ;; Mention if it's an alias
739 (unless (eq alias variable)
740 (setq extra-line t)
741 (princ (format " This variable is an alias for `%s'.\n" alias)))
743 (when obsolete
744 (setq extra-line t)
745 (princ " This variable is obsolete")
746 (if (cdr obsolete) (princ (format " since %s" (cdr obsolete))))
747 (princ (cond ((stringp use) (concat ";\n " use))
748 (use (format ";\n use `%s' instead." (car obsolete)))
749 (t ".")))
750 (terpri))
752 (when (member (cons variable val) file-local-variables-alist)
753 (setq extra-line t)
754 (if (member (cons variable val) dir-local-variables-alist)
755 (let ((file (and (buffer-file-name)
756 (not (file-remote-p (buffer-file-name)))
757 (dir-locals-find-file (buffer-file-name)))))
758 (princ " This variable is a directory local variable")
759 (when file
760 (princ (concat "\n from the file \""
761 (if (consp file)
762 (car file)
763 file)
764 "\"")))
765 (princ ".\n"))
766 (princ " This variable is a file local variable.\n")))
768 (when (memq variable ignored-local-variables)
769 (setq extra-line t)
770 (princ " This variable is ignored when used as a file local \
771 variable.\n"))
773 ;; Can be both risky and safe, eg auto-fill-function.
774 (when (risky-local-variable-p variable)
775 (setq extra-line t)
776 (princ " This variable is potentially risky when used as a \
777 file local variable.\n")
778 (when (assq variable safe-local-variable-values)
779 (princ " However, you have added it to \
780 `safe-local-variable-values'.\n")))
782 (when safe-var
783 (setq extra-line t)
784 (princ " This variable is safe as a file local variable ")
785 (princ "if its value\n satisfies the predicate ")
786 (princ (if (byte-code-function-p safe-var)
787 "which is byte-compiled expression.\n"
788 (format "`%s'.\n" safe-var))))
790 (if extra-line (terpri))
791 (princ "Documentation:\n")
792 (with-current-buffer standard-output
793 (insert (or doc "Not documented as a variable."))))
795 ;; Make a link to customize if this variable can be customized.
796 (when (custom-variable-p variable)
797 (let ((customize-label "customize"))
798 (terpri)
799 (terpri)
800 (princ (concat "You can " customize-label " this variable."))
801 (with-current-buffer standard-output
802 (save-excursion
803 (re-search-backward
804 (concat "\\(" customize-label "\\)") nil t)
805 (help-xref-button 1 'help-customize-variable variable))))
806 ;; Note variable's version or package version
807 (let ((output (describe-variable-custom-version-info variable)))
808 (when output
809 (terpri)
810 (terpri)
811 (princ output))))
813 (with-current-buffer standard-output
814 ;; Return the text we displayed.
815 (buffer-string))))))))
818 ;;;###autoload
819 (defun describe-syntax (&optional buffer)
820 "Describe the syntax specifications in the syntax table of BUFFER.
821 The descriptions are inserted in a help buffer, which is then displayed.
822 BUFFER defaults to the current buffer."
823 (interactive)
824 (setq buffer (or buffer (current-buffer)))
825 (help-setup-xref (list #'describe-syntax buffer)
826 (called-interactively-p 'interactive))
827 (with-help-window (help-buffer)
828 (let ((table (with-current-buffer buffer (syntax-table))))
829 (with-current-buffer standard-output
830 (describe-vector table 'internal-describe-syntax-value)
831 (while (setq table (char-table-parent table))
832 (insert "\nThe parent syntax table is:")
833 (describe-vector table 'internal-describe-syntax-value))))))
835 (defun help-describe-category-set (value)
836 (insert (cond
837 ((null value) "default")
838 ((char-table-p value) "deeper char-table ...")
839 (t (condition-case err
840 (category-set-mnemonics value)
841 (error "invalid"))))))
843 ;;;###autoload
844 (defun describe-categories (&optional buffer)
845 "Describe the category specifications in the current category table.
846 The descriptions are inserted in a buffer, which is then displayed.
847 If BUFFER is non-nil, then describe BUFFER's category table instead.
848 BUFFER should be a buffer or a buffer name."
849 (interactive)
850 (setq buffer (or buffer (current-buffer)))
851 (help-setup-xref (list #'describe-categories buffer)
852 (called-interactively-p 'interactive))
853 (with-help-window (help-buffer)
854 (let* ((table (with-current-buffer buffer (category-table)))
855 (docs (char-table-extra-slot table 0)))
856 (if (or (not (vectorp docs)) (/= (length docs) 95))
857 (error "Invalid first extra slot in this category table\n"))
858 (with-current-buffer standard-output
859 (insert "Legend of category mnemonics (see the tail for the longer description)\n")
860 (let ((pos (point)) (items 0) lines n)
861 (dotimes (i 95)
862 (if (aref docs i) (setq items (1+ items))))
863 (setq lines (1+ (/ (1- items) 4)))
864 (setq n 0)
865 (dotimes (i 95)
866 (let ((elt (aref docs i)))
867 (when elt
868 (string-match ".*" elt)
869 (setq elt (match-string 0 elt))
870 (if (>= (length elt) 17)
871 (setq elt (concat (substring elt 0 14) "...")))
872 (if (< (point) (point-max))
873 (move-to-column (* 20 (/ n lines)) t))
874 (insert (+ i ?\s) ?: elt)
875 (if (< (point) (point-max))
876 (forward-line 1)
877 (insert "\n"))
878 (setq n (1+ n))
879 (if (= (% n lines) 0)
880 (goto-char pos))))))
881 (goto-char (point-max))
882 (insert "\n"
883 "character(s)\tcategory mnemonics\n"
884 "------------\t------------------")
885 (describe-vector table 'help-describe-category-set)
886 (insert "Legend of category mnemonics:\n")
887 (dotimes (i 95)
888 (let ((elt (aref docs i)))
889 (when elt
890 (if (string-match "\n" elt)
891 (setq elt (substring elt (match-end 0))))
892 (insert (+ i ?\s) ": " elt "\n"))))
893 (while (setq table (char-table-parent table))
894 (insert "\nThe parent category table is:")
895 (describe-vector table 'help-describe-category-set))))))
898 ;;; Replacements for old lib-src/ programs. Don't seem especially useful.
900 ;; Replaces lib-src/digest-doc.c.
901 ;;;###autoload
902 (defun doc-file-to-man (file)
903 "Produce an nroff buffer containing the doc-strings from the DOC file."
904 (interactive (list (read-file-name "Name of DOC file: " doc-directory
905 internal-doc-file-name t)))
906 (or (file-readable-p file)
907 (error "Cannot read file `%s'" file))
908 (pop-to-buffer (generate-new-buffer "*man-doc*"))
909 (setq buffer-undo-list t)
910 (insert ".TH \"Command Summary for GNU Emacs\"\n"
911 ".AU Richard M. Stallman\n")
912 (insert-file-contents file)
913 (let (notfirst)
914 (while (search-forward "\x1f" nil 'move)
915 (if (looking-at "S")
916 (delete-region (1- (point)) (line-end-position))
917 (delete-char -1)
918 (if notfirst
919 (insert "\n.DE\n")
920 (setq notfirst t))
921 (insert "\n.SH ")
922 (insert (if (looking-at "F") "Function " "Variable "))
923 (delete-char 1)
924 (forward-line 1)
925 (insert ".DS L\n"))))
926 (insert "\n.DE\n")
927 (setq buffer-undo-list nil)
928 (nroff-mode))
930 ;; Replaces lib-src/sorted-doc.c.
931 ;;;###autoload
932 (defun doc-file-to-info (file)
933 "Produce a texinfo buffer with sorted doc-strings from the DOC file."
934 (interactive (list (read-file-name "Name of DOC file: " doc-directory
935 internal-doc-file-name t)))
936 (or (file-readable-p file)
937 (error "Cannot read file `%s'" file))
938 (let ((i 0) type name doc alist)
939 (with-temp-buffer
940 (insert-file-contents file)
941 ;; The characters "@{}" need special treatment.
942 (while (re-search-forward "[@{}]" nil t)
943 (backward-char)
944 (insert "@")
945 (forward-char 1))
946 (goto-char (point-min))
947 (while (search-forward "\x1f" nil t)
948 (unless (looking-at "S")
949 (setq type (char-after)
950 name (buffer-substring (1+ (point)) (line-end-position))
951 doc (buffer-substring (line-beginning-position 2)
952 (if (search-forward "\x1f" nil 'move)
953 (1- (point))
954 (point)))
955 alist (cons (list name type doc) alist))
956 (backward-char 1))))
957 (pop-to-buffer (generate-new-buffer "*info-doc*"))
958 (setq buffer-undo-list t)
959 ;; Write the output header.
960 (insert "\\input texinfo @c -*-texinfo-*-\n"
961 "@setfilename emacsdoc.info\n"
962 "@settitle Command Summary for GNU Emacs\n"
963 "@finalout\n"
964 "\n@node Top\n"
965 "@unnumbered Command Summary for GNU Emacs\n\n"
966 "@table @asis\n\n"
967 "@iftex\n"
968 "@global@let@ITEM@item\n"
969 "@def@item{@filbreak@vskip5pt@ITEM}\n"
970 "@font@tensy cmsy10 scaled @magstephalf\n"
971 "@font@teni cmmi10 scaled @magstephalf\n"
972 "@def\\{{@tensy@char110}}\n" ; this backslash goes with cmr10
973 "@def|{{@tensy@char106}}\n"
974 "@def@{{{@tensy@char102}}\n"
975 "@def@}{{@tensy@char103}}\n"
976 "@def<{{@teni@char62}}\n"
977 "@def>{{@teni@char60}}\n"
978 "@chardef@@64\n"
979 "@catcode43=12\n"
980 "@tableindent-0.2in\n"
981 "@end iftex\n")
982 ;; Sort the array by name; within each name, by type (functions first).
983 (setq alist (sort alist (lambda (e1 e2)
984 (if (string-equal (car e1) (car e2))
985 (<= (cadr e1) (cadr e2))
986 (string-lessp (car e1) (car e2))))))
987 ;; Print each function.
988 (dolist (e alist)
989 (insert "\n@item "
990 (if (char-equal (cadr e) ?\F) "Function" "Variable")
991 " @code{" (car e) "}\n@display\n"
992 (nth 2 e)
993 "\n@end display\n")
994 ;; Try to avoid a save size overflow in the TeX output routine.
995 (if (zerop (setq i (% (1+ i) 100)))
996 (insert "\n@end table\n@table @asis\n")))
997 (insert "@end table\n"
998 "@bye\n")
999 (setq buffer-undo-list nil)
1000 (texinfo-mode)))
1002 (provide 'help-fns)
1004 ;;; help-fns.el ends here