Trim trailing white spaces.
[geeqie.git] / src / editors.c
index 67e24e7..270c5d3 100644 (file)
@@ -1,6 +1,7 @@
 /*
- * GQview
+ * Geeqie
  * (C) 2006 John Ellis
+ * Copyright (C) 2008 - 2012 The Geeqie Team
  *
  * Author: John Ellis
  *
  */
 
 
-#include "gqview.h"
+#include "main.h"
 #include "editors.h"
 
-#include "utilops.h"
+#include "filedata.h"
+#include "filefilter.h"
+#include "misc.h"
+#include "pixbuf_util.h"
 #include "ui_fileops.h"
 #include "ui_spinner.h"
 #include "ui_utildlg.h"
+#include "utilops.h"
 
 #include <errno.h>
 
 #define EDITOR_WINDOW_WIDTH 500
 #define EDITOR_WINDOW_HEIGHT 300
 
-#define COMMAND_SHELL "sh"
-#define COMMAND_OPT  "-c"
 
 
 typedef struct _EditorVerboseData EditorVerboseData;
 struct _EditorVerboseData {
-       int fd;
-
        GenericDialog *gd;
        GtkWidget *button_close;
        GtkWidget *button_stop;
        GtkWidget *text;
        GtkWidget *progress;
        GtkWidget *spinner;
-       gint count;
-       gint total;
-
-       gchar *command_template;
-       GList *list;
 };
 
