55from tkinter import simpledialog
66import time , threading
77import os , json , platform
8+ import venv , sys
89
910# ██╗░░██╗██████╗░░█████╗░██████╗░
1011# ██║░██╔╝██╔══██╗██╔══██╗██╔══██╗
1112# █████═╝░██████╔╝███████║██║░░██║
1213# ██╔═██╗░██╔═══╝░██╔══██║██║░░██║
1314# ██║░╚██╗██║░░░░░██║░░██║██████╔╝
1415# ╚═╝░░╚═╝╚═╝░░░░░╚═╝░░╚═╝╚═════╝░
15- # Version 1.2.1 [SOURCE CODE]
16+ # Version 1.2.2 [SOURCE CODE]
17+
1618
1719if platform .system () == 'Darwin' :
1820 config_dir = os .path .expanduser ('~/Library/Application Support/kPad' )
1921 plugin_dir = os .path .expanduser (f'{ config_dir } /plugins' )
22+ plugin_env_path = os .path .expanduser ("~/Library/Application Support/kPad/plugin-env" )
2023elif platform .system () == 'Windows' :
2124 config_dir = os .path .join (os .getenv ('APPDATA' ), 'kPad' )
2225 plugin_dir = os .path .join (os .getenv ('APPDATA' ), 'kPad' , 'Plugins' )
26+ plugin_env_path = os .path .join (os .getenv ('APPDATA' , 'kPad' , 'Plugins' , 'plugin_env' ))
2327else :
2428 config_dir = os .path .expanduser ('~/.config/kpad' )
2529 plugin_dir = os .path .expanduser (f'{ config_dir } /plugins' )
30+ plugin_env_path = os .path .expanduser ("~/Library/Application Support/kPad/plugin-env" )
2631os .makedirs (config_dir , exist_ok = True )
2732os .makedirs (plugin_dir , exist_ok = True )
2833CONFIG_PATH = os .path .join (config_dir , 'config.json' )
4045 'recent_files' : {'enabled' : True , 'keep_recent_files_count' : 5 , 'recent_file_paths' : []},
4146 'auto_start_plugins' : []
4247}
48+
49+ if not os .path .exists (plugin_env_path ):
50+ venv .create (plugin_env_path , with_pip = True )
51+ os .environ ["KPAD_PLUGIN_ENV" ] = plugin_env_path
4352
4453_fonts = ['Menlo' , 'Monaco' , 'Helvetica' , 'Arial' , 'Times New Roman' , 'Georgia' , 'Avenir' , 'Baskerville' , 'Futura' , 'Verdana' , 'Gill Sans' , 'Courier' , 'Optima' , 'American Typewriter' ]
4554
@@ -64,7 +73,6 @@ def wrapper(event):
6473 callback (event )
6574 except TypeError :
6675 callback ()
67- return 'break'
6876 self .textbox .bind (sequence , wrapper )
6977 def get_plugin_path (self , plugin_name ):
7078 return os .path .join (plugin_dir , plugin_name )
@@ -101,6 +109,32 @@ def Widget_Button(self, parent, text, cmd, font=('', 13), **kwargs):
101109 def Widget_Other (self , parent , widget , ** kwargs ):
102110 widg = widget (parent , ** kwargs )
103111 return widg
112+ def prepare_for_external_libs (self ):
113+ 'Use this when external libs are required for a plugin at the top of the action() function.'
114+ ver = f"{ sys .version_info .major } .{ sys .version_info .minor } "
115+ env = os .getenv ("KPAD_PLUGIN_ENV" )
116+ sys .path .append (os .path .join (env , "lib" , f"python{ ver } " , "site-packages" ))
117+ def get_selected_text (self ):
118+ try :
119+ return self .textbox .get ('sel.first' , 'sel.last' )
120+ except :
121+ return ''
122+ def add_text_tag (self , tag_name , ** options ):
123+ """Create or update a text tag (foreground, background, font, etc.)"""
124+ self .textbox .tag_config (tag_name , ** options )
125+
126+ def tag_text (self , tag_name , start , end ):
127+ """Apply a tag to a range of text."""
128+ self .textbox .tag_add (tag_name , start , end )
129+
130+ def remove_tag (self , tag_name , start = "1.0" , end = "end" ):
131+ """Remove a tag from a range."""
132+ self .textbox .tag_remove (tag_name , start , end )
133+
134+ def clear_all_tags (self ):
135+ """Remove all tags from the textbox."""
136+ for tag in self .textbox .tag_names ():
137+ self .textbox .tag_remove (tag , "1.0" , "end" )
104138
105139
106140class App (ctk .CTk ):
@@ -448,4 +482,4 @@ def make_separator():
448482 make_separator ()
449483 self .main .pack (fill = 'both' , expand = 'True' )
450484
451- App ('kPad - Untitled' , CONFIGURATION ['window_geometry' ]).mainloop ()
485+ App ('kPad - Untitled' , CONFIGURATION ['window_geometry' ]).mainloop ()
0 commit comments