From ed240dc1b49101db2e2b9b6ac96d9c9521dae5e7 Mon Sep 17 00:00:00 2001 From: Oscar Lesta Date: Tue, 5 Mar 2024 07:15:54 -0300 Subject: [PATCH] Implement `lpe file:line:col` Fixes #68. --- Sources/PApp.cpp | 24 ++++++++++++++++++++---- lpe/lpe.cpp | 33 ++++++++++++++++++++------------- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/Sources/PApp.cpp b/Sources/PApp.cpp index dbdb725..15dd631 100644 --- a/Sources/PApp.cpp +++ b/Sources/PApp.cpp @@ -866,10 +866,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(OpenWindow(doc)); + if (d) + { + int32 offset = d->TextView()->Column2Offset(lineNr, colNr); + 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) diff --git a/lpe/lpe.cpp b/lpe/lpe.cpp index 91c102f..75a7734 100644 --- a/lpe/lpe.cpp +++ b/lpe/lpe.cpp @@ -44,6 +44,7 @@ #include #include #include +#include const long msg_CommandLineOpen = 'Cmdl'; @@ -53,12 +54,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 ] [file:linenr | +linenr [file] | file] " - "..."); + puts("usage: lpe [--type ] [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 specifies the file extension to"); puts("be used to help Pe recognize the content type."); @@ -80,7 +80,7 @@ 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); @@ -88,6 +88,9 @@ void OpenInPe(entry_ref& doc, int lineNr) 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!"); @@ -152,12 +155,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; @@ -190,14 +192,19 @@ int main(int argc, char *argv[]) { pathSeen = true; path = argv[i]; - dpPtr = strrchr(argv[i], ':'); - if (dpPtr != NULL) + + BStringList parts; + if (path.Split(":", true, parts) && parts.CountStrings()) { - nr = strtoul(dpPtr + 1, &p, 10); - if (strlen(p) == 0) + switch (parts.CountStrings()) { - path.SetTo(argv[i], dpPtr-argv[i]); - lineNr = nr; + case 3: + colNr = atoi(parts.StringAt(2).String()) - 1; + case 2: + lineNr = atoi(parts.StringAt(1).String()) - 1; + case 1: + path = parts.StringAt(0); + break; } } @@ -209,7 +216,7 @@ 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; } }