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.
17 char *MessageStrings[] =
31 "SetTextColumnsMsgID",
32 "SetTextDimensionsMsgID",
34 "SetTextDimensionsReplyMsgID",
35 "SetWindowTitleMsgID",
43 "RemoveMenuItemMsgID",
44 "EnableMenuItemMsgID",
48 "CreateScrollbarMsgID",
49 "DestroyScrollbarMsgID",
51 "SetScrollbarPositionMsgID",
52 "SetScrollbarThumbMsgID",
53 "ScrollbarEventMsgID",
56 "VimShouldCloseMsgID",
57 "SetDefaultColorsMsgID",
66 "AdjustLinespaceMsgID",
69 "EnterFullscreenMsgID",
70 "LeaveFullscreenMsgID",
71 "BuffersNotModifiedMsgID",
72 "BuffersModifiedMsgID",
74 "SetPreEditPositionMsgID",
77 "EnableAntialiasMsgID",
78 "DisableAntialiasMsgID",
80 "SetDocumentFilenameMsgID",
81 "OpenWithArgumentsMsgID",
83 "SetFullscreenColorMsgID",
84 "ShowFindReplaceDialogMsgID",
86 "ActivateKeyScriptMsgID",
87 "DeactivateKeyScriptMsgID",
88 "EnableImControlMsgID",
89 "DisableImControlMsgID",
97 "SetWindowPositionMsgID",
98 "END OF MESSAGE IDs" // NOTE: Must be last!
104 NSString *MMLogLevelKey = @"MMLogLevel";
105 NSString *MMLogToStdErrKey = @"MMLogToStdErr";
107 // Argument used to stop MacVim from opening an empty window on startup
108 // (techincally this is a user default but should not be used as such).
109 NSString *MMNoWindowKey = @"MMNoWindow";
111 NSString *MMAutosaveRowsKey = @"MMAutosaveRows";
112 NSString *MMAutosaveColumnsKey = @"MMAutosaveColumns";
113 NSString *MMRendererKey = @"MMRenderer";
115 // Vim pasteboard type (holds motion type + string)
116 NSString *VimPboardType = @"VimPboardType";
117 // Vim find pasteboard type (string contains Vim regex patterns)
118 NSString *VimFindPboardType = @"VimFindPboardType";
120 int ASLogLevel = ASL_LEVEL_NOTICE;
124 // Create a string holding the labels of all messages in message queue for
125 // debugging purposes (condense some messages since there may typically be LOTS
126 // of them on a queue).
128 debugStringForMessageQueue(NSArray *queue)
130 NSMutableString *s = [NSMutableString new];
131 unsigned i, count = [queue count];
132 int item = 0, menu = 0, enable = 0, remove = 0;
133 int sets = 0, sett = 0, shows = 0, cres = 0, dess = 0;
134 for (i = 0; i < count; i += 2) {
135 NSData *value = [queue objectAtIndex:i];
136 int msgid = *((int*)[value bytes]);
137 if (msgid < 1 || msgid >= LastMsgID)
139 if (msgid == AddMenuItemMsgID) ++item;
140 else if (msgid == AddMenuMsgID) ++menu;
141 else if (msgid == EnableMenuItemMsgID) ++enable;
142 else if (msgid == RemoveMenuItemMsgID) ++remove;
143 else if (msgid == SetScrollbarPositionMsgID) ++sets;
144 else if (msgid == SetScrollbarThumbMsgID) ++sett;
145 else if (msgid == ShowScrollbarMsgID) ++shows;
146 else if (msgid == CreateScrollbarMsgID) ++cres;
147 else if (msgid == DestroyScrollbarMsgID) ++dess;
148 else [s appendFormat:@"%s ", MessageStrings[msgid]];
150 if (item > 0) [s appendFormat:@"AddMenuItemMsgID(%d) ", item];
151 if (menu > 0) [s appendFormat:@"AddMenuMsgID(%d) ", menu];
152 if (enable > 0) [s appendFormat:@"EnableMenuItemMsgID(%d) ", enable];
153 if (remove > 0) [s appendFormat:@"RemoveMenuItemMsgID(%d) ", remove];
154 if (sets > 0) [s appendFormat:@"SetScrollbarPositionMsgID(%d) ", sets];
155 if (sett > 0) [s appendFormat:@"SetScrollbarThumbMsgID(%d) ", sett];
156 if (shows > 0) [s appendFormat:@"ShowScrollbarMsgID(%d) ", shows];
157 if (cres > 0) [s appendFormat:@"CreateScrollbarMsgID(%d) ", cres];
158 if (dess > 0) [s appendFormat:@"DestroyScrollbarMsgID(%d) ", dess];
160 return [s autorelease];
166 @implementation NSString (MMExtras)
168 - (NSString *)stringByEscapingSpecialFilenameCharacters
170 // NOTE: This code assumes that no characters already have been escaped.
171 NSMutableString *string = [self mutableCopy];
173 [string replaceOccurrencesOfString:@"\\"
175 options:NSLiteralSearch
176 range:NSMakeRange(0, [string length])];
177 [string replaceOccurrencesOfString:@" "
179 options:NSLiteralSearch
180 range:NSMakeRange(0, [string length])];
181 [string replaceOccurrencesOfString:@"\t"
183 options:NSLiteralSearch
184 range:NSMakeRange(0, [string length])];
185 [string replaceOccurrencesOfString:@"%"
187 options:NSLiteralSearch
188 range:NSMakeRange(0, [string length])];
189 [string replaceOccurrencesOfString:@"#"
191 options:NSLiteralSearch
192 range:NSMakeRange(0, [string length])];
193 [string replaceOccurrencesOfString:@"|"
195 options:NSLiteralSearch
196 range:NSMakeRange(0, [string length])];
197 [string replaceOccurrencesOfString:@"\""
199 options:NSLiteralSearch
200 range:NSMakeRange(0, [string length])];
202 return [string autorelease];
205 - (NSString *)stringByRemovingFindPatterns
207 // Remove some common patterns added to search strings that other apps are
210 NSMutableString *string = [self mutableCopy];
212 // Added when doing * search
213 [string replaceOccurrencesOfString:@"\\<"
215 options:NSLiteralSearch
216 range:NSMakeRange(0, [string length])];
217 [string replaceOccurrencesOfString:@"\\>"
219 options:NSLiteralSearch
220 range:NSMakeRange(0, [string length])];
221 // \V = match whole word
222 [string replaceOccurrencesOfString:@"\\V"
224 options:NSLiteralSearch
225 range:NSMakeRange(0, [string length])];
226 // \c = case insensitive, \C = case sensitive
227 [string replaceOccurrencesOfString:@"\\c"
229 options:NSCaseInsensitiveSearch|NSLiteralSearch
230 range:NSMakeRange(0, [string length])];
232 return [string autorelease];
235 @end // NSString (MMExtras)
239 @implementation NSColor (MMExtras)
241 + (NSColor *)colorWithRgbInt:(unsigned)rgb
243 float r = ((rgb>>16) & 0xff)/255.0f;
244 float g = ((rgb>>8) & 0xff)/255.0f;
245 float b = (rgb & 0xff)/255.0f;
247 return [NSColor colorWithDeviceRed:r green:g blue:b alpha:1.0f];
250 + (NSColor *)colorWithArgbInt:(unsigned)argb
252 float a = ((argb>>24) & 0xff)/255.0f;
253 float r = ((argb>>16) & 0xff)/255.0f;
254 float g = ((argb>>8) & 0xff)/255.0f;
255 float b = (argb & 0xff)/255.0f;
257 return [NSColor colorWithDeviceRed:r green:g blue:b alpha:a];
260 @end // NSColor (MMExtras)
265 @implementation NSDictionary (MMExtras)
267 + (id)dictionaryWithData:(NSData *)data
269 id plist = [NSPropertyListSerialization
270 propertyListFromData:data
271 mutabilityOption:NSPropertyListImmutable
273 errorDescription:NULL];
275 return [plist isKindOfClass:[NSDictionary class]] ? plist : nil;
278 - (NSData *)dictionaryAsData
280 return [NSPropertyListSerialization dataFromPropertyList:self
281 format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL];
289 @implementation NSMutableDictionary (MMExtras)
291 + (id)dictionaryWithData:(NSData *)data
293 id plist = [NSPropertyListSerialization
294 propertyListFromData:data
295 mutabilityOption:NSPropertyListMutableContainers
297 errorDescription:NULL];
299 return [plist isKindOfClass:[NSMutableDictionary class]] ? plist : nil;
310 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
312 // Allow for changing the log level via user defaults. If no key is found
313 // the default log level will be used (which for ASL is to log everything
314 // up to ASL_LEVEL_NOTICE). This key is an integer which corresponds to
315 // the ASL_LEVEL_* macros (0 is most severe, 7 is debug level).
316 id logLevelObj = [ud objectForKey:MMLogLevelKey];
318 int logLevel = [logLevelObj intValue];
319 if (logLevel < 0) logLevel = 0;
320 if (logLevel > ASL_LEVEL_DEBUG) logLevel = ASL_LEVEL_DEBUG;
322 ASLogLevel = logLevel;
323 asl_set_filter(NULL, ASL_FILTER_MASK_UPTO(logLevel));
326 // Allow for changing whether a copy of each log should be sent to stderr
327 // (this defaults to NO if this key is missing in the user defaults
328 // database). The above filter mask is applied to logs going to stderr,
329 // contrary to how "vanilla" ASL works.
330 BOOL logToStdErr = [ud boolForKey:MMLogToStdErrKey];
332 asl_add_log_file(NULL, 2); // The file descriptor for stderr is 2