<s> import os . path , pipes , re , subprocess , tempfile <EOL> import sublime , sublime_plugin <EOL> from functools import partial <EOL> def cwd_for_window ( window ) : <EOL> """<STR_LIT>""" <EOL> folders = window . folders ( ) <EOL> if len ( folders ) == <NUM_LIT:1> : <EOL> return folders [ <NUM_LIT:0> ] <EOL> else : <EOL> active_view = window . active_view ( ) <EOL> active_file_name = active_view . file_name ( ) if active_view else None <EOL> if not active_file_name : <EOL> return folders [ <NUM_LIT:0> ] if len ( folders ) else os . path . expanduser ( "<STR_LIT>" ) <EOL> for folder in folders : <EOL> if active_file_name . startswith ( folder ) : <EOL> return folder <EOL> return os . path . dirname ( active_file_name ) <EOL> def abbreviate_user ( path ) : <EOL> """<STR_LIT>""" <EOL> home_dir = os . path . expanduser ( "<STR_LIT>" ) <EOL> if path . startswith ( home_dir ) : <EOL> return "<STR_LIT>" + path [ len ( home_dir ) : ] <EOL> else : <EOL> return path <EOL> def settings ( ) : <EOL> return sublime . load_settings ( '<STR_LIT>' ) <EOL> def cmd_settings ( cmd ) : <EOL> """<STR_LIT>""" <EOL> d = { } <EOL> for setting in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> d [ setting ] = settings ( ) . get ( setting ) <EOL> try : <EOL> settings_for_cmd = next ( ( c for c <EOL> in settings ( ) . get ( '<STR_LIT>' ) <EOL> if re . search ( c [ '<STR_LIT>' ] , cmd ) ) ) <EOL> d . update ( settings_for_cmd ) <EOL> except StopIteration : <EOL> pass <EOL> return d <EOL> def parse_cmd ( cmd_str ) : <EOL> return re . match ( <EOL> r"<STR_LIT>" , <EOL> cmd_str <EOL> ) . groupdict ( ) <EOL> def run_cmd ( cwd , cmd , wait , input_str = None ) : <EOL> shell = isinstance ( cmd , str ) <EOL> if wait : <EOL> proc = subprocess . Popen ( cmd , cwd = cwd , <EOL> shell = shell , <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . PIPE , <EOL> stdin = ( subprocess . PIPE if input_str else None ) ) <EOL> encoded_input = None if input_str == None else input_str . encode ( '<STR_LIT:utf8>' ) <EOL> output , error = proc . communicate ( encoded_input ) <EOL> return_code = proc . poll ( ) <EOL> if return_code : <EOL> show_in_output_panel ( "<STR_LIT>" <EOL> % ( cmd , return_code , error ) ) <EOL> return ( False , None ) <EOL> else : <EOL> return ( True , output . decode ( '<STR_LIT:utf8>' ) ) <EOL> else : <EOL> subprocess . Popen ( cmd , cwd = cwd , shell = shell ) <EOL> return ( False , None ) <EOL> def show_in_output_panel ( message ) : <EOL> window = sublime . active_window ( ) <EOL> panel_name = '<STR_LIT>' <EOL> panel = window . get_output_panel ( panel_name ) <EOL> edit = panel . begin_edit ( ) <EOL> panel . insert ( edit , <NUM_LIT:0> , message ) <EOL> panel . end_edit ( edit ) <EOL> window . run_command ( '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' + panel_name } ) <EOL> class ShellPromptCommand ( sublime_plugin . WindowCommand ) : <EOL> """<STR_LIT>""" <EOL> def run ( self , ** args ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . cmd_history = [ ] <EOL> cwd = cwd_for_window ( self . window ) <EOL> args . setdefault ( "<STR_LIT>" , False ) <EOL> if len ( self . cmd_history ) > <NUM_LIT:0> and args [ "<STR_LIT>" ] : <EOL> self . on_done ( cwd , self . cmd_history [ - <NUM_LIT:1> ] ) <EOL> else : <EOL> on_done = partial ( self . on_done , cwd ) <EOL> inputview = show_input_panel_with_readline ( self . window , <EOL> abbreviate_user ( cwd ) + "<STR_LIT>" , <EOL> self . cmd_history , <EOL> on_done , None , None ) <EOL> for ( setting , value ) in list ( settings ( ) . get ( '<STR_LIT>' ) . items ( ) ) : <EOL> inputview . settings ( ) . set ( setting , value ) <EOL> def on_done ( self , cwd , cmd_str ) : <EOL> cmd = parse_cmd ( cmd_str ) <EOL> if not cmd [ '<STR_LIT:input>' ] and cmd [ '<STR_LIT>' ] == '<STR_LIT:|>' : <EOL> sublime . error_message ( <EOL> "<STR_LIT>" <EOL> + "<STR_LIT>" ) <EOL> return <EOL> active_view = self . window . active_view ( ) <EOL> if cmd [ '<STR_LIT:input>' ] or cmd [ '<STR_LIT>' ] == '<STR_LIT:|>' : <EOL> if not active_view : <EOL> sublime . error_message ( <EOL> "<STR_LIT>" ) <EOL> return <EOL> settings = cmd_settings ( cmd [ '<STR_LIT>' ] ) <EOL> before , after = settings [ '<STR_LIT>' ] <EOL> shell_cmd = before + cmd [ '<STR_LIT>' ] + after <EOL> if cmd [ '<STR_LIT:input>' ] : <EOL> input_regions = [ sel for sel in active_view . sel ( ) if sel . size ( ) > <NUM_LIT:0> ] <EOL> if len ( input_regions ) == <NUM_LIT:0> : <EOL> input_regions = [ sublime . Region ( <NUM_LIT:0> , active_view . size ( ) ) ] <EOL> else : <EOL> input_regions = None <EOL> if cmd [ '<STR_LIT>' ] : <EOL> for region in ( input_regions or [ None ] ) : <EOL> self . process_region ( active_view , region , cwd , shell_cmd , cmd [ '<STR_LIT>' ] ) <EOL> else : <EOL> if input_regions : <EOL> text = "<STR_LIT>" . join ( [ active_view . substr ( r ) for r in input_regions ] ) <EOL> temp = tempfile . NamedTemporaryFile ( delete = False ) <EOL> temp . write ( text . encode ( '<STR_LIT:utf8>' ) ) <EOL> shell_cmd = "<STR_LIT>" % ( shell_cmd , pipes . quote ( temp . name ) ) <EOL> exec_args = settings [ '<STR_LIT>' ] <EOL> exec_args . update ( { '<STR_LIT>' : shell_cmd , '<STR_LIT>' : True , '<STR_LIT>' : cwd } ) <EOL> self . window . run_command ( "<STR_LIT>" , exec_args ) <EOL> def process_region ( self , active_view , selection , cwd , shell_cmd , outpt ) : <EOL> input_str = None <EOL> if selection : <EOL> input_str = active_view . substr ( selection ) <EOL> ( success , output ) = run_cmd ( cwd , shell_cmd , True , input_str ) <EOL> if success : <EOL> if outpt == '<STR_LIT:|>' : <EOL> active_view . run_command ( "<STR_LIT>" , { '<STR_LIT>' : selection . a , <EOL> '<STR_LIT>' : selection . b , <EOL> '<STR_LIT:text>' : output } ) <EOL> elif outpt == '<STR_LIT:>>' : <EOL> self . window . run_command ( "<STR_LIT>" ) <EOL> new_view = self . window . active_view ( ) <EOL> new_view . set_name ( shell_cmd . strip ( ) ) <EOL> new_view . run_command ( "<STR_LIT>" , { '<STR_LIT:text>' : output } ) <EOL> class ReplaceWithTextCommand ( sublime_plugin . TextCommand ) : <EOL> """<STR_LIT>""" <EOL> def run ( self , edit , region_start = None , region_end = None , text = None ) : <EOL> if region_start and region_end : <EOL> self . view . replace ( edit , sublime . Region ( region_start , region_end ) , text ) <EOL> else : <EOL> self . view . insert ( edit , <NUM_LIT:0> , text ) <EOL> class SubprocessInCwdCommand ( sublime_plugin . WindowCommand ) : <EOL> """<STR_LIT>""" <EOL> def run ( self , cmd = None , wait = False ) : <EOL> cwd = cwd_for_window ( self . window ) <EOL> run_cmd ( cwd , cmd , wait ) <EOL> active_input_row = - <NUM_LIT:1> <EOL> def callback_with_history ( callback , cmd_history , input_text ) : <EOL> if callback : <EOL> cmd = input_text . split ( "<STR_LIT:\n>" ) [ active_input_row ] <EOL> if cmd in cmd_history : <EOL> cmd_history . remove ( cmd ) <EOL> cmd_history . append ( cmd ) <EOL> return callback ( cmd ) <EOL> def show_input_panel_with_readline ( window , caption , cmd_history , <EOL> on_done , on_change , on_cancel ) : <EOL> global active_input_row <EOL> active_input_row = - <NUM_LIT:1> <EOL> view = window . show_input_panel ( caption , "<STR_LIT:\n>" . join ( cmd_history ) + "<STR_LIT:\n>" , <EOL> partial ( callback_with_history , on_done , cmd_history ) , <EOL> on_change , on_cancel ) <EOL> view . settings ( ) . set ( '<STR_LIT>' , True ) <EOL> view . show ( view . size ( ) ) <EOL> return view <EOL> class ReadlineHistoryChange ( sublime_plugin . TextCommand ) : <EOL> def run_ ( self , someIntNotUsed , args ) : <EOL> if '<STR_LIT>' in args : <EOL> del args [ '<STR_LIT>' ] <EOL> return self . run ( ** args ) <EOL> def run ( self , movement , movement_args ) : <EOL> self . view . run_command ( movement , movement_args ) <EOL> self . view . run_command ( "<STR_LIT>" , { "<STR_LIT:to>" : "<STR_LIT>" , "<STR_LIT>" : False } ) <EOL> global active_input_row <EOL> active_input_row , _ = self . view . rowcol ( self . view . sel ( ) [ <NUM_LIT:0> ] . b ) <EOL> class LeftDeleteOnLine ( sublime_plugin . TextCommand ) : <EOL> def run ( self , edit ) : <EOL> if self . view . rowcol ( self . view . sel ( ) [ <NUM_LIT:0> ] . b ) [ <NUM_LIT:1> ] : <EOL> self . view . run_command ( '<STR_LIT>' ) </s>
