<s> import re , hashlib , json <EOL> try : <EOL> import javascript <EOL> import html <EOL> import latex <EOL> import python <EOL> except : <EOL> from . import javascript <EOL> from . import html <EOL> from . import latex <EOL> from . import python <EOL> def expand ( string , start , end , language = "<STR_LIT>" , settings = None ) : <EOL> if language == "<STR_LIT:html>" : <EOL> result = html . expand ( string , start , end ) <EOL> elif language == "<STR_LIT>" : <EOL> result = latex . expand ( string , start , end ) <EOL> elif language == "<STR_LIT>" : <EOL> result = python . expand ( string , start , end ) <EOL> else : <EOL> result = javascript . expand ( string , start , end ) <EOL> if ( result != None and settings ) : <EOL> expand_region_settings = settings . get ( "<STR_LIT>" ) <EOL> newSettingsJson = add_to_stack ( expand_region_settings , string . encode ( '<STR_LIT:utf-8>' ) , result . get ( "<STR_LIT:start>" ) , result . get ( "<STR_LIT:end>" ) , start , end ) <EOL> print ( newSettingsJson ) <EOL> settings . set ( "<STR_LIT>" , newSettingsJson ) <EOL> return result ; <EOL> def undo ( string , start , end , settings = None ) : <EOL> if ( settings ) : <EOL> expand_region_settings = settings . get ( "<STR_LIT>" ) <EOL> result = get_last_selection ( expand_region_settings , string . encode ( '<STR_LIT:utf-8>' ) , start , end ) <EOL> print ( result . get ( "<STR_LIT>" ) ) <EOL> settings . set ( "<STR_LIT>" , result . get ( "<STR_LIT>" ) ) <EOL> if ( result . get ( "<STR_LIT>" ) == None ) : <EOL> return None <EOL> return { "<STR_LIT:start>" : result . get ( "<STR_LIT>" ) . get ( "<STR_LIT>" ) , "<STR_LIT:end>" : result . get ( "<STR_LIT>" ) . get ( "<STR_LIT>" ) } <EOL> def add_to_stack ( settingsJson , string , startIndex , endIndex , oldStartIndex , oldEndIndex ) : <EOL> if ( settingsJson == "<STR_LIT>" or settingsJson == None ) : <EOL> settingsJson = '<STR_LIT>' <EOL> settings = json . loads ( settingsJson ) <EOL> lastStackItem = None <EOL> if ( settings . get ( "<STR_LIT>" ) and len ( settings . get ( "<STR_LIT>" ) ) > <NUM_LIT:0> ) : <EOL> lastStackItem = settings . get ( "<STR_LIT>" ) [ len ( settings . get ( "<STR_LIT>" ) ) - <NUM_LIT:1> ] <EOL> else : <EOL> lastStackItem = { "<STR_LIT:start>" : - <NUM_LIT:1> , "<STR_LIT:end>" : - <NUM_LIT:1> } <EOL> stringHash = hashlib . md5 ( string ) . hexdigest ( ) <EOL> if ( stringHash != settings . get ( "<STR_LIT>" ) ) : <EOL> settings [ "<STR_LIT>" ] = stringHash <EOL> settings [ "<STR_LIT>" ] = [ { "<STR_LIT:start>" : startIndex , "<STR_LIT:end>" : endIndex } ] <EOL> elif ( stringHash == settings . get ( "<STR_LIT>" ) and ( lastStackItem . get ( "<STR_LIT:start>" ) != oldStartIndex or lastStackItem . get ( "<STR_LIT:end>" ) != oldEndIndex ) ) : <EOL> settings [ "<STR_LIT>" ] = [ { "<STR_LIT:start>" : startIndex , "<STR_LIT:end>" : endIndex } ] <EOL> elif ( stringHash == settings . get ( "<STR_LIT>" ) ) : <EOL> settings [ "<STR_LIT>" ] . append ( { "<STR_LIT:start>" : startIndex , "<STR_LIT:end>" : endIndex } ) <EOL> newSettingsJson = json . dumps ( settings ) <EOL> return newSettingsJson <EOL> def get_last_selection ( settingsJson , string , startIndex , endIndex ) : <EOL> settings = json . loads ( settingsJson ) <EOL> newSelection = None <EOL> lastStackItem = None <EOL> if ( settings . get ( "<STR_LIT>" ) and len ( settings . get ( "<STR_LIT>" ) ) > <NUM_LIT:0> ) : <EOL> lastStackItem = settings . get ( "<STR_LIT>" ) [ len ( settings . get ( "<STR_LIT>" ) ) - <NUM_LIT:1> ] <EOL> else : <EOL> lastStackItem = { "<STR_LIT:start>" : - <NUM_LIT:1> , "<STR_LIT:end>" : - <NUM_LIT:1> } <EOL> newSelection = None <EOL> stringHash = hashlib . md5 ( string ) . hexdigest ( ) <EOL> if ( stringHash != settings . get ( "<STR_LIT>" ) ) : <EOL> settings [ "<STR_LIT>" ] = stringHash <EOL> settings [ "<STR_LIT>" ] = [ ] <EOL> elif ( stringHash == settings . get ( "<STR_LIT>" ) and ( lastStackItem . get ( "<STR_LIT:start>" ) != startIndex or lastStackItem . get ( "<STR_LIT:end>" ) != endIndex ) ) : <EOL> settings [ "<STR_LIT>" ] = stringHash <EOL> settings [ "<STR_LIT>" ] = [ ] <EOL> elif ( stringHash == settings . get ( "<STR_LIT>" ) and lastStackItem . get ( "<STR_LIT:start>" ) == startIndex and lastStackItem . get ( "<STR_LIT:end>" ) == endIndex ) : <EOL> settings . get ( "<STR_LIT>" ) . pop ( ) <EOL> if ( len ( settings . get ( "<STR_LIT>" ) ) > <NUM_LIT:0> ) : <EOL> lastStackItem = settings . get ( "<STR_LIT>" ) [ len ( settings . get ( "<STR_LIT>" ) ) - <NUM_LIT:1> ] <EOL> newSelection = { "<STR_LIT>" : lastStackItem . get ( "<STR_LIT:start>" ) , "<STR_LIT>" : lastStackItem . get ( "<STR_LIT:end>" ) } <EOL> newSettingsJson = json . dumps ( settings ) <EOL> return { "<STR_LIT>" : newSettingsJson , "<STR_LIT>" : newSelection } </s>
