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