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; 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* static girara_completion_t*
list_files_for_cc(zathura_t* zathura, const char* input, bool check_file_ext, int show_recent) 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; goto error_free;
} }
GIRARA_LIST_FOREACH_BODY(names, const char*, file, girara_list_foreach(names, group_add_element, group);
girara_completion_group_add_element(group, file, NULL);
);
girara_list_free(names); 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) { if (girara_list_size(recent_files) != 0) {
GIRARA_LIST_FOREACH_BODY(recent_files, const char*, file, girara_list_foreach(recent_files, group_add_element, history_group);
girara_debug("adding %s (recent file)", file);
girara_completion_group_add_element(history_group, file, NULL);
);
girara_list_free(recent_files); girara_list_free(recent_files);
} else { } else {
girara_completion_group_free(history_group); girara_completion_group_free(history_group);

View file

@ -511,22 +511,28 @@ plain_load_jumplist(zathura_database_t* db, const char* file)
return list; 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 static bool
plain_save_jumplist(zathura_database_t* db, const char* file, girara_list_t* jumplist) 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); g_return_val_if_fail(db != NULL && file != NULL && jumplist != NULL, false);
GString* str_val = g_string_new(NULL); GString* str_val = g_string_new(NULL);
girara_list_foreach(jumplist, jump_to_str, str_val);
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, ' ');
);
zathura_plaindatabase_private_t* priv = ZATHURA_PLAINDATABASE_GET_PRIVATE(db); 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 void
zathura_plugin_manager_load(zathura_plugin_manager_t* plugin_manager) 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; return;
} }
GIRARA_LIST_FOREACH_BODY(plugin_manager->path, char*, plugindir, /* read all files in the plugin directory */
/* read all files in the plugin directory */ girara_list_foreach(plugin_manager->path, load_dir, plugin_manager);
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);
}
);
} }
zathura_plugin_t* zathura_plugin_t*

View file

@ -320,6 +320,19 @@ synctex_highlight_rects(zathura_t* zathura, unsigned int page,
zathura_jumplist_add(zathura); 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 bool
synctex_view(zathura_t* zathura, const char* input_file, synctex_view(zathura_t* zathura, const char* input_file,
unsigned int line, unsigned int column) unsigned int line, unsigned int column)
@ -355,13 +368,7 @@ synctex_view(zathura_t* zathura, const char* input_file,
} }
if (secondary_rects != NULL) { if (secondary_rects != NULL) {
GIRARA_LIST_FOREACH_BODY(secondary_rects, synctex_page_rect_t*, rect, girara_list_foreach(secondary_rects, dup_and_append_rect, all_rectangles);
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);
}
);
} }
synctex_highlight_rects(zathura, page, 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 void
zathura_set_plugin_dir(zathura_t* zathura, const char* dir) 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); g_return_if_fail(zathura->plugins.manager != NULL);
if (dir != NULL) { if (dir != NULL) {
girara_list_t* paths = girara_split_path_array(dir); set_plugin_dir(zathura, dir);
GIRARA_LIST_FOREACH_BODY(paths, char*, path,
zathura_plugin_manager_add_dir(zathura->plugins.manager, path);
);
girara_list_free(paths);
} else {
#ifdef ZATHURA_PLUGINDIR #ifdef ZATHURA_PLUGINDIR
girara_list_t* paths = girara_split_path_array(ZATHURA_PLUGINDIR); } else {
GIRARA_LIST_FOREACH_BODY(paths, char*, path, set_plugin_dir(zathura, ZATHURA_PLUGINDIR);
zathura_plugin_manager_add_dir(zathura->plugins.manager, path);
);
girara_list_free(paths);
#endif #endif
} }
} }
void void