Fix documentation of 'menu-set-font' and 'set-frame-font'
[emacs.git] / lisp / help-fns.el
blob8ed9a477399dae8e573aec45e17b0a64b618bd84
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 "‘%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 " ‘%s’" handler))
357 (save-excursion
358 (re-search-backward (format "‘\\([^‘’]+\\)’") nil t)
359 (help-xref-button 1 'help-function handler)))
360 ;; FIXME: Obsolete since 24.4.
361 (let ((lib (get function 'compiler-macro-file)))
362 (when (stringp lib)
363 (insert (format " in ‘%s’" lib))
364 (save-excursion
365 (re-search-backward (format "‘\\([^‘’]+\\)’") nil t)
366 (help-xref-button 1 'help-function-cmacro function lib)))))
367 (insert ".\n"))))
369 (defun help-fns--signature (function doc real-def real-function raw)
370 "Insert usage at point and return docstring. With highlighting."
371 (if (keymapp function)
372 doc ; If definition is a keymap, skip arglist note.
373 (let* ((advertised (gethash real-def advertised-signature-table t))
374 (arglist (if (listp advertised)
375 advertised (help-function-arglist real-def)))
376 (usage (help-split-fundoc doc function)))
377 (if usage (setq doc (cdr usage)))
378 (let* ((use (cond
379 ((and usage (not (listp advertised))) (car usage))
380 ((listp arglist)
381 (help--make-usage-docstring function arglist))
382 ((stringp arglist) arglist)
383 ;; Maybe the arglist is in the docstring of a symbol
384 ;; this one is aliased to.
385 ((let ((fun real-function))
386 (while (and (symbolp fun)
387 (setq fun (symbol-function fun))
388 (not (setq usage (help-split-fundoc
389 (documentation fun)
390 function)))))
391 usage)
392 (car usage))
393 ((or (stringp real-def)
394 (vectorp real-def))
395 (format "\nMacro: %s"
396 (help--docstring-quote
397 (format-kbd-macro real-def))))
398 (t "[Missing arglist. Please make a bug report.]")))
399 ;; Insert "`X", not "(\` X)", when documenting `X.
400 (use1 (replace-regexp-in-string
401 "\\`(\\\\=\\\\\\\\=` \\([^\n ]*\\))\\'"
402 "\\\\=`\\1" use t))
403 (high (if raw
404 (cons use1 doc)
405 (help-highlight-arguments (substitute-command-keys use1)
406 (substitute-command-keys doc)))))
407 (let ((fill-begin (point))
408 (high-usage (car high))
409 (high-doc (cdr high)))
410 (insert high-usage "\n")
411 (fill-region fill-begin (point))
412 high-doc)))))
414 (defun help-fns--parent-mode (function)
415 ;; If this is a derived mode, link to the parent.
416 (let ((parent-mode (and (symbolp function)
417 (get function
418 'derived-mode-parent))))
419 (when parent-mode
420 (insert (format "\nParent mode: ‘"))
421 (let ((beg (point)))
422 (insert (format "%s" parent-mode))
423 (make-text-button beg (point)
424 'type 'help-function
425 'help-args (list parent-mode)))
426 (insert (format "’.\n")))))
428 (defun help-fns--obsolete (function)
429 ;; Ignore lambda constructs, keyboard macros, etc.
430 (let* ((obsolete (and (symbolp function)
431 (get function 'byte-obsolete-info)))
432 (use (car obsolete)))
433 (when obsolete
434 (insert "\nThis "
435 (if (eq (car-safe (symbol-function function)) 'macro)
436 "macro"
437 "function")
438 " is obsolete")
439 (when (nth 2 obsolete)
440 (insert (format " since %s" (nth 2 obsolete))))
441 (insert (cond ((stringp use)
442 (concat ";\n" (substitute-command-keys use)))
443 (use (format ";\nuse ‘%s’ instead." use))
444 (t "."))
445 "\n"))))
447 ;; We could use `symbol-file' but this is a wee bit more efficient.
448 (defun help-fns--autoloaded-p (function file)
449 "Return non-nil if FUNCTION has previously been autoloaded.
450 FILE is the file where FUNCTION was probably defined."
451 (let* ((file (file-name-sans-extension (file-truename file)))
452 (load-hist load-history)
453 (target (cons t function))
454 found)
455 (while (and load-hist (not found))
456 (and (caar load-hist)
457 (equal (file-name-sans-extension (caar load-hist)) file)
458 (setq found (member target (cdar load-hist))))
459 (setq load-hist (cdr load-hist)))
460 found))
462 (defun help-fns--interactive-only (function)
463 "Insert some help blurb if FUNCTION should only be used interactively."
464 ;; Ignore lambda constructs, keyboard macros, etc.
465 (and (symbolp function)
466 (not (eq (car-safe (symbol-function function)) 'macro))
467 (let* ((interactive-only
468 (or (get function 'interactive-only)
469 (if (boundp 'byte-compile-interactive-only-functions)
470 (memq function
471 byte-compile-interactive-only-functions)))))
472 (when interactive-only
473 (insert "\nThis function is for interactive use only"
474 ;; Cf byte-compile-form.
475 (cond ((stringp interactive-only)
476 (format ";\nin Lisp code %s" interactive-only))
477 ((and (symbolp 'interactive-only)
478 (not (eq interactive-only t)))
479 (format ";\nin Lisp code use ‘%s’ instead."
480 interactive-only))
481 (t "."))
482 "\n")))))
484 (defun help-fns-short-filename (filename)
485 (let* ((abbrev (abbreviate-file-name filename))
486 (short abbrev))
487 (dolist (dir load-path)
488 (let ((rel (file-relative-name filename dir)))
489 (if (< (length rel) (length short))
490 (setq short rel)))
491 (let ((rel (file-relative-name abbrev dir)))
492 (if (< (length rel) (length short))
493 (setq short rel))))
494 short))
496 ;;;###autoload
497 (defun describe-function-1 (function)
498 (let* ((advised (and (symbolp function)
499 (featurep 'nadvice)
500 (advice--p (advice--symbol-function function))))
501 ;; If the function is advised, use the symbol that has the
502 ;; real definition, if that symbol is already set up.
503 (real-function
504 (or (and advised
505 (advice--cd*r (advice--symbol-function function)))
506 function))
507 ;; Get the real definition.
508 (def (if (symbolp real-function)
509 (or (symbol-function real-function)
510 (signal 'void-function (list real-function)))
511 real-function))
512 (aliased (or (symbolp def)
513 ;; Advised & aliased function.
514 (and advised (symbolp real-function))))
515 (real-def (cond
516 (aliased (let ((f real-function))
517 (while (and (fboundp f)
518 (symbolp (symbol-function f)))
519 (setq f (symbol-function f)))
521 ((subrp def) (intern (subr-name def)))
522 (t def)))
523 (sig-key (if (subrp def)
524 (indirect-function real-def)
525 real-def))
526 (file-name (find-lisp-object-file-name function def))
527 (pt1 (with-current-buffer (help-buffer) (point)))
528 (beg (if (and (or (byte-code-function-p def)
529 (keymapp def)
530 (memq (car-safe def) '(macro lambda closure)))
531 (stringp file-name)
532 (help-fns--autoloaded-p function file-name))
533 (if (commandp def)
534 "an interactive autoloaded "
535 "an autoloaded ")
536 (if (commandp def) "an interactive " "a "))))
538 ;; Print what kind of function-like object FUNCTION is.
539 (princ (cond ((or (stringp def) (vectorp def))
540 "a keyboard macro")
541 ((subrp def)
542 (if (eq 'unevalled (cdr (subr-arity def)))
543 (concat beg "special form")
544 (concat beg "built-in function")))
545 ;; Aliases are Lisp functions, so we need to check
546 ;; aliases before functions.
547 (aliased
548 (format "an alias for ‘%s’" real-def))
549 ((autoloadp def)
550 (format "%s autoloaded %s"
551 (if (commandp def) "an interactive" "an")
552 (if (eq (nth 4 def) 'keymap) "keymap"
553 (if (nth 4 def) "Lisp macro" "Lisp function"))))
554 ((or (eq (car-safe def) 'macro)
555 ;; For advised macros, def is a lambda
556 ;; expression or a byte-code-function-p, so we
557 ;; need to check macros before functions.
558 (macrop function))
559 (concat beg "Lisp macro"))
560 ((byte-code-function-p def)
561 (concat beg "compiled Lisp function"))
562 ((eq (car-safe def) 'lambda)
563 (concat beg "Lisp function"))
564 ((eq (car-safe def) 'closure)
565 (concat beg "Lisp closure"))
566 ((keymapp def)
567 (let ((is-full nil)
568 (elts (cdr-safe def)))
569 (while elts
570 (if (char-table-p (car-safe elts))
571 (setq is-full t
572 elts nil))
573 (setq elts (cdr-safe elts)))
574 (concat beg (if is-full "keymap" "sparse keymap"))))
575 (t "")))
577 (if (and aliased (not (fboundp real-def)))
578 (princ ",\nwhich is not defined. Please make a bug report.")
579 (with-current-buffer standard-output
580 (save-excursion
581 (save-match-data
582 (when (re-search-backward (format "alias for ‘\\([^‘’]+\\)’")
583 nil t)
584 (help-xref-button 1 'help-function real-def)))))
586 (when file-name
587 (princ (format " in ‘"))
588 ;; We used to add .el to the file name,
589 ;; but that's completely wrong when the user used load-file.
590 (princ (if (eq file-name 'C-source)
591 "C source code"
592 (help-fns-short-filename file-name)))
593 (princ (format "’"))
594 ;; Make a hyperlink to the library.
595 (with-current-buffer standard-output
596 (save-excursion
597 (re-search-backward (format "‘\\([^‘’]+\\)’") nil t)
598 (help-xref-button 1 'help-function-def function file-name))))
599 (princ ".")
600 (with-current-buffer (help-buffer)
601 (fill-region-as-paragraph (save-excursion (goto-char pt1) (forward-line 0) (point))
602 (point)))
603 (terpri)(terpri)
605 (let ((doc-raw (documentation function t)))
607 ;; If the function is autoloaded, and its docstring has
608 ;; key substitution constructs, load the library.
609 (and (autoloadp real-def) doc-raw
610 help-enable-auto-load
611 (string-match "\\([^\\]=\\|[^=]\\|\\`\\)\\\\[[{<]" doc-raw)
612 (autoload-do-load real-def))
614 (help-fns--key-bindings function)
615 (with-current-buffer standard-output
616 (let ((doc (help-fns--signature function doc-raw sig-key
617 real-function nil)))
618 (run-hook-with-args 'help-fns-describe-function-functions function)
619 (insert "\n"
620 (or doc "Not documented."))))))))
622 ;; Add defaults to `help-fns-describe-function-functions'.
623 (add-hook 'help-fns-describe-function-functions #'help-fns--obsolete)
624 (add-hook 'help-fns-describe-function-functions #'help-fns--interactive-only)
625 (add-hook 'help-fns-describe-function-functions #'help-fns--parent-mode)
626 (add-hook 'help-fns-describe-function-functions #'help-fns--compiler-macro)
629 ;; Variables
631 ;;;###autoload
632 (defun variable-at-point (&optional any-symbol)
633 "Return the bound variable symbol found at or before point.
634 Return 0 if there is no such symbol.
635 If ANY-SYMBOL is non-nil, don't insist the symbol be bound."
636 (with-syntax-table emacs-lisp-mode-syntax-table
637 (or (condition-case ()
638 (save-excursion
639 (skip-chars-forward "'")
640 (or (not (zerop (skip-syntax-backward "_w")))
641 (eq (char-syntax (following-char)) ?w)
642 (eq (char-syntax (following-char)) ?_)
643 (forward-sexp -1))
644 (skip-chars-forward "'")
645 (let ((obj (read (current-buffer))))
646 (and (symbolp obj) (boundp obj) obj)))
647 (error nil))
648 (let* ((str (find-tag-default))
649 (sym (if str (intern-soft str))))
650 (if (and sym (or any-symbol (boundp sym)))
652 (save-match-data
653 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
654 (setq sym (intern-soft (match-string 1 str)))
655 (and (or any-symbol (boundp sym)) sym)))))
656 0)))
658 (defun describe-variable-custom-version-info (variable)
659 (let ((custom-version (get variable 'custom-version))
660 (cpv (get variable 'custom-package-version))
661 (output nil))
662 (if custom-version
663 (setq output
664 (format "This variable was introduced, or its default value was changed, in\nversion %s of Emacs.\n"
665 custom-version))
666 (when cpv
667 (let* ((package (car-safe cpv))
668 (version (if (listp (cdr-safe cpv))
669 (car (cdr-safe cpv))
670 (cdr-safe cpv)))
671 (pkg-versions (assq package customize-package-emacs-version-alist))
672 (emacsv (cdr (assoc version pkg-versions))))
673 (if (and package version)
674 (setq output
675 (format (concat "This variable was introduced, or its default value was changed, in\nversion %s of the %s package"
676 (if emacsv
677 (format " that is part of Emacs %s" emacsv))
678 ".\n")
679 version package))))))
680 output))
682 ;;;###autoload
683 (defun describe-variable (variable &optional buffer frame)
684 "Display the full documentation of VARIABLE (a symbol).
685 Returns the documentation as a string, also.
686 If VARIABLE has a buffer-local value in BUFFER or FRAME
687 \(default to the current buffer and current frame),
688 it is displayed along with the global value."
689 (interactive
690 (let ((v (variable-at-point))
691 (enable-recursive-minibuffers t)
692 val)
693 (setq val (completing-read (if (symbolp v)
694 (format
695 "Describe variable (default %s): " v)
696 "Describe variable: ")
697 obarray
698 (lambda (vv)
699 (or (get vv 'variable-documentation)
700 (and (boundp vv) (not (keywordp vv)))))
701 t nil nil
702 (if (symbolp v) (symbol-name v))))
703 (list (if (equal val "")
704 v (intern val)))))
705 (let (file-name)
706 (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
707 (unless (frame-live-p frame) (setq frame (selected-frame)))
708 (if (not (symbolp variable))
709 (message "You did not specify a variable")
710 (save-excursion
711 (let ((valvoid (not (with-current-buffer buffer (boundp variable))))
712 (permanent-local (get variable 'permanent-local))
713 val val-start-pos locus)
714 ;; Extract the value before setting up the output buffer,
715 ;; in case `buffer' *is* the output buffer.
716 (unless valvoid
717 (with-selected-frame frame
718 (with-current-buffer buffer
719 (setq val (symbol-value variable)
720 locus (variable-binding-locus variable)))))
721 (help-setup-xref (list #'describe-variable variable buffer)
722 (called-interactively-p 'interactive))
723 (with-help-window (help-buffer)
724 (with-current-buffer buffer
725 (prin1 variable)
726 (setq file-name (find-lisp-object-file-name variable 'defvar))
728 (if file-name
729 (progn
730 (princ (format " is a variable defined in ‘"))
731 (princ (if (eq file-name 'C-source)
732 "C source code"
733 (file-name-nondirectory file-name)))
734 (princ (format "’.\n"))
735 (with-current-buffer standard-output
736 (save-excursion
737 (re-search-backward (format "‘\\([^‘’]+\\)’") nil t)
738 (help-xref-button 1 'help-variable-def
739 variable file-name)))
740 (if valvoid
741 (princ "It is void as a variable.")
742 (princ "Its ")))
743 (if valvoid
744 (princ " is void as a variable.")
745 (princ "'s "))))
746 (unless valvoid
747 (with-current-buffer standard-output
748 (setq val-start-pos (point))
749 (princ "value is ")
750 (let ((from (point))
751 (line-beg (line-beginning-position))
752 (print-rep
753 (let ((print-quoted t))
754 (prin1-to-string val))))
755 (if (< (+ (length print-rep) (point) (- line-beg)) 68)
756 (insert print-rep)
757 (terpri)
758 (pp val)
759 (if (< (point) (+ 68 (line-beginning-position 0)))
760 (delete-region from (1+ from))
761 (delete-region (1- from) from)))
762 (let* ((sv (get variable 'standard-value))
763 (origval (and (consp sv)
764 (condition-case nil
765 (eval (car sv))
766 (error :help-eval-error)))))
767 (when (and (consp sv)
768 (not (equal origval val))
769 (not (equal origval :help-eval-error)))
770 (princ "\nOriginal value was \n")
771 (setq from (point))
772 (pp origval)
773 (if (< (point) (+ from 20))
774 (delete-region (1- from) from)))))))
775 (terpri)
776 (when locus
777 (cond
778 ((bufferp locus)
779 (princ (format "Local in buffer %s; "
780 (buffer-name buffer))))
781 ((framep locus)
782 (princ (format "It is a frame-local variable; ")))
783 ((terminal-live-p locus)
784 (princ (format "It is a terminal-local variable; ")))
786 (princ (format "It is local to %S" locus))))
787 (if (not (default-boundp variable))
788 (princ "globally void")
789 (let ((global-val (default-value variable)))
790 (with-current-buffer standard-output
791 (princ "global value is ")
792 (if (eq val global-val)
793 (princ "the same.")
794 (terpri)
795 ;; Fixme: pp can take an age if you happen to
796 ;; ask for a very large expression. We should
797 ;; probably print it raw once and check it's a
798 ;; sensible size before prettyprinting. -- fx
799 (let ((from (point)))
800 (pp global-val)
801 ;; See previous comment for this function.
802 ;; (help-xref-on-pp from (point))
803 (if (< (point) (+ from 20))
804 (delete-region (1- from) from)))))))
805 (terpri))
807 ;; If the value is large, move it to the end.
808 (with-current-buffer standard-output
809 (when (> (count-lines (point-min) (point-max)) 10)
810 ;; Note that setting the syntax table like below
811 ;; makes forward-sexp move over a `'s' at the end
812 ;; of a symbol.
813 (set-syntax-table emacs-lisp-mode-syntax-table)
814 (goto-char val-start-pos)
815 ;; The line below previously read as
816 ;; (delete-region (point) (progn (end-of-line) (point)))
817 ;; which suppressed display of the buffer local value for
818 ;; large values.
819 (when (looking-at "value is") (replace-match ""))
820 (save-excursion
821 (insert "\n\nValue:")
822 (set (make-local-variable 'help-button-cache)
823 (point-marker)))
824 (insert "value is shown ")
825 (insert-button "below"
826 'action help-button-cache
827 'follow-link t
828 'help-echo "mouse-2, RET: show value")
829 (insert ".\n")))
830 (terpri)
832 (let* ((alias (condition-case nil
833 (indirect-variable variable)
834 (error variable)))
835 (obsolete (get variable 'byte-obsolete-variable))
836 (use (car obsolete))
837 (safe-var (get variable 'safe-local-variable))
838 (doc (or (documentation-property
839 variable 'variable-documentation)
840 (documentation-property
841 alias 'variable-documentation)))
842 (extra-line nil))
844 ;; Mention if it's a local variable.
845 (cond
846 ((and (local-variable-if-set-p variable)
847 (or (not (local-variable-p variable))
848 (with-temp-buffer
849 (local-variable-if-set-p variable))))
850 (setq extra-line t)
851 (princ " Automatically becomes ")
852 (if permanent-local
853 (princ "permanently "))
854 (princ "buffer-local when set.\n"))
855 ((not permanent-local))
856 ((bufferp locus)
857 (setq extra-line t)
858 (princ " This variable's buffer-local value is permanent.\n"))
860 (setq extra-line t)
861 (princ " This variable's value is permanent \
862 if it is given a local binding.\n")))
864 ;; Mention if it's an alias.
865 (unless (eq alias variable)
866 (setq extra-line t)
867 (princ (format " 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)
876 (concat ";\n " (substitute-command-keys use)))
877 (use (format ";\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 (format
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 (format "’.\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 "‘%s’.\n" safe-var))))
942 (if extra-line (terpri))
943 (princ "Documentation:\n")
944 (with-current-buffer standard-output
945 (insert (or doc "Not documented as a variable."))))
947 ;; Make a link to customize if this variable can be customized.
948 (when (custom-variable-p variable)
949 (let ((customize-label "customize"))
950 (terpri)
951 (terpri)
952 (princ (concat "You can " customize-label " this variable."))
953 (with-current-buffer standard-output
954 (save-excursion
955 (re-search-backward
956 (concat "\\(" customize-label "\\)") nil t)
957 (help-xref-button 1 'help-customize-variable variable))))
958 ;; Note variable's version or package version
959 (let ((output (describe-variable-custom-version-info variable)))
960 (when output
961 (terpri)
962 (terpri)
963 (princ output))))
965 (with-current-buffer standard-output
966 ;; Return the text we displayed.
967 (buffer-string))))))))
970 (defvar help-xref-stack-item)
972 ;;;###autoload
973 (defun describe-symbol (symbol &optional buffer frame)
974 "Display the full documentation of SYMBOL.
975 Will show the info of SYMBOL as a function, variable, and/or face."
976 (interactive
977 (let* ((v-or-f (symbol-at-point))
978 (found (cl-some (lambda (x) (funcall (nth 1 x) v-or-f))
979 describe-symbol-backends))
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 (completing-read (if found
984 (format
985 "Describe symbol (default %s): " v-or-f)
986 "Describe symbol: ")
987 obarray
988 (lambda (vv)
989 (cl-some (lambda (x) (funcall (nth 1 x) vv))
990 describe-symbol-backends))
991 t nil nil
992 (if found (symbol-name v-or-f)))))
993 (list (if (equal val "")
994 v-or-f (intern val)))))
995 (if (not (symbolp symbol))
996 (user-error "You didn't specify a function or variable"))
997 (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
998 (unless (frame-live-p frame) (setq frame (selected-frame)))
999 (with-current-buffer (help-buffer)
1000 ;; Push the previous item on the stack before clobbering the output buffer.
1001 (help-setup-xref nil nil)
1002 (let* ((docs
1003 (nreverse
1004 (delq nil
1005 (mapcar (pcase-lambda (`(,name ,testfn ,descfn))
1006 (when (funcall testfn symbol)
1007 ;; Don't record the current entry in the stack.
1008 (setq help-xref-stack-item nil)
1009 (cons name
1010 (funcall descfn symbol buffer frame))))
1011 describe-symbol-backends))))
1012 (single (null (cdr docs))))
1013 (while (cdr docs)
1014 (goto-char (point-min))
1015 (let ((inhibit-read-only t)
1016 (name (caar docs)) ;Name of doc currently at BOB.
1017 (doc (cdr (cadr docs)))) ;Doc to add at BOB.
1018 (insert doc)
1019 (delete-region (point) (progn (skip-chars-backward " \t\n") (point)))
1020 (insert "\n\n"
1021 (eval-when-compile
1022 (propertize "\n" 'face '(:height 0.1 :inverse-video t)))
1023 "\n")
1024 (when name
1025 (insert (symbol-name symbol)
1026 " is also a " name "." "\n\n")))
1027 (setq docs (cdr docs)))
1028 (unless single
1029 ;; Don't record the `describe-variable' item in the stack.
1030 (setq help-xref-stack-item nil)
1031 (help-setup-xref (list #'describe-symbol symbol) nil))
1032 (goto-char (point-min)))))
1034 ;;;###autoload
1035 (defun describe-syntax (&optional buffer)
1036 "Describe the syntax specifications in the syntax table of BUFFER.
1037 The descriptions are inserted in a help buffer, which is then displayed.
1038 BUFFER defaults to the current buffer."
1039 (interactive)
1040 (setq buffer (or buffer (current-buffer)))
1041 (help-setup-xref (list #'describe-syntax buffer)
1042 (called-interactively-p 'interactive))
1043 (with-help-window (help-buffer)
1044 (let ((table (with-current-buffer buffer (syntax-table))))
1045 (with-current-buffer standard-output
1046 (describe-vector table 'internal-describe-syntax-value)
1047 (while (setq table (char-table-parent table))
1048 (insert "\nThe parent syntax table is:")
1049 (describe-vector table 'internal-describe-syntax-value))))))
1051 (defun help-describe-category-set (value)
1052 (insert (cond
1053 ((null value) "default")
1054 ((char-table-p value) "deeper char-table ...")
1055 (t (condition-case nil
1056 (category-set-mnemonics value)
1057 (error "invalid"))))))
1059 ;;;###autoload
1060 (defun describe-categories (&optional buffer)
1061 "Describe the category specifications in the current category table.
1062 The descriptions are inserted in a buffer, which is then displayed.
1063 If BUFFER is non-nil, then describe BUFFER's category table instead.
1064 BUFFER should be a buffer or a buffer name."
1065 (interactive)
1066 (setq buffer (or buffer (current-buffer)))
1067 (help-setup-xref (list #'describe-categories buffer)
1068 (called-interactively-p 'interactive))
1069 (with-help-window (help-buffer)
1070 (let* ((table (with-current-buffer buffer (category-table)))
1071 (docs (char-table-extra-slot table 0)))
1072 (if (or (not (vectorp docs)) (/= (length docs) 95))
1073 (error "Invalid first extra slot in this category table\n"))
1074 (with-current-buffer standard-output
1075 (insert "Legend of category mnemonics (see the tail for the longer description)\n")
1076 (let ((pos (point)) (items 0) lines n)
1077 (dotimes (i 95)
1078 (if (aref docs i) (setq items (1+ items))))
1079 (setq lines (1+ (/ (1- items) 4)))
1080 (setq n 0)
1081 (dotimes (i 95)
1082 (let ((elt (aref docs i)))
1083 (when elt
1084 (string-match ".*" elt)
1085 (setq elt (match-string 0 elt))
1086 (if (>= (length elt) 17)
1087 (setq elt (concat (substring elt 0 14) "...")))
1088 (if (< (point) (point-max))
1089 (move-to-column (* 20 (/ n lines)) t))
1090 (insert (+ i ?\s) ?: elt)
1091 (if (< (point) (point-max))
1092 (forward-line 1)
1093 (insert "\n"))
1094 (setq n (1+ n))
1095 (if (= (% n lines) 0)
1096 (goto-char pos))))))
1097 (goto-char (point-max))
1098 (insert "\n"
1099 "character(s)\tcategory mnemonics\n"
1100 "------------\t------------------")
1101 (describe-vector table 'help-describe-category-set)
1102 (insert "Legend of category mnemonics:\n")
1103 (dotimes (i 95)
1104 (let ((elt (aref docs i)))
1105 (when elt
1106 (if (string-match "\n" elt)
1107 (setq elt (substring elt (match-end 0))))
1108 (insert (+ i ?\s) ": " elt "\n"))))
1109 (while (setq table (char-table-parent table))
1110 (insert "\nThe parent category table is:")
1111 (describe-vector table 'help-describe-category-set))))))
1114 ;;; Replacements for old lib-src/ programs. Don't seem especially useful.
1116 ;; Replaces lib-src/digest-doc.c.
1117 ;;;###autoload
1118 (defun doc-file-to-man (file)
1119 "Produce an nroff buffer containing the doc-strings from the DOC file."
1120 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1121 internal-doc-file-name t)))
1122 (or (file-readable-p file)
1123 (error "Cannot read file `%s'" file))
1124 (pop-to-buffer (generate-new-buffer "*man-doc*"))
1125 (setq buffer-undo-list t)
1126 (insert ".TH \"Command Summary for GNU Emacs\"\n"
1127 ".AU Richard M. Stallman\n")
1128 (insert-file-contents file)
1129 (let (notfirst)
1130 (while (search-forward "\x1f" nil 'move)
1131 (if (looking-at "S")
1132 (delete-region (1- (point)) (line-end-position))
1133 (delete-char -1)
1134 (if notfirst
1135 (insert "\n.DE\n")
1136 (setq notfirst t))
1137 (insert "\n.SH ")
1138 (insert (if (looking-at "F") "Function " "Variable "))
1139 (delete-char 1)
1140 (forward-line 1)
1141 (insert ".DS L\n"))))
1142 (insert "\n.DE\n")
1143 (setq buffer-undo-list nil)
1144 (nroff-mode))
1146 ;; Replaces lib-src/sorted-doc.c.
1147 ;;;###autoload
1148 (defun doc-file-to-info (file)
1149 "Produce a texinfo buffer with sorted doc-strings from the DOC file."
1150 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1151 internal-doc-file-name t)))
1152 (or (file-readable-p file)
1153 (error "Cannot read file `%s'" file))
1154 (let ((i 0) type name doc alist)
1155 (with-temp-buffer
1156 (insert-file-contents file)
1157 ;; The characters "@{}" need special treatment.
1158 (while (re-search-forward "[@{}]" nil t)
1159 (backward-char)
1160 (insert "@")
1161 (forward-char 1))
1162 (goto-char (point-min))
1163 (while (search-forward "\x1f" nil t)
1164 (unless (looking-at "S")
1165 (setq type (char-after)
1166 name (buffer-substring (1+ (point)) (line-end-position))
1167 doc (buffer-substring (line-beginning-position 2)
1168 (if (search-forward "\x1f" nil 'move)
1169 (1- (point))
1170 (point)))
1171 alist (cons (list name type doc) alist))
1172 (backward-char 1))))
1173 (pop-to-buffer (generate-new-buffer "*info-doc*"))
1174 (setq buffer-undo-list t)
1175 ;; Write the output header.
1176 (insert "\\input texinfo @c -*-texinfo-*-\n"
1177 "@setfilename emacsdoc.info\n"
1178 "@settitle Command Summary for GNU Emacs\n"
1179 "@finalout\n"
1180 "\n@node Top\n"
1181 "@unnumbered Command Summary for GNU Emacs\n\n"
1182 "@table @asis\n\n"
1183 "@iftex\n"
1184 "@global@let@ITEM@item\n"
1185 "@def@item{@filbreak@vskip5pt@ITEM}\n"
1186 "@font@tensy cmsy10 scaled @magstephalf\n"
1187 "@font@teni cmmi10 scaled @magstephalf\n"
1188 "@def\\{{@tensy@char110}}\n" ; this backslash goes with cmr10
1189 "@def|{{@tensy@char106}}\n"
1190 "@def@{{{@tensy@char102}}\n"
1191 "@def@}{{@tensy@char103}}\n"
1192 "@def<{{@teni@char62}}\n"
1193 "@def>{{@teni@char60}}\n"
1194 "@chardef@@64\n"
1195 "@catcode43=12\n"
1196 "@tableindent-0.2in\n"
1197 "@end iftex\n")
1198 ;; Sort the array by name; within each name, by type (functions first).
1199 (setq alist (sort alist (lambda (e1 e2)
1200 (if (string-equal (car e1) (car e2))
1201 (<= (cadr e1) (cadr e2))
1202 (string-lessp (car e1) (car e2))))))
1203 ;; Print each function.
1204 (dolist (e alist)
1205 (insert "\n@item "
1206 (if (char-equal (cadr e) ?\F) "Function" "Variable")
1207 " @code{" (car e) "}\n@display\n"
1208 (nth 2 e)
1209 "\n@end display\n")
1210 ;; Try to avoid a save size overflow in the TeX output routine.
1211 (if (zerop (setq i (% (1+ i) 100)))
1212 (insert "\n@end table\n@table @asis\n")))
1213 (insert "@end table\n"
1214 "@bye\n")
1215 (setq buffer-undo-list nil)
1216 (texinfo-mode)))
1218 (provide 'help-fns)
1220 ;;; help-fns.el ends here