Add handling IM state
[MacVim/KaoriYa.git] / src / MacVim / MacVim.m
blob4ee113d957fff81a5d8ddab2d52fe1363418ac25
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  * MacVim.m:  Code shared between Vim and MacVim.
12  */
14 #import "MacVim.h"
16 char *MessageStrings[] = 
18     "INVALID MESSAGE ID",
19     "OpenWindowMsgID",
20     "InsertTextMsgID",
21     "KeyDownMsgID",
22     "CmdKeyMsgID",
23     "BatchDrawMsgID",
24     "SelectTabMsgID",
25     "CloseTabMsgID",
26     "AddNewTabMsgID",
27     "DraggedTabMsgID",
28     "UpdateTabBarMsgID",
29     "ShowTabBarMsgID",
30     "HideTabBarMsgID",
31     "SetTextRowsMsgID",
32     "SetTextColumnsMsgID",
33     "SetTextDimensionsMsgID",
34     "LiveResizeMsgID",
35     "SetTextDimensionsReplyMsgID",
36     "SetWindowTitleMsgID",
37     "ScrollWheelMsgID",
38     "MouseDownMsgID",
39     "MouseUpMsgID",
40     "MouseDraggedMsgID",
41     "FlushQueueMsgID",
42     "AddMenuMsgID",
43     "AddMenuItemMsgID",
44     "RemoveMenuItemMsgID",
45     "EnableMenuItemMsgID",
46     "ExecuteMenuMsgID",
47     "ShowToolbarMsgID",
48     "ToggleToolbarMsgID",
49     "CreateScrollbarMsgID",
50     "DestroyScrollbarMsgID",
51     "ShowScrollbarMsgID",
52     "SetScrollbarPositionMsgID",
53     "SetScrollbarThumbMsgID",
54     "ScrollbarEventMsgID",
55     "SetFontMsgID",
56     "SetWideFontMsgID",
57     "VimShouldCloseMsgID",
58     "SetDefaultColorsMsgID",
59     "ExecuteActionMsgID",
60     "DropFilesMsgID",
61     "DropStringMsgID",
62     "ShowPopupMenuMsgID",
63     "GotFocusMsgID",
64     "LostFocusMsgID",
65     "MouseMovedMsgID",
66     "SetMouseShapeMsgID",
67     "AdjustLinespaceMsgID",
68     "ActivateMsgID",
69     "SetServerNameMsgID",
70     "EnterFullscreenMsgID",
71     "LeaveFullscreenMsgID",
72     "BuffersNotModifiedMsgID",
73     "BuffersModifiedMsgID",
74     "AddInputMsgID",
75     "SetPreEditPositionMsgID",
76     "TerminateNowMsgID",
77     "XcodeModMsgID",
78     "EnableAntialiasMsgID",
79     "DisableAntialiasMsgID",
80     "SetVimStateMsgID",
81     "SetDocumentFilenameMsgID",
82     "OpenWithArgumentsMsgID",
83     "CloseWindowMsgID",
84     "SetFullscreenColorMsgID",
85     "ShowFindReplaceDialogMsgID",
86     "FindReplaceMsgID",
87     "ActivateKeyScriptID",
88     "DeactivateKeyScriptID",
89     "EnableImControlMsgID",
90     "DisableImControlMsgID",
91     "ActivatedImMsgID",
92     "DeactivatedImMsgID",
93     "BrowseForFileMsgID",
94     "ShowDialogMsgID",
95     "END OF MESSAGE IDs"     // NOTE: Must be last!
101 // Argument used to stop MacVim from opening an empty window on startup
102 // (techincally this is a user default but should not be used as such).
103 NSString *MMNoWindowKey = @"MMNoWindow";
105 // Vim pasteboard type (holds motion type + string)
106 NSString *VimPBoardType = @"VimPBoardType";
110 // Create a string holding the labels of all messages in message queue for
111 // debugging purposes (condense some messages since there may typically be LOTS
112 // of them on a queue).
113     NSString *
114 debugStringForMessageQueue(NSArray *queue)
116     NSMutableString *s = [NSMutableString new];
117     unsigned i, count = [queue count];
118     int item = 0, menu = 0, enable = 0;
119     for (i = 0; i < count; i += 2) {
120         NSData *value = [queue objectAtIndex:i];
121         int msgid = *((int*)[value bytes]);
122         if (msgid < 1 || msgid >= LastMsgID)
123             continue;
124         if (msgid == AddMenuItemMsgID) ++item;
125         else if (msgid == AddMenuMsgID) ++menu;
126         else if (msgid == EnableMenuItemMsgID) ++enable;
127         else [s appendFormat:@"%s ", MessageStrings[msgid]];
128     }
129     if (item > 0) [s appendFormat:@"AddMenuItemMsgID(%d) ", item];
130     if (menu > 0) [s appendFormat:@"AddMenuMsgID(%d) ", menu];
131     if (enable > 0) [s appendFormat:@"EnableMenuItemMsgID(%d) ", enable];
133     return [s autorelease];
139 @implementation NSString (MMExtras)
141 - (NSString *)stringByEscapingSpecialFilenameCharacters
143     // NOTE: This code assumes that no characters already have been escaped.
144     NSMutableString *string = [self mutableCopy];
146     [string replaceOccurrencesOfString:@"\\"
147                             withString:@"\\\\"
148                                options:NSLiteralSearch
149                                  range:NSMakeRange(0, [string length])];
150     [string replaceOccurrencesOfString:@" "
151                             withString:@"\\ "
152                                options:NSLiteralSearch
153                                  range:NSMakeRange(0, [string length])];
154     [string replaceOccurrencesOfString:@"\t"
155                             withString:@"\\\t "
156                                options:NSLiteralSearch
157                                  range:NSMakeRange(0, [string length])];
158     [string replaceOccurrencesOfString:@"%"
159                             withString:@"\\%"
160                                options:NSLiteralSearch
161                                  range:NSMakeRange(0, [string length])];
162     [string replaceOccurrencesOfString:@"#"
163                             withString:@"\\#"
164                                options:NSLiteralSearch
165                                  range:NSMakeRange(0, [string length])];
166     [string replaceOccurrencesOfString:@"|"
167                             withString:@"\\|"
168                                options:NSLiteralSearch
169                                  range:NSMakeRange(0, [string length])];
170     [string replaceOccurrencesOfString:@"\""
171                             withString:@"\\\""
172                                options:NSLiteralSearch
173                                  range:NSMakeRange(0, [string length])];
175     return [string autorelease];
178 @end // NSString (MMExtras)
182 @implementation NSColor (MMExtras)
184 + (NSColor *)colorWithRgbInt:(unsigned)rgb
186     float r = ((rgb>>16) & 0xff)/255.0f;
187     float g = ((rgb>>8) & 0xff)/255.0f;
188     float b = (rgb & 0xff)/255.0f;
190     return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:1.0f];
193 + (NSColor *)colorWithArgbInt:(unsigned)argb
195     float a = ((argb>>24) & 0xff)/255.0f;
196     float r = ((argb>>16) & 0xff)/255.0f;
197     float g = ((argb>>8) & 0xff)/255.0f;
198     float b = (argb & 0xff)/255.0f;
200     return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:a];
203 @end // NSColor (MMExtras)
208 @implementation NSDictionary (MMExtras)
210 + (id)dictionaryWithData:(NSData *)data
212     id plist = [NSPropertyListSerialization
213             propertyListFromData:data
214                 mutabilityOption:NSPropertyListImmutable
215                           format:NULL
216                 errorDescription:NULL];
218     return [plist isKindOfClass:[NSDictionary class]] ? plist : nil;
221 - (NSData *)dictionaryAsData
223     return [NSPropertyListSerialization dataFromPropertyList:self
224             format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL];
227 @end
232 @implementation NSMutableDictionary (MMExtras)
234 + (id)dictionaryWithData:(NSData *)data
236     id plist = [NSPropertyListSerialization
237             propertyListFromData:data
238                 mutabilityOption:NSPropertyListMutableContainers
239                           format:NULL
240                 errorDescription:NULL];
242     return [plist isKindOfClass:[NSMutableDictionary class]] ? plist : nil;
245 @end