From e8f4c4a9e16b0747647cc914c13d7597800b108d Mon Sep 17 00:00:00 2001 From: Bibiko Date: Sun, 12 Dec 2010 13:32:48 +0000 Subject: =?UTF-8?q?=E2=80=A2=20HTML=20output=20window's=20JavaScript=20sup?= =?UTF-8?q?port=20-=20changed=20the=20approach=20to=20make=20the=20HTML=20?= =?UTF-8?q?output=20window=20the=20key=20window;=20now=20it=20could=20be?= =?UTF-8?q?=20done=20directly=20via=20a=20JavaScript=20command:=20window.s?= =?UTF-8?q?ystem.makeHTMLOutputWindowKeyWindow()=20-=20added=20the=20chanc?= =?UTF-8?q?e=20to=20ask=20Sequel=20Pro=20via=20JavaScript=20for=20run-time?= =?UTF-8?q?=20shell=20variables=20via=20window.system.getShellEnvironmentF?= =?UTF-8?q?orName('shell=5Fvar=5Fname')=20=E2=80=A2=20more=20usage=20of=20?= =?UTF-8?q?constants?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/SPAppController.h | 1 + Source/SPAppController.m | 64 +++++++++++++++++++++++++++++++++++ Source/SPBundleEditorController.m | 1 + Source/SPBundleHTMLOutputController.m | 46 +++++++++++++++++++++---- Source/SPConstants.h | 5 +++ Source/SPConstants.m | 5 +++ Source/SPCopyTable.m | 2 +- Source/SPTextViewAdditions.m | 8 ++--- 8 files changed, 120 insertions(+), 12 deletions(-) diff --git a/Source/SPAppController.h b/Source/SPAppController.h index 938a6423..0d68b26c 100644 --- a/Source/SPAppController.h +++ b/Source/SPAppController.h @@ -113,6 +113,7 @@ - (void)handleEventWithURL:(NSURL*)url; - (IBAction)executeBundleItemForApp:(id)sender; +- (NSDictionary*)shellEnvironment; - (void)addHTMLOutputController:(id)controller; diff --git a/Source/SPAppController.m b/Source/SPAppController.m index aa587aba..a002f5cf 100644 --- a/Source/SPAppController.m +++ b/Source/SPAppController.m @@ -37,6 +37,7 @@ #import "SPBundleHTMLOutputController.h" #import "SPAlertSheets.h" #import "SPChooseMenuItemDialog.h" +#import "SPCustomQuery.h" #import #import @@ -884,6 +885,69 @@ } +/** + * Return of certain shell variables mainly for usage in JavaScript support inside the + * HTML output window to allow to ask on run-time + */ +- (NSDictionary*)shellEnvironment +{ + NSMutableDictionary *env = [NSMutableDictionary dictionary]; + SPDatabaseDocument *doc = [self frontDocument]; + if(doc) [env addEntriesFromDictionary:[doc shellVariables]]; + id firstResponder = [[NSApp keyWindow] firstResponder]; + if([firstResponder respondsToSelector:@selector(executeBundleItemForInputField:)]) { + BOOL selfIsQueryEditor = ([[[firstResponder class] description] isEqualToString:@"SPTextView"]) ; + NSRange currentWordRange, currentSelectionRange, currentLineRange, currentQueryRange; + currentSelectionRange = [firstResponder selectedRange]; + currentWordRange = [firstResponder getRangeForCurrentWord]; + currentLineRange = [[firstResponder string] lineRangeForRange:NSMakeRange([firstResponder selectedRange].location, 0)]; + + if(selfIsQueryEditor) { + currentQueryRange = [[firstResponder delegate] currentQueryRange]; + } else { + currentQueryRange = currentLineRange; + } + if(!currentQueryRange.length) + currentQueryRange = currentSelectionRange; + + [env setObject:SPBundleScopeInputField forKey:SPBundleShellVariableScope]; + + if(selfIsQueryEditor && [[firstResponder delegate] currentQueryRange].length) + [env setObject:[[firstResponder string] substringWithRange:[[firstResponder delegate] currentQueryRange]] forKey:SPBundleShellVariableCurrentQuery]; + + if(currentSelectionRange.length) + [env setObject:[[firstResponder string] substringWithRange:currentSelectionRange] forKey:SPBundleShellVariableSelectedText]; + + if(currentWordRange.length) + [env setObject:[[firstResponder string] substringWithRange:currentWordRange] forKey:SPBundleShellVariableCurrentWord]; + + if(currentLineRange.length) + [env setObject:[[firstResponder string] substringWithRange:currentLineRange] forKey:SPBundleShellVariableCurrentLine]; + } + else if([firstResponder respondsToSelector:@selector(executeBundleItemForDataTable:)]) { + + if([[firstResponder delegate] respondsToSelector:@selector(usedQuery)] && [[firstResponder delegate] usedQuery]) + [env setObject:[[firstResponder delegate] usedQuery] forKey:SPBundleShellVariableUsedQueryForTable]; + + if([firstResponder numberOfSelectedRows]) { + NSMutableArray *sel = [NSMutableArray array]; + NSIndexSet *selectedRows = [firstResponder selectedRowIndexes]; + NSUInteger rowIndex = [selectedRows firstIndex]; + while ( rowIndex != NSNotFound ) { + [sel addObject:[NSString stringWithFormat:@"%ld", rowIndex]]; + rowIndex = [selectedRows indexGreaterThanIndex:rowIndex]; + } + [env setObject:[sel componentsJoinedByString:@"\t"] forKey:SPBundleShellVariableSelectedRowIndices]; + } + + [env setObject:SPBundleScopeDataTable forKey:SPBundleShellVariableScope]; + + } else { + [env setObject:SPBundleScopeGeneral forKey:SPBundleShellVariableScope]; + } + return env; +} + - (void)registerActivity:(NSDictionary*)commandDict { [runningActivitiesArray addObject:commandDict]; diff --git a/Source/SPBundleEditorController.m b/Source/SPBundleEditorController.m index 272221d4..034200b1 100644 --- a/Source/SPBundleEditorController.m +++ b/Source/SPBundleEditorController.m @@ -344,6 +344,7 @@ @"SP_SELECTED_ROW_INDICES", @"SP_SELECTED_TABLE", @"SP_SELECTED_TABLES", + @"SP_SELECTED_TEXT", @"SP_USED_QUERY_FOR_TABLE", nil ] retain]; diff --git a/Source/SPBundleHTMLOutputController.m b/Source/SPBundleHTMLOutputController.m index 365c15a3..1e002ab8 100644 --- a/Source/SPBundleHTMLOutputController.m +++ b/Source/SPBundleHTMLOutputController.m @@ -328,6 +328,10 @@ { if (aSelector == @selector(run:)) return @"run"; + if (aSelector == @selector(getShellEnvironmentForName:)) + return @"getShellEnvironmentForName"; + if (aSelector == @selector(makeHTMLOutputWindowKeyWindow)) + return @"makeHTMLOutputWindowKeyWindow"; return @""; } @@ -335,6 +339,12 @@ if (selector == @selector(run:)) { return NO; } + if (selector == @selector(getShellEnvironmentForName:)) { + return NO; + } + if (selector == @selector(makeHTMLOutputWindowKeyWindow)) { + return NO; + } return YES; } @@ -342,13 +352,41 @@ if (strcmp(property, "run") == 0) { return NO; } + if (strcmp(property, "getShellEnvironmentForName") == 0) { + return NO; + } + if (strcmp(property, "makeHTMLOutputWindowKeyWindow") == 0) { + return NO; + } return YES; } -- (void) windowScriptObjectAvailable:(WebScriptObject*)webScriptObject { +- (void)windowScriptObjectAvailable:(WebScriptObject*)webScriptObject { [webScriptObject setValue:self forKey:@"system"]; } +/** + * JavaScript window.system.getShellEnvironmentForName('a_key') function to + * return the value for key keyName + */ +- (NSString *)getShellEnvironmentForName:(NSString*)keyName +{ + return [[[NSApp delegate] shellEnvironment] objectForKey:keyName]; +} + +/** + * JavaScript window.system.makeHTMLOutputWindowKeyWindow() function + * to make the HTML output window the first responder + */ +- (void)makeHTMLOutputWindowKeyWindow +{ + [[self window] makeKeyAndOrderFront:nil]; +} + +/** + * JavaScript window.system.run('a_command'|new Array('a_command', 'uuid')) function + * to return the result of the BASH command a_command + */ - (NSString *)run:(id)call { @@ -376,12 +414,6 @@ if(!command) return @"No JavaScript command found."; - // Check for internal commands passed via JavaScript - if([command isEqualToString:@"_SP_self_makeKeyWindow"]) { - [[self window] makeKeyAndOrderFront:nil]; - return @""; - } - NSString *output = nil; if(uuid == nil) output = [command runBashCommandWithEnvironment:nil atCurrentDirectoryPath:nil error:&err]; diff --git a/Source/SPConstants.h b/Source/SPConstants.h index d6a0ee5f..804a067f 100644 --- a/Source/SPConstants.h +++ b/Source/SPConstants.h @@ -507,6 +507,11 @@ extern NSString *SPBundleShellVariableCurrentEditedColumnName; extern NSString *SPBundleShellVariableSelectedTable; extern NSString *SPBundleShellVariableCurrentEditedTable; extern NSString *SPBundleShellVariableDataTableSource; +extern NSString *SPBundleShellVariableCurrentQuery; +extern NSString *SPBundleShellVariableSelectedText; +extern NSString *SPBundleShellVariableCurrentWord; +extern NSString *SPBundleShellVariableCurrentLine; +extern NSString *SPBundleShellVariableSelectedRowIndices; extern const NSInteger SPBundleRedirectActionNone; extern const NSInteger SPBundleRedirectActionReplaceSection; diff --git a/Source/SPConstants.m b/Source/SPConstants.m index 67c51b53..e6ce8771 100644 --- a/Source/SPConstants.m +++ b/Source/SPConstants.m @@ -320,6 +320,11 @@ NSString *SPBundleShellVariableCurrentEditedColumnName = @"SP_CURRENT_EDITE NSString *SPBundleShellVariableSelectedTable = @"SP_SELECTED_TABLE"; NSString *SPBundleShellVariableCurrentEditedTable = @"SP_CURRENT_EDITED_TABLE"; NSString *SPBundleShellVariableDataTableSource = @"SP_DATA_TABLE_SOURCE"; +NSString *SPBundleShellVariableCurrentQuery = @"SP_CURRENT_QUERY"; +NSString *SPBundleShellVariableSelectedText = @"SP_SELECTED_TEXT"; +NSString *SPBundleShellVariableCurrentWord = @"SP_CURRENT_WORD"; +NSString *SPBundleShellVariableCurrentLine = @"SP_CURRENT_LINE"; +NSString *SPBundleShellVariableSelectedRowIndices = @"SP_SELECTED_ROW_INDICES"; const NSInteger SPBundleRedirectActionNone = 200; const NSInteger SPBundleRedirectActionReplaceSection = 201; diff --git a/Source/SPCopyTable.m b/Source/SPCopyTable.m index 3c665693..1d833b76 100644 --- a/Source/SPCopyTable.m +++ b/Source/SPCopyTable.m @@ -1163,7 +1163,7 @@ NSInteger kBlobAsImageFile = 4; [sel addObject:[NSString stringWithFormat:@"%ld", rowIndex]]; rowIndex = [selectedRows indexGreaterThanIndex:rowIndex]; } - [env setObject:[sel componentsJoinedByString:@"\t"] forKey:@"SP_SELECTED_ROW_INDICES"]; + [env setObject:[sel componentsJoinedByString:@"\t"] forKey:SPBundleShellVariableSelectedRowIndices]; } NSError *inputFileError = nil; diff --git a/Source/SPTextViewAdditions.m b/Source/SPTextViewAdditions.m index 5dd7d06c..a37895b8 100644 --- a/Source/SPTextViewAdditions.m +++ b/Source/SPTextViewAdditions.m @@ -611,16 +611,16 @@ } if(selfIsQueryEditor && [[self delegate] currentQueryRange].length) - [env setObject:[[self string] substringWithRange:[[self delegate] currentQueryRange]] forKey:@"SP_CURRENT_QUERY"]; + [env setObject:[[self string] substringWithRange:[[self delegate] currentQueryRange]] forKey:SPBundleShellVariableCurrentQuery]; if(currentSelectionRange.length) - [env setObject:[[self string] substringWithRange:currentSelectionRange] forKey:@"SP_SELECTED_TEXT"]; + [env setObject:[[self string] substringWithRange:currentSelectionRange] forKey:SPBundleShellVariableSelectedText]; if(currentWordRange.length) - [env setObject:[[self string] substringWithRange:currentWordRange] forKey:@"SP_CURRENT_WORD"]; + [env setObject:[[self string] substringWithRange:currentWordRange] forKey:SPBundleShellVariableCurrentWord]; if(currentLineRange.length) - [env setObject:[[self string] substringWithRange:currentLineRange] forKey:@"SP_CURRENT_LINE"]; + [env setObject:[[self string] substringWithRange:currentLineRange] forKey:SPBundleShellVariableCurrentLine]; NSError *inputFileError = nil; NSString *input = [NSString stringWithString:[[self string] substringWithRange:replaceRange]]; -- cgit v1.2.3