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