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