1 ;;; help-fns.el --- Complex help functions -*- lexical-binding: t -*-
3 ;; Copyright (C) 1985-1986, 1993-1994, 1998-2015 Free Software
6 ;; Maintainer: emacs-devel@gnu.org
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 (defvar help-fns-describe-function-functions nil
39 "List of functions to run in help buffer in `describe-function'.
40 Those functions will be run after the header line and argument
41 list was inserted, and before the documentation will be inserted.
42 The functions will receive the function name as argument.")
47 (defun describe-function (function)
48 "Display the full documentation of FUNCTION (a symbol)."
50 (let ((fn (function-called-at-point))
51 (enable-recursive-minibuffers t
)
53 (setq val
(completing-read (if fn
54 (format "Describe function (default %s): " fn
)
55 "Describe function: ")
56 obarray
'fboundp t nil nil
57 (and fn
(symbol-name fn
))))
58 (list (if (equal val
"")
60 (or (and function
(symbolp function
))
61 (user-error "You didn't specify a function symbol"))
62 (or (fboundp function
)
63 (user-error "Symbol's function definition is void: %s" function
))
64 (help-setup-xref (list #'describe-function function
)
65 (called-interactively-p 'interactive
))
67 (with-help-window (help-buffer)
69 ;; Use " is " instead of a colon so that
70 ;; it is easier to get out the function name using forward-sexp.
72 (describe-function-1 function
)
73 (with-current-buffer standard-output
74 ;; Return the text we displayed.
78 ;; Could be this, if we make symbol-file do the work below.
79 ;; (defun help-C-file-name (subr-or-var kind)
80 ;; "Return the name of the C file where SUBR-OR-VAR is defined.
81 ;; KIND should be `var' for a variable or `subr' for a subroutine."
82 ;; (symbol-file (if (symbolp subr-or-var) subr-or-var
83 ;; (subr-name subr-or-var))
84 ;; (if (eq kind 'var) 'defvar 'defun)))
86 (defun help-C-file-name (subr-or-var kind
)
87 "Return the name of the C file where SUBR-OR-VAR is defined.
88 KIND should be `var' for a variable or `subr' for a subroutine."
89 (let ((docbuf (get-buffer-create " *DOC*"))
90 (name (if (eq 'var kind
)
91 (concat "V" (symbol-name subr-or-var
))
92 (concat "F" (subr-name (advice--cd*r subr-or-var
))))))
93 (with-current-buffer docbuf
94 (goto-char (point-min))
96 (insert-file-contents-literally
97 (expand-file-name internal-doc-file-name doc-directory
)))
98 (let ((file (catch 'loop
100 (let ((pnt (search-forward (concat "\x1f" name
"\n"))))
101 (re-search-backward "\x1fS\\(.*\\)")
102 (let ((file (match-string 1)))
103 (if (member file build-files
)
105 (goto-char pnt
))))))))
106 (if (string-match "^ns.*\\(\\.o\\|obj\\)\\'" file
)
107 (setq file
(replace-match ".m" t t file
1))
108 (if (string-match "\\.\\(o\\|obj\\)\\'" file
)
109 (setq file
(replace-match ".c" t t file
))))
110 (if (string-match "\\.\\(c\\|m\\)\\'" file
)
114 (defcustom help-downcase-arguments nil
115 "If non-nil, argument names in *Help* buffers are downcased."
120 (defun help-highlight-arg (arg)
121 "Highlight ARG as an argument name for a *Help* buffer.
122 Return ARG in face `help-argument-name'; ARG is also downcased
123 if the variable `help-downcase-arguments' is non-nil."
124 (propertize (if help-downcase-arguments
(downcase arg
) arg
)
125 'face
'help-argument-name
))
127 (defun help-do-arg-highlight (doc args
)
128 (with-syntax-table (make-syntax-table emacs-lisp-mode-syntax-table
)
129 (modify-syntax-entry ?\-
"w")
131 (setq doc
(replace-regexp-in-string
132 ;; This is heuristic, but covers all common cases
134 (concat "\\<" ; beginning of word
135 "\\(?:[a-z-]*-\\)?" ; for xxx-ARG
139 "\\(?:es\\|s\\|th\\)?" ; for ARGth, ARGs
140 "\\(?:-[a-z0-9-]+\\)?" ; for ARG-xxx, ARG-n
141 "\\(?:-[{([<`\"‘].*?\\)?"; for ARG-{x}, (x), <x>, [x], `x', ‘x’
143 (help-highlight-arg arg
)
147 (defun help-highlight-arguments (usage doc
&rest args
)
148 (when (and usage
(string-match "^(" usage
))
151 (goto-char (point-min))
152 (let ((case-fold-search nil
)
153 (next (not (or args
(looking-at "\\["))))
155 ;; Make a list of all arguments
156 (skip-chars-forward "^ ")
158 (or opt
(not (looking-at " &")) (setq opt t
))
159 (if (not (re-search-forward " \\([\\[(]*\\)\\([^] &)\.]+\\)" nil t
))
161 (setq args
(cons (match-string 2) args
))
162 (when (and opt
(string= (match-string 1) "("))
163 ;; A pesky CL-style optional argument with default value,
164 ;; so let's skip over it
165 (search-backward "(")
166 (goto-char (scan-sexps (point) 1)))))
167 ;; Highlight arguments in the USAGE string
168 (setq usage
(help-do-arg-highlight (buffer-string) args
))
169 ;; Highlight arguments in the DOC string
170 (setq doc
(and doc
(help-do-arg-highlight doc args
))))))
171 ;; Return value is like the one from help-split-fundoc, but highlighted
174 ;; The following function was compiled from the former functions
175 ;; `describe-simplify-lib-file-name' and `find-source-lisp-file' with
176 ;; some excerpts from `describe-function-1' and `describe-variable'.
177 ;; The only additional twists provided are (1) locate the defining file
178 ;; for autoloaded functions, and (2) give preference to files in the
179 ;; "install directory" (directories found via `load-path') rather than
180 ;; to files in the "compile directory" (directories found by searching
181 ;; the loaddefs.el file). We autoload it because it's also used by
182 ;; `describe-face' (instead of `describe-simplify-lib-file-name').
185 (defun find-lisp-object-file-name (object type
)
186 "Guess the file that defined the Lisp object OBJECT, of type TYPE.
187 OBJECT should be a symbol associated with a function, variable, or face;
188 alternatively, it can be a function definition.
189 If TYPE is `defvar', search for a variable definition.
190 If TYPE is `defface', search for a face definition.
191 If TYPE is not a symbol, search for a function definition.
193 The return value is the absolute name of a readable file where OBJECT is
194 defined. If several such files exist, preference is given to a file
195 found via `load-path'. The return value can also be `C-source', which
196 means that OBJECT is a function or variable defined in C. If no
197 suitable file is found, return nil."
198 (let* ((autoloaded (autoloadp type
))
199 (file-name (or (and autoloaded
(nth 1 type
))
201 ;; FIXME: Why do we have this weird "If TYPE is the
202 ;; value returned by `symbol-function' for a function
203 ;; symbol" exception?
204 object
(or (if (symbolp type
) type
) 'defun
)))))
207 ;; An autoloaded function: Locate the file since `symbol-function'
208 ;; has only returned a bare string here.
210 (locate-file file-name load-path
'(".el" ".elc") 'readable
)))
211 ((and (stringp file-name
)
212 (string-match "[.]*loaddefs.el\\'" file-name
))
213 ;; An autoloaded variable or face. Visit loaddefs.el in a buffer
214 ;; and try to extract the defining file. The following form is
215 ;; from `describe-function-1' and `describe-variable'.
218 (find-function-search-for-symbol object nil file-name
)
221 (with-current-buffer (car location
)
222 (goto-char (cdr location
))
223 (when (re-search-backward
224 "^;;; Generated autoloads from \\(.*\\)" nil t
)
227 (file-name-sans-extension
228 (match-string-no-properties 1))
229 load-path
'(".el" ".elc") 'readable
))))))))
232 ((and (not file-name
) (subrp type
))
233 ;; A built-in function. The form is from `describe-function-1'.
234 (if (get-buffer " *DOC*")
235 (help-C-file-name type
'subr
)
237 ((and (not file-name
) (symbolp object
)
238 (integerp (get object
'variable-documentation
)))
239 ;; A variable defined in C. The form is from `describe-variable'.
240 (if (get-buffer " *DOC*")
241 (help-C-file-name object
'var
)
243 ((not (stringp file-name
))
244 ;; If we don't have a file-name string by now, we lost.
246 ;; Now, `file-name' should have become an absolute file name.
247 ;; For files loaded from ~/.foo.elc, try ~/.foo.
248 ;; This applies to config files like ~/.emacs,
249 ;; which people sometimes compile.
251 (and (string-match "\\`\\..*\\.elc\\'"
252 (file-name-nondirectory file-name
))
253 (string-equal (file-name-directory file-name
)
254 (file-name-as-directory (expand-file-name "~")))
255 (file-readable-p (setq fn
(file-name-sans-extension file-name
)))
257 ;; When the Elisp source file can be found in the install
258 ;; directory, return the name of that file.
260 (if (string-match "[.]elc\\'" file-name
)
261 (substring-no-properties file-name
0 -
1)
263 (or (and (file-readable-p lib-name
) lib-name
)
264 ;; The library might be compressed.
265 (and (file-readable-p (concat lib-name
".gz")) lib-name
))))
266 ((let* ((lib-name (file-name-nondirectory file-name
))
267 ;; The next form is from `describe-simplify-lib-file-name'.
269 ;; Try converting the absolute file name to a library
270 ;; name, convert that back to a file name and see if we
271 ;; get the original one. If so, they are equivalent.
272 (if (equal file-name
(locate-file lib-name load-path
'("")))
273 (if (string-match "[.]elc\\'" lib-name
)
274 (substring-no-properties lib-name
0 -
1)
277 ;; The next three forms are from `find-source-lisp-file'.
278 (elc-file (locate-file
280 (if (string-match "\\.el\\'" file-name
)
283 load-path nil
'readable
))
286 (insert-file-contents-literally elc-file nil
0 256)
289 (string-match ";;; from file \\(.*\\.el\\)" str
)
290 (match-string 1 str
))))
291 (and src-file
(file-readable-p src-file
) src-file
))))))
293 (defun help-fns--key-bindings (function)
294 (when (commandp function
)
295 (let ((pt2 (with-current-buffer standard-output
(point)))
296 (remapped (command-remapping function
)))
297 (unless (memq remapped
'(ignore undefined
))
298 (let ((keys (where-is-internal
299 (or remapped function
) overriding-local-map nil nil
))
301 (if (and (eq function
'self-insert-command
)
302 (vectorp (car-safe keys
))
303 (consp (aref (car keys
) 0)))
304 (princ "It is bound to many ordinary text characters.\n")
305 ;; Which non-control non-meta keys run this command?
307 (if (member (event-modifiers (aref key
0)) '(nil (shift)))
308 (push key non-modified-keys
)))
310 (princ "Its keys are remapped to ")
311 (princ (if (symbolp remapped
)
312 (concat (substitute-command-keys "‘")
313 (symbol-name remapped
)
314 (substitute-command-keys "’"))
315 "an anonymous command"))
320 "Without this remapping, it would be bound to "
322 ;; If lots of ordinary text characters run this command,
323 ;; don't mention them one by one.
324 (if (< (length non-modified-keys
) 10)
325 (princ (mapconcat 'key-description keys
", "))
326 (dolist (key non-modified-keys
)
327 (setq keys
(delq key keys
)))
330 (princ (mapconcat 'key-description keys
", "))
331 (princ ", and many ordinary text characters"))
332 (princ "many ordinary text characters"))))
333 (when (or remapped keys non-modified-keys
)
337 (with-current-buffer standard-output
338 (fill-region-as-paragraph pt2
(point))
339 (unless (looking-back "\n\n" (- (point) 2))
342 (defun help-fns--compiler-macro (function)
343 (let ((handler (function-get function
'compiler-macro
)))
345 (insert "\nThis function has a compiler macro")
346 (if (symbolp handler
)
348 (insert (format (substitute-command-keys " ‘%s’") handler
))
350 (re-search-backward (substitute-command-keys "‘\\([^‘’]+\\)’")
352 (help-xref-button 1 'help-function handler
)))
353 ;; FIXME: Obsolete since 24.4.
354 (let ((lib (get function
'compiler-macro-file
)))
356 (insert (format (substitute-command-keys " in ‘%s’") lib
))
358 (re-search-backward (substitute-command-keys "‘\\([^‘’]+\\)’")
360 (help-xref-button 1 'help-function-cmacro function lib
)))))
363 (defun help-fns--signature (function doc real-def real-function raw
)
364 "Insert usage at point and return docstring. With highlighting."
365 (if (keymapp function
)
366 doc
; If definition is a keymap, skip arglist note.
367 (let* ((advertised (gethash real-def advertised-signature-table t
))
368 (arglist (if (listp advertised
)
369 advertised
(help-function-arglist real-def
)))
370 (usage (help-split-fundoc doc function
)))
371 (if usage
(setq doc
(cdr usage
)))
373 ((and usage
(not (listp advertised
))) (car usage
))
375 (help--make-usage-docstring function arglist
))
376 ((stringp arglist
) arglist
)
377 ;; Maybe the arglist is in the docstring of a symbol
378 ;; this one is aliased to.
379 ((let ((fun real-function
))
380 (while (and (symbolp fun
)
381 (setq fun
(symbol-function fun
))
382 (not (setq usage
(help-split-fundoc
387 ((or (stringp real-def
)
389 (format "\nMacro: %s"
390 (help--docstring-quote
391 (format-kbd-macro real-def
))))
392 (t "[Missing arglist. Please make a bug report.]")))
393 ;; Insert "`X", not "(\` X)", when documenting `X.
394 (use1 (replace-regexp-in-string
395 "\\`(\\\\=\\\\\\\\=` \\([^\n ]*\\))\\'"
399 (help-highlight-arguments (substitute-command-keys use1
)
400 (substitute-command-keys doc
)))))
401 (let ((fill-begin (point))
402 (high-usage (car high
))
403 (high-doc (cdr high
)))
404 (insert high-usage
"\n")
405 (fill-region fill-begin
(point))
408 (defun help-fns--parent-mode (function)
409 ;; If this is a derived mode, link to the parent.
410 (let ((parent-mode (and (symbolp function
)
412 'derived-mode-parent
))))
414 (insert (substitute-command-keys "\nParent mode: ‘"))
416 (insert (format "%s" parent-mode
))
417 (make-text-button beg
(point)
419 'help-args
(list parent-mode
)))
420 (insert (substitute-command-keys "’.\n")))))
422 (defun help-fns--obsolete (function)
423 ;; Ignore lambda constructs, keyboard macros, etc.
424 (let* ((obsolete (and (symbolp function
)
425 (get function
'byte-obsolete-info
)))
426 (use (car obsolete
)))
429 (if (eq (car-safe (symbol-function function
)) 'macro
)
433 (when (nth 2 obsolete
)
434 (insert (format " since %s" (nth 2 obsolete
))))
435 (insert (cond ((stringp use
) (concat ";\n" use
))
436 (use (format (substitute-command-keys
437 ";\nuse ‘%s’ instead.")
442 ;; We could use `symbol-file' but this is a wee bit more efficient.
443 (defun help-fns--autoloaded-p (function file
)
444 "Return non-nil if FUNCTION has previously been autoloaded.
445 FILE is the file where FUNCTION was probably defined."
446 (let* ((file (file-name-sans-extension (file-truename file
)))
447 (load-hist load-history
)
448 (target (cons t function
))
450 (while (and load-hist
(not found
))
451 (and (caar load-hist
)
452 (equal (file-name-sans-extension (caar load-hist
)) file
)
453 (setq found
(member target
(cdar load-hist
))))
454 (setq load-hist
(cdr load-hist
)))
457 (defun help-fns--interactive-only (function)
458 "Insert some help blurb if FUNCTION should only be used interactively."
459 ;; Ignore lambda constructs, keyboard macros, etc.
460 (and (symbolp function
)
461 (not (eq (car-safe (symbol-function function
)) 'macro
))
462 (let* ((interactive-only
463 (or (get function
'interactive-only
)
464 (if (boundp 'byte-compile-interactive-only-functions
)
466 byte-compile-interactive-only-functions
)))))
467 (when interactive-only
468 (insert "\nThis function is for interactive use only"
469 ;; Cf byte-compile-form.
470 (cond ((stringp interactive-only
)
471 (format ";\nin Lisp code %s" interactive-only
))
472 ((and (symbolp 'interactive-only
)
473 (not (eq interactive-only t
)))
474 (format (substitute-command-keys
475 ";\nin Lisp code use ‘%s’ instead.")
480 (defun help-fns-short-filename (filename)
481 (let* ((abbrev (abbreviate-file-name filename
))
483 (dolist (dir load-path
)
484 (let ((rel (file-relative-name filename dir
)))
485 (if (< (length rel
) (length short
))
487 (let ((rel (file-relative-name abbrev dir
)))
488 (if (< (length rel
) (length short
))
493 (defun describe-function-1 (function)
494 (let* ((advised (and (symbolp function
)
496 (advice--p (advice--symbol-function function
))))
497 ;; If the function is advised, use the symbol that has the
498 ;; real definition, if that symbol is already set up.
501 (advice--cd*r
(advice--symbol-function function
)))
503 ;; Get the real definition.
504 (def (if (symbolp real-function
)
505 (or (symbol-function real-function
)
506 (signal 'void-function
(list real-function
)))
508 (aliased (or (symbolp def
)
509 ;; Advised & aliased function.
510 (and advised
(symbolp real-function
))))
512 (aliased (let ((f real-function
))
513 (while (and (fboundp f
)
514 (symbolp (symbol-function f
)))
515 (setq f
(symbol-function f
)))
517 ((subrp def
) (intern (subr-name def
)))
519 (sig-key (if (subrp def
)
520 (indirect-function real-def
)
522 (file-name (find-lisp-object-file-name function def
))
523 (pt1 (with-current-buffer (help-buffer) (point)))
524 (beg (if (and (or (byte-code-function-p def
)
526 (memq (car-safe def
) '(macro lambda closure
)))
528 (help-fns--autoloaded-p function file-name
))
530 "an interactive autoloaded "
532 (if (commandp def
) "an interactive " "a "))))
534 ;; Print what kind of function-like object FUNCTION is.
535 (princ (cond ((or (stringp def
) (vectorp def
))
538 (if (eq 'unevalled
(cdr (subr-arity def
)))
539 (concat beg
"special form")
540 (concat beg
"built-in function")))
541 ;; Aliases are Lisp functions, so we need to check
542 ;; aliases before functions.
544 (format (substitute-command-keys "an alias for ‘%s’")
547 (format "%s autoloaded %s"
548 (if (commandp def
) "an interactive" "an")
549 (if (eq (nth 4 def
) 'keymap
) "keymap"
550 (if (nth 4 def
) "Lisp macro" "Lisp function"))))
551 ((or (eq (car-safe def
) 'macro
)
552 ;; For advised macros, def is a lambda
553 ;; expression or a byte-code-function-p, so we
554 ;; need to check macros before functions.
556 (concat beg
"Lisp macro"))
557 ((byte-code-function-p def
)
558 (concat beg
"compiled Lisp function"))
559 ((eq (car-safe def
) 'lambda
)
560 (concat beg
"Lisp function"))
561 ((eq (car-safe def
) 'closure
)
562 (concat beg
"Lisp closure"))
565 (elts (cdr-safe def
)))
567 (if (char-table-p (car-safe elts
))
570 (setq elts
(cdr-safe elts
)))
571 (concat beg
(if is-full
"keymap" "sparse keymap"))))
574 (if (and aliased
(not (fboundp real-def
)))
575 (princ ",\nwhich is not defined. Please make a bug report.")
576 (with-current-buffer standard-output
579 (when (re-search-backward (substitute-command-keys
580 "alias for ‘\\([^‘’]+\\)’")
582 (help-xref-button 1 'help-function real-def
)))))
585 (princ (substitute-command-keys " in ‘"))
586 ;; We used to add .el to the file name,
587 ;; but that's completely wrong when the user used load-file.
588 (princ (if (eq file-name
'C-source
)
590 (help-fns-short-filename file-name
)))
591 (princ (substitute-command-keys "’"))
592 ;; Make a hyperlink to the library.
593 (with-current-buffer standard-output
595 (re-search-backward (substitute-command-keys "‘\\([^‘’]+\\)’")
597 (help-xref-button 1 'help-function-def function file-name
))))
599 (with-current-buffer (help-buffer)
600 (fill-region-as-paragraph (save-excursion (goto-char pt1
) (forward-line 0) (point))
604 (let ((doc-raw (documentation function t
)))
606 ;; If the function is autoloaded, and its docstring has
607 ;; key substitution constructs, load the library.
608 (and (autoloadp real-def
) doc-raw
609 help-enable-auto-load
610 (string-match "\\([^\\]=\\|[^=]\\|\\`\\)\\\\[[{<]" doc-raw
)
611 (autoload-do-load real-def
))
613 (help-fns--key-bindings function
)
614 (with-current-buffer standard-output
615 (let ((doc (help-fns--signature function doc-raw sig-key
617 (run-hook-with-args 'help-fns-describe-function-functions function
)
619 (or doc
"Not documented."))))))))
621 ;; Add defaults to `help-fns-describe-function-functions'.
622 (add-hook 'help-fns-describe-function-functions
#'help-fns--obsolete
)
623 (add-hook 'help-fns-describe-function-functions
#'help-fns--interactive-only
)
624 (add-hook 'help-fns-describe-function-functions
#'help-fns--parent-mode
)
625 (add-hook 'help-fns-describe-function-functions
#'help-fns--compiler-macro
)
631 (defun variable-at-point (&optional any-symbol
)
632 "Return the bound variable symbol found at or before point.
633 Return 0 if there is no such symbol.
634 If ANY-SYMBOL is non-nil, don't insist the symbol be bound."
635 (with-syntax-table emacs-lisp-mode-syntax-table
636 (or (condition-case ()
638 (skip-chars-forward "'")
639 (or (not (zerop (skip-syntax-backward "_w")))
640 (eq (char-syntax (following-char)) ?w
)
641 (eq (char-syntax (following-char)) ?_
)
643 (skip-chars-forward "'")
644 (let ((obj (read (current-buffer))))
645 (and (symbolp obj
) (boundp obj
) obj
)))
647 (let* ((str (find-tag-default))
648 (sym (if str
(intern-soft str
))))
649 (if (and sym
(or any-symbol
(boundp sym
)))
652 (when (and str
(string-match "\\`\\W*\\(.*?\\)\\W*\\'" str
))
653 (setq sym
(intern-soft (match-string 1 str
)))
654 (and (or any-symbol
(boundp sym
)) sym
)))))
657 (defun describe-variable-custom-version-info (variable)
658 (let ((custom-version (get variable
'custom-version
))
659 (cpv (get variable
'custom-package-version
))
663 (format "This variable was introduced, or its default value was changed, in\nversion %s of Emacs.\n"
666 (let* ((package (car-safe cpv
))
667 (version (if (listp (cdr-safe cpv
))
670 (pkg-versions (assq package customize-package-emacs-version-alist
))
671 (emacsv (cdr (assoc version pkg-versions
))))
672 (if (and package version
)
674 (format (concat "This variable was introduced, or its default value was changed, in\nversion %s of the %s package"
676 (format " that is part of Emacs %s" emacsv
))
678 version package
))))))
682 (defun describe-variable (variable &optional buffer frame
)
683 "Display the full documentation of VARIABLE (a symbol).
684 Returns the documentation as a string, also.
685 If VARIABLE has a buffer-local value in BUFFER or FRAME
686 \(default to the current buffer and current frame),
687 it is displayed along with the global value."
689 (let ((v (variable-at-point))
690 (enable-recursive-minibuffers t
)
692 (setq val
(completing-read (if (symbolp v
)
694 "Describe variable (default %s): " v
)
695 "Describe variable: ")
698 (or (get vv
'variable-documentation
)
699 (and (boundp vv
) (not (keywordp vv
)))))
701 (if (symbolp v
) (symbol-name v
))))
702 (list (if (equal val
"")
705 (unless (buffer-live-p buffer
) (setq buffer
(current-buffer)))
706 (unless (frame-live-p frame
) (setq frame
(selected-frame)))
707 (if (not (symbolp variable
))
708 (message "You did not specify a variable")
710 (let ((valvoid (not (with-current-buffer buffer
(boundp variable
))))
711 (permanent-local (get variable
'permanent-local
))
712 val val-start-pos locus
)
713 ;; Extract the value before setting up the output buffer,
714 ;; in case `buffer' *is* the output buffer.
716 (with-selected-frame frame
717 (with-current-buffer buffer
718 (setq val
(symbol-value variable
)
719 locus
(variable-binding-locus variable
)))))
720 (help-setup-xref (list #'describe-variable variable buffer
)
721 (called-interactively-p 'interactive
))
722 (with-help-window (help-buffer)
723 (with-current-buffer buffer
725 (setq file-name
(find-lisp-object-file-name variable
'defvar
))
729 (princ (substitute-command-keys
730 " is a variable defined in ‘"))
731 (princ (if (eq file-name
'C-source
)
733 (file-name-nondirectory file-name
)))
734 (princ (substitute-command-keys "’.\n"))
735 (with-current-buffer standard-output
737 (re-search-backward (substitute-command-keys
740 (help-xref-button 1 'help-variable-def
741 variable file-name
)))
743 (princ "It is void as a variable.")
746 (princ " is void as a variable.")
749 (with-current-buffer standard-output
750 (setq val-start-pos
(point))
753 (line-beg (line-beginning-position))
755 (let ((print-quoted t
))
756 (prin1-to-string val
))))
757 (if (< (+ (length print-rep
) (point) (- line-beg
)) 68)
761 (if (< (point) (+ 68 (line-beginning-position 0)))
762 (delete-region from
(1+ from
))
763 (delete-region (1- from
) from
)))
764 (let* ((sv (get variable
'standard-value
))
765 (origval (and (consp sv
)
768 (error :help-eval-error
)))))
769 (when (and (consp sv
)
770 (not (equal origval val
))
771 (not (equal origval
:help-eval-error
)))
772 (princ "\nOriginal value was \n")
775 (if (< (point) (+ from
20))
776 (delete-region (1- from
) from
)))))))
781 (princ (format "Local in buffer %s; "
782 (buffer-name buffer
))))
784 (princ (format "It is a frame-local variable; ")))
785 ((terminal-live-p locus
)
786 (princ (format "It is a terminal-local variable; ")))
788 (princ (format "It is local to %S" locus
))))
789 (if (not (default-boundp variable
))
790 (princ "globally void")
791 (let ((global-val (default-value variable
)))
792 (with-current-buffer standard-output
793 (princ "global value is ")
794 (if (eq val global-val
)
797 ;; Fixme: pp can take an age if you happen to
798 ;; ask for a very large expression. We should
799 ;; probably print it raw once and check it's a
800 ;; sensible size before prettyprinting. -- fx
801 (let ((from (point)))
803 ;; See previous comment for this function.
804 ;; (help-xref-on-pp from (point))
805 (if (< (point) (+ from
20))
806 (delete-region (1- from
) from
)))))))
809 ;; If the value is large, move it to the end.
810 (with-current-buffer standard-output
811 (when (> (count-lines (point-min) (point-max)) 10)
812 ;; Note that setting the syntax table like below
813 ;; makes forward-sexp move over a `'s' at the end
815 (set-syntax-table emacs-lisp-mode-syntax-table
)
816 (goto-char val-start-pos
)
817 ;; The line below previously read as
818 ;; (delete-region (point) (progn (end-of-line) (point)))
819 ;; which suppressed display of the buffer local value for
821 (when (looking-at "value is") (replace-match ""))
823 (insert "\n\nValue:")
824 (set (make-local-variable 'help-button-cache
)
826 (insert "value is shown ")
827 (insert-button "below"
828 'action help-button-cache
830 'help-echo
"mouse-2, RET: show value")
834 (let* ((alias (condition-case nil
835 (indirect-variable variable
)
837 (obsolete (get variable
'byte-obsolete-variable
))
839 (safe-var (get variable
'safe-local-variable
))
840 (doc (or (documentation-property
841 variable
'variable-documentation
)
842 (documentation-property
843 alias
'variable-documentation
)))
846 ;; Mention if it's a local variable.
848 ((and (local-variable-if-set-p variable
)
849 (or (not (local-variable-p variable
))
851 (local-variable-if-set-p variable
))))
853 (princ " Automatically becomes ")
855 (princ "permanently "))
856 (princ "buffer-local when set.\n"))
857 ((not permanent-local
))
860 (princ " This variable's buffer-local value is permanent.\n"))
863 (princ " This variable's value is permanent \
864 if it is given a local binding.\n")))
866 ;; Mention if it's an alias.
867 (unless (eq alias variable
)
869 (princ (format (substitute-command-keys
870 " This variable is an alias for ‘%s’.\n")
875 (princ " This variable is obsolete")
877 (princ (format " since %s" (nth 2 obsolete
))))
878 (princ (cond ((stringp use
) (concat ";\n " use
))
879 (use (format (substitute-command-keys
880 ";\n use ‘%s’ instead.")
885 (when (member (cons variable val
)
886 (with-current-buffer buffer
887 file-local-variables-alist
))
889 (if (member (cons variable val
)
890 (with-current-buffer buffer
891 dir-local-variables-alist
))
892 (let ((file (and (buffer-file-name buffer
)
894 (buffer-file-name buffer
)))
895 (dir-locals-find-file
896 (buffer-file-name buffer
))))
898 (princ " This variable's value is directory-local")
902 (if (consp file
) ; result from cache
903 ;; If the cache element has an mtime, we
904 ;; assume it came from a file.
906 (setq file
(expand-file-name
907 dir-locals-file
(car file
)))
908 ;; Otherwise, assume it was set directly.
909 (setq file
(car file
)
911 (princ (substitute-command-keys
914 "for the directory\n ‘")))
915 (with-current-buffer standard-output
917 file
'type
'help-dir-local-var-def
918 'help-args
(list variable file
)))
919 (princ (substitute-command-keys "’.\n"))))
920 (princ " This variable's value is file-local.\n")))
922 (when (memq variable ignored-local-variables
)
924 (princ " This variable is ignored as a file-local \
927 ;; Can be both risky and safe, eg auto-fill-function.
928 (when (risky-local-variable-p variable
)
930 (princ " This variable may be risky if used as a \
931 file-local variable.\n")
932 (when (assq variable safe-local-variable-values
)
933 (princ (substitute-command-keys
934 " However, you have added it to \
935 ‘safe-local-variable-values’.\n"))))
939 (princ " This variable is safe as a file local variable ")
940 (princ "if its value\n satisfies the predicate ")
941 (princ (if (byte-code-function-p safe-var
)
942 "which is a byte-compiled expression.\n"
943 (format (substitute-command-keys "‘%s’.\n")
946 (if extra-line
(terpri))
947 (princ "Documentation:\n")
948 (with-current-buffer standard-output
949 (insert (or doc
"Not documented as a variable."))))
951 ;; Make a link to customize if this variable can be customized.
952 (when (custom-variable-p variable
)
953 (let ((customize-label "customize"))
956 (princ (concat "You can " customize-label
" this variable."))
957 (with-current-buffer standard-output
960 (concat "\\(" customize-label
"\\)") nil t
)
961 (help-xref-button 1 'help-customize-variable variable
))))
962 ;; Note variable's version or package version
963 (let ((output (describe-variable-custom-version-info variable
)))
969 (with-current-buffer standard-output
970 ;; Return the text we displayed.
971 (buffer-string))))))))
974 (defvar help-xref-stack-item
)
977 (defun describe-symbol (symbol &optional buffer frame
)
978 "Display the full documentation of SYMBOL.
979 Will show the info of SYMBOL as a function, variable, and/or face."
981 (let* ((v-or-f (symbol-at-point))
982 (found (cl-some (lambda (x) (funcall (nth 1 x
) v-or-f
))
983 describe-symbol-backends
))
984 (v-or-f (if found v-or-f
(function-called-at-point)))
985 (found (or found v-or-f
))
986 (enable-recursive-minibuffers t
)
987 (val (completing-read (if found
989 "Describe symbol (default %s): " v-or-f
)
993 (cl-some (lambda (x) (funcall (nth 1 x
) vv
))
994 describe-symbol-backends
))
996 (if found
(symbol-name v-or-f
)))))
997 (list (if (equal val
"")
998 v-or-f
(intern val
)))))
999 (if (not (symbolp symbol
))
1000 (user-error "You didn't specify a function or variable"))
1001 (unless (buffer-live-p buffer
) (setq buffer
(current-buffer)))
1002 (unless (frame-live-p frame
) (setq frame
(selected-frame)))
1003 (with-current-buffer (help-buffer)
1004 ;; Push the previous item on the stack before clobbering the output buffer.
1005 (help-setup-xref nil nil
)
1009 (mapcar (pcase-lambda (`(,name
,testfn
,descfn
))
1010 (when (funcall testfn symbol
)
1011 ;; Don't record the current entry in the stack.
1012 (setq help-xref-stack-item nil
)
1014 (funcall descfn symbol buffer frame
))))
1015 describe-symbol-backends
))))
1016 (single (null (cdr docs
))))
1018 (goto-char (point-min))
1019 (let ((inhibit-read-only t
)
1020 (name (caar docs
)) ;Name of doc currently at BOB.
1021 (doc (cdr (cadr docs
)))) ;Doc to add at BOB.
1023 (delete-region (point) (progn (skip-chars-backward " \t\n") (point)))
1026 (propertize "\n" 'face
'(:height
0.1 :inverse-video t
)))
1029 (insert (symbol-name symbol
)
1030 " is also a " name
"." "\n\n")))
1031 (setq docs
(cdr docs
)))
1033 ;; Don't record the `describe-variable' item in the stack.
1034 (setq help-xref-stack-item nil
)
1035 (help-setup-xref (list #'describe-symbol symbol
) nil
))
1036 (goto-char (point-min)))))
1039 (defun describe-syntax (&optional buffer
)
1040 "Describe the syntax specifications in the syntax table of BUFFER.
1041 The descriptions are inserted in a help buffer, which is then displayed.
1042 BUFFER defaults to the current buffer."
1044 (setq buffer
(or buffer
(current-buffer)))
1045 (help-setup-xref (list #'describe-syntax buffer
)
1046 (called-interactively-p 'interactive
))
1047 (with-help-window (help-buffer)
1048 (let ((table (with-current-buffer buffer
(syntax-table))))
1049 (with-current-buffer standard-output
1050 (describe-vector table
'internal-describe-syntax-value
)
1051 (while (setq table
(char-table-parent table
))
1052 (insert "\nThe parent syntax table is:")
1053 (describe-vector table
'internal-describe-syntax-value
))))))
1055 (defun help-describe-category-set (value)
1057 ((null value
) "default")
1058 ((char-table-p value
) "deeper char-table ...")
1059 (t (condition-case nil
1060 (category-set-mnemonics value
)
1061 (error "invalid"))))))
1064 (defun describe-categories (&optional buffer
)
1065 "Describe the category specifications in the current category table.
1066 The descriptions are inserted in a buffer, which is then displayed.
1067 If BUFFER is non-nil, then describe BUFFER's category table instead.
1068 BUFFER should be a buffer or a buffer name."
1070 (setq buffer
(or buffer
(current-buffer)))
1071 (help-setup-xref (list #'describe-categories buffer
)
1072 (called-interactively-p 'interactive
))
1073 (with-help-window (help-buffer)
1074 (let* ((table (with-current-buffer buffer
(category-table)))
1075 (docs (char-table-extra-slot table
0)))
1076 (if (or (not (vectorp docs
)) (/= (length docs
) 95))
1077 (error "Invalid first extra slot in this category table\n"))
1078 (with-current-buffer standard-output
1079 (insert "Legend of category mnemonics (see the tail for the longer description)\n")
1080 (let ((pos (point)) (items 0) lines n
)
1082 (if (aref docs i
) (setq items
(1+ items
))))
1083 (setq lines
(1+ (/ (1- items
) 4)))
1086 (let ((elt (aref docs i
)))
1088 (string-match ".*" elt
)
1089 (setq elt
(match-string 0 elt
))
1090 (if (>= (length elt
) 17)
1091 (setq elt
(concat (substring elt
0 14) "...")))
1092 (if (< (point) (point-max))
1093 (move-to-column (* 20 (/ n lines
)) t
))
1094 (insert (+ i ?\s
) ?
: elt
)
1095 (if (< (point) (point-max))
1099 (if (= (% n lines
) 0)
1100 (goto-char pos
))))))
1101 (goto-char (point-max))
1103 "character(s)\tcategory mnemonics\n"
1104 "------------\t------------------")
1105 (describe-vector table
'help-describe-category-set
)
1106 (insert "Legend of category mnemonics:\n")
1108 (let ((elt (aref docs i
)))
1110 (if (string-match "\n" elt
)
1111 (setq elt
(substring elt
(match-end 0))))
1112 (insert (+ i ?\s
) ": " elt
"\n"))))
1113 (while (setq table
(char-table-parent table
))
1114 (insert "\nThe parent category table is:")
1115 (describe-vector table
'help-describe-category-set
))))))
1118 ;;; Replacements for old lib-src/ programs. Don't seem especially useful.
1120 ;; Replaces lib-src/digest-doc.c.
1122 (defun doc-file-to-man (file)
1123 "Produce an nroff buffer containing the doc-strings from the DOC file."
1124 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1125 internal-doc-file-name t
)))
1126 (or (file-readable-p file
)
1127 (error "Cannot read file `%s'" file
))
1128 (pop-to-buffer (generate-new-buffer "*man-doc*"))
1129 (setq buffer-undo-list t
)
1130 (insert ".TH \"Command Summary for GNU Emacs\"\n"
1131 ".AU Richard M. Stallman\n")
1132 (insert-file-contents file
)
1134 (while (search-forward "\x1f" nil
'move
)
1135 (if (looking-at "S")
1136 (delete-region (1- (point)) (line-end-position))
1142 (insert (if (looking-at "F") "Function " "Variable "))
1145 (insert ".DS L\n"))))
1147 (setq buffer-undo-list nil
)
1150 ;; Replaces lib-src/sorted-doc.c.
1152 (defun doc-file-to-info (file)
1153 "Produce a texinfo buffer with sorted doc-strings from the DOC file."
1154 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1155 internal-doc-file-name t
)))
1156 (or (file-readable-p file
)
1157 (error "Cannot read file `%s'" file
))
1158 (let ((i 0) type name doc alist
)
1160 (insert-file-contents file
)
1161 ;; The characters "@{}" need special treatment.
1162 (while (re-search-forward "[@{}]" nil t
)
1166 (goto-char (point-min))
1167 (while (search-forward "\x1f" nil t
)
1168 (unless (looking-at "S")
1169 (setq type
(char-after)
1170 name
(buffer-substring (1+ (point)) (line-end-position))
1171 doc
(buffer-substring (line-beginning-position 2)
1172 (if (search-forward "\x1f" nil
'move
)
1175 alist
(cons (list name type doc
) alist
))
1176 (backward-char 1))))
1177 (pop-to-buffer (generate-new-buffer "*info-doc*"))
1178 (setq buffer-undo-list t
)
1179 ;; Write the output header.
1180 (insert "\\input texinfo @c -*-texinfo-*-\n"
1181 "@setfilename emacsdoc.info\n"
1182 "@settitle Command Summary for GNU Emacs\n"
1185 "@unnumbered Command Summary for GNU Emacs\n\n"
1188 "@global@let@ITEM@item\n"
1189 "@def@item{@filbreak@vskip5pt@ITEM}\n"
1190 "@font@tensy cmsy10 scaled @magstephalf\n"
1191 "@font@teni cmmi10 scaled @magstephalf\n"
1192 "@def\\{{@tensy@char110}}\n" ; this backslash goes with cmr10
1193 "@def|{{@tensy@char106}}\n"
1194 "@def@{{{@tensy@char102}}\n"
1195 "@def@}{{@tensy@char103}}\n"
1196 "@def<{{@teni@char62}}\n"
1197 "@def>{{@teni@char60}}\n"
1200 "@tableindent-0.2in\n"
1202 ;; Sort the array by name; within each name, by type (functions first).
1203 (setq alist
(sort alist
(lambda (e1 e2
)
1204 (if (string-equal (car e1
) (car e2
))
1205 (<= (cadr e1
) (cadr e2
))
1206 (string-lessp (car e1
) (car e2
))))))
1207 ;; Print each function.
1210 (if (char-equal (cadr e
) ?\F
) "Function" "Variable")
1211 " @code{" (car e
) "}\n@display\n"
1214 ;; Try to avoid a save size overflow in the TeX output routine.
1215 (if (zerop (setq i
(%
(1+ i
) 100)))
1216 (insert "\n@end table\n@table @asis\n")))
1217 (insert "@end table\n"
1219 (setq buffer-undo-list nil
)
1224 ;;; help-fns.el ends here