Fix pcase memoizing; change lexbound byte-code marker.
[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)))
1113ff44
MR
505 (doc (documentation function))
506 (usage (help-split-fundoc doc function)))
507 (with-current-buffer standard-output
508 ;; If definition is a keymap, skip arglist note.
509 (unless (keymapp function)
8d6c1239 510 (if usage (setq doc (cdr usage)))
1113ff44 511 (let* ((use (cond
8d6c1239 512 ((and usage (not (listp advertised))) (car usage))
1113ff44
MR
513 ((listp arglist)
514 (format "%S" (help-make-usage function arglist)))
515 ((stringp arglist) arglist)
516 ;; Maybe the arglist is in the docstring of a symbol
517 ;; this one is aliased to.
518 ((let ((fun real-function))
519 (while (and (symbolp fun)
520 (setq fun (symbol-function fun))
521 (not (setq usage (help-split-fundoc
522 (documentation fun)
523 function)))))
524 usage)
525 (car usage))
526 ((or (stringp def)
527 (vectorp def))
528 (format "\nMacro: %s" (format-kbd-macro def)))
529 (t "[Missing arglist. Please make a bug report.]")))
530 (high (help-highlight-arguments use doc)))
531 (let ((fill-begin (point)))
532 (insert (car high) "\n")
ce5b520a 533 (fill-region fill-begin (point)))
b38b1ec0 534 (setq doc (cdr high))))
ce5b520a
SM
535 (let* ((obsolete (and
536 ;; function might be a lambda construct.
537 (symbolp function)
538 (get function 'byte-obsolete-info)))
539 (use (car obsolete)))
540 (when obsolete
541 (princ "\nThis function is obsolete")
542 (when (nth 2 obsolete)
543 (insert (format " since %s" (nth 2 obsolete))))
544 (insert (cond ((stringp use) (concat ";\n" use))
545 (use (format ";\nuse `%s' instead." use))
546 (t "."))
547 "\n"))
548 (insert "\n"
549 (or doc "Not documented."))))))))
1113ff44
MR
550
551\f
552;; Variables
553
554;;;###autoload
555(defun variable-at-point (&optional any-symbol)
556 "Return the bound variable symbol found at or before point.
557Return 0 if there is no such symbol.
558If ANY-SYMBOL is non-nil, don't insist the symbol be bound."
559 (with-syntax-table emacs-lisp-mode-syntax-table
560 (or (condition-case ()
561 (save-excursion
562 (or (not (zerop (skip-syntax-backward "_w")))
563 (eq (char-syntax (following-char)) ?w)
564 (eq (char-syntax (following-char)) ?_)
565 (forward-sexp -1))
566 (skip-chars-forward "'")
567 (let ((obj (read (current-buffer))))
568 (and (symbolp obj) (boundp obj) obj)))
569 (error nil))
570 (let* ((str (find-tag-default))
571 (sym (if str (intern-soft str))))
572 (if (and sym (or any-symbol (boundp sym)))
573 sym
574 (save-match-data
575 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
576 (setq sym (intern-soft (match-string 1 str)))
577 (and (or any-symbol (boundp sym)) sym)))))
578 0)))
579
580(defun describe-variable-custom-version-info (variable)
581 (let ((custom-version (get variable 'custom-version))
582 (cpv (get variable 'custom-package-version))
583 (output nil))
584 (if custom-version
585 (setq output
586 (format "This variable was introduced, or its default value was changed, in\nversion %s of Emacs.\n"
587 custom-version))
588 (when cpv
589 (let* ((package (car-safe cpv))
590 (version (if (listp (cdr-safe cpv))
591 (car (cdr-safe cpv))
592 (cdr-safe cpv)))
593 (pkg-versions (assq package customize-package-emacs-version-alist))
594 (emacsv (cdr (assoc version pkg-versions))))
595 (if (and package version)
596 (setq output
597 (format (concat "This variable was introduced, or its default value was changed, in\nversion %s of the %s package"
598 (if emacsv
599 (format " that is part of Emacs %s" emacsv))
600 ".\n")
601 version package))))))
602 output))
603
604;;;###autoload
605(defun describe-variable (variable &optional buffer frame)
606 "Display the full documentation of VARIABLE (a symbol).
607Returns the documentation as a string, also.
608If VARIABLE has a buffer-local value in BUFFER or FRAME
609\(default to the current buffer and current frame),
610it is displayed along with the global value."
611 (interactive
612 (let ((v (variable-at-point))
613 (enable-recursive-minibuffers t)
614 val)
615 (setq val (completing-read (if (symbolp v)
616 (format
617 "Describe variable (default %s): " v)
618 "Describe variable: ")
619 obarray
620 '(lambda (vv)
621 (or (boundp vv)
622 (get vv 'variable-documentation)))
623 t nil nil
624 (if (symbolp v) (symbol-name v))))
625 (list (if (equal val "")
626 v (intern val)))))
627 (let (file-name)
628 (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
629 (unless (frame-live-p frame) (setq frame (selected-frame)))
630 (if (not (symbolp variable))
631 (message "You did not specify a variable")
632 (save-excursion
633 (let ((valvoid (not (with-current-buffer buffer (boundp variable))))
634 val val-start-pos locus)
635 ;; Extract the value before setting up the output buffer,
636 ;; in case `buffer' *is* the output buffer.
637 (unless valvoid
638 (with-selected-frame frame
639 (with-current-buffer buffer
640 (setq val (symbol-value variable)
641 locus (variable-binding-locus variable)))))
642 (help-setup-xref (list #'describe-variable variable buffer)
32226619 643 (called-interactively-p 'interactive))
1113ff44
MR
644 (with-help-window (help-buffer)
645 (with-current-buffer buffer
646 (prin1 variable)
647 (setq file-name (find-lisp-object-file-name variable 'defvar))
648
649 (if file-name
650 (progn
651 (princ " is a variable defined in `")
e8e8dbf9
MR
652 (princ (if (eq file-name 'C-source)
653 "C source code"
654 (file-name-nondirectory file-name)))
1113ff44
MR
655 (princ "'.\n")
656 (with-current-buffer standard-output
657 (save-excursion
658 (re-search-backward "`\\([^`']+\\)'" nil t)
659 (help-xref-button 1 'help-variable-def
660 variable file-name)))
661 (if valvoid
662 (princ "It is void as a variable.")
663 (princ "Its ")))
664 (if valvoid
665 (princ " is void as a variable.")
666 (princ "'s "))))
3c505d31 667 (unless valvoid
1113ff44
MR
668 (with-current-buffer standard-output
669 (setq val-start-pos (point))
670 (princ "value is ")
1113ff44 671 (let ((from (point)))
3c505d31 672 (terpri)
1113ff44 673 (pp val)
e1a23575 674 (if (< (point) (+ 68 (line-beginning-position 0)))
3c505d31 675 (delete-region from (1+ from))
7c420169 676 (delete-region (1- from) from))
0097720d
SM
677 (let* ((sv (get variable 'standard-value))
678 (origval (and (consp sv)
679 (condition-case nil
680 (eval (car sv))
681 (error :help-eval-error)))))
682 (when (and (consp sv)
683 (not (equal origval val))
684 (not (equal origval :help-eval-error)))
685 (princ "\nOriginal value was \n")
686 (setq from (point))
687 (pp origval)
688 (if (< (point) (+ from 20))
689 (delete-region (1- from) from)))))))
1113ff44 690 (terpri)
1113ff44
MR
691 (when locus
692 (if (bufferp locus)
693 (princ (format "%socal in buffer %s; "
694 (if (get variable 'permanent-local)
695 "Permanently l" "L")
696 (buffer-name)))
697 (princ (format "It is a frame-local variable; ")))
698 (if (not (default-boundp variable))
699 (princ "globally void")
700 (let ((val (default-value variable)))
701 (with-current-buffer standard-output
702 (princ "global value is ")
703 (terpri)
704 ;; Fixme: pp can take an age if you happen to
705 ;; ask for a very large expression. We should
706 ;; probably print it raw once and check it's a
707 ;; sensible size before prettyprinting. -- fx
708 (let ((from (point)))
709 (pp val)
710 ;; See previous comment for this function.
711 ;; (help-xref-on-pp from (point))
712 (if (< (point) (+ from 20))
713 (delete-region (1- from) from))))))
714 (terpri))
715
716 ;; If the value is large, move it to the end.
717 (with-current-buffer standard-output
718 (when (> (count-lines (point-min) (point-max)) 10)
719 ;; Note that setting the syntax table like below
720 ;; makes forward-sexp move over a `'s' at the end
721 ;; of a symbol.
722 (set-syntax-table emacs-lisp-mode-syntax-table)
723 (goto-char val-start-pos)
724 ;; The line below previously read as
725 ;; (delete-region (point) (progn (end-of-line) (point)))
726 ;; which suppressed display of the buffer local value for
727 ;; large values.
728 (when (looking-at "value is") (replace-match ""))
729 (save-excursion
730 (insert "\n\nValue:")
731 (set (make-local-variable 'help-button-cache)
732 (point-marker)))
733 (insert "value is shown ")
734 (insert-button "below"
735 'action help-button-cache
736 'follow-link t
737 'help-echo "mouse-2, RET: show value")
738 (insert ".\n")))
739 (terpri)
740
741 (let* ((alias (condition-case nil
742 (indirect-variable variable)
743 (error variable)))
744 (obsolete (get variable 'byte-obsolete-variable))
745 (use (car obsolete))
746 (safe-var (get variable 'safe-local-variable))
747 (doc (or (documentation-property variable 'variable-documentation)
748 (documentation-property alias 'variable-documentation)))
749 (extra-line nil))
750 ;; Add a note for variables that have been make-var-buffer-local.
751 (when (and (local-variable-if-set-p variable)
752 (or (not (local-variable-p variable))
753 (with-temp-buffer
754 (local-variable-if-set-p variable))))
755 (setq extra-line t)
756 (princ " Automatically becomes buffer-local when set in any fashion.\n"))
757
758 ;; Mention if it's an alias
759 (unless (eq alias variable)
760 (setq extra-line t)
761 (princ (format " This variable is an alias for `%s'.\n" alias)))
762
763 (when obsolete
764 (setq extra-line t)
765 (princ " This variable is obsolete")
766 (if (cdr obsolete) (princ (format " since %s" (cdr obsolete))))
767 (princ (cond ((stringp use) (concat ";\n " use))
768 (use (format ";\n use `%s' instead." (car obsolete)))
769 (t ".")))
770 (terpri))
2ee20f24
JL
771
772 (when (member (cons variable val) file-local-variables-alist)
773 (setq extra-line t)
774 (if (member (cons variable val) dir-local-variables-alist)
775 (let ((file (and (buffer-file-name)
776 (not (file-remote-p (buffer-file-name)))
777 (dir-locals-find-file (buffer-file-name)))))
778 (princ " This variable is a directory local variable")
b6ce92f1
CY
779 (when file
780 (princ (concat "\n from the file \""
781 (if (consp file)
782 (car file)
783 file)
784 "\"")))
2ee20f24
JL
785 (princ ".\n"))
786 (princ " This variable is a file local variable.\n")))
787
589a99f2
GM
788 (when (memq variable ignored-local-variables)
789 (setq extra-line t)
790 (princ " This variable is ignored when used as a file local \
791variable.\n"))
792
793 ;; Can be both risky and safe, eg auto-fill-function.
794 (when (risky-local-variable-p variable)
795 (setq extra-line t)
796 (princ " This variable is potentially risky when used as a \
797file local variable.\n")
798 (when (assq variable safe-local-variable-values)
799 (princ " However, you have added it to \
800`safe-local-variable-values'.\n")))
801
1113ff44
MR
802 (when safe-var
803 (setq extra-line t)
804 (princ " This variable is safe as a file local variable ")
805 (princ "if its value\n satisfies the predicate ")
806 (princ (if (byte-code-function-p safe-var)
807 "which is byte-compiled expression.\n"
808 (format "`%s'.\n" safe-var))))
809
810 (if extra-line (terpri))
811 (princ "Documentation:\n")
812 (with-current-buffer standard-output
813 (insert (or doc "Not documented as a variable."))))
814
815 ;; Make a link to customize if this variable can be customized.
816 (when (custom-variable-p variable)
817 (let ((customize-label "customize"))
818 (terpri)
819 (terpri)
820 (princ (concat "You can " customize-label " this variable."))
821 (with-current-buffer standard-output
822 (save-excursion
823 (re-search-backward
824 (concat "\\(" customize-label "\\)") nil t)
825 (help-xref-button 1 'help-customize-variable variable))))
826 ;; Note variable's version or package version
827 (let ((output (describe-variable-custom-version-info variable)))
828 (when output
829 (terpri)
830 (terpri)
831 (princ output))))
832
7fdbcd83 833 (with-current-buffer standard-output
1113ff44
MR
834 ;; Return the text we displayed.
835 (buffer-string))))))))
836
837
838;;;###autoload
839(defun describe-syntax (&optional buffer)
840 "Describe the syntax specifications in the syntax table of BUFFER.
841The descriptions are inserted in a help buffer, which is then displayed.
842BUFFER defaults to the current buffer."
843 (interactive)
844 (setq buffer (or buffer (current-buffer)))
32226619
JB
845 (help-setup-xref (list #'describe-syntax buffer)
846 (called-interactively-p 'interactive))
1113ff44
MR
847 (with-help-window (help-buffer)
848 (let ((table (with-current-buffer buffer (syntax-table))))
849 (with-current-buffer standard-output
850 (describe-vector table 'internal-describe-syntax-value)
851 (while (setq table (char-table-parent table))
852 (insert "\nThe parent syntax table is:")
853 (describe-vector table 'internal-describe-syntax-value))))))
854
855(defun help-describe-category-set (value)
856 (insert (cond
857 ((null value) "default")
858 ((char-table-p value) "deeper char-table ...")
859 (t (condition-case err
860 (category-set-mnemonics value)
861 (error "invalid"))))))
862
863;;;###autoload
864(defun describe-categories (&optional buffer)
865 "Describe the category specifications in the current category table.
866The descriptions are inserted in a buffer, which is then displayed.
867If BUFFER is non-nil, then describe BUFFER's category table instead.
868BUFFER should be a buffer or a buffer name."
869 (interactive)
870 (setq buffer (or buffer (current-buffer)))
32226619
JB
871 (help-setup-xref (list #'describe-categories buffer)
872 (called-interactively-p 'interactive))
1113ff44 873 (with-help-window (help-buffer)
c6ec96f8
KH
874 (let* ((table (with-current-buffer buffer (category-table)))
875 (docs (char-table-extra-slot table 0)))
876 (if (or (not (vectorp docs)) (/= (length docs) 95))
877 (error "Invalid first extra slot in this category table\n"))
1113ff44 878 (with-current-buffer standard-output
c6ec96f8
KH
879 (insert "Legend of category mnemonics (see the tail for the longer description)\n")
880 (let ((pos (point)) (items 0) lines n)
881 (dotimes (i 95)
882 (if (aref docs i) (setq items (1+ items))))
883 (setq lines (1+ (/ (1- items) 4)))
884 (setq n 0)
885 (dotimes (i 95)
886 (let ((elt (aref docs i)))
887 (when elt
888 (string-match ".*" elt)
889 (setq elt (match-string 0 elt))
890 (if (>= (length elt) 17)
891 (setq elt (concat (substring elt 0 14) "...")))
892 (if (< (point) (point-max))
893 (move-to-column (* 20 (/ n lines)) t))
894 (insert (+ i ?\s) ?: elt)
895 (if (< (point) (point-max))
896 (forward-line 1)
897 (insert "\n"))
898 (setq n (1+ n))
899 (if (= (% n lines) 0)
900 (goto-char pos))))))
901 (goto-char (point-max))
902 (insert "\n"
903 "character(s)\tcategory mnemonics\n"
904 "------------\t------------------")
1113ff44 905 (describe-vector table 'help-describe-category-set)
c6ec96f8
KH
906 (insert "Legend of category mnemonics:\n")
907 (dotimes (i 95)
908 (let ((elt (aref docs i)))
909 (when elt
910 (if (string-match "\n" elt)
911 (setq elt (substring elt (match-end 0))))
912 (insert (+ i ?\s) ": " elt "\n"))))
913 (while (setq table (char-table-parent table))
914 (insert "\nThe parent category table is:")
915 (describe-vector table 'help-describe-category-set))))))
1113ff44 916
17284e30
GM
917\f
918;;; Replacements for old lib-src/ programs. Don't seem especially useful.
919
920;; Replaces lib-src/digest-doc.c.
921;;;###autoload
922(defun doc-file-to-man (file)
923 "Produce an nroff buffer containing the doc-strings from the DOC file."
924 (interactive (list (read-file-name "Name of DOC file: " doc-directory
925 internal-doc-file-name t)))
926 (or (file-readable-p file)
927 (error "Cannot read file `%s'" file))
928 (pop-to-buffer (generate-new-buffer "*man-doc*"))
929 (setq buffer-undo-list t)
930 (insert ".TH \"Command Summary for GNU Emacs\"\n"
931 ".AU Richard M. Stallman\n")
932 (insert-file-contents file)
933 (let (notfirst)
934 (while (search-forward "\1f" nil 'move)
935 (if (looking-at "S")
936 (delete-region (1- (point)) (line-end-position))
937 (delete-char -1)
938 (if notfirst
939 (insert "\n.DE\n")
940 (setq notfirst t))
941 (insert "\n.SH ")
942 (insert (if (looking-at "F") "Function " "Variable "))
943 (delete-char 1)
944 (forward-line 1)
945 (insert ".DS L\n"))))
946 (insert "\n.DE\n")
947 (setq buffer-undo-list nil)
948 (nroff-mode))
949
950;; Replaces lib-src/sorted-doc.c.
951;;;###autoload
952(defun doc-file-to-info (file)
953 "Produce a texinfo buffer with sorted doc-strings from the DOC file."
954 (interactive (list (read-file-name "Name of DOC file: " doc-directory
955 internal-doc-file-name t)))
956 (or (file-readable-p file)
957 (error "Cannot read file `%s'" file))
958 (let ((i 0) type name doc alist)
959 (with-temp-buffer
960 (insert-file-contents file)
961 ;; The characters "@{}" need special treatment.
962 (while (re-search-forward "[@{}]" nil t)
963 (backward-char)
964 (insert "@")
965 (forward-char 1))
966 (goto-char (point-min))
967 (while (search-forward "\1f" nil t)
968 (unless (looking-at "S")
969 (setq type (char-after)
970 name (buffer-substring (1+ (point)) (line-end-position))
971 doc (buffer-substring (line-beginning-position 2)
972 (if (search-forward "\1f" nil 'move)
973 (1- (point))
974 (point)))
975 alist (cons (list name type doc) alist))
976 (backward-char 1))))
977 (pop-to-buffer (generate-new-buffer "*info-doc*"))
978 (setq buffer-undo-list t)
979 ;; Write the output header.
980 (insert "\\input texinfo @c -*-texinfo-*-\n"
981 "@setfilename emacsdoc.info\n"
982 "@settitle Command Summary for GNU Emacs\n"
983 "@finalout\n"
984 "\n@node Top\n"
985 "@unnumbered Command Summary for GNU Emacs\n\n"
986 "@table @asis\n\n"
987 "@iftex\n"
988 "@global@let@ITEM@item\n"
989 "@def@item{@filbreak@vskip5pt@ITEM}\n"
990 "@font@tensy cmsy10 scaled @magstephalf\n"
991 "@font@teni cmmi10 scaled @magstephalf\n"
992 "@def\\{{@tensy@char110}}\n" ; this backslash goes with cmr10
993 "@def|{{@tensy@char106}}\n"
994 "@def@{{{@tensy@char102}}\n"
995 "@def@}{{@tensy@char103}}\n"
996 "@def<{{@teni@char62}}\n"
997 "@def>{{@teni@char60}}\n"
998 "@chardef@@64\n"
999 "@catcode43=12\n"
1000 "@tableindent-0.2in\n"
1001 "@end iftex\n")
1002 ;; Sort the array by name; within each name, by type (functions first).
1003 (setq alist (sort alist (lambda (e1 e2)
1004 (if (string-equal (car e1) (car e2))
1005 (<= (cadr e1) (cadr e2))
1006 (string-lessp (car e1) (car e2))))))
1007 ;; Print each function.
1008 (dolist (e alist)
1009 (insert "\n@item "
1010 (if (char-equal (cadr e) ?\F) "Function" "Variable")
1011 " @code{" (car e) "}\n@display\n"
1012 (nth 2 e)
1013 "\n@end display\n")
1014 ;; Try to avoid a save size overflow in the TeX output routine.
1015 (if (zerop (setq i (% (1+ i) 100)))
1016 (insert "\n@end table\n@table @asis\n")))
1017 (insert "@end table\n"
1018 "@bye\n")
1019 (setq buffer-undo-list nil)
1020 (texinfo-mode)))
1021
1113ff44
MR
1022(provide 'help-fns)
1023
1113ff44 1024;;; help-fns.el ends here