xaizek / vifm (License: GPLv2+) (since 2018-12-07)
Vifm is a file manager with curses interface, which provides Vi[m]-like environment for managing objects within file systems, extended with some useful ideas from mutt.
Commit d27130be1bb16b6d0226c4b563f3b895c96bac1d

Implement longest completion in engine/completion
It's not hooked to anything yet.
Author: xaizek
Author date (UTC): 2026-11-09 14:11
Committer name: xaizek
Committer date (UTC): 2026-11-09 14:11
Parent(s): 97b8bda15a6b54e03f038e43e22765226e49efce
Signing key: 99DC5E4DB05F6BE2
Tree: 8f762b6d5bfe489dec868a0b35a576bca42b0997
File Lines added Lines deleted
src/engine/completion.c 58 0
src/engine/completion.h 4 0
tests/completion/completion.c 30 0
File src/engine/completion.c changed (mode: 100644) (index f98c3546e..034cbcd76)
32 32 static int add_match(char match[], const char descr[]); static int add_match(char match[], const char descr[]);
33 33 static void group_unique_sort(size_t start_index, size_t len); static void group_unique_sort(size_t start_index, size_t len);
34 34 static int sorter(const void *first, const void *second); static int sorter(const void *first, const void *second);
35 static char * complete_longest(void);
36 static size_t common_prefix_len(const char s[], const char t[]);
35 37 static void remove_duplicates(vle_compl_t arr[], size_t count); static void remove_duplicates(vle_compl_t arr[], size_t count);
36 38
37 39 static enum static enum
 
... ... static int curr = -1;
50 52 static int group_begin; static int group_begin;
51 53 /* Whether the next item is selected from the backward direction. */ /* Whether the next item is selected from the backward direction. */
52 54 static int compl_is_reversed; static int compl_is_reversed;
55 /* Complete the longest prefix instead of full matches. */
56 static int compl_longest;
53 57 /* Optional custom sorter of completion items. */ /* Optional custom sorter of completion items. */
54 58 static vle_compl_sorter_f compl_sorter; static vle_compl_sorter_f compl_sorter;
55 59 /* Cached sorting keys after Unicode normalization if it was necessary. If it /* Cached sorting keys after Unicode normalization if it was necessary. If it
 
... ... vle_compl_reset(void)
76 80 curr = -1; curr = -1;
77 81 group_begin = 0; group_begin = 0;
78 82 compl_is_reversed = 0; compl_is_reversed = 0;
83 compl_longest = 0;
79 84 compl_sorter = NULL; compl_sorter = NULL;
80 85 } }
81 86
 
... ... vle_compl_next(void)
315 320 return strdup(items[idx].text); return strdup(items[idx].text);
316 321 } }
317 322
323 /* Matching Vim (could be a bug, but the behaviour seems useful): reversed
324 * longest completion is full completion. */
325 if(compl_longest && !compl_is_reversed)
326 {
327 return complete_longest();
328 }
329
318 330 if(compl_is_reversed) if(compl_is_reversed)
319 331 { {
320 332 /* Backward. */ /* Backward. */
 
... ... remove_duplicates(vle_compl_t arr[], size_t count)
351 363 } }
352 364 } }
353 365
366 /* Performs the longest completion. */
367 static char *
368 complete_longest(void)
369 {
370 const char *first = items[0].text;
371 size_t match_len = (size_t)-1;
372
373 size_t i;
374 for(i = 0U; i < DA_SIZE(items) - 1; ++i)
375 {
376 size_t prefix_len = common_prefix_len(first, items[i].text);
377 if(match_len == (size_t)-1 || prefix_len < match_len)
378 {
379 match_len = prefix_len;
380 }
381
382 if(match_len == 0)
383 {
384 /* Can't go any lower. */
385 break;
386 }
387 }
388
389 return format_str("%.*s", (int)match_len, first);
390 }
391
392 /* Counts length of the matching prefix of two strings. Returns the length. */
393 static size_t
394 common_prefix_len(const char s[], const char t[])
395 {
396 size_t len = 0U;
397 while(*s == *t && *s != '\0')
398 {
399 ++len;
400 ++s;
401 ++t;
402 }
403 return len;
404 }
405
354 406 int int
355 407 vle_compl_get_count(void) vle_compl_get_count(void)
356 408 { {
 
... ... vle_compl_set_reversed(int reversed)
363 415 compl_is_reversed = reversed; compl_is_reversed = reversed;
364 416 } }
365 417
418 void
419 vle_compl_set_longest(int longest)
420 {
421 compl_longest = longest;
422 }
423
366 424 void void
367 425 vle_compl_set_sorter(vle_compl_sorter_f sorter) vle_compl_set_sorter(vle_compl_sorter_f sorter)
368 426 { {
File src/engine/completion.h changed (mode: 100644) (index 0fa5b8e88..653abce2d)
... ... int vle_compl_get_count(void);
81 81 * direction can be changed at any moment. */ * direction can be changed at any moment. */
82 82 void vle_compl_set_reversed(int reversed); void vle_compl_set_reversed(int reversed);
83 83
84 /* Sets whether the longest prefix should be completed instead of the next full
85 * match (the default). */
86 void vle_compl_set_longest(int longest);
87
84 88 /* Sets or resets (when the parameter is NULL) a custom completion sorter. The /* Sets or resets (when the parameter is NULL) a custom completion sorter. The
85 89 * sorter is always reset by vle_compl_reset(). Must be called after * sorter is always reset by vle_compl_reset(). Must be called after
86 90 * vle_compl_reset() before adding any matches or querying completions. */ * vle_compl_reset() before adding any matches or querying completions. */
File tests/completion/completion.c changed (mode: 100644) (index c9172801e..27f12789e)
... ... TEST(removes_duplicates)
336 336 free(buf); free(buf);
337 337 } }
338 338
339 TEST(longest_completion)
340 {
341 vle_compl_set_longest(1);
342
343 assert_int_equal(0, vle_compl_add_match("vifm", ""));
344 assert_int_equal(0, vle_compl_add_match("vifm.lua", ""));
345 assert_int_equal(0, vle_compl_add_match("vifm.vim", ""));
346 vle_compl_finish_group();
347
348 assert_int_equal(0, vle_compl_add_last_match("v"));
349
350 char *buf;
351
352 /* Completing the longest prefix and staying at it. */
353 assert_string_equal("vifm", buf = vle_compl_next());
354 free(buf);
355 assert_string_equal("vifm", buf = vle_compl_next());
356 free(buf);
357
358 /* In Vim reversed completion always seems to be "full" completion, be
359 * compatible. */
360 vle_compl_set_reversed(1);
361 assert_string_equal("vifm.vim", buf = vle_compl_next());
362 free(buf);
363 assert_string_equal("vifm.lua", buf = vle_compl_next());
364 free(buf);
365 assert_string_equal("vifm", buf = vle_compl_next());
366 free(buf);
367 }
368
339 369 TEST(unicode_sorting, IF(utf8_locale)) TEST(unicode_sorting, IF(utf8_locale))
340 370 { {
341 371 /* Hex-coded names are öäüßÖÄÜ written with and without compount /* Hex-coded names are öäüßÖÄÜ written with and without compount
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/vifm

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/vifm

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master