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