Skip to content

Commit 1f30462

Browse files
authored
Merge pull request #1453 from ychin/launch-with-clean-defaults
Allow launching MacVim in clean defaults, add menu to open clean Vim
2 parents b0ba0b9 + fa83ca5 commit 1f30462

15 files changed

+195
-57
lines changed

runtime/doc/gui_mac.txt

+4
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ Vim options (see |macvim-options|). These settings are stored in the user
264264
defaults database and can be accessed via the "MacVim.Settings…"
265265
("MacVim.Preferences…" in macOS 12 Monterey and older) menu item.
266266

267+
If you want to open MacVim with its default settings, you can open it by
268+
passing `-IgnoreUserDefaults 1` to the launch arguments (see the man page on
269+
the "open" for how to do so).
270+
267271
*macvim-user-defaults*
268272
Not all entries in the user defaults database are exposed via the settings
269273
panel, usually because they should not be changed by the user under normal
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
menutrans New\ Clean\ Window 新規クリーンウインドウ
2+
menutrans New\ Clean\ Window\ (No\ Defaults) 新規クリーンウインドウ\ (デフォルトなし)
13
menutrans MacVim\ Help MacVim\ ヘルプ
24
menutrans MacVim\ Website MacVim\ Webサイト
35
menutrans Vim\ Tutor Vim\ 教本\ (チュートリアル)
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
menutrans New\ Clean\ Window 新建干净窗口
2+
menutrans New\ Clean\ Window\ (No\ Defaults) 新建干净窗口(不用\ Defaults)
13
menutrans MacVim\ Help MacVim帮助
24
menutrans MacVim\ Website MacVim网站
35
menutrans Vim\ Tutor Vim教程
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
menutrans New\ Clean\ Window 新增乾淨視窗
2+
menutrans New\ Clean\ Window\ (No\ Defaults) 新增乾淨視窗(不用\ Defaults)
13
menutrans MacVim\ Help MacVim輔助說明
24
menutrans MacVim\ Website MacVim網站
35
menutrans Vim\ Tutor Vim教程

runtime/menu.vim

+6-2
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ enddef
160160

161161
" File menu
162162
if has("gui_macvim")
163-
an <silent> 10.290 &File.New\ Window <Nop>
163+
an <silent> 10.290 &File.New\ Window <Nop>
164+
an <silent> 10.291 &File.New\ Clean\ Window <Nop>
165+
an <silent> 10.292 &File.New\ Clean\ Window\ (No\ Defaults) <Nop>
164166
an 10.295 &File.New\ Tab :tabnew<CR>
165167
tln 10.295 &File.New\ Tab <C-W>:tabnew<CR>
166168
an <silent> 10.310 &File.Open… <Nop>
@@ -1267,6 +1269,8 @@ if has("gui_macvim")
12671269
" action message is specified here via the :macmenu command.
12681270
"
12691271
macm File.New\ Window key=<D-n> action=newWindow:
1272+
macm File.New\ Clean\ Window key=<D-N> action=newWindowClean:
1273+
macm File.New\ Clean\ Window\ (No\ Defaults) key=<D-M-N> action=newWindowCleanNoDefaults: alt=YES
12701274
macm File.New\ Tab key=<D-t>
12711275
macm File.Open… key=<D-o> action=fileOpen:
12721276
macm File.Open\ Tab\.\.\.<Tab>:tabnew key=<D-T>
@@ -1417,4 +1421,4 @@ if has("touchbar")
14171421
endif
14181422
endif
14191423

1420-
" vim: set sw=2 :
1424+
" vim: set sw=2 tabstop=8 :

src/MacVim/Actions.plist

