diff options
author | Björn Linse <bjorn.linse@gmail.com> | 2021-12-10 23:38:00 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-10 23:38:00 +0100 |
commit | 3f8703093de56254ffdbf8ef6ddbe7942af54257 (patch) | |
tree | 3261cb4da5542e767c386129f436c21bdcf78be6 /src/nvim/os/users.c | |
parent | 53c95ccd1bb63c89afca18f86d38aba26f5ec90d (diff) | |
parent | df54d82b7c98ae5b1608c56e0dc216d77ebb3101 (diff) | |
download | rneovim-3f8703093de56254ffdbf8ef6ddbe7942af54257.tar.gz rneovim-3f8703093de56254ffdbf8ef6ddbe7942af54257.tar.bz2 rneovim-3f8703093de56254ffdbf8ef6ddbe7942af54257.zip |
Merge pull request #16607 from bfredl/no2misc1
refactor: get rid of misc1.c ("functions that didn't seem to fit elsewhere")
Diffstat (limited to 'src/nvim/os/users.c')
-rw-r--r-- | src/nvim/os/users.c | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/nvim/os/users.c b/src/nvim/os/users.c index 9952e2b387..e0ce3fec31 100644 --- a/src/nvim/os/users.c +++ b/src/nvim/os/users.c @@ -18,6 +18,9 @@ # include <lm.h> #endif +// All user names (for ~user completion as done by shell). +static garray_T ga_users = GA_EMPTY_INIT_VALUE; + // Add a user name to the list of users in garray_T *users. // Do nothing if user name is NULL or empty. static void add_user(garray_T *users, char *user, bool need_copy) @@ -157,3 +160,60 @@ char *os_get_user_directory(const char *name) return NULL; } + +#if defined(EXITFREE) + +void free_users(void) +{ + ga_clear_strings(&ga_users); +} + +#endif + +/// Find all user names for user completion. +/// +/// Done only once and then cached. +static void init_users(void) +{ + static int lazy_init_done = false; + + if (lazy_init_done) { + return; + } + + lazy_init_done = true; + + os_get_usernames(&ga_users); +} + +/// Given to ExpandGeneric() to obtain an user names. +char_u *get_users(expand_T *xp, int idx) +{ + init_users(); + if (idx < ga_users.ga_len) { + return ((char_u **)ga_users.ga_data)[idx]; + } + return NULL; +} + +/// Check whether name matches a user name. +/// +/// @return 0 if name does not match any user name. +/// 1 if name partially matches the beginning of a user name. +/// 2 is name fully matches a user name. +int match_user(char_u *name) +{ + int n = (int)STRLEN(name); + int result = 0; + + init_users(); + for (int i = 0; i < ga_users.ga_len; i++) { + if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0) { + return 2; // full match + } + if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0) { + result = 1; // partial match + } + } + return result; +} |