2 * vte.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2010 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 gboolean popup_menu_created
= FALSE
;
64 static gchar
*gtk_menu_key_accel
= NULL
;
65 static gint vte_prefs_tab_num
= -1;
67 /* use vte wordchars to select paths */
68 static const gchar VTE_WORDCHARS
[] = "-A-Za-z0-9,./?%&#:_";
71 /* Incomplete VteTerminal struct from vte/vte.h. */
72 typedef struct _VteTerminal VteTerminal
;
76 GtkAdjustment
*adjustment
;
79 #define VTE_TERMINAL(obj) (GTK_CHECK_CAST((obj), VTE_TYPE_TERMINAL, VteTerminal))
80 #define VTE_TYPE_TERMINAL (vf->vte_terminal_get_type())
83 VTE_CURSOR_BLINK_SYSTEM
,
86 } VteTerminalCursorBlinkMode
;
89 /* Holds function pointers we need to access the VTE API. */
92 GtkWidget
* (*vte_terminal_new
) (void);
93 pid_t (*vte_terminal_fork_command
) (VteTerminal
*terminal
, const char *command
, char **argv
,
94 char **envv
, const char *directory
, gboolean lastlog
,
95 gboolean utmp
, gboolean wtmp
);
96 void (*vte_terminal_set_size
) (VteTerminal
*terminal
, glong columns
, glong rows
);
97 void (*vte_terminal_set_word_chars
) (VteTerminal
*terminal
, const char *spec
);
98 void (*vte_terminal_set_mouse_autohide
) (VteTerminal
*terminal
, gboolean setting
);
99 void (*vte_terminal_reset
) (VteTerminal
*terminal
, gboolean full
, gboolean clear_history
);
100 GtkType (*vte_terminal_get_type
) (void);
101 void (*vte_terminal_set_scroll_on_output
) (VteTerminal
*terminal
, gboolean scroll
);
102 void (*vte_terminal_set_scroll_on_keystroke
) (VteTerminal
*terminal
, gboolean scroll
);
103 void (*vte_terminal_set_font_from_string
) (VteTerminal
*terminal
, const char *name
);
104 void (*vte_terminal_set_scrollback_lines
) (VteTerminal
*terminal
, glong lines
);
105 gboolean (*vte_terminal_get_has_selection
) (VteTerminal
*terminal
);
106 void (*vte_terminal_copy_clipboard
) (VteTerminal
*terminal
);
107 void (*vte_terminal_paste_clipboard
) (VteTerminal
*terminal
);
108 void (*vte_terminal_set_emulation
) (VteTerminal
*terminal
, const gchar
*emulation
);
109 void (*vte_terminal_set_color_foreground
) (VteTerminal
*terminal
, const GdkColor
*foreground
);
110 void (*vte_terminal_set_color_bold
) (VteTerminal
*terminal
, const GdkColor
*foreground
);
111 void (*vte_terminal_set_color_background
) (VteTerminal
*terminal
, const GdkColor
*background
);
112 void (*vte_terminal_feed_child
) (VteTerminal
*terminal
, const char *data
, glong length
);
113 void (*vte_terminal_im_append_menuitems
) (VteTerminal
*terminal
, GtkMenuShell
*menushell
);
114 void (*vte_terminal_set_cursor_blink_mode
) (VteTerminal
*terminal
,
115 VteTerminalCursorBlinkMode mode
);
116 void (*vte_terminal_set_cursor_blinks
) (VteTerminal
*terminal
, gboolean blink
);
117 void (*vte_terminal_select_all
) (VteTerminal
*terminal
);
118 void (*vte_terminal_set_audible_bell
) (VteTerminal
*terminal
, gboolean is_audible
);
122 static void create_vte(void);
123 static void vte_start(GtkWidget
*widget
);
124 static void vte_restart(GtkWidget
*widget
);
125 static gboolean
vte_button_pressed(GtkWidget
*widget
, GdkEventButton
*event
, gpointer user_data
);
126 static gboolean
vte_keyrelease_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
);
127 static gboolean
vte_keypress_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
);
128 static void vte_register_symbols(GModule
*module
);
129 static void vte_popup_menu_clicked(GtkMenuItem
*menuitem
, gpointer user_data
);
130 static GtkWidget
*vte_create_popup_menu(void);
131 static void vte_commit_cb(VteTerminal
*vte
, gchar
*arg1
, guint arg2
, gpointer user_data
);
132 static void vte_drag_data_received(GtkWidget
*widget
, GdkDragContext
*drag_context
,
133 gint x
, gint y
, GtkSelectionData
*data
, guint info
, guint ltime
);
142 POPUP_RESTARTTERMINAL
,
144 TARGET_UTF8_STRING
= 0,
146 TARGET_COMPOUND_TEXT
,
151 static const GtkTargetEntry dnd_targets
[] =
153 { "UTF8_STRING", 0, TARGET_UTF8_STRING
},
154 { "TEXT", 0, TARGET_TEXT
},
155 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT
},
156 { "STRING", 0, TARGET_STRING
},
157 { "text/plain", 0, TARGET_TEXT_PLAIN
},
161 static gchar
**vte_get_child_environment(void)
163 const gchar
*exclude_vars
[] = {"COLUMNS", "LINES", "TERM", NULL
};
165 return utils_copy_environment(exclude_vars
, "TERM", "xterm", NULL
);
169 static void override_menu_key(void)
171 if (gtk_menu_key_accel
== NULL
) /* for restoring the default value */
172 g_object_get(G_OBJECT(gtk_settings_get_default()),
173 "gtk-menu-bar-accel", >k_menu_key_accel
, NULL
);
175 if (vc
->ignore_menu_bar_accel
)
176 gtk_settings_set_string_property(gtk_settings_get_default(),
177 "gtk-menu-bar-accel", "<Shift><Control><Mod1><Mod2><Mod3><Mod4><Mod5>F10", "Geany");
179 gtk_settings_set_string_property(gtk_settings_get_default(),
180 "gtk-menu-bar-accel", gtk_menu_key_accel
, "Geany");
186 if (vte_info
.have_vte
== FALSE
)
187 { /* vte_info.have_vte can be false even if VTE is compiled in, think of command line option */
188 geany_debug("Disabling terminal support");
192 if (NZV(vte_info
.lib_vte
))
194 module
= g_module_open(vte_info
.lib_vte
, G_MODULE_BIND_LAZY
);
196 #ifdef VTE_MODULE_PATH
199 module
= g_module_open(VTE_MODULE_PATH
, G_MODULE_BIND_LAZY
);
206 const gchar
*sonames
[] = { "libvte.so", "libvte.so.4",
207 "libvte.so.8", "libvte.so.9", NULL
};
209 for (i
= 0; sonames
[i
] != NULL
&& module
== NULL
; i
++)
211 module
= g_module_open(sonames
[i
], G_MODULE_BIND_LAZY
);
217 vte_info
.have_vte
= FALSE
;
218 geany_debug("Could not load libvte.so, embedded terminal support disabled");
223 vte_info
.have_vte
= TRUE
;
224 vf
= g_new0(struct VteFunctions
, 1);
225 vte_register_symbols(module
);
230 /* setup the F10 menu override (so it works before the widget is first realised). */
235 static void create_vte(void)
237 GtkWidget
*vte
, *scrollbar
, *hbox
, *frame
;
239 vc
->vte
= vte
= vf
->vte_terminal_new();
240 scrollbar
= gtk_vscrollbar_new(GTK_ADJUSTMENT(VTE_TERMINAL(vte
)->adjustment
));
241 GTK_WIDGET_UNSET_FLAGS(scrollbar
, GTK_CAN_FOCUS
);
243 frame
= gtk_frame_new(NULL
);
245 hbox
= gtk_hbox_new(FALSE
, 0);
246 gtk_container_add(GTK_CONTAINER(frame
), hbox
);
247 gtk_box_pack_start(GTK_BOX(hbox
), vte
, TRUE
, TRUE
, 0);
248 gtk_box_pack_start(GTK_BOX(hbox
), scrollbar
, FALSE
, FALSE
, 0);
250 /* set the default widget size first to prevent VTE expanding too much,
251 * sometimes causing the hscrollbar to be too big or out of view. */
252 gtk_widget_set_size_request(GTK_WIDGET(vte
), 10, 10);
253 vf
->vte_terminal_set_size(VTE_TERMINAL(vte
), 30, 1);
255 vf
->vte_terminal_set_mouse_autohide(VTE_TERMINAL(vte
), TRUE
);
256 vf
->vte_terminal_set_word_chars(VTE_TERMINAL(vte
), VTE_WORDCHARS
);
258 gtk_drag_dest_set(vte
, GTK_DEST_DEFAULT_ALL
,
259 dnd_targets
, G_N_ELEMENTS(dnd_targets
), GDK_ACTION_COPY
);
261 g_signal_connect(vte
, "child-exited", G_CALLBACK(vte_start
), NULL
);
262 g_signal_connect(vte
, "button-press-event", G_CALLBACK(vte_button_pressed
), NULL
);
263 g_signal_connect(vte
, "event", G_CALLBACK(vte_keypress_cb
), NULL
);
264 g_signal_connect(vte
, "key-release-event", G_CALLBACK(vte_keyrelease_cb
), NULL
);
265 g_signal_connect(vte
, "commit", G_CALLBACK(vte_commit_cb
), NULL
);
266 g_signal_connect(vte
, "motion-notify-event", G_CALLBACK(on_motion_event
), NULL
);
267 g_signal_connect(vte
, "drag-data-received", G_CALLBACK(vte_drag_data_received
), NULL
);
271 gtk_widget_show_all(frame
);
272 gtk_notebook_insert_page(GTK_NOTEBOOK(msgwindow
.notebook
), frame
, gtk_label_new(_("Terminal")), MSG_VTE
);
274 /* the vte widget has to be realised before color changes take effect */
275 g_signal_connect_after(vte
, "realize", G_CALLBACK(vte_apply_user_settings
), NULL
);
282 /* free the vte widget before unloading vte module
283 * this prevents a segfault on X close window if the message window is hidden */
284 gtk_widget_destroy(vc
->vte
);
285 if (popup_menu_created
)
286 gtk_widget_destroy(vc
->menu
);
287 g_free(vc
->emulation
);
290 g_free(vc
->colour_back
);
291 g_free(vc
->colour_fore
);
293 g_free(gtk_menu_key_accel
);
294 /* Don't unload the module explicitly because it causes a segfault on FreeBSD. The segfault
295 * happens when the app really exits, not directly on g_module_close(). This still needs to
296 * be investigated. */
297 /*g_module_close(module); */
301 static gboolean
vte_keyrelease_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
303 if (ui_is_keyval_enter_or_return(event
->keyval
) ||
304 ((event
->keyval
== GDK_c
) && (event
->state
& GDK_CONTROL_MASK
)))
306 /* assume any text on the prompt has been executed when pressing Enter/Return */
313 static gboolean
vte_keypress_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
315 if (vc
->enable_bash_keys
)
316 return FALSE
; /* Ctrl-[CD] will be handled by the VTE itself */
318 if (event
->type
!= GDK_KEY_RELEASE
)
321 if ((event
->keyval
== GDK_c
||
322 event
->keyval
== GDK_d
||
323 event
->keyval
== GDK_C
||
324 event
->keyval
== GDK_D
) &&
325 event
->state
& GDK_CONTROL_MASK
&&
326 ! (event
->state
& GDK_SHIFT_MASK
) && ! (event
->state
& GDK_MOD1_MASK
))
335 static void vte_commit_cb(VteTerminal
*vte
, gchar
*arg1
, guint arg2
, gpointer user_data
)
341 static void vte_start(GtkWidget
*widget
)
346 /* split the shell command line, so arguments will work too */
347 argv
= g_strsplit(vc
->shell
, " ", -1);
351 env
= vte_get_child_environment();
352 pid
= vf
->vte_terminal_fork_command(VTE_TERMINAL(widget
), argv
[0], argv
, env
,
353 vte_info
.dir
, TRUE
, TRUE
, TRUE
);
358 pid
= 0; /* use 0 as invalid pid */
364 static void vte_restart(GtkWidget
*widget
)
366 vte_get_working_directory(); /* try to keep the working directory when restarting the VTE */
372 vf
->vte_terminal_reset(VTE_TERMINAL(widget
), TRUE
, TRUE
);
377 static gboolean
vte_button_pressed(GtkWidget
*widget
, GdkEventButton
*event
, gpointer user_data
)
379 if (event
->button
== 3)
381 if (G_UNLIKELY(! popup_menu_created
))
383 vc
->menu
= vte_create_popup_menu();
384 vf
->vte_terminal_im_append_menuitems(VTE_TERMINAL(vc
->vte
), GTK_MENU_SHELL(vc
->im_submenu
));
385 popup_menu_created
= TRUE
;
388 gtk_widget_grab_focus(vc
->vte
);
389 gtk_menu_popup(GTK_MENU(vc
->menu
), NULL
, NULL
, NULL
, NULL
, event
->button
, event
->time
);
396 static void vte_set_cursor_blink_mode(void)
398 if (vf
->vte_terminal_set_cursor_blink_mode
!= NULL
)
400 vf
->vte_terminal_set_cursor_blink_mode(VTE_TERMINAL(vc
->vte
),
401 (vc
->cursor_blinks
) ? VTE_CURSOR_BLINK_ON
: VTE_CURSOR_BLINK_OFF
);
404 vf
->vte_terminal_set_cursor_blinks(VTE_TERMINAL(vc
->vte
), vc
->cursor_blinks
);
408 static void vte_register_symbols(GModule
*mod
)
410 g_module_symbol(mod
, "vte_terminal_new", (void*)&vf
->vte_terminal_new
);
411 g_module_symbol(mod
, "vte_terminal_set_size", (void*)&vf
->vte_terminal_set_size
);
412 g_module_symbol(mod
, "vte_terminal_fork_command", (void*)&vf
->vte_terminal_fork_command
);
413 g_module_symbol(mod
, "vte_terminal_set_word_chars", (void*)&vf
->vte_terminal_set_word_chars
);
414 g_module_symbol(mod
, "vte_terminal_set_mouse_autohide", (void*)&vf
->vte_terminal_set_mouse_autohide
);
415 g_module_symbol(mod
, "vte_terminal_reset", (void*)&vf
->vte_terminal_reset
);
416 g_module_symbol(mod
, "vte_terminal_get_type", (void*)&vf
->vte_terminal_get_type
);
417 g_module_symbol(mod
, "vte_terminal_set_scroll_on_output", (void*)&vf
->vte_terminal_set_scroll_on_output
);
418 g_module_symbol(mod
, "vte_terminal_set_scroll_on_keystroke", (void*)&vf
->vte_terminal_set_scroll_on_keystroke
);
419 g_module_symbol(mod
, "vte_terminal_set_font_from_string", (void*)&vf
->vte_terminal_set_font_from_string
);
420 g_module_symbol(mod
, "vte_terminal_set_scrollback_lines", (void*)&vf
->vte_terminal_set_scrollback_lines
);
421 g_module_symbol(mod
, "vte_terminal_get_has_selection", (void*)&vf
->vte_terminal_get_has_selection
);
422 g_module_symbol(mod
, "vte_terminal_copy_clipboard", (void*)&vf
->vte_terminal_copy_clipboard
);
423 g_module_symbol(mod
, "vte_terminal_paste_clipboard", (void*)&vf
->vte_terminal_paste_clipboard
);
424 g_module_symbol(mod
, "vte_terminal_set_emulation", (void*)&vf
->vte_terminal_set_emulation
);
425 g_module_symbol(mod
, "vte_terminal_set_color_foreground", (void*)&vf
->vte_terminal_set_color_foreground
);
426 g_module_symbol(mod
, "vte_terminal_set_color_bold", (void*)&vf
->vte_terminal_set_color_bold
);
427 g_module_symbol(mod
, "vte_terminal_set_color_background", (void*)&vf
->vte_terminal_set_color_background
);
428 g_module_symbol(mod
, "vte_terminal_feed_child", (void*)&vf
->vte_terminal_feed_child
);
429 g_module_symbol(mod
, "vte_terminal_im_append_menuitems", (void*)&vf
->vte_terminal_im_append_menuitems
);
430 g_module_symbol(mod
, "vte_terminal_set_cursor_blink_mode", (void*)&vf
->vte_terminal_set_cursor_blink_mode
);
431 if (vf
->vte_terminal_set_cursor_blink_mode
== NULL
)
432 /* vte_terminal_set_cursor_blink_mode() is only available since 0.17.1, so if we don't find
433 * this symbol, we are probably on an older version and use the old API instead */
434 g_module_symbol(mod
, "vte_terminal_set_cursor_blinks", (void*)&vf
->vte_terminal_set_cursor_blinks
);
435 g_module_symbol(mod
, "vte_terminal_select_all", (void*)&vf
->vte_terminal_select_all
);
436 g_module_symbol(mod
, "vte_terminal_set_audible_bell", (void*)&vf
->vte_terminal_set_audible_bell
);
440 void vte_apply_user_settings(void)
442 if (! ui_prefs
.msgwindow_visible
)
445 vf
->vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->vte
), vc
->scrollback_lines
);
446 vf
->vte_terminal_set_scroll_on_keystroke(VTE_TERMINAL(vc
->vte
), vc
->scroll_on_key
);
447 vf
->vte_terminal_set_scroll_on_output(VTE_TERMINAL(vc
->vte
), vc
->scroll_on_out
);
448 vf
->vte_terminal_set_emulation(VTE_TERMINAL(vc
->vte
), vc
->emulation
);
449 vf
->vte_terminal_set_font_from_string(VTE_TERMINAL(vc
->vte
), vc
->font
);
450 vf
->vte_terminal_set_color_foreground(VTE_TERMINAL(vc
->vte
), vc
->colour_fore
);
451 vf
->vte_terminal_set_color_bold(VTE_TERMINAL(vc
->vte
), vc
->colour_fore
);
452 vf
->vte_terminal_set_color_background(VTE_TERMINAL(vc
->vte
), vc
->colour_back
);
453 vf
->vte_terminal_set_audible_bell(VTE_TERMINAL(vc
->vte
), prefs
.beep_on_errors
);
454 vte_set_cursor_blink_mode();
460 static void vte_popup_menu_clicked(GtkMenuItem
*menuitem
, gpointer user_data
)
462 switch (GPOINTER_TO_INT(user_data
))
466 if (vf
->vte_terminal_get_has_selection(VTE_TERMINAL(vc
->vte
)))
467 vf
->vte_terminal_copy_clipboard(VTE_TERMINAL(vc
->vte
));
472 vf
->vte_terminal_paste_clipboard(VTE_TERMINAL(vc
->vte
));
475 case POPUP_SELECTALL
:
480 case POPUP_CHANGEPATH
:
482 GeanyDocument
*doc
= document_get_current();
484 vte_cwd(doc
->file_name
, TRUE
);
487 case POPUP_RESTARTTERMINAL
:
489 vte_restart(vc
->vte
);
492 case POPUP_PREFERENCES
:
498 notebook
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "notebook2");
500 gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook
), vte_prefs_tab_num
);
507 static GtkWidget
*vte_create_popup_menu(void)
509 GtkWidget
*menu
, *item
;
511 menu
= gtk_menu_new();
513 item
= gtk_image_menu_item_new_from_stock("gtk-copy", NULL
);
514 gtk_widget_show(item
);
515 gtk_container_add(GTK_CONTAINER(menu
), item
);
516 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_COPY
));
518 item
= gtk_image_menu_item_new_from_stock("gtk-paste", NULL
);
519 gtk_widget_show(item
);
520 gtk_container_add(GTK_CONTAINER(menu
), item
);
521 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_PASTE
));
523 item
= gtk_separator_menu_item_new();
524 gtk_widget_show(item
);
525 gtk_container_add(GTK_CONTAINER(menu
), item
);
527 item
= gtk_image_menu_item_new_from_stock("gtk-select-all", NULL
);
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_SELECTALL
));
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_with_mnemonic(_("_Set Path From Document"));
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_CHANGEPATH
));
541 item
= gtk_image_menu_item_new_with_mnemonic(_("_Restart Terminal"));
542 gtk_widget_show(item
);
543 gtk_container_add(GTK_CONTAINER(menu
), item
);
544 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_RESTARTTERMINAL
));
546 item
= gtk_separator_menu_item_new();
547 gtk_widget_show(item
);
548 gtk_container_add(GTK_CONTAINER(menu
), item
);
550 item
= gtk_image_menu_item_new_from_stock("gtk-preferences", NULL
);
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_PREFERENCES
));
555 msgwin_menu_add_common_items(GTK_MENU(menu
));
557 item
= gtk_separator_menu_item_new();
558 gtk_widget_show(item
);
559 gtk_container_add(GTK_CONTAINER(menu
), item
);
561 /* the IM submenu should always be the last item to be consistent with other GTK popup menus */
562 vc
->im_submenu
= gtk_menu_new();
564 item
= gtk_image_menu_item_new_with_mnemonic(_("_Input Methods"));
565 gtk_widget_show(item
);
566 gtk_container_add(GTK_CONTAINER(menu
), item
);
568 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item
), vc
->im_submenu
);
574 /* If the command could be executed, TRUE is returned, FALSE otherwise (i.e. there was some text
576 gboolean
vte_send_cmd(const gchar
*cmd
)
580 vf
->vte_terminal_feed_child(VTE_TERMINAL(vc
->vte
), cmd
, strlen(cmd
));
581 clean
= TRUE
; /* vte_terminal_feed_child() also marks the vte as not clean */
589 /* Taken from Terminal by os-cillation: terminal_screen_get_working_directory, thanks.
590 * Determines the working directory using various OS-specific mechanisms and stores the determined
591 * directory in vte_info.dir. Note: vte_info.dir contains the real path. */
592 const gchar
*vte_get_working_directory(void)
594 gchar buffer
[4096 + 1];
601 file
= g_strdup_printf("/proc/%d/cwd", pid
);
602 length
= readlink(file
, buffer
, sizeof(buffer
));
604 if (length
> 0 && *buffer
== '/')
606 buffer
[length
] = '\0';
607 g_free(vte_info
.dir
);
608 vte_info
.dir
= g_strdup(buffer
);
610 else if (length
== 0)
612 cwd
= g_get_current_dir();
615 if (chdir(file
) == 0)
617 g_free(vte_info
.dir
);
618 vte_info
.dir
= g_get_current_dir();
620 geany_debug("%s: %s", G_STRFUNC
, g_strerror(errno
));
632 /* Changes the current working directory of the VTE to the path of the given filename.
633 * filename is expected to be in UTF-8 encoding.
634 * filename can also be a path, then it is used directly.
635 * If force is set to TRUE, it will always change the cwd
637 void vte_cwd(const gchar
*filename
, gboolean force
)
639 if (vte_info
.have_vte
&& (vc
->follow_path
|| force
) &&
640 filename
!= NULL
&& g_path_is_absolute(filename
))
644 if (g_file_test(filename
, G_FILE_TEST_IS_DIR
))
645 path
= g_strdup(filename
);
647 path
= g_path_get_dirname(filename
);
649 vte_get_working_directory(); /* refresh vte_info.dir */
650 if (! utils_str_equal(path
, vte_info
.dir
))
652 /* use g_shell_quote to avoid problems with spaces, '!' or something else in path */
653 gchar
*quoted_path
= g_shell_quote(path
);
654 gchar
*cmd
= g_strconcat("cd ", quoted_path
, "\n", NULL
);
655 if (! vte_send_cmd(cmd
))
657 ui_set_statusbar(FALSE
,
658 _("Could not change the directory in the VTE because it probably contains a command."));
660 "Could not change the directory in the VTE because it probably contains a command.");
670 static void vte_drag_data_received(GtkWidget
*widget
, GdkDragContext
*drag_context
,
671 gint x
, gint y
, GtkSelectionData
*data
, guint info
, guint ltime
)
673 if (info
== TARGET_TEXT_PLAIN
)
675 if (data
->format
== 8 && data
->length
> 0)
676 vf
->vte_terminal_feed_child(VTE_TERMINAL(widget
),
677 (const gchar
*) data
->data
, data
->length
);
681 gchar
*text
= (gchar
*) gtk_selection_data_get_text(data
);
683 vf
->vte_terminal_feed_child(VTE_TERMINAL(widget
), text
, strlen(text
));
686 gtk_drag_finish(drag_context
, TRUE
, FALSE
, ltime
);
690 static void check_run_in_vte_toggled(GtkToggleButton
*togglebutton
, GtkWidget
*user_data
)
692 gtk_widget_set_sensitive(user_data
, gtk_toggle_button_get_active(togglebutton
));
696 static void font_button_clicked_cb(GtkFontButton
*widget
, gpointer user_data
)
698 const gchar
*fontbtn
= gtk_font_button_get_font_name(widget
);
700 if (! utils_str_equal(fontbtn
, vc
->font
))
702 setptr(vc
->font
, g_strdup(gtk_font_button_get_font_name(widget
)));
703 vte_apply_user_settings();
708 static void on_color_button_choose_cb(GtkColorButton
*widget
, gpointer user_data
)
710 switch (GPOINTER_TO_INT(user_data
))
714 g_free(vc
->colour_fore
);
715 vc
->colour_fore
= g_new0(GdkColor
, 1);
716 gtk_color_button_get_color(widget
, vc
->colour_fore
);
721 g_free(vc
->colour_back
);
722 vc
->colour_back
= g_new0(GdkColor
, 1);
723 gtk_color_button_get_color(widget
, vc
->colour_back
);
730 void vte_append_preferences_tab(void)
732 if (vte_info
.have_vte
)
734 GtkWidget
*notebook
, *vbox
, *label
, *alignment
, *table
, *frame
, *box
;
735 GtkWidget
*font_term
, *color_fore
, *color_back
, *spin_scrollback
;
736 GtkWidget
*check_scroll_key
, *check_scroll_out
, *check_follow_path
;
737 GtkWidget
*check_enable_bash_keys
, *check_ignore_menu_key
, *check_cursor_blinks
;
738 GtkWidget
*check_run_in_vte
, *check_skip_script
, *entry_shell
, *button_shell
, *image_shell
;
739 GtkObject
*spin_scrollback_adj
;
741 notebook
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "notebook2");
743 frame
= ui_frame_new_with_alignment(_("Terminal"), &alignment
);
744 gtk_container_set_border_width(GTK_CONTAINER(frame
), 5);
745 vbox
= gtk_vbox_new(FALSE
, 12);
746 gtk_container_add(GTK_CONTAINER(alignment
), vbox
);
748 label
= gtk_label_new(_("Terminal"));
749 vte_prefs_tab_num
= gtk_notebook_append_page(GTK_NOTEBOOK(notebook
), frame
, label
);
751 table
= gtk_table_new(6, 2, FALSE
);
752 gtk_box_pack_start(GTK_BOX(vbox
), table
, FALSE
, FALSE
, 0);
753 gtk_table_set_row_spacings(GTK_TABLE(table
), 3);
754 gtk_table_set_col_spacings(GTK_TABLE(table
), 10);
756 label
= gtk_label_new(_("Font:"));
757 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 0, 1,
758 (GtkAttachOptions
) (GTK_FILL
),
759 (GtkAttachOptions
) (0), 0, 0);
760 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
762 font_term
= gtk_font_button_new();
763 gtk_table_attach(GTK_TABLE(table
), font_term
, 1, 2, 0, 1,
764 (GtkAttachOptions
) (GTK_EXPAND
| GTK_FILL
),
765 (GtkAttachOptions
) (0), 0, 0);
766 ui_widget_set_tooltip_text(font_term
, _("Sets the font for the terminal widget"));
768 label
= gtk_label_new(_("Foreground color:"));
769 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 1, 2,
770 (GtkAttachOptions
) (GTK_FILL
),
771 (GtkAttachOptions
) (0), 0, 0);
772 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
774 label
= gtk_label_new(_("Background color:"));
775 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 2, 3,
776 (GtkAttachOptions
) (GTK_FILL
),
777 (GtkAttachOptions
) (0), 0, 0);
778 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
780 color_fore
= gtk_color_button_new();
781 gtk_table_attach(GTK_TABLE(table
), color_fore
, 1, 2, 1, 2,
782 (GtkAttachOptions
) (GTK_FILL
),
783 (GtkAttachOptions
) (0), 0, 0);
784 ui_widget_set_tooltip_text(color_fore
, _("Sets the foreground color of the text in the terminal widget"));
785 gtk_color_button_set_title(GTK_COLOR_BUTTON(color_fore
), _("Color Chooser"));
787 color_back
= gtk_color_button_new();
788 gtk_table_attach(GTK_TABLE(table
), color_back
, 1, 2, 2, 3,
789 (GtkAttachOptions
) (GTK_FILL
),
790 (GtkAttachOptions
) (0), 0, 0);
791 ui_widget_set_tooltip_text(color_back
, _("Sets the background color of the text in the terminal widget"));
792 gtk_color_button_set_title(GTK_COLOR_BUTTON(color_back
), _("Color Chooser"));
794 label
= gtk_label_new(_("Scrollback lines:"));
795 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 3, 4,
796 (GtkAttachOptions
) (GTK_FILL
),
797 (GtkAttachOptions
) (0), 0, 0);
798 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
800 spin_scrollback_adj
= gtk_adjustment_new(500, 0, 5000, 1, 10, 0);
801 spin_scrollback
= gtk_spin_button_new(GTK_ADJUSTMENT(spin_scrollback_adj
), 1, 0);
802 ui_entry_add_clear_icon(GTK_ENTRY(spin_scrollback
));
803 gtk_table_attach(GTK_TABLE(table
), spin_scrollback
, 1, 2, 3, 4,
804 (GtkAttachOptions
) (GTK_EXPAND
| GTK_FILL
),
805 (GtkAttachOptions
) (0), 0, 0);
806 ui_widget_set_tooltip_text(spin_scrollback
, _("Specifies the history in lines, which you can scroll back in the terminal widget"));
807 gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(spin_scrollback
), TRUE
);
808 gtk_spin_button_set_wrap(GTK_SPIN_BUTTON(spin_scrollback
), TRUE
);
810 label
= gtk_label_new(_("Shell:"));
811 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 5, 6,
812 (GtkAttachOptions
) (GTK_FILL
),
813 (GtkAttachOptions
) (0), 0, 0);
814 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
816 entry_shell
= gtk_entry_new();
817 ui_entry_add_clear_icon(GTK_ENTRY(entry_shell
));
818 ui_widget_set_tooltip_text(entry_shell
, _("Sets the path to the shell which should be started inside the terminal emulation"));
820 button_shell
= gtk_button_new();
821 gtk_widget_show(button_shell
);
823 box
= gtk_hbox_new(FALSE
, 6);
824 gtk_box_pack_start_defaults(GTK_BOX(box
), entry_shell
);
825 gtk_box_pack_start(GTK_BOX(box
), button_shell
, FALSE
, FALSE
, 0);
826 gtk_table_attach(GTK_TABLE(table
), box
, 1, 2, 5, 6,
827 (GtkAttachOptions
) (GTK_EXPAND
| GTK_FILL
),
828 (GtkAttachOptions
) (0), 0, 0);
830 image_shell
= gtk_image_new_from_stock("gtk-open", GTK_ICON_SIZE_BUTTON
);
831 gtk_widget_show(image_shell
);
832 gtk_container_add(GTK_CONTAINER(button_shell
), image_shell
);
834 box
= gtk_vbox_new(FALSE
, 3);
835 check_scroll_key
= gtk_check_button_new_with_mnemonic(_("Scroll on keystroke"));
836 ui_widget_set_tooltip_text(check_scroll_key
, _("Whether to scroll to the bottom if a key was pressed"));
837 gtk_container_add(GTK_CONTAINER(box
), check_scroll_key
);
839 check_scroll_out
= gtk_check_button_new_with_mnemonic(_("Scroll on output"));
840 ui_widget_set_tooltip_text(check_scroll_out
, _("Whether to scroll to the bottom when output is generated"));
841 gtk_container_add(GTK_CONTAINER(box
), check_scroll_out
);
843 check_cursor_blinks
= gtk_check_button_new_with_mnemonic(_("Cursor blinks"));
844 ui_widget_set_tooltip_text(check_cursor_blinks
, _("Whether to blink the cursor"));
845 gtk_container_add(GTK_CONTAINER(box
), check_cursor_blinks
);
847 check_enable_bash_keys
= gtk_check_button_new_with_mnemonic(_("Override Geany keybindings"));
848 ui_widget_set_tooltip_text(check_enable_bash_keys
,
849 _("Allows the VTE to receive keyboard shortcuts (apart from focus commands)"));
850 gtk_container_add(GTK_CONTAINER(box
), check_enable_bash_keys
);
852 check_ignore_menu_key
= gtk_check_button_new_with_mnemonic(_("Disable menu shortcut key (F10 by default)"));
853 ui_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."));
854 gtk_container_add(GTK_CONTAINER(box
), check_ignore_menu_key
);
856 check_follow_path
= gtk_check_button_new_with_mnemonic(_("Follow the path of the current file"));
857 ui_widget_set_tooltip_text(check_follow_path
, _("Whether to execute \"cd $path\" when you switch between opened files"));
858 gtk_container_add(GTK_CONTAINER(box
), check_follow_path
);
860 /* create check_skip_script checkbox before the check_skip_script checkbox to be able to
861 * use the object for the toggled handler of check_skip_script checkbox */
862 check_skip_script
= gtk_check_button_new_with_mnemonic(_("Don't use run script"));
863 ui_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"));
864 gtk_widget_set_sensitive(check_skip_script
, vc
->run_in_vte
);
866 check_run_in_vte
= gtk_check_button_new_with_mnemonic(_("Execute programs in VTE"));
867 ui_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"));
868 gtk_container_add(GTK_CONTAINER(box
), check_run_in_vte
);
869 g_signal_connect(check_run_in_vte
, "toggled",
870 G_CALLBACK(check_run_in_vte_toggled
), check_skip_script
);
872 /* now add the check_skip_script checkbox after the check_run_in_vte checkbox */
873 gtk_container_add(GTK_CONTAINER(box
), check_skip_script
);
875 gtk_box_pack_start(GTK_BOX(vbox
), box
, FALSE
, FALSE
, 0);
877 ui_hookup_widget(ui_widgets
.prefs_dialog
, font_term
, "font_term");
878 ui_hookup_widget(ui_widgets
.prefs_dialog
, color_fore
, "color_fore");
879 ui_hookup_widget(ui_widgets
.prefs_dialog
, color_back
, "color_back");
880 ui_hookup_widget(ui_widgets
.prefs_dialog
, spin_scrollback
, "spin_scrollback");
881 ui_hookup_widget(ui_widgets
.prefs_dialog
, entry_shell
, "entry_shell");
882 ui_hookup_widget(ui_widgets
.prefs_dialog
, check_scroll_key
, "check_scroll_key");
883 ui_hookup_widget(ui_widgets
.prefs_dialog
, check_scroll_out
, "check_scroll_out");
884 ui_hookup_widget(ui_widgets
.prefs_dialog
, check_cursor_blinks
, "check_cursor_blinks");
885 ui_hookup_widget(ui_widgets
.prefs_dialog
, check_enable_bash_keys
, "check_enable_bash_keys");
886 ui_hookup_widget(ui_widgets
.prefs_dialog
, check_ignore_menu_key
, "check_ignore_menu_key");
887 ui_hookup_widget(ui_widgets
.prefs_dialog
, check_follow_path
, "check_follow_path");
888 ui_hookup_widget(ui_widgets
.prefs_dialog
, check_run_in_vte
, "check_run_in_vte");
889 ui_hookup_widget(ui_widgets
.prefs_dialog
, check_skip_script
, "check_skip_script");
891 gtk_widget_show_all(frame
);
893 g_signal_connect(font_term
, "font-set", G_CALLBACK(font_button_clicked_cb
), NULL
);
894 g_signal_connect(color_fore
, "color-set", G_CALLBACK(on_color_button_choose_cb
),
896 g_signal_connect(color_back
, "color-set", G_CALLBACK(on_color_button_choose_cb
),
898 ui_setup_open_button_callback(button_shell
, NULL
,
899 GTK_FILE_CHOOSER_ACTION_OPEN
, GTK_ENTRY(entry_shell
));
904 void vte_select_all(void)
906 if (vf
->vte_terminal_select_all
!= NULL
)
907 vf
->vte_terminal_select_all(VTE_TERMINAL(vc
->vte
));
911 void vte_send_selection_to_vte(void)
917 doc
= document_get_current();
918 g_return_if_fail(doc
!= NULL
);
920 if (sci_has_selection(doc
->editor
->sci
))
922 text
= g_malloc0(sci_get_selected_text_length(doc
->editor
->sci
) + 1);
923 sci_get_selected_text(doc
->editor
->sci
, text
);
926 { /* Get the current line */
927 gint line_num
= sci_get_current_line(doc
->editor
->sci
);
928 text
= sci_get_line(doc
->editor
->sci
, line_num
);
933 if (vc
->send_selection_unsafe
)
934 { /* Explicitly append a trailing newline character to get the command executed,
935 this is disabled by default as it could cause all sorts of damage. */
936 if (text
[len
-1] != '\n' && text
[len
-1] != '\r')
938 setptr(text
, g_strconcat(text
, "\n", NULL
));
943 { /* Make sure there is no newline character at the end to prevent unwanted execution */
944 while (text
[len
-1] == '\n' || text
[len
-1] == '\r')
951 vf
->vte_terminal_feed_child(VTE_TERMINAL(vc
->vte
), text
, len
);
954 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_VTE
);
955 gtk_widget_grab_focus(vc
->vte
);
956 msgwin_show_hide(TRUE
);