Merge from 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 (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 ;;;###autoload
384 (defun describe-function-1 (function)
385 (let* ((advised (and (symbolp function) (featurep 'advice)
386 (ad-get-advice-info function)))
387 ;; If the function is advised, use the symbol that has the
388 ;; real definition, if that symbol is already set up.
389 (real-function
390 (or (and advised
391 (let ((origname (cdr (assq 'origname advised))))
392 (and (fboundp origname) origname)))
393 function))
394 ;; Get the real definition.
395 (def (if (symbolp real-function)
396 (symbol-function real-function)
397 function))
398 file-name string
399 (beg (if (commandp def) "an interactive " "a "))
400 (pt1 (with-current-buffer (help-buffer) (point)))
401 errtype)
402 (setq string
403 (cond ((or (stringp def) (vectorp def))
404 "a keyboard macro")
405 ((subrp def)
406 (if (eq 'unevalled (cdr (subr-arity def)))
407 (concat beg "special form")
408 (concat beg "built-in function")))
409 ((byte-code-function-p def)
410 (concat beg "compiled Lisp function"))
411 ((symbolp def)
412 (while (and (fboundp def)
413 (symbolp (symbol-function def)))
414 (setq def (symbol-function def)))
415 ;; Handle (defalias 'foo 'bar), where bar is undefined.
416 (or (fboundp def) (setq errtype 'alias))
417 (format "an alias for `%s'" def))
418 ((eq (car-safe def) 'lambda)
419 (concat beg "Lisp function"))
420 ((eq (car-safe def) 'macro)
421 "a Lisp macro")
422 ((eq (car-safe def) 'closure)
423 (concat beg "Lisp closure"))
424 ((eq (car-safe def) 'autoload)
425 (format "%s autoloaded %s"
426 (if (commandp def) "an interactive" "an")
427 (if (eq (nth 4 def) 'keymap) "keymap"
428 (if (nth 4 def) "Lisp macro" "Lisp function"))))
429 ((keymapp def)
430 (let ((is-full nil)
431 (elts (cdr-safe def)))
432 (while elts
433 (if (char-table-p (car-safe elts))
434 (setq is-full t
435 elts nil))
436 (setq elts (cdr-safe elts)))
437 (if is-full
438 "a full keymap"
439 "a sparse keymap")))
440 (t "")))
441 (princ string)
442 (if (eq errtype 'alias)
443 (princ ",\nwhich is not defined. Please make a bug report.")
444 (with-current-buffer standard-output
445 (save-excursion
446 (save-match-data
447 (when (re-search-backward "alias for `\\([^`']+\\)'" nil t)
448 (help-xref-button 1 'help-function def)))))
449
450 (setq file-name (find-lisp-object-file-name function def))
451 (when file-name
452 (princ " in `")
453 ;; We used to add .el to the file name,
454 ;; but that's completely wrong when the user used load-file.
455 (princ (if (eq file-name 'C-source)
456 "C source code"
457 (file-name-nondirectory file-name)))
458 (princ "'")
459 ;; Make a hyperlink to the library.
460 (with-current-buffer standard-output
461 (save-excursion
462 (re-search-backward "`\\([^`']+\\)'" nil t)
463 (help-xref-button 1 'help-function-def function file-name))))
464 (princ ".")
465 (with-current-buffer (help-buffer)
466 (fill-region-as-paragraph (save-excursion (goto-char pt1) (forward-line 0) (point))
467 (point)))
468 (terpri)(terpri)
469 (when (commandp function)
470 (let ((pt2 (with-current-buffer (help-buffer) (point)))
471 (remapped (command-remapping function)))
472 (unless (memq remapped '(ignore undefined))
473 (let ((keys (where-is-internal
474 (or remapped function) overriding-local-map nil nil))
475 non-modified-keys)
476 (if (and (eq function 'self-insert-command)
477 (vectorp (car-safe keys))
478 (consp (aref (car keys) 0)))
479 (princ "It is bound to many ordinary text characters.\n")
480 ;; Which non-control non-meta keys run this command?
481 (dolist (key keys)
482 (if (member (event-modifiers (aref key 0)) '(nil (shift)))
483 (push key non-modified-keys)))
484 (when remapped
485 (princ "Its keys are remapped to `")
486 (princ (symbol-name remapped))
487 (princ "'.\n"))
488
489 (when keys
490 (princ (if remapped
491 "Without this remapping, it would be bound to "
492 "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 (line-beg (line-beginning-position))
714 ;;
715 (print-rep
716 (let ((print-quoted t))
717 (prin1-to-string val))))
718 (if (< (+ (length print-rep) (point) (- line-beg)) 68)
719 (insert print-rep)
720 (terpri)
721 (pp val)
722 (if (< (point) (+ 68 (line-beginning-position 0)))
723 (delete-region from (1+ from))
724 (delete-region (1- from) from)))
725 (let* ((sv (get variable 'standard-value))
726 (origval (and (consp sv)
727 (condition-case nil
728 (eval (car sv))
729 (error :help-eval-error)))))
730 (when (and (consp sv)
731 (not (equal origval val))
732 (not (equal origval :help-eval-error)))
733 (princ "\nOriginal value was \n")
734 (setq from (point))
735 (pp origval)
736 (if (< (point) (+ from 20))
737 (delete-region (1- from) from)))))))
738 (terpri)
739 (when locus
740 (cond
741 ((bufferp locus)
742 (princ (format "%socal in buffer %s; "
743 (if (get variable 'permanent-local)
744 "Permanently l" "L")
745 (buffer-name))))
746 ((framep locus)
747 (princ (format "It is a frame-local variable; ")))
748 ((terminal-live-p locus)
749 (princ (format "It is a terminal-local variable; ")))
750 (t
751 (princ (format "It is local to %S" locus))))
752 (if (not (default-boundp variable))
753 (princ "globally void")
754 (let ((val (default-value variable)))
755 (with-current-buffer standard-output
756 (princ "global value is ")
757 (terpri)
758 ;; Fixme: pp can take an age if you happen to
759 ;; ask for a very large expression. We should
760 ;; probably print it raw once and check it's a
761 ;; sensible size before prettyprinting. -- fx
762 (let ((from (point)))
763 (pp val)
764 ;; See previous comment for this function.
765 ;; (help-xref-on-pp from (point))
766 (if (< (point) (+ from 20))
767 (delete-region (1- from) from))))))
768 (terpri))
769
770 ;; If the value is large, move it to the end.
771 (with-current-buffer standard-output
772 (when (> (count-lines (point-min) (point-max)) 10)
773 ;; Note that setting the syntax table like below
774 ;; makes forward-sexp move over a `'s' at the end
775 ;; of a symbol.
776 (set-syntax-table emacs-lisp-mode-syntax-table)
777 (goto-char val-start-pos)
778 ;; The line below previously read as
779 ;; (delete-region (point) (progn (end-of-line) (point)))
780 ;; which suppressed display of the buffer local value for
781 ;; large values.
782 (when (looking-at "value is") (replace-match ""))
783 (save-excursion
784 (insert "\n\nValue:")
785 (set (make-local-variable 'help-button-cache)
786 (point-marker)))
787 (insert "value is shown ")
788 (insert-button "below"
789 'action help-button-cache
790 'follow-link t
791 'help-echo "mouse-2, RET: show value")
792 (insert ".\n")))
793 (terpri)
794
795 (let* ((alias (condition-case nil
796 (indirect-variable variable)
797 (error variable)))
798 (obsolete (get variable 'byte-obsolete-variable))
799 (use (car obsolete))
800 (safe-var (get variable 'safe-local-variable))
801 (doc (or (documentation-property variable 'variable-documentation)
802 (documentation-property alias 'variable-documentation)))
803 (extra-line nil))
804 ;; Add a note for variables that have been make-var-buffer-local.
805 (when (and (local-variable-if-set-p variable)
806 (or (not (local-variable-p variable))
807 (with-temp-buffer
808 (local-variable-if-set-p variable))))
809 (setq extra-line t)
810 (princ " Automatically becomes buffer-local when set in any fashion.\n"))
811
812 ;; Mention if it's an alias
813 (unless (eq alias variable)
814 (setq extra-line t)
815 (princ (format " This variable is an alias for `%s'.\n" alias)))
816
817 (when obsolete
818 (setq extra-line t)
819 (princ " This variable is obsolete")
820 (if (nth 2 obsolete)
821 (princ (format " since %s" (nth 2 obsolete))))
822 (princ (cond ((stringp use) (concat ";\n " use))
823 (use (format ";\n use `%s' instead." (car obsolete)))
824 (t ".")))
825 (terpri))
826
827 (when (member (cons variable val) file-local-variables-alist)
828 (setq extra-line t)
829 (if (member (cons variable val) dir-local-variables-alist)
830 (let ((file (and (buffer-file-name)
831 (not (file-remote-p (buffer-file-name)))
832 (dir-locals-find-file
833 (buffer-file-name))))
834 (type "file"))
835 (princ " This variable is a directory local variable")
836 (when file
837 (if (consp file) ; result from cache
838 ;; If the cache element has an mtime, we
839 ;; assume it came from a file.
840 (if (nth 2 file)
841 (setq file (expand-file-name
842 dir-locals-file (car file)))
843 ;; Otherwise, assume it was set directly.
844 (setq type "directory")))
845 (princ (format "\n from the %s \"%s\"" type file)))
846 (princ ".\n"))
847 (princ " This variable is a file local variable.\n")))
848
849 (when (memq variable ignored-local-variables)
850 (setq extra-line t)
851 (princ " This variable is ignored when used as a file local \
852 variable.\n"))
853
854 ;; Can be both risky and safe, eg auto-fill-function.
855 (when (risky-local-variable-p variable)
856 (setq extra-line t)
857 (princ " This variable is potentially risky when used as a \
858 file local variable.\n")
859 (when (assq variable safe-local-variable-values)
860 (princ " However, you have added it to \
861 `safe-local-variable-values'.\n")))
862
863 (when safe-var
864 (setq extra-line t)
865 (princ " This variable is safe as a file local variable ")
866 (princ "if its value\n satisfies the predicate ")
867 (princ (if (byte-code-function-p safe-var)
868 "which is byte-compiled expression.\n"
869 (format "`%s'.\n" safe-var))))
870
871 (if extra-line (terpri))
872 (princ "Documentation:\n")
873 (with-current-buffer standard-output
874 (insert (or doc "Not documented as a variable."))))
875
876 ;; Make a link to customize if this variable can be customized.
877 (when (custom-variable-p variable)
878 (let ((customize-label "customize"))
879 (terpri)
880 (terpri)
881 (princ (concat "You can " customize-label " this variable."))
882 (with-current-buffer standard-output
883 (save-excursion
884 (re-search-backward
885 (concat "\\(" customize-label "\\)") nil t)
886 (help-xref-button 1 'help-customize-variable variable))))
887 ;; Note variable's version or package version
888 (let ((output (describe-variable-custom-version-info variable)))
889 (when output
890 (terpri)
891 (terpri)
892 (princ output))))
893
894 (with-current-buffer standard-output
895 ;; Return the text we displayed.
896 (buffer-string))))))))
897
898
899 ;;;###autoload
900 (defun describe-syntax (&optional buffer)
901 "Describe the syntax specifications in the syntax table of BUFFER.
902 The descriptions are inserted in a help buffer, which is then displayed.
903 BUFFER defaults to the current buffer."
904 (interactive)
905 (setq buffer (or buffer (current-buffer)))
906 (help-setup-xref (list #'describe-syntax buffer)
907 (called-interactively-p 'interactive))
908 (with-help-window (help-buffer)
909 (let ((table (with-current-buffer buffer (syntax-table))))
910 (with-current-buffer standard-output
911 (describe-vector table 'internal-describe-syntax-value)
912 (while (setq table (char-table-parent table))
913 (insert "\nThe parent syntax table is:")
914 (describe-vector table 'internal-describe-syntax-value))))))
915
916 (defun help-describe-category-set (value)
917 (insert (cond
918 ((null value) "default")
919 ((char-table-p value) "deeper char-table ...")
920 (t (condition-case nil
921 (category-set-mnemonics value)
922 (error "invalid"))))))
923
924 ;;;###autoload
925 (defun describe-categories (&optional buffer)
926 "Describe the category specifications in the current category table.
927 The descriptions are inserted in a buffer, which is then displayed.
928 If BUFFER is non-nil, then describe BUFFER's category table instead.
929 BUFFER should be a buffer or a buffer name."
930 (interactive)
931 (setq buffer (or buffer (current-buffer)))
932 (help-setup-xref (list #'describe-categories buffer)
933 (called-interactively-p 'interactive))
934 (with-help-window (help-buffer)
935 (let* ((table (with-current-buffer buffer (category-table)))
936 (docs (char-table-extra-slot table 0)))
937 (if (or (not (vectorp docs)) (/= (length docs) 95))
938 (error "Invalid first extra slot in this category table\n"))
939 (with-current-buffer standard-output
940 (insert "Legend of category mnemonics (see the tail for the longer description)\n")
941 (let ((pos (point)) (items 0) lines n)
942 (dotimes (i 95)
943 (if (aref docs i) (setq items (1+ items))))
944 (setq lines (1+ (/ (1- items) 4)))
945 (setq n 0)
946 (dotimes (i 95)
947 (let ((elt (aref docs i)))
948 (when elt
949 (string-match ".*" elt)
950 (setq elt (match-string 0 elt))
951 (if (>= (length elt) 17)
952 (setq elt (concat (substring elt 0 14) "...")))
953 (if (< (point) (point-max))
954 (move-to-column (* 20 (/ n lines)) t))
955 (insert (+ i ?\s) ?: elt)
956 (if (< (point) (point-max))
957 (forward-line 1)
958 (insert "\n"))
959 (setq n (1+ n))
960 (if (= (% n lines) 0)
961 (goto-char pos))))))
962 (goto-char (point-max))
963 (insert "\n"
964 "character(s)\tcategory mnemonics\n"
965 "------------\t------------------")
966 (describe-vector table 'help-describe-category-set)
967 (insert "Legend of category mnemonics:\n")
968 (dotimes (i 95)
969 (let ((elt (aref docs i)))
970 (when elt
971 (if (string-match "\n" elt)
972 (setq elt (substring elt (match-end 0))))
973 (insert (+ i ?\s) ": " elt "\n"))))
974 (while (setq table (char-table-parent table))
975 (insert "\nThe parent category table is:")
976 (describe-vector table 'help-describe-category-set))))))
977
978 \f
979 ;;; Replacements for old lib-src/ programs. Don't seem especially useful.
980
981 ;; Replaces lib-src/digest-doc.c.
982 ;;;###autoload
983 (defun doc-file-to-man (file)
984 "Produce an nroff buffer containing the doc-strings from the DOC file."
985 (interactive (list (read-file-name "Name of DOC file: " doc-directory
986 internal-doc-file-name t)))
987 (or (file-readable-p file)
988 (error "Cannot read file `%s'" file))
989 (pop-to-buffer (generate-new-buffer "*man-doc*"))
990 (setq buffer-undo-list t)
991 (insert ".TH \"Command Summary for GNU Emacs\"\n"
992 ".AU Richard M. Stallman\n")
993 (insert-file-contents file)
994 (let (notfirst)
995 (while (search-forward "\1f" nil 'move)
996 (if (looking-at "S")
997 (delete-region (1- (point)) (line-end-position))
998 (delete-char -1)
999 (if notfirst
1000 (insert "\n.DE\n")
1001 (setq notfirst t))
1002 (insert "\n.SH ")
1003 (insert (if (looking-at "F") "Function " "Variable "))
1004 (delete-char 1)
1005 (forward-line 1)
1006 (insert ".DS L\n"))))
1007 (insert "\n.DE\n")
1008 (setq buffer-undo-list nil)
1009 (nroff-mode))
1010
1011 ;; Replaces lib-src/sorted-doc.c.
1012 ;;;###autoload
1013 (defun doc-file-to-info (file)
1014 "Produce a texinfo buffer with sorted doc-strings from the DOC file."
1015 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1016 internal-doc-file-name t)))
1017 (or (file-readable-p file)
1018 (error "Cannot read file `%s'" file))
1019 (let ((i 0) type name doc alist)
1020 (with-temp-buffer
1021 (insert-file-contents file)
1022 ;; The characters "@{}" need special treatment.
1023 (while (re-search-forward "[@{}]" nil t)
1024 (backward-char)
1025 (insert "@")
1026 (forward-char 1))
1027 (goto-char (point-min))
1028 (while (search-forward "\1f" nil t)
1029 (unless (looking-at "S")
1030 (setq type (char-after)
1031 name (buffer-substring (1+ (point)) (line-end-position))
1032 doc (buffer-substring (line-beginning-position 2)
1033 (if (search-forward "\1f" nil 'move)
1034 (1- (point))
1035 (point)))
1036 alist (cons (list name type doc) alist))
1037 (backward-char 1))))
1038 (pop-to-buffer (generate-new-buffer "*info-doc*"))
1039 (setq buffer-undo-list t)
1040 ;; Write the output header.
1041 (insert "\\input texinfo @c -*-texinfo-*-\n"
1042 "@setfilename emacsdoc.info\n"
1043 "@settitle Command Summary for GNU Emacs\n"
1044 "@finalout\n"
1045 "\n@node Top\n"
1046 "@unnumbered Command Summary for GNU Emacs\n\n"
1047 "@table @asis\n\n"
1048 "@iftex\n"
1049 "@global@let@ITEM@item\n"
1050 "@def@item{@filbreak@vskip5pt@ITEM}\n"
1051 "@font@tensy cmsy10 scaled @magstephalf\n"
1052 "@font@teni cmmi10 scaled @magstephalf\n"
1053 "@def\\{{@tensy@char110}}\n" ; this backslash goes with cmr10
1054 "@def|{{@tensy@char106}}\n"
1055 "@def@{{{@tensy@char102}}\n"
1056 "@def@}{{@tensy@char103}}\n"
1057 "@def<{{@teni@char62}}\n"
1058 "@def>{{@teni@char60}}\n"
1059 "@chardef@@64\n"
1060 "@catcode43=12\n"
1061 "@tableindent-0.2in\n"
1062 "@end iftex\n")
1063 ;; Sort the array by name; within each name, by type (functions first).
1064 (setq alist (sort alist (lambda (e1 e2)
1065 (if (string-equal (car e1) (car e2))
1066 (<= (cadr e1) (cadr e2))
1067 (string-lessp (car e1) (car e2))))))
1068 ;; Print each function.
1069 (dolist (e alist)
1070 (insert "\n@item "
1071 (if (char-equal (cadr e) ?\F) "Function" "Variable")
1072 " @code{" (car e) "}\n@display\n"
1073 (nth 2 e)
1074 "\n@end display\n")
1075 ;; Try to avoid a save size overflow in the TeX output routine.
1076 (if (zerop (setq i (% (1+ i) 100)))
1077 (insert "\n@end table\n@table @asis\n")))
1078 (insert "@end table\n"
1079 "@bye\n")
1080 (setq buffer-undo-list nil)
1081 (texinfo-mode)))
1082
1083 (provide 'help-fns)
1084
1085 ;;; help-fns.el ends here