Index: src/orca/braille.py
===================================================================
--- src/orca/braille.py	(revision 4098)
+++ src/orca/braille.py	(working copy)
@@ -816,7 +816,7 @@
 
     _lines = []
     _regionWithFocus = None
-    viewport = [0, 0]
+    viewport = [viewport[0], 0]
 
 def setLines(lines):
     global _lines
@@ -879,13 +879,20 @@
     line = _lines[viewport[1]]
     [string, offset, attributeMask] = line.getLineInfo()
 
+    # How close the cursor is allowed to the edge of the display.
+    marginBuffer = 2 
     # If the cursor is too far right, we scroll the viewport
-    # so the cursor will be on the last cell of the display.
-    #
-    if _regionWithFocus.cursorOffset >= _displaySize[0]:
-        offset += _regionWithFocus.cursorOffset - _displaySize[0] + 1
+    totalOffset = offset + _regionWithFocus.cursorOffset
+    if totalOffset + marginBuffer > (viewport[0] + _displaySize[0]):
+        viewport[0] = max(0, totalOffset - _displaySize[0] + marginBuffer + 1)
+    elif totalOffset - marginBuffer < viewport[0]:
+        viewport[0] =  max(0, totalOffset - marginBuffer)
+    # The = case will not happen because the refresh() would 
+    # have dealt with it for us.
+    
+    # the cursor is within the current display
+    # so we dont need to do anything else.
 
-    viewport[0] = max(0, offset)
 
 def refresh(panToCursor=True, targetCursorCell=0):
     """Repaints the Braille on the physical display.  This clips the entire
@@ -937,20 +944,24 @@
     if focusOffset >= 0:
         cursorOffset = focusOffset + _regionWithFocus.cursorOffset
 
+    # How close the cursor is allowed to the edge of the display.
+    marginBuffer = 2
+
     # Now, if desired, we'll automatically pan the viewport to show
     # the cursor.  If there's no targetCursorCell, then we favor the
     # left of the display if we need to pan left, or we favor the
     # right of the display if we need to pan right.
     #
     if panToCursor and (cursorOffset >= 0):
-        if len(string.decode("UTF-8")) <= _displaySize[0]:
-            viewport[0] = 0
+        if (cursorOffset - focusOffset) <= marginBuffer:
+            viewport[0] = max(0, focusOffset)
         elif targetCursorCell:
             viewport[0] = max(0, cursorOffset - targetCursorCell + 1)
-        elif cursorOffset < viewport[0]:
-            viewport[0] = max(0, cursorOffset)
-        elif cursorOffset >= (viewport[0] + _displaySize[0]):
-            viewport[0] = max(0, cursorOffset - _displaySize[0] + 1)
+        elif cursorOffset - marginBuffer < viewport[0]:
+            viewport[0] = max(0, cursorOffset - marginBuffer)
+        elif cursorOffset + marginBuffer >= (viewport[0] + _displaySize[0]):
+            viewport[0] = max(0, cursorOffset - _displaySize[0] + \
+              marginBuffer + 1)
 
     startPos = viewport[0]
     endPos = startPos + _displaySize[0]
