editline-tabcomp

Tab completion implementation for BSD editline (libedit)

git clone https://git.8pit.net/editline-tabcomp.git

 1#include <err.h>
 2#include <histedit.h>
 3#include <locale.h>
 4#include <stdio.h>
 5#include <stdlib.h>
 6#include <string.h>
 7
 8#include "complete.h"
 9
10#define ARRAY_SIZE(X) (sizeof(X) / sizeof(X[0]))
11
12static char *completions[] = {
13    "föö",
14    "fööbar",
15    "fööbaz",
16};
17
18static void
19compfunc(const char *input, size_t inlen)
20{
21	size_t i;
22
23	for (i = 0; i < ARRAY_SIZE(completions); i++)
24		if (!strncmp(input, completions[i], inlen))
25			addcomp(completions[i]);
26}
27
28int
29main(void)
30{
31	int num;
32	static EditLine *el;
33	const char *line;
34
35	setlocale(LC_CTYPE, "");
36	if (!(el = el_init("tabcomp-example", stdin, stdout, stderr)))
37		errx(EXIT_FAILURE, "el_init failed");
38
39	/* Initialize word completions */
40	initcomp(compfunc, 1);
41
42	/* Bind completion function to tab key */
43	el_set(el, EL_ADDFN, "complete", "Complete input", complete);
44	el_set(el, EL_BIND, "^I", "complete", NULL);
45
46	while ((line = el_gets(el, &num)) && num >= 0)
47		printf("%s", line);
48	if (num == -1)
49		err(EXIT_FAILURE, "el_gets failed");
50
51	return EXIT_SUCCESS;
52}