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