diff options
author | James Booth <boothj5@gmail.com> | 2016-10-10 22:28:23 +0100 |
---|---|---|
committer | James Booth <boothj5@gmail.com> | 2016-10-10 22:28:23 +0100 |
commit | dcc2123ec4d2210c8631b181cae9d817504e8b48 (patch) | |
tree | a8bb9c746aa6b2739e0ef6af9ce01f9966bff1bd /src/command | |
parent | d485588a07f9d4a477a7615fee926fcfee44b29e (diff) | |
download | profani-tty-dcc2123ec4d2210c8631b181cae9d817504e8b48.tar.gz |
Allow filepath autocompletion in plugins
closes #858
Diffstat (limited to 'src/command')
-rw-r--r-- | src/command/cmd_ac.c | 219 | ||||
-rw-r--r-- | src/command/cmd_ac.h | 2 |
2 files changed, 111 insertions, 110 deletions
diff --git a/src/command/cmd_ac.c b/src/command/cmd_ac.c index 47655114..6d87a5d8 100644 --- a/src/command/cmd_ac.c +++ b/src/command/cmd_ac.c @@ -57,8 +57,6 @@ #include "pgp/gpg.h" #endif -static char* _complete_filepath(const char *const input, char *const startstr); - static char* _sub_autocomplete(ProfWin *window, const char *const input); static char* _notify_autocomplete(ProfWin *window, const char *const input); static char* _theme_autocomplete(ProfWin *window, const char *const input); @@ -1140,6 +1138,113 @@ cmd_ac_uninit(void) autocomplete_free(winpos_ac); } +char* +cmd_ac_complete_filepath(const char *const input, char *const startstr) +{ + static char* last_directory = NULL; + + unsigned int output_off = 0; + + char *result = NULL; + char *tmp; + + // strip command + char *inpcp = (char*)input + strlen(startstr); + while (*inpcp == ' ') { + inpcp++; + } + + inpcp = strdup(inpcp); + + // strip quotes + if (*inpcp == '"') { + tmp = strchr(inpcp+1, '"'); + if (tmp) { + *tmp = '\0'; + } + tmp = strdup(inpcp+1); + free(inpcp); + inpcp = tmp; + } + + // expand ~ to $HOME + if (inpcp[0] == '~' && inpcp[1] == '/') { + if (asprintf(&tmp, "%s/%sfoo", getenv("HOME"), inpcp+2) == -1) { + return NULL; + } + output_off = strlen(getenv("HOME"))+1; + } else { + if (asprintf(&tmp, "%sfoo", inpcp) == -1) { + return NULL; + } + } + free(inpcp); + inpcp = tmp; + + char* inpcp2 = strdup(inpcp); + char* foofile = strdup(basename(inpcp2)); + char* directory = strdup(dirname(inpcp)); + free(inpcp); + free(inpcp2); + + if (!last_directory || strcmp(last_directory, directory) != 0) { + free(last_directory); + last_directory = directory; + autocomplete_reset(filepath_ac); + + struct dirent *dir; + + DIR *d = opendir(directory); + if (d) { + while ((dir = readdir(d)) != NULL) { + if (strcmp(dir->d_name, ".") == 0) { + continue; + } else if (strcmp(dir->d_name, "..") == 0) { + continue; + } else if (*(dir->d_name) == '.' && *foofile != '.') { + // only show hidden files on explicit request + continue; + } + char * acstring; + if (output_off) { + if (asprintf(&tmp, "%s/%s", directory, dir->d_name) == -1) { + free(foofile); + return NULL; + } + if (asprintf(&acstring, "~/%s", tmp+output_off) == -1) { + free(foofile); + return NULL; + } + free(tmp); + } else if (strcmp(directory, "/") == 0) { + if (asprintf(&acstring, "/%s", dir->d_name) == -1) { + free(foofile); + return NULL; + } + } else { + if (asprintf(&acstring, "%s/%s", directory, dir->d_name) == -1) { + free(foofile); + return NULL; + } + } + autocomplete_add(filepath_ac, acstring); + free(acstring); + } + closedir(d); + } + } else { + free(directory); + } + free(foofile); + + result = autocomplete_param_with_ac(input, startstr, filepath_ac, TRUE); + if (result) { + return result; + } + + return NULL; +} + static char* _cmd_ac_complete_params(ProfWin *window, const char *const input) { @@ -1918,7 +2023,7 @@ _plugins_autocomplete(ProfWin *window, const char *const input) char *result = NULL; if (strncmp(input, "/plugins install ", 17) == 0) { - return _complete_filepath(input, "/plugins install"); + return cmd_ac_complete_filepath(input, "/plugins install"); } if (strncmp(input, "/plugins load ", 14) == 0) { @@ -2736,7 +2841,7 @@ _close_autocomplete(ProfWin *window, const char *const input) static char* _sendfile_autocomplete(ProfWin *window, const char *const input) { - return _complete_filepath(input, "/sendfile"); + return cmd_ac_complete_filepath(input, "/sendfile"); } static char* @@ -2945,109 +3050,3 @@ _presence_autocomplete(ProfWin *window, const char *const input) return NULL; } -static char* -_complete_filepath(const char *const input, char *const startstr) -{ - static char* last_directory = NULL; - - unsigned int output_off = 0; - - char *result = NULL; - char *tmp; - - // strip command - char *inpcp = (char*)input + strlen(startstr); - while (*inpcp == ' ') { - inpcp++; - } - - inpcp = strdup(inpcp); - - // strip quotes - if (*inpcp == '"') { - tmp = strchr(inpcp+1, '"'); - if (tmp) { - *tmp = '\0'; - } - tmp = strdup(inpcp+1); - free(inpcp); - inpcp = tmp; - } - - // expand ~ to $HOME - if (inpcp[0] == '~' && inpcp[1] == '/') { - if (asprintf(&tmp, "%s/%sfoo", getenv("HOME"), inpcp+2) == -1) { - return NULL; - } - output_off = strlen(getenv("HOME"))+1; - } else { - if (asprintf(&tmp, "%sfoo", inpcp) == -1) { - return NULL; - } - } - free(inpcp); - inpcp = tmp; - - char* inpcp2 = strdup(inpcp); - char* foofile = strdup(basename(inpcp2)); - char* directory = strdup(dirname(inpcp)); - free(inpcp); - free(inpcp2); - - if (!last_directory || strcmp(last_directory, directory) != 0) { - free(last_directory); - last_directory = directory; - autocomplete_reset(filepath_ac); - - struct dirent *dir; - - DIR *d = opendir(directory); - if (d) { - while ((dir = readdir(d)) != NULL) { - if (strcmp(dir->d_name, ".") == 0) { - continue; - } else if (strcmp(dir->d_name, "..") == 0) { - continue; - } else if (*(dir->d_name) == '.' && *foofile != '.') { - // only show hidden files on explicit request - continue; - } - char * acstring; - if (output_off) { - if (asprintf(&tmp, "%s/%s", directory, dir->d_name) == -1) { - free(foofile); - return NULL; - } - if (asprintf(&acstring, "~/%s", tmp+output_off) == -1) { - free(foofile); - return NULL; - } - free(tmp); - } else if (strcmp(directory, "/") == 0) { - if (asprintf(&acstring, "/%s", dir->d_name) == -1) { - free(foofile); - return NULL; - } - } else { - if (asprintf(&acstring, "%s/%s", directory, dir->d_name) == -1) { - free(foofile); - return NULL; - } - } - autocomplete_add(filepath_ac, acstring); - free(acstring); - } - closedir(d); - } - } else { - free(directory); - } - free(foofile); - - result = autocomplete_param_with_ac(input, startstr, filepath_ac, TRUE); - if (result) { - return result; - } - - return NULL; -} diff --git a/src/command/cmd_ac.h b/src/command/cmd_ac.h index 3d1069bc..78aec927 100644 --- a/src/command/cmd_ac.h +++ b/src/command/cmd_ac.h @@ -57,4 +57,6 @@ void cmd_ac_remove_alias_value(char *value); void cmd_ac_add_form_fields(DataForm *form); void cmd_ac_remove_form_fields(DataForm *form); +char* cmd_ac_complete_filepath(const char *const input, char *const startstr); + #endif |