Allow changing command autocompletions via #optionsfull

This commit is contained in:
Pasi Kallinen
2024-10-19 14:45:21 +03:00
parent 64bb0e2144
commit 945ccff1ff
5 changed files with 114 additions and 2 deletions

View File

@@ -1475,6 +1475,7 @@ a pet with the hides-under attribute could "move reluctantly over" a cursed
object and then hide under it object and then hide under it
prevent monster generation in the sokoban trap hallway prevent monster generation in the sokoban trap hallway
change MSGHANDLER from compile-time to sysconf option change MSGHANDLER from compile-time to sysconf option
allow changing extended command autocompletions via #optionsfull
Fixes to 3.7.0-x General Problems Exposed Via git Repository Fixes to 3.7.0-x General Problems Exposed Via git Repository

View File

@@ -394,8 +394,10 @@ extern char extcmd_initiator(void);
extern int doextcmd(void); extern int doextcmd(void);
extern struct ext_func_tab *extcmds_getentry(int); extern struct ext_func_tab *extcmds_getentry(int);
extern int count_bind_keys(void); extern int count_bind_keys(void);
extern int count_autocompletions(void);
extern void get_changed_key_binds(strbuf_t *); extern void get_changed_key_binds(strbuf_t *);
extern void handler_rebind_keys(void); extern void handler_rebind_keys(void);
extern void handler_change_autocompletions(void);
extern int extcmds_match(const char *, int, int **); extern int extcmds_match(const char *, int, int **);
extern const char *key2extcmddesc(uchar); extern const char *key2extcmddesc(uchar);
extern boolean bind_specialkey(uchar, const char *); extern boolean bind_specialkey(uchar, const char *);

View File

@@ -167,6 +167,8 @@ static int optfn_##a(int, int, boolean, char *, char *);
NHOPTB(ascii_map, Advanced, 0, opt_in, set_in_game, NHOPTB(ascii_map, Advanced, 0, opt_in, set_in_game,
ascii_map_Def, Yes, No, No, NoAlias, &iflags.wc_ascii_map, ascii_map_Def, Yes, No, No, NoAlias, &iflags.wc_ascii_map,
Term_False, "show map as text") Term_False, "show map as text")
NHOPTO("autocompletions", Advanced, o_autocomplete, BUFSZ, opt_in, set_in_game,
No, Yes, No, NoAlias, "edit autocompletions")
NHOPTB(autodescribe, Advanced, 0, opt_out, set_in_game, NHOPTB(autodescribe, Advanced, 0, opt_out, set_in_game,
On, Yes, No, No, NoAlias, &iflags.autodescribe, Term_False, On, Yes, No, No, NoAlias, &iflags.autodescribe, Term_False,
"describe terrain under cursor") "describe terrain under cursor")

View File

@@ -2266,6 +2266,73 @@ handler_rebind_keys(void)
} }
} }
void
handler_change_autocompletions(void)
{
winid win;
anything any;
int i, n;
menu_item *picks = (menu_item *) 0;
int clr = NO_COLOR;
struct ext_func_tab *ec;
char buf[BUFSZ];
win = create_nhwindow(NHW_MENU);
start_menu(win, MENU_BEHAVE_STANDARD);
any = cg.zeroany;
for (i = 0; i < extcmdlist_length; i++) {
ec = &extcmdlist[i];
if ((ec->flags & (INTERNALCMD|CMD_NOT_AVAILABLE)) != 0)
continue;
if (strlen(ec->ef_txt) < 2)
continue;
any.a_int = (i + 1);
Sprintf(buf, "%c %s: %s",
(ec->flags & AUTOCOMP_ADJ) ? '*' : ' ',
ec->ef_txt, ec->ef_desc);
add_menu(win, &nul_glyphinfo, &any, '\0', 0, ATR_NONE, clr, buf,
(ec->flags & AUTOCOMPLETE)
? MENU_ITEMFLAGS_SELECTED :
MENU_ITEMFLAGS_NONE);
}
end_menu(win, "Which commands autocomplete?");
n = select_menu(win, PICK_ANY, &picks);
if (n >= 0) {
int j;
for (i = 0; i < extcmdlist_length; i++) {
boolean setit = FALSE;
ec = &extcmdlist[i];
if ((ec->flags & (INTERNALCMD|CMD_NOT_AVAILABLE)) != 0)
continue;
if (strlen(ec->ef_txt) < 2)
continue;
Sprintf(buf, "%s", ec->ef_txt);
for (j = 0; j < n; ++j) {
if (ec == &extcmdlist[(picks[j].item.a_int - 1)]) {
parseautocomplete(buf, TRUE);
setit = TRUE;
break;
}
}
if (!setit) {
parseautocomplete(buf, FALSE);
}
}
}
destroy_nhwindow(win);
}
/* find extended command entries matching findstr. /* find extended command entries matching findstr.
if findstr is NULL, returns all available entries. if findstr is NULL, returns all available entries.
returns: number of matching extended commands, returns: number of matching extended commands,
@@ -2972,8 +3039,12 @@ parseautocomplete(char *autocomplete, boolean condition)
/* find and modify the extended command */ /* find and modify the extended command */
for (efp = extcmdlist; efp->ef_txt; efp++) { for (efp = extcmdlist; efp->ef_txt; efp++) {
if (!strcmp(autocomplete, efp->ef_txt)) { if (!strcmp(autocomplete, efp->ef_txt)) {
if (condition == ((efp->flags & AUTOCOMPLETE) ? FALSE : TRUE)) if (condition == ((efp->flags & AUTOCOMPLETE) ? FALSE : TRUE)) {
efp->flags |= AUTOCOMP_ADJ; if ((efp->flags & AUTOCOMP_ADJ))
efp->flags &= ~AUTOCOMP_ADJ;
else
efp->flags |= AUTOCOMP_ADJ;
}
if (condition) if (condition)
efp->flags |= AUTOCOMPLETE; efp->flags |= AUTOCOMPLETE;
else else
@@ -3004,6 +3075,20 @@ all_options_autocomplete(strbuf_t *sbuf)
} }
} }
/* return the number of changed autocompletions */
int
count_autocompletions(void)
{
struct ext_func_tab *efp;
int n = 0;
for (efp = extcmdlist; efp->ef_txt; efp++)
if ((efp->flags & AUTOCOMP_ADJ) != 0)
n++;
return n;
}
/* save&clear the mouse button actions, or restore the saved ones */ /* save&clear the mouse button actions, or restore the saved ones */
void void
lock_mouse_buttons(boolean savebtns) lock_mouse_buttons(boolean savebtns)

View File

@@ -8164,6 +8164,28 @@ optfn_o_bind_keys(
return optn_ok; return optn_ok;
} }
staticfn int
optfn_o_autocomplete(
int optidx UNUSED, int req, boolean negated UNUSED,
char *opts, char *op UNUSED)
{
if (req == do_init) {
return optn_ok;
}
if (req == do_set) {
}
if (req == get_val || req == get_cnf_val) {
if (!opts)
return optn_err;
Sprintf(opts, n_currently_set, count_autocompletions());
return optn_ok;
}
if (req == do_handler) {
handler_change_autocompletions();
}
return optn_ok;
}
staticfn int staticfn int
optfn_o_menu_colors(int optidx UNUSED, int req, boolean negated UNUSED, optfn_o_menu_colors(int optidx UNUSED, int req, boolean negated UNUSED,
char *opts, char *op UNUSED) char *opts, char *op UNUSED)