Permalink
Browse files

Merge branch 'kod-helper-wait-param' of https://github.com/lucsky/kod

…into lucsky-kod-helper-wait-param
  • Loading branch information...
2 parents fea5747 + 5ffe062 commit 7fc8fcc07cf409c18e7d7395130a911b3f180e6e @rsms committed Jan 19, 2011
Showing with 107 additions and 15 deletions.
  1. +3 −0 src/KDocument.h
  2. +7 −1 src/KDocument.mm
  3. +12 −0 src/KDocumentController.h
  4. +38 −0 src/KDocumentController.mm
  5. +15 −6 src/KMachService.mm
  6. +5 −2 src/KMachServiceProtocol.h
  7. +1 −0 src/kod_helper.h
  8. +26 −6 src/kod_helper.m
View
3 src/KDocument.h
@@ -43,6 +43,8 @@ extern NSString *const KDocumentDidLoadDataNotification;
// Internal state
hatomic_flags_t stateFlags_;
+
+ NSInvocation *closeCallback_;
}
@property(assign, nonatomic) BOOL isDirty;
@@ -65,6 +67,7 @@ extern NSString *const KDocumentDidLoadDataNotification;
// during a session (between starting and terminating Kod.app).
@property(readonly) uint64_t identifier;
+@property(retain) NSInvocation *closeCallback;
/*!
* Monotonically incrementing version number which changes for each edit.
View
8 src/KDocument.mm
@@ -26,6 +26,7 @@
#import "node-module/ASTNodeWrapper.h"
#import "KASTViewerWindowController.h"
#import "KASTViewerController.h"
+#import "KMachService-NSInvocation.h"
#import "NSImage-kod.h"
#import "CIImage-kod.h"
@@ -88,7 +89,8 @@ @implementation KDocument
@synthesize textEncoding = textEncoding_,
textView = textView_,
- ast = ast_;
+ ast = ast_,
+ closeCallback = closeCallback_;
static NSImage* _kDefaultIcon = nil;
static NSString* _kDefaultTitle = @"Untitled";
@@ -174,6 +176,8 @@ - (id)initWithBaseTabContents:(CTTabContents*)baseContents {
// register as text storage delegate
textView_.textStorage.delegate = self;
+
+ closeCallback_ = nil;
return self;
}
@@ -560,6 +564,8 @@ - (void)tabWillCloseInBrowser:(CTBrowser*)browser atIndex:(NSInteger)index {
}
[[NSDocumentController sharedDocumentController] removeDocument:self];
[[NSNotificationCenter defaultCenter] removeObserver:self];
+
+ [closeCallback_ invokeKMachServiceCallbackWithArgument:nil];
}
View
12 src/KDocumentController.h
@@ -48,13 +48,25 @@ withWindowController:(KBrowserWindowController*)windowController
withWindowController:(KBrowserWindowController*)windowController
priority:(long)priority
nonExistingFilesAsNewDocuments:(BOOL)newDocForNewURLs
+ closeCallbacks:(NSArray*)closeCallbacks
callback:(void(^)(NSError*))callback;
+- (void)openDocumentsWithContentsOfURLs:(NSArray*)urls
+ withWindowController:(KBrowserWindowController*)windowController
+ priority:(long)priority
+ nonExistingFilesAsNewDocuments:(BOOL)newDocForNewURLs
+ callback:(void(^)(NSError*))callback;
+
// Open |urls| in frontmost window with high priority
- (void)openDocumentsWithContentsOfURLs:(NSArray*)urls
nonExistingFilesAsNewDocuments:(BOOL)newDocForNewURLs
+ closeCallbacks:(NSArray*)closeCallbacks
callback:(void(^)(NSError*))callback;
+- (void)openDocumentsWithContentsOfURLs:(NSArray*)urls
+ nonExistingFilesAsNewDocuments:(BOOL)newDocForNewURLs
+ callback:(void(^)(NSError*))callback;
+
// Open |urls| in frontmost window with high priority
- (void)openDocumentsWithContentsOfURLs:(NSArray*)urls
callback:(void(^)(NSError*))callback;
View
38 src/KDocumentController.mm
@@ -10,6 +10,7 @@
#import "KFileURLHandler.h"
#import "KHTTPURLHandler.h"
#import "KKodURLHandler.h"
+#import "KMachService.h"
#import "HEventEmitter.h"
#import "kconf.h"
#import "kod_node_interface.h"
@@ -102,6 +103,21 @@ - (void)openDocumentsWithContentsOfURLs:(NSArray*)urls
priority:(long)priority
nonExistingFilesAsNewDocuments:(BOOL)newDocForNewURLs
callback:(void(^)(NSError*))callback {
+ [self openDocumentsWithContentsOfURLs:urls
+ withWindowController:windowController
+ priority:priority
+ nonExistingFilesAsNewDocuments:newDocForNewURLs
+ closeCallbacks:nil
+ callback:callback];
+}
+
+
+- (void)openDocumentsWithContentsOfURLs:(NSArray*)urls
+ withWindowController:(KBrowserWindowController*)windowController
+ priority:(long)priority
+ nonExistingFilesAsNewDocuments:(BOOL)newDocForNewURLs
+ closeCallbacks:(NSArray*)closeCallbacks
+ callback:(void(^)(NSError*))callback {
DLOG("openDocumentsWithContentsOfURLs:%@", urls);
// countdown
NSUInteger i = urls ? urls.count : 0;
@@ -175,6 +191,11 @@ - (void)openDocumentsWithContentsOfURLs:(NSArray*)urls
if (doc) doc.url = url;
if (countdown) countdown(error);
if (!doc) [windowController presentError:error];
+ else if (closeCallbacks != nil) {
+ NSInvocation *closeCallback =
+ [closeCallbacks objectAtIndex:[urls indexOfObject:url]];
+ doc.closeCallback = closeCallback;
+ }
} else {
url = [url copy];
dispatch_async(dispatchQueue, ^{
@@ -185,6 +206,11 @@ - (void)openDocumentsWithContentsOfURLs:(NSArray*)urls
groupWithSiblings:YES
/* display last document opened: */ display:index==0
error:&error];
+ if (doc && closeCallbacks != nil) {
+ NSInvocation *closeCallback =
+ [closeCallbacks objectAtIndex:[urls indexOfObject:url]];
+ doc.closeCallback = closeCallback;
+ }
if (doc && doc.isLoading) {
if (countdown) {
[doc on:@"load" call:^(KDocument *doc2){
@@ -206,13 +232,25 @@ - (void)openDocumentsWithContentsOfURLs:(NSArray*)urls
- (void)openDocumentsWithContentsOfURLs:(NSArray*)urls
nonExistingFilesAsNewDocuments:(BOOL)newDocForNewURLs
callback:(void(^)(NSError*))callback {
+ [self openDocumentsWithContentsOfURLs:urls
+ nonExistingFilesAsNewDocuments:newDocForNewURLs
+ closeCallbacks:nil
+ callback:callback];
+
+}
+
+- (void)openDocumentsWithContentsOfURLs:(NSArray*)urls
+ nonExistingFilesAsNewDocuments:(BOOL)newDocForNewURLs
+ closeCallbacks:(NSArray*)closeCallbacks
+ callback:(void(^)(NSError*))callback {
// open the documents in the frontmost window controller
KBrowserWindowController *windowController = (KBrowserWindowController *)
[KBrowserWindowController mainBrowserWindowController];
[self openDocumentsWithContentsOfURLs:urls
withWindowController:windowController
priority:DISPATCH_QUEUE_PRIORITY_HIGH
nonExistingFilesAsNewDocuments:(BOOL)newDocForNewURLs
+ closeCallbacks:closeCallbacks
callback:callback];
}
View
21 src/KMachService.mm
@@ -61,7 +61,14 @@ - (BOOL)connection:(NSConnection *)parentConnection
}
-- (void)openURLs:(NSArray*)urlStrings callback:(NSInvocation*)callback {
+- (void)openURLs:(NSArray*)urlStrings closeCallbacks:(NSArray *)closeCallbacks
+ errorCallback:(NSInvocation *)errorCallback {
+ if ((closeCallbacks != nil) && (urlStrings.count != closeCallbacks.count)) {
+ NSError *err = [NSError kodErrorWithFormat:@"URLs array items count"
+ " does not match the close callbacks count"];
+ [errorCallback invokeKMachServiceCallbackWithArgument:err];
+ return;
+ }
NSMutableArray *absoluteURLs =
[NSMutableArray arrayWithCapacity:[urlStrings count]];
for (NSString *url in urlStrings) {
@@ -76,8 +83,9 @@ - (void)openURLs:(NSArray*)urlStrings callback:(NSInvocation*)callback {
kassert(documentController != nil);
[documentController openDocumentsWithContentsOfURLs:absoluteURLs
nonExistingFilesAsNewDocuments:YES
- callback:^(NSError *err){
- [callback invokeKMachServiceCallbackWithArgument:err];
+ closeCallbacks:closeCallbacks
+ callback:^(NSError *err) {
+ [errorCallback invokeKMachServiceCallbackWithArgument:err];
}];
}
@@ -96,7 +104,8 @@ - (void)openURLs:(NSArray*)urlStrings callback:(NSInvocation*)callback {
- (void)openNewDocumentWithData:(NSData*)data
ofType:(NSString*)typeName
- callback:(NSInvocation*)callback {
+ closeCallback:(NSInvocation*)closeCallback
+ errorCallback:(NSInvocation*)errorCallback {
DLOG("%@ openNewDocumentWithData", self);
KDocumentController *documentController = [KDocumentController kodController];
@@ -118,8 +127,8 @@ - (void)openNewDocumentWithData:(NSData*)data
}
// invoke callback
- if (callback)
- [callback invokeKMachServiceCallbackWithArgument:error];
+ if (errorCallback)
+ [errorCallback invokeKMachServiceCallbackWithArgument:error];
/*
TODO(rsms): Pass the file descriptor by kernel FD delegation using sendmsg.
View
7 src/KMachServiceProtocol.h
@@ -6,10 +6,13 @@
@protocol KMachServiceProtocol
-- (void)openURLs:(NSArray*)absoluteURLStrings callback:(NSInvocation*)callback;
+- (void)openURLs:(NSArray*)absoluteURLStrings
+ closeCallbacks:(NSArray*)closeCallbacks
+ errorCallback:(NSInvocation*)errorCallback;
- (void)openNewDocumentWithData:(NSData*)data
ofType:(NSString*)typeName
- callback:(NSInvocation*)callback;
+ closeCallback:(NSInvocation*)callback
+ errorCallback:(NSInvocation*)callback;
@end
View
1 src/kod_helper.h
@@ -14,6 +14,7 @@
NSMutableDictionary *asyncWaitQueue_;
BOOL forceReadStdin_; // true if a "-" is passed in argv
int optNoWaitOpen_; // 1 if -n or --nowait-open
+ int optWaitForDocumentClose; // 1 if -w or --wait
}
@property(readonly) NSRunningApplication *kodApp;
View
32 src/kod_helper.m
@@ -63,12 +63,13 @@ - (void)parseOptionsOfLength:(int)argc argv:(char**)argv {
*/
struct option long_options[] = {
{"nowait-open", no_argument, &optNoWaitOpen_, 1},
+ {"wait", no_argument, &optWaitForDocumentClose, 1},
{"kod-app", required_argument, 0, 0},
{"version", no_argument, 0, 0},
{"help", no_argument, 0, 0},
{0, 0, 0, 0}
};
- static const char *short_options = "nh";
+ static const char *short_options = "nhw";
int c;
while (1) {
int option_index = 0;
@@ -92,6 +93,9 @@ - (void)parseOptionsOfLength:(int)argc argv:(char**)argv {
case 'n':
optNoWaitOpen_ = 1;
break;
+ case 'w':
+ optWaitForDocumentClose = 1;
+ break;
case '?':
[self printUsageAndExit:1];
break;
@@ -260,8 +264,12 @@ - (void)openNewDocumentWithStdin {
DLOG("reading stdin [%d] until EOF...", [fh fileDescriptor]);
NSData *data = [fh readDataToEndOfFile];
- [kodService_ openNewDocumentWithData:data ofType:nil callback:
- [self registerCallback:^(NSError *err){
+ [kodService_ openNewDocumentWithData:data
+ ofType:nil
+ closeCallback:[self registerCallback:^(void) {
+ DLOG("close callback for piped document executed.");
+ }]
+ errorCallback:[self registerCallback:^(NSError *err) {
DLOG("openNewDocumentWithData callback executed. err: %@", err);
}]];
}
@@ -270,10 +278,22 @@ - (void)openNewDocumentWithStdin {
- (void)openAnyURLs {
if (!URLsToOpen_ || URLsToOpen_.count == 0) return;
DLOG("invoking openURLs:");
- [kodService_ openURLs:URLsToOpen_ callback:[self registerCallback:
- ^(NSError *err){
+ NSInvocation *errorCallback = [self registerCallback:^(NSError *err) {
DLOG("openAnyURLs callback executed. err: %@", err);
- }]];
+ }];
+ NSMutableArray *closeCallbacks = nil;
+ if (optWaitForDocumentClose) {
+ closeCallbacks = [NSMutableArray arrayWithCapacity:[URLsToOpen_ count]];
+ for (NSString *url in URLsToOpen_) {
+ NSInvocation *closeCallback = nil;
+ closeCallback = [self registerCallback:^{
+ DLOG("document close callback executed.");
+ }];
+ [closeCallbacks addObject:closeCallback];
+ }
+ }
+ [kodService_ openURLs:URLsToOpen_ closeCallbacks:closeCallbacks
+ errorCallback:errorCallback];
}

0 comments on commit 7fc8fcc

Please sign in to comment.