Adding first version of Hindi translation
[geany-mirror.git] / src / tools.c
blob4e17ae4a0b6e388310741994621dbe4ea365cf3a
1 /*
2 * tools.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * Miscellaneous code for the built-in Tools menu items, and custom command code.
24 * For Plugins code see plugins.c.
27 #include "geany.h"
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <errno.h>
34 #ifdef G_OS_UNIX
35 # include <sys/types.h>
36 # include <sys/wait.h>
37 # include <signal.h>
38 #endif
40 #include "tools.h"
41 #include "support.h"
42 #include "document.h"
43 #include "editor.h"
44 #include "sciwrappers.h"
45 #include "utils.h"
46 #include "ui_utils.h"
47 #include "msgwindow.h"
48 #include "keybindings.h"
49 #include "templates.h"
50 #include "win32.h"
51 #include "dialogs.h"
54 enum
56 CC_COLUMN_ID,
57 CC_COLUMN_STATUS,
58 CC_COLUMN_TOOLTIP,
59 CC_COLUMN_CMD,
60 CC_COLUMN_LABEL,
61 CC_COLUMN_COUNT
64 /* custom commands code*/
65 struct cc_dialog
67 guint count;
68 GtkWidget *view;
69 GtkTreeViewColumn *edit_column;
70 GtkListStore *store;
71 GtkTreeSelection *selection;
72 GtkWidget *button_add;
73 GtkWidget *button_remove;
74 GtkWidget *button_up;
75 GtkWidget *button_down;
78 static gboolean cc_error_occurred = FALSE;
79 static gboolean cc_reading_finished = FALSE;
80 static GString *cc_buffer;
83 static gboolean cc_exists_command(const gchar *command)
85 gchar *path = g_find_program_in_path(command);
87 g_free(path);
89 return path != NULL;
93 /* update STATUS and TOOLTIP columns according to cmd */
94 static void cc_dialog_update_row_status(GtkListStore *store, GtkTreeIter *iter, const gchar *cmd)
96 GError *err = NULL;
97 const gchar *stock_id = GTK_STOCK_NO;
98 gchar *tooltip = NULL;
99 gint argc;
100 gchar **argv;
102 if (! NZV(cmd))
103 stock_id = GTK_STOCK_YES;
104 else if (g_shell_parse_argv(cmd, &argc, &argv, &err))
106 if (argc > 0 && cc_exists_command(argv[0]))
107 stock_id = GTK_STOCK_YES;
108 else
109 tooltip = g_strdup_printf(_("Invalid command: %s"), _("Command not found"));
110 g_strfreev(argv);
112 else
114 tooltip = g_strdup_printf(_("Invalid command: %s"), err->message);
115 g_error_free(err);
118 gtk_list_store_set(store, iter, CC_COLUMN_STATUS, stock_id, CC_COLUMN_TOOLTIP, tooltip, -1);
119 g_free(tooltip);
123 /* adds a new row for custom command @p idx, or an new empty one if < 0 */
124 static void cc_dialog_add_command(struct cc_dialog *cc, gint idx, gboolean start_editing)
126 GtkTreeIter iter;
127 const gchar *cmd = NULL;
128 const gchar *label = NULL;
129 guint id = cc->count;
131 if (idx >= 0)
133 cmd = ui_prefs.custom_commands[idx];
134 label = ui_prefs.custom_commands_labels[idx];
137 cc->count++;
138 gtk_list_store_append(cc->store, &iter);
139 gtk_list_store_set(cc->store, &iter, CC_COLUMN_ID, id, CC_COLUMN_CMD, cmd, CC_COLUMN_LABEL, label, -1);
140 cc_dialog_update_row_status(cc->store, &iter, cmd);
142 if (start_editing)
144 GtkTreePath *path;
146 gtk_widget_grab_focus(cc->view);
147 path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc->store), &iter);
148 gtk_tree_view_set_cursor(GTK_TREE_VIEW(cc->view), path, cc->edit_column, TRUE);
149 gtk_tree_path_free(path);
154 static void cc_on_dialog_add_clicked(GtkButton *button, struct cc_dialog *cc)
156 cc_dialog_add_command(cc, -1, TRUE);
160 static void cc_on_dialog_remove_clicked(GtkButton *button, struct cc_dialog *cc)
162 GtkTreeIter iter;
164 if (gtk_tree_selection_get_selected(cc->selection, NULL, &iter))
165 gtk_list_store_remove(cc->store, &iter);
169 static void cc_on_dialog_move_up_clicked(GtkButton *button, struct cc_dialog *cc)
171 GtkTreeIter iter;
173 if (gtk_tree_selection_get_selected(cc->selection, NULL, &iter))
175 GtkTreePath *path;
176 GtkTreeIter prev;
178 path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc->store), &iter);
179 if (gtk_tree_path_prev(path) &&
180 gtk_tree_model_get_iter(GTK_TREE_MODEL(cc->store), &prev, path))
182 gtk_list_store_move_before(cc->store, &iter, &prev);
184 gtk_tree_path_free(path);
189 static void cc_on_dialog_move_down_clicked(GtkButton *button, struct cc_dialog *cc)
191 GtkTreeIter iter;
193 if (gtk_tree_selection_get_selected(cc->selection, NULL, &iter))
195 GtkTreeIter next = iter;
197 if (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc->store), &next))
198 gtk_list_store_move_after(cc->store, &iter, &next);
203 static gboolean cc_iofunc(GIOChannel *ioc, GIOCondition cond, gpointer data)
205 if (cond & (G_IO_IN | G_IO_PRI))
207 gchar *msg = NULL;
208 GIOStatus rv;
209 GError *err = NULL;
211 cc_buffer = g_string_sized_new(256);
215 rv = g_io_channel_read_line(ioc, &msg, NULL, NULL, &err);
216 if (msg != NULL)
218 g_string_append(cc_buffer, msg);
219 g_free(msg);
221 if (G_UNLIKELY(err != NULL))
223 geany_debug("%s: %s", G_STRFUNC, err->message);
224 g_error_free(err);
225 err = NULL;
227 } while (rv == G_IO_STATUS_NORMAL || rv == G_IO_STATUS_AGAIN);
229 if (G_UNLIKELY(rv != G_IO_STATUS_EOF))
230 { /* Something went wrong? */
231 g_warning("%s: %s\n", G_STRFUNC, "Incomplete command output");
234 return FALSE;
238 static gboolean cc_iofunc_err(GIOChannel *ioc, GIOCondition cond, gpointer data)
240 if (cond & (G_IO_IN | G_IO_PRI))
242 gchar *msg = NULL;
243 GString *str = g_string_sized_new(256);
244 GIOStatus rv;
248 rv = g_io_channel_read_line(ioc, &msg, NULL, NULL, NULL);
249 if (msg != NULL)
251 g_string_append(str, msg);
252 g_free(msg);
254 } while (rv == G_IO_STATUS_NORMAL || rv == G_IO_STATUS_AGAIN);
256 if (NZV(str->str))
258 g_warning("%s: %s\n", (const gchar *) data, str->str);
259 ui_set_statusbar(TRUE,
260 _("The executed custom command returned an error. "
261 "Your selection was not changed. Error message: %s"),
262 str->str);
263 cc_error_occurred = TRUE;
266 g_string_free(str, TRUE);
268 cc_reading_finished = TRUE;
269 return FALSE;
273 static gboolean cc_replace_sel_cb(gpointer user_data)
275 GeanyDocument *doc = user_data;
277 if (! cc_reading_finished)
278 { /* keep this function in the main loop until cc_iofunc_err() has finished */
279 return TRUE;
282 if (! cc_error_occurred && cc_buffer != NULL)
283 { /* Command completed successfully */
284 sci_replace_sel(doc->editor->sci, cc_buffer->str);
285 g_string_free(cc_buffer, TRUE);
286 cc_buffer = NULL;
289 cc_error_occurred = FALSE;
290 cc_reading_finished = FALSE;
292 return FALSE;
296 /* check whether the executed command failed and if so do nothing.
297 * If it returned with a sucessful exit code, replace the selection. */
298 static void cc_exit_cb(GPid child_pid, gint status, gpointer user_data)
300 /* if there was already an error, skip further checks */
301 if (! cc_error_occurred)
303 #ifdef G_OS_UNIX
304 if (WIFEXITED(status))
306 if (WEXITSTATUS(status) != EXIT_SUCCESS)
307 cc_error_occurred = TRUE;
309 else if (WIFSIGNALED(status))
310 { /* the terminating signal: WTERMSIG (status)); */
311 cc_error_occurred = TRUE;
313 else
314 { /* any other failure occured */
315 cc_error_occurred = TRUE;
317 #else
318 cc_error_occurred = ! win32_get_exit_status(child_pid);
319 #endif
321 if (cc_error_occurred)
322 { /* here we are sure cc_error_occurred was set due to an unsuccessful exit code
323 * and so we add an error message */
324 /* TODO maybe include the exit code in the error message */
325 ui_set_statusbar(TRUE,
326 _("The executed custom command exited with an unsuccessful exit code."));
330 g_idle_add(cc_replace_sel_cb, user_data);
331 g_spawn_close_pid(child_pid);
335 /* Executes command (which should include all necessary command line args) and passes the current
336 * selection through the standard input of command. The whole output of command replaces the
337 * current selection. */
338 void tools_execute_custom_command(GeanyDocument *doc, const gchar *command)
340 GError *error = NULL;
341 GPid pid;
342 gchar **argv;
343 gint stdin_fd;
344 gint stdout_fd;
345 gint stderr_fd;
347 g_return_if_fail(doc != NULL && command != NULL);
349 if (! sci_has_selection(doc->editor->sci))
350 editor_select_lines(doc->editor, FALSE);
352 if (!g_shell_parse_argv(command, NULL, &argv, &error))
354 ui_set_statusbar(TRUE, _("Custom command failed: %s"), error->message);
355 g_error_free(error);
356 return;
358 ui_set_statusbar(TRUE, _("Passing data and executing custom command: %s"), command);
360 cc_error_occurred = FALSE;
362 if (g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
363 NULL, NULL, &pid, &stdin_fd, &stdout_fd, &stderr_fd, &error))
365 gchar *sel;
366 gint len, remaining, wrote;
368 if (pid != 0)
369 g_child_watch_add(pid, (GChildWatchFunc) cc_exit_cb, doc);
371 /* use GIOChannel to monitor stdout */
372 utils_set_up_io_channel(stdout_fd, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
373 FALSE, cc_iofunc, NULL);
374 /* copy program's stderr to Geany's stdout to help error tracking */
375 utils_set_up_io_channel(stderr_fd, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
376 FALSE, cc_iofunc_err, (gpointer)command);
378 /* get selection */
379 len = sci_get_selected_text_length(doc->editor->sci);
380 sel = g_malloc0(len + 1);
381 sci_get_selected_text(doc->editor->sci, sel);
383 /* write data to the command */
384 remaining = len - 1;
387 wrote = write(stdin_fd, sel, remaining);
388 if (G_UNLIKELY(wrote < 0))
390 g_warning("%s: %s: %s\n", G_STRFUNC, "Failed sending data to command",
391 g_strerror(errno));
392 break;
394 remaining -= wrote;
395 } while (remaining > 0);
396 close(stdin_fd);
397 g_free(sel);
399 else
401 geany_debug("g_spawn_async_with_pipes() failed: %s", error->message);
402 ui_set_statusbar(TRUE, _("Custom command failed: %s"), error->message);
403 g_error_free(error);
406 g_strfreev(argv);
410 static void cc_dialog_on_command_edited(GtkCellRendererText *renderer, gchar *path, gchar *text,
411 struct cc_dialog *cc)
413 GtkTreeIter iter;
415 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(cc->store), &iter, path);
416 gtk_list_store_set(cc->store, &iter, CC_COLUMN_CMD, text, -1);
417 cc_dialog_update_row_status(cc->store, &iter, text);
421 static void cc_dialog_on_label_edited(GtkCellRendererText *renderer, gchar *path, gchar *text,
422 struct cc_dialog *cc)
424 GtkTreeIter iter;
426 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(cc->store), &iter, path);
427 gtk_list_store_set(cc->store, &iter, CC_COLUMN_LABEL, text, -1);
431 /* re-compute IDs to reflect the current store state */
432 static void cc_dialog_update_ids(struct cc_dialog *cc)
434 GtkTreeIter iter;
436 cc->count = 1;
437 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc->store), &iter))
438 return;
442 gtk_list_store_set(cc->store, &iter, CC_COLUMN_ID, cc->count, -1);
443 cc->count++;
445 while (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc->store), &iter));
449 /* update sensitiveness of the buttons according to the selection */
450 static void cc_dialog_update_sensitive(struct cc_dialog *cc)
452 GtkTreeIter iter;
453 gboolean has_selection = FALSE;
454 gboolean first_selected = FALSE;
455 gboolean last_selected = FALSE;
457 if ((has_selection = gtk_tree_selection_get_selected(cc->selection, NULL, &iter)))
459 GtkTreePath *path;
460 GtkTreePath *copy;
462 path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc->store), &iter);
463 copy = gtk_tree_path_copy(path);
464 first_selected = ! gtk_tree_path_prev(copy);
465 gtk_tree_path_free(copy);
466 gtk_tree_path_next(path);
467 last_selected = ! gtk_tree_model_get_iter(GTK_TREE_MODEL(cc->store), &iter, path);
468 gtk_tree_path_free(path);
471 gtk_widget_set_sensitive(cc->button_remove, has_selection);
472 gtk_widget_set_sensitive(cc->button_up, has_selection && ! first_selected);
473 gtk_widget_set_sensitive(cc->button_down, has_selection && ! last_selected);
477 static void cc_dialog_on_tree_selection_changed(GtkTreeSelection *selection, struct cc_dialog *cc)
479 cc_dialog_update_sensitive(cc);
483 static void cc_dialog_on_row_inserted(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter,
484 struct cc_dialog *cc)
486 cc_dialog_update_ids(cc);
487 cc_dialog_update_sensitive(cc);
491 static void cc_dialog_on_row_deleted(GtkTreeModel *model, GtkTreePath *path, struct cc_dialog *cc)
493 cc_dialog_update_ids(cc);
494 cc_dialog_update_sensitive(cc);
498 static void cc_dialog_on_rows_reordered(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter,
499 gpointer new_order, struct cc_dialog *cc)
501 cc_dialog_update_ids(cc);
502 cc_dialog_update_sensitive(cc);
506 static void cc_show_dialog_custom_commands(void)
508 GtkWidget *dialog, *label, *vbox, *scroll, *buttonbox;
509 GtkCellRenderer *renderer;
510 GtkTreeViewColumn *column;
511 guint i;
512 struct cc_dialog cc;
514 dialog = gtk_dialog_new_with_buttons(_("Set Custom Commands"), GTK_WINDOW(main_widgets.window),
515 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
516 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
517 gtk_window_set_default_size(GTK_WINDOW(dialog), 300, 300); /* give a reasonable minimal default size */
518 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
519 gtk_box_set_spacing(GTK_BOX(vbox), 6);
520 gtk_widget_set_name(dialog, "GeanyDialog");
522 label = gtk_label_new(_("You can send the current selection to any of these commands and the output of the command replaces the current selection."));
523 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
524 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
525 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
527 cc.count = 1;
528 cc.store = gtk_list_store_new(CC_COLUMN_COUNT, G_TYPE_UINT, G_TYPE_STRING, G_TYPE_STRING,
529 G_TYPE_STRING, G_TYPE_STRING);
530 cc.view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(cc.store));
531 gtk_tree_view_set_tooltip_column(GTK_TREE_VIEW(cc.view), CC_COLUMN_TOOLTIP);
532 gtk_tree_view_set_reorderable(GTK_TREE_VIEW(cc.view), TRUE);
533 cc.selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cc.view));
534 /* ID column */
535 renderer = gtk_cell_renderer_text_new();
536 column = gtk_tree_view_column_new_with_attributes(_("ID"), renderer, "text", CC_COLUMN_ID, NULL);
537 gtk_tree_view_append_column(GTK_TREE_VIEW(cc.view), column);
538 /* command column, holding status and command display */
539 column = g_object_new(GTK_TYPE_TREE_VIEW_COLUMN, "title", _("Command"), "expand", TRUE, "resizable", TRUE, NULL);
540 renderer = gtk_cell_renderer_pixbuf_new();
541 gtk_tree_view_column_pack_start(column, renderer, FALSE);
542 gtk_tree_view_column_set_attributes(column, renderer, "stock-id", CC_COLUMN_STATUS, NULL);
543 renderer = gtk_cell_renderer_text_new();
544 g_object_set(renderer, "editable", TRUE, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
545 g_signal_connect(renderer, "edited", G_CALLBACK(cc_dialog_on_command_edited), &cc);
546 gtk_tree_view_column_pack_start(column, renderer, TRUE);
547 gtk_tree_view_column_set_attributes(column, renderer, "text", CC_COLUMN_CMD, NULL);
548 cc.edit_column = column;
549 gtk_tree_view_append_column(GTK_TREE_VIEW(cc.view), column);
550 /* label column */
551 renderer = gtk_cell_renderer_text_new();
552 g_object_set(renderer, "editable", TRUE, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
553 g_signal_connect(renderer, "edited", G_CALLBACK(cc_dialog_on_label_edited), &cc);
554 column = gtk_tree_view_column_new_with_attributes(_("Label"), renderer, "text", CC_COLUMN_LABEL, NULL);
555 g_object_set(column, "expand", TRUE, "resizable", TRUE, NULL);
556 gtk_tree_view_append_column(GTK_TREE_VIEW(cc.view), column);
558 scroll = gtk_scrolled_window_new(NULL, NULL);
559 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_AUTOMATIC,
560 GTK_POLICY_AUTOMATIC);
561 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll), GTK_SHADOW_IN);
562 gtk_container_add(GTK_CONTAINER(scroll), cc.view);
563 gtk_box_pack_start(GTK_BOX(vbox), scroll, TRUE, TRUE, 0);
565 if (ui_prefs.custom_commands != NULL)
567 GtkTreeIter iter;
568 guint len = g_strv_length(ui_prefs.custom_commands);
570 for (i = 0; i < len; i++)
572 if (! NZV(ui_prefs.custom_commands[i]))
573 continue; /* skip empty fields */
575 cc_dialog_add_command(&cc, i, FALSE);
578 /* focus the first row if any */
579 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc.store), &iter))
581 GtkTreePath *path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc.store), &iter);
583 gtk_tree_view_set_cursor(GTK_TREE_VIEW(cc.view), path, cc.edit_column, FALSE);
584 gtk_tree_path_free(path);
588 buttonbox = gtk_hbutton_box_new();
589 gtk_box_set_spacing(GTK_BOX(buttonbox), 6);
590 gtk_box_pack_start(GTK_BOX(vbox), buttonbox, FALSE, FALSE, 0);
591 cc.button_add = gtk_button_new_from_stock(GTK_STOCK_ADD);
592 g_signal_connect(cc.button_add, "clicked", G_CALLBACK(cc_on_dialog_add_clicked), &cc);
593 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_add);
594 cc.button_remove = gtk_button_new_from_stock(GTK_STOCK_REMOVE);
595 g_signal_connect(cc.button_remove, "clicked", G_CALLBACK(cc_on_dialog_remove_clicked), &cc);
596 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_remove);
597 cc.button_up = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
598 g_signal_connect(cc.button_up, "clicked", G_CALLBACK(cc_on_dialog_move_up_clicked), &cc);
599 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_up);
600 cc.button_down = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
601 g_signal_connect(cc.button_down, "clicked", G_CALLBACK(cc_on_dialog_move_down_clicked), &cc);
602 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_down);
604 cc_dialog_update_sensitive(&cc);
606 /* only connect the selection signal when all other cc_dialog fields are set */
607 g_signal_connect(cc.selection, "changed", G_CALLBACK(cc_dialog_on_tree_selection_changed), &cc);
608 g_signal_connect(cc.store, "row-inserted", G_CALLBACK(cc_dialog_on_row_inserted), &cc);
609 g_signal_connect(cc.store, "row-deleted", G_CALLBACK(cc_dialog_on_row_deleted), &cc);
610 g_signal_connect(cc.store, "rows-reordered", G_CALLBACK(cc_dialog_on_rows_reordered), &cc);
612 gtk_widget_show_all(vbox);
614 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
616 GSList *cmd_list = NULL;
617 GSList *lbl_list = NULL;
618 gint len = 0;
619 gchar **commands = NULL;
620 gchar **labels = NULL;
621 GtkTreeIter iter;
623 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc.store), &iter))
627 gchar *cmd;
628 gchar *lbl;
630 gtk_tree_model_get(GTK_TREE_MODEL(cc.store), &iter, CC_COLUMN_CMD, &cmd, CC_COLUMN_LABEL, &lbl, -1);
631 if (NZV(cmd))
633 cmd_list = g_slist_prepend(cmd_list, cmd);
634 lbl_list = g_slist_prepend(lbl_list, lbl);
635 len++;
637 else
639 g_free(cmd);
640 g_free(lbl);
643 while (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc.store), &iter));
645 cmd_list = g_slist_reverse(cmd_list);
646 lbl_list = g_slist_reverse(lbl_list);
647 /* create a new null-terminated array but only if there is any commands defined */
648 if (len > 0)
650 gint j = 0;
651 GSList *cmd_node, *lbl_node;
653 commands = g_new(gchar*, len + 1);
654 labels = g_new(gchar*, len + 1);
655 /* walk commands and labels lists */
656 for (cmd_node = cmd_list, lbl_node = lbl_list; cmd_node != NULL; cmd_node = cmd_node->next, lbl_node = lbl_node->next)
658 commands[j] = (gchar*) cmd_node->data;
659 labels[j] = (gchar*) lbl_node->data;
660 j++;
662 /* null-terminate the arrays */
663 commands[j] = NULL;
664 labels[j] = NULL;
666 /* set the new arrays */
667 g_strfreev(ui_prefs.custom_commands);
668 ui_prefs.custom_commands = commands;
669 g_strfreev(ui_prefs.custom_commands_labels);
670 ui_prefs.custom_commands_labels = labels;
671 /* rebuild the menu items */
672 tools_create_insert_custom_command_menu_items();
674 g_slist_free(cmd_list);
675 g_slist_free(lbl_list);
677 gtk_widget_destroy(dialog);
681 static void cc_on_custom_command_activate(GtkMenuItem *menuitem, gpointer user_data)
683 GeanyDocument *doc = document_get_current();
684 gint command_idx;
686 g_return_if_fail(doc != NULL);
688 command_idx = GPOINTER_TO_INT(user_data);
690 if (ui_prefs.custom_commands == NULL ||
691 command_idx < 0 || command_idx > (gint) g_strv_length(ui_prefs.custom_commands))
693 cc_show_dialog_custom_commands();
694 return;
697 /* send it through the command and when the command returned the output the current selection
698 * will be replaced */
699 tools_execute_custom_command(doc, ui_prefs.custom_commands[command_idx]);
703 static void cc_insert_custom_command_items(GtkMenu *me, const gchar *label, const gchar *tooltip, gint idx)
705 GtkWidget *item;
706 gint key_idx = -1;
707 GeanyKeyBinding *kb = NULL;
709 switch (idx)
711 case 0: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD1; break;
712 case 1: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD2; break;
713 case 2: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD3; break;
716 item = gtk_menu_item_new_with_label(label);
717 gtk_widget_set_tooltip_text(item, tooltip);
718 if (key_idx != -1)
720 kb = keybindings_lookup_item(GEANY_KEY_GROUP_FORMAT, key_idx);
721 gtk_widget_add_accelerator(item, "activate", gtk_accel_group_new(),
722 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
724 gtk_container_add(GTK_CONTAINER(me), item);
725 gtk_widget_show(item);
726 g_signal_connect(item, "activate", G_CALLBACK(cc_on_custom_command_activate),
727 GINT_TO_POINTER(idx));
731 void tools_create_insert_custom_command_menu_items(void)
733 GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "send_selection_to2_menu"));
734 GtkWidget *item;
735 GList *me_children, *node;
737 /* first clean the menus to be able to rebuild them */
738 me_children = gtk_container_get_children(GTK_CONTAINER(menu_edit));
739 foreach_list(node, me_children)
740 gtk_widget_destroy(GTK_WIDGET(node->data));
741 g_list_free(me_children);
743 if (ui_prefs.custom_commands == NULL || g_strv_length(ui_prefs.custom_commands) == 0)
745 item = gtk_menu_item_new_with_label(_("No custom commands defined."));
746 gtk_container_add(GTK_CONTAINER(menu_edit), item);
747 gtk_widget_set_sensitive(item, FALSE);
748 gtk_widget_show(item);
750 else
752 guint i, len;
753 gint idx = 0;
754 len = g_strv_length(ui_prefs.custom_commands);
755 for (i = 0; i < len; i++)
757 const gchar *label = ui_prefs.custom_commands_labels[i];
759 if (! NZV(label))
760 label = ui_prefs.custom_commands[i];
761 if (NZV(label)) /* skip empty items */
763 cc_insert_custom_command_items(menu_edit, label, ui_prefs.custom_commands[i], idx);
764 idx++;
769 /* separator and Set menu item */
770 item = gtk_separator_menu_item_new();
771 gtk_container_add(GTK_CONTAINER(menu_edit), item);
772 gtk_widget_show(item);
774 cc_insert_custom_command_items(menu_edit, _("Set Custom Commands"), NULL, -1);
778 /* (stolen from bluefish, thanks)
779 * Returns number of characters, lines and words in the supplied gchar*.
780 * Handles UTF-8 correctly. Input must be properly encoded UTF-8.
781 * Words are defined as any characters grouped, separated with spaces. */
782 static void word_count(gchar *text, guint *chars, guint *lines, guint *words)
784 guint in_word = 0;
785 gunichar utext;
787 if (! text)
788 return; /* politely refuse to operate on NULL */
790 *chars = *words = *lines = 0;
791 while (*text != '\0')
793 (*chars)++;
795 switch (*text)
797 case '\n':
798 (*lines)++;
799 case '\r':
800 case '\f':
801 case '\t':
802 case ' ':
803 case '\v':
804 mb_word_separator:
805 if (in_word)
807 in_word = 0;
808 (*words)++;
810 break;
811 default:
812 utext = g_utf8_get_char_validated(text, 2); /* This might be an utf-8 char */
813 if (g_unichar_isspace(utext)) /* Unicode encoded space? */
814 goto mb_word_separator;
815 if (g_unichar_isgraph(utext)) /* Is this something printable? */
816 in_word = 1;
817 break;
819 /* Even if the current char is 2 bytes, this will iterate correctly. */
820 text = g_utf8_next_char(text);
823 /* Capture last word, if there's no whitespace at the end of the file. */
824 if (in_word)
825 (*words)++;
826 /* We start counting line numbers from 1 */
827 if (*chars > 0)
828 (*lines)++;
832 void tools_word_count(void)
834 GtkWidget *dialog, *label, *vbox, *table;
835 GeanyDocument *doc;
836 guint chars = 0, lines = 0, words = 0;
837 gchar *text;
838 const gchar *range;
840 doc = document_get_current();
841 g_return_if_fail(doc != NULL);
843 dialog = gtk_dialog_new_with_buttons(_("Word Count"), GTK_WINDOW(main_widgets.window),
844 GTK_DIALOG_DESTROY_WITH_PARENT,
845 GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL, NULL);
846 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
847 gtk_widget_set_name(dialog, "GeanyDialog");
849 if (sci_has_selection(doc->editor->sci))
851 text = sci_get_selection_contents(doc->editor->sci);
852 range = _("selection");
854 else
856 text = sci_get_contents(doc->editor->sci, -1);
857 range = _("whole document");
859 word_count(text, &chars, &lines, &words);
860 g_free(text);
862 table = gtk_table_new(4, 2, FALSE);
863 gtk_table_set_row_spacings(GTK_TABLE(table), 5);
864 gtk_table_set_col_spacings(GTK_TABLE(table), 10);
866 label = gtk_label_new(_("Range:"));
867 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1,
868 (GtkAttachOptions) (GTK_FILL),
869 (GtkAttachOptions) (0), 0, 0);
870 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
872 label = gtk_label_new(range);
873 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 0, 1,
874 (GtkAttachOptions) (GTK_FILL),
875 (GtkAttachOptions) (0), 20, 0);
876 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
878 label = gtk_label_new(_("Lines:"));
879 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2,
880 (GtkAttachOptions) (GTK_FILL),
881 (GtkAttachOptions) (0), 0, 0);
882 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
884 text = g_strdup_printf("%d", lines);
885 label = gtk_label_new(text);
886 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 1, 2,
887 (GtkAttachOptions) (GTK_FILL),
888 (GtkAttachOptions) (0), 20, 0);
889 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
890 g_free(text);
892 label = gtk_label_new(_("Words:"));
893 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 2, 3,
894 (GtkAttachOptions) (GTK_FILL),
895 (GtkAttachOptions) (0), 0, 0);
896 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
898 text = g_strdup_printf("%d", words);
899 label = gtk_label_new(text);
900 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 2, 3,
901 (GtkAttachOptions) (GTK_FILL),
902 (GtkAttachOptions) (0), 20, 0);
903 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
904 g_free(text);
906 label = gtk_label_new(_("Characters:"));
907 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 3, 4,
908 (GtkAttachOptions) (GTK_FILL),
909 (GtkAttachOptions) (0), 0, 0);
910 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
912 text = g_strdup_printf("%d", chars);
913 label = gtk_label_new(text);
914 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 3, 4,
915 (GtkAttachOptions) (GTK_FILL),
916 (GtkAttachOptions) (0), 20, 0);
917 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
918 g_free(text);
920 gtk_container_add(GTK_CONTAINER(vbox), table);
922 g_signal_connect(dialog, "response", G_CALLBACK(gtk_widget_destroy), dialog);
923 g_signal_connect(dialog, "delete-event", G_CALLBACK(gtk_widget_destroy), dialog);
925 gtk_widget_show_all(dialog);
930 * color dialog callbacks
932 #ifndef G_OS_WIN32
933 static void on_color_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
935 switch (response)
937 case GTK_RESPONSE_OK:
939 GdkColor color;
940 GeanyDocument *doc = document_get_current();
941 gchar *hex;
942 GtkWidget *colorsel;
944 gtk_widget_hide(ui_widgets.open_colorsel);
945 g_return_if_fail(doc != NULL);
947 colorsel = gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel));
948 gtk_color_selection_get_current_color(GTK_COLOR_SELECTION(colorsel), &color);
950 hex = utils_get_hex_from_color(&color);
951 editor_insert_color(doc->editor, hex);
952 g_free(hex);
953 break;
956 default:
957 gtk_widget_hide(ui_widgets.open_colorsel);
960 #endif
963 /* This shows the color selection dialog to choose a color. */
964 void tools_color_chooser(const gchar *color)
966 #ifdef G_OS_WIN32
967 win32_show_color_dialog(color);
968 #else
969 gchar *c = (gchar*) color;
970 GtkWidget *colorsel;
972 if (ui_widgets.open_colorsel == NULL)
974 ui_widgets.open_colorsel = gtk_color_selection_dialog_new(_("Color Chooser"));
975 gtk_widget_set_name(ui_widgets.open_colorsel, "GeanyDialog");
976 gtk_window_set_transient_for(GTK_WINDOW(ui_widgets.open_colorsel), GTK_WINDOW(main_widgets.window));
977 colorsel = gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel));
978 gtk_color_selection_set_has_palette(GTK_COLOR_SELECTION(colorsel), TRUE);
980 g_signal_connect(ui_widgets.open_colorsel, "response",
981 G_CALLBACK(on_color_dialog_response), NULL);
982 g_signal_connect(ui_widgets.open_colorsel, "delete-event",
983 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
985 else
986 colorsel = gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel));
987 /* if color is non-NULL set it in the dialog as preselected color */
988 if (c != NULL && (c[0] == '0' || c[0] == '#'))
990 GdkColor gc;
992 if (c[0] == '0' && c[1] == 'x')
993 { /* we have a string of the format "0x00ff00" and we need it to "#00ff00" */
994 c[1] = '#';
995 c++;
997 gdk_color_parse(c, &gc);
998 gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(colorsel), &gc);
999 gtk_color_selection_set_previous_color(GTK_COLOR_SELECTION(colorsel), &gc);
1002 /* We make sure the dialog is visible. */
1003 gtk_window_present(GTK_WINDOW(ui_widgets.open_colorsel));
1004 #endif