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