Subject: [PATCH] Speed up em-smart

From: Morgan Smith <[email-masked]>
Date: Tue, 23 Aug 2022 16:06:21 -0400

Hello,

em-smart is very slow.  This patch makes it much faster.  idk why there
was all this redisplay stuff in the module as everything seems to work
great when I remove it all.

Thanks John Wiegley for writing this module!  It's a pretty cool concept
and I'm having fun playing with it

Thanks,

Morgan

>From f015d6ac42d18bf4dbc2223dc6c96d964e4dd994 Mon Sep 17 00:00:00 2001
From: Morgan Smith <[email-masked]>
Date: Tue, 23 Aug 2022 15:47:17 -0400
Subject: [PATCH] Speed up em-smart

em-smart was forcibly re-displaying the screen upwards of 500 times
per screen of output.  This caused the eshell to feel quite slow when
the module was in use.  Everything seems to work just fine without any
redisplay stuff so I just tore it all out.

list/eshell/em-smart.el:

(eshell-mart-unload-hook): Remove eshell-refresh-windows

(eshell-smart-displayed, eshell-currently-handling-window,
eshell-refresh-windows, eshell-smart-redisplay): Delete

(eshell-smart-scroll-window): Add a bunch of logic originally from
eshell-smart-redisplay

(eshell-smart-initialize): Don't add hooks for deleted functions.  Use
eshell-output-filter-functions to run eshell-smart-scroll-window

(eshell-smart-display-setup): Don't refresh windows
---
 lisp/eshell/em-smart.el | 79 +++++++++++------------------------------
 1 file changed, 20 insertions(+), 59 deletions(-)

diff --git a/lisp/eshell/em-smart.el b/lisp/eshell/em-smart.el
index 6768cee4c3..708f950d48 100644
--- a/lisp/eshell/em-smart.el
+++ b/lisp/eshell/em-smart.el
@@ -92,11 +92,7 @@ it to get a real sense of how it works."
   :type 'hook
   :group 'eshell-smart)
 
-(defcustom eshell-smart-unload-hook
-  (list
-   (lambda ()
-     (remove-hook 'window-configuration-change-hook
-                  'eshell-refresh-windows)))
+(defcustom eshell-smart-unload-hook nil
   "A hook that gets run when `eshell-smart' is unloaded."
   :type 'hook
   :group 'eshell-smart)
@@ -159,9 +155,7 @@ The options are `begin', `after' or `end'."
 
 ;;; Internal Variables:
 
-(defvar eshell-smart-displayed nil)
 (defvar eshell-smart-command-done nil)
-(defvar eshell-currently-handling-window nil)
 
 ;;; Functions:
 
@@ -174,10 +168,7 @@ The options are `begin', `after' or `end'."
     (setq-local eshell-scroll-to-bottom-on-input nil)
     (setq-local eshell-scroll-show-maximum-output t)
 
-    (add-hook 'window-scroll-functions 'eshell-smart-scroll-window nil t)
-    (add-hook 'window-configuration-change-hook 'eshell-refresh-windows)
-
-    (add-hook 'eshell-output-filter-functions 'eshell-refresh-windows t t)
+    (add-hook 'eshell-output-filter-functions 'eshell-smart-scroll-window nil t)
 
     (add-hook 'after-change-functions 'eshell-disable-after-change nil t)
 
@@ -193,29 +184,23 @@ The options are `begin', `after' or `end'."
       (add-hook 'eshell-post-command-hook
 		'eshell-smart-maybe-jump-to-end nil t))))
 
-;; This is called by window-scroll-functions with two arguments.
-(defun eshell-smart-scroll-window (wind _start)
-  "Scroll the given Eshell window WIND accordingly."
-  (unless eshell-currently-handling-window
-    (let ((inhibit-point-motion-hooks t)
-	  (eshell-currently-handling-window t))
-      (with-selected-window wind
-	(eshell-smart-redisplay)))))
-
-(defun eshell-refresh-windows (&optional frame)
-  "Refresh all visible Eshell buffers."
-  (let (affected)
-    (walk-windows
-     (lambda (wind)
-       (with-current-buffer (window-buffer wind)
-         (if eshell-mode
-             (let (window-scroll-functions) ;;FIXME: Why?
-               (eshell-smart-scroll-window wind (window-start))
-               (setq affected t)))))
-     0 frame)
-    (if affected
-	(let (window-scroll-functions) ;;FIXME: Why?
-	  (eshell-redisplay)))))
+(defun eshell-smart-scroll-window ()
+  "Scroll the selected window to display as much output as possible, smartly."
+  (when (eq (window-buffer (selected-window)) (current-buffer))
+    (let ((top-point (point)))
+      (and (memq 'eshell-smart-display-move pre-command-hook)
+           (>= (point) eshell-last-input-start)
+           (< (point) eshell-last-input-end)
+           (set-window-start (selected-window)
+                             (line-beginning-position) t))
+      (if (pos-visible-in-window-p (point-max))
+          (save-excursion
+            (goto-char (point-max))
+            (recenter -1)
+            (unless (pos-visible-in-window-p top-point)
+              (goto-char top-point)
+              (set-window-start (selected-window)
+                                (line-beginning-position) t)))))))
 
 (defun eshell-smart-display-setup ()
   "Set the point to somewhere in the beginning of the last command."
@@ -232,8 +217,7 @@ The options are `begin', `after' or `end'."
    (t
     (error "Invalid value for `eshell-where-to-jump'")))
   (setq eshell-smart-command-done nil)
-  (add-hook 'pre-command-hook 'eshell-smart-display-move nil t)
-  (eshell-refresh-windows))
+  (add-hook 'pre-command-hook 'eshell-smart-display-move nil t))
 
 ;; Called from after-change-functions with 3 arguments.
 (defun eshell-disable-after-change (_b _e _l)
@@ -255,29 +239,6 @@ and the end of the buffer are still visible."
     (goto-char (point-max))
     (remove-hook 'pre-command-hook 'eshell-smart-display-move t)))
 
-(defun eshell-smart-redisplay ()
-  "Display as much output as possible, smartly."
-  (if (eobp)
-      (save-excursion
-	(recenter -1)
-	;; trigger the redisplay now, so that we catch any attempted
-	;; point motion; this is to cover for a redisplay bug
-	(eshell-redisplay))
-    (let ((top-point (point)))
-      (and (memq 'eshell-smart-display-move pre-command-hook)
-	   (>= (point) eshell-last-input-start)
-	   (< (point) eshell-last-input-end)
-	   (set-window-start (selected-window)
-			     (line-beginning-position) t))
-      (if (pos-visible-in-window-p (point-max))
-	  (save-excursion
-	    (goto-char (point-max))
-	    (recenter -1)
-	    (unless (pos-visible-in-window-p top-point)
-	      (goto-char top-point)
-	      (set-window-start (selected-window)
-				(line-beginning-position) t)))))))
-
 (defun eshell-smart-goto-end ()
   "Like `end-of-buffer', but do not push a mark."
   (interactive)
-- 
2.37.2