+10-6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
<string></string>
3737
<key>newWindow:</key>
3838
<string></string>
39+
<key>newWindowClean:</key>
40+
<string></string>
41+
<key>newWindowCleanNoDefaults:</key>
42+
<string></string>
3943
<key>openWebsite:</key>
4044
<string></string>
4145
<key>showWhatsNew:</key>
@@ -86,11 +90,11 @@
8690
<string></string>
8791
<key>stayLevelNormal:</key>
8892
<string></string>
89-
<key>_removeWindowFromStageManagerSet:</key>
90-
<string></string>
91-
<key>joinAllStageManagerSets:</key>
92-
<string></string>
93-
<key>unjoinAllStageManagerSets:</key>
94-
<string></string>
93+
<key>_removeWindowFromStageManagerSet:</key>
94+
<string></string>
95+
<key>joinAllStageManagerSets:</key>
96+
<string></string>
97+
<key>unjoinAllStageManagerSets:</key>
98+
<string></string>
9599
</dict>
96100
</plist>

src/MacVim/Base.lproj/MainMenu.xib

+17
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@
8181
<action selector="newWindow:" target="235" id="251"/>
8282
</connections>
8383
</menuItem>
84+
<menuItem title="New Clean Window" keyEquivalent="N" id="6vC-1c-PzC" userLabel="New Clean Window">
85+
<connections>
86+
<action selector="newWindowCleanAndActivate:" target="-1" id="0IK-4B-mIA"/>
87+
</connections>
88+
</menuItem>
89+
<menuItem title="New Clean Window (No Defaults)" alternate="YES" keyEquivalent="N" id="I52-K0-dKN" userLabel="New Clean Window (No Defaults)">
90+
<modifierMask key="keyEquivalentModifierMask" option="YES" command="YES"/>
91+
<connections>
92+
<action selector="newWindowCleanNoDefaultsAndActivate:" target="-1" id="nAy-O6-DrK"/>
93+
</connections>
94+
</menuItem>
8495
<menuItem title="Open…" keyEquivalent="o" id="261">
8596
<connections>
8697
<action selector="fileOpen:" target="235" id="277"/>
@@ -205,6 +216,12 @@
205216
<action selector="newWindowAndActivate:" target="235" id="350"/>
206217
</connections>
207218
</menuItem>
219+
<menuItem title="New Clean Window" id="pDQ-Dc-v9a" userLabel="New Clean Window">
220+
<attributedString key="attributedTitle"/>
221+
<connections>
222+
<action selector="newWindowCleanAndActivate:" target="-1" id="p1b-M2-0zQ"/>
223+
</connections>
224+
</menuItem>
208225
</items>
209226
<point key="canvasLocation" x="139" y="67"/>
210227
</menu>

src/MacVim/MMAppController.h

