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>
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_from_string
) (VteTerminal
*terminal
, const char *name
);
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
);
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
);
146 POPUP_RESTARTTERMINAL
,
148 TARGET_UTF8_STRING
= 0,
150 TARGET_COMPOUND_TEXT
,
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 static gchar
**vte_get_child_environment(void)
167 const gchar
*exclude_vars
[] = {"COLUMNS", "LINES", "TERM", "TERM_PROGRAM", NULL
};
169 return utils_copy_environment(exclude_vars
, "TERM", "xterm", NULL
);
173 static void override_menu_key(void)
175 if (gtk_menu_key_accel
== NULL
) /* for restoring the default value */
176 g_object_get(G_OBJECT(gtk_settings_get_default()),
177 "gtk-menu-bar-accel", >k_menu_key_accel
, NULL
);
179 if (vc
->ignore_menu_bar_accel
)
180 gtk_settings_set_string_property(gtk_settings_get_default(),
181 "gtk-menu-bar-accel", "<Shift><Control><Mod1><Mod2><Mod3><Mod4><Mod5>F10", "Geany");
183 gtk_settings_set_string_property(gtk_settings_get_default(),
184 "gtk-menu-bar-accel", gtk_menu_key_accel
, "Geany");
188 static void on_startup_complete(G_GNUC_UNUSED GObject
*dummy
)
190 GeanyDocument
*doc
= document_get_current();
193 vte_cwd((doc
->real_path
!= NULL
) ? doc
->real_path
: doc
->file_name
, FALSE
);
199 if (vte_info
.have_vte
== FALSE
)
200 { /* vte_info.have_vte can be false even if VTE is compiled in, think of command line option */
201 geany_debug("Disabling terminal support");
205 if (!EMPTY(vte_info
.lib_vte
))
207 module
= g_module_open(vte_info
.lib_vte
, G_MODULE_BIND_LAZY
);
209 #ifdef VTE_MODULE_PATH
212 module
= g_module_open(VTE_MODULE_PATH
, G_MODULE_BIND_LAZY
);
219 const gchar
*sonames
[] = {
220 #if GTK_CHECK_VERSION(3, 0, 0)
221 "libvte2_90.so", "libvte2_90.so.9", "libvte2_90.dylib",
223 "libvte.so", "libvte.so.4", "libvte.so.8", "libvte.so.9", "libvte.dylib",
228 for (i
= 0; sonames
[i
] != NULL
&& module
== NULL
; i
++)
230 module
= g_module_open(sonames
[i
], G_MODULE_BIND_LAZY
);
236 vte_info
.have_vte
= FALSE
;
237 geany_debug("Could not load libvte.so, embedded terminal support disabled");
242 vf
= g_new0(struct VteFunctions
, 1);
243 if (vte_register_symbols(module
))
244 vte_info
.have_vte
= TRUE
;
247 vte_info
.have_vte
= FALSE
;
249 /* FIXME: is closing the module safe? see vte_close() and test on FreeBSD */
250 /*g_module_close(module);*/
258 /* setup the F10 menu override (so it works before the widget is first realised). */
261 g_signal_connect(geany_object
, "geany-startup-complete", G_CALLBACK(on_startup_complete
), NULL
);
265 static void on_vte_realize(void)
267 /* the vte widget has to be realised before color changes take effect */
268 vte_apply_user_settings();
270 vf
->vte_terminal_im_append_menuitems(VTE_TERMINAL(vc
->vte
), GTK_MENU_SHELL(vc
->im_submenu
));
274 static gboolean
vte_start_idle(G_GNUC_UNUSED gpointer user_data
)
281 static void create_vte(void)
283 GtkWidget
*vte
, *scrollbar
, *hbox
;
285 vc
->vte
= vte
= vf
->vte_terminal_new();
286 scrollbar
= gtk_vscrollbar_new(GTK_ADJUSTMENT(VTE_TERMINAL(vte
)->adjustment
));
287 gtk_widget_set_can_focus(scrollbar
, FALSE
);
289 /* create menu now so copy/paste shortcuts work */
290 vc
->menu
= vte_create_popup_menu();
291 g_object_ref_sink(vc
->menu
);
293 hbox
= gtk_hbox_new(FALSE
, 0);
294 gtk_box_pack_start(GTK_BOX(hbox
), vte
, TRUE
, TRUE
, 0);
295 gtk_box_pack_start(GTK_BOX(hbox
), scrollbar
, FALSE
, FALSE
, 0);
297 /* set the default widget size first to prevent VTE expanding too much,
298 * sometimes causing the hscrollbar to be too big or out of view. */
299 gtk_widget_set_size_request(GTK_WIDGET(vte
), 10, 10);
300 vf
->vte_terminal_set_size(VTE_TERMINAL(vte
), 30, 1);
302 vf
->vte_terminal_set_mouse_autohide(VTE_TERMINAL(vte
), TRUE
);
303 vf
->vte_terminal_set_word_chars(VTE_TERMINAL(vte
), VTE_WORDCHARS
);
305 gtk_drag_dest_set(vte
, GTK_DEST_DEFAULT_ALL
,
306 dnd_targets
, G_N_ELEMENTS(dnd_targets
), GDK_ACTION_COPY
);
308 g_signal_connect(vte
, "child-exited", G_CALLBACK(vte_start
), NULL
);
309 g_signal_connect(vte
, "button-press-event", G_CALLBACK(vte_button_pressed
), NULL
);
310 g_signal_connect(vte
, "event", G_CALLBACK(vte_keypress_cb
), NULL
);
311 g_signal_connect(vte
, "key-release-event", G_CALLBACK(vte_keyrelease_cb
), NULL
);
312 g_signal_connect(vte
, "commit", G_CALLBACK(vte_commit_cb
), NULL
);
313 g_signal_connect(vte
, "motion-notify-event", G_CALLBACK(on_motion_event
), NULL
);
314 g_signal_connect(vte
, "drag-data-received", G_CALLBACK(vte_drag_data_received
), NULL
);
316 /* start shell on idle otherwise the initial prompt can get corrupted */
317 g_idle_add(vte_start_idle
, NULL
);
319 gtk_widget_show_all(hbox
);
320 terminal_label
= gtk_label_new(_("Terminal"));
321 gtk_notebook_insert_page(GTK_NOTEBOOK(msgwindow
.notebook
), hbox
, terminal_label
, MSG_VTE
);
323 g_signal_connect_after(vte
, "realize", G_CALLBACK(on_vte_realize
), NULL
);
330 /* free the vte widget before unloading vte module
331 * this prevents a segfault on X close window if the message window is hidden */
332 gtk_widget_destroy(vc
->vte
);
333 gtk_widget_destroy(vc
->menu
);
334 g_object_unref(vc
->menu
);
335 g_free(vc
->emulation
);
339 g_free(vc
->send_cmd_prefix
);
341 g_free(gtk_menu_key_accel
);
342 /* Don't unload the module explicitly because it causes a segfault on FreeBSD. The segfault
343 * happens when the app really exits, not directly on g_module_close(). This still needs to
344 * be investigated. */
345 /*g_module_close(module); */
349 static gboolean
set_dirty_idle(gpointer user_data
)
351 gtk_widget_set_name(terminal_label
, "geany-terminal-dirty");
352 terminal_label_update_source
= 0;
357 static void set_clean(gboolean value
)
363 if (terminal_label_update_source
> 0)
365 g_source_remove(terminal_label_update_source
);
366 terminal_label_update_source
= 0;
369 gtk_widget_set_name(terminal_label
, NULL
);
371 terminal_label_update_source
= g_timeout_add(150, set_dirty_idle
, NULL
);
378 static gboolean
vte_keyrelease_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
380 if (ui_is_keyval_enter_or_return(event
->keyval
) ||
381 ((event
->keyval
== GDK_c
) && (event
->state
& GDK_CONTROL_MASK
)))
383 /* assume any text on the prompt has been executed when pressing Enter/Return */
390 static gboolean
vte_keypress_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
392 if (vc
->enable_bash_keys
)
393 return FALSE
; /* Ctrl-[CD] will be handled by the VTE itself */
395 if (event
->type
!= GDK_KEY_RELEASE
)
398 if ((event
->keyval
== GDK_c
||
399 event
->keyval
== GDK_d
||
400 event
->keyval
== GDK_C
||
401 event
->keyval
== GDK_D
) &&
402 event
->state
& GDK_CONTROL_MASK
&&
403 ! (event
->state
& GDK_SHIFT_MASK
) && ! (event
->state
& GDK_MOD1_MASK
))
412 static void vte_commit_cb(VteTerminal
*vte
, gchar
*arg1
, guint arg2
, gpointer user_data
)
418 static void vte_start(GtkWidget
*widget
)
423 /* split the shell command line, so arguments will work too */
424 argv
= g_strsplit(vc
->shell
, " ", -1);
428 env
= vte_get_child_environment();
429 pid
= vf
->vte_terminal_fork_command(VTE_TERMINAL(widget
), argv
[0], argv
, env
,
430 vte_info
.dir
, TRUE
, TRUE
, TRUE
);
435 pid
= 0; /* use 0 as invalid pid */
441 static void vte_restart(GtkWidget
*widget
)
443 vte_get_working_directory(); /* try to keep the working directory when restarting the VTE */
449 vf
->vte_terminal_reset(VTE_TERMINAL(widget
), TRUE
, TRUE
);
454 static gboolean
vte_button_pressed(GtkWidget
*widget
, GdkEventButton
*event
, gpointer user_data
)
456 if (event
->button
== 3)
458 gtk_widget_grab_focus(vc
->vte
);
459 gtk_menu_popup(GTK_MENU(vc
->menu
), NULL
, NULL
, NULL
, NULL
, event
->button
, event
->time
);
461 else if (event
->button
== 2)
463 gtk_widget_grab_focus(widget
);
469 static void vte_set_cursor_blink_mode(void)
471 if (vf
->vte_terminal_set_cursor_blink_mode
!= NULL
)
473 vf
->vte_terminal_set_cursor_blink_mode(VTE_TERMINAL(vc
->vte
),
474 (vc
->cursor_blinks
) ? VTE_CURSOR_BLINK_ON
: VTE_CURSOR_BLINK_OFF
);
477 vf
->vte_terminal_set_cursor_blinks(VTE_TERMINAL(vc
->vte
), vc
->cursor_blinks
);
481 static gboolean
vte_register_symbols(GModule
*mod
)
483 #define BIND_SYMBOL(field) \
484 g_module_symbol(mod, #field, (void*)&vf->field)
485 #define BIND_REQUIRED_SYMBOL(field) \
487 if (! BIND_SYMBOL(field)) \
489 g_critical(_("invalid VTE library \"%s\": missing symbol \"%s\""), \
490 g_module_name(mod), #field); \
495 BIND_REQUIRED_SYMBOL(vte_terminal_new
);
496 BIND_REQUIRED_SYMBOL(vte_terminal_set_size
);
497 BIND_REQUIRED_SYMBOL(vte_terminal_fork_command
);
498 BIND_REQUIRED_SYMBOL(vte_terminal_set_word_chars
);
499 BIND_REQUIRED_SYMBOL(vte_terminal_set_mouse_autohide
);
500 BIND_REQUIRED_SYMBOL(vte_terminal_reset
);
501 BIND_REQUIRED_SYMBOL(vte_terminal_get_type
);
502 BIND_REQUIRED_SYMBOL(vte_terminal_set_scroll_on_output
);
503 BIND_REQUIRED_SYMBOL(vte_terminal_set_scroll_on_keystroke
);
504 BIND_REQUIRED_SYMBOL(vte_terminal_set_font_from_string
);
505 BIND_REQUIRED_SYMBOL(vte_terminal_set_scrollback_lines
);
506 BIND_REQUIRED_SYMBOL(vte_terminal_get_has_selection
);
507 BIND_REQUIRED_SYMBOL(vte_terminal_copy_clipboard
);
508 BIND_REQUIRED_SYMBOL(vte_terminal_paste_clipboard
);
509 BIND_REQUIRED_SYMBOL(vte_terminal_set_emulation
);
510 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_foreground
);
511 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_bold
);
512 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_background
);
513 BIND_REQUIRED_SYMBOL(vte_terminal_set_background_image_file
);
514 BIND_REQUIRED_SYMBOL(vte_terminal_feed_child
);
515 BIND_REQUIRED_SYMBOL(vte_terminal_im_append_menuitems
);
516 if (! BIND_SYMBOL(vte_terminal_set_cursor_blink_mode
))
517 /* vte_terminal_set_cursor_blink_mode() is only available since 0.17.1, so if we don't find
518 * this symbol, we are probably on an older version and use the old API instead */
519 BIND_REQUIRED_SYMBOL(vte_terminal_set_cursor_blinks
);
520 BIND_REQUIRED_SYMBOL(vte_terminal_select_all
);
521 BIND_REQUIRED_SYMBOL(vte_terminal_set_audible_bell
);
523 #undef BIND_REQUIRED_SYMBOL
530 void vte_apply_user_settings(void)
532 if (! ui_prefs
.msgwindow_visible
)
535 vf
->vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->vte
), vc
->scrollback_lines
);
536 vf
->vte_terminal_set_scroll_on_keystroke(VTE_TERMINAL(vc
->vte
), vc
->scroll_on_key
);
537 vf
->vte_terminal_set_scroll_on_output(VTE_TERMINAL(vc
->vte
), vc
->scroll_on_out
);
538 vf
->vte_terminal_set_emulation(VTE_TERMINAL(vc
->vte
), vc
->emulation
);
539 vf
->vte_terminal_set_font_from_string(VTE_TERMINAL(vc
->vte
), vc
->font
);
540 vf
->vte_terminal_set_color_foreground(VTE_TERMINAL(vc
->vte
), &vc
->colour_fore
);
541 vf
->vte_terminal_set_color_bold(VTE_TERMINAL(vc
->vte
), &vc
->colour_fore
);
542 vf
->vte_terminal_set_color_background(VTE_TERMINAL(vc
->vte
), &vc
->colour_back
);
543 vf
->vte_terminal_set_background_image_file(VTE_TERMINAL(vc
->vte
), vc
->image
);
544 vf
->vte_terminal_set_audible_bell(VTE_TERMINAL(vc
->vte
), prefs
.beep_on_errors
);
545 vte_set_cursor_blink_mode();
551 static void vte_popup_menu_clicked(GtkMenuItem
*menuitem
, gpointer user_data
)
553 switch (GPOINTER_TO_INT(user_data
))
557 if (vf
->vte_terminal_get_has_selection(VTE_TERMINAL(vc
->vte
)))
558 vf
->vte_terminal_copy_clipboard(VTE_TERMINAL(vc
->vte
));
563 vf
->vte_terminal_paste_clipboard(VTE_TERMINAL(vc
->vte
));
566 case POPUP_SELECTALL
:
571 case POPUP_CHANGEPATH
:
573 GeanyDocument
*doc
= document_get_current();
575 vte_cwd(doc
->file_name
, TRUE
);
578 case POPUP_RESTARTTERMINAL
:
580 vte_restart(vc
->vte
);
583 case POPUP_PREFERENCES
:
585 GtkWidget
*notebook
, *tab_page
;
589 notebook
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "notebook2");
590 tab_page
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "frame_term");
592 gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook
),
593 gtk_notebook_page_num(GTK_NOTEBOOK(notebook
), GTK_WIDGET(tab_page
)));
601 static GtkWidget
*vte_create_popup_menu(void)
603 GtkWidget
*menu
, *item
;
604 GtkAccelGroup
*accel_group
;
606 menu
= gtk_menu_new();
608 accel_group
= gtk_accel_group_new();
609 gtk_window_add_accel_group(GTK_WINDOW(main_widgets
.window
), accel_group
);
611 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_COPY
, NULL
);
612 gtk_widget_add_accelerator(item
, "activate", accel_group
,
613 GDK_c
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, GTK_ACCEL_VISIBLE
);
614 gtk_widget_show(item
);
615 gtk_container_add(GTK_CONTAINER(menu
), item
);
616 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_COPY
));
618 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_PASTE
, NULL
);
619 gtk_widget_add_accelerator(item
, "activate", accel_group
,
620 GDK_v
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, GTK_ACCEL_VISIBLE
);
621 gtk_widget_show(item
);
622 gtk_container_add(GTK_CONTAINER(menu
), item
);
623 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_PASTE
));
625 item
= gtk_separator_menu_item_new();
626 gtk_widget_show(item
);
627 gtk_container_add(GTK_CONTAINER(menu
), item
);
629 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_SELECT_ALL
, NULL
);
630 gtk_widget_show(item
);
631 gtk_container_add(GTK_CONTAINER(menu
), item
);
632 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_SELECTALL
));
634 item
= gtk_separator_menu_item_new();
635 gtk_widget_show(item
);
636 gtk_container_add(GTK_CONTAINER(menu
), item
);
638 item
= gtk_image_menu_item_new_with_mnemonic(_("_Set Path From Document"));
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_CHANGEPATH
));
643 item
= gtk_image_menu_item_new_with_mnemonic(_("_Restart Terminal"));
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_RESTARTTERMINAL
));
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_from_stock(GTK_STOCK_PREFERENCES
, NULL
);
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_PREFERENCES
));
657 msgwin_menu_add_common_items(GTK_MENU(menu
));
659 item
= gtk_separator_menu_item_new();
660 gtk_widget_show(item
);
661 gtk_container_add(GTK_CONTAINER(menu
), item
);
663 /* the IM submenu should always be the last item to be consistent with other GTK popup menus */
664 vc
->im_submenu
= gtk_menu_new();
666 item
= gtk_image_menu_item_new_with_mnemonic(_("_Input Methods"));
667 gtk_widget_show(item
);
668 gtk_container_add(GTK_CONTAINER(menu
), item
);
670 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item
), vc
->im_submenu
);
671 /* submenu populated after vte realized */
676 /* If the command could be executed, TRUE is returned, FALSE otherwise (i.e. there was some text
678 gboolean
vte_send_cmd(const gchar
*cmd
)
682 vf
->vte_terminal_feed_child(VTE_TERMINAL(vc
->vte
), cmd
, strlen(cmd
));
683 set_clean(TRUE
); /* vte_terminal_feed_child() also marks the vte as not clean */
691 /* Taken from Terminal by os-cillation: terminal_screen_get_working_directory, thanks.
692 * Determines the working directory using various OS-specific mechanisms and stores the determined
693 * directory in vte_info.dir. Note: vte_info.dir contains the real path. */
694 const gchar
*vte_get_working_directory(void)
696 gchar buffer
[4096 + 1];
703 file
= g_strdup_printf("/proc/%d/cwd", pid
);
704 length
= readlink(file
, buffer
, sizeof(buffer
));
706 if (length
> 0 && *buffer
== '/')
708 buffer
[length
] = '\0';
709 g_free(vte_info
.dir
);
710 vte_info
.dir
= g_strdup(buffer
);
712 else if (length
== 0)
714 cwd
= g_get_current_dir();
717 if (chdir(file
) == 0)
719 g_free(vte_info
.dir
);
720 vte_info
.dir
= g_get_current_dir();
722 geany_debug("%s: %s", G_STRFUNC
, g_strerror(errno
));
734 /* Changes the current working directory of the VTE to the path of the given filename.
735 * filename is expected to be in UTF-8 encoding.
736 * filename can also be a path, then it is used directly.
737 * If force is set to TRUE, it will always change the cwd
739 void vte_cwd(const gchar
*filename
, gboolean force
)
741 if (vte_info
.have_vte
&& (vc
->follow_path
|| force
) &&
742 filename
!= NULL
&& g_path_is_absolute(filename
))
746 if (g_file_test(filename
, G_FILE_TEST_IS_DIR
))
747 path
= g_strdup(filename
);
749 path
= g_path_get_dirname(filename
);
751 vte_get_working_directory(); /* refresh vte_info.dir */
752 if (! utils_str_equal(path
, vte_info
.dir
))
754 /* use g_shell_quote to avoid problems with spaces, '!' or something else in path */
755 gchar
*quoted_path
= g_shell_quote(path
);
756 gchar
*cmd
= g_strconcat(vc
->send_cmd_prefix
, "cd ", quoted_path
, "\n", NULL
);
757 if (! vte_send_cmd(cmd
))
759 const gchar
*msg
= _("Directory not changed because the terminal may contain some input (press Ctrl+C or Enter to clear it).");
760 ui_set_statusbar(FALSE
, "%s", msg
);
761 geany_debug("%s", msg
);
771 static void vte_drag_data_received(GtkWidget
*widget
, GdkDragContext
*drag_context
,
772 gint x
, gint y
, GtkSelectionData
*data
, guint info
, guint ltime
)
774 if (info
== TARGET_TEXT_PLAIN
)
776 if (gtk_selection_data_get_format(data
) == 8 && gtk_selection_data_get_length(data
) > 0)
777 vf
->vte_terminal_feed_child(VTE_TERMINAL(widget
),
778 (const gchar
*) gtk_selection_data_get_data(data
),
779 gtk_selection_data_get_length(data
));
783 gchar
*text
= (gchar
*) gtk_selection_data_get_text(data
);
785 vf
->vte_terminal_feed_child(VTE_TERMINAL(widget
), text
, strlen(text
));
788 gtk_drag_finish(drag_context
, TRUE
, FALSE
, ltime
);
792 static void on_check_run_in_vte_toggled(GtkToggleButton
*togglebutton
, GtkWidget
*user_data
)
794 g_return_if_fail(GTK_IS_WIDGET(user_data
));
795 gtk_widget_set_sensitive(user_data
, gtk_toggle_button_get_active(togglebutton
));
799 static void on_term_font_set(GtkFontButton
*widget
, gpointer user_data
)
801 const gchar
*fontbtn
= gtk_font_button_get_font_name(widget
);
803 if (! utils_str_equal(fontbtn
, vc
->font
))
805 SETPTR(vc
->font
, g_strdup(gtk_font_button_get_font_name(widget
)));
806 vte_apply_user_settings();
811 static void on_term_fg_color_set(GtkColorButton
*widget
, gpointer user_data
)
813 gtk_color_button_get_color(widget
, &vc
->colour_fore
);
817 static void on_term_bg_color_set(GtkColorButton
*widget
, gpointer user_data
)
819 gtk_color_button_get_color(widget
, &vc
->colour_back
);
823 void vte_append_preferences_tab(void)
825 if (vte_info
.have_vte
)
827 GtkWidget
*frame_term
, *button_shell
, *entry_shell
;
828 GtkWidget
*check_run_in_vte
, *check_skip_script
;
829 GtkWidget
*font_button
, *fg_color_button
, *bg_color_button
;
830 GtkWidget
*entry_image
, *button_image
;
832 button_shell
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "button_term_shell"));
833 entry_shell
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "entry_shell"));
834 ui_setup_open_button_callback(button_shell
, NULL
,
835 GTK_FILE_CHOOSER_ACTION_OPEN
, GTK_ENTRY(entry_shell
));
837 button_image
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "button_term_image"));
838 entry_image
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "entry_image"));
839 ui_setup_open_button_callback(button_image
, NULL
,
840 GTK_FILE_CHOOSER_ACTION_OPEN
, GTK_ENTRY(entry_image
));
842 check_skip_script
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "check_skip_script"));
843 gtk_widget_set_sensitive(check_skip_script
, vc
->run_in_vte
);
845 check_run_in_vte
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "check_run_in_vte"));
846 g_signal_connect(G_OBJECT(check_run_in_vte
), "toggled",
847 G_CALLBACK(on_check_run_in_vte_toggled
), check_skip_script
);
849 font_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "font_term");
850 g_signal_connect(font_button
, "font-set", G_CALLBACK(on_term_font_set
), NULL
);
852 fg_color_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "color_fore");
853 g_signal_connect(fg_color_button
, "color-set", G_CALLBACK(on_term_fg_color_set
), NULL
);
855 bg_color_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "color_back");
856 g_signal_connect(bg_color_button
, "color-set", G_CALLBACK(on_term_bg_color_set
), NULL
);
858 frame_term
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "frame_term");
859 gtk_widget_show_all(frame_term
);
864 void vte_select_all(void)
866 if (vf
->vte_terminal_select_all
!= NULL
)
867 vf
->vte_terminal_select_all(VTE_TERMINAL(vc
->vte
));
871 void vte_send_selection_to_vte(void)
877 doc
= document_get_current();
878 g_return_if_fail(doc
!= NULL
);
880 if (sci_has_selection(doc
->editor
->sci
))
882 text
= sci_get_selection_contents(doc
->editor
->sci
);
885 { /* Get the current line */
886 gint line_num
= sci_get_current_line(doc
->editor
->sci
);
887 text
= sci_get_line(doc
->editor
->sci
, line_num
);
892 if (vc
->send_selection_unsafe
)
893 { /* Explicitly append a trailing newline character to get the command executed,
894 this is disabled by default as it could cause all sorts of damage. */
895 if (text
[len
-1] != '\n' && text
[len
-1] != '\r')
897 SETPTR(text
, g_strconcat(text
, "\n", NULL
));
902 { /* Make sure there is no newline character at the end to prevent unwanted execution */
903 while (text
[len
-1] == '\n' || text
[len
-1] == '\r')
910 vf
->vte_terminal_feed_child(VTE_TERMINAL(vc
->vte
), text
, len
);
913 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_VTE
);
914 gtk_widget_grab_focus(vc
->vte
);
915 msgwin_show_hide(TRUE
);