Add entry to NEWS
[emacs.git] / lisp / ehelp.el
blob34c7660c73d158da61ef5cef6822b3fb2f3b7af4
1 ;;; ehelp.el --- bindings for electric-help mode -*- lexical-binding: t -*-
3 ;; Copyright (C) 1986, 1995, 2000-2011 Free Software Foundation, Inc.
5 ;; Author: Richard Mlynarik
6 ;; (according to ack.texi and authors.el)
7 ;; Maintainer: FSF
8 ;; Keywords: help, extensions
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
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
31 ;; buffer.
33 ;; To make this the default, you must do
34 ;; (require 'ehelp)
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)
39 ;;; Code:
41 (require 'electric)
43 (defvar electric-help-form-to-execute nil)
45 (defgroup electric-help ()
46 "Electric help facility."
47 :version "21.1"
48 :group 'help)
50 (defcustom electric-help-shrink-window t
51 "If set, adjust help window sizes to buffer sizes when displaying help."
52 :type 'boolean
53 :group 'electric-help)
55 (defcustom electric-help-mode-hook nil
56 "Hook run by `with-electric-help' after initializing the buffer."
57 :type 'hook
58 :group 'electric-help)
60 (put 'electric-help-undefined 'suppress-keymap t)
62 (defvar electric-help-map
63 (let ((map (make-keymap)))
64 ;; allow all non-self-inserting keys - search, scroll, etc, but
65 ;; let M-x and C-x exit ehelp mode and retain buffer:
66 (suppress-keymap map)
67 (define-key map "\C-u" 'electric-help-undefined)
68 (define-key map [?\C-0] 'electric-help-undefined)
69 (define-key map [?\C-1] 'electric-help-undefined)
70 (define-key map [?\C-2] 'electric-help-undefined)
71 (define-key map [?\C-3] 'electric-help-undefined)
72 (define-key map [?\C-4] 'electric-help-undefined)
73 (define-key map [?\C-5] 'electric-help-undefined)
74 (define-key map [?\C-6] 'electric-help-undefined)
75 (define-key map [?\C-7] 'electric-help-undefined)
76 (define-key map [?\C-8] 'electric-help-undefined)
77 (define-key map [?\C-9] 'electric-help-undefined)
78 (define-key map (char-to-string help-char) 'electric-help-help)
79 (define-key map "?" 'electric-help-help)
80 (define-key map " " 'scroll-up)
81 (define-key map "\^?" 'scroll-down)
82 (define-key map "." 'beginning-of-buffer)
83 (define-key map "<" 'beginning-of-buffer)
84 (define-key map ">" 'end-of-buffer)
85 ;(define-key map "\C-g" 'electric-help-exit)
86 (define-key map "Q" 'electric-help-exit)
87 (define-key map "q" 'electric-help-exit)
88 ;;a better key than this?
89 (define-key map "R" 'electric-help-retain)
90 (define-key map "r" 'electric-help-retain)
91 (define-key map "\ex" 'electric-help-execute-extended)
92 (define-key map "\C-x" 'electric-help-ctrl-x-prefix)
93 map)
94 "Keymap defining commands available in `electric-help-mode'.")
96 (defvar electric-help-orig-major-mode nil)
97 (make-variable-buffer-local 'electric-help-orig-major-mode)
99 (defun electric-help-mode ()
100 "`with-electric-help' temporarily places its buffer in this mode.
101 \(On exit from `with-electric-help', the original `major-mode' is restored.)"
102 (setq buffer-read-only t)
103 (setq electric-help-orig-major-mode major-mode)
104 (setq mode-name "Help")
105 (setq major-mode 'help)
106 (setq mode-line-buffer-identification '(" Help: %b"))
107 (use-local-map electric-help-map)
108 (add-hook 'mouse-leave-buffer-hook 'electric-help-retain)
109 (view-mode -1)
110 ;; this is done below in with-electric-help
111 ;(run-hooks 'electric-help-mode-hook)
114 ;;;###autoload
115 (defun with-electric-help (thunk &optional buffer noerase minheight)
116 "Pop up an \"electric\" help buffer.
117 THUNK is a function of no arguments which is called to initialize the
118 contents of BUFFER. BUFFER defaults to `*Help*'. BUFFER will be
119 erased before THUNK is called unless NOERASE is non-nil. THUNK will
120 be called while BUFFER is current and with `standard-output' bound to
121 the buffer specified by BUFFER.
123 If THUNK returns nil, we display BUFFER starting at the top, and shrink
124 the window to fit. If THUNK returns non-nil, we don't do those things.
126 After THUNK has been called, this function \"electrically\" pops up a
127 window in which BUFFER is displayed and allows the user to scroll
128 through that buffer in `electric-help-mode'. The window's height will
129 be at least MINHEIGHT if this value is non-nil.
131 If THUNK returns nil, we display BUFFER starting at the top, and
132 shrink the window to fit if `electric-help-shrink-window' is non-nil.
133 If THUNK returns non-nil, we don't do those things.
135 When the user exits (with `electric-help-exit', or otherwise), the help
136 buffer's window disappears (i.e., we use `save-window-excursion'), and
137 BUFFER is put back into its original major mode."
138 (setq buffer (get-buffer-create (or buffer "*Help*")))
139 (let ((one (one-window-p t))
140 (config (current-window-configuration))
141 (bury nil)
142 (electric-help-form-to-execute nil))
143 (unwind-protect
144 (save-excursion
145 (when one
146 (goto-char (window-start (selected-window))))
147 (pop-to-buffer-other-window buffer)
148 (with-current-buffer buffer
149 (when (and minheight (< (window-height) minheight))
150 (enlarge-window (- minheight (window-height))))
151 (electric-help-mode)
152 (setq buffer-read-only nil)
153 (unless noerase
154 (erase-buffer)))
155 (let ((standard-output buffer))
156 (unless (funcall thunk)
157 (set-buffer buffer)
158 (set-buffer-modified-p nil)
159 (goto-char (point-min))
160 (when (and one electric-help-shrink-window)
161 (shrink-window-if-larger-than-buffer))))
162 (set-buffer buffer)
163 (run-hooks 'electric-help-mode-hook)
164 (setq buffer-read-only t)
165 (if (eq (car-safe (electric-help-command-loop)) 'retain)
166 (setq config (current-window-configuration))
167 (setq bury t))
168 ;; Remove the hook.
169 (when (memq 'electric-help-retain mouse-leave-buffer-hook)
170 (remove-hook 'mouse-leave-buffer-hook 'electric-help-retain)))
171 (message "")
172 (set-buffer buffer)
173 (setq buffer-read-only nil)
175 ;; Restore the original major mode saved by `electric-help-mode'.
176 ;; We should really get a usable *Help* buffer when retaining
177 ;; the electric one with `r'. The problem is that a simple
178 ;; call to `help-mode' won't cut it; e.g. RET is bound wrong
179 ;; afterwards (`View-scroll-line-forward' instead of `help-follow').
180 ;; That's because Help mode should be set with `with-help-window'
181 ;; instead of the direct call to `help-mode'. But at least
182 ;; RET works correctly on links after using `help-mode'.
183 ;; This is satisfactory enough.
184 (condition-case ()
185 (funcall (or electric-help-orig-major-mode 'fundamental-mode))
186 (error nil))
188 (set-window-configuration config)
189 (when bury
190 ;;>> Perhaps this shouldn't be done,
191 ;; so that when we say "Press space to bury" we mean it
192 (replace-buffer-in-windows buffer)
193 ;; must do this outside of save-window-excursion
194 (bury-buffer buffer))
195 (eval electric-help-form-to-execute))))
197 (defun electric-help-command-loop ()
198 (catch 'exit
199 (if (pos-visible-in-window-p (point-max))
200 (progn (message "%s" (substitute-command-keys "<<< Press Space to bury the help buffer, Press \\[electric-help-retain] to retain it >>>"))
201 (if (equal (setq unread-command-events (list (read-event)))
202 '(?\s))
203 (progn (setq unread-command-events nil)
204 (throw 'exit t)))))
205 (let (up down both neither
206 (standard (and (eq (key-binding " " nil t)
207 'scroll-up)
208 (eq (key-binding "\^?" nil t)
209 'scroll-down)
210 (eq (key-binding "q" nil t)
211 'electric-help-exit)
212 (eq (key-binding "r" nil t)
213 'electric-help-retain))))
214 (Electric-command-loop
215 'exit
216 (function (lambda ()
217 (sit-for 0) ;necessary if last command was end-of-buffer or
218 ;beginning-of-buffer - otherwise pos-visible-in-window-p
219 ;will yield a wrong result.
220 (let ((min (pos-visible-in-window-p (point-min)))
221 (max (pos-visible-in-window-p (1- (point-max)))))
222 (cond (isearch-mode 'noprompt)
223 ((and min max)
224 (cond (standard "Press q to exit, r to retain ")
225 (neither)
226 (t (setq neither (substitute-command-keys "Press \\[electric-help-exit] to exit, \\[electric-help-retain] to retain ")))))
227 (min
228 (cond (standard "Press SPC to scroll, q to exit, r to retain ")
229 (up)
230 (t (setq up (substitute-command-keys "Press \\[scroll-up] to scroll, \\[electric-help-exit] to exit, \\[electric-help-retain] to retain ")))))
231 (max
232 (cond (standard "Press DEL to scroll back, q to exit, r to retain ")
233 (down)
234 (t (setq down (substitute-command-keys "Press \\[scroll-down] to scroll back, \\[electric-help-exit] to exit, \\[electric-help-retain] to retain ")))))
236 (cond (standard "Press SPC to scroll, DEL to scroll back, q to exit, r to retain ")
237 (both)
238 (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 ")))))))))
239 t))))
243 ;(defun electric-help-scroll-up (arg)
244 ; ">>>Doc"
245 ; (interactive "P")
246 ; (if (and (null arg) (pos-visible-in-window-p (point-max)))
247 ; (electric-help-exit)
248 ; (scroll-up arg)))
250 (defun electric-help-exit ()
251 "Exit `with-electric-help', restoring the previous window/buffer configuration.
252 \(The *Help* buffer will be buried.)"
253 (interactive)
254 ;; Make sure that we don't throw twice, even if two events cause
255 ;; calling this function:
256 (if (memq 'electric-help-retain mouse-leave-buffer-hook)
257 (progn
258 (remove-hook 'mouse-leave-buffer-hook 'electric-help-retain)
259 (throw 'exit t))))
261 (defun electric-help-retain ()
262 "Exit `with-electric-help', retaining the current window/buffer configuration.
263 \(The *Help* buffer will not be selected, but \\[switch-to-buffer-other-window] RET
264 will select it.)"
265 (interactive)
266 ;; Make sure that we don't throw twice, even if two events cause
267 ;; calling this function:
268 (if (memq 'electric-help-retain mouse-leave-buffer-hook)
269 (progn
270 (remove-hook 'mouse-leave-buffer-hook 'electric-help-retain)
271 (throw 'exit '(retain)))))
274 (defun electric-help-undefined ()
275 (interactive)
276 (error "%s is undefined -- Press %s to exit"
277 (mapconcat 'single-key-description (this-command-keys) " ")
278 (if (eq (key-binding "q" nil t) 'electric-help-exit)
280 (substitute-command-keys "\\[electric-help-exit]"))))
283 ;>>> this needs to be hairified (recursive help, anybody?)
284 (defun electric-help-help ()
285 (interactive)
286 (if (and (eq (key-binding "q" nil t) 'electric-help-exit)
287 (eq (key-binding " " nil t) 'scroll-up)
288 (eq (key-binding "\^?" nil t) 'scroll-down)
289 (eq (key-binding "r" nil t) 'electric-help-retain))
290 (message "SPC scrolls up, DEL scrolls down, q exits burying help buffer, r exits")
291 (message "%s" (substitute-command-keys "\\[scroll-up] scrolls up, \\[scroll-down] scrolls down, \\[electric-help-exit] exits burying help buffer, \\[electric-help-retain] exits")))
292 (sit-for 2))
295 ;;;###autoload
296 (defun electric-helpify (fun &optional name)
297 (let ((name (or name "*Help*")))
298 (if (save-window-excursion
299 ;; kludge-o-rama
300 (let* ((p (symbol-function 'help-print-return-message))
301 (b (get-buffer name))
302 (m (buffer-modified-p b)))
303 (and b (not (get-buffer-window b))
304 (setq b nil))
305 (unwind-protect
306 (progn
307 (message "%s..." (capitalize (symbol-name fun)))
308 ;; with-output-to-temp-buffer marks the buffer as unmodified.
309 ;; kludging excessively and relying on that as some sort
310 ;; of indication leads to the following abomination...
311 ;;>> This would be doable without such icky kludges if either
312 ;;>> (a) there were a function to read the interactive
313 ;;>> args for a command and return a list of those args.
314 ;;>> (To which one would then just apply the command)
315 ;;>> (The only problem with this is that interactive-p
316 ;;>> would break, but that is such a misfeature in
317 ;;>> any case that I don't care)
318 ;;>> It is easy to do this for emacs-lisp functions;
319 ;;>> the only problem is getting the interactive spec
320 ;;>> for subrs
321 ;;>> (b) there were a function which returned a
322 ;;>> modification-tick for a buffer. One could tell
323 ;;>> whether a buffer had changed by whether the
324 ;;>> modification-tick were different.
325 ;;>> (Presumably there would have to be a way to either
326 ;;>> restore the tick to some previous value, or to
327 ;;>> suspend updating of the tick in order to allow
328 ;;>> things like momentary-string-display)
329 (and b
330 (with-current-buffer b
331 (set-buffer-modified-p t)))
332 (fset 'help-print-return-message 'ignore)
333 (call-interactively fun)
334 (and (get-buffer name)
335 (get-buffer-window (get-buffer name))
336 (or (not b)
337 (not (eq b (get-buffer name)))
338 (not (buffer-modified-p b)))))
339 (fset 'help-print-return-message p)
340 (and b (buffer-name b)
341 (with-current-buffer b
342 (set-buffer-modified-p m))))))
343 (with-electric-help 'ignore name t))))
347 ;; This is to be bound to M-x in ehelp mode. Retains ehelp buffer and then
348 ;; continues with execute-extended-command.
349 (defun electric-help-execute-extended (_prefixarg)
350 (interactive "p")
351 (setq electric-help-form-to-execute '(execute-extended-command nil))
352 (electric-help-retain))
354 ;; This is to be buond to C-x in ehelp mode. Retains ehelp buffer and then
355 ;; continues with ctrl-x prefix.
356 (defun electric-help-ctrl-x-prefix (_prefixarg)
357 (interactive "p")
358 (setq electric-help-form-to-execute '(progn (message nil) (setq unread-command-char ?\C-x)))
359 (electric-help-retain))
362 (defun electric-describe-key ()
363 (interactive)
364 (electric-helpify 'describe-key))
366 (defun electric-describe-mode ()
367 (interactive)
368 (electric-helpify 'describe-mode))
370 (defun electric-view-lossage ()
371 (interactive)
372 (electric-helpify 'view-lossage))
374 ;(defun electric-help-for-help ()
375 ; "See help-for-help"
376 ; (interactive)
379 (defun electric-describe-function ()
380 (interactive)
381 (electric-helpify 'describe-function))
383 (defun electric-describe-variable ()
384 (interactive)
385 (electric-helpify 'describe-variable))
387 (defun electric-describe-bindings ()
388 (interactive)
389 (electric-helpify 'describe-bindings))
391 (defun electric-describe-syntax ()
392 (interactive)
393 (electric-helpify 'describe-syntax))
395 (defun electric-command-apropos ()
396 (interactive)
397 (electric-helpify 'command-apropos "*Apropos*"))
399 ;(define-key help-map "a" 'electric-command-apropos)
401 (defun electric-apropos ()
402 (interactive)
403 (electric-helpify 'apropos))
406 ;;;; ehelp-map
408 (defvar ehelp-map
409 (let ((map (copy-keymap help-map)))
410 (substitute-key-definition 'apropos 'electric-apropos map)
411 (substitute-key-definition 'command-apropos 'electric-command-apropos map)
412 (substitute-key-definition 'describe-key 'electric-describe-key map)
413 (substitute-key-definition 'describe-mode 'electric-describe-mode map)
414 (substitute-key-definition 'view-lossage 'electric-view-lossage map)
415 (substitute-key-definition 'describe-function 'electric-describe-function map)
416 (substitute-key-definition 'describe-variable 'electric-describe-variable map)
417 (substitute-key-definition 'describe-bindings 'electric-describe-bindings map)
418 (substitute-key-definition 'describe-syntax 'electric-describe-syntax map)
419 map))
421 ;;;###(autoload 'ehelp-command "ehelp" "Prefix command for ehelp." t 'keymap)
422 (defalias 'ehelp-command ehelp-map)
423 (put 'ehelp-command 'documentation "Prefix command for ehelp.")
425 (provide 'ehelp)
427 ;;; ehelp.el ends here