From 2d330ed4285cb7c9bc07a71f67b50752d4f31c85 Mon Sep 17 00:00:00 2001 From: Bjorn Winckler Date: Wed, 7 Nov 2007 22:07:21 +0100 Subject: [PATCH] Changed application termination procedure On applicationShoulTerminate: check the 'documentEdited' flag of each window to decide whether any buffers were modified instead of making a DO call to each Vim process. --- src/MacVim/MMAppController.m | 63 ++++++++++++-------------------------------- src/MacVim/MMBackend.m | 25 +++++++++--------- src/MacVim/MacVim.h | 1 - 3 files changed, 30 insertions(+), 59 deletions(-) diff --git a/src/MacVim/MMAppController.m b/src/MacVim/MMAppController.m index b3896efa..e4cbf486 100644 --- a/src/MacVim/MMAppController.m +++ b/src/MacVim/MMAppController.m @@ -18,9 +18,6 @@ static NSTimeInterval MMRequestTimeout = 5; static NSTimeInterval MMReplyTimeout = 5; -// Timeout used when the app should terminate. -static NSTimeInterval MMTerminateTimeout = 3; - @interface MMAppController (MMServices) @@ -205,60 +202,34 @@ static NSTimeInterval MMTerminateTimeout = 3; - (NSApplicationTerminateReply)applicationShouldTerminate: (NSApplication *)sender { + // TODO: Follow Apple's guidelines for 'Graceful Application Termination' + // (in particular, allow user to review changes and save). int reply = NSTerminateNow; BOOL modifiedBuffers = NO; - BOOL notResponding = NO; - // Go through vim controllers, checking for modified buffers. If a process - // is not responding then note this as well. - unsigned i, count = [vimControllers count]; - for (i = 0; i < count; ++i) { - MMVimController *controller = [vimControllers objectAtIndex:i]; - id proxy = [controller backendProxy]; - NSConnection *connection = [proxy connectionForProxy]; - if (connection) { - NSTimeInterval req = [connection requestTimeout]; - NSTimeInterval rep = [connection replyTimeout]; - [connection setRequestTimeout:MMTerminateTimeout]; - [connection setReplyTimeout:MMTerminateTimeout]; - - @try { - if ([proxy checkForModifiedBuffers]) - modifiedBuffers = YES; - } - @catch (NSException *e) { - NSLog(@"WARNING: Got exception while waiting for " - "checkForModifiedBuffers: \"%@\"", e); - notResponding = YES; - } - @finally { - [connection setRequestTimeout:req]; - [connection setReplyTimeout:rep]; - if (modifiedBuffers || notResponding) - break; - } + // Go through windows, checking for modified buffers. (Each Vim process + // tells MacVim when any buffer has been modified and MacVim sets the + // 'documentEdited' flag of the window correspondingly.) + NSEnumerator *e = [[NSApp windows] objectEnumerator]; + id window; + while (window = [e nextObject]) { + if ([window isDocumentEdited]) { + modifiedBuffers = YES; + break; } } - if (modifiedBuffers || notResponding) { + if (modifiedBuffers) { NSAlert *alert = [[NSAlert alloc] init]; [alert addButtonWithTitle:@"Quit"]; [alert addButtonWithTitle:@"Cancel"]; - if (modifiedBuffers) { - [alert setMessageText:@"Quit without saving?"]; - [alert setInformativeText:@"There are modified buffers, " - "if you quit now all changes will be lost. Quit anyway?"]; - } else { - [alert setMessageText:@"Force Quit?"]; - [alert setInformativeText:@"At least one Vim process is not " - "responding, if you quit now any changes you have made " - "will be lost. Quit anyway?"]; - } + [alert setMessageText:@"Quit without saving?"]; + [alert setInformativeText:@"There are modified buffers, " + "if you quit now all changes will be lost. Quit anyway?"]; [alert setAlertStyle:NSWarningAlertStyle]; - if ([alert runModal] != NSAlertFirstButtonReturn) { + if ([alert runModal] != NSAlertFirstButtonReturn) reply = NSTerminateCancel; - } [alert release]; } @@ -273,7 +244,7 @@ static NSTimeInterval MMTerminateTimeout = 3; [[NSConnection defaultConnection] invalidate]; // Send a SIGINT to all running Vim processes, so that they are sure to - // receive the connectionDidDie: notification (a process has to checking + // receive the connectionDidDie: notification (a process has to be checking // the run-loop for this to happen). unsigned i, count = [vimControllers count]; for (i = 0; i < count; ++i) { diff --git a/src/MacVim/MMBackend.m b/src/MacVim/MMBackend.m index b440f2b0..d947e140 100644 --- a/src/MacVim/MMBackend.m +++ b/src/MacVim/MMBackend.m @@ -70,6 +70,7 @@ enum { - (void)handleSetFont:(NSData *)data; - (void)handleDropFiles:(NSData *)data; - (void)handleDropString:(NSData *)data; +- (BOOL)checkForModifiedBuffers; @end @@ -1165,18 +1166,6 @@ enum { } } -- (BOOL)checkForModifiedBuffers -{ - buf_T *buf; - for (buf = firstbuf; buf != NULL; buf = buf->b_next) { - if (bufIsChanged(buf)) { - return YES; - } - } - - return NO; -} - - (BOOL)starRegisterToPasteboard:(byref NSPasteboard *)pboard { if (VIsual_active && (State & NORMAL) && clip_star.available) { @@ -2084,6 +2073,18 @@ enum { #endif // FEAT_DND } +- (BOOL)checkForModifiedBuffers +{ + buf_T *buf; + for (buf = firstbuf; buf != NULL; buf = buf->b_next) { + if (bufIsChanged(buf)) { + return YES; + } + } + + return NO; +} + @end // MMBackend (Private) diff --git a/src/MacVim/MacVim.h b/src/MacVim/MacVim.h index e717c960..db6d1b07 100644 --- a/src/MacVim/MacVim.h +++ b/src/MacVim/MacVim.h @@ -29,7 +29,6 @@ @protocol MMBackendProtocol - (oneway void)processInput:(int)msgid data:(in bycopy NSData *)data; - (oneway void)processInputAndData:(in bycopy NSArray *)messages; -- (BOOL)checkForModifiedBuffers; - (oneway void)setDialogReturn:(in bycopy id)obj; - (BOOL)starRegisterToPasteboard:(byref NSPasteboard *)pboard; @end -- 2.11.4.GIT