|
|
1.1 root 1: ;; Help commands for Emacs
2: ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
3:
4: ;; This file is part of GNU Emacs.
5:
6: ;; GNU Emacs is distributed in the hope that it will be useful,
7: ;; but WITHOUT ANY WARRANTY. No author or distributor
8: ;; accepts responsibility to anyone for the consequences of using it
9: ;; or for whether it serves any particular purpose or works at all,
10: ;; unless he says so in writing. Refer to the GNU Emacs General Public
11: ;; License for full details.
12:
13: ;; Everyone is granted permission to copy, modify and redistribute
14: ;; GNU Emacs, but only under the conditions described in the
15: ;; GNU Emacs General Public License. A copy of this license is
16: ;; supposed to have been given to you along with GNU Emacs so you
17: ;; can know your rights and responsibilities. It should be in a
18: ;; file named COPYING. Among other things, the copyright notice
19: ;; and this notice must be preserved on all copies.
20:
21:
22: (defvar help-map (make-sparse-keymap)
23: "Keymap for characters following the Help key.")
24:
25: (define-key global-map "\C-h" 'help-command)
26: (fset 'help-command help-map)
27:
28: (define-key help-map "\C-h" 'help-for-help)
29: (define-key help-map "?" 'help-for-help)
30:
31: (define-key help-map "\C-c" 'describe-copying)
32: (define-key help-map "\C-d" 'describe-distribution)
33: (define-key help-map "\C-w" 'describe-no-warranty)
34: (define-key help-map "a" 'command-apropos)
35:
36: (define-key help-map "b" 'describe-bindings)
37:
38: (define-key help-map "c" 'describe-key-briefly)
39: (define-key help-map "k" 'describe-key)
40:
41: (define-key help-map "d" 'describe-function)
42: (define-key help-map "f" 'describe-function)
43:
44: (define-key help-map "i" 'info)
45:
46: (define-key help-map "l" 'view-lossage)
47:
48: (define-key help-map "m" 'describe-mode)
49:
50: (define-key help-map "\C-n" 'view-emacs-news)
51: (define-key help-map "n" 'view-emacs-news)
52:
53: (define-key help-map "s" 'describe-syntax)
54:
55: (define-key help-map "t" 'help-with-tutorial)
56:
57: (define-key help-map "w" 'where-is)
58:
59: (define-key help-map "v" 'describe-variable)
60:
61: (defun help-with-tutorial ()
62: "Select the Emacs learn-by-doing tutorial."
63: (interactive)
64: (let ((file (expand-file-name "~/TUTORIAL")))
65: (delete-other-windows)
66: (if (get-file-buffer file)
67: (switch-to-buffer (get-file-buffer file))
68: (switch-to-buffer (create-file-buffer file))
69: (setq buffer-file-name file)
70: (setq default-directory (expand-file-name "~/"))
71: (setq auto-save-file-name nil)
72: (insert-file-contents (expand-file-name "TUTORIAL" exec-directory))
73: (goto-char (point-min))
74: (search-forward "\n<<")
75: (beginning-of-line)
76: (delete-region (point) (progn (end-of-line) (point)))
77: (newline (- (window-height (selected-window))
78: (count-lines (point-min) (point))
79: 6))
80: (goto-char (point-min))
81: (set-buffer-modified-p nil))))
82:
83: (defun describe-key-briefly (key)
84: "Print the name of the function KEY invokes. KEY is a string."
85: (interactive "kDescribe key briefly: ")
86: (let ((defn (key-binding key)))
87: (if (or (null defn) (integerp defn))
88: (message "%s is undefined" (key-description key))
89: (message "%s runs the command %s"
90: (key-description key)
91: (if (symbolp defn) defn (prin1-to-string defn))))))
92:
93: (defun print-help-return-message (&optional function)
94: "Display or return message saying how to restore windows after help command.
95: Computes a message and applies the argument FUNCTION to it.
96: If FUNCTION is nil, applies `message' to it, thus printing it."
97: (and (not (get-buffer-window standard-output))
98: (funcall (or function 'message)
99: (substitute-command-keys
100: (if (one-window-p t)
101: (if pop-up-windows
102: "Type \\[delete-other-windows] to remove help window."
103: "Type \\[switch-to-buffer] RET to remove help window.")
104: "Type \\[switch-to-buffer-other-window] RET to restore old contents of help window.")))))
105:
106: (defun describe-key (key)
107: "Display documentation of the function KEY invokes. KEY is a string."
108: (interactive "kDescribe key: ")
109: (let ((defn (key-binding key)))
110: (if (or (null defn) (integerp defn))
111: (message "%s is undefined" (key-description key))
112: (with-output-to-temp-buffer "*Help*"
113: (prin1 defn)
114: (princ ":\n")
115: (if (documentation defn)
116: (princ (documentation defn))
117: (princ "not documented"))
118: (print-help-return-message)))))
119:
120: (defun describe-mode ()
121: "Display documentation of current major mode."
122: (interactive)
123: (with-output-to-temp-buffer "*Help*"
124: (princ mode-name)
125: (princ " Mode:\n")
126: (princ (documentation major-mode))
127: (print-help-return-message)))
128:
129: (defun describe-distribution ()
130: "Display info on how to obtain the latest version of GNU Emacs."
131: (interactive)
132: (find-file-read-only
133: (expand-file-name "DISTRIB" exec-directory)))
134:
135: (defun describe-copying ()
136: "Display info on how you may redistribute copies of GNU Emacs."
137: (interactive)
138: (find-file-read-only
139: (expand-file-name "COPYING" exec-directory))
140: (goto-char (point-min)))
141:
142: (defun describe-no-warranty ()
143: "Display info on all the kinds of warranty Emacs does NOT have."
144: (interactive)
145: (describe-copying)
146: (let (case-fold-search)
147: (search-forward "NO WARRANTY")
148: (recenter 0)))
149:
150: (defun view-emacs-news ()
151: "Display info on recent changes to Emacs."
152: (interactive)
153: (find-file-read-only (expand-file-name "NEWS" exec-directory)))
154:
155: (defun view-lossage ()
156: "Display last 100 input keystrokes."
157: (interactive)
158: (with-output-to-temp-buffer "*Help*"
159: (princ (key-description (recent-keys)))
160: (save-excursion
161: (set-buffer standard-output)
162: (goto-char (point-min))
163: (while (progn (move-to-column 50) (not (eobp)))
164: (search-forward " " nil t)
165: (newline)))
166: (print-help-return-message)))
167:
168: (defun help-for-help ()
169: "You have typed C-h, the help character. Type a Help option:
170:
171: A command-apropos. Give a substring, and see a list of commands
172: (functions interactively callable) that contain
173: that substring. See also the apropos command.
174: B describe-bindings. Display table of all key bindings.
175: C describe-key-briefly. Type a command key sequence;
176: it prints the function name that sequence runs.
177: F describe-function. Type a function name and get documentation of it.
178: I info. The info documentation reader.
179: K describe-key. Type a command key sequence;
180: it displays the full documentation.
181: L view-lossage. Shows last 100 characters you typed.
182: M describe-mode. Print documentation of current major mode,
183: which describes the commands peculiar to it.
184: N view-emacs-news. Shows emacs news file.
185: S describe-syntax. Display contents of syntax table, plus explanations
186: T help-with-tutorial. Select the Emacs learn-by-doing tutorial.
187: V describe-variable. Type name of a variable;
188: it displays the variable's documentation and value.
189: W where-is. Type command name; it prints which keystrokes
190: invoke that command.
191: C-c print Emacs copying permission (General Public License).
192: C-d print Emacs ordering information.
193: C-n print news of recent Emacs changes.
194: C-w print information on absence of warranty for GNU Emacs."
195: (interactive)
196: (message
197: "A B C F I K L M N S T V W C-c C-d C-n C-w. Type C-h again for more help: ")
198: (let ((char (read-char)))
199: (if (or (= char ?\C-h) (= char ??))
200: (save-window-excursion
201: (switch-to-buffer "*Help*")
202: (erase-buffer)
203: (insert (documentation 'help-for-help))
204: (goto-char (point-min))
205: (while (memq char '(?\C-h ?? ?\C-v ?\ ?\177 ?\M-v))
206: (if (memq char '(?\C-v ?\ ))
207: (scroll-up))
208: (if (memq char '(?\177 ?\M-v))
209: (scroll-down))
210: (message "A B C F I K L M N S T V W C-c C-d C-n C-w%s: "
211: (if (pos-visible-in-window-p (point-max))
212: "" " or Space to scroll"))
213: (let ((cursor-in-echo-area t))
214: (setq char (read-char))))))
215: (let ((defn (cdr (assq (downcase char) (cdr help-map)))))
216: (if defn (call-interactively defn) (ding)))))
217:
218:
219: (defun function-called-at-point ()
220: (condition-case ()
221: (save-excursion
222: (save-restriction
223: (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
224: (backward-up-list 1)
225: (forward-char 1)
226: (let (obj)
227: (setq obj (read (current-buffer)))
228: (and (symbolp obj) (fboundp obj) obj))))
229: (error nil)))
230:
231: (defun describe-function (function)
232: "Display the full documentation of FUNCTION (a symbol)."
233: (interactive
234: (let ((fn (function-called-at-point))
235: (enable-recursive-minibuffers t)
236: val)
237: (setq val (completing-read (if fn
238: (format "Describe function (default %s): " fn)
239: "Describe function: ")
240: obarray 'fboundp t))
241: (list (if (equal val "")
242: fn (intern val)))))
243: (with-output-to-temp-buffer "*Help*"
244: (prin1 function)
245: (princ ":
246: ")
247: (if (documentation function)
248: (princ (documentation function))
249: (princ "not documented"))
250: (print-help-return-message)))
251:
252: (defun variable-at-point ()
253: (condition-case ()
254: (save-excursion
255: (forward-sexp -1)
256: (skip-chars-forward "'")
257: (let ((obj (read (current-buffer))))
258: (and (symbolp obj) (boundp obj) obj)))
259: (error nil)))
260:
261: (defun describe-variable (variable)
262: "Display the full documentation of VARIABLE (a symbol)."
263: (interactive
264: (let ((v (variable-at-point))
265: (enable-recursive-minibuffers t)
266: val)
267: (setq val (completing-read (if v
268: (format "Describe variable (default %s): " v)
269: "Describe variable: ")
270: obarray 'boundp t))
271: (list (if (equal val "")
272: v (intern val)))))
273: (with-output-to-temp-buffer "*Help*"
274: (prin1 variable)
275: (princ "'s value is ")
276: (if (not (boundp variable))
277: (princ "void.")
278: (prin1 (symbol-value variable)))
279: (terpri) (terpri)
280: (princ "Documentation:")
281: (terpri)
282: (let ((doc (documentation-property variable 'variable-documentation)))
283: (if doc
284: (princ (substitute-command-keys doc))
285: (princ "not documented as a variable.")))
286: (print-help-return-message)))
287:
288: (defun command-apropos (string)
289: "Like apropos but lists only symbols that are names of commands
290: \(interactively callable functions)."
291: (interactive "sCommand apropos (regexp): ")
292: (let ((message
293: (let ((standard-output (get-buffer-create "*Help*")))
294: (print-help-return-message 'identity))))
295: (apropos string 'commandp)
296: (and message (message message))))
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.