Avoid code duplication

We can use girara_list_foreach here and avoid the repeated
GIRARA_LIST_FOREACH_BODY overhead.

Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
This commit is contained in:
Sebastian Ramacher 2018-02-26 22:45:05 +01:00
parent 90533f6773
commit 5baa31b83d
5 changed files with 81 additions and 49 deletions

View file

@ -120,6 +120,15 @@ error_free:
return NULL;
}
static void
group_add_element(void* data, void* userdata)
{
const char* element = data;
girara_completion_group_t* group = userdata;
girara_completion_group_add_element(group, element, NULL);
}
static girara_completion_t*
list_files_for_cc(zathura_t* zathura, const char* input, bool check_file_ext, int show_recent)
{
@ -184,9 +193,7 @@ list_files_for_cc(zathura_t* zathura, const char* input, bool check_file_ext, in
goto error_free;
}
GIRARA_LIST_FOREACH_BODY(names, const char*, file,
girara_completion_group_add_element(group, file, NULL);
);
girara_list_foreach(names, group_add_element, group);
girara_list_free(names);
}
@ -197,10 +204,7 @@ list_files_for_cc(zathura_t* zathura, const char* input, bool check_file_ext, in
}
if (girara_list_size(recent_files) != 0) {
GIRARA_LIST_FOREACH_BODY(recent_files, const char*, file,
girara_debug("adding %s (recent file)", file);
girara_completion_group_add_element(history_group, file, NULL);
);
girara_list_foreach(recent_files, group_add_element, history_group);
girara_list_free(recent_files);
} else {
girara_completion_group_free(history_group);

View file

@ -511,22 +511,28 @@ plain_load_jumplist(zathura_database_t* db, const char* file)
return list;
}
static void
jump_to_str(void* data, void* userdata)
{
const zathura_jump_t* jump = data;
GString* str_val = userdata;
char buffer[G_ASCII_DTOSTR_BUF_SIZE] = { '\0' };
g_string_append_printf(str_val, "%d ", jump->page);
g_string_append(str_val, g_ascii_dtostr(buffer, G_ASCII_DTOSTR_BUF_SIZE, jump->x));
g_string_append_c(str_val, ' ');
g_string_append(str_val, g_ascii_dtostr(buffer, G_ASCII_DTOSTR_BUF_SIZE, jump->y));
g_string_append_c(str_val, ' ');
}
static bool
plain_save_jumplist(zathura_database_t* db, const char* file, girara_list_t* jumplist)
{
g_return_val_if_fail(db != NULL && file != NULL && jumplist != NULL, false);
GString* str_val = g_string_new(NULL);
GIRARA_LIST_FOREACH_BODY(jumplist, zathura_jump_t*, jump,
char buffer[G_ASCII_DTOSTR_BUF_SIZE] = { '\0' };
g_string_append_printf(str_val, "%d ", jump->page);
g_string_append(str_val, g_ascii_dtostr(buffer, G_ASCII_DTOSTR_BUF_SIZE, jump->x));
g_string_append_c(str_val, ' ');
g_string_append(str_val, g_ascii_dtostr(buffer, G_ASCII_DTOSTR_BUF_SIZE, jump->y));
g_string_append_c(str_val, ' ');
);
girara_list_foreach(jumplist, jump_to_str, str_val);
zathura_plaindatabase_private_t* priv = ZATHURA_PLAINDATABASE_GET_PRIVATE(db);

View file

@ -174,6 +174,24 @@ load_plugin(zathura_plugin_manager_t* plugin_manager, const char* plugindir, con
}
}
static void
load_dir(void* data, void* userdata)
{
const char* plugindir = data;
zathura_plugin_manager_t* plugin_manager = userdata;
GDir* dir = g_dir_open(plugindir, 0, NULL);
if (dir == NULL) {
girara_error("could not open plugin directory: %s", plugindir);
} else {
const char* name = NULL;
while ((name = g_dir_read_name(dir)) != NULL) {
load_plugin(plugin_manager, plugindir, name);
}
g_dir_close(dir);
}
}
void
zathura_plugin_manager_load(zathura_plugin_manager_t* plugin_manager)
{
@ -181,19 +199,8 @@ zathura_plugin_manager_load(zathura_plugin_manager_t* plugin_manager)
return;
}
GIRARA_LIST_FOREACH_BODY(plugin_manager->path, char*, plugindir,
/* read all files in the plugin directory */
GDir* dir = g_dir_open(plugindir, 0, NULL);
if (dir == NULL) {
girara_error("could not open plugin directory: %s", plugindir);
} else {
const char* name = NULL;
while ((name = g_dir_read_name(dir)) != NULL) {
load_plugin(plugin_manager, plugindir, name);
}
g_dir_close(dir);
}
);
/* read all files in the plugin directory */
girara_list_foreach(plugin_manager->path, load_dir, plugin_manager);
}
zathura_plugin_t*

