Changeset 2896

Show
Ignore:
Timestamp:
12/17/07 12:30:39 (1 year ago)
Author:
jstenar
Message:

pyreadline comitted first cut of a callback interface to the callback branch

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • pyreadline/branches/callback/pyreadline/__init__.py

    r2876 r2896  
    2828            'get_completer_delims', 
    2929            'add_history', 
     30            'callback_handler_install', 
     31            'callback_handler_remove', 
     32            'callback_read_char', 
    3033            'GetOutputFile', 
    3134            'rl', 
  • pyreadline/branches/callback/pyreadline/modes/emacs.py

    r2864 r2896  
    3434        self.previous_func=None 
    3535        self.prompt=">>>" 
     36 
    3637    def __repr__(self): 
    3738        return "<EmacsMode>" 
     
    4041        """logfun should be function that takes disp_fun and line_buffer object """ 
    4142        self._keylog=logfun 
    42          
    43     def _readline_from_keyboard(self): 
     43 
     44 
     45    def _readline_from_keyboard_poll(self): 
    4446        c=self.console 
    4547        def nop(e): 
    4648            pass 
     49        try: 
     50            event = c.getkeypress() 
     51        except KeyboardInterrupt: 
     52            event=self.handle_ctrl_c() 
     53            self._update_line() 
     54            return False 
     55 
     56        if self.next_meta: 
     57            self.next_meta = False 
     58            control, meta, shift, code = event.keyinfo 
     59            event.keyinfo = (control, True, shift, code) 
     60 
     61        #Process exit keys. Only exit on empty line 
     62        keyinfo=event.keyinfo.tuple() 
     63        if keyinfo in self.exit_dispatch: 
     64            if lineobj.EndOfLine(self.l_buffer) == 0: 
     65                raise EOFError 
     66        if len(keyinfo[-1])>1: 
     67            default=nop 
     68        else: 
     69            default=self.self_insert 
     70        dispatch_func = self.key_dispatch.get(keyinfo,default) 
     71 
     72        log("readline from keyboard:%s,%s"%(keyinfo,dispatch_func)) 
     73        log_sock("%s|%s"%(format(keyinfo),dispatch_func.__name__),"bound_function") 
     74        r = None 
     75        if dispatch_func: 
     76            r = dispatch_func(event) 
     77            self._keylog(dispatch_func,self.l_buffer) 
     78            self.l_buffer.push_undo() 
     79 
     80        self.previous_func = dispatch_func 
     81        self._update_line() 
     82        if r: 
     83            return True 
     84        return False 
     85 
     86 
     87    def handle_ctrl_c(self): 
     88        from pyreadline.keysyms.common import KeyPress 
     89        from pyreadline.console.event import Event 
     90        log_sock("KBDIRQ") 
     91        event=Event(0,0) 
     92        event.char="c" 
     93        event.keyinfo=KeyPress("c",shift=False,control=True,meta=False,keyname=None) 
     94        if self.allow_ctrl_c: 
     95            now=time.time() 
     96            if (now-self.ctrl_c_timeout)<self.ctrl_c_tap_time_interval: 
     97                log_sock("Raise KeyboardInterrupt") 
     98                raise KeyboardInterrupt 
     99            else: 
     100                self.ctrl_c_timeout=now 
     101        else: 
     102            raise KeyboardInterrupt 
     103 
     104        dispatch_func = self.key_dispatch.get(event.keyinfo.tuple(),None) 
     105        if dispatch_func: 
     106            r = dispatch_func(event) 
     107            self._keylog(dispatch_func,self.l_buffer) 
     108            self.l_buffer.push_undo() 
     109         
     110    def _readline_from_keyboard(self): 
    47111        while 1: 
     112            if self._readline_from_keyboard_poll(): 
     113                break 
     114 
     115    def readline_event_available(self): 
     116        return self.console.peek() 
     117 
     118    def readline(self, prompt=''): 
     119        c = self.console 
     120        self.readline_setup(prompt) 
     121        log("in readline: %s"%self.paste_line_buffer) 
     122        if len(self.paste_line_buffer)>0: 
     123            self.l_buffer=lineobj.ReadLineTextBuffer(self.paste_line_buffer[0]) 
    48124            self._update_line() 
    49             lbuf=self.l_buffer 
    50             log_sock("point:%d mark:%d selection_mark:%d"%(lbuf.point,lbuf.mark,lbuf.selection_mark)) 
    51             try: 
    52                 event = c.getkeypress() 
    53                 log_sock(u">>%s"%event) 
    54             except KeyboardInterrupt: 
    55                 from pyreadline.keysyms.common import KeyPress 
    56                 from pyreadline.console.event import Event 
    57                 event=Event(0,0) 
    58                 event.char="c" 
    59                 event.keyinfo=KeyPress("c",shift=False,control=True,meta=False,keyname=None) 
    60                 log_sock("KBDIRQ") 
    61                 if self.allow_ctrl_c: 
    62                     now=time.time() 
    63                     if (now-self.ctrl_c_timeout)<self.ctrl_c_tap_time_interval: 
    64                         raise 
    65                     else: 
    66                         self.ctrl_c_timeout=now 
    67                     pass 
    68                 else: 
    69                     raise 
    70             if self.next_meta: 
    71                 self.next_meta = False 
    72                 control, meta, shift, code = event.keyinfo 
    73                 event.keyinfo = (control, True, shift, code) 
    74  
    75             #Process exit keys. Only exit on empty line 
    76             keyinfo=event.keyinfo.tuple() 
    77             if keyinfo in self.exit_dispatch: 
    78                 if lineobj.EndOfLine(self.l_buffer) == 0: 
    79                     raise EOFError 
    80             if len(keyinfo[-1])>1: 
    81                 default=nop 
    82             else: 
    83                 default=self.self_insert 
    84             dispatch_func = self.key_dispatch.get(keyinfo,default) 
    85              
    86             log("readline from keyboard:%s,%s"%(keyinfo,dispatch_func)) 
    87             log_sock((u"%s|%s"%(ensure_unicode(format(keyinfo)),dispatch_func.__name__)),"bound_function") 
    88             r = None 
    89             if dispatch_func: 
    90                 r = dispatch_func(event) 
    91                 self._keylog(dispatch_func,self.l_buffer) 
    92                 self.l_buffer.push_undo() 
    93  
    94             self.previous_func = dispatch_func 
    95             if r: 
    96                 self._update_line() 
    97                 break 
    98  
    99     def readline(self, prompt=''): 
     125            self.paste_line_buffer=self.paste_line_buffer[1:] 
     126        else: 
     127            self._readline_from_keyboard() 
     128        c.write('\r\n') 
     129 
     130        self.add_history(self.l_buffer.copy()) 
     131 
     132        log('returning(%s)' % self.l_buffer.get_line_text()) 
     133        return self.l_buffer.get_line_text() + '\n' 
     134 
     135 
     136    def readline_setup(self, prompt=''): 
    100137        '''Try to act like GNU readline.''' 
    101138        # handle startup_hook 
     
    123160                traceback.print_exc() 
    124161                self.pre_input_hook = None 
    125  
    126         log("in readline: %s"%self.paste_line_buffer) 
    127         if len(self.paste_line_buffer)>0: 
    128             self.l_buffer=lineobj.ReadLineTextBuffer(self.paste_line_buffer[0]) 
    129             self._update_line() 
    130             self.paste_line_buffer=self.paste_line_buffer[1:] 
    131             c.write('\r\n') 
    132         else: 
    133             self._readline_from_keyboard() 
    134             c.write('\r\n') 
    135  
    136         self.add_history(self.l_buffer.copy()) 
    137  
    138         log('returning(%s)' % self.l_buffer.get_line_text()) 
    139         return self.l_buffer.get_line_text() + '\n' 
     162        self._update_line() 
     163 
    140164 
    141165#########  History commands 
     
    370394    def yank(self, e): # (C-y) 
    371395        '''Yank the top of the kill ring into the buffer at point. ''' 
    372         self.l_buffer.yank() 
     396        pass 
    373397 
    374398    def yank_pop(self, e): # (M-y) 
    375399        '''Rotate the kill-ring, and yank the new top. You can only do this 
    376400        if the prior command is yank or yank-pop.''' 
    377         self.l_buffer.yank_pop() 
     401        pass 
    378402 
    379403 
     
    583607        self._bind_key('Control-v',         self.paste) 
    584608        self._bind_key('Alt-v',             self.ipython_paste) 
    585         self._bind_key('Control-y',         self.yank
     609        self._bind_key('Control-y',         self.paste
    586610        self._bind_key('Control-k',         self.kill_line) 
    587611        self._bind_key('Control-m',         self.set_mark) 
  • pyreadline/branches/callback/pyreadline/rlmain.py

    r2876 r2896  
    8989 
    9090        self.paste_line_buffer=[] 
     91        self.callback = None 
    9192 
    9293    #Below is for refactoring, raise errors when using old style attributes  
     
    341342    def readline(self, prompt=''): 
    342343        return self.mode.readline(prompt) 
     344 
     345    def event_available(self): 
     346        return self.mode.readline_event_available() 
     347 
     348    def setup(self,prompt=""): 
     349        return self.mode.readline_setup(prompt) 
     350 
     351    def keyboard_poll(self): 
     352        return self.mode._readline_from_keyboard_poll() 
     353 
     354# 
     355# Callback interface 
     356# 
     357 
     358    def callback_handler_install(self, prompt, callback): 
     359        '''bool readline_callback_handler_install ( string prompt, callback callback) 
     360        Initializes the readline callback interface and terminal, prints the prompt and returns immediately 
     361        ''' 
     362        self.callback = callback 
     363        self.mode.readline_setup(prompt) 
     364 
     365    def callback_handler_remove(self): 
     366        '''Removes a previously installed callback handler and restores terminal settings''' 
     367        self.callback = None 
     368 
     369    def callback_read_char(self): 
     370        '''Reads a character and informs the readline callback interface when a line is received''' 
     371        if self.keyboard_poll(): 
     372            line = self.l_buffer.get_line_text() + '\n' 
     373            self.console.write('\r\n') # this is the newline terminating input 
     374            # however there is another newline added by 
     375            # self.mode.readline_setup(prompt) which is called by callback_handler_install 
     376            # this differs from GNU readline 
     377            self.add_history(self.l_buffer.copy()) 
     378            # TADA: 
     379            self.callback(line) 
    343380 
    344381    def read_inputrc(self,inputrcpath=os.path.expanduser("~/pyreadlineconfig.ini")): 
     
    471508set_startup_hook = rl.set_startup_hook 
    472509set_pre_input_hook = rl.set_pre_input_hook 
     510callback_handler_install=rl.callback_handler_install 
     511callback_handler_remove=rl.callback_handler_remove 
     512callback_read_char=rl.callback_read_char 
    473513 
    474514if __name__ == '__main__':