Mercurial > hg > nnchat
comparison main.c @ 447:2d650408f5de
Modularize tab completion code a bit.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 26 May 2012 03:42:32 +0300 |
parents | 3396acd40147 |
children | 73ff0a592b2f |
comparison
equal
deleted
inserted
replaced
446:3396acd40147 | 447:2d650408f5de |
---|---|
1338 if (statusWin) redrawwin(statusWin); | 1338 if (statusWin) redrawwin(statusWin); |
1339 if (editWin) redrawwin(editWin); | 1339 if (editWin) redrawwin(editWin); |
1340 } | 1340 } |
1341 | 1341 |
1342 | 1342 |
1343 void tabCompletionProd(nn_editbuf_t *buf, size_t *pi, const size_t startPos, const size_t endPos, char *c) | |
1344 { | |
1345 size_t i; | |
1346 | |
1347 for (i = startPos; i <= endPos; i++) | |
1348 nn_editbuf_delete(buf, startPos); | |
1349 | |
1350 for (i = startPos; *c; i++, c++) | |
1351 nn_editbuf_insert(buf, i, *c); | |
1352 | |
1353 *pi = i; | |
1354 } | |
1355 | |
1356 | |
1357 void tabCompletionFinish(nn_editbuf_t *buf, char **previous, const size_t startPos, const char *name) | |
1358 { | |
1359 nn_editbuf_setpos(buf, startPos + 1 + strlen(name)); | |
1360 th_free(*previous); | |
1361 *previous = th_strdup(name); | |
1362 } | |
1363 | |
1364 | |
1343 BOOL performTabCompletion(nn_editbuf_t *buf) | 1365 BOOL performTabCompletion(nn_editbuf_t *buf) |
1344 { | 1366 { |
1345 static char *previous = NULL, *pattern = NULL; | 1367 static char *previous = NULL, *pattern = NULL; |
1346 BOOL again = FALSE, hasSeparator = FALSE, | 1368 BOOL again = FALSE, hasSeparator = FALSE, |
1347 newPattern = FALSE, hasSpace = FALSE, isCommand; | 1369 hasSpace, newPattern = FALSE, isCommand; |
1348 char *str = buf->data; | 1370 char *str = buf->data; |
1349 int mode = 0; | |
1350 size_t endPos, startPos = buf->pos; | 1371 size_t endPos, startPos = buf->pos; |
1351 | 1372 |
1352 // previous word | 1373 // previous word |
1353 if (startPos >= 2 && str[startPos - 1] == ' ' && str[startPos - 2] != ' ') | 1374 if (startPos >= 2 && str[startPos - 1] == ' ' && str[startPos - 2] != ' ') |
1354 { | 1375 { |
1355 startPos -= 2; | 1376 startPos -= 2; |
1356 endPos = startPos; | 1377 endPos = startPos; |
1357 while (startPos > 0 && str[startPos - 1] != ' ') startPos--; | 1378 while (startPos > 0 && str[startPos - 1] != ' ') startPos--; |
1358 mode = 1; | |
1359 } | 1379 } |
1360 else | 1380 else |
1361 // middle of a word, new pattern | 1381 // middle of a word, new pattern |
1362 if (startPos < buf->len && str[startPos] != ' ') | 1382 if (startPos < buf->len && str[startPos] != ' ') |
1363 { | 1383 { |
1364 endPos = startPos; | 1384 endPos = startPos; |
1365 while (startPos > 0 && str[startPos - 1] != ' ') startPos--; | 1385 while (startPos > 0 && str[startPos - 1] != ' ') startPos--; |
1366 while (endPos < buf->len - 1 && str[endPos + 1] != ' ') endPos++; | 1386 while (endPos < buf->len - 1 && str[endPos + 1] != ' ') endPos++; |
1367 newPattern = TRUE; | 1387 newPattern = TRUE; |
1368 mode = 2; | |
1369 } | 1388 } |
1370 else | 1389 else |
1371 // previous word, new pattern | 1390 // previous word, new pattern |
1372 if (startPos >= 1 && str[startPos - 1] != ' ') | 1391 if (startPos >= 1 && str[startPos - 1] != ' ') |
1373 { | 1392 { |
1374 startPos -= 1; | 1393 startPos -= 1; |
1375 endPos = startPos; | 1394 endPos = startPos; |
1376 while (startPos > 0 && str[startPos - 1] != ' ') startPos--; | 1395 while (startPos > 0 && str[startPos - 1] != ' ') startPos--; |
1377 newPattern = TRUE; | 1396 newPattern = TRUE; |
1378 mode = 3; | |
1379 } | 1397 } |
1380 else | 1398 else |
1381 { | 1399 { |
1382 if (optDebug) | 1400 if (optDebug) |
1383 printMsg(currWin, "no mode\n"); | 1401 printMsg(currWin, "no mode\n"); |
1397 return FALSE; | 1415 return FALSE; |
1398 } | 1416 } |
1399 hasSeparator = TRUE; | 1417 hasSeparator = TRUE; |
1400 } | 1418 } |
1401 | 1419 |
1402 if (buf->pos > 0 && str[buf->pos - 1] == ' ') | 1420 hasSpace = (buf->pos > 0 && str[buf->pos - 1] == ' ') || |
1403 hasSpace = TRUE; | 1421 (buf->pos <= buf->len && str[buf->pos] == ' '); |
1404 if (buf->pos <= buf->len && str[buf->pos] == ' ') | |
1405 hasSpace = TRUE; | |
1406 | 1422 |
1407 if (newPattern) | 1423 if (newPattern) |
1408 { | 1424 { |
1409 // Get pattern, check if it matches previous pattern and set 'again' flag | 1425 // Get pattern, check if it matches previous pattern and set 'again' flag |
1410 char *npattern = nn_editbuf_get_string(buf, startPos, endPos); | 1426 char *npattern = nn_editbuf_get_string(buf, startPos, endPos); |
1449 nn_user_t *user = nn_userhash_match(nnUsers, pattern, previous, again); | 1465 nn_user_t *user = nn_userhash_match(nnUsers, pattern, previous, again); |
1450 | 1466 |
1451 if (user) | 1467 if (user) |
1452 { | 1468 { |
1453 size_t i; | 1469 size_t i; |
1454 char *c = user->name; | 1470 tabCompletionProd(buf, &i, startPos, endPos, user->name); |
1455 if (optDebug) | |
1456 printMsg(currWin, "match='%s' / prev='%s'\n", user->name, previous); | |
1457 | |
1458 for (i = startPos; i <= endPos; i++) | |
1459 nn_editbuf_delete(buf, startPos); | |
1460 | |
1461 for (i = startPos; *c; i++, c++) | |
1462 nn_editbuf_insert(buf, i, *c); | |
1463 | 1471 |
1464 if (!hasSeparator && startPos == 0) | 1472 if (!hasSeparator && startPos == 0) |
1465 { | 1473 { |
1466 nn_editbuf_insert(buf, i++, optNickSep); | 1474 nn_editbuf_insert(buf, i++, optNickSep); |
1467 startPos++; | 1475 startPos++; |
1468 } | 1476 } |
1477 else | |
1469 if (hasSeparator) | 1478 if (hasSeparator) |
1470 startPos++; | 1479 startPos++; |
1480 | |
1471 if (!hasSpace) | 1481 if (!hasSpace) |
1472 nn_editbuf_insert(buf, i++, ' '); | 1482 nn_editbuf_insert(buf, i++, ' '); |
1473 | 1483 |
1474 nn_editbuf_setpos(buf, startPos + 1 + strlen(user->name)); | 1484 tabCompletionFinish(buf, &previous, startPos, user->name); |
1475 | |
1476 th_free(previous); | |
1477 previous = th_strdup(user->name); | |
1478 | |
1479 return TRUE; | 1485 return TRUE; |
1480 } | 1486 } |
1481 } | 1487 } |
1482 | 1488 |
1483 return FALSE; | 1489 return FALSE; |