From 58e25e8733107f68aaf92365d4b3cbd81d89392e Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Fri, 13 Jan 2012 18:00:12 +0100 Subject: [PATCH] sort files in :open --- completion.c | 96 +++++++++++++++++++++++++++++++--------------------- document.h | 2 -- 2 files changed, 58 insertions(+), 40 deletions(-) diff --git a/completion.c b/completion.c index 16f3474..fd513bc 100644 --- a/completion.c +++ b/completion.c @@ -15,6 +15,57 @@ #include #include +static girara_list_t* +list_files(zathura_t* zathura, const char* current_path, const char* current_file, int current_file_length, bool is_dir) +{ + /* read directory */ + GDir* dir = g_dir_open(current_path, 0, NULL); + if (dir == NULL) { + return NULL; + } + + girara_list_t* res = girara_sorted_list_new2((girara_compare_function_t)g_strcmp0, + (girara_free_function_t)g_free); + + /* read files */ + char* name = NULL; + while ((name = (char*) g_dir_read_name(dir)) != NULL) { + char* e_name = g_filename_display_name(name); + int e_length = strlen(e_name); + + if (e_name == NULL) { + continue; + } + + if ((current_file_length > e_length) || strncmp(current_file, e_name, + current_file_length)) { + g_free(e_name); + continue; + } + + char* full_path = g_strdup_printf("%s%s%s", current_path, is_dir ? "" : "/", e_name); + if (full_path == NULL) { + g_free(e_name); + continue; + } + + if (g_file_test(full_path, G_FILE_TEST_IS_DIR) == true) { + char* tmp_path = full_path; + full_path = g_strdup_printf("%s/", full_path); + g_free(tmp_path); + girara_list_append(res, full_path); + } else if (file_valid_extension(zathura, full_path) == true) { + girara_list_append(res, full_path); + } else { + g_free(full_path); + } + g_free(e_name); + } + + g_dir_close(dir); + return res; +} + girara_completion_t* cc_open(girara_session_t* session, const char* input) { @@ -87,50 +138,19 @@ cc_open(girara_session_t* session, const char* input) /* read directory */ if (g_file_test(current_path, G_FILE_TEST_IS_DIR) == TRUE) { - GDir* dir = g_dir_open(current_path, 0, NULL); - if (dir == NULL) { + girara_list_t* names = list_files(zathura, current_path, current_file, current_file_length, is_dir); + if (!names) { goto error_free; } - /* read files */ - char* name = NULL; - while ((name = (char*) g_dir_read_name(dir)) != NULL) { - char* e_name = g_filename_display_name(name); - int e_length = strlen(e_name); - - if (e_name == NULL) { - goto error_free; - } - - if ((current_file_length > e_length) || strncmp(current_file, e_name, - current_file_length)) { - g_free(e_name); - continue; - } - - char* full_path = g_strdup_printf("%s%s%s", current_path, is_dir ? "" : "/", e_name); - if (full_path == NULL) { - g_free(e_name); - goto error_free; - } - - if (g_file_test(full_path, G_FILE_TEST_IS_DIR) == true) { - char* tmp_path = full_path; - full_path = g_strdup_printf("%s/", full_path); - g_free(tmp_path); - girara_completion_group_add_element(group, full_path, NULL); - } else if (file_valid_extension(zathura, full_path) == true) { - girara_completion_group_add_element(group, full_path, NULL); - } - - g_free(full_path); - g_free(e_name); - } - - g_dir_close(dir); + GIRARA_LIST_FOREACH(names, const char*, iter, file) + girara_completion_group_add_element(group, file, NULL); + GIRARA_LIST_FOREACH_END(names, const char*, iter, file); + girara_list_free(names); } g_free(path); + g_free(current_path); girara_completion_add_group(completion, group); diff --git a/document.h b/document.h index 84d6a5c..d9487f8 100644 --- a/document.h +++ b/document.h @@ -12,8 +12,6 @@ #define PLUGIN_REGISTER_FUNCTION "plugin_register" -// typedef struct zathura_document_s zathura_document_t; - typedef bool (*zathura_document_open_t)(zathura_document_t* document); /**