split filelist.c to filefilter.c and filedata.c
[geeqie.git] / src / editors.c
1 /*
2  * Geeqie
3  * (C) 2006 John Ellis
4  * Copyright (C) 2008 The Geeqie Team
5  *
6  * Author: John Ellis
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13
14 #include "main.h"
15 #include "editors.h"
16
17 #include "utilops.h"
18 #include "ui_fileops.h"
19 #include "ui_spinner.h"
20 #include "ui_utildlg.h"
21
22 #include "filedata.h"
23 #include "filefilter.h"
24
25 #include <errno.h>
26
27
28 #define EDITOR_WINDOW_WIDTH 500
29 #define EDITOR_WINDOW_HEIGHT 300
30
31 #define COMMAND_SHELL "/bin/sh"
32 #define COMMAND_OPT  "-c"
33
34
35 typedef struct _EditorVerboseData EditorVerboseData;
36 struct _EditorVerboseData {
37         GenericDialog *gd;
38         GtkWidget *button_close;
39         GtkWidget *button_stop;
40         GtkWidget *text;
41         GtkWidget *progress;
42         GtkWidget *spinner;
43 };
44
45 typedef struct _EditorData EditorData;
46 struct _EditorData {
47         gint flags;
48         GPid pid;
49         gchar *command_template;
50         GList *list;
51         gint count;
52         gint total;
53         gboolean stopping;
54         EditorVerboseData *vd;
55         EditorCallback callback;
56         gpointer data;
57 };
58
59
60 static gchar *editor_slot_defaults[GQ_EDITOR_SLOTS * 2] = {
61         N_("The Gimp"), "gimp-remote %{.cr2;.crw;.nef;.raw;*}f",
62         N_("XV"), "xv %f",
63         N_("Xpaint"), "xpaint %f",
64         N_("UFraw"), "ufraw %{.cr2;.crw;.nef;.raw}p",
65         N_("Add XMP sidecar"), "%vFILE=%{.cr2;.crw;.nef;.raw}p;XMP=`echo \"$FILE\"|sed -e 's|\\.[^.]*$|.xmp|'`; exiftool -tagsfromfile \"$FILE\" \"$XMP\"",
66         NULL, NULL,
67         NULL, NULL,
68         NULL, NULL,
69         N_("Rotate jpeg clockwise"), "%vif jpegtran -rotate 90 -copy all -outfile %{.jpg;.jpeg}p_tmp %{.jpg;.jpeg}p; then mv %{.jpg;.jpeg}p_tmp %{.jpg;.jpeg}p;else rm %{.jpg;.jpeg}p_tmp;fi",
70         N_("Rotate jpeg counterclockwise"), "%vif jpegtran -rotate 270 -copy all -outfile %{.jpg;.jpeg}p_tmp %{.jpg;.jpeg}p; then mv %{.jpg;.jpeg}p_tmp %{.jpg;.jpeg}p;else rm %{.jpg;.jpeg}p_tmp;fi",
71         /* special slots */
72 #if 1
73         /* for testing */
74         N_("External Copy command"), "%vset -x;cp %p %d",
75         N_("External Move command"), "%vset -x;mv %p %d",
76         N_("External Rename command"), "%vset -x;mv %p %d",
77         N_("External Delete command"), NULL,
78         N_("External New Folder command"), NULL
79 #else
80         N_("External Copy command"), NULL,
81         N_("External Move command"), NULL,
82         N_("External Rename command"), NULL,
83         N_("External Delete command"), NULL,
84         N_("External New Folder command"), NULL
85 #endif
86 };
87
88 static void editor_verbose_window_progress(EditorData *ed, const gchar *text);
89 static gint editor_command_next_start(EditorData *ed);
90 static gint editor_command_next_finish(EditorData *ed, gint status);
91 static gint editor_command_done(EditorData *ed);
92
93 /*
94  *-----------------------------------------------------------------------------
95  * external editor routines
96  *-----------------------------------------------------------------------------
97  */
98
99 void editor_reset_defaults(void)
100 {
101         gint i;
102
103         for (i = 0; i < GQ_EDITOR_SLOTS; i++)
104                 {
105                 g_free(options->editor_name[i]);
106                 options->editor_name[i] = g_strdup(_(editor_slot_defaults[i * 2]));
107                 g_free(options->editor_command[i]);
108                 options->editor_command[i] = g_strdup(editor_slot_defaults[i * 2 + 1]);
109                 }
110 }
111
112 static void editor_verbose_data_free(EditorData *ed)
113 {
114         if (!ed->vd) return;
115         g_free(ed->vd);
116         ed->vd = NULL;
117 }
118
119 static void editor_data_free(EditorData *ed)
120 {
121         editor_verbose_data_free(ed);
122         g_free(ed->command_template);
123         g_free(ed);
124 }
125
126 static void editor_verbose_window_close(GenericDialog *gd, gpointer data)
127 {
128         EditorData *ed = data;
129
130         generic_dialog_close(gd);
131         editor_verbose_data_free(ed);
132         if (ed->pid == -1) editor_data_free(ed); /* the process has already terminated */
133 }
134
135 static void editor_verbose_window_stop(GenericDialog *gd, gpointer data)
136 {
137         EditorData *ed = data;
138         ed->stopping = TRUE;
139         ed->count = 0;
140         editor_verbose_window_progress(ed, _("stopping..."));
141 }
142
143 static void editor_verbose_window_enable_close(EditorVerboseData *vd)
144 {
145         vd->gd->cancel_cb = editor_verbose_window_close;
146
147         spinner_set_interval(vd->spinner, -1);
148         gtk_widget_set_sensitive(vd->button_stop, FALSE);
149         gtk_widget_set_sensitive(vd->button_close, TRUE);
150 }
151
152 static EditorVerboseData *editor_verbose_window(EditorData *ed, const gchar *text)
153 {
154         EditorVerboseData *vd;
155         GtkWidget *scrolled;
156         GtkWidget *hbox;
157         gchar *buf;
158
159         vd = g_new0(EditorVerboseData, 1);
160
161         vd->gd = file_util_gen_dlg(_("Edit command results"), GQ_WMCLASS, "editor_results",
162                                    NULL, FALSE,
163                                    NULL, ed);
164         buf = g_strdup_printf(_("Output of %s"), text);
165         generic_dialog_add_message(vd->gd, NULL, buf, NULL);
166         g_free(buf);
167         vd->button_stop = generic_dialog_add_button(vd->gd, GTK_STOCK_STOP, NULL,
168                                                    editor_verbose_window_stop, FALSE);
169         gtk_widget_set_sensitive(vd->button_stop, FALSE);
170         vd->button_close = generic_dialog_add_button(vd->gd, GTK_STOCK_CLOSE, NULL,
171                                                     editor_verbose_window_close, TRUE);
172         gtk_widget_set_sensitive(vd->button_close, FALSE);
173
174         scrolled = gtk_scrolled_window_new(NULL, NULL);
175         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
176         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
177                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
178         gtk_box_pack_start(GTK_BOX(vd->gd->vbox), scrolled, TRUE, TRUE, 5);
179         gtk_widget_show(scrolled);
180
181         vd->text = gtk_text_view_new();
182         gtk_text_view_set_editable(GTK_TEXT_VIEW(vd->text), FALSE);
183         gtk_widget_set_size_request(vd->text, EDITOR_WINDOW_WIDTH, EDITOR_WINDOW_HEIGHT);
184         gtk_container_add(GTK_CONTAINER(scrolled), vd->text);
185         gtk_widget_show(vd->text);
186
187         hbox = gtk_hbox_new(FALSE, 0);
188         gtk_box_pack_start(GTK_BOX(vd->gd->vbox), hbox, FALSE, FALSE, 0);
189         gtk_widget_show(hbox);
190
191         vd->progress = gtk_progress_bar_new();
192         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(vd->progress), 0.0);
193         gtk_box_pack_start(GTK_BOX(hbox), vd->progress, TRUE, TRUE, 0);
194         gtk_widget_show(vd->progress);
195
196         vd->spinner = spinner_new(NULL, SPINNER_SPEED);
197         gtk_box_pack_start(GTK_BOX(hbox), vd->spinner, FALSE, FALSE, 0);
198         gtk_widget_show(vd->spinner);
199
200         gtk_widget_show(vd->gd->dialog);
201
202         ed->vd = vd;
203         return vd;
204 }
205
206 static void editor_verbose_window_fill(EditorVerboseData *vd, gchar *text, gint len)
207 {
208         GtkTextBuffer *buffer;
209         GtkTextIter iter;
210
211         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(vd->text));
212         gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
213         gtk_text_buffer_insert(buffer, &iter, text, len);
214 }
215
216 static void editor_verbose_window_progress(EditorData *ed, const gchar *text)
217 {
218         if (!ed->vd) return;
219
220         if (ed->total)
221                 {
222                 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ed->vd->progress), (double)ed->count / ed->total);
223                 }
224
225         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ed->vd->progress), (text) ? text : "");
226 }
227
228 static gboolean editor_verbose_io_cb(GIOChannel *source, GIOCondition condition, gpointer data)
229 {
230         EditorData *ed = data;
231         gchar buf[512];
232         gsize count;
233
234         if (condition & G_IO_IN)
235                 {
236                 while (g_io_channel_read_chars(source, buf, sizeof(buf), &count, NULL) == G_IO_STATUS_NORMAL)
237                         {
238                         if (!g_utf8_validate(buf, count, NULL))
239                                 {
240                                 gchar *utf8;
241
242                                 utf8 = g_locale_to_utf8(buf, count, NULL, NULL, NULL);
243                                 if (utf8)
244                                         {
245                                         editor_verbose_window_fill(ed->vd, utf8, -1);
246                                         g_free(utf8);
247                                         }
248                                 else
249                                         {
250                                         editor_verbose_window_fill(ed->vd, "Error converting text to valid utf8\n", -1);
251                                         }
252                                 }
253                         else
254                                 {
255                                 editor_verbose_window_fill(ed->vd, buf, count);
256                                 }
257                         }
258                 }
259
260         if (condition & (G_IO_ERR | G_IO_HUP))
261                 {
262                 g_io_channel_shutdown(source, TRUE, NULL);
263                 return FALSE;
264                 }
265
266         return TRUE;
267 }
268
269 typedef enum {
270         PATH_FILE,
271         PATH_DEST
272 } PathType;
273
274
275 static gchar *editor_command_path_parse(const FileData *fd, PathType type, const gchar *extensions)
276 {
277         GString *string;
278         gchar *pathl;
279         const gchar *p = NULL;
280
281         string = g_string_new("");
282
283         if (type == PATH_FILE)
284                 {
285                 GList *ext_list = filter_to_list(extensions);
286                 GList *work = ext_list;
287
288                 if (!work)
289                         p = fd->path;
290                 else
291                         {
292                         while (work)
293                                 {
294                                 GList *work2;
295                                 gchar *ext = work->data;
296                                 work = work->next;
297
298                                 if (strcmp(ext, "*") == 0 ||
299                                     strcasecmp(ext, fd->extension) == 0)
300                                         {
301                                         p = fd->path;
302                                         break;
303                                         }
304
305                                 work2 = fd->sidecar_files;
306                                 while (work2)
307                                         {
308                                         FileData *sfd = work2->data;
309                                         work2 = work2->next;
310
311                                         if (strcasecmp(ext, sfd->extension) == 0)
312                                                 {
313                                                 p = sfd->path;
314                                                 break;
315                                                 }
316                                         }
317                                 if (p) break;
318                                 }
319                         string_list_free(ext_list);
320                         if (!p) return NULL;
321                         }
322                 }
323         else if (type == PATH_DEST)
324                 {
325                 if (fd->change && fd->change->dest)
326                         p = fd->change->dest;
327                 else
328                         p = "";
329                 }
330
331         while (*p != '\0')
332                 {
333                 /* must escape \, ", `, and $ to avoid problems,
334                  * we assume system shell supports bash-like escaping
335                  */
336                 if (strchr("\\\"`$", *p) != NULL)
337                         {
338                         string = g_string_append_c(string, '\\');
339                         }
340                 string = g_string_append_c(string, *p);
341                 p++;
342                 }
343
344         pathl = path_from_utf8(string->str);
345         g_string_free(string, TRUE);
346
347         return pathl;
348 }
349
350
351 /*
352  * The supported macros for editor commands:
353  *
354  *   %f   first occurence replaced by quoted sequence of filenames, command is run once.
355  *        only one occurence of this macro is supported.
356  *        ([ls %f] results in [ls "file1" "file2" ... "lastfile"])
357  *   %p   command is run for each filename in turn, each instance replaced with single filename.
358  *        multiple occurences of this macro is supported for complex shell commands.
359  *        This macro will BLOCK THE APPLICATION until it completes, since command is run once
360  *        for every file in syncronous order. To avoid blocking add the %v macro, below.
361  *        ([ls %p] results in [ls "file1"], [ls "file2"] ... [ls "lastfile"])
362  *   none if no macro is supplied, the result is equivalent to "command %f"
363  *        ([ls] results in [ls "file1" "file2" ... "lastfile"])
364  *
365  *  Only one of the macros %f or %p may be used in a given commmand.
366  *
367  *   %v   must be the first two characters[1] in a command, causes a window to display
368  *        showing the output of the command(s).
369  *   %V   same as %v except in the case of %p only displays a window for multiple files,
370  *        operating on a single file is suppresses the output dialog.
371  *
372  *   %w   must be first two characters in a command, presence will disable full screen
373  *        from exiting upon invocation.
374  *
375  *
376  * [1] Note: %v,%V may also be preceded by "%w".
377  */
378
379
380 gint editor_command_parse(const gchar *template, GList *list, gchar **output)
381 {
382         gint flags = 0;
383         const gchar *p = template;
384         GString *result = NULL;
385         gchar *extensions = NULL;
386
387         if (output)
388                 result = g_string_new("");
389
390         if (!template || template[0] == '\0')
391                 {
392                 flags |= EDITOR_ERROR_EMPTY;
393                 goto err;
394                 }
395
396
397         /* global flags */
398         while (*p == '%')
399                 {
400                 switch (*++p)
401                         {
402                         case 'w':
403                                 flags |= EDITOR_KEEP_FS;
404                                 p++;
405                                 break;
406                         case 'v':
407                                 flags |= EDITOR_VERBOSE;
408                                 p++;
409                                 break;
410                         case 'V':
411                                 flags |= EDITOR_VERBOSE_MULTI;
412                                 p++;
413                                 break;
414                         }
415                 }
416
417         /* command */
418
419         while (*p)
420                 {
421                 if (*p != '%')
422                         {
423                         if (output) result = g_string_append_c(result, *p);
424                         }
425                 else /* *p == '%' */
426                         {
427                         extensions = NULL;
428                         gchar *pathl = NULL;
429
430                         p++;
431
432                         /* for example "%f" or "%{.crw;.raw;.cr2}f" */
433                         if (*p == '{')
434                                 {
435                                 gchar *end;
436                                 
437                                 p++;
438                                 end = strchr(p, '}');
439                                 if (!end)
440                                         {
441                                         flags |= EDITOR_ERROR_SYNTAX;
442                                         goto err;
443                                         }
444
445                                 extensions = g_strndup(p, end - p);
446                                 p = end + 1;
447                                 }
448
449                         switch (*p)
450                                 {
451                                 case 'd':
452                                         flags |= EDITOR_DEST;
453                                 case 'p':
454                                         flags |= EDITOR_FOR_EACH;
455                                         if (flags & EDITOR_SINGLE_COMMAND)
456                                                 {
457                                                 flags |= EDITOR_ERROR_INCOMPATIBLE;
458                                                 goto err;
459                                                 }
460                                         if (output)
461                                                 {
462                                                 /* use the first file from the list */
463                                                 if (!list || !list->data)
464                                                         {
465                                                         flags |= EDITOR_ERROR_NO_FILE;
466                                                         goto err;
467                                                         }
468                                                 pathl = editor_command_path_parse((FileData *)list->data, (*p == 'd') ? PATH_DEST : PATH_FILE, extensions);
469                                                 if (!pathl)
470                                                         {
471                                                         flags |= EDITOR_ERROR_NO_FILE;
472                                                         goto err;
473                                                         }
474                                                 result = g_string_append_c(result, '"');
475                                                 result = g_string_append(result, pathl);
476                                                 g_free(pathl);
477                                                 result = g_string_append_c(result, '"');
478                                                 }
479                                         break;
480
481                                 case 'f':
482                                         flags |= EDITOR_SINGLE_COMMAND;
483                                         if (flags & (EDITOR_FOR_EACH | EDITOR_DEST))
484                                                 {
485                                                 flags |= EDITOR_ERROR_INCOMPATIBLE;
486                                                 goto err;
487                                                 }
488
489                                         if (output)
490                                                 {
491                                                 /* use whole list */
492                                                 GList *work = list;
493                                                 gboolean ok = FALSE;
494
495                                                 while (work)
496                                                         {
497                                                         FileData *fd = work->data;
498                                                         pathl = editor_command_path_parse(fd, PATH_FILE, extensions);
499
500                                                         if (pathl)
501                                                                 {
502                                                                 ok = TRUE;
503                                                                 if (work != list) g_string_append_c(result, ' ');
504                                                                 result = g_string_append_c(result, '"');
505                                                                 result = g_string_append(result, pathl);
506                                                                 g_free(pathl);
507                                                                 result = g_string_append_c(result, '"');
508                                                                 }
509                                                         work = work->next;
510                                                         }
511                                                 if (!ok)
512                                                         {
513                                                         flags |= EDITOR_ERROR_NO_FILE;
514                                                         goto err;
515                                                         }
516                                                 }
517                                         break;
518                                 default:
519                                         flags |= EDITOR_ERROR_SYNTAX;
520                                         goto err;
521                                 }
522                         if (extensions) g_free(extensions);
523                         extensions = NULL;
524                         }
525                 p++;
526                 }
527
528         if (output) *output = g_string_free(result, FALSE);
529         return flags;
530
531
532 err:
533         if (output)
534                 {
535                 g_string_free(result, TRUE);
536                 *output = NULL;
537                 }
538         if (extensions) g_free(extensions);
539         return flags;
540 }
541
542 static void editor_child_exit_cb (GPid pid, gint status, gpointer data)
543 {
544         EditorData *ed = data;
545         g_spawn_close_pid(pid);
546         ed->pid = -1;
547
548         editor_command_next_finish(ed, status);
549 }
550
551
552 static gint editor_command_one(const gchar *template, GList *list, EditorData *ed)
553 {
554         gchar *command;
555         FileData *fd = list->data;
556         GPid pid;
557         gint standard_output;
558         gint standard_error;
559         gboolean ok;
560
561         ed->pid = -1;
562         ed->flags = editor_command_parse(template, list, &command);
563
564         ok = !(ed->flags & EDITOR_ERROR_MASK);
565
566         if (ok)
567                 {
568                 gchar *working_directory;
569                 gchar *args[4];
570
571                 working_directory = remove_level_from_path(fd->path);
572                 args[0] = COMMAND_SHELL;
573                 args[1] = COMMAND_OPT;
574                 args[2] = command;
575                 args[3] = NULL;
576
577                 ok = g_spawn_async_with_pipes(working_directory, args, NULL,
578                                       G_SPAWN_DO_NOT_REAP_CHILD, /* GSpawnFlags */
579                                       NULL, NULL,
580                                       &pid,
581                                       NULL,
582                                       ed->vd ? &standard_output : NULL,
583                                       ed->vd ? &standard_error : NULL,
584                                       NULL);
585                 
586                 g_free(working_directory);
587
588                 if (!ok) ed->flags |= EDITOR_ERROR_CANT_EXEC;
589                 }
590
591         if (ok)
592                 {
593                 g_child_watch_add(pid, editor_child_exit_cb, ed);
594                 ed->pid = pid;
595                 }
596
597         if (ed->vd)
598                 {
599                 if (!ok)
600                         {
601                         gchar *buf;
602
603                         buf = g_strdup_printf(_("Failed to run command:\n%s\n"), template);
604                         editor_verbose_window_fill(ed->vd, buf, strlen(buf));
605                         g_free(buf);
606
607                         }
608                 else
609                         {
610                         GIOChannel *channel_output;
611                         GIOChannel *channel_error;
612
613                         channel_output = g_io_channel_unix_new(standard_output);
614                         g_io_channel_set_flags(channel_output, G_IO_FLAG_NONBLOCK, NULL);
615
616                         g_io_add_watch_full(channel_output, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP,
617                                             editor_verbose_io_cb, ed, NULL);
618                         g_io_channel_unref(channel_output);
619
620                         channel_error = g_io_channel_unix_new(standard_error);
621                         g_io_channel_set_flags(channel_error, G_IO_FLAG_NONBLOCK, NULL);
622
623                         g_io_add_watch_full(channel_error, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP,
624                                             editor_verbose_io_cb, ed, NULL);
625                         g_io_channel_unref(channel_error);
626                         }
627                 }
628
629         g_free(command);
630
631         return ed->flags & EDITOR_ERROR_MASK;
632 }
633
634 static gint editor_command_next_start(EditorData *ed)
635 {
636         if (ed->vd) editor_verbose_window_fill(ed->vd, "\n", 1);
637
638         if (ed->list && ed->count < ed->total)
639                 {
640                 FileData *fd;
641                 gint error;
642
643                 fd = ed->list->data;
644
645                 if (ed->vd)
646                         {
647                         editor_verbose_window_progress(ed, (ed->flags & EDITOR_FOR_EACH) ? fd->path : _("running..."));
648                         }
649                 ed->count++;
650
651                 error = editor_command_one(ed->command_template, ed->list, ed);
652                 if (!error && ed->vd)
653                         {
654                         gtk_widget_set_sensitive(ed->vd->button_stop, (ed->list != NULL) );
655                         if (ed->flags & EDITOR_FOR_EACH)
656                                 {
657                                 editor_verbose_window_fill(ed->vd, fd->path, strlen(fd->path));
658                                 editor_verbose_window_fill(ed->vd, "\n", 1);
659                                 }
660                         }
661
662                 if (!error)
663                         return 0;
664                 else
665                         /* command was not started, call the finish immediately */
666                         return editor_command_next_finish(ed, 0);
667                 }
668
669         /* everything is done */
670         return editor_command_done(ed);
671 }
672
673 static gint editor_command_next_finish(EditorData *ed, gint status)
674 {
675         gint cont = ed->stopping ? EDITOR_CB_SKIP : EDITOR_CB_CONTINUE;
676
677         if (status)
678                 ed->flags |= EDITOR_ERROR_STATUS;
679
680         if (ed->flags & EDITOR_FOR_EACH)
681                 {
682                 /* handle the first element from the list */
683                 GList *fd_element = ed->list;
684
685                 ed->list = g_list_remove_link(ed->list, fd_element);
686                 if (ed->callback)
687                         cont = ed->callback(ed->list ? ed : NULL, ed->flags, fd_element, ed->data);
688                 filelist_free(fd_element);
689                 }
690         else
691                 {
692                 /* handle whole list */
693                 if (ed->callback)
694                         cont = ed->callback(NULL, ed->flags, ed->list, ed->data);
695                 filelist_free(ed->list);
696                 ed->list = NULL;
697                 }
698
699         if (cont == EDITOR_CB_SUSPEND)
700                 return ed->flags & EDITOR_ERROR_MASK;
701         else if (cont == EDITOR_CB_SKIP)
702                 return editor_command_done(ed);
703         else
704                 return editor_command_next_start(ed);
705 }
706
707 static gint editor_command_done(EditorData *ed)
708 {
709         gint flags;
710
711         if (ed->vd)
712                 {
713                 const gchar *text;
714
715                 if (ed->count == ed->total)
716                         {
717                         text = _("done");
718                         }
719                 else
720                         {
721                         text = _("stopped by user");
722                         }
723                 editor_verbose_window_progress(ed, text);
724                 editor_verbose_window_enable_close(ed->vd);
725                 }
726
727         /* free the not-handled items */
728         if (ed->list)
729                 {
730                 ed->flags |= EDITOR_ERROR_SKIPPED;
731                 if (ed->callback) ed->callback(NULL, ed->flags, ed->list, ed->data);
732                 filelist_free(ed->list);
733                 ed->list = NULL;
734                 }
735
736         ed->count = 0;
737
738         flags = ed->flags & EDITOR_ERROR_MASK;
739
740         if (!ed->vd) editor_data_free(ed);
741
742         return flags;
743 }
744
745 void editor_resume(gpointer ed)
746 {
747         editor_command_next_start(ed);
748 }
749
750 void editor_skip(gpointer ed)
751 {
752         editor_command_done(ed);
753 }
754
755 static gint editor_command_start(const gchar *template, const gchar *text, GList *list, EditorCallback cb, gpointer data)
756 {
757         EditorData *ed;
758         gint flags = editor_command_parse(template, NULL, NULL);
759
760         if (flags & EDITOR_ERROR_MASK) return flags & EDITOR_ERROR_MASK;
761
762         ed = g_new0(EditorData, 1);
763         ed->list = filelist_copy(list);
764         ed->flags = flags;
765         ed->command_template = g_strdup(template);
766         ed->total = (flags & EDITOR_SINGLE_COMMAND) ? 1 : g_list_length(list);
767         ed->count = 0;
768         ed->stopping = FALSE;
769         ed->callback = cb;
770         ed->data =  data;
771
772         if ((flags & EDITOR_VERBOSE_MULTI) && list && list->next)
773                 flags |= EDITOR_VERBOSE;
774
775         if (flags & EDITOR_VERBOSE)
776                 editor_verbose_window(ed, text);
777
778         editor_command_next_start(ed);
779         /* errors from editor_command_next_start will be handled via callback */
780         return flags & EDITOR_ERROR_MASK;
781 }
782
783 static gint is_valid_editor_command(gint n)
784 {
785         return (n >= 0 && n < GQ_EDITOR_SLOTS
786                 && options->editor_command[n]
787                 && strlen(options->editor_command[n]) > 0); 
788 }
789
790 gint start_editor_from_filelist_full(gint n, GList *list, EditorCallback cb, gpointer data)
791 {
792         gchar *command;
793         gint error;
794
795         if (!list) return FALSE;
796         if (!is_valid_editor_command(n)) return FALSE;
797
798         command = g_locale_from_utf8(options->editor_command[n], -1, NULL, NULL, NULL);
799         error = editor_command_start(command, options->editor_name[n], list, cb, data);
800         g_free(command);
801         return error;
802 }
803
804 gint start_editor_from_filelist(gint n, GList *list)
805 {
806         return start_editor_from_filelist_full(n, list,  NULL, NULL);
807 }
808
809 gint start_editor_from_file_full(gint n, FileData *fd, EditorCallback cb, gpointer data)
810 {
811         GList *list;
812         gint error;
813
814         if (!fd) return FALSE;
815
816         list = g_list_append(NULL, fd);
817         error = start_editor_from_filelist_full(n, list, cb, data);
818         g_list_free(list);
819         return error;
820 }
821
822 gint start_editor_from_file(gint n, FileData *fd)
823 {
824         return start_editor_from_file_full(n, fd, NULL, NULL);
825 }
826
827 gint editor_window_flag_set(gint n)
828 {
829         if (!is_valid_editor_command(n)) return TRUE;
830
831         return (editor_command_parse(options->editor_command[n], NULL, NULL) & EDITOR_KEEP_FS);
832 }
833
834 const gchar *editor_get_error_str(gint flags)
835 {
836         if (flags & EDITOR_ERROR_EMPTY) return _("Editor template is empty.");
837         if (flags & EDITOR_ERROR_SYNTAX) return _("Editor template has incorrect syntax.");
838         if (flags & EDITOR_ERROR_INCOMPATIBLE) return _("Editor template uses incompatible macros.");
839         if (flags & EDITOR_ERROR_NO_FILE) return _("Can't find matching file type.");
840         if (flags & EDITOR_ERROR_CANT_EXEC) return _("Can't execute external editor.");
841         if (flags & EDITOR_ERROR_STATUS) return _("External editor returned error status.");
842         if (flags & EDITOR_ERROR_SKIPPED) return _("File was skipped.");
843         return _("Unknown error.");
844 }