<s> from . configHandler import idleConf <EOL> class History : <EOL> def __init__ ( self , text , output_sep = "<STR_LIT:\n>" ) : <EOL> self . text = text <EOL> self . history = [ ] <EOL> self . history_prefix = None <EOL> self . history_pointer = None <EOL> self . output_sep = output_sep <EOL> self . cyclic = idleConf . GetOption ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:1> , "<STR_LIT:bool>" ) <EOL> text . bind ( "<STR_LIT>" , self . history_prev ) <EOL> text . bind ( "<STR_LIT>" , self . history_next ) <EOL> def history_next ( self , event ) : <EOL> self . history_do ( <NUM_LIT:0> ) <EOL> return "<STR_LIT>" <EOL> def history_prev ( self , event ) : <EOL> self . history_do ( <NUM_LIT:1> ) <EOL> return "<STR_LIT>" <EOL> def _get_source ( self , start , end ) : <EOL> lines = self . text . get ( start , end ) . split ( self . output_sep ) <EOL> return "<STR_LIT:\n>" . join ( lines ) <EOL> def _put_source ( self , where , source ) : <EOL> output = self . output_sep . join ( source . split ( "<STR_LIT:\n>" ) ) <EOL> self . text . insert ( where , output ) <EOL> def history_do ( self , reverse ) : <EOL> nhist = len ( self . history ) <EOL> pointer = self . history_pointer <EOL> prefix = self . history_prefix <EOL> if pointer is not None and prefix is not None : <EOL> if self . text . compare ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) or self . _get_source ( "<STR_LIT>" , "<STR_LIT>" ) != self . history [ pointer ] : <EOL> pointer = prefix = None <EOL> if pointer is None or prefix is None : <EOL> prefix = self . _get_source ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if reverse : <EOL> pointer = nhist <EOL> else : <EOL> if self . cyclic : <EOL> pointer = - <NUM_LIT:1> <EOL> else : <EOL> self . text . bell ( ) <EOL> return <EOL> nprefix = len ( prefix ) <EOL> while <NUM_LIT:1> : <EOL> if reverse : <EOL> pointer = pointer - <NUM_LIT:1> <EOL> else : <EOL> pointer = pointer + <NUM_LIT:1> <EOL> if pointer < <NUM_LIT:0> or pointer >= nhist : <EOL> self . text . bell ( ) <EOL> if not self . cyclic and pointer < <NUM_LIT:0> : <EOL> return <EOL> else : <EOL> if self . _get_source ( "<STR_LIT>" , "<STR_LIT>" ) != prefix : <EOL> self . text . delete ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . _put_source ( "<STR_LIT>" , prefix ) <EOL> pointer = prefix = None <EOL> break <EOL> item = self . history [ pointer ] <EOL> if item [ : nprefix ] == prefix and len ( item ) > nprefix : <EOL> self . text . delete ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . _put_source ( "<STR_LIT>" , item ) <EOL> break <EOL> self . text . mark_set ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . text . see ( "<STR_LIT>" ) <EOL> self . text . tag_remove ( "<STR_LIT>" , "<STR_LIT:1.0>" , "<STR_LIT:end>" ) <EOL> self . history_pointer = pointer <EOL> self . history_prefix = prefix <EOL> def history_store ( self , source ) : <EOL> source = source . strip ( ) <EOL> if len ( source ) > <NUM_LIT:2> : <EOL> try : <EOL> self . history . remove ( source ) <EOL> except ValueError : <EOL> pass <EOL> self . history . append ( source ) <EOL> self . history_pointer = None <EOL> self . history_prefix = None </s>
