Merged in changes from CVS trunk.
[bpt/emacs.git] / lisp / help-fns.el
CommitLineData
125eb411 1;;; help-fns.el --- Complex help functions
44e75f50 2
154ee9b7 3;; Copyright (C) 1985, 86, 93, 94, 98, 1999, 2000, 01, 02, 03, 2004
44e75f50
MB
4;; Free Software Foundation, Inc.
5
6;; Maintainer: FSF
7;; Keywords: help, internal
8
9;; This file is part of GNU Emacs.
10
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.
15
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.
20
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., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
25
26;;; Commentary:
27
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
33
34;;; Code:
35
8422a3b8
SM
36(require 'help-mode)
37
38
44e75f50
MB
39;;;###autoload
40(defun help-with-tutorial (&optional arg)
41 "Select the Emacs learn-by-doing tutorial.
42If there is a tutorial version written in the language
43of the selected language environment, that version is used.
44If there's no tutorial in that language, `TUTORIAL' is selected.
bbb7041a 45With ARG, you are asked to choose which language."
44e75f50
MB
46 (interactive "P")
47 (let ((lang (if arg
e06d1742
RS
48 (let ((minibuffer-setup-hook minibuffer-setup-hook))
49 (add-hook 'minibuffer-setup-hook
bbb7041a
SM
50 'minibuffer-completion-help)
51 (read-language-name 'tutorial "Language: " "English"))
44e75f50
MB
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))
72266056 66 (hack-local-variables)
44e75f50
MB
67 (goto-char (point-min))
68 (search-forward "\n<<")
69 (beginning-of-line)
7dd3ed35
RS
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]"))
78 (t
79 (looking-at "<<")
80 (replace-match "[")
81 (search-forward ">>")
82 (replace-match "]")))
83 (beginning-of-line)
44e75f50
MB
84 (let ((n (- (window-height (selected-window))
85 (count-lines (point-min) (point))
86 6)))
7dd3ed35
RS
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))
44e75f50
MB
93 ;; Some people get confused by the large gap.
94 (newline (/ n 2))
71296446 95
7dd3ed35
RS
96 ;; Skip the [...] line (don't delete it).
97 (forward-line 1)
44e75f50
MB
98 (newline (- n (/ n 2)))))
99 (goto-char (point-min))
100 (set-buffer-modified-p nil))))
101
102;;;###autoload
103(defun locate-library (library &optional nosuffix path interactive-call)
104 "Show the precise file name of Emacs library LIBRARY.
26f19477
SM
105This command searches the directories in `load-path' like `\\[load-library]'
106to find the file that `\\[load-library] RET LIBRARY RET' would load.
be5fc59b 107Optional second arg NOSUFFIX non-nil means don't add suffixes `load-suffixes'
44e75f50
MB
108to the specified name LIBRARY.
109
110If the optional third arg PATH is specified, that list of directories
111is used instead of `load-path'.
112
113When called from a program, the file name is normaly returned as a
114string. When run interactively, the argument INTERACTIVE-CALL is t,
115and the file name is displayed in the echo area."
26f19477
SM
116 (interactive (list (completing-read "Locate library: "
117 'locate-file-completion
118 (cons load-path load-suffixes))
44e75f50
MB
119 nil nil
120 t))
26f19477
SM
121 (let ((file (locate-file library
122 (or path load-path)
123 (append (unless nosuffix load-suffixes) '("")))))
be5fc59b 124 (if interactive-call
26f19477
SM
125 (if file
126 (message "Library is file %s" (abbreviate-file-name file))
127 (message "No library %s in search path" library)))
128 file))
44e75f50
MB
129
130\f
131;; Functions
132
133;;;###autoload
134(defun describe-function (function)
135 "Display the full documentation of FUNCTION (a symbol)."
136 (interactive
137 (let ((fn (function-called-at-point))
138 (enable-recursive-minibuffers t)
139 val)
140 (setq val (completing-read (if fn
141 (format "Describe function (default %s): " fn)
142 "Describe function: ")
143 obarray 'fboundp t nil nil (symbol-name fn)))
144 (list (if (equal val "")
145 fn (intern val)))))
146 (if (null function)
147 (message "You didn't specify a function")
8422a3b8 148 (help-setup-xref (list #'describe-function function) (interactive-p))
5a6a8d3b
RS
149 (save-excursion
150 (with-output-to-temp-buffer (help-buffer)
151 (prin1 function)
152 ;; Use " is " instead of a colon so that
153 ;; it is easier to get out the function name using forward-sexp.
154 (princ " is ")
155 (describe-function-1 function)
156 (print-help-return-message)
157 (with-current-buffer standard-output
158 ;; Return the text we displayed.
159 (buffer-string))))))
44e75f50 160
f63f0981 161(defun help-split-fundoc (doc def)
2dbbed9e 162 "Split a function docstring DOC into the actual doc and the usage info.
f63f0981
SM
163Return (USAGE . DOC) or nil if there's no usage info.
164DEF is the function whose usage we're looking for in DOC."
165 ;; Functions can get the calling sequence at the end of the doc string.
2dbbed9e 166 ;; In cases where `function' has been fset to a subr we can't search for
f63f0981
SM
167 ;; function's name in the doc string so we use `fn' as the anonymous
168 ;; function name instead.
bbb7041a
SM
169 (when (and doc (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" doc))
170 (cons (format "(%s%s"
171 ;; Replace `fn' with the actual function name.
172 (if (consp def) "anonymous" def)
173 (match-string 1 doc))
174 (substring doc 0 (match-beginning 0)))))
175
176(defun help-add-fundoc-usage (doc arglist)
177 "Add the usage info to the docstring DOC.
178If DOC already has a usage info, then just return DOC unchanged.
95734598 179The usage info is built from ARGLIST. DOC can be nil.
f8daddcf 180ARGLIST can also be t or a string of the form \"(fun ARG1 ARG2 ...)\"."
bbb7041a 181 (unless (stringp doc) (setq doc "Not documented"))
f8daddcf 182 (if (or (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" doc) (eq arglist t))
bbb7041a
SM
183 doc
184 (format "%s%s%s" doc
185 (if (string-match "\n?\n\\'" doc)
88ff724b 186 (if (< (- (match-end 0) (match-beginning 0)) 2) "\n" "")
bbb7041a 187 "\n\n")
95734598
SM
188 (if (and (stringp arglist)
189 (string-match "\\`([^ ]+\\(.*\\))\\'" arglist))
190 (concat "(fn" (match-string 1 arglist) ")")
191 (help-make-usage 'fn arglist)))))
2dbbed9e
SM
192
193(defun help-function-arglist (def)
26f19477
SM
194 ;; Handle symbols aliased to other symbols.
195 (if (and (symbolp def) (fboundp def)) (setq def (indirect-function def)))
196 ;; If definition is a macro, find the function inside it.
197 (if (eq (car-safe def) 'macro) (setq def (cdr def)))
2dbbed9e
SM
198 (cond
199 ((byte-code-function-p def) (aref def 0))
200 ((eq (car-safe def) 'lambda) (nth 1 def))
201 ((and (eq (car-safe def) 'autoload) (not (eq (nth 4 def) 'keymap)))
202 "[Arg list not available until function definition is loaded.]")
203 (t t)))
204
205(defun help-make-usage (function arglist)
206 (cons (if (symbolp function) function 'anonymous)
207 (mapcar (lambda (arg)
f63f0981
SM
208 (if (not (symbolp arg))
209 (if (and (consp arg) (symbolp (car arg)))
210 ;; CL style default values for optional args.
211 (cons (intern (upcase (symbol-name (car arg))))
212 (cdr arg))
213 arg)
2dbbed9e
SM
214 (let ((name (symbol-name arg)))
215 (if (string-match "\\`&" name) arg
216 (intern (upcase name))))))
217 arglist)))
218
154ee9b7
SM
219(defvar help-C-source-directory
220 (let ((dir (expand-file-name "src" source-directory)))
221 (when (and (file-directory-p dir) (file-readable-p dir))
222 dir))
223 "Directory where the C source files of Emacs can be found.
224If nil, do not try to find the source code of functions and variables
225defined in C.")
226
227(defun help-subr-name (subr)
228 (let ((name (prin1-to-string subr)))
229 (if (string-match "\\`#<subr \\(.*\\)>\\'" name)
230 (match-string 1 name)
231 (error "Unexpected subroutine print name: %s" name))))
232
233(defun help-C-file-name (subr-or-var kind)
234 "Return the name of the C file where SUBR-OR-VAR is defined.
235KIND should be `var' for a variable or `subr' for a subroutine."
236 (let ((docbuf (get-buffer-create " *DOC*"))
237 (name (if (eq 'var kind)
238 (concat "V" (symbol-name subr-or-var))
239 (concat "F" (help-subr-name subr-or-var)))))
240 (with-current-buffer docbuf
241 (goto-char (point-min))
242 (if (eobp)
243 (insert-file-contents-literally
244 (expand-file-name internal-doc-file-name doc-directory)))
245 (search-forward (concat "\1f" name "\n"))
246 (re-search-backward "\1fS\\(.*\\)")
247 (let ((file (match-string 1)))
248 (if (string-match "\\.\\(o\\|obj\\)\\'" file)
249 (replace-match ".c" t t file)
250 file)))))
251
252(defun help-find-C-source (fun-or-var file kind)
253 "Find the source location where SUBR-OR-VAR is defined in FILE.
254KIND should be `var' for a variable or `subr' for a subroutine."
255 (setq file (expand-file-name file help-C-source-directory))
256 (unless (file-readable-p file)
257 (error "The C source file %s is not available"
258 (file-name-nondirectory file)))
259 (if (eq 'fun kind)
260 (setq fun-or-var (indirect-function fun-or-var)))
261 (with-current-buffer (find-file-noselect file)
262 (goto-char (point-min))
263 (unless (re-search-forward
264 (if (eq 'fun kind)
265 (concat "DEFUN[ \t\n]*([ \t\n]*\""
266 (regexp-quote (help-subr-name fun-or-var))
267 "\"")
268 (concat "DEFVAR[A-Z_]*[ \t\n]*([ \t\n]*\""
269 (regexp-quote (symbol-name fun-or-var))))
270 nil t)
271 (error "Can't find source for %s" fun))
272 (cons (current-buffer) (match-beginning 0))))
273
8422a3b8
SM
274;;;###autoload
275(defun describe-function-1 (function)
44e75f50
MB
276 (let* ((def (if (symbolp function)
277 (symbol-function function)
278 function))
8422a3b8 279 file-name string
44e75f50
MB
280 (beg (if (commandp def) "an interactive " "a ")))
281 (setq string
282 (cond ((or (stringp def)
283 (vectorp def))
284 "a keyboard macro")
285 ((subrp def)
286 (if (eq 'unevalled (cdr (subr-arity def)))
287 (concat beg "special form")
288 (concat beg "built-in function")))
289 ((byte-code-function-p def)
290 (concat beg "compiled Lisp function"))
291 ((symbolp def)
292 (while (symbolp (symbol-function def))
293 (setq def (symbol-function def)))
294 (format "an alias for `%s'" def))
295 ((eq (car-safe def) 'lambda)
296 (concat beg "Lisp function"))
297 ((eq (car-safe def) 'macro)
298 "a Lisp macro")
44e75f50
MB
299 ((eq (car-safe def) 'autoload)
300 (setq file-name (nth 1 def))
301 (format "%s autoloaded %s"
302 (if (commandp def) "an interactive" "an")
303 (if (eq (nth 4 def) 'keymap) "keymap"
304 (if (nth 4 def) "Lisp macro" "Lisp function"))
305 ))
26f19477 306 ((keymapp def)
44e75f50
MB
307 (let ((is-full nil)
308 (elts (cdr-safe def)))
309 (while elts
310 (if (char-table-p (car-safe elts))
311 (setq is-full t
312 elts nil))
313 (setq elts (cdr-safe elts)))
314 (if is-full
315 "a full keymap"
316 "a sparse keymap")))
317 (t "")))
44e75f50 318 (princ string)
8422a3b8 319 (with-current-buffer standard-output
44e75f50
MB
320 (save-excursion
321 (save-match-data
322 (if (re-search-backward "alias for `\\([^`']+\\)'" nil t)
323 (help-xref-button 1 'help-function def)))))
324 (or file-name
325 (setq file-name (symbol-file function)))
35679c3f
MR
326 (when (equal file-name "loaddefs.el")
327 ;; Find the real def site of the preloaded function.
328 ;; This is necessary only for defaliases.
329 (let ((location
330 (condition-case nil
d24c52bb 331 (find-function-search-for-symbol function nil "loaddefs.el")
35679c3f
MR
332 (error nil))))
333 (when location
334 (with-current-buffer (car location)
335 (goto-char (cdr location))
336 (when (re-search-backward
337 "^;;; Generated autoloads from \\(.*\\)" nil t)
338 (setq file-name (match-string 1)))))))
154ee9b7
SM
339 (when (and (null file-name) (subrp def) help-C-source-directory)
340 ;; Find the C source file name.
341 (setq file-name (concat "src/" (help-C-file-name def 'subr))))
342 (when file-name
5a6a8d3b
RS
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 file-name)
347 (princ "'")
348 ;; Make a hyperlink to the library.
8422a3b8 349 (with-current-buffer standard-output
154ee9b7 350 (save-excursion
5a6a8d3b 351 (re-search-backward "`\\([^`']+\\)'" nil t)
154ee9b7 352 (help-xref-button 1 'help-function-def function file-name))))
44e75f50
MB
353 (princ ".")
354 (terpri)
355 (when (commandp function)
023b93f6 356 (let* ((remapped (command-remapping function))
d2ab11c5 357 (keys (where-is-internal
19a151e5
RS
358 (or remapped function) overriding-local-map nil nil))
359 non-modified-keys)
360 ;; Which non-control non-meta keys run this command?
361 (dolist (key keys)
362 (if (member (event-modifiers (aref key 0)) '(nil (shift)))
363 (push key non-modified-keys)))
d2ab11c5
KS
364 (when remapped
365 (princ "It is remapped to `")
366 (princ (symbol-name remapped))
367 (princ "'"))
19a151e5 368
44e75f50 369 (when keys
d2ab11c5 370 (princ (if remapped " which is bound to " "It is bound to "))
44e75f50 371 ;; FIXME: This list can be very long (f.ex. for self-insert-command).
19a151e5
RS
372 ;; If there are many, remove them from KEYS.
373 (if (< (length non-modified-keys) 10)
374 (princ (mapconcat 'key-description keys ", "))
375 (dolist (key non-modified-keys)
376 (setq keys (delq key keys)))
377 (if keys
378 (progn
379 (princ (mapconcat 'key-description keys ", "))
380 (princ ", and many ordinary text characters"))
381 (princ "many ordinary text characters"))))
382 (when (or remapped keys non-modified-keys)
44e75f50
MB
383 (princ ".")
384 (terpri))))
2dbbed9e
SM
385 (let* ((arglist (help-function-arglist def))
386 (doc (documentation function))
f63f0981 387 (usage (help-split-fundoc doc function)))
dd66897f
JPW
388 ;; If definition is a keymap, skip arglist note.
389 (unless (keymapp def)
390 (princ (cond
26f19477 391 (usage (setq doc (cdr usage)) (car usage))
dd66897f
JPW
392 ((listp arglist) (help-make-usage function arglist))
393 ((stringp arglist) arglist)
26f19477
SM
394 ;; Maybe the arglist is in the docstring of the alias.
395 ((let ((fun function))
396 (while (and (symbolp fun)
397 (setq fun (symbol-function fun))
398 (not (setq usage (help-split-fundoc
399 (documentation fun)
f63f0981 400 function)))))
26f19477
SM
401 usage)
402 (car usage))
a01ba4f1
JB
403 ((or (stringp def)
404 (vectorp def))
405 (format "\nMacro: %s" (format-kbd-macro def)))
dd66897f
JPW
406 (t "[Missing arglist. Please make a bug report.]")))
407 (terpri))
32d9a725
MR
408 (let ((obsolete (and
409 ;; function might be a lambda construct.
410 (symbolp function)
411 (get function 'byte-obsolete-info))))
2dbbed9e
SM
412 (when obsolete
413 (terpri)
414 (princ "This function is obsolete")
415 (if (nth 2 obsolete) (princ (format " since %s" (nth 2 obsolete))))
416 (princ ";") (terpri)
417 (princ (if (stringp (car obsolete)) (car obsolete)
418 (format "use `%s' instead." (car obsolete))))
419 (terpri)))
3b622b44
JB
420 (terpri)
421 (princ (or doc "Not documented.")))))
44e75f50
MB
422
423\f
424;; Variables
425
426;;;###autoload
427(defun variable-at-point ()
428 "Return the bound variable symbol found around point.
429Return 0 if there is no such symbol."
430 (condition-case ()
431 (with-syntax-table emacs-lisp-mode-syntax-table
432 (save-excursion
433 (or (not (zerop (skip-syntax-backward "_w")))
434 (eq (char-syntax (following-char)) ?w)
435 (eq (char-syntax (following-char)) ?_)
436 (forward-sexp -1))
437 (skip-chars-forward "'")
438 (let ((obj (read (current-buffer))))
439 (or (and (symbolp obj) (boundp obj) obj)
440 0))))
441 (error 0)))
442
443;;;###autoload
444(defun describe-variable (variable &optional buffer)
445 "Display the full documentation of VARIABLE (a symbol).
446Returns the documentation as a string, also.
447If VARIABLE has a buffer-local value in BUFFER (default to the current buffer),
448it is displayed along with the global value."
449 (interactive
450 (let ((v (variable-at-point))
451 (enable-recursive-minibuffers t)
452 val)
453 (setq val (completing-read (if (symbolp v)
454 (format
455 "Describe variable (default %s): " v)
456 "Describe variable: ")
457 obarray 'boundp t nil nil
458 (if (symbolp v) (symbol-name v))))
459 (list (if (equal val "")
460 v (intern val)))))
d24c52bb 461 (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
44e75f50
MB
462 (if (not (symbolp variable))
463 (message "You did not specify a variable")
5a6a8d3b 464 (save-excursion
26f19477
SM
465 (let* ((valvoid (not (with-current-buffer buffer (boundp variable))))
466 ;; Extract the value before setting up the output buffer,
467 ;; in case `buffer' *is* the output buffer.
468 (val (unless valvoid (buffer-local-value variable buffer))))
5a6a8d3b
RS
469 (help-setup-xref (list #'describe-variable variable buffer)
470 (interactive-p))
471 (with-output-to-temp-buffer (help-buffer)
472 (with-current-buffer buffer
473 (prin1 variable)
26f19477
SM
474 (if valvoid
475 (princ " is void")
476 (with-current-buffer standard-output
477 (princ "'s value is ")
478 (terpri)
479 (let ((from (point)))
480 (pp val)
481 (help-xref-on-pp from (point))
482 (if (< (point) (+ from 20))
f63f0981 483 (delete-region (1- from) from)))))
5a6a8d3b
RS
484 (terpri)
485 (when (local-variable-p variable)
b0622c6d
SM
486 (princ (format "%socal in buffer %s; "
487 (if (get variable 'permanent-local)
488 "Permanently l" "L")
489 (buffer-name)))
5a6a8d3b
RS
490 (if (not (default-boundp variable))
491 (princ "globally void")
492 (let ((val (default-value variable)))
493 (with-current-buffer standard-output
494 (princ "global value is ")
495 (terpri)
496 ;; Fixme: pp can take an age if you happen to
497 ;; ask for a very large expression. We should
498 ;; probably print it raw once and check it's a
499 ;; sensible size before prettyprinting. -- fx
500 (let ((from (point)))
501 (pp val)
502 (help-xref-on-pp from (point))
503 (if (< (point) (+ from 20))
d24c52bb 504 (delete-region (1- from) from))))))
5a6a8d3b
RS
505 (terpri))
506 (terpri)
507 (with-current-buffer standard-output
508 (when (> (count-lines (point-min) (point-max)) 10)
509 ;; Note that setting the syntax table like below
510 ;; makes forward-sexp move over a `'s' at the end
511 ;; of a symbol.
512 (set-syntax-table emacs-lisp-mode-syntax-table)
513 (goto-char (point-min))
514 (if valvoid
515 (forward-line 1)
516 (forward-sexp 1)
517 (delete-region (point) (progn (end-of-line) (point)))
518 (insert " value is shown below.\n\n")
519 (save-excursion
520 (insert "\n\nValue:"))))
521 ;; Add a note for variables that have been make-var-buffer-local.
522 (when (and (local-variable-if-set-p variable)
523 (or (not (local-variable-p variable))
524 (with-temp-buffer
525 (local-variable-if-set-p variable))))
44e75f50 526 (save-excursion
5a6a8d3b
RS
527 (forward-line -1)
528 (insert "Automatically becomes buffer-local when set in any fashion.\n"))))
d00a3408 529 ;; Mention if it's an alias
50a2c5f9 530 (let* ((alias (condition-case nil
d00a3408 531 (indirect-variable variable)
50a2c5f9
JB
532 (error variable)))
533 (obsolete (get variable 'byte-obsolete-variable))
534 (doc (or (documentation-property variable 'variable-documentation)
535 (documentation-property alias 'variable-documentation))))
d00a3408
JB
536 (unless (eq alias variable)
537 (princ (format "This variable is an alias for `%s'." alias))
538 (terpri)
50a2c5f9
JB
539 (terpri))
540 (when obsolete
541 (princ "This variable is obsolete")
542 (if (cdr obsolete) (princ (format " since %s" (cdr obsolete))))
c72fcfc3 543 (princ ";") (terpri)
50a2c5f9
JB
544 (princ (if (stringp (car obsolete)) (car obsolete)
545 (format "use `%s' instead." (car obsolete))))
546 (terpri)
547 (terpri))
548 (princ (or doc "Not documented as a variable.")))
5a6a8d3b 549 ;; Make a link to customize if this variable can be customized.
13eb72d2 550 (if (custom-variable-p variable)
5a6a8d3b
RS
551 (let ((customize-label "customize"))
552 (terpri)
553 (terpri)
554 (princ (concat "You can " customize-label " this variable."))
555 (with-current-buffer standard-output
556 (save-excursion
557 (re-search-backward
558 (concat "\\(" customize-label "\\)") nil t)
559 (help-xref-button 1 'help-customize-variable variable)))))
560 ;; Make a hyperlink to the library if appropriate. (Don't
561 ;; change the format of the buffer's initial line in case
562 ;; anything expects the current format.)
dcae291f 563 (let ((file-name (symbol-file (cons 'defvar variable))))
5a6a8d3b
RS
564 (when (equal file-name "loaddefs.el")
565 ;; Find the real def site of the preloaded variable.
566 (let ((location
567 (condition-case nil
568 (find-variable-noselect variable file-name)
569 (error nil))))
570 (when location
571 (with-current-buffer (car location)
572 (goto-char (cdr location))
573 (when (re-search-backward
574 "^;;; Generated autoloads from \\(.*\\)" nil t)
575 (setq file-name (match-string 1)))))))
154ee9b7
SM
576 (when (and (null file-name)
577 (integerp (get variable 'variable-documentation)))
578 ;; It's a variable not defined in Elisp but in C.
579 (if help-C-source-directory
580 (setq file-name
581 (concat "src/" (help-C-file-name variable 'var)))
582 (princ "\n\nDefined in core C code.")))
5a6a8d3b
RS
583 (when file-name
584 (princ "\n\nDefined in `")
585 (princ file-name)
586 (princ "'.")
44e75f50
MB
587 (with-current-buffer standard-output
588 (save-excursion
5a6a8d3b
RS
589 (re-search-backward "`\\([^`']+\\)'" nil t)
590 (help-xref-button 1 'help-variable-def
591 variable file-name)))))
44e75f50 592
5a6a8d3b
RS
593 (print-help-return-message)
594 (save-excursion
595 (set-buffer standard-output)
596 ;; Return the text we displayed.
597 (buffer-string))))))))
44e75f50 598
44e75f50 599
c477f688
SM
600;;;###autoload
601(defun describe-syntax (&optional buffer)
c477f688
SM
602 "Describe the syntax specifications in the syntax table of BUFFER.
603The descriptions are inserted in a help buffer, which is then displayed.
604BUFFER defaults to the current buffer."
240cbfca 605 (interactive)
c477f688
SM
606 (setq buffer (or buffer (current-buffer)))
607 (help-setup-xref (list #'describe-syntax buffer) (interactive-p))
608 (with-output-to-temp-buffer (help-buffer)
609 (let ((table (with-current-buffer buffer (syntax-table))))
610 (with-current-buffer standard-output
611 (describe-vector table 'internal-describe-syntax-value)
612 (while (setq table (char-table-parent table))
613 (insert "\nThe parent syntax table is:")
614 (describe-vector table 'internal-describe-syntax-value))))))
615
861d9ef6
SM
616(defun help-describe-category-set (value)
617 (insert (cond
618 ((null value) "default")
619 ((char-table-p value) "deeper char-table ...")
620 (t (condition-case err
621 (category-set-mnemonics value)
622 (error "invalid"))))))
623
624;;;###autoload
625(defun describe-categories (&optional buffer)
626 "Describe the category specifications in the current category table.
47f8b8f1
LT
627The descriptions are inserted in a buffer, which is then displayed.
628If BUFFER is non-nil, then describe BUFFER's category table instead.
629BUFFER should be a buffer or a buffer name."
861d9ef6
SM
630 (interactive)
631 (setq buffer (or buffer (current-buffer)))
632 (help-setup-xref (list #'describe-categories buffer) (interactive-p))
633 (with-output-to-temp-buffer (help-buffer)
634 (let ((table (with-current-buffer buffer (category-table))))
635 (with-current-buffer standard-output
636 (describe-vector table 'help-describe-category-set)
637 (let ((docs (char-table-extra-slot table 0)))
638 (if (or (not (vectorp docs)) (/= (length docs) 95))
639 (insert "Invalid first extra slot in this char table\n")
640 (insert "Meanings of mnemonic characters are:\n")
641 (dotimes (i 95)
642 (let ((elt (aref docs i)))
643 (when elt
644 (insert (+ i ?\ ) ": " elt "\n"))))
645 (while (setq table (char-table-parent table))
646 (insert "\nThe parent category table is:")
647 (describe-vector table 'help-describe-category-set))))))))
648
125eb411 649(provide 'help-fns)
44e75f50 650
ab5796a9 651;;; arch-tag: 9e10331c-ae81-4d13-965d-c4819aaab0b3
125eb411 652;;; help-fns.el ends here