<s> import sublime <EOL> import sublime_plugin <EOL> import re <EOL> def get_wrap_width ( view ) : <EOL> wrap_width = view . settings ( ) . get ( '<STR_LIT>' ) <EOL> if not wrap_width or wrap_width == <NUM_LIT:0> : <EOL> wrap_width = view . settings ( ) . get ( '<STR_LIT>' ) <EOL> if not wrap_width or wrap_width == <NUM_LIT:0> : <EOL> rulers = view . settings ( ) . get ( '<STR_LIT>' ) <EOL> if rulers : <EOL> wrap_width = rulers [ <NUM_LIT:0> ] <EOL> else : <EOL> wrap_width = <NUM_LIT> <EOL> return wrap_width <EOL> class AutoWrapListener ( sublime_plugin . EventListener ) : <EOL> cursor = ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> status = <NUM_LIT:0> <EOL> left_delete = False <EOL> def reset_status ( self ) : <EOL> self . status = <NUM_LIT:0> <EOL> def set_status ( self ) : <EOL> self . status = self . status + <NUM_LIT:1> <EOL> def check_selection ( self , view ) : <EOL> sel = view . sel ( ) <EOL> if len ( sel ) == <NUM_LIT:0> or len ( sel ) > <NUM_LIT:1> or not sel [ <NUM_LIT:0> ] . empty ( ) : <EOL> self . reset_status ( ) <EOL> return False <EOL> pt = sel [ <NUM_LIT:0> ] . end ( ) <EOL> rc = view . rowcol ( pt ) <EOL> if rc [ <NUM_LIT:0> ] != self . cursor [ <NUM_LIT:0> ] : <EOL> self . reset_status ( ) <EOL> if rc [ <NUM_LIT:0> ] != self . cursor [ <NUM_LIT:0> ] or rc [ <NUM_LIT:1> ] <= self . cursor [ <NUM_LIT:1> ] or rc [ <NUM_LIT:1> ] > self . cursor [ <NUM_LIT:1> ] + <NUM_LIT:1> : <EOL> self . cursor = view . rowcol ( sel [ <NUM_LIT:0> ] . end ( ) ) <EOL> return False <EOL> else : <EOL> self . cursor = view . rowcol ( sel [ <NUM_LIT:0> ] . end ( ) ) <EOL> return True <EOL> def get_insert_pt ( self , view ) : <EOL> sel = view . sel ( ) <EOL> pt = sel [ <NUM_LIT:0> ] . end ( ) <EOL> content = view . substr ( view . line ( pt ) ) <EOL> wrap_width = get_wrap_width ( view ) <EOL> if len ( content ) <= wrap_width : <EOL> return None <EOL> if view . settings ( ) . get ( '<STR_LIT>' , False ) : <EOL> if view . rowcol ( pt ) [ <NUM_LIT:1> ] < wrap_width : <EOL> return None <EOL> default = [ r"<STR_LIT>" , r"<STR_LIT>" , r"<STR_LIT>" , "<STR_LIT:U+0020>" , r"<STR_LIT:\n>" ] <EOL> if view . score_selector ( pt , "<STR_LIT>" ) : <EOL> default = [ r"<STR_LIT>" , r"<STR_LIT>" , r"<STR_LIT>" ] + default <EOL> break_chars = "<STR_LIT:|>" . join ( view . settings ( ) . get ( '<STR_LIT>' , default ) ) <EOL> results = re . finditer ( break_chars , content ) <EOL> indices = [ m . start ( <NUM_LIT:0> ) for m in results ] + [ len ( content ) ] <EOL> index = next ( x [ <NUM_LIT:0> ] for x in enumerate ( indices ) if x [ <NUM_LIT:1> ] > wrap_width ) <EOL> if view . settings ( ) . get ( "<STR_LIT>" , True ) and index > <NUM_LIT:0> : <EOL> return view . line ( pt ) . begin ( ) + indices [ index - <NUM_LIT:1> ] <EOL> else : <EOL> if index == len ( indices ) - <NUM_LIT:1> : <EOL> return None <EOL> return view . line ( pt ) . begin ( ) + indices [ index ] <EOL> def on_modified ( self , view ) : <EOL> if view . settings ( ) . get ( '<STR_LIT>' ) : <EOL> return <EOL> if not view . settings ( ) . get ( '<STR_LIT>' , False ) : <EOL> return <EOL> if not self . check_selection ( view ) : <EOL> return <EOL> insertpt = self . get_insert_pt ( view ) <EOL> if not insertpt : <EOL> return <EOL> self . set_status ( ) <EOL> join = self . status >= <NUM_LIT:2> <EOL> left_delete = "<STR_LIT:U+0020>" not in view . substr ( sublime . Region ( insertpt - <NUM_LIT:1> , insertpt + <NUM_LIT:1> ) ) <EOL> view . settings ( ) . set ( '<STR_LIT>' , False ) <EOL> view . run_command ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : insertpt , '<STR_LIT>' : join , "<STR_LIT>" : self . left_delete <EOL> } ) <EOL> self . left_delete = left_delete <EOL> view . settings ( ) . set ( '<STR_LIT>' , True ) <EOL> def on_post_text_command ( self , view , command_name , args ) : <EOL> if view . settings ( ) . get ( '<STR_LIT>' ) : <EOL> return <EOL> if not view . settings ( ) . get ( '<STR_LIT>' , False ) : <EOL> return <EOL> if command_name in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> self . reset_status ( ) <EOL> def on_deactivated ( self , view ) : <EOL> if view . settings ( ) . get ( '<STR_LIT>' ) : <EOL> return <EOL> if not view . settings ( ) . get ( '<STR_LIT>' , False ) : <EOL> return <EOL> self . reset_status ( ) <EOL> class AutoWrapInsertCommand ( sublime_plugin . TextCommand ) : <EOL> def run ( self , edit , insertpt , join = False , left_delete = False ) : <EOL> view = self . view <EOL> insertpt = int ( insertpt ) <EOL> insertpt_row = view . rowcol ( insertpt ) [ <NUM_LIT:0> ] <EOL> iscomment = view . score_selector ( insertpt - <NUM_LIT:1> , "<STR_LIT>" ) > <NUM_LIT:0> and view . score_selector ( insertpt - <NUM_LIT:1> , "<STR_LIT>" ) == <NUM_LIT:0> <EOL> view . insert ( edit , insertpt , "<STR_LIT:\n>" ) <EOL> view . add_regions ( "<STR_LIT>" , [ s for s in view . sel ( ) ] , "<STR_LIT>" ) <EOL> if join and iscomment : <EOL> view . sel ( ) . clear ( ) <EOL> view . sel ( ) . add ( view . text_point ( insertpt_row + <NUM_LIT:2> , <NUM_LIT:0> ) ) <EOL> view . run_command ( '<STR_LIT>' , { "<STR_LIT>" : False } ) <EOL> view . sel ( ) . clear ( ) <EOL> view . sel ( ) . add ( sublime . Region ( insertpt + <NUM_LIT:1> , insertpt + <NUM_LIT:1> ) ) <EOL> if join : <EOL> view . run_command ( '<STR_LIT>' ) <EOL> pt = view . sel ( ) [ <NUM_LIT:0> ] . end ( ) <EOL> if left_delete and view . substr ( sublime . Region ( pt - <NUM_LIT:1> , pt ) ) == "<STR_LIT:U+0020>" : <EOL> view . run_command ( "<STR_LIT>" ) <EOL> if view . settings ( ) . get ( '<STR_LIT>' ) : <EOL> view . run_command ( '<STR_LIT>' , { '<STR_LIT>' : False } ) <EOL> if iscomment : <EOL> view . run_command ( '<STR_LIT>' , { "<STR_LIT>" : False } ) <EOL> view . sel ( ) . clear ( ) <EOL> for s in view . get_regions ( "<STR_LIT>" ) : <EOL> view . sel ( ) . add ( s ) <EOL> view . erase_regions ( "<STR_LIT>" ) <EOL> class ToggleAutoWrap ( sublime_plugin . WindowCommand ) : <EOL> def run ( self ) : <EOL> view = self . window . active_view ( ) <EOL> view . settings ( ) . set ( "<STR_LIT>" , not view . settings ( ) . get ( "<STR_LIT>" , False ) ) <EOL> onoff = "<STR_LIT>" if view . settings ( ) . get ( "<STR_LIT>" ) else "<STR_LIT>" <EOL> sublime . status_message ( "<STR_LIT>" % onoff ) </s>
