Skip to content

Commit 83e188e

Browse files
committed
Stop console script
- Run button now not default for console - Changes to stop button while script runs - Escape works to clear console entry
1 parent 219190f commit 83e188e

File tree

11 files changed

+98
-27
lines changed

11 files changed

+98
-27
lines changed

PythonScript/project/PythonScript2010.vcxproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
</PropertyGroup>
5050
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
5151
<LinkIncremental>false</LinkIncremental>
52+
<IncludePath>E:\work\PythonScript\NppPlugin\include;$(PythonBase)\Include;$(IncludePath)</IncludePath>
53+
<LibraryPath>$(BoostPythonLibPath);$(PythonLibPath);E:\work\PythonScript\NppPlugin\bin\release;$(LibraryPath)</LibraryPath>
5254
</PropertyGroup>
5355
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
5456
<ClCompile>
@@ -70,7 +72,7 @@
7072
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
7173
<ClCompile>
7274
<WarningLevel>Level3</WarningLevel>
73-
<PrecompiledHeader>Use</PrecompiledHeader>
75+
<PrecompiledHeader>Create</PrecompiledHeader>
7476
<Optimization>MaxSpeed</Optimization>
7577
<FunctionLevelLinking>true</FunctionLevelLinking>
7678
<IntrinsicFunctions>true</IntrinsicFunctions>
@@ -81,6 +83,7 @@
8183
<GenerateDebugInformation>true</GenerateDebugInformation>
8284
<EnableCOMDATFolding>true</EnableCOMDATFolding>
8385
<OptimizeReferences>true</OptimizeReferences>
86+
<AdditionalDependencies>python26.lib;NppPlugin.lib;shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
8487
</Link>
8588
</ItemDefinitionGroup>
8689
<ItemGroup>

PythonScript/src/ConsoleDialog.cpp

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,14 @@ BOOL ConsoleDialog::run_dlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM l
6868
case WM_COMMAND:
6969
if (LOWORD(wParam) == IDC_RUN)
7070
{
71-
runStatement();
71+
if (m_runButtonIsRun)
72+
{
73+
runStatement();
74+
}
75+
else
76+
{
77+
m_console->stopStatement();
78+
}
7279
//MessageBox(NULL, _T("Command") , _T("Python Command"), 0);
7380
return TRUE;
7481
}
@@ -171,8 +178,12 @@ void ConsoleDialog::historyNext()
171178

