]> code.delx.au - gnu-emacs/blob - lisp/help-fns.el
* lisp/progmodes/perl-mode.el: Refine handling of /re/ and y/abc/def/
[gnu-emacs] / lisp / help-fns.el
1 ;;; help-fns.el --- Complex help functions -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 1985-1986, 1993-1994, 1998-2015 Free Software
4 ;; Foundation, Inc.
5
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: help, internal
8 ;; Package: emacs
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; This file contains those help commands which are complicated, and
28 ;; which may not be used in every session. For example
29 ;; `describe-function' will probably be heavily used when doing elisp
30 ;; programming, but not if just editing C files. Simpler help commands
31 ;; are in help.el
32
33 ;;; Code:
34
35 (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.")
40
41 ;; Functions
42
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)))))
73
74
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 "\1f" name "\n"))))
98 (re-search-backward "\1fS\\(.*\\)")
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)))))
110
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")
116
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))
123
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', ‘x’
139 "\\>") ; end of word
140 (help-highlight-arg arg)
141 doc t t 1)))
142 doc))
143
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))
170
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').
180
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.
189
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))))))))
227
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)
278 "c"
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))))))
289
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"))
312
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)))))
331
332 (with-current-buffer standard-output
333 (fill-region-as-paragraph pt2 (point))
334 (unless (looking-back "\n\n" (- (point) 2))
335 (terpri))))))
336
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"))))
355
356 (defun help-fns--signature (function doc real-def real-function raw)
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 (help--make-usage-docstring 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"
383 (help--docstring-quote
384 (format-kbd-macro real-def))))
385 (t "[Missing arglist. Please make a bug report.]")))
386 ;; Insert "`X", not "(\` X)", when documenting `X.
387 (use1 (replace-regexp-in-string
388 "\\`(\\\\=\\\\\\\\=` \\([^\n ]*\\))\\'"
389 "\\\\=`\\1" use t))
390 (high (if raw
391 (cons use1 doc)
392 (help-highlight-arguments (substitute-command-keys use1)
393 (substitute-command-keys doc)))))
394 (let ((fill-begin (point))
395 (high-usage (car high))
396 (high-doc (cdr high)))
397 (insert high-usage "\n")
398 (fill-region fill-begin (point))
399 high-doc)))))
400
401 (defun help-fns--parent-mode (function)
402 ;; If this is a derived mode, link to the parent.
403 (let ((parent-mode (and (symbolp function)
404 (get function
405 'derived-mode-parent))))
406 (when parent-mode
407 (insert "\nParent mode: ‘")
408 (let ((beg (point)))
409 (insert (format "%s" parent-mode))
410 (make-text-button beg (point)
411 'type 'help-function
412 'help-args (list parent-mode)))
413 (insert "’.\n"))))
414
415 (defun help-fns--obsolete (function)
416 ;; Ignore lambda constructs, keyboard macros, etc.
417 (let* ((obsolete (and (symbolp function)
418 (get function 'byte-obsolete-info)))
419 (use (car obsolete)))
420 (when obsolete
421 (insert "\nThis "
422 (if (eq (car-safe (symbol-function function)) 'macro)
423 "macro"
424 "function")
425 " is obsolete")
426 (when (nth 2 obsolete)
427 (insert (format " since %s" (nth 2 obsolete))))
428 (insert (cond ((stringp use) (concat ";\n" use))
429 (use (format ";\nuse ‘%s’ instead." use))
430 (t "."))
431 "\n"))))
432
433 ;; We could use `symbol-file' but this is a wee bit more efficient.
434 (defun help-fns--autoloaded-p (function file)
435 "Return non-nil if FUNCTION has previously been autoloaded.
436 FILE is the file where FUNCTION was probably defined."
437 (let* ((file (file-name-sans-extension (file-truename file)))
438 (load-hist load-history)
439 (target (cons t function))
440 found)
441 (while (and load-hist (not found))
442 (and (caar load-hist)
443 (equal (file-name-sans-extension (caar load-hist)) file)
444 (setq found (member target (cdar load-hist))))
445 (setq load-hist (cdr load-hist)))
446 found))
447
448 (defun help-fns--interactive-only (function)
449 "Insert some help blurb if FUNCTION should only be used interactively."
450 ;; Ignore lambda constructs, keyboard macros, etc.
451 (and (symbolp function)
452 (not (eq (car-safe (symbol-function function)) 'macro))
453 (let* ((interactive-only
454 (or (get function 'interactive-only)
455 (if (boundp 'byte-compile-interactive-only-functions)
456 (memq function
457 byte-compile-interactive-only-functions)))))
458 (when interactive-only
459 (insert "\nThis function is for interactive use only"
460 ;; Cf byte-compile-form.
461 (cond ((stringp interactive-only)
462 (format ";\nin Lisp code %s" interactive-only))
463 ((and (symbolp 'interactive-only)
464 (not (eq interactive-only t)))
465 (format ";\nin Lisp code use ‘%s’ instead."
466 interactive-only))
467 (t "."))
468 "\n")))))
469
470 (defun help-fns-short-filename (filename)
471 (let* ((abbrev (abbreviate-file-name filename))
472 (short abbrev))
473 (dolist (dir load-path)
474 (let ((rel (file-relative-name filename dir)))
475 (if (< (length rel) (length short))
476 (setq short rel)))
477 (let ((rel (file-relative-name abbrev dir)))
478 (if (< (length rel) (length short))
479 (setq short rel))))
480 short))
481
482 ;;;###autoload
483 (defun describe-function-1 (function)
484 (let* ((advised (and (symbolp function)
485 (featurep 'nadvice)
486 (advice--p (advice--symbol-function function))))
487 ;; If the function is advised, use the symbol that has the
488 ;; real definition, if that symbol is already set up.
489 (real-function
490 (or (and advised
491 (advice--cd*r (advice--symbol-function function)))
492 function))
493 ;; Get the real definition.
494 (def (if (symbolp real-function)
495 (or (symbol-function real-function)
496 (signal 'void-function (list real-function)))
497 real-function))
498 (aliased (or (symbolp def)
499 ;; Advised & aliased function.
500 (and advised (symbolp real-function))))
501 (real-def (cond
502 (aliased (let ((f real-function))
503 (while (and (fboundp f)
504 (symbolp (symbol-function f)))
505 (setq f (symbol-function f)))
506 f))
507 ((subrp def) (intern (subr-name def)))
508 (t def)))
509 (sig-key (if (subrp def)
510 (indirect-function real-def)
511 real-def))
512 (file-name (find-lisp-object-file-name function def))
513 (pt1 (with-current-buffer (help-buffer) (point)))
514 (beg (if (and (or (byte-code-function-p def)
515 (keymapp def)
516 (memq (car-safe def) '(macro lambda closure)))
517 (stringp file-name)
518 (help-fns--autoloaded-p function file-name))
519 (if (commandp def)
520 "an interactive autoloaded "
521 "an autoloaded ")
522 (if (commandp def) "an interactive " "a "))))
523
524 ;; Print what kind of function-like object FUNCTION is.
525 (princ (cond ((or (stringp def) (vectorp def))
526 "a keyboard macro")
527 ((subrp def)
528 (if (eq 'unevalled (cdr (subr-arity def)))
529 (concat beg "special form")
530 (concat beg "built-in function")))
531 ;; Aliases are Lisp functions, so we need to check
532 ;; aliases before functions.
533 (aliased
534 (format "an alias for ‘%s’" real-def))
535 ((autoloadp def)
536 (format "%s autoloaded %s"
537 (if (commandp def) "an interactive" "an")
538 (if (eq (nth 4 def) 'keymap) "keymap"
539 (if (nth 4 def) "Lisp macro" "Lisp function"))))
540 ((or (eq (car-safe def) 'macro)
541 ;; For advised macros, def is a lambda
542 ;; expression or a byte-code-function-p, so we
543 ;; need to check macros before functions.
544 (macrop function))
545 (concat beg "Lisp macro"))
546 ((byte-code-function-p def)
547 (concat beg "compiled Lisp function"))
548 ((eq (car-safe def) 'lambda)
549 (concat beg "Lisp function"))
550 ((eq (car-safe def) 'closure)
551 (concat beg "Lisp closure"))
552 ((keymapp def)
553 (let ((is-full nil)
554 (elts (cdr-safe def)))
555 (while elts
556 (if (char-table-p (car-safe elts))
557 (setq is-full t
558 elts nil))
559 (setq elts (cdr-safe elts)))
560 (concat beg (if is-full "keymap" "sparse keymap"))))
561 (t "")))
562
563 (if (and aliased (not (fboundp real-def)))
564 (princ ",\nwhich is not defined. Please make a bug report.")
565 (with-current-buffer standard-output
566 (save-excursion
567 (save-match-data
568 (when (re-search-backward "alias for ‘\\([^‘’]+\\)’" nil t)
569 (help-xref-button 1 'help-function real-def)))))
570
571 (when file-name
572 (princ " in ‘")
573 ;; We used to add .el to the file name,
574 ;; but that's completely wrong when the user used load-file.
575 (princ (if (eq file-name 'C-source)
576 "C source code"
577 (help-fns-short-filename file-name)))
578 (princ "’")
579 ;; Make a hyperlink to the library.
580 (with-current-buffer standard-output
581 (save-excursion
582 (re-search-backward "‘\\([^‘’]+\\)’" nil t)
583 (help-xref-button 1 'help-function-def function file-name))))
584 (princ ".")
585 (with-current-buffer (help-buffer)
586 (fill-region-as-paragraph (save-excursion (goto-char pt1) (forward-line 0) (point))
587 (point)))
588 (terpri)(terpri)
589
590 (let ((doc-raw (documentation function t)))
591
592 ;; If the function is autoloaded, and its docstring has
593 ;; key substitution constructs, load the library.
594 (and (autoloadp real-def) doc-raw
595 help-enable-auto-load
596 (string-match "\\([^\\]=\\|[^=]\\|\\`\\)\\\\[[{<]" doc-raw)
597 (autoload-do-load real-def))
598
599 (help-fns--key-bindings function)
600 (with-current-buffer standard-output
601 (let ((doc (help-fns--signature function doc-raw sig-key
602 real-function nil)))
603 (run-hook-with-args 'help-fns-describe-function-functions function)
604 (insert "\n"
605 (or doc "Not documented."))))))))
606
607 ;; Add defaults to `help-fns-describe-function-functions'.
608 (add-hook 'help-fns-describe-function-functions #'help-fns--obsolete)
609 (add-hook 'help-fns-describe-function-functions #'help-fns--interactive-only)
610 (add-hook 'help-fns-describe-function-functions #'help-fns--parent-mode)
611 (add-hook 'help-fns-describe-function-functions #'help-fns--compiler-macro)
612
613 \f
614 ;; Variables
615
616 ;;;###autoload
617 (defun variable-at-point (&optional any-symbol)
618 "Return the bound variable symbol found at or before point.
619 Return 0 if there is no such symbol.
620 If ANY-SYMBOL is non-nil, don't insist the symbol be bound."
621 (with-syntax-table emacs-lisp-mode-syntax-table
622 (or (condition-case ()
623 (save-excursion
624 (skip-chars-forward "'")
625 (or (not (zerop (skip-syntax-backward "_w")))
626 (eq (char-syntax (following-char)) ?w)
627 (eq (char-syntax (following-char)) ?_)
628 (forward-sexp -1))
629 (skip-chars-forward "'")
630 (let ((obj (read (current-buffer))))
631 (and (symbolp obj) (boundp obj) obj)))
632 (error nil))
633 (let* ((str (find-tag-default))
634 (sym (if str (intern-soft str))))
635 (if (and sym (or any-symbol (boundp sym)))
636 sym
637 (save-match-data
638 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
639 (setq sym (intern-soft (match-string 1 str)))
640 (and (or any-symbol (boundp sym)) sym)))))
641 0)))
642
643 (defun describe-variable-custom-version-info (variable)
644 (let ((custom-version (get variable 'custom-version))
645 (cpv (get variable 'custom-package-version))
646 (output nil))
647 (if custom-version
648 (setq output
649 (format "This variable was introduced, or its default value was changed, in\nversion %s of Emacs.\n"
650 custom-version))
651 (when cpv
652 (let* ((package (car-safe cpv))
653 (version (if (listp (cdr-safe cpv))
654 (car (cdr-safe cpv))
655 (cdr-safe cpv)))
656 (pkg-versions (assq package customize-package-emacs-version-alist))
657 (emacsv (cdr (assoc version pkg-versions))))
658 (if (and package version)
659 (setq output
660 (format (concat "This variable was introduced, or its default value was changed, in\nversion %s of the %s package"
661 (if emacsv
662 (format " that is part of Emacs %s" emacsv))
663 ".\n")
664 version package))))))
665 output))
666
667 ;;;###autoload
668 (defun describe-variable (variable &optional buffer frame)
669 "Display the full documentation of VARIABLE (a symbol).
670 Returns the documentation as a string, also.
671 If VARIABLE has a buffer-local value in BUFFER or FRAME
672 \(default to the current buffer and current frame),
673 it is displayed along with the global value."
674 (interactive
675 (let ((v (variable-at-point))
676 (enable-recursive-minibuffers t)
677 val)
678 (setq val (completing-read (if (symbolp v)
679 (format
680 "Describe variable (default %s): " v)
681 "Describe variable: ")
682 obarray
683 (lambda (vv)
684 (or (get vv 'variable-documentation)
685 (and (boundp vv) (not (keywordp vv)))))
686 t nil nil
687 (if (symbolp v) (symbol-name v))))
688 (list (if (equal val "")
689 v (intern val)))))
690 (let (file-name)
691 (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
692 (unless (frame-live-p frame) (setq frame (selected-frame)))
693 (if (not (symbolp variable))
694 (message "You did not specify a variable")
695 (save-excursion
696 (let ((valvoid (not (with-current-buffer buffer (boundp variable))))
697 (permanent-local (get variable 'permanent-local))
698 val val-start-pos locus)
699 ;; Extract the value before setting up the output buffer,
700 ;; in case `buffer' *is* the output buffer.
701 (unless valvoid
702 (with-selected-frame frame
703 (with-current-buffer buffer
704 (setq val (symbol-value variable)
705 locus (variable-binding-locus variable)))))
706 (help-setup-xref (list #'describe-variable variable buffer)
707 (called-interactively-p 'interactive))
708 (with-help-window (help-buffer)
709 (with-current-buffer buffer
710 (prin1 variable)
711 (setq file-name (find-lisp-object-file-name variable 'defvar))
712
713 (if file-name
714 (progn
715 (princ " is a variable defined in ‘")
716 (princ (if (eq file-name 'C-source)
717 "C source code"
718 (file-name-nondirectory file-name)))
719 (princ "’.\n")
720 (with-current-buffer standard-output
721 (save-excursion
722 (re-search-backward "‘\\([^‘’]+\\)’" nil t)
723 (help-xref-button 1 'help-variable-def
724 variable file-name)))
725 (if valvoid
726 (princ "It is void as a variable.")
727 (princ "Its ")))
728 (if valvoid
729 (princ " is void as a variable.")
730 (princ "'s "))))
731 (unless valvoid
732 (with-current-buffer standard-output
733 (setq val-start-pos (point))
734 (princ "value is ")
735 (let ((from (point))
736 (line-beg (line-beginning-position))
737 (print-rep
738 (let ((print-quoted t))
739 (prin1-to-string val))))
740 (if (< (+ (length print-rep) (point) (- line-beg)) 68)
741 (insert print-rep)
742 (terpri)
743 (pp val)
744 (if (< (point) (+ 68 (line-beginning-position 0)))
745 (delete-region from (1+ from))
746 (delete-region (1- from) from)))
747 (let* ((sv (get variable 'standard-value))
748 (origval (and (consp sv)
749 (condition-case nil
750 (eval (car sv))
751 (error :help-eval-error)))))
752 (when (and (consp sv)
753 (not (equal origval val))
754 (not (equal origval :help-eval-error)))
755 (princ "\nOriginal value was \n")
756 (setq from (point))
757 (pp origval)
758 (if (< (point) (+ from 20))
759 (delete-region (1- from) from)))))))
760 (terpri)
761 (when locus
762 (cond
763 ((bufferp locus)
764 (princ (format "Local in buffer %s; "
765 (buffer-name buffer))))
766 ((framep locus)
767 (princ (format "It is a frame-local variable; ")))
768 ((terminal-live-p locus)
769 (princ (format "It is a terminal-local variable; ")))
770 (t
771 (princ (format "It is local to %S" locus))))
772 (if (not (default-boundp variable))
773 (princ "globally void")
774 (let ((global-val (default-value variable)))
775 (with-current-buffer standard-output
776 (princ "global value is ")
777 (if (eq val global-val)
778 (princ "the same.")
779 (terpri)
780 ;; Fixme: pp can take an age if you happen to
781 ;; ask for a very large expression. We should
782 ;; probably print it raw once and check it's a
783 ;; sensible size before prettyprinting. -- fx
784 (let ((from (point)))
785 (pp global-val)
786 ;; See previous comment for this function.
787 ;; (help-xref-on-pp from (point))
788 (if (< (point) (+ from 20))
789 (delete-region (1- from) from)))))))
790 (terpri))
791
792 ;; If the value is large, move it to the end.
793 (with-current-buffer standard-output
794 (when (> (count-lines (point-min) (point-max)) 10)
795 ;; Note that setting the syntax table like below
796 ;; makes forward-sexp move over a `'s' at the end
797 ;; of a symbol.
798 (set-syntax-table emacs-lisp-mode-syntax-table)
799 (goto-char val-start-pos)
800 ;; The line below previously read as
801 ;; (delete-region (point) (progn (end-of-line) (point)))
802 ;; which suppressed display of the buffer local value for
803 ;; large values.
804 (when (looking-at "value is") (replace-match ""))
805 (save-excursion
806 (insert "\n\nValue:")
807 (set (make-local-variable 'help-button-cache)
808 (point-marker)))
809 (insert "value is shown ")
810 (insert-button "below"
811 'action help-button-cache
812 'follow-link t
813 'help-echo "mouse-2, RET: show value")
814 (insert ".\n")))
815 (terpri)
816
817 (let* ((alias (condition-case nil
818 (indirect-variable variable)
819 (error variable)))
820 (obsolete (get variable 'byte-obsolete-variable))
821 (use (car obsolete))
822 (safe-var (get variable 'safe-local-variable))
823 (doc (or (documentation-property
824 variable 'variable-documentation)
825 (documentation-property
826 alias 'variable-documentation)))
827 (extra-line nil))
828
829 ;; Mention if it's a local variable.
830 (cond
831 ((and (local-variable-if-set-p variable)
832 (or (not (local-variable-p variable))
833 (with-temp-buffer
834 (local-variable-if-set-p variable))))
835 (setq extra-line t)
836 (princ " Automatically becomes ")
837 (if permanent-local
838 (princ "permanently "))
839 (princ "buffer-local when set.\n"))
840 ((not permanent-local))
841 ((bufferp locus)
842 (setq extra-line t)
843 (princ " This variable's buffer-local value is permanent.\n"))
844 (t
845 (setq extra-line t)
846 (princ " This variable's value is permanent \
847 if it is given a local binding.\n")))
848
849 ;; Mention if it's an alias.
850 (unless (eq alias variable)
851 (setq extra-line t)
852 (princ (format " This variable is an alias for ‘%s’.\n"
853 alias)))
854
855 (when obsolete
856 (setq extra-line t)
857 (princ " This variable is obsolete")
858 (if (nth 2 obsolete)
859 (princ (format " since %s" (nth 2 obsolete))))
860 (princ (cond ((stringp use) (concat ";\n " use))
861 (use (format ";\n use ‘%s’ instead."
862 (car obsolete)))
863 (t ".")))
864 (terpri))
865
866 (when (member (cons variable val)
867 (with-current-buffer buffer
868 file-local-variables-alist))
869 (setq extra-line t)
870 (if (member (cons variable val)
871 (with-current-buffer buffer
872 dir-local-variables-alist))
873 (let ((file (and (buffer-file-name buffer)
874 (not (file-remote-p
875 (buffer-file-name buffer)))
876 (dir-locals-find-file
877 (buffer-file-name buffer))))
878 (dir-file t))
879 (princ " This variable's value is directory-local")
880 (if (null file)
881 (princ ".\n")
882 (princ ", set ")
883 (if (consp file) ; result from cache
884 ;; If the cache element has an mtime, we
885 ;; assume it came from a file.
886 (if (nth 2 file)
887 (setq file (expand-file-name
888 dir-locals-file (car file)))
889 ;; Otherwise, assume it was set directly.
890 (setq file (car file)
891 dir-file nil)))
892 (princ (if dir-file
893 "by the file\n ‘"
894 "for the directory\n ‘"))
895 (with-current-buffer standard-output
896 (insert-text-button
897 file 'type 'help-dir-local-var-def
898 'help-args (list variable file)))
899 (princ "’.\n")))
900 (princ " This variable's value is file-local.\n")))
901
902 (when (memq variable ignored-local-variables)
903 (setq extra-line t)
904 (princ " This variable is ignored as a file-local \
905 variable.\n"))
906
907 ;; Can be both risky and safe, eg auto-fill-function.
908 (when (risky-local-variable-p variable)
909 (setq extra-line t)
910 (princ " This variable may be risky if used as a \
911 file-local variable.\n")
912 (when (assq variable safe-local-variable-values)
913 (princ " However, you have added it to \
914 ‘safe-local-variable-values’.\n")))
915
916 (when safe-var
917 (setq extra-line t)
918 (princ " This variable is safe as a file local variable ")
919 (princ "if its value\n satisfies the predicate ")
920 (princ (if (byte-code-function-p safe-var)
921 "which is a byte-compiled expression.\n"
922 (format "‘%s’.\n" safe-var))))
923
924 (if extra-line (terpri))
925 (princ "Documentation:\n")
926 (with-current-buffer standard-output
927 (insert (or doc "Not documented as a variable."))))
928
929 ;; Make a link to customize if this variable can be customized.
930 (when (custom-variable-p variable)
931 (let ((customize-label "customize"))
932 (terpri)
933 (terpri)
934 (princ (concat "You can " customize-label " this variable."))
935 (with-current-buffer standard-output
936 (save-excursion
937 (re-search-backward
938 (concat "\\(" customize-label "\\)") nil t)
939 (help-xref-button 1 'help-customize-variable variable))))
940 ;; Note variable's version or package version
941 (let ((output (describe-variable-custom-version-info variable)))
942 (when output
943 (terpri)
944 (terpri)
945 (princ output))))
946
947 (with-current-buffer standard-output
948 ;; Return the text we displayed.
949 (buffer-string))))))))
950
951
952 ;;;###autoload
953 (defun describe-function-or-variable (symbol &optional buffer frame)
954 "Display the full documentation of the function or variable SYMBOL.
955 If SYMBOL is a variable and has a buffer-local value in BUFFER or FRAME
956 \(default to the current buffer and current frame), it is displayed along
957 with the global value."
958 (interactive
959 (let* ((v-or-f (variable-at-point))
960 (found (symbolp v-or-f))
961 (v-or-f (if found v-or-f (function-called-at-point)))
962 (found (or found v-or-f))
963 (enable-recursive-minibuffers t)
964 val)
965 (setq val (completing-read (if found
966 (format
967 "Describe function or variable (default %s): " v-or-f)
968 "Describe function or variable: ")
969 obarray
970 (lambda (vv)
971 (or (fboundp vv)
972 (get vv 'variable-documentation)
973 (and (boundp vv) (not (keywordp vv)))))
974 t nil nil
975 (if found (symbol-name v-or-f))))
976 (list (if (equal val "")
977 v-or-f (intern val)))))
978 (if (not (symbolp symbol)) (message "You didn't specify a function or variable")
979 (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
980 (unless (frame-live-p frame) (setq frame (selected-frame)))
981 (help-xref-interned symbol buffer frame)))
982
983 ;;;###autoload
984 (defun describe-syntax (&optional buffer)
985 "Describe the syntax specifications in the syntax table of BUFFER.
986 The descriptions are inserted in a help buffer, which is then displayed.
987 BUFFER defaults to the current buffer."
988 (interactive)
989 (setq buffer (or buffer (current-buffer)))
990 (help-setup-xref (list #'describe-syntax buffer)
991 (called-interactively-p 'interactive))
992 (with-help-window (help-buffer)
993 (let ((table (with-current-buffer buffer (syntax-table))))
994 (with-current-buffer standard-output
995 (describe-vector table 'internal-describe-syntax-value)
996 (while (setq table (char-table-parent table))
997 (insert "\nThe parent syntax table is:")
998 (describe-vector table 'internal-describe-syntax-value))))))
999
1000 (defun help-describe-category-set (value)
1001 (insert (cond
1002 ((null value) "default")
1003 ((char-table-p value) "deeper char-table ...")
1004 (t (condition-case nil
1005 (category-set-mnemonics value)
1006 (error "invalid"))))))
1007
1008 ;;;###autoload
1009 (defun describe-categories (&optional buffer)
1010 "Describe the category specifications in the current category table.
1011 The descriptions are inserted in a buffer, which is then displayed.
1012 If BUFFER is non-nil, then describe BUFFER's category table instead.
1013 BUFFER should be a buffer or a buffer name."
1014 (interactive)
1015 (setq buffer (or buffer (current-buffer)))
1016 (help-setup-xref (list #'describe-categories buffer)
1017 (called-interactively-p 'interactive))
1018 (with-help-window (help-buffer)
1019 (let* ((table (with-current-buffer buffer (category-table)))
1020 (docs (char-table-extra-slot table 0)))
1021 (if (or (not (vectorp docs)) (/= (length docs) 95))
1022 (error "Invalid first extra slot in this category table\n"))
1023 (with-current-buffer standard-output
1024 (insert "Legend of category mnemonics (see the tail for the longer description)\n")
1025 (let ((pos (point)) (items 0) lines n)
1026 (dotimes (i 95)
1027 (if (aref docs i) (setq items (1+ items))))
1028 (setq lines (1+ (/ (1- items) 4)))
1029 (setq n 0)
1030 (dotimes (i 95)
1031 (let ((elt (aref docs i)))
1032 (when elt
1033 (string-match ".*" elt)
1034 (setq elt (match-string 0 elt))
1035 (if (>= (length elt) 17)
1036 (setq elt (concat (substring elt 0 14) "...")))
1037 (if (< (point) (point-max))
1038 (move-to-column (* 20 (/ n lines)) t))
1039 (insert (+ i ?\s) ?: elt)
1040 (if (< (point) (point-max))
1041 (forward-line 1)
1042 (insert "\n"))
1043 (setq n (1+ n))
1044 (if (= (% n lines) 0)
1045 (goto-char pos))))))
1046 (goto-char (point-max))
1047 (insert "\n"
1048 "character(s)\tcategory mnemonics\n"
1049 "------------\t------------------")
1050 (describe-vector table 'help-describe-category-set)
1051 (insert "Legend of category mnemonics:\n")
1052 (dotimes (i 95)
1053 (let ((elt (aref docs i)))
1054 (when elt
1055 (if (string-match "\n" elt)
1056 (setq elt (substring elt (match-end 0))))
1057 (insert (+ i ?\s) ": " elt "\n"))))
1058 (while (setq table (char-table-parent table))
1059 (insert "\nThe parent category table is:")
1060 (describe-vector table 'help-describe-category-set))))))
1061
1062 \f
1063 ;;; Replacements for old lib-src/ programs. Don't seem especially useful.
1064
1065 ;; Replaces lib-src/digest-doc.c.
1066 ;;;###autoload
1067 (defun doc-file-to-man (file)
1068 "Produce an nroff buffer containing the doc-strings from the DOC file."
1069 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1070 internal-doc-file-name t)))
1071 (or (file-readable-p file)
1072 (error "Cannot read file `%s'" file))
1073 (pop-to-buffer (generate-new-buffer "*man-doc*"))
1074 (setq buffer-undo-list t)
1075 (insert ".TH \"Command Summary for GNU Emacs\"\n"
1076 ".AU Richard M. Stallman\n")
1077 (insert-file-contents file)
1078 (let (notfirst)
1079 (while (search-forward "\1f" nil 'move)
1080 (if (looking-at "S")
1081 (delete-region (1- (point)) (line-end-position))
1082 (delete-char -1)
1083 (if notfirst
1084 (insert "\n.DE\n")
1085 (setq notfirst t))
1086 (insert "\n.SH ")
1087 (insert (if (looking-at "F") "Function " "Variable "))
1088 (delete-char 1)
1089 (forward-line 1)
1090 (insert ".DS L\n"))))
1091 (insert "\n.DE\n")
1092 (setq buffer-undo-list nil)
1093 (nroff-mode))
1094
1095 ;; Replaces lib-src/sorted-doc.c.
1096 ;;;###autoload
1097 (defun doc-file-to-info (file)
1098 "Produce a texinfo buffer with sorted doc-strings from the DOC file."
1099 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1100 internal-doc-file-name t)))
1101 (or (file-readable-p file)
1102 (error "Cannot read file `%s'" file))
1103 (let ((i 0) type name doc alist)
1104 (with-temp-buffer
1105 (insert-file-contents file)
1106 ;; The characters "@{}" need special treatment.
1107 (while (re-search-forward "[@{}]" nil t)
1108 (backward-char)
1109 (insert "@")
1110 (forward-char 1))
1111 (goto-char (point-min))
1112 (while (search-forward "\1f" nil t)
1113 (unless (looking-at "S")
1114 (setq type (char-after)
1115 name (buffer-substring (1+ (point)) (line-end-position))
1116 doc (buffer-substring (line-beginning-position 2)
1117 (if (search-forward "\1f" nil 'move)
1118 (1- (point))
1119 (point)))
1120 alist (cons (list name type doc) alist))
1121 (backward-char 1))))
1122 (pop-to-buffer (generate-new-buffer "*info-doc*"))
1123 (setq buffer-undo-list t)
1124 ;; Write the output header.
1125 (insert "\\input texinfo @c -*-texinfo-*-\n"
1126 "@setfilename emacsdoc.info\n"
1127 "@settitle Command Summary for GNU Emacs\n"
1128 "@finalout\n"
1129 "\n@node Top\n"
1130 "@unnumbered Command Summary for GNU Emacs\n\n"
1131 "@table @asis\n\n"
1132 "@iftex\n"
1133 "@global@let@ITEM@item\n"
1134 "@def@item{@filbreak@vskip5pt@ITEM}\n"
1135 "@font@tensy cmsy10 scaled @magstephalf\n"
1136 "@font@teni cmmi10 scaled @magstephalf\n"
1137 "@def\\{{@tensy@char110}}\n" ; this backslash goes with cmr10
1138 "@def|{{@tensy@char106}}\n"
1139 "@def@{{{@tensy@char102}}\n"
1140 "@def@}{{@tensy@char103}}\n"
1141 "@def<{{@teni@char62}}\n"
1142 "@def>{{@teni@char60}}\n"
1143 "@chardef@@64\n"
1144 "@catcode43=12\n"
1145 "@tableindent-0.2in\n"
1146 "@end iftex\n")
1147 ;; Sort the array by name; within each name, by type (functions first).
1148 (setq alist (sort alist (lambda (e1 e2)
1149 (if (string-equal (car e1) (car e2))
1150 (<= (cadr e1) (cadr e2))
1151 (string-lessp (car e1) (car e2))))))
1152 ;; Print each function.
1153 (dolist (e alist)
1154 (insert "\n@item "
1155 (if (char-equal (cadr e) ?\F) "Function" "Variable")
1156 " @code{" (car e) "}\n@display\n"
1157 (nth 2 e)
1158 "\n@end display\n")
1159 ;; Try to avoid a save size overflow in the TeX output routine.
1160 (if (zerop (setq i (% (1+ i) 100)))
1161 (insert "\n@end table\n@table @asis\n")))
1162 (insert "@end table\n"
1163 "@bye\n")
1164 (setq buffer-undo-list nil)
1165 (texinfo-mode)))
1166
1167 (provide 'help-fns)
1168
1169 ;;; help-fns.el ends here