File->Close sends performClose:
[MacVim.git] / src / MacVim / MacVim.m
blob23566f7786904636c77fa4619d9e6a5ac1494bc5
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";
107     ATSFontContainerRef
108 loadFonts()
110     // This loads all fonts from the Resources folder.  The fonts are only
111     // available to the process which loaded them, so loading has to be done
112     // once for MacVim and an additional time for each Vim process.  The
113     // returned container ref should be used to deactiave the font.
114     //
115     // (Code taken from cocoadev.com)
116     ATSFontContainerRef fontContainerRef = 0;
117     NSString *fontsFolder = [[NSBundle mainBundle] resourcePath];    
118     if (fontsFolder) {
119         NSURL *fontsURL = [NSURL fileURLWithPath:fontsFolder];
120         if (fontsURL) {
121             FSRef fsRef;
122             FSSpec fsSpec;
123             CFURLGetFSRef((CFURLRef)fontsURL, &fsRef);
125             if (FSGetCatalogInfo(&fsRef, kFSCatInfoNone, NULL, NULL, &fsSpec,
126                         NULL) == noErr) {
127                 ATSFontActivateFromFileSpecification(&fsSpec,
128                         kATSFontContextLocal, kATSFontFormatUnspecified, NULL,
129                         kATSOptionFlagsDefault, &fontContainerRef);
130             }
131         }
132     }
134     return fontContainerRef;
140     NSString *
141 buildTabDropCommand(NSArray *filenames)
143     // Create a command line string that will open the specified files in tabs.
145     if (!filenames || [filenames count] == 0)
146         return [NSString string];
148     NSMutableString *cmd = [NSMutableString stringWithString:
149             @"<C-\\><C-N>:tab drop"];
151     NSEnumerator *e = [filenames objectEnumerator];
152     id o;
153     while ((o = [e nextObject])) {
154         NSString *file = [o stringByEscapingSpecialFilenameCharacters];
155         [cmd appendString:@" "];
156         [cmd appendString:file];
157     }
159     [cmd appendString:@"|redr|f<CR>"];
161     return cmd;
164     NSString *
165 buildSelectRangeCommand(NSRange range)
167     // Build a command line string that will select the given range of lines.
168     // If range.length == 0, then position the cursor on the given line but do
169     // not select.
171     if (range.location == NSNotFound)
172         return [NSString string];
174     NSString *cmd;
175     if (range.length > 0) {
176         cmd = [NSString stringWithFormat:@"<C-\\><C-N>%dGV%dGz.0",
177                 NSMaxRange(range), range.location];
178     } else {
179         cmd = [NSString stringWithFormat:@"<C-\\><C-N>%dGz.0", range.location];
180     }
182     return cmd;
185     NSString *
186 buildSearchTextCommand(NSString *searchText)
188     // TODO: Searching is an exclusive motion, so if the pattern would match on
189     // row 0 column 0 then this pattern will miss that match.
190     return [NSString stringWithFormat:@"<C-\\><C-N>gg/\\c%@<CR>", searchText];
196 @implementation NSString (MMExtras)
198 - (NSString *)stringByEscapingSpecialFilenameCharacters
200     // NOTE: This code assumes that no characters already have been escaped.
201     NSMutableString *string = [self mutableCopy];
203     [string replaceOccurrencesOfString:@"\\"
204                             withString:@"\\\\"
205                                options:NSLiteralSearch
206                                  range:NSMakeRange(0, [string length])];
207     [string replaceOccurrencesOfString:@" "
208                             withString:@"\\ "
209                                options:NSLiteralSearch
210                                  range:NSMakeRange(0, [string length])];
211     [string replaceOccurrencesOfString:@"\t"
212                             withString:@"\\\t "
213                                options:NSLiteralSearch
214                                  range:NSMakeRange(0, [string length])];
215     [string replaceOccurrencesOfString:@"%"
216                             withString:@"\\%"
217                                options:NSLiteralSearch
218                                  range:NSMakeRange(0, [string length])];
219     [string replaceOccurrencesOfString:@"#"
220                             withString:@"\\#"
221                                options:NSLiteralSearch
222                                  range:NSMakeRange(0, [string length])];
223     [string replaceOccurrencesOfString:@"|"
224                             withString:@"\\|"
225                                options:NSLiteralSearch
226                                  range:NSMakeRange(0, [string length])];
227     [string replaceOccurrencesOfString:@"\""
228                             withString:@"\\\""
229                                options:NSLiteralSearch
230                                  range:NSMakeRange(0, [string length])];
232     return [string autorelease];
235 @end // NSString (MMExtras)
239 @implementation NSIndexSet (MMExtras)
241 + (id)indexSetWithVimList:(NSString *)list
243     NSMutableIndexSet *idxSet = [NSMutableIndexSet indexSet];
244     NSArray *array = [list componentsSeparatedByString:@"\n"];
245     unsigned i, count = [array count];
247     for (i = 0; i < count; ++i) {
248         NSString *entry = [array objectAtIndex:i];
249         if ([entry intValue] > 0)
250             [idxSet addIndex:i];
251     }
253     return idxSet;
256 @end // NSIndexSet (MMExtras)
261 @implementation NSColor (MMExtras)
263 + (NSColor *)colorWithRgbInt:(unsigned)rgb
265     float r = ((rgb>>16) & 0xff)/255.0f;
266     float g = ((rgb>>8) & 0xff)/255.0f;
267     float b = (rgb & 0xff)/255.0f;
269     return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:1.0f];
272 + (NSColor *)colorWithArgbInt:(unsigned)argb
274     float a = ((argb>>24) & 0xff)/255.0f;
275     float r = ((argb>>16) & 0xff)/255.0f;
276     float g = ((argb>>8) & 0xff)/255.0f;
277     float b = (argb & 0xff)/255.0f;
279     return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:a];
282 @end // NSColor (MMExtras)