View file

@ -320,6 +320,19 @@ synctex_highlight_rects(zathura_t* zathura, unsigned int page,
zathura_jumplist_add(zathura);
}
static void
dup_and_append_rect(void* data, void* userdata)
{
const synctex_page_rect_t* rect = data;
girara_list_t** all_rectangles = userdata;
zathura_rectangle_t* newrect = g_try_malloc0(sizeof(zathura_rectangle_t));
if (newrect != NULL) {
*newrect = rect->rect;
girara_list_append(all_rectangles[rect->page], newrect);
}
}
bool
synctex_view(zathura_t* zathura, const char* input_file,
unsigned int line, unsigned int column)
@ -355,13 +368,7 @@ synctex_view(zathura_t* zathura, const char* input_file,
}
if (secondary_rects != NULL) {
GIRARA_LIST_FOREACH_BODY(secondary_rects, synctex_page_rect_t*, rect,
zathura_rectangle_t* newrect = g_try_malloc0(sizeof(zathura_rectangle_t));
if (newrect != NULL) {
*newrect = rect->rect;
girara_list_append(all_rectangles[rect->page], newrect);
}
);
girara_list_foreach(secondary_rects, dup_and_append_rect, all_rectangles);
}
synctex_highlight_rects(zathura, page, all_rectangles);

View file

@ -574,6 +574,23 @@ zathura_set_cache_dir(zathura_t* zathura, const char* dir)
}
}
static void
add_dir(void* data, void* userdata)
{
const char* path = data;
zathura_plugin_manager_t* plugin_manager = userdata;
zathura_plugin_manager_add_dir(plugin_manager, path);
}
static void
set_plugin_dir(zathura_t* zathura, const char* dir)
{
girara_list_t* paths = girara_split_path_array(dir);
girara_list_foreach(paths, add_dir, zathura->plugins.manager);
girara_list_free(paths);
}
void
zathura_set_plugin_dir(zathura_t* zathura, const char* dir)
{
@ -581,21 +598,12 @@ zathura_set_plugin_dir(zathura_t* zathura, const char* dir)
g_return_if_fail(zathura->plugins.manager != NULL);
if (dir != NULL) {
girara_list_t* paths = girara_split_path_array(dir);
GIRARA_LIST_FOREACH_BODY(paths, char*, path,
zathura_plugin_manager_add_dir(zathura->plugins.manager, path);
);
girara_list_free(paths);
} else {
set_plugin_dir(zathura, dir);
#ifdef ZATHURA_PLUGINDIR
girara_list_t* paths = girara_split_path_array(ZATHURA_PLUGINDIR);
GIRARA_LIST_FOREACH_BODY(paths, char*, path,
zathura_plugin_manager_add_dir(zathura->plugins.manager, path);
);
girara_list_free(paths);
} else {
set_plugin_dir(zathura, ZATHURA_PLUGINDIR);
#endif
}
}
void