-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBCSearchController.m
587 lines (343 loc) · 17.1 KB
/
BCSearchController.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
//
// BCSearchController.m
// Caravan
//
// Created by Tom Houpt on 15/9/27.
// Copyright (c) 2015 Tom Houpt. All rights reserved.
//
#import "BCSearchController.h"
#import "CaravanSourceFile.h"
#import "CaravanSourceListDocument.h"
@implementation BCFindResult
@end
#define CONTEXT_WIDTH 30
@implementation BCSearchTableView
-(void)handleDoubleClick:(id)sender;
{
[(BCSearchController *)[self dataSource] handleRowDoubleClick:self];
}
@end
@implementation BCSearchController
@synthesize dialog;
-(id)init; {
self = [super init];
if (self) {
// set up some useful data structures
// to replace control characters and the unicode for NSAttachmentCharacter (whichmarks citekeys in text)
unsigned char bitmapRep[8192];
for (int i=0; i<8192; i++) {
bitmapRep[i] = 0;
}
bitmapRep[NSAttachmentCharacter >> 3] |= (((unsigned int)1) << (NSAttachmentCharacter & 7));
NSMutableCharacterSet *mut_controlChars = [NSMutableCharacterSet characterSetWithBitmapRepresentation:[NSData dataWithBytes:bitmapRep length:8192]];
[mut_controlChars formUnionWithCharacterSet:[NSCharacterSet controlCharacterSet]];
controlChars = [mut_controlChars copy];
//the font for highlighting found string in results
NSFont *boldFont = [NSFont fontWithName:@"Menlo Bold" size:12.0];
boldAttribute = [NSDictionary dictionaryWithObject:boldFont forKey:NSFontAttributeName];
// the dictionary for inserting odd characters into find or replace strings
unichar attachmentChar = NSAttachmentCharacter;
NSString *attachmentString = [NSString stringWithCharacters:&attachmentChar length:1];
insertDictionary = [ NSDictionary dictionaryWithObjectsAndKeys:
@"\t", @"Tab",
@"\r", @"Line Break",
@"\r\r", @"Paragraph Break",
@"???", @"Page Break",
@"@([a-zA-Z0-9])", @"Any Characters",
@"([a-zA-Z]+)", @"Letters",
@"([0-9]+)", @"Digits",
@"(\\s+)", @"White Space",
attachmentString, @"CiteKey Marker",
nil];
if (!dialog) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[NSBundle loadNibNamed:@"BCSearchDialog" owner:self];
#pragma clang diagnostic pop
}
}
return self;
}
-(void)awakeFromNib; {
[self.position setEnabled:YES];
[self.position selectCellAtRow:kContainsSearchTerm column:0];
[self.regex setState:NSOffState];
[self.ignoreCase setState:NSOnState];
}
-(NSArray *)dialogForWindow:(NSWindow *)ownerWindow;
{
// NOTE: run last used keywords, select last selected journal
[self regexPressed:self];
NSString *findPBoardString = [[NSPasteboard pasteboardWithName:NSFindPboard] stringForType:NSPasteboardTypeString];
if (nil != findPBoardString) {
[self.findString setStringValue:findPBoardString];
}
NSString *replacePBoardString = [[NSPasteboard pasteboardWithName:@"NSReplacePboard"] stringForType:NSPasteboardTypeString];
if (nil != replacePBoardString) {
[self.findString setStringValue:replacePBoardString];
}
[self.replace setEnabled: NO];
[self.resultsTable setDoubleAction:@selector(handleDoubleClick:)];
[self.resultsTable setRowHeight:24];
[self.resultsTable setDelegate:self];
[self.resultsTable setDataSource:self];
[self.results removeAllObjects];
NSTableColumn *fileColumn = [self.resultsTable tableColumnWithIdentifier:@"filename"];
[fileColumn setTitle: [NSString stringWithFormat:@"Files (--)"]];
NSTableColumn *resultsColumn = [self.resultsTable tableColumnWithIdentifier:@"text"];
[resultsColumn setTitle: [NSString stringWithFormat:@"Search Results (--)"]];
[self.resultsTable reloadData];
if (self.results == nil) { self.results = [NSMutableArray array]; }
[NSApp beginSheet: dialog
modalForWindow: ownerWindow
modalDelegate: nil
didEndSelector: nil
contextInfo: nil];
[NSApp runModalForWindow: dialog];
// See NSApplication Class Reference/runModalSession
[NSApp endSheet: dialog];
[dialog orderOut: self];
[self.resultsTable setDelegate:nil];
[self.resultsTable setDataSource:nil];
return nil;
}
-(void)handleRowDoubleClick:(BCSearchTableView*)table; {
NSInteger selectedRow = [self.resultsTable selectedRow];
if (-1 == selectedRow) {
return;
}
BCFindResult *result = [self.results objectAtIndex:selectedRow];
[result.file setLastInsertionIndex:result.resultRange.location];
[self.document expandAndSelectSourceListObject: result.file];
[[result.file textView] setSelectedRange:result.resultRange];
[NSApp stopModal];
}
-(IBAction)OKButtonPressed:(id)sender;{
[NSApp stopModal];
}
-(IBAction)cancelButtonPressed:(id)sender; {
[NSApp stopModal];
}
-(IBAction)regexPressed:(id)sender; {
[self.position setEnabled:![self.regex state]];
}
-(IBAction)ignoreCasePressed:(id)sender; {
[self findStringEntered:self];
}
-(IBAction)positionPressed:(id)sender; {
[self findStringEntered:self];
}
-(void)setSearchParameters;
{
searchOptions = 0;
regexOptions = 0;
if ([self.regex state] == NSOnState) {
regexPattern = [self.findString stringValue];
if ([self.ignoreCase state] == NSOnState) {
regexOptions = NSRegularExpressionCaseInsensitive;
}
regExp = [NSRegularExpression regularExpressionWithPattern:regexPattern options:regexOptions error:nil];
}
else {
if ([self.position selectedRow] == kFullWordSearchTerm) {
regexPattern = [NSString stringWithFormat:@"\\b(%@)\\b",[self.findString stringValue]];
if ([self.ignoreCase state] == NSOnState) {
regexOptions = NSRegularExpressionCaseInsensitive;
}
regExp = [NSRegularExpression regularExpressionWithPattern:regexPattern options:regexOptions error:nil];
}
else if ([self.position selectedRow] == kStartsWithSearchTerm) {
regexPattern = [NSString stringWithFormat:@"\\b(%@)",[self.findString stringValue]];
if ([self.ignoreCase state] == NSOnState) {
regexOptions = NSRegularExpressionCaseInsensitive;
}
regExp = [NSRegularExpression regularExpressionWithPattern:regexPattern options:regexOptions error:nil];
}
else if ([self.position selectedRow] == kEndsWithSearchTerm) {
regexPattern = [NSString stringWithFormat:@"(%@)\\b",[self.findString stringValue]];
if ([self.ignoreCase state] == NSOnState) {
regexOptions = NSRegularExpressionCaseInsensitive;
}
regExp = [NSRegularExpression regularExpressionWithPattern:regexPattern options:regexOptions error:nil];
}
if ([self.ignoreCase state] == NSOnState) {
searchOptions = searchOptions | NSCaseInsensitiveSearch;
}
}
}
-(NSRange)searchText:(NSString *)text inRange:(NSRange)searchRange; {
NSRange range;
if (([self.regex state] == NSOnState) || ([self.position selectedRow] != kContainsSearchTerm)) {
NSTextCheckingResult *regResult = [regExp firstMatchInString:text options:0 range:searchRange];
range = (nil == regResult) ? NSMakeRange(NSNotFound,0) : [regResult range];
}
else {
range = [text rangeOfString:[self.findString stringValue] options:searchOptions range:searchRange];
}
// {NSNotFound, 0} if find string is not found
return range;
}
-(IBAction)findStringEntered:(id)sender; {
[self.results removeAllObjects];
NSInteger numberOfFilesWithResults = 0;
// check if findString is valid
if (nil != [self.findString stringValue] && 0 < [[self.findString stringValue] length]) {
// save findString to NSFindPBoard
[[NSPasteboard pasteboardWithName:NSFindPboard] clearContents];
[[NSPasteboard pasteboardWithName:NSFindPboard] setString:[self.findString stringValue] forType:NSStringPboardType];
[[NSPasteboard pasteboardWithName:@"NSReplacePBoard"] clearContents];
[[NSPasteboard pasteboardWithName:@"NSReplacePBoard"] setString:[self.replaceString stringValue] forType:NSStringPboardType];
// set up regex and other search options
[self setSearchParameters];
// get all the text files we're going to search
NSArray *files = [self.document textFilesForFindAndReplace];
for (CaravanSourceFile *file in files) {
BOOL foundResult = NO;
NSString *text = [[[file textView] textStorage] string];
NSRange searchRange = NSMakeRange(0, [text length]);
while (searchRange.location < [text length]) {
NSRange range = [self searchText:text inRange:searchRange];
// {NSNotFound, 0} if find string is not found
if (NSNotFound != range.location) {
BCFindResult *result = [[BCFindResult alloc] init];
result.resultRange = range;
result.file = file;
NSRange resultRange = range;
resultRange.length += (CONTEXT_WIDTH * 2);
if (CONTEXT_WIDTH < resultRange.location) { resultRange.location -= CONTEXT_WIDTH; }
else { resultRange.location = 0; }
if ([text length] < (resultRange.length + resultRange.location)) {
resultRange.length = [text length] - resultRange.location; }
NSString *resultString = [text substringWithRange:resultRange];
// NOTE: get rid of citation marks as well?
resultString = [
[resultString componentsSeparatedByCharactersInSet:controlChars]
componentsJoinedByString:@" "];
NSRange resultInStringRange = [resultString rangeOfString:[self.findString stringValue] options:NSCaseInsensitiveSearch];
if (resultInStringRange.location < CONTEXT_WIDTH) {
NSInteger padding = CONTEXT_WIDTH - resultInStringRange.location;
NSMutableString *pad = [NSMutableString stringWithCapacity:padding];
for (NSInteger i=0; i<padding;i++) {
[pad appendString:@" "];
}
resultString = [pad stringByAppendingString:resultString];
// NOTE: truncate end by same amount
}
result.resultString = [[NSMutableAttributedString alloc] initWithString:resultString];
[result.resultString addAttribute:NSFontAttributeName
value:[NSFont fontWithName:@"Menlo Bold" size:12.0]
range:NSMakeRange(CONTEXT_WIDTH,range.length)];
// NOTE: need to fix padding/bolding if findString occurs twice in same context string
[self.results addObject:result];
searchRange = range;
searchRange.location += [[self.findString stringValue] length];
searchRange.length = [text length] - searchRange.location;
foundResult = YES;
}
else {
searchRange.location = NSNotFound;
}
}
if (foundResult) { numberOfFilesWithResults++; }
}
}// valid findString
// display number of results, and reload results table
NSTableColumn *fileColumn = [self.resultsTable tableColumnWithIdentifier:@"filename"];
[fileColumn setTitle: [NSString stringWithFormat:@"Files (%ld)",numberOfFilesWithResults]];
NSTableColumn *resultsColumn = [self.resultsTable tableColumnWithIdentifier:@"text"];
[resultsColumn setTitle: [NSString stringWithFormat:@"Search Results (%ld)",[self.results count]]];
[self.resultsTable reloadData];
}
-(IBAction)replaceAllPressed:(id)sender; {
if (self.results == 0 || nil == [self.replaceString stringValue] || 0 == [[self.replaceString stringValue] length]) {
return;
}
// NOTE: put up alert that can not be undone
for (NSInteger i= ([self.results count] - 1); i>=0; i--) {
BCFindResult *result = [self.results objectAtIndex:i];
[self replaceFindResult:result];
}
// try re-searching for find term as a way to update the results..
// NOTE: post announcement of x # of replacments
[self findStringEntered:self];
}
-(BOOL) replaceFindResult:(BCFindResult *)result; {
// check if ok to make replacement
if ([[result.file textView] shouldChangeTextInRange:result.resultRange
replacementString:[self.replaceString stringValue]]) {
//if we wanted to preserve current attributes, then we could get attributedSubstringFromRange,
// then swap out the text for the replaced text (but leave the attributes)
NSTextStorage *store= [[result.file textView] textStorage];
// then notify that change took place
[[store mutableString] replaceCharactersInRange:result.resultRange withString:[self.replaceString stringValue]];
[[result.file textView] didChangeText];
// force file to save itself
[result.file save];
// NOTE: do we need to post notification after all text in this file changed?
// [[self delegate] textDidChange:[NSNotification notificationWithName:NSTextDidChangeNotification object:self]];
return YES;
}
return NO;
}
-(IBAction)replacePressed:(id)sender; {
NSInteger selectedRow = [self.resultsTable selectedRow];
if (-1 == selectedRow) {
return;
}
BCFindResult *result = [self.results objectAtIndex:selectedRow];
if ([self replaceFindResult:result]){
// NOTE Make this UNDOABLE ?
[self.results removeObjectAtIndex:selectedRow];
[self.resultsTable reloadData];
}
}
-(IBAction)deleteFindPressed:(id)sender; {
[self.findString setStringValue:@""];
}
-(IBAction)deleteReplacePressed:(id)sender; {
[self.replaceString setStringValue:@""];
}
-(void)insertString:(NSString *)insertString intoTextField:(NSTextField *)field; {
// NOTE: maybe we want to retrieve string, insert offline, then put string back into control...
[[field currentEditor] insertText:insertString];
}
-(IBAction)insertFindPressed:(id)sender; {
NSString *insertKey = [self.insertFind titleOfSelectedItem];
NSString *insertString = [insertDictionary objectForKey:insertKey];
[self insertString:insertString intoTextField:self.findString];
}
-(IBAction)insertReplacePressed:(id)sender;{
NSString *insertKey = [self.insertFind titleOfSelectedItem];
NSString *insertString = [insertDictionary objectForKey:insertKey];
[self insertString:insertString intoTextField:self.replaceString];
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView;
{
if (aTableView != self.resultsTable) { return 0; }
return [self.results count];
}
- (id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(NSInteger)rowIndex;
{
if (aTableView != self.resultsTable) { return nil; }
if ([[aTableColumn identifier] isEqualToString:@"filename"]) {
return [[[self.results objectAtIndex:rowIndex] file] name];
}
else if ([[aTableColumn identifier] isEqualToString:@"text"] ) {
return [[self.results objectAtIndex:rowIndex] resultString];
}
return nil;
}
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification; {
if (self.resultsTable == [aNotification object]) {
NSInteger intSelectedRow = [self.resultsTable selectedRow];
if (-1 == intSelectedRow) {
[self.replace setEnabled: NO];
}
else {
[self.replace setEnabled: YES];
}
}
}
@end