Skip to content

Commit

Permalink
Add "Recent Files" menu
Browse files Browse the repository at this point in the history
The recent files menu will only remember files opened from Finder and
not files opened from within Vim (via :e etc.).  Recent files will also
be added to the "Recent Items" menu under the Apple menu.

(Patch by Nico Weber with some modifications by Bjorn Winckler.)
  • Loading branch information
nico authored and b4winckler committed May 9, 2008
1 parent 1576328 commit 862da53
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/MacVim/Actions.plist
Expand Up @@ -6,6 +6,8 @@
<string></string> <string></string>
<key>arrangeInFront:</key> <key>arrangeInFront:</key>
<string></string> <string></string>
<key>clearRecentDocuments:</key>
<string></string>
<key>fileOpen:</key> <key>fileOpen:</key>
<string></string> <string></string>
<key>findNext:</key> <key>findNext:</key>
Expand Down Expand Up @@ -38,6 +40,8 @@
<string></string> <string></string>
<key>performZoom:</key> <key>performZoom:</key>
<string></string> <string></string>
<key>recentFilesDummy:</key>
<string></string>
<key>selectNextWindow:</key> <key>selectNextWindow:</key>
<string></string> <string></string>
<key>selectPreviousWindow:</key> <key>selectPreviousWindow:</key>
Expand Down
2 changes: 2 additions & 0 deletions src/MacVim/MMAppController.h
Expand Up @@ -20,6 +20,8 @@
NSString *openSelectionString; NSString *openSelectionString;
ATSFontContainerRef fontContainerRef; ATSFontContainerRef fontContainerRef;
NSMutableDictionary *pidArguments; NSMutableDictionary *pidArguments;

NSMenuItem *recentFilesMenuItem;
} }


- (void)removeVimController:(id)controller; - (void)removeVimController:(id)controller;
Expand Down
44 changes: 41 additions & 3 deletions src/MacVim/MMAppController.m
Expand Up @@ -171,20 +171,45 @@ - (void)dealloc
[pidArguments release]; pidArguments = nil; [pidArguments release]; pidArguments = nil;
[vimControllers release]; vimControllers = nil; [vimControllers release]; vimControllers = nil;
[openSelectionString release]; openSelectionString = nil; [openSelectionString release]; openSelectionString = nil;
[recentFilesMenuItem release]; recentFilesMenuItem = nil;


[super dealloc]; [super dealloc];
} }


#if MM_HANDLE_XCODE_MOD_EVENT
- (void)applicationWillFinishLaunching:(NSNotification *)notification - (void)applicationWillFinishLaunching:(NSNotification *)notification
{ {
// Create the "Open Recent" menu. See
// http://lapcatsoftware.com/blog/2007/07/10/working-without-a-nib-part-5-open-recent-menu/
// and http://www.cocoabuilder.com/archive/message/cocoa/2007/8/15/187793
// for more information.
//
// The menu needs to be created and be added to a toplevel menu in
// applicationWillFinishLaunching at the latest, otherwise it doesn't work.

recentFilesMenuItem = [[NSMenuItem alloc] initWithTitle:@"Open Recent"
action:nil keyEquivalent:@""];

NSMenu *recentFilesMenu = [[NSMenu alloc] initWithTitle:@"Open Recent"];
[recentFilesMenu performSelector:@selector(_setMenuName:)
withObject:@"NSRecentDocumentsMenu"];

[recentFilesMenu addItemWithTitle:@"Clear Menu"
action:@selector(clearRecentDocuments:)
keyEquivalent:@""];
[recentFilesMenuItem setSubmenu:recentFilesMenu];
[recentFilesMenu release]; // the menu is retained by recentFilesMenuItem
[recentFilesMenuItem setTag:-1]; // must not be 0

[[[[NSApp mainMenu] itemWithTitle:@"File"] submenu] addItem:recentFilesMenuItem];

#if MM_HANDLE_XCODE_MOD_EVENT
[[NSAppleEventManager sharedAppleEventManager] [[NSAppleEventManager sharedAppleEventManager]
setEventHandler:self setEventHandler:self
andSelector:@selector(handleXcodeModEvent:replyEvent:) andSelector:@selector(handleXcodeModEvent:replyEvent:)
forEventClass:'KAHL' forEventClass:'KAHL'
andEventID:'MOD ']; andEventID:'MOD '];
}
#endif #endif
}


