Mercurial > emacs
annotate lisp/eshell/em-smart.el @ 63976:0629e50e00f6
(gnus-nocem): Finish `defgroup' description with period.
author | Juanma Barranquero <lekktu@gmail.com> |
---|---|
date | Mon, 04 Jul 2005 00:53:31 +0000 |
parents | 5aa470f4fc15 |
children | 18a818a2ee7c |
rev | line source |
---|---|
38414
67b464da13ec
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
37439
diff
changeset
|
1 ;;; em-smart.el --- smart display of output |
29876 | 2 |
29934
34b1ab9d583d
Change spelling of the Free Software Foundation.
Gerd Moellmann <gerd@gnu.org>
parents:
29876
diff
changeset
|
3 ;; Copyright (C) 1999, 2000 Free Software Foundation |
29876 | 4 |
32526 | 5 ;; Author: John Wiegley <johnw@gnu.org> |
6 | |
29876 | 7 ;; This file is part of GNU Emacs. |
8 | |
9 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
10 ;; it under the terms of the GNU General Public License as published by | |
11 ;; the Free Software Foundation; either version 2, or (at your option) | |
12 ;; any later version. | |
13 | |
14 ;; GNU Emacs is distributed in the hope that it will be useful, | |
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 ;; GNU General Public License for more details. | |
18 | |
19 ;; You should have received a copy of the GNU General Public License | |
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
22 ;; Boston, MA 02111-1307, USA. | |
23 | |
24 (provide 'em-smart) | |
25 | |
26 (eval-when-compile (require 'esh-maint)) | |
27 | |
28 (defgroup eshell-smart nil | |
29 "This module combines the facility of normal, modern shells with | |
30 some of the edit/review concepts inherent in the design of Plan 9's | |
31 9term. See the docs for more details. | |
32 | |
33 Most likely you will have to turn this option on and play around with | |
34 it to get a real sense of how it works." | |
35 :tag "Smart display of output" | |
54567
5aa470f4fc15
* eshell/em-smart.el (eshell-smart):
Juri Linkov <juri@jurta.org>
parents:
54204
diff
changeset
|
36 ;; :link '(info-link "(eshell)Smart display of output") |
29876 | 37 :group 'eshell-module) |
38 | |
39 ;;; Commentary: | |
40 | |
41 ;; The best way to get a sense of what this code is trying to do is by | |
42 ;; using it. Basically, the philosophy represents a blend between the | |
43 ;; ease of use of modern day shells, and the review-before-you-proceed | |
44 ;; mentality of Plan 9's 9term. | |
45 ;; | |
46 ;; @ When you invoke a command, it is assumed that you want to read | |
47 ;; the output of that command. | |
48 ;; | |
49 ;; @ If the output is not what you wanted, it is assumed that you will | |
50 ;; want to edit, and then resubmit a refined version of that | |
51 ;; command. | |
52 ;; | |
53 ;; @ If the output is valid, pressing any self-inserting character key | |
54 ;; will jump to end of the buffer and insert that character, in | |
55 ;; order to begin entry of a new command. | |
56 ;; | |
57 ;; @ If you show an intention to edit the previous command -- by | |
58 ;; moving around within it -- then the next self-inserting | |
59 ;; characters will insert *there*, instead of at the bottom of the | |
60 ;; buffer. | |
61 ;; | |
62 ;; @ If you show an intention to review old commands, such as M-p or | |
63 ;; M-r, point will jump to the bottom of the buffer before invoking | |
64 ;; that command. | |
65 ;; | |
66 ;; @ If none of the above has happened yet (i.e., your point is just | |
67 ;; sitting on the previous command), you can use SPACE and BACKSPACE | |
68 ;; (or DELETE) to page forward and backward *through the output of | |
69 ;; the last command only*. It will constrain the movement of the | |
70 ;; point and window so that the maximum amount of output is always | |
71 ;; displayed at all times. | |
72 ;; | |
73 ;; @ While output is being generated from a command, the window will | |
74 ;; be constantly reconfigured (until it would otherwise make no | |
75 ;; difference) in order to always show you the most output from the | |
76 ;; command possible. This happens if you change window sizes, | |
77 ;; scroll, etc. | |
78 ;; | |
79 ;; @ Like I said, it's not really comprehensible until you try it! ;) | |
33020 | 80 ;; |
81 ;; One disadvantage of this module is that it increases Eshell's | |
82 ;; memory consumption by a factor of two or more. With small commands | |
83 ;; (such as pwd), where the screen is mostly full, consumption can | |
84 ;; increase by orders of magnitude. | |
29876 | 85 |
86 ;;; User Variables: | |
87 | |
88 (defcustom eshell-smart-load-hook '(eshell-smart-initialize) | |
89 "*A list of functions to call when loading `eshell-smart'." | |
90 :type 'hook | |
91 :group 'eshell-smart) | |
92 | |
93 (defcustom eshell-smart-unload-hook | |
94 (list | |
95 (function | |
96 (lambda () | |
97 (remove-hook 'window-configuration-change-hook | |
98 'eshell-refresh-windows)))) | |
99 "*A hook that gets run when `eshell-smart' is unloaded." | |
100 :type 'hook | |
101 :group 'eshell-smart) | |
102 | |
103 (defcustom eshell-review-quick-commands nil | |
31240 | 104 "*If t, always review commands. |
105 Reviewing means keeping point on the text of the command that was just | |
106 invoked, to allow corrections to be made easily. | |
107 | |
108 If set to nil, quick commands won't be reviewed. A quick command is a | |
109 command that produces no output, and exits successfully. | |
110 | |
111 If set to `not-even-short-output', then the definition of \"quick | |
112 command\" is extended to include commands that produce output, iff | |
113 that output can be presented in its entirely in the Eshell window." | |
114 :type '(choice (const :tag "No" nil) | |
115 (const :tag "Yes" t) | |
116 (const :tag "Not even short output" | |
117 not-even-short-output)) | |
29876 | 118 :group 'eshell-smart) |
119 | |
120 (defcustom eshell-smart-display-navigate-list | |
121 '(insert-parentheses | |
122 mouse-yank-at-click | |
123 mouse-yank-secondary | |
124 yank-pop | |
125 yank-rectangle | |
126 yank) | |
127 "*A list of commands which cause Eshell to jump to the end of buffer." | |
128 :type '(repeat function) | |
129 :group 'eshell-smart) | |
130 | |
131 (defcustom eshell-smart-space-goes-to-end t | |
132 "*If non-nil, space will go to end of buffer when point-max is visible. | |
133 That is, if a command is running and the user presses SPACE at a time | |
134 when the end of the buffer is visible, point will go to the end of the | |
135 buffer and smart-display will be turned off (that is, subsequently | |
136 pressing backspace will not cause the buffer to scroll down). | |
137 | |
138 This feature is provided to make it very easy to watch the output of a | |
139 long-running command, such as make, where it's more desirable to see | |
140 the output go by than to review it afterward. | |
141 | |
142 Setting this variable to nil means that space and backspace will | |
143 always have a consistent behavior, which is to move back and forth | |
144 through displayed output. But it also means that enabling output | |
145 tracking requires the user to manually move point to the end of the | |
146 buffer using \\[end-of-buffer]." | |
147 :type 'boolean | |
148 :group 'eshell-smart) | |
149 | |
150 (defcustom eshell-where-to-jump 'begin | |
151 "*This variable indicates where point should jump to after a command. | |
152 The options are `begin', `after' or `end'." | |
153 :type '(radio (const :tag "Beginning of command" begin) | |
154 (const :tag "After command word" after) | |
155 (const :tag "End of command" end)) | |
156 :group 'eshell-smart) | |
157 | |
158 ;;; Internal Variables: | |
159 | |
160 (defvar eshell-smart-displayed nil) | |
161 (defvar eshell-smart-command-done nil) | |
33020 | 162 (defvar eshell-currently-handling-window nil) |
29876 | 163 |
164 ;;; Functions: | |
165 | |
166 (defun eshell-smart-initialize () | |
167 "Setup Eshell smart display." | |
168 (unless eshell-non-interactive-p | |
169 ;; override a few variables, since they would interfere with the | |
170 ;; smart display functionality. | |
171 (set (make-local-variable 'eshell-scroll-to-bottom-on-output) nil) | |
172 (set (make-local-variable 'eshell-scroll-to-bottom-on-input) nil) | |
173 (set (make-local-variable 'eshell-scroll-show-maximum-output) t) | |
174 | |
175 (add-hook 'window-scroll-functions 'eshell-smart-scroll-window nil t) | |
176 (add-hook 'window-configuration-change-hook 'eshell-refresh-windows) | |
177 | |
178 (add-hook 'eshell-output-filter-functions 'eshell-refresh-windows t t) | |
179 | |
33020 | 180 (add-hook 'after-change-functions 'eshell-disable-after-change nil t) |
29876 | 181 |
33020 | 182 (add-hook 'eshell-input-filter-functions 'eshell-smart-display-setup nil t) |
29876 | 183 |
184 (make-local-variable 'eshell-smart-command-done) | |
33020 | 185 (add-hook 'eshell-post-command-hook |
186 (function | |
187 (lambda () | |
188 (setq eshell-smart-command-done t))) t t) | |
29876 | 189 |
31240 | 190 (unless (eq eshell-review-quick-commands t) |
29876 | 191 (add-hook 'eshell-post-command-hook |
192 'eshell-smart-maybe-jump-to-end nil t)))) | |
193 | |
194 (defun eshell-smart-scroll-window (wind start) | |
195 "Scroll the given Eshell window accordingly." | |
196 (unless eshell-currently-handling-window | |
197 (let ((inhibit-point-motion-hooks t) | |
198 (eshell-currently-handling-window t)) | |
33020 | 199 (save-selected-window |
200 (select-window wind) | |
201 (eshell-smart-redisplay))))) | |
29876 | 202 |
203 (defun eshell-refresh-windows (&optional frame) | |
204 "Refresh all visible Eshell buffers." | |
205 (let (affected) | |
206 (walk-windows | |
207 (function | |
208 (lambda (wind) | |
209 (with-current-buffer (window-buffer wind) | |
33020 | 210 (if eshell-mode |
211 (let (window-scroll-functions) | |
212 (eshell-smart-scroll-window wind (window-start)) | |
213 (setq affected t)))))) | |
29876 | 214 0 frame) |
215 (if affected | |
216 (let (window-scroll-functions) | |
217 (eshell-redisplay))))) | |
218 | |
219 (defun eshell-smart-display-setup () | |
220 "Set the point to somewhere in the beginning of the last command." | |
221 (cond | |
222 ((eq eshell-where-to-jump 'begin) | |
223 (goto-char eshell-last-input-start)) | |
224 ((eq eshell-where-to-jump 'after) | |
225 (goto-char (next-single-property-change | |
226 eshell-last-input-start 'arg-end)) | |
227 (if (= (point) (- eshell-last-input-end 2)) | |
228 (forward-char))) | |
229 ((eq eshell-where-to-jump 'end) | |
230 (goto-char (1- eshell-last-input-end))) | |
231 (t | |
232 (error "Invalid value for `eshell-where-to-jump'"))) | |
233 (setq eshell-smart-command-done nil) | |
234 (add-hook 'pre-command-hook 'eshell-smart-display-move nil t) | |
235 (eshell-refresh-windows)) | |
236 | |
237 (defun eshell-disable-after-change (b e l) | |
238 "Disable smart display mode if the buffer changes in any way." | |
239 (when eshell-smart-command-done | |
240 (remove-hook 'pre-command-hook 'eshell-smart-display-move t) | |
241 (setq eshell-smart-command-done nil))) | |
242 | |
243 (defun eshell-smart-maybe-jump-to-end () | |
244 "Jump to the end of the input buffer. | |
54204
e0d19d483614
(eshell-smart-maybe-jump-to-end): Fix typo.
Juanma Barranquero <lekktu@gmail.com>
parents:
52401
diff
changeset
|
245 This is done whenever a command exits successfully and both the command |
31240 | 246 and the end of the buffer are still visible." |
29876 | 247 (when (and (= eshell-last-command-status 0) |
31240 | 248 (if (eq eshell-review-quick-commands 'not-even-short-output) |
249 (and (pos-visible-in-window-p (point-max)) | |
250 (pos-visible-in-window-p eshell-last-input-start)) | |
251 (= (count-lines eshell-last-input-end | |
252 eshell-last-output-end) 0))) | |
29876 | 253 (goto-char (point-max)) |
254 (remove-hook 'pre-command-hook 'eshell-smart-display-move t))) | |
255 | |
256 (defun eshell-smart-redisplay () | |
257 "Display as much output as possible, smartly." | |
258 (if (eobp) | |
37439
f8c03126b032
(eshell-smart-redisplay): Added some safety code to work around a
John Wiegley <johnw@newartisans.com>
parents:
33020
diff
changeset
|
259 (save-excursion |
f8c03126b032
(eshell-smart-redisplay): Added some safety code to work around a
John Wiegley <johnw@newartisans.com>
parents:
33020
diff
changeset
|
260 (recenter -1) |
f8c03126b032
(eshell-smart-redisplay): Added some safety code to work around a
John Wiegley <johnw@newartisans.com>
parents:
33020
diff
changeset
|
261 ;; trigger the redisplay now, so that we catch any attempted |
f8c03126b032
(eshell-smart-redisplay): Added some safety code to work around a
John Wiegley <johnw@newartisans.com>
parents:
33020
diff
changeset
|
262 ;; point motion; this is to cover for a redisplay bug |
f8c03126b032
(eshell-smart-redisplay): Added some safety code to work around a
John Wiegley <johnw@newartisans.com>
parents:
33020
diff
changeset
|
263 (eshell-redisplay)) |
31241 | 264 (let ((top-point (point))) |
265 (and (memq 'eshell-smart-display-move pre-command-hook) | |
266 (>= (point) eshell-last-input-start) | |
267 (< (point) eshell-last-input-end) | |
268 (set-window-start (selected-window) | |
269 (line-beginning-position) t)) | |
270 (if (pos-visible-in-window-p (point-max)) | |
271 (save-excursion | |
272 (goto-char (point-max)) | |
273 (recenter -1) | |
274 (unless (pos-visible-in-window-p top-point) | |
275 (goto-char top-point) | |
276 (set-window-start (selected-window) | |
277 (line-beginning-position) t))))))) | |
29876 | 278 |
279 (defun eshell-smart-goto-end () | |
280 "Like `end-of-buffer', but do not push a mark." | |
281 (interactive) | |
282 (goto-char (point-max))) | |
283 | |
284 (defun eshell-smart-display-move () | |
285 "Handle self-inserting or movement commands intelligently." | |
286 (let (clear) | |
287 (if (or current-prefix-arg | |
288 (and (> (point) eshell-last-input-start) | |
289 (< (point) eshell-last-input-end)) | |
290 (>= (point) eshell-last-output-end)) | |
291 (setq clear t) | |
292 (cond | |
293 ((eq this-command 'self-insert-command) | |
294 (if (eq last-command-char ? ) | |
295 (if (and eshell-smart-space-goes-to-end | |
296 eshell-current-command) | |
297 (if (not (pos-visible-in-window-p (point-max))) | |
298 (setq this-command 'scroll-up) | |
299 (setq this-command 'eshell-smart-goto-end)) | |
300 (setq this-command 'scroll-up)) | |
301 (setq clear t) | |
302 (goto-char (point-max)))) | |
303 ((eq this-command 'delete-backward-char) | |
304 (setq this-command 'ignore) | |
305 (if (< (point) eshell-last-input-start) | |
306 (eshell-show-output) | |
307 (if (pos-visible-in-window-p eshell-last-input-start) | |
308 (progn | |
309 (ignore-errors | |
310 (scroll-down)) | |
311 (eshell-show-output)) | |
312 (scroll-down) | |
313 (if (pos-visible-in-window-p eshell-last-input-end) | |
314 (eshell-show-output))))) | |
315 ((or (memq this-command eshell-smart-display-navigate-list) | |
316 (and (eq this-command 'eshell-send-input) | |
317 (not (and (>= (point) eshell-last-input-start) | |
318 (< (point) eshell-last-input-end))))) | |
319 (setq clear t) | |
320 (goto-char (point-max))))) | |
321 (if clear | |
322 (remove-hook 'pre-command-hook 'eshell-smart-display-move t)))) | |
323 | |
324 ;;; Code: | |
325 | |
52401 | 326 ;;; arch-tag: 8c0112c7-379c-4d54-9a1c-204d68786a4b |
29876 | 327 ;;; em-smart.el ends here |