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