diff --git a/src/MacVim/MMCoreTextView.m b/src/MacVim/MMCoreTextView.m index 44794a665c..5b91b1ad51 100644 --- a/src/MacVim/MMCoreTextView.m +++ b/src/MacVim/MMCoreTextView.m @@ -1333,6 +1333,15 @@ - (void)changeFont:(id)sender if (newFont) { NSString *name = [newFont fontName]; + + // If this is a system monospace font, retrieve the user-friendly + // name before we send it to Vim. We don't want to expose the OS- + // specific system font name which is confusing to the user. + NSString *userMonospaceFontName = [[self vimController] systemFontNamesToAlias][name]; + if (userMonospaceFontName != nil) { + name = userMonospaceFontName; + } + unsigned len = (unsigned)[name lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; if (len > 0) { NSMutableData *data = [NSMutableData data]; diff --git a/src/MacVim/MMVimController.h b/src/MacVim/MMVimController.h index 4b16e08253..fc35589118 100644 --- a/src/MacVim/MMVimController.h +++ b/src/MacVim/MMVimController.h @@ -56,6 +56,10 @@ BOOL hasModifiedBuffer; } +/// Mapping from internal names for system monospace font to user-visible one. +/// E.g. ".AppleSystemUIFontMonospaced-Medium" -> "-monospace-Medium" +@property (nonatomic, readonly) NSMutableDictionary* systemFontNamesToAlias; + - (id)initWithBackend:(id)backend pid:(int)processIdentifier; - (void)uninitialize; - (unsigned long)vimControllerId; diff --git a/src/MacVim/MMVimController.m b/src/MacVim/MMVimController.m index 12fa382f12..2137dedf43 100644 --- a/src/MacVim/MMVimController.m +++ b/src/MacVim/MMVimController.m @@ -250,6 +250,8 @@ - (void)dealloc [mainMenu release]; mainMenu = nil; [creationDate release]; creationDate = nil; + [_systemFontNamesToAlias release]; _systemFontNamesToAlias = nil; + [super dealloc]; } @@ -927,6 +929,14 @@ - (void)handleMessage:(int)msgid data:(NSData *)data fontWeight = NSFontWeightBlack; } font = [NSFont monospacedSystemFontOfSize:size weight:fontWeight]; + + // We cache the internal name -> user-facing alias mapping + // to allow fontSizeUp/Down actions to be able to retain + // the user-facing font name in 'guifont'. + if (_systemFontNamesToAlias == nil) { + _systemFontNamesToAlias = [[NSMutableDictionary alloc] initWithCapacity:9]; + } + _systemFontNamesToAlias[font.fontName] = name; } else #endif diff --git a/src/MacVim/MMWindowController.m b/src/MacVim/MMWindowController.m index a583aa395f..727e35e7d4 100644 --- a/src/MacVim/MMWindowController.m +++ b/src/MacVim/MMWindowController.m @@ -1239,12 +1239,14 @@ - (IBAction)vimTouchbarItemAction:(id)sender - (IBAction)fontSizeUp:(id)sender { + // This creates a new font and triggers text view's changeFont: callback [[NSFontManager sharedFontManager] modifyFont: [NSNumber numberWithInt:NSSizeUpFontAction]]; } - (IBAction)fontSizeDown:(id)sender { + // This creates a new font and triggers text view's changeFont: callback [[NSFontManager sharedFontManager] modifyFont: [NSNumber numberWithInt:NSSizeDownFontAction]]; } diff --git a/src/MacVim/MacVim.h b/src/MacVim/MacVim.h index aa12b8c575..6bcb61057d 100644 --- a/src/MacVim/MacVim.h +++ b/src/MacVim/MacVim.h @@ -446,7 +446,7 @@ enum { extern NSString *VimFindPboardType; -// Alias for system monospace font name +/// Alias for system monospace font name extern NSString *MMSystemFontAlias; diff --git a/src/MacVim/MacVimTests/MacVimTests.m b/src/MacVim/MacVimTests/MacVimTests.m index e90ad3bd91..4e632ad5e9 100644 --- a/src/MacVim/MacVimTests/MacVimTests.m +++ b/src/MacVim/MacVimTests/MacVimTests.m @@ -536,10 +536,12 @@ - (void) testGuifontSystemMonospace { [[[app keyVimController] windowController] fontSizeUp:nil]; [self waitForEventHandlingAndVimProcess]; XCTAssertEqualObjects([textView font], [NSFont monospacedSystemFontOfSize:13 weight:NSFontWeightHeavy]); + XCTAssertEqualObjects([[app keyVimController] evaluateVimExpression:@"&guifont"], @"-monospace-Heavy:h13"); [[[app keyVimController] windowController] fontSizeDown:nil]; [self waitForEventHandlingAndVimProcess]; XCTAssertEqualObjects([textView font], [NSFont monospacedSystemFontOfSize:12 weight:NSFontWeightHeavy]); + XCTAssertEqualObjects([[app keyVimController] evaluateVimExpression:@"&guifont"], @"-monospace-Heavy:h12"); } /// Test that dark mode settings work and the corresponding Vim bindings are functional.