1 ;;; ehelp.el --- bindings for electric-help mode
3 ;; Copyright (C) 1986, 1995 Free Software Foundation, Inc.
6 ;; Keywords: help, extensions
8 ;; This file is part of GNU Emacs.
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
12 ;; the Free Software Foundation; either version 2, or (at your option)
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.
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.
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 current
32 ;; To make this the default, you must do
34 ;; (define-key global-map "\C-h" 'ehelp-command)
35 ;; (define-key global-map [help] 'ehelp-command)
36 ;; (define-key global-map [f1] 'ehelp-command)
41 (defvar electric-help-map ()
42 "Keymap defining commands available in `electric-help-mode'.")
44 (put 'electric-help-undefined 'suppress-keymap t)
47 (let ((map (make-keymap)))
48 ;; allow all non-self-inserting keys - search, scroll, etc, but
49 ;; let M-x and C-x exit ehelp mode and retain buffer:
51 (define-key map "\C-u" 'electric-help-undefined)
52 (define-key map [?\C-0] 'electric-help-undefined)
53 (define-key map [?\C-1] 'electric-help-undefined)
54 (define-key map [?\C-2] 'electric-help-undefined)
55 (define-key map [?\C-3] 'electric-help-undefined)
56 (define-key map [?\C-4] 'electric-help-undefined)
57 (define-key map [?\C-5] 'electric-help-undefined)
58 (define-key map [?\C-6] 'electric-help-undefined)
59 (define-key map [?\C-7] 'electric-help-undefined)
60 (define-key map [?\C-8] 'electric-help-undefined)
61 (define-key map [?\C-9] 'electric-help-undefined)
62 (define-key map (char-to-string help-char) 'electric-help-help)
63 (define-key map "?" 'electric-help-help)
64 (define-key map " " 'scroll-up)
65 (define-key map "\^?" 'scroll-down)
66 (define-key map "." 'beginning-of-buffer)
67 (define-key map "<" 'beginning-of-buffer)
68 (define-key map ">" 'end-of-buffer)
69 ;(define-key map "\C-g" 'electric-help-exit)
70 (define-key map "q" 'electric-help-exit)
71 (define-key map "Q" 'electric-help-exit)
72 ;;a better key than this?
73 (define-key map "r" 'electric-help-retain)
74 (define-key map "R" 'electric-help-retain)
75 (define-key map "\ex" 'electric-help-execute-extended)
76 (define-key map "\C-x" 'electric-help-ctrl-x-prefix)
78 (setq electric-help-map map)))
80 (defun electric-help-mode ()
81 "`with-electric-help' temporarily places its buffer in this mode.
82 \(On exit from `with-electric-help', the buffer is put in `default-major-mode'.)"
83 (setq buffer-read-only t)
84 (setq mode-name "Help")
85 (setq major-mode 'help)
86 (setq mode-line-buffer-identification '(" Help: %b"))
87 (use-local-map electric-help-map)
88 (setq mouse-leave-buffer-hook '(electric-help-retain))
89 ;; this is done below in with-electric-help
90 ;(run-hooks 'electric-help-mode-hook)
94 (defun with-electric-help (thunk &optional buffer noerase minheight)
95 "Pop up an \"electric\" help buffer.
96 The arguments are THUNK &optional BUFFER NOERASE MINHEIGHT.
97 THUNK is a function of no arguments which is called to initialize the
98 contents of BUFFER. BUFFER defaults to `*Help*'. BUFFER will be
99 erased before THUNK is called unless NOERASE is non-nil. THUNK will
100 be called while BUFFER is current and with `standard-output' bound to
101 the buffer specified by BUFFER.
103 If THUNK returns nil, we display BUFFER starting at the top, and
104 shrink the window to fit. If THUNK returns non-nil, we don't do those things.
106 After THUNK has been called, this function \"electrically\" pops up a window
107 in which BUFFER is displayed and allows the user to scroll through that buffer
108 in electric-help-mode. The window's height will be at least MINHEIGHT if
109 this value is non-nil.
111 If THUNK returns nil, we display BUFFER starting at the top, and
112 shrink the window to fit. If THUNK returns non-nil, we don't do those
115 When the user exits (with `electric-help-exit', or otherwise) the help
116 buffer's window disappears (i.e., we use `save-window-excursion')
117 BUFFER is put into `default-major-mode' (or `fundamental-mode') when we exit."
118 (setq buffer (get-buffer-create (or buffer "*Help*")))
119 (let ((one (one-window-p t))
120 (config (current-window-configuration))
122 (to-be-executed nil))
125 (if one (goto-char (window-start (selected-window))))
126 (let ((pop-up-windows t))
127 (pop-to-buffer buffer))
130 (if (and minheight (< (window-height) minheight))
131 (enlarge-window (- minheight (window-height))))
133 (or noerase (erase-buffer)))
134 (let ((standard-output buffer))
135 (if (not (funcall thunk))
138 (set-buffer-modified-p nil)
139 (goto-char (point-min))
140 (if one (shrink-window-if-larger-than-buffer (selected-window))))))
142 (run-hooks 'electric-help-mode-hook)
143 (if (eq (car-safe (electric-help-command-loop))
145 (setq config (current-window-configuration))
149 (setq buffer-read-only nil)
151 (funcall (or default-major-mode 'fundamental-mode))
153 (set-window-configuration config)
156 ;;>> Perhaps this shouldn't be done.
157 ;; so that when we say "Press space to bury" we mean it
158 (replace-buffer-in-windows buffer)
159 ;; must do this outside of save-window-excursion
160 (bury-buffer buffer)))
161 (eval to-be-executed))))
163 (defun electric-help-command-loop ()
165 (if (pos-visible-in-window-p (point-max))
166 (progn (message (substitute-command-keys "<<< Press Space to bury the help buffer, Press \\[electric-help-retain] to retain it >>>"))
167 (if (equal (setq unread-command-events (list (read-event)))
169 (progn (setq unread-command-events nil)
171 (let (up down both neither
172 (standard (and (eq (key-binding " ")
174 (eq (key-binding "\^?")
176 (eq (key-binding "q")
178 (eq (key-binding "r")
179 'electric-help-retain))))
180 (Electric-command-loop
183 (sit-for 0) ;necessary if last command was end-of-buffer or
184 ;beginning-of-buffer - otherwise pos-visible-in-window-p
185 ;will yield a wrong result.
186 (let ((min (pos-visible-in-window-p (point-min)))
187 (max (pos-visible-in-window-p (point-max))))
188 (cond (isearch-mode 'noprompt)
190 (cond (standard "Press q to exit, r to retain ")
192 (t (setq neither (substitute-command-keys "Press \\[electric-help-exit] to exit, \\[electric-help-retain] to retain ")))))
194 (cond (standard "Press SPC to scroll, q to exit, r to retain ")
196 (t (setq up (substitute-command-keys "Press \\[scroll-up] to scroll, \\[electric-help-exit] to exit, \\[electric-help-retain] to retain ")))))
198 (cond (standard "Press DEL to scroll back, q to exit, r to retain ")
200 (t (setq down (substitute-command-keys "Press \\[scroll-down] to scroll back, \\[electric-help-exit] to exit, \\[electric-help-retain] to retain ")))))
202 (cond (standard "Press SPC to scroll, DEL to scroll back, q to exit, r to retain ")
204 (t (setq both (substitute-command-keys "Press \\[scroll-up] to scroll, \\[scroll-down] to scroll back, \\[electric-help-exit] to exit, \\[electric-help-retain] to retain ")))))))))
209 ;(defun electric-help-scroll-up (arg)
212 ; (if (and (null arg) (pos-visible-in-window-p (point-max)))
213 ; (electric-help-exit)
216 (defun electric-help-exit ()
221 (defun electric-help-retain ()
222 "Exit `electric-help', retaining the current window/buffer configuration.
223 \(The *Help* buffer will not be selected, but \\[switch-to-buffer-other-window] RET
226 ;; Make sure that we don't throw twice, even if two events cause
227 ;; calling this function:
228 (if mouse-leave-buffer-hook
230 (setq mouse-leave-buffer-hook nil)
231 (throw 'exit '(retain)))))
234 (defun electric-help-undefined ()
236 (error "%s is undefined -- Press %s to exit"
237 (mapconcat 'single-key-description (this-command-keys) " ")
238 (if (eq (key-binding "q") 'electric-help-exit)
240 (substitute-command-keys "\\[electric-help-exit]"))))
243 ;>>> this needs to be hairified (recursive help, anybody?)
244 (defun electric-help-help ()
246 (if (and (eq (key-binding "q") 'electric-help-exit)
247 (eq (key-binding " ") 'scroll-up)
248 (eq (key-binding "\^?") 'scroll-down)
249 (eq (key-binding "r") 'electric-help-retain))
250 (message "SPC scrolls up, DEL scrolls down, q exits burying help buffer, r exits")
251 (message "%s" (substitute-command-keys "\\[scroll-up] scrolls up, \\[scroll-down] scrolls down, \\[electric-help-exit] exits burying help buffer, \\[electric-help-retain] exits")))
256 (defun electric-helpify (fun)
257 (let ((name "*Help*"))
258 (if (save-window-excursion
260 (let* ((p (symbol-function 'print-help-return-message))
261 (b (get-buffer name))
262 (m (buffer-modified-p b)))
263 (and b (not (get-buffer-window b))
267 (message "%s..." (capitalize (symbol-name fun)))
268 ;; with-output-to-temp-buffer marks the buffer as unmodified.
269 ;; kludging excessively and relying on that as some sort
270 ;; of indication leads to the following abomination...
271 ;;>> This would be doable without such icky kludges if either
272 ;;>> (a) there were a function to read the interactive
273 ;;>> args for a command and return a list of those args.
274 ;;>> (To which one would then just apply the command)
275 ;;>> (The only problem with this is that interactive-p
276 ;;>> would break, but that is such a misfeature in
277 ;;>> any case that I don't care)
278 ;;>> It is easy to do this for emacs-lisp functions;
279 ;;>> the only problem is getting the interactive spec
281 ;;>> (b) there were a function which returned a
282 ;;>> modification-tick for a buffer. One could tell
283 ;;>> whether a buffer had changed by whether the
284 ;;>> modification-tick were different.
285 ;;>> (Presumably there would have to be a way to either
286 ;;>> restore the tick to some previous value, or to
287 ;;>> suspend updating of the tick in order to allow
288 ;;>> things like momentary-string-display)
292 (set-buffer-modified-p t)))
293 (fset 'print-help-return-message 'ignore)
294 (call-interactively fun)
295 (and (get-buffer name)
296 (get-buffer-window (get-buffer name))
298 (not (eq b (get-buffer name)))
299 (not (buffer-modified-p b)))))
300 (fset 'print-help-return-message p)
301 (and b (buffer-name b)
304 (set-buffer-modified-p m))))))
305 (with-electric-help 'ignore name t))))
309 ;; This is to be bound to M-x in ehelp mode. Retains ehelp buffer and then
310 ;; continues with execute-extended-command.
311 (defun electric-help-execute-extended (prefixarg)
313 (setq to-be-executed '(execute-extended-command nil))
314 (electric-help-retain))
316 ;; This is to be buond to C-x in ehelp mode. Retains ehelp buffer and then
317 ;; continues with ctrl-x prefix.
318 (defun electric-help-ctrl-x-prefix (prefixarg)
320 (setq to-be-executed '(progn (message nil) (setq unread-command-char ?\C-x)))
321 (electric-help-retain))
324 (defun electric-describe-key ()
326 (electric-helpify 'describe-key))
328 (defun electric-describe-mode ()
330 (electric-helpify 'describe-mode))
332 (defun electric-view-lossage ()
334 (electric-helpify 'view-lossage))
336 ;(defun electric-help-for-help ()
337 ; "See help-for-help"
341 (defun electric-describe-function ()
343 (electric-helpify 'describe-function))
345 (defun electric-describe-variable ()
347 (electric-helpify 'describe-variable))
349 (defun electric-describe-bindings ()
351 (electric-helpify 'describe-bindings))
353 (defun electric-describe-syntax ()
355 (electric-helpify 'describe-syntax))
357 (defun electric-command-apropos ()
359 (electric-helpify 'command-apropos))
361 ;(define-key help-map "a" 'electric-command-apropos)
363 (defun electric-apropos ()
365 (electric-helpify 'apropos))
370 (defvar ehelp-map ())
373 (let ((map (copy-keymap help-map)))
374 (substitute-key-definition 'apropos 'electric-apropos map)
375 (substitute-key-definition 'command-apropos 'electric-command-apropos map)
376 (substitute-key-definition 'describe-key 'electric-describe-key map)
377 (substitute-key-definition 'describe-mode 'electric-describe-mode map)
378 (substitute-key-definition 'view-lossage 'electric-view-lossage map)
379 (substitute-key-definition 'describe-function 'electric-describe-function map)
380 (substitute-key-definition 'describe-variable 'electric-describe-variable map)
381 (substitute-key-definition 'describe-bindings 'electric-describe-bindings map)
382 (substitute-key-definition 'describe-syntax 'electric-describe-syntax map)
385 (fset 'ehelp-command map)))
389 ;;; ehelp.el ends here