2 * vte.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2011 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2011 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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * Virtual Terminal Emulation setup and handling code, using the libvte plugin library.
32 /* include stdlib.h AND unistd.h, because on GNU/Linux pid_t seems to be
33 * in stdlib.h, on FreeBSD in unistd.h, sys/types.h is needed for C89 */
35 #include <sys/types.h>
38 #include <gdk/gdkkeysyms.h>
49 #include "msgwindow.h"
50 #include "callbacks.h"
51 #include "geanywraplabel.h"
53 #include "sciwrappers.h"
60 static gboolean clean
= TRUE
;
61 static GModule
*module
= NULL
;
62 static struct VteFunctions
*vf
;
63 static gchar
*gtk_menu_key_accel
= NULL
;
64 static gint vte_prefs_tab_num
= -1;
66 /* use vte wordchars to select paths */
67 static const gchar VTE_WORDCHARS
[] = "-A-Za-z0-9,./?%&#:_";
70 /* Incomplete VteTerminal struct from vte/vte.h. */
71 typedef struct _VteTerminal VteTerminal
;
75 GtkAdjustment
*adjustment
;
78 #define VTE_TERMINAL(obj) (GTK_CHECK_CAST((obj), VTE_TYPE_TERMINAL, VteTerminal))
79 #define VTE_TYPE_TERMINAL (vf->vte_terminal_get_type())
82 VTE_CURSOR_BLINK_SYSTEM
,
85 } VteTerminalCursorBlinkMode
;
88 /* Holds function pointers we need to access the VTE API. */
91 GtkWidget
* (*vte_terminal_new
) (void);
92 pid_t (*vte_terminal_fork_command
) (VteTerminal
*terminal
, const char *command
, char **argv
,
93 char **envv
, const char *directory
, gboolean lastlog
,
94 gboolean utmp
, gboolean wtmp
);
95 void (*vte_terminal_set_size
) (VteTerminal
*terminal
, glong columns
, glong rows
);
96 void (*vte_terminal_set_word_chars
) (VteTerminal
*terminal
, const char *spec
);
97 void (*vte_terminal_set_mouse_autohide
) (VteTerminal
*terminal
, gboolean setting
);
98 void (*vte_terminal_reset
) (VteTerminal
*terminal
, gboolean full
, gboolean clear_history
);
99 GtkType (*vte_terminal_get_type
) (void);
100 void (*vte_terminal_set_scroll_on_output
) (VteTerminal
*terminal
, gboolean scroll
);
101 void (*vte_terminal_set_scroll_on_keystroke
) (VteTerminal
*terminal
, gboolean scroll
);
102 void (*vte_terminal_set_font_from_string
) (VteTerminal
*terminal
, const char *name
);
103 void (*vte_terminal_set_scrollback_lines
) (VteTerminal
*terminal
, glong lines
);
104 gboolean (*vte_terminal_get_has_selection
) (VteTerminal
*terminal
);
105 void (*vte_terminal_copy_clipboard
) (VteTerminal
*terminal
);
106 void (*vte_terminal_paste_clipboard
) (VteTerminal
*terminal
);
107 void (*vte_terminal_set_emulation
) (VteTerminal
*terminal
, const gchar
*emulation
);
108 void (*vte_terminal_set_color_foreground
) (VteTerminal
*terminal
, const GdkColor
*foreground
);
109 void (*vte_terminal_set_color_bold
) (VteTerminal
*terminal
, const GdkColor
*foreground
);
110 void (*vte_terminal_set_color_background
) (VteTerminal
*terminal
, const GdkColor
*background
);
111 void (*vte_terminal_feed_child
) (VteTerminal
*terminal
, const char *data
, glong length
);
112 void (*vte_terminal_im_append_menuitems
) (VteTerminal
*terminal
, GtkMenuShell
*menushell
);
113 void (*vte_terminal_set_cursor_blink_mode
) (VteTerminal
*terminal
,
114 VteTerminalCursorBlinkMode mode
);
115 void (*vte_terminal_set_cursor_blinks
) (VteTerminal
*terminal
, gboolean blink
);
116 void (*vte_terminal_select_all
) (VteTerminal
*terminal
);
117 void (*vte_terminal_set_audible_bell
) (VteTerminal
*terminal
, gboolean is_audible
);
121 static void create_vte(void);
122 static void vte_start(GtkWidget
*widget
);
123 static void vte_restart(GtkWidget
*widget
);
124 static gboolean
vte_button_pressed(GtkWidget
*widget
, GdkEventButton
*event
, gpointer user_data
);
125 static gboolean
vte_keyrelease_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
);
126 static gboolean
vte_keypress_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
);
127 static void vte_register_symbols(GModule
*module
);
128 static void vte_popup_menu_clicked(GtkMenuItem
*menuitem
, gpointer user_data
);
129 static GtkWidget
*vte_create_popup_menu(void);
130 static void vte_commit_cb(VteTerminal
*vte
, gchar
*arg1
, guint arg2
, gpointer user_data
);
131 static void vte_drag_data_received(GtkWidget
*widget
, GdkDragContext
*drag_context
,
132 gint x
, gint y
, GtkSelectionData
*data
, guint info
, guint ltime
);
141 POPUP_RESTARTTERMINAL
,
143 TARGET_UTF8_STRING
= 0,
145 TARGET_COMPOUND_TEXT
,
150 static const GtkTargetEntry dnd_targets
[] =
152 { "UTF8_STRING", 0, TARGET_UTF8_STRING
},
153 { "TEXT", 0, TARGET_TEXT
},
154 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT
},
155 { "STRING", 0, TARGET_STRING
},
156 { "text/plain", 0, TARGET_TEXT_PLAIN
},
160 static gchar
**vte_get_child_environment(void)
162 const gchar
*exclude_vars
[] = {"COLUMNS", "LINES", "TERM", NULL
};
164 return utils_copy_environment(exclude_vars
, "TERM", "xterm", NULL
);
168 static void override_menu_key(void)
170 if (gtk_menu_key_accel
== NULL
) /* for restoring the default value */
171 g_object_get(G_OBJECT(gtk_settings_get_default()),
172 "gtk-menu-bar-accel", >k_menu_key_accel
, NULL
);
174 if (vc
->ignore_menu_bar_accel
)
175 gtk_settings_set_string_property(gtk_settings_get_default(),
176 "gtk-menu-bar-accel", "<Shift><Control><Mod1><Mod2><Mod3><Mod4><Mod5>F10", "Geany");
178 gtk_settings_set_string_property(gtk_settings_get_default(),
179 "gtk-menu-bar-accel", gtk_menu_key_accel
, "Geany");
185 if (vte_info
.have_vte
== FALSE
)
186 { /* vte_info.have_vte can be false even if VTE is compiled in, think of command line option */
187 geany_debug("Disabling terminal support");
191 if (NZV(vte_info
.lib_vte
))
193 module
= g_module_open(vte_info
.lib_vte
, G_MODULE_BIND_LAZY
);
195 #ifdef VTE_MODULE_PATH
198 module
= g_module_open(VTE_MODULE_PATH
, G_MODULE_BIND_LAZY
);
205 const gchar
*sonames
[] = { "libvte.so", "libvte.so.4",
206 "libvte.so.8", "libvte.so.9", NULL
};
208 for (i
= 0; sonames
[i
] != NULL
&& module
== NULL
; i
++)
210 module
= g_module_open(sonames
[i
], G_MODULE_BIND_LAZY
);
216 vte_info
.have_vte
= FALSE
;
217 geany_debug("Could not load libvte.so, embedded terminal support disabled");
222 vte_info
.have_vte
= TRUE
;
223 vf
= g_new0(struct VteFunctions
, 1);
224 vte_register_symbols(module
);
229 /* setup the F10 menu override (so it works before the widget is first realised). */
234 static void on_vte_realize(void)
236 /* the vte widget has to be realised before color changes take effect */
237 vte_apply_user_settings();
239 vf
->vte_terminal_im_append_menuitems(VTE_TERMINAL(vc
->vte
), GTK_MENU_SHELL(vc
->im_submenu
));
243 static void create_vte(void)
245 GtkWidget
*vte
, *scrollbar
, *hbox
, *frame
;
247 vc
->vte
= vte
= vf
->vte_terminal_new();
248 scrollbar
= gtk_vscrollbar_new(GTK_ADJUSTMENT(VTE_TERMINAL(vte
)->adjustment
));
249 GTK_WIDGET_UNSET_FLAGS(scrollbar
, GTK_CAN_FOCUS
);
251 /* create menu now so copy/paste shortcuts work */
252 vc
->menu
= vte_create_popup_menu();
254 frame
= gtk_frame_new(NULL
);
256 hbox
= gtk_hbox_new(FALSE
, 0);
257 gtk_container_add(GTK_CONTAINER(frame
), hbox
);
258 gtk_box_pack_start(GTK_BOX(hbox
), vte
, TRUE
, TRUE
, 0);
259 gtk_box_pack_start(GTK_BOX(hbox
), scrollbar
, FALSE
, FALSE
, 0);
261 /* set the default widget size first to prevent VTE expanding too much,
262 * sometimes causing the hscrollbar to be too big or out of view. */
263 gtk_widget_set_size_request(GTK_WIDGET(vte
), 10, 10);
264 vf
->vte_terminal_set_size(VTE_TERMINAL(vte
), 30, 1);
266 vf
->vte_terminal_set_mouse_autohide(VTE_TERMINAL(vte
), TRUE
);
267 vf
->vte_terminal_set_word_chars(VTE_TERMINAL(vte
), VTE_WORDCHARS
);
269 gtk_drag_dest_set(vte
, GTK_DEST_DEFAULT_ALL
,
270 dnd_targets
, G_N_ELEMENTS(dnd_targets
), GDK_ACTION_COPY
);
272 g_signal_connect(vte
, "child-exited", G_CALLBACK(vte_start
), NULL
);
273 g_signal_connect(vte
, "button-press-event", G_CALLBACK(vte_button_pressed
), NULL
);
274 g_signal_connect(vte
, "event", G_CALLBACK(vte_keypress_cb
), NULL
);
275 g_signal_connect(vte
, "key-release-event", G_CALLBACK(vte_keyrelease_cb
), NULL
);
276 g_signal_connect(vte
, "commit", G_CALLBACK(vte_commit_cb
), NULL
);
277 g_signal_connect(vte
, "motion-notify-event", G_CALLBACK(on_motion_event
), NULL
);
278 g_signal_connect(vte
, "drag-data-received", G_CALLBACK(vte_drag_data_received
), NULL
);
282 gtk_widget_show_all(frame
);
283 gtk_notebook_insert_page(GTK_NOTEBOOK(msgwindow
.notebook
), frame
, gtk_label_new(_("Terminal")), MSG_VTE
);
285 g_signal_connect_after(vte
, "realize", G_CALLBACK(on_vte_realize
), NULL
);
292 /* free the vte widget before unloading vte module
293 * this prevents a segfault on X close window if the message window is hidden */
294 gtk_widget_destroy(vc
->vte
);
295 gtk_widget_destroy(vc
->menu
);
296 g_free(vc
->emulation
);
299 g_free(vc
->colour_back
);
300 g_free(vc
->colour_fore
);
302 g_free(gtk_menu_key_accel
);
303 /* Don't unload the module explicitly because it causes a segfault on FreeBSD. The segfault
304 * happens when the app really exits, not directly on g_module_close(). This still needs to
305 * be investigated. */
306 /*g_module_close(module); */
310 static gboolean
vte_keyrelease_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
312 if (ui_is_keyval_enter_or_return(event
->keyval
) ||
313 ((event
->keyval
== GDK_c
) && (event
->state
& GDK_CONTROL_MASK
)))
315 /* assume any text on the prompt has been executed when pressing Enter/Return */
322 static gboolean
vte_keypress_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
324 if (vc
->enable_bash_keys
)
325 return FALSE
; /* Ctrl-[CD] will be handled by the VTE itself */
327 if (event
->type
!= GDK_KEY_RELEASE
)
330 if ((event
->keyval
== GDK_c
||
331 event
->keyval
== GDK_d
||
332 event
->keyval
== GDK_C
||
333 event
->keyval
== GDK_D
) &&
334 event
->state
& GDK_CONTROL_MASK
&&
335 ! (event
->state
& GDK_SHIFT_MASK
) && ! (event
->state
& GDK_MOD1_MASK
))
344 static void vte_commit_cb(VteTerminal
*vte
, gchar
*arg1
, guint arg2
, gpointer user_data
)
350 static void vte_start(GtkWidget
*widget
)
355 /* split the shell command line, so arguments will work too */
356 argv
= g_strsplit(vc
->shell
, " ", -1);
360 env
= vte_get_child_environment();
361 pid
= vf
->vte_terminal_fork_command(VTE_TERMINAL(widget
), argv
[0], argv
, env
,
362 vte_info
.dir
, TRUE
, TRUE
, TRUE
);
367 pid
= 0; /* use 0 as invalid pid */
373 static void vte_restart(GtkWidget
*widget
)
375 vte_get_working_directory(); /* try to keep the working directory when restarting the VTE */
381 vf
->vte_terminal_reset(VTE_TERMINAL(widget
), TRUE
, TRUE
);
386 static gboolean
vte_button_pressed(GtkWidget
*widget
, GdkEventButton
*event
, gpointer user_data
)
388 if (event
->button
== 3)
390 gtk_widget_grab_focus(vc
->vte
);
391 gtk_menu_popup(GTK_MENU(vc
->menu
), NULL
, NULL
, NULL
, NULL
, event
->button
, event
->time
);
397 static void vte_set_cursor_blink_mode(void)
399 if (vf
->vte_terminal_set_cursor_blink_mode
!= NULL
)
401 vf
->vte_terminal_set_cursor_blink_mode(VTE_TERMINAL(vc
->vte
),
402 (vc
->cursor_blinks
) ? VTE_CURSOR_BLINK_ON
: VTE_CURSOR_BLINK_OFF
);
405 vf
->vte_terminal_set_cursor_blinks(VTE_TERMINAL(vc
->vte
), vc
->cursor_blinks
);
409 static void vte_register_symbols(GModule
*mod
)
411 g_module_symbol(mod
, "vte_terminal_new", (void*)&vf
->vte_terminal_new
);
412 g_module_symbol(mod
, "vte_terminal_set_size", (void*)&vf
->vte_terminal_set_size
);
413 g_module_symbol(mod
, "vte_terminal_fork_command", (void*)&vf
->vte_terminal_fork_command
);
414 g_module_symbol(mod
, "vte_terminal_set_word_chars", (void*)&vf
->vte_terminal_set_word_chars
);
415 g_module_symbol(mod
, "vte_terminal_set_mouse_autohide", (void*)&vf
->vte_terminal_set_mouse_autohide
);
416 g_module_symbol(mod
, "vte_terminal_reset", (void*)&vf
->vte_terminal_reset
);
417 g_module_symbol(mod
, "vte_terminal_get_type", (void*)&vf
->vte_terminal_get_type
);
418 g_module_symbol(mod
, "vte_terminal_set_scroll_on_output", (void*)&vf
->vte_terminal_set_scroll_on_output
);
419 g_module_symbol(mod
, "vte_terminal_set_scroll_on_keystroke", (void*)&vf
->vte_terminal_set_scroll_on_keystroke
);
420 g_module_symbol(mod
, "vte_terminal_set_font_from_string", (void*)&vf
->vte_terminal_set_font_from_string
);
421 g_module_symbol(mod
, "vte_terminal_set_scrollback_lines", (void*)&vf
->vte_terminal_set_scrollback_lines
);
422 g_module_symbol(mod
, "vte_terminal_get_has_selection", (void*)&vf
->vte_terminal_get_has_selection
);
423 g_module_symbol(mod
, "vte_terminal_copy_clipboard", (void*)&vf
->vte_terminal_copy_clipboard
);
424 g_module_symbol(mod
, "vte_terminal_paste_clipboard", (void*)&vf
->vte_terminal_paste_clipboard
);
425 g_module_symbol(mod
, "vte_terminal_set_emulation", (void*)&vf
->vte_terminal_set_emulation
);
426 g_module_symbol(mod
, "vte_terminal_set_color_foreground", (void*)&vf
->vte_terminal_set_color_foreground
);
427 g_module_symbol(mod
, "vte_terminal_set_color_bold", (void*)&vf
->vte_terminal_set_color_bold
);
428 g_module_symbol(mod
, "vte_terminal_set_color_background", (void*)&vf
->vte_terminal_set_color_background
);
429 g_module_symbol(mod
, "vte_terminal_feed_child", (void*)&vf
->vte_terminal_feed_child
);
430 g_module_symbol(mod
, "vte_terminal_im_append_menuitems", (void*)&vf
->vte_terminal_im_append_menuitems
);
431 g_module_symbol(mod
, "vte_terminal_set_cursor_blink_mode", (void*)&vf
->vte_terminal_set_cursor_blink_mode
);
432 if (vf
->vte_terminal_set_cursor_blink_mode
== NULL
)
433 /* vte_terminal_set_cursor_blink_mode() is only available since 0.17.1, so if we don't find
434 * this symbol, we are probably on an older version and use the old API instead */
435 g_module_symbol(mod
, "vte_terminal_set_cursor_blinks", (void*)&vf
->vte_terminal_set_cursor_blinks
);
436 g_module_symbol(mod
, "vte_terminal_select_all", (void*)&vf
->vte_terminal_select_all
);
437 g_module_symbol(mod
, "vte_terminal_set_audible_bell", (void*)&vf
->vte_terminal_set_audible_bell
);
441 void vte_apply_user_settings(void)
443 if (! ui_prefs
.msgwindow_visible
)
446 vf
->vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->vte
), vc
->scrollback_lines
);
447 vf
->vte_terminal_set_scroll_on_keystroke(VTE_TERMINAL(vc
->vte
), vc
->scroll_on_key
);
448 vf
->vte_terminal_set_scroll_on_output(VTE_TERMINAL(vc
->vte
), vc
->scroll_on_out
);
449 vf
->vte_terminal_set_emulation(VTE_TERMINAL(vc
->vte
), vc
->emulation
);
450 vf
->vte_terminal_set_font_from_string(VTE_TERMINAL(vc
->vte
), vc
->font
);
451 vf
->vte_terminal_set_color_foreground(VTE_TERMINAL(vc
->vte
), vc
->colour_fore
);
452 vf
->vte_terminal_set_color_bold(VTE_TERMINAL(vc
->vte
), vc
->colour_fore
);
453 vf
->vte_terminal_set_color_background(VTE_TERMINAL(vc
->vte
), vc
->colour_back
);
454 vf
->vte_terminal_set_audible_bell(VTE_TERMINAL(vc
->vte
), prefs
.beep_on_errors
);
455 vte_set_cursor_blink_mode();
461 static void vte_popup_menu_clicked(GtkMenuItem
*menuitem
, gpointer user_data
)
463 switch (GPOINTER_TO_INT(user_data
))
467 if (vf
->vte_terminal_get_has_selection(VTE_TERMINAL(vc
->vte
)))
468 vf
->vte_terminal_copy_clipboard(VTE_TERMINAL(vc
->vte
));
473 vf
->vte_terminal_paste_clipboard(VTE_TERMINAL(vc
->vte
));
476 case POPUP_SELECTALL
:
481 case POPUP_CHANGEPATH
:
483 GeanyDocument
*doc
= document_get_current();
485 vte_cwd(doc
->file_name
, TRUE
);
488 case POPUP_RESTARTTERMINAL
:
490 vte_restart(vc
->vte
);
493 case POPUP_PREFERENCES
:
499 notebook
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "notebook2");
501 gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook
), vte_prefs_tab_num
);
508 static GtkWidget
*vte_create_popup_menu(void)
510 GtkWidget
*menu
, *item
;
511 GtkAccelGroup
*accel_group
;
513 menu
= gtk_menu_new();
515 accel_group
= gtk_accel_group_new();
516 gtk_window_add_accel_group(GTK_WINDOW(main_widgets
.window
), accel_group
);
518 item
= gtk_image_menu_item_new_from_stock("gtk-copy", NULL
);
519 gtk_widget_add_accelerator(item
, "activate", accel_group
,
520 GDK_c
, GDK_CONTROL_MASK
| GDK_SHIFT_MASK
, GTK_ACCEL_VISIBLE
);
521 gtk_widget_show(item
);
522 gtk_container_add(GTK_CONTAINER(menu
), item
);
523 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_COPY
));
525 item
= gtk_image_menu_item_new_from_stock("gtk-paste", NULL
);
526 gtk_widget_add_accelerator(item
, "activate", accel_group
,
527 GDK_v
, GDK_CONTROL_MASK
| GDK_SHIFT_MASK
, GTK_ACCEL_VISIBLE
);
528 gtk_widget_show(item
);
529 gtk_container_add(GTK_CONTAINER(menu
), item
);
530 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_PASTE
));
532 item
= gtk_separator_menu_item_new();
533 gtk_widget_show(item
);
534 gtk_container_add(GTK_CONTAINER(menu
), item
);
536 item
= gtk_image_menu_item_new_from_stock("gtk-select-all", NULL
);
537 gtk_widget_show(item
);
538 gtk_container_add(GTK_CONTAINER(menu
), item
);
539 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_SELECTALL
));
541 item
= gtk_separator_menu_item_new();
542 gtk_widget_show(item
);
543 gtk_container_add(GTK_CONTAINER(menu
), item
);
545 item
= gtk_image_menu_item_new_with_mnemonic(_("_Set Path From Document"));
546 gtk_widget_show(item
);
547 gtk_container_add(GTK_CONTAINER(menu
), item
);
548 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_CHANGEPATH
));
550 item
= gtk_image_menu_item_new_with_mnemonic(_("_Restart Terminal"));
551 gtk_widget_show(item
);
552 gtk_container_add(GTK_CONTAINER(menu
), item
);
553 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_RESTARTTERMINAL
));
555 item
= gtk_separator_menu_item_new();
556 gtk_widget_show(item
);
557 gtk_container_add(GTK_CONTAINER(menu
), item
);
559 item
= gtk_image_menu_item_new_from_stock("gtk-preferences", NULL
);
560 gtk_widget_show(item
);
561 gtk_container_add(GTK_CONTAINER(menu
), item
);
562 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_PREFERENCES
));
564 msgwin_menu_add_common_items(GTK_MENU(menu
));
566 item
= gtk_separator_menu_item_new();
567 gtk_widget_show(item
);
568 gtk_container_add(GTK_CONTAINER(menu
), item
);
570 /* the IM submenu should always be the last item to be consistent with other GTK popup menus */
571 vc
->im_submenu
= gtk_menu_new();
573 item
= gtk_image_menu_item_new_with_mnemonic(_("_Input Methods"));
574 gtk_widget_show(item
);
575 gtk_container_add(GTK_CONTAINER(menu
), item
);
577 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item
), vc
->im_submenu
);
578 /* submenu populated after vte realized */
583 /* If the command could be executed, TRUE is returned, FALSE otherwise (i.e. there was some text
585 gboolean
vte_send_cmd(const gchar
*cmd
)
589 vf
->vte_terminal_feed_child(VTE_TERMINAL(vc
->vte
), cmd
, strlen(cmd
));
590 clean
= TRUE
; /* vte_terminal_feed_child() also marks the vte as not clean */
598 /* Taken from Terminal by os-cillation: terminal_screen_get_working_directory, thanks.
599 * Determines the working directory using various OS-specific mechanisms and stores the determined
600 * directory in vte_info.dir. Note: vte_info.dir contains the real path. */
601 const gchar
*vte_get_working_directory(void)
603 gchar buffer
[4096 + 1];
610 file
= g_strdup_printf("/proc/%d/cwd", pid
);
611 length
= readlink(file
, buffer
, sizeof(buffer
));
613 if (length
> 0 && *buffer
== '/')
615 buffer
[length
] = '\0';
616 g_free(vte_info
.dir
);
617 vte_info
.dir
= g_strdup(buffer
);
619 else if (length
== 0)
621 cwd
= g_get_current_dir();
624 if (chdir(file
) == 0)
626 g_free(vte_info
.dir
);
627 vte_info
.dir
= g_get_current_dir();
629 geany_debug("%s: %s", G_STRFUNC
, g_strerror(errno
));
641 /* Changes the current working directory of the VTE to the path of the given filename.
642 * filename is expected to be in UTF-8 encoding.
643 * filename can also be a path, then it is used directly.
644 * If force is set to TRUE, it will always change the cwd
646 void vte_cwd(const gchar
*filename
, gboolean force
)
648 if (vte_info
.have_vte
&& (vc
->follow_path
|| force
) &&
649 filename
!= NULL
&& g_path_is_absolute(filename
))
653 if (g_file_test(filename
, G_FILE_TEST_IS_DIR
))
654 path
= g_strdup(filename
);
656 path
= g_path_get_dirname(filename
);
658 vte_get_working_directory(); /* refresh vte_info.dir */
659 if (! utils_str_equal(path
, vte_info
.dir
))
661 /* use g_shell_quote to avoid problems with spaces, '!' or something else in path */
662 gchar
*quoted_path
= g_shell_quote(path
);
663 gchar
*cmd
= g_strconcat("cd ", quoted_path
, "\n", NULL
);
664 if (! vte_send_cmd(cmd
))
666 ui_set_statusbar(FALSE
,
667 _("Could not change the directory in the VTE because it probably contains a command."));
669 "Could not change the directory in the VTE because it probably contains a command.");
679 static void vte_drag_data_received(GtkWidget
*widget
, GdkDragContext
*drag_context
,
680 gint x
, gint y
, GtkSelectionData
*data
, guint info
, guint ltime
)
682 if (info
== TARGET_TEXT_PLAIN
)
684 if (data
->format
== 8 && data
->length
> 0)
685 vf
->vte_terminal_feed_child(VTE_TERMINAL(widget
),
686 (const gchar
*) data
->data
, data
->length
);
690 gchar
*text
= (gchar
*) gtk_selection_data_get_text(data
);
692 vf
->vte_terminal_feed_child(VTE_TERMINAL(widget
), text
, strlen(text
));
695 gtk_drag_finish(drag_context
, TRUE
, FALSE
, ltime
);
699 static void check_run_in_vte_toggled(GtkToggleButton
*togglebutton
, GtkWidget
*user_data
)
701 gtk_widget_set_sensitive(user_data
, gtk_toggle_button_get_active(togglebutton
));
705 static void font_button_clicked_cb(GtkFontButton
*widget
, gpointer user_data
)
707 const gchar
*fontbtn
= gtk_font_button_get_font_name(widget
);
709 if (! utils_str_equal(fontbtn
, vc
->font
))
711 setptr(vc
->font
, g_strdup(gtk_font_button_get_font_name(widget
)));
712 vte_apply_user_settings();
717 static void on_color_button_choose_cb(GtkColorButton
*widget
, gpointer user_data
)
719 switch (GPOINTER_TO_INT(user_data
))
723 g_free(vc
->colour_fore
);
724 vc
->colour_fore
= g_new0(GdkColor
, 1);
725 gtk_color_button_get_color(widget
, vc
->colour_fore
);
730 g_free(vc
->colour_back
);
731 vc
->colour_back
= g_new0(GdkColor
, 1);
732 gtk_color_button_get_color(widget
, vc
->colour_back
);
739 void vte_append_preferences_tab(void)
741 if (vte_info
.have_vte
)
743 GtkWidget
*notebook
, *vbox
, *label
, *alignment
, *table
, *frame
, *box
;
744 GtkWidget
*font_term
, *color_fore
, *color_back
, *spin_scrollback
;
745 GtkWidget
*check_scroll_key
, *check_scroll_out
, *check_follow_path
;
746 GtkWidget
*check_enable_bash_keys
, *check_ignore_menu_key
, *check_cursor_blinks
;
747 GtkWidget
*check_run_in_vte
, *check_skip_script
, *entry_shell
, *button_shell
, *image_shell
;
748 GtkObject
*spin_scrollback_adj
;
750 notebook
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "notebook2");
752 frame
= ui_frame_new_with_alignment(_("Terminal"), &alignment
);
753 gtk_container_set_border_width(GTK_CONTAINER(frame
), 5);
754 vbox
= gtk_vbox_new(FALSE
, 12);
755 gtk_container_add(GTK_CONTAINER(alignment
), vbox
);
757 label
= gtk_label_new(_("Terminal"));
758 vte_prefs_tab_num
= gtk_notebook_append_page(GTK_NOTEBOOK(notebook
), frame
, label
);
760 table
= gtk_table_new(6, 2, FALSE
);
761 gtk_box_pack_start(GTK_BOX(vbox
), table
, FALSE
, FALSE
, 0);
762 gtk_table_set_row_spacings(GTK_TABLE(table
), 3);
763 gtk_table_set_col_spacings(GTK_TABLE(table
), 10);
765 label
= gtk_label_new(_("Font:"));
766 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 0, 1,
767 (GtkAttachOptions
) (GTK_FILL
),
768 (GtkAttachOptions
) (0), 0, 0);
769 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
771 font_term
= gtk_font_button_new();
772 gtk_table_attach(GTK_TABLE(table
), font_term
, 1, 2, 0, 1,
773 (GtkAttachOptions
) (GTK_EXPAND
| GTK_FILL
),
774 (GtkAttachOptions
) (0), 0, 0);
775 gtk_widget_set_tooltip_text(font_term
, _("Sets the font for the terminal widget"));
777 label
= gtk_label_new(_("Foreground color:"));
778 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 1, 2,
779 (GtkAttachOptions
) (GTK_FILL
),
780 (GtkAttachOptions
) (0), 0, 0);
781 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
783 label
= gtk_label_new(_("Background color:"));
784 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 2, 3,
785 (GtkAttachOptions
) (GTK_FILL
),
786 (GtkAttachOptions
) (0), 0, 0);
787 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
789 color_fore
= gtk_color_button_new();
790 gtk_table_attach(GTK_TABLE(table
), color_fore
, 1, 2, 1, 2,
791 (GtkAttachOptions
) (GTK_FILL
),
792 (GtkAttachOptions
) (0), 0, 0);
793 gtk_widget_set_tooltip_text(color_fore
, _("Sets the foreground color of the text in the terminal widget"));
794 gtk_color_button_set_title(GTK_COLOR_BUTTON(color_fore
), _("Color Chooser"));
796 color_back
= gtk_color_button_new();
797 gtk_table_attach(GTK_TABLE(table
), color_back
, 1, 2, 2, 3,
798 (GtkAttachOptions
) (GTK_FILL
),
799 (GtkAttachOptions
) (0), 0, 0);
800 gtk_widget_set_tooltip_text(color_back
, _("Sets the background color of the text in the terminal widget"));
801 gtk_color_button_set_title(GTK_COLOR_BUTTON(color_back
), _("Color Chooser"));
803 label
= gtk_label_new(_("Scrollback lines:"));
804 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 3, 4,
805 (GtkAttachOptions
) (GTK_FILL
),
806 (GtkAttachOptions
) (0), 0, 0);
807 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
809 spin_scrollback_adj
= gtk_adjustment_new(500, 0, 5000, 1, 10, 0);
810 spin_scrollback
= gtk_spin_button_new(GTK_ADJUSTMENT(spin_scrollback_adj
), 1, 0);
811 ui_entry_add_clear_icon(GTK_ENTRY(spin_scrollback
));
812 gtk_table_attach(GTK_TABLE(table
), spin_scrollback
, 1, 2, 3, 4,
813 (GtkAttachOptions
) (GTK_EXPAND
| GTK_FILL
),
814 (GtkAttachOptions
) (0), 0, 0);
815 gtk_widget_set_tooltip_text(spin_scrollback
, _("Specifies the history in lines, which you can scroll back in the terminal widget"));
816 gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(spin_scrollback
), TRUE
);
817 gtk_spin_button_set_wrap(GTK_SPIN_BUTTON(spin_scrollback
), TRUE
);
819 label
= gtk_label_new(_("Shell:"));
820 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 5, 6,
821 (GtkAttachOptions
) (GTK_FILL
),
822 (GtkAttachOptions
) (0), 0, 0);
823 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
825 entry_shell
= gtk_entry_new();
826 ui_entry_add_clear_icon(GTK_ENTRY(entry_shell
));
827 gtk_widget_set_tooltip_text(entry_shell
, _("Sets the path to the shell which should be started inside the terminal emulation"));
829 button_shell
= gtk_button_new();
830 gtk_widget_show(button_shell
);
832 box
= gtk_hbox_new(FALSE
, 6);
833 gtk_box_pack_start_defaults(GTK_BOX(box
), entry_shell
);
834 gtk_box_pack_start(GTK_BOX(box
), button_shell
, FALSE
, FALSE
, 0);
835 gtk_table_attach(GTK_TABLE(table
), box
, 1, 2, 5, 6,
836 (GtkAttachOptions
) (GTK_EXPAND
| GTK_FILL
),
837 (GtkAttachOptions
) (0), 0, 0);
839 image_shell
= gtk_image_new_from_stock("gtk-open", GTK_ICON_SIZE_BUTTON
);
840 gtk_widget_show(image_shell
);
841 gtk_container_add(GTK_CONTAINER(button_shell
), image_shell
);
843 box
= gtk_vbox_new(FALSE
, 3);
844 check_scroll_key
= gtk_check_button_new_with_mnemonic(_("Scroll on keystroke"));
845 gtk_widget_set_tooltip_text(check_scroll_key
, _("Whether to scroll to the bottom if a key was pressed"));
846 gtk_container_add(GTK_CONTAINER(box
), check_scroll_key
);
848 check_scroll_out
= gtk_check_button_new_with_mnemonic(_("Scroll on output"));
849 gtk_widget_set_tooltip_text(check_scroll_out
, _("Whether to scroll to the bottom when output is generated"));
850 gtk_container_add(GTK_CONTAINER(box
), check_scroll_out
);
852 check_cursor_blinks
= gtk_check_button_new_with_mnemonic(_("Cursor blinks"));
853 gtk_widget_set_tooltip_text(check_cursor_blinks
, _("Whether to blink the cursor"));
854 gtk_container_add(GTK_CONTAINER(box
), check_cursor_blinks
);
856 check_enable_bash_keys
= gtk_check_button_new_with_mnemonic(_("Override Geany keybindings"));
857 gtk_widget_set_tooltip_text(check_enable_bash_keys
,
858 _("Allows the VTE to receive keyboard shortcuts (apart from focus commands)"));
859 gtk_container_add(GTK_CONTAINER(box
), check_enable_bash_keys
);
861 check_ignore_menu_key
= gtk_check_button_new_with_mnemonic(_("Disable menu shortcut key (F10 by default)"));
862 gtk_widget_set_tooltip_text(check_ignore_menu_key
, _("This option disables the keybinding to popup the menu bar (default is F10). Disabling it can be useful if you use, for example, Midnight Commander within the VTE."));
863 gtk_container_add(GTK_CONTAINER(box
), check_ignore_menu_key
);
865 check_follow_path
= gtk_check_button_new_with_mnemonic(_("Follow the path of the current file"));
866 gtk_widget_set_tooltip_text(check_follow_path
, _("Whether to execute \"cd $path\" when you switch between opened files"));
867 gtk_container_add(GTK_CONTAINER(box
), check_follow_path
);
869 /* create check_skip_script checkbox before the check_skip_script checkbox to be able to
870 * use the object for the toggled handler of check_skip_script checkbox */
871 check_skip_script
= gtk_check_button_new_with_mnemonic(_("Don't use run script"));
872 gtk_widget_set_tooltip_text(check_skip_script
, _("Don't use the simple run script which is usually used to display the exit status of the executed program"));
873 gtk_widget_set_sensitive(check_skip_script
, vc
->run_in_vte
);
875 check_run_in_vte
= gtk_check_button_new_with_mnemonic(_("Execute programs in VTE"));
876 gtk_widget_set_tooltip_text(check_run_in_vte
, _("Run programs in VTE instead of opening a terminal emulation window. Please note, programs executed in VTE cannot be stopped"));
877 gtk_container_add(GTK_CONTAINER(box
), check_run_in_vte
);
878 g_signal_connect(check_run_in_vte
, "toggled",
879 G_CALLBACK(check_run_in_vte_toggled
), check_skip_script
);
881 /* now add the check_skip_script checkbox after the check_run_in_vte checkbox */
882 gtk_container_add(GTK_CONTAINER(box
), check_skip_script
);
884 gtk_box_pack_start(GTK_BOX(vbox
), box
, FALSE
, FALSE
, 0);
886 ui_hookup_widget(ui_widgets
.prefs_dialog
, font_term
, "font_term");
887 ui_hookup_widget(ui_widgets
.prefs_dialog
, color_fore
, "color_fore");
888 ui_hookup_widget(ui_widgets
.prefs_dialog
, color_back
, "color_back");
889 ui_hookup_widget(ui_widgets
.prefs_dialog
, spin_scrollback
, "spin_scrollback");
890 ui_hookup_widget(ui_widgets
.prefs_dialog
, entry_shell
, "entry_shell");
891 ui_hookup_widget(ui_widgets
.prefs_dialog
, check_scroll_key
, "check_scroll_key");
892 ui_hookup_widget(ui_widgets
.prefs_dialog
, check_scroll_out
, "check_scroll_out");
893 ui_hookup_widget(ui_widgets
.prefs_dialog
, check_cursor_blinks
, "check_cursor_blinks");
894 ui_hookup_widget(ui_widgets
.prefs_dialog
, check_enable_bash_keys
, "check_enable_bash_keys");
895 ui_hookup_widget(ui_widgets
.prefs_dialog
, check_ignore_menu_key
, "check_ignore_menu_key");
896 ui_hookup_widget(ui_widgets
.prefs_dialog
, check_follow_path
, "check_follow_path");
897 ui_hookup_widget(ui_widgets
.prefs_dialog
, check_run_in_vte
, "check_run_in_vte");
898 ui_hookup_widget(ui_widgets
.prefs_dialog
, check_skip_script
, "check_skip_script");
900 gtk_widget_show_all(frame
);
902 g_signal_connect(font_term
, "font-set", G_CALLBACK(font_button_clicked_cb
), NULL
);
903 g_signal_connect(color_fore
, "color-set", G_CALLBACK(on_color_button_choose_cb
),
905 g_signal_connect(color_back
, "color-set", G_CALLBACK(on_color_button_choose_cb
),
907 ui_setup_open_button_callback(button_shell
, NULL
,
908 GTK_FILE_CHOOSER_ACTION_OPEN
, GTK_ENTRY(entry_shell
));
913 void vte_select_all(void)
915 if (vf
->vte_terminal_select_all
!= NULL
)
916 vf
->vte_terminal_select_all(VTE_TERMINAL(vc
->vte
));
920 void vte_send_selection_to_vte(void)
926 doc
= document_get_current();
927 g_return_if_fail(doc
!= NULL
);
929 if (sci_has_selection(doc
->editor
->sci
))
931 text
= g_malloc0(sci_get_selected_text_length(doc
->editor
->sci
) + 1);
932 sci_get_selected_text(doc
->editor
->sci
, text
);
935 { /* Get the current line */
936 gint line_num
= sci_get_current_line(doc
->editor
->sci
);
937 text
= sci_get_line(doc
->editor
->sci
, line_num
);
942 if (vc
->send_selection_unsafe
)
943 { /* Explicitly append a trailing newline character to get the command executed,
944 this is disabled by default as it could cause all sorts of damage. */
945 if (text
[len
-1] != '\n' && text
[len
-1] != '\r')
947 setptr(text
, g_strconcat(text
, "\n", NULL
));
952 { /* Make sure there is no newline character at the end to prevent unwanted execution */
953 while (text
[len
-1] == '\n' || text
[len
-1] == '\r')
960 vf
->vte_terminal_feed_child(VTE_TERMINAL(vc
->vte
), text
, len
);
963 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_VTE
);
964 gtk_widget_grab_focus(vc
->vte
);
965 msgwin_show_hide(TRUE
);