-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmenu_migemo.patch
352 lines (338 loc) · 9.27 KB
/
dmenu_migemo.patch
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
diff -uprN dmenu-5.2/config.def.h dmenu-5.2-migemo/config.def.h
--- dmenu-5.2/config.def.h 2022-10-05 02:36:58.000000000 +0900
+++ dmenu-5.2-migemo/config.def.h 2022-10-10 14:21:06.911495888 +0900
@@ -21,3 +21,6 @@ static unsigned int lines = 0;
* for example: " /?\"&[]"
*/
static const char worddelimiters[] = " ";
+
+/* -migemo-dict option; migemo dictionary */
+static const char *migemo_dict = "/usr/share/migemo/utf-8/migemo-dict";
diff -uprN dmenu-5.2/config.mk dmenu-5.2-migemo/config.mk
--- dmenu-5.2/config.mk 2022-10-05 02:36:58.000000000 +0900
+++ dmenu-5.2-migemo/config.mk 2022-10-10 14:21:44.174875266 +0900
@@ -21,7 +21,7 @@ FREETYPEINC = /usr/include/freetype2
# includes and libs
INCS = -I$(X11INC) -I$(FREETYPEINC)
-LIBS = -L$(X11LIB) -lX11 $(XINERAMALIBS) $(FREETYPELIBS)
+LIBS = -L$(X11LIB) -lX11 $(XINERAMALIBS) $(FREETYPELIBS) -lmigemo -lpcre
# flags
CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L -DVERSION=\"$(VERSION)\" $(XINERAMAFLAGS)
diff -uprN dmenu-5.2/dmenu.c dmenu-5.2-migemo/dmenu.c
--- dmenu-5.2/dmenu.c 2022-10-05 02:36:58.000000000 +0900
+++ dmenu-5.2-migemo/dmenu.c 2022-10-10 14:34:06.599156676 +0900
@@ -16,6 +16,9 @@
#endif
#include <X11/Xft/Xft.h>
+#include <pcre.h>
+#include <migemo.h>
+
#include "drw.h"
#include "util.h"
@@ -53,6 +56,9 @@ static XIC xic;
static Drw *drw;
static Clr *scheme[SchemeLast];
+static migemo *mgm;
+static int migemo_mode = 0;
+
#include "config.h"
static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
@@ -227,39 +233,225 @@ grabkeyboard(void)
die("cannot grab keyboard");
}
+/*
+https://github.com/koron/cmigemo
+ charset.c: utf8_int2char
+ rxgen.c: default_int2char
+
+ Copyright (c) 2003-2007 MURAOKA Taro (KoRoN)
+ Released under the MIT license
+ URL: https://github.com/koron/cmigemo/blob/master/doc/LICENSE_MIT.txt
+
+ UTF8 + PCRE用に改変
+*/
+static int
+utf8pcre_int2char(unsigned int in, unsigned char* out)
+{
+ if (in < 0x80) {
+ int len = 0;
+
+ /* outは最低でも16バイトはある、という仮定を置く */
+ switch (in)
+ {
+ case '\\':
+ case '.': case '*': case '^': case '$': case '/':
+ case ' ': case '(': case ')': case '+':
+ case '-': case '?': case '[': case ']': case '{':
+ case '|': case '}':
+ if (out)
+ out[len] = '\\';
+ ++len;
+ default:
+ if (out)
+ out[len] = (unsigned char)(in & 0xFF);
+ ++len;
+ break;
+ }
+ return len;
+ }
+
+ if (in < 0x800)
+ {
+ if (out)
+ {
+ out[0] = 0xc0 + (in >> 6);
+ out[1] = 0x80 + ((in >> 0) & 0x3f);
+ }
+ return 2;
+ }
+ if (in < 0x10000)
+ {
+ if (out)
+ {
+ out[0] = 0xe0 + (in >> 12);
+ out[1] = 0x80 + ((in >> 6) & 0x3f);
+ out[2] = 0x80 + ((in >> 0) & 0x3f);
+ }
+ return 3;
+ }
+ if (in < 0x200000)
+ {
+ if (out)
+ {
+ out[0] = 0xf0 + (in >> 18);
+ out[1] = 0x80 + ((in >> 12) & 0x3f);
+ out[2] = 0x80 + ((in >> 6) & 0x3f);
+ out[3] = 0x80 + ((in >> 0) & 0x3f);
+ }
+ return 4;
+ }
+ if (in < 0x4000000)
+ {
+ if (out)
+ {
+ out[0] = 0xf8 + (in >> 24);
+ out[1] = 0x80 + ((in >> 18) & 0x3f);
+ out[2] = 0x80 + ((in >> 12) & 0x3f);
+ out[3] = 0x80 + ((in >> 6) & 0x3f);
+ out[4] = 0x80 + ((in >> 0) & 0x3f);
+ }
+ return 5;
+ }
+ else
+ {
+ if (out)
+ {
+ out[0] = 0xf8 + (in >> 30);
+ out[1] = 0x80 + ((in >> 24) & 0x3f);
+ out[2] = 0x80 + ((in >> 18) & 0x3f);
+ out[3] = 0x80 + ((in >> 12) & 0x3f);
+ out[4] = 0x80 + ((in >> 6) & 0x3f);
+ out[5] = 0x80 + ((in >> 0) & 0x3f);
+ }
+ return 6;
+ }
+}
+
+static int
+regex_cmp(const char *str, pcre *re, pcre_extra *extra)
+{
+ int ov[9];
+
+ if(re == NULL) return -1;
+
+ if(pcre_exec(re, extra, str, strlen(str), 0, 0, ov, 9) > 0){
+ /* 完全に一致 */
+ if(ov[0] == 0 && strlen(str) == ov[1]) {
+ return 0;
+ }
+
+ /* 頭で一致 */
+ if(ov[0] == 0) {
+ return 1;
+ }
+
+ /* それ以外のどこか */
+ return 2;
+ }
+
+ return -1;
+}
+
static void
match(void)
{
static char **tokv = NULL;
static int tokn = 0;
+ static unsigned char **migemo_tokv = NULL;
+ static pcre **regex_tokv = NULL;
char buf[sizeof text], *s;
- int i, tokc = 0;
+ const char* err;
+ int i, tokc = 0, errofs;
size_t len, textsize;
struct item *item, *lprefix, *lsubstr, *prefixend, *substrend;
+ static pcre_extra **extra = NULL;
+ pcre_jit_stack *jit_stack = 0;
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);
+ for (s = strtok(buf, " "); s; tokv[tokc - 1] = s, s = strtok(NULL, " ")) {
+ if (++tokc > tokn) {
+ if (!(tokv = realloc(tokv, ++tokn * sizeof *tokv))) {
+ die("cannot realloc %u bytes:", tokn * sizeof *tokv);
+ }
+ if (migemo_mode) {
+ if (!(migemo_tokv = realloc(migemo_tokv, tokn * sizeof(*migemo_tokv)))) {
+ die("cannot realloc %u bytes:", tokn * sizeof(*migemo_tokv));
+ }
+ if (!(regex_tokv = realloc(regex_tokv, tokn * sizeof(*regex_tokv)))) {
+ die("cannot realloc %u bytes:", tokn * sizeof(*regex_tokv));
+ }
+ if (!(extra = realloc(extra, tokn * sizeof(*extra)))) {
+ die("cannot realloc %u bytes:", tokn * sizeof(*extra));
+ }
+ }
+ }
+ }
len = tokc ? strlen(tokv[0]) : 0;
+ if (migemo_mode) {
+ jit_stack = pcre_jit_stack_alloc(32 * 1024, 512 * 1024);
+ for (i = 0; i < tokc; i++)
+ migemo_tokv[i] = migemo_query(mgm, (unsigned char*)tokv[i]);
+
+ for (i = 0; i < tokc; i++) {
+ pcre *re = pcre_compile((char*)migemo_tokv[i], PCRE_UTF8|PCRE_CASELESS, &err, &errofs, NULL);
+ extra[i] = pcre_study(re, PCRE_STUDY_JIT_COMPILE, &err);
+ pcre_assign_jit_stack(extra[i], NULL, jit_stack);
+
+ if(re == NULL) {
+ regex_tokv[i] = NULL;
+ continue;
+ }
+ else {
+ regex_tokv[i] = re;
+ }
+ }
+ }
+
matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL;
textsize = strlen(text) + 1;
for (item = items; item && item->text; item++) {
- for (i = 0; i < tokc; i++)
- if (!fstrstr(item->text, tokv[i]))
- break;
+ int tokv0_rval = -1;
+ for (i = 0; i < tokc; i++) {
+ if (migemo_mode) {
+ if (i == 0) {
+ tokv0_rval = regex_cmp(item->text, regex_tokv[i], extra[i]);
+ if (tokv0_rval == -1) break;
+ }
+ else {
+ if (regex_cmp(item->text, regex_tokv[i], extra[i]) == -1) {
+ break;
+ }
+ }
+ }
+ else {
+ if (!fstrstr(item->text, tokv[i]))
+ break;
+ }
+ }
+
if (i != tokc) /* not all tokens match */
continue;
- /* exact matches go first, then prefixes, then substrings */
- if (!tokc || !fstrncmp(text, item->text, textsize))
- appenditem(item, &matches, &matchend);
- else if (!fstrncmp(tokv[0], item->text, len))
- appenditem(item, &lprefix, &prefixend);
- else
- appenditem(item, &lsubstr, &substrend);
+ if (migemo_mode) {
+ if (!tokc || tokv0_rval == 0)
+ appenditem(item, &matches, &matchend);
+ else if (tokv0_rval == 1)
+ appenditem(item, &lprefix, &prefixend);
+ else
+ appenditem(item, &lsubstr, &substrend);
+ }
+ else {
+ /* exact matches go first, then prefixes, then substrings */
+ if (!tokc || !fstrncmp(text, item->text, textsize))
+ appenditem(item, &matches, &matchend);
+ else if (!fstrncmp(tokv[0], item->text, len))
+ appenditem(item, &lprefix, &prefixend);
+ else
+ appenditem(item, &lsubstr, &substrend);
+ }
+
}
if (lprefix) {
if (matches) {
@@ -277,6 +469,16 @@ match(void)
matches = lsubstr;
matchend = substrend;
}
+
+ if (migemo_mode) {
+ for (i = 0; i < tokc; i++) {
+ migemo_release(mgm, migemo_tokv[i]);
+ pcre_free(regex_tokv[i]);
+ pcre_free_study(extra[i]);
+ }
+ pcre_jit_stack_free(jit_stack);
+ }
+
curr = sel = matches;
calcoffsets();
}
@@ -710,7 +912,7 @@ setup(void)
static void
usage(void)
{
- die("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
+ die("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor] [-migemo] [-migemo-dict dict]\n"
" [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]");
}
@@ -729,6 +931,8 @@ main(int argc, char *argv[])
topbar = 0;
else if (!strcmp(argv[i], "-f")) /* grabs keyboard before reading stdin */
fast = 1;
+ else if (!strcmp(argv[i], "-migemo")) /* Migemo */
+ migemo_mode = 1;
else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */
fstrncmp = strncasecmp;
fstrstr = cistrstr;
@@ -753,6 +957,8 @@ main(int argc, char *argv[])
colors[SchemeSel][ColFg] = argv[++i];
else if (!strcmp(argv[i], "-w")) /* embedding window id */
embed = argv[++i];
+ else if (!strcmp(argv[i], "-migemo-dict")) /* migemo dictionary */
+ migemo_dict = argv[++i];
else
usage();
@@ -777,6 +983,12 @@ main(int argc, char *argv[])
die("pledge");
#endif
+ if (migemo_mode) {
+ mgm = migemo_open(migemo_dict);
+ migemo_set_operator(mgm, MIGEMO_OPINDEX_NEST_IN, (unsigned char*)"(?:");
+ migemo_setproc_int2char(mgm, (MIGEMO_PROC_INT2CHAR)(utf8pcre_int2char));
+ }
+
if (fast && !isatty(0)) {
grabkeyboard();
readstdin();
@@ -787,5 +999,9 @@ main(int argc, char *argv[])
setup();
run();
+ if (migemo_mode) {
+ migemo_close(mgm);
+ }
+
return 1; /* unreachable */
}