-
-static gchar *editor_slot_defaults[GQVIEW_EDITOR_SLOTS * 2] = {
-       N_("The Gimp"), "gimp-remote -n %f",
-       N_("XV"), "xv %f",
-       N_("Xpaint"), "xpaint %f",
-       NULL, NULL,
-       NULL, NULL,
-       NULL, NULL,
-       NULL, NULL,
-       NULL, NULL,
-       N_("Rotate jpeg clockwise"), "%vif jpegtran -rotate 90 -copy all -outfile %p_tmp %p; then mv %p_tmp %p;else rm %p_tmp;fi",
-       N_("Rotate jpeg counterclockwise"), "%vif jpegtran -rotate 270 -copy all -outfile %p_tmp %p; then mv %p_tmp %p;else rm %p_tmp;fi",
-       /* special slots */
-#if 1
-       /* for testing */
-       "External Copy command", "%vset -x;cp %p %t",
-       "External Move command", "%vset -x;mv %p %t",
-       "External Rename command", "%vset -x;mv %p %t",
-       "External Delete command", "%vset -x;rm %f",
-       "External New Folder command", NULL
-#else
-       "External Copy command", NULL,
-       "External Move command", NULL,
-       "External Rename command", NULL,
-       "External Delete command", NULL,
-       "External New Folder command", NULL
-#endif
+typedef struct _EditorData EditorData;
+struct _EditorData {
+       EditorFlags flags;
+       GPid pid;
+       GList *list;
+       gint count;
+       gint total;
+       gboolean stopping;
+       EditorVerboseData *vd;
+       EditorCallback callback;
+       gpointer data;
+       const EditorDescription *editor;
+       gchar *working_directory; /* fallback if no files are given (editor_no_param) */
 };
 
-static void editor_verbose_window_progress(EditorVerboseData *vd, const gchar *text);
-static gint editor_command_next(EditorVerboseData *vd);
 
+static void editor_verbose_window_progress(EditorData *ed, const gchar *text);
+static EditorFlags editor_command_next_start(EditorData *ed);
+static EditorFlags editor_command_next_finish(EditorData *ed, gint status);
+static EditorFlags editor_command_done(EditorData *ed);
 
 /*
  *-----------------------------------------------------------------------------
@@ -84,37 +68,443 @@ static gint editor_command_next(EditorVerboseData *vd);
  *-----------------------------------------------------------------------------
  */
 
-void editor_reset_defaults(void)
+GHashTable *editors = NULL;
+GtkListStore *desktop_file_list;
+gboolean editors_finished = FALSE;
+
+#ifdef G_KEY_FILE_DESKTOP_GROUP
+#define DESKTOP_GROUP G_KEY_FILE_DESKTOP_GROUP
+#else
+#define DESKTOP_GROUP "Desktop Entry"
+#endif
+
+void editor_description_free(EditorDescription *editor)
+{
+       if (!editor) return;
+       
+       g_free(editor->key);
+       g_free(editor->name);
+       g_free(editor->icon);
+       g_free(editor->exec);
+       g_free(editor->menu_path);
+       g_free(editor->hotkey);
+       g_free(editor->comment);
+       string_list_free(editor->ext_list);
+       g_free(editor->file);
+       g_free(editor);
+}
+
+static GList *editor_mime_types_to_extensions(gchar **mime_types)
+{
+       /* FIXME: this should be rewritten to use the shared mime database, as soon as we switch to gio */
+       
+       static const gchar *conv_table[][2] = {
+               {"application/x-ufraw", ".ufraw"},
+               {"image/*",             "*"},
+               {"image/bmp",           ".bmp"},
+               {"image/gif",           ".gif"},
+               {"image/jpeg",          ".jpeg;.jpg"},
+               {"image/jpg",           ".jpg;.jpeg"},
+               {"image/pcx",           ".pcx"},
+               {"image/png",           ".png"},
+               {"image/svg",           ".svg"},
+               {"image/svg+xml",       ".svg"},
+               {"image/svg+xml-compressed",    ".svg"},
+               {"image/tiff",          ".tiff;.tif"},
+               {"image/x-bmp",         ".bmp"},
+               {"image/x-canon-crw",   ".crw"},
+               {"image/x-cr2",         ".cr2"},
+               {"image/x-dcraw",       "%raw"},
+               {"image/x-ico",         ".ico"},
+               {"image/x-mrw",         ".mrw"},
+               {"image/x-MS-bmp",      ".bmp"},
+               {"image/x-nef",         ".nef"},
+               {"image/x-orf",         ".orf"},
+               {"image/x-pcx",         ".pcx"},
+               {"image/xpm",           ".xpm"},
+               {"image/x-png",         ".png"},
+               {"image/x-portable-anymap",     ".pam"},
+               {"image/x-portable-bitmap",     ".pbm"},
+               {"image/x-portable-graymap",    ".pgm"},
+               {"image/x-portable-pixmap",     ".ppm"},
+               {"image/x-psd",         ".psd"},
+               {"image/x-raf",         ".raf"},
+               {"image/x-sgi",         ".sgi"},
+               {"image/x-tga",         ".tga"},
+               {"image/x-xbitmap",     ".xbm"},
+               {"image/x-xcf",         ".xcf"},
+               {"image/x-xpixmap",     ".xpm"},
+               {"image/x-x3f",         ".x3f"},
+               {"application/x-ptoptimizer-script",    ".pto"},
+               {NULL, NULL}};
+       
+       gint i, j;
+       GList *list = NULL;
+       
+       for (i = 0; mime_types[i]; i++)
+               for (j = 0; conv_table[j][0]; j++)
+                       if (strcmp(mime_types[i], conv_table[j][0]) == 0)
+                               list = g_list_concat(list, filter_to_list(conv_table[j][1]));
+       
+       return list;
+}
+
+gboolean editor_read_desktop_file(const gchar *path)
+{
+       GKeyFile *key_file;
+       EditorDescription *editor;
+       gchar *extensions;
+       gchar *type;
+       const gchar *key = filename_from_path(path);
+       gchar **categories, **only_show_in, **not_show_in;
+       gchar *try_exec;
+       GtkTreeIter iter;
+       gboolean category_geeqie = FALSE;
+
+       if (g_hash_table_lookup(editors, key)) return FALSE; /* the file found earlier wins */
+       
+       key_file = g_key_file_new();
+       if (!g_key_file_load_from_file(key_file, path, 0, NULL))
+               {
+               g_key_file_free(key_file);
+               return FALSE;
+               }
+
+       type = g_key_file_get_string(key_file, DESKTOP_GROUP, "Type", NULL);
+       if (!type || strcmp(type, "Application") != 0)
+               {
+               /* We only consider desktop entries of Application type */
+               g_key_file_free(key_file);
+               g_free(type);
+               return FALSE;
+               }
+       g_free(type);
+       
+       editor = g_new0(EditorDescription, 1);
+       
+       editor->key = g_strdup(key);
+       editor->file = g_strdup(path);
+
+       g_hash_table_insert(editors, editor->key, editor);
+
+       if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "Hidden", NULL)
+           || g_key_file_get_boolean(key_file, DESKTOP_GROUP, "NoDisplay", NULL))
+               {
+               editor->hidden = TRUE;
+               }
+
+       categories = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "Categories", NULL, NULL);
+       if (categories)
+               {
+               gboolean found = FALSE;
+               gint i;
+               for (i = 0; categories[i]; i++)
+                       {
+                       /* IMHO "Graphics" is exactly the category that we are interested in, so this does not have to be configurable */
+                       if (strcmp(categories[i], "Graphics") == 0)
+                               {
+                               found = TRUE;
+                               }
+                       if (strcmp(categories[i], "X-Geeqie") == 0)
+                               {
+                               found = TRUE;
+                               category_geeqie = TRUE;
+                               break;
+                               }
+                       }
+               if (!found) editor->ignored = TRUE;
+               g_strfreev(categories);
+               }
+       else
+               {
+               editor->ignored = TRUE;
+               }
+
+       only_show_in = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "OnlyShowIn", NULL, NULL);
+       if (only_show_in)
+               {
+               gboolean found = FALSE;
+               gint i;
+               for (i = 0; only_show_in[i]; i++)
+                       if (strcmp(only_show_in[i], "X-Geeqie") == 0)
+                               {
+                               found = TRUE;
+                               break;
+                               }
+               if (!found) editor->ignored = TRUE;
+               g_strfreev(only_show_in);
+               }
+
+       not_show_in = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "NotShowIn", NULL, NULL);
+       if (not_show_in)
+               {
+               gboolean found = FALSE;
+               gint i;
+               for (i = 0; not_show_in[i]; i++)
+                       if (strcmp(not_show_in[i], "X-Geeqie") == 0)
+                               {
+                               found = TRUE;
+                               break;
+                               }
+               if (found) editor->ignored = TRUE;
+               g_strfreev(not_show_in);
+               }
+               
+               
+       try_exec = g_key_file_get_string(key_file, DESKTOP_GROUP, "TryExec", NULL);
+       if (try_exec && !editor->hidden && !editor->ignored)
+               {
+               gchar *try_exec_res = g_find_program_in_path(try_exec);
+               if (!try_exec_res) editor->hidden = TRUE;
+               g_free(try_exec_res);
+               g_free(try_exec);
+               }
+
+       if (editor->ignored)
+               {
+               /* ignored editors will be deleted, no need to parse the rest */
+               g_key_file_free(key_file);
+               return TRUE;
+               }
+       
+       editor->name = g_key_file_get_locale_string(key_file, DESKTOP_GROUP, "Name", NULL, NULL);
+       editor->icon = g_key_file_get_string(key_file, DESKTOP_GROUP, "Icon", NULL);
+       
+       /* Icon key can be either a full path (absolute with file name extension) or an icon name (without extension) */
+       if (editor->icon && !g_path_is_absolute(editor->icon))
+               {
+               gchar *ext = strrchr(editor->icon, '.');
+               
+               if (ext && strlen(ext) == 4 &&
+                   (!strcmp(ext, ".png") || !strcmp(ext, ".xpm") || !strcmp(ext, ".svg")))
+                       {
+                       log_printf(_("Desktop file '%s' should not include extension in Icon key: '%s'\n"),
+                                  editor->file, editor->icon);
+                       
+                       // drop extension
+                       *ext = '\0';
+                       }
+               }
+       if (editor->icon && !register_theme_icon_as_stock(editor->key, editor->icon))
+               {
+               g_free(editor->icon);
+               editor->icon = NULL;
+               }
+
+       editor->exec = g_key_file_get_string(key_file, DESKTOP_GROUP, "Exec", NULL);
+       
+       editor->menu_path = g_key_file_get_string(key_file, DESKTOP_GROUP, "X-Geeqie-Menu-Path", NULL);
+       if (!editor->menu_path) editor->menu_path = g_strdup("EditMenu/ExternalMenu");
+       
+       editor->hotkey = g_key_file_get_string(key_file, DESKTOP_GROUP, "X-Geeqie-Hotkey", NULL);
+
+       editor->comment = g_key_file_get_string(key_file, DESKTOP_GROUP, "Comment", NULL);
+
+       extensions = g_key_file_get_string(key_file, DESKTOP_GROUP, "X-Geeqie-File-Extensions", NULL);
+       if (extensions)
+               editor->ext_list = filter_to_list(extensions);
+       else
+               {
+               gchar **mime_types = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "MimeType", NULL, NULL);
+               if (mime_types)
+                       {
+                       editor->ext_list = editor_mime_types_to_extensions(mime_types);
+                       g_strfreev(mime_types);
+                       if (!editor->ext_list) editor->hidden = TRUE;
+                       }
+               }
+               
+       if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Keep-Fullscreen", NULL)) editor->flags |= EDITOR_KEEP_FS;
+       if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Verbose", NULL)) editor->flags |= EDITOR_VERBOSE;
+       if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Verbose-Multi", NULL)) editor->flags |= EDITOR_VERBOSE_MULTI;
+       if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Filter", NULL)) editor->flags |= EDITOR_DEST;
+       if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "Terminal", NULL)) editor->flags |= EDITOR_TERMINAL;
+       
+       editor->flags |= editor_command_parse(editor, NULL, FALSE, NULL);
+
+       if ((editor->flags & EDITOR_NO_PARAM) && !category_geeqie) editor->hidden = TRUE;
+
+       g_key_file_free(key_file);
+
+       if (editor->ignored) return TRUE;
+       
+       gtk_list_store_append(desktop_file_list, &iter);
+       gtk_list_store_set(desktop_file_list, &iter,
+                          DESKTOP_FILE_COLUMN_KEY, key,
+                          DESKTOP_FILE_COLUMN_NAME, editor->name,
+                          DESKTOP_FILE_COLUMN_HIDDEN, editor->hidden ? _("yes") : _("no"),
+                          DESKTOP_FILE_COLUMN_WRITABLE, access_file(path, W_OK),
+                          DESKTOP_FILE_COLUMN_PATH, path, -1);
+       
+       return TRUE;
+}
+
+static gboolean editor_remove_desktop_file_cb(gpointer key, gpointer value, gpointer user_data)
+{
+       EditorDescription *editor = value;
+       return editor->hidden || editor->ignored;
+}
+
+void editor_table_finish(void)
+{
+       g_hash_table_foreach_remove(editors, editor_remove_desktop_file_cb, NULL);
+       editors_finished = TRUE;
+}
+
+void editor_table_clear(void)
 {
+       if (desktop_file_list)
+               {
+               gtk_list_store_clear(desktop_file_list);
+               }
+       else
+               {
+               desktop_file_list = gtk_list_store_new(DESKTOP_FILE_COLUMN_COUNT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_STRING);
+               }
+       if (editors)
+               {
+               g_hash_table_destroy(editors);
+               }
+       editors = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify)editor_description_free);
+       editors_finished = FALSE;
+}
+
+static GList *editor_add_desktop_dir(GList *list, const gchar *path)
+{
+       DIR *dp;
+       struct dirent *dir;
+       gchar *pathl;
+
+       pathl = path_from_utf8(path);
+       dp = opendir(pathl);
+       g_free(pathl);
+       if (!dp)
+               {
+               /* dir not found */
+               return list;
+               }
+       while ((dir = readdir(dp)) != NULL)
+               {
+               gchar *namel = dir->d_name;
+               
+               if (g_str_has_suffix(namel, ".desktop"))
+                       {
+                       gchar *name = path_to_utf8(namel);
+                       gchar *dpath = g_build_filename(path, name, NULL);
+                       list = g_list_prepend(list, dpath);
+                       g_free(name);
+                       }
+               }
+       closedir(dp);
+       return list;
+}
+
+GList *editor_get_desktop_files(void)
+{
+       gchar *path;
+       gchar *xdg_data_dirs;
+       gchar *all_dirs;
+       gchar **split_dirs;
        gint i;
+       GList *list = NULL;
+       
+       xdg_data_dirs = getenv("XDG_DATA_DIRS");
+       if (xdg_data_dirs && xdg_data_dirs[0])
+               xdg_data_dirs = path_to_utf8(xdg_data_dirs);
+       else
+               xdg_data_dirs = g_strdup("/usr/share");
+       
+       all_dirs = g_strconcat(get_rc_dir(), ":", GQ_APP_DIR, ":", xdg_data_home_get(), ":", xdg_data_dirs, NULL);
+       
+       g_free(xdg_data_dirs);
+
+       split_dirs = g_strsplit(all_dirs, ":", 0);
+       
+       g_free(all_dirs);
 
-       for (i = 0; i < GQVIEW_EDITOR_SLOTS; i++)
+       for (i = 0; split_dirs[i]; i++);
+       for (--i; i >= 0; i--)
                {
-               g_free(editor_name[i]);
-               editor_name[i] = g_strdup(_(editor_slot_defaults[i * 2]));
-               g_free(editor_command[i]);
-               editor_command[i] = g_strdup(editor_slot_defaults[i * 2 + 1]);
+               path = g_build_filename(split_dirs[i], "applications", NULL);
+               list = editor_add_desktop_dir(list, path);
+               g_free(path);
                }
+               
+       g_strfreev(split_dirs);
+       return list;
+}
+
+static void editor_list_add_cb(gpointer key, gpointer value, gpointer data)
+{
+       GList **listp = data;
+       EditorDescription *editor = value;
+       
+       /* do not show the special commands in any list, they are called explicitly */
+       if (strcmp(editor->key, CMD_COPY) == 0 ||
+           strcmp(editor->key, CMD_MOVE) == 0 ||
+           strcmp(editor->key, CMD_RENAME) == 0 ||
+           strcmp(editor->key, CMD_DELETE) == 0 ||
+           strcmp(editor->key, CMD_FOLDER) == 0) return;
+
+       *listp = g_list_prepend(*listp, editor);
+}
+
+static gint editor_sort(gconstpointer a, gconstpointer b)
+{
+       const EditorDescription *ea = a;
+       const EditorDescription *eb = b;
+       gint ret;
+       
+       ret = strcmp(ea->menu_path, eb->menu_path);
+       if (ret != 0) return ret;
+       
+       return g_utf8_collate(ea->name, eb->name);
+}
+
+GList *editor_list_get(void)
+{
+       GList *editors_list = NULL;
+       
+       if (!editors_finished) return NULL;
+       
+       g_hash_table_foreach(editors, editor_list_add_cb, &editors_list);
+       editors_list = g_list_sort(editors_list, editor_sort);
+
+       return editors_list;
+}
+
+/* ------------------------------ */
+
+
+static void editor_verbose_data_free(EditorData *ed)
+{
+       if (!ed->vd) return;
+       g_free(ed->vd);
+       ed->vd = NULL;
+}
+
+static void editor_data_free(EditorData *ed)
+{
+       editor_verbose_data_free(ed);
+       g_free(ed->working_directory);
+       g_free(ed);
 }
 
 static void editor_verbose_window_close(GenericDialog *gd, gpointer data)
 {
-       EditorVerboseData *vd = data;
+       EditorData *ed = data;
 
        generic_dialog_close(gd);
-       g_free(vd->command_template);
-       g_free(vd);
+       editor_verbose_data_free(ed);
+       if (ed->pid == -1) editor_data_free(ed); /* the process has already terminated */
 }
 
 static void editor_verbose_window_stop(GenericDialog *gd, gpointer data)
 {
-       EditorVerboseData *vd = data;
-
-       filelist_free(vd->list);
-       vd->list = NULL;
-
-       vd->count = 0;
-       editor_verbose_window_progress(vd, _("stopping..."));
+       EditorData *ed = data;
+       ed->stopping = TRUE;
+       ed->count = 0;
+       editor_verbose_window_progress(ed, _("stopping..."));
 }
 
 static void editor_verbose_window_enable_close(EditorVerboseData *vd)
