<s> import sublime , sublime_plugin <EOL> import os , fnmatch , re , time , helpers <EOL> import copy <EOL> import json <EOL> ST3 = int ( sublime . version ( ) ) >= <NUM_LIT> <EOL> if not ST3 : <EOL> from codecs import open <EOL> def settings ( ) : <EOL> return sublime . load_settings ( '<STR_LIT>' ) <EOL> def get_root ( ) : <EOL> project_settings = sublime . active_window ( ) . active_view ( ) . settings ( ) . get ( '<STR_LIT>' ) <EOL> if project_settings : <EOL> return os . path . normpath ( os . path . expanduser ( project_settings . get ( '<STR_LIT:root>' , settings ( ) . get ( "<STR_LIT:root>" ) ) ) ) <EOL> else : <EOL> return os . path . normpath ( os . path . expanduser ( settings ( ) . get ( "<STR_LIT:root>" ) ) ) <EOL> def file_id ( path ) : <EOL> return os . path . relpath ( path , root ) <EOL> def find_notes ( self , root , exclude ) : <EOL> note_files = [ ] <EOL> for path , subdirs , files in os . walk ( root , topdown = True ) : <EOL> if exclude : <EOL> subdirs [ : ] = [ d for d in subdirs if d not in exclude ] <EOL> relpath = os . path . relpath ( path , root ) <EOL> for name in files : <EOL> for ext in settings ( ) . get ( "<STR_LIT>" ) : <EOL> if ( not relpath . startswith ( "<STR_LIT>" ) ) and fnmatch . fnmatch ( name , "<STR_LIT>" + ext ) : <EOL> title = re . sub ( '<STR_LIT>' + ext + '<STR_LIT:$>' , '<STR_LIT>' , name ) <EOL> tag = path . replace ( root , '<STR_LIT>' ) . replace ( os . path . sep , '<STR_LIT>' ) <EOL> if not tag == '<STR_LIT>' : <EOL> tag = tag + '<STR_LIT>' <EOL> modified_str = time . strftime ( "<STR_LIT>" , time . gmtime ( os . path . getmtime ( os . path . join ( path , name ) ) ) ) <EOL> note_files . append ( [ re . sub ( '<STR_LIT>' + ext + '<STR_LIT:$>' , '<STR_LIT>' , tag + title ) , os . path . join ( path , name ) , tag , modified_str ] ) <EOL> note_files . sort ( key = lambda item : os . path . getmtime ( item [ <NUM_LIT:1> ] ) , reverse = True ) <EOL> return note_files <EOL> def setup_notes_list ( file_list ) : <EOL> try : <EOL> display_modified_date = settings ( ) . get ( "<STR_LIT>" ) . get ( "<STR_LIT>" ) <EOL> display_folder = settings ( ) . get ( "<STR_LIT>" ) . get ( "<STR_LIT>" ) <EOL> display_full_path = settings ( ) . get ( "<STR_LIT>" ) . get ( "<STR_LIT>" ) <EOL> except : <EOL> display_modified_date = True <EOL> display_folder = True <EOL> display_full_path = False <EOL> indices = [ <NUM_LIT:0> ] <EOL> if display_modified_date : <EOL> indices . append ( <NUM_LIT:3> ) <EOL> if display_folder : <EOL> indices . append ( <NUM_LIT:2> ) <EOL> if display_full_path : <EOL> indices . append ( <NUM_LIT:1> ) <EOL> return helpers . return_sublist ( file_list , indices ) <EOL> def update_color ( old_file_path , new_file_path ) : <EOL> f_id_old = file_id ( old_file_path ) <EOL> if db . get ( f_id_old ) : <EOL> f_id_new = file_id ( new_file_path ) <EOL> if not db . get ( f_id_new ) : <EOL> db [ f_id_new ] = { } <EOL> db [ f_id_new ] [ "<STR_LIT>" ] = db [ f_id_old ] [ "<STR_LIT>" ] <EOL> db . pop ( f_id_old , None ) <EOL> save_to_brain ( ) <EOL> class NotesListCommand ( sublime_plugin . ApplicationCommand ) : <EOL> def run ( self ) : <EOL> exclude = set ( [ settings ( ) . get ( "<STR_LIT>" ) , '<STR_LIT>' ] ) <EOL> root = get_root ( ) <EOL> self . notes_dir = root <EOL> self . file_list = find_notes ( self , root , exclude ) <EOL> rlist = setup_notes_list ( self . file_list ) <EOL> window = sublime . active_window ( ) <EOL> window . show_quick_panel ( rlist , self . open_note ) <EOL> def open_note ( self , index ) : <EOL> if index == - <NUM_LIT:1> : <EOL> return <EOL> file_path = self . file_list [ index ] [ <NUM_LIT:1> ] <EOL> sublime . run_command ( "<STR_LIT>" , { "<STR_LIT>" : file_path } ) <EOL> class NotesOpenCommand ( sublime_plugin . ApplicationCommand ) : <EOL> def run ( self , file_path ) : <EOL> sublime . set_timeout ( lambda : self . async_open ( file_path ) , <NUM_LIT:0> ) <EOL> def async_open ( self , file_path ) : <EOL> view = sublime . active_window ( ) . open_file ( file_path , sublime . ENCODED_POSITION ) <EOL> f_id = file_id ( file_path ) <EOL> if db . get ( f_id ) and os . path . isfile ( db [ f_id ] [ "<STR_LIT>" ] ) : <EOL> view . settings ( ) . set ( "<STR_LIT>" , db [ f_id ] [ "<STR_LIT>" ] ) <EOL> view . settings ( ) . set ( "<STR_LIT>" , True ) <EOL> class NotesNewCommand ( sublime_plugin . ApplicationCommand ) : <EOL> def run ( self , title = None ) : <EOL> self . notes_dir = get_root ( ) <EOL> self . window = sublime . active_window ( ) <EOL> if title is None : <EOL> self . window . show_input_panel ( "<STR_LIT>" , "<STR_LIT>" , self . create_note , None , None ) <EOL> else : <EOL> self . create_note ( title ) <EOL> def create_note ( self , title ) : <EOL> filename = title . split ( "<STR_LIT:/>" ) <EOL> if len ( filename ) > <NUM_LIT:1> : <EOL> title = filename [ len ( filename ) - <NUM_LIT:1> ] <EOL> directory = os . path . join ( self . notes_dir , filename [ <NUM_LIT:0> ] ) <EOL> tag = filename [ <NUM_LIT:0> ] <EOL> else : <EOL> title = filename [ <NUM_LIT:0> ] <EOL> directory = self . notes_dir <EOL> tag = "<STR_LIT>" <EOL> if not os . path . exists ( directory ) : <EOL> os . makedirs ( directory ) <EOL> if any ( title . endswith ( "<STR_LIT:.>" + ext ) for ext in settings ( ) . get ( "<STR_LIT>" ) ) : <EOL> ext = "<STR_LIT>" <EOL> else : <EOL> ext = "<STR_LIT:.>" + settings ( ) . get ( "<STR_LIT>" ) <EOL> file = os . path . join ( directory , title + ext ) <EOL> if not os . path . exists ( file ) : <EOL> open ( file , '<STR_LIT>' ) . close ( ) <EOL> view = sublime . active_window ( ) . open_file ( file ) <EOL> self . insert_title_scheduled = False <EOL> self . insert_title ( title , tag , view ) <EOL> def insert_title ( self , title , tag , view ) : <EOL> if view . is_loading ( ) : <EOL> if not self . insert_title_scheduled : <EOL> self . insert_title_scheduled = True <EOL> sublime . set_timeout ( lambda : self . insert_title ( title , tag , view ) , <NUM_LIT:100> ) <EOL> return <EOL> else : <EOL> view . run_command ( "<STR_LIT>" , { "<STR_LIT:title>" : title , "<STR_LIT>" : tag } ) <EOL> class NotesEvents ( sublime_plugin . EventListener ) : <EOL> def on_load_async ( self , view ) : <EOL> if view . settings ( ) . get ( "<STR_LIT>" ) or not view . file_name ( ) : <EOL> return <EOL> if os . path . realpath ( view . file_name ( ) ) . startswith ( root ) : <EOL> f_id = file_id ( view . file_name ( ) ) <EOL> view . settings ( ) . set ( "<STR_LIT>" , True ) <EOL> if db . get ( f_id ) and db [ f_id ] [ "<STR_LIT>" ] : <EOL> view . settings ( ) . set ( "<STR_LIT>" , db [ f_id ] [ "<STR_LIT>" ] ) <EOL> class NoteInsertTitleCommand ( sublime_plugin . TextCommand ) : <EOL> def run ( self , edit , ** kwargs ) : <EOL> if settings ( ) . get ( "<STR_LIT>" ) : <EOL> header = "<STR_LIT>" <EOL> header = header + "<STR_LIT>" + kwargs [ "<STR_LIT:title>" ] . capitalize ( ) + "<STR_LIT:\n>" <EOL> header = header + "<STR_LIT>" + time . strftime ( "<STR_LIT>" ) + "<STR_LIT:\n>" <EOL> header = header + "<STR_LIT>" + kwargs [ "<STR_LIT>" ] + "<STR_LIT:\n>" <EOL> for yaml_el in settings ( ) . get ( "<STR_LIT>" ) : <EOL> header = header + yaml_el + "<STR_LIT>" <EOL> header = header + "<STR_LIT>" <EOL> self . view . insert ( edit , <NUM_LIT:0> , header ) <EOL> class NoteChangeColorCommand ( sublime_plugin . WindowCommand ) : <EOL> def run ( self ) : <EOL> self . colors = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> self . window = sublime . active_window ( ) <EOL> self . original_cs = self . window . active_view ( ) . settings ( ) . get ( "<STR_LIT>" ) <EOL> current_color = os . path . basename ( self . original_cs ) . replace ( "<STR_LIT>" , "<STR_LIT>" ) . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if ST3 : <EOL> self . window . show_quick_panel ( self . colors , self . on_select , <NUM_LIT:0> , self . colors . index ( current_color ) , self . on_highlight ) <EOL> else : <EOL> self . window . show_quick_panel ( self . colors , self . on_select , <NUM_LIT:0> , self . colors . index ( current_color ) ) <EOL> def on_select ( self , index ) : <EOL> global db <EOL> if index == - <NUM_LIT:1> : <EOL> self . window . active_view ( ) . settings ( ) . set ( "<STR_LIT>" , self . original_cs ) <EOL> else : <EOL> try : <EOL> path = sublime . find_resources ( "<STR_LIT>" + self . colors [ index ] + "<STR_LIT>" ) <EOL> path = path [ <NUM_LIT:0> ] <EOL> except : <EOL> path = os . path . join ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" + self . colors [ index ] + "<STR_LIT>" ) <EOL> view = self . window . active_view ( ) <EOL> view . settings ( ) . set ( "<STR_LIT>" , path ) <EOL> f_id = file_id ( view . file_name ( ) ) <EOL> if not db . get ( f_id ) : <EOL> db [ f_id ] = { } <EOL> db [ f_id ] [ "<STR_LIT>" ] = path <EOL> save_to_brain ( ) <EOL> def on_highlight ( self , index ) : <EOL> try : <EOL> path = sublime . find_resources ( "<STR_LIT>" + self . colors [ index ] + "<STR_LIT>" ) <EOL> path = path [ <NUM_LIT:0> ] <EOL> except : <EOL> path = os . path . join ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" + self . colors [ index ] + "<STR_LIT>" ) <EOL> self . window . active_view ( ) . settings ( ) . set ( "<STR_LIT>" , path ) <EOL> def is_enabled ( self ) : <EOL> return self . window . active_view ( ) . settings ( ) . get ( "<STR_LIT>" ) . endswith ( "<STR_LIT>" ) <EOL> class NoteArchiveCommand ( sublime_plugin . WindowCommand ) : <EOL> def run ( self ) : <EOL> window = sublime . active_window ( ) <EOL> self . notes_dir = get_root ( ) <EOL> self . archive_note ( ) <EOL> sublime . status_message ( "<STR_LIT>" ) <EOL> def archive_note ( self ) : <EOL> file_path = self . window . active_view ( ) . file_name ( ) <EOL> f_id = file_id ( file_path ) <EOL> archive_dir = os . path . join ( self . notes_dir , settings ( ) . get ( "<STR_LIT>" ) ) <EOL> new_file_path = os . path . join ( archive_dir , f_id ) <EOL> if not os . path . exists ( archive_dir ) : <EOL> os . makedirs ( archive_dir ) <EOL> if not os . path . isfile ( new_file_path ) : <EOL> os . renames ( file_path , new_file_path ) <EOL> self . window . run_command ( "<STR_LIT>" ) <EOL> update_color ( file_path , new_file_path ) <EOL> def is_enabled ( self ) : <EOL> is_note = self . window . active_view ( ) . settings ( ) . get ( "<STR_LIT>" ) <EOL> if is_note : <EOL> return is_note <EOL> else : <EOL> return False <EOL> class NoteUnarchiveCommand ( sublime_plugin . ApplicationCommand ) : <EOL> def run ( self ) : <EOL> self . notes_dir = get_root ( ) <EOL> archive_dir = os . path . join ( self . notes_dir , settings ( ) . get ( "<STR_LIT>" ) ) <EOL> self . file_list = find_notes ( self , archive_dir , [ ] ) <EOL> rlist = setup_notes_list ( self . file_list ) <EOL> window = sublime . active_window ( ) <EOL> if rlist : <EOL> window . show_quick_panel ( rlist , self . unarchive_note ) <EOL> else : <EOL> window . show_quick_panel ( [ '<STR_LIT>' ] , [ ] ) <EOL> def unarchive_note ( self , index ) : <EOL> if index == - <NUM_LIT:1> : <EOL> return <EOL> file_path = self . file_list [ index ] [ <NUM_LIT:1> ] <EOL> new_file_path = file_path . replace ( os . path . sep + settings ( ) . get ( "<STR_LIT>" ) , '<STR_LIT>' ) <EOL> if not os . path . isfile ( new_file_path ) : <EOL> os . renames ( file_path , new_file_path ) <EOL> update_color ( file_path , new_file_path ) <EOL> sublime . run_command ( "<STR_LIT>" , { "<STR_LIT>" : new_file_path } ) <EOL> def is_enabled ( self ) : <EOL> return True <EOL> class NoteRemoveCommand ( sublime_plugin . WindowCommand ) : <EOL> def run ( self ) : <EOL> f_path = self . window . active_view ( ) . file_name ( ) <EOL> delete = sublime . ok_cancel_dialog ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if delete : <EOL> self . window . active_view ( ) . set_scratch ( True ) <EOL> self . window . run_command ( "<STR_LIT>" ) <EOL> os . remove ( f_path ) <EOL> def is_enabled ( self ) : <EOL> is_note = self . window . active_view ( ) . settings ( ) . get ( "<STR_LIT>" ) <EOL> if is_note : <EOL> return is_note <EOL> else : <EOL> return False <EOL> class NoteRenameCommand ( sublime_plugin . WindowCommand ) : <EOL> def run ( self ) : <EOL> self . window . show_input_panel ( "<STR_LIT>" , "<STR_LIT>" , self . rename_note , None , None ) <EOL> def rename_note ( self , title ) : <EOL> global db <EOL> self . notes_dir = os . path . expanduser ( root ) <EOL> self . file_path = self . window . active_view ( ) . file_name ( ) <EOL> filename = title . split ( "<STR_LIT:/>" ) <EOL> if len ( filename ) > <NUM_LIT:1> : <EOL> title = filename [ len ( filename ) - <NUM_LIT:1> ] <EOL> directory = self . notes_dir + os . path . sep + filename [ <NUM_LIT:0> ] <EOL> tag = filename [ <NUM_LIT:0> ] <EOL> else : <EOL> title = filename [ <NUM_LIT:0> ] <EOL> directory = self . notes_dir <EOL> tag = "<STR_LIT>" <EOL> if not os . path . exists ( directory ) : <EOL> os . makedirs ( directory ) <EOL> if any ( title . endswith ( "<STR_LIT:.>" + ext ) for ext in settings ( ) . get ( "<STR_LIT>" ) ) : <EOL> ext = "<STR_LIT>" <EOL> else : <EOL> ext = "<STR_LIT:.>" + settings ( ) . get ( "<STR_LIT>" ) <EOL> new_file_path = os . path . join ( directory , title + ext ) <EOL> if not os . path . isfile ( new_file_path ) : <EOL> os . rename ( self . file_path , new_file_path ) <EOL> self . window . run_command ( "<STR_LIT>" ) <EOL> sublime . run_command ( "<STR_LIT>" , { "<STR_LIT>" : new_file_path } ) <EOL> update_color ( self . file_path , new_file_path ) <EOL> else : <EOL> sublime . error_message ( "<STR_LIT>" ) <EOL> self . window . show_input_panel ( "<STR_LIT>" , "<STR_LIT>" , self . rename_note , None , None ) <EOL> def is_enabled ( self ) : <EOL> is_note = self . window . active_view ( ) . settings ( ) . get ( "<STR_LIT>" ) <EOL> if is_note : <EOL> return is_note <EOL> else : <EOL> return False <EOL> def save_to_brain ( ) : <EOL> with open ( db_json_file , '<STR_LIT:w>' , encoding = '<STR_LIT:utf-8>' ) as f : <EOL> json . dump ( db , f , indent = <NUM_LIT:4> , sort_keys = True ) <EOL> def cleanup_brain ( ) : <EOL> to_delete = [ ] <EOL> for nfile in db : <EOL> if not os . path . exists ( os . path . join ( root , nfile ) ) : <EOL> to_delete . append ( nfile ) <EOL> for x in to_delete : <EOL> db . pop ( x , None ) <EOL> save_to_brain ( ) <EOL> def plugin_loaded ( ) : <EOL> global db , root , db_json_file <EOL> db = { } <EOL> root = get_root ( ) <EOL> brain = os . path . join ( root , '<STR_LIT>' ) <EOL> inbox = os . path . join ( root , '<STR_LIT>' , '<STR_LIT>' ) <EOL> db_json_file = os . path . join ( root , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not os . path . exists ( brain ) : <EOL> os . makedirs ( brain ) <EOL> if not os . path . isfile ( inbox ) : <EOL> open ( inbox , mode = '<STR_LIT:a>' , encoding = '<STR_LIT:utf-8>' ) . close ( ) <EOL> try : <EOL> with open ( db_json_file , '<STR_LIT:r>' ) as f : <EOL> db = json . load ( f ) <EOL> cleanup_brain ( ) <EOL> except : <EOL> db = { } <EOL> if not ST3 : <EOL> plugin_loaded ( ) </s>
