2 * Copyright Neil Brown ©2015-2020 <neil@brown.name>
3 * May be distributed under terms of GPLv2 - see file:COPYING
5 * render-complete - support string completion.
7 * This should be attached between render-lines and the pane which
8 * provides the lines. It is given a prefix and it suppresses all
9 * lines which don't start with the prefix.
10 * All events are redirected to the controlling window (where the text
11 * to be completed is being entered)
13 * This module doesn't hold any marks on any document. The marks
14 * held by the rendered should be sufficient.
17 #define _GNU_SOURCE for strcasestr
24 struct complete_data {
28 const char *substr safe;
33 static struct map *rc_map;
35 DEF_LOOKUP_CMD(complete_handle, rc_map);
40 const char *prefix safe, *str;
43 static const char *add_highlight_prefix(const char *orig, int start, int plen,
44 const char *attr safe)
53 while (start > 0 && *c) {
55 buf_append_byte(&ret, *c++);
56 buf_append_byte(&ret, *c++);
59 buf_concat(&ret, attr);
60 while (plen > 0 && *c) {
62 buf_append_byte(&ret, *c++);
63 buf_append_byte(&ret, *c++);
66 buf_concat(&ret, "</>");
68 return buf_final(&ret);
71 DEF_CB(save_highlighted)
73 struct rlcb *cb = container_of(ci->comm, struct rlcb, c);
79 start = strcasestr(ci->str, cb->prefix);
82 cb->str = add_highlight_prefix(ci->str, start - ci->str, cb->plen, "<fg:red>");
86 DEF_CMD(render_complete_line)
88 struct complete_data *cd = ci->home->data;
95 cb.prefix = cd->stk->substr;
96 cb.plen = strlen(cd->stk->substr);
98 cb.c = save_highlighted;
99 ret = call_comm(ci->key, ci->home->parent, &cb.c, ci->num, ci->mark,
101 if (ret < 0 || !cb.str)
104 ret = comm_call(ci->comm2, "callback:render", ci->focus, 0, NULL, cb.str);
109 DEF_CMD(complete_free)
111 struct complete_data *cd = ci->home->data;
112 struct stk *stk = cd->stk;
117 free((void*)t->substr);
126 static struct pane *complete_pane(struct pane *focus)
128 struct pane *complete;
129 struct complete_data *cd;
132 complete = pane_register(focus, 0, &complete_handle.c, cd);
137 cd->stk = malloc(sizeof(cd->stk[0]));
138 cd->stk->prev = NULL;
139 cd->stk->substr = strdup("");
144 DEF_CMD(complete_clone)
146 struct pane *parent = ci->focus;
147 struct pane *complete;
149 complete = complete_pane(parent);
151 pane_clone_children(ci->home, complete);
155 DEF_CMD(complete_ignore_replace)
160 DEF_CMD(complete_escape)
162 /* submit the original prefix back*/
163 struct complete_data *cd = ci->home->data;
165 /* This pane might be closed before the reply string is used,
166 * so we need to save it.
168 call("popup:close", ci->home->parent, NO_NUMERIC, NULL,
169 strsave(ci->home, cd->orig));
173 DEF_CMD(complete_char)
175 struct complete_data *cd = ci->home->data;
177 int pl = strlen(cd->stk->substr);
178 const char *suffix = ksuffix(ci, "doc:char-");
180 np = malloc(pl + strlen(suffix) + 1);
181 strcpy(np, cd->stk->substr);
182 strcpy(np+pl, suffix);
183 call("Complete:prefix", ci->focus, !cd->prefix_only, NULL, np);
189 struct complete_data *cd = ci->home->data;
190 struct stk *stk = cd->stk;
195 free((void*)stk->substr);
197 call("Complete:prefix", ci->home);
201 static int csame(char a, char b)
210 static int common_len(const char *a safe, const char *b safe)
213 while (*a && csame(*a, *b)) {
221 static void adjust_pre(char *common safe, const char *new safe, int len)
223 int l = strlen(common);
226 while (l && len && csame(common[l-1], new[len-1])) {
232 memmove(common, common+l, newlen+1);
237 struct complete_data *cd safe;
241 /* common_pre is the longest common prefix to 'common' that
242 * appears in all matches in which 'common' appears. It is
243 * allocated with enough space to append 'common' after the
253 struct setcb *cb = container_of(ci->comm, struct setcb, c);
254 struct complete_data *cd = cb->cd;
255 const char *ss = cb->ss;
256 int len = strlen(ss);
257 const char *c = ci->str;
264 if (cd->prefix_only) {
266 if (strncmp(match, ss, len) == 0)
269 match = strcasestr(c, ss);
270 if (strncasecmp(c, ss, len) == 0) {
272 if (strncmp(c, ss, len) == 0)
274 } else if (strstr(c, ss))
279 /* should be impossible */
283 if (l && match[l-1] == '\n')
286 if (this_match > cb->best_match) {
287 /* Only use matches at least this good to calculate
290 cb->best_match = this_match;
293 free(cb->common_pre);
294 cb->common_pre = NULL;
297 if (this_match == cb->best_match) {
298 /* This match can be used for 'common' and
301 mark_free(cb->bestm);
303 cb->bestm = mark_dup(ci->mark);
306 cb->common = strndup(match, l);
308 cb->common[common_len(match, cb->common)] = 0;
309 /* If 'match' and 'common' disagree on case of
310 * 'prefix', use that of 'prefix'
312 if (memcmp(cb->common, match, len) != 0)
313 memcpy(cb->common, ss, len);
315 if (!cb->common_pre) {
316 cb->common_pre = strndup(c, l + match-c);
317 strncpy(cb->common_pre, c, match-c);
318 cb->common_pre[match-c] = 0;
320 adjust_pre(cb->common_pre, c, match-c);
326 DEF_CMD(complete_set_prefix)
328 /* Set the prefix, force a full refresh, and move point
329 * to the first match at start-of-line, or first match
330 * If there is no match, return -1.
331 * Otherwise return number of matches in ->num2 and
332 * the longest common prefix in ->str.
334 struct pane *p = ci->home;
335 struct complete_data *cd = p->data;
340 /* Save a copy of the point so we can restore it if needed */
341 m = call_ret(mark, "doc:point", ci->focus);
349 cb.common_pre = NULL;
354 cd->prefix_only = !ci->num;
356 cb.ss = cd->stk->substr;
359 call_comm("Filter:set", ci->focus, &cb.c,
360 cd->prefix_only ? 3 : 2, NULL, cb.ss);
364 call("Filter:set", ci->focus,
365 cd->prefix_only ? 3 : 2, NULL, cd->stk->substr);
367 call("Move-to", ci->focus, 0, m);
371 if (cb.common_pre && cb.common && cb.cnt && ci->str) {
372 strcat(cb.common_pre, cb.common);
373 stk = malloc(sizeof(*stk));
374 stk->substr = cb.common_pre;
377 cb.common_pre = NULL;
378 call("Filter:set", ci->focus,
379 cd->prefix_only ? 3 : 2, NULL, cd->stk->substr);
380 comm_call(ci->comm2, "callback:prefix", ci->focus, cb.cnt,
381 NULL, cd->stk->substr);
383 cd->orig = strdup(ci->str);
385 comm_call(ci->comm2, "callback:prefix", ci->focus, 0);
390 call("Move-to", ci->focus, 0, cb.bestm);
394 call("view:changed", ci->focus);
401 struct call_return *cr = container_of(ci->comm, struct call_return, c);
402 cr->s = ci->str ? strdup(ci->str) : NULL;
406 DEF_CMD(complete_return)
408 /* submit the selected entry to the popup */
409 struct call_return cr;
418 home_call(ci->home, "doc:render-line",
419 ci->home, NO_NUMERIC, ci->mark, NULL, 0, NULL,
424 if (l && cr.s[l-1] == '\n')
437 while (*c2 && c2[-1] != '>')
442 call("popup:close", ci->home->parent, NO_NUMERIC, NULL,
448 static void register_map(void)
450 rc_map = key_alloc();
452 key_add(rc_map, "doc:render-line", &render_complete_line);
453 key_add(rc_map, "Free", &complete_free);
454 key_add(rc_map, "Clone", &complete_clone);
456 key_add(rc_map, "Replace", &complete_ignore_replace);
457 key_add(rc_map, "K:ESC", &complete_escape);
458 key_add_range(rc_map, "doc:char- ", "doc:char-~", &complete_char);
459 key_add(rc_map, "K:Backspace", &complete_bs);
461 key_add(rc_map, "K:Enter", &complete_return);
463 key_add(rc_map, "Complete:prefix", &complete_set_prefix);
466 DEF_CMD(complete_attach)
468 struct pane *p = ci->focus;
469 struct pane *complete;
474 p = call_ret(pane, "attach-linefilter", p);
477 complete = complete_pane(p);
483 return comm_call(ci->comm2, "callback:attach", complete);
486 void edlib_init(struct pane *ed safe)
488 call_comm("global-set-command", ed, &complete_attach,
489 0, NULL, "attach-render-complete");