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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; This package provides a pre-packaged `Electric Help Mode' for
28 ;; browsing on-line help screens. There is one entry point,
29 ;; `with-electric-help'; all you have to give it is a no-argument
30 ;; function that generates the actual text of the help into the current
33 ;; To make this the default, you must do
35 ;; (define-key global-map "\C-h" 'ehelp-command)
36 ;; (define-key global-map [help] 'ehelp-command)
37 ;; (define-key global-map [f1] 'ehelp-command)
42 (defvar electric-help-map
()
43 "Keymap defining commands available in `electric-help-mode'.")
45 (put 'electric-help-undefined
'suppress-keymap t
)
48 (let ((map (make-keymap)))
49 ;; allow all non-self-inserting keys - search, scroll, etc, but
50 ;; let M-x and C-x exit ehelp mode and retain buffer:
52 (define-key map
"\C-u" 'electric-help-undefined
)
53 (define-key map
[?\C-0
] 'electric-help-undefined
)
54 (define-key map
[?\C-1
] 'electric-help-undefined
)
55 (define-key map
[?\C-2
] 'electric-help-undefined
)
56 (define-key map
[?\C-3
] 'electric-help-undefined
)
57 (define-key map
[?\C-4
] 'electric-help-undefined
)
58 (define-key map
[?\C-5
] 'electric-help-undefined
)
59 (define-key map
[?\C-6
] 'electric-help-undefined
)
60 (define-key map
[?\C-7
] 'electric-help-undefined
)
61 (define-key map
[?\C-8
] 'electric-help-undefined
)
62 (define-key map
[?\C-9
] 'electric-help-undefined
)
63 (define-key map
(char-to-string help-char
) 'electric-help-help
)
64 (define-key map
"?" 'electric-help-help
)
65 (define-key map
" " 'scroll-up
)
66 (define-key map
"\^?" 'scroll-down
)
67 (define-key map
"." 'beginning-of-buffer
)
68 (define-key map
"<" 'beginning-of-buffer
)
69 (define-key map
">" 'end-of-buffer
)
70 ;(define-key map "\C-g" 'electric-help-exit)
71 (define-key map
"q" 'electric-help-exit
)
72 (define-key map
"Q" 'electric-help-exit
)
73 ;;a better key than this?
74 (define-key map
"r" 'electric-help-retain
)
75 (define-key map
"R" 'electric-help-retain
)
76 (define-key map
"\ex" 'electric-help-execute-extended
)
77 (define-key map
"\C-x" 'electric-help-ctrl-x-prefix
)
79 (setq electric-help-map map
)))
81 (defun electric-help-mode ()
82 "`with-electric-help' temporarily places its buffer in this mode.
83 \(On exit from `with-electric-help', the buffer is put in `default-major-mode'.)"
84 (setq buffer-read-only t
)
85 (setq mode-name
"Help")
86 (setq major-mode
'help
)
87 (setq mode-line-buffer-identification
'(" Help: %b"))
88 (use-local-map electric-help-map
)
89 (setq mouse-leave-buffer-hook
'(electric-help-retain))
90 ;; this is done below in with-electric-help
91 ;(run-hooks 'electric-help-mode-hook)
95 (defun with-electric-help (thunk &optional buffer noerase minheight
)
96 "Pop up an \"electric\" help buffer.
97 The arguments are THUNK &optional BUFFER NOERASE MINHEIGHT.
98 THUNK is a function of no arguments which is called to initialize the
99 contents of BUFFER. BUFFER defaults to `*Help*'. BUFFER will be
100 erased before THUNK is called unless NOERASE is non-nil. THUNK will
101 be called while BUFFER is current and with `standard-output' bound to
102 the buffer specified by BUFFER.
104 If THUNK returns nil, we display BUFFER starting at the top, and
105 shrink the window to fit. If THUNK returns non-nil, we don't do those things.
107 After THUNK has been called, this function \"electrically\" pops up a window
108 in which BUFFER is displayed and allows the user to scroll through that buffer
109 in electric-help-mode. The window's height will be at least MINHEIGHT if
110 this value is non-nil.
112 If THUNK returns nil, we display BUFFER starting at the top, and
113 shrink the window to fit. If THUNK returns non-nil, we don't do those
116 When the user exits (with `electric-help-exit', or otherwise) the help
117 buffer's window disappears (i.e., we use `save-window-excursion')
118 BUFFER is put into `default-major-mode' (or `fundamental-mode') when we exit."
119 (setq buffer
(get-buffer-create (or buffer
"*Help*")))
120 (let ((one (one-window-p t
))
121 (config (current-window-configuration))
123 (to-be-executed nil
))
126 (if one
(goto-char (window-start (selected-window))))
127 (let ((pop-up-windows t
))
128 (pop-to-buffer buffer
))
131 (if (and minheight
(< (window-height) minheight
))
132 (enlarge-window (- minheight
(window-height))))
134 (or noerase
(erase-buffer)))
135 (let ((standard-output buffer
))
136 (if (not (funcall thunk
))
139 (set-buffer-modified-p nil
)
140 (goto-char (point-min))
141 (if one
(shrink-window-if-larger-than-buffer (selected-window))))))
143 (run-hooks 'electric-help-mode-hook
)
144 (if (eq (car-safe (electric-help-command-loop))
146 (setq config
(current-window-configuration))
150 (setq buffer-read-only nil
)
152 (funcall (or default-major-mode
'fundamental-mode
))
154 (set-window-configuration config
)
157 ;;>> Perhaps this shouldn't be done.
158 ;; so that when we say "Press space to bury" we mean it
159 (replace-buffer-in-windows buffer
)
160 ;; must do this outside of save-window-excursion
161 (bury-buffer buffer
)))
162 (eval to-be-executed
))))
164 (defun electric-help-command-loop ()
166 (if (pos-visible-in-window-p (point-max))
167 (progn (message (substitute-command-keys "<<< Press Space to bury the help buffer, Press \\[electric-help-retain] to retain it >>>"))
168 (if (equal (setq unread-command-events
(list (read-event)))
170 (progn (setq unread-command-events nil
)
172 (let (up down both neither
173 (standard (and (eq (key-binding " ")
175 (eq (key-binding "\^?")
177 (eq (key-binding "q")
179 (eq (key-binding "r")
180 'electric-help-retain
))))
181 (Electric-command-loop
184 (sit-for 0) ;necessary if last command was end-of-buffer or
185 ;beginning-of-buffer - otherwise pos-visible-in-window-p
186 ;will yield a wrong result.
187 (let ((min (pos-visible-in-window-p (point-min)))
188 (max (pos-visible-in-window-p (point-max))))
189 (cond (isearch-mode 'noprompt
)
191 (cond (standard "Press q to exit, r to retain ")
193 (t (setq neither
(substitute-command-keys "Press \\[electric-help-exit] to exit, \\[electric-help-retain] to retain ")))))
195 (cond (standard "Press SPC to scroll, q to exit, r to retain ")
197 (t (setq up
(substitute-command-keys "Press \\[scroll-up] to scroll, \\[electric-help-exit] to exit, \\[electric-help-retain] to retain ")))))
199 (cond (standard "Press DEL to scroll back, q to exit, r to retain ")
201 (t (setq down
(substitute-command-keys "Press \\[scroll-down] to scroll back, \\[electric-help-exit] to exit, \\[electric-help-retain] to retain ")))))
203 (cond (standard "Press SPC to scroll, DEL to scroll back, q to exit, r to retain ")
205 (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 ")))))))))
210 ;(defun electric-help-scroll-up (arg)
213 ; (if (and (null arg) (pos-visible-in-window-p (point-max)))
214 ; (electric-help-exit)
217 (defun electric-help-exit ()
222 (defun electric-help-retain ()
223 "Exit `electric-help', retaining the current window/buffer configuration.
224 \(The *Help* buffer will not be selected, but \\[switch-to-buffer-other-window] RET
227 ;; Make sure that we don't throw twice, even if two events cause
228 ;; calling this function:
229 (if mouse-leave-buffer-hook
231 (setq mouse-leave-buffer-hook nil
)
232 (throw 'exit
'(retain)))))
235 (defun electric-help-undefined ()
237 (error "%s is undefined -- Press %s to exit"
238 (mapconcat 'single-key-description
(this-command-keys) " ")
239 (if (eq (key-binding "q") 'electric-help-exit
)
241 (substitute-command-keys "\\[electric-help-exit]"))))
244 ;>>> this needs to be hairified (recursive help, anybody?)
245 (defun electric-help-help ()
247 (if (and (eq (key-binding "q") 'electric-help-exit
)
248 (eq (key-binding " ") 'scroll-up
)
249 (eq (key-binding "\^?") 'scroll-down
)
250 (eq (key-binding "r") 'electric-help-retain
))
251 (message "SPC scrolls up, DEL scrolls down, q exits burying help buffer, r exits")
252 (message "%s" (substitute-command-keys "\\[scroll-up] scrolls up, \\[scroll-down] scrolls down, \\[electric-help-exit] exits burying help buffer, \\[electric-help-retain] exits")))
257 (defun electric-helpify (fun)
258 (let ((name "*Help*"))
259 (if (save-window-excursion
261 (let* ((p (symbol-function 'print-help-return-message
))
262 (b (get-buffer name
))
263 (m (buffer-modified-p b
)))
264 (and b
(not (get-buffer-window b
))
268 (message "%s..." (capitalize (symbol-name fun
)))
269 ;; with-output-to-temp-buffer marks the buffer as unmodified.
270 ;; kludging excessively and relying on that as some sort
271 ;; of indication leads to the following abomination...
272 ;;>> This would be doable without such icky kludges if either
273 ;;>> (a) there were a function to read the interactive
274 ;;>> args for a command and return a list of those args.
275 ;;>> (To which one would then just apply the command)
276 ;;>> (The only problem with this is that interactive-p
277 ;;>> would break, but that is such a misfeature in
278 ;;>> any case that I don't care)
279 ;;>> It is easy to do this for emacs-lisp functions;
280 ;;>> the only problem is getting the interactive spec
282 ;;>> (b) there were a function which returned a
283 ;;>> modification-tick for a buffer. One could tell
284 ;;>> whether a buffer had changed by whether the
285 ;;>> modification-tick were different.
286 ;;>> (Presumably there would have to be a way to either
287 ;;>> restore the tick to some previous value, or to
288 ;;>> suspend updating of the tick in order to allow
289 ;;>> things like momentary-string-display)
293 (set-buffer-modified-p t
)))
294 (fset 'print-help-return-message
'ignore
)
295 (call-interactively fun
)
296 (and (get-buffer name
)
297 (get-buffer-window (get-buffer name
))
299 (not (eq b
(get-buffer name
)))
300 (not (buffer-modified-p b
)))))
301 (fset 'print-help-return-message p
)
302 (and b
(buffer-name b
)
305 (set-buffer-modified-p m
))))))
306 (with-electric-help 'ignore name t
))))
310 ;; This is to be bound to M-x in ehelp mode. Retains ehelp buffer and then
311 ;; continues with execute-extended-command.
312 (defun electric-help-execute-extended (prefixarg)
314 (setq to-be-executed
'(execute-extended-command nil
))
315 (electric-help-retain))
317 ;; This is to be buond to C-x in ehelp mode. Retains ehelp buffer and then
318 ;; continues with ctrl-x prefix.
319 (defun electric-help-ctrl-x-prefix (prefixarg)
321 (setq to-be-executed
'(progn (message nil
) (setq unread-command-char ?\C-x
)))
322 (electric-help-retain))
325 (defun electric-describe-key ()
327 (electric-helpify 'describe-key
))
329 (defun electric-describe-mode ()
331 (electric-helpify 'describe-mode
))
333 (defun electric-view-lossage ()
335 (electric-helpify 'view-lossage
))
337 ;(defun electric-help-for-help ()
338 ; "See help-for-help"
342 (defun electric-describe-function ()
344 (electric-helpify 'describe-function
))
346 (defun electric-describe-variable ()
348 (electric-helpify 'describe-variable
))
350 (defun electric-describe-bindings ()
352 (electric-helpify 'describe-bindings
))
354 (defun electric-describe-syntax ()
356 (electric-helpify 'describe-syntax
))
358 (defun electric-command-apropos ()
360 (electric-helpify 'command-apropos
))
362 ;(define-key help-map "a" 'electric-command-apropos)
364 (defun electric-apropos ()
366 (electric-helpify 'apropos
))
371 (defvar ehelp-map
())
374 (let ((map (copy-keymap help-map
)))
375 (substitute-key-definition 'apropos
'electric-apropos map
)
376 (substitute-key-definition 'command-apropos
'electric-command-apropos map
)
377 (substitute-key-definition 'describe-key
'electric-describe-key map
)
378 (substitute-key-definition 'describe-mode
'electric-describe-mode map
)
379 (substitute-key-definition 'view-lossage
'electric-view-lossage map
)
380 (substitute-key-definition 'describe-function
'electric-describe-function map
)
381 (substitute-key-definition 'describe-variable
'electric-describe-variable map
)
382 (substitute-key-definition 'describe-bindings
'electric-describe-bindings map
)
383 (substitute-key-definition 'describe-syntax
'electric-describe-syntax map
)
386 (fset 'ehelp-command map
)))
390 ;;; ehelp.el ends here