<s> import sublime <EOL> import sublime_plugin <EOL> _ST3 = sublime . version ( ) >= "<STR_LIT>" <EOL> if _ST3 : <EOL> _FLAGS = sublime . DRAW_EMPTY | sublime . DRAW_NO_FILL <EOL> else : <EOL> _FLAGS = sublime . DRAW_EMPTY | sublime . DRAW_OUTLINED <EOL> _SCOPE = "<STR_LIT>" <EOL> def _set_fields ( view , regions ) : <EOL> """<STR_LIT>""" <EOL> if _ST3 : <EOL> view . add_regions ( "<STR_LIT>" , regions , scope = _SCOPE , <EOL> flags = _FLAGS ) <EOL> else : <EOL> view . add_regions ( "<STR_LIT>" , regions , _SCOPE , _FLAGS ) <EOL> def _change_selection ( view , regions , pos ) : <EOL> """<STR_LIT>""" <EOL> sel = regions [ pos ] <EOL> del regions [ pos ] <EOL> _set_fields ( view , regions ) <EOL> if len ( regions ) >= <NUM_LIT:1> : <EOL> view . set_status ( "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> . format ( pos + <NUM_LIT:1> , len ( regions ) + <NUM_LIT:1> ) ) <EOL> else : <EOL> view . erase_status ( "<STR_LIT>" ) <EOL> sel_regions = [ sel ] <EOL> return sel_regions <EOL> def _restore_selection ( view , only_other ) : <EOL> """<STR_LIT>""" <EOL> sel_regions = view . get_regions ( "<STR_LIT>" ) <EOL> if not only_other : <EOL> sel_regions . extend ( view . sel ( ) ) <EOL> view . erase_regions ( "<STR_LIT>" ) <EOL> view . erase_status ( "<STR_LIT>" ) <EOL> return sel_regions <EOL> def _execute_jump ( view , jump_forward , only_other ) : <EOL> """<STR_LIT>""" <EOL> regions = view . get_regions ( "<STR_LIT>" ) <EOL> try : <EOL> end = max ( sel . end ( ) for sel in view . sel ( ) ) <EOL> pos = next ( i for i , sel in enumerate ( regions ) if sel . begin ( ) > end ) <EOL> except : <EOL> pos = len ( regions ) <EOL> if only_other : <EOL> sel_count = <NUM_LIT:0> <EOL> else : <EOL> sel_count = len ( view . sel ( ) ) <EOL> if sel_count == <NUM_LIT:1> : <EOL> regions . insert ( pos , view . sel ( ) [ <NUM_LIT:0> ] ) <EOL> else : <EOL> regions = regions [ : pos ] + list ( view . sel ( ) ) + regions [ pos : ] <EOL> delta = sel_count if jump_forward else - <NUM_LIT:1> <EOL> pos = pos + delta <EOL> return regions , pos <EOL> def _subtract_selection ( pushed_regions , sel_regions ) : <EOL> """<STR_LIT>""" <EOL> for reg in pushed_regions : <EOL> for sel in sel_regions : <EOL> if sel . begin ( ) <= reg . end ( ) and reg . begin ( ) <= sel . end ( ) : <EOL> if reg . begin ( ) < sel . begin ( ) : <EOL> yield sublime . Region ( reg . begin ( ) , sel . begin ( ) ) <EOL> reg = sublime . Region ( sel . end ( ) , reg . end ( ) ) <EOL> if not reg . a < reg . b : <EOL> break <EOL> else : <EOL> yield reg <EOL> _valid_modes = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] <EOL> class SelectionFieldsCommand ( sublime_plugin . TextCommand ) : <EOL> def run ( self , edit , mode = "<STR_LIT>" , jump_forward = True , only_other = False ) : <EOL> if mode not in _valid_modes : <EOL> sublime . error_message ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> . format ( mode , "<STR_LIT:U+002CU+0020>" . join ( _valid_modes ) ) <EOL> ) <EOL> return <EOL> view = self . view <EOL> has_regions = bool ( view . get_regions ( "<STR_LIT>" ) ) <EOL> do_push = { <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT>" : False <EOL> } . get ( mode , not has_regions ) <EOL> sel_regions = None <EOL> if do_push : <EOL> sels = list ( view . sel ( ) ) <EOL> border_pos = <NUM_LIT:0> if jump_forward else len ( sels ) - <NUM_LIT:1> <EOL> sel_regions = _change_selection ( view , sels , border_pos ) <EOL> elif mode == "<STR_LIT>" : <EOL> sel_regions = list ( view . sel ( ) ) <EOL> pushed_regions = view . get_regions ( "<STR_LIT>" ) <EOL> regions = list ( _subtract_selection ( pushed_regions , sel_regions ) ) <EOL> _set_fields ( view , regions ) <EOL> elif mode == "<STR_LIT>" : <EOL> pushed_regions = view . get_regions ( "<STR_LIT>" ) <EOL> sel_regions = list ( view . sel ( ) ) <EOL> _set_fields ( view , sel_regions + pushed_regions ) <EOL> elif mode == "<STR_LIT>" : <EOL> pop_regions = _restore_selection ( view , only_other ) <EOL> sel_regions = list ( view . sel ( ) ) if not only_other else pop_regions <EOL> elif mode not in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> sel_regions = _restore_selection ( view , only_other ) <EOL> else : <EOL> regions , pos = _execute_jump ( view , jump_forward , only_other ) <EOL> if mode == "<STR_LIT>" : <EOL> pos = pos % len ( regions ) <EOL> pos_valid = pos == pos % len ( regions ) <EOL> if pos_valid : <EOL> sel_regions = _change_selection ( view , regions , pos ) <EOL> else : <EOL> sel_regions = _restore_selection ( view , only_other ) <EOL> if sel_regions : <EOL> view . sel ( ) . clear ( ) <EOL> if _ST3 : <EOL> view . sel ( ) . add_all ( sel_regions ) <EOL> else : <EOL> for sel in sel_regions : <EOL> view . sel ( ) . add ( sel ) <EOL> view . show ( sel_regions [ <NUM_LIT:0> ] ) <EOL> class SelectionFieldsContext ( sublime_plugin . EventListener ) : <EOL> def on_query_context ( self , view , key , operator , operand , match_all ) : <EOL> if key not in [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] : <EOL> return False <EOL> if key == "<STR_LIT>" : <EOL> result = bool ( view . get_regions ( "<STR_LIT>" ) ) <EOL> else : <EOL> settings = sublime . load_settings ( "<STR_LIT>" ) <EOL> result = settings . get ( key , False ) <EOL> if operator == sublime . OP_EQUAL : <EOL> result = result == operand <EOL> elif operator == sublime . OP_NOT_EQUAL : <EOL> result = result != operand <EOL> else : <EOL> raise Exception ( "<STR_LIT>" . format ( operator ) ) <EOL> return result <EOL> class MeuPopupVisibleProxyContext ( sublime_plugin . EventListener ) : <EOL> def on_query_context ( self , view , key , operator , operand , match_all ) : <EOL> if key != "<STR_LIT>" : <EOL> return False <EOL> if hasattr ( view , "<STR_LIT>" ) : <EOL> result = view . is_popup_visible ( ) <EOL> else : <EOL> result = False <EOL> if operator == sublime . OP_EQUAL : <EOL> result = result == operand <EOL> elif operator == sublime . OP_NOT_EQUAL : <EOL> result = result != operand <EOL> else : <EOL> raise Exception ( "<STR_LIT>" . format ( operator ) ) <EOL> return result </s>
