]> code.delx.au - gnu-emacs/blob - lisp/help-fns.el
(set-register): Use push.
[gnu-emacs] / lisp / help-fns.el
1 ;;; help-fns.el --- Complex help functions
2
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002
4 ;; Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: help, internal
8
9 ;; This file is part of GNU Emacs.
10
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 2, or (at your option)
14 ;; any later version.
15
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.
20
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., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
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
32 ;; are in help.el
33
34 ;;; Code:
35
36 (require 'help-mode)
37
38
39 ;;;###autoload
40 (defun help-with-tutorial (&optional arg)
41 "Select the Emacs learn-by-doing tutorial.
42 If there is a tutorial version written in the language
43 of the selected language environment, that version is used.
44 If there's no tutorial in that language, `TUTORIAL' is selected.
45 With arg, you are asked to choose which language."
46 (interactive "P")
47 (let ((lang (if arg
48 (let (completion-buffer)
49 ;; Display a completion list right away
50 ;; to guide the user.
51 (with-output-to-temp-buffer "*Completions*"
52 (display-completion-list
53 (all-completions "" language-info-alist
54 (lambda (elm)
55 (and (listp elm) (assq 'tutorial elm)))))
56 (setq completion-buffer standard-output))
57 ;; Arrange to set completion-reference-buffer
58 ;; in *Completions* to point to the minibuffer,
59 ;; after entering the minibuffer.
60 (let ((minibuffer-setup-hook minibuffer-setup-hook))
61 (add-hook 'minibuffer-setup-hook
62 (lambda ()
63 (let ((mini (current-buffer)))
64 (with-current-buffer completion-buffer
65 (make-local-variable 'completion-reference-buffer)
66 (setq completion-reference-buffer
67 mini)))))
68 (read-language-name 'tutorial "Language: " "English")))
69 (if (get-language-info current-language-environment 'tutorial)
70 current-language-environment
71 "English")))
72 file filename)
73 (setq filename (get-language-info lang 'tutorial))
74 (setq file (expand-file-name (concat "~/" filename)))
75 (delete-other-windows)
76 (if (get-file-buffer file)
77 (switch-to-buffer (get-file-buffer file))
78 (switch-to-buffer (create-file-buffer file))
79 (setq buffer-file-name file)
80 (setq default-directory (expand-file-name "~/"))
81 (setq buffer-auto-save-file-name nil)
82 (insert-file-contents (expand-file-name filename data-directory))
83 (goto-char (point-min))
84 (search-forward "\n<<")
85 (beginning-of-line)
86 ;; Convert the <<...>> line to the proper [...] line,
87 ;; or just delete the <<...>> line if a [...] line follows.
88 (cond ((save-excursion
89 (forward-line 1)
90 (looking-at "\\["))
91 (delete-region (point) (progn (forward-line 1) (point))))
92 ((looking-at "<<Blank lines inserted.*>>")
93 (replace-match "[Middle of page left blank for didactic purposes. Text continues below]"))
94 (t
95 (looking-at "<<")
96 (replace-match "[")
97 (search-forward ">>")
98 (replace-match "]")))
99 (beginning-of-line)
100 (let ((n (- (window-height (selected-window))
101 (count-lines (point-min) (point))
102 6)))
103 (if (< n 8)
104 (progn
105 ;; For a short gap, we don't need the [...] line,
106 ;; so delete it.
107 (delete-region (point) (progn (end-of-line) (point)))
108 (newline n))
109 ;; Some people get confused by the large gap.
110 (newline (/ n 2))
111
112 ;; Skip the [...] line (don't delete it).
113 (forward-line 1)
114 (newline (- n (/ n 2)))))
115 (goto-char (point-min))
116 (set-buffer-modified-p nil))))
117
118 ;;;###autoload
119 (defun locate-library (library &optional nosuffix path interactive-call)
120 "Show the precise file name of Emacs library LIBRARY.
121 This command searches the directories in `load-path' like `\\[load-library]'
122 to find the file that `\\[load-library] RET LIBRARY RET' would load.
123 Optional second arg NOSUFFIX non-nil means don't add suffixes `load-suffixes'
124 to the specified name LIBRARY.
125
126 If the optional third arg PATH is specified, that list of directories
127 is used instead of `load-path'.
128
129 When called from a program, the file name is normaly returned as a
130 string. When run interactively, the argument INTERACTIVE-CALL is t,
131 and the file name is displayed in the echo area."
132 (interactive (list (completing-read "Locate library: "
133 'locate-file-completion
134 (cons load-path load-suffixes))
135 nil nil
136 t))
137 (let ((file (locate-file library
138 (or path load-path)
139 (append (unless nosuffix load-suffixes) '("")))))
140 (if interactive-call
141 (if file
142 (message "Library is file %s" (abbreviate-file-name file))
143 (message "No library %s in search path" library)))
144 file))
145
146 \f
147 ;; Functions
148
149 ;;;###autoload
150 (defun describe-function (function)
151 "Display the full documentation of FUNCTION (a symbol)."
152 (interactive
153 (let ((fn (function-called-at-point))
154 (enable-recursive-minibuffers t)
155 val)
156 (setq val (completing-read (if fn
157 (format "Describe function (default %s): " fn)
158 "Describe function: ")
159 obarray 'fboundp t nil nil (symbol-name fn)))
160 (list (if (equal val "")
161 fn (intern val)))))
162 (if (null function)
163 (message "You didn't specify a function")
164 (help-setup-xref (list #'describe-function function) (interactive-p))
165 (save-excursion
166 (with-output-to-temp-buffer (help-buffer)
167 (prin1 function)
168 ;; Use " is " instead of a colon so that
169 ;; it is easier to get out the function name using forward-sexp.
170 (princ " is ")
171 (describe-function-1 function)
172 (print-help-return-message)
173 (with-current-buffer standard-output
174 ;; Return the text we displayed.
175 (buffer-string))))))
176
177 (defun help-split-fundoc (doc def)
178 "Split a function docstring DOC into the actual doc and the usage info.
179 Return (USAGE . DOC) or nil if there's no usage info.
180 DEF is the function whose usage we're looking for in DOC."
181 ;; Functions can get the calling sequence at the end of the doc string.
182 ;; In cases where `function' has been fset to a subr we can't search for
183 ;; function's name in the doc string so we use `fn' as the anonymous
184 ;; function name instead.
185 (when doc
186 (let* ((rep (prin1-to-string (indirect-function def)))
187 (name (if (string-match " \\([^ ]+\\)>$" rep)
188 (match-string 1 rep) (prin1-to-string def))))
189 (if (string-match (format "\n\n(\\(fn\\|%s\\)\\(\\( .*\\)?)\\)\\'"
190 (regexp-quote name))
191 doc)
192 (cons (format "(%s%s"
193 ;; Replace `fn' with the actual function name.
194 (if (consp def) "anonymous" def)
195 (match-string 2 doc))
196 (substring doc 0 (match-beginning 0)))))))
197
198 (defun help-function-arglist (def)
199 ;; Handle symbols aliased to other symbols.
200 (if (and (symbolp def) (fboundp def)) (setq def (indirect-function def)))
201 ;; If definition is a macro, find the function inside it.
202 (if (eq (car-safe def) 'macro) (setq def (cdr def)))
203 (cond
204 ((byte-code-function-p def) (aref def 0))
205 ((eq (car-safe def) 'lambda) (nth 1 def))
206 ((and (eq (car-safe def) 'autoload) (not (eq (nth 4 def) 'keymap)))
207 "[Arg list not available until function definition is loaded.]")
208 (t t)))
209
210 (defun help-make-usage (function arglist)
211 (cons (if (symbolp function) function 'anonymous)
212 (mapcar (lambda (arg)
213 (if (not (symbolp arg))
214 (if (and (consp arg) (symbolp (car arg)))
215 ;; CL style default values for optional args.
216 (cons (intern (upcase (symbol-name (car arg))))
217 (cdr arg))
218 arg)
219 (let ((name (symbol-name arg)))
220 (if (string-match "\\`&" name) arg
221 (intern (upcase name))))))
222 arglist)))
223
224 ;;;###autoload
225 (defun describe-function-1 (function)
226 (let* ((def (if (symbolp function)
227 (symbol-function function)
228 function))
229 file-name string
230 (beg (if (commandp def) "an interactive " "a ")))
231 (setq string
232 (cond ((or (stringp def)
233 (vectorp def))
234 "a keyboard macro")
235 ((subrp def)
236 (if (eq 'unevalled (cdr (subr-arity def)))
237 (concat beg "special form")
238 (concat beg "built-in function")))
239 ((byte-code-function-p def)
240 (concat beg "compiled Lisp function"))
241 ((symbolp def)
242 (while (symbolp (symbol-function def))
243 (setq def (symbol-function def)))
244 (format "an alias for `%s'" def))
245 ((eq (car-safe def) 'lambda)
246 (concat beg "Lisp function"))
247 ((eq (car-safe def) 'macro)
248 "a Lisp macro")
249 ((eq (car-safe def) 'autoload)
250 (setq file-name (nth 1 def))
251 (format "%s autoloaded %s"
252 (if (commandp def) "an interactive" "an")
253 (if (eq (nth 4 def) 'keymap) "keymap"
254 (if (nth 4 def) "Lisp macro" "Lisp function"))
255 ))
256 ((keymapp def)
257 (let ((is-full nil)
258 (elts (cdr-safe def)))
259 (while elts
260 (if (char-table-p (car-safe elts))
261 (setq is-full t
262 elts nil))
263 (setq elts (cdr-safe elts)))
264 (if is-full
265 "a full keymap"
266 "a sparse keymap")))
267 (t "")))
268 (princ string)
269 (with-current-buffer standard-output
270 (save-excursion
271 (save-match-data
272 (if (re-search-backward "alias for `\\([^`']+\\)'" nil t)
273 (help-xref-button 1 'help-function def)))))
274 (or file-name
275 (setq file-name (symbol-file function)))
276 (when (equal file-name "loaddefs.el")
277 ;; Find the real def site of the preloaded function.
278 ;; This is necessary only for defaliases.
279 (let ((location
280 (condition-case nil
281 (find-function-search-for-symbol function nil "loaddefs.el")
282 (error nil))))
283 (when location
284 (with-current-buffer (car location)
285 (goto-char (cdr location))
286 (when (re-search-backward
287 "^;;; Generated autoloads from \\(.*\\)" nil t)
288 (setq file-name (match-string 1)))))))
289 (cond
290 (file-name
291 (princ " in `")
292 ;; We used to add .el to the file name,
293 ;; but that's completely wrong when the user used load-file.
294 (princ file-name)
295 (princ "'")
296 ;; Make a hyperlink to the library.
297 (with-current-buffer standard-output
298 (save-excursion
299 (re-search-backward "`\\([^`']+\\)'" nil t)
300 (help-xref-button 1 'help-function-def function file-name)))))
301 (princ ".")
302 (terpri)
303 (when (commandp function)
304 (let* ((remapped (remap-command function))
305 (keys (where-is-internal
306 (or remapped function) overriding-local-map nil nil)))
307 (when remapped
308 (princ "It is remapped to `")
309 (princ (symbol-name remapped))
310 (princ "'"))
311 (when keys
312 (princ (if remapped " which is bound to " "It is bound to "))
313 ;; FIXME: This list can be very long (f.ex. for self-insert-command).
314 (princ (mapconcat 'key-description keys ", ")))
315 (when (or remapped keys)
316 (princ ".")
317 (terpri))))
318 (let* ((arglist (help-function-arglist def))
319 (doc (documentation function))
320 (usage (help-split-fundoc doc function)))
321 ;; If definition is a keymap, skip arglist note.
322 (unless (keymapp def)
323 (princ (cond
324 (usage (setq doc (cdr usage)) (car usage))
325 ((listp arglist) (help-make-usage function arglist))
326 ((stringp arglist) arglist)
327 ;; Maybe the arglist is in the docstring of the alias.
328 ((let ((fun function))
329 (while (and (symbolp fun)
330 (setq fun (symbol-function fun))
331 (not (setq usage (help-split-fundoc
332 (documentation fun)
333 function)))))
334 usage)
335 (car usage))
336 (t "[Missing arglist. Please make a bug report.]")))
337 (terpri))
338 (let ((obsolete (and
339 ;; function might be a lambda construct.
340 (symbolp function)
341 (get function 'byte-obsolete-info))))
342 (when obsolete
343 (terpri)
344 (princ "This function is obsolete")
345 (if (nth 2 obsolete) (princ (format " since %s" (nth 2 obsolete))))
346 (princ ";") (terpri)
347 (princ (if (stringp (car obsolete)) (car obsolete)
348 (format "use `%s' instead." (car obsolete))))
349 (terpri)))
350 (terpri)
351 (princ (or doc "Not documented.")))))
352
353 \f
354 ;; Variables
355
356 ;;;###autoload
357 (defun variable-at-point ()
358 "Return the bound variable symbol found around point.
359 Return 0 if there is no such symbol."
360 (condition-case ()
361 (with-syntax-table emacs-lisp-mode-syntax-table
362 (save-excursion
363 (or (not (zerop (skip-syntax-backward "_w")))
364 (eq (char-syntax (following-char)) ?w)
365 (eq (char-syntax (following-char)) ?_)
366 (forward-sexp -1))
367 (skip-chars-forward "'")
368 (let ((obj (read (current-buffer))))
369 (or (and (symbolp obj) (boundp obj) obj)
370 0))))
371 (error 0)))
372
373 ;;;###autoload
374 (defun describe-variable (variable &optional buffer)
375 "Display the full documentation of VARIABLE (a symbol).
376 Returns the documentation as a string, also.
377 If VARIABLE has a buffer-local value in BUFFER (default to the current buffer),
378 it is displayed along with the global value."
379 (interactive
380 (let ((v (variable-at-point))
381 (enable-recursive-minibuffers t)
382 val)
383 (setq val (completing-read (if (symbolp v)
384 (format
385 "Describe variable (default %s): " v)
386 "Describe variable: ")
387 obarray 'boundp t nil nil
388 (if (symbolp v) (symbol-name v))))
389 (list (if (equal val "")
390 v (intern val)))))
391 (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
392 (if (not (symbolp variable))
393 (message "You did not specify a variable")
394 (save-excursion
395 (let* ((valvoid (not (with-current-buffer buffer (boundp variable))))
396 ;; Extract the value before setting up the output buffer,
397 ;; in case `buffer' *is* the output buffer.
398 (val (unless valvoid (buffer-local-value variable buffer))))
399 (help-setup-xref (list #'describe-variable variable buffer)
400 (interactive-p))
401 (with-output-to-temp-buffer (help-buffer)
402 (with-current-buffer buffer
403 (prin1 variable)
404 (if valvoid
405 (princ " is void")
406 (with-current-buffer standard-output
407 (princ "'s value is ")
408 (terpri)
409 (let ((from (point)))
410 (pp val)
411 (help-xref-on-pp from (point))
412 (if (< (point) (+ from 20))
413 (delete-region (1- from) from)))))
414 (terpri)
415 (when (local-variable-p variable)
416 (princ (format "Local in buffer %s; " (buffer-name)))
417 (if (not (default-boundp variable))
418 (princ "globally void")
419 (let ((val (default-value variable)))
420 (with-current-buffer standard-output
421 (princ "global value is ")
422 (terpri)
423 ;; Fixme: pp can take an age if you happen to
424 ;; ask for a very large expression. We should
425 ;; probably print it raw once and check it's a
426 ;; sensible size before prettyprinting. -- fx
427 (let ((from (point)))
428 (pp val)
429 (help-xref-on-pp from (point))
430 (if (< (point) (+ from 20))
431 (delete-region (1- from) from))))))
432 (terpri))
433 (terpri)
434 (with-current-buffer standard-output
435 (when (> (count-lines (point-min) (point-max)) 10)
436 ;; Note that setting the syntax table like below
437 ;; makes forward-sexp move over a `'s' at the end
438 ;; of a symbol.
439 (set-syntax-table emacs-lisp-mode-syntax-table)
440 (goto-char (point-min))
441 (if valvoid
442 (forward-line 1)
443 (forward-sexp 1)
444 (delete-region (point) (progn (end-of-line) (point)))
445 (insert " value is shown below.\n\n")
446 (save-excursion
447 (insert "\n\nValue:"))))
448 ;; Add a note for variables that have been make-var-buffer-local.
449 (when (and (local-variable-if-set-p variable)
450 (or (not (local-variable-p variable))
451 (with-temp-buffer
452 (local-variable-if-set-p variable))))
453 (save-excursion
454 (forward-line -1)
455 (insert "Automatically becomes buffer-local when set in any fashion.\n"))))
456 ;; Mention if it's an alias
457 (let* ((alias (condition-case nil
458 (indirect-variable variable)
459 (error variable)))
460 (obsolete (get variable 'byte-obsolete-variable))
461 (doc (or (documentation-property variable 'variable-documentation)
462 (documentation-property alias 'variable-documentation))))
463 (unless (eq alias variable)
464 (princ (format "This variable is an alias for `%s'." alias))
465 (terpri)
466 (terpri))
467 (when obsolete
468 (princ "This variable is obsolete")
469 (if (cdr obsolete) (princ (format " since %s" (cdr obsolete))))
470 (princ ";") (terpri)
471 (princ (if (stringp (car obsolete)) (car obsolete)
472 (format "use `%s' instead." (car obsolete))))
473 (terpri)
474 (terpri))
475 (princ (or doc "Not documented as a variable.")))
476 ;; Make a link to customize if this variable can be customized.
477 ;; Note, it is not reliable to test only for a custom-type property
478 ;; because those are only present after the var's definition
479 ;; has been loaded.
480 (if (or (get variable 'custom-type) ; after defcustom
481 (get variable 'custom-loads) ; from loaddefs.el
482 (get variable 'standard-value)) ; from cus-start.el
483 (let ((customize-label "customize"))
484 (terpri)
485 (terpri)
486 (princ (concat "You can " customize-label " this variable."))
487 (with-current-buffer standard-output
488 (save-excursion
489 (re-search-backward
490 (concat "\\(" customize-label "\\)") nil t)
491 (help-xref-button 1 'help-customize-variable variable)))))
492 ;; Make a hyperlink to the library if appropriate. (Don't
493 ;; change the format of the buffer's initial line in case
494 ;; anything expects the current format.)
495 (let ((file-name (symbol-file variable)))
496 (when (equal file-name "loaddefs.el")
497 ;; Find the real def site of the preloaded variable.
498 (let ((location
499 (condition-case nil
500 (find-variable-noselect variable file-name)
501 (error nil))))
502 (when location
503 (with-current-buffer (car location)
504 (goto-char (cdr location))
505 (when (re-search-backward
506 "^;;; Generated autoloads from \\(.*\\)" nil t)
507 (setq file-name (match-string 1)))))))
508 (when file-name
509 (princ "\n\nDefined in `")
510 (princ file-name)
511 (princ "'.")
512 (with-current-buffer standard-output
513 (save-excursion
514 (re-search-backward "`\\([^`']+\\)'" nil t)
515 (help-xref-button 1 'help-variable-def
516 variable file-name)))))
517
518 (print-help-return-message)
519 (save-excursion
520 (set-buffer standard-output)
521 ;; Return the text we displayed.
522 (buffer-string))))))))
523
524
525 ;;;###autoload
526 (defun describe-syntax (&optional buffer)
527 "Describe the syntax specifications in the syntax table of BUFFER.
528 The descriptions are inserted in a help buffer, which is then displayed.
529 BUFFER defaults to the current buffer."
530 (interactive)
531 (setq buffer (or buffer (current-buffer)))
532 (help-setup-xref (list #'describe-syntax buffer) (interactive-p))
533 (with-output-to-temp-buffer (help-buffer)
534 (let ((table (with-current-buffer buffer (syntax-table))))
535 (with-current-buffer standard-output
536 (describe-vector table 'internal-describe-syntax-value)
537 (while (setq table (char-table-parent table))
538 (insert "\nThe parent syntax table is:")
539 (describe-vector table 'internal-describe-syntax-value))))))
540
541 (defun help-describe-category-set (value)
542 (insert (cond
543 ((null value) "default")
544 ((char-table-p value) "deeper char-table ...")
545 (t (condition-case err
546 (category-set-mnemonics value)
547 (error "invalid"))))))
548
549 ;;;###autoload
550 (defun describe-categories (&optional buffer)
551 "Describe the category specifications in the current category table.
552 The descriptions are inserted in a buffer, which is then displayed."
553 (interactive)
554 (setq buffer (or buffer (current-buffer)))
555 (help-setup-xref (list #'describe-categories buffer) (interactive-p))
556 (with-output-to-temp-buffer (help-buffer)
557 (let ((table (with-current-buffer buffer (category-table))))
558 (with-current-buffer standard-output
559 (describe-vector table 'help-describe-category-set)
560 (let ((docs (char-table-extra-slot table 0)))
561 (if (or (not (vectorp docs)) (/= (length docs) 95))
562 (insert "Invalid first extra slot in this char table\n")
563 (insert "Meanings of mnemonic characters are:\n")
564 (dotimes (i 95)
565 (let ((elt (aref docs i)))
566 (when elt
567 (insert (+ i ?\ ) ": " elt "\n"))))
568 (while (setq table (char-table-parent table))
569 (insert "\nThe parent category table is:")
570 (describe-vector table 'help-describe-category-set))))))))
571
572 (provide 'help-fns)
573
574 ;;; help-fns.el ends here