-
Notifications
You must be signed in to change notification settings - Fork 15
/
ShellView.m
executable file
·606 lines (502 loc) · 19.7 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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/* 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 <MPWFoundation/MPWFoundation.h>
#import "STCompiler.h"
#import "MPWExpression+autocomplete.h"
#import "MPWShellPrinter.h"
#define RETURN_CHAR 0x0D
#define BACKSPACE_CHAR 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)insertTextAtCursor:someText
{
[self insertText:someText];
}
+ (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 insertTextAtCursor: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;}
-(void)awakeFromNib
{
[super awakeFromNib];
[self setRichText:NO];
}
- (id)commandHandler { return commandHandler;}
- (NSArray *)completionsForPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)index
{
NSString *completionFor=[[self string] substringWithRange:charRange];
NSString *currentCommand = [self currentCommandLine];
NSArray *completions = [commandHandler completionsForString:currentCommand];
if ( [completions count]==1 && [[completions firstObject] isEqualToString:completionFor]) {
[self insertTextAtCursor:@" "];
completions=nil;
}
if ( [completions count]==1 && [[completions firstObject] isEqualToString:@" "]) {
[self insertTextAtCursor:@" "];
completions=nil;
}
NSString *common=[completions firstObject];
for ( NSString *s in completions) {
common=[common commonPrefixWithString:s options:NSLiteralSearch];
}
if ( [common length] > charRange.length) {
[self insertTextAtCursor:[common substringFromIndex:charRange.length]];
return nil;
}
return completions;
}
- (void) dealloc
{
[prompt release];
[history release];
if (shouldRetainCommandHandler) {
[commandHandler release];
}
[super dealloc];
}
- (id)initWithFrame:(NSRect)frameRect
{
return [self initWithFrame:frameRect prompt:@"] " historySize:20000 commandHandler:nil];
}
- (NSUndoManager*)undoManager
{
return 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)];
[self insertTextAtCursor: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];
[self setSmartInsertDeleteEnabled:NO];
[self setAutomaticTextReplacementEnabled:NO];
[self setAutomaticSpellingCorrectionEnabled:NO];
[self setAutomaticQuoteSubstitutionEnabled:NO];
[self setRichText:NO];
return self;
}
return nil;
}
- (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 insertTextAtCursor:[NSString stringWithFormat:@"\n%@\n%@%@", notification, prompt, command]];
// [self setFont:[NSFont boldSystemFontOfSize:[NSFont systemFontSize]] range:NSMakeRange(start, [notification length]+1)];
start += delta;
[self setSelectedRange:NSMakeRange(selectedRange.location+delta, selectedRange.length)];
}
-(void)jumpToNextPlaceHolder
{
const unichar placeHolderCharacter = 8226;
NSString *placeHolderString = [NSString stringWithCharacters:&placeHolderCharacter length:1];
NSString *text = [self string];
NSRange selectedRange = [self selectedRange];
NSRange nextPlaceHolderRange;
if ([[text substringWithRange:selectedRange] isEqualToString:placeHolderString])
nextPlaceHolderRange = [text rangeOfString:placeHolderString options:NSLiteralSearch range:NSMakeRange(selectedRange.location + 1, [text length] - (selectedRange.location + 1))];
else
nextPlaceHolderRange = [text rangeOfString:placeHolderString options:NSLiteralSearch range:NSMakeRange(selectedRange.location, [text length] - selectedRange.location)];
if (nextPlaceHolderRange.location == NSNotFound) nextPlaceHolderRange = [text rangeOfString:placeHolderString options:NSLiteralSearch range:NSMakeRange(0, selectedRange.location)];
if (nextPlaceHolderRange.location == NSNotFound)
{
if (![[text substringWithRange:selectedRange] isEqualToString:placeHolderString]) NSBeep();
}
else [self setSelectedRange:nextPlaceHolderRange];
}
- (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 '/':
[self jumpToNextPlaceHolder];
break;
case RETURN_CHAR:
[self insertNewlineIgnoringFieldEditor:self];
break;
case BACKSPACE_CHAR:
[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 9:
[self complete:self];
break;
case RETURN_CHAR:
[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];
}
}
-(NSString*)currentCommandLine
{
return [[self string] substringFromIndex:start];
}
- (void)executeCurrentCommand:(id)sender
{
NSString *command = [self currentCommandLine];
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 insertTextAtCursor:@"\n"];
id expr=nil;
id result=@"";
@try {
result=[commandHandler compileAndEvaluate:command];
} @catch ( NSException *e ) {
result=[e description];
}
// [commandHandler command:command from:self]; // The command handler is notified
if ( result && ![result isNil] ) {
// NSLog(@"result class: %@ result: '%@'",[result class],result);
[standardOut writeObject:result];
[standardOut appendBytes:"\n" length:1];
}
[self insertTextAtCursor: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 insertTextAtCursor: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 insertTextAtCursor:@"\n"];
[self scrollRangeToVisible:[self selectedRange]];
[commandHandler command:subCommand from:self]; // notify the command handler
// NSLog(@"puting command : %@",subCommand);
[self insertTextAtCursor:prompt];
start = [[self string] length];
lineEdited = NO;
subCommand = @"";
[scanner scanUpToCharactersFromSet:separatorSet intoString:&subCommand];
if ([subCommand length] > 0) [self insertTextAtCursor:subCommand];
[self scrollRangeToVisible:[self selectedRange]];
}
}
- (void)putText:(NSString *)text
{
[self moveToEndOfDocument:self];
[self insertTextAtCursor:text];
start = [[self string] length];
[self scrollRangeToVisible:[self selectedRange]];
}
-(void)setupStdioForCommandHandler
{
standardOut=[[MPWREPLViewPrinter streamWithTarget:[[self textStorage] mutableString] ] retain];
[commandHandler bindValue:standardOut toVariableNamed:@"stdout"];
}
- (void)setCommandHandler:handler
{
if (shouldRetainCommandHandler)
{
[handler retain];
[commandHandler release];
}
commandHandler = handler;
[self setupStdioForCommandHandler];
}
- (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;
}
}
- (void)insertCompletion:(NSString *)word forPartialWordRange:(NSRange)charRange movement:(NSInteger)movement isFinal:(BOOL)flag
{
// NSLog(@"word = %@, movement = %@, isFlinal = %@", word, [NSNumber numberWithInteger:movement], flag ? @"YES" : @"NO");
const unichar placeHolderCharacter = 8226;
NSString *placeHolderString = [NSString stringWithCharacters:&placeHolderCharacter length:1];
NSMutableString *stringToDisplay = [[word mutableCopy] autorelease];
NSUInteger replacedCount = [stringToDisplay replaceOccurrencesOfString:@":" withString:[NSString stringWithFormat:@":%@ ", placeHolderString] options:NSLiteralSearch range:NSMakeRange(0, [stringToDisplay length])];
if (flag && movement != NSCancelTextMovement && replacedCount > 1)
{
[super insertCompletion:stringToDisplay forPartialWordRange:charRange movement:movement isFinal:flag];
NSString *text = [self string];
[self setSelectedRange:[text rangeOfString:placeHolderString options:NSLiteralSearch range:NSMakeRange(charRange.location, [stringToDisplay length])]];
}
else {
[super insertCompletion:word forPartialWordRange:charRange movement:movement isFinal:flag];
}
}
@end