+18
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
, NSMenuItemValidation
3333
#endif
3434
> {
35+
enum NewWindowMode {
36+
NewWindowNormal = 0,
37+
NewWindowClean,
38+
NewWindowCleanNoDefaults,
39+
};
40+
3541
NSConnection *connection;
3642
NSMutableArray *vimControllers;
3743
NSString *openSelectionString;
@@ -82,13 +88,22 @@
8288
- (void)refreshAllResizeConstraints;
8389
- (void)refreshAllTextViews;
8490

91+
- (void)openNewWindow:(enum NewWindowMode)mode activate:(BOOL)activate;
92+
8593
//
8694
// NSMenuItemValidation
8795
//
8896
- (BOOL)validateMenuItem:(NSMenuItem *)item;
8997

98+
//
99+
// Actions exposed to Vim
100+
//
90101
- (IBAction)newWindow:(id)sender;
102+
- (IBAction)newWindowClean:(id)sender;
103+
- (IBAction)newWindowCleanNoDefaults:(id)sender;
91104
- (IBAction)newWindowAndActivate:(id)sender;
105+
- (IBAction)newWindowCleanAndActivate:(id)sender;
106+
- (IBAction)newWindowCleanNoDefaultsAndActivate:(id)sender;
92107
- (IBAction)fileOpen:(id)sender;
93108
- (IBAction)selectNextWindow:(id)sender;
94109
- (IBAction)selectPreviousWindow:(id)sender;
@@ -105,6 +120,9 @@
105120
- (IBAction)stayInBack:(id)sender;
106121
- (IBAction)stayLevelNormal:(id)sender;
107122

123+
//
124+
// NSUserInterfaceItemSearching
125+
//
108126
- (NSArray<NSString *> *)localizedTitlesForItem:(id)item;
109127
- (void)searchForItemsWithSearchString:(NSString *)searchString
110128
resultLimit:(NSInteger)resultLimit

src/MacVim/MMAppController.m

+104-46
Original file line numberDiff line numberDiff line change
@@ -166,45 +166,11 @@ - (void)inputSourceChanged:(NSNotification *)notification;
166166

167167
@implementation MMAppController
168168

169-
+ (void)initialize
169+
/// Register the default settings for MacVim. Supports an optional
170+
/// "-IgnoreUserDefaults 1" command-line argument, which will override
171+
/// persisted user settings to have a clean environment.
172+
+ (void)registerDefaults
170173
{
171-
static BOOL initDone = NO;
172-
if (initDone) return;
173-
initDone = YES;
174-
175-
ASLInit();
176-
177-
// HACK! The following user default must be reset, else Ctrl-q (or
178-
// whichever key is specified by the default) will be blocked by the input
179-
// manager (interpretKeyEvents: swallows that key). (We can't use
180-
// NSUserDefaults since it only allows us to write to the registration
181-
// domain and this preference has "higher precedence" than that so such a
182-
// change would have no effect.)
183-
CFPreferencesSetAppValue(CFSTR("NSQuotedKeystrokeBinding"),
184-
CFSTR(""),
185-
kCFPreferencesCurrentApplication);
186-
187-
// Also disable NSRepeatCountBinding -- it is not enabled by default, but
188-
// it does not make much sense to support it since Vim has its own way of
189-
// dealing with repeat counts.
190-
CFPreferencesSetAppValue(CFSTR("NSRepeatCountBinding"),
191-
CFSTR(""),
192-
kCFPreferencesCurrentApplication);
193-
194-
if ([NSWindow respondsToSelector:@selector(setAllowsAutomaticWindowTabbing:)]) {
195-
// Disable automatic tabbing on 10.12+. MacVim already has its own
196-
// tabbing interface, so we don't want multiple hierarchy of tabs mixing
197-
// native and Vim tabs. MacVim also doesn't work well with native tabs
198-
// right now since it doesn't respond well to the size change, and it
199-
// doesn't show the native menu items (e.g. move tab to new window) in
200-
// all the tabs.
201-
//
202-
// Note: MacVim cannot use macOS native tabs for Vim tabs because Vim
203-
// assumes only one tab can be shown at a time, and it would be hard to
204-
// handle native tab's "move tab to a new window" functionality.
205-
[NSWindow setAllowsAutomaticWindowTabbing:NO];
206-
}
207-
208174
int tabMinWidthKey;
209175
int tabMaxWidthKey;
210176
int tabOptimumWidthKey;
@@ -218,7 +184,9 @@ + (void)initialize
218184
tabOptimumWidthKey = 132;
219185
}
220186

221-
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
187+
NSUserDefaults *ud = NSUserDefaults.standardUserDefaults;
188+
189+
NSDictionary *macvimDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
222190
[NSNumber numberWithBool:NO], MMNoWindowKey,
223191
[NSNumber numberWithInt:tabMinWidthKey],
224192
MMTabMinWidthKey,
@@ -244,6 +212,10 @@ + (void)initialize
244212
[NSNumber numberWithInt:MMUntitledWindowAlways],
245213
MMUntitledWindowKey,
246214
[NSNumber numberWithBool:NO], MMNoWindowShadowKey,
215+
[NSNumber numberWithBool:NO], MMDisableLaunchAnimationKey,
216+
[NSNumber numberWithInt:0], MMAppearanceModeSelectionKey,
217+
[NSNumber numberWithBool:NO], MMNoTitleBarWindowKey,
218+
[NSNumber numberWithBool:NO], MMTitlebarAppearsTransparentKey,
247219
[NSNumber numberWithBool:NO], MMZoomBothKey,
248220
@"", MMLoginShellCommandKey,
249221
@"", MMLoginShellArgumentKey,
@@ -271,7 +243,58 @@ + (void)initialize
271243
[NSNumber numberWithBool:0], MMScrollOneDirectionOnlyKey,
272244
nil];
273245

