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