lisp/loadup.el: Use `string-to-number', not `string-to-int'.
[emacs.git] / lisp / help-fns.el
blob97ce7ca44efe207792195fba6a6239993e11ed0c
1 ;;; help-fns.el --- Complex help functions -*- lexical-binding: t -*-
3 ;; Copyright (C) 1985-1986, 1993-1994, 1998-2011
4 ;; Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: help, internal
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This file contains those help commands which are complicated, and
28 ;; which may not be used in every session. For example
29 ;; `describe-function' will probably be heavily used when doing elisp
30 ;; programming, but not if just editing C files. Simpler help commands
31 ;; are in help.el
33 ;;; Code:
35 ;; Functions
37 ;;;###autoload
38 (defun describe-function (function)
39 "Display the full documentation of FUNCTION (a symbol)."
40 (interactive
41 (let ((fn (function-called-at-point))
42 (enable-recursive-minibuffers t)
43 val)
44 (setq val (completing-read (if fn
45 (format "Describe function (default %s): " fn)
46 "Describe function: ")
47 obarray 'fboundp t nil nil
48 (and fn (symbol-name fn))))
49 (list (if (equal val "")
50 fn (intern val)))))
51 (if (null function)
52 (message "You didn't specify a function")
53 (help-setup-xref (list #'describe-function function)
54 (called-interactively-p 'interactive))
55 (save-excursion
56 (with-help-window (help-buffer)
57 (prin1 function)
58 ;; Use " is " instead of a colon so that
59 ;; it is easier to get out the function name using forward-sexp.
60 (princ " is ")
61 (describe-function-1 function)
62 (with-current-buffer standard-output
63 ;; Return the text we displayed.
64 (buffer-string))))))
66 (defun help-split-fundoc (docstring def)
67 "Split a function DOCSTRING into the actual doc and the usage info.
68 Return (USAGE . DOC) or nil if there's no usage info.
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))))))
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))))))
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)))
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)))
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 "\x1f" name "\n"))))
196 (re-search-backward "\x1fS\\(.*\\)")
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)))))
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")
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))
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)))))
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))
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').
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.
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))))))))
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)
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))))))
383 (declare-function ad-get-advice-info "advice" (function))
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)))))
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 "'"))
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)))))
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 (let* ((obsolete (and
561 ;; function might be a lambda construct.
562 (symbolp function)
563 (get function 'byte-obsolete-info)))
564 (use (car obsolete)))
565 (when obsolete
566 (princ "\nThis function is obsolete")
567 (when (nth 2 obsolete)
568 (insert (format " since %s" (nth 2 obsolete))))
569 (insert (cond ((stringp use) (concat ";\n" use))
570 (use (format ";\nuse `%s' instead." use))
571 (t "."))
572 "\n"))
573 (insert "\n"
574 (or doc "Not documented."))))))))
577 ;; Variables
579 ;;;###autoload
580 (defun variable-at-point (&optional any-symbol)
581 "Return the bound variable symbol found at or before point.
582 Return 0 if there is no such symbol.
583 If ANY-SYMBOL is non-nil, don't insist the symbol be bound."
584 (with-syntax-table emacs-lisp-mode-syntax-table
585 (or (condition-case ()
586 (save-excursion
587 (skip-chars-forward "'")
588 (or (not (zerop (skip-syntax-backward "_w")))
589 (eq (char-syntax (following-char)) ?w)
590 (eq (char-syntax (following-char)) ?_)
591 (forward-sexp -1))
592 (skip-chars-forward "'")
593 (let ((obj (read (current-buffer))))
594 (and (symbolp obj) (boundp obj) obj)))
595 (error nil))
596 (let* ((str (find-tag-default))
597 (sym (if str (intern-soft str))))
598 (if (and sym (or any-symbol (boundp sym)))
600 (save-match-data
601 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
602 (setq sym (intern-soft (match-string 1 str)))
603 (and (or any-symbol (boundp sym)) sym)))))
604 0)))
606 (defun describe-variable-custom-version-info (variable)
607 (let ((custom-version (get variable 'custom-version))
608 (cpv (get variable 'custom-package-version))
609 (output nil))
610 (if custom-version
611 (setq output
612 (format "This variable was introduced, or its default value was changed, in\nversion %s of Emacs.\n"
613 custom-version))
614 (when cpv
615 (let* ((package (car-safe cpv))
616 (version (if (listp (cdr-safe cpv))
617 (car (cdr-safe cpv))
618 (cdr-safe cpv)))
619 (pkg-versions (assq package customize-package-emacs-version-alist))
620 (emacsv (cdr (assoc version pkg-versions))))
621 (if (and package version)
622 (setq output
623 (format (concat "This variable was introduced, or its default value was changed, in\nversion %s of the %s package"
624 (if emacsv
625 (format " that is part of Emacs %s" emacsv))
626 ".\n")
627 version package))))))
628 output))
630 ;;;###autoload
631 (defun describe-variable (variable &optional buffer frame)
632 "Display the full documentation of VARIABLE (a symbol).
633 Returns the documentation as a string, also.
634 If VARIABLE has a buffer-local value in BUFFER or FRAME
635 \(default to the current buffer and current frame),
636 it is displayed along with the global value."
637 (interactive
638 (let ((v (variable-at-point))
639 (enable-recursive-minibuffers t)
640 val)
641 (setq val (completing-read (if (symbolp v)
642 (format
643 "Describe variable (default %s): " v)
644 "Describe variable: ")
645 obarray
646 (lambda (vv)
647 (or (get vv 'variable-documentation)
648 (and (boundp vv) (not (keywordp vv)))))
649 t nil nil
650 (if (symbolp v) (symbol-name v))))
651 (list (if (equal val "")
652 v (intern val)))))
653 (let (file-name)
654 (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
655 (unless (frame-live-p frame) (setq frame (selected-frame)))
656 (if (not (symbolp variable))
657 (message "You did not specify a variable")
658 (save-excursion
659 (let ((valvoid (not (with-current-buffer buffer (boundp variable))))
660 val val-start-pos locus)
661 ;; Extract the value before setting up the output buffer,
662 ;; in case `buffer' *is* the output buffer.
663 (unless valvoid
664 (with-selected-frame frame
665 (with-current-buffer buffer
666 (setq val (symbol-value variable)
667 locus (variable-binding-locus variable)))))
668 (help-setup-xref (list #'describe-variable variable buffer)
669 (called-interactively-p 'interactive))
670 (with-help-window (help-buffer)
671 (with-current-buffer buffer
672 (prin1 variable)
673 (setq file-name (find-lisp-object-file-name variable 'defvar))
675 (if file-name
676 (progn
677 (princ " is a variable defined in `")
678 (princ (if (eq file-name 'C-source)
679 "C source code"
680 (file-name-nondirectory file-name)))
681 (princ "'.\n")
682 (with-current-buffer standard-output
683 (save-excursion
684 (re-search-backward "`\\([^`']+\\)'" nil t)
685 (help-xref-button 1 'help-variable-def
686 variable file-name)))
687 (if valvoid
688 (princ "It is void as a variable.")
689 (princ "Its ")))
690 (if valvoid
691 (princ " is void as a variable.")
692 (princ "'s "))))
693 (unless valvoid
694 (with-current-buffer standard-output
695 (setq val-start-pos (point))
696 (princ "value is ")
697 (let ((from (point)))
698 (terpri)
699 (pp val)
700 (if (< (point) (+ 68 (line-beginning-position 0)))
701 (delete-region from (1+ from))
702 (delete-region (1- from) from))
703 (let* ((sv (get variable 'standard-value))
704 (origval (and (consp sv)
705 (condition-case nil
706 (eval (car sv))
707 (error :help-eval-error)))))
708 (when (and (consp sv)
709 (not (equal origval val))
710 (not (equal origval :help-eval-error)))
711 (princ "\nOriginal value was \n")
712 (setq from (point))
713 (pp origval)
714 (if (< (point) (+ from 20))
715 (delete-region (1- from) from)))))))
716 (terpri)
717 (when locus
718 (if (bufferp locus)
719 (princ (format "%socal in buffer %s; "
720 (if (get variable 'permanent-local)
721 "Permanently l" "L")
722 (buffer-name)))
723 (princ (format "It is a frame-local variable; ")))
724 (if (not (default-boundp variable))
725 (princ "globally void")
726 (let ((val (default-value variable)))
727 (with-current-buffer standard-output
728 (princ "global value is ")
729 (terpri)
730 ;; Fixme: pp can take an age if you happen to
731 ;; ask for a very large expression. We should
732 ;; probably print it raw once and check it's a
733 ;; sensible size before prettyprinting. -- fx
734 (let ((from (point)))
735 (pp val)
736 ;; See previous comment for this function.
737 ;; (help-xref-on-pp from (point))
738 (if (< (point) (+ from 20))
739 (delete-region (1- from) from))))))
740 (terpri))
742 ;; If the value is large, move it to the end.
743 (with-current-buffer standard-output
744 (when (> (count-lines (point-min) (point-max)) 10)
745 ;; Note that setting the syntax table like below
746 ;; makes forward-sexp move over a `'s' at the end
747 ;; of a symbol.
748 (set-syntax-table emacs-lisp-mode-syntax-table)
749 (goto-char val-start-pos)
750 ;; The line below previously read as
751 ;; (delete-region (point) (progn (end-of-line) (point)))
752 ;; which suppressed display of the buffer local value for
753 ;; large values.
754 (when (looking-at "value is") (replace-match ""))
755 (save-excursion
756 (insert "\n\nValue:")
757 (set (make-local-variable 'help-button-cache)
758 (point-marker)))
759 (insert "value is shown ")
760 (insert-button "below"
761 'action help-button-cache
762 'follow-link t
763 'help-echo "mouse-2, RET: show value")
764 (insert ".\n")))
765 (terpri)
767 (let* ((alias (condition-case nil
768 (indirect-variable variable)
769 (error variable)))
770 (obsolete (get variable 'byte-obsolete-variable))
771 (use (car obsolete))
772 (safe-var (get variable 'safe-local-variable))
773 (doc (or (documentation-property variable 'variable-documentation)
774 (documentation-property alias 'variable-documentation)))
775 (extra-line nil))
776 ;; Add a note for variables that have been make-var-buffer-local.
777 (when (and (local-variable-if-set-p variable)
778 (or (not (local-variable-p variable))
779 (with-temp-buffer
780 (local-variable-if-set-p variable))))
781 (setq extra-line t)
782 (princ " Automatically becomes buffer-local when set in any fashion.\n"))
784 ;; Mention if it's an alias
785 (unless (eq alias variable)
786 (setq extra-line t)
787 (princ (format " This variable is an alias for `%s'.\n" alias)))
789 (when obsolete
790 (setq extra-line t)
791 (princ " This variable is obsolete")
792 (if (cdr obsolete) (princ (format " since %s" (cdr obsolete))))
793 (princ (cond ((stringp use) (concat ";\n " use))
794 (use (format ";\n use `%s' instead." (car obsolete)))
795 (t ".")))
796 (terpri))
798 (when (member (cons variable val) file-local-variables-alist)
799 (setq extra-line t)
800 (if (member (cons variable val) dir-local-variables-alist)
801 (let ((file (and (buffer-file-name)
802 (not (file-remote-p (buffer-file-name)))
803 (dir-locals-find-file
804 (buffer-file-name))))
805 (type "file"))
806 (princ " This variable is a directory local variable")
807 (when file
808 (if (consp file) ; result from cache
809 ;; If the cache element has an mtime, we
810 ;; assume it came from a file.
811 (if (nth 2 file)
812 (setq file (expand-file-name
813 dir-locals-file (car file)))
814 ;; Otherwise, assume it was set directly.
815 (setq type "directory")))
816 (princ (format "\n from the %s \"%s\"" type file)))
817 (princ ".\n"))
818 (princ " This variable is a file local variable.\n")))
820 (when (memq variable ignored-local-variables)
821 (setq extra-line t)
822 (princ " This variable is ignored when used as a file local \
823 variable.\n"))
825 ;; Can be both risky and safe, eg auto-fill-function.
826 (when (risky-local-variable-p variable)
827 (setq extra-line t)
828 (princ " This variable is potentially risky when used as a \
829 file local variable.\n")
830 (when (assq variable safe-local-variable-values)
831 (princ " However, you have added it to \
832 `safe-local-variable-values'.\n")))
834 (when safe-var
835 (setq extra-line t)
836 (princ " This variable is safe as a file local variable ")
837 (princ "if its value\n satisfies the predicate ")
838 (princ (if (byte-code-function-p safe-var)
839 "which is byte-compiled expression.\n"
840 (format "`%s'.\n" safe-var))))
842 (if extra-line (terpri))
843 (princ "Documentation:\n")
844 (with-current-buffer standard-output
845 (insert (or doc "Not documented as a variable."))))
847 ;; Make a link to customize if this variable can be customized.
848 (when (custom-variable-p variable)
849 (let ((customize-label "customize"))
850 (terpri)
851 (terpri)
852 (princ (concat "You can " customize-label " this variable."))
853 (with-current-buffer standard-output
854 (save-excursion
855 (re-search-backward
856 (concat "\\(" customize-label "\\)") nil t)
857 (help-xref-button 1 'help-customize-variable variable))))
858 ;; Note variable's version or package version
859 (let ((output (describe-variable-custom-version-info variable)))
860 (when output
861 (terpri)
862 (terpri)
863 (princ output))))
865 (with-current-buffer standard-output
866 ;; Return the text we displayed.
867 (buffer-string))))))))
870 ;;;###autoload
871 (defun describe-syntax (&optional buffer)
872 "Describe the syntax specifications in the syntax table of BUFFER.
873 The descriptions are inserted in a help buffer, which is then displayed.
874 BUFFER defaults to the current buffer."
875 (interactive)
876 (setq buffer (or buffer (current-buffer)))
877 (help-setup-xref (list #'describe-syntax buffer)
878 (called-interactively-p 'interactive))
879 (with-help-window (help-buffer)
880 (let ((table (with-current-buffer buffer (syntax-table))))
881 (with-current-buffer standard-output
882 (describe-vector table 'internal-describe-syntax-value)
883 (while (setq table (char-table-parent table))
884 (insert "\nThe parent syntax table is:")
885 (describe-vector table 'internal-describe-syntax-value))))))
887 (defun help-describe-category-set (value)
888 (insert (cond
889 ((null value) "default")
890 ((char-table-p value) "deeper char-table ...")
891 (t (condition-case nil
892 (category-set-mnemonics value)
893 (error "invalid"))))))
895 ;;;###autoload
896 (defun describe-categories (&optional buffer)
897 "Describe the category specifications in the current category table.
898 The descriptions are inserted in a buffer, which is then displayed.
899 If BUFFER is non-nil, then describe BUFFER's category table instead.
900 BUFFER should be a buffer or a buffer name."
901 (interactive)
902 (setq buffer (or buffer (current-buffer)))
903 (help-setup-xref (list #'describe-categories buffer)
904 (called-interactively-p 'interactive))
905 (with-help-window (help-buffer)
906 (let* ((table (with-current-buffer buffer (category-table)))
907 (docs (char-table-extra-slot table 0)))
908 (if (or (not (vectorp docs)) (/= (length docs) 95))
909 (error "Invalid first extra slot in this category table\n"))
910 (with-current-buffer standard-output
911 (insert "Legend of category mnemonics (see the tail for the longer description)\n")
912 (let ((pos (point)) (items 0) lines n)
913 (dotimes (i 95)
914 (if (aref docs i) (setq items (1+ items))))
915 (setq lines (1+ (/ (1- items) 4)))
916 (setq n 0)
917 (dotimes (i 95)
918 (let ((elt (aref docs i)))
919 (when elt
920 (string-match ".*" elt)
921 (setq elt (match-string 0 elt))
922 (if (>= (length elt) 17)
923 (setq elt (concat (substring elt 0 14) "...")))
924 (if (< (point) (point-max))
925 (move-to-column (* 20 (/ n lines)) t))
926 (insert (+ i ?\s) ?: elt)
927 (if (< (point) (point-max))
928 (forward-line 1)
929 (insert "\n"))
930 (setq n (1+ n))
931 (if (= (% n lines) 0)
932 (goto-char pos))))))
933 (goto-char (point-max))
934 (insert "\n"
935 "character(s)\tcategory mnemonics\n"
936 "------------\t------------------")
937 (describe-vector table 'help-describe-category-set)
938 (insert "Legend of category mnemonics:\n")
939 (dotimes (i 95)
940 (let ((elt (aref docs i)))
941 (when elt
942 (if (string-match "\n" elt)
943 (setq elt (substring elt (match-end 0))))
944 (insert (+ i ?\s) ": " elt "\n"))))
945 (while (setq table (char-table-parent table))
946 (insert "\nThe parent category table is:")
947 (describe-vector table 'help-describe-category-set))))))
950 ;;; Replacements for old lib-src/ programs. Don't seem especially useful.
952 ;; Replaces lib-src/digest-doc.c.
953 ;;;###autoload
954 (defun doc-file-to-man (file)
955 "Produce an nroff buffer containing the doc-strings from the DOC file."
956 (interactive (list (read-file-name "Name of DOC file: " doc-directory
957 internal-doc-file-name t)))
958 (or (file-readable-p file)
959 (error "Cannot read file `%s'" file))
960 (pop-to-buffer (generate-new-buffer "*man-doc*"))
961 (setq buffer-undo-list t)
962 (insert ".TH \"Command Summary for GNU Emacs\"\n"
963 ".AU Richard M. Stallman\n")
964 (insert-file-contents file)
965 (let (notfirst)
966 (while (search-forward "\x1f" nil 'move)
967 (if (looking-at "S")
968 (delete-region (1- (point)) (line-end-position))
969 (delete-char -1)
970 (if notfirst
971 (insert "\n.DE\n")
972 (setq notfirst t))
973 (insert "\n.SH ")
974 (insert (if (looking-at "F") "Function " "Variable "))
975 (delete-char 1)
976 (forward-line 1)
977 (insert ".DS L\n"))))
978 (insert "\n.DE\n")
979 (setq buffer-undo-list nil)
980 (nroff-mode))
982 ;; Replaces lib-src/sorted-doc.c.
983 ;;;###autoload
984 (defun doc-file-to-info (file)
985 "Produce a texinfo buffer with sorted doc-strings from the DOC file."
986 (interactive (list (read-file-name "Name of DOC file: " doc-directory
987 internal-doc-file-name t)))
988 (or (file-readable-p file)
989 (error "Cannot read file `%s'" file))
990 (let ((i 0) type name doc alist)
991 (with-temp-buffer
992 (insert-file-contents file)
993 ;; The characters "@{}" need special treatment.
994 (while (re-search-forward "[@{}]" nil t)
995 (backward-char)
996 (insert "@")
997 (forward-char 1))
998 (goto-char (point-min))
999 (while (search-forward "\x1f" nil t)
1000 (unless (looking-at "S")
1001 (setq type (char-after)
1002 name (buffer-substring (1+ (point)) (line-end-position))
1003 doc (buffer-substring (line-beginning-position 2)
1004 (if (search-forward "\x1f" nil 'move)
1005 (1- (point))
1006 (point)))
1007 alist (cons (list name type doc) alist))
1008 (backward-char 1))))
1009 (pop-to-buffer (generate-new-buffer "*info-doc*"))
1010 (setq buffer-undo-list t)
1011 ;; Write the output header.
1012 (insert "\\input texinfo @c -*-texinfo-*-\n"
1013 "@setfilename emacsdoc.info\n"
1014 "@settitle Command Summary for GNU Emacs\n"
1015 "@finalout\n"
1016 "\n@node Top\n"
1017 "@unnumbered Command Summary for GNU Emacs\n\n"
1018 "@table @asis\n\n"
1019 "@iftex\n"
1020 "@global@let@ITEM@item\n"
1021 "@def@item{@filbreak@vskip5pt@ITEM}\n"
1022 "@font@tensy cmsy10 scaled @magstephalf\n"
1023 "@font@teni cmmi10 scaled @magstephalf\n"
1024 "@def\\{{@tensy@char110}}\n" ; this backslash goes with cmr10
1025 "@def|{{@tensy@char106}}\n"
1026 "@def@{{{@tensy@char102}}\n"
1027 "@def@}{{@tensy@char103}}\n"
1028 "@def<{{@teni@char62}}\n"
1029 "@def>{{@teni@char60}}\n"
1030 "@chardef@@64\n"
1031 "@catcode43=12\n"
1032 "@tableindent-0.2in\n"
1033 "@end iftex\n")
1034 ;; Sort the array by name; within each name, by type (functions first).
1035 (setq alist (sort alist (lambda (e1 e2)
1036 (if (string-equal (car e1) (car e2))
1037 (<= (cadr e1) (cadr e2))
1038 (string-lessp (car e1) (car e2))))))
1039 ;; Print each function.
1040 (dolist (e alist)
1041 (insert "\n@item "
1042 (if (char-equal (cadr e) ?\F) "Function" "Variable")
1043 " @code{" (car e) "}\n@display\n"
1044 (nth 2 e)
1045 "\n@end display\n")
1046 ;; Try to avoid a save size overflow in the TeX output routine.
1047 (if (zerop (setq i (% (1+ i) 100)))
1048 (insert "\n@end table\n@table @asis\n")))
1049 (insert "@end table\n"
1050 "@bye\n")
1051 (setq buffer-undo-list nil)
1052 (texinfo-mode)))
1054 (provide 'help-fns)
1056 ;;; help-fns.el ends here