diff options
| author | Dirk Engling <erdgeist@erdgeist.org> | 2014-01-05 21:23:39 +0100 |
|---|---|---|
| committer | Dirk Engling <erdgeist@erdgeist.org> | 2014-01-05 21:23:39 +0100 |
| commit | b6fdcbeb3ea50e0051749dc552ffb7a736d3c8e1 (patch) | |
| tree | 09171f25965722a710b7a8ed50c173ee372f7d79 /vchat-user.c | |
| parent | f409a5e841d91237fe4034818955dd88167a679b (diff) | |
Dict handling to allow custom completion from user dictionary
Diffstat (limited to 'vchat-user.c')
| -rwxr-xr-x | vchat-user.c | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/vchat-user.c b/vchat-user.c index ce36220..d7261d6 100755 --- a/vchat-user.c +++ b/vchat-user.c | |||
| @@ -30,6 +30,9 @@ static char *g_nick; //< own nick | |||
| 30 | static int g_channel; //< own channel | 30 | static int g_channel; //< own channel |
| 31 | unsigned int ul_case_first = 0; | 31 | unsigned int ul_case_first = 0; |
| 32 | 32 | ||
| 33 | static char **g_dict; | ||
| 34 | static size_t g_dict_len; | ||
| 35 | |||
| 33 | static int ul_nick_lookup( const char *nick, int *exact_match ) { | 36 | static int ul_nick_lookup( const char *nick, int *exact_match ) { |
| 34 | int i; | 37 | int i; |
| 35 | 38 | ||
| @@ -206,6 +209,15 @@ void ul_public_action(char *name) { | |||
| 206 | g_users[base].last_public = ul_now(); | 209 | g_users[base].last_public = ul_now(); |
| 207 | } | 210 | } |
| 208 | 211 | ||
| 212 | void ul_add_to_dict(char *dict_items) { | ||
| 213 | char *i; | ||
| 214 | for(i=strtok(dict_items," ");i;i=strtok(0," ")) { | ||
| 215 | g_dict = realloc( g_dict, sizeof(char*) * ( 1 + g_dict_len ) ); | ||
| 216 | if( !g_dict ) exit(1); | ||
| 217 | g_dict[g_dict_len++] = strdup(i); | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 209 | /* Finding users ul_finduser? */ | 221 | /* Finding users ul_finduser? */ |
| 210 | char * ul_match_user(char *regex) { | 222 | char * ul_match_user(char *regex) { |
| 211 | char *dest = tmpstr; | 223 | char *dest = tmpstr; |
| @@ -327,15 +339,21 @@ static int ul_compare_middle_case( const void *a, const void *b ) { | |||
| 327 | /* Nick completion function for readline */ | 339 | /* Nick completion function for readline */ |
| 328 | char **ul_complete_user(char *text, int start, int end ) { | 340 | char **ul_complete_user(char *text, int start, int end ) { |
| 329 | char **result = 0; | 341 | char **result = 0; |
| 330 | int i, result_count = 0; | 342 | int i, result_count = 0, dict_result_count = 0; |
| 331 | 343 | ||
| 332 | /* Never want readline to complete filenames */ | 344 | /* Never want readline to complete filenames */ |
| 333 | rl_attempted_completion_over = 1; | 345 | rl_attempted_completion_over = 1; |
| 334 | 346 | ||
| 347 | /* Check for amount of custom dict matches */ | ||
| 348 | if( end && ( start != end ) ) | ||
| 349 | for( i=0; i<g_dict_len; ++i ) | ||
| 350 | if( !strncasecmp( g_dict[i], text+start, end-start ) ) | ||
| 351 | ++dict_result_count; | ||
| 352 | |||
| 335 | /* Prepare return array ... of max g_users_count (char*) | 353 | /* Prepare return array ... of max g_users_count (char*) |
| 336 | Plus least common prefix in [0] and null terminator | 354 | Plus least common prefix in [0] and null terminator |
| 337 | */ | 355 | */ |
| 338 | result = malloc( sizeof(char*) * ( 2 + g_users_count ) ); | 356 | result = malloc( sizeof(char*) * ( 2 + g_users_count + dict_result_count ) ); |
| 339 | if( !result ) return 0; | 357 | if( !result ) return 0; |
| 340 | 358 | ||
| 341 | if( start == 0 && end == 0 ) { | 359 | if( start == 0 && end == 0 ) { |
| @@ -365,6 +383,14 @@ char **ul_complete_user(char *text, int start, int end ) { | |||
| 365 | snprintf( tmpstr, TMPSTRSIZE, "%s:", g_users[i].nick ); | 383 | snprintf( tmpstr, TMPSTRSIZE, "%s:", g_users[i].nick ); |
| 366 | result[++result_count] = strdup(tmpstr); | 384 | result[++result_count] = strdup(tmpstr); |
| 367 | } | 385 | } |
| 386 | |||
| 387 | /* Copy matches from personal dict to the end */ | ||
| 388 | for( i=0; i<g_dict_len; ++i ) | ||
| 389 | if( !strncasecmp( g_dict[i], tmpstr, end-start ) ) { | ||
| 390 | snprintf( tmpstr, TMPSTRSIZE, "%s:", g_dict[i] ); | ||
| 391 | result[++result_count] = strdup(tmpstr); | ||
| 392 | } | ||
| 393 | |||
| 368 | /* Copy common prefix */ | 394 | /* Copy common prefix */ |
| 369 | if( result_count ) result[0] = strndup(text, end); | 395 | if( result_count ) result[0] = strndup(text, end); |
| 370 | } else if( start != end ) { | 396 | } else if( start != end ) { |
| @@ -380,6 +406,12 @@ char **ul_complete_user(char *text, int start, int end ) { | |||
| 380 | for( i=0; i<g_users_count; ++i ) | 406 | for( i=0; i<g_users_count; ++i ) |
| 381 | if( !strncasecmp( g_users[i].nick, tmpstr, end - start ) ) | 407 | if( !strncasecmp( g_users[i].nick, tmpstr, end - start ) ) |
| 382 | result[++result_count] = strdup(g_users[i].nick); | 408 | result[++result_count] = strdup(g_users[i].nick); |
| 409 | |||
| 410 | /* Copy matches from personal dict to the end */ | ||
| 411 | for( i=0; i<g_dict_len; ++i ) | ||
| 412 | if( !strncasecmp( g_dict[i], tmpstr, end-start ) ) | ||
| 413 | result[++result_count] = strdup(g_dict[i]); | ||
| 414 | |||
| 383 | /* Copy common prefix */ | 415 | /* Copy common prefix */ |
| 384 | if( result_count ) result[0] = strndup(text, end - start); | 416 | if( result_count ) result[0] = strndup(text, end - start); |
| 385 | } /* else: completion of an empty word in the middle yields nothing */ | 417 | } /* else: completion of an empty word in the middle yields nothing */ |
