(vc-dired-mode): Make dired-after-readin-hook a local hook, not a local variable.
[bpt/emacs.git] / lisp / help.el
1 ;;; help.el --- help commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1993, 1994 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: help, internal
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; This code implements GNU Emacs' on-line help system, the one invoked by
28 ;;`M-x help-for-help'.
29
30 ;;; Code:
31
32 ;; Get the macro make-help-screen when this is compiled,
33 ;; or run interpreted, but not when the compiled code is loaded.
34 (eval-when-compile (require 'help-macro))
35
36 (defvar help-map (make-sparse-keymap)
37 "Keymap for characters following the Help key.")
38
39 (defvar help-mode-map (make-sparse-keymap)
40 "Keymap for help mode.")
41
42 (define-key global-map (char-to-string help-char) 'help-command)
43 (define-key global-map [help] 'help-command)
44 (define-key global-map [f1] 'help-command)
45 (fset 'help-command help-map)
46
47 (define-key help-map (char-to-string help-char) 'help-for-help)
48 (define-key help-map [help] 'help-for-help)
49 (define-key help-map [f1] 'help-for-help)
50 (define-key help-map "?" 'help-for-help)
51
52 (define-key help-map "\C-c" 'describe-copying)
53 (define-key help-map "\C-d" 'describe-distribution)
54 (define-key help-map "\C-w" 'describe-no-warranty)
55 (define-key help-map "\C-p" 'describe-project)
56 (define-key help-map "a" 'apropos-command)
57
58 (define-key help-map "b" 'describe-bindings)
59
60 (define-key help-map "c" 'describe-key-briefly)
61 (define-key help-map "k" 'describe-key)
62
63 (define-key help-map "d" 'describe-function)
64 (define-key help-map "f" 'describe-function)
65
66 (define-key help-map "F" 'view-emacs-FAQ)
67
68 (define-key help-map "i" 'info)
69 (define-key help-map "\C-f" 'Info-goto-emacs-command-node)
70 (define-key help-map "\C-k" 'Info-goto-emacs-key-command-node)
71 (define-key help-map "\C-i" 'info-lookup-symbol)
72
73 (define-key help-map "l" 'view-lossage)
74
75 (define-key help-map "m" 'describe-mode)
76
77 (define-key help-map "\C-n" 'view-emacs-news)
78 (define-key help-map "n" 'view-emacs-news)
79
80 (define-key help-map "p" 'finder-by-keyword)
81 (autoload 'finder-by-keyword "finder"
82 "Find packages matching a given keyword." t)
83
84 (define-key help-map "s" 'describe-syntax)
85
86 (define-key help-map "t" 'help-with-tutorial)
87
88 (define-key help-map "w" 'where-is)
89
90 (define-key help-map "v" 'describe-variable)
91
92 (define-key help-map "q" 'help-quit)
93
94 (defvar help-font-lock-keywords
95 (eval-when-compile
96 (let ((name-char "[-+a-zA-Z0-9_*]") (sym-char "[-+a-zA-Z0-9_:*]"))
97 (list
98 ;;
99 ;; The symbol itself.
100 (list (concat "\\`\\(" name-char "+\\)\\(\\(:\\)\\|\\('\\)\\)")
101 '(1 (if (match-beginning 3)
102 font-lock-function-name-face
103 font-lock-variable-name-face)))
104 ;;
105 ;; Words inside `' which tend to be symbol names.
106 (list (concat "`\\(" sym-char sym-char "+\\)'")
107 1 'font-lock-constant-face t)
108 ;;
109 ;; CLisp `:' keywords as builtins.
110 (list (concat "\\<:" sym-char "+\\>") 0 'font-lock-builtin-face t))))
111 "Default expressions to highlight in Help mode.")
112
113 (defun help-mode ()
114 "Major mode for viewing help text.
115 Entry to this mode runs the normal hook `help-mode-hook'.
116 Commands:
117 \\{help-mode-map}"
118 (interactive)
119 (kill-all-local-variables)
120 (use-local-map help-mode-map)
121 (setq mode-name "Help")
122 (setq major-mode 'help-mode)
123 (make-local-variable 'font-lock-defaults)
124 (setq font-lock-defaults '(help-font-lock-keywords))
125 (view-mode)
126 (make-local-variable 'view-no-disable-on-exit)
127 (setq view-no-disable-on-exit t)
128 (run-hooks 'help-mode-hook))
129
130 (defun help-mode-maybe ()
131 (if (eq major-mode 'fundamental-mode)
132 (help-mode))
133 (setq view-return-to-alist
134 (list (cons (selected-window) help-return-method))))
135
136 (add-hook 'temp-buffer-show-hook 'help-mode-maybe)
137
138 (defun help-quit ()
139 (interactive)
140 nil)
141
142 (defvar help-with-tutorial-alist
143 '(("German" . "TUTORIAL.de")
144 ("Korean" . "TUTORIAL.kr")
145 ("Japanese" . "TUTORIAL.jp")
146 ("Thai" . "TUTORIAL.th")
147 ("English" . "TUTORIAL"))
148 "Alist mapping language names to their translated Emacs tutorial files.")
149
150 (defun help-with-tutorial (&optional arg)
151 "Select the Emacs learn-by-doing tutorial.
152 If there is a tutorial version written in the language
153 of the selected language environment, that version is used.
154 If there's no tutorial in that language, `TUTORIAL' is selected.
155 With arg, you are asked to choose which language."
156 (interactive "P")
157 (let (lang filename file)
158 (if arg
159 (or (setq lang
160 (let* ((completion-ignore-case t))
161 (completing-read "Language: " help-with-tutorial-alist
162 nil t)))
163 (error "No tutorial file in language"))
164 (setq lang current-language-environment))
165 (setq filename (or (cdr (assoc lang help-with-tutorial-alist))
166 "TUTORIAL"))
167 (setq file (expand-file-name (concat "~/" filename)))
168 (delete-other-windows)
169 (if (get-file-buffer file)
170 (switch-to-buffer (get-file-buffer file))
171 (switch-to-buffer (create-file-buffer file))
172 (setq buffer-file-name file)
173 (setq default-directory (expand-file-name "~/"))
174 (setq buffer-auto-save-file-name nil)
175 (insert-file-contents (expand-file-name filename data-directory))
176 (goto-char (point-min))
177 (search-forward "\n<<")
178 (beginning-of-line)
179 (delete-region (point) (progn (end-of-line) (point)))
180 (let ((n (- (window-height (selected-window))
181 (count-lines (point-min) (point))
182 6)))
183 (if (< n 12)
184 (newline n)
185 ;; Some people get confused by the large gap.
186 (newline (/ n 2))
187 (insert "[Middle of page left blank for didactic purposes. "
188 "Text continues below]")
189 (newline (- n (/ n 2)))))
190 (goto-char (point-min))
191 (set-buffer-modified-p nil))))
192
193 (defun describe-key-briefly (key &optional insert)
194 "Print the name of the function KEY invokes. KEY is a string.
195 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
196 (interactive "kDescribe key briefly: \nP")
197 ;; If this key seq ends with a down event, discard the
198 ;; following click or drag event. Otherwise that would
199 ;; erase the message.
200 (let ((type (aref key (1- (length key)))))
201 (if (listp type) (setq type (car type)))
202 (and (symbolp type)
203 (memq 'down (event-modifiers type))
204 (read-event)))
205 (save-excursion
206 (let ((modifiers (event-modifiers (aref key 0)))
207 (standard-output (if insert (current-buffer) t))
208 window position)
209 ;; For a mouse button event, go to the button it applies to
210 ;; to get the right key bindings. And go to the right place
211 ;; in case the keymap depends on where you clicked.
212 (if (or (memq 'click modifiers) (memq 'down modifiers)
213 (memq 'drag modifiers))
214 (setq window (posn-window (event-start (aref key 0)))
215 position (posn-point (event-start (aref key 0)))))
216 (if (windowp window)
217 (progn
218 (set-buffer (window-buffer window))
219 (goto-char position)))
220 ;; Ok, now look up the key and name the command.
221 (let ((defn (key-binding key))
222 (key-desc (key-description key)))
223 (if (or (null defn) (integerp defn))
224 (princ (format "%s is undefined" key-desc))
225 (princ (format (if insert
226 "%s (%s)"
227 (if (windowp window)
228 "%s at that spot runs the command %s"
229 "%s runs the command %s"))
230 key-desc
231 (if (symbolp defn) defn (prin1-to-string defn)))))))))
232
233 (defvar help-return-method nil
234 "What to do to \"exit\" the help buffer.
235 This is a list
236 (WINDOW . t) delete the selected window, go to WINDOW.
237 (WINDOW . quit-window) do quit-window, then select WINDOW.
238 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
239
240 (defun print-help-return-message (&optional function)
241 "Display or return message saying how to restore windows after help command.
242 Computes a message and applies the optional argument FUNCTION to it.
243 If FUNCTION is nil, applies `message' to it, thus printing it."
244 (and (not (get-buffer-window standard-output))
245 (let ((first-message
246 (cond ((special-display-p (buffer-name standard-output))
247 (setq help-return-method (cons (selected-window) t))
248 ;; If the help output buffer is a special display buffer,
249 ;; don't say anything about how to get rid of it.
250 ;; First of all, the user will do that with the window
251 ;; manager, not with Emacs.
252 ;; Secondly, the buffer has not been displayed yet,
253 ;; so we don't know whether its frame will be selected.
254 nil)
255 ((not (one-window-p t))
256 (setq help-return-method
257 (cons (selected-window) 'quit-window))
258 "Type \\[switch-to-buffer-other-window] RET to restore the other window.")
259 (pop-up-windows
260 (setq help-return-method (cons (selected-window) t))
261 "Type \\[delete-other-windows] to remove help window.")
262 (t
263 (setq help-return-method
264 (list (selected-window) (window-buffer)
265 (window-start) (window-point)))
266 "Type \\[switch-to-buffer] RET to remove help window."))))
267 (funcall (or function 'message)
268 (concat
269 (if first-message
270 (substitute-command-keys first-message)
271 "")
272 (if first-message " " "")
273 ;; If the help buffer will go in a separate frame,
274 ;; it's no use mentioning a command to scroll, so don't.
275 (if (special-display-p (buffer-name standard-output))
276 nil
277 (if (same-window-p (buffer-name standard-output))
278 ;; Say how to scroll this window.
279 (substitute-command-keys
280 "\\[scroll-up] to scroll the help.")
281 ;; Say how to scroll some other window.
282 (substitute-command-keys
283 "\\[scroll-other-window] to scroll the help."))))))))
284
285 (defun describe-key (key)
286 "Display documentation of the function invoked by KEY. KEY is a string."
287 (interactive "kDescribe key: ")
288 ;; If this key seq ends with a down event, discard the
289 ;; following click or drag event. Otherwise that would
290 ;; erase the message.
291 (let ((type (aref key (1- (length key)))))
292 (if (listp type) (setq type (car type)))
293 (and (symbolp type)
294 (memq 'down (event-modifiers type))
295 (read-event)))
296 (save-excursion
297 (let ((modifiers (event-modifiers (aref key 0)))
298 window position)
299 ;; For a mouse button event, go to the button it applies to
300 ;; to get the right key bindings. And go to the right place
301 ;; in case the keymap depends on where you clicked.
302 (if (or (memq 'click modifiers) (memq 'down modifiers)
303 (memq 'drag modifiers))
304 (setq window (posn-window (event-start (aref key 0)))
305 position (posn-point (event-start (aref key 0)))))
306 (if (windowp window)
307 (progn
308 (set-buffer (window-buffer window))
309 (goto-char position)))
310 (let ((defn (key-binding key)))
311 (if (or (null defn) (integerp defn))
312 (message "%s is undefined" (key-description key))
313 (with-output-to-temp-buffer "*Help*"
314 (princ (key-description key))
315 (if (windowp window)
316 (princ " at that spot"))
317 (princ " runs the command ")
318 (prin1 defn)
319 (princ "\n")
320 (let ((doc (documentation defn)))
321 (if doc
322 (progn (terpri)
323 (princ doc))
324 (princ "not documented")))
325 (print-help-return-message)))))))
326
327 (defun describe-mode ()
328 "Display documentation of current major mode and minor modes.
329 For this to work correctly for a minor mode, the mode's indicator variable
330 \(listed in `minor-mode-alist') must also be a function whose documentation
331 describes the minor mode."
332 (interactive)
333 (with-output-to-temp-buffer "*Help*"
334 (let ((minor-modes minor-mode-alist)
335 (first t))
336 (while minor-modes
337 (let* ((minor-mode (car (car minor-modes)))
338 (indicator (car (cdr (car minor-modes)))))
339 ;; Document a minor mode if it is listed in minor-mode-alist,
340 ;; bound locally in this buffer, non-nil, and has a function
341 ;; definition.
342 (if (and (symbol-value minor-mode)
343 (fboundp minor-mode))
344 (let ((pretty-minor-mode minor-mode))
345 (if (string-match "-mode$" (symbol-name minor-mode))
346 (setq pretty-minor-mode
347 (capitalize
348 (substring (symbol-name minor-mode)
349 0 (match-beginning 0)))))
350 (while (and indicator (symbolp indicator))
351 (setq indicator (symbol-value indicator)))
352 (if first
353 (princ "The minor modes are described first,
354 followed by the major mode, which is described on the last page.\n\f\n"))
355 (setq first nil)
356 (princ (format "%s minor mode (%s):\n"
357 pretty-minor-mode
358 (if indicator
359 (format "indicator%s" indicator)
360 "no indicator")))
361 (princ (documentation minor-mode))
362 (princ "\n\f\n"))))
363 (setq minor-modes (cdr minor-modes))))
364 (princ mode-name)
365 (princ " mode:\n")
366 (princ (documentation major-mode))
367 (print-help-return-message)))
368
369 ;; So keyboard macro definitions are documented correctly
370 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
371
372 (defun describe-distribution ()
373 "Display info on how to obtain the latest version of GNU Emacs."
374 (interactive)
375 (find-file-read-only
376 (expand-file-name "DISTRIB" data-directory)))
377
378 (defun describe-copying ()
379 "Display info on how you may redistribute copies of GNU Emacs."
380 (interactive)
381 (find-file-read-only
382 (expand-file-name "COPYING" data-directory))
383 (goto-char (point-min)))
384
385 (defun describe-project ()
386 "Display info on the GNU project."
387 (interactive)
388 (find-file-read-only
389 (expand-file-name "GNU" data-directory))
390 (goto-char (point-min)))
391
392 (defun describe-no-warranty ()
393 "Display info on all the kinds of warranty Emacs does NOT have."
394 (interactive)
395 (describe-copying)
396 (let (case-fold-search)
397 (search-forward "NO WARRANTY")
398 (recenter 0)))
399
400 (defun describe-prefix-bindings ()
401 "Describe the bindings of the prefix used to reach this command.
402 The prefix described consists of all but the last event
403 of the key sequence that ran this command."
404 (interactive)
405 (let* ((key (this-command-keys)))
406 (describe-bindings
407 (if (stringp key)
408 (substring key 0 (1- (length key)))
409 (let ((prefix (make-vector (1- (length key)) nil))
410 (i 0))
411 (while (< i (length prefix))
412 (aset prefix i (aref key i))
413 (setq i (1+ i)))
414 prefix)))))
415 ;; Make C-h after a prefix, when not specifically bound,
416 ;; run describe-prefix-bindings.
417 (setq prefix-help-command 'describe-prefix-bindings)
418
419 (defun view-emacs-news (&optional arg)
420 "Display info on recent changes to Emacs.
421 With numeric argument display information on correspondingly older changes."
422 (interactive "P")
423 (let* ((arg (if arg (prefix-numeric-value arg) 0)))
424 (find-file-read-only
425 (expand-file-name (concat (make-string arg ?O) "NEWS")
426 data-directory))))
427
428 (defun view-emacs-FAQ ()
429 "Display the Emacs Frequently Asked Questions (FAQ) file."
430 (interactive)
431 (find-file-read-only (expand-file-name "FAQ" data-directory)))
432
433 (defun view-lossage ()
434 "Display last 100 input keystrokes."
435 (interactive)
436 (with-output-to-temp-buffer "*Help*"
437 (princ (mapconcat (function (lambda (key)
438 (if (or (integerp key)
439 (symbolp key)
440 (listp key))
441 (single-key-description key)
442 (prin1-to-string key nil))))
443 (recent-keys)
444 " "))
445 (save-excursion
446 (set-buffer standard-output)
447 (goto-char (point-min))
448 (while (progn (move-to-column 50) (not (eobp)))
449 (search-forward " " nil t)
450 (insert "\n")))
451 (print-help-return-message)))
452
453 (defalias 'help 'help-for-help)
454 (make-help-screen help-for-help
455 "a b c C f F C-f i I k C-k l L m n p s t v w C-c C-d C-n C-p C-w; ? for help:"
456 "You have typed \\[help-command], the help character. Type a Help option:
457 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
458
459 a command-apropos. Give a substring, and see a list of commands
460 (functions interactively callable) that contain
461 that substring. See also the apropos command.
462 b describe-bindings. Display table of all key bindings.
463 c describe-key-briefly. Type a command key sequence;
464 it prints the function name that sequence runs.
465 C describe-coding-system. This describes either a specific coding system
466 (if you type its name) or the coding systems currently in use
467 (if you type just RET).
468 f describe-function. Type a function name and get documentation of it.
469 C-f Info-goto-emacs-command-node. Type a function name;
470 it takes you to the Info node for that command.
471 i info. The info documentation reader.
472 I describe-input-method. Describe a specific input method (if you type
473 its name) or the current input method (if you type just RET).
474 k describe-key. Type a command key sequence;
475 it displays the full documentation.
476 C-k Info-goto-emacs-key-command-node. Type a command key sequence;
477 it takes you to the Info node for the command bound to that key.
478 l view-lossage. Shows last 100 characters you typed.
479 L describe-language-environment. This describes either the a
480 specific language environment (if you type its name)
481 or the current language environment (if you type just RET).
482 m describe-mode. Print documentation of current minor modes,
483 and the current major mode, including their special commands.
484 n view-emacs-news. Shows emacs news file.
485 p finder-by-keyword. Find packages matching a given topic keyword.
486 s describe-syntax. Display contents of syntax table, plus explanations
487 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
488 v describe-variable. Type name of a variable;
489 it displays the variable's documentation and value.
490 w where-is. Type command name; it prints which keystrokes
491 invoke that command.
492
493 F Display the frequently asked questions file.
494 h Display the HELLO file which illustrates various scripts.
495 C-c Display Emacs copying permission (General Public License).
496 C-d Display Emacs ordering information.
497 C-n Display news of recent Emacs changes.
498 C-p Display information about the GNU project.
499 C-w Display information on absence of warranty for GNU Emacs."
500 help-map)
501
502 ;; Return a function which is called by the list containing point.
503 ;; If that gives no function, return a function whose name is around point.
504 ;; If that doesn't give a function, return nil.
505 (defun function-called-at-point ()
506 (let ((stab (syntax-table)))
507 (set-syntax-table emacs-lisp-mode-syntax-table)
508 (unwind-protect
509 (or (condition-case ()
510 (save-excursion
511 (save-restriction
512 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
513 ;; Move up to surrounding paren, then after the open.
514 (backward-up-list 1)
515 (forward-char 1)
516 ;; If there is space here, this is probably something
517 ;; other than a real Lisp function call, so ignore it.
518 (if (looking-at "[ \t]")
519 (error "Probably not a Lisp function call"))
520 (let (obj)
521 (setq obj (read (current-buffer)))
522 (and (symbolp obj) (fboundp obj) obj))))
523 (error nil))
524 (condition-case ()
525 (save-excursion
526 (or (not (zerop (skip-syntax-backward "_w")))
527 (eq (char-syntax (following-char)) ?w)
528 (eq (char-syntax (following-char)) ?_)
529 (forward-sexp -1))
530 (skip-chars-forward "'")
531 (let ((obj (read (current-buffer))))
532 (and (symbolp obj) (fboundp obj) obj)))
533 (error nil)))
534 (set-syntax-table stab))))
535
536 (defun describe-function-find-file (function)
537 (let ((files load-history)
538 file functions)
539 (while files
540 (if (memq function (cdr (car files)))
541 (setq file (car (car files)) files nil))
542 (setq files (cdr files)))
543 file))
544
545 (defun describe-function (function)
546 "Display the full documentation of FUNCTION (a symbol)."
547 (interactive
548 (let ((fn (function-called-at-point))
549 (enable-recursive-minibuffers t)
550 val)
551 (setq val (completing-read (if fn
552 (format "Describe function (default %s): " fn)
553 "Describe function: ")
554 obarray 'fboundp t nil nil (symbol-name fn)))
555 (list (if (equal val "")
556 fn (intern val)))))
557 (if function
558 (with-output-to-temp-buffer "*Help*"
559 (prin1 function)
560 ;; Use " is " instead of a colon so that
561 ;; it is easier to get out the function name using forward-sexp.
562 (princ " is ")
563 (let* ((def (symbol-function function))
564 file-name
565 (beg (if (commandp def) "an interactive " "a ")))
566 (princ (cond ((or (stringp def)
567 (vectorp def))
568 "a keyboard macro")
569 ((subrp def)
570 (concat beg "built-in function"))
571 ((byte-code-function-p def)
572 (concat beg "compiled Lisp function"))
573 ((symbolp def)
574 (format "alias for `%s'" def))
575 ((eq (car-safe def) 'lambda)
576 (concat beg "Lisp function"))
577 ((eq (car-safe def) 'macro)
578 "a Lisp macro")
579 ((eq (car-safe def) 'mocklisp)
580 "a mocklisp function")
581 ((eq (car-safe def) 'autoload)
582 (setq file-name (nth 1 def))
583 (format "%s autoloaded Lisp %s"
584 (if (commandp def) "an interactive" "an")
585 (if (nth 4 def) "macro" "function")
586 ))
587 (t "")))
588 (or file-name
589 (setq file-name (describe-function-find-file function)))
590 (if file-name
591 (progn
592 (princ " in `")
593 ;; We used to add .el to the file name,
594 ;; but that's completely wrong when the user used load-file.
595 (princ file-name)
596 (princ "'")))
597 (princ ".")
598 (terpri)
599 (let* ((inner-function (if (and (listp def) 'macro)
600 (cdr def)
601 def))
602 (arglist (cond ((byte-code-function-p inner-function)
603 (car (append inner-function nil)))
604 ((eq (car-safe inner-function) 'lambda)
605 (nth 1 inner-function))
606 (t t))))
607 (if (listp arglist)
608 (progn
609 (princ (cons function
610 (mapcar (lambda (arg)
611 (if (memq arg '(&optional &rest))
612 arg
613 (intern (upcase (symbol-name arg)))))
614 arglist)))
615 (terpri))))
616 (let ((doc (documentation function)))
617 (if doc
618 (progn (terpri)
619 (princ doc))
620 (princ "not documented"))))
621 (print-help-return-message)
622 (save-excursion
623 (set-buffer standard-output)
624 ;; Return the text we displayed.
625 (buffer-string)))
626 (message "You didn't specify a function")))
627
628 ;; We return 0 if we can't find a variable to return.
629 (defun variable-at-point ()
630 (condition-case ()
631 (let ((stab (syntax-table)))
632 (unwind-protect
633 (save-excursion
634 (set-syntax-table emacs-lisp-mode-syntax-table)
635 (or (not (zerop (skip-syntax-backward "_w")))
636 (eq (char-syntax (following-char)) ?w)
637 (eq (char-syntax (following-char)) ?_)
638 (forward-sexp -1))
639 (skip-chars-forward "'")
640 (let ((obj (read (current-buffer))))
641 (or (and (symbolp obj) (boundp obj) obj)
642 0)))
643 (set-syntax-table stab)))
644 (error 0)))
645
646 (defun describe-variable (variable)
647 "Display the full documentation of VARIABLE (a symbol).
648 Returns the documentation as a string, also."
649 (interactive
650 (let ((v (variable-at-point))
651 (enable-recursive-minibuffers t)
652 val)
653 (setq val (completing-read (if (symbolp v)
654 (format "Describe variable (default %s): " v)
655 "Describe variable: ")
656 obarray 'boundp t nil nil
657 (if (symbolp v) (symbol-name v))))
658 (list (if (equal val "")
659 v (intern val)))))
660 (if (symbolp variable)
661 (let (valvoid)
662 (with-output-to-temp-buffer "*Help*"
663 (prin1 variable)
664 (if (not (boundp variable))
665 (progn
666 (princ " is void")
667 (terpri)
668 (setq valvoid t))
669 (princ "'s value is ")
670 (terpri)
671 (pp (symbol-value variable))
672 (terpri))
673 (if (local-variable-p variable)
674 (progn
675 (princ (format "Local in buffer %s; " (buffer-name)))
676 (if (not (default-boundp variable))
677 (princ "globally void")
678 (princ "global value is ")
679 (terpri)
680 (pp (default-value variable)))
681 (terpri)))
682 (terpri)
683 (save-current-buffer
684 (set-buffer standard-output)
685 (if (> (count-lines (point-min) (point-max)) 10)
686 (progn
687 (goto-char (point-min))
688 (if valvoid
689 (forward-line 1)
690 (forward-sexp 1)
691 (delete-region (point) (progn (end-of-line) (point)))
692 (insert "'s value is shown below.\n\n")
693 (save-excursion
694 (insert "\n\nValue:"))))))
695 (princ "Documentation:")
696 (terpri)
697 (let ((doc (documentation-property variable 'variable-documentation)))
698 (princ (or doc "not documented as a variable.")))
699 (print-help-return-message)
700 (save-excursion
701 (set-buffer standard-output)
702 ;; Return the text we displayed.
703 (buffer-string))))
704 (message "You did not specify a variable")))
705
706 (defun describe-bindings (&optional prefix)
707 "Show a list of all defined keys, and their definitions.
708 We put that list in a buffer, and display the buffer.
709
710 The optional argument PREFIX, if non-nil, should be a key sequence;
711 then we display only bindings that start with that prefix."
712 (interactive "P")
713 (describe-bindings-internal nil prefix))
714
715 (defun where-is (definition &optional insert)
716 "Print message listing key sequences that invoke specified command.
717 Argument is a command definition, usually a symbol with a function definition.
718 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
719 (interactive
720 (let ((fn (function-called-at-point))
721 (enable-recursive-minibuffers t)
722 val)
723 (setq val (completing-read (if fn
724 (format "Where is command (default %s): " fn)
725 "Where is command: ")
726 obarray 'fboundp t))
727 (list (if (equal val "")
728 fn (intern val))
729 current-prefix-arg)))
730 (let* ((keys (where-is-internal definition overriding-local-map nil nil))
731 (keys1 (mapconcat 'key-description keys ", "))
732 (standard-output (if insert (current-buffer) t)))
733 (if insert
734 (if (> (length keys1) 0)
735 (princ (format "%s (%s)" keys1 definition))
736 (princ (format "M-x %s RET" definition)))
737 (if (> (length keys1) 0)
738 (princ (format "%s is on %s" definition keys1))
739 (princ (format "%s is not on any key" definition)))))
740 nil)
741
742 (defun locate-library (library &optional nosuffix path interactive-call)
743 "Show the precise file name of Emacs library LIBRARY.
744 This command searches the directories in `load-path' like `M-x load-library'
745 to find the file that `M-x load-library RET LIBRARY RET' would load.
746 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
747 to the specified name LIBRARY.
748
749 If the optional third arg PATH is specified, that list of directories
750 is used instead of `load-path'."
751 (interactive (list (read-string "Locate library: ")
752 nil nil
753 t))
754 (let (result)
755 (catch 'answer
756 (mapcar
757 (lambda (dir)
758 (mapcar
759 (lambda (suf)
760 (let ((try (expand-file-name (concat library suf) dir)))
761 (and (file-readable-p try)
762 (null (file-directory-p try))
763 (progn
764 (setq result try)
765 (throw 'answer try)))))
766 (if nosuffix
767 '("")
768 '(".elc" ".el" "")
769 ;;; load doesn't handle this yet.
770 ;;; (let ((basic '(".elc" ".el" ""))
771 ;;; (compressed '(".Z" ".gz" "")))
772 ;;; ;; If autocompression mode is on,
773 ;;; ;; consider all combinations of library suffixes
774 ;;; ;; and compression suffixes.
775 ;;; (if (rassq 'jka-compr-handler file-name-handler-alist)
776 ;;; (apply 'nconc
777 ;;; (mapcar (lambda (compelt)
778 ;;; (mapcar (lambda (baselt)
779 ;;; (concat baselt compelt))
780 ;;; basic))
781 ;;; compressed))
782 ;;; basic))
783 )))
784 (or path load-path)))
785 (and interactive-call
786 (if result
787 (message "Library is file %s" result)
788 (message "No library %s in search path" library)))
789 result))
790
791 ;;; help.el ends here