1 ;;; help-fns.el --- Complex help functions -*- lexical-binding: t -*-
3 ;; Copyright (C) 1985-1986, 1993-1994, 1998-2011
4 ;; Free Software Foundation, Inc.
7 ;; 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/>.
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
38 (defun describe-function (function)
39 "Display the full documentation of FUNCTION (a symbol)."
41 (let ((fn (function-called-at-point))
42 (enable-recursive-minibuffers t
)
44 (setq val
(completing-read (if fn
45 (format "Describe function (default %s): " fn
)
46 "Describe function: ")
47 obarray
'fboundp t nil nil
48 (and fn
(symbol-name fn
))))
49 (list (if (equal val
"")
52 (message "You didn't specify a function")
53 (help-setup-xref (list #'describe-function function
)
54 (called-interactively-p 'interactive
))
56 (with-help-window (help-buffer)
58 ;; Use " is " instead of a colon so that
59 ;; it is easier to get out the function name using forward-sexp.
61 (describe-function-1 function
)
62 (with-current-buffer standard-output
63 ;; Return the text we displayed.
66 (defun help-split-fundoc (docstring def
)
67 "Split a function DOCSTRING into the actual doc and the usage info.
68 Return (USAGE . DOC) or nil if there's no usage info.
69 DEF is the function whose usage we're looking for in DOCSTRING."
70 ;; Functions can get the calling sequence at the end of the doc string.
71 ;; In cases where `function' has been fset to a subr we can't search for
72 ;; function's name in the doc string so we use `fn' as the anonymous
73 ;; function name instead.
74 (when (and docstring
(string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring
))
76 ;; Replace `fn' with the actual function name.
77 (if (consp def
) "anonymous" def
)
78 (match-string 1 docstring
))
79 (unless (zerop (match-beginning 0))
80 (substring docstring
0 (match-beginning 0))))))
82 ;; FIXME: Move to subr.el?
83 (defun help-add-fundoc-usage (docstring arglist
)
84 "Add the usage info to DOCSTRING.
85 If DOCSTRING already has a usage info, then just return it unchanged.
86 The usage info is built from ARGLIST. DOCSTRING can be nil.
87 ARGLIST can also be t or a string of the form \"(FUN ARG1 ARG2 ...)\"."
88 (unless (stringp docstring
) (setq docstring
""))
89 (if (or (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring
)
93 (if (string-match "\n?\n\\'" docstring
)
94 (if (< (- (match-end 0) (match-beginning 0)) 2) "\n" "")
96 (if (and (stringp arglist
)
97 (string-match "\\`([^ ]+\\(.*\\))\\'" arglist
))
98 (concat "(fn" (match-string 1 arglist
) ")")
99 (format "%S" (help-make-usage 'fn arglist
))))))
101 ;; FIXME: Move to subr.el?
102 (defun help-function-arglist (def &optional preserve-names
)
103 "Return a formal argument list for the function DEF.
104 IF PRESERVE-NAMES is non-nil, return a formal arglist that uses
105 the same names as used in the original source code, when possible."
106 ;; Handle symbols aliased to other symbols.
107 (if (and (symbolp def
) (fboundp def
)) (setq def
(indirect-function def
)))
108 ;; If definition is a macro, find the function inside it.
109 (if (eq (car-safe def
) 'macro
) (setq def
(cdr def
)))
111 ((and (byte-code-function-p def
) (listp (aref def
0))) (aref def
0))
112 ((eq (car-safe def
) 'lambda
) (nth 1 def
))
113 ((eq (car-safe def
) 'closure
) (nth 2 def
))
114 ((or (and (byte-code-function-p def
) (integerp (aref def
0)))
116 (or (when preserve-names
117 (let* ((doc (condition-case nil
(documentation def
) (error nil
)))
118 (docargs (if doc
(car (help-split-fundoc doc nil
))))
120 (cdar (read-from-string (downcase docargs
)))))
123 (dolist (arg arglist
)
124 (unless (and (symbolp arg
)
125 (let ((name (symbol-name arg
)))
126 (if (eq (aref name
0) ?
&)
127 (memq arg
'(&rest
&optional
))
128 (not (string-match "\\." name
)))))
130 (when valid arglist
)))
131 (let* ((args-desc (if (not (subrp def
))
133 (let ((a (subr-arity def
)))
135 (if (numberp (cdr a
))
138 (max (lsh args-desc -
8))
139 (min (logand args-desc
127))
140 (rest (logand args-desc
128))
143 (push (intern (concat "arg" (number-to-string (1+ i
)))) arglist
))
145 (push '&optional arglist
)
146 (dotimes (i (- max min
))
147 (push (intern (concat "arg" (number-to-string (+ 1 i min
))))
149 (unless (zerop rest
) (push '&rest arglist
) (push 'rest arglist
))
150 (nreverse arglist
))))
151 ((and (eq (car-safe def
) 'autoload
) (not (eq (nth 4 def
) 'keymap
)))
152 "[Arg list not available until function definition is loaded.]")
155 ;; FIXME: Move to subr.el?
156 (defun help-make-usage (function arglist
)
157 (cons (if (symbolp function
) function
'anonymous
)
158 (mapcar (lambda (arg)
159 (if (not (symbolp arg
))
160 (if (and (consp arg
) (symbolp (car arg
)))
161 ;; CL style default values for optional args.
162 (cons (intern (upcase (symbol-name (car arg
))))
165 (let ((name (symbol-name arg
)))
167 ((string-match "\\`&" name
) arg
)
168 ((string-match "\\`_" name
)
169 (intern (upcase (substring name
1))))
170 (t (intern (upcase name
)))))))
173 ;; Could be this, if we make symbol-file do the work below.
174 ;; (defun help-C-file-name (subr-or-var kind)
175 ;; "Return the name of the C file where SUBR-OR-VAR is defined.
176 ;; KIND should be `var' for a variable or `subr' for a subroutine."
177 ;; (symbol-file (if (symbolp subr-or-var) subr-or-var
178 ;; (subr-name subr-or-var))
179 ;; (if (eq kind 'var) 'defvar 'defun)))
181 (defun help-C-file-name (subr-or-var kind
)
182 "Return the name of the C file where SUBR-OR-VAR is defined.
183 KIND should be `var' for a variable or `subr' for a subroutine."
184 (let ((docbuf (get-buffer-create " *DOC*"))
185 (name (if (eq 'var kind
)
186 (concat "V" (symbol-name subr-or-var
))
187 (concat "F" (subr-name subr-or-var
)))))
188 (with-current-buffer docbuf
189 (goto-char (point-min))
191 (insert-file-contents-literally
192 (expand-file-name internal-doc-file-name doc-directory
)))
193 (let ((file (catch 'loop
195 (let ((pnt (search-forward (concat "\x1f" name
"\n"))))
196 (re-search-backward "\x1fS\\(.*\\)")
197 (let ((file (match-string 1)))
198 (if (member file build-files
)
200 (goto-char pnt
))))))))
201 (if (string-match "^ns.*\\(\\.o\\|obj\\)\\'" file
)
202 (setq file
(replace-match ".m" t t file
1))
203 (if (string-match "\\.\\(o\\|obj\\)\\'" file
)
204 (setq file
(replace-match ".c" t t file
))))
205 (if (string-match "\\.\\(c\\|m\\)\\'" file
)
209 (defcustom help-downcase-arguments nil
210 "If non-nil, argument names in *Help* buffers are downcased."
215 (defun help-highlight-arg (arg)
216 "Highlight ARG as an argument name for a *Help* buffer.
217 Return ARG in face `help-argument-name'; ARG is also downcased
218 if the variable `help-downcase-arguments' is non-nil."
219 (propertize (if help-downcase-arguments
(downcase arg
) arg
)
220 'face
'help-argument-name
))
222 (defun help-do-arg-highlight (doc args
)
223 (with-syntax-table (make-syntax-table emacs-lisp-mode-syntax-table
)
224 (modify-syntax-entry ?\-
"w")
225 (dolist (arg args doc
)
226 (setq doc
(replace-regexp-in-string
227 ;; This is heuristic, but covers all common cases
229 (concat "\\<" ; beginning of word
230 "\\(?:[a-z-]*-\\)?" ; for xxx-ARG
234 "\\(?:es\\|s\\|th\\)?" ; for ARGth, ARGs
235 "\\(?:-[a-z0-9-]+\\)?" ; for ARG-xxx, ARG-n
236 "\\(?:-[{([<`\"].*?\\)?"; for ARG-{x}, (x), <x>, [x], `x'
238 (help-highlight-arg arg
)
241 (defun help-highlight-arguments (usage doc
&rest args
)
242 (when (and usage
(string-match "^(" usage
))
245 (goto-char (point-min))
246 (let ((case-fold-search nil
)
247 (next (not (or args
(looking-at "\\["))))
249 ;; Make a list of all arguments
250 (skip-chars-forward "^ ")
252 (or opt
(not (looking-at " &")) (setq opt t
))
253 (if (not (re-search-forward " \\([\\[(]*\\)\\([^] &)\.]+\\)" nil t
))
255 (setq args
(cons (match-string 2) args
))
256 (when (and opt
(string= (match-string 1) "("))
257 ;; A pesky CL-style optional argument with default value,
258 ;; so let's skip over it
259 (search-backward "(")
260 (goto-char (scan-sexps (point) 1)))))
261 ;; Highlight aguments in the USAGE string
262 (setq usage
(help-do-arg-highlight (buffer-string) args
))
263 ;; Highlight arguments in the DOC string
264 (setq doc
(and doc
(help-do-arg-highlight doc args
))))))
265 ;; Return value is like the one from help-split-fundoc, but highlighted
268 ;; The following function was compiled from the former functions
269 ;; `describe-simplify-lib-file-name' and `find-source-lisp-file' with
270 ;; some excerpts from `describe-function-1' and `describe-variable'.
271 ;; The only additional twists provided are (1) locate the defining file
272 ;; for autoloaded functions, and (2) give preference to files in the
273 ;; "install directory" (directories found via `load-path') rather than
274 ;; to files in the "compile directory" (directories found by searching
275 ;; the loaddefs.el file). We autoload it because it's also used by
276 ;; `describe-face' (instead of `describe-simplify-lib-file-name').
279 (defun find-lisp-object-file-name (object type
)
280 "Guess the file that defined the Lisp object OBJECT, of type TYPE.
281 OBJECT should be a symbol associated with a function, variable, or face;
282 alternatively, it can be a function definition.
283 If TYPE is `defvar', search for a variable definition.
284 If TYPE is `defface', search for a face definition.
285 If TYPE is the value returned by `symbol-function' for a function symbol,
286 search for a function definition.
288 The return value is the absolute name of a readable file where OBJECT is
289 defined. If several such files exist, preference is given to a file
290 found via `load-path'. The return value can also be `C-source', which
291 means that OBJECT is a function or variable defined in C. If no
292 suitable file is found, return nil."
293 (let* ((autoloaded (eq (car-safe type
) 'autoload
))
294 (file-name (or (and autoloaded
(nth 1 type
))
296 object
(if (memq type
(list 'defvar
'defface
))
301 ;; An autoloaded function: Locate the file since `symbol-function'
302 ;; has only returned a bare string here.
304 (locate-file file-name load-path
'(".el" ".elc") 'readable
)))
305 ((and (stringp file-name
)
306 (string-match "[.]*loaddefs.el\\'" file-name
))
307 ;; An autoloaded variable or face. Visit loaddefs.el in a buffer
308 ;; and try to extract the defining file. The following form is
309 ;; from `describe-function-1' and `describe-variable'.
312 (find-function-search-for-symbol object nil file-name
)
315 (with-current-buffer (car location
)
316 (goto-char (cdr location
))
317 (when (re-search-backward
318 "^;;; Generated autoloads from \\(.*\\)" nil t
)
321 (file-name-sans-extension
322 (match-string-no-properties 1))
323 load-path
'(".el" ".elc") 'readable
))))))))
326 ((and (not file-name
) (subrp type
))
327 ;; A built-in function. The form is from `describe-function-1'.
328 (if (get-buffer " *DOC*")
329 (help-C-file-name type
'subr
)
331 ((and (not file-name
) (symbolp object
)
332 (integerp (get object
'variable-documentation
)))
333 ;; A variable defined in C. The form is from `describe-variable'.
334 (if (get-buffer " *DOC*")
335 (help-C-file-name object
'var
)
337 ((not (stringp file-name
))
338 ;; If we don't have a file-name string by now, we lost.
340 ;; Now, `file-name' should have become an absolute file name.
341 ;; For files loaded from ~/.emacs.elc, try ~/.emacs.
343 (and (string-equal file-name
344 (expand-file-name ".emacs.elc" "~"))
345 (file-readable-p (setq fn
(expand-file-name ".emacs" "~")))
347 ;; When the Elisp source file can be found in the install
348 ;; directory, return the name of that file.
350 (if (string-match "[.]elc\\'" file-name
)
351 (substring-no-properties file-name
0 -
1)
353 (or (and (file-readable-p lib-name
) lib-name
)
354 ;; The library might be compressed.
355 (and (file-readable-p (concat lib-name
".gz")) lib-name
))))
356 ((let* ((lib-name (file-name-nondirectory file-name
))
357 ;; The next form is from `describe-simplify-lib-file-name'.
359 ;; Try converting the absolute file name to a library
360 ;; name, convert that back to a file name and see if we
361 ;; get the original one. If so, they are equivalent.
362 (if (equal file-name
(locate-file lib-name load-path
'("")))
363 (if (string-match "[.]elc\\'" lib-name
)
364 (substring-no-properties lib-name
0 -
1)
367 ;; The next three forms are from `find-source-lisp-file'.
368 (elc-file (locate-file
370 (if (string-match "\\.el\\'" file-name
)
373 load-path nil
'readable
))
376 (insert-file-contents-literally elc-file nil
0 256)
379 (string-match ";;; from file \\(.*\\.el\\)" str
)
380 (match-string 1 str
))))
381 (and src-file
(file-readable-p src-file
) src-file
))))))
383 (declare-function ad-get-advice-info
"advice" (function))
386 (defun describe-function-1 (function)
387 (let* ((advised (and (symbolp function
) (featurep 'advice
)
388 (ad-get-advice-info function
)))
389 ;; If the function is advised, use the symbol that has the
390 ;; real definition, if that symbol is already set up.
393 (let ((origname (cdr (assq 'origname advised
))))
394 (and (fboundp origname
) origname
)))
396 ;; Get the real definition.
397 (def (if (symbolp real-function
)
398 (symbol-function real-function
)
401 (beg (if (commandp def
) "an interactive " "a "))
402 (pt1 (with-current-buffer (help-buffer) (point)))
405 (cond ((or (stringp def
) (vectorp def
))
408 (if (eq 'unevalled
(cdr (subr-arity def
)))
409 (concat beg
"special form")
410 (concat beg
"built-in function")))
411 ((byte-code-function-p def
)
412 (concat beg
"compiled Lisp function"))
414 (while (and (fboundp def
)
415 (symbolp (symbol-function def
)))
416 (setq def
(symbol-function def
)))
417 ;; Handle (defalias 'foo 'bar), where bar is undefined.
418 (or (fboundp def
) (setq errtype
'alias
))
419 (format "an alias for `%s'" def
))
420 ((eq (car-safe def
) 'lambda
)
421 (concat beg
"Lisp function"))
422 ((eq (car-safe def
) 'macro
)
424 ((eq (car-safe def
) 'closure
)
425 (concat beg
"Lisp closure"))
426 ((eq (car-safe def
) 'autoload
)
427 (format "%s autoloaded %s"
428 (if (commandp def
) "an interactive" "an")
429 (if (eq (nth 4 def
) 'keymap
) "keymap"
430 (if (nth 4 def
) "Lisp macro" "Lisp function"))))
433 (elts (cdr-safe def
)))
435 (if (char-table-p (car-safe elts
))
438 (setq elts
(cdr-safe elts
)))
444 (if (eq errtype
'alias
)
445 (princ ",\nwhich is not defined. Please make a bug report.")
446 (with-current-buffer standard-output
449 (when (re-search-backward "alias for `\\([^`']+\\)'" nil t
)
450 (help-xref-button 1 'help-function def
)))))
452 (setq file-name
(find-lisp-object-file-name function def
))
455 ;; We used to add .el to the file name,
456 ;; but that's completely wrong when the user used load-file.
457 (princ (if (eq file-name
'C-source
)
459 (file-name-nondirectory file-name
)))
461 ;; Make a hyperlink to the library.
462 (with-current-buffer standard-output
464 (re-search-backward "`\\([^`']+\\)'" nil t
)
465 (help-xref-button 1 'help-function-def function file-name
))))
467 (with-current-buffer (help-buffer)
468 (fill-region-as-paragraph (save-excursion (goto-char pt1
) (forward-line 0) (point))
471 (when (commandp function
)
472 (let ((pt2 (with-current-buffer (help-buffer) (point)))
473 (remapped (command-remapping function
)))
474 (unless (memq remapped
'(ignore undefined
))
475 (let ((keys (where-is-internal
476 (or remapped function
) overriding-local-map nil nil
))
478 (if (and (eq function
'self-insert-command
)
479 (vectorp (car-safe keys
))
480 (consp (aref (car keys
) 0)))
481 (princ "It is bound to many ordinary text characters.\n")
482 ;; Which non-control non-meta keys run this command?
484 (if (member (event-modifiers (aref key
0)) '(nil (shift)))
485 (push key non-modified-keys
)))
487 (princ "It is remapped to `")
488 (princ (symbol-name remapped
))
492 (princ (if remapped
", which is bound to " "It is bound to "))
493 ;; If lots of ordinary text characters run this command,
494 ;; don't mention them one by one.
495 (if (< (length non-modified-keys
) 10)
496 (princ (mapconcat 'key-description keys
", "))
497 (dolist (key non-modified-keys
)
498 (setq keys
(delq key keys
)))
501 (princ (mapconcat 'key-description keys
", "))
502 (princ ", and many ordinary text characters"))
503 (princ "many ordinary text characters"))))
504 (when (or remapped keys non-modified-keys
)
508 (with-current-buffer (help-buffer)
509 (fill-region-as-paragraph pt2
(point))
510 (unless (looking-back "\n\n")
512 ;; Note that list* etc do not get this property until
513 ;; cl-hack-byte-compiler runs, after bytecomp is loaded.
514 (when (and (symbolp function
)
515 (eq (get function
'byte-compile
)
516 'cl-byte-compile-compiler-macro
))
517 (princ "This function has a compiler macro")
518 (let ((lib (get function
'compiler-macro-file
)))
520 (princ (format " in `%s'" lib
))
521 (with-current-buffer standard-output
523 (re-search-backward "`\\([^`']+\\)'" nil t
)
524 (help-xref-button 1 'help-function-cmacro function lib
)))))
526 (let* ((advertised (gethash def advertised-signature-table t
))
527 (arglist (if (listp advertised
)
528 advertised
(help-function-arglist def
)))
529 (doc (condition-case err
(documentation function
)
530 (error (format "No Doc! %S" err
))))
531 (usage (help-split-fundoc doc function
)))
532 (with-current-buffer standard-output
533 ;; If definition is a keymap, skip arglist note.
534 (unless (keymapp function
)
535 (if usage
(setq doc
(cdr usage
)))
537 ((and usage
(not (listp advertised
))) (car usage
))
539 (format "%S" (help-make-usage function arglist
)))
540 ((stringp arglist
) arglist
)
541 ;; Maybe the arglist is in the docstring of a symbol
542 ;; this one is aliased to.
543 ((let ((fun real-function
))
544 (while (and (symbolp fun
)
545 (setq fun
(symbol-function fun
))
546 (not (setq usage
(help-split-fundoc
553 (format "\nMacro: %s" (format-kbd-macro def
)))
554 (t "[Missing arglist. Please make a bug report.]")))
555 (high (help-highlight-arguments use doc
)))
556 (let ((fill-begin (point)))
557 (insert (car high
) "\n")
558 (fill-region fill-begin
(point)))
559 (setq doc
(cdr high
))))
560 (let* ((obsolete (and
561 ;; function might be a lambda construct.
563 (get function
'byte-obsolete-info
)))
564 (use (car obsolete
)))
566 (princ "\nThis function is obsolete")
567 (when (nth 2 obsolete
)
568 (insert (format " since %s" (nth 2 obsolete
))))
569 (insert (cond ((stringp use
) (concat ";\n" use
))
570 (use (format ";\nuse `%s' instead." use
))
574 (or doc
"Not documented."))))))))
580 (defun variable-at-point (&optional any-symbol
)
581 "Return the bound variable symbol found at or before point.
582 Return 0 if there is no such symbol.
583 If ANY-SYMBOL is non-nil, don't insist the symbol be bound."
584 (with-syntax-table emacs-lisp-mode-syntax-table
585 (or (condition-case ()
587 (skip-chars-forward "'")
588 (or (not (zerop (skip-syntax-backward "_w")))
589 (eq (char-syntax (following-char)) ?w
)
590 (eq (char-syntax (following-char)) ?_
)
592 (skip-chars-forward "'")
593 (let ((obj (read (current-buffer))))
594 (and (symbolp obj
) (boundp obj
) obj
)))
596 (let* ((str (find-tag-default))
597 (sym (if str
(intern-soft str
))))
598 (if (and sym
(or any-symbol
(boundp sym
)))
601 (when (and str
(string-match "\\`\\W*\\(.*?\\)\\W*\\'" str
))
602 (setq sym
(intern-soft (match-string 1 str
)))
603 (and (or any-symbol
(boundp sym
)) sym
)))))
606 (defun describe-variable-custom-version-info (variable)
607 (let ((custom-version (get variable
'custom-version
))
608 (cpv (get variable
'custom-package-version
))
612 (format "This variable was introduced, or its default value was changed, in\nversion %s of Emacs.\n"
615 (let* ((package (car-safe cpv
))
616 (version (if (listp (cdr-safe cpv
))
619 (pkg-versions (assq package customize-package-emacs-version-alist
))
620 (emacsv (cdr (assoc version pkg-versions
))))
621 (if (and package version
)
623 (format (concat "This variable was introduced, or its default value was changed, in\nversion %s of the %s package"
625 (format " that is part of Emacs %s" emacsv
))
627 version package
))))))
631 (defun describe-variable (variable &optional buffer frame
)
632 "Display the full documentation of VARIABLE (a symbol).
633 Returns the documentation as a string, also.
634 If VARIABLE has a buffer-local value in BUFFER or FRAME
635 \(default to the current buffer and current frame),
636 it is displayed along with the global value."
638 (let ((v (variable-at-point))
639 (enable-recursive-minibuffers t
)
641 (setq val
(completing-read (if (symbolp v
)
643 "Describe variable (default %s): " v
)
644 "Describe variable: ")
647 (or (get vv
'variable-documentation
)
648 (and (boundp vv
) (not (keywordp vv
)))))
650 (if (symbolp v
) (symbol-name v
))))
651 (list (if (equal val
"")
654 (unless (buffer-live-p buffer
) (setq buffer
(current-buffer)))
655 (unless (frame-live-p frame
) (setq frame
(selected-frame)))
656 (if (not (symbolp variable
))
657 (message "You did not specify a variable")
659 (let ((valvoid (not (with-current-buffer buffer
(boundp variable
))))
660 val val-start-pos locus
)
661 ;; Extract the value before setting up the output buffer,
662 ;; in case `buffer' *is* the output buffer.
664 (with-selected-frame frame
665 (with-current-buffer buffer
666 (setq val
(symbol-value variable
)
667 locus
(variable-binding-locus variable
)))))
668 (help-setup-xref (list #'describe-variable variable buffer
)
669 (called-interactively-p 'interactive
))
670 (with-help-window (help-buffer)
671 (with-current-buffer buffer
673 (setq file-name
(find-lisp-object-file-name variable
'defvar
))
677 (princ " is a variable defined in `")
678 (princ (if (eq file-name
'C-source
)
680 (file-name-nondirectory file-name
)))
682 (with-current-buffer standard-output
684 (re-search-backward "`\\([^`']+\\)'" nil t
)
685 (help-xref-button 1 'help-variable-def
686 variable file-name
)))
688 (princ "It is void as a variable.")
691 (princ " is void as a variable.")
694 (with-current-buffer standard-output
695 (setq val-start-pos
(point))
697 (let ((from (point)))
700 (if (< (point) (+ 68 (line-beginning-position 0)))
701 (delete-region from
(1+ from
))
702 (delete-region (1- from
) from
))
703 (let* ((sv (get variable
'standard-value
))
704 (origval (and (consp sv
)
707 (error :help-eval-error
)))))
708 (when (and (consp sv
)
709 (not (equal origval val
))
710 (not (equal origval
:help-eval-error
)))
711 (princ "\nOriginal value was \n")
714 (if (< (point) (+ from
20))
715 (delete-region (1- from
) from
)))))))
719 (princ (format "%socal in buffer %s; "
720 (if (get variable
'permanent-local
)
723 (princ (format "It is a frame-local variable; ")))
724 (if (not (default-boundp variable
))
725 (princ "globally void")
726 (let ((val (default-value variable
)))
727 (with-current-buffer standard-output
728 (princ "global value is ")
730 ;; Fixme: pp can take an age if you happen to
731 ;; ask for a very large expression. We should
732 ;; probably print it raw once and check it's a
733 ;; sensible size before prettyprinting. -- fx
734 (let ((from (point)))
736 ;; See previous comment for this function.
737 ;; (help-xref-on-pp from (point))
738 (if (< (point) (+ from
20))
739 (delete-region (1- from
) from
))))))
742 ;; If the value is large, move it to the end.
743 (with-current-buffer standard-output
744 (when (> (count-lines (point-min) (point-max)) 10)
745 ;; Note that setting the syntax table like below
746 ;; makes forward-sexp move over a `'s' at the end
748 (set-syntax-table emacs-lisp-mode-syntax-table
)
749 (goto-char val-start-pos
)
750 ;; The line below previously read as
751 ;; (delete-region (point) (progn (end-of-line) (point)))
752 ;; which suppressed display of the buffer local value for
754 (when (looking-at "value is") (replace-match ""))
756 (insert "\n\nValue:")
757 (set (make-local-variable 'help-button-cache
)
759 (insert "value is shown ")
760 (insert-button "below"
761 'action help-button-cache
763 'help-echo
"mouse-2, RET: show value")
767 (let* ((alias (condition-case nil
768 (indirect-variable variable
)
770 (obsolete (get variable
'byte-obsolete-variable
))
772 (safe-var (get variable
'safe-local-variable
))
773 (doc (or (documentation-property variable
'variable-documentation
)
774 (documentation-property alias
'variable-documentation
)))
776 ;; Add a note for variables that have been make-var-buffer-local.
777 (when (and (local-variable-if-set-p variable
)
778 (or (not (local-variable-p variable
))
780 (local-variable-if-set-p variable
))))
782 (princ " Automatically becomes buffer-local when set in any fashion.\n"))
784 ;; Mention if it's an alias
785 (unless (eq alias variable
)
787 (princ (format " This variable is an alias for `%s'.\n" alias
)))
791 (princ " This variable is obsolete")
792 (if (cdr obsolete
) (princ (format " since %s" (cdr obsolete
))))
793 (princ (cond ((stringp use
) (concat ";\n " use
))
794 (use (format ";\n use `%s' instead." (car obsolete
)))
798 (when (member (cons variable val
) file-local-variables-alist
)
800 (if (member (cons variable val
) dir-local-variables-alist
)
801 (let ((file (and (buffer-file-name)
802 (not (file-remote-p (buffer-file-name)))
803 (dir-locals-find-file
804 (buffer-file-name))))
806 (princ " This variable is a directory local variable")
808 (if (consp file
) ; result from cache
809 ;; If the cache element has an mtime, we
810 ;; assume it came from a file.
812 (setq file
(expand-file-name
813 dir-locals-file
(car file
)))
814 ;; Otherwise, assume it was set directly.
815 (setq type
"directory")))
816 (princ (format "\n from the %s \"%s\"" type file
)))
818 (princ " This variable is a file local variable.\n")))
820 (when (memq variable ignored-local-variables
)
822 (princ " This variable is ignored when used as a file local \
825 ;; Can be both risky and safe, eg auto-fill-function.
826 (when (risky-local-variable-p variable
)
828 (princ " This variable is potentially risky when used as a \
829 file local variable.\n")
830 (when (assq variable safe-local-variable-values
)
831 (princ " However, you have added it to \
832 `safe-local-variable-values'.\n")))
836 (princ " This variable is safe as a file local variable ")
837 (princ "if its value\n satisfies the predicate ")
838 (princ (if (byte-code-function-p safe-var
)
839 "which is byte-compiled expression.\n"
840 (format "`%s'.\n" safe-var
))))
842 (if extra-line
(terpri))
843 (princ "Documentation:\n")
844 (with-current-buffer standard-output
845 (insert (or doc
"Not documented as a variable."))))
847 ;; Make a link to customize if this variable can be customized.
848 (when (custom-variable-p variable
)
849 (let ((customize-label "customize"))
852 (princ (concat "You can " customize-label
" this variable."))
853 (with-current-buffer standard-output
856 (concat "\\(" customize-label
"\\)") nil t
)
857 (help-xref-button 1 'help-customize-variable variable
))))
858 ;; Note variable's version or package version
859 (let ((output (describe-variable-custom-version-info variable
)))
865 (with-current-buffer standard-output
866 ;; Return the text we displayed.
867 (buffer-string))))))))
871 (defun describe-syntax (&optional buffer
)
872 "Describe the syntax specifications in the syntax table of BUFFER.
873 The descriptions are inserted in a help buffer, which is then displayed.
874 BUFFER defaults to the current buffer."
876 (setq buffer
(or buffer
(current-buffer)))
877 (help-setup-xref (list #'describe-syntax buffer
)
878 (called-interactively-p 'interactive
))
879 (with-help-window (help-buffer)
880 (let ((table (with-current-buffer buffer
(syntax-table))))
881 (with-current-buffer standard-output
882 (describe-vector table
'internal-describe-syntax-value
)
883 (while (setq table
(char-table-parent table
))
884 (insert "\nThe parent syntax table is:")
885 (describe-vector table
'internal-describe-syntax-value
))))))
887 (defun help-describe-category-set (value)
889 ((null value
) "default")
890 ((char-table-p value
) "deeper char-table ...")
891 (t (condition-case nil
892 (category-set-mnemonics value
)
893 (error "invalid"))))))
896 (defun describe-categories (&optional buffer
)
897 "Describe the category specifications in the current category table.
898 The descriptions are inserted in a buffer, which is then displayed.
899 If BUFFER is non-nil, then describe BUFFER's category table instead.
900 BUFFER should be a buffer or a buffer name."
902 (setq buffer
(or buffer
(current-buffer)))
903 (help-setup-xref (list #'describe-categories buffer
)
904 (called-interactively-p 'interactive
))
905 (with-help-window (help-buffer)
906 (let* ((table (with-current-buffer buffer
(category-table)))
907 (docs (char-table-extra-slot table
0)))
908 (if (or (not (vectorp docs
)) (/= (length docs
) 95))
909 (error "Invalid first extra slot in this category table\n"))
910 (with-current-buffer standard-output
911 (insert "Legend of category mnemonics (see the tail for the longer description)\n")
912 (let ((pos (point)) (items 0) lines n
)
914 (if (aref docs i
) (setq items
(1+ items
))))
915 (setq lines
(1+ (/ (1- items
) 4)))
918 (let ((elt (aref docs i
)))
920 (string-match ".*" elt
)
921 (setq elt
(match-string 0 elt
))
922 (if (>= (length elt
) 17)
923 (setq elt
(concat (substring elt
0 14) "...")))
924 (if (< (point) (point-max))
925 (move-to-column (* 20 (/ n lines
)) t
))
926 (insert (+ i ?\s
) ?
: elt
)
927 (if (< (point) (point-max))
931 (if (= (% n lines
) 0)
933 (goto-char (point-max))
935 "character(s)\tcategory mnemonics\n"
936 "------------\t------------------")
937 (describe-vector table
'help-describe-category-set
)
938 (insert "Legend of category mnemonics:\n")
940 (let ((elt (aref docs i
)))
942 (if (string-match "\n" elt
)
943 (setq elt
(substring elt
(match-end 0))))
944 (insert (+ i ?\s
) ": " elt
"\n"))))
945 (while (setq table
(char-table-parent table
))
946 (insert "\nThe parent category table is:")
947 (describe-vector table
'help-describe-category-set
))))))
950 ;;; Replacements for old lib-src/ programs. Don't seem especially useful.
952 ;; Replaces lib-src/digest-doc.c.
954 (defun doc-file-to-man (file)
955 "Produce an nroff buffer containing the doc-strings from the DOC file."
956 (interactive (list (read-file-name "Name of DOC file: " doc-directory
957 internal-doc-file-name t
)))
958 (or (file-readable-p file
)
959 (error "Cannot read file `%s'" file
))
960 (pop-to-buffer (generate-new-buffer "*man-doc*"))
961 (setq buffer-undo-list t
)
962 (insert ".TH \"Command Summary for GNU Emacs\"\n"
963 ".AU Richard M. Stallman\n")
964 (insert-file-contents file
)
966 (while (search-forward "\x1f" nil
'move
)
968 (delete-region (1- (point)) (line-end-position))
974 (insert (if (looking-at "F") "Function " "Variable "))
977 (insert ".DS L\n"))))
979 (setq buffer-undo-list nil
)
982 ;; Replaces lib-src/sorted-doc.c.
984 (defun doc-file-to-info (file)
985 "Produce a texinfo buffer with sorted doc-strings from the DOC file."
986 (interactive (list (read-file-name "Name of DOC file: " doc-directory
987 internal-doc-file-name t
)))
988 (or (file-readable-p file
)
989 (error "Cannot read file `%s'" file
))
990 (let ((i 0) type name doc alist
)
992 (insert-file-contents file
)
993 ;; The characters "@{}" need special treatment.
994 (while (re-search-forward "[@{}]" nil t
)
998 (goto-char (point-min))
999 (while (search-forward "\x1f" nil t
)
1000 (unless (looking-at "S")
1001 (setq type
(char-after)
1002 name
(buffer-substring (1+ (point)) (line-end-position))
1003 doc
(buffer-substring (line-beginning-position 2)
1004 (if (search-forward "\x1f" nil
'move
)
1007 alist
(cons (list name type doc
) alist
))
1008 (backward-char 1))))
1009 (pop-to-buffer (generate-new-buffer "*info-doc*"))
1010 (setq buffer-undo-list t
)
1011 ;; Write the output header.
1012 (insert "\\input texinfo @c -*-texinfo-*-\n"
1013 "@setfilename emacsdoc.info\n"
1014 "@settitle Command Summary for GNU Emacs\n"
1017 "@unnumbered Command Summary for GNU Emacs\n\n"
1020 "@global@let@ITEM@item\n"
1021 "@def@item{@filbreak@vskip5pt@ITEM}\n"
1022 "@font@tensy cmsy10 scaled @magstephalf\n"
1023 "@font@teni cmmi10 scaled @magstephalf\n"
1024 "@def\\{{@tensy@char110}}\n" ; this backslash goes with cmr10
1025 "@def|{{@tensy@char106}}\n"
1026 "@def@{{{@tensy@char102}}\n"
1027 "@def@}{{@tensy@char103}}\n"
1028 "@def<{{@teni@char62}}\n"
1029 "@def>{{@teni@char60}}\n"
1032 "@tableindent-0.2in\n"
1034 ;; Sort the array by name; within each name, by type (functions first).
1035 (setq alist
(sort alist
(lambda (e1 e2
)
1036 (if (string-equal (car e1
) (car e2
))
1037 (<= (cadr e1
) (cadr e2
))
1038 (string-lessp (car e1
) (car e2
))))))
1039 ;; Print each function.
1042 (if (char-equal (cadr e
) ?\F
) "Function" "Variable")
1043 " @code{" (car e
) "}\n@display\n"
1046 ;; Try to avoid a save size overflow in the TeX output routine.
1047 (if (zerop (setq i
(%
(1+ i
) 100)))
1048 (insert "\n@end table\n@table @asis\n")))
1049 (insert "@end table\n"
1051 (setq buffer-undo-list nil
)
1056 ;;; help-fns.el ends here