1 ;;; help.el --- help commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998 Free Software Foundation, Inc.
6 ;; Keywords: help, internal
8 ;; This file is part of GNU Emacs.
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)
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.
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.
27 ;; This code implements GNU Emacs' on-line help system, the one invoked by
28 ;;`M-x help-for-help'.
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
))
36 (defvar help-map
(make-sparse-keymap)
37 "Keymap for characters following the Help key.")
39 (defvar help-mode-map
(make-sparse-keymap)
40 "Keymap for help mode.")
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)
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)
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)
58 (define-key help-map "b" 'describe-bindings)
60 (define-key help-map "c" 'describe-key-briefly)
61 (define-key help-map "k" 'describe-key)
63 (define-key help-map "d" 'describe-function)
64 (define-key help-map "f" 'describe-function)
66 (define-key help-map "F" 'view-emacs-FAQ)
68 (define-key help-map "i" 'info)
69 (define-key help-map "\C-f" 'Info-goto-emacs-command-node)
70 (define-key help-map "\C-k" 'Info-goto-emacs-key-command-node)
71 (define-key help-map "\C-i" 'info-lookup-symbol)
73 (define-key help-map "l" 'view-lossage)
75 (define-key help-map "m" 'describe-mode)
77 (define-key help-map "\C-n" 'view-emacs-news)
78 (define-key help-map "n" 'view-emacs-news)
80 (define-key help-map "p" 'finder-by-keyword)
81 (autoload 'finder-by-keyword "finder"
82 "Find packages matching a given keyword." t)
84 (define-key help-map "s" 'describe-syntax)
86 (define-key help-map "t" 'help-with-tutorial)
88 (define-key help-map "w" 'where-is)
90 (define-key help-map "v" 'describe-variable)
92 (define-key help-map "q" 'help-quit)
94 (define-key help-mode-map [mouse-2] 'help-follow-mouse)
95 (define-key help-mode-map "\C-c\C-b" 'help-go-back)
96 (define-key help-mode-map "\C-c\C-c" 'help-follow)
97 (define-key help-mode-map "\t" 'help-next-ref)
98 (define-key help-mode-map [backtab] 'help-previous-ref)
99 ;; Documentation only, since we use minor-mode-overriding-map-alist.
100 (define-key help-mode-map "\r" 'help-follow)
102 ;; Font-locking is incompatible with the new xref stuff.
103 ;(defvar help-font-lock-keywords
105 ; (let ((name-char "[-+a-zA-Z0-9_*]") (sym-char "[-+a-zA-Z0-9_:*]"))
108 ; ;; The symbol itself.
109 ; (list (concat "\\`\\(" name-char "+\\)\\(\\(:\\)\\|\\('\\)\\)")
110 ; '(1 (if (match-beginning 3)
111 ; font-lock-function-name-face
112 ; font-lock-variable-name-face)))
114 ; ;; Words inside `' which tend to be symbol names.
115 ; (list (concat "`\\(" sym-char sym-char "+\\)'")
116 ; 1 'font-lock-constant-face t)
118 ; ;; CLisp `:' keywords as references.
119 ; (list (concat "\\<:" sym-char "+\\>") 0 'font-lock-builtin-face t))))
120 ; "Default expressions to highlight in Help mode.")
122 (defvar help-xref-stack nil
123 "A stack of ways by which to return to help buffers after following xrefs.
124 Used by `help-follow' and `help-xref-go-back'.")
125 (put 'help-xref-stack 'permanent-local t)
127 (defvar help-xref-stack-item nil
128 "An item for `help-follow' in this buffer to push onto `help-xref-stack'.")
129 (put 'help-xref-stack-item 'permanent-local t)
131 (setq-default help-xref-stack nil help-xref-stack-item nil)
134 "Major mode for viewing help text and navigating references in it.
135 Entry to this mode runs the normal hook `help-mode-hook'.
139 (kill-all-local-variables)
140 (use-local-map help-mode-map)
141 (setq mode-name "Help")
142 (setq major-mode 'help-mode)
143 (make-local-variable 'font-lock-defaults)
144 (setq font-lock-defaults nil) ; font-lock would defeat xref
146 (make-local-variable 'view-no-disable-on-exit)
147 (setq view-no-disable-on-exit t)
148 ;; `help-make-xrefs' would be run here if not invoked from
149 ;; `help-mode-maybe'.
150 (run-hooks 'help-mode-hook))
152 (defun help-mode-maybe ()
153 (if (eq major-mode 'fundamental-mode)
155 (when (eq major-mode 'help-mode)
156 ;; View mode's read-only status of existing *Help* buffer is lost
157 ;; by with-output-to-temp-buffer.
159 (help-make-xrefs (current-buffer)))
160 (setq view-return-to-alist
161 (list (cons (selected-window) help-return-method))))
163 (add-hook 'temp-buffer-show-hook 'help-mode-maybe)
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."
177 (read-language-name 'tutorial "Language: " "English")
178 (if (get-language-info current-language-environment 'tutorial)
179 current-language-environment
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<<")
195 (delete-region (point) (progn (end-of-line) (point)))
196 (let ((n (- (window-height (selected-window))
197 (count-lines (point-min) (point))
201 ;; Some people get confused by the large gap.
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))))
209 (defun describe-key-briefly (key &optional insert)
210 "Print the name of the function KEY invokes. KEY is a string.
211 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
212 (interactive "kDescribe key briefly: \nP")
213 ;; If this key seq ends with a down event, discard the
214 ;; following click or drag event. Otherwise that would
215 ;; erase the message.
216 (let ((type (aref key (1- (length key)))))
217 (if (listp type) (setq type (car type)))
219 (memq 'down (event-modifiers type))
222 (let ((modifiers (event-modifiers (aref key 0)))
223 (standard-output (if insert (current-buffer) t))
225 ;; For a mouse button event, go to the button it applies to
226 ;; to get the right key bindings. And go to the right place
227 ;; in case the keymap depends on where you clicked.
228 (if (or (memq 'click modifiers) (memq 'down modifiers)
229 (memq 'drag modifiers))
230 (setq window (posn-window (event-start (aref key 0)))
231 position (posn-point (event-start (aref key 0)))))
234 (set-buffer (window-buffer window))
235 (goto-char position)))
236 ;; Ok, now look up the key and name the command.
237 (let ((defn (key-binding key))
238 (key-desc (key-description key)))
239 (if (or (null defn) (integerp defn))
240 (princ (format "%s is undefined" key-desc))
241 (princ (format (if insert
244 "%s at that spot runs the command %s"
245 "%s runs the command %s"))
247 (if (symbolp defn) defn (prin1-to-string defn)))))))))
249 (defvar help-return-method nil
250 "What to do to \"exit\" the help buffer.
252 (WINDOW . t) delete the selected window, go to WINDOW.
253 (WINDOW . quit-window) do quit-window, then select WINDOW.
254 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
256 (defun print-help-return-message (&optional function)
257 "Display or return message saying how to restore windows after help command.
258 Computes a message and applies the optional argument FUNCTION to it.
259 If FUNCTION is nil, applies `message' to it, thus printing it."
260 (and (not (get-buffer-window standard-output))
262 (cond ((special-display-p (buffer-name standard-output))
263 (setq help-return-method (cons (selected-window) t))
264 ;; If the help output buffer is a special display buffer,
265 ;; don't say anything about how to get rid of it.
266 ;; First of all, the user will do that with the window
267 ;; manager, not with Emacs.
268 ;; Secondly, the buffer has not been displayed yet,
269 ;; so we don't know whether its frame will be selected.
271 ((not (one-window-p t))
272 (setq help-return-method
273 (cons (selected-window) 'quit-window))
274 "Type \\[switch-to-buffer-other-window] RET to restore the other window.")
276 (setq help-return-method (cons (selected-window) t))
277 "Type \\[delete-other-windows] to remove help window.")
279 (setq help-return-method
280 (list (selected-window) (window-buffer)
281 (window-start) (window-point)))
282 "Type \\[switch-to-buffer] RET to remove help window."))))
283 (funcall (or function 'message)
286 (substitute-command-keys first-message)
288 (if first-message " " "")
289 ;; If the help buffer will go in a separate frame,
290 ;; it's no use mentioning a command to scroll, so don't.
291 (if (special-display-p (buffer-name standard-output))
293 (if (same-window-p (buffer-name standard-output))
294 ;; Say how to scroll this window.
295 (substitute-command-keys
296 "\\[scroll-up] to scroll the help.")
297 ;; Say how to scroll some other window.
298 (substitute-command-keys
299 "\\[scroll-other-window] to scroll the help."))))))))
301 (defun describe-key (key)
302 "Display documentation of the function invoked by KEY. KEY is a string."
303 (interactive "kDescribe key: ")
304 ;; If this key seq ends with a down event, discard the
305 ;; following click or drag event. Otherwise that would
306 ;; erase the message.
307 (let ((type (aref key (1- (length key)))))
308 (if (listp type) (setq type (car type)))
310 (memq 'down (event-modifiers type))
313 (let ((modifiers (event-modifiers (aref key 0)))
315 ;; For a mouse button event, go to the button it applies to
316 ;; to get the right key bindings. And go to the right place
317 ;; in case the keymap depends on where you clicked.
318 (if (or (memq 'click modifiers) (memq 'down modifiers)
319 (memq 'drag modifiers))
320 (setq window (posn-window (event-start (aref key 0)))
321 position (posn-point (event-start (aref key 0)))))
324 (set-buffer (window-buffer window))
325 (goto-char position)))
326 (let ((defn (key-binding key)))
327 (if (or (null defn) (integerp defn))
328 (message "%s is undefined" (key-description key))
329 (with-output-to-temp-buffer "*Help*"
330 (princ (key-description key))
332 (princ " at that spot"))
333 (princ " runs the command ")
335 (princ "\n which is ")
336 (describe-function-1 defn nil)
337 (print-help-return-message)))))))
339 (defun describe-mode ()
340 "Display documentation of current major mode and minor modes.
341 For this to work correctly for a minor mode, the mode's indicator variable
342 \(listed in `minor-mode-alist') must also be a function whose documentation
343 describes the minor mode."
345 (with-output-to-temp-buffer "*Help*"
346 (let ((minor-modes minor-mode-alist)
349 (let* ((minor-mode (car (car minor-modes)))
350 (indicator (car (cdr (car minor-modes)))))
351 ;; Document a minor mode if it is listed in minor-mode-alist,
352 ;; bound locally in this buffer, non-nil, and has a function
354 (if (and (symbol-value minor-mode)
355 (fboundp minor-mode))
356 (let ((pretty-minor-mode minor-mode))
357 (if (string-match "-mode$" (symbol-name minor-mode))
358 (setq pretty-minor-mode
360 (substring (symbol-name minor-mode)
361 0 (match-beginning 0)))))
362 (while (and indicator (symbolp indicator))
363 (setq indicator (symbol-value indicator)))
365 (princ "The minor modes are described first,
366 followed by the major mode, which is described on the last page.\n\f\n"))
368 (princ (format "%s minor mode (%s):\n"
371 (format "indicator%s" indicator)
373 (princ (documentation minor-mode))
375 (setq minor-modes (cdr minor-modes))))
378 (princ (documentation major-mode))
379 (help-setup-xref (cons #'help-xref-mode (current-buffer)) (interactive-p))
380 (print-help-return-message)))
382 ;; So keyboard macro definitions are documented correctly
383 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
385 (defun describe-distribution ()
386 "Display info on how to obtain the latest version of GNU Emacs."
389 (expand-file-name "DISTRIB" data-directory)))
391 (defun describe-copying ()
392 "Display info on how you may redistribute copies of GNU Emacs."
395 (expand-file-name "COPYING" data-directory))
396 (goto-char (point-min)))
398 (defun describe-project ()
399 "Display info on the GNU project."
402 (expand-file-name "GNU" data-directory))
403 (goto-char (point-min)))
405 (defun describe-no-warranty ()
406 "Display info on all the kinds of warranty Emacs does NOT have."
409 (let (case-fold-search)
410 (search-forward "NO WARRANTY")
413 (defun describe-prefix-bindings ()
414 "Describe the bindings of the prefix used to reach this command.
415 The prefix described consists of all but the last event
416 of the key sequence that ran this command."
418 (let* ((key (this-command-keys)))
421 (substring key 0 (1- (length key)))
422 (let ((prefix (make-vector (1- (length key)) nil))
424 (while (< i (length prefix))
425 (aset prefix i (aref key i))
428 ;; Make C-h after a prefix, when not specifically bound,
429 ;; run describe-prefix-bindings.
430 (setq prefix-help-command 'describe-prefix-bindings)
432 (defun view-emacs-news (&optional arg)
433 "Display info on recent changes to Emacs.
434 With numeric argument display information on correspondingly older changes."
436 (let* ((arg (if arg (prefix-numeric-value arg) 0)))
438 (expand-file-name (concat (make-string arg ?O) "NEWS")
441 (defun view-emacs-FAQ ()
442 "Display the Emacs Frequently Asked Questions (FAQ) file."
444 (find-file-read-only (expand-file-name "FAQ" data-directory)))
446 (defun view-lossage ()
447 "Display last 100 input keystrokes."
449 (with-output-to-temp-buffer "*Help*"
450 (princ (mapconcat (function (lambda (key)
451 (if (or (integerp key)
454 (single-key-description key)
455 (prin1-to-string key nil))))
459 (set-buffer standard-output)
460 (goto-char (point-min))
461 (while (progn (move-to-column 50) (not (eobp)))
462 (search-forward " " nil t)
464 (setq help-xref-stack nil
465 help-xref-stack-item nil)
466 (print-help-return-message)))
468 (defalias 'help 'help-for-help)
469 (make-help-screen help-for-help
470 "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:"
471 "You have typed \\[help-command], the help character. Type a Help option:
472 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
474 a command-apropos. Give a substring, and see a list of commands
475 (functions interactively callable) that contain
476 that substring. See also the apropos command.
477 b describe-bindings. Display table of all key bindings.
478 c describe-key-briefly. Type a command key sequence;
479 it prints the function name that sequence runs.
480 C describe-coding-system. This describes either a specific coding system
481 (if you type its name) or the coding systems currently in use
482 (if you type just RET).
483 f describe-function. Type a function name and get documentation of it.
484 C-f Info-goto-emacs-command-node. Type a function name;
485 it takes you to the Info node for that command.
486 i info. The info documentation reader.
487 I describe-input-method. Describe a specific input method (if you type
488 its name) or the current input method (if you type just RET).
489 k describe-key. Type a command key sequence;
490 it displays the full documentation.
491 C-k Info-goto-emacs-key-command-node. Type a command key sequence;
492 it takes you to the Info node for the command bound to that key.
493 l view-lossage. Shows last 100 characters you typed.
494 L describe-language-environment. This describes either the a
495 specific language environment (if you type its name)
496 or the current language environment (if you type just RET).
497 m describe-mode. Print documentation of current minor modes,
498 and the current major mode, including their special commands.
499 n view-emacs-news. Shows emacs news file.
500 p finder-by-keyword. Find packages matching a given topic keyword.
501 s describe-syntax. Display contents of syntax table, plus explanations
502 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
503 v describe-variable. Type name of a variable;
504 it displays the variable's documentation and value.
505 w where-is. Type command name; it prints which keystrokes
508 F Display the frequently asked questions file.
509 h Display the HELLO file which illustrates various scripts.
510 C-c Display Emacs copying permission (General Public License).
511 C-d Display Emacs ordering information.
512 C-n Display news of recent Emacs changes.
513 C-p Display information about the GNU project.
514 C-w Display information on absence of warranty for GNU Emacs."
517 ;; Return a function which is called by the list containing point.
518 ;; If that gives no function, return a function whose name is around point.
519 ;; If that doesn't give a function, return nil.
520 (defun function-called-at-point ()
521 (let ((stab (syntax-table)))
522 (set-syntax-table emacs-lisp-mode-syntax-table)
524 (or (condition-case ()
527 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
528 ;; Move up to surrounding paren, then after the open.
531 ;; If there is space here, this is probably something
532 ;; other than a real Lisp function call, so ignore it.
533 (if (looking-at "[ \t]")
534 (error "Probably not a Lisp function call"))
536 (setq obj (read (current-buffer)))
537 (and (symbolp obj) (fboundp obj) obj))))
541 (or (not (zerop (skip-syntax-backward "_w")))
542 (eq (char-syntax (following-char)) ?w)
543 (eq (char-syntax (following-char)) ?_)
545 (skip-chars-forward "'")
546 (let ((obj (read (current-buffer))))
547 (and (symbolp obj) (fboundp obj) obj)))
549 (set-syntax-table stab))))
551 (defun describe-function-find-file (function)
552 (let ((files load-history)
555 (if (memq function (cdr (car files)))
556 (setq file (car (car files)) files nil))
557 (setq files (cdr files)))
560 (defun describe-function (function)
561 "Display the full documentation of FUNCTION (a symbol)."
563 (let ((fn (function-called-at-point))
564 (enable-recursive-minibuffers t)
566 (setq val (completing-read (if fn
567 (format "Describe function (default %s): " fn)
568 "Describe function: ")
569 obarray 'fboundp t nil nil (symbol-name fn)))
570 (list (if (equal val "")
573 (with-output-to-temp-buffer "*Help*"
575 ;; Use " is " instead of a colon so that
576 ;; it is easier to get out the function name using forward-sexp.
578 (describe-function-1 function nil)
579 (print-help-return-message)
581 (set-buffer standard-output)
582 ;; Return the text we displayed.
584 (message "You didn't specify a function")))
586 (defun describe-function-1 (function parens)
587 (let* ((def (symbol-function function))
588 file-name string need-close
589 (beg (if (commandp def) "an interactive " "a ")))
591 (cond ((or (stringp def)
595 (concat beg "built-in function"))
596 ((byte-code-function-p def)
597 (concat beg "compiled Lisp function"))
599 (format "alias for `%s'" def))
600 ((eq (car-safe def) 'lambda)
601 (concat beg "Lisp function"))
602 ((eq (car-safe def) 'macro)
604 ((eq (car-safe def) 'mocklisp)
605 "a mocklisp function")
606 ((eq (car-safe def) 'autoload)
607 (setq file-name (nth 1 def))
608 (format "%s autoloaded Lisp %s"
609 (if (commandp def) "an interactive" "an")
610 (if (nth 4 def) "macro" "function")
613 (when (and parens (not (equal string "")))
618 (setq file-name (describe-function-find-file function)))
622 ;; We used to add .el to the file name,
623 ;; but that's completely wrong when the user used load-file.
626 (if need-close (princ ")"))
629 (let* ((inner-function (if (and (listp def) 'macro)
632 (arglist (cond ((byte-code-function-p inner-function)
633 (car (append inner-function nil)))
634 ((eq (car-safe inner-function) 'lambda)
635 (nth 1 inner-function))
639 (princ (cons function
640 (mapcar (lambda (arg)
641 (if (memq arg '(&optional &rest))
643 (intern (upcase (symbol-name arg)))))
646 (let ((doc (documentation function)))
650 (help-setup-xref (cons #'describe-function function) (interactive-p)))
651 (princ "not documented")))))
653 ;; We return 0 if we can't find a variable to return.
654 (defun variable-at-point ()
656 (let ((stab (syntax-table)))
659 (set-syntax-table emacs-lisp-mode-syntax-table)
660 (or (not (zerop (skip-syntax-backward "_w")))
661 (eq (char-syntax (following-char)) ?w)
662 (eq (char-syntax (following-char)) ?_)
664 (skip-chars-forward "'")
665 (let ((obj (read (current-buffer))))
666 (or (and (symbolp obj) (boundp obj) obj)
668 (set-syntax-table stab)))
671 (defun describe-variable (variable)
672 "Display the full documentation of VARIABLE (a symbol).
673 Returns the documentation as a string, also."
675 (let ((v (variable-at-point))
676 (enable-recursive-minibuffers t)
678 (setq val (completing-read (if (symbolp v)
679 (format "Describe variable (default %s): " v)
680 "Describe variable: ")
681 obarray 'boundp t nil nil
682 (if (symbolp v) (symbol-name v))))
683 (list (if (equal val "")
685 (if (symbolp variable)
687 (with-output-to-temp-buffer "*Help*"
689 (if (not (boundp variable))
694 (princ "'s value is ")
696 (pp (symbol-value variable))
698 (if (local-variable-p variable)
700 (princ (format "Local in buffer %s; " (buffer-name)))
701 (if (not (default-boundp variable))
702 (princ "globally void")
703 (princ "global value is ")
705 (pp (default-value variable)))
709 (set-buffer standard-output)
710 (if (> (count-lines (point-min) (point-max)) 10)
712 (goto-char (point-min))
716 (delete-region (point) (progn (end-of-line) (point)))
717 (insert "'s value is shown below.\n\n")
719 (insert "\n\nValue:"))))))
720 (princ "Documentation:")
722 (let ((doc (documentation-property variable 'variable-documentation)))
723 (princ (or doc "not documented as a variable.")))
724 (help-setup-xref (cons #'describe-variable variable) (interactive-p))
725 (print-help-return-message)
727 (set-buffer standard-output)
728 ;; Return the text we displayed.
730 (message "You did not specify a variable")))
732 (defun describe-bindings (&optional prefix)
733 "Show a list of all defined keys, and their definitions.
734 We put that list in a buffer, and display the buffer.
736 The optional argument PREFIX, if non-nil, should be a key sequence;
737 then we display only bindings that start with that prefix."
739 (setq help-xref-stack nil
740 help-xref-stack-item nil)
741 (describe-bindings-internal nil prefix))
743 (defun where-is (definition &optional insert)
744 "Print message listing key sequences that invoke specified command.
745 Argument is a command definition, usually a symbol with a function definition.
746 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
748 (let ((fn (function-called-at-point))
749 (enable-recursive-minibuffers t)
751 (setq val (completing-read (if fn
752 (format "Where is command (default %s): " fn)
753 "Where is command: ")
755 (list (if (equal val "")
757 current-prefix-arg)))
758 (let* ((keys (where-is-internal definition overriding-local-map nil nil))
759 (keys1 (mapconcat 'key-description keys ", "))
760 (standard-output (if insert (current-buffer) t)))
762 (if (> (length keys1) 0)
763 (princ (format "%s (%s)" keys1 definition))
764 (princ (format "M-x %s RET" definition)))
765 (if (> (length keys1) 0)
766 (princ (format "%s is on %s" definition keys1))
767 (princ (format "%s is not on any key" definition)))))
770 (defun locate-library (library &optional nosuffix path interactive-call)
771 "Show the precise file name of Emacs library LIBRARY.
772 This command searches the directories in `load-path' like `M-x load-library'
773 to find the file that `M-x load-library RET LIBRARY RET' would load.
774 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
775 to the specified name LIBRARY.
777 If the optional third arg PATH is specified, that list of directories
778 is used instead of `load-path'."
779 (interactive (list (read-string "Locate library: ")
788 (let ((try (expand-file-name (concat library suf) dir)))
789 (and (file-readable-p try)
790 (null (file-directory-p try))
793 (throw 'answer try)))))
797 ;;; load doesn't handle this yet.
798 ;;; (let ((basic '(".elc" ".el" ""))
799 ;;; (compressed '(".Z" ".gz" "")))
800 ;;; ;; If autocompression mode is on,
801 ;;; ;; consider all combinations of library suffixes
802 ;;; ;; and compression suffixes.
803 ;;; (if (rassq 'jka-compr-handler file-name-handler-alist)
805 ;;; (mapcar (lambda (compelt)
806 ;;; (mapcar (lambda (baselt)
807 ;;; (concat baselt compelt))
812 (or path load-path)))
813 (and interactive-call
815 (message "Library is file %s" result)
816 (message "No library %s in search path" library)))
820 ;;; Grokking cross-reference information in doc strings and
823 ;; This may have some scope for extension and the same or something
824 ;; similar should be done for widget doc strings, which currently use
825 ;; another mechanism.
827 (defcustom help-highlight-p t
828 "*If non-nil, `help-make-xrefs' highlight cross-references.
829 Under a window system it highlights them with face defined by
830 `help-highlight-face'. On a character terminal highlighted
831 references look like cross-references in info mode."
836 (defcustom help-highlight-face 'underline
837 "Face used by `help-make-xrefs' to highlight cross-references.
838 Must be previously-defined."
843 (defvar help-back-label "[back]"
844 "Label to use by `help-make-xrefs' for the go-back reference.")
846 (defvar help-xref-symbol-regexp
847 (concat "\\(\\<\\(\\(variable\\|option\\)\\|"
848 "\\(function\\|command\\)\\|"
849 "\\(symbol\\)\\)\\s-+\\)?"
850 ;; Note starting with word-syntax character:
851 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'")
852 "Regexp matching doc string references to symbols.
854 The words preceding the quoted symbol can be used in doc strings to
855 distinguish references to variables, functions and symbols.")
857 (defvar help-xref-info-regexp
858 "\\<info\\s-+node\\s-`\\([^']+\\)'"
859 "Regexp matching doc string references to an Info node.")
861 (defun help-setup-xref (item interactive-p)
862 "Invoked from commands using the \"*Help*\" buffer to install some xref info.
864 ITEM is a (function . args) pair appropriate for recreating the help
865 buffer after following a reference. INTERACTIVE-P is non-nil if the
866 calling command was invoked interactively. In this case the stack of
867 items for help buffer \"back\" buttons is cleared."
869 (setq help-xref-stack nil))
870 (setq help-xref-stack-item item))
872 (defun help-make-xrefs (&optional buffer)
873 "Parse and hyperlink documentation cross-references in the given BUFFER.
875 Find cross-reference information in a buffer and, if
876 `help-highlight-p' is non-nil, highlight it with face defined by
877 `help-highlight-face'; activate such cross references for selection
878 with `help-follow'. Cross-references have the canonical form `...'
879 and the type of reference may be disambiguated by the preceding
880 word(s) used in `help-xref-symbol-regexp'.
882 A special reference `back' is made to return back through a stack of
883 help buffers. Variable `help-back-label' specifies the text for
887 (set-buffer (or buffer (current-buffer)))
888 (goto-char (point-min))
889 ;; Skip the header-type info, though it might be useful to parse
890 ;; it at some stage (e.g. "function in `library'").
892 (let ((old-modified (buffer-modified-p)))
893 (let ((stab (syntax-table))
895 (inhibit-read-only t))
896 (set-syntax-table emacs-lisp-mode-syntax-table)
897 ;; The following should probably be abstracted out.
902 (while (re-search-forward help-xref-symbol-regexp nil t)
903 (let* ((data (match-string 6))
904 (sym (intern-soft data)))
907 ((match-string 3) ; `variable' &c
908 (and (boundp sym) ; `variable' doesn't ensure
909 ; it's actually bound
910 (help-xref-button 6 #'describe-variable sym)))
911 ((match-string 4) ; `function' &c
912 (and (fboundp sym) ; similarly
913 (help-xref-button 6 #'describe-function sym)))
914 ((match-string 5)) ; nothing for symbol
915 ((and (boundp sym) (fboundp sym))
916 ;; We can't intuit whether to use the
917 ;; variable or function doc -- supply both.
918 (help-xref-button 6 #'help-xref-interned sym))
920 (help-xref-button 6 #'describe-variable sym))
922 (help-xref-button 6 #'describe-function sym)))))))
925 (while (re-search-forward help-xref-info-regexp nil t)
926 (help-xref-button 1 #'Info-goto-node (list (match-data 1)))))
927 ;; An obvious case of a key substitution:
929 (while (re-search-forward
930 "\\<M-x\\s-+\\(\\sw\\(\\sw\\|\\s_\\)+\\)" nil t)
931 (let ((sym (intern-soft (match-string 1))))
933 (help-xref-button 1 #'describe-function sym)))))
934 ;; Look for commands in whole keymap substitutions:
936 ;; Find a header and the column at which the command
937 ;; name will be found.
938 (while (re-search-forward "^key +binding\n\\(-+ +\\)-+\n\n"
940 (let ((col (- (match-end 1) (match-beginning 1))))
942 ;; Ignore single blank lines in table, but not
943 ;; double ones, which should terminate it.
944 (and (looking-at "^\n?[^\n]")
946 (if (and (> (move-to-column col) 0)
947 (looking-at "\\(\\sw\\|\\s_\\)+$"))
949 (let ((sym (intern-soft (match-string 0))))
952 0 #'describe-function sym))))
954 (zerop (forward-line))
955 (move-to-column 0)))))))
956 (set-syntax-table stab))
957 ;; Make a back-reference in this buffer if appropriate.
958 (when help-xref-stack
959 (goto-char (point-max))
961 (insert "\n\n" help-back-label))
962 ;; Just to provide the match data:
963 (looking-at (concat "\n\n\\(" (regexp-quote help-back-label) "\\)"))
964 (help-xref-button 1 #'help-xref-go-back nil)))
965 ;; View mode steals RET from us.
966 (set (make-local-variable 'minor-mode-overriding-map-alist)
967 (list (cons 'view-mode
968 (let ((map (make-sparse-keymap)))
969 (set-keymap-parent map view-mode-map)
970 (define-key map "\r" 'help-follow)
972 (set-buffer-modified-p old-modified))))
974 (defun help-xref-button (match-number function data)
975 "Make a hyperlink for cross-reference text previously matched.
977 MATCH-NUMBER is the subexpression of interest in the last matched
978 regexp. FUNCTION is a function to invoke when the button is
979 activated, applied to DATA. DATA may be a single value or a list.
980 See `help-make-xrefs'."
981 (add-text-properties (match-beginning match-number)
982 (match-end match-number)
983 (list 'mouse-face 'highlight
984 'help-xref (cons function
989 (put-text-property (match-beginning match-number)
990 (match-end match-number)
991 'face help-highlight-face)))
994 ;; Additional functions for (re-)creating types of help buffers.
995 (defun help-xref-interned (symbol)
996 "Follow a hyperlink which appeared to be an arbitrary interned SYMBOL.
998 Both variable and function documentation are extracted into a single
1000 (let ((fdoc (describe-function symbol)))
1001 (describe-variable symbol)
1002 ;; We now have a help buffer on the variable. Insert the function
1004 (goto-char (point-max))
1005 (insert "\n\n" fdoc))
1006 (goto-char (point-min))
1007 (help-setup-xref (cons #'help-xref-interned symbol) nil))
1009 (defun help-xref-mode (buffer)
1010 "Do a `describe-mode' for the specified BUFFER."
1015 ;;; Navigation/hyperlinking with xrefs
1017 (defun help-follow-mouse (click)
1018 "Follow the cross-reference that you click on."
1021 (let* ((start (event-start click))
1022 (window (car start))
1023 (pos (car (cdr start))))
1024 (set-buffer (window-buffer window))
1025 (help-follow pos))))
1027 (defun help-xref-go-back ()
1028 "Go back to the previous help buffer using info on `help-xref-stack'."
1030 (when help-xref-stack
1031 (setq help-xref-stack (cdr help-xref-stack)) ; due to help-follow
1032 (let* ((item (car help-xref-stack))
1035 (setq help-xref-stack (cdr help-xref-stack))
1038 (funcall method args)))))
1040 (defun help-go-back ()
1042 (help-follow (1- (point-max))))
1044 (defun help-follow (&optional pos)
1045 "Follow cross-reference at POS, defaulting to point.
1047 For the cross-reference format, see `help-make-xrefs'."
1049 (let* ((help-data (get-text-property pos 'help-xref))
1050 (method (car help-data))
1051 (args (cdr help-data)))
1052 (setq help-xref-stack (cons help-xref-stack-item help-xref-stack))
1053 (setq help-xref-stack-item nil)
1055 ;; There is a reference at point. Follow it.
1056 (apply method args))))
1058 ;; For tabbing through buffer.
1059 (defun help-next-ref ()
1060 "Find the next help cross-reference in the buffer."
1064 (if (get-text-property (point) 'help-xref) ; move off reference
1065 (goto-char (or (next-single-property-change (point) 'help-xref)
1067 (cond ((setq pos (next-single-property-change (point) 'help-xref))
1068 (if pos (goto-char pos)))
1070 (message "No cross references in the buffer.")
1073 (goto-char (point-min)))))))
1075 (defun help-previous-ref ()
1076 "Find the previous help cross-reference in the buffer."
1080 (if (get-text-property (point) 'help-xref) ; move off reference
1081 (goto-char (or (previous-single-property-change (point) 'help-xref)
1083 (cond ((setq pos (previous-single-property-change (point) 'help-xref))
1084 (if pos (goto-char pos)))
1086 (message "No cross references in the buffer.")
1089 (goto-char (point-max)))))))
1091 ;;; help.el ends here