(describe-function-1): Fill arglist.
[emacs.git] / lisp / help-fns.el
blobc3bff9273007dbba4e2202e742682cfb27843f6f
1 ;;; help-fns.el --- Complex help functions
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001,
4 ;; 2002, 2003, 2004, 2005 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 2, 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)
39 ;;;###autoload
40 (defun help-with-tutorial (&optional arg)
41 "Select the Emacs learn-by-doing tutorial.
42 If there is a tutorial version written in the language
43 of the selected language environment, that version is used.
44 If there's no tutorial in that language, `TUTORIAL' is selected.
45 With ARG, you are asked to choose which language."
46 (interactive "P")
47 (let ((lang (if arg
48 (let ((minibuffer-setup-hook minibuffer-setup-hook))
49 (add-hook 'minibuffer-setup-hook
50 'minibuffer-completion-help)
51 (read-language-name 'tutorial "Language: " "English"))
52 (if (get-language-info current-language-environment 'tutorial)
53 current-language-environment
54 "English")))
55 file filename)
56 (setq filename (get-language-info lang 'tutorial))
57 (setq file (expand-file-name (concat "~/" filename)))
58 (delete-other-windows)
59 (if (get-file-buffer file)
60 (switch-to-buffer (get-file-buffer file))
61 (switch-to-buffer (create-file-buffer file))
62 (setq buffer-file-name file)
63 (setq default-directory (expand-file-name "~/"))
64 (setq buffer-auto-save-file-name nil)
65 (insert-file-contents (expand-file-name filename data-directory))
66 (hack-local-variables)
67 (goto-char (point-min))
68 (search-forward "\n<<")
69 (beginning-of-line)
70 ;; Convert the <<...>> line to the proper [...] line,
71 ;; or just delete the <<...>> line if a [...] line follows.
72 (cond ((save-excursion
73 (forward-line 1)
74 (looking-at "\\["))
75 (delete-region (point) (progn (forward-line 1) (point))))
76 ((looking-at "<<Blank lines inserted.*>>")
77 (replace-match "[Middle of page left blank for didactic purposes. Text continues below]"))
79 (looking-at "<<")
80 (replace-match "[")
81 (search-forward ">>")
82 (replace-match "]")))
83 (beginning-of-line)
84 (let ((n (- (window-height (selected-window))
85 (count-lines (point-min) (point))
86 6)))
87 (if (< n 8)
88 (progn
89 ;; For a short gap, we don't need the [...] line,
90 ;; so delete it.
91 (delete-region (point) (progn (end-of-line) (point)))
92 (newline n))
93 ;; Some people get confused by the large gap.
94 (newline (/ n 2))
96 ;; Skip the [...] line (don't delete it).
97 (forward-line 1)
98 (newline (- n (/ n 2)))))
99 (goto-char (point-min))
100 (setq buffer-undo-list nil)
101 (set-buffer-modified-p nil))))
104 ;; Functions
106 ;;;###autoload
107 (defun describe-function (function)
108 "Display the full documentation of FUNCTION (a symbol)."
109 (interactive
110 (let ((fn (function-called-at-point))
111 (enable-recursive-minibuffers t)
112 val)
113 (setq val (completing-read (if fn
114 (format "Describe function (default %s): " fn)
115 "Describe function: ")
116 obarray 'fboundp t nil nil (symbol-name fn)))
117 (list (if (equal val "")
118 fn (intern val)))))
119 (if (null function)
120 (message "You didn't specify a function")
121 (help-setup-xref (list #'describe-function function) (interactive-p))
122 (save-excursion
123 (with-output-to-temp-buffer (help-buffer)
124 (prin1 function)
125 ;; Use " is " instead of a colon so that
126 ;; it is easier to get out the function name using forward-sexp.
127 (princ " is ")
128 (describe-function-1 function)
129 (print-help-return-message)
130 (with-current-buffer standard-output
131 ;; Return the text we displayed.
132 (buffer-string))))))
134 (defun help-split-fundoc (docstring def)
135 "Split a function DOCSTRING into the actual doc and the usage info.
136 Return (USAGE . DOC) or nil if there's no usage info.
137 DEF is the function whose usage we're looking for in DOCSTRING."
138 ;; Functions can get the calling sequence at the end of the doc string.
139 ;; In cases where `function' has been fset to a subr we can't search for
140 ;; function's name in the doc string so we use `fn' as the anonymous
141 ;; function name instead.
142 (when (and docstring (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring))
143 (cons (format "(%s%s"
144 ;; Replace `fn' with the actual function name.
145 (if (consp def) "anonymous" def)
146 (match-string 1 docstring))
147 (substring docstring 0 (match-beginning 0)))))
149 (defun help-add-fundoc-usage (docstring arglist)
150 "Add the usage info to DOCSTRING.
151 If DOCSTRING already has a usage info, then just return it unchanged.
152 The usage info is built from ARGLIST. DOCSTRING can be nil.
153 ARGLIST can also be t or a string of the form \"(FUN ARG1 ARG2 ...)\"."
154 (unless (stringp docstring) (setq docstring "Not documented"))
155 (if (or (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring) (eq arglist t))
156 docstring
157 (concat docstring
158 (if (string-match "\n?\n\\'" docstring)
159 (if (< (- (match-end 0) (match-beginning 0)) 2) "\n" "")
160 "\n\n")
161 (if (and (stringp arglist)
162 (string-match "\\`([^ ]+\\(.*\\))\\'" arglist))
163 (concat "(fn" (match-string 1 arglist) ")")
164 (format "%S" (help-make-usage 'fn arglist))))))
166 (defun help-function-arglist (def)
167 ;; Handle symbols aliased to other symbols.
168 (if (and (symbolp def) (fboundp def)) (setq def (indirect-function def)))
169 ;; If definition is a macro, find the function inside it.
170 (if (eq (car-safe def) 'macro) (setq def (cdr def)))
171 (cond
172 ((byte-code-function-p def) (aref def 0))
173 ((eq (car-safe def) 'lambda) (nth 1 def))
174 ((and (eq (car-safe def) 'autoload) (not (eq (nth 4 def) 'keymap)))
175 "[Arg list not available until function definition is loaded.]")
176 (t t)))
178 (defun help-make-usage (function arglist)
179 (cons (if (symbolp function) function 'anonymous)
180 (mapcar (lambda (arg)
181 (if (not (symbolp arg))
182 (if (and (consp arg) (symbolp (car arg)))
183 ;; CL style default values for optional args.
184 (cons (intern (upcase (symbol-name (car arg))))
185 (cdr arg))
186 arg)
187 (let ((name (symbol-name arg)))
188 (if (string-match "\\`&" name) arg
189 (intern (upcase name))))))
190 arglist)))
192 ;; Could be this, if we make symbol-file do the work below.
193 ;; (defun help-C-file-name (subr-or-var kind)
194 ;; "Return the name of the C file where SUBR-OR-VAR is defined.
195 ;; KIND should be `var' for a variable or `subr' for a subroutine."
196 ;; (symbol-file (if (symbolp subr-or-var) subr-or-var
197 ;; (subr-name subr-or-var))
198 ;; (if (eq kind 'var) 'defvar 'defun)))
199 ;;;###autoload
200 (defun help-C-file-name (subr-or-var kind)
201 "Return the name of the C file where SUBR-OR-VAR is defined.
202 KIND should be `var' for a variable or `subr' for a subroutine."
203 (let ((docbuf (get-buffer-create " *DOC*"))
204 (name (if (eq 'var kind)
205 (concat "V" (symbol-name subr-or-var))
206 (concat "F" (subr-name subr-or-var)))))
207 (with-current-buffer docbuf
208 (goto-char (point-min))
209 (if (eobp)
210 (insert-file-contents-literally
211 (expand-file-name internal-doc-file-name doc-directory)))
212 (let ((file (catch 'loop
213 (while t
214 (let ((pnt (search-forward (concat "\x1f" name "\n"))))
215 (re-search-backward "\x1fS\\(.*\\)")
216 (let ((file (match-string 1)))
217 (if (member file build-files)
218 (throw 'loop file)
219 (goto-char pnt))))))))
220 (if (string-match "\\.\\(o\\|obj\\)\\'" file)
221 (setq file (replace-match ".c" t t file)))
222 (if (string-match "\\.c\\'" file)
223 (concat "src/" file)
224 file)))))
226 ;;;###autoload
227 (defface help-argument-name '((((supports :slant italic)) :inherit italic))
228 "Face to highlight argument names in *Help* buffers."
229 :group 'help)
231 (defun help-default-arg-highlight (arg)
232 "Default function to highlight arguments in *Help* buffers.
233 It returns ARG in face `help-argument-name'; ARG is also
234 downcased if it displays differently than the default
235 face (according to `face-differs-from-default-p')."
236 (propertize (if (face-differs-from-default-p 'help-argument-name)
237 (downcase arg)
238 arg)
239 'face 'help-argument-name))
241 (defun help-do-arg-highlight (doc args)
242 (with-syntax-table (make-syntax-table emacs-lisp-mode-syntax-table)
243 (modify-syntax-entry ?\- "w")
244 (dolist (arg args doc)
245 (setq doc (replace-regexp-in-string
246 ;; This is heuristic, but covers all common cases
247 ;; except ARG1-ARG2
248 (concat "\\<" ; beginning of word
249 "\\(?:[a-z-]*-\\)?" ; for xxx-ARG
250 "\\("
251 (regexp-quote arg)
252 "\\)"
253 "\\(?:es\\|s\\|th\\)?" ; for ARGth, ARGs
254 "\\(?:-[a-z0-9-]+\\)?" ; for ARG-xxx, ARG-n
255 "\\>") ; end of word
256 (help-default-arg-highlight arg)
257 doc t t 1)))))
259 (defun help-highlight-arguments (usage doc &rest args)
260 (when usage
261 (with-temp-buffer
262 (insert usage)
263 (goto-char (point-min))
264 (let ((case-fold-search nil)
265 (next (not (or args (looking-at "\\["))))
266 (opt nil))
267 ;; Make a list of all arguments
268 (skip-chars-forward "^ ")
269 (while next
270 (or opt (not (looking-at " &")) (setq opt t))
271 (if (not (re-search-forward " \\([\\[(]*\\)\\([^] &)\.]+\\)" nil t))
272 (setq next nil)
273 (setq args (cons (match-string 2) args))
274 (when (and opt (string= (match-string 1) "("))
275 ;; A pesky CL-style optional argument with default value,
276 ;; so let's skip over it
277 (search-backward "(")
278 (goto-char (scan-sexps (point) 1)))))
279 ;; Highlight aguments in the USAGE string
280 (setq usage (help-do-arg-highlight (buffer-string) args))
281 ;; Highlight arguments in the DOC string
282 (setq doc (and doc (help-do-arg-highlight doc args))))))
283 ;; Return value is like the one from help-split-fundoc, but highlighted
284 (cons usage doc))
286 ;;;###autoload
287 (defun describe-simplify-lib-file-name (file)
288 "Simplify a library name FILE to a relative name, and make it a source file."
289 (if file
290 ;; Try converting the absolute file name to a library name.
291 (let ((libname (file-name-nondirectory file)))
292 ;; Now convert that back to a file name and see if we get
293 ;; the original one. If so, they are equivalent.
294 (if (equal file (locate-file libname load-path '("")))
295 (if (string-match "[.]elc\\'" libname)
296 (substring libname 0 -1)
297 libname)
298 file))))
300 ;;;###autoload
301 (defun describe-function-1 (function)
302 (let* ((def (if (symbolp function)
303 (symbol-function function)
304 function))
305 file-name string
306 (beg (if (commandp def) "an interactive " "a ")))
307 (setq string
308 (cond ((or (stringp def)
309 (vectorp def))
310 "a keyboard macro")
311 ((subrp def)
312 (if (eq 'unevalled (cdr (subr-arity def)))
313 (concat beg "special form")
314 (concat beg "built-in function")))
315 ((byte-code-function-p def)
316 (concat beg "compiled Lisp function"))
317 ((symbolp def)
318 (while (symbolp (symbol-function def))
319 (setq def (symbol-function def)))
320 (format "an alias for `%s'" def))
321 ((eq (car-safe def) 'lambda)
322 (concat beg "Lisp function"))
323 ((eq (car-safe def) 'macro)
324 "a Lisp macro")
325 ((eq (car-safe def) 'autoload)
326 (setq file-name (nth 1 def))
327 (format "%s autoloaded %s"
328 (if (commandp def) "an interactive" "an")
329 (if (eq (nth 4 def) 'keymap) "keymap"
330 (if (nth 4 def) "Lisp macro" "Lisp function"))
332 ((keymapp def)
333 (let ((is-full nil)
334 (elts (cdr-safe def)))
335 (while elts
336 (if (char-table-p (car-safe elts))
337 (setq is-full t
338 elts nil))
339 (setq elts (cdr-safe elts)))
340 (if is-full
341 "a full keymap"
342 "a sparse keymap")))
343 (t "")))
344 (princ string)
345 (with-current-buffer standard-output
346 (save-excursion
347 (save-match-data
348 (if (re-search-backward "alias for `\\([^`']+\\)'" nil t)
349 (help-xref-button 1 'help-function def)))))
350 (or file-name
351 (setq file-name (symbol-file function 'defun)))
352 (setq file-name (describe-simplify-lib-file-name file-name))
353 (when (equal file-name "loaddefs.el")
354 ;; Find the real def site of the preloaded function.
355 ;; This is necessary only for defaliases.
356 (let ((location
357 (condition-case nil
358 (find-function-search-for-symbol function nil "loaddefs.el")
359 (error nil))))
360 (when location
361 (with-current-buffer (car location)
362 (goto-char (cdr location))
363 (when (re-search-backward
364 "^;;; Generated autoloads from \\(.*\\)" nil t)
365 (setq file-name (match-string 1)))))))
366 (when (and (null file-name) (subrp def))
367 ;; Find the C source file name.
368 (setq file-name (if (get-buffer " *DOC*")
369 (help-C-file-name def 'subr)
370 'C-source)))
371 (when file-name
372 (princ " in `")
373 ;; We used to add .el to the file name,
374 ;; but that's completely wrong when the user used load-file.
375 (princ (if (eq file-name 'C-source) "C source code" file-name))
376 (princ "'")
377 ;; Make a hyperlink to the library.
378 (with-current-buffer standard-output
379 (save-excursion
380 (re-search-backward "`\\([^`']+\\)'" nil t)
381 (help-xref-button 1 'help-function-def function file-name))))
382 (princ ".")
383 (terpri)
384 (when (commandp function)
385 (let* ((remapped (command-remapping function))
386 (keys (where-is-internal
387 (or remapped function) overriding-local-map nil nil))
388 non-modified-keys)
389 ;; Which non-control non-meta keys run this command?
390 (dolist (key keys)
391 (if (member (event-modifiers (aref key 0)) '(nil (shift)))
392 (push key non-modified-keys)))
393 (when remapped
394 (princ "It is remapped to `")
395 (princ (symbol-name remapped))
396 (princ "'"))
398 (when keys
399 (princ (if remapped " which is bound to " "It is bound to "))
400 ;; FIXME: This list can be very long (f.ex. for self-insert-command).
401 ;; If there are many, remove them from KEYS.
402 (if (< (length non-modified-keys) 10)
403 (princ (mapconcat 'key-description keys ", "))
404 (dolist (key non-modified-keys)
405 (setq keys (delq key keys)))
406 (if keys
407 (progn
408 (princ (mapconcat 'key-description keys ", "))
409 (princ ", and many ordinary text characters"))
410 (princ "many ordinary text characters"))))
411 (when (or remapped keys non-modified-keys)
412 (princ ".")
413 (terpri))))
414 (let* ((arglist (help-function-arglist def))
415 (doc (documentation function))
416 (usage (help-split-fundoc doc function)))
417 (with-current-buffer standard-output
418 ;; If definition is a keymap, skip arglist note.
419 (unless (keymapp def)
420 (let* ((use (cond
421 (usage (setq doc (cdr usage)) (car usage))
422 ((listp arglist)
423 (format "%S" (help-make-usage function arglist)))
424 ((stringp arglist) arglist)
425 ;; Maybe the arglist is in the docstring of the alias.
426 ((let ((fun function))
427 (while (and (symbolp fun)
428 (setq fun (symbol-function fun))
429 (not (setq usage (help-split-fundoc
430 (documentation fun)
431 function)))))
432 usage)
433 (car usage))
434 ((or (stringp def)
435 (vectorp def))
436 (format "\nMacro: %s" (format-kbd-macro def)))
437 (t "[Missing arglist. Please make a bug report.]")))
438 (high (help-highlight-arguments use doc)))
439 (let ((fill-begin (point)))
440 (insert (car high) "\n")
441 (fill-region fill-begin (point)))
442 (setq doc (cdr high))))
443 (let ((obsolete (and
444 ;; function might be a lambda construct.
445 (symbolp function)
446 (get function 'byte-obsolete-info))))
447 (when obsolete
448 (princ "\nThis function is obsolete")
449 (when (nth 2 obsolete)
450 (insert (format " since %s" (nth 2 obsolete))))
451 (insert ";\n"
452 (if (stringp (car obsolete)) (car obsolete)
453 (format "use `%s' instead." (car obsolete)))
454 "\n"))
455 (insert "\n"
456 (or doc "Not documented.")))))))
459 ;; Variables
461 ;;;###autoload
462 (defun variable-at-point (&optional any-symbol)
463 "Return the bound variable symbol found around point.
464 Return 0 if there is no such symbol.
465 If ANY-SYMBOL is non-nil, don't insist the symbol be bound."
466 (or (condition-case ()
467 (with-syntax-table emacs-lisp-mode-syntax-table
468 (save-excursion
469 (or (not (zerop (skip-syntax-backward "_w")))
470 (eq (char-syntax (following-char)) ?w)
471 (eq (char-syntax (following-char)) ?_)
472 (forward-sexp -1))
473 (skip-chars-forward "'")
474 (let ((obj (read (current-buffer))))
475 (and (symbolp obj) (boundp obj) obj))))
476 (error nil))
477 (let* ((str (find-tag-default))
478 (sym (if str (intern-soft str))))
479 (if (and sym (or any-symbol (boundp sym)))
481 (save-match-data
482 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
483 (setq sym (intern-soft (match-string 1 str)))
484 (and (or any-symbol (boundp sym)) sym)))))
487 ;;;###autoload
488 (defun describe-variable (variable &optional buffer)
489 "Display the full documentation of VARIABLE (a symbol).
490 Returns the documentation as a string, also.
491 If VARIABLE has a buffer-local value in BUFFER (default to the current buffer),
492 it is displayed along with the global value."
493 (interactive
494 (let ((v (variable-at-point))
495 (enable-recursive-minibuffers t)
496 val)
497 (setq val (completing-read (if (symbolp v)
498 (format
499 "Describe variable (default %s): " v)
500 "Describe variable: ")
501 obarray
502 '(lambda (vv)
503 (or (boundp vv)
504 (get vv 'variable-documentation)))
505 t nil nil
506 (if (symbolp v) (symbol-name v))))
507 (list (if (equal val "")
508 v (intern val)))))
509 (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
510 (if (not (symbolp variable))
511 (message "You did not specify a variable")
512 (save-excursion
513 (let* ((valvoid (not (with-current-buffer buffer (boundp variable))))
514 ;; Extract the value before setting up the output buffer,
515 ;; in case `buffer' *is* the output buffer.
516 (val (unless valvoid (buffer-local-value variable buffer)))
517 val-start-pos)
518 (help-setup-xref (list #'describe-variable variable buffer)
519 (interactive-p))
520 (with-output-to-temp-buffer (help-buffer)
521 (with-current-buffer buffer
522 (prin1 variable)
523 ;; Make a hyperlink to the library if appropriate. (Don't
524 ;; change the format of the buffer's initial line in case
525 ;; anything expects the current format.)
526 (let ((file-name (symbol-file variable 'defvar)))
527 (setq file-name (describe-simplify-lib-file-name file-name))
528 (when (equal file-name "loaddefs.el")
529 ;; Find the real def site of the preloaded variable.
530 (let ((location
531 (condition-case nil
532 (find-variable-noselect variable file-name)
533 (error nil))))
534 (when location
535 (with-current-buffer (car location)
536 (goto-char (cdr location))
537 (when (re-search-backward
538 "^;;; Generated autoloads from \\(.*\\)" nil t)
539 (setq file-name (match-string 1)))))))
540 (when (and (null file-name)
541 (integerp (get variable 'variable-documentation)))
542 ;; It's a variable not defined in Elisp but in C.
543 (setq file-name
544 (if (get-buffer " *DOC*")
545 (help-C-file-name variable 'var)
546 'C-source)))
547 (if file-name
548 (progn
549 (princ " is a variable defined in `")
550 (princ (if (eq file-name 'C-source) "C source code" file-name))
551 (princ "'.\n")
552 (with-current-buffer standard-output
553 (save-excursion
554 (re-search-backward "`\\([^`']+\\)'" nil t)
555 (help-xref-button 1 'help-variable-def
556 variable file-name)))
557 (if valvoid
558 (princ "It is void as a variable.\n")
559 (princ "Its ")))
560 (if valvoid
561 (princ " is void as a variable.\n")
562 (princ "'s "))))
563 (if valvoid
565 (with-current-buffer standard-output
566 (setq val-start-pos (point))
567 (princ "value is ")
568 (terpri)
569 (let ((from (point)))
570 (pp val)
571 ;; Hyperlinks in variable's value are quite frequently
572 ;; inappropriate e.g C-h v <RET> features <RET>
573 ;; (help-xref-on-pp from (point))
574 (if (< (point) (+ from 20))
575 (delete-region (1- from) from)))))
576 (terpri)
578 (when (local-variable-p variable)
579 (princ (format "%socal in buffer %s; "
580 (if (get variable 'permanent-local)
581 "Permanently l" "L")
582 (buffer-name)))
583 (if (not (default-boundp variable))
584 (princ "globally void")
585 (let ((val (default-value variable)))
586 (with-current-buffer standard-output
587 (princ "global value is ")
588 (terpri)
589 ;; Fixme: pp can take an age if you happen to
590 ;; ask for a very large expression. We should
591 ;; probably print it raw once and check it's a
592 ;; sensible size before prettyprinting. -- fx
593 (let ((from (point)))
594 (pp val)
595 ;; See previous comment for this function.
596 ;; (help-xref-on-pp from (point))
597 (if (< (point) (+ from 20))
598 (delete-region (1- from) from)))))))
599 ;; Add a note for variables that have been make-var-buffer-local.
600 (when (and (local-variable-if-set-p variable)
601 (or (not (local-variable-p variable))
602 (with-temp-buffer
603 (local-variable-if-set-p variable))))
604 (princ "\nAutomatically becomes buffer-local when set in any fashion.\n"))
605 (terpri)
607 ;; If the value is large, move it to the end.
608 (with-current-buffer standard-output
609 (when (> (count-lines (point-min) (point-max)) 10)
610 ;; Note that setting the syntax table like below
611 ;; makes forward-sexp move over a `'s' at the end
612 ;; of a symbol.
613 (set-syntax-table emacs-lisp-mode-syntax-table)
614 (goto-char val-start-pos)
615 (delete-region (point) (progn (end-of-line) (point)))
616 (save-excursion
617 (insert "\n\nValue:")
618 (set (make-local-variable 'help-button-cache)
619 (point-marker)))
620 (insert "value is shown ")
621 (insert-button "below"
622 'action help-button-cache
623 'follow-link t
624 'help-echo "mouse-2, RET: show value")
625 (insert ".\n\n")))
627 ;; Mention if it's an alias
628 (let* ((alias (condition-case nil
629 (indirect-variable variable)
630 (error variable)))
631 (obsolete (get variable 'byte-obsolete-variable))
632 (doc (or (documentation-property variable 'variable-documentation)
633 (documentation-property alias 'variable-documentation))))
634 (unless (eq alias variable)
635 (princ (format "\nThis variable is an alias for `%s'.\n" alias)))
636 (when obsolete
637 (princ "\nThis variable is obsolete")
638 (if (cdr obsolete) (princ (format " since %s" (cdr obsolete))))
639 (princ ";") (terpri)
640 (princ (if (stringp (car obsolete)) (car obsolete)
641 (format "use `%s' instead." (car obsolete))))
642 (terpri))
643 (princ "Documentation:\n")
644 (princ (or doc "Not documented as a variable.")))
645 ;; Make a link to customize if this variable can be customized.
646 (if (custom-variable-p variable)
647 (let ((customize-label "customize"))
648 (terpri)
649 (terpri)
650 (princ (concat "You can " customize-label " this variable."))
651 (with-current-buffer standard-output
652 (save-excursion
653 (re-search-backward
654 (concat "\\(" customize-label "\\)") nil t)
655 (help-xref-button 1 'help-customize-variable variable)))))
656 (print-help-return-message)
657 (save-excursion
658 (set-buffer standard-output)
659 ;; Return the text we displayed.
660 (buffer-string))))))))
663 ;;;###autoload
664 (defun describe-syntax (&optional buffer)
665 "Describe the syntax specifications in the syntax table of BUFFER.
666 The descriptions are inserted in a help buffer, which is then displayed.
667 BUFFER defaults to the current buffer."
668 (interactive)
669 (setq buffer (or buffer (current-buffer)))
670 (help-setup-xref (list #'describe-syntax buffer) (interactive-p))
671 (with-output-to-temp-buffer (help-buffer)
672 (let ((table (with-current-buffer buffer (syntax-table))))
673 (with-current-buffer standard-output
674 (describe-vector table 'internal-describe-syntax-value)
675 (while (setq table (char-table-parent table))
676 (insert "\nThe parent syntax table is:")
677 (describe-vector table 'internal-describe-syntax-value))))))
679 (defun help-describe-category-set (value)
680 (insert (cond
681 ((null value) "default")
682 ((char-table-p value) "deeper char-table ...")
683 (t (condition-case err
684 (category-set-mnemonics value)
685 (error "invalid"))))))
687 ;;;###autoload
688 (defun describe-categories (&optional buffer)
689 "Describe the category specifications in the current category table.
690 The descriptions are inserted in a buffer, which is then displayed.
691 If BUFFER is non-nil, then describe BUFFER's category table instead.
692 BUFFER should be a buffer or a buffer name."
693 (interactive)
694 (setq buffer (or buffer (current-buffer)))
695 (help-setup-xref (list #'describe-categories buffer) (interactive-p))
696 (with-output-to-temp-buffer (help-buffer)
697 (let ((table (with-current-buffer buffer (category-table))))
698 (with-current-buffer standard-output
699 (describe-vector table 'help-describe-category-set)
700 (let ((docs (char-table-extra-slot table 0)))
701 (if (or (not (vectorp docs)) (/= (length docs) 95))
702 (insert "Invalid first extra slot in this char table\n")
703 (insert "Meanings of mnemonic characters are:\n")
704 (dotimes (i 95)
705 (let ((elt (aref docs i)))
706 (when elt
707 (insert (+ i ?\s) ": " elt "\n"))))
708 (while (setq table (char-table-parent table))
709 (insert "\nThe parent category table is:")
710 (describe-vector table 'help-describe-category-set))))))))
712 (provide 'help-fns)
714 ;; arch-tag: 9e10331c-ae81-4d13-965d-c4819aaab0b3
715 ;;; help-fns.el ends here