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