Keep whole window visible on ":set lines=X columns=Y"
[MacVim.git] / src / MacVim / MacVim.m
blob51ac78b47365b7482507fb927568699a689f7c31
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     "SetTextRowsMsgID",
32     "SetTextColumsMsgID",
33     "SetTextDimensionsMsgID",
34     "LiveResizeMsgID",
35     "SetTextDimensionsReplyMsgID",
36     "SetWindowTitleMsgID",
37     "ScrollWheelMsgID",
38     "MouseDownMsgID",
39     "MouseUpMsgID",
40     "MouseDraggedMsgID",
41     "FlushQueueMsgID",
42     "AddMenuMsgID",
43     "AddMenuItemMsgID",
44     "RemoveMenuItemMsgID",
45     "EnableMenuItemMsgID",
46     "ExecuteMenuMsgID",
47     "ShowToolbarMsgID",
48     "ToggleToolbarMsgID",
49     "CreateScrollbarMsgID",
50     "DestroyScrollbarMsgID",
51     "ShowScrollbarMsgID",
52     "SetScrollbarPositionMsgID",
53     "SetScrollbarThumbMsgID",
54     "ScrollbarEventMsgID",
55     "SetFontMsgID",
56     "SetWideFontMsgID",
57     "VimShouldCloseMsgID",
58     "SetDefaultColorsMsgID",
59     "ExecuteActionMsgID",
60     "DropFilesMsgID",
61     "DropStringMsgID",
62     "ShowPopupMenuMsgID",
63     "GotFocusMsgID",
64     "LostFocusMsgID",
65     "MouseMovedMsgID",
66     "SetMouseShapeMsgID",
67     "AdjustLinespaceMsgID",
68     "ActivateMsgID",
69     "SetServerNameMsgID",
70     "EnterFullscreenMsgID",
71     "LeaveFullscreenMsgID",
72     "BuffersNotModifiedMsgID",
73     "BuffersModifiedMsgID",
74     "AddInputMsgID",
75     "SetPreEditPositionMsgID",
76     "TerminateNowMsgID",
77     "XcodeModMsgID",
78     "EnableAntialiasMsgID",
79     "DisableAntialiasMsgID",
80     "SetVimStateMsgID",
81     "SetDocumentFilenameMsgID",
82     "OpenWithArgumentsMsgID",
83     "CloseWindowMsgID",
84     "InterruptMsgID",
85     "SetFullscreenColorMsgID",
86     "ShowFindReplaceDialogMsgID",
87     "FindReplaceMsgID",
93 // Argument used to stop MacVim from opening an empty window on startup
94 // (techincally this is a user default but should not be used as such).
95 NSString *MMNoWindowKey = @"MMNoWindow";
97 // Vim pasteboard type (holds motion type + string)
98 NSString *VimPBoardType = @"VimPBoardType";
103     ATSFontContainerRef
104 loadFonts()
106     // This loads all fonts from the Resources folder.  The fonts are only
107     // available to the process which loaded them, so loading has to be done
108     // once for MacVim and an additional time for each Vim process.  The
109     // returned container ref should be used to deactiave the font.
110     //
111     // (Code taken from cocoadev.com)
112     ATSFontContainerRef fontContainerRef = 0;
113     NSString *fontsFolder = [[NSBundle mainBundle] resourcePath];    
114     if (fontsFolder) {
115         NSURL *fontsURL = [NSURL fileURLWithPath:fontsFolder];
116         if (fontsURL) {
117             FSRef fsRef;
118             FSSpec fsSpec;
119             CFURLGetFSRef((CFURLRef)fontsURL, &fsRef);
121             if (FSGetCatalogInfo(&fsRef, kFSCatInfoNone, NULL, NULL, &fsSpec,
122                         NULL) == noErr) {
123                 ATSFontActivateFromFileSpecification(&fsSpec,
124                         kATSFontContextLocal, kATSFontFormatUnspecified, NULL,
125                         kATSOptionFlagsDefault, &fontContainerRef);
126             }
127         }
128     }
130     return fontContainerRef;
136 @implementation NSString (MMExtras)
138 - (NSString *)stringByEscapingSpecialFilenameCharacters
140     // NOTE: This code assumes that no characters already have been escaped.
141     NSMutableString *string = [self mutableCopy];
143     [string replaceOccurrencesOfString:@"\\"
144                             withString:@"\\\\"
145                                options:NSLiteralSearch
146                                  range:NSMakeRange(0, [string length])];
147     [string replaceOccurrencesOfString:@" "
148                             withString:@"\\ "
149                                options:NSLiteralSearch
150                                  range:NSMakeRange(0, [string length])];
151     [string replaceOccurrencesOfString:@"\t"
152                             withString:@"\\\t "
153                                options:NSLiteralSearch
154                                  range:NSMakeRange(0, [string length])];
155     [string replaceOccurrencesOfString:@"%"
156                             withString:@"\\%"
157                                options:NSLiteralSearch
158                                  range:NSMakeRange(0, [string length])];
159     [string replaceOccurrencesOfString:@"#"
160                             withString:@"\\#"
161                                options:NSLiteralSearch
162                                  range:NSMakeRange(0, [string length])];
163     [string replaceOccurrencesOfString:@"|"
164                             withString:@"\\|"
165                                options:NSLiteralSearch
166                                  range:NSMakeRange(0, [string length])];
167     [string replaceOccurrencesOfString:@"\""
168                             withString:@"\\\""
169                                options:NSLiteralSearch
170                                  range:NSMakeRange(0, [string length])];
172     return [string autorelease];
175 @end // NSString (MMExtras)
179 @implementation NSColor (MMExtras)
181 + (NSColor *)colorWithRgbInt:(unsigned)rgb
183     float r = ((rgb>>16) & 0xff)/255.0f;
184     float g = ((rgb>>8) & 0xff)/255.0f;
185     float b = (rgb & 0xff)/255.0f;
187     return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:1.0f];
190 + (NSColor *)colorWithArgbInt:(unsigned)argb
192     float a = ((argb>>24) & 0xff)/255.0f;
193     float r = ((argb>>16) & 0xff)/255.0f;
194     float g = ((argb>>8) & 0xff)/255.0f;
195     float b = (argb & 0xff)/255.0f;
197     return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:a];
200 @end // NSColor (MMExtras)
205 @implementation NSDictionary (MMExtras)
207 + (id)dictionaryWithData:(NSData *)data
209     id plist = [NSPropertyListSerialization
210             propertyListFromData:data
211                 mutabilityOption:NSPropertyListImmutable
212                           format:NULL
213                 errorDescription:NULL];
215     return [plist isKindOfClass:[NSDictionary class]] ? plist : nil;
218 - (NSData *)dictionaryAsData
220     return [NSPropertyListSerialization dataFromPropertyList:self
221             format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL];
224 @end
229 @implementation NSMutableDictionary (MMExtras)
231 + (id)dictionaryWithData:(NSData *)data
233     id plist = [NSPropertyListSerialization
234             propertyListFromData:data
235                 mutabilityOption:NSPropertyListMutableContainers
236                           format:NULL
237                 errorDescription:NULL];
239     return [plist isKindOfClass:[NSMutableDictionary class]] ? plist : nil;
242 @end