Remove configure's --with-mmdf option
[emacs.git] / lisp / help-fns.el
blob7ecd271d0c8e9f4c0fe8c87c11a2494498f18b28
1 ;;; help-fns.el --- Complex help functions -*- lexical-binding: t -*-
3 ;; Copyright (C) 1985-1986, 1993-1994, 1998-2015 Free Software
4 ;; Foundation, Inc.
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: help, internal
8 ;; Package: emacs
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 (defvar help-fns-describe-function-functions nil
36 "List of functions to run in help buffer in `describe-function'.
37 Those functions will be run after the header line and argument
38 list was inserted, and before the documentation will be inserted.
39 The functions will receive the function name as argument.")
41 ;; Functions
43 ;;;###autoload
44 (defun describe-function (function)
45 "Display the full documentation of FUNCTION (a symbol)."
46 (interactive
47 (let ((fn (function-called-at-point))
48 (enable-recursive-minibuffers t)
49 val)
50 (setq val (completing-read (if fn
51 (format "Describe function (default %s): " fn)
52 "Describe function: ")
53 obarray 'fboundp t nil nil
54 (and fn (symbol-name fn))))
55 (list (if (equal val "")
56 fn (intern val)))))
57 (if (null function)
58 (message "You didn't specify a function")
59 (help-setup-xref (list #'describe-function function)
60 (called-interactively-p 'interactive))
61 (save-excursion
62 (with-help-window (help-buffer)
63 (prin1 function)
64 ;; Use " is " instead of a colon so that
65 ;; it is easier to get out the function name using forward-sexp.
66 (princ " is ")
67 (describe-function-1 function)
68 (with-current-buffer standard-output
69 ;; Return the text we displayed.
70 (buffer-string))))))
73 ;; Could be this, if we make symbol-file do the work below.
74 ;; (defun help-C-file-name (subr-or-var kind)
75 ;; "Return the name of the C file where SUBR-OR-VAR is defined.
76 ;; KIND should be `var' for a variable or `subr' for a subroutine."
77 ;; (symbol-file (if (symbolp subr-or-var) subr-or-var
78 ;; (subr-name subr-or-var))
79 ;; (if (eq kind 'var) 'defvar 'defun)))
80 ;;;###autoload
81 (defun help-C-file-name (subr-or-var kind)
82 "Return the name of the C file where SUBR-OR-VAR is defined.
83 KIND should be `var' for a variable or `subr' for a subroutine."
84 (let ((docbuf (get-buffer-create " *DOC*"))
85 (name (if (eq 'var kind)
86 (concat "V" (symbol-name subr-or-var))
87 (concat "F" (subr-name (advice--cd*r subr-or-var))))))
88 (with-current-buffer docbuf
89 (goto-char (point-min))
90 (if (eobp)
91 (insert-file-contents-literally
92 (expand-file-name internal-doc-file-name doc-directory)))
93 (let ((file (catch 'loop
94 (while t
95 (let ((pnt (search-forward (concat "\x1f" name "\n"))))
96 (re-search-backward "\x1fS\\(.*\\)")
97 (let ((file (match-string 1)))
98 (if (member file build-files)
99 (throw 'loop file)
100 (goto-char pnt))))))))
101 (if (string-match "^ns.*\\(\\.o\\|obj\\)\\'" file)
102 (setq file (replace-match ".m" t t file 1))
103 (if (string-match "\\.\\(o\\|obj\\)\\'" file)
104 (setq file (replace-match ".c" t t file))))
105 (if (string-match "\\.\\(c\\|m\\)\\'" file)
106 (concat "src/" file)
107 file)))))
109 (defcustom help-downcase-arguments nil
110 "If non-nil, argument names in *Help* buffers are downcased."
111 :type 'boolean
112 :group 'help
113 :version "23.2")
115 (defun help-highlight-arg (arg)
116 "Highlight ARG as an argument name for a *Help* buffer.
117 Return ARG in face `help-argument-name'; ARG is also downcased
118 if the variable `help-downcase-arguments' is non-nil."
119 (propertize (if help-downcase-arguments (downcase arg) arg)
120 'face 'help-argument-name))
122 (defun help-do-arg-highlight (doc args)
123 (with-syntax-table (make-syntax-table emacs-lisp-mode-syntax-table)
124 (modify-syntax-entry ?\- "w")
125 (dolist (arg args)
126 (setq doc (replace-regexp-in-string
127 ;; This is heuristic, but covers all common cases
128 ;; except ARG1-ARG2
129 (concat "\\<" ; beginning of word
130 "\\(?:[a-z-]*-\\)?" ; for xxx-ARG
131 "\\("
132 (regexp-quote arg)
133 "\\)"
134 "\\(?:es\\|s\\|th\\)?" ; for ARGth, ARGs
135 "\\(?:-[a-z0-9-]+\\)?" ; for ARG-xxx, ARG-n
136 "\\(?:-[{([<`\"].*?\\)?"; for ARG-{x}, (x), <x>, [x], `x'
137 "\\>") ; end of word
138 (help-highlight-arg arg)
139 doc t t 1)))
140 doc))
142 (defun help-highlight-arguments (usage doc &rest args)
143 (when (and usage (string-match "^(" usage))
144 (with-temp-buffer
145 (insert usage)
146 (goto-char (point-min))
147 (let ((case-fold-search nil)
148 (next (not (or args (looking-at "\\["))))
149 (opt nil))
150 ;; Make a list of all arguments
151 (skip-chars-forward "^ ")
152 (while next
153 (or opt (not (looking-at " &")) (setq opt t))
154 (if (not (re-search-forward " \\([\\[(]*\\)\\([^] &)\.]+\\)" nil t))
155 (setq next nil)
156 (setq args (cons (match-string 2) args))
157 (when (and opt (string= (match-string 1) "("))
158 ;; A pesky CL-style optional argument with default value,
159 ;; so let's skip over it
160 (search-backward "(")
161 (goto-char (scan-sexps (point) 1)))))
162 ;; Highlight arguments in the USAGE string
163 (setq usage (help-do-arg-highlight (buffer-string) args))
164 ;; Highlight arguments in the DOC string
165 (setq doc (and doc (help-do-arg-highlight doc args))))))
166 ;; Return value is like the one from help-split-fundoc, but highlighted
167 (cons usage doc))
169 ;; The following function was compiled from the former functions
170 ;; `describe-simplify-lib-file-name' and `find-source-lisp-file' with
171 ;; some excerpts from `describe-function-1' and `describe-variable'.
172 ;; The only additional twists provided are (1) locate the defining file
173 ;; for autoloaded functions, and (2) give preference to files in the
174 ;; "install directory" (directories found via `load-path') rather than
175 ;; to files in the "compile directory" (directories found by searching
176 ;; the loaddefs.el file). We autoload it because it's also used by
177 ;; `describe-face' (instead of `describe-simplify-lib-file-name').
179 ;;;###autoload
180 (defun find-lisp-object-file-name (object type)
181 "Guess the file that defined the Lisp object OBJECT, of type TYPE.
182 OBJECT should be a symbol associated with a function, variable, or face;
183 alternatively, it can be a function definition.
184 If TYPE is `defvar', search for a variable definition.
185 If TYPE is `defface', search for a face definition.
186 If TYPE is not a symbol, search for a function definition.
188 The return value is the absolute name of a readable file where OBJECT is
189 defined. If several such files exist, preference is given to a file
190 found via `load-path'. The return value can also be `C-source', which
191 means that OBJECT is a function or variable defined in C. If no
192 suitable file is found, return nil."
193 (let* ((autoloaded (autoloadp type))
194 (file-name (or (and autoloaded (nth 1 type))
195 (symbol-file
196 ;; FIXME: Why do we have this weird "If TYPE is the
197 ;; value returned by `symbol-function' for a function
198 ;; symbol" exception?
199 object (or (if (symbolp type) type) 'defun)))))
200 (cond
201 (autoloaded
202 ;; An autoloaded function: Locate the file since `symbol-function'
203 ;; has only returned a bare string here.
204 (setq file-name
205 (locate-file file-name load-path '(".el" ".elc") 'readable)))
206 ((and (stringp file-name)
207 (string-match "[.]*loaddefs.el\\'" file-name))
208 ;; An autoloaded variable or face. Visit loaddefs.el in a buffer
209 ;; and try to extract the defining file. The following form is
210 ;; from `describe-function-1' and `describe-variable'.
211 (let ((location
212 (condition-case nil
213 (find-function-search-for-symbol object nil file-name)
214 (error nil))))
215 (when (cdr location)
216 (with-current-buffer (car location)
217 (goto-char (cdr location))
218 (when (re-search-backward
219 "^;;; Generated autoloads from \\(.*\\)" nil t)
220 (setq file-name
221 (locate-file
222 (file-name-sans-extension
223 (match-string-no-properties 1))
224 load-path '(".el" ".elc") 'readable))))))))
226 (cond
227 ((and (not file-name) (subrp type))
228 ;; A built-in function. The form is from `describe-function-1'.
229 (if (get-buffer " *DOC*")
230 (help-C-file-name type 'subr)
231 'C-source))
232 ((and (not file-name) (symbolp object)
233 (integerp (get object 'variable-documentation)))
234 ;; A variable defined in C. The form is from `describe-variable'.
235 (if (get-buffer " *DOC*")
236 (help-C-file-name object 'var)
237 'C-source))
238 ((not (stringp file-name))
239 ;; If we don't have a file-name string by now, we lost.
240 nil)
241 ;; Now, `file-name' should have become an absolute file name.
242 ;; For files loaded from ~/.foo.elc, try ~/.foo.
243 ;; This applies to config files like ~/.emacs,
244 ;; which people sometimes compile.
245 ((let (fn)
246 (and (string-match "\\`\\..*\\.elc\\'"
247 (file-name-nondirectory file-name))
248 (string-equal (file-name-directory file-name)
249 (file-name-as-directory (expand-file-name "~")))
250 (file-readable-p (setq fn (file-name-sans-extension file-name)))
251 fn)))
252 ;; When the Elisp source file can be found in the install
253 ;; directory, return the name of that file.
254 ((let ((lib-name
255 (if (string-match "[.]elc\\'" file-name)
256 (substring-no-properties file-name 0 -1)
257 file-name)))
258 (or (and (file-readable-p lib-name) lib-name)
259 ;; The library might be compressed.
260 (and (file-readable-p (concat lib-name ".gz")) lib-name))))
261 ((let* ((lib-name (file-name-nondirectory file-name))
262 ;; The next form is from `describe-simplify-lib-file-name'.
263 (file-name
264 ;; Try converting the absolute file name to a library
265 ;; name, convert that back to a file name and see if we
266 ;; get the original one. If so, they are equivalent.
267 (if (equal file-name (locate-file lib-name load-path '("")))
268 (if (string-match "[.]elc\\'" lib-name)
269 (substring-no-properties lib-name 0 -1)
270 lib-name)
271 file-name))
272 ;; The next three forms are from `find-source-lisp-file'.
273 (elc-file (locate-file
274 (concat file-name
275 (if (string-match "\\.el\\'" file-name)
277 ".elc"))
278 load-path nil 'readable))
279 (str (when elc-file
280 (with-temp-buffer
281 (insert-file-contents-literally elc-file nil 0 256)
282 (buffer-string))))
283 (src-file (and str
284 (string-match ";;; from file \\(.*\\.el\\)" str)
285 (match-string 1 str))))
286 (and src-file (file-readable-p src-file) src-file))))))
288 (defun help-fns--key-bindings (function)
289 (when (commandp function)
290 (let ((pt2 (with-current-buffer standard-output (point)))
291 (remapped (command-remapping function)))
292 (unless (memq remapped '(ignore undefined))
293 (let ((keys (where-is-internal
294 (or remapped function) overriding-local-map nil nil))
295 non-modified-keys)
296 (if (and (eq function 'self-insert-command)
297 (vectorp (car-safe keys))
298 (consp (aref (car keys) 0)))
299 (princ "It is bound to many ordinary text characters.\n")
300 ;; Which non-control non-meta keys run this command?
301 (dolist (key keys)
302 (if (member (event-modifiers (aref key 0)) '(nil (shift)))
303 (push key non-modified-keys)))
304 (when remapped
305 (princ "Its keys are remapped to ")
306 (princ (if (symbolp remapped)
307 (concat "`" (symbol-name remapped) "'")
308 "an anonymous command"))
309 (princ ".\n"))
311 (when keys
312 (princ (if remapped
313 "Without this remapping, it would be bound to "
314 "It is bound to "))
315 ;; If lots of ordinary text characters run this command,
316 ;; don't mention them one by one.
317 (if (< (length non-modified-keys) 10)
318 (princ (mapconcat 'key-description keys ", "))
319 (dolist (key non-modified-keys)
320 (setq keys (delq key keys)))
321 (if keys
322 (progn
323 (princ (mapconcat 'key-description keys ", "))
324 (princ ", and many ordinary text characters"))
325 (princ "many ordinary text characters"))))
326 (when (or remapped keys non-modified-keys)
327 (princ ".")
328 (terpri)))))
330 (with-current-buffer standard-output
331 (fill-region-as-paragraph pt2 (point))
332 (unless (looking-back "\n\n")
333 (terpri))))))
335 (defun help-fns--compiler-macro (function)
336 (let ((handler (function-get function 'compiler-macro)))
337 (when handler
338 (insert "\nThis function has a compiler macro")
339 (if (symbolp handler)
340 (progn
341 (insert (format " `%s'" handler))
342 (save-excursion
343 (re-search-backward "`\\([^`']+\\)'" nil t)
344 (help-xref-button 1 'help-function handler)))
345 ;; FIXME: Obsolete since 24.4.
346 (let ((lib (get function 'compiler-macro-file)))
347 (when (stringp lib)
348 (insert (format " in `%s'" lib))
349 (save-excursion
350 (re-search-backward "`\\([^`']+\\)'" nil t)
351 (help-xref-button 1 'help-function-cmacro function lib)))))
352 (insert ".\n"))))
354 (defun help-fns--signature (function doc real-def real-function)
355 "Insert usage at point and return docstring. With highlighting."
356 (if (keymapp function)
357 doc ; If definition is a keymap, skip arglist note.
358 (let* ((advertised (gethash real-def advertised-signature-table t))
359 (arglist (if (listp advertised)
360 advertised (help-function-arglist real-def)))
361 (usage (help-split-fundoc doc function)))
362 (if usage (setq doc (cdr usage)))
363 (let* ((use (cond
364 ((and usage (not (listp advertised))) (car usage))
365 ((listp arglist)
366 (format "%S" (help-make-usage function arglist)))
367 ((stringp arglist) arglist)
368 ;; Maybe the arglist is in the docstring of a symbol
369 ;; this one is aliased to.
370 ((let ((fun real-function))
371 (while (and (symbolp fun)
372 (setq fun (symbol-function fun))
373 (not (setq usage (help-split-fundoc
374 (documentation fun)
375 function)))))
376 usage)
377 (car usage))
378 ((or (stringp real-def)
379 (vectorp real-def))
380 (format "\nMacro: %s" (format-kbd-macro real-def)))
381 (t "[Missing arglist. Please make a bug report.]")))
382 (high (help-highlight-arguments use doc)))
383 (let ((fill-begin (point)))
384 (insert (car high) "\n")
385 (fill-region fill-begin (point)))
386 (cdr high)))))
388 (defun help-fns--parent-mode (function)
389 ;; If this is a derived mode, link to the parent.
390 (let ((parent-mode (and (symbolp function)
391 (get function
392 'derived-mode-parent))))
393 (when parent-mode
394 (insert "\nParent mode: `")
395 (let ((beg (point)))
396 (insert (format "%s" parent-mode))
397 (make-text-button beg (point)
398 'type 'help-function
399 'help-args (list parent-mode)))
400 (insert "'.\n"))))
402 (defun help-fns--obsolete (function)
403 ;; Ignore lambda constructs, keyboard macros, etc.
404 (let* ((obsolete (and (symbolp function)
405 (get function 'byte-obsolete-info)))
406 (use (car obsolete)))
407 (when obsolete
408 (insert "\nThis "
409 (if (eq (car-safe (symbol-function function)) 'macro)
410 "macro"
411 "function")
412 " is obsolete")
413 (when (nth 2 obsolete)
414 (insert (format " since %s" (nth 2 obsolete))))
415 (insert (cond ((stringp use) (concat ";\n" use))
416 (use (format ";\nuse `%s' instead." use))
417 (t "."))
418 "\n"))))
420 ;; We could use `symbol-file' but this is a wee bit more efficient.
421 (defun help-fns--autoloaded-p (function file)
422 "Return non-nil if FUNCTION has previously been autoloaded.
423 FILE is the file where FUNCTION was probably defined."
424 (let* ((file (file-name-sans-extension (file-truename file)))
425 (load-hist load-history)
426 (target (cons t function))
427 found)
428 (while (and load-hist (not found))
429 (and (caar load-hist)
430 (equal (file-name-sans-extension (caar load-hist)) file)
431 (setq found (member target (cdar load-hist))))
432 (setq load-hist (cdr load-hist)))
433 found))
435 (defun help-fns--interactive-only (function)
436 "Insert some help blurb if FUNCTION should only be used interactively."
437 ;; Ignore lambda constructs, keyboard macros, etc.
438 (and (symbolp function)
439 (not (eq (car-safe (symbol-function function)) 'macro))
440 (let* ((interactive-only
441 (or (get function 'interactive-only)
442 (if (boundp 'byte-compile-interactive-only-functions)
443 (memq function
444 byte-compile-interactive-only-functions)))))
445 (when interactive-only
446 (insert "\nThis function is for interactive use only"
447 ;; Cf byte-compile-form.
448 (cond ((stringp interactive-only)
449 (format ";\nin Lisp code %s" interactive-only))
450 ((and (symbolp 'interactive-only)
451 (not (eq interactive-only t)))
452 (format ";\nin Lisp code use `%s' instead."
453 interactive-only))
454 (t "."))
455 "\n")))))
457 (defun help-fns-short-filename (filename)
458 (let* ((abbrev (abbreviate-file-name filename))
459 (short abbrev))
460 (dolist (dir load-path)
461 (let ((rel (file-relative-name filename dir)))
462 (if (< (length rel) (length short))
463 (setq short rel)))
464 (let ((rel (file-relative-name abbrev dir)))
465 (if (< (length rel) (length short))
466 (setq short rel))))
467 short))
469 ;;;###autoload
470 (defun describe-function-1 (function)
471 (let* ((advised (and (symbolp function)
472 (featurep 'nadvice)
473 (advice--p (advice--symbol-function function))))
474 ;; If the function is advised, use the symbol that has the
475 ;; real definition, if that symbol is already set up.
476 (real-function
477 (or (and advised
478 (advice--cd*r (advice--symbol-function function)))
479 function))
480 ;; Get the real definition.
481 (def (if (symbolp real-function)
482 (symbol-function real-function)
483 real-function))
484 (aliased (or (symbolp def)
485 ;; Advised & aliased function.
486 (and advised (symbolp real-function))))
487 (real-def (cond
488 (aliased (let ((f real-function))
489 (while (and (fboundp f)
490 (symbolp (symbol-function f)))
491 (setq f (symbol-function f)))
493 ((subrp def) (intern (subr-name def)))
494 (t def)))
495 (file-name (find-lisp-object-file-name function def))
496 (pt1 (with-current-buffer (help-buffer) (point)))
497 (beg (if (and (or (byte-code-function-p def)
498 (keymapp def)
499 (memq (car-safe def) '(macro lambda closure)))
500 (stringp file-name)
501 (help-fns--autoloaded-p function file-name))
502 (if (commandp def)
503 "an interactive autoloaded "
504 "an autoloaded ")
505 (if (commandp def) "an interactive " "a "))))
507 ;; Print what kind of function-like object FUNCTION is.
508 (princ (cond ((or (stringp def) (vectorp def))
509 "a keyboard macro")
510 ((subrp def)
511 (if (eq 'unevalled (cdr (subr-arity def)))
512 (concat beg "special form")
513 (concat beg "built-in function")))
514 ;; Aliases are Lisp functions, so we need to check
515 ;; aliases before functions.
516 (aliased
517 (format "an alias for `%s'" real-def))
518 ((autoloadp def)
519 (format "%s autoloaded %s"
520 (if (commandp def) "an interactive" "an")
521 (if (eq (nth 4 def) 'keymap) "keymap"
522 (if (nth 4 def) "Lisp macro" "Lisp function"))))
523 ((or (eq (car-safe def) 'macro)
524 ;; For advised macros, def is a lambda
525 ;; expression or a byte-code-function-p, so we
526 ;; need to check macros before functions.
527 (macrop function))
528 (concat beg "Lisp macro"))
529 ((byte-code-function-p def)
530 (concat beg "compiled Lisp function"))
531 ((eq (car-safe def) 'lambda)
532 (concat beg "Lisp function"))
533 ((eq (car-safe def) 'closure)
534 (concat beg "Lisp closure"))
535 ((keymapp def)
536 (let ((is-full nil)
537 (elts (cdr-safe def)))
538 (while elts
539 (if (char-table-p (car-safe elts))
540 (setq is-full t
541 elts nil))
542 (setq elts (cdr-safe elts)))
543 (concat beg (if is-full "keymap" "sparse keymap"))))
544 (t "")))
546 (if (and aliased (not (fboundp real-def)))
547 (princ ",\nwhich is not defined. Please make a bug report.")
548 (with-current-buffer standard-output
549 (save-excursion
550 (save-match-data
551 (when (re-search-backward "alias for `\\([^`']+\\)'" nil t)
552 (help-xref-button 1 'help-function real-def)))))
554 (when file-name
555 (princ " in `")
556 ;; We used to add .el to the file name,
557 ;; but that's completely wrong when the user used load-file.
558 (princ (if (eq file-name 'C-source)
559 "C source code"
560 (help-fns-short-filename file-name)))
561 (princ "'")
562 ;; Make a hyperlink to the library.
563 (with-current-buffer standard-output
564 (save-excursion
565 (re-search-backward "`\\([^`']+\\)'" nil t)
566 (help-xref-button 1 'help-function-def function file-name))))
567 (princ ".")
568 (with-current-buffer (help-buffer)
569 (fill-region-as-paragraph (save-excursion (goto-char pt1) (forward-line 0) (point))
570 (point)))
571 (terpri)(terpri)
573 (let* ((doc-raw (documentation function t))
574 ;; If the function is autoloaded, and its docstring has
575 ;; key substitution constructs, load the library.
576 (doc (progn
577 (and (autoloadp real-def) doc-raw
578 help-enable-auto-load
579 (string-match "\\([^\\]=\\|[^=]\\|\\`\\)\\\\[[{<]"
580 doc-raw)
581 (autoload-do-load real-def))
582 (substitute-command-keys doc-raw))))
584 (help-fns--key-bindings function)
585 (with-current-buffer standard-output
586 (setq doc (help-fns--signature function doc real-def real-function))
587 (run-hook-with-args 'help-fns-describe-function-functions function)
588 (insert "\n"
589 (or doc "Not documented.")))))))
591 ;; Add defaults to `help-fns-describe-function-functions'.
592 (add-hook 'help-fns-describe-function-functions #'help-fns--obsolete)
593 (add-hook 'help-fns-describe-function-functions #'help-fns--interactive-only)
594 (add-hook 'help-fns-describe-function-functions #'help-fns--parent-mode)
595 (add-hook 'help-fns-describe-function-functions #'help-fns--compiler-macro)
598 ;; Variables
600 ;;;###autoload
601 (defun variable-at-point (&optional any-symbol)
602 "Return the bound variable symbol found at or before point.
603 Return 0 if there is no such symbol.
604 If ANY-SYMBOL is non-nil, don't insist the symbol be bound."
605 (with-syntax-table emacs-lisp-mode-syntax-table
606 (or (condition-case ()
607 (save-excursion
608 (skip-chars-forward "'")
609 (or (not (zerop (skip-syntax-backward "_w")))
610 (eq (char-syntax (following-char)) ?w)
611 (eq (char-syntax (following-char)) ?_)
612 (forward-sexp -1))
613 (skip-chars-forward "'")
614 (let ((obj (read (current-buffer))))
615 (and (symbolp obj) (boundp obj) obj)))
616 (error nil))
617 (let* ((str (find-tag-default))
618 (sym (if str (intern-soft str))))
619 (if (and sym (or any-symbol (boundp sym)))
621 (save-match-data
622 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
623 (setq sym (intern-soft (match-string 1 str)))
624 (and (or any-symbol (boundp sym)) sym)))))
625 0)))
627 (defun describe-variable-custom-version-info (variable)
628 (let ((custom-version (get variable 'custom-version))
629 (cpv (get variable 'custom-package-version))
630 (output nil))
631 (if custom-version
632 (setq output
633 (format "This variable was introduced, or its default value was changed, in\nversion %s of Emacs.\n"
634 custom-version))
635 (when cpv
636 (let* ((package (car-safe cpv))
637 (version (if (listp (cdr-safe cpv))
638 (car (cdr-safe cpv))
639 (cdr-safe cpv)))
640 (pkg-versions (assq package customize-package-emacs-version-alist))
641 (emacsv (cdr (assoc version pkg-versions))))
642 (if (and package version)
643 (setq output
644 (format (concat "This variable was introduced, or its default value was changed, in\nversion %s of the %s package"
645 (if emacsv
646 (format " that is part of Emacs %s" emacsv))
647 ".\n")
648 version package))))))
649 output))
651 ;;;###autoload
652 (defun describe-variable (variable &optional buffer frame)
653 "Display the full documentation of VARIABLE (a symbol).
654 Returns the documentation as a string, also.
655 If VARIABLE has a buffer-local value in BUFFER or FRAME
656 \(default to the current buffer and current frame),
657 it is displayed along with the global value."
658 (interactive
659 (let ((v (variable-at-point))
660 (enable-recursive-minibuffers t)
661 val)
662 (setq val (completing-read (if (symbolp v)
663 (format
664 "Describe variable (default %s): " v)
665 "Describe variable: ")
666 obarray
667 (lambda (vv)
668 (or (get vv 'variable-documentation)
669 (and (boundp vv) (not (keywordp vv)))))
670 t nil nil
671 (if (symbolp v) (symbol-name v))))
672 (list (if (equal val "")
673 v (intern val)))))
674 (let (file-name)
675 (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
676 (unless (frame-live-p frame) (setq frame (selected-frame)))
677 (if (not (symbolp variable))
678 (message "You did not specify a variable")
679 (save-excursion
680 (let ((valvoid (not (with-current-buffer buffer (boundp variable))))
681 (permanent-local (get variable 'permanent-local))
682 val val-start-pos locus)
683 ;; Extract the value before setting up the output buffer,
684 ;; in case `buffer' *is* the output buffer.
685 (unless valvoid
686 (with-selected-frame frame
687 (with-current-buffer buffer
688 (setq val (symbol-value variable)
689 locus (variable-binding-locus variable)))))
690 (help-setup-xref (list #'describe-variable variable buffer)
691 (called-interactively-p 'interactive))
692 (with-help-window (help-buffer)
693 (with-current-buffer buffer
694 (prin1 variable)
695 (setq file-name (find-lisp-object-file-name variable 'defvar))
697 (if file-name
698 (progn
699 (princ " is a variable defined in `")
700 (princ (if (eq file-name 'C-source)
701 "C source code"
702 (file-name-nondirectory file-name)))
703 (princ "'.\n")
704 (with-current-buffer standard-output
705 (save-excursion
706 (re-search-backward "`\\([^`']+\\)'" nil t)
707 (help-xref-button 1 'help-variable-def
708 variable file-name)))
709 (if valvoid
710 (princ "It is void as a variable.")
711 (princ "Its ")))
712 (if valvoid
713 (princ " is void as a variable.")
714 (princ "'s "))))
715 (unless valvoid
716 (with-current-buffer standard-output
717 (setq val-start-pos (point))
718 (princ "value is ")
719 (let ((from (point))
720 (line-beg (line-beginning-position))
721 (print-rep
722 (let ((print-quoted t))
723 (prin1-to-string val))))
724 (if (< (+ (length print-rep) (point) (- line-beg)) 68)
725 (insert print-rep)
726 (terpri)
727 (pp val)
728 (if (< (point) (+ 68 (line-beginning-position 0)))
729 (delete-region from (1+ from))
730 (delete-region (1- from) from)))
731 (let* ((sv (get variable 'standard-value))
732 (origval (and (consp sv)
733 (condition-case nil
734 (eval (car sv))
735 (error :help-eval-error)))))
736 (when (and (consp sv)
737 (not (equal origval val))
738 (not (equal origval :help-eval-error)))
739 (princ "\nOriginal value was \n")
740 (setq from (point))
741 (pp origval)
742 (if (< (point) (+ from 20))
743 (delete-region (1- from) from)))))))
744 (terpri)
745 (when locus
746 (cond
747 ((bufferp locus)
748 (princ (format "Local in buffer %s; "
749 (buffer-name buffer))))
750 ((framep locus)
751 (princ (format "It is a frame-local variable; ")))
752 ((terminal-live-p locus)
753 (princ (format "It is a terminal-local variable; ")))
755 (princ (format "It is local to %S" locus))))
756 (if (not (default-boundp variable))
757 (princ "globally void")
758 (let ((global-val (default-value variable)))
759 (with-current-buffer standard-output
760 (princ "global value is ")
761 (if (eq val global-val)
762 (princ "the same.")
763 (terpri)
764 ;; Fixme: pp can take an age if you happen to
765 ;; ask for a very large expression. We should
766 ;; probably print it raw once and check it's a
767 ;; sensible size before prettyprinting. -- fx
768 (let ((from (point)))
769 (pp global-val)
770 ;; See previous comment for this function.
771 ;; (help-xref-on-pp from (point))
772 (if (< (point) (+ from 20))
773 (delete-region (1- from) from)))))))
774 (terpri))
776 ;; If the value is large, move it to the end.
777 (with-current-buffer standard-output
778 (when (> (count-lines (point-min) (point-max)) 10)
779 ;; Note that setting the syntax table like below
780 ;; makes forward-sexp move over a `'s' at the end
781 ;; of a symbol.
782 (set-syntax-table emacs-lisp-mode-syntax-table)
783 (goto-char val-start-pos)
784 ;; The line below previously read as
785 ;; (delete-region (point) (progn (end-of-line) (point)))
786 ;; which suppressed display of the buffer local value for
787 ;; large values.
788 (when (looking-at "value is") (replace-match ""))
789 (save-excursion
790 (insert "\n\nValue:")
791 (set (make-local-variable 'help-button-cache)
792 (point-marker)))
793 (insert "value is shown ")
794 (insert-button "below"
795 'action help-button-cache
796 'follow-link t
797 'help-echo "mouse-2, RET: show value")
798 (insert ".\n")))
799 (terpri)
801 (let* ((alias (condition-case nil
802 (indirect-variable variable)
803 (error variable)))
804 (obsolete (get variable 'byte-obsolete-variable))
805 (use (car obsolete))
806 (safe-var (get variable 'safe-local-variable))
807 (doc (or (documentation-property
808 variable 'variable-documentation)
809 (documentation-property
810 alias 'variable-documentation)))
811 (extra-line nil))
813 ;; Mention if it's a local variable.
814 (cond
815 ((and (local-variable-if-set-p variable)
816 (or (not (local-variable-p variable))
817 (with-temp-buffer
818 (local-variable-if-set-p variable))))
819 (setq extra-line t)
820 (princ " Automatically becomes ")
821 (if permanent-local
822 (princ "permanently "))
823 (princ "buffer-local when set.\n"))
824 ((not permanent-local))
825 ((bufferp locus)
826 (setq extra-line t)
827 (princ " This variable's buffer-local value is permanent.\n"))
829 (setq extra-line t)
830 (princ " This variable's value is permanent \
831 if it is given a local binding.\n")))
833 ;; Mention if it's an alias.
834 (unless (eq alias variable)
835 (setq extra-line t)
836 (princ (format " This variable is an alias for `%s'.\n" alias)))
838 (when obsolete
839 (setq extra-line t)
840 (princ " This variable is obsolete")
841 (if (nth 2 obsolete)
842 (princ (format " since %s" (nth 2 obsolete))))
843 (princ (cond ((stringp use) (concat ";\n " use))
844 (use (format ";\n use `%s' instead." (car obsolete)))
845 (t ".")))
846 (terpri))
848 (when (member (cons variable val)
849 (with-current-buffer buffer
850 file-local-variables-alist))
851 (setq extra-line t)
852 (if (member (cons variable val)
853 (with-current-buffer buffer
854 dir-local-variables-alist))
855 (let ((file (and (buffer-file-name buffer)
856 (not (file-remote-p
857 (buffer-file-name buffer)))
858 (dir-locals-find-file
859 (buffer-file-name buffer))))
860 (dir-file t))
861 (princ " This variable's value is directory-local")
862 (if (null file)
863 (princ ".\n")
864 (princ ", set ")
865 (if (consp file) ; result from cache
866 ;; If the cache element has an mtime, we
867 ;; assume it came from a file.
868 (if (nth 2 file)
869 (setq file (expand-file-name
870 dir-locals-file (car file)))
871 ;; Otherwise, assume it was set directly.
872 (setq file (car file)
873 dir-file nil)))
874 (princ (if dir-file
875 "by the file\n `"
876 "for the directory\n `"))
877 (with-current-buffer standard-output
878 (insert-text-button
879 file 'type 'help-dir-local-var-def
880 'help-args (list variable file)))
881 (princ "'.\n")))
882 (princ " This variable's value is file-local.\n")))
884 (when (memq variable ignored-local-variables)
885 (setq extra-line t)
886 (princ " This variable is ignored as a file-local \
887 variable.\n"))
889 ;; Can be both risky and safe, eg auto-fill-function.
890 (when (risky-local-variable-p variable)
891 (setq extra-line t)
892 (princ " This variable may be risky if used as a \
893 file-local variable.\n")
894 (when (assq variable safe-local-variable-values)
895 (princ " However, you have added it to \
896 `safe-local-variable-values'.\n")))
898 (when safe-var
899 (setq extra-line t)
900 (princ " This variable is safe as a file local variable ")
901 (princ "if its value\n satisfies the predicate ")
902 (princ (if (byte-code-function-p safe-var)
903 "which is a byte-compiled expression.\n"
904 (format "`%s'.\n" safe-var))))
906 (if extra-line (terpri))
907 (princ "Documentation:\n")
908 (with-current-buffer standard-output
909 (insert (or doc "Not documented as a variable."))))
911 ;; Make a link to customize if this variable can be customized.
912 (when (custom-variable-p variable)
913 (let ((customize-label "customize"))
914 (terpri)
915 (terpri)
916 (princ (concat "You can " customize-label " this variable."))
917 (with-current-buffer standard-output
918 (save-excursion
919 (re-search-backward
920 (concat "\\(" customize-label "\\)") nil t)
921 (help-xref-button 1 'help-customize-variable variable))))
922 ;; Note variable's version or package version
923 (let ((output (describe-variable-custom-version-info variable)))
924 (when output
925 (terpri)
926 (terpri)
927 (princ output))))
929 (with-current-buffer standard-output
930 ;; Return the text we displayed.
931 (buffer-string))))))))
934 ;;;###autoload
935 (defun describe-function-or-variable (symbol &optional buffer frame)
936 "Display the full documentation of the function or variable SYMBOL.
937 If SYMBOL is a variable and has a buffer-local value in BUFFER or FRAME
938 \(default to the current buffer and current frame), it is displayed along
939 with the global value."
940 (interactive
941 (let* ((v-or-f (variable-at-point))
942 (found (symbolp v-or-f))
943 (v-or-f (if found v-or-f (function-called-at-point)))
944 (found (or found v-or-f))
945 (enable-recursive-minibuffers t)
946 val)
947 (setq val (completing-read (if found
948 (format
949 "Describe function or variable (default %s): " v-or-f)
950 "Describe function or variable: ")
951 obarray
952 (lambda (vv)
953 (or (fboundp vv)
954 (get vv 'variable-documentation)
955 (and (boundp vv) (not (keywordp vv)))))
956 t nil nil
957 (if found (symbol-name v-or-f))))
958 (list (if (equal val "")
959 v-or-f (intern val)))))
960 (if (not (symbolp symbol)) (message "You didn't specify a function or variable")
961 (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
962 (unless (frame-live-p frame) (setq frame (selected-frame)))
963 (help-xref-interned symbol buffer frame)))
965 ;;;###autoload
966 (defun describe-syntax (&optional buffer)
967 "Describe the syntax specifications in the syntax table of BUFFER.
968 The descriptions are inserted in a help buffer, which is then displayed.
969 BUFFER defaults to the current buffer."
970 (interactive)
971 (setq buffer (or buffer (current-buffer)))
972 (help-setup-xref (list #'describe-syntax buffer)
973 (called-interactively-p 'interactive))
974 (with-help-window (help-buffer)
975 (let ((table (with-current-buffer buffer (syntax-table))))
976 (with-current-buffer standard-output
977 (describe-vector table 'internal-describe-syntax-value)
978 (while (setq table (char-table-parent table))
979 (insert "\nThe parent syntax table is:")
980 (describe-vector table 'internal-describe-syntax-value))))))
982 (defun help-describe-category-set (value)
983 (insert (cond
984 ((null value) "default")
985 ((char-table-p value) "deeper char-table ...")
986 (t (condition-case nil
987 (category-set-mnemonics value)
988 (error "invalid"))))))
990 ;;;###autoload
991 (defun describe-categories (&optional buffer)
992 "Describe the category specifications in the current category table.
993 The descriptions are inserted in a buffer, which is then displayed.
994 If BUFFER is non-nil, then describe BUFFER's category table instead.
995 BUFFER should be a buffer or a buffer name."
996 (interactive)
997 (setq buffer (or buffer (current-buffer)))
998 (help-setup-xref (list #'describe-categories buffer)
999 (called-interactively-p 'interactive))
1000 (with-help-window (help-buffer)
1001 (let* ((table (with-current-buffer buffer (category-table)))
1002 (docs (char-table-extra-slot table 0)))
1003 (if (or (not (vectorp docs)) (/= (length docs) 95))
1004 (error "Invalid first extra slot in this category table\n"))
1005 (with-current-buffer standard-output
1006 (insert "Legend of category mnemonics (see the tail for the longer description)\n")
1007 (let ((pos (point)) (items 0) lines n)
1008 (dotimes (i 95)
1009 (if (aref docs i) (setq items (1+ items))))
1010 (setq lines (1+ (/ (1- items) 4)))
1011 (setq n 0)
1012 (dotimes (i 95)
1013 (let ((elt (aref docs i)))
1014 (when elt
1015 (string-match ".*" elt)
1016 (setq elt (match-string 0 elt))
1017 (if (>= (length elt) 17)
1018 (setq elt (concat (substring elt 0 14) "...")))
1019 (if (< (point) (point-max))
1020 (move-to-column (* 20 (/ n lines)) t))
1021 (insert (+ i ?\s) ?: elt)
1022 (if (< (point) (point-max))
1023 (forward-line 1)
1024 (insert "\n"))
1025 (setq n (1+ n))
1026 (if (= (% n lines) 0)
1027 (goto-char pos))))))
1028 (goto-char (point-max))
1029 (insert "\n"
1030 "character(s)\tcategory mnemonics\n"
1031 "------------\t------------------")
1032 (describe-vector table 'help-describe-category-set)
1033 (insert "Legend of category mnemonics:\n")
1034 (dotimes (i 95)
1035 (let ((elt (aref docs i)))
1036 (when elt
1037 (if (string-match "\n" elt)
1038 (setq elt (substring elt (match-end 0))))
1039 (insert (+ i ?\s) ": " elt "\n"))))
1040 (while (setq table (char-table-parent table))
1041 (insert "\nThe parent category table is:")
1042 (describe-vector table 'help-describe-category-set))))))
1045 ;;; Replacements for old lib-src/ programs. Don't seem especially useful.
1047 ;; Replaces lib-src/digest-doc.c.
1048 ;;;###autoload
1049 (defun doc-file-to-man (file)
1050 "Produce an nroff buffer containing the doc-strings from the DOC file."
1051 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1052 internal-doc-file-name t)))
1053 (or (file-readable-p file)
1054 (error "Cannot read file `%s'" file))
1055 (pop-to-buffer (generate-new-buffer "*man-doc*"))
1056 (setq buffer-undo-list t)
1057 (insert ".TH \"Command Summary for GNU Emacs\"\n"
1058 ".AU Richard M. Stallman\n")
1059 (insert-file-contents file)
1060 (let (notfirst)
1061 (while (search-forward "\x1f" nil 'move)
1062 (if (looking-at "S")
1063 (delete-region (1- (point)) (line-end-position))
1064 (delete-char -1)
1065 (if notfirst
1066 (insert "\n.DE\n")
1067 (setq notfirst t))
1068 (insert "\n.SH ")
1069 (insert (if (looking-at "F") "Function " "Variable "))
1070 (delete-char 1)
1071 (forward-line 1)
1072 (insert ".DS L\n"))))
1073 (insert "\n.DE\n")
1074 (setq buffer-undo-list nil)
1075 (nroff-mode))
1077 ;; Replaces lib-src/sorted-doc.c.
1078 ;;;###autoload
1079 (defun doc-file-to-info (file)
1080 "Produce a texinfo buffer with sorted doc-strings from the DOC file."
1081 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1082 internal-doc-file-name t)))
1083 (or (file-readable-p file)
1084 (error "Cannot read file `%s'" file))
1085 (let ((i 0) type name doc alist)
1086 (with-temp-buffer
1087 (insert-file-contents file)
1088 ;; The characters "@{}" need special treatment.
1089 (while (re-search-forward "[@{}]" nil t)
1090 (backward-char)
1091 (insert "@")
1092 (forward-char 1))
1093 (goto-char (point-min))
1094 (while (search-forward "\x1f" nil t)
1095 (unless (looking-at "S")
1096 (setq type (char-after)
1097 name (buffer-substring (1+ (point)) (line-end-position))
1098 doc (buffer-substring (line-beginning-position 2)
1099 (if (search-forward "\x1f" nil 'move)
1100 (1- (point))
1101 (point)))
1102 alist (cons (list name type doc) alist))
1103 (backward-char 1))))
1104 (pop-to-buffer (generate-new-buffer "*info-doc*"))
1105 (setq buffer-undo-list t)
1106 ;; Write the output header.
1107 (insert "\\input texinfo @c -*-texinfo-*-\n"
1108 "@setfilename emacsdoc.info\n"
1109 "@settitle Command Summary for GNU Emacs\n"
1110 "@finalout\n"
1111 "\n@node Top\n"
1112 "@unnumbered Command Summary for GNU Emacs\n\n"
1113 "@table @asis\n\n"
1114 "@iftex\n"
1115 "@global@let@ITEM@item\n"
1116 "@def@item{@filbreak@vskip5pt@ITEM}\n"
1117 "@font@tensy cmsy10 scaled @magstephalf\n"
1118 "@font@teni cmmi10 scaled @magstephalf\n"
1119 "@def\\{{@tensy@char110}}\n" ; this backslash goes with cmr10
1120 "@def|{{@tensy@char106}}\n"
1121 "@def@{{{@tensy@char102}}\n"
1122 "@def@}{{@tensy@char103}}\n"
1123 "@def<{{@teni@char62}}\n"
1124 "@def>{{@teni@char60}}\n"
1125 "@chardef@@64\n"
1126 "@catcode43=12\n"
1127 "@tableindent-0.2in\n"
1128 "@end iftex\n")
1129 ;; Sort the array by name; within each name, by type (functions first).
1130 (setq alist (sort alist (lambda (e1 e2)
1131 (if (string-equal (car e1) (car e2))
1132 (<= (cadr e1) (cadr e2))
1133 (string-lessp (car e1) (car e2))))))
1134 ;; Print each function.
1135 (dolist (e alist)
1136 (insert "\n@item "
1137 (if (char-equal (cadr e) ?\F) "Function" "Variable")
1138 " @code{" (car e) "}\n@display\n"
1139 (nth 2 e)
1140 "\n@end display\n")
1141 ;; Try to avoid a save size overflow in the TeX output routine.
1142 (if (zerop (setq i (% (1+ i) 100)))
1143 (insert "\n@end table\n@table @asis\n")))
1144 (insert "@end table\n"
1145 "@bye\n")
1146 (setq buffer-undo-list nil)
1147 (texinfo-mode)))
1149 (provide 'help-fns)
1151 ;;; help-fns.el ends here