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