<s> import os , sys , time <EOL> import pyreadline . logger as logger <EOL> from pyreadline . logger import log , log_sock <EOL> from pyreadline . lineeditor . lineobj import Point <EOL> import pyreadline . lineeditor . lineobj as lineobj <EOL> import pyreadline . lineeditor . history as history <EOL> import basemode <EOL> import string <EOL> from pyreadline . unicode_helper import ensure_unicode <EOL> def format ( keyinfo ) : <EOL> if len ( keyinfo [ - <NUM_LIT:1> ] ) != <NUM_LIT:1> : <EOL> k = keyinfo + ( - <NUM_LIT:1> , ) <EOL> else : <EOL> k = keyinfo + ( ord ( keyinfo [ - <NUM_LIT:1> ] ) , ) <EOL> return "<STR_LIT>" % k <EOL> in_ironpython = "<STR_LIT>" in sys . version <EOL> class EmacsMode ( basemode . BaseMode ) : <EOL> mode = "<STR_LIT>" <EOL> def __init__ ( self , rlobj ) : <EOL> super ( EmacsMode , self ) . __init__ ( rlobj ) <EOL> self . _keylog = ( lambda x , y : None ) <EOL> self . previous_func = None <EOL> self . prompt = "<STR_LIT>" <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" <EOL> def add_key_logger ( self , logfun ) : <EOL> """<STR_LIT>""" <EOL> self . _keylog = logfun <EOL> def _readline_from_keyboard ( self ) : <EOL> c = self . console <EOL> def nop ( e ) : <EOL> pass <EOL> while <NUM_LIT:1> : <EOL> self . _update_line ( ) <EOL> lbuf = self . l_buffer <EOL> log_sock ( "<STR_LIT>" % ( lbuf . point , lbuf . mark , lbuf . selection_mark ) ) <EOL> try : <EOL> event = c . getkeypress ( ) <EOL> log_sock ( u"<STR_LIT>" % event ) <EOL> except KeyboardInterrupt : <EOL> from pyreadline . keysyms . common import KeyPress <EOL> from pyreadline . console . event import Event <EOL> event = Event ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> event . char = "<STR_LIT:c>" <EOL> event . keyinfo = KeyPress ( "<STR_LIT:c>" , shift = False , control = True , meta = False , keyname = None ) <EOL> log_sock ( "<STR_LIT>" ) <EOL> if self . allow_ctrl_c : <EOL> now = time . time ( ) <EOL> if ( now - self . ctrl_c_timeout ) < self . ctrl_c_tap_time_interval : <EOL> raise <EOL> else : <EOL> self . ctrl_c_timeout = now <EOL> pass <EOL> else : <EOL> raise <EOL> if self . next_meta : <EOL> self . next_meta = False <EOL> control , meta , shift , code = event . keyinfo <EOL> event . keyinfo = ( control , True , shift , code ) <EOL> keyinfo = event . keyinfo . tuple ( ) <EOL> if keyinfo in self . exit_dispatch : <EOL> if lineobj . EndOfLine ( self . l_buffer ) == <NUM_LIT:0> : <EOL> raise EOFError <EOL> if len ( keyinfo [ - <NUM_LIT:1> ] ) > <NUM_LIT:1> : <EOL> default = nop <EOL> else : <EOL> default = self . self_insert <EOL> dispatch_func = self . key_dispatch . get ( keyinfo , default ) <EOL> log ( "<STR_LIT>" % ( keyinfo , dispatch_func ) ) <EOL> log_sock ( ( u"<STR_LIT>" % ( ensure_unicode ( format ( keyinfo ) ) , dispatch_func . __name__ ) ) , "<STR_LIT>" ) <EOL> r = None <EOL> if dispatch_func : <EOL> r = dispatch_func ( event ) <EOL> self . _keylog ( dispatch_func , self . l_buffer ) <EOL> self . l_buffer . push_undo ( ) <EOL> self . previous_func = dispatch_func <EOL> if r : <EOL> self . _update_line ( ) <EOL> break <EOL> def readline ( self , prompt = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> self . ctrl_c_timeout = time . time ( ) <EOL> self . l_buffer . selection_mark = - <NUM_LIT:1> <EOL> if self . first_prompt : <EOL> self . first_prompt = False <EOL> if self . startup_hook : <EOL> try : <EOL> self . startup_hook ( ) <EOL> except : <EOL> print '<STR_LIT>' <EOL> traceback . print_exc ( ) <EOL> c = self . console <EOL> self . l_buffer . reset_line ( ) <EOL> self . prompt = prompt <EOL> self . _print_prompt ( ) <EOL> if self . pre_input_hook : <EOL> try : <EOL> self . pre_input_hook ( ) <EOL> except : <EOL> print '<STR_LIT>' <EOL> traceback . print_exc ( ) <EOL> self . pre_input_hook = None <EOL> log ( "<STR_LIT>" % self . paste_line_buffer ) <EOL> if len ( self . paste_line_buffer ) > <NUM_LIT:0> : <EOL> self . l_buffer = lineobj . ReadLineTextBuffer ( self . paste_line_buffer [ <NUM_LIT:0> ] ) <EOL> self . _update_line ( ) <EOL> self . paste_line_buffer = self . paste_line_buffer [ <NUM_LIT:1> : ] <EOL> c . write ( '<STR_LIT:\r\n>' ) <EOL> else : <EOL> self . _readline_from_keyboard ( ) <EOL> c . write ( '<STR_LIT:\r\n>' ) <EOL> self . add_history ( self . l_buffer . copy ( ) ) <EOL> log ( '<STR_LIT>' % self . l_buffer . get_line_text ( ) ) <EOL> return self . l_buffer . get_line_text ( ) + '<STR_LIT:\n>' <EOL> def previous_history ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . _history . previous_history ( self . l_buffer ) <EOL> self . l_buffer . point = lineobj . EndOfLine <EOL> def next_history ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . _history . next_history ( self . l_buffer ) <EOL> def beginning_of_history ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . _history . beginning_of_history ( ) <EOL> def end_of_history ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . _history . end_of_history ( self . l_buffer ) <EOL> def _i_search ( self , searchfun , direction , init_event ) : <EOL> c = self . console <EOL> line = self . l_buffer . get_line_text ( ) <EOL> query = '<STR_LIT>' <EOL> if ( self . previous_func != self . history_search_forward and <EOL> self . previous_func != self . history_search_backward ) : <EOL> self . query = '<STR_LIT>' . join ( self . l_buffer [ <NUM_LIT:0> : Point ] . get_line_text ( ) ) <EOL> hc_start = self . _history . history_cursor <EOL> while <NUM_LIT:1> : <EOL> x , y = self . prompt_end_pos <EOL> c . pos ( <NUM_LIT:0> , y ) <EOL> if direction < <NUM_LIT:0> : <EOL> prompt = '<STR_LIT>' <EOL> else : <EOL> prompt = '<STR_LIT>' <EOL> scroll = c . write_scrolling ( "<STR_LIT>" % ( prompt , query , line ) ) <EOL> self . _update_prompt_pos ( scroll ) <EOL> self . _clear_after ( ) <EOL> event = c . getkeypress ( ) <EOL> if event . keyinfo . keyname == '<STR_LIT>' : <EOL> query = query [ : - <NUM_LIT:1> ] <EOL> if len ( query ) > <NUM_LIT:0> : <EOL> line = searchfun ( query ) <EOL> else : <EOL> self . _bell ( ) <EOL> line = "<STR_LIT>" <EOL> elif event . char in string . letters + string . digits + string . punctuation + '<STR_LIT:U+0020>' : <EOL> query += event . char <EOL> line = searchfun ( query ) <EOL> elif event . keyinfo == init_event . keyinfo : <EOL> self . _history . history_cursor += direction <EOL> line = searchfun ( query ) <EOL> else : <EOL> if event . keyinfo . keyname != '<STR_LIT>' : <EOL> self . _bell ( ) <EOL> break <EOL> px , py = self . prompt_begin_pos <EOL> c . pos ( <NUM_LIT:0> , py ) <EOL> self . l_buffer . set_line ( line ) <EOL> self . _print_prompt ( ) <EOL> self . _history . history_cursor = len ( self . _history . history ) <EOL> def reverse_search_history ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . _i_search ( self . _history . reverse_search_history , - <NUM_LIT:1> , e ) <EOL> def forward_search_history ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . _i_search ( self . _history . forward_search_history , <NUM_LIT:1> , e ) <EOL> def non_incremental_reverse_search_history ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> q = self . _history . non_incremental_reverse_search_history ( self . l_buffer ) <EOL> self . l_buffer = q <EOL> def non_incremental_forward_search_history ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> q = self . _history . non_incremental_reverse_search_history ( self . l_buffer ) <EOL> self . l_buffer = q <EOL> def history_search_forward ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> if self . previous_func and hasattr ( self . _history , self . previous_func . __name__ ) : <EOL> self . _history . lastcommand = getattr ( self . _history , self . previous_func . __name__ ) <EOL> else : <EOL> self . _history . lastcommand = None <EOL> q = self . _history . history_search_forward ( self . l_buffer ) <EOL> self . l_buffer = q <EOL> self . l_buffer . point = q . point <EOL> def history_search_backward ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> if self . previous_func and hasattr ( self . _history , self . previous_func . __name__ ) : <EOL> self . _history . lastcommand = getattr ( self . _history , self . previous_func . __name__ ) <EOL> else : <EOL> self . _history . lastcommand = None <EOL> q = self . _history . history_search_backward ( self . l_buffer ) <EOL> self . l_buffer = q <EOL> self . l_buffer . point = q . point <EOL> def yank_nth_arg ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def yank_last_arg ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def forward_backward_delete_char ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def quoted_insert ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> e = self . console . getkeypress ( ) <EOL> self . insert_text ( e . char ) <EOL> def tab_insert ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> ws = '<STR_LIT:U+0020>' * ( self . tabstop - ( self . line_cursor % self . tabstop ) ) <EOL> self . insert_text ( ws ) <EOL> def transpose_chars ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . transpose_chars ( ) <EOL> def transpose_words ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . transpose_words ( ) <EOL> def overwrite_mode ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def kill_line ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . kill_line ( ) <EOL> def backward_kill_line ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . backward_kill_line ( ) <EOL> def unix_line_discard ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . unix_line_discard ( ) <EOL> def kill_whole_line ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . kill_whole_line ( ) <EOL> def kill_word ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . kill_word ( ) <EOL> forward_kill_word = kill_word <EOL> def backward_kill_word ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . backward_kill_word ( ) <EOL> def unix_word_rubout ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . unix_word_rubout ( ) <EOL> def kill_region ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def copy_region_as_kill ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def copy_backward_word ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def copy_forward_word ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def yank ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . yank ( ) <EOL> def yank_pop ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . yank_pop ( ) <EOL> def digit_argument ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> args = e . char <EOL> c = self . console <EOL> line = self . l_buffer . get_line_text ( ) <EOL> oldprompt = self . prompt <EOL> def nop ( e ) : <EOL> pass <EOL> while <NUM_LIT:1> : <EOL> x , y = self . prompt_end_pos <EOL> c . pos ( <NUM_LIT:0> , y ) <EOL> self . prompt = "<STR_LIT>" % args <EOL> self . _print_prompt ( ) <EOL> self . _update_line ( ) <EOL> event = c . getkeypress ( ) <EOL> if event . keyinfo . keyname == '<STR_LIT>' : <EOL> break <EOL> elif event . char in "<STR_LIT>" : <EOL> args += event . char <EOL> else : <EOL> self . argument = int ( args ) <EOL> keyinfo = event . keyinfo . tuple ( ) <EOL> if len ( keyinfo [ - <NUM_LIT:1> ] ) > <NUM_LIT:1> : <EOL> default = nop <EOL> else : <EOL> default = self . self_insert <EOL> dispatch_func = self . key_dispatch . get ( keyinfo , default ) <EOL> dispatch_func ( event ) <EOL> break <EOL> self . prompt = oldprompt <EOL> x , y = self . prompt_end_pos <EOL> c . pos ( <NUM_LIT:0> , y ) <EOL> self . _print_prompt ( ) <EOL> self . _update_line ( ) <EOL> def universal_argument ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def delete_char_or_list ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def start_kbd_macro ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def end_kbd_macro ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def call_last_kbd_macro ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def re_read_init_file ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def abort ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . _bell ( ) <EOL> def do_uppercase_version ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def prefix_meta ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . next_meta = True <EOL> def undo ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . pop_undo ( ) <EOL> def revert_line ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def tilde_expand ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def set_mark ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . set_mark ( ) <EOL> def exchange_point_and_mark ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def character_search ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def character_search_backward ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def insert_comment ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def dump_variables ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def dump_macros ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def init_editing_mode ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . _bind_exit_key ( '<STR_LIT>' ) <EOL> self . _bind_exit_key ( '<STR_LIT>' ) <EOL> self . _bind_key ( '<STR_LIT>' , self . self_insert ) <EOL> self . _bind_key ( '<STR_LIT>' , self . self_insert ) <EOL> self . _bind_key ( '<STR_LIT>' , self . self_insert ) <EOL> self . _bind_key ( '<STR_LIT>' , self . accept_line ) <EOL> self . _bind_key ( '<STR_LIT>' , self . backward_char ) <EOL> self . _bind_key ( '<STR_LIT>' , self . backward_char ) <EOL> self . _bind_key ( '<STR_LIT>' , self . forward_char ) <EOL> self . _bind_key ( '<STR_LIT>' , self . forward_char ) <EOL> self . _bind_key ( '<STR_LIT>' , self . backward_delete_char ) <EOL> self . _bind_key ( '<STR_LIT>' , self . backward_delete_word ) <EOL> self . _bind_key ( '<STR_LIT>' , self . beginning_of_line ) <EOL> self . _bind_key ( '<STR_LIT>' , self . end_of_line ) <EOL> self . _bind_key ( '<STR_LIT>' , self . delete_char ) <EOL> self . _bind_key ( '<STR_LIT>' , self . delete_char ) <EOL> self . _bind_key ( '<STR_LIT>' , self . clear_screen ) <EOL> self . _bind_key ( '<STR_LIT>' , self . forward_word ) <EOL> self . _bind_key ( '<STR_LIT>' , self . backward_word ) <EOL> self . _bind_key ( '<STR_LIT>' , self . clear_screen ) <EOL> self . _bind_key ( '<STR_LIT>' , self . previous_history ) <EOL> self . _bind_key ( '<STR_LIT>' , self . history_search_backward ) <EOL> self . _bind_key ( '<STR_LIT>' , self . next_history ) <EOL> self . _bind_key ( '<STR_LIT>' , self . history_search_forward ) <EOL> self . _bind_key ( '<STR_LIT>' , self . beginning_of_line ) <EOL> self . _bind_key ( '<STR_LIT>' , self . end_of_line ) <EOL> self . _bind_key ( '<STR_LIT>' , self . beginning_of_history ) <EOL> self . _bind_key ( '<STR_LIT>' , self . end_of_history ) <EOL> self . _bind_key ( '<STR_LIT>' , self . reverse_search_history ) <EOL> self . _bind_key ( '<STR_LIT>' , self . forward_search_history ) <EOL> self . _bind_key ( '<STR_LIT>' , self . non_incremental_reverse_search_history ) <EOL> self . _bind_key ( '<STR_LIT>' , self . non_incremental_forward_search_history ) <EOL> self . _bind_key ( '<STR_LIT>' , self . undo ) <EOL> self . _bind_key ( '<STR_LIT>' , self . undo ) <EOL> self . _bind_key ( '<STR_LIT>' , self . kill_whole_line ) <EOL> self . _bind_key ( '<STR_LIT>' , self . kill_word ) <EOL> self . _bind_key ( '<STR_LIT>' , self . forward_delete_word ) <EOL> self . _bind_key ( '<STR_LIT>' , self . unix_word_rubout ) <EOL> self . _bind_key ( '<STR_LIT>' , self . paste ) <EOL> self . _bind_key ( '<STR_LIT>' , self . ipython_paste ) <EOL> self . _bind_key ( '<STR_LIT>' , self . yank ) <EOL> self . _bind_key ( '<STR_LIT>' , self . kill_line ) <EOL> self . _bind_key ( '<STR_LIT>' , self . set_mark ) <EOL> self . _bind_key ( '<STR_LIT>' , self . copy_region_to_clipboard ) <EOL> self . _bind_key ( '<STR_LIT>' , self . paste_mulitline_code ) <EOL> self . _bind_key ( "<STR_LIT>" , self . forward_word_end ) <EOL> self . _bind_key ( "<STR_LIT>" , self . backward_word ) <EOL> self . _bind_key ( "<STR_LIT>" , self . forward_char_extend_selection ) <EOL> self . _bind_key ( "<STR_LIT>" , self . backward_char_extend_selection ) <EOL> self . _bind_key ( "<STR_LIT>" , self . forward_word_end_extend_selection ) <EOL> self . _bind_key ( "<STR_LIT>" , self . backward_word_extend_selection ) <EOL> self . _bind_key ( "<STR_LIT>" , self . beginning_of_line_extend_selection ) <EOL> self . _bind_key ( "<STR_LIT>" , self . end_of_line_extend_selection ) <EOL> self . _bind_key ( "<STR_LIT>" , self . self_insert ) <EOL> self . _bind_key ( "<STR_LIT>" , self . self_insert ) <EOL> self . _bind_key ( "<STR_LIT>" , self . self_insert ) <EOL> self . _bind_key ( "<STR_LIT>" , self . self_insert ) <EOL> self . _bind_key ( "<STR_LIT>" , self . self_insert ) <EOL> self . _bind_key ( "<STR_LIT>" , self . self_insert ) <EOL> self . _bind_key ( "<STR_LIT>" , self . self_insert ) <EOL> self . _bind_key ( "<STR_LIT>" , self . self_insert ) <EOL> self . _bind_key ( "<STR_LIT>" , self . self_insert ) <EOL> self . _bind_key ( "<STR_LIT>" , self . self_insert ) <EOL> self . _bind_key ( "<STR_LIT>" , self . self_insert ) <EOL> self . _bind_key ( "<STR_LIT>" , self . self_insert ) <EOL> self . _bind_key ( "<STR_LIT>" , self . self_insert ) <EOL> self . _bind_key ( "<STR_LIT>" , self . self_insert ) <EOL> self . _bind_key ( "<STR_LIT>" , self . self_insert ) <EOL> log ( "<STR_LIT>" ) <EOL> def commonprefix ( m ) : <EOL> "<STR_LIT>" <EOL> if not m : return '<STR_LIT>' <EOL> prefix = m [ <NUM_LIT:0> ] <EOL> for item in m : <EOL> for i in range ( len ( prefix ) ) : <EOL> if prefix [ : i + <NUM_LIT:1> ] . lower ( ) != item [ : i + <NUM_LIT:1> ] . lower ( ) : <EOL> prefix = prefix [ : i ] <EOL> if i == <NUM_LIT:0> : return '<STR_LIT>' <EOL> break <EOL> return prefix </s>
