QScintilla/Shell.py

changeset 6029
0ce26b97e2c0
parent 5991
1ab3f4de8a4b
child 6037
9aec96a3a82a
diff -r 859f6894eed9 -r 0ce26b97e2c0 QScintilla/Shell.py
--- a/QScintilla/Shell.py	Mon Dec 18 18:09:39 2017 +0100
+++ b/QScintilla/Shell.py	Tue Dec 19 19:46:23 2017 +0100
@@ -966,20 +966,35 @@
         if self.__isCursorOnLastLine():
             line, col = self.getCursorPosition()
             lastLine = self.text(line)
+            if lastLine.startswith(sys.ps1):
+                lastLine = lastLine[len(sys.ps1):]
+                col -= len(sys.ps1)
+                prompt = sys.ps1
+            elif lastLine.startswith(sys.ps2):
+                lastLine = lastLine[len(sys.ps2):]
+                col -= len(sys.ps2)
+                prompt = sys.ps2
+            else:
+                prompt = ""
+            if col < 0:
+                col = 0
+                prompt = ""
             
             # Remove if text is selected
             if self.hasSelectedText():
                 lineFrom, indexFrom, lineTo, indexTo = self.getSelection()
                 if self.text(lineFrom).startswith(sys.ps1):
-                    if indexFrom < len(sys.ps1):
-                        indexFrom = len(sys.ps1)
+                    indexFrom -= len(sys.ps1)
+                    indexTo -= len(sys.ps1)
                 elif self.text(lineFrom).startswith(sys.ps2):
-                    if indexFrom < len(sys.ps2):
-                        indexFrom = len(sys.ps2)
+                    indexFrom -= len(sys.ps2)
+                    indexTo -= len(sys.ps2)
+                if indexFrom < 0:
+                    indexFrom = 0
                 lastLine = lastLine[:indexFrom] + lastLine[indexTo:]
                 col = indexFrom
 
-            self.setCursorPosition(line, 0)
+            self.setCursorPosition(line, len(prompt))
             self.deleteLineRight()
             
             lines = QApplication.clipboard().text()
@@ -1006,7 +1021,19 @@
         @param historyIndex history index to be set
         @type int
         """
-        for line in lines.splitlines(True):
+        lines = lines.splitlines(True)
+        if not lines:
+            return
+        
+        indentLen = self.__indentLength(lines[0])
+        for line in lines:
+            if line.startswith(sys.ps1):
+                line = line[len(sys.ps1) + indentLen:]
+            elif line.startswith(sys.ps2):
+                line = line[len(sys.ps2) + indentLen:]
+            else:
+                line = line[indentLen:]
+            
             if line.endswith("\r\n"):
                 fullline = True
                 cmd = line[:-2]
@@ -1020,16 +1047,27 @@
             self.__insertTextAtEnd(line)
             if fullline:
                 self.incrementalSearchActive = False
-                if cmd.startswith(sys.ps1):
-                    cmd = cmd[len(sys.ps1):]
-                elif cmd.startswith(sys.ps2):
-                    cmd = cmd[len(sys.ps2):]
                 
                 self.__executeCommand(cmd, historyIndex=historyIndex)
                 if self.interruptCommandExecution:
                     self.__executeCommand("")
                     break
+    
+    def __indentLength(self, line):
+        """
+        Private method to determine the indentation length of the given line.
         
+        @param line line to determine the indentation length for
+        @type str
+        @return indentation length
+        @rtype int
+        """
+        if line.startswith(sys.ps1):
+            line = line[len(sys.ps1):]
+        # If line starts with sys.ps2 or neither don't manipulate the line.
+        indentLen = len(line) - len(line.lstrip())
+        return indentLen
+    
     def __clearCurrentLine(self):
         """
         Private method to clear the line containing the cursor.

eric ide

mercurial