The R Project SVN R-packages

Rev

Rev 816 | Rev 1254 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 816 Rev 973
Line 83... Line 83...
83
        pos++;
83
        pos++;
84
    }
84
    }
85
    return nil;
85
    return nil;
86
}
86
}
87
 
87
 
-
 
88
+ (NSArray*) completeAll: (NSString*) part cutPrefix: (int) prefix {
-
 
89
    REngine *re = [REngine mainEngine];
-
 
90
    // first get the length of the search path so we can go environment by environment
-
 
91
    RSEXP *x = [re evaluateString:@"length(search())"];
-
 
92
    int pos=1, maxpos, matches=0;
-
 
93
	NSMutableArray *ca = nil;
-
 
94
	NSString *common=nil;
-
 
95
 
-
 
96
    if (x==nil) return nil;
-
 
97
    if ((maxpos = [x integer])==0) return nil;    
-
 
98
	
-
 
99
	ca = [[NSMutableArray alloc] initWithCapacity: 8];
-
 
100
	
-
 
101
    // ok, we got the search path length; search in each environment and if something matches, get it, otherwise go to the next one
-
 
102
    while (pos<=maxpos) {
-
 
103
        // use ls to get the names of the objects in the specific environment
-
 
104
        NSString *ls=[NSString stringWithFormat:@"ls(pos=%d, all.names=TRUE, pattern=\"^%@.*\")", pos, part];
-
 
105
        RSEXP *x = [re evaluateString:ls];
-
 
106
        //NSLog(@"attepmting to find %@ via %@", part, ls);
-
 
107
        if (x==nil)
-
 
108
            return nil;
-
 
109
        NSArray *a = [x array];
-
 
110
		
-
 
111
        if (a == nil) {
-
 
112
            [x release];
-
 
113
            return nil;
-
 
114
        }
-
 
115
        
-
 
116
        { // the following code works also if pattern is not specified; with pattern present we could make it even easier, but currently we use it just to narrow the search (e.g. "." could still be matched by something else ...)
-
 
117
            int i=0, firstMatch=-1;
-
 
118
            while (i<[a count]) {
-
 
119
                NSString *sx = (NSString*) [a objectAtIndex:i];
-
 
120
                if ([sx hasPrefix: part]) {
-
 
121
                    if (matches==0) {
-
 
122
                        firstMatch=i;
-
 
123
                        common=[[NSString alloc] initWithString: sx];
-
 
124
                    } else {
-
 
125
                        NSString *cpref=[[NSString alloc] initWithString:[common commonPrefixWithString:sx options:0]];
-
 
126
                        [common release];
-
 
127
                        common=cpref;
-
 
128
                    }
-
 
129
					[ca addObject: [sx substringFromIndex:prefix]];
-
 
130
                    matches++;
-
 
131
                }
-
 
132
                i++;
-
 
133
            }
-
 
134
        }
-
 
135
        pos++;
-
 
136
    }
-
 
137
	if (common) { 
-
 
138
		if (matches==1) {
-
 
139
			// attempt to get class of the object - it will fail if that's just a partial object, but who cares..
-
 
140
			x = [re evaluateString:[NSString stringWithFormat:@"try(class(%@),silent=TRUE)",common]];
-
 
141
			[ca release];
-
 
142
			if (x!=nil && [x string]!=nil && [[x string] isEqualToString:@"function"]) {
-
 
143
				return [NSArray arrayWithObject: [[[common autorelease] stringByAppendingString:@"("] substringFromIndex:prefix]];
-
 
144
			} else {
-
 
145
				return [NSArray arrayWithObject: [[common autorelease] substringFromIndex:prefix]];
-
 
146
			}
-
 
147
		} else {
-
 
148
			[common release];
-
 
149
			return ca;
-
 
150
		}
-
 
151
	}
-
 
152
	[ca release];
-
 
153
    return nil;
-
 
154
}
-
 
155
 
88
@end
156
@end