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