172179
void ConsoleDialog::historyAdd(const char *line)
173180
{
174-
m_history.push_back(string(line));
175-
m_currentHistory = m_history.size();
181+
if (line && line[0])
182+
{
183+
m_history.push_back(string(line));
184+
m_currentHistory = m_history.size();
185+
}
186+
176187
m_historyIter = m_history.end();
177188
m_changes.clear();
178189
}
@@ -193,6 +204,14 @@ LRESULT ConsoleDialog::inputWndProc(HWND hWnd, UINT message, WPARAM wParam, LPAR
193204

194205
LRESULT ConsoleDialog::run_inputWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
195206
{
207+
208+
#ifdef _DEBUG
209+
{
210+
TCHAR outputbuffer[500];
211+
_sntprintf_s(outputbuffer, 500, 500, _T("Message: %ud W:%ld L:%ld\n"), message, wParam, lParam);
212+
OutputDebugString(outputbuffer);
213+
}
214+
#endif
196215
switch(message)
197216
{
198217
case WM_KEYDOWN:
@@ -206,6 +225,19 @@ LRESULT ConsoleDialog::run_inputWndProc(HWND hWnd, UINT message, WPARAM wParam,
206225
historyNext();
207226
return FALSE;
208227

228+
229+
default:
230+
return CallWindowProc(m_originalInputWndProc, hWnd, message, wParam, lParam);
231+
}
232+
break;
233+
234+
case WM_KEYUP:
235+
switch(wParam)
236+
{
237+
case VK_RETURN:
238+
runStatement();
239+
return FALSE;
240+
209241
case VK_ESCAPE:
210242
historyEnd();
211243
return FALSE;
@@ -235,6 +267,12 @@ void ConsoleDialog::runStatement()
235267
}
236268

237269

270+
void ConsoleDialog::stopStatement()
271+
{
272+
m_console->stopStatement();
273+
}
274+
275+
238276
void ConsoleDialog::setPrompt(const char *prompt)
239277
{
240278
m_prompt = prompt;
@@ -287,7 +325,10 @@ void ConsoleDialog::doDialog()
287325

288326
void ConsoleDialog::runEnabled(bool enabled)
289327
{
290-
EnableWindow(GetDlgItem(_hSelf, IDC_RUN), enabled);
328+
//EnableWindow(GetDlgItem(_hSelf, IDC_RUN), enabled);
329+
::SetWindowText(GetDlgItem(_hSelf, IDC_RUN), enabled ? _T("Run") : _T("Stop"));
330+
m_runButtonIsRun = enabled;
331+
291332
if (enabled)
292333
{
293334
::SetForegroundWindow(_hSelf);

PythonScript/src/ConsoleDialog.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class ConsoleDialog : DockingDlgInterface
3030
private:
3131
void createOutputWindow(HWND hParentWindow);
3232
void runStatement();
33+
void stopStatement();
34+
3335
LRESULT run_inputWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
3436
static LRESULT inputWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
3537
void historyNext();
@@ -49,6 +51,8 @@ class ConsoleDialog : DockingDlgInterface
4951
std::list<std::string>::iterator m_historyIter;
5052
std::map<int, std::string> m_changes;
5153
int m_currentHistory;
54+
bool m_runButtonIsRun;
55+
5256
};
5357

5458

PythonScript/src/ConsoleInterface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class ConsoleInterface
99
ConsoleInterface() { };
1010
virtual ~ConsoleInterface() {};
1111
virtual void runStatement(const char *) = 0;
12+
virtual void stopStatement() = 0;
1213
};
1314

1415

PythonScript/src/PythonConsole.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,18 @@ void PythonConsole::message(const char *msg)
7777
}
7878

7979
/** To call this function, you MUST have the GIL
80+
* (it runs the __str__ attribute of the object)
8081
* If you don't, or aren't sure, you can call message() instead, which takes a const char*
8182
*/
8283
void PythonConsole::writeText(object text)
8384
{
8485
mp_consoleDlg->writeText(len(text), (const char *)extract<const char *>(text.attr("__str__")()));
8586
}
8687

87-
void PythonConsole::stopScript()
88+
void PythonConsole::stopStatement()
8889
{
8990
DWORD threadID;
90-
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PythonConsole::killStatement, this, 0, &threadID);
91+
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PythonConsole::stopStatementWorker, this, 0, &threadID);
9192

9293
}
9394

@@ -116,6 +117,7 @@ void PythonConsole::runStatement(const char *statement)
116117
produce(copy);
117118
}
118119

120+
119121
void PythonConsole::queueComplete()
120122
{
121123
mp_consoleDlg->runEnabled(true);
@@ -152,11 +154,11 @@ void PythonConsole::consume(const char *statement)
152154
}
153155

154156

155-
void PythonConsole::killStatement(PythonConsole *console)
157+
void PythonConsole::stopStatementWorker(PythonConsole *console)
156158
{
157159
PyGILState_STATE gstate = PyGILState_Ensure();
158160

159-
PyThreadState_SetAsyncExc(console->mp_python->getExecutingThreadID(), PyExc_KeyboardInterrupt);
161+
PyThreadState_SetAsyncExc(console->getConsumerThreadID(), PyExc_KeyboardInterrupt);
160162

161163
PyGILState_Release(gstate);
162164
}
@@ -166,7 +168,6 @@ void PythonConsole::killStatement(PythonConsole *console)
166168
void export_console()
167169
{
168170
class_<PythonConsole>("Console", no_init)
169-
.def("write", &PythonConsole::writeText, "Create a new document")
170-
.def("stopScript", &PythonConsole::stopScript, "Stops the currently script (if there's one running)");
171+
.def("write", &PythonConsole::writeText, "Writes text to the console. Uses the __str__ function of the object passed.");
171172

172173
}

