From b469d9629f68b1c20541994863fedd93be2b046d Mon Sep 17 00:00:00 2001 From: Bjorn Winckler Date: Sat, 28 Nov 2009 19:10:03 +0100 Subject: [PATCH] Improve Find pasteboard interaction Put two representation of the search pattern on the Find pasteboard: one that holds the unmodified pattern, and a second one that removes some common backslash escapes from the first. The second pattern will be used by other applications so e.g. hitting * in MacVim and then Cmd+g in another app now works. --- src/MacVim/MMWindowController.m | 11 +++++++++-- src/MacVim/MacVim.h | 4 +++- src/MacVim/MacVim.m | 34 +++++++++++++++++++++++++++++++++- src/MacVim/gui_macvim.m | 23 ++++++++++++++++------- 4 files changed, 61 insertions(+), 11 deletions(-) diff --git a/src/MacVim/MMWindowController.m b/src/MacVim/MMWindowController.m index 11b277a2..e63e77c1 100644 --- a/src/MacVim/MMWindowController.m +++ b/src/MacVim/MMWindowController.m @@ -1039,8 +1039,15 @@ if (!query) { // Use find pasteboard for next query. NSPasteboard *pb = [NSPasteboard pasteboardWithName:NSFindPboard]; - NSArray *types = [NSArray arrayWithObject:NSStringPboardType]; - if ([pb availableTypeFromArray:types]) + NSArray *supportedTypes = [NSArray arrayWithObjects:VimFindPboardType, + NSStringPboardType, nil]; + NSString *bestType = [pb availableTypeFromArray:supportedTypes]; + + // See gui_macvim_add_to_find_pboard() for an explanation of these + // types. + if ([bestType isEqual:VimFindPboardType]) + query = [pb stringForType:VimFindPboardType]; + else query = [pb stringForType:NSStringPboardType]; } diff --git a/src/MacVim/MacVim.h b/src/MacVim/MacVim.h index 64487190..b6fb0822 100644 --- a/src/MacVim/MacVim.h +++ b/src/MacVim/MacVim.h @@ -257,13 +257,15 @@ enum { // Vim pasteboard type (holds motion type + string) -extern NSString *VimPBoardType; +extern NSString *VimPboardType; +extern NSString *VimFindPboardType; @interface NSString (MMExtras) - (NSString *)stringByEscapingSpecialFilenameCharacters; +- (NSString *)stringByRemovingFindPatterns; @end diff --git a/src/MacVim/MacVim.m b/src/MacVim/MacVim.m index 6adbd558..06748c63 100644 --- a/src/MacVim/MacVim.m +++ b/src/MacVim/MacVim.m @@ -111,7 +111,9 @@ NSString *MMAutosaveColumnsKey = @"MMAutosaveColumns"; NSString *MMRendererKey = @"MMRenderer"; // Vim pasteboard type (holds motion type + string) -NSString *VimPBoardType = @"VimPBoardType"; +NSString *VimPboardType = @"VimPboardType"; +// Vim find pasteboard type (string contains Vim regex patterns) +NSString *VimFindPboardType = @"VimFindPboardType"; int ASLogLevel = ASL_LEVEL_NOTICE; @@ -198,6 +200,36 @@ debugStringForMessageQueue(NSArray *queue) return [string autorelease]; } +- (NSString *)stringByRemovingFindPatterns +{ + // Remove some common patterns added to search strings that other apps are + // not aware of. + + NSMutableString *string = [self mutableCopy]; + + // Added when doing * search + [string replaceOccurrencesOfString:@"\\<" + withString:@"" + options:NSLiteralSearch + range:NSMakeRange(0, [string length])]; + [string replaceOccurrencesOfString:@"\\>" + withString:@"" + options:NSLiteralSearch + range:NSMakeRange(0, [string length])]; + // \V = match whole word + [string replaceOccurrencesOfString:@"\\V" + withString:@"" + options:NSLiteralSearch + range:NSMakeRange(0, [string length])]; + // \c = case insensitive, \C = case sensitive + [string replaceOccurrencesOfString:@"\\c" + withString:@"" + options:NSCaseInsensitiveSearch|NSLiteralSearch + range:NSMakeRange(0, [string length])]; + + return [string autorelease]; +} + @end // NSString (MMExtras) diff --git a/src/MacVim/gui_macvim.m b/src/MacVim/gui_macvim.m index 07e9a1e1..edf03e48 100644 --- a/src/MacVim/gui_macvim.m +++ b/src/MacVim/gui_macvim.m @@ -611,7 +611,7 @@ clip_mch_own_selection(VimClipboard *cbd) clip_mch_request_selection(VimClipboard *cbd) { NSPasteboard *pb = [NSPasteboard generalPasteboard]; - NSArray *supportedTypes = [NSArray arrayWithObjects:VimPBoardType, + NSArray *supportedTypes = [NSArray arrayWithObjects:VimPboardType, NSStringPboardType, nil]; NSString *bestType = [pb availableTypeFromArray:supportedTypes]; if (!bestType) return; @@ -619,12 +619,12 @@ clip_mch_request_selection(VimClipboard *cbd) int motion_type = MCHAR; NSString *string = nil; - if ([bestType isEqual:VimPBoardType]) { + if ([bestType isEqual:VimPboardType]) { // This type should consist of an array with two objects: // 1. motion type (NSNumber) // 2. text (NSString) // If this is not the case we fall back on using NSStringPboardType. - id plist = [pb propertyListForType:VimPBoardType]; + id plist = [pb propertyListForType:VimPboardType]; if ([plist isKindOfClass:[NSArray class]] && [plist count] == 2) { id obj = [plist objectAtIndex:1]; if ([obj isKindOfClass:[NSString class]]) { @@ -718,13 +718,13 @@ clip_mch_set_selection(VimClipboard *cbd) // See clip_mch_request_selection() for info on pasteboard types. NSPasteboard *pb = [NSPasteboard generalPasteboard]; - NSArray *supportedTypes = [NSArray arrayWithObjects:VimPBoardType, + NSArray *supportedTypes = [NSArray arrayWithObjects:VimPboardType, NSStringPboardType, nil]; [pb declareTypes:supportedTypes owner:nil]; NSNumber *motion = [NSNumber numberWithInt:motion_type]; NSArray *plist = [NSArray arrayWithObjects:motion, string, nil]; - [pb setPropertyList:plist forType:VimPBoardType]; + [pb setPropertyList:plist forType:VimPboardType]; [pb setString:string forType:NSStringPboardType]; @@ -1818,8 +1818,17 @@ gui_macvim_add_to_find_pboard(char_u *pat) if (!s) return; NSPasteboard *pb = [NSPasteboard pasteboardWithName:NSFindPboard]; - [pb declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil]; - [pb setString:s forType:NSStringPboardType]; + NSArray *supportedTypes = [NSArray arrayWithObjects:VimFindPboardType, + NSStringPboardType, nil]; + [pb declareTypes:supportedTypes owner:nil]; + + // Put two entries on the Find pasteboard: + // * the pattern Vim uses + // * same as above but with some backslash escaped characters removed + // The second entry will be used by other applications when taking entries + // off the Find pasteboard, whereas MacVim will use the first if present. + [pb setString:s forType:VimFindPboardType]; + [pb setString:[s stringByRemovingFindPatterns] forType:NSStringPboardType]; } void -- 2.11.4.GIT