Merge pull request #464 from eht16/undeprecate_plugins
[geany-mirror.git] / src / tools.c
blob57d6005728fc8af41265d008e4e44631eb334c7a
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 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include "tools.h"
33 #include "document.h"
34 #include "keybindings.h"
35 #include "sciwrappers.h"
36 #include "support.h"
37 #include "ui_utils.h"
38 #include "utils.h"
39 #include "win32.h"
41 #include "gtkcompat.h"
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <string.h>
46 #include <errno.h>
48 #ifdef G_OS_UNIX
49 # include <sys/types.h>
50 # include <sys/wait.h>
51 # include <signal.h>
52 #endif
55 enum
57 CC_COLUMN_ID,
58 CC_COLUMN_STATUS,
59 CC_COLUMN_TOOLTIP,
60 CC_COLUMN_CMD,
61 CC_COLUMN_LABEL,
62 CC_COLUMN_COUNT
65 /* custom commands code*/
66 struct cc_dialog
68 guint count;
69 GtkWidget *view;
70 GtkTreeViewColumn *edit_column;
71 GtkListStore *store;
72 GtkTreeSelection *selection;
73 GtkWidget *button_add;
74 GtkWidget *button_remove;
75 GtkWidget *button_up;
76 GtkWidget *button_down;
79 /* data required by the custom command callbacks */
80 struct cc_data
82 const gchar *command; /* command launched */
83 GeanyDocument *doc; /* document in which replace the selection */
84 GString *buffer; /* buffer holding stdout content, or NULL */
85 gboolean error; /* whether and error occurred */
86 gboolean finished; /* whether the command has finished */
90 static gboolean cc_exists_command(const gchar *command)
92 gchar *path = g_find_program_in_path(command);
94 g_free(path);
96 return path != NULL;
100 /* update STATUS and TOOLTIP columns according to cmd */
101 static void cc_dialog_update_row_status(GtkListStore *store, GtkTreeIter *iter, const gchar *cmd)
103 GError *err = NULL;
104 const gchar *stock_id = GTK_STOCK_NO;
105 gchar *tooltip = NULL;
106 gint argc;
107 gchar **argv;
109 if (EMPTY(cmd))
110 stock_id = GTK_STOCK_YES;
111 else if (g_shell_parse_argv(cmd, &argc, &argv, &err))
113 if (argc > 0 && cc_exists_command(argv[0]))
114 stock_id = GTK_STOCK_YES;
115 else
116 tooltip = g_strdup_printf(_("Invalid command: %s"), _("Command not found"));
117 g_strfreev(argv);
119 else
121 tooltip = g_strdup_printf(_("Invalid command: %s"), err->message);
122 g_error_free(err);
125 gtk_list_store_set(store, iter, CC_COLUMN_STATUS, stock_id, CC_COLUMN_TOOLTIP, tooltip, -1);
126 g_free(tooltip);
130 /* adds a new row for custom command @p idx, or an new empty one if < 0 */
131 static void cc_dialog_add_command(struct cc_dialog *cc, gint idx, gboolean start_editing)
133 GtkTreeIter iter;
134 const gchar *cmd = NULL;
135 const gchar *label = NULL;
136 guint id = cc->count;
138 if (idx >= 0)
140 cmd = ui_prefs.custom_commands[idx];
141 label = ui_prefs.custom_commands_labels[idx];
144 cc->count++;
145 gtk_list_store_append(cc->store, &iter);
146 gtk_list_store_set(cc->store, &iter, CC_COLUMN_ID, id, CC_COLUMN_CMD, cmd, CC_COLUMN_LABEL, label, -1);
147 cc_dialog_update_row_status(cc->store, &iter, cmd);
149 if (start_editing)
151 GtkTreePath *path;
153 gtk_widget_grab_focus(cc->view);
154 path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc->store), &iter);
155 gtk_tree_view_set_cursor(GTK_TREE_VIEW(cc->view), path, cc->edit_column, TRUE);
156 gtk_tree_path_free(path);
161 static void cc_on_dialog_add_clicked(GtkButton *button, struct cc_dialog *cc)
163 cc_dialog_add_command(cc, -1, TRUE);
167 static void scroll_to_cursor(GtkTreeView *view)
169 GtkTreePath *path;
170 GtkTreeViewColumn *column;
172 gtk_tree_view_get_cursor(view, &path, &column);
173 if (path)
175 gtk_tree_view_scroll_to_cell(view, path, column, FALSE, 1.0, 1.0);
176 gtk_tree_path_free(path);
180 static void cc_on_dialog_remove_clicked(GtkButton *button, struct cc_dialog *cc)
182 GtkTreeIter iter;
184 if (gtk_tree_selection_get_selected(cc->selection, NULL, &iter))
186 gtk_list_store_remove(cc->store, &iter);
187 scroll_to_cursor(GTK_TREE_VIEW(cc->view));
192 static void cc_on_dialog_move_up_clicked(GtkButton *button, struct cc_dialog *cc)
194 GtkTreeIter iter;
196 if (gtk_tree_selection_get_selected(cc->selection, NULL, &iter))
198 GtkTreePath *path;
199 GtkTreeIter prev;
201 path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc->store), &iter);
202 if (gtk_tree_path_prev(path) &&
203 gtk_tree_model_get_iter(GTK_TREE_MODEL(cc->store), &prev, path))
205 gtk_list_store_move_before(cc->store, &iter, &prev);
206 scroll_to_cursor(GTK_TREE_VIEW(cc->view));
208 gtk_tree_path_free(path);
213 static void cc_on_dialog_move_down_clicked(GtkButton *button, struct cc_dialog *cc)
215 GtkTreeIter iter;
217 if (gtk_tree_selection_get_selected(cc->selection, NULL, &iter))
219 GtkTreeIter next = iter;
221 if (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc->store), &next))
223 gtk_list_store_move_after(cc->store, &iter, &next);
224 scroll_to_cursor(GTK_TREE_VIEW(cc->view));
230 static gboolean cc_iofunc(GIOChannel *ioc, GIOCondition cond, gpointer user_data)
232 struct cc_data *data = user_data;
234 if (cond & (G_IO_IN | G_IO_PRI))
236 gchar *msg = NULL;
237 GIOStatus rv;
238 GError *err = NULL;
240 if (! data->buffer)
241 data->buffer = g_string_sized_new(256);
245 rv = g_io_channel_read_line(ioc, &msg, NULL, NULL, &err);
246 if (msg != NULL)
248 g_string_append(data->buffer, msg);
249 g_free(msg);
251 if (G_UNLIKELY(err != NULL))
253 geany_debug("%s: %s", G_STRFUNC, err->message);
254 g_error_free(err);
255 err = NULL;
257 } while (rv == G_IO_STATUS_NORMAL || rv == G_IO_STATUS_AGAIN);
259 if (G_UNLIKELY(rv != G_IO_STATUS_EOF))
260 { /* Something went wrong? */
261 g_warning("%s: %s\n", G_STRFUNC, "Incomplete command output");
264 return FALSE;
268 static gboolean cc_iofunc_err(GIOChannel *ioc, GIOCondition cond, gpointer user_data)
270 struct cc_data *data = user_data;
272 if (cond & (G_IO_IN | G_IO_PRI))
274 gchar *msg = NULL;
275 GString *str = g_string_sized_new(256);
276 GIOStatus rv;
280 rv = g_io_channel_read_line(ioc, &msg, NULL, NULL, NULL);
281 if (msg != NULL)
283 g_string_append(str, msg);
284 g_free(msg);
286 } while (rv == G_IO_STATUS_NORMAL || rv == G_IO_STATUS_AGAIN);
288 if (!EMPTY(str->str))
290 g_warning("%s: %s\n", data->command, str->str);
291 ui_set_statusbar(TRUE,
292 _("The executed custom command returned an error. "
293 "Your selection was not changed. Error message: %s"),
294 str->str);
295 data->error = TRUE;
298 g_string_free(str, TRUE);
300 data->finished = TRUE;
301 return FALSE;
305 static gboolean cc_replace_sel_cb(gpointer user_data)
307 struct cc_data *data = user_data;
309 if (! data->finished)
310 { /* keep this function in the main loop until cc_iofunc_err() has finished */
311 return TRUE;
314 if (! data->error && data->buffer != NULL && DOC_VALID(data->doc))
315 { /* Command completed successfully */
316 sci_replace_sel(data->doc->editor->sci, data->buffer->str);
319 if (data->buffer)
320 g_string_free(data->buffer, TRUE);
321 g_slice_free1(sizeof *data, data);
323 return FALSE;
327 /* check whether the executed command failed and if so do nothing.
328 * If it returned with a successful exit code, replace the selection. */
329 static void cc_exit_cb(GPid child_pid, gint status, gpointer user_data)
331 struct cc_data *data = user_data;
333 /* if there was already an error, skip further checks */
334 if (! data->error)
336 #ifdef G_OS_UNIX
337 if (WIFEXITED(status))
339 if (WEXITSTATUS(status) != EXIT_SUCCESS)
340 data->error = TRUE;
342 else if (WIFSIGNALED(status))
343 { /* the terminating signal: WTERMSIG (status)); */
344 data->error = TRUE;
346 else
347 { /* any other failure occurred */
348 data->error = TRUE;
350 #else
351 data->error = ! win32_get_exit_status(child_pid);
352 #endif
354 if (data->error)
355 { /* here we are sure data->error was set due to an unsuccessful exit code
356 * and so we add an error message */
357 /* TODO maybe include the exit code in the error message */
358 ui_set_statusbar(TRUE,
359 _("The executed custom command exited with an unsuccessful exit code."));
363 g_idle_add(cc_replace_sel_cb, data);
364 g_spawn_close_pid(child_pid);
368 /* Executes command (which should include all necessary command line args) and passes the current
369 * selection through the standard input of command. The whole output of command replaces the
370 * current selection. */
371 void tools_execute_custom_command(GeanyDocument *doc, const gchar *command)
373 GError *error = NULL;
374 GPid pid;
375 gchar **argv;
376 gint stdin_fd;
377 gint stdout_fd;
378 gint stderr_fd;
380 g_return_if_fail(DOC_VALID(doc) && command != NULL);
382 if (! sci_has_selection(doc->editor->sci))
383 editor_select_lines(doc->editor, FALSE);
385 if (!g_shell_parse_argv(command, NULL, &argv, &error))
387 ui_set_statusbar(TRUE, _("Custom command failed: %s"), error->message);
388 g_error_free(error);
389 return;
391 ui_set_statusbar(TRUE, _("Passing data and executing custom command: %s"), command);
393 if (g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
394 NULL, NULL, &pid, &stdin_fd, &stdout_fd, &stderr_fd, &error))
396 gchar *sel;
397 gint remaining, wrote;
398 struct cc_data *data = g_slice_alloc(sizeof *data);
400 data->error = FALSE;
401 data->finished = FALSE;
402 data->buffer = NULL;
403 data->doc = doc;
404 data->command = command;
406 g_child_watch_add(pid, cc_exit_cb, data);
408 /* use GIOChannel to monitor stdout */
409 utils_set_up_io_channel(stdout_fd, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
410 FALSE, cc_iofunc, data);
411 /* copy program's stderr to Geany's stdout to help error tracking */
412 utils_set_up_io_channel(stderr_fd, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
413 FALSE, cc_iofunc_err, data);
415 /* get selection */
416 sel = sci_get_selection_contents(doc->editor->sci);
418 /* write data to the command */
419 remaining = strlen(sel);
422 wrote = write(stdin_fd, sel, remaining);
423 if (G_UNLIKELY(wrote < 0))
425 g_warning("%s: %s: %s\n", G_STRFUNC, "Failed sending data to command",
426 g_strerror(errno));
427 break;
429 remaining -= wrote;
430 } while (remaining > 0);
431 close(stdin_fd);
432 g_free(sel);
434 else
436 geany_debug("g_spawn_async_with_pipes() failed: %s", error->message);
437 ui_set_statusbar(TRUE, _("Custom command failed: %s"), error->message);
438 g_error_free(error);
441 g_strfreev(argv);
445 static void cc_dialog_on_command_edited(GtkCellRendererText *renderer, gchar *path, gchar *text,
446 struct cc_dialog *cc)
448 GtkTreeIter iter;
450 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(cc->store), &iter, path);
451 gtk_list_store_set(cc->store, &iter, CC_COLUMN_CMD, text, -1);
452 cc_dialog_update_row_status(cc->store, &iter, text);
456 static void cc_dialog_on_label_edited(GtkCellRendererText *renderer, gchar *path, gchar *text,
457 struct cc_dialog *cc)
459 GtkTreeIter iter;
461 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(cc->store), &iter, path);
462 gtk_list_store_set(cc->store, &iter, CC_COLUMN_LABEL, text, -1);
466 /* re-compute IDs to reflect the current store state */
467 static void cc_dialog_update_ids(struct cc_dialog *cc)
469 GtkTreeIter iter;
471 cc->count = 1;
472 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc->store), &iter))
473 return;
477 gtk_list_store_set(cc->store, &iter, CC_COLUMN_ID, cc->count, -1);
478 cc->count++;
480 while (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc->store), &iter));
484 /* update sensitiveness of the buttons according to the selection */
485 static void cc_dialog_update_sensitive(struct cc_dialog *cc)
487 GtkTreeIter iter;
488 gboolean has_selection = FALSE;
489 gboolean first_selected = FALSE;
490 gboolean last_selected = FALSE;
492 if ((has_selection = gtk_tree_selection_get_selected(cc->selection, NULL, &iter)))
494 GtkTreePath *path;
495 GtkTreePath *copy;
497 path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc->store), &iter);
498 copy = gtk_tree_path_copy(path);
499 first_selected = ! gtk_tree_path_prev(copy);
500 gtk_tree_path_free(copy);
501 gtk_tree_path_next(path);
502 last_selected = ! gtk_tree_model_get_iter(GTK_TREE_MODEL(cc->store), &iter, path);
503 gtk_tree_path_free(path);
506 gtk_widget_set_sensitive(cc->button_remove, has_selection);
507 gtk_widget_set_sensitive(cc->button_up, has_selection && ! first_selected);
508 gtk_widget_set_sensitive(cc->button_down, has_selection && ! last_selected);
512 static void cc_dialog_on_tree_selection_changed(GtkTreeSelection *selection, struct cc_dialog *cc)
514 cc_dialog_update_sensitive(cc);
518 static void cc_dialog_on_row_inserted(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter,
519 struct cc_dialog *cc)
521 cc_dialog_update_ids(cc);
522 cc_dialog_update_sensitive(cc);
526 static void cc_dialog_on_row_deleted(GtkTreeModel *model, GtkTreePath *path, struct cc_dialog *cc)
528 cc_dialog_update_ids(cc);
529 cc_dialog_update_sensitive(cc);
533 static void cc_dialog_on_rows_reordered(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter,
534 gpointer new_order, struct cc_dialog *cc)
536 cc_dialog_update_ids(cc);
537 cc_dialog_update_sensitive(cc);
541 static void cc_show_dialog_custom_commands(void)
543 GtkWidget *dialog, *label, *vbox, *scroll, *buttonbox;
544 GtkCellRenderer *renderer;
545 GtkTreeViewColumn *column;
546 guint i;
547 struct cc_dialog cc;
549 dialog = gtk_dialog_new_with_buttons(_("Set Custom Commands"), GTK_WINDOW(main_widgets.window),
550 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
551 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
552 gtk_window_set_default_size(GTK_WINDOW(dialog), 300, 300); /* give a reasonable minimal default size */
553 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
554 gtk_box_set_spacing(GTK_BOX(vbox), 6);
555 gtk_widget_set_name(dialog, "GeanyDialog");
557 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."));
558 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
559 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
560 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
562 cc.count = 1;
563 cc.store = gtk_list_store_new(CC_COLUMN_COUNT, G_TYPE_UINT, G_TYPE_STRING, G_TYPE_STRING,
564 G_TYPE_STRING, G_TYPE_STRING);
565 cc.view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(cc.store));
566 ui_tree_view_set_tooltip_text_column(GTK_TREE_VIEW(cc.view), CC_COLUMN_TOOLTIP);
567 gtk_tree_view_set_reorderable(GTK_TREE_VIEW(cc.view), TRUE);
568 cc.selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cc.view));
569 /* ID column */
570 renderer = gtk_cell_renderer_text_new();
571 column = gtk_tree_view_column_new_with_attributes(_("ID"), renderer, "text", CC_COLUMN_ID, NULL);
572 gtk_tree_view_append_column(GTK_TREE_VIEW(cc.view), column);
573 /* command column, holding status and command display */
574 column = g_object_new(GTK_TYPE_TREE_VIEW_COLUMN, "title", _("Command"), "expand", TRUE, "resizable", TRUE, NULL);
575 renderer = gtk_cell_renderer_pixbuf_new();
576 gtk_tree_view_column_pack_start(column, renderer, FALSE);
577 gtk_tree_view_column_set_attributes(column, renderer, "stock-id", CC_COLUMN_STATUS, NULL);
578 renderer = gtk_cell_renderer_text_new();
579 g_object_set(renderer, "editable", TRUE, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
580 g_signal_connect(renderer, "edited", G_CALLBACK(cc_dialog_on_command_edited), &cc);
581 gtk_tree_view_column_pack_start(column, renderer, TRUE);
582 gtk_tree_view_column_set_attributes(column, renderer, "text", CC_COLUMN_CMD, NULL);
583 cc.edit_column = column;
584 gtk_tree_view_append_column(GTK_TREE_VIEW(cc.view), column);
585 /* label column */
586 renderer = gtk_cell_renderer_text_new();
587 g_object_set(renderer, "editable", TRUE, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
588 g_signal_connect(renderer, "edited", G_CALLBACK(cc_dialog_on_label_edited), &cc);
589 column = gtk_tree_view_column_new_with_attributes(_("Label"), renderer, "text", CC_COLUMN_LABEL, NULL);
590 g_object_set(column, "expand", TRUE, "resizable", TRUE, NULL);
591 gtk_tree_view_append_column(GTK_TREE_VIEW(cc.view), column);
593 scroll = gtk_scrolled_window_new(NULL, NULL);
594 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_AUTOMATIC,
595 GTK_POLICY_AUTOMATIC);
596 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll), GTK_SHADOW_IN);
597 gtk_container_add(GTK_CONTAINER(scroll), cc.view);
598 gtk_box_pack_start(GTK_BOX(vbox), scroll, TRUE, TRUE, 0);
600 if (ui_prefs.custom_commands != NULL)
602 GtkTreeIter iter;
603 guint len = g_strv_length(ui_prefs.custom_commands);
605 for (i = 0; i < len; i++)
607 if (EMPTY(ui_prefs.custom_commands[i]))
608 continue; /* skip empty fields */
610 cc_dialog_add_command(&cc, i, FALSE);
613 /* focus the first row if any */
614 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc.store), &iter))
616 GtkTreePath *path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc.store), &iter);
618 gtk_tree_view_set_cursor(GTK_TREE_VIEW(cc.view), path, cc.edit_column, FALSE);
619 gtk_tree_path_free(path);
623 buttonbox = gtk_hbutton_box_new();
624 gtk_box_set_spacing(GTK_BOX(buttonbox), 6);
625 gtk_box_pack_start(GTK_BOX(vbox), buttonbox, FALSE, FALSE, 0);
626 cc.button_add = gtk_button_new_from_stock(GTK_STOCK_ADD);
627 g_signal_connect(cc.button_add, "clicked", G_CALLBACK(cc_on_dialog_add_clicked), &cc);
628 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_add);
629 cc.button_remove = gtk_button_new_from_stock(GTK_STOCK_REMOVE);
630 g_signal_connect(cc.button_remove, "clicked", G_CALLBACK(cc_on_dialog_remove_clicked), &cc);
631 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_remove);
632 cc.button_up = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
633 g_signal_connect(cc.button_up, "clicked", G_CALLBACK(cc_on_dialog_move_up_clicked), &cc);
634 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_up);
635 cc.button_down = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
636 g_signal_connect(cc.button_down, "clicked", G_CALLBACK(cc_on_dialog_move_down_clicked), &cc);
637 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_down);
639 cc_dialog_update_sensitive(&cc);
641 /* only connect the selection signal when all other cc_dialog fields are set */
642 g_signal_connect(cc.selection, "changed", G_CALLBACK(cc_dialog_on_tree_selection_changed), &cc);
643 g_signal_connect(cc.store, "row-inserted", G_CALLBACK(cc_dialog_on_row_inserted), &cc);
644 g_signal_connect(cc.store, "row-deleted", G_CALLBACK(cc_dialog_on_row_deleted), &cc);
645 g_signal_connect(cc.store, "rows-reordered", G_CALLBACK(cc_dialog_on_rows_reordered), &cc);
647 gtk_widget_show_all(vbox);
649 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
651 GSList *cmd_list = NULL;
652 GSList *lbl_list = NULL;
653 gint len = 0;
654 gchar **commands = NULL;
655 gchar **labels = NULL;
656 GtkTreeIter iter;
658 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc.store), &iter))
662 gchar *cmd;
663 gchar *lbl;
665 gtk_tree_model_get(GTK_TREE_MODEL(cc.store), &iter, CC_COLUMN_CMD, &cmd, CC_COLUMN_LABEL, &lbl, -1);
666 if (!EMPTY(cmd))
668 cmd_list = g_slist_prepend(cmd_list, cmd);
669 lbl_list = g_slist_prepend(lbl_list, lbl);
670 len++;
672 else
674 g_free(cmd);
675 g_free(lbl);
678 while (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc.store), &iter));
680 cmd_list = g_slist_reverse(cmd_list);
681 lbl_list = g_slist_reverse(lbl_list);
682 /* create a new null-terminated array but only if there is any commands defined */
683 if (len > 0)
685 gint j = 0;
686 GSList *cmd_node, *lbl_node;
688 commands = g_new(gchar*, len + 1);
689 labels = g_new(gchar*, len + 1);
690 /* walk commands and labels lists */
691 for (cmd_node = cmd_list, lbl_node = lbl_list; cmd_node != NULL; cmd_node = cmd_node->next, lbl_node = lbl_node->next)
693 commands[j] = (gchar*) cmd_node->data;
694 labels[j] = (gchar*) lbl_node->data;
695 j++;
697 /* null-terminate the arrays */
698 commands[j] = NULL;
699 labels[j] = NULL;
701 /* set the new arrays */
702 g_strfreev(ui_prefs.custom_commands);
703 ui_prefs.custom_commands = commands;
704 g_strfreev(ui_prefs.custom_commands_labels);
705 ui_prefs.custom_commands_labels = labels;
706 /* rebuild the menu items */
707 tools_create_insert_custom_command_menu_items();
709 g_slist_free(cmd_list);
710 g_slist_free(lbl_list);
712 gtk_widget_destroy(dialog);
716 static void cc_on_custom_command_activate(GtkMenuItem *menuitem, gpointer user_data)
718 GeanyDocument *doc = document_get_current();
719 gint command_idx;
721 g_return_if_fail(DOC_VALID(doc));
723 command_idx = GPOINTER_TO_INT(user_data);
725 if (ui_prefs.custom_commands == NULL ||
726 command_idx < 0 || command_idx > (gint) g_strv_length(ui_prefs.custom_commands))
728 cc_show_dialog_custom_commands();
729 return;
732 /* send it through the command and when the command returned the output the current selection
733 * will be replaced */
734 tools_execute_custom_command(doc, ui_prefs.custom_commands[command_idx]);
738 static void cc_insert_custom_command_items(GtkMenu *me, const gchar *label, const gchar *tooltip, gint idx)
740 GtkWidget *item;
741 gint key_idx = -1;
742 GeanyKeyBinding *kb = NULL;
744 switch (idx)
746 case 0: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD1; break;
747 case 1: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD2; break;
748 case 2: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD3; break;
751 item = gtk_menu_item_new_with_label(label);
752 gtk_widget_set_tooltip_text(item, tooltip);
753 if (key_idx != -1)
755 kb = keybindings_lookup_item(GEANY_KEY_GROUP_FORMAT, key_idx);
756 if (kb->key > 0)
758 gtk_widget_add_accelerator(item, "activate", gtk_accel_group_new(),
759 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
762 gtk_container_add(GTK_CONTAINER(me), item);
763 gtk_widget_show(item);
764 g_signal_connect(item, "activate", G_CALLBACK(cc_on_custom_command_activate),
765 GINT_TO_POINTER(idx));
769 void tools_create_insert_custom_command_menu_items(void)
771 GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "send_selection_to2_menu"));
772 GtkWidget *item;
773 GList *me_children, *node;
775 /* first clean the menus to be able to rebuild them */
776 me_children = gtk_container_get_children(GTK_CONTAINER(menu_edit));
777 foreach_list(node, me_children)
778 gtk_widget_destroy(GTK_WIDGET(node->data));
779 g_list_free(me_children);
781 if (ui_prefs.custom_commands == NULL || g_strv_length(ui_prefs.custom_commands) == 0)
783 item = gtk_menu_item_new_with_label(_("No custom commands defined."));
784 gtk_container_add(GTK_CONTAINER(menu_edit), item);
785 gtk_widget_set_sensitive(item, FALSE);
786 gtk_widget_show(item);
788 else
790 guint i, len;
791 gint idx = 0;
792 len = g_strv_length(ui_prefs.custom_commands);
793 for (i = 0; i < len; i++)
795 const gchar *label = ui_prefs.custom_commands_labels[i];
797 if (EMPTY(label))
798 label = ui_prefs.custom_commands[i];
799 if (!EMPTY(label)) /* skip empty items */
801 cc_insert_custom_command_items(menu_edit, label, ui_prefs.custom_commands[i], idx);
802 idx++;
807 /* separator and Set menu item */
808 item = gtk_separator_menu_item_new();
809 gtk_container_add(GTK_CONTAINER(menu_edit), item);
810 gtk_widget_show(item);
812 cc_insert_custom_command_items(menu_edit, _("Set Custom Commands"), NULL, -1);
816 /* (stolen from bluefish, thanks)
817 * Returns number of characters, lines and words in the supplied gchar*.
818 * Handles UTF-8 correctly. Input must be properly encoded UTF-8.
819 * Words are defined as any characters grouped, separated with spaces. */
820 static void word_count(gchar *text, guint *chars, guint *lines, guint *words)
822 guint in_word = 0;
823 gunichar utext;
825 if (! text)
826 return; /* politely refuse to operate on NULL */
828 *chars = *words = *lines = 0;
829 while (*text != '\0')
831 (*chars)++;
833 switch (*text)
835 case '\n':
836 (*lines)++;
837 case '\r':
838 case '\f':
839 case '\t':
840 case ' ':
841 case '\v':
842 mb_word_separator:
843 if (in_word)
845 in_word = 0;
846 (*words)++;
848 break;
849 default:
850 utext = g_utf8_get_char_validated(text, 2); /* This might be an utf-8 char */
851 if (g_unichar_isspace(utext)) /* Unicode encoded space? */
852 goto mb_word_separator;
853 if (g_unichar_isgraph(utext)) /* Is this something printable? */
854 in_word = 1;
855 break;
857 /* Even if the current char is 2 bytes, this will iterate correctly. */
858 text = g_utf8_next_char(text);
861 /* Capture last word, if there's no whitespace at the end of the file. */
862 if (in_word)
863 (*words)++;
864 /* We start counting line numbers from 1 */
865 if (*chars > 0)
866 (*lines)++;
870 void tools_word_count(void)
872 GtkWidget *dialog, *label, *vbox, *table;
873 GeanyDocument *doc;
874 guint chars = 0, lines = 0, words = 0;
875 gchar *text;
876 const gchar *range;
878 doc = document_get_current();
879 g_return_if_fail(doc != NULL);
881 dialog = gtk_dialog_new_with_buttons(_("Word Count"), GTK_WINDOW(main_widgets.window),
882 GTK_DIALOG_DESTROY_WITH_PARENT,
883 GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL, NULL);
884 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
885 gtk_widget_set_name(dialog, "GeanyDialog");
887 if (sci_has_selection(doc->editor->sci))
889 text = sci_get_selection_contents(doc->editor->sci);
890 range = _("selection");
892 else
894 text = sci_get_contents(doc->editor->sci, -1);
895 range = _("whole document");
897 word_count(text, &chars, &lines, &words);
898 g_free(text);
900 table = gtk_table_new(4, 2, FALSE);
901 gtk_table_set_row_spacings(GTK_TABLE(table), 5);
902 gtk_table_set_col_spacings(GTK_TABLE(table), 10);
904 label = gtk_label_new(_("Range:"));
905 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1,
906 (GtkAttachOptions) (GTK_FILL),
907 (GtkAttachOptions) (0), 0, 0);
908 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
910 label = gtk_label_new(range);
911 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 0, 1,
912 (GtkAttachOptions) (GTK_FILL),
913 (GtkAttachOptions) (0), 20, 0);
914 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
916 label = gtk_label_new(_("Lines:"));
917 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2,
918 (GtkAttachOptions) (GTK_FILL),
919 (GtkAttachOptions) (0), 0, 0);
920 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
922 text = g_strdup_printf("%d", lines);
923 label = gtk_label_new(text);
924 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 1, 2,
925 (GtkAttachOptions) (GTK_FILL),
926 (GtkAttachOptions) (0), 20, 0);
927 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
928 g_free(text);
930 label = gtk_label_new(_("Words:"));
931 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 2, 3,
932 (GtkAttachOptions) (GTK_FILL),
933 (GtkAttachOptions) (0), 0, 0);
934 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
936 text = g_strdup_printf("%d", words);
937 label = gtk_label_new(text);
938 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 2, 3,
939 (GtkAttachOptions) (GTK_FILL),
940 (GtkAttachOptions) (0), 20, 0);
941 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
942 g_free(text);
944 label = gtk_label_new(_("Characters:"));
945 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 3, 4,
946 (GtkAttachOptions) (GTK_FILL),
947 (GtkAttachOptions) (0), 0, 0);
948 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
950 text = g_strdup_printf("%d", chars);
951 label = gtk_label_new(text);
952 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 3, 4,
953 (GtkAttachOptions) (GTK_FILL),
954 (GtkAttachOptions) (0), 20, 0);
955 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
956 g_free(text);
958 gtk_container_add(GTK_CONTAINER(vbox), table);
960 g_signal_connect(dialog, "response", G_CALLBACK(gtk_widget_destroy), dialog);
961 g_signal_connect(dialog, "delete-event", G_CALLBACK(gtk_widget_destroy), dialog);
963 gtk_widget_show_all(dialog);
968 * color dialog callbacks
970 static void on_color_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
972 switch (response)
974 case GTK_RESPONSE_OK:
975 gtk_widget_hide(ui_widgets.open_colorsel);
976 /* fall through */
977 case GTK_RESPONSE_APPLY:
979 GdkColor color;
980 GeanyDocument *doc = document_get_current();
981 gchar *hex;
982 GtkWidget *colorsel;
984 g_return_if_fail(doc != NULL);
986 colorsel = gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel));
987 gtk_color_selection_get_current_color(GTK_COLOR_SELECTION(colorsel), &color);
989 hex = utils_get_hex_from_color(&color);
990 editor_insert_color(doc->editor, hex);
991 g_free(hex);
992 break;
995 default:
996 gtk_widget_hide(ui_widgets.open_colorsel);
1001 /* This shows the color selection dialog to choose a color. */
1002 void tools_color_chooser(const gchar *color)
1004 GdkColor gc;
1005 GtkWidget *colorsel;
1007 #ifdef G_OS_WIN32
1008 if (interface_prefs.use_native_windows_dialogs)
1010 win32_show_color_dialog(color);
1011 return;
1013 #endif
1015 if (ui_widgets.open_colorsel == NULL)
1017 ui_widgets.open_colorsel = gtk_color_selection_dialog_new(_("Color Chooser"));
1018 gtk_dialog_add_button(GTK_DIALOG(ui_widgets.open_colorsel), GTK_STOCK_APPLY, GTK_RESPONSE_APPLY);
1019 ui_dialog_set_primary_button_order(GTK_DIALOG(ui_widgets.open_colorsel),
1020 GTK_RESPONSE_APPLY, GTK_RESPONSE_CANCEL, GTK_RESPONSE_OK, -1);
1021 gtk_widget_set_name(ui_widgets.open_colorsel, "GeanyDialog");
1022 gtk_window_set_transient_for(GTK_WINDOW(ui_widgets.open_colorsel), GTK_WINDOW(main_widgets.window));
1023 colorsel = gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel));
1024 gtk_color_selection_set_has_palette(GTK_COLOR_SELECTION(colorsel), TRUE);
1026 g_signal_connect(ui_widgets.open_colorsel, "response",
1027 G_CALLBACK(on_color_dialog_response), NULL);
1028 g_signal_connect(ui_widgets.open_colorsel, "delete-event",
1029 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
1031 else
1032 colorsel = gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel));
1033 /* if color is non-NULL set it in the dialog as preselected color */
1034 if (color != NULL && utils_parse_color(color, &gc))
1036 gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(colorsel), &gc);
1037 gtk_color_selection_set_previous_color(GTK_COLOR_SELECTION(colorsel), &gc);
1040 /* We make sure the dialog is visible. */
1041 gtk_window_present(GTK_WINDOW(ui_widgets.open_colorsel));