Add function to print message queue
[MacVim.git] / src / MacVim / MacVim.m
bloba40b93f5ebaa5e7ca2dbe4c944127887aeccb765
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     "BrowseForFileMsgID",
90     "ShowDialogMsgID",
91     "END OF MESSAGE IDs"     // NOTE: Must be last!
97 // Argument used to stop MacVim from opening an empty window on startup
98 // (techincally this is a user default but should not be used as such).
99 NSString *MMNoWindowKey = @"MMNoWindow";
101 // Vim pasteboard type (holds motion type + string)
102 NSString *VimPBoardType = @"VimPBoardType";
106 // Create a string holding the labels of all messages in message queue for
107 // debugging purposes (condense some messages since there may typically be LOTS
108 // of them on a queue).
109     NSString *
110 debugStringForMessageQueue(NSArray *queue)
112     NSMutableString *s = [NSMutableString new];
113     unsigned i, count = [queue count];
114     int item = 0, menu = 0, enable = 0;
115     for (i = 0; i < count; i += 2) {
116         NSData *value = [queue objectAtIndex:i];
117         int msgid = *((int*)[value bytes]);
118         if (msgid < 1 || msgid >= LastMsgID)
119             continue;
120         if (msgid == AddMenuItemMsgID) ++item;
121         else if (msgid == AddMenuMsgID) ++menu;
122         else if (msgid == EnableMenuItemMsgID) ++enable;
123         else [s appendFormat:@"%s ", MessageStrings[msgid]];
124     }
125     if (item > 0) [s appendFormat:@"AddMenuItemMsgID(%d) ", item];
126     if (menu > 0) [s appendFormat:@"AddMenuMsgID(%d) ", menu];
127     if (enable > 0) [s appendFormat:@"EnableMenuItemMsgID(%d) ", enable];
129     return [s autorelease];
135 @implementation NSString (MMExtras)
137 - (NSString *)stringByEscapingSpecialFilenameCharacters
139     // NOTE: This code assumes that no characters already have been escaped.
140     NSMutableString *string = [self mutableCopy];
142     [string replaceOccurrencesOfString:@"\\"
143                             withString:@"\\\\"
144                                options:NSLiteralSearch
145                                  range:NSMakeRange(0, [string length])];
146     [string replaceOccurrencesOfString:@" "
147                             withString:@"\\ "
148                                options:NSLiteralSearch
149                                  range:NSMakeRange(0, [string length])];
150     [string replaceOccurrencesOfString:@"\t"
151                             withString:@"\\\t "
152                                options:NSLiteralSearch
153                                  range:NSMakeRange(0, [string length])];
154     [string replaceOccurrencesOfString:@"%"
155                             withString:@"\\%"
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])];
171     return [string autorelease];
174 @end // NSString (MMExtras)
178 @implementation NSColor (MMExtras)
180 + (NSColor *)colorWithRgbInt:(unsigned)rgb
182     float r = ((rgb>>16) & 0xff)/255.0f;
183     float g = ((rgb>>8) & 0xff)/255.0f;
184     float b = (rgb & 0xff)/255.0f;
186     return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:1.0f];
189 + (NSColor *)colorWithArgbInt:(unsigned)argb
191     float a = ((argb>>24) & 0xff)/255.0f;
192     float r = ((argb>>16) & 0xff)/255.0f;
193     float g = ((argb>>8) & 0xff)/255.0f;
194     float b = (argb & 0xff)/255.0f;
196     return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:a];
199 @end // NSColor (MMExtras)
204 @implementation NSDictionary (MMExtras)
206 + (id)dictionaryWithData:(NSData *)data
208     id plist = [NSPropertyListSerialization
209             propertyListFromData:data
210                 mutabilityOption:NSPropertyListImmutable
211                           format:NULL
212                 errorDescription:NULL];
214     return [plist isKindOfClass:[NSDictionary class]] ? plist : nil;
217 - (NSData *)dictionaryAsData
219     return [NSPropertyListSerialization dataFromPropertyList:self
220             format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL];
223 @end
228 @implementation NSMutableDictionary (MMExtras)
230 + (id)dictionaryWithData:(NSData *)data
232     id plist = [NSPropertyListSerialization
233             propertyListFromData:data
234                 mutabilityOption:NSPropertyListMutableContainers
235                           format:NULL
236                 errorDescription:NULL];
238     return [plist isKindOfClass:[NSMutableDictionary class]] ? plist : nil;
241 @end