VTE: drop support for selecting the emulated terminal
[geany-mirror.git] / src / vte.c
blob34591035c2f1490f0f2ffa072104c0f7f2379ce0
1 /*
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.
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #ifdef HAVE_VTE
32 #include "vte.h"
34 #include "callbacks.h"
35 #include "document.h"
36 #include "geanyobject.h"
37 #include "msgwindow.h"
38 #include "prefs.h"
39 #include "sciwrappers.h"
40 #include "support.h"
41 #include "ui_utils.h"
42 #include "utils.h"
43 #include "keybindings.h"
45 #include "gtkcompat.h"
47 /* include stdlib.h AND unistd.h, because on GNU/Linux pid_t seems to be
48 * in stdlib.h, on FreeBSD in unistd.h, sys/types.h is needed for C89 */
49 #include <stdlib.h>
50 #include <sys/types.h>
51 #include <unistd.h>
53 #include <gdk/gdkkeysyms.h>
54 #include <signal.h>
55 #include <string.h>
56 #include <errno.h>
59 VteInfo vte_info = { FALSE, FALSE, FALSE, NULL, NULL };
60 VteConfig *vc;
62 static pid_t pid = 0;
63 static gboolean clean = TRUE;
64 static GModule *module = NULL;
65 static struct VteFunctions *vf;
66 static gchar *gtk_menu_key_accel = NULL;
67 static GtkWidget *terminal_label = NULL;
68 static guint terminal_label_update_source = 0;
70 /* use vte wordchars to select paths */
71 static const gchar VTE_WORDCHARS[] = "-A-Za-z0-9,./?%&#:_";
74 /* Incomplete VteTerminal struct from vte/vte.h. */
75 typedef struct _VteTerminal VteTerminal;
76 struct _VteTerminal
78 GtkWidget widget;
79 GtkAdjustment *adjustment;
82 #define VTE_TERMINAL(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), VTE_TYPE_TERMINAL, VteTerminal))
83 #define VTE_TYPE_TERMINAL (vf->vte_terminal_get_type())
85 typedef enum {
86 VTE_CURSOR_BLINK_SYSTEM,
87 VTE_CURSOR_BLINK_ON,
88 VTE_CURSOR_BLINK_OFF
89 } VteTerminalCursorBlinkMode;
92 /* Holds function pointers we need to access the VTE API. */
93 struct VteFunctions
95 GtkWidget* (*vte_terminal_new) (void);
96 pid_t (*vte_terminal_fork_command) (VteTerminal *terminal, const char *command, char **argv,
97 char **envv, const char *directory, gboolean lastlog,
98 gboolean utmp, gboolean wtmp);
99 void (*vte_terminal_set_size) (VteTerminal *terminal, glong columns, glong rows);
100 void (*vte_terminal_set_word_chars) (VteTerminal *terminal, const char *spec);
101 void (*vte_terminal_set_mouse_autohide) (VteTerminal *terminal, gboolean setting);
102 void (*vte_terminal_reset) (VteTerminal *terminal, gboolean full, gboolean clear_history);
103 GType (*vte_terminal_get_type) (void);
104 void (*vte_terminal_set_scroll_on_output) (VteTerminal *terminal, gboolean scroll);
105 void (*vte_terminal_set_scroll_on_keystroke) (VteTerminal *terminal, gboolean scroll);
106 void (*vte_terminal_set_font) (VteTerminal *terminal, const PangoFontDescription *font_desc);
107 void (*vte_terminal_set_scrollback_lines) (VteTerminal *terminal, glong lines);
108 gboolean (*vte_terminal_get_has_selection) (VteTerminal *terminal);
109 void (*vte_terminal_copy_clipboard) (VteTerminal *terminal);
110 void (*vte_terminal_paste_clipboard) (VteTerminal *terminal);
111 void (*vte_terminal_set_color_foreground) (VteTerminal *terminal, const GdkColor *foreground);
112 void (*vte_terminal_set_color_bold) (VteTerminal *terminal, const GdkColor *foreground);
113 void (*vte_terminal_set_color_background) (VteTerminal *terminal, const GdkColor *background);
114 void (*vte_terminal_feed_child) (VteTerminal *terminal, const char *data, glong length);
115 void (*vte_terminal_im_append_menuitems) (VteTerminal *terminal, GtkMenuShell *menushell);
116 void (*vte_terminal_set_cursor_blink_mode) (VteTerminal *terminal,
117 VteTerminalCursorBlinkMode mode);
118 void (*vte_terminal_set_cursor_blinks) (VteTerminal *terminal, gboolean blink);
119 void (*vte_terminal_select_all) (VteTerminal *terminal);
120 void (*vte_terminal_set_audible_bell) (VteTerminal *terminal, gboolean is_audible);
121 void (*vte_terminal_set_background_image_file) (VteTerminal *terminal, const char *path);
122 GtkAdjustment* (*vte_terminal_get_adjustment) (VteTerminal *terminal);
126 static void create_vte(void);
127 static void vte_start(GtkWidget *widget);
128 static void vte_restart(GtkWidget *widget);
129 static gboolean vte_button_pressed(GtkWidget *widget, GdkEventButton *event, gpointer user_data);
130 static gboolean vte_keyrelease_cb(GtkWidget *widget, GdkEventKey *event, gpointer data);
131 static gboolean vte_keypress_cb(GtkWidget *widget, GdkEventKey *event, gpointer data);
132 static gboolean vte_register_symbols(GModule *module);
133 static void vte_popup_menu_clicked(GtkMenuItem *menuitem, gpointer user_data);
134 static GtkWidget *vte_create_popup_menu(void);
135 static void vte_commit_cb(VteTerminal *vte, gchar *arg1, guint arg2, gpointer user_data);
136 static void vte_drag_data_received(GtkWidget *widget, GdkDragContext *drag_context,
137 gint x, gint y, GtkSelectionData *data, guint info, guint ltime);
140 enum
142 POPUP_COPY,
143 POPUP_PASTE,
144 POPUP_SELECTALL,
145 POPUP_CHANGEPATH,
146 POPUP_RESTARTTERMINAL,
147 POPUP_PREFERENCES,
148 TARGET_UTF8_STRING = 0,
149 TARGET_TEXT,
150 TARGET_COMPOUND_TEXT,
151 TARGET_STRING,
152 TARGET_TEXT_PLAIN
155 static const GtkTargetEntry dnd_targets[] =
157 { "UTF8_STRING", 0, TARGET_UTF8_STRING },
158 { "TEXT", 0, TARGET_TEXT },
159 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT },
160 { "STRING", 0, TARGET_STRING },
161 { "text/plain", 0, TARGET_TEXT_PLAIN },
165 /* replacement for vte_terminal_get_adjustment() when it's not available */
166 static GtkAdjustment *default_vte_terminal_get_adjustment(VteTerminal *vte)
168 #if GTK_CHECK_VERSION(3, 0, 0)
169 if (GTK_IS_SCROLLABLE(vte))
170 return gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(vte));
171 #endif
172 /* this is only valid in < 0.38, 0.38 broke ABI */
173 return vte->adjustment;
177 static gchar **vte_get_child_environment(void)
179 const gchar *exclude_vars[] = {"COLUMNS", "LINES", "TERM", "TERM_PROGRAM", NULL};
181 return utils_copy_environment(exclude_vars, "TERM", "xterm", NULL);
185 static void override_menu_key(void)
187 if (gtk_menu_key_accel == NULL) /* for restoring the default value */
188 g_object_get(G_OBJECT(gtk_settings_get_default()),
189 "gtk-menu-bar-accel", &gtk_menu_key_accel, NULL);
191 if (vc->ignore_menu_bar_accel)
192 gtk_settings_set_string_property(gtk_settings_get_default(),
193 "gtk-menu-bar-accel", "<Shift><Control><Mod1><Mod2><Mod3><Mod4><Mod5>F10", "Geany");
194 else
195 gtk_settings_set_string_property(gtk_settings_get_default(),
196 "gtk-menu-bar-accel", gtk_menu_key_accel, "Geany");
200 static void on_startup_complete(G_GNUC_UNUSED GObject *dummy)
202 GeanyDocument *doc = document_get_current();
204 if (doc)
205 vte_cwd((doc->real_path != NULL) ? doc->real_path : doc->file_name, FALSE);
209 void vte_init(void)
211 if (vte_info.have_vte == FALSE)
212 { /* vte_info.have_vte can be false even if VTE is compiled in, think of command line option */
213 geany_debug("Disabling terminal support");
214 return;
217 if (!EMPTY(vte_info.lib_vte))
219 module = g_module_open(vte_info.lib_vte, G_MODULE_BIND_LAZY);
221 #ifdef VTE_MODULE_PATH
222 else
224 module = g_module_open(VTE_MODULE_PATH, G_MODULE_BIND_LAZY);
226 #endif
228 if (module == NULL)
230 gint i;
231 const gchar *sonames[] = {
232 #if GTK_CHECK_VERSION(3, 0, 0)
233 "libvte2_90.so", "libvte2_90.so.9", "libvte2_90.dylib",
234 #else
235 "libvte.so", "libvte.so.4", "libvte.so.8", "libvte.so.9", "libvte.dylib",
236 #endif
237 NULL
240 for (i = 0; sonames[i] != NULL && module == NULL; i++)
242 module = g_module_open(sonames[i], G_MODULE_BIND_LAZY);
246 if (module == NULL)
248 vte_info.have_vte = FALSE;
249 geany_debug("Could not load libvte.so, embedded terminal support disabled");
250 return;
252 else
254 vf = g_new0(struct VteFunctions, 1);
255 if (vte_register_symbols(module))
256 vte_info.have_vte = TRUE;
257 else
259 vte_info.have_vte = FALSE;
260 g_free(vf);
261 /* FIXME: is closing the module safe? see vte_close() and test on FreeBSD */
262 /*g_module_close(module);*/
263 module = NULL;
264 return;
268 create_vte();
270 /* setup the F10 menu override (so it works before the widget is first realised). */
271 override_menu_key();
273 g_signal_connect(geany_object, "geany-startup-complete", G_CALLBACK(on_startup_complete), NULL);
277 static void on_vte_realize(void)
279 /* the vte widget has to be realised before color changes take effect */
280 vte_apply_user_settings();
282 vf->vte_terminal_im_append_menuitems(VTE_TERMINAL(vc->vte), GTK_MENU_SHELL(vc->im_submenu));
286 static gboolean vte_start_idle(G_GNUC_UNUSED gpointer user_data)
288 vte_start(vc->vte);
289 return FALSE;
293 static void create_vte(void)
295 GtkWidget *vte, *scrollbar, *hbox;
297 vc->vte = vte = vf->vte_terminal_new();
298 scrollbar = gtk_vscrollbar_new(vf->vte_terminal_get_adjustment(VTE_TERMINAL(vte)));
299 gtk_widget_set_can_focus(scrollbar, FALSE);
301 /* create menu now so copy/paste shortcuts work */
302 vc->menu = vte_create_popup_menu();
303 g_object_ref_sink(vc->menu);
305 hbox = gtk_hbox_new(FALSE, 0);
306 gtk_box_pack_start(GTK_BOX(hbox), vte, TRUE, TRUE, 0);
307 gtk_box_pack_start(GTK_BOX(hbox), scrollbar, FALSE, FALSE, 0);
309 /* set the default widget size first to prevent VTE expanding too much,
310 * sometimes causing the hscrollbar to be too big or out of view. */
311 gtk_widget_set_size_request(GTK_WIDGET(vte), 10, 10);
312 vf->vte_terminal_set_size(VTE_TERMINAL(vte), 30, 1);
314 vf->vte_terminal_set_mouse_autohide(VTE_TERMINAL(vte), TRUE);
315 vf->vte_terminal_set_word_chars(VTE_TERMINAL(vte), VTE_WORDCHARS);
317 gtk_drag_dest_set(vte, GTK_DEST_DEFAULT_ALL,
318 dnd_targets, G_N_ELEMENTS(dnd_targets), GDK_ACTION_COPY);
320 g_signal_connect(vte, "child-exited", G_CALLBACK(vte_start), NULL);
321 g_signal_connect(vte, "button-press-event", G_CALLBACK(vte_button_pressed), NULL);
322 g_signal_connect(vte, "event", G_CALLBACK(vte_keypress_cb), NULL);
323 g_signal_connect(vte, "key-release-event", G_CALLBACK(vte_keyrelease_cb), NULL);
324 g_signal_connect(vte, "commit", G_CALLBACK(vte_commit_cb), NULL);
325 g_signal_connect(vte, "motion-notify-event", G_CALLBACK(on_motion_event), NULL);
326 g_signal_connect(vte, "drag-data-received", G_CALLBACK(vte_drag_data_received), NULL);
328 /* start shell on idle otherwise the initial prompt can get corrupted */
329 g_idle_add(vte_start_idle, NULL);
331 gtk_widget_show_all(hbox);
332 terminal_label = gtk_label_new(_("Terminal"));
333 gtk_notebook_insert_page(GTK_NOTEBOOK(msgwindow.notebook), hbox, terminal_label, MSG_VTE);
335 g_signal_connect_after(vte, "realize", G_CALLBACK(on_vte_realize), NULL);
339 void vte_close(void)
341 g_free(vf);
342 /* free the vte widget before unloading vte module
343 * this prevents a segfault on X close window if the message window is hidden */
344 gtk_widget_destroy(vc->vte);
345 gtk_widget_destroy(vc->menu);
346 g_object_unref(vc->menu);
347 g_free(vc->shell);
348 g_free(vc->image);
349 g_free(vc->font);
350 g_free(vc->send_cmd_prefix);
351 g_free(vc);
352 g_free(gtk_menu_key_accel);
353 /* Don't unload the module explicitly because it causes a segfault on FreeBSD. The segfault
354 * happens when the app really exits, not directly on g_module_close(). This still needs to
355 * be investigated. */
356 /*g_module_close(module); */
360 static gboolean set_dirty_idle(gpointer user_data)
362 gtk_widget_set_name(terminal_label, "geany-terminal-dirty");
363 terminal_label_update_source = 0;
364 return FALSE;
368 static void set_clean(gboolean value)
370 if (clean != value)
372 if (terminal_label)
374 if (terminal_label_update_source > 0)
376 g_source_remove(terminal_label_update_source);
377 terminal_label_update_source = 0;
379 if (value)
380 gtk_widget_set_name(terminal_label, NULL);
381 else
382 terminal_label_update_source = g_timeout_add(150, set_dirty_idle, NULL);
384 clean = value;
389 static gboolean vte_keyrelease_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
391 if (ui_is_keyval_enter_or_return(event->keyval) ||
392 ((event->keyval == GDK_c) && (event->state & GDK_CONTROL_MASK)))
394 /* assume any text on the prompt has been executed when pressing Enter/Return */
395 set_clean(TRUE);
397 return FALSE;
401 static gboolean vte_keypress_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
403 if (vc->enable_bash_keys)
404 return FALSE; /* Ctrl-[CD] will be handled by the VTE itself */
406 if (event->type != GDK_KEY_RELEASE)
407 return FALSE;
409 if ((event->keyval == GDK_c ||
410 event->keyval == GDK_d ||
411 event->keyval == GDK_C ||
412 event->keyval == GDK_D) &&
413 event->state & GDK_CONTROL_MASK &&
414 ! (event->state & GDK_SHIFT_MASK) && ! (event->state & GDK_MOD1_MASK))
416 vte_restart(widget);
417 return TRUE;
419 return FALSE;
423 static void vte_commit_cb(VteTerminal *vte, gchar *arg1, guint arg2, gpointer user_data)
425 set_clean(FALSE);
429 static void vte_start(GtkWidget *widget)
431 /* split the shell command line, so arguments will work too */
432 gchar **argv = g_strsplit(vc->shell, " ", -1);
434 if (argv != NULL)
436 gchar **env = vte_get_child_environment();
438 pid = vf->vte_terminal_fork_command(VTE_TERMINAL(widget), argv[0], argv, env,
439 vte_info.dir, TRUE, TRUE, TRUE);
440 g_strfreev(env);
441 g_strfreev(argv);
443 else
444 pid = 0; /* use 0 as invalid pid */
446 set_clean(TRUE);
450 static void vte_restart(GtkWidget *widget)
452 vte_get_working_directory(); /* try to keep the working directory when restarting the VTE */
453 if (pid > 0)
455 kill(pid, SIGINT);
456 pid = 0;
458 vf->vte_terminal_reset(VTE_TERMINAL(widget), TRUE, TRUE);
459 set_clean(TRUE);
463 static gboolean vte_button_pressed(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
465 if (event->button == 3)
467 gtk_widget_grab_focus(vc->vte);
468 gtk_menu_popup(GTK_MENU(vc->menu), NULL, NULL, NULL, NULL, event->button, event->time);
470 else if (event->button == 2)
472 gtk_widget_grab_focus(widget);
474 return FALSE;
478 static void vte_set_cursor_blink_mode(void)
480 if (vf->vte_terminal_set_cursor_blink_mode != NULL)
481 /* vte >= 0.17.1 */
482 vf->vte_terminal_set_cursor_blink_mode(VTE_TERMINAL(vc->vte),
483 (vc->cursor_blinks) ? VTE_CURSOR_BLINK_ON : VTE_CURSOR_BLINK_OFF);
484 else
485 /* vte < 0.17.1 */
486 vf->vte_terminal_set_cursor_blinks(VTE_TERMINAL(vc->vte), vc->cursor_blinks);
490 static gboolean vte_register_symbols(GModule *mod)
492 #define BIND_SYMBOL(field) \
493 g_module_symbol(mod, #field, (void*)&vf->field)
494 #define BIND_REQUIRED_SYMBOL(field) \
495 G_STMT_START { \
496 if (! BIND_SYMBOL(field)) \
498 g_critical(_("invalid VTE library \"%s\": missing symbol \"%s\""), \
499 g_module_name(mod), #field); \
500 return FALSE; \
502 } G_STMT_END
504 BIND_REQUIRED_SYMBOL(vte_terminal_new);
505 BIND_REQUIRED_SYMBOL(vte_terminal_set_size);
506 BIND_REQUIRED_SYMBOL(vte_terminal_fork_command);
507 BIND_REQUIRED_SYMBOL(vte_terminal_set_word_chars);
508 BIND_REQUIRED_SYMBOL(vte_terminal_set_mouse_autohide);
509 BIND_REQUIRED_SYMBOL(vte_terminal_reset);
510 BIND_REQUIRED_SYMBOL(vte_terminal_get_type);
511 BIND_REQUIRED_SYMBOL(vte_terminal_set_scroll_on_output);
512 BIND_REQUIRED_SYMBOL(vte_terminal_set_scroll_on_keystroke);
513 BIND_REQUIRED_SYMBOL(vte_terminal_set_font);
514 BIND_REQUIRED_SYMBOL(vte_terminal_set_scrollback_lines);
515 BIND_REQUIRED_SYMBOL(vte_terminal_get_has_selection);
516 BIND_REQUIRED_SYMBOL(vte_terminal_copy_clipboard);
517 BIND_REQUIRED_SYMBOL(vte_terminal_paste_clipboard);
518 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_foreground);
519 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_bold);
520 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_background);
521 BIND_REQUIRED_SYMBOL(vte_terminal_set_background_image_file);
522 BIND_REQUIRED_SYMBOL(vte_terminal_feed_child);
523 BIND_REQUIRED_SYMBOL(vte_terminal_im_append_menuitems);
524 if (! BIND_SYMBOL(vte_terminal_set_cursor_blink_mode))
525 /* vte_terminal_set_cursor_blink_mode() is only available since 0.17.1, so if we don't find
526 * this symbol, we are probably on an older version and use the old API instead */
527 BIND_REQUIRED_SYMBOL(vte_terminal_set_cursor_blinks);
528 BIND_REQUIRED_SYMBOL(vte_terminal_select_all);
529 BIND_REQUIRED_SYMBOL(vte_terminal_set_audible_bell);
530 if (! BIND_SYMBOL(vte_terminal_get_adjustment))
531 /* vte_terminal_get_adjustment() is available since 0.9 and removed in 0.38 */
532 vf->vte_terminal_get_adjustment = default_vte_terminal_get_adjustment;
534 #undef BIND_REQUIRED_SYMBOL
535 #undef BIND_SYMBOL
537 return TRUE;
541 void vte_apply_user_settings(void)
543 PangoFontDescription *font_desc;
545 if (! ui_prefs.msgwindow_visible)
546 return;
548 vf->vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte), vc->scrollback_lines);
549 vf->vte_terminal_set_scroll_on_keystroke(VTE_TERMINAL(vc->vte), vc->scroll_on_key);
550 vf->vte_terminal_set_scroll_on_output(VTE_TERMINAL(vc->vte), vc->scroll_on_out);
551 font_desc = pango_font_description_from_string(vc->font);
552 vf->vte_terminal_set_font(VTE_TERMINAL(vc->vte), font_desc);
553 pango_font_description_free(font_desc);
554 vf->vte_terminal_set_color_foreground(VTE_TERMINAL(vc->vte), &vc->colour_fore);
555 vf->vte_terminal_set_color_bold(VTE_TERMINAL(vc->vte), &vc->colour_fore);
556 vf->vte_terminal_set_color_background(VTE_TERMINAL(vc->vte), &vc->colour_back);
557 vf->vte_terminal_set_background_image_file(VTE_TERMINAL(vc->vte), vc->image);
558 vf->vte_terminal_set_audible_bell(VTE_TERMINAL(vc->vte), prefs.beep_on_errors);
559 vte_set_cursor_blink_mode();
561 override_menu_key();
565 static void vte_popup_menu_clicked(GtkMenuItem *menuitem, gpointer user_data)
567 switch (GPOINTER_TO_INT(user_data))
569 case POPUP_COPY:
571 if (vf->vte_terminal_get_has_selection(VTE_TERMINAL(vc->vte)))
572 vf->vte_terminal_copy_clipboard(VTE_TERMINAL(vc->vte));
573 break;
575 case POPUP_PASTE:
577 vf->vte_terminal_paste_clipboard(VTE_TERMINAL(vc->vte));
578 break;
580 case POPUP_SELECTALL:
582 vte_select_all();
583 break;
585 case POPUP_CHANGEPATH:
587 GeanyDocument *doc = document_get_current();
588 if (doc != NULL)
589 vte_cwd(doc->file_name, TRUE);
590 break;
592 case POPUP_RESTARTTERMINAL:
594 vte_restart(vc->vte);
595 break;
597 case POPUP_PREFERENCES:
599 GtkWidget *notebook, *tab_page;
601 prefs_show_dialog();
603 notebook = ui_lookup_widget(ui_widgets.prefs_dialog, "notebook2");
604 tab_page = ui_lookup_widget(ui_widgets.prefs_dialog, "frame_term");
606 gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook),
607 gtk_notebook_page_num(GTK_NOTEBOOK(notebook), GTK_WIDGET(tab_page)));
609 break;
615 static GtkWidget *vte_create_popup_menu(void)
617 GtkWidget *menu, *item;
618 GtkAccelGroup *accel_group;
620 menu = gtk_menu_new();
622 accel_group = gtk_accel_group_new();
623 gtk_window_add_accel_group(GTK_WINDOW(main_widgets.window), accel_group);
625 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_COPY, NULL);
626 gtk_widget_add_accelerator(item, "activate", accel_group,
627 GDK_c, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, GTK_ACCEL_VISIBLE);
628 gtk_widget_show(item);
629 gtk_container_add(GTK_CONTAINER(menu), item);
630 g_signal_connect(item, "activate", G_CALLBACK(vte_popup_menu_clicked), GINT_TO_POINTER(POPUP_COPY));
632 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_PASTE, NULL);
633 gtk_widget_add_accelerator(item, "activate", accel_group,
634 GDK_v, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, GTK_ACCEL_VISIBLE);
635 gtk_widget_show(item);
636 gtk_container_add(GTK_CONTAINER(menu), item);
637 g_signal_connect(item, "activate", G_CALLBACK(vte_popup_menu_clicked), GINT_TO_POINTER(POPUP_PASTE));
639 item = gtk_separator_menu_item_new();
640 gtk_widget_show(item);
641 gtk_container_add(GTK_CONTAINER(menu), item);
643 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_SELECT_ALL, NULL);
644 gtk_widget_show(item);
645 gtk_container_add(GTK_CONTAINER(menu), item);
646 g_signal_connect(item, "activate", G_CALLBACK(vte_popup_menu_clicked), GINT_TO_POINTER(POPUP_SELECTALL));
648 item = gtk_separator_menu_item_new();
649 gtk_widget_show(item);
650 gtk_container_add(GTK_CONTAINER(menu), item);
652 item = gtk_image_menu_item_new_with_mnemonic(_("_Set Path From Document"));
653 gtk_widget_show(item);
654 gtk_container_add(GTK_CONTAINER(menu), item);
655 g_signal_connect(item, "activate", G_CALLBACK(vte_popup_menu_clicked), GINT_TO_POINTER(POPUP_CHANGEPATH));
657 item = gtk_image_menu_item_new_with_mnemonic(_("_Restart Terminal"));
658 gtk_widget_show(item);
659 gtk_container_add(GTK_CONTAINER(menu), item);
660 g_signal_connect(item, "activate", G_CALLBACK(vte_popup_menu_clicked), GINT_TO_POINTER(POPUP_RESTARTTERMINAL));
662 item = gtk_separator_menu_item_new();
663 gtk_widget_show(item);
664 gtk_container_add(GTK_CONTAINER(menu), item);
666 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES, NULL);
667 gtk_widget_show(item);
668 gtk_container_add(GTK_CONTAINER(menu), item);
669 g_signal_connect(item, "activate", G_CALLBACK(vte_popup_menu_clicked), GINT_TO_POINTER(POPUP_PREFERENCES));
671 msgwin_menu_add_common_items(GTK_MENU(menu));
673 item = gtk_separator_menu_item_new();
674 gtk_widget_show(item);
675 gtk_container_add(GTK_CONTAINER(menu), item);
677 /* the IM submenu should always be the last item to be consistent with other GTK popup menus */
678 vc->im_submenu = gtk_menu_new();
680 item = gtk_image_menu_item_new_with_mnemonic(_("_Input Methods"));
681 gtk_widget_show(item);
682 gtk_container_add(GTK_CONTAINER(menu), item);
684 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), vc->im_submenu);
685 /* submenu populated after vte realized */
686 return menu;
690 /* If the command could be executed, TRUE is returned, FALSE otherwise (i.e. there was some text
691 * on the prompt). */
692 gboolean vte_send_cmd(const gchar *cmd)
694 if (clean)
696 vf->vte_terminal_feed_child(VTE_TERMINAL(vc->vte), cmd, strlen(cmd));
697 set_clean(TRUE); /* vte_terminal_feed_child() also marks the vte as not clean */
698 return TRUE;
700 else
701 return FALSE;
705 /* Taken from Terminal by os-cillation: terminal_screen_get_working_directory, thanks.
706 * Determines the working directory using various OS-specific mechanisms and stores the determined
707 * directory in vte_info.dir. Note: vte_info.dir contains the real path. */
708 const gchar *vte_get_working_directory(void)
710 if (pid > 0)
712 gchar buffer[4096 + 1];
713 gchar *file = g_strdup_printf("/proc/%d/cwd", pid);
714 gint length = readlink(file, buffer, sizeof(buffer));
716 if (length > 0 && *buffer == '/')
718 buffer[length] = '\0';
719 g_free(vte_info.dir);
720 vte_info.dir = g_strdup(buffer);
722 else if (length == 0)
724 gchar *cwd = g_get_current_dir();
726 if (cwd != NULL)
728 if (chdir(file) == 0)
730 g_free(vte_info.dir);
731 vte_info.dir = g_get_current_dir();
732 if (chdir(cwd) != 0)
733 geany_debug("%s: %s", G_STRFUNC, g_strerror(errno));
735 g_free(cwd);
738 g_free(file);
741 return vte_info.dir;
745 /* Changes the current working directory of the VTE to the path of the given filename.
746 * filename is expected to be in UTF-8 encoding.
747 * filename can also be a path, then it is used directly.
748 * If force is set to TRUE, it will always change the cwd
750 void vte_cwd(const gchar *filename, gboolean force)
752 if (vte_info.have_vte && (vc->follow_path || force) &&
753 filename != NULL && g_path_is_absolute(filename))
755 gchar *path;
757 if (g_file_test(filename, G_FILE_TEST_IS_DIR))
758 path = g_strdup(filename);
759 else
760 path = g_path_get_dirname(filename);
762 vte_get_working_directory(); /* refresh vte_info.dir */
763 if (! utils_str_equal(path, vte_info.dir))
765 /* use g_shell_quote to avoid problems with spaces, '!' or something else in path */
766 gchar *quoted_path = g_shell_quote(path);
767 gchar *cmd = g_strconcat(vc->send_cmd_prefix, "cd ", quoted_path, "\n", NULL);
768 if (! vte_send_cmd(cmd))
770 const gchar *msg = _("Directory not changed because the terminal may contain some input (press Ctrl+C or Enter to clear it).");
771 ui_set_statusbar(FALSE, "%s", msg);
772 geany_debug("%s", msg);
774 g_free(quoted_path);
775 g_free(cmd);
777 g_free(path);
782 static void vte_drag_data_received(GtkWidget *widget, GdkDragContext *drag_context,
783 gint x, gint y, GtkSelectionData *data, guint info, guint ltime)
785 if (info == TARGET_TEXT_PLAIN)
787 if (gtk_selection_data_get_format(data) == 8 && gtk_selection_data_get_length(data) > 0)
788 vf->vte_terminal_feed_child(VTE_TERMINAL(widget),
789 (const gchar*) gtk_selection_data_get_data(data),
790 gtk_selection_data_get_length(data));
792 else
794 gchar *text = (gchar*) gtk_selection_data_get_text(data);
795 if (!EMPTY(text))
796 vf->vte_terminal_feed_child(VTE_TERMINAL(widget), text, strlen(text));
797 g_free(text);
799 gtk_drag_finish(drag_context, TRUE, FALSE, ltime);
803 static void on_check_run_in_vte_toggled(GtkToggleButton *togglebutton, GtkWidget *user_data)
805 g_return_if_fail(GTK_IS_WIDGET(user_data));
806 gtk_widget_set_sensitive(user_data, gtk_toggle_button_get_active(togglebutton));
810 static void on_term_font_set(GtkFontButton *widget, gpointer user_data)
812 const gchar *fontbtn = gtk_font_button_get_font_name(widget);
814 if (! utils_str_equal(fontbtn, vc->font))
816 SETPTR(vc->font, g_strdup(gtk_font_button_get_font_name(widget)));
817 vte_apply_user_settings();
822 static void on_term_fg_color_set(GtkColorButton *widget, gpointer user_data)
824 gtk_color_button_get_color(widget, &vc->colour_fore);
828 static void on_term_bg_color_set(GtkColorButton *widget, gpointer user_data)
830 gtk_color_button_get_color(widget, &vc->colour_back);
834 void vte_append_preferences_tab(void)
836 if (vte_info.have_vte)
838 GtkWidget *frame_term, *button_shell, *entry_shell;
839 GtkWidget *check_run_in_vte, *check_skip_script;
840 GtkWidget *font_button, *fg_color_button, *bg_color_button;
841 GtkWidget *entry_image, *button_image;
843 button_shell = GTK_WIDGET(ui_lookup_widget(ui_widgets.prefs_dialog, "button_term_shell"));
844 entry_shell = GTK_WIDGET(ui_lookup_widget(ui_widgets.prefs_dialog, "entry_shell"));
845 ui_setup_open_button_callback(button_shell, NULL,
846 GTK_FILE_CHOOSER_ACTION_OPEN, GTK_ENTRY(entry_shell));
848 button_image = GTK_WIDGET(ui_lookup_widget(ui_widgets.prefs_dialog, "button_term_image"));
849 entry_image = GTK_WIDGET(ui_lookup_widget(ui_widgets.prefs_dialog, "entry_image"));
850 ui_setup_open_button_callback(button_image, NULL,
851 GTK_FILE_CHOOSER_ACTION_OPEN, GTK_ENTRY(entry_image));
853 check_skip_script = GTK_WIDGET(ui_lookup_widget(ui_widgets.prefs_dialog, "check_skip_script"));
854 gtk_widget_set_sensitive(check_skip_script, vc->run_in_vte);
856 check_run_in_vte = GTK_WIDGET(ui_lookup_widget(ui_widgets.prefs_dialog, "check_run_in_vte"));
857 g_signal_connect(G_OBJECT(check_run_in_vte), "toggled",
858 G_CALLBACK(on_check_run_in_vte_toggled), check_skip_script);
860 font_button = ui_lookup_widget(ui_widgets.prefs_dialog, "font_term");
861 g_signal_connect(font_button, "font-set", G_CALLBACK(on_term_font_set), NULL);
863 fg_color_button = ui_lookup_widget(ui_widgets.prefs_dialog, "color_fore");
864 g_signal_connect(fg_color_button, "color-set", G_CALLBACK(on_term_fg_color_set), NULL);
866 bg_color_button = ui_lookup_widget(ui_widgets.prefs_dialog, "color_back");
867 g_signal_connect(bg_color_button, "color-set", G_CALLBACK(on_term_bg_color_set), NULL);
869 frame_term = ui_lookup_widget(ui_widgets.prefs_dialog, "frame_term");
870 gtk_widget_show_all(frame_term);
875 void vte_select_all(void)
877 if (vf->vte_terminal_select_all != NULL)
878 vf->vte_terminal_select_all(VTE_TERMINAL(vc->vte));
882 void vte_send_selection_to_vte(void)
884 GeanyDocument *doc;
885 gchar *text;
886 gsize len;
888 doc = document_get_current();
889 g_return_if_fail(doc != NULL);
891 if (sci_has_selection(doc->editor->sci))
893 text = sci_get_selection_contents(doc->editor->sci);
895 else
896 { /* Get the current line */
897 gint line_num = sci_get_current_line(doc->editor->sci);
898 text = sci_get_line(doc->editor->sci, line_num);
901 len = strlen(text);
903 if (vc->send_selection_unsafe)
904 { /* Explicitly append a trailing newline character to get the command executed,
905 this is disabled by default as it could cause all sorts of damage. */
906 if (text[len-1] != '\n' && text[len-1] != '\r')
908 SETPTR(text, g_strconcat(text, "\n", NULL));
909 len++;
912 else
913 { /* Make sure there is no newline character at the end to prevent unwanted execution */
914 while (text[len-1] == '\n' || text[len-1] == '\r')
916 text[len-1] = '\0';
917 len--;
921 vf->vte_terminal_feed_child(VTE_TERMINAL(vc->vte), text, len);
923 /* show the VTE */
924 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_VTE);
925 gtk_widget_grab_focus(vc->vte);
926 msgwin_show_hide(TRUE);
928 g_free(text);
932 #endif