diff options
Diffstat (limited to 'Source/CMTextView.m')
-rw-r--r-- | Source/CMTextView.m | 55 |
1 files changed, 49 insertions, 6 deletions
diff --git a/Source/CMTextView.m b/Source/CMTextView.m index 427c66d1..62f84629 100644 --- a/Source/CMTextView.m +++ b/Source/CMTextView.m @@ -41,10 +41,59 @@ YY_BUFFER_STATE yy_scan_string (const char *); #define kSQLkeyword @"SQLkw" // attribute for found SQL keywords #define kQuote @"Quote" +#define MYSQL_DOC_SEARCH_URL @"http://search.mysql.com/search?q=%@" @implementation CMTextView /* + * Add a menu item to context menu for looking up mysql documentation. + */ +- (NSMenu *)menuForEvent:(NSEvent *)event +{ + // Set title of the menu item + lookupInDocumentationTitle = NSLocalizedString(@"Lookup In MySQL Documentation", @"Lookup In MySQL Documentation"); + + // Add the menu item if it doesn't yet exist + NSMenu *menu = [[self class] defaultMenu]; + + if ([[[self class] defaultMenu] itemWithTitle:lookupInDocumentationTitle] == nil) { + + [menu insertItem:[NSMenuItem separatorItem] atIndex:3]; + [menu insertItemWithTitle:lookupInDocumentationTitle action:@selector(lookupSelectionInDocumentation) keyEquivalent:@"" atIndex:4]; + } + + return menu; +} + +/* + * Open a search for the current selection on mysql.com + */ +- (void)lookupSelectionInDocumentation +{ + // Get the current selection and encode it to be used in a URL + NSString *keyword = [[[[self string] substringWithRange:[self selectedRange]] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; + + // Open MySQL Documentation search in browser using the terms + NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:MYSQL_DOC_SEARCH_URL, keyword]]; + + [[NSWorkspace sharedWorkspace] openURL:url]; +} + +/* + * Disable the lookup in documentation function when there is no selection. + */ +- (BOOL)validateMenuItem:(NSMenuItem *)menuItem +{ + // Enable or disable the lookup in documentation menu item depending on whether there is a + // selection and whether it is a reasonable length. + if ([menuItem action] == @selector(lookupSelectionInDocumentation)) { + return (([self selectedRange].length) || ([self selectedRange].length > 256)); + } + + return YES; +} + +/* * Checks if the char after the current caret position/selection matches a supplied attribute */ - (BOOL) isNextCharMarkedBy:(id)attribute @@ -61,7 +110,6 @@ YY_BUFFER_STATE yy_scan_string (const char *); return NO; } - /* * Checks if the caret is wrapped by auto-paired characters. * e.g. [| := caret]: "|" @@ -94,7 +142,6 @@ YY_BUFFER_STATE yy_scan_string (const char *); return NO; } - /* * If the textview has a selection, wrap it with the supplied prefix and suffix strings; * return whether or not any wrap was performed. @@ -133,10 +180,8 @@ YY_BUFFER_STATE yy_scan_string (const char *); [pb declareTypes:[NSArray arrayWithObject:NSRTFPboardType] owner:self]; [pb setData:rtf forType:NSRTFPboardType]; } - } - /* * Handle some keyDown events in order to provide autopairing functionality (if enabled). */ @@ -1260,7 +1305,6 @@ YY_BUFFER_STATE yy_scan_string (const char *); return autouppercaseKeywordsEnabled; } - /******************* SYNTAX HIGHLIGHTING! *******************/ @@ -1411,7 +1455,6 @@ SYNTAX HIGHLIGHTING! value: kWQval range: tokenRange ]; } - } @end |