- (void)applicationDidFinishLaunching:(NSNotification *)notification - (void)applicationDidFinishLaunching:(NSNotification *)notification
{ {
Expand Down Expand Up @@ -261,6 +286,18 @@ - (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames
[arguments setObject:filenames forKey:@"filenames"]; [arguments setObject:filenames forKey:@"filenames"];
[arguments setObject:[NSNumber numberWithBool:YES] forKey:@"openFiles"]; [arguments setObject:[NSNumber numberWithBool:YES] forKey:@"openFiles"];


// Add file names to "Recent Files" menu.
int i, count = [filenames count];
for (i = 0; i < count; ++i) {
// Don't add files that are being edited remotely (using ODB).
if ([arguments objectForKey:@"remoteID"]) continue;

NSURL *url = [NSURL fileURLWithPath:[filenames objectAtIndex:i]];
if (!url) continue;
[[NSDocumentController sharedDocumentController]
noteNewRecentDocumentURL:url];
}

if ((openInTabs && (vc = [self topmostVimController])) if ((openInTabs && (vc = [self topmostVimController]))
|| (vc = [self findUntitledWindow])) { || (vc = [self findUntitledWindow])) {
// Open files in an already open window. // Open files in an already open window.
Expand Down Expand Up @@ -599,7 +636,8 @@ - (IBAction)openWebsite:(id)sender
setProtocolForProxy:@protocol(MMBackendProtocol)]; setProtocolForProxy:@protocol(MMBackendProtocol)];


vc = [[[MMVimController alloc] vc = [[[MMVimController alloc]
initWithBackend:backend pid:pid] autorelease]; initWithBackend:backend pid:pid recentFiles:recentFilesMenuItem]
autorelease];


if (![vimControllers count]) { if (![vimControllers count]) {
// The first window autosaves its position. (The autosaving // The first window autosaves its position. (The autosaving
Expand Down
5 changes: 4 additions & 1 deletion src/MacVim/MMVimController.h
Expand Up @@ -40,9 +40,12 @@
NSData *resendData; NSData *resendData;
#endif #endif
NSMenu *lastMenuSearched; NSMenu *lastMenuSearched;
NSMenuItem *recentFilesMenuItem;
NSMenuItem *recentFilesDummy;
} }


- (id)initWithBackend:(id)backend pid:(int)processIdentifier; - (id)initWithBackend:(id)backend pid:(int)processIdentifier
recentFiles:(NSMenuItem*)menu;
- (id)backendProxy; - (id)backendProxy;
- (int)pid; - (int)pid;
- (void)setServerName:(NSString *)name; - (void)setServerName:(NSString *)name;
Expand Down
57 changes: 46 additions & 11 deletions src/MacVim/MMVimController.m
Expand Up @@ -81,6 +81,7 @@ - (void)connectionDidDie:(NSNotification *)notification;
#if MM_RESEND_LAST_FAILURE #if MM_RESEND_LAST_FAILURE
- (void)resendTimerFired:(NSTimer *)timer; - (void)resendTimerFired:(NSTimer *)timer;
#endif #endif
- (void)replaceMenuItem:(NSMenuItem*)old with:(NSMenuItem*)new;
@end @end




Expand All @@ -89,8 +90,12 @@ - (void)resendTimerFired:(NSTimer *)timer;
@implementation MMVimController @implementation MMVimController


- (id)initWithBackend:(id)backend pid:(int)processIdentifier - (id)initWithBackend:(id)backend pid:(int)processIdentifier
recentFiles:(NSMenuItem*)menu;
{ {
if ((self = [super init])) { if ((self = [super init])) {

recentFilesMenuItem = [menu retain];

windowController = windowController =
[[MMWindowController alloc] initWithVimController:self]; [[MMWindowController alloc] initWithVimController:self];
backendProxy = [backend retain]; backendProxy = [backend retain];
Expand Down Expand Up @@ -135,6 +140,9 @@ - (void)dealloc
[mainMenuItems release]; mainMenuItems = nil; [mainMenuItems release]; mainMenuItems = nil;
[windowController release]; windowController = nil; [windowController release]; windowController = nil;


[recentFilesMenuItem release]; recentFilesMenuItem = nil;
[recentFilesDummy release]; recentFilesDummy = nil;

[super dealloc]; [super dealloc];
} }


Expand Down Expand Up @@ -574,6 +582,14 @@ - (void)updateMainMenu
[NSApp setWindowsMenu:windowMenu]; [NSApp setWindowsMenu:windowMenu];
} }


// Replace real Recent Files menu in the old menu with the dummy, then
// remove dummy from new menu and put Recent Files menu there
NSMenuItem *oldItem = (NSMenuItem*)[recentFilesMenuItem representedObject];
if (oldItem)
[self replaceMenuItem:recentFilesMenuItem with:oldItem];
[recentFilesMenuItem setRepresentedObject:recentFilesDummy];
[self replaceMenuItem:recentFilesDummy with:recentFilesMenuItem];

shouldUpdateMainMenu = NO; shouldUpdateMainMenu = NO;
} }


Expand Down Expand Up @@ -1073,17 +1089,28 @@ - (void)addMenuItemWithTag:(int)tag parent:(NSMenu *)parent
} else { } else {
item = [[[NSMenuItem alloc] init] autorelease]; item = [[[NSMenuItem alloc] init] autorelease];
[item setTitle:title]; [item setTitle:title];
// TODO: Check that 'action' is a valid action (nothing will happen
// if it isn't, but it would be nice with a warning). if ([action isEqualToString:@"recentFilesDummy:"]) {
if (action) [item setAction:NSSelectorFromString(action)]; // Remove the recent files menu item from its current menu
else [item setAction:@selector(vimMenuItemAction:)]; // and put it in the current file menu. See -[MMAppController
if (tip) [item setToolTip:tip]; // applicationWillFinishLaunching for more information.

//[[recentFilesMenuItem menu] removeItem:recentFilesMenuItem];
if (key != 0) { //item = recentFilesMenuItem;
NSString *keyString = recentFilesDummy = [item retain];
[NSString stringWithFormat:@"%C", key];
[item setKeyEquivalent:keyString]; } else {
[item setKeyEquivalentModifierMask:mask]; // TODO: Check that 'action' is a valid action (nothing will
// happen if it isn't, but it would be nice with a warning).
if (action) [item setAction:NSSelectorFromString(action)];
else [item setAction:@selector(vimMenuItemAction:)];
if (tip) [item setToolTip:tip];

if (key != 0) {
NSString *keyString =
[NSString stringWithFormat:@"%C", key];
[item setKeyEquivalent:keyString];
[item setKeyEquivalentModifierMask:mask];
}
} }
} }


