#NEWLINE# def _get_closest_env_border( string , start_pos , end_pos , reverse = False ) : #NEWLINE# #INDENT# open_command = 'begin' #NEWLINE# close_command = 'end' #NEWLINE# if _ST3 : #NEWLINE# #INDENT# iterator = _BEGIN_END_REG . finditer ( string , pos = start_pos , endpos = end_pos ) #NEWLINE# offset = 0 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# s = string [ start_pos : end_pos ] #NEWLINE# iterator = _BEGIN_END_REG . finditer ( s ) #NEWLINE# offset = start_pos #NEWLINE# #UNINDENT# if reverse : #NEWLINE# #INDENT# iterator = reversed ( list ( iterator ) ) #NEWLINE# ( open_command , close_command ) = ( close_command , command ) #NEWLINE# #UNINDENT# count = 0 #NEWLINE# for before in iterator : #NEWLINE# #INDENT# line = utils . get_line ( string , before . start ( ) , before . end ( ) ) #NEWLINE# if ( string [ line [ 'start' ] : line [ 'end' ] ] . strip ( ) [ 0 ] == '%' ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# command = before . group ( 'command' ) #NEWLINE# if ( command == open_command ) : #NEWLINE# #INDENT# count += 1 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( command == close_command ) and ( count > 0 ) ) : #NEWLINE# #INDENT# count -= 1 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( command == close_command ) : #NEWLINE# #INDENT# return { #NEWLINE# #INDENT# 'start' : ( offset + before . start ( ) ) , #NEWLINE# 'end' : ( offset + before . end ( ) ) , #NEWLINE# 'name' : before . group ( 'name' ) , #NEWLINE# #UNINDENT# }