Add help on how to remap Caps Lock to Esc
[MacVim.git] / src / MacVim / MMVimController.m
blob37d4c26de18bd2e3ca36041d86213fc4f94deb65
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  * MMVimController
12  *
13  * Coordinates input/output to/from backend.  Each MMBackend communicates
14  * directly with a MMVimController.
15  *
16  * MMVimController does not deal with visual presentation.  Essentially it
17  * should be able to run with no window present.
18  *
19  * Output from the backend is received in processCommandQueue:.  Input is sent
20  * to the backend via sendMessage:data: or addVimInput:.  The latter allows
21  * execution of arbitrary stings in the Vim process, much like the Vim script
22  * function remote_send() does.  The messages that may be passed between
23  * frontend and backend are defined in an enum in MacVim.h.
24  */
26 #import "MMAppController.h"
27 #import "MMAtsuiTextView.h"
28 #import "MMFindReplaceController.h"
29 #import "MMTextView.h"
30 #import "MMVimController.h"
31 #import "MMVimView.h"
32 #import "MMWindowController.h"
33 #import "Miscellaneous.h"
35 #ifdef MM_ENABLE_PLUGINS
36 #import "MMPlugInManager.h"
37 #endif
39 static NSString *MMDefaultToolbarImageName = @"Attention";
40 static int MMAlertTextFieldHeight = 22;
42 // NOTE: By default a message sent to the backend will be dropped if it cannot
43 // be delivered instantly; otherwise there is a possibility that MacVim will
44 // 'beachball' while waiting to deliver DO messages to an unresponsive Vim
45 // process.  This means that you cannot rely on any message sent with
46 // sendMessage: to actually reach Vim.
47 static NSTimeInterval MMBackendProxyRequestTimeout = 0;
49 // Timeout used for setDialogReturn:.
50 static NSTimeInterval MMSetDialogReturnTimeout = 1.0;
52 // Maximum number of items in the receiveQueue.  (It is hard to predict what
53 // consequences changing this number will have.)
54 static int MMReceiveQueueCap = 100;
56 static BOOL isUnsafeMessage(int msgid);
59 @interface MMAlert : NSAlert {
60     NSTextField *textField;
62 - (void)setTextFieldString:(NSString *)textFieldString;
63 - (NSTextField *)textField;
64 @end
67 @interface MMVimController (Private)
68 - (void)doProcessCommandQueue:(NSArray *)queue;
69 - (void)handleMessage:(int)msgid data:(NSData *)data;
70 - (void)savePanelDidEnd:(NSSavePanel *)panel code:(int)code
71                 context:(void *)context;
72 - (void)alertDidEnd:(MMAlert *)alert code:(int)code context:(void *)context;
73 - (NSMenuItem *)menuItemForDescriptor:(NSArray *)desc;
74 - (NSMenu *)parentMenuForDescriptor:(NSArray *)desc;
75 - (NSMenu *)topLevelMenuForTitle:(NSString *)title;
76 - (void)addMenuWithDescriptor:(NSArray *)desc atIndex:(int)index;
77 - (void)addMenuItemWithDescriptor:(NSArray *)desc
78                           atIndex:(int)index
79                               tip:(NSString *)tip
80                              icon:(NSString *)icon
81                     keyEquivalent:(NSString *)keyEquivalent
82                      modifierMask:(int)modifierMask
83                            action:(NSString *)action
84                       isAlternate:(BOOL)isAlternate;
85 - (void)removeMenuItemWithDescriptor:(NSArray *)desc;
86 - (void)enableMenuItemWithDescriptor:(NSArray *)desc state:(BOOL)on;
87 - (void)addToolbarItemToDictionaryWithLabel:(NSString *)title
88         toolTip:(NSString *)tip icon:(NSString *)icon;
89 - (void)addToolbarItemWithLabel:(NSString *)label
90                           tip:(NSString *)tip icon:(NSString *)icon
91                       atIndex:(int)idx;
92 - (void)popupMenuWithDescriptor:(NSArray *)desc
93                           atRow:(NSNumber *)row
94                          column:(NSNumber *)col;
95 - (void)popupMenuWithAttributes:(NSDictionary *)attrs;
96 - (void)connectionDidDie:(NSNotification *)notification;
97 - (void)scheduleClose;
98 @end
103 @implementation MMVimController
105 - (id)initWithBackend:(id)backend pid:(int)processIdentifier
107     if (!(self = [super init]))
108         return nil;
110     windowController =
111         [[MMWindowController alloc] initWithVimController:self];
112     backendProxy = [backend retain];
113     sendQueue = [NSMutableArray new];
114     receiveQueue = [NSMutableArray new];
115     popupMenuItems = [[NSMutableArray alloc] init];
116     toolbarItemDict = [[NSMutableDictionary alloc] init];
117     pid = processIdentifier;
118     creationDate = [[NSDate alloc] init];
120     NSConnection *connection = [backendProxy connectionForProxy];
122     // TODO: Check that this will not set the timeout for the root proxy
123     // (in MMAppController).
124     [connection setRequestTimeout:MMBackendProxyRequestTimeout];
126     [[NSNotificationCenter defaultCenter] addObserver:self
127             selector:@selector(connectionDidDie:)
128                 name:NSConnectionDidDieNotification object:connection];
130     // Set up a main menu with only a "MacVim" menu (copied from a template
131     // which itself is set up in MainMenu.nib).  The main menu is populated
132     // by Vim later on.
133     mainMenu = [[NSMenu alloc] initWithTitle:@"MainMenu"];
134     NSMenuItem *appMenuItem = [[MMAppController sharedInstance]
135                                         appMenuItemTemplate];
136     appMenuItem = [[appMenuItem copy] autorelease];
138     // Note: If the title of the application menu is anything but what
139     // CFBundleName says then the application menu will not be typeset in
140     // boldface for some reason.  (It should already be set when we copy
141     // from the default main menu, but this is not the case for some
142     // reason.)
143     NSString *appName = [[NSBundle mainBundle]
144             objectForInfoDictionaryKey:@"CFBundleName"];
145     [appMenuItem setTitle:appName];
147     [mainMenu addItem:appMenuItem];
149 #ifdef MM_ENABLE_PLUGINS
150     instanceMediator = [[MMPlugInInstanceMediator alloc]
151             initWithVimController:self];
152 #endif
154     isInitialized = YES;
156     return self;
159 - (void)dealloc
161     LOG_DEALLOC
163     isInitialized = NO;
165 #ifdef MM_ENABLE_PLUGINS
166     [instanceMediator release]; instanceMediator = nil;
167 #endif
169     [serverName release];  serverName = nil;
170     [backendProxy release];  backendProxy = nil;
171     [sendQueue release];  sendQueue = nil;
172     [receiveQueue release];  receiveQueue = nil;
174     [toolbarItemDict release];  toolbarItemDict = nil;
175     [toolbar release];  toolbar = nil;
176     [popupMenuItems release];  popupMenuItems = nil;
177     [windowController release];  windowController = nil;
179     [vimState release];  vimState = nil;
180     [mainMenu release];  mainMenu = nil;
181     [creationDate release];  creationDate = nil;
183     [super dealloc];
186 - (MMWindowController *)windowController
188     return windowController;
191 #ifdef MM_ENABLE_PLUGINS
192 - (MMPlugInInstanceMediator *)instanceMediator
194     return instanceMediator;
196 #endif
198 - (NSDictionary *)vimState
200     return vimState;
203 - (NSMenu *)mainMenu
205     return mainMenu;
208 - (BOOL)isPreloading
210     return isPreloading;
213 - (void)setIsPreloading:(BOOL)yn
215     isPreloading = yn;
218 - (NSDate *)creationDate
220     return creationDate;
223 - (void)setServerName:(NSString *)name
225     if (name != serverName) {
226         [serverName release];
227         serverName = [name copy];
228     }
231 - (NSString *)serverName
233     return serverName;
236 - (int)pid
238     return pid;
241 - (void)dropFiles:(NSArray *)filenames forceOpen:(BOOL)force
243     NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
245     // Default to opening in tabs if layout is invalid or set to "windows".
246     int layout = [ud integerForKey:MMOpenLayoutKey];
247     if (layout < 0 || layout > MMLayoutTabs)
248         layout = MMLayoutTabs;
250     NSDictionary *args = [NSDictionary dictionaryWithObjectsAndKeys:
251             [NSNumber numberWithInt:layout],    @"layout",
252             filenames,                          @"filenames",
253             [NSNumber numberWithBool:force],    @"forceOpen",
254             nil];
256     [self sendMessage:DropFilesMsgID data:[args dictionaryAsData]];
259 - (void)file:(NSString *)filename draggedToTabAtIndex:(NSUInteger)tabIndex
261     NSString *fnEsc = [filename stringByEscapingSpecialFilenameCharacters];
262     NSString *input = [NSString stringWithFormat:@"<C-\\><C-N>:silent "
263                        "tabnext %d |"
264                        "edit! %@<CR>", tabIndex + 1, fnEsc];
265     [self addVimInput:input];
268 - (void)filesDraggedToTabBar:(NSArray *)filenames
270     NSUInteger i, count = [filenames count];
271     NSMutableString *input = [NSMutableString stringWithString:@"<C-\\><C-N>"
272                               ":silent! tabnext 9999"];
273     for (i = 0; i < count; i++) {
274         NSString *fn = [filenames objectAtIndex:i];
275         NSString *fnEsc = [fn stringByEscapingSpecialFilenameCharacters];
276         [input appendFormat:@"|tabedit %@", fnEsc];
277     }
278     [input appendString:@"<CR>"];
279     [self addVimInput:input];
282 - (void)dropString:(NSString *)string
284     int len = [string lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;
285     if (len > 0) {
286         NSMutableData *data = [NSMutableData data];
288         [data appendBytes:&len length:sizeof(int)];
289         [data appendBytes:[string UTF8String] length:len];
291         [self sendMessage:DropStringMsgID data:data];
292     }
295 - (void)passArguments:(NSDictionary *)args
297     if (!args) return;
299     [self sendMessage:OpenWithArgumentsMsgID data:[args dictionaryAsData]];
301     // HACK! Fool findUnusedEditor into thinking that this controller is not
302     // unused anymore, in case it is called before the arguments have reached
303     // the Vim process.  This should be a "safe" hack since the next time the
304     // Vim process flushes its output queue the state will be updated again (at
305     // which time the "unusedEditor" state will have been properly set).
306     NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:
307             vimState];
308     [dict setObject:[NSNumber numberWithBool:NO] forKey:@"unusedEditor"];
309     [vimState release];
310     vimState = [dict copy];
313 - (void)sendMessage:(int)msgid data:(NSData *)data
315     //NSLog(@"sendMessage:%s (isInitialized=%d inProcessCommandQueue=%d)",
316     //        MessageStrings[msgid], isInitialized, inProcessCommandQueue);
318     if (!isInitialized) return;
320     if (inProcessCommandQueue) {
321         //NSLog(@"In process command queue; delaying message send.");
322         [sendQueue addObject:[NSNumber numberWithInt:msgid]];
323         if (data)
324             [sendQueue addObject:data];
325         else
326             [sendQueue addObject:[NSNull null]];
327         return;
328     }
330     @try {
331         [backendProxy processInput:msgid data:data];
332     }
333     @catch (NSException *e) {
334         //NSLog(@"%@ %s Exception caught during DO call: %@",
335         //        [self className], _cmd, e);
336     }
339 - (BOOL)sendMessageNow:(int)msgid data:(NSData *)data
340                timeout:(NSTimeInterval)timeout
342     // Send a message with a timeout.  USE WITH EXTREME CAUTION!  Sending
343     // messages in rapid succession with a timeout may cause MacVim to beach
344     // ball forever.  In almost all circumstances sendMessage:data: should be
345     // used instead.
347     if (!isInitialized || inProcessCommandQueue)
348         return NO;
350     if (timeout < 0) timeout = 0;
352     BOOL sendOk = YES;
353     NSConnection *conn = [backendProxy connectionForProxy];
354     NSTimeInterval oldTimeout = [conn requestTimeout];
356     [conn setRequestTimeout:timeout];
358     @try {
359         [backendProxy processInput:msgid data:data];
360     }
361     @catch (NSException *e) {
362         sendOk = NO;
363     }
364     @finally {
365         [conn setRequestTimeout:oldTimeout];
366     }
368     return sendOk;
371 - (void)addVimInput:(NSString *)string
373     // This is a very general method of adding input to the Vim process.  It is
374     // basically the same as calling remote_send() on the process (see
375     // ':h remote_send').
376     if (string) {
377         NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
378         [self sendMessage:AddInputMsgID data:data];
379     }
382 - (NSString *)evaluateVimExpression:(NSString *)expr
384     NSString *eval = nil;
386     @try {
387         eval = [backendProxy evaluateExpression:expr];
388     }
389     @catch (NSException *ex) { /* do nothing */ }
391     return eval;
394 - (id)evaluateVimExpressionCocoa:(NSString *)expr errorString:(NSString **)errstr
396     id eval = nil;
398     @try {
399         eval = [backendProxy evaluateExpressionCocoa:expr
400                                          errorString:errstr];
401     } @catch (NSException *ex) {
402         *errstr = [ex reason];
403     }
405     return eval;
408 - (id)backendProxy
410     return backendProxy;
413 - (void)cleanup
415     if (!isInitialized) return;
417     isInitialized = NO;
418     [toolbar setDelegate:nil];
419     [[NSNotificationCenter defaultCenter] removeObserver:self];
420     //[[backendProxy connectionForProxy] invalidate];
421     //[windowController close];
422     [windowController cleanup];
425 - (oneway void)showSavePanelWithAttributes:(in bycopy NSDictionary *)attr
427     if (!isInitialized) return;
429     BOOL inDefaultMode = [[[NSRunLoop currentRunLoop] currentMode]
430                                         isEqual:NSDefaultRunLoopMode];
431     if (!inDefaultMode) {
432         // Delay call until run loop is in default mode.
433         [self performSelectorOnMainThread:
434                                         @selector(showSavePanelWithAttributes:)
435                                withObject:attr
436                             waitUntilDone:NO
437                                     modes:[NSArray arrayWithObject:
438                                            NSDefaultRunLoopMode]];
439         return;
440     }
442     NSString *dir = [attr objectForKey:@"dir"];
443     BOOL saving = [[attr objectForKey:@"saving"] boolValue];
445     if (!dir) {
446         // 'dir == nil' means: set dir to the pwd of the Vim process, or let
447         // open dialog decide (depending on the below user default).
448         BOOL trackPwd = [[NSUserDefaults standardUserDefaults]
449                 boolForKey:MMDialogsTrackPwdKey];
450         if (trackPwd)
451             dir = [vimState objectForKey:@"pwd"];
452     }
454     if (saving) {
455         [[NSSavePanel savePanel] beginSheetForDirectory:dir file:nil
456                 modalForWindow:[windowController window]
457                  modalDelegate:self
458                 didEndSelector:@selector(savePanelDidEnd:code:context:)
459                    contextInfo:NULL];
460     } else {
461         NSOpenPanel *panel = [NSOpenPanel openPanel];
462         [panel setAllowsMultipleSelection:NO];
463         [panel setAccessoryView:openPanelAccessoryView()];
465         [panel beginSheetForDirectory:dir file:nil types:nil
466                 modalForWindow:[windowController window]
467                  modalDelegate:self
468                 didEndSelector:@selector(savePanelDidEnd:code:context:)
469                    contextInfo:NULL];
470     }
473 - (oneway void)presentDialogWithAttributes:(in bycopy NSDictionary *)attr
475     if (!isInitialized) return;
477     BOOL inDefaultMode = [[[NSRunLoop currentRunLoop] currentMode]
478                                         isEqual:NSDefaultRunLoopMode];
479     if (!inDefaultMode) {
480         // Delay call until run loop is in default mode.
481         [self performSelectorOnMainThread:
482                                         @selector(presentDialogWithAttributes:)
483                                withObject:attr
484                             waitUntilDone:NO
485                                     modes:[NSArray arrayWithObject:
486                                            NSDefaultRunLoopMode]];
487         return;
488     }
490     NSArray *buttonTitles = [attr objectForKey:@"buttonTitles"];
491     if (!(buttonTitles && [buttonTitles count])) return;
493     int style = [[attr objectForKey:@"alertStyle"] intValue];
494     NSString *message = [attr objectForKey:@"messageText"];
495     NSString *text = [attr objectForKey:@"informativeText"];
496     NSString *textFieldString = [attr objectForKey:@"textFieldString"];
497     MMAlert *alert = [[MMAlert alloc] init];
499     // NOTE! This has to be done before setting the informative text.
500     if (textFieldString)
501         [alert setTextFieldString:textFieldString];
503     [alert setAlertStyle:style];
505     if (message) {
506         [alert setMessageText:message];
507     } else {
508         // If no message text is specified 'Alert' is used, which we don't
509         // want, so set an empty string as message text.
510         [alert setMessageText:@""];
511     }
513     if (text) {
514         [alert setInformativeText:text];
515     } else if (textFieldString) {
516         // Make sure there is always room for the input text field.
517         [alert setInformativeText:@""];
518     }
520     unsigned i, count = [buttonTitles count];
521     for (i = 0; i < count; ++i) {
522         NSString *title = [buttonTitles objectAtIndex:i];
523         // NOTE: The title of the button may contain the character '&' to
524         // indicate that the following letter should be the key equivalent
525         // associated with the button.  Extract this letter and lowercase it.
526         NSString *keyEquivalent = nil;
527         NSRange hotkeyRange = [title rangeOfString:@"&"];
528         if (NSNotFound != hotkeyRange.location) {
529             if ([title length] > NSMaxRange(hotkeyRange)) {
530                 NSRange keyEquivRange = NSMakeRange(hotkeyRange.location+1, 1);
531                 keyEquivalent = [[title substringWithRange:keyEquivRange]
532                     lowercaseString];
533             }
535             NSMutableString *string = [NSMutableString stringWithString:title];
536             [string deleteCharactersInRange:hotkeyRange];
537             title = string;
538         }
540         [alert addButtonWithTitle:title];
542         // Set key equivalent for the button, but only if NSAlert hasn't
543         // already done so.  (Check the documentation for
544         // - [NSAlert addButtonWithTitle:] to see what key equivalents are
545         // automatically assigned.)
546         NSButton *btn = [[alert buttons] lastObject];
547         if ([[btn keyEquivalent] length] == 0 && keyEquivalent) {
548             [btn setKeyEquivalent:keyEquivalent];
549         }
550     }
552     [alert beginSheetModalForWindow:[windowController window]
553                       modalDelegate:self
554                      didEndSelector:@selector(alertDidEnd:code:context:)
555                         contextInfo:NULL];
557     [alert release];
560 - (oneway void)processCommandQueue:(in bycopy NSArray *)queue
562     if (!isInitialized) return;
564     if (inProcessCommandQueue) {
565         // NOTE!  If a synchronous DO call is made during
566         // doProcessCommandQueue: below it may happen that this method is
567         // called a second time while the synchronous message is waiting for a
568         // reply (could also happen if doProcessCommandQueue: enters a modal
569         // loop, see comment below).  Since this method cannot be considered
570         // reentrant, we queue the input and return immediately.
571         //
572         // If doProcessCommandQueue: enters a modal loop (happens e.g. on
573         // ShowPopupMenuMsgID) then the receiveQueue could grow to become
574         // arbitrarily large because DO calls still get processed.  To avoid
575         // this we set a cap on the size of the queue and simply clear it if it
576         // becomes too large.  (That is messages will be dropped and hence Vim
577         // and MacVim will at least temporarily be out of sync.)
578         if ([receiveQueue count] >= MMReceiveQueueCap)
579             [receiveQueue removeAllObjects];
581         [receiveQueue addObject:queue];
582         return;
583     }
585     inProcessCommandQueue = YES;
586     [self doProcessCommandQueue:queue];
588     int i;
589     for (i = 0; i < [receiveQueue count]; ++i) {
590         // Note that doProcessCommandQueue: may cause the receiveQueue to grow
591         // or get cleared (due to cap being hit).  Make sure to retain the item
592         // to process or it may get released from under us.
593         NSArray *q = [[receiveQueue objectAtIndex:i] retain];
594         [self doProcessCommandQueue:q];
595         [q release];
596     }
598     // We assume that the remaining calls make no synchronous DO calls.  If
599     // that did happen anyway, the command queue could get processed out of
600     // order.
602     // See comment below why this is called here and not later.
603     [windowController processCommandQueueDidFinish];
605     // NOTE: Ensure that no calls are made after this "if" clause that may call
606     // sendMessage::.  If this happens anyway, such messages will be put on the
607     // send queue and then the queue will not be flushed until the next time
608     // this method is called.
609     if ([sendQueue count] > 0) {
610         @try {
611             [backendProxy processInputAndData:sendQueue];
612         }
613         @catch (NSException *e) {
614             // Connection timed out, just ignore this.
615             //NSLog(@"WARNING! Connection timed out in %s", _cmd);
616         }
618         [sendQueue removeAllObjects];
619     }
621     [receiveQueue removeAllObjects];
622     inProcessCommandQueue = NO;
625 - (NSToolbarItem *)toolbar:(NSToolbar *)theToolbar
626     itemForItemIdentifier:(NSString *)itemId
627     willBeInsertedIntoToolbar:(BOOL)flag
629     NSToolbarItem *item = [toolbarItemDict objectForKey:itemId];
630     if (!item) {
631         NSLog(@"WARNING:  No toolbar item with id '%@'", itemId);
632     }
634     return item;
637 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)theToolbar
639     return nil;
642 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)theToolbar
644     return nil;
647 @end // MMVimController
651 @implementation MMVimController (Private)
653 - (void)doProcessCommandQueue:(NSArray *)queue
655     NSMutableArray *delayQueue = nil;
657     @try {
658         unsigned i, count = [queue count];
659         if (count % 2) {
660             NSLog(@"WARNING: Uneven number of components (%d) in command "
661                     "queue.  Skipping...", count);
662             return;
663         }
665         //NSLog(@"======== %s BEGIN ========", _cmd);
666         for (i = 0; i < count; i += 2) {
667             NSData *value = [queue objectAtIndex:i];
668             NSData *data = [queue objectAtIndex:i+1];
670             int msgid = *((int*)[value bytes]);
671             //NSLog(@"%s%s", _cmd, MessageStrings[msgid]);
673             BOOL inDefaultMode = [[[NSRunLoop currentRunLoop] currentMode]
674                                                 isEqual:NSDefaultRunLoopMode];
675             if (!inDefaultMode && isUnsafeMessage(msgid)) {
676                 // NOTE: Because we listen to DO messages in 'event tracking'
677                 // mode we have to take extra care when doing things like
678                 // releasing view items (and other Cocoa objects).  Messages
679                 // that may be potentially "unsafe" are delayed until the run
680                 // loop is back to default mode at which time they are safe to
681                 // call again.
682                 //   A problem with this approach is that it is hard to
683                 // classify which messages are unsafe.  As a rule of thumb, if
684                 // a message may release an object used by the Cocoa framework
685                 // (e.g. views) then the message should be considered unsafe.
686                 //   Delaying messages may have undesired side-effects since it
687                 // means that messages may not be processed in the order Vim
688                 // sent them, so beware.
689                 if (!delayQueue)
690                     delayQueue = [NSMutableArray array];
692                 //NSLog(@"Adding unsafe message '%s' to delay queue (mode=%@)",
693                 //        MessageStrings[msgid],
694                 //        [[NSRunLoop currentRunLoop] currentMode]);
695                 [delayQueue addObject:value];
696                 [delayQueue addObject:data];
697             } else {
698                 [self handleMessage:msgid data:data];
699             }
700         }
701         //NSLog(@"======== %s  END  ========", _cmd);
702     }
703     @catch (NSException *e) {
704         NSLog(@"Exception caught whilst processing command queue: %@", e);
705     }
707     if (delayQueue) {
708         //NSLog(@"    Flushing delay queue (%d items)", [delayQueue count]/2);
709         [self performSelectorOnMainThread:@selector(processCommandQueue:)
710                                withObject:delayQueue
711                             waitUntilDone:NO
712                                     modes:[NSArray arrayWithObject:
713                                            NSDefaultRunLoopMode]];
714     }
717 - (void)handleMessage:(int)msgid data:(NSData *)data
719     //if (msgid != AddMenuMsgID && msgid != AddMenuItemMsgID &&
720     //        msgid != EnableMenuItemMsgID)
721     //    NSLog(@"%@ %s%s", [self className], _cmd, MessageStrings[msgid]);
723     if (OpenWindowMsgID == msgid) {
724         [windowController openWindow];
726         // If the vim controller is preloading then the window will be
727         // displayed when it is taken off the preload cache.
728         if (!isPreloading)
729             [windowController showWindow];
730     } else if (BatchDrawMsgID == msgid) {
731         [[[windowController vimView] textView] performBatchDrawWithData:data];
732     } else if (SelectTabMsgID == msgid) {
733 #if 0   // NOTE: Tab selection is done inside updateTabsWithData:.
734         const void *bytes = [data bytes];
735         int idx = *((int*)bytes);
736         //NSLog(@"Selecting tab with index %d", idx);
737         [windowController selectTabWithIndex:idx];
738 #endif
739     } else if (UpdateTabBarMsgID == msgid) {
740         [windowController updateTabsWithData:data];
741     } else if (ShowTabBarMsgID == msgid) {
742         [windowController showTabBar:YES];
743     } else if (HideTabBarMsgID == msgid) {
744         [windowController showTabBar:NO];
745     } else if (SetTextDimensionsMsgID == msgid || LiveResizeMsgID == msgid ||
746             SetTextDimensionsReplyMsgID == msgid) {
747         const void *bytes = [data bytes];
748         int rows = *((int*)bytes);  bytes += sizeof(int);
749         int cols = *((int*)bytes);  bytes += sizeof(int);
751         [windowController setTextDimensionsWithRows:rows
752                                  columns:cols
753                                   isLive:(LiveResizeMsgID==msgid)
754                                  isReply:(SetTextDimensionsReplyMsgID==msgid)];
755     } else if (SetWindowTitleMsgID == msgid) {
756         const void *bytes = [data bytes];
757         int len = *((int*)bytes);  bytes += sizeof(int);
759         NSString *string = [[NSString alloc] initWithBytes:(void*)bytes
760                 length:len encoding:NSUTF8StringEncoding];
762         // While in live resize the window title displays the dimensions of the
763         // window so don't clobber this with a spurious "set title" message
764         // from Vim.
765         if (![[windowController vimView] inLiveResize])
766             [windowController setTitle:string];
768         [string release];
769     } else if (SetDocumentFilenameMsgID == msgid) {
770         const void *bytes = [data bytes];
771         int len = *((int*)bytes);  bytes += sizeof(int);
773         if (len > 0) {
774             NSString *filename = [[NSString alloc] initWithBytes:(void*)bytes
775                     length:len encoding:NSUTF8StringEncoding];
777             [windowController setDocumentFilename:filename];
779             [filename release];
780         } else {
781             [windowController setDocumentFilename:@""];
782         }
783     } else if (AddMenuMsgID == msgid) {
784         NSDictionary *attrs = [NSDictionary dictionaryWithData:data];
785         [self addMenuWithDescriptor:[attrs objectForKey:@"descriptor"]
786                 atIndex:[[attrs objectForKey:@"index"] intValue]];
787     } else if (AddMenuItemMsgID == msgid) {
788         NSDictionary *attrs = [NSDictionary dictionaryWithData:data];
789         [self addMenuItemWithDescriptor:[attrs objectForKey:@"descriptor"]
790                       atIndex:[[attrs objectForKey:@"index"] intValue]
791                           tip:[attrs objectForKey:@"tip"]
792                          icon:[attrs objectForKey:@"icon"]
793                 keyEquivalent:[attrs objectForKey:@"keyEquivalent"]
794                  modifierMask:[[attrs objectForKey:@"modifierMask"] intValue]
795                        action:[attrs objectForKey:@"action"]
796                   isAlternate:[[attrs objectForKey:@"isAlternate"] boolValue]];
797     } else if (RemoveMenuItemMsgID == msgid) {
798         NSDictionary *attrs = [NSDictionary dictionaryWithData:data];
799         [self removeMenuItemWithDescriptor:[attrs objectForKey:@"descriptor"]];
800     } else if (EnableMenuItemMsgID == msgid) {
801         NSDictionary *attrs = [NSDictionary dictionaryWithData:data];
802         [self enableMenuItemWithDescriptor:[attrs objectForKey:@"descriptor"]
803                 state:[[attrs objectForKey:@"enable"] boolValue]];
804     } else if (ShowToolbarMsgID == msgid) {
805         const void *bytes = [data bytes];
806         int enable = *((int*)bytes);  bytes += sizeof(int);
807         int flags = *((int*)bytes);  bytes += sizeof(int);
809         int mode = NSToolbarDisplayModeDefault;
810         if (flags & ToolbarLabelFlag) {
811             mode = flags & ToolbarIconFlag ? NSToolbarDisplayModeIconAndLabel
812                     : NSToolbarDisplayModeLabelOnly;
813         } else if (flags & ToolbarIconFlag) {
814             mode = NSToolbarDisplayModeIconOnly;
815         }
817         int size = flags & ToolbarSizeRegularFlag ? NSToolbarSizeModeRegular
818                 : NSToolbarSizeModeSmall;
820         [windowController showToolbar:enable size:size mode:mode];
821     } else if (CreateScrollbarMsgID == msgid) {
822         const void *bytes = [data bytes];
823         long ident = *((long*)bytes);  bytes += sizeof(long);
824         int type = *((int*)bytes);  bytes += sizeof(int);
826         [windowController createScrollbarWithIdentifier:ident type:type];
827     } else if (DestroyScrollbarMsgID == msgid) {
828         const void *bytes = [data bytes];
829         long ident = *((long*)bytes);  bytes += sizeof(long);
831         [windowController destroyScrollbarWithIdentifier:ident];
832     } else if (ShowScrollbarMsgID == msgid) {
833         const void *bytes = [data bytes];
834         long ident = *((long*)bytes);  bytes += sizeof(long);
835         int visible = *((int*)bytes);  bytes += sizeof(int);
837         [windowController showScrollbarWithIdentifier:ident state:visible];
838     } else if (SetScrollbarPositionMsgID == msgid) {
839         const void *bytes = [data bytes];
840         long ident = *((long*)bytes);  bytes += sizeof(long);
841         int pos = *((int*)bytes);  bytes += sizeof(int);
842         int len = *((int*)bytes);  bytes += sizeof(int);
844         [windowController setScrollbarPosition:pos length:len
845                                     identifier:ident];
846     } else if (SetScrollbarThumbMsgID == msgid) {
847         const void *bytes = [data bytes];
848         long ident = *((long*)bytes);  bytes += sizeof(long);
849         float val = *((float*)bytes);  bytes += sizeof(float);
850         float prop = *((float*)bytes);  bytes += sizeof(float);
852         [windowController setScrollbarThumbValue:val proportion:prop
853                                       identifier:ident];
854     } else if (SetFontMsgID == msgid) {
855         const void *bytes = [data bytes];
856         float size = *((float*)bytes);  bytes += sizeof(float);
857         int len = *((int*)bytes);  bytes += sizeof(int);
858         NSString *name = [[NSString alloc]
859                 initWithBytes:(void*)bytes length:len
860                      encoding:NSUTF8StringEncoding];
861         NSFont *font = [NSFont fontWithName:name size:size];
863         if (font)
864             [windowController setFont:font];
866         [name release];
867     } else if (SetWideFontMsgID == msgid) {
868         const void *bytes = [data bytes];
869         float size = *((float*)bytes);  bytes += sizeof(float);
870         int len = *((int*)bytes);  bytes += sizeof(int);
871         if (len > 0) {
872             NSString *name = [[NSString alloc]
873                     initWithBytes:(void*)bytes length:len
874                          encoding:NSUTF8StringEncoding];
875             NSFont *font = [NSFont fontWithName:name size:size];
876             [windowController setWideFont:font];
878             [name release];
879         } else {
880             [windowController setWideFont:nil];
881         }
882     } else if (SetDefaultColorsMsgID == msgid) {
883         const void *bytes = [data bytes];
884         unsigned bg = *((unsigned*)bytes);  bytes += sizeof(unsigned);
885         unsigned fg = *((unsigned*)bytes);  bytes += sizeof(unsigned);
886         NSColor *back = [NSColor colorWithArgbInt:bg];
887         NSColor *fore = [NSColor colorWithRgbInt:fg];
889         [windowController setDefaultColorsBackground:back foreground:fore];
890     } else if (ExecuteActionMsgID == msgid) {
891         const void *bytes = [data bytes];
892         int len = *((int*)bytes);  bytes += sizeof(int);
893         NSString *actionName = [[NSString alloc]
894                 initWithBytes:(void*)bytes length:len
895                      encoding:NSUTF8StringEncoding];
897         SEL sel = NSSelectorFromString(actionName);
898         [NSApp sendAction:sel to:nil from:self];
900         [actionName release];
901     } else if (ShowPopupMenuMsgID == msgid) {
902         NSDictionary *attrs = [NSDictionary dictionaryWithData:data];
904         // The popup menu enters a modal loop so delay this call so that we
905         // don't block inside processCommandQueue:.
906         [self performSelectorOnMainThread:@selector(popupMenuWithAttributes:)
907                              withObject:attrs
908                           waitUntilDone:NO
909                                   modes:[NSArray arrayWithObject:
910                                          NSDefaultRunLoopMode]];
911     } else if (SetMouseShapeMsgID == msgid) {
912         const void *bytes = [data bytes];
913         int shape = *((int*)bytes);  bytes += sizeof(int);
915         [windowController setMouseShape:shape];
916     } else if (AdjustLinespaceMsgID == msgid) {
917         const void *bytes = [data bytes];
918         int linespace = *((int*)bytes);  bytes += sizeof(int);
920         [windowController adjustLinespace:linespace];
921     } else if (ActivateMsgID == msgid) {
922         //NSLog(@"ActivateMsgID");
923         [NSApp activateIgnoringOtherApps:YES];
924         [[windowController window] makeKeyAndOrderFront:self];
925     } else if (SetServerNameMsgID == msgid) {
926         NSString *name = [[NSString alloc] initWithData:data
927                                                encoding:NSUTF8StringEncoding];
928         [self setServerName:name];
929         [name release];
930     } else if (EnterFullscreenMsgID == msgid) {
931         const void *bytes = [data bytes];
932         int fuoptions = *((int*)bytes); bytes += sizeof(int);
933         int bg = *((int*)bytes);
934         NSColor *back = [NSColor colorWithArgbInt:bg];
936         [windowController enterFullscreen:fuoptions backgroundColor:back];
937     } else if (LeaveFullscreenMsgID == msgid) {
938         [windowController leaveFullscreen];
939     } else if (BuffersNotModifiedMsgID == msgid) {
940         [windowController setBuffersModified:NO];
941     } else if (BuffersModifiedMsgID == msgid) {
942         [windowController setBuffersModified:YES];
943     } else if (SetPreEditPositionMsgID == msgid) {
944         const int *dim = (const int*)[data bytes];
945         [[[windowController vimView] textView] setPreEditRow:dim[0]
946                                                       column:dim[1]];
947     } else if (EnableAntialiasMsgID == msgid) {
948         [[[windowController vimView] textView] setAntialias:YES];
949     } else if (DisableAntialiasMsgID == msgid) {
950         [[[windowController vimView] textView] setAntialias:NO];
951     } else if (SetVimStateMsgID == msgid) {
952         NSDictionary *dict = [NSDictionary dictionaryWithData:data];
953         if (dict) {
954             [vimState release];
955             vimState = [dict retain];
956         }
957     } else if (CloseWindowMsgID == msgid) {
958         [self scheduleClose];
959     } else if (SetFullscreenColorMsgID == msgid) {
960         const int *bg = (const int*)[data bytes];
961         NSColor *color = [NSColor colorWithRgbInt:*bg];
963         [windowController setFullscreenBackgroundColor:color];
964     } else if (ShowFindReplaceDialogMsgID == msgid) {
965         NSDictionary *dict = [NSDictionary dictionaryWithData:data];
966         if (dict) {
967             [[MMFindReplaceController sharedInstance]
968                 showWithText:[dict objectForKey:@"text"]
969                        flags:[[dict objectForKey:@"flags"] intValue]];
970         }
971     // IMPORTANT: When adding a new message, make sure to update
972     // isUnsafeMessage() if necessary!
973     } else {
974         NSLog(@"WARNING: Unknown message received (msgid=%d)", msgid);
975     }
978 - (void)savePanelDidEnd:(NSSavePanel *)panel code:(int)code
979                 context:(void *)context
981     NSString *path = (code == NSOKButton) ? [panel filename] : nil;
983     // NOTE! setDialogReturn: is a synchronous call so set a proper timeout to
984     // avoid waiting forever for it to finish.  We make this a synchronous call
985     // so that we can be fairly certain that Vim doesn't think the dialog box
986     // is still showing when MacVim has in fact already dismissed it.
987     NSConnection *conn = [backendProxy connectionForProxy];
988     NSTimeInterval oldTimeout = [conn requestTimeout];
989     [conn setRequestTimeout:MMSetDialogReturnTimeout];
991     @try {
992         [backendProxy setDialogReturn:path];
994         // Add file to the "Recent Files" menu (this ensures that files that
995         // are opened/saved from a :browse command are added to this menu).
996         if (path)
997             [[NSDocumentController sharedDocumentController]
998                     noteNewRecentFilePath:path];
999     }
1000     @catch (NSException *e) {
1001         NSLog(@"Exception caught in %s %@", _cmd, e);
1002     }
1003     @finally {
1004         [conn setRequestTimeout:oldTimeout];
1005     }
1008 - (void)alertDidEnd:(MMAlert *)alert code:(int)code context:(void *)context
1010     NSArray *ret = nil;
1012     code = code - NSAlertFirstButtonReturn + 1;
1014     if ([alert isKindOfClass:[MMAlert class]] && [alert textField]) {
1015         ret = [NSArray arrayWithObjects:[NSNumber numberWithInt:code],
1016             [[alert textField] stringValue], nil];
1017     } else {
1018         ret = [NSArray arrayWithObject:[NSNumber numberWithInt:code]];
1019     }
1021     @try {
1022         [backendProxy setDialogReturn:ret];
1023     }
1024     @catch (NSException *e) {
1025         NSLog(@"Exception caught in %s %@", _cmd, e);
1026     }
1029 - (NSMenuItem *)menuItemForDescriptor:(NSArray *)desc
1031     if (!(desc && [desc count] > 0)) return nil;
1033     NSString *rootName = [desc objectAtIndex:0];
1034     NSArray *rootItems = [rootName hasPrefix:@"PopUp"] ? popupMenuItems
1035                                                        : [mainMenu itemArray];
1037     NSMenuItem *item = nil;
1038     int i, count = [rootItems count];
1039     for (i = 0; i < count; ++i) {
1040         item = [rootItems objectAtIndex:i];
1041         if ([[item title] isEqual:rootName])
1042             break;
1043     }
1045     if (i == count) return nil;
1047     count = [desc count];
1048     for (i = 1; i < count; ++i) {
1049         item = [[item submenu] itemWithTitle:[desc objectAtIndex:i]];
1050         if (!item) return nil;
1051     }
1053     return item;
1056 - (NSMenu *)parentMenuForDescriptor:(NSArray *)desc
1058     if (!(desc && [desc count] > 0)) return nil;
1060     NSString *rootName = [desc objectAtIndex:0];
1061     NSArray *rootItems = [rootName hasPrefix:@"PopUp"] ? popupMenuItems
1062                                                        : [mainMenu itemArray];
1064     NSMenu *menu = nil;
1065     int i, count = [rootItems count];
1066     for (i = 0; i < count; ++i) {
1067         NSMenuItem *item = [rootItems objectAtIndex:i];
1068         if ([[item title] isEqual:rootName]) {
1069             menu = [item submenu];
1070             break;
1071         }
1072     }
1074     if (!menu) return nil;
1076     count = [desc count] - 1;
1077     for (i = 1; i < count; ++i) {
1078         NSMenuItem *item = [menu itemWithTitle:[desc objectAtIndex:i]];
1079         menu = [item submenu];
1080         if (!menu) return nil;
1081     }
1083     return menu;
1086 - (NSMenu *)topLevelMenuForTitle:(NSString *)title
1088     // Search only the top-level menus.
1090     unsigned i, count = [popupMenuItems count];
1091     for (i = 0; i < count; ++i) {
1092         NSMenuItem *item = [popupMenuItems objectAtIndex:i];
1093         if ([title isEqual:[item title]])
1094             return [item submenu];
1095     }
1097     count = [mainMenu numberOfItems];
1098     for (i = 0; i < count; ++i) {
1099         NSMenuItem *item = [mainMenu itemAtIndex:i];
1100         if ([title isEqual:[item title]])
1101             return [item submenu];
1102     }
1104     return nil;
1107 - (void)addMenuWithDescriptor:(NSArray *)desc atIndex:(int)idx
1109     if (!(desc && [desc count] > 0 && idx >= 0)) return;
1111     NSString *rootName = [desc objectAtIndex:0];
1112     if ([rootName isEqual:@"ToolBar"]) {
1113         // The toolbar only has one menu, we take this as a hint to create a
1114         // toolbar, then we return.
1115         if (!toolbar) {
1116             // NOTE! Each toolbar must have a unique identifier, else each
1117             // window will have the same toolbar.
1118             NSString *ident = [NSString stringWithFormat:@"%d", (int)self];
1119             toolbar = [[NSToolbar alloc] initWithIdentifier:ident];
1121             [toolbar setShowsBaselineSeparator:NO];
1122             [toolbar setDelegate:self];
1123             [toolbar setDisplayMode:NSToolbarDisplayModeIconOnly];
1124             [toolbar setSizeMode:NSToolbarSizeModeSmall];
1126             [windowController setToolbar:toolbar];
1127         }
1129         return;
1130     }
1132     // This is either a main menu item or a popup menu item.
1133     NSString *title = [desc lastObject];
1134     NSMenuItem *item = [[NSMenuItem alloc] init];
1135     NSMenu *menu = [[NSMenu alloc] initWithTitle:title];
1137     [item setTitle:title];
1138     [item setSubmenu:menu];
1140     NSMenu *parent = [self parentMenuForDescriptor:desc];
1141     if (!parent && [rootName hasPrefix:@"PopUp"]) {
1142         if ([popupMenuItems count] <= idx) {
1143             [popupMenuItems addObject:item];
1144         } else {
1145             [popupMenuItems insertObject:item atIndex:idx];
1146         }
1147     } else {
1148         // If descriptor has no parent and its not a popup (or toolbar) menu,
1149         // then it must belong to main menu.
1150         if (!parent) parent = mainMenu;
1152         if ([parent numberOfItems] <= idx) {
1153             [parent addItem:item];
1154         } else {
1155             [parent insertItem:item atIndex:idx];
1156         }
1157     }
1159     [item release];
1160     [menu release];
1163 - (void)addMenuItemWithDescriptor:(NSArray *)desc
1164                           atIndex:(int)idx
1165                               tip:(NSString *)tip
1166                              icon:(NSString *)icon
1167                     keyEquivalent:(NSString *)keyEquivalent
1168                      modifierMask:(int)modifierMask
1169                            action:(NSString *)action
1170                       isAlternate:(BOOL)isAlternate
1172     if (!(desc && [desc count] > 1 && idx >= 0)) return;
1174     NSString *title = [desc lastObject];
1175     NSString *rootName = [desc objectAtIndex:0];
1177     if ([rootName isEqual:@"ToolBar"]) {
1178         if (toolbar && [desc count] == 2)
1179             [self addToolbarItemWithLabel:title tip:tip icon:icon atIndex:idx];
1180         return;
1181     }
1183     NSMenu *parent = [self parentMenuForDescriptor:desc];
1184     if (!parent) {
1185         NSLog(@"WARNING: Menu item '%@' has no parent",
1186                 [desc componentsJoinedByString:@"->"]);
1187         return;
1188     }
1190     NSMenuItem *item = nil;
1191     if (0 == [title length]
1192             || ([title hasPrefix:@"-"] && [title hasSuffix:@"-"])) {
1193         item = [NSMenuItem separatorItem];
1194         [item setTitle:title];
1195     } else {
1196         item = [[[NSMenuItem alloc] init] autorelease];
1197         [item setTitle:title];
1199         // Note: It is possible to set the action to a message that "doesn't
1200         // exist" without problems.  We take advantage of this when adding
1201         // "dummy items" e.g. when dealing with the "Recent Files" menu (in
1202         // which case a recentFilesDummy: action is set, although it is never
1203         // used).
1204         if ([action length] > 0)
1205             [item setAction:NSSelectorFromString(action)];
1206         else
1207             [item setAction:@selector(vimMenuItemAction:)];
1208         if ([tip length] > 0) [item setToolTip:tip];
1209         if ([keyEquivalent length] > 0) {
1210             [item setKeyEquivalent:keyEquivalent];
1211             [item setKeyEquivalentModifierMask:modifierMask];
1212         }
1213         [item setAlternate:isAlternate];
1215         // The tag is used to indicate whether Vim thinks a menu item should be
1216         // enabled or disabled.  By default Vim thinks menu items are enabled.
1217         [item setTag:1];
1218     }
1220     if ([parent numberOfItems] <= idx) {
1221         [parent addItem:item];
1222     } else {
1223         [parent insertItem:item atIndex:idx];
1224     }
1227 - (void)removeMenuItemWithDescriptor:(NSArray *)desc
1229     if (!(desc && [desc count] > 0)) return;
1231     NSString *title = [desc lastObject];
1232     NSString *rootName = [desc objectAtIndex:0];
1233     if ([rootName isEqual:@"ToolBar"]) {
1234         if (toolbar) {
1235             // Only remove toolbar items, never actually remove the toolbar
1236             // itself or strange things may happen.
1237             if ([desc count] == 2) {
1238                 int idx = [toolbar indexOfItemWithItemIdentifier:title];
1239                 if (idx != NSNotFound)
1240                     [toolbar removeItemAtIndex:idx];
1241             }
1242         }
1243         return;
1244     }
1246     NSMenuItem *item = [self menuItemForDescriptor:desc];
1247     if (!item) {
1248         NSLog(@"Failed to remove menu item, descriptor not found: %@",
1249                 [desc componentsJoinedByString:@"->"]);
1250         return;
1251     }
1253     [item retain];
1255     if ([item menu] == [NSApp mainMenu] || ![item menu]) {
1256         // NOTE: To be on the safe side we try to remove the item from
1257         // both arrays (it is ok to call removeObject: even if an array
1258         // does not contain the object to remove).
1259         [popupMenuItems removeObject:item];
1260     }
1262     if ([item menu])
1263         [[item menu] removeItem:item];
1265     [item release];
1268 - (void)enableMenuItemWithDescriptor:(NSArray *)desc state:(BOOL)on
1270     if (!(desc && [desc count] > 0)) return;
1272     /*NSLog(@"%sable item %@", on ? "En" : "Dis",
1273             [desc componentsJoinedByString:@"->"]);*/
1275     NSString *rootName = [desc objectAtIndex:0];
1276     if ([rootName isEqual:@"ToolBar"]) {
1277         if (toolbar && [desc count] == 2) {
1278             NSString *title = [desc lastObject];
1279             [[toolbar itemWithItemIdentifier:title] setEnabled:on];
1280         }
1281     } else {
1282         // Use tag to set whether item is enabled or disabled instead of
1283         // calling setEnabled:.  This way the menus can autoenable themselves
1284         // but at the same time Vim can set if a menu is enabled whenever it
1285         // wants to.
1286         [[self menuItemForDescriptor:desc] setTag:on];
1287     }
1290 - (void)addToolbarItemToDictionaryWithLabel:(NSString *)title
1291                                     toolTip:(NSString *)tip
1292                                        icon:(NSString *)icon
1294     // If the item corresponds to a separator then do nothing, since it is
1295     // already defined by Cocoa.
1296     if (!title || [title isEqual:NSToolbarSeparatorItemIdentifier]
1297                || [title isEqual:NSToolbarSpaceItemIdentifier]
1298                || [title isEqual:NSToolbarFlexibleSpaceItemIdentifier])
1299         return;
1301     NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:title];
1302     [item setLabel:title];
1303     [item setToolTip:tip];
1304     [item setAction:@selector(vimToolbarItemAction:)];
1305     [item setAutovalidates:NO];
1307     NSImage *img = [NSImage imageNamed:icon];
1308     if (!img) {
1309         img = [[[NSImage alloc] initByReferencingFile:icon] autorelease];
1310         if (!(img && [img isValid]))
1311             img = nil;
1312     }
1313     if (!img) {
1314         NSLog(@"WARNING: Could not find image with name '%@' to use as toolbar"
1315                " image for identifier '%@';"
1316                " using default toolbar icon '%@' instead.",
1317                icon, title, MMDefaultToolbarImageName);
1319         img = [NSImage imageNamed:MMDefaultToolbarImageName];
1320     }
1322     [item setImage:img];
1324     [toolbarItemDict setObject:item forKey:title];
1326     [item release];
1329 - (void)addToolbarItemWithLabel:(NSString *)label
1330                             tip:(NSString *)tip
1331                            icon:(NSString *)icon
1332                         atIndex:(int)idx
1334     if (!toolbar) return;
1336     // Check for separator items.
1337     if (!label) {
1338         label = NSToolbarSeparatorItemIdentifier;
1339     } else if ([label length] >= 2 && [label hasPrefix:@"-"]
1340                                    && [label hasSuffix:@"-"]) {
1341         // The label begins and ends with '-'; decided which kind of separator
1342         // item it is by looking at the prefix.
1343         if ([label hasPrefix:@"-space"]) {
1344             label = NSToolbarSpaceItemIdentifier;
1345         } else if ([label hasPrefix:@"-flexspace"]) {
1346             label = NSToolbarFlexibleSpaceItemIdentifier;
1347         } else {
1348             label = NSToolbarSeparatorItemIdentifier;
1349         }
1350     }
1352     [self addToolbarItemToDictionaryWithLabel:label toolTip:tip icon:icon];
1354     int maxIdx = [[toolbar items] count];
1355     if (maxIdx < idx) idx = maxIdx;
1357     [toolbar insertItemWithItemIdentifier:label atIndex:idx];
1360 - (void)popupMenuWithDescriptor:(NSArray *)desc
1361                           atRow:(NSNumber *)row
1362                          column:(NSNumber *)col
1364     NSMenu *menu = [[self menuItemForDescriptor:desc] submenu];
1365     if (!menu) return;
1367     id textView = [[windowController vimView] textView];
1368     NSPoint pt;
1369     if (row && col) {
1370         // TODO: Let textView convert (row,col) to NSPoint.
1371         int r = [row intValue];
1372         int c = [col intValue];
1373         NSSize cellSize = [textView cellSize];
1374         pt = NSMakePoint((c+1)*cellSize.width, (r+1)*cellSize.height);
1375         pt = [textView convertPoint:pt toView:nil];
1376     } else {
1377         pt = [[windowController window] mouseLocationOutsideOfEventStream];
1378     }
1380     NSEvent *event = [NSEvent mouseEventWithType:NSRightMouseDown
1381                            location:pt
1382                       modifierFlags:0
1383                           timestamp:0
1384                        windowNumber:[[windowController window] windowNumber]
1385                             context:nil
1386                         eventNumber:0
1387                          clickCount:0
1388                            pressure:1.0];
1390     [NSMenu popUpContextMenu:menu withEvent:event forView:textView];
1393 - (void)popupMenuWithAttributes:(NSDictionary *)attrs
1395     if (!attrs) return;
1397     [self popupMenuWithDescriptor:[attrs objectForKey:@"descriptor"]
1398                             atRow:[attrs objectForKey:@"row"]
1399                            column:[attrs objectForKey:@"column"]];
1402 - (void)connectionDidDie:(NSNotification *)notification
1404     //NSLog(@"%@ %s%@", [self className], _cmd, notification);
1405     [self scheduleClose];
1408 - (void)scheduleClose
1410     // NOTE!  This message can arrive at pretty much anytime, e.g. while
1411     // the run loop is the 'event tracking' mode.  This means that Cocoa may
1412     // well be in the middle of processing some message while this message is
1413     // received.  If we were to remove the vim controller straight away we may
1414     // free objects that Cocoa is currently using (e.g. view objects).  The
1415     // following call ensures that the vim controller is not released until the
1416     // run loop is back in the 'default' mode.
1417     [[MMAppController sharedInstance]
1418             performSelectorOnMainThread:@selector(removeVimController:)
1419                              withObject:self
1420                           waitUntilDone:NO
1421                                   modes:[NSArray arrayWithObject:
1422                                          NSDefaultRunLoopMode]];
1425 @end // MMVimController (Private)
1430 @implementation MMAlert
1431 - (void)dealloc
1433     [textField release];  textField = nil;
1434     [super dealloc];
1437 - (void)setTextFieldString:(NSString *)textFieldString
1439     [textField release];
1440     textField = [[NSTextField alloc] init];
1441     [textField setStringValue:textFieldString];
1444 - (NSTextField *)textField
1446     return textField;
1449 - (void)setInformativeText:(NSString *)text
1451     if (textField) {
1452         // HACK! Add some space for the text field.
1453         [super setInformativeText:[text stringByAppendingString:@"\n\n\n"]];
1454     } else {
1455         [super setInformativeText:text];
1456     }
1459 - (void)beginSheetModalForWindow:(NSWindow *)window
1460                    modalDelegate:(id)delegate
1461                   didEndSelector:(SEL)didEndSelector
1462                      contextInfo:(void *)contextInfo
1464     [super beginSheetModalForWindow:window
1465                       modalDelegate:delegate
1466                      didEndSelector:didEndSelector
1467                         contextInfo:contextInfo];
1469     // HACK! Place the input text field at the bottom of the informative text
1470     // (which has been made a bit larger by adding newline characters).
1471     NSView *contentView = [[self window] contentView];
1472     NSRect rect = [contentView frame];
1473     rect.origin.y = rect.size.height;
1475     NSArray *subviews = [contentView subviews];
1476     unsigned i, count = [subviews count];
1477     for (i = 0; i < count; ++i) {
1478         NSView *view = [subviews objectAtIndex:i];
1479         if ([view isKindOfClass:[NSTextField class]]
1480                 && [view frame].origin.y < rect.origin.y) {
1481             // NOTE: The informative text field is the lowest NSTextField in
1482             // the alert dialog.
1483             rect = [view frame];
1484         }
1485     }
1487     rect.size.height = MMAlertTextFieldHeight;
1488     [textField setFrame:rect];
1489     [contentView addSubview:textField];
1490     [textField becomeFirstResponder];
1493 @end // MMAlert
1498     static BOOL
1499 isUnsafeMessage(int msgid)
1501     // Messages that may release Cocoa objects must be added to this list.  For
1502     // example, UpdateTabBarMsgID may delete NSTabViewItem objects so it goes
1503     // on this list.
1504     static int unsafeMessages[] = { // REASON MESSAGE IS ON THIS LIST:
1505         //OpenWindowMsgID,            // Changes lots of state
1506         UpdateTabBarMsgID,          // May delete NSTabViewItem
1507         RemoveMenuItemMsgID,        // Deletes NSMenuItem
1508         DestroyScrollbarMsgID,      // Deletes NSScroller
1509         ExecuteActionMsgID,         // Impossible to predict
1510         ShowPopupMenuMsgID,         // Enters modal loop
1511         ActivateMsgID,              // ?
1512         EnterFullscreenMsgID,       // Modifies delegate of window controller
1513         LeaveFullscreenMsgID,       // Modifies delegate of window controller
1514         CloseWindowMsgID,           // See note below
1515     };
1517     // NOTE about CloseWindowMsgID: If this arrives at the same time as say
1518     // ExecuteActionMsgID, then the "execute" message will be lost due to it
1519     // being queued and handled after the "close" message has caused the
1520     // controller to cleanup...UNLESS we add CloseWindowMsgID to the list of
1521     // unsafe messages.  This is the _only_ reason it is on this list (since
1522     // all that happens in response to it is that we schedule another message
1523     // for later handling).
1525     int i, count = sizeof(unsafeMessages)/sizeof(unsafeMessages[0]);
1526     for (i = 0; i < count; ++i)
1527         if (msgid == unsafeMessages[i])
1528             return YES;
1530     return NO;