Initial revision
[bpt/emacs.git] / lisp / help.el
1 ;;; help.el --- help commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998 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 (define-key help-mode-map [mouse-2] 'help-follow-mouse)
95 (define-key help-mode-map "\C-c\C-b" 'help-go-back)
96 (define-key help-mode-map "\C-c\C-c" 'help-follow)
97 (define-key help-mode-map "\t" 'help-next-ref)
98 (define-key help-mode-map [backtab] 'help-previous-ref)
99 ;; Documentation only, since we use minor-mode-overriding-map-alist.
100 (define-key help-mode-map "\r" 'help-follow)
101
102 ;; Font-locking is incompatible with the new xref stuff.
103 ;(defvar help-font-lock-keywords
104 ; (eval-when-compile
105 ; (let ((name-char "[-+a-zA-Z0-9_*]") (sym-char "[-+a-zA-Z0-9_:*]"))
106 ; (list
107 ; ;;
108 ; ;; The symbol itself.
109 ; (list (concat "\\`\\(" name-char "+\\)\\(\\(:\\)\\|\\('\\)\\)")
110 ; '(1 (if (match-beginning 3)
111 ; font-lock-function-name-face
112 ; font-lock-variable-name-face)))
113 ; ;;
114 ; ;; Words inside `' which tend to be symbol names.
115 ; (list (concat "`\\(" sym-char sym-char "+\\)'")
116 ; 1 'font-lock-constant-face t)
117 ; ;;
118 ; ;; CLisp `:' keywords as references.
119 ; (list (concat "\\<:" sym-char "+\\>") 0 'font-lock-builtin-face t))))
120 ; "Default expressions to highlight in Help mode.")
121
122 (defvar help-xref-stack nil
123 "A stack of ways by which to return to help buffers after following xrefs.
124 Used by `help-follow' and `help-xref-go-back'.")
125 (put 'help-xref-stack 'permanent-local t)
126
127 (defvar help-xref-stack-item nil
128 "An item for `help-follow' in this buffer to push onto `help-xref-stack'.")
129 (put 'help-xref-stack-item 'permanent-local t)
130
131 (setq-default help-xref-stack nil help-xref-stack-item nil)
132
133 (defun help-mode ()
134 "Major mode for viewing help text and navigating references in it.
135 Entry to this mode runs the normal hook `help-mode-hook'.
136 Commands:
137 \\{help-mode-map}"
138 (interactive)
139 (kill-all-local-variables)
140 (use-local-map help-mode-map)
141 (setq mode-name "Help")
142 (setq major-mode 'help-mode)
143 (make-local-variable 'font-lock-defaults)
144 (setq font-lock-defaults nil) ; font-lock would defeat xref
145 (view-mode)
146 (make-local-variable 'view-no-disable-on-exit)
147 (setq view-no-disable-on-exit t)
148 ;; `help-make-xrefs' would be run here if not invoked from
149 ;; `help-mode-maybe'.
150 (run-hooks 'help-mode-hook))
151
152 (defun help-mode-maybe ()
153 (if (eq major-mode 'fundamental-mode)
154 (help-mode))
155 (when (eq major-mode 'help-mode)
156 ;; View mode's read-only status of existing *Help* buffer is lost
157 ;; by with-output-to-temp-buffer.
158 (toggle-read-only 1)
159 (help-make-xrefs (current-buffer)))
160 (setq view-return-to-alist
161 (list (cons (selected-window) help-return-method))))
162
163 (add-hook 'temp-buffer-show-hook 'help-mode-maybe)
164
165 (defun help-quit ()
166 (interactive)
167 nil)
168
169 (defun help-with-tutorial (&optional arg)
170 "Select the Emacs learn-by-doing tutorial.
171 If there is a tutorial version written in the language
172 of the selected language environment, that version is used.
173 If there's no tutorial in that language, `TUTORIAL' is selected.
174 With arg, you are asked to choose which language."
175 (interactive "P")
176 (let ((lang (if arg
177 (read-language-name 'tutorial "Language: " "English")
178 (if (get-language-info current-language-environment 'tutorial)
179 current-language-environment
180 "English")))
181 file filename)
182 (setq filename (get-language-info lang 'tutorial))
183 (setq file (expand-file-name (concat "~/" filename)))
184 (delete-other-windows)
185 (if (get-file-buffer file)
186 (switch-to-buffer (get-file-buffer file))
187 (switch-to-buffer (create-file-buffer file))
188 (setq buffer-file-name file)
189 (setq default-directory (expand-file-name "~/"))
190 (setq buffer-auto-save-file-name nil)
191 (insert-file-contents (expand-file-name filename data-directory))
192 (goto-char (point-min))
193 (search-forward "\n<<")
194 (beginning-of-line)
195 (delete-region (point) (progn (end-of-line) (point)))
196 (let ((n (- (window-height (selected-window))
197 (count-lines (point-min) (point))
198 6)))
199 (if (< n 12)
200 (newline n)
201 ;; Some people get confused by the large gap.
202 (newline (/ n 2))
203 (insert "[Middle of page left blank for didactic purposes. "
204 "Text continues below]")
205 (newline (- n (/ n 2)))))
206 (goto-char (point-min))
207 (set-buffer-modified-p nil))))
208
209 (defun describe-key-briefly (key &optional insert)
210 "Print the name of the function KEY invokes. KEY is a string.
211 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
212 (interactive "kDescribe key briefly: \nP")
213 ;; If this key seq ends with a down event, discard the
214 ;; following click or drag event. Otherwise that would
215 ;; erase the message.
216 (let ((type (aref key (1- (length key)))))
217 (if (listp type) (setq type (car type)))
218 (and (symbolp type)
219 (memq 'down (event-modifiers type))
220 (read-event)))
221 (save-excursion
222 (let ((modifiers (event-modifiers (aref key 0)))
223 (standard-output (if insert (current-buffer) t))
224 window position)
225 ;; For a mouse button event, go to the button it applies to
226 ;; to get the right key bindings. And go to the right place
227 ;; in case the keymap depends on where you clicked.
228 (if (or (memq 'click modifiers) (memq 'down modifiers)
229 (memq 'drag modifiers))
230 (setq window (posn-window (event-start (aref key 0)))
231 position (posn-point (event-start (aref key 0)))))
232 (if (windowp window)
233 (progn
234 (set-buffer (window-buffer window))
235 (goto-char position)))
236 ;; Ok, now look up the key and name the command.
237 (let ((defn (key-binding key))
238 (key-desc (key-description key)))
239 (if (or (null defn) (integerp defn))
240 (princ (format "%s is undefined" key-desc))
241 (princ (format (if insert
242 "%s (%s)"
243 (if (windowp window)
244 "%s at that spot runs the command %s"
245 "%s runs the command %s"))
246 key-desc
247 (if (symbolp defn) defn (prin1-to-string defn)))))))))
248
249 (defvar help-return-method nil
250 "What to do to \"exit\" the help buffer.
251 This is a list
252 (WINDOW . t) delete the selected window, go to WINDOW.
253 (WINDOW . quit-window) do quit-window, then select WINDOW.
254 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
255
256 (defun print-help-return-message (&optional function)
257 "Display or return message saying how to restore windows after help command.
258 Computes a message and applies the optional argument FUNCTION to it.
259 If FUNCTION is nil, applies `message' to it, thus printing it."
260 (and (not (get-buffer-window standard-output))
261 (let ((first-message
262 (cond ((special-display-p (buffer-name standard-output))
263 (setq help-return-method (cons (selected-window) t))
264 ;; If the help output buffer is a special display buffer,
265 ;; don't say anything about how to get rid of it.
266 ;; First of all, the user will do that with the window
267 ;; manager, not with Emacs.
268 ;; Secondly, the buffer has not been displayed yet,
269 ;; so we don't know whether its frame will be selected.
270 nil)
271 ((not (one-window-p t))
272 (setq help-return-method
273 (cons (selected-window) 'quit-window))
274 "Type \\[switch-to-buffer-other-window] RET to restore the other window.")
275 (pop-up-windows
276 (setq help-return-method (cons (selected-window) t))
277 "Type \\[delete-other-windows] to remove help window.")
278 (t
279 (setq help-return-method
280 (list (selected-window) (window-buffer)
281 (window-start) (window-point)))
282 "Type \\[switch-to-buffer] RET to remove help window."))))
283 (funcall (or function 'message)
284 (concat
285 (if first-message
286 (substitute-command-keys first-message)
287 "")
288 (if first-message " " "")
289 ;; If the help buffer will go in a separate frame,
290 ;; it's no use mentioning a command to scroll, so don't.
291 (if (special-display-p (buffer-name standard-output))
292 nil
293 (if (same-window-p (buffer-name standard-output))
294 ;; Say how to scroll this window.
295 (substitute-command-keys
296 "\\[scroll-up] to scroll the help.")
297 ;; Say how to scroll some other window.
298 (substitute-command-keys
299 "\\[scroll-other-window] to scroll the help."))))))))
300
301 (defun describe-key (key)
302 "Display documentation of the function invoked by KEY. KEY is a string."
303 (interactive "kDescribe key: ")
304 ;; If this key seq ends with a down event, discard the
305 ;; following click or drag event. Otherwise that would
306 ;; erase the message.
307 (let ((type (aref key (1- (length key)))))
308 (if (listp type) (setq type (car type)))
309 (and (symbolp type)
310 (memq 'down (event-modifiers type))
311 (read-event)))
312 (save-excursion
313 (let ((modifiers (event-modifiers (aref key 0)))
314 window position)
315 ;; For a mouse button event, go to the button it applies to
316 ;; to get the right key bindings. And go to the right place
317 ;; in case the keymap depends on where you clicked.
318 (if (or (memq 'click modifiers) (memq 'down modifiers)
319 (memq 'drag modifiers))
320 (setq window (posn-window (event-start (aref key 0)))
321 position (posn-point (event-start (aref key 0)))))
322 (if (windowp window)
323 (progn
324 (set-buffer (window-buffer window))
325 (goto-char position)))
326 (let ((defn (key-binding key)))
327 (if (or (null defn) (integerp defn))
328 (message "%s is undefined" (key-description key))
329 (with-output-to-temp-buffer "*Help*"
330 (princ (key-description key))
331 (if (windowp window)
332 (princ " at that spot"))
333 (princ " runs the command ")
334 (prin1 defn)
335 (princ "\n which is ")
336 (describe-function-1 defn nil)
337 (print-help-return-message)))))))
338
339 (defun describe-mode ()
340 "Display documentation of current major mode and minor modes.
341 For this to work correctly for a minor mode, the mode's indicator variable
342 \(listed in `minor-mode-alist') must also be a function whose documentation
343 describes the minor mode."
344 (interactive)
345 (with-output-to-temp-buffer "*Help*"
346 (let ((minor-modes minor-mode-alist)
347 (first t))
348 (while minor-modes
349 (let* ((minor-mode (car (car minor-modes)))
350 (indicator (car (cdr (car minor-modes)))))
351 ;; Document a minor mode if it is listed in minor-mode-alist,
352 ;; bound locally in this buffer, non-nil, and has a function
353 ;; definition.
354 (if (and (symbol-value minor-mode)
355 (fboundp minor-mode))
356 (let ((pretty-minor-mode minor-mode))
357 (if (string-match "-mode$" (symbol-name minor-mode))
358 (setq pretty-minor-mode
359 (capitalize
360 (substring (symbol-name minor-mode)
361 0 (match-beginning 0)))))
362 (while (and indicator (symbolp indicator))
363 (setq indicator (symbol-value indicator)))
364 (if first
365 (princ "The minor modes are described first,
366 followed by the major mode, which is described on the last page.\n\f\n"))
367 (setq first nil)
368 (princ (format "%s minor mode (%s):\n"
369 pretty-minor-mode
370 (if indicator
371 (format "indicator%s" indicator)
372 "no indicator")))
373 (princ (documentation minor-mode))
374 (princ "\n\f\n"))))
375 (setq minor-modes (cdr minor-modes))))
376 (princ mode-name)
377 (princ " mode:\n")
378 (princ (documentation major-mode))
379 (help-setup-xref (cons #'help-xref-mode (current-buffer)) (interactive-p))
380 (print-help-return-message)))
381
382 ;; So keyboard macro definitions are documented correctly
383 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
384
385 (defun describe-distribution ()
386 "Display info on how to obtain the latest version of GNU Emacs."
387 (interactive)
388 (find-file-read-only
389 (expand-file-name "DISTRIB" data-directory)))
390
391 (defun describe-copying ()
392 "Display info on how you may redistribute copies of GNU Emacs."
393 (interactive)
394 (find-file-read-only
395 (expand-file-name "COPYING" data-directory))
396 (goto-char (point-min)))
397
398 (defun describe-project ()
399 "Display info on the GNU project."
400 (interactive)
401 (find-file-read-only
402 (expand-file-name "GNU" data-directory))
403 (goto-char (point-min)))
404
405 (defun describe-no-warranty ()
406 "Display info on all the kinds of warranty Emacs does NOT have."
407 (interactive)
408 (describe-copying)
409 (let (case-fold-search)
410 (search-forward "NO WARRANTY")
411 (recenter 0)))
412
413 (defun describe-prefix-bindings ()
414 "Describe the bindings of the prefix used to reach this command.
415 The prefix described consists of all but the last event
416 of the key sequence that ran this command."
417 (interactive)
418 (let* ((key (this-command-keys)))
419 (describe-bindings
420 (if (stringp key)
421 (substring key 0 (1- (length key)))
422 (let ((prefix (make-vector (1- (length key)) nil))
423 (i 0))
424 (while (< i (length prefix))
425 (aset prefix i (aref key i))
426 (setq i (1+ i)))
427 prefix)))))
428 ;; Make C-h after a prefix, when not specifically bound,
429 ;; run describe-prefix-bindings.
430 (setq prefix-help-command 'describe-prefix-bindings)
431
432 (defun view-emacs-news (&optional arg)
433 "Display info on recent changes to Emacs.
434 With numeric argument display information on correspondingly older changes."
435 (interactive "P")
436 (let* ((arg (if arg (prefix-numeric-value arg) 0)))
437 (find-file-read-only
438 (expand-file-name (concat (make-string arg ?O) "NEWS")
439 data-directory))))
440
441 (defun view-emacs-FAQ ()
442 "Display the Emacs Frequently Asked Questions (FAQ) file."
443 (interactive)
444 (find-file-read-only (expand-file-name "FAQ" data-directory)))
445
446 (defun view-lossage ()
447 "Display last 100 input keystrokes."
448 (interactive)
449 (with-output-to-temp-buffer "*Help*"
450 (princ (mapconcat (function (lambda (key)
451 (if (or (integerp key)
452 (symbolp key)
453 (listp key))
454 (single-key-description key)
455 (prin1-to-string key nil))))
456 (recent-keys)
457 " "))
458 (save-excursion
459 (set-buffer standard-output)
460 (goto-char (point-min))
461 (while (progn (move-to-column 50) (not (eobp)))
462 (search-forward " " nil t)
463 (insert "\n")))
464 (setq help-xref-stack nil
465 help-xref-stack-item nil)
466 (print-help-return-message)))
467
468 (defalias 'help 'help-for-help)
469 (make-help-screen help-for-help
470 "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:"
471 "You have typed \\[help-command], the help character. Type a Help option:
472 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
473
474 a command-apropos. Give a substring, and see a list of commands
475 (functions interactively callable) that contain
476 that substring. See also the apropos command.
477 b describe-bindings. Display table of all key bindings.
478 c describe-key-briefly. Type a command key sequence;
479 it prints the function name that sequence runs.
480 C describe-coding-system. This describes either a specific coding system
481 (if you type its name) or the coding systems currently in use
482 (if you type just RET).
483 f describe-function. Type a function name and get documentation of it.
484 C-f Info-goto-emacs-command-node. Type a function name;
485 it takes you to the Info node for that command.
486 i info. The info documentation reader.
487 I describe-input-method. Describe a specific input method (if you type
488 its name) or the current input method (if you type just RET).
489 k describe-key. Type a command key sequence;
490 it displays the full documentation.
491 C-k Info-goto-emacs-key-command-node. Type a command key sequence;
492 it takes you to the Info node for the command bound to that key.
493 l view-lossage. Shows last 100 characters you typed.
494 L describe-language-environment. This describes either the a
495 specific language environment (if you type its name)
496 or the current language environment (if you type just RET).
497 m describe-mode. Print documentation of current minor modes,
498 and the current major mode, including their special commands.
499 n view-emacs-news. Shows emacs news file.
500 p finder-by-keyword. Find packages matching a given topic keyword.
501 s describe-syntax. Display contents of syntax table, plus explanations
502 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
503 v describe-variable. Type name of a variable;
504 it displays the variable's documentation and value.
505 w where-is. Type command name; it prints which keystrokes
506 invoke that command.
507
508 F Display the frequently asked questions file.
509 h Display the HELLO file which illustrates various scripts.
510 C-c Display Emacs copying permission (General Public License).
511 C-d Display Emacs ordering information.
512 C-n Display news of recent Emacs changes.
513 C-p Display information about the GNU project.
514 C-w Display information on absence of warranty for GNU Emacs."
515 help-map)
516
517 ;; Return a function which is called by the list containing point.
518 ;; If that gives no function, return a function whose name is around point.
519 ;; If that doesn't give a function, return nil.
520 (defun function-called-at-point ()
521 (let ((stab (syntax-table)))
522 (set-syntax-table emacs-lisp-mode-syntax-table)
523 (unwind-protect
524 (or (condition-case ()
525 (save-excursion
526 (save-restriction
527 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
528 ;; Move up to surrounding paren, then after the open.
529 (backward-up-list 1)
530 (forward-char 1)
531 ;; If there is space here, this is probably something
532 ;; other than a real Lisp function call, so ignore it.
533 (if (looking-at "[ \t]")
534 (error "Probably not a Lisp function call"))
535 (let (obj)
536 (setq obj (read (current-buffer)))
537 (and (symbolp obj) (fboundp obj) obj))))
538 (error nil))
539 (condition-case ()
540 (save-excursion
541 (or (not (zerop (skip-syntax-backward "_w")))
542 (eq (char-syntax (following-char)) ?w)
543 (eq (char-syntax (following-char)) ?_)
544 (forward-sexp -1))
545 (skip-chars-forward "'")
546 (let ((obj (read (current-buffer))))
547 (and (symbolp obj) (fboundp obj) obj)))
548 (error nil)))
549 (set-syntax-table stab))))
550
551 (defun describe-function-find-file (function)
552 (let ((files load-history)
553 file functions)
554 (while files
555 (if (memq function (cdr (car files)))
556 (setq file (car (car files)) files nil))
557 (setq files (cdr files)))
558 file))
559
560 (defun describe-function (function)
561 "Display the full documentation of FUNCTION (a symbol)."
562 (interactive
563 (let ((fn (function-called-at-point))
564 (enable-recursive-minibuffers t)
565 val)
566 (setq val (completing-read (if fn
567 (format "Describe function (default %s): " fn)
568 "Describe function: ")
569 obarray 'fboundp t nil nil (symbol-name fn)))
570 (list (if (equal val "")
571 fn (intern val)))))
572 (if function
573 (with-output-to-temp-buffer "*Help*"
574 (prin1 function)
575 ;; Use " is " instead of a colon so that
576 ;; it is easier to get out the function name using forward-sexp.
577 (princ " is ")
578 (describe-function-1 function nil)
579 (print-help-return-message)
580 (save-excursion
581 (set-buffer standard-output)
582 ;; Return the text we displayed.
583 (buffer-string)))
584 (message "You didn't specify a function")))
585
586 (defun describe-function-1 (function parens)
587 (let* ((def (symbol-function function))
588 file-name string need-close
589 (beg (if (commandp def) "an interactive " "a ")))
590 (setq string
591 (cond ((or (stringp def)
592 (vectorp def))
593 "a keyboard macro")
594 ((subrp def)
595 (concat beg "built-in function"))
596 ((byte-code-function-p def)
597 (concat beg "compiled Lisp function"))
598 ((symbolp def)
599 (format "alias for `%s'" def))
600 ((eq (car-safe def) 'lambda)
601 (concat beg "Lisp function"))
602 ((eq (car-safe def) 'macro)
603 "a Lisp macro")
604 ((eq (car-safe def) 'mocklisp)
605 "a mocklisp function")
606 ((eq (car-safe def) 'autoload)
607 (setq file-name (nth 1 def))
608 (format "%s autoloaded Lisp %s"
609 (if (commandp def) "an interactive" "an")
610 (if (nth 4 def) "macro" "function")
611 ))
612 (t "")))
613 (when (and parens (not (equal string "")))
614 (setq need-close t)
615 (princ "("))
616 (princ string)
617 (or file-name
618 (setq file-name (describe-function-find-file function)))
619 (if file-name
620 (progn
621 (princ " in `")
622 ;; We used to add .el to the file name,
623 ;; but that's completely wrong when the user used load-file.
624 (princ file-name)
625 (princ "'")))
626 (if need-close (princ ")"))
627 (princ ".")
628 (terpri)
629 (let* ((inner-function (if (and (listp def) 'macro)
630 (cdr def)
631 def))
632 (arglist (cond ((byte-code-function-p inner-function)
633 (car (append inner-function nil)))
634 ((eq (car-safe inner-function) 'lambda)
635 (nth 1 inner-function))
636 (t t))))
637 (if (listp arglist)
638 (progn
639 (princ (cons function
640 (mapcar (lambda (arg)
641 (if (memq arg '(&optional &rest))
642 arg
643 (intern (upcase (symbol-name arg)))))
644 arglist)))
645 (terpri))))
646 (let ((doc (documentation function)))
647 (if doc
648 (progn (terpri)
649 (princ doc)
650 (help-setup-xref (cons #'describe-function function) (interactive-p)))
651 (princ "not documented")))))
652
653 ;; We return 0 if we can't find a variable to return.
654 (defun variable-at-point ()
655 (condition-case ()
656 (let ((stab (syntax-table)))
657 (unwind-protect
658 (save-excursion
659 (set-syntax-table emacs-lisp-mode-syntax-table)
660 (or (not (zerop (skip-syntax-backward "_w")))
661 (eq (char-syntax (following-char)) ?w)
662 (eq (char-syntax (following-char)) ?_)
663 (forward-sexp -1))
664 (skip-chars-forward "'")
665 (let ((obj (read (current-buffer))))
666 (or (and (symbolp obj) (boundp obj) obj)
667 0)))
668 (set-syntax-table stab)))
669 (error 0)))
670
671 (defun describe-variable (variable)
672 "Display the full documentation of VARIABLE (a symbol).
673 Returns the documentation as a string, also."
674 (interactive
675 (let ((v (variable-at-point))
676 (enable-recursive-minibuffers t)
677 val)
678 (setq val (completing-read (if (symbolp v)
679 (format "Describe variable (default %s): " v)
680 "Describe variable: ")
681 obarray 'boundp t nil nil
682 (if (symbolp v) (symbol-name v))))
683 (list (if (equal val "")
684 v (intern val)))))
685 (if (symbolp variable)
686 (let (valvoid)
687 (with-output-to-temp-buffer "*Help*"
688 (prin1 variable)
689 (if (not (boundp variable))
690 (progn
691 (princ " is void")
692 (terpri)
693 (setq valvoid t))
694 (princ "'s value is ")
695 (terpri)
696 (pp (symbol-value variable))
697 (terpri))
698 (if (local-variable-p variable)
699 (progn
700 (princ (format "Local in buffer %s; " (buffer-name)))
701 (if (not (default-boundp variable))
702 (princ "globally void")
703 (princ "global value is ")
704 (terpri)
705 (pp (default-value variable)))
706 (terpri)))
707 (terpri)
708 (save-current-buffer
709 (set-buffer standard-output)
710 (if (> (count-lines (point-min) (point-max)) 10)
711 (progn
712 (goto-char (point-min))
713 (if valvoid
714 (forward-line 1)
715 (forward-sexp 1)
716 (delete-region (point) (progn (end-of-line) (point)))
717 (insert "'s value is shown below.\n\n")
718 (save-excursion
719 (insert "\n\nValue:"))))))
720 (princ "Documentation:")
721 (terpri)
722 (let ((doc (documentation-property variable 'variable-documentation)))
723 (princ (or doc "not documented as a variable.")))
724 (help-setup-xref (cons #'describe-variable variable) (interactive-p))
725 (print-help-return-message)
726 (save-excursion
727 (set-buffer standard-output)
728 ;; Return the text we displayed.
729 (buffer-string))))
730 (message "You did not specify a variable")))
731
732 (defun describe-bindings (&optional prefix)
733 "Show a list of all defined keys, and their definitions.
734 We put that list in a buffer, and display the buffer.
735
736 The optional argument PREFIX, if non-nil, should be a key sequence;
737 then we display only bindings that start with that prefix."
738 (interactive "P")
739 (setq help-xref-stack nil
740 help-xref-stack-item nil)
741 (describe-bindings-internal nil prefix))
742
743 (defun where-is (definition &optional insert)
744 "Print message listing key sequences that invoke specified command.
745 Argument is a command definition, usually a symbol with a function definition.
746 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
747 (interactive
748 (let ((fn (function-called-at-point))
749 (enable-recursive-minibuffers t)
750 val)
751 (setq val (completing-read (if fn
752 (format "Where is command (default %s): " fn)
753 "Where is command: ")
754 obarray 'fboundp t))
755 (list (if (equal val "")
756 fn (intern val))
757 current-prefix-arg)))
758 (let* ((keys (where-is-internal definition overriding-local-map nil nil))
759 (keys1 (mapconcat 'key-description keys ", "))
760 (standard-output (if insert (current-buffer) t)))
761 (if insert
762 (if (> (length keys1) 0)
763 (princ (format "%s (%s)" keys1 definition))
764 (princ (format "M-x %s RET" definition)))
765 (if (> (length keys1) 0)
766 (princ (format "%s is on %s" definition keys1))
767 (princ (format "%s is not on any key" definition)))))
768 nil)
769
770 (defun locate-library (library &optional nosuffix path interactive-call)
771 "Show the precise file name of Emacs library LIBRARY.
772 This command searches the directories in `load-path' like `M-x load-library'
773 to find the file that `M-x load-library RET LIBRARY RET' would load.
774 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
775 to the specified name LIBRARY.
776
777 If the optional third arg PATH is specified, that list of directories
778 is used instead of `load-path'."
779 (interactive (list (read-string "Locate library: ")
780 nil nil
781 t))
782 (let (result)
783 (catch 'answer
784 (mapcar
785 (lambda (dir)
786 (mapcar
787 (lambda (suf)
788 (let ((try (expand-file-name (concat library suf) dir)))
789 (and (file-readable-p try)
790 (null (file-directory-p try))
791 (progn
792 (setq result try)
793 (throw 'answer try)))))
794 (if nosuffix
795 '("")
796 '(".elc" ".el" "")
797 ;;; load doesn't handle this yet.
798 ;;; (let ((basic '(".elc" ".el" ""))
799 ;;; (compressed '(".Z" ".gz" "")))
800 ;;; ;; If autocompression mode is on,
801 ;;; ;; consider all combinations of library suffixes
802 ;;; ;; and compression suffixes.
803 ;;; (if (rassq 'jka-compr-handler file-name-handler-alist)
804 ;;; (apply 'nconc
805 ;;; (mapcar (lambda (compelt)
806 ;;; (mapcar (lambda (baselt)
807 ;;; (concat baselt compelt))
808 ;;; basic))
809 ;;; compressed))
810 ;;; basic))
811 )))
812 (or path load-path)))
813 (and interactive-call
814 (if result
815 (message "Library is file %s" result)
816 (message "No library %s in search path" library)))
817 result))
818
819 \f
820 ;;; Grokking cross-reference information in doc strings and
821 ;;; hyperlinking it.
822
823 ;; This may have some scope for extension and the same or something
824 ;; similar should be done for widget doc strings, which currently use
825 ;; another mechanism.
826
827 (defcustom help-highlight-p t
828 "*If non-nil, `help-make-xrefs' highlight cross-references.
829 Under a window system it highlights them with face defined by
830 `help-highlight-face'. On a character terminal highlighted
831 references look like cross-references in info mode."
832 :group 'help
833 :version "20.3"
834 :type 'boolean)
835
836 (defcustom help-highlight-face 'underline
837 "Face used by `help-make-xrefs' to highlight cross-references.
838 Must be previously-defined."
839 :group 'help
840 :version "20.3"
841 :type 'symbol)
842
843 (defvar help-back-label "[back]"
844 "Label to use by `help-make-xrefs' for the go-back reference.")
845
846 (defvar help-xref-symbol-regexp
847 (concat "\\(\\<\\(\\(variable\\|option\\)\\|"
848 "\\(function\\|command\\)\\|"
849 "\\(symbol\\)\\)\\s-+\\)?"
850 ;; Note starting with word-syntax character:
851 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'")
852 "Regexp matching doc string references to symbols.
853
854 The words preceding the quoted symbol can be used in doc strings to
855 distinguish references to variables, functions and symbols.")
856
857 (defvar help-xref-info-regexp
858 "\\<info\\s-+node\\s-`\\([^']+\\)'"
859 "Regexp matching doc string references to an Info node.")
860
861 (defun help-setup-xref (item interactive-p)
862 "Invoked from commands using the \"*Help*\" buffer to install some xref info.
863
864 ITEM is a (function . args) pair appropriate for recreating the help
865 buffer after following a reference. INTERACTIVE-P is non-nil if the
866 calling command was invoked interactively. In this case the stack of
867 items for help buffer \"back\" buttons is cleared."
868 (if interactive-p
869 (setq help-xref-stack nil))
870 (setq help-xref-stack-item item))
871
872 (defun help-make-xrefs (&optional buffer)
873 "Parse and hyperlink documentation cross-references in the given BUFFER.
874
875 Find cross-reference information in a buffer and, if
876 `help-highlight-p' is non-nil, highlight it with face defined by
877 `help-highlight-face'; activate such cross references for selection
878 with `help-follow'. Cross-references have the canonical form `...'
879 and the type of reference may be disambiguated by the preceding
880 word(s) used in `help-xref-symbol-regexp'.
881
882 A special reference `back' is made to return back through a stack of
883 help buffers. Variable `help-back-label' specifies the text for
884 that."
885 (interactive "b")
886 (save-excursion
887 (set-buffer (or buffer (current-buffer)))
888 (goto-char (point-min))
889 ;; Skip the header-type info, though it might be useful to parse
890 ;; it at some stage (e.g. "function in `library'").
891 (forward-paragraph)
892 (let ((old-modified (buffer-modified-p)))
893 (let ((stab (syntax-table))
894 (case-fold-search t)
895 (inhibit-read-only t))
896 (set-syntax-table emacs-lisp-mode-syntax-table)
897 ;; The following should probably be abstracted out.
898 (unwind-protect
899 (progn
900 ;; Quoted symbols
901 (save-excursion
902 (while (re-search-forward help-xref-symbol-regexp nil t)
903 (let* ((data (match-string 6))
904 (sym (intern-soft data)))
905 (if sym
906 (cond
907 ((match-string 3) ; `variable' &c
908 (and (boundp sym) ; `variable' doesn't ensure
909 ; it's actually bound
910 (help-xref-button 6 #'describe-variable sym)))
911 ((match-string 4) ; `function' &c
912 (and (fboundp sym) ; similarly
913 (help-xref-button 6 #'describe-function sym)))
914 ((match-string 5)) ; nothing for symbol
915 ((and (boundp sym) (fboundp sym))
916 ;; We can't intuit whether to use the
917 ;; variable or function doc -- supply both.
918 (help-xref-button 6 #'help-xref-interned sym))
919 ((boundp sym)
920 (help-xref-button 6 #'describe-variable sym))
921 ((fboundp sym)
922 (help-xref-button 6 #'describe-function sym)))))))
923 ;; Info references
924 (save-excursion
925 (while (re-search-forward help-xref-info-regexp nil t)
926 (help-xref-button 1 #'Info-goto-node (list (match-data 1)))))
927 ;; An obvious case of a key substitution:
928 (save-excursion
929 (while (re-search-forward
930 "\\<M-x\\s-+\\(\\sw\\(\\sw\\|\\s_\\)+\\)" nil t)
931 (let ((sym (intern-soft (match-string 1))))
932 (if (fboundp sym)
933 (help-xref-button 1 #'describe-function sym)))))
934 ;; Look for commands in whole keymap substitutions:
935 (save-excursion
936 ;; Find a header and the column at which the command
937 ;; name will be found.
938 (while (re-search-forward "^key +binding\n\\(-+ +\\)-+\n\n"
939 nil t)
940 (let ((col (- (match-end 1) (match-beginning 1))))
941 (while
942 ;; Ignore single blank lines in table, but not
943 ;; double ones, which should terminate it.
944 (and (looking-at "^\n?[^\n]")
945 (progn
946 (if (and (> (move-to-column col) 0)
947 (looking-at "\\(\\sw\\|\\s_\\)+$"))
948 ;;
949 (let ((sym (intern-soft (match-string 0))))
950 (if (fboundp sym)
951 (help-xref-button
952 0 #'describe-function sym))))
953 t)
954 (zerop (forward-line))
955 (move-to-column 0)))))))
956 (set-syntax-table stab))
957 ;; Make a back-reference in this buffer if appropriate.
958 (when help-xref-stack
959 (goto-char (point-max))
960 (save-excursion
961 (insert "\n\n" help-back-label))
962 ;; Just to provide the match data:
963 (looking-at (concat "\n\n\\(" (regexp-quote help-back-label) "\\)"))
964 (help-xref-button 1 #'help-xref-go-back nil)))
965 ;; View mode steals RET from us.
966 (set (make-local-variable 'minor-mode-overriding-map-alist)
967 (list (cons 'view-mode
968 (let ((map (make-sparse-keymap)))
969 (set-keymap-parent map view-mode-map)
970 (define-key map "\r" 'help-follow)
971 map))))
972 (set-buffer-modified-p old-modified))))
973
974 (defun help-xref-button (match-number function data)
975 "Make a hyperlink for cross-reference text previously matched.
976
977 MATCH-NUMBER is the subexpression of interest in the last matched
978 regexp. FUNCTION is a function to invoke when the button is
979 activated, applied to DATA. DATA may be a single value or a list.
980 See `help-make-xrefs'."
981 (add-text-properties (match-beginning match-number)
982 (match-end match-number)
983 (list 'mouse-face 'highlight
984 'help-xref (cons function
985 (if (listp data)
986 data
987 (list data)))))
988 (if help-highlight-p
989 (put-text-property (match-beginning match-number)
990 (match-end match-number)
991 'face help-highlight-face)))
992
993 \f
994 ;; Additional functions for (re-)creating types of help buffers.
995 (defun help-xref-interned (symbol)
996 "Follow a hyperlink which appeared to be an arbitrary interned SYMBOL.
997
998 Both variable and function documentation are extracted into a single
999 help buffer."
1000 (let ((fdoc (describe-function symbol)))
1001 (describe-variable symbol)
1002 ;; We now have a help buffer on the variable. Insert the function
1003 ;; text after it.
1004 (goto-char (point-max))
1005 (insert "\n\n" fdoc))
1006 (goto-char (point-min))
1007 (help-setup-xref (cons #'help-xref-interned symbol) nil))
1008
1009 (defun help-xref-mode (buffer)
1010 "Do a `describe-mode' for the specified BUFFER."
1011 (save-excursion
1012 (set-buffer buffer)
1013 (describe-mode)))
1014 \f
1015 ;;; Navigation/hyperlinking with xrefs
1016
1017 (defun help-follow-mouse (click)
1018 "Follow the cross-reference that you click on."
1019 (interactive "e")
1020 (save-excursion
1021 (let* ((start (event-start click))
1022 (window (car start))
1023 (pos (car (cdr start))))
1024 (set-buffer (window-buffer window))
1025 (help-follow pos))))
1026
1027 (defun help-xref-go-back ()
1028 "Go back to the previous help buffer using info on `help-xref-stack'."
1029 (interactive)
1030 (when help-xref-stack
1031 (setq help-xref-stack (cdr help-xref-stack)) ; due to help-follow
1032 (let* ((item (car help-xref-stack))
1033 (method (car item))
1034 (args (cdr item)))
1035 (setq help-xref-stack (cdr help-xref-stack))
1036 (if (listp args)
1037 (apply method args)
1038 (funcall method args)))))
1039
1040 (defun help-go-back ()
1041 (interactive)
1042 (help-follow (1- (point-max))))
1043
1044 (defun help-follow (&optional pos)
1045 "Follow cross-reference at POS, defaulting to point.
1046
1047 For the cross-reference format, see `help-make-xrefs'."
1048 (interactive "d")
1049 (let* ((help-data (get-text-property pos 'help-xref))
1050 (method (car help-data))
1051 (args (cdr help-data)))
1052 (setq help-xref-stack (cons help-xref-stack-item help-xref-stack))
1053 (setq help-xref-stack-item nil)
1054 (when help-data
1055 ;; There is a reference at point. Follow it.
1056 (apply method args))))
1057
1058 ;; For tabbing through buffer.
1059 (defun help-next-ref ()
1060 "Find the next help cross-reference in the buffer."
1061 (interactive)
1062 (let (pos)
1063 (while (not pos)
1064 (if (get-text-property (point) 'help-xref) ; move off reference
1065 (goto-char (or (next-single-property-change (point) 'help-xref)
1066 (point))))
1067 (cond ((setq pos (next-single-property-change (point) 'help-xref))
1068 (if pos (goto-char pos)))
1069 ((bobp)
1070 (message "No cross references in the buffer.")
1071 (setq pos t))
1072 (t ; be circular
1073 (goto-char (point-min)))))))
1074
1075 (defun help-previous-ref ()
1076 "Find the previous help cross-reference in the buffer."
1077 (interactive)
1078 (let (pos)
1079 (while (not pos)
1080 (if (get-text-property (point) 'help-xref) ; move off reference
1081 (goto-char (or (previous-single-property-change (point) 'help-xref)
1082 (point))))
1083 (cond ((setq pos (previous-single-property-change (point) 'help-xref))
1084 (if pos (goto-char pos)))
1085 ((bobp)
1086 (message "No cross references in the buffer.")
1087 (setq pos t))
1088 (t ; be circular
1089 (goto-char (point-max)))))))
1090
1091 ;;; help.el ends here