changeset 1652:58a5d1e01e33

run external commands from current directory even with no files
author nadvornik
date Fri, 19 Jun 2009 22:34:52 +0000
parents 8966e72ae99a
children f66934d3706b
files src/collect-table.c src/dupe.c src/editors.c src/editors.h src/layout_util.c src/search.c src/utilops.c src/utilops.h src/view_file.c
diffstat 9 files changed, 43 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/src/collect-table.c	Thu Jun 18 20:46:33 2009 +0000
+++ b/src/collect-table.c	Fri Jun 19 22:34:52 2009 +0000
@@ -666,7 +666,7 @@
 	list = collection_table_popup_file_list(ct);
 	if (list)
 		{
-		file_util_start_editor_from_filelist(key, list, ct->listview);
+		file_util_start_editor_from_filelist(key, list, NULL, ct->listview);
 		filelist_free(list);
 		}
 }
--- a/src/dupe.c	Thu Jun 18 20:46:33 2009 +0000
+++ b/src/dupe.c	Fri Jun 19 22:34:52 2009 +0000
@@ -2046,7 +2046,7 @@
 
 	list = dupe_listview_get_selection(dw, dw->listview);
 
-	file_util_start_editor_from_filelist(key, list, dw->window);
+	file_util_start_editor_from_filelist(key, list, NULL, dw->window);
 
 	filelist_free(list);
 }
--- a/src/editors.c	Thu Jun 18 20:46:33 2009 +0000
+++ b/src/editors.c	Fri Jun 19 22:34:52 2009 +0000
@@ -52,6 +52,7 @@
 	EditorCallback callback;
 	gpointer data;
 	const EditorDescription *editor;
+	gchar *working_directory; /* fallback if no files are given (editor_no_param) */
 };
 
 
@@ -464,6 +465,7 @@
 static void editor_data_free(EditorData *ed)
 {
 	editor_verbose_data_free(ed);
+	g_free(ed->working_directory);
 	g_free(ed);
 }
 
@@ -963,7 +965,7 @@
 		gchar *args[4];
 		guint n = 0;
 
-		working_directory = fd ? remove_level_from_path(fd->path) : NULL;
+		working_directory = fd ? remove_level_from_path(fd->path) : g_strdup(ed->working_directory);
 		args[n++] = options->shell.path;
 		if (options->shell.options && *options->shell.options)
 			args[n++] = options->shell.options;
@@ -1165,7 +1167,7 @@
 	editor_command_done(ed);
 }
 
-static EditorFlags editor_command_start(const EditorDescription *editor, const gchar *text, GList *list, EditorCallback cb, gpointer data)
+static EditorFlags editor_command_start(const EditorDescription *editor, const gchar *text, GList *list, const gchar *working_directory, EditorCallback cb, gpointer data)
 {
 	EditorData *ed;
 	EditorFlags flags = editor->flags;
@@ -1178,7 +1180,8 @@
 	ed->editor = editor;
 	ed->total = (flags & (EDITOR_SINGLE_COMMAND | EDITOR_NO_PARAM)) ? 1 : g_list_length(list);
 	ed->callback = cb;
-	ed->data =  data;
+	ed->data = data;
+	ed->working_directory = g_strdup(working_directory);
 
 	if ((flags & EDITOR_VERBOSE_MULTI) && list && list->next)
 		flags |= EDITOR_VERBOSE;
@@ -1197,7 +1200,7 @@
 	return g_hash_table_lookup(editors, key) != NULL;
 }
 
