QScintilla/Shell.py

branch
maintenance
changeset 6050
25a4ddb25ca4
parent 6004
548a9ba8c970
parent 6048
82ad8ec9548c
child 6097
bf18415da0c7
--- a/QScintilla/Shell.py	Sat Dec 02 12:40:52 2017 +0100
+++ b/QScintilla/Shell.py	Sun Dec 31 18:27:40 2017 +0100
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 
-# Copyright (c) 2002 - 2017 Detlev Offenbach <detlev@die-offenbachs.de>
+# Copyright (c) 2002 - 2018 Detlev Offenbach <detlev@die-offenbachs.de>
 #
 
 """
@@ -322,9 +322,13 @@
             QsciScintilla.SCI_VCHOME: self.__QScintillaVCHome,
             QsciScintilla.SCI_LINEEND: self.__QScintillaLineEnd,
             
+            QsciScintilla.SCI_LINEUP: self.__QScintillaCursorCommand,
+            QsciScintilla.SCI_LINEDOWN: self.__QScintillaCursorCommand,
+            QsciScintilla.SCI_LINESCROLLUP: self.__QScintillaCursorCommand,
+            QsciScintilla.SCI_LINESCROLLDOWN: self.__QScintillaCursorCommand,
+            
             QsciScintilla.SCI_PAGEUP: self.__QScintillaAutoCompletionCommand,
             QsciScintilla.SCI_PAGEDOWN: self.__QScintillaAutoCompletionCommand,
-            QsciScintilla.SCI_CANCEL: self.__QScintillaAutoCompletionCommand,
             
             QsciScintilla.SCI_CHARLEFTEXTEND: self.__QScintillaCharLeftExtend,
             QsciScintilla.SCI_CHARRIGHTEXTEND: self.extendSelectionRight,
@@ -335,29 +339,12 @@
             
             QsciScintilla.SCI_CANCEL: self.__QScintillaCancel,
         }
-        self.__setupCursorKeys()
+        
+        self.__historyNavigateByCursor = \
+            Preferences.getShell("HistoryNavigateByCursor")
         
         self.grabGesture(Qt.PinchGesture)
     
-    def __setupCursorKeys(self):
-        """
-        Private method to setup the cursor up and down mode.
-        """
-        if Preferences.getShell("HistoryNavigateByCursor"):
-            self.supportedEditorCommands.update({
-                QsciScintilla.SCI_LINEUP: self.__QScintillaHistoryUp,
-                QsciScintilla.SCI_LINEDOWN: self.__QScintillaHistoryDown,
-                QsciScintilla.SCI_LINESCROLLUP: self.__QScintillaLineUp,
-                QsciScintilla.SCI_LINESCROLLDOWN: self.__QScintillaLineDown,
-            })
-        else:
-            self.supportedEditorCommands.update({
-                QsciScintilla.SCI_LINEUP: self.__QScintillaLineUp,
-                QsciScintilla.SCI_LINEDOWN: self.__QScintillaLineDown,
-                QsciScintilla.SCI_LINESCROLLUP: self.__QScintillaHistoryUp,
-                QsciScintilla.SCI_LINESCROLLDOWN: self.__QScintillaHistoryDown,
-            })
-    
     def __showLanguageMenu(self):
         """
         Private slot to prepare the language submenu.
@@ -966,20 +953,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 +1008,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 +1034,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.
@@ -1431,6 +1456,35 @@
         elif self.__isCursorOnLastLine():
             self.moveCursorToEOL()
     
+    def __QScintillaCursorCommand(self, cmd):
+        """
+        Private method to handle the cursor commands.
+        
+        @param cmd QScintilla command
+        """
+        if self.isListActive() or self.isCallTipActive():
+            if cmd in (QsciScintilla.SCI_LINEUP, QsciScintilla.SCI_LINEDOWN):
+                self.SendScintilla(cmd)
+        else:
+            if self.__historyNavigateByCursor:
+                if cmd == QsciScintilla.SCI_LINEUP:
+                    self.__QScintillaHistoryUp(cmd)
+                elif cmd == QsciScintilla.SCI_LINEDOWN:
+                    self.__QScintillaHistoryDown(cmd)
+                elif cmd == QsciScintilla.SCI_LINESCROLLUP:
+                    self.__QScintillaLineUp(cmd)
+                elif cmd == QsciScintilla.SCI_LINESCROLLDOWN:
+                    self.__QScintillaLineDown(cmd)
+            else:
+                if cmd == QsciScintilla.SCI_LINEUP:
+                    self.__QScintillaLineUp(cmd)
+                elif cmd == QsciScintilla.SCI_LINEDOWN:
+                    self.__QScintillaLineDown(cmd)
+                elif cmd == QsciScintilla.SCI_LINESCROLLUP:
+                    self.__QScintillaHistoryUp(cmd)
+                elif cmd == QsciScintilla.SCI_LINESCROLLDOWN:
+                    self.__QScintillaHistoryDown(cmd)
+    
     def __QScintillaLineUp(self, cmd):
         """
         Private method to handle the cursor up command.
@@ -1534,9 +1588,12 @@
         """
         Private method to handle the ESC command.
         """
-        if self.incrementalSearchActive:
-            self.__resetIncrementalHistorySearch()
-        self.__insertHistory("")
+        if self.isListActive() or self.isCallTipActive():
+            self.SendScintilla(QsciScintilla.SCI_CANCEL)
+        else:
+            if self.incrementalSearchActive:
+                self.__resetIncrementalHistorySearch()
+            self.__insertHistory("")
     
     def __QScintillaCharLeftExtend(self):
         """
@@ -1831,7 +1888,8 @@
         self.__setHistoryIndex()
         if not self.__windowed:
             self.hmenu.menuAction().setEnabled(self.isHistoryEnabled())
-        self.__setupCursorKeys()
+        self.__historyNavigateByCursor = \
+            Preferences.getShell("HistoryNavigateByCursor")
         self.historyStyleChanged.emit(self.__historyStyle)
         
         # do stdout /stderr stuff

eric ide

mercurial