termite/termite.cc

1001 lines
35 KiB
C++
Raw Normal View History

2012-07-20 17:00:49 +02:00
#include <algorithm>
2012-08-15 02:19:19 +02:00
#include <cstdlib>
2012-07-20 16:56:09 +02:00
#include <cstring>
2012-09-09 22:00:19 +02:00
#include <functional>
#include <limits>
2012-05-30 10:22:09 +02:00
#include <gdk/gdkx.h>
2012-05-22 06:34:35 +02:00
#include <gtk/gtk.h>
#include <vte/vte.h>
2012-07-20 10:54:25 +02:00
#include <vte/vteaccess.h>
2012-05-22 06:34:35 +02:00
2012-07-20 17:32:15 +02:00
#include "url_regex.h"
2012-07-06 04:12:32 +02:00
enum class overlay_mode {
2012-07-20 17:19:42 +02:00
hidden,
search,
rsearch,
completion
2012-07-20 17:01:57 +02:00
};
2012-07-20 17:19:42 +02:00
enum class vi_mode {
insert,
command,
visual,
visual_line,
visual_block
2012-07-20 17:01:57 +02:00
};
2012-07-05 08:56:28 +02:00
2012-07-20 17:01:57 +02:00
struct select_info {
2012-07-20 17:19:42 +02:00
vi_mode mode;
long begin_col;
long begin_row;
2012-09-08 04:41:41 +02:00
long origin_col;
long origin_row;
2012-07-20 17:01:57 +02:00
};
2012-07-20 17:01:57 +02:00
struct search_panel_info {
2012-07-17 00:20:00 +02:00
VteTerminal *vte;
GtkWidget *entry;
2012-06-20 17:25:04 +02:00
GtkWidget *panel;
2012-07-20 17:56:07 +02:00
overlay_mode mode;
2012-07-20 17:01:57 +02:00
};
2012-05-30 10:22:09 +02:00
2012-07-20 17:01:57 +02:00
struct config_info {
gboolean dynamic_title, urgent_on_bell, clickable_url;
int tag;
2012-07-20 17:01:57 +02:00
};
2012-07-20 17:01:57 +02:00
struct keybind_info {
search_panel_info panel;
select_info select;
config_info config;
2012-07-20 17:01:57 +02:00
};
2012-06-16 06:48:16 +02:00
static char *browser_cmd[3] = {NULL};
2012-05-31 23:16:01 +02:00
2012-06-08 22:04:48 +02:00
static void launch_browser(char *url);
static void window_title_cb(VteTerminal *vte, gboolean *dynamic_title);
static gboolean key_press_cb(VteTerminal *vte, GdkEventKey *event, keybind_info *info);
2012-06-08 22:04:48 +02:00
static gboolean entry_key_press_cb(GtkEntry *entry, GdkEventKey *event, search_panel_info *info);
static gboolean position_overlay_cb(GtkBin *overlay, GtkWidget *widget, GdkRectangle *alloc);
static gboolean button_press_cb(VteTerminal *vte, GdkEventButton *event, gboolean *clickable_url);
static void beep_cb(GtkWidget *vte, gboolean *urgent_on_bell);
static gboolean focus_cb(GtkWindow *window);
2012-06-08 22:04:48 +02:00
static gboolean add_to_list_store(char *key, void *, GtkListStore *store);
2012-06-08 22:04:48 +02:00
static GtkTreeModel *create_completion_model(VteTerminal *vte);
static void search(VteTerminal *vte, const char *pattern, bool reverse);
static void overlay_show(search_panel_info *info, overlay_mode mode, bool complete);
static void get_vte_padding(VteTerminal *vte, int *w, int *h);
static char *check_match(VteTerminal *vte, int event_x, int event_y);
static void load_config(GtkWindow *window, VteTerminal *vte, config_info *info,
const char **term, char **geometry);
2012-06-08 22:04:48 +02:00
void launch_browser(char *url) {
browser_cmd[1] = url;
g_spawn_async(NULL, browser_cmd, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL);
}
static void update_selection(VteTerminal *vte, const select_info *select) {
vte_terminal_select_none(vte);
2012-07-20 17:19:42 +02:00
if (select->mode == vi_mode::command) {
2012-09-08 01:00:00 +02:00
return;
2012-07-05 08:56:28 +02:00
}
const long n_columns = vte_terminal_get_column_count(vte);
long cursor_col, cursor_row;
vte_terminal_get_cursor_position(vte, &cursor_col, &cursor_row);
2012-07-20 17:19:42 +02:00
vte_terminal_set_selection_block_mode(vte, select->mode == vi_mode::visual_block);
2012-07-20 17:19:42 +02:00
if (select->mode == vi_mode::visual) {
2012-07-08 07:46:44 +02:00
const long begin = select->begin_row * n_columns + select->begin_col;
const long end = cursor_row * n_columns + cursor_col;
2012-07-07 02:38:46 +02:00
if (begin < end) {
vte_terminal_select_text(vte, select->begin_col, select->begin_row,
cursor_col, cursor_row);
2012-07-07 02:38:46 +02:00
} else {
vte_terminal_select_text(vte, cursor_col, cursor_row,
select->begin_col, select->begin_row);
2012-07-07 02:38:46 +02:00
}
2012-07-20 17:19:42 +02:00
} else if (select->mode == vi_mode::visual_line) {
2012-07-08 07:15:00 +02:00
vte_terminal_select_text(vte, 0,
std::min(select->begin_row, cursor_row),
2012-07-08 07:15:00 +02:00
n_columns - 1,
std::max(select->begin_row, cursor_row));
2012-07-20 17:19:42 +02:00
} else if (select->mode == vi_mode::visual_block) {
2012-07-08 07:12:26 +02:00
vte_terminal_select_text(vte,
std::min(select->begin_col, cursor_col),
std::min(select->begin_row, cursor_row),
std::max(select->begin_col, cursor_col),
std::max(select->begin_row, cursor_row));
}
vte_terminal_copy_primary(vte);
}
static void start_selection(VteTerminal *vte, select_info *select) {
2012-08-27 06:46:52 +02:00
vte_terminal_disconnect_pty_read(vte);
2012-07-20 17:19:42 +02:00
select->mode = vi_mode::command;
2012-09-08 04:41:41 +02:00
vte_terminal_get_cursor_position(vte, &select->origin_col, &select->origin_row);
update_selection(vte, select);
}
static void end_selection(VteTerminal *vte, select_info *select) {
2012-09-08 04:41:41 +02:00
vte_terminal_set_cursor_position(vte, select->origin_col, select->origin_row);
2012-08-27 06:46:52 +02:00
vte_terminal_connect_pty_read(vte);
vte_terminal_select_none(vte);
2012-07-20 17:19:42 +02:00
select->mode = vi_mode::insert;
}
2012-07-20 17:19:42 +02:00
static void toggle_visual(VteTerminal *vte, select_info *select, vi_mode mode) {
2012-07-07 02:38:46 +02:00
if (select->mode == mode) {
2012-07-20 17:19:42 +02:00
select->mode = vi_mode::command;
2012-07-05 08:56:28 +02:00
} else {
2012-07-20 17:19:42 +02:00
if (select->mode == vi_mode::command) {
vte_terminal_get_cursor_position(vte, &select->begin_col, &select->begin_row);
}
2012-07-07 02:38:46 +02:00
select->mode = mode;
2012-07-05 08:56:28 +02:00
}
update_selection(vte, select);
2012-07-05 08:56:28 +02:00
}
static long first_row(VteTerminal *vte) {
GtkAdjustment *adjust = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(vte));
2012-08-15 02:06:30 +02:00
return (long)gtk_adjustment_get_lower(adjust);
}
static long last_row(VteTerminal *vte) {
GtkAdjustment *adjust = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(vte));
2012-08-15 02:06:30 +02:00
return (long)gtk_adjustment_get_upper(adjust) - 1;
}
2012-08-30 10:28:15 +02:00
static void update_scroll(VteTerminal *vte) {
GtkAdjustment *adjust = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(vte));
2012-07-10 06:02:55 +02:00
const double scroll_row = gtk_adjustment_get_value(adjust);
const long n_rows = vte_terminal_get_row_count(vte);
2012-09-08 06:12:06 +02:00
long cursor_row;
vte_terminal_get_cursor_position(vte, nullptr, &cursor_row);
if (cursor_row < scroll_row) {
gtk_adjustment_set_value(adjust, (double)cursor_row);
} else if (cursor_row - n_rows >= (long)scroll_row) {
gtk_adjustment_set_value(adjust, (double)(cursor_row - n_rows + 1));
}
}
static void move(VteTerminal *vte, select_info *select, long col, long row) {
const long end_col = vte_terminal_get_column_count(vte) - 1;
long cursor_col, cursor_row;
vte_terminal_get_cursor_position(vte, &cursor_col, &cursor_row);
vte_terminal_set_cursor_position(vte,
CLAMP(cursor_col + col, 0, end_col),
CLAMP(cursor_row + row, first_row(vte), last_row(vte)));
2012-08-30 10:28:15 +02:00
update_scroll(vte);
update_selection(vte, select);
}
static void move_to_row_start(VteTerminal *vte, select_info *select, long row) {
vte_terminal_set_cursor_position(vte, 0, row);
2012-08-30 10:28:15 +02:00
update_scroll(vte);
update_selection(vte, select);
}
2012-07-20 10:54:25 +02:00
static void open_selection(VteTerminal *vte) {
2012-07-20 11:09:46 +02:00
if (browser_cmd[0]) {
AtkText *text = ATK_TEXT(vte_terminal_accessible_new(vte));
char *selection = atk_text_get_selection(text, 0, NULL, NULL);
2012-08-19 21:07:07 +02:00
if (selection && selection[0]) {
launch_browser(selection);
}
2012-07-20 11:09:46 +02:00
g_free(selection);
} else {
g_printerr("no browser to open url");
}
2012-07-20 10:54:25 +02:00
}
template<typename F>
static void move_backward(VteTerminal *vte, select_info *select, F is_word) {
long cursor_col, cursor_row;
vte_terminal_get_cursor_position(vte, &cursor_col, &cursor_row);
char *content = vte_terminal_get_text_range(vte, cursor_row, 0,
cursor_row, cursor_col,
2012-08-14 22:28:58 +02:00
NULL, NULL, NULL);
if (!content) {
return;
}
long length;
2012-08-14 22:28:58 +02:00
gunichar *codepoints = g_utf8_to_ucs4(content, -1, NULL, &length, NULL);
if (!codepoints) {
return;
}
bool in_word = false;
for (long i = length - 2; i > 0; i--) {
if (!is_word(codepoints[i - 1])) {
2012-08-14 22:28:58 +02:00
if (in_word) {
break;
}
} else {
in_word = true;
}
cursor_col--;
2012-08-14 22:28:58 +02:00
}
vte_terminal_set_cursor_position(vte, cursor_col, cursor_row);
2012-08-14 22:28:58 +02:00
update_selection(vte, select);
g_free(codepoints);
g_free(content);
}
static void move_backward_word(VteTerminal *vte, select_info *select) {
move_backward(vte, select, [vte](gunichar c) {
return !vte_terminal_is_word_char(vte, c);
});
}
2012-09-08 07:10:29 +02:00
static void move_backward_blank_word(VteTerminal *vte, select_info *select) {
move_backward(vte, select, std::not1(std::ref(g_unichar_isspace)));
2012-09-08 07:10:29 +02:00
}
template<typename F>
2012-09-09 22:50:17 +02:00
static void move_forward(VteTerminal *vte, select_info *select, F is_word) {
long cursor_col, cursor_row;
vte_terminal_get_cursor_position(vte, &cursor_col, &cursor_row);
2012-08-14 22:28:58 +02:00
const long end_col = vte_terminal_get_column_count(vte) - 1;
char *content = vte_terminal_get_text_range(vte, cursor_row, cursor_col,
cursor_row, end_col,
2012-08-14 22:28:58 +02:00
NULL, NULL, NULL);
if (!content) {
return;
}
long length;
gunichar *codepoints = g_utf8_to_ucs4(content, -1, NULL, &length, NULL);
2012-08-14 22:28:58 +02:00
if (!codepoints) {
return;
}
// prevent going past the end (get_text_range adds a \n)
if (codepoints[length - 1] == '\n') {
2012-09-09 22:50:17 +02:00
codepoints[--length] = 0;
}
2012-08-14 22:28:58 +02:00
bool end_of_word = false;
for (long i = 1; i < length; i++) {
if (is_word(codepoints[i - 1])) {
2012-08-14 22:28:58 +02:00
if (end_of_word) {
break;
}
} else {
end_of_word = true;
}
cursor_col++;
2012-08-14 22:28:58 +02:00
}
vte_terminal_set_cursor_position(vte, cursor_col, cursor_row);
2012-08-14 22:28:58 +02:00
update_selection(vte, select);
g_free(codepoints);
g_free(content);
}
static void move_forward_word(VteTerminal *vte, select_info *select) {
move_forward(vte, select, [vte](gunichar c) {
return vte_terminal_is_word_char(vte, c);
});
}
2012-09-08 07:10:29 +02:00
static void move_forward_blank_word(VteTerminal *vte, select_info *select) {
2012-09-09 22:00:19 +02:00
move_forward(vte, select, std::not1(std::ref(g_unichar_isspace)));
2012-09-08 07:10:29 +02:00
}
static void set_cursor_column(VteTerminal *vte, const select_info *select, long column) {
long cursor_row;
vte_terminal_get_cursor_position(vte, nullptr, &cursor_row);
vte_terminal_set_cursor_position(vte, column, cursor_row);
update_selection(vte, select);
}
/* {{{ CALLBACKS */
void window_title_cb(VteTerminal *vte, gboolean *dynamic_title) {
const char * const title = *dynamic_title ? vte_terminal_get_window_title(vte) : NULL;
gtk_window_set_title(GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(vte))),
title ? title : "termite");
}
gboolean key_press_cb(VteTerminal *vte, GdkEventKey *event, keybind_info *info) {
2012-06-08 22:04:48 +02:00
const guint modifiers = event->state & gtk_accelerator_get_default_mod_mask();
2012-07-20 17:19:42 +02:00
if (info->select.mode != vi_mode::insert) {
2012-07-07 04:46:00 +02:00
if (modifiers == GDK_CONTROL_MASK) {
if (gdk_keyval_to_lower(event->keyval) == GDK_KEY_v) {
2012-07-20 17:19:42 +02:00
toggle_visual(vte, &info->select, vi_mode::visual_block);
2012-07-07 04:46:00 +02:00
}
return TRUE;
}
switch (event->keyval) {
2012-07-05 20:34:23 +02:00
case GDK_KEY_Left:
case GDK_KEY_h:
move(vte, &info->select, -1, 0);
break;
case GDK_KEY_Down:
case GDK_KEY_j:
move(vte, &info->select, 0, 1);
break;
case GDK_KEY_Up:
case GDK_KEY_k:
move(vte, &info->select, 0, -1);
break;
2012-07-05 20:34:23 +02:00
case GDK_KEY_Right:
case GDK_KEY_l:
move(vte, &info->select, 1, 0);
break;
2012-08-14 22:28:58 +02:00
case GDK_KEY_b:
move_backward_word(vte, &info->select);
break;
2012-09-08 07:10:29 +02:00
case GDK_KEY_B:
move_backward_blank_word(vte, &info->select);
break;
2012-08-14 22:28:58 +02:00
case GDK_KEY_w:
move_forward_word(vte, &info->select);
break;
2012-09-08 07:10:29 +02:00
case GDK_KEY_W:
move_forward_blank_word(vte, &info->select);
break;
case GDK_KEY_asciicircum:
set_cursor_column(vte, &info->select, 0);
break;
case GDK_KEY_dollar:
set_cursor_column(vte, &info->select, vte_terminal_get_column_count(vte) - 1);
break;
case GDK_KEY_g:
move_to_row_start(vte, &info->select, first_row(vte));
break;
case GDK_KEY_G:
move_to_row_start(vte, &info->select, last_row(vte));
break;
2012-07-05 08:56:28 +02:00
case GDK_KEY_v:
2012-07-20 17:19:42 +02:00
toggle_visual(vte, &info->select, vi_mode::visual);
2012-07-07 02:38:46 +02:00
break;
case GDK_KEY_V:
2012-07-20 17:19:42 +02:00
toggle_visual(vte, &info->select, vi_mode::visual_line);
2012-07-05 08:56:28 +02:00
break;
case GDK_KEY_Escape:
end_selection(vte, &info->select);
break;
2012-07-11 08:06:40 +02:00
case GDK_KEY_y:
vte_terminal_copy_clipboard(vte);
break;
2012-07-11 09:35:38 +02:00
case GDK_KEY_slash:
overlay_show(&info->panel, overlay_mode::search, true);
2012-07-11 09:35:38 +02:00
break;
case GDK_KEY_question:
overlay_show(&info->panel, overlay_mode::rsearch, true);
2012-07-11 09:35:38 +02:00
break;
case GDK_KEY_n:
vte_terminal_search_find_next(vte);
vte_terminal_copy_primary(vte);
break;
case GDK_KEY_N:
vte_terminal_search_find_previous(vte);
vte_terminal_copy_primary(vte);
break;
case GDK_KEY_u:
search(vte, url_regex, false);
break;
case GDK_KEY_U:
search(vte, url_regex, true);
break;
case GDK_KEY_o:
open_selection(vte);
break;
2012-07-20 10:54:25 +02:00
case GDK_KEY_Return:
open_selection(vte);
end_selection(vte, &info->select);
2012-07-20 10:54:25 +02:00
break;
}
return TRUE;
}
2012-06-08 22:04:48 +02:00
if (modifiers == (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) {
switch (gdk_keyval_to_lower(event->keyval)) {
case GDK_KEY_space:
start_selection(vte, &info->select);
return TRUE;
2012-06-08 22:16:41 +02:00
case GDK_KEY_c:
2012-06-08 22:04:48 +02:00
vte_terminal_copy_clipboard(vte);
return TRUE;
2012-06-08 22:16:41 +02:00
case GDK_KEY_v:
2012-06-08 22:04:48 +02:00
vte_terminal_paste_clipboard(vte);
return TRUE;
case GDK_KEY_Escape:
load_config(GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(vte))),
vte, &info->config, NULL, NULL);
return TRUE;
2012-06-08 22:04:48 +02:00
}
} else if (modifiers == GDK_CONTROL_MASK && event->keyval == GDK_KEY_Tab) {
overlay_show(&info->panel, overlay_mode::completion, true);
2012-06-08 22:04:48 +02:00
return TRUE;
}
return FALSE;
}
static void synthesize_keypress(GtkWidget *widget, unsigned keyval) {
GdkEvent new_event;
new_event.key.type = GDK_KEY_PRESS;
new_event.key.window = gtk_widget_get_parent_window(widget);
new_event.key.send_event = TRUE;
new_event.key.time = GDK_CURRENT_TIME;
new_event.key.keyval = keyval;
new_event.key.state = GDK_KEY_PRESS_MASK;
new_event.key.length = 0;
2012-08-27 08:00:14 +02:00
new_event.key.string = nullptr;
new_event.key.hardware_keycode = 0;
new_event.key.group = 0;
gdk_event_put(&new_event);
}
2012-06-08 22:04:48 +02:00
gboolean entry_key_press_cb(GtkEntry *entry, GdkEventKey *event, search_panel_info *info) {
gboolean ret = FALSE;
if (event->keyval == GDK_KEY_Escape) {
ret = TRUE;
} else if (event->keyval == GDK_KEY_Return) {
2012-06-16 06:48:16 +02:00
const char *text = gtk_entry_get_text(entry);
2012-06-08 22:04:48 +02:00
switch (info->mode) {
case overlay_mode::search:
2012-07-17 00:20:00 +02:00
search(info->vte, text, false);
2012-06-08 22:04:48 +02:00
break;
case overlay_mode::rsearch:
2012-07-17 00:20:00 +02:00
search(info->vte, text, true);
2012-06-08 22:04:48 +02:00
break;
case overlay_mode::completion:
2012-07-17 00:20:00 +02:00
vte_terminal_feed_child(info->vte, text, -1);
2012-06-08 22:04:48 +02:00
break;
case overlay_mode::hidden:
2012-06-08 22:04:48 +02:00
break;
}
ret = TRUE;
} else if (info->mode == overlay_mode::completion) {
if (event->keyval == GDK_KEY_Tab) {
synthesize_keypress(GTK_WIDGET(entry), GDK_KEY_Down);
return TRUE;
} else if (event->keyval == GDK_KEY_ISO_Left_Tab) {
synthesize_keypress(GTK_WIDGET(entry), GDK_KEY_Up);
return TRUE;
} else if (event->keyval == GDK_KEY_Down) {
// this stops the down key from leaving the GtkEntry...
event->hardware_keycode = 0;
}
2012-06-08 22:04:48 +02:00
}
if (ret) {
info->mode = overlay_mode::hidden;
2012-06-20 17:25:04 +02:00
gtk_widget_hide(info->panel);
2012-07-17 00:20:00 +02:00
gtk_widget_grab_focus(GTK_WIDGET(info->vte));
2012-06-08 22:04:48 +02:00
}
return ret;
}
gboolean position_overlay_cb(GtkBin *overlay, GtkWidget *widget, GdkRectangle *alloc) {
GtkWidget *vte = gtk_bin_get_child(overlay);
2012-07-08 07:46:44 +02:00
const int width = gtk_widget_get_allocated_width(vte);
const int height = gtk_widget_get_allocated_height(vte);
2012-06-08 22:04:48 +02:00
GtkRequisition req;
gtk_widget_get_preferred_size(widget, NULL, &req);
alloc->x = width - req.width - 40;
alloc->y = 0;
2012-07-20 17:00:49 +02:00
alloc->width = std::min(width, req.width);
alloc->height = std::min(height, req.height);
2012-06-08 22:04:48 +02:00
return TRUE;
}
gboolean button_press_cb(VteTerminal *vte, GdkEventButton *event, gboolean *clickable_url) {
if (*clickable_url) {
char *match = check_match(vte, (int)event->x, (int)event->y);
if (event->button == 1 && event->type == GDK_BUTTON_PRESS && match) {
launch_browser(match);
g_free(match);
return TRUE;
}
2012-06-08 22:04:48 +02:00
}
return FALSE;
}
void beep_cb(GtkWidget *vte, gboolean *urgent_on_bell) {
if (*urgent_on_bell) {
gtk_window_set_urgency_hint(GTK_WINDOW(gtk_widget_get_toplevel(vte)), TRUE);
}
2012-06-08 22:04:48 +02:00
}
gboolean focus_cb(GtkWindow *window) {
gtk_window_set_urgency_hint(window, FALSE);
return FALSE;
}
2012-06-08 22:04:48 +02:00
/* }}} */
gboolean add_to_list_store(char *key, void *, GtkListStore *store) {
GtkTreeIter iter;
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, 0, key, -1);
return FALSE;
}
2012-06-08 22:04:48 +02:00
GtkTreeModel *create_completion_model(VteTerminal *vte) {
2012-06-06 01:39:44 +02:00
GtkListStore *store = gtk_list_store_new(1, G_TYPE_STRING);
2012-06-16 06:48:16 +02:00
long end_row, end_col;
2012-06-07 03:22:26 +02:00
vte_terminal_get_cursor_position(vte, &end_col, &end_row);
2012-06-16 06:48:16 +02:00
char *content = vte_terminal_get_text_range(vte, 0, 0, end_row, end_col,
NULL, NULL, NULL);
if (!content) {
2012-06-07 17:42:17 +02:00
g_printerr("no content returned for completion\n");
2012-06-07 03:26:22 +02:00
return GTK_TREE_MODEL(store);
}
char *s_ptr = content, *saveptr;
GTree *tree = g_tree_new((GCompareFunc)strcmp);
2012-06-07 04:06:46 +02:00
for (; ; s_ptr = NULL) {
char *token = strtok_r(s_ptr, " \n\t", &saveptr);
if (!token) {
break;
}
g_tree_insert(tree, token, NULL);
}
g_tree_foreach(tree, (GTraverseFunc)add_to_list_store, store);
g_tree_destroy(tree);
2012-06-07 01:17:39 +02:00
g_free(content);
return GTK_TREE_MODEL(store);
}
2012-06-08 22:04:48 +02:00
void search(VteTerminal *vte, const char *pattern, bool reverse) {
2012-05-30 10:22:09 +02:00
GRegex *regex = vte_terminal_search_get_gregex(vte);
if (regex) g_regex_unref(regex);
regex = g_regex_new(pattern, (GRegexCompileFlags)0, (GRegexMatchFlags)0, NULL);
2012-05-30 10:22:09 +02:00
vte_terminal_search_set_gregex(vte, regex);
if (!reverse) {
2012-05-30 10:22:09 +02:00
vte_terminal_search_find_next(vte);
} else {
vte_terminal_search_find_previous(vte);
}
2012-05-30 11:27:27 +02:00
vte_terminal_copy_primary(vte);
2012-05-30 10:22:09 +02:00
}
2012-06-08 22:04:48 +02:00
void overlay_show(search_panel_info *info, overlay_mode mode, bool complete) {
2012-06-04 05:37:15 +02:00
if (complete) {
GtkEntryCompletion *completion = gtk_entry_completion_new();
gtk_entry_set_completion(GTK_ENTRY(info->entry), completion);
g_object_unref(completion);
2012-07-17 00:20:00 +02:00
GtkTreeModel *completion_model = create_completion_model(info->vte);
2012-06-04 05:37:15 +02:00
gtk_entry_completion_set_model(completion, completion_model);
g_object_unref(completion_model);
2012-06-06 01:28:55 +02:00
gtk_entry_completion_set_inline_selection(completion, TRUE);
2012-06-04 05:37:15 +02:00
gtk_entry_completion_set_text_column(completion, 0);
}
gtk_entry_set_text(GTK_ENTRY(info->entry), "");
2012-06-04 05:37:15 +02:00
info->mode = mode;
2012-06-20 17:25:04 +02:00
gtk_widget_show(info->panel);
2012-06-04 05:37:15 +02:00
gtk_widget_grab_focus(info->entry);
}
2012-06-08 22:04:48 +02:00
void get_vte_padding(VteTerminal *vte, int *w, int *h) {
GtkBorder *border = NULL;
gtk_widget_style_get(GTK_WIDGET(vte), "inner-border", &border, NULL);
2012-05-31 11:32:51 +02:00
if (!border) {
g_warning("VTE's inner-border property unavailable");
*w = *h = 0;
} else {
*w = border->left + border->right;
*h = border->top + border->bottom;
gtk_border_free(border);
}
}
2012-06-08 22:04:48 +02:00
char *check_match(VteTerminal *vte, int event_x, int event_y) {
int xpad, ypad, tag;
get_vte_padding(vte, &xpad, &ypad);
2012-05-22 13:17:27 +02:00
return vte_terminal_match_check(vte,
(event_x - ypad) / vte_terminal_get_char_width(vte),
(event_y - ypad) / vte_terminal_get_char_height(vte),
&tag);
}
2012-06-08 22:04:48 +02:00
/* {{{ CONFIG LOADING */
2012-06-07 23:56:17 +02:00
#define MAKE_GET_CONFIG_FUNCTION(NAME, TYPE) \
static bool get_config_ ## NAME (GKeyFile *config, const char *group, const char *key, TYPE *value) { \
GError *error = NULL; \
*value = g_key_file_get_ ## NAME (config, group, key, &error); \
if (error) { \
g_error_free(error); \
return false; \
} \
return true; \
}
MAKE_GET_CONFIG_FUNCTION(boolean, gboolean)
2012-06-16 06:48:16 +02:00
MAKE_GET_CONFIG_FUNCTION(integer, int)
MAKE_GET_CONFIG_FUNCTION(string, char *)
MAKE_GET_CONFIG_FUNCTION(double, double)
static bool get_config_color(GKeyFile *config, const char *key, GdkColor *color) {
char *cfgstr;
bool success = false;
if (get_config_string(config, "colors", key, &cfgstr)) {
if (gdk_color_parse(cfgstr, color)) {
success = true;
} else {
g_printerr("invalid color string: %s\n", cfgstr);
}
g_free(cfgstr);
}
return success;
}
static void load_config(GtkWindow *window, VteTerminal *vte, config_info *info,
const char **term, char **geometry) {
2012-07-17 01:11:16 +02:00
const char * const filename = "termite.cfg";
2012-09-07 22:41:20 +02:00
char *path = g_build_filename(g_get_user_config_dir(), filename, nullptr);
2012-06-07 18:22:24 +02:00
GKeyFile *config = g_key_file_new();
2012-06-08 02:08:34 +02:00
if ((g_key_file_load_from_file(config, path, G_KEY_FILE_NONE, NULL) ||
2012-08-15 02:02:11 +02:00
g_key_file_load_from_dirs(config, filename,
const_cast<const char **>(g_get_system_config_dirs()),
2012-06-08 02:08:34 +02:00
NULL, G_KEY_FILE_NONE, NULL))) {
2012-06-07 23:56:17 +02:00
gboolean cfgbool;
2012-06-16 06:48:16 +02:00
double cfgdouble;
int cfgint;
char *cfgstr;
2012-06-07 23:56:17 +02:00
if (geometry && get_config_string(config, "options", "geometry", &cfgstr)) {
*geometry = cfgstr;
}
if (term && get_config_string(config, "options", "term", &cfgstr)) {
*term = cfgstr;
}
2012-06-08 18:44:08 +02:00
if (get_config_boolean(config, "options", "resize_grip", &cfgbool)) {
2012-06-07 23:56:17 +02:00
gtk_window_set_has_resize_grip(window, cfgbool);
2012-06-08 18:44:08 +02:00
}
if (get_config_boolean(config, "options", "scroll_on_output", &cfgbool)) {
2012-06-07 23:56:17 +02:00
vte_terminal_set_scroll_on_output(vte, cfgbool);
2012-06-08 18:44:08 +02:00
}
if (get_config_boolean(config, "options", "scroll_on_keystroke", &cfgbool)) {
2012-06-07 23:56:17 +02:00
vte_terminal_set_scroll_on_keystroke(vte, cfgbool);
2012-06-08 18:44:08 +02:00
}
if (get_config_boolean(config, "options", "audible_bell", &cfgbool)) {
2012-06-07 23:56:17 +02:00
vte_terminal_set_audible_bell(vte, cfgbool);
2012-06-08 18:44:08 +02:00
}
if (get_config_boolean(config, "options", "visible_bell", &cfgbool)) {
2012-06-07 23:56:17 +02:00
vte_terminal_set_visible_bell(vte, cfgbool);
2012-06-08 18:44:08 +02:00
}
if (get_config_boolean(config, "options", "mouse_autohide", &cfgbool)) {
2012-06-07 23:56:17 +02:00
vte_terminal_set_mouse_autohide(vte, cfgbool);
2012-06-08 18:44:08 +02:00
}
if (get_config_boolean(config, "options", "allow_bold", &cfgbool)) {
vte_terminal_set_allow_bold(vte, cfgbool);
2012-06-08 18:44:08 +02:00
}
if (get_config_boolean(config, "options", "dynamic_title", &cfgbool)) {
info->dynamic_title = cfgbool;
2012-06-08 18:44:08 +02:00
}
if (get_config_boolean(config, "options", "urgent_on_bell", &cfgbool)) {
info->urgent_on_bell = cfgbool;
2012-06-08 18:44:08 +02:00
}
if (get_config_boolean(config, "options", "search_wrap", &cfgbool)) {
vte_terminal_search_set_wrap_around(vte, cfgbool);
}
if (get_config_boolean(config, "options", "clickable_url", &cfgbool)) {
info->clickable_url = cfgbool;
}
if (info->clickable_url) {
info->tag =
vte_terminal_match_add_gregex(vte,
g_regex_new(url_regex,
G_REGEX_CASELESS,
G_REGEX_MATCH_NOTEMPTY,
NULL),
(GRegexMatchFlags)0);
vte_terminal_match_set_cursor_type(vte, info->tag, GDK_HAND2);
} else if (info->tag != -1) {
vte_terminal_match_remove(vte, info->tag);
info->tag = -1;
}
2012-06-11 20:32:44 +02:00
g_free(browser_cmd[0]);
if (get_config_string(config, "options", "browser", &cfgstr)) {
browser_cmd[0] = cfgstr;
} else {
browser_cmd[0] = g_strdup(g_getenv("BROWSER"));
if (!browser_cmd[0]) info->clickable_url = false;
}
2012-06-07 23:56:17 +02:00
if (get_config_string(config, "options", "font", &cfgstr)) {
vte_terminal_set_font_from_string(vte, cfgstr);
g_free(cfgstr);
2012-06-07 18:55:08 +02:00
}
2012-06-07 18:58:07 +02:00
2012-07-27 10:58:50 +02:00
if (get_config_string(config, "options", "word_chars", &cfgstr)) {
vte_terminal_set_word_chars(vte, cfgstr);
g_free(cfgstr);
}
2012-06-07 23:56:17 +02:00
if (get_config_integer(config, "options", "scrollback_lines", &cfgint)) {
vte_terminal_set_scrollback_lines(vte, cfgint);
2012-06-07 18:58:07 +02:00
}
2012-06-07 23:56:17 +02:00
if (get_config_string(config, "options", "cursor_blink", &cfgstr)) {
2012-06-08 20:52:17 +02:00
if (!g_ascii_strcasecmp(cfgstr, "system")) {
vte_terminal_set_cursor_blink_mode(vte, VTE_CURSOR_BLINK_SYSTEM);
2012-06-08 20:52:17 +02:00
} else if (!g_ascii_strcasecmp(cfgstr, "on")) {
vte_terminal_set_cursor_blink_mode(vte, VTE_CURSOR_BLINK_ON);
2012-06-08 20:52:17 +02:00
} else if (!g_ascii_strcasecmp(cfgstr, "off")) {
vte_terminal_set_cursor_blink_mode(vte, VTE_CURSOR_BLINK_OFF);
}
2012-06-07 23:56:17 +02:00
g_free(cfgstr);
}
2012-06-07 23:56:17 +02:00
if (get_config_string(config, "options", "cursor_shape", &cfgstr)) {
2012-06-08 20:52:17 +02:00
if (!g_ascii_strcasecmp(cfgstr, "block")) {
vte_terminal_set_cursor_shape(vte, VTE_CURSOR_SHAPE_BLOCK);
2012-06-08 20:52:17 +02:00
} else if (!g_ascii_strcasecmp(cfgstr, "ibeam")) {
vte_terminal_set_cursor_shape(vte, VTE_CURSOR_SHAPE_IBEAM);
2012-06-08 20:52:17 +02:00
} else if (!g_ascii_strcasecmp(cfgstr, "underline")) {
vte_terminal_set_cursor_shape(vte, VTE_CURSOR_SHAPE_UNDERLINE);
}
2012-06-07 23:56:17 +02:00
g_free(cfgstr);
}
2012-06-07 20:10:16 +02:00
2012-06-07 23:56:17 +02:00
if (get_config_string(config, "options", "icon_name", &cfgstr)) {
gtk_window_set_icon_name(window, cfgstr);
g_free(cfgstr);
2012-06-07 20:10:16 +02:00
}
2012-06-07 20:36:28 +02:00
if (get_config_double(config, "options", "transparency", &cfgdouble)) {
2012-06-19 22:11:48 +02:00
vte_terminal_set_background_saturation(vte, cfgdouble);
vte_terminal_set_opacity(vte, (guint16)(0xffff * (1 - cfgdouble)));
}
2012-07-17 01:11:16 +02:00
const long palette_size = 255;
2012-06-25 05:15:50 +02:00
GdkColor color, palette[palette_size];
2012-06-07 21:07:42 +02:00
char color_key[] = "color000";
2012-06-07 21:22:28 +02:00
2012-09-08 03:11:29 +02:00
for (unsigned i = 0; i < palette_size; i++) {
snprintf(color_key, sizeof color_key, "color%u", i);
2012-09-08 03:11:29 +02:00
if (!get_config_color(config, color_key, &palette[i])) {
if (i < 16) {
palette[i].blue = (i & 4) ? 0xc000 : 0;
palette[i].green = (i & 2) ? 0xc000 : 0;
palette[i].red = (i & 1) ? 0xc000 : 0;
if (i > 7) {
2012-06-27 03:30:34 +02:00
palette[i].blue = (guint16)(palette[i].blue + 0x3fff);
palette[i].green = (guint16)(palette[i].green + 0x3fff);
palette[i].red = (guint16)(palette[i].red + 0x3fff);
}
2012-06-25 22:08:53 +02:00
} else if (i < 232) {
2012-07-08 07:46:44 +02:00
const unsigned j = i - 16;
const unsigned r = j / 36, g = (j / 6) % 6, b = j % 6;
const unsigned red = (r == 0) ? 0 : r * 40 + 55;
const unsigned green = (g == 0) ? 0 : g * 40 + 55;
const unsigned blue = (b == 0) ? 0 : b * 40 + 55;
2012-06-27 03:30:34 +02:00
palette[i].red = (guint16)(red | red << 8);
palette[i].green = (guint16)(green | green << 8);
palette[i].blue = (guint16)(blue | blue << 8);
} else if (i < 256) {
2012-07-08 07:46:44 +02:00
const unsigned shade = 8 + (i - 232) * 10;
2012-06-27 03:30:34 +02:00
palette[i].red = palette[i].green = palette[i].blue = (guint16)(shade | shade << 8);
}
2012-06-07 21:22:28 +02:00
}
}
2012-09-08 03:11:29 +02:00
vte_terminal_set_colors(vte, nullptr, nullptr, palette, palette_size);
if (get_config_color(config, "foreground", &color)) {
vte_terminal_set_color_foreground(vte, &color);
}
if (get_config_color(config, "foreground_bold", &color)) {
vte_terminal_set_color_bold(vte, &color);
}
if (get_config_color(config, "foreground_dim", &color)) {
vte_terminal_set_color_dim(vte, &color);
}
if (get_config_color(config, "background", &color)) {
vte_terminal_set_color_background(vte, &color);
}
if (get_config_color(config, "cursor", &color)) {
vte_terminal_set_color_cursor(vte, &color);
2012-06-07 20:39:02 +02:00
}
2012-09-08 08:59:32 +02:00
if (get_config_color(config, "highlight", &color)) {
vte_terminal_set_color_highlight(vte, &color);
}
2012-06-07 18:22:24 +02:00
}
g_free(path);
2012-06-07 18:22:24 +02:00
g_key_file_free(config);
2012-06-08 22:04:48 +02:00
}/*}}}*/
2012-06-07 18:22:24 +02:00
2012-07-30 17:46:49 +02:00
static void exit_with_status(VteTerminal *vte) {
gtk_main_quit();
exit(vte_terminal_get_child_exit_status(vte));
}
2012-05-22 06:34:35 +02:00
int main(int argc, char **argv) {
GError *error = NULL;
const char *term = "xterm-termite";
2012-06-20 00:55:56 +02:00
gboolean version = FALSE;
2012-05-22 06:34:35 +02:00
2012-06-16 06:31:50 +02:00
GOptionContext *context = g_option_context_new(NULL);
2012-06-16 06:33:19 +02:00
char *role = NULL, *geometry = NULL, *execute = NULL;
const GOptionEntry entries[] = {
2012-06-11 20:26:51 +02:00
{"role", 'r', 0, G_OPTION_ARG_STRING, &role, "The role to use", "ROLE"},
2012-06-12 05:50:45 +02:00
{"geometry", 0, 0, G_OPTION_ARG_STRING, &geometry, "Window geometry", "GEOMETRY"},
2012-06-16 06:31:50 +02:00
{"exec", 'e', 0, G_OPTION_ARG_STRING, &execute, "Command to execute", "COMMAND"},
2012-06-20 01:01:27 +02:00
{"version", 'v', 0, G_OPTION_ARG_NONE, &version, "Version info", NULL},
2012-07-20 17:41:44 +02:00
{}
};
g_option_context_add_main_entries(context, entries, NULL);
2012-05-31 15:17:08 +02:00
g_option_context_add_group(context, gtk_get_option_group(TRUE));
2012-05-22 06:34:35 +02:00
if (!g_option_context_parse(context, &argc, &argv, &error)) {
2012-06-01 21:53:23 +02:00
g_printerr("option parsing failed: %s\n", error->message);
2012-08-15 02:34:11 +02:00
return EXIT_FAILURE;
}
2012-05-22 06:34:35 +02:00
2012-06-20 00:54:38 +02:00
if (version) {
g_print("termite %s\n", TERMITE_VERSION);
2012-08-15 02:34:11 +02:00
return EXIT_SUCCESS;
2012-06-20 00:54:38 +02:00
}
2012-05-22 17:50:01 +02:00
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2012-06-08 22:04:48 +02:00
GtkWidget *overlay = gtk_overlay_new();
2012-07-17 03:47:48 +02:00
GtkWidget *vte_widget = vte_terminal_new();
VteTerminal *vte = VTE_TERMINAL(vte_widget);
2012-05-30 12:54:10 +02:00
2012-06-11 22:38:01 +02:00
GdkScreen *screen = gtk_widget_get_screen(window);
GdkVisual *visual = gdk_screen_get_rgba_visual(screen);
if (!visual) {
visual = gdk_screen_get_system_visual(screen);
}
gtk_widget_set_visual(window, visual);
if (role) {
gtk_window_set_role(GTK_WINDOW(window), role);
2012-06-11 06:03:19 +02:00
g_free(role);
}
char **command_argv;
char *default_argv[2] = {NULL, NULL};
2012-06-16 06:31:50 +02:00
if (execute) {
2012-06-16 06:48:16 +02:00
int argcp;
char **argvp;
2012-06-16 06:31:50 +02:00
g_shell_parse_argv(execute, &argcp, &argvp, &error);
if (error) {
g_printerr("Failed to parse command: %s\n", error->message);
2012-08-15 02:34:11 +02:00
return EXIT_FAILURE;
2012-06-16 06:31:50 +02:00
}
command_argv = argvp;
} else {
default_argv[0] = vte_terminal_get_user_shell_with_fallback();
command_argv = default_argv;
}
2012-05-22 06:34:35 +02:00
2012-07-17 03:47:48 +02:00
VtePty *pty = vte_terminal_pty_new(vte, VTE_PTY_DEFAULT, &error);
2012-05-22 06:34:35 +02:00
if (!pty) {
2012-06-01 21:53:23 +02:00
g_printerr("Failed to create pty: %s\n", error->message);
2012-08-15 02:34:11 +02:00
return EXIT_FAILURE;
2012-05-22 06:34:35 +02:00
}
2012-07-17 03:47:48 +02:00
search_panel_info panel = {vte, gtk_entry_new(),
gtk_alignment_new(0, 0, 1, 1),
overlay_mode::hidden};
keybind_info info = {panel, {vi_mode::insert, 0, 0, 0, 0}, {FALSE, FALSE, FALSE, -1}};
load_config(GTK_WINDOW(window), vte, &info.config, &term, &geometry);
2012-07-17 03:47:48 +02:00
vte_terminal_set_pty_object(vte, pty);
2012-05-22 06:34:35 +02:00
vte_pty_set_term(pty, term);
gtk_alignment_set_padding(GTK_ALIGNMENT(panel.panel), 5, 5, 5, 5);
gtk_overlay_add_overlay(GTK_OVERLAY(overlay), panel.panel);
gtk_widget_set_halign(panel.entry, GTK_ALIGN_START);
gtk_widget_set_valign(panel.entry, GTK_ALIGN_END);
2012-05-22 06:34:35 +02:00
gtk_container_add(GTK_CONTAINER(panel.panel), panel.entry);
2012-07-17 03:47:48 +02:00
gtk_container_add(GTK_CONTAINER(overlay), vte_widget);
gtk_container_add(GTK_CONTAINER(window), overlay);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
2012-07-30 17:46:49 +02:00
g_signal_connect(vte, "child-exited", G_CALLBACK(exit_with_status), NULL);
g_signal_connect(vte, "key-press-event", G_CALLBACK(key_press_cb), &info);
2012-07-20 04:51:50 +02:00
g_signal_connect(panel.entry, "key-press-event", G_CALLBACK(entry_key_press_cb), &info.panel);
g_signal_connect(overlay, "get-child-position", G_CALLBACK(position_overlay_cb), NULL);
g_signal_connect(vte, "button-press-event", G_CALLBACK(button_press_cb), &info.config.clickable_url);
g_signal_connect(vte, "beep", G_CALLBACK(beep_cb), &info.config.urgent_on_bell);
g_signal_connect(window, "focus-in-event", G_CALLBACK(focus_cb), NULL);
g_signal_connect(window, "focus-out-event", G_CALLBACK(focus_cb), NULL);
g_signal_connect(vte, "window-title-changed", G_CALLBACK(window_title_cb),
&info.config.dynamic_title);
2012-07-17 03:47:48 +02:00
window_title_cb(vte, &info.config.dynamic_title);
2012-05-22 14:22:31 +02:00
2012-06-12 05:50:45 +02:00
if (geometry) {
gtk_widget_show_all(overlay);
gtk_widget_show_all(panel.panel);
2012-06-12 05:50:45 +02:00
if (!gtk_window_parse_geometry(GTK_WINDOW(window), geometry)) {
g_printerr("Invalid geometry string: %s\n", geometry);
}
g_free(geometry);
}
2012-07-17 03:47:48 +02:00
gtk_widget_grab_focus(vte_widget);
2012-05-22 06:34:35 +02:00
gtk_widget_show_all(window);
gtk_widget_hide(panel.panel);
GdkWindow *gdk_window = gtk_widget_get_window(window);
if (!gdk_window) {
g_printerr("no window");
2012-08-15 02:34:11 +02:00
return EXIT_FAILURE;
}
char xid_s[std::numeric_limits<long unsigned>::digits10 + 1];
snprintf(xid_s, sizeof xid_s, "%lu", GDK_WINDOW_XID(gdk_window));
char **env = g_get_environ();
env = g_environ_setenv(env, "WINDOWID", xid_s, TRUE);
env = g_environ_setenv(env, "TERM", term, TRUE);
GPid ppid;
if (g_spawn_async(NULL, command_argv, env,
(GSpawnFlags)(G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH),
(GSpawnChildSetupFunc)vte_pty_child_setup, pty,
&ppid, &error)) {
2012-07-17 03:47:48 +02:00
vte_terminal_watch_child(vte, ppid);
} else {
g_printerr("The new terminal's command failed to run: %s\n", error->message);
2012-08-27 02:29:32 +02:00
return EXIT_FAILURE;
}
int width, height, padding_w, padding_h;
long char_width = vte_terminal_get_char_width(vte);
long char_height = vte_terminal_get_char_height(vte);
gtk_window_get_size(GTK_WINDOW(window), &width, &height);
get_vte_padding(vte, &padding_w, &padding_h);
vte_terminal_set_size(vte,
(width - padding_w) / char_width,
(height - padding_h) / char_height);
g_strfreev(env);
2012-05-22 06:34:35 +02:00
gtk_main();
2012-07-30 17:46:49 +02:00
return EXIT_FAILURE; // child process did not exit
2012-05-22 06:34:35 +02:00
}
2012-06-08 18:56:46 +02:00
// vim: et:sts=4:sw=4:cino=(0