-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.c
2379 lines (2138 loc) · 79.1 KB
/
main.c
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
/*
(c) 2009 by Leon Winter
(c) 2009-2011 by Hannes Schueller
(c) 2009-2010 by Matto Fransen
(c) 2010-2011 by Hans-Peter Deifel
(c) 2010-2011 by Thomas Adam
(c) 2011 by Albert Kim
see LICENSE file
*/
#include <X11/Xlib.h>
#include "includes.h"
#include "vimprobable.h"
#include "utilities.h"
#include "callbacks.h"
#include "javascript.h"
/* the CLEAN_MOD_*_MASK defines have all the bits set that will be stripped from the modifier bit field */
#define CLEAN_MOD_NUMLOCK_MASK (GDK_MOD2_MASK)
#define CLEAN_MOD_BUTTON_MASK (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK|GDK_BUTTON3_MASK|GDK_BUTTON4_MASK|GDK_BUTTON5_MASK)
/* remove unused bits, numlock symbol and buttons from keymask */
#define CLEAN(mask) (mask & (GDK_MODIFIER_MASK) & ~(CLEAN_MOD_NUMLOCK_MASK) & ~(CLEAN_MOD_BUTTON_MASK))
#define IS_ESCAPE(event) (IS_ESCAPE_KEY(CLEAN(event->state), event->keyval))
#define IS_ESCAPE_KEY(s, k) ((s == 0 && k == GDK_Escape) || \
(s == GDK_CONTROL_MASK && k == GDK_bracketleft))
/* callbacks here */
static void inputbox_activate_cb(GtkEntry *entry, gpointer user_data);
static gboolean inputbox_keypress_cb(GtkEntry *entry, GdkEventKey *event);
static gboolean inputbox_keyrelease_cb(GtkEntry *entry, GdkEventKey *event);
static gboolean inputbox_changed_cb(GtkEditable *entry, gpointer user_data);
static WebKitWebView* inspector_inspect_web_view_cb(gpointer inspector, WebKitWebView* web_view);
static gboolean notify_event_cb(GtkWidget *widget, GdkEvent *event, gpointer user_data);
static gboolean webview_console_cb(WebKitWebView *webview, char *message, int line, char *source, gpointer user_data);
static gboolean webview_download_cb(WebKitWebView *webview, WebKitDownload *download, gpointer user_data);
static void webview_hoverlink_cb(WebKitWebView *webview, char *title, char *link, gpointer data);
static gboolean webview_keypress_cb(WebKitWebView *webview, GdkEventKey *event);
static void webview_load_committed_cb(WebKitWebView *webview, WebKitWebFrame *frame, gpointer user_data);
static void webview_load_finished_cb(WebKitWebView *webview, WebKitWebFrame *frame, gpointer user_data);
static gboolean webview_mimetype_cb(WebKitWebView *webview, WebKitWebFrame *frame, WebKitNetworkRequest *request,
char *mime_type, WebKitWebPolicyDecision *decision, gpointer user_data);
static gboolean webview_new_window_cb(WebKitWebView *webview, WebKitWebFrame *frame, WebKitNetworkRequest *request,
WebKitWebNavigationAction *action, WebKitWebPolicyDecision *decision, gpointer user_data);
static gboolean webview_open_in_new_window_cb(WebKitWebView *webview, WebKitWebFrame *frame, gpointer user_data);
static void webview_progress_changed_cb(WebKitWebView *webview, int progress, gpointer user_data);
static void webview_title_changed_cb(WebKitWebView *webview, WebKitWebFrame *frame, char *title, gpointer user_data);
static void window_destroyed_cb(GtkWidget *window, gpointer func_data);
/* functions */
static gboolean bookmark(const Arg *arg);
static gboolean browser_settings(const Arg *arg);
static gboolean commandhistoryfetch(const Arg *arg);
static gboolean complete(const Arg *arg);
static gboolean descend(const Arg *arg);
gboolean echo(const Arg *arg);
static gboolean focus_input(const Arg *arg);
static gboolean input(const Arg *arg);
static gboolean navigate(const Arg *arg);
static gboolean number(const Arg *arg);
static gboolean open_arg(const Arg *arg);
static gboolean open_remembered(const Arg *arg);
static gboolean paste(const Arg *arg);
static gboolean quickmark(const Arg *arg);
static gboolean quit(const Arg *arg);
static gboolean revive(const Arg *arg);
static gboolean print_frame(const Arg *arg);
static gboolean search(const Arg *arg);
static gboolean set(const Arg *arg);
static gboolean script(const Arg *arg);
static gboolean scroll(const Arg *arg);
static gboolean search_tag(const Arg *arg);
static gboolean yank(const Arg *arg);
static gboolean view_source(const Arg * arg);
static gboolean zoom(const Arg *arg);
static gboolean fake_key_event(const Arg *arg);
static void update_url(const char *uri);
static void setup_modkeys(void);
static void setup_gui(void);
static void setup_settings(void);
static void setup_signals(void);
static void ascii_bar(int total, int state, char *string);
static gchar *jsapi_ref_to_string(JSContextRef context, JSValueRef ref);
static void jsapi_evaluate_script(const gchar *script, gchar **value, gchar **message);
static void download_progress(WebKitDownload *d, GParamSpec *pspec);
static void set_widget_font_and_color(GtkWidget *widget, const char *font_str,
const char *bg_color_str, const char *fg_color_str);
static gboolean history(void);
static gboolean process_set_line(char *line);
void save_command_history(char *line);
void toggle_proxy(gboolean onoff);
void toggle_scrollbars(gboolean onoff);
gboolean process_keypress(GdkEventKey *event);
void fill_suggline(char * suggline, const char * command, const char *fill_with);
GtkWidget * fill_eventbox(const char * completion_line);
static void mop_up(void);
#include "main.h"
/* variables */
static GtkWindow *window;
static GtkWidget *viewport;
static GtkBox *box;
static GtkScrollbar *scroll_h;
static GtkScrollbar *scroll_v;
static GtkAdjustment *adjust_h;
static GtkAdjustment *adjust_v;
static GtkWidget *inputbox;
static GtkWidget *eventbox;
static GtkBox *statusbar;
static GtkWidget *status_url;
static GtkWidget *status_state;
static WebKitWebView *webview;
static SoupSession *session;
static GtkClipboard *clipboards[2];
static char **args;
static unsigned int mode = ModeNormal;
static unsigned int count = 0;
static float zoomstep;
static char *modkeys;
static char current_modkey;
static char *search_handle;
static gboolean search_direction;
static gboolean echo_active = TRUE;
WebKitWebInspector *inspector;
static GdkNativeWindow embed = 0;
static char *configfile = NULL;
static char *winid = NULL;
static char rememberedURI[1024] = "";
static char followTarget[8] = "";
char *error_msg = NULL;
GList *activeDownloads;
#include "config.h"
#include "keymap.h"
char commandhistory[COMMANDHISTSIZE][255];
int lastcommand = 0;
int maxcommands = 0;
int commandpointer = 0;
KeyList *keylistroot = NULL;
/* Cookie support. */
#ifdef ENABLE_COOKIE_SUPPORT
static SoupCookieJar *session_cookie_jar = NULL;
static SoupCookieJar *file_cookie_jar = NULL;
static time_t cookie_timeout = 4800;
static char *cookie_store;
static void setup_cookies(void);
static const char *get_cookies(SoupURI *soup_uri);
static void load_all_cookies(void);
static void new_generic_request(SoupSession *soup_ses, SoupMessage *soup_msg, gpointer unused);
static void update_cookie_jar(SoupCookieJar *jar, SoupCookie *old, SoupCookie *new);
static void handle_cookie_request(SoupMessage *soup_msg, gpointer unused);
#endif
/* callbacks */
void
window_destroyed_cb(GtkWidget *window, gpointer func_data) {
quit(NULL);
}
void
webview_title_changed_cb(WebKitWebView *webview, WebKitWebFrame *frame, char *title, gpointer user_data) {
gtk_window_set_title(window, title);
}
void
webview_progress_changed_cb(WebKitWebView *webview, int progress, gpointer user_data) {
#ifdef ENABLE_GTK_PROGRESS_BAR
gtk_entry_set_progress_fraction(GTK_ENTRY(inputbox), progress == 100 ? 0 : (double)progress/100);
#endif
update_state();
}
#ifdef ENABLE_WGET_PROGRESS_BAR
void
ascii_bar(int total, int state, char *string) {
int i;
for (i = 0; i < state; i++)
string[i] = progressbartickchar;
string[i++] = progressbarcurrent;
for (; i < total; i++)
string[i] = progressbarspacer;
string[i] = '\0';
}
#endif
void
webview_load_committed_cb(WebKitWebView *webview, WebKitWebFrame *frame, gpointer user_data) {
Arg a = { .i = Silent, .s = g_strdup(JS_SETUP_HINTS) };
const char *uri = webkit_web_view_get_uri(webview);
update_url(uri);
script(&a);
}
void
webview_load_finished_cb(WebKitWebView *webview, WebKitWebFrame *frame, gpointer user_data) {
Arg a = { .i = Silent, .s = g_strdup(JS_SETUP_INPUT_FOCUS) };
if (HISTORY_MAX_ENTRIES > 0)
history();
update_state();
script(&a);
}
static gboolean
webview_open_in_new_window_cb(WebKitWebView *webview, WebKitWebFrame *frame, gpointer user_data) {
Arg a = { .i = TargetNew, .s = (char*)webkit_web_view_get_uri(webview) };
if (strlen(rememberedURI) > 0) {
a.s = rememberedURI;
}
open_arg(&a);
return FALSE;
}
gboolean
webview_new_window_cb(WebKitWebView *webview, WebKitWebFrame *frame, WebKitNetworkRequest *request,
WebKitWebNavigationAction *action, WebKitWebPolicyDecision *decision, gpointer user_data) {
Arg a = { .i = TargetNew, .s = (char*)webkit_network_request_get_uri(request) };
open_arg(&a);
webkit_web_policy_decision_ignore(decision);
return TRUE;
}
gboolean
webview_mimetype_cb(WebKitWebView *webview, WebKitWebFrame *frame, WebKitNetworkRequest *request,
char *mime_type, WebKitWebPolicyDecision *decision, gpointer user_data) {
if (webkit_web_view_can_show_mime_type(webview, mime_type) == FALSE) {
webkit_web_policy_decision_download(decision);
return TRUE;
} else {
return FALSE;
}
}
static WebKitWebView*
inspector_inspect_web_view_cb(gpointer inspector, WebKitWebView* web_view) {
gchar* inspector_title;
GtkWidget* inspector_window;
GtkWidget* inspector_view;
/* just enough code to show the inspector - no signal handling etc. */
inspector_title = g_strdup_printf("Inspect page - %s - Vimprobable2", webkit_web_view_get_uri(web_view));
if (embed) {
inspector_window = gtk_plug_new(embed);
} else {
inspector_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_wmclass(window, "vimprobable2", "Vimprobable2");
}
gtk_window_set_title(GTK_WINDOW(inspector_window), inspector_title);
g_free(inspector_title);
inspector_view = webkit_web_view_new();
gtk_container_add(GTK_CONTAINER(inspector_window), inspector_view);
gtk_widget_show_all(inspector_window);
return WEBKIT_WEB_VIEW(inspector_view);
}
gboolean
webview_download_cb(WebKitWebView *webview, WebKitDownload *download, gpointer user_data) {
const gchar *filename;
gchar *uri, *path;
uint32_t size;
Arg a;
filename = webkit_download_get_suggested_filename(download);
if (filename == NULL || strlen(filename) == 0) {
filename = "vimprobable_download";
}
path = g_build_filename(g_strdup_printf(DOWNLOADS_PATH), filename, NULL);
uri = g_strconcat("file://", path, NULL);
webkit_download_set_destination_uri(download, uri);
g_free(uri);
size = (uint32_t)webkit_download_get_total_size(download);
a.i = Info;
if (size > 0)
a.s = g_strdup_printf("Download %s started (expected size: %u bytes)...", filename, size);
else
a.s = g_strdup_printf("Download %s started (unknown size)...", filename);
echo(&a);
g_free(a.s);
activeDownloads = g_list_prepend(activeDownloads, download);
g_signal_connect(download, "notify::progress", G_CALLBACK(download_progress), NULL);
g_signal_connect(download, "notify::status", G_CALLBACK(download_progress), NULL);
update_state();
return TRUE;
}
void
download_progress(WebKitDownload *d, GParamSpec *pspec) {
Arg a;
WebKitDownloadStatus status = webkit_download_get_status(d);
if (status != WEBKIT_DOWNLOAD_STATUS_STARTED && status != WEBKIT_DOWNLOAD_STATUS_CREATED) {
if (status != WEBKIT_DOWNLOAD_STATUS_FINISHED) {
a.i = Error;
a.s = g_strdup_printf("Error while downloading %s", webkit_download_get_suggested_filename(d));
echo(&a);
} else {
a.i = Info;
a.s = g_strdup_printf("Download %s finished", webkit_download_get_suggested_filename(d));
echo(&a);
}
g_free(a.s);
activeDownloads = g_list_remove(activeDownloads, d);
}
update_state();
}
gboolean
process_keypress(GdkEventKey *event) {
KeyList *current;
current = keylistroot;
while (current != NULL) {
if (current->Element.mask == CLEAN(event->state)
&& (current->Element.modkey == current_modkey
|| (!current->Element.modkey && !current_modkey)
|| current->Element.modkey == GDK_VoidSymbol ) /* wildcard */
&& current->Element.key == event->keyval
&& current->Element.func)
if (current->Element.func(¤t->Element.arg)) {
current_modkey = count = 0;
update_state();
return TRUE;
}
current = current->next;
}
return FALSE;
}
gboolean
webview_keypress_cb(WebKitWebView *webview, GdkEventKey *event) {
Arg a = { .i = ModeNormal, .s = NULL };
switch (mode) {
case ModeNormal:
if (CLEAN(event->state) == 0) {
if (IS_ESCAPE(event)) {
a.i = Info;
a.s = g_strdup("");
echo(&a);
g_free(a.s);
} else if (current_modkey == 0 && ((event->keyval >= GDK_1 && event->keyval <= GDK_9)
|| (event->keyval == GDK_0 && count))) {
count = (count ? count * 10 : 0) + (event->keyval - GDK_0);
update_state();
return TRUE;
} else if (strchr(modkeys, event->keyval) && current_modkey != event->keyval) {
current_modkey = event->keyval;
update_state();
return TRUE;
}
}
/* keybindings */
if (process_keypress(event) == TRUE) return TRUE;
break;
case ModeInsert:
if (IS_ESCAPE(event)) {
a.i = Silent;
a.s = g_strdup("vimprobable_clearfocus()");
script(&a);
a.i = ModeNormal;
return set(&a);
}
case ModePassThrough:
if (IS_ESCAPE(event)) {
echo(&a);
set(&a);
return TRUE;
}
break;
case ModeSendKey:
echo(&a);
set(&a);
break;
}
return FALSE;
}
void
set_widget_font_and_color(GtkWidget *widget, const char *font_str, const char *bg_color_str,
const char *fg_color_str) {
GdkColor fg_color;
GdkColor bg_color;
PangoFontDescription *font;
font = pango_font_description_from_string(font_str);
gtk_widget_modify_font(widget, font);
pango_font_description_free(font);
if (fg_color_str)
gdk_color_parse(fg_color_str, &fg_color);
if (bg_color_str)
gdk_color_parse(bg_color_str, &bg_color);
gtk_widget_modify_text(widget, GTK_STATE_NORMAL, fg_color_str ? &fg_color : NULL);
gtk_widget_modify_base(widget, GTK_STATE_NORMAL, bg_color_str ? &bg_color : NULL);
return;
}
void
webview_hoverlink_cb(WebKitWebView *webview, char *title, char *link, gpointer data) {
const char *uri = webkit_web_view_get_uri(webview);
memset(rememberedURI, 0, 1024);
if (link) {
gtk_label_set_markup(GTK_LABEL(status_url), g_markup_printf_escaped("<span font=\"%s\">Link: %s</span>", statusfont, link));
strncpy(rememberedURI, link, 1024);
} else
update_url(uri);
}
gboolean
webview_console_cb(WebKitWebView *webview, char *message, int line, char *source, gpointer user_data) {
Arg a;
/* Don't change internal mode if the browser doesn't have focus to prevent inconsistent states */
if (gtk_window_has_toplevel_focus(window)) {
if (!strcmp(message, "hintmode_off") || !strcmp(message, "insertmode_off")) {
a.i = ModeNormal;
return set(&a);
} else if (!strcmp(message, "insertmode_on")) {
a.i = ModeInsert;
return set(&a);
}
}
return FALSE;
}
void
inputbox_activate_cb(GtkEntry *entry, gpointer user_data) {
char *text;
guint16 length = gtk_entry_get_text_length(entry);
Arg a;
gboolean success = FALSE, forward = FALSE;
a.i = HideCompletion;
complete(&a);
if (length == 0)
return;
text = (char*)gtk_entry_get_text(entry);
if (length > 1 && text[0] == ':') {
success = process_line((text + 1));
} else if (length > 1 && ((forward = text[0] == '/') || text[0] == '?')) {
webkit_web_view_unmark_text_matches(webview);
#ifdef ENABLE_MATCH_HIGHLITING
webkit_web_view_mark_text_matches(webview, &text[1], FALSE, 0);
webkit_web_view_set_highlight_text_matches(webview, TRUE);
#endif
count = 0;
#ifndef ENABLE_INCREMENTAL_SEARCH
a.s =& text[1];
a.i = searchoptions | (forward ? DirectionForward : DirectionBackwards);
search(&a);
#else
search_direction = forward;
search_handle = g_strdup(&text[1]);
#endif
} else if (count && (text[0] == '.' || text[0] == ',')) {
a.i = Silent;
a.s = g_strdup_printf("vimprobable_fire(%d)", count);
script(&a);
update_state();
} else
return;
if (!echo_active)
gtk_entry_set_text(entry, "");
gtk_widget_grab_focus(GTK_WIDGET(webview));
}
gboolean
inputbox_keypress_cb(GtkEntry *entry, GdkEventKey *event) {
Arg a;
int numval;
switch (event->keyval) {
case GDK_bracketleft:
case GDK_Escape:
if (!IS_ESCAPE(event)) break;
a.i = HideCompletion;
complete(&a);
a.i = ModeNormal;
return set(&a);
break;
case GDK_Tab:
a.i = DirectionNext;
return complete(&a);
break;
case GDK_Up:
a.i = DirectionPrev;
return commandhistoryfetch(&a);
break;
case GDK_Down:
a.i = DirectionNext;
return commandhistoryfetch(&a);
break;
case GDK_ISO_Left_Tab:
a.i = DirectionPrev;
return complete(&a);
break;
}
if (mode == ModeHints) {
if ((CLEAN(event->state) & GDK_SHIFT_MASK) &&
(CLEAN(event->state) & GDK_CONTROL_MASK) &&
(event->keyval == GDK_BackSpace)) {
count /= 10;
a.i = Silent;
a.s = g_strdup_printf("vimprobable_update_hints(%d)", count);
script(&a);
update_state();
return TRUE;
}
numval = g_unichar_digit_value((gunichar) gdk_keyval_to_unicode(event->keyval));
if ((numval >= 1 && numval <= 9) || (numval == 0 && count)) {
/* allow a zero as non-first number */
count = (count ? count * 10 : 0) + numval;
a.i = Silent;
a.s = g_strdup_printf("vimprobable_update_hints(%d)", count);
script(&a);
update_state();
return TRUE;
}
}
return FALSE;
}
gboolean
notify_event_cb(GtkWidget *widget, GdkEvent *event, gpointer user_data) {
int i;
if (mode == ModeNormal && event->type == GDK_BUTTON_RELEASE) {
/* handle mouse click events */
for (i = 0; i < LENGTH(mouse); i++) {
if (mouse[i].mask == CLEAN(event->button.state)
&& (mouse[i].modkey == current_modkey
|| (!mouse[i].modkey && !current_modkey)
|| mouse[i].modkey == GDK_VoidSymbol) /* wildcard */
&& mouse[i].button == event->button.button
&& mouse[i].func) {
if (mouse[i].func(&mouse[i].arg)) {
current_modkey = count = 0;
update_state();
return TRUE;
}
}
}
}
return FALSE;
}
static gboolean inputbox_keyrelease_cb(GtkEntry *entry, GdkEventKey *event) {
Arg a;
guint16 length = gtk_entry_get_text_length(entry);
if (!length) {
a.i = HideCompletion;
complete(&a);
a.i = ModeNormal;
return set(&a);
}
return FALSE;
}
static gboolean inputbox_changed_cb(GtkEditable *entry, gpointer user_data) {
Arg a;
char *text = (char*)gtk_entry_get_text(GTK_ENTRY(entry));
guint16 length = gtk_entry_get_text_length(GTK_ENTRY(entry));
gboolean forward = FALSE;
/* Update incremental search if the user changes the search text.
*
* Note: gtk_widget_is_focus() is a poor way to check if the change comes
* from the user. But if the entry is focused and the text is set
* through gtk_entry_set_text() in some asyncrounous operation,
* I would consider that a bug.
*/
if (gtk_widget_is_focus(GTK_WIDGET(entry)) && length > 1 && ((forward = text[0] == '/') || text[0] == '?')) {
webkit_web_view_unmark_text_matches(webview);
webkit_web_view_search_text(webview, &text[1], searchoptions & CaseSensitive, forward, searchoptions & Wrapping);
return TRUE;
} else if (gtk_widget_is_focus(GTK_WIDGET(entry)) && length >= 1 &&
(text[0] == '.' || text[0] == ',')) {
a.i = Silent;
a.s = g_strdup("vimprobable_cleanup()");
script(&a);
a.i = Silent;
a.s = g_strconcat("vimprobable_show_hints('", text + 1, "')", NULL);
script(&a);
return TRUE;
} else if (length == 0 && followTarget[0]) {
mode = ModeNormal;
a.i = Silent;
a.s = g_strdup("vimprobable_clear()");
script(&a);
count = 0;
update_state();
}
return FALSE;
}
/* funcs here */
void fill_suggline(char * suggline, const char * command, const char *fill_with) {
memset(suggline, 0, 512);
strncpy(suggline, command, 512);
strncat(suggline, " ", 1);
strncat(suggline, fill_with, 512 - strlen(suggline) - 1);
}
GtkWidget * fill_eventbox(const char * completion_line) {
GtkBox * row;
GtkWidget *row_eventbox, *el;
GdkColor color;
char * markup;
row = GTK_BOX(gtk_hbox_new(FALSE, 0));
row_eventbox = gtk_event_box_new();
gdk_color_parse(completionbgcolor[0], &color);
gtk_widget_modify_bg(row_eventbox, GTK_STATE_NORMAL, &color);
el = gtk_label_new(NULL);
markup = g_strconcat("<span font=\"", completionfont[0], "\" color=\"", completioncolor[0], "\">",
g_markup_escape_text(completion_line, strlen(completion_line)), "</span>", NULL);
gtk_label_set_markup(GTK_LABEL(el), markup);
g_free(markup);
gtk_misc_set_alignment(GTK_MISC(el), 0, 0);
gtk_box_pack_start(row, el, TRUE, TRUE, 2);
gtk_container_add(GTK_CONTAINER(row_eventbox), GTK_WIDGET(row));
return row_eventbox;
}
gboolean
complete(const Arg *arg) {
char *str, *p, *s, *markup, *entry, *searchfor, command[32] = "", suggline[512] = "", **suggurls;
size_t listlen, len, cmdlen;
int i, spacepos;
Listelement *elementlist = NULL, *elementpointer;
gboolean highlight = FALSE;
GtkBox *row;
GtkWidget *row_eventbox, *el;
GtkBox *_table;
GdkColor color;
static GtkWidget *table, *top_border;
static char *prefix;
static char **suggestions;
static GtkWidget **widgets;
static int n = 0, m, current = -1;
str = (char*)gtk_entry_get_text(GTK_ENTRY(inputbox));
len = strlen(str);
/* Get the length of the list of commands for completion. We need this to
* malloc/realloc correctly.
*/
listlen = LENGTH(commands);
if ((len == 0 || str[0] != ':') && arg->i != HideCompletion)
return TRUE;
if (prefix) {
if (arg->i != HideCompletion && widgets && current != -1 && !strcmp(&str[1], suggestions[current])) {
gdk_color_parse(completionbgcolor[0], &color);
gtk_widget_modify_bg(widgets[current], GTK_STATE_NORMAL, &color);
current = (n + current + (arg->i == DirectionPrev ? -1 : 1)) % n;
if ((arg->i == DirectionNext && current == 0)
|| (arg->i == DirectionPrev && current == n - 1))
current = -1;
} else {
free(widgets);
free(suggestions);
free(prefix);
gtk_widget_destroy(GTK_WIDGET(table));
gtk_widget_destroy(GTK_WIDGET(top_border));
table = NULL;
widgets = NULL;
suggestions = NULL;
prefix = NULL;
n = 0;
current = -1;
if (arg->i == HideCompletion)
return TRUE;
}
} else if (arg->i == HideCompletion)
return TRUE;
if (!widgets) {
prefix = g_strdup_printf(str);
widgets = malloc(sizeof(GtkWidget*) * listlen);
suggestions = malloc(sizeof(char*) * listlen);
top_border = gtk_event_box_new();
gtk_widget_set_size_request(GTK_WIDGET(top_border), 0, 1);
gdk_color_parse(completioncolor[2], &color);
gtk_widget_modify_bg(top_border, GTK_STATE_NORMAL, &color);
table = gtk_event_box_new();
gdk_color_parse(completionbgcolor[0], &color);
_table = GTK_BOX(gtk_vbox_new(FALSE, 0));
highlight = len > 1;
if (strchr(str, ' ') == NULL) {
/* command completion */
listlen = LENGTH(commands);
for (i = 0; i < listlen; i++) {
if (commands[i].cmd == NULL)
break;
cmdlen = strlen(commands[i].cmd);
if (!highlight || (n < MAX_LIST_SIZE && len - 1 <= cmdlen && !strncmp(&str[1], commands[i].cmd, len - 1))) {
p = s = malloc(sizeof(char*) * (highlight ? sizeof(COMPLETION_TAG_OPEN) + sizeof(COMPLETION_TAG_CLOSE) - 1 : 1) + cmdlen);
if (highlight) {
memcpy(p, COMPLETION_TAG_OPEN, sizeof(COMPLETION_TAG_OPEN) - 1);
memcpy((p += sizeof(COMPLETION_TAG_OPEN) - 1), &str[1], len - 1);
memcpy((p += len - 1), COMPLETION_TAG_CLOSE, sizeof(COMPLETION_TAG_CLOSE) - 1);
p += sizeof(COMPLETION_TAG_CLOSE) - 1;
}
memcpy(p, &commands[i].cmd[len - 1], cmdlen - len + 2);
row = GTK_BOX(gtk_hbox_new(FALSE, 0));
row_eventbox = gtk_event_box_new();
gtk_widget_modify_bg(row_eventbox, GTK_STATE_NORMAL, &color);
el = gtk_label_new(NULL);
markup = g_strconcat("<span font=\"", completionfont[0], "\" color=\"", completioncolor[0], "\">", s, "</span>", NULL);
free(s);
gtk_label_set_markup(GTK_LABEL(el), markup);
g_free(markup);
gtk_misc_set_alignment(GTK_MISC(el), 0, 0);
gtk_box_pack_start(row, el, TRUE, TRUE, 2);
gtk_container_add(GTK_CONTAINER(row_eventbox), GTK_WIDGET(row));
gtk_box_pack_start(_table, GTK_WIDGET(row_eventbox), FALSE, FALSE, 0);
suggestions[n] = commands[i].cmd;
widgets[n++] = row_eventbox;
}
}
} else {
entry = (char *)malloc(512 * sizeof(char));
if (entry == NULL) {
return FALSE;
}
memset(entry, 0, 512);
suggurls = malloc(sizeof(char*) * listlen);
if (suggurls == NULL) {
return FALSE;
}
spacepos = strcspn(str, " ");
searchfor = (str + spacepos + 1);
strncpy(command, (str + 1), spacepos - 1);
if (strlen(command) == 3 && strncmp(command, "set", 3) == 0) {
/* browser settings */
listlen = LENGTH(browsersettings);
for (i = 0; i < listlen; i++) {
if (n < MAX_LIST_SIZE && strstr(browsersettings[i].name, searchfor) != NULL) {
/* match */
fill_suggline(suggline, command, browsersettings[i].name);
suggurls[n] = (char *)malloc(sizeof(char) * 512 + 1);
strncpy(suggurls[n], suggline, 512);
suggestions[n] = suggurls[n];
row_eventbox = fill_eventbox(suggline);
gtk_box_pack_start(_table, GTK_WIDGET(row_eventbox), FALSE, FALSE, 0);
widgets[n++] = row_eventbox;
}
}
} else if (strlen(command) == 2 && strncmp(command, "qt", 2) == 0) {
/* completion on tags */
spacepos = strcspn(str, " ");
searchfor = (str + spacepos + 1);
elementlist = complete_list(searchfor, 1, elementlist);
} else {
/* URL completion: bookmarks */
elementlist = complete_list(searchfor, 0, elementlist);
m = count_list(elementlist);
if (m < MAX_LIST_SIZE) {
/* URL completion: history */
elementlist = complete_list(searchfor, 2, elementlist);
}
}
elementpointer = elementlist;
while (elementpointer != NULL) {
fill_suggline(suggline, command, elementpointer->element);
suggurls[n] = (char *)malloc(sizeof(char) * 512 + 1);
strncpy(suggurls[n], suggline, 512);
suggestions[n] = suggurls[n];
row_eventbox = fill_eventbox(suggline);
gtk_box_pack_start(_table, GTK_WIDGET(row_eventbox), FALSE, FALSE, 0);
widgets[n++] = row_eventbox;
elementpointer = elementpointer->next;
if (n >= MAX_LIST_SIZE)
break;
}
free_list(elementlist);
if (suggurls != NULL) {
free(suggurls);
suggurls = NULL;
}
if (entry != NULL) {
free(entry);
entry = NULL;
}
}
/* TA: FIXME - this needs rethinking entirely. */
{
GtkWidget **widgets_temp = realloc(widgets, sizeof(*widgets) * n);
if (widgets_temp == NULL && widgets == NULL) {
fprintf(stderr, "Couldn't realloc() widgets\n");
exit(1);
}
widgets = widgets_temp;
char **suggestions_temp = realloc(suggestions, sizeof(*suggestions) * n);
if (suggestions_temp == NULL && suggestions == NULL) {
fprintf(stderr, "Couldn't realloc() suggestions\n");
exit(1);
}
suggestions = suggestions_temp;
}
if (!n) {
gdk_color_parse(completionbgcolor[1], &color);
gtk_widget_modify_bg(table, GTK_STATE_NORMAL, &color);
el = gtk_label_new(NULL);
gtk_misc_set_alignment(GTK_MISC(el), 0, 0);
markup = g_strconcat("<span font=\"", completionfont[1], "\" color=\"", completioncolor[1], "\">No Completions</span>", NULL);
gtk_label_set_markup(GTK_LABEL(el), markup);
g_free(markup);
gtk_box_pack_start(_table, GTK_WIDGET(el), FALSE, FALSE, 0);
}
gtk_box_pack_start(box, GTK_WIDGET(top_border), FALSE, FALSE, 0);
gtk_container_add(GTK_CONTAINER(table), GTK_WIDGET(_table));
gtk_box_pack_start(box, GTK_WIDGET(table), FALSE, FALSE, 0);
gtk_widget_show_all(GTK_WIDGET(window));
if (!n)
return TRUE;
current = arg->i == DirectionPrev ? n - 1 : 0;
}
if (current != -1) {
gdk_color_parse(completionbgcolor[2], &color);
gtk_widget_modify_bg(GTK_WIDGET(widgets[current]), GTK_STATE_NORMAL, &color);
s = g_strconcat(":", suggestions[current], NULL);
gtk_entry_set_text(GTK_ENTRY(inputbox), s);
g_free(s);
} else
gtk_entry_set_text(GTK_ENTRY(inputbox), prefix);
gtk_editable_set_position(GTK_EDITABLE(inputbox), -1);
return TRUE;
}
gboolean
descend(const Arg *arg) {
char *source = (char*)webkit_web_view_get_uri(webview), *p = &source[0], *new;
int i, len;
count = count ? count : 1;
if (!source)
return TRUE;
if (arg->i == Rootdir) {
for (i = 0; i < 3; i++) /* get to the third slash */
if (!(p = strchr(++p, '/')))
return TRUE; /* if we cannot find it quit */
} else {
len = strlen(source);
if (!len) /* if string is empty quit */
return TRUE;
p = source + len; /* start at the end */
if (*(p - 1) == '/') /* /\/$/ is not an additional level */
++count;
for (i = 0; i < count; i++)
while(*(p--) != '/' || *p == '/') /* count /\/+/ as one slash */
if (p == source) /* if we reach the first char pointer quit */
return TRUE;
++p; /* since we do p-- in the while, we are pointing at
the char before the slash, so +1 */
}
len = p - source + 1; /* new length = end - start + 1 */
new = malloc(len + 1);
memcpy(new, source, len);
new[len] = '\0';
webkit_web_view_load_uri(webview, new);
free(new);
return TRUE;
}
gboolean
echo(const Arg *arg) {
int index = !arg->s ? 0 : arg->i & (~NoAutoHide);
if (index < Info || index > Error)
return TRUE;
set_widget_font_and_color(inputbox, urlboxfont[index], urlboxbgcolor[index], urlboxcolor[index]);
gtk_entry_set_text(GTK_ENTRY(inputbox), !arg->s ? "" : arg->s);
return TRUE;
}
gboolean
input(const Arg *arg) {
int pos = 0;
count = 0;
const char *url;
int index = Info;
Arg a;
/* if inputbox hidden, show it again */
if (!gtk_widget_get_visible(inputbox))
gtk_widget_set_visible(inputbox, TRUE);
update_state();
/* Set the colour and font back to the default, so that we don't still
* maintain a red colour from a warning from an end of search indicator,
* etc.
*/
set_widget_font_and_color(inputbox, urlboxfont[index], urlboxbgcolor[index], urlboxcolor[index]);
if (arg->s[0] == '.' || arg->s[0] == ',') {
mode = ModeHints;
memset(followTarget, 0, 8);
strncpy(followTarget, arg->s[0] == '.' ? "current" : "new", 8);
a.i = Silent;
a.s = g_strdup("vimprobable_show_hints()");
script(&a);
}
/* to avoid things like :open URL :open URL2 or :open :open URL */
gtk_entry_set_text(GTK_ENTRY(inputbox), "");
gtk_editable_insert_text(GTK_EDITABLE(inputbox), arg->s, -1, &pos);
if (arg->i & InsertCurrentURL && (url = webkit_web_view_get_uri(webview)))
gtk_editable_insert_text(GTK_EDITABLE(inputbox), url, -1, &pos);
gtk_widget_grab_focus(inputbox);
gtk_editable_set_position(GTK_EDITABLE(inputbox), -1);
return TRUE;
}
gboolean
navigate(const Arg *arg) {
if (arg->i & NavigationForwardBack)
webkit_web_view_go_back_or_forward(webview, (arg->i == NavigationBack ? -1 : 1) * (count ? count : 1));
else if (arg->i & NavigationReloadActions)
(arg->i == NavigationReload ? webkit_web_view_reload : webkit_web_view_reload_bypass_cache)(webview);
else
webkit_web_view_stop_loading(webview);
return TRUE;
}
gboolean
number(const Arg *arg) {
const char *source = webkit_web_view_get_uri(webview);
char *uri, *p, *new;
int number, diff = (count ? count : 1) * (arg->i == Increment ? 1 : -1);
if (!source)
return TRUE;
uri = g_strdup_printf(source); /* copy string */
p =& uri[0];
while(*p != '\0') /* goto the end of the string */
++p;
--p;
while(*p >= '0' && *p <= '9') /* go back until non number char is reached */
--p;
if (*(++p) == '\0') { /* if no numbers were found abort */
free(uri);
return TRUE;
}
number = atoi(p) + diff; /* apply diff on number */
*p = '\0';
new = g_strdup_printf("%s%d", uri, number); /* create new uri */
webkit_web_view_load_uri(webview, new);
g_free(new);
free(uri);
return TRUE;
}
gboolean
open_arg(const Arg *arg) {
char *argv[6];
char *s = arg->s, *p = NULL, *new;
Arg a = { .i = NavigationReload };
int len;
char *search_uri, *search_term;
if (embed) {
argv[0] = *args;
argv[1] = "-e";
argv[2] = winid;
argv[3] = arg->s;
argv[4] = NULL;
} else {
argv[0] = *args;