(file_generator): Remove leading pathlen characters from the returned names.
Showing
1 changed file
with
35 additions
and
10 deletions
... | @@ -335,9 +335,19 @@ command_compl (int argc, char **argv, int ws) | ... | @@ -335,9 +335,19 @@ command_compl (int argc, char **argv, int ws) |
335 | return rl_completion_matches (argv[argc-1], ml_command_generator); | 335 | return rl_completion_matches (argv[argc-1], ml_command_generator); |
336 | } | 336 | } |
337 | 337 | ||
338 | /* Generate file list based on reference prefix TEXT, relative to PATH. | ||
339 | Remove PATHLEN leading characters from the returned names. Replace | ||
340 | them with REPL unless it is 0. | ||
341 | |||
342 | Select only those files that match given FLAGS (MU_FOLDER_ATTRIBUTE_* | ||
343 | constants). | ||
344 | |||
345 | STATE is 0 for the first call, 1 otherwise. | ||
346 | */ | ||
338 | static char * | 347 | static char * |
339 | file_generator (const char *text, int state, char *path, size_t pathlen, | 348 | file_generator (const char *text, int state, |
340 | char repl, | 349 | char *path, size_t pathlen, |
350 | char repl, | ||
341 | int flags) | 351 | int flags) |
342 | { | 352 | { |
343 | static mu_list_t list; | 353 | static mu_list_t list; |
... | @@ -352,7 +362,12 @@ file_generator (const char *text, int state, char *path, size_t pathlen, | ... | @@ -352,7 +362,12 @@ file_generator (const char *text, int state, char *path, size_t pathlen, |
352 | wcard = xmalloc (strlen (text) + 2); | 362 | wcard = xmalloc (strlen (text) + 2); |
353 | strcat (strcpy (wcard, text), "*"); | 363 | strcat (strcpy (wcard, text), "*"); |
354 | 364 | ||
355 | mu_folder_create (&folder, path); | 365 | if (mu_folder_create (&folder, path)) |
366 | { | ||
367 | free (wcard); | ||
368 | return NULL; | ||
369 | } | ||
370 | |||
356 | mu_folder_list (folder, path, wcard, 1, &list); | 371 | mu_folder_list (folder, path, wcard, 1, &list); |
357 | free (wcard); | 372 | free (wcard); |
358 | mu_folder_destroy (&folder); | 373 | mu_folder_destroy (&folder); |
... | @@ -381,13 +396,17 @@ file_generator (const char *text, int state, char *path, size_t pathlen, | ... | @@ -381,13 +396,17 @@ file_generator (const char *text, int state, char *path, size_t pathlen, |
381 | if (resp->type & flags) | 396 | if (resp->type & flags) |
382 | { | 397 | { |
383 | char *ret; | 398 | char *ret; |
384 | if (repl) | 399 | if (pathlen) |
385 | { | 400 | { |
386 | int len = strlen (resp->name + pathlen); | 401 | size_t len = strlen (resp->name + pathlen); |
387 | ret = xmalloc (len + 2); | 402 | char *ptr; |
388 | ret[0] = repl; | 403 | |
389 | memcpy (ret + 1, resp->name + pathlen, len); | 404 | ret = xmalloc (len + (repl ? 1 : 0) + 1); |
390 | ret[len+1] = 0; | 405 | ptr = ret; |
406 | if (repl) | ||
407 | *ptr++ = repl; | ||
408 | memcpy (ptr, resp->name + pathlen, len); | ||
409 | ptr[len] = 0; | ||
391 | } | 410 | } |
392 | else | 411 | else |
393 | ret = xstrdup (resp->name); | 412 | ret = xstrdup (resp->name); |
... | @@ -483,10 +502,16 @@ dir_generator (const char *text, int state) | ... | @@ -483,10 +502,16 @@ dir_generator (const char *text, int state) |
483 | } | 502 | } |
484 | /* else FIXME! */ | 503 | /* else FIXME! */ |
485 | 504 | ||
486 | default: | 505 | case '/': |
487 | path = strdup (text); | 506 | path = strdup (text); |
488 | pathlen = 0; | 507 | pathlen = 0; |
489 | repl = 0; | 508 | repl = 0; |
509 | break; | ||
510 | |||
511 | default: | ||
512 | path = strdup ("./"); | ||
513 | pathlen = 2; | ||
514 | repl = 0; | ||
490 | } | 515 | } |
491 | 516 | ||
492 | ret = file_generator (text, state, path, pathlen, repl, | 517 | ret = file_generator (text, state, path, pathlen, repl, | ... | ... |
-
Please register or sign in to post a comment