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
//
// CLIPSTextDocument.m
// CLIPSEditor
//
// Created by Gary Riley on 2/14/06.
//
#import "CLIPSTextDocument.h"
#import "CLIPSTerminalController.h"
#import "AppController.h"
#import "CLIPSEnvironment.h"
#import "CLIPSTextView.h"
#import <CLIPS/clips.h>
@implementation CLIPSTextDocument
/*********/
/* init: */
/*********/
- (id) init
{
self = [super init];
if (self)
{
hiliteColor = [[NSDictionary alloc] initWithObjectsAndKeys: [NSColor selectedTextBackgroundColor], NSBackgroundColorAttributeName, nil];
}
return self;
}
/************/
/* dealloc: */
/************/
- (void) dealloc
{
}
/*****************/
/* awakeFromNib: */
/*****************/
- (void) awakeFromNib
{
/*=====================================================================*/
/* The action "cog" menu is implemented by using a button with the cog */
/* icon (which can't display a menu) that sends an action message to a */
/* hidden NSPopButton that displays the menu. Normally a button takes */
/* effect when the mouse button is released, but in this case we want */
/* the menu to display immediately when the mouse button goes down. */
/*=====================================================================*/
[popupActivator sendActionOn: NSLeftMouseDownMask];
/*======================================================*/
/* Places a few pixels of white space between the edges */
/* of the window and the rectangle in which the text is */
/* displayed in the terminal window. */
/*======================================================*/
NSSize theSize = { 5, 5 };
[textView setTextContainerInset: theSize];
/*===========================================*/
/* Disable automatic text substitution (such */
/* as replacing quotes with curly quotes). */
/*===========================================*/
[textView setEnabledTextCheckingTypes: 0];
/*==================================*/
/* Set up the horizontal scrollbar. */
/*==================================*/
NSScrollView *textScrollView = [textView enclosingScrollView];
[textScrollView setHasHorizontalScroller: YES];
[textScrollView setAutoresizingMask: (NSViewWidthSizable | NSViewHeightSizable)];
[textView setMaxSize: NSMakeSize(FLT_MAX, FLT_MAX)];
[textView setHorizontallyResizable: YES];
[textView setAutoresizingMask: (NSViewWidthSizable | NSViewHeightSizable)];
[[textView textContainer] setContainerSize: NSMakeSize(FLT_MAX, FLT_MAX)];
[[textView textContainer] setWidthTracksTextView: NO];
}
/*************************************************************/
/* windowDidBecomeMain: When the window is the main window, */
/* the action 'cog' menu needs its normal unpressed image. */
/**************************************************************/
- (void) windowDidBecomeMain: (NSNotification *) aNotification
{
[popupActivator setImage:[NSImage imageNamed:@"CogUp.tiff"]];
}
/**************************************************************/
/* windowDidResignMain: When the window is no longer the main */
/* window, the action 'cog' menu needs to be greyed. */
/**************************************************************/
- (void) windowDidResignMain: (NSNotification *) aNotification
{
[popupActivator setImage:[NSImage imageNamed:@"CogGreyed.tiff"]];
}
/**************************/
/* resetBackgroundColour: */
/**************************/
- (void) resetBackgroundColour: (id) sender
{
[[textView layoutManager] removeTemporaryAttribute: NSBackgroundColorAttributeName forCharacterRange: NSRangeFromString(sender)];
}
/*******************************/
/* textViewDidChangeSelection: */
/*******************************/
- (void) textViewDidChangeSelection: (NSNotification *) aNotification
{
NSRange selectionRange;
NSUInteger cursorLocation;
unichar characterToCheck;
unsigned short nestingDepth;
NSString *theText = [textView string];
NSUserDefaults *theValues;
/*==============================================================*/
/* Check the defaults to see if parentheses should be balanced. */
/*==============================================================*/
theValues = [[NSUserDefaultsController sharedUserDefaultsController] values];
if (! [[theValues valueForKey: @"editorBalanceParens"] boolValue])
{ return; }
/*=========================================================*/
/* Don't balance parentheses in response to a mouse click. */
/*=========================================================*/
if ([textView mouseDownDetected] == YES)
{
[textView setMouseDownDetected: NO];
return;
}
/*================================================*/
/* Don't balance parentheses if there is no text. */
/*================================================*/
if ([theText length] == 0)
{ return; }
/*=================================*/
/* Retrieve the current selection. */
/*=================================*/
selectionRange = [textView selectedRange];
/*======================*/
/* Where is the cursor? */
/*======================*/
cursorLocation = selectionRange.location;
if (cursorLocation == 0) return;
cursorLocation--;
/*===============================================*/
/* What is the character at the cursor location? */
/*===============================================*/
characterToCheck = [theText characterAtIndex: cursorLocation];
/*======================================*/
/* We only balance a right parenthesis. */
/*======================================*/
if (characterToCheck != ')') return;
/*======================================================================*/
/* The nesting depth will start at zero. Each time a ')' is encountered */
/* the nesting depth is incremented by one and each time a '(' is */
/* encountered the nesting depth is decremented by one. If a '(' is */
/* encountered when the nesting depth is zero (the starting value), the */
/* matching parenthesis has been found. */
/*======================================================================*/
nestingDepth = 0;
/*==================================================*/
/* Start looking for the matching left parenthesis. */
/*==================================================*/
while (cursorLocation--)
{
characterToCheck = [theText characterAtIndex: cursorLocation];
if (characterToCheck == '(')
{
if (nestingDepth == 0)
{
[[textView layoutManager] addTemporaryAttributes: hiliteColor forCharacterRange: NSMakeRange(cursorLocation, 1)];
[self performSelector: @selector(resetBackgroundColour:) withObject: NSStringFromRange(NSMakeRange(cursorLocation, 1)) afterDelay: 0.12];
return;
}
else
{ nestingDepth--; }
}
else if (characterToCheck == ')')
{ nestingDepth++; }
}
/*================================================*/
/* Beep to indicate a matching ')' was not found. */
/*================================================*/
NSBeep();
}
/***********/
/* string: */
/***********/
- (NSString *) string
{
return string;
}
/**************/
/* setString: */
/**************/
- (void) setString: (NSString *) value
{
string = value;
}
/*****************/
/* updateString: */
/*****************/
- (void) updateString
{
[self setString: [textView string]];
}
/***************/
/* updateView: */
/***************/
- (void) updateView
{
[textView setString: [self string]];
}
/******************/
/* windowNibName: */
/******************/
- (NSString *) windowNibName
{
return @"CLIPSTextDocument";
}
/*******************************/
/* windowControllerDidLoadNib: */
/*******************************/
- (void) windowControllerDidLoadNib: (NSWindowController *) aController
{
[super windowControllerDidLoadNib: aController];
if (! string)
{ [self setString:@""]; }
[self updateView];
}
/*********************/
/* dataOfType:error: */
/*********************/
- (NSData *) dataOfType: (NSString *) typeName
error: (NSError * _Nullable *) outError
{
[self updateString];
return [string dataUsingEncoding: NSUTF8StringEncoding allowLossyConversion:YES];
}
/******************************/
/* readFromData:ofType:error: */
/******************************/
- (BOOL) readFromData: (NSData *) data
ofType: (NSString *) typeName
error: (NSError * _Nullable *) outError;
{
NSString *aString;
aString = [[NSString alloc] initWithData:data
encoding: NSUTF8StringEncoding];
if (aString == nil)
{ return NO; }
[self setString: aString];
[self updateView];
return YES;
}
/*********************/
/* validateMenuItem: */
/*********************/
- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
{
/*=========================================================*/
/* The "Load Selection" and "Batch Selection" commands are */
/* only available if there is a selection in the window. */
/*=========================================================*/
if (([menuItem action] == @selector(loadSelection:)) ||
([menuItem action] == @selector(batchSelection:)))
{
NSRange selectedRange = [textView selectedRange];
if (selectedRange.length < 1) return NO;
return YES;
}
else if ([menuItem action] == @selector(loadBuffer:))
{ return YES; }
return [super validateMenuItem: menuItem];
}
/*%%%%%%%%%%%%%%%%*/
/* Action Methods */
/*%%%%%%%%%%%%%%%%*/
/*****************************************************/
/* loadSelection: Handles the Load Selection action. */
/*****************************************************/
- (IBAction) loadSelection: (id) sender
{
#if (! RUN_TIME) && (! BLOAD_ONLY)
AppController *theDelegate = [NSApp delegate];
char *theString, *convString;
NSUInteger length;
NSString *entireString = [textView string];
/*============================================*/
/* There must be a selection for this action. */
/*============================================*/
NSRange selectedRange = [textView selectedRange];
if (selectedRange.length < 1) return;
/*=============================*/
/* Retrieve the selected text. */
/*=============================*/
NSString *theSelection = [entireString substringWithRange: selectedRange];
/*==============================*/
/* Move the text to a C string. */
/*==============================*/
Environment *theEnvironment = [[theDelegate mainEnvironment] environment];
length = [theSelection lengthOfBytesUsingEncoding: NSUTF8StringEncoding];
theString = (char *) gm2(theEnvironment,length + 1);
convString = (char *) [theSelection UTF8String];
strcpy(theString,convString);
/*=================================================*/
/* Bring the attached CLIPS terminal to the front. */
/*=================================================*/
[[[theDelegate mainTerminal] window] makeKeyAndOrderFront: self];
/*=====================*/
/* Load the selection. */
/*=====================*/
WriteString(theEnvironment,STDOUT,"Loading Selection...\n");
FlushCommandString(theEnvironment);
OpenTextSource(theEnvironment,"aecompiletext",theString,0,(unsigned) length); // TBD Make the name unique
SetPrintWhileLoading(theEnvironment,true);
LoadConstructsFromLogicalName(theEnvironment,"aecompiletext");
SetPrintWhileLoading(theEnvironment,false);
CloseStringSource(theEnvironment,"aecompiletext");
rm(theEnvironment,theString,(unsigned int) length+1);
PrintPrompt(theEnvironment);
#endif
}
/*******************************************************/
/* batchSelection: Handles the Batch Selection action. */
/*******************************************************/
- (IBAction) batchSelection: (id) sender
{
AppController *theDelegate = [NSApp delegate];
char *theString, *convString;
NSUInteger length;
NSString *entireString = [textView string];
/*============================================*/
/* There must be a selection for this action. */
/*============================================*/
NSRange selectedRange = [textView selectedRange];
if (selectedRange.length < 1) return;
/*=============================*/
/* Retrieve the selected text. */
/*=============================*/
NSString *theSelection = [entireString substringWithRange: selectedRange];
/*==============================*/
/* Move the text to a C string. */
/*==============================*/
Environment *theEnvironment = [[theDelegate mainEnvironment] environment];
length = [theSelection lengthOfBytesUsingEncoding: NSUTF8StringEncoding];
theString = (char *) gm2(theEnvironment,length + 1);
convString = (char *) [theSelection UTF8String];
strcpy(theString,convString);
/*=================================================*/
/* Bring the attached CLIPS terminal to the front. */
/*=================================================*/
[[[theDelegate mainTerminal] window] makeKeyAndOrderFront: self];
/*==========================*/
/* Batch the selected text. */
/*==========================*/
OpenStringBatch(theEnvironment,"batchtext",theString,false); // TBD generate unique name
}
/************************************************/
/* loadBuffer: Handles the Load Buffer command. */
/************************************************/
- (IBAction) loadBuffer: (id) sender
{
#if (! RUN_TIME) && (! BLOAD_ONLY)
AppController *theDelegate = [NSApp delegate];
char *theString, *convString;
NSUInteger length;
NSString *entireString = [textView string];
/*==============================*/
/* Move the text to a C string. */
/*==============================*/
Environment *theEnvironment = [[theDelegate mainEnvironment] environment];
length = [entireString lengthOfBytesUsingEncoding: NSUTF8StringEncoding];
theString = (char *) gm2(theEnvironment,length + 1);
convString = (char *) [entireString UTF8String];
strcpy(theString,convString);
/*=================================================*/
/* Bring the attached CLIPS terminal to the front. */
/*=================================================*/
[[[theDelegate mainTerminal] window] makeKeyAndOrderFront: self];
/*==================*/
/* Load the buffer. */
/*==================*/
WriteString(theEnvironment,STDOUT,"Loading Buffer...\n");
FlushCommandString(theEnvironment);
OpenTextSource(theEnvironment,"aecompiletext",theString,0,(unsigned) length); // TBD Make the name unique
SetPrintWhileLoading(theEnvironment,true);
LoadConstructsFromLogicalName(theEnvironment,"aecompiletext");
SetPrintWhileLoading(theEnvironment,false);
CloseStringSource(theEnvironment,"aecompiletext");
rm(theEnvironment,theString,(unsigned int) length+1);
PrintPrompt(theEnvironment);
#endif
}
/*****************************************/
/* balance: Handles the Balance command. */
/*****************************************/
- (IBAction) balance: (id) sender
{
NSRange selectionRange;
NSUInteger leftMiddle, rightMiddle, textLength;
unichar characterToCheck;
int count, leftCount, rightCount;
NSUInteger i;
BOOL endReached;
NSString *theText = [textView string];
/*====================================*/
/* Can't balance if there is no text. */
/*====================================*/
if ([theText length] == 0)
{
NSBeep();
return;
}
/*=================================*/
/* Retrieve the current selection. */
/*=================================*/
selectionRange = [textView selectedRange];
/*=============================================================*/
/* Get information about the current selection to be balanced. */
/*=============================================================*/
leftMiddle = selectionRange.location;
rightMiddle = selectionRange.location + selectionRange.length;
textLength = [theText length]; /* TBD UTF-8 */
/*===================================*/
/* If the selection is empty then... */
/*===================================*/
if (leftMiddle == rightMiddle)
{
/*============================================*/
/* If '(' is to the right of the cursor, then */
/* all balancing should occur to the right. */
/*============================================*/
if ((leftMiddle < textLength) && ([theText characterAtIndex: leftMiddle] == '('))
{
[self balanceIt: theText
leftMiddle: leftMiddle
rightMiddle: leftMiddle + 1
leftCount: 1
rightCount: 0
textLength: textLength];
}
/*================================================*/
/* Else if ')' is to the left of the cursor, then */
/* all balancing should occur to the left. */
/*================================================*/
else if ((leftMiddle > 0) ? ([theText characterAtIndex: (leftMiddle - 1)] == ')') : 0)
{
if (leftMiddle < 2)
{
NSBeep();
return;
}
[self balanceIt: theText
leftMiddle: leftMiddle - 2
rightMiddle: leftMiddle
leftCount: 0
rightCount: -1
textLength: textLength];
}
/*====================================================*/
/* Else balancing occurs to left and right of cursor. */
/*====================================================*/
else
{
[self balanceIt: theText
leftMiddle: leftMiddle - 1
rightMiddle: rightMiddle
leftCount: 0
rightCount: 0
textLength: textLength];
}
}
/*===================================================*/
/* Otherwise the selection is non-empty therefore... */
/*===================================================*/
else
{
/*===============================================*/
/* Determine the number of right parentheses ')' */
/* that need to be balanced from the left side. */
/*===============================================*/
count = 0;
leftCount = 0;
for (i = leftMiddle ; i < rightMiddle ; i++)
{
characterToCheck = [theText characterAtIndex: i];
if (characterToCheck == '(') count++;
else if (characterToCheck == ')') count--;
if (count < leftCount) leftCount = count;
}
/*===============================================*/
/* Determine the number of left parentheses '(' */
/* that need to be balanced from the right side. */
/*===============================================*/
count = 0;
rightCount = 0;
for (endReached = NO, i = rightMiddle - 1 ; ! endReached ;)
{
characterToCheck = [theText characterAtIndex: i];
if (characterToCheck == '(') count++;
else if (characterToCheck == ')') count--;
if (count > rightCount) rightCount = count;
if (i == leftMiddle) endReached = YES;
else i--;
}
/*==============================================*/
/* Balance to the left and right of the cursor. */
/*==============================================*/
[self balanceIt: theText
leftMiddle: ((leftMiddle == 0) ? 0 : leftMiddle - 1)
rightMiddle: rightMiddle
leftCount: leftCount
rightCount: rightCount
textLength: textLength];
}
}
/********************************************************/
/* balanceIt: Balances a selection of text by extending */
/* it to the left and right until the number of left */
/* and right parentheses is balanced. */
/********************************************************/
- (void) balanceIt: (NSString *) theText
leftMiddle: (NSUInteger) leftMiddle
rightMiddle: (NSUInteger) rightMiddle
leftCount: (int) leftCount
rightCount: (int) rightCount
textLength: (NSUInteger) textLength
{
unichar characterToCheck;
BOOL beginningReached = NO;
/*==========================================================*/
/* Balance the left side of the text by moving left and up. */
/*==========================================================*/
while (leftCount <= 0)
{
if (beginningReached)
{
NSBeep();
return;
}
characterToCheck = [theText characterAtIndex: leftMiddle];
if (characterToCheck == '(') leftCount++;
else if (characterToCheck == ')') leftCount--;
if (leftCount <= 0)
{
if (leftMiddle > 0) leftMiddle--;
else beginningReached = YES;
}
}
/*==============================================================*/
/* Balance the right side of the text by moving right and down. */
/*==============================================================*/
while (rightCount >= 0)
{
if (rightMiddle >= textLength)
{
NSBeep();
return;
}
characterToCheck = [theText characterAtIndex: rightMiddle];
if (characterToCheck == '(') rightCount++;
else if (characterToCheck == ')') rightCount--;
rightMiddle++;
}
/*=============================================*/
/* Set the current selection to balanced text. */
/*=============================================*/
NSRange theRange = { leftMiddle, rightMiddle - leftMiddle };
[textView setSelectedRange: theRange];
/*=====================================*/
/* Make sure the selection is visible. */
/*=====================================*/
[textView scrollRangeToVisible: theRange];
}
/*****************************************/
/* comment: Handles the Comment command. */
/*****************************************/
- (IBAction) comment: (id) sender
{
NSRange selectionRange, rangeOfLine, tempRange;
unsigned int numberOfLines = 0, i;
NSUInteger tempLocation, maxSelectedRange;
NSUInteger locationOfFirstLine, locationOfLastLine;
NSString *theText = [textView string];
/*==============================================*/
/* Retrieve the current selection and determine */
/* the location of the beginning and end. */
/*==============================================*/
selectionRange = [textView selectedRange];
tempLocation = selectionRange.location;
maxSelectedRange = NSMaxRange(selectionRange);
/*=====================================*/
/* Determine the beginning location of */
/* the first line to be commented. */
/*=====================================*/
tempRange.location = tempLocation;
tempRange.length = 0;
locationOfFirstLine = [theText lineRangeForRange: tempRange].location;
/*=============================================*/
/* Count the number of lines in the selection. */
/*=============================================*/
do
{
tempRange.location = tempLocation;
tempRange.length = 0;
tempLocation = NSMaxRange([theText lineRangeForRange: tempRange]);
numberOfLines++;
}
while (tempLocation < maxSelectedRange);
/*=========================================================*/
/* Remember the location of the end of the last line, then */
/* reset the temporary location back to the first line. */
/*=========================================================*/
locationOfLastLine = tempLocation;
tempLocation = selectionRange.location;
/*=======================================================*/
/* Iterate over each line adding a ';' at the beginning. */
/*=======================================================*/
for (i = 0; i < numberOfLines; i++)
{
/*==============================================*/
/* Determine the beginning and end of the line. */
/*==============================================*/
tempRange.location = tempLocation;
tempRange.length = 0;
rangeOfLine = [theText lineRangeForRange: tempRange];
/*======================================*/
/* Comment character should be inserted */
/* at the beginning of the line. */
/*======================================*/
tempRange.location = rangeOfLine.location;
tempRange.length = 0;
/*==============================================================*/
/* Add the comment character. Using shouldChangeTextInRange and */
/* didChangeText are necessary to support undoing this action. */
/*==============================================================*/
if ([textView shouldChangeTextInRange: tempRange replacementString: @";"])
{
[textView replaceCharactersInRange: tempRange withString: @";"];
[textView didChangeText];
}
/*========================*/
/* Move to the next line. */
/*========================*/
tempRange.location = tempLocation;
tempRange.length = 0;
tempLocation = NSMaxRange([theText lineRangeForRange: tempRange]);
}
/*=======================================================*/
/* If there was a selection when the comment command was */
/* invoked, extend it to completely include all of the */
/* lines in which comment characters were added. */
/*=======================================================*/
if (selectionRange.length > 0)
{
tempRange.location = locationOfFirstLine;
tempRange.length = (locationOfLastLine - locationOfFirstLine) + numberOfLines;
[textView setSelectedRange: tempRange];
}
}
/*********************************************/
/* uncomment: Handles the Uncomment command. */
/*********************************************/
- (IBAction) uncomment: (id) sender
{
NSRange selectionRange, rangeOfLine, tempRange;
unsigned int numberOfLines = 0, i;
unsigned int numberOfCharactersDeleted = 0;
NSUInteger tempLocation, maxSelectedRange;
NSUInteger locationOfFirstLine, locationOfLastLine;
NSString *theText = [textView string];
/*==============================================*/
/* Retrieve the current selection and determine */
/* the location of the beginning and end. */
/*==============================================*/
selectionRange = [textView selectedRange];
tempLocation = selectionRange.location;
maxSelectedRange = NSMaxRange(selectionRange);
/*=====================================*/
/* Determine the beginning location of */
/* the first line to be commented. */
/*=====================================*/
tempRange.location = tempLocation;
tempRange.length = 0;
locationOfFirstLine = [theText lineRangeForRange: tempRange].location;
/*=============================================*/
/* Count the number of lines in the selection. */
/*=============================================*/
do
{
tempRange.location = tempLocation;
tempRange.length = 0;
tempLocation = NSMaxRange([theText lineRangeForRange: tempRange]);
numberOfLines++;
}
while (tempLocation < maxSelectedRange);
/*=========================================================*/
/* Remember the location of the end of the last line, then */
/* reset the temporary location back to the first line. */
/*=========================================================*/
locationOfLastLine = tempLocation;
tempLocation = selectionRange.location;
/*=======================================================*/
/* Iterate over each line adding a ';' at the beginning. */
/*=======================================================*/
for (i = 0; i < numberOfLines; i++)
{
/*==============================================*/
/* Determine the beginning and end of the line. */
/*==============================================*/
tempRange.location = tempLocation;
tempRange.length = 0;
rangeOfLine = [theText lineRangeForRange: tempRange];
if ([theText characterAtIndex: rangeOfLine.location] == ';')
{
/*=====================================*/
/* Comment character should be removed */
/* from the beginning of the line. */
/*=====================================*/
tempRange.location = rangeOfLine.location;
tempRange.length = 1;
/*=================================================================*/
/* Remove the comment character. Using shouldChangeTextInRange and */
/* didChangeText are necessary to support undoing this action. */
/*=================================================================*/
if ([textView shouldChangeTextInRange: tempRange replacementString: @""])
{
[textView replaceCharactersInRange: tempRange withString: @""];
[textView didChangeText];
numberOfCharactersDeleted++;
}
}
/*========================*/
/* Move to the next line. */
/*========================*/
tempRange.location = tempLocation;
tempRange.length = 0;
tempLocation = NSMaxRange([theText lineRangeForRange: tempRange]);
}
/*===============================================*/
/* Beep if there weren't any characters deleted. */
/*===============================================*/
if (numberOfCharactersDeleted == 0)
{
NSBeep();
return;
}
/*=======================================================*/
/* If there was a selection when the comment command was */
/* invoked, extend it to completely include all of the */
/* lines in which comment characters were added. */
/*=======================================================*/
if (selectionRange.length > 0)
{
tempRange.location = locationOfFirstLine;
tempRange.length = (locationOfLastLine - locationOfFirstLine) - numberOfCharactersDeleted;
[textView setSelectedRange: tempRange];
}
}
@end