Here's my patch, in case you change your mind.
--
You received this message from the "vim_mac" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
>From 7eab726bdec1ba65be801324d04c86621a38bb42 Mon Sep 17 00:00:00 2001
From: David Whetstone <[email protected]>
Date: Thu, 24 Feb 2011 13:08:10 -0800
Subject: [PATCH] Use enum MessageID instead of raw int.
---
src/MacVim/MMVimController.h | 4 +-
src/MacVim/MMVimController.m | 228 ++++++++++++++++++++++++++++++------------
src/MacVim/MacVim.h | 4 +-
3 files changed, 170 insertions(+), 66 deletions(-)
diff --git a/src/MacVim/MMVimController.h b/src/MacVim/MMVimController.h
index 7246e68..6081f39 100644
--- a/src/MacVim/MMVimController.h
+++ b/src/MacVim/MMVimController.h
@@ -57,8 +57,8 @@
- (void)filesDraggedToTabBar:(NSArray *)filenames;
- (void)dropString:(NSString *)string;
- (void)passArguments:(NSDictionary *)args;
-- (void)sendMessage:(int)msgid data:(NSData *)data;
-- (BOOL)sendMessageNow:(int)msgid data:(NSData *)data
+- (void)sendMessage:(MessageID)msgid data:(NSData *)data;
+- (BOOL)sendMessageNow:(MessageID)msgid data:(NSData *)data
timeout:(NSTimeInterval)timeout;
- (void)addVimInput:(NSString *)string;
- (NSString *)evaluateVimExpression:(NSString *)expr;
diff --git a/src/MacVim/MMVimController.m b/src/MacVim/MMVimController.m
index c5bbd98..767baa7 100644
--- a/src/MacVim/MMVimController.m
+++ b/src/MacVim/MMVimController.m
@@ -51,7 +51,7 @@ static NSTimeInterval MMSetDialogReturnTimeout = 1.0;
static unsigned identifierCounter = 1;
-static BOOL isUnsafeMessage(int msgid);
+static BOOL isUnsafeMessage(MessageID msgid);
// HACK! AppKit private methods from NSToolTipManager. As an alternative to
@@ -74,7 +74,7 @@ static BOOL isUnsafeMessage(int msgid);
@interface MMVimController (Private)
- (void)doProcessInputQueue:(NSArray *)queue;
-- (void)handleMessage:(int)msgid data:(NSData *)data;
+- (void)handleMessage:(MessageID)msgid data:(NSData *)data;
- (void)savePanelDidEnd:(NSSavePanel *)panel code:(int)code
context:(void *)context;
- (void)alertDidEnd:(MMAlert *)alert code:(int)code context:(void *)context;
@@ -329,7 +329,7 @@ static BOOL isUnsafeMessage(int msgid);
[self sendMessage:OpenWithArgumentsMsgID data:[args dictionaryAsData]];
}
-- (void)sendMessage:(int)msgid data:(NSData *)data
+- (void)sendMessage:(MessageID)msgid data:(NSData *)data
{
ASLogDebug(@"msg=%s (isInitialized=%d)",
MessageStrings[msgid], isInitialized);
@@ -345,7 +345,7 @@ static BOOL isUnsafeMessage(int msgid);
}
}
-- (BOOL)sendMessageNow:(int)msgid data:(NSData *)data
+- (BOOL)sendMessageNow:(MessageID)msgid data:(NSData *)data
timeout:(NSTimeInterval)timeout
{
// Send a message with a timeout. USE WITH EXTREME CAUTION! Sending
@@ -507,7 +507,7 @@ static BOOL isUnsafeMessage(int msgid);
NSData *value = [queue objectAtIndex:i];
NSData *data = [queue objectAtIndex:i+1];
- int msgid = *((int*)[value bytes]);
+ MessageID msgid = *((MessageID*)[value bytes]);
BOOL inDefaultMode = [[[NSRunLoop currentRunLoop] currentMode]
isEqual:NSDefaultRunLoopMode];
@@ -547,9 +547,10 @@ static BOOL isUnsafeMessage(int msgid);
}
}
-- (void)handleMessage:(int)msgid data:(NSData *)data
+- (void)handleMessage:(MessageID)msgid data:(NSData *)data
{
- if (OpenWindowMsgID == msgid) {
+ switch (msgid) {
+ case OpenWindowMsgID: {
[windowController openWindow];
// HACK: Delay actually presenting the window onscreen until after
@@ -560,22 +561,35 @@ static BOOL isUnsafeMessage(int msgid);
[windowController performSelector:@selector(presentWindow:)
withObject:nil
afterDelay:0];
- } else if (BatchDrawMsgID == msgid) {
+ break;
+ }
+ case BatchDrawMsgID: {
[[[windowController vimView] textView] performBatchDrawWithData:data];
- } else if (SelectTabMsgID == msgid) {
+ break;
+ }
+ case SelectTabMsgID: {
#if 0 // NOTE: Tab selection is done inside updateTabsWithData:.
const void *bytes = [data bytes];
int idx = *((int*)bytes);
[windowController selectTabWithIndex:idx];
#endif
- } else if (UpdateTabBarMsgID == msgid) {
+ break;
+ }
+ case UpdateTabBarMsgID: {
[windowController updateTabsWithData:data];
- } else if (ShowTabBarMsgID == msgid) {
+ break;
+ }
+ case ShowTabBarMsgID: {
[windowController showTabBar:YES];
- } else if (HideTabBarMsgID == msgid) {
+ break;
+ }
+ case HideTabBarMsgID: {
[windowController showTabBar:NO];
- } else if (SetTextDimensionsMsgID == msgid || LiveResizeMsgID == msgid ||
- SetTextDimensionsReplyMsgID == msgid) {
+ break;
+ }
+ case SetTextDimensionsMsgID:
+ case LiveResizeMsgID:
+ case SetTextDimensionsReplyMsgID: {
const void *bytes = [data bytes];
int rows = *((int*)bytes); bytes += sizeof(int);
int cols = *((int*)bytes); bytes += sizeof(int);
@@ -589,7 +603,9 @@ static BOOL isUnsafeMessage(int msgid);
columns:cols
isLive:(LiveResizeMsgID==msgid)
keepOnScreen:onScreen];
- } else if (SetWindowTitleMsgID == msgid) {
+ break;
+ }
+ case SetWindowTitleMsgID: {
const void *bytes = [data bytes];
int len = *((int*)bytes); bytes += sizeof(int);
@@ -603,7 +619,9 @@ static BOOL isUnsafeMessage(int msgid);
[windowController setTitle:string];
[string release];
- } else if (SetDocumentFilenameMsgID == msgid) {
+ break;
+ }
+ case SetDocumentFilenameMsgID: {
const void *bytes = [data bytes];
int len = *((int*)bytes); bytes += sizeof(int);
@@ -617,11 +635,15 @@ static BOOL isUnsafeMessage(int msgid);
} else {
[windowController setDocumentFilename:@""];
}
- } else if (AddMenuMsgID == msgid) {
+ break;
+ }
+ case AddMenuMsgID: {
NSDictionary *attrs = [NSDictionary dictionaryWithData:data];
[self addMenuWithDescriptor:[attrs objectForKey:@"descriptor"]
atIndex:[[attrs objectForKey:@"index"] intValue]];
- } else if (AddMenuItemMsgID == msgid) {
+ break;
+ }
+ case AddMenuItemMsgID: {
NSDictionary *attrs = [NSDictionary dictionaryWithData:data];
[self addMenuItemWithDescriptor:[attrs objectForKey:@"descriptor"]
atIndex:[[attrs objectForKey:@"index"] intValue]
@@ -631,14 +653,20 @@ static BOOL isUnsafeMessage(int msgid);
modifierMask:[[attrs objectForKey:@"modifierMask"] intValue]
action:[attrs objectForKey:@"action"]
isAlternate:[[attrs objectForKey:@"isAlternate"] boolValue]];
- } else if (RemoveMenuItemMsgID == msgid) {
+ break;
+ }
+ case RemoveMenuItemMsgID: {
NSDictionary *attrs = [NSDictionary dictionaryWithData:data];
[self removeMenuItemWithDescriptor:[attrs objectForKey:@"descriptor"]];
- } else if (EnableMenuItemMsgID == msgid) {
+ break;
+ }
+ case EnableMenuItemMsgID: {
NSDictionary *attrs = [NSDictionary dictionaryWithData:data];
[self enableMenuItemWithDescriptor:[attrs objectForKey:@"descriptor"]
state:[[attrs objectForKey:@"enable"] boolValue]];
- } else if (ShowToolbarMsgID == msgid) {
+ break;
+ }
+ case ShowToolbarMsgID: {
const void *bytes = [data bytes];
int enable = *((int*)bytes); bytes += sizeof(int);
int flags = *((int*)bytes); bytes += sizeof(int);
@@ -655,24 +683,32 @@ static BOOL isUnsafeMessage(int msgid);
: NSToolbarSizeModeSmall;
[windowController showToolbar:enable size:size mode:mode];
- } else if (CreateScrollbarMsgID == msgid) {
+ break;
+ }
+ case CreateScrollbarMsgID: {
const void *bytes = [data bytes];
int32_t ident = *((int32_t*)bytes); bytes += sizeof(int32_t);
int type = *((int*)bytes); bytes += sizeof(int);
[windowController createScrollbarWithIdentifier:ident type:type];
- } else if (DestroyScrollbarMsgID == msgid) {
+ break;
+ }
+ case DestroyScrollbarMsgID: {
const void *bytes = [data bytes];
int32_t ident = *((int32_t*)bytes); bytes += sizeof(int32_t);
[windowController destroyScrollbarWithIdentifier:ident];
- } else if (ShowScrollbarMsgID == msgid) {
+ break;
+ }
+ case ShowScrollbarMsgID: {
const void *bytes = [data bytes];
int32_t ident = *((int32_t*)bytes); bytes += sizeof(int32_t);
int visible = *((int*)bytes); bytes += sizeof(int);
[windowController showScrollbarWithIdentifier:ident state:visible];
- } else if (SetScrollbarPositionMsgID == msgid) {
+ break;
+ }
+ case SetScrollbarPositionMsgID: {
const void *bytes = [data bytes];
int32_t ident = *((int32_t*)bytes); bytes += sizeof(int32_t);
int pos = *((int*)bytes); bytes += sizeof(int);
@@ -680,7 +716,9 @@ static BOOL isUnsafeMessage(int msgid);
[windowController setScrollbarPosition:pos length:len
identifier:ident];
- } else if (SetScrollbarThumbMsgID == msgid) {
+ break;
+ }
+ case SetScrollbarThumbMsgID: {
const void *bytes = [data bytes];
int32_t ident = *((int32_t*)bytes); bytes += sizeof(int32_t);
float val = *((float*)bytes); bytes += sizeof(float);
@@ -688,7 +726,9 @@ static BOOL isUnsafeMessage(int msgid);
[windowController setScrollbarThumbValue:val proportion:prop
identifier:ident];
- } else if (SetFontMsgID == msgid) {
+ break;
+ }
+ case SetFontMsgID: {
const void *bytes = [data bytes];
float size = *((float*)bytes); bytes += sizeof(float);
int len = *((int*)bytes); bytes += sizeof(int);
@@ -705,7 +745,9 @@ static BOOL isUnsafeMessage(int msgid);
[windowController setFont:font];
[name release];
- } else if (SetWideFontMsgID == msgid) {
+ break;
+ }
+ case SetWideFontMsgID: {
const void *bytes = [data bytes];
float size = *((float*)bytes); bytes += sizeof(float);
int len = *((int*)bytes); bytes += sizeof(int);
@@ -720,7 +762,9 @@ static BOOL isUnsafeMessage(int msgid);
} else {
[windowController setWideFont:nil];
}
- } else if (SetDefaultColorsMsgID == msgid) {
+ break;
+ }
+ case SetDefaultColorsMsgID: {
const void *bytes = [data bytes];
unsigned bg = *((unsigned*)bytes); bytes += sizeof(unsigned);
unsigned fg = *((unsigned*)bytes); bytes += sizeof(unsigned);
@@ -728,7 +772,9 @@ static BOOL isUnsafeMessage(int msgid);
NSColor *fore = [NSColor colorWithRgbInt:fg];
[windowController setDefaultColorsBackground:back foreground:fore];
- } else if (ExecuteActionMsgID == msgid) {
+ break;
+ }
+ case ExecuteActionMsgID: {
const void *bytes = [data bytes];
int len = *((int*)bytes); bytes += sizeof(int);
NSString *actionName = [[NSString alloc]
@@ -739,7 +785,9 @@ static BOOL isUnsafeMessage(int msgid);
[NSApp sendAction:sel to:nil from:self];
[actionName release];
- } else if (ShowPopupMenuMsgID == msgid) {
+ break;
+ }
+ case ShowPopupMenuMsgID: {
NSDictionary *attrs = [NSDictionary dictionaryWithData:data];
// The popup menu enters a modal loop so delay this call so that we
@@ -747,34 +795,48 @@ static BOOL isUnsafeMessage(int msgid);
[self performSelector:@selector(popupMenuWithAttributes:)
withObject:attrs
afterDelay:0];
- } else if (SetMouseShapeMsgID == msgid) {
+ break;
+ }
+ case SetMouseShapeMsgID: {
const void *bytes = [data bytes];
int shape = *((int*)bytes); bytes += sizeof(int);
[windowController setMouseShape:shape];
- } else if (AdjustLinespaceMsgID == msgid) {
+ break;
+ }
+ case AdjustLinespaceMsgID: {
const void *bytes = [data bytes];
int linespace = *((int*)bytes); bytes += sizeof(int);
[windowController adjustLinespace:linespace];
- } else if (ActivateMsgID == msgid) {
+ break;
+ }
+ case ActivateMsgID: {
[NSApp activateIgnoringOtherApps:YES];
[[windowController window] makeKeyAndOrderFront:self];
- } else if (SetServerNameMsgID == msgid) {
+ break;
+ }
+ case SetServerNameMsgID: {
NSString *name = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
[self setServerName:name];
[name release];
- } else if (EnterFullscreenMsgID == msgid) {
+ break;
+ }
+ case EnterFullscreenMsgID: {
const void *bytes = [data bytes];
int fuoptions = *((int*)bytes); bytes += sizeof(int);
int bg = *((int*)bytes);
NSColor *back = [NSColor colorWithArgbInt:bg];
[windowController enterFullscreen:fuoptions backgroundColor:back];
- } else if (LeaveFullscreenMsgID == msgid) {
+ break;
+ }
+ case LeaveFullscreenMsgID: {
[windowController leaveFullscreen];
- } else if (SetBuffersModifiedMsgID == msgid) {
+ break;
+ }
+ case SetBuffersModifiedMsgID: {
const void *bytes = [data bytes];
// state < 0 <-> some buffer modified
// state > 0 <-> current buffer modified
@@ -789,55 +851,85 @@ static BOOL isUnsafeMessage(int msgid);
// TODO: Make 'hasModifiedBuffer' part of the Vim state?
[windowController setBufferModified:(state > 0)];
hasModifiedBuffer = (state != 0);
- } else if (SetPreEditPositionMsgID == msgid) {
+ break;
+ }
+ case SetPreEditPositionMsgID: {
const int *dim = (const int*)[data bytes];
[[[windowController vimView] textView] setPreEditRow:dim[0]
column:dim[1]];
- } else if (EnableAntialiasMsgID == msgid) {
+ break;
+ }
+ case EnableAntialiasMsgID: {
[[[windowController vimView] textView] setAntialias:YES];
- } else if (DisableAntialiasMsgID == msgid) {
+ break;
+ }
+ case DisableAntialiasMsgID: {
[[[windowController vimView] textView] setAntialias:NO];
- } else if (SetVimStateMsgID == msgid) {
+ break;
+ }
+ case SetVimStateMsgID: {
NSDictionary *dict = [NSDictionary dictionaryWithData:data];
if (dict) {
[vimState release];
vimState = [dict retain];
}
- } else if (CloseWindowMsgID == msgid) {
+ break;
+ }
+ case CloseWindowMsgID: {
[self scheduleClose];
- } else if (SetFullscreenColorMsgID == msgid) {
+ break;
+ }
+ case SetFullscreenColorMsgID: {
const int *bg = (const int*)[data bytes];
NSColor *color = [NSColor colorWithRgbInt:*bg];
[windowController setFullscreenBackgroundColor:color];
- } else if (ShowFindReplaceDialogMsgID == msgid) {
+ break;
+ }
+ case ShowFindReplaceDialogMsgID: {
NSDictionary *dict = [NSDictionary dictionaryWithData:data];
if (dict) {
[[MMFindReplaceController sharedInstance]
showWithText:[dict objectForKey:@"text"]
flags:[[dict objectForKey:@"flags"] intValue]];
}
- } else if (ActivateKeyScriptMsgID == msgid) {
+ break;
+ }
+ case ActivateKeyScriptMsgID: {
[[[windowController vimView] textView] activateIm:YES];
- } else if (DeactivateKeyScriptMsgID == msgid) {
+ break;
+ }
+ case DeactivateKeyScriptMsgID: {
[[[windowController vimView] textView] activateIm:NO];
- } else if (EnableImControlMsgID == msgid) {
+ break;
+ }
+ case EnableImControlMsgID: {
[[[windowController vimView] textView] setImControl:YES];
- } else if (DisableImControlMsgID == msgid) {
+ break;
+ }
+ case DisableImControlMsgID: {
[[[windowController vimView] textView] setImControl:NO];
- } else if (BrowseForFileMsgID == msgid) {
+ break;
+ }
+ case BrowseForFileMsgID: {
NSDictionary *dict = [NSDictionary dictionaryWithData:data];
if (dict)
[self handleBrowseForFile:dict];
- } else if (ShowDialogMsgID == msgid) {
+ break;
+ }
+ case ShowDialogMsgID: {
NSDictionary *dict = [NSDictionary dictionaryWithData:data];
if (dict)
[self handleShowDialog:dict];
- } else if (DeleteSignMsgID == msgid) {
+ break;
+ }
+ case DeleteSignMsgID: {
NSDictionary *dict = [NSDictionary dictionaryWithData:data];
if (dict)
[self handleDeleteSign:dict];
- } else if (ZoomMsgID == msgid) {
+ break;
+ }
+ case ZoomMsgID: {
const void *bytes = [data bytes];
int rows = *((int*)bytes); bytes += sizeof(int);
int cols = *((int*)bytes); bytes += sizeof(int);
@@ -846,13 +938,17 @@ static BOOL isUnsafeMessage(int msgid);
[windowController zoomWithRows:rows
columns:cols
state:state];
- } else if (SetWindowPositionMsgID == msgid) {
+ break;
+ }
+ case SetWindowPositionMsgID: {
const void *bytes = [data bytes];
int x = *((int*)bytes); bytes += sizeof(int);
int y = *((int*)bytes); bytes += sizeof(int);
[windowController setTopLeft:NSMakePoint(x,y)];
- } else if (SetTooltipMsgID == msgid) {
+ break;
+ }
+ case SetTooltipMsgID: {
id textView = [[windowController vimView] textView];
NSDictionary *dict = [NSDictionary dictionaryWithData:data];
NSString *toolTip = dict ? [dict objectForKey:@"toolTip"] : nil;
@@ -860,23 +956,29 @@ static BOOL isUnsafeMessage(int msgid);
[textView setToolTipAtMousePoint:toolTip];
else
[textView setToolTipAtMousePoint:nil];
- } else if (SetTooltipDelayMsgID == msgid) {
+ break;
+ }
+ case SetTooltipDelayMsgID: {
NSDictionary *dict = [NSDictionary dictionaryWithData:data];
NSNumber *delay = dict ? [dict objectForKey:@"delay"] : nil;
if (delay)
[self setToolTipDelay:[delay floatValue]];
- } else if (AddToMRUMsgID == msgid) {
+ break;
+ }
+ case AddToMRUMsgID: {
NSDictionary *dict = [NSDictionary dictionaryWithData:data];
NSArray *filenames = dict ? [dict objectForKey:@"filenames"] : nil;
if (filenames)
[[NSDocumentController sharedDocumentController]
noteNewRecentFilePaths:filenames];
-
- // IMPORTANT: When adding a new message, make sure to update
- // isUnsafeMessage() if necessary!
- } else {
+ break;
+ }
+ default: {
+ // IMPORTANT: When adding a new message, make sure to update
+ // isUnsafeMessage() if necessary!
ASLogWarn(@"Unknown message received (msgid=%d)", msgid);
}
+ };
}
- (void)savePanelDidEnd:(NSSavePanel *)panel code:(int)code
@@ -1576,12 +1678,12 @@ static BOOL isUnsafeMessage(int msgid);
static BOOL
-isUnsafeMessage(int msgid)
+isUnsafeMessage(MessageID msgid)
{
// Messages that may release Cocoa objects must be added to this list. For
// example, UpdateTabBarMsgID may delete NSTabViewItem objects so it goes
// on this list.
- static int unsafeMessages[] = { // REASON MESSAGE IS ON THIS LIST:
+ static MessageID unsafeMessages[] = { // REASON MESSAGE IS ON THIS LIST:
//OpenWindowMsgID, // Changes lots of state
UpdateTabBarMsgID, // May delete NSTabViewItem
RemoveMenuItemMsgID, // Deletes NSMenuItem
diff --git a/src/MacVim/MacVim.h b/src/MacVim/MacVim.h
index ca37c2d..a486044 100644
--- a/src/MacVim/MacVim.h
+++ b/src/MacVim/MacVim.h
@@ -106,7 +106,7 @@
// NOTE! This array must be updated whenever the enum below changes!
extern char *MessageStrings[];
-enum {
+enum MessageID {
OpenWindowMsgID = 1, // NOTE: FIRST IN ENUM MUST BE 1
KeyDownMsgID,
BatchDrawMsgID,
@@ -192,6 +192,8 @@ enum {
LastMsgID // NOTE: MUST BE LAST MESSAGE IN ENUM!
};
+typedef enum MessageID MessageID;
+
enum {
ClearAllDrawType = 1,
--
1.7.3.4