@@ -126,7 +516,7 @@ static void editor_verbose_window_enable_close(EditorVerboseData *vd)
        gtk_widget_set_sensitive(vd->button_close, TRUE);
 }
 
-static EditorVerboseData *editor_verbose_window(const gchar *template, const gchar *text)
+static EditorVerboseData *editor_verbose_window(EditorData *ed, const gchar *text)
 {
        EditorVerboseData *vd;
        GtkWidget *scrolled;
@@ -135,15 +525,9 @@ static EditorVerboseData *editor_verbose_window(const gchar *template, const gch
 
        vd = g_new0(EditorVerboseData, 1);
 
-       vd->list = NULL;
-       vd->command_template = g_strdup(template);
-       vd->total = 0;
-       vd->count = 0;
-       vd->fd = -1;
-
-       vd->gd = file_util_gen_dlg(_("Edit command results"), "GQview", "editor_results",
+       vd->gd = file_util_gen_dlg(_("Edit command results"), "editor_results",
                                   NULL, FALSE,
-                                  NULL, vd);
+                                  NULL, ed);
        buf = g_strdup_printf(_("Output of %s"), text);
        generic_dialog_add_message(vd->gd, NULL, buf, NULL);
        g_free(buf);
@@ -179,9 +563,10 @@ static EditorVerboseData *editor_verbose_window(const gchar *template, const gch
        vd->spinner = spinner_new(NULL, SPINNER_SPEED);
        gtk_box_pack_start(GTK_BOX(hbox), vd->spinner, FALSE, FALSE, 0);
        gtk_widget_show(vd->spinner);
-       
+
        gtk_widget_show(vd->gd->dialog);
 
+       ed->vd = vd;
        return vd;
 }
 
@@ -195,505 +580,794 @@ static void editor_verbose_window_fill(EditorVerboseData *vd, gchar *text, gint
        gtk_text_buffer_insert(buffer, &iter, text, len);
 }
 
-static void editor_verbose_window_progress(EditorVerboseData *vd, const gchar *text)
+static void editor_verbose_window_progress(EditorData *ed, const gchar *text)
 {
-       if (vd->total)
+       if (!ed->vd) return;
+
+       if (ed->total)
                {
-               gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(vd->progress), (double)vd->count / vd->total);
+               gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ed->vd->progress), (gdouble)ed->count / ed->total);
                }
 
