QScintilla/Shell.py

branch
maintenance
changeset 6050
25a4ddb25ca4
parent 6004
548a9ba8c970
parent 6048
82ad8ec9548c
child 6097
bf18415da0c7
equal deleted inserted replaced
6005:6914826302ae 6050:25a4ddb25ca4
1 # -*- coding: utf-8 -*- 1 # -*- coding: utf-8 -*-
2 2
3 # Copyright (c) 2002 - 2017 Detlev Offenbach <detlev@die-offenbachs.de> 3 # Copyright (c) 2002 - 2018 Detlev Offenbach <detlev@die-offenbachs.de>
4 # 4 #
5 5
6 """ 6 """
7 Module implementing a graphical Python shell. 7 Module implementing a graphical Python shell.
8 """ 8 """
320 QsciScintilla.SCI_WORDLEFT: self.__QScintillaWordLeft, 320 QsciScintilla.SCI_WORDLEFT: self.__QScintillaWordLeft,
321 QsciScintilla.SCI_WORDRIGHT: self.__QScintillaWordRight, 321 QsciScintilla.SCI_WORDRIGHT: self.__QScintillaWordRight,
322 QsciScintilla.SCI_VCHOME: self.__QScintillaVCHome, 322 QsciScintilla.SCI_VCHOME: self.__QScintillaVCHome,
323 QsciScintilla.SCI_LINEEND: self.__QScintillaLineEnd, 323 QsciScintilla.SCI_LINEEND: self.__QScintillaLineEnd,
324 324
325 QsciScintilla.SCI_LINEUP: self.__QScintillaCursorCommand,
326 QsciScintilla.SCI_LINEDOWN: self.__QScintillaCursorCommand,
327 QsciScintilla.SCI_LINESCROLLUP: self.__QScintillaCursorCommand,
328 QsciScintilla.SCI_LINESCROLLDOWN: self.__QScintillaCursorCommand,
329
325 QsciScintilla.SCI_PAGEUP: self.__QScintillaAutoCompletionCommand, 330 QsciScintilla.SCI_PAGEUP: self.__QScintillaAutoCompletionCommand,
326 QsciScintilla.SCI_PAGEDOWN: self.__QScintillaAutoCompletionCommand, 331 QsciScintilla.SCI_PAGEDOWN: self.__QScintillaAutoCompletionCommand,
327 QsciScintilla.SCI_CANCEL: self.__QScintillaAutoCompletionCommand,
328 332
329 QsciScintilla.SCI_CHARLEFTEXTEND: self.__QScintillaCharLeftExtend, 333 QsciScintilla.SCI_CHARLEFTEXTEND: self.__QScintillaCharLeftExtend,
330 QsciScintilla.SCI_CHARRIGHTEXTEND: self.extendSelectionRight, 334 QsciScintilla.SCI_CHARRIGHTEXTEND: self.extendSelectionRight,
331 QsciScintilla.SCI_WORDLEFTEXTEND: self.__QScintillaWordLeftExtend, 335 QsciScintilla.SCI_WORDLEFTEXTEND: self.__QScintillaWordLeftExtend,
332 QsciScintilla.SCI_WORDRIGHTEXTEND: self.extendSelectionWordRight, 336 QsciScintilla.SCI_WORDRIGHTEXTEND: self.extendSelectionWordRight,
333 QsciScintilla.SCI_VCHOMEEXTEND: self.__QScintillaVCHomeExtend, 337 QsciScintilla.SCI_VCHOMEEXTEND: self.__QScintillaVCHomeExtend,
334 QsciScintilla.SCI_LINEENDEXTEND: self.extendSelectionToEOL, 338 QsciScintilla.SCI_LINEENDEXTEND: self.extendSelectionToEOL,
335 339
336 QsciScintilla.SCI_CANCEL: self.__QScintillaCancel, 340 QsciScintilla.SCI_CANCEL: self.__QScintillaCancel,
337 } 341 }
338 self.__setupCursorKeys() 342
343 self.__historyNavigateByCursor = \
344 Preferences.getShell("HistoryNavigateByCursor")
339 345
340 self.grabGesture(Qt.PinchGesture) 346 self.grabGesture(Qt.PinchGesture)
341
342 def __setupCursorKeys(self):
343 """
344 Private method to setup the cursor up and down mode.
345 """
346 if Preferences.getShell("HistoryNavigateByCursor"):
347 self.supportedEditorCommands.update({
348 QsciScintilla.SCI_LINEUP: self.__QScintillaHistoryUp,
349 QsciScintilla.SCI_LINEDOWN: self.__QScintillaHistoryDown,
350 QsciScintilla.SCI_LINESCROLLUP: self.__QScintillaLineUp,
351 QsciScintilla.SCI_LINESCROLLDOWN: self.__QScintillaLineDown,
352 })
353 else:
354 self.supportedEditorCommands.update({
355 QsciScintilla.SCI_LINEUP: self.__QScintillaLineUp,
356 QsciScintilla.SCI_LINEDOWN: self.__QScintillaLineDown,
357 QsciScintilla.SCI_LINESCROLLUP: self.__QScintillaHistoryUp,
358 QsciScintilla.SCI_LINESCROLLDOWN: self.__QScintillaHistoryDown,
359 })
360 347
361 def __showLanguageMenu(self): 348 def __showLanguageMenu(self):
362 """ 349 """
363 Private slot to prepare the language submenu. 350 Private slot to prepare the language submenu.
364 """ 351 """
964 Public slot to handle the paste action. 951 Public slot to handle the paste action.
965 """ 952 """
966 if self.__isCursorOnLastLine(): 953 if self.__isCursorOnLastLine():
967 line, col = self.getCursorPosition() 954 line, col = self.getCursorPosition()
968 lastLine = self.text(line) 955 lastLine = self.text(line)
956 if lastLine.startswith(sys.ps1):
957 lastLine = lastLine[len(sys.ps1):]
958 col -= len(sys.ps1)
959 prompt = sys.ps1
960 elif lastLine.startswith(sys.ps2):
961 lastLine = lastLine[len(sys.ps2):]
962 col -= len(sys.ps2)
963 prompt = sys.ps2
964 else:
965 prompt = ""
966 if col < 0:
967 col = 0
968 prompt = ""
969 969
970 # Remove if text is selected 970 # Remove if text is selected
971 if self.hasSelectedText(): 971 if self.hasSelectedText():
972 lineFrom, indexFrom, lineTo, indexTo = self.getSelection() 972 lineFrom, indexFrom, lineTo, indexTo = self.getSelection()
973 if self.text(lineFrom).startswith(sys.ps1): 973 if self.text(lineFrom).startswith(sys.ps1):
974 if indexFrom < len(sys.ps1): 974 indexFrom -= len(sys.ps1)
975 indexFrom = len(sys.ps1) 975 indexTo -= len(sys.ps1)
976 elif self.text(lineFrom).startswith(sys.ps2): 976 elif self.text(lineFrom).startswith(sys.ps2):
977 if indexFrom < len(sys.ps2): 977 indexFrom -= len(sys.ps2)
978 indexFrom = len(sys.ps2) 978 indexTo -= len(sys.ps2)
979 if indexFrom < 0:
980 indexFrom = 0
979 lastLine = lastLine[:indexFrom] + lastLine[indexTo:] 981 lastLine = lastLine[:indexFrom] + lastLine[indexTo:]
980 col = indexFrom 982 col = indexFrom
981 983
982 self.setCursorPosition(line, 0) 984 self.setCursorPosition(line, len(prompt))
983 self.deleteLineRight() 985 self.deleteLineRight()
984 986
985 lines = QApplication.clipboard().text() 987 lines = QApplication.clipboard().text()
986 lines = lastLine[:col] + lines + lastLine[col:] 988 lines = lastLine[:col] + lines + lastLine[col:]
987 self.executeLines(lines) 989 self.executeLines(lines)
1004 single commands 1006 single commands
1005 @type str 1007 @type str
1006 @param historyIndex history index to be set 1008 @param historyIndex history index to be set
1007 @type int 1009 @type int
1008 """ 1010 """
1009 for line in lines.splitlines(True): 1011 lines = lines.splitlines(True)
1012 if not lines:
1013 return
1014
1015 indentLen = self.__indentLength(lines[0])
1016 for line in lines:
1017 if line.startswith(sys.ps1):
1018 line = line[len(sys.ps1) + indentLen:]
1019 elif line.startswith(sys.ps2):
1020 line = line[len(sys.ps2) + indentLen:]
1021 else:
1022 line = line[indentLen:]
1023
1010 if line.endswith("\r\n"): 1024 if line.endswith("\r\n"):
1011 fullline = True 1025 fullline = True
1012 cmd = line[:-2] 1026 cmd = line[:-2]
1013 elif line.endswith("\r") or line.endswith("\n"): 1027 elif line.endswith("\r") or line.endswith("\n"):
1014 fullline = True 1028 fullline = True
1018 1032
1019 self.incrementalSearchActive = True 1033 self.incrementalSearchActive = True
1020 self.__insertTextAtEnd(line) 1034 self.__insertTextAtEnd(line)
1021 if fullline: 1035 if fullline:
1022 self.incrementalSearchActive = False 1036 self.incrementalSearchActive = False
1023 if cmd.startswith(sys.ps1):
1024 cmd = cmd[len(sys.ps1):]
1025 elif cmd.startswith(sys.ps2):
1026 cmd = cmd[len(sys.ps2):]
1027 1037
1028 self.__executeCommand(cmd, historyIndex=historyIndex) 1038 self.__executeCommand(cmd, historyIndex=historyIndex)
1029 if self.interruptCommandExecution: 1039 if self.interruptCommandExecution:
1030 self.__executeCommand("") 1040 self.__executeCommand("")
1031 break 1041 break
1032 1042
1043 def __indentLength(self, line):
1044 """
1045 Private method to determine the indentation length of the given line.
1046
1047 @param line line to determine the indentation length for
1048 @type str
1049 @return indentation length
1050 @rtype int
1051 """
1052 if line.startswith(sys.ps1):
1053 line = line[len(sys.ps1):]
1054 # If line starts with sys.ps2 or neither don't manipulate the line.
1055 indentLen = len(line) - len(line.lstrip())
1056 return indentLen
1057
1033 def __clearCurrentLine(self): 1058 def __clearCurrentLine(self):
1034 """ 1059 """
1035 Private method to clear the line containing the cursor. 1060 Private method to clear the line containing the cursor.
1036 """ 1061 """
1037 line, col = self.getCursorPosition() 1062 line, col = self.getCursorPosition()
1428 """ 1453 """
1429 if self.isListActive(): 1454 if self.isListActive():
1430 self.SendScintilla(cmd) 1455 self.SendScintilla(cmd)
1431 elif self.__isCursorOnLastLine(): 1456 elif self.__isCursorOnLastLine():
1432 self.moveCursorToEOL() 1457 self.moveCursorToEOL()
1458
1459 def __QScintillaCursorCommand(self, cmd):
1460 """
1461 Private method to handle the cursor commands.
1462
1463 @param cmd QScintilla command
1464 """
1465 if self.isListActive() or self.isCallTipActive():
1466 if cmd in (QsciScintilla.SCI_LINEUP, QsciScintilla.SCI_LINEDOWN):
1467 self.SendScintilla(cmd)
1468 else:
1469 if self.__historyNavigateByCursor:
1470 if cmd == QsciScintilla.SCI_LINEUP:
1471 self.__QScintillaHistoryUp(cmd)
1472 elif cmd == QsciScintilla.SCI_LINEDOWN:
1473 self.__QScintillaHistoryDown(cmd)
1474 elif cmd == QsciScintilla.SCI_LINESCROLLUP:
1475 self.__QScintillaLineUp(cmd)
1476 elif cmd == QsciScintilla.SCI_LINESCROLLDOWN:
1477 self.__QScintillaLineDown(cmd)
1478 else:
1479 if cmd == QsciScintilla.SCI_LINEUP:
1480 self.__QScintillaLineUp(cmd)
1481 elif cmd == QsciScintilla.SCI_LINEDOWN:
1482 self.__QScintillaLineDown(cmd)
1483 elif cmd == QsciScintilla.SCI_LINESCROLLUP:
1484 self.__QScintillaHistoryUp(cmd)
1485 elif cmd == QsciScintilla.SCI_LINESCROLLDOWN:
1486 self.__QScintillaHistoryDown(cmd)
1433 1487
1434 def __QScintillaLineUp(self, cmd): 1488 def __QScintillaLineUp(self, cmd):
1435 """ 1489 """
1436 Private method to handle the cursor up command. 1490 Private method to handle the cursor up command.
1437 1491
1532 1586
1533 def __QScintillaCancel(self): 1587 def __QScintillaCancel(self):
1534 """ 1588 """
1535 Private method to handle the ESC command. 1589 Private method to handle the ESC command.
1536 """ 1590 """
1537 if self.incrementalSearchActive: 1591 if self.isListActive() or self.isCallTipActive():
1538 self.__resetIncrementalHistorySearch() 1592 self.SendScintilla(QsciScintilla.SCI_CANCEL)
1539 self.__insertHistory("") 1593 else:
1594 if self.incrementalSearchActive:
1595 self.__resetIncrementalHistorySearch()
1596 self.__insertHistory("")
1540 1597
1541 def __QScintillaCharLeftExtend(self): 1598 def __QScintillaCharLeftExtend(self):
1542 """ 1599 """
1543 Private method to handle the Extend Selection Left command. 1600 Private method to handle the Extend Selection Left command.
1544 """ 1601 """
1829 self.__historyStyle = Preferences.getShell("HistoryStyle") 1886 self.__historyStyle = Preferences.getShell("HistoryStyle")
1830 self.__historyWrap = Preferences.getShell("HistoryWrap") 1887 self.__historyWrap = Preferences.getShell("HistoryWrap")
1831 self.__setHistoryIndex() 1888 self.__setHistoryIndex()
1832 if not self.__windowed: 1889 if not self.__windowed:
1833 self.hmenu.menuAction().setEnabled(self.isHistoryEnabled()) 1890 self.hmenu.menuAction().setEnabled(self.isHistoryEnabled())
1834 self.__setupCursorKeys() 1891 self.__historyNavigateByCursor = \
1892 Preferences.getShell("HistoryNavigateByCursor")
1835 self.historyStyleChanged.emit(self.__historyStyle) 1893 self.historyStyleChanged.emit(self.__historyStyle)
1836 1894
1837 # do stdout /stderr stuff 1895 # do stdout /stderr stuff
1838 showStdOutErr = Preferences.getShell("ShowStdOutErr") 1896 showStdOutErr = Preferences.getShell("ShowStdOutErr")
1839 if self.__showStdOutErr != showStdOutErr: 1897 if self.__showStdOutErr != showStdOutErr:

eric ide

mercurial