(menu-bar-options-menu) <transient-mark-mode>: Fix
[bpt/emacs.git] / lisp / help.el
1 ;;; help.el --- help commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001
4 ;; Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: help, internal
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; This code implements GNU Emacs' on-line help system, the one invoked by
29 ;; `M-x help-for-help'.
30
31 ;;; Code:
32
33 ;; Get the macro make-help-screen when this is compiled,
34 ;; or run interpreted, but not when the compiled code is loaded.
35 (eval-when-compile (require 'help-macro))
36 (eval-when-compile (require 'view))
37
38 (defvar help-map (make-sparse-keymap)
39 "Keymap for characters following the Help key.")
40
41 (defvar help-mode-map (make-sparse-keymap)
42 "Keymap for help mode.")
43
44 (define-key global-map (char-to-string help-char) 'help-command)
45 (define-key global-map [help] 'help-command)
46 (define-key global-map [f1] 'help-command)
47 (fset 'help-command help-map)
48
49 (define-key help-map (char-to-string help-char) 'help-for-help)
50 (define-key help-map [help] 'help-for-help)
51 (define-key help-map [f1] 'help-for-help)
52 (define-key help-map "?" 'help-for-help)
53
54 (define-key help-map "\C-c" 'describe-copying)
55 (define-key help-map "\C-d" 'describe-distribution)
56 (define-key help-map "\C-w" 'describe-no-warranty)
57 (define-key help-map "\C-p" 'describe-project)
58 (define-key help-map "a" 'apropos-command)
59
60 (define-key help-map "b" 'describe-bindings)
61
62 (define-key help-map "c" 'describe-key-briefly)
63 (define-key help-map "k" 'describe-key)
64
65 (define-key help-map "d" 'describe-function)
66 (define-key help-map "f" 'describe-function)
67
68 (define-key help-map "F" 'view-emacs-FAQ)
69
70 (define-key help-map "i" 'info)
71 (define-key help-map "4i" 'info-other-window)
72 (define-key help-map "\C-f" 'Info-goto-emacs-command-node)
73 (define-key help-map "\C-k" 'Info-goto-emacs-key-command-node)
74 (define-key help-map "\C-i" 'info-lookup-symbol)
75
76 (define-key help-map "l" 'view-lossage)
77
78 (define-key help-map "m" 'describe-mode)
79 (define-key help-map "\C-m" 'view-order-manuals)
80
81 (define-key help-map "\C-n" 'view-emacs-news)
82 (define-key help-map "n" 'view-emacs-news)
83
84 (define-key help-map "p" 'finder-by-keyword)
85 (autoload 'finder-by-keyword "finder"
86 "Find packages matching a given keyword." t)
87
88 (define-key help-map "P" 'view-emacs-problems)
89
90 (define-key help-map "s" 'describe-syntax)
91
92 (define-key help-map "t" 'help-with-tutorial)
93
94 (define-key help-map "w" 'where-is)
95
96 (define-key help-map "v" 'describe-variable)
97
98 (define-key help-map "q" 'help-quit)
99
100 (define-key help-mode-map [mouse-2] 'help-follow-mouse)
101 (define-key help-mode-map "\C-c\C-b" 'help-go-back)
102 (define-key help-mode-map "\C-c\C-c" 'help-follow)
103 (define-key help-mode-map "\t" 'help-next-ref)
104 (define-key help-mode-map [backtab] 'help-previous-ref)
105 (define-key help-mode-map [(shift tab)] 'help-previous-ref)
106 ;; Documentation only, since we use minor-mode-overriding-map-alist.
107 (define-key help-mode-map "\r" 'help-follow)
108
109 (defvar help-xref-stack nil
110 "A stack of ways by which to return to help buffers after following xrefs.
111 Used by `help-follow' and `help-xref-go-back'.
112 An element looks like (POSITION FUNCTION ARGS...), where POSITION is
113 `(POINT . BUFFER-NAME)'.
114 To use the element, do (apply FUNCTION ARGS) then goto the point in
115 the named buffer.")
116 (put 'help-xref-stack 'permanent-local t)
117
118 (defvar help-xref-stack-item nil
119 "An item for `help-follow' in this buffer to push onto `help-xref-stack'.
120 The format is (FUNCTION ARGS...).")
121 (put 'help-xref-stack-item 'permanent-local t)
122
123 (setq-default help-xref-stack nil help-xref-stack-item nil)
124
125 (defcustom help-mode-hook nil
126 "Hook run by `help-mode'."
127 :type 'hook
128 :group 'help)
129
130 (defun help-mode ()
131 "Major mode for viewing help text and navigating references in it.
132 Entry to this mode runs the normal hook `help-mode-hook'.
133 Commands:
134 \\{help-mode-map}"
135 (interactive)
136 (kill-all-local-variables)
137 (use-local-map help-mode-map)
138 (setq mode-name "Help")
139 (setq major-mode 'help-mode)
140 (make-local-variable 'font-lock-defaults)
141 (setq font-lock-defaults nil) ; font-lock would defeat xref
142 (view-mode)
143 (make-local-variable 'view-no-disable-on-exit)
144 (setq view-no-disable-on-exit t)
145 (run-hooks 'help-mode-hook))
146
147 (defun help-mode-setup ()
148 (help-mode)
149 (setq buffer-read-only nil))
150
151 (add-hook 'temp-buffer-setup-hook 'help-mode-setup)
152
153 (defun help-mode-finish ()
154 (when (eq major-mode 'help-mode)
155 ;; View mode's read-only status of existing *Help* buffer is lost
156 ;; by with-output-to-temp-buffer.
157 (toggle-read-only 1)
158 (help-make-xrefs (current-buffer)))
159 (setq view-return-to-alist
160 (list (cons (selected-window) help-return-method))))
161
162 (add-hook 'temp-buffer-show-hook 'help-mode-finish)
163
164 (defun help-quit ()
165 "Just exit from the Help command's command loop."
166 (interactive)
167 nil)
168
169 (defun help-with-tutorial (&optional arg)
170 "Select the Emacs learn-by-doing tutorial.
171 If there is a tutorial version written in the language
172 of the selected language environment, that version is used.
173 If there's no tutorial in that language, `TUTORIAL' is selected.
174 With arg, you are asked to choose which language."
175 (interactive "P")
176 (let ((lang (if arg
177 (read-language-name 'tutorial "Language: " "English")
178 (if (get-language-info current-language-environment 'tutorial)
179 current-language-environment
180 "English")))
181 file filename)
182 (setq filename (get-language-info lang 'tutorial))
183 (setq file (expand-file-name (concat "~/" filename)))
184 (delete-other-windows)
185 (if (get-file-buffer file)
186 (switch-to-buffer (get-file-buffer file))
187 (switch-to-buffer (create-file-buffer file))
188 (setq buffer-file-name file)
189 (setq default-directory (expand-file-name "~/"))
190 (setq buffer-auto-save-file-name nil)
191 (insert-file-contents (expand-file-name filename data-directory))
192 (goto-char (point-min))
193 (search-forward "\n<<")
194 (beginning-of-line)
195 (delete-region (point) (progn (end-of-line) (point)))
196 (let ((n (- (window-height (selected-window))
197 (count-lines (point-min) (point))
198 6)))
199 (if (< n 12)
200 (newline n)
201 ;; Some people get confused by the large gap.
202 (newline (/ n 2))
203 (insert "[Middle of page left blank for didactic purposes. "
204 "Text continues below]")
205 (newline (- n (/ n 2)))))
206 (goto-char (point-min))
207 (set-buffer-modified-p nil))))
208
209 (defun string-key-binding (key)
210 "Value is the binding of KEY in a string.
211 If KEY is an event on a string, and that string has a `local-map'
212 or `keymap' property, return the binding of KEY in the string's keymap."
213 (let* ((defn nil)
214 (start (when (vectorp key)
215 (if (memq (aref key 0) '(mode-line header-line))
216 (event-start (aref key 1))
217 (and (consp (aref key 0))
218 (event-start (aref key 0))))))
219 (string-info (and (consp start) (nth 4 start))))
220 (when string-info
221 (let* ((string (car string-info))
222 (pos (cdr string-info))
223 (local-map (and (> pos 0)
224 (< pos (length string))
225 (or (get-text-property pos 'local-map string)
226 (get-text-property pos 'keymap string)))))
227 (setq defn (and local-map (lookup-key local-map key)))))
228 defn))
229
230 (defun describe-key-briefly (key &optional insert)
231 "Print the name of the function KEY invokes. KEY is a string.
232 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
233 (interactive "kDescribe key briefly: \nP")
234 (save-excursion
235 (let ((modifiers (event-modifiers (aref key 0)))
236 (standard-output (if insert (current-buffer) t))
237 window position)
238 ;; For a mouse button event, go to the button it applies to
239 ;; to get the right key bindings. And go to the right place
240 ;; in case the keymap depends on where you clicked.
241 (if (or (memq 'click modifiers) (memq 'down modifiers)
242 (memq 'drag modifiers))
243 (setq window (posn-window (event-start (aref key 0)))
244 position (posn-point (event-start (aref key 0)))))
245 (if (windowp window)
246 (progn
247 (set-buffer (window-buffer window))
248 (goto-char position)))
249 ;; Ok, now look up the key and name the command.
250 (let ((defn (or (string-key-binding key)
251 (key-binding key)))
252 (key-desc (key-description key)))
253 (if (or (null defn) (integerp defn))
254 (princ (format "%s is undefined" key-desc))
255 (princ (format (if insert
256 "`%s' (`%s')"
257 (if (windowp window)
258 "%s at that spot runs the command %s"
259 "%s runs the command %s"))
260 key-desc
261 (if (symbolp defn) defn (prin1-to-string defn)))))))))
262
263 (defvar help-return-method nil
264 "What to do to \"exit\" the help buffer.
265 This is a list
266 (WINDOW . t) delete the selected window, go to WINDOW.
267 (WINDOW . quit-window) do quit-window, then select WINDOW.
268 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
269
270 (defun print-help-return-message (&optional function)
271 "Display or return message saying how to restore windows after help command.
272 Computes a message and applies the optional argument FUNCTION to it.
273 If FUNCTION is nil, applies `message' to it, thus printing it."
274 (and (not (get-buffer-window standard-output))
275 (let ((first-message
276 (cond ((special-display-p (buffer-name standard-output))
277 (setq help-return-method (cons (selected-window) t))
278 ;; If the help output buffer is a special display buffer,
279 ;; don't say anything about how to get rid of it.
280 ;; First of all, the user will do that with the window
281 ;; manager, not with Emacs.
282 ;; Secondly, the buffer has not been displayed yet,
283 ;; so we don't know whether its frame will be selected.
284 nil)
285 (display-buffer-reuse-frames
286 (setq help-return-method (cons (selected-window)
287 'quit-window))
288 nil)
289 ((not (one-window-p t))
290 (setq help-return-method
291 (cons (selected-window) 'quit-window))
292 "Type \\[switch-to-buffer-other-window] RET to restore the other window.")
293 (pop-up-windows
294 (setq help-return-method (cons (selected-window) t))
295 "Type \\[delete-other-windows] to remove help window.")
296 (t
297 (setq help-return-method
298 (list (selected-window) (window-buffer)
299 (window-start) (window-point)))
300 "Type \\[switch-to-buffer] RET to remove help window."))))
301 (funcall (or function 'message)
302 (concat
303 (if first-message
304 (substitute-command-keys first-message))
305 (if first-message " ")
306 ;; If the help buffer will go in a separate frame,
307 ;; it's no use mentioning a command to scroll, so don't.
308 (if (special-display-p (buffer-name standard-output))
309 nil
310 (if (same-window-p (buffer-name standard-output))
311 ;; Say how to scroll this window.
312 (substitute-command-keys
313 "\\[scroll-up] to scroll the help.")
314 ;; Say how to scroll some other window.
315 (substitute-command-keys
316 "\\[scroll-other-window] to scroll the help."))))))))
317
318 (defun describe-key (key)
319 "Display documentation of the function invoked by KEY. KEY is a string."
320 (interactive "kDescribe key: ")
321 (save-excursion
322 (let ((modifiers (event-modifiers (aref key 0)))
323 window position)
324 ;; For a mouse button event, go to the button it applies to
325 ;; to get the right key bindings. And go to the right place
326 ;; in case the keymap depends on where you clicked.
327 (if (or (memq 'click modifiers) (memq 'down modifiers)
328 (memq 'drag modifiers))
329 (setq window (posn-window (event-start (aref key 0)))
330 position (posn-point (event-start (aref key 0)))))
331 (if (windowp window)
332 (progn
333 (set-buffer (window-buffer window))
334 (goto-char position)))
335 (let ((defn (or (string-key-binding key) (key-binding key))))
336 (if (or (null defn) (integerp defn))
337 (message "%s is undefined" (key-description key))
338 (with-output-to-temp-buffer "*Help*"
339 (princ (key-description key))
340 (if (windowp window)
341 (princ " at that spot"))
342 (princ " runs the command ")
343 (prin1 defn)
344 (princ "\n which is ")
345 (describe-function-1 defn nil (interactive-p))
346 (print-help-return-message)))))))
347
348 (defun describe-mode ()
349 "Display documentation of current major mode and minor modes.
350 The major mode description comes first, followed by the minor modes,
351 each on a separate page.
352
353 For this to work correctly for a minor mode, the mode's indicator variable
354 \(listed in `minor-mode-alist') must also be a function whose documentation
355 describes the minor mode."
356 (interactive)
357 (with-output-to-temp-buffer "*Help*"
358 (when minor-mode-alist
359 (princ "The major mode is described first.
360 For minor modes, see following pages.\n\n"))
361 (princ mode-name)
362 (princ " mode:\n")
363 (princ (documentation major-mode))
364 (help-setup-xref (list #'help-xref-mode (current-buffer)) (interactive-p))
365 (let ((minor-modes minor-mode-alist))
366 (while minor-modes
367 (let* ((minor-mode (car (car minor-modes)))
368 (indicator (car (cdr (car minor-modes)))))
369 ;; Document a minor mode if it is listed in minor-mode-alist,
370 ;; bound locally in this buffer, non-nil, and has a function
371 ;; definition.
372 (if (and (boundp minor-mode)
373 (symbol-value minor-mode)
374 (fboundp minor-mode))
375 (let ((pretty-minor-mode minor-mode))
376 (if (string-match "-mode$" (symbol-name minor-mode))
377 (setq pretty-minor-mode
378 (capitalize
379 (substring (symbol-name minor-mode)
380 0 (match-beginning 0)))))
381 (while (and indicator (symbolp indicator)
382 (boundp indicator)
383 (not (eq indicator (symbol-value indicator))))
384 (setq indicator (symbol-value indicator)))
385 (princ "\n\f\n")
386 (princ (format "%s minor mode (%s):\n"
387 pretty-minor-mode
388 (if indicator
389 (format "indicator%s" indicator)
390 "no indicator")))
391 (princ (documentation minor-mode)))))
392 (setq minor-modes (cdr minor-modes))))
393 (print-help-return-message)))
394
395 ;; So keyboard macro definitions are documented correctly
396 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
397
398 (defun describe-distribution ()
399 "Display info on how to obtain the latest version of GNU Emacs."
400 (interactive)
401 (find-file-read-only
402 (expand-file-name "DISTRIB" data-directory)))
403
404 (defun describe-copying ()
405 "Display info on how you may redistribute copies of GNU Emacs."
406 (interactive)
407 (find-file-read-only
408 (expand-file-name "COPYING" data-directory))
409 (goto-char (point-min)))
410
411 (defun describe-project ()
412 "Display info on the GNU project."
413 (interactive)
414 (find-file-read-only
415 (expand-file-name "THE-GNU-PROJECT" data-directory))
416 (goto-char (point-min)))
417
418 (defun describe-no-warranty ()
419 "Display info on all the kinds of warranty Emacs does NOT have."
420 (interactive)
421 (describe-copying)
422 (let (case-fold-search)
423 (search-forward "NO WARRANTY")
424 (recenter 0)))
425
426 (defun describe-prefix-bindings ()
427 "Describe the bindings of the prefix used to reach this command.
428 The prefix described consists of all but the last event
429 of the key sequence that ran this command."
430 (interactive)
431 (let* ((key (this-command-keys)))
432 (describe-bindings
433 (if (stringp key)
434 (substring key 0 (1- (length key)))
435 (let ((prefix (make-vector (1- (length key)) nil))
436 (i 0))
437 (while (< i (length prefix))
438 (aset prefix i (aref key i))
439 (setq i (1+ i)))
440 prefix)))))
441 ;; Make C-h after a prefix, when not specifically bound,
442 ;; run describe-prefix-bindings.
443 (setq prefix-help-command 'describe-prefix-bindings)
444
445 (defun view-emacs-news (&optional arg)
446 "Display info on recent changes to Emacs.
447 With numeric argument, display information on correspondingly older changes."
448 (interactive "P")
449 (let* ((arg (if arg (prefix-numeric-value arg) 0))
450 (file (cond ((eq arg 0) "NEWS")
451 ((eq arg 1) "ONEWS")
452 (t
453 (nth (- arg 2)
454 (nreverse (directory-files data-directory
455 nil "^ONEWS\\.[0-9]+$"
456 nil)))))))
457 (if file
458 (find-file-read-only (expand-file-name file data-directory))
459 (error "No such old news"))))
460
461 (defun view-order-manuals ()
462 "Display the Emacs ORDERS file."
463 (interactive)
464 (find-file-read-only (expand-file-name "ORDERS" data-directory))
465 (goto-address))
466
467 (defun view-emacs-FAQ ()
468 "Display the Emacs Frequently Asked Questions (FAQ) file."
469 (interactive)
470 ;;; (find-file-read-only (expand-file-name "FAQ" data-directory))
471 (info "(efaq)"))
472
473 (defun view-emacs-problems ()
474 "Display info on known problems with Emacs and possible workarounds."
475 (interactive)
476 (view-file (expand-file-name "PROBLEMS" data-directory)))
477
478 (defun view-lossage ()
479 "Display last 100 input keystrokes.
480
481 To record all your input on a file, use `open-dribble-file'."
482 (interactive)
483 (with-output-to-temp-buffer "*Help*"
484 (princ (mapconcat (function (lambda (key)
485 (if (or (integerp key)
486 (symbolp key)
487 (listp key))
488 (single-key-description key)
489 (prin1-to-string key nil))))
490 (recent-keys)
491 " "))
492 (save-excursion
493 (set-buffer standard-output)
494 (goto-char (point-min))
495 (while (progn (move-to-column 50) (not (eobp)))
496 (search-forward " " nil t)
497 (insert "\n"))
498 (setq help-xref-stack nil
499 help-xref-stack-item nil))
500 (print-help-return-message)))
501
502 (defalias 'help 'help-for-help)
503 (make-help-screen help-for-help
504 "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:"
505 "You have typed %THIS-KEY%, the help character. Type a Help option:
506 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
507
508 a command-apropos. Give a substring, and see a list of commands
509 (functions interactively callable) that contain
510 that substring. See also the apropos command.
511 b describe-bindings. Display table of all key bindings.
512 c describe-key-briefly. Type a command key sequence;
513 it prints the function name that sequence runs.
514 C describe-coding-system. This describes either a specific coding system
515 (if you type its name) or the coding systems currently in use
516 (if you type just RET).
517 f describe-function. Type a function name and get documentation of it.
518 C-f Info-goto-emacs-command-node. Type a function name;
519 it takes you to the Info node for that command.
520 i info. The info documentation reader.
521 I describe-input-method. Describe a specific input method (if you type
522 its name) or the current input method (if you type just RET).
523 C-i info-lookup-symbol. Display the definition of a specific symbol
524 as found in the manual for the language this buffer is written in.
525 k describe-key. Type a command key sequence;
526 it displays the full documentation.
527 C-k Info-goto-emacs-key-command-node. Type a command key sequence;
528 it takes you to the Info node for the command bound to that key.
529 l view-lossage. Show last 100 characters you typed.
530 L describe-language-environment. This describes either a
531 specific language environment (if you type its name)
532 or the current language environment (if you type just RET).
533 m describe-mode. Print documentation of current minor modes,
534 and the current major mode, including their special commands.
535 n view-emacs-news. Display news of recent Emacs changes.
536 p finder-by-keyword. Find packages matching a given topic keyword.
537 s describe-syntax. Display contents of syntax table, plus explanations
538 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
539 v describe-variable. Type name of a variable;
540 it displays the variable's documentation and value.
541 w where-is. Type command name; it prints which keystrokes
542 invoke that command.
543
544 F Display the frequently asked questions file.
545 h Display the HELLO file which illustrates various scripts.
546 C-c Display Emacs copying permission (General Public License).
547 C-d Display Emacs ordering information.
548 C-n Display news of recent Emacs changes.
549 C-p Display information about the GNU project.
550 C-w Display information on absence of warranty for GNU Emacs."
551 help-map)
552
553 (defun function-called-at-point ()
554 "Return a function around point or else called by the list containing point.
555 If that doesn't give a function, return nil."
556 (with-syntax-table emacs-lisp-mode-syntax-table
557 (or (condition-case ()
558 (save-excursion
559 (or (not (zerop (skip-syntax-backward "_w")))
560 (eq (char-syntax (following-char)) ?w)
561 (eq (char-syntax (following-char)) ?_)
562 (forward-sexp -1))
563 (skip-chars-forward "'")
564 (let ((obj (read (current-buffer))))
565 (and (symbolp obj) (fboundp obj) obj)))
566 (error nil))
567 (condition-case ()
568 (save-excursion
569 (save-restriction
570 (narrow-to-region (max (point-min)
571 (- (point) 1000)) (point-max))
572 ;; Move up to surrounding paren, then after the open.
573 (backward-up-list 1)
574 (forward-char 1)
575 ;; If there is space here, this is probably something
576 ;; other than a real Lisp function call, so ignore it.
577 (if (looking-at "[ \t]")
578 (error "Probably not a Lisp function call"))
579 (let ((obj (read (current-buffer))))
580 (and (symbolp obj) (fboundp obj) obj))))
581 (error nil)))))
582
583 (defvar symbol-file-load-history-loaded nil
584 "Non-nil means we have loaded the file `fns-VERSION.el' in `exec-directory'.
585 That file records the part of `load-history' for preloaded files,
586 which is cleared out before dumping to make Emacs smaller.")
587
588 (defun load-symbol-file-load-history ()
589 "Load the file `fns-VERSION.el' in `exec-directory' if not already done.
590 That file records the part of `load-history' for preloaded files,
591 which is cleared out before dumping to make Emacs smaller."
592 (unless symbol-file-load-history-loaded
593 (load (expand-file-name
594 ;; fns-XX.YY.ZZ.el does not work on DOS filesystem.
595 (if (eq system-type 'ms-dos)
596 "fns.el"
597 (format "fns-%s.el" emacs-version))
598 exec-directory)
599 ;; The file name fns-%s.el already has a .el extension.
600 nil nil t)
601 (setq symbol-file-load-history-loaded t)))
602
603 (defun symbol-file (function)
604 "Return the input source from which FUNCTION was loaded.
605 The value is normally a string that was passed to `load':
606 either an absolute file name, or a library name
607 \(with no directory name and no `.el' or `.elc' at the end).
608 It can also be nil, if the definition is not associated with any file."
609 (load-symbol-file-load-history)
610 (let ((files load-history)
611 file functions)
612 (while files
613 (if (memq function (cdr (car files)))
614 (setq file (car (car files)) files nil))
615 (setq files (cdr files)))
616 file))
617
618 (defun describe-function (function)
619 "Display the full documentation of FUNCTION (a symbol)."
620 (interactive
621 (let ((fn (function-called-at-point))
622 (enable-recursive-minibuffers t)
623 val)
624 (setq val (completing-read (if fn
625 (format "Describe function (default %s): " fn)
626 "Describe function: ")
627 obarray 'fboundp t nil nil (symbol-name fn)))
628 (list (if (equal val "")
629 fn (intern val)))))
630 (if function
631 (with-output-to-temp-buffer "*Help*"
632 (prin1 function)
633 ;; Use " is " instead of a colon so that
634 ;; it is easier to get out the function name using forward-sexp.
635 (princ " is ")
636 (describe-function-1 function nil (interactive-p))
637 (print-help-return-message)
638 (save-excursion
639 (set-buffer standard-output)
640 ;; Return the text we displayed.
641 (buffer-string)))
642 (message "You didn't specify a function")))
643
644 (defun describe-function-1 (function parens interactive-p)
645 (let* ((def (if (symbolp function)
646 (symbol-function function)
647 function))
648 file-name string need-close
649 (beg (if (commandp def) "an interactive " "a ")))
650 (setq string
651 (cond ((or (stringp def)
652 (vectorp def))
653 "a keyboard macro")
654 ((subrp def)
655 (if (eq 'unevalled (cdr (subr-arity def)))
656 (concat beg "special form")
657 (concat beg "built-in function")))
658 ((byte-code-function-p def)
659 (concat beg "compiled Lisp function"))
660 ((symbolp def)
661 (while (symbolp (symbol-function def))
662 (setq def (symbol-function def)))
663 (format "an alias for `%s'" def))
664 ((eq (car-safe def) 'lambda)
665 (concat beg "Lisp function"))
666 ((eq (car-safe def) 'macro)
667 "a Lisp macro")
668 ((eq (car-safe def) 'mocklisp)
669 "a mocklisp function")
670 ((eq (car-safe def) 'autoload)
671 (setq file-name (nth 1 def))
672 (format "%s autoloaded %s"
673 (if (commandp def) "an interactive" "an")
674 (if (eq (nth 4 def) 'keymap) "keymap"
675 (if (nth 4 def) "Lisp macro" "Lisp function"))
676 ))
677 ;; perhaps use keymapp here instead
678 ((eq (car-safe def) 'keymap)
679 (let ((is-full nil)
680 (elts (cdr-safe def)))
681 (while elts
682 (if (char-table-p (car-safe elts))
683 (setq is-full t
684 elts nil))
685 (setq elts (cdr-safe elts)))
686 (if is-full
687 "a full keymap"
688 "a sparse keymap")))
689 (t "")))
690 (when (and parens (not (equal string "")))
691 (setq need-close t)
692 (princ "("))
693 (princ string)
694 (with-current-buffer "*Help*"
695 (save-excursion
696 (save-match-data
697 (if (re-search-backward "alias for `\\([^`']+\\)'" nil t)
698 (help-xref-button 1 #'describe-function def
699 "mouse-2, RET: describe this function")))))
700 (or file-name
701 (setq file-name (symbol-file function)))
702 (if file-name
703 (progn
704 (princ " in `")
705 ;; We used to add .el to the file name,
706 ;; but that's completely wrong when the user used load-file.
707 (princ file-name)
708 (princ "'")
709 ;; Make a hyperlink to the library.
710 (with-current-buffer "*Help*"
711 (save-excursion
712 (re-search-backward "`\\([^`']+\\)'" nil t)
713 (help-xref-button
714 1
715 #'(lambda (arg)
716 (require 'find-func)
717 (let* ((location (find-function-search-for-symbol
718 arg nil (symbol-file arg))))
719 (pop-to-buffer (car location))
720 (goto-char (cdr location))))
721 function
722 "mouse-2, RET: find function's definition")))))
723 (if need-close (princ ")"))
724 (princ ".")
725 (terpri)
726 ;; Handle symbols aliased to other symbols.
727 (setq def (indirect-function def))
728 ;; If definition is a macro, find the function inside it.
729 (if (eq (car-safe def) 'macro)
730 (setq def (cdr def)))
731 (let ((arglist (cond ((byte-code-function-p def)
732 (car (append def nil)))
733 ((eq (car-safe def) 'lambda)
734 (nth 1 def))
735 ((and (eq (car-safe def) 'autoload)
736 (not (eq (nth 4 def) 'keymap)))
737 (concat "[Arg list not available until "
738 "function definition is loaded.]"))
739 (t t))))
740 (cond ((listp arglist)
741 (princ (cons (if (symbolp function) function "anonymous")
742 (mapcar (lambda (arg)
743 (if (memq arg '(&optional &rest))
744 arg
745 (intern (upcase (symbol-name arg)))))
746 arglist)))
747 (terpri))
748 ((stringp arglist)
749 (princ arglist)
750 (terpri))))
751 (let ((doc (documentation function)))
752 (if doc
753 (progn (terpri)
754 (princ doc)
755 (if (subrp def)
756 (with-current-buffer standard-output
757 (beginning-of-line)
758 ;; Builtins get the calling sequence at the end of
759 ;; the doc string. Move it to the same place as
760 ;; for other functions.
761
762 ;; In cases where `function' has been fset to a
763 ;; subr we can't search for function's name in
764 ;; the doc string. Kluge round that using the
765 ;; printed representation. The arg list then
766 ;; shows the wrong function name, but that
767 ;; might be a useful hint.
768 (let* ((rep (prin1-to-string def))
769 (name (progn
770 (string-match " \\([^ ]+\\)>$" rep)
771 (match-string 1 rep))))
772 (if (looking-at (format "(%s[ )]" (regexp-quote name)))
773 (let ((start (point-marker)))
774 (goto-char (point-min))
775 (forward-paragraph)
776 (insert-buffer-substring (current-buffer) start)
777 (insert ?\n)
778 (delete-region (1- start) (point-max)))
779 (goto-char (point-min))
780 (forward-paragraph)
781 (insert
782 "[Missing arglist. Please make a bug report.]\n")))
783 (goto-char (point-max))))
784 (help-setup-xref (list #'describe-function function)
785 interactive-p))
786 (princ "not documented")))))
787
788 (defun variable-at-point ()
789 "Return the bound variable symbol found around point.
790 Return 0 if there is no such symbol."
791 (condition-case ()
792 (with-syntax-table emacs-lisp-mode-syntax-table
793 (save-excursion
794 (or (not (zerop (skip-syntax-backward "_w")))
795 (eq (char-syntax (following-char)) ?w)
796 (eq (char-syntax (following-char)) ?_)
797 (forward-sexp -1))
798 (skip-chars-forward "'")
799 (let ((obj (read (current-buffer))))
800 (or (and (symbolp obj) (boundp obj) obj)
801 0))))
802 (error 0)))
803
804 (defun help-xref-on-pp (from to)
805 "Add xrefs for symbols in `pp's output between FROM and TO."
806 (let ((ost (syntax-table)))
807 (unwind-protect
808 (save-excursion
809 (save-restriction
810 (set-syntax-table emacs-lisp-mode-syntax-table)
811 (narrow-to-region from to)
812 (goto-char (point-min))
813 (while (not (eobp))
814 (cond
815 ((looking-at "\"") (forward-sexp 1))
816 ((looking-at "#<") (search-forward ">" nil 'move))
817 ((looking-at "\\(\\(\\sw\\|\\s_\\)+\\)")
818 (let* ((sym (intern-soft (match-string 1)))
819 (fn (cond ((fboundp sym) #'describe-function)
820 ((or (memq sym '(t nil))
821 (keywordp sym))
822 nil)
823 ((and sym (boundp sym))
824 #'describe-variable))))
825 (when fn (help-xref-button 1 fn sym)))
826 (goto-char (match-end 1)))
827 (t (forward-char 1))))))
828 (set-syntax-table ost))))
829
830 (defun describe-variable (variable &optional buffer)
831 "Display the full documentation of VARIABLE (a symbol).
832 Returns the documentation as a string, also.
833 If VARIABLE has a buffer-local value in BUFFER (default to the current buffer),
834 it is displayed along with the global value."
835 (interactive
836 (let ((v (variable-at-point))
837 (enable-recursive-minibuffers t)
838 val)
839 (setq val (completing-read (if (symbolp v)
840 (format
841 "Describe variable (default %s): " v)
842 "Describe variable: ")
843 obarray 'boundp t nil nil
844 (if (symbolp v) (symbol-name v))))
845 (list (if (equal val "")
846 v (intern val)))))
847 (unless (bufferp buffer) (setq buffer (current-buffer)))
848 (if (not (symbolp variable))
849 (message "You did not specify a variable")
850 (let (valvoid)
851 (with-current-buffer buffer
852 (with-output-to-temp-buffer "*Help*"
853 (prin1 variable)
854 (if (not (boundp variable))
855 (progn
856 (princ " is void")
857 (setq valvoid t))
858 (let ((val (symbol-value variable)))
859 (with-current-buffer standard-output
860 (princ "'s value is ")
861 (terpri)
862 (let ((from (point)))
863 (pp val)
864 (help-xref-on-pp from (point))
865 (if (< (point) (+ from 20))
866 (save-excursion
867 (goto-char from)
868 (delete-char -1)))))))
869 (terpri)
870 (if (local-variable-p variable)
871 (progn
872 (princ (format "Local in buffer %s; " (buffer-name)))
873 (if (not (default-boundp variable))
874 (princ "globally void")
875 (let ((val (default-value variable)))
876 (with-current-buffer standard-output
877 (princ "global value is ")
878 (terpri)
879 ;; Fixme: pp can take an age if you happen to
880 ;; ask for a very large expression. We should
881 ;; probably print it raw once and check it's a
882 ;; sensible size before prettyprinting. -- fx
883 (let ((from (point)))
884 (pp val)
885 (help-xref-on-pp from (point))
886 (if (< (point) (+ from 20))
887 (save-excursion
888 (goto-char from)
889 (delete-char -1)))))))
890 (terpri)))
891 (terpri)
892 (with-current-buffer standard-output
893 (if (> (count-lines (point-min) (point-max)) 10)
894 (progn
895 ;; Note that setting the syntax table like below
896 ;; makes forward-sexp move over a `'s' at the end
897 ;; of a symbol.
898 (set-syntax-table emacs-lisp-mode-syntax-table)
899 (goto-char (point-min))
900 (if valvoid
901 (forward-line 1)
902 (forward-sexp 1)
903 (delete-region (point) (progn (end-of-line) (point)))
904 (insert " value is shown below.\n\n")
905 (save-excursion
906 (insert "\n\nValue:"))))))
907 (princ "Documentation:")
908 (terpri)
909 (let ((doc (documentation-property variable 'variable-documentation)))
910 (princ (or doc "not documented as a variable.")))
911 (help-setup-xref (list #'describe-variable variable (current-buffer))
912 (interactive-p))
913
914 ;; Make a link to customize if this variable can be customized.
915 ;; Note, it is not reliable to test only for a custom-type property
916 ;; because those are only present after the var's definition
917 ;; has been loaded.
918 (if (or (get variable 'custom-type) ; after defcustom
919 (get variable 'custom-loads) ; from loaddefs.el
920 (get variable 'standard-value)) ; from cus-start.el
921 (let ((customize-label "customize"))
922 (terpri)
923 (terpri)
924 (princ (concat "You can " customize-label " this variable."))
925 (with-current-buffer "*Help*"
926 (save-excursion
927 (re-search-backward
928 (concat "\\(" customize-label "\\)") nil t)
929 (help-xref-button 1 (lambda (v)
930 (if help-xref-stack
931 (pop help-xref-stack))
932 (customize-variable v))
933 variable
934 "mouse-2, RET: customize variable")))))
935 ;; Make a hyperlink to the library if appropriate. (Don't
936 ;; change the format of the buffer's initial line in case
937 ;; anything expects the current format.)
938 (let ((file-name (symbol-file variable)))
939 (when file-name
940 (princ "\n\nDefined in `")
941 (princ file-name)
942 (princ "'.")
943 (with-current-buffer "*Help*"
944 (save-excursion
945 (re-search-backward "`\\([^`']+\\)'" nil t)
946 (help-xref-button
947 1 (lambda (arg)
948 (let ((location
949 (find-variable-noselect arg)))
950 (pop-to-buffer (car location))
951 (goto-char (cdr location))))
952 variable "mouse-2, RET: find variable's definition")))))
953
954 (print-help-return-message)
955 (save-excursion
956 (set-buffer standard-output)
957 ;; Return the text we displayed.
958 (buffer-string)))))))
959
960 (defun describe-bindings (&optional prefix buffer)
961 "Show a list of all defined keys, and their definitions.
962 We put that list in a buffer, and display the buffer.
963
964 The optional argument PREFIX, if non-nil, should be a key sequence;
965 then we display only bindings that start with that prefix.
966 The optional argument BUFFER specifies which buffer's bindings
967 to display (default, the current buffer)."
968 (interactive "P")
969 (or buffer (setq buffer (current-buffer)))
970 (with-current-buffer buffer
971 (describe-bindings-internal nil prefix))
972 (with-current-buffer "*Help*"
973 (help-setup-xref (list #'describe-bindings prefix buffer)
974 (interactive-p))))
975
976 (defun where-is (definition &optional insert)
977 "Print message listing key sequences that invoke the command DEFINITION.
978 Argument is a command definition, usually a symbol with a function definition.
979 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
980 (interactive
981 (let ((fn (function-called-at-point))
982 (enable-recursive-minibuffers t)
983 val)
984 (setq val (completing-read (if fn
985 (format "Where is command (default %s): " fn)
986 "Where is command: ")
987 obarray 'commandp t))
988 (list (if (equal val "")
989 fn (intern val))
990 current-prefix-arg)))
991 (let* ((keys (where-is-internal definition overriding-local-map nil nil))
992 (keys1 (mapconcat 'key-description keys ", "))
993 (standard-output (if insert (current-buffer) t)))
994 (if insert
995 (if (> (length keys1) 0)
996 (princ (format "%s (%s)" keys1 definition))
997 (princ (format "M-x %s RET" definition)))
998 (if (> (length keys1) 0)
999 (princ (format "%s is on %s" definition keys1))
1000 (princ (format "%s is not on any key" definition)))))
1001 nil)
1002
1003 (defun locate-library (library &optional nosuffix path interactive-call)
1004 "Show the precise file name of Emacs library LIBRARY.
1005 This command searches the directories in `load-path' like `M-x load-library'
1006 to find the file that `M-x load-library RET LIBRARY RET' would load.
1007 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
1008 to the specified name LIBRARY.
1009
1010 If the optional third arg PATH is specified, that list of directories
1011 is used instead of `load-path'.
1012
1013 When called from a program, the file name is normaly returned as a
1014 string. When run interactively, the argument INTERACTIVE-CALL is t,
1015 and the file name is displayed in the echo area."
1016 (interactive (list (read-string "Locate library: ")
1017 nil nil
1018 t))
1019 (let (result)
1020 (catch 'answer
1021 (mapc
1022 (lambda (dir)
1023 (mapc
1024 (lambda (suf)
1025 (let ((try (expand-file-name (concat library suf) dir)))
1026 (and (file-readable-p try)
1027 (null (file-directory-p try))
1028 (progn
1029 (setq result try)
1030 (throw 'answer try)))))
1031 (if nosuffix
1032 '("")
1033 '(".elc" ".el" "")
1034 (let ((basic '(".elc" ".el" ""))
1035 (compressed '(".Z" ".gz" "")))
1036 ;; If autocompression mode is on,
1037 ;; consider all combinations of library suffixes
1038 ;; and compression suffixes.
1039 (if (rassq 'jka-compr-handler file-name-handler-alist)
1040 (apply 'nconc
1041 (mapcar (lambda (compelt)
1042 (mapcar (lambda (baselt)
1043 (concat baselt compelt))
1044 basic))
1045 compressed))
1046 basic)))))
1047 (or path load-path)))
1048 (and interactive-call
1049 (if result
1050 (message "Library is file %s" result)
1051 (message "No library %s in search path" library)))
1052 result))
1053
1054 \f
1055 ;;; Grokking cross-reference information in doc strings and
1056 ;;; hyperlinking it.
1057
1058 ;; This may have some scope for extension and the same or something
1059 ;; similar should be done for widget doc strings, which currently use
1060 ;; another mechanism.
1061
1062 (defcustom help-highlight-p t
1063 "*If non-nil, `help-make-xrefs' highlight cross-references.
1064 Under a window system it highlights them with face defined by
1065 `help-highlight-face'."
1066 :group 'help
1067 :version "20.3"
1068 :type 'boolean)
1069
1070 (defcustom help-highlight-face 'underline
1071 "Face used by `help-make-xrefs' to highlight cross-references.
1072 Must be previously-defined."
1073 :group 'help
1074 :version "20.3"
1075 :type 'face)
1076
1077 (defvar help-back-label (purecopy "[back]")
1078 "Label to use by `help-make-xrefs' for the go-back reference.")
1079
1080 (defconst help-xref-symbol-regexp
1081 (purecopy (concat "\\(\\<\\(\\(variable\\|option\\)\\|"
1082 "\\(function\\|command\\)\\|"
1083 "\\(face\\)\\|"
1084 "\\(symbol\\)\\|"
1085 "\\(source \\(?:code \\)?\\(?:of\\|for\\)\\)\\)\\s-+\\)?"
1086 ;; Note starting with word-syntax character:
1087 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'"))
1088 "Regexp matching doc string references to symbols.
1089
1090 The words preceding the quoted symbol can be used in doc strings to
1091 distinguish references to variables, functions and symbols.")
1092
1093 (defconst help-xref-mule-regexp nil
1094 "Regexp matching doc string references to MULE-related keywords.
1095
1096 It is usually nil, and is temporarily bound to an appropriate regexp
1097 when help commands related to multilingual environment (e.g.,
1098 `describe-coding-system') are invoked.")
1099
1100
1101 (defconst help-xref-info-regexp
1102 (purecopy "\\<[Ii]nfo[ \t\n]+node[ \t\n]+`\\([^']+\\)'")
1103 "Regexp matching doc string references to an Info node.")
1104
1105 (defun help-setup-xref (item interactive-p)
1106 "Invoked from commands using the \"*Help*\" buffer to install some xref info.
1107
1108 ITEM is a (FUNCTION . ARGS) pair appropriate for recreating the help
1109 buffer after following a reference. INTERACTIVE-P is non-nil if the
1110 calling command was invoked interactively. In this case the stack of
1111 items for help buffer \"back\" buttons is cleared."
1112 (if interactive-p
1113 (setq help-xref-stack nil))
1114 (setq help-xref-stack-item item))
1115
1116 (defvar help-xref-following nil
1117 "Non-nil when following a help cross-reference.")
1118
1119 (defun help-make-xrefs (&optional buffer)
1120 "Parse and hyperlink documentation cross-references in the given BUFFER.
1121
1122 Find cross-reference information in a buffer and, if
1123 `help-highlight-p' is non-nil, highlight it with face defined by
1124 `help-highlight-face'; activate such cross references for selection
1125 with `help-follow'. Cross-references have the canonical form `...'
1126 and the type of reference may be disambiguated by the preceding
1127 word(s) used in `help-xref-symbol-regexp'.
1128
1129 If the variable `help-xref-mule-regexp' is non-nil, find also
1130 cross-reference information related to multilingual environment
1131 \(e.g., coding-systems). This variable is also used to disambiguate
1132 the type of reference as the same way as `help-xref-symbol-regexp'.
1133
1134 A special reference `back' is made to return back through a stack of
1135 help buffers. Variable `help-back-label' specifies the text for
1136 that."
1137 (interactive "b")
1138 (save-excursion
1139 (set-buffer (or buffer (current-buffer)))
1140 (goto-char (point-min))
1141 ;; Skip the header-type info, though it might be useful to parse
1142 ;; it at some stage (e.g. "function in `library'").
1143 (forward-paragraph)
1144 (let ((old-modified (buffer-modified-p)))
1145 (let ((stab (syntax-table))
1146 (case-fold-search t)
1147 (inhibit-read-only t))
1148 (set-syntax-table emacs-lisp-mode-syntax-table)
1149 ;; The following should probably be abstracted out.
1150 (unwind-protect
1151 (progn
1152 ;; Info references
1153 (save-excursion
1154 (while (re-search-forward help-xref-info-regexp nil t)
1155 (let ((data (match-string 1)))
1156 (save-match-data
1157 (unless (string-match "^([^)]+)" data)
1158 (setq data (concat "(emacs)" data))))
1159 (help-xref-button 1 #'info data
1160 "mouse-2, RET: read this Info node"))))
1161 ;; Mule related keywords. Do this before trying
1162 ;; `help-xref-symbol-regexp' because some of Mule
1163 ;; keywords have variable or function definitions.
1164 (if help-xref-mule-regexp
1165 (save-excursion
1166 (while (re-search-forward help-xref-mule-regexp nil t)
1167 (let* ((data (match-string 7))
1168 (sym (intern-soft data)))
1169 (cond
1170 ((match-string 3) ; coding system
1171 (and sym (coding-system-p sym)
1172 (help-xref-button
1173 7 #'describe-coding-system sym
1174 "mouse-2, RET: describe this coding system")))
1175 ((match-string 4) ; input method
1176 (and (assoc data input-method-alist)
1177 (help-xref-button
1178 7 #'describe-input-method data
1179 "mouse-2, RET: describe this input method")))
1180 ((or (match-string 5) (match-string 6)) ; charset
1181 (and sym (charsetp sym)
1182 (help-xref-button
1183 7 #'describe-character-set sym
1184 "mouse-2, RET: describe this character set")))
1185 ((assoc data input-method-alist)
1186 (help-xref-button
1187 7 #'describe-input-method data
1188 "mouse-2, RET: describe this input method"))
1189 ((and sym (coding-system-p sym))
1190 (help-xref-button
1191 7 #'describe-coding-system sym
1192 "mouse-2, RET: describe this coding system"))
1193 ((and sym (charsetp sym))
1194 (help-xref-button
1195 7 #'describe-character-set sym
1196 "mouse-2, RET: describe this character set")))))))
1197 ;; Quoted symbols
1198 (save-excursion
1199 (while (re-search-forward help-xref-symbol-regexp nil t)
1200 (let* ((data (match-string 8))
1201 (sym (intern-soft data)))
1202 (if sym
1203 (cond
1204 ((match-string 3) ; `variable' &c
1205 (and (boundp sym) ; `variable' doesn't ensure
1206 ; it's actually bound
1207 (help-xref-button
1208 8 #'describe-variable sym
1209 "mouse-2, RET: describe this variable")))
1210 ((match-string 4) ; `function' &c
1211 (and (fboundp sym) ; similarly
1212 (help-xref-button
1213 8 #'describe-function sym
1214 "mouse-2, RET: describe this function")))
1215 ((match-string 5) ; `face'
1216 (and (facep sym)
1217 (help-xref-button 8 #'describe-face sym
1218 "mouse-2, RET: describe this face")))
1219 ((match-string 6)) ; nothing for `symbol'
1220 ((match-string 7)
1221 (help-xref-button
1222 8
1223 #'(lambda (arg)
1224 (let ((location
1225 (find-function-noselect arg)))
1226 (pop-to-buffer (car location))
1227 (goto-char (cdr location))))
1228 sym
1229 "mouse-2, RET: find function's definition"))
1230 ((and (boundp sym) (fboundp sym))
1231 ;; We can't intuit whether to use the
1232 ;; variable or function doc -- supply both.
1233 (help-xref-button
1234 8 #'help-xref-interned sym
1235 "mouse-2, RET: describe this symbol"))
1236 ((boundp sym)
1237 (help-xref-button
1238 8 #'describe-variable sym
1239 "mouse-2, RET: describe this variable"))
1240 ((fboundp sym)
1241 (help-xref-button
1242 8 #'describe-function sym
1243 "mouse-2, RET: describe this function"))
1244 ((facep sym)
1245 (help-xref-button
1246 8 #'describe-face sym)))))))
1247 ;; An obvious case of a key substitution:
1248 (save-excursion
1249 (while (re-search-forward
1250 ;; Assume command name is only word characters
1251 ;; and dashes to get things like `use M-x foo.'.
1252 "\\<M-x\\s-+\\(\\sw\\(\\sw\\|-\\)+\\)" nil t)
1253 (let ((sym (intern-soft (match-string 1))))
1254 (if (fboundp sym)
1255 (help-xref-button
1256 1 #'describe-function sym
1257 "mouse-2, RET: describe this command")))))
1258 ;; Look for commands in whole keymap substitutions:
1259 (save-excursion
1260 ;; Make sure to find the first keymap.
1261 (goto-char (point-min))
1262 ;; Find a header and the column at which the command
1263 ;; name will be found.
1264 (while (re-search-forward "^key +binding\n\\(-+ +\\)-+\n\n"
1265 nil t)
1266 (let ((col (- (match-end 1) (match-beginning 1))))
1267 (while
1268 ;; Ignore single blank lines in table, but not
1269 ;; double ones, which should terminate it.
1270 (and (not (looking-at "\n\\s-*\n"))
1271 (progn
1272 (and (eolp) (forward-line))
1273 (end-of-line)
1274 (skip-chars-backward "^\t\n")
1275 (if (and (>= (current-column) col)
1276 (looking-at "\\(\\sw\\|-\\)+$"))
1277 (let ((sym (intern-soft (match-string 0))))
1278 (if (fboundp sym)
1279 (help-xref-button
1280 0 #'describe-function sym
1281 "mouse-2, RET: describe this function"))))
1282 (zerop (forward-line)))))))))
1283 (set-syntax-table stab))
1284 ;; Delete extraneous newlines at the end of the docstring
1285 (goto-char (point-max))
1286 (while (and (not (bobp)) (bolp))
1287 (delete-char -1))
1288 ;; Make a back-reference in this buffer if appropriate.
1289 (when (and help-xref-following help-xref-stack)
1290 (save-excursion
1291 (insert "\n\n" help-back-label))
1292 ;; Just to provide the match data:
1293 (looking-at (concat "\n\n\\(" (regexp-quote help-back-label) "\\)"))
1294 (help-xref-button 1 #'help-xref-go-back (current-buffer))))
1295 ;; View mode steals RET from us.
1296 (set (make-local-variable 'minor-mode-overriding-map-alist)
1297 (list (cons 'view-mode
1298 (let ((map (make-sparse-keymap)))
1299 (set-keymap-parent map view-mode-map)
1300 (define-key map "\r" 'help-follow)
1301 map))))
1302 (set-buffer-modified-p old-modified))))
1303
1304 (defun help-xref-button (match-number function data &optional help-echo)
1305 "Make a hyperlink for cross-reference text previously matched.
1306
1307 MATCH-NUMBER is the subexpression of interest in the last matched
1308 regexp. FUNCTION is a function to invoke when the button is
1309 activated, applied to DATA. DATA may be a single value or a list.
1310 See `help-make-xrefs'.
1311 If optional arg HELP-ECHO is supplied, it is used as a help string."
1312 ;; Don't mung properties we've added specially in some instances.
1313 (unless (get-text-property (match-beginning match-number) 'help-xref)
1314 (add-text-properties (match-beginning match-number)
1315 (match-end match-number)
1316 (list 'mouse-face 'highlight
1317 'help-xref (cons function
1318 (if (listp data)
1319 data
1320 (list data)))))
1321 (if help-echo
1322 (put-text-property (match-beginning match-number)
1323 (match-end match-number)
1324 'help-echo help-echo))
1325 (if help-highlight-p
1326 (put-text-property (match-beginning match-number)
1327 (match-end match-number)
1328 'face help-highlight-face))))
1329
1330 (defun help-insert-xref-button (string function data &optional help-echo)
1331 "Insert STRING and make a hyperlink from cross-reference text on it.
1332
1333 FUNCTION is a function to invoke when the button is activated, applied
1334 to DATA. DATA may be a single value or a list. See `help-make-xrefs'.
1335 If optional arg HELP-ECHO is supplied, it is used as a help string."
1336 (let ((pos (point)))
1337 (insert string)
1338 (goto-char pos)
1339 (search-forward string)
1340 (help-xref-button 0 function data help-echo)))
1341
1342
1343 \f
1344 ;; Additional functions for (re-)creating types of help buffers.
1345 (defun help-xref-interned (symbol)
1346 "Follow a hyperlink which appeared to be an arbitrary interned SYMBOL.
1347
1348 Both variable and function documentation are extracted into a single
1349 help buffer."
1350 (let ((fdoc (when (fboundp symbol) (describe-function symbol)))
1351 (facedoc (when (facep symbol) (describe-face symbol))))
1352 (when (or (boundp symbol) (not fdoc))
1353 (describe-variable symbol)
1354 ;; We now have a help buffer on the variable. Insert the function
1355 ;; text before it.
1356 (when (or fdoc facedoc)
1357 (with-current-buffer "*Help*"
1358 (goto-char (point-min))
1359 (let ((inhibit-read-only t))
1360 (when fdoc
1361 (insert fdoc "\n\n"))
1362 (when facedoc
1363 (insert (make-string 30 ?-) "\n\n" (symbol-name symbol)
1364 " is also a " "face." "\n\n" facedoc "\n\n"))
1365 (insert (make-string 30 ?-) "\n\n" (symbol-name symbol)
1366 " is also a " "variable." "\n\n"))
1367 (help-setup-xref (list #'help-xref-interned symbol) nil))))))
1368
1369 (defun help-xref-mode (buffer)
1370 "Do a `describe-mode' for the specified BUFFER."
1371 (save-excursion
1372 (set-buffer buffer)
1373 (describe-mode)))
1374 \f
1375 ;;; Navigation/hyperlinking with xrefs
1376
1377 (defun help-follow-mouse (click)
1378 "Follow the cross-reference that you click on."
1379 (interactive "e")
1380 (let* ((start (event-start click))
1381 (window (car start))
1382 (pos (car (cdr start))))
1383 (with-current-buffer (window-buffer window)
1384 (help-follow pos))))
1385
1386 (defun help-xref-go-back (buffer)
1387 "From BUFFER, go back to previous help buffer text using `help-xref-stack'."
1388 (let (item position method args)
1389 (with-current-buffer buffer
1390 (when help-xref-stack
1391 (setq help-xref-stack (cdr help-xref-stack)) ; due to help-follow
1392 (setq item (pop help-xref-stack)
1393 position (car item)
1394 method (cadr item)
1395 args (cddr item))))
1396 (apply method args)
1397 ;; We assume that the buffer we just recreated has the saved name,
1398 ;; which might not always be true.
1399 (when (get-buffer (cdr position))
1400 (with-current-buffer (cdr position)
1401 (goto-char (car position))))))
1402
1403 (defun help-go-back ()
1404 "Invoke the [back] button (if any) in the Help mode buffer."
1405 (interactive)
1406 (help-follow (1- (point-max))))
1407
1408 (defun help-follow (&optional pos)
1409 "Follow cross-reference at POS, defaulting to point.
1410
1411 For the cross-reference format, see `help-make-xrefs'."
1412 (interactive "d")
1413 (unless pos
1414 (setq pos (point)))
1415 (let* ((help-data
1416 (or (and (not (= pos (point-max)))
1417 (get-text-property pos 'help-xref))
1418 (and (not (= pos (point-min)))
1419 (get-text-property (1- pos) 'help-xref))
1420 ;; check if the symbol under point is a function or variable
1421 (let ((sym
1422 (intern
1423 (save-excursion
1424 (goto-char pos) (skip-syntax-backward "w_")
1425 (buffer-substring (point)
1426 (progn (skip-syntax-forward "w_")
1427 (point)))))))
1428 (when (or (boundp sym) (fboundp sym))
1429 (list #'help-xref-interned sym)))))
1430 (method (car help-data))
1431 (args (cdr help-data)))
1432 (when help-data
1433 (setq help-xref-stack (cons (cons (cons pos (buffer-name))
1434 help-xref-stack-item)
1435 help-xref-stack))
1436 (setq help-xref-stack-item nil)
1437 ;; There is a reference at point. Follow it.
1438 (let ((help-xref-following t))
1439 (apply method args)))))
1440
1441 ;; For tabbing through buffer.
1442 (defun help-next-ref ()
1443 "Find the next help cross-reference in the buffer."
1444 (interactive)
1445 (let (pos)
1446 (while (not pos)
1447 (if (get-text-property (point) 'help-xref) ; move off reference
1448 (goto-char (or (next-single-property-change (point) 'help-xref)
1449 (point))))
1450 (cond ((setq pos (next-single-property-change (point) 'help-xref))
1451 (if pos (goto-char pos)))
1452 ((bobp)
1453 (message "No cross references in the buffer.")
1454 (setq pos t))
1455 (t ; be circular
1456 (goto-char (point-min)))))))
1457
1458 (defun help-previous-ref ()
1459 "Find the previous help cross-reference in the buffer."
1460 (interactive)
1461 (let (pos)
1462 (while (not pos)
1463 (if (get-text-property (point) 'help-xref) ; move off reference
1464 (goto-char (or (previous-single-property-change (point) 'help-xref)
1465 (point))))
1466 (cond ((setq pos (previous-single-property-change (point) 'help-xref))
1467 (if pos (goto-char pos)))
1468 ((bobp)
1469 (message "No cross references in the buffer.")
1470 (setq pos t))
1471 (t ; be circular
1472 (goto-char (point-max)))))))
1473
1474 \f
1475 ;;; Automatic resizing of temporary buffers.
1476
1477 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
1478 "*Maximum height of a window displaying a temporary buffer.
1479 This is the maximum height (in text lines) which `resize-temp-buffer-window'
1480 will give to a window displaying a temporary buffer.
1481 It can also be a function which will be called with the object corresponding
1482 to the buffer to be displayed as argument and should return an integer
1483 positive number."
1484 :type '(choice integer function)
1485 :group 'help
1486 :version "20.4")
1487
1488 (define-minor-mode temp-buffer-resize-mode
1489 "Toggle the mode which makes windows smaller for temporary buffers.
1490 With prefix argument ARG, turn the resizing of windows displaying temporary
1491 buffers on if ARG is positive or off otherwise.
1492 This makes the window the right height for its contents, but never
1493 more than `temp-buffer-max-height' nor less than `window-min-height'.
1494 This applies to `help', `apropos' and `completion' buffers, and some others."
1495 nil nil nil :global t :group 'help
1496 (if temp-buffer-resize-mode
1497 ;; `help-make-xrefs' may add a `back' button and thus increase the
1498 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
1499 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
1500 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
1501
1502 (defun resize-temp-buffer-window ()
1503 "Resize the current window to fit its contents.
1504 Will not make it higher than `temp-buffer-max-height' nor smaller than
1505 `window-min-height'. Do nothing if it is the only window on its frame, if it
1506 is not as wide as the frame or if some of the window's contents are scrolled
1507 out of view."
1508 (unless (or (one-window-p 'nomini)
1509 (not (pos-visible-in-window-p (point-min)))
1510 (/= (frame-width) (window-width)))
1511 (fit-window-to-buffer
1512 (selected-window)
1513 (if (functionp temp-buffer-max-height)
1514 (funcall temp-buffer-max-height (current-buffer))
1515 temp-buffer-max-height))))
1516
1517 ;; `help-manyarg-func-alist' is defined primitively (in doc.c).
1518 ;; New primitives with `MANY' or `UNEVALLED' arglists should be added
1519 ;; to this alist.
1520 ;; The parens and function name are redundant, but it's messy to add
1521 ;; them in `documentation'.
1522
1523 ;; This will find any missing items:
1524 ;; (let (l)
1525 ;; (mapatoms (lambda (x)
1526 ;; (if (and (fboundp x)
1527 ;; (subrp (symbol-function x))
1528 ;; (not (numberp (cdr (subr-arity (symbol-function x)))))
1529 ;; (not (assq x help-manyarg-func-alist)))
1530 ;; (push x l))))
1531 ;; l)
1532 (defconst help-manyarg-func-alist
1533 (purecopy
1534 '((list . "(list &rest OBJECTS)")
1535 (vector . "(vector &rest OBJECTS)")
1536 (make-byte-code . "(make-byte-code &rest ELEMENTS)")
1537 (call-process
1538 . "(call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)")
1539 (call-process-region
1540 . "(call-process-region START END PROGRAM &optional DELETE BUFFER DISPLAY &rest ARGS)")
1541 (string . "(string &rest CHARACTERS)")
1542 (+ . "(+ &rest NUMBERS-OR-MARKERS)")
1543 (- . "(- &optional NUMBER-OR-MARKER &rest MORE-NUMBERS-OR-MARKERS)")
1544 (* . "(* &rest NUMBERS-OR-MARKERS)")
1545 (/ . "(/ DIVIDEND DIVISOR &rest DIVISORS)")
1546 (max . "(max NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS)")
1547 (min . "(min NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS)")
1548 (logand . "(logand &rest INTS-OR-MARKERS)")
1549 (logior . "(logior &rest INTS-OR-MARKERS)")
1550 (logxor . "(logxor &rest INTS-OR-MARKERS)")
1551 (encode-time
1552 . "(encode-time SECOND MINUTE HOUR DAY MONTH YEAR &optional ZONE)")
1553 (insert . "(insert &rest ARGS)")
1554 (insert-and-inherit . "(insert-and-inherit &rest ARGS)")
1555 (insert-before-markers . "(insert-before-markers &rest ARGS)")
1556 (message . "(message STRING &rest ARGUMENTS)")
1557 (message-box . "(message-box STRING &rest ARGUMENTS)")
1558 (message-or-box . "(message-or-box STRING &rest ARGUMENTS)")
1559 (propertize . "(propertize STRING &rest PROPERTIES)")
1560 (format . "(format STRING &rest OBJECTS)")
1561 (apply . "(apply FUNCTION &rest ARGUMENTS)")
1562 (run-hooks . "(run-hooks &rest HOOKS)")
1563 (run-hook-with-args . "(run-hook-with-args HOOK &rest ARGS)")
1564 (run-hook-with-args-until-failure
1565 . "(run-hook-with-args-until-failure HOOK &rest ARGS)")
1566 (run-hook-with-args-until-success
1567 . "(run-hook-with-args-until-success HOOK &rest ARGS)")
1568 (funcall . "(funcall FUNCTION &rest ARGUMENTS)")
1569 (append . "(append &rest SEQUENCES)")
1570 (concat . "(concat &rest SEQUENCES)")
1571 (vconcat . "(vconcat &rest SEQUENCES)")
1572 (nconc . "(nconc &rest LISTS)")
1573 (widget-apply . "(widget-apply WIDGET PROPERTY &rest ARGS)")
1574 (make-hash-table . "(make-hash-table &rest KEYWORD-ARGS)")
1575 (insert-string . "(insert-string &rest ARGS)")
1576 (start-process . "(start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS)")
1577 (setq-default . "(setq-default SYMBOL VALUE [SYMBOL VALUE...])")
1578 (save-excursion . "(save-excursion &rest BODY)")
1579 (save-current-buffer . "(save-current-buffer &rest BODY)")
1580 (save-restriction . "(save-restriction &rest BODY)")
1581 (or . "(or CONDITIONS ...)")
1582 (and . "(and CONDITIONS ...)")
1583 (if . "(if COND THEN ELSE...)")
1584 (cond . "(cond CLAUSES...)")
1585 (progn . "(progn BODY ...)")
1586 (prog1 . "(prog1 FIRST BODY...)")
1587 (prog2 . "(prog2 X Y BODY...)")
1588 (setq . "(setq SYM VAL SYM VAL ...)")
1589 (quote . "(quote ARG)")
1590 (function . "(function ARG)")
1591 (defun . "(defun NAME ARGLIST [DOCSTRING] BODY...)")
1592 (defmacro . "(defmacro NAME ARGLIST [DOCSTRING] BODY...)")
1593 (defvar . "(defvar SYMBOL [INITVALUE DOCSTRING])")
1594 (defconst . "(defconst SYMBOL INITVALUE [DOCSTRING])")
1595 (let* . "(let* VARLIST BODY...)")
1596 (let . "(let VARLIST BODY...)")
1597 (while . "(while TEST BODY...)")
1598 (catch . "(catch TAG BODY...)")
1599 (unwind-protect . "(unwind-protect BODYFORM UNWINDFORMS...)")
1600 (condition-case . "(condition-case VAR BODYFORM HANDLERS...)")
1601 (track-mouse . "(track-mouse BODY ...)")
1602 (ml-if . "(ml-if COND THEN ELSE...)")
1603 (ml-provide-prefix-argument . "(ml-provide-prefix-argument ARG1 ARG2)")
1604 (ml-prefix-argument-loop . "(ml-prefix-argument-loop ...)")
1605 (with-output-to-temp-buffer
1606 . "(with-output-to-temp-buffer BUFFNAME BODY ...)")
1607 (save-window-excursion . "(save-window-excursion BODY ...)")
1608 (find-operation-coding-system
1609 . "(find-operation-coding-system OPERATION ARGUMENTS ...)")
1610 (insert-before-markers-and-inherit
1611 . "(insert-before-markers-and-inherit &rest ARGS)"))))
1612
1613 ;; Provide this for the sake of define-minor-mode which generates
1614 ;; defcustoms which require 'help'.
1615
1616 (provide 'help)
1617
1618 ;;; help.el ends here