-       gtk_progress_bar_set_text(GTK_PROGRESS_BAR(vd->progress), (text) ? text : "");
+       gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ed->vd->progress), (text) ? text : "");
 }
 
 static gboolean editor_verbose_io_cb(GIOChannel *source, GIOCondition condition, gpointer data)
 {
-       EditorVerboseData *vd = data;
+       EditorData *ed = data;
        gchar buf[512];
        gsize count;
 
-       switch (condition)
+       if (condition & G_IO_IN)
                {
-               case G_IO_IN:
-                       while (g_io_channel_read_chars(source, buf, sizeof(buf), &count, NULL) == G_IO_STATUS_NORMAL)
+               while (g_io_channel_read_chars(source, buf, sizeof(buf), &count, NULL) == G_IO_STATUS_NORMAL)
+                       {
+                       if (!g_utf8_validate(buf, count, NULL))
                                {
-                               if (!g_utf8_validate(buf, count, NULL))
+                               gchar *utf8;
+
+                               utf8 = g_locale_to_utf8(buf, count, NULL, NULL, NULL);
+                               if (utf8)
                                        {
-                                       gchar *utf8;
-                                       utf8 = g_locale_to_utf8(buf, count, NULL, NULL, NULL);
-                                       if (utf8)
-                                               {
-                                               editor_verbose_window_fill(vd, utf8, -1);
-                                               g_free(utf8);
-                                               }
-                                       else
-                                               {
-                                               editor_verbose_window_fill(vd, "GQview: Error converting text to valid utf8\n", -1);
-                                               }
+                                       editor_verbose_window_fill(ed->vd, utf8, -1);
+                                       g_free(utf8);
                                        }
                                else
                                        {
-                                       editor_verbose_window_fill(vd, buf, count);
+                                       editor_verbose_window_fill(ed->vd, "Error converting text to valid utf8\n", -1);
                                        }
                                }
-                       break;
-               case G_IO_ERR:
-                       printf("Error reading from command\n");
-               case G_IO_HUP:
-                       if (debug) printf("Editor command HUP\n");
-               default:
-                       while (g_source_remove_by_user_data(vd));
-                       close(vd->fd);
-                       vd->fd = -1;
-                       editor_command_next(vd);
-                       return FALSE;
-                       break;
+                       else
+                               {
+                               editor_verbose_window_fill(ed->vd, buf, count);
+                               }
+                       }
+               }
+
+       if (condition & (G_IO_ERR | G_IO_HUP))
+               {
+               g_io_channel_shutdown(source, TRUE, NULL);
+               return FALSE;
                }
 
        return TRUE;
 }
 
-static int command_pipe(char *command)
+typedef enum {
+       PATH_FILE,
+       PATH_FILE_URL,
+       PATH_DEST
+} PathType;
+
+
+static gchar *editor_command_path_parse(const FileData *fd, gboolean consider_sidecars, PathType type, const EditorDescription *editor)
 {
-       char *args[4];
-       int fpipe[2];
-       pid_t fpid;
+       GString *string;
+       gchar *pathl;
+       const gchar *p = NULL;
 
-       args[0] = COMMAND_SHELL;
-       args[1] = COMMAND_OPT;
-       args[2] = command;
-       args[3] = NULL;
+       DEBUG_2("editor_command_path_parse: %s %d %d %s", fd->path, consider_sidecars, type, editor->key);
 
-       if (pipe(fpipe) < 0)
-               {
-               printf("pipe setup failed: %s\n", strerror(errno));
-               return -1;
-               }
+       string = g_string_new("");
 
-       fpid = fork();
-       if (fpid < 0)
+       if (type == PATH_FILE || type == PATH_FILE_URL)
                {
-               /* fork failed */
-               printf("fork failed: %s\n", strerror(errno));
-               }
-       else if (fpid == 0)
-               {
-               /* child */
-               gchar *msg;
+               GList *work = editor->ext_list;
 
-               dup2(fpipe[1], 1);
-               dup2(fpipe[1], 2);
-               close(fpipe[0]);
+               if (!work)
+                       p = fd->path;
+               else
+                       {
+                       while (work)
+                               {
+                               GList *work2;
+                               gchar *ext = work->data;
+                               work = work->next;
 
-               execvp(args[0], args);
+                               if (strcmp(ext, "*") == 0 ||
+                                   g_ascii_strcasecmp(ext, fd->extension) == 0)
+                                       {
+                                       p = fd->path;
+                                       break;
+                                       }
 
-               msg = g_strdup_printf("Unable to exec command:\n%s\n\n%s\n", command, strerror(errno));
-               write(1, msg, strlen(msg));
+                               work2 = consider_sidecars ? fd->sidecar_files : NULL;
+                               while (work2)
+                                       {
+                                       FileData *sfd = work2->data;
+                                       work2 = work2->next;
 
-               _exit(1);
+                                       if (g_ascii_strcasecmp(ext, sfd->extension) == 0)
+                                               {
+                                               p = sfd->path;
+                                               break;
+                                               }
+                                       }
+                               if (p) break;
+                               }
+                       if (!p) return NULL;
+                       }
                }
-       else
+       else if (type == PATH_DEST)
                {
-               /* parent */
-               fcntl(fpipe[0], F_SETFL, O_NONBLOCK);
-               close(fpipe[1]);
-
-               return fpipe[0];
+               if (fd->change && fd->change->dest)
+                       p = fd->change->dest;
+               else
+                       p = "";
                }
 
-       return -1;
-}
+       g_assert(p);
+       string = g_string_append(string, p);
 
-static gint editor_verbose_start(EditorVerboseData *vd, gchar *command)
-       {
-       GIOChannel *channel;
-       int fd;
+       if (type == PATH_FILE_URL) g_string_prepend(string, "file://");
+       pathl = path_from_utf8(string->str);
+       g_string_free(string, TRUE);
 
-       fd = command_pipe(command);
-       if (fd < 0)
+       if (pathl && !pathl[0]) /* empty string case */
                {
-               gchar *buf;
-
-               buf = g_strdup_printf(_("Failed to run command:\n%s\n"), command);
-               editor_verbose_window_fill(vd, buf, strlen(buf));
-               g_free(buf);
-
-               return FALSE;
+               g_free(pathl);
+               pathl = NULL;
                }
-
-       vd->fd = fd;
-       channel = g_io_channel_unix_new(fd);
-
-       g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
-                           editor_verbose_io_cb, vd, NULL);
-       g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_ERR,
-                           editor_verbose_io_cb, vd, NULL);
-       g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_HUP,
-                           editor_verbose_io_cb, vd, NULL);
-       g_io_channel_unref(channel);
-
-       return TRUE;
+       
+       DEBUG_2("editor_command_path_parse: return %s", pathl);
+       return pathl;
 }
 
