Skip to content

Commit

Permalink
Support opening files with file:line:col style arguments.
Browse files Browse the repository at this point in the history
(support for ":col" is what's new)

lpe uses a custom message, while `/bin/open file:line:col` (what Terminal uses when
"hyperlinking" to filenames, for example) uses a B_REFS_RECEIVED message.

We support both.

Fixes #68.
  • Loading branch information
OscarL committed Nov 18, 2024
1 parent 13a6797 commit fcb94e4
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 23 deletions.
49 changes: 39 additions & 10 deletions Sources/PApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,16 +711,29 @@ void PApp::RefsReceived(BMessage *inMessage)
}
else if (inMessage->HasInt32("be:line"))
{
int32 line;
int32 line, column;

FailOSErr(inMessage->FindInt32("be:line", &line));

BMessage msg(msg_SelectLines);
FailOSErr(msg.AddInt32("from", line));
FailOSErr(msg.AddInt32("to", line - 1));

BMessenger msgr(d->TextView());
FailOSErr(msgr.SendMessage(&msg));

if (inMessage->FindInt32("be:column", &column) == B_OK)
{
int32 offset = d->TextView()->Column2Offset(line - 1, column - 1);
BMessage msg(msg_Select);
FailOSErr(msg.AddInt32("anchor", offset));
FailOSErr(msg.AddInt32("caret", offset));

FailOSErr(msgr.SendMessage(&msg));
}
else
{
BMessage msg(msg_SelectLines);
FailOSErr(msg.AddInt32("from", line));
FailOSErr(msg.AddInt32("to", line - 1));

FailOSErr(msgr.SendMessage(&msg));
}
}
}
}
Expand Down Expand Up @@ -893,10 +906,26 @@ void PApp::MessageReceived(BMessage *msg)
int32 lineNr;
if (w && msg->FindInt32("line", &lineNr) == B_OK)
{
BMessage m(msg_SelectLines);
FailOSErr(m.AddInt32("from", lineNr));
FailOSErr(m.AddInt32("to", lineNr - 1));
w->PostMessage(&m, w->PreferredHandler());
int32 colNr;
if (msg->FindInt32("column", &colNr) == B_OK)
{
PDoc *d = dynamic_cast<PDoc*>(OpenWindow(doc));
if (d)
{
int32 offset = d->TextView()->Column2Offset(lineNr - 1, colNr - 1);
BMessage m(msg_Select);
FailOSErr(m.AddInt32("anchor", offset));
FailOSErr(m.AddInt32("caret", offset));
w->PostMessage(&m, w->PreferredHandler());
}
}
else
{
BMessage m(msg_SelectLines);
FailOSErr(m.AddInt32("from", lineNr));
FailOSErr(m.AddInt32("to", lineNr - 1));
w->PostMessage(&m, w->PreferredHandler());
}
}

if (w)
Expand Down
57 changes: 44 additions & 13 deletions lpe/lpe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,11 @@ static BString sTempFilePath;

void DoError(const char *e, ...);
void Usage(bool error);
void OpenInPe(entry_ref& ref, int lineNr);
void OpenInPe(entry_ref& ref, int lineNr, int colNr=-1);

void Usage(bool error)
{
puts("usage: lpe [--type <fileType>] [file:linenr | +linenr [file] | file] "
"...");
puts("usage: lpe [--type <fileType>] [file:linenr[:colnr] | +linenr [file] | file] ...");
puts("If no file has been specified, copy stdin to a temporary file and");
puts("open that. In that case <fileType> specifies the file extension to");
puts("be used to help Pe recognize the content type.");
Expand All @@ -80,14 +79,17 @@ void DoError(const char *e, ...)
exit(1);
} /* error */

void OpenInPe(entry_ref& doc, int lineNr)
void OpenInPe(entry_ref& doc, int lineNr, int colNr)
{
BMessage msg(msg_CommandLineOpen), reply;
msg.AddRef("refs", &doc);

if (lineNr >= 0)
msg.AddInt32("line", lineNr);

if (colNr >= 0)
msg.AddInt32("column", colNr);

entry_ref pe;
if (be_roster->FindApp("application/x-vnd.beunited.pe", &pe))
DoError("Could not find Pe!");
Expand Down Expand Up @@ -152,12 +154,11 @@ int main(int argc, char *argv[])
{
int i = 0;
char *p;
char *dpPtr;
int lineNr = -1;
int colNr = -1;
status_t err;
BEntry e;
BString path;
int nr;
bool pathSeen = false;
const char* fileType = NULL;

Expand Down Expand Up @@ -190,14 +191,43 @@ int main(int argc, char *argv[])
{
pathSeen = true;
path = argv[i];
dpPtr = strrchr(argv[i], ':');
if (dpPtr != NULL)

err = e.SetTo(path.String());
if (err == B_OK && !e.Exists() && path.FindLast(':'))
{
nr = strtoul(dpPtr + 1, &p, 10);
if (strlen(p) == 0)
// remove final ':', if any.
if (path[path.Length() - 1] == ':')
path.Truncate(path.Length() - 1);

err = e.SetTo(path.String());
if (err == B_OK && !e.Exists())
{
path.SetTo(argv[i], dpPtr-argv[i]);
lineNr = nr;
// See if we find "file:line" or "file:line:col"
i = path.FindLast(':');
if (i > 0)
{
// "file:line"
errno = 0;
lineNr = strtol(path.String() + i + 1, NULL, 10);
if (errno == ERANGE)
lineNr = -1;

path.Truncate(i);

err = e.SetTo(path.String());
if (err == B_OK && !e.Exists())
{
// "file:line:col"
colNr = lineNr;
i = path.FindLast(':');
errno = 0;
lineNr = strtol(path.String() + i + 1, NULL, 10);
if (errno == ERANGE)
lineNr = -1;

path.Truncate(i);
}
}
}
}

Expand All @@ -209,8 +239,9 @@ int main(int argc, char *argv[])
entry_ref ref;
err = e.GetRef(&ref);
if (err) DoError("Error trying to access file %s, (%s)", path.String(), strerror(err));
OpenInPe(ref, lineNr);
OpenInPe(ref, lineNr, colNr);
lineNr = -1;
colNr = -1;
}
}
}
Expand Down

0 comments on commit fcb94e4

Please sign in to comment.