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