-typedef enum {
-       PATH_FILE,
-       PATH_TARGET
-} PathType;
-
-
-static gchar *editor_command_path_parse(const FileData *fd, PathType type)
+static GString *append_quoted(GString *str, const char *s, gboolean single_quotes, gboolean double_quotes)
 {
-       GString *string;
-       gchar *pathl;
-       const gchar *p;
-
-       string = g_string_new("");
+       const char *p;
        
-       if (type == PATH_FILE)
+       if (!single_quotes)
                {
-               p = fd->path;
+               if (!double_quotes)
+                       g_string_append_c(str, '\'');
+               else
+                       g_string_append(str, "\"'");
                }
-       else if (type == PATH_TARGET)
+
+       for (p = s; *p != '\0'; p++)
                {
-               if (fd->change && fd->change->dest)
-                       p = fd->change->dest;
+               if (*p == '\'')
+                       g_string_append(str, "'\\''");
                else
-                       p = "";
+                       g_string_append_c(str, *p);
                }
-       while (*p != '\0')
+       
+       if (!single_quotes)
                {
-               /* must escape \, ", `, and $ to avoid problems,
-                * we assume system shell supports bash-like escaping
-                */
-               if (strchr("\\\"`$", *p) != NULL)
-                       {
-                       string = g_string_append_c(string, '\\');
-                       }
-               string = g_string_append_c(string, *p);
-               p++;
+               if (!double_quotes)
+                       g_string_append_c(str, '\'');
+               else
+                       g_string_append(str, "'\"");
                }
 
-       pathl = path_from_utf8(string->str);
-       g_string_free(string, TRUE);
-
-       return pathl;
+       return str;
 }
 
-static gint editor_command_one(const gchar *template, const FileData *fd, EditorVerboseData *vd)
+
+EditorFlags editor_command_parse(const EditorDescription *editor, GList *list, gboolean consider_sidecars, gchar **output)
 {
+       EditorFlags flags = 0;
+       const gchar *p;
        GString *result = NULL;
-       gchar *pathl, *targetl;
-       gchar *found;
-       const gchar *ptr;
-       gchar path_buffer[512];
-       gchar *current_path;
-       gint path_change = FALSE;
-       gint ret;
+       gboolean escape = FALSE;
+       gboolean single_quotes = FALSE;
+       gboolean double_quotes = FALSE;
 
-       current_path = getcwd(path_buffer, sizeof(path_buffer));
+       DEBUG_2("editor_command_parse: %s %d %d", editor->key, consider_sidecars, !!output);
 
-       result = g_string_new("");
-       pathl = editor_command_path_parse(fd, PATH_FILE);
-       targetl = editor_command_path_parse(fd, PATH_TARGET);
+       if (output)
+               result = g_string_new("");
 
-       ptr = template;
-       while ( (found = strstr(ptr, "%")) )
+       if (editor->exec[0] == '\0')
                {
-               result = g_string_append_len(result, ptr, found - ptr);
-               ptr = found + 2;
-               switch (found[1])
-                       {
-                       case 'p':
-                               result = g_string_append_c(result, '"');
-                               result = g_string_append(result, pathl);
-                               result = g_string_append_c(result, '"');
-                               break;
-                       case 't':
-                               result = g_string_append_c(result, '"');
-                               result = g_string_append(result, targetl);
-                               result = g_string_append_c(result, '"');
-                               break;
-                       case '%':
-                               result = g_string_append_c(result, '%');
-                               break;
-                       default:
-                               break;
-                       }
+               flags |= EDITOR_ERROR_EMPTY;
+               goto err;
                }
-       result = g_string_append(result, ptr);
+       
+       p = editor->exec;
+       /* skip leading whitespaces if any */
+       while (g_ascii_isspace(*p)) p++;
 
-       if (debug) printf("system command: %s\n", result->str);
+       /* command */
 
-       if (current_path)
+       while (*p)
                {
-               gchar *base;
-               base = remove_level_from_path(fd->path);
-               if (chdir(base) == 0) path_change = TRUE;
-               g_free(base);
+               if (escape)
+                       {
+                       escape = FALSE;
+                       if (output) result = g_string_append_c(result, *p);
+                       }
+               else if (*p == '\\')
+                       {
+                       if (!single_quotes) escape = TRUE;
+                       if (output) result = g_string_append_c(result, *p);
+                       }
+               else if (*p == '\'')
+                       {
+                       if (output) result = g_string_append_c(result, *p);
+                       if (!single_quotes && !double_quotes)
+                               single_quotes = TRUE;
+                       else if (single_quotes)
+                               single_quotes = FALSE;
+                       }
+               else if (*p == '"')
+                       {
+                       if (output) result = g_string_append_c(result, *p);
+                       if (!single_quotes && !double_quotes)
+                               double_quotes = TRUE;
+                       else if (double_quotes)
+                               double_quotes = FALSE;
+                       }
+               else if (*p == '%' && p[1])
+                       {
+                       gchar *pathl = NULL;
+
+                       p++;
+
+                       switch (*p)
+                               {
+                               case 'f': /* single file */
+                               case 'u': /* single url */
+                                       flags |= EDITOR_FOR_EACH;
+                                       if (flags & EDITOR_SINGLE_COMMAND)
+                                               {
+                                               flags |= EDITOR_ERROR_INCOMPATIBLE;
+                                               goto err;
+                                               }
+                                       if (list)
+                                               {
+                                               /* use the first file from the list */
+                                               if (!list->data)
+                                                       {
+                                                       flags |= EDITOR_ERROR_NO_FILE;
+                                                       goto err;
+                                                       }
+                                               pathl = editor_command_path_parse((FileData *)list->data,
+                                                                                 consider_sidecars,
+                                                                                 (*p == 'f') ? PATH_FILE : PATH_FILE_URL,
+                                                                                 editor);
+                                               if (!output)
+                                                       {
+                                                       /* just testing, check also the rest of the list (like with F and U)
+                                                          any matching file is OK */
+                                                       GList *work = list->next;
+                                                       
+                                                       while (!pathl && work)
+                                                               {
+                                                               FileData *fd = work->data;
+                                                               pathl = editor_command_path_parse(fd,
+                                                                                                 consider_sidecars,
+                                                                                                 (*p == 'f') ? PATH_FILE : PATH_FILE_URL,
+                                                                                                 editor);
+                                                               work = work->next;
+                                                               }
+                                                       }
+                                                       
+                                               if (!pathl)
+                                                       {
+                                                       flags |= EDITOR_ERROR_NO_FILE;
+                                                       goto err;
+                                                       }
+                                               if (output)
+                                                       {
+                                                       result = append_quoted(result, pathl, single_quotes, double_quotes);
+                                                       }
+                                               g_free(pathl);
+                                               }
+                                       break;
+
+                               case 'F':
+                               case 'U':
+                                       flags |= EDITOR_SINGLE_COMMAND;
+                                       if (flags & (EDITOR_FOR_EACH | EDITOR_DEST))
+                                               {
+                                               flags |= EDITOR_ERROR_INCOMPATIBLE;
+                                               goto err;
+                                               }
+
+                                       if (list)
+                                               {
+                                               /* use whole list */
+                                               GList *work = list;
+                                               gboolean ok = FALSE;
+
+                                               while (work)
+                                                       {
+                                                       FileData *fd = work->data;
+                                                       pathl = editor_command_path_parse(fd, consider_sidecars, (*p == 'F') ? PATH_FILE : PATH_FILE_URL, editor);
+                                                       if (pathl)
+                                                               {
+                                                               ok = TRUE;
+
+                                                               if (output)
+                                                                       {
+                                                                       ok = TRUE;
+                                                                       if (work != list) g_string_append_c(result, ' ');
+                                                                       result = append_quoted(result, pathl, single_quotes, double_quotes);
+                                                                       }
+                                                               g_free(pathl);
+                                                               }
+                                                       work = work->next;
+                                                       }
+                                               if (!ok)
+                                                       {
+                                                       flags |= EDITOR_ERROR_NO_FILE;
+                                                       goto err;
+                                                       }
+                                               }
+                                       break;
+                               case 'i':
+                                       if (editor->icon && *editor->icon)
+                                               {
+                                               if (output)
+                                                       {
+                                                       result = g_string_append(result, "--icon ");
+                                                       result = append_quoted(result, editor->icon, single_quotes, double_quotes);
+                                                       }
+                                               }
+                                       break;
+                               case 'c':
+                                       if (output)
+                                               {
+                                               result = append_quoted(result, editor->name, single_quotes, double_quotes);
+                                               }
+                                       break;
+                               case 'k':
+                                       if (output)
+                                               {
+                                               result = append_quoted(result, editor->file, single_quotes, double_quotes);
+                                               }
+                                       break;
+                               case '%':
+                                       /* %% = % escaping */
+                                       if (output) result = g_string_append_c(result, *p);
+                                       break;
+                               case 'd':
+                               case 'D':
+                               case 'n':
+                               case 'N':
+                               case 'v':
+                               case 'm':
+                                       /* deprecated according to spec, ignore */
+                                       break;
+                               default:
+                                       flags |= EDITOR_ERROR_SYNTAX;
+                                       goto err;
+                               }
+                       }
+               else
+                       {
+                       if (output) result = g_string_append_c(result, *p);
+                       }
+               p++;
                }
 
-       if (vd)
+       if (!(flags & (EDITOR_FOR_EACH | EDITOR_SINGLE_COMMAND))) flags |= EDITOR_NO_PARAM;
+
+       if (output)
                {
-               result = g_string_append(result, " 2>&1");
-               ret = editor_verbose_start(vd, result->str);
+               *output = g_string_free(result, FALSE);
+               DEBUG_3("Editor cmd: %s", *output);
                }
-       else
+
+       return flags;
+
+
+err:
+       if (output)
                {
-               ret = !system(result->str);
+               g_string_free(result, TRUE);
+               *output = NULL;
                }
+       return flags;
+}
 
