Skip to content

Commit 1ca0a97

Browse files
authored
Merge pull request #1544 from ychin/guifont-monospace-fontsizeupdown-keep-alias
Fix fontSizeUp/Down actions to preserve -monospace- font alias
2 parents 8ac49d6 + dbdc27d commit 1ca0a97

File tree

6 files changed

+28
-1
lines changed

6 files changed

+28
-1
lines changed

src/MacVim/MMCoreTextView.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,15 @@ - (void)changeFont:(id)sender
13331333

13341334
if (newFont) {
13351335
NSString *name = [newFont fontName];
1336+
1337+
// If this is a system monospace font, retrieve the user-friendly
1338+
// name before we send it to Vim. We don't want to expose the OS-
1339+
// specific system font name which is confusing to the user.
1340+
NSString *userMonospaceFontName = [[self vimController] systemFontNamesToAlias][name];
1341+
if (userMonospaceFontName != nil) {
1342+
name = userMonospaceFontName;
1343+
}
1344+
13361345
unsigned len = (unsigned)[name lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
13371346
if (len > 0) {
13381347
NSMutableData *data = [NSMutableData data];

src/MacVim/MMVimController.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@
5656
BOOL hasModifiedBuffer;
5757
}
5858

59+
/// Mapping from internal names for system monospace font to user-visible one.
60+
/// E.g. ".AppleSystemUIFontMonospaced-Medium" -> "-monospace-Medium"
61+
@property (nonatomic, readonly) NSMutableDictionary<NSString*, NSString*>* systemFontNamesToAlias;
62+
5963
- (id)initWithBackend:(id)backend pid:(int)processIdentifier;
6064
- (void)uninitialize;
6165
- (unsigned long)vimControllerId;

src/MacVim/MMVimController.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ - (void)dealloc
250250
[mainMenu release]; mainMenu = nil;
251251
[creationDate release]; creationDate = nil;
252252

253+
[_systemFontNamesToAlias release]; _systemFontNamesToAlias = nil;
254+
253255
[super dealloc];
254256
}
255257

@@ -927,6 +929,14 @@ - (void)handleMessage:(int)msgid data:(NSData *)data
927929
fontWeight = NSFontWeightBlack;
928930
}
929931
font = [NSFont monospacedSystemFontOfSize:size weight:fontWeight];
932+
933+
// We cache the internal name -> user-facing alias mapping
934+
// to allow fontSizeUp/Down actions to be able to retain
935+
// the user-facing font name in 'guifont'.
936+
if (_systemFontNamesToAlias == nil) {
937+
_systemFontNamesToAlias = [[NSMutableDictionary alloc] initWithCapacity:9];
938+
}
939+
_systemFontNamesToAlias[font.fontName] = name;
930940
}
931941
else
932942
#endif

src/MacVim/MMWindowController.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,12 +1239,14 @@ - (IBAction)vimTouchbarItemAction:(id)sender
12391239

12401240
- (IBAction)fontSizeUp:(id)sender
12411241
{
1242+
// This creates a new font and triggers text view's changeFont: callback
12421243
[[NSFontManager sharedFontManager] modifyFont:
12431244
[NSNumber numberWithInt:NSSizeUpFontAction]];
12441245
}
12451246

12461247
- (IBAction)fontSizeDown:(id)sender
12471248
{
1249+
// This creates a new font and triggers text view's changeFont: callback
12481250
[[NSFontManager sharedFontManager] modifyFont:
12491251
[NSNumber numberWithInt:NSSizeDownFontAction]];
12501252
}

src/MacVim/MacVim.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ enum {
446446

447447
extern NSString *VimFindPboardType;
448448

449-
// Alias for system monospace font name
449+
/// Alias for system monospace font name
450450
extern NSString *MMSystemFontAlias;
451451

452452

src/MacVim/MacVimTests/MacVimTests.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,10 +536,12 @@ - (void) testGuifontSystemMonospace {
536536
[[[app keyVimController] windowController] fontSizeUp:nil];
537537
[self waitForEventHandlingAndVimProcess];
538538
XCTAssertEqualObjects([textView font], [NSFont monospacedSystemFontOfSize:13 weight:NSFontWeightHeavy]);
539+
XCTAssertEqualObjects([[app keyVimController] evaluateVimExpression:@"&guifont"], @"-monospace-Heavy:h13");
539540

540541
[[[app keyVimController] windowController] fontSizeDown:nil];
541542
[self waitForEventHandlingAndVimProcess];
542543
XCTAssertEqualObjects([textView font], [NSFont monospacedSystemFontOfSize:12 weight:NSFontWeightHeavy]);
544+
XCTAssertEqualObjects([[app keyVimController] evaluateVimExpression:@"&guifont"], @"-monospace-Heavy:h12");
543545
}
544546

545547
/// Test that dark mode settings work and the corresponding Vim bindings are functional.

0 commit comments

Comments
 (0)