forked from KosmicTask/Fragaria
-
Notifications
You must be signed in to change notification settings - Fork 3
/
MGSTextMenuController.m
1225 lines (976 loc) · 43.3 KB
/
MGSTextMenuController.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
/*
MGSFragaria
Written by Jonathan Mitchell, [email protected]
Find the latest version at https://github.com/mugginsoft/Fragaria
Smultron version 3.6b1, 2009-09-12
Written by Peter Borg, [email protected]
Find the latest version at http://smultron.sourceforge.net
Copyright 2004-2009 Peter Borg
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
#import "MGSFragaria.h"
#import "MGSFragariaFramework.h"
// class extension
@interface MGSTextMenuController()
- (void)setEdited:(BOOL)aBool;
- (void)reloadText:(id)sender;
- (void)performUndoChangeEncoding:(id)sender;
- (void)performUndoChangeLineEndings:(id)sender;
@end
@implementation MGSTextMenuController
static id sharedInstance = nil;
#pragma mark -
#pragma mark Class methods
/*
+ sharedInstance
*/
+ (MGSTextMenuController *)sharedInstance
{
if (sharedInstance == nil) {
sharedInstance = [[super allocWithZone:NULL] init];
}
return sharedInstance;
}
/*
+ allocWithZone:
alloc with zone for singleton
*/
+ (id)allocWithZone:(NSZone *)zone
{
#pragma unused(zone)
return [[self sharedInstance] retain];
}
#pragma mark -
#pragma mark Instance methods
/*
- init
*/
- (id)init
{
if (sharedInstance == nil) {
sharedInstance = [super init];
}
return sharedInstance;
}
/*
- setEdited:
*/
- (void)setEdited:(BOOL)aBool
{
if ([[SMLCurrentDocument valueForKey:MGSFOIsEdited] boolValue] != aBool) {
[[MGSFragaria currentInstance] setObject:[NSNumber numberWithBool:aBool] forKey:MGSFOIsEdited];
}
}
#pragma mark -
#pragma mark NSCopying
/*
- copyWithZone:
copy with zone for singleton
*/
- (id)copyWithZone:(NSZone *)zone
{
#pragma unused(zone)
return self;
}
#pragma mark -
#pragma mark Menu handling
/*
- buildEncodingsMenus
*/
- (void)buildEncodingsMenus
{
[SMLBasic removeAllItemsFromMenu:textEncodingMenu];
[SMLBasic removeAllItemsFromMenu:reloadTextWithEncodingMenu];
NSArray *encodingsArray = [SMLBasic fetchAll:@"EncodingSortKeyName"];
NSEnumerator *enumerator = [encodingsArray reverseObjectEnumerator];
id item;
NSMenuItem *menuItem;
for (item in enumerator) {
if ([[item valueForKey:@"active"] boolValue] == YES) {
NSUInteger encoding = [[item valueForKey:@"encoding"] unsignedIntegerValue];
menuItem = [[[NSMenuItem alloc] initWithTitle:[NSString localizedNameOfStringEncoding:encoding] action:@selector(changeEncodingAction:) keyEquivalent:@""] autorelease];
[menuItem setTag:encoding];
[menuItem setTarget:self];
[textEncodingMenu insertItem:menuItem atIndex:0];
}
}
enumerator = [encodingsArray reverseObjectEnumerator];
for (item in enumerator) {
if ([[item valueForKey:@"active"] boolValue] == YES) {
NSUInteger encoding = [[item valueForKey:@"encoding"] unsignedIntegerValue];
menuItem = [[[NSMenuItem alloc] initWithTitle:[NSString localizedNameOfStringEncoding:encoding] action:@selector(reloadText:) keyEquivalent:@""] autorelease];
[menuItem setTag:encoding];
[menuItem setTarget:self];
[reloadTextWithEncodingMenu insertItem:menuItem atIndex:0];
}
}
}
/*
- buildSyntaxDefinitionsMenu
*/
- (void)buildSyntaxDefinitionsMenu
{
NSArray *syntaxDefinitions = [SMLBasic fetchAll:@"SyntaxDefinitionSortKeySortOrder"];
NSEnumerator *enumerator = [syntaxDefinitions reverseObjectEnumerator];
NSMenuItem *menuItem;
NSInteger tag = [syntaxDefinitions count] - 1;
for (id item in enumerator) {
menuItem = [[[NSMenuItem alloc] initWithTitle:[item valueForKey:@"name"] action:@selector(changeSyntaxDefinitionAction:) keyEquivalent:@""] autorelease];
[menuItem setTag:tag];
[menuItem setTarget:self];
[syntaxDefinitionMenu insertItem:menuItem atIndex:0];
tag--;
}
}
/*
- validateMenuItem:
*/
- (BOOL)validateMenuItem:(NSMenuItem *)anItem
{
BOOL enableMenuItem = YES;
SEL action = [anItem action];
id responder = [SMLCurrentWindow firstResponder];
if (![responder isKindOfClass:[SMLTextView class]]) {
return NO;
}
// All items who should only be active if something is selected
if (action == @selector(removeNeedlessWhitespaceAction:) ||
action == @selector(removeLineEndingsAction:) ||
action == @selector(entabAction:) ||
action == @selector(detabAction:) ||
action == @selector(capitaliseAction:) ||
action == @selector(toUppercaseAction:) ||
action == @selector(toLowercaseAction:)
) {
if ([SMLCurrentTextView selectedRange].length < 1) {
enableMenuItem = NO;
}
}
// Items which should be active if nothing is selected
else if (action == @selector(toggleBreakpointAction:)) {
if ([SMLCurrentTextView selectedRange].length > 0) {
enableMenuItem = NO;
}
}
// Comment Or Uncomment
else if (action == @selector(commentOrUncommentAction:) ) {
if ([[[SMLCurrentDocument valueForKey:ro_MGSFOSyntaxColouring] valueForKey:@"firstSingleLineComment"] isEqualToString:@""]) {
enableMenuItem = NO;
}
}
return enableMenuItem;
}
/*
- emptyDummyAction:
*/
- (IBAction)emptyDummyAction:(id)sender
{
// An easy way to enable menu items with submenus without setting an action which actually does something
#pragma unused(sender)
}
#pragma mark -
#pragma mark Endcoding
/*
- changeEncodingAction:
*/
- (void)changeEncodingAction:(id)sender
{
NSUInteger encoding = [sender tag];
id document = SMLCurrentDocument;
[[[document valueForKey:ro_MGSFOSyntaxColouring] undoManager] registerUndoWithTarget:self selector:@selector(performUndoChangeEncoding:) object:[NSArray arrayWithObject:[document valueForKey:@"encoding"]]];
[[[document valueForKey:ro_MGSFOSyntaxColouring] undoManager] setActionName:NAME_FOR_UNDO_CHANGE_ENCODING];
[document setValue:[NSNumber numberWithInteger:encoding] forKey:@"encoding"];
[document setValue:[NSString localizedNameOfStringEncoding:encoding] forKey:@"encodingName"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"MGSFragariaTextEncodingChanged" object:[MGSFragaria currentInstance] userInfo:nil];
}
/*
- performUndoChangeEncoding:
*/
-(void)performUndoChangeEncoding:(id)sender
{
id document = SMLCurrentDocument;
[[[document valueForKey:ro_MGSFOSyntaxColouring] undoManager] registerUndoWithTarget:self selector:@selector(performUndoChangeEncoding:) object:[NSArray arrayWithObject:[document valueForKey:@"encoding"]]];
[[[document valueForKey:ro_MGSFOSyntaxColouring] undoManager] setActionName:NAME_FOR_UNDO_CHANGE_ENCODING];
[document setValue:[sender objectAtIndex:0] forKey:@"encoding"];
[document setValue:[NSString localizedNameOfStringEncoding:[[sender objectAtIndex:0] unsignedIntegerValue]] forKey:@"encodingName"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"MGSFragariaTextEncodingChanged" object:[MGSFragaria currentInstance] userInfo:nil];
}
#pragma mark -
#pragma mark Text shifting
/*
- shiftLeftAction:
*/
- (IBAction)shiftLeftAction:(id)sender
{
#pragma unused(sender)
NSTextView *textView = SMLCurrentTextView;
NSString *completeString = [textView string];
if ([completeString length] < 1) {
return;
}
[[SMLCurrentDocument valueForKey:ro_MGSFOSyntaxColouring] setReactToChanges:NO];
NSRange selectedRange;
NSArray *array = [SMLCurrentTextView selectedRanges];
NSInteger sumOfAllCharactersRemoved = 0;
NSInteger updatedLocation;
NSMutableArray *updatedSelectionsArray = [NSMutableArray array];
for (id item in array) {
selectedRange = NSMakeRange([item rangeValue].location - sumOfAllCharactersRemoved, [item rangeValue].length);
NSUInteger temporaryLocation = selectedRange.location;
NSUInteger maxSelectedRange = NSMaxRange(selectedRange);
NSInteger numberOfLines = 0;
NSInteger locationOfFirstLine = [completeString lineRangeForRange:NSMakeRange(temporaryLocation, 0)].location;
do {
temporaryLocation = NSMaxRange([completeString lineRangeForRange:NSMakeRange(temporaryLocation, 0)]);
numberOfLines++;
} while (temporaryLocation < maxSelectedRange);
temporaryLocation = selectedRange.location;
NSInteger idx;
NSInteger charactersRemoved = 0;
NSInteger charactersRemovedInSelection = 0;
NSRange rangeOfLine;
unichar characterToTest;
NSInteger numberOfSpacesPerTab = [[SMLDefaults valueForKey:MGSFragariaPrefsIndentWidth] integerValue];
NSInteger numberOfSpacesToDeleteOnFirstLine = -1;
for (idx = 0; idx < numberOfLines; idx++) {
rangeOfLine = [completeString lineRangeForRange:NSMakeRange(temporaryLocation, 0)];
if ([[SMLDefaults valueForKey:MGSFragariaPrefsUseTabStops] boolValue] == YES && [[SMLDefaults valueForKey:MGSFragariaPrefsIndentWithSpaces] boolValue] == YES) {
NSUInteger startOfLine = rangeOfLine.location;
while (startOfLine < NSMaxRange(rangeOfLine) && [completeString characterAtIndex:startOfLine] == ' ' && rangeOfLine.length > 0) {
startOfLine++;
}
NSInteger numberOfSpacesToDelete = numberOfSpacesPerTab;
if (numberOfSpacesPerTab != 0) {
numberOfSpacesToDelete = (startOfLine - rangeOfLine.location) % numberOfSpacesPerTab;
if (numberOfSpacesToDelete == 0) {
numberOfSpacesToDelete = numberOfSpacesPerTab;
}
}
if (numberOfSpacesToDeleteOnFirstLine != -1) {
numberOfSpacesToDeleteOnFirstLine = numberOfSpacesToDelete;
}
while (numberOfSpacesToDelete--) {
characterToTest = [completeString characterAtIndex:rangeOfLine.location];
if (characterToTest == ' ' || characterToTest == '\t') {
if ([textView shouldChangeTextInRange:NSMakeRange(rangeOfLine.location, 1) replacementString:@""]) { // Do it this way to mark it as an Undo
[textView replaceCharactersInRange:NSMakeRange(rangeOfLine.location, 1) withString:@""];
[textView didChangeText];
}
charactersRemoved++;
if (rangeOfLine.location >= selectedRange.location && rangeOfLine.location < maxSelectedRange) {
charactersRemovedInSelection++;
}
if (characterToTest == '\t') {
break;
}
}
}
} else {
characterToTest = [completeString characterAtIndex:rangeOfLine.location];
if ((characterToTest == ' ' || characterToTest == '\t') && rangeOfLine.length > 0) {
if ([textView shouldChangeTextInRange:NSMakeRange(rangeOfLine.location, 1) replacementString:@""]) { // Do it this way to mark it as an Undo
[textView replaceCharactersInRange:NSMakeRange(rangeOfLine.location, 1) withString:@""];
[textView didChangeText];
}
charactersRemoved++;
if (rangeOfLine.location >= selectedRange.location && rangeOfLine.location < maxSelectedRange) {
charactersRemovedInSelection++;
}
}
}
if (temporaryLocation < [[textView string] length]) {
temporaryLocation = NSMaxRange([completeString lineRangeForRange:NSMakeRange(temporaryLocation, 0)]);
}
}
if (selectedRange.length > 0) {
NSInteger selectedRangeLocation = selectedRange.location; // Make the location into an NSInteger because otherwise the value gets all screwed up when subtracting from it
NSInteger charactersToCountBackwards = 1;
if (numberOfSpacesToDeleteOnFirstLine != -1) {
charactersToCountBackwards = numberOfSpacesToDeleteOnFirstLine;
}
if (selectedRangeLocation - charactersToCountBackwards <= locationOfFirstLine) {
updatedLocation = locationOfFirstLine;
} else {
updatedLocation = selectedRangeLocation - charactersToCountBackwards;
}
[updatedSelectionsArray addObject:[NSValue valueWithRange:NSMakeRange(updatedLocation, selectedRange.length - charactersRemovedInSelection)]];
}
sumOfAllCharactersRemoved = sumOfAllCharactersRemoved + charactersRemoved;
}
[[SMLCurrentDocument valueForKey:ro_MGSFOSyntaxColouring] setReactToChanges:YES];
[[SMLCurrentDocument valueForKey:ro_MGSFOSyntaxColouring] pageRecolour];
if (sumOfAllCharactersRemoved == 0) {
NSBeep();
} else {
if ([[SMLCurrentDocument valueForKey:MGSFOIsEdited] boolValue] == NO) {
[self setEdited: YES];
}
[[SMLCurrentDocument valueForKey:ro_MGSFOLineNumbers] updateLineNumbersCheckWidth:NO recolour:NO];
}
if ([updatedSelectionsArray count] > 0) {
[textView setSelectedRanges:updatedSelectionsArray];
}
}
/*
- shiftRightAction:
*/
- (IBAction)shiftRightAction:(id)sender
{
#pragma unused(sender)
NSTextView *textView = SMLCurrentTextView;
NSString *completeString = [textView string];
if ([completeString length] < 1) {
return;
}
[[SMLCurrentDocument valueForKey:ro_MGSFOSyntaxColouring] setReactToChanges:NO];
NSRange selectedRange;
NSMutableString *replacementString;
if ([[SMLDefaults valueForKey:MGSFragariaPrefsIndentWithSpaces] boolValue] == YES) {
replacementString = [NSMutableString string];
NSInteger numberOfSpacesPerTab = [[SMLDefaults valueForKey:MGSFragariaPrefsIndentWidth] integerValue];
if ([[SMLDefaults valueForKey:MGSFragariaPrefsUseTabStops] boolValue] == YES) {
NSInteger locationOnLine = [textView selectedRange].location - [[textView string] lineRangeForRange:NSMakeRange([textView selectedRange].location, 0)].location;
if (numberOfSpacesPerTab != 0) {
NSInteger numberOfSpacesLess = locationOnLine % numberOfSpacesPerTab;
numberOfSpacesPerTab = numberOfSpacesPerTab - numberOfSpacesLess;
}
}
while (numberOfSpacesPerTab--) {
[replacementString appendString:@" "];
}
} else {
replacementString = [NSMutableString stringWithString:@"\t"];
}
NSInteger replacementStringLength = [replacementString length];
NSArray *array = [SMLCurrentTextView selectedRanges];
NSInteger sumOfAllCharactersInserted = 0;
NSInteger updatedLocation;
NSMutableArray *updatedSelectionsArray = [NSMutableArray array];
for (id item in array) {
selectedRange = NSMakeRange([item rangeValue].location + sumOfAllCharactersInserted, [item rangeValue].length);
NSUInteger temporaryLocation = selectedRange.location;
NSUInteger maxSelectedRange = NSMaxRange(selectedRange);
NSInteger numberOfLines = 0;
NSInteger locationOfFirstLine = [completeString lineRangeForRange:NSMakeRange(temporaryLocation, 0)].location;
do {
temporaryLocation = NSMaxRange([completeString lineRangeForRange:NSMakeRange(temporaryLocation, 0)]);
numberOfLines++;
} while (temporaryLocation < maxSelectedRange);
temporaryLocation = selectedRange.location;
NSInteger idx;
NSUInteger charactersInserted = 0;
NSInteger charactersInsertedInSelection = 0;
NSRange rangeOfLine;
for (idx = 0; idx < numberOfLines; idx++) {
rangeOfLine = [completeString lineRangeForRange:NSMakeRange(temporaryLocation, 0)];
if ([textView shouldChangeTextInRange:NSMakeRange(rangeOfLine.location, 0) replacementString:replacementString]) { // Do it this way to mark it as an Undo
[textView replaceCharactersInRange:NSMakeRange(rangeOfLine.location, 0) withString:replacementString];
[textView didChangeText];
}
charactersInserted = charactersInserted + replacementStringLength;
if (rangeOfLine.location >= selectedRange.location && rangeOfLine.location < maxSelectedRange + charactersInserted) {
charactersInsertedInSelection = charactersInsertedInSelection + replacementStringLength;
}
if (temporaryLocation < [[textView string] length]) {
temporaryLocation = NSMaxRange([completeString lineRangeForRange:NSMakeRange(temporaryLocation, 0)]);
}
}
if (selectedRange.length > 0) {
if (selectedRange.location + replacementStringLength >= [[textView string] length]) {
updatedLocation = locationOfFirstLine;
} else {
updatedLocation = selectedRange.location;
}
[updatedSelectionsArray addObject:[NSValue valueWithRange:NSMakeRange(updatedLocation, selectedRange.length + charactersInsertedInSelection)]];
}
sumOfAllCharactersInserted = sumOfAllCharactersInserted + charactersInserted;
}
[[SMLCurrentDocument valueForKey:ro_MGSFOSyntaxColouring] setReactToChanges:YES];
[[SMLCurrentDocument valueForKey:ro_MGSFOSyntaxColouring] pageRecolour];
[self setEdited:YES];
[[SMLCurrentDocument valueForKey:ro_MGSFOLineNumbers] updateLineNumbersCheckWidth:NO recolour:NO];
if ([updatedSelectionsArray count] > 0) {
[textView setSelectedRanges:updatedSelectionsArray];
}
}
#pragma mark -
#pragma mark Text manipulation
/*
- interchangeAdjacentCharactersAction:
*/
- (IBAction)interchangeAdjacentCharactersAction:(id)sender
{
#pragma unused(sender)
NSTextView *textView = SMLCurrentTextView;
[textView transpose:nil];
}
/*
- removeNeedlessWhitespaceAction:
*/
- (IBAction)removeNeedlessWhitespaceAction:(id)sender
{
#pragma unused(sender)
// First count the number of lines in which to perform the action, as the original range changes when you insert characters, and then perform the action line after line, by removing tabs and spaces after the last non-whitespace characters in every line
NSTextView *textView = SMLCurrentTextView;
NSString *completeString = [textView string];
if ([completeString length] < 1) {
return;
}
[[SMLCurrentDocument valueForKey:ro_MGSFOSyntaxColouring] setReactToChanges:NO];
NSRange selectedRange;
NSArray *array = [SMLCurrentTextView selectedRanges];
NSInteger sumOfAllCharactersRemoved = 0;
NSInteger updatedLocation;
NSMutableArray *updatedSelectionsArray = [NSMutableArray array];
for (id item in array) {
selectedRange = NSMakeRange([item rangeValue].location - sumOfAllCharactersRemoved, [item rangeValue].length);
NSUInteger tempLocation = selectedRange.location;
NSUInteger maxSelectedRange = NSMaxRange(selectedRange);
NSInteger numberOfLines = 0;
NSInteger locationOfFirstLine = [completeString lineRangeForRange:NSMakeRange(tempLocation, 0)].location;
do {
tempLocation = NSMaxRange([completeString lineRangeForRange:NSMakeRange(tempLocation, 0)]);
numberOfLines++;
} while (tempLocation < maxSelectedRange);
tempLocation = selectedRange.location;
NSInteger idx;
NSInteger charactersRemoved = 0;
NSInteger charactersRemovedInSelection = 0;
NSRange rangeOfLine;
NSUInteger endOfContentsLocation;
for (idx = 0; idx < numberOfLines; idx++) {
rangeOfLine = [completeString lineRangeForRange:NSMakeRange(tempLocation, 0)];
[completeString getLineStart:NULL end:NULL contentsEnd:&endOfContentsLocation forRange:rangeOfLine];
while (endOfContentsLocation != 0 && ([completeString characterAtIndex:endOfContentsLocation - 1] == ' ' || [completeString characterAtIndex:endOfContentsLocation - 1] == '\t')) {
if ([textView shouldChangeTextInRange:NSMakeRange(endOfContentsLocation - 1, 1) replacementString:@""]) { // Do it this way to mark it as an Undo
[textView replaceCharactersInRange:NSMakeRange(endOfContentsLocation - 1, 1) withString:@""];
[textView didChangeText];
}
endOfContentsLocation--;
charactersRemoved++;
if (rangeOfLine.location >= selectedRange.location && rangeOfLine.location < maxSelectedRange) {
charactersRemovedInSelection++;
}
}
tempLocation = NSMaxRange([completeString lineRangeForRange:NSMakeRange(tempLocation, 0)]);
}
if (selectedRange.length > 0) {
NSInteger selectedRangeLocation = selectedRange.location; // Make the location into an NSInteger because otherwise the value gets all screwed up when subtracting from it
if (selectedRangeLocation - 1 <= locationOfFirstLine) {
updatedLocation = locationOfFirstLine;
} else {
updatedLocation = selectedRangeLocation - 1;
}
[updatedSelectionsArray addObject:[NSValue valueWithRange:NSMakeRange(updatedLocation, selectedRange.length - charactersRemoved)]];
}
sumOfAllCharactersRemoved = sumOfAllCharactersRemoved + charactersRemoved;
}
[[SMLCurrentDocument valueForKey:ro_MGSFOSyntaxColouring] setReactToChanges:YES];
[[SMLCurrentDocument valueForKey:ro_MGSFOSyntaxColouring] pageRecolour];
if (sumOfAllCharactersRemoved == 0) {
NSBeep();
} else {
[self setEdited:YES];
[[SMLCurrentDocument valueForKey:ro_MGSFOLineNumbers] updateLineNumbersCheckWidth:NO recolour:NO];
}
if ([updatedSelectionsArray count] > 0) {
[textView setSelectedRanges:updatedSelectionsArray];
}
}
/*
- toLowercaseAction:
*/
- (IBAction)toLowercaseAction:(id)sender
{
#pragma unused(sender)
NSTextView *textView = SMLCurrentTextView;
NSArray *array = [textView selectedRanges];
for (id item in array) {
NSRange selectedRange = [item rangeValue];
NSString *originalString = [SMLCurrentText substringWithRange:selectedRange];
NSString *newString = [NSString stringWithString:[originalString lowercaseString]];
[textView setSelectedRange:selectedRange];
[textView insertText:newString];
}
}
/*
- toUppercaseAction:
*/
- (IBAction)toUppercaseAction:(id)sender
{
#pragma unused(sender)
NSTextView *textView = SMLCurrentTextView;
NSArray *array = [textView selectedRanges];
for (id item in array) {
NSRange selectedRange = [item rangeValue];
NSString *originalString = [SMLCurrentText substringWithRange:selectedRange];
NSString *newString = [NSString stringWithString:[originalString uppercaseString]];
[textView setSelectedRange:selectedRange];
[textView insertText:newString];
}
}
/*
- capitaliseAction:
*/
- (IBAction)capitaliseAction:(id)sender
{
#pragma unused(sender)
NSTextView *textView = SMLCurrentTextView;
NSArray *array = [textView selectedRanges];
for (id item in array) {
NSRange selectedRange = [item rangeValue];
NSString *originalString = [SMLCurrentText substringWithRange:selectedRange];
NSString *newString = [NSString stringWithString:[originalString capitalizedString]];
[textView setSelectedRange:selectedRange];
[textView insertText:newString];
}
}
/*
- entabAction:
*/
- (IBAction)entabAction:(id)sender
{
#pragma unused(sender)
[SMLCurrentExtraInterfaceController displayEntab];
}
/*
- detabAction
*/
- (IBAction)detabAction:(id)sender
{
#pragma unused(sender)
[SMLCurrentExtraInterfaceController displayDetab];
}
/*
- performEntab
*/
- (void)performEntab
{
NSTextView *textView = SMLCurrentTextView;
[[SMLCurrentDocument valueForKey:ro_MGSFOSyntaxColouring] setReactToChanges:NO];
NSRange selectedRange;
NSRange savedRange = [textView selectedRange];
NSArray *array = [SMLCurrentTextView selectedRanges];
NSMutableString *searchString = [NSMutableString string];
NSInteger numberOfSpaces = [[SMLDefaults valueForKey:MGSFragariaPrefsSpacesPerTabEntabDetab] integerValue];
while (numberOfSpaces--) {
[searchString appendString:@" "];
}
NSMutableString *completeString = [NSMutableString stringWithString:[textView string]];
NSInteger sumOfRemovedCharacters = 0;
for (id item in array) {
selectedRange = NSMakeRange([item rangeValue].location - sumOfRemovedCharacters, [item rangeValue].length);
sumOfRemovedCharacters = sumOfRemovedCharacters + ([completeString replaceOccurrencesOfString:searchString withString:@"\t" options:NSLiteralSearch range:selectedRange] * ([searchString length] - 1));
if ([textView shouldChangeTextInRange:NSMakeRange(0, [[textView string] length]) replacementString:completeString]) { // Do it this way to mark it as an Undo
[textView replaceCharactersInRange:NSMakeRange(0, [[textView string] length]) withString:completeString];
[textView didChangeText];
}
}
[[SMLCurrentDocument valueForKey:ro_MGSFOSyntaxColouring] setReactToChanges:YES];
[self setEdited:YES];
[[SMLCurrentDocument valueForKey:ro_MGSFOSyntaxColouring] pageRecolour];
[textView setSelectedRange:NSMakeRange(savedRange.location, 0)];
}
/*
- performDetab
*/
- (void)performDetab
{
NSTextView *textView = SMLCurrentTextView;
[[SMLCurrentDocument valueForKey:ro_MGSFOSyntaxColouring] setReactToChanges:NO];
NSRange selectedRange;
NSRange savedRange = [textView selectedRange];
NSArray *array = [SMLCurrentTextView selectedRanges];
NSMutableString *replacementString = [NSMutableString string];
NSInteger numberOfSpaces = [[SMLDefaults valueForKey:MGSFragariaPrefsSpacesPerTabEntabDetab] integerValue];
while (numberOfSpaces--) {
[replacementString appendString:@" "];
}
NSMutableString *completeString = [NSMutableString stringWithString:[textView string]];
NSInteger sumOfInsertedCharacters = 0;
for (id item in array) {
selectedRange = NSMakeRange([item rangeValue].location + sumOfInsertedCharacters, [item rangeValue].length);
sumOfInsertedCharacters = sumOfInsertedCharacters + ([completeString replaceOccurrencesOfString:@"\t" withString:replacementString options:NSLiteralSearch range:selectedRange] * ([replacementString length] - 1));
if ([textView shouldChangeTextInRange:NSMakeRange(0, [[textView string] length]) replacementString:completeString]) { // Do it this way to mark it as an Undo
[textView replaceCharactersInRange:NSMakeRange(0, [[textView string] length]) withString:completeString];
[textView didChangeText];
}
}
[[SMLCurrentDocument valueForKey:ro_MGSFOSyntaxColouring] setReactToChanges:YES];
[self setEdited:YES];
[[SMLCurrentDocument valueForKey:ro_MGSFOSyntaxColouring] pageRecolour];
[textView setSelectedRange:NSMakeRange(savedRange.location, 0)];
}
/*
- reloadText:
*/
- (void)reloadText:(id)sender
{
#pragma unused(sender)
id document = SMLCurrentDocument;
[document setValue:[NSNumber numberWithUnsignedInteger:[sender tag]] forKey:@"encoding"];
[document setValue:[NSString localizedNameOfStringEncoding:[sender tag]] forKey:@"encodingName"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"MGSFragariaReloadText" object:[MGSFragaria currentInstance] userInfo:nil];
}
#pragma mark -
#pragma mark Goto
/*
- goToLineAction:
*/
- (IBAction)goToLineAction:(id)sender
{
#pragma unused(sender)
[SMLCurrentExtraInterfaceController displayGoToLine];
}
/*
- performGoToLine:
*/
- (void)performGoToLine:(NSInteger)lineToGoTo
{
NSInteger lineNumber;
NSInteger idx;
NSString *completeString = SMLCurrentText;
NSInteger completeStringLength = [completeString length];
NSInteger numberOfLinesInDocument;
for (idx = 0, numberOfLinesInDocument = 1; idx < completeStringLength; numberOfLinesInDocument++) {
idx = NSMaxRange([completeString lineRangeForRange:NSMakeRange(idx, 0)]);
}
if (lineToGoTo > numberOfLinesInDocument) {
NSBeep();
return;
}
for (idx = 0, lineNumber = 1; lineNumber < lineToGoTo; lineNumber++) {
idx = NSMaxRange([completeString lineRangeForRange:NSMakeRange(idx, 0)]);
}
[SMLCurrentTextView setSelectedRange:[completeString lineRangeForRange:NSMakeRange(idx, 0)]];
[SMLCurrentTextView scrollRangeToVisible:[completeString lineRangeForRange:NSMakeRange(idx, 0)]];
}
#pragma mark -
#pragma mark Tag manipulation
/*
- closeTagAction:
*/
- (IBAction)closeTagAction:(id)sender
{
#pragma unused(sender)
NSTextView *textView = SMLCurrentTextView;
NSRange selectedRange = [textView selectedRange];
if (selectedRange.length > 0) {
NSBeep();
return;
}
NSUInteger location = selectedRange.location;
NSString *completeString = [textView string];
BOOL foundClosingBrace = NO;
BOOL foundOpeningBrace = NO;
while (location--) { // First check that there is a closing c i.e. >
if ([completeString characterAtIndex:location] == '>') {
foundClosingBrace = YES;
break;
}
}
if (!foundClosingBrace) {
NSBeep();
return;
}
NSInteger locationOfClosingBrace = location;
NSInteger numberOfClosingTags = 0;
while (location--) { // Then check for the opening brace i.e. <
if ([completeString characterAtIndex:location] == '<') {
// Divide into four checks as otherwise it will miss to skip the tag if it comes absolutely last in the document
if (location + 4 <= [completeString length]) { // Check that the tag is not one of the tags that aren't closed e.g. <br> or any of their variants
NSString *checkString = [completeString substringWithRange:NSMakeRange(location, 4)];
NSRange searchRange = NSMakeRange(0, [checkString length]);
if ([checkString rangeOfString:@"<br>" options:NSCaseInsensitiveSearch range:searchRange].location != NSNotFound) {
continue;
} else if ([checkString rangeOfString:@"<hr>" options:NSCaseInsensitiveSearch range:searchRange].location != NSNotFound) {
continue;
} else if ([checkString rangeOfString:@"<!--" options:NSCaseInsensitiveSearch range:searchRange].location != NSNotFound) {
continue;
} else if ([checkString rangeOfString:@"<?" options:NSCaseInsensitiveSearch range:searchRange].location != NSNotFound) {
continue;
} else if ([checkString rangeOfString:@"<%" options:NSCaseInsensitiveSearch range:searchRange].location != NSNotFound) {
continue;
}
}
if (location + 5 <= [completeString length]) { // Check that the tag is not one of the tags that aren't closed e.g. <br> or any of their variants
NSString *checkString = [completeString substringWithRange:NSMakeRange(location, 5)];
NSRange searchRange = NSMakeRange(0, [checkString length]);
if ([checkString rangeOfString:@"<img " options:NSCaseInsensitiveSearch range:searchRange].location != NSNotFound) {
continue;
} else if ([checkString rangeOfString:@"<br/>" options:NSCaseInsensitiveSearch range:searchRange].location != NSNotFound) {
continue;
}
}
if (location + 6 <= [completeString length]) { // Check that the tag is not one of the tags that aren't closed e.g. <br> or any of their variants
NSString *checkString = [completeString substringWithRange:NSMakeRange(location, 6)];
NSRange searchRange = NSMakeRange(0, [checkString length]);
if ([checkString rangeOfString:@"<br />" options:NSCaseInsensitiveSearch range:searchRange].location != NSNotFound) {
continue;
} else if ([checkString rangeOfString:@"<hr />" options:NSCaseInsensitiveSearch range:searchRange].location != NSNotFound) {
continue;
} else if ([checkString rangeOfString:@"<area " options:NSCaseInsensitiveSearch range:searchRange].location != NSNotFound) {
continue;
} else if ([checkString rangeOfString:@"<base " options:NSCaseInsensitiveSearch range:searchRange].location != NSNotFound) {
continue;
} else if ([checkString rangeOfString:@"<link " options:NSCaseInsensitiveSearch range:searchRange].location != NSNotFound) {
continue;
} else if ([checkString rangeOfString:@"<meta " options:NSCaseInsensitiveSearch range:searchRange].location != NSNotFound) {
continue;
}
}
if (location + 7 < [completeString length]) { // check that the tag is not one of the tags that aren't closed e.g. <br> and their variants
NSString *checkString = [completeString substringWithRange:NSMakeRange(location, 7)];
NSRange searchRange = NSMakeRange(0, [checkString length]);
if ([checkString rangeOfString:@"<frame " options:NSCaseInsensitiveSearch range:searchRange].location != NSNotFound) {
continue;
} else if ([checkString rangeOfString:@"<input " options:NSCaseInsensitiveSearch range:searchRange].location != NSNotFound) {
continue;
} else if ([checkString rangeOfString:@"<param " options:NSCaseInsensitiveSearch range:searchRange].location != NSNotFound) {
continue;
}
}
NSScanner *selfClosingScanner = [NSScanner scannerWithString:[completeString substringWithRange:NSMakeRange(location, locationOfClosingBrace - location)]];
[selfClosingScanner setCharactersToBeSkipped:nil];
NSString *selfClosingScanString = [NSString string];
[selfClosingScanner scanUpToCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:@">"] intoString:&selfClosingScanString];
if ([selfClosingScanString length] != 0) {
if ([selfClosingScanString characterAtIndex:([selfClosingScanString length] - 1)] == '/') {
continue;
}
}
if ([completeString characterAtIndex:location + 1] == '/') { // If it's a closing tag (e.g. </a>) continue the search
numberOfClosingTags++;
continue;
} else {
if (numberOfClosingTags) { // Try to find the "correct" tag to close by counting the number of closing tags and when they match up insert the created closing tag; if they don't write balanced code - well, tough luck...
numberOfClosingTags--;
} else {
foundOpeningBrace = YES;
break;
}
}
}
}
if (foundOpeningBrace == NO) {
NSBeep();
return;
}
NSScanner *scanner = [NSScanner scannerWithString:[completeString substringWithRange:NSMakeRange(location, locationOfClosingBrace - location)]];
[scanner setCharactersToBeSkipped:nil];
NSString *scanString = [NSString string];
[scanner scanUpToCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:@" >/"] intoString:&scanString]; // Set the string to everything up to any of the characters (space),> or / so that it will catch things like <a href... as well as <br>
NSMutableString *tagString = [NSMutableString stringWithString:scanString];
NSInteger tagStringLength = [tagString length];
if (tagStringLength == 0) {
NSBeep();
return;
}
[tagString insertString:@"/" atIndex:1];
[tagString insertString:@">" atIndex:tagStringLength + 1];
if ([textView shouldChangeTextInRange:selectedRange replacementString:tagString]) { // Do it this way to mark it as an Undo
[textView replaceCharactersInRange:selectedRange withString:tagString];
[textView didChangeText];
}
}
/*
- prepareForXMLAction:
*/
- (IBAction)prepareForXMLAction:(id)sender
{
#pragma unused(sender)
NSTextView *textView = SMLCurrentTextView;
NSRange selectedRange = [textView selectedRange];
NSMutableString *stringToConvert = [NSMutableString stringWithString:[[textView string] substringWithRange:selectedRange]];
[stringToConvert replaceOccurrencesOfString:@"&" withString:@"&" options:NSLiteralSearch range:NSMakeRange(0, [stringToConvert length])];
[stringToConvert replaceOccurrencesOfString:@"&" withString:@"&" options:NSLiteralSearch range:NSMakeRange(0, [stringToConvert length])];
[stringToConvert replaceOccurrencesOfString:@"<" withString:@"<" options:NSLiteralSearch range:NSMakeRange(0, [stringToConvert length])];
[stringToConvert replaceOccurrencesOfString:@">" withString:@">" options:NSLiteralSearch range:NSMakeRange(0, [stringToConvert length])];
if ([textView shouldChangeTextInRange:selectedRange replacementString:stringToConvert]) { // Do it this way to mark it as an Undo
[textView replaceCharactersInRange:selectedRange withString:stringToConvert];
[textView didChangeText];
}
}
#pragma mark -
#pragma mark Breakpoints
- (IBAction)toggleBreakpointAction:(id)sender
{
#pragma unused(sender)