-       if (path_change) chdir(current_path);
 
-       g_string_free(result, TRUE);
-       g_free(pathl);
-       g_free(targetl);
+static void editor_child_exit_cb(GPid pid, gint status, gpointer data)
+{
+       EditorData *ed = data;
+       g_spawn_close_pid(pid);
+       ed->pid = -1;
 
-       return ret;
+       editor_command_next_finish(ed, status);
 }
 
-static gint editor_command_next(EditorVerboseData *vd)
+
+static EditorFlags editor_command_one(const EditorDescription *editor, GList *list, EditorData *ed)
 {
-       const gchar *text;
+       gchar *command;
+       FileData *fd = (ed->flags & EDITOR_NO_PARAM) ? NULL : list->data;;
+       GPid pid;
+       gint standard_output;
+       gint standard_error;
+       gboolean ok;
 
-       editor_verbose_window_fill(vd, "\n", 1);
+       ed->pid = -1;
+       ed->flags = editor->flags;
+       ed->flags |= editor_command_parse(editor, list, TRUE, &command);
 
-       while (vd->list)
-               {
-               FileData *fd;
-               gint success;
+       ok = !EDITOR_ERRORS(ed->flags);
 
-               fd = vd->list->data;
-               vd->list = g_list_remove(vd->list, fd);
+       if (ok)
+               {
+               ok = (options->shell.path && *options->shell.path);
+               if (!ok) log_printf("ERROR: empty shell command\n");
+                       
+               if (ok)
+                       {
+                       ok = (access(options->shell.path, X_OK) == 0);
+                       if (!ok) log_printf("ERROR: cannot execute shell command '%s'\n", options->shell.path);
+                       }
 
-               editor_verbose_window_progress(vd, fd->path);
+               if (!ok) ed->flags |= EDITOR_ERROR_CANT_EXEC;
+               }
 
-               vd->count++;
-               success = editor_command_one(vd->command_template, fd, vd);
-               if (success)
+       if (ok)
+               {
+               gchar *working_directory;
+               gchar *args[4];
+               guint n = 0;
+
+               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;
+               args[n++] = command;
+               args[n] = NULL;
+
+               if ((ed->flags & EDITOR_DEST) && fd->change && fd->change->dest) /* FIXME: error handling */
+                       {
+                       g_setenv("GEEQIE_DESTINATION", fd->change->dest, TRUE);
+                       }
+               else
                        {
-                       gtk_widget_set_sensitive(vd->button_stop, (vd->list != NULL) );
-                       editor_verbose_window_fill(vd, fd->path, strlen(fd->path));
-                       editor_verbose_window_fill(vd, "\n", 1);
+                       g_unsetenv("GEEQIE_DESTINATION");
                        }
 
-               file_data_unref(fd);
-               if (success) return TRUE;
+               ok = g_spawn_async_with_pipes(working_directory, args, NULL,
+                                     G_SPAWN_DO_NOT_REAP_CHILD, /* GSpawnFlags */
+                                     NULL, NULL,
+                                     &pid,
+                                     NULL,
+                                     ed->vd ? &standard_output : NULL,
+                                     ed->vd ? &standard_error : NULL,
+                                     NULL);
+               
+               g_free(working_directory);
+
+               if (!ok) ed->flags |= EDITOR_ERROR_CANT_EXEC;
                }
 
-       if (vd->count == vd->total)
+       if (ok)
                {
-               text = _("done");
+               g_child_watch_add(pid, editor_child_exit_cb, ed);
+               ed->pid = pid;
                }
-       else
+
+       if (ed->vd)
                {
-               text = _("stopped by user");
+               if (!ok)
+                       {
+                       gchar *buf;
+
+                       buf = g_strdup_printf(_("Failed to run command:\n%s\n"), editor->file);
+                       editor_verbose_window_fill(ed->vd, buf, strlen(buf));
+                       g_free(buf);
+
+                       }
+               else
+                       {
+                       GIOChannel *channel_output;
+                       GIOChannel *channel_error;
+
+                       channel_output = g_io_channel_unix_new(standard_output);
+                       g_io_channel_set_flags(channel_output, G_IO_FLAG_NONBLOCK, NULL);
+                       g_io_channel_set_encoding(channel_output, NULL, NULL);
+
+                       g_io_add_watch_full(channel_output, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP,
+                                           editor_verbose_io_cb, ed, NULL);
+                       g_io_channel_unref(channel_output);
+
+                       channel_error = g_io_channel_unix_new(standard_error);
+                       g_io_channel_set_flags(channel_error, G_IO_FLAG_NONBLOCK, NULL);
+                       g_io_channel_set_encoding(channel_error, NULL, NULL);
+
+                       g_io_add_watch_full(channel_error, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP,
+                                           editor_verbose_io_cb, ed, NULL);
+                       g_io_channel_unref(channel_error);
+                       }
                }
-       vd->count = 0;
-       editor_verbose_window_progress(vd, text);
-       editor_verbose_window_enable_close(vd);
-       return FALSE;
+
+       g_free(command);
+
+       return EDITOR_ERRORS(ed->flags);
 }
 
-static gint editor_command_start(const gchar *template, const gchar *text, GList *list)
+static EditorFlags editor_command_next_start(EditorData *ed)
 {
-       EditorVerboseData *vd;
+       if (ed->vd) editor_verbose_window_fill(ed->vd, "\n", 1);
 
-       vd = editor_verbose_window(template, text);
-       vd->list = filelist_copy(list);
-       vd->total = g_list_length(list);
+       if ((ed->list || (ed->flags & EDITOR_NO_PARAM)) && ed->count < ed->total)
+               {
+               FileData *fd;
+               EditorFlags error;
 
-       return editor_command_next(vd);
-}
+               fd = (ed->flags & EDITOR_NO_PARAM) ? NULL : ed->list->data;
 
-static gint editor_line_break(const gchar *template, gchar **front, const gchar **end)
-{
-       gchar *found;
+               if (ed->vd)
+                       {
+                       if ((ed->flags & EDITOR_FOR_EACH) && fd)
+                               editor_verbose_window_progress(ed, fd->path);
+                       else
+                               editor_verbose_window_progress(ed, _("running..."));
+                       }
+               ed->count++;
 
-       *front = g_strdup(template);
-       found = strstr(*front, "%f");
+               error = editor_command_one(ed->editor, ed->list, ed);
+               if (!error && ed->vd)
+                       {
+                       gtk_widget_set_sensitive(ed->vd->button_stop, (ed->list != NULL) );
+                       if ((ed->flags & EDITOR_FOR_EACH) && fd)
+                               {
+                               editor_verbose_window_fill(ed->vd, fd->path, strlen(fd->path));
+                               editor_verbose_window_fill(ed->vd, "\n", 1);
+                               }
+                       }
 
-       if (found)
-               {
-               *found = '\0';
-               *end = found + 2;
-               return TRUE;
+               if (!error)
+                       return 0;
+               
+               /* command was not started, call the finish immediately */
+               return editor_command_next_finish(ed, 0);
                }
 
-       *end = "";
-       return FALSE;
+       /* everything is done */
+       return editor_command_done(ed);
 }
 
-/*
- * The supported macros for editor commands:
- *
- *   %f   first occurence replaced by quoted sequence of filenames, command is run once.
- *        only one occurence of this macro is supported.
- *        ([ls %f] results in [ls "file1" "file2" ... "lastfile"])
- *   %p   command is run for each filename in turn, each instance replaced with single filename.
- *        multiple occurences of this macro is supported for complex shell commands.
- *        This macro will BLOCK THE APPLICATION until it completes, since command is run once
- *        for every file in syncronous order. To avoid blocking add the %v macro, below.
- *        ([ls %p] results in [ls "file1"], [ls "file2"] ... [ls "lastfile"])
- *   none if no macro is supplied, the result is equivalent to "command %f"
- *        ([ls] results in [ls "file1" "file2" ... "lastfile"])
- *
- *  Only one of the macros %f or %p may be used in a given commmand.
- *
- *   %v   must be the first two characters[1] in a command, causes a window to display
- *        showing the output of the command(s).
- *   %V   same as %v except in the case of %p only displays a window for multiple files,
- *        operating on a single file is suppresses the output dialog.
- *
- *   %w   must be first two characters in a command, presence will disable full screen
- *        from exiting upon invocation.
- *
- *
- * [1] Note: %v,%V may also be preceded by "%w".
- */
-static gint editor_command_run(const gchar *template, const gchar *text, GList *list)
+static EditorFlags editor_command_next_finish(EditorData *ed, gint status)
 {
-       gint verbose = FALSE;
-       gint for_each = FALSE;
-       gint ret = TRUE;
-
-       if (!template || template[0] == '\0') return;
+       gint cont = ed->stopping ? EDITOR_CB_SKIP : EDITOR_CB_CONTINUE;
 
-       for_each = (strstr(template, "%p") != NULL);
+       if (status)
+               ed->flags |= EDITOR_ERROR_STATUS;
 
-       /* no window state change flag, skip */
-       if (strncmp(template, "%w", 2) == 0) template += 2;
+       if (ed->flags & EDITOR_FOR_EACH)
+               {
+               /* handle the first element from the list */
+               GList *fd_element = ed->list;
 
-       if (strncmp(template, "%v", 2) == 0)
+               ed->list = g_list_remove_link(ed->list, fd_element);
+               if (ed->callback)
+                       {
+                       cont = ed->callback(ed->list ? ed : NULL, ed->flags, fd_element, ed->data);
+                       if (ed->stopping && cont == EDITOR_CB_CONTINUE) cont = EDITOR_CB_SKIP;
+                       }
+               filelist_free(fd_element);
+               }
+       else
                {
-               template += 2;
-               verbose = TRUE;
+               /* handle whole list */
+               if (ed->callback)
+                       cont = ed->callback(NULL, ed->flags, ed->list, ed->data);
+               filelist_free(ed->list);
+               ed->list = NULL;
                }
-       else if (strncmp(template, "%V", 2) == 0)
+
+       switch (cont)
                {
-               template += 2;
-               if (!for_each || list->next) verbose = TRUE;
+               case EDITOR_CB_SUSPEND:
+                       return EDITOR_ERRORS(ed->flags);
+               case EDITOR_CB_SKIP:
+                       return editor_command_done(ed);
                }
+       
+       return editor_command_next_start(ed);
+}
+
+static EditorFlags editor_command_done(EditorData *ed)
+{
+       EditorFlags flags;
 
-       if (for_each)
+       if (ed->vd)
                {
-               if (verbose)
+               if (ed->count == ed->total)
                        {
-                       editor_command_start(template, text, list);
+                       editor_verbose_window_progress(ed, _("done"));
                        }
                else
                        {
-                       GList *work;
-
-                       work = list;
-                       while (work)
-                               {
-                               FileData *fd = work->data;
-                               ret = editor_command_one(template, fd, NULL);
-                               work = work->next;
-                               }
+                       editor_verbose_window_progress(ed, _("stopped by user"));
                        }
+               editor_verbose_window_enable_close(ed->vd);
                }
-       else
+
+       /* free the not-handled items */
+       if (ed->list)
                {
-               gchar *front;
-               const gchar *end;
-               GList *work;
-               GString *result = NULL;
-               gint parser_match;
+               ed->flags |= EDITOR_ERROR_SKIPPED;
+               if (ed->callback) ed->callback(NULL, ed->flags, ed->list, ed->data);
+               filelist_free(ed->list);
+               ed->list = NULL;
+               }
 
-               parser_match = editor_line_break(template, &front, &end);
-               result = g_string_new((parser_match) ? "" : " ");
+       ed->count = 0;
 
-               work = list;
-               while (work)
-                       {
-                       FileData *fd = work->data;
-                       gchar *pathl;
-
-                       if (work != list) g_string_append_c(result, ' ');
-                       result = g_string_append_c(result, '"');
-                       pathl = editor_command_path_parse(fd, PATH_FILE);
-                       result = g_string_append(result, pathl);
-                       g_free(pathl);
-                       result = g_string_append_c(result, '"');
-                       work = work->next;
-                       }
+       flags = EDITOR_ERRORS(ed->flags);
 
-               result = g_string_prepend(result, front);
-               result = g_string_append(result, end);
-               if (verbose) result = g_string_append(result, " 2>&1 ");
-               result = g_string_append(result, "&");
+       if (!ed->vd) editor_data_free(ed);
 
-               if (debug) printf("system command: %s\n", result->str);
+       return flags;
+}
 
-               if (verbose)
-                       {
-                       EditorVerboseData *vd;
+void editor_resume(gpointer ed)
+{
+       editor_command_next_start(ed);
+}
 
-                       vd = editor_verbose_window(template, text);
-                       editor_verbose_window_progress(vd, _("running..."));
-                       ret = editor_verbose_start(vd, result->str);
-                       }
-               else
-                       {
-                       ret = !system(result->str);
-                       }
+void editor_skip(gpointer ed)
+{
+       editor_command_done(ed);
+}
 
-               g_free(front);
-               g_string_free(result, TRUE);
-               }
-       return ret;
+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;
+
+       if (EDITOR_ERRORS(flags)) return EDITOR_ERRORS(flags);
+
+       ed = g_new0(EditorData, 1);
+       ed->list = filelist_copy(list);
+       ed->flags = flags;
+       ed->editor = editor;
+       ed->total = (flags & (EDITOR_SINGLE_COMMAND | EDITOR_NO_PARAM)) ? 1 : g_list_length(list);
+       ed->callback = cb;
+       ed->data = data;
+       ed->working_directory = g_strdup(working_directory);
+
+       if ((flags & EDITOR_VERBOSE_MULTI) && list && list->next)
+               flags |= EDITOR_VERBOSE;
+
+       if (flags & EDITOR_VERBOSE)
+               editor_verbose_window(ed, text);
+
+       editor_command_next_start(ed);
+       /* errors from editor_command_next_start will be handled via callback */
+       return EDITOR_ERRORS(flags);
 }
 
