Modify how and when preloaded windows open
[MacVim.git] / src / MacVim / MacVim.m
blob6dda85da0e64cd7feae23e0038bcc904189c95d6
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"
16 char *MessageStrings[] = 
18     "INVALID MESSAGE ID",
19     "OpenWindowMsgID",
20     "InsertTextMsgID",
21     "KeyDownMsgID",
22     "CmdKeyMsgID",
23     "BatchDrawMsgID",
24     "SelectTabMsgID",
25     "CloseTabMsgID",
26     "AddNewTabMsgID",
27     "DraggedTabMsgID",
28     "UpdateTabBarMsgID",
29     "ShowTabBarMsgID",
30     "HideTabBarMsgID",
31     "SetTextDimensionsMsgID",
32     "SetWindowTitleMsgID",
33     "ScrollWheelMsgID",
34     "MouseDownMsgID",
35     "MouseUpMsgID",
36     "MouseDraggedMsgID",
37     "FlushQueueMsgID",
38     "AddMenuMsgID",
39     "AddMenuItemMsgID",
40     "RemoveMenuItemMsgID",
41     "EnableMenuItemMsgID",
42     "ExecuteMenuMsgID",
43     "ShowToolbarMsgID",
44     "ToggleToolbarMsgID",
45     "CreateScrollbarMsgID",
46     "DestroyScrollbarMsgID",
47     "ShowScrollbarMsgID",
48     "SetScrollbarPositionMsgID",
49     "SetScrollbarThumbMsgID",
50     "ScrollbarEventMsgID",
51     "SetFontMsgID",
52     "SetWideFontMsgID",
53     "VimShouldCloseMsgID",
54     "SetDefaultColorsMsgID",
55     "ExecuteActionMsgID",
56     "DropFilesMsgID",
57     "DropStringMsgID",
58     "ShowPopupMenuMsgID",
59     "GotFocusMsgID",
60     "LostFocusMsgID",
61     "MouseMovedMsgID",
62     "SetMouseShapeMsgID",
63     "AdjustLinespaceMsgID",
64     "ActivateMsgID",
65     "SetServerNameMsgID",
66     "EnterFullscreenMsgID",
67     "LeaveFullscreenMsgID",
68     "BuffersNotModifiedMsgID",
69     "BuffersModifiedMsgID",
70     "AddInputMsgID",
71     "SetPreEditPositionMsgID",
72     "TerminateNowMsgID",
73     "XcodeModMsgID",
74     "LiveResizeMsgID",
75     "EnableAntialiasMsgID",
76     "DisableAntialiasMsgID",
77     "SetVimStateMsgID",
78     "SetDocumentFilenameMsgID",
79     "OpenWithArgumentsMsgID",
80     "CloseWindowMsgID",
86 // Argument used to stop MacVim from opening an empty window on startup
87 // (techincally this is a user default but should not be used as such).
88 NSString *MMNoWindowKey = @"MMNoWindow";
90 // Vim pasteboard type (holds motion type + string)
91 NSString *VimPBoardType = @"VimPBoardType";
96     ATSFontContainerRef
97 loadFonts()
99     // This loads all fonts from the Resources folder.  The fonts are only
100     // available to the process which loaded them, so loading has to be done
101     // once for MacVim and an additional time for each Vim process.  The
102     // returned container ref should be used to deactiave the font.
103     //
104     // (Code taken from cocoadev.com)
105     ATSFontContainerRef fontContainerRef = 0;
106     NSString *fontsFolder = [[NSBundle mainBundle] resourcePath];    
107     if (fontsFolder) {
108         NSURL *fontsURL = [NSURL fileURLWithPath:fontsFolder];
109         if (fontsURL) {
110             FSRef fsRef;
111             FSSpec fsSpec;
112             CFURLGetFSRef((CFURLRef)fontsURL, &fsRef);
114             if (FSGetCatalogInfo(&fsRef, kFSCatInfoNone, NULL, NULL, &fsSpec,
115                         NULL) == noErr) {
116                 ATSFontActivateFromFileSpecification(&fsSpec,
117                         kATSFontContextLocal, kATSFontFormatUnspecified, NULL,
118                         kATSOptionFlagsDefault, &fontContainerRef);
119             }
120         }
121     }
123     return fontContainerRef;
129 @implementation NSString (MMExtras)
131 - (NSString *)stringByEscapingSpecialFilenameCharacters
133     // NOTE: This code assumes that no characters already have been escaped.
134     NSMutableString *string = [self mutableCopy];
136     [string replaceOccurrencesOfString:@"\\"
137                             withString:@"\\\\"
138                                options:NSLiteralSearch
139                                  range:NSMakeRange(0, [string length])];
140     [string replaceOccurrencesOfString:@" "
141                             withString:@"\\ "
142                                options:NSLiteralSearch
143                                  range:NSMakeRange(0, [string length])];
144     [string replaceOccurrencesOfString:@"\t"
145                             withString:@"\\\t "
146                                options:NSLiteralSearch
147                                  range:NSMakeRange(0, [string length])];
148     [string replaceOccurrencesOfString:@"%"
149                             withString:@"\\%"
150                                options:NSLiteralSearch
151                                  range:NSMakeRange(0, [string length])];
152     [string replaceOccurrencesOfString:@"#"
153                             withString:@"\\#"
154                                options:NSLiteralSearch
155                                  range:NSMakeRange(0, [string length])];
156     [string replaceOccurrencesOfString:@"|"
157                             withString:@"\\|"
158                                options:NSLiteralSearch
159                                  range:NSMakeRange(0, [string length])];
160     [string replaceOccurrencesOfString:@"\""
161                             withString:@"\\\""
162                                options:NSLiteralSearch
163                                  range:NSMakeRange(0, [string length])];
165     return [string autorelease];
168 @end // NSString (MMExtras)
172 @implementation NSColor (MMExtras)
174 + (NSColor *)colorWithRgbInt:(unsigned)rgb
176     float r = ((rgb>>16) & 0xff)/255.0f;
177     float g = ((rgb>>8) & 0xff)/255.0f;
178     float b = (rgb & 0xff)/255.0f;
180     return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:1.0f];
183 + (NSColor *)colorWithArgbInt:(unsigned)argb
185     float a = ((argb>>24) & 0xff)/255.0f;
186     float r = ((argb>>16) & 0xff)/255.0f;
187     float g = ((argb>>8) & 0xff)/255.0f;
188     float b = (argb & 0xff)/255.0f;
190     return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:a];
193 @end // NSColor (MMExtras)
198 @implementation NSDictionary (MMExtras)
200 + (id)dictionaryWithData:(NSData *)data
202     id plist = [NSPropertyListSerialization
203             propertyListFromData:data
204                 mutabilityOption:NSPropertyListImmutable
205                           format:NULL
206                 errorDescription:NULL];
208     return [plist isKindOfClass:[NSDictionary class]] ? plist : nil;
211 - (NSData *)dictionaryAsData
213     return [NSPropertyListSerialization dataFromPropertyList:self
214             format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL];
217 @end
222 @implementation NSMutableDictionary (MMExtras)
224 + (id)dictionaryWithData:(NSData *)data
226     id plist = [NSPropertyListSerialization
227             propertyListFromData:data
228                 mutabilityOption:NSPropertyListMutableContainers
229                           format:NULL
230                 errorDescription:NULL];
232     return [plist isKindOfClass:[NSMutableDictionary class]] ? plist : nil;
235 @end