QScintilla/Shell.py

changeset 5799
e87f52c0374a
parent 5798
e4f9552f7f93
child 5800
c3379bf35654
--- a/QScintilla/Shell.py	Sun Jul 09 19:44:33 2017 +0200
+++ b/QScintilla/Shell.py	Mon Jul 10 18:24:35 2017 +0200
@@ -106,11 +106,11 @@
     
     @signal searchStringFound(bool) emitted to indicate the search
         result
-    @signal historyStyleChanged(int) emitted to indicate a change of
-        the history style
+    @signal historyStyleChanged(ShellHistoryStyle) emitted to indicate a
+        change of the history style
     """
     searchStringFound = pyqtSignal(bool)
-    historyStyleChanged = pyqtSignal(int)
+    historyStyleChanged = pyqtSignal(ShellHistoryStyle)
     
     def __init__(self, dbs, vm, windowedVariant, parent=None):
         """
@@ -220,18 +220,19 @@
         self.lexer_ = None
         self.completionText = ""
         
+        self.clientType = ''
+        
         # Initialize history
         self.__historyLists = {}
         self.__maxHistoryEntries = Preferences.getShell("MaxHistoryEntries")
         self.__historyStyle = Preferences.getShell("HistoryStyle")
+        self.__historyWrap = Preferences.getShell("HistoryWrap")
         self.__history = []
         self.__setHistoryIndex()
         # remove obsolete shell histories (Python and Ruby)
         for clientType in ["Python", "Ruby"]:
             Preferences.Prefs.settings.remove("Shell/Histories/" + clientType)
         
-        self.clientType = ''
-        
         # clear QScintilla defined keyboard commands
         # we do our own handling through the view manager
         self.clearAlternateKeys()
@@ -316,10 +317,6 @@
             QsciScintilla.SCI_WORDRIGHT: self.__QScintillaWordRight,
             QsciScintilla.SCI_VCHOME: self.__QScintillaVCHome,
             QsciScintilla.SCI_LINEEND: self.__QScintillaLineEnd,
-            QsciScintilla.SCI_LINEUP: self.__QScintillaCommand,
-            QsciScintilla.SCI_LINEDOWN: self.__QScintillaCommand,
-            QsciScintilla.SCI_LINESCROLLUP: self.__QScintillaHistoryUp,
-            QsciScintilla.SCI_LINESCROLLDOWN: self.__QScintillaHistoryDown,
             
             QsciScintilla.SCI_PAGEUP: self.__QScintillaAutoCompletionCommand,
             QsciScintilla.SCI_PAGEDOWN: self.__QScintillaAutoCompletionCommand,
@@ -334,9 +331,29 @@
             
             QsciScintilla.SCI_CANCEL: self.__QScintillaCancel,
         }
+        self.__setupCursorKeys()
         
         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.
@@ -594,9 +611,12 @@
         """
         if index is None:
             # determine based on history style
-            if self.__historyStyle == ShellHistoryStyle.WindowsStyle:
+            if self.clientType and \
+                    self.__historyStyle == ShellHistoryStyle.WindowsStyle:
                 idx = int(Preferences.Prefs.settings.value(
                     "Shell/HistoryIndexes/" + self.clientType, -1))
+                if idx >= len(self.__history):
+                    idx = -1
                 self.__histidx = idx
             else:
                 self.__histidx = -1
@@ -618,6 +638,15 @@
         """
         return (0 <= self.__histidx < len(self.__history))
     
+    def getHistoryIndex(self):
+        """
+        Public method to get the current value of the history index.
+        
+        @return history index
+        @rtype int
+        """
+        return self.__histidx
+    
     def loadHistory(self, clientType):
         """
         Public method to load the history for the given client type.
@@ -963,12 +992,15 @@
         lines = QApplication.clipboard().text(QClipboard.Selection)
         self.executeLines(lines)
         
-    def executeLines(self, lines):
+    def executeLines(self, lines, historyIndex=None):
         """
         Public method to execute a set of lines as multiple commands.
         
-        @param lines multiple lines of text to be executed as single
-            commands (string)
+        @param lines multiple lines of text to be executed as
+            single commands
+        @type str
+        @param historyIndex history index to be set
+        @type int
         """
         for line in lines.splitlines(True):
             if line.endswith("\r\n"):
@@ -989,7 +1021,7 @@
                 elif cmd.startswith(sys.ps2):
                     cmd = cmd[len(sys.ps2):]
                 
-                self.__executeCommand(cmd)
+                self.__executeCommand(cmd, historyIndex=historyIndex)
                 if self.interruptCommandExecution:
                     self.__executeCommand("")
                     break
@@ -1394,10 +1426,26 @@
             self.SendScintilla(cmd)
         elif self.__isCursorOnLastLine():
             self.moveCursorToEOL()
+    
+    def __QScintillaLineUp(self, cmd):
+        """
+        Private method to handle the cursor up command.
         
