(diff-find-source-location): Avoid goto-line.
[emacs.git] / lisp / help-fns.el
blobb9530d89be943a5f4879cfa9aa54fe884a182bee
1 ;;; help-fns.el --- Complex help functions
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001,
4 ;; 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 ;; Free Software Foundation, Inc.
7 ;; Maintainer: FSF
8 ;; Keywords: help, internal
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;;; Commentary:
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
33 ;;; Code:
35 (require 'help-mode)
37 ;; Functions
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))))))
67 (defun help-split-fundoc (docstring def)
68 "Split a function DOCSTRING into the actual doc and the usage info.
69 Return (USAGE . DOC) or nil if there's no usage info.
70 DEF 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)))))
82 (defun help-add-fundoc-usage (docstring arglist)
83 "Add the usage info to DOCSTRING.
84 If DOCSTRING already has a usage info, then just return it unchanged.
85 The usage info is built from ARGLIST. DOCSTRING can be nil.
86 ARGLIST 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))))))
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)))
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)))
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.
135 KIND 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 "\x1f" name "\n"))))
148 (re-search-backward "\x1fS\\(.*\\)")
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)))))
161 (defun help-default-arg-highlight (arg)
162 "Default function to highlight arguments in *Help* buffers.
163 It returns ARG in face `help-argument-name'; ARG is also
164 downcased if it displays differently than the default
165 face (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))
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)))))
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))
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').
227 ;;;###autoload
228 (defun find-lisp-object-file-name (object type)
229 "Guess the file that defined the Lisp object OBJECT, of type TYPE.
230 OBJECT should be a symbol associated with a function, variable, or face;
231 alternatively, it can be a function definition.
232 If TYPE is `variable', search for a variable definition.
233 If TYPE is `face', search for a face definition.
234 If TYPE is the value returned by `symbol-function' for a function symbol,
235 search for a function definition.
237 The return value is the absolute name of a readable file where OBJECT is
238 defined. If several such files exist, preference is given to a file
239 found via `load-path'. The return value can also be `C-source', which
240 means that OBJECT is a function or variable defined in C. If no
241 suitable 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))))
263 (when (cdr location)
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
270 (match-string-no-properties 1)
271 load-path nil 'readable))))))))
273 (cond
274 ((and (not file-name) (subrp type))
275 ;; A built-in function. The form is from `describe-function-1'.
276 (if (get-buffer " *DOC*")
277 (help-C-file-name type 'subr)
278 'C-source))
279 ((and (not file-name) (symbolp object)
280 (integerp (get object 'variable-documentation)))
281 ;; A variable defined in C. The form is from `describe-variable'.
282 (if (get-buffer " *DOC*")
283 (help-C-file-name object 'var)
284 'C-source))
285 ((not (stringp file-name))
286 ;; If we don't have a file-name string by now, we lost.
287 nil)
288 ((let ((lib-name
289 (if (string-match "[.]elc\\'" file-name)
290 (substring-no-properties file-name 0 -1)
291 file-name)))
292 ;; When the Elisp source file can be found in the install
293 ;; directory return the name of that file - `file-name' should
294 ;; have become an absolute file name ny now.
295 (or (and (file-readable-p lib-name) lib-name)
296 ;; The library might be compressed.
297 (and (file-readable-p (concat lib-name ".gz")) lib-name))))
298 ((let* ((lib-name (file-name-nondirectory file-name))
299 ;; The next form is from `describe-simplify-lib-file-name'.
300 (file-name
301 ;; Try converting the absolute file name to a library
302 ;; name, convert that back to a file name and see if we
303 ;; get the original one. If so, they are equivalent.
304 (if (equal file-name (locate-file lib-name load-path '("")))
305 (if (string-match "[.]elc\\'" lib-name)
306 (substring-no-properties lib-name 0 -1)
307 lib-name)
308 file-name))
309 ;; The next three forms are from `find-source-lisp-file'.
310 (elc-file (locate-file
311 (concat file-name
312 (if (string-match "\\.el\\'" file-name)
314 ".elc"))
315 load-path nil 'readable))
316 (str (when elc-file
317 (with-temp-buffer
318 (insert-file-contents-literally elc-file nil 0 256)
319 (buffer-string))))
320 (src-file (and str
321 (string-match ";;; from file \\(.*\\.el\\)" str)
322 (match-string 1 str))))
323 (and src-file (file-readable-p src-file) src-file))))))
325 (declare-function ad-get-advice-info "advice" (function))
327 ;;;###autoload
328 (defun describe-function-1 (function)
329 (let* ((advised (and (symbolp function) (featurep 'advice)
330 (ad-get-advice-info function)))
331 ;; If the function is advised, use the symbol that has the
332 ;; real definition, if that symbol is already set up.
333 (real-function
334 (or (and advised
335 (let ((origname (cdr (assq 'origname advised))))
336 (and (fboundp origname) origname)))
337 function))
338 ;; Get the real definition.
339 (def (if (symbolp real-function)
340 (symbol-function real-function)
341 function))
342 file-name string
343 (beg (if (commandp def) "an interactive " "a "))
344 (pt1 (with-current-buffer (help-buffer) (point)))
345 errtype)
346 (setq string
347 (cond ((or (stringp def)
348 (vectorp def))
349 "a keyboard macro")
350 ((subrp def)
351 (if (eq 'unevalled (cdr (subr-arity def)))
352 (concat beg "special form")
353 (concat beg "built-in function")))
354 ((byte-code-function-p def)
355 (concat beg "compiled Lisp function"))
356 ((symbolp def)
357 (while (and (fboundp def)
358 (symbolp (symbol-function def)))
359 (setq def (symbol-function def)))
360 ;; Handle (defalias 'foo 'bar), where bar is undefined.
361 (or (fboundp def) (setq errtype 'alias))
362 (format "an alias for `%s'" def))
363 ((eq (car-safe def) 'lambda)
364 (concat beg "Lisp function"))
365 ((eq (car-safe def) 'macro)
366 "a Lisp macro")
367 ((eq (car-safe def) 'autoload)
368 (format "%s autoloaded %s"
369 (if (commandp def) "an interactive" "an")
370 (if (eq (nth 4 def) 'keymap) "keymap"
371 (if (nth 4 def) "Lisp macro" "Lisp function"))))
372 ((keymapp def)
373 (let ((is-full nil)
374 (elts (cdr-safe def)))
375 (while elts
376 (if (char-table-p (car-safe elts))
377 (setq is-full t
378 elts nil))
379 (setq elts (cdr-safe elts)))
380 (if is-full
381 "a full keymap"
382 "a sparse keymap")))
383 (t "")))
384 (princ string)
385 (if (eq errtype 'alias)
386 (princ ",\nwhich is not defined. Please make a bug report.")
387 (with-current-buffer standard-output
388 (save-excursion
389 (save-match-data
390 (when (re-search-backward "alias for `\\([^`']+\\)'" nil t)
391 (help-xref-button 1 'help-function def)))))
393 (setq file-name (find-lisp-object-file-name function def))
394 (when file-name
395 (princ " in `")
396 ;; We used to add .el to the file name,
397 ;; but that's completely wrong when the user used load-file.
398 (princ (if (eq file-name 'C-source)
399 "C source code"
400 (file-name-nondirectory file-name)))
401 (princ "'")
402 ;; Make a hyperlink to the library.
403 (with-current-buffer standard-output
404 (save-excursion
405 (re-search-backward "`\\([^`']+\\)'" nil t)
406 (help-xref-button 1 'help-function-def function file-name))))
407 (princ ".")
408 (with-current-buffer (help-buffer)
409 (fill-region-as-paragraph (save-excursion (goto-char pt1) (forward-line 0) (point))
410 (point)))
411 (terpri)(terpri)
412 (when (commandp function)
413 (let ((pt2 (with-current-buffer (help-buffer) (point)))
414 (remapped (command-remapping function)))
415 (unless (memq remapped '(ignore undefined))
416 (let ((keys (where-is-internal
417 (or remapped function) overriding-local-map nil nil))
418 non-modified-keys)
419 (if (and (eq function 'self-insert-command)
420 (vectorp (car-safe keys))
421 (consp (aref (car keys) 0)))
422 (princ "It is bound to many ordinary text characters.\n")
423 ;; Which non-control non-meta keys run this command?
424 (dolist (key keys)
425 (if (member (event-modifiers (aref key 0)) '(nil (shift)))
426 (push key non-modified-keys)))
427 (when remapped
428 (princ "It is remapped to `")
429 (princ (symbol-name remapped))
430 (princ "'"))
432 (when keys
433 (princ (if remapped ", which is bound to " "It is bound to "))
434 ;; If lots of ordinary text characters run this command,
435 ;; don't mention them one by one.
436 (if (< (length non-modified-keys) 10)
437 (princ (mapconcat 'key-description keys ", "))
438 (dolist (key non-modified-keys)
439 (setq keys (delq key keys)))
440 (if keys
441 (progn
442 (princ (mapconcat 'key-description keys ", "))
443 (princ ", and many ordinary text characters"))
444 (princ "many ordinary text characters"))))
445 (when (or remapped keys non-modified-keys)
446 (princ ".")
447 (terpri)))))
449 (with-current-buffer (help-buffer)
450 (fill-region-as-paragraph pt2 (point))
451 (unless (looking-back "\n\n")
452 (terpri)))))
453 (let* ((arglist (help-function-arglist def))
454 (doc (documentation function))
455 (usage (help-split-fundoc doc function)))
456 (with-current-buffer standard-output
457 ;; If definition is a keymap, skip arglist note.
458 (unless (keymapp function)
459 (let* ((use (cond
460 (usage (setq doc (cdr usage)) (car usage))
461 ((listp arglist)
462 (format "%S" (help-make-usage function arglist)))
463 ((stringp arglist) arglist)
464 ;; Maybe the arglist is in the docstring of a symbol
465 ;; this one is aliased to.
466 ((let ((fun real-function))
467 (while (and (symbolp fun)
468 (setq fun (symbol-function fun))
469 (not (setq usage (help-split-fundoc
470 (documentation fun)
471 function)))))
472 usage)
473 (car usage))
474 ((or (stringp def)
475 (vectorp def))
476 (format "\nMacro: %s" (format-kbd-macro def)))
477 (t "[Missing arglist. Please make a bug report.]")))
478 (high (help-highlight-arguments use doc)))
479 (let ((fill-begin (point)))
480 (insert (car high) "\n")
481 (fill-region fill-begin (point)))
482 (setq doc (cdr high))))
483 (let* ((obsolete (and
484 ;; function might be a lambda construct.
485 (symbolp function)
486 (get function 'byte-obsolete-info)))
487 (use (car obsolete)))
488 (when obsolete
489 (princ "\nThis function is obsolete")
490 (when (nth 2 obsolete)
491 (insert (format " since %s" (nth 2 obsolete))))
492 (insert (cond ((stringp use) (concat ";\n" use))
493 (use (format ";\nuse `%s' instead." use))
494 (t "."))
495 "\n"))
496 (insert "\n"
497 (or doc "Not documented."))))))))
500 ;; Variables
502 ;;;###autoload
503 (defun variable-at-point (&optional any-symbol)
504 "Return the bound variable symbol found at or before point.
505 Return 0 if there is no such symbol.
506 If ANY-SYMBOL is non-nil, don't insist the symbol be bound."
507 (with-syntax-table emacs-lisp-mode-syntax-table
508 (or (condition-case ()
509 (save-excursion
510 (or (not (zerop (skip-syntax-backward "_w")))
511 (eq (char-syntax (following-char)) ?w)
512 (eq (char-syntax (following-char)) ?_)
513 (forward-sexp -1))
514 (skip-chars-forward "'")
515 (let ((obj (read (current-buffer))))
516 (and (symbolp obj) (boundp obj) obj)))
517 (error nil))
518 (let* ((str (find-tag-default))
519 (sym (if str (intern-soft str))))
520 (if (and sym (or any-symbol (boundp sym)))
522 (save-match-data
523 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
524 (setq sym (intern-soft (match-string 1 str)))
525 (and (or any-symbol (boundp sym)) sym)))))
526 0)))
528 (defun describe-variable-custom-version-info (variable)
529 (let ((custom-version (get variable 'custom-version))
530 (cpv (get variable 'custom-package-version))
531 (output nil))
532 (if custom-version
533 (setq output
534 (format "This variable was introduced, or its default value was changed, in\nversion %s of Emacs.\n"
535 custom-version))
536 (when cpv
537 (let* ((package (car-safe cpv))
538 (version (if (listp (cdr-safe cpv))
539 (car (cdr-safe cpv))
540 (cdr-safe cpv)))
541 (pkg-versions (assq package customize-package-emacs-version-alist))
542 (emacsv (cdr (assoc version pkg-versions))))
543 (if (and package version)
544 (setq output
545 (format (concat "This variable was introduced, or its default value was changed, in\nversion %s of the %s package"
546 (if emacsv
547 (format " that is part of Emacs %s" emacsv))
548 ".\n")
549 version package))))))
550 output))
552 ;;;###autoload
553 (defun describe-variable (variable &optional buffer frame)
554 "Display the full documentation of VARIABLE (a symbol).
555 Returns the documentation as a string, also.
556 If VARIABLE has a buffer-local value in BUFFER or FRAME
557 \(default to the current buffer and current frame),
558 it is displayed along with the global value."
559 (interactive
560 (let ((v (variable-at-point))
561 (enable-recursive-minibuffers t)
562 val)
563 (setq val (completing-read (if (symbolp v)
564 (format
565 "Describe variable (default %s): " v)
566 "Describe variable: ")
567 obarray
568 '(lambda (vv)
569 (or (boundp vv)
570 (get vv 'variable-documentation)))
571 t nil nil
572 (if (symbolp v) (symbol-name v))))
573 (list (if (equal val "")
574 v (intern val)))))
575 (let (file-name)
576 (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
577 (unless (frame-live-p frame) (setq frame (selected-frame)))
578 (if (not (symbolp variable))
579 (message "You did not specify a variable")
580 (save-excursion
581 (let ((valvoid (not (with-current-buffer buffer (boundp variable))))
582 val val-start-pos locus)
583 ;; Extract the value before setting up the output buffer,
584 ;; in case `buffer' *is* the output buffer.
585 (unless valvoid
586 (with-selected-frame frame
587 (with-current-buffer buffer
588 (setq val (symbol-value variable)
589 locus (variable-binding-locus variable)))))
590 (help-setup-xref (list #'describe-variable variable buffer)
591 (interactive-p))
592 (with-help-window (help-buffer)
593 (with-current-buffer buffer
594 (prin1 variable)
595 (setq file-name (find-lisp-object-file-name variable 'defvar))
597 (if file-name
598 (progn
599 (princ " is a variable defined in `")
600 (princ (if (eq file-name 'C-source)
601 "C source code"
602 (file-name-nondirectory file-name)))
603 (princ "'.\n")
604 (with-current-buffer standard-output
605 (save-excursion
606 (re-search-backward "`\\([^`']+\\)'" nil t)
607 (help-xref-button 1 'help-variable-def
608 variable file-name)))
609 (if valvoid
610 (princ "It is void as a variable.")
611 (princ "Its ")))
612 (if valvoid
613 (princ " is void as a variable.")
614 (princ "'s "))))
615 (if valvoid
617 (with-current-buffer standard-output
618 (setq val-start-pos (point))
619 (princ "value is ")
620 (terpri)
621 (let ((from (point)))
622 (pp val)
623 ;; Hyperlinks in variable's value are quite frequently
624 ;; inappropriate e.g C-h v <RET> features <RET>
625 ;; (help-xref-on-pp from (point))
626 (if (< (point) (+ from 20))
627 (delete-region (1- from) from)))))
628 (terpri)
630 (when locus
631 (if (bufferp locus)
632 (princ (format "%socal in buffer %s; "
633 (if (get variable 'permanent-local)
634 "Permanently l" "L")
635 (buffer-name)))
636 (princ (format "It is a frame-local variable; ")))
637 (if (not (default-boundp variable))
638 (princ "globally void")
639 (let ((val (default-value variable)))
640 (with-current-buffer standard-output
641 (princ "global value is ")
642 (terpri)
643 ;; Fixme: pp can take an age if you happen to
644 ;; ask for a very large expression. We should
645 ;; probably print it raw once and check it's a
646 ;; sensible size before prettyprinting. -- fx
647 (let ((from (point)))
648 (pp val)
649 ;; See previous comment for this function.
650 ;; (help-xref-on-pp from (point))
651 (if (< (point) (+ from 20))
652 (delete-region (1- from) from))))))
653 (terpri))
655 ;; If the value is large, move it to the end.
656 (with-current-buffer standard-output
657 (when (> (count-lines (point-min) (point-max)) 10)
658 ;; Note that setting the syntax table like below
659 ;; makes forward-sexp move over a `'s' at the end
660 ;; of a symbol.
661 (set-syntax-table emacs-lisp-mode-syntax-table)
662 (goto-char val-start-pos)
663 ;; The line below previously read as
664 ;; (delete-region (point) (progn (end-of-line) (point)))
665 ;; which suppressed display of the buffer local value for
666 ;; large values.
667 (when (looking-at "value is") (replace-match ""))
668 (save-excursion
669 (insert "\n\nValue:")
670 (set (make-local-variable 'help-button-cache)
671 (point-marker)))
672 (insert "value is shown ")
673 (insert-button "below"
674 'action help-button-cache
675 'follow-link t
676 'help-echo "mouse-2, RET: show value")
677 (insert ".\n")))
678 (terpri)
680 (let* ((alias (condition-case nil
681 (indirect-variable variable)
682 (error variable)))
683 (obsolete (get variable 'byte-obsolete-variable))
684 (use (car obsolete))
685 (safe-var (get variable 'safe-local-variable))
686 (doc (or (documentation-property variable 'variable-documentation)
687 (documentation-property alias 'variable-documentation)))
688 (extra-line nil))
689 ;; Add a note for variables that have been make-var-buffer-local.
690 (when (and (local-variable-if-set-p variable)
691 (or (not (local-variable-p variable))
692 (with-temp-buffer
693 (local-variable-if-set-p variable))))
694 (setq extra-line t)
695 (princ " Automatically becomes buffer-local when set in any fashion.\n"))
697 ;; Mention if it's an alias
698 (unless (eq alias variable)
699 (setq extra-line t)
700 (princ (format " This variable is an alias for `%s'.\n" alias)))
702 (when obsolete
703 (setq extra-line t)
704 (princ " This variable is obsolete")
705 (if (cdr obsolete) (princ (format " since %s" (cdr obsolete))))
706 (princ (cond ((stringp use) (concat ";\n " use))
707 (use (format ";\n use `%s' instead." (car obsolete)))
708 (t ".")))
709 (terpri))
711 (when (member (cons variable val) file-local-variables-alist)
712 (setq extra-line t)
713 (if (member (cons variable val) dir-local-variables-alist)
714 (let ((file (and (buffer-file-name)
715 (not (file-remote-p (buffer-file-name)))
716 (dir-locals-find-file (buffer-file-name)))))
717 (princ " This variable is a directory local variable")
718 (when file
719 (princ (concat "\n from the file \""
720 (if (consp file)
721 (car file)
722 file)
723 "\"")))
724 (princ ".\n"))
725 (princ " This variable is a file local variable.\n")))
727 (when (memq variable ignored-local-variables)
728 (setq extra-line t)
729 (princ " This variable is ignored when used as a file local \
730 variable.\n"))
732 ;; Can be both risky and safe, eg auto-fill-function.
733 (when (risky-local-variable-p variable)
734 (setq extra-line t)
735 (princ " This variable is potentially risky when used as a \
736 file local variable.\n")
737 (when (assq variable safe-local-variable-values)
738 (princ " However, you have added it to \
739 `safe-local-variable-values'.\n")))
741 (when safe-var
742 (setq extra-line t)
743 (princ " This variable is safe as a file local variable ")
744 (princ "if its value\n satisfies the predicate ")
745 (princ (if (byte-code-function-p safe-var)
746 "which is byte-compiled expression.\n"
747 (format "`%s'.\n" safe-var))))
749 (if extra-line (terpri))
750 (princ "Documentation:\n")
751 (with-current-buffer standard-output
752 (insert (or doc "Not documented as a variable."))))
754 ;; Make a link to customize if this variable can be customized.
755 (when (custom-variable-p variable)
756 (let ((customize-label "customize"))
757 (terpri)
758 (terpri)
759 (princ (concat "You can " customize-label " this variable."))
760 (with-current-buffer standard-output
761 (save-excursion
762 (re-search-backward
763 (concat "\\(" customize-label "\\)") nil t)
764 (help-xref-button 1 'help-customize-variable variable))))
765 ;; Note variable's version or package version
766 (let ((output (describe-variable-custom-version-info variable)))
767 (when output
768 (terpri)
769 (terpri)
770 (princ output))))
772 (save-excursion
773 (set-buffer standard-output)
774 ;; Return the text we displayed.
775 (buffer-string))))))))
778 ;;;###autoload
779 (defun describe-syntax (&optional buffer)
780 "Describe the syntax specifications in the syntax table of BUFFER.
781 The descriptions are inserted in a help buffer, which is then displayed.
782 BUFFER defaults to the current buffer."
783 (interactive)
784 (setq buffer (or buffer (current-buffer)))
785 (help-setup-xref (list #'describe-syntax buffer) (interactive-p))
786 (with-help-window (help-buffer)
787 (let ((table (with-current-buffer buffer (syntax-table))))
788 (with-current-buffer standard-output
789 (describe-vector table 'internal-describe-syntax-value)
790 (while (setq table (char-table-parent table))
791 (insert "\nThe parent syntax table is:")
792 (describe-vector table 'internal-describe-syntax-value))))))
794 (defun help-describe-category-set (value)
795 (insert (cond
796 ((null value) "default")
797 ((char-table-p value) "deeper char-table ...")
798 (t (condition-case err
799 (category-set-mnemonics value)
800 (error "invalid"))))))
802 ;;;###autoload
803 (defun describe-categories (&optional buffer)
804 "Describe the category specifications in the current category table.
805 The descriptions are inserted in a buffer, which is then displayed.
806 If BUFFER is non-nil, then describe BUFFER's category table instead.
807 BUFFER should be a buffer or a buffer name."
808 (interactive)
809 (setq buffer (or buffer (current-buffer)))
810 (help-setup-xref (list #'describe-categories buffer) (interactive-p))
811 (with-help-window (help-buffer)
812 (let* ((table (with-current-buffer buffer (category-table)))
813 (docs (char-table-extra-slot table 0)))
814 (if (or (not (vectorp docs)) (/= (length docs) 95))
815 (error "Invalid first extra slot in this category table\n"))
816 (with-current-buffer standard-output
817 (insert "Legend of category mnemonics (see the tail for the longer description)\n")
818 (let ((pos (point)) (items 0) lines n)
819 (dotimes (i 95)
820 (if (aref docs i) (setq items (1+ items))))
821 (setq lines (1+ (/ (1- items) 4)))
822 (setq n 0)
823 (dotimes (i 95)
824 (let ((elt (aref docs i)))
825 (when elt
826 (string-match ".*" elt)
827 (setq elt (match-string 0 elt))
828 (if (>= (length elt) 17)
829 (setq elt (concat (substring elt 0 14) "...")))
830 (if (< (point) (point-max))
831 (move-to-column (* 20 (/ n lines)) t))
832 (insert (+ i ?\s) ?: elt)
833 (if (< (point) (point-max))
834 (forward-line 1)
835 (insert "\n"))
836 (setq n (1+ n))
837 (if (= (% n lines) 0)
838 (goto-char pos))))))
839 (goto-char (point-max))
840 (insert "\n"
841 "character(s)\tcategory mnemonics\n"
842 "------------\t------------------")
843 (describe-vector table 'help-describe-category-set)
844 (insert "Legend of category mnemonics:\n")
845 (dotimes (i 95)
846 (let ((elt (aref docs i)))
847 (when elt
848 (if (string-match "\n" elt)
849 (setq elt (substring elt (match-end 0))))
850 (insert (+ i ?\s) ": " elt "\n"))))
851 (while (setq table (char-table-parent table))
852 (insert "\nThe parent category table is:")
853 (describe-vector table 'help-describe-category-set))))))
855 (provide 'help-fns)
857 ;; arch-tag: 9e10331c-ae81-4d13-965d-c4819aaab0b3
858 ;;; help-fns.el ends here