From 280c471e9691f5d0842140951f1ba9e59b018b49 Mon Sep 17 00:00:00 2001
From: Marijn Haverbeke <marijnh@gmail.com>
Date: Mon, 9 Jan 2012 09:38:00 +0100
Subject: [PATCH] Make refresh() restore the editor's scroll position

---
 lib/codemirror.js | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/lib/codemirror.js b/lib/codemirror.js
index bbc3cae9..4bae981a 100644
--- a/lib/codemirror.js
+++ b/lib/codemirror.js
@@ -69,7 +69,7 @@ var CodeMirror = (function() {
     var sel = {from: {line: 0, ch: 0}, to: {line: 0, ch: 0}, inverted: false};
     // Selection-related flags. shiftSelecting obviously tracks
     // whether the user is holding shift.
-    var shiftSelecting, lastClick, lastDoubleClick, draggingText, overwrite = false;
+    var shiftSelecting, lastClick, lastDoubleClick, lastScrollPos = 0, draggingText, overwrite = false;
     // Variables used by startOperation/endOperation to track what
     // happened during the operation.
     var updateInput, userSelChange, changes, textChanged, selectionChanged, leaveInputAlone,
@@ -97,6 +97,7 @@ var CodeMirror = (function() {
     // handled in onMouseDown for Gecko.
     if (!gecko) connect(scroller, "contextmenu", onContextMenu);
     connect(scroller, "scroll", function() {
+      lastScrollPos = scroller.scrollTop;
       updateDisplay([]);
       if (options.fixedGutter) gutter.style.left = scroller.scrollLeft + "px";
       if (options.onScroll) options.onScroll(instance);
@@ -268,7 +269,11 @@ var CodeMirror = (function() {
       },
 
       operation: function(f){return operation(f)();},
-      refresh: function(){updateDisplay(true);},
+      refresh: function(){
+        updateDisplay(true);
+        if (scroller.scrollHeight > lastScrollPos)
+          scroller.scrollTop = lastScrollPos;
+      },
       getInputField: function(){return input;},
       getWrapperElement: function(){return wrapper;},
       getScrollerElement: function(){return scroller;},
-- 
GitLab