Fix EOL mishap.
[emacs.git] / lisp / help-fns.el
blobbb97ef421735c1c47b47295d1453f8e09c363928
1 ;;; help-fns.el --- Complex help functions
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001,
4 ;; 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: help, internal
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This file contains those help commands which are complicated, and
27 ;; which may not be used in every session. For example
28 ;; `describe-function' will probably be heavily used when doing elisp
29 ;; programming, but not if just editing C files. Simpler help commands
30 ;; are in help.el
32 ;;; Code:
34 (require 'help-mode)
36 ;; Functions
38 ;;;###autoload
39 (defun describe-function (function)
40 "Display the full documentation of FUNCTION (a symbol)."
41 (interactive
42 (let ((fn (function-called-at-point))
43 (enable-recursive-minibuffers t)
44 val)
45 (setq val (completing-read (if fn
46 (format "Describe function (default %s): " fn)
47 "Describe function: ")
48 obarray 'fboundp t nil nil
49 (and fn (symbol-name fn))))
50 (list (if (equal val "")
51 fn (intern val)))))
52 (if (null function)
53 (message "You didn't specify a function")
54 (help-setup-xref (list #'describe-function function) (interactive-p))
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 (substring docstring 0 (match-beginning 0)))))
81 (defun help-add-fundoc-usage (docstring arglist)
82 "Add the usage info to DOCSTRING.
83 If DOCSTRING already has a usage info, then just return it unchanged.
84 The usage info is built from ARGLIST. DOCSTRING can be nil.
85 ARGLIST can also be t or a string of the form \"(FUN ARG1 ARG2 ...)\"."
86 (unless (stringp docstring) (setq docstring "Not documented"))
87 (if (or (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring) (eq arglist t))
88 docstring
89 (concat docstring
90 (if (string-match "\n?\n\\'" docstring)
91 (if (< (- (match-end 0) (match-beginning 0)) 2) "\n" "")
92 "\n\n")
93 (if (and (stringp arglist)
94 (string-match "\\`([^ ]+\\(.*\\))\\'" arglist))
95 (concat "(fn" (match-string 1 arglist) ")")
96 (format "%S" (help-make-usage 'fn arglist))))))
98 (defun help-function-arglist (def)
99 ;; Handle symbols aliased to other symbols.
100 (if (and (symbolp def) (fboundp def)) (setq def (indirect-function def)))
101 ;; If definition is a macro, find the function inside it.
102 (if (eq (car-safe def) 'macro) (setq def (cdr def)))
103 (cond
104 ((byte-code-function-p def) (aref def 0))
105 ((eq (car-safe def) 'lambda) (nth 1 def))
106 ((and (eq (car-safe def) 'autoload) (not (eq (nth 4 def) 'keymap)))
107 "[Arg list not available until function definition is loaded.]")
108 (t t)))
110 (defun help-make-usage (function arglist)
111 (cons (if (symbolp function) function 'anonymous)
112 (mapcar (lambda (arg)
113 (if (not (symbolp arg))
114 (if (and (consp arg) (symbolp (car arg)))
115 ;; CL style default values for optional args.
116 (cons (intern (upcase (symbol-name (car arg))))
117 (cdr arg))
118 arg)
119 (let ((name (symbol-name arg)))
120 (if (string-match "\\`&" name) arg
121 (intern (upcase name))))))
122 arglist)))
124 ;; Could be this, if we make symbol-file do the work below.
125 ;; (defun help-C-file-name (subr-or-var kind)
126 ;; "Return the name of the C file where SUBR-OR-VAR is defined.
127 ;; KIND should be `var' for a variable or `subr' for a subroutine."
128 ;; (symbol-file (if (symbolp subr-or-var) subr-or-var
129 ;; (subr-name subr-or-var))
130 ;; (if (eq kind 'var) 'defvar 'defun)))
131 ;;;###autoload
132 (defun help-C-file-name (subr-or-var kind)
133 "Return the name of the C file where SUBR-OR-VAR is defined.
134 KIND should be `var' for a variable or `subr' for a subroutine."
135 (let ((docbuf (get-buffer-create " *DOC*"))
136 (name (if (eq 'var kind)
137 (concat "V" (symbol-name subr-or-var))
138 (concat "F" (subr-name subr-or-var)))))
139 (with-current-buffer docbuf
140 (goto-char (point-min))
141 (if (eobp)
142 (insert-file-contents-literally
143 (expand-file-name internal-doc-file-name doc-directory)))
144 (let ((file (catch 'loop
145 (while t
146 (let ((pnt (search-forward (concat "\x1f" name "\n"))))
147 (re-search-backward "\x1fS\\(.*\\)")
148 (let ((file (match-string 1)))
149 (if (member file build-files)
150 (throw 'loop file)
151 (goto-char pnt))))))))
152 (if (string-match "^ns.*\\(\\.o\\|obj\\)\\'" file)
153 (setq file (replace-match ".m" t t file 1))
154 (if (string-match "\\.\\(o\\|obj\\)\\'" file)
155 (setq file (replace-match ".c" t t file))))
156 (if (string-match "\\.\\(c\\|m\\)\\'" file)
157 (concat "src/" file)
158 file)))))
160 (defface help-argument-name '((((supports :slant italic)) :inherit italic))
161 "Face to highlight argument names in *Help* buffers."
162 :group 'help)
164 (defun help-default-arg-highlight (arg)
165 "Default function to highlight arguments in *Help* buffers.
166 It returns ARG in face `help-argument-name'; ARG is also
167 downcased if it displays differently than the default
168 face (according to `face-differs-from-default-p')."
169 (propertize (if (face-differs-from-default-p 'help-argument-name)
170 (downcase arg)
171 arg)
172 'face 'help-argument-name))
174 (defun help-do-arg-highlight (doc args)
175 (with-syntax-table (make-syntax-table emacs-lisp-mode-syntax-table)
176 (modify-syntax-entry ?\- "w")
177 (dolist (arg args doc)
178 (setq doc (replace-regexp-in-string
179 ;; This is heuristic, but covers all common cases
180 ;; except ARG1-ARG2
181 (concat "\\<" ; beginning of word
182 "\\(?:[a-z-]*-\\)?" ; for xxx-ARG
183 "\\("
184 (regexp-quote arg)
185 "\\)"
186 "\\(?:es\\|s\\|th\\)?" ; for ARGth, ARGs
187 "\\(?:-[a-z0-9-]+\\)?" ; for ARG-xxx, ARG-n
188 "\\(?:-[{([<`\"].*?\\)?"; for ARG-{x}, (x), <x>, [x], `x'
189 "\\>") ; end of word
190 (help-default-arg-highlight arg)
191 doc t t 1)))))
193 (defun help-highlight-arguments (usage doc &rest args)
194 (when usage
195 (with-temp-buffer
196 (insert usage)
197 (goto-char (point-min))
198 (let ((case-fold-search nil)
199 (next (not (or args (looking-at "\\["))))
200 (opt nil))
201 ;; Make a list of all arguments
202 (skip-chars-forward "^ ")
203 (while next
204 (or opt (not (looking-at " &")) (setq opt t))
205 (if (not (re-search-forward " \\([\\[(]*\\)\\([^] &)\.]+\\)" nil t))
206 (setq next nil)
207 (setq args (cons (match-string 2) args))
208 (when (and opt (string= (match-string 1) "("))
209 ;; A pesky CL-style optional argument with default value,
210 ;; so let's skip over it
211 (search-backward "(")
212 (goto-char (scan-sexps (point) 1)))))
213 ;; Highlight aguments in the USAGE string
214 (setq usage (help-do-arg-highlight (buffer-string) args))
215 ;; Highlight arguments in the DOC string
216 (setq doc (and doc (help-do-arg-highlight doc args))))))
217 ;; Return value is like the one from help-split-fundoc, but highlighted
218 (cons usage doc))
220 ;;;###autoload
221 (defun describe-simplify-lib-file-name (file)
222 "Simplify a library name FILE to a relative name, and make it a source file."
223 (if file
224 ;; Try converting the absolute file name to a library name.
225 (let ((libname (file-name-nondirectory file)))
226 ;; Now convert that back to a file name and see if we get
227 ;; the original one. If so, they are equivalent.
228 (if (equal file (locate-file libname load-path '("")))
229 (if (string-match "[.]elc\\'" libname)
230 (substring libname 0 -1)
231 libname)
232 file))))
234 (defun find-source-lisp-file (file-name)
235 (let* ((elc-file (locate-file (concat file-name
236 (if (string-match "\\.el" file-name)
238 ".elc"))
239 load-path))
240 (str (if (and elc-file (file-readable-p elc-file))
241 (with-temp-buffer
242 (insert-file-contents-literally elc-file nil 0 256)
243 (buffer-string))))
244 (src-file (and str
245 (string-match ";;; from file \\(.*\\.el\\)" str)
246 (match-string 1 str))))
247 (if (and src-file (file-readable-p src-file))
248 src-file
249 file-name)))
251 (declare-function ad-get-advice-info "advice" (function))
253 ;;;###autoload
254 (defun describe-function-1 (function)
255 (let* ((advised (and (symbolp function) (featurep 'advice)
256 (ad-get-advice-info function)))
257 ;; If the function is advised, use the symbol that has the
258 ;; real definition, if that symbol is already set up.
259 (real-function
260 (or (and advised
261 (cdr (assq 'origname advised))
262 (fboundp (cdr (assq 'origname advised)))
263 (cdr (assq 'origname advised)))
264 function))
265 ;; Get the real definition.
266 (def (if (symbolp real-function)
267 (symbol-function real-function)
268 function))
269 file-name string
270 (beg (if (commandp def) "an interactive " "a "))
271 (pt1 (with-current-buffer (help-buffer) (point)))
272 errtype)
273 (setq string
274 (cond ((or (stringp def)
275 (vectorp def))
276 "a keyboard macro")
277 ((subrp def)
278 (if (eq 'unevalled (cdr (subr-arity def)))
279 (concat beg "special form")
280 (concat beg "built-in function")))
281 ((byte-code-function-p def)
282 (concat beg "compiled Lisp function"))
283 ((symbolp def)
284 (while (and (fboundp def)
285 (symbolp (symbol-function def)))
286 (setq def (symbol-function def)))
287 ;; Handle (defalias 'foo 'bar), where bar is undefined.
288 (or (fboundp def) (setq errtype 'alias))
289 (format "an alias for `%s'" def))
290 ((eq (car-safe def) 'lambda)
291 (concat beg "Lisp function"))
292 ((eq (car-safe def) 'macro)
293 "a Lisp macro")
294 ((eq (car-safe def) 'autoload)
295 (setq file-name (nth 1 def))
296 (format "%s autoloaded %s"
297 (if (commandp def) "an interactive" "an")
298 (if (eq (nth 4 def) 'keymap) "keymap"
299 (if (nth 4 def) "Lisp macro" "Lisp function"))
301 ((keymapp def)
302 (let ((is-full nil)
303 (elts (cdr-safe def)))
304 (while elts
305 (if (char-table-p (car-safe elts))
306 (setq is-full t
307 elts nil))
308 (setq elts (cdr-safe elts)))
309 (if is-full
310 "a full keymap"
311 "a sparse keymap")))
312 (t "")))
313 (princ string)
314 (if (eq errtype 'alias)
315 (princ ",\nwhich is not defined. Please make a bug report.")
316 (with-current-buffer standard-output
317 (save-excursion
318 (save-match-data
319 (if (re-search-backward "alias for `\\([^`']+\\)'" nil t)
320 (help-xref-button 1 'help-function def)))))
321 (or file-name
322 (setq file-name (symbol-file function 'defun)))
323 (setq file-name (describe-simplify-lib-file-name file-name))
324 (when (equal file-name "loaddefs.el")
325 ;; Find the real def site of the preloaded function.
326 ;; This is necessary only for defaliases.
327 (let ((location
328 (condition-case nil
329 (find-function-search-for-symbol function nil "loaddefs.el")
330 (error nil))))
331 (when location
332 (with-current-buffer (car location)
333 (goto-char (cdr location))
334 (when (re-search-backward
335 "^;;; Generated autoloads from \\(.*\\)" nil t)
336 (setq file-name (match-string 1)))))))
337 (when (and (null file-name) (subrp def))
338 ;; Find the C source file name.
339 (setq file-name (if (get-buffer " *DOC*")
340 (help-C-file-name def 'subr)
341 'C-source)))
342 (when file-name
343 (princ " in `")
344 ;; We used to add .el to the file name,
345 ;; but that's completely wrong when the user used load-file.
346 (princ (if (eq file-name 'C-source) "C source code" file-name))
347 (princ "'")
348 ;; See if lisp files are present where they where installed from.
349 (if (not (eq file-name 'C-source))
350 (setq file-name (find-source-lisp-file file-name)))
352 ;; Make a hyperlink to the library.
353 (with-current-buffer standard-output
354 (save-excursion
355 (re-search-backward "`\\([^`']+\\)'" nil t)
356 (help-xref-button 1 'help-function-def real-function file-name))))
357 (princ ".")
358 (with-current-buffer (help-buffer)
359 (fill-region-as-paragraph (save-excursion (goto-char pt1) (forward-line 0) (point))
360 (point)))
361 (terpri)(terpri)
362 (when (commandp function)
363 (let ((pt2 (with-current-buffer (help-buffer) (point))))
364 (if (and (eq function 'self-insert-command)
365 (eq (key-binding "a") 'self-insert-command)
366 (eq (key-binding "b") 'self-insert-command)
367 (eq (key-binding "c") 'self-insert-command))
368 (princ "It is bound to many ordinary text characters.\n")
369 (let* ((remapped (command-remapping function))
370 (keys (where-is-internal
371 (or remapped function) overriding-local-map nil nil))
372 non-modified-keys)
373 ;; Which non-control non-meta keys run this command?
374 (dolist (key keys)
375 (if (member (event-modifiers (aref key 0)) '(nil (shift)))
376 (push key non-modified-keys)))
377 (when remapped
378 (princ "It is remapped to `")
379 (princ (symbol-name remapped))
380 (princ "'"))
382 (when keys
383 (princ (if remapped ", which is bound to " "It is bound to "))
384 ;; If lots of ordinary text characters run this command,
385 ;; don't mention them one by one.
386 (if (< (length non-modified-keys) 10)
387 (princ (mapconcat 'key-description keys ", "))
388 (dolist (key non-modified-keys)
389 (setq keys (delq key keys)))
390 (if keys
391 (progn
392 (princ (mapconcat 'key-description keys ", "))
393 (princ ", and many ordinary text characters"))
394 (princ "many ordinary text characters"))))
395 (when (or remapped keys non-modified-keys)
396 (princ ".")
397 (terpri))))
398 (with-current-buffer (help-buffer) (fill-region-as-paragraph pt2 (point)))
399 (terpri)))
400 (let* ((arglist (help-function-arglist def))
401 (doc (documentation function))
402 (usage (help-split-fundoc doc function)))
403 (with-current-buffer standard-output
404 ;; If definition is a keymap, skip arglist note.
405 (unless (keymapp function)
406 (let* ((use (cond
407 (usage (setq doc (cdr usage)) (car usage))
408 ((listp arglist)
409 (format "%S" (help-make-usage function arglist)))
410 ((stringp arglist) arglist)
411 ;; Maybe the arglist is in the docstring of a symbol
412 ;; this one is aliased to.
413 ((let ((fun real-function))
414 (while (and (symbolp fun)
415 (setq fun (symbol-function fun))
416 (not (setq usage (help-split-fundoc
417 (documentation fun)
418 function)))))
419 usage)
420 (car usage))
421 ((or (stringp def)
422 (vectorp def))
423 (format "\nMacro: %s" (format-kbd-macro def)))
424 (t "[Missing arglist. Please make a bug report.]")))
425 (high (help-highlight-arguments use doc)))
426 (let ((fill-begin (point)))
427 (insert (car high) "\n")
428 (fill-region fill-begin (point)))
429 (setq doc (cdr high))))
430 (let* ((obsolete (and
431 ;; function might be a lambda construct.
432 (symbolp function)
433 (get function 'byte-obsolete-info)))
434 (use (car obsolete)))
435 (when obsolete
436 (princ "\nThis function is obsolete")
437 (when (nth 2 obsolete)
438 (insert (format " since %s" (nth 2 obsolete))))
439 (insert (cond ((stringp use) (concat ";\n" use))
440 (use (format ";\nuse `%s' instead." use))
441 (t "."))
442 "\n"))
443 (insert "\n"
444 (or doc "Not documented."))))))))
447 ;; Variables
449 ;;;###autoload
450 (defun variable-at-point (&optional any-symbol)
451 "Return the bound variable symbol found at or before point.
452 Return 0 if there is no such symbol.
453 If ANY-SYMBOL is non-nil, don't insist the symbol be bound."
454 (with-syntax-table emacs-lisp-mode-syntax-table
455 (or (condition-case ()
456 (save-excursion
457 (or (not (zerop (skip-syntax-backward "_w")))
458 (eq (char-syntax (following-char)) ?w)
459 (eq (char-syntax (following-char)) ?_)
460 (forward-sexp -1))
461 (skip-chars-forward "'")
462 (let ((obj (read (current-buffer))))
463 (and (symbolp obj) (boundp obj) obj)))
464 (error nil))
465 (let* ((str (find-tag-default))
466 (sym (if str (intern-soft str))))
467 (if (and sym (or any-symbol (boundp sym)))
469 (save-match-data
470 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
471 (setq sym (intern-soft (match-string 1 str)))
472 (and (or any-symbol (boundp sym)) sym)))))
473 0)))
475 (defun describe-variable-custom-version-info (variable)
476 (let ((custom-version (get variable 'custom-version))
477 (cpv (get variable 'custom-package-version))
478 (output nil))
479 (if custom-version
480 (setq output
481 (format "This variable was introduced, or its default value was changed, in\nversion %s of Emacs.\n"
482 custom-version))
483 (when cpv
484 (let* ((package (car-safe cpv))
485 (version (if (listp (cdr-safe cpv))
486 (car (cdr-safe cpv))
487 (cdr-safe cpv)))
488 (pkg-versions (assq package customize-package-emacs-version-alist))
489 (emacsv (cdr (assoc version pkg-versions))))
490 (if (and package version)
491 (setq output
492 (format (concat "This variable was introduced, or its default value was changed, in\nversion %s of the %s package"
493 (if emacsv
494 (format " that is part of Emacs %s" emacsv))
495 ".\n")
496 version package))))))
497 output))
499 ;;;###autoload
500 (defun describe-variable (variable &optional buffer frame)
501 "Display the full documentation of VARIABLE (a symbol).
502 Returns the documentation as a string, also.
503 If VARIABLE has a buffer-local value in BUFFER or FRAME
504 \(default to the current buffer and current frame),
505 it is displayed along with the global value."
506 (interactive
507 (let ((v (variable-at-point))
508 (enable-recursive-minibuffers t)
509 val)
510 (setq val (completing-read (if (symbolp v)
511 (format
512 "Describe variable (default %s): " v)
513 "Describe variable: ")
514 obarray
515 '(lambda (vv)
516 (or (boundp vv)
517 (get vv 'variable-documentation)))
518 t nil nil
519 (if (symbolp v) (symbol-name v))))
520 (list (if (equal val "")
521 v (intern val)))))
522 (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
523 (unless (frame-live-p frame) (setq frame (selected-frame)))
524 (if (not (symbolp variable))
525 (message "You did not specify a variable")
526 (save-excursion
527 (let ((valvoid (not (with-current-buffer buffer (boundp variable))))
528 val val-start-pos locus)
529 ;; Extract the value before setting up the output buffer,
530 ;; in case `buffer' *is* the output buffer.
531 (unless valvoid
532 (with-selected-frame frame
533 (with-current-buffer buffer
534 (setq val (symbol-value variable)
535 locus (variable-binding-locus variable)))))
536 (help-setup-xref (list #'describe-variable variable buffer)
537 (interactive-p))
538 (with-help-window (help-buffer)
539 (with-current-buffer buffer
540 (prin1 variable)
541 ;; Make a hyperlink to the library if appropriate. (Don't
542 ;; change the format of the buffer's initial line in case
543 ;; anything expects the current format.)
544 (let ((file-name (symbol-file variable 'defvar)))
545 (setq file-name (describe-simplify-lib-file-name file-name))
546 (when (equal file-name "loaddefs.el")
547 ;; Find the real def site of the preloaded variable.
548 (let ((location
549 (condition-case nil
550 (find-variable-noselect variable file-name)
551 (error nil))))
552 (when location
553 (with-current-buffer (car location)
554 (when (cdr location)
555 (goto-char (cdr location)))
556 (when (re-search-backward
557 "^;;; Generated autoloads from \\(.*\\)" nil t)
558 (setq file-name (match-string 1)))))))
559 (when (and (null file-name)
560 (integerp (get variable 'variable-documentation)))
561 ;; It's a variable not defined in Elisp but in C.
562 (setq file-name
563 (if (get-buffer " *DOC*")
564 (help-C-file-name variable 'var)
565 'C-source)))
566 (if file-name
567 (progn
568 (princ " is a variable defined in `")
569 (princ (if (eq file-name 'C-source) "C source code" file-name))
570 (princ "'.\n")
571 (with-current-buffer standard-output
572 (save-excursion
573 (re-search-backward "`\\([^`']+\\)'" nil t)
574 (help-xref-button 1 'help-variable-def
575 variable file-name)))
576 (if valvoid
577 (princ "It is void as a variable.")
578 (princ "Its ")))
579 (if valvoid
580 (princ " is void as a variable.")
581 (princ "'s "))))
582 (if valvoid
584 (with-current-buffer standard-output
585 (setq val-start-pos (point))
586 (princ "value is ")
587 (terpri)
588 (let ((from (point)))
589 (pp val)
590 ;; Hyperlinks in variable's value are quite frequently
591 ;; inappropriate e.g C-h v <RET> features <RET>
592 ;; (help-xref-on-pp from (point))
593 (if (< (point) (+ from 20))
594 (delete-region (1- from) from)))))
595 (terpri)
597 (when locus
598 (if (bufferp locus)
599 (princ (format "%socal in buffer %s; "
600 (if (get variable 'permanent-local)
601 "Permanently l" "L")
602 (buffer-name)))
603 (princ (format "It is a frame-local variable; ")))
604 (if (not (default-boundp variable))
605 (princ "globally void")
606 (let ((val (default-value variable)))
607 (with-current-buffer standard-output
608 (princ "global value is ")
609 (terpri)
610 ;; Fixme: pp can take an age if you happen to
611 ;; ask for a very large expression. We should
612 ;; probably print it raw once and check it's a
613 ;; sensible size before prettyprinting. -- fx
614 (let ((from (point)))
615 (pp val)
616 ;; See previous comment for this function.
617 ;; (help-xref-on-pp from (point))
618 (if (< (point) (+ from 20))
619 (delete-region (1- from) from))))))
620 (terpri))
622 ;; If the value is large, move it to the end.
623 (with-current-buffer standard-output
624 (when (> (count-lines (point-min) (point-max)) 10)
625 ;; Note that setting the syntax table like below
626 ;; makes forward-sexp move over a `'s' at the end
627 ;; of a symbol.
628 (set-syntax-table emacs-lisp-mode-syntax-table)
629 (goto-char val-start-pos)
630 ;; The line below previously read as
631 ;; (delete-region (point) (progn (end-of-line) (point)))
632 ;; which suppressed display of the buffer local value for
633 ;; large values.
634 (when (looking-at "value is") (replace-match ""))
635 (save-excursion
636 (insert "\n\nValue:")
637 (set (make-local-variable 'help-button-cache)
638 (point-marker)))
639 (insert "value is shown ")
640 (insert-button "below"
641 'action help-button-cache
642 'follow-link t
643 'help-echo "mouse-2, RET: show value")
644 (insert ".\n")))
645 (terpri)
647 (let* ((alias (condition-case nil
648 (indirect-variable variable)
649 (error variable)))
650 (obsolete (get variable 'byte-obsolete-variable))
651 (use (car obsolete))
652 (safe-var (get variable 'safe-local-variable))
653 (doc (or (documentation-property variable 'variable-documentation)
654 (documentation-property alias 'variable-documentation)))
655 (extra-line nil))
656 ;; Add a note for variables that have been make-var-buffer-local.
657 (when (and (local-variable-if-set-p variable)
658 (or (not (local-variable-p variable))
659 (with-temp-buffer
660 (local-variable-if-set-p variable))))
661 (setq extra-line t)
662 (princ " Automatically becomes buffer-local when set in any fashion.\n"))
664 ;; Mention if it's an alias
665 (unless (eq alias variable)
666 (setq extra-line t)
667 (princ (format " This variable is an alias for `%s'.\n" alias)))
669 (when obsolete
670 (setq extra-line t)
671 (princ " This variable is obsolete")
672 (if (cdr obsolete) (princ (format " since %s" (cdr obsolete))))
673 (princ (cond ((stringp use) (concat ";\n " use))
674 (use (format ";\n use `%s' instead." (car obsolete)))
675 (t ".")))
676 (terpri))
677 (when safe-var
678 (setq extra-line t)
679 (princ " This variable is safe as a file local variable ")
680 (princ "if its value\n satisfies the predicate ")
681 (princ (if (byte-code-function-p safe-var)
682 "which is byte-compiled expression.\n"
683 (format "`%s'.\n" safe-var))))
685 (if extra-line (terpri))
686 (princ "Documentation:\n")
687 (with-current-buffer standard-output
688 (insert (or doc "Not documented as a variable."))))
690 ;; Make a link to customize if this variable can be customized.
691 (when (custom-variable-p variable)
692 (let ((customize-label "customize"))
693 (terpri)
694 (terpri)
695 (princ (concat "You can " customize-label " this variable."))
696 (with-current-buffer standard-output
697 (save-excursion
698 (re-search-backward
699 (concat "\\(" customize-label "\\)") nil t)
700 (help-xref-button 1 'help-customize-variable variable))))
701 ;; Note variable's version or package version
702 (let ((output (describe-variable-custom-version-info variable)))
703 (when output
704 (terpri)
705 (terpri)
706 (princ output))))
708 (save-excursion
709 (set-buffer standard-output)
710 ;; Return the text we displayed.
711 (buffer-string))))))))
714 ;;;###autoload
715 (defun describe-syntax (&optional buffer)
716 "Describe the syntax specifications in the syntax table of BUFFER.
717 The descriptions are inserted in a help buffer, which is then displayed.
718 BUFFER defaults to the current buffer."
719 (interactive)
720 (setq buffer (or buffer (current-buffer)))
721 (help-setup-xref (list #'describe-syntax buffer) (interactive-p))
722 (with-help-window (help-buffer)
723 (let ((table (with-current-buffer buffer (syntax-table))))
724 (with-current-buffer standard-output
725 (describe-vector table 'internal-describe-syntax-value)
726 (while (setq table (char-table-parent table))
727 (insert "\nThe parent syntax table is:")
728 (describe-vector table 'internal-describe-syntax-value))))))
730 (defun help-describe-category-set (value)
731 (insert (cond
732 ((null value) "default")
733 ((char-table-p value) "deeper char-table ...")
734 (t (condition-case err
735 (category-set-mnemonics value)
736 (error "invalid"))))))
738 ;;;###autoload
739 (defun describe-categories (&optional buffer)
740 "Describe the category specifications in the current category table.
741 The descriptions are inserted in a buffer, which is then displayed.
742 If BUFFER is non-nil, then describe BUFFER's category table instead.
743 BUFFER should be a buffer or a buffer name."
744 (interactive)
745 (setq buffer (or buffer (current-buffer)))
746 (help-setup-xref (list #'describe-categories buffer) (interactive-p))
747 (with-help-window (help-buffer)
748 (let ((table (with-current-buffer buffer (category-table))))
749 (with-current-buffer standard-output
750 (describe-vector table 'help-describe-category-set)
751 (let ((docs (char-table-extra-slot table 0)))
752 (if (or (not (vectorp docs)) (/= (length docs) 95))
753 (insert "Invalid first extra slot in this char table\n")
754 (insert "Meanings of mnemonic characters are:\n")
755 (dotimes (i 95)
756 (let ((elt (aref docs i)))
757 (when elt
758 (insert (+ i ?\s) ": " elt "\n"))))
759 (while (setq table (char-table-parent table))
760 (insert "\nThe parent category table is:")
761 (describe-vector table 'help-describe-category-set))))))))
763 (provide 'help-fns)
765 ;; arch-tag: 9e10331c-ae81-4d13-965d-c4819aaab0b3
766 ;;; help-fns.el ends here