-gint start_editor_from_filelist(gint n, GList *list)
+gboolean is_valid_editor_command(const gchar *key)
 {
-       gchar *command;
-       gint ret;
+       if (!key) return FALSE;
+       return g_hash_table_lookup(editors, key) != NULL;
+}
 
-       if (n < 0 || n >= GQVIEW_EDITOR_SLOTS || !list ||
-           !editor_command[n] ||
-           strlen(editor_command[n]) == 0) return FALSE;
+EditorFlags start_editor_from_filelist_full(const gchar *key, GList *list, const gchar *working_directory, EditorCallback cb, gpointer data)
+{
+       EditorFlags error;
+       EditorDescription *editor;
+       if (!key) return EDITOR_ERROR_EMPTY;
+       
+       editor = g_hash_table_lookup(editors, key);
 
-       command = g_locale_from_utf8(editor_command[n], -1, NULL, NULL, NULL);
-       ret = editor_command_run(command, editor_name[n], list);
-       g_free(command);
-       return ret;
+       if (!editor) return EDITOR_ERROR_EMPTY;
+       if (!list && !(editor->flags & EDITOR_NO_PARAM)) return EDITOR_ERROR_NO_FILE;
+
+       error = editor_command_parse(editor, list, TRUE, NULL);
+
+       if (EDITOR_ERRORS(error)) return error;
+
+       error |= editor_command_start(editor, editor->name, list, working_directory, cb, data);
+
+       if (EDITOR_ERRORS(error))
+               {
+               gchar *text = g_strdup_printf(_("%s\n\"%s\""), editor_get_error_str(error), editor->file);
+               
+               file_util_warning_dialog(_("Invalid editor command"), text, GTK_STOCK_DIALOG_ERROR, NULL);
+               g_free(text);
+               }
+
+       return EDITOR_ERRORS(error);
+}
+
+EditorFlags start_editor_from_filelist(const gchar *key, GList *list)
+{
+       return start_editor_from_filelist_full(key, list, NULL, NULL, NULL);
 }
 
