From 3aee15e067e230457c26990e53f8314acdc0cd6d Mon Sep 17 00:00:00 2001 From: Bjorn Winckler Date: Thu, 15 Nov 2007 22:04:06 +0100 Subject: [PATCH] Preserve swap files after crash Before terminating gracefully, send a TerminateNowMsgID to every Vim process so that they can determine whether MacVim quit or crashed. If MacVim quits, call getout() to exit Vim (this removes swap files), otherwise call getout_preserve_modified() (this preserves swap files). --- src/MacVim/MMAppController.m | 11 ++++++++++- src/MacVim/MMBackend.h | 1 + src/MacVim/MMBackend.m | 14 ++++++++++---- src/MacVim/MacVim.h | 1 + src/MacVim/MacVim.m | 1 + src/main.c | 3 ++- 6 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/MacVim/MMAppController.m b/src/MacVim/MMAppController.m index 21f61c1e..18e106a6 100644 --- a/src/MacVim/MMAppController.m +++ b/src/MacVim/MMAppController.m @@ -198,7 +198,7 @@ static NSTimeInterval MMReplyTimeout = 5; // 'documentEdited' flag of the window correspondingly.) NSEnumerator *e = [[NSApp windows] objectEnumerator]; id window; - while (window = [e nextObject]) { + while ((window = [e nextObject])) { if ([window isDocumentEdited]) { modifiedBuffers = YES; break; @@ -220,6 +220,15 @@ static NSTimeInterval MMReplyTimeout = 5; [alert release]; } + // Tell all Vim processes to terminate now (otherwise they'll leave swap + // files behind). + if (NSTerminateNow == reply) { + e = [vimControllers objectEnumerator]; + id vc; + while ((vc = [e nextObject])) + [vc sendMessage:TerminateNowMsgID data:nil]; + } + return reply; } diff --git a/src/MacVim/MMBackend.h b/src/MacVim/MMBackend.h index 02b959e1..008a97b8 100644 --- a/src/MacVim/MMBackend.h +++ b/src/MacVim/MMBackend.h @@ -44,6 +44,7 @@ NSString *alternateServerName; ATSFontContainerRef fontContainerRef; NSFont *oldWideFont; + BOOL isTerminating; } + (MMBackend *)sharedInstance; diff --git a/src/MacVim/MMBackend.m b/src/MacVim/MMBackend.m index ee21c66b..40832d5e 100644 --- a/src/MacVim/MMBackend.m +++ b/src/MacVim/MMBackend.m @@ -1133,6 +1133,8 @@ enum { [self addInput:string]; [string release]; } + } else if (TerminateNowMsgID == msgid) { + isTerminating = YES; } else { // Not keyboard or mouse event, queue it and handle later. //NSLog(@"Add event %s to input event queue", MessageStrings[msgid]); @@ -1776,11 +1778,15 @@ enum { { // If the main connection to MacVim is lost this means that MacVim was // either quit (by the user chosing Quit on the MacVim menu), or it has - // crashed. In either case our only option is to quit now. - // TODO: Write backup file? + // crashed. In the former case the flag 'isTerminating' is set and we then + // quit cleanly; in the latter case we make sure the swap files are left + // for recovery. - //NSLog(@"A Vim process lost its connection to MacVim; quitting."); - getout(0); + NSLog(@"%s isTerminating=%d", _cmd, isTerminating); + if (isTerminating) + getout(0); + else + getout_preserve_modified(1); } - (void)blinkTimerFired:(NSTimer *)timer diff --git a/src/MacVim/MacVim.h b/src/MacVim/MacVim.h index b07b3333..515f8877 100644 --- a/src/MacVim/MacVim.h +++ b/src/MacVim/MacVim.h @@ -156,6 +156,7 @@ enum { BuffersModifiedMsgID, AddInputMsgID, SetPreEditPositionMsgID, + TerminateNowMsgID, }; diff --git a/src/MacVim/MacVim.m b/src/MacVim/MacVim.m index b9a5df8c..5e718fd6 100644 --- a/src/MacVim/MacVim.m +++ b/src/MacVim/MacVim.m @@ -69,6 +69,7 @@ char *MessageStrings[] = "BuffersModifiedMsgID", "AddInputMsgID", "SetPreEditPositionMsgID", + "TerminateNowMsgID", }; diff --git a/src/main.c b/src/main.c index f5533abb..4b469648 100644 --- a/src/main.c +++ b/src/main.c @@ -1226,7 +1226,8 @@ main_loop(cmdwin, noexmode) } -#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO) +#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO) \ + || defined(FEAT_GUI_MACVIM) /* * Exit, but leave behind swap files for modified buffers. */ -- 2.11.4.GIT