1 ;;; help-fns.el --- Complex help functions -*- lexical-binding: t -*-
3 ;; Copyright (C) 1985-1986, 1993-1994, 1998-2017 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
39 (defvar help-fns-describe-function-functions nil
40 "List of functions to run in help buffer in `describe-function'.
41 Those functions will be run after the header line and argument
42 list was inserted, and before the documentation will be inserted.
43 The functions will receive the function name as argument.")
47 (defvar help-definition-prefixes nil
48 ;; FIXME: We keep `definition-prefixes' as a hash-table so as to
49 ;; avoid pre-loading radix-tree and because it takes slightly less
50 ;; memory. But when we use this table it's more efficient to
51 ;; represent it as a radix tree, since the main operation is to do
52 ;; `radix-tree-prefixes'. Maybe we should just bite the bullet and
53 ;; use a radix tree for `definition-prefixes' (it's not *that*
55 "Radix-tree representation replacing `definition-prefixes'.")
57 (defun help-definition-prefixes ()
58 "Return the up-to-date radix-tree form of `definition-prefixes'."
59 (when (> (hash-table-count definition-prefixes
) 0)
60 (maphash (lambda (prefix files
)
61 (let ((old (radix-tree-lookup help-definition-prefixes prefix
)))
62 (setq help-definition-prefixes
63 (radix-tree-insert help-definition-prefixes
64 prefix
(append old files
)))))
66 (clrhash definition-prefixes
))
67 help-definition-prefixes
)
69 (defun help--loaded-p (file)
70 "Try and figure out if FILE has already been loaded."
71 (or (let ((feature (intern-soft file
)))
72 (and feature
(featurep feature
)))
73 (let* ((re (load-history-regexp file
))
75 (dolist (x load-history
)
76 (and (car x
) (string-match-p re
(car x
)) (setq done t
)))
79 (defun help--load-prefixes (prefixes)
80 (pcase-dolist (`(,prefix .
,files
) prefixes
)
81 (setq help-definition-prefixes
82 (radix-tree-insert help-definition-prefixes prefix nil
))
84 ;; FIXME: Should we scan help-definition-prefixes to remove
85 ;; other prefixes of the same file?
86 ;; FIXME: this regexp business is not good enough: for file
87 ;; `toto', it will say `toto' is loaded when in reality it was
88 ;; just cedet/semantic/toto that has been loaded.
89 (unless (help--loaded-p file
)
90 (load file
'noerror
'nomessage
)))))
92 (defun help--symbol-completion-table (string pred action
)
93 (let ((prefixes (radix-tree-prefixes (help-definition-prefixes) string
)))
94 (help--load-prefixes prefixes
))
95 (let ((prefix-completions
96 (mapcar #'intern
(all-completions string definition-prefixes
))))
97 (complete-with-action action obarray string
98 (if pred
(lambda (sym)
99 (or (funcall pred sym
)
100 (memq sym prefix-completions
)))))))
102 (defvar describe-function-orig-buffer nil
103 "Buffer that was current when `describe-function' was invoked.
104 Functions on `help-fns-describe-function-functions' can use this
105 to get buffer-local values.")
108 (defun describe-function (function)
109 "Display the full documentation of FUNCTION (a symbol).
110 When called from lisp, FUNCTION may also be a function object."
112 (let* ((fn (function-called-at-point))
113 (enable-recursive-minibuffers t
)
114 (val (completing-read
116 (format "Describe function (default %s): " fn
)
117 "Describe function: ")
118 #'help--symbol-completion-table
119 (lambda (f) (or (fboundp f
) (get f
'function-documentation
)))
121 (and fn
(symbol-name fn
)))))
122 (unless (equal val
"")
123 (setq fn
(intern val
)))
124 (unless (and fn
(symbolp fn
))
125 (user-error "You didn't specify a function symbol"))
126 (unless (or (fboundp fn
) (get fn
'function-documentation
))
127 (user-error "Symbol's function definition is void: %s" fn
))
130 ;; We save describe-function-orig-buffer on the help xref stack, so
131 ;; it is restored by the back/forward buttons. 'help-buffer'
132 ;; expects (current-buffer) to be a help buffer when processing
133 ;; those buttons, so we can't change the current buffer before
135 (let ((describe-function-orig-buffer
136 (or describe-function-orig-buffer
140 (list (lambda (function buffer
)
141 (let ((describe-function-orig-buffer
142 (if (buffer-live-p buffer
) buffer
)))
143 (describe-function function
)))
144 function describe-function-orig-buffer
)
145 (called-interactively-p 'interactive
))
148 (with-help-window (help-buffer)
149 (if (get function
'reader-construct
)
152 ;; Use " is " instead of a colon so that
153 ;; it is easier to get out the function name using forward-sexp.
155 (describe-function-1 function
)
156 (with-current-buffer standard-output
157 ;; Return the text we displayed.
162 ;; Could be this, if we make symbol-file do the work below.
163 ;; (defun help-C-file-name (subr-or-var kind)
164 ;; "Return the name of the C file where SUBR-OR-VAR is defined.
165 ;; KIND should be `var' for a variable or `subr' for a subroutine."
166 ;; (symbol-file (if (symbolp subr-or-var) subr-or-var
167 ;; (subr-name subr-or-var))
168 ;; (if (eq kind 'var) 'defvar 'defun)))
170 (defun help-C-file-name (subr-or-var kind
)
171 "Return the name of the C file where SUBR-OR-VAR is defined.
172 KIND should be `var' for a variable or `subr' for a subroutine."
173 (let ((docbuf (get-buffer-create " *DOC*"))
174 (name (if (eq 'var kind
)
175 (concat "V" (symbol-name subr-or-var
))
176 (concat "F" (subr-name (advice--cd*r subr-or-var
))))))
177 (with-current-buffer docbuf
178 (goto-char (point-min))
180 (insert-file-contents-literally
181 (expand-file-name internal-doc-file-name doc-directory
)))
182 (let ((file (catch 'loop
184 (let ((pnt (search-forward (concat "\x1f" name
"\n"))))
185 (re-search-backward "\x1fS\\(.*\\)")
186 (let ((file (match-string 1)))
187 (if (member file build-files
)
189 (goto-char pnt
))))))))
190 (if (string-match "^ns.*\\(\\.o\\|obj\\)\\'" file
)
191 (setq file
(replace-match ".m" t t file
1))
192 (if (string-match "\\.\\(o\\|obj\\)\\'" file
)
193 (setq file
(replace-match ".c" t t file
))))
194 (if (string-match "\\.\\(c\\|m\\)\\'" file
)
198 (defcustom help-downcase-arguments nil
199 "If non-nil, argument names in *Help* buffers are downcased."
204 (defun help-highlight-arg (arg)
205 "Highlight ARG as an argument name for a *Help* buffer.
206 Return ARG in face `help-argument-name'; ARG is also downcased
207 if the variable `help-downcase-arguments' is non-nil."
208 (propertize (if help-downcase-arguments
(downcase arg
) arg
)
209 'face
'help-argument-name
))
211 (defun help-do-arg-highlight (doc args
)
212 (with-syntax-table (make-syntax-table emacs-lisp-mode-syntax-table
)
213 (modify-syntax-entry ?\-
"w")
215 (setq doc
(replace-regexp-in-string
216 ;; This is heuristic, but covers all common cases
218 (concat "\\<" ; beginning of word
219 "\\(?:[a-z-]*-\\)?" ; for xxx-ARG
223 "\\(?:es\\|s\\|th\\)?" ; for ARGth, ARGs
224 "\\(?:-[a-z0-9-]+\\)?" ; for ARG-xxx, ARG-n
225 "\\(?:-[{([<`\"‘].*?\\)?"; for ARG-{x}, (x), <x>, [x], `x', ‘x’
227 (help-highlight-arg arg
)
231 (defun help-highlight-arguments (usage doc
&rest args
)
232 (when (and usage
(string-match "^(" usage
))
235 (goto-char (point-min))
236 (let ((case-fold-search nil
)
237 (next (not (or args
(looking-at "\\["))))
239 ;; Make a list of all arguments
240 (skip-chars-forward "^ ")
242 (or opt
(not (looking-at " &")) (setq opt t
))
243 (if (not (re-search-forward " \\([\\[(]*\\)\\([^] &).]+\\)" nil t
))
245 (setq args
(cons (match-string 2) args
))
246 (when (and opt
(string= (match-string 1) "("))
247 ;; A pesky CL-style optional argument with default value,
248 ;; so let's skip over it
249 (search-backward "(")
250 (goto-char (scan-sexps (point) 1)))))
251 ;; Highlight arguments in the USAGE string
252 (setq usage
(help-do-arg-highlight (buffer-string) args
))
253 ;; Highlight arguments in the DOC string
254 (setq doc
(and doc
(help-do-arg-highlight doc args
))))))
255 ;; Return value is like the one from help-split-fundoc, but highlighted
258 ;; The following function was compiled from the former functions
259 ;; `describe-simplify-lib-file-name' and `find-source-lisp-file' with
260 ;; some excerpts from `describe-function-1' and `describe-variable'.
261 ;; The only additional twists provided are (1) locate the defining file
262 ;; for autoloaded functions, and (2) give preference to files in the
263 ;; "install directory" (directories found via `load-path') rather than
264 ;; to files in the "compile directory" (directories found by searching
265 ;; the loaddefs.el file). We autoload it because it's also used by
266 ;; `describe-face' (instead of `describe-simplify-lib-file-name').
269 (defun find-lisp-object-file-name (object type
)
270 "Guess the file that defined the Lisp object OBJECT, of type TYPE.
271 OBJECT should be a symbol associated with a function, variable, or face;
272 alternatively, it can be a function definition.
273 If TYPE is `defvar', search for a variable definition.
274 If TYPE is `defface', search for a face definition.
275 If TYPE is not a symbol, search for a function definition.
277 The return value is the absolute name of a readable file where OBJECT is
278 defined. If several such files exist, preference is given to a file
279 found via `load-path'. The return value can also be `C-source', which
280 means that OBJECT is a function or variable defined in C. If no
281 suitable file is found, return nil."
282 (let* ((autoloaded (autoloadp type
))
283 (file-name (or (and autoloaded
(nth 1 type
))
285 ;; FIXME: Why do we have this weird "If TYPE is the
286 ;; value returned by `symbol-function' for a function
287 ;; symbol" exception?
288 object
(or (if (symbolp type
) type
) 'defun
)))))
291 ;; An autoloaded function: Locate the file since `symbol-function'
292 ;; has only returned a bare string here.
294 (locate-file file-name load-path
'(".el" ".elc") 'readable
)))
295 ((and (stringp file-name
)
296 (string-match "[.]*loaddefs.el\\'" file-name
))
297 ;; An autoloaded variable or face. Visit loaddefs.el in a buffer
298 ;; and try to extract the defining file. The following form is
299 ;; from `describe-function-1' and `describe-variable'.
302 (find-function-search-for-symbol object nil file-name
)
305 (with-current-buffer (car location
)
306 (goto-char (cdr location
))
307 (when (re-search-backward
308 "^;;; Generated autoloads from \\(.*\\)" nil t
)
311 (file-name-sans-extension
312 (match-string-no-properties 1))
313 load-path
'(".el" ".elc") 'readable
))))))))
316 ((and (not file-name
) (subrp type
))
317 ;; A built-in function. The form is from `describe-function-1'.
318 (if (get-buffer " *DOC*")
319 (help-C-file-name type
'subr
)
321 ((and (not file-name
) (symbolp object
)
322 (integerp (get object
'variable-documentation
)))
323 ;; A variable defined in C. The form is from `describe-variable'.
324 (if (get-buffer " *DOC*")
325 (help-C-file-name object
'var
)
327 ((not (stringp file-name
))
328 ;; If we don't have a file-name string by now, we lost.
330 ;; Now, `file-name' should have become an absolute file name.
331 ;; For files loaded from ~/.foo.elc, try ~/.foo.
332 ;; This applies to config files like ~/.emacs,
333 ;; which people sometimes compile.
335 (and (string-match "\\`\\..*\\.elc\\'"
336 (file-name-nondirectory file-name
))
337 (string-equal (file-name-directory file-name
)
338 (file-name-as-directory (expand-file-name "~")))
339 (file-readable-p (setq fn
(file-name-sans-extension file-name
)))
341 ;; When the Elisp source file can be found in the install
342 ;; directory, return the name of that file.
344 (if (string-match "[.]elc\\'" file-name
)
345 (substring-no-properties file-name
0 -
1)
347 (or (and (file-readable-p lib-name
) lib-name
)
348 ;; The library might be compressed.
349 (and (file-readable-p (concat lib-name
".gz")) lib-name
))))
350 ((let* ((lib-name (file-name-nondirectory file-name
))
351 ;; The next form is from `describe-simplify-lib-file-name'.
353 ;; Try converting the absolute file name to a library
354 ;; name, convert that back to a file name and see if we
355 ;; get the original one. If so, they are equivalent.
356 (if (equal file-name
(locate-file lib-name load-path
'("")))
357 (if (string-match "[.]elc\\'" lib-name
)
358 (substring-no-properties lib-name
0 -
1)
361 (src-file (locate-library file-name t nil
'readable
)))
362 (and src-file
(file-readable-p src-file
) src-file
))))))
364 (defun help-fns--key-bindings (function)
365 (when (commandp function
)
366 (let ((pt2 (with-current-buffer standard-output
(point)))
367 (remapped (command-remapping function
)))
368 (unless (memq remapped
'(ignore undefined
))
369 (let ((keys (where-is-internal
370 (or remapped function
) overriding-local-map nil nil
))
372 (if (and (eq function
'self-insert-command
)
373 (vectorp (car-safe keys
))
374 (consp (aref (car keys
) 0)))
375 (princ "It is bound to many ordinary text characters.\n")
376 ;; Which non-control non-meta keys run this command?
378 (if (member (event-modifiers (aref key
0)) '(nil (shift)))
379 (push key non-modified-keys
)))
381 (princ "Its keys are remapped to ")
382 (princ (if (symbolp remapped
)
383 (format-message "`%s'" remapped
)
384 "an anonymous command"))
389 "Without this remapping, it would be bound to "
391 ;; If lots of ordinary text characters run this command,
392 ;; don't mention them one by one.
393 (if (< (length non-modified-keys
) 10)
394 (princ (mapconcat #'key-description keys
", "))
395 (dolist (key non-modified-keys
)
396 (setq keys
(delq key keys
)))
399 (princ (mapconcat #'key-description keys
", "))
400 (princ ", and many ordinary text characters"))
401 (princ "many ordinary text characters"))))
402 (when (or remapped keys non-modified-keys
)
406 (with-current-buffer standard-output
407 (fill-region-as-paragraph pt2
(point))
408 (unless (looking-back "\n\n" (- (point) 2))
411 (defun help-fns--compiler-macro (function)
412 (let ((handler (function-get function
'compiler-macro
)))
414 (insert "\nThis function has a compiler macro")
415 (if (symbolp handler
)
417 (insert (format-message " `%s'" handler
))
419 (re-search-backward (substitute-command-keys "`\\([^`']+\\)'")
421 (help-xref-button 1 'help-function handler
)))
422 ;; FIXME: Obsolete since 24.4.
423 (let ((lib (get function
'compiler-macro-file
)))
425 (insert (format-message " in `%s'" lib
))
427 (re-search-backward (substitute-command-keys "`\\([^`']+\\)'")
429 (help-xref-button 1 'help-function-cmacro function lib
)))))
432 (defun help-fns--signature (function doc real-def real-function buffer
)
433 "Insert usage at point and return docstring. With highlighting."
434 (if (keymapp function
)
435 doc
; If definition is a keymap, skip arglist note.
436 (let* ((advertised (gethash real-def advertised-signature-table t
))
437 (arglist (if (listp advertised
)
438 advertised
(help-function-arglist real-def
)))
439 (usage (help-split-fundoc doc function
)))
440 (if usage
(setq doc
(cdr usage
)))
442 ((and usage
(not (listp advertised
))) (car usage
))
444 (help--make-usage-docstring function arglist
))
445 ((stringp arglist
) arglist
)
446 ;; Maybe the arglist is in the docstring of a symbol
447 ;; this one is aliased to.
448 ((let ((fun real-function
))
449 (while (and (symbolp fun
)
450 (setq fun
(symbol-function fun
))
451 (not (setq usage
(help-split-fundoc
456 ((or (stringp real-def
)
458 (format "\nMacro: %s"
459 (help--docstring-quote
460 (format-kbd-macro real-def
))))
461 (t "[Missing arglist. Please make a bug report.]")))
462 ;; Insert "`X", not "(\` X)", when documenting `X.
463 (use1 (replace-regexp-in-string
464 "\\`(\\\\=\\\\\\\\=` \\([^\n ]*\\))\\'"
467 (let (subst-use1 subst-doc
)
468 (with-current-buffer buffer
469 (setq subst-use1
(substitute-command-keys use1
))
470 (setq subst-doc
(substitute-command-keys doc
)))
471 (help-highlight-arguments subst-use1 subst-doc
))
473 (let ((fill-begin (point))
474 (high-usage (car high
))
475 (high-doc (cdr high
)))
476 (unless (and (symbolp function
)
477 (get function
'reader-construct
))
478 (insert high-usage
"\n"))
479 (fill-region fill-begin
(point))
482 (defun help-fns--parent-mode (function)
483 ;; If this is a derived mode, link to the parent.
484 (let ((parent-mode (and (symbolp function
)
486 'derived-mode-parent
))))
488 (insert (substitute-command-keys "\nParent mode: `"))
490 (insert (format "%s" parent-mode
))
491 (make-text-button beg
(point)
493 'help-args
(list parent-mode
)))
494 (insert (substitute-command-keys "'.\n")))))
496 (defun help-fns--obsolete (function)
497 ;; Ignore lambda constructs, keyboard macros, etc.
498 (let* ((obsolete (and (symbolp function
)
499 (get function
'byte-obsolete-info
)))
500 (use (car obsolete
)))
503 (if (eq (car-safe (symbol-function function
)) 'macro
)
507 (when (nth 2 obsolete
)
508 (insert (format " since %s" (nth 2 obsolete
))))
509 (insert (cond ((stringp use
) (concat ";\n" use
))
510 (use (format-message ";\nuse `%s' instead." use
))
514 ;; We could use `symbol-file' but this is a wee bit more efficient.
515 (defun help-fns--autoloaded-p (function file
)
516 "Return non-nil if FUNCTION has previously been autoloaded.
517 FILE is the file where FUNCTION was probably defined."
518 (let* ((file (file-name-sans-extension (file-truename file
)))
519 (load-hist load-history
)
520 (target (cons t function
))
522 (while (and load-hist
(not found
))
523 (and (caar load-hist
)
524 (equal (file-name-sans-extension (caar load-hist
)) file
)
525 (setq found
(member target
(cdar load-hist
))))
526 (setq load-hist
(cdr load-hist
)))
529 (defun help-fns--interactive-only (function)
530 "Insert some help blurb if FUNCTION should only be used interactively."
531 ;; Ignore lambda constructs, keyboard macros, etc.
532 (and (symbolp function
)
533 (not (eq (car-safe (symbol-function function
)) 'macro
))
534 (let* ((interactive-only
535 (or (get function
'interactive-only
)
536 (if (boundp 'byte-compile-interactive-only-functions
)
538 byte-compile-interactive-only-functions
)))))
539 (when interactive-only
540 (insert "\nThis function is for interactive use only"
541 ;; Cf byte-compile-form.
542 (cond ((stringp interactive-only
)
543 (format ";\nin Lisp code %s" interactive-only
))
544 ((and (symbolp 'interactive-only
)
545 (not (eq interactive-only t
)))
546 (format-message ";\nin Lisp code use `%s' instead."
551 (defun help-fns-short-filename (filename)
552 (let* ((abbrev (abbreviate-file-name filename
))
554 (dolist (dir load-path
)
555 (let ((rel (file-relative-name filename dir
)))
556 (if (< (length rel
) (length short
))
558 (let ((rel (file-relative-name abbrev dir
)))
559 (if (< (length rel
) (length short
))
564 (defun describe-function-1 (function)
565 (let* ((advised (and (symbolp function
)
567 (advice--p (advice--symbol-function function
))))
568 ;; If the function is advised, use the symbol that has the
569 ;; real definition, if that symbol is already set up.
572 (advice--cd*r
(advice--symbol-function function
)))
574 ;; Get the real definition, if any.
575 (def (if (symbolp real-function
)
576 (cond ((symbol-function real-function
))
577 ((get real-function
'function-documentation
)
579 (t (signal 'void-function
(list real-function
))))
583 ;; Advised & aliased function.
584 (and advised
(symbolp real-function
)
585 (not (eq 'autoload
(car-safe def
))))
587 (not (string= (subr-name def
)
588 (symbol-name function
)))))))
590 ((and aliased
(not (subrp def
)))
591 (let ((f real-function
))
592 (while (and (fboundp f
)
593 (symbolp (symbol-function f
)))
594 (setq f
(symbol-function f
)))
596 ((subrp def
) (intern (subr-name def
)))
598 (sig-key (if (subrp def
)
599 (indirect-function real-def
)
601 (file-name (find-lisp-object-file-name function
(if aliased
'defun
603 (pt1 (with-current-buffer (help-buffer) (point)))
604 (beg (if (and (or (byte-code-function-p def
)
606 (memq (car-safe def
) '(macro lambda closure
)))
608 (help-fns--autoloaded-p function file-name
))
610 "an interactive autoloaded "
612 (if (commandp def
) "an interactive " "a "))))
614 ;; Print what kind of function-like object FUNCTION is.
615 (princ (cond ((or (stringp def
) (vectorp def
))
617 ((and (symbolp function
)
618 (get function
'reader-construct
))
619 "a reader construct")
620 ;; Aliases are Lisp functions, so we need to check
621 ;; aliases before functions.
623 (format-message "an alias for `%s'" real-def
))
625 (if (eq 'unevalled
(cdr (subr-arity def
)))
626 (concat beg
"special form")
627 (concat beg
"built-in function")))
629 (format "%s autoloaded %s"
630 (if (commandp def
) "an interactive" "an")
631 (if (eq (nth 4 def
) 'keymap
) "keymap"
632 (if (nth 4 def
) "Lisp macro" "Lisp function"))))
633 ((or (eq (car-safe def
) 'macro
)
634 ;; For advised macros, def is a lambda
635 ;; expression or a byte-code-function-p, so we
636 ;; need to check macros before functions.
638 (concat beg
"Lisp macro"))
639 ((byte-code-function-p def
)
640 (concat beg
"compiled Lisp function"))
641 ((eq (car-safe def
) 'lambda
)
642 (concat beg
"Lisp function"))
643 ((eq (car-safe def
) 'closure
)
644 (concat beg
"Lisp closure"))
647 (elts (cdr-safe def
)))
649 (if (char-table-p (car-safe elts
))
652 (setq elts
(cdr-safe elts
)))
653 (concat beg
(if is-full
"keymap" "sparse keymap"))))
656 (if (and aliased
(not (fboundp real-def
)))
657 (princ ",\nwhich is not defined. Please make a bug report.")
658 (with-current-buffer standard-output
661 (when (re-search-backward (substitute-command-keys
662 "alias for `\\([^`']+\\)'")
664 (help-xref-button 1 'help-function real-def
)))))
667 ;; We used to add .el to the file name,
668 ;; but that's completely wrong when the user used load-file.
669 (princ (format-message " in `%s'"
670 (if (eq file-name
'C-source
)
672 (help-fns-short-filename file-name
))))
673 ;; Make a hyperlink to the library.
674 (with-current-buffer standard-output
676 (re-search-backward (substitute-command-keys "`\\([^`']+\\)'")
678 (help-xref-button 1 'help-function-def function file-name
))))
680 (with-current-buffer (help-buffer)
681 (fill-region-as-paragraph (save-excursion (goto-char pt1
) (forward-line 0) (point))
685 (let ((doc-raw (documentation function t
))
686 (key-bindings-buffer (current-buffer)))
688 ;; If the function is autoloaded, and its docstring has
689 ;; key substitution constructs, load the library.
690 (and (autoloadp real-def
) doc-raw
691 help-enable-auto-load
692 (string-match "\\([^\\]=\\|[^=]\\|\\`\\)\\\\[[{<]" doc-raw
)
693 (autoload-do-load real-def
))
695 (help-fns--key-bindings function
)
696 (with-current-buffer standard-output
697 (let ((doc (help-fns--signature function doc-raw sig-key
698 real-function key-bindings-buffer
)))
699 (run-hook-with-args 'help-fns-describe-function-functions function
)
701 (or doc
"Not documented."))
702 ;; Avoid asking the user annoying questions if she decides
703 ;; to save the help buffer, when her locale's codeset
705 (unless (memq text-quoting-style
'(straight grave
))
706 (set-buffer-file-coding-system 'utf-8
))))))))
708 ;; Add defaults to `help-fns-describe-function-functions'.
709 (add-hook 'help-fns-describe-function-functions
#'help-fns--obsolete
)
710 (add-hook 'help-fns-describe-function-functions
#'help-fns--interactive-only
)
711 (add-hook 'help-fns-describe-function-functions
#'help-fns--parent-mode
)
712 (add-hook 'help-fns-describe-function-functions
#'help-fns--compiler-macro
)
718 (defun variable-at-point (&optional any-symbol
)
719 "Return the bound variable symbol found at or before point.
720 Return 0 if there is no such symbol.
721 If ANY-SYMBOL is non-nil, don't insist the symbol be bound."
722 (with-syntax-table emacs-lisp-mode-syntax-table
723 (or (condition-case ()
725 (skip-chars-forward "'")
726 (or (not (zerop (skip-syntax-backward "_w")))
727 (eq (char-syntax (following-char)) ?w
)
728 (eq (char-syntax (following-char)) ?_
)
730 (skip-chars-forward "'")
731 (let ((obj (read (current-buffer))))
732 (and (symbolp obj
) (boundp obj
) obj
)))
734 (let* ((str (find-tag-default))
735 (sym (if str
(intern-soft str
))))
736 (if (and sym
(or any-symbol
(boundp sym
)))
739 (when (and str
(string-match "\\`\\W*\\(.*?\\)\\W*\\'" str
))
740 (setq sym
(intern-soft (match-string 1 str
)))
741 (and (or any-symbol
(boundp sym
)) sym
)))))
744 (defun describe-variable-custom-version-info (variable)
745 (let ((custom-version (get variable
'custom-version
))
746 (cpv (get variable
'custom-package-version
))
750 (format "This variable was introduced, or its default value was changed, in\nversion %s of Emacs.\n"
753 (let* ((package (car-safe cpv
))
754 (version (if (listp (cdr-safe cpv
))
757 (pkg-versions (assq package customize-package-emacs-version-alist
))
758 (emacsv (cdr (assoc version pkg-versions
))))
759 (if (and package version
)
761 (format (concat "This variable was introduced, or its default value was changed, in\nversion %s of the %s package"
763 (format " that is part of Emacs %s" emacsv
))
765 version package
))))))
769 (defun describe-variable (variable &optional buffer frame
)
770 "Display the full documentation of VARIABLE (a symbol).
771 Returns the documentation as a string, also.
772 If VARIABLE has a buffer-local value in BUFFER or FRAME
773 \(default to the current buffer and current frame),
774 it is displayed along with the global value."
776 (let ((v (variable-at-point))
777 (enable-recursive-minibuffers t
)
778 (orig-buffer (current-buffer))
780 (setq val
(completing-read
783 "Describe variable (default %s): " v
)
784 "Describe variable: ")
785 #'help--symbol-completion-table
787 ;; In case the variable only exists in the buffer
788 ;; the command we switch back to that buffer before
789 ;; we examine the variable.
790 (with-current-buffer orig-buffer
791 (or (get vv
'variable-documentation
)
792 (and (boundp vv
) (not (keywordp vv
))))))
794 (if (symbolp v
) (symbol-name v
))))
795 (list (if (equal val
"")
798 (unless (buffer-live-p buffer
) (setq buffer
(current-buffer)))
799 (unless (frame-live-p frame
) (setq frame
(selected-frame)))
800 (if (not (symbolp variable
))
801 (message "You did not specify a variable")
803 (let ((valvoid (not (with-current-buffer buffer
(boundp variable
))))
804 (permanent-local (get variable
'permanent-local
))
805 val val-start-pos locus
)
806 ;; Extract the value before setting up the output buffer,
807 ;; in case `buffer' *is* the output buffer.
809 (with-selected-frame frame
810 (with-current-buffer buffer
811 (setq val
(symbol-value variable
)
812 locus
(variable-binding-locus variable
)))))
813 (help-setup-xref (list #'describe-variable variable buffer
)
814 (called-interactively-p 'interactive
))
815 (with-help-window (help-buffer)
816 (with-current-buffer buffer
818 (setq file-name
(find-lisp-object-file-name variable
'defvar
))
822 (princ (format-message
823 " is a variable defined in `%s'.\n"
824 (if (eq file-name
'C-source
)
826 (file-name-nondirectory file-name
))))
827 (with-current-buffer standard-output
829 (re-search-backward (substitute-command-keys
832 (help-xref-button 1 'help-variable-def
833 variable file-name
)))
835 (princ "It is void as a variable.")
838 (princ " is void as a variable.")
839 (princ (substitute-command-keys "'s ")))))
841 (with-current-buffer standard-output
842 (setq val-start-pos
(point))
844 (let ((line-beg (line-beginning-position))
847 (let ((print-quoted t
)
849 (cl-prin1-to-string val
))))
850 (if (and (symbolp val
) (not (booleanp val
)))
851 (format-message "`%s'" rep
)
853 (if (< (+ (length print-rep
) (point) (- line-beg
)) 68)
854 (insert " " print-rep
)
856 (let ((buf (current-buffer)))
860 (let ((pp-buffer (current-buffer)))
861 (with-current-buffer buf
862 (insert-buffer-substring pp-buffer
)))))
863 ;; Remove trailing newline.
864 (and (= (char-before) ?
\n) (delete-char -
1)))
865 (let* ((sv (get variable
'standard-value
))
866 (origval (and (consp sv
)
869 (error :help-eval-error
))))
871 (when (and (consp sv
)
872 (not (equal origval val
))
873 (not (equal origval
:help-eval-error
)))
874 (princ "\nOriginal value was \n")
877 (if (< (point) (+ from
20))
878 (delete-region (1- from
) from
)))))))
883 (princ (format "Local in buffer %s; "
884 (buffer-name buffer
))))
885 ((terminal-live-p locus
)
886 (princ (format "It is a terminal-local variable; ")))
888 (princ (format "It is local to %S" locus
))))
889 (if (not (default-boundp variable
))
890 (princ "globally void")
891 (let ((global-val (default-value variable
)))
892 (with-current-buffer standard-output
893 (princ "global value is ")
894 (if (eq val global-val
)
897 ;; Fixme: pp can take an age if you happen to
898 ;; ask for a very large expression. We should
899 ;; probably print it raw once and check it's a
900 ;; sensible size before prettyprinting. -- fx
901 (let ((from (point)))
903 ;; See previous comment for this function.
904 ;; (help-xref-on-pp from (point))
905 (if (< (point) (+ from
20))
906 (delete-region (1- from
) from
)))))))
909 ;; If the value is large, move it to the end.
910 (with-current-buffer standard-output
911 (when (> (count-lines (point-min) (point-max)) 10)
912 ;; Note that setting the syntax table like below
913 ;; makes forward-sexp move over a `'s' at the end
915 (set-syntax-table emacs-lisp-mode-syntax-table
)
916 (goto-char val-start-pos
)
917 ;; The line below previously read as
918 ;; (delete-region (point) (progn (end-of-line) (point)))
919 ;; which suppressed display of the buffer local value for
921 (when (looking-at "value is") (replace-match ""))
923 (insert "\n\nValue:")
924 (set (make-local-variable 'help-button-cache
)
926 (insert "value is shown ")
927 (insert-button "below"
928 'action help-button-cache
930 'help-echo
"mouse-2, RET: show value")
934 (let* ((alias (condition-case nil
935 (indirect-variable variable
)
937 (obsolete (get variable
'byte-obsolete-variable
))
938 (watchpoints (get-variable-watchers variable
))
940 (safe-var (get variable
'safe-local-variable
))
941 (doc (or (documentation-property
942 variable
'variable-documentation
)
943 (documentation-property
944 alias
'variable-documentation
)))
947 ;; Mention if it's a local variable.
949 ((and (local-variable-if-set-p variable
)
950 (or (not (local-variable-p variable
))
952 (local-variable-if-set-p variable
))))
954 (princ " Automatically becomes ")
956 (princ "permanently "))
957 (princ "buffer-local when set.\n"))
958 ((not permanent-local
))
962 (substitute-command-keys
963 " This variable's buffer-local value is permanent.\n")))
966 (princ (substitute-command-keys
967 " This variable's value is permanent \
968 if it is given a local binding.\n"))))
970 ;; Mention if it's an alias.
971 (unless (eq alias variable
)
973 (princ (format-message
974 " This variable is an alias for `%s'.\n"
979 (princ " This variable is obsolete")
981 (princ (format " since %s" (nth 2 obsolete
))))
982 (princ (cond ((stringp use
) (concat ";\n " use
))
983 (use (format-message ";\n use `%s' instead."
990 (princ " Calls these functions when changed: ")
994 (when (member (cons variable val
)
995 (with-current-buffer buffer
996 file-local-variables-alist
))
998 (if (member (cons variable val
)
999 (with-current-buffer buffer
1000 dir-local-variables-alist
))
1001 (let ((file (and (buffer-file-name buffer
)
1003 (buffer-file-name buffer
)))
1004 (dir-locals-find-file
1005 (buffer-file-name buffer
))))
1007 (princ (substitute-command-keys
1008 " This variable's value is directory-local"))
1009 (when (consp file
) ; result from cache
1010 ;; If the cache element has an mtime, we
1011 ;; assume it came from a file.
1013 ;; (car file) is a directory.
1014 (setq file
(dir-locals--all-files (car file
)))
1015 ;; Otherwise, assume it was set directly.
1016 (setq file
(car file
)
1021 (princ (substitute-command-keys
1023 (is-directory "for the directory\n `")
1024 ;; Many files matched.
1025 ((and (consp file
) (cdr file
))
1026 (setq file
(file-name-directory (car file
)))
1027 (format "by one of the\n %s files in the directory\n `"
1029 (t (setq file
(car file
))
1030 "by the file\n `"))))
1031 (with-current-buffer standard-output
1033 file
'type
'help-dir-local-var-def
1034 'help-args
(list variable file
)))
1035 (princ (substitute-command-keys "'.\n"))))
1036 (princ (substitute-command-keys
1037 " This variable's value is file-local.\n"))))
1039 (when (memq variable ignored-local-variables
)
1041 (princ " This variable is ignored as a file-local \
1044 ;; Can be both risky and safe, eg auto-fill-function.
1045 (when (risky-local-variable-p variable
)
1047 (princ " This variable may be risky if used as a \
1048 file-local variable.\n")
1049 (when (assq variable safe-local-variable-values
)
1050 (princ (substitute-command-keys
1051 " However, you have added it to \
1052 `safe-local-variable-values'.\n"))))
1056 (princ " This variable is safe as a file local variable ")
1057 (princ "if its value\n satisfies the predicate ")
1058 (princ (if (byte-code-function-p safe-var
)
1059 "which is a byte-compiled expression.\n"
1060 (format-message "`%s'.\n" safe-var
))))
1062 (if extra-line
(terpri))
1063 (princ "Documentation:\n")
1064 (with-current-buffer standard-output
1065 (insert (or doc
"Not documented as a variable."))))
1067 ;; Make a link to customize if this variable can be customized.
1068 (when (custom-variable-p variable
)
1069 (let ((customize-label "customize"))
1072 (princ (concat "You can " customize-label
" this variable."))
1073 (with-current-buffer standard-output
1076 (concat "\\(" customize-label
"\\)") nil t
)
1077 (help-xref-button 1 'help-customize-variable variable
))))
1078 ;; Note variable's version or package version.
1079 (let ((output (describe-variable-custom-version-info variable
)))
1085 (with-current-buffer standard-output
1086 ;; Return the text we displayed.
1087 (buffer-string))))))))
1090 (defvar help-xref-stack-item
)
1093 (defun describe-symbol (symbol &optional buffer frame
)
1094 "Display the full documentation of SYMBOL.
1095 Will show the info of SYMBOL as a function, variable, and/or face.
1096 Optional arguments BUFFER and FRAME specify for which buffer and
1097 frame to show the information about SYMBOL; they default to the
1098 current buffer and the selected frame, respectively."
1100 (let* ((v-or-f (symbol-at-point))
1101 (found (if v-or-f
(cl-some (lambda (x) (funcall (nth 1 x
) v-or-f
))
1102 describe-symbol-backends
)))
1103 (v-or-f (if found v-or-f
(function-called-at-point)))
1104 (found (or found v-or-f
))
1105 (enable-recursive-minibuffers t
)
1106 (val (completing-read (if found
1108 "Describe symbol (default %s): " v-or-f
)
1109 "Describe symbol: ")
1112 (cl-some (lambda (x) (funcall (nth 1 x
) vv
))
1113 describe-symbol-backends
))
1115 (if found
(symbol-name v-or-f
)))))
1116 (list (if (equal val
"")
1117 v-or-f
(intern val
)))))
1118 (if (not (symbolp symbol
))
1119 (user-error "You didn't specify a function or variable"))
1120 (unless (buffer-live-p buffer
) (setq buffer
(current-buffer)))
1121 (unless (frame-live-p frame
) (setq frame
(selected-frame)))
1122 (with-current-buffer (help-buffer)
1123 ;; Push the previous item on the stack before clobbering the output buffer.
1124 (help-setup-xref nil nil
)
1128 (mapcar (pcase-lambda (`(,name
,testfn
,descfn
))
1129 (when (funcall testfn symbol
)
1130 ;; Don't record the current entry in the stack.
1131 (setq help-xref-stack-item nil
)
1133 (funcall descfn symbol buffer frame
))))
1134 describe-symbol-backends
))))
1135 (single (null (cdr docs
))))
1137 (goto-char (point-min))
1138 (let ((inhibit-read-only t
)
1139 (name (caar docs
)) ;Name of doc currently at BOB.
1140 (doc (cdr (cadr docs
)))) ;Doc to add at BOB.
1143 (delete-region (point)
1144 (progn (skip-chars-backward " \t\n") (point)))
1147 (propertize "\n" 'face
'(:height
0.1 :inverse-video t
)))
1150 (insert (symbol-name symbol
)
1151 " is also a " name
"." "\n\n"))))
1152 (setq docs
(cdr docs
)))
1154 ;; Don't record the `describe-variable' item in the stack.
1155 (setq help-xref-stack-item nil
)
1156 (help-setup-xref (list #'describe-symbol symbol
) nil
))
1157 (goto-char (point-min)))))
1160 (defun describe-syntax (&optional buffer
)
1161 "Describe the syntax specifications in the syntax table of BUFFER.
1162 The descriptions are inserted in a help buffer, which is then displayed.
1163 BUFFER defaults to the current buffer."
1165 (setq buffer
(or buffer
(current-buffer)))
1166 (help-setup-xref (list #'describe-syntax buffer
)
1167 (called-interactively-p 'interactive
))
1168 (with-help-window (help-buffer)
1169 (let ((table (with-current-buffer buffer
(syntax-table))))
1170 (with-current-buffer standard-output
1171 (describe-vector table
'internal-describe-syntax-value
)
1172 (while (setq table
(char-table-parent table
))
1173 (insert "\nThe parent syntax table is:")
1174 (describe-vector table
'internal-describe-syntax-value
))))))
1176 (defun help-describe-category-set (value)
1178 ((null value
) "default")
1179 ((char-table-p value
) "deeper char-table ...")
1180 (t (condition-case nil
1181 (category-set-mnemonics value
)
1182 (error "invalid"))))))
1185 (defun describe-categories (&optional buffer
)
1186 "Describe the category specifications in the current category table.
1187 The descriptions are inserted in a buffer, which is then displayed.
1188 If BUFFER is non-nil, then describe BUFFER's category table instead.
1189 BUFFER should be a buffer or a buffer name."
1191 (setq buffer
(or buffer
(current-buffer)))
1192 (help-setup-xref (list #'describe-categories buffer
)
1193 (called-interactively-p 'interactive
))
1194 (with-help-window (help-buffer)
1195 (let* ((table (with-current-buffer buffer
(category-table)))
1196 (docs (char-table-extra-slot table
0)))
1197 (if (or (not (vectorp docs
)) (/= (length docs
) 95))
1198 (error "Invalid first extra slot in this category table\n"))
1199 (with-current-buffer standard-output
1200 (setq-default help-button-cache
(make-marker))
1201 (insert "Legend of category mnemonics ")
1202 (insert-button "(longer descriptions at the bottom)"
1203 'action help-button-cache
1205 'help-echo
"mouse-2, RET: show full legend")
1207 (let ((pos (point)) (items 0) lines n
)
1209 (if (aref docs i
) (setq items
(1+ items
))))
1210 (setq lines
(1+ (/ (1- items
) 4)))
1213 (let ((elt (aref docs i
)))
1215 (string-match ".*" elt
)
1216 (setq elt
(match-string 0 elt
))
1217 (if (>= (length elt
) 17)
1218 (setq elt
(concat (substring elt
0 14) "...")))
1219 (if (< (point) (point-max))
1220 (move-to-column (* 20 (/ n lines
)) t
))
1221 (insert (+ i ?\s
) ?
: elt
)
1222 (if (< (point) (point-max))
1226 (if (= (% n lines
) 0)
1227 (goto-char pos
))))))
1228 (goto-char (point-max))
1230 "character(s)\tcategory mnemonics\n"
1231 "------------\t------------------")
1232 (describe-vector table
'help-describe-category-set
)
1233 (set-marker help-button-cache
(point))
1234 (insert "Legend of category mnemonics:\n")
1236 (let ((elt (aref docs i
)))
1238 (if (string-match "\n" elt
)
1239 (setq elt
(substring elt
(match-end 0))))
1240 (insert (+ i ?\s
) ": " elt
"\n"))))
1241 (while (setq table
(char-table-parent table
))
1242 (insert "\nThe parent category table is:")
1243 (describe-vector table
'help-describe-category-set
))))))
1246 ;;; Replacements for old lib-src/ programs. Don't seem especially useful.
1248 ;; Replaces lib-src/digest-doc.c.
1250 (defun doc-file-to-man (file)
1251 "Produce an nroff buffer containing the doc-strings from the DOC file."
1252 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1253 internal-doc-file-name t
)))
1254 (or (file-readable-p file
)
1255 (error "Cannot read file `%s'" file
))
1256 (pop-to-buffer (generate-new-buffer "*man-doc*"))
1257 (setq buffer-undo-list t
)
1258 (insert ".TH \"Command Summary for GNU Emacs\"\n"
1259 ".AU Richard M. Stallman\n")
1260 (insert-file-contents file
)
1262 (while (search-forward "\x1f" nil
'move
)
1263 (if (looking-at "S")
1264 (delete-region (1- (point)) (line-end-position))
1270 (insert (if (looking-at "F") "Function " "Variable "))
1273 (insert ".DS L\n"))))
1275 (setq buffer-undo-list nil
)
1278 ;; Replaces lib-src/sorted-doc.c.
1280 (defun doc-file-to-info (file)
1281 "Produce a texinfo buffer with sorted doc-strings from the DOC file."
1282 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1283 internal-doc-file-name t
)))
1284 (or (file-readable-p file
)
1285 (error "Cannot read file `%s'" file
))
1286 (let ((i 0) type name doc alist
)
1288 (insert-file-contents file
)
1289 ;; The characters "@{}" need special treatment.
1290 (while (re-search-forward "[@{}]" nil t
)
1294 (goto-char (point-min))
1295 (while (search-forward "\x1f" nil t
)
1296 (unless (looking-at "S")
1297 (setq type
(char-after)
1298 name
(buffer-substring (1+ (point)) (line-end-position))
1299 doc
(buffer-substring (line-beginning-position 2)
1300 (if (search-forward "\x1f" nil
'move
)
1303 alist
(cons (list name type doc
) alist
))
1304 (backward-char 1))))
1305 (pop-to-buffer (generate-new-buffer "*info-doc*"))
1306 (setq buffer-undo-list t
)
1307 ;; Write the output header.
1308 (insert "\\input texinfo @c -*-texinfo-*-\n"
1309 "@setfilename emacsdoc.info\n"
1310 "@settitle Command Summary for GNU Emacs\n"
1313 "@unnumbered Command Summary for GNU Emacs\n\n"
1316 "@global@let@ITEM@item\n"
1317 "@def@item{@filbreak@vskip5pt@ITEM}\n"
1318 "@font@tensy cmsy10 scaled @magstephalf\n"
1319 "@font@teni cmmi10 scaled @magstephalf\n"
1320 "@def\\{{@tensy@char110}}\n" ; this backslash goes with cmr10
1321 "@def|{{@tensy@char106}}\n"
1322 "@def@{{{@tensy@char102}}\n"
1323 "@def@}{{@tensy@char103}}\n"
1324 "@def<{{@teni@char62}}\n"
1325 "@def>{{@teni@char60}}\n"
1328 "@tableindent-0.2in\n"
1330 ;; Sort the array by name; within each name, by type (functions first).
1331 (setq alist
(sort alist
(lambda (e1 e2
)
1332 (if (string-equal (car e1
) (car e2
))
1333 (<= (cadr e1
) (cadr e2
))
1334 (string-lessp (car e1
) (car e2
))))))
1335 ;; Print each function.
1338 (if (char-equal (cadr e
) ?\F
) "Function" "Variable")
1339 " @code{" (car e
) "}\n@display\n"
1342 ;; Try to avoid a save size overflow in the TeX output routine.
1343 (if (zerop (setq i
(%
(1+ i
) 100)))
1344 (insert "\n@end table\n@table @asis\n")))
1345 (insert "@end table\n"
1347 (setq buffer-undo-list nil
)
1352 ;;; help-fns.el ends here