-gint start_editor_from_file(gint n, FileData *fd)
+EditorFlags start_editor_from_file_full(const gchar *key, FileData *fd, EditorCallback cb, gpointer data)
 {
        GList *list;
-       gint ret;
+       EditorFlags error;
 
        if (!fd) return FALSE;
 
        list = g_list_append(NULL, fd);
-       ret = start_editor_from_filelist(n, list);
+       error = start_editor_from_filelist_full(key, list, NULL, cb, data);
        g_list_free(list);
-       return ret;
+       return error;
 }
 
-gint start_editor_from_pair(gint n, const gchar *source, const gchar *target)
+EditorFlags start_editor_from_file(const gchar *key, FileData *fd)
 {
-       GList *list;
-       gint ret;
+       return start_editor_from_file_full(key, fd, NULL, NULL);
+}
 
-       if (!source) return FALSE;
-       if (!target) return FALSE;
+EditorFlags start_editor(const gchar *key, const gchar *working_directory)
+{
+       return start_editor_from_filelist_full(key, NULL, working_directory, NULL, NULL);
+}
 
-       list = g_list_append(NULL, (gchar *)source);
-       list = g_list_append(list, (gchar *)target);
-       ret = start_editor_from_filelist(n, list);
-       g_list_free(list);
-       return ret;
+gboolean editor_window_flag_set(const gchar *key)
+{
+       EditorDescription *editor;
+       if (!key) return TRUE;
+       
+       editor = g_hash_table_lookup(editors, key);
+       if (!editor) return TRUE;
+
+       return !!(editor->flags & EDITOR_KEEP_FS);
+}
+
+gboolean editor_is_filter(const gchar *key)
+{
+       EditorDescription *editor;
+       if (!key) return TRUE;
+       
+       editor = g_hash_table_lookup(editors, key);
+       if (!editor) return TRUE;
+
+       return !!(editor->flags & EDITOR_DEST);
 }
 
-gint editor_window_flag_set(gint n)
+gboolean editor_no_param(const gchar *key)
 {
-       if (n < 0 || n >= GQVIEW_EDITOR_SLOTS ||
-           !editor_command[n] ||
-           strlen(editor_command[n]) == 0) return TRUE;
+       EditorDescription *editor;
+       if (!key) return FALSE;
+       
+       editor = g_hash_table_lookup(editors, key);
+       if (!editor) return FALSE;
 
-       return (strncmp(editor_command[n], "%w", 2) == 0);
+       return !!(editor->flags & EDITOR_NO_PARAM);
 }
 
+gboolean editor_blocks_file(const gchar *key)
+{
+       EditorDescription *editor;
+       if (!key) return FALSE;
+       
+       editor = g_hash_table_lookup(editors, key);
+       if (!editor) return FALSE;
+
+       /* Decide if the image file should be blocked during editor execution
+          Editors like gimp can be used long time after the original file was
+          saved, for editing unrelated files.
+          %f vs. %F seems to be a good heuristic to detect this kind of editors.
+       */
+          
+       return !(editor->flags & EDITOR_SINGLE_COMMAND);
+}
 
+const gchar *editor_get_error_str(EditorFlags flags)
+{
+       if (flags & EDITOR_ERROR_EMPTY) return _("Editor template is empty.");
+       if (flags & EDITOR_ERROR_SYNTAX) return _("Editor template has incorrect syntax.");
+       if (flags & EDITOR_ERROR_INCOMPATIBLE) return _("Editor template uses incompatible macros.");
+       if (flags & EDITOR_ERROR_NO_FILE) return _("Can't find matching file type.");
+       if (flags & EDITOR_ERROR_CANT_EXEC) return _("Can't execute external editor.");
+       if (flags & EDITOR_ERROR_STATUS) return _("External editor returned error status.");
+       if (flags & EDITOR_ERROR_SKIPPED) return _("File was skipped.");
+       return _("Unknown error.");
+}
+
+const gchar *editor_get_name(const gchar *key)
+{
+       EditorDescription *editor = g_hash_table_lookup(editors, key);
+
+       if (!editor) return NULL;
+
+       return editor->name;
+}
+/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */