Skip to content

Instantly share code, notes, and snippets.

@codebrainz
Created July 28, 2015 01:03
Show Gist options
  • Save codebrainz/9dd84b5a7c08e710085c to your computer and use it in GitHub Desktop.
Save codebrainz/9dd84b5a7c08e710085c to your computer and use it in GitHub Desktop.
Fix "Open in New Window" feature
diff --git a/src/utils.c b/src/utils.c
index c5edd5a..37ae12a 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -2154,30 +2154,35 @@ const gchar *utils_resource_dir(GeanyResourceDirType type)
void utils_start_new_geany_instance(const gchar *doc_path)
{
- const gchar *const *argv;
const gchar *command = is_osx_bundle() ? "open" : "geany";
gchar *exec_path = g_find_program_in_path(command);
if (exec_path)
{
GError *err = NULL;
+ GPtrArray *args = g_ptr_array_sized_new (6);
+ g_ptr_array_add (args, exec_path);
if (is_osx_bundle())
{
- const gchar *const osx_argv[] = {exec_path, "-n", "-a", "Geany", doc_path, NULL};
- argv = osx_argv;
+ g_ptr_array_add (args, (gpointer) "-n");
+ g_ptr_array_add (args, (gpointer) "-a");
+ g_ptr_array_add (args, (gpointer) "Geany");
+ g_ptr_array_add (args, (gpointer) doc_path);
}
else
{
- const gchar *const unix_argv[] = {exec_path, "-i", doc_path, NULL};
- argv = unix_argv;
+ g_ptr_array_add (args, (gpointer) "-i");
+ g_ptr_array_add (args, (gpointer) doc_path);
}
+ g_ptr_array_add (args, NULL);
- if (!utils_spawn_async(NULL, (gchar**) argv, NULL, 0, NULL, NULL, NULL, &err))
+ if (!utils_spawn_async(NULL, (gchar**) args->pdata, NULL, 0, NULL, NULL, NULL, &err))
{
g_printerr("Unable to open new window: %s", err->message);
g_error_free(err);
}
+ g_ptr_array_free (args, TRUE);
g_free(exec_path);
}
else
diff --git a/src/utils.c b/src/utils.c
index c5edd5a..8638b13 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -2154,24 +2154,29 @@ const gchar *utils_resource_dir(GeanyResourceDirType type)
void utils_start_new_geany_instance(const gchar *doc_path)
{
- const gchar *const *argv;
const gchar *command = is_osx_bundle() ? "open" : "geany";
gchar *exec_path = g_find_program_in_path(command);
if (exec_path)
{
GError *err = NULL;
+ const gchar *argv[6]; // max args + 1
+ gint argc = 0;
+ argv[argc++] = exec_path;
if (is_osx_bundle())
{
- const gchar *const osx_argv[] = {exec_path, "-n", "-a", "Geany", doc_path, NULL};
- argv = osx_argv;
+ argv[argc++] = "-n";
+ argv[argc++] = "-a";
+ argv[argc++] = "Geany";
+ argv[argc++] = doc_path;
}
else
{
- const gchar *const unix_argv[] = {exec_path, "-i", doc_path, NULL};
- argv = unix_argv;
+ argv[argc++] = "-i";
+ argv[argc++] = doc_path;
}
+ argv[argc++] = NULL;
if (!utils_spawn_async(NULL, (gchar**) argv, NULL, 0, NULL, NULL, NULL, &err))
{
@elextr
Copy link

elextr commented Jul 28, 2015

Well, I would just move the declarations of osx_argv and unix_argv outside the if, but that isn't absolutely the maximum efficiency so I expect the premature optimisers will reject that :)

@codebrainz
Copy link
Author

I assume the osx_ and unix_ arrays were only used to take advantage C's (semi-)convenient initialization syntax, and since this doesn't use that anyway, it seems more natural to just use a single array big enough to handle both kinds of argument vectors.

@kugel-
Copy link

kugel- commented Aug 3, 2015

The last increment (argc++) is superflous but otherwise looks fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment