merge trunk
[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-2012
4 ;; Free Software 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 ;; Functions
36
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))))))
65
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 (consp def) "anonymous" def)
80 (match-string 1 docstring))
81 (unless (zerop (match-beginning 0))
82 (substring docstring 0 (match-beginning 0))))))
83
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))))))
102
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)))
156
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)))
169
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 "\1f" name "\n"))))
193 (re-search-backward "\1fS\\(.*\\)")
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)))))
205
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")
211
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))
218
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))
238
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))
265
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').
275
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.
285
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))))))))
322
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 ~/.emacs.elc, try ~/.emacs.
340 ((let (fn)
341 (and (string-equal file-name
342 (expand-file-name ".emacs.elc" "~"))
343 (file-readable-p (setq fn (expand-file-name ".emacs" "~")))
344 fn)))
345 ;; When the Elisp source file can be found in the install
346 ;; directory, return the name of that file.
347 ((let ((lib-name
348 (if (string-match "[.]elc\\'" file-name)
349 (substring-no-properties file-name 0 -1)
350 file-name)))
351 (or (and (file-readable-p lib-name) lib-name)
352 ;; The library might be compressed.
353 (and (file-readable-p (concat lib-name ".gz")) lib-name))))
354 ((let* ((lib-name (file-name-nondirectory file-name))
355 ;; The next form is from `describe-simplify-lib-file-name'.
356 (file-name
357 ;; Try converting the absolute file name to a library
358 ;; name, convert that back to a file name and see if we
359 ;; get the original one. If so, they are equivalent.
360 (if (equal file-name (locate-file lib-name load-path '("")))
361 (if (string-match "[.]elc\\'" lib-name)
362 (substring-no-properties lib-name 0 -1)
363 lib-name)
364 file-name))
365 ;; The next three forms are from `find-source-lisp-file'.
366 (elc-file (locate-file
367 (concat file-name
368 (if (string-match "\\.el\\'" file-name)
369 "c"
370 ".elc"))
371 load-path nil 'readable))
372 (str (when elc-file
373 (with-temp-buffer
374 (insert-file-contents-literally elc-file nil 0 256)
375 (buffer-string))))
376 (src-file (and str
377 (string-match ";;; from file \\(.*\\.el\\)" str)
378 (match-string 1 str))))
379 (and src-file (file-readable-p src-file) src-file))))))
380
381 (declare-function ad-get-advice-info "advice" (function))
382
383 (defun help-fns--key-bindings (function)
384 (when (commandp function)
385 (let ((pt2 (with-current-buffer standard-output (point)))
386 (remapped (command-remapping function)))
387 (unless (memq remapped '(ignore undefined))
388 (let ((keys (where-is-internal
389 (or remapped function) overriding-local-map nil nil))
390 non-modified-keys)
391 (if (and (eq function 'self-insert-command)
392 (vectorp (car-safe keys))
393 (consp (aref (car keys) 0)))
394 (princ "It is bound to many ordinary text characters.\n")
395 ;; Which non-control non-meta keys run this command?
396 (dolist (key keys)
397 (if (member (event-modifiers (aref key 0)) '(nil (shift)))
398 (push key non-modified-keys)))
399 (when remapped
400 (princ "Its keys are remapped to ")
401 (princ (if (symbolp remapped)
402 (concat "`" (symbol-name remapped) "'")
403 "an anonymous command"))
404 (princ ".\n"))
405
406 (when keys
407 (princ (if remapped
408 "Without this remapping, it would be bound to "
409 "It is bound to "))
410 ;; If lots of ordinary text characters run this command,
411 ;; don't mention them one by one.
412 (if (< (length non-modified-keys) 10)
413 (princ (mapconcat 'key-description keys ", "))
414 (dolist (key non-modified-keys)
415 (setq keys (delq key keys)))
416 (if keys
417 (progn
418 (princ (mapconcat 'key-description keys ", "))
419 (princ ", and many ordinary text characters"))
420 (princ "many ordinary text characters"))))
421 (when (or remapped keys non-modified-keys)
422 (princ ".")
423 (terpri)))))
424
425 (with-current-buffer standard-output
426 (fill-region-as-paragraph pt2 (point))
427 (unless (looking-back "\n\n")
428 (terpri))))))
429
430 (defun help-fns--compiler-macro (function)
431 (let ((handler (function-get function 'compiler-macro)))
432 (when handler
433 (insert "\nThis function has a compiler macro")
434 (let ((lib (get function 'compiler-macro-file)))
435 ;; FIXME: rather than look at the compiler-macro-file property,
436 ;; just look at `handler' itself.
437 (when (stringp lib)
438 (insert (format " in `%s'" lib))
439 (save-excursion
440 (re-search-backward "`\\([^`']+\\)'" nil t)
441 (help-xref-button 1 'help-function-cmacro function lib))))
442 (insert ".\n"))))
443
444 (defun help-fns--signature (function doc real-def real-function)
445 (unless (keymapp function) ; If definition is a keymap, skip arglist note.
446 (let* ((advertised (gethash real-def advertised-signature-table t))
447 (arglist (if (listp advertised)
448 advertised (help-function-arglist real-def)))
449 (usage (help-split-fundoc doc function)))
450 (if usage (setq doc (cdr usage)))
451 (let* ((use (cond
452 ((and usage (not (listp advertised))) (car usage))
453 ((listp arglist)
454 (format "%S" (help-make-usage function arglist)))
455 ((stringp arglist) arglist)
456 ;; Maybe the arglist is in the docstring of a symbol
457 ;; this one is aliased to.
458 ((let ((fun real-function))
459 (while (and (symbolp fun)
460 (setq fun (symbol-function fun))
461 (not (setq usage (help-split-fundoc
462 (documentation fun)
463 function)))))
464 usage)
465 (car usage))
466 ((or (stringp real-def)
467 (vectorp real-def))
468 (format "\nMacro: %s" (format-kbd-macro real-def)))
469 (t "[Missing arglist. Please make a bug report.]")))
470 (high (help-highlight-arguments use doc)))
471 (let ((fill-begin (point)))
472 (insert (car high) "\n")
473 (fill-region fill-begin (point)))
474 (cdr high)))))
475
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 "\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 "'.\n"))))
489
490 (defun help-fns--obsolete (function)
491 (let* ((obsolete (and
492 ;; `function' might be a lambda construct.
493 (symbolp function)
494 (get function 'byte-obsolete-info)))
495 (use (car obsolete)))
496 (when obsolete
497 (insert "\nThis function is obsolete")
498 (when (nth 2 obsolete)
499 (insert (format " since %s" (nth 2 obsolete))))
500 (insert (cond ((stringp use) (concat ";\n" use))
501 (use (format ";\nuse `%s' instead." use))
502 (t "."))
503 "\n"))))
504
505 ;; We could use `symbol-file' but this is a wee bit more efficient.
506 (defun help-fns--autoloaded-p (function file)
507 "Return non-nil if FUNCTION has previously been autoloaded.
508 FILE is the file where FUNCTION was probably defined."
509 (let* ((file (file-name-sans-extension (file-truename file)))
510 (load-hist load-history)
511 (target (cons t function))
512 found)
513 (while (and load-hist (not found))
514 (and (caar load-hist)
515 (equal (file-name-sans-extension (caar load-hist)) file)
516 (setq found (member target (cdar load-hist))))
517 (setq load-hist (cdr load-hist)))
518 found))
519
520 ;;;###autoload
521 (defun describe-function-1 (function)
522 (let* ((advised (and (symbolp function) (featurep 'advice)
523 (ad-get-advice-info function)))
524 ;; If the function is advised, use the symbol that has the
525 ;; real definition, if that symbol is already set up.
526 (real-function
527 (or (and advised
528 (let ((origname (cdr (assq 'origname advised))))
529 (and (fboundp origname) origname)))
530 function))
531 ;; Get the real definition.
532 (def (if (symbolp real-function)
533 (symbol-function real-function)
534 function))
535 (aliased (symbolp def))
536 (real-def (if aliased
537 (let ((f def))
538 (while (and (fboundp f)
539 (symbolp (symbol-function f)))
540 (setq f (symbol-function f)))
541 f)
542 def))
543 (file-name (find-lisp-object-file-name function def))
544 (pt1 (with-current-buffer (help-buffer) (point)))
545 (beg (if (and (or (byte-code-function-p def)
546 (keymapp def)
547 (memq (car-safe def) '(macro lambda closure)))
548 file-name
549 (help-fns--autoloaded-p function file-name))
550 (if (commandp def)
551 "an interactive autoloaded "
552 "an autoloaded ")
553 (if (commandp def) "an interactive " "a "))))
554
555 ;; Print what kind of function-like object FUNCTION is.
556 (princ (cond ((or (stringp def) (vectorp def))
557 "a keyboard macro")
558 ((subrp def)
559 (if (eq 'unevalled (cdr (subr-arity def)))
560 (concat beg "special form")
561 (concat beg "built-in function")))
562 ((byte-code-function-p def)
563 (concat beg "compiled Lisp function"))
564 (aliased
565 (format "an alias for `%s'" real-def))
566 ((eq (car-safe def) 'lambda)
567 (concat beg "Lisp function"))
568 ((eq (car-safe def) 'macro)
569 (concat beg "Lisp macro"))
570 ((eq (car-safe def) 'closure)
571 (concat beg "Lisp closure"))
572 ((autoloadp def)
573 (format "%s autoloaded %s"
574 (if (commandp def) "an interactive" "an")
575 (if (eq (nth 4 def) 'keymap) "keymap"
576 (if (nth 4 def) "Lisp macro" "Lisp function"))))
577 ((keymapp def)
578 (let ((is-full nil)
579 (elts (cdr-safe def)))
580 (while elts
581 (if (char-table-p (car-safe elts))
582 (setq is-full t
583 elts nil))
584 (setq elts (cdr-safe elts)))
585 (concat beg (if is-full "keymap" "sparse keymap"))))
586 (t "")))
587
588 (if (and aliased (not (fboundp real-def)))
589 (princ ",\nwhich is not defined. Please make a bug report.")
590 (with-current-buffer standard-output
591 (save-excursion
592 (save-match-data
593 (when (re-search-backward "alias for `\\([^`']+\\)'" nil t)
594 (help-xref-button 1 'help-function real-def)))))
595
596 (when file-name
597 (princ " in `")
598 ;; We used to add .el to the file name,
599 ;; but that's completely wrong when the user used load-file.
600 (princ (if (eq file-name 'C-source)
601 "C source code"
602 (file-name-nondirectory file-name)))
603 (princ "'")
604 ;; Make a hyperlink to the library.
605 (with-current-buffer standard-output
606 (save-excursion
607 (re-search-backward "`\\([^`']+\\)'" nil t)
608 (help-xref-button 1 'help-function-def function file-name))))
609 (princ ".")
610 (with-current-buffer (help-buffer)
611 (fill-region-as-paragraph (save-excursion (goto-char pt1) (forward-line 0) (point))
612 (point)))
613 (terpri)(terpri)
614
615 (let* ((doc-raw (condition-case err
616 (documentation function t)
617 (error (format "No Doc! %S" err))))
618 ;; If the function is autoloaded, and its docstring has
619 ;; key substitution constructs, load the library.
620 (doc (progn
621 (and (autoloadp real-def)
622 help-enable-auto-load
623 (string-match "\\([^\\]=\\|[^=]\\|\\`\\)\\\\[[{<]"
624 doc-raw)
625 (load (cadr real-def) t))
626 (substitute-command-keys doc-raw))))
627
628 (help-fns--key-bindings function)
629 (with-current-buffer standard-output
630 (setq doc (help-fns--signature function doc real-def real-function))
631
632 (help-fns--compiler-macro function)
633 (help-fns--parent-mode function)
634 (help-fns--obsolete function)
635
636 (insert "\n"
637 (or doc "Not documented.")))))))
638
639 \f
640 ;; Variables
641
642 ;;;###autoload
643 (defun variable-at-point (&optional any-symbol)
644 "Return the bound variable symbol found at or before point.
645 Return 0 if there is no such symbol.
646 If ANY-SYMBOL is non-nil, don't insist the symbol be bound."
647 (with-syntax-table emacs-lisp-mode-syntax-table
648 (or (condition-case ()
649 (save-excursion
650 (skip-chars-forward "'")
651 (or (not (zerop (skip-syntax-backward "_w")))
652 (eq (char-syntax (following-char)) ?w)
653 (eq (char-syntax (following-char)) ?_)
654 (forward-sexp -1))
655 (skip-chars-forward "'")
656 (let ((obj (read (current-buffer))))
657 (and (symbolp obj) (boundp obj) obj)))
658 (error nil))
659 (let* ((str (find-tag-default))
660 (sym (if str (intern-soft str))))
661 (if (and sym (or any-symbol (boundp sym)))
662 sym
663 (save-match-data
664 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
665 (setq sym (intern-soft (match-string 1 str)))
666 (and (or any-symbol (boundp sym)) sym)))))
667 0)))
668
669 (defun describe-variable-custom-version-info (variable)
670 (let ((custom-version (get variable 'custom-version))
671 (cpv (get variable 'custom-package-version))
672 (output nil))
673 (if custom-version
674 (setq output
675 (format "This variable was introduced, or its default value was changed, in\nversion %s of Emacs.\n"
676 custom-version))
677 (when cpv
678 (let* ((package (car-safe cpv))
679 (version (if (listp (cdr-safe cpv))
680 (car (cdr-safe cpv))
681 (cdr-safe cpv)))
682 (pkg-versions (assq package customize-package-emacs-version-alist))
683 (emacsv (cdr (assoc version pkg-versions))))
684 (if (and package version)
685 (setq output
686 (format (concat "This variable was introduced, or its default value was changed, in\nversion %s of the %s package"
687 (if emacsv
688 (format " that is part of Emacs %s" emacsv))
689 ".\n")
690 version package))))))
691 output))
692
693 ;;;###autoload
694 (defun describe-variable (variable &optional buffer frame)
695 "Display the full documentation of VARIABLE (a symbol).
696 Returns the documentation as a string, also.
697 If VARIABLE has a buffer-local value in BUFFER or FRAME
698 \(default to the current buffer and current frame),
699 it is displayed along with the global value."
700 (interactive
701 (let ((v (variable-at-point))
702 (enable-recursive-minibuffers t)
703 val)
704 (setq val (completing-read (if (symbolp v)
705 (format
706 "Describe variable (default %s): " v)
707 "Describe variable: ")
708 obarray
709 (lambda (vv)
710 (or (get vv 'variable-documentation)
711 (and (boundp vv) (not (keywordp vv)))))
712 t nil nil
713 (if (symbolp v) (symbol-name v))))
714 (list (if (equal val "")
715 v (intern val)))))
716 (let (file-name)
717 (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
718 (unless (frame-live-p frame) (setq frame (selected-frame)))
719 (if (not (symbolp variable))
720 (message "You did not specify a variable")
721 (save-excursion
722 (let ((valvoid (not (with-current-buffer buffer (boundp variable))))
723 (permanent-local (get variable 'permanent-local))
724 val val-start-pos locus)
725 ;; Extract the value before setting up the output buffer,
726 ;; in case `buffer' *is* the output buffer.
727 (unless valvoid
728 (with-selected-frame frame
729 (with-current-buffer buffer
730 (setq val (symbol-value variable)
731 locus (variable-binding-locus variable)))))
732 (help-setup-xref (list #'describe-variable variable buffer)
733 (called-interactively-p 'interactive))
734 (with-help-window (help-buffer)
735 (with-current-buffer buffer
736 (prin1 variable)
737 (setq file-name (find-lisp-object-file-name variable 'defvar))
738
739 (if file-name
740 (progn
741 (princ " is a variable defined in `")
742 (princ (if (eq file-name 'C-source)
743 "C source code"
744 (file-name-nondirectory file-name)))
745 (princ "'.\n")
746 (with-current-buffer standard-output
747 (save-excursion
748 (re-search-backward "`\\([^`']+\\)'" nil t)
749 (help-xref-button 1 'help-variable-def
750 variable file-name)))
751 (if valvoid
752 (princ "It is void as a variable.")
753 (princ "Its ")))
754 (if valvoid
755 (princ " is void as a variable.")
756 (princ "'s "))))
757 (unless valvoid
758 (with-current-buffer standard-output
759 (setq val-start-pos (point))
760 (princ "value is ")
761 (let ((from (point))
762 (line-beg (line-beginning-position))
763 (print-rep
764 (let ((print-quoted t))
765 (prin1-to-string val))))
766 (if (< (+ (length print-rep) (point) (- line-beg)) 68)
767 (insert print-rep)
768 (terpri)
769 (pp val)
770 (if (< (point) (+ 68 (line-beginning-position 0)))
771 (delete-region from (1+ from))
772 (delete-region (1- from) from)))
773 (let* ((sv (get variable 'standard-value))
774 (origval (and (consp sv)
775 (condition-case nil
776 (eval (car sv))
777 (error :help-eval-error)))))
778 (when (and (consp sv)
779 (not (equal origval val))
780 (not (equal origval :help-eval-error)))
781 (princ "\nOriginal value was \n")
782 (setq from (point))
783 (pp origval)
784 (if (< (point) (+ from 20))
785 (delete-region (1- from) from)))))))
786 (terpri)
787 (when locus
788 (cond
789 ((bufferp locus)
790 (princ (format "Local in buffer %s; "
791 (buffer-name))))
792 ((framep locus)
793 (princ (format "It is a frame-local variable; ")))
794 ((terminal-live-p locus)
795 (princ (format "It is a terminal-local variable; ")))
796 (t
797 (princ (format "It is local to %S" locus))))
798 (if (not (default-boundp variable))
799 (princ "globally void")
800 (let ((global-val (default-value variable)))
801 (with-current-buffer standard-output
802 (princ "global value is ")
803 (if (eq val global-val)
804 (princ "the same.")
805 (terpri)
806 ;; Fixme: pp can take an age if you happen to
807 ;; ask for a very large expression. We should
808 ;; probably print it raw once and check it's a
809 ;; sensible size before prettyprinting. -- fx
810 (let ((from (point)))
811 (pp global-val)
812 ;; See previous comment for this function.
813 ;; (help-xref-on-pp from (point))
814 (if (< (point) (+ from 20))
815 (delete-region (1- from) from)))))))
816 (terpri))
817
818 ;; If the value is large, move it to the end.
819 (with-current-buffer standard-output
820 (when (> (count-lines (point-min) (point-max)) 10)
821 ;; Note that setting the syntax table like below
822 ;; makes forward-sexp move over a `'s' at the end
823 ;; of a symbol.
824 (set-syntax-table emacs-lisp-mode-syntax-table)
825 (goto-char val-start-pos)
826 ;; The line below previously read as
827 ;; (delete-region (point) (progn (end-of-line) (point)))
828 ;; which suppressed display of the buffer local value for
829 ;; large values.
830 (when (looking-at "value is") (replace-match ""))
831 (save-excursion
832 (insert "\n\nValue:")
833 (set (make-local-variable 'help-button-cache)
834 (point-marker)))
835 (insert "value is shown ")
836 (insert-button "below"
837 'action help-button-cache
838 'follow-link t
839 'help-echo "mouse-2, RET: show value")
840 (insert ".\n")))
841 (terpri)
842
843 (let* ((alias (condition-case nil
844 (indirect-variable variable)
845 (error variable)))
846 (obsolete (get variable 'byte-obsolete-variable))
847 (use (car obsolete))
848 (safe-var (get variable 'safe-local-variable))
849 (doc (condition-case err
850 (or (documentation-property
851 variable 'variable-documentation)
852 (documentation-property
853 alias 'variable-documentation))
854 (error (format "Doc not found: %S" err))))
855 (extra-line nil))
856
857 ;; Mention if it's a local variable.
858 (cond
859 ((and (local-variable-if-set-p variable)
860 (or (not (local-variable-p variable))
861 (with-temp-buffer
862 (local-variable-if-set-p variable))))
863 (setq extra-line t)
864 (princ " Automatically becomes ")
865 (if permanent-local
866 (princ "permanently "))
867 (princ "buffer-local when set.\n"))
868 ((not permanent-local))
869 ((bufferp locus)
870 (princ " This variable's buffer-local value is permanent.\n"))
871 (t
872 (princ " This variable's value is permanent \
873 if it is given a local binding.\n")))
874
875 ;; Mention if it's an alias.
876 (unless (eq alias variable)
877 (setq extra-line t)
878 (princ (format " This variable is an alias for `%s'.\n" alias)))
879
880 (when obsolete
881 (setq extra-line t)
882 (princ " This variable is obsolete")
883 (if (nth 2 obsolete)
884 (princ (format " since %s" (nth 2 obsolete))))
885 (princ (cond ((stringp use) (concat ";\n " use))
886 (use (format ";\n use `%s' instead." (car obsolete)))
887 (t ".")))
888 (terpri))
889
890 (when (member (cons variable val) file-local-variables-alist)
891 (setq extra-line t)
892 (if (member (cons variable val) dir-local-variables-alist)
893 (let ((file (and (buffer-file-name)
894 (not (file-remote-p (buffer-file-name)))
895 (dir-locals-find-file
896 (buffer-file-name))))
897 (dir-file t))
898 (princ " This variable's value is directory-local")
899 (if (null file)
900 (princ ".\n")
901 (princ ", set ")
902 (if (consp file) ; result from cache
903 ;; If the cache element has an mtime, we
904 ;; assume it came from a file.
905 (if (nth 2 file)
906 (setq file (expand-file-name
907 dir-locals-file (car file)))
908 ;; Otherwise, assume it was set directly.
909 (setq dir-file nil)))
910 (princ (if dir-file
911 "by the file\n `"
912 "for the directory\n `"))
913 (with-current-buffer standard-output
914 (insert-text-button
915 file 'type 'help-dir-local-var-def
916 'help-args (list variable file)))
917 (princ "'.\n")))
918 (princ " This variable's value is file-local.\n")))
919
920 (when (memq variable ignored-local-variables)
921 (setq extra-line t)
922 (princ " This variable is ignored as a file-local \
923 variable.\n"))
924
925 ;; Can be both risky and safe, eg auto-fill-function.
926 (when (risky-local-variable-p variable)
927 (setq extra-line t)
928 (princ " This variable may be risky if used as a \
929 file-local variable.\n")
930 (when (assq variable safe-local-variable-values)
931 (princ " However, you have added it to \
932 `safe-local-variable-values'.\n")))
933
934 (when safe-var
935 (setq extra-line t)
936 (princ " This variable is safe as a file local variable ")
937 (princ "if its value\n satisfies the predicate ")
938 (princ (if (byte-code-function-p safe-var)
939 "which is byte-compiled expression.\n"
940 (format "`%s'.\n" safe-var))))
941
942 (if extra-line (terpri))
943 (princ "Documentation:\n")
944 (with-current-buffer standard-output
945 (insert (or doc "Not documented as a variable."))))
946
947 ;; Make a link to customize if this variable can be customized.
948 (when (custom-variable-p variable)
949 (let ((customize-label "customize"))
950 (terpri)
951 (terpri)
952 (princ (concat "You can " customize-label " this variable."))
953 (with-current-buffer standard-output
954 (save-excursion
955 (re-search-backward
956 (concat "\\(" customize-label "\\)") nil t)
957 (help-xref-button 1 'help-customize-variable variable))))
958 ;; Note variable's version or package version
959 (let ((output (describe-variable-custom-version-info variable)))
960 (when output
961 (terpri)
962 (terpri)
963 (princ output))))
964
965 (with-current-buffer standard-output
966 ;; Return the text we displayed.
967 (buffer-string))))))))
968
969
970 ;;;###autoload
971 (defun describe-syntax (&optional buffer)
972 "Describe the syntax specifications in the syntax table of BUFFER.
973 The descriptions are inserted in a help buffer, which is then displayed.
974 BUFFER defaults to the current buffer."
975 (interactive)
976 (setq buffer (or buffer (current-buffer)))
977 (help-setup-xref (list #'describe-syntax buffer)
978 (called-interactively-p 'interactive))
979 (with-help-window (help-buffer)
980 (let ((table (with-current-buffer buffer (syntax-table))))
981 (with-current-buffer standard-output
982 (describe-vector table 'internal-describe-syntax-value)
983 (while (setq table (char-table-parent table))
984 (insert "\nThe parent syntax table is:")
985 (describe-vector table 'internal-describe-syntax-value))))))
986
987 (defun help-describe-category-set (value)
988 (insert (cond
989 ((null value) "default")
990 ((char-table-p value) "deeper char-table ...")
991 (t (condition-case nil
992 (category-set-mnemonics value)
993 (error "invalid"))))))
994
995 ;;;###autoload
996 (defun describe-categories (&optional buffer)
997 "Describe the category specifications in the current category table.
998 The descriptions are inserted in a buffer, which is then displayed.
999 If BUFFER is non-nil, then describe BUFFER's category table instead.
1000 BUFFER should be a buffer or a buffer name."
1001 (interactive)
1002 (setq buffer (or buffer (current-buffer)))
1003 (help-setup-xref (list #'describe-categories buffer)
1004 (called-interactively-p 'interactive))
1005 (with-help-window (help-buffer)
1006 (let* ((table (with-current-buffer buffer (category-table)))
1007 (docs (char-table-extra-slot table 0)))
1008 (if (or (not (vectorp docs)) (/= (length docs) 95))
1009 (error "Invalid first extra slot in this category table\n"))
1010 (with-current-buffer standard-output
1011 (insert "Legend of category mnemonics (see the tail for the longer description)\n")
1012 (let ((pos (point)) (items 0) lines n)
1013 (dotimes (i 95)
1014 (if (aref docs i) (setq items (1+ items))))
1015 (setq lines (1+ (/ (1- items) 4)))
1016 (setq n 0)
1017 (dotimes (i 95)
1018 (let ((elt (aref docs i)))
1019 (when elt
1020 (string-match ".*" elt)
1021 (setq elt (match-string 0 elt))
1022 (if (>= (length elt) 17)
1023 (setq elt (concat (substring elt 0 14) "...")))
1024 (if (< (point) (point-max))
1025 (move-to-column (* 20 (/ n lines)) t))
1026 (insert (+ i ?\s) ?: elt)
1027 (if (< (point) (point-max))
1028 (forward-line 1)
1029 (insert "\n"))
1030 (setq n (1+ n))
1031 (if (= (% n lines) 0)
1032 (goto-char pos))))))
1033 (goto-char (point-max))
1034 (insert "\n"
1035 "character(s)\tcategory mnemonics\n"
1036 "------------\t------------------")
1037 (describe-vector table 'help-describe-category-set)
1038 (insert "Legend of category mnemonics:\n")
1039 (dotimes (i 95)
1040 (let ((elt (aref docs i)))
1041 (when elt
1042 (if (string-match "\n" elt)
1043 (setq elt (substring elt (match-end 0))))
1044 (insert (+ i ?\s) ": " elt "\n"))))
1045 (while (setq table (char-table-parent table))
1046 (insert "\nThe parent category table is:")
1047 (describe-vector table 'help-describe-category-set))))))
1048
1049 \f
1050 ;;; Replacements for old lib-src/ programs. Don't seem especially useful.
1051
1052 ;; Replaces lib-src/digest-doc.c.
1053 ;;;###autoload
1054 (defun doc-file-to-man (file)
1055 "Produce an nroff buffer containing the doc-strings from the DOC file."
1056 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1057 internal-doc-file-name t)))
1058 (or (file-readable-p file)
1059 (error "Cannot read file `%s'" file))
1060 (pop-to-buffer (generate-new-buffer "*man-doc*"))
1061 (setq buffer-undo-list t)
1062 (insert ".TH \"Command Summary for GNU Emacs\"\n"
1063 ".AU Richard M. Stallman\n")
1064 (insert-file-contents file)
1065 (let (notfirst)
1066 (while (search-forward "\1f" nil 'move)
1067 (if (looking-at "S")
1068 (delete-region (1- (point)) (line-end-position))
1069 (delete-char -1)
1070 (if notfirst
1071 (insert "\n.DE\n")
1072 (setq notfirst t))
1073 (insert "\n.SH ")
1074 (insert (if (looking-at "F") "Function " "Variable "))
1075 (delete-char 1)
1076 (forward-line 1)
1077 (insert ".DS L\n"))))
1078 (insert "\n.DE\n")
1079 (setq buffer-undo-list nil)
1080 (nroff-mode))
1081
1082 ;; Replaces lib-src/sorted-doc.c.
1083 ;;;###autoload
1084 (defun doc-file-to-info (file)
1085 "Produce a texinfo buffer with sorted doc-strings from the DOC file."
1086 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1087 internal-doc-file-name t)))
1088 (or (file-readable-p file)
1089 (error "Cannot read file `%s'" file))
1090 (let ((i 0) type name doc alist)
1091 (with-temp-buffer
1092 (insert-file-contents file)
1093 ;; The characters "@{}" need special treatment.
1094 (while (re-search-forward "[@{}]" nil t)
1095 (backward-char)
1096 (insert "@")
1097 (forward-char 1))
1098 (goto-char (point-min))
1099 (while (search-forward "\1f" nil t)
1100 (unless (looking-at "S")
1101 (setq type (char-after)
1102 name (buffer-substring (1+ (point)) (line-end-position))
1103 doc (buffer-substring (line-beginning-position 2)
1104 (if (search-forward "\1f" nil 'move)
1105 (1- (point))
1106 (point)))
1107 alist (cons (list name type doc) alist))
1108 (backward-char 1))))
1109 (pop-to-buffer (generate-new-buffer "*info-doc*"))
1110 (setq buffer-undo-list t)
1111 ;; Write the output header.
1112 (insert "\\input texinfo @c -*-texinfo-*-\n"
1113 "@setfilename emacsdoc.info\n"
1114 "@settitle Command Summary for GNU Emacs\n"
1115 "@finalout\n"
1116 "\n@node Top\n"
1117 "@unnumbered Command Summary for GNU Emacs\n\n"
1118 "@table @asis\n\n"
1119 "@iftex\n"
1120 "@global@let@ITEM@item\n"
1121 "@def@item{@filbreak@vskip5pt@ITEM}\n"
1122 "@font@tensy cmsy10 scaled @magstephalf\n"
1123 "@font@teni cmmi10 scaled @magstephalf\n"
1124 "@def\\{{@tensy@char110}}\n" ; this backslash goes with cmr10
1125 "@def|{{@tensy@char106}}\n"
1126 "@def@{{{@tensy@char102}}\n"
1127 "@def@}{{@tensy@char103}}\n"
1128 "@def<{{@teni@char62}}\n"
1129 "@def>{{@teni@char60}}\n"
1130 "@chardef@@64\n"
1131 "@catcode43=12\n"
1132 "@tableindent-0.2in\n"
1133 "@end iftex\n")
1134 ;; Sort the array by name; within each name, by type (functions first).
1135 (setq alist (sort alist (lambda (e1 e2)
1136 (if (string-equal (car e1) (car e2))
1137 (<= (cadr e1) (cadr e2))
1138 (string-lessp (car e1) (car e2))))))
1139 ;; Print each function.
1140 (dolist (e alist)
1141 (insert "\n@item "
1142 (if (char-equal (cadr e) ?\F) "Function" "Variable")
1143 " @code{" (car e) "}\n@display\n"
1144 (nth 2 e)
1145 "\n@end display\n")
1146 ;; Try to avoid a save size overflow in the TeX output routine.
1147 (if (zerop (setq i (% (1+ i) 100)))
1148 (insert "\n@end table\n@table @asis\n")))
1149 (insert "@end table\n"
1150 "@bye\n")
1151 (setq buffer-undo-list nil)
1152 (texinfo-mode)))
1153
1154 (provide 'help-fns)
1155
1156 ;;; help-fns.el ends here