Skip to content

Commit fc8573e

Browse files
committed
If a path is unicode, it is added as a unicode str
- Paths checked for extended UTF8 chars - Encoded in UTF8, and passed as such to Python - Relys on patched python27.dll being in use, to support Unicode paths
1 parent d4c65d9 commit fc8573e

4 files changed

Lines changed: 65 additions & 28 deletions

File tree

PythonScript/src/ConsoleDialog.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,10 +499,10 @@ void ConsoleDialog::doDialog()
499499
_tcscpy_s(m_data->pszName, 20, _T("Python"));
500500

501501
RECT rc;
502-
rc.bottom = 0;
502+
rc.bottom = 200;
503503
rc.top = 0;
504504
rc.left = 0;
505-
rc.right = 0;
505+
rc.right = 400;
506506
m_hTabIcon = (HICON)::LoadImage(_hInst, MAKEINTRESOURCE(IDI_PYTHON8), IMAGE_ICON, 16, 16, LR_LOADMAP3DCOLORS | LR_LOADTRANSPARENT);
507507
m_data->hIconTab = m_hTabIcon;
508508
m_data->pszModuleName = _T("Python Script");

PythonScript/src/PythonHandler.cpp

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
#include "NotepadPython.h"
99
#include "PythonConsole.h"
1010
#include "MenuManager.h"
11-
11+
#include "WcharMbcsConverter.h"
1212

1313
using namespace std;
1414
using 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+
124158
void 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
{

PythonScript/src/PythonHandler.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct RunScriptArgs;
1515
class PythonHandler : NppPythonScript::PyProducerConsumer<RunScriptArgs*>
1616
{
1717
public:
18-
PythonHandler::PythonHandler(char *pluginsDir, char *configDir, HINSTANCE hInst, HWND nppHandle, HWND scintilla1Handle, HWND scintilla2Handle, PythonConsole *pythonConsole);
18+
PythonHandler::PythonHandler(TCHAR *pluginsDir, TCHAR *configDir, HINSTANCE hInst, HWND nppHandle, HWND scintilla1Handle, HWND scintilla2Handle, PythonConsole *pythonConsole);
1919
~PythonHandler();
2020

2121
bool runScript(const char *filename, bool synchronous = false, bool allowQueuing = false, HANDLE completedEvent = NULL, bool isStatement = false);
@@ -53,13 +53,14 @@ class PythonHandler : NppPythonScript::PyProducerConsumer<RunScriptArgs*>
5353
void initModules();
5454

5555
static void stopScriptWorker(PythonHandler *handler);
56+
bool containsExtendedChars(char *s);
5657

5758
// Private member vars
5859

5960
HINSTANCE m_hInst;
6061

61-
std::string m_machineBaseDir;
62-
std::string m_userBaseDir;
62+
tstring m_machineBaseDir;
63+
tstring m_userBaseDir;
6364
ScintillaWrapper *mp_scintilla;
6465
ScintillaWrapper *mp_scintilla1;
6566
ScintillaWrapper *mp_scintilla2;

PythonScript/src/PythonScript.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ PythonConsole *g_console = 0;
4040
// Paths
4141
char g_pluginDir[MAX_PATH];
4242
char g_configDir[MAX_PATH];
43+
44+
TCHAR g_tPluginDir[MAX_PATH];
45+
TCHAR g_tConfigDir[MAX_PATH];
46+
4347
string g_previousScript;
4448

4549
bool g_infoSet = false;
@@ -120,17 +124,15 @@ extern "C" __declspec(dllexport) void setInfo(NppData notepadPlusData)
120124

121125

122126
// Get the two key directories (plugins config and the Npp dir)
123-
TCHAR pluginConfig[MAX_PATH];
124-
::SendMessage(nppData._nppHandle, NPPM_GETPLUGINSCONFIGDIR, MAX_PATH, reinterpret_cast<LPARAM>(pluginConfig));
125-
strcpy_s(g_configDir, MAX_PATH, WcharMbcsConverter::tchar2char(pluginConfig).get());
127+
::SendMessage(nppData._nppHandle, NPPM_GETPLUGINSCONFIGDIR, MAX_PATH, reinterpret_cast<LPARAM>(g_tConfigDir));
128+
strcpy_s(g_configDir, MAX_PATH, WcharMbcsConverter::tchar2char(g_tConfigDir).get());
126129

127-
TCHAR pluginDir[MAX_PATH];
128-
::SendMessage(nppData._nppHandle, NPPM_GETNPPDIRECTORY, MAX_PATH, reinterpret_cast<LPARAM>(pluginDir));
129-
_tcscat_s(pluginDir, MAX_PATH, _T("\\plugins"));
130-
strcpy_s(g_pluginDir, MAX_PATH, WcharMbcsConverter::tchar2char(pluginDir).get());
130+
::SendMessage(nppData._nppHandle, NPPM_GETNPPDIRECTORY, MAX_PATH, reinterpret_cast<LPARAM>(g_tPluginDir));
131+
_tcscat_s(g_tPluginDir, MAX_PATH, _T("\\plugins"));
132+
strcpy_s(g_pluginDir, MAX_PATH, WcharMbcsConverter::tchar2char(g_tPluginDir).get());
131133

132134

133-
ConfigFile::create(pluginConfig, pluginDir, reinterpret_cast<HINSTANCE>(g_hModule));
135+
ConfigFile::create(g_tConfigDir, g_tPluginDir, reinterpret_cast<HINSTANCE>(g_hModule));
134136
MenuManager::create(nppData._nppHandle, reinterpret_cast<HINSTANCE>(g_hModule), runScript);
135137
g_infoSet = true;
136138
}
@@ -225,7 +227,7 @@ void initialise()
225227
{
226228
g_console = new PythonConsole(nppData._nppHandle);
227229

228-
pythonHandler = new PythonHandler(g_pluginDir, g_configDir, (HINSTANCE)g_hModule, nppData._nppHandle, nppData._scintillaMainHandle, nppData._scintillaSecondHandle, g_console);
230+
pythonHandler = new PythonHandler(g_tPluginDir, g_tConfigDir, (HINSTANCE)g_hModule, nppData._nppHandle, nppData._scintillaMainHandle, nppData._scintillaSecondHandle, g_console);
229231

230232
aboutDlg.init((HINSTANCE)g_hModule, nppData);
231233

0 commit comments

Comments
 (0)