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.
34 #include "callbacks.h"
36 #include "msgwindow.h"
38 #include "sciwrappers.h"
42 #include "keybindings.h"
44 #include "gtkcompat.h"
46 /* include stdlib.h AND unistd.h, because on GNU/Linux pid_t seems to be
47 * in stdlib.h, on FreeBSD in unistd.h, sys/types.h is needed for C89 */
49 #include <sys/types.h>
52 #include <gdk/gdkkeysyms.h>
62 static gboolean clean
= TRUE
;
63 static GModule
*module
= NULL
;
64 static struct VteFunctions
*vf
;
65 static gchar
*gtk_menu_key_accel
= NULL
;
66 static GtkWidget
*terminal_label
= NULL
;
67 static guint terminal_label_update_source
= 0;
69 /* use vte wordchars to select paths */
70 static const gchar VTE_WORDCHARS
[] = "-A-Za-z0-9,./?%&#:_";
73 /* Incomplete VteTerminal struct from vte/vte.h. */
74 typedef struct _VteTerminal VteTerminal
;
78 GtkAdjustment
*adjustment
;
81 #define VTE_TERMINAL(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), VTE_TYPE_TERMINAL, VteTerminal))
82 #define VTE_TYPE_TERMINAL (vf->vte_terminal_get_type())
85 VTE_CURSOR_BLINK_SYSTEM
,
88 } VteTerminalCursorBlinkMode
;
91 /* Holds function pointers we need to access the VTE API. */
94 GtkWidget
* (*vte_terminal_new
) (void);
95 pid_t (*vte_terminal_fork_command
) (VteTerminal
*terminal
, const char *command
, char **argv
,
96 char **envv
, const char *directory
, gboolean lastlog
,
97 gboolean utmp
, gboolean wtmp
);
98 void (*vte_terminal_set_size
) (VteTerminal
*terminal
, glong columns
, glong rows
);
99 void (*vte_terminal_set_word_chars
) (VteTerminal
*terminal
, const char *spec
);
100 void (*vte_terminal_set_mouse_autohide
) (VteTerminal
*terminal
, gboolean setting
);
101 void (*vte_terminal_reset
) (VteTerminal
*terminal
, gboolean full
, gboolean clear_history
);
102 GType (*vte_terminal_get_type
) (void);
103 void (*vte_terminal_set_scroll_on_output
) (VteTerminal
*terminal
, gboolean scroll
);
104 void (*vte_terminal_set_scroll_on_keystroke
) (VteTerminal
*terminal
, gboolean scroll
);
105 void (*vte_terminal_set_font_from_string
) (VteTerminal
*terminal
, const char *name
);
106 void (*vte_terminal_set_scrollback_lines
) (VteTerminal
*terminal
, glong lines
);
107 gboolean (*vte_terminal_get_has_selection
) (VteTerminal
*terminal
);
108 void (*vte_terminal_copy_clipboard
) (VteTerminal
*terminal
);
109 void (*vte_terminal_paste_clipboard
) (VteTerminal
*terminal
);
110 void (*vte_terminal_set_emulation
) (VteTerminal
*terminal
, const gchar
*emulation
);
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
);
125 static void create_vte(void);
126 static void vte_start(GtkWidget
*widget
);
127 static void vte_restart(GtkWidget
*widget
);
128 static gboolean
vte_button_pressed(GtkWidget
*widget
, GdkEventButton
*event
, gpointer user_data
);
129 static gboolean
vte_keyrelease_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
);
130 static gboolean
vte_keypress_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
);
131 static gboolean
vte_register_symbols(GModule
*module
);
132 static void vte_popup_menu_clicked(GtkMenuItem
*menuitem
, gpointer user_data
);
133 static GtkWidget
*vte_create_popup_menu(void);
134 static void vte_commit_cb(VteTerminal
*vte
, gchar
*arg1
, guint arg2
, gpointer user_data
);
135 static void vte_drag_data_received(GtkWidget
*widget
, GdkDragContext
*drag_context
,
136 gint x
, gint y
, GtkSelectionData
*data
, guint info
, guint ltime
);
145 POPUP_RESTARTTERMINAL
,
147 TARGET_UTF8_STRING
= 0,
149 TARGET_COMPOUND_TEXT
,
154 static const GtkTargetEntry dnd_targets
[] =
156 { "UTF8_STRING", 0, TARGET_UTF8_STRING
},
157 { "TEXT", 0, TARGET_TEXT
},
158 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT
},
159 { "STRING", 0, TARGET_STRING
},
160 { "text/plain", 0, TARGET_TEXT_PLAIN
},
164 static gchar
**vte_get_child_environment(void)
166 const gchar
*exclude_vars
[] = {"COLUMNS", "LINES", "TERM", "TERM_PROGRAM", NULL
};
168 return utils_copy_environment(exclude_vars
, "TERM", "xterm", NULL
);
172 static void override_menu_key(void)
174 if (gtk_menu_key_accel
== NULL
) /* for restoring the default value */
175 g_object_get(G_OBJECT(gtk_settings_get_default()),
176 "gtk-menu-bar-accel", >k_menu_key_accel
, NULL
);
178 if (vc
->ignore_menu_bar_accel
)
179 gtk_settings_set_string_property(gtk_settings_get_default(),
180 "gtk-menu-bar-accel", "<Shift><Control><Mod1><Mod2><Mod3><Mod4><Mod5>F10", "Geany");
182 gtk_settings_set_string_property(gtk_settings_get_default(),
183 "gtk-menu-bar-accel", gtk_menu_key_accel
, "Geany");
189 if (vte_info
.have_vte
== FALSE
)
190 { /* vte_info.have_vte can be false even if VTE is compiled in, think of command line option */
191 geany_debug("Disabling terminal support");
195 if (!EMPTY(vte_info
.lib_vte
))
197 module
= g_module_open(vte_info
.lib_vte
, G_MODULE_BIND_LAZY
);
199 #ifdef VTE_MODULE_PATH
202 module
= g_module_open(VTE_MODULE_PATH
, G_MODULE_BIND_LAZY
);
209 const gchar
*sonames
[] = {
210 #if GTK_CHECK_VERSION(3, 0, 0)
211 "libvte2_90.so", "libvte2_90.so.9", "libvte2_90.dylib",
213 "libvte.so", "libvte.so.4", "libvte.so.8", "libvte.so.9", "libvte.dylib",
218 for (i
= 0; sonames
[i
] != NULL
&& module
== NULL
; i
++)
220 module
= g_module_open(sonames
[i
], G_MODULE_BIND_LAZY
);
226 vte_info
.have_vte
= FALSE
;
227 geany_debug("Could not load libvte.so, embedded terminal support disabled");
232 vf
= g_new0(struct VteFunctions
, 1);
233 if (vte_register_symbols(module
))
234 vte_info
.have_vte
= TRUE
;
237 vte_info
.have_vte
= FALSE
;
239 /* FIXME: is closing the module safe? see vte_close() and test on FreeBSD */
240 /*g_module_close(module);*/
248 /* setup the F10 menu override (so it works before the widget is first realised). */
253 static void on_vte_realize(void)
255 /* the vte widget has to be realised before color changes take effect */
256 vte_apply_user_settings();
258 vf
->vte_terminal_im_append_menuitems(VTE_TERMINAL(vc
->vte
), GTK_MENU_SHELL(vc
->im_submenu
));
262 static gboolean
vte_start_idle(G_GNUC_UNUSED gpointer user_data
)
269 static void create_vte(void)
271 GtkWidget
*vte
, *scrollbar
, *hbox
;
273 vc
->vte
= vte
= vf
->vte_terminal_new();
274 scrollbar
= gtk_vscrollbar_new(GTK_ADJUSTMENT(VTE_TERMINAL(vte
)->adjustment
));
275 gtk_widget_set_can_focus(scrollbar
, FALSE
);
277 /* create menu now so copy/paste shortcuts work */
278 vc
->menu
= vte_create_popup_menu();
279 g_object_ref_sink(vc
->menu
);
281 hbox
= gtk_hbox_new(FALSE
, 0);
282 gtk_box_pack_start(GTK_BOX(hbox
), vte
, TRUE
, TRUE
, 0);
283 gtk_box_pack_start(GTK_BOX(hbox
), scrollbar
, FALSE
, FALSE
, 0);
285 /* set the default widget size first to prevent VTE expanding too much,
286 * sometimes causing the hscrollbar to be too big or out of view. */
287 gtk_widget_set_size_request(GTK_WIDGET(vte
), 10, 10);
288 vf
->vte_terminal_set_size(VTE_TERMINAL(vte
), 30, 1);
290 vf
->vte_terminal_set_mouse_autohide(VTE_TERMINAL(vte
), TRUE
);
291 vf
->vte_terminal_set_word_chars(VTE_TERMINAL(vte
), VTE_WORDCHARS
);
293 gtk_drag_dest_set(vte
, GTK_DEST_DEFAULT_ALL
,
294 dnd_targets
, G_N_ELEMENTS(dnd_targets
), GDK_ACTION_COPY
);
296 g_signal_connect(vte
, "child-exited", G_CALLBACK(vte_start
), NULL
);
297 g_signal_connect(vte
, "button-press-event", G_CALLBACK(vte_button_pressed
), NULL
);
298 g_signal_connect(vte
, "event", G_CALLBACK(vte_keypress_cb
), NULL
);
299 g_signal_connect(vte
, "key-release-event", G_CALLBACK(vte_keyrelease_cb
), NULL
);
300 g_signal_connect(vte
, "commit", G_CALLBACK(vte_commit_cb
), NULL
);
301 g_signal_connect(vte
, "motion-notify-event", G_CALLBACK(on_motion_event
), NULL
);
302 g_signal_connect(vte
, "drag-data-received", G_CALLBACK(vte_drag_data_received
), NULL
);
304 /* start shell on idle otherwise the initial prompt can get corrupted */
305 g_idle_add(vte_start_idle
, NULL
);
307 gtk_widget_show_all(hbox
);
308 terminal_label
= gtk_label_new(_("Terminal"));
309 gtk_notebook_insert_page(GTK_NOTEBOOK(msgwindow
.notebook
), hbox
, terminal_label
, MSG_VTE
);
311 g_signal_connect_after(vte
, "realize", G_CALLBACK(on_vte_realize
), NULL
);
318 /* free the vte widget before unloading vte module
319 * this prevents a segfault on X close window if the message window is hidden */
320 gtk_widget_destroy(vc
->vte
);
321 gtk_widget_destroy(vc
->menu
);
322 g_object_unref(vc
->menu
);
323 g_free(vc
->emulation
);
327 g_free(vc
->send_cmd_prefix
);
329 g_free(gtk_menu_key_accel
);
330 /* Don't unload the module explicitly because it causes a segfault on FreeBSD. The segfault
331 * happens when the app really exits, not directly on g_module_close(). This still needs to
332 * be investigated. */
333 /*g_module_close(module); */
337 static gboolean
set_dirty_idle(gpointer user_data
)
339 gtk_widget_set_name(terminal_label
, "geany-terminal-dirty");
340 terminal_label_update_source
= 0;
345 static void set_clean(gboolean value
)
351 if (terminal_label_update_source
> 0)
353 g_source_remove(terminal_label_update_source
);
354 terminal_label_update_source
= 0;
357 gtk_widget_set_name(terminal_label
, NULL
);
359 terminal_label_update_source
= g_timeout_add(150, set_dirty_idle
, NULL
);
366 static gboolean
vte_keyrelease_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
368 if (ui_is_keyval_enter_or_return(event
->keyval
) ||
369 ((event
->keyval
== GDK_c
) && (event
->state
& GDK_CONTROL_MASK
)))
371 /* assume any text on the prompt has been executed when pressing Enter/Return */
378 static gboolean
vte_keypress_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
380 if (vc
->enable_bash_keys
)
381 return FALSE
; /* Ctrl-[CD] will be handled by the VTE itself */
383 if (event
->type
!= GDK_KEY_RELEASE
)
386 if ((event
->keyval
== GDK_c
||
387 event
->keyval
== GDK_d
||
388 event
->keyval
== GDK_C
||
389 event
->keyval
== GDK_D
) &&
390 event
->state
& GDK_CONTROL_MASK
&&
391 ! (event
->state
& GDK_SHIFT_MASK
) && ! (event
->state
& GDK_MOD1_MASK
))
400 static void vte_commit_cb(VteTerminal
*vte
, gchar
*arg1
, guint arg2
, gpointer user_data
)
406 static void vte_start(GtkWidget
*widget
)
411 /* split the shell command line, so arguments will work too */
412 argv
= g_strsplit(vc
->shell
, " ", -1);
416 env
= vte_get_child_environment();
417 pid
= vf
->vte_terminal_fork_command(VTE_TERMINAL(widget
), argv
[0], argv
, env
,
418 vte_info
.dir
, TRUE
, TRUE
, TRUE
);
423 pid
= 0; /* use 0 as invalid pid */
429 static void vte_restart(GtkWidget
*widget
)
431 vte_get_working_directory(); /* try to keep the working directory when restarting the VTE */
437 vf
->vte_terminal_reset(VTE_TERMINAL(widget
), TRUE
, TRUE
);
442 static gboolean
vte_button_pressed(GtkWidget
*widget
, GdkEventButton
*event
, gpointer user_data
)
444 if (event
->button
== 3)
446 gtk_widget_grab_focus(vc
->vte
);
447 gtk_menu_popup(GTK_MENU(vc
->menu
), NULL
, NULL
, NULL
, NULL
, event
->button
, event
->time
);
449 else if (event
->button
== 2)
451 gtk_widget_grab_focus(widget
);
457 static void vte_set_cursor_blink_mode(void)
459 if (vf
->vte_terminal_set_cursor_blink_mode
!= NULL
)
461 vf
->vte_terminal_set_cursor_blink_mode(VTE_TERMINAL(vc
->vte
),
462 (vc
->cursor_blinks
) ? VTE_CURSOR_BLINK_ON
: VTE_CURSOR_BLINK_OFF
);
465 vf
->vte_terminal_set_cursor_blinks(VTE_TERMINAL(vc
->vte
), vc
->cursor_blinks
);
469 static gboolean
vte_register_symbols(GModule
*mod
)
471 #define BIND_SYMBOL(field) \
472 g_module_symbol(mod, #field, (void*)&vf->field)
473 #define BIND_REQUIRED_SYMBOL(field) \
475 if (! BIND_SYMBOL(field)) \
477 g_critical(_("invalid VTE library \"%s\": missing symbol \"%s\""), \
478 g_module_name(mod), #field); \
483 BIND_REQUIRED_SYMBOL(vte_terminal_new
);
484 BIND_REQUIRED_SYMBOL(vte_terminal_set_size
);
485 BIND_REQUIRED_SYMBOL(vte_terminal_fork_command
);
486 BIND_REQUIRED_SYMBOL(vte_terminal_set_word_chars
);
487 BIND_REQUIRED_SYMBOL(vte_terminal_set_mouse_autohide
);
488 BIND_REQUIRED_SYMBOL(vte_terminal_reset
);
489 BIND_REQUIRED_SYMBOL(vte_terminal_get_type
);
490 BIND_REQUIRED_SYMBOL(vte_terminal_set_scroll_on_output
);
491 BIND_REQUIRED_SYMBOL(vte_terminal_set_scroll_on_keystroke
);
492 BIND_REQUIRED_SYMBOL(vte_terminal_set_font_from_string
);
493 BIND_REQUIRED_SYMBOL(vte_terminal_set_scrollback_lines
);
494 BIND_REQUIRED_SYMBOL(vte_terminal_get_has_selection
);
495 BIND_REQUIRED_SYMBOL(vte_terminal_copy_clipboard
);
496 BIND_REQUIRED_SYMBOL(vte_terminal_paste_clipboard
);
497 BIND_REQUIRED_SYMBOL(vte_terminal_set_emulation
);
498 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_foreground
);
499 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_bold
);
500 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_background
);
501 BIND_REQUIRED_SYMBOL(vte_terminal_set_background_image_file
);
502 BIND_REQUIRED_SYMBOL(vte_terminal_feed_child
);
503 BIND_REQUIRED_SYMBOL(vte_terminal_im_append_menuitems
);
504 if (! BIND_SYMBOL(vte_terminal_set_cursor_blink_mode
))
505 /* vte_terminal_set_cursor_blink_mode() is only available since 0.17.1, so if we don't find
506 * this symbol, we are probably on an older version and use the old API instead */
507 BIND_REQUIRED_SYMBOL(vte_terminal_set_cursor_blinks
);
508 BIND_REQUIRED_SYMBOL(vte_terminal_select_all
);
509 BIND_REQUIRED_SYMBOL(vte_terminal_set_audible_bell
);
511 #undef BIND_REQUIRED_SYMBOL
518 void vte_apply_user_settings(void)
520 if (! ui_prefs
.msgwindow_visible
)
523 vf
->vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->vte
), vc
->scrollback_lines
);
524 vf
->vte_terminal_set_scroll_on_keystroke(VTE_TERMINAL(vc
->vte
), vc
->scroll_on_key
);
525 vf
->vte_terminal_set_scroll_on_output(VTE_TERMINAL(vc
->vte
), vc
->scroll_on_out
);
526 vf
->vte_terminal_set_emulation(VTE_TERMINAL(vc
->vte
), vc
->emulation
);
527 vf
->vte_terminal_set_font_from_string(VTE_TERMINAL(vc
->vte
), vc
->font
);
528 vf
->vte_terminal_set_color_foreground(VTE_TERMINAL(vc
->vte
), &vc
->colour_fore
);
529 vf
->vte_terminal_set_color_bold(VTE_TERMINAL(vc
->vte
), &vc
->colour_fore
);
530 vf
->vte_terminal_set_color_background(VTE_TERMINAL(vc
->vte
), &vc
->colour_back
);
531 vf
->vte_terminal_set_background_image_file(VTE_TERMINAL(vc
->vte
), vc
->image
);
532 vf
->vte_terminal_set_audible_bell(VTE_TERMINAL(vc
->vte
), prefs
.beep_on_errors
);
533 vte_set_cursor_blink_mode();
539 static void vte_popup_menu_clicked(GtkMenuItem
*menuitem
, gpointer user_data
)
541 switch (GPOINTER_TO_INT(user_data
))
545 if (vf
->vte_terminal_get_has_selection(VTE_TERMINAL(vc
->vte
)))
546 vf
->vte_terminal_copy_clipboard(VTE_TERMINAL(vc
->vte
));
551 vf
->vte_terminal_paste_clipboard(VTE_TERMINAL(vc
->vte
));
554 case POPUP_SELECTALL
:
559 case POPUP_CHANGEPATH
:
561 GeanyDocument
*doc
= document_get_current();
563 vte_cwd(doc
->file_name
, TRUE
);
566 case POPUP_RESTARTTERMINAL
:
568 vte_restart(vc
->vte
);
571 case POPUP_PREFERENCES
:
573 GtkWidget
*notebook
, *tab_page
;
577 notebook
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "notebook2");
578 tab_page
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "frame_term");
580 gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook
),
581 gtk_notebook_page_num(GTK_NOTEBOOK(notebook
), GTK_WIDGET(tab_page
)));
589 static GtkWidget
*vte_create_popup_menu(void)
591 GtkWidget
*menu
, *item
;
592 GtkAccelGroup
*accel_group
;
594 menu
= gtk_menu_new();
596 accel_group
= gtk_accel_group_new();
597 gtk_window_add_accel_group(GTK_WINDOW(main_widgets
.window
), accel_group
);
599 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_COPY
, NULL
);
600 gtk_widget_add_accelerator(item
, "activate", accel_group
,
601 GDK_c
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, GTK_ACCEL_VISIBLE
);
602 gtk_widget_show(item
);
603 gtk_container_add(GTK_CONTAINER(menu
), item
);
604 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_COPY
));
606 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_PASTE
, NULL
);
607 gtk_widget_add_accelerator(item
, "activate", accel_group
,
608 GDK_v
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, GTK_ACCEL_VISIBLE
);
609 gtk_widget_show(item
);
610 gtk_container_add(GTK_CONTAINER(menu
), item
);
611 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_PASTE
));
613 item
= gtk_separator_menu_item_new();
614 gtk_widget_show(item
);
615 gtk_container_add(GTK_CONTAINER(menu
), item
);
617 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_SELECT_ALL
, NULL
);
618 gtk_widget_show(item
);
619 gtk_container_add(GTK_CONTAINER(menu
), item
);
620 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_SELECTALL
));
622 item
= gtk_separator_menu_item_new();
623 gtk_widget_show(item
);
624 gtk_container_add(GTK_CONTAINER(menu
), item
);
626 item
= gtk_image_menu_item_new_with_mnemonic(_("_Set Path From Document"));
627 gtk_widget_show(item
);
628 gtk_container_add(GTK_CONTAINER(menu
), item
);
629 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_CHANGEPATH
));
631 item
= gtk_image_menu_item_new_with_mnemonic(_("_Restart Terminal"));
632 gtk_widget_show(item
);
633 gtk_container_add(GTK_CONTAINER(menu
), item
);
634 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_RESTARTTERMINAL
));
636 item
= gtk_separator_menu_item_new();
637 gtk_widget_show(item
);
638 gtk_container_add(GTK_CONTAINER(menu
), item
);
640 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES
, NULL
);
641 gtk_widget_show(item
);
642 gtk_container_add(GTK_CONTAINER(menu
), item
);
643 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_PREFERENCES
));
645 msgwin_menu_add_common_items(GTK_MENU(menu
));
647 item
= gtk_separator_menu_item_new();
648 gtk_widget_show(item
);
649 gtk_container_add(GTK_CONTAINER(menu
), item
);
651 /* the IM submenu should always be the last item to be consistent with other GTK popup menus */
652 vc
->im_submenu
= gtk_menu_new();
654 item
= gtk_image_menu_item_new_with_mnemonic(_("_Input Methods"));
655 gtk_widget_show(item
);
656 gtk_container_add(GTK_CONTAINER(menu
), item
);
658 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item
), vc
->im_submenu
);
659 /* submenu populated after vte realized */
664 /* If the command could be executed, TRUE is returned, FALSE otherwise (i.e. there was some text
666 gboolean
vte_send_cmd(const gchar
*cmd
)
670 vf
->vte_terminal_feed_child(VTE_TERMINAL(vc
->vte
), cmd
, strlen(cmd
));
671 set_clean(TRUE
); /* vte_terminal_feed_child() also marks the vte as not clean */
679 /* Taken from Terminal by os-cillation: terminal_screen_get_working_directory, thanks.
680 * Determines the working directory using various OS-specific mechanisms and stores the determined
681 * directory in vte_info.dir. Note: vte_info.dir contains the real path. */
682 const gchar
*vte_get_working_directory(void)
684 gchar buffer
[4096 + 1];
691 file
= g_strdup_printf("/proc/%d/cwd", pid
);
692 length
= readlink(file
, buffer
, sizeof(buffer
));
694 if (length
> 0 && *buffer
== '/')
696 buffer
[length
] = '\0';
697 g_free(vte_info
.dir
);
698 vte_info
.dir
= g_strdup(buffer
);
700 else if (length
== 0)
702 cwd
= g_get_current_dir();
705 if (chdir(file
) == 0)
707 g_free(vte_info
.dir
);
708 vte_info
.dir
= g_get_current_dir();
710 geany_debug("%s: %s", G_STRFUNC
, g_strerror(errno
));
722 /* Changes the current working directory of the VTE to the path of the given filename.
723 * filename is expected to be in UTF-8 encoding.
724 * filename can also be a path, then it is used directly.
725 * If force is set to TRUE, it will always change the cwd
727 void vte_cwd(const gchar
*filename
, gboolean force
)
729 if (vte_info
.have_vte
&& (vc
->follow_path
|| force
) &&
730 filename
!= NULL
&& g_path_is_absolute(filename
))
734 if (g_file_test(filename
, G_FILE_TEST_IS_DIR
))
735 path
= g_strdup(filename
);
737 path
= g_path_get_dirname(filename
);
739 vte_get_working_directory(); /* refresh vte_info.dir */
740 if (! utils_str_equal(path
, vte_info
.dir
))
742 /* use g_shell_quote to avoid problems with spaces, '!' or something else in path */
743 gchar
*quoted_path
= g_shell_quote(path
);
744 gchar
*cmd
= g_strconcat(vc
->send_cmd_prefix
, "cd ", quoted_path
, "\n", NULL
);
745 if (! vte_send_cmd(cmd
))
747 const gchar
*msg
= _("Directory not changed because the terminal may contain some input (press Ctrl+C or Enter to clear it).");
748 ui_set_statusbar(FALSE
, "%s", msg
);
749 geany_debug("%s", msg
);
759 static void vte_drag_data_received(GtkWidget
*widget
, GdkDragContext
*drag_context
,
760 gint x
, gint y
, GtkSelectionData
*data
, guint info
, guint ltime
)
762 if (info
== TARGET_TEXT_PLAIN
)
764 if (gtk_selection_data_get_format(data
) == 8 && gtk_selection_data_get_length(data
) > 0)
765 vf
->vte_terminal_feed_child(VTE_TERMINAL(widget
),
766 (const gchar
*) gtk_selection_data_get_data(data
),
767 gtk_selection_data_get_length(data
));
771 gchar
*text
= (gchar
*) gtk_selection_data_get_text(data
);
773 vf
->vte_terminal_feed_child(VTE_TERMINAL(widget
), text
, strlen(text
));
776 gtk_drag_finish(drag_context
, TRUE
, FALSE
, ltime
);
780 static void on_check_run_in_vte_toggled(GtkToggleButton
*togglebutton
, GtkWidget
*user_data
)
782 g_return_if_fail(GTK_IS_WIDGET(user_data
));
783 gtk_widget_set_sensitive(user_data
, gtk_toggle_button_get_active(togglebutton
));
787 static void on_term_font_set(GtkFontButton
*widget
, gpointer user_data
)
789 const gchar
*fontbtn
= gtk_font_button_get_font_name(widget
);
791 if (! utils_str_equal(fontbtn
, vc
->font
))
793 SETPTR(vc
->font
, g_strdup(gtk_font_button_get_font_name(widget
)));
794 vte_apply_user_settings();
799 static void on_term_fg_color_set(GtkColorButton
*widget
, gpointer user_data
)
801 gtk_color_button_get_color(widget
, &vc
->colour_fore
);
805 static void on_term_bg_color_set(GtkColorButton
*widget
, gpointer user_data
)
807 gtk_color_button_get_color(widget
, &vc
->colour_back
);
811 void vte_append_preferences_tab(void)
813 if (vte_info
.have_vte
)
815 GtkWidget
*frame_term
, *button_shell
, *entry_shell
;
816 GtkWidget
*check_run_in_vte
, *check_skip_script
;
817 GtkWidget
*font_button
, *fg_color_button
, *bg_color_button
;
818 GtkWidget
*entry_image
, *button_image
;
820 button_shell
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "button_term_shell"));
821 entry_shell
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "entry_shell"));
822 ui_setup_open_button_callback(button_shell
, NULL
,
823 GTK_FILE_CHOOSER_ACTION_OPEN
, GTK_ENTRY(entry_shell
));
825 button_image
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "button_term_image"));
826 entry_image
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "entry_image"));
827 ui_setup_open_button_callback(button_image
, NULL
,
828 GTK_FILE_CHOOSER_ACTION_OPEN
, GTK_ENTRY(entry_image
));
830 check_skip_script
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "check_skip_script"));
831 gtk_widget_set_sensitive(check_skip_script
, vc
->run_in_vte
);
833 check_run_in_vte
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "check_run_in_vte"));
834 g_signal_connect(G_OBJECT(check_run_in_vte
), "toggled",
835 G_CALLBACK(on_check_run_in_vte_toggled
), check_skip_script
);
837 font_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "font_term");
838 g_signal_connect(font_button
, "font-set", G_CALLBACK(on_term_font_set
), NULL
);
840 fg_color_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "color_fore");
841 g_signal_connect(fg_color_button
, "color-set", G_CALLBACK(on_term_fg_color_set
), NULL
);
843 bg_color_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "color_back");
844 g_signal_connect(bg_color_button
, "color-set", G_CALLBACK(on_term_bg_color_set
), NULL
);
846 frame_term
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "frame_term");
847 gtk_widget_show_all(frame_term
);
852 void vte_select_all(void)
854 if (vf
->vte_terminal_select_all
!= NULL
)
855 vf
->vte_terminal_select_all(VTE_TERMINAL(vc
->vte
));
859 void vte_send_selection_to_vte(void)
865 doc
= document_get_current();
866 g_return_if_fail(doc
!= NULL
);
868 if (sci_has_selection(doc
->editor
->sci
))
870 text
= sci_get_selection_contents(doc
->editor
->sci
);
873 { /* Get the current line */
874 gint line_num
= sci_get_current_line(doc
->editor
->sci
);
875 text
= sci_get_line(doc
->editor
->sci
, line_num
);
880 if (vc
->send_selection_unsafe
)
881 { /* Explicitly append a trailing newline character to get the command executed,
882 this is disabled by default as it could cause all sorts of damage. */
883 if (text
[len
-1] != '\n' && text
[len
-1] != '\r')
885 SETPTR(text
, g_strconcat(text
, "\n", NULL
));
890 { /* Make sure there is no newline character at the end to prevent unwanted execution */
891 while (text
[len
-1] == '\n' || text
[len
-1] == '\r')
898 vf
->vte_terminal_feed_child(VTE_TERMINAL(vc
->vte
), text
, len
);
901 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_VTE
);
902 gtk_widget_grab_focus(vc
->vte
);
903 msgwin_show_hide(TRUE
);