Avoid crash with C-g C-g in GC
[emacs.git] / lisp / help-fns.el
blobd75fec2b56471dff4150f67f7741a9a60188bb09
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 <https://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 (and (car x) (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 (and (symbolp function)
477 (get function 'reader-construct))
478 (insert high-usage "\n"))
479 (fill-region fill-begin (point))
480 high-doc)))))
482 (defun help-fns--parent-mode (function)
483 ;; If this is a derived mode, link to the parent.
484 (let ((parent-mode (and (symbolp function)
485 (get function
486 'derived-mode-parent))))
487 (when parent-mode
488 (insert (substitute-command-keys "\nParent mode: `"))
489 (let ((beg (point)))
490 (insert (format "%s" parent-mode))
491 (make-text-button beg (point)
492 'type 'help-function
493 'help-args (list parent-mode)))
494 (insert (substitute-command-keys "'.\n")))))
496 (defun help-fns--obsolete (function)
497 ;; Ignore lambda constructs, keyboard macros, etc.
498 (let* ((obsolete (and (symbolp function)
499 (get function 'byte-obsolete-info)))
500 (use (car obsolete)))
501 (when obsolete
502 (insert "\nThis "
503 (if (eq (car-safe (symbol-function function)) 'macro)
504 "macro"
505 "function")
506 " is obsolete")
507 (when (nth 2 obsolete)
508 (insert (format " since %s" (nth 2 obsolete))))
509 (insert (cond ((stringp use) (concat ";\n" use))
510 (use (format-message ";\nuse `%s' instead." use))
511 (t "."))
512 "\n"))))
514 ;; We could use `symbol-file' but this is a wee bit more efficient.
515 (defun help-fns--autoloaded-p (function file)
516 "Return non-nil if FUNCTION has previously been autoloaded.
517 FILE is the file where FUNCTION was probably defined."
518 (let* ((file (file-name-sans-extension (file-truename file)))
519 (load-hist load-history)
520 (target (cons t function))
521 found)
522 (while (and load-hist (not found))
523 (and (caar load-hist)
524 (equal (file-name-sans-extension (caar load-hist)) file)
525 (setq found (member target (cdar load-hist))))
526 (setq load-hist (cdr load-hist)))
527 found))
529 (defun help-fns--interactive-only (function)
530 "Insert some help blurb if FUNCTION should only be used interactively."
531 ;; Ignore lambda constructs, keyboard macros, etc.
532 (and (symbolp function)
533 (not (eq (car-safe (symbol-function function)) 'macro))
534 (let* ((interactive-only
535 (or (get function 'interactive-only)
536 (if (boundp 'byte-compile-interactive-only-functions)
537 (memq function
538 byte-compile-interactive-only-functions)))))
539 (when interactive-only
540 (insert "\nThis function is for interactive use only"
541 ;; Cf byte-compile-form.
542 (cond ((stringp interactive-only)
543 (format ";\nin Lisp code %s" interactive-only))
544 ((and (symbolp 'interactive-only)
545 (not (eq interactive-only t)))
546 (format-message ";\nin Lisp code use `%s' instead."
547 interactive-only))
548 (t "."))
549 "\n")))))
551 (defun help-fns-short-filename (filename)
552 (let* ((abbrev (abbreviate-file-name filename))
553 (short abbrev))
554 (dolist (dir load-path)
555 (let ((rel (file-relative-name filename dir)))
556 (if (< (length rel) (length short))
557 (setq short rel)))
558 (let ((rel (file-relative-name abbrev dir)))
559 (if (< (length rel) (length short))
560 (setq short rel))))
561 short))
563 (defun help-fns--analyse-function (function)
564 "Return information about FUNCTION.
565 Returns a list of the form (REAL-FUNCTION DEF ALIASED REAL-DEF)."
566 (let* ((advised (and (symbolp function)
567 (advice--p (advice--symbol-function function))))
568 ;; If the function is advised, use the symbol that has the
569 ;; real definition, if that symbol is already set up.
570 (real-function
571 (or (and advised
572 (advice--cd*r (advice--symbol-function function)))
573 function))
574 ;; Get the real definition, if any.
575 (def (if (symbolp real-function)
576 (cond ((symbol-function real-function))
577 ((get real-function 'function-documentation)
578 nil)
579 (t (signal 'void-function (list real-function))))
580 real-function))
581 (aliased (and def
582 (or (symbolp def)
583 ;; Advised & aliased function.
584 (and advised (symbolp real-function)
585 (not (eq 'autoload (car-safe def))))
586 (and (subrp def)
587 (not (string= (subr-name def)
588 (symbol-name function)))))))
589 (real-def (cond
590 ((and aliased (not (subrp def)))
591 (let ((f real-function))
592 (while (and (fboundp f)
593 (symbolp (symbol-function f)))
594 (setq f (symbol-function f)))
596 ((subrp def) (intern (subr-name def)))
597 (t def))))
598 (list real-function def aliased real-def)))
600 (defun help-fns-function-description-header (function)
601 "Print a line describing FUNCTION to `standard-output'."
602 (pcase-let* ((`(,_real-function ,def ,aliased ,real-def)
603 (help-fns--analyse-function function))
604 (file-name (find-lisp-object-file-name function (if aliased 'defun
605 def)))
606 (beg (if (and (or (byte-code-function-p def)
607 (keymapp def)
608 (memq (car-safe def) '(macro lambda closure)))
609 (stringp file-name)
610 (help-fns--autoloaded-p function file-name))
611 (if (commandp def)
612 "an interactive autoloaded "
613 "an autoloaded ")
614 (if (commandp def) "an interactive " "a "))))
616 ;; Print what kind of function-like object FUNCTION is.
617 (princ (cond ((or (stringp def) (vectorp def))
618 "a keyboard macro")
619 ((and (symbolp function)
620 (get function 'reader-construct))
621 "a reader construct")
622 ;; Aliases are Lisp functions, so we need to check
623 ;; aliases before functions.
624 (aliased
625 (format-message "an alias for `%s'" real-def))
626 ((subrp def)
627 (if (eq 'unevalled (cdr (subr-arity def)))
628 (concat beg "special form")
629 (concat beg "built-in function")))
630 ((autoloadp def)
631 (format "%s autoloaded %s"
632 (if (commandp def) "an interactive" "an")
633 (if (eq (nth 4 def) 'keymap) "keymap"
634 (if (nth 4 def) "Lisp macro" "Lisp function"))))
635 ((or (eq (car-safe def) 'macro)
636 ;; For advised macros, def is a lambda
637 ;; expression or a byte-code-function-p, so we
638 ;; need to check macros before functions.
639 (macrop function))
640 (concat beg "Lisp macro"))
641 ((byte-code-function-p def)
642 (concat beg "compiled Lisp function"))
643 ((eq (car-safe def) 'lambda)
644 (concat beg "Lisp function"))
645 ((eq (car-safe def) 'closure)
646 (concat beg "Lisp closure"))
647 ((keymapp def)
648 (let ((is-full nil)
649 (elts (cdr-safe def)))
650 (while elts
651 (if (char-table-p (car-safe elts))
652 (setq is-full t
653 elts nil))
654 (setq elts (cdr-safe elts)))
655 (concat beg (if is-full "keymap" "sparse keymap"))))
656 (t "")))
658 (if (and aliased (not (fboundp real-def)))
659 (princ ",\nwhich is not defined. Please make a bug report.")
660 (with-current-buffer standard-output
661 (save-excursion
662 (save-match-data
663 (when (re-search-backward (substitute-command-keys
664 "alias for `\\([^`']+\\)'")
665 nil t)
666 (help-xref-button 1 'help-function real-def)))))
668 (when file-name
669 ;; We used to add .el to the file name,
670 ;; but that's completely wrong when the user used load-file.
671 (princ (format-message " in `%s'"
672 (if (eq file-name 'C-source)
673 "C source code"
674 (help-fns-short-filename file-name))))
675 ;; Make a hyperlink to the library.
676 (with-current-buffer standard-output
677 (save-excursion
678 (re-search-backward (substitute-command-keys "`\\([^`']+\\)'")
679 nil t)
680 (help-xref-button 1 'help-function-def function file-name))))
681 (princ "."))))
683 ;;;###autoload
684 (defun describe-function-1 (function)
685 (let ((pt1 (with-current-buffer (help-buffer) (point))))
686 (help-fns-function-description-header function)
687 (with-current-buffer (help-buffer)
688 (fill-region-as-paragraph (save-excursion (goto-char pt1) (forward-line 0) (point))
689 (point))))
690 (terpri)(terpri)
692 (pcase-let ((`(,real-function ,def ,_aliased ,real-def)
693 (help-fns--analyse-function function))
694 (doc-raw (documentation function t))
695 (key-bindings-buffer (current-buffer)))
697 ;; If the function is autoloaded, and its docstring has
698 ;; key substitution constructs, load the library.
699 (and (autoloadp real-def) doc-raw
700 help-enable-auto-load
701 (string-match "\\([^\\]=\\|[^=]\\|\\`\\)\\\\[[{<]" doc-raw)
702 (autoload-do-load real-def))
704 (help-fns--key-bindings function)
705 (with-current-buffer standard-output
706 (let ((doc (help-fns--signature
707 function doc-raw
708 (if (subrp def) (indirect-function real-def) real-def)
709 real-function key-bindings-buffer)))
710 (run-hook-with-args 'help-fns-describe-function-functions function)
711 (insert "\n" (or doc "Not documented.")))
712 ;; Avoid asking the user annoying questions if she decides
713 ;; to save the help buffer, when her locale's codeset
714 ;; isn't UTF-8.
715 (unless (memq text-quoting-style '(straight grave))
716 (set-buffer-file-coding-system 'utf-8)))))
718 ;; Add defaults to `help-fns-describe-function-functions'.
719 (add-hook 'help-fns-describe-function-functions #'help-fns--obsolete)
720 (add-hook 'help-fns-describe-function-functions #'help-fns--interactive-only)
721 (add-hook 'help-fns-describe-function-functions #'help-fns--parent-mode)
722 (add-hook 'help-fns-describe-function-functions #'help-fns--compiler-macro)
725 ;; Variables
727 ;;;###autoload
728 (defun variable-at-point (&optional any-symbol)
729 "Return the bound variable symbol found at or before point.
730 Return 0 if there is no such symbol.
731 If ANY-SYMBOL is non-nil, don't insist the symbol be bound."
732 (with-syntax-table emacs-lisp-mode-syntax-table
733 (or (condition-case ()
734 (save-excursion
735 (skip-chars-forward "'")
736 (or (not (zerop (skip-syntax-backward "_w")))
737 (eq (char-syntax (following-char)) ?w)
738 (eq (char-syntax (following-char)) ?_)
739 (forward-sexp -1))
740 (skip-chars-forward "'")
741 (let ((obj (read (current-buffer))))
742 (and (symbolp obj) (boundp obj) obj)))
743 (error nil))
744 (let* ((str (find-tag-default))
745 (sym (if str (intern-soft str))))
746 (if (and sym (or any-symbol (boundp sym)))
748 (save-match-data
749 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
750 (setq sym (intern-soft (match-string 1 str)))
751 (and (or any-symbol (boundp sym)) sym)))))
752 0)))
754 (defun describe-variable-custom-version-info (variable)
755 (let ((custom-version (get variable 'custom-version))
756 (cpv (get variable 'custom-package-version))
757 (output nil))
758 (if custom-version
759 (setq output
760 (format "This variable was introduced, or its default value was changed, in\nversion %s of Emacs.\n"
761 custom-version))
762 (when cpv
763 (let* ((package (car-safe cpv))
764 (version (if (listp (cdr-safe cpv))
765 (car (cdr-safe cpv))
766 (cdr-safe cpv)))
767 (pkg-versions (assq package customize-package-emacs-version-alist))
768 (emacsv (cdr (assoc version pkg-versions))))
769 (if (and package version)
770 (setq output
771 (format (concat "This variable was introduced, or its default value was changed, in\nversion %s of the %s package"
772 (if emacsv
773 (format " that is part of Emacs %s" emacsv))
774 ".\n")
775 version package))))))
776 output))
778 ;;;###autoload
779 (defun describe-variable (variable &optional buffer frame)
780 "Display the full documentation of VARIABLE (a symbol).
781 Returns the documentation as a string, also.
782 If VARIABLE has a buffer-local value in BUFFER or FRAME
783 \(default to the current buffer and current frame),
784 it is displayed along with the global value."
785 (interactive
786 (let ((v (variable-at-point))
787 (enable-recursive-minibuffers t)
788 (orig-buffer (current-buffer))
789 val)
790 (setq val (completing-read
791 (if (symbolp v)
792 (format
793 "Describe variable (default %s): " v)
794 "Describe variable: ")
795 #'help--symbol-completion-table
796 (lambda (vv)
797 ;; In case the variable only exists in the buffer
798 ;; the command we switch back to that buffer before
799 ;; we examine the variable.
800 (with-current-buffer orig-buffer
801 (or (get vv 'variable-documentation)
802 (and (boundp vv) (not (keywordp vv))))))
803 t nil nil
804 (if (symbolp v) (symbol-name v))))
805 (list (if (equal val "")
806 v (intern val)))))
807 (let (file-name)
808 (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
809 (unless (frame-live-p frame) (setq frame (selected-frame)))
810 (if (not (symbolp variable))
811 (message "You did not specify a variable")
812 (save-excursion
813 (let ((valvoid (not (with-current-buffer buffer (boundp variable))))
814 (permanent-local (get variable 'permanent-local))
815 val val-start-pos locus)
816 ;; Extract the value before setting up the output buffer,
817 ;; in case `buffer' *is* the output buffer.
818 (unless valvoid
819 (with-selected-frame frame
820 (with-current-buffer buffer
821 (setq val (symbol-value variable)
822 locus (variable-binding-locus variable)))))
823 (help-setup-xref (list #'describe-variable variable buffer)
824 (called-interactively-p 'interactive))
825 (with-help-window (help-buffer)
826 (with-current-buffer buffer
827 (prin1 variable)
828 (setq file-name (find-lisp-object-file-name variable 'defvar))
830 (if file-name
831 (progn
832 (princ (format-message
833 " is a variable defined in `%s'.\n"
834 (if (eq file-name 'C-source)
835 "C source code"
836 (file-name-nondirectory file-name))))
837 (with-current-buffer standard-output
838 (save-excursion
839 (re-search-backward (substitute-command-keys
840 "`\\([^`']+\\)'")
841 nil t)
842 (help-xref-button 1 'help-variable-def
843 variable file-name)))
844 (if valvoid
845 (princ "It is void as a variable.")
846 (princ "Its ")))
847 (if valvoid
848 (princ " is void as a variable.")
849 (princ (substitute-command-keys "'s ")))))
850 (unless valvoid
851 (with-current-buffer standard-output
852 (setq val-start-pos (point))
853 (princ "value is")
854 (let ((line-beg (line-beginning-position))
855 (print-rep
856 (let ((rep
857 (let ((print-quoted t)
858 (print-circle t))
859 (cl-prin1-to-string val))))
860 (if (and (symbolp val) (not (booleanp val)))
861 (format-message "`%s'" rep)
862 rep))))
863 (if (< (+ (length print-rep) (point) (- line-beg)) 68)
864 (insert " " print-rep)
865 (terpri)
866 (let ((buf (current-buffer)))
867 (with-temp-buffer
868 (insert print-rep)
869 (pp-buffer)
870 (let ((pp-buffer (current-buffer)))
871 (with-current-buffer buf
872 (insert-buffer-substring pp-buffer)))))
873 ;; Remove trailing newline.
874 (and (= (char-before) ?\n) (delete-char -1)))
875 (let* ((sv (get variable 'standard-value))
876 (origval (and (consp sv)
877 (condition-case nil
878 (eval (car sv))
879 (error :help-eval-error))))
880 from)
881 (when (and (consp sv)
882 (not (equal origval val))
883 (not (equal origval :help-eval-error)))
884 (princ "\nOriginal value was \n")
885 (setq from (point))
886 (cl-prin1 origval)
887 (save-restriction
888 (narrow-to-region from (point))
889 (save-excursion (pp-buffer)))
890 (if (< (point) (+ from 20))
891 (delete-region (1- from) from)))))))
892 (terpri)
893 (when locus
894 (cond
895 ((bufferp locus)
896 (princ (format "Local in buffer %s; "
897 (buffer-name buffer))))
898 ((terminal-live-p locus)
899 (princ (format "It is a terminal-local variable; ")))
901 (princ (format "It is local to %S" locus))))
902 (if (not (default-boundp variable))
903 (princ "globally void")
904 (let ((global-val (default-value variable)))
905 (with-current-buffer standard-output
906 (princ "global value is ")
907 (if (eq val global-val)
908 (princ "the same.")
909 (terpri)
910 ;; Fixme: pp can take an age if you happen to
911 ;; ask for a very large expression. We should
912 ;; probably print it raw once and check it's a
913 ;; sensible size before prettyprinting. -- fx
914 (let ((from (point)))
915 (cl-prin1 global-val)
916 (save-restriction
917 (narrow-to-region from (point))
918 (save-excursion (pp-buffer)))
919 ;; See previous comment for this function.
920 ;; (help-xref-on-pp from (point))
921 (if (< (point) (+ from 20))
922 (delete-region (1- from) from)))))))
923 (terpri))
925 ;; If the value is large, move it to the end.
926 (with-current-buffer standard-output
927 (when (> (count-lines (point-min) (point-max)) 10)
928 ;; Note that setting the syntax table like below
929 ;; makes forward-sexp move over a `'s' at the end
930 ;; of a symbol.
931 (set-syntax-table emacs-lisp-mode-syntax-table)
932 (goto-char val-start-pos)
933 ;; The line below previously read as
934 ;; (delete-region (point) (progn (end-of-line) (point)))
935 ;; which suppressed display of the buffer local value for
936 ;; large values.
937 (when (looking-at "value is") (replace-match ""))
938 (save-excursion
939 (insert "\n\nValue:")
940 (set (make-local-variable 'help-button-cache)
941 (point-marker)))
942 (insert "value is shown ")
943 (insert-button "below"
944 'action help-button-cache
945 'follow-link t
946 'help-echo "mouse-2, RET: show value")
947 (insert ".\n")))
948 (terpri)
950 (let* ((alias (condition-case nil
951 (indirect-variable variable)
952 (error variable)))
953 (obsolete (get variable 'byte-obsolete-variable))
954 (watchpoints (get-variable-watchers variable))
955 (use (car obsolete))
956 (safe-var (get variable 'safe-local-variable))
957 (doc (or (documentation-property
958 variable 'variable-documentation)
959 (documentation-property
960 alias 'variable-documentation)))
961 (extra-line nil))
963 ;; Mention if it's a local variable.
964 (cond
965 ((and (local-variable-if-set-p variable)
966 (or (not (local-variable-p variable))
967 (with-temp-buffer
968 (local-variable-if-set-p variable))))
969 (setq extra-line t)
970 (princ " Automatically becomes ")
971 (if permanent-local
972 (princ "permanently "))
973 (princ "buffer-local when set.\n"))
974 ((not permanent-local))
975 ((bufferp locus)
976 (setq extra-line t)
977 (princ
978 (substitute-command-keys
979 " This variable's buffer-local value is permanent.\n")))
981 (setq extra-line t)
982 (princ (substitute-command-keys
983 " This variable's value is permanent \
984 if it is given a local binding.\n"))))
986 ;; Mention if it's an alias.
987 (unless (eq alias variable)
988 (setq extra-line t)
989 (princ (format-message
990 " This variable is an alias for `%s'.\n"
991 alias)))
993 (when obsolete
994 (setq extra-line t)
995 (princ " This variable is obsolete")
996 (if (nth 2 obsolete)
997 (princ (format " since %s" (nth 2 obsolete))))
998 (princ (cond ((stringp use) (concat ";\n " use))
999 (use (format-message ";\n use `%s' instead."
1000 (car obsolete)))
1001 (t ".")))
1002 (terpri))
1004 (when watchpoints
1005 (setq extra-line t)
1006 (princ " Calls these functions when changed: ")
1007 (princ watchpoints)
1008 (terpri))
1010 (when (member (cons variable val)
1011 (with-current-buffer buffer
1012 file-local-variables-alist))
1013 (setq extra-line t)
1014 (if (member (cons variable val)
1015 (with-current-buffer buffer
1016 dir-local-variables-alist))
1017 (let ((file (and (buffer-file-name buffer)
1018 (not (file-remote-p
1019 (buffer-file-name buffer)))
1020 (dir-locals-find-file
1021 (buffer-file-name buffer))))
1022 (is-directory nil))
1023 (princ (substitute-command-keys
1024 " This variable's value is directory-local"))
1025 (when (consp file) ; result from cache
1026 ;; If the cache element has an mtime, we
1027 ;; assume it came from a file.
1028 (if (nth 2 file)
1029 ;; (car file) is a directory.
1030 (setq file (dir-locals--all-files (car file)))
1031 ;; Otherwise, assume it was set directly.
1032 (setq file (car file)
1033 is-directory t)))
1034 (if (null file)
1035 (princ ".\n")
1036 (princ ", set ")
1037 (princ (substitute-command-keys
1038 (cond
1039 (is-directory "for the directory\n `")
1040 ;; Many files matched.
1041 ((and (consp file) (cdr file))
1042 (setq file (file-name-directory (car file)))
1043 (format "by one of the\n %s files in the directory\n `"
1044 dir-locals-file))
1045 (t (setq file (car file))
1046 "by the file\n `"))))
1047 (with-current-buffer standard-output
1048 (insert-text-button
1049 file 'type 'help-dir-local-var-def
1050 'help-args (list variable file)))
1051 (princ (substitute-command-keys "'.\n"))))
1052 (princ (substitute-command-keys
1053 " This variable's value is file-local.\n"))))
1055 (when (memq variable ignored-local-variables)
1056 (setq extra-line t)
1057 (princ " This variable is ignored as a file-local \
1058 variable.\n"))
1060 ;; Can be both risky and safe, eg auto-fill-function.
1061 (when (risky-local-variable-p variable)
1062 (setq extra-line t)
1063 (princ " This variable may be risky if used as a \
1064 file-local variable.\n")
1065 (when (assq variable safe-local-variable-values)
1066 (princ (substitute-command-keys
1067 " However, you have added it to \
1068 `safe-local-variable-values'.\n"))))
1070 (when safe-var
1071 (setq extra-line t)
1072 (princ " This variable is safe as a file local variable ")
1073 (princ "if its value\n satisfies the predicate ")
1074 (princ (if (byte-code-function-p safe-var)
1075 "which is a byte-compiled expression.\n"
1076 (format-message "`%s'.\n" safe-var))))
1078 (if extra-line (terpri))
1079 (princ "Documentation:\n")
1080 (with-current-buffer standard-output
1081 (insert (or doc "Not documented as a variable."))))
1083 ;; Make a link to customize if this variable can be customized.
1084 (when (custom-variable-p variable)
1085 (let ((customize-label "customize"))
1086 (terpri)
1087 (terpri)
1088 (princ (concat "You can " customize-label " this variable."))
1089 (with-current-buffer standard-output
1090 (save-excursion
1091 (re-search-backward
1092 (concat "\\(" customize-label "\\)") nil t)
1093 (help-xref-button 1 'help-customize-variable variable))))
1094 ;; Note variable's version or package version.
1095 (let ((output (describe-variable-custom-version-info variable)))
1096 (when output
1097 (terpri)
1098 (terpri)
1099 (princ output))))
1101 (with-current-buffer standard-output
1102 ;; Return the text we displayed.
1103 (buffer-string))))))))
1106 (defvar help-xref-stack-item)
1108 ;;;###autoload
1109 (defun describe-symbol (symbol &optional buffer frame)
1110 "Display the full documentation of SYMBOL.
1111 Will show the info of SYMBOL as a function, variable, and/or face.
1112 Optional arguments BUFFER and FRAME specify for which buffer and
1113 frame to show the information about SYMBOL; they default to the
1114 current buffer and the selected frame, respectively."
1115 (interactive
1116 (let* ((v-or-f (symbol-at-point))
1117 (found (if v-or-f (cl-some (lambda (x) (funcall (nth 1 x) v-or-f))
1118 describe-symbol-backends)))
1119 (v-or-f (if found v-or-f (function-called-at-point)))
1120 (found (or found v-or-f))
1121 (enable-recursive-minibuffers t)
1122 (val (completing-read (if found
1123 (format
1124 "Describe symbol (default %s): " v-or-f)
1125 "Describe symbol: ")
1126 obarray
1127 (lambda (vv)
1128 (cl-some (lambda (x) (funcall (nth 1 x) vv))
1129 describe-symbol-backends))
1130 t nil nil
1131 (if found (symbol-name v-or-f)))))
1132 (list (if (equal val "")
1133 v-or-f (intern val)))))
1134 (if (not (symbolp symbol))
1135 (user-error "You didn't specify a function or variable"))
1136 (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
1137 (unless (frame-live-p frame) (setq frame (selected-frame)))
1138 (with-current-buffer (help-buffer)
1139 ;; Push the previous item on the stack before clobbering the output buffer.
1140 (help-setup-xref nil nil)
1141 (let* ((docs
1142 (nreverse
1143 (delq nil
1144 (mapcar (pcase-lambda (`(,name ,testfn ,descfn))
1145 (when (funcall testfn symbol)
1146 ;; Don't record the current entry in the stack.
1147 (setq help-xref-stack-item nil)
1148 (cons name
1149 (funcall descfn symbol buffer frame))))
1150 describe-symbol-backends))))
1151 (single (null (cdr docs))))
1152 (while (cdr docs)
1153 (goto-char (point-min))
1154 (let ((inhibit-read-only t)
1155 (name (caar docs)) ;Name of doc currently at BOB.
1156 (doc (cdr (cadr docs)))) ;Doc to add at BOB.
1157 (when doc
1158 (insert doc)
1159 (delete-region (point)
1160 (progn (skip-chars-backward " \t\n") (point)))
1161 (insert "\n\n"
1162 (eval-when-compile
1163 (propertize "\n" 'face '(:height 0.1 :inverse-video t)))
1164 "\n")
1165 (when name
1166 (insert (symbol-name symbol)
1167 " is also a " name "." "\n\n"))))
1168 (setq docs (cdr docs)))
1169 (unless single
1170 ;; Don't record the `describe-variable' item in the stack.
1171 (setq help-xref-stack-item nil)
1172 (help-setup-xref (list #'describe-symbol symbol) nil))
1173 (goto-char (point-min)))))
1175 ;;;###autoload
1176 (defun describe-syntax (&optional buffer)
1177 "Describe the syntax specifications in the syntax table of BUFFER.
1178 The descriptions are inserted in a help buffer, which is then displayed.
1179 BUFFER defaults to the current buffer."
1180 (interactive)
1181 (setq buffer (or buffer (current-buffer)))
1182 (help-setup-xref (list #'describe-syntax buffer)
1183 (called-interactively-p 'interactive))
1184 (with-help-window (help-buffer)
1185 (let ((table (with-current-buffer buffer (syntax-table))))
1186 (with-current-buffer standard-output
1187 (describe-vector table 'internal-describe-syntax-value)
1188 (while (setq table (char-table-parent table))
1189 (insert "\nThe parent syntax table is:")
1190 (describe-vector table 'internal-describe-syntax-value))))))
1192 (defun help-describe-category-set (value)
1193 (insert (cond
1194 ((null value) "default")
1195 ((char-table-p value) "deeper char-table ...")
1196 (t (condition-case nil
1197 (category-set-mnemonics value)
1198 (error "invalid"))))))
1200 ;;;###autoload
1201 (defun describe-categories (&optional buffer)
1202 "Describe the category specifications in the current category table.
1203 The descriptions are inserted in a buffer, which is then displayed.
1204 If BUFFER is non-nil, then describe BUFFER's category table instead.
1205 BUFFER should be a buffer or a buffer name."
1206 (interactive)
1207 (setq buffer (or buffer (current-buffer)))
1208 (help-setup-xref (list #'describe-categories buffer)
1209 (called-interactively-p 'interactive))
1210 (with-help-window (help-buffer)
1211 (let* ((table (with-current-buffer buffer (category-table)))
1212 (docs (char-table-extra-slot table 0)))
1213 (if (or (not (vectorp docs)) (/= (length docs) 95))
1214 (error "Invalid first extra slot in this category table\n"))
1215 (with-current-buffer standard-output
1216 (setq-default help-button-cache (make-marker))
1217 (insert "Legend of category mnemonics ")
1218 (insert-button "(longer descriptions at the bottom)"
1219 'action help-button-cache
1220 'follow-link t
1221 'help-echo "mouse-2, RET: show full legend")
1222 (insert "\n")
1223 (let ((pos (point)) (items 0) lines n)
1224 (dotimes (i 95)
1225 (if (aref docs i) (setq items (1+ items))))
1226 (setq lines (1+ (/ (1- items) 4)))
1227 (setq n 0)
1228 (dotimes (i 95)
1229 (let ((elt (aref docs i)))
1230 (when elt
1231 (string-match ".*" elt)
1232 (setq elt (match-string 0 elt))
1233 (if (>= (length elt) 17)
1234 (setq elt (concat (substring elt 0 14) "...")))
1235 (if (< (point) (point-max))
1236 (move-to-column (* 20 (/ n lines)) t))
1237 (insert (+ i ?\s) ?: elt)
1238 (if (< (point) (point-max))
1239 (forward-line 1)
1240 (insert "\n"))
1241 (setq n (1+ n))
1242 (if (= (% n lines) 0)
1243 (goto-char pos))))))
1244 (goto-char (point-max))
1245 (insert "\n"
1246 "character(s)\tcategory mnemonics\n"
1247 "------------\t------------------")
1248 (describe-vector table 'help-describe-category-set)
1249 (set-marker help-button-cache (point))
1250 (insert "Legend of category mnemonics:\n")
1251 (dotimes (i 95)
1252 (let ((elt (aref docs i)))
1253 (when elt
1254 (if (string-match "\n" elt)
1255 (setq elt (substring elt (match-end 0))))
1256 (insert (+ i ?\s) ": " elt "\n"))))
1257 (while (setq table (char-table-parent table))
1258 (insert "\nThe parent category table is:")
1259 (describe-vector table 'help-describe-category-set))))))
1262 ;;; Replacements for old lib-src/ programs. Don't seem especially useful.
1264 ;; Replaces lib-src/digest-doc.c.
1265 ;;;###autoload
1266 (defun doc-file-to-man (file)
1267 "Produce an nroff buffer containing the doc-strings from the DOC file."
1268 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1269 internal-doc-file-name t)))
1270 (or (file-readable-p file)
1271 (error "Cannot read file `%s'" file))
1272 (pop-to-buffer (generate-new-buffer "*man-doc*"))
1273 (setq buffer-undo-list t)
1274 (insert ".TH \"Command Summary for GNU Emacs\"\n"
1275 ".AU Richard M. Stallman\n")
1276 (insert-file-contents file)
1277 (let (notfirst)
1278 (while (search-forward "\x1f" nil 'move)
1279 (if (= (following-char) ?S)
1280 (delete-region (1- (point)) (line-end-position))
1281 (delete-char -1)
1282 (if notfirst
1283 (insert "\n.DE\n")
1284 (setq notfirst t))
1285 (insert "\n.SH ")
1286 (insert (if (= (following-char) ?F) "Function " "Variable "))
1287 (delete-char 1)
1288 (forward-line 1)
1289 (insert ".DS L\n"))))
1290 (insert "\n.DE\n")
1291 (setq buffer-undo-list nil)
1292 (nroff-mode))
1294 ;; Replaces lib-src/sorted-doc.c.
1295 ;;;###autoload
1296 (defun doc-file-to-info (file)
1297 "Produce a texinfo buffer with sorted doc-strings from the DOC file."
1298 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1299 internal-doc-file-name t)))
1300 (or (file-readable-p file)
1301 (error "Cannot read file `%s'" file))
1302 (let ((i 0) type name doc alist)
1303 (with-temp-buffer
1304 (insert-file-contents file)
1305 ;; The characters "@{}" need special treatment.
1306 (while (re-search-forward "[@{}]" nil t)
1307 (backward-char)
1308 (insert "@")
1309 (forward-char 1))
1310 (goto-char (point-min))
1311 (while (search-forward "\x1f" nil t)
1312 (when (/= (following-char) ?S)
1313 (setq type (char-after)
1314 name (buffer-substring (1+ (point)) (line-end-position))
1315 doc (buffer-substring (line-beginning-position 2)
1316 (if (search-forward "\x1f" nil 'move)
1317 (1- (point))
1318 (point)))
1319 alist (cons (list name type doc) alist))
1320 (backward-char 1))))
1321 (pop-to-buffer (generate-new-buffer "*info-doc*"))
1322 (setq buffer-undo-list t)
1323 ;; Write the output header.
1324 (insert "\\input texinfo @c -*-texinfo-*-\n"
1325 "@setfilename emacsdoc.info\n"
1326 "@settitle Command Summary for GNU Emacs\n"
1327 "@finalout\n"
1328 "\n@node Top\n"
1329 "@unnumbered Command Summary for GNU Emacs\n\n"
1330 "@table @asis\n\n"
1331 "@iftex\n"
1332 "@global@let@ITEM@item\n"
1333 "@def@item{@filbreak@vskip5pt@ITEM}\n"
1334 "@font@tensy cmsy10 scaled @magstephalf\n"
1335 "@font@teni cmmi10 scaled @magstephalf\n"
1336 "@def\\{{@tensy@char110}}\n" ; this backslash goes with cmr10
1337 "@def|{{@tensy@char106}}\n"
1338 "@def@{{{@tensy@char102}}\n"
1339 "@def@}{{@tensy@char103}}\n"
1340 "@def<{{@teni@char62}}\n"
1341 "@def>{{@teni@char60}}\n"
1342 "@chardef@@64\n"
1343 "@catcode43=12\n"
1344 "@tableindent-0.2in\n"
1345 "@end iftex\n")
1346 ;; Sort the array by name; within each name, by type (functions first).
1347 (setq alist (sort alist (lambda (e1 e2)
1348 (if (string-equal (car e1) (car e2))
1349 (<= (cadr e1) (cadr e2))
1350 (string-lessp (car e1) (car e2))))))
1351 ;; Print each function.
1352 (dolist (e alist)
1353 (insert "\n@item "
1354 (if (char-equal (cadr e) ?\F) "Function" "Variable")
1355 " @code{" (car e) "}\n@display\n"
1356 (nth 2 e)
1357 "\n@end display\n")
1358 ;; Try to avoid a save size overflow in the TeX output routine.
1359 (if (zerop (setq i (% (1+ i) 100)))
1360 (insert "\n@end table\n@table @asis\n")))
1361 (insert "@end table\n"
1362 "@bye\n")
1363 (setq buffer-undo-list nil)
1364 (texinfo-mode)))
1366 (provide 'help-fns)
1368 ;;; help-fns.el ends here