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