forked from erkyrath/Inform7-IDE-Mac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIFProject.m
1292 lines (1014 loc) · 41.6 KB
/
IFProject.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
//
// IFProject.m
// Inform
//
// Created by Andrew Hunter on Wed Aug 27 2003.
// Copyright (c) 2003 Andrew Hunter. All rights reserved.
//
#import "IFProject.h"
#import "IFProjectController.h"
#import "IFPreferences.h"
#import "IFSyntaxStorage.h"
#import "IFNaturalHighlighter.h"
#import "IFInform6Highlighter.h"
#import "IFNaturalIntel.h"
#import "IFSharedContextMatcher.h"
#include "uuid/uuid.h"
NSString* IFProjectFilesChangedNotification = @"IFProjectFilesChangedNotification";
NSString* IFProjectWatchExpressionsChangedNotification = @"IFProjectWatchExpressionsChangedNotification";
NSString* IFProjectBreakpointsChangedNotification = @"IFProjectBreakpointsChangedNotification";
NSString* IFProjectSourceFileRenamedNotification = @"IFProjectSourceFileRenamedNotification";
NSString* IFProjectSourceFileDeletedNotification = @"IFProjectSourceFileDeletedNotification";
NSString* IFProjectStartedBuildingSyntaxNotification = @"IFProjectStartedBuildingSyntaxNotification";
NSString* IFProjectFinishedBuildingSyntaxNotification = @"IFProjectFinishedBuildingSyntaxNotification";
@implementation IFProject
- (IFContextMatcher*) syntaxDictionaryMatcherForFile: (NSString*) filename {
NSString* extn = [[filename pathExtension] lowercaseString];
IFContextMatcher* result = nil;
[matcherLock lock];
if ([extn isEqualToString: @"inf"] ||
[extn isEqualToString: @"i6"] ||
[extn isEqualToString: @"h"]) {
// Inform 6 file
result = [[inform6Matcher retain] autorelease];
} else if ([extn isEqualToString: @"ni"] ||
[extn isEqualToString: @""]) {
// Natural Inform file
result = [[inform7Matcher retain] autorelease];
}
[matcherLock unlock];
return result;
}
- (id<IFSyntaxHighlighter,NSObject>) highlighterForFilename: (NSString*) filename {
NSString* extn = [[filename pathExtension] lowercaseString];
if (![[IFPreferences sharedPreferences] enableSyntaxHighlighting]) return nil;
if ([extn isEqualToString: @"inf"] ||
[extn isEqualToString: @"i6"] ||
[extn isEqualToString: @"h"]) {
// Inform 6 file
return [[[IFInform6Highlighter alloc] init] autorelease];
} else if ([extn isEqualToString: @"ni"] ||
[extn isEqualToString: @""]) {
// Natural Inform file
return [[[IFNaturalHighlighter alloc] init] autorelease];
}
// No highlighter
return nil;
}
- (id<IFSyntaxIntelligence,NSObject>) intelligenceForFilename: (NSString*) filename {
NSString* extn = [[filename pathExtension] lowercaseString];
if (![[IFPreferences sharedPreferences] enableIntelligence]) return nil;
if ([extn isEqualToString: @"inf"] ||
[extn isEqualToString: @"i6"] ||
[extn isEqualToString: @"h"]) {
// Inform 6 file (no intelligence yet)
return nil;
} else if ([extn isEqualToString: @"ni"] ||
[extn isEqualToString: @""]) {
// Natural Inform file
return [[[IFNaturalIntel alloc] init] autorelease];
}
// No intelligence
return nil;
}
- (IFSyntaxStorage*) storageWithString: (NSString*) string
forFilename: (NSString*) filename {
// Fix any newlines in string
int initialLength = [string length];
int len = 0;
// Note that the string can only get shorter
unichar* fixedString = malloc(sizeof(unichar)*[string length]);
int x;
BOOL useFixedVersion = NO;
for (x=0; x<initialLength; x++) {
// Fetch the next character
unichar ch = [string characterAtIndex: x];
// See if we've got a newline
if (ch == 10) {
// Ignore the next character if it's a carriage return
if (x+1<initialLength && [string characterAtIndex: x+1] == 13) {
x++;
useFixedVersion = YES;
}
// Store a newline
fixedString[len++] = 10;
} else if (ch == 13) {
// Length doesn't always change, but we've got a CR line ending where a NL one should exist
useFixedVersion = YES;
// Ignore the next character if it's a newline
if (x+1<initialLength && [string characterAtIndex: x+1] == 10) {
x++;
}
// Store a newline
fixedString[len++] = 10;
} else {
// Copy this character through
fixedString[len++] = ch;
}
}
// Use the fixed string if it's different from the original
if (useFixedVersion) {
NSLog(@"Warning: reformated newlines in file '%@'", filename);
string = [NSString stringWithCharacters: fixedString
length: len];
}
free(fixedString);
// Create the syntax-highlighting text storage object
IFSyntaxStorage* res = [[IFSyntaxStorage alloc] initWithString: string];
// Set the 'intelligence' and highlighting style appropriately
[res setIntelligence: [self intelligenceForFilename: filename]];
[res setHighlighter: [self highlighterForFilename: filename]];
[res setElasticTabs: [settings elasticTabs]];
[res setDelegate: self];
return [res autorelease];
}
- (IFSyntaxStorage*) storageWithAttributedString: (NSAttributedString*) string
forFilename: (NSString*) filename {
IFSyntaxStorage* res = [[IFSyntaxStorage alloc] initWithAttributedString: string];
[res setIntelligence: [self intelligenceForFilename: filename]];
[res setHighlighter: [self highlighterForFilename: filename]];
[res setElasticTabs: [settings elasticTabs]];
[res setDelegate: self];
return [res autorelease];
}
- (IFSyntaxStorage*) storageWithData: (NSData*) fileContents
forFilename: (NSString*) filename {
BOOL loadAsRtf = NO;
if ([[[filename pathExtension] lowercaseString] isEqualToString: @"rtf"])
loadAsRtf = YES;
if (loadAsRtf) {
return [self storageWithAttributedString: [[[NSAttributedString alloc] initWithRTF: fileContents
documentAttributes: nil] autorelease]
forFilename: filename];
} else {
// First, try loading as a UTF-8 string (this is the default behaviour)
NSString* fileString = [[NSString alloc] initWithData: fileContents
encoding: NSUTF8StringEncoding];
if (fileString == nil) {
// This can happen if fileString isn't in UTF-8 format for some reason (perhaps due to external editing)
// Retry with Latin-1 and log a warning
NSLog(@"Warning: file '%@' cannot be interpreted as UTF-8: trying Latin-1", filename);
fileString = [[NSString alloc] initWithData: fileContents
encoding: NSISOLatin1StringEncoding];
}
if (fileString == nil) {
// We can't interpret this file in any way - report the failure. An exception will be thrown later
NSLog(@"Warning: no text available for file '%@'", filename);
}
return [self storageWithString: [fileString autorelease]
forFilename: filename];
}
}
// == Initialisation ==
- (id) init {
self = [super init];
if (self) {
settings = [[IFCompilerSettings allocWithZone: [self zone]] init];
projectFile = nil;
sourceFiles = nil;
mainSource = nil;
singleFile = YES;
[settings setElasticTabs: [[IFPreferences sharedPreferences] elasticTabs]];
skein = [[ZoomSkein alloc] init];
compiler = [[IFCompiler allocWithZone: [self zone]] init];
notes = [[NSTextStorage alloc] initWithString: @""];
watchExpressions = [[NSMutableArray alloc] init];
breakpoints = [[NSMutableArray alloc] init];
inform6Matcher = [[IFSharedContextMatcher matcherForInform6] retain];
inform7Matcher = [[IFSharedContextMatcher matcherForInform7] retain];
}
return self;
}
- (void) dealloc {
if (sourceFiles) [sourceFiles release];
if (projectFile) [projectFile release];
if (mainSource) [mainSource release];
if (notes) [notes release];
if (indexFile) [indexFile release];
if (skein) [skein release];
[settings release];
[compiler release];
[watchExpressions release];
[breakpoints release];
[inform6Matcher release];
[inform7Matcher release];
if (mainThreadPort) [mainThreadPort release];
if (subThreadPort) [subThreadPort release];
if (subThreadConnection) [subThreadConnection release];
[super dealloc];
}
// == Loading/saving ==
- (BOOL) readFromFile: (NSString*) fileName
ofType: (NSString*) fileType {
if ([fileType isEqualTo: @"Inform project file"] || [fileType isEqualTo: @"Inform project"]) {
if (projectFile) [projectFile release];
if (sourceFiles) [sourceFiles release];
if (mainSource) [mainSource release];
// Open the directory
projectFile = [[IFProjectFile allocWithZone: [self zone]]
initWithPath: fileName];
if (![projectFile isDirectory]) {
[projectFile release];
projectFile = nil;
return NO;
}
// Refresh the settings
if (settings) [settings release];
settings = [[projectFile settings] retain];
// Turn the source directory into NSTextStorages
NSFileWrapper* sourceDir = [[projectFile fileWrappers] objectForKey: @"Source"];
if (sourceDir == nil ||
![sourceDir isDirectory]) {
[projectFile release];
projectFile = nil;
return NO;
}
if (sourceFiles) [sourceFiles release];
sourceFiles = [[NSMutableDictionary allocWithZone: [self zone]] init];
NSDictionary* source = [sourceDir fileWrappers];
NSEnumerator* sourceEnum = [source keyEnumerator];
NSString* key;
if (mainSource) [mainSource release];
mainSource = nil;
// Load all the source files
while (key = [sourceEnum nextObject]) {
IFSyntaxStorage* text;
if (![[source objectForKey: key] isRegularFile]) continue;
NSData* regularFileContents = [[source objectForKey: key] regularFileContents];
text = [self storageWithData: regularFileContents
forFilename: key];
[sourceFiles setObject: text
forKey: key];
if ([[key pathExtension] isEqualTo: @"inf"] ||
[[key pathExtension] isEqualTo: @"ni"]) {
if (mainSource) [mainSource release];
mainSource = [key copy];
}
}
// Re-create the settings as required
if (settings == nil) {
if ([[mainSource pathExtension] isEqualTo: @"ni"]) {
// Standard natural inform settings
settings = [[IFCompilerSettings alloc] init];
[settings setLibraryToUse: @"Natural"];
[settings setUsingNaturalInform: YES];
[settings setElasticTabs: [[IFPreferences sharedPreferences] elasticTabs]];
} else {
// Standard settings
settings = [[IFCompilerSettings alloc] init];
[settings setElasticTabs: [[IFPreferences sharedPreferences] elasticTabs]];
}
}
singleFile = NO;
// Create a uuid.txt file, if it doesn't already exit
if ([[projectFile fileWrappers] objectForKey: @"uuid.txt"] == nil) {
// Generate a UUID string
uuid_t newUID;
uuid_clear(newUID);
uuid_generate(newUID);
char uid[40];
uuid_unparse(newUID, uid);
NSString* uidString = [NSString stringWithCString: uid];
[projectFile addRegularFileWithContents: [uidString dataUsingEncoding: NSUTF8StringEncoding]
preferredFilename: @"uuid.txt"];
}
// Load the notes (if present)
NSFileWrapper* noteWrapper = [[projectFile fileWrappers] objectForKey: @"notes.rtf"];
if (noteWrapper != nil && [noteWrapper regularFileContents] != nil) {
NSTextStorage* newNotes = [[NSTextStorage alloc] initWithRTF: [noteWrapper regularFileContents]
documentAttributes: nil];
if (newNotes) {
if (notes) [notes release];
notes = newNotes;
}
}
// Load the skein file (if present)
NSFileWrapper* skeinWrapper = [[projectFile fileWrappers] objectForKey: @"Skein.skein"];
if (skeinWrapper != nil && [skeinWrapper regularFileContents] != nil) {
[skein parseXmlData: [skeinWrapper regularFileContents]];
[skein setActiveItem: [skein rootItem]];
}
// Load the watchpoints file (if present)
NSFileWrapper* watchWrapper = [[projectFile fileWrappers] objectForKey: @"Watchpoints.plist"];
if (watchWrapper != nil && [watchWrapper regularFileContents] != nil) {
NSString* propError = nil;
NSPropertyListFormat format = NSPropertyListXMLFormat_v1_0;
NSArray* watchpointsLoaded = [NSPropertyListSerialization propertyListFromData: [watchWrapper regularFileContents]
mutabilityOption: NSPropertyListImmutable
format: &format
errorDescription: &propError];
if (watchpointsLoaded && [watchpointsLoaded isKindOfClass: [NSArray class]]) {
[watchExpressions release];
watchExpressions = [watchpointsLoaded mutableCopy];
}
}
// Load the breakpoints file (if present)
NSFileWrapper* breakWrapper = [[projectFile fileWrappers] objectForKey: @"Breakpoints.plist"];
if (breakWrapper != nil && [breakWrapper regularFileContents] != nil) {
NSString* propError = nil;
NSPropertyListFormat format = NSPropertyListXMLFormat_v1_0;
NSArray* breakpointsLoaded = [NSPropertyListSerialization propertyListFromData: [breakWrapper regularFileContents]
mutabilityOption: NSPropertyListImmutable
format: &format
errorDescription: &propError];
if (breakpointsLoaded && [breakpointsLoaded isKindOfClass: [NSArray class]]) {
[breakpoints release];
breakpoints = [breakpointsLoaded mutableCopy];
}
}
// Load the index file (if present)
[self reloadIndexFile];
return YES;
} else if ([fileType isEqualTo: @"Inform source file"] ||
[fileType isEqualTo: @"Inform header file"]) {
// No project file
if (projectFile) [projectFile release];
projectFile = nil;
// Default settings
if (settings) [settings release];
settings = [[IFCompilerSettings allocWithZone: [self zone]] init];
[settings setElasticTabs: [[IFPreferences sharedPreferences] elasticTabs]];
// Load the single file
NSString* theFile = [NSString stringWithContentsOfFile: fileName];
IFSyntaxStorage* text = [[IFSyntaxStorage alloc] initWithString: theFile];
[text setHighlighter: [[[IFInform6Highlighter alloc] init] autorelease]];
if (sourceFiles) [sourceFiles release];
sourceFiles = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
[text autorelease],
[fileName lastPathComponent], nil];
if (mainSource) [mainSource release];
mainSource = [[fileName lastPathComponent] copy];
singleFile = YES;
return YES;
} else if ([fileType isEqualTo: @"Natural Inform source file"]) {
// No project file
if (projectFile) [projectFile release];
projectFile = nil;
// Default settings (Natural Inform)
if (settings) [settings release];
settings = [[IFCompilerSettings allocWithZone: [self zone]] init];
[settings setLibraryToUse: @"Natural"];
[settings setUsingNaturalInform: YES];
[settings setElasticTabs: [[IFPreferences sharedPreferences] elasticTabs]];
// Load the single file
NSString* theFile = [NSString stringWithContentsOfFile: fileName];
IFSyntaxStorage* text = [[IFSyntaxStorage alloc] initWithString: theFile];
[text setIntelligence: [[[IFNaturalIntel alloc] init] autorelease]];
[text setHighlighter: [[[IFNaturalHighlighter alloc] init] autorelease]];
[text setElasticTabs: [settings elasticTabs]];
if (sourceFiles) [sourceFiles release];
sourceFiles = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
[text autorelease],
[fileName lastPathComponent], nil];
if (mainSource) [mainSource release];
mainSource = [[fileName lastPathComponent] copy];
singleFile = YES;
return YES;
} else if ([fileType isEqualTo: @"Inform Extension Directory"]) {
// Opening a plain ole extension
editingExtension = YES;
singleFile = NO;
// Open the directory
projectFile = [[IFProjectFile allocWithZone: [self zone]]
initWithPath: fileName];
if (![projectFile isDirectory]) {
[projectFile release];
projectFile = nil;
return NO;
}
// Turn the source directory into NSTextStorages
NSFileWrapper* sourceDir = projectFile;
if (sourceDir == nil ||
![sourceDir isDirectory]) {
[projectFile release];
projectFile = nil;
return NO;
}
if (sourceFiles) [sourceFiles release];
sourceFiles = [[NSMutableDictionary allocWithZone: [self zone]] init];
NSDictionary* source = [sourceDir fileWrappers];
NSEnumerator* sourceEnum = [source keyEnumerator];
NSString* key;
if (mainSource) [mainSource release];
mainSource = nil;
// Load all the source files
while (key = [sourceEnum nextObject]) {
IFSyntaxStorage* text;
if ([key characterAtIndex: 0] == '.') continue;
if (![[source objectForKey: key] isRegularFile]) continue;
NSData* regularFileContents = [[source objectForKey: key] regularFileContents];
text = [self storageWithData: regularFileContents
forFilename: key];
[sourceFiles setObject: text
forKey: key];
if ([[key pathExtension] isEqualTo: @"inf"] ||
[[key pathExtension] isEqualTo: @"ni"] ||
[[key pathExtension] isEqualTo: @""]) {
if (mainSource) [mainSource release];
mainSource = [key copy];
}
}
// Create an 'Untitled' file if there's no mainSource
if (!mainSource) {
mainSource = [@"Untitled" retain];
[sourceFiles setObject: [self storageWithString: @""
forFilename: @"Untitled"]
forKey: mainSource];
}
return YES;
}
return NO;
}
- (BOOL) writeToFile: (NSString*) fileName ofType: (NSString*) fileType {
if ([fileType isEqualTo: @"Inform project file"] || [fileType isEqualTo: @"Inform project"]) {
[self prepareForSaving];
return [projectFile writeToFile: fileName
atomically: YES
updateFilenames: YES];
} else if ([fileType isEqualToString: @"Inform source file"] ||
[fileType isEqualToString: @"Inform header file"]) {
NSTextStorage* theFile = [self storageForFile: [self mainSourceFile]];
if (theFile == nil) {
NSLog(@"Bug: no file storage found");
}
return [[theFile string] writeToFile: fileName
atomically: YES];
} else if ([fileType isEqualToString: @"Inform Extension Directory"]) {
[self prepareForSaving];
return [projectFile writeToFile: fileName
atomically: YES
updateFilenames: YES];
}
return NO;
}
- (BOOL) addFile: (NSString*) newFile {
if ([sourceFiles objectForKey: newFile] != nil) return NO;
if (singleFile) return NO;
[sourceFiles setObject: [self storageWithString: @""
forFilename: newFile]
forKey: newFile];
[[NSNotificationCenter defaultCenter] postNotificationName: IFProjectFilesChangedNotification
object: self];
return YES;
}
- (BOOL) removeFile: (NSString*) oldFile {
if ([sourceFiles objectForKey: oldFile] == nil) return YES; // Deleting a non-existant file always succeeds
if (singleFile) return NO;
[sourceFiles removeObjectForKey: oldFile];
[[NSNotificationCenter defaultCenter] postNotificationName: IFProjectSourceFileDeletedNotification
object: self
userInfo: [NSDictionary dictionaryWithObjectsAndKeys:
oldFile, @"OldFilename",
nil]];
[[NSNotificationCenter defaultCenter] postNotificationName: IFProjectFilesChangedNotification
object: self];
return YES;
}
- (BOOL) renameFile: (NSString*) oldFile
withNewName: (NSString*) newFile {
if ([sourceFiles objectForKey: oldFile] == nil) return NO;
if ([sourceFiles objectForKey: newFile] != nil) return NO;
if (singleFile) return NO;
NSTextStorage* oldFileStorage = [[sourceFiles objectForKey: oldFile] retain];
[sourceFiles removeObjectForKey: oldFile];
[sourceFiles setObject: oldFileStorage
forKey: [[newFile copy] autorelease]];
[[NSNotificationCenter defaultCenter] postNotificationName: IFProjectSourceFileRenamedNotification
object: self
userInfo: [NSDictionary dictionaryWithObjectsAndKeys:
[[oldFile copy] autorelease], @"OldFilename",
[[newFile copy] autorelease], @"NewFilename",
nil]];
[[NSNotificationCenter defaultCenter] postNotificationName: IFProjectFilesChangedNotification
object: self];
return YES;
}
// == General housekeeping ==
- (void)windowControllerDidLoadNib:(NSWindowController *) aController {
[super windowControllerDidLoadNib:aController];
}
- (void)makeWindowControllers {
IFProjectController *aController = [[IFProjectController allocWithZone:[self zone]] init];
[self addWindowController:aController];
[aController release];
}
// == Document info ==
- (IFCompilerSettings*) settings {
return settings;
}
- (IFCompiler*) compiler {
return compiler;
}
- (BOOL) singleFile {
return singleFile;
}
// == Getting the files ==
- (void) prepareForSaving {
// Update the project file wrapper from whatever is on the disk
// (Implement me)
// Clean out any files that we aren't using any more, if set in the preferences
if ([[IFPreferences sharedPreferences] cleanProjectOnClose]) {
[self cleanOutUnnecessaryFiles: NO];
}
// Output all the source files to the project file wrapper
NSEnumerator* keyEnum = [sourceFiles keyEnumerator];
NSString* key;
NSFileWrapper* source;
if (!editingExtension) {
source = [[NSFileWrapper alloc] initDirectoryWithFileWrappers: nil];
[source setPreferredFilename: @"Source"];
[source setFilename: @"Source"];
} else {
//source = [projectFile retain];
source = [[IFProjectFile alloc] initDirectoryWithFileWrappers: [NSDictionary dictionary]];
[source setPreferredFilename: @"Source"];
[projectFile release];
projectFile = [source retain];
}
while (key = [keyEnum nextObject]) {
NSData* data;
NSFileWrapper* file;
if ([[key pathExtension] isEqualToString: @"rtf"]) {
NSAttributedString* str = [sourceFiles objectForKey: key];
data = [str RTFFromRange: NSMakeRange(0, [str length]) documentAttributes: nil];
} else {
data = [[[sourceFiles objectForKey: key] string] dataUsingEncoding: NSUTF8StringEncoding];
}
file = [[NSFileWrapper alloc] initRegularFileWithContents: data];
[file setFilename: key];
[file setPreferredFilename: key];
[source addFileWrapper: [file autorelease]];
}
if (editingExtension) {
// That's all folks
[source release];
return;
}
// Replace the source file wrapper
[projectFile removeFileWrapper: [[projectFile fileWrappers] objectForKey: @"Source"]];
[projectFile addFileWrapper: [source autorelease]];
// The notes file
NSFileWrapper* notesWrapper = [[[NSFileWrapper alloc] initRegularFileWithContents:
[notes RTFFromRange: NSMakeRange(0, [notes length])
documentAttributes: nil]] autorelease];
[notesWrapper setPreferredFilename: @"notes.rtf"];
[projectFile removeFileWrapper: [[projectFile fileWrappers] objectForKey: @"notes.rtf"]];
[projectFile addFileWrapper: notesWrapper];
// The skein file
NSFileWrapper* skeinWrapper = [[[NSFileWrapper alloc] initRegularFileWithContents:
[[@"<?xml version=\"1.0\"?>\n" stringByAppendingString: [skein xmlData]] dataUsingEncoding: NSUTF8StringEncoding]] autorelease];
[skeinWrapper setPreferredFilename: @"Skein.skein"];
[projectFile removeFileWrapper: [[projectFile fileWrappers] objectForKey: @"Skein.skein"]];
[projectFile addFileWrapper: skeinWrapper];
// The watchpoints file
[projectFile removeFileWrapper: [[projectFile fileWrappers] objectForKey: @"Watchpoints.plist"]];
if ([watchExpressions count] > 0) {
NSString* plistError = nil;
NSFileWrapper* watchWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:
[NSPropertyListSerialization dataFromPropertyList: watchExpressions
format: NSPropertyListXMLFormat_v1_0
errorDescription: &plistError]];
[watchWrapper setPreferredFilename: @"Watchpoints.plist"];
[projectFile addFileWrapper: watchWrapper];
[watchWrapper release];
}
// The breakpoints file
[projectFile removeFileWrapper: [[projectFile fileWrappers] objectForKey: @"Breakpoints.plist"]];
if ([breakpoints count] > 0) {
NSString* plistError = nil;
NSFileWrapper* breakWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:
[NSPropertyListSerialization dataFromPropertyList: breakpoints
format: NSPropertyListXMLFormat_v1_0
errorDescription: &plistError]];
[breakWrapper setPreferredFilename: @"Breakpoints.plist"];
[projectFile addFileWrapper: breakWrapper];
[breakWrapper release];
}
// Setup the settings
[projectFile setSettings: settings];
}
- (NSString*) mainSourceFile {
if (singleFile || editingExtension) return mainSource;
NSFileWrapper* sourceDir = [[projectFile fileWrappers] objectForKey: @"Source"];
NSDictionary* source = [sourceDir fileWrappers];
NSEnumerator* sourceEnum = [source keyEnumerator];
NSString* key;
if (mainSource) [mainSource autorelease];
mainSource = nil;
while (key = [sourceEnum nextObject]) {
if ([[key pathExtension] isEqualTo: @"inf"] ||
[[key pathExtension] isEqualTo: @"ni"]) {
if (mainSource) [mainSource autorelease];
mainSource = [key copy];
}
}
return mainSource;
}
- (NSTextStorage*) storageForFile: (NSString*) sourceFile {
NSTextStorage* storage;
NSString* originalSourceFile = sourceFile;
NSString* sourceDir = [[[self fileName] stringByAppendingPathComponent: @"Source"] stringByStandardizingPath];
if (editingExtension) {
// Special case: we're editing an extension, so source files are in the root directory
sourceDir = [[self fileName] stringByStandardizingPath];
}
if (projectFile == nil && [[sourceFile lastPathComponent] isEqualToString: [[self fileName] lastPathComponent]]) {
if (![sourceFile isAbsolutePath]) {
// Special case: when we're editing an individual file, then we always use that filename if possible
sourceFile = [self fileName];
}
}
// Refuse to return storage for files outside the project directory
if (sourceDir && [sourceFile isAbsolutePath]) {
if (![[sourceFile stringByStandardizingPath] hasPrefix: sourceDir]) {
return nil;
}
}
if (![sourceFile isAbsolutePath]) {
// Force absolute path
sourceFile = [[sourceDir stringByAppendingPathComponent: sourceFile] stringByStandardizingPath];
if (![[NSFileManager defaultManager] fileExistsAtPath: sourceFile]) {
// project/Source/whatever doesn't exist: try project/whatever
sourceFile = [[[self fileName] stringByAppendingPathComponent: originalSourceFile] stringByStandardizingPath];
if (![[NSFileManager defaultManager] fileExistsAtPath: sourceFile]) {
// If neither exists, use project/Source/whatever by default
sourceFile = [[sourceDir stringByAppendingPathComponent: sourceFile] stringByStandardizingPath];
}
}
}
if ([sourceFile isAbsolutePath]) {
// Absolute path
if ([[[sourceFile stringByDeletingLastPathComponent] stringByStandardizingPath] isEqualToString: sourceDir]) {
return [sourceFiles objectForKey: [sourceFile lastPathComponent]];
}
if (![[NSFileManager defaultManager] fileExistsAtPath: sourceFile]) {
return nil;
}
// Temporary text storage
NSString* textData = [[NSString alloc] initWithData: [NSData dataWithContentsOfFile: sourceFile]
encoding: NSUTF8StringEncoding];
if (textData == nil) {
// Sometimes a file cannot be interpreted using UTF-8: present something in this case
textData = [[NSString alloc] initWithData: [NSData dataWithContentsOfFile: sourceFile]
encoding: NSISOLatin1StringEncoding];
}
storage = [self storageWithString: textData
forFilename: sourceFile];
return storage;
} else {
// Not absolute path
}
return [sourceFiles objectForKey: sourceFile];
}
- (BOOL) fileIsTemporary: (NSString*) sourceFile {
// If the filename is Source/Whatever, make it just Whatever
if ([[sourceFile stringByDeletingLastPathComponent] isEqualToString: @"Source"]) {
sourceFile = [sourceFile lastPathComponent];
}
// Work out the source directory
NSString* sourceDir = [[[self fileName] stringByAppendingPathComponent: @"Source"] stringByStandardizingPath];
if (editingExtension) {
// Special case: we're editing an extension, so source files are in the root directory
sourceDir = [[self fileName] stringByStandardizingPath];
}
if (projectFile == nil && [[sourceFile lastPathComponent] isEqualToString: [[self fileName] lastPathComponent]]) {
if (![sourceFile isAbsolutePath]) {
// Special case: when we're editing an individual file, then we always use that filename if possible
sourceFile = [self fileName];
}
}
sourceFile = [sourceFile stringByStandardizingPath];
sourceDir = [sourceDir stringByStandardizingPath];
NSString* filename = [[self fileName] stringByStandardizingPath];
if ([sourceFile isAbsolutePath]) {
// Must begin with our filename/source
if (projectFile == nil) {
// Must be our filename
if ([filename isEqualToString: sourceFile])
return NO;
else
return YES;
}
if ([[sourceFile stringByDeletingLastPathComponent] isEqualToString: sourceDir]) {
return NO;
} else {
return YES;
}
} else {
// Must be in the list of project files
if ([sourceFiles objectForKey: sourceFile] != nil) {
return NO;
} else {
return YES;
}
}
return YES;
}
- (IFProjectFile*) projectFile {
return projectFile;
}
- (NSDictionary*) sourceFiles {
return sourceFiles;
}
- (NSString*) pathForFile: (NSString*) file {
if ([file isAbsolutePath]) return [file stringByStandardizingPath];
if (!editingExtension)
return [[[[self fileName] stringByAppendingPathComponent: @"Source"] stringByAppendingPathComponent: file] stringByStandardizingPath];
else
return [[[self fileName] stringByAppendingPathComponent: file] stringByStandardizingPath];
if ([sourceFiles objectForKey: file] != nil) {
return [[[self fileName] stringByAppendingPathComponent: @"Source"] stringByAppendingPathComponent: file];
}
// FIXME: search libraries
return file;
}
- (NSString*) materialsPath {
// Work out the location of the materials folder
NSString* projectPath = [self fileName];
NSString* projectName = [[projectPath lastPathComponent] stringByDeletingPathExtension];
NSString* materialsName = [NSString stringWithFormat: @"%@ Materials", projectName];
NSString* materialsPath = [[projectPath stringByDeletingLastPathComponent] stringByAppendingPathComponent: materialsName];
return materialsPath;
}
- (NSTextStorage*) notes {
return notes;
}
// = The index file =
- (IFIndexFile*) indexFile {
return indexFile;
}
- (void) reloadIndexFile {
if (singleFile) return; // Nothing to do
if (indexFile) [indexFile release];
indexFile = nil;
indexFile = [[IFIndexFile alloc] initWithContentsOfFile: [[[self fileName] stringByAppendingPathComponent: @"Index"] stringByAppendingPathComponent: @"Headings.xml"]];
}
- (void) reloadIndexDirectory {
// Nothing to do if this is a single file
if (singleFile) return;
// Try to get the index file wrapper
NSFileWrapper* oldIndexWrapper = [[projectFile fileWrappers] objectForKey: @"Index"];
if (oldIndexWrapper && [self fileName]) {
// Load in the index again
NSFileWrapper* indexWrapper = nil;
NSString* indexPath = [[self fileName] stringByAppendingPathComponent: @"Index"];
if ([[NSFileManager defaultManager] fileExistsAtPath: indexPath]) {
indexWrapper = [[[NSFileWrapper alloc] initWithPath: indexPath] autorelease];
[indexWrapper setPreferredFilename: @"Index"];
}
// Remove the old index wrapper
if (oldIndexWrapper) {
[projectFile removeFileWrapper: oldIndexWrapper];
}
// Replace with the new one
if (indexWrapper) {
[projectFile addFileWrapper: indexWrapper];
}
}
}
- (BOOL) editingExtension {
return editingExtension;
}
// = The skein =
- (ZoomSkein*) skein {
return skein;
}
// = Watch expressions =
- (void) addWatchExpression: (NSString*) expression {
[watchExpressions addObject: [[expression copy] autorelease]];
}
- (void) replaceWatchExpressionAtIndex: (unsigned) index
withExpression: (NSString*) expression {
[watchExpressions replaceObjectAtIndex: index
withObject: [[expression copy] autorelease]];
}
- (void) removeWatchExpressionAtIndex: (unsigned) index {
[watchExpressions removeObjectAtIndex: index];