Add MMZoomBoth user default
[MacVim.git] / src / MacVim / MacVim.m
blob8ec6aa5a1e2ce84428ac76e1f0180d6ebb0478de
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";
101 NSString *MMUntitledWindowKey           = @"MMUntitledWindow";
102 NSString *MMTexturedWindowKey           = @"MMTexturedWindow";
103 NSString *MMZoomBothKey                 = @"MMZoomBoth";
108     ATSFontContainerRef
109 loadFonts()
111     // This loads all fonts from the Resources folder.  The fonts are only
112     // available to the process which loaded them, so loading has to be done
113     // once for MacVim and an additional time for each Vim process.  The
114     // returned container ref should be used to deactiave the font.
115     //
116     // (Code taken from cocoadev.com)
117     ATSFontContainerRef fontContainerRef = 0;
118     NSString *fontsFolder = [[NSBundle mainBundle] resourcePath];    
119     if (fontsFolder) {
120         NSURL *fontsURL = [NSURL fileURLWithPath:fontsFolder];
121         if (fontsURL) {
122             FSRef fsRef;
123             FSSpec fsSpec;
124             CFURLGetFSRef((CFURLRef)fontsURL, &fsRef);
126             if (FSGetCatalogInfo(&fsRef, kFSCatInfoNone, NULL, NULL, &fsSpec,
127                         NULL) == noErr) {
128                 ATSFontActivateFromFileSpecification(&fsSpec,
129                         kATSFontContextLocal, kATSFontFormatUnspecified, NULL,
130                         kATSOptionFlagsDefault, &fontContainerRef);
131             }
132         }
133     }
135     return fontContainerRef;
141     NSString *
142 buildTabDropCommand(NSArray *filenames)
144     // Create a command line string that will open the specified files in tabs.
146     if (!filenames || [filenames count] == 0)
147         return [NSString string];
149     NSMutableString *cmd = [NSMutableString stringWithString:
150             @"<C-\\><C-N>:tab drop"];
152     NSEnumerator *e = [filenames objectEnumerator];
153     id o;
154     while ((o = [e nextObject])) {
155         NSString *file = [o stringByEscapingSpecialFilenameCharacters];
156         [cmd appendString:@" "];
157         [cmd appendString:file];
158     }
160     [cmd appendString:@"|redr|f<CR>"];
162     return cmd;
165     NSString *
166 buildSelectRangeCommand(NSRange range)
168     // Build a command line string that will select the given range of lines.
169     // If range.length == 0, then position the cursor on the given line but do
170     // not select.
172     if (range.location == NSNotFound)
173         return [NSString string];
175     NSString *cmd;
176     if (range.length > 0) {
177         cmd = [NSString stringWithFormat:@"<C-\\><C-N>%dGV%dGz.0",
178                 NSMaxRange(range), range.location];
179     } else {
180         cmd = [NSString stringWithFormat:@"<C-\\><C-N>%dGz.0", range.location];
181     }
183     return cmd;
186     NSString *
187 buildSearchTextCommand(NSString *searchText)
189     // TODO: Searching is an exclusive motion, so if the pattern would match on
190     // row 0 column 0 then this pattern will miss that match.
191     return [NSString stringWithFormat:@"<C-\\><C-N>gg/\\c%@<CR>", searchText];
197 @implementation NSString (MMExtras)
199 - (NSString *)stringByEscapingSpecialFilenameCharacters
201     // NOTE: This code assumes that no characters already have been escaped.
202     NSMutableString *string = [self mutableCopy];
204     [string replaceOccurrencesOfString:@"\\"
205                             withString:@"\\\\"
206                                options:NSLiteralSearch
207                                  range:NSMakeRange(0, [string length])];
208     [string replaceOccurrencesOfString:@" "
209                             withString:@"\\ "
210                                options:NSLiteralSearch
211                                  range:NSMakeRange(0, [string length])];
212     [string replaceOccurrencesOfString:@"\t"
213                             withString:@"\\\t "
214                                options:NSLiteralSearch
215                                  range:NSMakeRange(0, [string length])];
216     [string replaceOccurrencesOfString:@"%"
217                             withString:@"\\%"
218                                options:NSLiteralSearch
219                                  range:NSMakeRange(0, [string length])];
220     [string replaceOccurrencesOfString:@"#"
221                             withString:@"\\#"
222                                options:NSLiteralSearch
223                                  range:NSMakeRange(0, [string length])];
224     [string replaceOccurrencesOfString:@"|"
225                             withString:@"\\|"
226                                options:NSLiteralSearch
227                                  range:NSMakeRange(0, [string length])];
228     [string replaceOccurrencesOfString:@"\""
229                             withString:@"\\\""
230                                options:NSLiteralSearch
231                                  range:NSMakeRange(0, [string length])];
233     return [string autorelease];
236 @end // NSString (MMExtras)
240 @implementation NSIndexSet (MMExtras)
242 + (id)indexSetWithVimList:(NSString *)list
244     NSMutableIndexSet *idxSet = [NSMutableIndexSet indexSet];
245     NSArray *array = [list componentsSeparatedByString:@"\n"];
246     unsigned i, count = [array count];
248     for (i = 0; i < count; ++i) {
249         NSString *entry = [array objectAtIndex:i];
250         if ([entry intValue] > 0)
251             [idxSet addIndex:i];
252     }
254     return idxSet;
257 @end // NSIndexSet (MMExtras)
262 @implementation NSColor (MMExtras)
264 + (NSColor *)colorWithRgbInt:(unsigned)rgb
266     float r = ((rgb>>16) & 0xff)/255.0f;
267     float g = ((rgb>>8) & 0xff)/255.0f;
268     float b = (rgb & 0xff)/255.0f;
270     return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:1.0f];
273 + (NSColor *)colorWithArgbInt:(unsigned)argb
275     float a = ((argb>>24) & 0xff)/255.0f;
276     float r = ((argb>>16) & 0xff)/255.0f;
277     float g = ((argb>>8) & 0xff)/255.0f;
278     float b = (argb & 0xff)/255.0f;
280     return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:a];
283 @end // NSColor (MMExtras)