diff --git a/commands.c b/commands.c index a8450b4..bb14888 100644 --- a/commands.c +++ b/commands.c @@ -41,6 +41,12 @@ cmd_info(girara_session_t* session, girara_list_t* argument_list) return true; } +bool +cmd_open(girara_session_t* session, girara_list_t* argument_list) +{ + return true; +} + bool cmd_print(girara_session_t* session, girara_list_t* argument_list) { diff --git a/commands.h b/commands.h index fa6d8eb..d174455 100644 --- a/commands.h +++ b/commands.h @@ -51,6 +51,15 @@ bool cmd_close(girara_session_t* session, girara_list_t* argument_list); */ bool cmd_info(girara_session_t* session, girara_list_t* argument_list); +/** + * Opens a document file + * + * @param session The used girara session + * @param argument_list List of passed arguments + * @return true if no error occured + */ +bool cmd_open(girara_session_t* session, girara_list_t* argument_list); + /** * Print the current file * diff --git a/completion.c b/completion.c index b51927e..4936767 100644 --- a/completion.c +++ b/completion.c @@ -63,3 +63,79 @@ cc_print(girara_session_t* session, char* input) return completion; } + +girara_completion_t* +cc_open(girara_session_t* session, char* input) +{ + girara_completion_t* completion = girara_completion_init(); + girara_completion_group_t* group = girara_completion_group_create(session, NULL); + + if (!session || !input || !completion || !group) { + goto error_free; + } + + gchar* path = girara_fix_path(input); + if (path == NULL || strlen(path) == 0) { + goto error_free; + } + + /* If the path does not begin with a slash we update the path with the current + * working directory */ + if (path[0] != '/') { + size_t path_max; +#ifdef PATH_MAX + path_max = PATH_MAX; +#else + path_max = pathconf(path,_PC_PATH_MAX); + if (path_max <= 0) + path_max = 4096; +#endif + + char cwd[path_max]; + getcwd(cwd, path_max); + + char* tmp_path = g_strdup_printf("%s/%s", cwd, path); + if (tmp_path == NULL) { + goto error_free; + } + + g_free(path); + path = tmp_path; + } + + /* read directory */ + if (g_file_test(path, G_FILE_TEST_IS_DIR) == TRUE) { + GDir* dir = g_dir_open(path, 0, NULL); + if (dir == NULL) { + 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); + g_free(e_name); + } + + g_dir_close(dir); + } + + g_free(path); + + girara_completion_add_group(completion, group); + + return completion; + +error_free: + + if (completion) { + girara_completion_free(completion); + } + if (group) { + girara_completion_group_free(group); + } + + g_free(path); + + return NULL; +} diff --git a/completion.h b/completion.h index 3baba94..81b7522 100644 --- a/completion.h +++ b/completion.h @@ -14,4 +14,14 @@ */ girara_completion_t* cc_print(girara_session_t* session, char* input); +/** + * Completion for the open command - Creates a list of accesible directories or + * files + * + * @param session The used girara session + * @param input The current input + * @return The completion object or NULL if an error occured + */ +girara_completion_t* cc_open(girara_session_t* session, char* input); + #endif // COMPLETION_H diff --git a/config.c b/config.c index 1b3d650..63607b9 100644 --- a/config.c +++ b/config.c @@ -111,6 +111,7 @@ config_load_default(zathura_t* zathura) girara_inputbar_command_add(gsession, "blist", NULL, cmd_bookmark_open, NULL, "List all bookmarks"); girara_inputbar_command_add(gsession, "close", NULL, cmd_close, NULL, "Close current file"); girara_inputbar_command_add(gsession, "info", NULL, cmd_info, NULL, "Show file information"); + girara_inputbar_command_add(gsession, "open", "o", cmd_open, cc_open, "Open document"); girara_inputbar_command_add(gsession, "print", NULL, cmd_print, cc_print, "Print document"); girara_inputbar_command_add(gsession, "save", NULL, cmd_save, NULL, "Save document");