1 ;; Copyright (C) 1986 Free Software Foundation, Inc.
3 ;; This file is part of GNU Emacs.
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)
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.
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.
22 (defvar electric-help-map ()
23 "Keymap defining commands available whilst scrolling
24 through a buffer in electric-help-mode")
26 (put 'electric-help-undefined 'suppress-keymap t)
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)
45 (setq electric-help-map map)))
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)
59 (defun with-electric-help (thunk &optional buffer noerase)
60 "Arguments are THUNK &optional BUFFER NOERASE.
61 BUFFER defaults to \"*Help*\"
62 THUNK 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
67 After THUNK has been called, this function \"electrically\" pops up a window
68 in which BUFFER is displayed and allows the user to scroll through that buffer
69 in electric-help-mode.
70 When the user exits (with electric-help-exit, or otherwise) the help
71 buffer's window disappears (ie we use save-window-excursion)
72 BUFFER 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))
75 (config (current-window-configuration))
79 (if one (goto-char (window-start (selected-window))))
80 (let ((pop-up-windows t))
81 (pop-to-buffer buffer))
85 (setq buffer-read-only nil)
86 (or noerase (erase-buffer)))
87 (let ((standard-output buffer))
88 (if (not (funcall thunk))
91 (set-buffer-modified-p nil)
92 (goto-char (point-min))
93 (if one (shrink-window-if-larger-than-buffer (selected-window))))))
95 (run-hooks 'electric-help-mode-hook)
96 (if (eq (car-safe (electric-help-command-loop))
98 (setq config (current-window-configuration))
102 (setq buffer-read-only nil)
104 (funcall (or default-major-mode 'fundamental-mode))
106 (set-window-configuration config)
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))))))
115 (defun electric-help-command-loop ()
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)
122 (let (up down both neither
123 (standard (and (eq (key-binding " ")
125 (eq (key-binding "\^?")
127 (eq (key-binding "Q")
129 (eq (key-binding "q")
130 'electric-help-exit))))
131 (Electric-command-loop
134 (let ((min (pos-visible-in-window-p (point-min)))
135 (max (pos-visible-in-window-p (point-max))))
137 (cond (standard "Press Q to exit ")
139 (t (setq neither (substitute-command-keys "Press \\[scroll-up] to exit ")))))
141 (cond (standard "Press SPC to scroll, Q to exit ")
143 (t (setq up (substitute-command-keys "Press \\[scroll-up] to scroll; \\[electric-help-exit] to exit ")))))
145 (cond (standard "Press DEL to scroll back, Q to exit ")
147 (t (setq down (substitute-command-keys "Press \\[scroll-down] to scroll back, \\[scroll-up] to exit ")))))
149 (cond (standard "Press SPC to scroll, DEL to scroll back, Q to exit ")
151 (t (setq both (substitute-command-keys "Press \\[scroll-up] to scroll, \\[scroll-down] to scroll back, \\[electric-help-exit] to exit ")))))))))
156 ;(defun electric-help-scroll-up (arg)
159 ; (if (and (null arg) (pos-visible-in-window-p (point-max)))
160 ; (electric-help-exit)
163 (defun electric-help-exit ()
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
173 (throw 'exit '(retain)))
176 ;(defun electric-help-undefined ()
178 ; (let* ((keys (this-command-keys))
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
192 (defun electric-help-undefined ()
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)
198 (substitute-command-keys "\\[electric-help-exit]"))))
201 ;>>> this needs to be hairified (recursive help, anybody?)
202 (defun electric-help-help ()
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
211 (message "%s" (substitute-command-keys "\\[scroll-up] scrolls forward, \\[scroll-down] scrolls back, \\[electric-help-exit] exits.")))
215 (defun electric-helpify (fun)
216 (let ((name "*Help*"))
217 (if (save-window-excursion
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))
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
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)
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))
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)
263 (set-buffer-modified-p m))))))
264 (with-electric-help 'ignore name t))))
267 (defun electric-describe-key ()
269 (electric-helpify 'describe-key))
271 (defun electric-describe-mode ()
273 (electric-helpify 'describe-mode))
275 (defun electric-view-lossage ()
277 (electric-helpify 'view-lossage))
279 ;(defun electric-help-for-help ()
280 ; "See help-for-help"
284 (defun electric-describe-function ()
286 (electric-helpify 'describe-function))
288 (defun electric-describe-variable ()
290 (electric-helpify 'describe-variable))
292 (defun electric-describe-bindings ()
294 (electric-helpify 'describe-bindings))
296 (defun electric-describe-syntax ()
298 (electric-helpify 'describe-syntax))
300 (defun electric-command-apropos ()
302 (electric-helpify 'command-apropos))
304 ;(define-key help-map "a" 'electric-command-apropos)
311 (defvar ehelp-map ())
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)
324 (fset 'ehelp-command map)))
326 ;; Do (define-key global-map "\C-h" 'ehelp-command) if you want to win