274-
[[NSUserDefaults standardUserDefaults] registerDefaults:dict];
246+
[ud registerDefaults:macvimDefaults];
247+
248+
NSArray<NSString *> *arguments = NSProcessInfo.processInfo.arguments;
249+
if ([arguments containsObject:@"-IgnoreUserDefaults"]) {
250+
NSDictionary<NSString *, id> *argDefaults = [ud volatileDomainForName:NSArgumentDomain];
251+
NSMutableDictionary<NSString *, id> *combinedDefaults = [NSMutableDictionary dictionaryWithCapacity: macvimDefaults.count];
252+
[combinedDefaults setDictionary:macvimDefaults];
253+
[combinedDefaults addEntriesFromDictionary:argDefaults];
254+
[ud setVolatileDomain:combinedDefaults forName:NSArgumentDomain];
255+
}
256+
}
257+
258+
+ (void)initialize
259+
{
260+
static BOOL initDone = NO;
261+
if (initDone) return;
262+
initDone = YES;
263+
264+
ASLInit();
265+
266+
// HACK! The following user default must be reset, else Ctrl-q (or
267+
// whichever key is specified by the default) will be blocked by the input
268+
// manager (interpreargumenttKeyEvents: swallows that key). (We can't use
269+
// NSUserDefaults since it only allows us to write to the registration
270+
// domain and this preference has "higher precedence" than that so such a
271+
// change would have no effect.)
272+
CFPreferencesSetAppValue(CFSTR("NSQuotedKeystrokeBinding"),
273+
CFSTR(""),
274+
kCFPreferencesCurrentApplication);
275+
276+
// Also disable NSRepeatCountBinding -- it is not enabled by default, but
277+
// it does not make much sense to support it since Vim has its own way of
278+
// dealing with repeat counts.
279+
CFPreferencesSetAppValue(CFSTR("NSRepeatCountBinding"),
280+
CFSTR(""),
281+
kCFPreferencesCurrentApplication);
282+
283+
if ([NSWindow respondsToSelector:@selector(setAllowsAutomaticWindowTabbing:)]) {
284+
// Disable automatic tabbing on 10.12+. MacVim already has its own
285+
// tabbing interface, so we don't want multiple hierarchy of tabs mixing
286+
// native and Vim tabs. MacVim also doesn't work well with native tabs
287+
// right now since it doesn't respond well to the size change, and it
288+
// doesn't show the native menu items (e.g. move tab to new window) in
289+
// all the tabs.
290+
//
291+
// Note: MacVim cannot use macOS native tabs for Vim tabs because Vim
292+
// assumes only one tab can be shown at a time, and it would be hard to
293+
// handle native tab's "move tab to a new window" functionality.
294+
[NSWindow setAllowsAutomaticWindowTabbing:NO];
295+
}
296+
297+
[MMAppController registerDefaults];
275298

276299
NSArray *types = [NSArray arrayWithObject:NSPasteboardTypeString];
277300
[NSApp registerServicesMenuSendTypes:types returnTypes:types];
@@ -1296,25 +1319,60 @@ - (BOOL)validateMenuItem:(NSMenuItem *)item
12961319
return YES;
12971320
}
12981321

