1 ;;; help-fns.el --- Complex help functions -*- lexical-binding: t -*-
3 ;; Copyright (C) 1985-1986, 1993-1994, 1998-2016 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.")
46 (defvar describe-function-orig-buffer nil
47 "Buffer that was current when `describe-function' was invoked.
48 Functions on `help-fns-describe-function-functions' can use this
49 to get buffer-local values.")
52 (defun describe-function (function)
53 "Display the full documentation of FUNCTION (a symbol).
54 When called from lisp, FUNCTION may also be a function object."
56 (let* ((fn (function-called-at-point))
57 (enable-recursive-minibuffers t
)
60 (format "Describe function (default %s): " fn
)
61 "Describe function: ")
62 obarray
'fboundp t nil nil
63 (and fn
(symbol-name fn
)))))
64 (unless (equal val
"")
65 (setq fn
(intern val
)))
66 (unless (and fn
(symbolp fn
))
67 (user-error "You didn't specify a function symbol"))
69 (user-error "Symbol's function definition is void: %s" fn
))
71 ;; We save describe-function-orig-buffer on the help xref stack, so
72 ;; it is restored by the back/forward buttons. 'help-buffer'
73 ;; expects (current-buffer) to be a help buffer when processing
74 ;; those buttons, so we can't change the current buffer before
76 (let ((describe-function-orig-buffer
77 (or describe-function-orig-buffer
81 (list (lambda (function buffer
)
82 (let ((describe-function-orig-buffer
83 (if (buffer-live-p buffer
) buffer
)))
84 (describe-function function
)))
85 function describe-function-orig-buffer
)
86 (called-interactively-p 'interactive
))
89 (with-help-window (help-buffer)
91 ;; Use " is " instead of a colon so that
92 ;; it is easier to get out the function name using forward-sexp.
94 (describe-function-1 function
)
95 (with-current-buffer standard-output
96 ;; Return the text we displayed.
101 ;; Could be this, if we make symbol-file do the work below.
102 ;; (defun help-C-file-name (subr-or-var kind)
103 ;; "Return the name of the C file where SUBR-OR-VAR is defined.
104 ;; KIND should be `var' for a variable or `subr' for a subroutine."
105 ;; (symbol-file (if (symbolp subr-or-var) subr-or-var
106 ;; (subr-name subr-or-var))
107 ;; (if (eq kind 'var) 'defvar 'defun)))
109 (defun help-C-file-name (subr-or-var kind
)
110 "Return the name of the C file where SUBR-OR-VAR is defined.
111 KIND should be `var' for a variable or `subr' for a subroutine."
112 (let ((docbuf (get-buffer-create " *DOC*"))
113 (name (if (eq 'var kind
)
114 (concat "V" (symbol-name subr-or-var
))
115 (concat "F" (subr-name (advice--cd*r subr-or-var
))))))
116 (with-current-buffer docbuf
117 (goto-char (point-min))
119 (insert-file-contents-literally
120 (expand-file-name internal-doc-file-name doc-directory
)))
121 (let ((file (catch 'loop
123 (let ((pnt (search-forward (concat "\x1f" name
"\n"))))
124 (re-search-backward "\x1fS\\(.*\\)")
125 (let ((file (match-string 1)))
126 (if (member file build-files
)
128 (goto-char pnt
))))))))
129 (if (string-match "^ns.*\\(\\.o\\|obj\\)\\'" file
)
130 (setq file
(replace-match ".m" t t file
1))
131 (if (string-match "\\.\\(o\\|obj\\)\\'" file
)
132 (setq file
(replace-match ".c" t t file
))))
133 (if (string-match "\\.\\(c\\|m\\)\\'" file
)
137 (defcustom help-downcase-arguments nil
138 "If non-nil, argument names in *Help* buffers are downcased."
143 (defun help-highlight-arg (arg)
144 "Highlight ARG as an argument name for a *Help* buffer.
145 Return ARG in face `help-argument-name'; ARG is also downcased
146 if the variable `help-downcase-arguments' is non-nil."
147 (propertize (if help-downcase-arguments
(downcase arg
) arg
)
148 'face
'help-argument-name
))
150 (defun help-do-arg-highlight (doc args
)
151 (with-syntax-table (make-syntax-table emacs-lisp-mode-syntax-table
)
152 (modify-syntax-entry ?\-
"w")
154 (setq doc
(replace-regexp-in-string
155 ;; This is heuristic, but covers all common cases
157 (concat "\\<" ; beginning of word
158 "\\(?:[a-z-]*-\\)?" ; for xxx-ARG
162 "\\(?:es\\|s\\|th\\)?" ; for ARGth, ARGs
163 "\\(?:-[a-z0-9-]+\\)?" ; for ARG-xxx, ARG-n
164 "\\(?:-[{([<`\"‘].*?\\)?"; for ARG-{x}, (x), <x>, [x], `x', ‘x’
166 (help-highlight-arg arg
)
170 (defun help-highlight-arguments (usage doc
&rest args
)
171 (when (and usage
(string-match "^(" usage
))
174 (goto-char (point-min))
175 (let ((case-fold-search nil
)
176 (next (not (or args
(looking-at "\\["))))
178 ;; Make a list of all arguments
179 (skip-chars-forward "^ ")
181 (or opt
(not (looking-at " &")) (setq opt t
))
182 (if (not (re-search-forward " \\([\\[(]*\\)\\([^] &).]+\\)" nil t
))
184 (setq args
(cons (match-string 2) args
))
185 (when (and opt
(string= (match-string 1) "("))
186 ;; A pesky CL-style optional argument with default value,
187 ;; so let's skip over it
188 (search-backward "(")
189 (goto-char (scan-sexps (point) 1)))))
190 ;; Highlight arguments in the USAGE string
191 (setq usage
(help-do-arg-highlight (buffer-string) args
))
192 ;; Highlight arguments in the DOC string
193 (setq doc
(and doc
(help-do-arg-highlight doc args
))))))
194 ;; Return value is like the one from help-split-fundoc, but highlighted
197 ;; The following function was compiled from the former functions
198 ;; `describe-simplify-lib-file-name' and `find-source-lisp-file' with
199 ;; some excerpts from `describe-function-1' and `describe-variable'.
200 ;; The only additional twists provided are (1) locate the defining file
201 ;; for autoloaded functions, and (2) give preference to files in the
202 ;; "install directory" (directories found via `load-path') rather than
203 ;; to files in the "compile directory" (directories found by searching
204 ;; the loaddefs.el file). We autoload it because it's also used by
205 ;; `describe-face' (instead of `describe-simplify-lib-file-name').
208 (defun find-lisp-object-file-name (object type
)
209 "Guess the file that defined the Lisp object OBJECT, of type TYPE.
210 OBJECT should be a symbol associated with a function, variable, or face;
211 alternatively, it can be a function definition.
212 If TYPE is `defvar', search for a variable definition.
213 If TYPE is `defface', search for a face definition.
214 If TYPE is not a symbol, search for a function definition.
216 The return value is the absolute name of a readable file where OBJECT is
217 defined. If several such files exist, preference is given to a file
218 found via `load-path'. The return value can also be `C-source', which
219 means that OBJECT is a function or variable defined in C. If no
220 suitable file is found, return nil."
221 (let* ((autoloaded (autoloadp type
))
222 (file-name (or (and autoloaded
(nth 1 type
))
224 ;; FIXME: Why do we have this weird "If TYPE is the
225 ;; value returned by `symbol-function' for a function
226 ;; symbol" exception?
227 object
(or (if (symbolp type
) type
) 'defun
)))))
230 ;; An autoloaded function: Locate the file since `symbol-function'
231 ;; has only returned a bare string here.
233 (locate-file file-name load-path
'(".el" ".elc") 'readable
)))
234 ((and (stringp file-name
)
235 (string-match "[.]*loaddefs.el\\'" file-name
))
236 ;; An autoloaded variable or face. Visit loaddefs.el in a buffer
237 ;; and try to extract the defining file. The following form is
238 ;; from `describe-function-1' and `describe-variable'.
241 (find-function-search-for-symbol object nil file-name
)
244 (with-current-buffer (car location
)
245 (goto-char (cdr location
))
246 (when (re-search-backward
247 "^;;; Generated autoloads from \\(.*\\)" nil t
)
250 (file-name-sans-extension
251 (match-string-no-properties 1))
252 load-path
'(".el" ".elc") 'readable
))))))))
255 ((and (not file-name
) (subrp type
))
256 ;; A built-in function. The form is from `describe-function-1'.
257 (if (get-buffer " *DOC*")
258 (help-C-file-name type
'subr
)
260 ((and (not file-name
) (symbolp object
)
261 (integerp (get object
'variable-documentation
)))
262 ;; A variable defined in C. The form is from `describe-variable'.
263 (if (get-buffer " *DOC*")
264 (help-C-file-name object
'var
)
266 ((not (stringp file-name
))
267 ;; If we don't have a file-name string by now, we lost.
269 ;; Now, `file-name' should have become an absolute file name.
270 ;; For files loaded from ~/.foo.elc, try ~/.foo.
271 ;; This applies to config files like ~/.emacs,
272 ;; which people sometimes compile.
274 (and (string-match "\\`\\..*\\.elc\\'"
275 (file-name-nondirectory file-name
))
276 (string-equal (file-name-directory file-name
)
277 (file-name-as-directory (expand-file-name "~")))
278 (file-readable-p (setq fn
(file-name-sans-extension file-name
)))
280 ;; When the Elisp source file can be found in the install
281 ;; directory, return the name of that file.
283 (if (string-match "[.]elc\\'" file-name
)
284 (substring-no-properties file-name
0 -
1)
286 (or (and (file-readable-p lib-name
) lib-name
)
287 ;; The library might be compressed.
288 (and (file-readable-p (concat lib-name
".gz")) lib-name
))))
289 ((let* ((lib-name (file-name-nondirectory file-name
))
290 ;; The next form is from `describe-simplify-lib-file-name'.
292 ;; Try converting the absolute file name to a library
293 ;; name, convert that back to a file name and see if we
294 ;; get the original one. If so, they are equivalent.
295 (if (equal file-name
(locate-file lib-name load-path
'("")))
296 (if (string-match "[.]elc\\'" lib-name
)
297 (substring-no-properties lib-name
0 -
1)
300 (src-file (locate-library file-name t nil
'readable
)))
301 (and src-file
(file-readable-p src-file
) src-file
))))))
303 (defun help-fns--key-bindings (function)
304 (when (commandp function
)
305 (let ((pt2 (with-current-buffer standard-output
(point)))
306 (remapped (command-remapping function
)))
307 (unless (memq remapped
'(ignore undefined
))
308 (let ((keys (where-is-internal
309 (or remapped function
) overriding-local-map nil nil
))
311 (if (and (eq function
'self-insert-command
)
312 (vectorp (car-safe keys
))
313 (consp (aref (car keys
) 0)))
314 (princ "It is bound to many ordinary text characters.\n")
315 ;; Which non-control non-meta keys run this command?
317 (if (member (event-modifiers (aref key
0)) '(nil (shift)))
318 (push key non-modified-keys
)))
320 (princ "Its keys are remapped to ")
321 (princ (if (symbolp remapped
)
322 (format-message "`%s'" remapped
)
323 "an anonymous command"))
328 "Without this remapping, it would be bound to "
330 ;; If lots of ordinary text characters run this command,
331 ;; don't mention them one by one.
332 (if (< (length non-modified-keys
) 10)
333 (princ (mapconcat 'key-description keys
", "))
334 (dolist (key non-modified-keys
)
335 (setq keys
(delq key keys
)))
338 (princ (mapconcat 'key-description keys
", "))
339 (princ ", and many ordinary text characters"))
340 (princ "many ordinary text characters"))))
341 (when (or remapped keys non-modified-keys
)
345 (with-current-buffer standard-output
346 (fill-region-as-paragraph pt2
(point))
347 (unless (looking-back "\n\n" (- (point) 2))
350 (defun help-fns--compiler-macro (function)
351 (let ((handler (function-get function
'compiler-macro
)))
353 (insert "\nThis function has a compiler macro")
354 (if (symbolp handler
)
356 (insert (format-message " `%s'" handler
))
358 (re-search-backward (substitute-command-keys "`\\([^`']+\\)'")
360 (help-xref-button 1 'help-function handler
)))
361 ;; FIXME: Obsolete since 24.4.
362 (let ((lib (get function
'compiler-macro-file
)))
364 (insert (format-message " in `%s'" lib
))
366 (re-search-backward (substitute-command-keys "`\\([^`']+\\)'")
368 (help-xref-button 1 'help-function-cmacro function lib
)))))
371 (defun help-fns--signature (function doc real-def real-function buffer
)
372 "Insert usage at point and return docstring. With highlighting."
373 (if (keymapp function
)
374 doc
; If definition is a keymap, skip arglist note.
375 (let* ((advertised (gethash real-def advertised-signature-table t
))
376 (arglist (if (listp advertised
)
377 advertised
(help-function-arglist real-def
)))
378 (usage (help-split-fundoc doc function
)))
379 (if usage
(setq doc
(cdr usage
)))
381 ((and usage
(not (listp advertised
))) (car usage
))
383 (help--make-usage-docstring function arglist
))
384 ((stringp arglist
) arglist
)
385 ;; Maybe the arglist is in the docstring of a symbol
386 ;; this one is aliased to.
387 ((let ((fun real-function
))
388 (while (and (symbolp fun
)
389 (setq fun
(symbol-function fun
))
390 (not (setq usage
(help-split-fundoc
395 ((or (stringp real-def
)
397 (format "\nMacro: %s"
398 (help--docstring-quote
399 (format-kbd-macro real-def
))))
400 (t "[Missing arglist. Please make a bug report.]")))
401 ;; Insert "`X", not "(\` X)", when documenting `X.
402 (use1 (replace-regexp-in-string
403 "\\`(\\\\=\\\\\\\\=` \\([^\n ]*\\))\\'"
406 (let (subst-use1 subst-doc
)
407 (with-current-buffer buffer
408 (setq subst-use1
(substitute-command-keys use1
))
409 (setq subst-doc
(substitute-command-keys doc
)))
410 (help-highlight-arguments subst-use1 subst-doc
))
412 (let ((fill-begin (point))
413 (high-usage (car high
))
414 (high-doc (cdr high
)))
415 (insert high-usage
"\n")
416 (fill-region fill-begin
(point))
419 (defun help-fns--parent-mode (function)
420 ;; If this is a derived mode, link to the parent.
421 (let ((parent-mode (and (symbolp function
)
423 'derived-mode-parent
))))
425 (insert (substitute-command-keys "\nParent mode: `"))
427 (insert (format "%s" parent-mode
))
428 (make-text-button beg
(point)
430 'help-args
(list parent-mode
)))
431 (insert (substitute-command-keys "'.\n")))))
433 (defun help-fns--obsolete (function)
434 ;; Ignore lambda constructs, keyboard macros, etc.
435 (let* ((obsolete (and (symbolp function
)
436 (get function
'byte-obsolete-info
)))
437 (use (car obsolete
)))
440 (if (eq (car-safe (symbol-function function
)) 'macro
)
444 (when (nth 2 obsolete
)
445 (insert (format " since %s" (nth 2 obsolete
))))
446 (insert (cond ((stringp use
) (concat ";\n" use
))
447 (use (format-message ";\nuse `%s' instead." use
))
451 ;; We could use `symbol-file' but this is a wee bit more efficient.
452 (defun help-fns--autoloaded-p (function file
)
453 "Return non-nil if FUNCTION has previously been autoloaded.
454 FILE is the file where FUNCTION was probably defined."
455 (let* ((file (file-name-sans-extension (file-truename file
)))
456 (load-hist load-history
)
457 (target (cons t function
))
459 (while (and load-hist
(not found
))
460 (and (caar load-hist
)
461 (equal (file-name-sans-extension (caar load-hist
)) file
)
462 (setq found
(member target
(cdar load-hist
))))
463 (setq load-hist
(cdr load-hist
)))
466 (defun help-fns--interactive-only (function)
467 "Insert some help blurb if FUNCTION should only be used interactively."
468 ;; Ignore lambda constructs, keyboard macros, etc.
469 (and (symbolp function
)
470 (not (eq (car-safe (symbol-function function
)) 'macro
))
471 (let* ((interactive-only
472 (or (get function
'interactive-only
)
473 (if (boundp 'byte-compile-interactive-only-functions
)
475 byte-compile-interactive-only-functions
)))))
476 (when interactive-only
477 (insert "\nThis function is for interactive use only"
478 ;; Cf byte-compile-form.
479 (cond ((stringp interactive-only
)
480 (format ";\nin Lisp code %s" interactive-only
))
481 ((and (symbolp 'interactive-only
)
482 (not (eq interactive-only t
)))
483 (format-message ";\nin Lisp code use `%s' instead."
488 (defun help-fns-short-filename (filename)
489 (let* ((abbrev (abbreviate-file-name filename
))
491 (dolist (dir load-path
)
492 (let ((rel (file-relative-name filename dir
)))
493 (if (< (length rel
) (length short
))
495 (let ((rel (file-relative-name abbrev dir
)))
496 (if (< (length rel
) (length short
))
501 (defun describe-function-1 (function)
502 (let* ((advised (and (symbolp function
)
504 (advice--p (advice--symbol-function function
))))
505 ;; If the function is advised, use the symbol that has the
506 ;; real definition, if that symbol is already set up.
509 (advice--cd*r
(advice--symbol-function function
)))
511 ;; Get the real definition.
512 (def (if (symbolp real-function
)
513 (or (symbol-function real-function
)
514 (signal 'void-function
(list real-function
)))
516 (aliased (or (symbolp def
)
517 ;; Advised & aliased function.
518 (and advised
(symbolp real-function
)
519 (not (eq 'autoload
(car-safe def
))))))
521 (aliased (let ((f real-function
))
522 (while (and (fboundp f
)
523 (symbolp (symbol-function f
)))
524 (setq f
(symbol-function f
)))
526 ((subrp def
) (intern (subr-name def
)))
528 (sig-key (if (subrp def
)
529 (indirect-function real-def
)
531 (file-name (find-lisp-object-file-name function
(if aliased
'defun
533 (pt1 (with-current-buffer (help-buffer) (point)))
534 (beg (if (and (or (byte-code-function-p def
)
536 (memq (car-safe def
) '(macro lambda closure
)))
538 (help-fns--autoloaded-p function file-name
))
540 "an interactive autoloaded "
542 (if (commandp def
) "an interactive " "a "))))
544 ;; Print what kind of function-like object FUNCTION is.
545 (princ (cond ((or (stringp def
) (vectorp def
))
548 (if (eq 'unevalled
(cdr (subr-arity def
)))
549 (concat beg
"special form")
550 (concat beg
"built-in function")))
551 ;; Aliases are Lisp functions, so we need to check
552 ;; aliases before functions.
554 (format-message "an alias for `%s'" real-def
))
556 (format "%s autoloaded %s"
557 (if (commandp def
) "an interactive" "an")
558 (if (eq (nth 4 def
) 'keymap
) "keymap"
559 (if (nth 4 def
) "Lisp macro" "Lisp function"))))
560 ((or (eq (car-safe def
) 'macro
)
561 ;; For advised macros, def is a lambda
562 ;; expression or a byte-code-function-p, so we
563 ;; need to check macros before functions.
565 (concat beg
"Lisp macro"))
566 ((byte-code-function-p def
)
567 (concat beg
"compiled Lisp function"))
568 ((eq (car-safe def
) 'lambda
)
569 (concat beg
"Lisp function"))
570 ((eq (car-safe def
) 'closure
)
571 (concat beg
"Lisp closure"))
574 (elts (cdr-safe def
)))
576 (if (char-table-p (car-safe elts
))
579 (setq elts
(cdr-safe elts
)))
580 (concat beg
(if is-full
"keymap" "sparse keymap"))))
583 (if (and aliased
(not (fboundp real-def
)))
584 (princ ",\nwhich is not defined. Please make a bug report.")
585 (with-current-buffer standard-output
588 (when (re-search-backward (substitute-command-keys
589 "alias for `\\([^`']+\\)'")
591 (help-xref-button 1 'help-function real-def
)))))
594 ;; We used to add .el to the file name,
595 ;; but that's completely wrong when the user used load-file.
596 (princ (format-message " in `%s'"
597 (if (eq file-name
'C-source
)
599 (help-fns-short-filename file-name
))))
600 ;; Make a hyperlink to the library.
601 (with-current-buffer standard-output
603 (re-search-backward (substitute-command-keys "`\\([^`']+\\)'")
605 (help-xref-button 1 'help-function-def function file-name
))))
607 (with-current-buffer (help-buffer)
608 (fill-region-as-paragraph (save-excursion (goto-char pt1
) (forward-line 0) (point))
612 (let ((doc-raw (documentation function t
))
613 (key-bindings-buffer (current-buffer)))
615 ;; If the function is autoloaded, and its docstring has
616 ;; key substitution constructs, load the library.
617 (and (autoloadp real-def
) doc-raw
618 help-enable-auto-load
619 (string-match "\\([^\\]=\\|[^=]\\|\\`\\)\\\\[[{<]" doc-raw
)
620 (autoload-do-load real-def
))
622 (help-fns--key-bindings function
)
623 (with-current-buffer standard-output
624 (let ((doc (help-fns--signature function doc-raw sig-key
625 real-function key-bindings-buffer
)))
626 (run-hook-with-args 'help-fns-describe-function-functions function
)
628 (or doc
"Not documented."))
629 ;; Avoid asking the user annoying questions if she decides
630 ;; to save the help buffer, when her locale's codeset
632 (unless (memq text-quoting-style
'(straight grave
))
633 (set-buffer-file-coding-system 'utf-8
))))))))
635 ;; Add defaults to `help-fns-describe-function-functions'.
636 (add-hook 'help-fns-describe-function-functions
#'help-fns--obsolete
)
637 (add-hook 'help-fns-describe-function-functions
#'help-fns--interactive-only
)
638 (add-hook 'help-fns-describe-function-functions
#'help-fns--parent-mode
)
639 (add-hook 'help-fns-describe-function-functions
#'help-fns--compiler-macro
)
645 (defun variable-at-point (&optional any-symbol
)
646 "Return the bound variable symbol found at or before point.
647 Return 0 if there is no such symbol.
648 If ANY-SYMBOL is non-nil, don't insist the symbol be bound."
649 (with-syntax-table emacs-lisp-mode-syntax-table
650 (or (condition-case ()
652 (skip-chars-forward "'")
653 (or (not (zerop (skip-syntax-backward "_w")))
654 (eq (char-syntax (following-char)) ?w
)
655 (eq (char-syntax (following-char)) ?_
)
657 (skip-chars-forward "'")
658 (let ((obj (read (current-buffer))))
659 (and (symbolp obj
) (boundp obj
) obj
)))
661 (let* ((str (find-tag-default))
662 (sym (if str
(intern-soft str
))))
663 (if (and sym
(or any-symbol
(boundp sym
)))
666 (when (and str
(string-match "\\`\\W*\\(.*?\\)\\W*\\'" str
))
667 (setq sym
(intern-soft (match-string 1 str
)))
668 (and (or any-symbol
(boundp sym
)) sym
)))))
671 (defun describe-variable-custom-version-info (variable)
672 (let ((custom-version (get variable
'custom-version
))
673 (cpv (get variable
'custom-package-version
))
677 (format "This variable was introduced, or its default value was changed, in\nversion %s of Emacs.\n"
680 (let* ((package (car-safe cpv
))
681 (version (if (listp (cdr-safe cpv
))
684 (pkg-versions (assq package customize-package-emacs-version-alist
))
685 (emacsv (cdr (assoc version pkg-versions
))))
686 (if (and package version
)
688 (format (concat "This variable was introduced, or its default value was changed, in\nversion %s of the %s package"
690 (format " that is part of Emacs %s" emacsv
))
692 version package
))))))
696 (defun describe-variable (variable &optional buffer frame
)
697 "Display the full documentation of VARIABLE (a symbol).
698 Returns the documentation as a string, also.
699 If VARIABLE has a buffer-local value in BUFFER or FRAME
700 \(default to the current buffer and current frame),
701 it is displayed along with the global value."
703 (let ((v (variable-at-point))
704 (enable-recursive-minibuffers t
)
706 (setq val
(completing-read (if (symbolp v
)
708 "Describe variable (default %s): " v
)
709 "Describe variable: ")
712 (or (get vv
'variable-documentation
)
713 (and (boundp vv
) (not (keywordp vv
)))))
715 (if (symbolp v
) (symbol-name v
))))
716 (list (if (equal val
"")
719 (unless (buffer-live-p buffer
) (setq buffer
(current-buffer)))
720 (unless (frame-live-p frame
) (setq frame
(selected-frame)))
721 (if (not (symbolp variable
))
722 (message "You did not specify a variable")
724 (let ((valvoid (not (with-current-buffer buffer
(boundp variable
))))
725 (permanent-local (get variable
'permanent-local
))
726 val val-start-pos locus
)
727 ;; Extract the value before setting up the output buffer,
728 ;; in case `buffer' *is* the output buffer.
730 (with-selected-frame frame
731 (with-current-buffer buffer
732 (setq val
(symbol-value variable
)
733 locus
(variable-binding-locus variable
)))))
734 (help-setup-xref (list #'describe-variable variable buffer
)
735 (called-interactively-p 'interactive
))
736 (with-help-window (help-buffer)
737 (with-current-buffer buffer
739 (setq file-name
(find-lisp-object-file-name variable
'defvar
))
743 (princ (format-message
744 " is a variable defined in `%s'.\n"
745 (if (eq file-name
'C-source
)
747 (file-name-nondirectory file-name
))))
748 (with-current-buffer standard-output
750 (re-search-backward (substitute-command-keys
753 (help-xref-button 1 'help-variable-def
754 variable file-name
)))
756 (princ "It is void as a variable.")
759 (princ " is void as a variable.")
760 (princ (substitute-command-keys "'s ")))))
762 (with-current-buffer standard-output
763 (setq val-start-pos
(point))
766 (line-beg (line-beginning-position))
769 (let ((print-quoted t
))
770 (prin1-to-string val
))))
771 (if (and (symbolp val
) (not (booleanp val
)))
772 (format-message "`%s'" rep
)
774 (if (< (+ (length print-rep
) (point) (- line-beg
)) 68)
778 (if (< (point) (+ 68 (line-beginning-position 0)))
779 (delete-region from
(1+ from
))
780 (delete-region (1- from
) from
)))
781 (let* ((sv (get variable
'standard-value
))
782 (origval (and (consp sv
)
785 (error :help-eval-error
)))))
786 (when (and (consp sv
)
787 (not (equal origval val
))
788 (not (equal origval
:help-eval-error
)))
789 (princ "\nOriginal value was \n")
792 (if (< (point) (+ from
20))
793 (delete-region (1- from
) from
)))))))
798 (princ (format "Local in buffer %s; "
799 (buffer-name buffer
))))
801 (princ (format "It is a frame-local variable; ")))
802 ((terminal-live-p locus
)
803 (princ (format "It is a terminal-local variable; ")))
805 (princ (format "It is local to %S" locus
))))
806 (if (not (default-boundp variable
))
807 (princ "globally void")
808 (let ((global-val (default-value variable
)))
809 (with-current-buffer standard-output
810 (princ "global value is ")
811 (if (eq val global-val
)
814 ;; Fixme: pp can take an age if you happen to
815 ;; ask for a very large expression. We should
816 ;; probably print it raw once and check it's a
817 ;; sensible size before prettyprinting. -- fx
818 (let ((from (point)))
820 ;; See previous comment for this function.
821 ;; (help-xref-on-pp from (point))
822 (if (< (point) (+ from
20))
823 (delete-region (1- from
) from
)))))))
826 ;; If the value is large, move it to the end.
827 (with-current-buffer standard-output
828 (when (> (count-lines (point-min) (point-max)) 10)
829 ;; Note that setting the syntax table like below
830 ;; makes forward-sexp move over a `'s' at the end
832 (set-syntax-table emacs-lisp-mode-syntax-table
)
833 (goto-char val-start-pos
)
834 ;; The line below previously read as
835 ;; (delete-region (point) (progn (end-of-line) (point)))
836 ;; which suppressed display of the buffer local value for
838 (when (looking-at "value is") (replace-match ""))
840 (insert "\n\nValue:")
841 (set (make-local-variable 'help-button-cache
)
843 (insert "value is shown ")
844 (insert-button "below"
845 'action help-button-cache
847 'help-echo
"mouse-2, RET: show value")
851 (let* ((alias (condition-case nil
852 (indirect-variable variable
)
854 (obsolete (get variable
'byte-obsolete-variable
))
856 (safe-var (get variable
'safe-local-variable
))
857 (doc (or (documentation-property
858 variable
'variable-documentation
)
859 (documentation-property
860 alias
'variable-documentation
)))
863 ;; Mention if it's a local variable.
865 ((and (local-variable-if-set-p variable
)
866 (or (not (local-variable-p variable
))
868 (local-variable-if-set-p variable
))))
870 (princ " Automatically becomes ")
872 (princ "permanently "))
873 (princ "buffer-local when set.\n"))
874 ((not permanent-local
))
878 (substitute-command-keys
879 " This variable's buffer-local value is permanent.\n")))
882 (princ (substitute-command-keys
883 " This variable's value is permanent \
884 if it is given a local binding.\n"))))
886 ;; Mention if it's an alias.
887 (unless (eq alias variable
)
889 (princ (format-message
890 " This variable is an alias for `%s'.\n"
895 (princ " This variable is obsolete")
897 (princ (format " since %s" (nth 2 obsolete
))))
898 (princ (cond ((stringp use
) (concat ";\n " use
))
899 (use (format-message ";\n use `%s' instead."
904 (when (member (cons variable val
)
905 (with-current-buffer buffer
906 file-local-variables-alist
))
908 (if (member (cons variable val
)
909 (with-current-buffer buffer
910 dir-local-variables-alist
))
911 (let ((file (and (buffer-file-name buffer
)
913 (buffer-file-name buffer
)))
914 (dir-locals-find-file
915 (buffer-file-name buffer
))))
917 (princ (substitute-command-keys
918 " This variable's value is directory-local"))
922 (if (consp file
) ; result from cache
923 ;; If the cache element has an mtime, we
924 ;; assume it came from a file.
926 (setq file
(expand-file-name
927 dir-locals-file
(car file
)))
928 ;; Otherwise, assume it was set directly.
929 (setq file
(car file
)
931 (princ (substitute-command-keys
934 "for the directory\n `")))
935 (with-current-buffer standard-output
937 file
'type
'help-dir-local-var-def
938 'help-args
(list variable file
)))
939 (princ (substitute-command-keys "'.\n"))))
940 (princ (substitute-command-keys
941 " This variable's value is file-local.\n"))))
943 (when (memq variable ignored-local-variables
)
945 (princ " This variable is ignored as a file-local \
948 ;; Can be both risky and safe, eg auto-fill-function.
949 (when (risky-local-variable-p variable
)
951 (princ " This variable may be risky if used as a \
952 file-local variable.\n")
953 (when (assq variable safe-local-variable-values
)
954 (princ (substitute-command-keys
955 " However, you have added it to \
956 `safe-local-variable-values'.\n"))))
960 (princ " This variable is safe as a file local variable ")
961 (princ "if its value\n satisfies the predicate ")
962 (princ (if (byte-code-function-p safe-var
)
963 "which is a byte-compiled expression.\n"
964 (format-message "`%s'.\n" safe-var
))))
966 (if extra-line
(terpri))
967 (princ "Documentation:\n")
968 (with-current-buffer standard-output
969 (insert (or doc
"Not documented as a variable."))))
971 ;; Make a link to customize if this variable can be customized.
972 (when (custom-variable-p variable
)
973 (let ((customize-label "customize"))
976 (princ (concat "You can " customize-label
" this variable."))
977 (with-current-buffer standard-output
980 (concat "\\(" customize-label
"\\)") nil t
)
981 (help-xref-button 1 'help-customize-variable variable
))))
982 ;; Note variable's version or package version.
983 (let ((output (describe-variable-custom-version-info variable
)))
989 (with-current-buffer standard-output
990 ;; Return the text we displayed.
991 (buffer-string))))))))
994 (defvar help-xref-stack-item
)
997 (defun describe-symbol (symbol &optional buffer frame
)
998 "Display the full documentation of SYMBOL.
999 Will show the info of SYMBOL as a function, variable, and/or face.
1000 Optional arguments BUFFER and FRAME specify for which buffer and
1001 frame to show the information about SYMBOL; they default to the
1002 current buffer and the selected frame, respectively."
1004 (let* ((v-or-f (symbol-at-point))
1005 (found (cl-some (lambda (x) (funcall (nth 1 x
) v-or-f
))
1006 describe-symbol-backends
))
1007 (v-or-f (if found v-or-f
(function-called-at-point)))
1008 (found (or found v-or-f
))
1009 (enable-recursive-minibuffers t
)
1010 (val (completing-read (if found
1012 "Describe symbol (default %s): " v-or-f
)
1013 "Describe symbol: ")
1016 (cl-some (lambda (x) (funcall (nth 1 x
) vv
))
1017 describe-symbol-backends
))
1019 (if found
(symbol-name v-or-f
)))))
1020 (list (if (equal val
"")
1021 v-or-f
(intern val
)))))
1022 (if (not (symbolp symbol
))
1023 (user-error "You didn't specify a function or variable"))
1024 (unless (buffer-live-p buffer
) (setq buffer
(current-buffer)))
1025 (unless (frame-live-p frame
) (setq frame
(selected-frame)))
1026 (with-current-buffer (help-buffer)
1027 ;; Push the previous item on the stack before clobbering the output buffer.
1028 (help-setup-xref nil nil
)
1032 (mapcar (pcase-lambda (`(,name
,testfn
,descfn
))
1033 (when (funcall testfn symbol
)
1034 ;; Don't record the current entry in the stack.
1035 (setq help-xref-stack-item nil
)
1037 (funcall descfn symbol buffer frame
))))
1038 describe-symbol-backends
))))
1039 (single (null (cdr docs
))))
1041 (goto-char (point-min))
1042 (let ((inhibit-read-only t
)
1043 (name (caar docs
)) ;Name of doc currently at BOB.
1044 (doc (cdr (cadr docs
)))) ;Doc to add at BOB.
1047 (delete-region (point)
1048 (progn (skip-chars-backward " \t\n") (point)))
1051 (propertize "\n" 'face
'(:height
0.1 :inverse-video t
)))
1054 (insert (symbol-name symbol
)
1055 " is also a " name
"." "\n\n"))))
1056 (setq docs
(cdr docs
)))
1058 ;; Don't record the `describe-variable' item in the stack.
1059 (setq help-xref-stack-item nil
)
1060 (help-setup-xref (list #'describe-symbol symbol
) nil
))
1061 (goto-char (point-min)))))
1064 (defun describe-syntax (&optional buffer
)
1065 "Describe the syntax specifications in the syntax table of BUFFER.
1066 The descriptions are inserted in a help buffer, which is then displayed.
1067 BUFFER defaults to the current buffer."
1069 (setq buffer
(or buffer
(current-buffer)))
1070 (help-setup-xref (list #'describe-syntax buffer
)
1071 (called-interactively-p 'interactive
))
1072 (with-help-window (help-buffer)
1073 (let ((table (with-current-buffer buffer
(syntax-table))))
1074 (with-current-buffer standard-output
1075 (describe-vector table
'internal-describe-syntax-value
)
1076 (while (setq table
(char-table-parent table
))
1077 (insert "\nThe parent syntax table is:")
1078 (describe-vector table
'internal-describe-syntax-value
))))))
1080 (defun help-describe-category-set (value)
1082 ((null value
) "default")
1083 ((char-table-p value
) "deeper char-table ...")
1084 (t (condition-case nil
1085 (category-set-mnemonics value
)
1086 (error "invalid"))))))
1089 (defun describe-categories (&optional buffer
)
1090 "Describe the category specifications in the current category table.
1091 The descriptions are inserted in a buffer, which is then displayed.
1092 If BUFFER is non-nil, then describe BUFFER's category table instead.
1093 BUFFER should be a buffer or a buffer name."
1095 (setq buffer
(or buffer
(current-buffer)))
1096 (help-setup-xref (list #'describe-categories buffer
)
1097 (called-interactively-p 'interactive
))
1098 (with-help-window (help-buffer)
1099 (let* ((table (with-current-buffer buffer
(category-table)))
1100 (docs (char-table-extra-slot table
0)))
1101 (if (or (not (vectorp docs
)) (/= (length docs
) 95))
1102 (error "Invalid first extra slot in this category table\n"))
1103 (with-current-buffer standard-output
1104 (insert "Legend of category mnemonics (see the tail for the longer description)\n")
1105 (let ((pos (point)) (items 0) lines n
)
1107 (if (aref docs i
) (setq items
(1+ items
))))
1108 (setq lines
(1+ (/ (1- items
) 4)))
1111 (let ((elt (aref docs i
)))
1113 (string-match ".*" elt
)
1114 (setq elt
(match-string 0 elt
))
1115 (if (>= (length elt
) 17)
1116 (setq elt
(concat (substring elt
0 14) "...")))
1117 (if (< (point) (point-max))
1118 (move-to-column (* 20 (/ n lines
)) t
))
1119 (insert (+ i ?\s
) ?
: elt
)
1120 (if (< (point) (point-max))
1124 (if (= (% n lines
) 0)
1125 (goto-char pos
))))))
1126 (goto-char (point-max))
1128 "character(s)\tcategory mnemonics\n"
1129 "------------\t------------------")
1130 (describe-vector table
'help-describe-category-set
)
1131 (insert "Legend of category mnemonics:\n")
1133 (let ((elt (aref docs i
)))
1135 (if (string-match "\n" elt
)
1136 (setq elt
(substring elt
(match-end 0))))
1137 (insert (+ i ?\s
) ": " elt
"\n"))))
1138 (while (setq table
(char-table-parent table
))
1139 (insert "\nThe parent category table is:")
1140 (describe-vector table
'help-describe-category-set
))))))
1143 ;;; Replacements for old lib-src/ programs. Don't seem especially useful.
1145 ;; Replaces lib-src/digest-doc.c.
1147 (defun doc-file-to-man (file)
1148 "Produce an nroff buffer containing the doc-strings from the DOC file."
1149 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1150 internal-doc-file-name t
)))
1151 (or (file-readable-p file
)
1152 (error "Cannot read file `%s'" file
))
1153 (pop-to-buffer (generate-new-buffer "*man-doc*"))
1154 (setq buffer-undo-list t
)
1155 (insert ".TH \"Command Summary for GNU Emacs\"\n"
1156 ".AU Richard M. Stallman\n")
1157 (insert-file-contents file
)
1159 (while (search-forward "\x1f" nil
'move
)
1160 (if (looking-at "S")
1161 (delete-region (1- (point)) (line-end-position))
1167 (insert (if (looking-at "F") "Function " "Variable "))
1170 (insert ".DS L\n"))))
1172 (setq buffer-undo-list nil
)
1175 ;; Replaces lib-src/sorted-doc.c.
1177 (defun doc-file-to-info (file)
1178 "Produce a texinfo buffer with sorted doc-strings from the DOC file."
1179 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1180 internal-doc-file-name t
)))
1181 (or (file-readable-p file
)
1182 (error "Cannot read file `%s'" file
))
1183 (let ((i 0) type name doc alist
)
1185 (insert-file-contents file
)
1186 ;; The characters "@{}" need special treatment.
1187 (while (re-search-forward "[@{}]" nil t
)
1191 (goto-char (point-min))
1192 (while (search-forward "\x1f" nil t
)
1193 (unless (looking-at "S")
1194 (setq type
(char-after)
1195 name
(buffer-substring (1+ (point)) (line-end-position))
1196 doc
(buffer-substring (line-beginning-position 2)
1197 (if (search-forward "\x1f" nil
'move
)
1200 alist
(cons (list name type doc
) alist
))
1201 (backward-char 1))))
1202 (pop-to-buffer (generate-new-buffer "*info-doc*"))
1203 (setq buffer-undo-list t
)
1204 ;; Write the output header.
1205 (insert "\\input texinfo @c -*-texinfo-*-\n"
1206 "@setfilename emacsdoc.info\n"
1207 "@settitle Command Summary for GNU Emacs\n"
1210 "@unnumbered Command Summary for GNU Emacs\n\n"
1213 "@global@let@ITEM@item\n"
1214 "@def@item{@filbreak@vskip5pt@ITEM}\n"
1215 "@font@tensy cmsy10 scaled @magstephalf\n"
1216 "@font@teni cmmi10 scaled @magstephalf\n"
1217 "@def\\{{@tensy@char110}}\n" ; this backslash goes with cmr10
1218 "@def|{{@tensy@char106}}\n"
1219 "@def@{{{@tensy@char102}}\n"
1220 "@def@}{{@tensy@char103}}\n"
1221 "@def<{{@teni@char62}}\n"
1222 "@def>{{@teni@char60}}\n"
1225 "@tableindent-0.2in\n"
1227 ;; Sort the array by name; within each name, by type (functions first).
1228 (setq alist
(sort alist
(lambda (e1 e2
)
1229 (if (string-equal (car e1
) (car e2
))
1230 (<= (cadr e1
) (cadr e2
))
1231 (string-lessp (car e1
) (car e2
))))))
1232 ;; Print each function.
1235 (if (char-equal (cadr e
) ?\F
) "Function" "Variable")
1236 " @code{" (car e
) "}\n@display\n"
1239 ;; Try to avoid a save size overflow in the TeX output routine.
1240 (if (zerop (setq i
(%
(1+ i
) 100)))
1241 (insert "\n@end table\n@table @asis\n")))
1242 (insert "@end table\n"
1244 (setq buffer-undo-list nil
)
1249 ;;; help-fns.el ends here