-EditorFlags start_editor_from_filelist_full(const gchar *key, GList *list, EditorCallback cb, gpointer data)
+EditorFlags start_editor_from_filelist_full(const gchar *key, GList *list, const gchar *working_directory, EditorCallback cb, gpointer data)
 {
 	EditorFlags error;
 	EditorDescription *editor;
@@ -1208,7 +1211,7 @@
 	if (!editor) return FALSE;
 	if (!list && !(editor->flags & EDITOR_NO_PARAM)) return FALSE;
 
-	error = editor_command_start(editor, editor->name, list, cb, data);
+	error = editor_command_start(editor, editor->name, list, working_directory, cb, data);
 
 	if (EDITOR_ERRORS(error))
 		{
@@ -1223,7 +1226,7 @@
 
 EditorFlags start_editor_from_filelist(const gchar *key, GList *list)
 {
-	return start_editor_from_filelist_full(key, list,  NULL, NULL);
+	return start_editor_from_filelist_full(key, list, NULL, NULL, NULL);
 }
 
 EditorFlags start_editor_from_file_full(const gchar *key, FileData *fd, EditorCallback cb, gpointer data)
@@ -1234,7 +1237,7 @@
 	if (!fd) return FALSE;
 
 	list = g_list_append(NULL, fd);
-	error = start_editor_from_filelist_full(key, list, cb, data);
+	error = start_editor_from_filelist_full(key, list, NULL, cb, data);
 	g_list_free(list);
 	return error;
 }
@@ -1244,9 +1247,9 @@
 	return start_editor_from_file_full(key, fd, NULL, NULL);
 }
 
-EditorFlags start_editor(const gchar *key)
+EditorFlags start_editor(const gchar *key, const gchar *working_directory)
 {
-	return start_editor_from_filelist_full(key, NULL, NULL, NULL);
+	return start_editor_from_filelist_full(key, NULL, working_directory, NULL, NULL);
 }
 
 gboolean editor_window_flag_set(const gchar *key)
--- a/src/editors.h	Thu Jun 18 20:46:33 2009 +0000
+++ b/src/editors.h	Fri Jun 19 22:34:52 2009 +0000
@@ -99,11 +99,11 @@
 
 
 
-EditorFlags start_editor(const gchar *key);
+EditorFlags start_editor(const gchar *key, const gchar *working_directory);
 EditorFlags start_editor_from_file(const gchar *key, FileData *fd);
 EditorFlags start_editor_from_filelist(const gchar *key, GList *list);
 EditorFlags start_editor_from_file_full(const gchar *key, FileData *fd, EditorCallback cb, gpointer data);
-EditorFlags start_editor_from_filelist_full(const gchar *key, GList *list, EditorCallback cb, gpointer data);
+EditorFlags start_editor_from_filelist_full(const gchar *key, GList *list, const gchar *working_directory, EditorCallback cb, gpointer data);
 gboolean editor_window_flag_set(const gchar *key);
 gboolean editor_is_filter(const gchar *key);
 gboolean editor_no_param(const gchar *key);
--- a/src/layout_util.c	Thu Jun 18 20:46:33 2009 +0000
+++ b/src/layout_util.c	Fri Jun 19 22:34:52 2009 +0000
@@ -998,7 +998,7 @@
 		layout_exit_fullscreen(lw);
 
 	list = layout_selection_list(lw);
-	file_util_start_editor_from_filelist(key, list, lw->window);
+	file_util_start_editor_from_filelist(key, list, layout_get_path(lw), lw->window);
 	filelist_free(list);
 }
 
--- a/src/search.c	Thu Jun 18 20:46:33 2009 +0000
+++ b/src/search.c	Fri Jun 19 22:34:52 2009 +0000
@@ -665,7 +665,7 @@
 	GList *list;
 
 	list = search_result_selection_list(sd);
-	file_util_start_editor_from_filelist(key, list, sd->window);
+	file_util_start_editor_from_filelist(key, list, NULL, sd->window);
 	filelist_free(list);
 }
 
--- a/src/utilops.c	Thu Jun 18 20:46:33 2009 +0000
+++ b/src/utilops.c	Fri Jun 19 22:34:52 2009 +0000
@@ -848,7 +848,7 @@
 			if (editor_blocks_file(ud->external_command))
 				{
 				DEBUG_1("Starting %s and waiting for results", ud->external_command);
-				flags = start_editor_from_filelist_full(ud->external_command, ud->flist, file_util_perform_ci_cb, ud);
+				flags = start_editor_from_filelist_full(ud->external_command, ud->flist, NULL, file_util_perform_ci_cb, ud);
 				}
 			else
 				{
@@ -2164,7 +2164,7 @@
 	file_util_dialog_run(ud);
 }
 
