예제 #1
0
파일: shell.py 프로젝트: kriek197/Eelbrain
    def OnKeyDown(self, event):
        key = event.GetKeyCode()
        mod = wxutils.key_mod(event)
#        logging.debug("app.shell.OnKeyDown(): key=%r, mod=%r" % (key, mod))
        
        if key == 68: # 'd'
            if mod == [1, 0, 1]: # alt -> also include output
                mod = [1, 0, 0]
                duplicate_output = True
            else:
                duplicate_output = False
        # copy selection to first editor
        if mod == [1, 0, 0]: # [command]
            if key==80: # 'p'
                plt.draw()
                if plt.get_backend() == 'WXAgg':
                    plt.show()
            elif key==68: # 'd'
                # make sure we have a target editor
                if not hasattr(self.active_editor, 'InsertLine'):
                    self.OnPyEd_New(event)
                    self.active_editor = self.editors[-1]
                editor = self.active_editor
                # prepare text for transfer
                text = self.shell.GetSelectedText()
                lines = [line for line in text.split(os.linesep) if len(line)>0]
                # FIXME: alt
                for line in lines:
                    line_stripped = self.shell.lstripPrompt(line)
                    if duplicate_output or len(line_stripped) < len(line):
                        try:
                            editor.InsertLine(line_stripped)
                        except:
                            logging.debug("'Duplicate to Editor' failed")
            else:
                event.Skip()
        else:
#            logging.info("shell key: %s"%key)
            event.Skip()
예제 #2
0
    def OnKeyDown(self, event):
        # these ought to be handles in stc.StyledTextCtrl
        # src/osx_cocoa/stc.py ?
        key = event.GetKeyCode()
        mod = wxutils.key_mod(event)
        if any(mod):
            mod_str = '-'.join(d for d, s in zip(['ctrl', 'cmd', 'alt'], mod) if s)
            logging.info("Editor OnKeyDown: {0} {1}".format(mod_str, key))

        if mod == [1, 0, 0]:  # [ctrl]
            if key == 47:  # [/] --> comment
                w = self.editor.window
                start_pos, end_pos = w.GetSelection()
                start = w.LineFromPosition(start_pos)
                end = w.LineFromPosition(end_pos)
                if start > end:
                    start, end = end, start
                if start != end and end_pos == w.PositionFromLine(end):
                    end -= 1  # if no element in the last line is selected
                lines = range(start, end + 1)
                first = w.GetLine(lines[0])
                if len(first) == 0 or first[0] != '#':  # de-activate
                    for i in lines:
                        line = w.GetLine(i)
                        pos = w.PositionFromLine(i)
                        if len(line) > 1 and line[:2] == '##':
                            pass
                        elif len(line) > 0 and line[0] == '#':
                            w.InsertText(pos, '#')
                        else:
                            w.InsertText(pos, '##')
                else:  # un-comment
                    for i in lines:
                        while w.GetLine(i)[0] == '#':
                            pos = w.PositionFromLine(i) + 1
                            w.SetSelection(pos, pos)
#                                w.SetCurrentPos(pos)
                            w.DeleteBack()
                start = w.PositionFromLine(start)
                end = w.PositionFromLine(end + 1) - 1
                w.SetSelection(start, end)
                return
            elif key == 68:  # d
                self.OnCopySelectionToShell(event)
            elif key == 69:  # e -> execute excerpt
                self.OnExecSelected(event)
            elif key == 82:  # r -> execute the script
                self.OnExecFromDrive(event)
            else:
                event.Skip()

        elif mod == [0, 0, 1]:  # alt down
            if key in [315, 317]:  # arrow
                # FIXME: when last line without endline is selected, someting
                #        bad happens
                w = self.editor.window
                w.LineCut()
                if key == 315:  # [up]
                    w.LineUp()
                elif key == 317:  # [down]
                    w.LineDown()
                w.Paste()
                w.LineUp()
                return
            else:
                event.Skip()
#        elif mod == [0, 1, 0]: # command (Os-X)
#            pass
        else:
            event.Skip()
예제 #3
0
    def OnKeyDown(self, event):
        # these ought to be handles in stc.StyledTextCtrl
        # src/osx_cocoa/stc.py ?
        key = event.GetKeyCode()
#        logging.debug(dir(event))
        mod = wxutils.key_mod(event)
        logging.debug("Editor OnKeyDown: {0} {1}".format(mod, key))
        #print dir(event)
        
        if mod == [1, 0, 0]: # [ctrl]
            if key == 47: # [/] --> comment
                w = self.editor.window
                start_pos, end_pos = w.GetSelection()
                start = w.LineFromPosition(start_pos)
                end = w.LineFromPosition(end_pos)
                if start > end:
                    start, end = end, start
                if start != end and end_pos == w.PositionFromLine(end):
                    end -= 1 # if no element in the last line is selected
                lines = range(start, end+1)
                first = w.GetLine(lines[0])
                if len(first) == 0 or first[0] != '#': # de-activate
                    for i in lines:
                        line = w.GetLine(i)
                        pos = w.PositionFromLine(i)
                        if len(line) > 1 and line[:2] == '##':
                            pass
                        elif len(line) > 0 and line[0] == '#':
                            w.InsertText(pos, '#')
                        else:
                            w.InsertText(pos, '##')
                else: # un-comment
                    for i in lines:
                        while w.GetLine(i)[0] == '#':
                            pos = w.PositionFromLine(i) + 1
                            w.SetSelection(pos, pos)
#                                w.SetCurrentPos(pos)
                            w.DeleteBack()
                start = w.PositionFromLine(start)
                end = w.PositionFromLine(end+1)-1
                w.SetSelection(start, end)
                return
            elif key == 82: # r -> run the script
                self.OnExecFromDrive(event)
#            elif key == 83: # 's'
#                # FIXME: OnKeyDown is not called by command-s 
#                self.OnFileSave()
#                return
        elif mod == [0, 0, 1]: # alt down
            if key in [315, 317]: # arrow
                # FIXME: when last line without endline is selected, someting 
                #        bad happens
                w = self.editor.window
                w.LineCut()
                if key == 315: # [up]
                    w.LineUp()
                elif key == 317: # [down]
                    w.LineDown()
                w.Paste()
                w.LineUp()
                return
        elif mod == [0, 1, 0]: # command (Os-X)
            pass
        event.Skip()