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