Prepared for ATSUI renderer
[MacVim.git] / src / MacVim / MacVim.m
blob36c1b3551a3e025ecda76c8f15494101f3f33a1d
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",
80 // NSUserDefaults keys
81 NSString *MMNoWindowKey                 = @"MMNoWindow";
82 NSString *MMTabMinWidthKey              = @"MMTabMinWidth";
83 NSString *MMTabMaxWidthKey              = @"MMTabMaxWidth";
84 NSString *MMTabOptimumWidthKey          = @"MMTabOptimumWidth";
85 NSString *MMTextInsetLeftKey            = @"MMTextInsetLeft";
86 NSString *MMTextInsetRightKey           = @"MMTextInsetRight";
87 NSString *MMTextInsetTopKey             = @"MMTextInsetTop";
88 NSString *MMTextInsetBottomKey          = @"MMTextInsetBottom";
89 NSString *MMTerminateAfterLastWindowClosedKey
90                                         = @"MMTerminateAfterLastWindowClosed";
91 NSString *MMTypesetterKey               = @"MMTypesetter";
92 NSString *MMCellWidthMultiplierKey      = @"MMCellWidthMultiplier";
93 NSString *MMBaselineOffsetKey           = @"MMBaselineOffset";
94 NSString *MMTranslateCtrlClickKey       = @"MMTranslateCtrlClick";
95 NSString *MMTopLeftPointKey             = @"MMTopLeftPoint";
96 NSString *MMOpenFilesInTabsKey          = @"MMOpenFilesInTabs";
97 NSString *MMNoFontSubstitutionKey       = @"MMNoFontSubstitution";
98 NSString *MMLoginShellKey               = @"MMLoginShell";
99 NSString *MMAtsuiRendererKey            = @"MMAtsuiRenderer";
104     ATSFontContainerRef
105 loadFonts()
107     // This loads all fonts from the Resources folder.  The fonts are only
108     // available to the process which loaded them, so loading has to be done
109     // once for MacVim and an additional time for each Vim process.  The
110     // returned container ref should be used to deactiave the font.
111     //
112     // (Code taken from cocoadev.com)
113     ATSFontContainerRef fontContainerRef = 0;
114     NSString *fontsFolder = [[NSBundle mainBundle] resourcePath];    
115     if (fontsFolder) {
116         NSURL *fontsURL = [NSURL fileURLWithPath:fontsFolder];
117         if (fontsURL) {
118             FSRef fsRef;
119             FSSpec fsSpec;
120             CFURLGetFSRef((CFURLRef)fontsURL, &fsRef);
122             if (FSGetCatalogInfo(&fsRef, kFSCatInfoNone, NULL, NULL, &fsSpec,
123                         NULL) == noErr) {
124                 ATSFontActivateFromFileSpecification(&fsSpec,
125                         kATSFontContextLocal, kATSFontFormatUnspecified, NULL,
126                         kATSOptionFlagsDefault, &fontContainerRef);
127             }
128         }
129     }
131     return fontContainerRef;
137 @implementation NSString (MMExtras)
139 - (NSString *)stringByEscapingSpecialFilenameCharacters
141     // NOTE: This code assumes that no characters already have been escaped.
142     NSMutableString *string = [self mutableCopy];
144     [string replaceOccurrencesOfString:@"\\"
145                             withString:@"\\\\"
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:@"\t"
153                             withString:@"\\\t "
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])];
164     [string replaceOccurrencesOfString:@"|"
165                             withString:@"\\|"
166                                options:NSLiteralSearch
167                                  range:NSMakeRange(0, [string length])];
168     [string replaceOccurrencesOfString:@"\""
169                             withString:@"\\\""
170                                options:NSLiteralSearch
171                                  range:NSMakeRange(0, [string length])];
173     return [string autorelease];
176 @end // NSString (MMExtras)
180 @implementation NSIndexSet (MMExtras)
182 + (id)indexSetWithVimList:(NSString *)list
184     NSMutableIndexSet *idxSet = [NSMutableIndexSet indexSet];
185     NSArray *array = [list componentsSeparatedByString:@"\n"];
186     unsigned i, count = [array count];
188     for (i = 0; i < count; ++i) {
189         NSString *entry = [array objectAtIndex:i];
190         if ([entry intValue] > 0)
191             [idxSet addIndex:i];
192     }
194     return idxSet;
197 @end // NSIndexSet (MMExtras)
202 @implementation NSColor (MMExtras)
204 + (NSColor *)colorWithRgbInt:(unsigned)rgb
206     float r = ((rgb>>16) & 0xff)/255.0f;
207     float g = ((rgb>>8) & 0xff)/255.0f;
208     float b = (rgb & 0xff)/255.0f;
210     return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:1.0f];
213 + (NSColor *)colorWithArgbInt:(unsigned)argb
215     float a = ((argb>>24) & 0xff)/255.0f;
216     float r = ((argb>>16) & 0xff)/255.0f;
217     float g = ((argb>>8) & 0xff)/255.0f;
218     float b = (argb & 0xff)/255.0f;
220     return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:a];
223 @end // NSColor (MMExtras)