Revert "Modifier key sends Esc" related commits
[MacVim.git] / src / MacVim / Miscellaneous.m
blob15259d48d7a6e31c5c49ea4a80f6aa1e3a333a75
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 *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";
42 #endif
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)
61             [idxSet addIndex:i];
62     }
64     return idxSet;
67 @end // NSIndexSet (MMExtras)
72 @implementation NSDocumentController (MMExtras)
74 - (void)noteNewRecentFilePath:(NSString *)path
76     NSURL *url = [NSURL fileURLWithPath:path];
77     if (url)
78         [self noteNewRecentDocumentURL:url];
81 - (void)noteNewRecentFilePaths:(NSArray *)paths
83     NSEnumerator *e = [paths objectEnumerator];
84     NSString *path;
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)])
108         return;
110     id navView = [self performSelector:@selector(_navView)];
111     if (![navView respondsToSelector:@selector(setShowsHiddenFiles:)])
112         return;
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];
120     [invocation invoke];
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)
136             return i;
137     }
139     return -1;
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;
156     }
158     return nil;
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])
208             return i;
209     }
211     return NSNotFound;
214 - (NSToolbarItem *)itemAtIndex:(int)idx
216     NSArray *items = [self items];
217     if (idx < 0 || idx >= [items count])
218         return nil;
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];
240     NSTabViewItem *item;
241     while (item = [e nextObject]){
242         [self removeTabViewItem:item];
243     }
246 @end // NSTabView (MMExtras)
251 @implementation NSNumber (MMExtras)
253 - (int)tag
255     return [self intValue];
258 @end // NSNumber (MMExtras)
263     NSView *
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];
272     [button setTitle:
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]
284                                       size:fontSize];
285     [theCell setFont:theFont];
286     [theCell setControlSize:buttonSize];
287     [button sizeToFit];
289     return button;