2 * vte.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-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 * 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) (GTK_CHECK_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 GtkType (*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 void 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 vte_info
.have_vte
= TRUE
;
220 vf
= g_new0(struct VteFunctions
, 1);
221 vte_register_symbols(module
);
226 /* setup the F10 menu override (so it works before the widget is first realised). */
231 static void on_vte_realize(void)
233 /* the vte widget has to be realised before color changes take effect */
234 vte_apply_user_settings();
236 vf
->vte_terminal_im_append_menuitems(VTE_TERMINAL(vc
->vte
), GTK_MENU_SHELL(vc
->im_submenu
));
240 static void create_vte(void)
242 GtkWidget
*vte
, *scrollbar
, *hbox
, *frame
;
244 vc
->vte
= vte
= vf
->vte_terminal_new();
245 scrollbar
= gtk_vscrollbar_new(GTK_ADJUSTMENT(VTE_TERMINAL(vte
)->adjustment
));
246 GTK_WIDGET_UNSET_FLAGS(scrollbar
, GTK_CAN_FOCUS
);
248 /* create menu now so copy/paste shortcuts work */
249 vc
->menu
= vte_create_popup_menu();
250 g_object_ref_sink(vc
->menu
);
252 frame
= gtk_frame_new(NULL
);
254 hbox
= gtk_hbox_new(FALSE
, 0);
255 gtk_container_add(GTK_CONTAINER(frame
), hbox
);
256 gtk_box_pack_start(GTK_BOX(hbox
), vte
, TRUE
, TRUE
, 0);
257 gtk_box_pack_start(GTK_BOX(hbox
), scrollbar
, FALSE
, FALSE
, 0);
259 /* set the default widget size first to prevent VTE expanding too much,
260 * sometimes causing the hscrollbar to be too big or out of view. */
261 gtk_widget_set_size_request(GTK_WIDGET(vte
), 10, 10);
262 vf
->vte_terminal_set_size(VTE_TERMINAL(vte
), 30, 1);
264 vf
->vte_terminal_set_mouse_autohide(VTE_TERMINAL(vte
), TRUE
);
265 vf
->vte_terminal_set_word_chars(VTE_TERMINAL(vte
), VTE_WORDCHARS
);
267 gtk_drag_dest_set(vte
, GTK_DEST_DEFAULT_ALL
,
268 dnd_targets
, G_N_ELEMENTS(dnd_targets
), GDK_ACTION_COPY
);
270 g_signal_connect(vte
, "child-exited", G_CALLBACK(vte_start
), NULL
);
271 g_signal_connect(vte
, "button-press-event", G_CALLBACK(vte_button_pressed
), NULL
);
272 g_signal_connect(vte
, "event", G_CALLBACK(vte_keypress_cb
), NULL
);
273 g_signal_connect(vte
, "key-release-event", G_CALLBACK(vte_keyrelease_cb
), NULL
);
274 g_signal_connect(vte
, "commit", G_CALLBACK(vte_commit_cb
), NULL
);
275 g_signal_connect(vte
, "motion-notify-event", G_CALLBACK(on_motion_event
), NULL
);
276 g_signal_connect(vte
, "drag-data-received", G_CALLBACK(vte_drag_data_received
), NULL
);
280 gtk_widget_show_all(frame
);
281 gtk_notebook_insert_page(GTK_NOTEBOOK(msgwindow
.notebook
), frame
, gtk_label_new(_("Terminal")), MSG_VTE
);
283 g_signal_connect_after(vte
, "realize", G_CALLBACK(on_vte_realize
), NULL
);
290 /* free the vte widget before unloading vte module
291 * this prevents a segfault on X close window if the message window is hidden */
292 gtk_widget_destroy(vc
->vte
);
293 gtk_widget_destroy(vc
->menu
);
294 g_object_unref(vc
->menu
);
295 g_free(vc
->emulation
);
298 g_free(vc
->colour_back
);
299 g_free(vc
->colour_fore
);
300 g_free(vc
->send_cmd_prefix
);
302 g_free(gtk_menu_key_accel
);
303 /* Don't unload the module explicitly because it causes a segfault on FreeBSD. The segfault
304 * happens when the app really exits, not directly on g_module_close(). This still needs to
305 * be investigated. */
306 /*g_module_close(module); */
310 static gboolean
vte_keyrelease_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
312 if (ui_is_keyval_enter_or_return(event
->keyval
) ||
313 ((event
->keyval
== GDK_c
) && (event
->state
& GDK_CONTROL_MASK
)))
315 /* assume any text on the prompt has been executed when pressing Enter/Return */
322 static gboolean
vte_keypress_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
324 if (vc
->enable_bash_keys
)
325 return FALSE
; /* Ctrl-[CD] will be handled by the VTE itself */
327 if (event
->type
!= GDK_KEY_RELEASE
)
330 if ((event
->keyval
== GDK_c
||
331 event
->keyval
== GDK_d
||
332 event
->keyval
== GDK_C
||
333 event
->keyval
== GDK_D
) &&
334 event
->state
& GDK_CONTROL_MASK
&&
335 ! (event
->state
& GDK_SHIFT_MASK
) && ! (event
->state
& GDK_MOD1_MASK
))
344 static void vte_commit_cb(VteTerminal
*vte
, gchar
*arg1
, guint arg2
, gpointer user_data
)
350 static void vte_start(GtkWidget
*widget
)
355 /* split the shell command line, so arguments will work too */
356 argv
= g_strsplit(vc
->shell
, " ", -1);
360 env
= vte_get_child_environment();
361 pid
= vf
->vte_terminal_fork_command(VTE_TERMINAL(widget
), argv
[0], argv
, env
,
362 vte_info
.dir
, TRUE
, TRUE
, TRUE
);
367 pid
= 0; /* use 0 as invalid pid */
373 static void vte_restart(GtkWidget
*widget
)
375 vte_get_working_directory(); /* try to keep the working directory when restarting the VTE */
381 vf
->vte_terminal_reset(VTE_TERMINAL(widget
), TRUE
, TRUE
);
386 static gboolean
vte_button_pressed(GtkWidget
*widget
, GdkEventButton
*event
, gpointer user_data
)
388 if (event
->button
== 3)
390 gtk_widget_grab_focus(vc
->vte
);
391 gtk_menu_popup(GTK_MENU(vc
->menu
), NULL
, NULL
, NULL
, NULL
, event
->button
, event
->time
);
397 static void vte_set_cursor_blink_mode(void)
399 if (vf
->vte_terminal_set_cursor_blink_mode
!= NULL
)
401 vf
->vte_terminal_set_cursor_blink_mode(VTE_TERMINAL(vc
->vte
),
402 (vc
->cursor_blinks
) ? VTE_CURSOR_BLINK_ON
: VTE_CURSOR_BLINK_OFF
);
405 vf
->vte_terminal_set_cursor_blinks(VTE_TERMINAL(vc
->vte
), vc
->cursor_blinks
);
409 static void vte_register_symbols(GModule
*mod
)
411 g_module_symbol(mod
, "vte_terminal_new", (void*)&vf
->vte_terminal_new
);
412 g_module_symbol(mod
, "vte_terminal_set_size", (void*)&vf
->vte_terminal_set_size
);
413 g_module_symbol(mod
, "vte_terminal_fork_command", (void*)&vf
->vte_terminal_fork_command
);
414 g_module_symbol(mod
, "vte_terminal_set_word_chars", (void*)&vf
->vte_terminal_set_word_chars
);
415 g_module_symbol(mod
, "vte_terminal_set_mouse_autohide", (void*)&vf
->vte_terminal_set_mouse_autohide
);
416 g_module_symbol(mod
, "vte_terminal_reset", (void*)&vf
->vte_terminal_reset
);
417 g_module_symbol(mod
, "vte_terminal_get_type", (void*)&vf
->vte_terminal_get_type
);
418 g_module_symbol(mod
, "vte_terminal_set_scroll_on_output", (void*)&vf
->vte_terminal_set_scroll_on_output
);
419 g_module_symbol(mod
, "vte_terminal_set_scroll_on_keystroke", (void*)&vf
->vte_terminal_set_scroll_on_keystroke
);
420 g_module_symbol(mod
, "vte_terminal_set_font_from_string", (void*)&vf
->vte_terminal_set_font_from_string
);
421 g_module_symbol(mod
, "vte_terminal_set_scrollback_lines", (void*)&vf
->vte_terminal_set_scrollback_lines
);
422 g_module_symbol(mod
, "vte_terminal_get_has_selection", (void*)&vf
->vte_terminal_get_has_selection
);
423 g_module_symbol(mod
, "vte_terminal_copy_clipboard", (void*)&vf
->vte_terminal_copy_clipboard
);
424 g_module_symbol(mod
, "vte_terminal_paste_clipboard", (void*)&vf
->vte_terminal_paste_clipboard
);
425 g_module_symbol(mod
, "vte_terminal_set_emulation", (void*)&vf
->vte_terminal_set_emulation
);
426 g_module_symbol(mod
, "vte_terminal_set_color_foreground", (void*)&vf
->vte_terminal_set_color_foreground
);
427 g_module_symbol(mod
, "vte_terminal_set_color_bold", (void*)&vf
->vte_terminal_set_color_bold
);
428 g_module_symbol(mod
, "vte_terminal_set_color_background", (void*)&vf
->vte_terminal_set_color_background
);
429 g_module_symbol(mod
, "vte_terminal_feed_child", (void*)&vf
->vte_terminal_feed_child
);
430 g_module_symbol(mod
, "vte_terminal_im_append_menuitems", (void*)&vf
->vte_terminal_im_append_menuitems
);
431 g_module_symbol(mod
, "vte_terminal_set_cursor_blink_mode", (void*)&vf
->vte_terminal_set_cursor_blink_mode
);
432 if (vf
->vte_terminal_set_cursor_blink_mode
== NULL
)
433 /* vte_terminal_set_cursor_blink_mode() is only available since 0.17.1, so if we don't find
434 * this symbol, we are probably on an older version and use the old API instead */
435 g_module_symbol(mod
, "vte_terminal_set_cursor_blinks", (void*)&vf
->vte_terminal_set_cursor_blinks
);
436 g_module_symbol(mod
, "vte_terminal_select_all", (void*)&vf
->vte_terminal_select_all
);
437 g_module_symbol(mod
, "vte_terminal_set_audible_bell", (void*)&vf
->vte_terminal_set_audible_bell
);
441 void vte_apply_user_settings(void)
443 if (! ui_prefs
.msgwindow_visible
)
446 vf
->vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->vte
), vc
->scrollback_lines
);
447 vf
->vte_terminal_set_scroll_on_keystroke(VTE_TERMINAL(vc
->vte
), vc
->scroll_on_key
);
448 vf
->vte_terminal_set_scroll_on_output(VTE_TERMINAL(vc
->vte
), vc
->scroll_on_out
);
449 vf
->vte_terminal_set_emulation(VTE_TERMINAL(vc
->vte
), vc
->emulation
);
450 vf
->vte_terminal_set_font_from_string(VTE_TERMINAL(vc
->vte
), vc
->font
);
451 vf
->vte_terminal_set_color_foreground(VTE_TERMINAL(vc
->vte
), vc
->colour_fore
);
452 vf
->vte_terminal_set_color_bold(VTE_TERMINAL(vc
->vte
), vc
->colour_fore
);
453 vf
->vte_terminal_set_color_background(VTE_TERMINAL(vc
->vte
), vc
->colour_back
);
454 vf
->vte_terminal_set_audible_bell(VTE_TERMINAL(vc
->vte
), prefs
.beep_on_errors
);
455 vte_set_cursor_blink_mode();
461 static void vte_popup_menu_clicked(GtkMenuItem
*menuitem
, gpointer user_data
)
463 switch (GPOINTER_TO_INT(user_data
))
467 if (vf
->vte_terminal_get_has_selection(VTE_TERMINAL(vc
->vte
)))
468 vf
->vte_terminal_copy_clipboard(VTE_TERMINAL(vc
->vte
));
473 vf
->vte_terminal_paste_clipboard(VTE_TERMINAL(vc
->vte
));
476 case POPUP_SELECTALL
:
481 case POPUP_CHANGEPATH
:
483 GeanyDocument
*doc
= document_get_current();
485 vte_cwd(doc
->file_name
, TRUE
);
488 case POPUP_RESTARTTERMINAL
:
490 vte_restart(vc
->vte
);
493 case POPUP_PREFERENCES
:
495 GtkWidget
*notebook
, *tab_page
;
499 notebook
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "notebook2");
500 tab_page
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "frame_term");
502 gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook
),
503 gtk_notebook_page_num(GTK_NOTEBOOK(notebook
), GTK_WIDGET(tab_page
)));
511 static GtkWidget
*vte_create_popup_menu(void)
513 GtkWidget
*menu
, *item
;
514 GtkAccelGroup
*accel_group
;
516 menu
= gtk_menu_new();
518 accel_group
= gtk_accel_group_new();
519 gtk_window_add_accel_group(GTK_WINDOW(main_widgets
.window
), accel_group
);
521 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_COPY
, NULL
);
522 gtk_widget_add_accelerator(item
, "activate", accel_group
,
523 GDK_c
, GDK_CONTROL_MASK
| GDK_SHIFT_MASK
, GTK_ACCEL_VISIBLE
);
524 gtk_widget_show(item
);
525 gtk_container_add(GTK_CONTAINER(menu
), item
);
526 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_COPY
));
528 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_PASTE
, NULL
);
529 gtk_widget_add_accelerator(item
, "activate", accel_group
,
530 GDK_v
, GDK_CONTROL_MASK
| GDK_SHIFT_MASK
, GTK_ACCEL_VISIBLE
);
531 gtk_widget_show(item
);
532 gtk_container_add(GTK_CONTAINER(menu
), item
);
533 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_PASTE
));
535 item
= gtk_separator_menu_item_new();
536 gtk_widget_show(item
);
537 gtk_container_add(GTK_CONTAINER(menu
), item
);
539 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_SELECT_ALL
, NULL
);
540 gtk_widget_show(item
);
541 gtk_container_add(GTK_CONTAINER(menu
), item
);
542 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_SELECTALL
));
544 item
= gtk_separator_menu_item_new();
545 gtk_widget_show(item
);
546 gtk_container_add(GTK_CONTAINER(menu
), item
);
548 item
= gtk_image_menu_item_new_with_mnemonic(_("_Set Path From Document"));
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_CHANGEPATH
));
553 item
= gtk_image_menu_item_new_with_mnemonic(_("_Restart Terminal"));
554 gtk_widget_show(item
);
555 gtk_container_add(GTK_CONTAINER(menu
), item
);
556 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_RESTARTTERMINAL
));
558 item
= gtk_separator_menu_item_new();
559 gtk_widget_show(item
);
560 gtk_container_add(GTK_CONTAINER(menu
), item
);
562 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES
, NULL
);
563 gtk_widget_show(item
);
564 gtk_container_add(GTK_CONTAINER(menu
), item
);
565 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_PREFERENCES
));
567 msgwin_menu_add_common_items(GTK_MENU(menu
));
569 item
= gtk_separator_menu_item_new();
570 gtk_widget_show(item
);
571 gtk_container_add(GTK_CONTAINER(menu
), item
);
573 /* the IM submenu should always be the last item to be consistent with other GTK popup menus */
574 vc
->im_submenu
= gtk_menu_new();
576 item
= gtk_image_menu_item_new_with_mnemonic(_("_Input Methods"));
577 gtk_widget_show(item
);
578 gtk_container_add(GTK_CONTAINER(menu
), item
);
580 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item
), vc
->im_submenu
);
581 /* submenu populated after vte realized */
586 /* If the command could be executed, TRUE is returned, FALSE otherwise (i.e. there was some text
588 gboolean
vte_send_cmd(const gchar
*cmd
)
592 vf
->vte_terminal_feed_child(VTE_TERMINAL(vc
->vte
), cmd
, strlen(cmd
));
593 clean
= TRUE
; /* vte_terminal_feed_child() also marks the vte as not clean */
601 /* Taken from Terminal by os-cillation: terminal_screen_get_working_directory, thanks.
602 * Determines the working directory using various OS-specific mechanisms and stores the determined
603 * directory in vte_info.dir. Note: vte_info.dir contains the real path. */
604 const gchar
*vte_get_working_directory(void)
606 gchar buffer
[4096 + 1];
613 file
= g_strdup_printf("/proc/%d/cwd", pid
);
614 length
= readlink(file
, buffer
, sizeof(buffer
));
616 if (length
> 0 && *buffer
== '/')
618 buffer
[length
] = '\0';
619 g_free(vte_info
.dir
);
620 vte_info
.dir
= g_strdup(buffer
);
622 else if (length
== 0)
624 cwd
= g_get_current_dir();
627 if (chdir(file
) == 0)
629 g_free(vte_info
.dir
);
630 vte_info
.dir
= g_get_current_dir();
632 geany_debug("%s: %s", G_STRFUNC
, g_strerror(errno
));
644 /* Changes the current working directory of the VTE to the path of the given filename.
645 * filename is expected to be in UTF-8 encoding.
646 * filename can also be a path, then it is used directly.
647 * If force is set to TRUE, it will always change the cwd
649 void vte_cwd(const gchar
*filename
, gboolean force
)
651 if (vte_info
.have_vte
&& (vc
->follow_path
|| force
) &&
652 filename
!= NULL
&& g_path_is_absolute(filename
))
656 if (g_file_test(filename
, G_FILE_TEST_IS_DIR
))
657 path
= g_strdup(filename
);
659 path
= g_path_get_dirname(filename
);
661 vte_get_working_directory(); /* refresh vte_info.dir */
662 if (! utils_str_equal(path
, vte_info
.dir
))
664 /* use g_shell_quote to avoid problems with spaces, '!' or something else in path */
665 gchar
*quoted_path
= g_shell_quote(path
);
666 gchar
*cmd
= g_strconcat(vc
->send_cmd_prefix
, "cd ", quoted_path
, "\n", NULL
);
667 if (! vte_send_cmd(cmd
))
669 ui_set_statusbar(FALSE
,
670 _("Could not change the directory in the VTE because it probably contains a command."));
672 "Could not change the directory in the VTE because it probably contains a command.");
682 static void vte_drag_data_received(GtkWidget
*widget
, GdkDragContext
*drag_context
,
683 gint x
, gint y
, GtkSelectionData
*data
, guint info
, guint ltime
)
685 if (info
== TARGET_TEXT_PLAIN
)
687 if (data
->format
== 8 && data
->length
> 0)
688 vf
->vte_terminal_feed_child(VTE_TERMINAL(widget
),
689 (const gchar
*) data
->data
, data
->length
);
693 gchar
*text
= (gchar
*) gtk_selection_data_get_text(data
);
695 vf
->vte_terminal_feed_child(VTE_TERMINAL(widget
), text
, strlen(text
));
698 gtk_drag_finish(drag_context
, TRUE
, FALSE
, ltime
);
702 G_MODULE_EXPORT
void on_check_run_in_vte_toggled(GtkToggleButton
*togglebutton
, GtkWidget
*user_data
)
704 g_return_if_fail(GTK_IS_WIDGET(user_data
));
705 gtk_widget_set_sensitive(user_data
, gtk_toggle_button_get_active(togglebutton
));
709 G_MODULE_EXPORT
void on_term_font_set(GtkFontButton
*widget
, gpointer user_data
)
711 const gchar
*fontbtn
= gtk_font_button_get_font_name(widget
);
713 if (! utils_str_equal(fontbtn
, vc
->font
))
715 SETPTR(vc
->font
, g_strdup(gtk_font_button_get_font_name(widget
)));
716 vte_apply_user_settings();
721 G_MODULE_EXPORT
void on_term_fg_color_set(GtkColorButton
*widget
, gpointer user_data
)
723 g_free(vc
->colour_fore
);
724 vc
->colour_fore
= g_new0(GdkColor
, 1);
725 gtk_color_button_get_color(widget
, vc
->colour_fore
);
729 G_MODULE_EXPORT
void on_term_bg_color_set(GtkColorButton
*widget
, gpointer user_data
)
731 g_free(vc
->colour_back
);
732 vc
->colour_back
= g_new0(GdkColor
, 1);
733 gtk_color_button_get_color(widget
, vc
->colour_back
);
737 void vte_append_preferences_tab(void)
739 if (vte_info
.have_vte
)
741 GtkWidget
*frame_term
, *button_shell
, *entry_shell
;
742 GtkWidget
*check_run_in_vte
, *check_skip_script
;
743 GtkWidget
*font_button
, *fg_color_button
, *bg_color_button
;
745 button_shell
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "button_term_shell"));
746 entry_shell
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "entry_shell"));
747 ui_setup_open_button_callback(button_shell
, NULL
,
748 GTK_FILE_CHOOSER_ACTION_OPEN
, GTK_ENTRY(entry_shell
));
750 check_skip_script
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "check_skip_script"));
751 gtk_widget_set_sensitive(check_skip_script
, vc
->run_in_vte
);
753 check_run_in_vte
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "check_run_in_vte"));
754 g_signal_connect(G_OBJECT(check_run_in_vte
), "toggled",
755 G_CALLBACK(on_check_run_in_vte_toggled
), check_skip_script
);
757 font_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "font_term");
758 g_signal_connect(font_button
, "font-set", G_CALLBACK(on_term_font_set
), NULL
);
760 fg_color_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "color_fore");
761 g_signal_connect(fg_color_button
, "color-set", G_CALLBACK(on_term_fg_color_set
), NULL
);
763 bg_color_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "color_back");
764 g_signal_connect(bg_color_button
, "color-set", G_CALLBACK(on_term_bg_color_set
), NULL
);
766 frame_term
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "frame_term");
767 gtk_widget_show_all(frame_term
);
772 void vte_select_all(void)
774 if (vf
->vte_terminal_select_all
!= NULL
)
775 vf
->vte_terminal_select_all(VTE_TERMINAL(vc
->vte
));
779 void vte_send_selection_to_vte(void)
785 doc
= document_get_current();
786 g_return_if_fail(doc
!= NULL
);
788 if (sci_has_selection(doc
->editor
->sci
))
790 text
= g_malloc0(sci_get_selected_text_length(doc
->editor
->sci
) + 1);
791 sci_get_selected_text(doc
->editor
->sci
, text
);
794 { /* Get the current line */
795 gint line_num
= sci_get_current_line(doc
->editor
->sci
);
796 text
= sci_get_line(doc
->editor
->sci
, line_num
);
801 if (vc
->send_selection_unsafe
)
802 { /* Explicitly append a trailing newline character to get the command executed,
803 this is disabled by default as it could cause all sorts of damage. */
804 if (text
[len
-1] != '\n' && text
[len
-1] != '\r')
806 SETPTR(text
, g_strconcat(text
, "\n", NULL
));
811 { /* Make sure there is no newline character at the end to prevent unwanted execution */
812 while (text
[len
-1] == '\n' || text
[len
-1] == '\r')
819 vf
->vte_terminal_feed_child(VTE_TERMINAL(vc
->vte
), text
, len
);
822 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_VTE
);
823 gtk_widget_grab_focus(vc
->vte
);
824 msgwin_show_hide(TRUE
);