<s> import sublime <EOL> import sublime_plugin <EOL> from . edit import Edit <EOL> from . view import ViewMeta <EOL> from . vim import Vim , VISUAL_MODES <EOL> class ActualVim ( ViewMeta ) : <EOL> def __init__ ( self , view ) : <EOL> super ( ) . __init__ ( view ) <EOL> if view . settings ( ) . get ( '<STR_LIT>' ) : <EOL> return <EOL> view . settings ( ) . set ( '<STR_LIT>' , True ) <EOL> view . settings ( ) . set ( '<STR_LIT>' , True ) <EOL> self . vim = vim = Vim ( view , update = self . update , modify = self . modify ) <EOL> vim . set_path ( view . file_name ( ) ) <EOL> vim . insert ( <NUM_LIT:0> , view . substr ( sublime . Region ( <NUM_LIT:0> , view . size ( ) ) ) ) <EOL> vim . init_done ( ) <EOL> self . output = None <EOL> @ property <EOL> def actual ( self ) : <EOL> return self . view and self . view . settings ( ) . get ( '<STR_LIT>' ) <EOL> def monitor ( self ) : <EOL> if self . output : <EOL> return <EOL> window = sublime . active_window ( ) <EOL> self . output = output = window . new_file ( ) <EOL> ActualVim . views [ output . id ( ) ] = self <EOL> output . settings ( ) . set ( '<STR_LIT>' , True ) <EOL> output . set_read_only ( True ) <EOL> output . set_scratch ( True ) <EOL> output . set_name ( '<STR_LIT>' ) <EOL> output . settings ( ) . set ( '<STR_LIT>' , True ) <EOL> output . settings ( ) . set ( '<STR_LIT>' , True ) <EOL> with Edit ( output ) as edit : <EOL> edit . insert ( <NUM_LIT:0> , self . vim . tty . dump ( ) ) <EOL> self . vim . monitor = output <EOL> if window . num_groups ( ) > <NUM_LIT:1> : <EOL> target = int ( not window . active_group ( ) ) <EOL> window . set_view_index ( output , target , <NUM_LIT:0> ) <EOL> def update ( self , vim , dirty , moved ) : <EOL> mode = vim . mode <EOL> view = vim . view <EOL> tty = vim . tty <EOL> if vim . cmdline : <EOL> view . set_status ( '<STR_LIT>' , vim . cmdline ) <EOL> else : <EOL> view . erase_status ( '<STR_LIT>' ) <EOL> if tty . row == tty . rows and tty . col > <NUM_LIT:0> : <EOL> char = tty . buf [ tty . row - <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> if char in '<STR_LIT>' : <EOL> if vim . panel : <EOL> panel = vim . panel . panel <EOL> with Edit ( panel ) as edit : <EOL> edit . replace ( sublime . Region ( <NUM_LIT:0> , panel . size ( ) ) , vim . cmdline ) <EOL> else : <EOL> row , col = ( tty . row - <NUM_LIT:1> , tty . col - <NUM_LIT:1> ) <EOL> vim . panel = ActualPanel ( self ) <EOL> vim . panel . show ( char ) <EOL> return <EOL> elif vim . panel : <EOL> vim . panel . close ( ) <EOL> vim . panel = None <EOL> if mode in VISUAL_MODES : <EOL> def select ( ) : <EOL> v = ActualVim . get ( view ) <EOL> start = vim . visual <EOL> end = ( vim . row , vim . col ) <EOL> regions = v . visual ( vim . mode , start , end ) <EOL> view . sel ( ) . clear ( ) <EOL> for r in regions : <EOL> view . sel ( ) . add ( sublime . Region ( * r ) ) <EOL> Edit . defer ( view , select ) <EOL> return <EOL> else : <EOL> vim . update_cursor ( ) <EOL> def modify ( self , vim ) : <EOL> pass <EOL> def close ( self , view ) : <EOL> if self . output : <EOL> self . output . close ( ) <EOL> self . output = None <EOL> if view == self . view : <EOL> self . view . close ( ) <EOL> self . vim . close ( ) <EOL> def set_path ( self , path ) : <EOL> self . vim . set_path ( path ) <EOL> class ActualKeypress ( sublime_plugin . TextCommand ) : <EOL> def run ( self , edit , key ) : <EOL> v = ActualVim . get ( self . view , exact = False ) <EOL> if v and v . actual : <EOL> v . vim . press ( key ) <EOL> class ActualListener ( sublime_plugin . EventListener ) : <EOL> def on_new_async ( self , view ) : <EOL> ActualVim . get ( view ) <EOL> def on_load ( self , view ) : <EOL> ActualVim . get ( view ) <EOL> def on_selection_modified_async ( self , view ) : <EOL> v = ActualVim . get ( view , create = False ) <EOL> if v and v . actual : <EOL> if not v . sel_changed ( ) : <EOL> return <EOL> sel = view . sel ( ) <EOL> if not sel : <EOL> return <EOL> vim = v . vim <EOL> sel = sel [ <NUM_LIT:0> ] <EOL> def cursor ( args ) : <EOL> buf , lnum , col , off = [ int ( a ) for a in args . split ( '<STR_LIT:U+0020>' ) ] <EOL> if vim . mode in VISUAL_MODES : <EOL> start = vim . visual <EOL> end = lnum , col + <NUM_LIT:1> <EOL> region = v . visual ( vim . mode , start , end ) [ <NUM_LIT:0> ] <EOL> if ( sel . a , sel . b ) == region : <EOL> return <EOL> if off == sel . b or off > view . size ( ) : <EOL> return <EOL> if sel . b == sel . a : <EOL> if vim . mode in VISUAL_MODES : <EOL> vim . press ( '<STR_LIT>' ) <EOL> vim . set_cursor ( sel . b , callback = vim . update_cursor ) <EOL> else : <EOL> return <EOL> if vim . mode != '<STR_LIT:n>' : <EOL> vim . press ( '<STR_LIT>' ) <EOL> a , b = sel . a , sel . b <EOL> if b > a : <EOL> a += <NUM_LIT:1> <EOL> else : <EOL> b += <NUM_LIT:1> <EOL> vim . type ( '<STR_LIT>' . format ( a , b ) ) <EOL> vim . get_cursor ( cursor ) <EOL> def on_modified ( self , view ) : <EOL> v = ActualVim . get ( view , create = False ) <EOL> if v : <EOL> v . sel_changed ( ) <EOL> def on_close ( self , view ) : <EOL> v = ActualVim . get ( view , create = False ) <EOL> if v : <EOL> v . close ( view ) <EOL> def on_post_save_async ( self , view ) : <EOL> v = ActualVim . get ( view , create = False ) <EOL> if v : <EOL> v . set_path ( view . file_name ( ) ) <EOL> class ActualPanel : <EOL> def __init__ ( self , actual ) : <EOL> self . actual = actual <EOL> self . vim = actual . vim <EOL> self . view = actual . view <EOL> self . panel = None <EOL> def close ( self ) : <EOL> if self . panel : <EOL> self . panel . close ( ) <EOL> def show ( self , char ) : <EOL> window = self . view . window ( ) <EOL> self . panel = window . show_input_panel ( '<STR_LIT>' , char , self . on_done , None , self . on_cancel ) <EOL> settings = self . panel . settings ( ) <EOL> settings . set ( '<STR_LIT>' , True ) <EOL> settings . set ( '<STR_LIT>' , self . view . id ( ) ) <EOL> ActualVim . views [ self . panel . id ( ) ] = self . actual <EOL> def on_done ( self , text ) : <EOL> self . vim . press ( '<STR_LIT>' ) <EOL> self . vim . panel = None <EOL> def on_cancel ( self ) : <EOL> self . vim . press ( '<STR_LIT>' ) <EOL> self . vim . panel = None </s>
