* lisp/emacs-lisp/package.el (package--with-work-buffer-async):
[emacs.git] / lisp / help-fns.el
blob1c7a68abdecd9a511eac391055cce9d16ea0dd3d
1 ;;; help-fns.el --- Complex help functions -*- lexical-binding: t -*-
3 ;; Copyright (C) 1985-1986, 1993-1994, 1998-2015 Free Software
4 ;; Foundation, Inc.
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: help, internal
8 ;; Package: emacs
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/>.
25 ;;; Commentary:
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
31 ;; are in help.el
33 ;;; Code:
35 (require 'cl-lib)
36 (require 'help-mode)
38 (defvar help-fns-describe-function-functions nil
39 "List of functions to run in help buffer in `describe-function'.
40 Those functions will be run after the header line and argument
41 list was inserted, and before the documentation will be inserted.
42 The functions will receive the function name as argument.")
44 ;; Functions
46 ;;;###autoload
47 (defun describe-function (function)
48 "Display the full documentation of FUNCTION (a symbol)."
49 (interactive
50 (let ((fn (function-called-at-point))
51 (enable-recursive-minibuffers t)
52 val)
53 (setq val (completing-read (if fn
54 (format "Describe function (default %s): " fn)
55 "Describe function: ")
56 obarray 'fboundp t nil nil
57 (and fn (symbol-name fn))))
58 (list (if (equal val "")
59 fn (intern val)))))
60 (or (and function (symbolp function))
61 (user-error "You didn't specify a function symbol"))
62 (or (fboundp function)
63 (user-error "Symbol's function definition is void: %s" function))
64 (help-setup-xref (list #'describe-function function)
65 (called-interactively-p 'interactive))
66 (save-excursion
67 (with-help-window (help-buffer)
68 (prin1 function)
69 ;; Use " is " instead of a colon so that
70 ;; it is easier to get out the function name using forward-sexp.
71 (princ " is ")
72 (describe-function-1 function)
73 (with-current-buffer standard-output
74 ;; Return the text we displayed.
75 (buffer-string)))))
78 ;; Could be this, if we make symbol-file do the work below.
79 ;; (defun help-C-file-name (subr-or-var kind)
80 ;; "Return the name of the C file where SUBR-OR-VAR is defined.
81 ;; KIND should be `var' for a variable or `subr' for a subroutine."
82 ;; (symbol-file (if (symbolp subr-or-var) subr-or-var
83 ;; (subr-name subr-or-var))
84 ;; (if (eq kind 'var) 'defvar 'defun)))
85 ;;;###autoload
86 (defun help-C-file-name (subr-or-var kind)
87 "Return the name of the C file where SUBR-OR-VAR is defined.
88 KIND should be `var' for a variable or `subr' for a subroutine."
89 (let ((docbuf (get-buffer-create " *DOC*"))
90 (name (if (eq 'var kind)
91 (concat "V" (symbol-name subr-or-var))
92 (concat "F" (subr-name (advice--cd*r subr-or-var))))))
93 (with-current-buffer docbuf
94 (goto-char (point-min))
95 (if (eobp)
96 (insert-file-contents-literally
97 (expand-file-name internal-doc-file-name doc-directory)))
98 (let ((file (catch 'loop
99 (while t
100 (let ((pnt (search-forward (concat "\x1f" name "\n"))))
101 (re-search-backward "\x1fS\\(.*\\)")
102 (let ((file (match-string 1)))
103 (if (member file build-files)
104 (throw 'loop file)
105 (goto-char pnt))))))))
106 (if (string-match "^ns.*\\(\\.o\\|obj\\)\\'" file)
107 (setq file (replace-match ".m" t t file 1))
108 (if (string-match "\\.\\(o\\|obj\\)\\'" file)
109 (setq file (replace-match ".c" t t file))))
110 (if (string-match "\\.\\(c\\|m\\)\\'" file)
111 (concat "src/" file)
112 file)))))
114 (defcustom help-downcase-arguments nil
115 "If non-nil, argument names in *Help* buffers are downcased."
116 :type 'boolean
117 :group 'help
118 :version "23.2")
120 (defun help-highlight-arg (arg)
121 "Highlight ARG as an argument name for a *Help* buffer.
122 Return ARG in face `help-argument-name'; ARG is also downcased
123 if the variable `help-downcase-arguments' is non-nil."
124 (propertize (if help-downcase-arguments (downcase arg) arg)
125 'face 'help-argument-name))
127 (defun help-do-arg-highlight (doc args)
128 (with-syntax-table (make-syntax-table emacs-lisp-mode-syntax-table)
129 (modify-syntax-entry ?\- "w")
130 (dolist (arg args)
131 (setq doc (replace-regexp-in-string
132 ;; This is heuristic, but covers all common cases
133 ;; except ARG1-ARG2
134 (concat "\\<" ; beginning of word
135 "\\(?:[a-z-]*-\\)?" ; for xxx-ARG
136 "\\("
137 (regexp-quote arg)
138 "\\)"
139 "\\(?:es\\|s\\|th\\)?" ; for ARGth, ARGs
140 "\\(?:-[a-z0-9-]+\\)?" ; for ARG-xxx, ARG-n
141 "\\(?:-[{([<`\"‘].*?\\)?"; for ARG-{x}, (x), <x>, [x], `x', ‘x’
142 "\\>") ; end of word
143 (help-highlight-arg arg)
144 doc t t 1)))
145 doc))
147 (defun help-highlight-arguments (usage doc &rest args)
148 (when (and usage (string-match "^(" usage))
149 (with-temp-buffer
150 (insert usage)
151 (goto-char (point-min))
152 (let ((case-fold-search nil)
153 (next (not (or args (looking-at "\\["))))
154 (opt nil))
155 ;; Make a list of all arguments
156 (skip-chars-forward "^ ")
157 (while next
158 (or opt (not (looking-at " &")) (setq opt t))
159 (if (not (re-search-forward " \\([\\[(]*\\)\\([^] &)\.]+\\)" nil t))
160 (setq next nil)
161 (setq args (cons (match-string 2) args))
162 (when (and opt (string= (match-string 1) "("))
163 ;; A pesky CL-style optional argument with default value,
164 ;; so let's skip over it
165 (search-backward "(")
166 (goto-char (scan-sexps (point) 1)))))
167 ;; Highlight arguments in the USAGE string
168 (setq usage (help-do-arg-highlight (buffer-string) args))
169 ;; Highlight arguments in the DOC string
170 (setq doc (and doc (help-do-arg-highlight doc args))))))
171 ;; Return value is like the one from help-split-fundoc, but highlighted
172 (cons usage doc))
174 ;; The following function was compiled from the former functions
175 ;; `describe-simplify-lib-file-name' and `find-source-lisp-file' with
176 ;; some excerpts from `describe-function-1' and `describe-variable'.
177 ;; The only additional twists provided are (1) locate the defining file
178 ;; for autoloaded functions, and (2) give preference to files in the
179 ;; "install directory" (directories found via `load-path') rather than
180 ;; to files in the "compile directory" (directories found by searching
181 ;; the loaddefs.el file). We autoload it because it's also used by
182 ;; `describe-face' (instead of `describe-simplify-lib-file-name').
184 ;;;###autoload
185 (defun find-lisp-object-file-name (object type)
186 "Guess the file that defined the Lisp object OBJECT, of type TYPE.
187 OBJECT should be a symbol associated with a function, variable, or face;
188 alternatively, it can be a function definition.
189 If TYPE is `defvar', search for a variable definition.
190 If TYPE is `defface', search for a face definition.
191 If TYPE is not a symbol, search for a function definition.
193 The return value is the absolute name of a readable file where OBJECT is
194 defined. If several such files exist, preference is given to a file
195 found via `load-path'. The return value can also be `C-source', which
196 means that OBJECT is a function or variable defined in C. If no
197 suitable file is found, return nil."
198 (let* ((autoloaded (autoloadp type))
199 (file-name (or (and autoloaded (nth 1 type))
200 (symbol-file
201 ;; FIXME: Why do we have this weird "If TYPE is the
202 ;; value returned by `symbol-function' for a function
203 ;; symbol" exception?
204 object (or (if (symbolp type) type) 'defun)))))
205 (cond
206 (autoloaded
207 ;; An autoloaded function: Locate the file since `symbol-function'
208 ;; has only returned a bare string here.
209 (setq file-name
210 (locate-file file-name load-path '(".el" ".elc") 'readable)))
211 ((and (stringp file-name)
212 (string-match "[.]*loaddefs.el\\'" file-name))
213 ;; An autoloaded variable or face. Visit loaddefs.el in a buffer
214 ;; and try to extract the defining file. The following form is
215 ;; from `describe-function-1' and `describe-variable'.
216 (let ((location
217 (condition-case nil
218 (find-function-search-for-symbol object nil file-name)
219 (error nil))))
220 (when (cdr location)
221 (with-current-buffer (car location)
222 (goto-char (cdr location))
223 (when (re-search-backward
224 "^;;; Generated autoloads from \\(.*\\)" nil t)
225 (setq file-name
226 (locate-file
227 (file-name-sans-extension
228 (match-string-no-properties 1))
229 load-path '(".el" ".elc") 'readable))))))))
231 (cond
232 ((and (not file-name) (subrp type))
233 ;; A built-in function. The form is from `describe-function-1'.
234 (if (get-buffer " *DOC*")
235 (help-C-file-name type 'subr)
236 'C-source))
237 ((and (not file-name) (symbolp object)
238 (integerp (get object 'variable-documentation)))
239 ;; A variable defined in C. The form is from `describe-variable'.
240 (if (get-buffer " *DOC*")
241 (help-C-file-name object 'var)
242 'C-source))
243 ((not (stringp file-name))
244 ;; If we don't have a file-name string by now, we lost.
245 nil)
246 ;; Now, `file-name' should have become an absolute file name.
247 ;; For files loaded from ~/.foo.elc, try ~/.foo.
248 ;; This applies to config files like ~/.emacs,
249 ;; which people sometimes compile.
250 ((let (fn)
251 (and (string-match "\\`\\..*\\.elc\\'"
252 (file-name-nondirectory file-name))
253 (string-equal (file-name-directory file-name)
254 (file-name-as-directory (expand-file-name "~")))
255 (file-readable-p (setq fn (file-name-sans-extension file-name)))
256 fn)))
257 ;; When the Elisp source file can be found in the install
258 ;; directory, return the name of that file.
259 ((let ((lib-name
260 (if (string-match "[.]elc\\'" file-name)
261 (substring-no-properties file-name 0 -1)
262 file-name)))
263 (or (and (file-readable-p lib-name) lib-name)
264 ;; The library might be compressed.
265 (and (file-readable-p (concat lib-name ".gz")) lib-name))))
266 ((let* ((lib-name (file-name-nondirectory file-name))
267 ;; The next form is from `describe-simplify-lib-file-name'.
268 (file-name
269 ;; Try converting the absolute file name to a library
270 ;; name, convert that back to a file name and see if we
271 ;; get the original one. If so, they are equivalent.
272 (if (equal file-name (locate-file lib-name load-path '("")))
273 (if (string-match "[.]elc\\'" lib-name)
274 (substring-no-properties lib-name 0 -1)
275 lib-name)
276 file-name))
277 ;; The next three forms are from `find-source-lisp-file'.
278 (elc-file (locate-file
279 (concat file-name
280 (if (string-match "\\.el\\'" file-name)
282 ".elc"))
283 load-path nil 'readable))
284 (str (when elc-file
285 (with-temp-buffer
286 (insert-file-contents-literally elc-file nil 0 256)
287 (buffer-string))))
288 (src-file (and str
289 (string-match ";;; from file \\(.*\\.el\\)" str)
290 (match-string 1 str))))
291 (and src-file (file-readable-p src-file) src-file))))))
293 (defun help-fns--key-bindings (function)
294 (when (commandp function)
295 (let ((pt2 (with-current-buffer standard-output (point)))
296 (remapped (command-remapping function)))
297 (unless (memq remapped '(ignore undefined))
298 (let ((keys (where-is-internal
299 (or remapped function) overriding-local-map nil nil))
300 non-modified-keys)
301 (if (and (eq function 'self-insert-command)
302 (vectorp (car-safe keys))
303 (consp (aref (car keys) 0)))
304 (princ "It is bound to many ordinary text characters.\n")
305 ;; Which non-control non-meta keys run this command?
306 (dolist (key keys)
307 (if (member (event-modifiers (aref key 0)) '(nil (shift)))
308 (push key non-modified-keys)))
309 (when remapped
310 (princ "Its keys are remapped to ")
311 (princ (if (symbolp remapped)
312 (concat (substitute-command-keys "‘")
313 (symbol-name remapped)
314 (substitute-command-keys "’"))
315 "an anonymous command"))
316 (princ ".\n"))
318 (when keys
319 (princ (if remapped
320 "Without this remapping, it would be bound to "
321 "It is bound to "))
322 ;; If lots of ordinary text characters run this command,
323 ;; don't mention them one by one.
324 (if (< (length non-modified-keys) 10)
325 (princ (mapconcat 'key-description keys ", "))
326 (dolist (key non-modified-keys)
327 (setq keys (delq key keys)))
328 (if keys
329 (progn
330 (princ (mapconcat 'key-description keys ", "))
331 (princ ", and many ordinary text characters"))
332 (princ "many ordinary text characters"))))
333 (when (or remapped keys non-modified-keys)
334 (princ ".")
335 (terpri)))))
337 (with-current-buffer standard-output
338 (fill-region-as-paragraph pt2 (point))
339 (unless (looking-back "\n\n" (- (point) 2))
340 (terpri))))))
342 (defun help-fns--compiler-macro (function)
343 (let ((handler (function-get function 'compiler-macro)))
344 (when handler
345 (insert "\nThis function has a compiler macro")
346 (if (symbolp handler)
347 (progn
348 (insert (format (substitute-command-keys " ‘%s’") handler))
349 (save-excursion
350 (re-search-backward (substitute-command-keys "‘\\([^‘’]+\\)’")
351 nil t)
352 (help-xref-button 1 'help-function handler)))
353 ;; FIXME: Obsolete since 24.4.
354 (let ((lib (get function 'compiler-macro-file)))
355 (when (stringp lib)
356 (insert (format (substitute-command-keys " in ‘%s’") lib))
357 (save-excursion
358 (re-search-backward (substitute-command-keys "‘\\([^‘’]+\\)’")
359 nil t)
360 (help-xref-button 1 'help-function-cmacro function lib)))))
361 (insert ".\n"))))
363 (defun help-fns--signature (function doc real-def real-function raw)
364 "Insert usage at point and return docstring. With highlighting."
365 (if (keymapp function)
366 doc ; If definition is a keymap, skip arglist note.
367 (let* ((advertised (gethash real-def advertised-signature-table t))
368 (arglist (if (listp advertised)
369 advertised (help-function-arglist real-def)))
370 (usage (help-split-fundoc doc function)))
371 (if usage (setq doc (cdr usage)))
372 (let* ((use (cond
373 ((and usage (not (listp advertised))) (car usage))
374 ((listp arglist)
375 (help--make-usage-docstring function arglist))
376 ((stringp arglist) arglist)
377 ;; Maybe the arglist is in the docstring of a symbol
378 ;; this one is aliased to.
379 ((let ((fun real-function))
380 (while (and (symbolp fun)
381 (setq fun (symbol-function fun))
382 (not (setq usage (help-split-fundoc
383 (documentation fun)
384 function)))))
385 usage)
386 (car usage))
387 ((or (stringp real-def)
388 (vectorp real-def))
389 (format "\nMacro: %s"
390 (help--docstring-quote
391 (format-kbd-macro real-def))))
392 (t "[Missing arglist. Please make a bug report.]")))
393 ;; Insert "`X", not "(\` X)", when documenting `X.
394 (use1 (replace-regexp-in-string
395 "\\`(\\\\=\\\\\\\\=` \\([^\n ]*\\))\\'"
396 "\\\\=`\\1" use t))
397 (high (if raw
398 (cons use1 doc)
399 (help-highlight-arguments (substitute-command-keys use1)
400 (substitute-command-keys doc)))))
401 (let ((fill-begin (point))
402 (high-usage (car high))
403 (high-doc (cdr high)))
404 (insert high-usage "\n")
405 (fill-region fill-begin (point))
406 high-doc)))))
408 (defun help-fns--parent-mode (function)
409 ;; If this is a derived mode, link to the parent.
410 (let ((parent-mode (and (symbolp function)
411 (get function
412 'derived-mode-parent))))
413 (when parent-mode
414 (insert (substitute-command-keys "\nParent mode: ‘"))
415 (let ((beg (point)))
416 (insert (format "%s" parent-mode))
417 (make-text-button beg (point)
418 'type 'help-function
419 'help-args (list parent-mode)))
420 (insert (substitute-command-keys "’.\n")))))
422 (defun help-fns--obsolete (function)
423 ;; Ignore lambda constructs, keyboard macros, etc.
424 (let* ((obsolete (and (symbolp function)
425 (get function 'byte-obsolete-info)))
426 (use (car obsolete)))
427 (when obsolete
428 (insert "\nThis "
429 (if (eq (car-safe (symbol-function function)) 'macro)
430 "macro"
431 "function")
432 " is obsolete")
433 (when (nth 2 obsolete)
434 (insert (format " since %s" (nth 2 obsolete))))
435 (insert (cond ((stringp use) (concat ";\n" use))
436 (use (format (substitute-command-keys
437 ";\nuse ‘%s’ instead.")
438 use))
439 (t "."))
440 "\n"))))
442 ;; We could use `symbol-file' but this is a wee bit more efficient.
443 (defun help-fns--autoloaded-p (function file)
444 "Return non-nil if FUNCTION has previously been autoloaded.
445 FILE is the file where FUNCTION was probably defined."
446 (let* ((file (file-name-sans-extension (file-truename file)))
447 (load-hist load-history)
448 (target (cons t function))
449 found)
450 (while (and load-hist (not found))
451 (and (caar load-hist)
452 (equal (file-name-sans-extension (caar load-hist)) file)
453 (setq found (member target (cdar load-hist))))
454 (setq load-hist (cdr load-hist)))
455 found))
457 (defun help-fns--interactive-only (function)
458 "Insert some help blurb if FUNCTION should only be used interactively."
459 ;; Ignore lambda constructs, keyboard macros, etc.
460 (and (symbolp function)
461 (not (eq (car-safe (symbol-function function)) 'macro))
462 (let* ((interactive-only
463 (or (get function 'interactive-only)
464 (if (boundp 'byte-compile-interactive-only-functions)
465 (memq function
466 byte-compile-interactive-only-functions)))))
467 (when interactive-only
468 (insert "\nThis function is for interactive use only"
469 ;; Cf byte-compile-form.
470 (cond ((stringp interactive-only)
471 (format ";\nin Lisp code %s" interactive-only))
472 ((and (symbolp 'interactive-only)
473 (not (eq interactive-only t)))
474 (format (substitute-command-keys
475 ";\nin Lisp code use ‘%s’ instead.")
476 interactive-only))
477 (t "."))
478 "\n")))))
480 (defun help-fns-short-filename (filename)
481 (let* ((abbrev (abbreviate-file-name filename))
482 (short abbrev))
483 (dolist (dir load-path)
484 (let ((rel (file-relative-name filename dir)))
485 (if (< (length rel) (length short))
486 (setq short rel)))
487 (let ((rel (file-relative-name abbrev dir)))
488 (if (< (length rel) (length short))
489 (setq short rel))))
490 short))
492 ;;;###autoload
493 (defun describe-function-1 (function)
494 (let* ((advised (and (symbolp function)
495 (featurep 'nadvice)
496 (advice--p (advice--symbol-function function))))
497 ;; If the function is advised, use the symbol that has the
498 ;; real definition, if that symbol is already set up.
499 (real-function
500 (or (and advised
501 (advice--cd*r (advice--symbol-function function)))
502 function))
503 ;; Get the real definition.
504 (def (if (symbolp real-function)
505 (or (symbol-function real-function)
506 (signal 'void-function (list real-function)))
507 real-function))
508 (aliased (or (symbolp def)
509 ;; Advised & aliased function.
510 (and advised (symbolp real-function))))
511 (real-def (cond
512 (aliased (let ((f real-function))
513 (while (and (fboundp f)
514 (symbolp (symbol-function f)))
515 (setq f (symbol-function f)))
517 ((subrp def) (intern (subr-name def)))
518 (t def)))
519 (sig-key (if (subrp def)
520 (indirect-function real-def)
521 real-def))
522 (file-name (find-lisp-object-file-name function def))
523 (pt1 (with-current-buffer (help-buffer) (point)))
524 (beg (if (and (or (byte-code-function-p def)
525 (keymapp def)
526 (memq (car-safe def) '(macro lambda closure)))
527 (stringp file-name)
528 (help-fns--autoloaded-p function file-name))
529 (if (commandp def)
530 "an interactive autoloaded "
531 "an autoloaded ")
532 (if (commandp def) "an interactive " "a "))))
534 ;; Print what kind of function-like object FUNCTION is.
535 (princ (cond ((or (stringp def) (vectorp def))
536 "a keyboard macro")
537 ((subrp def)
538 (if (eq 'unevalled (cdr (subr-arity def)))
539 (concat beg "special form")
540 (concat beg "built-in function")))
541 ;; Aliases are Lisp functions, so we need to check
542 ;; aliases before functions.
543 (aliased
544 (format (substitute-command-keys "an alias for ‘%s’")
545 real-def))
546 ((autoloadp def)
547 (format "%s autoloaded %s"
548 (if (commandp def) "an interactive" "an")
549 (if (eq (nth 4 def) 'keymap) "keymap"
550 (if (nth 4 def) "Lisp macro" "Lisp function"))))
551 ((or (eq (car-safe def) 'macro)
552 ;; For advised macros, def is a lambda
553 ;; expression or a byte-code-function-p, so we
554 ;; need to check macros before functions.
555 (macrop function))
556 (concat beg "Lisp macro"))
557 ((byte-code-function-p def)
558 (concat beg "compiled Lisp function"))
559 ((eq (car-safe def) 'lambda)
560 (concat beg "Lisp function"))
561 ((eq (car-safe def) 'closure)
562 (concat beg "Lisp closure"))
563 ((keymapp def)
564 (let ((is-full nil)
565 (elts (cdr-safe def)))
566 (while elts
567 (if (char-table-p (car-safe elts))
568 (setq is-full t
569 elts nil))
570 (setq elts (cdr-safe elts)))
571 (concat beg (if is-full "keymap" "sparse keymap"))))
572 (t "")))
574 (if (and aliased (not (fboundp real-def)))
575 (princ ",\nwhich is not defined. Please make a bug report.")
576 (with-current-buffer standard-output
577 (save-excursion
578 (save-match-data
579 (when (re-search-backward (substitute-command-keys
580 "alias for ‘\\([^‘’]+\\)’")
581 nil t)
582 (help-xref-button 1 'help-function real-def)))))
584 (when file-name
585 (princ (substitute-command-keys " in ‘"))
586 ;; We used to add .el to the file name,
587 ;; but that's completely wrong when the user used load-file.
588 (princ (if (eq file-name 'C-source)
589 "C source code"
590 (help-fns-short-filename file-name)))
591 (princ (substitute-command-keys "’"))
592 ;; Make a hyperlink to the library.
593 (with-current-buffer standard-output
594 (save-excursion
595 (re-search-backward (substitute-command-keys "‘\\([^‘’]+\\)’")
596 nil t)
597 (help-xref-button 1 'help-function-def function file-name))))
598 (princ ".")
599 (with-current-buffer (help-buffer)
600 (fill-region-as-paragraph (save-excursion (goto-char pt1) (forward-line 0) (point))
601 (point)))
602 (terpri)(terpri)
604 (let ((doc-raw (documentation function t)))
606 ;; If the function is autoloaded, and its docstring has
607 ;; key substitution constructs, load the library.
608 (and (autoloadp real-def) doc-raw
609 help-enable-auto-load
610 (string-match "\\([^\\]=\\|[^=]\\|\\`\\)\\\\[[{<]" doc-raw)
611 (autoload-do-load real-def))
613 (help-fns--key-bindings function)
614 (with-current-buffer standard-output
615 (let ((doc (help-fns--signature function doc-raw sig-key
616 real-function nil)))
617 (run-hook-with-args 'help-fns-describe-function-functions function)
618 (insert "\n"
619 (or doc "Not documented."))))))))
621 ;; Add defaults to `help-fns-describe-function-functions'.
622 (add-hook 'help-fns-describe-function-functions #'help-fns--obsolete)
623 (add-hook 'help-fns-describe-function-functions #'help-fns--interactive-only)
624 (add-hook 'help-fns-describe-function-functions #'help-fns--parent-mode)
625 (add-hook 'help-fns-describe-function-functions #'help-fns--compiler-macro)
628 ;; Variables
630 ;;;###autoload
631 (defun variable-at-point (&optional any-symbol)
632 "Return the bound variable symbol found at or before point.
633 Return 0 if there is no such symbol.
634 If ANY-SYMBOL is non-nil, don't insist the symbol be bound."
635 (with-syntax-table emacs-lisp-mode-syntax-table
636 (or (condition-case ()
637 (save-excursion
638 (skip-chars-forward "'")
639 (or (not (zerop (skip-syntax-backward "_w")))
640 (eq (char-syntax (following-char)) ?w)
641 (eq (char-syntax (following-char)) ?_)
642 (forward-sexp -1))
643 (skip-chars-forward "'")
644 (let ((obj (read (current-buffer))))
645 (and (symbolp obj) (boundp obj) obj)))
646 (error nil))
647 (let* ((str (find-tag-default))
648 (sym (if str (intern-soft str))))
649 (if (and sym (or any-symbol (boundp sym)))
651 (save-match-data
652 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
653 (setq sym (intern-soft (match-string 1 str)))
654 (and (or any-symbol (boundp sym)) sym)))))
655 0)))
657 (defun describe-variable-custom-version-info (variable)
658 (let ((custom-version (get variable 'custom-version))
659 (cpv (get variable 'custom-package-version))
660 (output nil))
661 (if custom-version
662 (setq output
663 (format "This variable was introduced, or its default value was changed, in\nversion %s of Emacs.\n"
664 custom-version))
665 (when cpv
666 (let* ((package (car-safe cpv))
667 (version (if (listp (cdr-safe cpv))
668 (car (cdr-safe cpv))
669 (cdr-safe cpv)))
670 (pkg-versions (assq package customize-package-emacs-version-alist))
671 (emacsv (cdr (assoc version pkg-versions))))
672 (if (and package version)
673 (setq output
674 (format (concat "This variable was introduced, or its default value was changed, in\nversion %s of the %s package"
675 (if emacsv
676 (format " that is part of Emacs %s" emacsv))
677 ".\n")
678 version package))))))
679 output))
681 ;;;###autoload
682 (defun describe-variable (variable &optional buffer frame)
683 "Display the full documentation of VARIABLE (a symbol).
684 Returns the documentation as a string, also.
685 If VARIABLE has a buffer-local value in BUFFER or FRAME
686 \(default to the current buffer and current frame),
687 it is displayed along with the global value."
688 (interactive
689 (let ((v (variable-at-point))
690 (enable-recursive-minibuffers t)
691 val)
692 (setq val (completing-read (if (symbolp v)
693 (format
694 "Describe variable (default %s): " v)
695 "Describe variable: ")
696 obarray
697 (lambda (vv)
698 (or (get vv 'variable-documentation)
699 (and (boundp vv) (not (keywordp vv)))))
700 t nil nil
701 (if (symbolp v) (symbol-name v))))
702 (list (if (equal val "")
703 v (intern val)))))
704 (let (file-name)
705 (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
706 (unless (frame-live-p frame) (setq frame (selected-frame)))
707 (if (not (symbolp variable))
708 (message "You did not specify a variable")
709 (save-excursion
710 (let ((valvoid (not (with-current-buffer buffer (boundp variable))))
711 (permanent-local (get variable 'permanent-local))
712 val val-start-pos locus)
713 ;; Extract the value before setting up the output buffer,
714 ;; in case `buffer' *is* the output buffer.
715 (unless valvoid
716 (with-selected-frame frame
717 (with-current-buffer buffer
718 (setq val (symbol-value variable)
719 locus (variable-binding-locus variable)))))
720 (help-setup-xref (list #'describe-variable variable buffer)
721 (called-interactively-p 'interactive))
722 (with-help-window (help-buffer)
723 (with-current-buffer buffer
724 (prin1 variable)
725 (setq file-name (find-lisp-object-file-name variable 'defvar))
727 (if file-name
728 (progn
729 (princ (substitute-command-keys
730 " is a variable defined in ‘"))
731 (princ (if (eq file-name 'C-source)
732 "C source code"
733 (file-name-nondirectory file-name)))
734 (princ (substitute-command-keys "’.\n"))
735 (with-current-buffer standard-output
736 (save-excursion
737 (re-search-backward (substitute-command-keys
738 "‘\\([^‘’]+\\)’")
739 nil t)
740 (help-xref-button 1 'help-variable-def
741 variable file-name)))
742 (if valvoid
743 (princ "It is void as a variable.")
744 (princ "Its ")))
745 (if valvoid
746 (princ " is void as a variable.")
747 (princ "'s "))))
748 (unless valvoid
749 (with-current-buffer standard-output
750 (setq val-start-pos (point))
751 (princ "value is ")
752 (let ((from (point))
753 (line-beg (line-beginning-position))
754 (print-rep
755 (let ((print-quoted t))
756 (prin1-to-string val))))
757 (if (< (+ (length print-rep) (point) (- line-beg)) 68)
758 (insert print-rep)
759 (terpri)
760 (pp val)
761 (if (< (point) (+ 68 (line-beginning-position 0)))
762 (delete-region from (1+ from))
763 (delete-region (1- from) from)))
764 (let* ((sv (get variable 'standard-value))
765 (origval (and (consp sv)
766 (condition-case nil
767 (eval (car sv))
768 (error :help-eval-error)))))
769 (when (and (consp sv)
770 (not (equal origval val))
771 (not (equal origval :help-eval-error)))
772 (princ "\nOriginal value was \n")
773 (setq from (point))
774 (pp origval)
775 (if (< (point) (+ from 20))
776 (delete-region (1- from) from)))))))
777 (terpri)
778 (when locus
779 (cond
780 ((bufferp locus)
781 (princ (format "Local in buffer %s; "
782 (buffer-name buffer))))
783 ((framep locus)
784 (princ (format "It is a frame-local variable; ")))
785 ((terminal-live-p locus)
786 (princ (format "It is a terminal-local variable; ")))
788 (princ (format "It is local to %S" locus))))
789 (if (not (default-boundp variable))
790 (princ "globally void")
791 (let ((global-val (default-value variable)))
792 (with-current-buffer standard-output
793 (princ "global value is ")
794 (if (eq val global-val)
795 (princ "the same.")
796 (terpri)
797 ;; Fixme: pp can take an age if you happen to
798 ;; ask for a very large expression. We should
799 ;; probably print it raw once and check it's a
800 ;; sensible size before prettyprinting. -- fx
801 (let ((from (point)))
802 (pp global-val)
803 ;; See previous comment for this function.
804 ;; (help-xref-on-pp from (point))
805 (if (< (point) (+ from 20))
806 (delete-region (1- from) from)))))))
807 (terpri))
809 ;; If the value is large, move it to the end.
810 (with-current-buffer standard-output
811 (when (> (count-lines (point-min) (point-max)) 10)
812 ;; Note that setting the syntax table like below
813 ;; makes forward-sexp move over a `'s' at the end
814 ;; of a symbol.
815 (set-syntax-table emacs-lisp-mode-syntax-table)
816 (goto-char val-start-pos)
817 ;; The line below previously read as
818 ;; (delete-region (point) (progn (end-of-line) (point)))
819 ;; which suppressed display of the buffer local value for
820 ;; large values.
821 (when (looking-at "value is") (replace-match ""))
822 (save-excursion
823 (insert "\n\nValue:")
824 (set (make-local-variable 'help-button-cache)
825 (point-marker)))
826 (insert "value is shown ")
827 (insert-button "below"
828 'action help-button-cache
829 'follow-link t
830 'help-echo "mouse-2, RET: show value")
831 (insert ".\n")))
832 (terpri)
834 (let* ((alias (condition-case nil
835 (indirect-variable variable)
836 (error variable)))
837 (obsolete (get variable 'byte-obsolete-variable))
838 (use (car obsolete))
839 (safe-var (get variable 'safe-local-variable))
840 (doc (or (documentation-property
841 variable 'variable-documentation)
842 (documentation-property
843 alias 'variable-documentation)))
844 (extra-line nil))
846 ;; Mention if it's a local variable.
847 (cond
848 ((and (local-variable-if-set-p variable)
849 (or (not (local-variable-p variable))
850 (with-temp-buffer
851 (local-variable-if-set-p variable))))
852 (setq extra-line t)
853 (princ " Automatically becomes ")
854 (if permanent-local
855 (princ "permanently "))
856 (princ "buffer-local when set.\n"))
857 ((not permanent-local))
858 ((bufferp locus)
859 (setq extra-line t)
860 (princ " This variable's buffer-local value is permanent.\n"))
862 (setq extra-line t)
863 (princ " This variable's value is permanent \
864 if it is given a local binding.\n")))
866 ;; Mention if it's an alias.
867 (unless (eq alias variable)
868 (setq extra-line t)
869 (princ (format (substitute-command-keys
870 " This variable is an alias for ‘%s’.\n")
871 alias)))
873 (when obsolete
874 (setq extra-line t)
875 (princ " This variable is obsolete")
876 (if (nth 2 obsolete)
877 (princ (format " since %s" (nth 2 obsolete))))
878 (princ (cond ((stringp use) (concat ";\n " use))
879 (use (format (substitute-command-keys
880 ";\n use ‘%s’ instead.")
881 (car obsolete)))
882 (t ".")))
883 (terpri))
885 (when (member (cons variable val)
886 (with-current-buffer buffer
887 file-local-variables-alist))
888 (setq extra-line t)
889 (if (member (cons variable val)
890 (with-current-buffer buffer
891 dir-local-variables-alist))
892 (let ((file (and (buffer-file-name buffer)
893 (not (file-remote-p
894 (buffer-file-name buffer)))
895 (dir-locals-find-file
896 (buffer-file-name buffer))))
897 (dir-file t))
898 (princ " This variable's value is directory-local")
899 (if (null file)
900 (princ ".\n")
901 (princ ", set ")
902 (if (consp file) ; result from cache
903 ;; If the cache element has an mtime, we
904 ;; assume it came from a file.
905 (if (nth 2 file)
906 (setq file (expand-file-name
907 dir-locals-file (car file)))
908 ;; Otherwise, assume it was set directly.
909 (setq file (car file)
910 dir-file nil)))
911 (princ (substitute-command-keys
912 (if dir-file
913 "by the file\n ‘"
914 "for the directory\n ‘")))
915 (with-current-buffer standard-output
916 (insert-text-button
917 file 'type 'help-dir-local-var-def
918 'help-args (list variable file)))
919 (princ (substitute-command-keys "’.\n"))))
920 (princ " This variable's value is file-local.\n")))
922 (when (memq variable ignored-local-variables)
923 (setq extra-line t)
924 (princ " This variable is ignored as a file-local \
925 variable.\n"))
927 ;; Can be both risky and safe, eg auto-fill-function.
928 (when (risky-local-variable-p variable)
929 (setq extra-line t)
930 (princ " This variable may be risky if used as a \
931 file-local variable.\n")
932 (when (assq variable safe-local-variable-values)
933 (princ (substitute-command-keys
934 " However, you have added it to \
935 ‘safe-local-variable-values’.\n"))))
937 (when safe-var
938 (setq extra-line t)
939 (princ " This variable is safe as a file local variable ")
940 (princ "if its value\n satisfies the predicate ")
941 (princ (if (byte-code-function-p safe-var)
942 "which is a byte-compiled expression.\n"
943 (format (substitute-command-keys "‘%s’.\n")
944 safe-var))))
946 (if extra-line (terpri))
947 (princ "Documentation:\n")
948 (with-current-buffer standard-output
949 (insert (or doc "Not documented as a variable."))))
951 ;; Make a link to customize if this variable can be customized.
952 (when (custom-variable-p variable)
953 (let ((customize-label "customize"))
954 (terpri)
955 (terpri)
956 (princ (concat "You can " customize-label " this variable."))
957 (with-current-buffer standard-output
958 (save-excursion
959 (re-search-backward
960 (concat "\\(" customize-label "\\)") nil t)
961 (help-xref-button 1 'help-customize-variable variable))))
962 ;; Note variable's version or package version
963 (let ((output (describe-variable-custom-version-info variable)))
964 (when output
965 (terpri)
966 (terpri)
967 (princ output))))
969 (with-current-buffer standard-output
970 ;; Return the text we displayed.
971 (buffer-string))))))))
974 (defvar help-xref-stack-item)
976 ;;;###autoload
977 (defun describe-symbol (symbol &optional buffer frame)
978 "Display the full documentation of SYMBOL.
979 Will show the info of SYMBOL as a function, variable, and/or face."
980 (interactive
981 (let* ((v-or-f (symbol-at-point))
982 (found (cl-some (lambda (x) (funcall (nth 1 x) v-or-f))
983 describe-symbol-backends))
984 (v-or-f (if found v-or-f (function-called-at-point)))
985 (found (or found v-or-f))
986 (enable-recursive-minibuffers t)
987 (val (completing-read (if found
988 (format
989 "Describe symbol (default %s): " v-or-f)
990 "Describe symbol: ")
991 obarray
992 (lambda (vv)
993 (cl-some (lambda (x) (funcall (nth 1 x) vv))
994 describe-symbol-backends))
995 t nil nil
996 (if found (symbol-name v-or-f)))))
997 (list (if (equal val "")
998 v-or-f (intern val)))))
999 (if (not (symbolp symbol))
1000 (user-error "You didn't specify a function or variable"))
1001 (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
1002 (unless (frame-live-p frame) (setq frame (selected-frame)))
1003 (with-current-buffer (help-buffer)
1004 ;; Push the previous item on the stack before clobbering the output buffer.
1005 (help-setup-xref nil nil)
1006 (let* ((docs
1007 (nreverse
1008 (delq nil
1009 (mapcar (pcase-lambda (`(,name ,testfn ,descfn))
1010 (when (funcall testfn symbol)
1011 ;; Don't record the current entry in the stack.
1012 (setq help-xref-stack-item nil)
1013 (cons name
1014 (funcall descfn symbol buffer frame))))
1015 describe-symbol-backends))))
1016 (single (null (cdr docs))))
1017 (while (cdr docs)
1018 (goto-char (point-min))
1019 (let ((inhibit-read-only t)
1020 (name (caar docs)) ;Name of doc currently at BOB.
1021 (doc (cdr (cadr docs)))) ;Doc to add at BOB.
1022 (insert doc)
1023 (delete-region (point) (progn (skip-chars-backward " \t\n") (point)))
1024 (insert "\n\n"
1025 (eval-when-compile
1026 (propertize "\n" 'face '(:height 0.1 :inverse-video t)))
1027 "\n")
1028 (when name
1029 (insert (symbol-name symbol)
1030 " is also a " name "." "\n\n")))
1031 (setq docs (cdr docs)))
1032 (unless single
1033 ;; Don't record the `describe-variable' item in the stack.
1034 (setq help-xref-stack-item nil)
1035 (help-setup-xref (list #'describe-symbol symbol) nil))
1036 (goto-char (point-min)))))
1038 ;;;###autoload
1039 (defun describe-syntax (&optional buffer)
1040 "Describe the syntax specifications in the syntax table of BUFFER.
1041 The descriptions are inserted in a help buffer, which is then displayed.
1042 BUFFER defaults to the current buffer."
1043 (interactive)
1044 (setq buffer (or buffer (current-buffer)))
1045 (help-setup-xref (list #'describe-syntax buffer)
1046 (called-interactively-p 'interactive))
1047 (with-help-window (help-buffer)
1048 (let ((table (with-current-buffer buffer (syntax-table))))
1049 (with-current-buffer standard-output
1050 (describe-vector table 'internal-describe-syntax-value)
1051 (while (setq table (char-table-parent table))
1052 (insert "\nThe parent syntax table is:")
1053 (describe-vector table 'internal-describe-syntax-value))))))
1055 (defun help-describe-category-set (value)
1056 (insert (cond
1057 ((null value) "default")
1058 ((char-table-p value) "deeper char-table ...")
1059 (t (condition-case nil
1060 (category-set-mnemonics value)
1061 (error "invalid"))))))
1063 ;;;###autoload
1064 (defun describe-categories (&optional buffer)
1065 "Describe the category specifications in the current category table.
1066 The descriptions are inserted in a buffer, which is then displayed.
1067 If BUFFER is non-nil, then describe BUFFER's category table instead.
1068 BUFFER should be a buffer or a buffer name."
1069 (interactive)
1070 (setq buffer (or buffer (current-buffer)))
1071 (help-setup-xref (list #'describe-categories buffer)
1072 (called-interactively-p 'interactive))
1073 (with-help-window (help-buffer)
1074 (let* ((table (with-current-buffer buffer (category-table)))
1075 (docs (char-table-extra-slot table 0)))
1076 (if (or (not (vectorp docs)) (/= (length docs) 95))
1077 (error "Invalid first extra slot in this category table\n"))
1078 (with-current-buffer standard-output
1079 (insert "Legend of category mnemonics (see the tail for the longer description)\n")
1080 (let ((pos (point)) (items 0) lines n)
1081 (dotimes (i 95)
1082 (if (aref docs i) (setq items (1+ items))))
1083 (setq lines (1+ (/ (1- items) 4)))
1084 (setq n 0)
1085 (dotimes (i 95)
1086 (let ((elt (aref docs i)))
1087 (when elt
1088 (string-match ".*" elt)
1089 (setq elt (match-string 0 elt))
1090 (if (>= (length elt) 17)
1091 (setq elt (concat (substring elt 0 14) "...")))
1092 (if (< (point) (point-max))
1093 (move-to-column (* 20 (/ n lines)) t))
1094 (insert (+ i ?\s) ?: elt)
1095 (if (< (point) (point-max))
1096 (forward-line 1)
1097 (insert "\n"))
1098 (setq n (1+ n))
1099 (if (= (% n lines) 0)
1100 (goto-char pos))))))
1101 (goto-char (point-max))
1102 (insert "\n"
1103 "character(s)\tcategory mnemonics\n"
1104 "------------\t------------------")
1105 (describe-vector table 'help-describe-category-set)
1106 (insert "Legend of category mnemonics:\n")
1107 (dotimes (i 95)
1108 (let ((elt (aref docs i)))
1109 (when elt
1110 (if (string-match "\n" elt)
1111 (setq elt (substring elt (match-end 0))))
1112 (insert (+ i ?\s) ": " elt "\n"))))
1113 (while (setq table (char-table-parent table))
1114 (insert "\nThe parent category table is:")
1115 (describe-vector table 'help-describe-category-set))))))
1118 ;;; Replacements for old lib-src/ programs. Don't seem especially useful.
1120 ;; Replaces lib-src/digest-doc.c.
1121 ;;;###autoload
1122 (defun doc-file-to-man (file)
1123 "Produce an nroff buffer containing the doc-strings from the DOC file."
1124 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1125 internal-doc-file-name t)))
1126 (or (file-readable-p file)
1127 (error "Cannot read file `%s'" file))
1128 (pop-to-buffer (generate-new-buffer "*man-doc*"))
1129 (setq buffer-undo-list t)
1130 (insert ".TH \"Command Summary for GNU Emacs\"\n"
1131 ".AU Richard M. Stallman\n")
1132 (insert-file-contents file)
1133 (let (notfirst)
1134 (while (search-forward "\x1f" nil 'move)
1135 (if (looking-at "S")
1136 (delete-region (1- (point)) (line-end-position))
1137 (delete-char -1)
1138 (if notfirst
1139 (insert "\n.DE\n")
1140 (setq notfirst t))
1141 (insert "\n.SH ")
1142 (insert (if (looking-at "F") "Function " "Variable "))
1143 (delete-char 1)
1144 (forward-line 1)
1145 (insert ".DS L\n"))))
1146 (insert "\n.DE\n")
1147 (setq buffer-undo-list nil)
1148 (nroff-mode))
1150 ;; Replaces lib-src/sorted-doc.c.
1151 ;;;###autoload
1152 (defun doc-file-to-info (file)
1153 "Produce a texinfo buffer with sorted doc-strings from the DOC file."
1154 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1155 internal-doc-file-name t)))
1156 (or (file-readable-p file)
1157 (error "Cannot read file `%s'" file))
1158 (let ((i 0) type name doc alist)
1159 (with-temp-buffer
1160 (insert-file-contents file)
1161 ;; The characters "@{}" need special treatment.
1162 (while (re-search-forward "[@{}]" nil t)
1163 (backward-char)
1164 (insert "@")
1165 (forward-char 1))
1166 (goto-char (point-min))
1167 (while (search-forward "\x1f" nil t)
1168 (unless (looking-at "S")
1169 (setq type (char-after)
1170 name (buffer-substring (1+ (point)) (line-end-position))
1171 doc (buffer-substring (line-beginning-position 2)
1172 (if (search-forward "\x1f" nil 'move)
1173 (1- (point))
1174 (point)))
1175 alist (cons (list name type doc) alist))
1176 (backward-char 1))))
1177 (pop-to-buffer (generate-new-buffer "*info-doc*"))
1178 (setq buffer-undo-list t)
1179 ;; Write the output header.
1180 (insert "\\input texinfo @c -*-texinfo-*-\n"
1181 "@setfilename emacsdoc.info\n"
1182 "@settitle Command Summary for GNU Emacs\n"
1183 "@finalout\n"
1184 "\n@node Top\n"
1185 "@unnumbered Command Summary for GNU Emacs\n\n"
1186 "@table @asis\n\n"
1187 "@iftex\n"
1188 "@global@let@ITEM@item\n"
1189 "@def@item{@filbreak@vskip5pt@ITEM}\n"
1190 "@font@tensy cmsy10 scaled @magstephalf\n"
1191 "@font@teni cmmi10 scaled @magstephalf\n"
1192 "@def\\{{@tensy@char110}}\n" ; this backslash goes with cmr10
1193 "@def|{{@tensy@char106}}\n"
1194 "@def@{{{@tensy@char102}}\n"
1195 "@def@}{{@tensy@char103}}\n"
1196 "@def<{{@teni@char62}}\n"
1197 "@def>{{@teni@char60}}\n"
1198 "@chardef@@64\n"
1199 "@catcode43=12\n"
1200 "@tableindent-0.2in\n"
1201 "@end iftex\n")
1202 ;; Sort the array by name; within each name, by type (functions first).
1203 (setq alist (sort alist (lambda (e1 e2)
1204 (if (string-equal (car e1) (car e2))
1205 (<= (cadr e1) (cadr e2))
1206 (string-lessp (car e1) (car e2))))))
1207 ;; Print each function.
1208 (dolist (e alist)
1209 (insert "\n@item "
1210 (if (char-equal (cadr e) ?\F) "Function" "Variable")
1211 " @code{" (car e) "}\n@display\n"
1212 (nth 2 e)
1213 "\n@end display\n")
1214 ;; Try to avoid a save size overflow in the TeX output routine.
1215 (if (zerop (setq i (% (1+ i) 100)))
1216 (insert "\n@end table\n@table @asis\n")))
1217 (insert "@end table\n"
1218 "@bye\n")
1219 (setq buffer-undo-list nil)
1220 (texinfo-mode)))
1222 (provide 'help-fns)
1224 ;;; help-fns.el ends here