forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlobalSearch.m
739 lines (657 loc) · 23.1 KB
/
GlobalSearch.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
// -*- mode:objc -*-
/*
** GlobalSearchView.m
**
** Copyright (c) 2011
**
** Author: George Nachman
**
** Project: iTerm2
**
** Description: Logic and custom NSView subclass for searching all tabs
** simultaneously.
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#import "GlobalSearch.h"
#import "VT100Screen.h"
#import "PTYTextView.h"
#import "PseudoTerminal.h"
#import "PTYSession.h"
#import "iTermController.h"
#import "iTermExpose.h"
#import "PTYTextView.h"
#import "iTermSearchField.h"
const double GLOBAL_SEARCH_MARGIN = 10;
@interface GlobalSearchInstance : NSObject
{
PTYTextView* textView_;
id<PTYTextViewDataSource> textViewDataSource_;
PTYSession* theSession_;
NSMutableArray* results_;
BOOL more_;
NSString* findString_;
NSString* label_;
FindContext findContext_;
NSMutableSet* matchLocations_;
}
- (id)initWithTextView:(PTYTextView*)textView
findString:(NSString*)findString
label:(NSString*)label;
- (void)dealloc;
- (BOOL)more;
- (NSArray*)results;
- (NSString*)label;
- (PTYTextView*)textView;
- (PTYSession*)session;
@end
@interface GlobalSearchResult : NSObject
{
GlobalSearchInstance* instance_;
NSString* context_;
NSString* findString_;
int x_;
int endX_;
long long absY_;
long long absEndY_;
}
- (id)initWithInstance:(GlobalSearchInstance*)instance context:(NSString*)theContext x:(int)x absY:(long long)absY endX:(int)endX y:(long long)absEndY findString:(NSString*)findString;
- (void)dealloc;
- (NSString*)context;
- (NSString*)findString;
- (GlobalSearchInstance*)instance;
- (int)x;
- (int)endX;
- (long long)absY;
- (long long)absEndY;
- (int)y;
- (int)endY;
@end
@implementation GlobalSearchResult
- (id)initWithInstance:(GlobalSearchInstance*)instance context:(NSString*)theContext x:(int)x absY:(long long)absY endX:(int)endX y:(long long)absEndY findString:(NSString*)findString;
{
assert(findString);
assert(theContext);
self = [super init];
if (self) {
instance_ = [instance retain];
context_ = [theContext copy];
x_ = x;
endX_ = endX;
absY_ = absY;
absEndY_ = absEndY;
findString_ = [findString copy];
}
return self;
}
- (void)dealloc
{
[instance_ release];
[context_ release];
[findString_ release];
[super dealloc];
}
- (NSString*)context
{
return context_;
}
- (NSString*)findString
{
assert(findString_);
return findString_;
}
- (GlobalSearchInstance*)instance
{
return instance_;
}
- (int)x
{
return x_;
}
- (int)endX
{
return endX_;
}
- (long long)absY
{
return absY_;
}
- (long long)absEndY
{
return absEndY_;
}
- (int)y
{
return absY_ - [[[instance_ textView] dataSource] totalScrollbackOverflow];
}
- (int)endY
{
return absEndY_ - [[[instance_ textView] dataSource] totalScrollbackOverflow];
}
@end
@implementation GlobalSearchInstance
- (id)initWithTextView:(PTYTextView*)textView
findString:(NSString*)findString
label:(NSString*)label
{
assert(findString);
assert(label);
self = [super init];
if (self) {
results_ = [[NSMutableArray alloc] init];
findString_ = [findString copy];
more_ = YES;
textView_ = textView;
textViewDataSource_ = [textView dataSource]; // TODO: this is a weak ref. Be on the lookout for its death.
theSession_ = [textViewDataSource_ session];
label_ = [label retain];
[textViewDataSource_ initFindString:findString_
forwardDirection:NO
ignoringCase:YES
regex:NO
startingAtX:0
startingAtY:(long long)([textViewDataSource_ numberOfLines] + 1) + [textViewDataSource_ totalScrollbackOverflow]
withOffset:0 // 1?
inContext:&findContext_
multipleResults:NO];
matchLocations_ = [[NSMutableSet alloc] init];
findContext_.hasWrapped = YES;
}
return self;
}
- (void)dealloc
{
[matchLocations_ release];
[results_ release];
[findString_ release];
[label_ release];
[super dealloc];
}
- (BOOL)more
{
return more_;
}
- (NSArray*)results
{
return results_;
}
- (NSString*)label
{
return label_;
}
- (BOOL)_emitResultFromX:(int)startX y:(int)startY toX:(int)endX y:(int)endY
{
// Don't add the same line twice.
long long absY = startY + [[textView_ dataSource] totalScrollbackOverflow];
NSNumber* setObj = [NSNumber numberWithLongLong:absY];
if ([matchLocations_ containsObject:setObj]) {
return NO;
}
[matchLocations_ addObject:setObj];
NSString* theContext = [textView_ contentFromX:0
Y:startY
ToX:[textViewDataSource_ width] - 1
Y:endY
pad:NO
includeLastNewline:NO
trimTrailingWhitespace:YES];
theContext = [theContext stringByReplacingOccurrencesOfString:@"\n"
withString:@" "];
[results_ addObject:[[[GlobalSearchResult alloc] initWithInstance:self
context:theContext
x:startX
absY:absY
endX:endX
y:endY + [[textView_ dataSource] totalScrollbackOverflow]
findString:findString_] autorelease]];
return YES;
}
- (int)doSearch
{
BOOL more;
BOOL found;
int newResults = 0;
NSDate* begin = [NSDate date];
NSDate* now;
const double kMaxTime = 0.01;
do {
int startX;
int startY;
int endX;
int endY;
more = [textViewDataSource_ continueFindResultAtStartX:&startX
atStartY:&startY
atEndX:&endX
atEndY:&endY
found:&found
inContext:&findContext_];
if (found) {
if ([self _emitResultFromX:startX y:startY toX:endX y:endY]) {
++newResults;
}
[textViewDataSource_ initFindString:findString_
forwardDirection:NO
ignoringCase:YES
regex:NO
startingAtX:startX
startingAtY:startY
withOffset:1
inContext:&findContext_
multipleResults:NO];
findContext_.hasWrapped = YES;
}
now = [NSDate date];
} while ((found || more) && [now timeIntervalSinceDate:begin] < kMaxTime);
more_ = (found || more);
return newResults;
}
- (PTYTextView*)textView
{
return textView_;
}
- (PTYSession*)session
{
return theSession_;
}
@end
@implementation GlobalSearchView
- (void)drawRect:(NSRect)rect
{
NSRect myFrame = [self frame];
myFrame.origin.x = GLOBAL_SEARCH_MARGIN;
myFrame.origin.y = GLOBAL_SEARCH_MARGIN;
myFrame.size.height -= GLOBAL_SEARCH_MARGIN;
myFrame.size.width -= 2 * GLOBAL_SEARCH_MARGIN;
NSShadow *dropShadow = [[[NSShadow alloc] init] autorelease];
[dropShadow setShadowColor:[NSColor colorWithCalibratedHue:0
saturation:0
brightness:0.2
alpha:1]];
[dropShadow setShadowBlurRadius:5];
[dropShadow setShadowOffset:NSMakeSize(0,-4)];
NSBezierPath* thePath = [NSBezierPath bezierPath];
[thePath appendBezierPathWithRoundedRect:NSMakeRect(myFrame.origin.x + 5,
myFrame.origin.y,
myFrame.size.width - 10,
myFrame.size.height)
xRadius:10
yRadius:10];
[dropShadow set];
[thePath fill];
[[NSColor windowBackgroundColor] set];
[thePath fill];
}
@end
@implementation GlobalSearch
- (void)awakeFromNib
{
[self view]; // make sure the view is instantiated
[searchField_ setDelegate:self];
[searchField_ setArrowHandler:tableView_];
[tableView_ setDataSource:self];
[tableView_ setDelegate:self];
[tableView_ setDoubleAction:@selector(onDoubleClick:)];
[tableView_ setTarget:self];
for (NSTableColumn* aCol in [tableView_ tableColumns]) {
[aCol setEditable:NO];
}
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
searches_ = [[NSMutableArray alloc] init];
combinedResults_ = [[NSMutableArray alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(removeDanglers)
name:@"iTermWindowDidClose"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(removeDanglers)
name:@"iTermNumberOfSessionsDidChange"
object:nil];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[combinedResults_ release];
[timer_ invalidate];
[searches_ release];
[super dealloc];
}
- (void)_resizeView
{
NSRect viewFrame = [[self view] frame];
const NSRect origViewFrame = viewFrame;
double dh = viewFrame.size.height;
int n = [combinedResults_ count];
double newTableHeight = 1 + [[tableView_ headerView] frame].size.height + n * ([tableView_ rowHeight] + [tableView_ intercellSpacing].height);
if (n == 0) {
newTableHeight = 0;
}
viewFrame.size.height = [tableView_ frame].origin.y + newTableHeight + 70;
const double maxViewHeight = [[self view] frame].origin.y + [[self view] frame].size.height - 2 * GLOBAL_SEARCH_MARGIN;
viewFrame.size.height = MIN(maxViewHeight, viewFrame.size.height);
dh -= viewFrame.size.height;
viewFrame.origin.y += dh;
if (!NSEqualRects(viewFrame, [[self view] frame])) {
[[self view] setFrame:viewFrame];
[delegate_ globalSearchViewDidResize:origViewFrame];
}
}
- (void)removeDanglers
{
NSMutableArray* allSessions = [NSMutableArray arrayWithCapacity:100];
for (PseudoTerminal* term in [[iTermController sharedInstance] terminals]) {
[allSessions addObjectsFromArray:[term allSessions]];
}
for (int i = [searches_ count] - 1; i >= 0; i--) {
GlobalSearchInstance* inst = [searches_ objectAtIndex:i];
if ([allSessions indexOfObjectIdenticalTo:[inst session]] == NSNotFound) {
[searches_ removeObjectAtIndex:i];
}
}
for (int i = [combinedResults_ count] - 1; i >= 0; i--) {
if ([searches_ indexOfObjectIdenticalTo:[[combinedResults_ objectAtIndex:i] instance]] == NSNotFound) {
[combinedResults_ removeObjectAtIndex:i];
}
}
[self _resizeView];
[tableView_ reloadData];
}
- (void)_startSearches
{
[combinedResults_ removeAllObjects];
[self _resizeView];
[tableView_ reloadData];
timer_ = [NSTimer scheduledTimerWithTimeInterval:0.2
target:self
selector:@selector(_continueSearch)
userInfo:nil
repeats:NO];
}
- (void)_addResult:(GlobalSearchResult*)result
{
int j = [combinedResults_ count];
for (int i = [combinedResults_ count] - 1; i >= 0; i--) {
GlobalSearchResult* current = [combinedResults_ objectAtIndex:i];
if ([current instance] == [result instance]) {
j = i;
break;
}
}
[combinedResults_ insertObject:result atIndex:j];
}
- (void)_addLastResultsToTable:(int)n fromInstance:(GlobalSearchInstance*)inst
{
NSArray* results = [inst results];
for (int i = [results count] - n; i < [results count]; i++) {
[self _addResult:[results objectAtIndex:i]];
}
[self _resizeView];
[tableView_ reloadData];
}
- (void)_continueSearch
{
NSDate* begin = [NSDate date];
const float kMaxTime = 0.1;
while ([searches_ count]) {
GlobalSearchInstance* inst = [searches_ objectAtIndex:0];
[inst retain];
[searches_ removeObjectAtIndex:0];
int newResults = [inst doSearch];
if ([inst more]) {
[searches_ addObject:inst];
}
[self _addLastResultsToTable:newResults fromInstance:inst];
[inst release];
NSDate* now = [NSDate date];
if ([now timeIntervalSinceDate:begin] > kMaxTime) {
break;
}
}
if (![searches_ count]) {
timer_ = nil;
} else {
timer_ = [NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(_continueSearch)
userInfo:nil
repeats:NO];
}
}
- (void)_clearSearches
{
[timer_ invalidate];
timer_ = nil;
[searches_ removeAllObjects];
}
- (IBAction)onDoubleClick:(id)sender
{
[delegate_ globalSearchOpenSelection];
}
#pragma mark Search field delegate
- (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
{
if ([[searchField_ stringValue] length] == 0 &&
commandSelector == @selector(cancelOperation:)) {
[delegate_ globalSearchCanceled];
return YES;
}
return NO;
}
- (void)controlTextDidEndEditing:(NSNotification *)aNotification
{
int move = [[[aNotification userInfo] objectForKey:@"NSTextMovement"] intValue];
if (move == NSReturnTextMovement) {
[delegate_ globalSearchOpenSelection];
}
}
- (void)controlTextDidChange:(NSNotification *)aNotification
{
NSTextField *field = [aNotification object];
if (field != searchField_) {
return;
}
[self _clearSearches];
NSString* findString = [searchField_ stringValue];
if ([findString length] == 0) {
[combinedResults_ removeAllObjects];
[self _resizeView];
[tableView_ reloadData];
return;
}
int i = 0;
for (PseudoTerminal* aTerminal in [[iTermController sharedInstance] terminals]) {
for (PTYSession* aSession in [aTerminal sessions]) {
NSArray* tabs = [aTerminal tabs];
int j;
for (j = 0; j < [tabs count]; ++j) {
if ([[[tabs objectAtIndex:j] sessions] indexOfObjectIdenticalTo:aSession] != NSNotFound) {
break;
}
}
GlobalSearchInstance* aSearch;
aSearch = [[[GlobalSearchInstance alloc] initWithTextView:[aSession TEXTVIEW]
findString:findString
label:[iTermExpose labelForTab:[aSession tab]
windowNumber:i+1
tabNumber:j+1]] autorelease];
[searches_ addObject:aSearch];
}
i++;
}
[self _startSearches];
}
- (NSString*)_tabNameForResult:(GlobalSearchResult*)theResult
{
return [[theResult instance] label];
}
- (NSAttributedString*)_attributedSubstringOf:(NSAttributedString *)as
narrowerThan:(CGFloat)maxWidth
wantHead:(BOOL)wantHead
{
// Binary search for the length that best fits.
int minLen = 0;
int maxLen = [as length];
int length = [as length];
int prev = -1;
int cur = (minLen + maxLen) / 2;
NSAttributedString* subString = nil;
while (cur != prev) {
if (wantHead) {
subString = [as attributedSubstringFromRange:NSMakeRange(0, MIN(length, 1+cur))];
} else {
int start = MAX(0, length - cur - 1);
subString = [as attributedSubstringFromRange:NSMakeRange(start, length-start)];
}
CGFloat w = [subString size].width;
if (w >= maxWidth) {
maxLen = cur;
} else if (w < maxWidth) {
minLen = cur;
}
prev = cur;
cur = (maxLen + minLen) / 2;
}
return subString;
}
- (NSAttributedString*)_snippetForResult:(GlobalSearchResult*)theResult isSelected:(BOOL)isSelected maxWidth:(CGFloat)maxWidth
{
NSMutableAttributedString* as = [[[NSMutableAttributedString alloc] init] autorelease];
NSColor* textColor;
if (isSelected) {
textColor = [NSColor whiteColor];
} else {
textColor = [NSColor blackColor];
}
float size = [NSFont systemFontSize];
NSFont* sysFont = [NSFont systemFontOfSize:size];
NSDictionary* plainAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
sysFont, NSFontAttributeName,
textColor, NSForegroundColorAttributeName,
nil];
NSDictionary* boldAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSFont boldSystemFontOfSize:size], NSFontAttributeName,
textColor, NSForegroundColorAttributeName,
nil];
NSString* findString = [theResult findString];
assert(findString);
NSMutableString* contextTail = [NSMutableString stringWithString:[theResult context]];
NSAttributedString* matchStr = [[[NSAttributedString alloc] initWithString:findString attributes:boldAttributes] autorelease];
CGFloat matchLen = [matchStr size].width;
CGFloat maxPrefixWidth = (maxWidth - matchLen) / 2;
if (maxPrefixWidth < 0) {
maxPrefixWidth = maxWidth * 0.2;
}
while ([contextTail length]) {
int end;
NSRange match = [contextTail rangeOfString:findString
options:(NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch | NSWidthInsensitiveSearch)];
NSString* plainPart;
NSString* matchPart;
if (match.location != NSNotFound) {
if (match.location > 0) {
plainPart = [contextTail substringWithRange:NSMakeRange(0, match.location)];
} else {
plainPart = nil;
}
matchPart = [contextTail substringWithRange:match];
end = match.location + match.length;
} else {
plainPart = contextTail;
matchPart = nil;
end = [contextTail length];
}
if (plainPart) {
NSAttributedString* substr = [[[NSAttributedString alloc] initWithString:plainPart
attributes:plainAttributes] autorelease];
// Append the first plain part only if it doesn't take more than
// half the space remaining after including the findString.
if ([as length] == 0 &&
[substr size].width > maxPrefixWidth) {
substr = [self _attributedSubstringOf:substr
narrowerThan:maxPrefixWidth
wantHead:NO];
}
[as appendAttributedString:substr];
}
if (matchPart) {
[as appendAttributedString:[[[NSAttributedString alloc] initWithString:matchPart
attributes:boldAttributes] autorelease]];
}
[contextTail deleteCharactersInRange:NSMakeRange(0, end)];
}
return [self _attributedSubstringOf:as
narrowerThan:maxWidth
wantHead:YES];
}
- (int)numResults
{
return [combinedResults_ count];
}
#pragma mark NSTableView dataSource
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [combinedResults_ count];
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
GlobalSearchResult* theResult = [combinedResults_ objectAtIndex:rowIndex];
switch ([[aTableColumn identifier] intValue]) {
case 0:
// Tab name
return [self _tabNameForResult:theResult];
case 1:
// Snippet
return [self _snippetForResult:theResult isSelected:[aTableView selectedRow]==rowIndex maxWidth:[aTableColumn width]];
default:
// ??
return @"";
}
}
- (void)setDelegate:(id<GlobalSearchDelegate>)delegate;
{
delegate_ = delegate;
}
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
{
int i = [tableView_ selectedRow];
GlobalSearchInstance* inst = nil;
PTYTextView* tv = nil;
GlobalSearchResult* theResult = nil;
PTYSession* session = nil;
if (i >= 0) {
theResult = [combinedResults_ objectAtIndex:i];
inst = [theResult instance];
tv = [inst textView];
[tv setSelectionFromX:[theResult x] fromY:[theResult y] toX:[theResult endX]+1 toY:[theResult endY]];
[tv scrollToSelection];
session = [[tv dataSource] session];
} else {
theResult = nil;
}
[delegate_ globalSearchSelectionChangedToSession:session];
}
- (void)abort
{
if (timer_) {
[timer_ invalidate];
timer_ = nil;
}
}
@end