1 ;;; help-fns.el --- Complex help functions -*- lexical-binding: t -*-
3 ;; Copyright (C) 1985-1986, 1993-1994, 1998-2011
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.
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
))
76 ;; Replace `fn' with the actual function name.
77 (if (consp def
) "anonymous" def
)
78 (match-string 1 docstring
))
79 (unless (zerop (match-beginning 0))
80 (substring docstring
0 (match-beginning 0))))))
82 ;; FIXME: Move to subr.el?
83 (defun help-add-fundoc-usage (docstring arglist
)
84 "Add the usage info to DOCSTRING.
85 If DOCSTRING already has a usage info, then just return it unchanged.
86 The usage info is built from ARGLIST. DOCSTRING can be nil.
87 ARGLIST can also be t or a string of the form \"(FUN ARG1 ARG2 ...)\"."
88 (unless (stringp docstring
) (setq docstring
""))
89 (if (or (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring
)
93 (if (string-match "\n?\n\\'" docstring
)
94 (if (< (- (match-end 0) (match-beginning 0)) 2) "\n" "")
96 (if (and (stringp arglist
)
97 (string-match "\\`([^ ]+\\(.*\\))\\'" arglist
))
98 (concat "(fn" (match-string 1 arglist
) ")")
99 (format "%S" (help-make-usage 'fn arglist
))))))
101 ;; FIXME: Move to subr.el?
102 (defun help-function-arglist (def &optional preserve-names
)
103 "Return a formal argument list for the function DEF.
104 IF PRESERVE-NAMES is non-nil, return a formal arglist that uses
105 the same names as used in the original source code, when possible."
106 ;; Handle symbols aliased to other symbols.
107 (if (and (symbolp def
) (fboundp def
)) (setq def
(indirect-function def
)))
108 ;; If definition is a macro, find the function inside it.
109 (if (eq (car-safe def
) 'macro
) (setq def
(cdr def
)))
111 ((and (byte-code-function-p def
) (listp (aref def
0))) (aref def
0))
112 ((eq (car-safe def
) 'lambda
) (nth 1 def
))
113 ((eq (car-safe def
) 'closure
) (nth 2 def
))
114 ((or (and (byte-code-function-p def
) (integerp (aref def
0)))
116 (or (when preserve-names
117 (let* ((doc (condition-case nil
(documentation def
) (error nil
)))
118 (docargs (if doc
(car (help-split-fundoc doc nil
))))
120 (cdar (read-from-string (downcase docargs
)))))
123 (dolist (arg arglist
)
124 (unless (and (symbolp arg
)
125 (let ((name (symbol-name arg
)))
126 (if (eq (aref name
0) ?
&)
127 (memq arg
'(&rest
&optional
))
128 (not (string-match "\\." name
)))))
130 (when valid arglist
)))
131 (let* ((args-desc (if (not (subrp def
))
133 (let ((a (subr-arity def
)))
135 (if (numberp (cdr a
))
138 (max (lsh args-desc -
8))
139 (min (logand args-desc
127))
140 (rest (logand args-desc
128))
143 (push (intern (concat "arg" (number-to-string (1+ i
)))) arglist
))
145 (push '&optional arglist
)
146 (dotimes (i (- max min
))
147 (push (intern (concat "arg" (number-to-string (+ 1 i min
))))
149 (unless (zerop rest
) (push '&rest arglist
) (push 'rest arglist
))
150 (nreverse arglist
))))
151 ((and (eq (car-safe def
) 'autoload
) (not (eq (nth 4 def
) 'keymap
)))
152 "[Arg list not available until function definition is loaded.]")
155 ;; FIXME: Move to subr.el?
156 (defun help-make-usage (function arglist
)
157 (cons (if (symbolp function
) function
'anonymous
)
158 (mapcar (lambda (arg)
159 (if (not (symbolp arg
))
160 (if (and (consp arg
) (symbolp (car arg
)))
161 ;; CL style default values for optional args.
162 (cons (intern (upcase (symbol-name (car arg
))))
165 (let ((name (symbol-name arg
)))
167 ((string-match "\\`&" name
) arg
)
168 ((string-match "\\`_" name
)
169 (intern (upcase (substring name
1))))
170 (t (intern (upcase name
)))))))
173 ;; Could be this, if we make symbol-file do the work below.
174 ;; (defun help-C-file-name (subr-or-var kind)
175 ;; "Return the name of the C file where SUBR-OR-VAR is defined.
176 ;; KIND should be `var' for a variable or `subr' for a subroutine."
177 ;; (symbol-file (if (symbolp subr-or-var) subr-or-var
178 ;; (subr-name subr-or-var))
179 ;; (if (eq kind 'var) 'defvar 'defun)))
181 (defun help-C-file-name (subr-or-var kind
)
182 "Return the name of the C file where SUBR-OR-VAR is defined.
183 KIND should be `var' for a variable or `subr' for a subroutine."
184 (let ((docbuf (get-buffer-create " *DOC*"))
185 (name (if (eq 'var kind
)
186 (concat "V" (symbol-name subr-or-var
))
187 (concat "F" (subr-name subr-or-var
)))))
188 (with-current-buffer docbuf
189 (goto-char (point-min))
191 (insert-file-contents-literally
192 (expand-file-name internal-doc-file-name doc-directory
)))
193 (let ((file (catch 'loop
195 (let ((pnt (search-forward (concat "\x1f" name
"\n"))))
196 (re-search-backward "\x1fS\\(.*\\)")
197 (let ((file (match-string 1)))
198 (if (member file build-files
)
200 (goto-char pnt
))))))))
201 (if (string-match "^ns.*\\(\\.o\\|obj\\)\\'" file
)
202 (setq file
(replace-match ".m" t t file
1))
203 (if (string-match "\\.\\(o\\|obj\\)\\'" file
)
204 (setq file
(replace-match ".c" t t file
))))
205 (if (string-match "\\.\\(c\\|m\\)\\'" file
)
209 (defcustom help-downcase-arguments nil
210 "If non-nil, argument names in *Help* buffers are downcased."
215 (defun help-highlight-arg (arg)
216 "Highlight ARG as an argument name for a *Help* buffer.
217 Return ARG in face `help-argument-name'; ARG is also downcased
218 if the variable `help-downcase-arguments' is non-nil."
219 (propertize (if help-downcase-arguments
(downcase arg
) arg
)
220 'face
'help-argument-name
))
222 (defun help-do-arg-highlight (doc args
)
223 (with-syntax-table (make-syntax-table emacs-lisp-mode-syntax-table
)
224 (modify-syntax-entry ?\-
"w")
226 (setq doc
(replace-regexp-in-string
227 ;; This is heuristic, but covers all common cases
229 (concat "\\<" ; beginning of word
230 "\\(?:[a-z-]*-\\)?" ; for xxx-ARG
234 "\\(?:es\\|s\\|th\\)?" ; for ARGth, ARGs
235 "\\(?:-[a-z0-9-]+\\)?" ; for ARG-xxx, ARG-n
236 "\\(?:-[{([<`\"].*?\\)?"; for ARG-{x}, (x), <x>, [x], `x'
238 (help-highlight-arg arg
)
242 (defun help-highlight-arguments (usage doc
&rest args
)
243 (when (and usage
(string-match "^(" usage
))
246 (goto-char (point-min))
247 (let ((case-fold-search nil
)
248 (next (not (or args
(looking-at "\\["))))
250 ;; Make a list of all arguments
251 (skip-chars-forward "^ ")
253 (or opt
(not (looking-at " &")) (setq opt t
))
254 (if (not (re-search-forward " \\([\\[(]*\\)\\([^] &)\.]+\\)" nil t
))
256 (setq args
(cons (match-string 2) args
))
257 (when (and opt
(string= (match-string 1) "("))
258 ;; A pesky CL-style optional argument with default value,
259 ;; so let's skip over it
260 (search-backward "(")
261 (goto-char (scan-sexps (point) 1)))))
262 ;; Highlight aguments in the USAGE string
263 (setq usage
(help-do-arg-highlight (buffer-string) args
))
264 ;; Highlight arguments in the DOC string
265 (setq doc
(and doc
(help-do-arg-highlight doc args
))))))
266 ;; Return value is like the one from help-split-fundoc, but highlighted
269 ;; The following function was compiled from the former functions
270 ;; `describe-simplify-lib-file-name' and `find-source-lisp-file' with
271 ;; some excerpts from `describe-function-1' and `describe-variable'.
272 ;; The only additional twists provided are (1) locate the defining file
273 ;; for autoloaded functions, and (2) give preference to files in the
274 ;; "install directory" (directories found via `load-path') rather than
275 ;; to files in the "compile directory" (directories found by searching
276 ;; the loaddefs.el file). We autoload it because it's also used by
277 ;; `describe-face' (instead of `describe-simplify-lib-file-name').
280 (defun find-lisp-object-file-name (object type
)
281 "Guess the file that defined the Lisp object OBJECT, of type TYPE.
282 OBJECT should be a symbol associated with a function, variable, or face;
283 alternatively, it can be a function definition.
284 If TYPE is `defvar', search for a variable definition.
285 If TYPE is `defface', search for a face definition.
286 If TYPE is the value returned by `symbol-function' for a function symbol,
287 search for a function definition.
289 The return value is the absolute name of a readable file where OBJECT is
290 defined. If several such files exist, preference is given to a file
291 found via `load-path'. The return value can also be `C-source', which
292 means that OBJECT is a function or variable defined in C. If no
293 suitable file is found, return nil."
294 (let* ((autoloaded (eq (car-safe type
) 'autoload
))
295 (file-name (or (and autoloaded
(nth 1 type
))
297 object
(if (memq type
(list 'defvar
'defface
))
302 ;; An autoloaded function: Locate the file since `symbol-function'
303 ;; has only returned a bare string here.
305 (locate-file file-name load-path
'(".el" ".elc") 'readable
)))
306 ((and (stringp file-name
)
307 (string-match "[.]*loaddefs.el\\'" file-name
))
308 ;; An autoloaded variable or face. Visit loaddefs.el in a buffer
309 ;; and try to extract the defining file. The following form is
310 ;; from `describe-function-1' and `describe-variable'.
313 (find-function-search-for-symbol object nil file-name
)
316 (with-current-buffer (car location
)
317 (goto-char (cdr location
))
318 (when (re-search-backward
319 "^;;; Generated autoloads from \\(.*\\)" nil t
)
322 (file-name-sans-extension
323 (match-string-no-properties 1))
324 load-path
'(".el" ".elc") 'readable
))))))))
327 ((and (not file-name
) (subrp type
))
328 ;; A built-in function. The form is from `describe-function-1'.
329 (if (get-buffer " *DOC*")
330 (help-C-file-name type
'subr
)
332 ((and (not file-name
) (symbolp object
)
333 (integerp (get object
'variable-documentation
)))
334 ;; A variable defined in C. The form is from `describe-variable'.
335 (if (get-buffer " *DOC*")
336 (help-C-file-name object
'var
)
338 ((not (stringp file-name
))
339 ;; If we don't have a file-name string by now, we lost.
341 ;; Now, `file-name' should have become an absolute file name.
342 ;; For files loaded from ~/.emacs.elc, try ~/.emacs.
344 (and (string-equal file-name
345 (expand-file-name ".emacs.elc" "~"))
346 (file-readable-p (setq fn
(expand-file-name ".emacs" "~")))
348 ;; When the Elisp source file can be found in the install
349 ;; directory, return the name of that file.
351 (if (string-match "[.]elc\\'" file-name
)
352 (substring-no-properties file-name
0 -
1)
354 (or (and (file-readable-p lib-name
) lib-name
)
355 ;; The library might be compressed.
356 (and (file-readable-p (concat lib-name
".gz")) lib-name
))))
357 ((let* ((lib-name (file-name-nondirectory file-name
))
358 ;; The next form is from `describe-simplify-lib-file-name'.
360 ;; Try converting the absolute file name to a library
361 ;; name, convert that back to a file name and see if we
362 ;; get the original one. If so, they are equivalent.
363 (if (equal file-name
(locate-file lib-name load-path
'("")))
364 (if (string-match "[.]elc\\'" lib-name
)
365 (substring-no-properties lib-name
0 -
1)
368 ;; The next three forms are from `find-source-lisp-file'.
369 (elc-file (locate-file
371 (if (string-match "\\.el\\'" file-name
)
374 load-path nil
'readable
))
377 (insert-file-contents-literally elc-file nil
0 256)
380 (string-match ";;; from file \\(.*\\.el\\)" str
)
381 (match-string 1 str
))))
382 (and src-file
(file-readable-p src-file
) src-file
))))))
384 (declare-function ad-get-advice-info
"advice" (function))
387 (defun describe-function-1 (function)
388 (let* ((advised (and (symbolp function
) (featurep 'advice
)
389 (ad-get-advice-info function
)))
390 ;; If the function is advised, use the symbol that has the
391 ;; real definition, if that symbol is already set up.
394 (let ((origname (cdr (assq 'origname advised
))))
395 (and (fboundp origname
) origname
)))
397 ;; Get the real definition.
398 (def (if (symbolp real-function
)
399 (symbol-function real-function
)
402 (beg (if (commandp def
) "an interactive " "a "))
403 (pt1 (with-current-buffer (help-buffer) (point)))
406 (cond ((or (stringp def
) (vectorp def
))
409 (if (eq 'unevalled
(cdr (subr-arity def
)))
410 (concat beg
"special form")
411 (concat beg
"built-in function")))
412 ((byte-code-function-p def
)
413 (concat beg
"compiled Lisp function"))
415 (while (and (fboundp def
)
416 (symbolp (symbol-function def
)))
417 (setq def
(symbol-function def
)))
418 ;; Handle (defalias 'foo 'bar), where bar is undefined.
419 (or (fboundp def
) (setq errtype
'alias
))
420 (format "an alias for `%s'" def
))
421 ((eq (car-safe def
) 'lambda
)
422 (concat beg
"Lisp function"))
423 ((eq (car-safe def
) 'macro
)
425 ((eq (car-safe def
) 'closure
)
426 (concat beg
"Lisp closure"))
427 ((eq (car-safe def
) 'autoload
)
428 (format "%s autoloaded %s"
429 (if (commandp def
) "an interactive" "an")
430 (if (eq (nth 4 def
) 'keymap
) "keymap"
431 (if (nth 4 def
) "Lisp macro" "Lisp function"))))
434 (elts (cdr-safe def
)))
436 (if (char-table-p (car-safe elts
))
439 (setq elts
(cdr-safe elts
)))
445 (if (eq errtype
'alias
)
446 (princ ",\nwhich is not defined. Please make a bug report.")
447 (with-current-buffer standard-output
450 (when (re-search-backward "alias for `\\([^`']+\\)'" nil t
)
451 (help-xref-button 1 'help-function def
)))))
453 (setq file-name
(find-lisp-object-file-name function def
))
456 ;; We used to add .el to the file name,
457 ;; but that's completely wrong when the user used load-file.
458 (princ (if (eq file-name
'C-source
)
460 (file-name-nondirectory file-name
)))
462 ;; Make a hyperlink to the library.
463 (with-current-buffer standard-output
465 (re-search-backward "`\\([^`']+\\)'" nil t
)
466 (help-xref-button 1 'help-function-def function file-name
))))
468 (with-current-buffer (help-buffer)
469 (fill-region-as-paragraph (save-excursion (goto-char pt1
) (forward-line 0) (point))
472 (when (commandp function
)
473 (let ((pt2 (with-current-buffer (help-buffer) (point)))
474 (remapped (command-remapping function
)))
475 (unless (memq remapped
'(ignore undefined
))
476 (let ((keys (where-is-internal
477 (or remapped function
) overriding-local-map nil nil
))
479 (if (and (eq function
'self-insert-command
)
480 (vectorp (car-safe keys
))
481 (consp (aref (car keys
) 0)))
482 (princ "It is bound to many ordinary text characters.\n")
483 ;; Which non-control non-meta keys run this command?
485 (if (member (event-modifiers (aref key
0)) '(nil (shift)))
486 (push key non-modified-keys
)))
488 (princ "It is remapped to `")
489 (princ (symbol-name remapped
))
493 (princ (if remapped
", which is bound to " "It is bound to "))
494 ;; If lots of ordinary text characters run this command,
495 ;; don't mention them one by one.
496 (if (< (length non-modified-keys
) 10)
497 (princ (mapconcat 'key-description keys
", "))
498 (dolist (key non-modified-keys
)
499 (setq keys
(delq key keys
)))
502 (princ (mapconcat 'key-description keys
", "))
503 (princ ", and many ordinary text characters"))
504 (princ "many ordinary text characters"))))
505 (when (or remapped keys non-modified-keys
)
509 (with-current-buffer (help-buffer)
510 (fill-region-as-paragraph pt2
(point))
511 (unless (looking-back "\n\n")
513 ;; Note that list* etc do not get this property until
514 ;; cl-hack-byte-compiler runs, after bytecomp is loaded.
515 (when (and (symbolp function
)
516 (eq (get function
'byte-compile
)
517 'cl-byte-compile-compiler-macro
))
518 (princ "This function has a compiler macro")
519 (let ((lib (get function
'compiler-macro-file
)))
521 (princ (format " in `%s'" lib
))
522 (with-current-buffer standard-output
524 (re-search-backward "`\\([^`']+\\)'" nil t
)
525 (help-xref-button 1 'help-function-cmacro function lib
)))))
527 (let* ((advertised (gethash def advertised-signature-table t
))
528 (arglist (if (listp advertised
)
529 advertised
(help-function-arglist def
)))
530 (doc (condition-case err
(documentation function
)
531 (error (format "No Doc! %S" err
))))
532 (usage (help-split-fundoc doc function
)))
533 (with-current-buffer standard-output
534 ;; If definition is a keymap, skip arglist note.
535 (unless (keymapp function
)
536 (if usage
(setq doc
(cdr usage
)))
538 ((and usage
(not (listp advertised
))) (car usage
))
540 (format "%S" (help-make-usage function arglist
)))
541 ((stringp arglist
) arglist
)
542 ;; Maybe the arglist is in the docstring of a symbol
543 ;; this one is aliased to.
544 ((let ((fun real-function
))
545 (while (and (symbolp fun
)
546 (setq fun
(symbol-function fun
))
547 (not (setq usage
(help-split-fundoc
554 (format "\nMacro: %s" (format-kbd-macro def
)))
555 (t "[Missing arglist. Please make a bug report.]")))
556 (high (help-highlight-arguments use doc
)))
557 (let ((fill-begin (point)))
558 (insert (car high
) "\n")
559 (fill-region fill-begin
(point)))
560 (setq doc
(cdr high
))))
562 ;; If this is a derived mode, link to the parent.
563 (let ((parent-mode (and (symbolp real-function
)
565 'derived-mode-parent
))))
567 (with-current-buffer standard-output
568 (insert "\nParent mode: `")
570 (insert (format "%s" parent-mode
))
571 (make-text-button beg
(point)
573 'help-args
(list parent-mode
))))
576 (let* ((obsolete (and
577 ;; function might be a lambda construct.
579 (get function
'byte-obsolete-info
)))
580 (use (car obsolete
)))
582 (princ "\nThis function is obsolete")
583 (when (nth 2 obsolete
)
584 (insert (format " since %s" (nth 2 obsolete
))))
585 (insert (cond ((stringp use
) (concat ";\n" use
))
586 (use (format ";\nuse `%s' instead." use
))
590 (or doc
"Not documented."))))))))
596 (defun variable-at-point (&optional any-symbol
)
597 "Return the bound variable symbol found at or before point.
598 Return 0 if there is no such symbol.
599 If ANY-SYMBOL is non-nil, don't insist the symbol be bound."
600 (with-syntax-table emacs-lisp-mode-syntax-table
601 (or (condition-case ()
603 (skip-chars-forward "'")
604 (or (not (zerop (skip-syntax-backward "_w")))
605 (eq (char-syntax (following-char)) ?w
)
606 (eq (char-syntax (following-char)) ?_
)
608 (skip-chars-forward "'")
609 (let ((obj (read (current-buffer))))
610 (and (symbolp obj
) (boundp obj
) obj
)))
612 (let* ((str (find-tag-default))
613 (sym (if str
(intern-soft str
))))
614 (if (and sym
(or any-symbol
(boundp sym
)))
617 (when (and str
(string-match "\\`\\W*\\(.*?\\)\\W*\\'" str
))
618 (setq sym
(intern-soft (match-string 1 str
)))
619 (and (or any-symbol
(boundp sym
)) sym
)))))
622 (defun describe-variable-custom-version-info (variable)
623 (let ((custom-version (get variable
'custom-version
))
624 (cpv (get variable
'custom-package-version
))
628 (format "This variable was introduced, or its default value was changed, in\nversion %s of Emacs.\n"
631 (let* ((package (car-safe cpv
))
632 (version (if (listp (cdr-safe cpv
))
635 (pkg-versions (assq package customize-package-emacs-version-alist
))
636 (emacsv (cdr (assoc version pkg-versions
))))
637 (if (and package version
)
639 (format (concat "This variable was introduced, or its default value was changed, in\nversion %s of the %s package"
641 (format " that is part of Emacs %s" emacsv
))
643 version package
))))))
647 (defun describe-variable (variable &optional buffer frame
)
648 "Display the full documentation of VARIABLE (a symbol).
649 Returns the documentation as a string, also.
650 If VARIABLE has a buffer-local value in BUFFER or FRAME
651 \(default to the current buffer and current frame),
652 it is displayed along with the global value."
654 (let ((v (variable-at-point))
655 (enable-recursive-minibuffers t
)
657 (setq val
(completing-read (if (symbolp v
)
659 "Describe variable (default %s): " v
)
660 "Describe variable: ")
663 (or (get vv
'variable-documentation
)
664 (and (boundp vv
) (not (keywordp vv
)))))
666 (if (symbolp v
) (symbol-name v
))))
667 (list (if (equal val
"")
670 (unless (buffer-live-p buffer
) (setq buffer
(current-buffer)))
671 (unless (frame-live-p frame
) (setq frame
(selected-frame)))
672 (if (not (symbolp variable
))
673 (message "You did not specify a variable")
675 (let ((valvoid (not (with-current-buffer buffer
(boundp variable
))))
676 val val-start-pos locus
)
677 ;; Extract the value before setting up the output buffer,
678 ;; in case `buffer' *is* the output buffer.
680 (with-selected-frame frame
681 (with-current-buffer buffer
682 (setq val
(symbol-value variable
)
683 locus
(variable-binding-locus variable
)))))
684 (help-setup-xref (list #'describe-variable variable buffer
)
685 (called-interactively-p 'interactive
))
686 (with-help-window (help-buffer)
687 (with-current-buffer buffer
689 (setq file-name
(find-lisp-object-file-name variable
'defvar
))
693 (princ " is a variable defined in `")
694 (princ (if (eq file-name
'C-source
)
696 (file-name-nondirectory file-name
)))
698 (with-current-buffer standard-output
700 (re-search-backward "`\\([^`']+\\)'" nil t
)
701 (help-xref-button 1 'help-variable-def
702 variable file-name
)))
704 (princ "It is void as a variable.")
707 (princ " is void as a variable.")
710 (with-current-buffer standard-output
711 (setq val-start-pos
(point))
713 (let ((from (point)))
716 (if (< (point) (+ 68 (line-beginning-position 0)))
717 (delete-region from
(1+ from
))
718 (delete-region (1- from
) from
))
719 (let* ((sv (get variable
'standard-value
))
720 (origval (and (consp sv
)
723 (error :help-eval-error
)))))
724 (when (and (consp sv
)
725 (not (equal origval val
))
726 (not (equal origval
:help-eval-error
)))
727 (princ "\nOriginal value was \n")
730 (if (< (point) (+ from
20))
731 (delete-region (1- from
) from
)))))))
736 (princ (format "%socal in buffer %s; "
737 (if (get variable
'permanent-local
)
741 (princ (format "It is a frame-local variable; ")))
742 ((terminal-live-p locus
)
743 (princ (format "It is a terminal-local variable; ")))
745 (princ (format "It is local to %S" locus
))))
746 (if (not (default-boundp variable
))
747 (princ "globally void")
748 (let ((val (default-value variable
)))
749 (with-current-buffer standard-output
750 (princ "global value is ")
752 ;; Fixme: pp can take an age if you happen to
753 ;; ask for a very large expression. We should
754 ;; probably print it raw once and check it's a
755 ;; sensible size before prettyprinting. -- fx
756 (let ((from (point)))
758 ;; See previous comment for this function.
759 ;; (help-xref-on-pp from (point))
760 (if (< (point) (+ from
20))
761 (delete-region (1- from
) from
))))))
764 ;; If the value is large, move it to the end.
765 (with-current-buffer standard-output
766 (when (> (count-lines (point-min) (point-max)) 10)
767 ;; Note that setting the syntax table like below
768 ;; makes forward-sexp move over a `'s' at the end
770 (set-syntax-table emacs-lisp-mode-syntax-table
)
771 (goto-char val-start-pos
)
772 ;; The line below previously read as
773 ;; (delete-region (point) (progn (end-of-line) (point)))
774 ;; which suppressed display of the buffer local value for
776 (when (looking-at "value is") (replace-match ""))
778 (insert "\n\nValue:")
779 (set (make-local-variable 'help-button-cache
)
781 (insert "value is shown ")
782 (insert-button "below"
783 'action help-button-cache
785 'help-echo
"mouse-2, RET: show value")
789 (let* ((alias (condition-case nil
790 (indirect-variable variable
)
792 (obsolete (get variable
'byte-obsolete-variable
))
794 (safe-var (get variable
'safe-local-variable
))
795 (doc (or (documentation-property variable
'variable-documentation
)
796 (documentation-property alias
'variable-documentation
)))
798 ;; Add a note for variables that have been make-var-buffer-local.
799 (when (and (local-variable-if-set-p variable
)
800 (or (not (local-variable-p variable
))
802 (local-variable-if-set-p variable
))))
804 (princ " Automatically becomes buffer-local when set in any fashion.\n"))
806 ;; Mention if it's an alias
807 (unless (eq alias variable
)
809 (princ (format " This variable is an alias for `%s'.\n" alias
)))
813 (princ " This variable is obsolete")
815 (princ (format " since %s" (nth 2 obsolete
))))
816 (princ (cond ((stringp use
) (concat ";\n " use
))
817 (use (format ";\n use `%s' instead." (car obsolete
)))
821 (when (member (cons variable val
) file-local-variables-alist
)
823 (if (member (cons variable val
) dir-local-variables-alist
)
824 (let ((file (and (buffer-file-name)
825 (not (file-remote-p (buffer-file-name)))
826 (dir-locals-find-file
827 (buffer-file-name))))
829 (princ " This variable is a directory local variable")
831 (if (consp file
) ; result from cache
832 ;; If the cache element has an mtime, we
833 ;; assume it came from a file.
835 (setq file
(expand-file-name
836 dir-locals-file
(car file
)))
837 ;; Otherwise, assume it was set directly.
838 (setq type
"directory")))
839 (princ (format "\n from the %s \"%s\"" type file
)))
841 (princ " This variable is a file local variable.\n")))
843 (when (memq variable ignored-local-variables
)
845 (princ " This variable is ignored when used as a file local \
848 ;; Can be both risky and safe, eg auto-fill-function.
849 (when (risky-local-variable-p variable
)
851 (princ " This variable is potentially risky when used as a \
852 file local variable.\n")
853 (when (assq variable safe-local-variable-values
)
854 (princ " However, you have added it to \
855 `safe-local-variable-values'.\n")))
859 (princ " This variable is safe as a file local variable ")
860 (princ "if its value\n satisfies the predicate ")
861 (princ (if (byte-code-function-p safe-var
)
862 "which is byte-compiled expression.\n"
863 (format "`%s'.\n" safe-var
))))
865 (if extra-line
(terpri))
866 (princ "Documentation:\n")
867 (with-current-buffer standard-output
868 (insert (or doc
"Not documented as a variable."))))
870 ;; Make a link to customize if this variable can be customized.
871 (when (custom-variable-p variable
)
872 (let ((customize-label "customize"))
875 (princ (concat "You can " customize-label
" this variable."))
876 (with-current-buffer standard-output
879 (concat "\\(" customize-label
"\\)") nil t
)
880 (help-xref-button 1 'help-customize-variable variable
))))
881 ;; Note variable's version or package version
882 (let ((output (describe-variable-custom-version-info variable
)))
888 (with-current-buffer standard-output
889 ;; Return the text we displayed.
890 (buffer-string))))))))
894 (defun describe-syntax (&optional buffer
)
895 "Describe the syntax specifications in the syntax table of BUFFER.
896 The descriptions are inserted in a help buffer, which is then displayed.
897 BUFFER defaults to the current buffer."
899 (setq buffer
(or buffer
(current-buffer)))
900 (help-setup-xref (list #'describe-syntax buffer
)
901 (called-interactively-p 'interactive
))
902 (with-help-window (help-buffer)
903 (let ((table (with-current-buffer buffer
(syntax-table))))
904 (with-current-buffer standard-output
905 (describe-vector table
'internal-describe-syntax-value
)
906 (while (setq table
(char-table-parent table
))
907 (insert "\nThe parent syntax table is:")
908 (describe-vector table
'internal-describe-syntax-value
))))))
910 (defun help-describe-category-set (value)
912 ((null value
) "default")
913 ((char-table-p value
) "deeper char-table ...")
914 (t (condition-case nil
915 (category-set-mnemonics value
)
916 (error "invalid"))))))
919 (defun describe-categories (&optional buffer
)
920 "Describe the category specifications in the current category table.
921 The descriptions are inserted in a buffer, which is then displayed.
922 If BUFFER is non-nil, then describe BUFFER's category table instead.
923 BUFFER should be a buffer or a buffer name."
925 (setq buffer
(or buffer
(current-buffer)))
926 (help-setup-xref (list #'describe-categories buffer
)
927 (called-interactively-p 'interactive
))
928 (with-help-window (help-buffer)
929 (let* ((table (with-current-buffer buffer
(category-table)))
930 (docs (char-table-extra-slot table
0)))
931 (if (or (not (vectorp docs
)) (/= (length docs
) 95))
932 (error "Invalid first extra slot in this category table\n"))
933 (with-current-buffer standard-output
934 (insert "Legend of category mnemonics (see the tail for the longer description)\n")
935 (let ((pos (point)) (items 0) lines n
)
937 (if (aref docs i
) (setq items
(1+ items
))))
938 (setq lines
(1+ (/ (1- items
) 4)))
941 (let ((elt (aref docs i
)))
943 (string-match ".*" elt
)
944 (setq elt
(match-string 0 elt
))
945 (if (>= (length elt
) 17)
946 (setq elt
(concat (substring elt
0 14) "...")))
947 (if (< (point) (point-max))
948 (move-to-column (* 20 (/ n lines
)) t
))
949 (insert (+ i ?\s
) ?
: elt
)
950 (if (< (point) (point-max))
954 (if (= (% n lines
) 0)
956 (goto-char (point-max))
958 "character(s)\tcategory mnemonics\n"
959 "------------\t------------------")
960 (describe-vector table
'help-describe-category-set
)
961 (insert "Legend of category mnemonics:\n")
963 (let ((elt (aref docs i
)))
965 (if (string-match "\n" elt
)
966 (setq elt
(substring elt
(match-end 0))))
967 (insert (+ i ?\s
) ": " elt
"\n"))))
968 (while (setq table
(char-table-parent table
))
969 (insert "\nThe parent category table is:")
970 (describe-vector table
'help-describe-category-set
))))))
973 ;;; Replacements for old lib-src/ programs. Don't seem especially useful.
975 ;; Replaces lib-src/digest-doc.c.
977 (defun doc-file-to-man (file)
978 "Produce an nroff buffer containing the doc-strings from the DOC file."
979 (interactive (list (read-file-name "Name of DOC file: " doc-directory
980 internal-doc-file-name t
)))
981 (or (file-readable-p file
)
982 (error "Cannot read file `%s'" file
))
983 (pop-to-buffer (generate-new-buffer "*man-doc*"))
984 (setq buffer-undo-list t
)
985 (insert ".TH \"Command Summary for GNU Emacs\"\n"
986 ".AU Richard M. Stallman\n")
987 (insert-file-contents file
)
989 (while (search-forward "\x1f" nil
'move
)
991 (delete-region (1- (point)) (line-end-position))
997 (insert (if (looking-at "F") "Function " "Variable "))
1000 (insert ".DS L\n"))))
1002 (setq buffer-undo-list nil
)
1005 ;; Replaces lib-src/sorted-doc.c.
1007 (defun doc-file-to-info (file)
1008 "Produce a texinfo buffer with sorted doc-strings from the DOC file."
1009 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1010 internal-doc-file-name t
)))
1011 (or (file-readable-p file
)
1012 (error "Cannot read file `%s'" file
))
1013 (let ((i 0) type name doc alist
)
1015 (insert-file-contents file
)
1016 ;; The characters "@{}" need special treatment.
1017 (while (re-search-forward "[@{}]" nil t
)
1021 (goto-char (point-min))
1022 (while (search-forward "\x1f" nil t
)
1023 (unless (looking-at "S")
1024 (setq type
(char-after)
1025 name
(buffer-substring (1+ (point)) (line-end-position))
1026 doc
(buffer-substring (line-beginning-position 2)
1027 (if (search-forward "\x1f" nil
'move
)
1030 alist
(cons (list name type doc
) alist
))
1031 (backward-char 1))))
1032 (pop-to-buffer (generate-new-buffer "*info-doc*"))
1033 (setq buffer-undo-list t
)
1034 ;; Write the output header.
1035 (insert "\\input texinfo @c -*-texinfo-*-\n"
1036 "@setfilename emacsdoc.info\n"
1037 "@settitle Command Summary for GNU Emacs\n"
1040 "@unnumbered Command Summary for GNU Emacs\n\n"
1043 "@global@let@ITEM@item\n"
1044 "@def@item{@filbreak@vskip5pt@ITEM}\n"
1045 "@font@tensy cmsy10 scaled @magstephalf\n"
1046 "@font@teni cmmi10 scaled @magstephalf\n"
1047 "@def\\{{@tensy@char110}}\n" ; this backslash goes with cmr10
1048 "@def|{{@tensy@char106}}\n"
1049 "@def@{{{@tensy@char102}}\n"
1050 "@def@}{{@tensy@char103}}\n"
1051 "@def<{{@teni@char62}}\n"
1052 "@def>{{@teni@char60}}\n"
1055 "@tableindent-0.2in\n"
1057 ;; Sort the array by name; within each name, by type (functions first).
1058 (setq alist
(sort alist
(lambda (e1 e2
)
1059 (if (string-equal (car e1
) (car e2
))
1060 (<= (cadr e1
) (cadr e2
))
1061 (string-lessp (car e1
) (car e2
))))))
1062 ;; Print each function.
1065 (if (char-equal (cadr e
) ?\F
) "Function" "Variable")
1066 " @code{" (car e
) "}\n@display\n"
1069 ;; Try to avoid a save size overflow in the TeX output routine.
1070 (if (zerop (setq i
(%
(1+ i
) 100)))
1071 (insert "\n@end table\n@table @asis\n")))
1072 (insert "@end table\n"
1074 (setq buffer-undo-list nil
)
1079 ;;; help-fns.el ends here