Improve <D-w> behaviour
[MacVim.git] / src / MacVim / MacVim.m
bloba2edd6cf0d1b5c1f76f5c876dffc7b6aefe95c26
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",
73     "ODBEditMsgID",
74     "XcodeModMsgID",
75     "LiveResizeMsgID",
76     "EnableAntialiasMsgID",
77     "DisableAntialiasMsgID",
78     "CloseMsgID",
84 // NSUserDefaults keys
85 NSString *MMNoWindowKey                 = @"MMNoWindow";
86 NSString *MMTabMinWidthKey              = @"MMTabMinWidth";
87 NSString *MMTabMaxWidthKey              = @"MMTabMaxWidth";
88 NSString *MMTabOptimumWidthKey          = @"MMTabOptimumWidth";
89 NSString *MMTextInsetLeftKey            = @"MMTextInsetLeft";
90 NSString *MMTextInsetRightKey           = @"MMTextInsetRight";
91 NSString *MMTextInsetTopKey             = @"MMTextInsetTop";
92 NSString *MMTextInsetBottomKey          = @"MMTextInsetBottom";
93 NSString *MMTerminateAfterLastWindowClosedKey
94                                         = @"MMTerminateAfterLastWindowClosed";
95 NSString *MMTypesetterKey               = @"MMTypesetter";
96 NSString *MMCellWidthMultiplierKey      = @"MMCellWidthMultiplier";
97 NSString *MMBaselineOffsetKey           = @"MMBaselineOffset";
98 NSString *MMTranslateCtrlClickKey       = @"MMTranslateCtrlClick";
99 NSString *MMTopLeftPointKey             = @"MMTopLeftPoint";
100 NSString *MMOpenFilesInTabsKey          = @"MMOpenFilesInTabs";
101 NSString *MMNoFontSubstitutionKey       = @"MMNoFontSubstitution";
102 NSString *MMLoginShellKey               = @"MMLoginShell";
103 NSString *MMAtsuiRendererKey            = @"MMAtsuiRenderer";
104 NSString *MMUntitledWindowKey           = @"MMUntitledWindow";
105 NSString *MMTexturedWindowKey           = @"MMTexturedWindow";
106 NSString *MMZoomBothKey                 = @"MMZoomBoth";
107 NSString *MMCurrentPreferencePaneKey    = @"MMCurrentPreferencePane";
108 NSString *MMLoginShellCommandKey        = @"MMLoginShellCommand";
109 NSString *MMLoginShellArgumentKey       = @"MMLoginShellArgument";
114     ATSFontContainerRef
115 loadFonts()
117     // This loads all fonts from the Resources folder.  The fonts are only
118     // available to the process which loaded them, so loading has to be done
119     // once for MacVim and an additional time for each Vim process.  The
120     // returned container ref should be used to deactiave the font.
121     //
122     // (Code taken from cocoadev.com)
123     ATSFontContainerRef fontContainerRef = 0;
124     NSString *fontsFolder = [[NSBundle mainBundle] resourcePath];    
125     if (fontsFolder) {
126         NSURL *fontsURL = [NSURL fileURLWithPath:fontsFolder];
127         if (fontsURL) {
128             FSRef fsRef;
129             FSSpec fsSpec;
130             CFURLGetFSRef((CFURLRef)fontsURL, &fsRef);
132             if (FSGetCatalogInfo(&fsRef, kFSCatInfoNone, NULL, NULL, &fsSpec,
133                         NULL) == noErr) {
134                 ATSFontActivateFromFileSpecification(&fsSpec,
135                         kATSFontContextLocal, kATSFontFormatUnspecified, NULL,
136                         kATSOptionFlagsDefault, &fontContainerRef);
137             }
138         }
139     }
141     return fontContainerRef;
147     NSString *
148 buildTabDropCommand(NSArray *filenames)
150     // Create a command line string that will open the specified files in tabs.
152     if (!filenames || [filenames count] == 0)
153         return [NSString string];
155     NSMutableString *cmd = [NSMutableString stringWithString:
156             @"<C-\\><C-N>:tab drop"];
158     NSEnumerator *e = [filenames objectEnumerator];
159     id o;
160     while ((o = [e nextObject])) {
161         NSString *file = [o stringByEscapingSpecialFilenameCharacters];
162         [cmd appendString:@" "];
163         [cmd appendString:file];
164     }
166     [cmd appendString:@"|redr|f<CR>"];
168     return cmd;
171     NSString *
172 buildSelectRangeCommand(NSRange range)
174     // Build a command line string that will select the given range of lines.
175     // If range.length == 0, then position the cursor on the given line but do
176     // not select.
178     if (range.location == NSNotFound)
179         return [NSString string];
181     NSString *cmd;
182     if (range.length > 0) {
183         cmd = [NSString stringWithFormat:@"<C-\\><C-N>%dGV%dGz.0",
184                 NSMaxRange(range), range.location];
185     } else {
186         cmd = [NSString stringWithFormat:@"<C-\\><C-N>%dGz.0", range.location];
187     }
189     return cmd;
192     NSString *
193 buildSearchTextCommand(NSString *searchText)
195     // TODO: Searching is an exclusive motion, so if the pattern would match on
196     // row 0 column 0 then this pattern will miss that match.
197     return [NSString stringWithFormat:@"<C-\\><C-N>gg/\\c%@<CR>", searchText];
203 @implementation NSString (MMExtras)
205 - (NSString *)stringByEscapingSpecialFilenameCharacters
207     // NOTE: This code assumes that no characters already have been escaped.
208     NSMutableString *string = [self mutableCopy];
210     [string replaceOccurrencesOfString:@"\\"
211                             withString:@"\\\\"
212                                options:NSLiteralSearch
213                                  range:NSMakeRange(0, [string length])];
214     [string replaceOccurrencesOfString:@" "
215                             withString:@"\\ "
216                                options:NSLiteralSearch
217                                  range:NSMakeRange(0, [string length])];
218     [string replaceOccurrencesOfString:@"\t"
219                             withString:@"\\\t "
220                                options:NSLiteralSearch
221                                  range:NSMakeRange(0, [string length])];
222     [string replaceOccurrencesOfString:@"%"
223                             withString:@"\\%"
224                                options:NSLiteralSearch
225                                  range:NSMakeRange(0, [string length])];
226     [string replaceOccurrencesOfString:@"#"
227                             withString:@"\\#"
228                                options:NSLiteralSearch
229                                  range:NSMakeRange(0, [string length])];
230     [string replaceOccurrencesOfString:@"|"
231                             withString:@"\\|"
232                                options:NSLiteralSearch
233                                  range:NSMakeRange(0, [string length])];
234     [string replaceOccurrencesOfString:@"\""
235                             withString:@"\\\""
236                                options:NSLiteralSearch
237                                  range:NSMakeRange(0, [string length])];
239     return [string autorelease];
242 @end // NSString (MMExtras)
246 @implementation NSIndexSet (MMExtras)
248 + (id)indexSetWithVimList:(NSString *)list
250     NSMutableIndexSet *idxSet = [NSMutableIndexSet indexSet];
251     NSArray *array = [list componentsSeparatedByString:@"\n"];
252     unsigned i, count = [array count];
254     for (i = 0; i < count; ++i) {
255         NSString *entry = [array objectAtIndex:i];
256         if ([entry intValue] > 0)
257             [idxSet addIndex:i];
258     }
260     return idxSet;
263 @end // NSIndexSet (MMExtras)
268 @implementation NSColor (MMExtras)
270 + (NSColor *)colorWithRgbInt:(unsigned)rgb
272     float r = ((rgb>>16) & 0xff)/255.0f;
273     float g = ((rgb>>8) & 0xff)/255.0f;
274     float b = (rgb & 0xff)/255.0f;
276     return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:1.0f];
279 + (NSColor *)colorWithArgbInt:(unsigned)argb
281     float a = ((argb>>24) & 0xff)/255.0f;
282     float r = ((argb>>16) & 0xff)/255.0f;
283     float g = ((argb>>8) & 0xff)/255.0f;
284     float b = (argb & 0xff)/255.0f;
286     return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:a];
289 @end // NSColor (MMExtras)