Fixup various filedefs mappings
[geany-mirror.git] / src / tools.c
blob4e8fe191c403f8fa66eb714cfd3d400b9672e1bd
1 /*
2 * tools.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2011 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2011 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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 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_COUNT
63 /* custom commands code*/
64 struct cc_dialog
66 guint count;
67 GtkWidget *view;
68 GtkTreeViewColumn *edit_column;
69 GtkListStore *store;
70 GtkTreeSelection *selection;
71 GtkWidget *button_add;
72 GtkWidget *button_remove;
73 GtkWidget *button_up;
74 GtkWidget *button_down;
77 static gboolean cc_error_occurred = FALSE;
78 static gboolean cc_reading_finished = FALSE;
79 static GString *cc_buffer;
82 static gboolean cc_exists_command(const gchar *command)
84 gchar *path = g_find_program_in_path(command);
86 g_free(path);
88 return path != NULL;
92 /* update STATUS and TOOLTIP columns according to cmd */
93 static void cc_dialog_update_row_status(GtkListStore *store, GtkTreeIter *iter, const gchar *cmd)
95 GError *err = NULL;
96 const gchar *stock_id = GTK_STOCK_NO;
97 gchar *tooltip = NULL;
98 gint argc;
99 gchar **argv;
101 if (! NZV(cmd))
102 stock_id = GTK_STOCK_YES;
103 else if (g_shell_parse_argv(cmd, &argc, &argv, &err))
105 if (argc > 0 && cc_exists_command(argv[0]))
106 stock_id = GTK_STOCK_YES;
107 else
108 tooltip = g_strdup_printf(_("Invalid command: %s"), _("Command not found"));
109 g_strfreev(argv);
111 else
113 tooltip = g_strdup_printf(_("Invalid command: %s"), err->message);
114 g_error_free(err);
117 gtk_list_store_set(store, iter, CC_COLUMN_STATUS, stock_id, CC_COLUMN_TOOLTIP, tooltip, -1);
118 g_free(tooltip);
122 /* adds a new row for custom command @p idx, or an new empty one if < 0 */
123 static void cc_dialog_add_command(struct cc_dialog *cc, gint idx, gboolean start_editing)
125 GtkTreeIter iter;
126 const gchar *cmd;
127 guint id = cc->count;
129 cmd = (idx >= 0) ? ui_prefs.custom_commands[idx] : NULL;
131 cc->count++;
132 gtk_list_store_append(cc->store, &iter);
133 gtk_list_store_set(cc->store, &iter, CC_COLUMN_ID, id, CC_COLUMN_CMD, cmd, -1);
134 cc_dialog_update_row_status(cc->store, &iter, cmd);
136 if (start_editing)
138 GtkTreePath *path;
140 gtk_widget_grab_focus(cc->view);
141 path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc->store), &iter);
142 gtk_tree_view_set_cursor(GTK_TREE_VIEW(cc->view), path, cc->edit_column, TRUE);
143 gtk_tree_path_free(path);
148 static void cc_on_dialog_add_clicked(GtkButton *button, struct cc_dialog *cc)
150 cc_dialog_add_command(cc, -1, TRUE);
154 static void cc_on_dialog_remove_clicked(GtkButton *button, struct cc_dialog *cc)
156 GtkTreeIter iter;
158 if (gtk_tree_selection_get_selected(cc->selection, NULL, &iter))
159 gtk_list_store_remove(cc->store, &iter);
163 static void cc_on_dialog_move_up_clicked(GtkButton *button, struct cc_dialog *cc)
165 GtkTreeIter iter;
167 if (gtk_tree_selection_get_selected(cc->selection, NULL, &iter))
169 GtkTreePath *path;
170 GtkTreeIter prev;
172 path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc->store), &iter);
173 if (gtk_tree_path_prev(path) &&
174 gtk_tree_model_get_iter(GTK_TREE_MODEL(cc->store), &prev, path))
176 gtk_list_store_move_before(cc->store, &iter, &prev);
178 gtk_tree_path_free(path);
183 static void cc_on_dialog_move_down_clicked(GtkButton *button, struct cc_dialog *cc)
185 GtkTreeIter iter;
187 if (gtk_tree_selection_get_selected(cc->selection, NULL, &iter))
189 GtkTreeIter next = iter;
191 if (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc->store), &next))
192 gtk_list_store_move_after(cc->store, &iter, &next);
197 static gboolean cc_iofunc(GIOChannel *ioc, GIOCondition cond, gpointer data)
199 if (cond & (G_IO_IN | G_IO_PRI))
201 gchar *msg = NULL;
202 GIOStatus rv;
203 GError *err = NULL;
205 cc_buffer = g_string_sized_new(256);
209 rv = g_io_channel_read_line(ioc, &msg, NULL, NULL, &err);
210 if (msg != NULL)
212 g_string_append(cc_buffer, msg);
213 g_free(msg);
215 if (G_UNLIKELY(err != NULL))
217 geany_debug("%s: %s", G_STRFUNC, err->message);
218 g_error_free(err);
219 err = NULL;
221 } while (rv == G_IO_STATUS_NORMAL || rv == G_IO_STATUS_AGAIN);
223 if (G_UNLIKELY(rv != G_IO_STATUS_EOF))
224 { /* Something went wrong? */
225 g_warning("%s: %s\n", G_STRFUNC, "Incomplete command output");
228 return FALSE;
232 static gboolean cc_iofunc_err(GIOChannel *ioc, GIOCondition cond, gpointer data)
234 if (cond & (G_IO_IN | G_IO_PRI))
236 gchar *msg = NULL;
237 GString *str = g_string_sized_new(256);
238 GIOStatus rv;
242 rv = g_io_channel_read_line(ioc, &msg, NULL, NULL, NULL);
243 if (msg != NULL)
245 g_string_append(str, msg);
246 g_free(msg);
248 } while (rv == G_IO_STATUS_NORMAL || rv == G_IO_STATUS_AGAIN);
250 if (NZV(str->str))
252 g_warning("%s: %s\n", (const gchar *) data, str->str);
253 ui_set_statusbar(TRUE,
254 _("The executed custom command returned an error. "
255 "Your selection was not changed. Error message: %s"),
256 str->str);
257 cc_error_occurred = TRUE;
260 g_string_free(str, TRUE);
262 cc_reading_finished = TRUE;
263 return FALSE;
267 static gboolean cc_replace_sel_cb(gpointer user_data)
269 GeanyDocument *doc = user_data;
271 if (! cc_reading_finished)
272 { /* keep this function in the main loop until cc_iofunc_err() has finished */
273 return TRUE;
276 if (! cc_error_occurred && cc_buffer != NULL)
277 { /* Command completed successfully */
278 sci_replace_sel(doc->editor->sci, cc_buffer->str);
279 g_string_free(cc_buffer, TRUE);
280 cc_buffer = NULL;
283 cc_error_occurred = FALSE;
284 cc_reading_finished = FALSE;
286 return FALSE;
290 /* check whether the executed command failed and if so do nothing.
291 * If it returned with a sucessful exit code, replace the selection. */
292 static void cc_exit_cb(GPid child_pid, gint status, gpointer user_data)
294 /* if there was already an error, skip further checks */
295 if (! cc_error_occurred)
297 #ifdef G_OS_UNIX
298 if (WIFEXITED(status))
300 if (WEXITSTATUS(status) != EXIT_SUCCESS)
301 cc_error_occurred = TRUE;
303 else if (WIFSIGNALED(status))
304 { /* the terminating signal: WTERMSIG (status)); */
305 cc_error_occurred = TRUE;
307 else
308 { /* any other failure occured */
309 cc_error_occurred = TRUE;
311 #else
312 cc_error_occurred = ! win32_get_exit_status(child_pid);
313 #endif
315 if (cc_error_occurred)
316 { /* here we are sure cc_error_occurred was set due to an unsuccessful exit code
317 * and so we add an error message */
318 /* TODO maybe include the exit code in the error message */
319 ui_set_statusbar(TRUE,
320 _("The executed custom command exited with an unsuccessful exit code."));
324 g_idle_add(cc_replace_sel_cb, user_data);
325 g_spawn_close_pid(child_pid);
329 /* Executes command (which should include all necessary command line args) and passes the current
330 * selection through the standard input of command. The whole output of command replaces the
331 * current selection. */
332 void tools_execute_custom_command(GeanyDocument *doc, const gchar *command)
334 GError *error = NULL;
335 GPid pid;
336 gchar **argv;
337 gint stdin_fd;
338 gint stdout_fd;
339 gint stderr_fd;
341 g_return_if_fail(doc != NULL && command != NULL);
343 if (! sci_has_selection(doc->editor->sci))
344 editor_select_lines(doc->editor, FALSE);
346 if (!g_shell_parse_argv(command, NULL, &argv, &error))
348 ui_set_statusbar(TRUE, _("Custom command failed: %s"), error->message);
349 g_error_free(error);
350 return;
352 ui_set_statusbar(TRUE, _("Passing data and executing custom command: %s"), command);
354 cc_error_occurred = FALSE;
356 if (g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
357 NULL, NULL, &pid, &stdin_fd, &stdout_fd, &stderr_fd, &error))
359 gchar *sel;
360 gint len, remaining, wrote;
362 if (pid > 0)
363 g_child_watch_add(pid, (GChildWatchFunc) cc_exit_cb, doc);
365 /* use GIOChannel to monitor stdout */
366 utils_set_up_io_channel(stdout_fd, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
367 FALSE, cc_iofunc, NULL);
368 /* copy program's stderr to Geany's stdout to help error tracking */
369 utils_set_up_io_channel(stderr_fd, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
370 FALSE, cc_iofunc_err, (gpointer)command);
372 /* get selection */
373 len = sci_get_selected_text_length(doc->editor->sci);
374 sel = g_malloc0(len + 1);
375 sci_get_selected_text(doc->editor->sci, sel);
377 /* write data to the command */
378 remaining = len - 1;
381 wrote = write(stdin_fd, sel, remaining);
382 if (G_UNLIKELY(wrote < 0))
384 g_warning("%s: %s: %s\n", G_STRFUNC, "Failed sending data to command",
385 g_strerror(errno));
386 break;
388 remaining -= wrote;
389 } while (remaining > 0);
390 close(stdin_fd);
391 g_free(sel);
393 else
395 geany_debug("g_spawn_async_with_pipes() failed: %s", error->message);
396 ui_set_statusbar(TRUE, _("Custom command failed: %s"), error->message);
397 g_error_free(error);
400 g_strfreev(argv);
404 static void cc_dialog_on_command_edited(GtkCellRendererText *renderer, gchar *path, gchar *text,
405 struct cc_dialog *cc)
407 GtkTreeIter iter;
409 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(cc->store), &iter, path);
410 gtk_list_store_set(cc->store, &iter, CC_COLUMN_CMD, text, -1);
411 cc_dialog_update_row_status(cc->store, &iter, text);
415 /* re-compute IDs to reflect the current store state */
416 static void cc_dialog_update_ids(struct cc_dialog *cc)
418 GtkTreeIter iter;
420 cc->count = 1;
421 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc->store), &iter))
422 return;
426 gtk_list_store_set(cc->store, &iter, CC_COLUMN_ID, cc->count, -1);
427 cc->count++;
429 while (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc->store), &iter));
433 /* update sensitiveness of the buttons according to the selection */
434 static void cc_dialog_update_sensitive(struct cc_dialog *cc)
436 GtkTreeIter iter;
437 gboolean has_selection = FALSE;
438 gboolean first_selected = FALSE;
439 gboolean last_selected = FALSE;
441 if ((has_selection = gtk_tree_selection_get_selected(cc->selection, NULL, &iter)))
443 GtkTreePath *path;
444 GtkTreePath *copy;
446 path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc->store), &iter);
447 copy = gtk_tree_path_copy(path);
448 first_selected = ! gtk_tree_path_prev(copy);
449 gtk_tree_path_free(copy);
450 gtk_tree_path_next(path);
451 last_selected = ! gtk_tree_model_get_iter(GTK_TREE_MODEL(cc->store), &iter, path);
452 gtk_tree_path_free(path);
455 gtk_widget_set_sensitive(cc->button_remove, has_selection);
456 gtk_widget_set_sensitive(cc->button_up, has_selection && ! first_selected);
457 gtk_widget_set_sensitive(cc->button_down, has_selection && ! last_selected);
461 static void cc_dialog_on_tree_selection_changed(GtkTreeSelection *selection, struct cc_dialog *cc)
463 cc_dialog_update_sensitive(cc);
467 static void cc_dialog_on_row_inserted(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter,
468 struct cc_dialog *cc)
470 cc_dialog_update_ids(cc);
471 cc_dialog_update_sensitive(cc);
475 static void cc_dialog_on_row_deleted(GtkTreeModel *model, GtkTreePath *path, struct cc_dialog *cc)
477 cc_dialog_update_ids(cc);
478 cc_dialog_update_sensitive(cc);
482 static void cc_dialog_on_rows_reordered(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter,
483 gpointer new_order, struct cc_dialog *cc)
485 cc_dialog_update_ids(cc);
486 cc_dialog_update_sensitive(cc);
490 static void cc_show_dialog_custom_commands(void)
492 GtkWidget *dialog, *label, *vbox, *scroll, *buttonbox;
493 GtkCellRenderer *renderer;
494 GtkTreeViewColumn *column;
495 guint i;
496 struct cc_dialog cc;
498 dialog = gtk_dialog_new_with_buttons(_("Set Custom Commands"), GTK_WINDOW(main_widgets.window),
499 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
500 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
501 gtk_window_set_default_size(GTK_WINDOW(dialog), 300, 300); /* give a reasonable minimal default size */
502 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
503 gtk_box_set_spacing(GTK_BOX(vbox), 6);
504 gtk_widget_set_name(dialog, "GeanyDialog");
506 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."));
507 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
508 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
509 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
511 cc.count = 1;
512 cc.store = gtk_list_store_new(CC_COLUMN_COUNT, G_TYPE_UINT, G_TYPE_STRING, G_TYPE_STRING,
513 G_TYPE_STRING);
514 cc.view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(cc.store));
515 gtk_tree_view_set_tooltip_column(GTK_TREE_VIEW(cc.view), CC_COLUMN_TOOLTIP);
516 gtk_tree_view_set_reorderable(GTK_TREE_VIEW(cc.view), TRUE);
517 cc.selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cc.view));
518 /* ID column */
519 renderer = gtk_cell_renderer_text_new();
520 column = gtk_tree_view_column_new_with_attributes(_("ID"), renderer, "text", CC_COLUMN_ID, NULL);
521 gtk_tree_view_append_column(GTK_TREE_VIEW(cc.view), column);
522 /* command column, holding status and command display */
523 column = gtk_tree_view_column_new();
524 gtk_tree_view_column_set_title(column, _("Command"));
525 renderer = gtk_cell_renderer_pixbuf_new();
526 gtk_tree_view_column_pack_start(column, renderer, FALSE);
527 gtk_tree_view_column_set_attributes(column, renderer, "stock-id", CC_COLUMN_STATUS, NULL);
528 renderer = gtk_cell_renderer_text_new();
529 g_object_set(renderer, "editable", TRUE, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
530 g_signal_connect(renderer, "edited", G_CALLBACK(cc_dialog_on_command_edited), &cc);
531 gtk_tree_view_column_pack_start(column, renderer, TRUE);
532 gtk_tree_view_column_set_attributes(column, renderer, "text", CC_COLUMN_CMD, NULL);
533 cc.edit_column = column;
534 gtk_tree_view_append_column(GTK_TREE_VIEW(cc.view), column);
536 scroll = gtk_scrolled_window_new(NULL, NULL);
537 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_AUTOMATIC,
538 GTK_POLICY_AUTOMATIC);
539 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll), GTK_SHADOW_IN);
540 gtk_container_add(GTK_CONTAINER(scroll), cc.view);
541 gtk_box_pack_start(GTK_BOX(vbox), scroll, TRUE, TRUE, 0);
543 if (ui_prefs.custom_commands != NULL)
545 GtkTreeIter iter;
546 guint len = g_strv_length(ui_prefs.custom_commands);
548 for (i = 0; i < len; i++)
550 if (! NZV(ui_prefs.custom_commands[i]))
551 continue; /* skip empty fields */
553 cc_dialog_add_command(&cc, i, FALSE);
556 /* focus the first row if any */
557 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc.store), &iter))
559 GtkTreePath *path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc.store), &iter);
561 gtk_tree_view_set_cursor(GTK_TREE_VIEW(cc.view), path, cc.edit_column, FALSE);
562 gtk_tree_path_free(path);
566 buttonbox = gtk_hbutton_box_new();
567 gtk_box_set_spacing(GTK_BOX(buttonbox), 6);
568 gtk_box_pack_start(GTK_BOX(vbox), buttonbox, FALSE, FALSE, 0);
569 cc.button_add = gtk_button_new_from_stock(GTK_STOCK_ADD);
570 g_signal_connect(cc.button_add, "clicked", G_CALLBACK(cc_on_dialog_add_clicked), &cc);
571 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_add);
572 cc.button_remove = gtk_button_new_from_stock(GTK_STOCK_REMOVE);
573 g_signal_connect(cc.button_remove, "clicked", G_CALLBACK(cc_on_dialog_remove_clicked), &cc);
574 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_remove);
575 cc.button_up = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
576 g_signal_connect(cc.button_up, "clicked", G_CALLBACK(cc_on_dialog_move_up_clicked), &cc);
577 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_up);
578 cc.button_down = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
579 g_signal_connect(cc.button_down, "clicked", G_CALLBACK(cc_on_dialog_move_down_clicked), &cc);
580 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_down);
582 cc_dialog_update_sensitive(&cc);
584 /* only connect the selection signal when all other cc_dialog fields are set */
585 g_signal_connect(cc.selection, "changed", G_CALLBACK(cc_dialog_on_tree_selection_changed), &cc);
586 g_signal_connect(cc.store, "row-inserted", G_CALLBACK(cc_dialog_on_row_inserted), &cc);
587 g_signal_connect(cc.store, "row-deleted", G_CALLBACK(cc_dialog_on_row_deleted), &cc);
588 g_signal_connect(cc.store, "rows-reordered", G_CALLBACK(cc_dialog_on_rows_reordered), &cc);
590 gtk_widget_show_all(vbox);
592 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
594 GSList *result_list = NULL;
595 gint len = 0;
596 gchar **result = NULL;
597 GtkTreeIter iter;
599 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc.store), &iter))
603 gchar *cmd;
605 gtk_tree_model_get(GTK_TREE_MODEL(cc.store), &iter, CC_COLUMN_CMD, &cmd, -1);
606 if (NZV(cmd))
608 result_list = g_slist_prepend(result_list, cmd);
609 len++;
611 else
612 g_free(cmd);
614 while (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc.store), &iter));
616 result_list = g_slist_reverse(result_list);
617 /* create a new null-terminated array but only if there is any commands defined */
618 if (len > 0)
620 gint j = 0;
621 GSList *node;
623 result = g_new(gchar*, len + 1);
624 foreach_list(node, result_list)
626 result[j] = (gchar*) node->data;
627 j++;
629 result[j] = NULL; /* null-terminate the array */
631 /* set the new array */
632 g_strfreev(ui_prefs.custom_commands);
633 ui_prefs.custom_commands = result;
634 /* rebuild the menu items */
635 tools_create_insert_custom_command_menu_items();
637 g_slist_free(result_list);
639 gtk_widget_destroy(dialog);
643 static void cc_on_custom_command_activate(GtkMenuItem *menuitem, gpointer user_data)
645 GeanyDocument *doc = document_get_current();
646 gint command_idx;
648 g_return_if_fail(doc != NULL);
650 command_idx = GPOINTER_TO_INT(user_data);
652 if (ui_prefs.custom_commands == NULL ||
653 command_idx < 0 || command_idx > (gint) g_strv_length(ui_prefs.custom_commands))
655 cc_show_dialog_custom_commands();
656 return;
659 /* send it through the command and when the command returned the output the current selection
660 * will be replaced */
661 tools_execute_custom_command(doc, ui_prefs.custom_commands[command_idx]);
665 static void cc_insert_custom_command_items(GtkMenu *me, gchar *label, gint idx)
667 GtkWidget *item;
668 gint key_idx = -1;
669 GeanyKeyBinding *kb = NULL;
671 switch (idx)
673 case 0: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD1; break;
674 case 1: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD2; break;
675 case 2: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD3; break;
678 item = gtk_menu_item_new_with_label(label);
679 if (key_idx != -1)
681 kb = keybindings_lookup_item(GEANY_KEY_GROUP_FORMAT, key_idx);
682 gtk_widget_add_accelerator(item, "activate", gtk_accel_group_new(),
683 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
685 gtk_container_add(GTK_CONTAINER(me), item);
686 gtk_widget_show(item);
687 g_signal_connect(item, "activate", G_CALLBACK(cc_on_custom_command_activate),
688 GINT_TO_POINTER(idx));
692 void tools_create_insert_custom_command_menu_items(void)
694 GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "send_selection_to2_menu"));
695 GtkWidget *item;
696 GList *me_children, *node;
698 /* first clean the menus to be able to rebuild them */
699 me_children = gtk_container_get_children(GTK_CONTAINER(menu_edit));
700 foreach_list(node, me_children)
701 gtk_widget_destroy(GTK_WIDGET(node->data));
702 g_list_free(me_children);
704 if (ui_prefs.custom_commands == NULL || g_strv_length(ui_prefs.custom_commands) == 0)
706 item = gtk_menu_item_new_with_label(_("No custom commands defined."));
707 gtk_container_add(GTK_CONTAINER(menu_edit), item);
708 gtk_widget_set_sensitive(item, FALSE);
709 gtk_widget_show(item);
711 else
713 guint i, len;
714 gint idx = 0;
715 len = g_strv_length(ui_prefs.custom_commands);
716 for (i = 0; i < len; i++)
718 if (ui_prefs.custom_commands[i][0] != '\0') /* skip empty fields */
720 cc_insert_custom_command_items(menu_edit, ui_prefs.custom_commands[i], idx);
721 idx++;
726 /* separator and Set menu item */
727 item = gtk_separator_menu_item_new();
728 gtk_container_add(GTK_CONTAINER(menu_edit), item);
729 gtk_widget_show(item);
731 cc_insert_custom_command_items(menu_edit, _("Set Custom Commands"), -1);
735 /* (stolen from bluefish, thanks)
736 * Returns number of characters, lines and words in the supplied gchar*.
737 * Handles UTF-8 correctly. Input must be properly encoded UTF-8.
738 * Words are defined as any characters grouped, separated with spaces. */
739 static void word_count(gchar *text, guint *chars, guint *lines, guint *words)
741 guint in_word = 0;
742 gunichar utext;
744 if (! text)
745 return; /* politely refuse to operate on NULL */
747 *chars = *words = *lines = 0;
748 while (*text != '\0')
750 (*chars)++;
752 switch (*text)
754 case '\n':
755 (*lines)++;
756 case '\r':
757 case '\f':
758 case '\t':
759 case ' ':
760 case '\v':
761 mb_word_separator:
762 if (in_word)
764 in_word = 0;
765 (*words)++;
767 break;
768 default:
769 utext = g_utf8_get_char_validated(text, 2); /* This might be an utf-8 char */
770 if (g_unichar_isspace(utext)) /* Unicode encoded space? */
771 goto mb_word_separator;
772 if (g_unichar_isgraph(utext)) /* Is this something printable? */
773 in_word = 1;
774 break;
776 /* Even if the current char is 2 bytes, this will iterate correctly. */
777 text = g_utf8_next_char(text);
780 /* Capture last word, if there's no whitespace at the end of the file. */
781 if (in_word)
782 (*words)++;
783 /* We start counting line numbers from 1 */
784 if (*chars > 0)
785 (*lines)++;
789 void tools_word_count(void)
791 GtkWidget *dialog, *label, *vbox, *table;
792 GeanyDocument *doc;
793 guint chars = 0, lines = 0, words = 0;
794 gchar *text;
795 const gchar *range;
797 doc = document_get_current();
798 g_return_if_fail(doc != NULL);
800 dialog = gtk_dialog_new_with_buttons(_("Word Count"), GTK_WINDOW(main_widgets.window),
801 GTK_DIALOG_DESTROY_WITH_PARENT,
802 GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL, NULL);
803 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
804 gtk_widget_set_name(dialog, "GeanyDialog");
806 if (sci_has_selection(doc->editor->sci))
808 text = g_malloc0(sci_get_selected_text_length(doc->editor->sci) + 1);
809 sci_get_selected_text(doc->editor->sci, text);
810 range = _("selection");
812 else
814 text = g_malloc(sci_get_length(doc->editor->sci) + 1);
815 sci_get_text(doc->editor->sci, sci_get_length(doc->editor->sci) + 1 , text);
816 range = _("whole document");
818 word_count(text, &chars, &lines, &words);
819 g_free(text);
821 table = gtk_table_new(4, 2, FALSE);
822 gtk_table_set_row_spacings(GTK_TABLE(table), 5);
823 gtk_table_set_col_spacings(GTK_TABLE(table), 10);
825 label = gtk_label_new(_("Range:"));
826 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1,
827 (GtkAttachOptions) (GTK_FILL),
828 (GtkAttachOptions) (0), 0, 0);
829 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
831 label = gtk_label_new(range);
832 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 0, 1,
833 (GtkAttachOptions) (GTK_FILL),
834 (GtkAttachOptions) (0), 20, 0);
835 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
837 label = gtk_label_new(_("Lines:"));
838 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2,
839 (GtkAttachOptions) (GTK_FILL),
840 (GtkAttachOptions) (0), 0, 0);
841 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
843 text = g_strdup_printf("%d", lines);
844 label = gtk_label_new(text);
845 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 1, 2,
846 (GtkAttachOptions) (GTK_FILL),
847 (GtkAttachOptions) (0), 20, 0);
848 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
849 g_free(text);
851 label = gtk_label_new(_("Words:"));
852 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 2, 3,
853 (GtkAttachOptions) (GTK_FILL),
854 (GtkAttachOptions) (0), 0, 0);
855 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
857 text = g_strdup_printf("%d", words);
858 label = gtk_label_new(text);
859 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 2, 3,
860 (GtkAttachOptions) (GTK_FILL),
861 (GtkAttachOptions) (0), 20, 0);
862 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
863 g_free(text);
865 label = gtk_label_new(_("Characters:"));
866 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 3, 4,
867 (GtkAttachOptions) (GTK_FILL),
868 (GtkAttachOptions) (0), 0, 0);
869 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
871 text = g_strdup_printf("%d", chars);
872 label = gtk_label_new(text);
873 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 3, 4,
874 (GtkAttachOptions) (GTK_FILL),
875 (GtkAttachOptions) (0), 20, 0);
876 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
877 g_free(text);
879 gtk_container_add(GTK_CONTAINER(vbox), table);
881 g_signal_connect(dialog, "response", G_CALLBACK(gtk_widget_destroy), dialog);
882 g_signal_connect(dialog, "delete-event", G_CALLBACK(gtk_widget_destroy), dialog);
884 gtk_widget_show_all(dialog);
889 * color dialog callbacks
891 #ifndef G_OS_WIN32
892 static void
893 on_color_cancel_button_clicked(GtkButton *button, gpointer user_data)
895 gtk_widget_hide(ui_widgets.open_colorsel);
899 static void
900 on_color_ok_button_clicked(GtkButton *button, gpointer user_data)
902 GdkColor color;
903 GeanyDocument *doc = document_get_current();
904 gchar *hex;
906 gtk_widget_hide(ui_widgets.open_colorsel);
907 g_return_if_fail(doc != NULL);
909 gtk_color_selection_get_current_color(
910 GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel)->colorsel), &color);
912 hex = utils_get_hex_from_color(&color);
913 editor_insert_color(doc->editor, hex);
914 g_free(hex);
916 #endif
919 /* This shows the color selection dialog to choose a color. */
920 void tools_color_chooser(const gchar *color)
922 #ifdef G_OS_WIN32
923 win32_show_color_dialog(color);
924 #else
925 gchar *c = (gchar*) color;
927 if (ui_widgets.open_colorsel == NULL)
929 ui_widgets.open_colorsel = gtk_color_selection_dialog_new(_("Color Chooser"));
930 gtk_widget_set_name(ui_widgets.open_colorsel, "GeanyDialog");
931 gtk_window_set_transient_for(GTK_WINDOW(ui_widgets.open_colorsel), GTK_WINDOW(main_widgets.window));
932 gtk_color_selection_set_has_palette(
933 GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel)->colorsel), TRUE);
935 g_signal_connect(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel)->cancel_button, "clicked",
936 G_CALLBACK(on_color_cancel_button_clicked), NULL);
937 g_signal_connect(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel)->ok_button, "clicked",
938 G_CALLBACK(on_color_ok_button_clicked), NULL);
939 g_signal_connect(ui_widgets.open_colorsel, "delete-event",
940 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
942 /* if color is non-NULL set it in the dialog as preselected color */
943 if (c != NULL && (c[0] == '0' || c[0] == '#'))
945 GdkColor gc;
947 if (c[0] == '0' && c[1] == 'x')
948 { /* we have a string of the format "0x00ff00" and we need it to "#00ff00" */
949 c[1] = '#';
950 c++;
952 gdk_color_parse(c, &gc);
953 gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(
954 GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel)->colorsel), &gc);
955 gtk_color_selection_set_previous_color(GTK_COLOR_SELECTION(
956 GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel)->colorsel), &gc);
959 /* We make sure the dialog is visible. */
960 gtk_window_present(GTK_WINDOW(ui_widgets.open_colorsel));
961 #endif