<s> import sys , os <EOL> class Command ( object ) : <EOL> finish = <NUM_LIT:0> <EOL> kills_digit_arg = <NUM_LIT:1> <EOL> def __init__ ( self , reader , ( event_name , event ) ) : <EOL> self . reader = reader <EOL> self . event = event <EOL> self . event_name = event_name <EOL> def do ( self ) : <EOL> pass <EOL> class KillCommand ( Command ) : <EOL> def kill_range ( self , start , end ) : <EOL> if start == end : <EOL> return <EOL> r = self . reader <EOL> b = r . buffer <EOL> text = b [ start : end ] <EOL> del b [ start : end ] <EOL> if is_kill ( r . last_command ) : <EOL> if start < r . pos : <EOL> r . kill_ring [ - <NUM_LIT:1> ] = text + r . kill_ring [ - <NUM_LIT:1> ] <EOL> else : <EOL> r . kill_ring [ - <NUM_LIT:1> ] = r . kill_ring [ - <NUM_LIT:1> ] + text <EOL> else : <EOL> r . kill_ring . append ( text ) <EOL> r . pos = start <EOL> r . dirty = <NUM_LIT:1> <EOL> class YankCommand ( Command ) : <EOL> pass <EOL> class MotionCommand ( Command ) : <EOL> pass <EOL> class EditCommand ( Command ) : <EOL> pass <EOL> class FinishCommand ( Command ) : <EOL> finish = <NUM_LIT:1> <EOL> pass <EOL> def is_kill ( command ) : <EOL> return command and issubclass ( command , KillCommand ) <EOL> def is_yank ( command ) : <EOL> return command and issubclass ( command , YankCommand ) <EOL> class digit_arg ( Command ) : <EOL> kills_digit_arg = <NUM_LIT:0> <EOL> def do ( self ) : <EOL> r = self . reader <EOL> c = self . event [ - <NUM_LIT:1> ] <EOL> if c == "<STR_LIT:->" : <EOL> if r . arg is not None : <EOL> r . arg = - r . arg <EOL> else : <EOL> r . arg = - <NUM_LIT:1> <EOL> else : <EOL> d = int ( c ) <EOL> if r . arg is None : <EOL> r . arg = d <EOL> else : <EOL> if r . arg < <NUM_LIT:0> : <EOL> r . arg = <NUM_LIT:10> * r . arg - d <EOL> else : <EOL> r . arg = <NUM_LIT:10> * r . arg + d <EOL> r . dirty = <NUM_LIT:1> <EOL> class clear_screen ( Command ) : <EOL> def do ( self ) : <EOL> r = self . reader <EOL> r . console . clear ( ) <EOL> r . dirty = <NUM_LIT:1> <EOL> class refresh ( Command ) : <EOL> def do ( self ) : <EOL> self . reader . dirty = <NUM_LIT:1> <EOL> class repaint ( Command ) : <EOL> def do ( self ) : <EOL> self . reader . dirty = <NUM_LIT:1> <EOL> self . reader . console . repaint_prep ( ) <EOL> class kill_line ( KillCommand ) : <EOL> def do ( self ) : <EOL> r = self . reader <EOL> b = r . buffer <EOL> eol = r . eol ( ) <EOL> for c in b [ r . pos : eol ] : <EOL> if not c . isspace ( ) : <EOL> self . kill_range ( r . pos , eol ) <EOL> return <EOL> else : <EOL> self . kill_range ( r . pos , eol + <NUM_LIT:1> ) <EOL> class unix_line_discard ( KillCommand ) : <EOL> def do ( self ) : <EOL> r = self . reader <EOL> self . kill_range ( r . bol ( ) , r . pos ) <EOL> class unix_word_rubout ( KillCommand ) : <EOL> def do ( self ) : <EOL> r = self . reader <EOL> for i in range ( r . get_arg ( ) ) : <EOL> self . kill_range ( r . bow ( ) , r . pos ) <EOL> class kill_word ( KillCommand ) : <EOL> def do ( self ) : <EOL> r = self . reader <EOL> for i in range ( r . get_arg ( ) ) : <EOL> self . kill_range ( r . pos , r . eow ( ) ) <EOL> class backward_kill_word ( KillCommand ) : <EOL> def do ( self ) : <EOL> r = self . reader <EOL> for i in range ( r . get_arg ( ) ) : <EOL> self . kill_range ( r . bow ( ) , r . pos ) <EOL> class yank ( YankCommand ) : <EOL> def do ( self ) : <EOL> r = self . reader <EOL> if not r . kill_ring : <EOL> r . error ( "<STR_LIT>" ) <EOL> return <EOL> r . insert ( r . kill_ring [ - <NUM_LIT:1> ] ) <EOL> class yank_pop ( YankCommand ) : <EOL> def do ( self ) : <EOL> r = self . reader <EOL> b = r . buffer <EOL> if not r . kill_ring : <EOL> r . error ( "<STR_LIT>" ) <EOL> return <EOL> if not is_yank ( r . last_command ) : <EOL> r . error ( "<STR_LIT>" ) <EOL> return <EOL> repl = len ( r . kill_ring [ - <NUM_LIT:1> ] ) <EOL> r . kill_ring . insert ( <NUM_LIT:0> , r . kill_ring . pop ( ) ) <EOL> t = r . kill_ring [ - <NUM_LIT:1> ] <EOL> b [ r . pos - repl : r . pos ] = t <EOL> r . pos = r . pos - repl + len ( t ) <EOL> r . dirty = <NUM_LIT:1> <EOL> class interrupt ( FinishCommand ) : <EOL> def do ( self ) : <EOL> import signal <EOL> self . reader . console . finish ( ) <EOL> os . kill ( os . getpid ( ) , signal . SIGINT ) <EOL> class suspend ( Command ) : <EOL> def do ( self ) : <EOL> import signal <EOL> r = self . reader <EOL> p = r . pos <EOL> r . console . finish ( ) <EOL> os . kill ( os . getpid ( ) , signal . SIGSTOP ) <EOL> r . pos = p <EOL> r . posxy = <NUM_LIT:0> , <NUM_LIT:0> <EOL> r . dirty = <NUM_LIT:1> <EOL> r . console . screen = [ ] <EOL> class up ( MotionCommand ) : <EOL> def do ( self ) : <EOL> r = self . reader <EOL> for i in range ( r . get_arg ( ) ) : <EOL> bol1 = r . bol ( ) <EOL> if bol1 == <NUM_LIT:0> : <EOL> if r . historyi > <NUM_LIT:0> : <EOL> r . select_item ( r . historyi - <NUM_LIT:1> ) <EOL> return <EOL> r . pos = <NUM_LIT:0> <EOL> r . error ( "<STR_LIT>" ) <EOL> return <EOL> bol2 = r . bol ( bol1 - <NUM_LIT:1> ) <EOL> line_pos = r . pos - bol1 <EOL> if line_pos > bol1 - bol2 - <NUM_LIT:1> : <EOL> r . sticky_y = line_pos <EOL> r . pos = bol1 - <NUM_LIT:1> <EOL> else : <EOL> r . pos = bol2 + line_pos <EOL> class down ( MotionCommand ) : <EOL> def do ( self ) : <EOL> r = self . reader <EOL> b = r . buffer <EOL> for i in range ( r . get_arg ( ) ) : <EOL> bol1 = r . bol ( ) <EOL> eol1 = r . eol ( ) <EOL> if eol1 == len ( b ) : <EOL> if r . historyi < len ( r . history ) : <EOL> r . select_item ( r . historyi + <NUM_LIT:1> ) <EOL> r . pos = r . eol ( <NUM_LIT:0> ) <EOL> return <EOL> r . pos = len ( b ) <EOL> r . error ( "<STR_LIT>" ) <EOL> return <EOL> eol2 = r . eol ( eol1 + <NUM_LIT:1> ) <EOL> if r . pos - bol1 > eol2 - eol1 - <NUM_LIT:1> : <EOL> r . pos = eol2 <EOL> else : <EOL> r . pos = eol1 + ( r . pos - bol1 ) + <NUM_LIT:1> <EOL> class left ( MotionCommand ) : <EOL> def do ( self ) : <EOL> r = self . reader <EOL> for i in range ( r . get_arg ( ) ) : <EOL> p = r . pos - <NUM_LIT:1> <EOL> if p >= <NUM_LIT:0> : <EOL> r . pos = p <EOL> else : <EOL> self . reader . error ( "<STR_LIT>" ) <EOL> class right ( MotionCommand ) : <EOL> def do ( self ) : <EOL> r = self . reader <EOL> b = r . buffer <EOL> for i in range ( r . get_arg ( ) ) : <EOL> p = r . pos + <NUM_LIT:1> <EOL> if p <= len ( b ) : <EOL> r . pos = p <EOL> else : <EOL> self . reader . error ( "<STR_LIT>" ) <EOL> class beginning_of_line ( MotionCommand ) : <EOL> def do ( self ) : <EOL> self . reader . pos = self . reader . bol ( ) <EOL> class end_of_line ( MotionCommand ) : <EOL> def do ( self ) : <EOL> r = self . reader <EOL> self . reader . pos = self . reader . eol ( ) <EOL> class home ( MotionCommand ) : <EOL> def do ( self ) : <EOL> self . reader . pos = <NUM_LIT:0> <EOL> class end ( MotionCommand ) : <EOL> def do ( self ) : <EOL> self . reader . pos = len ( self . reader . buffer ) <EOL> class forward_word ( MotionCommand ) : <EOL> def do ( self ) : <EOL> r = self . reader <EOL> for i in range ( r . get_arg ( ) ) : <EOL> r . pos = r . eow ( ) <EOL> class backward_word ( MotionCommand ) : <EOL> def do ( self ) : <EOL> r = self . reader <EOL> for i in range ( r . get_arg ( ) ) : <EOL> r . pos = r . bow ( ) <EOL> class self_insert ( EditCommand ) : <EOL> def do ( self ) : <EOL> r = self . reader <EOL> r . insert ( self . event * r . get_arg ( ) ) <EOL> class insert_nl ( EditCommand ) : <EOL> def do ( self ) : <EOL> r = self . reader <EOL> r . insert ( "<STR_LIT:\n>" * r . get_arg ( ) ) <EOL> class transpose_characters ( EditCommand ) : <EOL> def do ( self ) : <EOL> r = self . reader <EOL> b = r . buffer <EOL> s = r . pos - <NUM_LIT:1> <EOL> if s < <NUM_LIT:0> : <EOL> r . error ( "<STR_LIT>" ) <EOL> else : <EOL> if s == len ( b ) : <EOL> s -= <NUM_LIT:1> <EOL> t = min ( s + r . get_arg ( ) , len ( b ) - <NUM_LIT:1> ) <EOL> c = b [ s ] <EOL> del b [ s ] <EOL> b . insert ( t , c ) <EOL> r . pos = t <EOL> r . dirty = <NUM_LIT:1> <EOL> class backspace ( EditCommand ) : <EOL> def do ( self ) : <EOL> r = self . reader <EOL> b = r . buffer <EOL> for i in range ( r . get_arg ( ) ) : <EOL> if r . pos > <NUM_LIT:0> : <EOL> r . pos -= <NUM_LIT:1> <EOL> del b [ r . pos ] <EOL> r . dirty = <NUM_LIT:1> <EOL> else : <EOL> self . reader . error ( "<STR_LIT>" ) <EOL> class delete ( EditCommand ) : <EOL> def do ( self ) : <EOL> r = self . reader <EOL> b = r . buffer <EOL> if ( r . pos == <NUM_LIT:0> and len ( b ) == <NUM_LIT:0> <EOL> and self . event [ - <NUM_LIT:1> ] == "<STR_LIT>" ) : <EOL> r . update_screen ( ) <EOL> r . console . finish ( ) <EOL> raise EOFError <EOL> for i in range ( r . get_arg ( ) ) : <EOL> if r . pos != len ( b ) : <EOL> del b [ r . pos ] <EOL> r . dirty = <NUM_LIT:1> <EOL> else : <EOL> self . reader . error ( "<STR_LIT>" ) <EOL> class accept ( FinishCommand ) : <EOL> def do ( self ) : <EOL> pass <EOL> class help ( Command ) : <EOL> def do ( self ) : <EOL> self . reader . msg = self . reader . help_text <EOL> self . reader . dirty = <NUM_LIT:1> <EOL> class invalid_key ( Command ) : <EOL> def do ( self ) : <EOL> pending = self . reader . console . getpending ( ) <EOL> s = '<STR_LIT>' . join ( self . event ) + pending . data <EOL> self . reader . error ( "<STR_LIT>" % s ) <EOL> class invalid_command ( Command ) : <EOL> def do ( self ) : <EOL> s = self . event_name <EOL> self . reader . error ( "<STR_LIT>" % s ) <EOL> class qIHelp ( Command ) : <EOL> def do ( self ) : <EOL> r = self . reader <EOL> r . insert ( ( self . event + r . console . getpending ( ) . data ) * r . get_arg ( ) ) <EOL> r . pop_input_trans ( ) <EOL> from pyrepl import input <EOL> class QITrans ( object ) : <EOL> def push ( self , evt ) : <EOL> self . evt = evt <EOL> def get ( self ) : <EOL> return ( '<STR_LIT>' , self . evt . raw ) <EOL> class quoted_insert ( Command ) : <EOL> kills_digit_arg = <NUM_LIT:0> <EOL> def do ( self ) : <EOL> self . reader . push_input_trans ( QITrans ( ) ) </s>
