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