-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathPlainTextEditor.m
More file actions
2736 lines (2244 loc) · 115 KB
/
PlainTextEditor.m
File metadata and controls
2736 lines (2244 loc) · 115 KB
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
// PlainTextEditor.m
// SubEthaEdit
//
// Created by Dominik Wagner on Tue Apr 06 2004.
#import "FindReplaceController.h"
#import "SEEDocumentController.h"
#import "PlainTextEditor.h"
#import "SEEParticipantsOverlayViewController.h"
#import "PlainTextDocument.h"
#import "PlainTextWindowController.h"
#import "LayoutManager.h"
#import "SEETextView.h"
#import "GutterRulerView.h"
#import "DocumentMode.h"
#import "TCMMMUserManager.h"
#import "TCMMMUser.h"
#import "TCMMMUserSEEAdditions.h"
#import "TCMMMSession.h"
#import "TCMMMTransformator.h"
#import "SEEPlainTextEditorScrollView.h"
#import "PopUpButtonCell.h"
#import "RadarScroller.h"
#import "SelectionOperation.h"
#import "UndoManager.h"
#import "BorderedTextField.h"
#import "DocumentModeManager.h"
#import "AppController.h"
#import "InsetTextFieldCell.h"
#import <OgreKit/OgreKit.h>
#import "SyntaxDefinition.h"
#import "SyntaxHighlighter.h"
#import "ScriptTextSelection.h"
#import "NSMenuTCMAdditions.h"
#import "NSImageTCMAdditions.h"
#import "NSMutableAttributedStringSEEAdditions.h"
#import "FoldableTextStorage.h"
#import "FoldedTextAttachment.h"
#import "URLBubbleWindow.h"
#import "SEEFindAndReplaceViewController.h"
#import <objc/objc-runtime.h>
#import "SEEPlainTextEditorTopBarViewController.h"
#import "SEEOverlayView.h"
#import "NSLayoutConstraint+TCMAdditions.h"
#import "TCMHoverButton.h"
NSString * const PlainTextEditorDidFollowUserNotification = @"PlainTextEditorDidFollowUserNotification";
NSString * const PlainTextEditorDidChangeSearchScopeNotification = @"PlainTextEditorDidChangeSearchScopeNotification";
@interface NSTextView (PrivateAdditions)
- (BOOL)_isUnmarking;
@end
@interface PlainTextEditor () <TCMHoverButtonRightMouseDownHandler>
@property (nonatomic, strong) IBOutlet NSView *O_editorView;
@property (nonatomic, strong) IBOutlet SEEOverlayView *O_bottomStatusBarView;
@property (nonatomic, strong) IBOutlet TCMHoverButton *shareInviteUsersButtonOutlet;
@property (nonatomic, strong) IBOutlet TCMHoverButton *shareAnnounceButtonOutlet;
@property (nonatomic, strong) IBOutlet TCMHoverButton *showParticipantsButtonOutlet;
@property (nonatomic, strong) IBOutlet NSObjectController *ownerController;
@property (nonatomic, strong) NSArray *topLevelNibObjects;
@property (nonatomic, strong) NSViewController *bottomOverlayViewController;
@property (nonatomic, strong) NSViewController *topOverlayViewController;
@property (nonatomic, strong) SEEFindAndReplaceViewController *findAndReplaceController;
@property (nonatomic, strong) SEEPlainTextEditorTopBarViewController *topBarViewController;
@property (nonatomic, strong) NSArray *topBlurBackgroundConstraints;
@property (nonatomic, strong) SEEOverlayView *topBlurLayerView;
@property (nonatomic, strong) NSArray *bottomBlurBackgroundConstraints;
@property (nonatomic, strong) SEEOverlayView *bottomBlurLayerView;
- (void)TCM_updateBottomStatusBar;
- (float)pageGuidePositionForColumns:(int)aColumns;
@end
@implementation PlainTextEditor {
IBOutlet PopUpButton *O_tabStatusPopUpButton;
IBOutlet PopUpButton *O_modePopUpButton;
IBOutlet PopUpButton *O_encodingPopUpButton;
IBOutlet PopUpButton *O_lineEndingPopUpButton;
IBOutlet BorderedTextField *O_windowWidthTextField;
IBOutlet NSView *O_bottomBarSeparatorLineView;
IBOutlet SEEPlainTextEditorScrollView *O_scrollView;
RadarScroller *I_radarScroller;
SEETextView *I_textView;
NSTextContainer *I_textContainer;
NSMutableArray *I_storedSelectedRanges;
__weak PlainTextWindowControllerTabContext *I_windowControllerTabContext;
NSString *I_followUserID;
struct {
BOOL showTopStatusBar;
BOOL showBottomStatusBar;
BOOL hasSplitButton;
BOOL symbolPopUpIsSorted;
BOOL pausedProcessing;
} I_flags;
SelectionOperation *I_storedPosition;
}
- (instancetype)initWithWindowControllerTabContext:(PlainTextWindowControllerTabContext *)aWindowControllerTabContext splitButton:(BOOL)aFlag {
self = [super init];
if (self) {
I_windowControllerTabContext = aWindowControllerTabContext;
I_flags.hasSplitButton = aFlag;
I_flags.showTopStatusBar = NO;
I_flags.showBottomStatusBar = NO;
I_flags.pausedProcessing = NO;
I_storedSelectedRanges = [NSMutableArray new];
[self setFollowUserID:nil];
NSArray *topLevelNibObjects = nil;
[[NSBundle mainBundle] loadNibNamed:@"PlainTextEditor" owner:self topLevelObjects:&topLevelNibObjects];
self.topLevelNibObjects = topLevelNibObjects;
[self loadViewPostprocessing];
}
return self;
}
- (void)_removeNotificationRegistrations {
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
PlainTextDocument *document = I_windowControllerTabContext.document;
[defaultCenter removeObserver:document name:NSTextViewDidChangeSelectionNotification object:I_textView];
[defaultCenter removeObserver:document name:NSTextDidChangeNotification object:I_textView];
[defaultCenter removeObserver:self];
}
- (void)prepareForDealloc {
// remove our layoutmanager from the textstorage
PlainTextDocument *document = I_windowControllerTabContext.document;
[[document textStorage] removeLayoutManager:self.textView.layoutManager];
[self _removeNotificationRegistrations];
// release the objects that are bound so we get dealloced later
self.ownerController.content = nil;
self.topLevelNibObjects = nil;
}
- (void)dealloc {
[self _removeNotificationRegistrations];
[I_textView setEditor:nil]; // in case our editor outlives us
[self.O_editorView setNextResponder:nil];
}
- (BOOL)hitTestOverlayViewsWithEvent:(NSEvent *)aEvent {
BOOL result = NO;
NSPoint eventLocationInWindow = aEvent.locationInWindow;
if ([self.topBlurLayerView hitTest:[self.topBlurLayerView.superview convertPoint:eventLocationInWindow fromView:nil]] != nil) {
result = YES;
} else if ([self.bottomBlurLayerView hitTest:[self.bottomBlurLayerView.superview convertPoint:eventLocationInWindow fromView:nil]] != nil) {
result = YES;
}
return result;
}
- (void)participantsDidChange:(NSNotification *)aNotification {
[self TCM_updateNumberOfActiveParticipants];
}
- (void)sessionWillChange:(NSNotification *)aNotification {
PlainTextDocument *document = [self document];
__auto_type session = document.session;
[[NSNotificationCenter defaultCenter] removeObserver:self name:TCMMMSessionParticipantsDidChangeNotification object:session];
[[NSNotificationCenter defaultCenter] removeObserver:self name:TCMMMSessionDidChangeNotification object:session];
}
- (void)sessionDidChange:(NSNotification *)aNotification {
BOOL isServer = [[[self document] session] isServer];
self.canAnnounceAndShare = isServer;
[self TCM_updateLocalizedToolTips];
[self updateAnnounceButton];
[self TCM_updateNumberOfActiveParticipants];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(participantsDidChange:)
name:TCMMMSessionParticipantsDidChangeNotification
object:[[self document] session]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(documentSessionPropertysDidUpdate:)
name:TCMMMSessionDidChangeNotification
object:[[self document] session]];
}
- (void)documentSessionPropertysDidUpdate:(NSNotification *)aNotification {
[self TCM_updateLocalizedToolTips];
[self updateAnnounceButton];
}
- (void)loadViewSetupBarsAndOverlays {
// topbarviewcontroller
self.topBarViewController = [[SEEPlainTextEditorTopBarViewController alloc] initWithPlainTextEditor:self];
[self.topBarViewController updateColorsForIsDarkBackground:[self hasDarkBackground]];
[self.topBarViewController setSplitButtonVisible:NO];
self.topBarViewController.splitButtonVisible = I_flags.hasSplitButton;
[self.topBarViewController setVisible:I_flags.showTopStatusBar];
[self.O_editorView addSubview:self.topBarViewController.view];
// generate top blur layer
self.topBlurLayerView = ({
SEEOverlayView *view = [[SEEOverlayView alloc] initWithFrame:NSZeroRect];
NSView *containerView = self.O_editorView;
view.translatesAutoresizingMaskIntoConstraints = NO;
[containerView addSubview:view];
[containerView addConstraints:@[
[NSLayoutConstraint TCM_constraintWithItem:view secondItem:containerView
equalAttribute:NSLayoutAttributeLeft],
[NSLayoutConstraint TCM_constraintWithItem:view secondItem:containerView
equalAttribute:NSLayoutAttributeRight],
[NSLayoutConstraint TCM_constraintWithItem:view secondItem:containerView
equalAttribute:NSLayoutAttributeTop],
]];
self.topBlurBackgroundConstraints = @[
[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:0]
];
[containerView addConstraints:self.topBlurBackgroundConstraints];
// view.layer.backgroundColor = [[[NSColor redColor] colorWithAlphaComponent:0.8] CGColor];
view;
});
// change the top status bar to use constraints
{
NSView *statusBarView = self.topBarViewController.view;
NSView *containerView = self.topBlurLayerView;
[statusBarView removeFromSuperview];
[statusBarView setTranslatesAutoresizingMaskIntoConstraints:NO];
[containerView addSubview:statusBarView];
[containerView addConstraints:@[
[NSLayoutConstraint TCM_constraintWithItem:statusBarView secondItem:containerView equalAttribute:NSLayoutAttributeLeft],
[NSLayoutConstraint TCM_constraintWithItem:statusBarView secondItem:containerView
equalAttribute:NSLayoutAttributeRight],
[NSLayoutConstraint TCM_constraintWithItem:statusBarView secondItem:containerView equalAttribute:NSLayoutAttributeBottom],
[NSLayoutConstraint constraintWithItem:statusBarView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0 constant:CGRectGetHeight(statusBarView.bounds)],
]];
[self updateTopPinConstraints];
}
// generate bottom blur layer
self.bottomBlurLayerView = ({
SEEOverlayView *view = [[SEEOverlayView alloc] initWithFrame:NSZeroRect];
NSView *containerView = self.O_editorView;
view.translatesAutoresizingMaskIntoConstraints = NO;
[containerView addSubview:view];
[containerView addConstraints:@[
[NSLayoutConstraint TCM_constraintWithItem:view secondItem:containerView
equalAttribute:NSLayoutAttributeLeft],
[NSLayoutConstraint TCM_constraintWithItem:view secondItem:containerView
equalAttribute:NSLayoutAttributeRight],
[NSLayoutConstraint TCM_constraintWithItem:view secondItem:containerView
equalAttribute:NSLayoutAttributeBottom],
]];
self.bottomBlurBackgroundConstraints = @[
[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:0]
];
[containerView addConstraints:self.bottomBlurBackgroundConstraints];
// view.layer.backgroundColor = [[[NSColor redColor] colorWithAlphaComponent:0.8] CGColor];
view;
});
// change the bottom status bar to use constraints
{
NSView *statusBarView = self.O_bottomStatusBarView;
NSView *containerView = self.bottomBlurLayerView;
[statusBarView removeFromSuperview];
// configure truncate mode
[O_tabStatusPopUpButton.cell setLineBreakMode:NSLineBreakByTruncatingMiddle];
[O_encodingPopUpButton.cell setLineBreakMode:NSLineBreakByTruncatingMiddle];
[statusBarView setTranslatesAutoresizingMaskIntoConstraints:NO];
[statusBarView setAutoresizesSubviews:YES];
[containerView addSubview:statusBarView];
[containerView addConstraints:@[
[NSLayoutConstraint TCM_constraintWithItem:statusBarView secondItem:containerView
equalAttribute:NSLayoutAttributeRight],
[NSLayoutConstraint TCM_constraintWithItem:statusBarView secondItem:containerView equalAttribute:NSLayoutAttributeLeft],
[NSLayoutConstraint TCM_constraintWithItem:statusBarView secondItem:containerView
equalAttribute:NSLayoutAttributeBottom],
[NSLayoutConstraint constraintWithItem:statusBarView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0 constant:CGRectGetHeight(statusBarView.bounds)],
]];
[statusBarView setHidden:!I_flags.showBottomStatusBar];
}
// setup all the UI for the bottom bar
[O_windowWidthTextField setHasRightBorder:NO];
[O_windowWidthTextField setHasLeftBorder:YES];
DocumentModeMenu *menu = [DocumentModeMenu new];
[menu configureWithAction:@selector(chooseMode:) alternateDisplay:NO];
[[O_modePopUpButton cell] setMenu:menu];
EncodingMenu *fileEncodingsSubmenu = [EncodingMenu new];
[fileEncodingsSubmenu configureWithAction:@selector(selectEncoding:)];
[[[fileEncodingsSubmenu itemArray] lastObject] setTarget:self];
[[[fileEncodingsSubmenu itemArray] lastObject] setAction:@selector(showCustomizeEncodingPanel:)];
[[O_encodingPopUpButton cell] setMenu:fileEncodingsSubmenu];
NSMenu *lineEndingMenu = [NSMenu new];
[O_lineEndingPopUpButton setPullsDown:YES];
// insert title item of pulldown popupbutton
[lineEndingMenu addItem:[[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""]];
NSMenuItem *item = nil;
SEL chooseLineEndings = @selector(chooseLineEndings:);
NSEnumerator *formatSubmenuItems = [[[[[NSApp mainMenu] itemWithTag:FormatMenuTag] submenu] itemArray] objectEnumerator];
while ((item = [formatSubmenuItems nextObject])) {
if ([item hasSubmenu] && [[[[item submenu] itemArray] objectAtIndex:0] action] == chooseLineEndings) {
NSEnumerator *interestingItems = [[[item submenu] itemArray] objectEnumerator];
NSMenuItem *innerItem = nil;
while ((innerItem = [interestingItems nextObject])) {
if ([innerItem isSeparatorItem]) {
[lineEndingMenu addItem:[NSMenuItem separatorItem]];
} else {
item = [[NSMenuItem alloc] initWithTitle:[innerItem title] action:[innerItem action] keyEquivalent:@""];
[item setTarget:[innerItem target]];
[item setTag:[innerItem tag]];
[lineEndingMenu addItem:item];
}
}
break;
}
}
[[O_lineEndingPopUpButton cell] setMenu:lineEndingMenu];
[O_tabStatusPopUpButton setPullsDown:YES];
NSMenu *tabMenu = [NSMenu new];
// insert title item of pulldown popupbutton
[tabMenu addItem:[[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""]];
formatSubmenuItems = [[[[[NSApp mainMenu] itemWithTag:FormatMenuTag] submenu] itemArray] objectEnumerator];
BOOL copyItems = NO;
while ((item = [formatSubmenuItems nextObject])) {
if ([item action] == @selector(toggleUsesTabs:)) { copyItems = YES; }
if (copyItems) {
if ([item isSeparatorItem]) {
[tabMenu addItem:[NSMenuItem separatorItem]];
} else {
NSMenuItem *newItem = [tabMenu addItemWithTitle:[item title] action:[item action] keyEquivalent:@""];
[newItem setTarget:[item target]];
[newItem setTag:[item tag]];
if ([item hasSubmenu]) {
[newItem setSubmenu:[[item submenu] copy]];
}
}
}
}
[[O_tabStatusPopUpButton cell] setMenu:tabMenu];
[self updateTopScrollViewInset];
[self updateBottomScrollViewInset];
}
- (void)loadViewPostprocessing {
[self loadViewSetupBarsAndOverlays];
[self TCM_updateBottomStatusBar];
PlainTextDocument *document = [self document];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
if (document) {
[notificationCenter addObserver:self selector:@selector(defaultParagraphStyleDidChange:)
name:PlainTextDocumentDefaultParagraphStyleDidChangeNotification
object:document];
[notificationCenter addObserver:self selector:@selector(userDidChangeSelection:)
name:PlainTextDocumentUserDidChangeSelectionNotification
object:document];
[notificationCenter addObserver:self selector:@selector(plainTextDocumentDidChangeEditStatus:)
name:PlainTextDocumentDidChangeEditStatusNotification
object:document];
[notificationCenter addObserver:self selector:@selector(plainTextDocumentUserDidChangeSelection:)
name:PlainTextDocumentUserDidChangeSelectionNotification
object:document];
[notificationCenter addObserver:self selector:@selector(sessionWillChange:)
name:PlainTextDocumentSessionWillChangeNotification
object:document];
[notificationCenter addObserver:self selector:@selector(sessionDidChange:)
name:PlainTextDocumentSessionDidChangeNotification
object:document];
}
[notificationCenter addObserver:self selector:@selector(TCM_updateBottomStatusBar) name:@"AfterEncodingsListChanged" object:nil];
[notificationCenter addObserver:self selector:@selector(SEE_appEffectiveAppearanceDidChange:) name:SEEAppEffectiveAppearanceDidChangeNotification object:NSApp];
I_radarScroller = [RadarScroller new];
[O_scrollView setHasVerticalScroller:YES];
[O_scrollView setVerticalScroller:I_radarScroller];
NSRect frame = NSZeroRect;
frame.size = [O_scrollView SEE_effectiveContentSize];
[self.shareInviteUsersButtonOutlet sendActionOn:NSEventMaskLeftMouseDown];
self.shareInviteUsersButtonOutlet.rightMouseDownEventHandler = self;
[self.shareInviteUsersButtonOutlet setImagesByPrefix:@"BottomBarSharingIconAdd"];
[self.showParticipantsButtonOutlet setImagesByPrefix:@"BottomBarSharingIconGroupTurnOn"];
[self.shareAnnounceButtonOutlet setImagesByPrefix:@"BottomBarSharingIconAnnounceTurnOn"];
LayoutManager *layoutManager = [LayoutManager new];
[[document textStorage] addLayoutManager:layoutManager];
I_textContainer = [[NSTextContainer alloc] initWithContainerSize:NSMakeSize(frame.size.width, FLT_MAX)];
I_textView = ({
SEETextView *textView = [[SEETextView alloc] initWithFrame:frame textContainer:I_textContainer];
[textView setEditor:self];
[textView setHorizontallyResizable:NO];
[textView setVerticallyResizable:YES];
[textView setAutoresizingMask:NSViewWidthSizable];
[textView setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
[textView setSelectable:YES];
[textView setEditable:YES];
[textView setRichText:NO];
[textView setImportsGraphics:NO];
[textView setUsesFontPanel:NO];
[textView setUsesRuler:YES];
[textView setUsesFindPanel:YES];
[textView setAllowsUndo:NO];
[textView setSmartInsertDeleteEnabled:NO];
[textView turnOffLigatures:self];
[textView setLinkTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSCursor pointingHandCursor], NSCursorAttributeName, nil]];
[textView setDelegate:self];
textView;
});
[I_textContainer setHeightTracksTextView:NO];
[I_textContainer setWidthTracksTextView:YES];
[layoutManager addTextContainer:I_textContainer];
[O_scrollView setVerticalRulerView:[[GutterRulerView alloc] initWithScrollView:O_scrollView orientation:NSVerticalRuler]];
[O_scrollView setHasVerticalRuler:YES];
[[O_scrollView verticalRulerView] setRuleThickness:42.];
[O_scrollView setDocumentView:I_textView];
[[O_scrollView verticalRulerView] setClientView:I_textView];
[[O_scrollView contentView] setPostsBoundsChangedNotifications:YES];
[I_textView setFrameOrigin:CGPointZero];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contentViewBoundsDidChange:) name:NSViewBoundsDidChangeNotification object:[O_scrollView contentView]];
[I_textView setDefaultParagraphStyle:[document defaultParagraphStyle]];
[notificationCenter addObserver:document selector:@selector(textViewDidChangeSelection:) name:NSTextViewDidChangeSelectionNotification object:I_textView];
[notificationCenter addObserver:document selector:@selector(textDidChange:) name:NSTextDidChangeNotification object:I_textView];
[notificationCenter addObserver:self selector:@selector(textDidChange:) name:PlainTextDocumentDidChangeTextStorageNotification object:document];
[I_textView setPostsFrameChangedNotifications:YES];
[notificationCenter addObserver:self selector:@selector(textViewFrameDidChange:) name:NSViewFrameDidChangeNotification object:I_textView];
// Adding a second view hierachy to include this controller into the responder chain
NSView *view = [[NSView alloc] initWithFrame:[self.O_editorView frame]];
[view setAutoresizesSubviews:YES];
[view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[view addSubview:self.O_editorView];
[view setPostsFrameChangedNotifications:YES];
[view setWantsLayer:self.O_editorView.wantsLayer];
[self.O_editorView setNextResponder:self];
[self setNextResponder:view];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewFrameDidChange:) name:NSViewFrameDidChangeNotification object:view];
self.O_editorView = view;
// localize the announce status menu - take from main menu
NSMenuItem *accessMenuItem = [[AppController sharedInstance] accessControlMenuItem];
NSMenu *accessPopUpMenu = self.shareAnnounceButtonOutlet.menu;
NSInteger menuItemIndex = 0;
for (NSMenuItem *item in [accessMenuItem.submenu itemArray]) {
if (!item.isAlternate) {
NSMenuItem *targetMenuItem = accessPopUpMenu.itemArray[menuItemIndex++];
targetMenuItem.title = item.title;
targetMenuItem.keyEquivalent = item.keyEquivalent;
targetMenuItem.keyEquivalentModifierMask = item.keyEquivalentModifierMask;
}
if (menuItemIndex >= accessPopUpMenu.itemArray.count) break;
}
[self takeStyleSettingsFromDocument];
[self takeSettingsFromDocument];
[self setShowsBottomStatusBar:[document showsBottomStatusBar]]; // needed for initial setup when plainTextEditors array has no objects
[self setShowsChangeMarks:[document showsChangeMarks]];
// set the right values for the status bars
[self TCM_updateBottomStatusBar];
[self.topBarViewController updateForSelectionDidChange];
// make sure we start out right
[self updateTopScrollViewInset];
[self updateBottomScrollViewInset];
// trigger the notfications for the first time
[self sessionDidChange:nil];
[self participantsDidChange:nil];
}
- (void)pushSelectedRanges {
[I_storedSelectedRanges addObject:[NSValue valueWithRange:[I_textView selectedRange]]];
}
- (void)popSelectedRanges {
NSValue *value = [I_storedSelectedRanges lastObject];
if (value) {
NSRange selectedRange = [value rangeValue];
[I_textView setSelectedRange:RangeConfinedToRange(selectedRange, NSMakeRange(0, [[I_textView string] length]))];
[I_storedSelectedRanges removeLastObject];
} else {
[I_textView setSelectedRange:NSMakeRange(0, 0)];
}
}
- (void)adjustDisplayOfPageGuide {
PlainTextDocument *document = [self document];
if (document) {
DocumentMode *mode = [document documentMode];
if ([[mode defaultForKey:DocumentModeShowPageGuidePreferenceKey] boolValue]) {
[(SEETextView *)I_textView setPageGuidePosition :[self pageGuidePositionForColumns:[[mode defaultForKey:DocumentModePageGuideWidthPreferenceKey] intValue]]];
} else {
[(SEETextView *)I_textView setPageGuidePosition : 0];
}
}
}
- (BOOL)hasDarkBackground {
BOOL result = self.document.documentBackgroundColor.isDark;
return result;
}
- (void)updateColorsForIsDarkBackground:(BOOL)isDark {
if (@available(macOS 10.14, *)) {
NSAppearance *desiredAppearance = [NSAppearance appearanceNamed:isDark ? NSAppearanceNameDarkAqua : NSAppearanceNameAqua];
O_scrollView.appearance = desiredAppearance;
}
[self.topBarViewController updateColorsForIsDarkBackground:isDark];
BOOL isDarkAppearance = NSApp.SEE_effectiveAppearanceIsDark;
// bottom bar
NSColor *darkSeparatorColor = [NSColor darkOverlaySeparatorColorBackgroundIsDark:isDark appearanceIsDark:isDarkAppearance];
[O_windowWidthTextField setBorderColor:darkSeparatorColor];
[O_bottomBarSeparatorLineView.layer setBackgroundColor:[darkSeparatorColor CGColor]];
for (PopUpButton *button in @[O_modePopUpButton,O_tabStatusPopUpButton, O_encodingPopUpButton, O_lineEndingPopUpButton]) {
[button setLineColor:darkSeparatorColor];
}
NSScrollerKnobStyle knobStyle = isDark ? NSScrollerKnobStyleLight : NSScrollerKnobStyleDefault;
[[O_scrollView verticalScroller] setKnobStyle:knobStyle];
[[O_scrollView horizontalScroller] setKnobStyle:knobStyle];
// overlays?
NSViewController *bottomOverlayViewController = self.bottomOverlayViewController;
if (bottomOverlayViewController) {
if ([bottomOverlayViewController isKindOfClass:[SEEParticipantsOverlayViewController class]]) {
SEEParticipantsOverlayViewController *viewController = (SEEParticipantsOverlayViewController *)bottomOverlayViewController;
[viewController updateColorsForIsDarkBackground:isDark];
}
}
if (self.findAndReplaceController &&
self.findAndReplaceController == self.topOverlayViewController) {
[self.findAndReplaceController updateColorsForIsDarkBackground:isDark];
}
}
- (void)takeStyleSettingsFromDocument {
PlainTextDocument *document = [self document];
if (document) {
BOOL isDark = [self hasDarkBackground];
[[self textView] setBackgroundColor:[document documentBackgroundColor]];
[self updateColorsForIsDarkBackground:isDark];
NSColor *invisibleCharacterColor = [[document styleAttributesForScope:@"meta.invisible.character" languageContext:nil] objectForKey:NSForegroundColorAttributeName];
[(LayoutManager *)[[self textView] layoutManager] setInvisibleCharacterColor : invisibleCharacterColor ? invisibleCharacterColor :[NSColor grayColor]];
}
}
- (void)SEE_appEffectiveAppearanceDidChange:(NSNotification *)notification {
[self takeStyleSettingsFromDocument]; // update all the relevant bits for an appearance change
[self.document triggerUpdateSymbolTableTimer]; // make sure the symbol graphics are the right tone
}
- (void)takeSettingsFromDocument {
PlainTextDocument *document = [self document];
if (document) {
[self setShowsInvisibleCharacters:[document showInvisibleCharacters]];
[self setWrapsLines:[document wrapLines]];
[self setShowsGutter:[document showsGutter]];
[self setShowsTopStatusBar:[document showsTopStatusBar]];
if (self.windowControllerTabContext.plainTextEditors.lastObject == self) {
[self setShowsBottomStatusBar:[document showsBottomStatusBar]]; // this is done, because one editor should not display a status bar on the top split editor
}
[I_textView setEditable:[document isEditable]];
[I_textView setContinuousSpellCheckingEnabled:[document isContinuousSpellCheckingEnabled]];
DocumentMode *documentMode = [document documentMode];
NSDictionary *attributeForDefaultKeyDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
DocumentModeGrammarCheckingPreferenceKey, @"setGrammarCheckingEnabled:",
DocumentModeAutomaticLinkDetectionPreferenceKey, @"setAutomaticLinkDetectionEnabled:",
DocumentModeAutomaticDashSubstitutionPreferenceKey, @"setAutomaticDashSubstitutionEnabled:",
DocumentModeAutomaticQuoteSubstitutionPreferenceKey, @"setAutomaticQuoteSubstitutionEnabled:",
DocumentModeAutomaticTextReplacementPreferenceKey, @"setAutomaticTextReplacementEnabled:",
DocumentModeAutomaticSpellingCorrectionPreferenceKey, @"setAutomaticSpellingCorrectionEnabled:",
nil];
NSEnumerator *keyEnumerator = [attributeForDefaultKeyDictionary keyEnumerator];
NSString *attributeString = nil;
while ((attributeString = [keyEnumerator nextObject])) {
NSString *defaultKey = [attributeForDefaultKeyDictionary objectForKey:attributeString];
SEL attributeSetter = NSSelectorFromString(attributeString);
if ([I_textView respondsToSelector:attributeSetter]) {
((void (*)(id, SEL, BOOL))objc_msgSend)(I_textView, attributeSetter, [[documentMode defaultForKey:defaultKey] boolValue]);
// NSLog(@"%s set %@ for %@ now %@",__FUNCTION__,attributeString, defaultKey, [documentMode defaultForKey:defaultKey]);
}
}
}
[self.topBarViewController updateSymbolPopUpContent];
[self.topBarViewController updateForSelectionDidChange];
[self TCM_updateBottomStatusBar];
[self adjustDisplayOfPageGuide];
}
- (BOOL)isShowingFindAndReplaceInterface {
BOOL result = self.findAndReplaceController && [self.topOverlayViewController isEqual:self.findAndReplaceController];
return result;
}
- (void)adjustToScrollViewInsets {
[I_textView adjustContainerInsetToScrollView];
[[O_scrollView verticalRulerView] setNeedsDisplay:YES];
[[[O_scrollView window] windowController] updateWindowMinSize];
}
- (float)pageGuidePositionForColumns:(int)aColumns
{
NSFont *font = [[self document] fontWithTrait:0];
CGFloat characterWidth = [@"n" sizeWithAttributes :[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]].width;
return aColumns * characterWidth + [[I_textView textContainer] lineFragmentPadding] + [I_textView textContainerInset].width;
}
- (NSSize)desiredSizeForColumns:(int)aColumns rows:(int)aRows {
NSSize result;
NSFont *font = [[self document] fontWithTrait:0];
CGFloat characterWidth = [@"n" sizeWithAttributes :[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]].width;
result.width = characterWidth * aColumns + [[I_textView textContainer] lineFragmentPadding] * 2 + [I_textView textContainerInset].width * 2 + ([self.O_editorView bounds].size.width - [O_scrollView SEE_effectiveContentSize].width) + 2.0;
result.height = [[I_textContainer layoutManager] defaultLineHeightForFont:font] * aRows +
([self.O_editorView bounds].size.height - [O_scrollView SEE_effectiveContentSize].height) + O_scrollView.topOverlayHeight + O_scrollView.bottomOverlayHeight + 2.0;
return result;
}
- (int)displayedRows {
int rows = 0;
if ([self document]) {
NSFont *font = [[self document] fontWithTrait:0];
rows = (int)(([(SEEPlainTextEditorScrollView *)[I_textView enclosingScrollView] SEE_effectiveContentSize].height - [I_textView textContainerInset].height * 2) / [[I_textView layoutManager] defaultLineHeightForFont:font]);
}
return rows;
}
- (int)displayedColumns {
int columns = 0;
if ([self document]) {
NSFont *font = [[self document] fontWithTrait:0];
CGFloat characterWidth = [@"n" sizeWithAttributes :[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]].width;
columns = (int)(([I_textView bounds].size.width - [I_textView textContainerInset].width * 2 - [[I_textView textContainer] lineFragmentPadding] * 2) / characterWidth);
}
return columns;
}
- (CGFloat)desiredMinHeight {
CGFloat result = 50.0;
SEEPlainTextEditorScrollView *scrollView = O_scrollView;
result += scrollView.topOverlayHeight + scrollView.bottomOverlayHeight;
return result;
}
#pragma mark - Editor Button Tooltips
- (void)TCM_updateLocalizedToolTips {
self.localizedToolTipToggleParticipantsButton = ({
NSString *string;
if (self.hasBottomOverlayView) {
string = NSLocalizedStringWithDefaultValue(@"TOOL_TIP_PARTICIPANTS_BUTTON_HIDE", nil, [NSBundle mainBundle], @"Hide Participants", @"Editor Tool Tip Participants Button - Hide");
} else {
string = NSLocalizedStringWithDefaultValue(@"TOOL_TIP_PARTICIPANTS_BUTTON_SHOW", nil, [NSBundle mainBundle], @"Show Participants", @"Editor Tool Tip Participants Button - Show");
}
string;
});
BOOL isServer = [[[self document] session] isServer];
self.localizedToolTipShareInviteButton = ({
NSString *string;
if (isServer) {
string = NSLocalizedStringWithDefaultValue(@"TOOL_TIP_SHARE_BUTTON_DEFAULT", nil, [NSBundle mainBundle], @"Share Document", @"Editor Tool Tip Share Invite Button - Share");
} else {
string = NSLocalizedStringWithDefaultValue(@"TOOL_TIP_SHARE_BUTTON_DISABLED", nil, [NSBundle mainBundle], @"Share Document (Disabled)", @"Editor Tool Tip Share Invite Button - Cannot share");
}
string;
});
self.localizedToolTipAnnounceButton = ({
NSString *string;
if (isServer) {
if ([self.document isAnnounced]) {
string = NSLocalizedStringWithDefaultValue(@"TOOL_TIP_ANNOUNCE_BUTTON_CONCEAL", nil, [NSBundle mainBundle], @"Conceal Document", @"Editor Tool Tip Advertise Button - Conceal");
} else {
string = NSLocalizedStringWithDefaultValue(@"TOOL_TIP_ANNOUNCE_BUTTON_ANNOUNCE", nil, [NSBundle mainBundle], @"Advertise Document", @"Editor Tool Tip Advertise Button - Advertise");
}
} else {
string = NSLocalizedStringWithDefaultValue(@"TOOL_TIP_ANNOUNCE_BUTTON_DISABLED", nil, [NSBundle mainBundle], @"Advertise Document (Disabled)", @"Editor Tool Tip Advertise Button - Disabled");
}
string;
});
}
- (void)TCM_updateNumberOfActiveParticipants {
NSUInteger participantCount = [[[self document] session] participantCount];
self.numberOfActiveParticipants = @(participantCount);
self.showsNumberOfActiveParticipants = participantCount > 1;
}
- (void)TCM_updateBottomStatusBar {
if (I_flags.showBottomStatusBar) {
PlainTextDocument *document = [self document];
[O_tabStatusPopUpButton setTitle:[NSString stringWithFormat:NSLocalizedString(@"%@ (%d)", @"arrangement of Tab setting and tab width in Bottm Status Bar"), [document usesTabs] ? NSLocalizedString(@"TrueTab", @"Bottom status bar text for TrueTab setting"):NSLocalizedString(@"Spaces", @"Bottom status bar text for use Spaces (instead of Tab) setting"), [document tabWidth]]];
[O_modePopUpButton selectItemAtIndex:[O_modePopUpButton indexOfItemWithTag:[[DocumentModeManager sharedInstance] tagForDocumentModeIdentifier:[[document documentMode] documentModeIdentifier]]]];
[O_encodingPopUpButton selectItemAtIndex:[O_encodingPopUpButton indexOfItemWithTag:[document fileEncoding]]];
int charactersPerLine = [self displayedColumns];
[O_windowWidthTextField setStringValue:[NSString stringWithFormat:NSLocalizedString(@"WindowWidth%d%@", @"WindowWidthArangementString"), charactersPerLine, [O_scrollView hasHorizontalScroller] ? @"":([document wrapMode] == DocumentModeWrapModeCharacters ? NSLocalizedString(@"CharacterWrap", @"As shown in bottom status bar") : NSLocalizedString(@"WordWrap", @"As shown in bottom status bar"))]];
[O_lineEndingPopUpButton selectItemAtIndex:[O_lineEndingPopUpButton indexOfItemWithTag:[document lineEnding]]];
NSString *lineEndingStatusString = @"";
switch ([document lineEnding]) {
case LineEndingLF :
lineEndingStatusString = @"LF";
break;
case LineEndingCR :
lineEndingStatusString = @"CR";
break;
case LineEndingCRLF:
lineEndingStatusString = @"CRLF";
break;
case LineEndingUnicodeLineSeparator:
lineEndingStatusString = @"LSEP";
break;
case LineEndingUnicodeParagraphSeparator:
lineEndingStatusString = @"PSEP";
break;
}
[O_lineEndingPopUpButton setTitle:SEE_NoLocalizationNeeded(lineEndingStatusString)];
}
}
- (void)updateAnnounceButton {
BOOL isAnnounced = self.document.isAnnounced;
[self.shareAnnounceButtonOutlet setImagesByPrefix:isAnnounced ?
@"BottomBarSharingIconAnnounceTurnOff" :
@"BottomBarSharingIconAnnounceTurnOn"];
if (isAnnounced) {
TCMMMSession *session = self.document.session;
switch (session.accessState) {
case TCMMMSessionAccessReadOnlyState:
self.shareAnnounceButtonOutlet.normalImage = [NSImage imageNamed:@"BottomBarSharingIconAnnounceTurnOffNormalReadOnly"];
break;
case TCMMMSessionAccessReadWriteState:
self.shareAnnounceButtonOutlet.normalImage = [NSImage imageNamed:@"BottomBarSharingIconAnnounceTurnOffNormalReadWrite"];
break;
case TCMMMSessionAccessLockedState:
default:
// intentionally nothing
break;
}
}
}
- (void)updateShowParticipantsButton {
}
- (NSView *)editorView {
return self.O_editorView;
}
- (NSTextView *)textView {
return I_textView;
}
- (PlainTextDocument *)document {
return (PlainTextDocument *)[I_windowControllerTabContext document];
}
- (void)setWindowControllerTabContext:(PlainTextWindowControllerTabContext *)aContext {
I_windowControllerTabContext = aContext;
}
- (PlainTextWindowControllerTabContext *)windowControllerTabContext {
return I_windowControllerTabContext;
}
- (void)updateSplitButtonForIsSplit:(BOOL)aFlag {
if (I_flags.hasSplitButton) {
self.topBarViewController.splitButtonShowsClose = aFlag;
}
}
- (NSInteger)dentLineInTextView:(NSTextView *)aTextView withRange:(NSRange)aLineRange in:(BOOL)aIndent {
NSInteger changedChars = 0;
static NSCharacterSet *spaceTabSet = nil;
if (!spaceTabSet) {
spaceTabSet = [NSCharacterSet whitespaceCharacterSet];
}
NSRange affectedCharRange = NSMakeRange(aLineRange.location, 0);
NSString *replacementString = @"";
NSTextStorage *textStorage = [aTextView textStorage];
NSString *string = [textStorage string];
PlainTextDocument *document = self.document;
int tabWidth = [document tabWidth];
if ([document usesTabs]) {
if (aIndent) {
// replace spaces with tabs and add one tab
NSUInteger lastCharacter = aLineRange.location;
while (lastCharacter < NSMaxRange(aLineRange) &&
[spaceTabSet characterIsMember:[string characterAtIndex:lastCharacter]]) {
lastCharacter++;
}
if (aLineRange.location != lastCharacter && lastCharacter < NSMaxRange(aLineRange)) {
affectedCharRange = NSMakeRange(aLineRange.location, lastCharacter - aLineRange.location);
unsigned detabbedLength = [string detabbedLengthForRange:affectedCharRange
tabWidth :tabWidth];
replacementString = [NSString stringWithFormat:@"\t%@%@",
[@"" stringByPaddingToLength : (int)detabbedLength / tabWidth
withString: @"\t" startingAtIndex : 0],
[@"" stringByPaddingToLength : (int)detabbedLength % tabWidth
withString : @" " startingAtIndex : 0]
];
if (affectedCharRange.length != [replacementString length] - 1) {
changedChars = [replacementString length] - affectedCharRange.length;
} else {
affectedCharRange = NSMakeRange(aLineRange.location, 0);
replacementString = @"\t";
changedChars = 1;
}
} else {
replacementString = @"\t";
changedChars = 1;
}
} else {
if ([string length] > aLineRange.location) {
// replace spaces with tabs and remove one tab or the remaining whitespace
NSUInteger lastCharacter = aLineRange.location;
while (lastCharacter < NSMaxRange(aLineRange) &&
[spaceTabSet characterIsMember:[string characterAtIndex:lastCharacter]])
lastCharacter++;
affectedCharRange = NSMakeRange(aLineRange.location, lastCharacter - aLineRange.location);
if (aLineRange.location != lastCharacter && lastCharacter < NSMaxRange(aLineRange)) {
affectedCharRange = NSMakeRange(aLineRange.location, lastCharacter - aLineRange.location);
unsigned detabbedLength = [string detabbedLengthForRange:affectedCharRange
tabWidth :tabWidth];
replacementString = [NSString stringWithFormat:@"%@%@",
[@"" stringByPaddingToLength : (int)detabbedLength / tabWidth
withString : @"\t" startingAtIndex : 0],
[@"" stringByPaddingToLength : (int)detabbedLength % tabWidth
withString : @" " startingAtIndex : 0]
];
if ([replacementString length] != affectedCharRange.length ||
((int)detabbedLength / tabWidth) == 0) {
if ((int)detabbedLength / tabWidth > 0) {
replacementString = [replacementString substringWithRange:NSMakeRange(1, [replacementString length] - 1)];
} else {
replacementString = @"";
}
changedChars = [replacementString length] - affectedCharRange.length;
} else {
// this if is always true due to the ifs above
// if ([string characterAtIndex:aLineRange.location]==[@"\t" characterAtIndex:0]) {
affectedCharRange = NSMakeRange(aLineRange.location, 1);
changedChars = -1;
replacementString = @"";
// }
}
} else {
changedChars = [replacementString length] - affectedCharRange.length;
}
}
}
} else {
NSUInteger firstCharacter = aLineRange.location;
// replace tabs with spaces
while (firstCharacter < NSMaxRange(aLineRange)) {
unichar character;
character = [string characterAtIndex:firstCharacter];
if (character == [@" " characterAtIndex : 0]) {
firstCharacter++;
}
else if (character == [@"\t" characterAtIndex : 0]) {
changedChars += tabWidth - 1;
firstCharacter++;
}
else {
break;
}
}
if (changedChars != 0) {
NSRange affectedRange = NSMakeRange(aLineRange.location, firstCharacter - aLineRange.location);
NSString *replacementString = [@" " stringByPaddingToLength : firstCharacter - aLineRange.location + changedChars
withString : @" " startingAtIndex : 0];
if ([aTextView shouldChangeTextInRange:affectedRange
replacementString:replacementString]) {
NSAttributedString *attributedReplaceString = [[NSAttributedString alloc]
initWithString:replacementString
attributes:[aTextView typingAttributes]];
[textStorage replaceCharactersInRange:affectedRange
withAttributedString:attributedReplaceString];
firstCharacter += changedChars;
}
}
if (aIndent) {
changedChars += tabWidth;
replacementString = [@" " stringByPaddingToLength : tabWidth
withString : @" " startingAtIndex : 0];
} else {
if (firstCharacter >= affectedCharRange.location + tabWidth) {
affectedCharRange.length = tabWidth;
changedChars -= tabWidth;