1 ;;; help-fns.el --- Complex help functions -*- lexical-binding: t -*-
3 ;; Copyright (C) 1985-1986, 1993-1994, 1998-2012
4 ;; Free Software Foundation, Inc.
7 ;; Keywords: help, internal
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/>.
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
38 (defun describe-function (function)
39 "Display the full documentation of FUNCTION (a symbol)."
41 (let ((fn (function-called-at-point))
42 (enable-recursive-minibuffers t
)
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
"")
52 (message "You didn't specify a function")
53 (help-setup-xref (list #'describe-function function
)
54 (called-interactively-p 'interactive
))
56 (with-help-window (help-buffer)
58 ;; Use " is " instead of a colon so that
59 ;; it is easier to get out the function name using forward-sexp.
61 (describe-function-1 function
)
62 (with-current-buffer standard-output
63 ;; Return the text we displayed.
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, where USAGE info
69 is a string describing the argument list of DEF, such as
70 \"(apply FUNCTION &rest ARGUMENTS)\".
71 DEF is the function whose usage we're looking for in DOCSTRING."
72 ;; Functions can get the calling sequence at the end of the doc string.
73 ;; In cases where `function' has been fset to a subr we can't search for
74 ;; function's name in the doc string so we use `fn' as the anonymous
75 ;; function name instead.
76 (when (and docstring
(string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring
))
78 ;; Replace `fn' with the actual function name.
79 (if (consp def
) "anonymous" def
)
80 (match-string 1 docstring
))
81 (unless (zerop (match-beginning 0))
82 (substring docstring
0 (match-beginning 0))))))
84 ;; FIXME: Move to subr.el?
85 (defun help-add-fundoc-usage (docstring arglist
)
86 "Add the usage info to DOCSTRING.
87 If DOCSTRING already has a usage info, then just return it unchanged.
88 The usage info is built from ARGLIST. DOCSTRING can be nil.
89 ARGLIST can also be t or a string of the form \"(FUN ARG1 ARG2 ...)\"."
90 (unless (stringp docstring
) (setq docstring
""))
91 (if (or (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring
)
95 (if (string-match "\n?\n\\'" docstring
)
96 (if (< (- (match-end 0) (match-beginning 0)) 2) "\n" "")
98 (if (and (stringp arglist
)
99 (string-match "\\`([^ ]+\\(.*\\))\\'" arglist
))
100 (concat "(fn" (match-string 1 arglist
) ")")
101 (format "%S" (help-make-usage 'fn arglist
))))))
103 ;; FIXME: Move to subr.el?
104 (defun help-function-arglist (def &optional preserve-names
)
105 "Return a formal argument list for the function DEF.
106 IF PRESERVE-NAMES is non-nil, return a formal arglist that uses
107 the same names as used in the original source code, when possible."
108 ;; Handle symbols aliased to other symbols.
109 (if (and (symbolp def
) (fboundp def
)) (setq def
(indirect-function def
)))
110 ;; If definition is a macro, find the function inside it.
111 (if (eq (car-safe def
) 'macro
) (setq def
(cdr def
)))
113 ((and (byte-code-function-p def
) (listp (aref def
0))) (aref def
0))
114 ((eq (car-safe def
) 'lambda
) (nth 1 def
))
115 ((eq (car-safe def
) 'closure
) (nth 2 def
))
116 ((or (and (byte-code-function-p def
) (integerp (aref def
0)))
118 (or (when preserve-names
119 (let* ((doc (condition-case nil
(documentation def
) (error nil
)))
120 (docargs (if doc
(car (help-split-fundoc doc nil
))))
122 (cdar (read-from-string (downcase docargs
)))))
125 (dolist (arg arglist
)
126 (unless (and (symbolp arg
)
127 (let ((name (symbol-name arg
)))
128 (if (eq (aref name
0) ?
&)
129 (memq arg
'(&rest
&optional
))
130 (not (string-match "\\." name
)))))
132 (when valid arglist
)))
133 (let* ((args-desc (if (not (subrp def
))
135 (let ((a (subr-arity def
)))
137 (if (numberp (cdr a
))
140 (max (lsh args-desc -
8))
141 (min (logand args-desc
127))
142 (rest (logand args-desc
128))
145 (push (intern (concat "arg" (number-to-string (1+ i
)))) arglist
))
147 (push '&optional arglist
)
148 (dotimes (i (- max min
))
149 (push (intern (concat "arg" (number-to-string (+ 1 i min
))))
151 (unless (zerop rest
) (push '&rest arglist
) (push 'rest arglist
))
152 (nreverse arglist
))))
153 ((and (autoloadp def
) (not (eq (nth 4 def
) 'keymap
)))
154 "[Arg list not available until function definition is loaded.]")
157 ;; FIXME: Move to subr.el?
158 (defun help-make-usage (function arglist
)
159 (cons (if (symbolp function
) function
'anonymous
)
160 (mapcar (lambda (arg)
161 (if (not (symbolp arg
)) arg
162 (let ((name (symbol-name arg
)))
164 ((string-match "\\`&" name
) arg
)
165 ((string-match "\\`_" name
)
166 (intern (upcase (substring name
1))))
167 (t (intern (upcase name
)))))))
170 ;; Could be this, if we make symbol-file do the work below.
171 ;; (defun help-C-file-name (subr-or-var kind)
172 ;; "Return the name of the C file where SUBR-OR-VAR is defined.
173 ;; KIND should be `var' for a variable or `subr' for a subroutine."
174 ;; (symbol-file (if (symbolp subr-or-var) subr-or-var
175 ;; (subr-name subr-or-var))
176 ;; (if (eq kind 'var) 'defvar 'defun)))
178 (defun help-C-file-name (subr-or-var kind
)
179 "Return the name of the C file where SUBR-OR-VAR is defined.
180 KIND should be `var' for a variable or `subr' for a subroutine."
181 (let ((docbuf (get-buffer-create " *DOC*"))
182 (name (if (eq 'var kind
)
183 (concat "V" (symbol-name subr-or-var
))
184 (concat "F" (subr-name subr-or-var
)))))
185 (with-current-buffer docbuf
186 (goto-char (point-min))
188 (insert-file-contents-literally
189 (expand-file-name internal-doc-file-name doc-directory
)))
190 (let ((file (catch 'loop
192 (let ((pnt (search-forward (concat "\x1f" name
"\n"))))
193 (re-search-backward "\x1fS\\(.*\\)")
194 (let ((file (match-string 1)))
195 (if (member file build-files
)
197 (goto-char pnt
))))))))
198 (if (string-match "^ns.*\\(\\.o\\|obj\\)\\'" file
)
199 (setq file
(replace-match ".m" t t file
1))
200 (if (string-match "\\.\\(o\\|obj\\)\\'" file
)
201 (setq file
(replace-match ".c" t t file
))))
202 (if (string-match "\\.\\(c\\|m\\)\\'" file
)
206 (defcustom help-downcase-arguments nil
207 "If non-nil, argument names in *Help* buffers are downcased."
212 (defun help-highlight-arg (arg)
213 "Highlight ARG as an argument name for a *Help* buffer.
214 Return ARG in face `help-argument-name'; ARG is also downcased
215 if the variable `help-downcase-arguments' is non-nil."
216 (propertize (if help-downcase-arguments
(downcase arg
) arg
)
217 'face
'help-argument-name
))
219 (defun help-do-arg-highlight (doc args
)
220 (with-syntax-table (make-syntax-table emacs-lisp-mode-syntax-table
)
221 (modify-syntax-entry ?\-
"w")
223 (setq doc
(replace-regexp-in-string
224 ;; This is heuristic, but covers all common cases
226 (concat "\\<" ; beginning of word
227 "\\(?:[a-z-]*-\\)?" ; for xxx-ARG
231 "\\(?:es\\|s\\|th\\)?" ; for ARGth, ARGs
232 "\\(?:-[a-z0-9-]+\\)?" ; for ARG-xxx, ARG-n
233 "\\(?:-[{([<`\"].*?\\)?"; for ARG-{x}, (x), <x>, [x], `x'
235 (help-highlight-arg arg
)
239 (defun help-highlight-arguments (usage doc
&rest args
)
240 (when (and usage
(string-match "^(" usage
))
243 (goto-char (point-min))
244 (let ((case-fold-search nil
)
245 (next (not (or args
(looking-at "\\["))))
247 ;; Make a list of all arguments
248 (skip-chars-forward "^ ")
250 (or opt
(not (looking-at " &")) (setq opt t
))
251 (if (not (re-search-forward " \\([\\[(]*\\)\\([^] &)\.]+\\)" nil t
))
253 (setq args
(cons (match-string 2) args
))
254 (when (and opt
(string= (match-string 1) "("))
255 ;; A pesky CL-style optional argument with default value,
256 ;; so let's skip over it
257 (search-backward "(")
258 (goto-char (scan-sexps (point) 1)))))
259 ;; Highlight arguments in the USAGE string
260 (setq usage
(help-do-arg-highlight (buffer-string) args
))
261 ;; Highlight arguments in the DOC string
262 (setq doc
(and doc
(help-do-arg-highlight doc args
))))))
263 ;; Return value is like the one from help-split-fundoc, but highlighted
266 ;; The following function was compiled from the former functions
267 ;; `describe-simplify-lib-file-name' and `find-source-lisp-file' with
268 ;; some excerpts from `describe-function-1' and `describe-variable'.
269 ;; The only additional twists provided are (1) locate the defining file
270 ;; for autoloaded functions, and (2) give preference to files in the
271 ;; "install directory" (directories found via `load-path') rather than
272 ;; to files in the "compile directory" (directories found by searching
273 ;; the loaddefs.el file). We autoload it because it's also used by
274 ;; `describe-face' (instead of `describe-simplify-lib-file-name').
277 (defun find-lisp-object-file-name (object type
)
278 "Guess the file that defined the Lisp object OBJECT, of type TYPE.
279 OBJECT should be a symbol associated with a function, variable, or face;
280 alternatively, it can be a function definition.
281 If TYPE is `defvar', search for a variable definition.
282 If TYPE is `defface', search for a face definition.
283 If TYPE is the value returned by `symbol-function' for a function symbol,
284 search for a function definition.
286 The return value is the absolute name of a readable file where OBJECT is
287 defined. If several such files exist, preference is given to a file
288 found via `load-path'. The return value can also be `C-source', which
289 means that OBJECT is a function or variable defined in C. If no
290 suitable file is found, return nil."
291 (let* ((autoloaded (autoloadp type
))
292 (file-name (or (and autoloaded
(nth 1 type
))
294 object
(if (memq type
(list 'defvar
'defface
))
299 ;; An autoloaded function: Locate the file since `symbol-function'
300 ;; has only returned a bare string here.
302 (locate-file file-name load-path
'(".el" ".elc") 'readable
)))
303 ((and (stringp file-name
)
304 (string-match "[.]*loaddefs.el\\'" file-name
))
305 ;; An autoloaded variable or face. Visit loaddefs.el in a buffer
306 ;; and try to extract the defining file. The following form is
307 ;; from `describe-function-1' and `describe-variable'.
310 (find-function-search-for-symbol object nil file-name
)
313 (with-current-buffer (car location
)
314 (goto-char (cdr location
))
315 (when (re-search-backward
316 "^;;; Generated autoloads from \\(.*\\)" nil t
)
319 (file-name-sans-extension
320 (match-string-no-properties 1))
321 load-path
'(".el" ".elc") 'readable
))))))))
324 ((and (not file-name
) (subrp type
))
325 ;; A built-in function. The form is from `describe-function-1'.
326 (if (get-buffer " *DOC*")
327 (help-C-file-name type
'subr
)
329 ((and (not file-name
) (symbolp object
)
330 (integerp (get object
'variable-documentation
)))
331 ;; A variable defined in C. The form is from `describe-variable'.
332 (if (get-buffer " *DOC*")
333 (help-C-file-name object
'var
)
335 ((not (stringp file-name
))
336 ;; If we don't have a file-name string by now, we lost.
338 ;; Now, `file-name' should have become an absolute file name.
339 ;; For files loaded from ~/.emacs.elc, try ~/.emacs.
341 (and (string-equal file-name
342 (expand-file-name ".emacs.elc" "~"))
343 (file-readable-p (setq fn
(expand-file-name ".emacs" "~")))
345 ;; When the Elisp source file can be found in the install
346 ;; directory, return the name of that file.
348 (if (string-match "[.]elc\\'" file-name
)
349 (substring-no-properties file-name
0 -
1)
351 (or (and (file-readable-p lib-name
) lib-name
)
352 ;; The library might be compressed.
353 (and (file-readable-p (concat lib-name
".gz")) lib-name
))))
354 ((let* ((lib-name (file-name-nondirectory file-name
))
355 ;; The next form is from `describe-simplify-lib-file-name'.
357 ;; Try converting the absolute file name to a library
358 ;; name, convert that back to a file name and see if we
359 ;; get the original one. If so, they are equivalent.
360 (if (equal file-name
(locate-file lib-name load-path
'("")))
361 (if (string-match "[.]elc\\'" lib-name
)
362 (substring-no-properties lib-name
0 -
1)
365 ;; The next three forms are from `find-source-lisp-file'.
366 (elc-file (locate-file
368 (if (string-match "\\.el\\'" file-name
)
371 load-path nil
'readable
))
374 (insert-file-contents-literally elc-file nil
0 256)
377 (string-match ";;; from file \\(.*\\.el\\)" str
)
378 (match-string 1 str
))))
379 (and src-file
(file-readable-p src-file
) src-file
))))))
381 (declare-function ad-get-advice-info
"advice" (function))
383 (defun help-fns--key-bindings (function)
384 (when (commandp function
)
385 (let ((pt2 (with-current-buffer standard-output
(point)))
386 (remapped (command-remapping function
)))
387 (unless (memq remapped
'(ignore undefined
))
388 (let ((keys (where-is-internal
389 (or remapped function
) overriding-local-map nil nil
))
391 (if (and (eq function
'self-insert-command
)
392 (vectorp (car-safe keys
))
393 (consp (aref (car keys
) 0)))
394 (princ "It is bound to many ordinary text characters.\n")
395 ;; Which non-control non-meta keys run this command?
397 (if (member (event-modifiers (aref key
0)) '(nil (shift)))
398 (push key non-modified-keys
)))
400 (princ "Its keys are remapped to `")
401 (princ (symbol-name remapped
))
406 "Without this remapping, it would be bound to "
408 ;; If lots of ordinary text characters run this command,
409 ;; don't mention them one by one.
410 (if (< (length non-modified-keys
) 10)
411 (princ (mapconcat 'key-description keys
", "))
412 (dolist (key non-modified-keys
)
413 (setq keys
(delq key keys
)))
416 (princ (mapconcat 'key-description keys
", "))
417 (princ ", and many ordinary text characters"))
418 (princ "many ordinary text characters"))))
419 (when (or remapped keys non-modified-keys
)
423 (with-current-buffer standard-output
424 (fill-region-as-paragraph pt2
(point))
425 (unless (looking-back "\n\n")
428 (defun help-fns--compiler-macro (function)
429 (let ((handler (function-get function
'compiler-macro
)))
431 (insert "\nThis function has a compiler macro")
432 (let ((lib (get function
'compiler-macro-file
)))
433 ;; FIXME: rather than look at the compiler-macro-file property,
434 ;; just look at `handler' itself.
436 (insert (format " in `%s'" lib
))
438 (re-search-backward "`\\([^`']+\\)'" nil t
)
439 (help-xref-button 1 'help-function-cmacro function lib
))))
442 (defun help-fns--signature (function doc real-def real-function
)
443 (unless (keymapp function
) ; If definition is a keymap, skip arglist note.
444 (let* ((advertised (gethash real-def advertised-signature-table t
))
445 (arglist (if (listp advertised
)
446 advertised
(help-function-arglist real-def
)))
447 (usage (help-split-fundoc doc function
)))
448 (if usage
(setq doc
(cdr usage
)))
450 ((and usage
(not (listp advertised
))) (car usage
))
452 (format "%S" (help-make-usage function arglist
)))
453 ((stringp arglist
) arglist
)
454 ;; Maybe the arglist is in the docstring of a symbol
455 ;; this one is aliased to.
456 ((let ((fun real-function
))
457 (while (and (symbolp fun
)
458 (setq fun
(symbol-function fun
))
459 (not (setq usage
(help-split-fundoc
464 ((or (stringp real-def
)
466 (format "\nMacro: %s" (format-kbd-macro real-def
)))
467 (t "[Missing arglist. Please make a bug report.]")))
468 (high (help-highlight-arguments use doc
)))
469 (let ((fill-begin (point)))
470 (insert (car high
) "\n")
471 (fill-region fill-begin
(point)))
474 (defun help-fns--parent-mode (function)
475 ;; If this is a derived mode, link to the parent.
476 (let ((parent-mode (and (symbolp function
)
478 'derived-mode-parent
))))
480 (insert "\nParent mode: `")
482 (insert (format "%s" parent-mode
))
483 (make-text-button beg
(point)
485 'help-args
(list parent-mode
)))
488 (defun help-fns--obsolete (function)
489 (let* ((obsolete (and
490 ;; `function' might be a lambda construct.
492 (get function
'byte-obsolete-info
)))
493 (use (car obsolete
)))
495 (insert "\nThis function is obsolete")
496 (when (nth 2 obsolete
)
497 (insert (format " since %s" (nth 2 obsolete
))))
498 (insert (cond ((stringp use
) (concat ";\n" use
))
499 (use (format ";\nuse `%s' instead." use
))
503 ;; We could use `symbol-file' but this is a wee bit more efficient.
504 (defun help-fns--autoloaded-p (function file
)
505 "Return non-nil if FUNCTION has previously been autoloaded.
506 FILE is the file where FUNCTION was probably defined."
507 (let* ((file (file-name-sans-extension (file-truename file
)))
508 (load-hist load-history
)
509 (target (cons t function
))
511 (while (and load-hist
(not found
))
512 (and (caar load-hist
)
513 (equal (file-name-sans-extension (caar load-hist
)) file
)
514 (setq found
(member target
(cdar load-hist
))))
515 (setq load-hist
(cdr load-hist
)))
519 (defun describe-function-1 (function)
520 (let* ((advised (and (symbolp function
) (featurep 'advice
)
521 (ad-get-advice-info function
)))
522 ;; If the function is advised, use the symbol that has the
523 ;; real definition, if that symbol is already set up.
526 (let ((origname (cdr (assq 'origname advised
))))
527 (and (fboundp origname
) origname
)))
529 ;; Get the real definition.
530 (def (if (symbolp real-function
)
531 (symbol-function real-function
)
533 (aliased (symbolp def
))
534 (real-def (if aliased
536 (while (and (fboundp f
)
537 (symbolp (symbol-function f
)))
538 (setq f
(symbol-function f
)))
541 (file-name (find-lisp-object-file-name function def
))
542 (pt1 (with-current-buffer (help-buffer) (point)))
543 (beg (if (and (or (byte-code-function-p def
)
545 (memq (car-safe def
) '(macro lambda closure
)))
547 (help-fns--autoloaded-p function file-name
))
549 "an interactive autoloaded "
551 (if (commandp def
) "an interactive " "a "))))
553 ;; Print what kind of function-like object FUNCTION is.
554 (princ (cond ((or (stringp def
) (vectorp def
))
557 (if (eq 'unevalled
(cdr (subr-arity def
)))
558 (concat beg
"special form")
559 (concat beg
"built-in function")))
560 ((byte-code-function-p def
)
561 (concat beg
"compiled Lisp function"))
563 (format "an alias for `%s'" real-def
))
564 ((eq (car-safe def
) 'lambda
)
565 (concat beg
"Lisp function"))
566 ((eq (car-safe def
) 'macro
)
567 (concat beg
"Lisp macro"))
568 ((eq (car-safe def
) 'closure
)
569 (concat beg
"Lisp closure"))
571 (format "%s autoloaded %s"
572 (if (commandp def
) "an interactive" "an")
573 (if (eq (nth 4 def
) 'keymap
) "keymap"
574 (if (nth 4 def
) "Lisp macro" "Lisp function"))))
577 (elts (cdr-safe def
)))
579 (if (char-table-p (car-safe elts
))
582 (setq elts
(cdr-safe elts
)))
583 (concat beg
(if is-full
"keymap" "sparse keymap"))))
586 (if (and aliased
(not (fboundp real-def
)))
587 (princ ",\nwhich is not defined. Please make a bug report.")
588 (with-current-buffer standard-output
591 (when (re-search-backward "alias for `\\([^`']+\\)'" nil t
)
592 (help-xref-button 1 'help-function real-def
)))))
596 ;; We used to add .el to the file name,
597 ;; but that's completely wrong when the user used load-file.
598 (princ (if (eq file-name
'C-source
)
600 (file-name-nondirectory file-name
)))
602 ;; Make a hyperlink to the library.
603 (with-current-buffer standard-output
605 (re-search-backward "`\\([^`']+\\)'" nil t
)
606 (help-xref-button 1 'help-function-def function file-name
))))
608 (with-current-buffer (help-buffer)
609 (fill-region-as-paragraph (save-excursion (goto-char pt1
) (forward-line 0) (point))
613 (let* ((doc-raw (condition-case err
614 (documentation function t
)
615 (error (format "No Doc! %S" err
))))
616 ;; If the function is autoloaded, and its docstring has
617 ;; key substitution constructs, load the library.
619 (and (autoloadp real-def
)
620 help-enable-auto-load
621 (string-match "\\([^\\]=\\|[^=]\\|\\`\\)\\\\[[{<]"
623 (load (cadr real-def
) t
))
624 (substitute-command-keys doc-raw
))))
626 (help-fns--key-bindings function
)
627 (with-current-buffer standard-output
628 (setq doc
(help-fns--signature function doc real-def real-function
))
630 (help-fns--compiler-macro function
)
631 (help-fns--parent-mode function
)
632 (help-fns--obsolete function
)
635 (or doc
"Not documented.")))))))
641 (defun variable-at-point (&optional any-symbol
)
642 "Return the bound variable symbol found at or before point.
643 Return 0 if there is no such symbol.
644 If ANY-SYMBOL is non-nil, don't insist the symbol be bound."
645 (with-syntax-table emacs-lisp-mode-syntax-table
646 (or (condition-case ()
648 (skip-chars-forward "'")
649 (or (not (zerop (skip-syntax-backward "_w")))
650 (eq (char-syntax (following-char)) ?w
)
651 (eq (char-syntax (following-char)) ?_
)
653 (skip-chars-forward "'")
654 (let ((obj (read (current-buffer))))
655 (and (symbolp obj
) (boundp obj
) obj
)))
657 (let* ((str (find-tag-default))
658 (sym (if str
(intern-soft str
))))
659 (if (and sym
(or any-symbol
(boundp sym
)))
662 (when (and str
(string-match "\\`\\W*\\(.*?\\)\\W*\\'" str
))
663 (setq sym
(intern-soft (match-string 1 str
)))
664 (and (or any-symbol
(boundp sym
)) sym
)))))
667 (defun describe-variable-custom-version-info (variable)
668 (let ((custom-version (get variable
'custom-version
))
669 (cpv (get variable
'custom-package-version
))
673 (format "This variable was introduced, or its default value was changed, in\nversion %s of Emacs.\n"
676 (let* ((package (car-safe cpv
))
677 (version (if (listp (cdr-safe cpv
))
680 (pkg-versions (assq package customize-package-emacs-version-alist
))
681 (emacsv (cdr (assoc version pkg-versions
))))
682 (if (and package version
)
684 (format (concat "This variable was introduced, or its default value was changed, in\nversion %s of the %s package"
686 (format " that is part of Emacs %s" emacsv
))
688 version package
))))))
692 (defun describe-variable (variable &optional buffer frame
)
693 "Display the full documentation of VARIABLE (a symbol).
694 Returns the documentation as a string, also.
695 If VARIABLE has a buffer-local value in BUFFER or FRAME
696 \(default to the current buffer and current frame),
697 it is displayed along with the global value."
699 (let ((v (variable-at-point))
700 (enable-recursive-minibuffers t
)
702 (setq val
(completing-read (if (symbolp v
)
704 "Describe variable (default %s): " v
)
705 "Describe variable: ")
708 (or (get vv
'variable-documentation
)
709 (and (boundp vv
) (not (keywordp vv
)))))
711 (if (symbolp v
) (symbol-name v
))))
712 (list (if (equal val
"")
715 (unless (buffer-live-p buffer
) (setq buffer
(current-buffer)))
716 (unless (frame-live-p frame
) (setq frame
(selected-frame)))
717 (if (not (symbolp variable
))
718 (message "You did not specify a variable")
720 (let ((valvoid (not (with-current-buffer buffer
(boundp variable
))))
721 (permanent-local (get variable
'permanent-local
))
722 val val-start-pos locus
)
723 ;; Extract the value before setting up the output buffer,
724 ;; in case `buffer' *is* the output buffer.
726 (with-selected-frame frame
727 (with-current-buffer buffer
728 (setq val
(symbol-value variable
)
729 locus
(variable-binding-locus variable
)))))
730 (help-setup-xref (list #'describe-variable variable buffer
)
731 (called-interactively-p 'interactive
))
732 (with-help-window (help-buffer)
733 (with-current-buffer buffer
735 (setq file-name
(find-lisp-object-file-name variable
'defvar
))
739 (princ " is a variable defined in `")
740 (princ (if (eq file-name
'C-source
)
742 (file-name-nondirectory file-name
)))
744 (with-current-buffer standard-output
746 (re-search-backward "`\\([^`']+\\)'" nil t
)
747 (help-xref-button 1 'help-variable-def
748 variable file-name
)))
750 (princ "It is void as a variable.")
753 (princ " is void as a variable.")
756 (with-current-buffer standard-output
757 (setq val-start-pos
(point))
760 (line-beg (line-beginning-position))
762 (let ((print-quoted t
))
763 (prin1-to-string val
))))
764 (if (< (+ (length print-rep
) (point) (- line-beg
)) 68)
768 (if (< (point) (+ 68 (line-beginning-position 0)))
769 (delete-region from
(1+ from
))
770 (delete-region (1- from
) from
)))
771 (let* ((sv (get variable
'standard-value
))
772 (origval (and (consp sv
)
775 (error :help-eval-error
)))))
776 (when (and (consp sv
)
777 (not (equal origval val
))
778 (not (equal origval
:help-eval-error
)))
779 (princ "\nOriginal value was \n")
782 (if (< (point) (+ from
20))
783 (delete-region (1- from
) from
)))))))
788 (princ (format "Local in buffer %s; "
791 (princ (format "It is a frame-local variable; ")))
792 ((terminal-live-p locus
)
793 (princ (format "It is a terminal-local variable; ")))
795 (princ (format "It is local to %S" locus
))))
796 (if (not (default-boundp variable
))
797 (princ "globally void")
798 (let ((global-val (default-value variable
)))
799 (with-current-buffer standard-output
800 (princ "global value is ")
801 (if (eq val global-val
)
804 ;; Fixme: pp can take an age if you happen to
805 ;; ask for a very large expression. We should
806 ;; probably print it raw once and check it's a
807 ;; sensible size before prettyprinting. -- fx
808 (let ((from (point)))
810 ;; See previous comment for this function.
811 ;; (help-xref-on-pp from (point))
812 (if (< (point) (+ from
20))
813 (delete-region (1- from
) from
)))))))
816 ;; If the value is large, move it to the end.
817 (with-current-buffer standard-output
818 (when (> (count-lines (point-min) (point-max)) 10)
819 ;; Note that setting the syntax table like below
820 ;; makes forward-sexp move over a `'s' at the end
822 (set-syntax-table emacs-lisp-mode-syntax-table
)
823 (goto-char val-start-pos
)
824 ;; The line below previously read as
825 ;; (delete-region (point) (progn (end-of-line) (point)))
826 ;; which suppressed display of the buffer local value for
828 (when (looking-at "value is") (replace-match ""))
830 (insert "\n\nValue:")
831 (set (make-local-variable 'help-button-cache
)
833 (insert "value is shown ")
834 (insert-button "below"
835 'action help-button-cache
837 'help-echo
"mouse-2, RET: show value")
841 (let* ((alias (condition-case nil
842 (indirect-variable variable
)
844 (obsolete (get variable
'byte-obsolete-variable
))
846 (safe-var (get variable
'safe-local-variable
))
847 (doc (condition-case err
848 (or (documentation-property
849 variable
'variable-documentation
)
850 (documentation-property
851 alias
'variable-documentation
))
852 (error (format "Doc not found: %S" err
))))
855 ;; Mention if it's a local variable.
857 ((and (local-variable-if-set-p variable
)
858 (or (not (local-variable-p variable
))
860 (local-variable-if-set-p variable
))))
862 (princ " Automatically becomes ")
864 (princ "permanently "))
865 (princ "buffer-local when set.\n"))
866 ((not permanent-local
))
868 (princ " This variable's buffer-local value is permanent.\n"))
870 (princ " This variable's value is permanent \
871 if it is given a local binding.\n")))
873 ;; Mention if it's an alias.
874 (unless (eq alias variable
)
876 (princ (format " This variable is an alias for `%s'.\n" alias
)))
880 (princ " This variable is obsolete")
882 (princ (format " since %s" (nth 2 obsolete
))))
883 (princ (cond ((stringp use
) (concat ";\n " use
))
884 (use (format ";\n use `%s' instead." (car obsolete
)))
888 (when (member (cons variable val
) file-local-variables-alist
)
890 (if (member (cons variable val
) dir-local-variables-alist
)
891 (let ((file (and (buffer-file-name)
892 (not (file-remote-p (buffer-file-name)))
893 (dir-locals-find-file
894 (buffer-file-name))))
896 (princ " This variable's value is directory-local")
900 (if (consp file
) ; result from cache
901 ;; If the cache element has an mtime, we
902 ;; assume it came from a file.
904 (setq file
(expand-file-name
905 dir-locals-file
(car file
)))
906 ;; Otherwise, assume it was set directly.
907 (setq dir-file nil
)))
910 "for the directory\n `"))
911 (with-current-buffer standard-output
913 file
'type
'help-dir-local-var-def
914 'help-args
(list variable file
)))
916 (princ " This variable's value is file-local.\n")))
918 (when (memq variable ignored-local-variables
)
920 (princ " This variable is ignored as a file-local \
923 ;; Can be both risky and safe, eg auto-fill-function.
924 (when (risky-local-variable-p variable
)
926 (princ " This variable may be risky if used as a \
927 file-local variable.\n")
928 (when (assq variable safe-local-variable-values
)
929 (princ " However, you have added it to \
930 `safe-local-variable-values'.\n")))
934 (princ " This variable is safe as a file local variable ")
935 (princ "if its value\n satisfies the predicate ")
936 (princ (if (byte-code-function-p safe-var
)
937 "which is byte-compiled expression.\n"
938 (format "`%s'.\n" safe-var
))))
940 (if extra-line
(terpri))
941 (princ "Documentation:\n")
942 (with-current-buffer standard-output
943 (insert (or doc
"Not documented as a variable."))))
945 ;; Make a link to customize if this variable can be customized.
946 (when (custom-variable-p variable
)
947 (let ((customize-label "customize"))
950 (princ (concat "You can " customize-label
" this variable."))
951 (with-current-buffer standard-output
954 (concat "\\(" customize-label
"\\)") nil t
)
955 (help-xref-button 1 'help-customize-variable variable
))))
956 ;; Note variable's version or package version
957 (let ((output (describe-variable-custom-version-info variable
)))
963 (with-current-buffer standard-output
964 ;; Return the text we displayed.
965 (buffer-string))))))))
969 (defun describe-syntax (&optional buffer
)
970 "Describe the syntax specifications in the syntax table of BUFFER.
971 The descriptions are inserted in a help buffer, which is then displayed.
972 BUFFER defaults to the current buffer."
974 (setq buffer
(or buffer
(current-buffer)))
975 (help-setup-xref (list #'describe-syntax buffer
)
976 (called-interactively-p 'interactive
))
977 (with-help-window (help-buffer)
978 (let ((table (with-current-buffer buffer
(syntax-table))))
979 (with-current-buffer standard-output
980 (describe-vector table
'internal-describe-syntax-value
)
981 (while (setq table
(char-table-parent table
))
982 (insert "\nThe parent syntax table is:")
983 (describe-vector table
'internal-describe-syntax-value
))))))
985 (defun help-describe-category-set (value)
987 ((null value
) "default")
988 ((char-table-p value
) "deeper char-table ...")
989 (t (condition-case nil
990 (category-set-mnemonics value
)
991 (error "invalid"))))))
994 (defun describe-categories (&optional buffer
)
995 "Describe the category specifications in the current category table.
996 The descriptions are inserted in a buffer, which is then displayed.
997 If BUFFER is non-nil, then describe BUFFER's category table instead.
998 BUFFER should be a buffer or a buffer name."
1000 (setq buffer
(or buffer
(current-buffer)))
1001 (help-setup-xref (list #'describe-categories buffer
)
1002 (called-interactively-p 'interactive
))
1003 (with-help-window (help-buffer)
1004 (let* ((table (with-current-buffer buffer
(category-table)))
1005 (docs (char-table-extra-slot table
0)))
1006 (if (or (not (vectorp docs
)) (/= (length docs
) 95))
1007 (error "Invalid first extra slot in this category table\n"))
1008 (with-current-buffer standard-output
1009 (insert "Legend of category mnemonics (see the tail for the longer description)\n")
1010 (let ((pos (point)) (items 0) lines n
)
1012 (if (aref docs i
) (setq items
(1+ items
))))
1013 (setq lines
(1+ (/ (1- items
) 4)))
1016 (let ((elt (aref docs i
)))
1018 (string-match ".*" elt
)
1019 (setq elt
(match-string 0 elt
))
1020 (if (>= (length elt
) 17)
1021 (setq elt
(concat (substring elt
0 14) "...")))
1022 (if (< (point) (point-max))
1023 (move-to-column (* 20 (/ n lines
)) t
))
1024 (insert (+ i ?\s
) ?
: elt
)
1025 (if (< (point) (point-max))
1029 (if (= (% n lines
) 0)
1030 (goto-char pos
))))))
1031 (goto-char (point-max))
1033 "character(s)\tcategory mnemonics\n"
1034 "------------\t------------------")
1035 (describe-vector table
'help-describe-category-set
)
1036 (insert "Legend of category mnemonics:\n")
1038 (let ((elt (aref docs i
)))
1040 (if (string-match "\n" elt
)
1041 (setq elt
(substring elt
(match-end 0))))
1042 (insert (+ i ?\s
) ": " elt
"\n"))))
1043 (while (setq table
(char-table-parent table
))
1044 (insert "\nThe parent category table is:")
1045 (describe-vector table
'help-describe-category-set
))))))
1048 ;;; Replacements for old lib-src/ programs. Don't seem especially useful.
1050 ;; Replaces lib-src/digest-doc.c.
1052 (defun doc-file-to-man (file)
1053 "Produce an nroff buffer containing the doc-strings from the DOC file."
1054 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1055 internal-doc-file-name t
)))
1056 (or (file-readable-p file
)
1057 (error "Cannot read file `%s'" file
))
1058 (pop-to-buffer (generate-new-buffer "*man-doc*"))
1059 (setq buffer-undo-list t
)
1060 (insert ".TH \"Command Summary for GNU Emacs\"\n"
1061 ".AU Richard M. Stallman\n")
1062 (insert-file-contents file
)
1064 (while (search-forward "\x1f" nil
'move
)
1065 (if (looking-at "S")
1066 (delete-region (1- (point)) (line-end-position))
1072 (insert (if (looking-at "F") "Function " "Variable "))
1075 (insert ".DS L\n"))))
1077 (setq buffer-undo-list nil
)
1080 ;; Replaces lib-src/sorted-doc.c.
1082 (defun doc-file-to-info (file)
1083 "Produce a texinfo buffer with sorted doc-strings from the DOC file."
1084 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1085 internal-doc-file-name t
)))
1086 (or (file-readable-p file
)
1087 (error "Cannot read file `%s'" file
))
1088 (let ((i 0) type name doc alist
)
1090 (insert-file-contents file
)
1091 ;; The characters "@{}" need special treatment.
1092 (while (re-search-forward "[@{}]" nil t
)
1096 (goto-char (point-min))
1097 (while (search-forward "\x1f" nil t
)
1098 (unless (looking-at "S")
1099 (setq type
(char-after)
1100 name
(buffer-substring (1+ (point)) (line-end-position))
1101 doc
(buffer-substring (line-beginning-position 2)
1102 (if (search-forward "\x1f" nil
'move
)
1105 alist
(cons (list name type doc
) alist
))
1106 (backward-char 1))))
1107 (pop-to-buffer (generate-new-buffer "*info-doc*"))
1108 (setq buffer-undo-list t
)
1109 ;; Write the output header.
1110 (insert "\\input texinfo @c -*-texinfo-*-\n"
1111 "@setfilename emacsdoc.info\n"
1112 "@settitle Command Summary for GNU Emacs\n"
1115 "@unnumbered Command Summary for GNU Emacs\n\n"
1118 "@global@let@ITEM@item\n"
1119 "@def@item{@filbreak@vskip5pt@ITEM}\n"
1120 "@font@tensy cmsy10 scaled @magstephalf\n"
1121 "@font@teni cmmi10 scaled @magstephalf\n"
1122 "@def\\{{@tensy@char110}}\n" ; this backslash goes with cmr10
1123 "@def|{{@tensy@char106}}\n"
1124 "@def@{{{@tensy@char102}}\n"
1125 "@def@}{{@tensy@char103}}\n"
1126 "@def<{{@teni@char62}}\n"
1127 "@def>{{@teni@char60}}\n"
1130 "@tableindent-0.2in\n"
1132 ;; Sort the array by name; within each name, by type (functions first).
1133 (setq alist
(sort alist
(lambda (e1 e2
)
1134 (if (string-equal (car e1
) (car e2
))
1135 (<= (cadr e1
) (cadr e2
))
1136 (string-lessp (car e1
) (car e2
))))))
1137 ;; Print each function.
1140 (if (char-equal (cadr e
) ?\F
) "Function" "Variable")
1141 " @code{" (car e
) "}\n@display\n"
1144 ;; Try to avoid a save size overflow in the TeX output routine.
1145 (if (zerop (setq i
(%
(1+ i
) 100)))
1146 (insert "\n@end table\n@table @asis\n")))
1147 (insert "@end table\n"
1149 (setq buffer-undo-list nil
)
1154 ;;; help-fns.el ends here