refactor: remove CPO_TO_CPO_FLAGS() (#26718)

Just pass p_cpo to replace_termcodes() directly.
This allows removing option_vars.h from keycodes.h, and also avoids the
mistake of passing 0 as cpo_flags.
This commit is contained in:
zeertzjq 2023-12-23 15:53:28 +08:00 committed by GitHub
parent 3c667d3e0f
commit c16d5729b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 32 additions and 39 deletions

View File

@ -912,7 +912,6 @@ def CheckIncludes(filename, lines, error):
"src/nvim/globals.h", "src/nvim/globals.h",
"src/nvim/grid.h", "src/nvim/grid.h",
"src/nvim/highlight.h", "src/nvim/highlight.h",
"src/nvim/keycodes.h",
"src/nvim/lua/executor.h", "src/nvim/lua/executor.h",
"src/nvim/main.h", "src/nvim/main.h",
"src/nvim/mark.h", "src/nvim/mark.h",

View File

@ -461,7 +461,7 @@ String nvim_replace_termcodes(String str, Boolean from_part, Boolean do_lt, Bool
} }
char *ptr = NULL; char *ptr = NULL;
replace_termcodes(str.data, str.size, &ptr, 0, flags, NULL, CPO_TO_CPO_FLAGS); replace_termcodes(str.data, str.size, &ptr, 0, flags, NULL, p_cpo);
return cstr_as_string(ptr); return cstr_as_string(ptr);
} }

View File

@ -4,7 +4,6 @@
#include <stdint.h> #include <stdint.h>
#include "klib/kvec.h" #include "klib/kvec.h"
#include "nvim/decoration_defs.h" // IWYU pragma: keep
#include "nvim/fold_defs.h" // IWYU pragma: keep #include "nvim/fold_defs.h" // IWYU pragma: keep
#include "nvim/macros_defs.h" #include "nvim/macros_defs.h"
#include "nvim/pos_defs.h" #include "nvim/pos_defs.h"

View File

