forked from pmougin/F-Script
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathShellView.m
507 lines (428 loc) · 16.2 KB
/
ShellView.m
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/* ShellView.m Copyright (c) 1998-2009 Philippe Mougin. */
/* This software is open source. See the license. */
/* This file includes contributions from Stephen C. Gilardi */
#import "ShellView.h"
#import "FSCommandHistory.h"
#import "FSInterpreterView.h"
#import "FSInterpreter.h"
#import "FSMiscTools.h"
#define RETURN 0x0D
#define BACKSPACE 0x7F // Note SHIFT + BACKSPACE gives 0x08
static NSDictionary *errorAttributes; // a dictionary of attributes that defines how an error in a command is shown.
static BOOL useMaxSize;
@implementation ShellView
/////////////////////////////// PRIVATE ////////////////////////////
+ (void) setUseMaxSize:(BOOL)shouldUseMaxSize
{
useMaxSize = shouldUseMaxSize;
}
- (void) replaceCurrentCommandWith:(NSString *)newCommand // This method is used when the user browse into the command history
{
[self setSelectedRange:NSMakeRange(start,[[self string] length])];
[self insertText:newCommand];
[self moveToEndOfDocument:self];
[self scrollRangeToVisible:[self selectedRange]];
lineEdited = NO;
}
/////////////////////////////// PUBLIC ////////////////////////////
+ (void)initialize
{
static BOOL tooLate = NO;
if (!tooLate)
{
tooLate = YES;
errorAttributes = [[NSDictionary alloc] initWithObjectsAndKeys: [NSColor whiteColor],NSForegroundColorAttributeName, [NSColor blackColor], NSBackgroundColorAttributeName, nil];
useMaxSize = YES;
}
}
//- (BOOL)acceptsFirstResponder {/*NSLog(@"ShellView acceptsFirstResponder");*/ return YES;}
//- (BOOL)becomeFirstResponder {/*NSLog(@"ShellView becomeFirstResponder");*/ return YES;}
//- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent {/*NSLog(@"ShellView acceptsFirstMouse:");*/ return YES;}
- (id)commandHandler { return commandHandler;}
- (NSArray *)completionsForPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)index
{
NSString *stringToComplete;
NSMutableArray *result;
NSArray *superResult;
NSUInteger i, count;
superResult = [super completionsForPartialWordRange:charRange indexOfSelectedItem:index];
result = [NSMutableArray arrayWithCapacity:[superResult count]];
if ([commandHandler isKindOfClass:[FSInterpreterView class]])
{
NSArray *identifiers = [[[(FSInterpreterView *)commandHandler interpreter] identifiers] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
stringToComplete = [[self string] substringWithRange:charRange];
//NSLog(stringToComplete);
for (i = 0, count = [identifiers count]; i < count; i++)
{
NSString *completionCandidate = [identifiers objectAtIndex:i];
if ([completionCandidate hasPrefix:stringToComplete])
[result addObject:completionCandidate];
}
}
for (i = 0, count = [superResult count]; i < count; i++)
{
[result addObject:[superResult objectAtIndex:i]];
}
if ([[self delegate] respondsToSelector:@selector(textView:completions:forPartialWordRange:indexOfSelectedItem:)])
return [[self delegate] textView:self completions:result forPartialWordRange:charRange indexOfSelectedItem:index];
else
return result;
}
- (void) dealloc
{
//NSLog(@"ShellView dealloc");
[prompt release];
[history release];
if (shouldRetainCommandHandler) [commandHandler release];
[super dealloc];
}
- (id)initWithFrame:(NSRect)frameRect
{
return [self initWithFrame:frameRect prompt:@"> " historySize:20000 commandHandler:nil];
}
- (id)initWithFrame:(NSRect)frameRect prompt:(NSString *)thePrompt historySize:(NSInteger)theHistorySize commandHandler:(id)theCommandHandler
{
if (self = [super initWithFrame:frameRect])
{
prompt = [thePrompt retain];
history = [[FSCommandHistory alloc] initWithUIntSize:theHistorySize];
parserMode = NO_DECOMPOSE;
commandHandler = [theCommandHandler retain];
lineEdited = NO;
last_command_start = 0;
shouldRetainCommandHandler = YES;
[self setUsesFindPanel:YES];
[self setFont:[NSFont userFixedPitchFontOfSize:-1]]; // -1 to get the default font size
[self setSelectedRange:NSMakeRange([[self string] length],0)];
[super insertText:prompt];
start = [[self string] length];
[self setDelegate:self]; // A ShellView is its own delegate! (see the section implementing delegate methods)
maxSize = 900000;
[self setAllowsUndo:YES];
return self;
}
return nil;
}
- (void)insertText:(id)aString
{
[super insertText:aString];
}
- (void) notifyUser:(NSString *)notification
{
NSString *command = [[self string] substringFromIndex:start];
NSRange selectedRange = [self selectedRange];
NSInteger delta = [prompt length] + [notification length] + 2;
[self setSelectedRange:NSMakeRange(start,[[self string] length])];
[self insertText:[NSString stringWithFormat:@"\n%@\n%@%@", notification, prompt, command]];
[self setFont:[NSFont boldSystemFontOfSize:systemFontSize()] range:NSMakeRange(start, [notification length]+1)];
start += delta;
[self setSelectedRange:NSMakeRange(selectedRange.location+delta, selectedRange.length)];
}
- (void)keyDown:(NSEvent *)theEvent // Called by the AppKit when the user press a key.
{
//NSLog(@"key = %@",[theEvent characters]);
//NSLog(@"char0 = %d", (int)[[theEvent characters] characterAtIndex:0]);
//NSLog(@"modifierFlags = %x",[theEvent modifierFlags]);
if ([theEvent type] != NSKeyDown)
{
[super keyDown:theEvent];
return;
}
if ([[theEvent characters] length] == 0) // this is the case in Jaguar for accents
{
[super keyDown:theEvent];
return;
}
//unichar theCharacter = [[theEvent charactersIgnoringModifiers] characterAtIndex:0];
unichar theCharacter = [[theEvent characters] characterAtIndex:0];
NSUInteger theModifierFlags = [theEvent modifierFlags];
// Is the current insertion point valid ?
if ([self selectedRange].location < start
&& !(theModifierFlags & NSShiftKeyMask
&& ( theCharacter == NSLeftArrowFunctionKey
|| theCharacter == NSRightArrowFunctionKey
|| theCharacter == NSUpArrowFunctionKey
|| theCharacter == NSDownArrowFunctionKey)))
{
/* if ([self selectedRange].location < (start - [prompt length]))
[self moveToEndOfDocument:self];
else
[self setSelectedRange:NSMakeRange(start,0)];*/
if ([self selectedRange].location < start)
[self setSelectedRange:NSMakeRange(start,0)];
[self scrollRangeToVisible:[self selectedRange]];
}
if (theModifierFlags & NSControlKeyMask)
{
switch (theCharacter)
{
case RETURN:
[self insertNewlineIgnoringFieldEditor:self];
break;
case BACKSPACE:
[self setSelectedRange:NSMakeRange(start,[[self string] length])];
[self delete:self];
break;
case NSUpArrowFunctionKey:
[self replaceCurrentCommandWith:[[history goToPrevious] getStr]];
break;
case NSDownArrowFunctionKey:
[self replaceCurrentCommandWith:[[history goToNext] getStr]];
break;
default:
[super keyDown:theEvent];
break;
}
}
else
{
switch (theCharacter)
{
case RETURN:
[self executeCurrentCommand:self];
break;
case NSF7FunctionKey:
[self switchParserMode:self];
break;
case NSF8FunctionKey:
[self parenthesizeCommand:self];
break;
default:
[super keyDown:theEvent];
break;
}
}
}
- (void)moveToBeginningOfLine:(id)sender
{
[self setSelectedRange:NSMakeRange(start,0)];
}
- (void)moveToEndOfLine:(id)sender
{
[self moveToEndOfDocument:sender];
}
- (void)moveToBeginningOfParagraph:(id)sender
{
[self setSelectedRange:NSMakeRange(start,0)];
}
- (void)moveToEndOfParagraph:(id)sender
{
[self moveToEndOfDocument:sender];
}
- (void)moveLeft:(id)sender
{
if ([self selectedRange].location > start)
[super moveLeft:sender];
}
- (void)moveUp:(id)sender
{
// if we are on the first line of current command ==> replace current command by the previous one (history)
// else ==> apply the normal text editing behaviour.
NSUInteger loc = [self selectedRange].location;
[super moveUp:sender];
if ([self selectedRange].location < start || [self selectedRange].location == loc) // moved before start of command || not moved because we are on the first line of the text view
{
if ([self selectedRange].location >= start-[prompt length] && [self selectedRange].location < start)
// we are on the prompt, so we move to the start of the current command (the insertion point should not be on the prompt)
[self setSelectedRange:NSMakeRange(start,0)];
else
{
[self saveEditedCommand:self];
[self replaceCurrentCommandWith:[[history goToPrevious] getStr]];
}
}
}
- (void)moveDown:(id)sender
{
// if we are on the last line of current command ==> replace current command by the next one (history)
// else ==> apply the normal text editing behaviour.
NSUInteger loc = [self selectedRange].location;
[super moveDown:sender];
if ([self selectedRange].location == loc || [self selectedRange].location == [[self string] length]) // no movement || move to end of document because we are on the last line
{
[self saveEditedCommand:self];
[self replaceCurrentCommandWith:[[history goToNext] getStr]];
}
}
- (void)saveEditedCommand:(id)sender
{
if (lineEdited) // if the current command has been edited by the user, save it in the history.
{
NSString *command = [[self string] substringFromIndex:start];
if ([command length] > 0 && ![command isEqualToString:[history getMostRecentlyInsertedStr]])
{
[history addStr:command];
[history goToPrevious];
}
}
}
- (void)parenthesizeCommand:(id)sender
{
if ([self shouldChangeTextInRange:NSMakeRange(start,0) replacementString:@"("])
{
[self replaceCharactersInRange:NSMakeRange(start,0) withString:@"("];
[self didChangeText];
}
if ([self shouldChangeTextInRange:NSMakeRange([self selectedRange].location,0) replacementString:@")"])
{
[self replaceCharactersInRange:NSMakeRange([self selectedRange].location,0) withString:@")"];
[self didChangeText];
}
lineEdited = YES;
}
- (void)switchParserMode:(id)sender
{
if (parserMode == NO_DECOMPOSE)
{
[self notifyUser:@"When pasting text in this console, newline and carriage return characters are now interpreted as command separators"];
parserMode = DECOMPOSE;
[[self undoManager] removeAllActions];
}
else
{
[self notifyUser:@"When pasting text in this console, newline and carriage return characters are now NOT interpreted as command separators"];
parserMode = NO_DECOMPOSE;
[[self undoManager] removeAllActions];
}
}
- (void)executeCurrentCommand:(id)sender
{
NSString *command = [[self string] substringFromIndex:start];
long overflow;
if (useMaxSize && (overflow = [[self string] length] - maxSize) > 0)
{
overflow = overflow + maxSize / 3;
[self replaceCharactersInRange:NSMakeRange(0,overflow) withString:@""];
start = start - overflow;
}
last_command_start = start;
if ([command length] > 0 && ![command isEqualToString:[history getMostRecentlyInsertedStr]])
[history addStr:command];
[history goToLast];
[self moveToEndOfDocument:self];
[self insertText:@"\n"];
[commandHandler command:command from:self]; // The command handler is notified
[self insertText:prompt];
[self scrollRangeToVisible:[self selectedRange]];
start = [[self string] length];
lineEdited = NO;
[[self undoManager] removeAllActions];
}
- (void)paste:(id)sender
{
NSPasteboard *pb = [NSPasteboard pasteboardByFilteringTypesInPasteboard:[NSPasteboard generalPasteboard]];
if ([pb availableTypeFromArray:[NSArray arrayWithObject:NSStringPboardType]] == NSStringPboardType)
{
NSMutableString *command = [[[pb stringForType:NSStringPboardType] mutableCopy] autorelease];
switch (parserMode)
{
case DECOMPOSE: [command replaceOccurrencesOfString:@"\n" withString:@"\r" options:NSLiteralSearch range:NSMakeRange(0, [command length])];
break;
case NO_DECOMPOSE: [command replaceOccurrencesOfString:@"\r" withString:@"\n" options:NSLiteralSearch range:NSMakeRange(0, [command length])];
break;
}
[self putCommand:command];
}
}
- (void)putCommand:(NSString *)command
{
NSCharacterSet *separatorSet;
NSScanner *scanner = [NSScanner scannerWithString:command];
NSString *subCommand;
separatorSet = [NSCharacterSet characterSetWithCharactersInString:@"\r"];
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@""]]; // because Scanners skip whitespace and newline characters by default
if ([self selectedRange].location < start)
[self moveToEndOfDocument:self];
if ([scanner scanUpToCharactersFromSet:separatorSet intoString:&subCommand])
[self insertText:subCommand];
while (![scanner isAtEnd])
{
[scanner scanString:@"\r" intoString:NULL];
subCommand = [[self string] substringFromIndex:start];
last_command_start = start;
if ([subCommand length] > 0) [history addStr:subCommand];
[self moveToEndOfDocument:self];
[self insertText:@"\n"];
[self scrollRangeToVisible:[self selectedRange]];
[commandHandler command:subCommand from:self]; // notify the command handler
// NSLog(@"puting command : %@",subCommand);
[self insertText:prompt];
start = [[self string] length];
lineEdited = NO;
subCommand = @"";
[scanner scanUpToCharactersFromSet:separatorSet intoString:&subCommand];
if ([subCommand length] > 0) [self insertText:subCommand];
[self scrollRangeToVisible:[self selectedRange]];
}
}
- (void)putText:(NSString *)text
{
[self moveToEndOfDocument:self];
[self insertText:text];
start = [[self string] length];
[self scrollRangeToVisible:[self selectedRange]];
}
- (void)setCommandHandler:handler
{
if (shouldRetainCommandHandler)
{
[handler retain];
[commandHandler release];
}
commandHandler = handler;
}
- (void)setShouldRetainCommandHandler:(BOOL)shouldRetain
{
if (shouldRetainCommandHandler == YES && shouldRetain == NO)
[commandHandler release];
else if (shouldRetainCommandHandler == NO && shouldRetain == YES)
[commandHandler retain];
shouldRetainCommandHandler = shouldRetain;
}
- (BOOL)shouldRetainCommandHandler { return shouldRetainCommandHandler;}
- (void)showErrorRange:(NSRange)range
{
NSTextStorage *theTextStore = [self textStorage];
range.location += last_command_start;
// The folowing instruction gives an better visual result.
// Note that for it to work, showError:, the current method, must be called before outputing any error message
// due to the use of the text's length in the test.
if (range.location + range.length >= [[self string] length] && range.length > 1) range.length--;
if ([self shouldChangeTextInRange:range replacementString:nil])
{
[theTextStore beginEditing];
[theTextStore addAttributes:errorAttributes range:range];
[theTextStore endEditing];
[self didChangeText];
}
}
// I implement this method, inherited from NSTextView,in order to prevent
// the "smart delete" to delete parts of the prompt (in practice, this
// was seen when the prompt ends with whithespace)
- (NSRange)smartDeleteRangeForProposedRange:(NSRange)proposedCharRange
{
NSRange r = [super smartDeleteRangeForProposedRange:proposedCharRange];
if (proposedCharRange.location >= start && r.location < start)
{
r.length = r.length - (start - r.location) ;
r.location = start;
}
return r;
}
///////////////////////// Delegate methods /////////////////
// Since a CLIView is his own delegate, it receives the NSTextView(its super class) delegate calls.
- (BOOL)textView:(NSTextView *)aTextView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString *)replacementString
{
// policy: do not accept a modification outside the current command.
if (replacementString && affectedCharRange.location < start)
{
NSBeep();
return NO;
}
else
{
lineEdited = YES;
return YES;
}
}
@end