1299-
- (IBAction)newWindow:(id)sender
1322+
/// Open a new Vim window, potentially taking from cached (if preload is used).
1323+
///
1324+
/// @param mode Determine whether to use clean mode or not. Preload will only
1325+
/// be used if using normal mode.
1326+
///
1327+
/// @param activate Activate the window after it's opened.
1328+
- (void)openNewWindow:(enum NewWindowMode)mode activate:(BOOL)activate
13001329
{
1301-
ASLogDebug(@"Open new window");
1330+
if (activate)
1331+
[self activateWhenNextWindowOpens];
13021332

13031333
// A cached controller requires no loading times and results in the new
13041334
// window popping up instantaneously. If the cache is empty it may take
13051335
// 1-2 seconds to start a new Vim process.
1306-
MMVimController *vc = [self takeVimControllerFromCache];
1336+
MMVimController *vc = (mode == NewWindowNormal) ? [self takeVimControllerFromCache] : nil;
13071337
if (vc) {
13081338
[[vc backendProxy] acknowledgeConnection];
13091339
} else {
1310-
[self launchVimProcessWithArguments:nil workingDirectory:nil];
1340+
NSArray *args = (mode == NewWindowNormal) ? nil
1341+
: (mode == NewWindowClean ? @[@"--clean"]
1342+
: @[@"--clean", @"-u", @"NONE"]);
1343+
[self launchVimProcessWithArguments:args workingDirectory:nil];
13111344
}
13121345
}
13131346

1347+
- (IBAction)newWindow:(id)sender
1348+
{
1349+
ASLogDebug(@"Open new window");
1350+
[self openNewWindow:NewWindowNormal activate:NO];
1351+
}
1352+
1353+
- (IBAction)newWindowClean:(id)sender
1354+
{
1355+
[self openNewWindow:NewWindowClean activate:NO];
1356+
}
1357+
1358+
- (IBAction)newWindowCleanNoDefaults:(id)sender
1359+
{
1360+
[self openNewWindow:NewWindowCleanNoDefaults activate:NO];
1361+
}
1362+
13141363
- (IBAction)newWindowAndActivate:(id)sender
13151364
{
1316-
[self activateWhenNextWindowOpens];
1317-
[self newWindow:sender];
1365+
[self openNewWindow:NewWindowNormal activate:YES];
1366+
}
1367+
1368+
- (IBAction)newWindowCleanAndActivate:(id)sender
1369+
{
1370+
[self openNewWindow:NewWindowClean activate:YES];
1371+
}
1372+
1373+
- (IBAction)newWindowCleanNoDefaultsAndActivate:(id)sender
1374+
{
1375+
[self openNewWindow:NewWindowCleanNoDefaults activate:YES];
13181376
}
13191377

13201378
- (IBAction)fileOpen:(id)sender

src/MacVim/MMWindowController.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ - (id)initWithVimController:(MMVimController *)controller
232232
// This makes windows animate when opened
233233
if ([win respondsToSelector:@selector(setAnimationBehavior:)]) {
234234
if (![[NSUserDefaults standardUserDefaults]
235-
boolForKey:MMDisableLaunchAnimation]) {
235+
boolForKey:MMDisableLaunchAnimationKey]) {
236236
[win setAnimationBehavior:NSWindowAnimationBehaviorDocumentWindow];
237237
}
238238
}

src/MacVim/Miscellaneous.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ extern NSString *MMAppearanceModeSelectionKey;
3838
extern NSString *MMNoTitleBarWindowKey;
3939
extern NSString *MMTitlebarAppearsTransparentKey;
4040
extern NSString *MMNoWindowShadowKey;
41-
extern NSString *MMDisableLaunchAnimation;
41+
extern NSString *MMDisableLaunchAnimationKey;
4242
extern NSString *MMLoginShellKey;
4343
extern NSString *MMUntitledWindowKey;
4444
extern NSString *MMZoomBothKey;

src/MacVim/Miscellaneous.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
NSString *MMNoTitleBarWindowKey = @"MMNoTitleBarWindow";
3535
NSString *MMTitlebarAppearsTransparentKey = @"MMTitlebarAppearsTransparent";
3636
NSString *MMNoWindowShadowKey = @"MMNoWindowShadow";
37-
NSString *MMDisableLaunchAnimation = @"MMDisableLaunchAnimation";
37+
NSString *MMDisableLaunchAnimationKey = @"MMDisableLaunchAnimation";
3838
NSString *MMLoginShellKey = @"MMLoginShell";
3939
NSString *MMUntitledWindowKey = @"MMUntitledWindow";
4040
NSString *MMZoomBothKey = @"MMZoomBoth";

0 commit comments

Comments
 (0)