2 * vte.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-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 * Virtual Terminal Emulation setup and handling code, using the libvte plugin library.
30 /* include stdlib.h AND unistd.h, because on GNU/Linux pid_t seems to be
31 * in stdlib.h, on FreeBSD in unistd.h, sys/types.h is needed for C89 */
33 #include <sys/types.h>
36 #include <gdk/gdkkeysyms.h>
47 #include "msgwindow.h"
48 #include "callbacks.h"
49 #include "geanywraplabel.h"
51 #include "sciwrappers.h"
58 static gboolean clean
= TRUE
;
59 static GModule
*module
= NULL
;
60 static struct VteFunctions
*vf
;
61 static gchar
*gtk_menu_key_accel
= NULL
;
63 /* use vte wordchars to select paths */
64 static const gchar VTE_WORDCHARS
[] = "-A-Za-z0-9,./?%&#:_";
67 /* Incomplete VteTerminal struct from vte/vte.h. */
68 typedef struct _VteTerminal VteTerminal
;
72 GtkAdjustment
*adjustment
;
75 #define VTE_TERMINAL(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), VTE_TYPE_TERMINAL, VteTerminal))
76 #define VTE_TYPE_TERMINAL (vf->vte_terminal_get_type())
79 VTE_CURSOR_BLINK_SYSTEM
,
82 } VteTerminalCursorBlinkMode
;
85 /* Holds function pointers we need to access the VTE API. */
88 GtkWidget
* (*vte_terminal_new
) (void);
89 pid_t (*vte_terminal_fork_command
) (VteTerminal
*terminal
, const char *command
, char **argv
,
90 char **envv
, const char *directory
, gboolean lastlog
,
91 gboolean utmp
, gboolean wtmp
);
92 void (*vte_terminal_set_size
) (VteTerminal
*terminal
, glong columns
, glong rows
);
93 void (*vte_terminal_set_word_chars
) (VteTerminal
*terminal
, const char *spec
);
94 void (*vte_terminal_set_mouse_autohide
) (VteTerminal
*terminal
, gboolean setting
);
95 void (*vte_terminal_reset
) (VteTerminal
*terminal
, gboolean full
, gboolean clear_history
);
96 GType (*vte_terminal_get_type
) (void);
97 void (*vte_terminal_set_scroll_on_output
) (VteTerminal
*terminal
, gboolean scroll
);
98 void (*vte_terminal_set_scroll_on_keystroke
) (VteTerminal
*terminal
, gboolean scroll
);
99 void (*vte_terminal_set_font_from_string
) (VteTerminal
*terminal
, const char *name
);
100 void (*vte_terminal_set_scrollback_lines
) (VteTerminal
*terminal
, glong lines
);
101 gboolean (*vte_terminal_get_has_selection
) (VteTerminal
*terminal
);
102 void (*vte_terminal_copy_clipboard
) (VteTerminal
*terminal
);
103 void (*vte_terminal_paste_clipboard
) (VteTerminal
*terminal
);
104 void (*vte_terminal_set_emulation
) (VteTerminal
*terminal
, const gchar
*emulation
);
105 void (*vte_terminal_set_color_foreground
) (VteTerminal
*terminal
, const GdkColor
*foreground
);
106 void (*vte_terminal_set_color_bold
) (VteTerminal
*terminal
, const GdkColor
*foreground
);
107 void (*vte_terminal_set_color_background
) (VteTerminal
*terminal
, const GdkColor
*background
);
108 void (*vte_terminal_feed_child
) (VteTerminal
*terminal
, const char *data
, glong length
);
109 void (*vte_terminal_im_append_menuitems
) (VteTerminal
*terminal
, GtkMenuShell
*menushell
);
110 void (*vte_terminal_set_cursor_blink_mode
) (VteTerminal
*terminal
,
111 VteTerminalCursorBlinkMode mode
);
112 void (*vte_terminal_set_cursor_blinks
) (VteTerminal
*terminal
, gboolean blink
);
113 void (*vte_terminal_select_all
) (VteTerminal
*terminal
);
114 void (*vte_terminal_set_audible_bell
) (VteTerminal
*terminal
, gboolean is_audible
);
118 static void create_vte(void);
119 static void vte_start(GtkWidget
*widget
);
120 static void vte_restart(GtkWidget
*widget
);
121 static gboolean
vte_button_pressed(GtkWidget
*widget
, GdkEventButton
*event
, gpointer user_data
);
122 static gboolean
vte_keyrelease_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
);
123 static gboolean
vte_keypress_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
);
124 static gboolean
vte_register_symbols(GModule
*module
);
125 static void vte_popup_menu_clicked(GtkMenuItem
*menuitem
, gpointer user_data
);
126 static GtkWidget
*vte_create_popup_menu(void);
127 static void vte_commit_cb(VteTerminal
*vte
, gchar
*arg1
, guint arg2
, gpointer user_data
);
128 static void vte_drag_data_received(GtkWidget
*widget
, GdkDragContext
*drag_context
,
129 gint x
, gint y
, GtkSelectionData
*data
, guint info
, guint ltime
);
138 POPUP_RESTARTTERMINAL
,
140 TARGET_UTF8_STRING
= 0,
142 TARGET_COMPOUND_TEXT
,
147 static const GtkTargetEntry dnd_targets
[] =
149 { "UTF8_STRING", 0, TARGET_UTF8_STRING
},
150 { "TEXT", 0, TARGET_TEXT
},
151 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT
},
152 { "STRING", 0, TARGET_STRING
},
153 { "text/plain", 0, TARGET_TEXT_PLAIN
},
157 static gchar
**vte_get_child_environment(void)
159 const gchar
*exclude_vars
[] = {"COLUMNS", "LINES", "TERM", NULL
};
161 return utils_copy_environment(exclude_vars
, "TERM", "xterm", NULL
);
165 static void override_menu_key(void)
167 if (gtk_menu_key_accel
== NULL
) /* for restoring the default value */
168 g_object_get(G_OBJECT(gtk_settings_get_default()),
169 "gtk-menu-bar-accel", >k_menu_key_accel
, NULL
);
171 if (vc
->ignore_menu_bar_accel
)
172 gtk_settings_set_string_property(gtk_settings_get_default(),
173 "gtk-menu-bar-accel", "<Shift><Control><Mod1><Mod2><Mod3><Mod4><Mod5>F10", "Geany");
175 gtk_settings_set_string_property(gtk_settings_get_default(),
176 "gtk-menu-bar-accel", gtk_menu_key_accel
, "Geany");
182 if (vte_info
.have_vte
== FALSE
)
183 { /* vte_info.have_vte can be false even if VTE is compiled in, think of command line option */
184 geany_debug("Disabling terminal support");
188 if (NZV(vte_info
.lib_vte
))
190 module
= g_module_open(vte_info
.lib_vte
, G_MODULE_BIND_LAZY
);
192 #ifdef VTE_MODULE_PATH
195 module
= g_module_open(VTE_MODULE_PATH
, G_MODULE_BIND_LAZY
);
202 const gchar
*sonames
[] = { "libvte.so", "libvte.so.4",
203 "libvte.so.8", "libvte.so.9", NULL
};
205 for (i
= 0; sonames
[i
] != NULL
&& module
== NULL
; i
++)
207 module
= g_module_open(sonames
[i
], G_MODULE_BIND_LAZY
);
213 vte_info
.have_vte
= FALSE
;
214 geany_debug("Could not load libvte.so, embedded terminal support disabled");
219 vf
= g_new0(struct VteFunctions
, 1);
220 if (vte_register_symbols(module
))
221 vte_info
.have_vte
= TRUE
;
224 vte_info
.have_vte
= FALSE
;
226 /* FIXME: is closing the module safe? see vte_close() and test on FreeBSD */
227 /*g_module_close(module);*/
235 /* setup the F10 menu override (so it works before the widget is first realised). */
240 static void on_vte_realize(void)
242 /* the vte widget has to be realised before color changes take effect */
243 vte_apply_user_settings();
245 vf
->vte_terminal_im_append_menuitems(VTE_TERMINAL(vc
->vte
), GTK_MENU_SHELL(vc
->im_submenu
));
249 static void create_vte(void)
251 GtkWidget
*vte
, *scrollbar
, *hbox
, *frame
;
253 vc
->vte
= vte
= vf
->vte_terminal_new();
254 scrollbar
= gtk_vscrollbar_new(GTK_ADJUSTMENT(VTE_TERMINAL(vte
)->adjustment
));
255 GTK_WIDGET_UNSET_FLAGS(scrollbar
, GTK_CAN_FOCUS
);
257 /* create menu now so copy/paste shortcuts work */
258 vc
->menu
= vte_create_popup_menu();
259 g_object_ref_sink(vc
->menu
);
261 frame
= gtk_frame_new(NULL
);
263 hbox
= gtk_hbox_new(FALSE
, 0);
264 gtk_container_add(GTK_CONTAINER(frame
), hbox
);
265 gtk_box_pack_start(GTK_BOX(hbox
), vte
, TRUE
, TRUE
, 0);
266 gtk_box_pack_start(GTK_BOX(hbox
), scrollbar
, FALSE
, FALSE
, 0);
268 /* set the default widget size first to prevent VTE expanding too much,
269 * sometimes causing the hscrollbar to be too big or out of view. */
270 gtk_widget_set_size_request(GTK_WIDGET(vte
), 10, 10);
271 vf
->vte_terminal_set_size(VTE_TERMINAL(vte
), 30, 1);
273 vf
->vte_terminal_set_mouse_autohide(VTE_TERMINAL(vte
), TRUE
);
274 vf
->vte_terminal_set_word_chars(VTE_TERMINAL(vte
), VTE_WORDCHARS
);
276 gtk_drag_dest_set(vte
, GTK_DEST_DEFAULT_ALL
,
277 dnd_targets
, G_N_ELEMENTS(dnd_targets
), GDK_ACTION_COPY
);
279 g_signal_connect(vte
, "child-exited", G_CALLBACK(vte_start
), NULL
);
280 g_signal_connect(vte
, "button-press-event", G_CALLBACK(vte_button_pressed
), NULL
);
281 g_signal_connect(vte
, "event", G_CALLBACK(vte_keypress_cb
), NULL
);
282 g_signal_connect(vte
, "key-release-event", G_CALLBACK(vte_keyrelease_cb
), NULL
);
283 g_signal_connect(vte
, "commit", G_CALLBACK(vte_commit_cb
), NULL
);
284 g_signal_connect(vte
, "motion-notify-event", G_CALLBACK(on_motion_event
), NULL
);
285 g_signal_connect(vte
, "drag-data-received", G_CALLBACK(vte_drag_data_received
), NULL
);
289 gtk_widget_show_all(frame
);
290 gtk_notebook_insert_page(GTK_NOTEBOOK(msgwindow
.notebook
), frame
, gtk_label_new(_("Terminal")), MSG_VTE
);
292 g_signal_connect_after(vte
, "realize", G_CALLBACK(on_vte_realize
), NULL
);
299 /* free the vte widget before unloading vte module
300 * this prevents a segfault on X close window if the message window is hidden */
301 gtk_widget_destroy(vc
->vte
);
302 gtk_widget_destroy(vc
->menu
);
303 g_object_unref(vc
->menu
);
304 g_free(vc
->emulation
);
307 g_free(vc
->colour_back
);
308 g_free(vc
->colour_fore
);
309 g_free(vc
->send_cmd_prefix
);
311 g_free(gtk_menu_key_accel
);
312 /* Don't unload the module explicitly because it causes a segfault on FreeBSD. The segfault
313 * happens when the app really exits, not directly on g_module_close(). This still needs to
314 * be investigated. */
315 /*g_module_close(module); */
319 static gboolean
vte_keyrelease_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
321 if (ui_is_keyval_enter_or_return(event
->keyval
) ||
322 ((event
->keyval
== GDK_c
) && (event
->state
& GDK_CONTROL_MASK
)))
324 /* assume any text on the prompt has been executed when pressing Enter/Return */
331 static gboolean
vte_keypress_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
333 if (vc
->enable_bash_keys
)
334 return FALSE
; /* Ctrl-[CD] will be handled by the VTE itself */
336 if (event
->type
!= GDK_KEY_RELEASE
)
339 if ((event
->keyval
== GDK_c
||
340 event
->keyval
== GDK_d
||
341 event
->keyval
== GDK_C
||
342 event
->keyval
== GDK_D
) &&
343 event
->state
& GDK_CONTROL_MASK
&&
344 ! (event
->state
& GDK_SHIFT_MASK
) && ! (event
->state
& GDK_MOD1_MASK
))
353 static void vte_commit_cb(VteTerminal
*vte
, gchar
*arg1
, guint arg2
, gpointer user_data
)
359 static void vte_start(GtkWidget
*widget
)
364 /* split the shell command line, so arguments will work too */
365 argv
= g_strsplit(vc
->shell
, " ", -1);
369 env
= vte_get_child_environment();
370 pid
= vf
->vte_terminal_fork_command(VTE_TERMINAL(widget
), argv
[0], argv
, env
,
371 vte_info
.dir
, TRUE
, TRUE
, TRUE
);
376 pid
= 0; /* use 0 as invalid pid */
382 static void vte_restart(GtkWidget
*widget
)
384 vte_get_working_directory(); /* try to keep the working directory when restarting the VTE */
390 vf
->vte_terminal_reset(VTE_TERMINAL(widget
), TRUE
, TRUE
);
395 static gboolean
vte_button_pressed(GtkWidget
*widget
, GdkEventButton
*event
, gpointer user_data
)
397 if (event
->button
== 3)
399 gtk_widget_grab_focus(vc
->vte
);
400 gtk_menu_popup(GTK_MENU(vc
->menu
), NULL
, NULL
, NULL
, NULL
, event
->button
, event
->time
);
406 static void vte_set_cursor_blink_mode(void)
408 if (vf
->vte_terminal_set_cursor_blink_mode
!= NULL
)
410 vf
->vte_terminal_set_cursor_blink_mode(VTE_TERMINAL(vc
->vte
),
411 (vc
->cursor_blinks
) ? VTE_CURSOR_BLINK_ON
: VTE_CURSOR_BLINK_OFF
);
414 vf
->vte_terminal_set_cursor_blinks(VTE_TERMINAL(vc
->vte
), vc
->cursor_blinks
);
418 static gboolean
vte_register_symbols(GModule
*mod
)
420 #define BIND_SYMBOL(field) \
421 g_module_symbol(mod, #field, (void*)&vf->field)
422 #define BIND_REQUIRED_SYMBOL(field) \
424 if (! BIND_SYMBOL(field)) \
426 g_critical(_("invalid VTE library \"%s\": missing symbol \"%s\""), \
427 g_module_name(mod), #field); \
432 BIND_REQUIRED_SYMBOL(vte_terminal_new
);
433 BIND_REQUIRED_SYMBOL(vte_terminal_set_size
);
434 BIND_REQUIRED_SYMBOL(vte_terminal_fork_command
);
435 BIND_REQUIRED_SYMBOL(vte_terminal_set_word_chars
);
436 BIND_REQUIRED_SYMBOL(vte_terminal_set_mouse_autohide
);
437 BIND_REQUIRED_SYMBOL(vte_terminal_reset
);
438 BIND_REQUIRED_SYMBOL(vte_terminal_get_type
);
439 BIND_REQUIRED_SYMBOL(vte_terminal_set_scroll_on_output
);
440 BIND_REQUIRED_SYMBOL(vte_terminal_set_scroll_on_keystroke
);
441 BIND_REQUIRED_SYMBOL(vte_terminal_set_font_from_string
);
442 BIND_REQUIRED_SYMBOL(vte_terminal_set_scrollback_lines
);
443 BIND_REQUIRED_SYMBOL(vte_terminal_get_has_selection
);
444 BIND_REQUIRED_SYMBOL(vte_terminal_copy_clipboard
);
445 BIND_REQUIRED_SYMBOL(vte_terminal_paste_clipboard
);
446 BIND_REQUIRED_SYMBOL(vte_terminal_set_emulation
);
447 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_foreground
);
448 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_bold
);
449 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_background
);
450 BIND_REQUIRED_SYMBOL(vte_terminal_feed_child
);
451 BIND_REQUIRED_SYMBOL(vte_terminal_im_append_menuitems
);
452 if (! BIND_SYMBOL(vte_terminal_set_cursor_blink_mode
))
453 /* vte_terminal_set_cursor_blink_mode() is only available since 0.17.1, so if we don't find
454 * this symbol, we are probably on an older version and use the old API instead */
455 BIND_REQUIRED_SYMBOL(vte_terminal_set_cursor_blinks
);
456 BIND_REQUIRED_SYMBOL(vte_terminal_select_all
);
457 BIND_REQUIRED_SYMBOL(vte_terminal_set_audible_bell
);
459 #undef BIND_REQUIRED_SYMBOL
466 void vte_apply_user_settings(void)
468 if (! ui_prefs
.msgwindow_visible
)
471 vf
->vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->vte
), vc
->scrollback_lines
);
472 vf
->vte_terminal_set_scroll_on_keystroke(VTE_TERMINAL(vc
->vte
), vc
->scroll_on_key
);
473 vf
->vte_terminal_set_scroll_on_output(VTE_TERMINAL(vc
->vte
), vc
->scroll_on_out
);
474 vf
->vte_terminal_set_emulation(VTE_TERMINAL(vc
->vte
), vc
->emulation
);
475 vf
->vte_terminal_set_font_from_string(VTE_TERMINAL(vc
->vte
), vc
->font
);
476 vf
->vte_terminal_set_color_foreground(VTE_TERMINAL(vc
->vte
), vc
->colour_fore
);
477 vf
->vte_terminal_set_color_bold(VTE_TERMINAL(vc
->vte
), vc
->colour_fore
);
478 vf
->vte_terminal_set_color_background(VTE_TERMINAL(vc
->vte
), vc
->colour_back
);
479 vf
->vte_terminal_set_audible_bell(VTE_TERMINAL(vc
->vte
), prefs
.beep_on_errors
);
480 vte_set_cursor_blink_mode();
486 static void vte_popup_menu_clicked(GtkMenuItem
*menuitem
, gpointer user_data
)
488 switch (GPOINTER_TO_INT(user_data
))
492 if (vf
->vte_terminal_get_has_selection(VTE_TERMINAL(vc
->vte
)))
493 vf
->vte_terminal_copy_clipboard(VTE_TERMINAL(vc
->vte
));
498 vf
->vte_terminal_paste_clipboard(VTE_TERMINAL(vc
->vte
));
501 case POPUP_SELECTALL
:
506 case POPUP_CHANGEPATH
:
508 GeanyDocument
*doc
= document_get_current();
510 vte_cwd(doc
->file_name
, TRUE
);
513 case POPUP_RESTARTTERMINAL
:
515 vte_restart(vc
->vte
);
518 case POPUP_PREFERENCES
:
520 GtkWidget
*notebook
, *tab_page
;
524 notebook
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "notebook2");
525 tab_page
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "frame_term");
527 gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook
),
528 gtk_notebook_page_num(GTK_NOTEBOOK(notebook
), GTK_WIDGET(tab_page
)));
536 static GtkWidget
*vte_create_popup_menu(void)
538 GtkWidget
*menu
, *item
;
539 GtkAccelGroup
*accel_group
;
541 menu
= gtk_menu_new();
543 accel_group
= gtk_accel_group_new();
544 gtk_window_add_accel_group(GTK_WINDOW(main_widgets
.window
), accel_group
);
546 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_COPY
, NULL
);
547 gtk_widget_add_accelerator(item
, "activate", accel_group
,
548 GDK_c
, GDK_CONTROL_MASK
| GDK_SHIFT_MASK
, GTK_ACCEL_VISIBLE
);
549 gtk_widget_show(item
);
550 gtk_container_add(GTK_CONTAINER(menu
), item
);
551 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_COPY
));
553 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_PASTE
, NULL
);
554 gtk_widget_add_accelerator(item
, "activate", accel_group
,
555 GDK_v
, GDK_CONTROL_MASK
| GDK_SHIFT_MASK
, GTK_ACCEL_VISIBLE
);
556 gtk_widget_show(item
);
557 gtk_container_add(GTK_CONTAINER(menu
), item
);
558 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_PASTE
));
560 item
= gtk_separator_menu_item_new();
561 gtk_widget_show(item
);
562 gtk_container_add(GTK_CONTAINER(menu
), item
);
564 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_SELECT_ALL
, NULL
);
565 gtk_widget_show(item
);
566 gtk_container_add(GTK_CONTAINER(menu
), item
);
567 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_SELECTALL
));
569 item
= gtk_separator_menu_item_new();
570 gtk_widget_show(item
);
571 gtk_container_add(GTK_CONTAINER(menu
), item
);
573 item
= gtk_image_menu_item_new_with_mnemonic(_("_Set Path From Document"));
574 gtk_widget_show(item
);
575 gtk_container_add(GTK_CONTAINER(menu
), item
);
576 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_CHANGEPATH
));
578 item
= gtk_image_menu_item_new_with_mnemonic(_("_Restart Terminal"));
579 gtk_widget_show(item
);
580 gtk_container_add(GTK_CONTAINER(menu
), item
);
581 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_RESTARTTERMINAL
));
583 item
= gtk_separator_menu_item_new();
584 gtk_widget_show(item
);
585 gtk_container_add(GTK_CONTAINER(menu
), item
);
587 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES
, NULL
);
588 gtk_widget_show(item
);
589 gtk_container_add(GTK_CONTAINER(menu
), item
);
590 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_PREFERENCES
));
592 msgwin_menu_add_common_items(GTK_MENU(menu
));
594 item
= gtk_separator_menu_item_new();
595 gtk_widget_show(item
);
596 gtk_container_add(GTK_CONTAINER(menu
), item
);
598 /* the IM submenu should always be the last item to be consistent with other GTK popup menus */
599 vc
->im_submenu
= gtk_menu_new();
601 item
= gtk_image_menu_item_new_with_mnemonic(_("_Input Methods"));
602 gtk_widget_show(item
);
603 gtk_container_add(GTK_CONTAINER(menu
), item
);
605 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item
), vc
->im_submenu
);
606 /* submenu populated after vte realized */
611 /* If the command could be executed, TRUE is returned, FALSE otherwise (i.e. there was some text
613 gboolean
vte_send_cmd(const gchar
*cmd
)
617 vf
->vte_terminal_feed_child(VTE_TERMINAL(vc
->vte
), cmd
, strlen(cmd
));
618 clean
= TRUE
; /* vte_terminal_feed_child() also marks the vte as not clean */
626 /* Taken from Terminal by os-cillation: terminal_screen_get_working_directory, thanks.
627 * Determines the working directory using various OS-specific mechanisms and stores the determined
628 * directory in vte_info.dir. Note: vte_info.dir contains the real path. */
629 const gchar
*vte_get_working_directory(void)
631 gchar buffer
[4096 + 1];
638 file
= g_strdup_printf("/proc/%d/cwd", pid
);
639 length
= readlink(file
, buffer
, sizeof(buffer
));
641 if (length
> 0 && *buffer
== '/')
643 buffer
[length
] = '\0';
644 g_free(vte_info
.dir
);
645 vte_info
.dir
= g_strdup(buffer
);
647 else if (length
== 0)
649 cwd
= g_get_current_dir();
652 if (chdir(file
) == 0)
654 g_free(vte_info
.dir
);
655 vte_info
.dir
= g_get_current_dir();
657 geany_debug("%s: %s", G_STRFUNC
, g_strerror(errno
));
669 /* Changes the current working directory of the VTE to the path of the given filename.
670 * filename is expected to be in UTF-8 encoding.
671 * filename can also be a path, then it is used directly.
672 * If force is set to TRUE, it will always change the cwd
674 void vte_cwd(const gchar
*filename
, gboolean force
)
676 if (vte_info
.have_vte
&& (vc
->follow_path
|| force
) &&
677 filename
!= NULL
&& g_path_is_absolute(filename
))
681 if (g_file_test(filename
, G_FILE_TEST_IS_DIR
))
682 path
= g_strdup(filename
);
684 path
= g_path_get_dirname(filename
);
686 vte_get_working_directory(); /* refresh vte_info.dir */
687 if (! utils_str_equal(path
, vte_info
.dir
))
689 /* use g_shell_quote to avoid problems with spaces, '!' or something else in path */
690 gchar
*quoted_path
= g_shell_quote(path
);
691 gchar
*cmd
= g_strconcat(vc
->send_cmd_prefix
, "cd ", quoted_path
, "\n", NULL
);
692 if (! vte_send_cmd(cmd
))
694 ui_set_statusbar(FALSE
,
695 _("Could not change the directory in the VTE because it probably contains a command."));
697 "Could not change the directory in the VTE because it probably contains a command.");
707 static void vte_drag_data_received(GtkWidget
*widget
, GdkDragContext
*drag_context
,
708 gint x
, gint y
, GtkSelectionData
*data
, guint info
, guint ltime
)
710 if (info
== TARGET_TEXT_PLAIN
)
712 if (gtk_selection_data_get_format(data
) == 8 && gtk_selection_data_get_length(data
) > 0)
713 vf
->vte_terminal_feed_child(VTE_TERMINAL(widget
),
714 (const gchar
*) gtk_selection_data_get_data(data
),
715 gtk_selection_data_get_length(data
));
719 gchar
*text
= (gchar
*) gtk_selection_data_get_text(data
);
721 vf
->vte_terminal_feed_child(VTE_TERMINAL(widget
), text
, strlen(text
));
724 gtk_drag_finish(drag_context
, TRUE
, FALSE
, ltime
);
728 G_MODULE_EXPORT
void on_check_run_in_vte_toggled(GtkToggleButton
*togglebutton
, GtkWidget
*user_data
)
730 g_return_if_fail(GTK_IS_WIDGET(user_data
));
731 gtk_widget_set_sensitive(user_data
, gtk_toggle_button_get_active(togglebutton
));
735 G_MODULE_EXPORT
void on_term_font_set(GtkFontButton
*widget
, gpointer user_data
)
737 const gchar
*fontbtn
= gtk_font_button_get_font_name(widget
);
739 if (! utils_str_equal(fontbtn
, vc
->font
))
741 SETPTR(vc
->font
, g_strdup(gtk_font_button_get_font_name(widget
)));
742 vte_apply_user_settings();
747 G_MODULE_EXPORT
void on_term_fg_color_set(GtkColorButton
*widget
, gpointer user_data
)
749 g_free(vc
->colour_fore
);
750 vc
->colour_fore
= g_new0(GdkColor
, 1);
751 gtk_color_button_get_color(widget
, vc
->colour_fore
);
755 G_MODULE_EXPORT
void on_term_bg_color_set(GtkColorButton
*widget
, gpointer user_data
)
757 g_free(vc
->colour_back
);
758 vc
->colour_back
= g_new0(GdkColor
, 1);
759 gtk_color_button_get_color(widget
, vc
->colour_back
);
763 void vte_append_preferences_tab(void)
765 if (vte_info
.have_vte
)
767 GtkWidget
*frame_term
, *button_shell
, *entry_shell
;
768 GtkWidget
*check_run_in_vte
, *check_skip_script
;
769 GtkWidget
*font_button
, *fg_color_button
, *bg_color_button
;
771 button_shell
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "button_term_shell"));
772 entry_shell
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "entry_shell"));
773 ui_setup_open_button_callback(button_shell
, NULL
,
774 GTK_FILE_CHOOSER_ACTION_OPEN
, GTK_ENTRY(entry_shell
));
776 check_skip_script
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "check_skip_script"));
777 gtk_widget_set_sensitive(check_skip_script
, vc
->run_in_vte
);
779 check_run_in_vte
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "check_run_in_vte"));
780 g_signal_connect(G_OBJECT(check_run_in_vte
), "toggled",
781 G_CALLBACK(on_check_run_in_vte_toggled
), check_skip_script
);
783 font_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "font_term");
784 g_signal_connect(font_button
, "font-set", G_CALLBACK(on_term_font_set
), NULL
);
786 fg_color_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "color_fore");
787 g_signal_connect(fg_color_button
, "color-set", G_CALLBACK(on_term_fg_color_set
), NULL
);
789 bg_color_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "color_back");
790 g_signal_connect(bg_color_button
, "color-set", G_CALLBACK(on_term_bg_color_set
), NULL
);
792 frame_term
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "frame_term");
793 gtk_widget_show_all(frame_term
);
798 void vte_select_all(void)
800 if (vf
->vte_terminal_select_all
!= NULL
)
801 vf
->vte_terminal_select_all(VTE_TERMINAL(vc
->vte
));
805 void vte_send_selection_to_vte(void)
811 doc
= document_get_current();
812 g_return_if_fail(doc
!= NULL
);
814 if (sci_has_selection(doc
->editor
->sci
))
816 text
= g_malloc0(sci_get_selected_text_length(doc
->editor
->sci
) + 1);
817 sci_get_selected_text(doc
->editor
->sci
, text
);
820 { /* Get the current line */
821 gint line_num
= sci_get_current_line(doc
->editor
->sci
);
822 text
= sci_get_line(doc
->editor
->sci
, line_num
);
827 if (vc
->send_selection_unsafe
)
828 { /* Explicitly append a trailing newline character to get the command executed,
829 this is disabled by default as it could cause all sorts of damage. */
830 if (text
[len
-1] != '\n' && text
[len
-1] != '\r')
832 SETPTR(text
, g_strconcat(text
, "\n", NULL
));
837 { /* Make sure there is no newline character at the end to prevent unwanted execution */
838 while (text
[len
-1] == '\n' || text
[len
-1] == '\r')
845 vf
->vte_terminal_feed_child(VTE_TERMINAL(vc
->vte
), text
, len
);
848 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_VTE
);
849 gtk_widget_grab_focus(vc
->vte
);
850 msgwin_show_hide(TRUE
);