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 <https://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
))
563 (defun help-fns--analyze-function (function)
564 ;; FIXME: Document/explain the differences between FUNCTION,
565 ;; REAL-FUNCTION, DEF, and REAL-DEF.
566 "Return information about FUNCTION.
567 Returns a list of the form (REAL-FUNCTION DEF ALIASED REAL-DEF)."
568 (let* ((advised (and (symbolp function
)
569 (advice--p (advice--symbol-function function
))))
570 ;; If the function is advised, use the symbol that has the
571 ;; real definition, if that symbol is already set up.
574 (advice--cd*r
(advice--symbol-function function
)))
576 ;; Get the real definition, if any.
577 (def (if (symbolp real-function
)
578 (cond ((symbol-function real-function
))
579 ((get real-function
'function-documentation
)
581 (t (signal 'void-function
(list real-function
))))
585 ;; Advised & aliased function.
586 (and advised
(symbolp real-function
)
587 (not (eq 'autoload
(car-safe def
))))
589 (not (string= (subr-name def
)
590 (symbol-name function
)))))))
592 ((and aliased
(not (subrp def
)))
593 (let ((f real-function
))
594 (while (and (fboundp f
)
595 (symbolp (symbol-function f
)))
596 (setq f
(symbol-function f
)))
598 ((subrp def
) (intern (subr-name def
)))
600 (list real-function def aliased real-def
)))
602 (defun help-fns-function-description-header (function)
603 "Print a line describing FUNCTION to `standard-output'."
604 (pcase-let* ((`(,_real-function
,def
,aliased
,real-def
)
605 (help-fns--analyze-function function
))
606 (file-name (find-lisp-object-file-name function
(if aliased
'defun
608 (beg (if (and (or (byte-code-function-p def
)
610 (memq (car-safe def
) '(macro lambda closure
)))
612 (help-fns--autoloaded-p function file-name
))
614 "an interactive autoloaded "
616 (if (commandp def
) "an interactive " "a "))))
618 ;; Print what kind of function-like object FUNCTION is.
619 (princ (cond ((or (stringp def
) (vectorp def
))
621 ((and (symbolp function
)
622 (get function
'reader-construct
))
623 "a reader construct")
624 ;; Aliases are Lisp functions, so we need to check
625 ;; aliases before functions.
627 (format-message "an alias for `%s'" real-def
))
629 (if (eq 'unevalled
(cdr (subr-arity def
)))
630 (concat beg
"special form")
631 (concat beg
"built-in function")))
633 (format "%s autoloaded %s"
634 (if (commandp def
) "an interactive" "an")
635 (if (eq (nth 4 def
) 'keymap
) "keymap"
636 (if (nth 4 def
) "Lisp macro" "Lisp function"))))
637 ((or (eq (car-safe def
) 'macro
)
638 ;; For advised macros, def is a lambda
639 ;; expression or a byte-code-function-p, so we
640 ;; need to check macros before functions.
642 (concat beg
"Lisp macro"))
643 ((byte-code-function-p def
)
644 (concat beg
"compiled Lisp function"))
645 ((eq (car-safe def
) 'lambda
)
646 (concat beg
"Lisp function"))
647 ((eq (car-safe def
) 'closure
)
648 (concat beg
"Lisp closure"))
651 (elts (cdr-safe def
)))
653 (if (char-table-p (car-safe elts
))
656 (setq elts
(cdr-safe elts
)))
657 (concat beg
(if is-full
"keymap" "sparse keymap"))))
660 (if (and aliased
(not (fboundp real-def
)))
661 (princ ",\nwhich is not defined. Please make a bug report.")
662 (with-current-buffer standard-output
665 (when (re-search-backward (substitute-command-keys
666 "alias for `\\([^`']+\\)'")
668 (help-xref-button 1 'help-function real-def
)))))
671 ;; We used to add .el to the file name,
672 ;; but that's completely wrong when the user used load-file.
673 (princ (format-message " in `%s'"
674 (if (eq file-name
'C-source
)
676 (help-fns-short-filename file-name
))))
677 ;; Make a hyperlink to the library.
678 (with-current-buffer standard-output
680 (re-search-backward (substitute-command-keys "`\\([^`']+\\)'")
682 (help-xref-button 1 'help-function-def function file-name
))))
686 (defun describe-function-1 (function)
687 (let ((pt1 (with-current-buffer (help-buffer) (point))))
688 (help-fns-function-description-header function
)
689 (with-current-buffer (help-buffer)
690 (fill-region-as-paragraph (save-excursion (goto-char pt1
) (forward-line 0) (point))
694 (pcase-let* ((`(,real-function
,def
,_aliased
,real-def
)
695 (help-fns--analyze-function function
))
696 (doc-raw (condition-case nil
697 ;; FIXME: Maybe `documentation' should return nil
698 ;; for invalid functions i.s.o. signaling an error.
699 (documentation function t
)
700 ;; E.g. an alias for a not yet defined function.
701 ((invalid-function void-function
) nil
)))
702 (key-bindings-buffer (current-buffer)))
704 ;; If the function is autoloaded, and its docstring has
705 ;; key substitution constructs, load the library.
706 (and (autoloadp real-def
) doc-raw
707 help-enable-auto-load
708 (string-match "\\([^\\]=\\|[^=]\\|\\`\\)\\\\[[{<]" doc-raw
)
709 (autoload-do-load real-def
))
711 (help-fns--key-bindings function
)
712 (with-current-buffer standard-output
713 (let ((doc (condition-case nil
714 ;; FIXME: Maybe `help-fns--signature' should return `doc'
715 ;; for invalid functions i.s.o. signaling an error.
718 (if (subrp def
) (indirect-function real-def
) real-def
)
719 real-function key-bindings-buffer
)
720 ;; E.g. an alias for a not yet defined function.
721 ((invalid-function void-function
) doc-raw
))))
722 (run-hook-with-args 'help-fns-describe-function-functions function
)
723 (insert "\n" (or doc
"Not documented.")))
724 (when (or (function-get function
'pure
)
725 (function-get function
'side-effect-free
))
726 (insert "\nThis function does not change global state, "
727 "including the match data."))
728 ;; Avoid asking the user annoying questions if she decides
729 ;; to save the help buffer, when her locale's codeset
731 (unless (memq text-quoting-style
'(straight grave
))
732 (set-buffer-file-coding-system 'utf-8
)))))
734 ;; Add defaults to `help-fns-describe-function-functions'.
735 (add-hook 'help-fns-describe-function-functions
#'help-fns--obsolete
)
736 (add-hook 'help-fns-describe-function-functions
#'help-fns--interactive-only
)
737 (add-hook 'help-fns-describe-function-functions
#'help-fns--parent-mode
)
738 (add-hook 'help-fns-describe-function-functions
#'help-fns--compiler-macro
)
744 (defun variable-at-point (&optional any-symbol
)
745 "Return the bound variable symbol found at or before point.
746 Return 0 if there is no such symbol.
747 If ANY-SYMBOL is non-nil, don't insist the symbol be bound."
748 (with-syntax-table emacs-lisp-mode-syntax-table
749 (or (condition-case ()
751 (skip-chars-forward "'")
752 (or (not (zerop (skip-syntax-backward "_w")))
753 (eq (char-syntax (following-char)) ?w
)
754 (eq (char-syntax (following-char)) ?_
)
756 (skip-chars-forward "'")
757 (let ((obj (read (current-buffer))))
758 (and (symbolp obj
) (boundp obj
) obj
)))
760 (let* ((str (find-tag-default))
761 (sym (if str
(intern-soft str
))))
762 (if (and sym
(or any-symbol
(boundp sym
)))
765 (when (and str
(string-match "\\`\\W*\\(.*?\\)\\W*\\'" str
))
766 (setq sym
(intern-soft (match-string 1 str
)))
767 (and (or any-symbol
(boundp sym
)) sym
)))))
770 (defun describe-variable-custom-version-info (variable)
771 (let ((custom-version (get variable
'custom-version
))
772 (cpv (get variable
'custom-package-version
))
776 (format "This variable was introduced, or its default value was changed, in\nversion %s of Emacs.\n"
779 (let* ((package (car-safe cpv
))
780 (version (if (listp (cdr-safe cpv
))
783 (pkg-versions (assq package customize-package-emacs-version-alist
))
784 (emacsv (cdr (assoc version pkg-versions
))))
785 (if (and package version
)
787 (format (concat "This variable was introduced, or its default value was changed, in\nversion %s of the %s package"
789 (format " that is part of Emacs %s" emacsv
))
791 version package
))))))
795 (defun describe-variable (variable &optional buffer frame
)
796 "Display the full documentation of VARIABLE (a symbol).
797 Returns the documentation as a string, also.
798 If VARIABLE has a buffer-local value in BUFFER or FRAME
799 \(default to the current buffer and current frame),
800 it is displayed along with the global value."
802 (let ((v (variable-at-point))
803 (enable-recursive-minibuffers t
)
804 (orig-buffer (current-buffer))
806 (setq val
(completing-read
809 "Describe variable (default %s): " v
)
810 "Describe variable: ")
811 #'help--symbol-completion-table
813 ;; In case the variable only exists in the buffer
814 ;; the command we switch back to that buffer before
815 ;; we examine the variable.
816 (with-current-buffer orig-buffer
817 (or (get vv
'variable-documentation
)
818 (and (boundp vv
) (not (keywordp vv
))))))
820 (if (symbolp v
) (symbol-name v
))))
821 (list (if (equal val
"")
824 (unless (buffer-live-p buffer
) (setq buffer
(current-buffer)))
825 (unless (frame-live-p frame
) (setq frame
(selected-frame)))
826 (if (not (symbolp variable
))
827 (message "You did not specify a variable")
829 (let ((valvoid (not (with-current-buffer buffer
(boundp variable
))))
830 (permanent-local (get variable
'permanent-local
))
831 val val-start-pos locus
)
832 ;; Extract the value before setting up the output buffer,
833 ;; in case `buffer' *is* the output buffer.
835 (with-selected-frame frame
836 (with-current-buffer buffer
837 (setq val
(symbol-value variable
)
838 locus
(variable-binding-locus variable
)))))
839 (help-setup-xref (list #'describe-variable variable buffer
)
840 (called-interactively-p 'interactive
))
841 (with-help-window (help-buffer)
842 (with-current-buffer buffer
844 (setq file-name
(find-lisp-object-file-name variable
'defvar
))
848 (princ (format-message
849 " is a variable defined in `%s'.\n"
850 (if (eq file-name
'C-source
)
852 (file-name-nondirectory file-name
))))
853 (with-current-buffer standard-output
855 (re-search-backward (substitute-command-keys
858 (help-xref-button 1 'help-variable-def
859 variable file-name
)))
861 (princ "It is void as a variable.")
864 (princ " is void as a variable.")
865 (princ (substitute-command-keys "'s ")))))
867 (with-current-buffer standard-output
868 (setq val-start-pos
(point))
870 (let ((line-beg (line-beginning-position))
873 (let ((print-quoted t
)
875 (cl-prin1-to-string val
))))
876 (if (and (symbolp val
) (not (booleanp val
)))
877 (format-message "`%s'" rep
)
879 (if (< (+ (length print-rep
) (point) (- line-beg
)) 68)
880 (insert " " print-rep
)
882 (let ((buf (current-buffer)))
886 (let ((pp-buffer (current-buffer)))
887 (with-current-buffer buf
888 (insert-buffer-substring pp-buffer
)))))
889 ;; Remove trailing newline.
890 (and (= (char-before) ?
\n) (delete-char -
1)))
891 (let* ((sv (get variable
'standard-value
))
892 (origval (and (consp sv
)
895 (error :help-eval-error
))))
897 (when (and (consp sv
)
898 (not (equal origval val
))
899 (not (equal origval
:help-eval-error
)))
900 (princ "\nOriginal value was \n")
904 (narrow-to-region from
(point))
905 (save-excursion (pp-buffer)))
906 (if (< (point) (+ from
20))
907 (delete-region (1- from
) from
)))))))
912 (princ (format "Local in buffer %s; "
913 (buffer-name buffer
))))
914 ((terminal-live-p locus
)
915 (princ (format "It is a terminal-local variable; ")))
917 (princ (format "It is local to %S" locus
))))
918 (if (not (default-boundp variable
))
919 (princ "globally void")
920 (let ((global-val (default-value variable
)))
921 (with-current-buffer standard-output
922 (princ "global value is ")
923 (if (eq val global-val
)
926 ;; Fixme: pp can take an age if you happen to
927 ;; ask for a very large expression. We should
928 ;; probably print it raw once and check it's a
929 ;; sensible size before prettyprinting. -- fx
930 (let ((from (point)))
931 (cl-prin1 global-val
)
933 (narrow-to-region from
(point))
934 (save-excursion (pp-buffer)))
935 ;; See previous comment for this function.
936 ;; (help-xref-on-pp from (point))
937 (if (< (point) (+ from
20))
938 (delete-region (1- from
) from
)))))))
941 ;; If the value is large, move it to the end.
942 (with-current-buffer standard-output
943 (when (> (count-lines (point-min) (point-max)) 10)
944 ;; Note that setting the syntax table like below
945 ;; makes forward-sexp move over a `'s' at the end
947 (set-syntax-table emacs-lisp-mode-syntax-table
)
948 (goto-char val-start-pos
)
949 ;; The line below previously read as
950 ;; (delete-region (point) (progn (end-of-line) (point)))
951 ;; which suppressed display of the buffer local value for
953 (when (looking-at "value is") (replace-match ""))
955 (insert "\n\nValue:")
956 (set (make-local-variable 'help-button-cache
)
958 (insert "value is shown ")
959 (insert-button "below"
960 'action help-button-cache
962 'help-echo
"mouse-2, RET: show value")
966 (let* ((alias (condition-case nil
967 (indirect-variable variable
)
969 (obsolete (get variable
'byte-obsolete-variable
))
970 (watchpoints (get-variable-watchers variable
))
972 (safe-var (get variable
'safe-local-variable
))
973 (doc (or (documentation-property
974 variable
'variable-documentation
)
975 (documentation-property
976 alias
'variable-documentation
)))
979 ;; Mention if it's a local variable.
981 ((and (local-variable-if-set-p variable
)
982 (or (not (local-variable-p variable
))
984 (local-variable-if-set-p variable
))))
986 (princ " Automatically becomes ")
988 (princ "permanently "))
989 (princ "buffer-local when set.\n"))
990 ((not permanent-local
))
994 (substitute-command-keys
995 " This variable's buffer-local value is permanent.\n")))
998 (princ (substitute-command-keys
999 " This variable's value is permanent \
1000 if it is given a local binding.\n"))))
1002 ;; Mention if it's an alias.
1003 (unless (eq alias variable
)
1005 (princ (format-message
1006 " This variable is an alias for `%s'.\n"
1011 (princ " This variable is obsolete")
1012 (if (nth 2 obsolete
)
1013 (princ (format " since %s" (nth 2 obsolete
))))
1014 (princ (cond ((stringp use
) (concat ";\n " use
))
1015 (use (format-message ";\n use `%s' instead."
1022 (princ " Calls these functions when changed: ")
1026 (when (member (cons variable val
)
1027 (with-current-buffer buffer
1028 file-local-variables-alist
))
1030 (if (member (cons variable val
)
1031 (with-current-buffer buffer
1032 dir-local-variables-alist
))
1033 (let ((file (and (buffer-file-name buffer
)
1035 (buffer-file-name buffer
)))
1036 (dir-locals-find-file
1037 (buffer-file-name buffer
))))
1039 (princ (substitute-command-keys
1040 " This variable's value is directory-local"))
1041 (when (consp file
) ; result from cache
1042 ;; If the cache element has an mtime, we
1043 ;; assume it came from a file.
1045 ;; (car file) is a directory.
1046 (setq file
(dir-locals--all-files (car file
)))
1047 ;; Otherwise, assume it was set directly.
1048 (setq file
(car file
)
1053 (princ (substitute-command-keys
1055 (is-directory "for the directory\n `")
1056 ;; Many files matched.
1057 ((and (consp file
) (cdr file
))
1058 (setq file
(file-name-directory (car file
)))
1059 (format "by one of the\n %s files in the directory\n `"
1061 (t (setq file
(car file
))
1062 "by the file\n `"))))
1063 (with-current-buffer standard-output
1065 file
'type
'help-dir-local-var-def
1066 'help-args
(list variable file
)))
1067 (princ (substitute-command-keys "'.\n"))))
1068 (princ (substitute-command-keys
1069 " This variable's value is file-local.\n"))))
1071 (when (memq variable ignored-local-variables
)
1073 (princ " This variable is ignored as a file-local \
1076 ;; Can be both risky and safe, eg auto-fill-function.
1077 (when (risky-local-variable-p variable
)
1079 (princ " This variable may be risky if used as a \
1080 file-local variable.\n")
1081 (when (assq variable safe-local-variable-values
)
1082 (princ (substitute-command-keys
1083 " However, you have added it to \
1084 `safe-local-variable-values'.\n"))))
1088 (princ " This variable is safe as a file local variable ")
1089 (princ "if its value\n satisfies the predicate ")
1090 (princ (if (byte-code-function-p safe-var
)
1091 "which is a byte-compiled expression.\n"
1092 (format-message "`%s'.\n" safe-var
))))
1094 (if extra-line
(terpri))
1095 (princ "Documentation:\n")
1096 (with-current-buffer standard-output
1097 (insert (or doc
"Not documented as a variable."))))
1099 ;; Make a link to customize if this variable can be customized.
1100 (when (custom-variable-p variable
)
1101 (let ((customize-label "customize"))
1104 (princ (concat "You can " customize-label
" this variable."))
1105 (with-current-buffer standard-output
1108 (concat "\\(" customize-label
"\\)") nil t
)
1109 (help-xref-button 1 'help-customize-variable variable
))))
1110 ;; Note variable's version or package version.
1111 (let ((output (describe-variable-custom-version-info variable
)))
1117 (with-current-buffer standard-output
1118 ;; Return the text we displayed.
1119 (buffer-string))))))))
1122 (defvar help-xref-stack-item
)
1125 (defun describe-symbol (symbol &optional buffer frame
)
1126 "Display the full documentation of SYMBOL.
1127 Will show the info of SYMBOL as a function, variable, and/or face.
1128 Optional arguments BUFFER and FRAME specify for which buffer and
1129 frame to show the information about SYMBOL; they default to the
1130 current buffer and the selected frame, respectively."
1132 (let* ((v-or-f (symbol-at-point))
1133 (found (if v-or-f
(cl-some (lambda (x) (funcall (nth 1 x
) v-or-f
))
1134 describe-symbol-backends
)))
1135 (v-or-f (if found v-or-f
(function-called-at-point)))
1136 (found (or found v-or-f
))
1137 (enable-recursive-minibuffers t
)
1138 (val (completing-read (if found
1140 "Describe symbol (default %s): " v-or-f
)
1141 "Describe symbol: ")
1144 (cl-some (lambda (x) (funcall (nth 1 x
) vv
))
1145 describe-symbol-backends
))
1147 (if found
(symbol-name v-or-f
)))))
1148 (list (if (equal val
"")
1149 v-or-f
(intern val
)))))
1150 (if (not (symbolp symbol
))
1151 (user-error "You didn't specify a function or variable"))
1152 (unless (buffer-live-p buffer
) (setq buffer
(current-buffer)))
1153 (unless (frame-live-p frame
) (setq frame
(selected-frame)))
1154 (with-current-buffer (help-buffer)
1155 ;; Push the previous item on the stack before clobbering the output buffer.
1156 (help-setup-xref nil nil
)
1160 (mapcar (pcase-lambda (`(,name
,testfn
,descfn
))
1161 (when (funcall testfn symbol
)
1162 ;; Don't record the current entry in the stack.
1163 (setq help-xref-stack-item nil
)
1165 (funcall descfn symbol buffer frame
))))
1166 describe-symbol-backends
))))
1167 (single (null (cdr docs
))))
1169 (goto-char (point-min))
1170 (let ((inhibit-read-only t
)
1171 (name (caar docs
)) ;Name of doc currently at BOB.
1172 (doc (cdr (cadr docs
)))) ;Doc to add at BOB.
1175 (delete-region (point)
1176 (progn (skip-chars-backward " \t\n") (point)))
1179 (propertize "\n" 'face
'(:height
0.1 :inverse-video t
)))
1182 (insert (symbol-name symbol
)
1183 " is also a " name
"." "\n\n"))))
1184 (setq docs
(cdr docs
)))
1186 ;; Don't record the `describe-variable' item in the stack.
1187 (setq help-xref-stack-item nil
)
1188 (help-setup-xref (list #'describe-symbol symbol
) nil
))
1189 (goto-char (point-min)))))
1192 (defun describe-syntax (&optional buffer
)
1193 "Describe the syntax specifications in the syntax table of BUFFER.
1194 The descriptions are inserted in a help buffer, which is then displayed.
1195 BUFFER defaults to the current buffer."
1197 (setq buffer
(or buffer
(current-buffer)))
1198 (help-setup-xref (list #'describe-syntax buffer
)
1199 (called-interactively-p 'interactive
))
1200 (with-help-window (help-buffer)
1201 (let ((table (with-current-buffer buffer
(syntax-table))))
1202 (with-current-buffer standard-output
1203 (describe-vector table
'internal-describe-syntax-value
)
1204 (while (setq table
(char-table-parent table
))
1205 (insert "\nThe parent syntax table is:")
1206 (describe-vector table
'internal-describe-syntax-value
))))))
1208 (defun help-describe-category-set (value)
1210 ((null value
) "default")
1211 ((char-table-p value
) "deeper char-table ...")
1212 (t (condition-case nil
1213 (category-set-mnemonics value
)
1214 (error "invalid"))))))
1217 (defun describe-categories (&optional buffer
)
1218 "Describe the category specifications in the current category table.
1219 The descriptions are inserted in a buffer, which is then displayed.
1220 If BUFFER is non-nil, then describe BUFFER's category table instead.
1221 BUFFER should be a buffer or a buffer name."
1223 (setq buffer
(or buffer
(current-buffer)))
1224 (help-setup-xref (list #'describe-categories buffer
)
1225 (called-interactively-p 'interactive
))
1226 (with-help-window (help-buffer)
1227 (let* ((table (with-current-buffer buffer
(category-table)))
1228 (docs (char-table-extra-slot table
0)))
1229 (if (or (not (vectorp docs
)) (/= (length docs
) 95))
1230 (error "Invalid first extra slot in this category table\n"))
1231 (with-current-buffer standard-output
1232 (setq-default help-button-cache
(make-marker))
1233 (insert "Legend of category mnemonics ")
1234 (insert-button "(longer descriptions at the bottom)"
1235 'action help-button-cache
1237 'help-echo
"mouse-2, RET: show full legend")
1239 (let ((pos (point)) (items 0) lines n
)
1241 (if (aref docs i
) (setq items
(1+ items
))))
1242 (setq lines
(1+ (/ (1- items
) 4)))
1245 (let ((elt (aref docs i
)))
1247 (string-match ".*" elt
)
1248 (setq elt
(match-string 0 elt
))
1249 (if (>= (length elt
) 17)
1250 (setq elt
(concat (substring elt
0 14) "...")))
1251 (if (< (point) (point-max))
1252 (move-to-column (* 20 (/ n lines
)) t
))
1253 (insert (+ i ?\s
) ?
: elt
)
1254 (if (< (point) (point-max))
1258 (if (= (% n lines
) 0)
1259 (goto-char pos
))))))
1260 (goto-char (point-max))
1262 "character(s)\tcategory mnemonics\n"
1263 "------------\t------------------")
1264 (describe-vector table
'help-describe-category-set
)
1265 (set-marker help-button-cache
(point))
1266 (insert "Legend of category mnemonics:\n")
1268 (let ((elt (aref docs i
)))
1270 (if (string-match "\n" elt
)
1271 (setq elt
(substring elt
(match-end 0))))
1272 (insert (+ i ?\s
) ": " elt
"\n"))))
1273 (while (setq table
(char-table-parent table
))
1274 (insert "\nThe parent category table is:")
1275 (describe-vector table
'help-describe-category-set
))))))
1278 ;;; Replacements for old lib-src/ programs. Don't seem especially useful.
1280 ;; Replaces lib-src/digest-doc.c.
1282 (defun doc-file-to-man (file)
1283 "Produce an nroff buffer containing the doc-strings from the DOC file."
1284 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1285 internal-doc-file-name t
)))
1286 (or (file-readable-p file
)
1287 (error "Cannot read file `%s'" file
))
1288 (pop-to-buffer (generate-new-buffer "*man-doc*"))
1289 (setq buffer-undo-list t
)
1290 (insert ".TH \"Command Summary for GNU Emacs\"\n"
1291 ".AU Richard M. Stallman\n")
1292 (insert-file-contents file
)
1294 (while (search-forward "\x1f" nil
'move
)
1295 (if (= (following-char) ?S
)
1296 (delete-region (1- (point)) (line-end-position))
1302 (insert (if (= (following-char) ?F
) "Function " "Variable "))
1305 (insert ".DS L\n"))))
1307 (setq buffer-undo-list nil
)
1310 ;; Replaces lib-src/sorted-doc.c.
1312 (defun doc-file-to-info (file)
1313 "Produce a texinfo buffer with sorted doc-strings from the DOC file."
1314 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1315 internal-doc-file-name t
)))
1316 (or (file-readable-p file
)
1317 (error "Cannot read file `%s'" file
))
1318 (let ((i 0) type name doc alist
)
1320 (insert-file-contents file
)
1321 ;; The characters "@{}" need special treatment.
1322 (while (re-search-forward "[@{}]" nil t
)
1326 (goto-char (point-min))
1327 (while (search-forward "\x1f" nil t
)
1328 (when (/= (following-char) ?S
)
1329 (setq type
(char-after)
1330 name
(buffer-substring (1+ (point)) (line-end-position))
1331 doc
(buffer-substring (line-beginning-position 2)
1332 (if (search-forward "\x1f" nil
'move
)
1335 alist
(cons (list name type doc
) alist
))
1336 (backward-char 1))))
1337 (pop-to-buffer (generate-new-buffer "*info-doc*"))
1338 (setq buffer-undo-list t
)
1339 ;; Write the output header.
1340 (insert "\\input texinfo @c -*-texinfo-*-\n"
1341 "@setfilename emacsdoc.info\n"
1342 "@settitle Command Summary for GNU Emacs\n"
1345 "@unnumbered Command Summary for GNU Emacs\n\n"
1348 "@global@let@ITEM@item\n"
1349 "@def@item{@filbreak@vskip5pt@ITEM}\n"
1350 "@font@tensy cmsy10 scaled @magstephalf\n"
1351 "@font@teni cmmi10 scaled @magstephalf\n"
1352 "@def\\{{@tensy@char110}}\n" ; this backslash goes with cmr10
1353 "@def|{{@tensy@char106}}\n"
1354 "@def@{{{@tensy@char102}}\n"
1355 "@def@}{{@tensy@char103}}\n"
1356 "@def<{{@teni@char62}}\n"
1357 "@def>{{@teni@char60}}\n"
1360 "@tableindent-0.2in\n"
1362 ;; Sort the array by name; within each name, by type (functions first).
1363 (setq alist
(sort alist
(lambda (e1 e2
)
1364 (if (string-equal (car e1
) (car e2
))
1365 (<= (cadr e1
) (cadr e2
))
1366 (string-lessp (car e1
) (car e2
))))))
1367 ;; Print each function.
1370 (if (char-equal (cadr e
) ?\F
) "Function" "Variable")
1371 " @code{" (car e
) "}\n@display\n"
1374 ;; Try to avoid a save size overflow in the TeX output routine.
1375 (if (zerop (setq i
(%
(1+ i
) 100)))
1376 (insert "\n@end table\n@table @asis\n")))
1377 (insert "@end table\n"
1379 (setq buffer-undo-list nil
)
1384 ;;; help-fns.el ends here