-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIFCompilerController.m
1135 lines (880 loc) · 36.9 KB
/
IFCompilerController.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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// IFCompilerController.m
// Inform
//
// Created by Andrew Hunter on Mon Aug 18 2003.
// Copyright (c) 2003 Andrew Hunter. All rights reserved.
//
#import "IFCompilerController.h"
#import "IFAppDelegate.h"
#import "IFCompiler.h"
#import "IFError.h"
#import "IFProjectController.h"
#import "IFPretendWebView.h"
#import "IFPretendTextView.h"
#import "Preferences/IFPreferences.h"
// Possible styles (stored in the styles dictionary)
NSString* IFStyleBase = @"IFStyleBase";
// Basic compiler messages
NSString* IFStyleCompilerVersion = @"IFStyleCompilerVersion";
NSString* IFStyleCompilerMessage = @"IFStyleCompilerMessage";
NSString* IFStyleCompilerWarning = @"IFStyleCompilerWarning";
NSString* IFStyleCompilerError = @"IFStyleCompilerError";
NSString* IFStyleCompilerFatalError = @"IFStyleCompilerFatalError";
NSString* IFStyleProgress = @"IFStyleProgress";
NSString* IFStyleFilename = @"IFStyleFilename";
// Compiler statistics/dumps/etc
NSString* IFStyleAssembly = @"IFStyleAssembly";
NSString* IFStyleHexDump = @"IFStyleHexDump";
NSString* IFStyleStatistics = @"IFStyleStatistics";
static IFCompilerController* activeController = nil;
@implementation IFCompilerController
// == Styles ==
+ (NSDictionary*) defaultStyles {
NSFont* smallFont = [NSFont labelFontOfSize: 6];
NSFont* baseFont = [NSFont labelFontOfSize: 10];
NSFont* bigFont = [NSFont labelFontOfSize: 10];
smallFont = baseFont = bigFont = [NSFont fontWithName: @"Monaco" size: 10.0];
NSFont* boldFont = [[NSFontManager sharedFontManager] convertFont: bigFont
toHaveTrait: NSBoldFontMask];
NSFont* italicFont = [[NSFontManager sharedFontManager] convertFont: boldFont
toHaveTrait: NSItalicFontMask];
NSMutableParagraphStyle* centered = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
[centered setAlignment: NSCenterTextAlignment];
NSDictionary* baseStyle = [NSDictionary dictionaryWithObjectsAndKeys:
baseFont, NSFontAttributeName,
[NSColor blackColor], NSForegroundColorAttributeName,
0];
NSDictionary* versionStyle = [NSDictionary dictionaryWithObjectsAndKeys:
bigFont, NSFontAttributeName,
centered, NSParagraphStyleAttributeName,
0];
NSDictionary* filenameStyle = [NSDictionary dictionaryWithObjectsAndKeys:
[NSColor blackColor],
NSForegroundColorAttributeName,
boldFont, NSFontAttributeName,
0];
NSDictionary* messageStyle = [NSDictionary dictionaryWithObjectsAndKeys:
[NSColor colorWithDeviceRed: 0 green: 0.5 blue: 0 alpha: 1.0],
NSForegroundColorAttributeName, 0];
NSDictionary* warningStyle = [NSDictionary dictionaryWithObjectsAndKeys:
[NSColor colorWithDeviceRed: 0 green: 0 blue: 0.7 alpha: 1.0],
NSForegroundColorAttributeName,
boldFont, NSFontAttributeName,
0];
NSDictionary* errorStyle = [NSDictionary dictionaryWithObjectsAndKeys:
[NSColor colorWithDeviceRed: 0.7 green: 0 blue: 0.0 alpha: 1.0],
NSForegroundColorAttributeName,
boldFont, NSFontAttributeName,
0];
NSDictionary* fatalErrorStyle = [NSDictionary dictionaryWithObjectsAndKeys:
[NSColor colorWithDeviceRed: 1.0 green: 0 blue: 0.0 alpha: 1.0],
NSForegroundColorAttributeName,
italicFont, NSFontAttributeName,
0];
NSDictionary* progressStyle = [NSDictionary dictionaryWithObjectsAndKeys:
[NSColor colorWithDeviceRed: 0.0 green: 0 blue: 0.6 alpha: 1.0],
NSForegroundColorAttributeName,
smallFont, NSFontAttributeName,
0];
return [NSDictionary dictionaryWithObjectsAndKeys:
baseStyle, IFStyleBase,
versionStyle, IFStyleCompilerVersion,
messageStyle, IFStyleCompilerMessage, warningStyle, IFStyleCompilerWarning,
errorStyle, IFStyleCompilerError, fatalErrorStyle, IFStyleCompilerFatalError,
filenameStyle, IFStyleFilename,
progressStyle, IFStyleProgress,
0];
}
// == Initialisation ==
- (void) _registerHandlers {
if (compiler != nil) {
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(started:)
name: IFCompilerStartingNotification
object: compiler];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(finished:)
name: IFCompilerFinishedNotification
object: compiler];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(gotStdout:)
name: IFCompilerStdoutNotification
object: compiler];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(gotStderr:)
name: IFCompilerStderrNotification
object: compiler];
}
}
- (void) _removeHandlers {
if (compiler != nil) {
[[NSNotificationCenter defaultCenter] removeObserver: self
name: IFCompilerStartingNotification
object: compiler];
[[NSNotificationCenter defaultCenter] removeObserver: self
name: IFCompilerFinishedNotification
object: compiler];
[[NSNotificationCenter defaultCenter] removeObserver: self
name: IFCompilerStdoutNotification
object: compiler];
[[NSNotificationCenter defaultCenter] removeObserver: self
name: IFCompilerStderrNotification
object: compiler];
}
}
- (id) init {
self = [super init];
if (self) {
compiler = [[IFCompiler allocWithZone: [self zone]] init];
styles = [[[self class] defaultStyles] mutableCopy];
highlightPos = 0;
errorFiles = nil;
errorMessages = nil;
delegate = nil;
auxViews = [[NSMutableArray alloc] init];
viewNames = [[NSMutableArray alloc] init];
runtimeView = -1;
[self _registerHandlers];
}
return self;
}
- (void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver: self];
[compiler release];
[styles release];
if (errorFiles) [errorFiles release];
if (errorMessages) [errorMessages release];
if (window) [window release];
//if (delegate) [delegate release];
if (lastProblemURL) [lastProblemURL release];
if (overrideURL) [overrideURL release];
if (auxViews) [auxViews release];
if (viewNames) [viewNames release];
if (splitView) [splitView release];
if (activeView) [activeView release];
[super dealloc];
}
- (void) awakeFromNib {
[[compilerResults textStorage] setDelegate: self];
messagesSize = [messageScroller frame].size.height;
NSRect newFrame = [messageScroller frame];
newFrame.size.height = 0;
[messageScroller setFrame: newFrame];
[splitView adjustSubviews];
// Mutter, interface builder won't let you change the enclosing scrollview
// of an outlineview
[messageScroller setBorderType: NSNoBorder];
[resultScroller setHasHorizontalScroller: YES];
[compilerResults setMaxSize: NSMakeSize(1e8, 1e8)];
[compilerResults setHorizontallyResizable: YES];
[compilerResults setVerticallyResizable: YES];
[[compilerResults textContainer] setWidthTracksTextView: NO];
[[compilerResults textContainer] setContainerSize: NSMakeSize(1e8, 1e8)];
}
- (void) showWindow: (id) sender {
if (!awake) {
[NSBundle loadNibNamed: @"Compiling"
owner: self];
}
[window orderFront: sender];
}
// == Information ==
- (void) resetCompiler {
[self _removeHandlers];
[compiler release];
compiler = [[IFCompiler allocWithZone: [self zone]] init];
[self _registerHandlers];
NSRect newFrame = [messageScroller frame];
newFrame.size.height = 0;
[messageScroller setFrame: newFrame];
[splitView adjustSubviews];
}
- (void) setCompiler: (IFCompiler*) comp {
[self _removeHandlers];
[compiler release];
compiler = [comp retain];
[self _registerHandlers];
NSRect newFrame = [messageScroller frame];
newFrame.size.height = 0;
[messageScroller setFrame: newFrame];
[splitView adjustSubviews];
}
- (IFCompiler*) compiler {
return compiler;
}
// == Starting/stopping the compiler ==
- (BOOL) startCompiling {
if (window)
[window setTitle: [NSString stringWithFormat: [[NSBundle mainBundle] localizedStringForKey:@"Compiling - '%@'..."
value:@"Compiling - '%@'..."
table:nil],
[[compiler inputFile] lastPathComponent]]];
if (errorFiles) [errorFiles release];
if (errorMessages) [errorMessages release];
errorFiles = [[NSMutableArray array] retain];
errorMessages = [[NSMutableArray array] retain];
if (overrideURL != nil) [overrideURL release];
overrideURL = nil;
if (delegate &&
[delegate respondsToSelector: @selector(errorMessagesCleared:)]) {
[delegate errorMessagesCleared: self];
}
[[[compilerResults textStorage] mutableString] setString: @""];
highlightPos = 0;
[compiler prepareForLaunchWithBlorbStage: NO];
[compiler launch];
return YES;
}
- (BOOL) abortCompiling {
if (window)
[window setTitle: [NSString stringWithFormat: @"Aborted - '%@'",
[[compiler inputFile] lastPathComponent]]];
return YES;
}
// == Compiler messages ==
- (void) scrollToEnd {
[compilerResults scrollRangeToVisible: NSMakeRange([[compilerResults textStorage] length], 0)];
}
- (void) started: (NSNotification*) not {
if (errorFiles == nil) errorFiles = [[NSMutableArray alloc] init];
if (errorMessages == nil) errorMessages = [[NSMutableArray alloc] init];
if (overrideURL != nil) [overrideURL release];
overrideURL = nil;
[self clearTabViews];
[errorMessages removeAllObjects];
[errorFiles removeAllObjects];
[compilerMessages reloadData];
[blorbLocation release]; blorbLocation = nil;
[[[compilerResults textStorage] mutableString] setString: @""];
highlightPos = 0;
if (delegate &&
[delegate respondsToSelector: @selector(compileStarted:)]) {
[delegate compileStarted: self];
}
}
- (void) finished: (NSNotification*) not {
int exitCode = [[[not userInfo] objectForKey: @"exitCode"] intValue];
[lastProblemURL release];
if (overrideURL)
lastProblemURL = [overrideURL retain];
else
lastProblemURL = [[compiler problemsURL] retain];
[[[compilerResults textStorage] mutableString] appendString: @"\n"];
[[[compilerResults textStorage] mutableString] appendString:
[NSString stringWithFormat: [[NSBundle mainBundle] localizedStringForKey: @"Compiler finished with code %i"
value: @"Compiler finished with code %i"
table: nil], exitCode]];
[[[compilerResults textStorage] mutableString] appendString: @"\n"];
NSString* msg;
if (exitCode == 0) {
[[compiler progress] setMessage: [NSString stringWithFormat: [[NSBundle mainBundle] localizedStringForKey: @"Compilation succeeded"
value: @"Compilation succeeded"
table: nil], exitCode]];
msg = [[NSBundle mainBundle] localizedStringForKey: @"Success"
value: @"Success"
table: nil];
if (delegate &&
[delegate respondsToSelector: @selector(compileCompletedAndSucceeded:)]) {
[delegate compileCompletedAndSucceeded: self];
}
} else {
switch (exitCode) {
case SIGILL:
case SIGABRT:
case SIGBUS:
case SIGSEGV:
[[compiler progress] setMessage: [NSString stringWithFormat: [[NSBundle mainBundle] localizedStringForKey: @"Compiler crashed with code %i"
value: @"Compiler crashed with code %i"
table: nil], exitCode]];
break;
default:
[[compiler progress] setMessage: [NSString stringWithFormat: [[NSBundle mainBundle] localizedStringForKey: @"Compilation failed with code %i"
value: @"Compilation failed with code %i"
table: nil], exitCode]];
break;
}
msg = [[NSBundle mainBundle] localizedStringForKey: @"Failed"
value: @"Failed"
table: nil];
if (delegate &&
[delegate respondsToSelector: @selector(compileCompletedAndSucceeded:)]) {
[delegate compileCompletedAndFailed: self];
}
}
if (window)
[window setTitle: [NSString stringWithFormat: @"%@ - '%@'",
msg, [[compiler inputFile] lastPathComponent]]];
[self scrollToEnd];
}
- (void) gotStdout: (NSNotification*) not {
NSString* data = [[not userInfo] objectForKey: @"string"];
NSAttributedString* newString = [[[NSAttributedString alloc] initWithString: data
attributes: [styles objectForKey: IFStyleBase]] autorelease];
[[compilerResults textStorage] appendAttributedString: newString];
}
- (void) gotStderr: (NSNotification*) not {
NSString* data = [[not userInfo] objectForKey: @"string"];
NSAttributedString* newString = [[[NSAttributedString alloc] initWithString: data
attributes: [styles objectForKey: IFStyleBase]] autorelease];
[[compilerResults textStorage] appendAttributedString: newString];
}
// == Dealing with highlighting of the compiler output ==
- (NSString*) styleForLine: (NSString*) line {
activeController = self;
IFLex res = IFErrorScanString([line cString]);
switch (res) {
case IFLexBase:
return IFStyleBase;
case IFLexCompilerVersion:
return IFStyleCompilerVersion;
case IFLexCompilerMessage:
return IFStyleCompilerMessage;
case IFLexCompilerWarning:
return IFStyleCompilerWarning;
case IFLexCompilerError:
return IFStyleCompilerError;
case IFLexCompilerFatalError:
return IFStyleCompilerFatalError;
case IFLexAssembly:
return IFStyleAssembly;
case IFLexHexDump:
return IFStyleHexDump;
case IFLexStatistics:
return IFStyleStatistics;
case IFLexProgress:
[[compiler progress] setPercentage: IFLexLastProgress];
if (IFLexLastProgressString) {
NSString* msg;
msg = [[NSString alloc] initWithBytes: IFLexLastProgressString
length: strlen(IFLexLastProgressString)-2
encoding: NSUTF8StringEncoding];
// (Second attempt if UTF-8 makes no sense)
if (msg == nil) msg = [[NSString alloc] initWithBytes: IFLexLastProgressString
length: strlen(IFLexLastProgressString)-2
encoding: NSISOLatin1StringEncoding];
[[compiler progress] setMessage: [msg autorelease]];
}
return IFStyleProgress;
}
return nil;
// Version strings have the form 'Foo Inform x.xx (Date)'
// MPW style errors and warnings have the form:
// File "file.h"; line 10 #
if ([[line substringWithRange: NSMakeRange(0, 4)] isEqualTo: @"File"]) {
// May be an MPW string
return nil;
}
return nil;
}
- (void)textStorageDidProcessEditing:(NSNotification *)aNotification {
NSTextStorage* storage = [compilerResults textStorage];
// Set the text to the base style
[storage beginEditing];
// For each line since highlightPos...
NSString* str = [storage string];
int len = [str length];
int newlinePos;
do {
int x;
newlinePos = -1;
for (x=highlightPos; x<len; x++) {
if ([str characterAtIndex: x] == '\n') {
newlinePos = x;
break;
}
}
if (newlinePos == -1) {
break;
}
// ... set the style appropriately
NSRange lineRange = NSMakeRange(highlightPos, (newlinePos-highlightPos)+1);
NSString* newStyle = [self styleForLine: [str substringWithRange: lineRange]];
if (newStyle != nil) {
[storage addAttributes: [styles objectForKey: newStyle]
range: lineRange];
}
highlightPos = newlinePos + 1;
} while (newlinePos != -1);
// Finish up
[storage endEditing];
[[NSRunLoop currentRunLoop] performSelector: @selector(scrollToEnd)
target: self
argument: nil
order: 128
modes: [NSArray arrayWithObject: NSDefaultRunLoopMode]];
}
// == The error OutlineView ==
- (void) addErrorForFile: (NSString*) file
atLine: (int) line
withType: (IFLex) type
message: (NSString*) message {
// Find the entry for this error message, if it exists. If not,
// add this as a new file...
int fileNum = [errorFiles indexOfObject: file];
if (fileNum == NSNotFound) {
fileNum = [errorFiles count];
[errorFiles addObject: file];
[errorMessages addObject: [NSMutableArray array]];
[compilerMessages reloadData];
[compilerMessages reloadItem: file
reloadChildren: YES];
[compilerMessages expandItem: file];
}
// Add an entry for this error message
NSMutableArray* fileMessages = [errorMessages objectAtIndex: fileNum];
NSArray* newMessage = [NSArray arrayWithObjects: message, [NSNumber numberWithInt: line], [NSNumber numberWithInt: type], [NSNumber numberWithInt: fileNum], nil];
[fileMessages addObject: newMessage];
// Update the outline view
[compilerMessages reloadData];
// Pop up the error view if required
if ([messageScroller frame].size.height == 0) {
NSRect newFrame = [messageScroller frame];
newFrame.size.height = messagesSize;
[messageScroller setFrame: newFrame];
NSRect splitFrame = [splitView frame];
NSRect resultFrame = [resultScroller frame];
resultFrame.size.height = splitFrame.size.height - newFrame.size.height - [splitView dividerThickness];
[resultScroller setFrame: resultFrame];
[splitView adjustSubviews];
}
// Notify the delegate
if (delegate != nil &&
[delegate respondsToSelector: @selector(compilerAddError:forFile:atLine:withType:message:)]) {
[delegate compilerAddError: self
forFile: file
atLine: line
withType: type
message: message];
}
}
- (int) outlineView:(NSOutlineView *)outlineView
numberOfChildrenOfItem:(id)item {
if (item == nil) {
return [errorFiles count];
}
int fileNum = [errorFiles indexOfObjectIdenticalTo: item];
if (fileNum == NSNotFound) {
return 0;
}
return [[errorMessages objectAtIndex: fileNum] count];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView
isItemExpandable:(id)item {
if (item == nil) return YES;
return [errorFiles indexOfObjectIdenticalTo: item] != NSNotFound;
}
- (id)outlineView:(NSOutlineView *)outlineView
child:(int)index
ofItem:(id)item {
if (item == nil) {
return [errorFiles objectAtIndex: index];
}
int fileNum = [errorFiles indexOfObjectIdenticalTo: item];
if (fileNum == NSNotFound) {
return nil;
}
return [[errorMessages objectAtIndex: fileNum] objectAtIndex: index];
}
- (id) outlineView:(NSOutlineView *)outlineView
objectValueForTableColumn:(NSTableColumn *)tableColumn
byItem:(id)item {
if ([item isKindOfClass: [NSString class]]) {
// Must be a filename
NSAttributedString* str = [[NSAttributedString alloc] initWithString: [item lastPathComponent]
attributes: [styles objectForKey: IFStyleFilename]];
return [str autorelease];
}
// Is an array of the form message, line, type
NSString* message = [item objectAtIndex: 0];
int line = [[item objectAtIndex: 1] intValue];
IFLex type = [[item objectAtIndex: 2] intValue];
NSDictionary* attr = [styles objectForKey: IFStyleCompilerMessage];
switch (type) {
case IFLexCompilerWarning:
attr = [styles objectForKey: IFStyleCompilerWarning];
break;
case IFLexCompilerError:
attr = [styles objectForKey: IFStyleCompilerError];
break;
case IFLexCompilerFatalError:
attr = [styles objectForKey: IFStyleCompilerFatalError];
break;
default:
break;
}
NSString* msg = [NSString stringWithFormat: @"L%i: %@", line, message];
NSAttributedString* res = [[NSAttributedString alloc] initWithString: msg
attributes: attr];
return [res autorelease];
}
- (void)outlineViewSelectionDidChange:(NSNotification *)notification {
NSObject* obj = [compilerMessages itemAtRow: [compilerMessages selectedRow]];
if (obj == nil) {
return; // Nothing selected
}
int fileNum = [errorFiles indexOfObjectIdenticalTo: obj];
if (fileNum != NSNotFound) {
return; // File item selected
}
// obj is an array of the form [message, line, type]
NSArray* msg = (NSArray*) obj;
// NSString* message = [msg objectAtIndex: 0];
int line = [[msg objectAtIndex: 1] intValue];
// IFLex type = [[msg objectAtIndex: 2] intValue];
fileNum = [[msg objectAtIndex: 3] intValue];
// Send to the delegate
if (delegate &&
[delegate respondsToSelector: @selector(errorMessageHighlighted:atLine:inFile:)]) {
[delegate errorMessageHighlighted: self
atLine: line
inFile: [errorFiles objectAtIndex: fileNum]];
}
return;
}
- (void) windowWillClose: (NSNotification*) not {
[self autorelease];
}
// Other information
- (int) makeTabViewItemNamed: (NSString*) tabName
withView: (NSView*) newView {
int newViewIndex = [auxViews count];
[auxViews addObject: newView];
[viewNames addObject: tabName];
if (delegate && [delegate respondsToSelector: @selector(viewSetHasUpdated:)]) {
[delegate viewSetHasUpdated: self];
}
return newViewIndex;
}
- (int) makeTabForURL: (NSURL*) url
named: (NSString*) tabName {
// Create a parent view
NSRect viewRect;
if (activeView == nil) {
viewRect = NSMakeRect(0,0, 100,100);
} else {
viewRect = [activeView frame];
}
NSView* aView = [[NSView alloc] initWithFrame: viewRect];
[aView setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
// Create a 'fake' web view which will get replaced when the view is actually displayed on screen
IFPretendWebView* pretendView = [[IFPretendWebView alloc] initWithFrame: [aView bounds]];
[pretendView setHostWindow: [[splitView superview] window]];
[pretendView setRequest: [[[NSURLRequest alloc] initWithURL: url] autorelease]];
[pretendView setPolicyDelegate: self];
[pretendView setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
// Add it to aView
[aView addSubview: [pretendView autorelease]];
// Add it to the list of tabs
return [self makeTabViewItemNamed: tabName
withView: [aView autorelease]];
}
- (int) makeTabForFile: (NSString*) file {
NSString* type = [[file pathExtension] lowercaseString];
if ([[NSApp delegate] isWebKitAvailable] && ([type isEqualTo: @"html"] ||
[type isEqualTo: @"htm"])) {
// Treat as a webkit URL
return [self makeTabForURL: [IFProjectPolicy fileURLWithPath: file]
named: [[NSBundle mainBundle] localizedStringForKey: [file lastPathComponent]
value: [[file lastPathComponent] stringByDeletingPathExtension]
table: @"CompilerOutput"]];
} else {
// Create the 'parent' view
NSView* aView = [[NSView alloc] initWithFrame: [activeView frame]];
// Create the 'pretend' text view
IFPretendTextView* pretendView = [[IFPretendTextView alloc] initWithFrame: [aView bounds]];
[pretendView setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
// Load the data for the file
NSString* textData = [[NSString alloc] initWithData: [NSData dataWithContentsOfFile: file]
encoding: NSUTF8StringEncoding];
// Set up the view
[pretendView setEventualString: [textData autorelease]];
// This is our new view
[aView addSubview: [pretendView autorelease]];
// Add the tab
return [self makeTabViewItemNamed: [[NSBundle mainBundle] localizedStringForKey: [file lastPathComponent]
value: [[file lastPathComponent] stringByDeletingPathExtension]
table: @"CompilerOutput"]
withView: [aView autorelease]];
}
}
- (void) showRuntimeError: (NSURL*) errorURL {
// Create a parent view
NSRect viewRect;
if (activeView == nil) {
viewRect = NSMakeRect(0,0, 100,100);
} else {
viewRect = [activeView frame];
}
NSView* aView = [[NSView alloc] initWithFrame: viewRect];
[aView setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
// Create a 'fake' web view which will get replaced when the view is actually displayed on screen
IFPretendWebView* pretendView = [[IFPretendWebView alloc] initWithFrame: [aView bounds]];
[pretendView setHostWindow: [[splitView superview] window]];
[pretendView setRequest: [[[NSURLRequest alloc] initWithURL: errorURL] autorelease]];
[pretendView setPolicyDelegate: self];
[pretendView setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
// This is our new view
[aView addSubview: [pretendView autorelease]];
[aView autorelease];
if (runtimeView >= 0) {
// Replace the existing runtime error view
[auxViews replaceObjectAtIndex: runtimeView
withObject: aView];
} else {
// Add a new runtime error view
runtimeView = [self makeTabViewItemNamed: [[NSBundle mainBundle] localizedStringForKey: @"Runtime errors"
value: @"Runtime errors"
table: nil]
withView: aView];
}
// Switch to the runtime view
[self switchToViewWithIndex: runtimeView];
}
- (void) showContentsOfFilesIn: (NSFileWrapper*) files
fromPath: (NSString*) path {
if (![files isDirectory]) {
return; // Nothing to do
}
// The set of files we should avoid showing
NSMutableSet* excludedFiles = [NSMutableSet set];
if (![[IFPreferences sharedPreferences] showDebuggingLogs]) {
[excludedFiles addObject: @"debug log.txt"];
[excludedFiles addObject: @"auto.inf"];
}
NSEnumerator* keyEnum = [[files fileWrappers] keyEnumerator];
NSString* key;
int preferredTabView = -1;
// If there is a compiler-supplied problems file, add this to the tab view
if (lastProblemURL != nil) {
// Create a web view for the new problems view
preferredTabView = [self makeTabForURL: lastProblemURL
named: [[NSBundle mainBundle] localizedStringForKey: @"Problems.html"
value: @"Problems"
table: @"CompilerOutput"]];
}
// Enumerate across the list of files in the filewrapper
while (key = [keyEnum nextObject]) {
NSString* type = [[key pathExtension] lowercaseString];
// Skip this file if it's in the excluded list
if ([excludedFiles containsObject: [key lowercaseString]]) continue;
// HTML, text and inf files go in a tab view showing various different status messages
// With NI, the problems file is most important: we substitute this if the compiler wants
if ((![[[key substringToIndex: 4] lowercaseString] isEqualToString: @"temp"]) &&
(lastProblemURL == nil || ![[[key stringByDeletingPathExtension] lowercaseString] isEqualToString: @"problems"]) &&
([type isEqualTo: @"inf"] ||
[type isEqualTo: @"txt"] ||
[type isEqualTo: @"html"] ||
[type isEqualTo: @"htm"])) {
int fileItem;
fileItem = [self makeTabForFile: [path stringByAppendingPathComponent: key]];
// 'Problems.html' is the preferred view. May also be called 'log of problems?'
// A file called 'Problems' is preferred to a file called 'log of problems'
if ([[[key stringByDeletingPathExtension] lowercaseString] isEqualToString: @"problems"]) {
preferredTabView = fileItem;
}
if (preferredTabView == nil &&
[[[key stringByDeletingPathExtension] lowercaseString] isEqualToString: @"log of problems"]) {
preferredTabView = fileItem;
}
}
}
if (preferredTabView >= 0) {
[self switchToViewWithIndex: preferredTabView];
}
}
- (void) clearTabViews {
// Do nothing if the only view available is already the split view
if ([auxViews count] <= 1) return;
// Clear all views except the split view
[auxViews removeObjectsInRange: NSMakeRange(1, [auxViews count]-1)];
[viewNames removeObjectsInRange: NSMakeRange(1, [viewNames count]-1)];
runtimeView = -1;
// Notify the delegate that the set of views has updated
if (delegate && [delegate respondsToSelector: @selector(viewSetHasUpdated:)]) {
[delegate viewSetHasUpdated: self];
}
// Switch to the split view if available
[self switchToViewWithIndex: splitViewIndex];
}
// == Delegate ==
- (void) setDelegate: (NSObject*) dg {
delegate = dg;
}
- (NSObject*) delegate {
return delegate;
}
// = Web policy delegate methods =
- (void) webView: (WebView *)sender
decidePolicyForNavigationAction: (NSDictionary *)actionInformation
request: (NSURLRequest *)request
frame: (WebFrame *)frame
decisionListener: (id<WebPolicyDecisionListener>)listener {
// Blah. Link failure if WebKit isn't available here. Constants aren't weak linked
// Double blah. WebNavigationTypeLinkClicked == null, but the action value == 0. Bleh
if ([[actionInformation objectForKey: WebActionNavigationTypeKey] intValue] == 0) {
NSURL* url = [request URL];
if ([[url scheme] isEqualTo: @"source"]) {
// We deal with these ourselves
[listener ignore];
// Format is 'source file name#line number'
NSString* path = [[[request URL] resourceSpecifier] stringByReplacingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
NSArray* components = [path componentsSeparatedByString: @"#"];
if ([components count] != 2) {
NSLog(@"Bad source URL: %@", path);
if ([components count] < 2) return;
// (try anyway)
}
NSString* sourceFile = [[components objectAtIndex: 0] stringByReplacingPercentEscapesUsingEncoding: NSUnicodeStringEncoding];
NSString* sourceLine = [[components objectAtIndex: 1] stringByReplacingPercentEscapesUsingEncoding: NSUnicodeStringEncoding];
// sourceLine can have format 'line10' or '10'. 'line10' is more likely
int lineNumber = [sourceLine intValue];
if (lineNumber == 0 && [[sourceLine substringToIndex: 4] isEqualToString: @"line"]) {
lineNumber = [[sourceLine substringFromIndex: 4] intValue];
}
if (delegate &&
[delegate respondsToSelector: @selector(errorMessageHighlighted:atLine:inFile:)]) {
[delegate errorMessageHighlighted: self
atLine: lineNumber
inFile: sourceFile];
}
// Finished
return;
}
// General URL policy
WebDataSource* activeSource = [frame dataSource];
if (activeSource == nil) {
activeSource = [frame provisionalDataSource];
if (activeSource != nil) {
NSLog(@"Using the provisional data source - frame not finished loading?");
}
}
if (activeSource == nil) {
NSLog(@"Unable to establish a datasource for this frame: will probably redirect anyway");
}
NSURL* absolute1 = [[[request URL] absoluteURL] standardizedURL];
NSURL* absolute2 = [[[[activeSource request] URL] absoluteURL] standardizedURL];
// We only redirect if the page is different to the current one
if (!([[absolute1 scheme] caseInsensitiveCompare: [absolute2 scheme]] == 0 &&
[[absolute1 path] caseInsensitiveCompare: [absolute2 path]] == 0 &&
([absolute1 query] == [absolute2 query] || [[absolute1 query] caseInsensitiveCompare: [absolute2 query]] == 0))) {
if (delegate &&
[delegate respondsToSelector: @selector(handleURLRequest:)]) {
if ([delegate handleURLRequest: request]) {
[listener ignore];
return;
}
}
}
}
// default action
[listener use];
}
- (void) setBlorbLocation: (NSString*) location {