1 ;;; help-fns.el --- Complex help functions
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001,
4 ;; 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
7 ;; Keywords: help, internal
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; This file contains those help commands which are complicated, and
29 ;; which may not be used in every session. For example
30 ;; `describe-function' will probably be heavily used when doing elisp
31 ;; programming, but not if just editing C files. Simpler help commands
41 (defun describe-function (function)
42 "Display the full documentation of FUNCTION (a symbol)."
44 (let ((fn (function-called-at-point))
45 (enable-recursive-minibuffers t)
47 (setq val (completing-read (if fn
48 (format "Describe function (default %s): " fn)
49 "Describe function: ")
50 obarray 'fboundp t nil nil
51 (and fn (symbol-name fn))))
52 (list (if (equal val "")
55 (message "You didn't specify a function")
56 (help-setup-xref (list #'describe-function function) (interactive-p))
58 (with-output-to-temp-buffer (help-buffer)
60 ;; Use " is " instead of a colon so that
61 ;; it is easier to get out the function name using forward-sexp.
63 (describe-function-1 function)
64 (print-help-return-message)
65 (with-current-buffer standard-output
66 ;; Return the text we displayed.
69 (defun help-split-fundoc (docstring def)
70 "Split a function DOCSTRING into the actual doc and the usage info.
71 Return (USAGE . DOC) or nil if there's no usage info.
72 DEF is the function whose usage we're looking for in DOCSTRING."
73 ;; Functions can get the calling sequence at the end of the doc string.
74 ;; In cases where `function' has been fset to a subr we can't search for
75 ;; function's name in the doc string so we use `fn' as the anonymous
76 ;; function name instead.
77 (when (and docstring (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring))
79 ;; Replace `fn' with the actual function name.
80 (if (consp def) "anonymous" def)
81 (match-string 1 docstring))
82 (substring docstring 0 (match-beginning 0)))))
84 (defun help-add-fundoc-usage (docstring arglist)
85 "Add the usage info to DOCSTRING.
86 If DOCSTRING already has a usage info, then just return it unchanged.
87 The usage info is built from ARGLIST. DOCSTRING can be nil.
88 ARGLIST can also be t or a string of the form \"(FUN ARG1 ARG2 ...)\"."
89 (unless (stringp docstring) (setq docstring "Not documented"))
90 (if (or (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring) (eq arglist t))
93 (if (string-match "\n?\n\\'" docstring)
94 (if (< (- (match-end 0) (match-beginning 0)) 2) "\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))))))
101 (defun help-function-arglist (def)
102 ;; Handle symbols aliased to other symbols.
103 (if (and (symbolp def) (fboundp def)) (setq def (indirect-function def)))
104 ;; If definition is a macro, find the function inside it.
105 (if (eq (car-safe def) 'macro) (setq def (cdr def)))
107 ((byte-code-function-p def) (aref def 0))
108 ((eq (car-safe def) 'lambda) (nth 1 def))
109 ((and (eq (car-safe def) 'autoload) (not (eq (nth 4 def) 'keymap)))
110 "[Arg list not available until function definition is loaded.]")
113 (defun help-make-usage (function arglist)
114 (cons (if (symbolp function) function 'anonymous)
115 (mapcar (lambda (arg)
116 (if (not (symbolp arg))
117 (if (and (consp arg) (symbolp (car arg)))
118 ;; CL style default values for optional args.
119 (cons (intern (upcase (symbol-name (car arg))))
122 (let ((name (symbol-name arg)))
123 (if (string-match "\\`&" name) arg
124 (intern (upcase name))))))
127 ;; Could be this, if we make symbol-file do the work below.
128 ;; (defun help-C-file-name (subr-or-var kind)
129 ;; "Return the name of the C file where SUBR-OR-VAR is defined.
130 ;; KIND should be `var' for a variable or `subr' for a subroutine."
131 ;; (symbol-file (if (symbolp subr-or-var) subr-or-var
132 ;; (subr-name subr-or-var))
133 ;; (if (eq kind 'var) 'defvar 'defun)))
135 (defun help-C-file-name (subr-or-var kind)
136 "Return the name of the C file where SUBR-OR-VAR is defined.
137 KIND should be `var' for a variable or `subr' for a subroutine."
138 (let ((docbuf (get-buffer-create " *DOC*"))
139 (name (if (eq 'var kind)
140 (concat "V" (symbol-name subr-or-var))
141 (concat "F" (subr-name subr-or-var)))))
142 (with-current-buffer docbuf
143 (goto-char (point-min))
145 (insert-file-contents-literally
146 (expand-file-name internal-doc-file-name doc-directory)))
147 (let ((file (catch 'loop
149 (let ((pnt (search-forward (concat "
\1f" name "\n"))))
150 (re-search-backward "
\1fS\\(.*\\)")
151 (let ((file (match-string 1)))
152 (if (member file build-files)
154 (goto-char pnt))))))))
155 (if (string-match "\\.\\(o\\|obj\\)\\'" file)
156 (setq file (replace-match ".c" t t file)))
157 (if (string-match "\\.c\\'" file)
161 (defface help-argument-name '((((supports :slant italic)) :inherit italic))
162 "Face to highlight argument names in *Help* buffers."
165 (defun help-default-arg-highlight (arg)
166 "Default function to highlight arguments in *Help* buffers.
167 It returns ARG in face `help-argument-name'; ARG is also
168 downcased if it displays differently than the default
169 face (according to `face-differs-from-default-p')."
170 (propertize (if (face-differs-from-default-p 'help-argument-name)
173 'face 'help-argument-name))
175 (defun help-do-arg-highlight (doc args)
176 (with-syntax-table (make-syntax-table emacs-lisp-mode-syntax-table)
177 (modify-syntax-entry ?\- "w")
178 (dolist (arg args doc)
179 (setq doc (replace-regexp-in-string
180 ;; This is heuristic, but covers all common cases
182 (concat "\\<" ; beginning of word
183 "\\(?:[a-z-]*-\\)?" ; for xxx-ARG
187 "\\(?:es\\|s\\|th\\)?" ; for ARGth, ARGs
188 "\\(?:-[a-z0-9-]+\\)?" ; for ARG-xxx, ARG-n
189 "\\(?:-[{([<`\"].*?\\)?"; for ARG-{x}, (x), <x>, [x], `x'
191 (help-default-arg-highlight arg)
194 (defun help-highlight-arguments (usage doc &rest args)
198 (goto-char (point-min))
199 (let ((case-fold-search nil)
200 (next (not (or args (looking-at "\\["))))
202 ;; Make a list of all arguments
203 (skip-chars-forward "^ ")
205 (or opt (not (looking-at " &")) (setq opt t))
206 (if (not (re-search-forward " \\([\\[(]*\\)\\([^] &)\.]+\\)" nil t))
208 (setq args (cons (match-string 2) args))
209 (when (and opt (string= (match-string 1) "("))
210 ;; A pesky CL-style optional argument with default value,
211 ;; so let's skip over it
212 (search-backward "(")
213 (goto-char (scan-sexps (point) 1)))))
214 ;; Highlight aguments in the USAGE string
215 (setq usage (help-do-arg-highlight (buffer-string) args))
216 ;; Highlight arguments in the DOC string
217 (setq doc (and doc (help-do-arg-highlight doc args))))))
218 ;; Return value is like the one from help-split-fundoc, but highlighted
222 (defun describe-simplify-lib-file-name (file)
223 "Simplify a library name FILE to a relative name, and make it a source file."
225 ;; Try converting the absolute file name to a library name.
226 (let ((libname (file-name-nondirectory file)))
227 ;; Now convert that back to a file name and see if we get
228 ;; the original one. If so, they are equivalent.
229 (if (equal file (locate-file libname load-path '("")))
230 (if (string-match "[.]elc\\'" libname)
231 (substring libname 0 -1)
235 (defun find-source-lisp-file (file-name)
236 (let* ((elc-file (locate-file (concat file-name
237 (if (string-match "\\.el" file-name)
241 (str (if (and elc-file (file-readable-p elc-file))
243 (insert-file-contents-literally elc-file nil 0 256)
246 (string-match ";;; from file \\(.*\\.el\\)" str)
247 (match-string 1 str))))
248 (if (and src-file (file-readable-p src-file))
253 (defun describe-function-1 (function)
254 (let* ((advised (and (featurep 'advice) (ad-get-advice-info function)))
255 ;; If the function is advised, use the symbol that has the
256 ;; real definition, if that symbol is already set up.
259 (cdr (assq 'origname advised))
260 (fboundp (cdr (assq 'origname advised)))
261 (cdr (assq 'origname advised)))
263 ;; Get the real definition.
264 (def (if (symbolp real-function)
265 (symbol-function real-function)
268 (beg (if (commandp def) "an interactive " "a ")))
270 (cond ((or (stringp def)
274 (if (eq 'unevalled (cdr (subr-arity def)))
275 (concat beg "special form")
276 (concat beg "built-in function")))
277 ((byte-code-function-p def)
278 (concat beg "compiled Lisp function"))
280 (while (symbolp (symbol-function def))
281 (setq def (symbol-function def)))
282 (format "an alias for `%s'" def))
283 ((eq (car-safe def) 'lambda)
284 (concat beg "Lisp function"))
285 ((eq (car-safe def) 'macro)
287 ((eq (car-safe def) 'autoload)
288 (setq file-name (nth 1 def))
289 (format "%s autoloaded %s"
290 (if (commandp def) "an interactive" "an")
291 (if (eq (nth 4 def) 'keymap) "keymap"
292 (if (nth 4 def) "Lisp macro" "Lisp function"))
296 (elts (cdr-safe def)))
298 (if (char-table-p (car-safe elts))
301 (setq elts (cdr-safe elts)))
307 (with-current-buffer standard-output
310 (if (re-search-backward "alias for `\\([^`']+\\)'" nil t)
311 (help-xref-button 1 'help-function def)))))
313 (setq file-name (symbol-file function 'defun)))
314 (setq file-name (describe-simplify-lib-file-name file-name))
315 (when (equal file-name "loaddefs.el")
316 ;; Find the real def site of the preloaded function.
317 ;; This is necessary only for defaliases.
320 (find-function-search-for-symbol function nil "loaddefs.el")
323 (with-current-buffer (car location)
324 (goto-char (cdr location))
325 (when (re-search-backward
326 "^;;; Generated autoloads from \\(.*\\)" nil t)
327 (setq file-name (match-string 1)))))))
328 (when (and (null file-name) (subrp def))
329 ;; Find the C source file name.
330 (setq file-name (if (get-buffer " *DOC*")
331 (help-C-file-name def 'subr)
335 ;; We used to add .el to the file name,
336 ;; but that's completely wrong when the user used load-file.
337 (princ (if (eq file-name 'C-source) "C source code" file-name))
339 ;; See if lisp files are present where they where installed from.
340 (if (not (eq file-name 'C-source))
341 (setq file-name (find-source-lisp-file file-name)))
343 ;; Make a hyperlink to the library.
344 (with-current-buffer standard-output
346 (re-search-backward "`\\([^`']+\\)'" nil t)
347 (help-xref-button 1 'help-function-def real-function file-name))))
350 (when (commandp function)
351 (if (and (eq function 'self-insert-command)
352 (eq (key-binding "a") 'self-insert-command)
353 (eq (key-binding "b") 'self-insert-command)
354 (eq (key-binding "c") 'self-insert-command))
355 (princ "It is bound to many ordinary text characters.\n")
356 (let* ((remapped (command-remapping function))
357 (keys (where-is-internal
358 (or remapped function) overriding-local-map nil nil))
360 ;; Which non-control non-meta keys run this command?
362 (if (member (event-modifiers (aref key 0)) '(nil (shift)))
363 (push key non-modified-keys)))
365 (princ "It is remapped to `")
366 (princ (symbol-name remapped))
370 (princ (if remapped " which is bound to " "It is bound to "))
371 ;; If lots of ordinary text characters run this command,
372 ;; don't mention them one by one.
373 (if (< (length non-modified-keys) 10)
374 (princ (mapconcat 'key-description keys ", "))
375 (dolist (key non-modified-keys)
376 (setq keys (delq key keys)))
379 (princ (mapconcat 'key-description keys ", "))
380 (princ ", and many ordinary text characters"))
381 (princ "many ordinary text characters"))))
382 (when (or remapped keys non-modified-keys)
385 (let* ((arglist (help-function-arglist def))
386 (doc (documentation function))
387 (usage (help-split-fundoc doc function)))
388 (with-current-buffer standard-output
389 ;; If definition is a keymap, skip arglist note.
390 (unless (keymapp def)
392 (usage (setq doc (cdr usage)) (car usage))
394 (format "%S" (help-make-usage function arglist)))
395 ((stringp arglist) arglist)
396 ;; Maybe the arglist is in the docstring of a symbol
397 ;; this one is aliased to.
398 ((let ((fun real-function))
399 (while (and (symbolp fun)
400 (setq fun (symbol-function fun))
401 (not (setq usage (help-split-fundoc
408 (format "\nMacro: %s" (format-kbd-macro def)))
409 (t "[Missing arglist. Please make a bug report.]")))
410 (high (help-highlight-arguments use doc)))
411 (let ((fill-begin (point)))
412 (insert (car high) "\n")
413 (fill-region fill-begin (point)))
414 (setq doc (cdr high))))
416 ;; function might be a lambda construct.
418 (get function 'byte-obsolete-info))))
420 (princ "\nThis function is obsolete")
421 (when (nth 2 obsolete)
422 (insert (format " since %s" (nth 2 obsolete))))
424 (if (stringp (car obsolete)) (car obsolete)
425 (format "use `%s' instead." (car obsolete)))
428 (or doc "Not documented.")))))))
434 (defun variable-at-point (&optional any-symbol)
435 "Return the bound variable symbol found at or before point.
436 Return 0 if there is no such symbol.
437 If ANY-SYMBOL is non-nil, don't insist the symbol be bound."
438 (or (condition-case ()
439 (with-syntax-table emacs-lisp-mode-syntax-table
441 (or (not (zerop (skip-syntax-backward "_w")))
442 (eq (char-syntax (following-char)) ?w)
443 (eq (char-syntax (following-char)) ?_)
445 (skip-chars-forward "'")
446 (let ((obj (read (current-buffer))))
447 (and (symbolp obj) (boundp obj) obj))))
449 (let* ((str (find-tag-default))
450 (sym (if str (intern-soft str))))
451 (if (and sym (or any-symbol (boundp sym)))
454 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
455 (setq sym (intern-soft (match-string 1 str)))
456 (and (or any-symbol (boundp sym)) sym)))))
460 (defun describe-variable (variable &optional buffer frame)
461 "Display the full documentation of VARIABLE (a symbol).
462 Returns the documentation as a string, also.
463 If VARIABLE has a buffer-local value in BUFFER or FRAME
464 \(default to the current buffer and current frame),
465 it is displayed along with the global value."
467 (let ((v (variable-at-point))
468 (enable-recursive-minibuffers t)
470 (setq val (completing-read (if (symbolp v)
472 "Describe variable (default %s): " v)
473 "Describe variable: ")
477 (get vv 'variable-documentation)))
479 (if (symbolp v) (symbol-name v))))
480 (list (if (equal val "")
482 (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
483 (unless (frame-live-p frame) (setq frame (selected-frame)))
484 (if (not (symbolp variable))
485 (message "You did not specify a variable")
487 (let ((valvoid (not (with-current-buffer buffer (boundp variable))))
488 val val-start-pos locus)
489 ;; Extract the value before setting up the output buffer,
490 ;; in case `buffer' *is* the output buffer.
492 (with-selected-frame frame
493 (with-current-buffer buffer
494 (setq val (symbol-value variable)
495 locus (variable-binding-locus variable)))))
496 (help-setup-xref (list #'describe-variable variable buffer)
498 (with-output-to-temp-buffer (help-buffer)
499 (with-current-buffer buffer
501 ;; Make a hyperlink to the library if appropriate. (Don't
502 ;; change the format of the buffer's initial line in case
503 ;; anything expects the current format.)
504 (let ((file-name (symbol-file variable 'defvar)))
505 (setq file-name (describe-simplify-lib-file-name file-name))
506 (when (equal file-name "loaddefs.el")
507 ;; Find the real def site of the preloaded variable.
510 (find-variable-noselect variable file-name)
513 (with-current-buffer (car location)
515 (goto-char (cdr location)))
516 (when (re-search-backward
517 "^;;; Generated autoloads from \\(.*\\)" nil t)
518 (setq file-name (match-string 1)))))))
519 (when (and (null file-name)
520 (integerp (get variable 'variable-documentation)))
521 ;; It's a variable not defined in Elisp but in C.
523 (if (get-buffer " *DOC*")
524 (help-C-file-name variable 'var)
528 (princ " is a variable defined in `")
529 (princ (if (eq file-name 'C-source) "C source code" file-name))
531 (with-current-buffer standard-output
533 (re-search-backward "`\\([^`']+\\)'" nil t)
534 (help-xref-button 1 'help-variable-def
535 variable file-name)))
537 (princ "It is void as a variable.")
540 (princ " is void as a variable.")
544 (with-current-buffer standard-output
545 (setq val-start-pos (point))
548 (let ((from (point)))
550 ;; Hyperlinks in variable's value are quite frequently
551 ;; inappropriate e.g C-h v <RET> features <RET>
552 ;; (help-xref-on-pp from (point))
553 (if (< (point) (+ from 20))
554 (delete-region (1- from) from)))))
559 (princ (format "%socal in buffer %s; "
560 (if (get variable 'permanent-local)
563 (princ (format "It is a frame-local variable; ")))
564 (if (not (default-boundp variable))
565 (princ "globally void")
566 (let ((val (default-value variable)))
567 (with-current-buffer standard-output
568 (princ "global value is ")
570 ;; Fixme: pp can take an age if you happen to
571 ;; ask for a very large expression. We should
572 ;; probably print it raw once and check it's a
573 ;; sensible size before prettyprinting. -- fx
574 (let ((from (point)))
576 ;; See previous comment for this function.
577 ;; (help-xref-on-pp from (point))
578 (if (< (point) (+ from 20))
579 (delete-region (1- from) from))))))
582 ;; If the value is large, move it to the end.
583 (with-current-buffer standard-output
584 (when (> (count-lines (point-min) (point-max)) 10)
585 ;; Note that setting the syntax table like below
586 ;; makes forward-sexp move over a `'s' at the end
588 (set-syntax-table emacs-lisp-mode-syntax-table)
589 (goto-char val-start-pos)
590 ;; The line below previously read as
591 ;; (delete-region (point) (progn (end-of-line) (point)))
592 ;; which suppressed display of the buffer local value for
594 (when (looking-at "value is") (replace-match ""))
596 (insert "\n\nValue:")
597 (set (make-local-variable 'help-button-cache)
599 (insert "value is shown ")
600 (insert-button "below"
601 'action help-button-cache
603 'help-echo "mouse-2, RET: show value")
607 (let* ((alias (condition-case nil
608 (indirect-variable variable)
610 (obsolete (get variable 'byte-obsolete-variable))
611 (safe-var (get variable 'safe-local-variable))
612 (doc (or (documentation-property variable 'variable-documentation)
613 (documentation-property alias 'variable-documentation)))
615 ;; Add a note for variables that have been make-var-buffer-local.
616 (when (and (local-variable-if-set-p variable)
617 (or (not (local-variable-p variable))
619 (local-variable-if-set-p variable))))
621 (princ " Automatically becomes buffer-local when set in any fashion.\n"))
623 ;; Mention if it's an alias
624 (unless (eq alias variable)
626 (princ (format " This variable is an alias for `%s'.\n" alias)))
630 (princ " This variable is obsolete")
631 (if (cdr obsolete) (princ (format " since %s" (cdr obsolete))))
633 (princ (if (stringp (car obsolete)) (car obsolete)
634 (format "use `%s' instead." (car obsolete))))
638 (princ " This variable is safe as a file local variable ")
639 (princ "if its value\n satisfies the predicate ")
640 (princ (if (byte-code-function-p safe-var)
641 "which is byte-compiled expression.\n"
642 (format "`%s'.\n" safe-var))))
644 (if extra-line (terpri))
645 (princ "Documentation:\n")
646 (with-current-buffer standard-output
647 (insert (or doc "Not documented as a variable."))))
648 ;; Make a link to customize if this variable can be customized.
649 (if (custom-variable-p variable)
650 (let ((customize-label "customize"))
653 (princ (concat "You can " customize-label " this variable."))
654 (with-current-buffer standard-output
657 (concat "\\(" customize-label "\\)") nil t)
658 (help-xref-button 1 'help-customize-variable variable)))))
659 (print-help-return-message)
661 (set-buffer standard-output)
662 ;; Return the text we displayed.
663 (buffer-string))))))))
667 (defun describe-syntax (&optional buffer)
668 "Describe the syntax specifications in the syntax table of BUFFER.
669 The descriptions are inserted in a help buffer, which is then displayed.
670 BUFFER defaults to the current buffer."
672 (setq buffer (or buffer (current-buffer)))
673 (help-setup-xref (list #'describe-syntax buffer) (interactive-p))
674 (with-output-to-temp-buffer (help-buffer)
675 (let ((table (with-current-buffer buffer (syntax-table))))
676 (with-current-buffer standard-output
677 (describe-vector table 'internal-describe-syntax-value)
678 (while (setq table (char-table-parent table))
679 (insert "\nThe parent syntax table is:")
680 (describe-vector table 'internal-describe-syntax-value))))))
682 (defun help-describe-category-set (value)
684 ((null value) "default")
685 ((char-table-p value) "deeper char-table ...")
686 (t (condition-case err
687 (category-set-mnemonics value)
688 (error "invalid"))))))
691 (defun describe-categories (&optional buffer)
692 "Describe the category specifications in the current category table.
693 The descriptions are inserted in a buffer, which is then displayed.
694 If BUFFER is non-nil, then describe BUFFER's category table instead.
695 BUFFER should be a buffer or a buffer name."
697 (setq buffer (or buffer (current-buffer)))
698 (help-setup-xref (list #'describe-categories buffer) (interactive-p))
699 (with-output-to-temp-buffer (help-buffer)
700 (let ((table (with-current-buffer buffer (category-table))))
701 (with-current-buffer standard-output
702 (describe-vector table 'help-describe-category-set)
703 (let ((docs (char-table-extra-slot table 0)))
704 (if (or (not (vectorp docs)) (/= (length docs) 95))
705 (insert "Invalid first extra slot in this char table\n")
706 (insert "Meanings of mnemonic characters are:\n")
708 (let ((elt (aref docs i)))
710 (insert (+ i ?\s) ": " elt "\n"))))
711 (while (setq table (char-table-parent table))
712 (insert "\nThe parent category table is:")
713 (describe-vector table 'help-describe-category-set))))))))
717 ;; arch-tag: 9e10331c-ae81-4d13-965d-c4819aaab0b3
718 ;;; help-fns.el ends here