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-setup ()
159 (setq buffer-read-only nil))
161 (add-hook 'temp-buffer-setup-hook 'help-mode-setup)
163 (defun help-mode-finish ()
164 (when (eq major-mode 'help-mode)
165 ;; View mode's read-only status of existing *Help* buffer is lost
166 ;; by with-output-to-temp-buffer.
168 (help-make-xrefs (current-buffer)))
169 (setq view-return-to-alist
170 (list (cons (selected-window) help-return-method))))
172 (add-hook 'temp-buffer-show-hook 'help-mode-finish)
175 "Just exit from the Help command's command loop."
179 (defun help-with-tutorial (&optional arg)
180 "Select the Emacs learn-by-doing tutorial.
181 If there is a tutorial version written in the language
182 of the selected language environment, that version is used.
183 If there's no tutorial in that language, `TUTORIAL' is selected.
184 With arg, you are asked to choose which language."
187 (read-language-name 'tutorial "Language: " "English")
188 (if (get-language-info current-language-environment 'tutorial)
189 current-language-environment
192 (setq filename (get-language-info lang 'tutorial))
193 (setq file (expand-file-name (concat "~/" filename)))
194 (delete-other-windows)
195 (if (get-file-buffer file)
196 (switch-to-buffer (get-file-buffer file))
197 (switch-to-buffer (create-file-buffer file))
198 (setq buffer-file-name file)
199 (setq default-directory (expand-file-name "~/"))
200 (setq buffer-auto-save-file-name nil)
201 (insert-file-contents (expand-file-name filename data-directory))
202 (goto-char (point-min))
203 (search-forward "\n<<")
205 (delete-region (point) (progn (end-of-line) (point)))
206 (let ((n (- (window-height (selected-window))
207 (count-lines (point-min) (point))
211 ;; Some people get confused by the large gap.
213 (insert "[Middle of page left blank for didactic purposes. "
214 "Text continues below]")
215 (newline (- n (/ n 2)))))
216 (goto-char (point-min))
217 (set-buffer-modified-p nil))))
219 (defun describe-key-briefly (key &optional insert)
220 "Print the name of the function KEY invokes. KEY is a string.
221 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
222 (interactive "kDescribe key briefly: \nP")
224 (let ((modifiers (event-modifiers (aref key 0)))
225 (standard-output (if insert (current-buffer) t))
227 ;; For a mouse button event, go to the button it applies to
228 ;; to get the right key bindings. And go to the right place
229 ;; in case the keymap depends on where you clicked.
230 (if (or (memq 'click modifiers) (memq 'down modifiers)
231 (memq 'drag modifiers))
232 (setq window (posn-window (event-start (aref key 0)))
233 position (posn-point (event-start (aref key 0)))))
236 (set-buffer (window-buffer window))
237 (goto-char position)))
238 ;; Ok, now look up the key and name the command.
239 (let ((defn (key-binding key))
240 (key-desc (key-description key)))
241 (if (or (null defn) (integerp defn))
242 (princ (format "%s is undefined" key-desc))
243 (princ (format (if insert
246 "%s at that spot runs the command %s"
247 "%s runs the command %s"))
249 (if (symbolp defn) defn (prin1-to-string defn)))))))))
251 (defvar help-return-method nil
252 "What to do to \"exit\" the help buffer.
254 (WINDOW . t) delete the selected window, go to WINDOW.
255 (WINDOW . quit-window) do quit-window, then select WINDOW.
256 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
258 (defun print-help-return-message (&optional function)
259 "Display or return message saying how to restore windows after help command.
260 Computes a message and applies the optional argument FUNCTION to it.
261 If FUNCTION is nil, applies `message' to it, thus printing it."
262 (and (not (get-buffer-window standard-output))
264 (cond ((special-display-p (buffer-name standard-output))
265 (setq help-return-method (cons (selected-window) t))
266 ;; If the help output buffer is a special display buffer,
267 ;; don't say anything about how to get rid of it.
268 ;; First of all, the user will do that with the window
269 ;; manager, not with Emacs.
270 ;; Secondly, the buffer has not been displayed yet,
271 ;; so we don't know whether its frame will be selected.
273 ((not (one-window-p t))
274 (setq help-return-method
275 (cons (selected-window) 'quit-window))
276 "Type \\[switch-to-buffer-other-window] RET to restore the other window.")
278 (setq help-return-method (cons (selected-window) t))
279 "Type \\[delete-other-windows] to remove help window.")
281 (setq help-return-method
282 (list (selected-window) (window-buffer)
283 (window-start) (window-point)))
284 "Type \\[switch-to-buffer] RET to remove help window."))))
285 (funcall (or function 'message)
288 (substitute-command-keys first-message)
290 (if first-message " " "")
291 ;; If the help buffer will go in a separate frame,
292 ;; it's no use mentioning a command to scroll, so don't.
293 (if (special-display-p (buffer-name standard-output))
295 (if (same-window-p (buffer-name standard-output))
296 ;; Say how to scroll this window.
297 (substitute-command-keys
298 "\\[scroll-up] to scroll the help.")
299 ;; Say how to scroll some other window.
300 (substitute-command-keys
301 "\\[scroll-other-window] to scroll the help."))))))))
303 (defun describe-key (key)
304 "Display documentation of the function invoked by KEY. KEY is a string."
305 (interactive "kDescribe key: ")
307 (let ((modifiers (event-modifiers (aref key 0)))
309 ;; For a mouse button event, go to the button it applies to
310 ;; to get the right key bindings. And go to the right place
311 ;; in case the keymap depends on where you clicked.
312 (if (or (memq 'click modifiers) (memq 'down modifiers)
313 (memq 'drag modifiers))
314 (setq window (posn-window (event-start (aref key 0)))
315 position (posn-point (event-start (aref key 0)))))
318 (set-buffer (window-buffer window))
319 (goto-char position)))
320 (let ((defn (key-binding key)))
321 (if (or (null defn) (integerp defn))
322 (message "%s is undefined" (key-description key))
323 (with-output-to-temp-buffer "*Help*"
324 (princ (key-description key))
326 (princ " at that spot"))
327 (princ " runs the command ")
329 (princ "\n which is ")
330 (describe-function-1 defn nil)
331 (print-help-return-message)))))))
333 (defun describe-mode ()
334 "Display documentation of current major mode and minor modes.
335 The major mode description comes first, followed by the minor modes,
336 each on a separate page.
338 For this to work correctly for a minor mode, the mode's indicator variable
339 \(listed in `minor-mode-alist') must also be a function whose documentation
340 describes the minor mode."
342 (with-output-to-temp-buffer "*Help*"
343 (when minor-mode-alist
344 (princ "The major mode is described first.
345 For minor modes, see following pages.\n\n"))
348 (princ (documentation major-mode))
349 (help-setup-xref (list #'help-xref-mode (current-buffer)) (interactive-p))
350 (let ((minor-modes minor-mode-alist))
352 (let* ((minor-mode (car (car minor-modes)))
353 (indicator (car (cdr (car minor-modes)))))
354 ;; Document a minor mode if it is listed in minor-mode-alist,
355 ;; bound locally in this buffer, non-nil, and has a function
357 (if (and (symbol-value minor-mode)
358 (fboundp minor-mode))
359 (let ((pretty-minor-mode minor-mode))
360 (if (string-match "-mode$" (symbol-name minor-mode))
361 (setq pretty-minor-mode
363 (substring (symbol-name minor-mode)
364 0 (match-beginning 0)))))
365 (while (and indicator (symbolp indicator)
367 (not (eq indicator (symbol-value indicator))))
368 (setq indicator (symbol-value indicator)))
370 (princ (format "%s minor mode (%s):\n"
373 (format "indicator%s" indicator)
375 (princ (documentation minor-mode)))))
376 (setq minor-modes (cdr minor-modes))))
377 (print-help-return-message)))
379 ;; So keyboard macro definitions are documented correctly
380 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
382 (defun describe-distribution ()
383 "Display info on how to obtain the latest version of GNU Emacs."
386 (expand-file-name "DISTRIB" data-directory)))
388 (defun describe-copying ()
389 "Display info on how you may redistribute copies of GNU Emacs."
392 (expand-file-name "COPYING" data-directory))
393 (goto-char (point-min)))
395 (defun describe-project ()
396 "Display info on the GNU project."
399 (expand-file-name "GNU" data-directory))
400 (goto-char (point-min)))
402 (defun describe-no-warranty ()
403 "Display info on all the kinds of warranty Emacs does NOT have."
406 (let (case-fold-search)
407 (search-forward "NO WARRANTY")
410 (defun describe-prefix-bindings ()
411 "Describe the bindings of the prefix used to reach this command.
412 The prefix described consists of all but the last event
413 of the key sequence that ran this command."
415 (let* ((key (this-command-keys)))
418 (substring key 0 (1- (length key)))
419 (let ((prefix (make-vector (1- (length key)) nil))
421 (while (< i (length prefix))
422 (aset prefix i (aref key i))
425 ;; Make C-h after a prefix, when not specifically bound,
426 ;; run describe-prefix-bindings.
427 (setq prefix-help-command 'describe-prefix-bindings)
429 (defun view-emacs-news (&optional arg)
430 "Display info on recent changes to Emacs.
431 With numeric argument display information on correspondingly older changes."
433 (let* ((arg (if arg (prefix-numeric-value arg) 0)))
435 (expand-file-name (concat (make-string arg ?O) "NEWS")
438 (defun view-emacs-FAQ ()
439 "Display the Emacs Frequently Asked Questions (FAQ) file."
441 (find-file-read-only (expand-file-name "FAQ" data-directory)))
443 (defun view-lossage ()
444 "Display last 100 input keystrokes."
446 (with-output-to-temp-buffer "*Help*"
447 (princ (mapconcat (function (lambda (key)
448 (if (or (integerp key)
451 (single-key-description key)
452 (prin1-to-string key nil))))
456 (set-buffer standard-output)
457 (goto-char (point-min))
458 (while (progn (move-to-column 50) (not (eobp)))
459 (search-forward " " nil t)
461 (setq help-xref-stack nil
462 help-xref-stack-item nil))
463 (print-help-return-message)))
465 (defalias 'help 'help-for-help)
466 (make-help-screen help-for-help
467 "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:"
468 "You have typed %THIS-KEY%, the help character. Type a Help option:
469 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
471 a command-apropos. Give a substring, and see a list of commands
472 (functions interactively callable) that contain
473 that substring. See also the apropos command.
474 b describe-bindings. Display table of all key bindings.
475 c describe-key-briefly. Type a command key sequence;
476 it prints the function name that sequence runs.
477 C describe-coding-system. This describes either a specific coding system
478 (if you type its name) or the coding systems currently in use
479 (if you type just RET).
480 f describe-function. Type a function name and get documentation of it.
481 C-f Info-goto-emacs-command-node. Type a function name;
482 it takes you to the Info node for that command.
483 i info. The info documentation reader.
484 I describe-input-method. Describe a specific input method (if you type
485 its name) or the current input method (if you type just RET).
486 k describe-key. Type a command key sequence;
487 it displays the full documentation.
488 C-k Info-goto-emacs-key-command-node. Type a command key sequence;
489 it takes you to the Info node for the command bound to that key.
490 l view-lossage. Shows last 100 characters you typed.
491 L describe-language-environment. This describes either the a
492 specific language environment (if you type its name)
493 or the current language environment (if you type just RET).
494 m describe-mode. Print documentation of current minor modes,
495 and the current major mode, including their special commands.
496 n view-emacs-news. Shows emacs news file.
497 p finder-by-keyword. Find packages matching a given topic keyword.
498 s describe-syntax. Display contents of syntax table, plus explanations
499 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
500 v describe-variable. Type name of a variable;
501 it displays the variable's documentation and value.
502 w where-is. Type command name; it prints which keystrokes
505 F Display the frequently asked questions file.
506 h Display the HELLO file which illustrates various scripts.
507 C-c Display Emacs copying permission (General Public License).
508 C-d Display Emacs ordering information.
509 C-n Display news of recent Emacs changes.
510 C-p Display information about the GNU project.
511 C-w Display information on absence of warranty for GNU Emacs."
514 (defun function-called-at-point ()
515 "Return a function around point or else called by the list containing point.
516 If that doesn't give a function, return nil."
517 (let ((stab (syntax-table)))
518 (set-syntax-table emacs-lisp-mode-syntax-table)
520 (or (condition-case ()
522 (or (not (zerop (skip-syntax-backward "_w")))
523 (eq (char-syntax (following-char)) ?w)
524 (eq (char-syntax (following-char)) ?_)
526 (skip-chars-forward "'")
527 (let ((obj (read (current-buffer))))
528 (and (symbolp obj) (fboundp obj) obj)))
533 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
534 ;; Move up to surrounding paren, then after the open.
537 ;; If there is space here, this is probably something
538 ;; other than a real Lisp function call, so ignore it.
539 (if (looking-at "[ \t]")
540 (error "Probably not a Lisp function call"))
542 (setq obj (read (current-buffer)))
543 (and (symbolp obj) (fboundp obj) obj))))
545 (set-syntax-table stab))))
547 (defvar symbol-file-load-history-loaded nil
548 "Non-nil means we have loaded the file `fns-VERSION.el' in `exec-directory'.
549 That file records the part of `load-history' for preloaded files,
550 which is cleared out before dumping to make Emacs smaller.")
552 (defun symbol-file (function)
553 "Return the input source from which FUNCTION was loaded.
554 The value is normally a string that was passed to `load':
555 either an absolute file name, or a library name
556 \(with no directory name and no `.el' or `.elc' at the end).
557 It can also be nil, if the definition is not associated with any file."
558 (unless symbol-file-load-history-loaded
559 (load (expand-file-name
560 ;; fns-XX.YY.ZZ.el does not work on DOS filesystem.
561 (if (eq system-type 'ms-dos)
563 (format "fns-%s.el" emacs-version))
565 ;; The file name fns-%s.el already has a .el extension.
567 (setq symbol-file-load-history-loaded t))
568 (let ((files load-history)
571 (if (memq function (cdr (car files)))
572 (setq file (car (car files)) files nil))
573 (setq files (cdr files)))
576 (defun describe-function (function)
577 "Display the full documentation of FUNCTION (a symbol)."
579 (let ((fn (function-called-at-point))
580 (enable-recursive-minibuffers t)
582 (setq val (completing-read (if fn
583 (format "Describe function (default %s): " fn)
584 "Describe function: ")
585 obarray 'fboundp t nil nil (symbol-name fn)))
586 (list (if (equal val "")
589 (with-output-to-temp-buffer "*Help*"
591 ;; Use " is " instead of a colon so that
592 ;; it is easier to get out the function name using forward-sexp.
594 (describe-function-1 function nil)
595 (print-help-return-message)
597 (set-buffer standard-output)
598 ;; Return the text we displayed.
600 (message "You didn't specify a function")))
602 (defun describe-function-1 (function parens)
603 (let* ((def (if (symbolp function)
604 (symbol-function function)
606 file-name string need-close
607 (beg (if (commandp def) "an interactive " "a ")))
609 (cond ((or (stringp def)
613 (concat beg "built-in function"))
614 ((byte-code-function-p def)
615 (concat beg "compiled Lisp function"))
617 (while (symbolp (symbol-function def))
618 (setq def (symbol-function def)))
619 (format "an alias for `%s'" def))
620 ((eq (car-safe def) 'lambda)
621 (concat beg "Lisp function"))
622 ((eq (car-safe def) 'macro)
624 ((eq (car-safe def) 'mocklisp)
625 "a mocklisp function")
626 ((eq (car-safe def) 'autoload)
627 (setq file-name (nth 1 def))
628 (format "%s autoloaded %s"
629 (if (commandp def) "an interactive" "an")
630 (if (eq (nth 4 def) 'keymap) "keymap"
631 (if (nth 4 def) "Lisp macro" "Lisp function"))
634 (when (and parens (not (equal string "")))
638 (with-current-buffer "*Help*"
641 (if (re-search-backward "alias for `\\([^`']+\\)'" nil t)
642 (help-xref-button 1 #'describe-function def)))))
644 (setq file-name (symbol-file function)))
648 ;; We used to add .el to the file name,
649 ;; but that's completely wrong when the user used load-file.
652 ;; Make a hyperlink to the library.
653 (with-current-buffer "*Help*"
655 (re-search-backward "`\\([^`']+\\)'" nil t)
656 (help-xref-button 1 #'(lambda (arg)
658 (find-function-noselect arg)))
659 (pop-to-buffer (car location))
660 (goto-char (cdr location))))
662 (if need-close (princ ")"))
665 ;; Handle symbols aliased to other symbols.
666 (setq def (indirect-function def))
667 ;; If definition is a macro, find the function inside it.
668 (if (eq (car-safe def) 'macro)
669 (setq def (cdr def)))
670 (let ((arglist (cond ((byte-code-function-p def)
671 (car (append def nil)))
672 ((eq (car-safe def) 'lambda)
677 (princ (cons (if (symbolp function) function "anonymous")
678 (mapcar (lambda (arg)
679 (if (memq arg '(&optional &rest))
681 (intern (upcase (symbol-name arg)))))
684 (let ((doc (documentation function)))
688 (help-setup-xref (list #'describe-function function) (interactive-p)))
689 (princ "not documented")))))
691 (defun variable-at-point ()
692 "Return the bound variable symbol found around point.
693 Return 0 if there is no such symbol."
695 (let ((stab (syntax-table)))
698 (set-syntax-table emacs-lisp-mode-syntax-table)
699 (or (not (zerop (skip-syntax-backward "_w")))
700 (eq (char-syntax (following-char)) ?w)
701 (eq (char-syntax (following-char)) ?_)
703 (skip-chars-forward "'")
704 (let ((obj (read (current-buffer))))
705 (or (and (symbolp obj) (boundp obj) obj)
707 (set-syntax-table stab)))
710 (defun describe-variable (variable)
711 "Display the full documentation of VARIABLE (a symbol).
712 Returns the documentation as a string, also."
714 (let ((v (variable-at-point))
715 (enable-recursive-minibuffers t)
717 (setq val (completing-read (if (symbolp v)
718 (format "Describe variable (default %s): " v)
719 "Describe variable: ")
720 obarray 'boundp t nil nil
721 (if (symbolp v) (symbol-name v))))
722 (list (if (equal val "")
724 (if (symbolp variable)
726 (with-output-to-temp-buffer "*Help*"
728 (if (not (boundp variable))
733 (princ "'s value is ")
735 (pp (symbol-value variable))
737 (if (local-variable-p variable)
739 (princ (format "Local in buffer %s; " (buffer-name)))
740 (if (not (default-boundp variable))
741 (princ "globally void")
742 (princ "global value is ")
744 (pp (default-value variable)))
748 (set-buffer standard-output)
749 (if (> (count-lines (point-min) (point-max)) 10)
751 (goto-char (point-min))
755 (delete-region (point) (progn (end-of-line) (point)))
756 (insert "'s value is shown below.\n\n")
758 (insert "\n\nValue:"))))))
759 (princ "Documentation:")
761 (let ((doc (documentation-property variable 'variable-documentation)))
762 (princ (or doc "not documented as a variable.")))
763 (help-setup-xref (list #'describe-variable variable) (interactive-p))
765 ;; Make a link to customize if this variable can be customized.
766 ;; Note, it is not reliable to test only for a custom-type property
767 ;; because those are only present after the var's definition
769 (if (or (user-variable-p variable)
770 (get variable 'custom-loads)
771 (get variable 'custom-type))
772 (let ((customize-label "customize"))
775 (princ (concat "You can " customize-label " this variable."))
776 (with-current-buffer "*Help*"
779 (concat "\\(" customize-label "\\)") nil t)
780 (help-xref-button 1 #'(lambda (v)
781 (customize-variable v)) variable)
783 ;; Make a hyperlink to the library if appropriate. (Don't
784 ;; change the format of the buffer's initial line in case
785 ;; anything expects the current format.)
786 (let ((file-name (symbol-file variable)))
788 (princ "\n\nDefined in `")
791 (with-current-buffer "*Help*"
793 (re-search-backward "`\\([^`']+\\)'" nil t)
794 (help-xref-button 1 (lambda (arg)
796 (find-variable-noselect arg)))
797 (pop-to-buffer (car location))
798 (goto-char (cdr location))))
801 (print-help-return-message)
803 (set-buffer standard-output)
804 ;; Return the text we displayed.
806 (message "You did not specify a variable")))
808 (defun describe-bindings (&optional prefix buffer)
809 "Show a list of all defined keys, and their definitions.
810 We put that list in a buffer, and display the buffer.
812 The optional argument PREFIX, if non-nil, should be a key sequence;
813 then we display only bindings that start with that prefix.
814 The optional argument BUFFER specifies which buffer's bindings
815 to display (default, the current buffer)."
817 (or buffer (setq buffer (current-buffer)))
818 (with-current-buffer buffer
819 (describe-bindings-internal nil prefix))
820 (with-current-buffer "*Help*"
821 (help-setup-xref (list #'describe-bindings prefix buffer)
824 (defun where-is (definition &optional insert)
825 "Print message listing key sequences that invoke the command DEFINITION.
826 Argument is a command definition, usually a symbol with a function definition.
827 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
829 (let ((fn (function-called-at-point))
830 (enable-recursive-minibuffers t)
832 (setq val (completing-read (if fn
833 (format "Where is command (default %s): " fn)
834 "Where is command: ")
836 (list (if (equal val "")
838 current-prefix-arg)))
839 (let* ((keys (where-is-internal definition overriding-local-map nil nil))
840 (keys1 (mapconcat 'key-description keys ", "))
841 (standard-output (if insert (current-buffer) t)))
843 (if (> (length keys1) 0)
844 (princ (format "%s (%s)" keys1 definition))
845 (princ (format "M-x %s RET" definition)))
846 (if (> (length keys1) 0)
847 (princ (format "%s is on %s" definition keys1))
848 (princ (format "%s is not on any key" definition)))))
851 (defun locate-library (library &optional nosuffix path interactive-call)
852 "Show the precise file name of Emacs library LIBRARY.
853 This command searches the directories in `load-path' like `M-x load-library'
854 to find the file that `M-x load-library RET LIBRARY RET' would load.
855 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
856 to the specified name LIBRARY.
858 If the optional third arg PATH is specified, that list of directories
859 is used instead of `load-path'.
861 When called from a program, the file name is normaly returned as a
862 string. When run interactively, the argument INTERACTIVE-CALL is t,
863 and the file name is displayed in the echo area."
864 (interactive (list (read-string "Locate library: ")
873 (let ((try (expand-file-name (concat library suf) dir)))
874 (and (file-readable-p try)
875 (null (file-directory-p try))
878 (throw 'answer try)))))
882 ;;; load doesn't handle this yet.
883 ;;; (let ((basic '(".elc" ".el" ""))
884 ;;; (compressed '(".Z" ".gz" "")))
885 ;;; ;; If autocompression mode is on,
886 ;;; ;; consider all combinations of library suffixes
887 ;;; ;; and compression suffixes.
888 ;;; (if (rassq 'jka-compr-handler file-name-handler-alist)
890 ;;; (mapcar (lambda (compelt)
891 ;;; (mapcar (lambda (baselt)
892 ;;; (concat baselt compelt))
897 (or path load-path)))
898 (and interactive-call
900 (message "Library is file %s" result)
901 (message "No library %s in search path" library)))
905 ;;; Grokking cross-reference information in doc strings and
908 ;; This may have some scope for extension and the same or something
909 ;; similar should be done for widget doc strings, which currently use
910 ;; another mechanism.
912 (defcustom help-highlight-p t
913 "*If non-nil, `help-make-xrefs' highlight cross-references.
914 Under a window system it highlights them with face defined by
915 `help-highlight-face'. On a character terminal highlighted
916 references look like cross-references in info mode."
921 (defcustom help-highlight-face 'underline
922 "Face used by `help-make-xrefs' to highlight cross-references.
923 Must be previously-defined."
928 (defvar help-back-label "[back]"
929 "Label to use by `help-make-xrefs' for the go-back reference.")
931 (defvar help-xref-symbol-regexp
932 (concat "\\(\\<\\(\\(variable\\|option\\)\\|"
933 "\\(function\\|command\\)\\|"
934 "\\(symbol\\)\\)\\s-+\\)?"
935 ;; Note starting with word-syntax character:
936 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'")
937 "Regexp matching doc string references to symbols.
939 The words preceding the quoted symbol can be used in doc strings to
940 distinguish references to variables, functions and symbols.")
942 (defvar help-xref-info-regexp
943 "\\<[Ii]nfo[ \t\n]+node[ \t\n]+`\\([^']+\\)'"
944 "Regexp matching doc string references to an Info node.")
946 (defun help-setup-xref (item interactive-p)
947 "Invoked from commands using the \"*Help*\" buffer to install some xref info.
949 ITEM is a (FUNCTION . ARGS) pair appropriate for recreating the help
950 buffer after following a reference. INTERACTIVE-P is non-nil if the
951 calling command was invoked interactively. In this case the stack of
952 items for help buffer \"back\" buttons is cleared."
954 (setq help-xref-stack nil))
955 (setq help-xref-stack-item item))
957 (defun help-make-xrefs (&optional buffer)
958 "Parse and hyperlink documentation cross-references in the given BUFFER.
960 Find cross-reference information in a buffer and, if
961 `help-highlight-p' is non-nil, highlight it with face defined by
962 `help-highlight-face'; activate such cross references for selection
963 with `help-follow'. Cross-references have the canonical form `...'
964 and the type of reference may be disambiguated by the preceding
965 word(s) used in `help-xref-symbol-regexp'.
967 A special reference `back' is made to return back through a stack of
968 help buffers. Variable `help-back-label' specifies the text for
972 (set-buffer (or buffer (current-buffer)))
973 (goto-char (point-min))
974 ;; Skip the header-type info, though it might be useful to parse
975 ;; it at some stage (e.g. "function in `library'").
977 (let ((old-modified (buffer-modified-p)))
978 (let ((stab (syntax-table))
980 (inhibit-read-only t))
981 (set-syntax-table emacs-lisp-mode-syntax-table)
982 ;; The following should probably be abstracted out.
987 (while (re-search-forward help-xref-info-regexp nil t)
988 (let ((data (match-string 1)))
990 (unless (string-match "^([^)]+)" data)
991 (setq data (concat "(emacs)" data))))
992 (help-xref-button 1 #'info data))))
995 (while (re-search-forward help-xref-symbol-regexp nil t)
996 (let* ((data (match-string 6))
997 (sym (intern-soft data)))
1000 ((match-string 3) ; `variable' &c
1001 (and (boundp sym) ; `variable' doesn't ensure
1002 ; it's actually bound
1003 (help-xref-button 6 #'describe-variable sym)))
1004 ((match-string 4) ; `function' &c
1005 (and (fboundp sym) ; similarly
1006 (help-xref-button 6 #'describe-function sym)))
1007 ((match-string 5)) ; nothing for symbol
1008 ((and (boundp sym) (fboundp sym))
1009 ;; We can't intuit whether to use the
1010 ;; variable or function doc -- supply both.
1011 (help-xref-button 6 #'help-xref-interned sym))
1013 (help-xref-button 6 #'describe-variable sym))
1015 (help-xref-button 6 #'describe-function sym)))))))
1016 ;; An obvious case of a key substitution:
1018 (while (re-search-forward
1019 ;; Assume command name is only word characters
1020 ;; and dashes to get things like `use M-x foo.'.
1021 "\\<M-x\\s-+\\(\\sw\\(\\sw\\|-\\)+\\)" nil t)
1022 (let ((sym (intern-soft (match-string 1))))
1024 (help-xref-button 1 #'describe-function sym)))))
1025 ;; Look for commands in whole keymap substitutions:
1027 ;; Make sure to find the first keymap.
1028 (goto-char (point-min))
1029 ;; Find a header and the column at which the command
1030 ;; name will be found.
1031 (while (re-search-forward "^key +binding\n\\(-+ +\\)-+\n\n"
1033 (let ((col (- (match-end 1) (match-beginning 1))))
1035 ;; Ignore single blank lines in table, but not
1036 ;; double ones, which should terminate it.
1037 (and (not (looking-at "\n\n"))
1039 (and (eolp) (forward-line))
1041 (skip-chars-backward "^\t\n")
1042 (if (and (>= (current-column) col)
1043 +(looking-at "\\(\\sw\\|-\\)+$"))
1045 (let ((sym (intern-soft (match-string 0))))
1048 0 #'describe-function sym))))
1049 (zerop (forward-line)))))))))
1050 (set-syntax-table stab))
1051 ;; Make a back-reference in this buffer if appropriate.
1052 (when help-xref-stack
1053 (goto-char (point-max))
1055 (insert "\n\n" help-back-label))
1056 ;; Just to provide the match data:
1057 (looking-at (concat "\n\n\\(" (regexp-quote help-back-label) "\\)"))
1058 (help-xref-button 1 #'help-xref-go-back (current-buffer))))
1059 ;; View mode steals RET from us.
1060 (set (make-local-variable 'minor-mode-overriding-map-alist)
1061 (list (cons 'view-mode
1062 (let ((map (make-sparse-keymap)))
1063 (set-keymap-parent map view-mode-map)
1064 (define-key map "\r" 'help-follow)
1066 (set-buffer-modified-p old-modified))))
1068 (defun help-xref-button (match-number function data)
1069 "Make a hyperlink for cross-reference text previously matched.
1071 MATCH-NUMBER is the subexpression of interest in the last matched
1072 regexp. FUNCTION is a function to invoke when the button is
1073 activated, applied to DATA. DATA may be a single value or a list.
1074 See `help-make-xrefs'."
1075 ;; Don't mung properties we've added specially in some instances.
1076 (unless (get-text-property (match-beginning match-number) 'help-xref)
1077 (add-text-properties (match-beginning match-number)
1078 (match-end match-number)
1079 (list 'mouse-face 'highlight
1080 'help-xref (cons function
1084 (if help-highlight-p
1085 (put-text-property (match-beginning match-number)
1086 (match-end match-number)
1087 'face help-highlight-face))))
1090 ;; Additional functions for (re-)creating types of help buffers.
1091 (defun help-xref-interned (symbol)
1092 "Follow a hyperlink which appeared to be an arbitrary interned SYMBOL.
1094 Both variable and function documentation are extracted into a single
1096 (let ((fdoc (describe-function symbol)))
1097 (describe-variable symbol)
1098 ;; We now have a help buffer on the variable. Insert the function
1100 (goto-char (point-max))
1101 (let ((inhibit-read-only t))
1102 (insert "\n\n" fdoc)))
1103 (goto-char (point-min))
1104 (help-setup-xref (list #'help-xref-interned symbol) nil))
1106 (defun help-xref-mode (buffer)
1107 "Do a `describe-mode' for the specified BUFFER."
1112 ;;; Navigation/hyperlinking with xrefs
1114 (defun help-follow-mouse (click)
1115 "Follow the cross-reference that you click on."
1117 (let* ((start (event-start click))
1118 (window (car start))
1119 (pos (car (cdr start))))
1120 (with-current-buffer (window-buffer window)
1121 (help-follow pos))))
1123 (defun help-xref-go-back (buffer)
1124 "From BUFFER, go back to previous help buffer text using `help-xref-stack'."
1125 (let (item position method args)
1126 (with-current-buffer buffer
1127 (when help-xref-stack
1128 (setq help-xref-stack (cdr help-xref-stack)) ; due to help-follow
1129 (setq item (car help-xref-stack)
1133 (setq help-xref-stack (cdr help-xref-stack))))
1135 (goto-char position)))
1137 (defun help-go-back ()
1138 "Invoke the [back] button (if any) in the Help mode buffer."
1140 (help-follow (1- (point-max))))
1142 (defun help-follow (&optional pos)
1143 "Follow cross-reference at POS, defaulting to point.
1145 For the cross-reference format, see `help-make-xrefs'."
1147 (let* ((help-data (or (and (not (= pos (point-max)))
1148 (get-text-property pos 'help-xref))
1149 (and (not (= pos (point-min)))
1150 (get-text-property (1- pos) 'help-xref))))
1151 (method (car help-data))
1152 (args (cdr help-data)))
1153 (setq help-xref-stack (cons (cons (point) help-xref-stack-item)
1155 (setq help-xref-stack-item nil)
1157 ;; There is a reference at point. Follow it.
1158 (apply method args))))
1160 ;; For tabbing through buffer.
1161 (defun help-next-ref ()
1162 "Find the next help cross-reference in the buffer."
1166 (if (get-text-property (point) 'help-xref) ; move off reference
1167 (goto-char (or (next-single-property-change (point) 'help-xref)
1169 (cond ((setq pos (next-single-property-change (point) 'help-xref))
1170 (if pos (goto-char pos)))
1172 (message "No cross references in the buffer.")
1175 (goto-char (point-min)))))))
1177 (defun help-previous-ref ()
1178 "Find the previous help cross-reference in the buffer."
1182 (if (get-text-property (point) 'help-xref) ; move off reference
1183 (goto-char (or (previous-single-property-change (point) 'help-xref)
1185 (cond ((setq pos (previous-single-property-change (point) 'help-xref))
1186 (if pos (goto-char pos)))
1188 (message "No cross references in the buffer.")
1191 (goto-char (point-max)))))))
1194 ;;; Automatic resizing of temporary buffers.
1196 (defcustom temp-buffer-resize-mode nil
1197 "Non-nil means resize windows displaying temporary buffers.
1198 This makes the window the right height for its contents, but never
1199 more than `temp-buffer-max-height' nor less than `window-min-height'.
1200 This applies to `help', `apropos' and `completion' buffers, and some others.
1202 Setting this variable directly does not take effect;
1203 use either \\[customize] or the function `temp-buffer-resize-mode'."
1204 :get (lambda (symbol)
1205 (and (memq 'resize-temp-buffer-window temp-buffer-show-hook) t))
1206 :set (lambda (symbol value)
1207 (temp-buffer-resize-mode (if value 1 -1)))
1208 :initialize 'custom-initialize-default
1213 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
1214 "*Maximum height of a window displaying a temporary buffer.
1215 This is the maximum height (in text lines) which `resize-temp-buffer-window'
1216 will give to a window displaying a temporary buffer.
1217 It can also be a function which will be called with the object corresponding
1218 to the buffer to be displayed as argument and should return an integer
1220 :type '(choice integer function)
1224 (defun temp-buffer-resize-mode (arg)
1225 "Toggle the mode which that makes windows smaller for temporary buffers.
1226 With prefix argument ARG, turn the resizing of windows displaying temporary
1227 buffers on if ARG is positive or off otherwise.
1228 See the documentation of the variable `temp-buffer-resize-mode' for
1233 (not (memq 'resize-temp-buffer-window temp-buffer-show-hook))
1234 (> (prefix-numeric-value arg) 0))))
1237 ;; `help-mode-maybe' may add a `back' button and thus increase the
1238 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
1239 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
1240 (setq temp-buffer-resize-mode t))
1241 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)
1242 (setq temp-buffer-resize-mode nil))))
1244 (defun resize-temp-buffer-window ()
1245 "Resize the current window to fit its contents.
1246 Will not make it higher than `temp-buffer-max-height' nor smaller than
1247 `window-min-height'. Do nothing if it is the only window on its frame, if it
1248 is not as wide as the frame or if some of the window's contents are scrolled
1250 (unless (or (one-window-p 'nomini)
1251 (not (pos-visible-in-window-p (point-min)))
1252 (/= (frame-width) (window-width)))
1253 (let* ((max-height (if (functionp temp-buffer-max-height)
1254 (funcall temp-buffer-max-height (current-buffer))
1255 temp-buffer-max-height))
1256 (win-height (1- (window-height)))
1257 (min-height (1- window-min-height))
1258 (text-height (window-buffer-height(selected-window)))
1259 (new-height (max (min text-height max-height) min-height)))
1260 (enlarge-window (- new-height win-height)))))
1262 ;;; help.el ends here