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
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 (if (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
#'fboundp t nil nil
119 (and fn
(symbol-name fn
)))))
120 (unless (equal val
"")
121 (setq fn
(intern val
)))
122 (unless (and fn
(symbolp fn
))
123 (user-error "You didn't specify a function symbol"))
125 (user-error "Symbol's function definition is void: %s" fn
))
128 ;; We save describe-function-orig-buffer on the help xref stack, so
129 ;; it is restored by the back/forward buttons. 'help-buffer'
130 ;; expects (current-buffer) to be a help buffer when processing
131 ;; those buttons, so we can't change the current buffer before
133 (let ((describe-function-orig-buffer
134 (or describe-function-orig-buffer
138 (list (lambda (function buffer
)
139 (let ((describe-function-orig-buffer
140 (if (buffer-live-p buffer
) buffer
)))
141 (describe-function function
)))
142 function describe-function-orig-buffer
)
143 (called-interactively-p 'interactive
))
146 (with-help-window (help-buffer)
148 ;; Use " is " instead of a colon so that
149 ;; it is easier to get out the function name using forward-sexp.
151 (describe-function-1 function
)
152 (with-current-buffer standard-output
153 ;; Return the text we displayed.
158 ;; Could be this, if we make symbol-file do the work below.
159 ;; (defun help-C-file-name (subr-or-var kind)
160 ;; "Return the name of the C file where SUBR-OR-VAR is defined.
161 ;; KIND should be `var' for a variable or `subr' for a subroutine."
162 ;; (symbol-file (if (symbolp subr-or-var) subr-or-var
163 ;; (subr-name subr-or-var))
164 ;; (if (eq kind 'var) 'defvar 'defun)))
166 (defun help-C-file-name (subr-or-var kind
)
167 "Return the name of the C file where SUBR-OR-VAR is defined.
168 KIND should be `var' for a variable or `subr' for a subroutine."
169 (let ((docbuf (get-buffer-create " *DOC*"))
170 (name (if (eq 'var kind
)
171 (concat "V" (symbol-name subr-or-var
))
172 (concat "F" (subr-name (advice--cd*r subr-or-var
))))))
173 (with-current-buffer docbuf
174 (goto-char (point-min))
176 (insert-file-contents-literally
177 (expand-file-name internal-doc-file-name doc-directory
)))
178 (let ((file (catch 'loop
180 (let ((pnt (search-forward (concat "\x1f" name
"\n"))))
181 (re-search-backward "\x1fS\\(.*\\)")
182 (let ((file (match-string 1)))
183 (if (member file build-files
)
185 (goto-char pnt
))))))))
186 (if (string-match "^ns.*\\(\\.o\\|obj\\)\\'" file
)
187 (setq file
(replace-match ".m" t t file
1))
188 (if (string-match "\\.\\(o\\|obj\\)\\'" file
)
189 (setq file
(replace-match ".c" t t file
))))
190 (if (string-match "\\.\\(c\\|m\\)\\'" file
)
194 (defcustom help-downcase-arguments nil
195 "If non-nil, argument names in *Help* buffers are downcased."
200 (defun help-highlight-arg (arg)
201 "Highlight ARG as an argument name for a *Help* buffer.
202 Return ARG in face `help-argument-name'; ARG is also downcased
203 if the variable `help-downcase-arguments' is non-nil."
204 (propertize (if help-downcase-arguments
(downcase arg
) arg
)
205 'face
'help-argument-name
))
207 (defun help-do-arg-highlight (doc args
)
208 (with-syntax-table (make-syntax-table emacs-lisp-mode-syntax-table
)
209 (modify-syntax-entry ?\-
"w")
211 (setq doc
(replace-regexp-in-string
212 ;; This is heuristic, but covers all common cases
214 (concat "\\<" ; beginning of word
215 "\\(?:[a-z-]*-\\)?" ; for xxx-ARG
219 "\\(?:es\\|s\\|th\\)?" ; for ARGth, ARGs
220 "\\(?:-[a-z0-9-]+\\)?" ; for ARG-xxx, ARG-n
221 "\\(?:-[{([<`\"‘].*?\\)?"; for ARG-{x}, (x), <x>, [x], `x', ‘x’
223 (help-highlight-arg arg
)
227 (defun help-highlight-arguments (usage doc
&rest args
)
228 (when (and usage
(string-match "^(" usage
))
231 (goto-char (point-min))
232 (let ((case-fold-search nil
)
233 (next (not (or args
(looking-at "\\["))))
235 ;; Make a list of all arguments
236 (skip-chars-forward "^ ")
238 (or opt
(not (looking-at " &")) (setq opt t
))
239 (if (not (re-search-forward " \\([\\[(]*\\)\\([^] &).]+\\)" nil t
))
241 (setq args
(cons (match-string 2) args
))
242 (when (and opt
(string= (match-string 1) "("))
243 ;; A pesky CL-style optional argument with default value,
244 ;; so let's skip over it
245 (search-backward "(")
246 (goto-char (scan-sexps (point) 1)))))
247 ;; Highlight arguments in the USAGE string
248 (setq usage
(help-do-arg-highlight (buffer-string) args
))
249 ;; Highlight arguments in the DOC string
250 (setq doc
(and doc
(help-do-arg-highlight doc args
))))))
251 ;; Return value is like the one from help-split-fundoc, but highlighted
254 ;; The following function was compiled from the former functions
255 ;; `describe-simplify-lib-file-name' and `find-source-lisp-file' with
256 ;; some excerpts from `describe-function-1' and `describe-variable'.
257 ;; The only additional twists provided are (1) locate the defining file
258 ;; for autoloaded functions, and (2) give preference to files in the
259 ;; "install directory" (directories found via `load-path') rather than
260 ;; to files in the "compile directory" (directories found by searching
261 ;; the loaddefs.el file). We autoload it because it's also used by
262 ;; `describe-face' (instead of `describe-simplify-lib-file-name').
265 (defun find-lisp-object-file-name (object type
)
266 "Guess the file that defined the Lisp object OBJECT, of type TYPE.
267 OBJECT should be a symbol associated with a function, variable, or face;
268 alternatively, it can be a function definition.
269 If TYPE is `defvar', search for a variable definition.
270 If TYPE is `defface', search for a face definition.
271 If TYPE is not a symbol, search for a function definition.
273 The return value is the absolute name of a readable file where OBJECT is
274 defined. If several such files exist, preference is given to a file
275 found via `load-path'. The return value can also be `C-source', which
276 means that OBJECT is a function or variable defined in C. If no
277 suitable file is found, return nil."
278 (let* ((autoloaded (autoloadp type
))
279 (file-name (or (and autoloaded
(nth 1 type
))
281 ;; FIXME: Why do we have this weird "If TYPE is the
282 ;; value returned by `symbol-function' for a function
283 ;; symbol" exception?
284 object
(or (if (symbolp type
) type
) 'defun
)))))
287 ;; An autoloaded function: Locate the file since `symbol-function'
288 ;; has only returned a bare string here.
290 (locate-file file-name load-path
'(".el" ".elc") 'readable
)))
291 ((and (stringp file-name
)
292 (string-match "[.]*loaddefs.el\\'" file-name
))
293 ;; An autoloaded variable or face. Visit loaddefs.el in a buffer
294 ;; and try to extract the defining file. The following form is
295 ;; from `describe-function-1' and `describe-variable'.
298 (find-function-search-for-symbol object nil file-name
)
301 (with-current-buffer (car location
)
302 (goto-char (cdr location
))
303 (when (re-search-backward
304 "^;;; Generated autoloads from \\(.*\\)" nil t
)
307 (file-name-sans-extension
308 (match-string-no-properties 1))
309 load-path
'(".el" ".elc") 'readable
))))))))
312 ((and (not file-name
) (subrp type
))
313 ;; A built-in function. The form is from `describe-function-1'.
314 (if (get-buffer " *DOC*")
315 (help-C-file-name type
'subr
)
317 ((and (not file-name
) (symbolp object
)
318 (integerp (get object
'variable-documentation
)))
319 ;; A variable defined in C. The form is from `describe-variable'.
320 (if (get-buffer " *DOC*")
321 (help-C-file-name object
'var
)
323 ((not (stringp file-name
))
324 ;; If we don't have a file-name string by now, we lost.
326 ;; Now, `file-name' should have become an absolute file name.
327 ;; For files loaded from ~/.foo.elc, try ~/.foo.
328 ;; This applies to config files like ~/.emacs,
329 ;; which people sometimes compile.
331 (and (string-match "\\`\\..*\\.elc\\'"
332 (file-name-nondirectory file-name
))
333 (string-equal (file-name-directory file-name
)
334 (file-name-as-directory (expand-file-name "~")))
335 (file-readable-p (setq fn
(file-name-sans-extension file-name
)))
337 ;; When the Elisp source file can be found in the install
338 ;; directory, return the name of that file.
340 (if (string-match "[.]elc\\'" file-name
)
341 (substring-no-properties file-name
0 -
1)
343 (or (and (file-readable-p lib-name
) lib-name
)
344 ;; The library might be compressed.
345 (and (file-readable-p (concat lib-name
".gz")) lib-name
))))
346 ((let* ((lib-name (file-name-nondirectory file-name
))
347 ;; The next form is from `describe-simplify-lib-file-name'.
349 ;; Try converting the absolute file name to a library
350 ;; name, convert that back to a file name and see if we
351 ;; get the original one. If so, they are equivalent.
352 (if (equal file-name
(locate-file lib-name load-path
'("")))
353 (if (string-match "[.]elc\\'" lib-name
)
354 (substring-no-properties lib-name
0 -
1)
357 (src-file (locate-library file-name t nil
'readable
)))
358 (and src-file
(file-readable-p src-file
) src-file
))))))
360 (defun help-fns--key-bindings (function)
361 (when (commandp function
)
362 (let ((pt2 (with-current-buffer standard-output
(point)))
363 (remapped (command-remapping function
)))
364 (unless (memq remapped
'(ignore undefined
))
365 (let ((keys (where-is-internal
366 (or remapped function
) overriding-local-map nil nil
))
368 (if (and (eq function
'self-insert-command
)
369 (vectorp (car-safe keys
))
370 (consp (aref (car keys
) 0)))
371 (princ "It is bound to many ordinary text characters.\n")
372 ;; Which non-control non-meta keys run this command?
374 (if (member (event-modifiers (aref key
0)) '(nil (shift)))
375 (push key non-modified-keys
)))
377 (princ "Its keys are remapped to ")
378 (princ (if (symbolp remapped
)
379 (format-message "`%s'" remapped
)
380 "an anonymous command"))
385 "Without this remapping, it would be bound to "
387 ;; If lots of ordinary text characters run this command,
388 ;; don't mention them one by one.
389 (if (< (length non-modified-keys
) 10)
390 (princ (mapconcat 'key-description keys
", "))
391 (dolist (key non-modified-keys
)
392 (setq keys
(delq key keys
)))
395 (princ (mapconcat 'key-description keys
", "))
396 (princ ", and many ordinary text characters"))
397 (princ "many ordinary text characters"))))
398 (when (or remapped keys non-modified-keys
)
402 (with-current-buffer standard-output
403 (fill-region-as-paragraph pt2
(point))
404 (unless (looking-back "\n\n" (- (point) 2))
407 (defun help-fns--compiler-macro (function)
408 (let ((handler (function-get function
'compiler-macro
)))
410 (insert "\nThis function has a compiler macro")
411 (if (symbolp handler
)
413 (insert (format-message " `%s'" handler
))
415 (re-search-backward (substitute-command-keys "`\\([^`']+\\)'")
417 (help-xref-button 1 'help-function handler
)))
418 ;; FIXME: Obsolete since 24.4.
419 (let ((lib (get function
'compiler-macro-file
)))
421 (insert (format-message " in `%s'" lib
))
423 (re-search-backward (substitute-command-keys "`\\([^`']+\\)'")
425 (help-xref-button 1 'help-function-cmacro function lib
)))))
428 (defun help-fns--signature (function doc real-def real-function buffer
)
429 "Insert usage at point and return docstring. With highlighting."
430 (if (keymapp function
)
431 doc
; If definition is a keymap, skip arglist note.
432 (let* ((advertised (gethash real-def advertised-signature-table t
))
433 (arglist (if (listp advertised
)
434 advertised
(help-function-arglist real-def
)))
435 (usage (help-split-fundoc doc function
)))
436 (if usage
(setq doc
(cdr usage
)))
438 ((and usage
(not (listp advertised
))) (car usage
))
440 (help--make-usage-docstring function arglist
))
441 ((stringp arglist
) arglist
)
442 ;; Maybe the arglist is in the docstring of a symbol
443 ;; this one is aliased to.
444 ((let ((fun real-function
))
445 (while (and (symbolp fun
)
446 (setq fun
(symbol-function fun
))
447 (not (setq usage
(help-split-fundoc
452 ((or (stringp real-def
)
454 (format "\nMacro: %s"
455 (help--docstring-quote
456 (format-kbd-macro real-def
))))
457 (t "[Missing arglist. Please make a bug report.]")))
458 ;; Insert "`X", not "(\` X)", when documenting `X.
459 (use1 (replace-regexp-in-string
460 "\\`(\\\\=\\\\\\\\=` \\([^\n ]*\\))\\'"
463 (let (subst-use1 subst-doc
)
464 (with-current-buffer buffer
465 (setq subst-use1
(substitute-command-keys use1
))
466 (setq subst-doc
(substitute-command-keys doc
)))
467 (help-highlight-arguments subst-use1 subst-doc
))
469 (let ((fill-begin (point))
470 (high-usage (car high
))
471 (high-doc (cdr high
)))
472 (insert high-usage
"\n")
473 (fill-region fill-begin
(point))
476 (defun help-fns--parent-mode (function)
477 ;; If this is a derived mode, link to the parent.
478 (let ((parent-mode (and (symbolp function
)
480 'derived-mode-parent
))))
482 (insert (substitute-command-keys "\nParent mode: `"))
484 (insert (format "%s" parent-mode
))
485 (make-text-button beg
(point)
487 'help-args
(list parent-mode
)))
488 (insert (substitute-command-keys "'.\n")))))
490 (defun help-fns--obsolete (function)
491 ;; Ignore lambda constructs, keyboard macros, etc.
492 (let* ((obsolete (and (symbolp function
)
493 (get function
'byte-obsolete-info
)))
494 (use (car obsolete
)))
497 (if (eq (car-safe (symbol-function function
)) 'macro
)
501 (when (nth 2 obsolete
)
502 (insert (format " since %s" (nth 2 obsolete
))))
503 (insert (cond ((stringp use
) (concat ";\n" use
))
504 (use (format-message ";\nuse `%s' instead." use
))
508 ;; We could use `symbol-file' but this is a wee bit more efficient.
509 (defun help-fns--autoloaded-p (function file
)
510 "Return non-nil if FUNCTION has previously been autoloaded.
511 FILE is the file where FUNCTION was probably defined."
512 (let* ((file (file-name-sans-extension (file-truename file
)))
513 (load-hist load-history
)
514 (target (cons t function
))
516 (while (and load-hist
(not found
))
517 (and (caar load-hist
)
518 (equal (file-name-sans-extension (caar load-hist
)) file
)
519 (setq found
(member target
(cdar load-hist
))))
520 (setq load-hist
(cdr load-hist
)))
523 (defun help-fns--interactive-only (function)
524 "Insert some help blurb if FUNCTION should only be used interactively."
525 ;; Ignore lambda constructs, keyboard macros, etc.
526 (and (symbolp function
)
527 (not (eq (car-safe (symbol-function function
)) 'macro
))
528 (let* ((interactive-only
529 (or (get function
'interactive-only
)
530 (if (boundp 'byte-compile-interactive-only-functions
)
532 byte-compile-interactive-only-functions
)))))
533 (when interactive-only
534 (insert "\nThis function is for interactive use only"
535 ;; Cf byte-compile-form.
536 (cond ((stringp interactive-only
)
537 (format ";\nin Lisp code %s" interactive-only
))
538 ((and (symbolp 'interactive-only
)
539 (not (eq interactive-only t
)))
540 (format-message ";\nin Lisp code use `%s' instead."
545 (defun help-fns-short-filename (filename)
546 (let* ((abbrev (abbreviate-file-name filename
))
548 (dolist (dir load-path
)
549 (let ((rel (file-relative-name filename dir
)))
550 (if (< (length rel
) (length short
))
552 (let ((rel (file-relative-name abbrev dir
)))
553 (if (< (length rel
) (length short
))
558 (defun describe-function-1 (function)
559 (let* ((advised (and (symbolp function
)
561 (advice--p (advice--symbol-function function
))))
562 ;; If the function is advised, use the symbol that has the
563 ;; real definition, if that symbol is already set up.
566 (advice--cd*r
(advice--symbol-function function
)))
568 ;; Get the real definition.
569 (def (if (symbolp real-function
)
570 (or (symbol-function real-function
)
571 (signal 'void-function
(list real-function
)))
573 (aliased (or (symbolp def
)
574 ;; Advised & aliased function.
575 (and advised
(symbolp real-function
)
576 (not (eq 'autoload
(car-safe def
))))
578 (not (string= (subr-name def
)
579 (symbol-name function
))))))
581 ((and aliased
(not (subrp def
)))
582 (let ((f real-function
))
583 (while (and (fboundp f
)
584 (symbolp (symbol-function f
)))
585 (setq f
(symbol-function f
)))
587 ((subrp def
) (intern (subr-name def
)))
589 (sig-key (if (subrp def
)
590 (indirect-function real-def
)
592 (file-name (find-lisp-object-file-name function
(if aliased
'defun
594 (pt1 (with-current-buffer (help-buffer) (point)))
595 (beg (if (and (or (byte-code-function-p def
)
597 (memq (car-safe def
) '(macro lambda closure
)))
599 (help-fns--autoloaded-p function file-name
))
601 "an interactive autoloaded "
603 (if (commandp def
) "an interactive " "a "))))
605 ;; Print what kind of function-like object FUNCTION is.
606 (princ (cond ((or (stringp def
) (vectorp def
))
608 ;; Aliases are Lisp functions, so we need to check
609 ;; aliases before functions.
611 (format-message "an alias for `%s'" real-def
))
613 (if (eq 'unevalled
(cdr (subr-arity def
)))
614 (concat beg
"special form")
615 (concat beg
"built-in function")))
617 (format "%s autoloaded %s"
618 (if (commandp def
) "an interactive" "an")
619 (if (eq (nth 4 def
) 'keymap
) "keymap"
620 (if (nth 4 def
) "Lisp macro" "Lisp function"))))
621 ((or (eq (car-safe def
) 'macro
)
622 ;; For advised macros, def is a lambda
623 ;; expression or a byte-code-function-p, so we
624 ;; need to check macros before functions.
626 (concat beg
"Lisp macro"))
627 ((byte-code-function-p def
)
628 (concat beg
"compiled Lisp function"))
629 ((eq (car-safe def
) 'lambda
)
630 (concat beg
"Lisp function"))
631 ((eq (car-safe def
) 'closure
)
632 (concat beg
"Lisp closure"))
635 (elts (cdr-safe def
)))
637 (if (char-table-p (car-safe elts
))
640 (setq elts
(cdr-safe elts
)))
641 (concat beg
(if is-full
"keymap" "sparse keymap"))))
644 (if (and aliased
(not (fboundp real-def
)))
645 (princ ",\nwhich is not defined. Please make a bug report.")
646 (with-current-buffer standard-output
649 (when (re-search-backward (substitute-command-keys
650 "alias for `\\([^`']+\\)'")
652 (help-xref-button 1 'help-function real-def
)))))
655 ;; We used to add .el to the file name,
656 ;; but that's completely wrong when the user used load-file.
657 (princ (format-message " in `%s'"
658 (if (eq file-name
'C-source
)
660 (help-fns-short-filename file-name
))))
661 ;; Make a hyperlink to the library.
662 (with-current-buffer standard-output
664 (re-search-backward (substitute-command-keys "`\\([^`']+\\)'")
666 (help-xref-button 1 'help-function-def function file-name
))))
668 (with-current-buffer (help-buffer)
669 (fill-region-as-paragraph (save-excursion (goto-char pt1
) (forward-line 0) (point))
673 (let ((doc-raw (documentation function t
))
674 (key-bindings-buffer (current-buffer)))
676 ;; If the function is autoloaded, and its docstring has
677 ;; key substitution constructs, load the library.
678 (and (autoloadp real-def
) doc-raw
679 help-enable-auto-load
680 (string-match "\\([^\\]=\\|[^=]\\|\\`\\)\\\\[[{<]" doc-raw
)
681 (autoload-do-load real-def
))
683 (help-fns--key-bindings function
)
684 (with-current-buffer standard-output
685 (let ((doc (help-fns--signature function doc-raw sig-key
686 real-function key-bindings-buffer
)))
687 (run-hook-with-args 'help-fns-describe-function-functions function
)
689 (or doc
"Not documented."))
690 ;; Avoid asking the user annoying questions if she decides
691 ;; to save the help buffer, when her locale's codeset
693 (unless (memq text-quoting-style
'(straight grave
))
694 (set-buffer-file-coding-system 'utf-8
))))))))
696 ;; Add defaults to `help-fns-describe-function-functions'.
697 (add-hook 'help-fns-describe-function-functions
#'help-fns--obsolete
)
698 (add-hook 'help-fns-describe-function-functions
#'help-fns--interactive-only
)
699 (add-hook 'help-fns-describe-function-functions
#'help-fns--parent-mode
)
700 (add-hook 'help-fns-describe-function-functions
#'help-fns--compiler-macro
)
706 (defun variable-at-point (&optional any-symbol
)
707 "Return the bound variable symbol found at or before point.
708 Return 0 if there is no such symbol.
709 If ANY-SYMBOL is non-nil, don't insist the symbol be bound."
710 (with-syntax-table emacs-lisp-mode-syntax-table
711 (or (condition-case ()
713 (skip-chars-forward "'")
714 (or (not (zerop (skip-syntax-backward "_w")))
715 (eq (char-syntax (following-char)) ?w
)
716 (eq (char-syntax (following-char)) ?_
)
718 (skip-chars-forward "'")
719 (let ((obj (read (current-buffer))))
720 (and (symbolp obj
) (boundp obj
) obj
)))
722 (let* ((str (find-tag-default))
723 (sym (if str
(intern-soft str
))))
724 (if (and sym
(or any-symbol
(boundp sym
)))
727 (when (and str
(string-match "\\`\\W*\\(.*?\\)\\W*\\'" str
))
728 (setq sym
(intern-soft (match-string 1 str
)))
729 (and (or any-symbol
(boundp sym
)) sym
)))))
732 (defun describe-variable-custom-version-info (variable)
733 (let ((custom-version (get variable
'custom-version
))
734 (cpv (get variable
'custom-package-version
))
738 (format "This variable was introduced, or its default value was changed, in\nversion %s of Emacs.\n"
741 (let* ((package (car-safe cpv
))
742 (version (if (listp (cdr-safe cpv
))
745 (pkg-versions (assq package customize-package-emacs-version-alist
))
746 (emacsv (cdr (assoc version pkg-versions
))))
747 (if (and package version
)
749 (format (concat "This variable was introduced, or its default value was changed, in\nversion %s of the %s package"
751 (format " that is part of Emacs %s" emacsv
))
753 version package
))))))
757 (defun describe-variable (variable &optional buffer frame
)
758 "Display the full documentation of VARIABLE (a symbol).
759 Returns the documentation as a string, also.
760 If VARIABLE has a buffer-local value in BUFFER or FRAME
761 \(default to the current buffer and current frame),
762 it is displayed along with the global value."
764 (let ((v (variable-at-point))
765 (enable-recursive-minibuffers t
)
766 (orig-buffer (current-buffer))
768 (setq val
(completing-read
771 "Describe variable (default %s): " v
)
772 "Describe variable: ")
773 #'help--symbol-completion-table
775 ;; In case the variable only exists in the buffer
776 ;; the command we switch back to that buffer before
777 ;; we examine the variable.
778 (with-current-buffer orig-buffer
779 (or (get vv
'variable-documentation
)
780 (and (boundp vv
) (not (keywordp vv
))))))
782 (if (symbolp v
) (symbol-name v
))))
783 (list (if (equal val
"")
786 (unless (buffer-live-p buffer
) (setq buffer
(current-buffer)))
787 (unless (frame-live-p frame
) (setq frame
(selected-frame)))
788 (if (not (symbolp variable
))
789 (message "You did not specify a variable")
791 (let ((valvoid (not (with-current-buffer buffer
(boundp variable
))))
792 (permanent-local (get variable
'permanent-local
))
793 val val-start-pos locus
)
794 ;; Extract the value before setting up the output buffer,
795 ;; in case `buffer' *is* the output buffer.
797 (with-selected-frame frame
798 (with-current-buffer buffer
799 (setq val
(symbol-value variable
)
800 locus
(variable-binding-locus variable
)))))
801 (help-setup-xref (list #'describe-variable variable buffer
)
802 (called-interactively-p 'interactive
))
803 (with-help-window (help-buffer)
804 (with-current-buffer buffer
806 (setq file-name
(find-lisp-object-file-name variable
'defvar
))
810 (princ (format-message
811 " is a variable defined in `%s'.\n"
812 (if (eq file-name
'C-source
)
814 (file-name-nondirectory file-name
))))
815 (with-current-buffer standard-output
817 (re-search-backward (substitute-command-keys
820 (help-xref-button 1 'help-variable-def
821 variable file-name
)))
823 (princ "It is void as a variable.")
826 (princ " is void as a variable.")
827 (princ (substitute-command-keys "'s ")))))
829 (with-current-buffer standard-output
830 (setq val-start-pos
(point))
832 (let ((line-beg (line-beginning-position))
835 (let ((print-quoted t
))
836 (prin1-to-string val
))))
837 (if (and (symbolp val
) (not (booleanp val
)))
838 (format-message "`%s'" rep
)
840 (if (< (+ (length print-rep
) (point) (- line-beg
)) 68)
841 (insert " " print-rep
)
844 ;; Remove trailing newline.
846 (let* ((sv (get variable
'standard-value
))
847 (origval (and (consp sv
)
850 (error :help-eval-error
))))
852 (when (and (consp sv
)
853 (not (equal origval val
))
854 (not (equal origval
:help-eval-error
)))
855 (princ "\nOriginal value was \n")
858 (if (< (point) (+ from
20))
859 (delete-region (1- from
) from
)))))))
864 (princ (format "Local in buffer %s; "
865 (buffer-name buffer
))))
867 (princ (format "It is a frame-local variable; ")))
868 ((terminal-live-p locus
)
869 (princ (format "It is a terminal-local variable; ")))
871 (princ (format "It is local to %S" locus
))))
872 (if (not (default-boundp variable
))
873 (princ "globally void")
874 (let ((global-val (default-value variable
)))
875 (with-current-buffer standard-output
876 (princ "global value is ")
877 (if (eq val global-val
)
880 ;; Fixme: pp can take an age if you happen to
881 ;; ask for a very large expression. We should
882 ;; probably print it raw once and check it's a
883 ;; sensible size before prettyprinting. -- fx
884 (let ((from (point)))
886 ;; See previous comment for this function.
887 ;; (help-xref-on-pp from (point))
888 (if (< (point) (+ from
20))
889 (delete-region (1- from
) from
)))))))
892 ;; If the value is large, move it to the end.
893 (with-current-buffer standard-output
894 (when (> (count-lines (point-min) (point-max)) 10)
895 ;; Note that setting the syntax table like below
896 ;; makes forward-sexp move over a `'s' at the end
898 (set-syntax-table emacs-lisp-mode-syntax-table
)
899 (goto-char val-start-pos
)
900 ;; The line below previously read as
901 ;; (delete-region (point) (progn (end-of-line) (point)))
902 ;; which suppressed display of the buffer local value for
904 (when (looking-at "value is") (replace-match ""))
906 (insert "\n\nValue:")
907 (set (make-local-variable 'help-button-cache
)
909 (insert "value is shown ")
910 (insert-button "below"
911 'action help-button-cache
913 'help-echo
"mouse-2, RET: show value")
917 (let* ((alias (condition-case nil
918 (indirect-variable variable
)
920 (obsolete (get variable
'byte-obsolete-variable
))
922 (safe-var (get variable
'safe-local-variable
))
923 (doc (or (documentation-property
924 variable
'variable-documentation
)
925 (documentation-property
926 alias
'variable-documentation
)))
929 ;; Mention if it's a local variable.
931 ((and (local-variable-if-set-p variable
)
932 (or (not (local-variable-p variable
))
934 (local-variable-if-set-p variable
))))
936 (princ " Automatically becomes ")
938 (princ "permanently "))
939 (princ "buffer-local when set.\n"))
940 ((not permanent-local
))
944 (substitute-command-keys
945 " This variable's buffer-local value is permanent.\n")))
948 (princ (substitute-command-keys
949 " This variable's value is permanent \
950 if it is given a local binding.\n"))))
952 ;; Mention if it's an alias.
953 (unless (eq alias variable
)
955 (princ (format-message
956 " This variable is an alias for `%s'.\n"
961 (princ " This variable is obsolete")
963 (princ (format " since %s" (nth 2 obsolete
))))
964 (princ (cond ((stringp use
) (concat ";\n " use
))
965 (use (format-message ";\n use `%s' instead."
970 (when (member (cons variable val
)
971 (with-current-buffer buffer
972 file-local-variables-alist
))
974 (if (member (cons variable val
)
975 (with-current-buffer buffer
976 dir-local-variables-alist
))
977 (let ((file (and (buffer-file-name buffer
)
979 (buffer-file-name buffer
)))
980 (dir-locals-find-file
981 (buffer-file-name buffer
))))
983 (princ (substitute-command-keys
984 " This variable's value is directory-local"))
985 (when (consp file
) ; result from cache
986 ;; If the cache element has an mtime, we
987 ;; assume it came from a file.
989 ;; (car file) is a directory.
990 (setq file
(dir-locals--all-files (car file
)))
991 ;; Otherwise, assume it was set directly.
992 (setq file
(car file
)
997 (princ (substitute-command-keys
999 (is-directory "for the directory\n `")
1000 ;; Many files matched.
1001 ((and (consp file
) (cdr file
))
1002 (setq file
(file-name-directory (car file
)))
1003 (format "by one of the\n %s files in the directory\n `"
1005 (t (setq file
(car file
))
1006 "by the file\n `"))))
1007 (with-current-buffer standard-output
1009 file
'type
'help-dir-local-var-def
1010 'help-args
(list variable file
)))
1011 (princ (substitute-command-keys "'.\n"))))
1012 (princ (substitute-command-keys
1013 " This variable's value is file-local.\n"))))
1015 (when (memq variable ignored-local-variables
)
1017 (princ " This variable is ignored as a file-local \
1020 ;; Can be both risky and safe, eg auto-fill-function.
1021 (when (risky-local-variable-p variable
)
1023 (princ " This variable may be risky if used as a \
1024 file-local variable.\n")
1025 (when (assq variable safe-local-variable-values
)
1026 (princ (substitute-command-keys
1027 " However, you have added it to \
1028 `safe-local-variable-values'.\n"))))
1032 (princ " This variable is safe as a file local variable ")
1033 (princ "if its value\n satisfies the predicate ")
1034 (princ (if (byte-code-function-p safe-var
)
1035 "which is a byte-compiled expression.\n"
1036 (format-message "`%s'.\n" safe-var
))))
1038 (if extra-line
(terpri))
1039 (princ "Documentation:\n")
1040 (with-current-buffer standard-output
1041 (insert (or doc
"Not documented as a variable."))))
1043 ;; Make a link to customize if this variable can be customized.
1044 (when (custom-variable-p variable
)
1045 (let ((customize-label "customize"))
1048 (princ (concat "You can " customize-label
" this variable."))
1049 (with-current-buffer standard-output
1052 (concat "\\(" customize-label
"\\)") nil t
)
1053 (help-xref-button 1 'help-customize-variable variable
))))
1054 ;; Note variable's version or package version.
1055 (let ((output (describe-variable-custom-version-info variable
)))
1061 (with-current-buffer standard-output
1062 ;; Return the text we displayed.
1063 (buffer-string))))))))
1066 (defvar help-xref-stack-item
)
1069 (defun describe-symbol (symbol &optional buffer frame
)
1070 "Display the full documentation of SYMBOL.
1071 Will show the info of SYMBOL as a function, variable, and/or face.
1072 Optional arguments BUFFER and FRAME specify for which buffer and
1073 frame to show the information about SYMBOL; they default to the
1074 current buffer and the selected frame, respectively."
1076 (let* ((v-or-f (symbol-at-point))
1077 (found (cl-some (lambda (x) (funcall (nth 1 x
) v-or-f
))
1078 describe-symbol-backends
))
1079 (v-or-f (if found v-or-f
(function-called-at-point)))
1080 (found (or found v-or-f
))
1081 (enable-recursive-minibuffers t
)
1082 (val (completing-read (if found
1084 "Describe symbol (default %s): " v-or-f
)
1085 "Describe symbol: ")
1088 (cl-some (lambda (x) (funcall (nth 1 x
) vv
))
1089 describe-symbol-backends
))
1091 (if found
(symbol-name v-or-f
)))))
1092 (list (if (equal val
"")
1093 v-or-f
(intern val
)))))
1094 (if (not (symbolp symbol
))
1095 (user-error "You didn't specify a function or variable"))
1096 (unless (buffer-live-p buffer
) (setq buffer
(current-buffer)))
1097 (unless (frame-live-p frame
) (setq frame
(selected-frame)))
1098 (with-current-buffer (help-buffer)
1099 ;; Push the previous item on the stack before clobbering the output buffer.
1100 (help-setup-xref nil nil
)
1104 (mapcar (pcase-lambda (`(,name
,testfn
,descfn
))
1105 (when (funcall testfn symbol
)
1106 ;; Don't record the current entry in the stack.
1107 (setq help-xref-stack-item nil
)
1109 (funcall descfn symbol buffer frame
))))
1110 describe-symbol-backends
))))
1111 (single (null (cdr docs
))))
1113 (goto-char (point-min))
1114 (let ((inhibit-read-only t
)
1115 (name (caar docs
)) ;Name of doc currently at BOB.
1116 (doc (cdr (cadr docs
)))) ;Doc to add at BOB.
1119 (delete-region (point)
1120 (progn (skip-chars-backward " \t\n") (point)))
1123 (propertize "\n" 'face
'(:height
0.1 :inverse-video t
)))
1126 (insert (symbol-name symbol
)
1127 " is also a " name
"." "\n\n"))))
1128 (setq docs
(cdr docs
)))
1130 ;; Don't record the `describe-variable' item in the stack.
1131 (setq help-xref-stack-item nil
)
1132 (help-setup-xref (list #'describe-symbol symbol
) nil
))
1133 (goto-char (point-min)))))
1136 (defun describe-syntax (&optional buffer
)
1137 "Describe the syntax specifications in the syntax table of BUFFER.
1138 The descriptions are inserted in a help buffer, which is then displayed.
1139 BUFFER defaults to the current buffer."
1141 (setq buffer
(or buffer
(current-buffer)))
1142 (help-setup-xref (list #'describe-syntax buffer
)
1143 (called-interactively-p 'interactive
))
1144 (with-help-window (help-buffer)
1145 (let ((table (with-current-buffer buffer
(syntax-table))))
1146 (with-current-buffer standard-output
1147 (describe-vector table
'internal-describe-syntax-value
)
1148 (while (setq table
(char-table-parent table
))
1149 (insert "\nThe parent syntax table is:")
1150 (describe-vector table
'internal-describe-syntax-value
))))))
1152 (defun help-describe-category-set (value)
1154 ((null value
) "default")
1155 ((char-table-p value
) "deeper char-table ...")
1156 (t (condition-case nil
1157 (category-set-mnemonics value
)
1158 (error "invalid"))))))
1161 (defun describe-categories (&optional buffer
)
1162 "Describe the category specifications in the current category table.
1163 The descriptions are inserted in a buffer, which is then displayed.
1164 If BUFFER is non-nil, then describe BUFFER's category table instead.
1165 BUFFER should be a buffer or a buffer name."
1167 (setq buffer
(or buffer
(current-buffer)))
1168 (help-setup-xref (list #'describe-categories buffer
)
1169 (called-interactively-p 'interactive
))
1170 (with-help-window (help-buffer)
1171 (let* ((table (with-current-buffer buffer
(category-table)))
1172 (docs (char-table-extra-slot table
0)))
1173 (if (or (not (vectorp docs
)) (/= (length docs
) 95))
1174 (error "Invalid first extra slot in this category table\n"))
1175 (with-current-buffer standard-output
1176 (setq-default help-button-cache
(make-marker))
1177 (insert "Legend of category mnemonics ")
1178 (insert-button "(longer descriptions at the bottom)"
1179 'action help-button-cache
1181 'help-echo
"mouse-2, RET: show full legend")
1183 (let ((pos (point)) (items 0) lines n
)
1185 (if (aref docs i
) (setq items
(1+ items
))))
1186 (setq lines
(1+ (/ (1- items
) 4)))
1189 (let ((elt (aref docs i
)))
1191 (string-match ".*" elt
)
1192 (setq elt
(match-string 0 elt
))
1193 (if (>= (length elt
) 17)
1194 (setq elt
(concat (substring elt
0 14) "...")))
1195 (if (< (point) (point-max))
1196 (move-to-column (* 20 (/ n lines
)) t
))
1197 (insert (+ i ?\s
) ?
: elt
)
1198 (if (< (point) (point-max))
1202 (if (= (% n lines
) 0)
1203 (goto-char pos
))))))
1204 (goto-char (point-max))
1206 "character(s)\tcategory mnemonics\n"
1207 "------------\t------------------")
1208 (describe-vector table
'help-describe-category-set
)
1209 (set-marker help-button-cache
(point))
1210 (insert "Legend of category mnemonics:\n")
1212 (let ((elt (aref docs i
)))
1214 (if (string-match "\n" elt
)
1215 (setq elt
(substring elt
(match-end 0))))
1216 (insert (+ i ?\s
) ": " elt
"\n"))))
1217 (while (setq table
(char-table-parent table
))
1218 (insert "\nThe parent category table is:")
1219 (describe-vector table
'help-describe-category-set
))))))
1222 ;;; Replacements for old lib-src/ programs. Don't seem especially useful.
1224 ;; Replaces lib-src/digest-doc.c.
1226 (defun doc-file-to-man (file)
1227 "Produce an nroff buffer containing the doc-strings from the DOC file."
1228 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1229 internal-doc-file-name t
)))
1230 (or (file-readable-p file
)
1231 (error "Cannot read file `%s'" file
))
1232 (pop-to-buffer (generate-new-buffer "*man-doc*"))
1233 (setq buffer-undo-list t
)
1234 (insert ".TH \"Command Summary for GNU Emacs\"\n"
1235 ".AU Richard M. Stallman\n")
1236 (insert-file-contents file
)
1238 (while (search-forward "\x1f" nil
'move
)
1239 (if (looking-at "S")
1240 (delete-region (1- (point)) (line-end-position))
1246 (insert (if (looking-at "F") "Function " "Variable "))
1249 (insert ".DS L\n"))))
1251 (setq buffer-undo-list nil
)
1254 ;; Replaces lib-src/sorted-doc.c.
1256 (defun doc-file-to-info (file)
1257 "Produce a texinfo buffer with sorted doc-strings from the DOC file."
1258 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1259 internal-doc-file-name t
)))
1260 (or (file-readable-p file
)
1261 (error "Cannot read file `%s'" file
))
1262 (let ((i 0) type name doc alist
)
1264 (insert-file-contents file
)
1265 ;; The characters "@{}" need special treatment.
1266 (while (re-search-forward "[@{}]" nil t
)
1270 (goto-char (point-min))
1271 (while (search-forward "\x1f" nil t
)
1272 (unless (looking-at "S")
1273 (setq type
(char-after)
1274 name
(buffer-substring (1+ (point)) (line-end-position))
1275 doc
(buffer-substring (line-beginning-position 2)
1276 (if (search-forward "\x1f" nil
'move
)
1279 alist
(cons (list name type doc
) alist
))
1280 (backward-char 1))))
1281 (pop-to-buffer (generate-new-buffer "*info-doc*"))
1282 (setq buffer-undo-list t
)
1283 ;; Write the output header.
1284 (insert "\\input texinfo @c -*-texinfo-*-\n"
1285 "@setfilename emacsdoc.info\n"
1286 "@settitle Command Summary for GNU Emacs\n"
1289 "@unnumbered Command Summary for GNU Emacs\n\n"
1292 "@global@let@ITEM@item\n"
1293 "@def@item{@filbreak@vskip5pt@ITEM}\n"
1294 "@font@tensy cmsy10 scaled @magstephalf\n"
1295 "@font@teni cmmi10 scaled @magstephalf\n"
1296 "@def\\{{@tensy@char110}}\n" ; this backslash goes with cmr10
1297 "@def|{{@tensy@char106}}\n"
1298 "@def@{{{@tensy@char102}}\n"
1299 "@def@}{{@tensy@char103}}\n"
1300 "@def<{{@teni@char62}}\n"
1301 "@def>{{@teni@char60}}\n"
1304 "@tableindent-0.2in\n"
1306 ;; Sort the array by name; within each name, by type (functions first).
1307 (setq alist
(sort alist
(lambda (e1 e2
)
1308 (if (string-equal (car e1
) (car e2
))
1309 (<= (cadr e1
) (cadr e2
))
1310 (string-lessp (car e1
) (car e2
))))))
1311 ;; Print each function.
1314 (if (char-equal (cadr e
) ?\F
) "Function" "Variable")
1315 " @code{" (car e
) "}\n@display\n"
1318 ;; Try to avoid a save size overflow in the TeX output routine.
1319 (if (zerop (setq i
(%
(1+ i
) 100)))
1320 (insert "\n@end table\n@table @asis\n")))
1321 (insert "@end table\n"
1323 (setq buffer-undo-list nil
)
1328 ;;; help-fns.el ends here