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