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