Skip to content

Commit

Permalink
Add cmdline completion for MacVim options: 'fuoptions', 'guifont(wide)'
Browse files Browse the repository at this point in the history
  • Loading branch information
ychin committed Oct 5, 2023
1 parent 42f62db commit 772df7a
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 6 deletions.
4 changes: 3 additions & 1 deletion runtime/doc/options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3873,7 +3873,9 @@ A jump table for the options with a short description can be found at |Q_op|.
In non-native fullscreen mode, MacVim can be configured to either show
all the content filling up the whole screen, or only use part of the
screen to show the content, centered. This option controls the size
of the Vim control as well as the color of the unused screen area.
of the Vim control as well as the color of the unused screen area. It
is a comma-separated list of values as follows:

value effect ~
maxvert When entering fullscreen, 'lines' is set to the maximum number
of lines fitting on the screen in fullscreen mode. If unset,
Expand Down
46 changes: 45 additions & 1 deletion src/MacVim/gui_macvim.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
// Enabled when files passed on command line should not be added to MRU.
static BOOL MMNoMRU = NO;

static NSString *MMDefaultFontName = @"Menlo Regular";
static NSString *MMDefaultFontName = @"Menlo-Regular";
static int MMDefaultFontSize = 11;
static char *MMDefaultFontStr = "Menlo-Regular:h11";
static char *MMDefaultFontSizeStr = "h11";
static int MMMinFontSize = 6;
static int MMMaxFontSize = 100;

Expand Down Expand Up @@ -1153,6 +1155,48 @@
return NOFONT;
}

/**
* Cmdline expansion for setting 'guifont' / 'guifontwide'. Will enumerate
* through all fonts for completion. When setting 'guifont' it will only show
* monospace fonts as it's unlikely other fonts would be useful.
*/
void
gui_mch_expand_font(optexpand_T *args, void *param, int (*add_match)(char_u *val))
{
expand_T *xp = args->oe_xp;
int wide = *(int *)param;

if (args->oe_include_orig_val && *args->oe_opt_value == NUL && !wide)
{
// If guifont is empty, and we want to fill in the orig value, suggest
// the default so the user can modify it.
if (add_match((char_u *)MMDefaultFontStr) != OK)
return;
}

if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
{
// Fill in the existing font size to help switching only font family
char_u *colon = vim_strchr(p_guifont, ':');
if (colon != NULL)
add_match(colon + 1);
else
add_match((char_u*)MMDefaultFontSizeStr);
return;
}

NSFontManager *fontManager = [NSFontManager sharedFontManager];
NSArray<NSString *> *availableFonts;
if (wide)
availableFonts = [fontManager availableFonts];
else
availableFonts = [fontManager availableFontNamesWithTraits:NSFixedPitchFontMask];
for (NSString *font in availableFonts) {
if (add_match((char_u*)[font UTF8String]) != OK)
return;
}
}

// -- Scrollbars ------------------------------------------------------------

// NOTE: Even though scrollbar identifiers are 'long' we tacitly assume that
Expand Down
4 changes: 2 additions & 2 deletions src/optiondefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1175,9 +1175,9 @@ static struct vimoption options[] =
{(char_u *)NULL, (char_u *)0L}
#endif
SCTX_INIT},
{"fuoptions", "fuopt", P_STRING|P_COMMA|P_NODUP|P_VI_DEF,
{"fuoptions", "fuopt", P_STRING|P_COMMA|P_NODUP|P_VI_DEF|P_COLON,
#ifdef FEAT_FULLSCREEN
(char_u *)&p_fuoptions, PV_NONE, did_set_fuoptions, NULL,
(char_u *)&p_fuoptions, PV_NONE, did_set_fuoptions, expand_set_fuoptions,
{(char_u *)"maxvert,maxhorz", (char_u *)0L}
#else
(char_u *)NULL, PV_NONE, NULL, NULL,
Expand Down
43 changes: 41 additions & 2 deletions src/optionstr.c
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ expand_set_opt_generic(
return ret;
}

# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MACVIM)
static garray_T *expand_cb_ga;
static optexpand_T *expand_cb_args;

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

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

Expand Down Expand Up @@ -4259,6 +4259,45 @@ did_set_fuoptions(optset_T *args UNUSED)
return e_invalid_argument;
return NULL;
}

static char_u *
get_fuopt_background_value(expand_T *xp, int idx)
{
// "background:" supports '#' for expicit RGB color, or highlight names.
if (idx == 0)
return (char_u *)"#";
return get_highlight_name(xp, idx - 1);
}

int
expand_set_fuoptions(optexpand_T *args, int *numMatches, char_u ***matches)
{
expand_T *xp = args->oe_xp;

if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
{
// Within "background:", we have a subgroup of possible options.
int bg_len = STRLEN("background:");
if (xp->xp_pattern - args->oe_set_arg >= bg_len &&
STRNCMP(xp->xp_pattern - bg_len, "background:", bg_len) == 0)
{
return expand_set_opt_generic(
args,
get_fuopt_background_value,
numMatches,
matches);
}
return FAIL;
}

static char *(p_fuopt_values[]) = {"maxvert", "maxhorz", "background:", NULL};
return expand_set_opt_string(
args,
p_fuopt_values,
ARRAY_LENGTH(p_fuopt_values) - 1,
numMatches,
matches);
}
#endif

#pragma endregion MacVim specific options
Expand Down
1 change: 1 addition & 0 deletions src/proto/gui_macvim.pro
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ GuiFont gui_mch_get_font(char_u *name, int giveErrorIfMissing);
char_u *gui_mch_get_fontname(GuiFont font, char_u *name);
int gui_mch_init_font(char_u *font_name, int fontset);
void gui_mch_set_font(GuiFont font);
void gui_mch_expand_font(optexpand_T *args, void *param, int (*add_match)(char_u *val));
int gui_mch_adjust_charheight(void);
int gui_mch_adjust_charwidth(void);
void gui_mch_beep(void);
Expand Down
1 change: 1 addition & 0 deletions src/proto/optionstr.pro
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ int expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches);
int expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches);
int expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches);
int expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches);
int expand_set_fuoptions(optexpand_T *args, int *numMatches, char_u ***matches);
int expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches);
int expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches);
int expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches);
Expand Down
22 changes: 22 additions & 0 deletions src/testdir/test_gui.vim
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,28 @@ func Test_expand_guifont()
call assert_equal([], getcompletion('set guifont=Sans$', 'cmdline'))
call assert_equal(['Sans'], getcompletion('set guifontwide=Sans$', 'cmdline'))

let &guifontwide = guifontwide_saved
let &guifont = guifont_saved
elseif has('gui_macvim')
let guifont_saved = &guifont
let guifontwide_saved = &guifontwide

" Test recalling default and existing option, and suggesting current font
" size
set guifont=
call assert_equal('Menlo-Regular:h11', getcompletion('set guifont=', 'cmdline')[0])
set guifont=Monaco:h12
call assert_equal('Monaco:h12', getcompletion('set guifont=', 'cmdline')[0])
call assert_equal('h12', getcompletion('set guifont=Menlo\ Italic:', 'cmdline')[0])

" Test auto-completion working for font names
call assert_equal(['Menlo-Regular'], getcompletion('set guifont=Menl*lar$', 'cmdline'))
call assert_equal(['Menlo-Regular'], getcompletion('set guifontwide=Menl*lar$', 'cmdline'))

" Make sure non-monospace fonts are filtered out only in 'guifont'
call assert_equal([], getcompletion('set guifont=Hel*tica$', 'cmdline'))
call assert_equal(['Helvetica'], getcompletion('set guifontwide=Hel*tica$', 'cmdline'))

let &guifontwide = guifontwide_saved
let &guifont = guifont_saved
else
Expand Down
7 changes: 7 additions & 0 deletions src/testdir/test_options.vim
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,13 @@ func Test_set_completion_string_values()
\ '')
call assert_equal([], getcompletion('set hl+=8'..hl_display_modes, 'cmdline'))

"
" MacVim options
"
call assert_equal(getcompletion('set fuoptions+=', 'cmdline')[0], 'maxvert')
call assert_equal(getcompletion('set fuoptions+=maxvert,background:', 'cmdline')[0], '#')
call assert_equal(getcompletion('set fuoptions+=maxvert,background:No*ext', 'cmdline')[0], 'NonText')

"
" Test flag lists
"
Expand Down

0 comments on commit 772df7a

Please sign in to comment.