1 /* vi:set ts=8 sts=4 sw=4 ft=objc:
3 * VIM - Vi IMproved by Bram Moolenaar
4 * MacVim GUI port by Bjorn Winckler
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.
11 * MacVim.m: Code shared between Vim and MacVim.
16 char *MessageStrings[] =
31 "SetTextDimensionsMsgID",
32 "SetWindowTitleMsgID",
40 "RemoveMenuItemMsgID",
41 "EnableMenuItemMsgID",
45 "CreateScrollbarMsgID",
46 "DestroyScrollbarMsgID",
48 "SetScrollbarPositionMsgID",
49 "SetScrollbarThumbMsgID",
50 "ScrollbarEventMsgID",
53 "VimShouldCloseMsgID",
54 "SetDefaultColorsMsgID",
63 "AdjustLinespaceMsgID",
66 "EnterFullscreenMsgID",
67 "LeaveFullscreenMsgID",
68 "BuffersNotModifiedMsgID",
69 "BuffersModifiedMsgID",
71 "SetPreEditPositionMsgID",
76 "EnableAntialiasMsgID",
77 "DisableAntialiasMsgID",
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";
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.
129 // (Code taken from cocoadev.com)
130 ATSFontContainerRef fontContainerRef = 0;
131 NSString *fontsFolder = [[NSBundle mainBundle] resourcePath];
133 NSURL *fontsURL = [NSURL fileURLWithPath:fontsFolder];
137 CFURLGetFSRef((CFURLRef)fontsURL, &fsRef);
139 if (FSGetCatalogInfo(&fsRef, kFSCatInfoNone, NULL, NULL, &fsSpec,
141 ATSFontActivateFromFileSpecification(&fsSpec,
142 kATSFontContextLocal, kATSFontFormatUnspecified, NULL,
143 kATSOptionFlagsDefault, &fontContainerRef);
148 return fontContainerRef;
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];
167 while ((o = [e nextObject])) {
168 NSString *file = [o stringByEscapingSpecialFilenameCharacters];
169 [cmd appendString:@" "];
170 [cmd appendString:file];
173 [cmd appendString:@"|redr|f<CR>"];
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
185 if (range.location == NSNotFound)
186 return [NSString string];
189 if (range.length > 0) {
190 cmd = [NSString stringWithFormat:@"<C-\\><C-N>%dGV%dGz.0",
191 NSMaxRange(range), range.location];
193 cmd = [NSString stringWithFormat:@"<C-\\><C-N>%dGz.0", range.location];
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:@"\\"
219 options:NSLiteralSearch
220 range:NSMakeRange(0, [string length])];
221 [string replaceOccurrencesOfString:@" "
223 options:NSLiteralSearch
224 range:NSMakeRange(0, [string length])];
225 [string replaceOccurrencesOfString:@"\t"
227 options:NSLiteralSearch
228 range:NSMakeRange(0, [string length])];
229 [string replaceOccurrencesOfString:@"%"
231 options:NSLiteralSearch
232 range:NSMakeRange(0, [string length])];
233 [string replaceOccurrencesOfString:@"#"
235 options:NSLiteralSearch
236 range:NSMakeRange(0, [string length])];
237 [string replaceOccurrencesOfString:@"|"
239 options:NSLiteralSearch
240 range:NSMakeRange(0, [string length])];
241 [string replaceOccurrencesOfString:@"\""
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)
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];
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
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];