2 * vte.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005 The Geany contributors
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 * Virtual Terminal Emulation setup and handling code, using the libvte plugin library.
33 #include "callbacks.h"
35 #include "geanyobject.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>
58 VteInfo vte_info
= { FALSE
, FALSE
, FALSE
, NULL
, NULL
};
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,./?%&#:_";
71 static const gchar VTE_ADDITIONAL_WORDCHARS
[] = "-,./?%&#:_";
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 /* we don't care for the other possible values */
97 /* Holds function pointers we need to access the VTE API. */
100 guint (*vte_get_major_version
) (void);
101 guint (*vte_get_minor_version
) (void);
102 GtkWidget
* (*vte_terminal_new
) (void);
103 pid_t (*vte_terminal_fork_command
) (VteTerminal
*terminal
, const char *command
, char **argv
,
104 char **envv
, const char *directory
, gboolean lastlog
,
105 gboolean utmp
, gboolean wtmp
);
106 gboolean (*vte_terminal_spawn_sync
) (VteTerminal
*terminal
, VtePtyFlags pty_flags
,
107 const char *working_directory
, char **argv
, char **envv
,
108 GSpawnFlags spawn_flags
, GSpawnChildSetupFunc child_setup
,
109 gpointer child_setup_data
, GPid
*child_pid
,
110 GCancellable
*cancellable
, GError
**error
);
111 void (*vte_terminal_set_size
) (VteTerminal
*terminal
, glong columns
, glong rows
);
112 void (*vte_terminal_set_word_chars
) (VteTerminal
*terminal
, const char *spec
);
113 void (*vte_terminal_set_word_char_exceptions
) (VteTerminal
*terminal
, const char *exceptions
);
114 void (*vte_terminal_set_mouse_autohide
) (VteTerminal
*terminal
, gboolean setting
);
115 void (*vte_terminal_reset
) (VteTerminal
*terminal
, gboolean full
, gboolean clear_history
);
116 GType (*vte_terminal_get_type
) (void);
117 void (*vte_terminal_set_scroll_on_output
) (VteTerminal
*terminal
, gboolean scroll
);
118 void (*vte_terminal_set_scroll_on_keystroke
) (VteTerminal
*terminal
, gboolean scroll
);
119 void (*vte_terminal_set_font
) (VteTerminal
*terminal
, const PangoFontDescription
*font_desc
);
120 void (*vte_terminal_set_scrollback_lines
) (VteTerminal
*terminal
, glong lines
);
121 gboolean (*vte_terminal_get_has_selection
) (VteTerminal
*terminal
);
122 void (*vte_terminal_copy_clipboard
) (VteTerminal
*terminal
);
123 void (*vte_terminal_paste_clipboard
) (VteTerminal
*terminal
);
124 void (*vte_terminal_set_color_foreground
) (VteTerminal
*terminal
, const GdkColor
*foreground
);
125 void (*vte_terminal_set_color_bold
) (VteTerminal
*terminal
, const GdkColor
*foreground
);
126 void (*vte_terminal_set_color_background
) (VteTerminal
*terminal
, const GdkColor
*background
);
127 void (*vte_terminal_feed_child
) (VteTerminal
*terminal
, const char *data
, glong length
);
128 void (*vte_terminal_im_append_menuitems
) (VteTerminal
*terminal
, GtkMenuShell
*menushell
);
129 void (*vte_terminal_set_cursor_blink_mode
) (VteTerminal
*terminal
,
130 VteTerminalCursorBlinkMode mode
);
131 void (*vte_terminal_set_cursor_blinks
) (VteTerminal
*terminal
, gboolean blink
);
132 void (*vte_terminal_select_all
) (VteTerminal
*terminal
);
133 void (*vte_terminal_set_audible_bell
) (VteTerminal
*terminal
, gboolean is_audible
);
134 GtkAdjustment
* (*vte_terminal_get_adjustment
) (VteTerminal
*terminal
);
135 #if GTK_CHECK_VERSION(3, 0, 0)
136 /* hack for the VTE 2.91 API using GdkRGBA: we wrap the API to keep using GdkColor on our side */
137 void (*vte_terminal_set_color_foreground_rgba
) (VteTerminal
*terminal
, const GdkRGBA
*foreground
);
138 void (*vte_terminal_set_color_bold_rgba
) (VteTerminal
*terminal
, const GdkRGBA
*foreground
);
139 void (*vte_terminal_set_color_background_rgba
) (VteTerminal
*terminal
, const GdkRGBA
*background
);
144 static void create_vte(void);
145 static void vte_start(GtkWidget
*widget
);
146 static void vte_restart(GtkWidget
*widget
);
147 static gboolean
vte_button_pressed(GtkWidget
*widget
, GdkEventButton
*event
, gpointer user_data
);
148 static gboolean
vte_keyrelease_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
);
149 static gboolean
vte_keypress_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
);
150 static gboolean
vte_register_symbols(GModule
*module
);
151 static void vte_popup_menu_clicked(GtkMenuItem
*menuitem
, gpointer user_data
);
152 static GtkWidget
*vte_create_popup_menu(void);
153 static void vte_commit_cb(VteTerminal
*vte
, gchar
*arg1
, guint arg2
, gpointer user_data
);
154 static void vte_drag_data_received(GtkWidget
*widget
, GdkDragContext
*drag_context
,
155 gint x
, gint y
, GtkSelectionData
*data
, guint info
, guint ltime
);
164 POPUP_RESTARTTERMINAL
,
166 TARGET_UTF8_STRING
= 0,
168 TARGET_COMPOUND_TEXT
,
173 static const GtkTargetEntry dnd_targets
[] =
175 { "UTF8_STRING", 0, TARGET_UTF8_STRING
},
176 { "TEXT", 0, TARGET_TEXT
},
177 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT
},
178 { "STRING", 0, TARGET_STRING
},
179 { "text/plain", 0, TARGET_TEXT_PLAIN
},
183 /* replacement for vte_terminal_get_adjustment() when it's not available */
184 static GtkAdjustment
*default_vte_terminal_get_adjustment(VteTerminal
*vte
)
186 #if GTK_CHECK_VERSION(3, 0, 0)
187 if (GTK_IS_SCROLLABLE(vte
))
188 return gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(vte
));
190 /* this is only valid in < 0.38, 0.38 broke ABI */
191 return vte
->adjustment
;
195 #if GTK_CHECK_VERSION(3, 0, 0)
196 /* Wrap VTE 2.91 API using GdkRGBA with GdkColor so we use a single API on our side */
198 static void rgba_from_color(GdkRGBA
*rgba
, const GdkColor
*color
)
200 rgba
->red
= color
->red
/ 65535.0;
201 rgba
->green
= color
->green
/ 65535.0;
202 rgba
->blue
= color
->blue
/ 65535.0;
206 # define WRAP_RGBA_SETTER(name) \
207 static void wrap_##name(VteTerminal *terminal, const GdkColor *color) \
210 rgba_from_color(&rgba, color); \
211 vf->name##_rgba(terminal, &rgba); \
214 WRAP_RGBA_SETTER(vte_terminal_set_color_background
)
215 WRAP_RGBA_SETTER(vte_terminal_set_color_bold
)
216 WRAP_RGBA_SETTER(vte_terminal_set_color_foreground
)
218 # undef WRAP_RGBA_SETTER
222 static gchar
**vte_get_child_environment(void)
224 const gchar
*exclude_vars
[] = {"COLUMNS", "LINES", "TERM", "TERM_PROGRAM", NULL
};
226 return utils_copy_environment(exclude_vars
, "TERM", "xterm", NULL
);
230 static void override_menu_key(void)
232 if (gtk_menu_key_accel
== NULL
) /* for restoring the default value */
233 g_object_get(G_OBJECT(gtk_settings_get_default()),
234 "gtk-menu-bar-accel", >k_menu_key_accel
, NULL
);
236 if (vc
->ignore_menu_bar_accel
)
237 gtk_settings_set_string_property(gtk_settings_get_default(),
238 "gtk-menu-bar-accel", "<Shift><Control><Mod1><Mod2><Mod3><Mod4><Mod5>F10", "Geany");
240 gtk_settings_set_string_property(gtk_settings_get_default(),
241 "gtk-menu-bar-accel", gtk_menu_key_accel
, "Geany");
245 static void on_startup_complete(G_GNUC_UNUSED GObject
*dummy
)
247 GeanyDocument
*doc
= document_get_current();
250 vte_cwd((doc
->real_path
!= NULL
) ? doc
->real_path
: doc
->file_name
, FALSE
);
256 if (vte_info
.have_vte
== FALSE
)
257 { /* vte_info.have_vte can be false even if VTE is compiled in, think of command line option */
258 geany_debug("Disabling terminal support");
262 if (!EMPTY(vte_info
.lib_vte
))
264 module
= g_module_open(vte_info
.lib_vte
, G_MODULE_BIND_LAZY
);
266 #ifdef VTE_MODULE_PATH
269 module
= g_module_open(VTE_MODULE_PATH
, G_MODULE_BIND_LAZY
);
276 const gchar
*sonames
[] = {
277 #if GTK_CHECK_VERSION(3, 0, 0)
279 "libvte-2.91.0.dylib", "libvte-2.91.dylib",
280 "libvte2_90.9.dylib", "libvte2_90.dylib",
282 "libvte-2.91.so", "libvte-2.91.so.0",
283 "libvte2_90.so", "libvte2_90.so.9",
286 "libvte.9.dylib", "libvte.dylib",
288 "libvte.so", "libvte.so.9", "libvte.so.8", "libvte.so.4",
293 for (i
= 0; sonames
[i
] != NULL
&& module
== NULL
; i
++)
295 module
= g_module_open(sonames
[i
], G_MODULE_BIND_LAZY
);
301 vte_info
.have_vte
= FALSE
;
302 geany_debug("Could not load libvte.so, embedded terminal support disabled");
307 geany_debug("Loaded libvte from %s", g_module_name(module
));
308 vf
= g_new0(struct VteFunctions
, 1);
309 if (vte_register_symbols(module
))
310 vte_info
.have_vte
= TRUE
;
313 vte_info
.have_vte
= FALSE
;
315 /* FIXME: is closing the module safe? see vte_close() and test on FreeBSD */
316 /*g_module_close(module);*/
324 /* setup the F10 menu override (so it works before the widget is first realised). */
327 g_signal_connect(geany_object
, "geany-startup-complete", G_CALLBACK(on_startup_complete
), NULL
);
331 static void on_vte_realize(void)
333 /* the vte widget has to be realised before color changes take effect */
334 vte_apply_user_settings();
336 if (vf
->vte_terminal_im_append_menuitems
&& vc
->im_submenu
)
337 vf
->vte_terminal_im_append_menuitems(VTE_TERMINAL(vc
->vte
), GTK_MENU_SHELL(vc
->im_submenu
));
341 static gboolean
vte_start_idle(G_GNUC_UNUSED gpointer user_data
)
348 static void create_vte(void)
350 GtkWidget
*vte
, *scrollbar
, *hbox
;
352 vc
->vte
= vte
= vf
->vte_terminal_new();
353 scrollbar
= gtk_vscrollbar_new(vf
->vte_terminal_get_adjustment(VTE_TERMINAL(vte
)));
354 gtk_widget_set_can_focus(scrollbar
, FALSE
);
356 /* create menu now so copy/paste shortcuts work */
357 vc
->menu
= vte_create_popup_menu();
358 g_object_ref_sink(vc
->menu
);
360 hbox
= gtk_hbox_new(FALSE
, 0);
361 gtk_box_pack_start(GTK_BOX(hbox
), vte
, TRUE
, TRUE
, 0);
362 gtk_box_pack_start(GTK_BOX(hbox
), scrollbar
, FALSE
, FALSE
, 0);
364 /* set the default widget size first to prevent VTE expanding too much,
365 * sometimes causing the hscrollbar to be too big or out of view. */
366 gtk_widget_set_size_request(GTK_WIDGET(vte
), 10, 10);
367 vf
->vte_terminal_set_size(VTE_TERMINAL(vte
), 30, 1);
369 vf
->vte_terminal_set_mouse_autohide(VTE_TERMINAL(vte
), TRUE
);
370 if (vf
->vte_terminal_set_word_chars
)
371 vf
->vte_terminal_set_word_chars(VTE_TERMINAL(vte
), VTE_WORDCHARS
);
372 else if (vf
->vte_terminal_set_word_char_exceptions
)
373 vf
->vte_terminal_set_word_char_exceptions(VTE_TERMINAL(vte
), VTE_ADDITIONAL_WORDCHARS
);
375 gtk_drag_dest_set(vte
, GTK_DEST_DEFAULT_ALL
,
376 dnd_targets
, G_N_ELEMENTS(dnd_targets
), GDK_ACTION_COPY
);
378 g_signal_connect(vte
, "child-exited", G_CALLBACK(vte_start
), NULL
);
379 g_signal_connect(vte
, "button-press-event", G_CALLBACK(vte_button_pressed
), NULL
);
380 g_signal_connect(vte
, "event", G_CALLBACK(vte_keypress_cb
), NULL
);
381 g_signal_connect(vte
, "key-release-event", G_CALLBACK(vte_keyrelease_cb
), NULL
);
382 g_signal_connect(vte
, "commit", G_CALLBACK(vte_commit_cb
), NULL
);
383 g_signal_connect(vte
, "motion-notify-event", G_CALLBACK(on_motion_event
), NULL
);
384 g_signal_connect(vte
, "drag-data-received", G_CALLBACK(vte_drag_data_received
), NULL
);
386 /* start shell on idle otherwise the initial prompt can get corrupted */
387 g_idle_add(vte_start_idle
, NULL
);
389 gtk_widget_show_all(hbox
);
390 terminal_label
= gtk_label_new(_("Terminal"));
391 gtk_notebook_insert_page(GTK_NOTEBOOK(msgwindow
.notebook
), hbox
, terminal_label
, MSG_VTE
);
393 g_signal_connect_after(vte
, "realize", G_CALLBACK(on_vte_realize
), NULL
);
399 /* free the vte widget before unloading vte module
400 * this prevents a segfault on X close window if the message window is hidden */
401 g_signal_handlers_disconnect_by_func(vc
->vte
, G_CALLBACK(vte_start
), NULL
);
402 gtk_widget_destroy(vc
->vte
);
403 gtk_widget_destroy(vc
->menu
);
404 g_object_unref(vc
->menu
);
407 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
);
534 static gboolean
vte_button_pressed(GtkWidget
*widget
, GdkEventButton
*event
, gpointer user_data
)
536 if (event
->button
== 3)
538 gtk_widget_grab_focus(vc
->vte
);
539 gtk_menu_popup(GTK_MENU(vc
->menu
), NULL
, NULL
, NULL
, NULL
, event
->button
, event
->time
);
542 else if (event
->button
== 2)
544 gtk_widget_grab_focus(widget
);
550 static void vte_set_cursor_blink_mode(void)
552 if (vf
->vte_terminal_set_cursor_blink_mode
!= NULL
)
554 vf
->vte_terminal_set_cursor_blink_mode(VTE_TERMINAL(vc
->vte
),
555 (vc
->cursor_blinks
) ? VTE_CURSOR_BLINK_ON
: VTE_CURSOR_BLINK_OFF
);
558 vf
->vte_terminal_set_cursor_blinks(VTE_TERMINAL(vc
->vte
), vc
->cursor_blinks
);
562 #if GTK_CHECK_VERSION(3, 0, 0)
563 static gboolean
vte_is_2_91(void)
565 guint major
= vf
->vte_get_major_version
? vf
->vte_get_major_version() : 0;
566 guint minor
= vf
->vte_get_minor_version
? vf
->vte_get_minor_version() : 0;
568 /* 2.91 API started at 0.38 */
569 return ((major
> 0 || (major
== 0 && minor
>= 38)) ||
570 /* 0.38 doesn't have runtime version checks, so check a symbol that didn't exist before */
571 vf
->vte_terminal_spawn_sync
!= NULL
);
576 static gboolean
vte_register_symbols(GModule
*mod
)
578 #define BIND_SYMBOL_FULL(name, dest) \
579 g_module_symbol(mod, name, (void*)(dest))
580 #define BIND_SYMBOL(field) \
581 BIND_SYMBOL_FULL(#field, &vf->field)
582 #define BIND_REQUIRED_SYMBOL_FULL(name, dest) \
584 if (! BIND_SYMBOL_FULL(name, dest)) \
586 g_critical(_("invalid VTE library \"%s\": missing symbol \"%s\""), \
587 g_module_name(mod), name); \
591 #define BIND_REQUIRED_SYMBOL(field) \
592 BIND_REQUIRED_SYMBOL_FULL(#field, &vf->field)
593 #define BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(field) \
595 BIND_REQUIRED_SYMBOL_FULL(#field, &vf->field##_rgba); \
596 vf->field = wrap_##field; \
599 BIND_SYMBOL(vte_get_major_version
);
600 BIND_SYMBOL(vte_get_minor_version
);
601 BIND_REQUIRED_SYMBOL(vte_terminal_new
);
602 BIND_REQUIRED_SYMBOL(vte_terminal_set_size
);
603 if (! BIND_SYMBOL(vte_terminal_spawn_sync
))
604 /* vte_terminal_spawn_sync() is available only in 0.38 */
605 BIND_REQUIRED_SYMBOL(vte_terminal_fork_command
);
606 /* 0.38 removed vte_terminal_set_word_chars() */
607 BIND_SYMBOL(vte_terminal_set_word_chars
);
608 /* 0.40 introduced it under a different API */
609 BIND_SYMBOL(vte_terminal_set_word_char_exceptions
);
610 BIND_REQUIRED_SYMBOL(vte_terminal_set_mouse_autohide
);
611 BIND_REQUIRED_SYMBOL(vte_terminal_reset
);
612 BIND_REQUIRED_SYMBOL(vte_terminal_get_type
);
613 BIND_REQUIRED_SYMBOL(vte_terminal_set_scroll_on_output
);
614 BIND_REQUIRED_SYMBOL(vte_terminal_set_scroll_on_keystroke
);
615 BIND_REQUIRED_SYMBOL(vte_terminal_set_font
);
616 BIND_REQUIRED_SYMBOL(vte_terminal_set_scrollback_lines
);
617 BIND_REQUIRED_SYMBOL(vte_terminal_get_has_selection
);
618 BIND_REQUIRED_SYMBOL(vte_terminal_copy_clipboard
);
619 BIND_REQUIRED_SYMBOL(vte_terminal_paste_clipboard
);
620 #if GTK_CHECK_VERSION(3, 0, 0)
623 BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(vte_terminal_set_color_foreground
);
624 BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(vte_terminal_set_color_bold
);
625 BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(vte_terminal_set_color_background
);
630 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_foreground
);
631 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_bold
);
632 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_background
);
634 BIND_REQUIRED_SYMBOL(vte_terminal_feed_child
);
635 BIND_SYMBOL(vte_terminal_im_append_menuitems
);
636 if (! BIND_SYMBOL(vte_terminal_set_cursor_blink_mode
))
637 /* vte_terminal_set_cursor_blink_mode() is only available since 0.17.1, so if we don't find
638 * this symbol, we are probably on an older version and use the old API instead */
639 BIND_REQUIRED_SYMBOL(vte_terminal_set_cursor_blinks
);
640 BIND_REQUIRED_SYMBOL(vte_terminal_select_all
);
641 BIND_REQUIRED_SYMBOL(vte_terminal_set_audible_bell
);
642 if (! BIND_SYMBOL(vte_terminal_get_adjustment
))
643 /* vte_terminal_get_adjustment() is available since 0.9 and removed in 0.38 */
644 vf
->vte_terminal_get_adjustment
= default_vte_terminal_get_adjustment
;
646 #undef BIND_REQUIRED_SYMBOL_RGBA_WRAPPED
647 #undef BIND_REQUIRED_SYMBOL
648 #undef BIND_REQUIRED_SYMBOL_FULL
650 #undef BIND_SYMBOL_FULL
656 void vte_apply_user_settings(void)
658 PangoFontDescription
*font_desc
;
660 if (! ui_prefs
.msgwindow_visible
)
663 vf
->vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->vte
), vc
->scrollback_lines
);
664 vf
->vte_terminal_set_scroll_on_keystroke(VTE_TERMINAL(vc
->vte
), vc
->scroll_on_key
);
665 vf
->vte_terminal_set_scroll_on_output(VTE_TERMINAL(vc
->vte
), vc
->scroll_on_out
);
666 font_desc
= pango_font_description_from_string(vc
->font
);
667 vf
->vte_terminal_set_font(VTE_TERMINAL(vc
->vte
), font_desc
);
668 pango_font_description_free(font_desc
);
669 vf
->vte_terminal_set_color_foreground(VTE_TERMINAL(vc
->vte
), &vc
->colour_fore
);
670 vf
->vte_terminal_set_color_bold(VTE_TERMINAL(vc
->vte
), &vc
->colour_fore
);
671 vf
->vte_terminal_set_color_background(VTE_TERMINAL(vc
->vte
), &vc
->colour_back
);
672 vf
->vte_terminal_set_audible_bell(VTE_TERMINAL(vc
->vte
), prefs
.beep_on_errors
);
673 vte_set_cursor_blink_mode();
679 static void vte_popup_menu_clicked(GtkMenuItem
*menuitem
, gpointer user_data
)
681 switch (GPOINTER_TO_INT(user_data
))
685 if (vf
->vte_terminal_get_has_selection(VTE_TERMINAL(vc
->vte
)))
686 vf
->vte_terminal_copy_clipboard(VTE_TERMINAL(vc
->vte
));
691 vf
->vte_terminal_paste_clipboard(VTE_TERMINAL(vc
->vte
));
694 case POPUP_SELECTALL
:
699 case POPUP_CHANGEPATH
:
701 GeanyDocument
*doc
= document_get_current();
703 vte_cwd(doc
->file_name
, TRUE
);
706 case POPUP_RESTARTTERMINAL
:
708 vte_restart(vc
->vte
);
711 case POPUP_PREFERENCES
:
713 GtkWidget
*notebook
, *tab_page
;
717 notebook
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "notebook2");
718 tab_page
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "frame_term");
720 gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook
),
721 gtk_notebook_page_num(GTK_NOTEBOOK(notebook
), GTK_WIDGET(tab_page
)));
729 static GtkWidget
*vte_create_popup_menu(void)
731 GtkWidget
*menu
, *item
;
732 GtkAccelGroup
*accel_group
;
733 gboolean show_im_menu
= TRUE
;
735 menu
= gtk_menu_new();
737 accel_group
= gtk_accel_group_new();
738 gtk_window_add_accel_group(GTK_WINDOW(main_widgets
.window
), accel_group
);
740 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_COPY
, NULL
);
741 gtk_widget_add_accelerator(item
, "activate", accel_group
,
742 GDK_c
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, GTK_ACCEL_VISIBLE
);
743 gtk_widget_show(item
);
744 gtk_container_add(GTK_CONTAINER(menu
), item
);
745 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_COPY
));
747 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_PASTE
, NULL
);
748 gtk_widget_add_accelerator(item
, "activate", accel_group
,
749 GDK_v
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, GTK_ACCEL_VISIBLE
);
750 gtk_widget_show(item
);
751 gtk_container_add(GTK_CONTAINER(menu
), item
);
752 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_PASTE
));
754 item
= gtk_separator_menu_item_new();
755 gtk_widget_show(item
);
756 gtk_container_add(GTK_CONTAINER(menu
), item
);
758 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_SELECT_ALL
, NULL
);
759 gtk_widget_show(item
);
760 gtk_container_add(GTK_CONTAINER(menu
), item
);
761 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_SELECTALL
));
763 item
= gtk_separator_menu_item_new();
764 gtk_widget_show(item
);
765 gtk_container_add(GTK_CONTAINER(menu
), item
);
767 item
= gtk_image_menu_item_new_with_mnemonic(_("_Set Path From Document"));
768 gtk_widget_show(item
);
769 gtk_container_add(GTK_CONTAINER(menu
), item
);
770 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_CHANGEPATH
));
772 item
= gtk_image_menu_item_new_with_mnemonic(_("_Restart Terminal"));
773 gtk_widget_show(item
);
774 gtk_container_add(GTK_CONTAINER(menu
), item
);
775 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_RESTARTTERMINAL
));
777 item
= gtk_separator_menu_item_new();
778 gtk_widget_show(item
);
779 gtk_container_add(GTK_CONTAINER(menu
), item
);
781 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES
, NULL
);
782 gtk_widget_show(item
);
783 gtk_container_add(GTK_CONTAINER(menu
), item
);
784 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_PREFERENCES
));
786 msgwin_menu_add_common_items(GTK_MENU(menu
));
788 /* VTE 2.91 doesn't have IM context items, and GTK >= 3.10 doesn't show them anyway */
789 if (! vf
->vte_terminal_im_append_menuitems
|| gtk_check_version(3, 10, 0) == NULL
)
790 show_im_menu
= FALSE
;
791 else /* otherwise, query the setting */
792 g_object_get(gtk_settings_get_default(), "gtk-show-input-method-menu", &show_im_menu
, NULL
);
795 vc
->im_submenu
= NULL
;
798 item
= gtk_separator_menu_item_new();
799 gtk_widget_show(item
);
800 gtk_container_add(GTK_CONTAINER(menu
), item
);
802 /* the IM submenu should always be the last item to be consistent with other GTK popup menus */
803 vc
->im_submenu
= gtk_menu_new();
805 item
= gtk_image_menu_item_new_with_mnemonic(_("_Input Methods"));
806 gtk_widget_show(item
);
807 gtk_container_add(GTK_CONTAINER(menu
), item
);
809 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item
), vc
->im_submenu
);
810 /* submenu populated after vte realized */
817 /* If the command could be executed, TRUE is returned, FALSE otherwise (i.e. there was some text
819 gboolean
vte_send_cmd(const gchar
*cmd
)
823 vf
->vte_terminal_feed_child(VTE_TERMINAL(vc
->vte
), cmd
, strlen(cmd
));
824 set_clean(TRUE
); /* vte_terminal_feed_child() also marks the vte as not clean */
832 /* Taken from Terminal by os-cillation: terminal_screen_get_working_directory, thanks.
833 * Determines the working directory using various OS-specific mechanisms and stores the determined
834 * directory in vte_info.dir. Note: vte_info.dir contains the real path. */
835 const gchar
*vte_get_working_directory(void)
839 gchar buffer
[4096 + 1];
840 gchar
*file
= g_strdup_printf("/proc/%d/cwd", pid
);
841 gint length
= readlink(file
, buffer
, sizeof(buffer
));
843 if (length
> 0 && *buffer
== '/')
845 buffer
[length
] = '\0';
846 g_free(vte_info
.dir
);
847 vte_info
.dir
= g_strdup(buffer
);
849 else if (length
== 0)
851 gchar
*cwd
= g_get_current_dir();
855 if (chdir(file
) == 0)
857 g_free(vte_info
.dir
);
858 vte_info
.dir
= g_get_current_dir();
860 geany_debug("%s: %s", G_STRFUNC
, g_strerror(errno
));
872 /* Changes the current working directory of the VTE to the path of the given filename.
873 * filename is expected to be in UTF-8 encoding.
874 * filename can also be a path, then it is used directly.
875 * If force is set to TRUE, it will always change the cwd
877 void vte_cwd(const gchar
*filename
, gboolean force
)
879 if (vte_info
.have_vte
&& (vc
->follow_path
|| force
) &&
880 filename
!= NULL
&& g_path_is_absolute(filename
))
884 if (g_file_test(filename
, G_FILE_TEST_IS_DIR
))
885 path
= g_strdup(filename
);
887 path
= g_path_get_dirname(filename
);
889 vte_get_working_directory(); /* refresh vte_info.dir */
890 if (! utils_str_equal(path
, vte_info
.dir
))
892 /* use g_shell_quote to avoid problems with spaces, '!' or something else in path */
893 gchar
*quoted_path
= g_shell_quote(path
);
894 gchar
*cmd
= g_strconcat(vc
->send_cmd_prefix
, "cd ", quoted_path
, "\n", NULL
);
895 if (! vte_send_cmd(cmd
))
897 const gchar
*msg
= _("Directory not changed because the terminal may contain some input (press Ctrl+C or Enter to clear it).");
898 ui_set_statusbar(FALSE
, "%s", msg
);
899 geany_debug("%s", msg
);
909 static void vte_drag_data_received(GtkWidget
*widget
, GdkDragContext
*drag_context
,
910 gint x
, gint y
, GtkSelectionData
*data
, guint info
, guint ltime
)
912 if (info
== TARGET_TEXT_PLAIN
)
914 if (gtk_selection_data_get_format(data
) == 8 && gtk_selection_data_get_length(data
) > 0)
915 vf
->vte_terminal_feed_child(VTE_TERMINAL(widget
),
916 (const gchar
*) gtk_selection_data_get_data(data
),
917 gtk_selection_data_get_length(data
));
921 gchar
*text
= (gchar
*) gtk_selection_data_get_text(data
);
923 vf
->vte_terminal_feed_child(VTE_TERMINAL(widget
), text
, strlen(text
));
926 gtk_drag_finish(drag_context
, TRUE
, FALSE
, ltime
);
930 static void on_check_run_in_vte_toggled(GtkToggleButton
*togglebutton
, GtkWidget
*user_data
)
932 g_return_if_fail(GTK_IS_WIDGET(user_data
));
933 gtk_widget_set_sensitive(user_data
, gtk_toggle_button_get_active(togglebutton
));
937 static void on_term_font_set(GtkFontButton
*widget
, gpointer user_data
)
939 const gchar
*fontbtn
= gtk_font_button_get_font_name(widget
);
941 if (! utils_str_equal(fontbtn
, vc
->font
))
943 SETPTR(vc
->font
, g_strdup(gtk_font_button_get_font_name(widget
)));
944 vte_apply_user_settings();
949 static void on_term_fg_color_set(GtkColorButton
*widget
, gpointer user_data
)
951 gtk_color_button_get_color(widget
, &vc
->colour_fore
);
955 static void on_term_bg_color_set(GtkColorButton
*widget
, gpointer user_data
)
957 gtk_color_button_get_color(widget
, &vc
->colour_back
);
961 void vte_append_preferences_tab(void)
963 if (vte_info
.have_vte
)
965 GtkWidget
*frame_term
, *button_shell
, *entry_shell
;
966 GtkWidget
*check_run_in_vte
, *check_skip_script
;
967 GtkWidget
*font_button
, *fg_color_button
, *bg_color_button
;
969 button_shell
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "button_term_shell"));
970 entry_shell
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "entry_shell"));
971 ui_setup_open_button_callback(button_shell
, NULL
,
972 GTK_FILE_CHOOSER_ACTION_OPEN
, GTK_ENTRY(entry_shell
));
974 check_skip_script
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "check_skip_script"));
975 gtk_widget_set_sensitive(check_skip_script
, vc
->run_in_vte
);
977 check_run_in_vte
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "check_run_in_vte"));
978 g_signal_connect(G_OBJECT(check_run_in_vte
), "toggled",
979 G_CALLBACK(on_check_run_in_vte_toggled
), check_skip_script
);
981 font_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "font_term");
982 g_signal_connect(font_button
, "font-set", G_CALLBACK(on_term_font_set
), NULL
);
984 fg_color_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "color_fore");
985 g_signal_connect(fg_color_button
, "color-set", G_CALLBACK(on_term_fg_color_set
), NULL
);
987 bg_color_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "color_back");
988 g_signal_connect(bg_color_button
, "color-set", G_CALLBACK(on_term_bg_color_set
), NULL
);
990 frame_term
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "frame_term");
991 gtk_widget_show_all(frame_term
);
996 void vte_select_all(void)
998 if (vf
->vte_terminal_select_all
!= NULL
)
999 vf
->vte_terminal_select_all(VTE_TERMINAL(vc
->vte
));
1003 void vte_send_selection_to_vte(void)
1009 doc
= document_get_current();
1010 g_return_if_fail(doc
!= NULL
);
1012 if (sci_has_selection(doc
->editor
->sci
))
1014 text
= sci_get_selection_contents(doc
->editor
->sci
);
1017 { /* Get the current line */
1018 gint line_num
= sci_get_current_line(doc
->editor
->sci
);
1019 text
= sci_get_line(doc
->editor
->sci
, line_num
);
1024 if (vc
->send_selection_unsafe
)
1025 { /* Explicitly append a trailing newline character to get the command executed,
1026 this is disabled by default as it could cause all sorts of damage. */
1027 if (text
[len
-1] != '\n' && text
[len
-1] != '\r')
1029 SETPTR(text
, g_strconcat(text
, "\n", NULL
));
1034 { /* Make sure there is no newline character at the end to prevent unwanted execution */
1035 while (text
[len
-1] == '\n' || text
[len
-1] == '\r')
1042 vf
->vte_terminal_feed_child(VTE_TERMINAL(vc
->vte
), text
, len
);
1045 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_VTE
);
1046 gtk_widget_grab_focus(vc
->vte
);
1047 msgwin_show_hide(TRUE
);