@ -852,8 +852,8 @@ int get_mouse_button(int code, bool *is_click, bool *is_drag)
/// K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER. /// K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
/// ///
/// When "flags" has REPTERM_FROM_PART, trailing <C-v> is included, otherwise it is removed (to make /// When "flags" has REPTERM_FROM_PART, trailing <C-v> is included, otherwise it is removed (to make
/// ":map xx ^V" map xx to nothing). When cpo_flags contains FLAG_CPO_BSLASH, a backslash can be /// ":map xx ^V" map xx to nothing). When cpo_val contains CPO_BSLASH, a backslash can be used in
/// used in place of <C-v>. All other <C-v> characters are removed. /// place of <C-v>. All other <C-v> characters are removed.
/// ///
/// @param[in] from What characters to replace. /// @param[in] from What characters to replace.
/// @param[in] from_len Length of the "from" argument. /// @param[in] from_len Length of the "from" argument.
@ -867,20 +867,21 @@ int get_mouse_button(int code, bool *is_click, bool *is_drag)
/// REPTERM_NO_SPECIAL do not accept <key> notation /// REPTERM_NO_SPECIAL do not accept <key> notation
/// REPTERM_NO_SIMPLIFY do not simplify <C-H> into 0x08, etc. /// REPTERM_NO_SIMPLIFY do not simplify <C-H> into 0x08, etc.
/// @param[out] did_simplify set when some <C-H> code was simplified, unless it is NULL. /// @param[out] did_simplify set when some <C-H> code was simplified, unless it is NULL.
/// @param[in] cpo_flags Relevant flags derived from p_cpo, see CPO_TO_CPO_FLAGS. /// @param[in] cpo_val The value of 'cpoptions' to use. Only CPO_BSLASH matters.
/// ///
/// @return The same as what `*bufp` is set to. /// @return The same as what `*bufp` is set to.
char *replace_termcodes(const char *const from, const size_t from_len, char **const bufp, char *replace_termcodes(const char *const from, const size_t from_len, char **const bufp,
const scid_T sid_arg, const int flags, bool *const did_simplify, const scid_T sid_arg, const int flags, bool *const did_simplify,
const int cpo_flags) const char *const cpo_val)
FUNC_ATTR_NONNULL_ARG(1, 3) FUNC_ATTR_NONNULL_ARG(1, 3, 7)
{ {
char key; char key;
size_t dlen = 0; size_t dlen = 0;
const char *src; const char *src;
const char *const end = from + from_len - 1; const char *const end = from + from_len - 1;
const bool do_backslash = !(cpo_flags & FLAG_CPO_BSLASH); // backslash is a special character // backslash is a special character
const bool do_backslash = (vim_strchr(cpo_val, CPO_BSLASH) == NULL);
const bool do_special = !(flags & REPTERM_NO_SPECIAL); const bool do_special = !(flags & REPTERM_NO_SPECIAL);
bool allocated = (*bufp == NULL); bool allocated = (*bufp == NULL);

View File

@ -4,7 +4,6 @@
#include "nvim/ascii_defs.h" #include "nvim/ascii_defs.h"
#include "nvim/eval/typval_defs.h" // IWYU pragma: keep #include "nvim/eval/typval_defs.h" // IWYU pragma: keep
#include "nvim/option_vars.h"
// Keycode definitions for special keys. // Keycode definitions for special keys.
// //
@ -475,11 +474,6 @@ enum key_extra {
/// This is a total of 6 tokens, and is currently the longest one possible. /// This is a total of 6 tokens, and is currently the longest one possible.
#define MAX_KEY_CODE_LEN 6 #define MAX_KEY_CODE_LEN 6
#define FLAG_CPO_BSLASH 0x01
#define CPO_TO_CPO_FLAGS ((vim_strchr((char *)p_cpo, CPO_BSLASH) == NULL) \
? 0 \
: FLAG_CPO_BSLASH)
/// Flags for replace_termcodes() /// Flags for replace_termcodes()
enum { enum {
REPTERM_FROM_PART = 1, REPTERM_FROM_PART = 1,

View File

@ -303,11 +303,11 @@ static void showmap(mapblock_T *mp, bool local)
/// @param[in] orig_rhs Original mapping RHS, with characters to replace. /// @param[in] orig_rhs Original mapping RHS, with characters to replace.
/// @param[in] rhs_lua Lua reference for Lua mappings. /// @param[in] rhs_lua Lua reference for Lua mappings.
/// @param[in] orig_rhs_len `strlen` of orig_rhs. /// @param[in] orig_rhs_len `strlen` of orig_rhs.
/// @param[in] cpo_flags See param docs for @ref replace_termcodes. /// @param[in] cpo_val See param docs for @ref replace_termcodes.
/// @param[out] mapargs MapArguments struct holding the replaced strings. /// @param[out] mapargs MapArguments struct holding the replaced strings.
static bool set_maparg_lhs_rhs(const char *const orig_lhs, const size_t orig_lhs_len, static bool set_maparg_lhs_rhs(const char *const orig_lhs, const size_t orig_lhs_len,
const char *const orig_rhs, const size_t orig_rhs_len, const char *const orig_rhs, const size_t orig_rhs_len,
const LuaRef rhs_lua, const int cpo_flags, const LuaRef rhs_lua, const char *const cpo_val,
MapArguments *const mapargs) MapArguments *const mapargs)
{ {
char lhs_buf[128]; char lhs_buf[128];
@ -324,7 +324,7 @@ static bool set_maparg_lhs_rhs(const char *const orig_lhs, const size_t orig_lhs
const int flags = REPTERM_FROM_PART | REPTERM_DO_LT; const int flags = REPTERM_FROM_PART | REPTERM_DO_LT;
char *bufarg = lhs_buf; char *bufarg = lhs_buf;
char *replaced = replace_termcodes(orig_lhs, orig_lhs_len, &bufarg, 0, char *replaced = replace_termcodes(orig_lhs, orig_lhs_len, &bufarg, 0,
flags, &did_simplify, cpo_flags); flags, &did_simplify, cpo_val);
if (replaced == NULL) { if (replaced == NULL) {
return false; return false;
} }
@ -332,7 +332,7 @@ static bool set_maparg_lhs_rhs(const char *const orig_lhs, const size_t orig_lhs
xstrlcpy(mapargs->lhs, replaced, sizeof(mapargs->lhs)); xstrlcpy(mapargs->lhs, replaced, sizeof(mapargs->lhs));
if (did_simplify) { if (did_simplify) {
replaced = replace_termcodes(orig_lhs, orig_lhs_len, &bufarg, 0, replaced = replace_termcodes(orig_lhs, orig_lhs_len, &bufarg, 0,
flags | REPTERM_NO_SIMPLIFY, NULL, cpo_flags); flags | REPTERM_NO_SIMPLIFY, NULL, cpo_val);
if (replaced == NULL) { if (replaced == NULL) {
return false; return false;
} }
@ -342,14 +342,14 @@ static bool set_maparg_lhs_rhs(const char *const orig_lhs, const size_t orig_lhs
mapargs->alt_lhs_len = 0; mapargs->alt_lhs_len = 0;
} }
set_maparg_rhs(orig_rhs, orig_rhs_len, rhs_lua, 0, cpo_flags, mapargs); set_maparg_rhs(orig_rhs, orig_rhs_len, rhs_lua, 0, cpo_val, mapargs);
return true; return true;
} }
/// @see set_maparg_lhs_rhs /// @see set_maparg_lhs_rhs
static void set_maparg_rhs(const char *const orig_rhs, const size_t orig_rhs_len, static void set_maparg_rhs(const char *const orig_rhs, const size_t orig_rhs_len,
const LuaRef rhs_lua, const scid_T sid, const int cpo_flags, const LuaRef rhs_lua, const scid_T sid, const char *const cpo_val,
MapArguments *const mapargs) MapArguments *const mapargs)
{ {
mapargs->rhs_lua = rhs_lua; mapargs->rhs_lua = rhs_lua;
@ -365,7 +365,7 @@ static void set_maparg_rhs(const char *const orig_rhs, const size_t orig_rhs_len
} else { } else {
char *rhs_buf = NULL; char *rhs_buf = NULL;
char *replaced = replace_termcodes(orig_rhs, orig_rhs_len, &rhs_buf, sid, char *replaced = replace_termcodes(orig_rhs, orig_rhs_len, &rhs_buf, sid,
REPTERM_DO_LT, NULL, cpo_flags); REPTERM_DO_LT, NULL, cpo_val);
mapargs->rhs_len = strlen(replaced); mapargs->rhs_len = strlen(replaced);
// NB: replace_termcodes may produce an empty string even if orig_rhs is non-empty // NB: replace_termcodes may produce an empty string even if orig_rhs is non-empty
// (e.g. a single ^V, see :h map-empty-rhs) // (e.g. a single ^V, see :h map-empty-rhs)
@ -492,7 +492,7 @@ static int str_to_mapargs(const char *strargs, bool is_unmap, MapArguments *mapa
size_t orig_rhs_len = strlen(rhs_start); size_t orig_rhs_len = strlen(rhs_start);
if (!set_maparg_lhs_rhs(lhs_to_replace, orig_lhs_len, if (!set_maparg_lhs_rhs(lhs_to_replace, orig_lhs_len,
rhs_start, orig_rhs_len, LUA_NOREF, rhs_start, orig_rhs_len, LUA_NOREF,
CPO_TO_CPO_FLAGS, mapargs)) { p_cpo, mapargs)) {
return 1; return 1;
} }
@ -1103,7 +1103,7 @@ bool map_to_exists(const char *const str, const char *const modechars, const boo
char *buf = NULL; char *buf = NULL;
const char *const rhs = replace_termcodes(str, strlen(str), &buf, 0, const char *const rhs = replace_termcodes(str, strlen(str), &buf, 0,
REPTERM_DO_LT, NULL, CPO_TO_CPO_FLAGS); REPTERM_DO_LT, NULL, p_cpo);
#define MAPMODE(mode, modechars, chr, modeflags) \ #define MAPMODE(mode, modechars, chr, modeflags) \
do { \ do { \
@ -1189,16 +1189,16 @@ static bool expand_buffer = false;
/// wider than the original description. The caller has to free the string /// wider than the original description. The caller has to free the string
/// afterwards. /// afterwards.
/// ///
/// @param cpo_flags Value of various flags present in &cpo /// @param[in] cpo_val See param docs for @ref replace_termcodes.
/// ///
/// @return NULL when there is a problem. /// @return NULL when there is a problem.
static char *translate_mapping(char *str_in, int cpo_flags) static char *translate_mapping(const char *const str_in, const char *const cpo_val)
{ {
uint8_t *str = (uint8_t *)str_in; const uint8_t *str = (const uint8_t *)str_in;
garray_T ga; garray_T ga;
ga_init(&ga, 1, 40); ga_init(&ga, 1, 40);
bool cpo_bslash = cpo_flags & FLAG_CPO_BSLASH; const bool cpo_bslash = (vim_strchr(cpo_val, CPO_BSLASH) != NULL);
for (; *str; str++) { for (; *str; str++) {
int c = *str; int c = *str;
@ -1377,7 +1377,7 @@ int ExpandMappings(char *pat, regmatch_T *regmatch, int *numMatches, char ***mat
continue; continue;
} }
char *p = translate_mapping(mp->m_keys, CPO_TO_CPO_FLAGS); char *p = translate_mapping(mp->m_keys, p_cpo);
if (p == NULL) { if (p == NULL) {
continue; continue;
} }
@ -1677,7 +1677,7 @@ char *eval_map_expr(mapblock_T *mp, int c)
char *res = NULL; char *res = NULL;
if (replace_keycodes) { if (replace_keycodes) {
replace_termcodes(p, strlen(p), &res, 0, REPTERM_DO_LT, NULL, CPO_TO_CPO_FLAGS); replace_termcodes(p, strlen(p), &res, 0, REPTERM_DO_LT, NULL, p_cpo);
} else { } else {
// Escape K_SPECIAL in the result to be able to use the string as typeahead. // Escape K_SPECIAL in the result to be able to use the string as typeahead.
res = vim_strsave_escape_ks(p); res = vim_strsave_escape_ks(p);
@ -2168,7 +2168,7 @@ static void get_maparg(typval_T *argvars, typval_T *rettv, int exact)
const int mode = get_map_mode((char **)&which, 0); const int mode = get_map_mode((char **)&which, 0);
char *keys_simplified = replace_termcodes(keys, strlen(keys), &keys_buf, 0, char *keys_simplified = replace_termcodes(keys, strlen(keys), &keys_buf, 0,
flags, &did_simplify, CPO_TO_CPO_FLAGS); flags, &did_simplify, p_cpo);
mapblock_T *mp = NULL; mapblock_T *mp = NULL;
int buffer_local; int buffer_local;
LuaRef rhs_lua; LuaRef rhs_lua;
@ -2178,7 +2178,7 @@ static void get_maparg(typval_T *argvars, typval_T *rettv, int exact)
// When the lhs is being simplified the not-simplified keys are // When the lhs is being simplified the not-simplified keys are
// preferred for printing, like in do_map(). // preferred for printing, like in do_map().
(void)replace_termcodes(keys, strlen(keys), &alt_keys_buf, 0, (void)replace_termcodes(keys, strlen(keys), &alt_keys_buf, 0,
flags | REPTERM_NO_SIMPLIFY, NULL, CPO_TO_CPO_FLAGS); flags | REPTERM_NO_SIMPLIFY, NULL, p_cpo);
rhs = check_map(alt_keys_buf, mode, exact, false, abbr, &mp, &buffer_local, &rhs_lua); rhs = check_map(alt_keys_buf, mode, exact, false, abbr, &mp, &buffer_local, &rhs_lua);
} }
@ -2343,14 +2343,14 @@ void f_mapset(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
bool buffer = tv_dict_get_number(d, "buffer") != 0; bool buffer = tv_dict_get_number(d, "buffer") != 0;
// mode from the dict is not used // mode from the dict is not used
set_maparg_rhs(orig_rhs, strlen(orig_rhs), rhs_lua, sid, CPO_TO_CPO_FLAGS, &args); set_maparg_rhs(orig_rhs, strlen(orig_rhs), rhs_lua, sid, p_cpo, &args);
mapblock_T **map_table = buffer ? curbuf->b_maphash : maphash; mapblock_T **map_table = buffer ? curbuf->b_maphash : maphash;
mapblock_T **abbr_table = buffer ? &curbuf->b_first_abbr : &first_abbr; mapblock_T **abbr_table = buffer ? &curbuf->b_first_abbr : &first_abbr;
// Delete any existing mapping for this lhs and mode. // Delete any existing mapping for this lhs and mode.
MapArguments unmap_args = MAP_ARGUMENTS_INIT; MapArguments unmap_args = MAP_ARGUMENTS_INIT;
set_maparg_lhs_rhs(lhs, strlen(lhs), "", 0, LUA_NOREF, CPO_TO_CPO_FLAGS, &unmap_args); set_maparg_lhs_rhs(lhs, strlen(lhs), "", 0, LUA_NOREF, p_cpo, &unmap_args);
unmap_args.buffer = buffer; unmap_args.buffer = buffer;
buf_do_map(MAPTYPE_UNMAP, &unmap_args, mode, is_abbr, curbuf); buf_do_map(MAPTYPE_UNMAP, &unmap_args, mode, is_abbr, curbuf);
xfree(unmap_args.rhs); xfree(unmap_args.rhs);
@ -2400,7 +2400,7 @@ void f_maplist(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
char *lhs = str2special_save(mp->m_keys, true, false); char *lhs = str2special_save(mp->m_keys, true, false);
(void)replace_termcodes(lhs, strlen(lhs), &keys_buf, 0, flags, &did_simplify, (void)replace_termcodes(lhs, strlen(lhs), &keys_buf, 0, flags, &did_simplify,
CPO_TO_CPO_FLAGS); p_cpo);
xfree(lhs); xfree(lhs);
Dictionary dict = mapblock_fill_dict(mp, Dictionary dict = mapblock_fill_dict(mp,
@ -2440,7 +2440,7 @@ void f_mapcheck(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
void add_map(char *lhs, char *rhs, int mode, bool buffer) void add_map(char *lhs, char *rhs, int mode, bool buffer)
{ {
MapArguments args = MAP_ARGUMENTS_INIT; MapArguments args = MAP_ARGUMENTS_INIT;
set_maparg_lhs_rhs(lhs, strlen(lhs), rhs, strlen(rhs), LUA_NOREF, 0, &args); set_maparg_lhs_rhs(lhs, strlen(lhs), rhs, strlen(rhs), LUA_NOREF, p_cpo, &args);
args.buffer = buffer; args.buffer = buffer;
buf_do_map(MAPTYPE_NOREMAP, &args, mode, false, curbuf); buf_do_map(MAPTYPE_NOREMAP, &args, mode, false, curbuf);
@ -2720,7 +2720,7 @@ void modify_keymap(uint64_t channel_id, Buffer buffer, bool is_unmap, String mod
if (!set_maparg_lhs_rhs(lhs.data, lhs.size, if (!set_maparg_lhs_rhs(lhs.data, lhs.size,
rhs.data, rhs.size, lua_funcref, rhs.data, rhs.size, lua_funcref,
CPO_TO_CPO_FLAGS, &parsed_args)) { p_cpo, &parsed_args)) {
api_set_error(err, kErrorTypeValidation, "LHS exceeds maximum map length: %s", lhs.data); api_set_error(err, kErrorTypeValidation, "LHS exceeds maximum map length: %s", lhs.data);
goto fail_and_free; goto fail_and_free;
} }

View File

@ -226,7 +226,7 @@ void ex_menu(exarg_T *eap)
} else { } else {
map_buf = NULL; map_buf = NULL;
map_to = replace_termcodes(map_to, strlen(map_to), &map_buf, 0, map_to = replace_termcodes(map_to, strlen(map_to), &map_buf, 0,
REPTERM_DO_LT, NULL, CPO_TO_CPO_FLAGS); REPTERM_DO_LT, NULL, p_cpo);
} }
menuarg.modes = modes; menuarg.modes = modes;
menuarg.noremap[0] = noremap; menuarg.noremap[0] = noremap;

View File

@ -872,7 +872,7 @@ int uc_add_command(char *name, size_t name_len, const char *rep, uint32_t argt,
char *rep_buf = NULL; char *rep_buf = NULL;
garray_T *gap; garray_T *gap;
replace_termcodes(rep, strlen(rep), &rep_buf, 0, 0, NULL, CPO_TO_CPO_FLAGS); replace_termcodes(rep, strlen(rep), &rep_buf, 0, 0, NULL, p_cpo);
if (rep_buf == NULL) { if (rep_buf == NULL) {
// Can't replace termcodes - try using the string as is // Can't replace termcodes - try using the string as is
rep_buf = xstrdup(rep); rep_buf = xstrdup(rep);