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