More file opening options (plus quickstart feature)
[MacVim.git] / src / MacVim / Miscellaneous.m
blob95782b8d482d48e993accdfa8f7d08a7a275393c
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 #import "MacVim.h"
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 *MMTerminateAfterLastWindowClosedKey
25                                         = @"MMTerminateAfterLastWindowClosed";
26 NSString *MMTypesetterKey               = @"MMTypesetter";
27 NSString *MMCellWidthMultiplierKey      = @"MMCellWidthMultiplier";
28 NSString *MMBaselineOffsetKey           = @"MMBaselineOffset";
29 NSString *MMTranslateCtrlClickKey       = @"MMTranslateCtrlClick";
30 NSString *MMTopLeftPointKey             = @"MMTopLeftPoint";
31 NSString *MMOpenInCurrentWindowKey      = @"MMOpenInCurrentWindow";
32 NSString *MMNoFontSubstitutionKey       = @"MMNoFontSubstitution";
33 NSString *MMLoginShellKey               = @"MMLoginShell";
34 NSString *MMAtsuiRendererKey            = @"MMAtsuiRenderer";
35 NSString *MMUntitledWindowKey           = @"MMUntitledWindow";
36 NSString *MMTexturedWindowKey           = @"MMTexturedWindow";
37 NSString *MMZoomBothKey                 = @"MMZoomBoth";
38 NSString *MMCurrentPreferencePaneKey    = @"MMCurrentPreferencePane";
39 NSString *MMLoginShellCommandKey        = @"MMLoginShellCommand";
40 NSString *MMLoginShellArgumentKey       = @"MMLoginShellArgument";
41 NSString *MMDialogsTrackPwdKey          = @"MMDialogsTrackPwd";
42 #ifdef MM_ENABLE_PLUGINS
43 NSString *MMShowLeftPlugInContainerKey  = @"MMShowLeftPlugInContainer";
44 #endif
45 NSString *MMOpenLayoutKey               = @"MMOpenLayout";
46 NSString *MMVerticalSplitKey            = @"MMVerticalSplit";
47 NSString *MMPreloadCacheSizeKey         = @"MMPreloadCacheSize";
51 @implementation NSIndexSet (MMExtras)
53 + (id)indexSetWithVimList:(NSString *)list
55     NSMutableIndexSet *idxSet = [NSMutableIndexSet indexSet];
56     NSArray *array = [list componentsSeparatedByString:@"\n"];
57     unsigned i, count = [array count];
59     for (i = 0; i < count; ++i) {
60         NSString *entry = [array objectAtIndex:i];
61         if ([entry intValue] > 0)
62             [idxSet addIndex:i];
63     }
65     return idxSet;
68 @end // NSIndexSet (MMExtras)
73 @implementation NSDocumentController (MMExtras)
75 - (void)noteNewRecentFilePath:(NSString *)path
77     NSURL *url = [NSURL fileURLWithPath:path];
78     if (url)
79         [self noteNewRecentDocumentURL:url];
82 - (void)noteNewRecentFilePaths:(NSArray *)paths
84     NSEnumerator *e = [paths objectEnumerator];
85     NSString *path;
86     while ((path = [e nextObject]))
87         [self noteNewRecentFilePath:path];
90 @end // NSDocumentController (MMExtras)
95 @implementation NSOpenPanel (MMExtras)
97 - (void)hiddenFilesButtonToggled:(id)sender
99     [self setShowsHiddenFiles:[sender intValue]];
102 - (void)setShowsHiddenFiles:(BOOL)show
104     // This is undocumented stuff, so be careful. This does the same as
105     //     [[self _navView] setShowsHiddenFiles:show];
106     // but does not produce warnings.
108     if (![self respondsToSelector:@selector(_navView)])
109         return;
111     id navView = [self performSelector:@selector(_navView)];
112     if (![navView respondsToSelector:@selector(setShowsHiddenFiles:)])
113         return;
115     // performSelector:withObject: does not support a BOOL
116     NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
117         [navView methodSignatureForSelector:@selector(setShowsHiddenFiles:)]];
118     [invocation setTarget:navView];
119     [invocation setSelector:@selector(setShowsHiddenFiles:)];
120     [invocation setArgument:&show atIndex:2];
121     [invocation invoke];
124 @end // NSOpenPanel (MMExtras)
129 @implementation NSMenu (MMExtras)
131 - (int)indexOfItemWithAction:(SEL)action
133     int i, count = [self numberOfItems];
134     for (i = 0; i < count; ++i) {
135         NSMenuItem *item = [self itemAtIndex:i];
136         if ([item action] == action)
137             return i;
138     }
140     return -1;
143 - (NSMenuItem *)itemWithAction:(SEL)action
145     int idx = [self indexOfItemWithAction:action];
146     return idx >= 0 ? [self itemAtIndex:idx] : nil;
149 - (NSMenu *)findMenuContainingItemWithAction:(SEL)action
151     // NOTE: We only look for the action in the submenus of 'self'
152     int i, count = [self numberOfItems];
153     for (i = 0; i < count; ++i) {
154         NSMenu *menu = [[self itemAtIndex:i] submenu];
155         NSMenuItem *item = [menu itemWithAction:action];
156         if (item) return menu;
157     }
159     return nil;
162 - (NSMenu *)findWindowsMenu
164     return [self findMenuContainingItemWithAction:
165         @selector(performMiniaturize:)];
168 - (NSMenu *)findApplicationMenu
170     // TODO: Just return [self itemAtIndex:0]?
171     return [self findMenuContainingItemWithAction:@selector(terminate:)];
174 - (NSMenu *)findServicesMenu
176     // NOTE!  Our heuristic for finding the "Services" menu is to look for the
177     // second item before the "Hide MacVim" menu item on the "MacVim" menu.
178     // (The item before "Hide MacVim" should be a separator, but this is not
179     // important as long as the item before that is the "Services" menu.)
181     NSMenu *appMenu = [self findApplicationMenu];
182     if (!appMenu) return nil;
184     int idx = [appMenu indexOfItemWithAction: @selector(hide:)];
185     if (idx-2 < 0) return nil;  // idx == -1, if selector not found
187     return [[appMenu itemAtIndex:idx-2] submenu];
190 - (NSMenu *)findFileMenu
192     return [self findMenuContainingItemWithAction:@selector(performClose:)];
195 @end // NSMenu (MMExtras)
200 @implementation NSToolbar (MMExtras)
202 - (int)indexOfItemWithItemIdentifier:(NSString *)identifier
204     NSArray *items = [self items];
205     int i, count = [items count];
206     for (i = 0; i < count; ++i) {
207         id item = [items objectAtIndex:i];
208         if ([[item itemIdentifier] isEqual:identifier])
209             return i;
210     }
212     return NSNotFound;
215 - (NSToolbarItem *)itemAtIndex:(int)idx
217     NSArray *items = [self items];
218     if (idx < 0 || idx >= [items count])
219         return nil;
221     return [items objectAtIndex:idx];
224 - (NSToolbarItem *)itemWithItemIdentifier:(NSString *)identifier
226     int idx = [self indexOfItemWithItemIdentifier:identifier];
227     return idx != NSNotFound ? [self itemAtIndex:idx] : nil;
230 @end // NSToolbar (MMExtras)
235 @implementation NSTabView (MMExtras)
237 - (void)removeAllTabViewItems
239     NSArray *existingItems = [self tabViewItems];
240     NSEnumerator *e = [existingItems objectEnumerator];
241     NSTabViewItem *item;
242     while (item = [e nextObject]){
243         [self removeTabViewItem:item];
244     }
247 @end // NSTabView (MMExtras)
252 @implementation NSNumber (MMExtras)
254 - (int)tag
256     return [self intValue];
259 @end // NSNumber (MMExtras)
264     NSView *
265 openPanelAccessoryView()
267     // Return a new button object for each NSOpenPanel -- several of them
268     // could be displayed at once.
269     // If the accessory view should get more complex, it should probably be
270     // loaded from a nib file.
271     NSButton *button = [[[NSButton alloc]
272         initWithFrame:NSMakeRect(0, 0, 140, 18)] autorelease];
273     [button setTitle:
274         NSLocalizedString(@"Show Hidden Files", @"Open File Dialog")];
275     [button setButtonType:NSSwitchButton];
277     [button setTarget:nil];
278     [button setAction:@selector(hiddenFilesButtonToggled:)];
280     // use the regular control size (checkbox is a bit smaller without this)
281     NSControlSize buttonSize = NSRegularControlSize;
282     float fontSize = [NSFont systemFontSizeForControlSize:buttonSize];
283     NSCell *theCell = [button cell];
284     NSFont *theFont = [NSFont fontWithName:[[theCell font] fontName]
285                                       size:fontSize];
286     [theCell setFont:theFont];
287     [theCell setControlSize:buttonSize];
288     [button sizeToFit];
290     return button;