1 ;;; em-smart.el --- smart display of output -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2014 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
7 ;; This file is part of GNU Emacs.
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 3 of the License, or
12 ;; (at your option) any later version.
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.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;; The best way to get a sense of what this code is trying to do is by
25 ;; using it. Basically, the philosophy represents a blend between the
26 ;; ease of use of modern day shells, and the review-before-you-proceed
27 ;; mentality of Plan 9's 9term.
29 ;; @ When you invoke a command, it is assumed that you want to read
30 ;; the output of that command.
32 ;; @ If the output is not what you wanted, it is assumed that you will
33 ;; want to edit, and then resubmit a refined version of that
36 ;; @ If the output is valid, pressing any self-inserting character key
37 ;; will jump to end of the buffer and insert that character, in
38 ;; order to begin entry of a new command.
40 ;; @ If you show an intention to edit the previous command -- by
41 ;; moving around within it -- then the next self-inserting
42 ;; characters will insert *there*, instead of at the bottom of the
45 ;; @ If you show an intention to review old commands, such as M-p or
46 ;; M-r, point will jump to the bottom of the buffer before invoking
49 ;; @ If none of the above has happened yet (i.e., your point is just
50 ;; sitting on the previous command), you can use SPACE and BACKSPACE
51 ;; (or DELETE) to page forward and backward *through the output of
52 ;; the last command only*. It will constrain the movement of the
53 ;; point and window so that the maximum amount of output is always
54 ;; displayed at all times.
56 ;; @ While output is being generated from a command, the window will
57 ;; be constantly reconfigured (until it would otherwise make no
58 ;; difference) in order to always show you the most output from the
59 ;; command possible. This happens if you change window sizes,
62 ;; @ Like I said, it's not really comprehensible until you try it! ;)
64 ;; One disadvantage of this module is that it increases Eshell's
65 ;; memory consumption by a factor of two or more. With small commands
66 ;; (such as pwd), where the screen is mostly full, consumption can
67 ;; increase by orders of magnitude.
72 (eval-when-compile (require 'eshell
))
76 (defgroup eshell-smart nil
77 "This module combines the facility of normal, modern shells with
78 some of the edit/review concepts inherent in the design of Plan 9's
79 9term. See the docs for more details.
81 Most likely you will have to turn this option on and play around with
82 it to get a real sense of how it works."
83 :tag
"Smart display of output"
84 ;; :link '(info-link "(eshell)Smart display of output")
85 :group
'eshell-module
))
89 (defcustom eshell-smart-load-hook nil
90 "A list of functions to call when loading `eshell-smart'."
91 :version
"24.1" ; removed eshell-smart-initialize
95 (defcustom eshell-smart-unload-hook
99 (remove-hook 'window-configuration-change-hook
100 'eshell-refresh-windows
))))
101 "A hook that gets run when `eshell-smart' is unloaded."
103 :group
'eshell-smart
)
105 (defcustom eshell-review-quick-commands nil
106 "If t, always review commands.
107 Reviewing means keeping point on the text of the command that was just
108 invoked, to allow corrections to be made easily.
110 If set to nil, quick commands won't be reviewed. A quick command is a
111 command that produces no output, and exits successfully.
113 If set to `not-even-short-output', then the definition of \"quick
114 command\" is extended to include commands that produce output, if and
115 only if that output can be presented in its entirely in the Eshell window."
116 :type
'(choice (const :tag
"No" nil
)
118 (const :tag
"Not even short output"
119 not-even-short-output
))
120 :group
'eshell-smart
)
122 (defcustom eshell-smart-display-navigate-list
130 "A list of commands which cause Eshell to jump to the end of buffer."
131 :type
'(repeat function
)
132 :group
'eshell-smart
)
134 (defcustom eshell-smart-space-goes-to-end t
135 "If non-nil, space will go to end of buffer when point-max is visible.
136 That is, if a command is running and the user presses SPACE at a time
137 when the end of the buffer is visible, point will go to the end of the
138 buffer and smart-display will be turned off (that is, subsequently
139 pressing backspace will not cause the buffer to scroll down).
141 This feature is provided to make it very easy to watch the output of a
142 long-running command, such as make, where it's more desirable to see
143 the output go by than to review it afterward.
145 Setting this variable to nil means that space and backspace will
146 always have a consistent behavior, which is to move back and forth
147 through displayed output. But it also means that enabling output
148 tracking requires the user to manually move point to the end of the
149 buffer using \\[end-of-buffer]."
151 :group
'eshell-smart
)
153 (defcustom eshell-where-to-jump
'begin
154 "This variable indicates where point should jump to after a command.
155 The options are `begin', `after' or `end'."
156 :type
'(radio (const :tag
"Beginning of command" begin
)
157 (const :tag
"After command word" after
)
158 (const :tag
"End of command" end
))
159 :group
'eshell-smart
)
161 ;;; Internal Variables:
163 (defvar eshell-smart-displayed nil
)
164 (defvar eshell-smart-command-done nil
)
165 (defvar eshell-currently-handling-window nil
)
169 (defun eshell-smart-initialize ()
170 "Setup Eshell smart display."
171 (unless eshell-non-interactive-p
172 ;; override a few variables, since they would interfere with the
173 ;; smart display functionality.
174 (set (make-local-variable 'eshell-scroll-to-bottom-on-output
) nil
)
175 (set (make-local-variable 'eshell-scroll-to-bottom-on-input
) nil
)
176 (set (make-local-variable 'eshell-scroll-show-maximum-output
) t
)
178 (add-hook 'window-scroll-functions
'eshell-smart-scroll-window nil t
)
179 (add-hook 'window-configuration-change-hook
'eshell-refresh-windows
)
181 (add-hook 'eshell-output-filter-functions
'eshell-refresh-windows t t
)
183 (add-hook 'after-change-functions
'eshell-disable-after-change nil t
)
185 (add-hook 'eshell-input-filter-functions
'eshell-smart-display-setup nil t
)
187 (make-local-variable 'eshell-smart-command-done
)
188 (add-hook 'eshell-post-command-hook
191 (setq eshell-smart-command-done t
))) t t
)
193 (unless (eq eshell-review-quick-commands t
)
194 (add-hook 'eshell-post-command-hook
195 'eshell-smart-maybe-jump-to-end nil t
))))
197 ;; This is called by window-scroll-functions with two arguments.
198 (defun eshell-smart-scroll-window (wind _start
)
199 "Scroll the given Eshell window accordingly."
200 (unless eshell-currently-handling-window
201 (let ((inhibit-point-motion-hooks t
)
202 (eshell-currently-handling-window t
))
203 (save-selected-window
205 (eshell-smart-redisplay)))))
207 (defun eshell-refresh-windows (&optional frame
)
208 "Refresh all visible Eshell buffers."
213 (with-current-buffer (window-buffer wind
)
215 (let (window-scroll-functions)
216 (eshell-smart-scroll-window wind
(window-start))
217 (setq affected t
))))))
220 (let (window-scroll-functions)
221 (eshell-redisplay)))))
223 (defun eshell-smart-display-setup ()
224 "Set the point to somewhere in the beginning of the last command."
226 ((eq eshell-where-to-jump
'begin
)
227 (goto-char eshell-last-input-start
))
228 ((eq eshell-where-to-jump
'after
)
229 (goto-char (next-single-property-change
230 eshell-last-input-start
'arg-end
))
231 (if (= (point) (- eshell-last-input-end
2))
233 ((eq eshell-where-to-jump
'end
)
234 (goto-char (1- eshell-last-input-end
)))
236 (error "Invalid value for `eshell-where-to-jump'")))
237 (setq eshell-smart-command-done nil
)
238 (add-hook 'pre-command-hook
'eshell-smart-display-move nil t
)
239 (eshell-refresh-windows))
241 ;; Called from after-change-functions with 3 arguments.
242 (defun eshell-disable-after-change (_b _e _l
)
243 "Disable smart display mode if the buffer changes in any way."
244 (when eshell-smart-command-done
245 (remove-hook 'pre-command-hook
'eshell-smart-display-move t
)
246 (setq eshell-smart-command-done nil
)))
248 (defun eshell-smart-maybe-jump-to-end ()
249 "Jump to the end of the input buffer.
250 This is done whenever a command exits successfully and both the command
251 and the end of the buffer are still visible."
252 (when (and (= eshell-last-command-status
0)
253 (if (eq eshell-review-quick-commands
'not-even-short-output
)
254 (and (pos-visible-in-window-p (point-max))
255 (pos-visible-in-window-p eshell-last-input-start
))
256 (= (count-lines eshell-last-input-end
257 eshell-last-output-end
) 0)))
258 (goto-char (point-max))
259 (remove-hook 'pre-command-hook
'eshell-smart-display-move t
)))
261 (defun eshell-smart-redisplay ()
262 "Display as much output as possible, smartly."
266 ;; trigger the redisplay now, so that we catch any attempted
267 ;; point motion; this is to cover for a redisplay bug
269 (let ((top-point (point)))
270 (and (memq 'eshell-smart-display-move pre-command-hook
)
271 (>= (point) eshell-last-input-start
)
272 (< (point) eshell-last-input-end
)
273 (set-window-start (selected-window)
274 (line-beginning-position) t
))
275 (if (pos-visible-in-window-p (point-max))
277 (goto-char (point-max))
279 (unless (pos-visible-in-window-p top-point
)
280 (goto-char top-point
)
281 (set-window-start (selected-window)
282 (line-beginning-position) t
)))))))
284 (defun eshell-smart-goto-end ()
285 "Like `end-of-buffer', but do not push a mark."
287 (goto-char (point-max)))
289 (defun eshell-smart-display-move ()
290 "Handle self-inserting or movement commands intelligently."
292 (if (or current-prefix-arg
293 (and (> (point) eshell-last-input-start
)
294 (< (point) eshell-last-input-end
))
295 (>= (point) eshell-last-output-end
))
298 ((eq this-command
'self-insert-command
)
299 (if (eq last-command-event ?
)
300 (if (and eshell-smart-space-goes-to-end
301 eshell-current-command
)
302 (if (not (pos-visible-in-window-p (point-max)))
303 (setq this-command
'scroll-up
)
304 (setq this-command
'eshell-smart-goto-end
))
305 (setq this-command
'scroll-up
))
307 (goto-char (point-max))))
308 ((eq this-command
'delete-backward-char
)
309 (setq this-command
'ignore
)
310 (if (< (point) eshell-last-input-start
)
312 (if (pos-visible-in-window-p eshell-last-input-start
)
316 (eshell-show-output))
318 (if (pos-visible-in-window-p eshell-last-input-end
)
319 (eshell-show-output)))))
320 ((or (memq this-command eshell-smart-display-navigate-list
)
321 (and (eq this-command
'eshell-send-input
)
322 (not (and (>= (point) eshell-last-input-start
)
323 (< (point) eshell-last-input-end
)))))
325 (goto-char (point-max)))))
327 (remove-hook 'pre-command-hook
'eshell-smart-display-move t
))))
332 ;; generated-autoload-file: "esh-groups.el"
335 ;;; em-smart.el ends here