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