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.
12 #import "Miscellaneous.h"
16 // NSUserDefaults keys
17 NSString *MMTabMinWidthKey = @"MMTabMinWidth";
18 NSString *MMTabMaxWidthKey = @"MMTabMaxWidth";
19 NSString *MMTabOptimumWidthKey = @"MMTabOptimumWidth";
20 NSString *MMTextInsetLeftKey = @"MMTextInsetLeft";
21 NSString *MMTextInsetRightKey = @"MMTextInsetRight";
22 NSString *MMTextInsetTopKey = @"MMTextInsetTop";
23 NSString *MMTextInsetBottomKey = @"MMTextInsetBottom";
24 NSString *MMTypesetterKey = @"MMTypesetter";
25 NSString *MMCellWidthMultiplierKey = @"MMCellWidthMultiplier";
26 NSString *MMBaselineOffsetKey = @"MMBaselineOffset";
27 NSString *MMTranslateCtrlClickKey = @"MMTranslateCtrlClick";
28 NSString *MMTopLeftPointKey = @"MMTopLeftPoint";
29 NSString *MMOpenInCurrentWindowKey = @"MMOpenInCurrentWindow";
30 NSString *MMNoFontSubstitutionKey = @"MMNoFontSubstitution";
31 NSString *MMLoginShellKey = @"MMLoginShell";
32 NSString *MMAtsuiRendererKey = @"MMAtsuiRenderer";
33 NSString *MMUntitledWindowKey = @"MMUntitledWindow";
34 NSString *MMTexturedWindowKey = @"MMTexturedWindow";
35 NSString *MMZoomBothKey = @"MMZoomBoth";
36 NSString *MMCurrentPreferencePaneKey = @"MMCurrentPreferencePane";
37 NSString *MMLoginShellCommandKey = @"MMLoginShellCommand";
38 NSString *MMLoginShellArgumentKey = @"MMLoginShellArgument";
39 NSString *MMDialogsTrackPwdKey = @"MMDialogsTrackPwd";
40 #ifdef MM_ENABLE_PLUGINS
41 NSString *MMShowLeftPlugInContainerKey = @"MMShowLeftPlugInContainer";
43 NSString *MMOpenLayoutKey = @"MMOpenLayout";
44 NSString *MMVerticalSplitKey = @"MMVerticalSplit";
45 NSString *MMPreloadCacheSizeKey = @"MMPreloadCacheSize";
46 NSString *MMLastWindowClosedBehaviorKey = @"MMLastWindowClosedBehavior";
50 @implementation NSIndexSet (MMExtras)
52 + (id)indexSetWithVimList:(NSString *)list
54 NSMutableIndexSet *idxSet = [NSMutableIndexSet indexSet];
55 NSArray *array = [list componentsSeparatedByString:@"\n"];
56 unsigned i, count = [array count];
58 for (i = 0; i < count; ++i) {
59 NSString *entry = [array objectAtIndex:i];
60 if ([entry intValue] > 0)
67 @end // NSIndexSet (MMExtras)
72 @implementation NSDocumentController (MMExtras)
74 - (void)noteNewRecentFilePath:(NSString *)path
76 NSURL *url = [NSURL fileURLWithPath:path];
78 [self noteNewRecentDocumentURL:url];
81 - (void)noteNewRecentFilePaths:(NSArray *)paths
83 NSEnumerator *e = [paths objectEnumerator];
85 while ((path = [e nextObject]))
86 [self noteNewRecentFilePath:path];
89 @end // NSDocumentController (MMExtras)
94 @implementation NSOpenPanel (MMExtras)
96 - (void)hiddenFilesButtonToggled:(id)sender
98 [self setShowsHiddenFiles:[sender intValue]];
101 - (void)setShowsHiddenFiles:(BOOL)show
103 // This is undocumented stuff, so be careful. This does the same as
104 // [[self _navView] setShowsHiddenFiles:show];
105 // but does not produce warnings.
107 if (![self respondsToSelector:@selector(_navView)])
110 id navView = [self performSelector:@selector(_navView)];
111 if (![navView respondsToSelector:@selector(setShowsHiddenFiles:)])
114 // performSelector:withObject: does not support a BOOL
115 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
116 [navView methodSignatureForSelector:@selector(setShowsHiddenFiles:)]];
117 [invocation setTarget:navView];
118 [invocation setSelector:@selector(setShowsHiddenFiles:)];
119 [invocation setArgument:&show atIndex:2];
123 @end // NSOpenPanel (MMExtras)
128 @implementation NSMenu (MMExtras)
130 - (int)indexOfItemWithAction:(SEL)action
132 int i, count = [self numberOfItems];
133 for (i = 0; i < count; ++i) {
134 NSMenuItem *item = [self itemAtIndex:i];
135 if ([item action] == action)
142 - (NSMenuItem *)itemWithAction:(SEL)action
144 int idx = [self indexOfItemWithAction:action];
145 return idx >= 0 ? [self itemAtIndex:idx] : nil;
148 - (NSMenu *)findMenuContainingItemWithAction:(SEL)action
150 // NOTE: We only look for the action in the submenus of 'self'
151 int i, count = [self numberOfItems];
152 for (i = 0; i < count; ++i) {
153 NSMenu *menu = [[self itemAtIndex:i] submenu];
154 NSMenuItem *item = [menu itemWithAction:action];
155 if (item) return menu;
161 - (NSMenu *)findWindowsMenu
163 return [self findMenuContainingItemWithAction:
164 @selector(performMiniaturize:)];
167 - (NSMenu *)findApplicationMenu
169 // TODO: Just return [self itemAtIndex:0]?
170 return [self findMenuContainingItemWithAction:@selector(terminate:)];
173 - (NSMenu *)findServicesMenu
175 // NOTE! Our heuristic for finding the "Services" menu is to look for the
176 // second item before the "Hide MacVim" menu item on the "MacVim" menu.
177 // (The item before "Hide MacVim" should be a separator, but this is not
178 // important as long as the item before that is the "Services" menu.)
180 NSMenu *appMenu = [self findApplicationMenu];
181 if (!appMenu) return nil;
183 int idx = [appMenu indexOfItemWithAction: @selector(hide:)];
184 if (idx-2 < 0) return nil; // idx == -1, if selector not found
186 return [[appMenu itemAtIndex:idx-2] submenu];
189 - (NSMenu *)findFileMenu
191 return [self findMenuContainingItemWithAction:@selector(performClose:)];
194 @end // NSMenu (MMExtras)
199 @implementation NSToolbar (MMExtras)
201 - (int)indexOfItemWithItemIdentifier:(NSString *)identifier
203 NSArray *items = [self items];
204 int i, count = [items count];
205 for (i = 0; i < count; ++i) {
206 id item = [items objectAtIndex:i];
207 if ([[item itemIdentifier] isEqual:identifier])
214 - (NSToolbarItem *)itemAtIndex:(int)idx
216 NSArray *items = [self items];
217 if (idx < 0 || idx >= [items count])
220 return [items objectAtIndex:idx];
223 - (NSToolbarItem *)itemWithItemIdentifier:(NSString *)identifier
225 int idx = [self indexOfItemWithItemIdentifier:identifier];
226 return idx != NSNotFound ? [self itemAtIndex:idx] : nil;
229 @end // NSToolbar (MMExtras)
234 @implementation NSTabView (MMExtras)
236 - (void)removeAllTabViewItems
238 NSArray *existingItems = [self tabViewItems];
239 NSEnumerator *e = [existingItems objectEnumerator];
241 while (item = [e nextObject]){
242 [self removeTabViewItem:item];
246 @end // NSTabView (MMExtras)
251 @implementation NSNumber (MMExtras)
255 return [self intValue];
258 @end // NSNumber (MMExtras)
264 openPanelAccessoryView()
266 // Return a new button object for each NSOpenPanel -- several of them
267 // could be displayed at once.
268 // If the accessory view should get more complex, it should probably be
269 // loaded from a nib file.
270 NSButton *button = [[[NSButton alloc]
271 initWithFrame:NSMakeRect(0, 0, 140, 18)] autorelease];
273 NSLocalizedString(@"Show Hidden Files", @"Open File Dialog")];
274 [button setButtonType:NSSwitchButton];
276 [button setTarget:nil];
277 [button setAction:@selector(hiddenFilesButtonToggled:)];
279 // use the regular control size (checkbox is a bit smaller without this)
280 NSControlSize buttonSize = NSRegularControlSize;
281 float fontSize = [NSFont systemFontSizeForControlSize:buttonSize];
282 NSCell *theCell = [button cell];
283 NSFont *theFont = [NSFont fontWithName:[[theCell font] fontName]
285 [theCell setFont:theFont];
286 [theCell setControlSize:buttonSize];