In Lisp manual, don't capitalize pty.
[bpt/emacs.git] / lisp / help-fns.el
CommitLineData
2fbc1934 1;;; help-fns.el --- Complex help functions -*- lexical-binding: t -*-
1113ff44 2
acaf905b 3;; Copyright (C) 1985-1986, 1993-1994, 1998-2012
589a99f2 4;; Free Software Foundation, Inc.
1113ff44
MR
5
6;; Maintainer: FSF
7;; Keywords: help, internal
bd78fa1d 8;; Package: emacs
1113ff44
MR
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
1113ff44
MR
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")
32226619
JB
53 (help-setup-xref (list #'describe-function function)
54 (called-interactively-p 'interactive))
1113ff44
MR
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.
673e08bb
SM
68Return (USAGE . DOC) or nil if there's no usage info, where USAGE info
69is a string describing the argument list of DEF, such as
70\"(apply FUNCTION &rest ARGUMENTS)\".
1113ff44
MR
71DEF 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 (consp def) "anonymous" def)
80 (match-string 1 docstring))
e2abe5a1
SM
81 (unless (zerop (match-beginning 0))
82 (substring docstring 0 (match-beginning 0))))))
1113ff44 83
e2abe5a1 84;; FIXME: Move to subr.el?
1113ff44
MR
85(defun help-add-fundoc-usage (docstring arglist)
86 "Add the usage info to DOCSTRING.
87If DOCSTRING already has a usage info, then just return it unchanged.
88The usage info is built from ARGLIST. DOCSTRING can be nil.
89ARGLIST can also be t or a string of the form \"(FUN ARG1 ARG2 ...)\"."
e2abe5a1
SM
90 (unless (stringp docstring) (setq docstring ""))
91 (if (or (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring)
92 (eq arglist t))
1113ff44
MR
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
e2abe5a1 103;; FIXME: Move to subr.el?
c2bd2ab0
SM
104(defun help-function-arglist (def &optional preserve-names)
105 "Return a formal argument list for the function DEF.
106IF PRESERVE-NAMES is non-nil, return a formal arglist that uses
107the same names as used in the original source code, when possible."
1113ff44
MR
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.
8d6c1239
SM
111 (if (eq (car-safe def) 'macro) (setq def (cdr def)))
112 (cond
c2bd2ab0 113 ((and (byte-code-function-p def) (listp (aref def 0))) (aref def 0))
8d6c1239 114 ((eq (car-safe def) 'lambda) (nth 1 def))
23aba0ea 115 ((eq (car-safe def) 'closure) (nth 2 def))
c2bd2ab0
SM
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))))
8d6c1239
SM
153 ((and (eq (car-safe def) 'autoload) (not (eq (nth 4 def) 'keymap)))
154 "[Arg list not available until function definition is loaded.]")
155 (t t)))
1113ff44 156
e2abe5a1 157;; FIXME: Move to subr.el?
1113ff44
MR
158(defun help-make-usage (function arglist)
159 (cons (if (symbolp function) function 'anonymous)
160 (mapcar (lambda (arg)
673e08bb 161 (if (not (symbolp arg)) arg
1113ff44 162 (let ((name (symbol-name arg)))
d032d5e7
SM
163 (cond
164 ((string-match "\\`&" name) arg)
165 ((string-match "\\`_" name)
166 (intern (upcase (substring name 1))))
167 (t (intern (upcase name)))))))
1113ff44
MR
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.
180KIND 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
1659ada0
JB
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.
214Return ARG in face `help-argument-name'; ARG is also downcased
215if the variable `help-downcase-arguments' is non-nil."
216 (propertize (if help-downcase-arguments (downcase arg) arg)
217 'face 'help-argument-name))
1113ff44
MR
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")
2462470b 222 (dolist (arg args)
1113ff44
MR
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
1659ada0 235 (help-highlight-arg arg)
2462470b
SM
236 doc t t 1)))
237 doc))
1113ff44
MR
238
239(defun help-highlight-arguments (usage doc &rest args)
b9598260 240 (when (and usage (string-match "^(" usage))
1113ff44
MR
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)))))
91af3942 259 ;; Highlight arguments in the USAGE string
1113ff44
MR
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.
279OBJECT should be a symbol associated with a function, variable, or face;
280 alternatively, it can be a function definition.
fe4be04c
JB
281If TYPE is `defvar', search for a variable definition.
282If TYPE is `defface', search for a face definition.
1113ff44
MR
283If TYPE is the value returned by `symbol-function' for a function symbol,
284 search for a function definition.
285
286The return value is the absolute name of a readable file where OBJECT is
287defined. If several such files exist, preference is given to a file
288found via `load-path'. The return value can also be `C-source', which
289means that OBJECT is a function or variable defined in C. If no
290suitable file is found, return nil."
291 (let* ((autoloaded (eq (car-safe type) 'autoload))
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))))
6565b5ab 312 (when (cdr location)
1113ff44
MR
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
273cefaa
GM
319 (file-name-sans-extension
320 (match-string-no-properties 1))
321 load-path '(".el" ".elc") 'readable))))))))
1113ff44
MR
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)
d78cdcf7
CY
338 ;; Now, `file-name' should have become an absolute file name.
339 ;; For files loaded from ~/.emacs.elc, try ~/.emacs.
340 ((let (fn)
341 (and (string-equal file-name
342 (expand-file-name ".emacs.elc" "~"))
343 (file-readable-p (setq fn (expand-file-name ".emacs" "~")))
344 fn)))
345 ;; When the Elisp source file can be found in the install
346 ;; directory, return the name of that file.
1113ff44
MR
347 ((let ((lib-name
348 (if (string-match "[.]elc\\'" file-name)
349 (substring-no-properties file-name 0 -1)
350 file-name)))
1113ff44
MR
351 (or (and (file-readable-p lib-name) lib-name)
352 ;; The library might be compressed.
353 (and (file-readable-p (concat lib-name ".gz")) lib-name))))
354 ((let* ((lib-name (file-name-nondirectory file-name))
355 ;; The next form is from `describe-simplify-lib-file-name'.
356 (file-name
357 ;; Try converting the absolute file name to a library
358 ;; name, convert that back to a file name and see if we
359 ;; get the original one. If so, they are equivalent.
360 (if (equal file-name (locate-file lib-name load-path '("")))
361 (if (string-match "[.]elc\\'" lib-name)
362 (substring-no-properties lib-name 0 -1)
363 lib-name)
364 file-name))
365 ;; The next three forms are from `find-source-lisp-file'.
366 (elc-file (locate-file
367 (concat file-name
368 (if (string-match "\\.el\\'" file-name)
369 "c"
370 ".elc"))
371 load-path nil 'readable))
372 (str (when elc-file
373 (with-temp-buffer
374 (insert-file-contents-literally elc-file nil 0 256)
375 (buffer-string))))
376 (src-file (and str
377 (string-match ";;; from file \\(.*\\.el\\)" str)
378 (match-string 1 str))))
379 (and src-file (file-readable-p src-file) src-file))))))
380
381(declare-function ad-get-advice-info "advice" (function))
382
71adb94b
SM
383(defun help-fns--compiler-macro (function)
384 (let ((handler nil))
385 ;; FIXME: Copied from macroexp.el.
386 (while (and (symbolp function)
387 (not (setq handler (get function 'compiler-macro)))
388 (fboundp function))
389 ;; Follow the sequence of aliases.
390 (setq function (symbol-function function)))
391 (when handler
392 (princ "This function has a compiler macro")
393 (let ((lib (get function 'compiler-macro-file)))
394 ;; FIXME: rather than look at the compiler-macro-file property,
395 ;; just look at `handler' itself.
396 (when (stringp lib)
397 (princ (format " in `%s'" lib))
398 (with-current-buffer standard-output
399 (save-excursion
400 (re-search-backward "`\\([^`']+\\)'" nil t)
401 (help-xref-button 1 'help-function-cmacro function lib)))))
402 (princ ".\n\n"))))
403
1113ff44
MR
404;;;###autoload
405(defun describe-function-1 (function)
406 (let* ((advised (and (symbolp function) (featurep 'advice)
407 (ad-get-advice-info function)))
408 ;; If the function is advised, use the symbol that has the
409 ;; real definition, if that symbol is already set up.
410 (real-function
411 (or (and advised
412 (let ((origname (cdr (assq 'origname advised))))
413 (and (fboundp origname) origname)))
414 function))
415 ;; Get the real definition.
416 (def (if (symbolp real-function)
417 (symbol-function real-function)
418 function))
419 file-name string
420 (beg (if (commandp def) "an interactive " "a "))
421 (pt1 (with-current-buffer (help-buffer) (point)))
422 errtype)
423 (setq string
b9598260 424 (cond ((or (stringp def) (vectorp def))
1113ff44
MR
425 "a keyboard macro")
426 ((subrp def)
427 (if (eq 'unevalled (cdr (subr-arity def)))
428 (concat beg "special form")
429 (concat beg "built-in function")))
430 ((byte-code-function-p def)
431 (concat beg "compiled Lisp function"))
432 ((symbolp def)
433 (while (and (fboundp def)
434 (symbolp (symbol-function def)))
435 (setq def (symbol-function def)))
436 ;; Handle (defalias 'foo 'bar), where bar is undefined.
437 (or (fboundp def) (setq errtype 'alias))
438 (format "an alias for `%s'" def))
439 ((eq (car-safe def) 'lambda)
440 (concat beg "Lisp function"))
441 ((eq (car-safe def) 'macro)
442 "a Lisp macro")
b9598260
SM
443 ((eq (car-safe def) 'closure)
444 (concat beg "Lisp closure"))
1113ff44
MR
445 ((eq (car-safe def) 'autoload)
446 (format "%s autoloaded %s"
447 (if (commandp def) "an interactive" "an")
448 (if (eq (nth 4 def) 'keymap) "keymap"
449 (if (nth 4 def) "Lisp macro" "Lisp function"))))
450 ((keymapp def)
451 (let ((is-full nil)
452 (elts (cdr-safe def)))
453 (while elts
454 (if (char-table-p (car-safe elts))
455 (setq is-full t
456 elts nil))
457 (setq elts (cdr-safe elts)))
458 (if is-full
459 "a full keymap"
460 "a sparse keymap")))
461 (t "")))
462 (princ string)
463 (if (eq errtype 'alias)
464 (princ ",\nwhich is not defined. Please make a bug report.")
465 (with-current-buffer standard-output
466 (save-excursion
467 (save-match-data
468 (when (re-search-backward "alias for `\\([^`']+\\)'" nil t)
469 (help-xref-button 1 'help-function def)))))
470
471 (setq file-name (find-lisp-object-file-name function def))
472 (when file-name
473 (princ " in `")
474 ;; We used to add .el to the file name,
475 ;; but that's completely wrong when the user used load-file.
e8e8dbf9
MR
476 (princ (if (eq file-name 'C-source)
477 "C source code"
478 (file-name-nondirectory file-name)))
1113ff44
MR
479 (princ "'")
480 ;; Make a hyperlink to the library.
481 (with-current-buffer standard-output
482 (save-excursion
483 (re-search-backward "`\\([^`']+\\)'" nil t)
cebabb67 484 (help-xref-button 1 'help-function-def function file-name))))
1113ff44
MR
485 (princ ".")
486 (with-current-buffer (help-buffer)
487 (fill-region-as-paragraph (save-excursion (goto-char pt1) (forward-line 0) (point))
488 (point)))
489 (terpri)(terpri)
490 (when (commandp function)
63326785
CY
491 (let ((pt2 (with-current-buffer (help-buffer) (point)))
492 (remapped (command-remapping function)))
493 (unless (memq remapped '(ignore undefined))
494 (let ((keys (where-is-internal
495 (or remapped function) overriding-local-map nil nil))
496 non-modified-keys)
497 (if (and (eq function 'self-insert-command)
498 (vectorp (car-safe keys))
499 (consp (aref (car keys) 0)))
500 (princ "It is bound to many ordinary text characters.\n")
501 ;; Which non-control non-meta keys run this command?
502 (dolist (key keys)
503 (if (member (event-modifiers (aref key 0)) '(nil (shift)))
504 (push key non-modified-keys)))
505 (when remapped
07498861 506 (princ "Its keys are remapped to `")
63326785 507 (princ (symbol-name remapped))
07498861 508 (princ "'.\n"))
63326785
CY
509
510 (when keys
07498861
CY
511 (princ (if remapped
512 "Without this remapping, it would be bound to "
513 "It is bound to "))
63326785
CY
514 ;; If lots of ordinary text characters run this command,
515 ;; don't mention them one by one.
516 (if (< (length non-modified-keys) 10)
517 (princ (mapconcat 'key-description keys ", "))
518 (dolist (key non-modified-keys)
519 (setq keys (delq key keys)))
520 (if keys
521 (progn
522 (princ (mapconcat 'key-description keys ", "))
523 (princ ", and many ordinary text characters"))
524 (princ "many ordinary text characters"))))
525 (when (or remapped keys non-modified-keys)
526 (princ ".")
527 (terpri)))))
1113ff44 528
1113ff44
MR
529 (with-current-buffer (help-buffer)
530 (fill-region-as-paragraph pt2 (point))
531 (unless (looking-back "\n\n")
532 (terpri)))))
71adb94b 533 (help-fns--compiler-macro function)
8d6c1239
SM
534 (let* ((advertised (gethash def advertised-signature-table t))
535 (arglist (if (listp advertised)
536 advertised (help-function-arglist def)))
5f9d345c
SM
537 (doc (condition-case err (documentation function)
538 (error (format "No Doc! %S" err))))
1113ff44
MR
539 (usage (help-split-fundoc doc function)))
540 (with-current-buffer standard-output
541 ;; If definition is a keymap, skip arglist note.
542 (unless (keymapp function)
8d6c1239 543 (if usage (setq doc (cdr usage)))
1113ff44 544 (let* ((use (cond
8d6c1239 545 ((and usage (not (listp advertised))) (car usage))
1113ff44
MR
546 ((listp arglist)
547 (format "%S" (help-make-usage function arglist)))
548 ((stringp arglist) arglist)
549 ;; Maybe the arglist is in the docstring of a symbol
550 ;; this one is aliased to.
551 ((let ((fun real-function))
552 (while (and (symbolp fun)
553 (setq fun (symbol-function fun))
554 (not (setq usage (help-split-fundoc
555 (documentation fun)
556 function)))))
557 usage)
558 (car usage))
559 ((or (stringp def)
560 (vectorp def))
561 (format "\nMacro: %s" (format-kbd-macro def)))
562 (t "[Missing arglist. Please make a bug report.]")))
563 (high (help-highlight-arguments use doc)))
564 (let ((fill-begin (point)))
565 (insert (car high) "\n")
ce5b520a 566 (fill-region fill-begin (point)))
b38b1ec0 567 (setq doc (cdr high))))
5199bde1
CY
568
569 ;; If this is a derived mode, link to the parent.
570 (let ((parent-mode (and (symbolp real-function)
571 (get real-function
572 'derived-mode-parent))))
573 (when parent-mode
574 (with-current-buffer standard-output
575 (insert "\nParent mode: `")
576 (let ((beg (point)))
577 (insert (format "%s" parent-mode))
578 (make-text-button beg (point)
579 'type 'help-function
580 'help-args (list parent-mode))))
581 (princ "'.\n")))
582
ce5b520a
SM
583 (let* ((obsolete (and
584 ;; function might be a lambda construct.
585 (symbolp function)
586 (get function 'byte-obsolete-info)))
587 (use (car obsolete)))
588 (when obsolete
589 (princ "\nThis function is obsolete")
590 (when (nth 2 obsolete)
591 (insert (format " since %s" (nth 2 obsolete))))
592 (insert (cond ((stringp use) (concat ";\n" use))
593 (use (format ";\nuse `%s' instead." use))
594 (t "."))
595 "\n"))
596 (insert "\n"
597 (or doc "Not documented."))))))))
1113ff44
MR
598
599\f
600;; Variables
601
602;;;###autoload
603(defun variable-at-point (&optional any-symbol)
604 "Return the bound variable symbol found at or before point.
605Return 0 if there is no such symbol.
606If ANY-SYMBOL is non-nil, don't insist the symbol be bound."
607 (with-syntax-table emacs-lisp-mode-syntax-table
608 (or (condition-case ()
609 (save-excursion
047b2bb9 610 (skip-chars-forward "'")
1113ff44
MR
611 (or (not (zerop (skip-syntax-backward "_w")))
612 (eq (char-syntax (following-char)) ?w)
613 (eq (char-syntax (following-char)) ?_)
614 (forward-sexp -1))
615 (skip-chars-forward "'")
616 (let ((obj (read (current-buffer))))
617 (and (symbolp obj) (boundp obj) obj)))
618 (error nil))
619 (let* ((str (find-tag-default))
620 (sym (if str (intern-soft str))))
621 (if (and sym (or any-symbol (boundp sym)))
622 sym
623 (save-match-data
624 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
625 (setq sym (intern-soft (match-string 1 str)))
626 (and (or any-symbol (boundp sym)) sym)))))
627 0)))
628
629(defun describe-variable-custom-version-info (variable)
630 (let ((custom-version (get variable 'custom-version))
631 (cpv (get variable 'custom-package-version))
632 (output nil))
633 (if custom-version
634 (setq output
635 (format "This variable was introduced, or its default value was changed, in\nversion %s of Emacs.\n"
636 custom-version))
637 (when cpv
638 (let* ((package (car-safe cpv))
639 (version (if (listp (cdr-safe cpv))
640 (car (cdr-safe cpv))
641 (cdr-safe cpv)))
642 (pkg-versions (assq package customize-package-emacs-version-alist))
643 (emacsv (cdr (assoc version pkg-versions))))
644 (if (and package version)
645 (setq output
646 (format (concat "This variable was introduced, or its default value was changed, in\nversion %s of the %s package"
647 (if emacsv
648 (format " that is part of Emacs %s" emacsv))
649 ".\n")
650 version package))))))
651 output))
652
653;;;###autoload
654(defun describe-variable (variable &optional buffer frame)
655 "Display the full documentation of VARIABLE (a symbol).
656Returns the documentation as a string, also.
657If VARIABLE has a buffer-local value in BUFFER or FRAME
658\(default to the current buffer and current frame),
659it is displayed along with the global value."
660 (interactive
661 (let ((v (variable-at-point))
662 (enable-recursive-minibuffers t)
663 val)
664 (setq val (completing-read (if (symbolp v)
665 (format
666 "Describe variable (default %s): " v)
667 "Describe variable: ")
668 obarray
ba83908c 669 (lambda (vv)
8d17e7ca 670 (or (get vv 'variable-documentation)
6f4e1aed 671 (and (boundp vv) (not (keywordp vv)))))
1113ff44
MR
672 t nil nil
673 (if (symbolp v) (symbol-name v))))
674 (list (if (equal val "")
675 v (intern val)))))
676 (let (file-name)
677 (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
678 (unless (frame-live-p frame) (setq frame (selected-frame)))
679 (if (not (symbolp variable))
680 (message "You did not specify a variable")
681 (save-excursion
682 (let ((valvoid (not (with-current-buffer buffer (boundp variable))))
683 val val-start-pos locus)
684 ;; Extract the value before setting up the output buffer,
685 ;; in case `buffer' *is* the output buffer.
686 (unless valvoid
687 (with-selected-frame frame
688 (with-current-buffer buffer
689 (setq val (symbol-value variable)
690 locus (variable-binding-locus variable)))))
691 (help-setup-xref (list #'describe-variable variable buffer)
32226619 692 (called-interactively-p 'interactive))
1113ff44
MR
693 (with-help-window (help-buffer)
694 (with-current-buffer buffer
695 (prin1 variable)
696 (setq file-name (find-lisp-object-file-name variable 'defvar))
697
698 (if file-name
699 (progn
700 (princ " is a variable defined in `")
e8e8dbf9
MR
701 (princ (if (eq file-name 'C-source)
702 "C source code"
703 (file-name-nondirectory file-name)))
1113ff44
MR
704 (princ "'.\n")
705 (with-current-buffer standard-output
706 (save-excursion
707 (re-search-backward "`\\([^`']+\\)'" nil t)
708 (help-xref-button 1 'help-variable-def
709 variable file-name)))
710 (if valvoid
711 (princ "It is void as a variable.")
712 (princ "Its ")))
713 (if valvoid
714 (princ " is void as a variable.")
715 (princ "'s "))))
3c505d31 716 (unless valvoid
1113ff44
MR
717 (with-current-buffer standard-output
718 (setq val-start-pos (point))
719 (princ "value is ")
6c1e4b46
CY
720 (let ((from (point))
721 (line-beg (line-beginning-position))
722 ;;
723 (print-rep
724 (let ((print-quoted t))
725 (prin1-to-string val))))
726 (if (< (+ (length print-rep) (point) (- line-beg)) 68)
727 (insert print-rep)
728 (terpri)
729 (pp val)
730 (if (< (point) (+ 68 (line-beginning-position 0)))
731 (delete-region from (1+ from))
732 (delete-region (1- from) from)))
0097720d
SM
733 (let* ((sv (get variable 'standard-value))
734 (origval (and (consp sv)
735 (condition-case nil
736 (eval (car sv))
737 (error :help-eval-error)))))
738 (when (and (consp sv)
739 (not (equal origval val))
740 (not (equal origval :help-eval-error)))
741 (princ "\nOriginal value was \n")
742 (setq from (point))
743 (pp origval)
744 (if (< (point) (+ from 20))
745 (delete-region (1- from) from)))))))
1113ff44 746 (terpri)
1113ff44 747 (when locus
e2b551c5
SM
748 (cond
749 ((bufferp locus)
750 (princ (format "%socal in buffer %s; "
751 (if (get variable 'permanent-local)
752 "Permanently l" "L")
753 (buffer-name))))
754 ((framep locus)
755 (princ (format "It is a frame-local variable; ")))
756 ((terminal-live-p locus)
757 (princ (format "It is a terminal-local variable; ")))
758 (t
759 (princ (format "It is local to %S" locus))))
1113ff44
MR
760 (if (not (default-boundp variable))
761 (princ "globally void")
762 (let ((val (default-value variable)))
763 (with-current-buffer standard-output
764 (princ "global value is ")
765 (terpri)
766 ;; Fixme: pp can take an age if you happen to
767 ;; ask for a very large expression. We should
768 ;; probably print it raw once and check it's a
769 ;; sensible size before prettyprinting. -- fx
770 (let ((from (point)))
771 (pp val)
772 ;; See previous comment for this function.
773 ;; (help-xref-on-pp from (point))
774 (if (< (point) (+ from 20))
775 (delete-region (1- from) from))))))
776 (terpri))
777
778 ;; If the value is large, move it to the end.
779 (with-current-buffer standard-output
780 (when (> (count-lines (point-min) (point-max)) 10)
781 ;; Note that setting the syntax table like below
782 ;; makes forward-sexp move over a `'s' at the end
783 ;; of a symbol.
784 (set-syntax-table emacs-lisp-mode-syntax-table)
785 (goto-char val-start-pos)
786 ;; The line below previously read as
787 ;; (delete-region (point) (progn (end-of-line) (point)))
788 ;; which suppressed display of the buffer local value for
789 ;; large values.
790 (when (looking-at "value is") (replace-match ""))
791 (save-excursion
792 (insert "\n\nValue:")
793 (set (make-local-variable 'help-button-cache)
794 (point-marker)))
795 (insert "value is shown ")
796 (insert-button "below"
797 'action help-button-cache
798 'follow-link t
799 'help-echo "mouse-2, RET: show value")
800 (insert ".\n")))
801 (terpri)
802
803 (let* ((alias (condition-case nil
804 (indirect-variable variable)
805 (error variable)))
806 (obsolete (get variable 'byte-obsolete-variable))
807 (use (car obsolete))
808 (safe-var (get variable 'safe-local-variable))
dc5d230c
SM
809 (doc (condition-case err
810 (or (documentation-property
811 variable 'variable-documentation)
812 (documentation-property
813 alias 'variable-documentation))
814 (error (format "Doc not found: %S" err))))
1113ff44
MR
815 (extra-line nil))
816 ;; Add a note for variables that have been make-var-buffer-local.
817 (when (and (local-variable-if-set-p variable)
818 (or (not (local-variable-p variable))
819 (with-temp-buffer
820 (local-variable-if-set-p variable))))
821 (setq extra-line t)
822 (princ " Automatically becomes buffer-local when set in any fashion.\n"))
823
824 ;; Mention if it's an alias
825 (unless (eq alias variable)
826 (setq extra-line t)
827 (princ (format " This variable is an alias for `%s'.\n" alias)))
828
829 (when obsolete
830 (setq extra-line t)
831 (princ " This variable is obsolete")
2403c841
SM
832 (if (nth 2 obsolete)
833 (princ (format " since %s" (nth 2 obsolete))))
1113ff44
MR
834 (princ (cond ((stringp use) (concat ";\n " use))
835 (use (format ";\n use `%s' instead." (car obsolete)))
836 (t ".")))
837 (terpri))
2ee20f24
JL
838
839 (when (member (cons variable val) file-local-variables-alist)
840 (setq extra-line t)
841 (if (member (cons variable val) dir-local-variables-alist)
842 (let ((file (and (buffer-file-name)
303f9ae0
GM
843 (not (file-remote-p (buffer-file-name)))
844 (dir-locals-find-file
845 (buffer-file-name))))
846 (type "file"))
2ee20f24 847 (princ " This variable is a directory local variable")
b6ce92f1 848 (when file
303f9ae0
GM
849 (if (consp file) ; result from cache
850 ;; If the cache element has an mtime, we
851 ;; assume it came from a file.
852 (if (nth 2 file)
853 (setq file (expand-file-name
854 dir-locals-file (car file)))
855 ;; Otherwise, assume it was set directly.
856 (setq type "directory")))
857 (princ (format "\n from the %s \"%s\"" type file)))
2ee20f24
JL
858 (princ ".\n"))
859 (princ " This variable is a file local variable.\n")))
860
589a99f2
GM
861 (when (memq variable ignored-local-variables)
862 (setq extra-line t)
863 (princ " This variable is ignored when used as a file local \
864variable.\n"))
865
866 ;; Can be both risky and safe, eg auto-fill-function.
867 (when (risky-local-variable-p variable)
868 (setq extra-line t)
869 (princ " This variable is potentially risky when used as a \
870file local variable.\n")
871 (when (assq variable safe-local-variable-values)
872 (princ " However, you have added it to \
873`safe-local-variable-values'.\n")))
874
1113ff44
MR
875 (when safe-var
876 (setq extra-line t)
877 (princ " This variable is safe as a file local variable ")
878 (princ "if its value\n satisfies the predicate ")
879 (princ (if (byte-code-function-p safe-var)
880 "which is byte-compiled expression.\n"
881 (format "`%s'.\n" safe-var))))
882
883 (if extra-line (terpri))
884 (princ "Documentation:\n")
885 (with-current-buffer standard-output
886 (insert (or doc "Not documented as a variable."))))
887
888 ;; Make a link to customize if this variable can be customized.
889 (when (custom-variable-p variable)
890 (let ((customize-label "customize"))
891 (terpri)
892 (terpri)
893 (princ (concat "You can " customize-label " this variable."))
894 (with-current-buffer standard-output
895 (save-excursion
896 (re-search-backward
897 (concat "\\(" customize-label "\\)") nil t)
898 (help-xref-button 1 'help-customize-variable variable))))
899 ;; Note variable's version or package version
900 (let ((output (describe-variable-custom-version-info variable)))
901 (when output
902 (terpri)
903 (terpri)
904 (princ output))))
905
7fdbcd83 906 (with-current-buffer standard-output
1113ff44
MR
907 ;; Return the text we displayed.
908 (buffer-string))))))))
909
910
911;;;###autoload
912(defun describe-syntax (&optional buffer)
913 "Describe the syntax specifications in the syntax table of BUFFER.
914The descriptions are inserted in a help buffer, which is then displayed.
915BUFFER defaults to the current buffer."
916 (interactive)
917 (setq buffer (or buffer (current-buffer)))
32226619
JB
918 (help-setup-xref (list #'describe-syntax buffer)
919 (called-interactively-p 'interactive))
1113ff44
MR
920 (with-help-window (help-buffer)
921 (let ((table (with-current-buffer buffer (syntax-table))))
922 (with-current-buffer standard-output
923 (describe-vector table 'internal-describe-syntax-value)
924 (while (setq table (char-table-parent table))
925 (insert "\nThe parent syntax table is:")
926 (describe-vector table 'internal-describe-syntax-value))))))
927
928(defun help-describe-category-set (value)
929 (insert (cond
930 ((null value) "default")
931 ((char-table-p value) "deeper char-table ...")
2fbc1934 932 (t (condition-case nil
1113ff44
MR
933 (category-set-mnemonics value)
934 (error "invalid"))))))
935
936;;;###autoload
937(defun describe-categories (&optional buffer)
938 "Describe the category specifications in the current category table.
939The descriptions are inserted in a buffer, which is then displayed.
940If BUFFER is non-nil, then describe BUFFER's category table instead.
941BUFFER should be a buffer or a buffer name."
942 (interactive)
943 (setq buffer (or buffer (current-buffer)))
32226619
JB
944 (help-setup-xref (list #'describe-categories buffer)
945 (called-interactively-p 'interactive))
1113ff44 946 (with-help-window (help-buffer)
c6ec96f8
KH
947 (let* ((table (with-current-buffer buffer (category-table)))
948 (docs (char-table-extra-slot table 0)))
949 (if (or (not (vectorp docs)) (/= (length docs) 95))
950 (error "Invalid first extra slot in this category table\n"))
1113ff44 951 (with-current-buffer standard-output
c6ec96f8
KH
952 (insert "Legend of category mnemonics (see the tail for the longer description)\n")
953 (let ((pos (point)) (items 0) lines n)
954 (dotimes (i 95)
955 (if (aref docs i) (setq items (1+ items))))
956 (setq lines (1+ (/ (1- items) 4)))
957 (setq n 0)
958 (dotimes (i 95)
959 (let ((elt (aref docs i)))
960 (when elt
961 (string-match ".*" elt)
962 (setq elt (match-string 0 elt))
963 (if (>= (length elt) 17)
964 (setq elt (concat (substring elt 0 14) "...")))
965 (if (< (point) (point-max))
966 (move-to-column (* 20 (/ n lines)) t))
967 (insert (+ i ?\s) ?: elt)
968 (if (< (point) (point-max))
969 (forward-line 1)
970 (insert "\n"))
971 (setq n (1+ n))
972 (if (= (% n lines) 0)
973 (goto-char pos))))))
974 (goto-char (point-max))
975 (insert "\n"
976 "character(s)\tcategory mnemonics\n"
977 "------------\t------------------")
1113ff44 978 (describe-vector table 'help-describe-category-set)
c6ec96f8
KH
979 (insert "Legend of category mnemonics:\n")
980 (dotimes (i 95)
981 (let ((elt (aref docs i)))
982 (when elt
983 (if (string-match "\n" elt)
984 (setq elt (substring elt (match-end 0))))
985 (insert (+ i ?\s) ": " elt "\n"))))
986 (while (setq table (char-table-parent table))
987 (insert "\nThe parent category table is:")
988 (describe-vector table 'help-describe-category-set))))))
1113ff44 989
17284e30
GM
990\f
991;;; Replacements for old lib-src/ programs. Don't seem especially useful.
992
993;; Replaces lib-src/digest-doc.c.
994;;;###autoload
995(defun doc-file-to-man (file)
996 "Produce an nroff buffer containing the doc-strings from the DOC file."
997 (interactive (list (read-file-name "Name of DOC file: " doc-directory
998 internal-doc-file-name t)))
999 (or (file-readable-p file)
1000 (error "Cannot read file `%s'" file))
1001 (pop-to-buffer (generate-new-buffer "*man-doc*"))
1002 (setq buffer-undo-list t)
1003 (insert ".TH \"Command Summary for GNU Emacs\"\n"
1004 ".AU Richard M. Stallman\n")
1005 (insert-file-contents file)
1006 (let (notfirst)
1007 (while (search-forward "\1f" nil 'move)
1008 (if (looking-at "S")
1009 (delete-region (1- (point)) (line-end-position))
1010 (delete-char -1)
1011 (if notfirst
1012 (insert "\n.DE\n")
1013 (setq notfirst t))
1014 (insert "\n.SH ")
1015 (insert (if (looking-at "F") "Function " "Variable "))
1016 (delete-char 1)
1017 (forward-line 1)
1018 (insert ".DS L\n"))))
1019 (insert "\n.DE\n")
1020 (setq buffer-undo-list nil)
1021 (nroff-mode))
1022
1023;; Replaces lib-src/sorted-doc.c.
1024;;;###autoload
1025(defun doc-file-to-info (file)
1026 "Produce a texinfo buffer with sorted doc-strings from the DOC file."
1027 (interactive (list (read-file-name "Name of DOC file: " doc-directory
1028 internal-doc-file-name t)))
1029 (or (file-readable-p file)
1030 (error "Cannot read file `%s'" file))
1031 (let ((i 0) type name doc alist)
1032 (with-temp-buffer
1033 (insert-file-contents file)
1034 ;; The characters "@{}" need special treatment.
1035 (while (re-search-forward "[@{}]" nil t)
1036 (backward-char)
1037 (insert "@")
1038 (forward-char 1))
1039 (goto-char (point-min))
1040 (while (search-forward "\1f" nil t)
1041 (unless (looking-at "S")
1042 (setq type (char-after)
1043 name (buffer-substring (1+ (point)) (line-end-position))
1044 doc (buffer-substring (line-beginning-position 2)
1045 (if (search-forward "\1f" nil 'move)
1046 (1- (point))
1047 (point)))
1048 alist (cons (list name type doc) alist))
1049 (backward-char 1))))
1050 (pop-to-buffer (generate-new-buffer "*info-doc*"))
1051 (setq buffer-undo-list t)
1052 ;; Write the output header.
1053 (insert "\\input texinfo @c -*-texinfo-*-\n"
1054 "@setfilename emacsdoc.info\n"
1055 "@settitle Command Summary for GNU Emacs\n"
1056 "@finalout\n"
1057 "\n@node Top\n"
1058 "@unnumbered Command Summary for GNU Emacs\n\n"
1059 "@table @asis\n\n"
1060 "@iftex\n"
1061 "@global@let@ITEM@item\n"
1062 "@def@item{@filbreak@vskip5pt@ITEM}\n"
1063 "@font@tensy cmsy10 scaled @magstephalf\n"
1064 "@font@teni cmmi10 scaled @magstephalf\n"
1065 "@def\\{{@tensy@char110}}\n" ; this backslash goes with cmr10
1066 "@def|{{@tensy@char106}}\n"
1067 "@def@{{{@tensy@char102}}\n"
1068 "@def@}{{@tensy@char103}}\n"
1069 "@def<{{@teni@char62}}\n"
1070 "@def>{{@teni@char60}}\n"
1071 "@chardef@@64\n"
1072 "@catcode43=12\n"
1073 "@tableindent-0.2in\n"
1074 "@end iftex\n")
1075 ;; Sort the array by name; within each name, by type (functions first).
1076 (setq alist (sort alist (lambda (e1 e2)
1077 (if (string-equal (car e1) (car e2))
1078 (<= (cadr e1) (cadr e2))
1079 (string-lessp (car e1) (car e2))))))
1080 ;; Print each function.
1081 (dolist (e alist)
1082 (insert "\n@item "
1083 (if (char-equal (cadr e) ?\F) "Function" "Variable")
1084 " @code{" (car e) "}\n@display\n"
1085 (nth 2 e)
1086 "\n@end display\n")
1087 ;; Try to avoid a save size overflow in the TeX output routine.
1088 (if (zerop (setq i (% (1+ i) 100)))
1089 (insert "\n@end table\n@table @asis\n")))
1090 (insert "@end table\n"
1091 "@bye\n")
1092 (setq buffer-undo-list nil)
1093 (texinfo-mode)))
1094
1113ff44
MR
1095(provide 'help-fns)
1096
1113ff44 1097;;; help-fns.el ends here