forked from bakkeby/dmenu-flexipatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmenu.c
2279 lines (2178 loc) · 58.1 KB
/
dmenu.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
/* See LICENSE file for copyright and license details. */
#include <ctype.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#ifdef XINERAMA
#include <X11/extensions/Xinerama.h>
#endif
#include <X11/Xft/Xft.h>
#include "patches.h"
/* Patch incompatibility overrides */
#if MULTI_SELECTION_PATCH
#undef NON_BLOCKING_STDIN_PATCH
#undef PIPEOUT_PATCH
#undef PRINTINPUTTEXT_PATCH
#endif // MULTI_SELECTION_PATCH
#include "drw.h"
#include "util.h"
#if GRIDNAV_PATCH
#include <stdbool.h>
#endif // GRIDNAV_PATCH
/* macros */
#define INTERSECT(x,y,w,h,r) (MAX(0, MIN((x)+(w),(r).x_org+(r).width) - MAX((x),(r).x_org)) \
* MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org)))
#if PANGO_PATCH
#define TEXTW(X) (drw_font_getwidth(drw, (X), False) + lrpad)
#define TEXTWM(X) (drw_font_getwidth(drw, (X), True) + lrpad)
#else
#define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
#endif // PANGO_PATCH
#if ALPHA_PATCH
#define OPAQUE 0xffU
#define OPACITY "_NET_WM_WINDOW_OPACITY"
#endif // ALPHA_PATCH
/* enums */
enum {
SchemeNorm,
SchemeSel,
SchemeOut,
#if BORDER_PATCH
SchemeBorder,
#endif // BORDER_PATCH
#if MORECOLOR_PATCH
SchemeMid,
#endif // MORECOLOR_PATCH
#if HIGHLIGHT_PATCH
SchemeNormHighlight,
SchemeSelHighlight,
#endif // HIGHLIGHT_PATCH
#if HIGHPRIORITY_PATCH
SchemeHp,
#endif // HIGHPRIORITY_PATCH
#if EMOJI_HIGHLIGHT_PATCH
SchemeHover,
SchemeGreen,
SchemeYellow,
SchemeBlue,
SchemePurple,
SchemeRed,
#endif // EMOJI_HIGHLIGHT_PATCH
SchemeLast,
}; /* color schemes */
struct item {
char *text;
#if SEPARATOR_PATCH
char *text_output;
#elif TSV_PATCH
char *stext;
#endif // SEPARATOR_PATCH | TSV_PATCH
struct item *left, *right;
#if NON_BLOCKING_STDIN_PATCH
struct item *next;
#endif // NON_BLOCKING_STDIN_PATCH
#if MULTI_SELECTION_PATCH
int id; /* for multiselect */
#else
int out;
#endif // MULTI_SELECTION_PATCH
#if HIGHPRIORITY_PATCH
int hp;
#endif // HIGHPRIORITY_PATCH
#if FUZZYMATCH_PATCH
double distance;
#endif // FUZZYMATCH_PATCH
#if PRINTINDEX_PATCH
int index;
#endif // PRINTINDEX_PATCH
};
static char text[BUFSIZ] = "";
#if PIPEOUT_PATCH
static char pipeout[8] = " | dmenu";
#endif // PIPEOUT_PATCH
static char *embed;
#if SEPARATOR_PATCH
static char separator;
static int separator_greedy;
static int separator_reverse;
#endif // SEPARATOR_PATCH
static int bh, mw, mh;
#if XYW_PATCH
static int dmx = 0, dmy = 0; /* put dmenu at these x and y offsets */
static unsigned int dmw = 0; /* make dmenu this wide */
#endif // XYW_PATCH
static int inputw = 0, promptw;
#if PASSWORD_PATCH
static int passwd = 0;
#endif // PASSWORD_PATCH
static int lrpad; /* sum of left and right padding */
#if BARPADDING_PATCH
static int vp; /* vertical padding for bar */
static int sp; /* side padding for bar */
#endif // BARPADDING_PATCH
#if REJECTNOMATCH_PATCH
static int reject_no_match = 0;
#endif // REJECTNOMATCH_PATCH
static size_t cursor;
static struct item *items = NULL;
static struct item *matches, *matchend;
static struct item *prev, *curr, *next, *sel;
static int mon = -1, screen;
#if PRINTINDEX_PATCH
static int print_index = 0;
#endif // PRINTINDEX_PATCH
#if MANAGED_PATCH
static int managed = 0;
#endif // MANAGED_PATCH
#if MULTI_SELECTION_PATCH
static int *selid = NULL;
static unsigned int selidsize = 0;
#endif // MULTI_SELECTION_PATCH
#if NO_SORT_PATCH
static unsigned int sortmatches = 1;
#endif // NO_SORT_PATCH
#if PRINTINPUTTEXT_PATCH
static int use_text_input = 0;
#endif // PRINTINPUTTEXT_PATCH
#if PRESELECT_PATCH
static unsigned int preselected = 0;
#endif // PRESELECT_PATCH
#if EMOJI_HIGHLIGHT_PATCH
static int commented = 0;
static int animated = 0;
#endif // EMOJI_HIGHLIGHT_PATCH
static Atom clip, utf8;
#if WMTYPE_PATCH
static Atom type, dock;
#endif // WMTYPE_PATCH
static Display *dpy;
static Window root, parentwin, win;
static XIC xic;
#if ALPHA_PATCH
static int useargb = 0;
static Visual *visual;
static int depth;
static Colormap cmap;
#endif // ALPHA_PATCH
static Drw *drw;
static Clr *scheme[SchemeLast];
#include "patch/include.h"
#include "config.h"
static unsigned int
textw_clamp(const char *str, unsigned int n)
{
unsigned int w = drw_fontset_getwidth_clamp(drw, str, n) + lrpad;
return MIN(w, n);
}
static void appenditem(struct item *item, struct item **list, struct item **last);
static void calcoffsets(void);
static void cleanup(void);
static char * cistrstr(const char *s, const char *sub);
static int drawitem(struct item *item, int x, int y, int w);
static void drawmenu(void);
static void grabfocus(void);
static void grabkeyboard(void);
static void match(void);
static void insert(const char *str, ssize_t n);
static size_t nextrune(int inc);
static void movewordedge(int dir);
static void keypress(XKeyEvent *ev);
static void paste(void);
#if ALPHA_PATCH
static void xinitvisual(void);
#endif // ALPHA_PATCH
static void readstdin(void);
static void run(void);
static void setup(void);
static void usage(void);
#if CASEINSENSITIVE_PATCH
static int (*fstrncmp)(const char *, const char *, size_t) = strncasecmp;
static char *(*fstrstr)(const char *, const char *) = cistrstr;
#else
static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
static char *(*fstrstr)(const char *, const char *) = strstr;
#endif // CASEINSENSITIVE_PATCH
#include "patch/include.c"
static void
appenditem(struct item *item, struct item **list, struct item **last)
{
if (*last)
(*last)->right = item;
else
*list = item;
item->left = *last;
item->right = NULL;
*last = item;
}
static void
calcoffsets(void)
{
int i, n, rpad = 0;
if (lines > 0) {
#if GRID_PATCH
if (columns)
n = lines * columns * bh;
else
n = lines * bh;
#else
n = lines * bh;
#endif // GRID_PATCH
} else {
#if NUMBERS_PATCH
rpad = TEXTW(numbers);
#endif // NUMBERS_PATCH
#if SYMBOLS_PATCH
n = mw - (promptw + inputw + TEXTW(symbol_1) + TEXTW(symbol_2) + rpad);
#else
n = mw - (promptw + inputw + TEXTW("<") + TEXTW(">") + rpad);
#endif // SYMBOLS_PATCH
}
/* calculate which items will begin the next page and previous page */
for (i = 0, next = curr; next; next = next->right)
if ((i += (lines > 0) ? bh : textw_clamp(next->text, n)) > n)
break;
for (i = 0, prev = curr; prev && prev->left; prev = prev->left)
if ((i += (lines > 0) ? bh : textw_clamp(prev->left->text, n)) > n)
break;
}
static void
cleanup(void)
{
size_t i;
XUngrabKey(dpy, AnyKey, AnyModifier, root);
#if INPUTMETHOD_PATCH
XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
#endif // INPUTMETHOD_PATCH
for (i = 0; i < SchemeLast; i++)
free(scheme[i]);
for (i = 0; items && items[i].text; ++i)
free(items[i].text);
free(items);
#if HIGHPRIORITY_PATCH
for (i = 0; i < hplength; ++i)
free(hpitems[i]);
free(hpitems);
#endif // HIGHPRIORITY_PATCH
drw_free(drw);
XSync(dpy, False);
XCloseDisplay(dpy);
#if MULTI_SELECTION_PATCH
free(selid);
#endif // MULTI_SELECTION_PATCH
}
static char *
cistrstr(const char *s, const char *sub)
{
size_t len;
for (len = strlen(sub); *s; s++)
if (!strncasecmp(s, sub, len))
return (char *)s;
return NULL;
}
static int
drawitem(struct item *item, int x, int y, int w)
{
int r;
#if TSV_PATCH && !SEPARATOR_PATCH
char *text = item->stext;
#else
char *text = item->text;
#endif // TSV_PATCH
#if EMOJI_HIGHLIGHT_PATCH
int iscomment = 0;
if (text[0] == '>') {
if (text[1] == '>') {
iscomment = 3;
switch (text[2]) {
case 'r':
drw_setscheme(drw, scheme[SchemeRed]);
break;
case 'g':
drw_setscheme(drw, scheme[SchemeGreen]);
break;
case 'y':
drw_setscheme(drw, scheme[SchemeYellow]);
break;
case 'b':
drw_setscheme(drw, scheme[SchemeBlue]);
break;
case 'p':
drw_setscheme(drw, scheme[SchemePurple]);
break;
#if HIGHLIGHT_PATCH
case 'h':
drw_setscheme(drw, scheme[SchemeNormHighlight]);
break;
#endif // HIGHLIGHT_PATCH
case 's':
drw_setscheme(drw, scheme[SchemeSel]);
break;
default:
iscomment = 1;
drw_setscheme(drw, scheme[SchemeNorm]);
break;
}
} else {
drw_setscheme(drw, scheme[SchemeNorm]);
iscomment = 1;
}
} else if (text[0] == ':') {
iscomment = 2;
if (item == sel) {
switch (text[1]) {
case 'r':
drw_setscheme(drw, scheme[SchemeRed]);
break;
case 'g':
drw_setscheme(drw, scheme[SchemeGreen]);
break;
case 'y':
drw_setscheme(drw, scheme[SchemeYellow]);
break;
case 'b':
drw_setscheme(drw, scheme[SchemeBlue]);
break;
case 'p':
drw_setscheme(drw, scheme[SchemePurple]);
break;
#if HIGHLIGHT_PATCH
case 'h':
drw_setscheme(drw, scheme[SchemeNormHighlight]);
break;
#endif // HIGHLIGHT_PATCH
case 's':
drw_setscheme(drw, scheme[SchemeSel]);
break;
default:
drw_setscheme(drw, scheme[SchemeSel]);
iscomment = 0;
break;
}
} else {
drw_setscheme(drw, scheme[SchemeNorm]);
}
}
#endif // EMOJI_HIGHLIGHT_PATCH
#if EMOJI_HIGHLIGHT_PATCH
int temppadding = 0;
if (iscomment == 2) {
if (text[2] == ' ') {
#if PANGO_PATCH
temppadding = drw->font->h * 3;
#else
temppadding = drw->fonts->h * 3;
#endif // PANGO_PATCH
animated = 1;
char dest[1000];
strcpy(dest, text);
dest[6] = '\0';
drw_text(drw, x, y
, temppadding
#if LINE_HEIGHT_PATCH
, MAX(lineheight, bh)
#else
, bh
#endif // LINE_HEIGHT_PATCH
, temppadding / 2.6
, dest + 3
, 0
#if PANGO_PATCH
, True
#endif // PANGO_PATCH
);
iscomment = 6;
drw_setscheme(drw, sel == item ? scheme[SchemeHover] : scheme[SchemeNorm]);
}
}
char *output;
if (commented) {
static char onestr[2];
onestr[0] = text[0];
onestr[1] = '\0';
output = onestr;
} else {
output = text;
}
#endif // EMOJI_HIGHLIGHT_PATCH
if (item == sel)
drw_setscheme(drw, scheme[SchemeSel]);
#if HIGHPRIORITY_PATCH
else if (item->hp)
drw_setscheme(drw, scheme[SchemeHp]);
#endif // HIGHPRIORITY_PATCH
#if MORECOLOR_PATCH
else if (item->left == sel || item->right == sel)
drw_setscheme(drw, scheme[SchemeMid]);
#endif // MORECOLOR_PATCH
#if MULTI_SELECTION_PATCH
else if (issel(item->id))
#else
else if (item->out)
#endif // MULTI_SELECTION_PATCH
drw_setscheme(drw, scheme[SchemeOut]);
else
drw_setscheme(drw, scheme[SchemeNorm]);
r = drw_text(drw
#if EMOJI_HIGHLIGHT_PATCH
, x + ((iscomment == 6) ? temppadding : 0)
#else
, x
#endif // EMOJI_HIGHLIGHT_PATCH
, y
, w
, bh
#if EMOJI_HIGHLIGHT_PATCH
, commented ? (bh - TEXTW(output) - lrpad) / 2 : lrpad / 2
#else
, lrpad / 2
#endif // EMOJI_HIGHLIGHT_PATCH
#if EMOJI_HIGHLIGHT_PATCH
, output + iscomment
#else
, text
#endif // EMOJI_HIGHLIGHT_PATCH
, 0
#if PANGO_PATCH
, True
#endif // PANGO_PATCH
);
#if HIGHLIGHT_PATCH
#if EMOJI_HIGHLIGHT_PATCH
drawhighlights(item, output + iscomment, x + ((iscomment == 6) ? temppadding : 0), y, w);
#else
drawhighlights(item, x, y, w);
#endif // EMOJI_HIGHLIGHT_PATCH
#endif // HIGHLIGHT_PATCH
return r;
}
static void
drawmenu(void)
{
#if SCROLL_PATCH
static int curpos, oldcurlen;
int curlen, rcurlen;
#else
unsigned int curpos;
#endif // SCROLL_PATCH
struct item *item;
int x = 0, y = 0, w, rpad = 0, itw = 0, stw = 0;
#if LINE_HEIGHT_PATCH && PANGO_PATCH
int fh = drw->font->h;
#elif LINE_HEIGHT_PATCH
int fh = drw->fonts->h;
#endif // LINE_HEIGHT_PATCH
#if PASSWORD_PATCH
char *censort;
#endif // PASSWORD_PATCH
drw_setscheme(drw, scheme[SchemeNorm]);
drw_rect(drw, 0, 0, mw, mh, 1, 1);
if (prompt && *prompt) {
#if !PLAIN_PROMPT_PATCH
drw_setscheme(drw, scheme[SchemeSel]);
#endif // PLAIN_PROMPT_PATCH
x = drw_text(drw, x, 0, promptw, bh, lrpad / 2, prompt, 0
#if PANGO_PATCH
, True
#endif // PANGO_PATCH
);
}
/* draw input field */
w = (lines > 0 || !matches) ? mw - x : inputw;
#if SCROLL_PATCH
w -= lrpad / 2;
x += lrpad / 2;
rcurlen = TEXTW(text + cursor) - lrpad;
curlen = TEXTW(text) - lrpad - rcurlen;
curpos += curlen - oldcurlen;
curpos = MIN(w, MAX(0, curpos));
curpos = MAX(curpos, w - rcurlen);
curpos = MIN(curpos, curlen);
oldcurlen = curlen;
drw_setscheme(drw, scheme[SchemeNorm]);
#if PASSWORD_PATCH
if (passwd) {
censort = ecalloc(1, sizeof(text));
memset(censort, '.', strlen(text));
drw_text_align(drw, x, 0, curpos, bh, censort, cursor, AlignR);
drw_text_align(drw, x + curpos, 0, w - curpos, bh, censort + cursor, strlen(censort) - cursor, AlignL);
free(censort);
} else {
drw_text_align(drw, x, 0, curpos, bh, text, cursor, AlignR);
drw_text_align(drw, x + curpos, 0, w - curpos, bh, text + cursor, strlen(text) - cursor, AlignL);
}
#else
drw_text_align(drw, x, 0, curpos, bh, text, cursor, AlignR);
drw_text_align(drw, x + curpos, 0, w - curpos, bh, text + cursor, strlen(text) - cursor, AlignL);
#endif // PASSWORD_PATCH
#if LINE_HEIGHT_PATCH
drw_rect(drw, x + curpos - 1, 2 + (bh-fh)/2, 2, fh - 4, 1, 0);
#else
drw_rect(drw, x + curpos - 1, 2, 2, bh - 4, 1, 0);
#endif // LINE_HEIGHT_PATCH
#else // !SCROLL_PATCH
drw_setscheme(drw, scheme[SchemeNorm]);
#if PASSWORD_PATCH
if (passwd) {
censort = ecalloc(1, sizeof(text));
memset(censort, '.', strlen(text));
drw_text(drw, x, 0, w, bh, lrpad / 2, censort, 0
#if PANGO_PATCH
, False
#endif // PANGO_PATCH
);
drw_text(drw, x, 0, w, bh, lrpad / 2, censort, 0
#if PANGO_PATCH
, False
#endif // PANGO_PATCH
);
free(censort);
} else {
drw_text(drw, x, 0, w, bh, lrpad / 2, text, 0
#if PANGO_PATCH
, False
#endif // PANGO_PATCH
);
}
#else
drw_text(drw, x, 0, w, bh, lrpad / 2, text, 0
#if PANGO_PATCH
, False
#endif // PANGO_PATCH
);
#endif // PASSWORD_PATCH
curpos = TEXTW(text) - TEXTW(&text[cursor]);
if ((curpos += lrpad / 2 - 1) < w) {
drw_setscheme(drw, scheme[SchemeNorm]);
#if CARET_WIDTH_PATCH && LINE_HEIGHT_PATCH
drw_rect(drw, x + curpos, 2 + (bh-fh)/2, caret_width, fh - 4, 1, 0);
#elif CARET_WIDTH_PATCH
drw_rect(drw, x + curpos, 2, caret_width, bh - 4, 1, 0);
#elif LINE_HEIGHT_PATCH
drw_rect(drw, x + curpos, 2 + (bh-fh)/2, 2, fh - 4, 1, 0);
#else
drw_rect(drw, x + curpos, 2, 2, bh - 4, 1, 0);
#endif // LINE_HEIGHT_PATCH
}
#endif // SCROLL_PATCH
#if NUMBERS_PATCH
recalculatenumbers();
rpad = TEXTW(numbers);
#if BARPADDING_PATCH
rpad += 2 * sp;
#endif // BARPADDING_PATCH
#if BORDER_PATCH
rpad += border_width;
#endif // BORDER_PATCH
#endif // NUMBERS_PATCH
if (lines > 0) {
#if GRID_PATCH
/* draw grid */
int i = 0;
for (item = curr; item != next; item = item->right, i++)
if (columns)
#if VERTFULL_PATCH
drawitem(
item,
0 + ((i / lines) * (mw / columns)),
y + (((i % lines) + 1) * bh),
mw / columns
);
#else
drawitem(
item,
x + ((i / lines) * ((mw - x) / columns)),
y + (((i % lines) + 1) * bh),
(mw - x) / columns
);
#endif // VERTFULL_PATCH
else
#if VERTFULL_PATCH
drawitem(item, 0, y += bh, mw);
#else
drawitem(item, x, y += bh, mw - x);
#endif // VERTFULL_PATCH
#else
/* draw vertical list */
for (item = curr; item != next; item = item->right)
#if VERTFULL_PATCH
drawitem(item, 0, y += bh, mw);
#else
drawitem(item, x, y += bh, mw - x);
#endif // VERTFULL_PATCH
#endif // GRID_PATCH
} else if (matches) {
/* draw horizontal list */
x += inputw;
#if SYMBOLS_PATCH
w = TEXTW(symbol_1);
#else
w = TEXTW("<");
#endif // SYMBOLS_PATCH
if (curr->left) {
drw_setscheme(drw, scheme[SchemeNorm]);
#if SYMBOLS_PATCH
drw_text(drw, x, 0, w, bh, lrpad / 2, symbol_1, 0
#else
drw_text(drw, x, 0, w, bh, lrpad / 2, "<", 0
#endif // SYMBOLS_PATCH
#if PANGO_PATCH
, True
#endif // PANGO_PATCH
);
}
x += w;
for (item = curr; item != next; item = item->right) {
#if SYMBOLS_PATCH
stw = TEXTW(symbol_2);
#else
stw = TEXTW(">");
#endif // SYMBOLS_PATCH
#if TSV_PATCH && !SEPARATOR_PATCH
itw = textw_clamp(item->stext, mw - x - stw - rpad);
#else
itw = textw_clamp(item->text, mw - x - stw - rpad);
#endif // TSV_PATCH
x = drawitem(item, x, 0, itw);
}
if (next) {
#if SYMBOLS_PATCH
w = TEXTW(symbol_2);
#else
w = TEXTW(">");
#endif // SYMBOLS_PATCH
drw_setscheme(drw, scheme[SchemeNorm]);
drw_text(drw, mw - w - rpad, 0, w, bh, lrpad / 2
#if SYMBOLS_PATCH
, symbol_2
#else
, ">"
#endif // SYMBOLS_PATCH
, 0
#if PANGO_PATCH
, True
#endif // PANGO_PATCH
);
}
}
#if NUMBERS_PATCH
drw_setscheme(drw, scheme[SchemeNorm]);
#if PANGO_PATCH
drw_text(drw, mw - rpad, 0, TEXTW(numbers), bh, lrpad / 2, numbers, 0, False);
#else
drw_text(drw, mw - rpad, 0, TEXTW(numbers), bh, lrpad / 2, numbers, 0);
#endif // PANGO_PATCH
#endif // NUMBERS_PATCH
drw_map(drw, win, 0, 0, mw, mh);
#if NON_BLOCKING_STDIN_PATCH
XFlush(dpy);
#endif // NON_BLOCKING_STDIN_PATCH
}
static void
grabfocus(void)
{
struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000 };
Window focuswin;
int i, revertwin;
for (i = 0; i < 100; ++i) {
XGetInputFocus(dpy, &focuswin, &revertwin);
if (focuswin == win)
return;
#if !MANAGED_PATCH
XSetInputFocus(dpy, win, RevertToParent, CurrentTime);
#endif // MANAGED_PATCH
nanosleep(&ts, NULL);
}
die("cannot grab focus");
}
static void
grabkeyboard(void)
{
struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
int i;
#if MANAGED_PATCH
if (embed || managed)
#else
if (embed)
#endif // MANAGED_PATCH
return;
/* try to grab keyboard, we may have to wait for another process to ungrab */
for (i = 0; i < 1000; i++) {
if (XGrabKeyboard(dpy, DefaultRootWindow(dpy), True, GrabModeAsync,
GrabModeAsync, CurrentTime) == GrabSuccess)
return;
nanosleep(&ts, NULL);
}
die("cannot grab keyboard");
}
static void
match(void)
{
#if DYNAMIC_OPTIONS_PATCH
if (dynamic && *dynamic)
refreshoptions();
#endif // DYNAMIC_OPTIONS_PATCH
#if FUZZYMATCH_PATCH
if (fuzzy) {
fuzzymatch();
return;
}
#endif
static char **tokv = NULL;
static int tokn = 0;
char buf[sizeof text], *s;
int i, tokc = 0;
size_t len, textsize;
struct item *item, *lprefix, *lsubstr, *prefixend, *substrend;
#if HIGHPRIORITY_PATCH
struct item *lhpprefix, *hpprefixend;
#endif // HIGHPRIORITY_PATCH
#if NON_BLOCKING_STDIN_PATCH
int preserve = 0;
#endif // NON_BLOCKING_STDIN_PATCH
strcpy(buf, text);
/* separate input text into tokens to be matched individually */
for (s = strtok(buf, " "); s; tokv[tokc - 1] = s, s = strtok(NULL, " "))
if (++tokc > tokn && !(tokv = realloc(tokv, ++tokn * sizeof *tokv)))
die("cannot realloc %zu bytes:", tokn * sizeof *tokv);
len = tokc ? strlen(tokv[0]) : 0;
#if PREFIXCOMPLETION_PATCH
if (use_prefix) {
matches = lprefix = matchend = prefixend = NULL;
textsize = strlen(text);
} else {
matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL;
textsize = strlen(text) + 1;
}
#else
matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL;
textsize = strlen(text) + 1;
#endif // PREFIXCOMPLETION_PATCH
#if HIGHPRIORITY_PATCH
lhpprefix = hpprefixend = NULL;
#endif // HIGHPRIORITY_PATCH
#if NON_BLOCKING_STDIN_PATCH && DYNAMIC_OPTIONS_PATCH
for (item = items; item && (!(dynamic && *dynamic) || item->text); item = (dynamic && *dynamic) ? item + 1 : item->next)
#elif NON_BLOCKING_STDIN_PATCH
for (item = items; item; item = item->next)
#else
for (item = items; item && item->text; item++)
#endif
{
for (i = 0; i < tokc; i++)
if (!fstrstr(item->text, tokv[i]))
break;
#if DYNAMIC_OPTIONS_PATCH
if (i != tokc && !(dynamic && *dynamic)) /* not all tokens match */
continue;
#else
if (i != tokc) /* not all tokens match */
continue;
#endif // DYNAMIC_OPTIONS_PATCH
#if HIGHPRIORITY_PATCH
/* exact matches go first, then prefixes with high priority, then prefixes, then substrings */
#else
/* exact matches go first, then prefixes, then substrings */
#endif // HIGHPRIORITY_PATCH
#if NO_SORT_PATCH
if (!sortmatches)
appenditem(item, &matches, &matchend);
else
#endif // NO_SORT_PATCH
if (!tokc || !fstrncmp(text, item->text, textsize))
appenditem(item, &matches, &matchend);
#if HIGHPRIORITY_PATCH
else if (item->hp && !fstrncmp(tokv[0], item->text, len))
appenditem(item, &lhpprefix, &hpprefixend);
#endif // HIGHPRIORITY_PATCH
else if (!fstrncmp(tokv[0], item->text, len))
appenditem(item, &lprefix, &prefixend);
#if PREFIXCOMPLETION_PATCH
else if (!use_prefix)
#else
else
#endif // PREFIXCOMPLETION_PATCH
appenditem(item, &lsubstr, &substrend);
#if NON_BLOCKING_STDIN_PATCH
if (sel == item)
preserve = 1;
#endif // NON_BLOCKING_STDIN_PATCH
}
#if HIGHPRIORITY_PATCH
if (lhpprefix) {
if (matches) {
matchend->right = lhpprefix;
lhpprefix->left = matchend;
} else
matches = lhpprefix;
matchend = hpprefixend;
}
#endif // HIGHPRIORITY_PATCH
if (lprefix) {
if (matches) {
matchend->right = lprefix;
lprefix->left = matchend;
} else
matches = lprefix;
matchend = prefixend;
}
#if PREFIXCOMPLETION_PATCH
if (!use_prefix && lsubstr)
#else
if (lsubstr)
#endif // PREFIXCOMPLETION_PATCH
{
if (matches) {
matchend->right = lsubstr;
lsubstr->left = matchend;
} else
matches = lsubstr;
matchend = substrend;
}
#if NON_BLOCKING_STDIN_PATCH
if (!preserve)
#endif // NON_BLOCKING_STDIN_PATCH
curr = sel = matches;
#if INSTANT_PATCH
if (instant && matches && matches==matchend && !lsubstr) {
puts(matches->text);
cleanup();
exit(0);
}
#endif // INSTANT_PATCH
calcoffsets();
}
static void
insert(const char *str, ssize_t n)
{
if (strlen(text) + n > sizeof text - 1)
return;
#if REJECTNOMATCH_PATCH
static char last[BUFSIZ] = "";
if (reject_no_match) {
/* store last text value in case we need to revert it */
memcpy(last, text, BUFSIZ);
}
#endif // REJECTNOMATCH_PATCH
/* move existing text out of the way, insert new text, and update cursor */
memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0));
if (n > 0)
memcpy(&text[cursor], str, n);
cursor += n;
match();
#if REJECTNOMATCH_PATCH
if (!matches && reject_no_match) {
/* revert to last text value if theres no match */
memcpy(text, last, BUFSIZ);
cursor -= n;
match();
}
#endif // REJECTNOMATCH_PATCH
}
static size_t
nextrune(int inc)
{
ssize_t n;
/* return location of next utf8 rune in the given direction (+1 or -1) */
for (n = cursor + inc; n + inc >= 0 && (text[n] & 0xc0) == 0x80; n += inc)
;
return n;
}
static void
movewordedge(int dir)
{
if (dir < 0) { /* move cursor to the start of the word*/
while (cursor > 0 && strchr(worddelimiters, text[nextrune(-1)]))
cursor = nextrune(-1);
while (cursor > 0 && !strchr(worddelimiters, text[nextrune(-1)]))
cursor = nextrune(-1);
} else { /* move cursor to the end of the word */
while (text[cursor] && strchr(worddelimiters, text[cursor]))
cursor = nextrune(+1);
while (text[cursor] && !strchr(worddelimiters, text[cursor]))
cursor = nextrune(+1);
}
}
static void
keypress(XKeyEvent *ev)
{
char buf[64];
int len;
#if PREFIXCOMPLETION_PATCH
struct item * item;
#endif // PREFIXCOMPLETION_PATCH
KeySym ksym = NoSymbol;
Status status;
#if GRID_PATCH && GRIDNAV_PATCH
int i;
struct item *tmpsel;
bool offscreen = false;
#endif // GRIDNAV_PATCH
len = XmbLookupString(xic, ev, buf, sizeof buf, &ksym, &status);
switch (status) {
default: /* XLookupNone, XBufferOverflow */
return;
case XLookupChars: /* composed string from input method */
goto insert;
case XLookupKeySym:
case XLookupBoth: /* a KeySym and a string are returned: use keysym */
break;
}
if (ev->state & ControlMask) {
switch(ksym) {
#if FZFEXPECT_PATCH
case XK_a: expect("ctrl-a", ev); ksym = XK_Home; break;
case XK_b: expect("ctrl-b", ev); ksym = XK_Left; break;
case XK_c: expect("ctrl-c", ev); ksym = XK_Escape; break;
case XK_d: expect("ctrl-d", ev); ksym = XK_Delete; break;
case XK_e: expect("ctrl-e", ev); ksym = XK_End; break;
case XK_f: expect("ctrl-f", ev); ksym = XK_Right; break;
case XK_g: expect("ctrl-g", ev); ksym = XK_Escape; break;
case XK_h: expect("ctrl-h", ev); ksym = XK_BackSpace; break;
case XK_i: expect("ctrl-i", ev); ksym = XK_Tab; break;
case XK_j: expect("ctrl-j", ev); ksym = XK_Down; break;
case XK_J:/* fallthrough */
case XK_l: expect("ctrl-l", ev); break;
case XK_m: expect("ctrl-m", ev); /* fallthrough */