Skip to content

Commit

Permalink
Partial/alternative solution to #64.
Browse files Browse the repository at this point in the history
Added a new "Execute command and send output to Worksheet" command.
Default keybinding is CMD+Shift+ENTER.

On existing Pe installations, you need to manually assign a keybinding
for the new command, under:

Preferences->Keybindings->Miscellaneous
  • Loading branch information
OscarL committed Mar 15, 2024
1 parent c69274c commit 3e2f5e5
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Resources/Bindings.r
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ resource rtyp_Cmnd (rid_Cmnd_Miscellaneous, "Miscellaneous") {
kmsg_SplitWindow, "Split window",
msg_CancelCommand, "Cancel executing command",
msg_Execute, "Execute command",
msg_ExecuteToWorksheet, "Execute command (w/ output to Worksheet)",
msg_SwitchHeaderSource, "Open header/source",
}
};
Expand Down Expand Up @@ -421,6 +422,7 @@ resource rtyp_Bind (rid_Bind_Miscellaneous, "Miscellaneous bindings") {
{
Ctrl, 0x63, 0, 0, msg_CancelCommand,
Cmd, 0x0a, 0, 0, msg_Execute,
Cmd|Shift, 0x0a, 0, 0, msg_ExecuteToWorksheet,
// 0, 0x0a, 0, 0, msg_Execute,
Cmd, 0x09, 0, 0, msg_SwitchHeaderSource
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/PExec.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ class PErrorWindow;

class PExec : public MThread {
public:
PExec(PText *txt, const char *cmd, const char *wd);
PExec(PText *txt, const char *cmd, const char *wd, PText *dest=NULL);
~PExec();

virtual long Execute();

PText *fDest;
PText *fText;
BLooper *fWindow;
PErrorWindow *fErrorWindow;
Expand Down
1 change: 1 addition & 0 deletions Sources/PMessages.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@

#define msg_OutputWillFollow 'OutP'
#define msg_Execute 'Exec'
#define msg_ExecuteToWorksheet 'ExWs'
#define msg_ExecFinished 'Fnsh'
#define msg_TypeString 'TypS'
#define msg_Preferences 'Pref'
Expand Down
38 changes: 34 additions & 4 deletions Sources/PText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4447,10 +4447,12 @@ const char
#include "PExec.h"
#include "PErrorWindow.h"

PExec::PExec(PText *txt, const char *cmd, const char *wd)
PExec::PExec(PText *txt, const char *cmd, const char *wd, PText *dest)
: MThread("execvp")
{
fText = txt;
fDest = dest; // destination for "msg_ExecFinished" if not NULL.

fWindow = fText->Looper();

BPath appDirPath, settingsDirPath;
Expand Down Expand Up @@ -4637,14 +4639,17 @@ long PExec::Execute()
}

BMessage msg(msg_ExecFinished);
fWindow->PostMessage(&msg, fText);
if (fDest != NULL)
fDest->Looper()->PostMessage(&msg, fDest);
else
fWindow->PostMessage(&msg, fText);

return 0;
} /* PExec::Execute */

// #pragma mark -

void PText::ExecuteSelection()
void PText::ExecuteSelection(bool outputToWorkSheet)
{
if (fExec)
{
Expand All @@ -4669,7 +4674,28 @@ void PText::ExecuteSelection()
fText.Copy(s, from, to - from);
}

fExec = new PExec(this, s, fCWD);
PText* outputText = this;
if (outputToWorkSheet)
{
if (!this->Doc()->IsWorksheet())
{
// We're not the Worksheet, is one already open?.
PDoc *doc = PDoc::GetWorksheet();

if (doc && !doc->IsWorksheet()) // GetWorksheet() bogus?
{
// Let's open it ourselves.
PApp *app = dynamic_cast<PApp*>(be_app);
doc = app->OpenWorksheet();
}
outputText = doc->TextView();
}
}

// if outputText != this, PExec's fWindow needs to be set to *this* 'this', otherwise
// the doc window never gets the msg_ExecFinished message, causing the
// "Execute Command" button to gets stuck on low.
fExec = new PExec(outputText, s, fCWD, (outputText != this) ? this : NULL);
fExec->Run();
Doc()->ButtonBar()->SetDown(msg_Execute, true);

Expand Down Expand Up @@ -6127,6 +6153,10 @@ void PText::MessageReceived(BMessage *msg)
ExecuteSelection();
break;

case msg_ExecuteToWorksheet:
ExecuteSelection(true);
break;

case msg_CancelCommand:
KillCurrentJob();
break;
Expand Down
2 changes: 1 addition & 1 deletion Sources/PText.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ virtual void FrameResized(float w, float h);
void JumpToFunction(const char *func, int32 offset);
void FindNextError(bool backward);

void ExecuteSelection();
void ExecuteSelection(bool outputToWorkSheet=false);
void KillCurrentJob();
void PrepareForOutput();
void SetCWD(const char *cwd);
Expand Down

0 comments on commit 3e2f5e5

Please sign in to comment.