<s> import os , re , math , glob , sys <EOL> import pyreadline . logger as logger <EOL> from pyreadline . logger import log , log_sock <EOL> from pyreadline . keysyms . common import make_KeyPress_from_keydescr <EOL> import pyreadline . lineeditor . lineobj as lineobj <EOL> import pyreadline . lineeditor . history as history <EOL> import pyreadline . clipboard as clipboard <EOL> from pyreadline . error import ReadlineError , GetSetError <EOL> from pyreadline . unicode_helper import ensure_str , ensure_unicode <EOL> in_ironpython = "<STR_LIT>" in sys . version <EOL> class BaseMode ( object ) : <EOL> mode = "<STR_LIT>" <EOL> def __init__ ( self , rlobj ) : <EOL> self . rlobj = rlobj <EOL> self . exit_dispatch = { } <EOL> self . key_dispatch = { } <EOL> self . argument = <NUM_LIT:1> <EOL> self . prevargument = None <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" <EOL> def _gs ( x ) : <EOL> def g ( self ) : <EOL> return getattr ( self . rlobj , x ) <EOL> def s ( self , q ) : <EOL> setattr ( self . rlobj , x , q ) <EOL> return g , s <EOL> def _g ( x ) : <EOL> def g ( self ) : <EOL> return getattr ( self . rlobj , x ) <EOL> return g <EOL> def _argreset ( self ) : <EOL> val = self . argument <EOL> self . argument = <NUM_LIT:1> <EOL> return val <EOL> argument_reset = property ( _argreset ) <EOL> ctrl_c_tap_time_interval = property ( * _gs ( "<STR_LIT>" ) ) <EOL> allow_ctrl_c = property ( * _gs ( "<STR_LIT>" ) ) <EOL> l_buffer = property ( * _gs ( "<STR_LIT>" ) ) <EOL> next_meta = property ( * _gs ( "<STR_LIT>" ) ) <EOL> first_prompt = property ( * _gs ( "<STR_LIT>" ) ) <EOL> prompt = property ( * _gs ( "<STR_LIT>" ) ) <EOL> paste_line_buffer = property ( * _gs ( "<STR_LIT>" ) ) <EOL> completer_delims = property ( * _gs ( "<STR_LIT>" ) ) <EOL> show_all_if_ambiguous = property ( * _gs ( "<STR_LIT>" ) ) <EOL> mark_directories = property ( * _gs ( "<STR_LIT>" ) ) <EOL> completer = property ( * _gs ( "<STR_LIT>" ) ) <EOL> begidx = property ( * _gs ( "<STR_LIT>" ) ) <EOL> startup_hook = property ( * _gs ( "<STR_LIT>" ) ) <EOL> pre_input_hook = property ( * _gs ( "<STR_LIT>" ) ) <EOL> endidx = property ( * _gs ( "<STR_LIT>" ) ) <EOL> console = property ( _g ( "<STR_LIT>" ) ) <EOL> insert_text = property ( _g ( "<STR_LIT>" ) ) <EOL> _print_prompt = property ( _g ( "<STR_LIT>" ) ) <EOL> _update_line = property ( _g ( "<STR_LIT>" ) ) <EOL> add_history = property ( _g ( "<STR_LIT>" ) ) <EOL> _bell = property ( _g ( "<STR_LIT>" ) ) <EOL> _clear_after = property ( _g ( "<STR_LIT>" ) ) <EOL> _set_cursor = property ( _g ( "<STR_LIT>" ) ) <EOL> _update_prompt_pos = property ( _g ( "<STR_LIT>" ) ) <EOL> _update_line = property ( _g ( "<STR_LIT>" ) ) <EOL> enable_win32_clipboard = property ( _g ( "<STR_LIT>" ) ) <EOL> enable_ipython_paste_list_of_lists = property ( _g ( "<STR_LIT>" ) ) <EOL> enable_ipython_paste_for_paths = property ( _g ( "<STR_LIT>" ) ) <EOL> _bell = property ( _g ( "<STR_LIT>" ) ) <EOL> _history = property ( _g ( "<STR_LIT>" ) ) <EOL> prompt_end_pos = property ( _g ( "<STR_LIT>" ) ) <EOL> prompt_begin_pos = property ( _g ( "<STR_LIT>" ) ) <EOL> rl_settings_to_string = property ( _g ( "<STR_LIT>" ) ) <EOL> def _readline_from_keyboard ( self ) : <EOL> raise NotImplementedError <EOL> def readline ( self , prompt = '<STR_LIT>' ) : <EOL> raise NotImplementedError <EOL> def _bind_key ( self , key , func ) : <EOL> '''<STR_LIT>''' <EOL> if type ( func ) != type ( self . _bind_key ) : <EOL> print "<STR_LIT>" % ( key , func ) <EOL> raise PyreadlineError ( "<STR_LIT>" % ( key , func , type ( func ) , type ( self . _bind_key ) ) ) <EOL> keyinfo = make_KeyPress_from_keydescr ( key . lower ( ) ) . tuple ( ) <EOL> log ( "<STR_LIT>" % ( keyinfo , func . __name__ ) ) <EOL> self . key_dispatch [ keyinfo ] = func <EOL> def _bind_exit_key ( self , key ) : <EOL> '''<STR_LIT>''' <EOL> keyinfo = make_KeyPress_from_keydescr ( key . lower ( ) ) . tuple ( ) <EOL> self . exit_dispatch [ keyinfo ] = None <EOL> def init_editing_mode ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> raise NotImplementedError <EOL> def _get_completions ( self ) : <EOL> '''<STR_LIT>''' <EOL> completions = [ ] <EOL> self . begidx = self . l_buffer . point <EOL> self . endidx = self . l_buffer . point <EOL> buf = self . l_buffer . line_buffer <EOL> if self . completer : <EOL> while self . begidx > <NUM_LIT:0> : <EOL> self . begidx -= <NUM_LIT:1> <EOL> if buf [ self . begidx ] in self . completer_delims : <EOL> self . begidx += <NUM_LIT:1> <EOL> break <EOL> text = ensure_str ( '<STR_LIT>' . join ( buf [ self . begidx : self . endidx ] ) ) <EOL> log ( '<STR_LIT>' % text ) <EOL> i = <NUM_LIT:0> <EOL> while <NUM_LIT:1> : <EOL> try : <EOL> r = ensure_unicode ( self . completer ( text , i ) ) <EOL> except : <EOL> break <EOL> i += <NUM_LIT:1> <EOL> if r and r not in completions : <EOL> completions . append ( r ) <EOL> else : <EOL> break <EOL> log ( '<STR_LIT>' % completions ) <EOL> if not completions : <EOL> while self . begidx > <NUM_LIT:0> : <EOL> self . begidx -= <NUM_LIT:1> <EOL> if buf [ self . begidx ] in '<STR_LIT>' : <EOL> self . begidx += <NUM_LIT:1> <EOL> break <EOL> text = ensure_str ( '<STR_LIT>' . join ( buf [ self . begidx : self . endidx ] ) ) <EOL> log ( '<STR_LIT>' % text ) <EOL> completions = map ( ensure_unicode , glob . glob ( os . path . expanduser ( text ) + '<STR_LIT:*>' ) ) <EOL> if self . mark_directories == '<STR_LIT>' : <EOL> mc = [ ] <EOL> for f in completions : <EOL> if os . path . isdir ( f ) : <EOL> mc . append ( f + os . sep ) <EOL> else : <EOL> mc . append ( f ) <EOL> completions = mc <EOL> log ( '<STR_LIT>' % completions ) <EOL> return completions <EOL> def _display_completions ( self , completions ) : <EOL> if not completions : <EOL> return <EOL> self . console . write ( '<STR_LIT:\n>' ) <EOL> wmax = max ( map ( len , completions ) ) <EOL> w , h = self . console . size ( ) <EOL> cols = max ( <NUM_LIT:1> , int ( ( w - <NUM_LIT:1> ) / ( wmax + <NUM_LIT:1> ) ) ) <EOL> rows = int ( math . ceil ( float ( len ( completions ) ) / cols ) ) <EOL> for row in range ( rows ) : <EOL> s = '<STR_LIT>' <EOL> for col in range ( cols ) : <EOL> i = col * rows + row <EOL> if i < len ( completions ) : <EOL> self . console . write ( completions [ i ] . ljust ( wmax + <NUM_LIT:1> ) ) <EOL> self . console . write ( '<STR_LIT:\n>' ) <EOL> if in_ironpython : <EOL> self . prompt = sys . ps1 <EOL> self . _print_prompt ( ) <EOL> def complete ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> completions = self . _get_completions ( ) <EOL> if completions : <EOL> cprefix = commonprefix ( completions ) <EOL> rep = [ c for c in cprefix ] <EOL> point = self . l_buffer . point <EOL> self . l_buffer [ self . begidx : self . endidx ] = rep <EOL> self . l_buffer . point = point + len ( rep ) - ( self . endidx - self . begidx ) <EOL> if len ( completions ) > <NUM_LIT:1> : <EOL> if self . show_all_if_ambiguous == '<STR_LIT>' : <EOL> self . _display_completions ( completions ) <EOL> else : <EOL> self . _bell ( ) <EOL> else : <EOL> self . _bell ( ) <EOL> def possible_completions ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> completions = self . _get_completions ( ) <EOL> self . _display_completions ( completions ) <EOL> def insert_completions ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> completions = self . _get_completions ( ) <EOL> b = self . begidx <EOL> e = self . endidx <EOL> for comp in completions : <EOL> rep = [ c for c in comp ] <EOL> rep . append ( '<STR_LIT:U+0020>' ) <EOL> self . l_buffer [ b : e ] = rep <EOL> b += len ( rep ) <EOL> e = b <EOL> self . line_cursor = b <EOL> def menu_complete ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def beginning_of_line ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . beginning_of_line ( ) <EOL> def end_of_line ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . end_of_line ( ) <EOL> def forward_char ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . forward_char ( self . argument_reset ) <EOL> def backward_char ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . backward_char ( self . argument_reset ) <EOL> def forward_word ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . forward_word ( self . argument_reset ) <EOL> def backward_word ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . backward_word ( self . argument_reset ) <EOL> def forward_word_end ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . forward_word_end ( self . argument_reset ) <EOL> def backward_word_end ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . backward_word_end ( self . argument_reset ) <EOL> def beginning_of_line_extend_selection ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . beginning_of_line_extend_selection ( ) <EOL> def end_of_line_extend_selection ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . end_of_line_extend_selection ( ) <EOL> def forward_char_extend_selection ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . forward_char_extend_selection ( self . argument_reset ) <EOL> def backward_char_extend_selection ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . backward_char_extend_selection ( self . argument_reset ) <EOL> def forward_word_extend_selection ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . forward_word_extend_selection ( self . argument_reset ) <EOL> def backward_word_extend_selection ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . backward_word_extend_selection ( self . argument_reset ) <EOL> def forward_word_end_extend_selection ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . forward_word_end_extend_selection ( self . argument_reset ) <EOL> def backward_word_end_extend_selection ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . forward_word_end_extend_selection ( self . argument_reset ) <EOL> def upcase_word ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . upcase_word ( ) <EOL> def downcase_word ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . downcase_word ( ) <EOL> def capitalize_word ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . capitalize_word ( ) <EOL> def clear_screen ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . console . page ( ) <EOL> def redraw_current_line ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def accept_line ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> return True <EOL> def delete_char ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . delete_char ( self . argument_reset ) <EOL> def backward_delete_char ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . backward_delete_char ( self . argument_reset ) <EOL> def backward_delete_word ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . backward_delete_word ( self . argument_reset ) <EOL> def forward_delete_word ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . forward_delete_word ( self . argument_reset ) <EOL> def delete_horizontal_space ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . delete_horizontal_space ( ) <EOL> def self_insert ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> if e . char and ord ( e . char ) != <NUM_LIT:0> : <EOL> self . insert_text ( e . char ) <EOL> def paste ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> if self . enable_win32_clipboard : <EOL> txt = clipboard . get_clipboard_text_and_convert ( False ) <EOL> txt = txt . split ( "<STR_LIT:\n>" ) [ <NUM_LIT:0> ] . strip ( "<STR_LIT:\r>" ) . strip ( "<STR_LIT:\n>" ) <EOL> log ( "<STR_LIT>" % map ( ord , txt ) ) <EOL> self . insert_text ( txt ) <EOL> def paste_mulitline_code ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> reg = re . compile ( "<STR_LIT>" ) <EOL> if self . enable_win32_clipboard : <EOL> txt = clipboard . get_clipboard_text_and_convert ( False ) <EOL> t = reg . split ( txt ) <EOL> t = [ row for row in t if row . strip ( ) != "<STR_LIT>" ] <EOL> if t != [ "<STR_LIT>" ] : <EOL> self . insert_text ( t [ <NUM_LIT:0> ] ) <EOL> self . add_history ( self . l_buffer . copy ( ) ) <EOL> self . paste_line_buffer = t [ <NUM_LIT:1> : ] <EOL> log ( "<STR_LIT>" % self . paste_line_buffer ) <EOL> return True <EOL> else : <EOL> return False <EOL> def ipython_paste ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> if self . enable_win32_clipboard : <EOL> txt = clipboard . get_clipboard_text_and_convert ( <EOL> self . enable_ipython_paste_list_of_lists ) <EOL> if self . enable_ipython_paste_for_paths : <EOL> if len ( txt ) < <NUM_LIT> and ( "<STR_LIT:\t>" not in txt ) and ( "<STR_LIT:\n>" not in txt ) : <EOL> txt = txt . replace ( "<STR_LIT:\\>" , "<STR_LIT:/>" ) . replace ( "<STR_LIT:U+0020>" , r"<STR_LIT>" ) <EOL> self . insert_text ( txt ) <EOL> def copy_region_to_clipboard ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . copy_region_to_clipboard ( ) <EOL> def copy_selection_to_clipboard ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . copy_selection_to_clipboard ( ) <EOL> def cut_selection_to_clipboard ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> self . l_buffer . cut_selection_to_clipboard ( ) <EOL> def dump_functions ( self , e ) : <EOL> '''<STR_LIT>''' <EOL> print <EOL> txt = "<STR_LIT:\n>" . join ( self . rl_settings_to_string ( ) ) <EOL> print txt <EOL> self . _print_prompt ( ) <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>