-static void file_util_start_editor_full(const gchar *key, FileData *source_fd, GList *source_list, const gchar *dest_path, GtkWidget *parent, UtilityPhase phase)
+static void file_util_start_editor_full(const gchar *key, FileData *source_fd, GList *source_list, const gchar *dest_path, const gchar *working_directory, GtkWidget *parent, UtilityPhase phase)
 {
 	UtilityData *ud;
 	GList *flist;
@@ -2172,8 +2172,21 @@
 	
 	if (editor_no_param(key))
 		{
+		gchar *file_directory = NULL;
+		if (!working_directory)
+			{
+			/* working directory was not specified, try to extract it from the files */
+			if (source_fd)
+				file_directory = remove_level_from_path(source_fd->path);
+
+			if (!file_directory && source_list)
+				file_directory = remove_level_from_path(((FileData *)source_list->data)->path);
+			working_directory = file_directory;
+			}
+		
 		/* just start the editor, don't care about files */
-		start_editor(key);
+		start_editor(key, working_directory);
+		g_free(file_directory);
 		return;
 		}
 	
@@ -2715,22 +2728,22 @@
 
 void file_util_start_editor_from_file(const gchar *key, FileData *fd, GtkWidget *parent)
 {
-	file_util_start_editor_full(key, fd, NULL, NULL, parent, UTILITY_PHASE_ENTERING);
+	file_util_start_editor_full(key, fd, NULL, NULL, NULL, parent, UTILITY_PHASE_ENTERING);
 }
 
-void file_util_start_editor_from_filelist(const gchar *key, GList *list, GtkWidget *parent)
+void file_util_start_editor_from_filelist(const gchar *key, GList *list, const gchar *working_directory, GtkWidget *parent)
 {
-	file_util_start_editor_full(key, NULL, list, NULL, parent, UTILITY_PHASE_ENTERING);
+	file_util_start_editor_full(key, NULL, list, NULL, working_directory, parent, UTILITY_PHASE_ENTERING);
 }
 
 void file_util_start_filter_from_file(const gchar *key, FileData *fd, const gchar *dest_path, GtkWidget *parent)
 {
-	file_util_start_editor_full(key, fd, NULL, dest_path, parent, UTILITY_PHASE_ENTERING);
+	file_util_start_editor_full(key, fd, NULL, dest_path, NULL, parent, UTILITY_PHASE_ENTERING);
 }
 
 void file_util_start_filter_from_filelist(const gchar *key, GList *list, const gchar *dest_path, GtkWidget *parent)
 {
-	file_util_start_editor_full(key, NULL, list, dest_path, parent, UTILITY_PHASE_ENTERING);
+	file_util_start_editor_full(key, NULL, list, dest_path, NULL, parent, UTILITY_PHASE_ENTERING);
 }
 
 void file_util_delete_dir(FileData *fd, GtkWidget *parent)
--- a/src/utilops.h	Thu Jun 18 20:46:33 2009 +0000
+++ b/src/utilops.h	Fri Jun 19 22:34:52 2009 +0000
@@ -51,7 +51,9 @@
 void file_util_rename_simple(FileData *fd, const gchar *dest_path, GtkWidget *parent);
 
 void file_util_start_editor_from_file(const gchar *key, FileData *fd, GtkWidget *parent);
-void file_util_start_editor_from_filelist(const gchar *key, GList *list, GtkWidget *parent);
+
+/* working directory is used only as a fallback when the filelist is empty */
+void file_util_start_editor_from_filelist(const gchar *key, GList *list, const gchar *working_directory, GtkWidget *parent);
 void file_util_start_filter_from_file(const gchar *key, FileData *fd, const gchar *dest_path, GtkWidget *parent);
 void file_util_start_filter_from_filelist(const gchar *key, GList *list, const gchar *dest_path, GtkWidget *parent);
 
--- a/src/view_file.c	Thu Jun 18 20:46:33 2009 +0000
+++ b/src/view_file.c	Fri Jun 19 22:34:52 2009 +0000
@@ -321,7 +321,7 @@
 	if (!vf) return;
 
 	list = vf_pop_menu_file_list(vf);
-	file_util_start_editor_from_filelist(key, list, vf->listview);
+	file_util_start_editor_from_filelist(key, list, vf->dir_fd->path, vf->listview);
 	filelist_free(list);
 }