~kris/suckless

dmenu

ref: 39e0e33c9db0825c6f13bf0ff51d4911704f2394 dmenu/patch/highlight.c -rw-r--r-- 2.7 KiB
39e0e33c — Kris Yotam Disable caseinsensitive patch to restore -i flag 7 months ago
                                                                                
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
static void
#if EMOJI_HIGHLIGHT_PATCH
drawhighlights(struct item *item, char *output, int x, int y, int maxw)
#else
drawhighlights(struct item *item, int x, int y, int maxw)
#endif // EMOJI_HIGHLIGHT_PATCH
{
	char restorechar, tokens[sizeof text], *highlight,  *token;
	int indent, highlightlen;

	#if FUZZYMATCH_PATCH
	int i;
	#endif // FUZZYMATCH_PATCH

	#if EMOJI_HIGHLIGHT_PATCH
	char *itemtext = output;
	#elif TSV_PATCH && !SEPARATOR_PATCH
	char *itemtext = item->stext;
	#else
	char *itemtext = item->text;
	#endif // EMOJI_HIGHLIGHT_PATCH | TSV_PATCH

	if (!(strlen(itemtext) && strlen(text)))
		return;

	/* Do not highlight items scheduled for output */
	#if MULTI_SELECTION_PATCH
	if (issel(item->id))
		return;
	#else
	if (item->out)
		return;
	#endif // MULTI_SELECTION_PATCH

	drw_setscheme(drw, scheme[item == sel ? SchemeSelHighlight : SchemeNormHighlight]);

	#if FUZZYMATCH_PATCH
	if (fuzzy) {
		for (i = 0, highlight = itemtext; *highlight && text[i];) {
			highlightlen = utf8len(highlight);
			#if FUZZYMATCH_PATCH
			if (!fstrncmp(&(*highlight), &text[i], highlightlen))
			#else
			if (*highlight == text[i])
			#endif // FUZZYMATCH_PATCH
			{
				/* get indentation */
				restorechar = *highlight;
				*highlight = '\0';
				indent = TEXTW(itemtext) - lrpad;
				*highlight = restorechar;

				/* highlight character */
				restorechar = highlight[highlightlen];
				highlight[highlightlen] = '\0';
				drw_text(
					drw,
					x + indent + (lrpad / 2),
					y,
					MIN(maxw - indent - lrpad, TEXTW(highlight) - lrpad),
					bh, 0, highlight, 0
					#if PANGO_PATCH
					, True
					#endif // PANGO_PATCH
				);
				highlight[highlightlen] = restorechar;
				i += highlightlen;
			}
			highlight++;
		}
		return;
	}
	#endif // FUZZYMATCH_PATCH

	strcpy(tokens, text);
	for (token = strtok(tokens, " "); token; token = strtok(NULL, " ")) {
		highlight = fstrstr(itemtext, token);
		while (highlight) {
			// Move item str end, calc width for highlight indent, & restore
			highlightlen = highlight - itemtext;
			restorechar = *highlight;
			itemtext[highlightlen] = '\0';
			indent = TEXTW(itemtext);
			itemtext[highlightlen] = restorechar;

			// Move highlight str end, draw highlight, & restore
			restorechar = highlight[strlen(token)];
			highlight[strlen(token)] = '\0';
			if (indent - (lrpad / 2) - 1 < maxw)
				drw_text(
					drw,
					x + indent - (lrpad / 2) - 1,
					y,
					MIN(maxw - indent, TEXTW(highlight) - lrpad),
					bh, 0, highlight, 0
					#if PANGO_PATCH
					, True
					#endif // PANGO_PATCH
				);
			highlight[strlen(token)] = restorechar;

			if (strlen(highlight) - strlen(token) < strlen(token))
				break;
			highlight = fstrstr(highlight + strlen(token), token);
		}
	}
}