File: complete.c

package info (click to toggle)
ion3 20050502-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 5,188 kB
  • ctags: 5,450
  • sloc: ansic: 39,916; perl: 2,445; sh: 1,229; makefile: 639; ruby: 90
file content (124 lines) | stat: -rw-r--r-- 2,397 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * ion/mod_query/complete.c
 *
 * Copyright (c) Tuomo Valkonen 1999-2005. 
 *
 * Ion is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 */

#include <string.h>
#include <ctype.h>
#include <string.h>
#include <limits.h>

#include <ioncore/common.h>
#include "complete.h"
#include "edln.h"
#include "wedln.h"


static int str_common_part_l(const char *p1, const char *p2)
{
    int i=0;
    
    while(1){
        if(*p1=='\0' || *p1!=*p2)
            break;
        p1++; p2++; i++;
    }
    
    return i;
}


/* Get length of part common to all completions
 * and remove duplicates
 */
static int get_common_part_rmdup(char **completions, int *ncomp)
{
    int i, j, c=INT_MAX, c2;
    
    for(i=0, j=1; j<*ncomp; j++){
        c2=str_common_part_l(completions[i], completions[j]);
        if(c2<c)
            c=c2;

        if(completions[i][c2]=='\0' && completions[j][c2]=='\0'){
            free(completions[j]);
            completions[j]=NULL;
            continue;
        }
        i++;
        if(i!=j){
            completions[i]=completions[j];
            completions[j]=NULL;
        }
    }
    *ncomp=i+1;
    
    return c;
}


static int compare(const void *p1, const void *p2)
{
    const char **v1, **v2;
    
    v1=(const char **)p1;
    v2=(const char **)p2;
    return strcmp(*v1, *v2);
}


void edln_complete(Edln *edln)
{
    char *p;
    int len;
    
    if(edln->completion_handler==NULL)
        return;
    
    len=edln->point;
    
    p=ALLOC_N(char, len+1);
    
    if(p==NULL)
        return;
    
    memcpy(p, edln->p, len);
    p[len]='\0';
    
    edln->completion_handler(edln->uiptr, p);
    
    free(p);
}


int edln_do_completions(Edln *edln, char **completions, int ncomp,
                         const char *beg)
{
    int len;
    
    if(ncomp==0){
        return 0;
    }else if(ncomp==1){
        len=strlen(completions[0]);
    }else{
        qsort(completions, ncomp, sizeof(char**), compare);
        len=get_common_part_rmdup(completions, &ncomp);
    }
    
    edln_kill_to_bol(edln);
    
    if(beg!=NULL)
        edln_insstr(edln, beg);
    
    if(len!=0)
        edln_insstr_n(edln, completions[0], len);
    
    return ncomp;
}