PythonScript/src/PythonConsole.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ class PythonConsole : public NppPythonScript::PyProducerConsumer<const char *>,
2626

2727
/* Console Interface members */
2828
void runStatement(const char *statement);
29+
void stopStatement();
2930
void setPrompt(const char *prompt);
3031

31-
static void killStatement(PythonConsole *console);
32+
static void stopStatementWorker(PythonConsole *console);
3233
bool runStatementWorker(const char *statement);
3334
virtual void consume(const char *statement);
3435

PythonScript/src/PythonHandler.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ PythonHandler::~PythonHandler(void)
4343
{
4444
if (consumerBusy())
4545
{
46-
killScript();
46+
stopScript();
4747
}
4848

4949
// We need to swap back to the main thread
@@ -243,13 +243,14 @@ void PythonHandler::queueComplete()
243243
}
244244

245245

246-
void PythonHandler::killScript()
246+
void PythonHandler::stopScript()
247247
{
248-
PythonHandler::killScriptWorker(this);
248+
DWORD threadID;
249+
CreateThread(NULL, 0, reinterpret_cast<LPTHREAD_START_ROUTINE>(stopScriptWorker), this, 0, &threadID);
249250
}
250251

251252

252-
void PythonHandler::killScriptWorker(PythonHandler *handler)
253+
void PythonHandler::stopScriptWorker(PythonHandler *handler)
253254
{
254255
PyGILState_STATE gstate = PyGILState_Ensure();
255256

PythonScript/src/PythonHandler.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class PythonHandler : NppPythonScript::PyProducerConsumer<RunScriptArgs*>
2222

2323
bool runScript(const char *filename, bool synchronous = false);
2424
bool runScript(const std::string& filename, bool synchronous = false);
25-
25+
2626
void runScriptWorker(RunScriptArgs* args);
2727

2828
void consume(RunScriptArgs* args);
@@ -31,7 +31,7 @@ class PythonHandler : NppPythonScript::PyProducerConsumer<RunScriptArgs*>
3131

3232
void initPython();
3333
void runStartupScripts();
34-
void killScript();
34+
void stopScript();
3535

3636
PyThreadState* getMainThreadState() { return mp_mainThreadState; };
3737

@@ -54,7 +54,7 @@ class PythonHandler : NppPythonScript::PyProducerConsumer<RunScriptArgs*>
5454
// Private methods
5555
void initModules();
5656

57-
static void killScriptWorker(PythonHandler *handler);
57+
static void stopScriptWorker(PythonHandler *handler);
5858

5959
// Private member vars
6060
std::string m_machineBaseDir;

PythonScript/src/PythonScript.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,9 +521,9 @@ void saveSettings(void)
521521

522522
void stopScript()
523523
{
524-
if (g_console)
524+
if (pythonHandler)
525525
{
526-
g_console->stopScript();
526+
pythonHandler->stopScript();
527527
}
528528
}
529529

PythonScript/src/PythonScript.rc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSM
6767
CAPTION "Python Script"
6868
FONT 8, "MS Shell Dlg", 400, 0, 0x1
6969
BEGIN
70-
EDITTEXT IDC_INPUT,28,156,121,14,ES_AUTOHSCROLL
71-
DEFPUSHBUTTON "Run",IDC_RUN,152,156,40,14
70+
EDITTEXT IDC_INPUT,28,156,121,14,ES_AUTOHSCROLL | ES_WANTRETURN
71+
PUSHBUTTON "Run",IDC_RUN,152,156,40,14
7272
LTEXT ">>>",IDC_PROMPT,7,161,17,8
7373
END
7474

0 commit comments

Comments
 (0)