Skip to content

Commit c1e6d6b

Browse files
committed
Allow underscores in guifont PostScript names
MacVim guifont's can be specified by either full PostScript names or family names. Since family names can contain spaces, MacVim allows using underscores to serve as substitute to make it easier for the user to type, and this is similar to how Win32 gVim works. However, some fonts use underscores in their full PostScript name. For example, Cascadia Code has names like "CascadiaCode-Regular_Light". The previous substitution code always replaced underscores with spaces which was wrong. Fix it to only do the substitution if the font could not be loaded. Also, fix documentation to be clearer in what MacVim expects as guifont's input. In retrospect, implicit substitution like this was probably a bad idea when the user could just escape with spaces, but given this has been the behavior for a long time we should just keep it to avoid breaking backwards compatibility. Fix #1208
1 parent 37bfc1f commit c1e6d6b

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

runtime/doc/gui.txt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,10 +1135,18 @@ That's all. XLFDs are not used. For Chinese this is reported to work well: >
11351135

11361136
MacVim *macvim-guifont*
11371137

1138-
For MacVim you can use something like this: >
1139-
:set guifont=Menlo:h10
1140-
Fonts with spaces are set like this: >
1138+
In MacVim, you can specify fonts by either the full PostScript name or the
1139+
family name (both can be found in the Font Book app), followed by an optional
1140+
font size (which defaults to `h11`). Font names can also be completed in the
1141+
cmdline using |complete-set-option|.
1142+
1143+
Basic example of setting the font to Menlo with default size: >
1144+
:set guifont=Menlo
1145+
To set a font by its family name which contains spaces: >
11411146
:set guifont=DejaVu\ Sans\ Mono:h13
1147+
When specifying family names with spaces, you can also use underscores
1148+
instead: >
1149+
:set guifont=DejaVu_Sans_Mono:h13
11421150
To use bold/italic fonts, use the fully specified PostScript name of the
11431151
font, like so: >
11441152
:set guifont=Menlo-Bold:h13

src/MacVim/gui_macvim.m

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,12 +1196,6 @@
11961196
parseFailed = YES;
11971197
}
11981198

1199-
if (!parseFailed) {
1200-
// Replace underscores with spaces.
1201-
fontName = [[fontName componentsSeparatedByString:@"_"]
1202-
componentsJoinedByString:@" "];
1203-
}
1204-
12051199
const BOOL isSystemFont = [fontName hasPrefix:MMSystemFontAlias];
12061200
if (isSystemFont) {
12071201
if (fontName.length > MMSystemFontAlias.length) {
@@ -1228,6 +1222,16 @@
12281222
|| isSystemFont
12291223
|| [NSFont fontWithName:fontName size:size])
12301224
return [[NSString alloc] initWithFormat:@"%@:h%d", fontName, size];
1225+
1226+
// If font loading failed, try to replace underscores with spaces for
1227+
// user convenience. This really only works if the name is a family
1228+
// name because PostScript names should not have spaces.
1229+
if ([fontName rangeOfString:@"_"].location != NSNotFound) {
1230+
fontName = [fontName stringByReplacingOccurrencesOfString:@"_" withString:@" "];
1231+
if ([NSFont fontWithName:fontName size:size]) {
1232+
return [[NSString alloc] initWithFormat:@"%@:h%d", fontName, size];
1233+
}
1234+
}
12311235
}
12321236

12331237
return NOFONT;

0 commit comments

Comments
 (0)