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