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