Began to implement cmd_open completion

This commit is contained in:
Moritz Lipp 2011-05-25 00:24:43 +02:00
parent 7a0f870ff1
commit bd37f6333e
5 changed files with 102 additions and 0 deletions

View file

@ -41,6 +41,12 @@ cmd_info(girara_session_t* session, girara_list_t* argument_list)
return true; return true;
} }
bool
cmd_open(girara_session_t* session, girara_list_t* argument_list)
{
return true;
}
bool bool
cmd_print(girara_session_t* session, girara_list_t* argument_list) cmd_print(girara_session_t* session, girara_list_t* argument_list)
{ {

View file

@ -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); 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 * Print the current file
* *

View file

@ -63,3 +63,79 @@ cc_print(girara_session_t* session, char* input)
return completion; 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;
}

View file

@ -14,4 +14,14 @@
*/ */
girara_completion_t* cc_print(girara_session_t* session, char* input); 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 #endif // COMPLETION_H

View file

@ -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, "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, "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, "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, "print", NULL, cmd_print, cc_print, "Print document");
girara_inputbar_command_add(gsession, "save", NULL, cmd_save, NULL, "Save document"); girara_inputbar_command_add(gsession, "save", NULL, cmd_save, NULL, "Save document");