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