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