Add support for :winpos
[MacVim.git] / src / MacVim / MacVim.m
blobce060d2494f90e6fcd94183a54c05836c4dd26e4
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"
17 char *MessageStrings[] = 
19     "INVALID MESSAGE ID",
20     "OpenWindowMsgID",
21     "KeyDownMsgID",
22     "BatchDrawMsgID",
23     "SelectTabMsgID",
24     "CloseTabMsgID",
25     "AddNewTabMsgID",
26     "DraggedTabMsgID",
27     "UpdateTabBarMsgID",
28     "ShowTabBarMsgID",
29     "HideTabBarMsgID",
30     "SetTextRowsMsgID",
31     "SetTextColumnsMsgID",
32     "SetTextDimensionsMsgID",
33     "LiveResizeMsgID",
34     "SetTextDimensionsReplyMsgID",
35     "SetWindowTitleMsgID",
36     "ScrollWheelMsgID",
37     "MouseDownMsgID",
38     "MouseUpMsgID",
39     "MouseDraggedMsgID",
40     "FlushQueueMsgID",
41     "AddMenuMsgID",
42     "AddMenuItemMsgID",
43     "RemoveMenuItemMsgID",
44     "EnableMenuItemMsgID",
45     "ExecuteMenuMsgID",
46     "ShowToolbarMsgID",
47     "ToggleToolbarMsgID",
48     "CreateScrollbarMsgID",
49     "DestroyScrollbarMsgID",
50     "ShowScrollbarMsgID",
51     "SetScrollbarPositionMsgID",
52     "SetScrollbarThumbMsgID",
53     "ScrollbarEventMsgID",
54     "SetFontMsgID",
55     "SetWideFontMsgID",
56     "VimShouldCloseMsgID",
57     "SetDefaultColorsMsgID",
58     "ExecuteActionMsgID",
59     "DropFilesMsgID",
60     "DropStringMsgID",
61     "ShowPopupMenuMsgID",
62     "GotFocusMsgID",
63     "LostFocusMsgID",
64     "MouseMovedMsgID",
65     "SetMouseShapeMsgID",
66     "AdjustLinespaceMsgID",
67     "ActivateMsgID",
68     "SetServerNameMsgID",
69     "EnterFullscreenMsgID",
70     "LeaveFullscreenMsgID",
71     "BuffersNotModifiedMsgID",
72     "BuffersModifiedMsgID",
73     "AddInputMsgID",
74     "SetPreEditPositionMsgID",
75     "TerminateNowMsgID",
76     "XcodeModMsgID",
77     "EnableAntialiasMsgID",
78     "DisableAntialiasMsgID",
79     "SetVimStateMsgID",
80     "SetDocumentFilenameMsgID",
81     "OpenWithArgumentsMsgID",
82     "CloseWindowMsgID",
83     "SetFullscreenColorMsgID",
84     "ShowFindReplaceDialogMsgID",
85     "FindReplaceMsgID",
86     "ActivateKeyScriptMsgID",
87     "DeactivateKeyScriptMsgID",
88     "EnableImControlMsgID",
89     "DisableImControlMsgID",
90     "ActivatedImMsgID",
91     "DeactivatedImMsgID",
92     "BrowseForFileMsgID",
93     "ShowDialogMsgID",
94     "NetBeansMsgID",
95     "SetMarkedTextMsgID",
96     "ZoomMsgID",
97     "SetWindowPositionMsgID",
98     "END OF MESSAGE IDs"     // NOTE: Must be last!
104 NSString *MMLogLevelKey     = @"MMLogLevel";
105 NSString *MMLogToStdErrKey  = @"MMLogToStdErr";
107 // Argument used to stop MacVim from opening an empty window on startup
108 // (techincally this is a user default but should not be used as such).
109 NSString *MMNoWindowKey = @"MMNoWindow";
111 NSString *MMAutosaveRowsKey    = @"MMAutosaveRows";
112 NSString *MMAutosaveColumnsKey = @"MMAutosaveColumns";
113 NSString *MMRendererKey        = @"MMRenderer";
115 // Vim pasteboard type (holds motion type + string)
116 NSString *VimPboardType = @"VimPboardType";
117 // Vim find pasteboard type (string contains Vim regex patterns)
118 NSString *VimFindPboardType = @"VimFindPboardType";
120 int ASLogLevel = ASL_LEVEL_NOTICE;
124 // Create a string holding the labels of all messages in message queue for
125 // debugging purposes (condense some messages since there may typically be LOTS
126 // of them on a queue).
127     NSString *
128 debugStringForMessageQueue(NSArray *queue)
130     NSMutableString *s = [NSMutableString new];
131     unsigned i, count = [queue count];
132     int item = 0, menu = 0, enable = 0, remove = 0;
133     int sets = 0, sett = 0, shows = 0, cres = 0, dess = 0;
134     for (i = 0; i < count; i += 2) {
135         NSData *value = [queue objectAtIndex:i];
136         int msgid = *((int*)[value bytes]);
137         if (msgid < 1 || msgid >= LastMsgID)
138             continue;
139         if (msgid == AddMenuItemMsgID) ++item;
140         else if (msgid == AddMenuMsgID) ++menu;
141         else if (msgid == EnableMenuItemMsgID) ++enable;
142         else if (msgid == RemoveMenuItemMsgID) ++remove;
143         else if (msgid == SetScrollbarPositionMsgID) ++sets;
144         else if (msgid == SetScrollbarThumbMsgID) ++sett;
145         else if (msgid == ShowScrollbarMsgID) ++shows;
146         else if (msgid == CreateScrollbarMsgID) ++cres;
147         else if (msgid == DestroyScrollbarMsgID) ++dess;
148         else [s appendFormat:@"%s ", MessageStrings[msgid]];
149     }
150     if (item > 0) [s appendFormat:@"AddMenuItemMsgID(%d) ", item];
151     if (menu > 0) [s appendFormat:@"AddMenuMsgID(%d) ", menu];
152     if (enable > 0) [s appendFormat:@"EnableMenuItemMsgID(%d) ", enable];
153     if (remove > 0) [s appendFormat:@"RemoveMenuItemMsgID(%d) ", remove];
154     if (sets > 0) [s appendFormat:@"SetScrollbarPositionMsgID(%d) ", sets];
155     if (sett > 0) [s appendFormat:@"SetScrollbarThumbMsgID(%d) ", sett];
156     if (shows > 0) [s appendFormat:@"ShowScrollbarMsgID(%d) ", shows];
157     if (cres > 0) [s appendFormat:@"CreateScrollbarMsgID(%d) ", cres];
158     if (dess > 0) [s appendFormat:@"DestroyScrollbarMsgID(%d) ", dess];
160     return [s autorelease];
166 @implementation NSString (MMExtras)
168 - (NSString *)stringByEscapingSpecialFilenameCharacters
170     // NOTE: This code assumes that no characters already have been escaped.
171     NSMutableString *string = [self mutableCopy];
173     [string replaceOccurrencesOfString:@"\\"
174                             withString:@"\\\\"
175                                options:NSLiteralSearch
176                                  range:NSMakeRange(0, [string length])];
177     [string replaceOccurrencesOfString:@" "
178                             withString:@"\\ "
179                                options:NSLiteralSearch
180                                  range:NSMakeRange(0, [string length])];
181     [string replaceOccurrencesOfString:@"\t"
182                             withString:@"\\\t "
183                                options:NSLiteralSearch
184                                  range:NSMakeRange(0, [string length])];
185     [string replaceOccurrencesOfString:@"%"
186                             withString:@"\\%"
187                                options:NSLiteralSearch
188                                  range:NSMakeRange(0, [string length])];
189     [string replaceOccurrencesOfString:@"#"
190                             withString:@"\\#"
191                                options:NSLiteralSearch
192                                  range:NSMakeRange(0, [string length])];
193     [string replaceOccurrencesOfString:@"|"
194                             withString:@"\\|"
195                                options:NSLiteralSearch
196                                  range:NSMakeRange(0, [string length])];
197     [string replaceOccurrencesOfString:@"\""
198                             withString:@"\\\""
199                                options:NSLiteralSearch
200                                  range:NSMakeRange(0, [string length])];
202     return [string autorelease];
205 - (NSString *)stringByRemovingFindPatterns
207     // Remove some common patterns added to search strings that other apps are
208     // not aware of.
210     NSMutableString *string = [self mutableCopy];
212     // Added when doing * search
213     [string replaceOccurrencesOfString:@"\\<"
214                             withString:@""
215                                options:NSLiteralSearch
216                                  range:NSMakeRange(0, [string length])];
217     [string replaceOccurrencesOfString:@"\\>"
218                             withString:@""
219                                options:NSLiteralSearch
220                                  range:NSMakeRange(0, [string length])];
221     // \V = match whole word
222     [string replaceOccurrencesOfString:@"\\V"
223                             withString:@""
224                                options:NSLiteralSearch
225                                  range:NSMakeRange(0, [string length])];
226     // \c = case insensitive, \C = case sensitive
227     [string replaceOccurrencesOfString:@"\\c"
228                             withString:@""
229                                options:NSCaseInsensitiveSearch|NSLiteralSearch
230                                  range:NSMakeRange(0, [string length])];
232     return [string autorelease];
235 @end // NSString (MMExtras)
239 @implementation NSColor (MMExtras)
241 + (NSColor *)colorWithRgbInt:(unsigned)rgb
243     float r = ((rgb>>16) & 0xff)/255.0f;
244     float g = ((rgb>>8) & 0xff)/255.0f;
245     float b = (rgb & 0xff)/255.0f;
247     return [NSColor colorWithDeviceRed:r green:g blue:b alpha:1.0f];
250 + (NSColor *)colorWithArgbInt:(unsigned)argb
252     float a = ((argb>>24) & 0xff)/255.0f;
253     float r = ((argb>>16) & 0xff)/255.0f;
254     float g = ((argb>>8) & 0xff)/255.0f;
255     float b = (argb & 0xff)/255.0f;
257     return [NSColor colorWithDeviceRed:r green:g blue:b alpha:a];
260 @end // NSColor (MMExtras)
265 @implementation NSDictionary (MMExtras)
267 + (id)dictionaryWithData:(NSData *)data
269     id plist = [NSPropertyListSerialization
270             propertyListFromData:data
271                 mutabilityOption:NSPropertyListImmutable
272                           format:NULL
273                 errorDescription:NULL];
275     return [plist isKindOfClass:[NSDictionary class]] ? plist : nil;
278 - (NSData *)dictionaryAsData
280     return [NSPropertyListSerialization dataFromPropertyList:self
281             format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL];
284 @end
289 @implementation NSMutableDictionary (MMExtras)
291 + (id)dictionaryWithData:(NSData *)data
293     id plist = [NSPropertyListSerialization
294             propertyListFromData:data
295                 mutabilityOption:NSPropertyListMutableContainers
296                           format:NULL
297                 errorDescription:NULL];
299     return [plist isKindOfClass:[NSMutableDictionary class]] ? plist : nil;
302 @end
307     void
308 ASLInit()
310     NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
312     // Allow for changing the log level via user defaults.  If no key is found
313     // the default log level will be used (which for ASL is to log everything
314     // up to ASL_LEVEL_NOTICE).  This key is an integer which corresponds to
315     // the ASL_LEVEL_* macros (0 is most severe, 7 is debug level).
316     id logLevelObj = [ud objectForKey:MMLogLevelKey];
317     if (logLevelObj) {
318         int logLevel = [logLevelObj intValue];
319         if (logLevel < 0) logLevel = 0;
320         if (logLevel > ASL_LEVEL_DEBUG) logLevel = ASL_LEVEL_DEBUG;
322         ASLogLevel = logLevel;
323         asl_set_filter(NULL, ASL_FILTER_MASK_UPTO(logLevel));
324     }
326     // Allow for changing whether a copy of each log should be sent to stderr
327     // (this defaults to NO if this key is missing in the user defaults
328     // database).  The above filter mask is applied to logs going to stderr,
329     // contrary to how "vanilla" ASL works.
330     BOOL logToStdErr = [ud boolForKey:MMLogToStdErrKey];
331     if (logToStdErr)
332         asl_add_log_file(NULL, 2);  // The file descriptor for stderr is 2