QScintilla/Shell.py

changeset 5799
e87f52c0374a
parent 5798
e4f9552f7f93
child 5800
c3379bf35654
equal deleted inserted replaced
5798:e4f9552f7f93 5799:e87f52c0374a
104 A user can enter commands that are executed in the remote 104 A user can enter commands that are executed in the remote
105 Python interpreter. 105 Python interpreter.
106 106
107 @signal searchStringFound(bool) emitted to indicate the search 107 @signal searchStringFound(bool) emitted to indicate the search
108 result 108 result
109 @signal historyStyleChanged(int) emitted to indicate a change of 109 @signal historyStyleChanged(ShellHistoryStyle) emitted to indicate a
110 the history style 110 change of the history style
111 """ 111 """
112 searchStringFound = pyqtSignal(bool) 112 searchStringFound = pyqtSignal(bool)
113 historyStyleChanged = pyqtSignal(int) 113 historyStyleChanged = pyqtSignal(ShellHistoryStyle)
114 114
115 def __init__(self, dbs, vm, windowedVariant, parent=None): 115 def __init__(self, dbs, vm, windowedVariant, parent=None):
116 """ 116 """
117 Constructor 117 Constructor
118 118
218 self.prcol = 0 218 self.prcol = 0
219 self.inDragDrop = False 219 self.inDragDrop = False
220 self.lexer_ = None 220 self.lexer_ = None
221 self.completionText = "" 221 self.completionText = ""
222 222
223 self.clientType = ''
224
223 # Initialize history 225 # Initialize history
224 self.__historyLists = {} 226 self.__historyLists = {}
225 self.__maxHistoryEntries = Preferences.getShell("MaxHistoryEntries") 227 self.__maxHistoryEntries = Preferences.getShell("MaxHistoryEntries")
226 self.__historyStyle = Preferences.getShell("HistoryStyle") 228 self.__historyStyle = Preferences.getShell("HistoryStyle")
229 self.__historyWrap = Preferences.getShell("HistoryWrap")
227 self.__history = [] 230 self.__history = []
228 self.__setHistoryIndex() 231 self.__setHistoryIndex()
229 # remove obsolete shell histories (Python and Ruby) 232 # remove obsolete shell histories (Python and Ruby)
230 for clientType in ["Python", "Ruby"]: 233 for clientType in ["Python", "Ruby"]:
231 Preferences.Prefs.settings.remove("Shell/Histories/" + clientType) 234 Preferences.Prefs.settings.remove("Shell/Histories/" + clientType)
232
233 self.clientType = ''
234 235
235 # clear QScintilla defined keyboard commands 236 # clear QScintilla defined keyboard commands
236 # we do our own handling through the view manager 237 # we do our own handling through the view manager
237 self.clearAlternateKeys() 238 self.clearAlternateKeys()
238 self.clearKeys() 239 self.clearKeys()
314 QsciScintilla.SCI_CHARRIGHT: self.__QScintillaCharRight, 315 QsciScintilla.SCI_CHARRIGHT: self.__QScintillaCharRight,
315 QsciScintilla.SCI_WORDLEFT: self.__QScintillaWordLeft, 316 QsciScintilla.SCI_WORDLEFT: self.__QScintillaWordLeft,
316 QsciScintilla.SCI_WORDRIGHT: self.__QScintillaWordRight, 317 QsciScintilla.SCI_WORDRIGHT: self.__QScintillaWordRight,
317 QsciScintilla.SCI_VCHOME: self.__QScintillaVCHome, 318 QsciScintilla.SCI_VCHOME: self.__QScintillaVCHome,
318 QsciScintilla.SCI_LINEEND: self.__QScintillaLineEnd, 319 QsciScintilla.SCI_LINEEND: self.__QScintillaLineEnd,
319 QsciScintilla.SCI_LINEUP: self.__QScintillaCommand,
320 QsciScintilla.SCI_LINEDOWN: self.__QScintillaCommand,
321 QsciScintilla.SCI_LINESCROLLUP: self.__QScintillaHistoryUp,
322 QsciScintilla.SCI_LINESCROLLDOWN: self.__QScintillaHistoryDown,
323 320
324 QsciScintilla.SCI_PAGEUP: self.__QScintillaAutoCompletionCommand, 321 QsciScintilla.SCI_PAGEUP: self.__QScintillaAutoCompletionCommand,
325 QsciScintilla.SCI_PAGEDOWN: self.__QScintillaAutoCompletionCommand, 322 QsciScintilla.SCI_PAGEDOWN: self.__QScintillaAutoCompletionCommand,
326 QsciScintilla.SCI_CANCEL: self.__QScintillaAutoCompletionCommand, 323 QsciScintilla.SCI_CANCEL: self.__QScintillaAutoCompletionCommand,
327 324
332 QsciScintilla.SCI_VCHOMEEXTEND: self.__QScintillaVCHomeExtend, 329 QsciScintilla.SCI_VCHOMEEXTEND: self.__QScintillaVCHomeExtend,
333 QsciScintilla.SCI_LINEENDEXTEND: self.extendSelectionToEOL, 330 QsciScintilla.SCI_LINEENDEXTEND: self.extendSelectionToEOL,
334 331
335 QsciScintilla.SCI_CANCEL: self.__QScintillaCancel, 332 QsciScintilla.SCI_CANCEL: self.__QScintillaCancel,
336 } 333 }
334 self.__setupCursorKeys()
337 335
338 self.grabGesture(Qt.PinchGesture) 336 self.grabGesture(Qt.PinchGesture)
339 337
338 def __setupCursorKeys(self):
339 """
340 Private method to setup the cursor up and down mode.
341 """
342 if Preferences.getShell("HistoryNavigateByCursor"):
343 self.supportedEditorCommands.update({
344 QsciScintilla.SCI_LINEUP: self.__QScintillaHistoryUp,
345 QsciScintilla.SCI_LINEDOWN: self.__QScintillaHistoryDown,
346 QsciScintilla.SCI_LINESCROLLUP: self.__QScintillaLineUp,
347 QsciScintilla.SCI_LINESCROLLDOWN: self.__QScintillaLineDown,
348 })
349 else:
350 self.supportedEditorCommands.update({
351 QsciScintilla.SCI_LINEUP: self.__QScintillaLineUp,
352 QsciScintilla.SCI_LINEDOWN: self.__QScintillaLineDown,
353 QsciScintilla.SCI_LINESCROLLUP: self.__QScintillaHistoryUp,
354 QsciScintilla.SCI_LINESCROLLDOWN: self.__QScintillaHistoryDown,
355 })
356
340 def __showLanguageMenu(self): 357 def __showLanguageMenu(self):
341 """ 358 """
342 Private slot to prepare the language submenu. 359 Private slot to prepare the language submenu.
343 """ 360 """
344 self.lmenu.clear() 361 self.lmenu.clear()
592 @param index index value to be set 609 @param index index value to be set
593 @type int or None 610 @type int or None
594 """ 611 """
595 if index is None: 612 if index is None:
596 # determine based on history style 613 # determine based on history style
597 if self.__historyStyle == ShellHistoryStyle.WindowsStyle: 614 if self.clientType and \
615 self.__historyStyle == ShellHistoryStyle.WindowsStyle:
598 idx = int(Preferences.Prefs.settings.value( 616 idx = int(Preferences.Prefs.settings.value(
599 "Shell/HistoryIndexes/" + self.clientType, -1)) 617 "Shell/HistoryIndexes/" + self.clientType, -1))
618 if idx >= len(self.__history):
619 idx = -1
600 self.__histidx = idx 620 self.__histidx = idx
601 else: 621 else:
602 self.__histidx = -1 622 self.__histidx = -1
603 else: 623 else:
604 self.__histidx = index 624 self.__histidx = index
615 635
616 @return flag indicating validity 636 @return flag indicating validity
617 @rtype bool 637 @rtype bool
618 """ 638 """
619 return (0 <= self.__histidx < len(self.__history)) 639 return (0 <= self.__histidx < len(self.__history))
640
641 def getHistoryIndex(self):
642 """
643 Public method to get the current value of the history index.
644
645 @return history index
646 @rtype int
647 """
648 return self.__histidx
620 649
621 def loadHistory(self, clientType): 650 def loadHistory(self, clientType):
622 """ 651 """
623 Public method to load the history for the given client type. 652 Public method to load the history for the given client type.
624 653
961 Private method to handle the middle mouse button press. 990 Private method to handle the middle mouse button press.
962 """ 991 """
963 lines = QApplication.clipboard().text(QClipboard.Selection) 992 lines = QApplication.clipboard().text(QClipboard.Selection)
964 self.executeLines(lines) 993 self.executeLines(lines)
965 994
966 def executeLines(self, lines): 995 def executeLines(self, lines, historyIndex=None):
967 """ 996 """
968 Public method to execute a set of lines as multiple commands. 997 Public method to execute a set of lines as multiple commands.
969 998
970 @param lines multiple lines of text to be executed as single 999 @param lines multiple lines of text to be executed as
971 commands (string) 1000 single commands
1001 @type str
1002 @param historyIndex history index to be set
1003 @type int
972 """ 1004 """
973 for line in lines.splitlines(True): 1005 for line in lines.splitlines(True):
974 if line.endswith("\r\n"): 1006 if line.endswith("\r\n"):
975 fullline = True 1007 fullline = True
976 cmd = line[:-2] 1008 cmd = line[:-2]
987 if cmd.startswith(sys.ps1): 1019 if cmd.startswith(sys.ps1):
988 cmd = cmd[len(sys.ps1):] 1020 cmd = cmd[len(sys.ps1):]
989 elif cmd.startswith(sys.ps2): 1021 elif cmd.startswith(sys.ps2):
990 cmd = cmd[len(sys.ps2):] 1022 cmd = cmd[len(sys.ps2):]
991 1023
992 self.__executeCommand(cmd) 1024 self.__executeCommand(cmd, historyIndex=historyIndex)
993 if self.interruptCommandExecution: 1025 if self.interruptCommandExecution:
994 self.__executeCommand("") 1026 self.__executeCommand("")
995 break 1027 break
996 1028
997 def __clearCurrentLine(self): 1029 def __clearCurrentLine(self):
1392 """ 1424 """
1393 if self.isListActive(): 1425 if self.isListActive():
1394 self.SendScintilla(cmd) 1426 self.SendScintilla(cmd)
1395 elif self.__isCursorOnLastLine(): 1427 elif self.__isCursorOnLastLine():
1396 self.moveCursorToEOL() 1428 self.moveCursorToEOL()
1397 1429
1430 def __QScintillaLineUp(self, cmd):
1431 """
1432 Private method to handle the cursor up command.
1433
1434 @param cmd QScintilla command
1435 """
1436 self.SendScintilla(QsciScintilla.SCI_LINEUP)
1437
1438 def __QScintillaLineDown(self, cmd):
1439 """
1440 Private method to handle the cursor down command.
1441
1442 @param cmd QScintilla command
1443 """
1444 self.SendScintilla(QsciScintilla.SCI_LINEDOWN)
1445
1398 def __QScintillaHistoryUp(self, cmd): 1446 def __QScintillaHistoryUp(self, cmd):
1399 """ 1447 """
1400 Private method to handle the Ctrl+Up key. 1448 Private method to handle the history up command.
1401 1449
1402 @param cmd QScintilla command 1450 @param cmd QScintilla command
1403 """ 1451 """
1404 if self.isHistoryEnabled(): 1452 if self.isHistoryEnabled():
1405 line, col = self.__getEndPos() 1453 line, col = self.__getEndPos()
1421 if found and idx >= 0: 1469 if found and idx >= 0:
1422 self.__setHistoryIndex(index=idx) 1470 self.__setHistoryIndex(index=idx)
1423 self.incrementalSearchString = buf 1471 self.incrementalSearchString = buf
1424 self.__useHistory() 1472 self.__useHistory()
1425 else: 1473 else:
1426 if self.__histidx < 0: 1474 if self.__historyWrap:
1427 # wrap around 1475 if self.__histidx < 0:
1428 self.__setHistoryIndex(index=len(self.__history) - 1) 1476 # wrap around
1477 self.__setHistoryIndex(index=len(self.__history) - 1)
1478 else:
1479 self.__setHistoryIndex(index=self.__histidx - 1)
1480 self.__useHistory()
1429 else: 1481 else:
1430 self.__setHistoryIndex(index=self.__histidx - 1) 1482 if self.__histidx < 0:
1431 self.__useHistory() 1483 self.__setHistoryIndex(index=len(self.__history) - 1)
1484 self.__useHistory()
1485 elif self.__histidx > 0:
1486 self.__setHistoryIndex(index=self.__histidx - 1)
1487 self.__useHistory()
1432 1488
1433 def __QScintillaHistoryDown(self, cmd): 1489 def __QScintillaHistoryDown(self, cmd):
1434 """ 1490 """
1435 Private method to handle the Ctrl+Down key. 1491 Private method to handle the history down command.
1436 1492
1437 @param cmd QScintilla command 1493 @param cmd QScintilla command
1438 """ 1494 """
1439 if self.isHistoryEnabled(): 1495 if self.isHistoryEnabled():
1440 line, col = self.__getEndPos() 1496 line, col = self.__getEndPos()
1456 if found and idx >= 0: 1512 if found and idx >= 0:
1457 self.__setHistoryIndex(index=idx) 1513 self.__setHistoryIndex(index=idx)
1458 self.incrementalSearchString = buf 1514 self.incrementalSearchString = buf
1459 self.__useHistory() 1515 self.__useHistory()
1460 else: 1516 else:
1461 if self.__histidx >= len(self.__history) - 1: 1517 if self.__historyWrap:
1462 # wrap around 1518 if self.__histidx >= len(self.__history) - 1:
1463 self.__setHistoryIndex(index=0) 1519 # wrap around
1520 self.__setHistoryIndex(index=0)
1521 else:
1522 self.__setHistoryIndex(index=self.__histidx + 1)
1523 self.__useHistory()
1464 else: 1524 else:
1465 self.__setHistoryIndex(index=self.__histidx + 1) 1525 if self.__isHistoryIndexValid():
1466 self.__useHistory() 1526 self.__setHistoryIndex(index=self.__histidx + 1)
1527 self.__useHistory()
1467 1528
1468 def __QScintillaCancel(self): 1529 def __QScintillaCancel(self):
1469 """ 1530 """
1470 Private method to handle the ESC command. 1531 Private method to handle the ESC command.
1471 """ 1532 """
1509 @param cmd QScintilla command 1570 @param cmd QScintilla command
1510 """ 1571 """
1511 if self.isListActive() or self.isCallTipActive(): 1572 if self.isListActive() or self.isCallTipActive():
1512 self.SendScintilla(cmd) 1573 self.SendScintilla(cmd)
1513 1574
1514 def __executeCommand(self, cmd): 1575 def __executeCommand(self, cmd, historyIndex=None):
1515 """ 1576 """
1516 Private slot to execute a command. 1577 Private slot to execute a command.
1517 1578
1518 @param cmd command to be executed by debug client (string) 1579 @param cmd command to be executed by debug client
1580 @type str
1581 @param historyIndex history index to be set
1582 @type int
1519 """ 1583 """
1520 if not self.inRawMode: 1584 if not self.inRawMode:
1521 self.inCommandExecution = True 1585 self.inCommandExecution = True
1522 self.interruptCommandExecution = False 1586 self.interruptCommandExecution = False
1523 if not cmd: 1587 if not cmd:
1524 # make sure cmd is a string 1588 # make sure cmd is a string
1525 cmd = '' 1589 cmd = ''
1526 # TODO: change according to history style 1590
1527 # Linux - append (i.e. keep as is) 1591 # History Handling
1528 # Windows - modify current entry if index not at end, add otherwise
1529 if self.isHistoryEnabled(): 1592 if self.isHistoryEnabled():
1530 if cmd != "" and ( 1593 if cmd != "" and (
1531 len(self.__history) == 0 or self.__history[-1] != cmd): 1594 len(self.__history) == 0 or self.__history[-1] != cmd):
1532 if len(self.__history) == self.__maxHistoryEntries: 1595 if len(self.__history) == self.__maxHistoryEntries:
1533 del self.__history[0] 1596 del self.__history[0]
1534 self.__history.append(cmd) 1597 self.__history.append(cmd)
1535 self.__histidx = -1 1598 if self.__historyStyle == ShellHistoryStyle.LinuxStyle:
1599 self.__setHistoryIndex(index=-1)
1600 elif self.__historyStyle == ShellHistoryStyle.WindowsStyle:
1601 if historyIndex is None:
1602 if cmd != self.__history[self.__histidx - 1]:
1603 self.__setHistoryIndex(index=-1)
1604 else:
1605 self.__setHistoryIndex(historyIndex)
1606
1536 if cmd.startswith('start '): 1607 if cmd.startswith('start '):
1537 if not self.passive: 1608 if not self.passive:
1538 cmdList = cmd.split(None, 1) 1609 cmdList = cmd.split(None, 1)
1539 if len(cmdList) < 2: 1610 if len(cmdList) < 2:
1540 self.dbs.startClient(False) # same as reset 1611 self.dbs.startClient(False) # same as reset
1749 self.__maxHistoryEntries = Preferences.getShell("MaxHistoryEntries") 1820 self.__maxHistoryEntries = Preferences.getShell("MaxHistoryEntries")
1750 for key in list(self.__historyLists.keys()): 1821 for key in list(self.__historyLists.keys()):
1751 self.__historyLists[key] = \ 1822 self.__historyLists[key] = \
1752 self.__historyLists[key][-self.__maxHistoryEntries:] 1823 self.__historyLists[key][-self.__maxHistoryEntries:]
1753 self.__historyStyle = Preferences.getShell("HistoryStyle") 1824 self.__historyStyle = Preferences.getShell("HistoryStyle")
1825 self.__historyWrap = Preferences.getShell("HistoryWrap")
1754 self.__setHistoryIndex() 1826 self.__setHistoryIndex()
1755 self.historyStyleChanged.emit(self.__historyStyle)
1756 if not self.__windowed: 1827 if not self.__windowed:
1757 self.hmenu.menuAction().setEnabled(self.isHistoryEnabled()) 1828 self.hmenu.menuAction().setEnabled(self.isHistoryEnabled())
1829 self.__setupCursorKeys()
1830 self.historyStyleChanged.emit(self.__historyStyle)
1758 1831
1759 # do stdout /stderr stuff 1832 # do stdout /stderr stuff
1760 showStdOutErr = Preferences.getShell("ShowStdOutErr") 1833 showStdOutErr = Preferences.getShell("ShowStdOutErr")
1761 if self.__showStdOutErr != showStdOutErr: 1834 if self.__showStdOutErr != showStdOutErr:
1762 if showStdOutErr: 1835 if showStdOutErr:

eric ide

mercurial