keymap: Do not use vim_isIDc in keymap.c

Note: there are three changes to ascii_isident. Reverting first two (in 
find_special_key and first in get_special_key_code) normally fails the new test 
with empty &isident, but reverting the third does not. Hence adding `>` to 
&isident.

Ref vim/vim#2389.
This commit is contained in:
ZyX
2017-11-30 02:01:49 +03:00
parent 36a4f3a259
commit de45ec0146
6 changed files with 44 additions and 17 deletions

View File

@@ -3,6 +3,7 @@
#include <stdbool.h> #include <stdbool.h>
#include "nvim/macros.h"
#include "nvim/func_attr.h" #include "nvim/func_attr.h"
#include "nvim/os/os_defs.h" #include "nvim/os/os_defs.h"
@@ -98,6 +99,10 @@ static inline bool ascii_isxdigit(int)
REAL_FATTR_CONST REAL_FATTR_CONST
REAL_FATTR_ALWAYS_INLINE; REAL_FATTR_ALWAYS_INLINE;
static inline bool ascii_isident(int)
REAL_FATTR_CONST
REAL_FATTR_ALWAYS_INLINE;
static inline bool ascii_isbdigit(int) static inline bool ascii_isbdigit(int)
REAL_FATTR_CONST REAL_FATTR_CONST
REAL_FATTR_ALWAYS_INLINE; REAL_FATTR_ALWAYS_INLINE;
@@ -138,6 +143,14 @@ static inline bool ascii_isxdigit(int c)
|| (c >= 'A' && c <= 'F'); || (c >= 'A' && c <= 'F');
} }
/// Checks if `c` is an “identifier” character
///
/// That is, whether it is alphanumeric character or underscore.
static inline bool ascii_isident(const int c)
{
return ASCII_ISALNUM(c) || c == '_';
}
/// Checks if `c` is a binary digit, that is, 0-1. /// Checks if `c` is a binary digit, that is, 0-1.
/// ///
/// @see {ascii_isdigit} /// @see {ascii_isdigit}

View File

