88#include " NotepadPython.h"
99#include " PythonConsole.h"
1010#include " MenuManager.h"
11-
11+ # include " WcharMbcsConverter.h "
1212
1313using namespace std ;
1414using namespace NppPythonScript ;
1515
16- PythonHandler::PythonHandler (char *pluginsDir, char *configDir, HINSTANCE hInst, HWND nppHandle, HWND scintilla1Handle, HWND scintilla2Handle, PythonConsole *pythonConsole)
16+ PythonHandler::PythonHandler (TCHAR *pluginsDir, TCHAR *configDir, HINSTANCE hInst, HWND nppHandle, HWND scintilla1Handle, HWND scintilla2Handle, PythonConsole *pythonConsole)
1717 : PyProducerConsumer<RunScriptArgs*>(),
1818 m_nppHandle(nppHandle),
1919 m_scintilla1Handle(scintilla1Handle),
@@ -27,8 +27,8 @@ PythonHandler::PythonHandler(char *pluginsDir, char *configDir, HINSTANCE hInst,
2727 mp_python(NULL ),
2828 m_consumerStarted(false )
2929{
30- m_machineBaseDir.append (" \\ PythonScript\\ " );
31- m_userBaseDir.append (" \\ PythonScript\\ " );
30+ m_machineBaseDir.append (_T ( " \\ PythonScript\\ " ) );
31+ m_userBaseDir.append (_T ( " \\ PythonScript\\ " ) );
3232
3333 mp_notepad = createNotepadPlusWrapper ();
3434 mp_scintilla = createScintillaWrapper ();
@@ -84,19 +84,39 @@ void PythonHandler::initPython()
8484
8585 Py_Initialize ();
8686
87+ shared_ptr<char > machineBaseDir = WcharMbcsConverter::tchar2char (m_machineBaseDir.c_str ());
88+ shared_ptr<char > configDir = WcharMbcsConverter::tchar2char (m_userBaseDir.c_str ());
89+
90+ bool machineIsUnicode = containsExtendedChars (machineBaseDir.get ());
91+ bool userIsUnicode = containsExtendedChars (configDir.get ());
92+
93+
94+ string smachineDir (machineBaseDir.get ());
95+ string suserDir (configDir.get ());
96+
97+
8798
8899 // Init paths
89100 char initBuffer[1024 ];
101+
90102 _snprintf_s (initBuffer, 1024 , 1024 ,
91103 " import sys\n "
92- " sys.path.append(r'%slib')\n "
93- " sys.path.append(r'%slib')\n "
94- " sys.path.append(r'%sscripts')\n "
95- " sys.path.append(r'%sscripts')\n " ,
96- m_machineBaseDir.c_str (),
97- m_userBaseDir.c_str (),
98- m_machineBaseDir.c_str (),
99- m_userBaseDir.c_str ());
104+ " sys.path.append(r'%slib'%s)\n "
105+ " sys.path.append(r'%slib'%s)\n "
106+ " sys.path.append(r'%sscripts'%s)\n "
107+ " sys.path.append(r'%sscripts'%s)\n " ,
108+
109+ smachineDir.c_str (),
110+ machineIsUnicode ? " .decode('utf8')" : " " ,
111+
112+ suserDir.c_str (),
113+ userIsUnicode ? " .decode('utf8')" : " " ,
114+
115+ smachineDir.c_str (),
116+ machineIsUnicode ? " .decode('utf8')" : " " ,
117+
118+ suserDir.c_str (),
119+ userIsUnicode ? " .decode('utf8')" : " " );
100120
101121 PyRun_SimpleString (initBuffer);
102122
@@ -121,11 +141,25 @@ void PythonHandler::initModules()
121141}
122142
123143
144+ bool PythonHandler::containsExtendedChars (char *s)
145+ {
146+ bool retVal = false ;
147+ for (int pos = 0 ; s[pos]; ++pos)
148+ {
149+ if (s[pos] & 0x80 )
150+ {
151+ retVal = true ;
152+ break ;
153+ }
154+ }
155+ return retVal;
156+ }
157+
124158void PythonHandler::runStartupScripts ()
125159{
126160
127161 // Machine scripts (N++\Plugins\PythonScript\scripts dir)
128- string startupPath (m_machineBaseDir);
162+ string startupPath (WcharMbcsConverter::tchar2char ( m_machineBaseDir. c_str ()). get () );
129163 startupPath.append (" scripts\\ startup.py" );
130164 if (::PathFileExistsA (startupPath.c_str ()))
131165 {
@@ -134,7 +168,7 @@ void PythonHandler::runStartupScripts()
134168 }
135169
136170 // User scripts ($CONFIGDIR$\PythonScript\scripts dir)
137- startupPath = m_userBaseDir;
171+ startupPath = WcharMbcsConverter::tchar2char ( m_userBaseDir. c_str ()). get () ;
138172 startupPath.append (" scripts\\ startup.py" );
139173 if (::PathFileExistsA (startupPath.c_str ()))
140174 {
0 commit comments