4 * Copyright (C) 2008 The Geeqie Team
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!
18 #include "filefilter.h"
20 #include "ui_fileops.h"
21 #include "ui_spinner.h"
22 #include "ui_utildlg.h"
28 #define EDITOR_WINDOW_WIDTH 500
29 #define EDITOR_WINDOW_HEIGHT 300
33 typedef struct _EditorVerboseData EditorVerboseData;
34 struct _EditorVerboseData {
36 GtkWidget *button_close;
37 GtkWidget *button_stop;
43 typedef struct _EditorData EditorData;
51 EditorVerboseData *vd;
52 EditorCallback callback;
54 const EditorDescription *editor;
58 static void editor_verbose_window_progress(EditorData *ed, const gchar *text);
59 static gint editor_command_next_start(EditorData *ed);
60 static gint editor_command_next_finish(EditorData *ed, gint status);
61 static gint editor_command_done(EditorData *ed);
62 static gint editor_command_parse(const EditorDescription *editor, GList *list, gchar **output);
65 *-----------------------------------------------------------------------------
66 * external editor routines
67 *-----------------------------------------------------------------------------
70 GHashTable *editors = NULL;
72 #ifdef G_KEY_FILE_DESKTOP_GROUP
73 #define DESKTOP_GROUP G_KEY_FILE_DESKTOP_GROUP
75 #define DESKTOP_GROUP "Desktop Entry"
78 void editor_description_free(EditorDescription *editor)
85 g_free(editor->menu_path);
86 g_free(editor->hotkey);
87 string_list_free(editor->ext_list);
93 static GList *editor_mime_types_to_extensions(gchar **mime_types)
95 /* FIXME: this should be rewritten to use the shared mime database, as soon as we switch to gio */
97 static const gchar *conv_table[][2] = {
98 {"application/x-ufraw", "%raw"},
100 {"image/bmp", ".bmp"},
101 {"image/gif", ".gif"},
102 {"image/jpeg", ".jpeg;.jpg"},
103 {"image/jpg", ".jpg;.jpeg"},
104 {"image/pcx", ".pcx"},
105 {"image/png", ".png"},
106 {"image/svg", ".svg"},
107 {"image/svg+xml", ".svg"},
108 {"image/svg+xml-compressed", ".svg"},
109 {"image/tiff", ".tiff;.tif"},
110 {"image/x-bmp", ".bmp"},
111 {"image/x-canon-crw", ".crw"},
112 {"image/x-cr2", ".cr2"},
113 {"image/x-dcraw", "%raw"},
114 {"image/x-ico", ".ico"},
115 {"image/x-mrw", ".mrw"},
116 {"image/x-MS-bmp", ".bmp"},
117 {"image/x-nef", ".nef"},
118 {"image/x-orf", ".orf"},
119 {"image/x-pcx", ".pcx"},
120 {"image/xpm", ".xpm"},
121 {"image/x-png", ".png"},
122 {"image/x-portable-anymap", ".pam"},
123 {"image/x-portable-bitmap", ".pbm"},
124 {"image/x-portable-graymap", ".pgm"},
125 {"image/x-portable-pixmap", ".ppm"},
126 {"image/x-psd", ".psd"},
127 {"image/x-raf", ".raf"},
128 {"image/x-sgi", ".sgi"},
129 {"image/x-tga", ".tga"},
130 {"image/x-xbitmap", ".xbm"},
131 {"image/x-xcf", ".xcf"},
132 {"image/x-xpixmap", ".xpm"},
133 {"image/x-x3f", ".x3f"},
139 for (i = 0; mime_types[i]; i++)
140 for (j = 0; conv_table[j][0]; j++)
141 if (strcmp(mime_types[i], conv_table[j][0]) == 0)
142 list = g_list_concat(list, filter_to_list(conv_table[j][1]));
147 static gboolean editor_read_desktop_file(const gchar *path)
150 EditorDescription *editor;
152 const gchar *key = filename_from_path(path);
153 gchar **categories, **only_show_in, **not_show_in;
155 if (g_hash_table_lookup(editors, key)) return FALSE; /* the file found earlier wins */
157 key_file = g_key_file_new();
158 if (!g_key_file_load_from_file(key_file, path, 0, NULL))
160 g_key_file_free(key_file);
164 editor = g_new0(EditorDescription, 1);
166 editor->key = g_strdup(key);
167 editor->file = g_strdup(path);
169 g_hash_table_insert(editors, editor->key, editor);
171 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "Hidden", NULL)) editor->hidden = TRUE;
173 categories = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "Categories", NULL, NULL);
176 gboolean found = FALSE;
178 for (i = 0; categories[i]; i++)
179 /* IMHO "Graphics" is exactly the category that we are interested in, so this does not have to be configurable */
180 if (strcmp(categories[i], "Graphics") == 0 ||
181 strcmp(categories[i], "X-Geeqie") == 0)
186 if (!found) editor->hidden = TRUE;
187 g_strfreev(categories);
191 editor->hidden = TRUE;
194 only_show_in = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "OnlyShowIn", NULL, NULL);
197 gboolean found = FALSE;
199 for (i = 0; only_show_in[i]; i++)
200 if (strcmp(only_show_in[i], "X-Geeqie") == 0)
205 if (!found) editor->hidden = TRUE;
206 g_strfreev(only_show_in);
209 not_show_in = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "NotShowIn", NULL, NULL);
212 gboolean found = FALSE;
214 for (i = 0; not_show_in[i]; i++)
215 if (strcmp(not_show_in[i], "X-Geeqie") == 0)
220 if (found) editor->hidden = TRUE;
221 g_strfreev(not_show_in);
226 /* hidden editors will be deleted, no need to parse the rest */
227 g_key_file_free(key_file);
231 editor->name = g_key_file_get_locale_string(key_file, DESKTOP_GROUP, "Name", NULL, NULL);
232 editor->icon = g_key_file_get_string(key_file, DESKTOP_GROUP, "Icon", NULL);
234 editor->exec = g_key_file_get_string(key_file, DESKTOP_GROUP, "Exec", NULL);
236 /* we take only editors that accept parameters, FIXME: the test can be improved */
237 if (!strchr(editor->exec, '%')) editor->hidden = TRUE;
239 editor->menu_path = g_key_file_get_string(key_file, DESKTOP_GROUP, "X-Geeqie-Menu-Path", NULL);
240 if (!editor->menu_path) editor->menu_path = g_strdup("EditMenu/ExternalMenu");
242 editor->hotkey = g_key_file_get_string(key_file, DESKTOP_GROUP, "X-Geeqie-Hotkey", NULL);
244 extensions = g_key_file_get_string(key_file, DESKTOP_GROUP, "X-Geeqie-File-Extensions", NULL);
246 editor->ext_list = filter_to_list(extensions);
249 gchar **mime_types = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "MimeType", NULL, NULL);
252 editor->ext_list = editor_mime_types_to_extensions(mime_types);
253 g_strfreev(mime_types);
254 if (!editor->ext_list) editor->hidden = TRUE;
260 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Keep-Fullscreen", NULL)) editor->flags |= EDITOR_KEEP_FS;
261 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Verbose", NULL)) editor->flags |= EDITOR_VERBOSE;
262 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Verbose-Multi", NULL)) editor->flags |= EDITOR_VERBOSE_MULTI;
263 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Filter", NULL)) editor->flags |= EDITOR_DEST;
264 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "Terminal", NULL)) editor->flags |= EDITOR_TERMINAL;
267 editor->flags |= editor_command_parse(editor, NULL, NULL);
268 g_key_file_free(key_file);
273 static gboolean editor_remove_desktop_file_cb(gpointer key, gpointer value, gpointer user_data)
275 EditorDescription *editor = value;
276 return editor->hidden;
279 static void editor_read_desktop_dir(const gchar *path)
285 pathl = path_from_utf8(path);
293 while ((dir = readdir(dp)) != NULL)
295 gchar *namel = dir->d_name;
296 size_t len = strlen(namel);
298 if (len > 8 && strncasecmp(namel + len - 8, ".desktop", 8) == 0)
300 gchar *name = path_to_utf8(namel);
301 gchar *dpath = g_build_filename(path, name, NULL);
302 editor_read_desktop_file(dpath);
310 void editor_load_descriptions(void)
313 gchar *xdg_data_dirs;
320 editors = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify)editor_description_free);
323 xdg_data_dirs = getenv("XDG_DATA_DIRS");
324 if (xdg_data_dirs && xdg_data_dirs[0])
325 xdg_data_dirs = path_to_utf8(xdg_data_dirs);
327 xdg_data_dirs = g_strdup("/usr/share");
329 all_dirs = g_strconcat(get_rc_dir(), ":", GQ_APP_DIR, ":", xdg_data_dirs, NULL);
331 g_free(xdg_data_dirs);
333 split_dirs = g_strsplit(all_dirs, ":", 0);
337 for (i = 0; split_dirs[i]; i++)
339 path = g_build_filename(split_dirs[i], "applications", NULL);
340 editor_read_desktop_dir(path);
344 g_strfreev(split_dirs);
346 g_hash_table_foreach_remove(editors, editor_remove_desktop_file_cb, NULL);
349 static void editor_list_add_cb(gpointer key, gpointer value, gpointer data)
351 GList **listp = data;
352 EditorDescription *editor = value;
354 /* do not show the special commands in any list, they are called explicitelly */
355 if (strcmp(editor->key, CMD_COPY) == 0 ||
356 strcmp(editor->key, CMD_MOVE) == 0 ||
357 strcmp(editor->key, CMD_RENAME) == 0 ||
358 strcmp(editor->key, CMD_DELETE) == 0 ||
359 strcmp(editor->key, CMD_FOLDER) == 0) return;
361 *listp = g_list_prepend(*listp, editor);
364 GList *editor_list_get(void)
366 GList *editors_list = NULL;
367 g_hash_table_foreach(editors, editor_list_add_cb, &editors_list);
371 /* ------------------------------ */
374 static void editor_verbose_data_free(EditorData *ed)
381 static void editor_data_free(EditorData *ed)
383 editor_verbose_data_free(ed);
387 static void editor_verbose_window_close(GenericDialog *gd, gpointer data)
389 EditorData *ed = data;
391 generic_dialog_close(gd);
392 editor_verbose_data_free(ed);
393 if (ed->pid == -1) editor_data_free(ed); /* the process has already terminated */
396 static void editor_verbose_window_stop(GenericDialog *gd, gpointer data)
398 EditorData *ed = data;
401 editor_verbose_window_progress(ed, _("stopping..."));
404 static void editor_verbose_window_enable_close(EditorVerboseData *vd)
406 vd->gd->cancel_cb = editor_verbose_window_close;
408 spinner_set_interval(vd->spinner, -1);
409 gtk_widget_set_sensitive(vd->button_stop, FALSE);
410 gtk_widget_set_sensitive(vd->button_close, TRUE);
413 static EditorVerboseData *editor_verbose_window(EditorData *ed, const gchar *text)
415 EditorVerboseData *vd;
420 vd = g_new0(EditorVerboseData, 1);
422 vd->gd = file_util_gen_dlg(_("Edit command results"), "editor_results",
425 buf = g_strdup_printf(_("Output of %s"), text);
426 generic_dialog_add_message(vd->gd, NULL, buf, NULL);
428 vd->button_stop = generic_dialog_add_button(vd->gd, GTK_STOCK_STOP, NULL,
429 editor_verbose_window_stop, FALSE);
430 gtk_widget_set_sensitive(vd->button_stop, FALSE);
431 vd->button_close = generic_dialog_add_button(vd->gd, GTK_STOCK_CLOSE, NULL,
432 editor_verbose_window_close, TRUE);
433 gtk_widget_set_sensitive(vd->button_close, FALSE);
435 scrolled = gtk_scrolled_window_new(NULL, NULL);
436 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
437 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
438 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
439 gtk_box_pack_start(GTK_BOX(vd->gd->vbox), scrolled, TRUE, TRUE, 5);
440 gtk_widget_show(scrolled);
442 vd->text = gtk_text_view_new();
443 gtk_text_view_set_editable(GTK_TEXT_VIEW(vd->text), FALSE);
444 gtk_widget_set_size_request(vd->text, EDITOR_WINDOW_WIDTH, EDITOR_WINDOW_HEIGHT);
445 gtk_container_add(GTK_CONTAINER(scrolled), vd->text);
446 gtk_widget_show(vd->text);
448 hbox = gtk_hbox_new(FALSE, 0);
449 gtk_box_pack_start(GTK_BOX(vd->gd->vbox), hbox, FALSE, FALSE, 0);
450 gtk_widget_show(hbox);
452 vd->progress = gtk_progress_bar_new();
453 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(vd->progress), 0.0);
454 gtk_box_pack_start(GTK_BOX(hbox), vd->progress, TRUE, TRUE, 0);
455 gtk_widget_show(vd->progress);
457 vd->spinner = spinner_new(NULL, SPINNER_SPEED);
458 gtk_box_pack_start(GTK_BOX(hbox), vd->spinner, FALSE, FALSE, 0);
459 gtk_widget_show(vd->spinner);
461 gtk_widget_show(vd->gd->dialog);
467 static void editor_verbose_window_fill(EditorVerboseData *vd, gchar *text, gint len)
469 GtkTextBuffer *buffer;
472 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(vd->text));
473 gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
474 gtk_text_buffer_insert(buffer, &iter, text, len);
477 static void editor_verbose_window_progress(EditorData *ed, const gchar *text)
483 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ed->vd->progress), (gdouble)ed->count / ed->total);
486 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ed->vd->progress), (text) ? text : "");
489 static gboolean editor_verbose_io_cb(GIOChannel *source, GIOCondition condition, gpointer data)
491 EditorData *ed = data;
495 if (condition & G_IO_IN)
497 while (g_io_channel_read_chars(source, buf, sizeof(buf), &count, NULL) == G_IO_STATUS_NORMAL)
499 if (!g_utf8_validate(buf, count, NULL))
503 utf8 = g_locale_to_utf8(buf, count, NULL, NULL, NULL);
506 editor_verbose_window_fill(ed->vd, utf8, -1);
511 editor_verbose_window_fill(ed->vd, "Error converting text to valid utf8\n", -1);
516 editor_verbose_window_fill(ed->vd, buf, count);
521 if (condition & (G_IO_ERR | G_IO_HUP))
523 g_io_channel_shutdown(source, TRUE, NULL);
537 static gchar *editor_command_path_parse(const FileData *fd, PathType type, const EditorDescription *editor)
541 const gchar *p = NULL;
543 string = g_string_new("");
545 if (type == PATH_FILE || type == PATH_FILE_URL)
547 GList *work = editor->ext_list;
556 gchar *ext = work->data;
559 if (strcmp(ext, "*") == 0 ||
560 strcasecmp(ext, fd->extension) == 0)
566 work2 = fd->sidecar_files;
569 FileData *sfd = work2->data;
572 if (strcasecmp(ext, sfd->extension) == 0)
583 else if (type == PATH_DEST)
585 if (fd->change && fd->change->dest)
586 p = fd->change->dest;
593 /* must escape \, ", `, and $ to avoid problems,
594 * we assume system shell supports bash-like escaping
596 if (strchr("\\\"`$", *p) != NULL)
598 string = g_string_append_c(string, '\\');
600 string = g_string_append_c(string, *p);
604 if (type == PATH_FILE_URL) g_string_prepend(string, "file://");
605 pathl = path_from_utf8(string->str);
606 g_string_free(string, TRUE);
612 static gint editor_command_parse(const EditorDescription *editor, GList *list, gchar **output)
616 GString *result = NULL;
619 result = g_string_new("");
621 if (editor->exec[0] == '\0')
623 flags |= EDITOR_ERROR_EMPTY;
628 /* skip leading whitespaces if any */
629 while (g_ascii_isspace(*p)) p++;
637 if (output) result = g_string_append_c(result, *p);
647 case 'f': /* single file */
648 case 'u': /* single url */
649 flags |= EDITOR_FOR_EACH;
650 if (flags & EDITOR_SINGLE_COMMAND)
652 flags |= EDITOR_ERROR_INCOMPATIBLE;
657 /* use the first file from the list */
658 if (!list || !list->data)
660 flags |= EDITOR_ERROR_NO_FILE;
663 pathl = editor_command_path_parse((FileData *)list->data,
664 (*p == 'f') ? PATH_FILE : PATH_FILE_URL,
668 flags |= EDITOR_ERROR_NO_FILE;
671 result = g_string_append_c(result, '"');
672 result = g_string_append(result, pathl);
674 result = g_string_append_c(result, '"');
680 flags |= EDITOR_SINGLE_COMMAND;
681 if (flags & (EDITOR_FOR_EACH | EDITOR_DEST))
683 flags |= EDITOR_ERROR_INCOMPATIBLE;
695 FileData *fd = work->data;
696 pathl = editor_command_path_parse(fd, (*p == 'F') ? PATH_FILE : PATH_FILE_URL, editor);
701 if (work != list) g_string_append_c(result, ' ');
702 result = g_string_append_c(result, '"');
703 result = g_string_append(result, pathl);
705 result = g_string_append_c(result, '"');
711 flags |= EDITOR_ERROR_NO_FILE;
719 result = g_string_append(result, editor->icon);
725 result = g_string_append(result, editor->name);
731 result = g_string_append(result, editor->file);
735 /* %% = % escaping */
736 if (output) result = g_string_append_c(result, *p);
744 /* deprecated according to spec, ignore */
747 flags |= EDITOR_ERROR_SYNTAX;
754 if (output) *output = g_string_free(result, FALSE);
761 g_string_free(result, TRUE);
768 static void editor_child_exit_cb (GPid pid, gint status, gpointer data)
770 EditorData *ed = data;
771 g_spawn_close_pid(pid);
774 editor_command_next_finish(ed, status);
778 static gint editor_command_one(const EditorDescription *editor, GList *list, EditorData *ed)
781 FileData *fd = list->data;
783 gint standard_output;
788 ed->flags = editor->flags | editor_command_parse(editor, list, &command);
790 ok = !(ed->flags & EDITOR_ERROR_MASK);
794 ok = (options->shell.path && *options->shell.path);
795 if (!ok) log_printf("ERROR: empty shell command\n");
799 ok = (access(options->shell.path, X_OK) == 0);
800 if (!ok) log_printf("ERROR: cannot execute shell command '%s'\n", options->shell.path);
803 if (!ok) ed->flags |= EDITOR_ERROR_CANT_EXEC;
808 gchar *working_directory;
812 working_directory = remove_level_from_path(fd->path);
813 args[n++] = options->shell.path;
814 if (options->shell.options && *options->shell.options)
815 args[n++] = options->shell.options;
819 if ((ed->flags & EDITOR_DEST) && fd->change && fd->change->dest) /* FIXME: error handling */
821 setenv("GEEQIE_DESTINATION", fd->change->dest, TRUE);
825 unsetenv("GEEQIE_DESTINATION");
828 ok = g_spawn_async_with_pipes(working_directory, args, NULL,
829 G_SPAWN_DO_NOT_REAP_CHILD, /* GSpawnFlags */
833 ed->vd ? &standard_output : NULL,
834 ed->vd ? &standard_error : NULL,
837 g_free(working_directory);
839 if (!ok) ed->flags |= EDITOR_ERROR_CANT_EXEC;
844 g_child_watch_add(pid, editor_child_exit_cb, ed);
854 buf = g_strdup_printf(_("Failed to run command:\n%s\n"), editor->file);
855 editor_verbose_window_fill(ed->vd, buf, strlen(buf));
861 GIOChannel *channel_output;
862 GIOChannel *channel_error;
864 channel_output = g_io_channel_unix_new(standard_output);
865 g_io_channel_set_flags(channel_output, G_IO_FLAG_NONBLOCK, NULL);
866 g_io_channel_set_encoding(channel_output, NULL, NULL);
868 g_io_add_watch_full(channel_output, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP,
869 editor_verbose_io_cb, ed, NULL);
870 g_io_channel_unref(channel_output);
872 channel_error = g_io_channel_unix_new(standard_error);
873 g_io_channel_set_flags(channel_error, G_IO_FLAG_NONBLOCK, NULL);
874 g_io_channel_set_encoding(channel_error, NULL, NULL);
876 g_io_add_watch_full(channel_error, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP,
877 editor_verbose_io_cb, ed, NULL);
878 g_io_channel_unref(channel_error);
884 return ed->flags & EDITOR_ERROR_MASK;
887 static gint editor_command_next_start(EditorData *ed)
889 if (ed->vd) editor_verbose_window_fill(ed->vd, "\n", 1);
891 if (ed->list && ed->count < ed->total)
900 editor_verbose_window_progress(ed, (ed->flags & EDITOR_FOR_EACH) ? fd->path : _("running..."));
904 error = editor_command_one(ed->editor, ed->list, ed);
905 if (!error && ed->vd)
907 gtk_widget_set_sensitive(ed->vd->button_stop, (ed->list != NULL) );
908 if (ed->flags & EDITOR_FOR_EACH)
910 editor_verbose_window_fill(ed->vd, fd->path, strlen(fd->path));
911 editor_verbose_window_fill(ed->vd, "\n", 1);
918 /* command was not started, call the finish immediately */
919 return editor_command_next_finish(ed, 0);
922 /* everything is done */
923 return editor_command_done(ed);
926 static gint editor_command_next_finish(EditorData *ed, gint status)
928 gint cont = ed->stopping ? EDITOR_CB_SKIP : EDITOR_CB_CONTINUE;
931 ed->flags |= EDITOR_ERROR_STATUS;
933 if (ed->flags & EDITOR_FOR_EACH)
935 /* handle the first element from the list */
936 GList *fd_element = ed->list;
938 ed->list = g_list_remove_link(ed->list, fd_element);
941 cont = ed->callback(ed->list ? ed : NULL, ed->flags, fd_element, ed->data);
942 if (ed->stopping && cont == EDITOR_CB_CONTINUE) cont = EDITOR_CB_SKIP;
944 filelist_free(fd_element);
948 /* handle whole list */
950 cont = ed->callback(NULL, ed->flags, ed->list, ed->data);
951 filelist_free(ed->list);
955 if (cont == EDITOR_CB_SUSPEND)
956 return ed->flags & EDITOR_ERROR_MASK;
957 else if (cont == EDITOR_CB_SKIP)
958 return editor_command_done(ed);
960 return editor_command_next_start(ed);
963 static gint editor_command_done(EditorData *ed)
971 if (ed->count == ed->total)
977 text = _("stopped by user");
979 editor_verbose_window_progress(ed, text);
980 editor_verbose_window_enable_close(ed->vd);
983 /* free the not-handled items */
986 ed->flags |= EDITOR_ERROR_SKIPPED;
987 if (ed->callback) ed->callback(NULL, ed->flags, ed->list, ed->data);
988 filelist_free(ed->list);
994 flags = ed->flags & EDITOR_ERROR_MASK;
996 if (!ed->vd) editor_data_free(ed);
1001 void editor_resume(gpointer ed)
1003 editor_command_next_start(ed);
1006 void editor_skip(gpointer ed)
1008 editor_command_done(ed);
1011 static gint editor_command_start(const EditorDescription *editor, const gchar *text, GList *list, EditorCallback cb, gpointer data)
1014 gint flags = editor->flags;
1016 if (flags & EDITOR_ERROR_MASK) return flags & EDITOR_ERROR_MASK;
1018 ed = g_new0(EditorData, 1);
1019 ed->list = filelist_copy(list);
1021 ed->editor = editor;
1022 ed->total = (flags & EDITOR_SINGLE_COMMAND) ? 1 : g_list_length(list);
1024 ed->stopping = FALSE;
1028 if ((flags & EDITOR_VERBOSE_MULTI) && list && list->next)
1029 flags |= EDITOR_VERBOSE;
1031 if (flags & EDITOR_VERBOSE)
1032 editor_verbose_window(ed, text);
1034 editor_command_next_start(ed);
1035 /* errors from editor_command_next_start will be handled via callback */
1036 return flags & EDITOR_ERROR_MASK;
1039 gboolean is_valid_editor_command(const gchar *key)
1041 if (!key) return FALSE;
1042 return g_hash_table_lookup(editors, key) != NULL;
1045 gint start_editor_from_filelist_full(const gchar *key, GList *list, EditorCallback cb, gpointer data)
1048 EditorDescription *editor;
1049 if (!key) return FALSE;
1051 editor = g_hash_table_lookup(editors, key);
1053 if (!list) return FALSE;
1054 if (!editor) return FALSE;
1056 error = editor_command_start(editor, editor->name, list, cb, data);
1058 if (error & EDITOR_ERROR_MASK)
1060 gchar *text = g_strdup_printf(_("%s\n\"%s\""), editor_get_error_str(error), editor->file);
1062 file_util_warning_dialog(_("Invalid editor command"), text, GTK_STOCK_DIALOG_ERROR, NULL);
1069 gint start_editor_from_filelist(const gchar *key, GList *list)
1071 return start_editor_from_filelist_full(key, list, NULL, NULL);
1074 gint start_editor_from_file_full(const gchar *key, FileData *fd, EditorCallback cb, gpointer data)
1079 if (!fd) return FALSE;
1081 list = g_list_append(NULL, fd);
1082 error = start_editor_from_filelist_full(key, list, cb, data);
1087 gint start_editor_from_file(const gchar *key, FileData *fd)
1089 return start_editor_from_file_full(key, fd, NULL, NULL);
1092 gint editor_window_flag_set(const gchar *key)
1094 EditorDescription *editor;
1095 if (!key) return TRUE;
1097 editor = g_hash_table_lookup(editors, key);
1098 if (!editor) return TRUE;
1100 return (editor->flags & EDITOR_KEEP_FS);
1103 gint editor_is_filter(const gchar *key)
1105 EditorDescription *editor;
1106 if (!key) return TRUE;
1108 editor = g_hash_table_lookup(editors, key);
1109 if (!editor) return TRUE;
1111 return (editor->flags & EDITOR_DEST);
1114 const gchar *editor_get_error_str(gint flags)
1116 if (flags & EDITOR_ERROR_EMPTY) return _("Editor template is empty.");
1117 if (flags & EDITOR_ERROR_SYNTAX) return _("Editor template has incorrect syntax.");
1118 if (flags & EDITOR_ERROR_INCOMPATIBLE) return _("Editor template uses incompatible macros.");
1119 if (flags & EDITOR_ERROR_NO_FILE) return _("Can't find matching file type.");
1120 if (flags & EDITOR_ERROR_CANT_EXEC) return _("Can't execute external editor.");
1121 if (flags & EDITOR_ERROR_STATUS) return _("External editor returned error status.");
1122 if (flags & EDITOR_ERROR_SKIPPED) return _("File was skipped.");
1123 return _("Unknown error.");
1126 const gchar *editor_get_name(const gchar *key)
1128 EditorDescription *editor = g_hash_table_lookup(editors, key);
1130 if (!editor) return NULL;
1132 return editor->name;
1134 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */