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