2 * Copyright Neil Brown ©2023 <neil@brown.name>
3 * May be distributed under terms of GPLv2 - see file:COPYING
5 * Parse the Unicode NamesList.txt file to find names for
20 static void report_names(struct unicode_data *ud safe, const char *name safe,
22 struct pane *p safe, struct command *c safe)
24 /* name must be start of a word, as either primary or secondary
26 * If "which" is zero, return them all, else only return the
27 * nth one where which==n
29 char *ptn = strconcat(p, "?i:^([0-9A-F]{4,5} | = ).*\\b", name);
35 for (i = 0; i < ud->len; ) {
39 s = call("text-search", p, 0, NULL, ptn,
40 ud->len - i, NULL, ud->names + i);
44 /* i is now the start of the match */
46 eol = strchr(cp, '\n');
49 i = (eol - ud->names) + 1;
53 /* secondary name "\t= "*/
54 n = strndup(cp+3, eol-cp-3);
56 while (cp > ud->names &&
57 (cp[-1] != '\n' || cp[0] == '\t'))
60 /* primary name "XXXXX?\t" */
62 n = strndup(cp+5, eol-cp-5);
64 n = strndup(cp+6, eol-cp-6);
66 ch = strtoul(cp, &eol, 16);
67 if (eol == cp+4 || eol == cp+5) {
69 comm_call(c, "cb", p, ch, NULL, n);
73 comm_call(c, "cb", p, ch, NULL, n);
82 static void unicode_free(struct command *c safe)
84 struct unicode_data *ud = container_of(c, struct unicode_data, c);
87 munmap(ud->names, ud->len);
90 DEF_CMD(unicode_names)
92 struct unicode_data *ud;
93 if (ci->comm == &unicode_names) {
94 /* This is the first call - need to allocate storage,
95 * load the NamesList file, and register a new command.
101 ud->c = unicode_names;
102 ud->c.free = unicode_free;
103 call_comm("global-set-command", ci->home, &ud->c, 0, NULL,
105 p = call_ret(str, "xdg-find-edlib-file", ci->focus, 0, NULL,
106 "NamesList.txt", 0, NULL, "data");
109 fd = open(p, O_RDONLY);
113 ud->len = lseek(fd, 0, 2);
114 ud->names = mmap(NULL, ud->len, PROT_READ, MAP_SHARED, fd, 0);
117 ud = container_of(ci->comm, struct unicode_data, c);
121 if (ci->str && ci->comm2)
122 report_names(ud, ci->str, ci->num, ci->focus, ci->comm2);
126 void edlib_init(struct pane *ed safe)
128 call_comm("global-set-command", ed, &unicode_names,
129 0, NULL, "Unicode-names");