From 548cead6a1473d5cb01964bf7df5f45f07e78c1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Mon, 22 May 2006 20:10:41 +0000 Subject: [PATCH] - Handle args in the form /pathto/file[:line[:column]][:] so you can double-click on a gcc error in the terminal, type open and right-click to edit at the buggy line. Discard last : form gcc errors to avoid having to type backspace. Works with Pe for the line at least, BeIDE doesn't seem to handle it though the documentations says it should. Others apps welcome to handle the be:line and be:column int32s on B_REFS_RECEIVED :) - Updated copyright. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@17546 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/bin/open.cpp | 77 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 58 insertions(+), 19 deletions(-) diff --git a/src/bin/open.cpp b/src/bin/open.cpp index af3d3398a5..b93feaf3d0 100644 --- a/src/bin/open.cpp +++ b/src/bin/open.cpp @@ -1,5 +1,6 @@ /* * Copyright 2003-2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved. + * Copyright 2005-2006, François Revol, revol@free.fr. * Distributed under the terms of the MIT License. */ @@ -19,11 +20,33 @@ const char *kTrackerSignature = "application/x-vnd.Be-TRAK"; +const char *openWith = kTrackerSignature; + + +status_t OpenFile(BEntry &entry, int32 line=-1, int32 col=-1) +{ + status_t status; + entry_ref ref; + entry.GetRef(&ref); + + BMessenger target(openWith); + if (target.IsValid()) { + BMessage message(B_REFS_RECEIVED); + message.AddRef("refs", &ref); + if (line > -1) + message.AddInt32("be:line", line); + if (col > -1) + message.AddInt32("be:column", col); + // tell the app to open the file + status = target.SendMessage(&message); + } else + status = be_roster->Launch(&ref); + return status; +} int main(int argc, char **argv) { - const char *openWith = kTrackerSignature; int exitcode = EXIT_SUCCESS; char *progName = argv[0]; @@ -31,9 +54,7 @@ main(int argc, char **argv) progName = strrchr(progName, '/') + 1; if (argc < 2) - fprintf(stderr,"usage: %s ...\n", progName); - - BRoster roster; + fprintf(stderr,"usage: %s ...\n", progName); while (*++argv) { status_t status = B_OK; @@ -41,17 +62,7 @@ main(int argc, char **argv) BEntry entry(*argv); if ((status = entry.InitCheck()) == B_OK && entry.Exists()) { - entry_ref ref; - entry.GetRef(&ref); - - BMessenger target(openWith); - if (target.IsValid()) { - BMessage message(B_REFS_RECEIVED); - message.AddRef("refs", &ref); - // tell the app to open the file - status = target.SendMessage(&message); - } else - status = roster.Launch(&ref); + status = OpenFile(entry); } else if (!strncasecmp("application/", *argv, 12)) { // maybe it's an application-mimetype? @@ -64,10 +75,10 @@ main(int argc, char **argv) // don't start it twice if we have other args BList teams; if (argc > 1) - roster.GetAppList(*argv, &teams); + be_roster->GetAppList(*argv, &teams); if (teams.IsEmpty()) - status = roster.Launch(*argv); + status = be_roster->Launch(*argv); } else if (strchr(*argv, ':')) { // try to open it as an URI BString mimeType = "application/x-vnd.Be.URL."; @@ -78,9 +89,37 @@ main(int argc, char **argv) mimeType.Append(arg, arg.FindFirst(":")); char *args[2] = { *argv, NULL }; - status = roster.Launch(mimeType.String(), 1, args); - } else + status = be_roster->Launch(mimeType.String(), 1, args); + if (status == B_OK) + continue; + + // maybe it's "file:line" or "file:line:col" + int line = 0, col = 0, i; status = B_ENTRY_NOT_FOUND; + // remove gcc error's last : + if (arg[arg.Length()-1] == ':') + arg.Truncate(arg.Length()-1); + i = arg.FindLast(':'); + if (i > 0) { + line = atoi(arg.String()+i+1); + arg.Truncate(i); + entry.SetTo(arg.String()); + if (entry.Exists()) + status = OpenFile(entry, line-1, col-1); + if (status == B_OK) + continue; + // get the col + col = line; + i = arg.FindLast(':'); + line = atoi(arg.String()+i+1); + arg.Truncate(i); + entry.SetTo(arg.String()); + if (entry.Exists()) + status = OpenFile(entry, line-1, col-1); + } + } else { + status = B_ENTRY_NOT_FOUND; + } if (status != B_OK && status != B_ALREADY_RUNNING) { fprintf(stderr, "%s: \"%s\": %s\n", progName, *argv, strerror(status));