Expand Down Expand Up @@ -1220,6 +1247,14 @@ - (void)resendTimerFired:(NSTimer *)timer
} }
#endif #endif


- (void)replaceMenuItem:(NSMenuItem*)old with:(NSMenuItem*)new
{
NSMenu *menu = [old menu];
int index = [menu indexOfItem:old];
[menu removeItemAtIndex:index];
[menu insertItem:new atIndex:index];
}

@end // MMVimController (Private) @end // MMVimController (Private)




Expand Down
1 change: 1 addition & 0 deletions src/MacVim/gvimrc
Expand Up @@ -45,6 +45,7 @@ aunmenu File.Save-Exit
an <silent> 10.290 File.New\ Window :maca newWindow:<CR> an <silent> 10.290 File.New\ Window :maca newWindow:<CR>
an 10.295 File.New\ Tab :tabnew<CR> an 10.295 File.New\ Tab :tabnew<CR>
an <silent> 10.310 File.&Open\.\.\. :maca fileOpen:<CR> an <silent> 10.310 File.&Open\.\.\. :maca fileOpen:<CR>
an <silent> 10.314 File.Open\ Recent :maca recentFilesDummy:<CR>
an 10.328 File.-SEP0- <Nop> an 10.328 File.-SEP0- <Nop>
an <silent> 10.330 File.Close\ Window<Tab>:qa :confirm qa<CR> an <silent> 10.330 File.Close\ Window<Tab>:qa :confirm qa<CR>
an <silent> 10.331 File.Close :maca performClose:<CR> an <silent> 10.331 File.Close :maca performClose:<CR>
Expand Down

0 comments on commit 862da53

Please sign in to comment.