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