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.
11 #import "MMVimController.h"
12 #import "MMWindowController.h"
13 #import "MMAppController.h"
14 #import "MMTextView.h"
15 #import "MMTextStorage.h"
18 //static NSString *AttentionToolbarItemID = @"Attention";
19 static NSString *DefaultToolbarImageName = @"Attention";
22 @interface MMVimController (Private)
23 - (void)handleMessage:(int)msgid data:(NSData *)data;
24 - (void)performBatchDrawWithData:(NSData *)data;
25 - (void)panelDidEnd:(NSSavePanel *)panel code:(int)code
26 context:(void *)context;
27 - (NSMenuItem *)menuItemForTag:(int)tag;
28 - (NSMenu *)menuForTag:(int)tag;
29 - (void)addMenuWithTag:(int)tag parent:(NSMenu *)parent title:(NSString *)title
31 - (void)addMenuItemWithTag:(int)tag parent:(NSMenu *)parent
32 title:(NSString *)title tip:(NSString *)tip
33 keyEquivalent:(int)key modifiers:(int)mask
34 action:(NSString *)action atIndex:(int)idx;
35 - (void)updateMainMenu;
36 - (NSToolbarItem *)toolbarItemForTag:(int)tag index:(int *)index;
37 - (IBAction)toolbarAction:(id)sender;
38 - (void)addToolbarItemToDictionaryWithTag:(int)tag label:(NSString *)title
39 toolTip:(NSString *)tip icon:(NSString *)icon;
40 - (void)addToolbarItemWithTag:(int)tag label:(NSString *)label
41 tip:(NSString *)tip icon:(NSString *)icon
44 - (void)connectionDidDie:(NSNotification *)notification;
46 - (BOOL)executeActionWithName:(NSString *)name;
51 // TODO: Move to separate file
52 @interface NSColor (MMProtocol)
53 + (NSColor *)colorWithRgbInt:(int)rgb;
58 static NSMenuItem *findMenuItemWithTagInMenu(NSMenu *root, int tag)
61 NSMenuItem *item = [root itemWithTag:tag];
62 if (item) return item;
64 NSArray *items = [root itemArray];
65 unsigned i, count = [items count];
66 for (i = 0; i < count; ++i) {
67 item = [items objectAtIndex:i];
68 if ([item hasSubmenu]) {
69 item = findMenuItemWithTagInMenu([item submenu], tag);
70 if (item) return item;
80 @implementation MMVimController
83 - (id)initWithBackend:(id)backend
85 - (id)initWithPort:(NSPort *)port
88 if ((self = [super init])) {
90 [[MMWindowController alloc] initWithVimController:self];
92 backendProxy = [backend retain];
93 # if MM_DELAY_SEND_IN_PROCESS_CMD_QUEUE
94 sendQueue = [NSMutableArray new];
97 NSConnection *connection = [backendProxy connectionForProxy];
98 [[NSNotificationCenter defaultCenter] addObserver:self
99 selector:@selector(connectionDidDie:)
100 name:NSConnectionDidDieNotification object:connection];
102 sendPort = [port retain];
104 // Init receive port and send connected message to VimTask
105 receivePort = [NSMachPort new];
106 [receivePort setDelegate:self];
108 // Add to the default run loop mode as well as the event tracking mode;
109 // the latter ensures that updates from the VimTask reaches
110 // MMVimController whilst the user resizes a window with the mouse.
111 [[NSRunLoop currentRunLoop] addPort:receivePort
112 forMode:NSDefaultRunLoopMode];
113 [[NSRunLoop currentRunLoop] addPort:receivePort
114 forMode:NSEventTrackingRunLoopMode];
116 [NSPortMessage sendMessage:ConnectedMsgID withSendPort:sendPort
117 receivePort:receivePort wait:YES];
120 mainMenuItems = [[NSMutableArray alloc] init];
122 toolbarItemDict = [[NSMutableDictionary alloc] init];
123 //[self addToolbarItemToDictionaryWithTag:0 label:@"Attention"
124 // toolTip:@"A toolbar item is missing"
125 // icon:@"Attention"];
127 [[NSNotificationCenter defaultCenter]
129 selector:@selector(windowWillClose:)
130 name:NSWindowWillCloseNotification
131 object:[windowController window]];
132 [[NSNotificationCenter defaultCenter]
134 selector:@selector(windowDidBecomeMain:)
135 name:NSWindowDidBecomeMainNotification
136 object:[windowController window]];
145 //NSLog(@"%@ %s", [self className], _cmd);
147 [[NSNotificationCenter defaultCenter] removeObserver:self];
150 [backendProxy release];
151 # if MM_DELAY_SEND_IN_PROCESS_CMD_QUEUE
156 // Kill task immediately
157 [NSPortMessage sendMessage:KillTaskMsgID withSendPort:sendPort
158 receivePort:receivePort wait:NO];
162 [receivePort release];
165 [toolbarItemDict release];
167 [mainMenuItems release];
168 [windowController release];
173 - (MMWindowController *)windowController
175 return windowController;
178 - (void)sendMessage:(int)msgid data:(NSData *)data wait:(BOOL)wait
181 # if MM_DELAY_SEND_IN_PROCESS_CMD_QUEUE
182 if (inProcessCommandQueue) {
183 //NSLog(@"In process command queue; delaying message send.");
184 [sendQueue addObject:[NSNumber numberWithInt:msgid]];
186 [sendQueue addObject:data];
188 [sendQueue addObject:[NSNull null]];
193 [backendProxy processInput:msgid data:data];
195 // Do not wait for the message to be sent, i.e. drop the message if it
196 // can't be delivered immediately.
197 NSConnection *connection = [backendProxy connectionForProxy];
199 NSTimeInterval req = [connection requestTimeout];
200 [connection setRequestTimeout:0];
202 [backendProxy processInput:msgid data:data];
204 @catch (NSException *e) {
205 // Connection timed out, just ignore this.
206 //NSLog(@"WARNING! Connection timed out in %s", _cmd);
209 [connection setRequestTimeout:req];
214 [NSPortMessage sendMessage:msgid withSendPort:sendPort data:data
225 - (oneway void)showSavePanelForDirectory:(in bycopy NSString *)dir
226 title:(in bycopy NSString *)title
229 [windowController setStatusText:title];
232 [[NSSavePanel savePanel] beginSheetForDirectory:dir file:nil
233 modalForWindow:[windowController window]
235 didEndSelector:@selector(panelDidEnd:code:context:)
238 NSOpenPanel *panel = [NSOpenPanel openPanel];
239 [panel setAllowsMultipleSelection:NO];
240 [panel beginSheetForDirectory:dir file:nil types:nil
241 modalForWindow:[windowController window]
243 didEndSelector:@selector(panelDidEnd:code:context:)
248 - (oneway void)processCommandQueue:(in NSArray *)queue
250 unsigned i, count = [queue count];
252 NSLog(@"WARNING: Uneven number of components (%d) in flush queue "
253 "message; ignoring this message.", count);
257 #if MM_DELAY_SEND_IN_PROCESS_CMD_QUEUE
258 inProcessCommandQueue = YES;
261 //NSLog(@"======== %s BEGIN ========", _cmd);
262 for (i = 0; i < count; i += 2) {
263 NSData *value = [queue objectAtIndex:i];
264 NSData *data = [queue objectAtIndex:i+1];
266 int msgid = *((int*)[value bytes]);
268 if (msgid != EnableMenuItemMsgID && msgid != AddMenuItemMsgID
269 && msgid != AddMenuMsgID) {
270 NSLog(@"%s%s", _cmd, MessageStrings[msgid]);
274 [self handleMessage:msgid data:data];
276 //NSLog(@"======== %s END ========", _cmd);
278 if (shouldUpdateMainMenu) {
279 [self updateMainMenu];
282 #if MM_DELAY_SEND_IN_PROCESS_CMD_QUEUE
283 inProcessCommandQueue = NO;
285 count = [sendQueue count];
287 if (count % 2 == 0) {
288 //NSLog(@"%s Sending %d queued messages", _cmd, count/2);
290 for (i = 0; i < count; i += 2) {
291 int msgid = [[sendQueue objectAtIndex:i] intValue];
292 id data = [sendQueue objectAtIndex:i+1];
293 if ([data isEqual:[NSNull null]])
296 [backendProxy processInput:msgid data:data];
300 [sendQueue removeAllObjects];
312 - (void)handlePortMessage:(NSPortMessage *)portMessage
314 //NSLog(@"%@ %s %@", [self className], _cmd, portMessage);
316 NSArray *components = [portMessage components];
317 unsigned msgid = [portMessage msgid];
319 //NSLog(@"%s%d", _cmd, msgid);
321 if (FlushQueueMsgID == msgid) {
322 unsigned i, count = [components count];
324 NSLog(@"WARNING: Uneven number of components (%d) in flush queue "
325 "message; ignoring this message.", count);
329 for (i = 0; i < count; i += 2) {
330 NSData *value = [components objectAtIndex:i];
331 NSData *data = [components objectAtIndex:i+1];
333 [self handleMessage:*((int*)[value bytes]) data:data];
337 if ([components count] > 0)
338 data = [components objectAtIndex:0];
340 [self handleMessage:msgid data:data];
343 if (shouldUpdateMainMenu) {
344 [self updateMainMenu];
349 - (void)windowWillClose:(NSNotification *)notification
351 // NOTE! This causes the call to removeVimController: to be delayed.
353 performSelectorOnMainThread:@selector(removeVimController:)
354 withObject:self waitUntilDone:NO];
357 - (void)windowDidBecomeMain:(NSNotification *)notification
359 [self updateMainMenu];
362 - (NSToolbarItem *)toolbar:(NSToolbar *)theToolbar
363 itemForItemIdentifier:(NSString *)itemId
364 willBeInsertedIntoToolbar:(BOOL)flag
366 //NSLog(@"%s", _cmd);
368 NSToolbarItem *item = [toolbarItemDict objectForKey:itemId];
370 NSLog(@"WARNING: No toolbar item with id '%@'", itemId);
376 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)theToolbar
378 //NSLog(@"%s", _cmd);
382 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)theToolbar
384 //NSLog(@"%s", _cmd);
388 @end // MMVimController
392 @implementation MMVimController (Private)
394 - (void)handleMessage:(int)msgid data:(NSData *)data
396 //NSLog(@"%@ %s", [self className], _cmd);
398 if (OpenVimWindowMsgID == msgid) {
399 [windowController openWindow];
402 else if (TaskExitedMsgID == msgid) {
403 //NSLog(@"Received task exited message from VimTask; closing window.");
405 // Release sendPort immediately to avoid dealloc trying to send a 'kill
406 // task' message to the task.
407 [sendPort release]; sendPort = nil;
408 // NOTE! This causes windowWillClose: to be called, which in turn asks
409 // the MMAppController to remove this MMVimController.
410 [windowController close];
412 // HACK! Make sure no menu updating is done, we're about to close.
413 shouldUpdateMainMenu = NO;
416 else if (BatchDrawMsgID == msgid) {
417 //NSLog(@"Received batch draw message from VimTask.");
419 [self performBatchDrawWithData:data];
420 } else if (SelectTabMsgID == msgid) {
421 const void *bytes = [data bytes];
422 int idx = *((int*)bytes);
423 //NSLog(@"Selecting tab with index %d", idx);
424 [windowController selectTabWithIndex:idx];
425 } else if (UpdateTabBarMsgID == msgid) {
426 //NSLog(@"Updating tabs");
427 [windowController updateTabsWithData:data];
428 } else if (ShowTabBarMsgID == msgid) {
429 //NSLog(@"Showing tab bar");
431 // HACK! Vim sends several draw commands etc. after the show message
432 // and these can mess up the display when showing the tab bar results
433 // in the window having to resize to fit the screen; delaying this
434 // message alleviates this problem.
435 [windowController performSelectorOnMainThread:@selector(showTabBar:)
436 withObject:self waitUntilDone:NO];
437 //[windowController showTabBar:self];
438 } else if (HideTabBarMsgID == msgid) {
439 //NSLog(@"Hiding tab bar");
440 [windowController hideTabBar:self];
441 } else if (SetTextDimensionsMsgID == msgid) {
442 const void *bytes = [data bytes];
443 int rows = *((int*)bytes); bytes += sizeof(int);
444 int cols = *((int*)bytes); bytes += sizeof(int);
446 [windowController setTextDimensionsWithRows:rows columns:cols];
447 } else if (SetVimWindowTitleMsgID == msgid) {
448 const void *bytes = [data bytes];
449 int len = *((int*)bytes); bytes += sizeof(int);
452 // BUG! Using this call leads to ALL windows getting the same title
453 // and then the app crashes if you :q a window.
454 NSString *string = [[NSString alloc]
455 initWithBytesNoCopy:(void*)bytes
457 encoding:NSUTF8StringEncoding
460 NSString *string = [[NSString alloc] initWithBytes:(void*)bytes
461 length:len encoding:NSUTF8StringEncoding];
464 [[windowController window] setTitle:string];
467 } else if (BrowseForFileMsgID == msgid) {
468 const void *bytes = [data bytes];
469 int save = *((int*)bytes); bytes += sizeof(int);
471 int len = *((int*)bytes); bytes += sizeof(int);
474 dir = [[NSString alloc] initWithBytes:(void*)bytes
476 encoding:NSUTF8StringEncoding];
480 len = *((int*)bytes); bytes += sizeof(int);
482 NSString *title = [[NSString alloc]
483 initWithBytes:(void*)bytes length:len
484 encoding:NSUTF8StringEncoding];
487 [windowController setStatusText:title];
492 [[NSSavePanel savePanel] beginSheetForDirectory:dir file:nil
493 modalForWindow:[windowController window]
495 didEndSelector:@selector(panelDidEnd:code:context:)
498 NSOpenPanel *panel = [NSOpenPanel openPanel];
499 [panel setAllowsMultipleSelection:NO];
500 [panel beginSheetForDirectory:dir file:nil types:nil
501 modalForWindow:[windowController window]
503 didEndSelector:@selector(panelDidEnd:code:context:)
508 } else if (UpdateInsertionPointMsgID == msgid) {
509 const void *bytes = [data bytes];
510 int color = *((int*)bytes); bytes += sizeof(int);
511 int row = *((int*)bytes); bytes += sizeof(int);
512 int col = *((int*)bytes); bytes += sizeof(int);
513 int state = *((int*)bytes); bytes += sizeof(int);
515 // TODO! Move to window controller.
516 MMTextView *textView = [windowController textView];
518 MMTextStorage *textStorage = (MMTextStorage*)[textView textStorage];
519 unsigned off = [textStorage offsetFromRow:row column:col];
521 [textView setInsertionPointColor:[NSColor colorWithRgbInt:color]];
522 [textView setSelectedRange:NSMakeRange(off, 0)];
523 [textView setShouldDrawInsertionPoint:state];
525 } else if (AddMenuMsgID == msgid) {
526 NSString *title = nil;
527 const void *bytes = [data bytes];
528 int tag = *((int*)bytes); bytes += sizeof(int);
529 int parentTag = *((int*)bytes); bytes += sizeof(int);
530 int len = *((int*)bytes); bytes += sizeof(int);
532 title = [[NSString alloc] initWithBytes:(void*)bytes length:len
533 encoding:NSUTF8StringEncoding];
536 int idx = *((int*)bytes); bytes += sizeof(int);
538 if (MenuToolbarType == parentTag) {
540 NSString *ident = [NSString stringWithFormat:@"%d.%d",
542 //NSLog(@"Creating toolbar with identifier %@", ident);
543 toolbar = [[NSToolbar alloc] initWithIdentifier:ident];
545 [toolbar setShowsBaselineSeparator:NO];
546 [toolbar setDelegate:self];
547 [toolbar setDisplayMode:NSToolbarDisplayModeIconOnly];
548 [toolbar setSizeMode:NSToolbarSizeModeSmall];
550 [[windowController window] setToolbar:toolbar];
552 } else if (MenuPopupType == parentTag) {
555 NSMenu *parent = [self menuForTag:parentTag];
556 [self addMenuWithTag:tag parent:parent title:title atIndex:idx];
560 } else if (AddMenuItemMsgID == msgid) {
561 NSString *title = nil, *tip = nil, *icon = nil, *action = nil;
562 const void *bytes = [data bytes];
563 int tag = *((int*)bytes); bytes += sizeof(int);
564 int parentTag = *((int*)bytes); bytes += sizeof(int);
565 int namelen = *((int*)bytes); bytes += sizeof(int);
567 title = [[NSString alloc] initWithBytes:(void*)bytes length:namelen
568 encoding:NSUTF8StringEncoding];
571 int tiplen = *((int*)bytes); bytes += sizeof(int);
573 tip = [[NSString alloc] initWithBytes:(void*)bytes length:tiplen
574 encoding:NSUTF8StringEncoding];
577 int iconlen = *((int*)bytes); bytes += sizeof(int);
579 icon = [[NSString alloc] initWithBytes:(void*)bytes length:iconlen
580 encoding:NSUTF8StringEncoding];
583 int actionlen = *((int*)bytes); bytes += sizeof(int);
585 action = [[NSString alloc] initWithBytes:(void*)bytes
587 encoding:NSUTF8StringEncoding];
590 int idx = *((int*)bytes); bytes += sizeof(int);
591 if (idx < 0) idx = 0;
592 int key = *((int*)bytes); bytes += sizeof(int);
593 int mask = *((int*)bytes); bytes += sizeof(int);
595 NSString *ident = [NSString stringWithFormat:@"%d.%d",
596 (int)self, parentTag];
597 if (toolbar && [[toolbar identifier] isEqual:ident]) {
598 [self addToolbarItemWithTag:tag label:title tip:tip icon:icon
601 NSMenu *parent = [self menuForTag:parentTag];
602 [self addMenuItemWithTag:tag parent:parent title:title tip:tip
603 keyEquivalent:key modifiers:mask action:action
611 } else if (RemoveMenuItemMsgID == msgid) {
612 const void *bytes = [data bytes];
613 int tag = *((int*)bytes); bytes += sizeof(int);
615 // TODO: Search for tag in popup menus.
618 if ((item = [self toolbarItemForTag:tag index:&idx])) {
619 [toolbar removeItemAtIndex:idx];
620 } else if ((item = [self menuItemForTag:tag])) {
621 if ([item menu] == [NSApp mainMenu]) {
622 NSLog(@"Removing menu: %@", item);
623 [mainMenuItems removeObject:item];
625 [[item menu] removeItem:item];
627 } else if (EnableMenuItemMsgID == msgid) {
628 const void *bytes = [data bytes];
629 int tag = *((int*)bytes); bytes += sizeof(int);
630 int state = *((int*)bytes); bytes += sizeof(int);
632 // TODO: Search for tag in popup menus.
633 id item = [self toolbarItemForTag:tag index:NULL];
635 item = [self menuItemForTag:tag];
637 [item setEnabled:state];
638 } else if (ShowToolbarMsgID == msgid) {
639 const void *bytes = [data bytes];
640 int enable = *((int*)bytes); bytes += sizeof(int);
641 int flags = *((int*)bytes); bytes += sizeof(int);
643 int mode = NSToolbarDisplayModeDefault;
644 if (flags & ToolbarLabelFlag) {
645 mode = flags & ToolbarIconFlag ? NSToolbarDisplayModeIconAndLabel
646 : NSToolbarDisplayModeLabelOnly;
647 } else if (flags & ToolbarIconFlag) {
648 mode = NSToolbarDisplayModeIconOnly;
651 int size = flags & ToolbarSizeRegularFlag ? NSToolbarSizeModeRegular
652 : NSToolbarSizeModeSmall;
654 [toolbar setSizeMode:size];
655 [toolbar setDisplayMode:mode];
656 [toolbar setVisible:enable];
657 } else if (CreateScrollbarMsgID == msgid) {
658 const void *bytes = [data bytes];
659 long ident = *((long*)bytes); bytes += sizeof(long);
660 int type = *((int*)bytes); bytes += sizeof(int);
662 [windowController createScrollbarWithIdentifier:ident type:type];
663 } else if (DestroyScrollbarMsgID == msgid) {
664 const void *bytes = [data bytes];
665 long ident = *((long*)bytes); bytes += sizeof(long);
667 [windowController destroyScrollbarWithIdentifier:ident];
668 } else if (ShowScrollbarMsgID == msgid) {
669 const void *bytes = [data bytes];
670 long ident = *((long*)bytes); bytes += sizeof(long);
671 int visible = *((int*)bytes); bytes += sizeof(int);
673 [windowController showScrollbarWithIdentifier:ident state:visible];
674 } else if (SetScrollbarPositionMsgID == msgid) {
675 const void *bytes = [data bytes];
676 long ident = *((long*)bytes); bytes += sizeof(long);
677 int pos = *((int*)bytes); bytes += sizeof(int);
678 int len = *((int*)bytes); bytes += sizeof(int);
680 [windowController setScrollbarPosition:pos length:len
682 } else if (SetScrollbarThumbMsgID == msgid) {
683 const void *bytes = [data bytes];
684 long ident = *((long*)bytes); bytes += sizeof(long);
685 float val = *((float*)bytes); bytes += sizeof(float);
686 float prop = *((float*)bytes); bytes += sizeof(float);
688 [windowController setScrollbarThumbValue:val proportion:prop
690 } else if (SetFontMsgID == msgid) {
691 const void *bytes = [data bytes];
692 float size = *((float*)bytes); bytes += sizeof(float);
693 int len = *((int*)bytes); bytes += sizeof(int);
694 NSString *name = [[NSString alloc]
695 initWithBytes:(void*)bytes length:len
696 encoding:NSUTF8StringEncoding];
697 NSFont *font = [NSFont fontWithName:name size:size];
700 [windowController setFont:font];
703 } else if (SetDefaultColorsMsgID == msgid) {
704 const void *bytes = [data bytes];
705 int bg = *((int*)bytes); bytes += sizeof(int);
706 int fg = *((int*)bytes); bytes += sizeof(int);
707 NSColor *back = [NSColor colorWithRgbInt:bg];
708 NSColor *fore = [NSColor colorWithRgbInt:fg];
710 [windowController setDefaultColorsBackground:back foreground:fore];
711 } else if (ExecuteActionMsgID == msgid) {
712 const void *bytes = [data bytes];
713 int len = *((int*)bytes); bytes += sizeof(int);
714 NSString *actionName = [[NSString alloc]
715 initWithBytesNoCopy:(void*)bytes
717 encoding:NSUTF8StringEncoding
720 SEL sel = NSSelectorFromString(actionName);
721 [NSApp sendAction:sel to:nil from:self];
723 [actionName release];
725 NSLog(@"WARNING: Unknown message received (msgid=%d)", msgid);
729 - (void)performBatchDrawWithData:(NSData *)data
731 // TODO! Move to window controller.
732 MMTextStorage *textStorage = [windowController textStorage];
736 const void *bytes = [data bytes];
737 const void *end = bytes + [data length];
739 [textStorage beginEditing];
742 // 1. Sanity check input
743 // 2. Cache rgb -> NSColor lookups?
745 while (bytes < end) {
746 int type = *((int*)bytes); bytes += sizeof(int);
748 if (ClearAllDrawType == type) {
749 int color = *((int*)bytes); bytes += sizeof(int);
751 [textStorage clearAllWithColor:[NSColor colorWithRgbInt:color]];
752 } else if (ClearBlockDrawType == type) {
753 int color = *((int*)bytes); bytes += sizeof(int);
754 int row1 = *((int*)bytes); bytes += sizeof(int);
755 int col1 = *((int*)bytes); bytes += sizeof(int);
756 int row2 = *((int*)bytes); bytes += sizeof(int);
757 int col2 = *((int*)bytes); bytes += sizeof(int);
759 [textStorage clearBlockFromRow:row1 column:col1
760 toRow:row2 column:col2
761 color:[NSColor colorWithRgbInt:color]];
762 } else if (DeleteLinesDrawType == type) {
763 int color = *((int*)bytes); bytes += sizeof(int);
764 int row = *((int*)bytes); bytes += sizeof(int);
765 int count = *((int*)bytes); bytes += sizeof(int);
766 int bot = *((int*)bytes); bytes += sizeof(int);
767 int left = *((int*)bytes); bytes += sizeof(int);
768 int right = *((int*)bytes); bytes += sizeof(int);
770 [textStorage deleteLinesFromRow:row lineCount:count
771 scrollBottom:bot left:left right:right
772 color:[NSColor colorWithRgbInt:color]];
773 } else if (ReplaceStringDrawType == type) {
774 int bg = *((int*)bytes); bytes += sizeof(int);
775 int fg = *((int*)bytes); bytes += sizeof(int);
776 int row = *((int*)bytes); bytes += sizeof(int);
777 int col = *((int*)bytes); bytes += sizeof(int);
778 int flags = *((int*)bytes); bytes += sizeof(int);
779 int len = *((int*)bytes); bytes += sizeof(int);
780 NSString *string = [[NSString alloc]
781 initWithBytesNoCopy:(void*)bytes
783 encoding:NSUTF8StringEncoding
787 [textStorage replaceString:string
790 foregroundColor:[NSColor colorWithRgbInt:fg]
791 backgroundColor:[NSColor colorWithRgbInt:bg]];
794 } else if (InsertLinesDrawType == type) {
795 int color = *((int*)bytes); bytes += sizeof(int);
796 int row = *((int*)bytes); bytes += sizeof(int);
797 int count = *((int*)bytes); bytes += sizeof(int);
798 int bot = *((int*)bytes); bytes += sizeof(int);
799 int left = *((int*)bytes); bytes += sizeof(int);
800 int right = *((int*)bytes); bytes += sizeof(int);
802 [textStorage insertLinesAtRow:row lineCount:count
803 scrollBottom:bot left:left right:right
804 color:[NSColor colorWithRgbInt:color]];
806 NSLog(@"WARNING: Unknown draw type (type=%d)", type);
810 [textStorage endEditing];
813 - (void)panelDidEnd:(NSSavePanel *)panel code:(int)code context:(void *)context
816 [windowController setStatusText:@""];
818 NSString *string = (code == NSOKButton) ? [panel filename] : nil;
819 [backendProxy setBrowseForFileString:string];
821 NSMutableData *data = [NSMutableData data];
822 int ok = (code == NSOKButton);
823 NSString *filename = [panel filename];
824 int len = [filename lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
826 [data appendBytes:&ok length:sizeof(int)];
827 [data appendBytes:&len length:sizeof(int)];
829 [data appendBytes:[filename UTF8String] length:len];
831 if (![NSPortMessage sendMessage:BrowseForFileReplyMsgID
832 withSendPort:sendPort data:data wait:YES]) {
833 NSLog(@"WARNING: Failed to send browse for files reply back to "
837 [windowController setStatusText:@""];
841 - (NSMenuItem *)menuItemForTag:(int)tag
843 int i, count = [mainMenuItems count];
844 for (i = 0; i < count; ++i) {
845 NSMenuItem *item = [mainMenuItems objectAtIndex:i];
846 if ([item tag] == tag) return item;
847 item = findMenuItemWithTagInMenu([item submenu], tag);
848 if (item) return item;
854 - (NSMenu *)menuForTag:(int)tag
856 return [[self menuItemForTag:tag] submenu];
859 - (void)addMenuWithTag:(int)tag parent:(NSMenu *)parent title:(NSString *)title
862 NSMenuItem *item = [[NSMenuItem alloc] init];
863 NSMenu *menu = [[NSMenu alloc] initWithTitle:title];
865 [menu setAutoenablesItems:NO];
867 [item setTitle:title];
868 [item setSubmenu:menu];
871 if ([parent numberOfItems] <= idx) {
872 [parent addItem:item];
874 [parent insertItem:item atIndex:idx];
877 if ([mainMenuItems count] <= idx) {
878 [mainMenuItems addObject:item];
880 [mainMenuItems insertObject:item atIndex:idx];
883 shouldUpdateMainMenu = YES;
890 - (void)addMenuItemWithTag:(int)tag parent:(NSMenu *)parent
891 title:(NSString *)title tip:(NSString *)tip
892 keyEquivalent:(int)key modifiers:(int)mask
893 action:(NSString *)action atIndex:(int)idx
896 NSMenuItem *item = nil;
898 item = [[[NSMenuItem alloc] init] autorelease];
899 [item setTitle:title];
900 // TODO: Check that 'action' is a valid action (nothing will happen
901 // if it isn't, but it would be nice with a warning).
902 if (action) [item setAction:NSSelectorFromString(action)];
903 else [item setAction:@selector(vimMenuItemAction:)];
904 if (tip) [item setToolTip:tip];
907 NSString *keyString =
908 [NSString stringWithFormat:@"%C", key];
909 [item setKeyEquivalent:keyString];
910 [item setKeyEquivalentModifierMask:mask];
913 item = [NSMenuItem separatorItem];
916 // NOTE! The tag is used to idenfity which menu items were
917 // added by Vim (tag != 0) and which were added by the AppKit
921 if ([parent numberOfItems] <= idx) {
922 [parent addItem:item];
924 [parent insertItem:item atIndex:idx];
929 - (void)updateMainMenu
931 NSMenu *mainMenu = [NSApp mainMenu];
933 // Stop NSApp from updating the Window menu.
934 [NSApp setWindowsMenu:nil];
936 // Remove all menus from main menu (except the MacVim menu).
937 int i, count = [mainMenu numberOfItems];
938 for (i = count-1; i > 0; --i) {
939 [mainMenu removeItemAtIndex:i];
942 // Add menus from 'mainMenuItems' to main menu.
943 count = [mainMenuItems count];
944 for (i = 0; i < count; ++i) {
945 [mainMenu addItem:[mainMenuItems objectAtIndex:i]];
948 // Set the new Window menu.
949 // TODO! Need to look for 'Window' in all localized languages.
950 NSMenu *windowMenu = [[mainMenu itemWithTitle:@"Window"] submenu];
952 // Remove all AppKit owned menu items (tag == 0); they will be added
953 // again when setWindowsMenu: is called.
954 count = [windowMenu numberOfItems];
955 for (i = count-1; i >= 0; --i) {
956 NSMenuItem *item = [windowMenu itemAtIndex:i];
958 [windowMenu removeItem:item];
962 [NSApp setWindowsMenu:windowMenu];
965 shouldUpdateMainMenu = NO;
968 - (NSToolbarItem *)toolbarItemForTag:(int)tag index:(int *)index
970 if (!toolbar) return nil;
972 NSArray *items = [toolbar items];
973 int i, count = [items count];
974 for (i = 0; i < count; ++i) {
975 NSToolbarItem *item = [items objectAtIndex:i];
976 if ([item tag] == tag) {
977 if (index) *index = i;
985 - (IBAction)toolbarAction:(id)sender
987 NSLog(@"%s%@", _cmd, sender);
990 - (void)addToolbarItemToDictionaryWithTag:(int)tag label:(NSString *)title
991 toolTip:(NSString *)tip icon:(NSString *)icon
993 // NOTE! 'title' is nul for separator item. Since this is already defined
994 // by Coca, we don't need to do anything here.
997 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:title];
999 [item setLabel:title];
1000 [item setToolTip:tip];
1001 [item setAction:@selector(vimMenuItemAction:)];
1002 [item setAutovalidates:NO];
1004 NSImage *img = [NSImage imageNamed:icon];
1006 NSLog(@"WARNING: Could not find image with name '%@' to use as toolbar"
1007 " image for identifier '%@';"
1008 " using default toolbar icon '%@' instead.",
1009 icon, title, DefaultToolbarImageName);
1011 img = [NSImage imageNamed:DefaultToolbarImageName];
1014 [item setImage:img];
1016 [toolbarItemDict setObject:item forKey:title];
1021 - (void)addToolbarItemWithTag:(int)tag label:(NSString *)label tip:(NSString
1022 *)tip icon:(NSString *)icon atIndex:(int)idx
1024 if (!toolbar) return;
1026 [self addToolbarItemToDictionaryWithTag:tag label:label toolTip:tip
1029 int maxIdx = [[toolbar items] count];
1030 if (maxIdx < idx) idx = maxIdx;
1032 // If 'label' is nul, insert a separator.
1033 if (!label) label = NSToolbarSeparatorItemIdentifier;
1034 [toolbar insertItemWithItemIdentifier:label atIndex:idx];
1038 - (void)connectionDidDie:(NSNotification *)notification
1040 //NSLog(@"A MMVimController lost its connection to the backend; "
1041 // "closing the controller.");
1042 [windowController close];
1046 - (BOOL)executeActionWithName:(NSString *)name
1049 static NSDictionary *actionDict = nil;
1052 NSBundle *mainBundle = [NSBundle mainBundle];
1053 NSString *path = [mainBundle pathForResource:@"Actions"
1056 actionDict = [[NSDictionary alloc] initWithContentsOfFile:path];
1057 NSLog(@"Actions = %@", actionDict);
1059 NSLog(@"WARNING: Failed to load dictionary of actions "
1060 "(Actions.plist).");
1065 if ([actionDict objectForKey:name]) {
1066 NSLog(@"Executing action %@", name);
1067 SEL sel = NSSelectorFromString(name);
1069 if ([NSApp sendAction:sel to:nil from:self])
1072 NSLog(@"WARNING: Failed to send action");
1074 NSLog(@"WARNING: Action with name '%@' cannot be executed.", name);
1081 @end // MMVimController (Private)
1085 @implementation NSColor (MMProtocol)
1087 + (NSColor *)colorWithRgbInt:(int)rgb
1089 float r = ((rgb>>16) & 0xff)/255.0f;
1090 float g = ((rgb>>8) & 0xff)/255.0f;
1091 float b = (rgb & 0xff)/255.0f;
1093 return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:1.0f];
1096 @end // NSColor (MMProtocol)