2 * ion/mod_query/complete.c
4 * Copyright (c) Tuomo Valkonen 1999-2007.
6 * See the included file LICENSE for details.
14 #include <libtu/objp.h>
15 #include <ioncore/common.h>
21 /*{{{ Completion list processing */
24 static int str_common_part_l(const char *p1, const char *p2)
29 if(*p1=='\0' || *p1!=*p2)
38 /* Get length of part common to all completions
39 * and remove duplicates
41 static int get_common_part_rmdup(char **completions, int *ncomp)
43 int i, j, c=INT_MAX, c2;
45 for(i=0, j=1; j<*ncomp; j++){
46 c2=str_common_part_l(completions[i], completions[j]);
50 if(completions[i][c2]=='\0' && completions[j][c2]=='\0'){
57 completions[i]=completions[j];
67 static int compare(const void *p1, const void *p2)
69 const char **v1, **v2;
73 return strcoll(*v1, *v2);
77 static void edln_reset(Edln *edln)
79 assert(edln->palloced>=1);
89 static void edln_do_set_completion(Edln *edln, const char *comp, int len,
90 const char *beg, const char *end)
95 edln_insstr_n(edln, beg, strlen(beg), FALSE, TRUE);
98 edln_insstr_n(edln, comp, len, FALSE, TRUE);
101 edln_insstr_n(edln, end, strlen(end), FALSE, FALSE);
103 if(edln->ui_update!=NULL){
104 edln->ui_update(edln->uiptr, 0,
105 EDLN_UPDATE_MOVED|EDLN_UPDATE_CHANGED|
112 void edln_set_completion(Edln *edln, const char *comp,
113 const char *beg, const char *end)
115 edln_do_set_completion(edln, comp, strlen(comp), beg, end);
119 int edln_do_completions(Edln *edln, char **completions, int ncomp,
120 const char *beg, const char *end, bool setcommon,
129 len=strlen(completions[0]);
132 qsort(completions, ncomp, sizeof(char**), compare);
133 len=get_common_part_rmdup(completions, &ncomp);
137 edln_do_set_completion(edln, completions[0], len, beg, end);
149 bool complproxy_init(WComplProxy *proxy, WEdln *wedln, int id, int cycle)
151 watch_init(&(proxy->wedln_watch));
152 if(!watch_setup(&(proxy->wedln_watch), (Obj*)wedln, NULL))
162 WComplProxy *create_complproxy(WEdln *wedln, int id, int cycle)
164 CREATEOBJ_IMPL(WComplProxy, complproxy, (p, wedln, id, cycle));
168 void complproxy_deinit(WComplProxy *proxy)
170 watch_reset(&(proxy->wedln_watch));
175 * Set completion list of the \type{WEdln} that \var{proxy} refers to to
176 * \var{compls}, if it is still waiting for this completion run. The
177 * numerical indexes of \var{compls} list the found completions. If the
178 * entry \var{common_beg} (\var{common_end}) exists, it gives an extra
179 * common prefix (suffix) of all found completions.
182 bool complproxy_set_completions(WComplProxy *proxy, ExtlTab compls)
184 WEdln *wedln=(WEdln*)proxy->wedln_watch.obj;
187 if(wedln->compl_waiting_id==proxy->id){
188 wedln_set_completions(wedln, compls, proxy->cycle);
189 wedln->compl_current_id=proxy->id;
199 IMPLCLASS(WComplProxy, Obj, complproxy_deinit, NULL);