Fix commentary.
[emacs.git] / lisp / help-fns.el
blobb5172ec024bca9ede30c4e58218dc79992956440
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, or (at your option)
14 ;; 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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; This file contains those help commands which are complicated, and
29 ;; which may not be used in every session. For example
30 ;; `describe-function' will probably be heavily used when doing elisp
31 ;; programming, but not if just editing C files. Simpler help commands
32 ;; are in help.el
34 ;;; Code:
36 (require 'help-mode)
38 ;; Functions
40 ;;;###autoload
41 (defun describe-function (function)
42 "Display the full documentation of FUNCTION (a symbol)."
43 (interactive
44 (let ((fn (function-called-at-point))
45 (enable-recursive-minibuffers t)
46 val)
47 (setq val (completing-read (if fn
48 (format "Describe function (default %s): " fn)
49 "Describe function: ")
50 obarray 'fboundp t nil nil
51 (and fn (symbol-name fn))))
52 (list (if (equal val "")
53 fn (intern val)))))
54 (if (null function)
55 (message "You didn't specify a function")
56 (help-setup-xref (list #'describe-function function) (interactive-p))
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))))))
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)))))
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))))))
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 (if (eq (car-safe def) 'macro) (setq def (cdr def)))
105 (cond
106 ((byte-code-function-p def) (aref def 0))
107 ((eq (car-safe def) 'lambda) (nth 1 def))
108 ((and (eq (car-safe def) 'autoload) (not (eq (nth 4 def) 'keymap)))
109 "[Arg list not available until function definition is loaded.]")
110 (t t)))
112 (defun help-make-usage (function arglist)
113 (cons (if (symbolp function) function 'anonymous)
114 (mapcar (lambda (arg)
115 (if (not (symbolp arg))
116 (if (and (consp arg) (symbolp (car arg)))
117 ;; CL style default values for optional args.
118 (cons (intern (upcase (symbol-name (car arg))))
119 (cdr arg))
120 arg)
121 (let ((name (symbol-name arg)))
122 (if (string-match "\\`&" name) arg
123 (intern (upcase name))))))
124 arglist)))
126 ;; Could be this, if we make symbol-file do the work below.
127 ;; (defun help-C-file-name (subr-or-var kind)
128 ;; "Return the name of the C file where SUBR-OR-VAR is defined.
129 ;; KIND should be `var' for a variable or `subr' for a subroutine."
130 ;; (symbol-file (if (symbolp subr-or-var) subr-or-var
131 ;; (subr-name subr-or-var))
132 ;; (if (eq kind 'var) 'defvar 'defun)))
133 ;;;###autoload
134 (defun help-C-file-name (subr-or-var kind)
135 "Return the name of the C file where SUBR-OR-VAR is defined.
136 KIND should be `var' for a variable or `subr' for a subroutine."
137 (let ((docbuf (get-buffer-create " *DOC*"))
138 (name (if (eq 'var kind)
139 (concat "V" (symbol-name subr-or-var))
140 (concat "F" (subr-name subr-or-var)))))
141 (with-current-buffer docbuf
142 (goto-char (point-min))
143 (if (eobp)
144 (insert-file-contents-literally
145 (expand-file-name internal-doc-file-name doc-directory)))
146 (let ((file (catch 'loop
147 (while t
148 (let ((pnt (search-forward (concat "\x1f" name "\n"))))
149 (re-search-backward "\x1fS\\(.*\\)")
150 (let ((file (match-string 1)))
151 (if (member file build-files)
152 (throw 'loop file)
153 (goto-char pnt))))))))
154 (if (string-match "\\.\\(o\\|obj\\)\\'" file)
155 (setq file (replace-match ".c" t t file)))
156 (if (string-match "\\.c\\'" 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 (setq string
273 (cond ((or (stringp def)
274 (vectorp def))
275 "a keyboard macro")
276 ((subrp def)
277 (if (eq 'unevalled (cdr (subr-arity def)))
278 (concat beg "special form")
279 (concat beg "built-in function")))
280 ((byte-code-function-p def)
281 (concat beg "compiled Lisp function"))
282 ((symbolp def)
283 (while (symbolp (symbol-function def))
284 (setq def (symbol-function def)))
285 (format "an alias for `%s'" def))
286 ((eq (car-safe def) 'lambda)
287 (concat beg "Lisp function"))
288 ((eq (car-safe def) 'macro)
289 "a Lisp macro")
290 ((eq (car-safe def) 'autoload)
291 (setq file-name (nth 1 def))
292 (format "%s autoloaded %s"
293 (if (commandp def) "an interactive" "an")
294 (if (eq (nth 4 def) 'keymap) "keymap"
295 (if (nth 4 def) "Lisp macro" "Lisp function"))
297 ((keymapp def)
298 (let ((is-full nil)
299 (elts (cdr-safe def)))
300 (while elts
301 (if (char-table-p (car-safe elts))
302 (setq is-full t
303 elts nil))
304 (setq elts (cdr-safe elts)))
305 (if is-full
306 "a full keymap"
307 "a sparse keymap")))
308 (t "")))
309 (princ string)
310 (with-current-buffer standard-output
311 (save-excursion
312 (save-match-data
313 (if (re-search-backward "alias for `\\([^`']+\\)'" nil t)
314 (help-xref-button 1 'help-function def)))))
315 (or file-name
316 (setq file-name (symbol-file function 'defun)))
317 (setq file-name (describe-simplify-lib-file-name file-name))
318 (when (equal file-name "loaddefs.el")
319 ;; Find the real def site of the preloaded function.
320 ;; This is necessary only for defaliases.
321 (let ((location
322 (condition-case nil
323 (find-function-search-for-symbol function nil "loaddefs.el")
324 (error nil))))
325 (when location
326 (with-current-buffer (car location)
327 (goto-char (cdr location))
328 (when (re-search-backward
329 "^;;; Generated autoloads from \\(.*\\)" nil t)
330 (setq file-name (match-string 1)))))))
331 (when (and (null file-name) (subrp def))
332 ;; Find the C source file name.
333 (setq file-name (if (get-buffer " *DOC*")
334 (help-C-file-name def 'subr)
335 'C-source)))
336 (when file-name
337 (princ " in `")
338 ;; We used to add .el to the file name,
339 ;; but that's completely wrong when the user used load-file.
340 (princ (if (eq file-name 'C-source) "C source code" file-name))
341 (princ "'")
342 ;; See if lisp files are present where they where installed from.
343 (if (not (eq file-name 'C-source))
344 (setq file-name (find-source-lisp-file file-name)))
346 ;; Make a hyperlink to the library.
347 (with-current-buffer standard-output
348 (save-excursion
349 (re-search-backward "`\\([^`']+\\)'" nil t)
350 (help-xref-button 1 'help-function-def real-function file-name))))
351 (princ ".")
352 (with-current-buffer (help-buffer)
353 (fill-region-as-paragraph (save-excursion (goto-char pt1) (forward-line 0) (point))
354 (point)))
355 (terpri)(terpri)
356 (when (commandp function)
357 (let ((pt2 (with-current-buffer (help-buffer) (point))))
358 (if (and (eq function 'self-insert-command)
359 (eq (key-binding "a") 'self-insert-command)
360 (eq (key-binding "b") 'self-insert-command)
361 (eq (key-binding "c") 'self-insert-command))
362 (princ "It is bound to many ordinary text characters.\n")
363 (let* ((remapped (command-remapping function))
364 (keys (where-is-internal
365 (or remapped function) overriding-local-map nil nil))
366 non-modified-keys)
367 ;; Which non-control non-meta keys run this command?
368 (dolist (key keys)
369 (if (member (event-modifiers (aref key 0)) '(nil (shift)))
370 (push key non-modified-keys)))
371 (when remapped
372 (princ "It is remapped to `")
373 (princ (symbol-name remapped))
374 (princ "'"))
376 (when keys
377 (princ (if remapped ", which is bound to " "It is bound to "))
378 ;; If lots of ordinary text characters run this command,
379 ;; don't mention them one by one.
380 (if (< (length non-modified-keys) 10)
381 (princ (mapconcat 'key-description keys ", "))
382 (dolist (key non-modified-keys)
383 (setq keys (delq key keys)))
384 (if keys
385 (progn
386 (princ (mapconcat 'key-description keys ", "))
387 (princ ", and many ordinary text characters"))
388 (princ "many ordinary text characters"))))
389 (when (or remapped keys non-modified-keys)
390 (princ ".")
391 (terpri))))
392 (with-current-buffer (help-buffer) (fill-region-as-paragraph pt2 (point)))
393 (terpri)))
394 (let* ((arglist (help-function-arglist def))
395 (doc (documentation function))
396 (usage (help-split-fundoc doc function)))
397 (with-current-buffer standard-output
398 ;; If definition is a keymap, skip arglist note.
399 (unless (keymapp def)
400 (let* ((use (cond
401 (usage (setq doc (cdr usage)) (car usage))
402 ((listp arglist)
403 (format "%S" (help-make-usage function arglist)))
404 ((stringp arglist) arglist)
405 ;; Maybe the arglist is in the docstring of a symbol
406 ;; this one is aliased to.
407 ((let ((fun real-function))
408 (while (and (symbolp fun)
409 (setq fun (symbol-function fun))
410 (not (setq usage (help-split-fundoc
411 (documentation fun)
412 function)))))
413 usage)
414 (car usage))
415 ((or (stringp def)
416 (vectorp def))
417 (format "\nMacro: %s" (format-kbd-macro def)))
418 (t "[Missing arglist. Please make a bug report.]")))
419 (high (help-highlight-arguments use doc)))
420 (let ((fill-begin (point)))
421 (insert (car high) "\n")
422 (fill-region fill-begin (point)))
423 (setq doc (cdr high))))
424 (let ((obsolete (and
425 ;; function might be a lambda construct.
426 (symbolp function)
427 (get function 'byte-obsolete-info))))
428 (when obsolete
429 (princ "\nThis function is obsolete")
430 (when (nth 2 obsolete)
431 (insert (format " since %s" (nth 2 obsolete))))
432 (insert ";\n"
433 (if (stringp (car obsolete)) (car obsolete)
434 (format "use `%s' instead." (car obsolete)))
435 "\n"))
436 (insert "\n"
437 (or doc "Not documented.")))))))
440 ;; Variables
442 ;;;###autoload
443 (defun variable-at-point (&optional any-symbol)
444 "Return the bound variable symbol found at or before point.
445 Return 0 if there is no such symbol.
446 If ANY-SYMBOL is non-nil, don't insist the symbol be bound."
447 (or (condition-case ()
448 (with-syntax-table emacs-lisp-mode-syntax-table
449 (save-excursion
450 (or (not (zerop (skip-syntax-backward "_w")))
451 (eq (char-syntax (following-char)) ?w)
452 (eq (char-syntax (following-char)) ?_)
453 (forward-sexp -1))
454 (skip-chars-forward "'")
455 (let ((obj (read (current-buffer))))
456 (and (symbolp obj) (boundp obj) obj))))
457 (error nil))
458 (let* ((str (find-tag-default))
459 (sym (if str (intern-soft str))))
460 (if (and sym (or any-symbol (boundp sym)))
462 (save-match-data
463 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
464 (setq sym (intern-soft (match-string 1 str)))
465 (and (or any-symbol (boundp sym)) sym)))))
468 (defun describe-variable-custom-version-info (variable)
469 (let ((custom-version (get variable 'custom-version))
470 (cpv (get variable 'custom-package-version))
471 (output nil))
472 (if custom-version
473 (setq output
474 (format "This variable was introduced, or its default value was changed, in\nversion %s of Emacs.\n"
475 custom-version))
476 (when cpv
477 (let* ((package (car-safe cpv))
478 (version (car (cdr-safe cpv)))
479 (pkg-versions (assq package customize-package-emacs-version-alist))
480 (emacsv (cdr (assoc version pkg-versions))))
481 (if (and package version)
482 (setq output
483 (format (concat "This variable was introduced, or its default value was changed, in\nversion %s of the %s package"
484 (if emacsv
485 (format " that is part of Emacs %s" emacsv))
486 ".\n")
487 version package))))))
488 output))
490 ;;;###autoload
491 (defun describe-variable (variable &optional buffer frame)
492 "Display the full documentation of VARIABLE (a symbol).
493 Returns the documentation as a string, also.
494 If VARIABLE has a buffer-local value in BUFFER or FRAME
495 \(default to the current buffer and current frame),
496 it is displayed along with the global value."
497 (interactive
498 (let ((v (variable-at-point))
499 (enable-recursive-minibuffers t)
500 val)
501 (setq val (completing-read (if (symbolp v)
502 (format
503 "Describe variable (default %s): " v)
504 "Describe variable: ")
505 obarray
506 '(lambda (vv)
507 (or (boundp vv)
508 (get vv 'variable-documentation)))
509 t nil nil
510 (if (symbolp v) (symbol-name v))))
511 (list (if (equal val "")
512 v (intern val)))))
513 (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
514 (unless (frame-live-p frame) (setq frame (selected-frame)))
515 (if (not (symbolp variable))
516 (message "You did not specify a variable")
517 (save-excursion
518 (let ((valvoid (not (with-current-buffer buffer (boundp variable))))
519 val val-start-pos locus)
520 ;; Extract the value before setting up the output buffer,
521 ;; in case `buffer' *is* the output buffer.
522 (unless valvoid
523 (with-selected-frame frame
524 (with-current-buffer buffer
525 (setq val (symbol-value variable)
526 locus (variable-binding-locus variable)))))
527 (help-setup-xref (list #'describe-variable variable buffer)
528 (interactive-p))
529 (with-help-window (help-buffer)
530 (with-current-buffer buffer
531 (prin1 variable)
532 ;; Make a hyperlink to the library if appropriate. (Don't
533 ;; change the format of the buffer's initial line in case
534 ;; anything expects the current format.)
535 (let ((file-name (symbol-file variable 'defvar)))
536 (setq file-name (describe-simplify-lib-file-name file-name))
537 (when (equal file-name "loaddefs.el")
538 ;; Find the real def site of the preloaded variable.
539 (let ((location
540 (condition-case nil
541 (find-variable-noselect variable file-name)
542 (error nil))))
543 (when location
544 (with-current-buffer (car location)
545 (when (cdr location)
546 (goto-char (cdr location)))
547 (when (re-search-backward
548 "^;;; Generated autoloads from \\(.*\\)" nil t)
549 (setq file-name (match-string 1)))))))
550 (when (and (null file-name)
551 (integerp (get variable 'variable-documentation)))
552 ;; It's a variable not defined in Elisp but in C.
553 (setq file-name
554 (if (get-buffer " *DOC*")
555 (help-C-file-name variable 'var)
556 'C-source)))
557 (if file-name
558 (progn
559 (princ " is a variable defined in `")
560 (princ (if (eq file-name 'C-source) "C source code" file-name))
561 (princ "'.\n")
562 (with-current-buffer standard-output
563 (save-excursion
564 (re-search-backward "`\\([^`']+\\)'" nil t)
565 (help-xref-button 1 'help-variable-def
566 variable file-name)))
567 (if valvoid
568 (princ "It is void as a variable.")
569 (princ "Its ")))
570 (if valvoid
571 (princ " is void as a variable.")
572 (princ "'s "))))
573 (if valvoid
575 (with-current-buffer standard-output
576 (setq val-start-pos (point))
577 (princ "value is ")
578 (terpri)
579 (let ((from (point)))
580 (pp val)
581 ;; Hyperlinks in variable's value are quite frequently
582 ;; inappropriate e.g C-h v <RET> features <RET>
583 ;; (help-xref-on-pp from (point))
584 (if (< (point) (+ from 20))
585 (delete-region (1- from) from)))))
586 (terpri)
588 (when locus
589 (if (bufferp locus)
590 (princ (format "%socal in buffer %s; "
591 (if (get variable 'permanent-local)
592 "Permanently l" "L")
593 (buffer-name)))
594 (princ (format "It is a frame-local variable; ")))
595 (if (not (default-boundp variable))
596 (princ "globally void")
597 (let ((val (default-value variable)))
598 (with-current-buffer standard-output
599 (princ "global value is ")
600 (terpri)
601 ;; Fixme: pp can take an age if you happen to
602 ;; ask for a very large expression. We should
603 ;; probably print it raw once and check it's a
604 ;; sensible size before prettyprinting. -- fx
605 (let ((from (point)))
606 (pp val)
607 ;; See previous comment for this function.
608 ;; (help-xref-on-pp from (point))
609 (if (< (point) (+ from 20))
610 (delete-region (1- from) from))))))
611 (terpri))
613 ;; If the value is large, move it to the end.
614 (with-current-buffer standard-output
615 (when (> (count-lines (point-min) (point-max)) 10)
616 ;; Note that setting the syntax table like below
617 ;; makes forward-sexp move over a `'s' at the end
618 ;; of a symbol.
619 (set-syntax-table emacs-lisp-mode-syntax-table)
620 (goto-char val-start-pos)
621 ;; The line below previously read as
622 ;; (delete-region (point) (progn (end-of-line) (point)))
623 ;; which suppressed display of the buffer local value for
624 ;; large values.
625 (when (looking-at "value is") (replace-match ""))
626 (save-excursion
627 (insert "\n\nValue:")
628 (set (make-local-variable 'help-button-cache)
629 (point-marker)))
630 (insert "value is shown ")
631 (insert-button "below"
632 'action help-button-cache
633 'follow-link t
634 'help-echo "mouse-2, RET: show value")
635 (insert ".\n")))
636 (terpri)
638 (let* ((alias (condition-case nil
639 (indirect-variable variable)
640 (error variable)))
641 (obsolete (get variable 'byte-obsolete-variable))
642 (safe-var (get variable 'safe-local-variable))
643 (doc (or (documentation-property variable 'variable-documentation)
644 (documentation-property alias 'variable-documentation)))
645 (extra-line nil))
646 ;; Add a note for variables that have been make-var-buffer-local.
647 (when (and (local-variable-if-set-p variable)
648 (or (not (local-variable-p variable))
649 (with-temp-buffer
650 (local-variable-if-set-p variable))))
651 (setq extra-line t)
652 (princ " Automatically becomes buffer-local when set in any fashion.\n"))
654 ;; Mention if it's an alias
655 (unless (eq alias variable)
656 (setq extra-line t)
657 (princ (format " This variable is an alias for `%s'.\n" alias)))
659 (when obsolete
660 (setq extra-line t)
661 (princ " This variable is obsolete")
662 (if (cdr obsolete) (princ (format " since %s" (cdr obsolete))))
663 (princ ";\n ")
664 (princ (if (stringp (car obsolete)) (car obsolete)
665 (format "use `%s' instead." (car obsolete))))
666 (terpri))
667 (when safe-var
668 (setq extra-line t)
669 (princ " This variable is safe as a file local variable ")
670 (princ "if its value\n satisfies the predicate ")
671 (princ (if (byte-code-function-p safe-var)
672 "which is byte-compiled expression.\n"
673 (format "`%s'.\n" safe-var))))
675 (if extra-line (terpri))
676 (princ "Documentation:\n")
677 (with-current-buffer standard-output
678 (insert (or doc "Not documented as a variable."))))
680 (let ((customize-label "customize")
681 (initialization-file "initialization file"))
682 ;; All variables can be set; some can be customized
683 (when (and (symbolp variable) (not (custom-variable-p variable)))
684 (terpri)
685 (terpri)
686 (princ (concat "You can set this variable in your "
687 initialization-file "."))
688 (with-current-buffer standard-output
689 (save-excursion
690 (re-search-backward
691 (concat "\\(" initialization-file "\\)") nil t)
692 (help-xref-button 1 'help-info-variable variable
693 "(emacs)Init File"))))
694 ;; Make a link to customize if this variable can be customized.
695 (when (custom-variable-p variable)
696 (terpri)
697 (terpri)
698 (princ (concat "You can " customize-label " this variable"))
699 (princ (concat " or set it in your " initialization-file "."))
700 (with-current-buffer standard-output
701 (save-excursion
702 (re-search-backward
703 (concat "\\(" customize-label "\\)") nil t)
704 (help-xref-button 1 'help-customize-variable variable))
705 (save-excursion
706 (re-search-backward
707 (concat "\\(" initialization-file "\\)") nil t)
708 (help-xref-button 1 'help-info-variable variable
709 "(emacs)Init File")
712 ;; Note variable's version or package version
713 (let ((output (describe-variable-custom-version-info variable)))
714 (when output
715 (terpri)
716 (terpri)
717 (princ output))))
719 (save-excursion
720 (set-buffer standard-output)
721 ;; Return the text we displayed.
722 (buffer-string))))))))
725 ;;;###autoload
726 (defun describe-syntax (&optional buffer)
727 "Describe the syntax specifications in the syntax table of BUFFER.
728 The descriptions are inserted in a help buffer, which is then displayed.
729 BUFFER defaults to the current buffer."
730 (interactive)
731 (setq buffer (or buffer (current-buffer)))
732 (help-setup-xref (list #'describe-syntax buffer) (interactive-p))
733 (with-help-window (help-buffer)
734 (let ((table (with-current-buffer buffer (syntax-table))))
735 (with-current-buffer standard-output
736 (describe-vector table 'internal-describe-syntax-value)
737 (while (setq table (char-table-parent table))
738 (insert "\nThe parent syntax table is:")
739 (describe-vector table 'internal-describe-syntax-value))))))
741 (defun help-describe-category-set (value)
742 (insert (cond
743 ((null value) "default")
744 ((char-table-p value) "deeper char-table ...")
745 (t (condition-case err
746 (category-set-mnemonics value)
747 (error "invalid"))))))
749 ;;;###autoload
750 (defun describe-categories (&optional buffer)
751 "Describe the category specifications in the current category table.
752 The descriptions are inserted in a buffer, which is then displayed.
753 If BUFFER is non-nil, then describe BUFFER's category table instead.
754 BUFFER should be a buffer or a buffer name."
755 (interactive)
756 (setq buffer (or buffer (current-buffer)))
757 (help-setup-xref (list #'describe-categories buffer) (interactive-p))
758 (with-help-window (help-buffer)
759 (let ((table (with-current-buffer buffer (category-table))))
760 (with-current-buffer standard-output
761 (describe-vector table 'help-describe-category-set)
762 (let ((docs (char-table-extra-slot table 0)))
763 (if (or (not (vectorp docs)) (/= (length docs) 95))
764 (insert "Invalid first extra slot in this char table\n")
765 (insert "Meanings of mnemonic characters are:\n")
766 (dotimes (i 95)
767 (let ((elt (aref docs i)))
768 (when elt
769 (insert (+ i ?\s) ": " elt "\n"))))
770 (while (setq table (char-table-parent table))
771 (insert "\nThe parent category table is:")
772 (describe-vector table 'help-describe-category-set))))))))
774 (provide 'help-fns)
776 ;; arch-tag: 9e10331c-ae81-4d13-965d-c4819aaab0b3
777 ;;; help-fns.el ends here