2010-11-10 20:31:15 +01:00
|
|
|
/* See LICENSE file for license and copyright information */
|
2010-11-29 14:58:56 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2011-07-20 09:56:57 +02:00
|
|
|
#include <unistd.h>
|
2011-05-25 10:26:37 +02:00
|
|
|
#include <libgen.h>
|
2010-11-29 14:58:56 +01:00
|
|
|
|
2011-09-29 17:05:20 +02:00
|
|
|
#include "bookmarks.h"
|
2010-11-29 14:58:56 +01:00
|
|
|
#include "completion.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
2011-05-25 00:24:43 +02:00
|
|
|
girara_completion_t*
|
2011-10-08 23:19:28 +02:00
|
|
|
cc_open(girara_session_t* session, const char* input)
|
2011-05-25 00:24:43 +02:00
|
|
|
{
|
2011-05-25 00:54:16 +02:00
|
|
|
g_return_val_if_fail(session != NULL, NULL);
|
|
|
|
g_return_val_if_fail(session->global.data != NULL, NULL);
|
|
|
|
zathura_t* zathura = session->global.data;
|
|
|
|
|
2011-05-25 00:24:43 +02:00
|
|
|
girara_completion_t* completion = girara_completion_init();
|
|
|
|
girara_completion_group_t* group = girara_completion_group_create(session, NULL);
|
|
|
|
|
2011-05-26 14:58:10 +02:00
|
|
|
gchar* path = NULL;
|
|
|
|
gchar* current_path = NULL;
|
|
|
|
|
2011-05-25 10:26:37 +02:00
|
|
|
if (completion == NULL || group == NULL) {
|
2011-05-25 00:24:43 +02:00
|
|
|
goto error_free;
|
|
|
|
}
|
|
|
|
|
2011-05-26 14:58:10 +02:00
|
|
|
path = girara_fix_path(input);
|
2011-05-25 10:26:37 +02:00
|
|
|
if (path == NULL) {
|
2011-05-25 00:24:43 +02:00
|
|
|
goto error_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the path does not begin with a slash we update the path with the current
|
|
|
|
* working directory */
|
2011-05-25 10:26:37 +02:00
|
|
|
if (strlen(path) == 0 || path[0] != '/') {
|
2011-10-05 14:20:59 +02:00
|
|
|
long path_max;
|
2011-05-25 00:24:43 +02:00
|
|
|
#ifdef PATH_MAX
|
|
|
|
path_max = PATH_MAX;
|
|
|
|
#else
|
|
|
|
path_max = pathconf(path,_PC_PATH_MAX);
|
2011-10-05 14:20:59 +02:00
|
|
|
if (path_max <= 0)
|
2011-05-25 00:24:43 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-05-25 10:26:37 +02:00
|
|
|
/* Append a slash if the given argument is a directory */
|
2011-05-26 14:58:10 +02:00
|
|
|
bool is_dir = (path[strlen(path) - 1] == '/') ? true : false;
|
|
|
|
if ((g_file_test(path, G_FILE_TEST_IS_DIR) == TRUE) && !is_dir) {
|
2011-05-25 10:26:37 +02:00
|
|
|
char* tmp_path = g_strdup_printf("%s/", path);
|
|
|
|
if (tmp_path == NULL) {
|
|
|
|
goto error_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(path);
|
|
|
|
path = tmp_path;
|
2011-05-26 14:58:10 +02:00
|
|
|
is_dir = true;
|
2011-05-25 10:26:37 +02:00
|
|
|
}
|
|
|
|
|
2011-05-26 14:58:10 +02:00
|
|
|
/* get current path */
|
|
|
|
char* tmp = g_strdup(path);
|
|
|
|
current_path = is_dir ? g_strdup(tmp) : g_strdup(dirname(tmp));
|
|
|
|
g_free(tmp);
|
|
|
|
|
|
|
|
/* get current file */
|
|
|
|
gchar* current_file = is_dir ? "" : basename(path);
|
2011-05-25 10:26:37 +02:00
|
|
|
int current_file_length = strlen(current_file);
|
|
|
|
|
2011-05-25 00:24:43 +02:00
|
|
|
/* read directory */
|
2011-05-25 10:26:37 +02:00
|
|
|
if (g_file_test(current_path, G_FILE_TEST_IS_DIR) == TRUE) {
|
|
|
|
GDir* dir = g_dir_open(current_path, 0, NULL);
|
2011-05-25 00:24:43 +02:00
|
|
|
if (dir == NULL) {
|
|
|
|
goto error_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read files */
|
|
|
|
char* name = NULL;
|
|
|
|
while ((name = (char*) g_dir_read_name(dir)) != NULL) {
|
2011-05-25 10:26:37 +02:00
|
|
|
char* e_name = g_filename_display_name(name);
|
|
|
|
int e_length = strlen(e_name);
|
|
|
|
|
2011-05-25 00:54:16 +02:00
|
|
|
if (e_name == NULL) {
|
|
|
|
goto error_free;
|
|
|
|
}
|
|
|
|
|
2011-05-26 14:58:10 +02:00
|
|
|
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);
|
2011-05-25 00:54:16 +02:00
|
|
|
if (full_path == NULL) {
|
2011-05-25 10:26:37 +02:00
|
|
|
g_free(e_name);
|
2011-05-25 00:54:16 +02:00
|
|
|
goto error_free;
|
|
|
|
}
|
|
|
|
|
2011-05-25 10:26:37 +02:00
|
|
|
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) {
|
2011-05-25 00:54:16 +02:00
|
|
|
girara_completion_group_add_element(group, full_path, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(full_path);
|
2011-05-25 00:24:43 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-05-26 14:58:10 +02:00
|
|
|
g_free(current_path);
|
2011-05-25 00:24:43 +02:00
|
|
|
g_free(path);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-09-29 17:05:20 +02:00
|
|
|
|
|
|
|
girara_completion_t*
|
2011-10-08 23:19:28 +02:00
|
|
|
cc_bookmarks(girara_session_t* session, const char* input)
|
2011-09-29 17:05:20 +02:00
|
|
|
{
|
|
|
|
g_return_val_if_fail(session != NULL, NULL);
|
|
|
|
g_return_val_if_fail(session->global.data != NULL, NULL);
|
|
|
|
zathura_t* zathura = session->global.data;
|
|
|
|
|
|
|
|
girara_completion_t* completion = girara_completion_init();
|
|
|
|
girara_completion_group_t* group = girara_completion_group_create(session, NULL);
|
|
|
|
|
2011-10-12 12:49:52 +02:00
|
|
|
if (completion == NULL || group == NULL) {
|
|
|
|
goto error_free;
|
|
|
|
}
|
|
|
|
|
2011-09-29 17:05:20 +02:00
|
|
|
const size_t input_length = input ? strlen(input) : 0;
|
|
|
|
GIRARA_LIST_FOREACH(zathura->bookmarks.bookmarks, zathura_bookmark_t*, iter, bookmark)
|
|
|
|
if (input_length <= strlen(bookmark->id) && !strncmp(input, bookmark->id, input_length)) {
|
|
|
|
gchar* paged = g_strdup_printf("Page %d", bookmark->page);
|
|
|
|
girara_completion_group_add_element(group, bookmark->id, paged);
|
|
|
|
g_free(paged);
|
|
|
|
}
|
|
|
|
GIRARA_LIST_FOREACH_END(zathura->bookmarks.bookmarks, zathura_bookmark_t*, iter, bookmark)
|
|
|
|
|
2011-10-12 12:49:52 +02:00
|
|
|
girara_completion_add_group(completion, group);
|
|
|
|
|
2011-09-29 17:05:20 +02:00
|
|
|
return completion;
|
2011-10-12 12:49:52 +02:00
|
|
|
|
|
|
|
error_free:
|
|
|
|
|
|
|
|
if (completion) {
|
|
|
|
girara_completion_free(completion);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (group) {
|
|
|
|
girara_completion_group_free(group);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2011-09-29 17:05:20 +02:00
|
|
|
}
|