+        @param cmd QScintilla command
+        """
+        self.SendScintilla(QsciScintilla.SCI_LINEUP)
+    
+    def __QScintillaLineDown(self, cmd):
+        """
+        Private method to handle the cursor down command.
+        
+        @param cmd QScintilla command
+        """
+        self.SendScintilla(QsciScintilla.SCI_LINEDOWN)
+    
     def __QScintillaHistoryUp(self, cmd):
         """
-        Private method to handle the Ctrl+Up key.
+        Private method to handle the history up command.
         
         @param cmd QScintilla command
         """
@@ -1423,16 +1471,24 @@
                         self.incrementalSearchString = buf
                         self.__useHistory()
             else:
-                if self.__histidx < 0:
-                    # wrap around
-                    self.__setHistoryIndex(index=len(self.__history) - 1)
+                if self.__historyWrap:
+                    if self.__histidx < 0:
+                        # wrap around
+                        self.__setHistoryIndex(index=len(self.__history) - 1)
+                    else:
+                        self.__setHistoryIndex(index=self.__histidx - 1)
+                    self.__useHistory()
                 else:
-                    self.__setHistoryIndex(index=self.__histidx - 1)
-                self.__useHistory()
+                    if self.__histidx < 0:
+                        self.__setHistoryIndex(index=len(self.__history) - 1)
+                        self.__useHistory()
+                    elif self.__histidx > 0:
+                        self.__setHistoryIndex(index=self.__histidx - 1)
+                        self.__useHistory()
     
     def __QScintillaHistoryDown(self, cmd):
         """
-        Private method to handle the Ctrl+Down key.
+        Private method to handle the history down command.
         
         @param cmd QScintilla command
         """
@@ -1458,12 +1514,17 @@
                         self.incrementalSearchString = buf
                         self.__useHistory()
             else:
-                if self.__histidx >= len(self.__history) - 1:
-                    # wrap around
-                    self.__setHistoryIndex(index=0)
+                if self.__historyWrap:
+                    if self.__histidx >= len(self.__history) - 1:
+                        # wrap around
+                        self.__setHistoryIndex(index=0)
+                    else:
+                        self.__setHistoryIndex(index=self.__histidx + 1)
+                    self.__useHistory()
                 else:
-                    self.__setHistoryIndex(index=self.__histidx + 1)
-                self.__useHistory()
+                    if self.__isHistoryIndexValid():
+                        self.__setHistoryIndex(index=self.__histidx + 1)
+                        self.__useHistory()
     
     def __QScintillaCancel(self):
         """
@@ -1511,11 +1572,14 @@
         if self.isListActive() or self.isCallTipActive():
             self.SendScintilla(cmd)
         
-    def __executeCommand(self, cmd):
+    def __executeCommand(self, cmd, historyIndex=None):
         """
         Private slot to execute a command.
         
-        @param cmd command to be executed by debug client (string)
+        @param cmd command to be executed by debug client
+        @type str
+        @param historyIndex history index to be set
+        @type int
         """
         if not self.inRawMode:
             self.inCommandExecution = True
@@ -1523,16 +1587,23 @@
             if not cmd:
                 # make sure cmd is a string
                 cmd = ''
-            # TODO: change according to history style
-            #       Linux - append (i.e. keep as is)
-            #       Windows - modify current entry if index not at end, add otherwise
+            
+            # History Handling
             if self.isHistoryEnabled():
                 if cmd != "" and (
                         len(self.__history) == 0 or self.__history[-1] != cmd):
                     if len(self.__history) == self.__maxHistoryEntries:
                         del self.__history[0]
                     self.__history.append(cmd)
-                self.__histidx = -1
+                if self.__historyStyle == ShellHistoryStyle.LinuxStyle:
+                    self.__setHistoryIndex(index=-1)
+                elif self.__historyStyle == ShellHistoryStyle.WindowsStyle:
+                    if historyIndex is None:
+                        if cmd != self.__history[self.__histidx - 1]:
+                            self.__setHistoryIndex(index=-1)
+                    else:
+                        self.__setHistoryIndex(historyIndex)
+            
             if cmd.startswith('start '):
                 if not self.passive:
                     cmdList = cmd.split(None, 1)
@@ -1751,10 +1822,12 @@
             self.__historyLists[key] = \
                 self.__historyLists[key][-self.__maxHistoryEntries:]
         self.__historyStyle = Preferences.getShell("HistoryStyle")
+        self.__historyWrap = Preferences.getShell("HistoryWrap")
         self.__setHistoryIndex()
-        self.historyStyleChanged.emit(self.__historyStyle)
         if not self.__windowed:
             self.hmenu.menuAction().setEnabled(self.isHistoryEnabled())
+        self.__setupCursorKeys()
+        self.historyStyleChanged.emit(self.__historyStyle)
         
         # do stdout /stderr stuff
         showStdOutErr = Preferences.getShell("ShowStdOutErr")

eric ide

mercurial