Created
February 17, 2011 16:37
-
-
Save masakih/832061 to your computer and use it in GitHub Desktop.
BathyScaphで次すれ候補を検索出来るかもしれないImagePreviewerプラグイン
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // NextThreadFinder.h | |
| // NextThreadFinder | |
| // | |
| // Created by Hori,Masaki on 11/02/17. | |
| // Copyright 2011 masakih. All rights reserved. | |
| // | |
| #import <Cocoa/Cocoa.h> | |
| #import "BSImagePreviewerInterface.h" | |
| #import "BSPreviewPluginInterface.h" | |
| @interface NextThreadFinder : NSResponder <BSImagePreviewerProtocol, BSLinkPreviewing> | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // NextThreadFinder.m | |
| // NextThreadFinder | |
| // | |
| // Created by Hori,Masaki on 11/02/17. | |
| // Copyright 2011 masakih. All rights reserved. | |
| // | |
| #import "NextThreadFinder.h" | |
| @interface NSObject(BathyScapheAPIs) | |
| - (id)threadAttributes; | |
| - (NSString *)threadTitle; | |
| - (NSString *)boardName; | |
| - (void)setSearchString:(NSString *)string; | |
| // CMRBrowser | |
| - (void)synchronizeWithSearchField; | |
| - (void)showThreadsListWithBoardName:(NSString *)boardName; | |
| - (void)selectRowOfName:(NSString *)boardName forceReload:(BOOL)flag; | |
| @end | |
| @interface NextThreadFinder (HMPrivate) | |
| const NSInteger kScriptMenuTag = 8; | |
| - (void)setupMenu; | |
| @end | |
| @implementation NextThreadFinder | |
| - (id)initWithPreferences:(AppDefaults *)prefs | |
| { | |
| self = [super init]; | |
| [self setupMenu]; | |
| [NSApp setNextResponder:self]; | |
| return self; | |
| } | |
| - (BOOL)previewLink:(NSURL *)url { return NO; } | |
| - (BOOL)validateLink:(NSURL *)url { return NO; } | |
| - (void)setPreferences:(AppDefaults *)aPreferences {} | |
| - (AppDefaults *)preferences { return nil; } | |
| - (BOOL)showImageWithURL:(NSURL *)imageURL { return NO; } | |
| - (void)setupMenu | |
| { | |
| NSMenuItem *tabMenuItem; | |
| NSMenu *mainMenu; | |
| int scriptMenuIndex; | |
| NSString *itemTitle = NSLocalizedStringFromTableInBundle(@"Search Next Thread", | |
| nil, | |
| [NSBundle bundleForClass:[self class]], | |
| @"Menu title"); | |
| findMenuItem = [[[NSMenuItem alloc] initWithTitle:itemTitle | |
| action:@selector(findNextThread:) | |
| keyEquivalent:@""] autorelease]; | |
| mainMenu = [NSApp mainMenu]; | |
| scriptMenuIndex = [mainMenu indexOfItemWithTag:kScriptMenuTag]; | |
| NSMenuItem *windowMenuItem = [mainMenu itemAtIndex:scriptMenuIndex - 1]; | |
| NSMenu *windowMenu = [windowMenuItem submenu]; | |
| NSInteger separatorNum = 0; | |
| for(NSMenuItem *item in [windowMenu itemArray]) { | |
| if([item isSeparatorItem]) { | |
| separatorNum++; | |
| continue; | |
| } | |
| if(separatorNum == 2) { | |
| NSUInteger index = [windowMenu indexOfItem:item]; | |
| [windowMenu insertItem:findMenuItem atIndex:index + 1]; | |
| break; | |
| } | |
| } | |
| } | |
| - (BOOL)validateMenuItem:(NSMenuItem *)menuItem | |
| { | |
| SEL action = [menuItem action]; | |
| if(action != @selector(findNextThread:)) return NO; | |
| if([[[[NSDocumentController sharedDocumentController] currentDocument] threadAttributes] threadTitle]) return YES; | |
| return NO; | |
| } | |
| - (id)findCMRBrowser | |
| { | |
| for(NSWindow *window in [NSApp windows]) { | |
| NSWindowController *wc = [window windowController]; | |
| if([wc isMemberOfClass:NSClassFromString(@"CMRBrowser")]) { | |
| return wc; | |
| } | |
| } | |
| return nil; | |
| } | |
| - (NSArray *)tokensForTitle:(NSString *)title | |
| { | |
| NSMutableArray *tokens = [NSMutableArray array]; | |
| CFLocaleRef locale = CFLocaleCreate(kCFAllocatorDefault, (CFStringRef)@"ja"); | |
| CFStringTokenizerRef tokenizer = CFStringTokenizerCreate(kCFAllocatorDefault, | |
| (CFStringRef)title, | |
| CFRangeMake(0, [title length]), | |
| kCFStringTokenizerUnitWordBoundary, | |
| locale); | |
| while(CFStringTokenizerAdvanceToNextToken(tokenizer) != kCFStringTokenizerTokenNone) { | |
| CFRange range = CFStringTokenizerGetCurrentTokenRange(tokenizer); | |
| if(range.location != kCFNotFound) { | |
| NSString *token = [title substringWithRange:NSMakeRange(range.location, range.length)]; | |
| [tokens addObject:token]; | |
| } | |
| } | |
| return tokens; | |
| } | |
| - (NSString *)searchStringWithTitle:(NSString *)title | |
| { | |
| NSArray *tokens = [self tokensForTitle:title]; | |
| NSCharacterSet *numSet = [NSCharacterSet decimalDigitCharacterSet]; | |
| NSMutableArray *array = [NSMutableArray array]; | |
| for(NSString *token in tokens) { | |
| if([token length] < 3) continue; | |
| if([numSet characterIsMember:[token characterAtIndex:0]]) continue; | |
| if([numSet characterIsMember:[token characterAtIndex:[token length] - 1]]) continue; | |
| [array addObject:token]; | |
| } | |
| return [array componentsJoinedByString:@" "]; | |
| } | |
| - (IBAction)findNextThread:(id)sender | |
| { | |
| NSDocumentController *dc = [NSDocumentController sharedDocumentController]; | |
| id cmrBrowser = [self findCMRBrowser]; | |
| id browser = [cmrBrowser document]; | |
| id threadAttributes = [[dc currentDocument] threadAttributes]; | |
| [cmrBrowser showThreadsListWithBoardName:[threadAttributes boardName]]; | |
| [cmrBrowser selectRowOfName:[threadAttributes boardName] forceReload:NO]; | |
| [browser setSearchString:[self searchStringWithTitle:[threadAttributes threadTitle]]]; | |
| [cmrBrowser synchronizeWithSearchField]; | |
| [cmrBrowser showWindow:nil]; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment