Skip to content

Commit 772df7a

Browse files
committed
Add cmdline completion for MacVim options: 'fuoptions', 'guifont(wide)'
1 parent 42f62db commit 772df7a

File tree

8 files changed

+122
-6
lines changed

8 files changed

+122
-6
lines changed

runtime/doc/options.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3873,7 +3873,9 @@ A jump table for the options with a short description can be found at |Q_op|.
38733873
In non-native fullscreen mode, MacVim can be configured to either show
38743874
all the content filling up the whole screen, or only use part of the
38753875
screen to show the content, centered. This option controls the size
3876-
of the Vim control as well as the color of the unused screen area.
3876+
of the Vim control as well as the color of the unused screen area. It
3877+
is a comma-separated list of values as follows:
3878+
38773879
value effect ~
38783880
maxvert When entering fullscreen, 'lines' is set to the maximum number
38793881
of lines fitting on the screen in fullscreen mode. If unset,

src/MacVim/gui_macvim.m

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@
3030
// Enabled when files passed on command line should not be added to MRU.
3131
static BOOL MMNoMRU = NO;
3232

33-
static NSString *MMDefaultFontName = @"Menlo Regular";
33+
static NSString *MMDefaultFontName = @"Menlo-Regular";
3434
static int MMDefaultFontSize = 11;
35+
static char *MMDefaultFontStr = "Menlo-Regular:h11";
36+
static char *MMDefaultFontSizeStr = "h11";
3537
static int MMMinFontSize = 6;
3638
static int MMMaxFontSize = 100;
3739

@@ -1153,6 +1155,48 @@
11531155
return NOFONT;
11541156
}
11551157

1158+
/**
1159+
* Cmdline expansion for setting 'guifont' / 'guifontwide'. Will enumerate
1160+
* through all fonts for completion. When setting 'guifont' it will only show
1161+
* monospace fonts as it's unlikely other fonts would be useful.
1162+
*/
1163+
void
1164+
gui_mch_expand_font(optexpand_T *args, void *param, int (*add_match)(char_u *val))
1165+
{
1166+
expand_T *xp = args->oe_xp;
1167+
int wide = *(int *)param;
1168+
1169+
if (args->oe_include_orig_val && *args->oe_opt_value == NUL && !wide)
1170+
{
1171+
// If guifont is empty, and we want to fill in the orig value, suggest
1172+
// the default so the user can modify it.
1173+
if (add_match((char_u *)MMDefaultFontStr) != OK)
1174+
return;
1175+
}
1176+
1177+
if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
1178+
{
1179+
// Fill in the existing font size to help switching only font family
1180+
char_u *colon = vim_strchr(p_guifont, ':');
1181+
if (colon != NULL)
1182+
add_match(colon + 1);
1183+
else
1184+
add_match((char_u*)MMDefaultFontSizeStr);
1185+
return;
1186+
}
1187+
1188+
NSFontManager *fontManager = [NSFontManager sharedFontManager];
1189+
NSArray<NSString *> *availableFonts;
1190+
if (wide)
1191+
availableFonts = [fontManager availableFonts];
1192+
else
1193+
availableFonts = [fontManager availableFontNamesWithTraits:NSFixedPitchFontMask];
1194+
for (NSString *font in availableFonts) {
1195+
if (add_match((char_u*)[font UTF8String]) != OK)
1196+
return;
1197+
}
1198+
}
1199+
11561200
// -- Scrollbars ------------------------------------------------------------
11571201

11581202
// NOTE: Even though scrollbar identifiers are 'long' we tacitly assume that

src/optiondefs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,9 +1175,9 @@ static struct vimoption options[] =
11751175
{(char_u *)NULL, (char_u *)0L}
11761176
#endif
11771177
SCTX_INIT},
1178-
{"fuoptions", "fuopt", P_STRING|P_COMMA|P_NODUP|P_VI_DEF,
1178+
{"fuoptions", "fuopt", P_STRING|P_COMMA|P_NODUP|P_VI_DEF|P_COLON,
11791179
#ifdef FEAT_FULLSCREEN
1180-
(char_u *)&p_fuoptions, PV_NONE, did_set_fuoptions, NULL,
1180+
(char_u *)&p_fuoptions, PV_NONE, did_set_fuoptions, expand_set_fuoptions,
11811181
{(char_u *)"maxvert,maxhorz", (char_u *)0L}
11821182
#else
11831183
(char_u *)NULL, PV_NONE, NULL, NULL,

src/optionstr.c

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ expand_set_opt_generic(
849849
return ret;
850850
}
851851

852-
# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
852+
# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MACVIM)
853853
static garray_T *expand_cb_ga;
854854
static optexpand_T *expand_cb_args;
855855

@@ -2351,7 +2351,7 @@ expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
23512351
if (!gui.in_use)
23522352
return FAIL;
23532353

2354-
# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2354+
# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MACVIM)
23552355
char_u **varp = (char_u **)args->oe_varp;
23562356
int wide = (varp == &p_guifontwide);
23572357

@@ -4259,6 +4259,45 @@ did_set_fuoptions(optset_T *args UNUSED)
42594259
return e_invalid_argument;
42604260
return NULL;
42614261
}
4262+
4263+
static char_u *
4264+
get_fuopt_background_value(expand_T *xp, int idx)
4265+
{
4266+
// "background:" supports '#' for expicit RGB color, or highlight names.
4267+
if (idx == 0)
4268+
return (char_u *)"#";
4269+
return get_highlight_name(xp, idx - 1);
4270+
}
4271+
4272+
int
4273+
expand_set_fuoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4274+
{
4275+
expand_T *xp = args->oe_xp;
4276+
4277+
if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
4278+
{
4279+
// Within "background:", we have a subgroup of possible options.
4280+
int bg_len = STRLEN("background:");
4281+
if (xp->xp_pattern - args->oe_set_arg >= bg_len &&
4282+
STRNCMP(xp->xp_pattern - bg_len, "background:", bg_len) == 0)
4283+
{
4284+
return expand_set_opt_generic(
4285+
args,
4286+
get_fuopt_background_value,
4287+
numMatches,
4288+
matches);
4289+
}
4290+
return FAIL;
4291+
}
4292+
4293+
static char *(p_fuopt_values[]) = {"maxvert", "maxhorz", "background:", NULL};
4294+
return expand_set_opt_string(
4295+
args,
4296+
p_fuopt_values,
4297+
ARRAY_LENGTH(p_fuopt_values) - 1,
4298+
numMatches,
4299+
matches);
4300+
}
42624301
#endif
42634302

42644303
#pragma endregion MacVim specific options

src/proto/gui_macvim.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ GuiFont gui_mch_get_font(char_u *name, int giveErrorIfMissing);
4040
char_u *gui_mch_get_fontname(GuiFont font, char_u *name);
4141
int gui_mch_init_font(char_u *font_name, int fontset);
4242
void gui_mch_set_font(GuiFont font);
43+
void gui_mch_expand_font(optexpand_T *args, void *param, int (*add_match)(char_u *val));
4344
int gui_mch_adjust_charheight(void);
4445
int gui_mch_adjust_charwidth(void);
4546
void gui_mch_beep(void);

src/proto/optionstr.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ int expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches);
156156
int expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches);
157157
int expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches);
158158
int expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches);
159+
int expand_set_fuoptions(optexpand_T *args, int *numMatches, char_u ***matches);
159160
int expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches);
160161
int expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches);
161162
int expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches);

src/testdir/test_gui.vim

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,28 @@ func Test_expand_guifont()
623623
call assert_equal([], getcompletion('set guifont=Sans$', 'cmdline'))
624624
call assert_equal(['Sans'], getcompletion('set guifontwide=Sans$', 'cmdline'))
625625

626+
let &guifontwide = guifontwide_saved
627+
let &guifont = guifont_saved
628+
elseif has('gui_macvim')
629+
let guifont_saved = &guifont
630+
let guifontwide_saved = &guifontwide
631+
632+
" Test recalling default and existing option, and suggesting current font
633+
" size
634+
set guifont=
635+
call assert_equal('Menlo-Regular:h11', getcompletion('set guifont=', 'cmdline')[0])
636+
set guifont=Monaco:h12
637+
call assert_equal('Monaco:h12', getcompletion('set guifont=', 'cmdline')[0])
638+
call assert_equal('h12', getcompletion('set guifont=Menlo\ Italic:', 'cmdline')[0])
639+
640+
" Test auto-completion working for font names
641+
call assert_equal(['Menlo-Regular'], getcompletion('set guifont=Menl*lar$', 'cmdline'))
642+
call assert_equal(['Menlo-Regular'], getcompletion('set guifontwide=Menl*lar$', 'cmdline'))
643+
644+
" Make sure non-monospace fonts are filtered out only in 'guifont'
645+
call assert_equal([], getcompletion('set guifont=Hel*tica$', 'cmdline'))
646+
call assert_equal(['Helvetica'], getcompletion('set guifontwide=Hel*tica$', 'cmdline'))
647+
626648
let &guifontwide = guifontwide_saved
627649
let &guifont = guifont_saved
628650
else

src/testdir/test_options.vim

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,13 @@ func Test_set_completion_string_values()
568568
\ '')
569569
call assert_equal([], getcompletion('set hl+=8'..hl_display_modes, 'cmdline'))
570570

571+
"
572+
" MacVim options
573+
"
574+
call assert_equal(getcompletion('set fuoptions+=', 'cmdline')[0], 'maxvert')
575+
call assert_equal(getcompletion('set fuoptions+=maxvert,background:', 'cmdline')[0], '#')
576+
call assert_equal(getcompletion('set fuoptions+=maxvert,background:No*ext', 'cmdline')[0], 'NonText')
577+
571578
"
572579
" Test flag lists
573580
"

0 commit comments

Comments
 (0)