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