Support more folding icon styles: arrows, +/- and no lines
[geany-mirror.git] / src / vte.c
bloba52f8f1143cda08400895fece8365fe63812315b
1 /*
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.
21 * $Id$
25 * Virtual Terminal Emulation setup and handling code, using the libvte plugin library.
28 #include "geany.h"
30 #ifdef HAVE_VTE
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 */
34 #include <stdlib.h>
35 #include <sys/types.h>
36 #include <unistd.h>
38 #include <gdk/gdkkeysyms.h>
39 #include <signal.h>
40 #include <string.h>
41 #include <errno.h>
43 #include "vte.h"
44 #include "support.h"
45 #include "prefs.h"
46 #include "ui_utils.h"
47 #include "utils.h"
48 #include "document.h"
49 #include "msgwindow.h"
50 #include "callbacks.h"
51 #include "geanywraplabel.h"
52 #include "editor.h"
53 #include "sciwrappers.h"
56 VteInfo vte_info;
57 VteConfig *vc;
59 static pid_t pid = 0;
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;
73 struct _VteTerminal
75 GtkWidget widget;
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())
82 typedef enum {
83 VTE_CURSOR_BLINK_SYSTEM,
84 VTE_CURSOR_BLINK_ON,
85 VTE_CURSOR_BLINK_OFF
86 } VteTerminalCursorBlinkMode;
89 /* Holds function pointers we need to access the VTE API. */
90 struct VteFunctions
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);
120 static void create_vte(void);
121 static void vte_start(GtkWidget *widget);
122 static void vte_restart(GtkWidget *widget);
123 static gboolean vte_button_pressed(GtkWidget *widget, GdkEventButton *event, gpointer user_data);
124 static gboolean vte_keyrelease_cb(GtkWidget *widget, GdkEventKey *event, gpointer data);
125 static gboolean vte_keypress_cb(GtkWidget *widget, GdkEventKey *event, gpointer data);
126 static void vte_register_symbols(GModule *module);
127 static void vte_popup_menu_clicked(GtkMenuItem *menuitem, gpointer user_data);
128 static GtkWidget *vte_create_popup_menu(void);
129 static void vte_commit_cb(VteTerminal *vte, gchar *arg1, guint arg2, gpointer user_data);
130 static void vte_drag_data_received(GtkWidget *widget, GdkDragContext *drag_context,
131 gint x, gint y, GtkSelectionData *data, guint info, guint ltime);
134 enum
136 POPUP_COPY,
137 POPUP_PASTE,
138 POPUP_CHANGEPATH,
139 POPUP_RESTARTTERMINAL,
140 POPUP_PREFERENCES,
141 TARGET_UTF8_STRING = 0,
142 TARGET_TEXT,
143 TARGET_COMPOUND_TEXT,
144 TARGET_STRING,
145 TARGET_TEXT_PLAIN
148 static const GtkTargetEntry dnd_targets[] =
150 { "UTF8_STRING", 0, TARGET_UTF8_STRING },
151 { "TEXT", 0, TARGET_TEXT },
152 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT },
153 { "STRING", 0, TARGET_STRING },
154 { "text/plain", 0, TARGET_TEXT_PLAIN },
158 static gchar **vte_get_child_environment(void)
160 const gchar *exclude_vars[] = {"COLUMNS", "LINES", "TERM", NULL};
162 return utils_copy_environment(exclude_vars, "TERM", "xterm", NULL);
166 static void override_menu_key(void)
168 if (gtk_menu_key_accel == NULL) /* for restoring the default value */
169 g_object_get(G_OBJECT(gtk_settings_get_default()),
170 "gtk-menu-bar-accel", &gtk_menu_key_accel, NULL);
172 if (vc->ignore_menu_bar_accel)
173 gtk_settings_set_string_property(gtk_settings_get_default(),
174 "gtk-menu-bar-accel", "<Shift><Control><Mod1><Mod2><Mod3><Mod4><Mod5>F10", "Geany");
175 else
176 gtk_settings_set_string_property(gtk_settings_get_default(),
177 "gtk-menu-bar-accel", gtk_menu_key_accel, "Geany");
181 void vte_init(void)
183 if (vte_info.have_vte == FALSE)
184 { /* vte_info.have_vte can be false even if VTE is compiled in, think of command line option */
185 geany_debug("Disabling terminal support");
186 return;
189 if (NZV(vte_info.lib_vte))
191 module = g_module_open(vte_info.lib_vte, G_MODULE_BIND_LAZY);
193 #ifdef VTE_MODULE_PATH
194 else
196 module = g_module_open(VTE_MODULE_PATH, G_MODULE_BIND_LAZY);
198 #endif
200 if (module == NULL)
202 gint i;
203 const gchar *sonames[] = { "libvte.so", "libvte.so.4",
204 "libvte.so.8", "libvte.so.9", NULL };
206 for (i = 0; sonames[i] != NULL && module == NULL; i++)
208 module = g_module_open(sonames[i], G_MODULE_BIND_LAZY);
212 if (module == NULL)
214 vte_info.have_vte = FALSE;
215 geany_debug("Could not load libvte.so, embedded terminal support disabled");
216 return;
218 else
220 vte_info.have_vte = TRUE;
221 vf = g_new0(struct VteFunctions, 1);
222 vte_register_symbols(module);
225 create_vte();
227 /* setup the F10 menu override (so it works before the widget is first realised). */
228 override_menu_key();
232 static void create_vte(void)
234 GtkWidget *vte, *scrollbar, *hbox, *frame;
236 vc->vte = vte = vf->vte_terminal_new();
237 scrollbar = gtk_vscrollbar_new(GTK_ADJUSTMENT(VTE_TERMINAL(vte)->adjustment));
238 GTK_WIDGET_UNSET_FLAGS(scrollbar, GTK_CAN_FOCUS);
240 frame = gtk_frame_new(NULL);
242 hbox = gtk_hbox_new(FALSE, 0);
243 gtk_container_add(GTK_CONTAINER(frame), hbox);
244 gtk_box_pack_start(GTK_BOX(hbox), vte, TRUE, TRUE, 0);
245 gtk_box_pack_start(GTK_BOX(hbox), scrollbar, FALSE, FALSE, 0);
247 /* set the default widget size first to prevent VTE expanding too much,
248 * sometimes causing the hscrollbar to be too big or out of view. */
249 gtk_widget_set_size_request(GTK_WIDGET(vte), 10, 10);
250 vf->vte_terminal_set_size(VTE_TERMINAL(vte), 30, 1);
252 vf->vte_terminal_set_mouse_autohide(VTE_TERMINAL(vte), TRUE);
253 vf->vte_terminal_set_word_chars(VTE_TERMINAL(vte), VTE_WORDCHARS);
255 gtk_drag_dest_set(vte, GTK_DEST_DEFAULT_ALL,
256 dnd_targets, G_N_ELEMENTS(dnd_targets), GDK_ACTION_COPY);
258 g_signal_connect(vte, "child-exited", G_CALLBACK(vte_start), NULL);
259 g_signal_connect(vte, "button-press-event", G_CALLBACK(vte_button_pressed), NULL);
260 g_signal_connect(vte, "event", G_CALLBACK(vte_keypress_cb), NULL);
261 g_signal_connect(vte, "key-release-event", G_CALLBACK(vte_keyrelease_cb), NULL);
262 g_signal_connect(vte, "commit", G_CALLBACK(vte_commit_cb), NULL);
263 g_signal_connect(vte, "motion-notify-event", G_CALLBACK(on_motion_event), NULL);
264 g_signal_connect(vte, "drag-data-received", G_CALLBACK(vte_drag_data_received), NULL);
266 vte_start(vte);
268 gtk_widget_show_all(frame);
269 gtk_notebook_insert_page(GTK_NOTEBOOK(msgwindow.notebook), frame, gtk_label_new(_("Terminal")), MSG_VTE);
271 /* the vte widget has to be realised before color changes take effect */
272 g_signal_connect_after(vte, "realize", G_CALLBACK(vte_apply_user_settings), NULL);
276 void vte_close(void)
278 g_free(vf);
279 /* free the vte widget before unloading vte module
280 * this prevents a segfault on X close window if the message window is hidden */
281 gtk_widget_destroy(vc->vte);
282 if (popup_menu_created)
283 gtk_widget_destroy(vc->menu);
284 g_free(vc->emulation);
285 g_free(vc->shell);
286 g_free(vc->font);
287 g_free(vc->colour_back);
288 g_free(vc->colour_fore);
289 g_free(vc);
290 g_free(gtk_menu_key_accel);
291 /* Don't unload the module explicitly because it causes a segfault on FreeBSD. The segfault
292 * happens when the app really exits, not directly on g_module_close(). This still needs to
293 * be investigated. */
294 /*g_module_close(module); */
298 static gboolean vte_keyrelease_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
300 if (ui_is_keyval_enter_or_return(event->keyval) ||
301 ((event->keyval == GDK_c) && (event->state & GDK_CONTROL_MASK)))
303 /* assume any text on the prompt has been executed when pressing Enter/Return */
304 clean = TRUE;
306 return FALSE;
310 static gboolean vte_keypress_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
312 if (vc->enable_bash_keys)
313 return FALSE; /* Ctrl-[CD] will be handled by the VTE itself */
315 if (event->type != GDK_KEY_RELEASE)
316 return FALSE;
318 if ((event->keyval == GDK_c ||
319 event->keyval == GDK_d ||
320 event->keyval == GDK_C ||
321 event->keyval == GDK_D) &&
322 event->state & GDK_CONTROL_MASK &&
323 ! (event->state & GDK_SHIFT_MASK) && ! (event->state & GDK_MOD1_MASK))
325 vte_restart(widget);
326 return TRUE;
328 return FALSE;
332 static void vte_commit_cb(VteTerminal *vte, gchar *arg1, guint arg2, gpointer user_data)
334 clean = FALSE;
338 static void vte_start(GtkWidget *widget)
340 gchar **env;
341 gchar **argv;
343 /* split the shell command line, so arguments will work too */
344 argv = g_strsplit(vc->shell, " ", -1);
346 if (argv != NULL)
348 env = vte_get_child_environment();
349 pid = vf->vte_terminal_fork_command(VTE_TERMINAL(widget), argv[0], argv, env,
350 vte_info.dir, TRUE, TRUE, TRUE);
351 g_strfreev(env);
352 g_strfreev(argv);
354 else
355 pid = 0; /* use 0 as invalid pid */
357 clean = TRUE;
361 static void vte_restart(GtkWidget *widget)
363 vte_get_working_directory(); /* try to keep the working directory when restarting the VTE */
364 if (pid > 0)
366 kill(pid, SIGINT);
367 pid = 0;
369 vf->vte_terminal_reset(VTE_TERMINAL(widget), TRUE, TRUE);
370 clean = TRUE;
374 static gboolean vte_button_pressed(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
376 if (event->button == 3)
378 if (G_UNLIKELY(! popup_menu_created))
380 vc->menu = vte_create_popup_menu();
381 vf->vte_terminal_im_append_menuitems(VTE_TERMINAL(vc->vte), GTK_MENU_SHELL(vc->im_submenu));
382 popup_menu_created = TRUE;
385 gtk_widget_grab_focus(vc->vte);
386 gtk_menu_popup(GTK_MENU(vc->menu), NULL, NULL, NULL, NULL, event->button, event->time);
389 return FALSE;
393 static void vte_set_cursor_blink_mode(void)
395 if (vf->vte_terminal_set_cursor_blink_mode != NULL)
396 /* vte >= 0.17.1 */
397 vf->vte_terminal_set_cursor_blink_mode(VTE_TERMINAL(vc->vte),
398 (vc->cursor_blinks) ? VTE_CURSOR_BLINK_ON : VTE_CURSOR_BLINK_OFF);
399 else
400 /* vte < 0.17.1 */
401 vf->vte_terminal_set_cursor_blinks(VTE_TERMINAL(vc->vte), vc->cursor_blinks);
405 static void vte_register_symbols(GModule *mod)
407 g_module_symbol(mod, "vte_terminal_new", (void*)&vf->vte_terminal_new);
408 g_module_symbol(mod, "vte_terminal_set_size", (void*)&vf->vte_terminal_set_size);
409 g_module_symbol(mod, "vte_terminal_fork_command", (void*)&vf->vte_terminal_fork_command);
410 g_module_symbol(mod, "vte_terminal_set_word_chars", (void*)&vf->vte_terminal_set_word_chars);
411 g_module_symbol(mod, "vte_terminal_set_mouse_autohide", (void*)&vf->vte_terminal_set_mouse_autohide);
412 g_module_symbol(mod, "vte_terminal_reset", (void*)&vf->vte_terminal_reset);
413 g_module_symbol(mod, "vte_terminal_get_type", (void*)&vf->vte_terminal_get_type);
414 g_module_symbol(mod, "vte_terminal_set_scroll_on_output", (void*)&vf->vte_terminal_set_scroll_on_output);
415 g_module_symbol(mod, "vte_terminal_set_scroll_on_keystroke", (void*)&vf->vte_terminal_set_scroll_on_keystroke);
416 g_module_symbol(mod, "vte_terminal_set_font_from_string", (void*)&vf->vte_terminal_set_font_from_string);
417 g_module_symbol(mod, "vte_terminal_set_scrollback_lines", (void*)&vf->vte_terminal_set_scrollback_lines);
418 g_module_symbol(mod, "vte_terminal_get_has_selection", (void*)&vf->vte_terminal_get_has_selection);
419 g_module_symbol(mod, "vte_terminal_copy_clipboard", (void*)&vf->vte_terminal_copy_clipboard);
420 g_module_symbol(mod, "vte_terminal_paste_clipboard", (void*)&vf->vte_terminal_paste_clipboard);
421 g_module_symbol(mod, "vte_terminal_set_emulation", (void*)&vf->vte_terminal_set_emulation);
422 g_module_symbol(mod, "vte_terminal_set_color_foreground", (void*)&vf->vte_terminal_set_color_foreground);
423 g_module_symbol(mod, "vte_terminal_set_color_bold", (void*)&vf->vte_terminal_set_color_bold);
424 g_module_symbol(mod, "vte_terminal_set_color_background", (void*)&vf->vte_terminal_set_color_background);
425 g_module_symbol(mod, "vte_terminal_feed_child", (void*)&vf->vte_terminal_feed_child);
426 g_module_symbol(mod, "vte_terminal_im_append_menuitems", (void*)&vf->vte_terminal_im_append_menuitems);
427 g_module_symbol(mod, "vte_terminal_set_cursor_blink_mode", (void*)&vf->vte_terminal_set_cursor_blink_mode);
428 if (vf->vte_terminal_set_cursor_blink_mode == NULL)
429 /* vte_terminal_set_cursor_blink_mode() is only available since 0.17.1, so if we don't find
430 * this symbol, we are probably on an older version and use the old API instead */
431 g_module_symbol(mod, "vte_terminal_set_cursor_blinks", (void*)&vf->vte_terminal_set_cursor_blinks);
435 void vte_apply_user_settings(void)
437 if (! ui_prefs.msgwindow_visible)
438 return;
440 vf->vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte), vc->scrollback_lines);
441 vf->vte_terminal_set_scroll_on_keystroke(VTE_TERMINAL(vc->vte), vc->scroll_on_key);
442 vf->vte_terminal_set_scroll_on_output(VTE_TERMINAL(vc->vte), vc->scroll_on_out);
443 vf->vte_terminal_set_emulation(VTE_TERMINAL(vc->vte), vc->emulation);
444 vf->vte_terminal_set_font_from_string(VTE_TERMINAL(vc->vte), vc->font);
445 vf->vte_terminal_set_color_foreground(VTE_TERMINAL(vc->vte), vc->colour_fore);
446 vf->vte_terminal_set_color_bold(VTE_TERMINAL(vc->vte), vc->colour_fore);
447 vf->vte_terminal_set_color_background(VTE_TERMINAL(vc->vte), vc->colour_back);
448 vte_set_cursor_blink_mode();
450 override_menu_key();
454 static void vte_popup_menu_clicked(GtkMenuItem *menuitem, gpointer user_data)
456 switch (GPOINTER_TO_INT(user_data))
458 case POPUP_COPY:
460 if (vf->vte_terminal_get_has_selection(VTE_TERMINAL(vc->vte)))
461 vf->vte_terminal_copy_clipboard(VTE_TERMINAL(vc->vte));
462 break;
464 case POPUP_PASTE:
466 vf->vte_terminal_paste_clipboard(VTE_TERMINAL(vc->vte));
467 break;
469 case POPUP_CHANGEPATH:
471 GeanyDocument *doc = document_get_current();
472 if (doc != NULL)
473 vte_cwd(doc->file_name, TRUE);
474 break;
476 case POPUP_RESTARTTERMINAL:
478 vte_restart(vc->vte);
479 break;
481 case POPUP_PREFERENCES:
483 GtkWidget *notebook;
485 prefs_show_dialog();
487 notebook = ui_lookup_widget(ui_widgets.prefs_dialog, "notebook2");
489 gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), vte_prefs_tab_num);
490 break;
496 static GtkWidget *vte_create_popup_menu(void)
498 GtkWidget *menu, *item;
500 menu = gtk_menu_new();
502 item = gtk_image_menu_item_new_from_stock("gtk-copy", NULL);
503 gtk_widget_show(item);
504 gtk_container_add(GTK_CONTAINER(menu), item);
505 g_signal_connect(item, "activate", G_CALLBACK(vte_popup_menu_clicked), GINT_TO_POINTER(POPUP_COPY));
507 item = gtk_image_menu_item_new_from_stock("gtk-paste", NULL);
508 gtk_widget_show(item);
509 gtk_container_add(GTK_CONTAINER(menu), item);
510 g_signal_connect(item, "activate", G_CALLBACK(vte_popup_menu_clicked), GINT_TO_POINTER(POPUP_PASTE));
512 item = gtk_separator_menu_item_new();
513 gtk_widget_show(item);
514 gtk_container_add(GTK_CONTAINER(menu), item);
516 item = gtk_image_menu_item_new_with_mnemonic(_("_Set Path From Document"));
517 gtk_widget_show(item);
518 gtk_container_add(GTK_CONTAINER(menu), item);
519 g_signal_connect(item, "activate", G_CALLBACK(vte_popup_menu_clicked), GINT_TO_POINTER(POPUP_CHANGEPATH));
521 item = gtk_image_menu_item_new_with_mnemonic(_("_Restart Terminal"));
522 gtk_widget_show(item);
523 gtk_container_add(GTK_CONTAINER(menu), item);
524 g_signal_connect(item, "activate", G_CALLBACK(vte_popup_menu_clicked), GINT_TO_POINTER(POPUP_RESTARTTERMINAL));
526 item = gtk_separator_menu_item_new();
527 gtk_widget_show(item);
528 gtk_container_add(GTK_CONTAINER(menu), item);
530 item = gtk_image_menu_item_new_from_stock("gtk-preferences", NULL);
531 gtk_widget_show(item);
532 gtk_container_add(GTK_CONTAINER(menu), item);
533 g_signal_connect(item, "activate", G_CALLBACK(vte_popup_menu_clicked), GINT_TO_POINTER(POPUP_PREFERENCES));
535 msgwin_menu_add_common_items(GTK_MENU(menu));
537 item = gtk_separator_menu_item_new();
538 gtk_widget_show(item);
539 gtk_container_add(GTK_CONTAINER(menu), item);
541 /* the IM submenu should always be the last item to be consistent with other GTK popup menus */
542 vc->im_submenu = gtk_menu_new();
544 item = gtk_image_menu_item_new_with_mnemonic(_("_Input Methods"));
545 gtk_widget_show(item);
546 gtk_container_add(GTK_CONTAINER(menu), item);
548 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), vc->im_submenu);
550 return menu;
554 /* If the command could be executed, TRUE is returned, FALSE otherwise (i.e. there was some text
555 * on the prompt). */
556 gboolean vte_send_cmd(const gchar *cmd)
558 if (clean)
560 vf->vte_terminal_feed_child(VTE_TERMINAL(vc->vte), cmd, strlen(cmd));
561 clean = TRUE; /* vte_terminal_feed_child() also marks the vte as not clean */
562 return TRUE;
564 else
565 return FALSE;
569 /* Taken from Terminal by os-cillation: terminal_screen_get_working_directory, thanks.
570 * Determines the working directory using various OS-specific mechanisms and stores the determined
571 * directory in vte_info.dir. Note: vte_info.dir contains the real path. */
572 const gchar *vte_get_working_directory(void)
574 gchar buffer[4096 + 1];
575 gchar *file;
576 gchar *cwd;
577 gint length;
579 if (pid > 0)
581 file = g_strdup_printf("/proc/%d/cwd", pid);
582 length = readlink(file, buffer, sizeof(buffer));
584 if (length > 0 && *buffer == '/')
586 buffer[length] = '\0';
587 g_free(vte_info.dir);
588 vte_info.dir = g_strdup(buffer);
590 else if (length == 0)
592 cwd = g_get_current_dir();
593 if (cwd != NULL)
595 if (chdir(file) == 0)
597 g_free(vte_info.dir);
598 vte_info.dir = g_get_current_dir();
599 if (chdir(cwd) != 0)
600 geany_debug("%s: %s", G_STRFUNC, g_strerror(errno));
602 g_free(cwd);
605 g_free(file);
608 return vte_info.dir;
612 /* Changes the current working directory of the VTE to the path of the given filename.
613 * filename is expected to be in UTF-8 encoding.
614 * filename can also be a path, then it is used directly.
615 * If force is set to TRUE, it will always change the cwd
617 void vte_cwd(const gchar *filename, gboolean force)
619 if (vte_info.have_vte && (vc->follow_path || force) &&
620 filename != NULL && g_path_is_absolute(filename))
622 gchar *path;
624 if (g_file_test(filename, G_FILE_TEST_IS_DIR))
625 path = g_strdup(filename);
626 else
627 path = g_path_get_dirname(filename);
629 vte_get_working_directory(); /* refresh vte_info.dir */
630 if (! utils_str_equal(path, vte_info.dir))
632 /* use g_shell_quote to avoid problems with spaces, '!' or something else in path */
633 gchar *quoted_path = g_shell_quote(path);
634 gchar *cmd = g_strconcat("cd ", quoted_path, "\n", NULL);
635 if (! vte_send_cmd(cmd))
637 ui_set_statusbar(FALSE,
638 _("Could not change the directory in the VTE because it probably contains a command."));
639 geany_debug(
640 "Could not change the directory in the VTE because it probably contains a command.");
642 g_free(quoted_path);
643 g_free(cmd);
645 g_free(path);
650 static void vte_drag_data_received(GtkWidget *widget, GdkDragContext *drag_context,
651 gint x, gint y, GtkSelectionData *data, guint info, guint ltime)
653 if (info == TARGET_TEXT_PLAIN)
655 if (data->format == 8 && data->length > 0)
656 vf->vte_terminal_feed_child(VTE_TERMINAL(widget),
657 (const gchar*) data->data, data->length);
659 else
661 gchar *text = (gchar*) gtk_selection_data_get_text(data);
662 if (NZV(text))
663 vf->vte_terminal_feed_child(VTE_TERMINAL(widget), text, strlen(text));
664 g_free(text);
666 gtk_drag_finish(drag_context, TRUE, FALSE, ltime);
670 static void check_run_in_vte_toggled(GtkToggleButton *togglebutton, GtkWidget *user_data)
672 gtk_widget_set_sensitive(user_data, gtk_toggle_button_get_active(togglebutton));
676 static void font_button_clicked_cb(GtkFontButton *widget, gpointer user_data)
678 const gchar *fontbtn = gtk_font_button_get_font_name(widget);
680 if (! utils_str_equal(fontbtn, vc->font))
682 setptr(vc->font, g_strdup(gtk_font_button_get_font_name(widget)));
683 vte_apply_user_settings();
688 static void on_color_button_choose_cb(GtkColorButton *widget, gpointer user_data)
690 switch (GPOINTER_TO_INT(user_data))
692 case 1:
694 g_free(vc->colour_fore);
695 vc->colour_fore = g_new0(GdkColor, 1);
696 gtk_color_button_get_color(widget, vc->colour_fore);
697 break;
699 case 2:
701 g_free(vc->colour_back);
702 vc->colour_back = g_new0(GdkColor, 1);
703 gtk_color_button_get_color(widget, vc->colour_back);
704 break;
710 void vte_append_preferences_tab(void)
712 if (vte_info.have_vte)
714 GtkWidget *notebook, *vbox, *label, *alignment, *table, *frame, *box;
715 GtkWidget *font_term, *color_fore, *color_back, *spin_scrollback;
716 GtkWidget *check_scroll_key, *check_scroll_out, *check_follow_path;
717 GtkWidget *check_enable_bash_keys, *check_ignore_menu_key, *check_cursor_blinks;
718 GtkWidget *check_run_in_vte, *check_skip_script, *entry_shell, *button_shell, *image_shell;
719 GtkObject *spin_scrollback_adj;
721 notebook = ui_lookup_widget(ui_widgets.prefs_dialog, "notebook2");
723 frame = ui_frame_new_with_alignment(_("Terminal"), &alignment);
724 gtk_container_set_border_width(GTK_CONTAINER(frame), 5);
725 vbox = gtk_vbox_new(FALSE, 12);
726 gtk_container_add(GTK_CONTAINER(alignment), vbox);
728 label = gtk_label_new(_("Terminal"));
729 vte_prefs_tab_num = gtk_notebook_append_page(GTK_NOTEBOOK(notebook), frame, label);
731 table = gtk_table_new(6, 2, FALSE);
732 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
733 gtk_table_set_row_spacings(GTK_TABLE(table), 3);
734 gtk_table_set_col_spacings(GTK_TABLE(table), 10);
736 label = gtk_label_new(_("Font:"));
737 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1,
738 (GtkAttachOptions) (GTK_FILL),
739 (GtkAttachOptions) (0), 0, 0);
740 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
742 font_term = gtk_font_button_new();
743 gtk_table_attach(GTK_TABLE(table), font_term, 1, 2, 0, 1,
744 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
745 (GtkAttachOptions) (0), 0, 0);
746 ui_widget_set_tooltip_text(font_term, _("Sets the font for the terminal widget"));
748 label = gtk_label_new(_("Foreground color:"));
749 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2,
750 (GtkAttachOptions) (GTK_FILL),
751 (GtkAttachOptions) (0), 0, 0);
752 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
754 label = gtk_label_new(_("Background color:"));
755 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 2, 3,
756 (GtkAttachOptions) (GTK_FILL),
757 (GtkAttachOptions) (0), 0, 0);
758 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
760 color_fore = gtk_color_button_new();
761 gtk_table_attach(GTK_TABLE(table), color_fore, 1, 2, 1, 2,
762 (GtkAttachOptions) (GTK_FILL),
763 (GtkAttachOptions) (0), 0, 0);
764 ui_widget_set_tooltip_text(color_fore, _("Sets the foreground color of the text in the terminal widget"));
765 gtk_color_button_set_title(GTK_COLOR_BUTTON(color_fore), _("Color Chooser"));
767 color_back = gtk_color_button_new();
768 gtk_table_attach(GTK_TABLE(table), color_back, 1, 2, 2, 3,
769 (GtkAttachOptions) (GTK_FILL),
770 (GtkAttachOptions) (0), 0, 0);
771 ui_widget_set_tooltip_text(color_back, _("Sets the background color of the text in the terminal widget"));
772 gtk_color_button_set_title(GTK_COLOR_BUTTON(color_back), _("Color Chooser"));
774 label = gtk_label_new(_("Scrollback lines:"));
775 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 3, 4,
776 (GtkAttachOptions) (GTK_FILL),
777 (GtkAttachOptions) (0), 0, 0);
778 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
780 spin_scrollback_adj = gtk_adjustment_new(500, 0, 5000, 1, 10, 0);
781 spin_scrollback = gtk_spin_button_new(GTK_ADJUSTMENT(spin_scrollback_adj), 1, 0);
782 ui_entry_add_clear_icon(GTK_ENTRY(spin_scrollback));
783 gtk_table_attach(GTK_TABLE(table), spin_scrollback, 1, 2, 3, 4,
784 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
785 (GtkAttachOptions) (0), 0, 0);
786 ui_widget_set_tooltip_text(spin_scrollback, _("Specifies the history in lines, which you can scroll back in the terminal widget"));
787 gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(spin_scrollback), TRUE);
788 gtk_spin_button_set_wrap(GTK_SPIN_BUTTON(spin_scrollback), TRUE);
790 label = gtk_label_new(_("Shell:"));
791 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 5, 6,
792 (GtkAttachOptions) (GTK_FILL),
793 (GtkAttachOptions) (0), 0, 0);
794 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
796 entry_shell = gtk_entry_new();
797 ui_entry_add_clear_icon(GTK_ENTRY(entry_shell));
798 ui_widget_set_tooltip_text(entry_shell, _("Sets the path to the shell which should be started inside the terminal emulation"));
800 button_shell = gtk_button_new();
801 gtk_widget_show(button_shell);
803 box = gtk_hbox_new(FALSE, 6);
804 gtk_box_pack_start_defaults(GTK_BOX(box), entry_shell);
805 gtk_box_pack_start(GTK_BOX(box), button_shell, FALSE, FALSE, 0);
806 gtk_table_attach(GTK_TABLE(table), box, 1, 2, 5, 6,
807 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
808 (GtkAttachOptions) (0), 0, 0);
810 image_shell = gtk_image_new_from_stock("gtk-open", GTK_ICON_SIZE_BUTTON);
811 gtk_widget_show(image_shell);
812 gtk_container_add(GTK_CONTAINER(button_shell), image_shell);
814 box = gtk_vbox_new(FALSE, 3);
815 check_scroll_key = gtk_check_button_new_with_mnemonic(_("Scroll on keystroke"));
816 ui_widget_set_tooltip_text(check_scroll_key, _("Whether to scroll to the bottom if a key was pressed"));
817 gtk_container_add(GTK_CONTAINER(box), check_scroll_key);
819 check_scroll_out = gtk_check_button_new_with_mnemonic(_("Scroll on output"));
820 ui_widget_set_tooltip_text(check_scroll_out, _("Whether to scroll to the bottom when output is generated"));
821 gtk_container_add(GTK_CONTAINER(box), check_scroll_out);
823 check_cursor_blinks = gtk_check_button_new_with_mnemonic(_("Cursor blinks"));
824 ui_widget_set_tooltip_text(check_cursor_blinks, _("Whether to blink the cursor"));
825 gtk_container_add(GTK_CONTAINER(box), check_cursor_blinks);
827 check_enable_bash_keys = gtk_check_button_new_with_mnemonic(_("Override Geany keybindings"));
828 ui_widget_set_tooltip_text(check_enable_bash_keys,
829 _("Allows the VTE to receive keyboard shortcuts (apart from focus commands)"));
830 gtk_container_add(GTK_CONTAINER(box), check_enable_bash_keys);
832 check_ignore_menu_key = gtk_check_button_new_with_mnemonic(_("Disable menu shortcut key (F10 by default)"));
833 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."));
834 gtk_container_add(GTK_CONTAINER(box), check_ignore_menu_key);
836 check_follow_path = gtk_check_button_new_with_mnemonic(_("Follow the path of the current file"));
837 ui_widget_set_tooltip_text(check_follow_path, _("Whether to execute \"cd $path\" when you switch between opened files"));
838 gtk_container_add(GTK_CONTAINER(box), check_follow_path);
840 /* create check_skip_script checkbox before the check_skip_script checkbox to be able to
841 * use the object for the toggled handler of check_skip_script checkbox */
842 check_skip_script = gtk_check_button_new_with_mnemonic(_("Don't use run script"));
843 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"));
844 gtk_widget_set_sensitive(check_skip_script, vc->run_in_vte);
846 check_run_in_vte = gtk_check_button_new_with_mnemonic(_("Execute programs in VTE"));
847 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"));
848 gtk_container_add(GTK_CONTAINER(box), check_run_in_vte);
849 g_signal_connect(check_run_in_vte, "toggled",
850 G_CALLBACK(check_run_in_vte_toggled), check_skip_script);
852 /* now add the check_skip_script checkbox after the check_run_in_vte checkbox */
853 gtk_container_add(GTK_CONTAINER(box), check_skip_script);
855 gtk_box_pack_start(GTK_BOX(vbox), box, FALSE, FALSE, 0);
857 g_object_set_data_full(G_OBJECT(ui_widgets.prefs_dialog), "font_term",
858 g_object_ref(font_term), (GDestroyNotify) g_object_unref);
859 g_object_set_data_full(G_OBJECT(ui_widgets.prefs_dialog), "color_fore",
860 g_object_ref(color_fore), (GDestroyNotify) g_object_unref);
861 g_object_set_data_full(G_OBJECT(ui_widgets.prefs_dialog), "color_back",
862 g_object_ref(color_back), (GDestroyNotify) g_object_unref);
863 g_object_set_data_full(G_OBJECT(ui_widgets.prefs_dialog), "spin_scrollback",
864 g_object_ref(spin_scrollback), (GDestroyNotify) g_object_unref);
865 g_object_set_data_full(G_OBJECT(ui_widgets.prefs_dialog), "entry_shell",
866 g_object_ref(entry_shell), (GDestroyNotify) g_object_unref);
867 g_object_set_data_full(G_OBJECT(ui_widgets.prefs_dialog), "check_scroll_key",
868 g_object_ref(check_scroll_key), (GDestroyNotify) g_object_unref);
869 g_object_set_data_full(G_OBJECT(ui_widgets.prefs_dialog), "check_scroll_out",
870 g_object_ref(check_scroll_out), (GDestroyNotify) g_object_unref);
871 g_object_set_data_full(G_OBJECT(ui_widgets.prefs_dialog), "check_cursor_blinks",
872 g_object_ref(check_cursor_blinks), (GDestroyNotify) g_object_unref);
873 g_object_set_data_full(G_OBJECT(ui_widgets.prefs_dialog), "check_enable_bash_keys",
874 g_object_ref(check_enable_bash_keys), (GDestroyNotify) g_object_unref);
875 g_object_set_data_full(G_OBJECT(ui_widgets.prefs_dialog), "check_ignore_menu_key",
876 g_object_ref(check_ignore_menu_key), (GDestroyNotify) g_object_unref);
877 g_object_set_data_full(G_OBJECT(ui_widgets.prefs_dialog), "check_follow_path",
878 g_object_ref(check_follow_path), (GDestroyNotify) g_object_unref);
879 g_object_set_data_full(G_OBJECT(ui_widgets.prefs_dialog), "check_run_in_vte",
880 g_object_ref(check_run_in_vte), (GDestroyNotify) g_object_unref);
881 g_object_set_data_full(G_OBJECT(ui_widgets.prefs_dialog), "check_skip_script",
882 g_object_ref(check_skip_script), (GDestroyNotify) g_object_unref);
884 gtk_widget_show_all(frame);
886 g_signal_connect(font_term, "font-set", G_CALLBACK(font_button_clicked_cb), NULL);
887 g_signal_connect(color_fore, "color-set", G_CALLBACK(on_color_button_choose_cb),
888 GINT_TO_POINTER(1));
889 g_signal_connect(color_back, "color-set", G_CALLBACK(on_color_button_choose_cb),
890 GINT_TO_POINTER(2));
891 ui_setup_open_button_callback(button_shell, NULL,
892 GTK_FILE_CHOOSER_ACTION_OPEN, GTK_ENTRY(entry_shell));
897 void vte_send_selection_to_vte(void)
899 GeanyDocument *doc;
900 gchar *text;
901 gsize len;
903 doc = document_get_current();
904 g_return_if_fail(doc != NULL);
906 if (sci_has_selection(doc->editor->sci))
908 text = g_malloc0(sci_get_selected_text_length(doc->editor->sci) + 1);
909 sci_get_selected_text(doc->editor->sci, text);
911 else
912 { /* Get the current line */
913 gint line_num = sci_get_current_line(doc->editor->sci);
914 text = sci_get_line(doc->editor->sci, line_num);
917 len = strlen(text);
919 if (vc->send_selection_unsafe)
920 { /* Explicitly append a trailing newline character to get the command executed,
921 this is disabled by default as it could cause all sorts of damage. */
922 if (text[len-1] != '\n' && text[len-1] != '\r')
924 setptr(text, g_strconcat(text, "\n", NULL));
925 len++;
928 else
929 { /* Make sure there is no newline character at the end to prevent unwanted execution */
930 while (text[len-1] == '\n' || text[len-1] == '\r')
932 text[len-1] = '\0';
933 len--;
937 vf->vte_terminal_feed_child(VTE_TERMINAL(vc->vte), text, len);
939 /* show the VTE */
940 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_VTE);
941 gtk_widget_grab_focus(vc->vte);
942 msgwin_show_hide(TRUE);
944 g_free(text);
948 #endif