This repository has been archived by the owner on May 8, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 240
/
termite.cc
1856 lines (1608 loc) · 66.6 KB
/
termite.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2013 Daniel Micay
*
* This is free software; you can redistribute it and/or modify it under
* the terms of the GNU Library General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <algorithm>
#include <array>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <functional>
#include <limits>
#include <map>
#include <memory>
#include <vector>
#include <set>
#include <string>
#include <gtk/gtk.h>
#include <vte/vte.h>
#define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h>
#ifdef GDK_WINDOWING_X11
#include <gdk/gdkx.h>
#endif
#include "url_regex.hh"
#include "util/clamp.hh"
#include "util/maybe.hh"
#include "util/memory.hh"
using namespace std::placeholders;
/* Allow scales a bit smaller and a bit larger than the usual pango ranges */
#define TERMINAL_SCALE_XXX_SMALL (PANGO_SCALE_XX_SMALL/1.2)
#define TERMINAL_SCALE_XXXX_SMALL (TERMINAL_SCALE_XXX_SMALL/1.2)
#define TERMINAL_SCALE_XXXXX_SMALL (TERMINAL_SCALE_XXXX_SMALL/1.2)
#define TERMINAL_SCALE_XXX_LARGE (PANGO_SCALE_XX_LARGE*1.2)
#define TERMINAL_SCALE_XXXX_LARGE (TERMINAL_SCALE_XXX_LARGE*1.2)
#define TERMINAL_SCALE_XXXXX_LARGE (TERMINAL_SCALE_XXXX_LARGE*1.2)
#define TERMINAL_SCALE_MINIMUM (TERMINAL_SCALE_XXXXX_SMALL/1.2)
#define TERMINAL_SCALE_MAXIMUM (TERMINAL_SCALE_XXXXX_LARGE*1.2)
static const std::vector<double> zoom_factors = {
TERMINAL_SCALE_MINIMUM,
TERMINAL_SCALE_XXXXX_SMALL,
TERMINAL_SCALE_XXXX_SMALL,
TERMINAL_SCALE_XXX_SMALL,
PANGO_SCALE_XX_SMALL,
PANGO_SCALE_X_SMALL,
PANGO_SCALE_SMALL,
PANGO_SCALE_MEDIUM,
PANGO_SCALE_LARGE,
PANGO_SCALE_X_LARGE,
PANGO_SCALE_XX_LARGE,
TERMINAL_SCALE_XXX_LARGE,
TERMINAL_SCALE_XXXX_LARGE,
TERMINAL_SCALE_XXXXX_LARGE,
TERMINAL_SCALE_MAXIMUM
};
enum class overlay_mode {
hidden,
search,
rsearch,
completion,
urlselect
};
enum class vi_mode {
insert,
command,
visual,
visual_line,
visual_block
};
struct select_info {
vi_mode mode;
long begin_col;
long begin_row;
long origin_col;
long origin_row;
};
struct url_data {
url_data(char *u, long c, long r) : url(u, g_free), col(c), row(r) {}
std::unique_ptr<char, decltype(&g_free)> url;
long col, row;
};
struct search_panel_info {
GtkWidget *entry;
GtkWidget *da;
overlay_mode mode;
std::vector<url_data> url_list;
char *fulltext;
};
struct hint_info {
PangoFontDescription *font;
cairo_pattern_t *fg, *bg, *af, *ab, *border;
double padding, border_width, roundness;
};
struct config_info {
hint_info hints;
char *browser;
gboolean dynamic_title, urgent_on_bell, clickable_url, size_hints;
gboolean filter_unmatched_urls, modify_other_keys;
gboolean fullscreen;
int tag;
char *config_file;
gdouble font_scale;
};
struct keybind_info {
GtkWindow *window;
VteTerminal *vte;
search_panel_info panel;
select_info select;
config_info config;
std::function<void (GtkWindow *)> fullscreen_toggle;
};
struct draw_cb_info {
VteTerminal *vte;
search_panel_info *panel;
hint_info *hints;
gboolean filter_unmatched_urls;
};
static void launch_browser(char *browser, char *url);
static void window_title_cb(VteTerminal *vte, gboolean *dynamic_title);
static gboolean window_state_cb(GtkWindow *window, GdkEventWindowState *event, keybind_info *info);
static gboolean key_press_cb(VteTerminal *vte, GdkEventKey *event, keybind_info *info);
static gboolean entry_key_press_cb(GtkEntry *entry, GdkEventKey *event, keybind_info *info);
static gboolean position_overlay_cb(GtkBin *overlay, GtkWidget *widget, GdkRectangle *alloc);
static gboolean button_press_cb(VteTerminal *vte, GdkEventButton *event, const config_info *info);
static void bell_cb(GtkWidget *vte, gboolean *urgent_on_bell);
static gboolean focus_cb(GtkWindow *window);
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, VteTerminal *vte);
static void get_vte_padding(VteTerminal *vte, int *left, int *top, int *right, int *bottom);
static char *check_match(VteTerminal *vte, GdkEventButton *event);
static void load_config(GtkWindow *window, VteTerminal *vte, GtkWidget *scrollbar, GtkWidget *hbox,
config_info *info, char **icon, bool *show_scrollbar);
static void set_config(GtkWindow *window, VteTerminal *vte, GtkWidget *scrollbar, GtkWidget *hbox,
config_info *info, char **icon, bool *show_scrollbar,
GKeyFile *config);
static long first_row(VteTerminal *vte);
static std::function<void ()> reload_config;
static void override_background_color(GtkWidget *widget, GdkRGBA *rgba) {
GtkCssProvider *provider = gtk_css_provider_new();
gchar *colorstr = gdk_rgba_to_string(rgba);
char *css = g_strdup_printf("* { background-color: %s; }", colorstr);
gtk_css_provider_load_from_data(provider, css, -1, nullptr);
g_free(colorstr);
g_free(css);
gtk_style_context_add_provider(gtk_widget_get_style_context(widget),
GTK_STYLE_PROVIDER(provider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
g_object_unref(provider);
}
static const std::map<int, const char *> modify_table = {
{ GDK_KEY_Tab, "\033[27;5;9~" },
{ GDK_KEY_Return, "\033[27;5;13~" },
{ GDK_KEY_apostrophe, "\033[27;5;39~" },
{ GDK_KEY_comma, "\033[27;5;44~" },
{ GDK_KEY_minus, "\033[27;5;45~" },
{ GDK_KEY_period, "\033[27;5;46~" },
{ GDK_KEY_0, "\033[27;5;48~" },
{ GDK_KEY_1, "\033[27;5;49~" },
{ GDK_KEY_9, "\033[27;5;57~" },
{ GDK_KEY_semicolon, "\033[27;5;59~" },
{ GDK_KEY_equal, "\033[27;5;61~" },
{ GDK_KEY_exclam, "\033[27;6;33~" },
{ GDK_KEY_quotedbl, "\033[27;6;34~" },
{ GDK_KEY_numbersign, "\033[27;6;35~" },
{ GDK_KEY_dollar, "\033[27;6;36~" },
{ GDK_KEY_percent, "\033[27;6;37~" },
{ GDK_KEY_ampersand, "\033[27;6;38~" },
{ GDK_KEY_parenleft, "\033[27;6;40~" },
{ GDK_KEY_parenright, "\033[27;6;41~" },
{ GDK_KEY_asterisk, "\033[27;6;42~" },
{ GDK_KEY_plus, "\033[27;6;43~" },
{ GDK_KEY_colon, "\033[27;6;58~" },
{ GDK_KEY_less, "\033[27;6;60~" },
{ GDK_KEY_greater, "\033[27;6;62~" },
{ GDK_KEY_question, "\033[27;6;63~" },
};
static const std::map<int, const char *> modify_meta_table = {
{ GDK_KEY_Tab, "\033[27;13;9~" },
{ GDK_KEY_Return, "\033[27;13;13~" },
{ GDK_KEY_apostrophe, "\033[27;13;39~" },
{ GDK_KEY_comma, "\033[27;13;44~" },
{ GDK_KEY_minus, "\033[27;13;45~" },
{ GDK_KEY_period, "\033[27;13;46~" },
{ GDK_KEY_0, "\033[27;13;48~" },
{ GDK_KEY_1, "\033[27;13;49~" },
{ GDK_KEY_9, "\033[27;13;57~" },
{ GDK_KEY_semicolon, "\033[27;13;59~" },
{ GDK_KEY_equal, "\033[27;13;61~" },
{ GDK_KEY_exclam, "\033[27;14;33~" },
{ GDK_KEY_quotedbl, "\033[27;14;34~" },
{ GDK_KEY_numbersign, "\033[27;14;35~" },
{ GDK_KEY_dollar, "\033[27;14;36~" },
{ GDK_KEY_percent, "\033[27;14;37~" },
{ GDK_KEY_ampersand, "\033[27;14;38~" },
{ GDK_KEY_parenleft, "\033[27;14;40~" },
{ GDK_KEY_parenright, "\033[27;14;41~" },
{ GDK_KEY_asterisk, "\033[27;14;42~" },
{ GDK_KEY_plus, "\033[27;14;43~" },
{ GDK_KEY_colon, "\033[27;14;58~" },
{ GDK_KEY_less, "\033[27;14;60~" },
{ GDK_KEY_greater, "\033[27;14;62~" },
{ GDK_KEY_question, "\033[27;14;63~" },
};
static gboolean modify_key_feed(GdkEventKey *event, keybind_info *info,
const std::map<int, const char *>& table) {
if (info->config.modify_other_keys) {
unsigned int keyval = gdk_keyval_to_lower(event->keyval);
auto entry = table.find((int)keyval);
if (entry != table.end()) {
vte_terminal_feed_child(info->vte, entry->second, -1);
return TRUE;
}
}
return FALSE;
}
void launch_browser(char *browser, char *url) {
char *browser_cmd[3] = {browser, url, nullptr};
GError *error = nullptr;
if (!browser) {
g_printerr("browser not set, can't open url\n");
return;
}
GPid child_pid;
if (!g_spawn_async(nullptr, browser_cmd, nullptr, G_SPAWN_SEARCH_PATH,
nullptr, nullptr, &child_pid, &error)) {
g_printerr("error launching '%s': %s\n", browser, error->message);
g_error_free(error);
}
g_spawn_close_pid(child_pid);
}
static void set_size_hints(GtkWindow *window, VteTerminal *vte) {
static const GdkWindowHints wh = (GdkWindowHints)(GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE |
GDK_HINT_BASE_SIZE);
const int char_width = (int)vte_terminal_get_char_width(vte);
const int char_height = (int)vte_terminal_get_char_height(vte);
int padding_left, padding_top, padding_right, padding_bottom;
get_vte_padding(vte, &padding_left, &padding_top, &padding_right, &padding_bottom);
GdkGeometry hints;
hints.base_width = char_width + padding_left + padding_right;
hints.base_height = char_height + padding_top + padding_bottom;
hints.min_width = hints.base_width;
hints.min_height = hints.base_height;
hints.width_inc = char_width;
hints.height_inc = char_height;
gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &hints, wh);
}
static void launch_in_directory(VteTerminal *vte) {
const char *uri = vte_terminal_get_current_directory_uri(vte);
if (!uri) {
g_printerr("no directory uri set\n");
return;
}
auto dir = make_unique(g_filename_from_uri(uri, nullptr, nullptr), g_free);
char term[] = "termite"; // maybe this should be argv[0]
char *cmd[] = {term, nullptr};
g_spawn_async(dir.get(), cmd, nullptr, G_SPAWN_SEARCH_PATH, nullptr, nullptr, nullptr, nullptr);
}
static void find_urls(VteTerminal *vte, search_panel_info *panel_info) {
GRegex *regex = g_regex_new(url_regex, G_REGEX_CASELESS, G_REGEX_MATCH_NOTEMPTY, nullptr);
GArray *attributes = g_array_new(FALSE, FALSE, sizeof(VteCharAttributes));
auto content = make_unique(vte_terminal_get_text(vte, nullptr, nullptr, attributes), g_free);
for (char *s_ptr = content.get(), *saveptr; ; s_ptr = nullptr) {
const char *token = strtok_r(s_ptr, "\n", &saveptr);
if (!token) {
break;
}
GError *error = nullptr;
GMatchInfo *info;
g_regex_match_full(regex, token, -1, 0, (GRegexMatchFlags)0, &info, &error);
while (g_match_info_matches(info)) {
int pos;
g_match_info_fetch_pos(info, 0, &pos, nullptr);
const long first_row = g_array_index(attributes, VteCharAttributes, 0).row;
const auto attr = g_array_index(attributes, VteCharAttributes, token + pos - content.get());
panel_info->url_list.emplace_back(g_match_info_fetch(info, 0),
attr.column,
attr.row - first_row);
g_match_info_next(info, &error);
}
g_match_info_free(info);
if (error) {
g_printerr("error while matching: %s\n", error->message);
g_error_free(error);
}
}
g_regex_unref(regex);
g_array_free(attributes, TRUE);
}
static void launch_url(char *browser, const char *text, search_panel_info *info) {
char *end;
errno = 0;
unsigned long id = strtoul(text, &end, 10);
if (!errno && id && id <= info->url_list.size() && !*end) {
launch_browser(browser, info->url_list[id - 1].url.get());
} else {
g_printerr("url hint invalid: %s\n", text);
}
}
static void draw_rectangle(cairo_t *cr, double x, double y, double height,
double width, double radius) {
double a = x, b = x + height, c = y, d = y + width;
cairo_arc(cr, a + radius, c + radius, radius, 2*(M_PI/2), 3*(M_PI/2));
cairo_arc(cr, b - radius, c + radius, radius, 3*(M_PI/2), 4*(M_PI/2));
cairo_arc(cr, b - radius, d - radius, radius, 0*(M_PI/2), 1*(M_PI/2));
cairo_arc(cr, a + radius, d - radius, radius, 1*(M_PI/2), 2*(M_PI/2));
cairo_close_path(cr);
}
static void draw_marker(cairo_t *cr, const PangoFontDescription *desc,
const hint_info *hints, long x, long y, const char *msg,
bool active) {
cairo_text_extents_t ext;
int width, height;
cairo_text_extents(cr, msg, &ext);
PangoLayout *layout = pango_cairo_create_layout(cr);
pango_layout_set_font_description(layout, desc);
pango_layout_set_text(layout, msg, -1);
pango_layout_get_size(layout, &width, &height);
draw_rectangle(cr, static_cast<double>(x), static_cast<double>(y),
static_cast<double>(width / PANGO_SCALE) + hints->padding * 2,
static_cast<double>(height / PANGO_SCALE) + hints->padding * 2,
hints->roundness);
cairo_set_source(cr, hints->border);
cairo_set_line_width(cr, hints->border_width);
cairo_stroke_preserve(cr);
cairo_set_source(cr, active ? hints->ab : hints->bg);
cairo_fill(cr);
cairo_new_path(cr);
cairo_move_to(cr, static_cast<double>(x) + hints->padding,
static_cast<double>(y) + hints->padding);
cairo_set_source(cr, active ? hints->af : hints->fg);
pango_cairo_update_layout(cr, layout);
pango_cairo_layout_path(cr, layout);
cairo_fill(cr);
g_object_unref(layout);
}
static gboolean draw_cb(const draw_cb_info *info, cairo_t *cr) {
if (!info->panel->url_list.empty()) {
char buffer[std::numeric_limits<unsigned>::digits10 + 1];
int padding_left, padding_top, padding_right, padding_bottom;
const long cw = vte_terminal_get_char_width(info->vte);
const long ch = vte_terminal_get_char_height(info->vte);
const PangoFontDescription *desc = info->hints->font ?
info->hints->font : vte_terminal_get_font(info->vte);
size_t len = info->panel->fulltext == nullptr ?
0 : strlen(info->panel->fulltext);
cairo_set_line_width(cr, 1);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_stroke(cr);
get_vte_padding(info->vte, &padding_left, &padding_top, &padding_right, &padding_bottom);
for (unsigned i = 0; i < info->panel->url_list.size(); i++) {
const url_data &data = info->panel->url_list[i];
const long x = data.col * cw + padding_left;
const long y = data.row * ch + padding_top;
bool active = false;
snprintf(buffer, sizeof(buffer), "%u", i + 1);
if (len)
active = strncmp(buffer, info->panel->fulltext, len) == 0;
if (!info->filter_unmatched_urls || active || len == 0)
draw_marker(cr, desc, info->hints, x, y, buffer, active);
}
}
return FALSE;
}
static void update_selection(VteTerminal *vte, const select_info *select) {
vte_terminal_unselect_all(vte);
if (select->mode == vi_mode::command) {
return;
}
const long n_columns = vte_terminal_get_column_count(vte);
long cursor_col, cursor_row, selection_x_end;
vte_terminal_get_cursor_position(vte, &cursor_col, &cursor_row);
vte_terminal_set_selection_block_mode(vte, select->mode == vi_mode::visual_block);
if (select->mode == vi_mode::visual) {
const long begin = select->begin_row * n_columns + select->begin_col;
const long end = cursor_row * n_columns + cursor_col;
if (begin < end) {
selection_x_end = cursor_col;
#if VTE_CHECK_VERSION(0, 55, 0)
selection_x_end += 1;
#endif
vte_terminal_select_text(vte, select->begin_col, select->begin_row,
selection_x_end, cursor_row);
} else {
selection_x_end = select->begin_col;
#if VTE_CHECK_VERSION(0, 55, 0)
selection_x_end += 1;
#endif
vte_terminal_select_text(vte, cursor_col, cursor_row,
selection_x_end, select->begin_row);
}
} else if (select->mode == vi_mode::visual_line) {
selection_x_end = n_columns - 1;
#if VTE_CHECK_VERSION(0, 55, 0)
selection_x_end += 1;
#endif
vte_terminal_select_text(vte, 0,
std::min(select->begin_row, cursor_row),
selection_x_end,
std::max(select->begin_row, cursor_row));
} else if (select->mode == vi_mode::visual_block) {
selection_x_end = std::max(select->begin_col, cursor_col);
#if VTE_CHECK_VERSION(0, 55, 0)
selection_x_end += 1;
#endif
vte_terminal_select_text(vte,
std::min(select->begin_col, cursor_col),
std::min(select->begin_row, cursor_row),
selection_x_end,
std::max(select->begin_row, cursor_row));
}
vte_terminal_copy_primary(vte);
}
static void enter_command_mode(VteTerminal *vte, select_info *select) {
vte_terminal_disconnect_pty_read(vte);
select->mode = vi_mode::command;
vte_terminal_get_cursor_position(vte, &select->origin_col, &select->origin_row);
update_selection(vte, select);
}
static void exit_command_mode(VteTerminal *vte, select_info *select) {
vte_terminal_set_cursor_position(vte, select->origin_col, select->origin_row);
vte_terminal_connect_pty_read(vte);
vte_terminal_unselect_all(vte);
select->mode = vi_mode::insert;
}
static void toggle_visual(VteTerminal *vte, select_info *select, vi_mode mode) {
if (select->mode == mode) {
select->mode = vi_mode::command;
} else {
if (select->mode == vi_mode::command) {
vte_terminal_get_cursor_position(vte, &select->begin_col, &select->begin_row);
}
select->mode = mode;
}
update_selection(vte, select);
}
static long first_row(VteTerminal *vte) {
GtkAdjustment *adjust = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(vte));
return (long)gtk_adjustment_get_lower(adjust);
}
static long last_row(VteTerminal *vte) {
GtkAdjustment *adjust = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(vte));
return (long)gtk_adjustment_get_upper(adjust) - 1;
}
static long top_row(VteTerminal *vte) {
GtkAdjustment *adjust = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(vte));
return (long)gtk_adjustment_get_value(adjust);
}
static long middle_row(VteTerminal *vte) {
GtkAdjustment *adjust = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(vte));
return (long)gtk_adjustment_get_value(adjust) +
(long)vte_terminal_get_row_count(vte) / 2;
}
static long bottom_row(VteTerminal *vte) {
GtkAdjustment *adjust = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(vte));
return (long)gtk_adjustment_get_value(adjust) +
(long)vte_terminal_get_row_count(vte) - 1;
}
static void update_scroll(VteTerminal *vte) {
GtkAdjustment *adjust = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(vte));
const double scroll_row = gtk_adjustment_get_value(adjust);
const long n_rows = vte_terminal_get_row_count(vte);
long cursor_row;
vte_terminal_get_cursor_position(vte, nullptr, &cursor_row);
if ( (double)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);
VteCursorBlinkMode mode = vte_terminal_get_cursor_blink_mode(vte);
vte_terminal_set_cursor_blink_mode(vte, VTE_CURSOR_BLINK_OFF);
vte_terminal_set_cursor_position(vte,
clamp(cursor_col + col, 0l, end_col),
clamp(cursor_row + row, first_row(vte), last_row(vte)));
update_scroll(vte);
update_selection(vte, select);
vte_terminal_set_cursor_blink_mode(vte, mode);
}
static void move_to_row_start(VteTerminal *vte, select_info *select, long row) {
vte_terminal_set_cursor_position(vte, 0, row);
update_scroll(vte);
update_selection(vte, select);
}
static void open_selection(char *browser, VteTerminal *vte) {
if (!vte_terminal_get_has_selection(vte)) {
g_printerr("no selection to open\n");
return;
}
if (browser) {
auto selection = make_unique(vte_terminal_get_selection(vte), g_free);
if (selection && *selection) {
launch_browser(browser, selection.get());
}
} else {
g_printerr("no browser to open url\n");
}
}
static std::unique_ptr<char, decltype(&g_free)>
get_text_range(VteTerminal *vte, long start_row, long start_col, long end_row, long end_col) {
return {vte_terminal_get_text_range(vte, start_row, start_col, end_row, end_col,
nullptr, nullptr, nullptr), g_free};
}
static bool is_word_char(gunichar c) {
static const char *word_char_ascii_punct = "-,./?%&#_=+@~";
return g_unichar_isgraph(c) &&
(g_unichar_isalnum(c) || (g_unichar_ispunct(c) &&
(c >= 0x80 || strchr(word_char_ascii_punct, (int)c) != NULL)));
}
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);
auto content = get_text_range(vte, cursor_row, 0, cursor_row, cursor_col);
if (!content) {
return;
}
long length;
gunichar *codepoints = g_utf8_to_ucs4(content.get(), -1, nullptr, &length, nullptr);
if (!codepoints) {
return;
}
bool in_word = false;
for (long i = length - 2; i > 0; i--) {
cursor_col--;
if (!is_word(codepoints[i - 1])) {
if (in_word) {
break;
}
} else {
in_word = true;
}
}
vte_terminal_set_cursor_position(vte, cursor_col, cursor_row);
update_selection(vte, select);
g_free(codepoints);
}
static void move_backward_word(VteTerminal *vte, select_info *select) {
move_backward(vte, select, is_word_char);
}
static void move_backward_blank_word(VteTerminal *vte, select_info *select) {
move_backward(vte, select, std::not1(std::ref(g_unichar_isspace)));
}
template<typename F>
void move_first(VteTerminal *vte, select_info *select, F is_match) {
long cursor_col, cursor_row;
vte_terminal_get_cursor_position(vte, &cursor_col, &cursor_row);
const long end_col = vte_terminal_get_column_count(vte) - 1;
auto content = get_text_range(vte, cursor_row, cursor_col, cursor_row, end_col);
if (!content) {
return;
}
long length;
gunichar *codepoints = g_utf8_to_ucs4(content.get(), -1, nullptr, &length, nullptr);
if (!codepoints) {
return;
}
auto iter = std::find_if(codepoints, codepoints + length, is_match);
if (iter != codepoints + length) {
vte_terminal_set_cursor_position(vte, iter - codepoints, cursor_row);
update_selection(vte, select);
}
g_free(codepoints);
}
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);
}
static void move_to_eol(VteTerminal *vte, select_info *select) {
long cursor_row;
vte_terminal_get_cursor_position(vte, nullptr, &cursor_row);
const long end_col = vte_terminal_get_column_count(vte) - 1;
auto content = get_text_range(vte, cursor_row, 0, cursor_row, end_col);
if (!content) {
return;
}
long length;
gunichar *codepoints = g_utf8_to_ucs4(content.get(), -1, nullptr, &length, nullptr);
if (!codepoints) {
return;
}
auto iter = std::find(codepoints, codepoints + length, '\n');
set_cursor_column(vte, select, std::max(iter - codepoints - 1l, 0l));
g_free(codepoints);
}
template<typename F>
static void move_forward(VteTerminal *vte, select_info *select, F is_word, bool goto_word_end) {
long cursor_col, cursor_row;
vte_terminal_get_cursor_position(vte, &cursor_col, &cursor_row);
const long end_col = vte_terminal_get_column_count(vte) - 1;
auto content = get_text_range(vte, cursor_row, cursor_col, cursor_row, end_col);
if (!content) {
return;
}
long length;
gunichar *codepoints = g_utf8_to_ucs4(content.get(), -1, nullptr, &length, nullptr);
if (!codepoints) {
return;
}
// prevent going past the end (get_text_range adds a \n)
if (codepoints[length - 1] == '\n') {
length--;
}
bool end_of_word = false;
if (!goto_word_end) {
for (long i = 1; i < length; i++) {
if (is_word(codepoints[i - 1])) {
if (end_of_word) {
break;
}
} else {
end_of_word = true;
}
cursor_col++;
}
} else {
for (long i = 2; i <= length; i++) {
cursor_col++;
if (is_word(codepoints[i - 1]) && !is_word(codepoints[i])) {
break;
}
}
}
vte_terminal_set_cursor_position(vte, cursor_col, cursor_row);
update_selection(vte, select);
g_free(codepoints);
}
static void move_forward_end_word(VteTerminal *vte, select_info *select) {
move_forward(vte, select, is_word_char, true);
}
static void move_forward_end_blank_word(VteTerminal *vte, select_info *select) {
move_forward(vte, select, std::not1(std::ref(g_unichar_isspace)), true);
}
static void move_forward_word(VteTerminal *vte, select_info *select) {
move_forward(vte, select, is_word_char, false);
}
static void move_forward_blank_word(VteTerminal *vte, select_info *select) {
move_forward(vte, select, std::not1(std::ref(g_unichar_isspace)), false);
}
/* {{{ CALLBACKS */
void window_title_cb(VteTerminal *vte, gboolean *dynamic_title) {
const char *const title = *dynamic_title ? vte_terminal_get_window_title(vte) : nullptr;
gtk_window_set_title(GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(vte))),
title ? title : "termite");
}
static void reset_font_scale(VteTerminal *vte, gdouble scale) {
vte_terminal_set_font_scale(vte, scale);
}
static void increase_font_scale(VteTerminal *vte) {
gdouble scale = vte_terminal_get_font_scale(vte);
for (auto it = zoom_factors.begin(); it != zoom_factors.end(); ++it) {
if ((*it - scale) > 1e-6) {
vte_terminal_set_font_scale(vte, *it);
return;
}
}
}
static void decrease_font_scale(VteTerminal *vte) {
gdouble scale = vte_terminal_get_font_scale(vte);
for (auto it = zoom_factors.rbegin(); it != zoom_factors.rend(); ++it) {
if ((scale - *it) > 1e-6) {
vte_terminal_set_font_scale(vte, *it);
return;
}
}
}
gboolean window_state_cb(GtkWindow *, GdkEventWindowState *event, keybind_info *info) {
if (event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN)
info->fullscreen_toggle = gtk_window_unfullscreen;
else
info->fullscreen_toggle = gtk_window_fullscreen;
return FALSE;
}
gboolean key_press_cb(VteTerminal *vte, GdkEventKey *event, keybind_info *info) {
const guint modifiers = event->state & gtk_accelerator_get_default_mod_mask();
if (info->config.fullscreen && event->keyval == GDK_KEY_F11 && !modifiers) {
info->fullscreen_toggle(info->window);
return TRUE;
}
if (info->select.mode != vi_mode::insert) {
if (modifiers == GDK_CONTROL_MASK) {
switch (gdk_keyval_to_lower(event->keyval)) {
case GDK_KEY_bracketleft:
exit_command_mode(vte, &info->select);
gtk_widget_hide(info->panel.da);
gtk_widget_hide(info->panel.entry);
info->panel.url_list.clear();
break;
case GDK_KEY_v:
toggle_visual(vte, &info->select, vi_mode::visual_block);
break;
case GDK_KEY_Left:
move_backward_blank_word(vte, &info->select);
break;
case GDK_KEY_Right:
move_forward_blank_word(vte, &info->select);
break;
case GDK_KEY_u:
move(vte, &info->select, 0, -(vte_terminal_get_row_count(vte) / 2));
break;
case GDK_KEY_d:
move(vte, &info->select, 0, vte_terminal_get_row_count(vte) / 2);
break;
case GDK_KEY_b:
move(vte, &info->select, 0, -(vte_terminal_get_row_count(vte) - 1));
break;
case GDK_KEY_f:
move(vte, &info->select, 0, vte_terminal_get_row_count(vte) - 1);
break;
}
return TRUE;
}
if (modifiers == GDK_SHIFT_MASK) {
switch (event->keyval) {
case GDK_KEY_Left:
move_backward_word(vte, &info->select);
return TRUE;
case GDK_KEY_Right:
move_forward_word(vte, &info->select);
return TRUE;
}
}
switch (event->keyval) {
case GDK_KEY_Escape:
case GDK_KEY_q:
exit_command_mode(vte, &info->select);
gtk_widget_hide(info->panel.da);
gtk_widget_hide(info->panel.entry);
info->panel.url_list.clear();
break;
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;
case GDK_KEY_Right:
case GDK_KEY_l:
move(vte, &info->select, 1, 0);
break;
case GDK_KEY_b:
move_backward_word(vte, &info->select);
break;
case GDK_KEY_B:
move_backward_blank_word(vte, &info->select);
break;
case GDK_KEY_w:
move_forward_word(vte, &info->select);
break;
case GDK_KEY_W:
move_forward_blank_word(vte, &info->select);
break;
case GDK_KEY_e:
move_forward_end_word(vte, &info->select);
break;
case GDK_KEY_E:
move_forward_end_blank_word(vte, &info->select);
break;
case GDK_KEY_0:
case GDK_KEY_Home:
set_cursor_column(vte, &info->select, 0);
break;
case GDK_KEY_asciicircum:
set_cursor_column(vte, &info->select, 0);
move_first(vte, &info->select, std::not1(std::ref(g_unichar_isspace)));
break;
case GDK_KEY_dollar:
case GDK_KEY_End:
move_to_eol(vte, &info->select);
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;
case GDK_KEY_H:
move_to_row_start(vte, &info->select, top_row(vte));
break;
case GDK_KEY_M:
move_to_row_start(vte, &info->select, middle_row(vte));
break;
case GDK_KEY_L:
move_to_row_start(vte, &info->select, bottom_row(vte));
break;
case GDK_KEY_v:
toggle_visual(vte, &info->select, vi_mode::visual);
break;
case GDK_KEY_V:
toggle_visual(vte, &info->select, vi_mode::visual_line);
break;
case GDK_KEY_y:
#if VTE_CHECK_VERSION(0, 50, 0)
vte_terminal_copy_clipboard_format(vte, VTE_FORMAT_TEXT);
#else
vte_terminal_copy_clipboard(vte);
#endif
break;
case GDK_KEY_slash:
overlay_show(&info->panel, overlay_mode::search, vte);
break;
case GDK_KEY_question:
overlay_show(&info->panel, overlay_mode::rsearch, vte);
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(info->config.browser, vte);
break;
case GDK_KEY_Return:
open_selection(info->config.browser, vte);
exit_command_mode(vte, &info->select);
break;
case GDK_KEY_x:
if (!info->config.browser)
break;
find_urls(vte, &info->panel);
gtk_widget_show(info->panel.da);
overlay_show(&info->panel, overlay_mode::urlselect, nullptr);
break;
}
return TRUE;
}
if (modifiers == (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) {
switch (gdk_keyval_to_lower(event->keyval)) {
case GDK_KEY_plus:
increase_font_scale(vte);
return TRUE;
case GDK_KEY_equal: