2 * Copyright Neil Brown ©2015-2023 <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 string and it suppresses all
9 * lines which don't match the string. Matching can be case-insensitive,
10 * and may require the string to be at the start of the line.
12 * The linefilter module is used manage the selective display of lines.
13 * This module examine the results provided by linefilter and extends the
14 * string to the maximum that still matches the same set of lines.
15 * Keystrokes can extend or contract the match, which will cause display
18 * This module doesn't hold any marks on any document. The marks
19 * held by the rendered should be sufficient.
22 #define _GNU_SOURCE for strcasestr
29 struct complete_data {
34 const char *substr safe;
39 static struct map *rc_map;
41 DEF_LOOKUP_CMD(complete_handle, rc_map);
46 const char *prefix safe, *str;
49 static void strip_attrs(char *c safe)
54 if (*c == ack || *c == etx)
65 if (*c == '<' && c[1] == '<') {
80 static const char *add_highlight(const char *orig, int start, int len,
81 const char *attr safe, int *offset, int *cpos)
83 /* Create a copy of 'orig' with all non-attr chars from start for len
84 * given the extra 'attr'. start and len count non-attr chars.
85 * If offset!=NULL, stop when we get to that place in the result,
86 * and update *offset with that place in orig.
87 * If cpos, then when we reach cpos in orig, report len of result.
111 buf_append_byte(&ret, ack);
114 while (*c && (!offset || ret.len < *offset)) {
115 if (cp >= 0 && (c-orig) >= cp && *cpos == -1)
117 if ((use_lt && (*c != '<' || c[1] == '<')) ||
118 (!use_lt && (*c != ack && *c != soh && *c != etx))) {
119 /* This is regular text */
122 else if (start == 0) {
124 buf_append(&ret, '<');
125 buf_concat(&ret, attr);
126 buf_append(&ret, '>');
128 buf_append(&ret, soh);
129 buf_concat(&ret, attr);
130 buf_append(&ret, stx);
134 if (use_lt && *c == '<')
135 buf_append_byte(&ret, *c++);
136 buf_append_byte(&ret, *c++);
137 if (start < 0 && len > 0) {
141 buf_concat(&ret, "</>");
143 buf_append(&ret, etx);
148 /* Not regular text. */
149 if (start < 0 && len > 0) {
150 /* Close the attr highlight */
153 buf_concat(&ret, "</>");
155 buf_append(&ret, etx);
158 buf_append(&ret, *c);
159 if (*c == ack || *c == etx) {
164 while (*c && *c != etx)
165 buf_append(&ret, *c++);
167 buf_append(&ret, *c++);
169 while (*c && *c != '>')
170 buf_append_byte(&ret, *c++);
172 buf_append(&ret, *c++);
177 return buf_final(&ret);
188 DEF_CMD(render_complete_line)
190 struct complete_data *cd = ci->home->data;
191 char *line, *l2, *start = NULL;
201 m = mark_dup(ci->mark);
202 line = call_ret(str, ci->key, ci->home->parent, -1, m);
207 match = cd->stk->substr;
208 l2 = strsave(ci->home, line);
211 start = strcasestr(l2, match);
216 startlen = start - l2;
218 /* Only want 'num' bytes from start, with ->mark positioned.
219 * So need to find how many bytes of 'line' produce num bytes
220 * of highlighted line.
223 hl = add_highlight(line, startlen, strlen(match), "fg:red", &num, NULL);
228 line = call_ret(str, ci->key, ci->home->parent,
230 } else if (ci->mark2) {
231 /* Only want up-to the cursor, which might be in the middle of
232 * the highlighted region. Now we know where that is, we can
233 * highlight whatever part is still visible.
236 offset = call_comm(ci->key, ci->home->parent, &get_offset,
237 ci->num, ci->mark, NULL,
244 mark_to_mark(ci->mark, m);
249 hl = add_highlight(line, startlen, strlen(match), "fg:red", NULL, &offset);
251 ret = comm_call(ci->comm2, "callback:render", ci->focus,
259 DEF_CMD(complete_free)
261 struct complete_data *cd = ci->home->data;
262 struct stk *stk = cd->stk;
267 free((void*)t->substr);
276 static struct pane *complete_pane(struct pane *focus safe)
278 struct pane *complete;
279 struct complete_data *cd;
282 complete = pane_register(focus, 0, &complete_handle.c, cd);
287 cd->stk = malloc(sizeof(cd->stk[0]));
288 cd->stk->prev = NULL;
289 cd->stk->substr = strdup("");
294 DEF_CMD(complete_clone)
296 struct pane *parent = ci->focus;
297 struct pane *complete;
299 complete = complete_pane(parent);
301 pane_clone_children(ci->home, complete);
305 DEF_CMD(complete_ignore_replace)
310 DEF_CMD(complete_escape)
312 /* submit the original prefix back*/
313 struct complete_data *cd = ci->home->data;
315 /* This pane might be closed before the reply string is used,
316 * so we need to save it.
318 call("popup:close", ci->home->parent, NO_NUMERIC, NULL,
319 strsave(ci->home, cd->orig));
323 DEF_CMD(complete_char)
325 struct complete_data *cd = ci->home->data;
327 int pl = strlen(cd->stk->substr);
328 const char *suffix = ksuffix(ci, "doc:char-");
330 np = malloc(pl + strlen(suffix) + 1);
331 strcpy(np, cd->stk->substr);
332 strcpy(np+pl, suffix);
333 call("Complete:prefix", ci->focus, !cd->prefix_only, NULL, np,
341 struct complete_data *cd = ci->home->data;
342 struct stk *stk = cd->stk;
347 if (stk->substr[0] && !stk->prev->substr[0]) {
348 old = (void*)stk->substr;
349 old[strlen(old)-1] = 0;
352 free((void*)stk->substr);
355 call("Complete:prefix", ci->home, 0, NULL, NULL, 1,
360 static int csame(char a, char b)
369 static int common_len(const char *a safe, const char *b safe)
372 while (*a && csame(*a, *b)) {
380 static void adjust_pre(char *common safe, const char *new safe, int len)
382 int l = strlen(common);
385 while (l && len && csame(common[l-1], new[len-1])) {
391 memmove(common, common+l, newlen+1);
396 struct complete_data *cd safe;
400 /* common_pre is the longest common prefix to 'common' that
401 * appears in all matches in which 'common' appears. It is
402 * allocated with enough space to append 'common' after the
412 struct setcb *cb = container_of(ci->comm, struct setcb, c);
413 struct complete_data *cd = cb->cd;
414 const char *ss = cb->ss;
415 int len = strlen(ss);
416 const char *c = ci->str;
423 if (cd->prefix_only) {
425 if (strncmp(match, ss, len) == 0)
428 match = strcasestr(c, ss);
429 if (strncasecmp(c, ss, len) == 0) {
431 if (strncmp(c, ss, len) == 0)
433 } else if (strstr(c, ss))
438 /* should be impossible */
442 if (l && match[l-1] == '\n')
445 if (this_match > cb->best_match) {
446 /* Only use matches at least this good to calculate
449 cb->best_match = this_match;
452 free(cb->common_pre);
453 cb->common_pre = NULL;
456 if (this_match == cb->best_match) {
457 /* This match can be used for 'common' and
460 mark_free(cb->bestm);
462 cb->bestm = mark_dup(ci->mark);
465 cb->common = strndup(match, l);
467 cb->common[common_len(match, cb->common)] = 0;
468 /* If 'match' and 'common' disagree on case of
469 * 'prefix', use that of 'prefix'
471 if (memcmp(cb->common, match, len) != 0)
472 memcpy(cb->common, ss, len);
474 if (!cb->common_pre) {
475 cb->common_pre = strndup(c, l + match-c);
476 strncpy(cb->common_pre, c, match-c);
477 cb->common_pre[match-c] = 0;
479 adjust_pre(cb->common_pre, c, match-c);
485 DEF_CMD(complete_set_prefix)
487 /* Set the prefix, force a full refresh, and move point
488 * to the first match at start-of-line, or first match
489 * If there is no match, return -1.
490 * Otherwise return number of matches in ->num2 and
491 * the longest common prefix in ->str.
492 * If ci->num with ->str, allow substrings, else prefix-only
493 * if ci->num2, don't autocomplete, just display matches
495 struct pane *p = ci->home;
496 struct complete_data *cd = p->data;
501 /* Save a copy of the point so we can restore it if needed */
502 m = call_ret(mark, "doc:point", ci->focus);
510 cb.common_pre = NULL;
515 cd->prefix_only = !ci->num;
517 cb.ss = cd->stk->substr;
519 if (ci->str2 && (!cd->attr || strcmp(cd->attr, ci->str2) != 0)) {
521 cd->attr = strdup(ci->str2);
524 call_comm("Filter:set", ci->focus, &cb.c,
525 cd->prefix_only ? 3 : 2, NULL, cb.ss, 0, NULL, cd->attr);
529 call("Filter:set", ci->focus,
530 cd->prefix_only ? 3 : 2, NULL, cd->stk->substr,
533 call("Move-to", ci->focus, 0, m);
537 if (cb.common_pre && cb.common && cb.cnt && ci->str) {
539 strcat(cb.common_pre, cb.common);
540 stk = malloc(sizeof(*stk));
541 stk->substr = cb.common_pre;
544 cb.common_pre = NULL;
545 call("Filter:set", ci->focus,
546 cd->prefix_only ? 3 : 2, NULL, cd->stk->substr,
548 comm_call(ci->comm2, "callback:prefix", ci->focus, cb.cnt,
549 NULL, cd->stk->substr);
551 cd->orig = strdup(ci->str);
553 comm_call(ci->comm2, "callback:prefix", ci->focus, 0);
558 call("Move-to", ci->focus, 0, cb.bestm);
562 call("view:changed", ci->focus);
569 struct call_return *cr = container_of(ci->comm, struct call_return, c);
570 cr->s = ci->str ? strdup(ci->str) : NULL;
574 DEF_CMD(complete_return)
576 /* submit the selected entry to the popup */
577 struct call_return cr;
585 /* Go to start of line */
586 home_call(ci->home, "doc:render-line-prev", ci->home, 0, ci->mark);
587 home_call(ci->home, "doc:render-line",
588 ci->home, -1, ci->mark, NULL, 0, NULL,
594 if (l && cr.s[l-1] == '\n')
597 call("popup:close", ci->home->parent, NO_NUMERIC, NULL,
603 static void register_map(void)
605 rc_map = key_alloc();
607 key_add(rc_map, "doc:render-line", &render_complete_line);
608 key_add(rc_map, "Free", &complete_free);
609 key_add(rc_map, "Clone", &complete_clone);
611 key_add(rc_map, "Replace", &complete_ignore_replace);
612 key_add(rc_map, "K:ESC", &complete_escape);
613 key_add_range(rc_map, "doc:char- ", "doc:char-~", &complete_char);
614 key_add(rc_map, "K:Backspace", &complete_bs);
616 key_add(rc_map, "K:Enter", &complete_return);
618 key_add(rc_map, "Complete:prefix", &complete_set_prefix);
621 DEF_CMD(complete_attach)
623 struct pane *p = ci->focus;
624 struct pane *complete;
629 p = call_ret(pane, "attach-linefilter", p);
632 complete = complete_pane(p);
638 return comm_call(ci->comm2, "callback:attach", complete);
641 void edlib_init(struct pane *ed safe)
643 call_comm("global-set-command", ed, &complete_attach,
644 0, NULL, "attach-render-complete");