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