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.
34 #include "keybindings.h"
35 #include "sciwrappers.h"
40 #include "gtkcompat.h"
48 # include <sys/types.h>
49 # include <sys/wait.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
&& DOC_VALID(data
->doc
))
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_VALID(doc
) && 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_VALID(doc
));
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
);
737 gtk_widget_add_accelerator(item
, "activate", gtk_accel_group_new(),
738 kb
->key
, kb
->mods
, GTK_ACCEL_VISIBLE
);
741 gtk_container_add(GTK_CONTAINER(me
), item
);
742 gtk_widget_show(item
);
743 g_signal_connect(item
, "activate", G_CALLBACK(cc_on_custom_command_activate
),
744 GINT_TO_POINTER(idx
));
748 void tools_create_insert_custom_command_menu_items(void)
750 GtkMenu
*menu_edit
= GTK_MENU(ui_lookup_widget(main_widgets
.window
, "send_selection_to2_menu"));
752 GList
*me_children
, *node
;
754 /* first clean the menus to be able to rebuild them */
755 me_children
= gtk_container_get_children(GTK_CONTAINER(menu_edit
));
756 foreach_list(node
, me_children
)
757 gtk_widget_destroy(GTK_WIDGET(node
->data
));
758 g_list_free(me_children
);
760 if (ui_prefs
.custom_commands
== NULL
|| g_strv_length(ui_prefs
.custom_commands
) == 0)
762 item
= gtk_menu_item_new_with_label(_("No custom commands defined."));
763 gtk_container_add(GTK_CONTAINER(menu_edit
), item
);
764 gtk_widget_set_sensitive(item
, FALSE
);
765 gtk_widget_show(item
);
771 len
= g_strv_length(ui_prefs
.custom_commands
);
772 for (i
= 0; i
< len
; i
++)
774 const gchar
*label
= ui_prefs
.custom_commands_labels
[i
];
777 label
= ui_prefs
.custom_commands
[i
];
778 if (!EMPTY(label
)) /* skip empty items */
780 cc_insert_custom_command_items(menu_edit
, label
, ui_prefs
.custom_commands
[i
], idx
);
786 /* separator and Set menu item */
787 item
= gtk_separator_menu_item_new();
788 gtk_container_add(GTK_CONTAINER(menu_edit
), item
);
789 gtk_widget_show(item
);
791 cc_insert_custom_command_items(menu_edit
, _("Set Custom Commands"), NULL
, -1);
795 /* (stolen from bluefish, thanks)
796 * Returns number of characters, lines and words in the supplied gchar*.
797 * Handles UTF-8 correctly. Input must be properly encoded UTF-8.
798 * Words are defined as any characters grouped, separated with spaces. */
799 static void word_count(gchar
*text
, guint
*chars
, guint
*lines
, guint
*words
)
805 return; /* politely refuse to operate on NULL */
807 *chars
= *words
= *lines
= 0;
808 while (*text
!= '\0')
829 utext
= g_utf8_get_char_validated(text
, 2); /* This might be an utf-8 char */
830 if (g_unichar_isspace(utext
)) /* Unicode encoded space? */
831 goto mb_word_separator
;
832 if (g_unichar_isgraph(utext
)) /* Is this something printable? */
836 /* Even if the current char is 2 bytes, this will iterate correctly. */
837 text
= g_utf8_next_char(text
);
840 /* Capture last word, if there's no whitespace at the end of the file. */
843 /* We start counting line numbers from 1 */
849 void tools_word_count(void)
851 GtkWidget
*dialog
, *label
, *vbox
, *table
;
853 guint chars
= 0, lines
= 0, words
= 0;
857 doc
= document_get_current();
858 g_return_if_fail(doc
!= NULL
);
860 dialog
= gtk_dialog_new_with_buttons(_("Word Count"), GTK_WINDOW(main_widgets
.window
),
861 GTK_DIALOG_DESTROY_WITH_PARENT
,
862 GTK_STOCK_CLOSE
, GTK_RESPONSE_CANCEL
, NULL
);
863 vbox
= ui_dialog_vbox_new(GTK_DIALOG(dialog
));
864 gtk_widget_set_name(dialog
, "GeanyDialog");
866 if (sci_has_selection(doc
->editor
->sci
))
868 text
= sci_get_selection_contents(doc
->editor
->sci
);
869 range
= _("selection");
873 text
= sci_get_contents(doc
->editor
->sci
, -1);
874 range
= _("whole document");
876 word_count(text
, &chars
, &lines
, &words
);
879 table
= gtk_table_new(4, 2, FALSE
);
880 gtk_table_set_row_spacings(GTK_TABLE(table
), 5);
881 gtk_table_set_col_spacings(GTK_TABLE(table
), 10);
883 label
= gtk_label_new(_("Range:"));
884 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 0, 1,
885 (GtkAttachOptions
) (GTK_FILL
),
886 (GtkAttachOptions
) (0), 0, 0);
887 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
889 label
= gtk_label_new(range
);
890 gtk_table_attach(GTK_TABLE(table
), label
, 1, 2, 0, 1,
891 (GtkAttachOptions
) (GTK_FILL
),
892 (GtkAttachOptions
) (0), 20, 0);
893 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0);
895 label
= gtk_label_new(_("Lines:"));
896 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 1, 2,
897 (GtkAttachOptions
) (GTK_FILL
),
898 (GtkAttachOptions
) (0), 0, 0);
899 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
901 text
= g_strdup_printf("%d", lines
);
902 label
= gtk_label_new(text
);
903 gtk_table_attach(GTK_TABLE(table
), label
, 1, 2, 1, 2,
904 (GtkAttachOptions
) (GTK_FILL
),
905 (GtkAttachOptions
) (0), 20, 0);
906 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0);
909 label
= gtk_label_new(_("Words:"));
910 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 2, 3,
911 (GtkAttachOptions
) (GTK_FILL
),
912 (GtkAttachOptions
) (0), 0, 0);
913 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
915 text
= g_strdup_printf("%d", words
);
916 label
= gtk_label_new(text
);
917 gtk_table_attach(GTK_TABLE(table
), label
, 1, 2, 2, 3,
918 (GtkAttachOptions
) (GTK_FILL
),
919 (GtkAttachOptions
) (0), 20, 0);
920 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0);
923 label
= gtk_label_new(_("Characters:"));
924 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 3, 4,
925 (GtkAttachOptions
) (GTK_FILL
),
926 (GtkAttachOptions
) (0), 0, 0);
927 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
929 text
= g_strdup_printf("%d", chars
);
930 label
= gtk_label_new(text
);
931 gtk_table_attach(GTK_TABLE(table
), label
, 1, 2, 3, 4,
932 (GtkAttachOptions
) (GTK_FILL
),
933 (GtkAttachOptions
) (0), 20, 0);
934 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0);
937 gtk_container_add(GTK_CONTAINER(vbox
), table
);
939 g_signal_connect(dialog
, "response", G_CALLBACK(gtk_widget_destroy
), dialog
);
940 g_signal_connect(dialog
, "delete-event", G_CALLBACK(gtk_widget_destroy
), dialog
);
942 gtk_widget_show_all(dialog
);
947 * color dialog callbacks
949 static void on_color_dialog_response(GtkDialog
*dialog
, gint response
, gpointer user_data
)
953 case GTK_RESPONSE_OK
:
954 gtk_widget_hide(ui_widgets
.open_colorsel
);
956 case GTK_RESPONSE_APPLY
:
959 GeanyDocument
*doc
= document_get_current();
963 g_return_if_fail(doc
!= NULL
);
965 colorsel
= gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets
.open_colorsel
));
966 gtk_color_selection_get_current_color(GTK_COLOR_SELECTION(colorsel
), &color
);
968 hex
= utils_get_hex_from_color(&color
);
969 editor_insert_color(doc
->editor
, hex
);
975 gtk_widget_hide(ui_widgets
.open_colorsel
);
980 /* This shows the color selection dialog to choose a color. */
981 void tools_color_chooser(const gchar
*color
)
987 if (interface_prefs
.use_native_windows_dialogs
)
989 win32_show_color_dialog(color
);
994 if (ui_widgets
.open_colorsel
== NULL
)
996 ui_widgets
.open_colorsel
= gtk_color_selection_dialog_new(_("Color Chooser"));
997 gtk_dialog_add_button(GTK_DIALOG(ui_widgets
.open_colorsel
), GTK_STOCK_APPLY
, GTK_RESPONSE_APPLY
);
998 ui_dialog_set_primary_button_order(GTK_DIALOG(ui_widgets
.open_colorsel
),
999 GTK_RESPONSE_APPLY
, GTK_RESPONSE_CANCEL
, GTK_RESPONSE_OK
, -1);
1000 gtk_widget_set_name(ui_widgets
.open_colorsel
, "GeanyDialog");
1001 gtk_window_set_transient_for(GTK_WINDOW(ui_widgets
.open_colorsel
), GTK_WINDOW(main_widgets
.window
));
1002 colorsel
= gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets
.open_colorsel
));
1003 gtk_color_selection_set_has_palette(GTK_COLOR_SELECTION(colorsel
), TRUE
);
1005 g_signal_connect(ui_widgets
.open_colorsel
, "response",
1006 G_CALLBACK(on_color_dialog_response
), NULL
);
1007 g_signal_connect(ui_widgets
.open_colorsel
, "delete-event",
1008 G_CALLBACK(gtk_widget_hide_on_delete
), NULL
);
1011 colorsel
= gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets
.open_colorsel
));
1012 /* if color is non-NULL set it in the dialog as preselected color */
1013 if (color
!= NULL
&& utils_parse_color(color
, &gc
))
1015 gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(colorsel
), &gc
);
1016 gtk_color_selection_set_previous_color(GTK_COLOR_SELECTION(colorsel
), &gc
);
1019 /* We make sure the dialog is visible. */
1020 gtk_window_present(GTK_WINDOW(ui_widgets
.open_colorsel
));