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,./?%&#:_";
72 static const gchar VTE_ADDITIONAL_WORDCHARS
[] = "-,./?%&#:_";
75 /* Incomplete VteTerminal struct from vte/vte.h. */
76 typedef struct _VteTerminal VteTerminal
;
80 GtkAdjustment
*adjustment
;
83 #define VTE_TERMINAL(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), VTE_TYPE_TERMINAL, VteTerminal))
84 #define VTE_TYPE_TERMINAL (vf->vte_terminal_get_type())
87 VTE_CURSOR_BLINK_SYSTEM
,
90 } VteTerminalCursorBlinkMode
;
93 /* we don't care for the other possible values */
98 /* Holds function pointers we need to access the VTE API. */
101 guint (*vte_get_major_version
) (void);
102 guint (*vte_get_minor_version
) (void);
103 GtkWidget
* (*vte_terminal_new
) (void);
104 pid_t (*vte_terminal_fork_command
) (VteTerminal
*terminal
, const char *command
, char **argv
,
105 char **envv
, const char *directory
, gboolean lastlog
,
106 gboolean utmp
, gboolean wtmp
);
107 gboolean (*vte_terminal_spawn_sync
) (VteTerminal
*terminal
, VtePtyFlags pty_flags
,
108 const char *working_directory
, char **argv
, char **envv
,
109 GSpawnFlags spawn_flags
, GSpawnChildSetupFunc child_setup
,
110 gpointer child_setup_data
, GPid
*child_pid
,
111 GCancellable
*cancellable
, GError
**error
);
112 void (*vte_terminal_set_size
) (VteTerminal
*terminal
, glong columns
, glong rows
);
113 void (*vte_terminal_set_word_chars
) (VteTerminal
*terminal
, const char *spec
);
114 void (*vte_terminal_set_word_char_exceptions
) (VteTerminal
*terminal
, const char *exceptions
);
115 void (*vte_terminal_set_mouse_autohide
) (VteTerminal
*terminal
, gboolean setting
);
116 void (*vte_terminal_reset
) (VteTerminal
*terminal
, gboolean full
, gboolean clear_history
);
117 GType (*vte_terminal_get_type
) (void);
118 void (*vte_terminal_set_scroll_on_output
) (VteTerminal
*terminal
, gboolean scroll
);
119 void (*vte_terminal_set_scroll_on_keystroke
) (VteTerminal
*terminal
, gboolean scroll
);
120 void (*vte_terminal_set_font
) (VteTerminal
*terminal
, const PangoFontDescription
*font_desc
);
121 void (*vte_terminal_set_scrollback_lines
) (VteTerminal
*terminal
, glong lines
);
122 gboolean (*vte_terminal_get_has_selection
) (VteTerminal
*terminal
);
123 void (*vte_terminal_copy_clipboard
) (VteTerminal
*terminal
);
124 void (*vte_terminal_paste_clipboard
) (VteTerminal
*terminal
);
125 void (*vte_terminal_set_color_foreground
) (VteTerminal
*terminal
, const GdkColor
*foreground
);
126 void (*vte_terminal_set_color_bold
) (VteTerminal
*terminal
, const GdkColor
*foreground
);
127 void (*vte_terminal_set_color_background
) (VteTerminal
*terminal
, const GdkColor
*background
);
128 void (*vte_terminal_feed_child
) (VteTerminal
*terminal
, const char *data
, glong length
);
129 void (*vte_terminal_im_append_menuitems
) (VteTerminal
*terminal
, GtkMenuShell
*menushell
);
130 void (*vte_terminal_set_cursor_blink_mode
) (VteTerminal
*terminal
,
131 VteTerminalCursorBlinkMode mode
);
132 void (*vte_terminal_set_cursor_blinks
) (VteTerminal
*terminal
, gboolean blink
);
133 void (*vte_terminal_select_all
) (VteTerminal
*terminal
);
134 void (*vte_terminal_set_audible_bell
) (VteTerminal
*terminal
, gboolean is_audible
);
135 GtkAdjustment
* (*vte_terminal_get_adjustment
) (VteTerminal
*terminal
);
136 #if GTK_CHECK_VERSION(3, 0, 0)
137 /* hack for the VTE 2.91 API using GdkRGBA: we wrap the API to keep using GdkColor on our side */
138 void (*vte_terminal_set_color_foreground_rgba
) (VteTerminal
*terminal
, const GdkRGBA
*foreground
);
139 void (*vte_terminal_set_color_bold_rgba
) (VteTerminal
*terminal
, const GdkRGBA
*foreground
);
140 void (*vte_terminal_set_color_background_rgba
) (VteTerminal
*terminal
, const GdkRGBA
*background
);
145 static void create_vte(void);
146 static void vte_start(GtkWidget
*widget
);
147 static void vte_restart(GtkWidget
*widget
);
148 static gboolean
vte_button_pressed(GtkWidget
*widget
, GdkEventButton
*event
, gpointer user_data
);
149 static gboolean
vte_keyrelease_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
);
150 static gboolean
vte_keypress_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
);
151 static gboolean
vte_register_symbols(GModule
*module
);
152 static void vte_popup_menu_clicked(GtkMenuItem
*menuitem
, gpointer user_data
);
153 static GtkWidget
*vte_create_popup_menu(void);
154 static void vte_commit_cb(VteTerminal
*vte
, gchar
*arg1
, guint arg2
, gpointer user_data
);
155 static void vte_drag_data_received(GtkWidget
*widget
, GdkDragContext
*drag_context
,
156 gint x
, gint y
, GtkSelectionData
*data
, guint info
, guint ltime
);
165 POPUP_RESTARTTERMINAL
,
167 TARGET_UTF8_STRING
= 0,
169 TARGET_COMPOUND_TEXT
,
174 static const GtkTargetEntry dnd_targets
[] =
176 { "UTF8_STRING", 0, TARGET_UTF8_STRING
},
177 { "TEXT", 0, TARGET_TEXT
},
178 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT
},
179 { "STRING", 0, TARGET_STRING
},
180 { "text/plain", 0, TARGET_TEXT_PLAIN
},
184 /* replacement for vte_terminal_get_adjustment() when it's not available */
185 static GtkAdjustment
*default_vte_terminal_get_adjustment(VteTerminal
*vte
)
187 #if GTK_CHECK_VERSION(3, 0, 0)
188 if (GTK_IS_SCROLLABLE(vte
))
189 return gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(vte
));
191 /* this is only valid in < 0.38, 0.38 broke ABI */
192 return vte
->adjustment
;
196 #if GTK_CHECK_VERSION(3, 0, 0)
197 /* Wrap VTE 2.91 API using GdkRGBA with GdkColor so we use a single API on our side */
199 static void rgba_from_color(GdkRGBA
*rgba
, const GdkColor
*color
)
201 rgba
->red
= color
->red
/ 65535.0;
202 rgba
->green
= color
->green
/ 65535.0;
203 rgba
->blue
= color
->blue
/ 65535.0;
207 # define WRAP_RGBA_SETTER(name) \
208 static void wrap_##name(VteTerminal *terminal, const GdkColor *color) \
211 rgba_from_color(&rgba, color); \
212 vf->name##_rgba(terminal, &rgba); \
215 WRAP_RGBA_SETTER(vte_terminal_set_color_background
)
216 WRAP_RGBA_SETTER(vte_terminal_set_color_bold
)
217 WRAP_RGBA_SETTER(vte_terminal_set_color_foreground
)
219 # undef WRAP_RGBA_SETTER
223 static gchar
**vte_get_child_environment(void)
225 const gchar
*exclude_vars
[] = {"COLUMNS", "LINES", "TERM", "TERM_PROGRAM", NULL
};
227 return utils_copy_environment(exclude_vars
, "TERM", "xterm", NULL
);
231 static void override_menu_key(void)
233 if (gtk_menu_key_accel
== NULL
) /* for restoring the default value */
234 g_object_get(G_OBJECT(gtk_settings_get_default()),
235 "gtk-menu-bar-accel", >k_menu_key_accel
, NULL
);
237 if (vc
->ignore_menu_bar_accel
)
238 gtk_settings_set_string_property(gtk_settings_get_default(),
239 "gtk-menu-bar-accel", "<Shift><Control><Mod1><Mod2><Mod3><Mod4><Mod5>F10", "Geany");
241 gtk_settings_set_string_property(gtk_settings_get_default(),
242 "gtk-menu-bar-accel", gtk_menu_key_accel
, "Geany");
246 static void on_startup_complete(G_GNUC_UNUSED GObject
*dummy
)
248 GeanyDocument
*doc
= document_get_current();
251 vte_cwd((doc
->real_path
!= NULL
) ? doc
->real_path
: doc
->file_name
, FALSE
);
257 if (vte_info
.have_vte
== FALSE
)
258 { /* vte_info.have_vte can be false even if VTE is compiled in, think of command line option */
259 geany_debug("Disabling terminal support");
263 if (!EMPTY(vte_info
.lib_vte
))
265 module
= g_module_open(vte_info
.lib_vte
, G_MODULE_BIND_LAZY
);
267 #ifdef VTE_MODULE_PATH
270 module
= g_module_open(VTE_MODULE_PATH
, G_MODULE_BIND_LAZY
);
277 const gchar
*sonames
[] = {
278 #if GTK_CHECK_VERSION(3, 0, 0)
280 "libvte-2.91.0.dylib", "libvte-2.91.dylib",
281 "libvte2_90.9.dylib", "libvte2_90.dylib",
283 "libvte-2.91.so", "libvte-2.91.so.0",
284 "libvte2_90.so", "libvte2_90.so.9",
287 "libvte.9.dylib", "libvte.dylib",
289 "libvte.so", "libvte.so.9", "libvte.so.8", "libvte.so.4",
294 for (i
= 0; sonames
[i
] != NULL
&& module
== NULL
; i
++)
296 module
= g_module_open(sonames
[i
], G_MODULE_BIND_LAZY
);
302 vte_info
.have_vte
= FALSE
;
303 geany_debug("Could not load libvte.so, embedded terminal support disabled");
308 geany_debug("Loaded libvte from %s", g_module_name(module
));
309 vf
= g_new0(struct VteFunctions
, 1);
310 if (vte_register_symbols(module
))
311 vte_info
.have_vte
= TRUE
;
314 vte_info
.have_vte
= FALSE
;
316 /* FIXME: is closing the module safe? see vte_close() and test on FreeBSD */
317 /*g_module_close(module);*/
325 /* setup the F10 menu override (so it works before the widget is first realised). */
328 g_signal_connect(geany_object
, "geany-startup-complete", G_CALLBACK(on_startup_complete
), NULL
);
332 static void on_vte_realize(void)
334 /* the vte widget has to be realised before color changes take effect */
335 vte_apply_user_settings();
337 if (vf
->vte_terminal_im_append_menuitems
&& vc
->im_submenu
)
338 vf
->vte_terminal_im_append_menuitems(VTE_TERMINAL(vc
->vte
), GTK_MENU_SHELL(vc
->im_submenu
));
342 static gboolean
vte_start_idle(G_GNUC_UNUSED gpointer user_data
)
349 static void create_vte(void)
351 GtkWidget
*vte
, *scrollbar
, *hbox
;
353 vc
->vte
= vte
= vf
->vte_terminal_new();
354 scrollbar
= gtk_vscrollbar_new(vf
->vte_terminal_get_adjustment(VTE_TERMINAL(vte
)));
355 gtk_widget_set_can_focus(scrollbar
, FALSE
);
357 /* create menu now so copy/paste shortcuts work */
358 vc
->menu
= vte_create_popup_menu();
359 g_object_ref_sink(vc
->menu
);
361 hbox
= gtk_hbox_new(FALSE
, 0);
362 gtk_box_pack_start(GTK_BOX(hbox
), vte
, TRUE
, TRUE
, 0);
363 gtk_box_pack_start(GTK_BOX(hbox
), scrollbar
, FALSE
, FALSE
, 0);
365 /* set the default widget size first to prevent VTE expanding too much,
366 * sometimes causing the hscrollbar to be too big or out of view. */
367 gtk_widget_set_size_request(GTK_WIDGET(vte
), 10, 10);
368 vf
->vte_terminal_set_size(VTE_TERMINAL(vte
), 30, 1);
370 vf
->vte_terminal_set_mouse_autohide(VTE_TERMINAL(vte
), TRUE
);
371 if (vf
->vte_terminal_set_word_chars
)
372 vf
->vte_terminal_set_word_chars(VTE_TERMINAL(vte
), VTE_WORDCHARS
);
373 else if (vf
->vte_terminal_set_word_char_exceptions
)
374 vf
->vte_terminal_set_word_char_exceptions(VTE_TERMINAL(vte
), VTE_ADDITIONAL_WORDCHARS
);
376 gtk_drag_dest_set(vte
, GTK_DEST_DEFAULT_ALL
,
377 dnd_targets
, G_N_ELEMENTS(dnd_targets
), GDK_ACTION_COPY
);
379 g_signal_connect(vte
, "child-exited", G_CALLBACK(vte_start
), NULL
);
380 g_signal_connect(vte
, "button-press-event", G_CALLBACK(vte_button_pressed
), NULL
);
381 g_signal_connect(vte
, "event", G_CALLBACK(vte_keypress_cb
), NULL
);
382 g_signal_connect(vte
, "key-release-event", G_CALLBACK(vte_keyrelease_cb
), NULL
);
383 g_signal_connect(vte
, "commit", G_CALLBACK(vte_commit_cb
), NULL
);
384 g_signal_connect(vte
, "motion-notify-event", G_CALLBACK(on_motion_event
), NULL
);
385 g_signal_connect(vte
, "drag-data-received", G_CALLBACK(vte_drag_data_received
), NULL
);
387 /* start shell on idle otherwise the initial prompt can get corrupted */
388 g_idle_add(vte_start_idle
, NULL
);
390 gtk_widget_show_all(hbox
);
391 terminal_label
= gtk_label_new(_("Terminal"));
392 gtk_notebook_insert_page(GTK_NOTEBOOK(msgwindow
.notebook
), hbox
, terminal_label
, MSG_VTE
);
394 g_signal_connect_after(vte
, "realize", G_CALLBACK(on_vte_realize
), NULL
);
401 /* free the vte widget before unloading vte module
402 * this prevents a segfault on X close window if the message window is hidden */
403 gtk_widget_destroy(vc
->vte
);
404 gtk_widget_destroy(vc
->menu
);
405 g_object_unref(vc
->menu
);
408 g_free(vc
->send_cmd_prefix
);
410 g_free(gtk_menu_key_accel
);
411 /* Don't unload the module explicitly because it causes a segfault on FreeBSD. The segfault
412 * happens when the app really exits, not directly on g_module_close(). This still needs to
413 * be investigated. */
414 /*g_module_close(module); */
418 static gboolean
set_dirty_idle(gpointer user_data
)
420 gtk_widget_set_name(terminal_label
, "geany-terminal-dirty");
421 terminal_label_update_source
= 0;
426 static void set_clean(gboolean value
)
432 if (terminal_label_update_source
> 0)
434 g_source_remove(terminal_label_update_source
);
435 terminal_label_update_source
= 0;
438 gtk_widget_set_name(terminal_label
, NULL
);
440 terminal_label_update_source
= g_timeout_add(150, set_dirty_idle
, NULL
);
447 static gboolean
vte_keyrelease_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
449 if (ui_is_keyval_enter_or_return(event
->keyval
) ||
450 ((event
->keyval
== GDK_c
) && (event
->state
& GDK_CONTROL_MASK
)))
452 /* assume any text on the prompt has been executed when pressing Enter/Return */
459 static gboolean
vte_keypress_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
461 if (vc
->enable_bash_keys
)
462 return FALSE
; /* Ctrl-[CD] will be handled by the VTE itself */
464 if (event
->type
!= GDK_KEY_RELEASE
)
467 if ((event
->keyval
== GDK_c
||
468 event
->keyval
== GDK_d
||
469 event
->keyval
== GDK_C
||
470 event
->keyval
== GDK_D
) &&
471 event
->state
& GDK_CONTROL_MASK
&&
472 ! (event
->state
& GDK_SHIFT_MASK
) && ! (event
->state
& GDK_MOD1_MASK
))
481 static void vte_commit_cb(VteTerminal
*vte
, gchar
*arg1
, guint arg2
, gpointer user_data
)
487 static void vte_start(GtkWidget
*widget
)
489 /* split the shell command line, so arguments will work too */
490 gchar
**argv
= g_strsplit(vc
->shell
, " ", -1);
494 gchar
**env
= vte_get_child_environment();
496 if (vf
->vte_terminal_spawn_sync
)
498 if (! vf
->vte_terminal_spawn_sync(VTE_TERMINAL(widget
), VTE_PTY_DEFAULT
,
499 vte_info
.dir
, argv
, env
, 0, NULL
, NULL
,
507 pid
= vf
->vte_terminal_fork_command(VTE_TERMINAL(widget
), argv
[0], argv
, env
,
508 vte_info
.dir
, TRUE
, TRUE
, TRUE
);
514 pid
= 0; /* use 0 as invalid pid */
520 static void vte_restart(GtkWidget
*widget
)
522 vte_get_working_directory(); /* try to keep the working directory when restarting the VTE */
528 vf
->vte_terminal_reset(VTE_TERMINAL(widget
), TRUE
, TRUE
);
533 static gboolean
vte_button_pressed(GtkWidget
*widget
, GdkEventButton
*event
, gpointer user_data
)
535 if (event
->button
== 3)
537 gtk_widget_grab_focus(vc
->vte
);
538 gtk_menu_popup(GTK_MENU(vc
->menu
), NULL
, NULL
, NULL
, NULL
, event
->button
, event
->time
);
541 else if (event
->button
== 2)
543 gtk_widget_grab_focus(widget
);
549 static void vte_set_cursor_blink_mode(void)
551 if (vf
->vte_terminal_set_cursor_blink_mode
!= NULL
)
553 vf
->vte_terminal_set_cursor_blink_mode(VTE_TERMINAL(vc
->vte
),
554 (vc
->cursor_blinks
) ? VTE_CURSOR_BLINK_ON
: VTE_CURSOR_BLINK_OFF
);
557 vf
->vte_terminal_set_cursor_blinks(VTE_TERMINAL(vc
->vte
), vc
->cursor_blinks
);
561 #if GTK_CHECK_VERSION(3, 0, 0)
562 static gboolean
vte_is_2_91(void)
564 guint major
= vf
->vte_get_major_version
? vf
->vte_get_major_version() : 0;
565 guint minor
= vf
->vte_get_minor_version
? vf
->vte_get_minor_version() : 0;
567 /* 2.91 API started at 0.38 */
568 return ((major
> 0 || (major
== 0 && minor
>= 38)) ||
569 /* 0.38 doesn't have runtime version checks, so check a symbol that didn't exist before */
570 vf
->vte_terminal_spawn_sync
!= NULL
);
575 static gboolean
vte_register_symbols(GModule
*mod
)
577 #define BIND_SYMBOL_FULL(name, dest) \
578 g_module_symbol(mod, name, (void*)(dest))
579 #define BIND_SYMBOL(field) \
580 BIND_SYMBOL_FULL(#field, &vf->field)
581 #define BIND_REQUIRED_SYMBOL_FULL(name, dest) \
583 if (! BIND_SYMBOL_FULL(name, dest)) \
585 g_critical(_("invalid VTE library \"%s\": missing symbol \"%s\""), \
586 g_module_name(mod), name); \
590 #define BIND_REQUIRED_SYMBOL(field) \
591 BIND_REQUIRED_SYMBOL_FULL(#field, &vf->field)
592 #define BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(field) \
594 BIND_REQUIRED_SYMBOL_FULL(#field, &vf->field##_rgba); \
595 vf->field = wrap_##field; \
598 BIND_SYMBOL(vte_get_major_version
);
599 BIND_SYMBOL(vte_get_minor_version
);
600 BIND_REQUIRED_SYMBOL(vte_terminal_new
);
601 BIND_REQUIRED_SYMBOL(vte_terminal_set_size
);
602 if (! BIND_SYMBOL(vte_terminal_spawn_sync
))
603 /* vte_terminal_spawn_sync() is available only in 0.38 */
604 BIND_REQUIRED_SYMBOL(vte_terminal_fork_command
);
605 /* 0.38 removed vte_terminal_set_word_chars() */
606 BIND_SYMBOL(vte_terminal_set_word_chars
);
607 /* 0.40 introduced it under a different API */
608 BIND_SYMBOL(vte_terminal_set_word_char_exceptions
);
609 BIND_REQUIRED_SYMBOL(vte_terminal_set_mouse_autohide
);
610 BIND_REQUIRED_SYMBOL(vte_terminal_reset
);
611 BIND_REQUIRED_SYMBOL(vte_terminal_get_type
);
612 BIND_REQUIRED_SYMBOL(vte_terminal_set_scroll_on_output
);
613 BIND_REQUIRED_SYMBOL(vte_terminal_set_scroll_on_keystroke
);
614 BIND_REQUIRED_SYMBOL(vte_terminal_set_font
);
615 BIND_REQUIRED_SYMBOL(vte_terminal_set_scrollback_lines
);
616 BIND_REQUIRED_SYMBOL(vte_terminal_get_has_selection
);
617 BIND_REQUIRED_SYMBOL(vte_terminal_copy_clipboard
);
618 BIND_REQUIRED_SYMBOL(vte_terminal_paste_clipboard
);
619 #if GTK_CHECK_VERSION(3, 0, 0)
622 BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(vte_terminal_set_color_foreground
);
623 BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(vte_terminal_set_color_bold
);
624 BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(vte_terminal_set_color_background
);
629 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_foreground
);
630 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_bold
);
631 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_background
);
633 BIND_REQUIRED_SYMBOL(vte_terminal_feed_child
);
634 BIND_SYMBOL(vte_terminal_im_append_menuitems
);
635 if (! BIND_SYMBOL(vte_terminal_set_cursor_blink_mode
))
636 /* vte_terminal_set_cursor_blink_mode() is only available since 0.17.1, so if we don't find
637 * this symbol, we are probably on an older version and use the old API instead */
638 BIND_REQUIRED_SYMBOL(vte_terminal_set_cursor_blinks
);
639 BIND_REQUIRED_SYMBOL(vte_terminal_select_all
);
640 BIND_REQUIRED_SYMBOL(vte_terminal_set_audible_bell
);
641 if (! BIND_SYMBOL(vte_terminal_get_adjustment
))
642 /* vte_terminal_get_adjustment() is available since 0.9 and removed in 0.38 */
643 vf
->vte_terminal_get_adjustment
= default_vte_terminal_get_adjustment
;
645 #undef BIND_REQUIRED_SYMBOL_RGBA_WRAPPED
646 #undef BIND_REQUIRED_SYMBOL
647 #undef BIND_REQUIRED_SYMBOL_FULL
649 #undef BIND_SYMBOL_FULL
655 void vte_apply_user_settings(void)
657 PangoFontDescription
*font_desc
;
659 if (! ui_prefs
.msgwindow_visible
)
662 vf
->vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->vte
), vc
->scrollback_lines
);
663 vf
->vte_terminal_set_scroll_on_keystroke(VTE_TERMINAL(vc
->vte
), vc
->scroll_on_key
);
664 vf
->vte_terminal_set_scroll_on_output(VTE_TERMINAL(vc
->vte
), vc
->scroll_on_out
);
665 font_desc
= pango_font_description_from_string(vc
->font
);
666 vf
->vte_terminal_set_font(VTE_TERMINAL(vc
->vte
), font_desc
);
667 pango_font_description_free(font_desc
);
668 vf
->vte_terminal_set_color_foreground(VTE_TERMINAL(vc
->vte
), &vc
->colour_fore
);
669 vf
->vte_terminal_set_color_bold(VTE_TERMINAL(vc
->vte
), &vc
->colour_fore
);
670 vf
->vte_terminal_set_color_background(VTE_TERMINAL(vc
->vte
), &vc
->colour_back
);
671 vf
->vte_terminal_set_audible_bell(VTE_TERMINAL(vc
->vte
), prefs
.beep_on_errors
);
672 vte_set_cursor_blink_mode();
678 static void vte_popup_menu_clicked(GtkMenuItem
*menuitem
, gpointer user_data
)
680 switch (GPOINTER_TO_INT(user_data
))
684 if (vf
->vte_terminal_get_has_selection(VTE_TERMINAL(vc
->vte
)))
685 vf
->vte_terminal_copy_clipboard(VTE_TERMINAL(vc
->vte
));
690 vf
->vte_terminal_paste_clipboard(VTE_TERMINAL(vc
->vte
));
693 case POPUP_SELECTALL
:
698 case POPUP_CHANGEPATH
:
700 GeanyDocument
*doc
= document_get_current();
702 vte_cwd(doc
->file_name
, TRUE
);
705 case POPUP_RESTARTTERMINAL
:
707 vte_restart(vc
->vte
);
710 case POPUP_PREFERENCES
:
712 GtkWidget
*notebook
, *tab_page
;
716 notebook
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "notebook2");
717 tab_page
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "frame_term");
719 gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook
),
720 gtk_notebook_page_num(GTK_NOTEBOOK(notebook
), GTK_WIDGET(tab_page
)));
728 static GtkWidget
*vte_create_popup_menu(void)
730 GtkWidget
*menu
, *item
;
731 GtkAccelGroup
*accel_group
;
732 gboolean show_im_menu
= TRUE
;
734 menu
= gtk_menu_new();
736 accel_group
= gtk_accel_group_new();
737 gtk_window_add_accel_group(GTK_WINDOW(main_widgets
.window
), accel_group
);
739 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_COPY
, NULL
);
740 gtk_widget_add_accelerator(item
, "activate", accel_group
,
741 GDK_c
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, GTK_ACCEL_VISIBLE
);
742 gtk_widget_show(item
);
743 gtk_container_add(GTK_CONTAINER(menu
), item
);
744 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_COPY
));
746 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_PASTE
, NULL
);
747 gtk_widget_add_accelerator(item
, "activate", accel_group
,
748 GDK_v
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, GTK_ACCEL_VISIBLE
);
749 gtk_widget_show(item
);
750 gtk_container_add(GTK_CONTAINER(menu
), item
);
751 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_PASTE
));
753 item
= gtk_separator_menu_item_new();
754 gtk_widget_show(item
);
755 gtk_container_add(GTK_CONTAINER(menu
), item
);
757 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_SELECT_ALL
, NULL
);
758 gtk_widget_show(item
);
759 gtk_container_add(GTK_CONTAINER(menu
), item
);
760 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_SELECTALL
));
762 item
= gtk_separator_menu_item_new();
763 gtk_widget_show(item
);
764 gtk_container_add(GTK_CONTAINER(menu
), item
);
766 item
= gtk_image_menu_item_new_with_mnemonic(_("_Set Path From Document"));
767 gtk_widget_show(item
);
768 gtk_container_add(GTK_CONTAINER(menu
), item
);
769 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_CHANGEPATH
));
771 item
= gtk_image_menu_item_new_with_mnemonic(_("_Restart Terminal"));
772 gtk_widget_show(item
);
773 gtk_container_add(GTK_CONTAINER(menu
), item
);
774 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_RESTARTTERMINAL
));
776 item
= gtk_separator_menu_item_new();
777 gtk_widget_show(item
);
778 gtk_container_add(GTK_CONTAINER(menu
), item
);
780 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES
, NULL
);
781 gtk_widget_show(item
);
782 gtk_container_add(GTK_CONTAINER(menu
), item
);
783 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_PREFERENCES
));
785 msgwin_menu_add_common_items(GTK_MENU(menu
));
787 /* VTE 2.91 doesn't have IM context items, and GTK >= 3.10 doesn't show them anyway */
788 if (! vf
->vte_terminal_im_append_menuitems
|| gtk_check_version(3, 10, 0) == NULL
)
789 show_im_menu
= FALSE
;
790 else /* otherwise, query the setting */
791 g_object_get(gtk_settings_get_default(), "gtk-show-input-method-menu", &show_im_menu
, NULL
);
794 vc
->im_submenu
= NULL
;
797 item
= gtk_separator_menu_item_new();
798 gtk_widget_show(item
);
799 gtk_container_add(GTK_CONTAINER(menu
), item
);
801 /* the IM submenu should always be the last item to be consistent with other GTK popup menus */
802 vc
->im_submenu
= gtk_menu_new();
804 item
= gtk_image_menu_item_new_with_mnemonic(_("_Input Methods"));
805 gtk_widget_show(item
);
806 gtk_container_add(GTK_CONTAINER(menu
), item
);
808 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item
), vc
->im_submenu
);
809 /* submenu populated after vte realized */
816 /* If the command could be executed, TRUE is returned, FALSE otherwise (i.e. there was some text
818 gboolean
vte_send_cmd(const gchar
*cmd
)
822 vf
->vte_terminal_feed_child(VTE_TERMINAL(vc
->vte
), cmd
, strlen(cmd
));
823 set_clean(TRUE
); /* vte_terminal_feed_child() also marks the vte as not clean */
831 /* Taken from Terminal by os-cillation: terminal_screen_get_working_directory, thanks.
832 * Determines the working directory using various OS-specific mechanisms and stores the determined
833 * directory in vte_info.dir. Note: vte_info.dir contains the real path. */
834 const gchar
*vte_get_working_directory(void)
838 gchar buffer
[4096 + 1];
839 gchar
*file
= g_strdup_printf("/proc/%d/cwd", pid
);
840 gint length
= readlink(file
, buffer
, sizeof(buffer
));
842 if (length
> 0 && *buffer
== '/')
844 buffer
[length
] = '\0';
845 g_free(vte_info
.dir
);
846 vte_info
.dir
= g_strdup(buffer
);
848 else if (length
== 0)
850 gchar
*cwd
= g_get_current_dir();
854 if (chdir(file
) == 0)
856 g_free(vte_info
.dir
);
857 vte_info
.dir
= g_get_current_dir();
859 geany_debug("%s: %s", G_STRFUNC
, g_strerror(errno
));
871 /* Changes the current working directory of the VTE to the path of the given filename.
872 * filename is expected to be in UTF-8 encoding.
873 * filename can also be a path, then it is used directly.
874 * If force is set to TRUE, it will always change the cwd
876 void vte_cwd(const gchar
*filename
, gboolean force
)
878 if (vte_info
.have_vte
&& (vc
->follow_path
|| force
) &&
879 filename
!= NULL
&& g_path_is_absolute(filename
))
883 if (g_file_test(filename
, G_FILE_TEST_IS_DIR
))
884 path
= g_strdup(filename
);
886 path
= g_path_get_dirname(filename
);
888 vte_get_working_directory(); /* refresh vte_info.dir */
889 if (! utils_str_equal(path
, vte_info
.dir
))
891 /* use g_shell_quote to avoid problems with spaces, '!' or something else in path */
892 gchar
*quoted_path
= g_shell_quote(path
);
893 gchar
*cmd
= g_strconcat(vc
->send_cmd_prefix
, "cd ", quoted_path
, "\n", NULL
);
894 if (! vte_send_cmd(cmd
))
896 const gchar
*msg
= _("Directory not changed because the terminal may contain some input (press Ctrl+C or Enter to clear it).");
897 ui_set_statusbar(FALSE
, "%s", msg
);
898 geany_debug("%s", msg
);
908 static void vte_drag_data_received(GtkWidget
*widget
, GdkDragContext
*drag_context
,
909 gint x
, gint y
, GtkSelectionData
*data
, guint info
, guint ltime
)
911 if (info
== TARGET_TEXT_PLAIN
)
913 if (gtk_selection_data_get_format(data
) == 8 && gtk_selection_data_get_length(data
) > 0)
914 vf
->vte_terminal_feed_child(VTE_TERMINAL(widget
),
915 (const gchar
*) gtk_selection_data_get_data(data
),
916 gtk_selection_data_get_length(data
));
920 gchar
*text
= (gchar
*) gtk_selection_data_get_text(data
);
922 vf
->vte_terminal_feed_child(VTE_TERMINAL(widget
), text
, strlen(text
));
925 gtk_drag_finish(drag_context
, TRUE
, FALSE
, ltime
);
929 static void on_check_run_in_vte_toggled(GtkToggleButton
*togglebutton
, GtkWidget
*user_data
)
931 g_return_if_fail(GTK_IS_WIDGET(user_data
));
932 gtk_widget_set_sensitive(user_data
, gtk_toggle_button_get_active(togglebutton
));
936 static void on_term_font_set(GtkFontButton
*widget
, gpointer user_data
)
938 const gchar
*fontbtn
= gtk_font_button_get_font_name(widget
);
940 if (! utils_str_equal(fontbtn
, vc
->font
))
942 SETPTR(vc
->font
, g_strdup(gtk_font_button_get_font_name(widget
)));
943 vte_apply_user_settings();
948 static void on_term_fg_color_set(GtkColorButton
*widget
, gpointer user_data
)
950 gtk_color_button_get_color(widget
, &vc
->colour_fore
);
954 static void on_term_bg_color_set(GtkColorButton
*widget
, gpointer user_data
)
956 gtk_color_button_get_color(widget
, &vc
->colour_back
);
960 void vte_append_preferences_tab(void)
962 if (vte_info
.have_vte
)
964 GtkWidget
*frame_term
, *button_shell
, *entry_shell
;
965 GtkWidget
*check_run_in_vte
, *check_skip_script
;
966 GtkWidget
*font_button
, *fg_color_button
, *bg_color_button
;
968 button_shell
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "button_term_shell"));
969 entry_shell
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "entry_shell"));
970 ui_setup_open_button_callback(button_shell
, NULL
,
971 GTK_FILE_CHOOSER_ACTION_OPEN
, GTK_ENTRY(entry_shell
));
973 check_skip_script
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "check_skip_script"));
974 gtk_widget_set_sensitive(check_skip_script
, vc
->run_in_vte
);
976 check_run_in_vte
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "check_run_in_vte"));
977 g_signal_connect(G_OBJECT(check_run_in_vte
), "toggled",
978 G_CALLBACK(on_check_run_in_vte_toggled
), check_skip_script
);
980 font_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "font_term");
981 g_signal_connect(font_button
, "font-set", G_CALLBACK(on_term_font_set
), NULL
);
983 fg_color_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "color_fore");
984 g_signal_connect(fg_color_button
, "color-set", G_CALLBACK(on_term_fg_color_set
), NULL
);
986 bg_color_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "color_back");
987 g_signal_connect(bg_color_button
, "color-set", G_CALLBACK(on_term_bg_color_set
), NULL
);
989 frame_term
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "frame_term");
990 gtk_widget_show_all(frame_term
);
995 void vte_select_all(void)
997 if (vf
->vte_terminal_select_all
!= NULL
)
998 vf
->vte_terminal_select_all(VTE_TERMINAL(vc
->vte
));
1002 void vte_send_selection_to_vte(void)
1008 doc
= document_get_current();
1009 g_return_if_fail(doc
!= NULL
);
1011 if (sci_has_selection(doc
->editor
->sci
))
1013 text
= sci_get_selection_contents(doc
->editor
->sci
);
1016 { /* Get the current line */
1017 gint line_num
= sci_get_current_line(doc
->editor
->sci
);
1018 text
= sci_get_line(doc
->editor
->sci
, line_num
);
1023 if (vc
->send_selection_unsafe
)
1024 { /* Explicitly append a trailing newline character to get the command executed,
1025 this is disabled by default as it could cause all sorts of damage. */
1026 if (text
[len
-1] != '\n' && text
[len
-1] != '\r')
1028 SETPTR(text
, g_strconcat(text
, "\n", NULL
));
1033 { /* Make sure there is no newline character at the end to prevent unwanted execution */
1034 while (text
[len
-1] == '\n' || text
[len
-1] == '\r')
1041 vf
->vte_terminal_feed_child(VTE_TERMINAL(vc
->vte
), text
, len
);
1044 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_VTE
);
1045 gtk_widget_grab_focus(vc
->vte
);
1046 msgwin_show_hide(TRUE
);