comparison src/map.c @ 27908:099c2e612827 v8.2.4479

patch 8.2.4479: no fuzzy completieon for maps and abbreviations Commit: https://github.com/vim/vim/commit/6caeda2fce4bccac2dd43ca9fee1d32ee96b708d Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Sun Feb 27 12:07:30 2022 +0000 patch 8.2.4479: no fuzzy completieon for maps and abbreviations Problem: No fuzzy completieon for maps and abbreviations. Solution: Fuzzy complete maps and abbreviations. (Yegappan Lakshmanan, closes #9856)
author Bram Moolenaar <Bram@vim.org>
date Sun, 27 Feb 2022 13:15:03 +0100
parents fc0a37304590
children 6efa2f193c94
comparison
equal deleted inserted replaced
27907:172f5b915674 27908:099c2e612827
1255 * For command line expansion of ":[un]map" and ":[un]abbrev" in all modes. 1255 * For command line expansion of ":[un]map" and ":[un]abbrev" in all modes.
1256 * Return OK if matches found, FAIL otherwise. 1256 * Return OK if matches found, FAIL otherwise.
1257 */ 1257 */
1258 int 1258 int
1259 ExpandMappings( 1259 ExpandMappings(
1260 char_u *pat,
1260 regmatch_T *regmatch, 1261 regmatch_T *regmatch,
1261 int *num_file, 1262 int *numMatches,
1262 char_u ***file) 1263 char_u ***matches)
1263 { 1264 {
1264 mapblock_T *mp; 1265 mapblock_T *mp;
1265 int hash; 1266 int hash;
1266 int count; 1267 int count;
1267 int round; 1268 int round;
1268 char_u *p; 1269 char_u *p;
1269 int i; 1270 int i;
1271 int fuzzy;
1272 int match;
1273 int score;
1274 fuzmatch_str_T *fuzmatch = NULL;
1275
1276 fuzzy = cmdline_fuzzy_complete(pat);
1270 1277
1271 validate_maphash(); 1278 validate_maphash();
1272 1279
1273 *num_file = 0; // return values in case of FAIL 1280 *numMatches = 0; // return values in case of FAIL
1274 *file = NULL; 1281 *matches = NULL;
1275 1282
1276 // round == 1: Count the matches. 1283 // round == 1: Count the matches.
1277 // round == 2: Build the array to keep the matches. 1284 // round == 2: Build the array to keep the matches.
1278 for (round = 1; round <= 2; ++round) 1285 for (round = 1; round <= 2; ++round)
1279 { 1286 {
1280 count = 0; 1287 count = 0;
1281 1288
1289 // First search in map modifier arguments
1282 for (i = 0; i < 7; ++i) 1290 for (i = 0; i < 7; ++i)
1283 { 1291 {
1284 if (i == 0) 1292 if (i == 0)
1285 p = (char_u *)"<silent>"; 1293 p = (char_u *)"<silent>";
1286 else if (i == 1) 1294 else if (i == 1)
1298 else if (i == 6) 1306 else if (i == 6)
1299 p = (char_u *)"<special>"; 1307 p = (char_u *)"<special>";
1300 else 1308 else
1301 continue; 1309 continue;
1302 1310
1303 if (vim_regexec(regmatch, p, (colnr_T)0)) 1311 if (!fuzzy)
1304 { 1312 match = vim_regexec(regmatch, p, (colnr_T)0);
1305 if (round == 1) 1313 else
1306 ++count; 1314 {
1315 score = fuzzy_match_str(p, pat);
1316 match = (score != 0);
1317 }
1318
1319 if (!match)
1320 continue;
1321
1322 if (round == 2)
1323 {
1324 if (fuzzy)
1325 {
1326 fuzmatch[count].idx = count;
1327 fuzmatch[count].str = vim_strsave(p);
1328 fuzmatch[count].score = score;
1329 }
1307 else 1330 else
1308 (*file)[count++] = vim_strsave(p); 1331 (*matches)[count] = vim_strsave(p);
1309 } 1332 }
1333 ++count;
1310 } 1334 }
1311 1335
1312 for (hash = 0; hash < 256; ++hash) 1336 for (hash = 0; hash < 256; ++hash)
1313 { 1337 {
1314 if (expand_isabbrev) 1338 if (expand_isabbrev)
1324 for (; mp; mp = mp->m_next) 1348 for (; mp; mp = mp->m_next)
1325 { 1349 {
1326 if (mp->m_mode & expand_mapmodes) 1350 if (mp->m_mode & expand_mapmodes)
1327 { 1351 {
1328 p = translate_mapping(mp->m_keys); 1352 p = translate_mapping(mp->m_keys);
1329 if (p != NULL && vim_regexec(regmatch, p, (colnr_T)0)) 1353 if (p != NULL)
1330 { 1354 {
1331 if (round == 1) 1355 if (!fuzzy)
1332 ++count; 1356 match = vim_regexec(regmatch, p, (colnr_T)0);
1333 else 1357 else
1334 { 1358 {
1335 (*file)[count++] = p; 1359 score = fuzzy_match_str(p, pat);
1336 p = NULL; 1360 match = (score != 0);
1361 }
1362
1363 if (match)
1364 {
1365 if (round == 2)
1366 {
1367 if (fuzzy)
1368 {
1369 fuzmatch[count].idx = count;
1370 fuzmatch[count].str = p;
1371 fuzmatch[count].score = score;
1372 }
1373 else
1374 (*matches)[count] = p;
1375 p = NULL;
1376 }
1377 ++count;
1337 } 1378 }
1338 } 1379 }
1339 vim_free(p); 1380 vim_free(p);
1340 } 1381 }
1341 } // for (mp) 1382 } // for (mp)
1344 if (count == 0) // no match found 1385 if (count == 0) // no match found
1345 break; // for (round) 1386 break; // for (round)
1346 1387
1347 if (round == 1) 1388 if (round == 1)
1348 { 1389 {
1349 *file = ALLOC_MULT(char_u *, count); 1390 if (fuzzy)
1350 if (*file == NULL) 1391 {
1351 return FAIL; 1392 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
1393 if (fuzmatch == NULL)
1394 return FAIL;
1395 }
1396 else
1397 {
1398 *matches = ALLOC_MULT(char_u *, count);
1399 if (*matches == NULL)
1400 return FAIL;
1401 }
1352 } 1402 }
1353 } // for (round) 1403 } // for (round)
1404
1405 if (fuzzy && fuzzymatches_to_strmatches(fuzmatch, matches, count,
1406 FALSE) == FAIL)
1407 return FAIL;
1354 1408
1355 if (count > 1) 1409 if (count > 1)
1356 { 1410 {
1357 char_u **ptr1; 1411 char_u **ptr1;
1358 char_u **ptr2; 1412 char_u **ptr2;
1359 char_u **ptr3; 1413 char_u **ptr3;
1360 1414
1361 // Sort the matches 1415 // Sort the matches
1362 sort_strings(*file, count); 1416 // Fuzzy matching already sorts the matches
1417 if (!fuzzy)
1418 sort_strings(*matches, count);
1363 1419
1364 // Remove multiple entries 1420 // Remove multiple entries
1365 ptr1 = *file; 1421 ptr1 = *matches;
1366 ptr2 = ptr1 + 1; 1422 ptr2 = ptr1 + 1;
1367 ptr3 = ptr1 + count; 1423 ptr3 = ptr1 + count;
1368 1424
1369 while (ptr2 < ptr3) 1425 while (ptr2 < ptr3)
1370 { 1426 {
1376 count--; 1432 count--;
1377 } 1433 }
1378 } 1434 }
1379 } 1435 }
1380 1436
1381 *num_file = count; 1437 *numMatches = count;
1382 return (count == 0 ? FAIL : OK); 1438 return (count == 0 ? FAIL : OK);
1383 } 1439 }
1384 1440
1385 /* 1441 /*
1386 * Check for an abbreviation. 1442 * Check for an abbreviation.