@@ -567,7 +567,7 @@ int find_special_key(const char_u **srcp, const size_t src_len, int *const modp,
// Find end of modifier list // Find end of modifier list
last_dash = src; last_dash = src;
for (bp = src + 1; bp <= end && (*bp == '-' || vim_isIDc(*bp)); bp++) { for (bp = src + 1; bp <= end && (*bp == '-' || ascii_isident(*bp)); bp++) {
if (*bp == '-') { if (*bp == '-') {
last_dash = bp; last_dash = bp;
if (bp + 1 <= end) { if (bp + 1 <= end) {
@@ -721,12 +721,12 @@ int get_special_key_code(const char_u *name)
for (int i = 0; key_names_table[i].name != NULL; i++) { for (int i = 0; key_names_table[i].name != NULL; i++) {
const char *const table_name = key_names_table[i].name; const char *const table_name = key_names_table[i].name;
int j; int j;
for (j = 0; vim_isIDc(name[j]) && table_name[j] != NUL; j++) { for (j = 0; ascii_isident(name[j]) && table_name[j] != NUL; j++) {
if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j])) { if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j])) {
break; break;
} }
} }
if (!vim_isIDc(name[j]) && table_name[j] == NUL) { if (!ascii_isident(name[j]) && table_name[j] == NUL) {
return key_names_table[i].key; return key_names_table[i].key;
} }
} }

View File

@@ -386,13 +386,11 @@ LexExprToken viml_pexpr_next_token(ParserState *const pstate, const int flags)
} }
#define ISWORD_OR_AUTOLOAD(x) \ #define ISWORD_OR_AUTOLOAD(x) \
(ASCII_ISALNUM(x) || (x) == AUTOLOAD_CHAR || (x) == '_') (ascii_isident(x) || (x) == AUTOLOAD_CHAR)
#define ISWORD(x) \
(ASCII_ISALNUM(x) || (x) == '_')
// Environment variable. // Environment variable.
case '$': { case '$': {
CHARREG(kExprLexEnv, ISWORD); CHARREG(kExprLexEnv, ascii_isident);
break; break;
} }
@@ -408,7 +406,7 @@ LexExprToken viml_pexpr_next_token(ParserState *const pstate, const int flags)
case '_': { case '_': {
ret.data.var.scope = 0; ret.data.var.scope = 0;
ret.data.var.autoload = false; ret.data.var.autoload = false;
CHARREG(kExprLexPlainIdentifier, ISWORD); CHARREG(kExprLexPlainIdentifier, ascii_isident);
// "is" and "isnot" operators. // "is" and "isnot" operators.
if (!(flags & kELFlagIsNotCmp) if (!(flags & kELFlagIsNotCmp)
&& ((ret.len == 2 && memcmp(pline.data, "is", 2) == 0) && ((ret.len == 2 && memcmp(pline.data, "is", 2) == 0)
@@ -445,7 +443,6 @@ LexExprToken viml_pexpr_next_token(ParserState *const pstate, const int flags)
break; break;
} }
#undef ISWORD
#undef ISWORD_OR_AUTOLOAD #undef ISWORD_OR_AUTOLOAD
#undef CHARREG #undef CHARREG

View File

@@ -0,0 +1,21 @@
local helpers = require("test.functional.helpers")(after_each)
local eq = helpers.eq
local feed = helpers.feed
local meths = helpers.meths
local clear = helpers.clear
local command = helpers.command
describe(':*map', function()
before_each(clear)
it('are not affected by &isident', function()
meths.set_var('counter', 0)
command('nnoremap <C-x> :let counter+=1<CR>')
meths.set_option('isident', ('%u'):format(('>'):byte()))
command('nnoremap <C-y> :let counter+=1<CR>')
-- &isident used to disable keycode parsing here as well
feed('\24\25<C-x><C-y>')
eq(4, meths.get_var('counter'))
end)
end)

View File

@@ -6,11 +6,6 @@
#include "nvim/eval/typval.h" #include "nvim/eval/typval.h"
#include "nvim/vim.h" #include "nvim/vim.h"
bool vim_isIDc(int c)
{
return ASCII_ISALNUM(c);
}
int hex2nr(int c) int hex2nr(int c)
{ {
if ((c >= 'a') && (c <= 'f')) { if ((c >= 'a') && (c <= 'f')) {

View File

@@ -2,6 +2,7 @@
#include "nvim/types.h" #include "nvim/types.h"
#include "nvim/keymap.h" #include "nvim/keymap.h"
#include "nvim/ascii.h"
#include "nvim/eval/typval.h" #include "nvim/eval/typval.h"
#define MOD_KEYS_ENTRY_SIZE 5 #define MOD_KEYS_ENTRY_SIZE 5
@@ -294,12 +295,12 @@ int get_special_key_code(const char_u *name)
for (int i = 0; key_names_table[i].name != NULL; i++) { for (int i = 0; key_names_table[i].name != NULL; i++) {
const char *const table_name = key_names_table[i].name; const char *const table_name = key_names_table[i].name;
int j; int j;
for (j = 0; vim_isIDc(name[j]) && table_name[j] != NUL; j++) { for (j = 0; ascii_isident(name[j]) && table_name[j] != NUL; j++) {
if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j])) { if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j])) {
break; break;
} }
} }
if (!vim_isIDc(name[j]) && table_name[j] == NUL) { if (!ascii_isident(name[j]) && table_name[j] == NUL) {
return key_names_table[i].key; return key_names_table[i].key;
} }
} }
@@ -386,7 +387,7 @@ int find_special_key(const char_u **srcp, const size_t src_len, int *const modp,
// Find end of modifier list // Find end of modifier list
last_dash = src; last_dash = src;
for (bp = src + 1; bp <= end && (*bp == '-' || vim_isIDc(*bp)); bp++) { for (bp = src + 1; bp <= end && (*bp == '-' || ascii_isident(*bp)); bp++) {
if (*bp == '-') { if (*bp == '-') {
last_dash = bp; last_dash = bp;
if (bp + 1 <= end) { if (bp + 1 <= end) {