Preserve swap files after crash
[MacVim.git] / src / MacVim / MacVim.m
blob5e718fd6d0f0bf842e573891752706daf9ce02ae
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     "OpenVimWindowMsgID",
20     "InsertTextMsgID",
21     "KeyDownMsgID",
22     "CmdKeyMsgID",
23     "BatchDrawMsgID",
24     "SelectTabMsgID",
25     "CloseTabMsgID",
26     "AddNewTabMsgID",
27     "DraggedTabMsgID",
28     "UpdateTabBarMsgID",
29     "ShowTabBarMsgID",
30     "HideTabBarMsgID",
31     "SetTextDimensionsMsgID",
32     "SetWindowTitleMsgID",
33     "ScrollWheelMsgID",
34     "MouseDownMsgID",
35     "MouseUpMsgID",
36     "MouseDraggedMsgID",
37     "FlushQueueMsgID",
38     "AddMenuMsgID",
39     "AddMenuItemMsgID",
40     "RemoveMenuItemMsgID",
41     "EnableMenuItemMsgID",
42     "ExecuteMenuMsgID",
43     "ShowToolbarMsgID",
44     "ToggleToolbarMsgID",
45     "CreateScrollbarMsgID",
46     "DestroyScrollbarMsgID",
47     "ShowScrollbarMsgID",
48     "SetScrollbarPositionMsgID",
49     "SetScrollbarThumbMsgID",
50     "ScrollbarEventMsgID",
51     "SetFontMsgID",
52     "SetWideFontMsgID",
53     "VimShouldCloseMsgID",
54     "SetDefaultColorsMsgID",
55     "ExecuteActionMsgID",
56     "DropFilesMsgID",
57     "DropStringMsgID",
58     "ShowPopupMenuMsgID",
59     "GotFocusMsgID",
60     "LostFocusMsgID",
61     "MouseMovedMsgID",
62     "SetMouseShapeMsgID",
63     "AdjustLinespaceMsgID",
64     "ActivateMsgID",
65     "SetServerNameMsgID",
66     "EnterFullscreenMsgID",
67     "LeaveFullscreenMsgID",
68     "BuffersNotModifiedMsgID",
69     "BuffersModifiedMsgID",
70     "AddInputMsgID",
71     "SetPreEditPositionMsgID",
72     "TerminateNowMsgID",
78 // NSUserDefaults keys
79 NSString *MMNoWindowKey                 = @"MMNoWindow";
80 NSString *MMTabMinWidthKey              = @"MMTabMinWidth";
81 NSString *MMTabMaxWidthKey              = @"MMTabMaxWidth";
82 NSString *MMTabOptimumWidthKey          = @"MMTabOptimumWidth";
83 NSString *MMTextInsetLeftKey            = @"MMTextInsetLeft";
84 NSString *MMTextInsetRightKey           = @"MMTextInsetRight";
85 NSString *MMTextInsetTopKey             = @"MMTextInsetTop";
86 NSString *MMTextInsetBottomKey          = @"MMTextInsetBottom";
87 NSString *MMTerminateAfterLastWindowClosedKey
88                                         = @"MMTerminateAfterLastWindowClosed";
89 NSString *MMTypesetterKey               = @"MMTypesetter";
90 NSString *MMCellWidthMultiplierKey      = @"MMCellWidthMultiplier";
91 NSString *MMBaselineOffsetKey           = @"MMBaselineOffset";
92 NSString *MMTranslateCtrlClickKey       = @"MMTranslateCtrlClick";
93 NSString *MMTopLeftPointKey             = @"MMTopLeftPoint";
94 NSString *MMOpenFilesInTabsKey          = @"MMOpenFilesInTabs";
95 NSString *MMNoFontSubstitutionKey       = @"MMNoFontSubstitution";
96 NSString *MMLoginShellKey               = @"MMLoginShell";
101     ATSFontContainerRef
102 loadFonts()
104     // This loads all fonts from the Resources folder.  The fonts are only
105     // available to the process which loaded them, so loading has to be done
106     // once for MacVim and an additional time for each Vim process.  The
107     // returned container ref should be used to deactiave the font.
108     //
109     // (Code taken from cocoadev.com)
110     ATSFontContainerRef fontContainerRef = 0;
111     NSString *fontsFolder = [[NSBundle mainBundle] resourcePath];    
112     if (fontsFolder) {
113         NSURL *fontsURL = [NSURL fileURLWithPath:fontsFolder];
114         if (fontsURL) {
115             FSRef fsRef;
116             FSSpec fsSpec;
117             CFURLGetFSRef((CFURLRef)fontsURL, &fsRef);
119             if (FSGetCatalogInfo(&fsRef, kFSCatInfoNone, NULL, NULL, &fsSpec,
120                         NULL) == noErr) {
121                 ATSFontActivateFromFileSpecification(&fsSpec,
122                         kATSFontContextLocal, kATSFontFormatUnspecified, NULL,
123                         kATSOptionFlagsDefault, &fontContainerRef);
124             }
125         }
126     }
128     return fontContainerRef;
134 @implementation NSString (MMExtras)
136 - (NSString *)stringByEscapingSpecialFilenameCharacters
138     // NOTE: This code assumes that no characters already have been escaped.
139     NSMutableString *string = [self mutableCopy];
141     [string replaceOccurrencesOfString:@"\\"
142                             withString:@"\\\\"
143                                options:NSLiteralSearch
144                                  range:NSMakeRange(0, [string length])];
145     [string replaceOccurrencesOfString:@" "
146                             withString:@"\\ "
147                                options:NSLiteralSearch
148                                  range:NSMakeRange(0, [string length])];
149     [string replaceOccurrencesOfString:@"\t"
150                             withString:@"\\\t "
151                                options:NSLiteralSearch
152                                  range:NSMakeRange(0, [string length])];
153     [string replaceOccurrencesOfString:@"%"
154                             withString:@"\\%"
155                                options:NSLiteralSearch
156                                  range:NSMakeRange(0, [string length])];
157     [string replaceOccurrencesOfString:@"#"
158                             withString:@"\\#"
159                                options:NSLiteralSearch
160                                  range:NSMakeRange(0, [string length])];
161     [string replaceOccurrencesOfString:@"|"
162                             withString:@"\\|"
163                                options:NSLiteralSearch
164                                  range:NSMakeRange(0, [string length])];
165     [string replaceOccurrencesOfString:@"\""
166                             withString:@"\\\""
167                                options:NSLiteralSearch
168                                  range:NSMakeRange(0, [string length])];
170     return [string autorelease];
173 @end // NSString (MMExtras)
177 @implementation NSIndexSet (MMExtras)
179 + (id)indexSetWithVimList:(NSString *)list
181     NSMutableIndexSet *idxSet = [NSMutableIndexSet indexSet];
182     NSArray *array = [list componentsSeparatedByString:@"\n"];
183     unsigned i, count = [array count];
185     for (i = 0; i < count; ++i) {
186         NSString *entry = [array objectAtIndex:i];
187         if ([entry intValue] > 0)
188             [idxSet addIndex:i];
189     }
191     return idxSet;
194 @end // NSIndexSet (MMExtras)