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 "geanyobject.h"
37 #include "msgwindow.h"
39 #include "sciwrappers.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 */
50 #include <sys/types.h>
53 #include <gdk/gdkkeysyms.h>
59 VteInfo vte_info
= { FALSE
, FALSE
, FALSE
, NULL
, NULL
};
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
;
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())
86 VTE_CURSOR_BLINK_SYSTEM
,
89 } VteTerminalCursorBlinkMode
;
92 /* Holds function pointers we need to access the VTE API. */
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_emulation
) (VteTerminal
*terminal
, const gchar
*emulation
);
112 void (*vte_terminal_set_color_foreground
) (VteTerminal
*terminal
, const GdkColor
*foreground
);
113 void (*vte_terminal_set_color_bold
) (VteTerminal
*terminal
, const GdkColor
*foreground
);
114 void (*vte_terminal_set_color_background
) (VteTerminal
*terminal
, const GdkColor
*background
);
115 void (*vte_terminal_feed_child
) (VteTerminal
*terminal
, const char *data
, glong length
);
116 void (*vte_terminal_im_append_menuitems
) (VteTerminal
*terminal
, GtkMenuShell
*menushell
);
117 void (*vte_terminal_set_cursor_blink_mode
) (VteTerminal
*terminal
,
118 VteTerminalCursorBlinkMode mode
);
119 void (*vte_terminal_set_cursor_blinks
) (VteTerminal
*terminal
, gboolean blink
);
120 void (*vte_terminal_select_all
) (VteTerminal
*terminal
);
121 void (*vte_terminal_set_audible_bell
) (VteTerminal
*terminal
, gboolean is_audible
);
122 void (*vte_terminal_set_background_image_file
) (VteTerminal
*terminal
, const char *path
);
123 GtkAdjustment
* (*vte_terminal_get_adjustment
) (VteTerminal
*terminal
);
127 static void create_vte(void);
128 static void vte_start(GtkWidget
*widget
);
129 static void vte_restart(GtkWidget
*widget
);
130 static gboolean
vte_button_pressed(GtkWidget
*widget
, GdkEventButton
*event
, gpointer user_data
);
131 static gboolean
vte_keyrelease_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
);
132 static gboolean
vte_keypress_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
);
133 static gboolean
vte_register_symbols(GModule
*module
);
134 static void vte_popup_menu_clicked(GtkMenuItem
*menuitem
, gpointer user_data
);
135 static GtkWidget
*vte_create_popup_menu(void);
136 static void vte_commit_cb(VteTerminal
*vte
, gchar
*arg1
, guint arg2
, gpointer user_data
);
137 static void vte_drag_data_received(GtkWidget
*widget
, GdkDragContext
*drag_context
,
138 gint x
, gint y
, GtkSelectionData
*data
, guint info
, guint ltime
);
147 POPUP_RESTARTTERMINAL
,
149 TARGET_UTF8_STRING
= 0,
151 TARGET_COMPOUND_TEXT
,
156 static const GtkTargetEntry dnd_targets
[] =
158 { "UTF8_STRING", 0, TARGET_UTF8_STRING
},
159 { "TEXT", 0, TARGET_TEXT
},
160 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT
},
161 { "STRING", 0, TARGET_STRING
},
162 { "text/plain", 0, TARGET_TEXT_PLAIN
},
166 /* replacement for vte_terminal_get_adjustment() when it's not available */
167 static GtkAdjustment
*default_vte_terminal_get_adjustment(VteTerminal
*vte
)
169 #if GTK_CHECK_VERSION(3, 0, 0)
170 if (GTK_IS_SCROLLABLE(vte
))
171 return gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(vte
));
173 /* this is only valid in < 0.38, 0.38 broke ABI */
174 return vte
->adjustment
;
178 static gchar
**vte_get_child_environment(void)
180 const gchar
*exclude_vars
[] = {"COLUMNS", "LINES", "TERM", "TERM_PROGRAM", NULL
};
182 return utils_copy_environment(exclude_vars
, "TERM", "xterm", NULL
);
186 static void override_menu_key(void)
188 if (gtk_menu_key_accel
== NULL
) /* for restoring the default value */
189 g_object_get(G_OBJECT(gtk_settings_get_default()),
190 "gtk-menu-bar-accel", >k_menu_key_accel
, NULL
);
192 if (vc
->ignore_menu_bar_accel
)
193 gtk_settings_set_string_property(gtk_settings_get_default(),
194 "gtk-menu-bar-accel", "<Shift><Control><Mod1><Mod2><Mod3><Mod4><Mod5>F10", "Geany");
196 gtk_settings_set_string_property(gtk_settings_get_default(),
197 "gtk-menu-bar-accel", gtk_menu_key_accel
, "Geany");
201 static void on_startup_complete(G_GNUC_UNUSED GObject
*dummy
)
203 GeanyDocument
*doc
= document_get_current();
206 vte_cwd((doc
->real_path
!= NULL
) ? doc
->real_path
: doc
->file_name
, FALSE
);
212 if (vte_info
.have_vte
== FALSE
)
213 { /* vte_info.have_vte can be false even if VTE is compiled in, think of command line option */
214 geany_debug("Disabling terminal support");
218 if (!EMPTY(vte_info
.lib_vte
))
220 module
= g_module_open(vte_info
.lib_vte
, G_MODULE_BIND_LAZY
);
222 #ifdef VTE_MODULE_PATH
225 module
= g_module_open(VTE_MODULE_PATH
, G_MODULE_BIND_LAZY
);
232 const gchar
*sonames
[] = {
233 #if GTK_CHECK_VERSION(3, 0, 0)
234 "libvte2_90.so", "libvte2_90.so.9", "libvte2_90.dylib",
236 "libvte.so", "libvte.so.4", "libvte.so.8", "libvte.so.9", "libvte.dylib",
241 for (i
= 0; sonames
[i
] != NULL
&& module
== NULL
; i
++)
243 module
= g_module_open(sonames
[i
], G_MODULE_BIND_LAZY
);
249 vte_info
.have_vte
= FALSE
;
250 geany_debug("Could not load libvte.so, embedded terminal support disabled");
255 vf
= g_new0(struct VteFunctions
, 1);
256 if (vte_register_symbols(module
))
257 vte_info
.have_vte
= TRUE
;
260 vte_info
.have_vte
= FALSE
;
262 /* FIXME: is closing the module safe? see vte_close() and test on FreeBSD */
263 /*g_module_close(module);*/
271 /* setup the F10 menu override (so it works before the widget is first realised). */
274 g_signal_connect(geany_object
, "geany-startup-complete", G_CALLBACK(on_startup_complete
), NULL
);
278 static void on_vte_realize(void)
280 /* the vte widget has to be realised before color changes take effect */
281 vte_apply_user_settings();
283 vf
->vte_terminal_im_append_menuitems(VTE_TERMINAL(vc
->vte
), GTK_MENU_SHELL(vc
->im_submenu
));
287 static gboolean
vte_start_idle(G_GNUC_UNUSED gpointer user_data
)
294 static void create_vte(void)
296 GtkWidget
*vte
, *scrollbar
, *hbox
;
298 vc
->vte
= vte
= vf
->vte_terminal_new();
299 scrollbar
= gtk_vscrollbar_new(vf
->vte_terminal_get_adjustment(VTE_TERMINAL(vte
)));
300 gtk_widget_set_can_focus(scrollbar
, FALSE
);
302 /* create menu now so copy/paste shortcuts work */
303 vc
->menu
= vte_create_popup_menu();
304 g_object_ref_sink(vc
->menu
);
306 hbox
= gtk_hbox_new(FALSE
, 0);
307 gtk_box_pack_start(GTK_BOX(hbox
), vte
, TRUE
, TRUE
, 0);
308 gtk_box_pack_start(GTK_BOX(hbox
), scrollbar
, FALSE
, FALSE
, 0);
310 /* set the default widget size first to prevent VTE expanding too much,
311 * sometimes causing the hscrollbar to be too big or out of view. */
312 gtk_widget_set_size_request(GTK_WIDGET(vte
), 10, 10);
313 vf
->vte_terminal_set_size(VTE_TERMINAL(vte
), 30, 1);
315 vf
->vte_terminal_set_mouse_autohide(VTE_TERMINAL(vte
), TRUE
);
316 vf
->vte_terminal_set_word_chars(VTE_TERMINAL(vte
), VTE_WORDCHARS
);
318 gtk_drag_dest_set(vte
, GTK_DEST_DEFAULT_ALL
,
319 dnd_targets
, G_N_ELEMENTS(dnd_targets
), GDK_ACTION_COPY
);
321 g_signal_connect(vte
, "child-exited", G_CALLBACK(vte_start
), NULL
);
322 g_signal_connect(vte
, "button-press-event", G_CALLBACK(vte_button_pressed
), NULL
);
323 g_signal_connect(vte
, "event", G_CALLBACK(vte_keypress_cb
), NULL
);
324 g_signal_connect(vte
, "key-release-event", G_CALLBACK(vte_keyrelease_cb
), NULL
);
325 g_signal_connect(vte
, "commit", G_CALLBACK(vte_commit_cb
), NULL
);
326 g_signal_connect(vte
, "motion-notify-event", G_CALLBACK(on_motion_event
), NULL
);
327 g_signal_connect(vte
, "drag-data-received", G_CALLBACK(vte_drag_data_received
), NULL
);
329 /* start shell on idle otherwise the initial prompt can get corrupted */
330 g_idle_add(vte_start_idle
, NULL
);
332 gtk_widget_show_all(hbox
);
333 terminal_label
= gtk_label_new(_("Terminal"));
334 gtk_notebook_insert_page(GTK_NOTEBOOK(msgwindow
.notebook
), hbox
, terminal_label
, MSG_VTE
);
336 g_signal_connect_after(vte
, "realize", G_CALLBACK(on_vte_realize
), NULL
);
343 /* free the vte widget before unloading vte module
344 * this prevents a segfault on X close window if the message window is hidden */
345 gtk_widget_destroy(vc
->vte
);
346 gtk_widget_destroy(vc
->menu
);
347 g_object_unref(vc
->menu
);
348 g_free(vc
->emulation
);
352 g_free(vc
->send_cmd_prefix
);
354 g_free(gtk_menu_key_accel
);
355 /* Don't unload the module explicitly because it causes a segfault on FreeBSD. The segfault
356 * happens when the app really exits, not directly on g_module_close(). This still needs to
357 * be investigated. */
358 /*g_module_close(module); */
362 static gboolean
set_dirty_idle(gpointer user_data
)
364 gtk_widget_set_name(terminal_label
, "geany-terminal-dirty");
365 terminal_label_update_source
= 0;
370 static void set_clean(gboolean value
)
376 if (terminal_label_update_source
> 0)
378 g_source_remove(terminal_label_update_source
);
379 terminal_label_update_source
= 0;
382 gtk_widget_set_name(terminal_label
, NULL
);
384 terminal_label_update_source
= g_timeout_add(150, set_dirty_idle
, NULL
);
391 static gboolean
vte_keyrelease_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
393 if (ui_is_keyval_enter_or_return(event
->keyval
) ||
394 ((event
->keyval
== GDK_c
) && (event
->state
& GDK_CONTROL_MASK
)))
396 /* assume any text on the prompt has been executed when pressing Enter/Return */
403 static gboolean
vte_keypress_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
405 if (vc
->enable_bash_keys
)
406 return FALSE
; /* Ctrl-[CD] will be handled by the VTE itself */
408 if (event
->type
!= GDK_KEY_RELEASE
)
411 if ((event
->keyval
== GDK_c
||
412 event
->keyval
== GDK_d
||
413 event
->keyval
== GDK_C
||
414 event
->keyval
== GDK_D
) &&
415 event
->state
& GDK_CONTROL_MASK
&&
416 ! (event
->state
& GDK_SHIFT_MASK
) && ! (event
->state
& GDK_MOD1_MASK
))
425 static void vte_commit_cb(VteTerminal
*vte
, gchar
*arg1
, guint arg2
, gpointer user_data
)
431 static void vte_start(GtkWidget
*widget
)
433 /* split the shell command line, so arguments will work too */
434 gchar
**argv
= g_strsplit(vc
->shell
, " ", -1);
438 gchar
**env
= vte_get_child_environment();
440 pid
= vf
->vte_terminal_fork_command(VTE_TERMINAL(widget
), argv
[0], argv
, env
,
441 vte_info
.dir
, TRUE
, TRUE
, TRUE
);
446 pid
= 0; /* use 0 as invalid pid */
452 static void vte_restart(GtkWidget
*widget
)
454 vte_get_working_directory(); /* try to keep the working directory when restarting the VTE */
460 vf
->vte_terminal_reset(VTE_TERMINAL(widget
), TRUE
, TRUE
);
465 static gboolean
vte_button_pressed(GtkWidget
*widget
, GdkEventButton
*event
, gpointer user_data
)
467 if (event
->button
== 3)
469 gtk_widget_grab_focus(vc
->vte
);
470 gtk_menu_popup(GTK_MENU(vc
->menu
), NULL
, NULL
, NULL
, NULL
, event
->button
, event
->time
);
472 else if (event
->button
== 2)
474 gtk_widget_grab_focus(widget
);
480 static void vte_set_cursor_blink_mode(void)
482 if (vf
->vte_terminal_set_cursor_blink_mode
!= NULL
)
484 vf
->vte_terminal_set_cursor_blink_mode(VTE_TERMINAL(vc
->vte
),
485 (vc
->cursor_blinks
) ? VTE_CURSOR_BLINK_ON
: VTE_CURSOR_BLINK_OFF
);
488 vf
->vte_terminal_set_cursor_blinks(VTE_TERMINAL(vc
->vte
), vc
->cursor_blinks
);
492 static gboolean
vte_register_symbols(GModule
*mod
)
494 #define BIND_SYMBOL(field) \
495 g_module_symbol(mod, #field, (void*)&vf->field)
496 #define BIND_REQUIRED_SYMBOL(field) \
498 if (! BIND_SYMBOL(field)) \
500 g_critical(_("invalid VTE library \"%s\": missing symbol \"%s\""), \
501 g_module_name(mod), #field); \
506 BIND_REQUIRED_SYMBOL(vte_terminal_new
);
507 BIND_REQUIRED_SYMBOL(vte_terminal_set_size
);
508 BIND_REQUIRED_SYMBOL(vte_terminal_fork_command
);
509 BIND_REQUIRED_SYMBOL(vte_terminal_set_word_chars
);
510 BIND_REQUIRED_SYMBOL(vte_terminal_set_mouse_autohide
);
511 BIND_REQUIRED_SYMBOL(vte_terminal_reset
);
512 BIND_REQUIRED_SYMBOL(vte_terminal_get_type
);
513 BIND_REQUIRED_SYMBOL(vte_terminal_set_scroll_on_output
);
514 BIND_REQUIRED_SYMBOL(vte_terminal_set_scroll_on_keystroke
);
515 BIND_REQUIRED_SYMBOL(vte_terminal_set_font
);
516 BIND_REQUIRED_SYMBOL(vte_terminal_set_scrollback_lines
);
517 BIND_REQUIRED_SYMBOL(vte_terminal_get_has_selection
);
518 BIND_REQUIRED_SYMBOL(vte_terminal_copy_clipboard
);
519 BIND_REQUIRED_SYMBOL(vte_terminal_paste_clipboard
);
520 BIND_REQUIRED_SYMBOL(vte_terminal_set_emulation
);
521 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_foreground
);
522 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_bold
);
523 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_background
);
524 BIND_REQUIRED_SYMBOL(vte_terminal_set_background_image_file
);
525 BIND_REQUIRED_SYMBOL(vte_terminal_feed_child
);
526 BIND_REQUIRED_SYMBOL(vte_terminal_im_append_menuitems
);
527 if (! BIND_SYMBOL(vte_terminal_set_cursor_blink_mode
))
528 /* vte_terminal_set_cursor_blink_mode() is only available since 0.17.1, so if we don't find
529 * this symbol, we are probably on an older version and use the old API instead */
530 BIND_REQUIRED_SYMBOL(vte_terminal_set_cursor_blinks
);
531 BIND_REQUIRED_SYMBOL(vte_terminal_select_all
);
532 BIND_REQUIRED_SYMBOL(vte_terminal_set_audible_bell
);
533 if (! BIND_SYMBOL(vte_terminal_get_adjustment
))
534 /* vte_terminal_get_adjustment() is available since 0.9 and removed in 0.38 */
535 vf
->vte_terminal_get_adjustment
= default_vte_terminal_get_adjustment
;
537 #undef BIND_REQUIRED_SYMBOL
544 void vte_apply_user_settings(void)
546 PangoFontDescription
*font_desc
;
548 if (! ui_prefs
.msgwindow_visible
)
551 vf
->vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->vte
), vc
->scrollback_lines
);
552 vf
->vte_terminal_set_scroll_on_keystroke(VTE_TERMINAL(vc
->vte
), vc
->scroll_on_key
);
553 vf
->vte_terminal_set_scroll_on_output(VTE_TERMINAL(vc
->vte
), vc
->scroll_on_out
);
554 vf
->vte_terminal_set_emulation(VTE_TERMINAL(vc
->vte
), vc
->emulation
);
555 font_desc
= pango_font_description_from_string(vc
->font
);
556 vf
->vte_terminal_set_font(VTE_TERMINAL(vc
->vte
), font_desc
);
557 pango_font_description_free(font_desc
);
558 vf
->vte_terminal_set_color_foreground(VTE_TERMINAL(vc
->vte
), &vc
->colour_fore
);
559 vf
->vte_terminal_set_color_bold(VTE_TERMINAL(vc
->vte
), &vc
->colour_fore
);
560 vf
->vte_terminal_set_color_background(VTE_TERMINAL(vc
->vte
), &vc
->colour_back
);
561 vf
->vte_terminal_set_background_image_file(VTE_TERMINAL(vc
->vte
), vc
->image
);
562 vf
->vte_terminal_set_audible_bell(VTE_TERMINAL(vc
->vte
), prefs
.beep_on_errors
);
563 vte_set_cursor_blink_mode();
569 static void vte_popup_menu_clicked(GtkMenuItem
*menuitem
, gpointer user_data
)
571 switch (GPOINTER_TO_INT(user_data
))
575 if (vf
->vte_terminal_get_has_selection(VTE_TERMINAL(vc
->vte
)))
576 vf
->vte_terminal_copy_clipboard(VTE_TERMINAL(vc
->vte
));
581 vf
->vte_terminal_paste_clipboard(VTE_TERMINAL(vc
->vte
));
584 case POPUP_SELECTALL
:
589 case POPUP_CHANGEPATH
:
591 GeanyDocument
*doc
= document_get_current();
593 vte_cwd(doc
->file_name
, TRUE
);
596 case POPUP_RESTARTTERMINAL
:
598 vte_restart(vc
->vte
);
601 case POPUP_PREFERENCES
:
603 GtkWidget
*notebook
, *tab_page
;
607 notebook
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "notebook2");
608 tab_page
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "frame_term");
610 gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook
),
611 gtk_notebook_page_num(GTK_NOTEBOOK(notebook
), GTK_WIDGET(tab_page
)));
619 static GtkWidget
*vte_create_popup_menu(void)
621 GtkWidget
*menu
, *item
;
622 GtkAccelGroup
*accel_group
;
624 menu
= gtk_menu_new();
626 accel_group
= gtk_accel_group_new();
627 gtk_window_add_accel_group(GTK_WINDOW(main_widgets
.window
), accel_group
);
629 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_COPY
, NULL
);
630 gtk_widget_add_accelerator(item
, "activate", accel_group
,
631 GDK_c
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, GTK_ACCEL_VISIBLE
);
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_COPY
));
636 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_PASTE
, NULL
);
637 gtk_widget_add_accelerator(item
, "activate", accel_group
,
638 GDK_v
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, GTK_ACCEL_VISIBLE
);
639 gtk_widget_show(item
);
640 gtk_container_add(GTK_CONTAINER(menu
), item
);
641 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_PASTE
));
643 item
= gtk_separator_menu_item_new();
644 gtk_widget_show(item
);
645 gtk_container_add(GTK_CONTAINER(menu
), item
);
647 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_SELECT_ALL
, NULL
);
648 gtk_widget_show(item
);
649 gtk_container_add(GTK_CONTAINER(menu
), item
);
650 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_SELECTALL
));
652 item
= gtk_separator_menu_item_new();
653 gtk_widget_show(item
);
654 gtk_container_add(GTK_CONTAINER(menu
), item
);
656 item
= gtk_image_menu_item_new_with_mnemonic(_("_Set Path From Document"));
657 gtk_widget_show(item
);
658 gtk_container_add(GTK_CONTAINER(menu
), item
);
659 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_CHANGEPATH
));
661 item
= gtk_image_menu_item_new_with_mnemonic(_("_Restart Terminal"));
662 gtk_widget_show(item
);
663 gtk_container_add(GTK_CONTAINER(menu
), item
);
664 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_RESTARTTERMINAL
));
666 item
= gtk_separator_menu_item_new();
667 gtk_widget_show(item
);
668 gtk_container_add(GTK_CONTAINER(menu
), item
);
670 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES
, NULL
);
671 gtk_widget_show(item
);
672 gtk_container_add(GTK_CONTAINER(menu
), item
);
673 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_PREFERENCES
));
675 msgwin_menu_add_common_items(GTK_MENU(menu
));
677 item
= gtk_separator_menu_item_new();
678 gtk_widget_show(item
);
679 gtk_container_add(GTK_CONTAINER(menu
), item
);
681 /* the IM submenu should always be the last item to be consistent with other GTK popup menus */
682 vc
->im_submenu
= gtk_menu_new();
684 item
= gtk_image_menu_item_new_with_mnemonic(_("_Input Methods"));
685 gtk_widget_show(item
);
686 gtk_container_add(GTK_CONTAINER(menu
), item
);
688 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item
), vc
->im_submenu
);
689 /* submenu populated after vte realized */
694 /* If the command could be executed, TRUE is returned, FALSE otherwise (i.e. there was some text
696 gboolean
vte_send_cmd(const gchar
*cmd
)
700 vf
->vte_terminal_feed_child(VTE_TERMINAL(vc
->vte
), cmd
, strlen(cmd
));
701 set_clean(TRUE
); /* vte_terminal_feed_child() also marks the vte as not clean */
709 /* Taken from Terminal by os-cillation: terminal_screen_get_working_directory, thanks.
710 * Determines the working directory using various OS-specific mechanisms and stores the determined
711 * directory in vte_info.dir. Note: vte_info.dir contains the real path. */
712 const gchar
*vte_get_working_directory(void)
716 gchar buffer
[4096 + 1];
717 gchar
*file
= g_strdup_printf("/proc/%d/cwd", pid
);
718 gint length
= readlink(file
, buffer
, sizeof(buffer
));
720 if (length
> 0 && *buffer
== '/')
722 buffer
[length
] = '\0';
723 g_free(vte_info
.dir
);
724 vte_info
.dir
= g_strdup(buffer
);
726 else if (length
== 0)
728 gchar
*cwd
= g_get_current_dir();
732 if (chdir(file
) == 0)
734 g_free(vte_info
.dir
);
735 vte_info
.dir
= g_get_current_dir();
737 geany_debug("%s: %s", G_STRFUNC
, g_strerror(errno
));
749 /* Changes the current working directory of the VTE to the path of the given filename.
750 * filename is expected to be in UTF-8 encoding.
751 * filename can also be a path, then it is used directly.
752 * If force is set to TRUE, it will always change the cwd
754 void vte_cwd(const gchar
*filename
, gboolean force
)
756 if (vte_info
.have_vte
&& (vc
->follow_path
|| force
) &&
757 filename
!= NULL
&& g_path_is_absolute(filename
))
761 if (g_file_test(filename
, G_FILE_TEST_IS_DIR
))
762 path
= g_strdup(filename
);
764 path
= g_path_get_dirname(filename
);
766 vte_get_working_directory(); /* refresh vte_info.dir */
767 if (! utils_str_equal(path
, vte_info
.dir
))
769 /* use g_shell_quote to avoid problems with spaces, '!' or something else in path */
770 gchar
*quoted_path
= g_shell_quote(path
);
771 gchar
*cmd
= g_strconcat(vc
->send_cmd_prefix
, "cd ", quoted_path
, "\n", NULL
);
772 if (! vte_send_cmd(cmd
))
774 const gchar
*msg
= _("Directory not changed because the terminal may contain some input (press Ctrl+C or Enter to clear it).");
775 ui_set_statusbar(FALSE
, "%s", msg
);
776 geany_debug("%s", msg
);
786 static void vte_drag_data_received(GtkWidget
*widget
, GdkDragContext
*drag_context
,
787 gint x
, gint y
, GtkSelectionData
*data
, guint info
, guint ltime
)
789 if (info
== TARGET_TEXT_PLAIN
)
791 if (gtk_selection_data_get_format(data
) == 8 && gtk_selection_data_get_length(data
) > 0)
792 vf
->vte_terminal_feed_child(VTE_TERMINAL(widget
),
793 (const gchar
*) gtk_selection_data_get_data(data
),
794 gtk_selection_data_get_length(data
));
798 gchar
*text
= (gchar
*) gtk_selection_data_get_text(data
);
800 vf
->vte_terminal_feed_child(VTE_TERMINAL(widget
), text
, strlen(text
));
803 gtk_drag_finish(drag_context
, TRUE
, FALSE
, ltime
);
807 static void on_check_run_in_vte_toggled(GtkToggleButton
*togglebutton
, GtkWidget
*user_data
)
809 g_return_if_fail(GTK_IS_WIDGET(user_data
));
810 gtk_widget_set_sensitive(user_data
, gtk_toggle_button_get_active(togglebutton
));
814 static void on_term_font_set(GtkFontButton
*widget
, gpointer user_data
)
816 const gchar
*fontbtn
= gtk_font_button_get_font_name(widget
);
818 if (! utils_str_equal(fontbtn
, vc
->font
))
820 SETPTR(vc
->font
, g_strdup(gtk_font_button_get_font_name(widget
)));
821 vte_apply_user_settings();
826 static void on_term_fg_color_set(GtkColorButton
*widget
, gpointer user_data
)
828 gtk_color_button_get_color(widget
, &vc
->colour_fore
);
832 static void on_term_bg_color_set(GtkColorButton
*widget
, gpointer user_data
)
834 gtk_color_button_get_color(widget
, &vc
->colour_back
);
838 void vte_append_preferences_tab(void)
840 if (vte_info
.have_vte
)
842 GtkWidget
*frame_term
, *button_shell
, *entry_shell
;
843 GtkWidget
*check_run_in_vte
, *check_skip_script
;
844 GtkWidget
*font_button
, *fg_color_button
, *bg_color_button
;
845 GtkWidget
*entry_image
, *button_image
;
847 button_shell
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "button_term_shell"));
848 entry_shell
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "entry_shell"));
849 ui_setup_open_button_callback(button_shell
, NULL
,
850 GTK_FILE_CHOOSER_ACTION_OPEN
, GTK_ENTRY(entry_shell
));
852 button_image
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "button_term_image"));
853 entry_image
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "entry_image"));
854 ui_setup_open_button_callback(button_image
, NULL
,
855 GTK_FILE_CHOOSER_ACTION_OPEN
, GTK_ENTRY(entry_image
));
857 check_skip_script
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "check_skip_script"));
858 gtk_widget_set_sensitive(check_skip_script
, vc
->run_in_vte
);
860 check_run_in_vte
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "check_run_in_vte"));
861 g_signal_connect(G_OBJECT(check_run_in_vte
), "toggled",
862 G_CALLBACK(on_check_run_in_vte_toggled
), check_skip_script
);
864 font_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "font_term");
865 g_signal_connect(font_button
, "font-set", G_CALLBACK(on_term_font_set
), NULL
);
867 fg_color_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "color_fore");
868 g_signal_connect(fg_color_button
, "color-set", G_CALLBACK(on_term_fg_color_set
), NULL
);
870 bg_color_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "color_back");
871 g_signal_connect(bg_color_button
, "color-set", G_CALLBACK(on_term_bg_color_set
), NULL
);
873 frame_term
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "frame_term");
874 gtk_widget_show_all(frame_term
);
879 void vte_select_all(void)
881 if (vf
->vte_terminal_select_all
!= NULL
)
882 vf
->vte_terminal_select_all(VTE_TERMINAL(vc
->vte
));
886 void vte_send_selection_to_vte(void)
892 doc
= document_get_current();
893 g_return_if_fail(doc
!= NULL
);
895 if (sci_has_selection(doc
->editor
->sci
))
897 text
= sci_get_selection_contents(doc
->editor
->sci
);
900 { /* Get the current line */
901 gint line_num
= sci_get_current_line(doc
->editor
->sci
);
902 text
= sci_get_line(doc
->editor
->sci
, line_num
);
907 if (vc
->send_selection_unsafe
)
908 { /* Explicitly append a trailing newline character to get the command executed,
909 this is disabled by default as it could cause all sorts of damage. */
910 if (text
[len
-1] != '\n' && text
[len
-1] != '\r')
912 SETPTR(text
, g_strconcat(text
, "\n", NULL
));
917 { /* Make sure there is no newline character at the end to prevent unwanted execution */
918 while (text
[len
-1] == '\n' || text
[len
-1] == '\r')
925 vf
->vte_terminal_feed_child(VTE_TERMINAL(vc
->vte
), text
, len
);
928 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_VTE
);
929 gtk_widget_grab_focus(vc
->vte
);
930 msgwin_show_hide(TRUE
);