Open and save dialogs track the Vim pwd
[MacVim.git] / src / MacVim / MacVim.m
blobd8b1c47013b4654421e7f3f5b4a8f38706880cbf
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",
76     "EnableAntialiasMsgID",
77     "DisableAntialiasMsgID",
78     "SetVimStateMsgID",
84 // NSUserDefaults keys
85 NSString *MMNoWindowKey                 = @"MMNoWindow";
86 NSString *MMTabMinWidthKey              = @"MMTabMinWidth";
87 NSString *MMTabMaxWidthKey              = @"MMTabMaxWidth";
88 NSString *MMTabOptimumWidthKey          = @"MMTabOptimumWidth";
89 NSString *MMTextInsetLeftKey            = @"MMTextInsetLeft";
90 NSString *MMTextInsetRightKey           = @"MMTextInsetRight";
91 NSString *MMTextInsetTopKey             = @"MMTextInsetTop";
92 NSString *MMTextInsetBottomKey          = @"MMTextInsetBottom";
93 NSString *MMTerminateAfterLastWindowClosedKey
94                                         = @"MMTerminateAfterLastWindowClosed";
95 NSString *MMTypesetterKey               = @"MMTypesetter";
96 NSString *MMCellWidthMultiplierKey      = @"MMCellWidthMultiplier";
97 NSString *MMBaselineOffsetKey           = @"MMBaselineOffset";
98 NSString *MMTranslateCtrlClickKey       = @"MMTranslateCtrlClick";
99 NSString *MMTopLeftPointKey             = @"MMTopLeftPoint";
100 NSString *MMOpenFilesInTabsKey          = @"MMOpenFilesInTabs";
101 NSString *MMNoFontSubstitutionKey       = @"MMNoFontSubstitution";
102 NSString *MMLoginShellKey               = @"MMLoginShell";
103 NSString *MMAtsuiRendererKey            = @"MMAtsuiRenderer";
104 NSString *MMUntitledWindowKey           = @"MMUntitledWindow";
105 NSString *MMTexturedWindowKey           = @"MMTexturedWindow";
106 NSString *MMZoomBothKey                 = @"MMZoomBoth";
107 NSString *MMCurrentPreferencePaneKey    = @"MMCurrentPreferencePane";
108 NSString *MMLoginShellCommandKey        = @"MMLoginShellCommand";
109 NSString *MMLoginShellArgumentKey       = @"MMLoginShellArgument";
110 NSString *MMDialogsTrackPwdKey          = @"MMDialogsTrackPwd";
115 // Vim pasteboard type (holds motion type + string)
116 NSString *VimPBoardType = @"VimPBoardType";
121     ATSFontContainerRef
122 loadFonts()
124     // This loads all fonts from the Resources folder.  The fonts are only
125     // available to the process which loaded them, so loading has to be done
126     // once for MacVim and an additional time for each Vim process.  The
127     // returned container ref should be used to deactiave the font.
128     //
129     // (Code taken from cocoadev.com)
130     ATSFontContainerRef fontContainerRef = 0;
131     NSString *fontsFolder = [[NSBundle mainBundle] resourcePath];    
132     if (fontsFolder) {
133         NSURL *fontsURL = [NSURL fileURLWithPath:fontsFolder];
134         if (fontsURL) {
135             FSRef fsRef;
136             FSSpec fsSpec;
137             CFURLGetFSRef((CFURLRef)fontsURL, &fsRef);
139             if (FSGetCatalogInfo(&fsRef, kFSCatInfoNone, NULL, NULL, &fsSpec,
140                         NULL) == noErr) {
141                 ATSFontActivateFromFileSpecification(&fsSpec,
142                         kATSFontContextLocal, kATSFontFormatUnspecified, NULL,
143                         kATSOptionFlagsDefault, &fontContainerRef);
144             }
145         }
146     }
148     return fontContainerRef;
154     NSString *
155 buildTabDropCommand(NSArray *filenames)
157     // Create a command line string that will open the specified files in tabs.
159     if (!filenames || [filenames count] == 0)
160         return [NSString string];
162     NSMutableString *cmd = [NSMutableString stringWithString:
163             @"<C-\\><C-N>:tab drop"];
165     NSEnumerator *e = [filenames objectEnumerator];
166     id o;
167     while ((o = [e nextObject])) {
168         NSString *file = [o stringByEscapingSpecialFilenameCharacters];
169         [cmd appendString:@" "];
170         [cmd appendString:file];
171     }
173     [cmd appendString:@"|redr|f<CR>"];
175     return cmd;
178     NSString *
179 buildSelectRangeCommand(NSRange range)
181     // Build a command line string that will select the given range of lines.
182     // If range.length == 0, then position the cursor on the given line but do
183     // not select.
185     if (range.location == NSNotFound)
186         return [NSString string];
188     NSString *cmd;
189     if (range.length > 0) {
190         cmd = [NSString stringWithFormat:@"<C-\\><C-N>%dGV%dGz.0",
191                 NSMaxRange(range), range.location];
192     } else {
193         cmd = [NSString stringWithFormat:@"<C-\\><C-N>%dGz.0", range.location];
194     }
196     return cmd;
199     NSString *
200 buildSearchTextCommand(NSString *searchText)
202     // TODO: Searching is an exclusive motion, so if the pattern would match on
203     // row 0 column 0 then this pattern will miss that match.
204     return [NSString stringWithFormat:@"<C-\\><C-N>gg/\\c%@<CR>", searchText];
210 @implementation NSString (MMExtras)
212 - (NSString *)stringByEscapingSpecialFilenameCharacters
214     // NOTE: This code assumes that no characters already have been escaped.
215     NSMutableString *string = [self mutableCopy];
217     [string replaceOccurrencesOfString:@"\\"
218                             withString:@"\\\\"
219                                options:NSLiteralSearch
220                                  range:NSMakeRange(0, [string length])];
221     [string replaceOccurrencesOfString:@" "
222                             withString:@"\\ "
223                                options:NSLiteralSearch
224                                  range:NSMakeRange(0, [string length])];
225     [string replaceOccurrencesOfString:@"\t"
226                             withString:@"\\\t "
227                                options:NSLiteralSearch
228                                  range:NSMakeRange(0, [string length])];
229     [string replaceOccurrencesOfString:@"%"
230                             withString:@"\\%"
231                                options:NSLiteralSearch
232                                  range:NSMakeRange(0, [string length])];
233     [string replaceOccurrencesOfString:@"#"
234                             withString:@"\\#"
235                                options:NSLiteralSearch
236                                  range:NSMakeRange(0, [string length])];
237     [string replaceOccurrencesOfString:@"|"
238                             withString:@"\\|"
239                                options:NSLiteralSearch
240                                  range:NSMakeRange(0, [string length])];
241     [string replaceOccurrencesOfString:@"\""
242                             withString:@"\\\""
243                                options:NSLiteralSearch
244                                  range:NSMakeRange(0, [string length])];
246     return [string autorelease];
249 @end // NSString (MMExtras)
253 @implementation NSIndexSet (MMExtras)
255 + (id)indexSetWithVimList:(NSString *)list
257     NSMutableIndexSet *idxSet = [NSMutableIndexSet indexSet];
258     NSArray *array = [list componentsSeparatedByString:@"\n"];
259     unsigned i, count = [array count];
261     for (i = 0; i < count; ++i) {
262         NSString *entry = [array objectAtIndex:i];
263         if ([entry intValue] > 0)
264             [idxSet addIndex:i];
265     }
267     return idxSet;
270 @end // NSIndexSet (MMExtras)
275 @implementation NSColor (MMExtras)
277 + (NSColor *)colorWithRgbInt:(unsigned)rgb
279     float r = ((rgb>>16) & 0xff)/255.0f;
280     float g = ((rgb>>8) & 0xff)/255.0f;
281     float b = (rgb & 0xff)/255.0f;
283     return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:1.0f];
286 + (NSColor *)colorWithArgbInt:(unsigned)argb
288     float a = ((argb>>24) & 0xff)/255.0f;
289     float r = ((argb>>16) & 0xff)/255.0f;
290     float g = ((argb>>8) & 0xff)/255.0f;
291     float b = (argb & 0xff)/255.0f;
293     return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:a];
296 @end // NSColor (MMExtras)
301 @implementation NSDocumentController (MMExtras)
303 - (void)noteNewRecentFilePath:(NSString *)path
305     NSURL *url = [NSURL fileURLWithPath:path];
306     if (url)
307         [self noteNewRecentDocumentURL:url];
310 @end // NSDocumentController (MMExtras)
315 @implementation NSDictionary (MMExtras)
317 + (id)dictionaryWithData:(NSData *)data
319     id plist = [NSPropertyListSerialization
320             propertyListFromData:data
321                 mutabilityOption:NSPropertyListImmutable
322                           format:NULL
323                 errorDescription:NULL];
325     return [plist isKindOfClass:[NSDictionary class]] ? plist : nil;
328 - (NSData *)dictionaryAsData
330     return [NSPropertyListSerialization dataFromPropertyList:self
331             format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL];
334 @end