QScintilla/Terminal.py

changeset 945
8cd4d08fa9f6
parent 943
1246bd8280a6
child 1112
8a7d1b9d18db
equal deleted inserted replaced
944:1b59c4ba121e 945:8cd4d08fa9f6
24 import Utilities 24 import Utilities
25 25
26 import UI.PixmapCache 26 import UI.PixmapCache
27 27
28 from .ShellHistoryDialog import ShellHistoryDialog 28 from .ShellHistoryDialog import ShellHistoryDialog
29
29 30
30 class Terminal(QsciScintillaCompat): 31 class Terminal(QsciScintillaCompat):
31 """ 32 """
32 Class implementing a simple terminal based on QScintilla. 33 Class implementing a simple terminal based on QScintilla.
33 34
34 A user can enter commands that are executed by a shell process. 35 A user can enter commands that are executed by a shell process.
35 """ 36 """
36 def __init__(self, vm, parent = None): 37 def __init__(self, vm, parent=None):
37 """ 38 """
38 Constructor 39 Constructor
39 40
40 @param vm reference to the viewmanager object 41 @param vm reference to the viewmanager object
41 @param parent parent widget (QWidget) 42 @param parent parent widget (QWidget)
126 127
127 self.incrementalSearchString = "" 128 self.incrementalSearchString = ""
128 self.incrementalSearchActive = False 129 self.incrementalSearchActive = False
129 130
130 self.supportedEditorCommands = { 131 self.supportedEditorCommands = {
131 QsciScintilla.SCI_LINEDELETE : self.__clearCurrentLine, 132 QsciScintilla.SCI_LINEDELETE: self.__clearCurrentLine,
132 QsciScintilla.SCI_NEWLINE : self.__QScintillaNewline, 133 QsciScintilla.SCI_NEWLINE: self.__QScintillaNewline,
133 134
134 QsciScintilla.SCI_DELETEBACK : self.__QScintillaDeleteBack, 135 QsciScintilla.SCI_DELETEBACK: self.__QScintillaDeleteBack,
135 QsciScintilla.SCI_CLEAR : self.__QScintillaDelete, 136 QsciScintilla.SCI_CLEAR: self.__QScintillaDelete,
136 QsciScintilla.SCI_DELWORDLEFT : self.__QScintillaDeleteWordLeft, 137 QsciScintilla.SCI_DELWORDLEFT: self.__QScintillaDeleteWordLeft,
137 QsciScintilla.SCI_DELWORDRIGHT : self.__QScintillaDeleteWordRight, 138 QsciScintilla.SCI_DELWORDRIGHT: self.__QScintillaDeleteWordRight,
138 QsciScintilla.SCI_DELLINELEFT : self.__QScintillaDeleteLineLeft, 139 QsciScintilla.SCI_DELLINELEFT: self.__QScintillaDeleteLineLeft,
139 QsciScintilla.SCI_DELLINERIGHT : self.__QScintillaDeleteLineRight, 140 QsciScintilla.SCI_DELLINERIGHT: self.__QScintillaDeleteLineRight,
140 141
141 QsciScintilla.SCI_CHARLEFT : self.__QScintillaCharLeft, 142 QsciScintilla.SCI_CHARLEFT: self.__QScintillaCharLeft,
142 QsciScintilla.SCI_CHARRIGHT : self.__QScintillaCharRight, 143 QsciScintilla.SCI_CHARRIGHT: self.__QScintillaCharRight,
143 QsciScintilla.SCI_WORDLEFT : self.__QScintillaWordLeft, 144 QsciScintilla.SCI_WORDLEFT: self.__QScintillaWordLeft,
144 QsciScintilla.SCI_WORDRIGHT : self.__QScintillaWordRight, 145 QsciScintilla.SCI_WORDRIGHT: self.__QScintillaWordRight,
145 QsciScintilla.SCI_VCHOME : self.__QScintillaVCHome, 146 QsciScintilla.SCI_VCHOME: self.__QScintillaVCHome,
146 QsciScintilla.SCI_LINEEND : self.__QScintillaLineEnd, 147 QsciScintilla.SCI_LINEEND: self.__QScintillaLineEnd,
147 QsciScintilla.SCI_LINEUP : self.__QScintillaLineUp, 148 QsciScintilla.SCI_LINEUP: self.__QScintillaLineUp,
148 QsciScintilla.SCI_LINEDOWN : self.__QScintillaLineDown, 149 QsciScintilla.SCI_LINEDOWN: self.__QScintillaLineDown,
149 150
150 QsciScintilla.SCI_CHARLEFTEXTEND : self.__QScintillaCharLeftExtend, 151 QsciScintilla.SCI_CHARLEFTEXTEND: self.__QScintillaCharLeftExtend,
151 QsciScintilla.SCI_CHARRIGHTEXTEND : self.extendSelectionRight, 152 QsciScintilla.SCI_CHARRIGHTEXTEND: self.extendSelectionRight,
152 QsciScintilla.SCI_WORDLEFTEXTEND : self.__QScintillaWordLeftExtend, 153 QsciScintilla.SCI_WORDLEFTEXTEND: self.__QScintillaWordLeftExtend,
153 QsciScintilla.SCI_WORDRIGHTEXTEND : self.extendSelectionWordRight, 154 QsciScintilla.SCI_WORDRIGHTEXTEND: self.extendSelectionWordRight,
154 QsciScintilla.SCI_VCHOMEEXTEND : self.__QScintillaVCHomeExtend, 155 QsciScintilla.SCI_VCHOMEEXTEND: self.__QScintillaVCHomeExtend,
155 QsciScintilla.SCI_LINEENDEXTEND : self.extendSelectionToEOL, 156 QsciScintilla.SCI_LINEENDEXTEND: self.extendSelectionToEOL,
156 } 157 }
157 158
158 self.__ioEncoding = Preferences.getSystem("IOEncoding") 159 self.__ioEncoding = Preferences.getSystem("IOEncoding")
159 160
160 self.__process = QProcess() 161 self.__process = QProcess()
175 176
176 def __readOutput(self): 177 def __readOutput(self):
177 """ 178 """
178 Private method to process the output of the shell. 179 Private method to process the output of the shell.
179 """ 180 """
180 output = str(self.__process.readAllStandardOutput(), 181 output = str(self.__process.readAllStandardOutput(),
181 self.__ioEncoding, 'replace') 182 self.__ioEncoding, 'replace')
182 self.__write(self.ansi_re.sub("", output)) 183 self.__write(self.ansi_re.sub("", output))
183 self.__lastPos = self.__getEndPos() 184 self.__lastPos = self.__getEndPos()
184 185
185 def __started(self): 186 def __started(self):
223 """ 224 """
224 self.__send(chr(cmd)) 225 self.__send(chr(cmd))
225 226
226 def closeTerminal(self): 227 def closeTerminal(self):
227 """ 228 """
228 Public method to shutdown the terminal. 229 Public method to shutdown the terminal.
229 """ 230 """
230 self.__stopShell() 231 self.__stopShell()
231 self.saveHistory() 232 self.saveHistory()
232 233
233 def __bindLexer(self): 234 def __bindLexer(self):
586 buf = self.selectedText() 587 buf = self.selectedText()
587 self.setCursorPosition(line, col) # select nothin 588 self.setCursorPosition(line, col) # select nothin
588 self.insert('\n') 589 self.insert('\n')
589 self.__executeCommand(buf) 590 self.__executeCommand(buf)
590 591
591 def __QScintillaLeftCommand(self, method, allLinesAllowed = False): 592 def __QScintillaLeftCommand(self, method, allLinesAllowed=False):
592 """ 593 """
593 Private method to handle a QScintilla command working to the left. 594 Private method to handle a QScintilla command working to the left.
594 595
595 @param method shell method to execute 596 @param method shell method to execute
596 """ 597 """
668 """ 669 """
669 line, col = self.__getEndPos() 670 line, col = self.__getEndPos()
670 buf = self.text(line)[self.__lastPos[1]:] 671 buf = self.text(line)[self.__lastPos[1]:]
671 if buf and self.incrementalSearchActive: 672 if buf and self.incrementalSearchActive:
672 if self.incrementalSearchString: 673 if self.incrementalSearchString:
673 idx = self.__rsearchHistory(self.incrementalSearchString, 674 idx = self.__rsearchHistory(self.incrementalSearchString,
674 self.histidx) 675 self.histidx)
675 if idx >= 0: 676 if idx >= 0:
676 self.histidx = idx 677 self.histidx = idx
677 self.__useHistory() 678 self.__useHistory()
678 else: 679 else:
780 self.setSelection(self.prline, self.prcol,\ 781 self.setSelection(self.prline, self.prcol,\
781 self.prline, self.lineLength(self.prline)) 782 self.prline, self.lineLength(self.prline))
782 self.removeSelectedText() 783 self.removeSelectedText()
783 self.__insertText(cmd) 784 self.__insertText(cmd)
784 785
785 def __searchHistory(self, txt, startIdx = -1): 786 def __searchHistory(self, txt, startIdx=-1):
786 """ 787 """
787 Private method used to search the history. 788 Private method used to search the history.
788 789
789 @param txt text to match at the beginning (string) 790 @param txt text to match at the beginning (string)
790 @param startIdx index to start search from (integer) 791 @param startIdx index to start search from (integer)
797 while idx < len(self.history) and \ 798 while idx < len(self.history) and \
798 not self.history[idx].startswith(txt): 799 not self.history[idx].startswith(txt):
799 idx += 1 800 idx += 1
800 return idx 801 return idx
801 802
802 def __rsearchHistory(self, txt, startIdx = -1): 803 def __rsearchHistory(self, txt, startIdx=-1):
803 """ 804 """
804 Private method used to reverse search the history. 805 Private method used to reverse search the history.
805 806
806 @param txt text to match at the beginning (string) 807 @param txt text to match at the beginning (string)
807 @param startIdx index to start search from (integer) 808 @param startIdx index to start search from (integer)
814 while idx >= 0 and \ 815 while idx >= 0 and \
815 not self.history[idx].startswith(txt): 816 not self.history[idx].startswith(txt):
816 idx -= 1 817 idx -= 1
817 return idx 818 return idx
818 819
819 def contextMenuEvent(self,ev): 820 def contextMenuEvent(self, ev):
820 """ 821 """
821 Reimplemented to show our own context menu. 822 Reimplemented to show our own context menu.
822 823
823 @param ev context menu event (QContextMenuEvent) 824 @param ev context menu event (QContextMenuEvent)
824 """ 825 """
932 l = len(txt) 933 l = len(txt)
933 line, col = self.getCursorPosition() 934 line, col = self.getCursorPosition()
934 self.insertAt(txt, line, col) 935 self.insertAt(txt, line, col)
935 if re.search(self.linesepRegExp, txt) is not None: 936 if re.search(self.linesepRegExp, txt) is not None:
936 line += 1 937 line += 1
937 self.setCursorPosition(line, col + l) 938 self.setCursorPosition(line, col + l)
938 939
939 def __configure(self): 940 def __configure(self):
940 """ 941 """
941 Private method to open the configuration dialog. 942 Private method to open the configuration dialog.
942 """ 943 """

eric ide

mercurial