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