Window and view refactoring
[MacVim.git] / src / MacVim / MacVim.m
blobd78bf433e5880ef95b68bb0f59e0b1113c664c20
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     "OpenVimWindowMsgID",
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     "ODBEditMsgID",
74     "XcodeModMsgID",
75     "LiveResizeMsgID",
81 // NSUserDefaults keys
82 NSString *MMNoWindowKey                 = @"MMNoWindow";
83 NSString *MMTabMinWidthKey              = @"MMTabMinWidth";
84 NSString *MMTabMaxWidthKey              = @"MMTabMaxWidth";
85 NSString *MMTabOptimumWidthKey          = @"MMTabOptimumWidth";
86 NSString *MMTextInsetLeftKey            = @"MMTextInsetLeft";
87 NSString *MMTextInsetRightKey           = @"MMTextInsetRight";
88 NSString *MMTextInsetTopKey             = @"MMTextInsetTop";
89 NSString *MMTextInsetBottomKey          = @"MMTextInsetBottom";
90 NSString *MMTerminateAfterLastWindowClosedKey
91                                         = @"MMTerminateAfterLastWindowClosed";
92 NSString *MMTypesetterKey               = @"MMTypesetter";
93 NSString *MMCellWidthMultiplierKey      = @"MMCellWidthMultiplier";
94 NSString *MMBaselineOffsetKey           = @"MMBaselineOffset";
95 NSString *MMTranslateCtrlClickKey       = @"MMTranslateCtrlClick";
96 NSString *MMTopLeftPointKey             = @"MMTopLeftPoint";
97 NSString *MMOpenFilesInTabsKey          = @"MMOpenFilesInTabs";
98 NSString *MMNoFontSubstitutionKey       = @"MMNoFontSubstitution";
99 NSString *MMLoginShellKey               = @"MMLoginShell";
100 NSString *MMAtsuiRendererKey            = @"MMAtsuiRenderer";
105     ATSFontContainerRef
106 loadFonts()
108     // This loads all fonts from the Resources folder.  The fonts are only
109     // available to the process which loaded them, so loading has to be done
110     // once for MacVim and an additional time for each Vim process.  The
111     // returned container ref should be used to deactiave the font.
112     //
113     // (Code taken from cocoadev.com)
114     ATSFontContainerRef fontContainerRef = 0;
115     NSString *fontsFolder = [[NSBundle mainBundle] resourcePath];    
116     if (fontsFolder) {
117         NSURL *fontsURL = [NSURL fileURLWithPath:fontsFolder];
118         if (fontsURL) {
119             FSRef fsRef;
120             FSSpec fsSpec;
121             CFURLGetFSRef((CFURLRef)fontsURL, &fsRef);
123             if (FSGetCatalogInfo(&fsRef, kFSCatInfoNone, NULL, NULL, &fsSpec,
124                         NULL) == noErr) {
125                 ATSFontActivateFromFileSpecification(&fsSpec,
126                         kATSFontContextLocal, kATSFontFormatUnspecified, NULL,
127                         kATSOptionFlagsDefault, &fontContainerRef);
128             }
129         }
130     }
132     return fontContainerRef;
138 @implementation NSString (MMExtras)
140 - (NSString *)stringByEscapingSpecialFilenameCharacters
142     // NOTE: This code assumes that no characters already have been escaped.
143     NSMutableString *string = [self mutableCopy];
145     [string replaceOccurrencesOfString:@"\\"
146                             withString:@"\\\\"
147                                options:NSLiteralSearch
148                                  range:NSMakeRange(0, [string length])];
149     [string replaceOccurrencesOfString:@" "
150                             withString:@"\\ "
151                                options:NSLiteralSearch
152                                  range:NSMakeRange(0, [string length])];
153     [string replaceOccurrencesOfString:@"\t"
154                             withString:@"\\\t "
155                                options:NSLiteralSearch
156                                  range:NSMakeRange(0, [string length])];
157     [string replaceOccurrencesOfString:@"%"
158                             withString:@"\\%"
159                                options:NSLiteralSearch
160                                  range:NSMakeRange(0, [string length])];
161     [string replaceOccurrencesOfString:@"#"
162                             withString:@"\\#"
163                                options:NSLiteralSearch
164                                  range:NSMakeRange(0, [string length])];
165     [string replaceOccurrencesOfString:@"|"
166                             withString:@"\\|"
167                                options:NSLiteralSearch
168                                  range:NSMakeRange(0, [string length])];
169     [string replaceOccurrencesOfString:@"\""
170                             withString:@"\\\""
171                                options:NSLiteralSearch
172                                  range:NSMakeRange(0, [string length])];
174     return [string autorelease];
177 @end // NSString (MMExtras)
181 @implementation NSIndexSet (MMExtras)
183 + (id)indexSetWithVimList:(NSString *)list
185     NSMutableIndexSet *idxSet = [NSMutableIndexSet indexSet];
186     NSArray *array = [list componentsSeparatedByString:@"\n"];
187     unsigned i, count = [array count];
189     for (i = 0; i < count; ++i) {
190         NSString *entry = [array objectAtIndex:i];
191         if ([entry intValue] > 0)
192             [idxSet addIndex:i];
193     }
195     return idxSet;
198 @end // NSIndexSet (MMExtras)
203 @implementation NSColor (MMExtras)
205 + (NSColor *)colorWithRgbInt:(unsigned)rgb
207     float r = ((rgb>>16) & 0xff)/255.0f;
208     float g = ((rgb>>8) & 0xff)/255.0f;
209     float b = (rgb & 0xff)/255.0f;
211     return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:1.0f];
214 + (NSColor *)colorWithArgbInt:(unsigned)argb
216     float a = ((argb>>24) & 0xff)/255.0f;
217     float r = ((argb>>16) & 0xff)/255.0f;
218     float g = ((argb>>8) & 0xff)/255.0f;
219     float b = (argb & 0xff)/255.0f;
221     return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:a];
224 @end // NSColor (MMExtras)