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