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)."
111 (let ((fn (function-called-at-point))
112 (enable-recursive-minibuffers t
)
114 (setq val
(completing-read (if fn
115 (format "Describe function (default %s): " fn
)
116 "Describe function: ")
117 #'help--symbol-completion-table
119 t nil nil
(and fn
(symbol-name fn
))))
120 (list (if (equal val
"")
122 (or (and function
(symbolp function
))
123 (user-error "You didn't specify a function symbol"))
124 (or (fboundp function
)
125 (user-error "Symbol's function definition is void: %s" function
))
127 ;; We save describe-function-orig-buffer on the help xref stack, so
128 ;; it is restored by the back/forward buttons. 'help-buffer'
129 ;; expects (current-buffer) to be a help buffer when processing
130 ;; those buttons, so we can't change the current buffer before
132 (let ((describe-function-orig-buffer
133 (or describe-function-orig-buffer
137 (list (lambda (function buffer
)
138 (let ((describe-function-orig-buffer
139 (if (buffer-live-p buffer
) buffer
)))
140 (describe-function function
)))
141 function describe-function-orig-buffer
)
142 (called-interactively-p 'interactive
))
145 (with-help-window (help-buffer)
147 ;; Use " is " instead of a colon so that
148 ;; it is easier to get out the function name using forward-sexp.
150 (describe-function-1 function
)
151 (with-current-buffer standard-output
152 ;; Return the text we displayed.
157 ;; Could be this, if we make symbol-file do the work below.
158 ;; (defun help-C-file-name (subr-or-var kind)
159 ;; "Return the name of the C file where SUBR-OR-VAR is defined.
160 ;; KIND should be `var' for a variable or `subr' for a subroutine."
161 ;; (symbol-file (if (symbolp subr-or-var) subr-or-var
162 ;; (subr-name subr-or-var))
163 ;; (if (eq kind 'var) 'defvar 'defun)))
165 (defun help-C-file-name (subr-or-var kind
)
166 "Return the name of the C file where SUBR-OR-VAR is defined.
167 KIND should be `var' for a variable or `subr' for a subroutine."
168 (let ((docbuf (get-buffer-create " *DOC*"))
169 (name (if (eq 'var kind
)
170 (concat "V" (symbol-name subr-or-var
))
171 (concat "F" (subr-name (advice--cd*r subr-or-var
))))))
172 (with-current-buffer docbuf
173 (goto-char (point-min))
175 (insert-file-contents-literally
176 (expand-file-name internal-doc-file-name doc-directory
)))
177 (let ((file (catch 'loop
179 (let ((pnt (search-forward (concat "\x1f" name
"\n"))))
180 (re-search-backward "\x1fS\\(.*\\)")
181 (let ((file (match-string 1)))
182 (if (member file build-files
)
184 (goto-char pnt
))))))))
185 (if (string-match "^ns.*\\(\\.o\\|obj\\)\\'" file
)
186 (setq file
(replace-match ".m" t t file
1))
187 (if (string-match "\\.\\(o\\|obj\\)\\'" file
)
188 (setq file
(replace-match ".c" t t file
))))
189 (if (string-match "\\.\\(c\\|m\\)\\'" file
)
193 (defcustom help-downcase-arguments nil
194 "If non-nil, argument names in *Help* buffers are downcased."
199 (defun help-highlight-arg (arg)
200 "Highlight ARG as an argument name for a *Help* buffer.
201 Return ARG in face `help-argument-name'; ARG is also downcased
202 if the variable `help-downcase-arguments' is non-nil."
203 (propertize (if help-downcase-arguments
(downcase arg
) arg
)
204 'face
'help-argument-name
))
206 (defun help-do-arg-highlight (doc args
)
207 (with-syntax-table (make-syntax-table emacs-lisp-mode-syntax-table
)
208 (modify-syntax-entry ?\-
"w")
210 (setq doc
(replace-regexp-in-string
211 ;; This is heuristic, but covers all common cases
213 (concat "\\<" ; beginning of word
214 "\\(?:[a-z-]*-\\)?" ; for xxx-ARG
218 "\\(?:es\\|s\\|th\\)?" ; for ARGth, ARGs
219 "\\(?:-[a-z0-9-]+\\)?" ; for ARG-xxx, ARG-n
220 "\\(?:-[{([<`\"‘].*?\\)?"; for ARG-{x}, (x), <x>, [x], `x', ‘x’
222 (help-highlight-arg arg
)
226 (defun help-highlight-arguments (usage doc
&rest args
)
227 (when (and usage
(string-match "^(" usage
))
230 (goto-char (point-min))
231 (let ((case-fold-search nil
)
232 (next (not (or args
(looking-at "\\["))))
234 ;; Make a list of all arguments
235 (skip-chars-forward "^ ")
237 (or opt
(not (looking-at " &")) (setq opt t
))
238 (if (not (re-search-forward " \\([\\[(]*\\)\\([^] &).]+\\)" nil t
))
240 (setq args
(cons (match-string 2) args
))
241 (when (and opt
(string= (match-string 1) "("))
242 ;; A pesky CL-style optional argument with default value,
243 ;; so let's skip over it
244 (search-backward "(")
245 (goto-char (scan-sexps (point) 1)))))
246 ;; Highlight arguments in the USAGE string
247 (setq usage
(help-do-arg-highlight (buffer-string) args
))
248 ;; Highlight arguments in the DOC string
249 (setq doc
(and doc
(help-do-arg-highlight doc args
))))))
250 ;; Return value is like the one from help-split-fundoc, but highlighted
253 ;; The following function was compiled from the former functions
254 ;; `describe-simplify-lib-file-name' and `find-source-lisp-file' with
255 ;; some excerpts from `describe-function-1' and `describe-variable'.
256 ;; The only additional twists provided are (1) locate the defining file
257 ;; for autoloaded functions, and (2) give preference to files in the
258 ;; "install directory" (directories found via `load-path') rather than
259 ;; to files in the "compile directory" (directories found by searching
260 ;; the loaddefs.el file). We autoload it because it's also used by
261 ;; `describe-face' (instead of `describe-simplify-lib-file-name').
264 (defun find-lisp-object-file-name (object type
)
265 "Guess the file that defined the Lisp object OBJECT, of type TYPE.
266 OBJECT should be a symbol associated with a function, variable, or face;
267 alternatively, it can be a function definition.
268 If TYPE is `defvar', search for a variable definition.
269 If TYPE is `defface', search for a face definition.
270 If TYPE is not a symbol, search for a function definition.
272 The return value is the absolute name of a readable file where OBJECT is
273 defined. If several such files exist, preference is given to a file
274 found via `load-path'. The return value can also be `C-source', which
275 means that OBJECT is a function or variable defined in C. If no
276 suitable file is found, return nil."
277 (let* ((autoloaded (autoloadp type
))
278 (file-name (or (and autoloaded
(nth 1 type
))
280 ;; FIXME: Why do we have this weird "If TYPE is the
281 ;; value returned by `symbol-function' for a function
282 ;; symbol" exception?
283 object
(or (if (symbolp type
) type
) 'defun
)))))
286 ;; An autoloaded function: Locate the file since `symbol-function'
287 ;; has only returned a bare string here.
289 (locate-file file-name load-path
'(".el" ".elc") 'readable
)))
290 ((and (stringp file-name
)
291 (string-match "[.]*loaddefs.el\\'" file-name
))
292 ;; An autoloaded variable or face. Visit loaddefs.el in a buffer
293 ;; and try to extract the defining file. The following form is
294 ;; from `describe-function-1' and `describe-variable'.
297 (find-function-search-for-symbol object nil file-name
)
300 (with-current-buffer (car location
)
301 (goto-char (cdr location
))
302 (when (re-search-backward
303 "^;;; Generated autoloads from \\(.*\\)" nil t
)
306 (file-name-sans-extension
307 (match-string-no-properties 1))
308 load-path
'(".el" ".elc") 'readable
))))))))
311 ((and (not file-name
) (subrp type
))
312 ;; A built-in function. The form is from `describe-function-1'.
313 (if (get-buffer " *DOC*")
314 (help-C-file-name type
'subr
)
316 ((and (not file-name
) (symbolp object
)
317 (integerp (get object
'variable-documentation
)))
318 ;; A variable defined in C. The form is from `describe-variable'.
319 (if (get-buffer " *DOC*")
320 (help-C-file-name object
'var
)
322 ((not (stringp file-name
))
323 ;; If we don't have a file-name string by now, we lost.
325 ;; Now, `file-name' should have become an absolute file name.
326 ;; For files loaded from ~/.foo.elc, try ~/.foo.
327 ;; This applies to config files like ~/.emacs,
328 ;; which people sometimes compile.
330 (and (string-match "\\`\\..*\\.elc\\'"
331 (file-name-nondirectory file-name
))
332 (string-equal (file-name-directory file-name
)
333 (file-name-as-directory (expand-file-name "~")))
334 (file-readable-p (setq fn
(file-name-sans-extension file-name
)))
336 ;; When the Elisp source file can be found in the install
337 ;; directory, return the name of that file.
339 (if (string-match "[.]elc\\'" file-name
)
340 (substring-no-properties file-name
0 -
1)
342 (or (and (file-readable-p lib-name
) lib-name
)
343 ;; The library might be compressed.
344 (and (file-readable-p (concat lib-name
".gz")) lib-name
))))
345 ((let* ((lib-name (file-name-nondirectory file-name
))
346 ;; The next form is from `describe-simplify-lib-file-name'.
348 ;; Try converting the absolute file name to a library
349 ;; name, convert that back to a file name and see if we
350 ;; get the original one. If so, they are equivalent.
351 (if (equal file-name
(locate-file lib-name load-path
'("")))
352 (if (string-match "[.]elc\\'" lib-name
)
353 (substring-no-properties lib-name
0 -
1)
356 (src-file (locate-library file-name t nil
'readable
)))
357 (and src-file
(file-readable-p src-file
) src-file
))))))
359 (defun help-fns--key-bindings (function)
360 (when (commandp function
)
361 (let ((pt2 (with-current-buffer standard-output
(point)))
362 (remapped (command-remapping function
)))
363 (unless (memq remapped
'(ignore undefined
))
364 (let ((keys (where-is-internal
365 (or remapped function
) overriding-local-map nil nil
))
367 (if (and (eq function
'self-insert-command
)
368 (vectorp (car-safe keys
))
369 (consp (aref (car keys
) 0)))
370 (princ "It is bound to many ordinary text characters.\n")
371 ;; Which non-control non-meta keys run this command?
373 (if (member (event-modifiers (aref key
0)) '(nil (shift)))
374 (push key non-modified-keys
)))
376 (princ "Its keys are remapped to ")
377 (princ (if (symbolp remapped
)
378 (format-message "`%s'" remapped
)
379 "an anonymous command"))
384 "Without this remapping, it would be bound to "
386 ;; If lots of ordinary text characters run this command,
387 ;; don't mention them one by one.
388 (if (< (length non-modified-keys
) 10)
389 (princ (mapconcat 'key-description keys
", "))
390 (dolist (key non-modified-keys
)
391 (setq keys
(delq key keys
)))
394 (princ (mapconcat 'key-description keys
", "))
395 (princ ", and many ordinary text characters"))
396 (princ "many ordinary text characters"))))
397 (when (or remapped keys non-modified-keys
)
401 (with-current-buffer standard-output
402 (fill-region-as-paragraph pt2
(point))
403 (unless (looking-back "\n\n" (- (point) 2))
406 (defun help-fns--compiler-macro (function)
407 (let ((handler (function-get function
'compiler-macro
)))
409 (insert "\nThis function has a compiler macro")
410 (if (symbolp handler
)
412 (insert (format-message " `%s'" handler
))
414 (re-search-backward (substitute-command-keys "`\\([^`']+\\)'")
416 (help-xref-button 1 'help-function handler
)))
417 ;; FIXME: Obsolete since 24.4.
418 (let ((lib (get function
'compiler-macro-file
)))
420 (insert (format-message " in `%s'" lib
))
422 (re-search-backward (substitute-command-keys "`\\([^`']+\\)'")
424 (help-xref-button 1 'help-function-cmacro function lib
)))))
427 (defun help-fns--signature (function doc real-def real-function buffer
)
428 "Insert usage at point and return docstring. With highlighting."
429 (if (keymapp function
)
430 doc
; If definition is a keymap, skip arglist note.
431 (let* ((advertised (gethash real-def advertised-signature-table t
))
432 (arglist (if (listp advertised
)
433 advertised
(help-function-arglist real-def
)))
434 (usage (help-split-fundoc doc function
)))
435 (if usage
(setq doc
(cdr usage
)))
437 ((and usage
(not (listp advertised
))) (car usage
))
439 (help--make-usage-docstring function arglist
))
440 ((stringp arglist
) arglist
)
441 ;; Maybe the arglist is in the docstring of a symbol
442 ;; this one is aliased to.
443 ((let ((fun real-function
))
444 (while (and (symbolp fun
)
445 (setq fun
(symbol-function fun
))
446 (not (setq usage
(help-split-fundoc
451 ((or (stringp real-def
)
453 (format "\nMacro: %s"
454 (help--docstring-quote
455 (format-kbd-macro real-def
))))
456 (t "[Missing arglist. Please make a bug report.]")))
457 ;; Insert "`X", not "(\` X)", when documenting `X.
458 (use1 (replace-regexp-in-string
459 "\\`(\\\\=\\\\\\\\=` \\([^\n ]*\\))\\'"
462 (let (subst-use1 subst-doc
)
463 (with-current-buffer buffer
464 (setq subst-use1
(substitute-command-keys use1
))
465 (setq subst-doc
(substitute-command-keys doc
)))
466 (help-highlight-arguments subst-use1 subst-doc
))
468 (let ((fill-begin (point))
469 (high-usage (car high
))
470 (high-doc (cdr high
)))
471 (insert high-usage
"\n")
472 (fill-region fill-begin
(point))
475 (defun help-fns--parent-mode (function)
476 ;; If this is a derived mode, link to the parent.
477 (let ((parent-mode (and (symbolp function
)
479 'derived-mode-parent
))))
481 (insert (substitute-command-keys "\nParent mode: `"))
483 (insert (format "%s" parent-mode
))
484 (make-text-button beg
(point)
486 'help-args
(list parent-mode
)))
487 (insert (substitute-command-keys "'.\n")))))
489 (defun help-fns--obsolete (function)
490 ;; Ignore lambda constructs, keyboard macros, etc.
491 (let* ((obsolete (and (symbolp function
)
492 (get function
'byte-obsolete-info
)))
493 (use (car obsolete
)))
496 (if (eq (car-safe (symbol-function function
)) 'macro
)
500 (when (nth 2 obsolete
)
501 (insert (format " since %s" (nth 2 obsolete
))))
502 (insert (cond ((stringp use
) (concat ";\n" use
))
503 (use (format-message ";\nuse `%s' instead." use
))
507 ;; We could use `symbol-file' but this is a wee bit more efficient.
508 (defun help-fns--autoloaded-p (function file
)
509 "Return non-nil if FUNCTION has previously been autoloaded.
510 FILE is the file where FUNCTION was probably defined."
511 (let* ((file (file-name-sans-extension (file-truename file
)))
512 (load-hist load-history
)
513 (target (cons t function
))
515 (while (and load-hist
(not found
))
516 (and (caar load-hist
)
517 (equal (file-name-sans-extension (caar load-hist
)) file
)
518 (setq found
(member target
(cdar load-hist
))))
519 (setq load-hist
(cdr load-hist
)))
522 (defun help-fns--interactive-only (function)
523 "Insert some help blurb if FUNCTION should only be used interactively."
524 ;; Ignore lambda constructs, keyboard macros, etc.
525 (and (symbolp function
)
526 (not (eq (car-safe (symbol-function function
)) 'macro
))
527 (let* ((interactive-only
528 (or (get function
'interactive-only
)
529 (if (boundp 'byte-compile-interactive-only-functions
)
531 byte-compile-interactive-only-functions
)))))
532 (when interactive-only
533 (insert "\nThis function is for interactive use only"
534 ;; Cf byte-compile-form.
535 (cond ((stringp interactive-only
)
536 (format ";\nin Lisp code %s" interactive-only
))
537 ((and (symbolp 'interactive-only
)
538 (not (eq interactive-only t
)))
539 (format-message ";\nin Lisp code use `%s' instead."
544 (defun help-fns-short-filename (filename)
545 (let* ((abbrev (abbreviate-file-name filename
))
547 (dolist (dir load-path
)
548 (let ((rel (file-relative-name filename dir
)))
549 (if (< (length rel
) (length short
))
551 (let ((rel (file-relative-name abbrev dir
)))
552 (if (< (length rel
) (length short
))
557 (defun describe-function-1 (function)
558 (let* ((advised (and (symbolp function
)
560 (advice--p (advice--symbol-function function
))))
561 ;; If the function is advised, use the symbol that has the
562 ;; real definition, if that symbol is already set up.
565 (advice--cd*r
(advice--symbol-function function
)))
567 ;; Get the real definition.
568 (def (if (symbolp real-function
)
569 (or (symbol-function real-function
)
570 (signal 'void-function
(list real-function
)))
572 (aliased (or (symbolp def
)
573 ;; Advised & aliased function.
574 (and advised
(symbolp real-function
)
575 (not (eq 'autoload
(car-safe def
))))
577 (not (string= (subr-name def
)
578 (symbol-name function
))))))
580 ((and aliased
(not (subrp def
)))
581 (let ((f real-function
))
582 (while (and (fboundp f
)
583 (symbolp (symbol-function f
)))
584 (setq f
(symbol-function f
)))
586 ((subrp def
) (intern (subr-name def
)))
588 (sig-key (if (subrp def
)
589 (indirect-function real-def
)
591 (file-name (find-lisp-object-file-name function
(if aliased
'defun
593 (pt1 (with-current-buffer (help-buffer) (point)))
594 (beg (if (and (or (byte-code-function-p def
)
596 (memq (car-safe def
) '(macro lambda closure
)))
598 (help-fns--autoloaded-p function file-name
))
600 "an interactive autoloaded "
602 (if (commandp def
) "an interactive " "a "))))
604 ;; Print what kind of function-like object FUNCTION is.
605 (princ (cond ((or (stringp def
) (vectorp def
))
607 ;; Aliases are Lisp functions, so we need to check
608 ;; aliases before functions.
610 (format-message "an alias for `%s'" real-def
))
612 (if (eq 'unevalled
(cdr (subr-arity def
)))
613 (concat beg
"special form")
614 (concat beg
"built-in function")))
616 (format "%s autoloaded %s"
617 (if (commandp def
) "an interactive" "an")
618 (if (eq (nth 4 def
) 'keymap
) "keymap"
619 (if (nth 4 def
) "Lisp macro" "Lisp function"))))
620 ((or (eq (car-safe def
) 'macro
)
621 ;; For advised macros, def is a lambda
622 ;; expression or a byte-code-function-p, so we
623 ;; need to check macros before functions.
625 (concat beg
"Lisp macro"))
626 ((byte-code-function-p def
)
627 (concat beg
"compiled Lisp function"))
628 ((eq (car-safe def
) 'lambda
)
629 (concat beg
"Lisp function"))
630 ((eq (car-safe def
) 'closure
)
631 (concat beg
"Lisp closure"))
634 (elts (cdr-safe def
)))
636 (if (char-table-p (car-safe elts
))
639 (setq elts
(cdr-safe elts
)))
640 (concat beg
(if is-full
"keymap" "sparse keymap"))))
643 (if (and aliased
(not (fboundp real-def
)))
644 (princ ",\nwhich is not defined. Please make a bug report.")
645 (with-current-buffer standard-output
648 (when (re-search-backward (substitute-command-keys
649 "alias for `\\([^`']+\\)'")
651 (help-xref-button 1 'help-function real-def
)))))
654 ;; We used to add .el to the file name,
655 ;; but that's completely wrong when the user used load-file.
656 (princ (format-message " in `%s'"
657 (if (eq file-name
'C-source
)
659 (help-fns-short-filename file-name
))))
660 ;; Make a hyperlink to the library.
661 (with-current-buffer standard-output
663 (re-search-backward (substitute-command-keys "`\\([^`']+\\)'")
665 (help-xref-button 1 'help-function-def function file-name
))))
667 (with-current-buffer (help-buffer)
668 (fill-region-as-paragraph (save-excursion (goto-char pt1
) (forward-line 0) (point))
672 (let ((doc-raw (documentation function t
))
673 (key-bindings-buffer (current-buffer)))
675 ;; If the function is autoloaded, and its docstring has
676 ;; key substitution constructs, load the library.
677 (and (autoloadp real-def
) doc-raw
678 help-enable-auto-load
679 (string-match "\\([^\\]=\\|[^=]\\|\\`\\)\\\\[[{<]" doc-raw
)
680 (autoload-do-load real-def
))
682 (help-fns--key-bindings function
)
683 (with-current-buffer standard-output
684 (let ((doc (help-fns--signature function doc-raw sig-key
685 real-function key-bindings-buffer
)))
686 (run-hook-with-args 'help-fns-describe-function-functions function
)
688 (or doc
"Not documented."))
689 ;; Avoid asking the user annoying questions if she decides
690 ;; to save the help buffer, when her locale's codeset
692 (unless (memq text-quoting-style
'(straight grave
))
693 (set-buffer-file-coding-system 'utf-8
))))))))
695 ;; Add defaults to `help-fns-describe-function-functions'.
696 (add-hook 'help-fns-describe-function-functions
#'help-fns--obsolete
)
697 (add-hook 'help-fns-describe-function-functions
#'help-fns--interactive-only
)
698 (add-hook 'help-fns-describe-function-functions
#'help-fns--parent-mode
)
699 (add-hook 'help-fns-describe-function-functions
#'help-fns--compiler-macro
)
705 (defun variable-at-point (&optional any-symbol
)
706 "Return the bound variable symbol found at or before point.
707 Return 0 if there is no such symbol.
708 If ANY-SYMBOL is non-nil, don't insist the symbol be bound."
709 (with-syntax-table emacs-lisp-mode-syntax-table
710 (or (condition-case ()
712 (skip-chars-forward "'")
713 (or (not (zerop (skip-syntax-backward "_w")))
714 (eq (char-syntax (following-char)) ?w
)
715 (eq (char-syntax (following-char)) ?_
)
717 (skip-chars-forward "'")
718 (let ((obj (read (current-buffer))))
719 (and (symbolp obj
) (boundp obj
) obj
)))
721 (let* ((str (find-tag-default))
722 (sym (if str
(intern-soft str
))))
723 (if (and sym
(or any-symbol
(boundp sym
)))
726 (when (and str
(string-match "\\`\\W*\\(.*?\\)\\W*\\'" str
))
727 (setq sym
(intern-soft (match-string 1 str
)))
728 (and (or any-symbol
(boundp sym
)) sym
)))))
731 (defun describe-variable-custom-version-info (variable)
732 (let ((custom-version (get variable
'custom-version
))
733 (cpv (get variable
'custom-package-version
))
737 (format "This variable was introduced, or its default value was changed, in\nversion %s of Emacs.\n"
740 (let* ((package (car-safe cpv
))
741 (version (if (listp (cdr-safe cpv
))
744 (pkg-versions (assq package customize-package-emacs-version-alist
))
745 (emacsv (cdr (assoc version pkg-versions
))))
746 (if (and package version
)
748 (format (concat "This variable was introduced, or its default value was changed, in\nversion %s of the %s package"
750 (format " that is part of Emacs %s" emacsv
))
752 version package
))))))
756 (defun describe-variable (variable &optional buffer frame
)
757 "Display the full documentation of VARIABLE (a symbol).
758 Returns the documentation as a string, also.
759 If VARIABLE has a buffer-local value in BUFFER or FRAME
760 \(default to the current buffer and current frame),
761 it is displayed along with the global value."
763 (let ((v (variable-at-point))
764 (enable-recursive-minibuffers t
)
765 (orig-buffer (current-buffer))
767 (setq val
(completing-read
770 "Describe variable (default %s): " v
)
771 "Describe variable: ")
772 #'help--symbol-completion-table
774 ;; In case the variable only exists in the buffer
775 ;; the command we switch back to that buffer before
776 ;; we examine the variable.
777 (with-current-buffer orig-buffer
778 (or (get vv
'variable-documentation
)
779 (and (boundp vv
) (not (keywordp vv
))))))
781 (if (symbolp v
) (symbol-name v
))))
782 (list (if (equal val
"")
785 (unless (buffer-live-p buffer
) (setq buffer
(current-buffer)))
786 (unless (frame-live-p frame
) (setq frame
(selected-frame)))
787 (if (not (symbolp variable
))
788 (message "You did not specify a variable")
790 (let ((valvoid (not (with-current-buffer buffer
(boundp variable
))))
791 (permanent-local (get variable
'permanent-local
))
792 val val-start-pos locus
)
793 ;; Extract the value before setting up the output buffer,
794 ;; in case `buffer' *is* the output buffer.
796 (with-selected-frame frame
797 (with-current-buffer buffer
798 (setq val
(symbol-value variable
)
799 locus
(variable-binding-locus variable
)))))
800 (help-setup-xref (list #'describe-variable variable buffer
)
801 (called-interactively-p 'interactive
))
802 (with-help-window (help-buffer)
803 (with-current-buffer buffer
805 (setq file-name
(find-lisp-object-file-name variable
'defvar
))
809 (princ (format-message
810 " is a variable defined in `%s'.\n"
811 (if (eq file-name
'C-source
)
813 (file-name-nondirectory file-name
))))
814 (with-current-buffer standard-output
816 (re-search-backward (substitute-command-keys
819 (help-xref-button 1 'help-variable-def
820 variable file-name
)))
822 (princ "It is void as a variable.")
825 (princ " is void as a variable.")
826 (princ (substitute-command-keys "'s ")))))
828 (with-current-buffer standard-output
829 (setq val-start-pos
(point))
831 (let ((line-beg (line-beginning-position))
834 (let ((print-quoted t
))
835 (prin1-to-string val
))))
836 (if (and (symbolp val
) (not (booleanp val
)))
837 (format-message "`%s'" rep
)
839 (if (< (+ (length print-rep
) (point) (- line-beg
)) 68)
840 (insert " " print-rep
)
843 ;; Remove trailing newline.
845 (let* ((sv (get variable
'standard-value
))
846 (origval (and (consp sv
)
849 (error :help-eval-error
))))
851 (when (and (consp sv
)
852 (not (equal origval val
))
853 (not (equal origval
:help-eval-error
)))
854 (princ "\nOriginal value was \n")
857 (if (< (point) (+ from
20))
858 (delete-region (1- from
) from
)))))))
863 (princ (format "Local in buffer %s; "
864 (buffer-name buffer
))))
866 (princ (format "It is a frame-local variable; ")))
867 ((terminal-live-p locus
)
868 (princ (format "It is a terminal-local variable; ")))
870 (princ (format "It is local to %S" locus
))))
871 (if (not (default-boundp variable
))
872 (princ "globally void")
873 (let ((global-val (default-value variable
)))
874 (with-current-buffer standard-output
875 (princ "global value is ")
876 (if (eq val global-val
)
879 ;; Fixme: pp can take an age if you happen to
880 ;; ask for a very large expression. We should
881 ;; probably print it raw once and check it's a
882 ;; sensible size before prettyprinting. -- fx
883 (let ((from (point)))
885 ;; See previous comment for this function.
886 ;; (help-xref-on-pp from (point))
887 (if (< (point) (+ from
20))
888 (delete-region (1- from
) from
)))))))
891 ;; If the value is large, move it to the end.
892 (with-current-buffer standard-output
893 (when (> (count-lines (point-min) (point-max)) 10)
894 ;; Note that setting the syntax table like below
895 ;; makes forward-sexp move over a `'s' at the end
897 (set-syntax-table emacs-lisp-mode-syntax-table
)
898 (goto-char val-start-pos
)
899 ;; The line below previously read as
900 ;; (delete-region (point) (progn (end-of-line) (point)))
901 ;; which suppressed display of the buffer local value for
903 (when (looking-at "value is") (replace-match ""))
905 (insert "\n\nValue:")
906 (set (make-local-variable 'help-button-cache
)
908 (insert "value is shown ")
909 (insert-button "below"
910 'action help-button-cache
912 'help-echo
"mouse-2, RET: show value")
916 (let* ((alias (condition-case nil
917 (indirect-variable variable
)
919 (obsolete (get variable
'byte-obsolete-variable
))
921 (safe-var (get variable
'safe-local-variable
))
922 (doc (or (documentation-property
923 variable
'variable-documentation
)
924 (documentation-property
925 alias
'variable-documentation
)))
928 ;; Mention if it's a local variable.
930 ((and (local-variable-if-set-p variable
)
931 (or (not (local-variable-p variable
))
933 (local-variable-if-set-p variable
))))
935 (princ " Automatically becomes ")
937 (princ "permanently "))
938 (princ "buffer-local when set.\n"))
939 ((not permanent-local
))
943 (substitute-command-keys
944 " This variable's buffer-local value is permanent.\n")))
947 (princ (substitute-command-keys
948 " This variable's value is permanent \
949 if it is given a local binding.\n"))))
951 ;; Mention if it's an alias.
952 (unless (eq alias variable
)
954 (princ (format-message
955 " This variable is an alias for `%s'.\n"
960 (princ " This variable is obsolete")
962 (princ (format " since %s" (nth 2 obsolete
))))
963 (princ (cond ((stringp use
) (concat ";\n " use
))
964 (use (format-message ";\n use `%s' instead."
969 (when (member (cons variable val
)
970 (with-current-buffer buffer
971 file-local-variables-alist
))
973 (if (member (cons variable val
)
974 (with-current-buffer buffer
975 dir-local-variables-alist
))
976 (let ((file (and (buffer-file-name buffer
)
978 (buffer-file-name buffer
)))
979 (dir-locals-find-file
980 (buffer-file-name buffer
))))
982 (princ (substitute-command-keys
983 " This variable's value is directory-local"))
984 (when (consp file
) ; result from cache
985 ;; If the cache element has an mtime, we
986 ;; assume it came from a file.
988 ;; (car file) is a directory.
989 (setq file
(dir-locals--all-files (car file
)))
990 ;; Otherwise, assume it was set directly.
991 (setq file
(car file
)
996 (princ (substitute-command-keys
998 (is-directory "for the directory\n `")
999 ;; Many files matched.
1000 ((and (consp file
) (cdr file
))
1001 (setq file
(file-name-directory (car file
)))
1002 (format "by one of the\n %s files in the directory\n `"
1004 (t (setq file
(car file
))
1005 "by the file\n `"))))
1006 (with-current-buffer standard-output
1008 file
'type
'help-dir-local-var-def
1009 'help-args
(list variable file
)))
1010 (princ (substitute-command-keys "'.\n"))))
1011 (princ (substitute-command-keys
1012 " This variable's value is file-local.\n"))))
1014 (when (memq variable ignored-local-variables
)
1016 (princ " This variable is ignored as a file-local \
1019 ;; Can be both risky and safe, eg auto-fill-function.
1020 (when (risky-local-variable-p variable
)
1022 (princ " This variable may be risky if used as a \
1023 file-local variable.\n")
1024 (when (assq variable safe-local-variable-values
)
1025 (princ (substitute-command-keys
1026 " However, you have added it to \
1027 `safe-local-variable-values'.\n"))))
1031 (princ " This variable is safe as a file local variable ")
1032 (princ "if its value\n satisfies the predicate ")
1033 (princ (if (byte-code-function-p safe-var
)
1034 "which is a byte-compiled expression.\n"
1035 (format-message "`%s'.\n" safe-var
))))
1037 (if extra-line
(terpri))
1038 (princ "Documentation:\n")
1039 (with-current-buffer standard-output
1040 (insert (or doc
"Not documented as a variable."))))
1042 ;; Make a link to customize if this variable can be customized.
1043 (when (custom-variable-p variable
)
1044 (let ((customize-label "customize"))
1047 (princ (concat "You can " customize-label
" this variable."))
1048 (with-current-buffer standard-output
1051 (concat "\\(" customize-label
"\\)") nil t
)
1052 (help-xref-button 1 'help-customize-variable variable
))))
1053 ;; Note variable's version or package version.
1054 (let ((output (describe-variable-custom-version-info variable
)))
1060 (with-current-buffer standard-output
1061 ;; Return the text we displayed.
1062 (buffer-string))))))))
1065 (defvar help-xref-stack-item
)
1068 (defun describe-symbol (symbol &optional buffer frame
)
1069 "Display the full documentation of SYMBOL.
1070 Will show the info of SYMBOL as a function, variable, and/or face.
1071 Optional arguments BUFFER and FRAME specify for which buffer and
1072 frame to show the information about SYMBOL; they default to the
1073 current buffer and the selected frame, respectively."
1075 (let* ((v-or-f (symbol-at-point))
1076 (found (cl-some (lambda (x) (funcall (nth 1 x
) v-or-f
))
1077 describe-symbol-backends
))
1078 (v-or-f (if found v-or-f
(function-called-at-point)))
1079 (found (or found v-or-f
))
1080 (enable-recursive-minibuffers t
)
1081 (val (completing-read (if found
1083 "Describe symbol (default %s): " v-or-f
)
1084 "Describe symbol: ")
1087 (cl-some (lambda (x) (funcall (nth 1 x
) vv
))
1088 describe-symbol-backends
))
1090 (if found
(symbol-name v-or-f
)))))
1091 (list (if (equal val
"")
1092 v-or-f
(intern val
)))))
1093 (if (not (symbolp symbol
))
1094 (user-error "You didn't specify a function or variable"))
1095 (unless (buffer-live-p buffer
) (setq buffer
(current-buffer)))
1096 (unless (frame-live-p frame
) (setq frame
(selected-frame)))
1097 (with-current-buffer (help-buffer)
1098 ;; Push the previous item on the stack before clobbering the output buffer.
1099 (help-setup-xref nil nil
)
1103 (mapcar (pcase-lambda (`(,name
,testfn
,descfn
))
1104 (when (funcall testfn symbol
)
1105 ;; Don't record the current entry in the stack.
1106 (setq help-xref-stack-item nil
)
1108 (funcall descfn symbol buffer frame
))))
1109 describe-symbol-backends
))))
1110 (single (null (cdr docs
))))
1112 (goto-char (point-min))
1113 (let ((inhibit-read-only t
)
1114 (name (caar docs
)) ;Name of doc currently at BOB.
1115 (doc (cdr (cadr docs
)))) ;Doc to add at BOB.
1118 (delete-region (point)
1119 (progn (skip-chars-backward " \t\n") (point)))
1122 (propertize "\n" 'face
'(:height
0.1 :inverse-video t
)))
1125 (insert (symbol-name symbol
)
1126 " is also a " name
"." "\n\n"))))
1127 (setq docs
(cdr docs
)))
1129 ;; Don't record the `describe-variable' item in the stack.
1130 (setq help-xref-stack-item nil
)
1131 (help-setup-xref (list #'describe-symbol symbol
) nil
))
1132 (goto-char (point-min)))))
1135 (defun describe-syntax (&optional buffer
)
1136 "Describe the syntax specifications in the syntax table of BUFFER.
1137 The descriptions are inserted in a help buffer, which is then displayed.
1138 BUFFER defaults to the current buffer."
1140 (setq buffer
(or buffer
(current-buffer)))
1141 (help-setup-xref (list #'describe-syntax buffer
)
1142 (called-interactively-p 'interactive
))
1143 (with-help-window (help-buffer)
1144 (let ((table (with-current-buffer buffer
(syntax-table))))
1145 (with-current-buffer standard-output
1146 (describe-vector table
'internal-describe-syntax-value
)
1147 (while (setq table
(char-table-parent table
))
1148 (insert "\nThe parent syntax table is:")
1149 (describe-vector table
'internal-describe-syntax-value
))))))
1151 (defun help-describe-category-set (value)
1153 ((null value
) "default")
1154 ((char-table-p value
) "deeper char-table ...")
1155 (t (condition-case nil
1156 (category-set-mnemonics value
)
1157 (error "invalid"))))))
1160 (defun describe-categories (&optional buffer
)
1161 "Describe the category specifications in the current category table.
1162 The descriptions are inserted in a buffer, which is then displayed.
1163 If BUFFER is non-nil, then describe BUFFER's category table instead.
1164 BUFFER should be a buffer or a buffer name."
1166 (setq buffer
(or buffer
(current-buffer)))
1167 (help-setup-xref (list #'describe-categories buffer
)
1168 (called-interactively-p 'interactive
))
1169 (with-help-window (help-buffer)
1170 (let* ((table (with-current-buffer buffer
(category-table)))
1171 (docs (char-table-extra-slot table
0)))
1172 (if (or (not (vectorp docs
)) (/= (length docs
) 95))
1173 (error "Invalid first extra slot in this category table\n"))
1174 (with-current-buffer standard-output
1175 (setq-default help-button-cache
(make-marker))
1176 (insert "Legend of category mnemonics ")
1177 (insert-button "(longer descriptions at the bottom)"
1178 'action help-button-cache
1180 'help-echo
"mouse-2, RET: show full legend")
1182 (let ((pos (point)) (items 0) lines n
)
1184 (if (aref docs i
) (setq items
(1+ items
))))
1185 (setq lines
(1+ (/ (1- items
) 4)))
1188 (let ((elt (aref docs i
)))
1190 (string-match ".*" elt
)
1191 (setq elt
(match-string 0 elt
))
1192 (if (>= (length elt
) 17)
1193 (setq elt
(concat (substring elt
0 14) "...")))
1194 (if (< (point) (point-max))
1195 (move-to-column (* 20 (/ n lines
)) t
))
1196 (insert (+ i ?\s
) ?
: elt
)
1197 (if (< (point) (point-max))
1201 (if (= (% n lines
) 0)
1202 (goto-char pos
))))))
1203 (goto-char (point-max))
1205 "character(s)\tcategory mnemonics\n"
1206 "------------\t------------------")
1207 (describe-vector table
'help-describe-category-set
)
1208 (set-marker help-button-cache
(point))
1209 (insert "Legend of category mnemonics:\n")
1211 (let ((elt (aref docs i
)))
1213 (if (string-match "\n" elt
)
1214 (setq elt
(substring elt
(match-end 0))))
1215 (insert (+ i ?\s
) ": " elt
"\n"))))
1216 (while (setq table
(char-table-parent table
))
1217 (insert "\nThe parent category table is:")
1218 (describe-vector table
'help-describe-category-set
))))))
1221 ;;; Replacements for old lib-src/ programs. Don't seem especially useful.
1223 ;; Replaces lib-src/digest-doc.c.
1225 (defun doc-file-to-man (file)
1226 "Produce an nroff buffer containing the doc-strings from the DOC file."
1227 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1228 internal-doc-file-name t
)))
1229 (or (file-readable-p file
)
1230 (error "Cannot read file `%s'" file
))
1231 (pop-to-buffer (generate-new-buffer "*man-doc*"))
1232 (setq buffer-undo-list t
)
1233 (insert ".TH \"Command Summary for GNU Emacs\"\n"
1234 ".AU Richard M. Stallman\n")
1235 (insert-file-contents file
)
1237 (while (search-forward "\x1f" nil
'move
)
1238 (if (looking-at "S")
1239 (delete-region (1- (point)) (line-end-position))
1245 (insert (if (looking-at "F") "Function " "Variable "))
1248 (insert ".DS L\n"))))
1250 (setq buffer-undo-list nil
)
1253 ;; Replaces lib-src/sorted-doc.c.
1255 (defun doc-file-to-info (file)
1256 "Produce a texinfo buffer with sorted doc-strings from the DOC file."
1257 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1258 internal-doc-file-name t
)))
1259 (or (file-readable-p file
)
1260 (error "Cannot read file `%s'" file
))
1261 (let ((i 0) type name doc alist
)
1263 (insert-file-contents file
)
1264 ;; The characters "@{}" need special treatment.
1265 (while (re-search-forward "[@{}]" nil t
)
1269 (goto-char (point-min))
1270 (while (search-forward "\x1f" nil t
)
1271 (unless (looking-at "S")
1272 (setq type
(char-after)
1273 name
(buffer-substring (1+ (point)) (line-end-position))
1274 doc
(buffer-substring (line-beginning-position 2)
1275 (if (search-forward "\x1f" nil
'move
)
1278 alist
(cons (list name type doc
) alist
))
1279 (backward-char 1))))
1280 (pop-to-buffer (generate-new-buffer "*info-doc*"))
1281 (setq buffer-undo-list t
)
1282 ;; Write the output header.
1283 (insert "\\input texinfo @c -*-texinfo-*-\n"
1284 "@setfilename emacsdoc.info\n"
1285 "@settitle Command Summary for GNU Emacs\n"
1288 "@unnumbered Command Summary for GNU Emacs\n\n"
1291 "@global@let@ITEM@item\n"
1292 "@def@item{@filbreak@vskip5pt@ITEM}\n"
1293 "@font@tensy cmsy10 scaled @magstephalf\n"
1294 "@font@teni cmmi10 scaled @magstephalf\n"
1295 "@def\\{{@tensy@char110}}\n" ; this backslash goes with cmr10
1296 "@def|{{@tensy@char106}}\n"
1297 "@def@{{{@tensy@char102}}\n"
1298 "@def@}{{@tensy@char103}}\n"
1299 "@def<{{@teni@char62}}\n"
1300 "@def>{{@teni@char60}}\n"
1303 "@tableindent-0.2in\n"
1305 ;; Sort the array by name; within each name, by type (functions first).
1306 (setq alist
(sort alist
(lambda (e1 e2
)
1307 (if (string-equal (car e1
) (car e2
))
1308 (<= (cadr e1
) (cadr e2
))
1309 (string-lessp (car e1
) (car e2
))))))
1310 ;; Print each function.
1313 (if (char-equal (cadr e
) ?\F
) "Function" "Variable")
1314 " @code{" (car e
) "}\n@display\n"
1317 ;; Try to avoid a save size overflow in the TeX output routine.
1318 (if (zerop (setq i
(%
(1+ i
) 100)))
1319 (insert "\n@end table\n@table @asis\n")))
1320 (insert "@end table\n"
1322 (setq buffer-undo-list nil
)
1327 ;;; help-fns.el ends here