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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
/* CompletionField.m
*
* Copyright (C) 2003-2024 Free Software Foundation, Inc.
*
* Authors: Enrico Sersale
* Riccardo Mottola
* Date: August 2001
*
* This file is part of the GNUstep GWorkspace application
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 31 Milk Street #960789 Boston, MA 02196 USA.
*/
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <GNUstepBase/GNUstep.h>
#import "GWFunctions.h"
#import "FSNFunctions.h"
#import "CompletionField.h"
@implementation CompletionField
- (void)dealloc
{
[super dealloc];
}
- (id)initWithCoder: (NSCoder *) coder
{
self = [super initWithCoder: coder];
if (self)
{
[self setRichText: NO];
[self setImportsGraphics: NO];
[self setUsesFontPanel: NO];
[self setUsesRuler: NO];
[self setEditable: YES];
fm = [NSFileManager defaultManager];
}
return self;
}
- (void)setFrame:(NSRect)frameRect
{
NSSize size;
[super setFrame: frameRect];
size = NSMakeSize(1e7, [self bounds].size.height);
[[self textContainer] setContainerSize: size];
[[self textContainer] setWidthTracksTextView: YES];
}
- (void)keyDown:(NSEvent *)theEvent
{
NSString *eventstr = [theEvent characters];
NSString *str = [self string];
#define CHECK_SEPARATOR \
if ([path hasSuffix: pathSeparator] == NO) \
[path appendString: pathSeparator]
if (([eventstr isEqual: @"\r"] == NO)
&& ([eventstr isEqual: @"\t"] == NO))
{
[super keyDown: theEvent];
}
if ([eventstr isEqual: @"\t"] && [str length])
{
CREATE_AUTORELEASE_POOL(arp);
NSString *pathSeparator = path_separator();
NSArray *components = [str componentsSeparatedByString: pathSeparator];
NSMutableString *path = [NSMutableString string];
NSUInteger i, j, m, n;
if ([[components objectAtIndex: 0] isEqual: str])
{
NSArray *appPaths;
NSMutableArray *appBundles;
NSUInteger i;
NSString *completedAppName = nil;
appPaths = NSStandardApplicationPaths();
appBundles = [[NSMutableArray alloc] initWithCapacity:1];
for (i = 0; i < [appPaths count]; i++)
{
NSArray *contents;
contents = [fm directoryContentsAtPath: [appPaths objectAtIndex:i]];
[appBundles addObjectsFromArray:contents];
}
for (i = 0; i < [appBundles count]; i++)
{
NSString *appToCheck = [appBundles objectAtIndex:i];
// for simplicity, we complete with the first app found
if ([appToCheck hasPrefix: str] || [appToCheck isEqualToString:str])
{
completedAppName = appToCheck;
}
}
[self setString: completedAppName];
[appBundles release];
RELEASE (arp);
return;
}
[path appendString: pathSeparator];
for (i = 0; i < [components count]; i++)
{
NSString *component = [components objectAtIndex: i];
NSString *teststr = [path stringByAppendingString: component];
BOOL isDir;
if (([fm fileExistsAtPath: teststr isDirectory: &isDir] && isDir)
&& ([path isEqual: teststr] == NO))
{
NSArray *contents = [fm directoryContentsAtPath: teststr];
if (contents && ([str hasSuffix: pathSeparator] == NO))
{
BOOL found = NO;
for (j = 0; j < [contents count]; j++)
{
NSString *fname = [contents objectAtIndex: j];
if ([fname hasPrefix: component] && (![fname isEqual: component]))
{
found = YES;
}
}
if (found)
{
CHECK_SEPARATOR;
[path appendString: component];
NSBeep();
}
else
{
CHECK_SEPARATOR;
[path appendString: component];
if (isDir)
{
[path appendString: pathSeparator];
}
}
}
else
{
CHECK_SEPARATOR;
[path appendString: component];
if (isDir)
{
[path appendString: pathSeparator];
}
}
}
else
{
NSArray *contents = [fm directoryContentsAtPath: path];
if (contents)
{
NSMutableArray *common = [NSMutableArray array];
NSUInteger *lengths = NSZoneMalloc (NSDefaultMallocZone(), sizeof(NSUInteger) * [contents count]);
NSUInteger prefLength = 0;
NSUInteger index = 0;
for (j = 0; j < [contents count]; j++)
{
lengths[j] = 0;
}
for (j = 0; j < [contents count]; j++)
{
NSString *fname = [contents objectAtIndex: j];
if ([fname hasPrefix: component])
{
NSRange range = [fname rangeOfString: component];
if (range.length >= prefLength)
{
prefLength = range.length;
lengths[j] = range.length;
index = j;
}
[common addObject: fname];
}
}
if (prefLength != 0)
{
BOOL found = NO;
for (m = 0; m < [contents count]; m++)
{
NSUInteger l1 = lengths[m];
for (n = 0; n < [contents count]; n++)
{
NSUInteger l2 = lengths[n];
if ((m != n) && ((l1 != 0) && (l2 != 0)))
{
if ((l1 == l2) && (l1 == prefLength))
{
found = YES;
break;
}
}
}
if (found)
{
break;
}
}
if (found == NO)
{
NSString *cprefix = commonPrefixInArray(common);
if (cprefix)
{
CHECK_SEPARATOR;
[path appendString: cprefix];
if ([fm fileExistsAtPath: path isDirectory: &isDir])
{
if (isDir)
{
[path appendString: pathSeparator];
}
}
else
{
NSBeep();
}
}
else
{
CHECK_SEPARATOR;
[path appendString: [contents objectAtIndex: index]];
[path appendString: pathSeparator];
}
}
else
{
NSString *cprefix = commonPrefixInArray(common);
if (cprefix)
{
CHECK_SEPARATOR;
[path appendString: cprefix];
}
else
{
NSString *s = [[contents objectAtIndex: index] substringToIndex: prefLength];
[path appendString: s];
}
NSZoneFree (NSDefaultMallocZone(), lengths);
NSBeep();
break;
}
}
NSZoneFree (NSDefaultMallocZone(), lengths);
}
}
}
[self setString: path];
RELEASE (arp);
} else if ([eventstr isEqual: @"\r"] && [str length]) {
[controller completionFieldDidEndLine: self];
}
}
@end
|