Remove feat/core-text from README-repo.txt
[MacVim.git] / src / MacVim / MacVim.m
blob6adbd558ee97b31e4d5b35d767e035ac67213207
1 /* vi:set ts=8 sts=4 sw=4 ft=objc:
2  *
3  * VIM - Vi IMproved            by Bram Moolenaar
4  *                              MacVim GUI port by Bjorn Winckler
5  *
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.
9  */
11  * MacVim.m:  Code shared between Vim and MacVim.
12  */
14 #import "MacVim.h"
17 char *MessageStrings[] = 
19     "INVALID MESSAGE ID",
20     "OpenWindowMsgID",
21     "KeyDownMsgID",
22     "BatchDrawMsgID",
23     "SelectTabMsgID",
24     "CloseTabMsgID",
25     "AddNewTabMsgID",
26     "DraggedTabMsgID",
27     "UpdateTabBarMsgID",
28     "ShowTabBarMsgID",
29     "HideTabBarMsgID",
30     "SetTextRowsMsgID",
31     "SetTextColumnsMsgID",
32     "SetTextDimensionsMsgID",
33     "LiveResizeMsgID",
34     "SetTextDimensionsReplyMsgID",
35     "SetWindowTitleMsgID",
36     "ScrollWheelMsgID",
37     "MouseDownMsgID",
38     "MouseUpMsgID",
39     "MouseDraggedMsgID",
40     "FlushQueueMsgID",
41     "AddMenuMsgID",
42     "AddMenuItemMsgID",
43     "RemoveMenuItemMsgID",
44     "EnableMenuItemMsgID",
45     "ExecuteMenuMsgID",
46     "ShowToolbarMsgID",
47     "ToggleToolbarMsgID",
48     "CreateScrollbarMsgID",
49     "DestroyScrollbarMsgID",
50     "ShowScrollbarMsgID",
51     "SetScrollbarPositionMsgID",
52     "SetScrollbarThumbMsgID",
53     "ScrollbarEventMsgID",
54     "SetFontMsgID",
55     "SetWideFontMsgID",
56     "VimShouldCloseMsgID",
57     "SetDefaultColorsMsgID",
58     "ExecuteActionMsgID",
59     "DropFilesMsgID",
60     "DropStringMsgID",
61     "ShowPopupMenuMsgID",
62     "GotFocusMsgID",
63     "LostFocusMsgID",
64     "MouseMovedMsgID",
65     "SetMouseShapeMsgID",
66     "AdjustLinespaceMsgID",
67     "ActivateMsgID",
68     "SetServerNameMsgID",
69     "EnterFullscreenMsgID",
70     "LeaveFullscreenMsgID",
71     "BuffersNotModifiedMsgID",
72     "BuffersModifiedMsgID",
73     "AddInputMsgID",
74     "SetPreEditPositionMsgID",
75     "TerminateNowMsgID",
76     "XcodeModMsgID",
77     "EnableAntialiasMsgID",
78     "DisableAntialiasMsgID",
79     "SetVimStateMsgID",
80     "SetDocumentFilenameMsgID",
81     "OpenWithArgumentsMsgID",
82     "CloseWindowMsgID",
83     "SetFullscreenColorMsgID",
84     "ShowFindReplaceDialogMsgID",
85     "FindReplaceMsgID",
86     "ActivateKeyScriptMsgID",
87     "DeactivateKeyScriptMsgID",
88     "EnableImControlMsgID",
89     "DisableImControlMsgID",
90     "ActivatedImMsgID",
91     "DeactivatedImMsgID",
92     "BrowseForFileMsgID",
93     "ShowDialogMsgID",
94     "NetBeansMsgID",
95     "SetMarkedTextMsgID",
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).
123     NSString *
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)
134             continue;
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]];
145     }
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:@"\\"
170                             withString:@"\\\\"
171                                options:NSLiteralSearch
172                                  range:NSMakeRange(0, [string length])];
173     [string replaceOccurrencesOfString:@" "
174                             withString:@"\\ "
175                                options:NSLiteralSearch
176                                  range:NSMakeRange(0, [string length])];
177     [string replaceOccurrencesOfString:@"\t"
178                             withString:@"\\\t "
179                                options:NSLiteralSearch
180                                  range:NSMakeRange(0, [string length])];
181     [string replaceOccurrencesOfString:@"%"
182                             withString:@"\\%"
183                                options:NSLiteralSearch
184                                  range:NSMakeRange(0, [string length])];
185     [string replaceOccurrencesOfString:@"#"
186                             withString:@"\\#"
187                                options:NSLiteralSearch
188                                  range:NSMakeRange(0, [string length])];
189     [string replaceOccurrencesOfString:@"|"
190                             withString:@"\\|"
191                                options:NSLiteralSearch
192                                  range:NSMakeRange(0, [string length])];
193     [string replaceOccurrencesOfString:@"\""
194                             withString:@"\\\""
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
238                           format:NULL
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];
250 @end
255 @implementation NSMutableDictionary (MMExtras)
257 + (id)dictionaryWithData:(NSData *)data
259     id plist = [NSPropertyListSerialization
260             propertyListFromData:data
261                 mutabilityOption:NSPropertyListMutableContainers
262                           format:NULL
263                 errorDescription:NULL];
265     return [plist isKindOfClass:[NSMutableDictionary class]] ? plist : nil;
268 @end
273     void
274 ASLInit()
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];
283     if (logLevelObj) {
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));
290     }
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];
297     if (logToStdErr)
298         asl_add_log_file(NULL, 2);  // The file descriptor for stderr is 2