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",
96 "END OF MESSAGE IDs" // NOTE: Must be last!
102 NSString *MMLogLevelKey = @"MMLogLevel";
103 NSString *MMLogToStdErrKey = @"MMLogToStdErr";
105 // Argument used to stop MacVim from opening an empty window on startup
106 // (techincally this is a user default but should not be used as such).
107 NSString *MMNoWindowKey = @"MMNoWindow";
109 NSString *MMAutosaveRowsKey = @"MMAutosaveRows";
110 NSString *MMAutosaveColumnsKey = @"MMAutosaveColumns";
111 NSString *MMRendererKey = @"MMRenderer";
113 // Vim pasteboard type (holds motion type + string)
114 NSString *VimPBoardType = @"VimPBoardType";
116 int ASLogLevel = ASL_LEVEL_NOTICE;
120 // Create a string holding the labels of all messages in message queue for
121 // debugging purposes (condense some messages since there may typically be LOTS
122 // of them on a queue).
124 debugStringForMessageQueue(NSArray *queue)
126 NSMutableString *s = [NSMutableString new];
127 unsigned i, count = [queue count];
128 int item = 0, menu = 0, enable = 0, remove = 0;
129 int sets = 0, sett = 0, shows = 0, cres = 0, dess = 0;
130 for (i = 0; i < count; i += 2) {
131 NSData *value = [queue objectAtIndex:i];
132 int msgid = *((int*)[value bytes]);
133 if (msgid < 1 || msgid >= LastMsgID)
135 if (msgid == AddMenuItemMsgID) ++item;
136 else if (msgid == AddMenuMsgID) ++menu;
137 else if (msgid == EnableMenuItemMsgID) ++enable;
138 else if (msgid == RemoveMenuItemMsgID) ++remove;
139 else if (msgid == SetScrollbarPositionMsgID) ++sets;
140 else if (msgid == SetScrollbarThumbMsgID) ++sett;
141 else if (msgid == ShowScrollbarMsgID) ++shows;
142 else if (msgid == CreateScrollbarMsgID) ++cres;
143 else if (msgid == DestroyScrollbarMsgID) ++dess;
144 else [s appendFormat:@"%s ", MessageStrings[msgid]];
146 if (item > 0) [s appendFormat:@"AddMenuItemMsgID(%d) ", item];
147 if (menu > 0) [s appendFormat:@"AddMenuMsgID(%d) ", menu];
148 if (enable > 0) [s appendFormat:@"EnableMenuItemMsgID(%d) ", enable];
149 if (remove > 0) [s appendFormat:@"RemoveMenuItemMsgID(%d) ", remove];
150 if (sets > 0) [s appendFormat:@"SetScrollbarPositionMsgID(%d) ", sets];
151 if (sett > 0) [s appendFormat:@"SetScrollbarThumbMsgID(%d) ", sett];
152 if (shows > 0) [s appendFormat:@"ShowScrollbarMsgID(%d) ", shows];
153 if (cres > 0) [s appendFormat:@"CreateScrollbarMsgID(%d) ", cres];
154 if (dess > 0) [s appendFormat:@"DestroyScrollbarMsgID(%d) ", dess];
156 return [s autorelease];
162 @implementation NSString (MMExtras)
164 - (NSString *)stringByEscapingSpecialFilenameCharacters
166 // NOTE: This code assumes that no characters already have been escaped.
167 NSMutableString *string = [self mutableCopy];
169 [string replaceOccurrencesOfString:@"\\"
171 options:NSLiteralSearch
172 range:NSMakeRange(0, [string length])];
173 [string replaceOccurrencesOfString:@" "
175 options:NSLiteralSearch
176 range:NSMakeRange(0, [string length])];
177 [string replaceOccurrencesOfString:@"\t"
179 options:NSLiteralSearch
180 range:NSMakeRange(0, [string length])];
181 [string replaceOccurrencesOfString:@"%"
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])];
198 return [string autorelease];
201 @end // NSString (MMExtras)
205 @implementation NSColor (MMExtras)
207 + (NSColor *)colorWithRgbInt:(unsigned)rgb
209 float r = ((rgb>>16) & 0xff)/255.0f;
210 float g = ((rgb>>8) & 0xff)/255.0f;
211 float b = (rgb & 0xff)/255.0f;
213 return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:1.0f];
216 + (NSColor *)colorWithArgbInt:(unsigned)argb
218 float a = ((argb>>24) & 0xff)/255.0f;
219 float r = ((argb>>16) & 0xff)/255.0f;
220 float g = ((argb>>8) & 0xff)/255.0f;
221 float b = (argb & 0xff)/255.0f;
223 return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:a];
226 @end // NSColor (MMExtras)
231 @implementation NSDictionary (MMExtras)
233 + (id)dictionaryWithData:(NSData *)data
235 id plist = [NSPropertyListSerialization
236 propertyListFromData:data
237 mutabilityOption:NSPropertyListImmutable
239 errorDescription:NULL];
241 return [plist isKindOfClass:[NSDictionary class]] ? plist : nil;
244 - (NSData *)dictionaryAsData
246 return [NSPropertyListSerialization dataFromPropertyList:self
247 format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL];
255 @implementation NSMutableDictionary (MMExtras)
257 + (id)dictionaryWithData:(NSData *)data
259 id plist = [NSPropertyListSerialization
260 propertyListFromData:data
261 mutabilityOption:NSPropertyListMutableContainers
263 errorDescription:NULL];
265 return [plist isKindOfClass:[NSMutableDictionary class]] ? plist : nil;
276 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
278 // Allow for changing the log level via user defaults. If no key is found
279 // the default log level will be used (which for ASL is to log everything
280 // up to ASL_LEVEL_NOTICE). This key is an integer which corresponds to
281 // the ASL_LEVEL_* macros (0 is most severe, 7 is debug level).
282 id logLevelObj = [ud objectForKey:MMLogLevelKey];
284 int logLevel = [logLevelObj intValue];
285 if (logLevel < 0) logLevel = 0;
286 if (logLevel > ASL_LEVEL_DEBUG) logLevel = ASL_LEVEL_DEBUG;
288 ASLogLevel = logLevel;
289 asl_set_filter(NULL, ASL_FILTER_MASK_UPTO(logLevel));
292 // Allow for changing whether a copy of each log should be sent to stderr
293 // (this defaults to NO if this key is missing in the user defaults
294 // database). The above filter mask is applied to logs going to stderr,
295 // contrary to how "vanilla" ASL works.
296 BOOL logToStdErr = [ud boolForKey:MMLogToStdErrKey];
298 asl_add_log_file(NULL, 2); // The file descriptor for stderr is 2