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