1 /* vi:set ts=8 sts=4 sw=4 ft=objc:
3 * VIM - Vi IMproved by Bram Moolenaar
4 * MacVim GUI port by Bjorn Winckler
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
11 * MacVim.m: Code shared between Vim and MacVim.
16 char *MessageStrings[] =
32 "SetTextColumnsMsgID",
33 "SetTextDimensionsMsgID",
35 "SetTextDimensionsReplyMsgID",
36 "SetWindowTitleMsgID",
44 "RemoveMenuItemMsgID",
45 "EnableMenuItemMsgID",
49 "CreateScrollbarMsgID",
50 "DestroyScrollbarMsgID",
52 "SetScrollbarPositionMsgID",
53 "SetScrollbarThumbMsgID",
54 "ScrollbarEventMsgID",
57 "VimShouldCloseMsgID",
58 "SetDefaultColorsMsgID",
67 "AdjustLinespaceMsgID",
70 "EnterFullscreenMsgID",
71 "LeaveFullscreenMsgID",
72 "BuffersNotModifiedMsgID",
73 "BuffersModifiedMsgID",
75 "SetPreEditPositionMsgID",
78 "EnableAntialiasMsgID",
79 "DisableAntialiasMsgID",
81 "SetDocumentFilenameMsgID",
82 "OpenWithArgumentsMsgID",
84 "SetFullscreenColorMsgID",
85 "ShowFindReplaceDialogMsgID",
87 "ActivateKeyScriptID",
88 "DeactivateKeyScriptID",
89 "EnableImControlMsgID",
90 "DisableImControlMsgID",
95 "END OF MESSAGE IDs" // NOTE: Must be last!
101 // Argument used to stop MacVim from opening an empty window on startup
102 // (techincally this is a user default but should not be used as such).
103 NSString *MMNoWindowKey = @"MMNoWindow";
105 // Vim pasteboard type (holds motion type + string)
106 NSString *VimPBoardType = @"VimPBoardType";
110 // Create a string holding the labels of all messages in message queue for
111 // debugging purposes (condense some messages since there may typically be LOTS
112 // of them on a queue).
114 debugStringForMessageQueue(NSArray *queue)
116 NSMutableString *s = [NSMutableString new];
117 unsigned i, count = [queue count];
118 int item = 0, menu = 0, enable = 0;
119 for (i = 0; i < count; i += 2) {
120 NSData *value = [queue objectAtIndex:i];
121 int msgid = *((int*)[value bytes]);
122 if (msgid < 1 || msgid >= LastMsgID)
124 if (msgid == AddMenuItemMsgID) ++item;
125 else if (msgid == AddMenuMsgID) ++menu;
126 else if (msgid == EnableMenuItemMsgID) ++enable;
127 else [s appendFormat:@"%s ", MessageStrings[msgid]];
129 if (item > 0) [s appendFormat:@"AddMenuItemMsgID(%d) ", item];
130 if (menu > 0) [s appendFormat:@"AddMenuMsgID(%d) ", menu];
131 if (enable > 0) [s appendFormat:@"EnableMenuItemMsgID(%d) ", enable];
133 return [s autorelease];
139 @implementation NSString (MMExtras)
141 - (NSString *)stringByEscapingSpecialFilenameCharacters
143 // NOTE: This code assumes that no characters already have been escaped.
144 NSMutableString *string = [self mutableCopy];
146 [string replaceOccurrencesOfString:@"\\"
148 options:NSLiteralSearch
149 range:NSMakeRange(0, [string length])];
150 [string replaceOccurrencesOfString:@" "
152 options:NSLiteralSearch
153 range:NSMakeRange(0, [string length])];
154 [string replaceOccurrencesOfString:@"\t"
156 options:NSLiteralSearch
157 range:NSMakeRange(0, [string length])];
158 [string replaceOccurrencesOfString:@"%"
160 options:NSLiteralSearch
161 range:NSMakeRange(0, [string length])];
162 [string replaceOccurrencesOfString:@"#"
164 options:NSLiteralSearch
165 range:NSMakeRange(0, [string length])];
166 [string replaceOccurrencesOfString:@"|"
168 options:NSLiteralSearch
169 range:NSMakeRange(0, [string length])];
170 [string replaceOccurrencesOfString:@"\""
172 options:NSLiteralSearch
173 range:NSMakeRange(0, [string length])];
175 return [string autorelease];
178 @end // NSString (MMExtras)
182 @implementation NSColor (MMExtras)
184 + (NSColor *)colorWithRgbInt:(unsigned)rgb
186 float r = ((rgb>>16) & 0xff)/255.0f;
187 float g = ((rgb>>8) & 0xff)/255.0f;
188 float b = (rgb & 0xff)/255.0f;
190 return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:1.0f];
193 + (NSColor *)colorWithArgbInt:(unsigned)argb
195 float a = ((argb>>24) & 0xff)/255.0f;
196 float r = ((argb>>16) & 0xff)/255.0f;
197 float g = ((argb>>8) & 0xff)/255.0f;
198 float b = (argb & 0xff)/255.0f;
200 return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:a];
203 @end // NSColor (MMExtras)
208 @implementation NSDictionary (MMExtras)
210 + (id)dictionaryWithData:(NSData *)data
212 id plist = [NSPropertyListSerialization
213 propertyListFromData:data
214 mutabilityOption:NSPropertyListImmutable
216 errorDescription:NULL];
218 return [plist isKindOfClass:[NSDictionary class]] ? plist : nil;
221 - (NSData *)dictionaryAsData
223 return [NSPropertyListSerialization dataFromPropertyList:self
224 format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL];
232 @implementation NSMutableDictionary (MMExtras)
234 + (id)dictionaryWithData:(NSData *)data
236 id plist = [NSPropertyListSerialization
237 propertyListFromData:data
238 mutabilityOption:NSPropertyListMutableContainers
240 errorDescription:NULL];
242 return [plist isKindOfClass:[NSMutableDictionary class]] ? plist : nil;