<s> from time import sleep , time <EOL> from . import vfunc , vim <EOL> from . utils import get_key_code , get_key , redraw <EOL> TIME_SLICE = <NUM_LIT> <EOL> waiting_loops = [ ] <EOL> def pop ( ) : <EOL> waiting_loops . pop ( ) . enter ( ) <EOL> class Loop ( object ) : <EOL> def __init__ ( self , feedkeys ) : <EOL> self . tasks = [ ] <EOL> self . handlers = { } <EOL> self . do_release = False <EOL> self . reenter = False <EOL> self . feedkeys = feedkeys <EOL> self . is_lazy = False <EOL> def enter ( self ) : <EOL> self . is_lazy = vim . eval ( '<STR_LIT>' ) <EOL> while True : <EOL> key , is_special = get_key ( ) <EOL> if key : <EOL> if is_special : <EOL> if key in self . handlers : <EOL> h , a = self . handlers [ key ] <EOL> h ( * a ) <EOL> else : <EOL> if '<STR_LIT>' in self . handlers : <EOL> h , a = self . handlers [ '<STR_LIT>' ] <EOL> h ( key , * a ) <EOL> else : <EOL> t = time ( ) <EOL> self . reenter = True <EOL> while self . tasks : <EOL> task = self . tasks . pop ( <NUM_LIT:0> ) <EOL> try : <EOL> next ( task ) <EOL> except StopIteration : <EOL> self . do_release = True <EOL> else : <EOL> self . tasks . append ( task ) <EOL> tt = time ( ) <EOL> if tt - t > TIME_SLICE : <EOL> break <EOL> else : <EOL> sleep ( TIME_SLICE ) <EOL> if self . do_release : <EOL> self . do_release = False <EOL> if self . reenter : <EOL> waiting_loops . append ( self ) <EOL> vfunc . feedkeys ( self . feedkeys ) <EOL> if self . is_lazy : <EOL> redraw ( ) <EOL> return <EOL> def refresh ( self ) : <EOL> self . do_release = True <EOL> self . reenter = True <EOL> def exit ( self ) : <EOL> self . do_release = True <EOL> self . reenter = False <EOL> def idle ( self , generator ) : <EOL> self . tasks . append ( generator ) <EOL> def on_key ( self , key , handler , * args ) : <EOL> self . handlers [ get_key_code ( key ) ] = ( handler , args ) <EOL> def on_printable ( self , handler , * args ) : <EOL> self . handlers [ '<STR_LIT>' ] = ( handler , args ) </s>
