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