<s> """<STR_LIT>""" <EOL> import re <EOL> import sys , os <EOL> import sublime <EOL> import sublime_plugin <EOL> sys . path . append ( os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) ) <EOL> import headline <EOL> from utilities import is_region_void <EOL> HEADLINE_PATTERN = re . compile ( r'<STR_LIT>' ) <EOL> class NoteSmartNewLineCommand ( sublime_plugin . TextCommand ) : <EOL> """<STR_LIT>""" <EOL> def run ( self , edit ) : <EOL> points = [ ] <EOL> for s in self . view . sel ( ) : <EOL> r = self . view . full_line ( s ) <EOL> if headline . _is_region_folded ( r . b + <NUM_LIT:1> , self . view ) : <EOL> i = headline . region_of_content_of_headline_at_point ( self . view , s . b ) <EOL> else : <EOL> i = sublime . Region ( r . a , r . b - <NUM_LIT:1> ) <EOL> points . append ( i ) <EOL> self . view . insert ( edit , i . b , '<STR_LIT:\n>' ) <EOL> self . view . sel ( ) . clear ( ) <EOL> for p in points : <EOL> self . view . sel ( ) . add ( p . b + <NUM_LIT:1> ) <EOL> class NoteSmartFoldingCommand ( sublime_plugin . TextCommand ) : <EOL> """<STR_LIT>""" <EOL> def run ( self , edit ) : <EOL> ever_matched = False <EOL> for region in self . view . sel ( ) : <EOL> matched = self . fold_or_unfold_headline_at_point ( region . a ) <EOL> if matched : <EOL> ever_matched = True <EOL> if not ever_matched : <EOL> for r in self . view . sel ( ) : <EOL> self . view . insert ( edit , r . a , '<STR_LIT:\t>' ) <EOL> self . view . show ( r ) <EOL> def fold_or_unfold_headline_at_point ( self , from_point ) : <EOL> """<STR_LIT>""" <EOL> _ , level = headline . headline_and_level_at_point ( self . view , <EOL> from_point ) <EOL> if level is None or not headline . is_scope_headline ( self . view , from_point ) : <EOL> return False <EOL> content_region = headline . region_of_content_of_headline_at_point ( self . view , <EOL> from_point ) <EOL> if content_region is None : <EOL> return True <EOL> if self . is_region_totally_folded ( content_region ) : <EOL> self . unfold_yet_fold_subheads ( content_region , level ) <EOL> else : <EOL> self . view . fold ( sublime . Region ( content_region . a - <NUM_LIT:1> , content_region . b ) ) <EOL> return True <EOL> def is_region_totally_folded ( self , region ) : <EOL> """<STR_LIT>""" <EOL> if ( region is None ) or ( region . a == region . b ) : <EOL> return True <EOL> for i in self . view . folded_regions ( ) : <EOL> if i . contains ( region ) : <EOL> return True <EOL> return False <EOL> def unfold_yet_fold_subheads ( self , region , level ) : <EOL> """<STR_LIT>""" <EOL> self . view . unfold ( region ) <EOL> child_headline_region , _ = headline . find_headline ( self . view , region . a , level , True , headline . MATCH_CHILD ) <EOL> while ( not is_region_void ( child_headline_region ) and child_headline_region . b <= region . b ) : <EOL> child_content_region = headline . region_of_content_of_headline_at_point ( self . view , <EOL> child_headline_region . a ) <EOL> if child_content_region is not None : <EOL> self . view . fold ( sublime . Region ( child_content_region . a - <NUM_LIT:1> , child_content_region . b ) ) <EOL> search_start_point = child_content_region . b <EOL> else : <EOL> search_start_point = child_headline_region . b <EOL> child_headline_region , _ = headline . find_headline ( self . view , search_start_point , level , True , headline . MATCH_CHILD , <EOL> skip_headline_at_point = True ) <EOL> class NoteGlobalFoldingCommand ( NoteSmartFoldingCommand ) : <EOL> """<STR_LIT>""" <EOL> def run ( self , edit ) : <EOL> if self . is_global_folded ( ) : <EOL> self . unfold_all ( ) <EOL> else : <EOL> self . fold_all ( ) <EOL> def is_global_folded ( self ) : <EOL> """<STR_LIT>""" <EOL> region , level = headline . find_headline ( self . view , <NUM_LIT:0> , headline . ANY_LEVEL , True ) <EOL> if is_region_void ( region ) : <EOL> return True <EOL> point = region . a <EOL> while ( point is not None and region ) : <EOL> region = headline . region_of_content_of_headline_at_point ( self . view , point ) <EOL> if not is_region_void ( region ) : <EOL> point = region . b <EOL> if not self . is_region_totally_folded ( region ) : <EOL> return False <EOL> else : <EOL> region , level = headline . find_headline ( self . view , point , headline . ANY_LEVEL , True , <EOL> skip_headline_at_point = True ) <EOL> if not is_region_void ( region ) : <EOL> point = region . a <EOL> return True <EOL> def unfold_all ( self ) : <EOL> self . view . unfold ( sublime . Region ( <NUM_LIT:0> , self . view . size ( ) ) ) <EOL> self . view . show ( self . view . sel ( ) [ <NUM_LIT:0> ] ) <EOL> def fold_all ( self ) : <EOL> region , level = headline . find_headline ( self . view , <NUM_LIT:0> , headline . ANY_LEVEL , True ) <EOL> point = region . a <EOL> while ( point is not None and region ) : <EOL> region = headline . region_of_content_of_headline_at_point ( self . view , point ) <EOL> if not is_region_void ( region ) : <EOL> point = region . b <EOL> self . view . fold ( sublime . Region ( region . a - <NUM_LIT:1> , region . b ) ) <EOL> region , level = headline . find_headline ( self . view , point , headline . ANY_LEVEL , <EOL> True , skip_headline_at_point = True ) <EOL> if not is_region_void ( region ) : <EOL> point = region . a <EOL> self . adjust_cursors_and_view ( ) <EOL> def adjust_cursors_and_view ( self ) : <EOL> """<STR_LIT>""" <EOL> folded_regions = self . view . folded_regions ( ) <EOL> new_sel = [ ] <EOL> for r in self . view . sel ( ) : <EOL> for folded in folded_regions : <EOL> if folded . contains ( r ) : <EOL> new_sel . append ( sublime . Region ( folded . b , folded . b ) ) <EOL> break <EOL> else : <EOL> new_sel . append ( r ) <EOL> self . view . sel ( ) . clear ( ) <EOL> for r in new_sel : <EOL> self . view . sel ( ) . add ( r ) <EOL> self . view . show ( r ) </s>
