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.
35 # include <sys/types.h>
36 # include <sys/wait.h>
44 #include "sciwrappers.h"
47 #include "msgwindow.h"
48 #include "keybindings.h"
49 #include "templates.h"
64 /* custom commands code*/
69 GtkTreeViewColumn
*edit_column
;
71 GtkTreeSelection
*selection
;
72 GtkWidget
*button_add
;
73 GtkWidget
*button_remove
;
75 GtkWidget
*button_down
;
78 /* data required by the custom command callbacks */
81 const gchar
*command
; /* command launched */
82 GeanyDocument
*doc
; /* document in which replace the selection */
83 GString
*buffer
; /* buffer holding stdout content, or NULL */
84 gboolean error
; /* whether and error occurred */
85 gboolean finished
; /* whether the command has finished */
89 static gboolean
cc_exists_command(const gchar
*command
)
91 gchar
*path
= g_find_program_in_path(command
);
99 /* update STATUS and TOOLTIP columns according to cmd */
100 static void cc_dialog_update_row_status(GtkListStore
*store
, GtkTreeIter
*iter
, const gchar
*cmd
)
103 const gchar
*stock_id
= GTK_STOCK_NO
;
104 gchar
*tooltip
= NULL
;
109 stock_id
= GTK_STOCK_YES
;
110 else if (g_shell_parse_argv(cmd
, &argc
, &argv
, &err
))
112 if (argc
> 0 && cc_exists_command(argv
[0]))
113 stock_id
= GTK_STOCK_YES
;
115 tooltip
= g_strdup_printf(_("Invalid command: %s"), _("Command not found"));
120 tooltip
= g_strdup_printf(_("Invalid command: %s"), err
->message
);
124 gtk_list_store_set(store
, iter
, CC_COLUMN_STATUS
, stock_id
, CC_COLUMN_TOOLTIP
, tooltip
, -1);
129 /* adds a new row for custom command @p idx, or an new empty one if < 0 */
130 static void cc_dialog_add_command(struct cc_dialog
*cc
, gint idx
, gboolean start_editing
)
133 const gchar
*cmd
= NULL
;
134 const gchar
*label
= NULL
;
135 guint id
= cc
->count
;
139 cmd
= ui_prefs
.custom_commands
[idx
];
140 label
= ui_prefs
.custom_commands_labels
[idx
];
144 gtk_list_store_append(cc
->store
, &iter
);
145 gtk_list_store_set(cc
->store
, &iter
, CC_COLUMN_ID
, id
, CC_COLUMN_CMD
, cmd
, CC_COLUMN_LABEL
, label
, -1);
146 cc_dialog_update_row_status(cc
->store
, &iter
, cmd
);
152 gtk_widget_grab_focus(cc
->view
);
153 path
= gtk_tree_model_get_path(GTK_TREE_MODEL(cc
->store
), &iter
);
154 gtk_tree_view_set_cursor(GTK_TREE_VIEW(cc
->view
), path
, cc
->edit_column
, TRUE
);
155 gtk_tree_path_free(path
);
160 static void cc_on_dialog_add_clicked(GtkButton
*button
, struct cc_dialog
*cc
)
162 cc_dialog_add_command(cc
, -1, TRUE
);
166 static void cc_on_dialog_remove_clicked(GtkButton
*button
, struct cc_dialog
*cc
)
170 if (gtk_tree_selection_get_selected(cc
->selection
, NULL
, &iter
))
171 gtk_list_store_remove(cc
->store
, &iter
);
175 static void cc_on_dialog_move_up_clicked(GtkButton
*button
, struct cc_dialog
*cc
)
179 if (gtk_tree_selection_get_selected(cc
->selection
, NULL
, &iter
))
184 path
= gtk_tree_model_get_path(GTK_TREE_MODEL(cc
->store
), &iter
);
185 if (gtk_tree_path_prev(path
) &&
186 gtk_tree_model_get_iter(GTK_TREE_MODEL(cc
->store
), &prev
, path
))
188 gtk_list_store_move_before(cc
->store
, &iter
, &prev
);
190 gtk_tree_path_free(path
);
195 static void cc_on_dialog_move_down_clicked(GtkButton
*button
, struct cc_dialog
*cc
)
199 if (gtk_tree_selection_get_selected(cc
->selection
, NULL
, &iter
))
201 GtkTreeIter next
= iter
;
203 if (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc
->store
), &next
))
204 gtk_list_store_move_after(cc
->store
, &iter
, &next
);
209 static gboolean
cc_iofunc(GIOChannel
*ioc
, GIOCondition cond
, gpointer user_data
)
211 struct cc_data
*data
= user_data
;
213 if (cond
& (G_IO_IN
| G_IO_PRI
))
220 data
->buffer
= g_string_sized_new(256);
224 rv
= g_io_channel_read_line(ioc
, &msg
, NULL
, NULL
, &err
);
227 g_string_append(data
->buffer
, msg
);
230 if (G_UNLIKELY(err
!= NULL
))
232 geany_debug("%s: %s", G_STRFUNC
, err
->message
);
236 } while (rv
== G_IO_STATUS_NORMAL
|| rv
== G_IO_STATUS_AGAIN
);
238 if (G_UNLIKELY(rv
!= G_IO_STATUS_EOF
))
239 { /* Something went wrong? */
240 g_warning("%s: %s\n", G_STRFUNC
, "Incomplete command output");
247 static gboolean
cc_iofunc_err(GIOChannel
*ioc
, GIOCondition cond
, gpointer user_data
)
249 struct cc_data
*data
= user_data
;
251 if (cond
& (G_IO_IN
| G_IO_PRI
))
254 GString
*str
= g_string_sized_new(256);
259 rv
= g_io_channel_read_line(ioc
, &msg
, NULL
, NULL
, NULL
);
262 g_string_append(str
, msg
);
265 } while (rv
== G_IO_STATUS_NORMAL
|| rv
== G_IO_STATUS_AGAIN
);
267 if (!EMPTY(str
->str
))
269 g_warning("%s: %s\n", data
->command
, str
->str
);
270 ui_set_statusbar(TRUE
,
271 _("The executed custom command returned an error. "
272 "Your selection was not changed. Error message: %s"),
277 g_string_free(str
, TRUE
);
279 data
->finished
= TRUE
;
284 static gboolean
cc_replace_sel_cb(gpointer user_data
)
286 struct cc_data
*data
= user_data
;
288 if (! data
->finished
)
289 { /* keep this function in the main loop until cc_iofunc_err() has finished */
293 if (! data
->error
&& data
->buffer
!= NULL
)
294 { /* Command completed successfully */
295 sci_replace_sel(data
->doc
->editor
->sci
, data
->buffer
->str
);
299 g_string_free(data
->buffer
, TRUE
);
300 g_slice_free1(sizeof *data
, data
);
306 /* check whether the executed command failed and if so do nothing.
307 * If it returned with a sucessful exit code, replace the selection. */
308 static void cc_exit_cb(GPid child_pid
, gint status
, gpointer user_data
)
310 struct cc_data
*data
= user_data
;
312 /* if there was already an error, skip further checks */
316 if (WIFEXITED(status
))
318 if (WEXITSTATUS(status
) != EXIT_SUCCESS
)
321 else if (WIFSIGNALED(status
))
322 { /* the terminating signal: WTERMSIG (status)); */
326 { /* any other failure occured */
330 data
->error
= ! win32_get_exit_status(child_pid
);
334 { /* here we are sure data->error was set due to an unsuccessful exit code
335 * and so we add an error message */
336 /* TODO maybe include the exit code in the error message */
337 ui_set_statusbar(TRUE
,
338 _("The executed custom command exited with an unsuccessful exit code."));
342 g_idle_add(cc_replace_sel_cb
, data
);
343 g_spawn_close_pid(child_pid
);
347 /* Executes command (which should include all necessary command line args) and passes the current
348 * selection through the standard input of command. The whole output of command replaces the
349 * current selection. */
350 void tools_execute_custom_command(GeanyDocument
*doc
, const gchar
*command
)
352 GError
*error
= NULL
;
359 g_return_if_fail(doc
!= NULL
&& command
!= NULL
);
361 if (! sci_has_selection(doc
->editor
->sci
))
362 editor_select_lines(doc
->editor
, FALSE
);
364 if (!g_shell_parse_argv(command
, NULL
, &argv
, &error
))
366 ui_set_statusbar(TRUE
, _("Custom command failed: %s"), error
->message
);
370 ui_set_statusbar(TRUE
, _("Passing data and executing custom command: %s"), command
);
372 if (g_spawn_async_with_pipes(NULL
, argv
, NULL
, G_SPAWN_SEARCH_PATH
| G_SPAWN_DO_NOT_REAP_CHILD
,
373 NULL
, NULL
, &pid
, &stdin_fd
, &stdout_fd
, &stderr_fd
, &error
))
376 gint remaining
, wrote
;
377 struct cc_data
*data
= g_slice_alloc(sizeof *data
);
380 data
->finished
= FALSE
;
383 data
->command
= command
;
385 g_child_watch_add(pid
, cc_exit_cb
, data
);
387 /* use GIOChannel to monitor stdout */
388 utils_set_up_io_channel(stdout_fd
, G_IO_IN
| G_IO_PRI
| G_IO_ERR
| G_IO_HUP
| G_IO_NVAL
,
389 FALSE
, cc_iofunc
, data
);
390 /* copy program's stderr to Geany's stdout to help error tracking */
391 utils_set_up_io_channel(stderr_fd
, G_IO_IN
| G_IO_PRI
| G_IO_ERR
| G_IO_HUP
| G_IO_NVAL
,
392 FALSE
, cc_iofunc_err
, data
);
395 sel
= sci_get_selection_contents(doc
->editor
->sci
);
397 /* write data to the command */
398 remaining
= strlen(sel
);
401 wrote
= write(stdin_fd
, sel
, remaining
);
402 if (G_UNLIKELY(wrote
< 0))
404 g_warning("%s: %s: %s\n", G_STRFUNC
, "Failed sending data to command",
409 } while (remaining
> 0);
415 geany_debug("g_spawn_async_with_pipes() failed: %s", error
->message
);
416 ui_set_statusbar(TRUE
, _("Custom command failed: %s"), error
->message
);
424 static void cc_dialog_on_command_edited(GtkCellRendererText
*renderer
, gchar
*path
, gchar
*text
,
425 struct cc_dialog
*cc
)
429 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(cc
->store
), &iter
, path
);
430 gtk_list_store_set(cc
->store
, &iter
, CC_COLUMN_CMD
, text
, -1);
431 cc_dialog_update_row_status(cc
->store
, &iter
, text
);
435 static void cc_dialog_on_label_edited(GtkCellRendererText
*renderer
, gchar
*path
, gchar
*text
,
436 struct cc_dialog
*cc
)
440 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(cc
->store
), &iter
, path
);
441 gtk_list_store_set(cc
->store
, &iter
, CC_COLUMN_LABEL
, text
, -1);
445 /* re-compute IDs to reflect the current store state */
446 static void cc_dialog_update_ids(struct cc_dialog
*cc
)
451 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc
->store
), &iter
))
456 gtk_list_store_set(cc
->store
, &iter
, CC_COLUMN_ID
, cc
->count
, -1);
459 while (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc
->store
), &iter
));
463 /* update sensitiveness of the buttons according to the selection */
464 static void cc_dialog_update_sensitive(struct cc_dialog
*cc
)
467 gboolean has_selection
= FALSE
;
468 gboolean first_selected
= FALSE
;
469 gboolean last_selected
= FALSE
;
471 if ((has_selection
= gtk_tree_selection_get_selected(cc
->selection
, NULL
, &iter
)))
476 path
= gtk_tree_model_get_path(GTK_TREE_MODEL(cc
->store
), &iter
);
477 copy
= gtk_tree_path_copy(path
);
478 first_selected
= ! gtk_tree_path_prev(copy
);
479 gtk_tree_path_free(copy
);
480 gtk_tree_path_next(path
);
481 last_selected
= ! gtk_tree_model_get_iter(GTK_TREE_MODEL(cc
->store
), &iter
, path
);
482 gtk_tree_path_free(path
);
485 gtk_widget_set_sensitive(cc
->button_remove
, has_selection
);
486 gtk_widget_set_sensitive(cc
->button_up
, has_selection
&& ! first_selected
);
487 gtk_widget_set_sensitive(cc
->button_down
, has_selection
&& ! last_selected
);
491 static void cc_dialog_on_tree_selection_changed(GtkTreeSelection
*selection
, struct cc_dialog
*cc
)
493 cc_dialog_update_sensitive(cc
);
497 static void cc_dialog_on_row_inserted(GtkTreeModel
*model
, GtkTreePath
*path
, GtkTreeIter
*iter
,
498 struct cc_dialog
*cc
)
500 cc_dialog_update_ids(cc
);
501 cc_dialog_update_sensitive(cc
);
505 static void cc_dialog_on_row_deleted(GtkTreeModel
*model
, GtkTreePath
*path
, struct cc_dialog
*cc
)
507 cc_dialog_update_ids(cc
);
508 cc_dialog_update_sensitive(cc
);
512 static void cc_dialog_on_rows_reordered(GtkTreeModel
*model
, GtkTreePath
*path
, GtkTreeIter
*iter
,
513 gpointer new_order
, struct cc_dialog
*cc
)
515 cc_dialog_update_ids(cc
);
516 cc_dialog_update_sensitive(cc
);
520 static void cc_show_dialog_custom_commands(void)
522 GtkWidget
*dialog
, *label
, *vbox
, *scroll
, *buttonbox
;
523 GtkCellRenderer
*renderer
;
524 GtkTreeViewColumn
*column
;
528 dialog
= gtk_dialog_new_with_buttons(_("Set Custom Commands"), GTK_WINDOW(main_widgets
.window
),
529 GTK_DIALOG_DESTROY_WITH_PARENT
, GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
530 GTK_STOCK_OK
, GTK_RESPONSE_ACCEPT
, NULL
);
531 gtk_window_set_default_size(GTK_WINDOW(dialog
), 300, 300); /* give a reasonable minimal default size */
532 vbox
= ui_dialog_vbox_new(GTK_DIALOG(dialog
));
533 gtk_box_set_spacing(GTK_BOX(vbox
), 6);
534 gtk_widget_set_name(dialog
, "GeanyDialog");
536 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."));
537 gtk_label_set_line_wrap(GTK_LABEL(label
), TRUE
);
538 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
539 gtk_box_pack_start(GTK_BOX(vbox
), label
, FALSE
, FALSE
, 0);
542 cc
.store
= gtk_list_store_new(CC_COLUMN_COUNT
, G_TYPE_UINT
, G_TYPE_STRING
, G_TYPE_STRING
,
543 G_TYPE_STRING
, G_TYPE_STRING
);
544 cc
.view
= gtk_tree_view_new_with_model(GTK_TREE_MODEL(cc
.store
));
545 gtk_tree_view_set_tooltip_column(GTK_TREE_VIEW(cc
.view
), CC_COLUMN_TOOLTIP
);
546 gtk_tree_view_set_reorderable(GTK_TREE_VIEW(cc
.view
), TRUE
);
547 cc
.selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(cc
.view
));
549 renderer
= gtk_cell_renderer_text_new();
550 column
= gtk_tree_view_column_new_with_attributes(_("ID"), renderer
, "text", CC_COLUMN_ID
, NULL
);
551 gtk_tree_view_append_column(GTK_TREE_VIEW(cc
.view
), column
);
552 /* command column, holding status and command display */
553 column
= g_object_new(GTK_TYPE_TREE_VIEW_COLUMN
, "title", _("Command"), "expand", TRUE
, "resizable", TRUE
, NULL
);
554 renderer
= gtk_cell_renderer_pixbuf_new();
555 gtk_tree_view_column_pack_start(column
, renderer
, FALSE
);
556 gtk_tree_view_column_set_attributes(column
, renderer
, "stock-id", CC_COLUMN_STATUS
, NULL
);
557 renderer
= gtk_cell_renderer_text_new();
558 g_object_set(renderer
, "editable", TRUE
, "ellipsize", PANGO_ELLIPSIZE_END
, NULL
);
559 g_signal_connect(renderer
, "edited", G_CALLBACK(cc_dialog_on_command_edited
), &cc
);
560 gtk_tree_view_column_pack_start(column
, renderer
, TRUE
);
561 gtk_tree_view_column_set_attributes(column
, renderer
, "text", CC_COLUMN_CMD
, NULL
);
562 cc
.edit_column
= column
;
563 gtk_tree_view_append_column(GTK_TREE_VIEW(cc
.view
), column
);
565 renderer
= gtk_cell_renderer_text_new();
566 g_object_set(renderer
, "editable", TRUE
, "ellipsize", PANGO_ELLIPSIZE_END
, NULL
);
567 g_signal_connect(renderer
, "edited", G_CALLBACK(cc_dialog_on_label_edited
), &cc
);
568 column
= gtk_tree_view_column_new_with_attributes(_("Label"), renderer
, "text", CC_COLUMN_LABEL
, NULL
);
569 g_object_set(column
, "expand", TRUE
, "resizable", TRUE
, NULL
);
570 gtk_tree_view_append_column(GTK_TREE_VIEW(cc
.view
), column
);
572 scroll
= gtk_scrolled_window_new(NULL
, NULL
);
573 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll
), GTK_POLICY_AUTOMATIC
,
574 GTK_POLICY_AUTOMATIC
);
575 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll
), GTK_SHADOW_IN
);
576 gtk_container_add(GTK_CONTAINER(scroll
), cc
.view
);
577 gtk_box_pack_start(GTK_BOX(vbox
), scroll
, TRUE
, TRUE
, 0);
579 if (ui_prefs
.custom_commands
!= NULL
)
582 guint len
= g_strv_length(ui_prefs
.custom_commands
);
584 for (i
= 0; i
< len
; i
++)
586 if (EMPTY(ui_prefs
.custom_commands
[i
]))
587 continue; /* skip empty fields */
589 cc_dialog_add_command(&cc
, i
, FALSE
);
592 /* focus the first row if any */
593 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc
.store
), &iter
))
595 GtkTreePath
*path
= gtk_tree_model_get_path(GTK_TREE_MODEL(cc
.store
), &iter
);
597 gtk_tree_view_set_cursor(GTK_TREE_VIEW(cc
.view
), path
, cc
.edit_column
, FALSE
);
598 gtk_tree_path_free(path
);
602 buttonbox
= gtk_hbutton_box_new();
603 gtk_box_set_spacing(GTK_BOX(buttonbox
), 6);
604 gtk_box_pack_start(GTK_BOX(vbox
), buttonbox
, FALSE
, FALSE
, 0);
605 cc
.button_add
= gtk_button_new_from_stock(GTK_STOCK_ADD
);
606 g_signal_connect(cc
.button_add
, "clicked", G_CALLBACK(cc_on_dialog_add_clicked
), &cc
);
607 gtk_container_add(GTK_CONTAINER(buttonbox
), cc
.button_add
);
608 cc
.button_remove
= gtk_button_new_from_stock(GTK_STOCK_REMOVE
);
609 g_signal_connect(cc
.button_remove
, "clicked", G_CALLBACK(cc_on_dialog_remove_clicked
), &cc
);
610 gtk_container_add(GTK_CONTAINER(buttonbox
), cc
.button_remove
);
611 cc
.button_up
= gtk_button_new_from_stock(GTK_STOCK_GO_UP
);
612 g_signal_connect(cc
.button_up
, "clicked", G_CALLBACK(cc_on_dialog_move_up_clicked
), &cc
);
613 gtk_container_add(GTK_CONTAINER(buttonbox
), cc
.button_up
);
614 cc
.button_down
= gtk_button_new_from_stock(GTK_STOCK_GO_DOWN
);
615 g_signal_connect(cc
.button_down
, "clicked", G_CALLBACK(cc_on_dialog_move_down_clicked
), &cc
);
616 gtk_container_add(GTK_CONTAINER(buttonbox
), cc
.button_down
);
618 cc_dialog_update_sensitive(&cc
);
620 /* only connect the selection signal when all other cc_dialog fields are set */
621 g_signal_connect(cc
.selection
, "changed", G_CALLBACK(cc_dialog_on_tree_selection_changed
), &cc
);
622 g_signal_connect(cc
.store
, "row-inserted", G_CALLBACK(cc_dialog_on_row_inserted
), &cc
);
623 g_signal_connect(cc
.store
, "row-deleted", G_CALLBACK(cc_dialog_on_row_deleted
), &cc
);
624 g_signal_connect(cc
.store
, "rows-reordered", G_CALLBACK(cc_dialog_on_rows_reordered
), &cc
);
626 gtk_widget_show_all(vbox
);
628 if (gtk_dialog_run(GTK_DIALOG(dialog
)) == GTK_RESPONSE_ACCEPT
)
630 GSList
*cmd_list
= NULL
;
631 GSList
*lbl_list
= NULL
;
633 gchar
**commands
= NULL
;
634 gchar
**labels
= NULL
;
637 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc
.store
), &iter
))
644 gtk_tree_model_get(GTK_TREE_MODEL(cc
.store
), &iter
, CC_COLUMN_CMD
, &cmd
, CC_COLUMN_LABEL
, &lbl
, -1);
647 cmd_list
= g_slist_prepend(cmd_list
, cmd
);
648 lbl_list
= g_slist_prepend(lbl_list
, lbl
);
657 while (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc
.store
), &iter
));
659 cmd_list
= g_slist_reverse(cmd_list
);
660 lbl_list
= g_slist_reverse(lbl_list
);
661 /* create a new null-terminated array but only if there is any commands defined */
665 GSList
*cmd_node
, *lbl_node
;
667 commands
= g_new(gchar
*, len
+ 1);
668 labels
= g_new(gchar
*, len
+ 1);
669 /* walk commands and labels lists */
670 for (cmd_node
= cmd_list
, lbl_node
= lbl_list
; cmd_node
!= NULL
; cmd_node
= cmd_node
->next
, lbl_node
= lbl_node
->next
)
672 commands
[j
] = (gchar
*) cmd_node
->data
;
673 labels
[j
] = (gchar
*) lbl_node
->data
;
676 /* null-terminate the arrays */
680 /* set the new arrays */
681 g_strfreev(ui_prefs
.custom_commands
);
682 ui_prefs
.custom_commands
= commands
;
683 g_strfreev(ui_prefs
.custom_commands_labels
);
684 ui_prefs
.custom_commands_labels
= labels
;
685 /* rebuild the menu items */
686 tools_create_insert_custom_command_menu_items();
688 g_slist_free(cmd_list
);
689 g_slist_free(lbl_list
);
691 gtk_widget_destroy(dialog
);
695 static void cc_on_custom_command_activate(GtkMenuItem
*menuitem
, gpointer user_data
)
697 GeanyDocument
*doc
= document_get_current();
700 g_return_if_fail(doc
!= NULL
);
702 command_idx
= GPOINTER_TO_INT(user_data
);
704 if (ui_prefs
.custom_commands
== NULL
||
705 command_idx
< 0 || command_idx
> (gint
) g_strv_length(ui_prefs
.custom_commands
))
707 cc_show_dialog_custom_commands();
711 /* send it through the command and when the command returned the output the current selection
712 * will be replaced */
713 tools_execute_custom_command(doc
, ui_prefs
.custom_commands
[command_idx
]);
717 static void cc_insert_custom_command_items(GtkMenu
*me
, const gchar
*label
, const gchar
*tooltip
, gint idx
)
721 GeanyKeyBinding
*kb
= NULL
;
725 case 0: key_idx
= GEANY_KEYS_FORMAT_SENDTOCMD1
; break;
726 case 1: key_idx
= GEANY_KEYS_FORMAT_SENDTOCMD2
; break;
727 case 2: key_idx
= GEANY_KEYS_FORMAT_SENDTOCMD3
; break;
730 item
= gtk_menu_item_new_with_label(label
);
731 gtk_widget_set_tooltip_text(item
, tooltip
);
734 kb
= keybindings_lookup_item(GEANY_KEY_GROUP_FORMAT
, key_idx
);
735 gtk_widget_add_accelerator(item
, "activate", gtk_accel_group_new(),
736 kb
->key
, kb
->mods
, GTK_ACCEL_VISIBLE
);
738 gtk_container_add(GTK_CONTAINER(me
), item
);
739 gtk_widget_show(item
);
740 g_signal_connect(item
, "activate", G_CALLBACK(cc_on_custom_command_activate
),
741 GINT_TO_POINTER(idx
));
745 void tools_create_insert_custom_command_menu_items(void)
747 GtkMenu
*menu_edit
= GTK_MENU(ui_lookup_widget(main_widgets
.window
, "send_selection_to2_menu"));
749 GList
*me_children
, *node
;
751 /* first clean the menus to be able to rebuild them */
752 me_children
= gtk_container_get_children(GTK_CONTAINER(menu_edit
));
753 foreach_list(node
, me_children
)
754 gtk_widget_destroy(GTK_WIDGET(node
->data
));
755 g_list_free(me_children
);
757 if (ui_prefs
.custom_commands
== NULL
|| g_strv_length(ui_prefs
.custom_commands
) == 0)
759 item
= gtk_menu_item_new_with_label(_("No custom commands defined."));
760 gtk_container_add(GTK_CONTAINER(menu_edit
), item
);
761 gtk_widget_set_sensitive(item
, FALSE
);
762 gtk_widget_show(item
);
768 len
= g_strv_length(ui_prefs
.custom_commands
);
769 for (i
= 0; i
< len
; i
++)
771 const gchar
*label
= ui_prefs
.custom_commands_labels
[i
];
774 label
= ui_prefs
.custom_commands
[i
];
775 if (!EMPTY(label
)) /* skip empty items */
777 cc_insert_custom_command_items(menu_edit
, label
, ui_prefs
.custom_commands
[i
], idx
);
783 /* separator and Set menu item */
784 item
= gtk_separator_menu_item_new();
785 gtk_container_add(GTK_CONTAINER(menu_edit
), item
);
786 gtk_widget_show(item
);
788 cc_insert_custom_command_items(menu_edit
, _("Set Custom Commands"), NULL
, -1);
792 /* (stolen from bluefish, thanks)
793 * Returns number of characters, lines and words in the supplied gchar*.
794 * Handles UTF-8 correctly. Input must be properly encoded UTF-8.
795 * Words are defined as any characters grouped, separated with spaces. */
796 static void word_count(gchar
*text
, guint
*chars
, guint
*lines
, guint
*words
)
802 return; /* politely refuse to operate on NULL */
804 *chars
= *words
= *lines
= 0;
805 while (*text
!= '\0')
826 utext
= g_utf8_get_char_validated(text
, 2); /* This might be an utf-8 char */
827 if (g_unichar_isspace(utext
)) /* Unicode encoded space? */
828 goto mb_word_separator
;
829 if (g_unichar_isgraph(utext
)) /* Is this something printable? */
833 /* Even if the current char is 2 bytes, this will iterate correctly. */
834 text
= g_utf8_next_char(text
);
837 /* Capture last word, if there's no whitespace at the end of the file. */
840 /* We start counting line numbers from 1 */
846 void tools_word_count(void)
848 GtkWidget
*dialog
, *label
, *vbox
, *table
;
850 guint chars
= 0, lines
= 0, words
= 0;
854 doc
= document_get_current();
855 g_return_if_fail(doc
!= NULL
);
857 dialog
= gtk_dialog_new_with_buttons(_("Word Count"), GTK_WINDOW(main_widgets
.window
),
858 GTK_DIALOG_DESTROY_WITH_PARENT
,
859 GTK_STOCK_CLOSE
, GTK_RESPONSE_CANCEL
, NULL
);
860 vbox
= ui_dialog_vbox_new(GTK_DIALOG(dialog
));
861 gtk_widget_set_name(dialog
, "GeanyDialog");
863 if (sci_has_selection(doc
->editor
->sci
))
865 text
= sci_get_selection_contents(doc
->editor
->sci
);
866 range
= _("selection");
870 text
= sci_get_contents(doc
->editor
->sci
, -1);
871 range
= _("whole document");
873 word_count(text
, &chars
, &lines
, &words
);
876 table
= gtk_table_new(4, 2, FALSE
);
877 gtk_table_set_row_spacings(GTK_TABLE(table
), 5);
878 gtk_table_set_col_spacings(GTK_TABLE(table
), 10);
880 label
= gtk_label_new(_("Range:"));
881 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 0, 1,
882 (GtkAttachOptions
) (GTK_FILL
),
883 (GtkAttachOptions
) (0), 0, 0);
884 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
886 label
= gtk_label_new(range
);
887 gtk_table_attach(GTK_TABLE(table
), label
, 1, 2, 0, 1,
888 (GtkAttachOptions
) (GTK_FILL
),
889 (GtkAttachOptions
) (0), 20, 0);
890 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0);
892 label
= gtk_label_new(_("Lines:"));
893 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 1, 2,
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", lines
);
899 label
= gtk_label_new(text
);
900 gtk_table_attach(GTK_TABLE(table
), label
, 1, 2, 1, 2,
901 (GtkAttachOptions
) (GTK_FILL
),
902 (GtkAttachOptions
) (0), 20, 0);
903 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0);
906 label
= gtk_label_new(_("Words:"));
907 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 2, 3,
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", words
);
913 label
= gtk_label_new(text
);
914 gtk_table_attach(GTK_TABLE(table
), label
, 1, 2, 2, 3,
915 (GtkAttachOptions
) (GTK_FILL
),
916 (GtkAttachOptions
) (0), 20, 0);
917 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0);
920 label
= gtk_label_new(_("Characters:"));
921 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 3, 4,
922 (GtkAttachOptions
) (GTK_FILL
),
923 (GtkAttachOptions
) (0), 0, 0);
924 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
926 text
= g_strdup_printf("%d", chars
);
927 label
= gtk_label_new(text
);
928 gtk_table_attach(GTK_TABLE(table
), label
, 1, 2, 3, 4,
929 (GtkAttachOptions
) (GTK_FILL
),
930 (GtkAttachOptions
) (0), 20, 0);
931 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0);
934 gtk_container_add(GTK_CONTAINER(vbox
), table
);
936 g_signal_connect(dialog
, "response", G_CALLBACK(gtk_widget_destroy
), dialog
);
937 g_signal_connect(dialog
, "delete-event", G_CALLBACK(gtk_widget_destroy
), dialog
);
939 gtk_widget_show_all(dialog
);
944 * color dialog callbacks
947 static void on_color_dialog_response(GtkDialog
*dialog
, gint response
, gpointer user_data
)
951 case GTK_RESPONSE_OK
:
954 GeanyDocument
*doc
= document_get_current();
958 gtk_widget_hide(ui_widgets
.open_colorsel
);
959 g_return_if_fail(doc
!= NULL
);
961 colorsel
= gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets
.open_colorsel
));
962 gtk_color_selection_get_current_color(GTK_COLOR_SELECTION(colorsel
), &color
);
964 hex
= utils_get_hex_from_color(&color
);
965 editor_insert_color(doc
->editor
, hex
);
971 gtk_widget_hide(ui_widgets
.open_colorsel
);
977 /* This shows the color selection dialog to choose a color. */
978 void tools_color_chooser(const gchar
*color
)
981 win32_show_color_dialog(color
);
983 gchar
*c
= (gchar
*) color
;
986 if (ui_widgets
.open_colorsel
== NULL
)
988 ui_widgets
.open_colorsel
= gtk_color_selection_dialog_new(_("Color Chooser"));
989 gtk_widget_set_name(ui_widgets
.open_colorsel
, "GeanyDialog");
990 gtk_window_set_transient_for(GTK_WINDOW(ui_widgets
.open_colorsel
), GTK_WINDOW(main_widgets
.window
));
991 colorsel
= gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets
.open_colorsel
));
992 gtk_color_selection_set_has_palette(GTK_COLOR_SELECTION(colorsel
), TRUE
);
994 g_signal_connect(ui_widgets
.open_colorsel
, "response",
995 G_CALLBACK(on_color_dialog_response
), NULL
);
996 g_signal_connect(ui_widgets
.open_colorsel
, "delete-event",
997 G_CALLBACK(gtk_widget_hide_on_delete
), NULL
);
1000 colorsel
= gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets
.open_colorsel
));
1001 /* if color is non-NULL set it in the dialog as preselected color */
1002 if (c
!= NULL
&& (c
[0] == '0' || c
[0] == '#'))
1006 if (c
[0] == '0' && c
[1] == 'x')
1007 { /* we have a string of the format "0x00ff00" and we need it to "#00ff00" */
1011 gdk_color_parse(c
, &gc
);
1012 gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(colorsel
), &gc
);
1013 gtk_color_selection_set_previous_color(GTK_COLOR_SELECTION(colorsel
), &gc
);
1016 /* We make sure the dialog is visible. */
1017 gtk_window_present(GTK_WINDOW(ui_widgets
.open_colorsel
));