<s> import os <EOL> import sublime <EOL> import sublime_plugin <EOL> class MarkAndMoveWindowCommand ( sublime_plugin . WindowCommand ) : <EOL> mark_and_move_views = { } <EOL> mark_and_move_files = { } <EOL> mark_and_move_selections = { } <EOL> def remember_file_name ( self , view , file_name ) : <EOL> ids = [ view_id for view_id in self . mark_and_move_files if self . mark_and_move_files [ view_id ] == file_name ] <EOL> if ids : <EOL> new_view_id = view . id ( ) <EOL> old_view_id = ids [ <NUM_LIT:0> ] <EOL> self . fix_ids ( new_view_id , old_view_id ) <EOL> def lookup_view ( self , from_view , goto_view_id ) : <EOL> goto_view = None <EOL> goto_views = [ check_view for check_view in self . window . views ( ) if check_view . id ( ) == goto_view_id ] <EOL> if goto_views : <EOL> goto_view = goto_views [ <NUM_LIT:0> ] <EOL> elif goto_view_id in self . mark_and_move_files : <EOL> file_name = self . mark_and_move_files [ goto_view_id ] <EOL> if os . path . isfile ( file_name ) : <EOL> goto_view = self . window . open_file ( file_name ) <EOL> if goto_view is None : <EOL> return <EOL> del self . mark_and_move_files [ goto_view_id ] <EOL> self . mark_and_move_files [ goto_view . id ( ) ] = file_name <EOL> self . fix_ids ( goto_view . id ( ) , goto_view_id , from_view ) <EOL> return goto_view <EOL> def fix_ids ( self , new_view_id , old_view_id , from_view = None ) : <EOL> if new_view_id != old_view_id : <EOL> if old_view_id in self . mark_and_move_views : <EOL> self . mark_and_move_views [ new_view_id ] = self . mark_and_move_views [ old_view_id ] <EOL> del self . mark_and_move_views [ old_view_id ] <EOL> if from_view : <EOL> for from_id , to_ids in self . mark_and_move_selections . iteritems ( ) : <EOL> new_ids = [ ] <EOL> for to_id in to_ids : <EOL> if to_id == old_view_id : <EOL> new_ids . append ( new_view_id ) <EOL> from_view_id = from_view . id ( ) <EOL> key = '<STR_LIT>' % ( from_view_id , old_view_id ) <EOL> new_key = '<STR_LIT>' % ( from_view_id , new_view_id ) <EOL> new_regions = from_view . get_regions ( key ) <EOL> from_view . add_regions ( <EOL> new_key , <EOL> new_regions , <EOL> '<STR_LIT:source>' , <EOL> '<STR_LIT>' , <EOL> sublime . HIDDEN <EOL> ) <EOL> from_view . erase_regions ( key ) <EOL> else : <EOL> new_ids . append ( to_id ) <EOL> self . mark_and_move_selections [ from_id ] = new_ids <EOL> else : <EOL> if old_view_id in self . mark_and_move_selections : <EOL> del self . mark_and_move_selections [ old_view_id ] <EOL> for from_id , to_ids in self . mark_and_move_selections . iteritems ( ) : <EOL> new_ids = [ ] <EOL> for to_id in to_ids : <EOL> if to_id == old_view_id : <EOL> new_ids . append ( to_id ) <EOL> self . mark_and_move_selections [ from_id ] = new_ids <EOL> for from_id , to_id in self . mark_and_move_views . iteritems ( ) : <EOL> if to_id == old_view_id : <EOL> self . mark_and_move_views [ from_id ] = new_view_id <EOL> class MarkAndMoveWindowSelectCommand ( MarkAndMoveWindowCommand ) : <EOL> def run ( self , goto = True ) : <EOL> view = self . window . active_view ( ) <EOL> if view . file_name ( ) : <EOL> self . mark_and_move_files [ view . id ( ) ] = view . file_name ( ) <EOL> my_id = view . id ( ) <EOL> files = [ ] <EOL> views = [ ] <EOL> untitled_count = <NUM_LIT:1> <EOL> for v in view . window ( ) . views ( ) : <EOL> if v . id ( ) != my_id : <EOL> views . append ( v ) <EOL> if v . file_name ( ) : <EOL> files . append ( v . file_name ( ) ) <EOL> elif v . name ( ) : <EOL> files . append ( v . name ( ) ) <EOL> else : <EOL> files . append ( '<STR_LIT>' % untitled_count ) <EOL> untitled_count += <NUM_LIT:1> <EOL> def on_done ( index ) : <EOL> if index > - <NUM_LIT:1> and len ( views ) > index : <EOL> goto_view = views [ index ] <EOL> if goto_view . id ( ) not in self . mark_and_move_views : <EOL> self . mark_and_move_views [ goto_view . id ( ) ] = view . id ( ) <EOL> if goto_view . file_name ( ) : <EOL> self . mark_and_move_files [ goto_view . id ( ) ] = goto_view . file_name ( ) <EOL> if all ( region . empty ( ) for region in view . sel ( ) ) : <EOL> self . mark_and_move_views [ view . id ( ) ] = goto_view . id ( ) <EOL> else : <EOL> if view . id ( ) not in self . mark_and_move_selections : <EOL> self . mark_and_move_selections [ view . id ( ) ] = [ ] <EOL> self . mark_and_move_selections [ view . id ( ) ] . append ( goto_view . id ( ) ) <EOL> regions = list ( filter ( bool , view . sel ( ) ) ) <EOL> key = '<STR_LIT>' % ( view . id ( ) , goto_view . id ( ) ) <EOL> regions . extend ( view . get_regions ( key ) ) <EOL> view . add_regions ( <EOL> key , <EOL> regions , <EOL> '<STR_LIT:source>' , <EOL> '<STR_LIT>' , <EOL> sublime . HIDDEN <EOL> ) <EOL> if goto : <EOL> self . window . focus_view ( goto_view ) <EOL> if len ( files ) == <NUM_LIT:1> : <EOL> on_done ( <NUM_LIT:0> ) <EOL> else : <EOL> menu_items = [ os . path . basename ( f ) for f in files ] <EOL> view . window ( ) . show_quick_panel ( menu_items , on_done ) <EOL> class MarkAndMoveWindowToggleCommand ( MarkAndMoveWindowCommand ) : <EOL> def run ( self , * args , ** kwargs ) : <EOL> view = self . window . active_view ( ) <EOL> if view . file_name ( ) and any ( view . id ( ) != view_id and view . file_name ( ) == file_name for view_id , file_name in self . mark_and_move_files . items ( ) ) : <EOL> self . remember_file_name ( view , view . file_name ( ) ) <EOL> view_id = view . id ( ) <EOL> cursor = view . sel ( ) [ <NUM_LIT:0> ] . b <EOL> if view_id in self . mark_and_move_selections : <EOL> for goto_view_id in self . mark_and_move_selections [ view_id ] : <EOL> key = '<STR_LIT>' % ( view_id , goto_view_id ) <EOL> check_regions = view . get_regions ( key ) <EOL> for check_region in check_regions : <EOL> if check_region . contains ( cursor ) : <EOL> goto_view = self . lookup_view ( view , goto_view_id ) <EOL> if goto_view is not None : <EOL> self . window . focus_view ( goto_view ) <EOL> return <EOL> if all ( region . empty ( ) for region in view . sel ( ) ) and view_id in self . mark_and_move_views : <EOL> goto_view_id = self . mark_and_move_views [ view_id ] <EOL> goto_view = self . lookup_view ( view , goto_view_id ) <EOL> if goto_view is not None : <EOL> self . window . focus_view ( goto_view ) <EOL> if self . window . active_view ( ) . id ( ) != goto_view . id ( ) : <EOL> del self . mark_and_move_views [ view_id ] <EOL> del self . mark_and_move_views [ goto_view . id ( ) ] <EOL> self . window . run_command ( '<STR_LIT>' , { '<STR_LIT>' : True } ) <EOL> else : <EOL> self . window . run_command ( '<STR_LIT>' , { '<STR_LIT>' : True } ) <EOL> class MarkAndMoveSaveCommand ( sublime_plugin . TextCommand ) : <EOL> def run ( self , edit ) : <EOL> mark_and_move_marks = self . view . get_regions ( '<STR_LIT>' ) <EOL> for region in self . view . sel ( ) : <EOL> mark_and_move_marks . append ( region ) <EOL> self . view . add_regions ( <EOL> '<STR_LIT>' , <EOL> mark_and_move_marks , <EOL> '<STR_LIT:source>' , <EOL> '<STR_LIT>' , <EOL> sublime . DRAW_OUTLINED <EOL> ) <EOL> class MarkAndMoveRecallCommand ( sublime_plugin . TextCommand ) : <EOL> def run ( self , edit , clear = False ) : <EOL> mark_and_move_marks = self . view . get_regions ( '<STR_LIT>' ) <EOL> if not mark_and_move_marks : <EOL> return <EOL> self . view . sel ( ) . clear ( ) <EOL> for region in mark_and_move_marks : <EOL> self . view . sel ( ) . add ( region ) <EOL> if clear : <EOL> mark_and_move_marks = [ ] <EOL> self . view . erase_regions ( '<STR_LIT>' ) <EOL> class MarkAndMoveSaveAndRecallCommand ( sublime_plugin . TextCommand ) : <EOL> def run ( self , edit , clear = False ) : <EOL> self . view . run_command ( '<STR_LIT>' ) <EOL> self . view . run_command ( '<STR_LIT>' , { "<STR_LIT>" : clear } ) <EOL> class MarkAndMoveClearCommand ( sublime_plugin . TextCommand ) : <EOL> def run ( self , edit ) : <EOL> mark_and_move_marks = self . view . get_regions ( '<STR_LIT>' ) <EOL> if not mark_and_move_marks : <EOL> return <EOL> mark_and_move_marks = [ ] <EOL> self . view . erase_regions ( '<STR_LIT>' ) <EOL> class MarkAndMoveDoItAllCommand ( sublime_plugin . TextCommand ) : <EOL> """<STR_LIT>""" <EOL> def run ( self , edit ) : <EOL> mark_and_move_marks = self . view . get_regions ( '<STR_LIT>' ) <EOL> def region_in ( region , marks ) : <EOL> for mark in marks : <EOL> if region . begin ( ) == mark . begin ( ) and region . end ( ) == mark . end ( ) : <EOL> return True <EOL> return False <EOL> if any ( not region_in ( region , mark_and_move_marks ) for region in self . view . sel ( ) ) : <EOL> self . view . run_command ( '<STR_LIT>' ) <EOL> else : <EOL> self . view . run_command ( '<STR_LIT>' , { "<STR_LIT>" : True } ) <EOL> class MarkAndMoveRotateCommand ( sublime_plugin . TextCommand ) : <EOL> def rotate ( self , edit , direction ) : <EOL> mark_and_move_marks = self . view . get_regions ( '<STR_LIT>' ) <EOL> if not mark_and_move_marks : <EOL> return <EOL> current_region = self . view . sel ( ) [ <NUM_LIT:0> ] <EOL> if direction > <NUM_LIT:0> : <EOL> next_region = mark_and_move_marks [ <NUM_LIT:0> ] <EOL> find = mark_and_move_marks <EOL> else : <EOL> next_region = mark_and_move_marks [ - <NUM_LIT:1> ] <EOL> find = mark_and_move_marks [ : : - <NUM_LIT:1> ] <EOL> for test_region in find : <EOL> if direction > <NUM_LIT:0> and test_region != current_region and test_region . begin ( ) >= current_region . begin ( ) : <EOL> next_region = test_region <EOL> break <EOL> elif direction < <NUM_LIT:0> and test_region != current_region and test_region . begin ( ) <= current_region . begin ( ) : <EOL> next_region = test_region <EOL> break <EOL> self . view . sel ( ) . clear ( ) <EOL> self . view . sel ( ) . add ( next_region ) <EOL> self . view . show ( next_region . b ) <EOL> class MarkAndMoveNextCommand ( MarkAndMoveRotateCommand ) : <EOL> def run ( self , edit ) : <EOL> return self . rotate ( edit , + <NUM_LIT:1> ) <EOL> class MarkAndMovePrevCommand ( MarkAndMoveRotateCommand ) : <EOL> def run ( self , edit ) : <EOL> return self . rotate ( edit , - <NUM_LIT:1> ) </s>
