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