Use `called-interactively-p' instead of `interactive-p'.
[bpt/emacs.git] / lisp / help-fns.el
1 ;;; help-fns.el --- Complex help functions
2
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 ;; Free Software Foundation, Inc.
6
7 ;; Maintainer: FSF
8 ;; Keywords: help, internal
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 (require 'help-mode)
36
37 ;; Functions
38
39 ;;;###autoload
40 (defun describe-function (function)
41 "Display the full documentation of FUNCTION (a symbol)."
42 (interactive
43 (let ((fn (function-called-at-point))
44 (enable-recursive-minibuffers t)
45 val)
46 (setq val (completing-read (if fn
47 (format "Describe function (default %s): " fn)
48 "Describe function: ")
49 obarray 'fboundp t nil nil
50 (and fn (symbol-name fn))))
51 (list (if (equal val "")
52 fn (intern val)))))
53 (if (null function)
54 (message "You didn't specify a function")
55 (help-setup-xref (list #'describe-function function)
56 (called-interactively-p 'interactive))
57 (save-excursion
58 (with-help-window (help-buffer)
59 (prin1 function)
60 ;; Use " is " instead of a colon so that
61 ;; it is easier to get out the function name using forward-sexp.
62 (princ " is ")
63 (describe-function-1 function)
64 (with-current-buffer standard-output
65 ;; Return the text we displayed.
66 (buffer-string))))))
67
68 (defun help-split-fundoc (docstring def)
69 "Split a function DOCSTRING into the actual doc and the usage info.
70 Return (USAGE . DOC) or nil if there's no usage info.
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 (substring docstring 0 (match-beginning 0)))))
82
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 "Not documented"))
89 (if (or (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring) (eq arglist t))
90 docstring
91 (concat docstring
92 (if (string-match "\n?\n\\'" docstring)
93 (if (< (- (match-end 0) (match-beginning 0)) 2) "\n" "")
94 "\n\n")
95 (if (and (stringp arglist)
96 (string-match "\\`([^ ]+\\(.*\\))\\'" arglist))
97 (concat "(fn" (match-string 1 arglist) ")")
98 (format "%S" (help-make-usage 'fn arglist))))))
99
100 (defun help-function-arglist (def)
101 ;; Handle symbols aliased to other symbols.
102 (if (and (symbolp def) (fboundp def)) (setq def (indirect-function def)))
103 ;; If definition is a macro, find the function inside it.
104 (let ((advertised (gethash def advertised-signature-table t)))
105 (if (listp advertised) advertised
106 (if (eq (car-safe def) 'macro) (setq def (cdr def)))
107 (cond
108 ((byte-code-function-p def) (aref def 0))
109 ((eq (car-safe def) 'lambda) (nth 1 def))
110 ((and (eq (car-safe def) 'autoload) (not (eq (nth 4 def) 'keymap)))
111 "[Arg list not available until function definition is loaded.]")
112 (t t)))))
113
114 (defun help-make-usage (function arglist)
115 (cons (if (symbolp function) function 'anonymous)
116 (mapcar (lambda (arg)
117 (if (not (symbolp arg))
118 (if (and (consp arg) (symbolp (car arg)))
119 ;; CL style default values for optional args.
120 (cons (intern (upcase (symbol-name (car arg))))
121 (cdr arg))
122 arg)
123 (let ((name (symbol-name arg)))
124 (if (string-match "\\`&" name) arg
125 (intern (upcase name))))))
126 arglist)))
127
128 ;; Could be this, if we make symbol-file do the work below.
129 ;; (defun help-C-file-name (subr-or-var kind)
130 ;; "Return the name of the C file where SUBR-OR-VAR is defined.
131 ;; KIND should be `var' for a variable or `subr' for a subroutine."
132 ;; (symbol-file (if (symbolp subr-or-var) subr-or-var
133 ;; (subr-name subr-or-var))
134 ;; (if (eq kind 'var) 'defvar 'defun)))
135 ;;;###autoload
136 (defun help-C-file-name (subr-or-var kind)
137 "Return the name of the C file where SUBR-OR-VAR is defined.
138 KIND should be `var' for a variable or `subr' for a subroutine."
139 (let ((docbuf (get-buffer-create " *DOC*"))
140 (name (if (eq 'var kind)
141 (concat "V" (symbol-name subr-or-var))
142 (concat "F" (subr-name subr-or-var)))))
143 (with-current-buffer docbuf
144 (goto-char (point-min))
145 (if (eobp)
146 (insert-file-contents-literally
147 (expand-file-name internal-doc-file-name doc-directory)))
148 (let ((file (catch 'loop
149 (while t
150 (let ((pnt (search-forward (concat "\1f" name "\n"))))
151 (re-search-backward "\1fS\\(.*\\)")
152 (let ((file (match-string 1)))
153 (if (member file build-files)
154 (throw 'loop file)
155 (goto-char pnt))))))))
156 (if (string-match "^ns.*\\(\\.o\\|obj\\)\\'" file)
157 (setq file (replace-match ".m" t t file 1))
158 (if (string-match "\\.\\(o\\|obj\\)\\'" file)
159 (setq file (replace-match ".c" t t file))))
160 (if (string-match "\\.\\(c\\|m\\)\\'" file)
161 (concat "src/" file)
162 file)))))
163
164 (defcustom help-downcase-arguments nil
165 "If non-nil, argument names in *Help* buffers are downcased."
166 :type 'boolean
167 :group 'help
168 :version "23.2")
169
170 (defun help-highlight-arg (arg)
171 "Highlight ARG as an argument name for a *Help* buffer.
172 Return ARG in face `help-argument-name'; ARG is also downcased
173 if the variable `help-downcase-arguments' is non-nil."
174 (propertize (if help-downcase-arguments (downcase arg) arg)
175 'face 'help-argument-name))
176
177 (defun help-do-arg-highlight (doc args)
178 (with-syntax-table (make-syntax-table emacs-lisp-mode-syntax-table)
179 (modify-syntax-entry ?\- "w")
180 (dolist (arg args doc)
181 (setq doc (replace-regexp-in-string
182 ;; This is heuristic, but covers all common cases
183 ;; except ARG1-ARG2
184 (concat "\\<" ; beginning of word
185 "\\(?:[a-z-]*-\\)?" ; for xxx-ARG
186 "\\("
187 (regexp-quote arg)
188 "\\)"
189 "\\(?:es\\|s\\|th\\)?" ; for ARGth, ARGs
190 "\\(?:-[a-z0-9-]+\\)?" ; for ARG-xxx, ARG-n
191 "\\(?:-[{([<`\"].*?\\)?"; for ARG-{x}, (x), <x>, [x], `x'
192 "\\>") ; end of word
193 (help-highlight-arg arg)
194 doc t t 1)))))
195
196 (defun help-highlight-arguments (usage doc &rest args)
197 (when usage
198 (with-temp-buffer
199 (insert usage)
200 (goto-char (point-min))
201 (let ((case-fold-search nil)
202 (next (not (or args (looking-at "\\["))))
203 (opt nil))
204 ;; Make a list of all arguments
205 (skip-chars-forward "^ ")
206 (while next
207 (or opt (not (looking-at " &")) (setq opt t))
208 (if (not (re-search-forward " \\([\\[(]*\\)\\([^] &)\.]+\\)" nil t))
209 (setq next nil)
210 (setq args (cons (match-string 2) args))
211 (when (and opt (string= (match-string 1) "("))
212 ;; A pesky CL-style optional argument with default value,
213 ;; so let's skip over it
214 (search-backward "(")
215 (goto-char (scan-sexps (point) 1)))))
216 ;; Highlight aguments in the USAGE string
217 (setq usage (help-do-arg-highlight (buffer-string) args))
218 ;; Highlight arguments in the DOC string
219 (setq doc (and doc (help-do-arg-highlight doc args))))))
220 ;; Return value is like the one from help-split-fundoc, but highlighted
221 (cons usage doc))
222
223 ;; The following function was compiled from the former functions
224 ;; `describe-simplify-lib-file-name' and `find-source-lisp-file' with
225 ;; some excerpts from `describe-function-1' and `describe-variable'.
226 ;; The only additional twists provided are (1) locate the defining file
227 ;; for autoloaded functions, and (2) give preference to files in the
228 ;; "install directory" (directories found via `load-path') rather than
229 ;; to files in the "compile directory" (directories found by searching
230 ;; the loaddefs.el file). We autoload it because it's also used by
231 ;; `describe-face' (instead of `describe-simplify-lib-file-name').
232
233 ;;;###autoload
234 (defun find-lisp-object-file-name (object type)
235 "Guess the file that defined the Lisp object OBJECT, of type TYPE.
236 OBJECT should be a symbol associated with a function, variable, or face;
237 alternatively, it can be a function definition.
238 If TYPE is `variable', search for a variable definition.
239 If TYPE is `face', search for a face definition.
240 If TYPE is the value returned by `symbol-function' for a function symbol,
241 search for a function definition.
242
243 The return value is the absolute name of a readable file where OBJECT is
244 defined. If several such files exist, preference is given to a file
245 found via `load-path'. The return value can also be `C-source', which
246 means that OBJECT is a function or variable defined in C. If no
247 suitable file is found, return nil."
248 (let* ((autoloaded (eq (car-safe type) 'autoload))
249 (file-name (or (and autoloaded (nth 1 type))
250 (symbol-file
251 object (if (memq type (list 'defvar 'defface))
252 type
253 'defun)))))
254 (cond
255 (autoloaded
256 ;; An autoloaded function: Locate the file since `symbol-function'
257 ;; has only returned a bare string here.
258 (setq file-name
259 (locate-file file-name load-path '(".el" ".elc") 'readable)))
260 ((and (stringp file-name)
261 (string-match "[.]*loaddefs.el\\'" file-name))
262 ;; An autoloaded variable or face. Visit loaddefs.el in a buffer
263 ;; and try to extract the defining file. The following form is
264 ;; from `describe-function-1' and `describe-variable'.
265 (let ((location
266 (condition-case nil
267 (find-function-search-for-symbol object nil file-name)
268 (error nil))))
269 (when (cdr location)
270 (with-current-buffer (car location)
271 (goto-char (cdr location))
272 (when (re-search-backward
273 "^;;; Generated autoloads from \\(.*\\)" nil t)
274 (setq file-name
275 (locate-file
276 (file-name-sans-extension
277 (match-string-no-properties 1))
278 load-path '(".el" ".elc") 'readable))))))))
279
280 (cond
281 ((and (not file-name) (subrp type))
282 ;; A built-in function. The form is from `describe-function-1'.
283 (if (get-buffer " *DOC*")
284 (help-C-file-name type 'subr)
285 'C-source))
286 ((and (not file-name) (symbolp object)
287 (integerp (get object 'variable-documentation)))
288 ;; A variable defined in C. The form is from `describe-variable'.
289 (if (get-buffer " *DOC*")
290 (help-C-file-name object 'var)
291 'C-source))
292 ((not (stringp file-name))
293 ;; If we don't have a file-name string by now, we lost.
294 nil)
295 ((let ((lib-name
296 (if (string-match "[.]elc\\'" file-name)
297 (substring-no-properties file-name 0 -1)
298 file-name)))
299 ;; When the Elisp source file can be found in the install
300 ;; directory return the name of that file - `file-name' should
301 ;; have become an absolute file name ny now.
302 (or (and (file-readable-p lib-name) lib-name)
303 ;; The library might be compressed.
304 (and (file-readable-p (concat lib-name ".gz")) lib-name))))
305 ((let* ((lib-name (file-name-nondirectory file-name))
306 ;; The next form is from `describe-simplify-lib-file-name'.
307 (file-name
308 ;; Try converting the absolute file name to a library
309 ;; name, convert that back to a file name and see if we
310 ;; get the original one. If so, they are equivalent.
311 (if (equal file-name (locate-file lib-name load-path '("")))
312 (if (string-match "[.]elc\\'" lib-name)
313 (substring-no-properties lib-name 0 -1)
314 lib-name)
315 file-name))
316 ;; The next three forms are from `find-source-lisp-file'.
317 (elc-file (locate-file
318 (concat file-name
319 (if (string-match "\\.el\\'" file-name)
320 "c"
321 ".elc"))
322 load-path nil 'readable))
323 (str (when elc-file
324 (with-temp-buffer
325 (insert-file-contents-literally elc-file nil 0 256)
326 (buffer-string))))
327 (src-file (and str
328 (string-match ";;; from file \\(.*\\.el\\)" str)
329 (match-string 1 str))))
330 (and src-file (file-readable-p src-file) src-file))))))
331
332 (declare-function ad-get-advice-info "advice" (function))
333
334 ;;;###autoload
335 (defun describe-function-1 (function)
336 (let* ((advised (and (symbolp function) (featurep 'advice)
337 (ad-get-advice-info function)))
338 ;; If the function is advised, use the symbol that has the
339 ;; real definition, if that symbol is already set up.
340 (real-function
341 (or (and advised
342 (let ((origname (cdr (assq 'origname advised))))
343 (and (fboundp origname) origname)))
344 function))
345 ;; Get the real definition.
346 (def (if (symbolp real-function)
347 (symbol-function real-function)
348 function))
349 file-name string
350 (beg (if (commandp def) "an interactive " "a "))
351 (pt1 (with-current-buffer (help-buffer) (point)))
352 errtype)
353 (setq string
354 (cond ((or (stringp def)
355 (vectorp def))
356 "a keyboard macro")
357 ((subrp def)
358 (if (eq 'unevalled (cdr (subr-arity def)))
359 (concat beg "special form")
360 (concat beg "built-in function")))
361 ((byte-code-function-p def)
362 (concat beg "compiled Lisp function"))
363 ((symbolp def)
364 (while (and (fboundp def)
365 (symbolp (symbol-function def)))
366 (setq def (symbol-function def)))
367 ;; Handle (defalias 'foo 'bar), where bar is undefined.
368 (or (fboundp def) (setq errtype 'alias))
369 (format "an alias for `%s'" def))
370 ((eq (car-safe def) 'lambda)
371 (concat beg "Lisp function"))
372 ((eq (car-safe def) 'macro)
373 "a Lisp macro")
374 ((eq (car-safe def) 'autoload)
375 (format "%s autoloaded %s"
376 (if (commandp def) "an interactive" "an")
377 (if (eq (nth 4 def) 'keymap) "keymap"
378 (if (nth 4 def) "Lisp macro" "Lisp function"))))
379 ((keymapp def)
380 (let ((is-full nil)
381 (elts (cdr-safe def)))
382 (while elts
383 (if (char-table-p (car-safe elts))
384 (setq is-full t
385 elts nil))
386 (setq elts (cdr-safe elts)))
387 (if is-full
388 "a full keymap"
389 "a sparse keymap")))
390 (t "")))
391 (princ string)
392 (if (eq errtype 'alias)
393 (princ ",\nwhich is not defined. Please make a bug report.")
394 (with-current-buffer standard-output
395 (save-excursion
396 (save-match-data
397 (when (re-search-backward "alias for `\\([^`']+\\)'" nil t)
398 (help-xref-button 1 'help-function def)))))
399
400 (setq file-name (find-lisp-object-file-name function def))
401 (when file-name
402 (princ " in `")
403 ;; We used to add .el to the file name,
404 ;; but that's completely wrong when the user used load-file.
405 (princ (if (eq file-name 'C-source)
406 "C source code"
407 (file-name-nondirectory file-name)))
408 (princ "'")
409 ;; Make a hyperlink to the library.
410 (with-current-buffer standard-output
411 (save-excursion
412 (re-search-backward "`\\([^`']+\\)'" nil t)
413 (help-xref-button 1 'help-function-def function file-name))))
414 (princ ".")
415 (with-current-buffer (help-buffer)
416 (fill-region-as-paragraph (save-excursion (goto-char pt1) (forward-line 0) (point))
417 (point)))
418 (terpri)(terpri)
419 (when (commandp function)
420 (let ((pt2 (with-current-buffer (help-buffer) (point)))
421 (remapped (command-remapping function)))
422 (unless (memq remapped '(ignore undefined))
423 (let ((keys (where-is-internal
424 (or remapped function) overriding-local-map nil nil))
425 non-modified-keys)
426 (if (and (eq function 'self-insert-command)
427 (vectorp (car-safe keys))
428 (consp (aref (car keys) 0)))
429 (princ "It is bound to many ordinary text characters.\n")
430 ;; Which non-control non-meta keys run this command?
431 (dolist (key keys)
432 (if (member (event-modifiers (aref key 0)) '(nil (shift)))
433 (push key non-modified-keys)))
434 (when remapped
435 (princ "It is remapped to `")
436 (princ (symbol-name remapped))
437 (princ "'"))
438
439 (when keys
440 (princ (if remapped ", which is bound to " "It is bound to "))
441 ;; If lots of ordinary text characters run this command,
442 ;; don't mention them one by one.
443 (if (< (length non-modified-keys) 10)
444 (princ (mapconcat 'key-description keys ", "))
445 (dolist (key non-modified-keys)
446 (setq keys (delq key keys)))
447 (if keys
448 (progn
449 (princ (mapconcat 'key-description keys ", "))
450 (princ ", and many ordinary text characters"))
451 (princ "many ordinary text characters"))))
452 (when (or remapped keys non-modified-keys)
453 (princ ".")
454 (terpri)))))
455
456 (with-current-buffer (help-buffer)
457 (fill-region-as-paragraph pt2 (point))
458 (unless (looking-back "\n\n")
459 (terpri)))))
460 ;; Note that list* etc do not get this property until
461 ;; cl-hack-byte-compiler runs, after bytecomp is loaded.
462 (when (eq (get function 'byte-compile) 'cl-byte-compile-compiler-macro)
463 (princ "This function has a compiler macro")
464 (let ((lib (get function 'compiler-macro-file)))
465 (when (stringp lib)
466 (princ (format " in `%s'" lib))
467 (with-current-buffer standard-output
468 (save-excursion
469 (re-search-backward "`\\([^`']+\\)'" nil t)
470 (help-xref-button 1 'help-function-cmacro function lib)))))
471 (princ ".\n\n"))
472 (let* ((arglist (help-function-arglist def))
473 (doc (documentation function))
474 (usage (help-split-fundoc doc function)))
475 (with-current-buffer standard-output
476 ;; If definition is a keymap, skip arglist note.
477 (unless (keymapp function)
478 (let* ((use (cond
479 (usage (setq doc (cdr usage)) (car usage))
480 ((listp arglist)
481 (format "%S" (help-make-usage function arglist)))
482 ((stringp arglist) arglist)
483 ;; Maybe the arglist is in the docstring of a symbol
484 ;; this one is aliased to.
485 ((let ((fun real-function))
486 (while (and (symbolp fun)
487 (setq fun (symbol-function fun))
488 (not (setq usage (help-split-fundoc
489 (documentation fun)
490 function)))))
491 usage)
492 (car usage))
493 ((or (stringp def)
494 (vectorp def))
495 (format "\nMacro: %s" (format-kbd-macro def)))
496 (t "[Missing arglist. Please make a bug report.]")))
497 (high (help-highlight-arguments use doc)))
498 (let ((fill-begin (point)))
499 (insert (car high) "\n")
500 (fill-region fill-begin (point)))
501 (setq doc (cdr high))))
502 (let* ((obsolete (and
503 ;; function might be a lambda construct.
504 (symbolp function)
505 (get function 'byte-obsolete-info)))
506 (use (car obsolete)))
507 (when obsolete
508 (princ "\nThis function is obsolete")
509 (when (nth 2 obsolete)
510 (insert (format " since %s" (nth 2 obsolete))))
511 (insert (cond ((stringp use) (concat ";\n" use))
512 (use (format ";\nuse `%s' instead." use))
513 (t "."))
514 "\n"))
515 (insert "\n"
516 (or doc "Not documented."))))))))
517
518 \f
519 ;; Variables
520
521 ;;;###autoload
522 (defun variable-at-point (&optional any-symbol)
523 "Return the bound variable symbol found at or before point.
524 Return 0 if there is no such symbol.
525 If ANY-SYMBOL is non-nil, don't insist the symbol be bound."
526 (with-syntax-table emacs-lisp-mode-syntax-table
527 (or (condition-case ()
528 (save-excursion
529 (or (not (zerop (skip-syntax-backward "_w")))
530 (eq (char-syntax (following-char)) ?w)
531 (eq (char-syntax (following-char)) ?_)
532 (forward-sexp -1))
533 (skip-chars-forward "'")
534 (let ((obj (read (current-buffer))))
535 (and (symbolp obj) (boundp obj) obj)))
536 (error nil))
537 (let* ((str (find-tag-default))
538 (sym (if str (intern-soft str))))
539 (if (and sym (or any-symbol (boundp sym)))
540 sym
541 (save-match-data
542 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
543 (setq sym (intern-soft (match-string 1 str)))
544 (and (or any-symbol (boundp sym)) sym)))))
545 0)))
546
547 (defun describe-variable-custom-version-info (variable)
548 (let ((custom-version (get variable 'custom-version))
549 (cpv (get variable 'custom-package-version))
550 (output nil))
551 (if custom-version
552 (setq output
553 (format "This variable was introduced, or its default value was changed, in\nversion %s of Emacs.\n"
554 custom-version))
555 (when cpv
556 (let* ((package (car-safe cpv))
557 (version (if (listp (cdr-safe cpv))
558 (car (cdr-safe cpv))
559 (cdr-safe cpv)))
560 (pkg-versions (assq package customize-package-emacs-version-alist))
561 (emacsv (cdr (assoc version pkg-versions))))
562 (if (and package version)
563 (setq output
564 (format (concat "This variable was introduced, or its default value was changed, in\nversion %s of the %s package"
565 (if emacsv
566 (format " that is part of Emacs %s" emacsv))
567 ".\n")
568 version package))))))
569 output))
570
571 ;;;###autoload
572 (defun describe-variable (variable &optional buffer frame)
573 "Display the full documentation of VARIABLE (a symbol).
574 Returns the documentation as a string, also.
575 If VARIABLE has a buffer-local value in BUFFER or FRAME
576 \(default to the current buffer and current frame),
577 it is displayed along with the global value."
578 (interactive
579 (let ((v (variable-at-point))
580 (enable-recursive-minibuffers t)
581 val)
582 (setq val (completing-read (if (symbolp v)
583 (format
584 "Describe variable (default %s): " v)
585 "Describe variable: ")
586 obarray
587 '(lambda (vv)
588 (or (boundp vv)
589 (get vv 'variable-documentation)))
590 t nil nil
591 (if (symbolp v) (symbol-name v))))
592 (list (if (equal val "")
593 v (intern val)))))
594 (let (file-name)
595 (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
596 (unless (frame-live-p frame) (setq frame (selected-frame)))
597 (if (not (symbolp variable))
598 (message "You did not specify a variable")
599 (save-excursion
600 (let ((valvoid (not (with-current-buffer buffer (boundp variable))))
601 val val-start-pos locus)
602 ;; Extract the value before setting up the output buffer,
603 ;; in case `buffer' *is* the output buffer.
604 (unless valvoid
605 (with-selected-frame frame
606 (with-current-buffer buffer
607 (setq val (symbol-value variable)
608 locus (variable-binding-locus variable)))))
609 (help-setup-xref (list #'describe-variable variable buffer)
610 (called-interactively-p 'interactive))
611 (with-help-window (help-buffer)
612 (with-current-buffer buffer
613 (prin1 variable)
614 (setq file-name (find-lisp-object-file-name variable 'defvar))
615
616 (if file-name
617 (progn
618 (princ " is a variable defined in `")
619 (princ (if (eq file-name 'C-source)
620 "C source code"
621 (file-name-nondirectory file-name)))
622 (princ "'.\n")
623 (with-current-buffer standard-output
624 (save-excursion
625 (re-search-backward "`\\([^`']+\\)'" nil t)
626 (help-xref-button 1 'help-variable-def
627 variable file-name)))
628 (if valvoid
629 (princ "It is void as a variable.")
630 (princ "Its ")))
631 (if valvoid
632 (princ " is void as a variable.")
633 (princ "'s "))))
634 (if valvoid
635 nil
636 (with-current-buffer standard-output
637 (setq val-start-pos (point))
638 (princ "value is ")
639 (terpri)
640 (let ((from (point)))
641 (pp val)
642 ;; Hyperlinks in variable's value are quite frequently
643 ;; inappropriate e.g C-h v <RET> features <RET>
644 ;; (help-xref-on-pp from (point))
645 (if (< (point) (+ from 20))
646 (delete-region (1- from) from)))))
647 (terpri)
648
649 (when locus
650 (if (bufferp locus)
651 (princ (format "%socal in buffer %s; "
652 (if (get variable 'permanent-local)
653 "Permanently l" "L")
654 (buffer-name)))
655 (princ (format "It is a frame-local variable; ")))
656 (if (not (default-boundp variable))
657 (princ "globally void")
658 (let ((val (default-value variable)))
659 (with-current-buffer standard-output
660 (princ "global value is ")
661 (terpri)
662 ;; Fixme: pp can take an age if you happen to
663 ;; ask for a very large expression. We should
664 ;; probably print it raw once and check it's a
665 ;; sensible size before prettyprinting. -- fx
666 (let ((from (point)))
667 (pp val)
668 ;; See previous comment for this function.
669 ;; (help-xref-on-pp from (point))
670 (if (< (point) (+ from 20))
671 (delete-region (1- from) from))))))
672 (terpri))
673
674 ;; If the value is large, move it to the end.
675 (with-current-buffer standard-output
676 (when (> (count-lines (point-min) (point-max)) 10)
677 ;; Note that setting the syntax table like below
678 ;; makes forward-sexp move over a `'s' at the end
679 ;; of a symbol.
680 (set-syntax-table emacs-lisp-mode-syntax-table)
681 (goto-char val-start-pos)
682 ;; The line below previously read as
683 ;; (delete-region (point) (progn (end-of-line) (point)))
684 ;; which suppressed display of the buffer local value for
685 ;; large values.
686 (when (looking-at "value is") (replace-match ""))
687 (save-excursion
688 (insert "\n\nValue:")
689 (set (make-local-variable 'help-button-cache)
690 (point-marker)))
691 (insert "value is shown ")
692 (insert-button "below"
693 'action help-button-cache
694 'follow-link t
695 'help-echo "mouse-2, RET: show value")
696 (insert ".\n")))
697 (terpri)
698
699 (let* ((alias (condition-case nil
700 (indirect-variable variable)
701 (error variable)))
702 (obsolete (get variable 'byte-obsolete-variable))
703 (use (car obsolete))
704 (safe-var (get variable 'safe-local-variable))
705 (doc (or (documentation-property variable 'variable-documentation)
706 (documentation-property alias 'variable-documentation)))
707 (extra-line nil))
708 ;; Add a note for variables that have been make-var-buffer-local.
709 (when (and (local-variable-if-set-p variable)
710 (or (not (local-variable-p variable))
711 (with-temp-buffer
712 (local-variable-if-set-p variable))))
713 (setq extra-line t)
714 (princ " Automatically becomes buffer-local when set in any fashion.\n"))
715
716 ;; Mention if it's an alias
717 (unless (eq alias variable)
718 (setq extra-line t)
719 (princ (format " This variable is an alias for `%s'.\n" alias)))
720
721 (when obsolete
722 (setq extra-line t)
723 (princ " This variable is obsolete")
724 (if (cdr obsolete) (princ (format " since %s" (cdr obsolete))))
725 (princ (cond ((stringp use) (concat ";\n " use))
726 (use (format ";\n use `%s' instead." (car obsolete)))
727 (t ".")))
728 (terpri))
729
730 (when (member (cons variable val) file-local-variables-alist)
731 (setq extra-line t)
732 (if (member (cons variable val) dir-local-variables-alist)
733 (let ((file (and (buffer-file-name)
734 (not (file-remote-p (buffer-file-name)))
735 (dir-locals-find-file (buffer-file-name)))))
736 (princ " This variable is a directory local variable")
737 (when file
738 (princ (concat "\n from the file \""
739 (if (consp file)
740 (car file)
741 file)
742 "\"")))
743 (princ ".\n"))
744 (princ " This variable is a file local variable.\n")))
745
746 (when (memq variable ignored-local-variables)
747 (setq extra-line t)
748 (princ " This variable is ignored when used as a file local \
749 variable.\n"))
750
751 ;; Can be both risky and safe, eg auto-fill-function.
752 (when (risky-local-variable-p variable)
753 (setq extra-line t)
754 (princ " This variable is potentially risky when used as a \
755 file local variable.\n")
756 (when (assq variable safe-local-variable-values)
757 (princ " However, you have added it to \
758 `safe-local-variable-values'.\n")))
759
760 (when safe-var
761 (setq extra-line t)
762 (princ " This variable is safe as a file local variable ")
763 (princ "if its value\n satisfies the predicate ")
764 (princ (if (byte-code-function-p safe-var)
765 "which is byte-compiled expression.\n"
766 (format "`%s'.\n" safe-var))))
767
768 (if extra-line (terpri))
769 (princ "Documentation:\n")
770 (with-current-buffer standard-output
771 (insert (or doc "Not documented as a variable."))))
772
773 ;; Make a link to customize if this variable can be customized.
774 (when (custom-variable-p variable)
775 (let ((customize-label "customize"))
776 (terpri)
777 (terpri)
778 (princ (concat "You can " customize-label " this variable."))
779 (with-current-buffer standard-output
780 (save-excursion
781 (re-search-backward
782 (concat "\\(" customize-label "\\)") nil t)
783 (help-xref-button 1 'help-customize-variable variable))))
784 ;; Note variable's version or package version
785 (let ((output (describe-variable-custom-version-info variable)))
786 (when output
787 (terpri)
788 (terpri)
789 (princ output))))
790
791 (save-excursion
792 (set-buffer standard-output)
793 ;; Return the text we displayed.
794 (buffer-string))))))))
795
796
797 ;;;###autoload
798 (defun describe-syntax (&optional buffer)
799 "Describe the syntax specifications in the syntax table of BUFFER.
800 The descriptions are inserted in a help buffer, which is then displayed.
801 BUFFER defaults to the current buffer."
802 (interactive)
803 (setq buffer (or buffer (current-buffer)))
804 (help-setup-xref (list #'describe-syntax buffer)
805 (called-interactively-p 'interactive))
806 (with-help-window (help-buffer)
807 (let ((table (with-current-buffer buffer (syntax-table))))
808 (with-current-buffer standard-output
809 (describe-vector table 'internal-describe-syntax-value)
810 (while (setq table (char-table-parent table))
811 (insert "\nThe parent syntax table is:")
812 (describe-vector table 'internal-describe-syntax-value))))))
813
814 (defun help-describe-category-set (value)
815 (insert (cond
816 ((null value) "default")
817 ((char-table-p value) "deeper char-table ...")
818 (t (condition-case err
819 (category-set-mnemonics value)
820 (error "invalid"))))))
821
822 ;;;###autoload
823 (defun describe-categories (&optional buffer)
824 "Describe the category specifications in the current category table.
825 The descriptions are inserted in a buffer, which is then displayed.
826 If BUFFER is non-nil, then describe BUFFER's category table instead.
827 BUFFER should be a buffer or a buffer name."
828 (interactive)
829 (setq buffer (or buffer (current-buffer)))
830 (help-setup-xref (list #'describe-categories buffer)
831 (called-interactively-p 'interactive))
832 (with-help-window (help-buffer)
833 (let* ((table (with-current-buffer buffer (category-table)))
834 (docs (char-table-extra-slot table 0)))
835 (if (or (not (vectorp docs)) (/= (length docs) 95))
836 (error "Invalid first extra slot in this category table\n"))
837 (with-current-buffer standard-output
838 (insert "Legend of category mnemonics (see the tail for the longer description)\n")
839 (let ((pos (point)) (items 0) lines n)
840 (dotimes (i 95)
841 (if (aref docs i) (setq items (1+ items))))
842 (setq lines (1+ (/ (1- items) 4)))
843 (setq n 0)
844 (dotimes (i 95)
845 (let ((elt (aref docs i)))
846 (when elt
847 (string-match ".*" elt)
848 (setq elt (match-string 0 elt))
849 (if (>= (length elt) 17)
850 (setq elt (concat (substring elt 0 14) "...")))
851 (if (< (point) (point-max))
852 (move-to-column (* 20 (/ n lines)) t))
853 (insert (+ i ?\s) ?: elt)
854 (if (< (point) (point-max))
855 (forward-line 1)
856 (insert "\n"))
857 (setq n (1+ n))
858 (if (= (% n lines) 0)
859 (goto-char pos))))))
860 (goto-char (point-max))
861 (insert "\n"
862 "character(s)\tcategory mnemonics\n"
863 "------------\t------------------")
864 (describe-vector table 'help-describe-category-set)
865 (insert "Legend of category mnemonics:\n")
866 (dotimes (i 95)
867 (let ((elt (aref docs i)))
868 (when elt
869 (if (string-match "\n" elt)
870 (setq elt (substring elt (match-end 0))))
871 (insert (+ i ?\s) ": " elt "\n"))))
872 (while (setq table (char-table-parent table))
873 (insert "\nThe parent category table is:")
874 (describe-vector table 'help-describe-category-set))))))
875
876 (provide 'help-fns)
877
878 ;; arch-tag: 9e10331c-ae81-4d13-965d-c4819aaab0b3
879 ;;; help-fns.el ends here