Initial revision
[bpt/emacs.git] / lisp / ehelp.el
CommitLineData
a2535589
JA
1;; Copyright (C) 1986 Free Software Foundation, Inc.
2
3;; This file is part of GNU Emacs.
4
5;; GNU Emacs is free software; you can redistribute it and/or modify
6;; it under the terms of the GNU General Public License as published by
7;; the Free Software Foundation; either version 1, or (at your option)
8;; any later version.
9
10;; GNU Emacs is distributed in the hope that it will be useful,
11;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13;; GNU General Public License for more details.
14
15;; You should have received a copy of the GNU General Public License
16;; along with GNU Emacs; see the file COPYING. If not, write to
17;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18
19(require 'electric)
20(provide 'ehelp)
21
22(defvar electric-help-map ()
23 "Keymap defining commands available whilst scrolling
24through a buffer in electric-help-mode")
25
26(put 'electric-help-undefined 'suppress-keymap t)
27(if electric-help-map
28 ()
29 (let ((map (make-keymap)))
30 (fillarray map 'electric-help-undefined)
31 (define-key map (char-to-string meta-prefix-char) (copy-keymap map))
32 (define-key map (char-to-string help-char) 'electric-help-help)
33 (define-key map "?" 'electric-help-help)
34 (define-key map " " 'scroll-up)
35 (define-key map "\^?" 'scroll-down)
36 (define-key map "." 'beginning-of-buffer)
37 (define-key map "<" 'beginning-of-buffer)
38 (define-key map ">" 'end-of-buffer)
39 ;(define-key map "\C-g" 'electric-help-exit)
40 (define-key map "q" 'electric-help-exit)
41 (define-key map "Q" 'electric-help-exit)
42 ;;a better key than this?
43 (define-key map "r" 'electric-help-retain)
44
45 (setq electric-help-map map)))
46
47(defun electric-help-mode ()
48 "with-electric-help temporarily places its buffer in this mode
49\(On exit from with-electric-help, the buffer is put in default-major-mode)"
50 (setq buffer-read-only t)
51 (setq mode-name "Help")
52 (setq major-mode 'help)
53 (setq mode-line-buffer-identification '(" Help: %b"))
54 (use-local-map electric-help-map)
55 ;; this is done below in with-electric-help
56 ;(run-hooks 'electric-help-mode-hook)
57 )
58
59(defun with-electric-help (thunk &optional buffer noerase)
60 "Arguments are THUNK &optional BUFFER NOERASE.
61BUFFER defaults to \"*Help*\"
62THUNK is a function of no arguments which is called to initialise
63 the contents of BUFFER. BUFFER will be erased before THUNK is called unless
64 NOERASE is non-nil. THUNK will be called with standard-output bound to
65 the buffer specified by BUFFER
66
67After THUNK has been called, this function \"electrically\" pops up a window
68in which BUFFER is displayed and allows the user to scroll through that buffer
69in electric-help-mode.
70When the user exits (with electric-help-exit, or otherwise) the help
71buffer's window disappears (ie we use save-window-excursion)
72BUFFER is put into default-major-mode (or fundamental-mode) when we exit"
73 (setq buffer (get-buffer-create (or buffer "*Help*")))
74 (let ((one (one-window-p t))
88c269cb 75 (config (current-window-configuration))
76 (bury nil))
77 (unwind-protect
78 (save-excursion
79 (if one (goto-char (window-start (selected-window))))
80 (let ((pop-up-windows t))
81 (pop-to-buffer buffer))
82 (save-excursion
83 (set-buffer buffer)
84 (electric-help-mode)
85 (setq buffer-read-only nil)
86 (or noerase (erase-buffer)))
87 (let ((standard-output buffer))
88 (if (not (funcall thunk))
89 (progn
90 (set-buffer buffer)
91 (set-buffer-modified-p nil)
92 (goto-char (point-min))
93 (if one (shrink-window-if-larger-than-buffer (selected-window))))))
94 (set-buffer buffer)
95 (run-hooks 'electric-help-mode-hook)
96 (if (eq (car-safe (electric-help-command-loop))
97 'retain)
98 (setq config (current-window-configuration))
99 (setq bury t)))
100 (message "")
101 (set-buffer buffer)
102 (setq buffer-read-only nil)
103 (condition-case ()
104 (funcall (or default-major-mode 'fundamental-mode))
105 (error nil))
106 (set-window-configuration config)
107 (if bury
108 (progn
109 ;;>> Perhaps this shouldn't be done.
110 ;; so that when we say "Press space to bury" we mean it
111 (replace-buffer-in-windows buffer)
112 ;; must do this outside of save-window-excursion
113 (bury-buffer buffer))))))
a2535589
JA
114
115(defun electric-help-command-loop ()
116 (catch 'exit
117 (if (pos-visible-in-window-p (point-max))
118 (progn (message "<<< Press Space to bury the help buffer >>>")
119 (if (= (setq unread-command-char (read-char)) ?\ )
120 (progn (setq unread-command-char -1)
121 (throw 'exit t)))))
122 (let (up down both neither
123 (standard (and (eq (key-binding " ")
124 'scroll-up)
125 (eq (key-binding "\^?")
126 'scroll-down)
127 (eq (key-binding "Q")
128 'electric-help-exit)
129 (eq (key-binding "q")
130 'electric-help-exit))))
131 (Electric-command-loop
132 'exit
133 (function (lambda ()
134 (let ((min (pos-visible-in-window-p (point-min)))
135 (max (pos-visible-in-window-p (point-max))))
136 (cond ((and min max)
137 (cond (standard "Press Q to exit ")
138 (neither)
139 (t (setq neither (substitute-command-keys "Press \\[scroll-up] to exit ")))))
140 (min
141 (cond (standard "Press SPC to scroll, Q to exit ")
142 (up)
143 (t (setq up (substitute-command-keys "Press \\[scroll-up] to scroll; \\[electric-help-exit] to exit ")))))
144 (max
145 (cond (standard "Press DEL to scroll back, Q to exit ")
146 (down)
147 (t (setq down (substitute-command-keys "Press \\[scroll-down] to scroll back, \\[scroll-up] to exit ")))))
148 (t
149 (cond (standard "Press SPC to scroll, DEL to scroll back, Q to exit ")
150 (both)
151 (t (setq both (substitute-command-keys "Press \\[scroll-up] to scroll, \\[scroll-down] to scroll back, \\[electric-help-exit] to exit ")))))))))
152 t))))
153
154
155\f
156;(defun electric-help-scroll-up (arg)
157; ">>>Doc"
158; (interactive "P")
159; (if (and (null arg) (pos-visible-in-window-p (point-max)))
160; (electric-help-exit)
161; (scroll-up arg)))
162
163(defun electric-help-exit ()
164 ">>>Doc"
165 (interactive)
166 (throw 'exit t))
167
168(defun electric-help-retain ()
169 "Exit electric-help, retaining the current window/buffer conifiguration.
170\(The *Help* buffer will not be selected, but \\[switch-to-buffer-other-window] RET
171will select it.)"
172 (interactive)
173 (throw 'exit '(retain)))
174
175
176;(defun electric-help-undefined ()
177; (interactive)
178; (let* ((keys (this-command-keys))
179; (n (length keys)))
180; (if (or (= n 1)
181; (and (= n 2)
182; meta-flag
183; (eq (aref keys 0) meta-prefix-char)))
184; (setq unread-command-char last-input-char
185; current-prefix-arg prefix-arg)
186; ;;>>> I don't care.
187; ;;>>> The emacs command-loop is too much pure pain to
188; ;;>>> duplicate
189; ))
190; (throw 'exit t))
191
192(defun electric-help-undefined ()
193 (interactive)
194 (error "%s is undefined -- Press %s to exit"
195 (mapconcat 'single-key-description (this-command-keys) " ")
196 (if (eq (key-binding "Q") 'electric-help-exit)
197 "Q"
198 (substitute-command-keys "\\[electric-help-exit]"))))
199
200
201;>>> this needs to be hairified (recursive help, anybody?)
202(defun electric-help-help ()
203 (interactive)
204 (if (and (eq (key-binding "Q") 'electric-help-exit)
205 (eq (key-binding " ") 'scroll-up)
206 (eq (key-binding "\^?") 'scroll-down))
207 (message "SPC scrolls forward, DEL scrolls back, Q exits and burys help buffer")
208 ;; to give something for user to look at while slow substitute-cmd-keys
209 ;; grinds away
210 (message "Help...")
211 (message "%s" (substitute-command-keys "\\[scroll-up] scrolls forward, \\[scroll-down] scrolls back, \\[electric-help-exit] exits.")))
212 (sit-for 2))
213
214\f
215(defun electric-helpify (fun)
216 (let ((name "*Help*"))
217 (if (save-window-excursion
218 ;; kludge-o-rama
219 (let* ((p (symbol-function 'print-help-return-message))
220 (b (get-buffer name))
221 (m (buffer-modified-p b)))
222 (and b (not (get-buffer-window b))
223 (setq b nil))
224 (unwind-protect
225 (progn
226 (message "%s..." (capitalize (symbol-name fun)))
227 ;; with-output-to-temp-buffer marks the buffer as unmodified.
228 ;; kludging excessively and relying on that as some sort
229 ;; of indication leads to the following abomination...
230 ;;>> This would be doable without such icky kludges if either
231 ;;>> (a) there were a function to read the interactive
232 ;;>> args for a command and return a list of those args.
233 ;;>> (To which one would then just apply the command)
234 ;;>> (The only problem with this is that interactive-p
235 ;;>> would break, but that is such a misfeature in
236 ;;>> any case that I don't care)
237 ;;>> It is easy to do this for emacs-lisp functions;
238 ;;>> the only problem is getting the interactive spec
239 ;;>> for subrs
240 ;;>> (b) there were a function which returned a
241 ;;>> modification-tick for a buffer. One could tell
242 ;;>> whether a buffer had changed by whether the
243 ;;>> modification-tick were different.
244 ;;>> (Presumably there would have to be a way to either
245 ;;>> restore the tick to some previous value, or to
246 ;;>> suspend updating of the tick in order to allow
247 ;;>> things like momentary-string-display)
248 (and b
249 (save-excursion
250 (set-buffer b)
251 (set-buffer-modified-p t)))
252 (fset 'print-help-return-message 'ignore)
253 (call-interactively fun)
254 (and (get-buffer name)
255 (get-buffer-window (get-buffer name))
256 (or (not b)
257 (not (eq b (get-buffer name)))
258 (not (buffer-modified-p b)))))
259 (fset 'print-help-return-message p)
260 (and b (buffer-name b)
261 (save-excursion
262 (set-buffer b)
263 (set-buffer-modified-p m))))))
264 (with-electric-help 'ignore name t))))
265
266\f
267(defun electric-describe-key ()
268 (interactive)
269 (electric-helpify 'describe-key))
270
271(defun electric-describe-mode ()
272 (interactive)
273 (electric-helpify 'describe-mode))
274
275(defun electric-view-lossage ()
276 (interactive)
277 (electric-helpify 'view-lossage))
278
279;(defun electric-help-for-help ()
280; "See help-for-help"
281; (interactive)
282; )
283
284(defun electric-describe-function ()
285 (interactive)
286 (electric-helpify 'describe-function))
287
288(defun electric-describe-variable ()
289 (interactive)
290 (electric-helpify 'describe-variable))
291
292(defun electric-describe-bindings ()
293 (interactive)
294 (electric-helpify 'describe-bindings))
295
296(defun electric-describe-syntax ()
297 (interactive)
298 (electric-helpify 'describe-syntax))
299
300(defun electric-command-apropos ()
301 (interactive)
302 (electric-helpify 'command-apropos))
303
304;(define-key help-map "a" 'electric-command-apropos)
305
306
307
308\f
309;;;; ehelp-map
310
311(defvar ehelp-map ())
312(if ehelp-map
313 nil
314 (let ((map (copy-keymap help-map)))
315 (substitute-key-definition 'describe-key 'electric-describe-key map)
316 (substitute-key-definition 'describe-mode 'electric-describe-mode map)
317 (substitute-key-definition 'view-lossage 'electric-view-lossage map)
318 (substitute-key-definition 'describe-function 'electric-describe-function map)
319 (substitute-key-definition 'describe-variable 'electric-describe-variable map)
320 (substitute-key-definition 'describe-bindings 'electric-describe-bindings map)
321 (substitute-key-definition 'describe-syntax 'electric-describe-syntax map)
322
323 (setq ehelp-map map)
324 (fset 'ehelp-command map)))
325
326;; Do (define-key global-map "\C-h" 'ehelp-command) if you want to win
327